]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a7334b09 | 2 | |
25ac040b | 3 | #include <errno.h> |
f94ea366 | 4 | #include <sys/epoll.h> |
25ac040b | 5 | |
ad172d19 LP |
6 | #include "sd-messages.h" |
7 | ||
b5efdb8a | 8 | #include "alloc-util.h" |
b4e2fcb6 | 9 | #include "bus-common-errors.h" |
4139c1b2 | 10 | #include "dbus-device.h" |
6fcbec6f | 11 | #include "dbus-unit.h" |
4212fa83 | 12 | #include "device-private.h" |
4366e598 | 13 | #include "device-util.h" |
6bedfcbb | 14 | #include "device.h" |
07630cea | 15 | #include "log.h" |
6bedfcbb | 16 | #include "parse-util.h" |
9eb977db | 17 | #include "path-util.h" |
ad172d19 | 18 | #include "ratelimit.h" |
d68c645b | 19 | #include "serialize.h" |
8fcde012 | 20 | #include "stat-util.h" |
07630cea LP |
21 | #include "string-util.h" |
22 | #include "swap.h" | |
2efa43dc | 23 | #include "udev-util.h" |
07630cea | 24 | #include "unit-name.h" |
9670d583 | 25 | #include "unit.h" |
5cb5a6ff | 26 | |
f50e0a01 | 27 | static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = { |
1363eeca | 28 | [DEVICE_DEAD] = UNIT_INACTIVE, |
628c89cc | 29 | [DEVICE_TENTATIVE] = UNIT_ACTIVATING, |
1363eeca | 30 | [DEVICE_PLUGGED] = UNIT_ACTIVE, |
f50e0a01 LP |
31 | }; |
32 | ||
d0955f00 | 33 | static int device_dispatch_io(sd_device_monitor *monitor, sd_device *dev, void *userdata); |
718db961 | 34 | |
c072b84c YW |
35 | static int device_by_path(Manager *m, const char *path, Unit **ret) { |
36 | _cleanup_free_ char *e = NULL; | |
37 | Unit *u; | |
38 | int r; | |
39 | ||
40 | assert(m); | |
41 | assert(path); | |
42 | ||
43 | r = unit_name_from_path(path, ".device", &e); | |
44 | if (r < 0) | |
45 | return r; | |
46 | ||
47 | u = manager_get_unit(m, e); | |
48 | if (!u) | |
49 | return -ENOENT; | |
50 | ||
51 | if (ret) | |
52 | *ret = u; | |
53 | return 0; | |
54 | } | |
55 | ||
8fe914ec | 56 | static void device_unset_sysfs(Device *d) { |
f1421cc6 | 57 | Hashmap *devices; |
8fe914ec LP |
58 | |
59 | assert(d); | |
60 | ||
a7f241db LP |
61 | if (!d->sysfs) |
62 | return; | |
63 | ||
114e85d2 YW |
64 | /* Remove this unit from the chain of devices which share the same sysfs path. */ |
65 | ||
f1421cc6 | 66 | devices = UNIT(d)->manager->devices_by_sysfs; |
8fe914ec | 67 | |
114e85d2 YW |
68 | if (d->same_sysfs_prev) |
69 | /* If this is not the first unit, then simply remove this unit. */ | |
70 | d->same_sysfs_prev->same_sysfs_next = d->same_sysfs_next; | |
71 | else if (d->same_sysfs_next) | |
72 | /* If this is the first unit, replace with the next unit. */ | |
73 | assert_se(hashmap_replace(devices, d->same_sysfs_next->sysfs, d->same_sysfs_next) >= 0); | |
a7f241db | 74 | else |
114e85d2 | 75 | /* Otherwise, remove the entry. */ |
f1421cc6 | 76 | hashmap_remove(devices, d->sysfs); |
a7f241db | 77 | |
114e85d2 YW |
78 | if (d->same_sysfs_next) |
79 | d->same_sysfs_next->same_sysfs_prev = d->same_sysfs_prev; | |
80 | ||
81 | d->same_sysfs_prev = d->same_sysfs_next = NULL; | |
82 | ||
a1e58e8e | 83 | d->sysfs = mfree(d->sysfs); |
8fe914ec LP |
84 | } |
85 | ||
628c89cc | 86 | static int device_set_sysfs(Device *d, const char *sysfs) { |
ccd419f0 | 87 | _cleanup_free_ char *copy = NULL; |
628c89cc | 88 | Device *first; |
628c89cc LP |
89 | int r; |
90 | ||
91 | assert(d); | |
92 | ||
93 | if (streq_ptr(d->sysfs, sysfs)) | |
94 | return 0; | |
95 | ||
548f6937 | 96 | r = hashmap_ensure_allocated(&UNIT(d)->manager->devices_by_sysfs, &path_hash_ops); |
628c89cc LP |
97 | if (r < 0) |
98 | return r; | |
99 | ||
100 | copy = strdup(sysfs); | |
101 | if (!copy) | |
102 | return -ENOMEM; | |
103 | ||
104 | device_unset_sysfs(d); | |
105 | ||
106 | first = hashmap_get(UNIT(d)->manager->devices_by_sysfs, sysfs); | |
107 | LIST_PREPEND(same_sysfs, first, d); | |
108 | ||
109 | r = hashmap_replace(UNIT(d)->manager->devices_by_sysfs, copy, first); | |
110 | if (r < 0) { | |
111 | LIST_REMOVE(same_sysfs, first, d); | |
628c89cc LP |
112 | return r; |
113 | } | |
114 | ||
ccd419f0 | 115 | d->sysfs = TAKE_PTR(copy); |
7c4d1394 MS |
116 | unit_add_to_dbus_queue(UNIT(d)); |
117 | ||
628c89cc LP |
118 | return 0; |
119 | } | |
120 | ||
faf919f1 LP |
121 | static void device_init(Unit *u) { |
122 | Device *d = DEVICE(u); | |
123 | ||
124 | assert(d); | |
1124fe6f | 125 | assert(UNIT(d)->load_state == UNIT_STUB); |
faf919f1 | 126 | |
8fe914ec LP |
127 | /* In contrast to all other unit types we timeout jobs waiting |
128 | * for devices by default. This is because they otherwise wait | |
35b8ca3a | 129 | * indefinitely for plugged in devices, something which cannot |
8fe914ec LP |
130 | * happen for the other units since their operations time out |
131 | * anyway. */ | |
c9e120e0 | 132 | u->job_running_timeout = u->manager->defaults.device_timeout_usec; |
c8f4d764 | 133 | |
f1421cc6 | 134 | u->ignore_on_isolate = true; |
66f3fdbb LP |
135 | |
136 | d->deserialized_state = _DEVICE_STATE_INVALID; | |
faf919f1 LP |
137 | } |
138 | ||
87f0e418 LP |
139 | static void device_done(Unit *u) { |
140 | Device *d = DEVICE(u); | |
034c6ed7 LP |
141 | |
142 | assert(d); | |
e537352b | 143 | |
8fe914ec | 144 | device_unset_sysfs(d); |
1ea74fca | 145 | d->deserialized_sysfs = mfree(d->deserialized_sysfs); |
88116909 | 146 | d->wants_property = strv_free(d->wants_property); |
367a2597 | 147 | d->path = mfree(d->path); |
e537352b LP |
148 | } |
149 | ||
1d4c6f5b ZJS |
150 | static int device_load(Unit *u) { |
151 | int r; | |
152 | ||
c3620770 | 153 | r = unit_load_fragment_and_dropin(u, false); |
1d4c6f5b ZJS |
154 | if (r < 0) |
155 | return r; | |
156 | ||
157 | if (!u->description) { | |
e41db484 LP |
158 | /* Generate a description based on the path, to be used until the device is initialized |
159 | properly */ | |
1d4c6f5b ZJS |
160 | r = unit_name_to_path(u->id, &u->description); |
161 | if (r < 0) | |
162 | log_unit_debug_errno(u, r, "Failed to unescape name: %m"); | |
163 | } | |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
f50e0a01 LP |
168 | static void device_set_state(Device *d, DeviceState state) { |
169 | DeviceState old_state; | |
1363eeca | 170 | |
f50e0a01 | 171 | assert(d); |
5cb5a6ff | 172 | |
6fcbec6f LP |
173 | if (d->state != state) |
174 | bus_unit_send_pending_change_signal(UNIT(d), false); | |
175 | ||
f50e0a01 LP |
176 | old_state = d->state; |
177 | d->state = state; | |
5cb5a6ff | 178 | |
244f8055 LP |
179 | if (state == DEVICE_DEAD) |
180 | device_unset_sysfs(d); | |
181 | ||
e537352b | 182 | if (state != old_state) |
f2341e0a | 183 | log_unit_debug(UNIT(d), "Changed %s -> %s", device_state_to_string(old_state), device_state_to_string(state)); |
f50e0a01 | 184 | |
96b09de5 | 185 | unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state], /* reload_success = */ true); |
f50e0a01 LP |
186 | } |
187 | ||
dce2d35c YW |
188 | static void device_found_changed(Device *d, DeviceFound previous, DeviceFound now) { |
189 | assert(d); | |
190 | ||
191 | /* Didn't exist before, but does now? if so, generate a new invocation ID for it */ | |
192 | if (previous == DEVICE_NOT_FOUND && now != DEVICE_NOT_FOUND) | |
193 | (void) unit_acquire_invocation_id(UNIT(d)); | |
194 | ||
195 | if (FLAGS_SET(now, DEVICE_FOUND_UDEV)) | |
196 | /* When the device is known to udev we consider it plugged. */ | |
197 | device_set_state(d, DEVICE_PLUGGED); | |
198 | else if (now != DEVICE_NOT_FOUND && !FLAGS_SET(previous, DEVICE_FOUND_UDEV)) | |
199 | /* If the device has not been seen by udev yet, but is now referenced by the kernel, then we assume the | |
200 | * kernel knows it now, and udev might soon too. */ | |
201 | device_set_state(d, DEVICE_TENTATIVE); | |
202 | else | |
203 | /* If nobody sees the device, or if the device was previously seen by udev and now is only referenced | |
204 | * from the kernel, then we consider the device is gone, the kernel just hasn't noticed it yet. */ | |
205 | device_set_state(d, DEVICE_DEAD); | |
206 | } | |
207 | ||
208 | static void device_update_found_one(Device *d, DeviceFound found, DeviceFound mask) { | |
209 | assert(d); | |
210 | ||
211 | if (MANAGER_IS_RUNNING(UNIT(d)->manager)) { | |
212 | DeviceFound n, previous; | |
213 | ||
214 | /* When we are already running, then apply the new mask right-away, and trigger state changes | |
215 | * right-away */ | |
216 | ||
217 | n = (d->found & ~mask) | (found & mask); | |
218 | if (n == d->found) | |
219 | return; | |
220 | ||
221 | previous = d->found; | |
222 | d->found = n; | |
223 | ||
224 | device_found_changed(d, previous, n); | |
225 | } else | |
226 | /* We aren't running yet, let's apply the new mask to the shadow variable instead, which we'll apply as | |
227 | * soon as we catch-up with the state. */ | |
228 | d->enumerated_found = (d->enumerated_found & ~mask) | (found & mask); | |
229 | } | |
230 | ||
231 | static void device_update_found_by_sysfs(Manager *m, const char *sysfs, DeviceFound found, DeviceFound mask) { | |
232 | Device *l; | |
233 | ||
234 | assert(m); | |
235 | assert(sysfs); | |
236 | ||
237 | if (mask == 0) | |
238 | return; | |
239 | ||
240 | l = hashmap_get(m->devices_by_sysfs, sysfs); | |
241 | LIST_FOREACH(same_sysfs, d, l) | |
242 | device_update_found_one(d, found, mask); | |
243 | } | |
244 | ||
245 | static void device_update_found_by_name(Manager *m, const char *path, DeviceFound found, DeviceFound mask) { | |
dce2d35c | 246 | Unit *u; |
dce2d35c YW |
247 | |
248 | assert(m); | |
249 | assert(path); | |
250 | ||
251 | if (mask == 0) | |
252 | return; | |
253 | ||
c072b84c | 254 | if (device_by_path(m, path, &u) < 0) |
dce2d35c YW |
255 | return; |
256 | ||
257 | device_update_found_one(DEVICE(u), found, mask); | |
258 | } | |
259 | ||
be847e82 | 260 | static int device_coldplug(Unit *u) { |
f50e0a01 LP |
261 | Device *d = DEVICE(u); |
262 | ||
263 | assert(d); | |
264 | assert(d->state == DEVICE_DEAD); | |
265 | ||
66f3fdbb | 266 | /* First, let's put the deserialized state and found mask into effect, if we have it. */ |
75d7b598 YW |
267 | if (d->deserialized_state < 0) |
268 | return 0; | |
269 | ||
270 | Manager *m = u->manager; | |
271 | DeviceFound found = d->deserialized_found; | |
272 | DeviceState state = d->deserialized_state; | |
273 | ||
274 | /* On initial boot, switch-root, reload, reexecute, the following happen: | |
275 | * 1. MANAGER_IS_RUNNING() == false | |
276 | * 2. enumerate devices: manager_enumerate() -> device_enumerate() | |
277 | * Device.enumerated_found is set. | |
54a4d715 | 278 | * 3. deserialize devices: manager_deserialize() -> device_deserialize_item() |
75d7b598 YW |
279 | * Device.deserialize_state and Device.deserialized_found are set. |
280 | * 4. coldplug devices: manager_coldplug() -> device_coldplug() | |
281 | * deserialized properties are copied to the main properties. | |
282 | * 5. MANAGER_IS_RUNNING() == true: manager_ready() | |
283 | * 6. catchup devices: manager_catchup() -> device_catchup() | |
284 | * Device.enumerated_found is applied to Device.found, and state is updated based on that. | |
285 | * | |
286 | * Notes: | |
287 | * - On initial boot, no udev database exists. Hence, no devices are enumerated in the step 2. | |
288 | * Also, there is no deserialized device. Device units are (a) generated based on dependencies of | |
289 | * other units, or (b) generated when uevents are received. | |
290 | * | |
3881fd40 | 291 | * - On switch-root, the udev database may be cleared, except for devices with sticky bit, i.e. |
32e27670 | 292 | * OPTIONS="db_persist". Hence, almost no devices are enumerated in the step 2. However, in |
54a4d715 YW |
293 | * general, we have several serialized devices. So, DEVICE_FOUND_UDEV bit in the |
294 | * Device.deserialized_found must be ignored, as udev rules in initrd and the main system are often | |
295 | * different. If the deserialized state is DEVICE_PLUGGED, we need to downgrade it to | |
296 | * DEVICE_TENTATIVE. Unlike the other starting mode, MANAGER_IS_SWITCHING_ROOT() is true when | |
297 | * device_coldplug() and device_catchup() are called. Hence, let's conditionalize the operations by | |
298 | * using the flag. After switch-root, systemd-udevd will (re-)process all devices, and the | |
299 | * Device.found and Device.state will be adjusted. | |
75d7b598 | 300 | * |
54a4d715 YW |
301 | * - On reload or reexecute, we can trust Device.enumerated_found, Device.deserialized_found, and |
302 | * Device.deserialized_state. Of course, deserialized parameters may be outdated, but the unit | |
303 | * state can be adjusted later by device_catchup() or uevents. */ | |
75d7b598 | 304 | |
7870de03 | 305 | if (MANAGER_IS_SWITCHING_ROOT(m) && |
4fc69e8a | 306 | !FLAGS_SET(d->enumerated_found, DEVICE_FOUND_UDEV)) { |
54a4d715 YW |
307 | |
308 | /* The device has not been enumerated. On switching-root, such situation is natural. See the | |
309 | * above comment. To prevent problematic state transition active → dead → active, let's | |
310 | * drop the DEVICE_FOUND_UDEV flag and downgrade state to DEVICE_TENTATIVE(activating). See | |
311 | * issue #12953 and #23208. */ | |
312 | found &= ~DEVICE_FOUND_UDEV; | |
75d7b598 | 313 | if (state == DEVICE_PLUGGED) |
54a4d715 | 314 | state = DEVICE_TENTATIVE; |
b6c86ae2 YW |
315 | |
316 | /* Also check the validity of the device syspath. Without this check, if the device was | |
317 | * removed while switching root, it would never go to inactive state, as both Device.found | |
318 | * and Device.enumerated_found do not have the DEVICE_FOUND_UDEV flag, so device_catchup() in | |
319 | * device_update_found_one() does nothing in most cases. See issue #25106. Note that the | |
320 | * syspath field is only serialized when systemd is sufficiently new and the device has been | |
321 | * already processed by udevd. */ | |
322 | if (d->deserialized_sysfs) { | |
323 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; | |
324 | ||
325 | if (sd_device_new_from_syspath(&dev, d->deserialized_sysfs) < 0) | |
326 | state = DEVICE_DEAD; | |
327 | } | |
75d7b598 | 328 | } |
918e6f1c | 329 | |
75d7b598 | 330 | if (d->found == found && d->state == state) |
66f3fdbb | 331 | return 0; |
f50e0a01 | 332 | |
75d7b598 YW |
333 | d->found = found; |
334 | device_set_state(d, state); | |
f50e0a01 LP |
335 | return 0; |
336 | } | |
337 | ||
66f3fdbb LP |
338 | static void device_catchup(Unit *u) { |
339 | Device *d = DEVICE(u); | |
340 | ||
341 | assert(d); | |
342 | ||
f33bc879 | 343 | /* Second, let's update the state with the enumerated state */ |
66f3fdbb LP |
344 | device_update_found_one(d, d->enumerated_found, DEVICE_FOUND_MASK); |
345 | } | |
346 | ||
75d0aba4 YW |
347 | static const struct { |
348 | DeviceFound flag; | |
349 | const char *name; | |
350 | } device_found_map[] = { | |
66f3fdbb LP |
351 | { DEVICE_FOUND_UDEV, "found-udev" }, |
352 | { DEVICE_FOUND_MOUNT, "found-mount" }, | |
353 | { DEVICE_FOUND_SWAP, "found-swap" }, | |
75d0aba4 YW |
354 | }; |
355 | ||
356 | static int device_found_to_string_many(DeviceFound flags, char **ret) { | |
357 | _cleanup_free_ char *s = NULL; | |
75d0aba4 YW |
358 | |
359 | assert(ret); | |
360 | ||
fe96c0f8 | 361 | for (size_t i = 0; i < ELEMENTSOF(device_found_map); i++) { |
6e0f878e | 362 | if (!FLAGS_SET(flags, device_found_map[i].flag)) |
75d0aba4 YW |
363 | continue; |
364 | ||
c2bc710b | 365 | if (!strextend_with_separator(&s, ",", device_found_map[i].name)) |
75d0aba4 YW |
366 | return -ENOMEM; |
367 | } | |
368 | ||
369 | *ret = TAKE_PTR(s); | |
370 | ||
371 | return 0; | |
372 | } | |
373 | ||
374 | static int device_found_from_string_many(const char *name, DeviceFound *ret) { | |
375 | DeviceFound flags = 0; | |
376 | int r; | |
377 | ||
378 | assert(ret); | |
379 | ||
380 | for (;;) { | |
381 | _cleanup_free_ char *word = NULL; | |
382 | DeviceFound f = 0; | |
383 | unsigned i; | |
384 | ||
385 | r = extract_first_word(&name, &word, ",", 0); | |
386 | if (r < 0) | |
387 | return r; | |
388 | if (r == 0) | |
389 | break; | |
390 | ||
69ce73d1 | 391 | for (i = 0; i < ELEMENTSOF(device_found_map); i++) |
75d0aba4 YW |
392 | if (streq(word, device_found_map[i].name)) { |
393 | f = device_found_map[i].flag; | |
394 | break; | |
395 | } | |
396 | ||
397 | if (f == 0) | |
398 | return -EINVAL; | |
399 | ||
400 | flags |= f; | |
401 | } | |
402 | ||
403 | *ret = flags; | |
404 | return 0; | |
405 | } | |
406 | ||
f6200941 | 407 | static int device_serialize(Unit *u, FILE *f, FDSet *fds) { |
75d0aba4 | 408 | _cleanup_free_ char *s = NULL; |
f6200941 LP |
409 | Device *d = DEVICE(u); |
410 | ||
c73f413d | 411 | assert(d); |
f6200941 LP |
412 | assert(u); |
413 | assert(f); | |
414 | assert(fds); | |
415 | ||
1ea74fca YW |
416 | if (d->sysfs) |
417 | (void) serialize_item(f, "sysfs", d->sysfs); | |
418 | ||
367a2597 YW |
419 | if (d->path) |
420 | (void) serialize_item(f, "path", d->path); | |
421 | ||
d68c645b | 422 | (void) serialize_item(f, "state", device_state_to_string(d->state)); |
75d0aba4 | 423 | |
43ba7d71 | 424 | if (device_found_to_string_many(d->found, &s) >= 0) |
d68c645b | 425 | (void) serialize_item(f, "found", s); |
0108f6ec DM |
426 | |
427 | return 0; | |
f6200941 LP |
428 | } |
429 | ||
430 | static int device_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
431 | Device *d = DEVICE(u); | |
75d0aba4 | 432 | int r; |
f6200941 | 433 | |
c73f413d | 434 | assert(d); |
f6200941 LP |
435 | assert(u); |
436 | assert(key); | |
437 | assert(value); | |
438 | assert(fds); | |
439 | ||
1ea74fca YW |
440 | if (streq(key, "sysfs")) { |
441 | if (!d->deserialized_sysfs) { | |
442 | d->deserialized_sysfs = strdup(value); | |
443 | if (!d->deserialized_sysfs) | |
444 | log_oom_debug(); | |
445 | } | |
446 | ||
447 | } else if (streq(key, "path")) { | |
367a2597 YW |
448 | if (!d->path) { |
449 | d->path = strdup(value); | |
450 | if (!d->path) | |
451 | log_oom_debug(); | |
452 | } | |
453 | ||
454 | } else if (streq(key, "state")) { | |
f6200941 LP |
455 | DeviceState state; |
456 | ||
457 | state = device_state_from_string(value); | |
458 | if (state < 0) | |
8bb2c8c9 | 459 | log_unit_debug(u, "Failed to parse state value, ignoring: %s", value); |
f6200941 LP |
460 | else |
461 | d->deserialized_state = state; | |
918e6f1c FB |
462 | |
463 | } else if (streq(key, "found")) { | |
66f3fdbb | 464 | r = device_found_from_string_many(value, &d->deserialized_found); |
75d0aba4 | 465 | if (r < 0) |
5e1ee764 | 466 | log_unit_debug_errno(u, r, "Failed to parse found value '%s', ignoring: %m", value); |
918e6f1c | 467 | |
f6200941 | 468 | } else |
f2341e0a | 469 | log_unit_debug(u, "Unknown serialization key: %s", key); |
f6200941 LP |
470 | |
471 | return 0; | |
472 | } | |
473 | ||
f50e0a01 | 474 | static void device_dump(Unit *u, FILE *f, const char *prefix) { |
25ac040b | 475 | Device *d = DEVICE(u); |
4d86c235 | 476 | _cleanup_free_ char *s = NULL; |
5cb5a6ff | 477 | |
25ac040b | 478 | assert(d); |
5cb5a6ff | 479 | |
4d86c235 ZJS |
480 | (void) device_found_to_string_many(d->found, &s); |
481 | ||
5cb5a6ff | 482 | fprintf(f, |
25ac040b | 483 | "%sDevice State: %s\n" |
367a2597 | 484 | "%sDevice Path: %s\n" |
4d86c235 ZJS |
485 | "%sSysfs Path: %s\n" |
486 | "%sFound: %s\n", | |
a16e1123 | 487 | prefix, device_state_to_string(d->state), |
367a2597 | 488 | prefix, strna(d->path), |
4d86c235 ZJS |
489 | prefix, strna(d->sysfs), |
490 | prefix, strna(s)); | |
88116909 | 491 | |
de010b0b YW |
492 | STRV_FOREACH(i, d->wants_property) |
493 | fprintf(f, "%sudev SYSTEMD_WANTS: %s\n", | |
494 | prefix, *i); | |
f50e0a01 LP |
495 | } |
496 | ||
d1e8e8b5 | 497 | static UnitActiveState device_active_state(Unit *u) { |
f50e0a01 LP |
498 | assert(u); |
499 | ||
500 | return state_translation_table[DEVICE(u)->state]; | |
25ac040b LP |
501 | } |
502 | ||
d1e8e8b5 | 503 | static const char *device_sub_state_to_string(Unit *u) { |
10a94420 LP |
504 | assert(u); |
505 | ||
a16e1123 | 506 | return device_state_to_string(DEVICE(u)->state); |
10a94420 LP |
507 | } |
508 | ||
4366e598 | 509 | static int device_update_description(Unit *u, sd_device *dev, const char *path) { |
1d4c6f5b ZJS |
510 | _cleanup_free_ char *j = NULL; |
511 | const char *model, *label, *desc; | |
628c89cc | 512 | int r; |
965e5c5d LP |
513 | |
514 | assert(u); | |
965e5c5d LP |
515 | assert(path); |
516 | ||
1d4c6f5b ZJS |
517 | desc = path; |
518 | ||
519 | if (dev && | |
520 | (sd_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE", &model) >= 0 || | |
521 | sd_device_get_property_value(dev, "ID_MODEL", &model) >= 0)) { | |
522 | desc = model; | |
965e5c5d LP |
523 | |
524 | /* Try to concatenate the device model string with a label, if there is one */ | |
4366e598 YW |
525 | if (sd_device_get_property_value(dev, "ID_FS_LABEL", &label) >= 0 || |
526 | sd_device_get_property_value(dev, "ID_PART_ENTRY_NAME", &label) >= 0 || | |
527 | sd_device_get_property_value(dev, "ID_PART_ENTRY_NUMBER", &label) >= 0) { | |
965e5c5d | 528 | |
1d4c6f5b | 529 | desc = j = strjoin(model, " ", label); |
ea8ec43b LP |
530 | if (!j) |
531 | return log_oom(); | |
1d4c6f5b ZJS |
532 | } |
533 | } | |
ea8ec43b | 534 | |
1d4c6f5b | 535 | r = unit_set_description(u, desc); |
628c89cc | 536 | if (r < 0) |
ea8ec43b | 537 | return log_unit_error_errno(u, r, "Failed to set device description: %m"); |
965e5c5d | 538 | |
ea8ec43b | 539 | return 0; |
965e5c5d LP |
540 | } |
541 | ||
4366e598 | 542 | static int device_add_udev_wants(Unit *u, sd_device *dev) { |
88116909 | 543 | _cleanup_strv_free_ char **added = NULL; |
de040543 | 544 | const char *wants, *property; |
88116909 | 545 | Device *d = DEVICE(u); |
965e5c5d LP |
546 | int r; |
547 | ||
88116909 | 548 | assert(d); |
965e5c5d LP |
549 | assert(dev); |
550 | ||
463d0d15 | 551 | property = MANAGER_IS_USER(u->manager) ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS"; |
de040543 | 552 | |
4366e598 YW |
553 | r = sd_device_get_property_value(dev, property, &wants); |
554 | if (r < 0) | |
de040543 LP |
555 | return 0; |
556 | ||
557 | for (;;) { | |
ceed8f0c | 558 | _cleanup_free_ char *word = NULL, *k = NULL; |
965e5c5d | 559 | |
4ec85141 | 560 | r = extract_first_word(&wants, &word, NULL, EXTRACT_UNQUOTE); |
ceed8f0c | 561 | if (r == 0) |
88116909 | 562 | break; |
ceed8f0c ZJS |
563 | if (r == -ENOMEM) |
564 | return log_oom(); | |
565 | if (r < 0) | |
dcebc9ba | 566 | return log_unit_error_errno(u, r, "Failed to parse property %s with value %s: %m", property, wants); |
965e5c5d | 567 | |
88116909 | 568 | if (unit_name_is_valid(word, UNIT_NAME_TEMPLATE) && d->sysfs) { |
dcebc9ba LP |
569 | _cleanup_free_ char *escaped = NULL; |
570 | ||
571 | /* If the unit name is specified as template, then automatically fill in the sysfs path of the | |
572 | * device as instance name, properly escaped. */ | |
573 | ||
88116909 | 574 | r = unit_name_path_escape(d->sysfs, &escaped); |
dcebc9ba | 575 | if (r < 0) |
88116909 | 576 | return log_unit_error_errno(u, r, "Failed to escape %s: %m", d->sysfs); |
dcebc9ba LP |
577 | |
578 | r = unit_name_replace_instance(word, escaped, &k); | |
579 | if (r < 0) | |
580 | return log_unit_error_errno(u, r, "Failed to build %s instance of template %s: %m", escaped, word); | |
581 | } else { | |
582 | /* If this is not a template, then let's mangle it so, that it becomes a valid unit name. */ | |
583 | ||
37cbc1d5 | 584 | r = unit_name_mangle(word, UNIT_NAME_MANGLE_WARN, &k); |
dcebc9ba LP |
585 | if (r < 0) |
586 | return log_unit_error_errno(u, r, "Failed to mangle unit name \"%s\": %m", word); | |
587 | } | |
965e5c5d | 588 | |
35d8c19a | 589 | r = unit_add_dependency_by_name(u, UNIT_WANTS, k, true, UNIT_DEPENDENCY_UDEV); |
965e5c5d | 590 | if (r < 0) |
de040543 | 591 | return log_unit_error_errno(u, r, "Failed to add Wants= dependency: %m"); |
88116909 | 592 | |
47e72170 | 593 | r = strv_consume(&added, TAKE_PTR(k)); |
88116909 LP |
594 | if (r < 0) |
595 | return log_oom(); | |
965e5c5d | 596 | } |
88116909 | 597 | |
de010b0b | 598 | if (d->state != DEVICE_DEAD) |
88116909 LP |
599 | /* So here's a special hack, to compensate for the fact that the udev database's reload cycles are not |
600 | * synchronized with our own reload cycles: when we detect that the SYSTEMD_WANTS property of a device | |
033fe650 YW |
601 | * changes while the device unit is already up, let's skip to trigger units that were already listed |
602 | * and are active, and start units otherwise. This typically happens during the boot-time switch root | |
603 | * transition, as udev devices will generally already be up in the initrd, but SYSTEMD_WANTS properties | |
604 | * get then added through udev rules only available on the host system, and thus only when the initial | |
605 | * udev coldplug trigger runs. | |
88116909 LP |
606 | * |
607 | * We do this only if the device has been up already when we parse this, as otherwise the usual | |
608 | * dependency logic that is run from the dead → plugged transition will trigger these deps. */ | |
88116909 LP |
609 | STRV_FOREACH(i, added) { |
610 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
611 | ||
033fe650 YW |
612 | if (strv_contains(d->wants_property, *i)) { |
613 | Unit *v; | |
614 | ||
615 | v = manager_get_unit(u->manager, *i); | |
616 | if (v && UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(v))) | |
617 | continue; /* The unit was already listed and is running. */ | |
618 | } | |
88116909 | 619 | |
50cbaba4 | 620 | r = manager_add_job_by_name(u->manager, JOB_START, *i, JOB_FAIL, NULL, &error, NULL); |
88116909 | 621 | if (r < 0) |
b4e2fcb6 YW |
622 | log_unit_full_errno(u, sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ? LOG_DEBUG : LOG_WARNING, r, |
623 | "Failed to enqueue %s job, ignoring: %s", property, bus_error_message(&error, r)); | |
88116909 | 624 | } |
88116909 | 625 | |
8cc53fae | 626 | return strv_free_and_replace(d->wants_property, added); |
965e5c5d LP |
627 | } |
628 | ||
4366e598 | 629 | static bool device_is_bound_by_mounts(Device *d, sd_device *dev) { |
40b1a32c | 630 | int r; |
ebc8968b FB |
631 | |
632 | assert(d); | |
633 | assert(dev); | |
634 | ||
4212fa83 YW |
635 | r = device_get_property_bool(dev, "SYSTEMD_MOUNT_DEVICE_BOUND"); |
636 | if (r < 0 && r != -ENOENT) | |
637 | log_device_warning_errno(dev, r, "Failed to parse SYSTEMD_MOUNT_DEVICE_BOUND= udev property, ignoring: %m"); | |
ebc8968b | 638 | |
4212fa83 | 639 | d->bind_mounts = r > 0; |
40b1a32c LP |
640 | |
641 | return d->bind_mounts; | |
ebc8968b FB |
642 | } |
643 | ||
86cdffd1 | 644 | static void device_upgrade_mount_deps(Unit *u) { |
ebc8968b | 645 | Unit *other; |
eef85c4a | 646 | void *v; |
ebc8968b FB |
647 | int r; |
648 | ||
eef85c4a LP |
649 | /* Let's upgrade Requires= to BindsTo= on us. (Used when SYSTEMD_MOUNT_DEVICE_BOUND is set) */ |
650 | ||
15ed3c3a | 651 | HASHMAP_FOREACH_KEY(v, other, unit_get_dependencies(u, UNIT_REQUIRED_BY)) { |
ebc8968b FB |
652 | if (other->type != UNIT_MOUNT) |
653 | continue; | |
654 | ||
eef85c4a | 655 | r = unit_add_dependency(other, UNIT_BINDS_TO, u, true, UNIT_DEPENDENCY_UDEV); |
ebc8968b | 656 | if (r < 0) |
86cdffd1 | 657 | log_unit_warning_errno(u, r, "Failed to add BindsTo= dependency between device and mount unit, ignoring: %m"); |
ebc8968b | 658 | } |
ebc8968b FB |
659 | } |
660 | ||
4228306b | 661 | static int device_setup_unit(Manager *m, sd_device *dev, const char *path, bool main, Set **units) { |
dd309fcd | 662 | _cleanup_(unit_freep) Unit *new_unit = NULL; |
628c89cc | 663 | _cleanup_free_ char *e = NULL; |
2005219f | 664 | const char *sysfs = NULL; |
dd309fcd | 665 | Unit *u; |
965e5c5d | 666 | int r; |
25ac040b LP |
667 | |
668 | assert(m); | |
965e5c5d | 669 | assert(path); |
25ac040b | 670 | |
2005219f | 671 | if (dev) { |
4366e598 | 672 | r = sd_device_get_syspath(dev, &sysfs); |
9951c8df LP |
673 | if (r < 0) |
674 | return log_device_debug_errno(dev, r, "Couldn't get syspath from device, ignoring: %m"); | |
2005219f | 675 | } |
7f275a9f | 676 | |
7410616c | 677 | r = unit_name_from_path(path, ".device", &e); |
a7fb1f2e | 678 | if (r < 0) |
ad172d19 | 679 | return log_struct_errno( |
a7fb1f2e | 680 | LOG_WARNING, r, |
ad172d19 LP |
681 | "MESSAGE_ID=" SD_MESSAGE_DEVICE_PATH_NOT_SUITABLE_STR, |
682 | "DEVICE=%s", path, | |
92663a5e ZJS |
683 | LOG_MESSAGE("Failed to generate valid unit name from device path '%s', ignoring device: %m", |
684 | path)); | |
628c89cc LP |
685 | |
686 | u = manager_get_unit(m, e); | |
38b9b72e | 687 | if (u) { |
66f3fdbb LP |
688 | /* The device unit can still be present even if the device was unplugged: a mount unit can reference it |
689 | * hence preventing the GC to have garbaged it. That's desired since the device unit may have a | |
690 | * dependency on the mount unit which was added during the loading of the later. When the device is | |
691 | * plugged the sysfs might not be initialized yet, as we serialize the device's state but do not | |
692 | * serialize the sysfs path across reloads/reexecs. Hence, when coming back from a reload/restart we | |
4a1a1caf YW |
693 | * might have the state valid, but not the sysfs path. Also, there is another possibility; when multiple |
694 | * devices have the same devlink (e.g. /dev/disk/by-uuid/xxxx), adding/updating/removing one of the | |
0b75493d | 695 | * device causes syspath change. Hence, let's always update sysfs path. */ |
25ac040b | 696 | |
5238e957 | 697 | /* Let's remove all dependencies generated due to udev properties. We'll re-add whatever is configured |
38b9b72e LP |
698 | * now below. */ |
699 | unit_remove_dependencies(u, UNIT_DEPENDENCY_UDEV); | |
dd309fcd | 700 | |
38b9b72e | 701 | } else { |
dd309fcd YW |
702 | r = unit_new_for_name(m, sizeof(Device), e, &new_unit); |
703 | if (r < 0) | |
704 | return log_device_error_errno(dev, r, "Failed to allocate device unit %s: %m", e); | |
aab14b13 | 705 | |
dd309fcd | 706 | u = new_unit; |
25ac040b | 707 | |
ee6cb288 | 708 | unit_add_to_load_queue(u); |
38b9b72e | 709 | } |
ee6cb288 | 710 | |
367a2597 YW |
711 | if (!DEVICE(u)->path) { |
712 | DEVICE(u)->path = strdup(path); | |
713 | if (!DEVICE(u)->path) | |
714 | return log_oom(); | |
715 | } | |
716 | ||
66f3fdbb | 717 | /* If this was created via some dependency and has not actually been seen yet ->sysfs will not be |
ee6cb288 | 718 | * initialized. Hence initialize it if necessary. */ |
2005219f MP |
719 | if (sysfs) { |
720 | r = device_set_sysfs(DEVICE(u), sysfs); | |
dd309fcd YW |
721 | if (r < 0) |
722 | return log_unit_error_errno(u, r, "Failed to set sysfs path %s: %m", sysfs); | |
ee6cb288 | 723 | |
66f3fdbb | 724 | /* The additional systemd udev properties we only interpret for the main object */ |
2005219f MP |
725 | if (main) |
726 | (void) device_add_udev_wants(u, dev); | |
727 | } | |
25ac040b | 728 | |
1d4c6f5b ZJS |
729 | (void) device_update_description(u, dev, path); |
730 | ||
75a50eb0 LP |
731 | /* So the user wants the mount units to be bound to the device but a mount unit might has been seen |
732 | * by systemd before the device appears on its radar. In this case the device unit is partially | |
733 | * initialized and includes the deps on the mount unit but at that time the "bind mounts" flag wasn't | |
734 | * present. Fix this up now. */ | |
40b1a32c | 735 | if (dev && device_is_bound_by_mounts(DEVICE(u), dev)) |
ebc8968b | 736 | device_upgrade_mount_deps(u); |
f94ea366 | 737 | |
4228306b YW |
738 | if (units) { |
739 | r = set_ensure_put(units, NULL, DEVICE(u)); | |
740 | if (r < 0) | |
741 | return log_unit_error_errno(u, r, "Failed to store unit: %m"); | |
742 | } | |
743 | ||
dd309fcd | 744 | TAKE_PTR(new_unit); |
25ac040b | 745 | return 0; |
25ac040b LP |
746 | } |
747 | ||
dce2d35c YW |
748 | static bool device_is_ready(sd_device *dev) { |
749 | int r; | |
750 | ||
751 | assert(dev); | |
752 | ||
1cb89339 YW |
753 | if (device_for_action(dev, SD_DEVICE_REMOVE)) |
754 | return false; | |
755 | ||
dce2d35c YW |
756 | r = device_is_renaming(dev); |
757 | if (r < 0) | |
758 | log_device_warning_errno(dev, r, "Failed to check if device is renaming, assuming device is not renaming: %m"); | |
759 | if (r > 0) { | |
760 | log_device_debug(dev, "Device busy: device is renaming"); | |
761 | return false; | |
762 | } | |
763 | ||
764 | /* Is it really tagged as 'systemd' right now? */ | |
765 | r = sd_device_has_current_tag(dev, "systemd"); | |
766 | if (r < 0) | |
767 | log_device_warning_errno(dev, r, "Failed to check if device has \"systemd\" tag, assuming device is not tagged with \"systemd\": %m"); | |
768 | if (r == 0) | |
769 | log_device_debug(dev, "Device busy: device is not tagged with \"systemd\""); | |
770 | if (r <= 0) | |
771 | return false; | |
772 | ||
773 | r = device_get_property_bool(dev, "SYSTEMD_READY"); | |
774 | if (r < 0 && r != -ENOENT) | |
775 | log_device_warning_errno(dev, r, "Failed to get device SYSTEMD_READY property, assuming device does not have \"SYSTEMD_READY\" property: %m"); | |
776 | if (r == 0) | |
777 | log_device_debug(dev, "Device busy: SYSTEMD_READY property from device is false"); | |
778 | ||
779 | return r != 0; | |
780 | } | |
781 | ||
4228306b | 782 | static int device_setup_devlink_unit_one(Manager *m, const char *devlink, Set **ready_units, Set **not_ready_units) { |
4a1a1caf | 783 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
4228306b | 784 | Unit *u; |
4a1a1caf YW |
785 | |
786 | assert(m); | |
787 | assert(devlink); | |
4228306b YW |
788 | assert(ready_units); |
789 | assert(not_ready_units); | |
4a1a1caf | 790 | |
4228306b YW |
791 | if (sd_device_new_from_devname(&dev, devlink) >= 0 && device_is_ready(dev)) |
792 | return device_setup_unit(m, dev, devlink, /* main = */ false, ready_units); | |
4a1a1caf | 793 | |
4228306b YW |
794 | /* the devlink is already removed or not ready */ |
795 | if (device_by_path(m, devlink, &u) < 0) | |
796 | return 0; /* The corresponding .device unit not found. That's fine. */ | |
4a1a1caf | 797 | |
4228306b | 798 | return set_ensure_put(not_ready_units, NULL, DEVICE(u)); |
4a1a1caf YW |
799 | } |
800 | ||
4228306b YW |
801 | static int device_setup_extra_units(Manager *m, sd_device *dev, Set **ready_units, Set **not_ready_units) { |
802 | _cleanup_strv_free_ char **aliases = NULL; | |
a1af8372 | 803 | const char *syspath, *devname = NULL; |
4228306b | 804 | Device *l; |
4a1a1caf YW |
805 | int r; |
806 | ||
807 | assert(m); | |
808 | assert(dev); | |
4228306b YW |
809 | assert(ready_units); |
810 | assert(not_ready_units); | |
4a1a1caf YW |
811 | |
812 | r = sd_device_get_syspath(dev, &syspath); | |
813 | if (r < 0) | |
814 | return r; | |
815 | ||
4228306b | 816 | (void) sd_device_get_devname(dev, &devname); |
4a1a1caf | 817 | |
4228306b YW |
818 | /* devlink units */ |
819 | FOREACH_DEVICE_DEVLINK(dev, devlink) { | |
820 | /* These are a kind of special devlink. They should be always unique, but neither persistent | |
821 | * nor predictable. Hence, let's refuse them. See also the comments for alias units below. */ | |
4a1a1caf YW |
822 | if (PATH_STARTSWITH_SET(devlink, "/dev/block/", "/dev/char/")) |
823 | continue; | |
824 | ||
4228306b YW |
825 | (void) device_setup_devlink_unit_one(m, devlink, ready_units, not_ready_units); |
826 | } | |
4a1a1caf | 827 | |
4228306b YW |
828 | if (device_is_ready(dev)) { |
829 | const char *s; | |
4a1a1caf | 830 | |
4228306b YW |
831 | r = sd_device_get_property_value(dev, "SYSTEMD_ALIAS", &s); |
832 | if (r < 0 && r != -ENOENT) | |
833 | log_device_warning_errno(dev, r, "Failed to get SYSTEMD_ALIAS property, ignoring: %m"); | |
834 | if (r >= 0) { | |
835 | r = strv_split_full(&aliases, s, NULL, EXTRACT_UNQUOTE); | |
836 | if (r < 0) | |
837 | log_device_warning_errno(dev, r, "Failed to parse SYSTEMD_ALIAS property, ignoring: %m"); | |
838 | } | |
4a1a1caf YW |
839 | } |
840 | ||
4228306b YW |
841 | /* alias units */ |
842 | STRV_FOREACH(alias, aliases) { | |
843 | if (!path_is_absolute(*alias)) { | |
844 | log_device_warning(dev, "The alias \"%s\" specified in SYSTEMD_ALIAS is not an absolute path, ignoring.", *alias); | |
845 | continue; | |
846 | } | |
4a1a1caf | 847 | |
c352110a YW |
848 | if (!path_is_safe(*alias)) { |
849 | log_device_warning(dev, "The alias \"%s\" specified in SYSTEMD_ALIAS is not safe, ignoring.", *alias); | |
4228306b YW |
850 | continue; |
851 | } | |
4a1a1caf | 852 | |
4228306b YW |
853 | /* Note, even if the devlink is not persistent, LVM expects /dev/block/ symlink units exist. |
854 | * To achieve that, they set the path to SYSTEMD_ALIAS. Hence, we cannot refuse aliases start | |
855 | * with /dev/, unfortunately. */ | |
4a1a1caf | 856 | |
4228306b YW |
857 | (void) device_setup_unit(m, dev, *alias, /* main = */ false, ready_units); |
858 | } | |
4a1a1caf YW |
859 | |
860 | l = hashmap_get(m->devices_by_sysfs, syspath); | |
861 | LIST_FOREACH(same_sysfs, d, l) { | |
4a1a1caf YW |
862 | if (!d->path) |
863 | continue; | |
864 | ||
4228306b YW |
865 | if (path_equal(d->path, syspath)) |
866 | continue; /* This is the main unit. */ | |
4a1a1caf | 867 | |
4228306b YW |
868 | if (devname && path_equal(d->path, devname)) |
869 | continue; /* This is the real device node. */ | |
4a1a1caf | 870 | |
4228306b YW |
871 | if (device_has_devlink(dev, d->path)) |
872 | continue; /* The devlink was already processed in the above loop. */ | |
4a1a1caf | 873 | |
4228306b YW |
874 | if (strv_contains(aliases, d->path)) |
875 | continue; /* This is already processed in the above, and ready. */ | |
4a1a1caf | 876 | |
4228306b YW |
877 | if (path_startswith(d->path, "/dev/")) |
878 | /* This is a devlink unit. Check existence and update syspath. */ | |
879 | (void) device_setup_devlink_unit_one(m, d->path, ready_units, not_ready_units); | |
880 | else | |
881 | /* This is an alias unit of dropped or not ready device. */ | |
882 | (void) set_ensure_put(not_ready_units, NULL, d); | |
4a1a1caf YW |
883 | } |
884 | ||
4a1a1caf YW |
885 | return 0; |
886 | } | |
887 | ||
4228306b YW |
888 | static int device_setup_units(Manager *m, sd_device *dev, Set **ready_units, Set **not_ready_units) { |
889 | const char *syspath, *devname = NULL; | |
003ac9d0 | 890 | int r; |
8fe914ec LP |
891 | |
892 | assert(m); | |
1363eeca | 893 | assert(dev); |
4228306b YW |
894 | assert(ready_units); |
895 | assert(not_ready_units); | |
8fe914ec | 896 | |
4228306b YW |
897 | r = sd_device_get_syspath(dev, &syspath); |
898 | if (r < 0) | |
899 | return log_device_debug_errno(dev, r, "Couldn't get syspath from device, ignoring: %m"); | |
8fe914ec | 900 | |
4228306b YW |
901 | /* First, process the main (that is, points to the syspath) and (real, not symlink) devnode units. */ |
902 | if (device_for_action(dev, SD_DEVICE_REMOVE)) | |
30fd9a2d | 903 | /* If the device is removed, the main and devnode units will be removed by |
4228306b YW |
904 | * device_update_found_by_sysfs() in device_dispatch_io(). Hence, it is not necessary to |
905 | * store them to not_ready_units, and we have nothing to do here. | |
906 | * | |
907 | * Note, still we need to process devlink units below, as a devlink previously points to this | |
908 | * device may still exist and now point to another device node. That is, do not forget to | |
909 | * call device_setup_extra_units(). */ | |
910 | ; | |
911 | else if (device_is_ready(dev)) { | |
912 | /* Add the main unit named after the syspath. If this one fails, don't bother with the rest, | |
913 | * as this one shall be the main device unit the others just follow. (Compare with how | |
914 | * device_following() is implemented, see below, which looks for the sysfs device.) */ | |
915 | r = device_setup_unit(m, dev, syspath, /* main = */ true, ready_units); | |
916 | if (r < 0) | |
917 | return r; | |
8fe914ec | 918 | |
4228306b YW |
919 | /* Add an additional unit for the device node */ |
920 | if (sd_device_get_devname(dev, &devname) >= 0) | |
921 | (void) device_setup_unit(m, dev, devname, /* main = */ false, ready_units); | |
4366e598 | 922 | |
4228306b YW |
923 | } else { |
924 | Unit *u; | |
8fe914ec | 925 | |
4228306b | 926 | /* If the device exists but not ready, then save the units and unset udev bits later. */ |
f1421cc6 | 927 | |
4228306b YW |
928 | if (device_by_path(m, syspath, &u) >= 0) { |
929 | r = set_ensure_put(not_ready_units, NULL, DEVICE(u)); | |
930 | if (r < 0) | |
931 | log_unit_debug_errno(u, r, "Failed to store unit, ignoring: %m"); | |
932 | } | |
933 | ||
934 | if (sd_device_get_devname(dev, &devname) >= 0 && | |
935 | device_by_path(m, devname, &u) >= 0) { | |
936 | r = set_ensure_put(not_ready_units, NULL, DEVICE(u)); | |
937 | if (r < 0) | |
938 | log_unit_debug_errno(u, r, "Failed to store unit, ignoring: %m"); | |
939 | } | |
8fe914ec | 940 | } |
4228306b YW |
941 | |
942 | /* Next, add/update additional .device units point to aliases and symlinks. */ | |
943 | (void) device_setup_extra_units(m, dev, ready_units, not_ready_units); | |
a4cb8afb YW |
944 | |
945 | /* Safety check: no unit should be in ready_units and not_ready_units simultaneously. */ | |
946 | Unit *u; | |
947 | SET_FOREACH(u, *not_ready_units) | |
948 | if (set_remove(*ready_units, u)) | |
3b51a183 | 949 | log_unit_error(u, "Cannot activate and deactivate the unit simultaneously. Deactivating."); |
a4cb8afb | 950 | |
4228306b | 951 | return 0; |
8fe914ec LP |
952 | } |
953 | ||
a7f241db LP |
954 | static Unit *device_following(Unit *u) { |
955 | Device *d = DEVICE(u); | |
03677889 | 956 | Device *first = NULL; |
a7f241db LP |
957 | |
958 | assert(d); | |
959 | ||
ac155bb8 | 960 | if (startswith(u->id, "sys-")) |
a7f241db LP |
961 | return NULL; |
962 | ||
963 | /* Make everybody follow the unit that's named after the sysfs path */ | |
bd335c96 | 964 | LIST_FOREACH(same_sysfs, other, d->same_sysfs_next) |
1124fe6f | 965 | if (startswith(UNIT(other)->id, "sys-")) |
a7f241db LP |
966 | return UNIT(other); |
967 | ||
bd335c96 | 968 | LIST_FOREACH_BACKWARDS(same_sysfs, other, d->same_sysfs_prev) { |
1124fe6f | 969 | if (startswith(UNIT(other)->id, "sys-")) |
a7f241db LP |
970 | return UNIT(other); |
971 | ||
972 | first = other; | |
973 | } | |
974 | ||
975 | return UNIT(first); | |
976 | } | |
977 | ||
f1421cc6 | 978 | static int device_following_set(Unit *u, Set **_set) { |
03677889 | 979 | Device *d = DEVICE(u); |
af4fa99d | 980 | _cleanup_set_free_ Set *set = NULL; |
6210e7fc LP |
981 | int r; |
982 | ||
983 | assert(d); | |
f1421cc6 | 984 | assert(_set); |
6210e7fc | 985 | |
f1421cc6 LP |
986 | if (LIST_JUST_US(same_sysfs, d)) { |
987 | *_set = NULL; | |
6210e7fc LP |
988 | return 0; |
989 | } | |
990 | ||
d5099efc | 991 | set = set_new(NULL); |
f1421cc6 | 992 | if (!set) |
6210e7fc LP |
993 | return -ENOMEM; |
994 | ||
bd335c96 | 995 | LIST_FOREACH(same_sysfs, other, d->same_sysfs_next) { |
f1421cc6 LP |
996 | r = set_put(set, other); |
997 | if (r < 0) | |
95f14a3e | 998 | return r; |
f1421cc6 | 999 | } |
6210e7fc | 1000 | |
bd335c96 | 1001 | LIST_FOREACH_BACKWARDS(same_sysfs, other, d->same_sysfs_prev) { |
f1421cc6 LP |
1002 | r = set_put(set, other); |
1003 | if (r < 0) | |
95f14a3e | 1004 | return r; |
f1421cc6 | 1005 | } |
6210e7fc | 1006 | |
95f14a3e | 1007 | *_set = TAKE_PTR(set); |
6210e7fc | 1008 | return 1; |
6210e7fc LP |
1009 | } |
1010 | ||
25ac040b LP |
1011 | static void device_shutdown(Manager *m) { |
1012 | assert(m); | |
1013 | ||
d0955f00 | 1014 | m->device_monitor = sd_device_monitor_unref(m->device_monitor); |
525d3cc7 | 1015 | m->devices_by_sysfs = hashmap_free(m->devices_by_sysfs); |
25ac040b LP |
1016 | } |
1017 | ||
ba64af90 | 1018 | static void device_enumerate(Manager *m) { |
4366e598 | 1019 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
718db961 | 1020 | int r; |
25ac040b LP |
1021 | |
1022 | assert(m); | |
1023 | ||
d0955f00 YW |
1024 | if (!m->device_monitor) { |
1025 | r = sd_device_monitor_new(&m->device_monitor); | |
1026 | if (r < 0) { | |
1027 | log_error_errno(r, "Failed to allocate device monitor: %m"); | |
a16e1123 LP |
1028 | goto fail; |
1029 | } | |
f94ea366 | 1030 | |
47ae6e67 LP |
1031 | /* This will fail if we are unprivileged, but that |
1032 | * should not matter much, as user instances won't run | |
1033 | * during boot. */ | |
d0955f00 | 1034 | (void) sd_device_monitor_set_receive_buffer_size(m->device_monitor, 128*1024*1024); |
99448c1f | 1035 | |
d0955f00 | 1036 | r = sd_device_monitor_filter_add_match_tag(m->device_monitor, "systemd"); |
ba64af90 LP |
1037 | if (r < 0) { |
1038 | log_error_errno(r, "Failed to add udev tag match: %m"); | |
e1ce2c27 | 1039 | goto fail; |
ba64af90 | 1040 | } |
e1ce2c27 | 1041 | |
deb2b734 | 1042 | r = sd_device_monitor_attach_event(m->device_monitor, m->event); |
ba64af90 | 1043 | if (r < 0) { |
d0955f00 | 1044 | log_error_errno(r, "Failed to attach event to device monitor: %m"); |
a16e1123 | 1045 | goto fail; |
ba64af90 | 1046 | } |
f94ea366 | 1047 | |
deb2b734 | 1048 | r = sd_device_monitor_start(m->device_monitor, device_dispatch_io, m); |
ba64af90 | 1049 | if (r < 0) { |
d0955f00 | 1050 | log_error_errno(r, "Failed to start device monitor: %m"); |
718db961 | 1051 | goto fail; |
ba64af90 | 1052 | } |
a16e1123 | 1053 | } |
f94ea366 | 1054 | |
4366e598 | 1055 | r = sd_device_enumerator_new(&e); |
ba64af90 | 1056 | if (r < 0) { |
9b674e25 | 1057 | log_error_errno(r, "Failed to allocate device enumerator: %m"); |
e1ce2c27 | 1058 | goto fail; |
ba64af90 | 1059 | } |
25ac040b | 1060 | |
4366e598 | 1061 | r = sd_device_enumerator_add_match_tag(e, "systemd"); |
ba64af90 | 1062 | if (r < 0) { |
4366e598 | 1063 | log_error_errno(r, "Failed to set tag for device enumeration: %m"); |
e1202047 | 1064 | goto fail; |
ba64af90 | 1065 | } |
e1202047 | 1066 | |
8437c059 | 1067 | FOREACH_DEVICE(e, dev) { |
4228306b YW |
1068 | _cleanup_set_free_ Set *ready_units = NULL, *not_ready_units = NULL; |
1069 | Device *d; | |
628c89cc | 1070 | |
4228306b | 1071 | if (device_setup_units(m, dev, &ready_units, ¬_ready_units) < 0) |
4366e598 | 1072 | continue; |
4a1a1caf | 1073 | |
4228306b YW |
1074 | SET_FOREACH(d, ready_units) |
1075 | device_update_found_one(d, DEVICE_FOUND_UDEV, DEVICE_FOUND_UDEV); | |
1076 | SET_FOREACH(d, not_ready_units) | |
1077 | device_update_found_one(d, DEVICE_NOT_FOUND, DEVICE_FOUND_UDEV); | |
628c89cc | 1078 | } |
25ac040b | 1079 | |
ba64af90 | 1080 | return; |
25ac040b LP |
1081 | |
1082 | fail: | |
25ac040b | 1083 | device_shutdown(m); |
5cb5a6ff LP |
1084 | } |
1085 | ||
9616f550 YW |
1086 | static void device_propagate_reload(Manager *m, Device *d) { |
1087 | int r; | |
1088 | ||
1089 | assert(m); | |
1090 | assert(d); | |
1091 | ||
1092 | if (d->state == DEVICE_DEAD) | |
1093 | return; | |
1094 | ||
1095 | r = manager_propagate_reload(m, UNIT(d), JOB_REPLACE, NULL); | |
1096 | if (r < 0) | |
1097 | log_unit_warning_errno(UNIT(d), r, "Failed to propagate reload, ignoring: %m"); | |
1098 | } | |
1099 | ||
c8ad151a | 1100 | static void device_remove_old_on_move(Manager *m, sd_device *dev) { |
42ebcebf | 1101 | _cleanup_free_ char *syspath_old = NULL; |
87bc687a YW |
1102 | const char *devpath_old; |
1103 | int r; | |
1104 | ||
42ebcebf YW |
1105 | assert(m); |
1106 | assert(dev); | |
1107 | ||
87bc687a | 1108 | r = sd_device_get_property_value(dev, "DEVPATH_OLD", &devpath_old); |
c8ad151a LP |
1109 | if (r < 0) |
1110 | return (void) log_device_debug_errno(dev, r, "Failed to get DEVPATH_OLD= property on 'move' uevent, ignoring: %m"); | |
87bc687a YW |
1111 | |
1112 | syspath_old = path_join("/sys", devpath_old); | |
1113 | if (!syspath_old) | |
c8ad151a | 1114 | return (void) log_oom(); |
87bc687a | 1115 | |
b1ba0ce8 | 1116 | device_update_found_by_sysfs(m, syspath_old, DEVICE_NOT_FOUND, DEVICE_FOUND_MASK); |
87bc687a YW |
1117 | } |
1118 | ||
d0955f00 | 1119 | static int device_dispatch_io(sd_device_monitor *monitor, sd_device *dev, void *userdata) { |
4228306b | 1120 | _cleanup_set_free_ Set *ready_units = NULL, *not_ready_units = NULL; |
1363eeca | 1121 | Manager *m = ASSERT_PTR(userdata); |
a1130022 | 1122 | sd_device_action_t action; |
a7395c86 | 1123 | const char *sysfs; |
4a1a1caf | 1124 | bool ready; |
4228306b | 1125 | Device *d; |
718db961 | 1126 | int r; |
f94ea366 | 1127 | |
d0955f00 | 1128 | assert(dev); |
f94ea366 | 1129 | |
6cc4da5e DDM |
1130 | log_device_uevent(dev, "Processing udev action"); |
1131 | ||
4366e598 YW |
1132 | r = sd_device_get_syspath(dev, &sysfs); |
1133 | if (r < 0) { | |
58b0a3e5 | 1134 | log_device_warning_errno(dev, r, "Failed to get device syspath, ignoring: %m"); |
628c89cc LP |
1135 | return 0; |
1136 | } | |
1137 | ||
a1130022 | 1138 | r = sd_device_get_action(dev, &action); |
4366e598 | 1139 | if (r < 0) { |
58b0a3e5 | 1140 | log_device_warning_errno(dev, r, "Failed to get udev action, ignoring: %m"); |
718db961 | 1141 | return 0; |
f94ea366 LP |
1142 | } |
1143 | ||
dc53421d LP |
1144 | log_device_debug(dev, "Got '%s' action on syspath '%s'.", device_action_to_string(action), sysfs); |
1145 | ||
a1130022 | 1146 | if (action == SD_DEVICE_MOVE) |
c8ad151a | 1147 | device_remove_old_on_move(m, dev); |
87bc687a | 1148 | |
e9336d6a YW |
1149 | /* When udevd failed to process the device, SYSTEMD_ALIAS or any other properties may contain invalid |
1150 | * values. Let's refuse to handle the uevent. */ | |
1151 | if (sd_device_get_property_value(dev, "UDEV_WORKER_FAILED", NULL) >= 0) { | |
1152 | int v; | |
1153 | ||
1154 | if (device_get_property_int(dev, "UDEV_WORKER_ERRNO", &v) >= 0) | |
1155 | log_device_warning_errno(dev, v, "systemd-udevd failed to process the device, ignoring: %m"); | |
1156 | else if (device_get_property_int(dev, "UDEV_WORKER_EXIT_STATUS", &v) >= 0) | |
1157 | log_device_warning(dev, "systemd-udevd failed to process the device with exit status %i, ignoring.", v); | |
1158 | else if (device_get_property_int(dev, "UDEV_WORKER_SIGNAL", &v) >= 0) { | |
1159 | const char *s; | |
1160 | (void) sd_device_get_property_value(dev, "UDEV_WORKER_SIGNAL_NAME", &s); | |
1161 | log_device_warning(dev, "systemd-udevd failed to process the device with signal %i(%s), ignoring.", v, strna(s)); | |
1162 | } else | |
1163 | log_device_warning(dev, "systemd-udevd failed to process the device with unknown result, ignoring."); | |
1164 | ||
1165 | return 0; | |
1166 | } | |
1167 | ||
ae6ad21e LP |
1168 | /* A change event can signal that a device is becoming ready, in particular if the device is using |
1169 | * the SYSTEMD_READY logic in udev so we need to reach the else block of the following if, even for | |
1170 | * change events */ | |
1cb89339 YW |
1171 | ready = device_is_ready(dev); |
1172 | ||
4228306b YW |
1173 | (void) device_setup_units(m, dev, &ready_units, ¬_ready_units); |
1174 | ||
a1130022 | 1175 | if (action == SD_DEVICE_REMOVE) { |
628c89cc | 1176 | r = swap_process_device_remove(m, dev); |
9670d583 | 1177 | if (r < 0) |
71f79b56 | 1178 | log_device_warning_errno(dev, r, "Failed to process swap device remove event, ignoring: %m"); |
4228306b YW |
1179 | } else if (ready) { |
1180 | r = swap_process_device_new(m, dev); | |
1181 | if (r < 0) | |
1182 | log_device_warning_errno(dev, r, "Failed to process swap device new event, ignoring: %m"); | |
4a1a1caf YW |
1183 | } |
1184 | ||
4228306b YW |
1185 | if (!IN_SET(action, SD_DEVICE_ADD, SD_DEVICE_REMOVE, SD_DEVICE_MOVE)) |
1186 | SET_FOREACH(d, ready_units) | |
1187 | device_propagate_reload(m, d); | |
4a1a1caf | 1188 | |
4228306b | 1189 | if (!set_isempty(ready_units)) |
f1421cc6 LP |
1190 | manager_dispatch_load_queue(m); |
1191 | ||
4a1a1caf YW |
1192 | if (action == SD_DEVICE_REMOVE) |
1193 | /* If we get notified that a device was removed by udev, then it's completely gone, hence | |
4228306b YW |
1194 | * unset all found bits. Note this affects all .device units still point to the removed |
1195 | * device. */ | |
ef9ca572 | 1196 | device_update_found_by_sysfs(m, sysfs, DEVICE_NOT_FOUND, DEVICE_FOUND_MASK); |
f94ea366 | 1197 | |
4228306b YW |
1198 | /* These devices are found and ready now, set the udev found bit. Note, this is also necessary to do |
1199 | * on remove uevent, as some devlinks may be updated and now point to other device nodes. */ | |
1200 | SET_FOREACH(d, ready_units) | |
1201 | device_update_found_one(d, DEVICE_FOUND_UDEV, DEVICE_FOUND_UDEV); | |
1202 | ||
1203 | /* These devices may be nominally around, but not ready for us. Hence unset the udev bit, but leave | |
1204 | * the rest around. This may be redundant for remove uevent, but should be harmless. */ | |
1205 | SET_FOREACH(d, not_ready_units) | |
1206 | device_update_found_one(d, DEVICE_NOT_FOUND, DEVICE_FOUND_UDEV); | |
4a1a1caf | 1207 | |
718db961 | 1208 | return 0; |
f94ea366 LP |
1209 | } |
1210 | ||
e9222bfc | 1211 | void device_found_node(Manager *m, const char *node, DeviceFound found, DeviceFound mask) { |
e5ca27b7 | 1212 | int r; |
628c89cc LP |
1213 | |
1214 | assert(m); | |
1215 | assert(node); | |
03a94b73 | 1216 | assert(!FLAGS_SET(mask, DEVICE_FOUND_UDEV)); |
628c89cc | 1217 | |
f374631a | 1218 | if (!udev_available()) |
485ae697 | 1219 | return; |
4c6d20de | 1220 | |
34c20ee0 LP |
1221 | if (mask == 0) |
1222 | return; | |
1223 | ||
1224 | /* This is called whenever we find a device referenced in /proc/swaps or /proc/self/mounts. Such a device might | |
1225 | * be mounted/enabled at a time where udev has not finished probing it yet, and we thus haven't learned about | |
66f3fdbb LP |
1226 | * it yet. In this case we will set the device unit to "tentative" state. |
1227 | * | |
1228 | * This takes a pair of DeviceFound flags parameters. The 'mask' parameter is a bit mask that indicates which | |
1229 | * bits of 'found' to copy into the per-device DeviceFound flags field. Thus, this function may be used to set | |
1230 | * and unset individual bits in a single call, while merging partially with previous state. */ | |
628c89cc | 1231 | |
485ae697 | 1232 | if ((found & mask) != 0) { |
4366e598 | 1233 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
628c89cc | 1234 | |
34c20ee0 LP |
1235 | /* If the device is known in the kernel and newly appeared, then we'll create a device unit for it, |
1236 | * under the name referenced in /proc/swaps or /proc/self/mountinfo. But first, let's validate if | |
e9222bfc DDM |
1237 | * everything is alright with the device node. Note that we're fine with missing device nodes, |
1238 | * but not with badly set up ones. */ | |
1239 | ||
1240 | r = sd_device_new_from_devname(&dev, node); | |
1241 | if (r == -ENODEV) | |
1242 | log_debug("Could not find device for %s, continuing without device node", node); | |
1243 | else if (r < 0) { | |
1244 | /* Reduce log noise from nodes which are not device nodes by skipping EINVAL. */ | |
1245 | if (r != -EINVAL) | |
1246 | log_error_errno(r, "Failed to open %s device, ignoring: %m", node); | |
1247 | return; | |
1248 | } | |
628c89cc | 1249 | |
4228306b | 1250 | (void) device_setup_unit(m, dev, node, /* main = */ false, NULL); /* 'dev' may be NULL. */ |
628c89cc LP |
1251 | } |
1252 | ||
1253 | /* Update the device unit's state, should it exist */ | |
485ae697 | 1254 | (void) device_update_found_by_name(m, node, found, mask); |
628c89cc LP |
1255 | } |
1256 | ||
ebc8968b | 1257 | bool device_shall_be_bound_by(Unit *device, Unit *u) { |
8bb2c8c9 LP |
1258 | assert(device); |
1259 | assert(u); | |
ebc8968b FB |
1260 | |
1261 | if (u->type != UNIT_MOUNT) | |
1262 | return false; | |
1263 | ||
1264 | return DEVICE(device)->bind_mounts; | |
1265 | } | |
1266 | ||
87f0e418 | 1267 | const UnitVTable device_vtable = { |
7d17cfbc | 1268 | .object_size = sizeof(Device), |
f975e971 LP |
1269 | .sections = |
1270 | "Unit\0" | |
1271 | "Device\0" | |
1272 | "Install\0", | |
5cb5a6ff | 1273 | |
c5a97ed1 LP |
1274 | .gc_jobs = true, |
1275 | ||
faf919f1 | 1276 | .init = device_init, |
034c6ed7 | 1277 | .done = device_done, |
1d4c6f5b | 1278 | .load = device_load, |
718db961 | 1279 | |
f50e0a01 | 1280 | .coldplug = device_coldplug, |
66f3fdbb | 1281 | .catchup = device_catchup, |
f50e0a01 | 1282 | |
f6200941 LP |
1283 | .serialize = device_serialize, |
1284 | .deserialize_item = device_deserialize_item, | |
1285 | ||
5cb5a6ff LP |
1286 | .dump = device_dump, |
1287 | ||
f50e0a01 | 1288 | .active_state = device_active_state, |
10a94420 | 1289 | .sub_state_to_string = device_sub_state_to_string, |
25ac040b | 1290 | |
a7f241db | 1291 | .following = device_following, |
6210e7fc | 1292 | .following_set = device_following_set, |
a7f241db | 1293 | |
f50e0a01 | 1294 | .enumerate = device_enumerate, |
c6918296 | 1295 | .shutdown = device_shutdown, |
f374631a | 1296 | .supported = udev_available, |
c6918296 MS |
1297 | |
1298 | .status_message_formats = { | |
1299 | .starting_stopping = { | |
1300 | [0] = "Expecting device %s...", | |
1301 | }, | |
1302 | .finished_start_job = { | |
1303 | [JOB_DONE] = "Found device %s.", | |
1304 | [JOB_TIMEOUT] = "Timed out waiting for device %s.", | |
1305 | }, | |
1306 | }, | |
5cb5a6ff | 1307 | }; |