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>
30 #include "unit-name.h"
31 #include "dbus-device.h"
33 #include "path-util.h"
34 #include "udev-util.h"
36 static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = {
37 [DEVICE_DEAD] = UNIT_INACTIVE,
38 [DEVICE_PLUGGED] = UNIT_ACTIVE
41 static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
43 static void device_unset_sysfs(Device *d) {
52 /* Remove this unit from the chain of devices which share the
54 devices = UNIT(d)->manager->devices_by_sysfs;
55 first = hashmap_get(devices, d->sysfs);
56 LIST_REMOVE(same_sysfs, first, d);
59 hashmap_remove_and_replace(devices, d->sysfs, first->sysfs, first);
61 hashmap_remove(devices, d->sysfs);
67 static void device_init(Unit *u) {
68 Device *d = DEVICE(u);
71 assert(UNIT(d)->load_state == UNIT_STUB);
73 /* In contrast to all other unit types we timeout jobs waiting
74 * for devices by default. This is because they otherwise wait
75 * indefinitely for plugged in devices, something which cannot
76 * happen for the other units since their operations time out
78 u->job_timeout = u->manager->default_timeout_start_usec;
80 u->ignore_on_isolate = true;
81 u->ignore_on_snapshot = true;
84 static void device_done(Unit *u) {
85 Device *d = DEVICE(u);
89 device_unset_sysfs(d);
92 static void device_set_state(Device *d, DeviceState state) {
93 DeviceState old_state;
99 if (state != old_state)
100 log_debug_unit(UNIT(d)->id,
101 "%s changed %s -> %s", UNIT(d)->id,
102 device_state_to_string(old_state),
103 device_state_to_string(state));
105 unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state], true);
108 static int device_coldplug(Unit *u) {
109 Device *d = DEVICE(u);
112 assert(d->state == DEVICE_DEAD);
115 device_set_state(d, DEVICE_PLUGGED);
120 static void device_dump(Unit *u, FILE *f, const char *prefix) {
121 Device *d = DEVICE(u);
126 "%sDevice State: %s\n"
127 "%sSysfs Path: %s\n",
128 prefix, device_state_to_string(d->state),
129 prefix, strna(d->sysfs));
132 _pure_ static UnitActiveState device_active_state(Unit *u) {
135 return state_translation_table[DEVICE(u)->state];
138 _pure_ static const char *device_sub_state_to_string(Unit *u) {
141 return device_state_to_string(DEVICE(u)->state);
144 static int device_add_escaped_name(Unit *u, const char *dn) {
145 _cleanup_free_ char *e = NULL;
150 assert(dn[0] == '/');
152 e = unit_name_from_path(dn, ".device");
156 r = unit_add_name(u, e);
157 if (r < 0 && r != -EEXIST)
163 static int device_find_escape_name(Manager *m, const char *dn, Unit **_u) {
164 _cleanup_free_ char *e = NULL;
169 assert(dn[0] == '/');
172 e = unit_name_from_path(dn, ".device");
176 u = manager_get_unit(m, e);
185 static int device_update_unit(Manager *m, struct udev_device *dev, const char *path, bool main) {
186 const char *sysfs, *model;
193 sysfs = udev_device_get_syspath(dev);
197 r = device_find_escape_name(m, path, &u);
201 if (u && DEVICE(u)->sysfs && !path_equal(DEVICE(u)->sysfs, sysfs))
207 u = unit_new(m, sizeof(Device));
211 r = device_add_escaped_name(u, path);
215 unit_add_to_load_queue(u);
219 /* If this was created via some dependency and has not
220 * actually been seen yet ->sysfs will not be
221 * initialized. Hence initialize it if necessary. */
223 if (!DEVICE(u)->sysfs) {
226 DEVICE(u)->sysfs = strdup(sysfs);
227 if (!DEVICE(u)->sysfs) {
232 r = hashmap_ensure_allocated(&m->devices_by_sysfs, string_hash_func, string_compare_func);
236 first = hashmap_get(m->devices_by_sysfs, sysfs);
237 LIST_PREPEND(same_sysfs, first, DEVICE(u));
239 r = hashmap_replace(m->devices_by_sysfs, DEVICE(u)->sysfs, first);
244 if ((model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE")) ||
245 (model = udev_device_get_property_value(dev, "ID_MODEL")))
246 r = unit_set_description(u, model);
248 r = unit_set_description(u, path);
255 /* The additional systemd udev properties we only
256 * interpret for the main object */
258 wants = udev_device_get_property_value(dev, m->running_as == SYSTEMD_USER ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS");
263 FOREACH_WORD_QUOTED(w, l, wants, state) {
264 _cleanup_free_ char *n = NULL;
270 n = unit_name_mangle(e);
276 r = unit_add_dependency_by_name(u, UNIT_WANTS, n, NULL, true);
283 /* Note that this won't dispatch the load queue, the caller
284 * has to do that if needed and appropriate */
286 unit_add_to_dbus_queue(u);
290 log_warning("Failed to load device unit: %s", strerror(-r));
298 static int device_process_new_device(Manager *m, struct udev_device *dev) {
299 const char *sysfs, *dn, *alias;
300 struct udev_list_entry *item = NULL, *first = NULL;
305 sysfs = udev_device_get_syspath(dev);
309 /* Add the main unit named after the sysfs path */
310 r = device_update_unit(m, dev, sysfs, true);
314 /* Add an additional unit for the device node */
315 dn = udev_device_get_devnode(dev);
317 device_update_unit(m, dev, dn, false);
319 /* Add additional units for all symlinks */
320 first = udev_device_get_devlinks_list_entry(dev);
321 udev_list_entry_foreach(item, first) {
325 /* Don't bother with the /dev/block links */
326 p = udev_list_entry_get_name(item);
328 if (path_startswith(p, "/dev/block/") ||
329 path_startswith(p, "/dev/char/"))
332 /* Verify that the symlink in the FS actually belongs
333 * to this device. This is useful to deal with
334 * conflicting devices, e.g. when two disks want the
335 * same /dev/disk/by-label/xxx link because they have
336 * the same label. We want to make sure that the same
337 * device that won the symlink wins in systemd, so we
338 * check the device node major/minor*/
339 if (stat(p, &st) >= 0)
340 if ((!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) ||
341 st.st_rdev != udev_device_get_devnum(dev))
344 device_update_unit(m, dev, p, false);
347 /* Add additional units for all explicitly configured
349 alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS");
354 FOREACH_WORD_QUOTED(w, l, alias, state) {
360 if (path_is_absolute(e))
361 device_update_unit(m, dev, e, false);
363 log_warning("SYSTEMD_ALIAS for %s is not an absolute path, ignoring: %s", sysfs, e);
370 static void device_set_path_plugged(Manager *m, struct udev_device *dev) {
377 sysfs = udev_device_get_syspath(dev);
381 l = hashmap_get(m->devices_by_sysfs, sysfs);
382 LIST_FOREACH(same_sysfs, d, l)
383 device_set_state(d, DEVICE_PLUGGED);
386 static int device_process_removed_device(Manager *m, struct udev_device *dev) {
393 sysfs = udev_device_get_syspath(dev);
397 /* Remove all units of this sysfs path */
398 while ((d = hashmap_get(m->devices_by_sysfs, sysfs))) {
399 device_unset_sysfs(d);
400 device_set_state(d, DEVICE_DEAD);
406 static bool device_is_ready(struct udev_device *dev) {
411 ready = udev_device_get_property_value(dev, "SYSTEMD_READY");
415 return parse_boolean(ready) != 0;
418 static int device_process_new_path(Manager *m, const char *path) {
419 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
424 dev = udev_device_new_from_syspath(m->udev, path);
428 if (!device_is_ready(dev))
431 return device_process_new_device(m, dev);
434 static Unit *device_following(Unit *u) {
435 Device *d = DEVICE(u);
436 Device *other, *first = NULL;
440 if (startswith(u->id, "sys-"))
443 /* Make everybody follow the unit that's named after the sysfs path */
444 for (other = d->same_sysfs_next; other; other = other->same_sysfs_next)
445 if (startswith(UNIT(other)->id, "sys-"))
448 for (other = d->same_sysfs_prev; other; other = other->same_sysfs_prev) {
449 if (startswith(UNIT(other)->id, "sys-"))
458 static int device_following_set(Unit *u, Set **_set) {
459 Device *d = DEVICE(u), *other;
466 if (LIST_JUST_US(same_sysfs, d)) {
471 set = set_new(NULL, NULL);
475 LIST_FOREACH_AFTER(same_sysfs, other, d) {
476 r = set_put(set, other);
481 LIST_FOREACH_BEFORE(same_sysfs, other, d) {
482 r = set_put(set, other);
495 static void device_shutdown(Manager *m) {
498 m->udev_event_source = sd_event_source_unref(m->udev_event_source);
500 if (m->udev_monitor) {
501 udev_monitor_unref(m->udev_monitor);
502 m->udev_monitor = NULL;
510 hashmap_free(m->devices_by_sysfs);
511 m->devices_by_sysfs = NULL;
514 static int device_enumerate(Manager *m) {
515 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
516 struct udev_list_entry *item = NULL, *first = NULL;
522 m->udev = udev_new();
526 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
527 if (!m->udev_monitor) {
532 /* This will fail if we are unprivileged, but that
533 * should not matter much, as user instances won't run
535 udev_monitor_set_receive_buffer_size(m->udev_monitor, 128*1024*1024);
537 r = udev_monitor_filter_add_match_tag(m->udev_monitor, "systemd");
541 r = udev_monitor_enable_receiving(m->udev_monitor);
545 r = sd_event_add_io(m->event, udev_monitor_get_fd(m->udev_monitor), EPOLLIN, device_dispatch_io, m, &m->udev_event_source);
550 e = udev_enumerate_new(m->udev);
556 r = udev_enumerate_add_match_tag(e, "systemd");
560 r = udev_enumerate_scan_devices(e);
564 first = udev_enumerate_get_list_entry(e);
565 udev_list_entry_foreach(item, first)
566 device_process_new_path(m, udev_list_entry_get_name(item));
575 static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
576 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
577 Manager *m = userdata;
583 if (revents != EPOLLIN) {
584 static RATELIMIT_DEFINE(limit, 10*USEC_PER_SEC, 5);
586 if (!ratelimit_test(&limit))
587 log_error("Failed to get udev event: %m");
588 if (!(revents & EPOLLIN))
593 * libudev might filter-out devices which pass the bloom
594 * filter, so getting NULL here is not necessarily an error.
596 dev = udev_monitor_receive_device(m->udev_monitor);
600 action = udev_device_get_action(dev);
602 log_error("Failed to get udev action string.");
606 if (streq(action, "remove") || !device_is_ready(dev)) {
607 r = device_process_removed_device(m, dev);
609 log_error("Failed to process device remove event: %s", strerror(-r));
611 r = device_process_new_device(m, dev);
613 log_error("Failed to process device new event: %s", strerror(-r));
615 manager_dispatch_load_queue(m);
617 device_set_path_plugged(m, dev);
623 static const char* const device_state_table[_DEVICE_STATE_MAX] = {
624 [DEVICE_DEAD] = "dead",
625 [DEVICE_PLUGGED] = "plugged"
628 DEFINE_STRING_TABLE_LOOKUP(device_state, DeviceState);
630 const UnitVTable device_vtable = {
631 .object_size = sizeof(Device),
637 .no_instances = true,
641 .load = unit_load_fragment_and_dropin_optional,
643 .coldplug = device_coldplug,
647 .active_state = device_active_state,
648 .sub_state_to_string = device_sub_state_to_string,
650 .bus_interface = "org.freedesktop.systemd1.Device",
651 .bus_changing_properties = bus_device_changing_properties,
652 .bus_vtable = bus_device_vtable,
654 .following = device_following,
655 .following_set = device_following_set,
657 .enumerate = device_enumerate,
658 .shutdown = device_shutdown,
660 .status_message_formats = {
661 .starting_stopping = {
662 [0] = "Expecting device %s...",
664 .finished_start_job = {
665 [JOB_DONE] = "Found device %s.",
666 [JOB_TIMEOUT] = "Timed out waiting for device %s.",