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