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