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

systemd / systemd / 21050823169

15 Jan 2026 12:56PM UTC coverage: 72.522% (-0.2%) from 72.704%
21050823169

push

github

keszybz
tree-wide: lock in all memory pages when mlockall() is utilized, and on demand

When employing MCL_FUTURE we don't actually want it to impose
immediate population of malloc()-ed pages. Hence let's set
MCL_ONFAULT everywhere.

Additionally, specify MCL_CURRENT to ensure future memory allocations
on already mapped pages are covered too. (Addresses
https://github.com/systemd/systemd/pull/40319#discussion_r2693726196)

Note that in shutdown the mlockall() is done to avoid keeping swap space
busy, hence a dedicated call w/ MCL_CURRENT and w/o MCL_ONFAULT is made.

2 of 4 new or added lines in 3 files covered. (50.0%)

1162 existing lines in 49 files now uncovered.

309356 of 426567 relevant lines covered (72.52%)

1129967.96 hits per line

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

81.0
/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) {
60,229✔
37
        assert(dev);
60,229✔
38
        assert(slink);
60,229✔
39

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

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

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

51
        assert(dev);
126,178✔
52
        assert(slink);
126,178✔
53

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

60
        if (lstat(slink, &st) >= 0) {
126,178✔
61
                if (!S_ISLNK(st.st_mode))
56,257✔
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)
69,921✔
66
                return log_device_debug_errno(dev, errno, "Failed to lstat() '%s': %m", slink);
×
67

68
        r = mkdir_parents_label(slink, 0755);
126,178✔
69
        if (r < 0)
126,178✔
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);
126,178✔
74
        if (r < 0)
126,178✔
UNCOV
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);
128,129✔
78
        return 0;
126,178✔
79
}
80

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

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

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

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

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

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

118
                if (!devnode)
435,242✔
119
                        goto finalize;
80,672✔
120

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

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

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

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

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

170
        assert(dev);
125,996✔
171
        assert(dirfd >= 0);
125,996✔
172
        assert(ret);
125,996✔
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) {
125,996✔
178
                const char *n;
69,213✔
179

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

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

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

193
        dir = xopendirat(dirfd, /* path= */ NULL, /* flags= */ 0);
125,996✔
194
        if (!dir)
125,996✔
195
                return -errno;
×
196

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

201
        FOREACH_DIRENT(de, dir, break) {
821,564✔
202

203
                /* skip ourself */
204
                if (streq(de->d_name, id))
443,576✔
205
                        continue;
69,213✔
206

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

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

216
static int link_search_and_update(sd_device *dev, const char *slink, int dirfd, bool add) {
125,996✔
217
        int r;
125,996✔
218

219
        assert(dev);
125,996✔
220
        assert(slink);
125,996✔
221
        assert(dirfd >= 0);
125,996✔
222

223
        _cleanup_free_ char *devnode = NULL;
125,996✔
224
        r = stack_directory_find_prioritized_devnode(dev, dirfd, add, &devnode);
125,996✔
225
        if (r < 0)
125,996✔
226
                return log_device_debug_errno(dev, r, "Failed to determine device node with the highest priority for '%s': %m", slink);
×
227
        if (r > 0)
125,996✔
228
                return node_create_symlink(dev, devnode, slink);
74,845✔
229

230
        log_device_debug(dev, "No reference left for '%s', removing", slink);
51,151✔
231
        return node_remove_symlink(dev, slink);
51,151✔
232
}
233

234
static int stack_directory_update(sd_device *dev, int fd, bool add) {
206,662✔
235
        const char *id;
206,662✔
236
        int r;
206,662✔
237

238
        assert(dev);
206,662✔
239
        assert(fd >= 0);
206,662✔
240

241
        r = sd_device_get_device_id(dev, &id);
206,662✔
242
        if (r < 0)
206,662✔
243
                return r;
206,662✔
244

245
        if (add) {
206,662✔
246
                _cleanup_free_ char *data = NULL, *buf = NULL;
149,879✔
247
                const char *devname;
149,879✔
248
                int priority;
149,879✔
249

250
                r = sd_device_get_devname(dev, &devname);
149,879✔
251
                if (r < 0)
149,879✔
252
                        return r;
253

254
                r = device_get_devlink_priority(dev, &priority);
149,879✔
255
                if (r < 0)
149,879✔
256
                        return r;
257

258
                if (asprintf(&data, "%i:%s", priority, devname) < 0)
149,879✔
259
                        return -ENOMEM;
260

261
                if (readlinkat_malloc(fd, id, &buf) >= 0 && streq(buf, data))
149,879✔
262
                        return 0; /* Unchanged. */
263

264
                (void) unlinkat(fd, id, 0);
61,279✔
265

266
                if (symlinkat(data, fd, id) < 0)
61,279✔
267
                        return -errno;
×
268

269
        } else {
270
                if (unlinkat(fd, id, 0) < 0) {
56,783✔
271
                        if (errno == ENOENT)
×
272
                                return 0; /* Unchanged. */
273
                        return -errno;
×
274
                }
275
        }
276

277
        return 1; /* Updated. */
278
}
279

280
size_t udev_node_escape_path(const char *src, char *dest, size_t size) {
206,673✔
281
        size_t i, j;
206,673✔
282
        uint64_t h;
206,673✔
283

284
        assert(src);
206,673✔
285
        assert(dest);
206,673✔
286
        assert(size >= 12);
206,673✔
287

288
        for (i = 0, j = 0; src[i] != '\0'; i++) {
10,426,173✔
289
                if (src[i] == '/') {
10,219,503✔
290
                        if (j+4 >= size - 12 + 1)
530,870✔
291
                                goto toolong;
×
292
                        memcpy(&dest[j], "\\x2f", 4);
530,870✔
293
                        j += 4;
530,870✔
294
                } else if (src[i] == '\\') {
9,688,633✔
295
                        if (j+4 >= size - 12 + 1)
4,366✔
296
                                goto toolong;
×
297
                        memcpy(&dest[j], "\\x5c", 4);
4,366✔
298
                        j += 4;
4,366✔
299
                } else {
300
                        if (j+1 >= size - 12 + 1)
9,684,267✔
301
                                goto toolong;
3✔
302
                        dest[j] = src[i];
9,684,264✔
303
                        j++;
9,684,264✔
304
                }
305
        }
306
        dest[j] = '\0';
206,670✔
307
        return j;
206,670✔
308

309
toolong:
3✔
310
        /* If the input path is too long to encode as a filename, then let's suffix with a string
311
         * generated from the hash of the path. */
312

313
        h = siphash24_string(src, UDEV_NODE_HASH_KEY.bytes);
3✔
314

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

318
        dest[size - 1] = '\0';
3✔
319
        return size - 1;
3✔
320
}
321

322
static int stack_directory_get_name(const char *slink, char **ret) {
206,662✔
323
        _cleanup_free_ char *s = NULL;
206,662✔
324
        char name_enc[NAME_MAX+1];
206,662✔
325
        const char *name;
206,662✔
326
        int r;
206,662✔
327

328
        assert(slink);
206,662✔
329
        assert(ret);
206,662✔
330

331
        r = path_simplify_alloc(slink, &s);
206,662✔
332
        if (r < 0)
206,662✔
333
                return r;
334

335
        if (!path_is_normalized(s))
206,662✔
336
                return -EINVAL;
337

338
        name = path_startswith(s, "/dev");
206,662✔
339
        if (empty_or_root(name))
206,662✔
340
                return -EINVAL;
341

342
        udev_node_escape_path(name, name_enc, sizeof(name_enc));
206,662✔
343

344
        return strdup_to(ret, name_enc);
206,662✔
345
}
346

347
static int stack_directory_open_and_lock(
206,662✔
348
                sd_device *dev,
349
                const char *slink,
350
                char **ret_dirpath,
351
                int *ret_dirfd,
352
                LockFile *ret_lockfile) {
353

354
        _cleanup_(release_lock_file) LockFile lockfile = LOCK_FILE_INIT;
×
355
        _cleanup_close_ int dirfd = -EBADF;
206,662✔
356
        _cleanup_free_ char *name = NULL, *dirpath = NULL, *lockname = NULL;
206,662✔
357
        int r;
206,662✔
358

359
        assert(dev);
206,662✔
360
        assert(slink);
206,662✔
361
        assert(ret_dirpath);
206,662✔
362
        assert(ret_dirfd);
206,662✔
363
        assert(ret_lockfile);
206,662✔
364

365
        r = stack_directory_get_name(slink, &name);
206,662✔
366
        if (r < 0)
206,662✔
367
                return log_device_debug_errno(dev, r, "Failed to build stack directory name for '%s': %m", slink);
×
368

369
        FOREACH_STRING(s, "/run/udev/links/", "/run/udev/links.lock/") {
619,986✔
370
                r = mkdir_p(s, 0755);
413,324✔
371
                if (r < 0)
413,324✔
372
                        return log_device_debug_errno(dev, r, "Failed to create '%s': %m", s);
×
373
        }
374

375
        /* 1. Take a lock for the stack directory. */
376
        lockname = path_join("/run/udev/links.lock/", name);
206,662✔
377
        if (!lockname)
206,662✔
378
                return -ENOMEM;
379

380
        r = make_lock_file(lockname, LOCK_EX, &lockfile);
206,662✔
381
        if (r < 0)
206,662✔
382
                return log_device_debug_errno(dev, r, "Failed to create and lock '%s': %m", lockname);
×
383

384
        /* 2. Create and open the stack directory. Do not create the stack directory before taking a lock,
385
         * otherwise the directory may be removed by another worker. */
386
        dirpath = path_join("/run/udev/links/", name);
206,662✔
387
        if (!dirpath)
206,662✔
388
                return -ENOMEM;
389

390
        dirfd = open_mkdir(dirpath, O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW | O_RDONLY, 0755);
206,662✔
391
        if (dirfd < 0)
206,662✔
392
                return log_device_debug_errno(dev, dirfd, "Failed to open stack directory '%s': %m", dirpath);
×
393

394
        *ret_dirpath = TAKE_PTR(dirpath);
206,662✔
395
        *ret_dirfd = TAKE_FD(dirfd);
206,662✔
396
        *ret_lockfile = TAKE_GENERIC(lockfile, LockFile, LOCK_FILE_INIT);
206,662✔
397
        return 0;
206,662✔
398
}
399

400
static int node_get_current(const char *slink, int dirfd, char **ret_id, int *ret_prio) {
149,879✔
401
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
149,879✔
402
        _cleanup_free_ char *id_dup = NULL;
149,879✔
403
        const char *id;
149,879✔
404
        int r;
149,879✔
405

406
        assert(slink);
149,879✔
407
        assert(dirfd >= 0);
149,879✔
408
        assert(ret_id);
149,879✔
409

410
        r = sd_device_new_from_devname(&dev, slink);
149,879✔
411
        if (r < 0)
149,879✔
412
                return r;
413

414
        r = sd_device_get_device_id(dev, &id);
81,046✔
415
        if (r < 0)
81,046✔
416
                return r;
417

418
        id_dup = strdup(id);
81,046✔
419
        if (!id_dup)
81,046✔
420
                return -ENOMEM;
421

422
        if (ret_prio) {
81,046✔
423
                r = stack_directory_read_one(dirfd, id, NULL, ret_prio);
81,046✔
424
                if (r < 0)
81,046✔
425
                        return r;
426
        }
427

428
        *ret_id = TAKE_PTR(id_dup);
80,672✔
429
        return 0;
80,672✔
430
}
431

432
static int link_update(sd_device *dev, const char *slink, bool add) {
206,662✔
433
        /* On cleaning up,
434
         * 1. close the stack directory,
435
         * 2. remove the stack directory if it is empty,
436
         * 3. then finally release the lock.
437
         * Hence, the variables must be declared in the reverse order. */
438
        _cleanup_(release_lock_file) LockFile lockfile = LOCK_FILE_INIT; /* #3 */
206,662✔
439
        _cleanup_(rmdir_and_freep) char *dirpath = NULL; /* #2 */
×
440
        _cleanup_close_ int dirfd = -EBADF; /* #1 */
206,662✔
441
        _cleanup_free_ char *current_id = NULL;
206,662✔
442
        int r, current_prio;
206,662✔
443

444
        assert(dev);
206,662✔
445
        assert(slink);
206,662✔
446

447
        r = stack_directory_open_and_lock(dev, slink, &dirpath, &dirfd, &lockfile);
206,662✔
448
        if (r < 0)
206,662✔
449
                return r;
450

451
        if (add) {
206,662✔
452
                r = node_get_current(slink, dirfd, &current_id, &current_prio);
149,879✔
453
                if (r < 0 && !ERRNO_IS_DEVICE_ABSENT_OR_EMPTY(r))
149,879✔
454
                        return log_device_debug_errno(dev, r, "Failed to get the current device node priority for '%s': %m", slink);
×
455
        }
456

457
        r = stack_directory_update(dev, dirfd, add);
206,662✔
458
        if (r < 0)
206,662✔
459
                return log_device_debug_errno(dev, r, "Failed to update stack directory for '%s': %m", slink);
×
460

461
        if (!add) {
206,662✔
462
                _cleanup_free_ char *target = NULL;
56,783✔
463

464
                /* Especially on 'remove' event, the device ID obtained by node_get_current() may not be
465
                 * reliable, as the device node and/or device number may be reused. Hence, let's read the
466
                 * devlink here and if it points to our device node, then we need to update the devlink. If
467
                 * it points to another device node, then it is already owned by another device, hence we
468
                 * should not touch it and keep it as is. */
469

470
                r = readlink_malloc(slink, &target);
56,783✔
471
                if (r < 0) {
56,783✔
472
                        if (r != -ENOENT)
1,201✔
473
                                log_device_debug_errno(dev, r, "Failed to read symbolic link '%s', ignoring: %m", slink);
×
474

475
                        /* The devlink does not exist. Let's find the most suitable owner, and create the
476
                         * devlink. This is typically not necessary and does nothing, but for safety in the
477
                         * case that the devlink is removed manually. */
478
                        return link_search_and_update(dev, slink, dirfd, add);
1,201✔
479
                }
480

481
                const char *node;
55,582✔
482
                r = sd_device_get_devname(dev, &node);
55,582✔
483
                if (r < 0)
55,582✔
484
                        return log_device_debug_errno(dev, r, "Failed to get device node: %m");
×
485

486
                if (streq(node, target)) /* owned by us, needs to update. */
55,582✔
487
                        return link_search_and_update(dev, slink, dirfd, add);
×
488

489
                /* The devlink does not point to our device node. For extra safety, let's validate the
490
                 * current devlink, in case that the devlink is manually modified by user. */
491

492
                if (!path_startswith(target, "/dev/")) {
55,582✔
493
                        log_device_debug(dev, "Symbolic link '%s' points to '%s' which is outside of '/dev/', updating it.", slink, target);
55,582✔
494
                        return link_search_and_update(dev, slink, dirfd, add);
55,582✔
495
                }
496

497
                struct stat st;
×
498
                if (lstat(target, &st) < 0) {
×
499
                        if (errno != ENOENT)
×
500
                                log_device_debug_errno(dev, errno, "Failed to stat '%s', ignoring: %m", target);
×
501

502
                        /* The current target device is also already removed? Let's update. */
503
                        return link_search_and_update(dev, slink, dirfd, add);
×
504
                }
505

506
                if (!IN_SET(st.st_mode & S_IFMT, S_IFBLK, S_IFCHR)) {
×
507
                        log_device_debug(dev, "Symbolic link '%s' points to '%s' which is not a device node, updating it.", slink, target);
×
508
                        return link_search_and_update(dev, slink, dirfd, add);
×
509
                }
510

511
                return 0; /* the devlink is owned by another device, and we should keep it as is. */
512
        }
513

514
        if (!current_id)
149,879✔
515
                /* The requested devlink does not exist, or the target device does not exist and the devlink
516
                 * points to a non-existing device. Let's search the device that has the highest priority,
517
                 * and update the devlink. */
518
                return link_search_and_update(dev, slink, dirfd, add);
69,207✔
519

520
        const char *id;
80,672✔
521
        r = sd_device_get_device_id(dev, &id);
80,672✔
522
        if (r < 0)
80,672✔
523
                return log_device_debug_errno(dev, r, "Failed to get device id: %m");
×
524

525
        int prio;
80,672✔
526
        r = device_get_devlink_priority(dev, &prio);
80,672✔
527
        if (r < 0)
80,672✔
528
                return log_device_debug_errno(dev, r, "Failed to get devlink priority: %m");
×
529

530
        if (streq(current_id, id)) {
80,672✔
531
                if (current_prio <= prio)
66,497✔
532
                        /* The devlink is ours and already exists, and the new priority is equal or higher
533
                         * than the previous. Hence, it is not necessary to recreate it. */
534
                        return 0;
535

536
                /* The devlink priority is downgraded. Another device may have a higher priority now. Let's
537
                 * find the device node with the highest priority. */
538
                return link_search_and_update(dev, slink, dirfd, add);
6✔
539
        }
540

541
        if (current_prio > prio)
14,175✔
542
                /* The devlink with a higher priority already exists and is owned by another device. Hence,
543
                 * it is not necessary to recreate it. */
544
                return 0;
545

546
        /* This device has the equal or a higher priority than the current. Let's create the devlink to our
547
         * device node. */
548
        return node_create_symlink(dev, /* devnode= */ NULL, slink);
10,733✔
549
}
550

551
static int device_get_devpath_by_devnum(sd_device *dev, char **ret) {
49,678✔
552
        dev_t devnum;
49,678✔
553
        int r;
49,678✔
554

555
        assert(dev);
49,678✔
556
        assert(ret);
49,678✔
557

558
        r = sd_device_get_devnum(dev, &devnum);
49,678✔
559
        if (r < 0)
49,678✔
560
                return r;
49,678✔
561

562
        r = device_in_subsystem(dev, "block");
49,678✔
563
        if (r < 0)
49,678✔
564
                return r;
565

566
        return device_path_make_major_minor(r > 0 ? S_IFBLK : S_IFCHR, devnum, ret);
62,088✔
567
}
568

569
int udev_node_update(sd_device *dev, sd_device *dev_old) {
40,600✔
570
        _cleanup_free_ char *filename = NULL;
40,600✔
571
        int r;
40,600✔
572

573
        assert(dev);
40,600✔
574
        assert(dev_old);
40,600✔
575

576
        /* update possible left-over symlinks */
577
        FOREACH_DEVICE_DEVLINK(dev_old, devlink) {
132,665✔
578
                /* check if old link name still belongs to this device */
579
                if (device_has_devlink(dev, devlink))
92,065✔
580
                        continue;
88,613✔
581

582
                log_device_debug(dev,
3,452✔
583
                                 "Removing/updating old device symlink '%s', which is no longer belonging to this device.",
584
                                 devlink);
585

586
                r = link_update(dev, devlink, /* add= */ false);
3,452✔
587
                if (r < 0)
3,452✔
588
                        log_device_warning_errno(dev, r,
×
589
                                                 "Failed to remove/update device symlink '%s', ignoring: %m",
590
                                                 devlink);
591
        }
592

593
        /* create/update symlinks, add symlinks to name index */
594
        FOREACH_DEVICE_DEVLINK(dev, devlink) {
190,479✔
595
                r = link_update(dev, devlink, /* add= */ true);
149,879✔
596
                if (r < 0)
149,879✔
UNCOV
597
                        log_device_warning_errno(dev, r,
×
598
                                                 "Failed to create/update device symlink '%s', ignoring: %m",
599
                                                 devlink);
600
        }
601

602
        r = device_get_devpath_by_devnum(dev, &filename);
40,600✔
603
        if (r < 0)
40,600✔
604
                return log_device_debug_errno(dev, r, "Failed to get device path: %m");
×
605

606
        /* always add /dev/{block,char}/$major:$minor */
607
        r = node_create_symlink(dev, /* devnode= */ NULL, filename);
40,600✔
608
        if (r < 0)
40,600✔
609
                return log_device_warning_errno(dev, r, "Failed to create device symlink '%s': %m", filename);
×
610

611
        return 0;
612
}
613

614
int udev_node_remove(sd_device *dev) {
9,078✔
615
        _cleanup_free_ char *filename = NULL;
9,078✔
616
        int r;
9,078✔
617

618
        assert(dev);
9,078✔
619

620
        /* remove/update symlinks, remove symlinks from name index */
621
        FOREACH_DEVICE_DEVLINK(dev, devlink) {
62,409✔
622
                r = link_update(dev, devlink, /* add= */ false);
53,331✔
623
                if (r < 0)
53,331✔
624
                        log_device_warning_errno(dev, r,
×
625
                                                 "Failed to remove/update device symlink '%s', ignoring: %m",
626
                                                 devlink);
627
        }
628

629
        r = device_get_devpath_by_devnum(dev, &filename);
9,078✔
630
        if (r < 0)
9,078✔
631
                return log_device_debug_errno(dev, r, "Failed to get device path: %m");
×
632

633
        /* remove /dev/{block,char}/$major:$minor */
634
        return node_remove_symlink(dev, filename);
9,078✔
635
}
636

637
static int udev_node_apply_permissions_impl(
34,551✔
638
                sd_device *dev, /* can be NULL, only used for logging. */
639
                int node_fd,
640
                const char *devnode,
641
                bool apply_mac,
642
                mode_t mode,
643
                uid_t uid,
644
                gid_t gid,
645
                OrderedHashmap *seclabel_list) {
646

647
        bool apply_mode, apply_uid, apply_gid;
34,551✔
648
        struct stat stats;
34,551✔
649
        int r;
34,551✔
650

651
        assert(node_fd >= 0);
34,551✔
652
        assert(devnode);
34,551✔
653

654
        if (fstat(node_fd, &stats) < 0)
34,551✔
655
                return log_device_debug_errno(dev, errno, "cannot stat() node %s: %m", devnode);
×
656

657
        /* If group is set, but mode is not set, "upgrade" mode for the group. */
658
        if (mode == MODE_INVALID && gid_is_valid(gid) && gid > 0)
63,639✔
659
                mode = 0660;
660

661
        apply_mode = mode != MODE_INVALID && (stats.st_mode & 0777) != (mode & 0777);
15,239✔
662
        apply_uid = uid_is_valid(uid) && stats.st_uid != uid;
34,551✔
663
        apply_gid = gid_is_valid(gid) && stats.st_gid != gid;
34,551✔
664

665
        if (apply_mode || apply_uid || apply_gid || apply_mac) {
34,551✔
666
                bool selinux = false, smack = false;
15,145✔
667
                const char *name, *label;
15,145✔
668

669
                if (apply_mode || apply_uid || apply_gid) {
15,145✔
670
                        log_device_debug(dev, "Setting permissions %s, uid=" UID_FMT ", gid=" GID_FMT ", mode=%#o",
18,318✔
671
                                         devnode,
672
                                         uid_is_valid(uid) ? uid : stats.st_uid,
673
                                         gid_is_valid(gid) ? gid : stats.st_gid,
674
                                         mode != MODE_INVALID ? mode & 0777 : stats.st_mode & 0777);
675

676
                        r = fchmod_and_chown(node_fd, mode, uid, gid);
9,159✔
677
                        if (r < 0)
9,159✔
678
                                log_device_full_errno(dev, r == -ENOENT ? LOG_DEBUG : LOG_ERR, r,
×
679
                                                      "Failed to set owner/mode of %s to uid=" UID_FMT
680
                                                      ", gid=" GID_FMT ", mode=%#o: %m",
681
                                                      devnode,
682
                                                      uid_is_valid(uid) ? uid : stats.st_uid,
683
                                                      gid_is_valid(gid) ? gid : stats.st_gid,
684
                                                      mode != MODE_INVALID ? mode & 0777 : stats.st_mode & 0777);
685
                } else
686
                        log_device_debug(dev, "Preserve permissions of %s, uid=" UID_FMT ", gid=" GID_FMT ", mode=%#o",
11,972✔
687
                                         devnode,
688
                                         uid_is_valid(uid) ? uid : stats.st_uid,
689
                                         gid_is_valid(gid) ? gid : stats.st_gid,
690
                                         mode != MODE_INVALID ? mode & 0777 : stats.st_mode & 0777);
691

692
                /* apply SECLABEL{$module}=$label */
693
                ORDERED_HASHMAP_FOREACH_KEY(label, name, seclabel_list) {
15,145✔
694
                        int q;
×
695

696
                        if (streq(name, "selinux")) {
×
697
                                selinux = true;
×
698

699
                                q = mac_selinux_apply_fd(node_fd, devnode, label);
×
700
                                if (q < 0)
×
701
                                        log_device_full_errno(dev, q == -ENOENT ? LOG_DEBUG : LOG_ERR, q,
×
702
                                                              "SECLABEL: failed to set SELinux label '%s': %m", label);
703
                                else
704
                                        log_device_debug(dev, "SECLABEL: set SELinux label '%s'", label);
×
705

706
                        } else if (streq(name, "smack")) {
×
707
                                smack = true;
×
708

709
                                q = mac_smack_apply_fd(node_fd, SMACK_ATTR_ACCESS, label);
×
710
                                if (q < 0)
×
711
                                        log_device_full_errno(dev, q == -ENOENT ? LOG_DEBUG : LOG_ERR, q,
×
712
                                                              "SECLABEL: failed to set SMACK label '%s': %m", label);
713
                                else
714
                                        log_device_debug(dev, "SECLABEL: set SMACK label '%s'", label);
×
715

716
                        } else
717
                                log_device_error(dev, "SECLABEL: unknown subsystem, ignoring '%s'='%s'", name, label);
×
718
                }
719

720
                /* set the defaults */
721
                if (!selinux)
15,145✔
722
                        (void) mac_selinux_fix_full(node_fd, NULL, devnode, LABEL_IGNORE_ENOENT);
15,145✔
723
                if (!smack)
15,145✔
724
                        (void) mac_smack_apply_fd(node_fd, SMACK_ATTR_ACCESS, NULL);
15,145✔
725
        }
726

727
        /* always update timestamp when we re-use the node, like on media change events */
728
        r = futimens_opath(node_fd, NULL);
34,551✔
729
        if (r < 0)
34,551✔
730
                log_device_debug_errno(dev, r, "Failed to adjust timestamp of node %s: %m", devnode);
×
731

732
        return 0;
733
}
734

735
int udev_node_apply_permissions(
40,600✔
736
                sd_device *dev,
737
                bool apply_mac,
738
                mode_t mode,
739
                uid_t uid,
740
                gid_t gid,
741
                OrderedHashmap *seclabel_list) {
742

743
        const char *devnode;
40,600✔
744
        _cleanup_close_ int node_fd = -EBADF;
40,600✔
745
        int r;
40,600✔
746

747
        assert(dev);
40,600✔
748

749
        r = sd_device_get_devname(dev, &devnode);
40,600✔
750
        if (r < 0)
40,600✔
751
                return log_device_debug_errno(dev, r, "Failed to get devname: %m");
×
752

753
        node_fd = sd_device_open(dev, O_PATH|O_CLOEXEC);
40,600✔
754
        if (node_fd < 0) {
40,600✔
755
                if (ERRNO_IS_DEVICE_ABSENT_OR_EMPTY(node_fd)) {
6,607✔
756
                        log_device_debug_errno(dev, node_fd, "Device node %s is missing, skipping handling.", devnode);
6,607✔
757
                        return 0; /* This is necessarily racey, so ignore missing the device */
6,607✔
758
                }
759

760
                return log_device_debug_errno(dev, node_fd, "Cannot open node %s: %m", devnode);
×
761
        }
762

763
        return udev_node_apply_permissions_impl(dev, node_fd, devnode, apply_mac, mode, uid, gid, seclabel_list);
33,993✔
764
}
765

766
int static_node_apply_permissions(
583✔
767
                const char *name,
768
                mode_t mode,
769
                uid_t uid,
770
                gid_t gid,
771
                char **tags) {
772

773
        _cleanup_free_ char *unescaped_filename = NULL;
583✔
774
        _cleanup_close_ int node_fd = -EBADF;
583✔
775
        const char *devnode;
583✔
776
        struct stat stats;
583✔
777
        int r;
583✔
778

779
        assert(name);
583✔
780

781
        if (uid == UID_INVALID && gid == GID_INVALID && mode == MODE_INVALID && !tags)
583✔
782
                return 0;
783

784
        devnode = strjoina("/dev/", name);
2,915✔
785

786
        node_fd = open(devnode, O_PATH|O_CLOEXEC);
583✔
787
        if (node_fd < 0) {
583✔
788
                bool ignore = ERRNO_IS_DEVICE_ABSENT_OR_EMPTY(errno);
25✔
789
                log_full_errno(ignore ? LOG_DEBUG : LOG_WARNING, errno,
25✔
790
                               "Failed to open device node '%s'%s: %m",
791
                               devnode, ignore ? ", ignoring" : "");
792
                return ignore ? 0 : -errno;
25✔
793
        }
794

795
        if (fstat(node_fd, &stats) < 0)
558✔
796
                return log_error_errno(errno, "Failed to stat %s: %m", devnode);
×
797

798
        if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode)) {
558✔
799
                log_warning("%s is neither block nor character device, ignoring.", devnode);
×
800
                return 0;
×
801
        }
802

803
        if (!strv_isempty(tags)) {
558✔
804
                unescaped_filename = xescape(name, "/.");
106✔
805
                if (!unescaped_filename)
106✔
806
                        return log_oom();
×
807
        }
808

809
        /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
810
        STRV_FOREACH(t, tags) {
664✔
811
                _cleanup_free_ char *p = NULL;
106✔
812

813
                p = path_join("/run/udev/static_node-tags/", *t, unescaped_filename);
106✔
814
                if (!p)
106✔
815
                        return log_oom();
×
816

817
                r = mkdir_parents(p, 0755);
106✔
818
                if (r < 0)
106✔
819
                        return log_error_errno(r, "Failed to create parent directory for %s: %m", p);
×
820

821
                r = symlink(devnode, p);
106✔
822
                if (r < 0 && errno != EEXIST)
106✔
823
                        return log_error_errno(errno, "Failed to create symlink %s -> %s: %m", p, devnode);
×
824
        }
825

826
        return udev_node_apply_permissions_impl(NULL, node_fd, devnode, false, mode, uid, gid, NULL);
558✔
827
}
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