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>
28 #include "unit-name.h"
29 #include "dbus-device.h"
31 #include "path-util.h"
32 #include "udev-util.h"
37 static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = {
38 [DEVICE_DEAD] = UNIT_INACTIVE,
39 [DEVICE_PLUGGED] = UNIT_ACTIVE
42 static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
44 static void device_unset_sysfs(Device *d) {
53 /* Remove this unit from the chain of devices which share the
55 devices = UNIT(d)->manager->devices_by_sysfs;
56 first = hashmap_get(devices, d->sysfs);
57 LIST_REMOVE(same_sysfs, first, d);
60 hashmap_remove_and_replace(devices, d->sysfs, first->sysfs, first);
62 hashmap_remove(devices, d->sysfs);
68 static void device_init(Unit *u) {
69 Device *d = DEVICE(u);
72 assert(UNIT(d)->load_state == UNIT_STUB);
74 /* In contrast to all other unit types we timeout jobs waiting
75 * for devices by default. This is because they otherwise wait
76 * indefinitely for plugged in devices, something which cannot
77 * happen for the other units since their operations time out
79 u->job_timeout = u->manager->default_timeout_start_usec;
81 u->ignore_on_isolate = true;
82 u->ignore_on_snapshot = true;
85 static void device_done(Unit *u) {
86 Device *d = DEVICE(u);
90 device_unset_sysfs(d);
93 static void device_set_state(Device *d, DeviceState state) {
94 DeviceState old_state;
100 if (state != old_state)
101 log_unit_debug(UNIT(d)->id,
102 "%s changed %s -> %s", UNIT(d)->id,
103 device_state_to_string(old_state),
104 device_state_to_string(state));
106 unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state], true);
109 static int device_coldplug(Unit *u) {
110 Device *d = DEVICE(u);
113 assert(d->state == DEVICE_DEAD);
116 device_set_state(d, DEVICE_PLUGGED);
121 static void device_dump(Unit *u, FILE *f, const char *prefix) {
122 Device *d = DEVICE(u);
127 "%sDevice State: %s\n"
128 "%sSysfs Path: %s\n",
129 prefix, device_state_to_string(d->state),
130 prefix, strna(d->sysfs));
133 _pure_ static UnitActiveState device_active_state(Unit *u) {
136 return state_translation_table[DEVICE(u)->state];
139 _pure_ static const char *device_sub_state_to_string(Unit *u) {
142 return device_state_to_string(DEVICE(u)->state);
145 static int device_add_escaped_name(Unit *u, const char *dn) {
146 _cleanup_free_ char *e = NULL;
151 assert(dn[0] == '/');
153 e = unit_name_from_path(dn, ".device");
157 r = unit_add_name(u, e);
158 if (r < 0 && r != -EEXIST)
164 static int device_find_escape_name(Manager *m, const char *dn, Unit **_u) {
165 _cleanup_free_ char *e = NULL;
170 assert(dn[0] == '/');
173 e = unit_name_from_path(dn, ".device");
177 u = manager_get_unit(m, e);
186 static int device_make_description(Unit *u, struct udev_device *dev, const char *path) {
193 model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE");
195 model = udev_device_get_property_value(dev, "ID_MODEL");
200 /* Try to concatenate the device model string with a label, if there is one */
201 label = udev_device_get_property_value(dev, "ID_FS_LABEL");
203 label = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME");
205 label = udev_device_get_property_value(dev, "ID_PART_ENTRY_NUMBER");
208 _cleanup_free_ char *j;
210 j = strjoin(model, " ", label, NULL);
212 return unit_set_description(u, j);
215 return unit_set_description(u, model);
218 return unit_set_description(u, path);
221 static int device_add_udev_wants(Unit *u, struct udev_device *dev) {
223 const char *word, *state;
226 const char *property;
231 property = u->manager->running_as == SYSTEMD_USER ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS";
232 wants = udev_device_get_property_value(dev, property);
236 FOREACH_WORD_QUOTED(word, l, wants, state) {
237 _cleanup_free_ char *n = NULL;
243 n = unit_name_mangle(e, MANGLE_NOGLOB);
247 r = unit_add_dependency_by_name(u, UNIT_WANTS, n, NULL, true);
252 log_unit_warning(u->id, "Property %s on %s has trailing garbage, ignoring.",
253 property, strna(udev_device_get_syspath(dev)));
258 static int device_update_unit(Manager *m, struct udev_device *dev, const char *path, bool main) {
268 sysfs = udev_device_get_syspath(dev);
272 r = device_find_escape_name(m, path, &u);
276 if (u && DEVICE(u)->sysfs && !path_equal(DEVICE(u)->sysfs, sysfs))
282 u = unit_new(m, sizeof(Device));
286 r = device_add_escaped_name(u, path);
290 unit_add_to_load_queue(u);
294 /* If this was created via some dependency and has not
295 * actually been seen yet ->sysfs will not be
296 * initialized. Hence initialize it if necessary. */
298 if (!DEVICE(u)->sysfs) {
301 DEVICE(u)->sysfs = strdup(sysfs);
302 if (!DEVICE(u)->sysfs) {
307 r = hashmap_ensure_allocated(&m->devices_by_sysfs, &string_hash_ops);
311 first = hashmap_get(m->devices_by_sysfs, sysfs);
312 LIST_PREPEND(same_sysfs, first, DEVICE(u));
314 r = hashmap_replace(m->devices_by_sysfs, DEVICE(u)->sysfs, first);
319 device_make_description(u, dev, path);
322 /* The additional systemd udev properties we only
323 * interpret for the main object */
325 r = device_add_udev_wants(u, dev);
330 /* Note that this won't dispatch the load queue, the caller
331 * has to do that if needed and appropriate */
333 unit_add_to_dbus_queue(u);
337 log_warning_errno(r, "Failed to load device unit: %m");
345 static int device_process_new_device(Manager *m, struct udev_device *dev) {
346 const char *sysfs, *dn, *alias;
347 struct udev_list_entry *item = NULL, *first = NULL;
352 sysfs = udev_device_get_syspath(dev);
356 /* Add the main unit named after the sysfs path */
357 r = device_update_unit(m, dev, sysfs, true);
361 /* Add an additional unit for the device node */
362 dn = udev_device_get_devnode(dev);
364 device_update_unit(m, dev, dn, false);
366 /* Add additional units for all symlinks */
367 first = udev_device_get_devlinks_list_entry(dev);
368 udev_list_entry_foreach(item, first) {
372 /* Don't bother with the /dev/block links */
373 p = udev_list_entry_get_name(item);
375 if (path_startswith(p, "/dev/block/") ||
376 path_startswith(p, "/dev/char/"))
379 /* Verify that the symlink in the FS actually belongs
380 * to this device. This is useful to deal with
381 * conflicting devices, e.g. when two disks want the
382 * same /dev/disk/by-label/xxx link because they have
383 * the same label. We want to make sure that the same
384 * device that won the symlink wins in systemd, so we
385 * check the device node major/minor */
386 if (stat(p, &st) >= 0)
387 if ((!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) ||
388 st.st_rdev != udev_device_get_devnum(dev))
391 device_update_unit(m, dev, p, false);
394 /* Add additional units for all explicitly configured
396 alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS");
398 const char *word, *state;
401 FOREACH_WORD_QUOTED(word, l, alias, state) {
407 if (path_is_absolute(e))
408 device_update_unit(m, dev, e, false);
410 log_warning("SYSTEMD_ALIAS for %s is not an absolute path, ignoring: %s", sysfs, e);
413 log_warning("SYSTEMD_ALIAS for %s has trailing garbage, ignoring.", sysfs);
419 static void device_set_path_plugged(Manager *m, struct udev_device *dev) {
426 sysfs = udev_device_get_syspath(dev);
430 l = hashmap_get(m->devices_by_sysfs, sysfs);
431 LIST_FOREACH(same_sysfs, d, l)
432 device_set_state(d, DEVICE_PLUGGED);
435 static int device_process_removed_device(Manager *m, struct udev_device *dev) {
442 sysfs = udev_device_get_syspath(dev);
446 /* Remove all units of this sysfs path */
447 while ((d = hashmap_get(m->devices_by_sysfs, sysfs))) {
448 device_unset_sysfs(d);
449 device_set_state(d, DEVICE_DEAD);
455 static bool device_is_ready(struct udev_device *dev) {
460 ready = udev_device_get_property_value(dev, "SYSTEMD_READY");
464 return parse_boolean(ready) != 0;
467 static int device_process_new_path(Manager *m, const char *path) {
468 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
473 dev = udev_device_new_from_syspath(m->udev, path);
477 if (!device_is_ready(dev))
480 return device_process_new_device(m, dev);
483 static Unit *device_following(Unit *u) {
484 Device *d = DEVICE(u);
485 Device *other, *first = NULL;
489 if (startswith(u->id, "sys-"))
492 /* Make everybody follow the unit that's named after the sysfs path */
493 for (other = d->same_sysfs_next; other; other = other->same_sysfs_next)
494 if (startswith(UNIT(other)->id, "sys-"))
497 for (other = d->same_sysfs_prev; other; other = other->same_sysfs_prev) {
498 if (startswith(UNIT(other)->id, "sys-"))
507 static int device_following_set(Unit *u, Set **_set) {
508 Device *d = DEVICE(u), *other;
515 if (LIST_JUST_US(same_sysfs, d)) {
524 LIST_FOREACH_AFTER(same_sysfs, other, d) {
525 r = set_put(set, other);
530 LIST_FOREACH_BEFORE(same_sysfs, other, d) {
531 r = set_put(set, other);
544 static void device_shutdown(Manager *m) {
547 m->udev_event_source = sd_event_source_unref(m->udev_event_source);
549 if (m->udev_monitor) {
550 udev_monitor_unref(m->udev_monitor);
551 m->udev_monitor = NULL;
554 hashmap_free(m->devices_by_sysfs);
555 m->devices_by_sysfs = NULL;
558 static int device_enumerate(Manager *m) {
559 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
560 struct udev_list_entry *item = NULL, *first = NULL;
565 if (!m->udev_monitor) {
566 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
567 if (!m->udev_monitor) {
572 /* This will fail if we are unprivileged, but that
573 * should not matter much, as user instances won't run
575 udev_monitor_set_receive_buffer_size(m->udev_monitor, 128*1024*1024);
577 r = udev_monitor_filter_add_match_tag(m->udev_monitor, "systemd");
581 r = udev_monitor_enable_receiving(m->udev_monitor);
585 r = sd_event_add_io(m->event, &m->udev_event_source, udev_monitor_get_fd(m->udev_monitor), EPOLLIN, device_dispatch_io, m);
590 e = udev_enumerate_new(m->udev);
596 r = udev_enumerate_add_match_tag(e, "systemd");
600 r = udev_enumerate_add_match_is_initialized(e);
604 r = udev_enumerate_scan_devices(e);
608 first = udev_enumerate_get_list_entry(e);
609 udev_list_entry_foreach(item, first)
610 device_process_new_path(m, udev_list_entry_get_name(item));
619 static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
620 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
621 Manager *m = userdata;
627 if (revents != EPOLLIN) {
628 static RATELIMIT_DEFINE(limit, 10*USEC_PER_SEC, 5);
630 if (!ratelimit_test(&limit))
631 log_error_errno(errno, "Failed to get udev event: %m");
632 if (!(revents & EPOLLIN))
637 * libudev might filter-out devices which pass the bloom
638 * filter, so getting NULL here is not necessarily an error.
640 dev = udev_monitor_receive_device(m->udev_monitor);
644 action = udev_device_get_action(dev);
646 log_error("Failed to get udev action string.");
650 if (streq(action, "remove") || !device_is_ready(dev)) {
651 r = device_process_removed_device(m, dev);
653 log_error_errno(r, "Failed to process device remove event: %m");
655 r = swap_process_removed_device(m, dev);
657 log_error_errno(r, "Failed to process swap device remove event: %m");
660 r = device_process_new_device(m, dev);
662 log_error_errno(r, "Failed to process device new event: %m");
664 r = swap_process_new_device(m, dev);
666 log_error_errno(r, "Failed to process swap device new event: %m");
668 manager_dispatch_load_queue(m);
670 device_set_path_plugged(m, dev);
676 static bool device_supported(Manager *m) {
677 static int read_only = -1;
680 /* If /sys is read-only we don't support device units, and any
681 * attempts to start one should fail immediately. */
684 read_only = path_is_read_only_fs("/sys");
686 return read_only <= 0;
689 static const char* const device_state_table[_DEVICE_STATE_MAX] = {
690 [DEVICE_DEAD] = "dead",
691 [DEVICE_PLUGGED] = "plugged"
694 DEFINE_STRING_TABLE_LOOKUP(device_state, DeviceState);
696 const UnitVTable device_vtable = {
697 .object_size = sizeof(Device),
703 .no_instances = true,
707 .load = unit_load_fragment_and_dropin_optional,
709 .coldplug = device_coldplug,
713 .active_state = device_active_state,
714 .sub_state_to_string = device_sub_state_to_string,
716 .bus_interface = "org.freedesktop.systemd1.Device",
717 .bus_vtable = bus_device_vtable,
719 .following = device_following,
720 .following_set = device_following_set,
722 .enumerate = device_enumerate,
723 .shutdown = device_shutdown,
724 .supported = device_supported,
726 .status_message_formats = {
727 .starting_stopping = {
728 [0] = "Expecting device %s...",
730 .finished_start_job = {
731 [JOB_DONE] = "Found device %s.",
732 [JOB_TIMEOUT] = "Timed out waiting for device %s.",