1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/epoll.h>
27 #include "unit-name.h"
28 #include "dbus-device.h"
29 #include "path-util.h"
30 #include "udev-util.h"
35 static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = {
36 [DEVICE_DEAD] = UNIT_INACTIVE,
37 [DEVICE_PLUGGED] = UNIT_ACTIVE
40 static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
42 static void device_unset_sysfs(Device *d) {
51 /* Remove this unit from the chain of devices which share the
53 devices = UNIT(d)->manager->devices_by_sysfs;
54 first = hashmap_get(devices, d->sysfs);
55 LIST_REMOVE(same_sysfs, first, d);
58 hashmap_remove_and_replace(devices, d->sysfs, first->sysfs, first);
60 hashmap_remove(devices, d->sysfs);
66 static void device_init(Unit *u) {
67 Device *d = DEVICE(u);
70 assert(UNIT(d)->load_state == UNIT_STUB);
72 /* In contrast to all other unit types we timeout jobs waiting
73 * for devices by default. This is because they otherwise wait
74 * indefinitely for plugged in devices, something which cannot
75 * happen for the other units since their operations time out
77 u->job_timeout = u->manager->default_timeout_start_usec;
79 u->ignore_on_isolate = true;
80 u->ignore_on_snapshot = true;
83 static void device_done(Unit *u) {
84 Device *d = DEVICE(u);
88 device_unset_sysfs(d);
91 static void device_set_state(Device *d, DeviceState state) {
92 DeviceState old_state;
98 if (state != old_state)
99 log_unit_debug(UNIT(d)->id,
100 "%s changed %s -> %s", UNIT(d)->id,
101 device_state_to_string(old_state),
102 device_state_to_string(state));
104 unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state], true);
107 static int device_coldplug(Unit *u) {
108 Device *d = DEVICE(u);
111 assert(d->state == DEVICE_DEAD);
114 device_set_state(d, DEVICE_PLUGGED);
119 static void device_dump(Unit *u, FILE *f, const char *prefix) {
120 Device *d = DEVICE(u);
125 "%sDevice State: %s\n"
126 "%sSysfs Path: %s\n",
127 prefix, device_state_to_string(d->state),
128 prefix, strna(d->sysfs));
131 _pure_ static UnitActiveState device_active_state(Unit *u) {
134 return state_translation_table[DEVICE(u)->state];
137 _pure_ static const char *device_sub_state_to_string(Unit *u) {
140 return device_state_to_string(DEVICE(u)->state);
143 static int device_add_escaped_name(Unit *u, const char *dn) {
144 _cleanup_free_ char *e = NULL;
149 assert(dn[0] == '/');
151 e = unit_name_from_path(dn, ".device");
155 r = unit_add_name(u, e);
156 if (r < 0 && r != -EEXIST)
162 static int device_find_escape_name(Manager *m, const char *dn, Unit **_u) {
163 _cleanup_free_ char *e = NULL;
168 assert(dn[0] == '/');
171 e = unit_name_from_path(dn, ".device");
175 u = manager_get_unit(m, e);
184 static int device_make_description(Unit *u, struct udev_device *dev, const char *path) {
191 model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE");
193 model = udev_device_get_property_value(dev, "ID_MODEL");
198 /* Try to concatenate the device model string with a label, if there is one */
199 label = udev_device_get_property_value(dev, "ID_FS_LABEL");
201 label = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME");
203 label = udev_device_get_property_value(dev, "ID_PART_ENTRY_NUMBER");
206 _cleanup_free_ char *j;
208 j = strjoin(model, " ", label, NULL);
210 return unit_set_description(u, j);
213 return unit_set_description(u, model);
216 return unit_set_description(u, path);
219 static int device_add_udev_wants(Unit *u, struct udev_device *dev) {
221 const char *word, *state;
224 const char *property;
229 property = u->manager->running_as == SYSTEMD_USER ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS";
230 wants = udev_device_get_property_value(dev, property);
234 FOREACH_WORD_QUOTED(word, l, wants, state) {
235 _cleanup_free_ char *n = NULL;
241 n = unit_name_mangle(e, MANGLE_NOGLOB);
245 r = unit_add_dependency_by_name(u, UNIT_WANTS, n, NULL, true);
250 log_unit_warning(u->id, "Property %s on %s has trailing garbage, ignoring.",
251 property, strna(udev_device_get_syspath(dev)));
256 static int device_update_unit(Manager *m, struct udev_device *dev, const char *path, bool main) {
266 sysfs = udev_device_get_syspath(dev);
270 r = device_find_escape_name(m, path, &u);
274 if (u && DEVICE(u)->sysfs && !path_equal(DEVICE(u)->sysfs, sysfs))
280 u = unit_new(m, sizeof(Device));
284 r = device_add_escaped_name(u, path);
288 unit_add_to_load_queue(u);
292 /* If this was created via some dependency and has not
293 * actually been seen yet ->sysfs will not be
294 * initialized. Hence initialize it if necessary. */
296 if (!DEVICE(u)->sysfs) {
299 DEVICE(u)->sysfs = strdup(sysfs);
300 if (!DEVICE(u)->sysfs) {
305 r = hashmap_ensure_allocated(&m->devices_by_sysfs, &string_hash_ops);
309 first = hashmap_get(m->devices_by_sysfs, sysfs);
310 LIST_PREPEND(same_sysfs, first, DEVICE(u));
312 r = hashmap_replace(m->devices_by_sysfs, DEVICE(u)->sysfs, first);
317 device_make_description(u, dev, path);
320 /* The additional systemd udev properties we only
321 * interpret for the main object */
323 r = device_add_udev_wants(u, dev);
328 /* Note that this won't dispatch the load queue, the caller
329 * has to do that if needed and appropriate */
331 unit_add_to_dbus_queue(u);
335 log_warning_errno(r, "Failed to load device unit: %m");
343 static int device_process_new_device(Manager *m, struct udev_device *dev) {
344 const char *sysfs, *dn, *alias;
345 struct udev_list_entry *item = NULL, *first = NULL;
350 sysfs = udev_device_get_syspath(dev);
354 /* Add the main unit named after the sysfs path */
355 r = device_update_unit(m, dev, sysfs, true);
359 /* Add an additional unit for the device node */
360 dn = udev_device_get_devnode(dev);
362 device_update_unit(m, dev, dn, false);
364 /* Add additional units for all symlinks */
365 first = udev_device_get_devlinks_list_entry(dev);
366 udev_list_entry_foreach(item, first) {
370 /* Don't bother with the /dev/block links */
371 p = udev_list_entry_get_name(item);
373 if (path_startswith(p, "/dev/block/") ||
374 path_startswith(p, "/dev/char/"))
377 /* Verify that the symlink in the FS actually belongs
378 * to this device. This is useful to deal with
379 * conflicting devices, e.g. when two disks want the
380 * same /dev/disk/by-label/xxx link because they have
381 * the same label. We want to make sure that the same
382 * device that won the symlink wins in systemd, so we
383 * check the device node major/minor */
384 if (stat(p, &st) >= 0)
385 if ((!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) ||
386 st.st_rdev != udev_device_get_devnum(dev))
389 device_update_unit(m, dev, p, false);
392 /* Add additional units for all explicitly configured
394 alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS");
396 const char *word, *state;
399 FOREACH_WORD_QUOTED(word, l, alias, state) {
405 if (path_is_absolute(e))
406 device_update_unit(m, dev, e, false);
408 log_warning("SYSTEMD_ALIAS for %s is not an absolute path, ignoring: %s", sysfs, e);
411 log_warning("SYSTEMD_ALIAS for %s has trailing garbage, ignoring.", sysfs);
417 static void device_set_path_plugged(Manager *m, struct udev_device *dev) {
424 sysfs = udev_device_get_syspath(dev);
428 l = hashmap_get(m->devices_by_sysfs, sysfs);
429 LIST_FOREACH(same_sysfs, d, l)
430 device_set_state(d, DEVICE_PLUGGED);
433 static int device_process_removed_device(Manager *m, struct udev_device *dev) {
440 sysfs = udev_device_get_syspath(dev);
444 /* Remove all units of this sysfs path */
445 while ((d = hashmap_get(m->devices_by_sysfs, sysfs))) {
446 device_unset_sysfs(d);
447 device_set_state(d, DEVICE_DEAD);
453 static bool device_is_ready(struct udev_device *dev) {
458 ready = udev_device_get_property_value(dev, "SYSTEMD_READY");
462 return parse_boolean(ready) != 0;
465 static int device_process_new_path(Manager *m, const char *path) {
466 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
471 dev = udev_device_new_from_syspath(m->udev, path);
475 if (!device_is_ready(dev))
478 return device_process_new_device(m, dev);
481 static Unit *device_following(Unit *u) {
482 Device *d = DEVICE(u);
483 Device *other, *first = NULL;
487 if (startswith(u->id, "sys-"))
490 /* Make everybody follow the unit that's named after the sysfs path */
491 for (other = d->same_sysfs_next; other; other = other->same_sysfs_next)
492 if (startswith(UNIT(other)->id, "sys-"))
495 for (other = d->same_sysfs_prev; other; other = other->same_sysfs_prev) {
496 if (startswith(UNIT(other)->id, "sys-"))
505 static int device_following_set(Unit *u, Set **_set) {
506 Device *d = DEVICE(u), *other;
513 if (LIST_JUST_US(same_sysfs, d)) {
522 LIST_FOREACH_AFTER(same_sysfs, other, d) {
523 r = set_put(set, other);
528 LIST_FOREACH_BEFORE(same_sysfs, other, d) {
529 r = set_put(set, other);
542 static void device_shutdown(Manager *m) {
545 m->udev_event_source = sd_event_source_unref(m->udev_event_source);
547 if (m->udev_monitor) {
548 udev_monitor_unref(m->udev_monitor);
549 m->udev_monitor = NULL;
552 hashmap_free(m->devices_by_sysfs);
553 m->devices_by_sysfs = NULL;
556 static int device_enumerate(Manager *m) {
557 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
558 struct udev_list_entry *item = NULL, *first = NULL;
563 if (!m->udev_monitor) {
564 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
565 if (!m->udev_monitor) {
570 /* This will fail if we are unprivileged, but that
571 * should not matter much, as user instances won't run
573 udev_monitor_set_receive_buffer_size(m->udev_monitor, 128*1024*1024);
575 r = udev_monitor_filter_add_match_tag(m->udev_monitor, "systemd");
579 r = udev_monitor_enable_receiving(m->udev_monitor);
583 r = sd_event_add_io(m->event, &m->udev_event_source, udev_monitor_get_fd(m->udev_monitor), EPOLLIN, device_dispatch_io, m);
588 e = udev_enumerate_new(m->udev);
594 r = udev_enumerate_add_match_tag(e, "systemd");
598 r = udev_enumerate_add_match_is_initialized(e);
602 r = udev_enumerate_scan_devices(e);
606 first = udev_enumerate_get_list_entry(e);
607 udev_list_entry_foreach(item, first)
608 device_process_new_path(m, udev_list_entry_get_name(item));
617 static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
618 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
619 Manager *m = userdata;
625 if (revents != EPOLLIN) {
626 static RATELIMIT_DEFINE(limit, 10*USEC_PER_SEC, 5);
628 if (!ratelimit_test(&limit))
629 log_error_errno(errno, "Failed to get udev event: %m");
630 if (!(revents & EPOLLIN))
635 * libudev might filter-out devices which pass the bloom
636 * filter, so getting NULL here is not necessarily an error.
638 dev = udev_monitor_receive_device(m->udev_monitor);
642 action = udev_device_get_action(dev);
644 log_error("Failed to get udev action string.");
648 if (streq(action, "remove") || !device_is_ready(dev)) {
649 r = device_process_removed_device(m, dev);
651 log_error_errno(r, "Failed to process device remove event: %m");
653 r = swap_process_removed_device(m, dev);
655 log_error_errno(r, "Failed to process swap device remove event: %m");
658 r = device_process_new_device(m, dev);
660 log_error_errno(r, "Failed to process device new event: %m");
662 r = swap_process_new_device(m, dev);
664 log_error_errno(r, "Failed to process swap device new event: %m");
666 manager_dispatch_load_queue(m);
668 device_set_path_plugged(m, dev);
674 static bool device_supported(Manager *m) {
675 static int read_only = -1;
678 /* If /sys is read-only we don't support device units, and any
679 * attempts to start one should fail immediately. */
682 read_only = path_is_read_only_fs("/sys");
684 return read_only <= 0;
687 static const char* const device_state_table[_DEVICE_STATE_MAX] = {
688 [DEVICE_DEAD] = "dead",
689 [DEVICE_PLUGGED] = "plugged"
692 DEFINE_STRING_TABLE_LOOKUP(device_state, DeviceState);
694 const UnitVTable device_vtable = {
695 .object_size = sizeof(Device),
701 .no_instances = true,
705 .load = unit_load_fragment_and_dropin_optional,
707 .coldplug = device_coldplug,
711 .active_state = device_active_state,
712 .sub_state_to_string = device_sub_state_to_string,
714 .bus_interface = "org.freedesktop.systemd1.Device",
715 .bus_vtable = bus_device_vtable,
717 .following = device_following,
718 .following_set = device_following_set,
720 .enumerate = device_enumerate,
721 .shutdown = device_shutdown,
722 .supported = device_supported,
724 .status_message_formats = {
725 .starting_stopping = {
726 [0] = "Expecting device %s...",
728 .finished_start_job = {
729 [JOB_DONE] = "Found device %s.",
730 [JOB_TIMEOUT] = "Timed out waiting for device %s.",