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