]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/device.c
Merge pull request #5009 from ian-kelling/ian-mnt-namespace-doc
[thirdparty/systemd.git] / src / core / device.c
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
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
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
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <sys/epoll.h>
22
23 #include "libudev.h"
24
25 #include "alloc-util.h"
26 #include "dbus-device.h"
27 #include "device.h"
28 #include "log.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "stat-util.h"
32 #include "string-util.h"
33 #include "swap.h"
34 #include "udev-util.h"
35 #include "unit-name.h"
36 #include "unit.h"
37
38 static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = {
39 [DEVICE_DEAD] = UNIT_INACTIVE,
40 [DEVICE_TENTATIVE] = UNIT_ACTIVATING,
41 [DEVICE_PLUGGED] = UNIT_ACTIVE,
42 };
43
44 static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
45
46 static void device_unset_sysfs(Device *d) {
47 Hashmap *devices;
48 Device *first;
49
50 assert(d);
51
52 if (!d->sysfs)
53 return;
54
55 /* Remove this unit from the chain of devices which share the
56 * same sysfs path. */
57 devices = UNIT(d)->manager->devices_by_sysfs;
58 first = hashmap_get(devices, d->sysfs);
59 LIST_REMOVE(same_sysfs, first, d);
60
61 if (first)
62 hashmap_remove_and_replace(devices, d->sysfs, first->sysfs, first);
63 else
64 hashmap_remove(devices, d->sysfs);
65
66 d->sysfs = mfree(d->sysfs);
67 }
68
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
104 static void device_init(Unit *u) {
105 Device *d = DEVICE(u);
106
107 assert(d);
108 assert(UNIT(d)->load_state == UNIT_STUB);
109
110 /* In contrast to all other unit types we timeout jobs waiting
111 * for devices by default. This is because they otherwise wait
112 * indefinitely for plugged in devices, something which cannot
113 * happen for the other units since their operations time out
114 * anyway. */
115 u->job_timeout = u->manager->default_timeout_start_usec;
116
117 u->ignore_on_isolate = true;
118 }
119
120 static void device_done(Unit *u) {
121 Device *d = DEVICE(u);
122
123 assert(d);
124
125 device_unset_sysfs(d);
126 }
127
128 static void device_set_state(Device *d, DeviceState state) {
129 DeviceState old_state;
130 assert(d);
131
132 old_state = d->state;
133 d->state = state;
134
135 if (state != old_state)
136 log_unit_debug(UNIT(d), "Changed %s -> %s", device_state_to_string(old_state), device_state_to_string(state));
137
138 unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state], true);
139 }
140
141 static int device_coldplug(Unit *u) {
142 Device *d = DEVICE(u);
143
144 assert(d);
145 assert(d->state == DEVICE_DEAD);
146
147 if (d->found & DEVICE_FOUND_UDEV)
148 /* If udev says the device is around, it's around */
149 device_set_state(d, DEVICE_PLUGGED);
150 else if (d->found != DEVICE_NOT_FOUND && d->deserialized_state != DEVICE_PLUGGED)
151 /* If a device is found in /proc/self/mountinfo or
152 * /proc/swaps, and was not yet announced via udev,
153 * it's "tentatively" around. */
154 device_set_state(d, DEVICE_TENTATIVE);
155
156 return 0;
157 }
158
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));
167
168 return 0;
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)
184 log_unit_debug(u, "Failed to parse state value: %s", value);
185 else
186 d->deserialized_state = state;
187 } else
188 log_unit_debug(u, "Unknown serialization key: %s", key);
189
190 return 0;
191 }
192
193 static void device_dump(Unit *u, FILE *f, const char *prefix) {
194 Device *d = DEVICE(u);
195
196 assert(d);
197
198 fprintf(f,
199 "%sDevice State: %s\n"
200 "%sSysfs Path: %s\n",
201 prefix, device_state_to_string(d->state),
202 prefix, strna(d->sysfs));
203 }
204
205 _pure_ static UnitActiveState device_active_state(Unit *u) {
206 assert(u);
207
208 return state_translation_table[DEVICE(u)->state];
209 }
210
211 _pure_ static const char *device_sub_state_to_string(Unit *u) {
212 assert(u);
213
214 return device_state_to_string(DEVICE(u)->state);
215 }
216
217 static int device_update_description(Unit *u, struct udev_device *dev, const char *path) {
218 const char *model;
219 int r;
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
242 j = strjoin(model, " ", label);
243 if (j)
244 r = unit_set_description(u, j);
245 else
246 r = -ENOMEM;
247 } else
248 r = unit_set_description(u, model);
249 } else
250 r = unit_set_description(u, path);
251
252 if (r < 0)
253 log_unit_error_errno(u, r, "Failed to set device description: %m");
254
255 return r;
256 }
257
258 static int device_add_udev_wants(Unit *u, struct udev_device *dev) {
259 const char *wants, *property, *p;
260 int r;
261
262 assert(u);
263 assert(dev);
264
265 property = MANAGER_IS_USER(u->manager) ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS";
266 wants = udev_device_get_property_value(dev, property);
267 for (p = wants;;) {
268 _cleanup_free_ char *word = NULL, *k = NULL;
269
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);
277
278 r = unit_name_mangle(word, UNIT_NAME_NOGLOB, &k);
279 if (r < 0)
280 return log_unit_error_errno(u, r, "Failed to mangle unit name \"%s\": %m", word);
281
282 r = unit_add_dependency_by_name(u, UNIT_WANTS, k, NULL, true);
283 if (r < 0)
284 return log_unit_error_errno(u, r, "Failed to add wants dependency: %m");
285 }
286 }
287
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
319 static int device_setup_unit(Manager *m, struct udev_device *dev, const char *path, bool main) {
320 _cleanup_free_ char *e = NULL;
321 const char *sysfs = NULL;
322 Unit *u = NULL;
323 bool delete;
324 int r;
325
326 assert(m);
327 assert(path);
328
329 if (dev) {
330 sysfs = udev_device_get_syspath(dev);
331 if (!sysfs)
332 return 0;
333 }
334
335 r = unit_name_from_path(path, ".device", &e);
336 if (r < 0)
337 return log_error_errno(r, "Failed to generate unit name from device path: %m");
338
339 u = manager_get_unit(m, e);
340
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. */
346 if (dev && u && DEVICE(u)->state == DEVICE_PLUGGED) {
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)) {
350 log_unit_debug(u, "Dev %s appeared twice with different sysfs paths %s and %s",
351 e, DEVICE(u)->sysfs, sysfs);
352 return -EEXIST;
353 }
354 }
355
356 if (!u) {
357 delete = true;
358
359 r = unit_new_for_name(m, sizeof(Device), e, &u);
360 if (r < 0)
361 goto fail;
362
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. */
370 if (sysfs) {
371 r = device_set_sysfs(DEVICE(u), sysfs);
372 if (r < 0)
373 goto fail;
374
375 (void) device_update_description(u, dev, path);
376
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 }
382
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. */
388 if (dev && device_is_bound_by_mounts(u, dev))
389 device_upgrade_mount_deps(u);
390
391 /* Note that this won't dispatch the load queue, the caller
392 * has to do that if needed and appropriate */
393
394 unit_add_to_dbus_queue(u);
395 return 0;
396
397 fail:
398 log_unit_warning_errno(u, r, "Failed to set up device unit: %m");
399
400 if (delete)
401 unit_free(u);
402
403 return r;
404 }
405
406 static int device_process_new(Manager *m, struct udev_device *dev) {
407 const char *sysfs, *dn, *alias;
408 struct udev_list_entry *item = NULL, *first = NULL;
409 int r;
410
411 assert(m);
412
413 sysfs = udev_device_get_syspath(dev);
414 if (!sysfs)
415 return 0;
416
417 /* Add the main unit named after the sysfs path */
418 r = device_setup_unit(m, dev, sysfs, true);
419 if (r < 0)
420 return r;
421
422 /* Add an additional unit for the device node */
423 dn = udev_device_get_devnode(dev);
424 if (dn)
425 (void) device_setup_unit(m, dev, dn, false);
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;
431 struct stat st;
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
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
446 * check the device node major/minor */
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
452 (void) device_setup_unit(m, dev, p, false);
453 }
454
455 /* Add additional units for all explicitly configured
456 * aliases */
457 alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS");
458 for (;;) {
459 _cleanup_free_ char *word = NULL;
460
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);
468
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);
473 }
474 }
475
476 static void device_update_found_one(Device *d, bool add, DeviceFound found, bool now) {
477 DeviceFound n, previous;
478
479 assert(d);
480
481 n = add ? (d->found | found) : (d->found & ~found);
482 if (n == d->found)
483 return;
484
485 previous = d->found;
486 d->found = n;
487
488 if (!now)
489 return;
490
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
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);
504 else
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. */
509 device_set_state(d, DEVICE_DEAD);
510 }
511
512 static int device_update_found_by_sysfs(Manager *m, const char *sysfs, bool add, DeviceFound found, bool now) {
513 Device *d, *l;
514
515 assert(m);
516 assert(sysfs);
517
518 if (found == DEVICE_NOT_FOUND)
519 return 0;
520
521 l = hashmap_get(m->devices_by_sysfs, sysfs);
522 LIST_FOREACH(same_sysfs, d, l)
523 device_update_found_one(d, add, found, now);
524
525 return 0;
526 }
527
528 static int device_update_found_by_name(Manager *m, const char *path, bool add, DeviceFound found, bool now) {
529 _cleanup_free_ char *e = NULL;
530 Unit *u;
531 int r;
532
533 assert(m);
534 assert(path);
535
536 if (found == DEVICE_NOT_FOUND)
537 return 0;
538
539 r = unit_name_from_path(path, ".device", &e);
540 if (r < 0)
541 return log_error_errno(r, "Failed to generate unit name from device path: %m");
542
543 u = manager_get_unit(m, e);
544 if (!u)
545 return 0;
546
547 device_update_found_one(DEVICE(u), add, found, now);
548 return 0;
549 }
550
551 static bool device_is_ready(struct udev_device *dev) {
552 const char *ready;
553
554 assert(dev);
555
556 ready = udev_device_get_property_value(dev, "SYSTEMD_READY");
557 if (!ready)
558 return true;
559
560 return parse_boolean(ready) != 0;
561 }
562
563 static Unit *device_following(Unit *u) {
564 Device *d = DEVICE(u);
565 Device *other, *first = NULL;
566
567 assert(d);
568
569 if (startswith(u->id, "sys-"))
570 return NULL;
571
572 /* Make everybody follow the unit that's named after the sysfs path */
573 for (other = d->same_sysfs_next; other; other = other->same_sysfs_next)
574 if (startswith(UNIT(other)->id, "sys-"))
575 return UNIT(other);
576
577 for (other = d->same_sysfs_prev; other; other = other->same_sysfs_prev) {
578 if (startswith(UNIT(other)->id, "sys-"))
579 return UNIT(other);
580
581 first = other;
582 }
583
584 return UNIT(first);
585 }
586
587 static int device_following_set(Unit *u, Set **_set) {
588 Device *d = DEVICE(u), *other;
589 Set *set;
590 int r;
591
592 assert(d);
593 assert(_set);
594
595 if (LIST_JUST_US(same_sysfs, d)) {
596 *_set = NULL;
597 return 0;
598 }
599
600 set = set_new(NULL);
601 if (!set)
602 return -ENOMEM;
603
604 LIST_FOREACH_AFTER(same_sysfs, other, d) {
605 r = set_put(set, other);
606 if (r < 0)
607 goto fail;
608 }
609
610 LIST_FOREACH_BEFORE(same_sysfs, other, d) {
611 r = set_put(set, other);
612 if (r < 0)
613 goto fail;
614 }
615
616 *_set = set;
617 return 1;
618
619 fail:
620 set_free(set);
621 return r;
622 }
623
624 static void device_shutdown(Manager *m) {
625 assert(m);
626
627 m->udev_event_source = sd_event_source_unref(m->udev_event_source);
628
629 if (m->udev_monitor) {
630 udev_monitor_unref(m->udev_monitor);
631 m->udev_monitor = NULL;
632 }
633
634 m->devices_by_sysfs = hashmap_free(m->devices_by_sysfs);
635 }
636
637 static void device_enumerate(Manager *m) {
638 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
639 struct udev_list_entry *item = NULL, *first = NULL;
640 int r;
641
642 assert(m);
643
644 if (!m->udev_monitor) {
645 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
646 if (!m->udev_monitor) {
647 log_oom();
648 goto fail;
649 }
650
651 /* This will fail if we are unprivileged, but that
652 * should not matter much, as user instances won't run
653 * during boot. */
654 (void) udev_monitor_set_receive_buffer_size(m->udev_monitor, 128*1024*1024);
655
656 r = udev_monitor_filter_add_match_tag(m->udev_monitor, "systemd");
657 if (r < 0) {
658 log_error_errno(r, "Failed to add udev tag match: %m");
659 goto fail;
660 }
661
662 r = udev_monitor_enable_receiving(m->udev_monitor);
663 if (r < 0) {
664 log_error_errno(r, "Failed to enable udev event reception: %m");
665 goto fail;
666 }
667
668 r = sd_event_add_io(m->event, &m->udev_event_source, udev_monitor_get_fd(m->udev_monitor), EPOLLIN, device_dispatch_io, m);
669 if (r < 0) {
670 log_error_errno(r, "Failed to watch udev file descriptor: %m");
671 goto fail;
672 }
673
674 (void) sd_event_source_set_description(m->udev_event_source, "device");
675 }
676
677 e = udev_enumerate_new(m->udev);
678 if (!e) {
679 log_oom();
680 goto fail;
681 }
682
683 r = udev_enumerate_add_match_tag(e, "systemd");
684 if (r < 0) {
685 log_error_errno(r, "Failed to create udev tag enumeration: %m");
686 goto fail;
687 }
688
689 r = udev_enumerate_add_match_is_initialized(e);
690 if (r < 0) {
691 log_error_errno(r, "Failed to install initialization match into enumeration: %m");
692 goto fail;
693 }
694
695 r = udev_enumerate_scan_devices(e);
696 if (r < 0) {
697 log_error_errno(r, "Failed to enumerate devices: %m");
698 goto fail;
699 }
700
701 first = udev_enumerate_get_list_entry(e);
702 udev_list_entry_foreach(item, first) {
703 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
704 const char *sysfs;
705
706 sysfs = udev_list_entry_get_name(item);
707
708 dev = udev_device_new_from_syspath(m->udev, sysfs);
709 if (!dev) {
710 log_oom();
711 continue;
712 }
713
714 if (!device_is_ready(dev))
715 continue;
716
717 (void) device_process_new(m, dev);
718
719 device_update_found_by_sysfs(m, sysfs, true, DEVICE_FOUND_UDEV, false);
720 }
721
722 return;
723
724 fail:
725 device_shutdown(m);
726 }
727
728 static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
729 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
730 Manager *m = userdata;
731 const char *action, *sysfs;
732 int r;
733
734 assert(m);
735
736 if (revents != EPOLLIN) {
737 static RATELIMIT_DEFINE(limit, 10*USEC_PER_SEC, 5);
738
739 if (!ratelimit_test(&limit))
740 log_error_errno(errno, "Failed to get udev event: %m");
741 if (!(revents & EPOLLIN))
742 return 0;
743 }
744
745 /*
746 * libudev might filter-out devices which pass the bloom
747 * filter, so getting NULL here is not necessarily an error.
748 */
749 dev = udev_monitor_receive_device(m->udev_monitor);
750 if (!dev)
751 return 0;
752
753 sysfs = udev_device_get_syspath(dev);
754 if (!sysfs) {
755 log_error("Failed to get udev sys path.");
756 return 0;
757 }
758
759 action = udev_device_get_action(dev);
760 if (!action) {
761 log_error("Failed to get udev action string.");
762 return 0;
763 }
764
765 if (streq(action, "remove")) {
766 r = swap_process_device_remove(m, dev);
767 if (r < 0)
768 log_error_errno(r, "Failed to process swap device remove event: %m");
769
770 /* If we get notified that a device was removed by
771 * udev, then it's completely gone, hence unset all
772 * found bits */
773 device_update_found_by_sysfs(m, sysfs, false, DEVICE_FOUND_UDEV|DEVICE_FOUND_MOUNT|DEVICE_FOUND_SWAP, true);
774
775 } else if (device_is_ready(dev)) {
776
777 (void) device_process_new(m, dev);
778
779 r = swap_process_device_new(m, dev);
780 if (r < 0)
781 log_error_errno(r, "Failed to process swap device new event: %m");
782
783 manager_dispatch_load_queue(m);
784
785 /* The device is found now, set the udev found bit */
786 device_update_found_by_sysfs(m, sysfs, true, DEVICE_FOUND_UDEV, true);
787
788 } else {
789 /* The device is nominally around, but not ready for
790 * us. Hence unset the udev bit, but leave the rest
791 * around. */
792
793 device_update_found_by_sysfs(m, sysfs, false, DEVICE_FOUND_UDEV, true);
794 }
795
796 return 0;
797 }
798
799 static bool device_supported(void) {
800 static int read_only = -1;
801
802 /* If /sys is read-only we don't support device units, and any
803 * attempts to start one should fail immediately. */
804
805 if (read_only < 0)
806 read_only = path_is_read_only_fs("/sys");
807
808 return read_only <= 0;
809 }
810
811 int device_found_node(Manager *m, const char *node, bool add, DeviceFound found, bool now) {
812 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
813 struct stat st;
814
815 assert(m);
816 assert(node);
817
818 if (!device_supported())
819 return 0;
820
821 /* This is called whenever we find a device referenced in
822 * /proc/swaps or /proc/self/mounts. Such a device might be
823 * mounted/enabled at a time where udev has not finished
824 * probing it yet, and we thus haven't learned about it
825 * yet. In this case we will set the device unit to
826 * "tentative" state. */
827
828 if (add) {
829 if (!path_startswith(node, "/dev"))
830 return 0;
831
832 /* We make an extra check here, if the device node
833 * actually exists. If it's missing, then this is an
834 * indication that device was unplugged but is still
835 * referenced in /proc/swaps or
836 * /proc/self/mountinfo. Note that this check doesn't
837 * really cover all cases where a device might be gone
838 * away, since drives that can have a medium inserted
839 * will still have a device node even when the medium
840 * is not there... */
841
842 if (stat(node, &st) >= 0) {
843 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
844 return 0;
845
846 dev = udev_device_new_from_devnum(m->udev, S_ISBLK(st.st_mode) ? 'b' : 'c', st.st_rdev);
847 if (!dev && errno != ENOENT)
848 return log_error_errno(errno, "Failed to get udev device from devnum %u:%u: %m", major(st.st_rdev), minor(st.st_rdev));
849
850 } else if (errno != ENOENT)
851 return log_error_errno(errno, "Failed to stat device node file %s: %m", node);
852
853 /* If the device is known in the kernel and newly
854 * appeared, then we'll create a device unit for it,
855 * under the name referenced in /proc/swaps or
856 * /proc/self/mountinfo. */
857
858 (void) device_setup_unit(m, dev, node, false);
859 }
860
861 /* Update the device unit's state, should it exist */
862 return device_update_found_by_name(m, node, add, found, now);
863 }
864
865 bool device_shall_be_bound_by(Unit *device, Unit *u) {
866
867 if (u->type != UNIT_MOUNT)
868 return false;
869
870 return DEVICE(device)->bind_mounts;
871 }
872
873 const UnitVTable device_vtable = {
874 .object_size = sizeof(Device),
875 .sections =
876 "Unit\0"
877 "Device\0"
878 "Install\0",
879
880 .gc_jobs = true,
881
882 .init = device_init,
883 .done = device_done,
884 .load = unit_load_fragment_and_dropin_optional,
885
886 .coldplug = device_coldplug,
887
888 .serialize = device_serialize,
889 .deserialize_item = device_deserialize_item,
890
891 .dump = device_dump,
892
893 .active_state = device_active_state,
894 .sub_state_to_string = device_sub_state_to_string,
895
896 .bus_vtable = bus_device_vtable,
897
898 .following = device_following,
899 .following_set = device_following_set,
900
901 .enumerate = device_enumerate,
902 .shutdown = device_shutdown,
903 .supported = device_supported,
904
905 .status_message_formats = {
906 .starting_stopping = {
907 [0] = "Expecting device %s...",
908 },
909 .finished_start_job = {
910 [JOB_DONE] = "Found device %s.",
911 [JOB_TIMEOUT] = "Timed out waiting for device %s.",
912 },
913 },
914 };