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

systemd / systemd / 20766109442

06 Jan 2026 09:50PM UTC coverage: 72.714% (+0.3%) from 72.444%
20766109442

push

github

YHNdnzj
man: do not manually update man/rules/meson.build

Follow-up for 25393c7c9.

310283 of 426715 relevant lines covered (72.71%)

1142928.51 hits per line

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

77.31
/src/shared/udev-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <ctype.h>
4
#include <time.h>
5
#include <unistd.h>
6

7
#include "sd-event.h"
8
#include "sd-id128.h"
9

10
#include "alloc-util.h"
11
#include "device-nodes.h"
12
#include "device-private.h"
13
#include "device-util.h"
14
#include "errno-util.h"
15
#include "hashmap.h"
16
#include "log.h"
17
#include "path-util.h"
18
#include "stat-util.h"
19
#include "string-util.h"
20
#include "strv.h"
21
#include "udev-util.h"
22
#include "utf8.h"
23

24
int udev_parse_config_full(const ConfigTableItem config_table[]) {
22,271✔
25
        int r;
22,271✔
26

27
        assert(config_table);
22,271✔
28

29
        r = config_parse_standard_file_with_dropins(
22,271✔
30
                        "udev/udev.conf",
31
                        /* sections= */ NULL,
32
                        config_item_table_lookup, config_table,
33
                        CONFIG_PARSE_WARN,
34
                        /* userdata= */ NULL);
35
        if (r == -ENOENT)
22,271✔
36
                return 0;
×
37
        return r;
38
}
39

40
int udev_parse_config(void) {
22,113✔
41
        int r, log_val = -1;
22,113✔
42
        const ConfigTableItem config_table[] = {
22,113✔
43
                { NULL, "udev_log",       config_parse_log_level, 0, &log_val },
44
                { NULL, "children_max",   NULL,                   0, NULL     },
45
                { NULL, "exec_delay",     NULL,                   0, NULL     },
46
                { NULL, "event_timeout",  NULL,                   0, NULL     },
47
                { NULL, "resolve_names",  NULL,                   0, NULL     },
48
                { NULL, "timeout_signal", NULL,                   0, NULL     },
49
                {}
50
        };
51

52
        r = udev_parse_config_full(config_table);
22,113✔
53
        if (r < 0)
22,113✔
54
                return r;
22,113✔
55

56
        if (log_val >= 0)
22,113✔
57
                log_set_max_level(log_val);
×
58

59
        return 0;
60
}
61

62
struct DeviceMonitorData {
63
        const char *sysname;
64
        const char *devlink;
65
        sd_device *device;
66
};
67

68
static void device_monitor_data_free(struct DeviceMonitorData *d) {
5✔
69
        assert(d);
5✔
70

71
        sd_device_unref(d->device);
5✔
72
}
5✔
73

74
static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) {
×
75
        struct DeviceMonitorData *data = ASSERT_PTR(userdata);
×
76
        const char *sysname;
×
77

78
        assert(device);
×
79
        assert(data->sysname || data->devlink);
×
80
        assert(!data->device);
×
81

82
        /* Ignore REMOVE events here. We are waiting for initialization after all, not de-initialization. We
83
         * might see a REMOVE event from an earlier use of the device (devices by the same name are recycled
84
         * by the kernel after all), which we should not get confused by. After all we cannot distinguish use
85
         * cycles of the devices, as the udev queue is entirely asynchronous.
86
         *
87
         * If we see a REMOVE event here for the use cycle we actually care about then we won't notice of
88
         * course, but that should be OK, given the timeout logic used on the wait loop: this will be noticed
89
         * by means of -ETIMEDOUT. Thus we won't notice immediately, but eventually, and that should be
90
         * sufficient for an error path that should regularly not happen.
91
         *
92
         * (And yes, we only need to special case REMOVE. It's the only "negative" event type, where a device
93
         * ceases to exist. All other event types are "positive": the device exists and is registered in the
94
         * udev database, thus whenever we see the event, we can consider it initialized.) */
95
        if (device_for_action(device, SD_DEVICE_REMOVE))
×
96
                return 0;
×
97

98
        if (data->sysname && sd_device_get_sysname(device, &sysname) >= 0 && streq(sysname, data->sysname))
×
99
                goto found;
×
100

101
        if (data->devlink) {
×
102
                const char *devlink;
×
103

104
                FOREACH_DEVICE_DEVLINK(device, link)
×
105
                        if (path_equal(link, data->devlink))
×
106
                                goto found;
×
107

108
                if (sd_device_get_devname(device, &devlink) >= 0 && path_equal(devlink, data->devlink))
×
109
                        goto found;
×
110
        }
111

112
        return 0;
113

114
found:
×
115
        data->device = sd_device_ref(device);
×
116
        return sd_event_exit(sd_device_monitor_get_event(monitor), 0);
×
117
}
118

119
static int device_wait_for_initialization_internal(
5✔
120
                sd_device *_device,
121
                const char *devlink,
122
                const char *subsystem,
123
                usec_t timeout_usec,
124
                sd_device **ret) {
125

126
        _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
5✔
127
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
5✔
128
        /* Ensure that if !_device && devlink, device gets unrefd on errors since it will be new */
129
        _cleanup_(sd_device_unrefp) sd_device *device = sd_device_ref(_device);
10✔
130
        _cleanup_(device_monitor_data_free) struct DeviceMonitorData data = {
5✔
131
                .devlink = devlink,
132
        };
133
        int r;
5✔
134

135
        assert(device || (subsystem && devlink));
5✔
136

137
        /* Devlink might already exist, if it does get the device to use the sysname filtering */
138
        if (!device && devlink) {
5✔
139
                r = sd_device_new_from_devname(&device, devlink);
×
140
                if (r < 0 && !ERRNO_IS_DEVICE_ABSENT(r))
×
141
                        return log_error_errno(r, "Failed to create sd-device object from %s: %m", devlink);
×
142
        }
143

144
        if (device) {
5✔
145
                if (device_is_processed(device) > 0) {
5✔
146
                        if (ret)
4✔
147
                                *ret = sd_device_ref(device);
4✔
148
                        return 0;
4✔
149
                }
150
                /* We need either the sysname or the devlink for filtering */
151
                assert_se(sd_device_get_sysname(device, &data.sysname) >= 0 || devlink);
1✔
152
        }
153

154
        /* Wait until the device is initialized, so that we can get access to the ID_PATH property */
155

156
        r = sd_event_new(&event);
1✔
157
        if (r < 0)
1✔
158
                return log_error_errno(r, "Failed to get default event: %m");
×
159

160
        r = sd_device_monitor_new(&monitor);
1✔
161
        if (r < 0)
1✔
162
                return log_error_errno(r, "Failed to acquire monitor: %m");
×
163

164
        if (device && !subsystem) {
1✔
165
                r = sd_device_get_subsystem(device, &subsystem);
1✔
166
                if (r < 0 && r != -ENOENT)
1✔
167
                        return log_device_error_errno(device, r, "Failed to get subsystem: %m");
×
168
        }
169

170
        if (subsystem) {
1✔
171
                r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, subsystem, NULL);
1✔
172
                if (r < 0)
1✔
173
                        return log_error_errno(r, "Failed to add %s subsystem match to monitor: %m", subsystem);
×
174
        }
175

176
        _cleanup_free_ char *desc = NULL;
1✔
177
        const char *sysname = NULL;
1✔
178
        if (device)
1✔
179
                (void) sd_device_get_sysname(device, &sysname);
1✔
180

181
        desc = strjoin(sysname ?: subsystem, devlink ? ":" : ":initialization", devlink);
2✔
182
        if (desc)
1✔
183
                (void) sd_device_monitor_set_description(monitor, desc);
1✔
184

185
        r = sd_device_monitor_attach_event(monitor, event);
1✔
186
        if (r < 0)
1✔
187
                return log_error_errno(r, "Failed to attach event to device monitor: %m");
×
188

189
        r = sd_device_monitor_start(monitor, device_monitor_handler, &data);
1✔
190
        if (r < 0)
1✔
191
                return log_error_errno(r, "Failed to start device monitor: %m");
×
192

193
        if (timeout_usec != USEC_INFINITY) {
1✔
194
                r = sd_event_add_time_relative(
1✔
195
                                event, NULL,
196
                                CLOCK_MONOTONIC, timeout_usec, 0,
197
                                NULL, INT_TO_PTR(-ETIMEDOUT));
198
                if (r < 0)
1✔
199
                        return log_error_errno(r, "Failed to add timeout event source: %m");
×
200
        }
201

202
        /* Check again, maybe things changed. Udev will re-read the db if the device wasn't initialized yet. */
203
        if (!device && devlink) {
1✔
204
                r = sd_device_new_from_devname(&device, devlink);
×
205
                if (r < 0 && !ERRNO_IS_DEVICE_ABSENT(r))
×
206
                        return log_error_errno(r, "Failed to create sd-device object from %s: %m", devlink);
×
207
        }
208
        if (device && device_is_processed(device) > 0) {
1✔
209
                if (ret)
×
210
                        *ret = sd_device_ref(device);
×
211
                return 0;
×
212
        }
213

214
        r = sd_event_loop(event);
1✔
215
        if (r < 0)
1✔
216
                return log_error_errno(r, "Failed to wait for device to be initialized: %m");
1✔
217

218
        if (ret)
×
219
                *ret = TAKE_PTR(data.device);
×
220
        return 0;
221
}
222

223
int device_wait_for_initialization(sd_device *device, const char *subsystem, usec_t timeout_usec, sd_device **ret) {
5✔
224
        return device_wait_for_initialization_internal(device, NULL, subsystem, timeout_usec, ret);
5✔
225
}
226

227
int device_wait_for_devlink(const char *devlink, const char *subsystem, usec_t timeout_usec, sd_device **ret) {
×
228
        return device_wait_for_initialization_internal(NULL, devlink, subsystem, timeout_usec, ret);
×
229
}
230

231
int device_is_renaming(sd_device *dev) {
87,357✔
232
        int r;
87,357✔
233

234
        assert(dev);
87,357✔
235

236
        r = device_get_property_bool(dev, "ID_RENAMING");
87,357✔
237
        if (r == -ENOENT)
87,357✔
238
                return false; /* defaults to false */
87,207✔
239

240
        return r;
241
}
242

243
int device_is_processed(sd_device *dev) {
30,299✔
244
        int r;
30,299✔
245

246
        assert(dev);
30,299✔
247

248
        /* sd_device_get_is_initialized() only checks if the udev database file exists. However, even if the
249
         * database file exist, systemd-udevd may be still processing the device, e.g. when the udev rules
250
         * for the device have RUN tokens. See issue #30056. Hence, to check if the device is really
251
         * processed by systemd-udevd, we also need to read ID_PROCESSING property. */
252

253
        r = sd_device_get_is_initialized(dev);
30,299✔
254
        if (r <= 0)
30,299✔
255
                return r;
256

257
        r = device_get_property_bool(dev, "ID_PROCESSING");
29,717✔
258
        if (r == -ENOENT)
29,717✔
259
                return true; /* If the property does not exist, then it means that the device is processed. */
260
        if (r < 0)
1,239✔
261
                return r;
262

263
        return !r;
1,239✔
264
}
265

266
bool device_for_action(sd_device *dev, sd_device_action_t a) {
511,430✔
267
        sd_device_action_t b;
511,430✔
268

269
        assert(dev);
511,430✔
270

271
        if (a < 0)
511,430✔
272
                return false;
511,430✔
273

274
        if (sd_device_get_action(dev, &b) < 0)
511,430✔
275
                return false;
276

277
        return a == b;
466,513✔
278
}
279

280
void log_device_uevent(sd_device *device, const char *str) {
297,890✔
281
        sd_device_action_t action = _SD_DEVICE_ACTION_INVALID;
297,890✔
282
        sd_id128_t event_id = SD_ID128_NULL;
297,890✔
283
        uint64_t seqnum = 0;
297,890✔
284

285
        if (!DEBUG_LOGGING)
297,890✔
286
                return;
16,997✔
287

288
        (void) sd_device_get_seqnum(device, &seqnum);
280,893✔
289
        (void) sd_device_get_action(device, &action);
280,893✔
290
        (void) sd_device_get_trigger_uuid(device, &event_id);
280,893✔
291
        log_device_debug(device, "%s%s(SEQNUM=%"PRIu64", ACTION=%s%s%s)",
962,412✔
292
                         strempty(str), isempty(str) ? "" : " ",
293
                         seqnum, strna(device_action_to_string(action)),
294
                         sd_id128_is_null(event_id) ? "" : ", UUID=",
295
                         sd_id128_is_null(event_id) ? "" : SD_ID128_TO_UUID_STRING(event_id));
296
}
297

298
size_t udev_replace_whitespace(const char *str, char *to, size_t len) {
333,602✔
299
        bool is_space = false;
333,602✔
300
        size_t i, j;
333,602✔
301

302
        assert(str);
333,602✔
303
        assert(to);
333,602✔
304

305
        /* Copy from 'str' to 'to', while removing all leading and trailing whitespace, and replacing
306
         * each run of consecutive whitespace with a single underscore. The chars from 'str' are copied
307
         * up to the \0 at the end of the string, or at most 'len' chars.  This appends \0 to 'to', at
308
         * the end of the copied characters.
309
         *
310
         * If 'len' chars are copied into 'to', the final \0 is placed at len+1 (i.e. 'to[len] = \0'),
311
         * so the 'to' buffer must have at least len+1 chars available.
312
         *
313
         * Note this may be called with 'str' == 'to', i.e. to replace whitespace in-place in a buffer.
314
         * This function can handle that situation.
315
         *
316
         * Note that only 'len' characters are read from 'str'. */
317

318
        i = strspn(str, WHITESPACE);
333,602✔
319

320
        for (j = 0; j < len && i < len && str[i] != '\0'; i++) {
5,420,767✔
321
                if (isspace(str[i])) {
5,087,165✔
322
                        is_space = true;
82,000✔
323
                        continue;
82,000✔
324
                }
325

326
                if (is_space) {
5,005,165✔
327
                        if (j + 1 >= len)
16,263✔
328
                                break;
329

330
                        to[j++] = '_';
16,263✔
331
                        is_space = false;
16,263✔
332
                }
333
                to[j++] = str[i];
5,005,165✔
334
        }
335

336
        to[j] = '\0';
333,602✔
337
        return j;
333,602✔
338
}
339

340
size_t udev_replace_chars(char *str, const char *allow) {
189,257✔
341
        size_t i = 0, replaced = 0;
189,257✔
342

343
        assert(str);
189,257✔
344

345
        /* allow chars in allow list, plain ascii, hex-escaping and valid utf8. */
346

347
        while (str[i] != '\0') {
8,551,544✔
348
                int len;
8,362,287✔
349

350
                if (allow_listed_char_for_devnode(str[i], allow)) {
8,362,287✔
351
                        i++;
8,356,931✔
352
                        continue;
8,356,931✔
353
                }
354

355
                /* accept hex encoding */
356
                if (str[i] == '\\' && str[i+1] == 'x') {
5,356✔
357
                        i += 2;
3,336✔
358
                        continue;
3,336✔
359
                }
360

361
                /* accept valid utf8 */
362
                len = utf8_encoded_valid_unichar(str + i, SIZE_MAX);
2,020✔
363
                if (len > 1) {
2,020✔
364
                        i += len;
×
365
                        continue;
×
366
                }
367

368
                /* if space is allowed, replace whitespace with ordinary space */
369
                if (isspace(str[i]) && allow && strchr(allow, ' ')) {
2,020✔
370
                        str[i] = ' ';
×
371
                        i++;
×
372
                        replaced++;
×
373
                        continue;
×
374
                }
375

376
                /* everything else is replaced with '_' */
377
                str[i] = '_';
2,020✔
378
                i++;
2,020✔
379
                replaced++;
2,020✔
380
        }
381
        return replaced;
189,257✔
382
}
383

384
int udev_queue_is_empty(void) {
11,161✔
385
        return access("/run/udev/queue", F_OK) < 0 ?
11,161✔
386
                (errno == ENOENT ? true : -errno) : false;
11,161✔
387
}
388

389
static int cached_udev_availability = -1;
390

391
void reset_cached_udev_availability(void) {
5✔
392
        cached_udev_availability = -1;
5✔
393
}
5✔
394

395
bool udev_available(void) {
88,439✔
396
        /* The service systemd-udevd is started only when /sys is read write.
397
         * See systemd-udevd.service: ConditionPathIsReadWrite=/sys
398
         * Also, our container interface (http://systemd.io/CONTAINER_INTERFACE/) states that /sys must
399
         * be mounted in read-only mode in containers. */
400

401
        if (cached_udev_availability >= 0)
88,439✔
402
                return cached_udev_availability;
87,734✔
403

404
        return (cached_udev_availability = (path_is_read_only_fs("/sys/") <= 0));
705✔
405
}
406

407
int device_get_vendor_string(sd_device *device, const char **ret) {
2,028✔
408
        int r;
2,028✔
409

410
        assert(device);
2,028✔
411

412
        FOREACH_STRING(field, "ID_VENDOR_FROM_DATABASE", "ID_VENDOR") {
6,084✔
413
                r = sd_device_get_property_value(device, field, ret);
4,056✔
414
                if (r != -ENOENT)
4,056✔
415
                        return r;
×
416
        }
417

418
        return -ENOENT;
2,028✔
419
}
420

421
int device_get_model_string(sd_device *device, const char **ret) {
53,504✔
422
        int r;
53,504✔
423

424
        assert(device);
53,504✔
425

426
        FOREACH_STRING(field, "ID_MODEL_FROM_DATABASE", "ID_MODEL") {
160,431✔
427
                r = sd_device_get_property_value(device, field, ret);
107,008✔
428
                if (r != -ENOENT)
107,008✔
429
                        return r;
81✔
430
        }
431

432
        return -ENOENT;
53,423✔
433
}
434

435
int device_get_property_value_with_fallback(
7,240,421✔
436
                sd_device *device,
437
                const char *prop,
438
                Hashmap *extra_props,
439
                const char **ret) {
440
        const char *value;
7,240,421✔
441
        int r;
7,240,421✔
442

443
        assert(device);
7,240,421✔
444
        assert(prop);
7,240,421✔
445
        assert(ret);
7,240,421✔
446

447
        r = sd_device_get_property_value(device, prop, &value);
7,240,421✔
448
        if (r < 0) {
7,240,421✔
449
                if (r != -ENOENT)
5,958,601✔
450
                        return r;
7,240,421✔
451

452
                value = hashmap_get(extra_props, prop);
5,958,601✔
453
                if (!value)
5,958,601✔
454
                        return -ENOENT;
455
        }
456

457
        *ret = value;
1,281,842✔
458

459
        return 1;
1,281,842✔
460
}
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