1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
11 #include "alloc-util.h"
12 #include "dbus-device.h"
15 #include "parse-util.h"
16 #include "path-util.h"
17 #include "stat-util.h"
18 #include "string-util.h"
20 #include "udev-util.h"
21 #include "unit-name.h"
24 static const UnitActiveState state_translation_table
[_DEVICE_STATE_MAX
] = {
25 [DEVICE_DEAD
] = UNIT_INACTIVE
,
26 [DEVICE_TENTATIVE
] = UNIT_ACTIVATING
,
27 [DEVICE_PLUGGED
] = UNIT_ACTIVE
,
30 static int device_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
31 static void device_update_found_one(Device
*d
, DeviceFound found
, DeviceFound mask
);
33 static void device_unset_sysfs(Device
*d
) {
42 /* Remove this unit from the chain of devices which share the
44 devices
= UNIT(d
)->manager
->devices_by_sysfs
;
45 first
= hashmap_get(devices
, d
->sysfs
);
46 LIST_REMOVE(same_sysfs
, first
, d
);
49 hashmap_remove_and_replace(devices
, d
->sysfs
, first
->sysfs
, first
);
51 hashmap_remove(devices
, d
->sysfs
);
53 d
->sysfs
= mfree(d
->sysfs
);
56 static int device_set_sysfs(Device
*d
, const char *sysfs
) {
57 _cleanup_free_
char *copy
= NULL
;
63 if (streq_ptr(d
->sysfs
, sysfs
))
66 r
= hashmap_ensure_allocated(&UNIT(d
)->manager
->devices_by_sysfs
, &path_hash_ops
);
74 device_unset_sysfs(d
);
76 first
= hashmap_get(UNIT(d
)->manager
->devices_by_sysfs
, sysfs
);
77 LIST_PREPEND(same_sysfs
, first
, d
);
79 r
= hashmap_replace(UNIT(d
)->manager
->devices_by_sysfs
, copy
, first
);
81 LIST_REMOVE(same_sysfs
, first
, d
);
85 d
->sysfs
= TAKE_PTR(copy
);
89 static void device_init(Unit
*u
) {
90 Device
*d
= DEVICE(u
);
93 assert(UNIT(d
)->load_state
== UNIT_STUB
);
95 /* In contrast to all other unit types we timeout jobs waiting
96 * for devices by default. This is because they otherwise wait
97 * indefinitely for plugged in devices, something which cannot
98 * happen for the other units since their operations time out
100 u
->job_running_timeout
= u
->manager
->default_timeout_start_usec
;
102 u
->ignore_on_isolate
= true;
104 d
->deserialized_state
= _DEVICE_STATE_INVALID
;
107 static void device_done(Unit
*u
) {
108 Device
*d
= DEVICE(u
);
112 device_unset_sysfs(d
);
115 static void device_set_state(Device
*d
, DeviceState state
) {
116 DeviceState old_state
;
119 old_state
= d
->state
;
122 if (state
== DEVICE_DEAD
)
123 device_unset_sysfs(d
);
125 if (state
!= old_state
)
126 log_unit_debug(UNIT(d
), "Changed %s -> %s", device_state_to_string(old_state
), device_state_to_string(state
));
128 unit_notify(UNIT(d
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
131 static int device_coldplug(Unit
*u
) {
132 Device
*d
= DEVICE(u
);
135 assert(d
->state
== DEVICE_DEAD
);
137 /* First, let's put the deserialized state and found mask into effect, if we have it. */
139 if (d
->deserialized_state
< 0 ||
140 (d
->deserialized_state
== d
->state
&&
141 d
->deserialized_found
== d
->found
))
144 d
->found
= d
->deserialized_found
;
145 device_set_state(d
, d
->deserialized_state
);
149 static void device_catchup(Unit
*u
) {
150 Device
*d
= DEVICE(u
);
154 /* Second, let's update the state with the enumerated state if it's different */
155 if (d
->enumerated_found
== d
->found
)
158 device_update_found_one(d
, d
->enumerated_found
, DEVICE_FOUND_MASK
);
161 static const struct {
164 } device_found_map
[] = {
165 { DEVICE_FOUND_UDEV
, "found-udev" },
166 { DEVICE_FOUND_MOUNT
, "found-mount" },
167 { DEVICE_FOUND_SWAP
, "found-swap" },
170 static int device_found_to_string_many(DeviceFound flags
, char **ret
) {
171 _cleanup_free_
char *s
= NULL
;
176 for (i
= 0; i
< ELEMENTSOF(device_found_map
); i
++) {
177 if (!FLAGS_SET(flags
, device_found_map
[i
].flag
))
180 if (!strextend_with_separator(&s
, ",", device_found_map
[i
].name
, NULL
))
189 static int device_found_from_string_many(const char *name
, DeviceFound
*ret
) {
190 DeviceFound flags
= 0;
196 _cleanup_free_
char *word
= NULL
;
200 r
= extract_first_word(&name
, &word
, ",", 0);
206 for (i
= 0; i
< ELEMENTSOF(device_found_map
); i
++)
207 if (streq(word
, device_found_map
[i
].name
)) {
208 f
= device_found_map
[i
].flag
;
222 static int device_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
223 _cleanup_free_
char *s
= NULL
;
224 Device
*d
= DEVICE(u
);
230 unit_serialize_item(u
, f
, "state", device_state_to_string(d
->state
));
232 if (device_found_to_string_many(d
->found
, &s
) >= 0)
233 unit_serialize_item(u
, f
, "found", s
);
238 static int device_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
239 Device
*d
= DEVICE(u
);
247 if (streq(key
, "state")) {
250 state
= device_state_from_string(value
);
252 log_unit_debug(u
, "Failed to parse state value, ignoring: %s", value
);
254 d
->deserialized_state
= state
;
256 } else if (streq(key
, "found")) {
257 r
= device_found_from_string_many(value
, &d
->deserialized_found
);
259 log_unit_debug_errno(u
, r
, "Failed to parse found value, ignoring: %s", value
);
262 log_unit_debug(u
, "Unknown serialization key: %s", key
);
267 static void device_dump(Unit
*u
, FILE *f
, const char *prefix
) {
268 Device
*d
= DEVICE(u
);
269 _cleanup_free_
char *s
= NULL
;
273 (void) device_found_to_string_many(d
->found
, &s
);
276 "%sDevice State: %s\n"
279 prefix
, device_state_to_string(d
->state
),
280 prefix
, strna(d
->sysfs
),
284 _pure_
static UnitActiveState
device_active_state(Unit
*u
) {
287 return state_translation_table
[DEVICE(u
)->state
];
290 _pure_
static const char *device_sub_state_to_string(Unit
*u
) {
293 return device_state_to_string(DEVICE(u
)->state
);
296 static int device_update_description(Unit
*u
, struct udev_device
*dev
, const char *path
) {
304 model
= udev_device_get_property_value(dev
, "ID_MODEL_FROM_DATABASE");
306 model
= udev_device_get_property_value(dev
, "ID_MODEL");
311 /* Try to concatenate the device model string with a label, if there is one */
312 label
= udev_device_get_property_value(dev
, "ID_FS_LABEL");
314 label
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_NAME");
316 label
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_NUMBER");
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
, struct udev_device
*dev
) {
337 const char *wants
, *property
;
343 property
= MANAGER_IS_USER(u
->manager
) ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS";
345 wants
= udev_device_get_property_value(dev
, property
);
350 _cleanup_free_
char *word
= NULL
, *k
= NULL
;
352 r
= extract_first_word(&wants
, &word
, NULL
, EXTRACT_QUOTES
);
358 return log_unit_error_errno(u
, r
, "Failed to parse property %s with value %s: %m", property
, wants
);
360 if (unit_name_is_valid(word
, UNIT_NAME_TEMPLATE
) && DEVICE(u
)->sysfs
) {
361 _cleanup_free_
char *escaped
= NULL
;
363 /* If the unit name is specified as template, then automatically fill in the sysfs path of the
364 * device as instance name, properly escaped. */
366 r
= unit_name_path_escape(DEVICE(u
)->sysfs
, &escaped
);
368 return log_unit_error_errno(u
, r
, "Failed to escape %s: %m", DEVICE(u
)->sysfs
);
370 r
= unit_name_replace_instance(word
, escaped
, &k
);
372 return log_unit_error_errno(u
, r
, "Failed to build %s instance of template %s: %m", escaped
, word
);
374 /* If this is not a template, then let's mangle it so, that it becomes a valid unit name. */
376 r
= unit_name_mangle(word
, UNIT_NAME_MANGLE_WARN
, &k
);
378 return log_unit_error_errno(u
, r
, "Failed to mangle unit name \"%s\": %m", word
);
381 r
= unit_add_dependency_by_name(u
, UNIT_WANTS
, k
, NULL
, true, UNIT_DEPENDENCY_UDEV
);
383 return log_unit_error_errno(u
, r
, "Failed to add Wants= dependency: %m");
387 static bool device_is_bound_by_mounts(Device
*d
, struct udev_device
*dev
) {
388 const char *bound_by
;
394 bound_by
= udev_device_get_property_value(dev
, "SYSTEMD_MOUNT_DEVICE_BOUND");
396 r
= parse_boolean(bound_by
);
398 log_warning_errno(r
, "Failed to parse SYSTEMD_MOUNT_DEVICE_BOUND='%s' udev property of %s, ignoring: %m", bound_by
, strna(d
->sysfs
));
400 d
->bind_mounts
= r
> 0;
402 d
->bind_mounts
= false;
404 return d
->bind_mounts
;
407 static void device_upgrade_mount_deps(Unit
*u
) {
413 /* Let's upgrade Requires= to BindsTo= on us. (Used when SYSTEMD_MOUNT_DEVICE_BOUND is set) */
415 HASHMAP_FOREACH_KEY(v
, other
, u
->dependencies
[UNIT_REQUIRED_BY
], i
) {
416 if (other
->type
!= UNIT_MOUNT
)
419 r
= unit_add_dependency(other
, UNIT_BINDS_TO
, u
, true, UNIT_DEPENDENCY_UDEV
);
421 log_unit_warning_errno(u
, r
, "Failed to add BindsTo= dependency between device and mount unit, ignoring: %m");
425 static int device_setup_unit(Manager
*m
, struct udev_device
*dev
, const char *path
, bool main
) {
426 _cleanup_free_
char *e
= NULL
;
427 const char *sysfs
= NULL
;
436 sysfs
= udev_device_get_syspath(dev
);
438 log_debug("Couldn't get syspath from udev device, ignoring.");
443 r
= unit_name_from_path(path
, ".device", &e
);
445 return log_error_errno(r
, "Failed to generate unit name from device path: %m");
447 u
= manager_get_unit(m
, e
);
449 /* The device unit can still be present even if the device was unplugged: a mount unit can reference it
450 * hence preventing the GC to have garbaged it. That's desired since the device unit may have a
451 * dependency on the mount unit which was added during the loading of the later. When the device is
452 * plugged the sysfs might not be initialized yet, as we serialize the device's state but do not
453 * serialize the sysfs path across reloads/reexecs. Hence, when coming back from a reload/restart we
454 * might have the state valid, but not the sysfs path. Hence, let's filter out conflicting devices, but
455 * let's accept devices in any state with no sysfs path set. */
457 if (DEVICE(u
)->state
== DEVICE_PLUGGED
&&
460 !path_equal(DEVICE(u
)->sysfs
, sysfs
)) {
461 log_unit_debug(u
, "Device %s appeared twice with different sysfs paths %s and %s, ignoring the latter.",
462 e
, DEVICE(u
)->sysfs
, sysfs
);
468 /* Let's remove all dependencies generated due to udev properties. We'll readd whatever is configured
470 unit_remove_dependencies(u
, UNIT_DEPENDENCY_UDEV
);
474 r
= unit_new_for_name(m
, sizeof(Device
), e
, &u
);
476 log_error_errno(r
, "Failed to allocate device unit %s: %m", e
);
480 unit_add_to_load_queue(u
);
483 /* If this was created via some dependency and has not actually been seen yet ->sysfs will not be
484 * initialized. Hence initialize it if necessary. */
486 r
= device_set_sysfs(DEVICE(u
), sysfs
);
488 log_error_errno(r
, "Failed to set sysfs path %s for device unit %s: %m", sysfs
, e
);
492 (void) device_update_description(u
, dev
, path
);
494 /* The additional systemd udev properties we only interpret for the main object */
496 (void) device_add_udev_wants(u
, dev
);
499 /* So the user wants the mount units to be bound to the device but a mount unit might has been seen by systemd
500 * before the device appears on its radar. In this case the device unit is partially initialized and includes
501 * the deps on the mount unit but at that time the "bind mounts" flag wasn't not present. Fix this up now. */
502 if (dev
&& device_is_bound_by_mounts(DEVICE(u
), dev
))
503 device_upgrade_mount_deps(u
);
505 /* Note that this won't dispatch the load queue, the caller has to do that if needed and appropriate */
506 unit_add_to_dbus_queue(u
);
517 static int device_process_new(Manager
*m
, struct udev_device
*dev
) {
518 const char *sysfs
, *dn
, *alias
;
519 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
524 sysfs
= udev_device_get_syspath(dev
);
528 /* Add the main unit named after the sysfs path */
529 r
= device_setup_unit(m
, dev
, sysfs
, true);
533 /* Add an additional unit for the device node */
534 dn
= udev_device_get_devnode(dev
);
536 (void) device_setup_unit(m
, dev
, dn
, false);
538 /* Add additional units for all symlinks */
539 first
= udev_device_get_devlinks_list_entry(dev
);
540 udev_list_entry_foreach(item
, first
) {
544 /* Don't bother with the /dev/block links */
545 p
= udev_list_entry_get_name(item
);
547 if (PATH_STARTSWITH_SET(p
, "/dev/block/", "/dev/char/"))
550 /* Verify that the symlink in the FS actually belongs
551 * to this device. This is useful to deal with
552 * conflicting devices, e.g. when two disks want the
553 * same /dev/disk/by-label/xxx link because they have
554 * the same label. We want to make sure that the same
555 * device that won the symlink wins in systemd, so we
556 * check the device node major/minor */
557 if (stat(p
, &st
) >= 0)
558 if ((!S_ISBLK(st
.st_mode
) && !S_ISCHR(st
.st_mode
)) ||
559 st
.st_rdev
!= udev_device_get_devnum(dev
))
562 (void) device_setup_unit(m
, dev
, p
, false);
565 /* Add additional units for all explicitly configured
567 alias
= udev_device_get_property_value(dev
, "SYSTEMD_ALIAS");
569 _cleanup_free_
char *word
= NULL
;
571 r
= extract_first_word(&alias
, &word
, NULL
, EXTRACT_QUOTES
);
577 return log_warning_errno(r
, "Failed to add parse SYSTEMD_ALIAS for %s: %m", sysfs
);
579 if (!path_is_absolute(word
))
580 log_warning("SYSTEMD_ALIAS for %s is not an absolute path, ignoring: %s", sysfs
, word
);
581 else if (!path_is_normalized(word
))
582 log_warning("SYSTEMD_ALIAS for %s is not a normalized path, ignoring: %s", sysfs
, word
);
584 (void) device_setup_unit(m
, dev
, word
, false);
590 static void device_found_changed(Device
*d
, DeviceFound previous
, DeviceFound now
) {
593 /* Didn't exist before, but does now? if so, generate a new invocation ID for it */
594 if (previous
== DEVICE_NOT_FOUND
&& now
!= DEVICE_NOT_FOUND
)
595 (void) unit_acquire_invocation_id(UNIT(d
));
597 if (FLAGS_SET(now
, DEVICE_FOUND_UDEV
))
598 /* When the device is known to udev we consider it plugged. */
599 device_set_state(d
, DEVICE_PLUGGED
);
600 else if (now
!= DEVICE_NOT_FOUND
&& !FLAGS_SET(previous
, DEVICE_FOUND_UDEV
))
601 /* If the device has not been seen by udev yet, but is now referenced by the kernel, then we assume the
602 * kernel knows it now, and udev might soon too. */
603 device_set_state(d
, DEVICE_TENTATIVE
);
605 /* If nobody sees the device, or if the device was previously seen by udev and now is only referenced
606 * from the kernel, then we consider the device is gone, the kernel just hasn't noticed it yet. */
607 device_set_state(d
, DEVICE_DEAD
);
610 static void device_update_found_one(Device
*d
, DeviceFound found
, DeviceFound mask
) {
613 if (MANAGER_IS_RUNNING(UNIT(d
)->manager
)) {
614 DeviceFound n
, previous
;
616 /* When we are already running, then apply the new mask right-away, and trigger state changes
619 n
= (d
->found
& ~mask
) | (found
& mask
);
626 device_found_changed(d
, previous
, n
);
628 /* We aren't running yet, let's apply the new mask to the shadow variable instead, which we'll apply as
629 * soon as we catch-up with the state. */
630 d
->enumerated_found
= (d
->enumerated_found
& ~mask
) | (found
& mask
);
633 static void device_update_found_by_sysfs(Manager
*m
, const char *sysfs
, DeviceFound found
, DeviceFound mask
) {
642 l
= hashmap_get(m
->devices_by_sysfs
, sysfs
);
643 LIST_FOREACH_SAFE(same_sysfs
, d
, n
, l
)
644 device_update_found_one(d
, found
, mask
);
647 static int device_update_found_by_name(Manager
*m
, const char *path
, DeviceFound found
, DeviceFound mask
) {
648 _cleanup_free_
char *e
= NULL
;
658 r
= unit_name_from_path(path
, ".device", &e
);
660 return log_error_errno(r
, "Failed to generate unit name from device path: %m");
662 u
= manager_get_unit(m
, e
);
666 device_update_found_one(DEVICE(u
), found
, mask
);
670 static bool device_is_ready(struct udev_device
*dev
) {
675 ready
= udev_device_get_property_value(dev
, "SYSTEMD_READY");
679 return parse_boolean(ready
) != 0;
682 static Unit
*device_following(Unit
*u
) {
683 Device
*d
= DEVICE(u
);
684 Device
*other
, *first
= NULL
;
688 if (startswith(u
->id
, "sys-"))
691 /* Make everybody follow the unit that's named after the sysfs path */
692 for (other
= d
->same_sysfs_next
; other
; other
= other
->same_sysfs_next
)
693 if (startswith(UNIT(other
)->id
, "sys-"))
696 for (other
= d
->same_sysfs_prev
; other
; other
= other
->same_sysfs_prev
) {
697 if (startswith(UNIT(other
)->id
, "sys-"))
706 static int device_following_set(Unit
*u
, Set
**_set
) {
707 Device
*d
= DEVICE(u
), *other
;
708 _cleanup_set_free_ Set
*set
= NULL
;
714 if (LIST_JUST_US(same_sysfs
, d
)) {
723 LIST_FOREACH_AFTER(same_sysfs
, other
, d
) {
724 r
= set_put(set
, other
);
729 LIST_FOREACH_BEFORE(same_sysfs
, other
, d
) {
730 r
= set_put(set
, other
);
735 *_set
= TAKE_PTR(set
);
739 static void device_shutdown(Manager
*m
) {
742 m
->udev_event_source
= sd_event_source_unref(m
->udev_event_source
);
743 m
->udev_monitor
= udev_monitor_unref(m
->udev_monitor
);
744 m
->devices_by_sysfs
= hashmap_free(m
->devices_by_sysfs
);
747 static void device_enumerate(Manager
*m
) {
748 _cleanup_(udev_enumerate_unrefp
) struct udev_enumerate
*e
= NULL
;
749 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
754 if (!m
->udev_monitor
) {
755 m
->udev_monitor
= udev_monitor_new_from_netlink(m
->udev
, "udev");
756 if (!m
->udev_monitor
) {
757 log_error_errno(errno
, "Failed to allocate udev monitor: %m");
761 /* This will fail if we are unprivileged, but that
762 * should not matter much, as user instances won't run
764 (void) udev_monitor_set_receive_buffer_size(m
->udev_monitor
, 128*1024*1024);
766 r
= udev_monitor_filter_add_match_tag(m
->udev_monitor
, "systemd");
768 log_error_errno(r
, "Failed to add udev tag match: %m");
772 r
= udev_monitor_enable_receiving(m
->udev_monitor
);
774 log_error_errno(r
, "Failed to enable udev event reception: %m");
778 r
= sd_event_add_io(m
->event
, &m
->udev_event_source
, udev_monitor_get_fd(m
->udev_monitor
), EPOLLIN
, device_dispatch_io
, m
);
780 log_error_errno(r
, "Failed to watch udev file descriptor: %m");
784 (void) sd_event_source_set_description(m
->udev_event_source
, "device");
787 e
= udev_enumerate_new(m
->udev
);
789 log_error_errno(errno
, "Failed to alloacte udev enumerator: %m");
793 r
= udev_enumerate_add_match_tag(e
, "systemd");
795 log_error_errno(r
, "Failed to create udev tag enumeration: %m");
799 r
= udev_enumerate_add_match_is_initialized(e
);
801 log_error_errno(r
, "Failed to install initialization match into enumeration: %m");
805 r
= udev_enumerate_scan_devices(e
);
807 log_error_errno(r
, "Failed to enumerate devices: %m");
811 first
= udev_enumerate_get_list_entry(e
);
812 udev_list_entry_foreach(item
, first
) {
813 _cleanup_(udev_device_unrefp
) struct udev_device
*dev
= NULL
;
816 sysfs
= udev_list_entry_get_name(item
);
818 dev
= udev_device_new_from_syspath(m
->udev
, sysfs
);
820 if (errno
== ENOMEM
) {
825 /* If we can't create a device, don't bother, it probably just disappeared. */
826 log_debug_errno(errno
, "Failed to create udev device object for %s: %m", sysfs
);
830 if (!device_is_ready(dev
))
833 (void) device_process_new(m
, dev
);
834 device_update_found_by_sysfs(m
, sysfs
, DEVICE_FOUND_UDEV
, DEVICE_FOUND_UDEV
);
843 static void device_propagate_reload_by_sysfs(Manager
*m
, const char *sysfs
) {
850 l
= hashmap_get(m
->devices_by_sysfs
, sysfs
);
851 LIST_FOREACH_SAFE(same_sysfs
, d
, n
, l
) {
852 if (d
->state
== DEVICE_DEAD
)
855 r
= manager_propagate_reload(m
, UNIT(d
), JOB_REPLACE
, NULL
);
857 log_warning_errno(r
, "Failed to propagate reload, ignoring: %m");
861 static int device_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
862 _cleanup_(udev_device_unrefp
) struct udev_device
*dev
= NULL
;
863 Manager
*m
= userdata
;
864 const char *action
, *sysfs
;
869 if (revents
!= EPOLLIN
) {
870 static RATELIMIT_DEFINE(limit
, 10*USEC_PER_SEC
, 5);
872 if (ratelimit_below(&limit
))
873 log_warning("Failed to get udev event");
874 if (!(revents
& EPOLLIN
))
879 * libudev might filter-out devices which pass the bloom
880 * filter, so getting NULL here is not necessarily an error.
882 dev
= udev_monitor_receive_device(m
->udev_monitor
);
886 sysfs
= udev_device_get_syspath(dev
);
888 log_error("Failed to get udev sys path.");
892 action
= udev_device_get_action(dev
);
894 log_error("Failed to get udev action string.");
898 if (streq(action
, "change"))
899 device_propagate_reload_by_sysfs(m
, sysfs
);
901 /* A change event can signal that a device is becoming ready, in particular if
902 * the device is using the SYSTEMD_READY logic in udev
903 * so we need to reach the else block of the follwing if, even for change events */
904 if (streq(action
, "remove")) {
905 r
= swap_process_device_remove(m
, dev
);
907 log_warning_errno(r
, "Failed to process swap device remove event, ignoring: %m");
909 /* If we get notified that a device was removed by
910 * udev, then it's completely gone, hence unset all
912 device_update_found_by_sysfs(m
, sysfs
, 0, DEVICE_FOUND_UDEV
|DEVICE_FOUND_MOUNT
|DEVICE_FOUND_SWAP
);
914 } else if (device_is_ready(dev
)) {
916 (void) device_process_new(m
, dev
);
918 r
= swap_process_device_new(m
, dev
);
920 log_warning_errno(r
, "Failed to process swap device new event, ignoring: %m");
922 manager_dispatch_load_queue(m
);
924 /* The device is found now, set the udev found bit */
925 device_update_found_by_sysfs(m
, sysfs
, DEVICE_FOUND_UDEV
, DEVICE_FOUND_UDEV
);
928 /* The device is nominally around, but not ready for
929 * us. Hence unset the udev bit, but leave the rest
932 device_update_found_by_sysfs(m
, sysfs
, 0, DEVICE_FOUND_UDEV
);
938 static bool device_supported(void) {
939 static int read_only
= -1;
941 /* If /sys is read-only we don't support device units, and any
942 * attempts to start one should fail immediately. */
945 read_only
= path_is_read_only_fs("/sys");
947 return read_only
<= 0;
950 static int validate_node(Manager
*m
, const char *node
, struct udev_device
**ret
) {
958 /* Validates a device node that showed up in /proc/swaps or /proc/self/mountinfo if it makes sense for us to
959 * track. Note that this validator is fine within missing device nodes, but not with badly set up ones! */
961 if (!path_startswith(node
, "/dev")) {
966 if (stat(node
, &st
) < 0) {
968 return log_error_errno(errno
, "Failed to stat() device node file %s: %m", node
);
971 return 1; /* good! (though missing) */
974 _cleanup_(udev_device_unrefp
) struct udev_device
*dev
= NULL
;
976 r
= udev_device_new_from_stat_rdev(m
->udev
, &st
, &dev
);
979 return 1; /* good! (though missing) */
980 } else if (r
== -ENOTTY
) {
982 return 0; /* bad! (not a device node but some other kind of file system node) */
984 return log_error_errno(r
, "Failed to get udev device from devnum %u:%u: %m", major(st
.st_rdev
), minor(st
.st_rdev
));
986 *ret
= TAKE_PTR(dev
);
987 return 1; /* good! */
991 void device_found_node(Manager
*m
, const char *node
, DeviceFound found
, DeviceFound mask
) {
997 if (!device_supported())
1003 /* This is called whenever we find a device referenced in /proc/swaps or /proc/self/mounts. Such a device might
1004 * be mounted/enabled at a time where udev has not finished probing it yet, and we thus haven't learned about
1005 * it yet. In this case we will set the device unit to "tentative" state.
1007 * This takes a pair of DeviceFound flags parameters. The 'mask' parameter is a bit mask that indicates which
1008 * bits of 'found' to copy into the per-device DeviceFound flags field. Thus, this function may be used to set
1009 * and unset individual bits in a single call, while merging partially with previous state. */
1011 if ((found
& mask
) != 0) {
1012 _cleanup_(udev_device_unrefp
) struct udev_device
*dev
= NULL
;
1014 /* If the device is known in the kernel and newly appeared, then we'll create a device unit for it,
1015 * under the name referenced in /proc/swaps or /proc/self/mountinfo. But first, let's validate if
1016 * everything is alright with the device node. */
1018 r
= validate_node(m
, node
, &dev
);
1020 return; /* Don't create a device unit for this if the device node is borked. */
1022 (void) device_setup_unit(m
, dev
, node
, false);
1025 /* Update the device unit's state, should it exist */
1026 (void) device_update_found_by_name(m
, node
, found
, mask
);
1029 bool device_shall_be_bound_by(Unit
*device
, Unit
*u
) {
1033 if (u
->type
!= UNIT_MOUNT
)
1036 return DEVICE(device
)->bind_mounts
;
1039 const UnitVTable device_vtable
= {
1040 .object_size
= sizeof(Device
),
1048 .init
= device_init
,
1049 .done
= device_done
,
1050 .load
= unit_load_fragment_and_dropin_optional
,
1052 .coldplug
= device_coldplug
,
1053 .catchup
= device_catchup
,
1055 .serialize
= device_serialize
,
1056 .deserialize_item
= device_deserialize_item
,
1058 .dump
= device_dump
,
1060 .active_state
= device_active_state
,
1061 .sub_state_to_string
= device_sub_state_to_string
,
1063 .bus_vtable
= bus_device_vtable
,
1065 .following
= device_following
,
1066 .following_set
= device_following_set
,
1068 .enumerate
= device_enumerate
,
1069 .shutdown
= device_shutdown
,
1070 .supported
= device_supported
,
1072 .status_message_formats
= {
1073 .starting_stopping
= {
1074 [0] = "Expecting device %s...",
1076 .finished_start_job
= {
1077 [JOB_DONE
] = "Found device %s.",
1078 [JOB_TIMEOUT
] = "Timed out waiting for device %s.",