]>
Commit | Line | Data |
---|---|---|
a7334b09 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
a7334b09 | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
25ac040b | 20 | #include <errno.h> |
f94ea366 | 21 | #include <sys/epoll.h> |
25ac040b | 22 | |
b4bbcaa9 TA |
23 | #include "libudev.h" |
24 | ||
b5efdb8a | 25 | #include "alloc-util.h" |
4139c1b2 | 26 | #include "dbus-device.h" |
6bedfcbb | 27 | #include "device.h" |
07630cea | 28 | #include "log.h" |
6bedfcbb | 29 | #include "parse-util.h" |
9eb977db | 30 | #include "path-util.h" |
8fcde012 | 31 | #include "stat-util.h" |
07630cea LP |
32 | #include "string-util.h" |
33 | #include "swap.h" | |
718db961 | 34 | #include "udev-util.h" |
07630cea | 35 | #include "unit-name.h" |
9670d583 | 36 | #include "unit.h" |
5cb5a6ff | 37 | |
f50e0a01 LP |
38 | static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = { |
39 | [DEVICE_DEAD] = UNIT_INACTIVE, | |
628c89cc LP |
40 | [DEVICE_TENTATIVE] = UNIT_ACTIVATING, |
41 | [DEVICE_PLUGGED] = UNIT_ACTIVE, | |
f50e0a01 LP |
42 | }; |
43 | ||
718db961 LP |
44 | static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
45 | ||
8fe914ec | 46 | static void device_unset_sysfs(Device *d) { |
f1421cc6 | 47 | Hashmap *devices; |
8fe914ec LP |
48 | Device *first; |
49 | ||
50 | assert(d); | |
51 | ||
a7f241db LP |
52 | if (!d->sysfs) |
53 | return; | |
54 | ||
55 | /* Remove this unit from the chain of devices which share the | |
56 | * same sysfs path. */ | |
f1421cc6 LP |
57 | devices = UNIT(d)->manager->devices_by_sysfs; |
58 | first = hashmap_get(devices, d->sysfs); | |
71fda00f | 59 | LIST_REMOVE(same_sysfs, first, d); |
8fe914ec | 60 | |
a7f241db | 61 | if (first) |
f1421cc6 | 62 | hashmap_remove_and_replace(devices, d->sysfs, first->sysfs, first); |
a7f241db | 63 | else |
f1421cc6 | 64 | hashmap_remove(devices, d->sysfs); |
a7f241db | 65 | |
a1e58e8e | 66 | d->sysfs = mfree(d->sysfs); |
8fe914ec LP |
67 | } |
68 | ||
628c89cc LP |
69 | static int device_set_sysfs(Device *d, const char *sysfs) { |
70 | Device *first; | |
71 | char *copy; | |
72 | int r; | |
73 | ||
74 | assert(d); | |
75 | ||
76 | if (streq_ptr(d->sysfs, sysfs)) | |
77 | return 0; | |
78 | ||
79 | r = hashmap_ensure_allocated(&UNIT(d)->manager->devices_by_sysfs, &string_hash_ops); | |
80 | if (r < 0) | |
81 | return r; | |
82 | ||
83 | copy = strdup(sysfs); | |
84 | if (!copy) | |
85 | return -ENOMEM; | |
86 | ||
87 | device_unset_sysfs(d); | |
88 | ||
89 | first = hashmap_get(UNIT(d)->manager->devices_by_sysfs, sysfs); | |
90 | LIST_PREPEND(same_sysfs, first, d); | |
91 | ||
92 | r = hashmap_replace(UNIT(d)->manager->devices_by_sysfs, copy, first); | |
93 | if (r < 0) { | |
94 | LIST_REMOVE(same_sysfs, first, d); | |
95 | free(copy); | |
96 | return r; | |
97 | } | |
98 | ||
99 | d->sysfs = copy; | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
faf919f1 LP |
104 | static void device_init(Unit *u) { |
105 | Device *d = DEVICE(u); | |
106 | ||
107 | assert(d); | |
1124fe6f | 108 | assert(UNIT(d)->load_state == UNIT_STUB); |
faf919f1 | 109 | |
8fe914ec LP |
110 | /* In contrast to all other unit types we timeout jobs waiting |
111 | * for devices by default. This is because they otherwise wait | |
35b8ca3a | 112 | * indefinitely for plugged in devices, something which cannot |
8fe914ec LP |
113 | * happen for the other units since their operations time out |
114 | * anyway. */ | |
d9732d78 | 115 | u->job_running_timeout = u->manager->default_timeout_start_usec; |
c8f4d764 | 116 | |
f1421cc6 | 117 | u->ignore_on_isolate = true; |
faf919f1 LP |
118 | } |
119 | ||
87f0e418 LP |
120 | static void device_done(Unit *u) { |
121 | Device *d = DEVICE(u); | |
034c6ed7 LP |
122 | |
123 | assert(d); | |
e537352b | 124 | |
8fe914ec | 125 | device_unset_sysfs(d); |
e537352b LP |
126 | } |
127 | ||
f50e0a01 LP |
128 | static void device_set_state(Device *d, DeviceState state) { |
129 | DeviceState old_state; | |
130 | assert(d); | |
5cb5a6ff | 131 | |
f50e0a01 LP |
132 | old_state = d->state; |
133 | d->state = state; | |
5cb5a6ff | 134 | |
e537352b | 135 | if (state != old_state) |
f2341e0a | 136 | log_unit_debug(UNIT(d), "Changed %s -> %s", device_state_to_string(old_state), device_state_to_string(state)); |
f50e0a01 | 137 | |
e2f3b44c | 138 | unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state], true); |
f50e0a01 LP |
139 | } |
140 | ||
be847e82 | 141 | static int device_coldplug(Unit *u) { |
f50e0a01 LP |
142 | Device *d = DEVICE(u); |
143 | ||
144 | assert(d); | |
145 | assert(d->state == DEVICE_DEAD); | |
146 | ||
628c89cc LP |
147 | if (d->found & DEVICE_FOUND_UDEV) |
148 | /* If udev says the device is around, it's around */ | |
73608ed9 | 149 | device_set_state(d, DEVICE_PLUGGED); |
f6200941 | 150 | else if (d->found != DEVICE_NOT_FOUND && d->deserialized_state != DEVICE_PLUGGED) |
628c89cc | 151 | /* If a device is found in /proc/self/mountinfo or |
f6200941 LP |
152 | * /proc/swaps, and was not yet announced via udev, |
153 | * it's "tentatively" around. */ | |
628c89cc | 154 | device_set_state(d, DEVICE_TENTATIVE); |
f50e0a01 LP |
155 | |
156 | return 0; | |
157 | } | |
158 | ||
f6200941 LP |
159 | static int device_serialize(Unit *u, FILE *f, FDSet *fds) { |
160 | Device *d = DEVICE(u); | |
161 | ||
162 | assert(u); | |
163 | assert(f); | |
164 | assert(fds); | |
165 | ||
166 | unit_serialize_item(u, f, "state", device_state_to_string(d->state)); | |
0108f6ec DM |
167 | |
168 | return 0; | |
f6200941 LP |
169 | } |
170 | ||
171 | static int device_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
172 | Device *d = DEVICE(u); | |
173 | ||
174 | assert(u); | |
175 | assert(key); | |
176 | assert(value); | |
177 | assert(fds); | |
178 | ||
179 | if (streq(key, "state")) { | |
180 | DeviceState state; | |
181 | ||
182 | state = device_state_from_string(value); | |
183 | if (state < 0) | |
f2341e0a | 184 | log_unit_debug(u, "Failed to parse state value: %s", value); |
f6200941 LP |
185 | else |
186 | d->deserialized_state = state; | |
187 | } else | |
f2341e0a | 188 | log_unit_debug(u, "Unknown serialization key: %s", key); |
f6200941 LP |
189 | |
190 | return 0; | |
191 | } | |
192 | ||
f50e0a01 | 193 | static void device_dump(Unit *u, FILE *f, const char *prefix) { |
25ac040b | 194 | Device *d = DEVICE(u); |
5cb5a6ff | 195 | |
25ac040b | 196 | assert(d); |
5cb5a6ff LP |
197 | |
198 | fprintf(f, | |
25ac040b LP |
199 | "%sDevice State: %s\n" |
200 | "%sSysfs Path: %s\n", | |
a16e1123 | 201 | prefix, device_state_to_string(d->state), |
f50e0a01 LP |
202 | prefix, strna(d->sysfs)); |
203 | } | |
204 | ||
44a6b1b6 | 205 | _pure_ static UnitActiveState device_active_state(Unit *u) { |
f50e0a01 LP |
206 | assert(u); |
207 | ||
208 | return state_translation_table[DEVICE(u)->state]; | |
25ac040b LP |
209 | } |
210 | ||
44a6b1b6 | 211 | _pure_ static const char *device_sub_state_to_string(Unit *u) { |
10a94420 LP |
212 | assert(u); |
213 | ||
a16e1123 | 214 | return device_state_to_string(DEVICE(u)->state); |
10a94420 LP |
215 | } |
216 | ||
628c89cc | 217 | static int device_update_description(Unit *u, struct udev_device *dev, const char *path) { |
965e5c5d | 218 | const char *model; |
628c89cc | 219 | int r; |
965e5c5d LP |
220 | |
221 | assert(u); | |
222 | assert(dev); | |
223 | assert(path); | |
224 | ||
225 | model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE"); | |
226 | if (!model) | |
227 | model = udev_device_get_property_value(dev, "ID_MODEL"); | |
228 | ||
229 | if (model) { | |
230 | const char *label; | |
231 | ||
232 | /* Try to concatenate the device model string with a label, if there is one */ | |
233 | label = udev_device_get_property_value(dev, "ID_FS_LABEL"); | |
234 | if (!label) | |
235 | label = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME"); | |
236 | if (!label) | |
237 | label = udev_device_get_property_value(dev, "ID_PART_ENTRY_NUMBER"); | |
238 | ||
239 | if (label) { | |
240 | _cleanup_free_ char *j; | |
241 | ||
605405c6 | 242 | j = strjoin(model, " ", label); |
965e5c5d | 243 | if (j) |
628c89cc | 244 | r = unit_set_description(u, j); |
c43b2132 TA |
245 | else |
246 | r = -ENOMEM; | |
628c89cc LP |
247 | } else |
248 | r = unit_set_description(u, model); | |
249 | } else | |
250 | r = unit_set_description(u, path); | |
965e5c5d | 251 | |
628c89cc | 252 | if (r < 0) |
f2341e0a | 253 | log_unit_error_errno(u, r, "Failed to set device description: %m"); |
965e5c5d | 254 | |
628c89cc | 255 | return r; |
965e5c5d LP |
256 | } |
257 | ||
258 | static int device_add_udev_wants(Unit *u, struct udev_device *dev) { | |
ceed8f0c | 259 | const char *wants, *property, *p; |
965e5c5d LP |
260 | int r; |
261 | ||
262 | assert(u); | |
263 | assert(dev); | |
264 | ||
463d0d15 | 265 | property = MANAGER_IS_USER(u->manager) ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS"; |
b2fadec6 | 266 | wants = udev_device_get_property_value(dev, property); |
ceed8f0c ZJS |
267 | for (p = wants;;) { |
268 | _cleanup_free_ char *word = NULL, *k = NULL; | |
965e5c5d | 269 | |
ceed8f0c ZJS |
270 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
271 | if (r == 0) | |
272 | return 0; | |
273 | if (r == -ENOMEM) | |
274 | return log_oom(); | |
275 | if (r < 0) | |
276 | return log_unit_error_errno(u, r, "Failed to add parse %s: %m", property); | |
965e5c5d | 277 | |
ceed8f0c | 278 | r = unit_name_mangle(word, UNIT_NAME_NOGLOB, &k); |
7410616c | 279 | if (r < 0) |
ceed8f0c | 280 | return log_unit_error_errno(u, r, "Failed to mangle unit name \"%s\": %m", word); |
965e5c5d | 281 | |
ceed8f0c | 282 | r = unit_add_dependency_by_name(u, UNIT_WANTS, k, NULL, true); |
965e5c5d | 283 | if (r < 0) |
f2341e0a | 284 | return log_unit_error_errno(u, r, "Failed to add wants dependency: %m"); |
965e5c5d | 285 | } |
965e5c5d LP |
286 | } |
287 | ||
ebc8968b FB |
288 | static bool device_is_bound_by_mounts(Unit *d, struct udev_device *dev) { |
289 | const char *bound_by; | |
290 | int r = false; | |
291 | ||
292 | assert(d); | |
293 | assert(dev); | |
294 | ||
295 | bound_by = udev_device_get_property_value(dev, "SYSTEMD_MOUNT_DEVICE_BOUND"); | |
296 | if (bound_by) | |
297 | r = parse_boolean(bound_by) > 0; | |
298 | ||
299 | DEVICE(d)->bind_mounts = r; | |
300 | return r; | |
301 | } | |
302 | ||
303 | static int device_upgrade_mount_deps(Unit *u) { | |
304 | Unit *other; | |
305 | Iterator i; | |
306 | int r; | |
307 | ||
308 | SET_FOREACH(other, u->dependencies[UNIT_REQUIRED_BY], i) { | |
309 | if (other->type != UNIT_MOUNT) | |
310 | continue; | |
311 | ||
312 | r = unit_add_dependency(other, UNIT_BINDS_TO, u, true); | |
313 | if (r < 0) | |
314 | return r; | |
315 | } | |
316 | return 0; | |
317 | } | |
318 | ||
628c89cc LP |
319 | static int device_setup_unit(Manager *m, struct udev_device *dev, const char *path, bool main) { |
320 | _cleanup_free_ char *e = NULL; | |
2005219f | 321 | const char *sysfs = NULL; |
25ac040b | 322 | Unit *u = NULL; |
25ac040b | 323 | bool delete; |
965e5c5d | 324 | int r; |
25ac040b LP |
325 | |
326 | assert(m); | |
965e5c5d | 327 | assert(path); |
25ac040b | 328 | |
2005219f MP |
329 | if (dev) { |
330 | sysfs = udev_device_get_syspath(dev); | |
331 | if (!sysfs) | |
332 | return 0; | |
333 | } | |
7f275a9f | 334 | |
7410616c LP |
335 | r = unit_name_from_path(path, ".device", &e); |
336 | if (r < 0) | |
7f629b74 | 337 | return log_error_errno(r, "Failed to generate unit name from device path: %m"); |
628c89cc LP |
338 | |
339 | u = manager_get_unit(m, e); | |
25ac040b | 340 | |
ac9d396b FB |
341 | /* The device unit can still be present even if the device was |
342 | * unplugged: a mount unit can reference it hence preventing | |
343 | * the GC to have garbaged it. That's desired since the device | |
344 | * unit may have a dependency on the mount unit which was | |
345 | * added during the loading of the later. */ | |
51f21740 | 346 | if (dev && u && DEVICE(u)->state == DEVICE_PLUGGED) { |
ac9d396b FB |
347 | /* This unit is in plugged state: we're sure it's |
348 | * attached to a device. */ | |
349 | if (!path_equal(DEVICE(u)->sysfs, sysfs)) { | |
071e0b8b | 350 | log_unit_debug(u, "Dev %s appeared twice with different sysfs paths %s and %s", |
ac9d396b FB |
351 | e, DEVICE(u)->sysfs, sysfs); |
352 | return -EEXIST; | |
353 | } | |
628c89cc | 354 | } |
25ac040b | 355 | |
aab14b13 LP |
356 | if (!u) { |
357 | delete = true; | |
358 | ||
a581e45a | 359 | r = unit_new_for_name(m, sizeof(Device), e, &u); |
7d17cfbc | 360 | if (r < 0) |
25ac040b LP |
361 | goto fail; |
362 | ||
ee6cb288 LP |
363 | unit_add_to_load_queue(u); |
364 | } else | |
365 | delete = false; | |
366 | ||
367 | /* If this was created via some dependency and has not | |
368 | * actually been seen yet ->sysfs will not be | |
369 | * initialized. Hence initialize it if necessary. */ | |
2005219f MP |
370 | if (sysfs) { |
371 | r = device_set_sysfs(DEVICE(u), sysfs); | |
372 | if (r < 0) | |
373 | goto fail; | |
ee6cb288 | 374 | |
2005219f | 375 | (void) device_update_description(u, dev, path); |
aab14b13 | 376 | |
2005219f MP |
377 | /* The additional systemd udev properties we only interpret |
378 | * for the main object */ | |
379 | if (main) | |
380 | (void) device_add_udev_wants(u, dev); | |
381 | } | |
25ac040b | 382 | |
ebc8968b FB |
383 | /* So the user wants the mount units to be bound to the device but a |
384 | * mount unit might has been seen by systemd before the device appears | |
385 | * on its radar. In this case the device unit is partially initialized | |
386 | * and includes the deps on the mount unit but at that time the "bind | |
387 | * mounts" flag wasn't not present. Fix this up now. */ | |
d13489e2 | 388 | if (dev && device_is_bound_by_mounts(u, dev)) |
ebc8968b | 389 | device_upgrade_mount_deps(u); |
f94ea366 | 390 | |
f1421cc6 LP |
391 | /* Note that this won't dispatch the load queue, the caller |
392 | * has to do that if needed and appropriate */ | |
393 | ||
c1e1601e | 394 | unit_add_to_dbus_queue(u); |
25ac040b LP |
395 | return 0; |
396 | ||
397 | fail: | |
f2341e0a | 398 | log_unit_warning_errno(u, r, "Failed to set up device unit: %m"); |
ee5f3479 | 399 | |
c9d5c9c0 | 400 | if (delete) |
25ac040b | 401 | unit_free(u); |
ee5f3479 | 402 | |
25ac040b LP |
403 | return r; |
404 | } | |
405 | ||
628c89cc | 406 | static int device_process_new(Manager *m, struct udev_device *dev) { |
f1421cc6 | 407 | const char *sysfs, *dn, *alias; |
8fe914ec | 408 | struct udev_list_entry *item = NULL, *first = NULL; |
003ac9d0 | 409 | int r; |
8fe914ec LP |
410 | |
411 | assert(m); | |
412 | ||
718db961 LP |
413 | sysfs = udev_device_get_syspath(dev); |
414 | if (!sysfs) | |
f1421cc6 | 415 | return 0; |
8fe914ec LP |
416 | |
417 | /* Add the main unit named after the sysfs path */ | |
628c89cc | 418 | r = device_setup_unit(m, dev, sysfs, true); |
003ac9d0 HH |
419 | if (r < 0) |
420 | return r; | |
8fe914ec LP |
421 | |
422 | /* Add an additional unit for the device node */ | |
f1421cc6 LP |
423 | dn = udev_device_get_devnode(dev); |
424 | if (dn) | |
628c89cc | 425 | (void) device_setup_unit(m, dev, dn, false); |
8fe914ec LP |
426 | |
427 | /* Add additional units for all symlinks */ | |
428 | first = udev_device_get_devlinks_list_entry(dev); | |
429 | udev_list_entry_foreach(item, first) { | |
430 | const char *p; | |
5845b46b | 431 | struct stat st; |
8fe914ec LP |
432 | |
433 | /* Don't bother with the /dev/block links */ | |
434 | p = udev_list_entry_get_name(item); | |
435 | ||
436 | if (path_startswith(p, "/dev/block/") || | |
437 | path_startswith(p, "/dev/char/")) | |
438 | continue; | |
439 | ||
5845b46b LP |
440 | /* Verify that the symlink in the FS actually belongs |
441 | * to this device. This is useful to deal with | |
442 | * conflicting devices, e.g. when two disks want the | |
443 | * same /dev/disk/by-label/xxx link because they have | |
444 | * the same label. We want to make sure that the same | |
445 | * device that won the symlink wins in systemd, so we | |
ee33e53a | 446 | * check the device node major/minor */ |
5845b46b LP |
447 | if (stat(p, &st) >= 0) |
448 | if ((!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) || | |
449 | st.st_rdev != udev_device_get_devnum(dev)) | |
450 | continue; | |
451 | ||
628c89cc | 452 | (void) device_setup_unit(m, dev, p, false); |
8fe914ec LP |
453 | } |
454 | ||
f1421cc6 LP |
455 | /* Add additional units for all explicitly configured |
456 | * aliases */ | |
457 | alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS"); | |
ceed8f0c | 458 | for (;;) { |
8fb242ab | 459 | _cleanup_free_ char *word = NULL; |
8fe914ec | 460 | |
ceed8f0c ZJS |
461 | r = extract_first_word(&alias, &word, NULL, EXTRACT_QUOTES); |
462 | if (r == 0) | |
463 | return 0; | |
464 | if (r == -ENOMEM) | |
465 | return log_oom(); | |
466 | if (r < 0) | |
467 | return log_warning_errno(r, "Failed to add parse SYSTEMD_ALIAS for %s: %m", sysfs); | |
f1421cc6 | 468 | |
ceed8f0c ZJS |
469 | if (path_is_absolute(word)) |
470 | (void) device_setup_unit(m, dev, word, false); | |
471 | else | |
472 | log_warning("SYSTEMD_ALIAS for %s is not an absolute path, ignoring: %s", sysfs, word); | |
8fe914ec | 473 | } |
8fe914ec LP |
474 | } |
475 | ||
628c89cc | 476 | static void device_update_found_one(Device *d, bool add, DeviceFound found, bool now) { |
f6200941 | 477 | DeviceFound n, previous; |
628c89cc LP |
478 | |
479 | assert(d); | |
480 | ||
481 | n = add ? (d->found | found) : (d->found & ~found); | |
482 | if (n == d->found) | |
483 | return; | |
484 | ||
f6200941 | 485 | previous = d->found; |
628c89cc LP |
486 | d->found = n; |
487 | ||
f6200941 LP |
488 | if (!now) |
489 | return; | |
490 | ||
4b58153d LP |
491 | /* Didn't exist before, but does now? if so, generate a new invocation ID for it */ |
492 | if (previous == DEVICE_NOT_FOUND && d->found != DEVICE_NOT_FOUND) | |
493 | (void) unit_acquire_invocation_id(UNIT(d)); | |
494 | ||
f6200941 LP |
495 | if (d->found & DEVICE_FOUND_UDEV) |
496 | /* When the device is known to udev we consider it | |
497 | * plugged. */ | |
498 | device_set_state(d, DEVICE_PLUGGED); | |
499 | else if (d->found != DEVICE_NOT_FOUND && (previous & DEVICE_FOUND_UDEV) == 0) | |
500 | /* If the device has not been seen by udev yet, but is | |
501 | * now referenced by the kernel, then we assume the | |
502 | * kernel knows it now, and udev might soon too. */ | |
503 | device_set_state(d, DEVICE_TENTATIVE); | |
05e33aa1 | 504 | else { |
f6200941 LP |
505 | /* If nobody sees the device, or if the device was |
506 | * previously seen by udev and now is only referenced | |
507 | * from the kernel, then we consider the device is | |
508 | * gone, the kernel just hasn't noticed it yet. */ | |
05e33aa1 | 509 | |
f6200941 | 510 | device_set_state(d, DEVICE_DEAD); |
05e33aa1 MS |
511 | device_unset_sysfs(d); |
512 | } | |
513 | ||
628c89cc LP |
514 | } |
515 | ||
516 | static int device_update_found_by_sysfs(Manager *m, const char *sysfs, bool add, DeviceFound found, bool now) { | |
f1421cc6 | 517 | Device *d, *l; |
25ac040b LP |
518 | |
519 | assert(m); | |
628c89cc | 520 | assert(sysfs); |
25ac040b | 521 | |
628c89cc LP |
522 | if (found == DEVICE_NOT_FOUND) |
523 | return 0; | |
25ac040b | 524 | |
f1421cc6 LP |
525 | l = hashmap_get(m->devices_by_sysfs, sysfs); |
526 | LIST_FOREACH(same_sysfs, d, l) | |
628c89cc LP |
527 | device_update_found_one(d, add, found, now); |
528 | ||
529 | return 0; | |
25ac040b LP |
530 | } |
531 | ||
628c89cc LP |
532 | static int device_update_found_by_name(Manager *m, const char *path, bool add, DeviceFound found, bool now) { |
533 | _cleanup_free_ char *e = NULL; | |
534 | Unit *u; | |
7410616c | 535 | int r; |
f94ea366 LP |
536 | |
537 | assert(m); | |
628c89cc | 538 | assert(path); |
f94ea366 | 539 | |
628c89cc LP |
540 | if (found == DEVICE_NOT_FOUND) |
541 | return 0; | |
f94ea366 | 542 | |
7410616c LP |
543 | r = unit_name_from_path(path, ".device", &e); |
544 | if (r < 0) | |
545 | return log_error_errno(r, "Failed to generate unit name from device path: %m"); | |
f94ea366 | 546 | |
628c89cc LP |
547 | u = manager_get_unit(m, e); |
548 | if (!u) | |
549 | return 0; | |
550 | ||
551 | device_update_found_one(DEVICE(u), add, found, now); | |
f94ea366 LP |
552 | return 0; |
553 | } | |
554 | ||
f1421cc6 LP |
555 | static bool device_is_ready(struct udev_device *dev) { |
556 | const char *ready; | |
557 | ||
558 | assert(dev); | |
559 | ||
560 | ready = udev_device_get_property_value(dev, "SYSTEMD_READY"); | |
561 | if (!ready) | |
562 | return true; | |
563 | ||
564 | return parse_boolean(ready) != 0; | |
565 | } | |
566 | ||
a7f241db LP |
567 | static Unit *device_following(Unit *u) { |
568 | Device *d = DEVICE(u); | |
569 | Device *other, *first = NULL; | |
570 | ||
571 | assert(d); | |
572 | ||
ac155bb8 | 573 | if (startswith(u->id, "sys-")) |
a7f241db LP |
574 | return NULL; |
575 | ||
576 | /* Make everybody follow the unit that's named after the sysfs path */ | |
577 | for (other = d->same_sysfs_next; other; other = other->same_sysfs_next) | |
1124fe6f | 578 | if (startswith(UNIT(other)->id, "sys-")) |
a7f241db LP |
579 | return UNIT(other); |
580 | ||
581 | for (other = d->same_sysfs_prev; other; other = other->same_sysfs_prev) { | |
1124fe6f | 582 | if (startswith(UNIT(other)->id, "sys-")) |
a7f241db LP |
583 | return UNIT(other); |
584 | ||
585 | first = other; | |
586 | } | |
587 | ||
588 | return UNIT(first); | |
589 | } | |
590 | ||
f1421cc6 LP |
591 | static int device_following_set(Unit *u, Set **_set) { |
592 | Device *d = DEVICE(u), *other; | |
593 | Set *set; | |
6210e7fc LP |
594 | int r; |
595 | ||
596 | assert(d); | |
f1421cc6 | 597 | assert(_set); |
6210e7fc | 598 | |
f1421cc6 LP |
599 | if (LIST_JUST_US(same_sysfs, d)) { |
600 | *_set = NULL; | |
6210e7fc LP |
601 | return 0; |
602 | } | |
603 | ||
d5099efc | 604 | set = set_new(NULL); |
f1421cc6 | 605 | if (!set) |
6210e7fc LP |
606 | return -ENOMEM; |
607 | ||
f1421cc6 LP |
608 | LIST_FOREACH_AFTER(same_sysfs, other, d) { |
609 | r = set_put(set, other); | |
610 | if (r < 0) | |
6210e7fc | 611 | goto fail; |
f1421cc6 | 612 | } |
6210e7fc | 613 | |
f1421cc6 LP |
614 | LIST_FOREACH_BEFORE(same_sysfs, other, d) { |
615 | r = set_put(set, other); | |
616 | if (r < 0) | |
6210e7fc | 617 | goto fail; |
f1421cc6 | 618 | } |
6210e7fc | 619 | |
f1421cc6 | 620 | *_set = set; |
6210e7fc LP |
621 | return 1; |
622 | ||
623 | fail: | |
f1421cc6 | 624 | set_free(set); |
6210e7fc LP |
625 | return r; |
626 | } | |
627 | ||
25ac040b LP |
628 | static void device_shutdown(Manager *m) { |
629 | assert(m); | |
630 | ||
718db961 LP |
631 | m->udev_event_source = sd_event_source_unref(m->udev_event_source); |
632 | ||
a16e1123 | 633 | if (m->udev_monitor) { |
f94ea366 | 634 | udev_monitor_unref(m->udev_monitor); |
a16e1123 LP |
635 | m->udev_monitor = NULL; |
636 | } | |
f94ea366 | 637 | |
525d3cc7 | 638 | m->devices_by_sysfs = hashmap_free(m->devices_by_sysfs); |
25ac040b LP |
639 | } |
640 | ||
ba64af90 | 641 | static void device_enumerate(Manager *m) { |
718db961 | 642 | _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL; |
25ac040b | 643 | struct udev_list_entry *item = NULL, *first = NULL; |
718db961 | 644 | int r; |
25ac040b LP |
645 | |
646 | assert(m); | |
647 | ||
9670d583 | 648 | if (!m->udev_monitor) { |
718db961 LP |
649 | m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); |
650 | if (!m->udev_monitor) { | |
ba64af90 | 651 | log_oom(); |
a16e1123 LP |
652 | goto fail; |
653 | } | |
f94ea366 | 654 | |
47ae6e67 LP |
655 | /* This will fail if we are unprivileged, but that |
656 | * should not matter much, as user instances won't run | |
657 | * during boot. */ | |
8fa158e7 | 658 | (void) udev_monitor_set_receive_buffer_size(m->udev_monitor, 128*1024*1024); |
99448c1f | 659 | |
718db961 | 660 | r = udev_monitor_filter_add_match_tag(m->udev_monitor, "systemd"); |
ba64af90 LP |
661 | if (r < 0) { |
662 | log_error_errno(r, "Failed to add udev tag match: %m"); | |
e1ce2c27 | 663 | goto fail; |
ba64af90 | 664 | } |
e1ce2c27 | 665 | |
718db961 | 666 | r = udev_monitor_enable_receiving(m->udev_monitor); |
ba64af90 LP |
667 | if (r < 0) { |
668 | log_error_errno(r, "Failed to enable udev event reception: %m"); | |
a16e1123 | 669 | goto fail; |
ba64af90 | 670 | } |
f94ea366 | 671 | |
151b9b96 | 672 | r = sd_event_add_io(m->event, &m->udev_event_source, udev_monitor_get_fd(m->udev_monitor), EPOLLIN, device_dispatch_io, m); |
ba64af90 LP |
673 | if (r < 0) { |
674 | log_error_errno(r, "Failed to watch udev file descriptor: %m"); | |
718db961 | 675 | goto fail; |
ba64af90 | 676 | } |
7dfbe2e3 TG |
677 | |
678 | (void) sd_event_source_set_description(m->udev_event_source, "device"); | |
a16e1123 | 679 | } |
f94ea366 | 680 | |
718db961 LP |
681 | e = udev_enumerate_new(m->udev); |
682 | if (!e) { | |
ba64af90 | 683 | log_oom(); |
4f2d528d LP |
684 | goto fail; |
685 | } | |
718db961 LP |
686 | |
687 | r = udev_enumerate_add_match_tag(e, "systemd"); | |
ba64af90 LP |
688 | if (r < 0) { |
689 | log_error_errno(r, "Failed to create udev tag enumeration: %m"); | |
e1ce2c27 | 690 | goto fail; |
ba64af90 | 691 | } |
25ac040b | 692 | |
e1202047 | 693 | r = udev_enumerate_add_match_is_initialized(e); |
ba64af90 LP |
694 | if (r < 0) { |
695 | log_error_errno(r, "Failed to install initialization match into enumeration: %m"); | |
e1202047 | 696 | goto fail; |
ba64af90 | 697 | } |
e1202047 | 698 | |
718db961 | 699 | r = udev_enumerate_scan_devices(e); |
ba64af90 LP |
700 | if (r < 0) { |
701 | log_error_errno(r, "Failed to enumerate devices: %m"); | |
4f2d528d | 702 | goto fail; |
ba64af90 | 703 | } |
25ac040b | 704 | |
4f2d528d | 705 | first = udev_enumerate_get_list_entry(e); |
628c89cc LP |
706 | udev_list_entry_foreach(item, first) { |
707 | _cleanup_udev_device_unref_ struct udev_device *dev = NULL; | |
708 | const char *sysfs; | |
709 | ||
710 | sysfs = udev_list_entry_get_name(item); | |
711 | ||
712 | dev = udev_device_new_from_syspath(m->udev, sysfs); | |
713 | if (!dev) { | |
714 | log_oom(); | |
715 | continue; | |
716 | } | |
717 | ||
718 | if (!device_is_ready(dev)) | |
719 | continue; | |
720 | ||
721 | (void) device_process_new(m, dev); | |
722 | ||
723 | device_update_found_by_sysfs(m, sysfs, true, DEVICE_FOUND_UDEV, false); | |
724 | } | |
25ac040b | 725 | |
ba64af90 | 726 | return; |
25ac040b LP |
727 | |
728 | fail: | |
25ac040b | 729 | device_shutdown(m); |
5cb5a6ff LP |
730 | } |
731 | ||
718db961 LP |
732 | static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
733 | _cleanup_udev_device_unref_ struct udev_device *dev = NULL; | |
718db961 | 734 | Manager *m = userdata; |
628c89cc | 735 | const char *action, *sysfs; |
718db961 | 736 | int r; |
f94ea366 LP |
737 | |
738 | assert(m); | |
99448c1f | 739 | |
718db961 | 740 | if (revents != EPOLLIN) { |
99448c1f KS |
741 | static RATELIMIT_DEFINE(limit, 10*USEC_PER_SEC, 5); |
742 | ||
743 | if (!ratelimit_test(&limit)) | |
56f64d95 | 744 | log_error_errno(errno, "Failed to get udev event: %m"); |
718db961 LP |
745 | if (!(revents & EPOLLIN)) |
746 | return 0; | |
99448c1f | 747 | } |
f94ea366 | 748 | |
718db961 LP |
749 | /* |
750 | * libudev might filter-out devices which pass the bloom | |
751 | * filter, so getting NULL here is not necessarily an error. | |
752 | */ | |
753 | dev = udev_monitor_receive_device(m->udev_monitor); | |
754 | if (!dev) | |
755 | return 0; | |
f94ea366 | 756 | |
628c89cc LP |
757 | sysfs = udev_device_get_syspath(dev); |
758 | if (!sysfs) { | |
759 | log_error("Failed to get udev sys path."); | |
760 | return 0; | |
761 | } | |
762 | ||
718db961 LP |
763 | action = udev_device_get_action(dev); |
764 | if (!action) { | |
f94ea366 | 765 | log_error("Failed to get udev action string."); |
718db961 | 766 | return 0; |
f94ea366 LP |
767 | } |
768 | ||
628c89cc LP |
769 | if (streq(action, "remove")) { |
770 | r = swap_process_device_remove(m, dev); | |
9670d583 | 771 | if (r < 0) |
da927ba9 | 772 | log_error_errno(r, "Failed to process swap device remove event: %m"); |
9670d583 | 773 | |
628c89cc LP |
774 | /* If we get notified that a device was removed by |
775 | * udev, then it's completely gone, hence unset all | |
776 | * found bits */ | |
777 | device_update_found_by_sysfs(m, sysfs, false, DEVICE_FOUND_UDEV|DEVICE_FOUND_MOUNT|DEVICE_FOUND_SWAP, true); | |
f1421cc6 | 778 | |
628c89cc LP |
779 | } else if (device_is_ready(dev)) { |
780 | ||
781 | (void) device_process_new(m, dev); | |
782 | ||
783 | r = swap_process_device_new(m, dev); | |
9670d583 | 784 | if (r < 0) |
da927ba9 | 785 | log_error_errno(r, "Failed to process swap device new event: %m"); |
9670d583 | 786 | |
f1421cc6 LP |
787 | manager_dispatch_load_queue(m); |
788 | ||
628c89cc LP |
789 | /* The device is found now, set the udev found bit */ |
790 | device_update_found_by_sysfs(m, sysfs, true, DEVICE_FOUND_UDEV, true); | |
791 | ||
792 | } else { | |
793 | /* The device is nominally around, but not ready for | |
794 | * us. Hence unset the udev bit, but leave the rest | |
795 | * around. */ | |
796 | ||
797 | device_update_found_by_sysfs(m, sysfs, false, DEVICE_FOUND_UDEV, true); | |
f94ea366 LP |
798 | } |
799 | ||
718db961 | 800 | return 0; |
f94ea366 LP |
801 | } |
802 | ||
1c2e9646 | 803 | static bool device_supported(void) { |
0faacd47 | 804 | static int read_only = -1; |
0faacd47 LP |
805 | |
806 | /* If /sys is read-only we don't support device units, and any | |
807 | * attempts to start one should fail immediately. */ | |
808 | ||
809 | if (read_only < 0) | |
810 | read_only = path_is_read_only_fs("/sys"); | |
811 | ||
812 | return read_only <= 0; | |
813 | } | |
814 | ||
628c89cc LP |
815 | int device_found_node(Manager *m, const char *node, bool add, DeviceFound found, bool now) { |
816 | _cleanup_udev_device_unref_ struct udev_device *dev = NULL; | |
817 | struct stat st; | |
818 | ||
819 | assert(m); | |
820 | assert(node); | |
821 | ||
4c6d20de LP |
822 | if (!device_supported()) |
823 | return 0; | |
824 | ||
628c89cc LP |
825 | /* This is called whenever we find a device referenced in |
826 | * /proc/swaps or /proc/self/mounts. Such a device might be | |
827 | * mounted/enabled at a time where udev has not finished | |
828 | * probing it yet, and we thus haven't learned about it | |
829 | * yet. In this case we will set the device unit to | |
830 | * "tentative" state. */ | |
831 | ||
832 | if (add) { | |
833 | if (!path_startswith(node, "/dev")) | |
834 | return 0; | |
835 | ||
f6200941 LP |
836 | /* We make an extra check here, if the device node |
837 | * actually exists. If it's missing, then this is an | |
838 | * indication that device was unplugged but is still | |
839 | * referenced in /proc/swaps or | |
840 | * /proc/self/mountinfo. Note that this check doesn't | |
841 | * really cover all cases where a device might be gone | |
842 | * away, since drives that can have a medium inserted | |
843 | * will still have a device node even when the medium | |
844 | * is not there... */ | |
845 | ||
2005219f MP |
846 | if (stat(node, &st) >= 0) { |
847 | if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) | |
628c89cc LP |
848 | return 0; |
849 | ||
2005219f MP |
850 | dev = udev_device_new_from_devnum(m->udev, S_ISBLK(st.st_mode) ? 'b' : 'c', st.st_rdev); |
851 | if (!dev && errno != ENOENT) | |
852 | return log_error_errno(errno, "Failed to get udev device from devnum %u:%u: %m", major(st.st_rdev), minor(st.st_rdev)); | |
628c89cc | 853 | |
2005219f MP |
854 | } else if (errno != ENOENT) |
855 | return log_error_errno(errno, "Failed to stat device node file %s: %m", node); | |
628c89cc LP |
856 | |
857 | /* If the device is known in the kernel and newly | |
858 | * appeared, then we'll create a device unit for it, | |
859 | * under the name referenced in /proc/swaps or | |
860 | * /proc/self/mountinfo. */ | |
861 | ||
862 | (void) device_setup_unit(m, dev, node, false); | |
863 | } | |
864 | ||
865 | /* Update the device unit's state, should it exist */ | |
866 | return device_update_found_by_name(m, node, add, found, now); | |
867 | } | |
868 | ||
ebc8968b FB |
869 | bool device_shall_be_bound_by(Unit *device, Unit *u) { |
870 | ||
871 | if (u->type != UNIT_MOUNT) | |
872 | return false; | |
873 | ||
874 | return DEVICE(device)->bind_mounts; | |
875 | } | |
876 | ||
87f0e418 | 877 | const UnitVTable device_vtable = { |
7d17cfbc | 878 | .object_size = sizeof(Device), |
f975e971 LP |
879 | .sections = |
880 | "Unit\0" | |
881 | "Device\0" | |
882 | "Install\0", | |
5cb5a6ff | 883 | |
c5a97ed1 LP |
884 | .gc_jobs = true, |
885 | ||
faf919f1 | 886 | .init = device_init, |
034c6ed7 | 887 | .done = device_done, |
718db961 LP |
888 | .load = unit_load_fragment_and_dropin_optional, |
889 | ||
f50e0a01 LP |
890 | .coldplug = device_coldplug, |
891 | ||
f6200941 LP |
892 | .serialize = device_serialize, |
893 | .deserialize_item = device_deserialize_item, | |
894 | ||
5cb5a6ff LP |
895 | .dump = device_dump, |
896 | ||
f50e0a01 | 897 | .active_state = device_active_state, |
10a94420 | 898 | .sub_state_to_string = device_sub_state_to_string, |
25ac040b | 899 | |
718db961 | 900 | .bus_vtable = bus_device_vtable, |
4139c1b2 | 901 | |
a7f241db | 902 | .following = device_following, |
6210e7fc | 903 | .following_set = device_following_set, |
a7f241db | 904 | |
f50e0a01 | 905 | .enumerate = device_enumerate, |
c6918296 | 906 | .shutdown = device_shutdown, |
0faacd47 | 907 | .supported = device_supported, |
c6918296 MS |
908 | |
909 | .status_message_formats = { | |
910 | .starting_stopping = { | |
911 | [0] = "Expecting device %s...", | |
912 | }, | |
913 | .finished_start_job = { | |
914 | [JOB_DONE] = "Found device %s.", | |
915 | [JOB_TIMEOUT] = "Timed out waiting for device %s.", | |
916 | }, | |
917 | }, | |
5cb5a6ff | 918 | }; |