1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
11 static void device_done(Unit
*u
) {
12 Device
*d
= DEVICE(u
);
18 static void device_dump(Unit
*u
, FILE *f
, const char *prefix
) {
20 static const char* const state_table
[_DEVICE_STATE_MAX
] = {
21 [DEVICE_DEAD
] = "dead",
22 [DEVICE_AVAILABLE
] = "available"
25 Device
*d
= DEVICE(u
);
30 "%sDevice State: %s\n"
32 prefix
, state_table
[d
->state
],
36 static int device_add_escaped_name(Unit
*u
, const char *dn
) {
44 if (!(e
= unit_name_escape_path(dn
+1, ".device")))
47 r
= unit_add_name(u
, e
);
50 if (r
< 0 && r
!= -EEXIST
)
56 static int device_process_device(Manager
*m
, struct udev_device
*dev
) {
57 const char *dn
, *names
, *wants
, *sysfs
;
63 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
67 /* Check whether this entry is even relevant for us. */
68 dn
= udev_device_get_devnode(dev
);
69 names
= udev_device_get_property_value(dev
, "SYSTEMD_NAMES");
70 wants
= udev_device_get_property_value(dev
, "SYSTEMD_WANTS");
72 if (!dn
&& !names
&& !wants
)
75 /* Ok, seems kinda interesting. Now, let's see if this one
78 if (!(sysfs
= udev_device_get_syspath(dev
)))
81 assert(sysfs
[0] == '/');
82 if (!(e
= unit_name_escape_path(sysfs
+1, ".device")))
85 if (!(u
= manager_get_unit(m
, e
))) {
90 if (!(u
= unit_new(m
))) {
95 r
= unit_add_name(u
, e
);
101 if (!(DEVICE(u
)->sysfs
= strdup(sysfs
))) {
106 if ((model
= udev_device_get_property_value(dev
, "ID_MODEL_FROM_DATABASE")) ||
107 (model
= udev_device_get_property_value(dev
, "ID_MODEL")))
108 if (!(u
->meta
.description
= strdup(model
))) {
119 if ((r
= device_add_escaped_name(u
, dn
)) < 0)
122 first
= udev_device_get_devlinks_list_entry(dev
);
123 udev_list_entry_foreach(item
, first
)
124 if ((r
= device_add_escaped_name(u
, udev_list_entry_get_name(item
))) < 0)
128 FOREACH_WORD(w
, l
, names
, state
) {
129 if (!(e
= strndup(w
, l
)))
132 r
= unit_add_name(u
, e
);
135 if (r
< 0 && r
!= -EEXIST
)
141 if (set_isempty(u
->meta
.names
)) {
147 FOREACH_WORD(w
, l
, wants
, state
) {
148 if (!(e
= strndup(w
, l
)))
151 r
= unit_add_dependency_by_name(u
, UNIT_WANTS
, e
);
159 unit_add_to_load_queue(u
);
168 static int device_process_path(Manager
*m
, const char *path
) {
170 struct udev_device
*dev
;
175 if (!(dev
= udev_device_new_from_syspath(m
->udev
, path
))) {
176 log_warning("Failed to get udev device object from udev for path %s.", path
);
180 r
= device_process_device(m
, dev
);
181 udev_device_unref(dev
);
185 static void device_shutdown(Manager
*m
) {
192 static int device_enumerate(Manager
*m
) {
194 struct udev_enumerate
*e
= NULL
;
195 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
199 if (!(m
->udev
= udev_new()))
202 if (!(e
= udev_enumerate_new(m
->udev
))) {
207 if (udev_enumerate_scan_devices(e
) < 0) {
212 first
= udev_enumerate_get_list_entry(e
);
213 udev_list_entry_foreach(item
, first
)
214 device_process_path(m
, udev_list_entry_get_name(item
));
216 udev_enumerate_unref(e
);
222 udev_enumerate_unref(e
);
228 static UnitActiveState
device_active_state(Unit
*u
) {
229 return DEVICE(u
)->state
== DEVICE_DEAD
? UNIT_INACTIVE
: UNIT_ACTIVE
;
232 const UnitVTable device_vtable
= {
235 .init
= unit_load_fragment_and_dropin
,
239 .enumerate
= device_enumerate
,
240 .shutdown
= device_shutdown
,
242 .active_state
= device_active_state