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) {
51 /* Remove this unit from the chain of devices which share the
53 first = hashmap_get(UNIT(d)->manager->devices_by_sysfs, d->sysfs);
54 LIST_REMOVE(same_sysfs, first, d);
57 hashmap_remove_and_replace(UNIT(d)->manager->devices_by_sysfs, d->sysfs, first->sysfs, first);
59 hashmap_remove(UNIT(d)->manager->devices_by_sysfs, d->sysfs);
65 static void device_init(Unit *u) {
66 Device *d = DEVICE(u);
69 assert(UNIT(d)->load_state == UNIT_STUB);
71 /* In contrast to all other unit types we timeout jobs waiting
72 * for devices by default. This is because they otherwise wait
73 * indefinitely for plugged in devices, something which cannot
74 * happen for the other units since their operations time out
76 UNIT(d)->job_timeout = u->manager->default_timeout_start_usec;
78 UNIT(d)->ignore_on_isolate = true;
79 UNIT(d)->ignore_on_snapshot = true;
82 static void device_done(Unit *u) {
83 Device *d = DEVICE(u);
87 device_unset_sysfs(d);
90 static void device_set_state(Device *d, DeviceState state) {
91 DeviceState old_state;
97 if (state != old_state)
98 log_debug_unit(UNIT(d)->id,
99 "%s changed %s -> %s", UNIT(d)->id,
100 device_state_to_string(old_state),
101 device_state_to_string(state));
103 unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state], true);
106 static int device_coldplug(Unit *u) {
107 Device *d = DEVICE(u);
110 assert(d->state == DEVICE_DEAD);
113 device_set_state(d, DEVICE_PLUGGED);
118 static void device_dump(Unit *u, FILE *f, const char *prefix) {
119 Device *d = DEVICE(u);
124 "%sDevice State: %s\n"
125 "%sSysfs Path: %s\n",
126 prefix, device_state_to_string(d->state),
127 prefix, strna(d->sysfs));
130 _pure_ static UnitActiveState device_active_state(Unit *u) {
133 return state_translation_table[DEVICE(u)->state];
136 _pure_ static const char *device_sub_state_to_string(Unit *u) {
139 return device_state_to_string(DEVICE(u)->state);
142 static int device_add_escaped_name(Unit *u, const char *dn) {
143 _cleanup_free_ char *e = NULL;
148 assert(dn[0] == '/');
150 e = unit_name_from_path(dn, ".device");
154 r = unit_add_name(u, e);
155 if (r < 0 && r != -EEXIST)
161 static int device_find_escape_name(Manager *m, const char *dn, Unit **_u) {
162 _cleanup_free_ char *e = NULL;
167 assert(dn[0] == '/');
170 e = unit_name_from_path(dn, ".device");
174 u = manager_get_unit(m, e);
183 static int device_update_unit(Manager *m, struct udev_device *dev, const char *path, bool main) {
184 const char *sysfs, *model;
191 sysfs = udev_device_get_syspath(dev);
195 r = device_find_escape_name(m, path, &u);
199 if (u && DEVICE(u)->sysfs && !path_equal(DEVICE(u)->sysfs, sysfs))
205 u = unit_new(m, sizeof(Device));
209 r = device_add_escaped_name(u, path);
213 unit_add_to_load_queue(u);
217 /* If this was created via some dependency and has not
218 * actually been seen yet ->sysfs will not be
219 * initialized. Hence initialize it if necessary. */
221 if (!DEVICE(u)->sysfs) {
224 DEVICE(u)->sysfs = strdup(sysfs);
225 if (!DEVICE(u)->sysfs) {
230 r = hashmap_ensure_allocated(&m->devices_by_sysfs, string_hash_func, string_compare_func);
234 first = hashmap_get(m->devices_by_sysfs, sysfs);
235 LIST_PREPEND(same_sysfs, first, DEVICE(u));
237 r = hashmap_replace(m->devices_by_sysfs, DEVICE(u)->sysfs, first);
242 if ((model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE")) ||
243 (model = udev_device_get_property_value(dev, "ID_MODEL")))
244 r = unit_set_description(u, model);
246 r = unit_set_description(u, path);
251 /* The additional systemd udev properties we only
252 * interpret for the main object */
253 const char *wants, *alias;
255 alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS");
260 FOREACH_WORD_QUOTED(w, l, alias, state) {
261 _cleanup_free_ char *e;
270 device_update_unit(m, dev, e, false);
272 log_warning("SYSTEMD_ALIAS for %s is not a path, ignoring: %s", sysfs, e);
276 wants = udev_device_get_property_value(dev, m->running_as == SYSTEMD_USER ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS");
281 FOREACH_WORD_QUOTED(w, l, wants, state) {
282 _cleanup_free_ char *e, *n = NULL;
290 n = unit_name_mangle(e);
296 r = unit_add_dependency_by_name(u, UNIT_WANTS, n, NULL, true);
303 unit_add_to_dbus_queue(u);
307 log_warning("Failed to load device unit: %s", strerror(-r));
315 static int device_process_new_device(Manager *m, struct udev_device *dev, bool update_state) {
316 const char *sysfs, *dn;
317 struct udev_list_entry *item = NULL, *first = NULL;
322 sysfs = udev_device_get_syspath(dev);
326 /* Add the main unit named after the sysfs path */
327 r = device_update_unit(m, dev, sysfs, true);
331 /* Add an additional unit for the device node */
332 if ((dn = udev_device_get_devnode(dev)))
333 device_update_unit(m, dev, dn, false);
335 /* Add additional units for all symlinks */
336 first = udev_device_get_devlinks_list_entry(dev);
337 udev_list_entry_foreach(item, first) {
341 /* Don't bother with the /dev/block links */
342 p = udev_list_entry_get_name(item);
344 if (path_startswith(p, "/dev/block/") ||
345 path_startswith(p, "/dev/char/"))
348 /* Verify that the symlink in the FS actually belongs
349 * to this device. This is useful to deal with
350 * conflicting devices, e.g. when two disks want the
351 * same /dev/disk/by-label/xxx link because they have
352 * the same label. We want to make sure that the same
353 * device that won the symlink wins in systemd, so we
354 * check the device node major/minor*/
355 if (stat(p, &st) >= 0)
356 if ((!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) ||
357 st.st_rdev != udev_device_get_devnum(dev))
360 device_update_unit(m, dev, p, false);
366 manager_dispatch_load_queue(m);
368 l = hashmap_get(m->devices_by_sysfs, sysfs);
369 LIST_FOREACH(same_sysfs, d, l)
370 device_set_state(d, DEVICE_PLUGGED);
376 static int device_process_path(Manager *m, const char *path, bool update_state) {
378 struct udev_device *dev;
383 dev = udev_device_new_from_syspath(m->udev, path);
385 log_warning("Failed to get udev device object from udev for path %s.", path);
389 r = device_process_new_device(m, dev, update_state);
390 udev_device_unref(dev);
394 static int device_process_removed_device(Manager *m, struct udev_device *dev) {
401 if (!(sysfs = udev_device_get_syspath(dev)))
404 /* Remove all units of this sysfs path */
405 while ((d = hashmap_get(m->devices_by_sysfs, sysfs))) {
406 device_unset_sysfs(d);
407 device_set_state(d, DEVICE_DEAD);
413 static Unit *device_following(Unit *u) {
414 Device *d = DEVICE(u);
415 Device *other, *first = NULL;
419 if (startswith(u->id, "sys-"))
422 /* Make everybody follow the unit that's named after the sysfs path */
423 for (other = d->same_sysfs_next; other; other = other->same_sysfs_next)
424 if (startswith(UNIT(other)->id, "sys-"))
427 for (other = d->same_sysfs_prev; other; other = other->same_sysfs_prev) {
428 if (startswith(UNIT(other)->id, "sys-"))
437 static int device_following_set(Unit *u, Set **_s) {
438 Device *d = DEVICE(u);
446 if (!d->same_sysfs_prev && !d->same_sysfs_next) {
451 if (!(s = set_new(NULL, NULL)))
454 for (other = d->same_sysfs_next; other; other = other->same_sysfs_next)
455 if ((r = set_put(s, other)) < 0)
458 for (other = d->same_sysfs_prev; other; other = other->same_sysfs_prev)
459 if ((r = set_put(s, other)) < 0)
470 static void device_shutdown(Manager *m) {
473 m->udev_event_source = sd_event_source_unref(m->udev_event_source);
475 if (m->udev_monitor) {
476 udev_monitor_unref(m->udev_monitor);
477 m->udev_monitor = NULL;
485 hashmap_free(m->devices_by_sysfs);
486 m->devices_by_sysfs = NULL;
489 static int device_enumerate(Manager *m) {
490 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
491 struct udev_list_entry *item = NULL, *first = NULL;
497 m->udev = udev_new();
501 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
502 if (!m->udev_monitor) {
507 /* This will fail if we are unprivileged, but that
508 * should not matter much, as user instances won't run
510 udev_monitor_set_receive_buffer_size(m->udev_monitor, 128*1024*1024);
512 r = udev_monitor_filter_add_match_tag(m->udev_monitor, "systemd");
516 r = udev_monitor_enable_receiving(m->udev_monitor);
520 r = sd_event_add_io(m->event, udev_monitor_get_fd(m->udev_monitor), EPOLLIN, device_dispatch_io, m, &m->udev_event_source);
525 e = udev_enumerate_new(m->udev);
531 r = udev_enumerate_add_match_tag(e, "systemd");
535 r = udev_enumerate_scan_devices(e);
539 first = udev_enumerate_get_list_entry(e);
540 udev_list_entry_foreach(item, first)
541 device_process_path(m, udev_list_entry_get_name(item), false);
550 static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
551 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
552 const char *action, *ready;
553 Manager *m = userdata;
558 if (revents != EPOLLIN) {
559 static RATELIMIT_DEFINE(limit, 10*USEC_PER_SEC, 5);
561 if (!ratelimit_test(&limit))
562 log_error("Failed to get udev event: %m");
563 if (!(revents & EPOLLIN))
568 * libudev might filter-out devices which pass the bloom
569 * filter, so getting NULL here is not necessarily an error.
571 dev = udev_monitor_receive_device(m->udev_monitor);
575 action = udev_device_get_action(dev);
577 log_error("Failed to get udev action string.");
581 ready = udev_device_get_property_value(dev, "SYSTEMD_READY");
583 if (streq(action, "remove") || (ready && parse_boolean(ready) == 0)) {
584 r = device_process_removed_device(m, dev);
586 log_error("Failed to process udev device event: %s", strerror(-r));
588 r = device_process_new_device(m, dev, true);
590 log_error("Failed to process udev device event: %s", strerror(-r));
596 static const char* const device_state_table[_DEVICE_STATE_MAX] = {
597 [DEVICE_DEAD] = "dead",
598 [DEVICE_PLUGGED] = "plugged"
601 DEFINE_STRING_TABLE_LOOKUP(device_state, DeviceState);
603 const UnitVTable device_vtable = {
604 .object_size = sizeof(Device),
610 .no_instances = true,
614 .load = unit_load_fragment_and_dropin_optional,
616 .coldplug = device_coldplug,
620 .active_state = device_active_state,
621 .sub_state_to_string = device_sub_state_to_string,
623 .bus_interface = "org.freedesktop.systemd1.Device",
624 .bus_changing_properties = bus_device_changing_properties,
625 .bus_vtable = bus_device_vtable,
627 .following = device_following,
628 .following_set = device_following_set,
630 .enumerate = device_enumerate,
631 .shutdown = device_shutdown,
633 .status_message_formats = {
634 .starting_stopping = {
635 [0] = "Expecting device %s...",
637 .finished_start_job = {
638 [JOB_DONE] = "Found device %s.",
639 [JOB_TIMEOUT] = "Timed out waiting for device %s.",