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