1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
11 static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = {
12 [DEVICE_DEAD] = UNIT_INACTIVE,
13 [DEVICE_AVAILABLE] = UNIT_ACTIVE
16 static const char* const state_string_table[_DEVICE_STATE_MAX] = {
17 [DEVICE_DEAD] = "dead",
18 [DEVICE_AVAILABLE] = "available"
21 static void device_done(Unit *u) {
22 Device *d = DEVICE(u);
28 static void device_set_state(Device *d, DeviceState state) {
29 DeviceState old_state;
35 log_debug("%s changed %s → %s", unit_id(UNIT(d)), state_string_table[old_state], state_string_table[state]);
37 unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state]);
40 static int device_coldplug(Unit *u) {
41 Device *d = DEVICE(u);
44 assert(d->state == DEVICE_DEAD);
47 device_set_state(d, DEVICE_AVAILABLE);
52 static void device_dump(Unit *u, FILE *f, const char *prefix) {
53 Device *d = DEVICE(u);
58 "%sDevice State: %s\n"
60 prefix, state_string_table[d->state],
61 prefix, strna(d->sysfs));
64 static UnitActiveState device_active_state(Unit *u) {
67 return state_translation_table[DEVICE(u)->state];
70 static int device_add_escaped_name(Unit *u, const char *prefix, const char *dn, bool make_id) {
78 if (!(e = unit_name_escape_path(prefix, dn+1, ".device")))
81 r = unit_add_name(u, e);
83 if (r >= 0 && make_id)
88 if (r < 0 && r != -EEXIST)
94 static int device_process_device(Manager *m, struct udev_device *dev) {
95 const char *dn, *names, *wants, *sysfs;
101 struct udev_list_entry *item = NULL, *first = NULL;
105 /* Check whether this entry is even relevant for us. */
106 dn = udev_device_get_devnode(dev);
107 names = udev_device_get_property_value(dev, "SYSTEMD_NAMES");
108 wants = udev_device_get_property_value(dev, "SYSTEMD_WANTS");
110 if (!dn && !names && !wants)
113 /* Ok, seems kinda interesting. Now, let's see if this one
116 if (!(sysfs = udev_device_get_syspath(dev)))
119 assert(sysfs[0] == '/');
120 if (!(e = unit_name_escape_path("sysfs-", sysfs+1, ".device")))
123 if (!(u = manager_get_unit(m, e))) {
128 if (!(u = unit_new(m))) {
133 r = unit_add_name(u, e);
139 if (!(DEVICE(u)->sysfs = strdup(sysfs))) {
144 if ((model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE")) ||
145 (model = udev_device_get_property_value(dev, "ID_MODEL")))
146 if ((r = unit_set_description(u, model)) < 0)
155 if ((r = device_add_escaped_name(u, "node-", dn, true)) < 0)
158 first = udev_device_get_devlinks_list_entry(dev);
159 udev_list_entry_foreach(item, first)
160 if ((r = device_add_escaped_name(u, "node-", udev_list_entry_get_name(item), false)) < 0)
164 FOREACH_WORD(w, l, names, state) {
165 if (!(e = strndup(w, l)))
168 r = unit_add_name(u, e);
171 if (r < 0 && r != -EEXIST)
177 if (set_isempty(u->meta.names)) {
183 FOREACH_WORD(w, l, wants, state) {
184 if (!(e = strndup(w, l)))
187 r = unit_add_dependency_by_name(u, UNIT_WANTS, e);
195 unit_add_to_load_queue(u);
204 static int device_process_path(Manager *m, const char *path) {
206 struct udev_device *dev;
211 if (!(dev = udev_device_new_from_syspath(m->udev, path))) {
212 log_warning("Failed to get udev device object from udev for path %s.", path);
216 r = device_process_device(m, dev);
217 udev_device_unref(dev);
221 static void device_shutdown(Manager *m) {
228 static int device_enumerate(Manager *m) {
230 struct udev_enumerate *e = NULL;
231 struct udev_list_entry *item = NULL, *first = NULL;
235 if (!(m->udev = udev_new()))
238 if (!(e = udev_enumerate_new(m->udev))) {
243 if (udev_enumerate_scan_devices(e) < 0) {
248 first = udev_enumerate_get_list_entry(e);
249 udev_list_entry_foreach(item, first)
250 device_process_path(m, udev_list_entry_get_name(item));
252 udev_enumerate_unref(e);
258 udev_enumerate_unref(e);
264 const UnitVTable device_vtable = {
267 .init = unit_load_fragment_and_dropin,
269 .coldplug = device_coldplug,
273 .active_state = device_active_state,
275 .enumerate = device_enumerate,
276 .shutdown = device_shutdown