]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/device.c
8a3e888e5eb9e3bbbf3f0bd3c393b69693c68a55
[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, NULL);
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;
260 const char *word, *state;
261 size_t l;
262 int r;
263 const char *property;
264
265 assert(u);
266 assert(dev);
267
268 property = MANAGER_IS_USER(u->manager) ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS";
269 wants = udev_device_get_property_value(dev, property);
270 if (!wants)
271 return 0;
272
273 FOREACH_WORD_QUOTED(word, l, wants, state) {
274 _cleanup_free_ char *n = NULL;
275 char e[l+1];
276
277 memcpy(e, word, l);
278 e[l] = 0;
279
280 r = unit_name_mangle(e, UNIT_NAME_NOGLOB, &n);
281 if (r < 0)
282 return log_unit_error_errno(u, r, "Failed to mangle unit name: %m");
283
284 r = unit_add_dependency_by_name(u, UNIT_WANTS, n, NULL, true);
285 if (r < 0)
286 return log_unit_error_errno(u, r, "Failed to add wants dependency: %m");
287 }
288 if (!isempty(state))
289 log_unit_warning(u, "Property %s on %s has trailing garbage, ignoring.", property, strna(udev_device_get_syspath(dev)));
290
291 return 0;
292 }
293
294 static int device_setup_unit(Manager *m, struct udev_device *dev, const char *path, bool main) {
295 _cleanup_free_ char *e = NULL;
296 const char *sysfs = NULL;
297 Unit *u = NULL;
298 bool delete;
299 int r;
300
301 assert(m);
302 assert(path);
303
304 if (dev) {
305 sysfs = udev_device_get_syspath(dev);
306 if (!sysfs)
307 return 0;
308 }
309
310 r = unit_name_from_path(path, ".device", &e);
311 if (r < 0)
312 return log_error_errno(r, "Failed to generate unit name from device path: %m");
313
314 u = manager_get_unit(m, e);
315
316 /* The device unit can still be present even if the device was
317 * unplugged: a mount unit can reference it hence preventing
318 * the GC to have garbaged it. That's desired since the device
319 * unit may have a dependency on the mount unit which was
320 * added during the loading of the later. */
321 if (dev && u && DEVICE(u)->state == DEVICE_PLUGGED) {
322 /* This unit is in plugged state: we're sure it's
323 * attached to a device. */
324 if (!path_equal(DEVICE(u)->sysfs, sysfs)) {
325 log_unit_debug(u, "Dev %s appeared twice with different sysfs paths %s and %s",
326 e, DEVICE(u)->sysfs, sysfs);
327 return -EEXIST;
328 }
329 }
330
331 if (!u) {
332 delete = true;
333
334 u = unit_new(m, sizeof(Device));
335 if (!u)
336 return log_oom();
337
338 r = unit_add_name(u, e);
339 if (r < 0)
340 goto fail;
341
342 unit_add_to_load_queue(u);
343 } else
344 delete = false;
345
346 /* If this was created via some dependency and has not
347 * actually been seen yet ->sysfs will not be
348 * initialized. Hence initialize it if necessary. */
349 if (sysfs) {
350 r = device_set_sysfs(DEVICE(u), sysfs);
351 if (r < 0)
352 goto fail;
353
354 (void) device_update_description(u, dev, path);
355
356 /* The additional systemd udev properties we only interpret
357 * for the main object */
358 if (main)
359 (void) device_add_udev_wants(u, dev);
360 }
361
362
363 /* Note that this won't dispatch the load queue, the caller
364 * has to do that if needed and appropriate */
365
366 unit_add_to_dbus_queue(u);
367 return 0;
368
369 fail:
370 log_unit_warning_errno(u, r, "Failed to set up device unit: %m");
371
372 if (delete)
373 unit_free(u);
374
375 return r;
376 }
377
378 static int device_process_new(Manager *m, struct udev_device *dev) {
379 const char *sysfs, *dn, *alias;
380 struct udev_list_entry *item = NULL, *first = NULL;
381 int r;
382
383 assert(m);
384
385 sysfs = udev_device_get_syspath(dev);
386 if (!sysfs)
387 return 0;
388
389 /* Add the main unit named after the sysfs path */
390 r = device_setup_unit(m, dev, sysfs, true);
391 if (r < 0)
392 return r;
393
394 /* Add an additional unit for the device node */
395 dn = udev_device_get_devnode(dev);
396 if (dn)
397 (void) device_setup_unit(m, dev, dn, false);
398
399 /* Add additional units for all symlinks */
400 first = udev_device_get_devlinks_list_entry(dev);
401 udev_list_entry_foreach(item, first) {
402 const char *p;
403 struct stat st;
404
405 /* Don't bother with the /dev/block links */
406 p = udev_list_entry_get_name(item);
407
408 if (path_startswith(p, "/dev/block/") ||
409 path_startswith(p, "/dev/char/"))
410 continue;
411
412 /* Verify that the symlink in the FS actually belongs
413 * to this device. This is useful to deal with
414 * conflicting devices, e.g. when two disks want the
415 * same /dev/disk/by-label/xxx link because they have
416 * the same label. We want to make sure that the same
417 * device that won the symlink wins in systemd, so we
418 * check the device node major/minor */
419 if (stat(p, &st) >= 0)
420 if ((!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) ||
421 st.st_rdev != udev_device_get_devnum(dev))
422 continue;
423
424 (void) device_setup_unit(m, dev, p, false);
425 }
426
427 /* Add additional units for all explicitly configured
428 * aliases */
429 alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS");
430 if (alias) {
431 const char *word, *state;
432 size_t l;
433
434 FOREACH_WORD_QUOTED(word, l, alias, state) {
435 char e[l+1];
436
437 memcpy(e, word, l);
438 e[l] = 0;
439
440 if (path_is_absolute(e))
441 (void) device_setup_unit(m, dev, e, false);
442 else
443 log_warning("SYSTEMD_ALIAS for %s is not an absolute path, ignoring: %s", sysfs, e);
444 }
445 if (!isempty(state))
446 log_warning("SYSTEMD_ALIAS for %s has trailing garbage, ignoring.", sysfs);
447 }
448
449 return 0;
450 }
451
452 static void device_update_found_one(Device *d, bool add, DeviceFound found, bool now) {
453 DeviceFound n, previous;
454
455 assert(d);
456
457 n = add ? (d->found | found) : (d->found & ~found);
458 if (n == d->found)
459 return;
460
461 previous = d->found;
462 d->found = n;
463
464 if (!now)
465 return;
466
467 /* Didn't exist before, but does now? if so, generate a new invocation ID for it */
468 if (previous == DEVICE_NOT_FOUND && d->found != DEVICE_NOT_FOUND)
469 (void) unit_acquire_invocation_id(UNIT(d));
470
471 if (d->found & DEVICE_FOUND_UDEV)
472 /* When the device is known to udev we consider it
473 * plugged. */
474 device_set_state(d, DEVICE_PLUGGED);
475 else if (d->found != DEVICE_NOT_FOUND && (previous & DEVICE_FOUND_UDEV) == 0)
476 /* If the device has not been seen by udev yet, but is
477 * now referenced by the kernel, then we assume the
478 * kernel knows it now, and udev might soon too. */
479 device_set_state(d, DEVICE_TENTATIVE);
480 else
481 /* If nobody sees the device, or if the device was
482 * previously seen by udev and now is only referenced
483 * from the kernel, then we consider the device is
484 * gone, the kernel just hasn't noticed it yet. */
485 device_set_state(d, DEVICE_DEAD);
486 }
487
488 static int device_update_found_by_sysfs(Manager *m, const char *sysfs, bool add, DeviceFound found, bool now) {
489 Device *d, *l;
490
491 assert(m);
492 assert(sysfs);
493
494 if (found == DEVICE_NOT_FOUND)
495 return 0;
496
497 l = hashmap_get(m->devices_by_sysfs, sysfs);
498 LIST_FOREACH(same_sysfs, d, l)
499 device_update_found_one(d, add, found, now);
500
501 return 0;
502 }
503
504 static int device_update_found_by_name(Manager *m, const char *path, bool add, DeviceFound found, bool now) {
505 _cleanup_free_ char *e = NULL;
506 Unit *u;
507 int r;
508
509 assert(m);
510 assert(path);
511
512 if (found == DEVICE_NOT_FOUND)
513 return 0;
514
515 r = unit_name_from_path(path, ".device", &e);
516 if (r < 0)
517 return log_error_errno(r, "Failed to generate unit name from device path: %m");
518
519 u = manager_get_unit(m, e);
520 if (!u)
521 return 0;
522
523 device_update_found_one(DEVICE(u), add, found, now);
524 return 0;
525 }
526
527 static bool device_is_ready(struct udev_device *dev) {
528 const char *ready;
529
530 assert(dev);
531
532 ready = udev_device_get_property_value(dev, "SYSTEMD_READY");
533 if (!ready)
534 return true;
535
536 return parse_boolean(ready) != 0;
537 }
538
539 static Unit *device_following(Unit *u) {
540 Device *d = DEVICE(u);
541 Device *other, *first = NULL;
542
543 assert(d);
544
545 if (startswith(u->id, "sys-"))
546 return NULL;
547
548 /* Make everybody follow the unit that's named after the sysfs path */
549 for (other = d->same_sysfs_next; other; other = other->same_sysfs_next)
550 if (startswith(UNIT(other)->id, "sys-"))
551 return UNIT(other);
552
553 for (other = d->same_sysfs_prev; other; other = other->same_sysfs_prev) {
554 if (startswith(UNIT(other)->id, "sys-"))
555 return UNIT(other);
556
557 first = other;
558 }
559
560 return UNIT(first);
561 }
562
563 static int device_following_set(Unit *u, Set **_set) {
564 Device *d = DEVICE(u), *other;
565 Set *set;
566 int r;
567
568 assert(d);
569 assert(_set);
570
571 if (LIST_JUST_US(same_sysfs, d)) {
572 *_set = NULL;
573 return 0;
574 }
575
576 set = set_new(NULL);
577 if (!set)
578 return -ENOMEM;
579
580 LIST_FOREACH_AFTER(same_sysfs, other, d) {
581 r = set_put(set, other);
582 if (r < 0)
583 goto fail;
584 }
585
586 LIST_FOREACH_BEFORE(same_sysfs, other, d) {
587 r = set_put(set, other);
588 if (r < 0)
589 goto fail;
590 }
591
592 *_set = set;
593 return 1;
594
595 fail:
596 set_free(set);
597 return r;
598 }
599
600 static void device_shutdown(Manager *m) {
601 assert(m);
602
603 m->udev_event_source = sd_event_source_unref(m->udev_event_source);
604
605 if (m->udev_monitor) {
606 udev_monitor_unref(m->udev_monitor);
607 m->udev_monitor = NULL;
608 }
609
610 m->devices_by_sysfs = hashmap_free(m->devices_by_sysfs);
611 }
612
613 static void device_enumerate(Manager *m) {
614 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
615 struct udev_list_entry *item = NULL, *first = NULL;
616 int r;
617
618 assert(m);
619
620 if (!m->udev_monitor) {
621 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
622 if (!m->udev_monitor) {
623 log_oom();
624 goto fail;
625 }
626
627 /* This will fail if we are unprivileged, but that
628 * should not matter much, as user instances won't run
629 * during boot. */
630 (void) udev_monitor_set_receive_buffer_size(m->udev_monitor, 128*1024*1024);
631
632 r = udev_monitor_filter_add_match_tag(m->udev_monitor, "systemd");
633 if (r < 0) {
634 log_error_errno(r, "Failed to add udev tag match: %m");
635 goto fail;
636 }
637
638 r = udev_monitor_enable_receiving(m->udev_monitor);
639 if (r < 0) {
640 log_error_errno(r, "Failed to enable udev event reception: %m");
641 goto fail;
642 }
643
644 r = sd_event_add_io(m->event, &m->udev_event_source, udev_monitor_get_fd(m->udev_monitor), EPOLLIN, device_dispatch_io, m);
645 if (r < 0) {
646 log_error_errno(r, "Failed to watch udev file descriptor: %m");
647 goto fail;
648 }
649
650 (void) sd_event_source_set_description(m->udev_event_source, "device");
651 }
652
653 e = udev_enumerate_new(m->udev);
654 if (!e) {
655 log_oom();
656 goto fail;
657 }
658
659 r = udev_enumerate_add_match_tag(e, "systemd");
660 if (r < 0) {
661 log_error_errno(r, "Failed to create udev tag enumeration: %m");
662 goto fail;
663 }
664
665 r = udev_enumerate_add_match_is_initialized(e);
666 if (r < 0) {
667 log_error_errno(r, "Failed to install initialization match into enumeration: %m");
668 goto fail;
669 }
670
671 r = udev_enumerate_scan_devices(e);
672 if (r < 0) {
673 log_error_errno(r, "Failed to enumerate devices: %m");
674 goto fail;
675 }
676
677 first = udev_enumerate_get_list_entry(e);
678 udev_list_entry_foreach(item, first) {
679 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
680 const char *sysfs;
681
682 sysfs = udev_list_entry_get_name(item);
683
684 dev = udev_device_new_from_syspath(m->udev, sysfs);
685 if (!dev) {
686 log_oom();
687 continue;
688 }
689
690 if (!device_is_ready(dev))
691 continue;
692
693 (void) device_process_new(m, dev);
694
695 device_update_found_by_sysfs(m, sysfs, true, DEVICE_FOUND_UDEV, false);
696 }
697
698 return;
699
700 fail:
701 device_shutdown(m);
702 }
703
704 static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
705 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
706 Manager *m = userdata;
707 const char *action, *sysfs;
708 int r;
709
710 assert(m);
711
712 if (revents != EPOLLIN) {
713 static RATELIMIT_DEFINE(limit, 10*USEC_PER_SEC, 5);
714
715 if (!ratelimit_test(&limit))
716 log_error_errno(errno, "Failed to get udev event: %m");
717 if (!(revents & EPOLLIN))
718 return 0;
719 }
720
721 /*
722 * libudev might filter-out devices which pass the bloom
723 * filter, so getting NULL here is not necessarily an error.
724 */
725 dev = udev_monitor_receive_device(m->udev_monitor);
726 if (!dev)
727 return 0;
728
729 sysfs = udev_device_get_syspath(dev);
730 if (!sysfs) {
731 log_error("Failed to get udev sys path.");
732 return 0;
733 }
734
735 action = udev_device_get_action(dev);
736 if (!action) {
737 log_error("Failed to get udev action string.");
738 return 0;
739 }
740
741 if (streq(action, "remove")) {
742 r = swap_process_device_remove(m, dev);
743 if (r < 0)
744 log_error_errno(r, "Failed to process swap device remove event: %m");
745
746 /* If we get notified that a device was removed by
747 * udev, then it's completely gone, hence unset all
748 * found bits */
749 device_update_found_by_sysfs(m, sysfs, false, DEVICE_FOUND_UDEV|DEVICE_FOUND_MOUNT|DEVICE_FOUND_SWAP, true);
750
751 } else if (device_is_ready(dev)) {
752
753 (void) device_process_new(m, dev);
754
755 r = swap_process_device_new(m, dev);
756 if (r < 0)
757 log_error_errno(r, "Failed to process swap device new event: %m");
758
759 manager_dispatch_load_queue(m);
760
761 /* The device is found now, set the udev found bit */
762 device_update_found_by_sysfs(m, sysfs, true, DEVICE_FOUND_UDEV, true);
763
764 } else {
765 /* The device is nominally around, but not ready for
766 * us. Hence unset the udev bit, but leave the rest
767 * around. */
768
769 device_update_found_by_sysfs(m, sysfs, false, DEVICE_FOUND_UDEV, true);
770 }
771
772 return 0;
773 }
774
775 static bool device_supported(void) {
776 static int read_only = -1;
777
778 /* If /sys is read-only we don't support device units, and any
779 * attempts to start one should fail immediately. */
780
781 if (read_only < 0)
782 read_only = path_is_read_only_fs("/sys");
783
784 return read_only <= 0;
785 }
786
787 int device_found_node(Manager *m, const char *node, bool add, DeviceFound found, bool now) {
788 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
789 struct stat st;
790
791 assert(m);
792 assert(node);
793
794 if (!device_supported())
795 return 0;
796
797 /* This is called whenever we find a device referenced in
798 * /proc/swaps or /proc/self/mounts. Such a device might be
799 * mounted/enabled at a time where udev has not finished
800 * probing it yet, and we thus haven't learned about it
801 * yet. In this case we will set the device unit to
802 * "tentative" state. */
803
804 if (add) {
805 if (!path_startswith(node, "/dev"))
806 return 0;
807
808 /* We make an extra check here, if the device node
809 * actually exists. If it's missing, then this is an
810 * indication that device was unplugged but is still
811 * referenced in /proc/swaps or
812 * /proc/self/mountinfo. Note that this check doesn't
813 * really cover all cases where a device might be gone
814 * away, since drives that can have a medium inserted
815 * will still have a device node even when the medium
816 * is not there... */
817
818 if (stat(node, &st) >= 0) {
819 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
820 return 0;
821
822 dev = udev_device_new_from_devnum(m->udev, S_ISBLK(st.st_mode) ? 'b' : 'c', st.st_rdev);
823 if (!dev && errno != ENOENT)
824 return log_error_errno(errno, "Failed to get udev device from devnum %u:%u: %m", major(st.st_rdev), minor(st.st_rdev));
825
826 } else if (errno != ENOENT)
827 return log_error_errno(errno, "Failed to stat device node file %s: %m", node);
828
829 /* If the device is known in the kernel and newly
830 * appeared, then we'll create a device unit for it,
831 * under the name referenced in /proc/swaps or
832 * /proc/self/mountinfo. */
833
834 (void) device_setup_unit(m, dev, node, false);
835 }
836
837 /* Update the device unit's state, should it exist */
838 return device_update_found_by_name(m, node, add, found, now);
839 }
840
841 const UnitVTable device_vtable = {
842 .object_size = sizeof(Device),
843 .sections =
844 "Unit\0"
845 "Device\0"
846 "Install\0",
847
848 .init = device_init,
849 .done = device_done,
850 .load = unit_load_fragment_and_dropin_optional,
851
852 .coldplug = device_coldplug,
853
854 .serialize = device_serialize,
855 .deserialize_item = device_deserialize_item,
856
857 .dump = device_dump,
858
859 .active_state = device_active_state,
860 .sub_state_to_string = device_sub_state_to_string,
861
862 .bus_vtable = bus_device_vtable,
863
864 .following = device_following,
865 .following_set = device_following_set,
866
867 .enumerate = device_enumerate,
868 .shutdown = device_shutdown,
869 .supported = device_supported,
870
871 .status_message_formats = {
872 .starting_stopping = {
873 [0] = "Expecting device %s...",
874 },
875 .finished_start_job = {
876 [JOB_DONE] = "Found device %s.",
877 [JOB_TIMEOUT] = "Timed out waiting for device %s.",
878 },
879 },
880 };