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