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