]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/device.c
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[thirdparty/systemd.git] / src / core / device.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <sys/epoll.h>
5
6 #include "sd-messages.h"
7
8 #include "alloc-util.h"
9 #include "bus-common-errors.h"
10 #include "dbus-device.h"
11 #include "dbus-unit.h"
12 #include "device-private.h"
13 #include "device-util.h"
14 #include "device.h"
15 #include "log.h"
16 #include "parse-util.h"
17 #include "path-util.h"
18 #include "ratelimit.h"
19 #include "serialize.h"
20 #include "stat-util.h"
21 #include "string-util.h"
22 #include "swap.h"
23 #include "udev-util.h"
24 #include "unit-name.h"
25 #include "unit.h"
26
27 static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = {
28 [DEVICE_DEAD] = UNIT_INACTIVE,
29 [DEVICE_TENTATIVE] = UNIT_ACTIVATING,
30 [DEVICE_PLUGGED] = UNIT_ACTIVE,
31 };
32
33 static int device_dispatch_io(sd_device_monitor *monitor, sd_device *dev, void *userdata);
34
35 static int device_by_path(Manager *m, const char *path, Unit **ret) {
36 _cleanup_free_ char *e = NULL;
37 Unit *u;
38 int r;
39
40 assert(m);
41 assert(path);
42
43 r = unit_name_from_path(path, ".device", &e);
44 if (r < 0)
45 return r;
46
47 u = manager_get_unit(m, e);
48 if (!u)
49 return -ENOENT;
50
51 if (ret)
52 *ret = u;
53 return 0;
54 }
55
56 static void device_unset_sysfs(Device *d) {
57 Hashmap *devices;
58
59 assert(d);
60
61 if (!d->sysfs)
62 return;
63
64 /* Remove this unit from the chain of devices which share the same sysfs path. */
65
66 devices = UNIT(d)->manager->devices_by_sysfs;
67
68 if (d->same_sysfs_prev)
69 /* If this is not the first unit, then simply remove this unit. */
70 d->same_sysfs_prev->same_sysfs_next = d->same_sysfs_next;
71 else if (d->same_sysfs_next)
72 /* If this is the first unit, replace with the next unit. */
73 assert_se(hashmap_replace(devices, d->same_sysfs_next->sysfs, d->same_sysfs_next) >= 0);
74 else
75 /* Otherwise, remove the entry. */
76 hashmap_remove(devices, d->sysfs);
77
78 if (d->same_sysfs_next)
79 d->same_sysfs_next->same_sysfs_prev = d->same_sysfs_prev;
80
81 d->same_sysfs_prev = d->same_sysfs_next = NULL;
82
83 d->sysfs = mfree(d->sysfs);
84 }
85
86 static int device_set_sysfs(Device *d, const char *sysfs) {
87 _cleanup_free_ char *copy = NULL;
88 Device *first;
89 int r;
90
91 assert(d);
92
93 if (streq_ptr(d->sysfs, sysfs))
94 return 0;
95
96 r = hashmap_ensure_allocated(&UNIT(d)->manager->devices_by_sysfs, &path_hash_ops);
97 if (r < 0)
98 return r;
99
100 copy = strdup(sysfs);
101 if (!copy)
102 return -ENOMEM;
103
104 device_unset_sysfs(d);
105
106 first = hashmap_get(UNIT(d)->manager->devices_by_sysfs, sysfs);
107 LIST_PREPEND(same_sysfs, first, d);
108
109 r = hashmap_replace(UNIT(d)->manager->devices_by_sysfs, copy, first);
110 if (r < 0) {
111 LIST_REMOVE(same_sysfs, first, d);
112 return r;
113 }
114
115 d->sysfs = TAKE_PTR(copy);
116 unit_add_to_dbus_queue(UNIT(d));
117
118 return 0;
119 }
120
121 static void device_init(Unit *u) {
122 Device *d = DEVICE(u);
123
124 assert(d);
125 assert(UNIT(d)->load_state == UNIT_STUB);
126
127 /* In contrast to all other unit types we timeout jobs waiting
128 * for devices by default. This is because they otherwise wait
129 * indefinitely for plugged in devices, something which cannot
130 * happen for the other units since their operations time out
131 * anyway. */
132 u->job_running_timeout = u->manager->defaults.device_timeout_usec;
133
134 u->ignore_on_isolate = true;
135
136 d->deserialized_state = _DEVICE_STATE_INVALID;
137 }
138
139 static void device_done(Unit *u) {
140 Device *d = DEVICE(u);
141
142 assert(d);
143
144 device_unset_sysfs(d);
145 d->deserialized_sysfs = mfree(d->deserialized_sysfs);
146 d->wants_property = strv_free(d->wants_property);
147 d->path = mfree(d->path);
148 }
149
150 static int device_load(Unit *u) {
151 int r;
152
153 r = unit_load_fragment_and_dropin(u, false);
154 if (r < 0)
155 return r;
156
157 if (!u->description) {
158 /* Generate a description based on the path, to be used until the device is initialized
159 properly */
160 r = unit_name_to_path(u->id, &u->description);
161 if (r < 0)
162 log_unit_debug_errno(u, r, "Failed to unescape name: %m");
163 }
164
165 return 0;
166 }
167
168 static void device_set_state(Device *d, DeviceState state) {
169 DeviceState old_state;
170
171 assert(d);
172
173 if (d->state != state)
174 bus_unit_send_pending_change_signal(UNIT(d), false);
175
176 old_state = d->state;
177 d->state = state;
178
179 if (state == DEVICE_DEAD)
180 device_unset_sysfs(d);
181
182 if (state != old_state)
183 log_unit_debug(UNIT(d), "Changed %s -> %s", device_state_to_string(old_state), device_state_to_string(state));
184
185 unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state], /* reload_success = */ true);
186 }
187
188 static void device_found_changed(Device *d, DeviceFound previous, DeviceFound now) {
189 assert(d);
190
191 /* Didn't exist before, but does now? if so, generate a new invocation ID for it */
192 if (previous == DEVICE_NOT_FOUND && now != DEVICE_NOT_FOUND)
193 (void) unit_acquire_invocation_id(UNIT(d));
194
195 if (FLAGS_SET(now, DEVICE_FOUND_UDEV))
196 /* When the device is known to udev we consider it plugged. */
197 device_set_state(d, DEVICE_PLUGGED);
198 else if (now != DEVICE_NOT_FOUND && !FLAGS_SET(previous, DEVICE_FOUND_UDEV))
199 /* If the device has not been seen by udev yet, but is now referenced by the kernel, then we assume the
200 * kernel knows it now, and udev might soon too. */
201 device_set_state(d, DEVICE_TENTATIVE);
202 else
203 /* If nobody sees the device, or if the device was previously seen by udev and now is only referenced
204 * from the kernel, then we consider the device is gone, the kernel just hasn't noticed it yet. */
205 device_set_state(d, DEVICE_DEAD);
206 }
207
208 static void device_update_found_one(Device *d, DeviceFound found, DeviceFound mask) {
209 assert(d);
210
211 if (MANAGER_IS_RUNNING(UNIT(d)->manager)) {
212 DeviceFound n, previous;
213
214 /* When we are already running, then apply the new mask right-away, and trigger state changes
215 * right-away */
216
217 n = (d->found & ~mask) | (found & mask);
218 if (n == d->found)
219 return;
220
221 previous = d->found;
222 d->found = n;
223
224 device_found_changed(d, previous, n);
225 } else
226 /* We aren't running yet, let's apply the new mask to the shadow variable instead, which we'll apply as
227 * soon as we catch-up with the state. */
228 d->enumerated_found = (d->enumerated_found & ~mask) | (found & mask);
229 }
230
231 static void device_update_found_by_sysfs(Manager *m, const char *sysfs, DeviceFound found, DeviceFound mask) {
232 Device *l;
233
234 assert(m);
235 assert(sysfs);
236
237 if (mask == 0)
238 return;
239
240 l = hashmap_get(m->devices_by_sysfs, sysfs);
241 LIST_FOREACH(same_sysfs, d, l)
242 device_update_found_one(d, found, mask);
243 }
244
245 static void device_update_found_by_name(Manager *m, const char *path, DeviceFound found, DeviceFound mask) {
246 Unit *u;
247
248 assert(m);
249 assert(path);
250
251 if (mask == 0)
252 return;
253
254 if (device_by_path(m, path, &u) < 0)
255 return;
256
257 device_update_found_one(DEVICE(u), found, mask);
258 }
259
260 static int device_coldplug(Unit *u) {
261 Device *d = DEVICE(u);
262
263 assert(d);
264 assert(d->state == DEVICE_DEAD);
265
266 /* First, let's put the deserialized state and found mask into effect, if we have it. */
267 if (d->deserialized_state < 0)
268 return 0;
269
270 Manager *m = u->manager;
271 DeviceFound found = d->deserialized_found;
272 DeviceState state = d->deserialized_state;
273
274 /* On initial boot, switch-root, reload, reexecute, the following happen:
275 * 1. MANAGER_IS_RUNNING() == false
276 * 2. enumerate devices: manager_enumerate() -> device_enumerate()
277 * Device.enumerated_found is set.
278 * 3. deserialize devices: manager_deserialize() -> device_deserialize_item()
279 * Device.deserialize_state and Device.deserialized_found are set.
280 * 4. coldplug devices: manager_coldplug() -> device_coldplug()
281 * deserialized properties are copied to the main properties.
282 * 5. MANAGER_IS_RUNNING() == true: manager_ready()
283 * 6. catchup devices: manager_catchup() -> device_catchup()
284 * Device.enumerated_found is applied to Device.found, and state is updated based on that.
285 *
286 * Notes:
287 * - On initial boot, no udev database exists. Hence, no devices are enumerated in the step 2.
288 * Also, there is no deserialized device. Device units are (a) generated based on dependencies of
289 * other units, or (b) generated when uevents are received.
290 *
291 * - On switch-root, the udev database may be cleared, except for devices with sticky bit, i.e.
292 * OPTIONS="db_persist". Hence, almost no devices are enumerated in the step 2. However, in
293 * general, we have several serialized devices. So, DEVICE_FOUND_UDEV bit in the
294 * Device.deserialized_found must be ignored, as udev rules in initrd and the main system are often
295 * different. If the deserialized state is DEVICE_PLUGGED, we need to downgrade it to
296 * DEVICE_TENTATIVE. Unlike the other starting mode, MANAGER_IS_SWITCHING_ROOT() is true when
297 * device_coldplug() and device_catchup() are called. Hence, let's conditionalize the operations by
298 * using the flag. After switch-root, systemd-udevd will (re-)process all devices, and the
299 * Device.found and Device.state will be adjusted.
300 *
301 * - On reload or reexecute, we can trust Device.enumerated_found, Device.deserialized_found, and
302 * Device.deserialized_state. Of course, deserialized parameters may be outdated, but the unit
303 * state can be adjusted later by device_catchup() or uevents. */
304
305 if (MANAGER_IS_SWITCHING_ROOT(m) &&
306 !FLAGS_SET(d->enumerated_found, DEVICE_FOUND_UDEV)) {
307
308 /* The device has not been enumerated. On switching-root, such situation is natural. See the
309 * above comment. To prevent problematic state transition active → dead → active, let's
310 * drop the DEVICE_FOUND_UDEV flag and downgrade state to DEVICE_TENTATIVE(activating). See
311 * issue #12953 and #23208. */
312 found &= ~DEVICE_FOUND_UDEV;
313 if (state == DEVICE_PLUGGED)
314 state = DEVICE_TENTATIVE;
315
316 /* Also check the validity of the device syspath. Without this check, if the device was
317 * removed while switching root, it would never go to inactive state, as both Device.found
318 * and Device.enumerated_found do not have the DEVICE_FOUND_UDEV flag, so device_catchup() in
319 * device_update_found_one() does nothing in most cases. See issue #25106. Note that the
320 * syspath field is only serialized when systemd is sufficiently new and the device has been
321 * already processed by udevd. */
322 if (d->deserialized_sysfs) {
323 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
324
325 if (sd_device_new_from_syspath(&dev, d->deserialized_sysfs) < 0)
326 state = DEVICE_DEAD;
327 }
328 }
329
330 if (d->found == found && d->state == state)
331 return 0;
332
333 d->found = found;
334 device_set_state(d, state);
335 return 0;
336 }
337
338 static void device_catchup(Unit *u) {
339 Device *d = DEVICE(u);
340
341 assert(d);
342
343 /* Second, let's update the state with the enumerated state */
344 device_update_found_one(d, d->enumerated_found, DEVICE_FOUND_MASK);
345 }
346
347 static const struct {
348 DeviceFound flag;
349 const char *name;
350 } device_found_map[] = {
351 { DEVICE_FOUND_UDEV, "found-udev" },
352 { DEVICE_FOUND_MOUNT, "found-mount" },
353 { DEVICE_FOUND_SWAP, "found-swap" },
354 };
355
356 static int device_found_to_string_many(DeviceFound flags, char **ret) {
357 _cleanup_free_ char *s = NULL;
358
359 assert(ret);
360
361 for (size_t i = 0; i < ELEMENTSOF(device_found_map); i++) {
362 if (!FLAGS_SET(flags, device_found_map[i].flag))
363 continue;
364
365 if (!strextend_with_separator(&s, ",", device_found_map[i].name))
366 return -ENOMEM;
367 }
368
369 *ret = TAKE_PTR(s);
370
371 return 0;
372 }
373
374 static int device_found_from_string_many(const char *name, DeviceFound *ret) {
375 DeviceFound flags = 0;
376 int r;
377
378 assert(ret);
379
380 for (;;) {
381 _cleanup_free_ char *word = NULL;
382 DeviceFound f = 0;
383 unsigned i;
384
385 r = extract_first_word(&name, &word, ",", 0);
386 if (r < 0)
387 return r;
388 if (r == 0)
389 break;
390
391 for (i = 0; i < ELEMENTSOF(device_found_map); i++)
392 if (streq(word, device_found_map[i].name)) {
393 f = device_found_map[i].flag;
394 break;
395 }
396
397 if (f == 0)
398 return -EINVAL;
399
400 flags |= f;
401 }
402
403 *ret = flags;
404 return 0;
405 }
406
407 static int device_serialize(Unit *u, FILE *f, FDSet *fds) {
408 _cleanup_free_ char *s = NULL;
409 Device *d = DEVICE(u);
410
411 assert(d);
412 assert(u);
413 assert(f);
414 assert(fds);
415
416 if (d->sysfs)
417 (void) serialize_item(f, "sysfs", d->sysfs);
418
419 if (d->path)
420 (void) serialize_item(f, "path", d->path);
421
422 (void) serialize_item(f, "state", device_state_to_string(d->state));
423
424 if (device_found_to_string_many(d->found, &s) >= 0)
425 (void) serialize_item(f, "found", s);
426
427 return 0;
428 }
429
430 static int device_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
431 Device *d = DEVICE(u);
432 int r;
433
434 assert(d);
435 assert(u);
436 assert(key);
437 assert(value);
438 assert(fds);
439
440 if (streq(key, "sysfs")) {
441 if (!d->deserialized_sysfs) {
442 d->deserialized_sysfs = strdup(value);
443 if (!d->deserialized_sysfs)
444 log_oom_debug();
445 }
446
447 } else if (streq(key, "path")) {
448 if (!d->path) {
449 d->path = strdup(value);
450 if (!d->path)
451 log_oom_debug();
452 }
453
454 } else if (streq(key, "state")) {
455 DeviceState state;
456
457 state = device_state_from_string(value);
458 if (state < 0)
459 log_unit_debug(u, "Failed to parse state value, ignoring: %s", value);
460 else
461 d->deserialized_state = state;
462
463 } else if (streq(key, "found")) {
464 r = device_found_from_string_many(value, &d->deserialized_found);
465 if (r < 0)
466 log_unit_debug_errno(u, r, "Failed to parse found value '%s', ignoring: %m", value);
467
468 } else
469 log_unit_debug(u, "Unknown serialization key: %s", key);
470
471 return 0;
472 }
473
474 static void device_dump(Unit *u, FILE *f, const char *prefix) {
475 Device *d = DEVICE(u);
476 _cleanup_free_ char *s = NULL;
477
478 assert(d);
479
480 (void) device_found_to_string_many(d->found, &s);
481
482 fprintf(f,
483 "%sDevice State: %s\n"
484 "%sDevice Path: %s\n"
485 "%sSysfs Path: %s\n"
486 "%sFound: %s\n",
487 prefix, device_state_to_string(d->state),
488 prefix, strna(d->path),
489 prefix, strna(d->sysfs),
490 prefix, strna(s));
491
492 STRV_FOREACH(i, d->wants_property)
493 fprintf(f, "%sudev SYSTEMD_WANTS: %s\n",
494 prefix, *i);
495 }
496
497 static UnitActiveState device_active_state(Unit *u) {
498 assert(u);
499
500 return state_translation_table[DEVICE(u)->state];
501 }
502
503 static const char *device_sub_state_to_string(Unit *u) {
504 assert(u);
505
506 return device_state_to_string(DEVICE(u)->state);
507 }
508
509 static int device_update_description(Unit *u, sd_device *dev, const char *path) {
510 _cleanup_free_ char *j = NULL;
511 const char *model, *label, *desc;
512 int r;
513
514 assert(u);
515 assert(path);
516
517 desc = path;
518
519 if (dev && device_get_model_string(dev, &model) >= 0) {
520 desc = model;
521
522 /* Try to concatenate the device model string with a label, if there is one */
523 if (sd_device_get_property_value(dev, "ID_FS_LABEL", &label) >= 0 ||
524 sd_device_get_property_value(dev, "ID_PART_ENTRY_NAME", &label) >= 0 ||
525 sd_device_get_property_value(dev, "ID_PART_ENTRY_NUMBER", &label) >= 0) {
526
527 desc = j = strjoin(model, " ", label);
528 if (!j)
529 return log_oom();
530 }
531 }
532
533 r = unit_set_description(u, desc);
534 if (r < 0)
535 return log_unit_error_errno(u, r, "Failed to set device description: %m");
536
537 return 0;
538 }
539
540 static int device_add_udev_wants(Unit *u, sd_device *dev) {
541 _cleanup_strv_free_ char **added = NULL;
542 const char *wants, *property;
543 Device *d = DEVICE(u);
544 int r;
545
546 assert(d);
547 assert(dev);
548
549 property = MANAGER_IS_USER(u->manager) ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS";
550
551 r = sd_device_get_property_value(dev, property, &wants);
552 if (r < 0)
553 return 0;
554
555 for (;;) {
556 _cleanup_free_ char *word = NULL, *k = NULL;
557
558 r = extract_first_word(&wants, &word, NULL, EXTRACT_UNQUOTE);
559 if (r == 0)
560 break;
561 if (r == -ENOMEM)
562 return log_oom();
563 if (r < 0)
564 return log_unit_error_errno(u, r, "Failed to parse property %s with value %s: %m", property, wants);
565
566 if (unit_name_is_valid(word, UNIT_NAME_TEMPLATE) && d->sysfs) {
567 _cleanup_free_ char *escaped = NULL;
568
569 /* If the unit name is specified as template, then automatically fill in the sysfs path of the
570 * device as instance name, properly escaped. */
571
572 r = unit_name_path_escape(d->sysfs, &escaped);
573 if (r < 0)
574 return log_unit_error_errno(u, r, "Failed to escape %s: %m", d->sysfs);
575
576 r = unit_name_replace_instance(word, escaped, &k);
577 if (r < 0)
578 return log_unit_error_errno(u, r, "Failed to build %s instance of template %s: %m", escaped, word);
579 } else {
580 /* If this is not a template, then let's mangle it so, that it becomes a valid unit name. */
581
582 r = unit_name_mangle(word, UNIT_NAME_MANGLE_WARN, &k);
583 if (r < 0)
584 return log_unit_error_errno(u, r, "Failed to mangle unit name \"%s\": %m", word);
585 }
586
587 r = unit_add_dependency_by_name(u, UNIT_WANTS, k, true, UNIT_DEPENDENCY_UDEV);
588 if (r < 0)
589 return log_unit_error_errno(u, r, "Failed to add Wants= dependency: %m");
590
591 r = strv_consume(&added, TAKE_PTR(k));
592 if (r < 0)
593 return log_oom();
594 }
595
596 if (d->state != DEVICE_DEAD)
597 /* So here's a special hack, to compensate for the fact that the udev database's reload cycles are not
598 * synchronized with our own reload cycles: when we detect that the SYSTEMD_WANTS property of a device
599 * changes while the device unit is already up, let's skip to trigger units that were already listed
600 * and are active, and start units otherwise. This typically happens during the boot-time switch root
601 * transition, as udev devices will generally already be up in the initrd, but SYSTEMD_WANTS properties
602 * get then added through udev rules only available on the host system, and thus only when the initial
603 * udev coldplug trigger runs.
604 *
605 * We do this only if the device has been up already when we parse this, as otherwise the usual
606 * dependency logic that is run from the dead → plugged transition will trigger these deps. */
607 STRV_FOREACH(i, added) {
608 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
609
610 if (strv_contains(d->wants_property, *i)) {
611 Unit *v;
612
613 v = manager_get_unit(u->manager, *i);
614 if (v && UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(v)))
615 continue; /* The unit was already listed and is running. */
616 }
617
618 r = manager_add_job_by_name(u->manager, JOB_START, *i, JOB_FAIL, NULL, &error, NULL);
619 if (r < 0)
620 log_unit_full_errno(u, sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ? LOG_DEBUG : LOG_WARNING, r,
621 "Failed to enqueue %s job, ignoring: %s", property, bus_error_message(&error, r));
622 }
623
624 return strv_free_and_replace(d->wants_property, added);
625 }
626
627 static bool device_is_bound_by_mounts(Device *d, sd_device *dev) {
628 int r;
629
630 assert(d);
631 assert(dev);
632
633 r = device_get_property_bool(dev, "SYSTEMD_MOUNT_DEVICE_BOUND");
634 if (r < 0 && r != -ENOENT)
635 log_device_warning_errno(dev, r, "Failed to parse SYSTEMD_MOUNT_DEVICE_BOUND= udev property, ignoring: %m");
636
637 d->bind_mounts = r > 0;
638
639 return d->bind_mounts;
640 }
641
642 static void device_upgrade_mount_deps(Unit *u) {
643 Unit *other;
644 void *v;
645 int r;
646
647 /* Let's upgrade Requires= to BindsTo= on us. (Used when SYSTEMD_MOUNT_DEVICE_BOUND is set) */
648
649 HASHMAP_FOREACH_KEY(v, other, unit_get_dependencies(u, UNIT_REQUIRED_BY)) {
650 if (other->type != UNIT_MOUNT)
651 continue;
652
653 r = unit_add_dependency(other, UNIT_BINDS_TO, u, true, UNIT_DEPENDENCY_UDEV);
654 if (r < 0)
655 log_unit_warning_errno(u, r, "Failed to add BindsTo= dependency between device and mount unit, ignoring: %m");
656 }
657 }
658
659 static int device_setup_unit(Manager *m, sd_device *dev, const char *path, bool main, Set **units) {
660 _cleanup_(unit_freep) Unit *new_unit = NULL;
661 _cleanup_free_ char *e = NULL;
662 const char *sysfs = NULL;
663 Unit *u;
664 int r;
665
666 assert(m);
667 assert(path);
668
669 if (dev) {
670 r = sd_device_get_syspath(dev, &sysfs);
671 if (r < 0)
672 return log_device_debug_errno(dev, r, "Couldn't get syspath from device, ignoring: %m");
673 }
674
675 r = unit_name_from_path(path, ".device", &e);
676 if (r < 0)
677 return log_struct_errno(
678 LOG_WARNING, r,
679 "MESSAGE_ID=" SD_MESSAGE_DEVICE_PATH_NOT_SUITABLE_STR,
680 "DEVICE=%s", path,
681 LOG_MESSAGE("Failed to generate valid unit name from device path '%s', ignoring device: %m",
682 path));
683
684 u = manager_get_unit(m, e);
685 if (u) {
686 /* The device unit can still be present even if the device was unplugged: a mount unit can reference it
687 * hence preventing the GC to have garbaged it. That's desired since the device unit may have a
688 * dependency on the mount unit which was added during the loading of the later. When the device is
689 * plugged the sysfs might not be initialized yet, as we serialize the device's state but do not
690 * serialize the sysfs path across reloads/reexecs. Hence, when coming back from a reload/restart we
691 * might have the state valid, but not the sysfs path. Also, there is another possibility; when multiple
692 * devices have the same devlink (e.g. /dev/disk/by-uuid/xxxx), adding/updating/removing one of the
693 * device causes syspath change. Hence, let's always update sysfs path. */
694
695 /* Let's remove all dependencies generated due to udev properties. We'll re-add whatever is configured
696 * now below. */
697 unit_remove_dependencies(u, UNIT_DEPENDENCY_UDEV);
698
699 } else {
700 r = unit_new_for_name(m, sizeof(Device), e, &new_unit);
701 if (r < 0)
702 return log_device_error_errno(dev, r, "Failed to allocate device unit %s: %m", e);
703
704 u = new_unit;
705
706 unit_add_to_load_queue(u);
707 }
708
709 if (!DEVICE(u)->path) {
710 DEVICE(u)->path = strdup(path);
711 if (!DEVICE(u)->path)
712 return log_oom();
713 }
714
715 /* If this was created via some dependency and has not actually been seen yet ->sysfs will not be
716 * initialized. Hence initialize it if necessary. */
717 if (sysfs) {
718 r = device_set_sysfs(DEVICE(u), sysfs);
719 if (r < 0)
720 return log_unit_error_errno(u, r, "Failed to set sysfs path %s: %m", sysfs);
721
722 /* The additional systemd udev properties we only interpret for the main object */
723 if (main)
724 (void) device_add_udev_wants(u, dev);
725 }
726
727 (void) device_update_description(u, dev, path);
728
729 /* So the user wants the mount units to be bound to the device but a mount unit might has been seen
730 * by systemd before the device appears on its radar. In this case the device unit is partially
731 * initialized and includes the deps on the mount unit but at that time the "bind mounts" flag wasn't
732 * present. Fix this up now. */
733 if (dev && device_is_bound_by_mounts(DEVICE(u), dev))
734 device_upgrade_mount_deps(u);
735
736 if (units) {
737 r = set_ensure_put(units, NULL, DEVICE(u));
738 if (r < 0)
739 return log_unit_error_errno(u, r, "Failed to store unit: %m");
740 }
741
742 TAKE_PTR(new_unit);
743 return 0;
744 }
745
746 static bool device_is_ready(sd_device *dev) {
747 int r;
748
749 assert(dev);
750
751 if (device_for_action(dev, SD_DEVICE_REMOVE))
752 return false;
753
754 r = device_is_renaming(dev);
755 if (r < 0)
756 log_device_warning_errno(dev, r, "Failed to check if device is renaming, assuming device is not renaming: %m");
757 if (r > 0) {
758 log_device_debug(dev, "Device busy: device is renaming");
759 return false;
760 }
761
762 /* Is it really tagged as 'systemd' right now? */
763 r = sd_device_has_current_tag(dev, "systemd");
764 if (r < 0)
765 log_device_warning_errno(dev, r, "Failed to check if device has \"systemd\" tag, assuming device is not tagged with \"systemd\": %m");
766 if (r == 0)
767 log_device_debug(dev, "Device busy: device is not tagged with \"systemd\"");
768 if (r <= 0)
769 return false;
770
771 r = device_get_property_bool(dev, "SYSTEMD_READY");
772 if (r < 0 && r != -ENOENT)
773 log_device_warning_errno(dev, r, "Failed to get device SYSTEMD_READY property, assuming device does not have \"SYSTEMD_READY\" property: %m");
774 if (r == 0)
775 log_device_debug(dev, "Device busy: SYSTEMD_READY property from device is false");
776
777 return r != 0;
778 }
779
780 static int device_setup_devlink_unit_one(Manager *m, const char *devlink, Set **ready_units, Set **not_ready_units) {
781 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
782 Unit *u;
783
784 assert(m);
785 assert(devlink);
786 assert(ready_units);
787 assert(not_ready_units);
788
789 if (sd_device_new_from_devname(&dev, devlink) >= 0 && device_is_ready(dev))
790 return device_setup_unit(m, dev, devlink, /* main = */ false, ready_units);
791
792 /* the devlink is already removed or not ready */
793 if (device_by_path(m, devlink, &u) < 0)
794 return 0; /* The corresponding .device unit not found. That's fine. */
795
796 return set_ensure_put(not_ready_units, NULL, DEVICE(u));
797 }
798
799 static int device_setup_extra_units(Manager *m, sd_device *dev, Set **ready_units, Set **not_ready_units) {
800 _cleanup_strv_free_ char **aliases = NULL;
801 const char *syspath, *devname = NULL;
802 Device *l;
803 int r;
804
805 assert(m);
806 assert(dev);
807 assert(ready_units);
808 assert(not_ready_units);
809
810 r = sd_device_get_syspath(dev, &syspath);
811 if (r < 0)
812 return r;
813
814 (void) sd_device_get_devname(dev, &devname);
815
816 /* devlink units */
817 FOREACH_DEVICE_DEVLINK(dev, devlink) {
818 /* These are a kind of special devlink. They should be always unique, but neither persistent
819 * nor predictable. Hence, let's refuse them. See also the comments for alias units below. */
820 if (PATH_STARTSWITH_SET(devlink, "/dev/block/", "/dev/char/"))
821 continue;
822
823 (void) device_setup_devlink_unit_one(m, devlink, ready_units, not_ready_units);
824 }
825
826 if (device_is_ready(dev)) {
827 const char *s;
828
829 r = sd_device_get_property_value(dev, "SYSTEMD_ALIAS", &s);
830 if (r < 0 && r != -ENOENT)
831 log_device_warning_errno(dev, r, "Failed to get SYSTEMD_ALIAS property, ignoring: %m");
832 if (r >= 0) {
833 r = strv_split_full(&aliases, s, NULL, EXTRACT_UNQUOTE);
834 if (r < 0)
835 log_device_warning_errno(dev, r, "Failed to parse SYSTEMD_ALIAS property, ignoring: %m");
836 }
837 }
838
839 /* alias units */
840 STRV_FOREACH(alias, aliases) {
841 if (!path_is_absolute(*alias)) {
842 log_device_warning(dev, "The alias \"%s\" specified in SYSTEMD_ALIAS is not an absolute path, ignoring.", *alias);
843 continue;
844 }
845
846 if (!path_is_safe(*alias)) {
847 log_device_warning(dev, "The alias \"%s\" specified in SYSTEMD_ALIAS is not safe, ignoring.", *alias);
848 continue;
849 }
850
851 /* Note, even if the devlink is not persistent, LVM expects /dev/block/ symlink units exist.
852 * To achieve that, they set the path to SYSTEMD_ALIAS. Hence, we cannot refuse aliases start
853 * with /dev/, unfortunately. */
854
855 (void) device_setup_unit(m, dev, *alias, /* main = */ false, ready_units);
856 }
857
858 l = hashmap_get(m->devices_by_sysfs, syspath);
859 LIST_FOREACH(same_sysfs, d, l) {
860 if (!d->path)
861 continue;
862
863 if (path_equal(d->path, syspath))
864 continue; /* This is the main unit. */
865
866 if (devname && path_equal(d->path, devname))
867 continue; /* This is the real device node. */
868
869 if (device_has_devlink(dev, d->path))
870 continue; /* The devlink was already processed in the above loop. */
871
872 if (strv_contains(aliases, d->path))
873 continue; /* This is already processed in the above, and ready. */
874
875 if (path_startswith(d->path, "/dev/"))
876 /* This is a devlink unit. Check existence and update syspath. */
877 (void) device_setup_devlink_unit_one(m, d->path, ready_units, not_ready_units);
878 else
879 /* This is an alias unit of dropped or not ready device. */
880 (void) set_ensure_put(not_ready_units, NULL, d);
881 }
882
883 return 0;
884 }
885
886 static int device_setup_units(Manager *m, sd_device *dev, Set **ready_units, Set **not_ready_units) {
887 const char *syspath, *devname = NULL;
888 int r;
889
890 assert(m);
891 assert(dev);
892 assert(ready_units);
893 assert(not_ready_units);
894
895 r = sd_device_get_syspath(dev, &syspath);
896 if (r < 0)
897 return log_device_debug_errno(dev, r, "Couldn't get syspath from device, ignoring: %m");
898
899 /* First, process the main (that is, points to the syspath) and (real, not symlink) devnode units. */
900 if (device_for_action(dev, SD_DEVICE_REMOVE))
901 /* If the device is removed, the main and devnode units will be removed by
902 * device_update_found_by_sysfs() in device_dispatch_io(). Hence, it is not necessary to
903 * store them to not_ready_units, and we have nothing to do here.
904 *
905 * Note, still we need to process devlink units below, as a devlink previously points to this
906 * device may still exist and now point to another device node. That is, do not forget to
907 * call device_setup_extra_units(). */
908 ;
909 else if (device_is_ready(dev)) {
910 /* Add the main unit named after the syspath. If this one fails, don't bother with the rest,
911 * as this one shall be the main device unit the others just follow. (Compare with how
912 * device_following() is implemented, see below, which looks for the sysfs device.) */
913 r = device_setup_unit(m, dev, syspath, /* main = */ true, ready_units);
914 if (r < 0)
915 return r;
916
917 /* Add an additional unit for the device node */
918 if (sd_device_get_devname(dev, &devname) >= 0)
919 (void) device_setup_unit(m, dev, devname, /* main = */ false, ready_units);
920
921 } else {
922 Unit *u;
923
924 /* If the device exists but not ready, then save the units and unset udev bits later. */
925
926 if (device_by_path(m, syspath, &u) >= 0) {
927 r = set_ensure_put(not_ready_units, NULL, DEVICE(u));
928 if (r < 0)
929 log_unit_debug_errno(u, r, "Failed to store unit, ignoring: %m");
930 }
931
932 if (sd_device_get_devname(dev, &devname) >= 0 &&
933 device_by_path(m, devname, &u) >= 0) {
934 r = set_ensure_put(not_ready_units, NULL, DEVICE(u));
935 if (r < 0)
936 log_unit_debug_errno(u, r, "Failed to store unit, ignoring: %m");
937 }
938 }
939
940 /* Next, add/update additional .device units point to aliases and symlinks. */
941 (void) device_setup_extra_units(m, dev, ready_units, not_ready_units);
942
943 /* Safety check: no unit should be in ready_units and not_ready_units simultaneously. */
944 Unit *u;
945 SET_FOREACH(u, *not_ready_units)
946 if (set_remove(*ready_units, u))
947 log_unit_error(u, "Cannot activate and deactivate the unit simultaneously. Deactivating.");
948
949 return 0;
950 }
951
952 static Unit *device_following(Unit *u) {
953 Device *d = DEVICE(u);
954 Device *first = NULL;
955
956 assert(d);
957
958 if (startswith(u->id, "sys-"))
959 return NULL;
960
961 /* Make everybody follow the unit that's named after the sysfs path */
962 LIST_FOREACH(same_sysfs, other, d->same_sysfs_next)
963 if (startswith(UNIT(other)->id, "sys-"))
964 return UNIT(other);
965
966 LIST_FOREACH_BACKWARDS(same_sysfs, other, d->same_sysfs_prev) {
967 if (startswith(UNIT(other)->id, "sys-"))
968 return UNIT(other);
969
970 first = other;
971 }
972
973 return UNIT(first);
974 }
975
976 static int device_following_set(Unit *u, Set **_set) {
977 Device *d = DEVICE(u);
978 _cleanup_set_free_ Set *set = NULL;
979 int r;
980
981 assert(d);
982 assert(_set);
983
984 if (LIST_JUST_US(same_sysfs, d)) {
985 *_set = NULL;
986 return 0;
987 }
988
989 set = set_new(NULL);
990 if (!set)
991 return -ENOMEM;
992
993 LIST_FOREACH(same_sysfs, other, d->same_sysfs_next) {
994 r = set_put(set, other);
995 if (r < 0)
996 return r;
997 }
998
999 LIST_FOREACH_BACKWARDS(same_sysfs, other, d->same_sysfs_prev) {
1000 r = set_put(set, other);
1001 if (r < 0)
1002 return r;
1003 }
1004
1005 *_set = TAKE_PTR(set);
1006 return 1;
1007 }
1008
1009 static void device_shutdown(Manager *m) {
1010 assert(m);
1011
1012 m->device_monitor = sd_device_monitor_unref(m->device_monitor);
1013 m->devices_by_sysfs = hashmap_free(m->devices_by_sysfs);
1014 }
1015
1016 static void device_enumerate(Manager *m) {
1017 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
1018 int r;
1019
1020 assert(m);
1021
1022 if (!m->device_monitor) {
1023 r = sd_device_monitor_new(&m->device_monitor);
1024 if (r < 0) {
1025 log_error_errno(r, "Failed to allocate device monitor: %m");
1026 goto fail;
1027 }
1028
1029 r = sd_device_monitor_filter_add_match_tag(m->device_monitor, "systemd");
1030 if (r < 0) {
1031 log_error_errno(r, "Failed to add udev tag match: %m");
1032 goto fail;
1033 }
1034
1035 r = sd_device_monitor_attach_event(m->device_monitor, m->event);
1036 if (r < 0) {
1037 log_error_errno(r, "Failed to attach event to device monitor: %m");
1038 goto fail;
1039 }
1040
1041 r = sd_device_monitor_start(m->device_monitor, device_dispatch_io, m);
1042 if (r < 0) {
1043 log_error_errno(r, "Failed to start device monitor: %m");
1044 goto fail;
1045 }
1046 }
1047
1048 r = sd_device_enumerator_new(&e);
1049 if (r < 0) {
1050 log_error_errno(r, "Failed to allocate device enumerator: %m");
1051 goto fail;
1052 }
1053
1054 r = sd_device_enumerator_add_match_tag(e, "systemd");
1055 if (r < 0) {
1056 log_error_errno(r, "Failed to set tag for device enumeration: %m");
1057 goto fail;
1058 }
1059
1060 FOREACH_DEVICE(e, dev) {
1061 _cleanup_set_free_ Set *ready_units = NULL, *not_ready_units = NULL;
1062 Device *d;
1063
1064 if (device_setup_units(m, dev, &ready_units, &not_ready_units) < 0)
1065 continue;
1066
1067 SET_FOREACH(d, ready_units)
1068 device_update_found_one(d, DEVICE_FOUND_UDEV, DEVICE_FOUND_UDEV);
1069 SET_FOREACH(d, not_ready_units)
1070 device_update_found_one(d, DEVICE_NOT_FOUND, DEVICE_FOUND_UDEV);
1071 }
1072
1073 return;
1074
1075 fail:
1076 device_shutdown(m);
1077 }
1078
1079 static void device_propagate_reload(Manager *m, Device *d) {
1080 int r;
1081
1082 assert(m);
1083 assert(d);
1084
1085 if (d->state == DEVICE_DEAD)
1086 return;
1087
1088 r = manager_propagate_reload(m, UNIT(d), JOB_REPLACE, NULL);
1089 if (r < 0)
1090 log_unit_warning_errno(UNIT(d), r, "Failed to propagate reload, ignoring: %m");
1091 }
1092
1093 static void device_remove_old_on_move(Manager *m, sd_device *dev) {
1094 _cleanup_free_ char *syspath_old = NULL;
1095 const char *devpath_old;
1096 int r;
1097
1098 assert(m);
1099 assert(dev);
1100
1101 r = sd_device_get_property_value(dev, "DEVPATH_OLD", &devpath_old);
1102 if (r < 0)
1103 return (void) log_device_debug_errno(dev, r, "Failed to get DEVPATH_OLD= property on 'move' uevent, ignoring: %m");
1104
1105 syspath_old = path_join("/sys", devpath_old);
1106 if (!syspath_old)
1107 return (void) log_oom();
1108
1109 device_update_found_by_sysfs(m, syspath_old, DEVICE_NOT_FOUND, DEVICE_FOUND_MASK);
1110 }
1111
1112 static int device_dispatch_io(sd_device_monitor *monitor, sd_device *dev, void *userdata) {
1113 _cleanup_set_free_ Set *ready_units = NULL, *not_ready_units = NULL;
1114 Manager *m = ASSERT_PTR(userdata);
1115 sd_device_action_t action;
1116 const char *sysfs;
1117 bool ready;
1118 Device *d;
1119 int r;
1120
1121 assert(dev);
1122
1123 log_device_uevent(dev, "Processing udev action");
1124
1125 r = sd_device_get_syspath(dev, &sysfs);
1126 if (r < 0) {
1127 log_device_warning_errno(dev, r, "Failed to get device syspath, ignoring: %m");
1128 return 0;
1129 }
1130
1131 r = sd_device_get_action(dev, &action);
1132 if (r < 0) {
1133 log_device_warning_errno(dev, r, "Failed to get udev action, ignoring: %m");
1134 return 0;
1135 }
1136
1137 log_device_debug(dev, "Got '%s' action on syspath '%s'.", device_action_to_string(action), sysfs);
1138
1139 if (action == SD_DEVICE_MOVE)
1140 device_remove_old_on_move(m, dev);
1141
1142 /* When udevd failed to process the device, SYSTEMD_ALIAS or any other properties may contain invalid
1143 * values. Let's refuse to handle the uevent. */
1144 if (sd_device_get_property_value(dev, "UDEV_WORKER_FAILED", NULL) >= 0) {
1145 int v;
1146
1147 if (device_get_property_int(dev, "UDEV_WORKER_ERRNO", &v) >= 0)
1148 log_device_warning_errno(dev, v, "systemd-udevd failed to process the device, ignoring: %m");
1149 else if (device_get_property_int(dev, "UDEV_WORKER_EXIT_STATUS", &v) >= 0)
1150 log_device_warning(dev, "systemd-udevd failed to process the device with exit status %i, ignoring.", v);
1151 else if (device_get_property_int(dev, "UDEV_WORKER_SIGNAL", &v) >= 0) {
1152 const char *s;
1153 (void) sd_device_get_property_value(dev, "UDEV_WORKER_SIGNAL_NAME", &s);
1154 log_device_warning(dev, "systemd-udevd failed to process the device with signal %i(%s), ignoring.", v, strna(s));
1155 } else
1156 log_device_warning(dev, "systemd-udevd failed to process the device with unknown result, ignoring.");
1157
1158 return 0;
1159 }
1160
1161 /* A change event can signal that a device is becoming ready, in particular if the device is using
1162 * the SYSTEMD_READY logic in udev so we need to reach the else block of the following if, even for
1163 * change events */
1164 ready = device_is_ready(dev);
1165
1166 (void) device_setup_units(m, dev, &ready_units, &not_ready_units);
1167
1168 if (action == SD_DEVICE_REMOVE) {
1169 r = swap_process_device_remove(m, dev);
1170 if (r < 0)
1171 log_device_warning_errno(dev, r, "Failed to process swap device remove event, ignoring: %m");
1172 } else if (ready) {
1173 r = swap_process_device_new(m, dev);
1174 if (r < 0)
1175 log_device_warning_errno(dev, r, "Failed to process swap device new event, ignoring: %m");
1176 }
1177
1178 if (!IN_SET(action, SD_DEVICE_ADD, SD_DEVICE_REMOVE, SD_DEVICE_MOVE))
1179 SET_FOREACH(d, ready_units)
1180 device_propagate_reload(m, d);
1181
1182 if (!set_isempty(ready_units))
1183 manager_dispatch_load_queue(m);
1184
1185 if (action == SD_DEVICE_REMOVE)
1186 /* If we get notified that a device was removed by udev, then it's completely gone, hence
1187 * unset all found bits. Note this affects all .device units still point to the removed
1188 * device. */
1189 device_update_found_by_sysfs(m, sysfs, DEVICE_NOT_FOUND, DEVICE_FOUND_MASK);
1190
1191 /* These devices are found and ready now, set the udev found bit. Note, this is also necessary to do
1192 * on remove uevent, as some devlinks may be updated and now point to other device nodes. */
1193 SET_FOREACH(d, ready_units)
1194 device_update_found_one(d, DEVICE_FOUND_UDEV, DEVICE_FOUND_UDEV);
1195
1196 /* These devices may be nominally around, but not ready for us. Hence unset the udev bit, but leave
1197 * the rest around. This may be redundant for remove uevent, but should be harmless. */
1198 SET_FOREACH(d, not_ready_units)
1199 device_update_found_one(d, DEVICE_NOT_FOUND, DEVICE_FOUND_UDEV);
1200
1201 return 0;
1202 }
1203
1204 void device_found_node(Manager *m, const char *node, DeviceFound found, DeviceFound mask) {
1205 int r;
1206
1207 assert(m);
1208 assert(node);
1209 assert(!FLAGS_SET(mask, DEVICE_FOUND_UDEV));
1210
1211 if (!udev_available())
1212 return;
1213
1214 if (mask == 0)
1215 return;
1216
1217 /* This is called whenever we find a device referenced in /proc/swaps or /proc/self/mounts. Such a device might
1218 * be mounted/enabled at a time where udev has not finished probing it yet, and we thus haven't learned about
1219 * it yet. In this case we will set the device unit to "tentative" state.
1220 *
1221 * This takes a pair of DeviceFound flags parameters. The 'mask' parameter is a bit mask that indicates which
1222 * bits of 'found' to copy into the per-device DeviceFound flags field. Thus, this function may be used to set
1223 * and unset individual bits in a single call, while merging partially with previous state. */
1224
1225 if ((found & mask) != 0) {
1226 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
1227
1228 /* If the device is known in the kernel and newly appeared, then we'll create a device unit for it,
1229 * under the name referenced in /proc/swaps or /proc/self/mountinfo. But first, let's validate if
1230 * everything is alright with the device node. Note that we're fine with missing device nodes,
1231 * but not with badly set up ones. */
1232
1233 r = sd_device_new_from_devname(&dev, node);
1234 if (r == -ENODEV)
1235 log_debug("Could not find device for %s, continuing without device node", node);
1236 else if (r < 0) {
1237 /* Reduce log noise from nodes which are not device nodes by skipping EINVAL. */
1238 if (r != -EINVAL)
1239 log_error_errno(r, "Failed to open %s device, ignoring: %m", node);
1240 return;
1241 }
1242
1243 (void) device_setup_unit(m, dev, node, /* main = */ false, NULL); /* 'dev' may be NULL. */
1244 }
1245
1246 /* Update the device unit's state, should it exist */
1247 (void) device_update_found_by_name(m, node, found, mask);
1248 }
1249
1250 bool device_shall_be_bound_by(Unit *device, Unit *u) {
1251 assert(device);
1252 assert(u);
1253
1254 if (u->type != UNIT_MOUNT)
1255 return false;
1256
1257 return DEVICE(device)->bind_mounts;
1258 }
1259
1260 const UnitVTable device_vtable = {
1261 .object_size = sizeof(Device),
1262 .sections =
1263 "Unit\0"
1264 "Device\0"
1265 "Install\0",
1266
1267 .gc_jobs = true,
1268
1269 .init = device_init,
1270 .done = device_done,
1271 .load = device_load,
1272
1273 .coldplug = device_coldplug,
1274 .catchup = device_catchup,
1275
1276 .serialize = device_serialize,
1277 .deserialize_item = device_deserialize_item,
1278
1279 .dump = device_dump,
1280
1281 .active_state = device_active_state,
1282 .sub_state_to_string = device_sub_state_to_string,
1283
1284 .following = device_following,
1285 .following_set = device_following_set,
1286
1287 .enumerate = device_enumerate,
1288 .shutdown = device_shutdown,
1289 .supported = udev_available,
1290
1291 .status_message_formats = {
1292 .starting_stopping = {
1293 [0] = "Expecting device %s...",
1294 [1] = "Waiting for device %s to disappear...",
1295 },
1296 .finished_start_job = {
1297 [JOB_DONE] = "Found device %s.",
1298 [JOB_TIMEOUT] = "Timed out waiting for device %s.",
1299 },
1300 },
1301 };