• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 15430420074

03 Jun 2025 10:29PM UTC coverage: 72.013% (-0.03%) from 72.041%
15430420074

push

github

yuwata
doc: fix integration tests guide reference

299598 of 416033 relevant lines covered (72.01%)

700977.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.56
/src/udev/udev-node.c
1
/* SPDX-License-Identifier: GPL-2.0-or-later */
2

3
#include <sys/file.h>
4
#include <sys/stat.h>
5
#include <unistd.h>
6

7
#include "sd-id128.h"
8

9
#include "alloc-util.h"
10
#include "device-private.h"
11
#include "device-util.h"
12
#include "devnum-util.h"
13
#include "dirent-util.h"
14
#include "errno-util.h"
15
#include "escape.h"
16
#include "fd-util.h"
17
#include "fileio.h"
18
#include "format-util.h"
19
#include "fs-util.h"
20
#include "hashmap.h"
21
#include "hexdecoct.h"
22
#include "label-util.h"
23
#include "mkdir-label.h"
24
#include "parse-util.h"
25
#include "path-util.h"
26
#include "selinux-util.h"
27
#include "siphash24.h"
28
#include "smack-util.h"
29
#include "string-util.h"
30
#include "strv.h"
31
#include "udev-node.h"
32
#include "user-util.h"
33

34
#define UDEV_NODE_HASH_KEY SD_ID128_MAKE(b9,6a,f1,ce,40,31,44,1a,9e,19,ec,8b,ae,f3,e3,2f)
35

36
static int node_remove_symlink(sd_device *dev, const char *slink) {
96,888✔
37
        assert(dev);
96,888✔
38
        assert(slink);
96,888✔
39

40
        if (unlink(slink) < 0 && errno != ENOENT)
96,888✔
41
                return log_device_debug_errno(dev, errno, "Failed to remove '%s': %m", slink);
×
42

43
        (void) rmdir_parents(slink, "/dev");
96,888✔
44
        return 0;
96,888✔
45
}
46

47
static int node_create_symlink(sd_device *dev, const char *devnode, const char *slink) {
157,052✔
48
        struct stat st;
157,052✔
49
        int r;
157,052✔
50

51
        assert(dev);
157,052✔
52
        assert(slink);
157,052✔
53

54
        if (!devnode) {
157,052✔
55
                r = sd_device_get_devname(dev, &devnode);
53,684✔
56
                if (r < 0)
53,684✔
57
                        return log_device_debug_errno(dev, r, "Failed to get device node: %m");
×
58
        }
59

60
        if (lstat(slink, &st) >= 0) {
157,052✔
61
                if (!S_ISLNK(st.st_mode))
39,351✔
62
                        return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EEXIST),
×
63
                                                      "Conflicting inode '%s' found, symlink to '%s' will not be created.",
64
                                                      slink, devnode);
65
        } else if (errno != ENOENT)
117,701✔
66
                return log_device_debug_errno(dev, errno, "Failed to lstat() '%s': %m", slink);
×
67

68
        r = mkdir_parents_label(slink, 0755);
157,052✔
69
        if (r < 0)
157,052✔
70
                return log_device_debug_errno(dev, r, "Failed to create parent directory of '%s': %m", slink);
×
71

72
        /* use relative link */
73
        r = symlink_atomic_full_label(devnode, slink, /* make_relative = */ true);
157,052✔
74
        if (r < 0)
157,052✔
75
                return log_device_debug_errno(dev, r, "Failed to create symlink '%s' to '%s': %m", slink, devnode);
×
76

77
        log_device_debug(dev, "Successfully created symlink '%s' to '%s'", slink, devnode);
159,004✔
78
        return 0;
157,052✔
79
}
80

81
static int stack_directory_read_one(int dirfd, const char *id, char **devnode, int *priority) {
184,793✔
82
        _cleanup_free_ char *buf = NULL;
184,793✔
83
        int tmp_prio, r;
184,793✔
84

85
        assert(dirfd >= 0);
184,793✔
86
        assert(id);
184,793✔
87
        assert(priority);
184,793✔
88

89
        /* This reads priority and device node from the symlink under /run/udev/links (or udev database).
90
         * If 'devnode' is NULL, obtained priority is always set to '*priority'. If 'devnode' is non-NULL,
91
         * this updates '*devnode' and '*priority'. */
92

93
        /* First, let's try to read the entry with the new format, which should replace the old format pretty
94
         * quickly. */
95
        r = readlinkat_malloc(dirfd, id, &buf);
184,793✔
96
        if (r >= 0) {
184,793✔
97
                char *colon;
178,331✔
98

99
                /* With the new format, the devnode and priority can be obtained from symlink itself. */
100

101
                colon = strchr(buf, ':');
178,331✔
102
                if (!colon || colon == buf)
178,331✔
103
                        return -EINVAL;
104

105
                *colon = '\0';
178,331✔
106

107
                /* Of course, this check is racy, but it is not necessary to be perfect. Even if the device
108
                 * node will be removed after this check, we will receive 'remove' uevent, and the invalid
109
                 * symlink will be removed during processing the event. The check is just for shortening the
110
                 * timespan that the symlink points to a non-existing device node. */
111
                if (access(colon + 1, F_OK) < 0)
178,331✔
112
                        return -ENODEV;
113

114
                r = safe_atoi(buf, &tmp_prio);
46,846✔
115
                if (r < 0)
46,846✔
116
                        return r;
117

118
                if (!devnode)
46,846✔
119
                        goto finalize;
39,897✔
120

121
                if (*devnode && tmp_prio <= *priority)
6,949✔
122
                        return 0; /* Unchanged */
123

124
                r = free_and_strdup(devnode, colon + 1);
1,192✔
125
                if (r < 0)
1,192✔
126
                        return r;
127

128
        } else if (r == -EINVAL) { /* Not a symlink ? try the old format */
6,462✔
129
                _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
×
130
                const char *val;
×
131

132
                /* Old format. The devnode and priority must be obtained from uevent and udev database. */
133

134
                r = sd_device_new_from_device_id(&dev, id);
×
135
                if (r < 0)
×
136
                        return r;
137

138
                r = device_get_devlink_priority(dev, &tmp_prio);
×
139
                if (r < 0)
×
140
                        return r;
141

142
                if (!devnode)
×
143
                        goto finalize;
×
144

145
                if (*devnode && tmp_prio <= *priority)
×
146
                        return 0; /* Unchanged */
147

148
                r = sd_device_get_devname(dev, &val);
×
149
                if (r < 0)
×
150
                        return r;
151

152
                r = free_and_strdup(devnode, val);
×
153
                if (r < 0)
×
154
                        return r;
155

156
        } else
157
                return r == -ENOENT ? -ENODEV : r;
6,462✔
158

159
finalize:
41,089✔
160
        *priority = tmp_prio;
41,089✔
161
        return 1; /* Updated */
41,089✔
162
}
163

164
static int stack_directory_find_prioritized_devnode(sd_device *dev, int dirfd, bool add, char **ret) {
184,760✔
165
        _cleanup_closedir_ DIR *dir = NULL;
184,760✔
166
        _cleanup_free_ char *devnode = NULL;
184,760✔
167
        int r, priority;
184,760✔
168
        const char *id;
184,760✔
169

170
        assert(dev);
184,760✔
171
        assert(dirfd >= 0);
184,760✔
172
        assert(ret);
184,760✔
173

174
        /* Find device node of device with highest priority. This returns 1 if a device found, 0 if no
175
         * device found, or a negative errno on error. */
176

177
        if (add) {
184,760✔
178
                const char *n;
102,186✔
179

180
                r = device_get_devlink_priority(dev, &priority);
102,186✔
181
                if (r < 0)
102,186✔
182
                        return r;
×
183

184
                r = sd_device_get_devname(dev, &n);
102,186✔
185
                if (r < 0)
102,186✔
186
                        return r;
187

188
                devnode = strdup(n);
102,186✔
189
                if (!devnode)
102,186✔
190
                        return -ENOMEM;
191
        }
192

193
        dir = xopendirat(dirfd, ".", O_NOFOLLOW);
184,760✔
194
        if (!dir)
184,760✔
195
                return -errno;
×
196

197
        r = sd_device_get_device_id(dev, &id);
184,760✔
198
        if (r < 0)
184,760✔
199
                return r;
200

201
        FOREACH_DIRENT(de, dir, break) {
794,900✔
202

203
                /* skip ourself */
204
                if (streq(de->d_name, id))
240,620✔
205
                        continue;
102,186✔
206

207
                r = stack_directory_read_one(dirfd, de->d_name, &devnode, &priority);
138,434✔
208
                if (r < 0 && r != -ENODEV)
138,434✔
209
                        log_debug_errno(r, "Failed to read '%s', ignoring: %m", de->d_name);
610,140✔
210
        }
211

212
        *ret = TAKE_PTR(devnode);
184,760✔
213
        return !!*ret;
184,760✔
214
}
215

216
static int stack_directory_update(sd_device *dev, int fd, bool add) {
245,131✔
217
        const char *id;
245,131✔
218
        int r;
245,131✔
219

220
        assert(dev);
245,131✔
221
        assert(fd >= 0);
245,131✔
222

223
        r = sd_device_get_device_id(dev, &id);
245,131✔
224
        if (r < 0)
245,131✔
225
                return r;
245,131✔
226

227
        if (add) {
245,131✔
228
                _cleanup_free_ char *data = NULL, *buf = NULL;
142,077✔
229
                const char *devname;
142,077✔
230
                int priority;
142,077✔
231

232
                r = sd_device_get_devname(dev, &devname);
142,077✔
233
                if (r < 0)
142,077✔
234
                        return r;
235

236
                r = device_get_devlink_priority(dev, &priority);
142,077✔
237
                if (r < 0)
142,077✔
238
                        return r;
239

240
                if (asprintf(&data, "%i:%s", priority, devname) < 0)
142,077✔
241
                        return -ENOMEM;
242

243
                if (readlinkat_malloc(fd, id, &buf) >= 0 && streq(buf, data))
142,077✔
244
                        return 0; /* Unchanged. */
245

246
                (void) unlinkat(fd, id, 0);
115,192✔
247

248
                if (symlinkat(data, fd, id) < 0)
115,192✔
249
                        return -errno;
×
250

251
        } else {
252
                if (unlinkat(fd, id, 0) < 0) {
103,054✔
253
                        if (errno == ENOENT)
×
254
                                return 0; /* Unchanged. */
255
                        return -errno;
×
256
                }
257
        }
258

259
        return 1; /* Updated. */
260
}
261

262
size_t udev_node_escape_path(const char *src, char *dest, size_t size) {
245,142✔
263
        size_t i, j;
245,142✔
264
        uint64_t h;
245,142✔
265

266
        assert(src);
245,142✔
267
        assert(dest);
245,142✔
268
        assert(size >= 12);
245,142✔
269

270
        for (i = 0, j = 0; src[i] != '\0'; i++) {
12,683,960✔
271
                if (src[i] == '/') {
12,438,821✔
272
                        if (j+4 >= size - 12 + 1)
639,726✔
273
                                goto toolong;
×
274
                        memcpy(&dest[j], "\\x2f", 4);
639,726✔
275
                        j += 4;
639,726✔
276
                } else if (src[i] == '\\') {
11,799,095✔
277
                        if (j+4 >= size - 12 + 1)
3,155✔
278
                                goto toolong;
×
279
                        memcpy(&dest[j], "\\x5c", 4);
3,155✔
280
                        j += 4;
3,155✔
281
                } else {
282
                        if (j+1 >= size - 12 + 1)
11,795,940✔
283
                                goto toolong;
3✔
284
                        dest[j] = src[i];
11,795,937✔
285
                        j++;
11,795,937✔
286
                }
287
        }
288
        dest[j] = '\0';
245,139✔
289
        return j;
245,139✔
290

291
toolong:
3✔
292
        /* If the input path is too long to encode as a filename, then let's suffix with a string
293
         * generated from the hash of the path. */
294

295
        h = siphash24_string(src, UDEV_NODE_HASH_KEY.bytes);
3✔
296

297
        for (unsigned k = 0; k <= 10; k++)
36✔
298
                dest[size - k - 2] = urlsafe_base64char((h >> (k * 6)) & 63);
33✔
299

300
        dest[size - 1] = '\0';
3✔
301
        return size - 1;
3✔
302
}
303

304
static int stack_directory_get_name(const char *slink, char **ret) {
245,131✔
305
        _cleanup_free_ char *s = NULL;
245,131✔
306
        char name_enc[NAME_MAX+1];
245,131✔
307
        const char *name;
245,131✔
308
        int r;
245,131✔
309

310
        assert(slink);
245,131✔
311
        assert(ret);
245,131✔
312

313
        r = path_simplify_alloc(slink, &s);
245,131✔
314
        if (r < 0)
245,131✔
315
                return r;
316

317
        if (!path_is_normalized(s))
245,131✔
318
                return -EINVAL;
319

320
        name = path_startswith(s, "/dev");
245,131✔
321
        if (empty_or_root(name))
245,131✔
322
                return -EINVAL;
323

324
        udev_node_escape_path(name, name_enc, sizeof(name_enc));
245,131✔
325

326
        return strdup_to(ret, name_enc);
245,131✔
327
}
328

329
static int stack_directory_open_and_lock(
245,131✔
330
                sd_device *dev,
331
                const char *slink,
332
                char **ret_dirpath,
333
                int *ret_dirfd,
334
                LockFile *ret_lockfile) {
335

336
        _cleanup_(release_lock_file) LockFile lockfile = LOCK_FILE_INIT;
×
337
        _cleanup_close_ int dirfd = -EBADF;
245,131✔
338
        _cleanup_free_ char *name = NULL, *dirpath = NULL, *lockname = NULL;
245,131✔
339
        int r;
245,131✔
340

341
        assert(dev);
245,131✔
342
        assert(slink);
245,131✔
343
        assert(ret_dirpath);
245,131✔
344
        assert(ret_dirfd);
245,131✔
345
        assert(ret_lockfile);
245,131✔
346

347
        r = stack_directory_get_name(slink, &name);
245,131✔
348
        if (r < 0)
245,131✔
349
                return log_device_debug_errno(dev, r, "Failed to build stack directory name for '%s': %m", slink);
×
350

351
        FOREACH_STRING(s, "/run/udev/links/", "/run/udev/links.lock/") {
735,393✔
352
                r = mkdir_p(s, 0755);
490,262✔
353
                if (r < 0)
490,262✔
354
                        return log_device_debug_errno(dev, r, "Failed to create '%s': %m", s);
×
355
        }
356

357
        /* 1. Take a lock for the stack directory. */
358
        lockname = path_join("/run/udev/links.lock/", name);
245,131✔
359
        if (!lockname)
245,131✔
360
                return -ENOMEM;
361

362
        r = make_lock_file(lockname, LOCK_EX, &lockfile);
245,131✔
363
        if (r < 0)
245,131✔
364
                return log_device_debug_errno(dev, r, "Failed to create and lock '%s': %m", lockname);
×
365

366
        /* 2. Create and open the stack directory. Do not create the stack directory before taking a lock,
367
         * otherwise the directory may be removed by another worker. */
368
        dirpath = path_join("/run/udev/links/", name);
245,131✔
369
        if (!dirpath)
245,131✔
370
                return -ENOMEM;
371

372
        dirfd = open_mkdir(dirpath, O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW | O_RDONLY, 0755);
245,131✔
373
        if (dirfd < 0)
245,131✔
374
                return log_device_debug_errno(dev, dirfd, "Failed to open stack directory '%s': %m", dirpath);
×
375

376
        *ret_dirpath = TAKE_PTR(dirpath);
245,131✔
377
        *ret_dirfd = TAKE_FD(dirfd);
245,131✔
378
        *ret_lockfile = TAKE_GENERIC(lockfile, LockFile, LOCK_FILE_INIT);
245,131✔
379
        return 0;
245,131✔
380
}
381

382
static int node_get_current(const char *slink, int dirfd, char **ret_id, int *ret_prio) {
245,131✔
383
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
245,131✔
384
        _cleanup_free_ char *id_dup = NULL;
245,131✔
385
        const char *id;
245,131✔
386
        int r;
245,131✔
387

388
        assert(slink);
245,131✔
389
        assert(dirfd >= 0);
245,131✔
390
        assert(ret_id);
245,131✔
391

392
        r = sd_device_new_from_devname(&dev, slink);
245,131✔
393
        if (r < 0)
245,131✔
394
                return r;
395

396
        r = sd_device_get_device_id(dev, &id);
121,330✔
397
        if (r < 0)
121,330✔
398
                return r;
399

400
        id_dup = strdup(id);
121,330✔
401
        if (!id_dup)
121,330✔
402
                return -ENOMEM;
403

404
        if (ret_prio) {
121,330✔
405
                r = stack_directory_read_one(dirfd, id, NULL, ret_prio);
46,359✔
406
                if (r < 0)
46,359✔
407
                        return r;
408
        }
409

410
        *ret_id = TAKE_PTR(id_dup);
114,868✔
411
        return 0;
114,868✔
412
}
413

414
static int link_update(sd_device *dev, const char *slink, bool add) {
245,131✔
415
        /* On cleaning up,
416
         * 1. close the stack directory,
417
         * 2. remove the stack directory if it is empty,
418
         * 3. then finally release the lock.
419
         * Hence, the variables must be declared in the reverse order. */
420
        _cleanup_(release_lock_file) LockFile lockfile = LOCK_FILE_INIT; /* #3 */
245,131✔
421
        _cleanup_(rmdir_and_freep) char *dirpath = NULL; /* #2 */
×
422
        _cleanup_close_ int dirfd = -EBADF; /* #1 */
245,131✔
423
        _cleanup_free_ char *current_id = NULL, *devnode = NULL;
245,131✔
424
        int r, current_prio;
245,131✔
425

426
        assert(dev);
245,131✔
427
        assert(slink);
245,131✔
428

429
        r = stack_directory_open_and_lock(dev, slink, &dirpath, &dirfd, &lockfile);
245,131✔
430
        if (r < 0)
245,131✔
431
                return r;
432

433
        r = node_get_current(slink, dirfd, &current_id, add ? &current_prio : NULL);
348,185✔
434
        if (r < 0 && !ERRNO_IS_DEVICE_ABSENT(r))
245,131✔
435
                return log_device_debug_errno(dev, r, "Failed to get the current device node priority for '%s': %m", slink);
×
436

437
        r = stack_directory_update(dev, dirfd, add);
245,131✔
438
        if (r < 0)
245,131✔
439
                return log_device_debug_errno(dev, r, "Failed to update stack directory for '%s': %m", slink);
×
440

441
        if (current_id) {
245,131✔
442
                const char *id;
114,868✔
443

444
                r = sd_device_get_device_id(dev, &id);
114,868✔
445
                if (r < 0)
114,868✔
446
                        return log_device_debug_errno(dev, r, "Failed to get device id: %m");
60,371✔
447

448
                if (add) {
114,868✔
449
                        int prio;
39,897✔
450

451
                        r = device_get_devlink_priority(dev, &prio);
39,897✔
452
                        if (r < 0)
39,897✔
453
                                return log_device_debug_errno(dev, r, "Failed to get devlink priority: %m");
39,891✔
454

455
                        if (streq(current_id, id)) {
39,897✔
456
                                if (current_prio <= prio)
23,467✔
457
                                        /* The devlink is ours and already exists, and the new priority is
458
                                         * equal or higher than the previous. Hence, it is not necessary to
459
                                         * recreate it. */
460
                                        return 0;
461

462
                                /* The devlink priority is downgraded. Another device may have a higher
463
                                 * priority now. Let's find the device node with the highest priority. */
464
                        } else {
465
                                if (current_prio > prio)
16,430✔
466
                                        /* The devlink with a higher priority already exists and is owned by
467
                                         * another device. Hence, it is not necessary to recreate it. */
468
                                        return 0;
469

470
                                /* This device has the equal or a higher priority than the current. Let's
471
                                 * create the devlink to our device node. */
472
                                return node_create_symlink(dev, /* devnode = */ NULL, slink);
15,105✔
473
                        }
474

475
                } else {
476
                        if (!streq(current_id, id))
74,971✔
477
                                /* The devlink already exists and is owned by another device. Hence, it is
478
                                 * not necessary to recreate it. */
479
                                return 0;
480

481
                        /* The current devlink is ours, and the target device will be removed. Hence, we need
482
                         * to search the device that has the highest priority. and update the devlink. */
483
                }
484
        } else {
485
                /* The requested devlink does not exist, or the target device does not exist and the devlink
486
                 * points to a non-existing device. Let's search the device that has the highest priority,
487
                 * and update the devlink. */
488
                ;
184,760✔
489
        }
490

491
        r = stack_directory_find_prioritized_devnode(dev, dirfd, add, &devnode);
184,760✔
492
        if (r < 0)
184,760✔
493
                return log_device_debug_errno(dev, r, "Failed to determine device node with the highest priority for '%s': %m", slink);
×
494
        if (r > 0)
184,760✔
495
                return node_create_symlink(dev, devnode, slink);
103,368✔
496

497
        log_device_debug(dev, "No reference left for '%s', removing", slink);
81,392✔
498
        return node_remove_symlink(dev, slink);
81,392✔
499
}
500

501
static int device_get_devpath_by_devnum(sd_device *dev, char **ret) {
54,075✔
502
        dev_t devnum;
54,075✔
503
        int r;
54,075✔
504

505
        assert(dev);
54,075✔
506
        assert(ret);
54,075✔
507

508
        r = sd_device_get_devnum(dev, &devnum);
54,075✔
509
        if (r < 0)
54,075✔
510
                return r;
54,075✔
511

512
        r = device_in_subsystem(dev, "block");
54,075✔
513
        if (r < 0)
54,075✔
514
                return r;
515

516
        return device_path_make_major_minor(r > 0 ? S_IFBLK : S_IFCHR, devnum, ret);
67,155✔
517
}
518

519
int udev_node_update(sd_device *dev, sd_device *dev_old) {
38,579✔
520
        _cleanup_free_ char *filename = NULL;
38,579✔
521
        int r;
38,579✔
522

523
        assert(dev);
38,579✔
524
        assert(dev_old);
38,579✔
525

526
        /* update possible left-over symlinks */
527
        FOREACH_DEVICE_DEVLINK(dev_old, devlink) {
68,935✔
528
                /* check if old link name still belongs to this device */
529
                if (device_has_devlink(dev, devlink))
30,356✔
530
                        continue;
26,897✔
531

532
                log_device_debug(dev,
3,459✔
533
                                 "Removing/updating old device symlink '%s', which is no longer belonging to this device.",
534
                                 devlink);
535

536
                r = link_update(dev, devlink, /* add = */ false);
3,459✔
537
                if (r < 0)
3,459✔
538
                        log_device_warning_errno(dev, r,
×
539
                                                 "Failed to remove/update device symlink '%s', ignoring: %m",
540
                                                 devlink);
541
        }
542

543
        /* create/update symlinks, add symlinks to name index */
544
        FOREACH_DEVICE_DEVLINK(dev, devlink) {
180,656✔
545
                r = link_update(dev, devlink, /* add = */ true);
142,077✔
546
                if (r < 0)
142,077✔
547
                        log_device_warning_errno(dev, r,
×
548
                                                 "Failed to create/update device symlink '%s', ignoring: %m",
549
                                                 devlink);
550
        }
551

552
        r = device_get_devpath_by_devnum(dev, &filename);
38,579✔
553
        if (r < 0)
38,579✔
554
                return log_device_debug_errno(dev, r, "Failed to get device path: %m");
×
555

556
        /* always add /dev/{block,char}/$major:$minor */
557
        r = node_create_symlink(dev, /* devnode = */ NULL, filename);
38,579✔
558
        if (r < 0)
38,579✔
559
                return log_device_warning_errno(dev, r, "Failed to create device symlink '%s': %m", filename);
×
560

561
        return 0;
562
}
563

564
int udev_node_remove(sd_device *dev) {
15,496✔
565
        _cleanup_free_ char *filename = NULL;
15,496✔
566
        int r;
15,496✔
567

568
        assert(dev);
15,496✔
569

570
        /* remove/update symlinks, remove symlinks from name index */
571
        FOREACH_DEVICE_DEVLINK(dev, devlink) {
115,091✔
572
                r = link_update(dev, devlink, /* add = */ false);
99,595✔
573
                if (r < 0)
99,595✔
574
                        log_device_warning_errno(dev, r,
×
575
                                                 "Failed to remove/update device symlink '%s', ignoring: %m",
576
                                                 devlink);
577
        }
578

579
        r = device_get_devpath_by_devnum(dev, &filename);
15,496✔
580
        if (r < 0)
15,496✔
581
                return log_device_debug_errno(dev, r, "Failed to get device path: %m");
×
582

583
        /* remove /dev/{block,char}/$major:$minor */
584
        return node_remove_symlink(dev, filename);
15,496✔
585
}
586

587
static int udev_node_apply_permissions_impl(
34,434✔
588
                sd_device *dev, /* can be NULL, only used for logging. */
589
                int node_fd,
590
                const char *devnode,
591
                bool apply_mac,
592
                mode_t mode,
593
                uid_t uid,
594
                gid_t gid,
595
                OrderedHashmap *seclabel_list) {
596

597
        bool apply_mode, apply_uid, apply_gid;
34,434✔
598
        struct stat stats;
34,434✔
599
        int r;
34,434✔
600

601
        assert(node_fd >= 0);
34,434✔
602
        assert(devnode);
34,434✔
603

604
        if (fstat(node_fd, &stats) < 0)
34,434✔
605
                return log_device_debug_errno(dev, errno, "cannot stat() node %s: %m", devnode);
×
606

607
        /* If group is set, but mode is not set, "upgrade" mode for the group. */
608
        if (mode == MODE_INVALID && gid_is_valid(gid) && gid > 0)
63,561✔
609
                mode = 0660;
610

611
        apply_mode = mode != MODE_INVALID && (stats.st_mode & 0777) != (mode & 0777);
21,401✔
612
        apply_uid = uid_is_valid(uid) && stats.st_uid != uid;
34,434✔
613
        apply_gid = gid_is_valid(gid) && stats.st_gid != gid;
34,434✔
614

615
        if (apply_mode || apply_uid || apply_gid || apply_mac) {
34,434✔
616
                bool selinux = false, smack = false;
21,924✔
617
                const char *name, *label;
21,924✔
618

619
                if (apply_mode || apply_uid || apply_gid) {
21,924✔
620
                        log_device_debug(dev, "Setting permissions %s, uid=" UID_FMT ", gid=" GID_FMT ", mode=%#o",
26,312✔
621
                                         devnode,
622
                                         uid_is_valid(uid) ? uid : stats.st_uid,
623
                                         gid_is_valid(gid) ? gid : stats.st_gid,
624
                                         mode != MODE_INVALID ? mode & 0777 : stats.st_mode & 0777);
625

626
                        r = fchmod_and_chown(node_fd, mode, uid, gid);
13,156✔
627
                        if (r < 0)
13,156✔
628
                                log_device_full_errno(dev, r == -ENOENT ? LOG_DEBUG : LOG_ERR, r,
×
629
                                                      "Failed to set owner/mode of %s to uid=" UID_FMT
630
                                                      ", gid=" GID_FMT ", mode=%#o: %m",
631
                                                      devnode,
632
                                                      uid_is_valid(uid) ? uid : stats.st_uid,
633
                                                      gid_is_valid(gid) ? gid : stats.st_gid,
634
                                                      mode != MODE_INVALID ? mode & 0777 : stats.st_mode & 0777);
635
                } else
636
                        log_device_debug(dev, "Preserve permissions of %s, uid=" UID_FMT ", gid=" GID_FMT ", mode=%#o",
17,536✔
637
                                         devnode,
638
                                         uid_is_valid(uid) ? uid : stats.st_uid,
639
                                         gid_is_valid(gid) ? gid : stats.st_gid,
640
                                         mode != MODE_INVALID ? mode & 0777 : stats.st_mode & 0777);
641

642
                /* apply SECLABEL{$module}=$label */
643
                ORDERED_HASHMAP_FOREACH_KEY(label, name, seclabel_list) {
21,924✔
644
                        int q;
×
645

646
                        if (streq(name, "selinux")) {
×
647
                                selinux = true;
×
648

649
                                q = mac_selinux_apply_fd(node_fd, devnode, label);
×
650
                                if (q < 0)
×
651
                                        log_device_full_errno(dev, q == -ENOENT ? LOG_DEBUG : LOG_ERR, q,
×
652
                                                              "SECLABEL: failed to set SELinux label '%s': %m", label);
653
                                else
654
                                        log_device_debug(dev, "SECLABEL: set SELinux label '%s'", label);
×
655

656
                        } else if (streq(name, "smack")) {
×
657
                                smack = true;
×
658

659
                                q = mac_smack_apply_fd(node_fd, SMACK_ATTR_ACCESS, label);
×
660
                                if (q < 0)
×
661
                                        log_device_full_errno(dev, q == -ENOENT ? LOG_DEBUG : LOG_ERR, q,
×
662
                                                              "SECLABEL: failed to set SMACK label '%s': %m", label);
663
                                else
664
                                        log_device_debug(dev, "SECLABEL: set SMACK label '%s'", label);
×
665

666
                        } else
667
                                log_device_error(dev, "SECLABEL: unknown subsystem, ignoring '%s'='%s'", name, label);
×
668
                }
669

670
                /* set the defaults */
671
                if (!selinux)
21,924✔
672
                        (void) mac_selinux_fix_full(node_fd, NULL, devnode, LABEL_IGNORE_ENOENT);
21,924✔
673
                if (!smack)
21,924✔
674
                        (void) mac_smack_apply_fd(node_fd, SMACK_ATTR_ACCESS, NULL);
21,924✔
675
        }
676

677
        /* always update timestamp when we re-use the node, like on media change events */
678
        r = futimens_opath(node_fd, NULL);
34,434✔
679
        if (r < 0)
34,434✔
680
                log_device_debug_errno(dev, r, "Failed to adjust timestamp of node %s: %m", devnode);
×
681

682
        return 0;
683
}
684

685
int udev_node_apply_permissions(
38,579✔
686
                sd_device *dev,
687
                bool apply_mac,
688
                mode_t mode,
689
                uid_t uid,
690
                gid_t gid,
691
                OrderedHashmap *seclabel_list) {
692

693
        const char *devnode;
38,579✔
694
        _cleanup_close_ int node_fd = -EBADF;
38,579✔
695
        int r;
38,579✔
696

697
        assert(dev);
38,579✔
698

699
        r = sd_device_get_devname(dev, &devnode);
38,579✔
700
        if (r < 0)
38,579✔
701
                return log_device_debug_errno(dev, r, "Failed to get devname: %m");
×
702

703
        node_fd = sd_device_open(dev, O_PATH|O_CLOEXEC);
38,579✔
704
        if (node_fd < 0) {
38,579✔
705
                if (ERRNO_IS_DEVICE_ABSENT(node_fd)) {
4,732✔
706
                        log_device_debug_errno(dev, node_fd, "Device node %s is missing, skipping handling.", devnode);
4,732✔
707
                        return 0; /* This is necessarily racey, so ignore missing the device */
4,732✔
708
                }
709

710
                return log_device_debug_errno(dev, node_fd, "Cannot open node %s: %m", devnode);
×
711
        }
712

713
        return udev_node_apply_permissions_impl(dev, node_fd, devnode, apply_mac, mode, uid, gid, seclabel_list);
33,847✔
714
}
715

716
int static_node_apply_permissions(
616✔
717
                const char *name,
718
                mode_t mode,
719
                uid_t uid,
720
                gid_t gid,
721
                char **tags) {
722

723
        _cleanup_free_ char *unescaped_filename = NULL;
616✔
724
        _cleanup_close_ int node_fd = -EBADF;
616✔
725
        const char *devnode;
616✔
726
        struct stat stats;
616✔
727
        int r;
616✔
728

729
        assert(name);
616✔
730

731
        if (uid == UID_INVALID && gid == GID_INVALID && mode == MODE_INVALID && !tags)
616✔
732
                return 0;
733

734
        devnode = strjoina("/dev/", name);
3,080✔
735

736
        node_fd = open(devnode, O_PATH|O_CLOEXEC);
616✔
737
        if (node_fd < 0) {
616✔
738
                if (errno != ENOENT)
29✔
739
                        return log_error_errno(errno, "Failed to open %s: %m", devnode);
×
740
                return 0;
741
        }
742

743
        if (fstat(node_fd, &stats) < 0)
587✔
744
                return log_error_errno(errno, "Failed to stat %s: %m", devnode);
×
745

746
        if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode)) {
587✔
747
                log_warning("%s is neither block nor character device, ignoring.", devnode);
×
748
                return 0;
×
749
        }
750

751
        if (!strv_isempty(tags)) {
587✔
752
                unescaped_filename = xescape(name, "/.");
112✔
753
                if (!unescaped_filename)
112✔
754
                        return log_oom();
×
755
        }
756

757
        /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
758
        STRV_FOREACH(t, tags) {
699✔
759
                _cleanup_free_ char *p = NULL;
112✔
760

761
                p = path_join("/run/udev/static_node-tags/", *t, unescaped_filename);
112✔
762
                if (!p)
112✔
763
                        return log_oom();
×
764

765
                r = mkdir_parents(p, 0755);
112✔
766
                if (r < 0)
112✔
767
                        return log_error_errno(r, "Failed to create parent directory for %s: %m", p);
×
768

769
                r = symlink(devnode, p);
112✔
770
                if (r < 0 && errno != EEXIST)
112✔
771
                        return log_error_errno(errno, "Failed to create symlink %s -> %s: %m", p, devnode);
×
772
        }
773

774
        return udev_node_apply_permissions_impl(NULL, node_fd, devnode, false, mode, uid, gid, NULL);
587✔
775
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc