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