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

systemd / systemd / 16610200353

29 Jul 2025 11:26PM UTC coverage: 72.278% (+0.06%) from 72.215%
16610200353

push

github

yuwata
po: Translated using Weblate (Chinese (Simplified) (zh_CN))

Currently translated at 100.0% (264 of 264 strings)

Co-authored-by: Jesse Guo <jesseguotech@outlook.com>
Translate-URL: https://translate.fedoraproject.org/projects/systemd/main/zh_CN/
Translation: systemd/main

303003 of 419221 relevant lines covered (72.28%)

740914.66 hits per line

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

82.86
/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) {
94,031✔
37
        assert(dev);
94,031✔
38
        assert(slink);
94,031✔
39

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

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

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

51
        assert(dev);
156,281✔
52
        assert(slink);
156,281✔
53

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

60
        if (lstat(slink, &st) >= 0) {
156,281✔
61
                if (!S_ISLNK(st.st_mode))
41,216✔
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)
115,065✔
66
                return log_device_debug_errno(dev, errno, "Failed to lstat() '%s': %m", slink);
×
67

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

72
        /* use relative link */
73
        r = symlinkat_atomic_full(devnode, AT_FDCWD, slink, SYMLINK_MAKE_RELATIVE|SYMLINK_LABEL);
156,281✔
74
        if (r < 0)
156,281✔
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);
158,255✔
78
        return 0;
156,281✔
79
}
80

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

85
        assert(dirfd >= 0);
95,410✔
86
        assert(id);
95,410✔
87
        assert(priority);
95,410✔
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);
95,410✔
96
        if (r >= 0) {
95,410✔
97
                char *colon;
88,276✔
98

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

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

105
                *colon = '\0';
88,276✔
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)
88,276✔
112
                        return -ENODEV;
113

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

118
                if (!devnode)
48,230✔
119
                        goto finalize;
43,413✔
120

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

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

128
        } else if (r == -EINVAL) { /* Not a symlink ? try the old format */
7,134✔
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;
7,134✔
158

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

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

170
        assert(dev);
180,809✔
171
        assert(dirfd >= 0);
180,809✔
172
        assert(ret);
180,809✔
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) {
180,809✔
178
                const char *n;
100,586✔
179

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

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

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

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

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

201
        FOREACH_DIRENT(de, dir, break) {
687,875✔
202

203
                /* skip ourself */
204
                if (streq(de->d_name, id))
145,448✔
205
                        continue;
100,586✔
206

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

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

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

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

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

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

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

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

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

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

246
                (void) unlinkat(fd, id, 0);
114,166✔
247

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

251
        } else {
252
                if (unlinkat(fd, id, 0) < 0) {
101,423✔
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,427✔
263
        size_t i, j;
245,427✔
264
        uint64_t h;
245,427✔
265

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

270
        for (i = 0, j = 0; src[i] != '\0'; i++) {
12,702,064✔
271
                if (src[i] == '/') {
12,456,640✔
272
                        if (j+4 >= size - 12 + 1)
640,704✔
273
                                goto toolong;
×
274
                        memcpy(&dest[j], "\\x2f", 4);
640,704✔
275
                        j += 4;
640,704✔
276
                } else if (src[i] == '\\') {
11,815,936✔
277
                        if (j+4 >= size - 12 + 1)
3,241✔
278
                                goto toolong;
×
279
                        memcpy(&dest[j], "\\x5c", 4);
3,241✔
280
                        j += 4;
3,241✔
281
                } else {
282
                        if (j+1 >= size - 12 + 1)
11,812,695✔
283
                                goto toolong;
3✔
284
                        dest[j] = src[i];
11,812,692✔
285
                        j++;
11,812,692✔
286
                }
287
        }
288
        dest[j] = '\0';
245,424✔
289
        return j;
245,424✔
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,416✔
305
        _cleanup_free_ char *s = NULL;
245,416✔
306
        char name_enc[NAME_MAX+1];
245,416✔
307
        const char *name;
245,416✔
308
        int r;
245,416✔
309

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

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

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

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

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

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

329
static int stack_directory_open_and_lock(
245,416✔
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,416✔
338
        _cleanup_free_ char *name = NULL, *dirpath = NULL, *lockname = NULL;
245,416✔
339
        int r;
245,416✔
340

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

347
        r = stack_directory_get_name(slink, &name);
245,416✔
348
        if (r < 0)
245,416✔
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/") {
736,248✔
352
                r = mkdir_p(s, 0755);
490,832✔
353
                if (r < 0)
490,832✔
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,416✔
359
        if (!lockname)
245,416✔
360
                return -ENOMEM;
361

362
        r = make_lock_file(lockname, LOCK_EX, &lockfile);
245,416✔
363
        if (r < 0)
245,416✔
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,416✔
369
        if (!dirpath)
245,416✔
370
                return -ENOMEM;
371

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

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

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

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

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

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

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

404
        if (ret_prio) {
124,895✔
405
                r = stack_directory_read_one(dirfd, id, NULL, ret_prio);
50,548✔
406
                if (r < 0)
50,548✔
407
                        return r;
408
        }
409

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

414
static int link_update(sd_device *dev, const char *slink, bool add) {
245,416✔
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,416✔
421
        _cleanup_(rmdir_and_freep) char *dirpath = NULL; /* #2 */
×
422
        _cleanup_close_ int dirfd = -EBADF; /* #1 */
245,416✔
423
        _cleanup_free_ char *current_id = NULL, *devnode = NULL;
245,416✔
424
        int r, current_prio;
245,416✔
425

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

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

433
        r = node_get_current(slink, dirfd, &current_id, add ? &current_prio : NULL);
346,839✔
434
        if (r < 0 && !ERRNO_IS_DEVICE_ABSENT_OR_EMPTY(r))
245,416✔
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,416✔
438
        if (r < 0)
245,416✔
439
                return log_device_debug_errno(dev, r, "Failed to update stack directory for '%s': %m", slink);
×
440

441
        if (current_id) {
245,416✔
442
                const char *id;
117,760✔
443

444
                r = sd_device_get_device_id(dev, &id);
117,760✔
445
                if (r < 0)
117,760✔
446
                        return log_device_debug_errno(dev, r, "Failed to get device id: %m");
64,607✔
447

448
                if (add) {
117,760✔
449
                        int prio;
43,413✔
450

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

455
                        if (streq(current_id, id)) {
43,413✔
456
                                if (current_prio <= prio)
26,606✔
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,807✔
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,529✔
473
                        }
474

475
                } else {
476
                        if (!streq(current_id, id))
74,347✔
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
                ;
180,809✔
489
        }
490

491
        r = stack_directory_find_prioritized_devnode(dev, dirfd, add, &devnode);
180,809✔
492
        if (r < 0)
180,809✔
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)
180,809✔
495
                return node_create_symlink(dev, devnode, slink);
102,034✔
496

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

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

505
        assert(dev);
53,974✔
506
        assert(ret);
53,974✔
507

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

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

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

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

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

526
        /* update possible left-over symlinks */
527
        FOREACH_DEVICE_DEVLINK(dev_old, devlink) {
71,060✔
528
                /* check if old link name still belongs to this device */
529
                if (device_has_devlink(dev, devlink))
32,342✔
530
                        continue;
29,839✔
531

532
                log_device_debug(dev,
2,503✔
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);
2,503✔
537
                if (r < 0)
2,503✔
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) {
182,711✔
545
                r = link_update(dev, devlink, /* add = */ true);
143,993✔
546
                if (r < 0)
143,993✔
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,718✔
553
        if (r < 0)
38,718✔
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,718✔
558
        if (r < 0)
38,718✔
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,256✔
565
        _cleanup_free_ char *filename = NULL;
15,256✔
566
        int r;
15,256✔
567

568
        assert(dev);
15,256✔
569

570
        /* remove/update symlinks, remove symlinks from name index */
571
        FOREACH_DEVICE_DEVLINK(dev, devlink) {
114,176✔
572
                r = link_update(dev, devlink, /* add = */ false);
98,920✔
573
                if (r < 0)
98,920✔
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,256✔
580
        if (r < 0)
15,256✔
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,256✔
585
}
586

587
static int udev_node_apply_permissions_impl(
34,711✔
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,711✔
598
        struct stat stats;
34,711✔
599
        int r;
34,711✔
600

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

604
        if (fstat(node_fd, &stats) < 0)
34,711✔
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,758✔
609
                mode = 0660;
610

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

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

619
                if (apply_mode || apply_uid || apply_gid) {
21,847✔
620
                        log_device_debug(dev, "Setting permissions %s, uid=" UID_FMT ", gid=" GID_FMT ", mode=%#o",
25,098✔
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);
12,549✔
627
                        if (r < 0)
12,549✔
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",
18,596✔
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,847✔
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,847✔
672
                        (void) mac_selinux_fix_full(node_fd, NULL, devnode, LABEL_IGNORE_ENOENT);
21,847✔
673
                if (!smack)
21,847✔
674
                        (void) mac_smack_apply_fd(node_fd, SMACK_ATTR_ACCESS, NULL);
21,847✔
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,711✔
679
        if (r < 0)
34,711✔
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,718✔
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,718✔
694
        _cleanup_close_ int node_fd = -EBADF;
38,718✔
695
        int r;
38,718✔
696

697
        assert(dev);
38,718✔
698

699
        r = sd_device_get_devname(dev, &devnode);
38,718✔
700
        if (r < 0)
38,718✔
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,718✔
704
        if (node_fd < 0) {
38,718✔
705
                if (ERRNO_IS_DEVICE_ABSENT_OR_EMPTY(node_fd)) {
4,544✔
706
                        log_device_debug_errno(dev, node_fd, "Device node %s is missing, skipping handling.", devnode);
4,544✔
707
                        return 0; /* This is necessarily racey, so ignore missing the device */
4,544✔
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);
34,174✔
714
}
715

716
int static_node_apply_permissions(
561✔
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;
561✔
724
        _cleanup_close_ int node_fd = -EBADF;
561✔
725
        const char *devnode;
561✔
726
        struct stat stats;
561✔
727
        int r;
561✔
728

729
        assert(name);
561✔
730

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

734
        devnode = strjoina("/dev/", name);
2,805✔
735

736
        node_fd = open(devnode, O_PATH|O_CLOEXEC);
561✔
737
        if (node_fd < 0) {
561✔
738
                bool ignore = ERRNO_IS_DEVICE_ABSENT_OR_EMPTY(errno);
24✔
739
                log_full_errno(ignore ? LOG_DEBUG : LOG_WARNING, errno,
24✔
740
                               "Failed to open device node '%s'%s: %m",
741
                               devnode, ignore ? ", ignoring" : "");
742
                return ignore ? 0 : -errno;
24✔
743
        }
744

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

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

753
        if (!strv_isempty(tags)) {
537✔
754
                unescaped_filename = xescape(name, "/.");
102✔
755
                if (!unescaped_filename)
102✔
756
                        return log_oom();
×
757
        }
758

759
        /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
760
        STRV_FOREACH(t, tags) {
639✔
761
                _cleanup_free_ char *p = NULL;
102✔
762

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

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

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

776
        return udev_node_apply_permissions_impl(NULL, node_fd, devnode, false, mode, uid, gid, NULL);
537✔
777
}
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