1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
8 #include "dbus-device.h"
9 #include "device-private.h"
10 #include "device-util.h"
13 #include "parse-util.h"
14 #include "path-util.h"
15 #include "serialize.h"
16 #include "stat-util.h"
17 #include "string-util.h"
19 #include "unit-name.h"
22 static const UnitActiveState state_translation_table
[_DEVICE_STATE_MAX
] = {
23 [DEVICE_DEAD
] = UNIT_INACTIVE
,
24 [DEVICE_TENTATIVE
] = UNIT_ACTIVATING
,
25 [DEVICE_PLUGGED
] = UNIT_ACTIVE
,
28 static int device_dispatch_io(sd_device_monitor
*monitor
, sd_device
*dev
, void *userdata
);
29 static void device_update_found_one(Device
*d
, DeviceFound found
, DeviceFound mask
);
31 static void device_unset_sysfs(Device
*d
) {
40 /* Remove this unit from the chain of devices which share the
42 devices
= UNIT(d
)->manager
->devices_by_sysfs
;
43 first
= hashmap_get(devices
, d
->sysfs
);
44 LIST_REMOVE(same_sysfs
, first
, d
);
47 hashmap_remove_and_replace(devices
, d
->sysfs
, first
->sysfs
, first
);
49 hashmap_remove(devices
, d
->sysfs
);
51 d
->sysfs
= mfree(d
->sysfs
);
54 static int device_set_sysfs(Device
*d
, const char *sysfs
) {
55 _cleanup_free_
char *copy
= NULL
;
61 if (streq_ptr(d
->sysfs
, sysfs
))
64 r
= hashmap_ensure_allocated(&UNIT(d
)->manager
->devices_by_sysfs
, &path_hash_ops
);
72 device_unset_sysfs(d
);
74 first
= hashmap_get(UNIT(d
)->manager
->devices_by_sysfs
, sysfs
);
75 LIST_PREPEND(same_sysfs
, first
, d
);
77 r
= hashmap_replace(UNIT(d
)->manager
->devices_by_sysfs
, copy
, first
);
79 LIST_REMOVE(same_sysfs
, first
, d
);
83 d
->sysfs
= TAKE_PTR(copy
);
87 static void device_init(Unit
*u
) {
88 Device
*d
= DEVICE(u
);
91 assert(UNIT(d
)->load_state
== UNIT_STUB
);
93 /* In contrast to all other unit types we timeout jobs waiting
94 * for devices by default. This is because they otherwise wait
95 * indefinitely for plugged in devices, something which cannot
96 * happen for the other units since their operations time out
98 u
->job_running_timeout
= u
->manager
->default_timeout_start_usec
;
100 u
->ignore_on_isolate
= true;
102 d
->deserialized_state
= _DEVICE_STATE_INVALID
;
105 static void device_done(Unit
*u
) {
106 Device
*d
= DEVICE(u
);
110 device_unset_sysfs(d
);
111 d
->wants_property
= strv_free(d
->wants_property
);
114 static void device_set_state(Device
*d
, DeviceState state
) {
115 DeviceState old_state
;
118 old_state
= d
->state
;
121 if (state
== DEVICE_DEAD
)
122 device_unset_sysfs(d
);
124 if (state
!= old_state
)
125 log_unit_debug(UNIT(d
), "Changed %s -> %s", device_state_to_string(old_state
), device_state_to_string(state
));
127 unit_notify(UNIT(d
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
130 static int device_coldplug(Unit
*u
) {
131 Device
*d
= DEVICE(u
);
134 assert(d
->state
== DEVICE_DEAD
);
136 /* First, let's put the deserialized state and found mask into effect, if we have it. */
138 if (d
->deserialized_state
< 0 ||
139 (d
->deserialized_state
== d
->state
&&
140 d
->deserialized_found
== d
->found
))
143 d
->found
= d
->deserialized_found
;
144 device_set_state(d
, d
->deserialized_state
);
148 static void device_catchup(Unit
*u
) {
149 Device
*d
= DEVICE(u
);
153 /* Second, let's update the state with the enumerated state if it's different */
154 if (d
->enumerated_found
== d
->found
)
157 device_update_found_one(d
, d
->enumerated_found
, DEVICE_FOUND_MASK
);
160 static const struct {
163 } device_found_map
[] = {
164 { DEVICE_FOUND_UDEV
, "found-udev" },
165 { DEVICE_FOUND_MOUNT
, "found-mount" },
166 { DEVICE_FOUND_SWAP
, "found-swap" },
169 static int device_found_to_string_many(DeviceFound flags
, char **ret
) {
170 _cleanup_free_
char *s
= NULL
;
175 for (i
= 0; i
< ELEMENTSOF(device_found_map
); i
++) {
176 if (!FLAGS_SET(flags
, device_found_map
[i
].flag
))
179 if (!strextend_with_separator(&s
, ",", device_found_map
[i
].name
, NULL
))
188 static int device_found_from_string_many(const char *name
, DeviceFound
*ret
) {
189 DeviceFound flags
= 0;
195 _cleanup_free_
char *word
= NULL
;
199 r
= extract_first_word(&name
, &word
, ",", 0);
205 for (i
= 0; i
< ELEMENTSOF(device_found_map
); i
++)
206 if (streq(word
, device_found_map
[i
].name
)) {
207 f
= device_found_map
[i
].flag
;
221 static int device_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
222 _cleanup_free_
char *s
= NULL
;
223 Device
*d
= DEVICE(u
);
229 (void) serialize_item(f
, "state", device_state_to_string(d
->state
));
231 if (device_found_to_string_many(d
->found
, &s
) >= 0)
232 (void) serialize_item(f
, "found", s
);
237 static int device_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
238 Device
*d
= DEVICE(u
);
246 if (streq(key
, "state")) {
249 state
= device_state_from_string(value
);
251 log_unit_debug(u
, "Failed to parse state value, ignoring: %s", value
);
253 d
->deserialized_state
= state
;
255 } else if (streq(key
, "found")) {
256 r
= device_found_from_string_many(value
, &d
->deserialized_found
);
258 log_unit_debug_errno(u
, r
, "Failed to parse found value '%s', ignoring: %m", value
);
261 log_unit_debug(u
, "Unknown serialization key: %s", key
);
266 static void device_dump(Unit
*u
, FILE *f
, const char *prefix
) {
267 Device
*d
= DEVICE(u
);
268 _cleanup_free_
char *s
= NULL
;
272 (void) device_found_to_string_many(d
->found
, &s
);
275 "%sDevice State: %s\n"
278 prefix
, device_state_to_string(d
->state
),
279 prefix
, strna(d
->sysfs
),
282 if (!strv_isempty(d
->wants_property
)) {
285 STRV_FOREACH(i
, d
->wants_property
)
286 fprintf(f
, "%sudev SYSTEMD_WANTS: %s\n",
291 _pure_
static UnitActiveState
device_active_state(Unit
*u
) {
294 return state_translation_table
[DEVICE(u
)->state
];
297 _pure_
static const char *device_sub_state_to_string(Unit
*u
) {
300 return device_state_to_string(DEVICE(u
)->state
);
303 static int device_update_description(Unit
*u
, sd_device
*dev
, const char *path
) {
304 const char *model
, *label
;
311 if (sd_device_get_property_value(dev
, "ID_MODEL_FROM_DATABASE", &model
) >= 0 ||
312 sd_device_get_property_value(dev
, "ID_MODEL", &model
) >= 0) {
314 /* Try to concatenate the device model string with a label, if there is one */
315 if (sd_device_get_property_value(dev
, "ID_FS_LABEL", &label
) >= 0 ||
316 sd_device_get_property_value(dev
, "ID_PART_ENTRY_NAME", &label
) >= 0 ||
317 sd_device_get_property_value(dev
, "ID_PART_ENTRY_NUMBER", &label
) >= 0) {
319 _cleanup_free_
char *j
;
321 j
= strjoin(model
, " ", label
);
325 r
= unit_set_description(u
, j
);
327 r
= unit_set_description(u
, model
);
329 r
= unit_set_description(u
, path
);
331 return log_unit_error_errno(u
, r
, "Failed to set device description: %m");
336 static int device_add_udev_wants(Unit
*u
, sd_device
*dev
) {
337 _cleanup_strv_free_
char **added
= NULL
;
338 const char *wants
, *property
;
339 Device
*d
= DEVICE(u
);
345 property
= MANAGER_IS_USER(u
->manager
) ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS";
347 r
= sd_device_get_property_value(dev
, property
, &wants
);
352 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
354 r
= extract_first_word(&wants
, &word
, NULL
, EXTRACT_QUOTES
);
360 return log_unit_error_errno(u
, r
, "Failed to parse property %s with value %s: %m", property
, wants
);
362 if (unit_name_is_valid(word
, UNIT_NAME_TEMPLATE
) && d
->sysfs
) {
363 _cleanup_free_
char *escaped
= NULL
;
365 /* If the unit name is specified as template, then automatically fill in the sysfs path of the
366 * device as instance name, properly escaped. */
368 r
= unit_name_path_escape(d
->sysfs
, &escaped
);
370 return log_unit_error_errno(u
, r
, "Failed to escape %s: %m", d
->sysfs
);
372 r
= unit_name_replace_instance(word
, escaped
, &k
);
374 return log_unit_error_errno(u
, r
, "Failed to build %s instance of template %s: %m", escaped
, word
);
376 /* If this is not a template, then let's mangle it so, that it becomes a valid unit name. */
378 r
= unit_name_mangle(word
, UNIT_NAME_MANGLE_WARN
, &k
);
380 return log_unit_error_errno(u
, r
, "Failed to mangle unit name \"%s\": %m", word
);
383 r
= unit_add_dependency_by_name(u
, UNIT_WANTS
, k
, true, UNIT_DEPENDENCY_UDEV
);
385 return log_unit_error_errno(u
, r
, "Failed to add Wants= dependency: %m");
387 r
= strv_push(&added
, k
);
394 if (d
->state
!= DEVICE_DEAD
) {
397 /* So here's a special hack, to compensate for the fact that the udev database's reload cycles are not
398 * synchronized with our own reload cycles: when we detect that the SYSTEMD_WANTS property of a device
399 * changes while the device unit is already up, let's manually trigger any new units listed in it not
400 * seen before. This typically appens during the boot-time switch root transition, as udev devices
401 * will generally already be up in the initrd, but SYSTEMD_WANTS properties get then added through udev
402 * rules only available on the host system, and thus only when the initial udev coldplug trigger runs.
404 * We do this only if the device has been up already when we parse this, as otherwise the usual
405 * dependency logic that is run from the dead → plugged transition will trigger these deps. */
407 STRV_FOREACH(i
, added
) {
408 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
410 if (strv_contains(d
->wants_property
, *i
)) /* Was this unit already listed before? */
413 r
= manager_add_job_by_name(u
->manager
, JOB_START
, *i
, JOB_FAIL
, &error
, NULL
);
415 log_unit_warning_errno(u
, r
, "Failed to enqueue SYSTEMD_WANTS= job, ignoring: %s", bus_error_message(&error
, r
));
419 strv_free(d
->wants_property
);
420 d
->wants_property
= TAKE_PTR(added
);
425 static bool device_is_bound_by_mounts(Device
*d
, sd_device
*dev
) {
426 const char *bound_by
;
432 if (sd_device_get_property_value(dev
, "SYSTEMD_MOUNT_DEVICE_BOUND", &bound_by
) >= 0) {
433 r
= parse_boolean(bound_by
);
435 log_device_warning_errno(dev
, r
, "Failed to parse SYSTEMD_MOUNT_DEVICE_BOUND='%s' udev property, ignoring: %m", bound_by
);
437 d
->bind_mounts
= r
> 0;
439 d
->bind_mounts
= false;
441 return d
->bind_mounts
;
444 static void device_upgrade_mount_deps(Unit
*u
) {
450 /* Let's upgrade Requires= to BindsTo= on us. (Used when SYSTEMD_MOUNT_DEVICE_BOUND is set) */
452 HASHMAP_FOREACH_KEY(v
, other
, u
->dependencies
[UNIT_REQUIRED_BY
], i
) {
453 if (other
->type
!= UNIT_MOUNT
)
456 r
= unit_add_dependency(other
, UNIT_BINDS_TO
, u
, true, UNIT_DEPENDENCY_UDEV
);
458 log_unit_warning_errno(u
, r
, "Failed to add BindsTo= dependency between device and mount unit, ignoring: %m");
462 static int device_setup_unit(Manager
*m
, sd_device
*dev
, const char *path
, bool main
) {
463 _cleanup_free_
char *e
= NULL
;
464 const char *sysfs
= NULL
;
473 r
= sd_device_get_syspath(dev
, &sysfs
);
475 log_device_debug_errno(dev
, r
, "Couldn't get syspath from device, ignoring: %m");
480 r
= unit_name_from_path(path
, ".device", &e
);
482 return log_device_error_errno(dev
, r
, "Failed to generate unit name from device path: %m");
484 u
= manager_get_unit(m
, e
);
486 /* The device unit can still be present even if the device was unplugged: a mount unit can reference it
487 * hence preventing the GC to have garbaged it. That's desired since the device unit may have a
488 * dependency on the mount unit which was added during the loading of the later. When the device is
489 * plugged the sysfs might not be initialized yet, as we serialize the device's state but do not
490 * serialize the sysfs path across reloads/reexecs. Hence, when coming back from a reload/restart we
491 * might have the state valid, but not the sysfs path. Hence, let's filter out conflicting devices, but
492 * let's accept devices in any state with no sysfs path set. */
494 if (DEVICE(u
)->state
== DEVICE_PLUGGED
&&
497 !path_equal(DEVICE(u
)->sysfs
, sysfs
)) {
498 log_unit_debug(u
, "Device %s appeared twice with different sysfs paths %s and %s, ignoring the latter.",
499 e
, DEVICE(u
)->sysfs
, sysfs
);
505 /* Let's remove all dependencies generated due to udev properties. We'll readd whatever is configured
507 unit_remove_dependencies(u
, UNIT_DEPENDENCY_UDEV
);
511 r
= unit_new_for_name(m
, sizeof(Device
), e
, &u
);
513 log_device_error_errno(dev
, r
, "Failed to allocate device unit %s: %m", e
);
517 unit_add_to_load_queue(u
);
520 /* If this was created via some dependency and has not actually been seen yet ->sysfs will not be
521 * initialized. Hence initialize it if necessary. */
523 r
= device_set_sysfs(DEVICE(u
), sysfs
);
525 log_unit_error_errno(u
, r
, "Failed to set sysfs path %s: %m", sysfs
);
529 (void) device_update_description(u
, dev
, path
);
531 /* The additional systemd udev properties we only interpret for the main object */
533 (void) device_add_udev_wants(u
, dev
);
536 /* So the user wants the mount units to be bound to the device but a mount unit might has been seen by systemd
537 * before the device appears on its radar. In this case the device unit is partially initialized and includes
538 * the deps on the mount unit but at that time the "bind mounts" flag wasn't not present. Fix this up now. */
539 if (dev
&& device_is_bound_by_mounts(DEVICE(u
), dev
))
540 device_upgrade_mount_deps(u
);
542 /* Note that this won't dispatch the load queue, the caller has to do that if needed and appropriate */
543 unit_add_to_dbus_queue(u
);
554 static int device_process_new(Manager
*m
, sd_device
*dev
) {
555 const char *sysfs
, *dn
, *alias
;
561 if (sd_device_get_syspath(dev
, &sysfs
) < 0)
564 /* Add the main unit named after the sysfs path */
565 r
= device_setup_unit(m
, dev
, sysfs
, true);
569 /* Add an additional unit for the device node */
570 if (sd_device_get_devname(dev
, &dn
) >= 0)
571 (void) device_setup_unit(m
, dev
, dn
, false);
573 /* Add additional units for all symlinks */
574 if (sd_device_get_devnum(dev
, &devnum
) >= 0) {
577 FOREACH_DEVICE_DEVLINK(dev
, p
) {
580 if (PATH_STARTSWITH_SET(p
, "/dev/block/", "/dev/char/"))
583 /* Verify that the symlink in the FS actually belongs
584 * to this device. This is useful to deal with
585 * conflicting devices, e.g. when two disks want the
586 * same /dev/disk/by-label/xxx link because they have
587 * the same label. We want to make sure that the same
588 * device that won the symlink wins in systemd, so we
589 * check the device node major/minor */
590 if (stat(p
, &st
) >= 0 &&
591 ((!S_ISBLK(st
.st_mode
) && !S_ISCHR(st
.st_mode
)) ||
592 st
.st_rdev
!= devnum
))
595 (void) device_setup_unit(m
, dev
, p
, false);
599 /* Add additional units for all explicitly configured aliases */
600 if (sd_device_get_property_value(dev
, "SYSTEMD_ALIAS", &alias
) < 0)
604 _cleanup_free_
char *word
= NULL
;
606 r
= extract_first_word(&alias
, &word
, NULL
, EXTRACT_QUOTES
);
612 return log_device_warning_errno(dev
, r
, "Failed to add parse SYSTEMD_ALIAS property: %m");
614 if (!path_is_absolute(word
))
615 log_device_warning(dev
, "SYSTEMD_ALIAS is not an absolute path, ignoring: %s", word
);
616 else if (!path_is_normalized(word
))
617 log_device_warning(dev
, "SYSTEMD_ALIAS is not a normalized path, ignoring: %s", word
);
619 (void) device_setup_unit(m
, dev
, word
, false);
625 static void device_found_changed(Device
*d
, DeviceFound previous
, DeviceFound now
) {
628 /* Didn't exist before, but does now? if so, generate a new invocation ID for it */
629 if (previous
== DEVICE_NOT_FOUND
&& now
!= DEVICE_NOT_FOUND
)
630 (void) unit_acquire_invocation_id(UNIT(d
));
632 if (FLAGS_SET(now
, DEVICE_FOUND_UDEV
))
633 /* When the device is known to udev we consider it plugged. */
634 device_set_state(d
, DEVICE_PLUGGED
);
635 else if (now
!= DEVICE_NOT_FOUND
&& !FLAGS_SET(previous
, DEVICE_FOUND_UDEV
))
636 /* If the device has not been seen by udev yet, but is now referenced by the kernel, then we assume the
637 * kernel knows it now, and udev might soon too. */
638 device_set_state(d
, DEVICE_TENTATIVE
);
640 /* If nobody sees the device, or if the device was previously seen by udev and now is only referenced
641 * from the kernel, then we consider the device is gone, the kernel just hasn't noticed it yet. */
642 device_set_state(d
, DEVICE_DEAD
);
645 static void device_update_found_one(Device
*d
, DeviceFound found
, DeviceFound mask
) {
648 if (MANAGER_IS_RUNNING(UNIT(d
)->manager
)) {
649 DeviceFound n
, previous
;
651 /* When we are already running, then apply the new mask right-away, and trigger state changes
654 n
= (d
->found
& ~mask
) | (found
& mask
);
661 device_found_changed(d
, previous
, n
);
663 /* We aren't running yet, let's apply the new mask to the shadow variable instead, which we'll apply as
664 * soon as we catch-up with the state. */
665 d
->enumerated_found
= (d
->enumerated_found
& ~mask
) | (found
& mask
);
668 static void device_update_found_by_sysfs(Manager
*m
, const char *sysfs
, DeviceFound found
, DeviceFound mask
) {
677 l
= hashmap_get(m
->devices_by_sysfs
, sysfs
);
678 LIST_FOREACH_SAFE(same_sysfs
, d
, n
, l
)
679 device_update_found_one(d
, found
, mask
);
682 static int device_update_found_by_name(Manager
*m
, const char *path
, DeviceFound found
, DeviceFound mask
) {
683 _cleanup_free_
char *e
= NULL
;
693 r
= unit_name_from_path(path
, ".device", &e
);
695 return log_error_errno(r
, "Failed to generate unit name from device path: %m");
697 u
= manager_get_unit(m
, e
);
701 device_update_found_one(DEVICE(u
), found
, mask
);
705 static bool device_is_ready(sd_device
*dev
) {
710 if (sd_device_get_property_value(dev
, "SYSTEMD_READY", &ready
) < 0)
713 return parse_boolean(ready
) != 0;
716 static Unit
*device_following(Unit
*u
) {
717 Device
*d
= DEVICE(u
);
718 Device
*other
, *first
= NULL
;
722 if (startswith(u
->id
, "sys-"))
725 /* Make everybody follow the unit that's named after the sysfs path */
726 for (other
= d
->same_sysfs_next
; other
; other
= other
->same_sysfs_next
)
727 if (startswith(UNIT(other
)->id
, "sys-"))
730 for (other
= d
->same_sysfs_prev
; other
; other
= other
->same_sysfs_prev
) {
731 if (startswith(UNIT(other
)->id
, "sys-"))
740 static int device_following_set(Unit
*u
, Set
**_set
) {
741 Device
*d
= DEVICE(u
), *other
;
742 _cleanup_set_free_ Set
*set
= NULL
;
748 if (LIST_JUST_US(same_sysfs
, d
)) {
757 LIST_FOREACH_AFTER(same_sysfs
, other
, d
) {
758 r
= set_put(set
, other
);
763 LIST_FOREACH_BEFORE(same_sysfs
, other
, d
) {
764 r
= set_put(set
, other
);
769 *_set
= TAKE_PTR(set
);
773 static void device_shutdown(Manager
*m
) {
776 m
->device_monitor
= sd_device_monitor_unref(m
->device_monitor
);
777 m
->devices_by_sysfs
= hashmap_free(m
->devices_by_sysfs
);
780 static void device_enumerate(Manager
*m
) {
781 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
787 if (!m
->device_monitor
) {
788 r
= sd_device_monitor_new(&m
->device_monitor
);
790 log_error_errno(r
, "Failed to allocate device monitor: %m");
794 /* This will fail if we are unprivileged, but that
795 * should not matter much, as user instances won't run
797 (void) sd_device_monitor_set_receive_buffer_size(m
->device_monitor
, 128*1024*1024);
799 r
= sd_device_monitor_filter_add_match_tag(m
->device_monitor
, "systemd");
801 log_error_errno(r
, "Failed to add udev tag match: %m");
805 r
= sd_device_monitor_attach_event(m
->device_monitor
, m
->event
, 0);
807 log_error_errno(r
, "Failed to attach event to device monitor: %m");
811 r
= sd_device_monitor_start(m
->device_monitor
, device_dispatch_io
, m
, "systemd-device-monitor");
813 log_error_errno(r
, "Failed to start device monitor: %m");
818 r
= sd_device_enumerator_new(&e
);
820 log_error_errno(r
, "Failed to alloacte device enumerator: %m");
824 r
= sd_device_enumerator_add_match_tag(e
, "systemd");
826 log_error_errno(r
, "Failed to set tag for device enumeration: %m");
830 FOREACH_DEVICE(e
, dev
) {
833 if (!device_is_ready(dev
))
836 (void) device_process_new(m
, dev
);
838 if (sd_device_get_syspath(dev
, &sysfs
) < 0)
841 device_update_found_by_sysfs(m
, sysfs
, DEVICE_FOUND_UDEV
, DEVICE_FOUND_UDEV
);
850 static void device_propagate_reload_by_sysfs(Manager
*m
, const char *sysfs
) {
857 l
= hashmap_get(m
->devices_by_sysfs
, sysfs
);
858 LIST_FOREACH_SAFE(same_sysfs
, d
, n
, l
) {
859 if (d
->state
== DEVICE_DEAD
)
862 r
= manager_propagate_reload(m
, UNIT(d
), JOB_REPLACE
, NULL
);
864 log_warning_errno(r
, "Failed to propagate reload, ignoring: %m");
868 static int device_dispatch_io(sd_device_monitor
*monitor
, sd_device
*dev
, void *userdata
) {
869 Manager
*m
= userdata
;
870 const char *action
, *sysfs
;
876 r
= sd_device_get_syspath(dev
, &sysfs
);
878 log_device_error_errno(dev
, r
, "Failed to get device sys path: %m");
882 r
= sd_device_get_property_value(dev
, "ACTION", &action
);
884 log_device_error_errno(dev
, r
, "Failed to get udev action string: %m");
888 if (streq(action
, "change"))
889 device_propagate_reload_by_sysfs(m
, sysfs
);
891 /* A change event can signal that a device is becoming ready, in particular if
892 * the device is using the SYSTEMD_READY logic in udev
893 * so we need to reach the else block of the follwing if, even for change events */
894 if (streq(action
, "remove")) {
895 r
= swap_process_device_remove(m
, dev
);
897 log_device_warning_errno(dev
, r
, "Failed to process swap device remove event, ignoring: %m");
899 /* If we get notified that a device was removed by
900 * udev, then it's completely gone, hence unset all
902 device_update_found_by_sysfs(m
, sysfs
, 0, DEVICE_FOUND_UDEV
|DEVICE_FOUND_MOUNT
|DEVICE_FOUND_SWAP
);
904 } else if (device_is_ready(dev
)) {
906 (void) device_process_new(m
, dev
);
908 r
= swap_process_device_new(m
, dev
);
910 log_device_warning_errno(dev
, r
, "Failed to process swap device new event, ignoring: %m");
912 manager_dispatch_load_queue(m
);
914 /* The device is found now, set the udev found bit */
915 device_update_found_by_sysfs(m
, sysfs
, DEVICE_FOUND_UDEV
, DEVICE_FOUND_UDEV
);
918 /* The device is nominally around, but not ready for
919 * us. Hence unset the udev bit, but leave the rest
922 device_update_found_by_sysfs(m
, sysfs
, 0, DEVICE_FOUND_UDEV
);
928 static bool device_supported(void) {
929 static int read_only
= -1;
931 /* If /sys is read-only we don't support device units, and any
932 * attempts to start one should fail immediately. */
935 read_only
= path_is_read_only_fs("/sys");
937 return read_only
<= 0;
940 static int validate_node(Manager
*m
, const char *node
, sd_device
**ret
) {
948 /* Validates a device node that showed up in /proc/swaps or /proc/self/mountinfo if it makes sense for us to
949 * track. Note that this validator is fine within missing device nodes, but not with badly set up ones! */
951 if (!path_startswith(node
, "/dev")) {
956 if (stat(node
, &st
) < 0) {
958 return log_error_errno(errno
, "Failed to stat() device node file %s: %m", node
);
961 return 1; /* good! (though missing) */
964 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
966 r
= device_new_from_stat_rdev(&dev
, &st
);
969 return 1; /* good! (though missing) */
970 } else if (r
== -ENOTTY
) {
972 return 0; /* bad! (not a device node but some other kind of file system node) */
974 return log_error_errno(r
, "Failed to get udev device from devnum %u:%u: %m", major(st
.st_rdev
), minor(st
.st_rdev
));
976 *ret
= TAKE_PTR(dev
);
977 return 1; /* good! */
981 void device_found_node(Manager
*m
, const char *node
, DeviceFound found
, DeviceFound mask
) {
987 if (!device_supported())
993 /* This is called whenever we find a device referenced in /proc/swaps or /proc/self/mounts. Such a device might
994 * be mounted/enabled at a time where udev has not finished probing it yet, and we thus haven't learned about
995 * it yet. In this case we will set the device unit to "tentative" state.
997 * This takes a pair of DeviceFound flags parameters. The 'mask' parameter is a bit mask that indicates which
998 * bits of 'found' to copy into the per-device DeviceFound flags field. Thus, this function may be used to set
999 * and unset individual bits in a single call, while merging partially with previous state. */
1001 if ((found
& mask
) != 0) {
1002 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
1004 /* If the device is known in the kernel and newly appeared, then we'll create a device unit for it,
1005 * under the name referenced in /proc/swaps or /proc/self/mountinfo. But first, let's validate if
1006 * everything is alright with the device node. */
1008 r
= validate_node(m
, node
, &dev
);
1010 return; /* Don't create a device unit for this if the device node is borked. */
1012 (void) device_setup_unit(m
, dev
, node
, false);
1015 /* Update the device unit's state, should it exist */
1016 (void) device_update_found_by_name(m
, node
, found
, mask
);
1019 bool device_shall_be_bound_by(Unit
*device
, Unit
*u
) {
1023 if (u
->type
!= UNIT_MOUNT
)
1026 return DEVICE(device
)->bind_mounts
;
1029 const UnitVTable device_vtable
= {
1030 .object_size
= sizeof(Device
),
1038 .init
= device_init
,
1039 .done
= device_done
,
1040 .load
= unit_load_fragment_and_dropin_optional
,
1042 .coldplug
= device_coldplug
,
1043 .catchup
= device_catchup
,
1045 .serialize
= device_serialize
,
1046 .deserialize_item
= device_deserialize_item
,
1048 .dump
= device_dump
,
1050 .active_state
= device_active_state
,
1051 .sub_state_to_string
= device_sub_state_to_string
,
1053 .bus_vtable
= bus_device_vtable
,
1055 .following
= device_following
,
1056 .following_set
= device_following_set
,
1058 .enumerate
= device_enumerate
,
1059 .shutdown
= device_shutdown
,
1060 .supported
= device_supported
,
1062 .status_message_formats
= {
1063 .starting_stopping
= {
1064 [0] = "Expecting device %s...",
1066 .finished_start_job
= {
1067 [JOB_DONE
] = "Found device %s.",
1068 [JOB_TIMEOUT
] = "Timed out waiting for device %s.",