]>
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 LP |
27 | static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = { |
28 | [DEVICE_DEAD] = UNIT_INACTIVE, | |
628c89cc LP |
29 | [DEVICE_TENTATIVE] = UNIT_ACTIVATING, |
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); |
66f3fdbb | 34 | static void device_update_found_one(Device *d, DeviceFound found, DeviceFound mask); |
718db961 | 35 | |
8fe914ec | 36 | static void device_unset_sysfs(Device *d) { |
f1421cc6 | 37 | Hashmap *devices; |
8fe914ec LP |
38 | Device *first; |
39 | ||
40 | assert(d); | |
41 | ||
a7f241db LP |
42 | if (!d->sysfs) |
43 | return; | |
44 | ||
45 | /* Remove this unit from the chain of devices which share the | |
46 | * same sysfs path. */ | |
f1421cc6 LP |
47 | devices = UNIT(d)->manager->devices_by_sysfs; |
48 | first = hashmap_get(devices, d->sysfs); | |
71fda00f | 49 | LIST_REMOVE(same_sysfs, first, d); |
8fe914ec | 50 | |
a7f241db | 51 | if (first) |
f1421cc6 | 52 | hashmap_remove_and_replace(devices, d->sysfs, first->sysfs, first); |
a7f241db | 53 | else |
f1421cc6 | 54 | hashmap_remove(devices, d->sysfs); |
a7f241db | 55 | |
a1e58e8e | 56 | d->sysfs = mfree(d->sysfs); |
8fe914ec LP |
57 | } |
58 | ||
628c89cc | 59 | static int device_set_sysfs(Device *d, const char *sysfs) { |
ccd419f0 | 60 | _cleanup_free_ char *copy = NULL; |
628c89cc | 61 | Device *first; |
628c89cc LP |
62 | int r; |
63 | ||
64 | assert(d); | |
65 | ||
66 | if (streq_ptr(d->sysfs, sysfs)) | |
67 | return 0; | |
68 | ||
548f6937 | 69 | r = hashmap_ensure_allocated(&UNIT(d)->manager->devices_by_sysfs, &path_hash_ops); |
628c89cc LP |
70 | if (r < 0) |
71 | return r; | |
72 | ||
73 | copy = strdup(sysfs); | |
74 | if (!copy) | |
75 | return -ENOMEM; | |
76 | ||
77 | device_unset_sysfs(d); | |
78 | ||
79 | first = hashmap_get(UNIT(d)->manager->devices_by_sysfs, sysfs); | |
80 | LIST_PREPEND(same_sysfs, first, d); | |
81 | ||
82 | r = hashmap_replace(UNIT(d)->manager->devices_by_sysfs, copy, first); | |
83 | if (r < 0) { | |
84 | LIST_REMOVE(same_sysfs, first, d); | |
628c89cc LP |
85 | return r; |
86 | } | |
87 | ||
ccd419f0 | 88 | d->sysfs = TAKE_PTR(copy); |
7c4d1394 MS |
89 | unit_add_to_dbus_queue(UNIT(d)); |
90 | ||
628c89cc LP |
91 | return 0; |
92 | } | |
93 | ||
faf919f1 LP |
94 | static void device_init(Unit *u) { |
95 | Device *d = DEVICE(u); | |
96 | ||
97 | assert(d); | |
1124fe6f | 98 | assert(UNIT(d)->load_state == UNIT_STUB); |
faf919f1 | 99 | |
8fe914ec LP |
100 | /* In contrast to all other unit types we timeout jobs waiting |
101 | * for devices by default. This is because they otherwise wait | |
35b8ca3a | 102 | * indefinitely for plugged in devices, something which cannot |
8fe914ec LP |
103 | * happen for the other units since their operations time out |
104 | * anyway. */ | |
d9732d78 | 105 | u->job_running_timeout = u->manager->default_timeout_start_usec; |
c8f4d764 | 106 | |
f1421cc6 | 107 | u->ignore_on_isolate = true; |
66f3fdbb LP |
108 | |
109 | d->deserialized_state = _DEVICE_STATE_INVALID; | |
faf919f1 LP |
110 | } |
111 | ||
87f0e418 LP |
112 | static void device_done(Unit *u) { |
113 | Device *d = DEVICE(u); | |
034c6ed7 LP |
114 | |
115 | assert(d); | |
e537352b | 116 | |
8fe914ec | 117 | device_unset_sysfs(d); |
88116909 | 118 | d->wants_property = strv_free(d->wants_property); |
e537352b LP |
119 | } |
120 | ||
1d4c6f5b ZJS |
121 | static int device_load(Unit *u) { |
122 | int r; | |
123 | ||
c3620770 | 124 | r = unit_load_fragment_and_dropin(u, false); |
1d4c6f5b ZJS |
125 | if (r < 0) |
126 | return r; | |
127 | ||
128 | if (!u->description) { | |
e41db484 LP |
129 | /* Generate a description based on the path, to be used until the device is initialized |
130 | properly */ | |
1d4c6f5b ZJS |
131 | r = unit_name_to_path(u->id, &u->description); |
132 | if (r < 0) | |
133 | log_unit_debug_errno(u, r, "Failed to unescape name: %m"); | |
134 | } | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
f50e0a01 LP |
139 | static void device_set_state(Device *d, DeviceState state) { |
140 | DeviceState old_state; | |
141 | assert(d); | |
5cb5a6ff | 142 | |
6fcbec6f LP |
143 | if (d->state != state) |
144 | bus_unit_send_pending_change_signal(UNIT(d), false); | |
145 | ||
f50e0a01 LP |
146 | old_state = d->state; |
147 | d->state = state; | |
5cb5a6ff | 148 | |
244f8055 LP |
149 | if (state == DEVICE_DEAD) |
150 | device_unset_sysfs(d); | |
151 | ||
e537352b | 152 | if (state != old_state) |
f2341e0a | 153 | log_unit_debug(UNIT(d), "Changed %s -> %s", device_state_to_string(old_state), device_state_to_string(state)); |
f50e0a01 | 154 | |
2ad2e41a | 155 | unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state], 0); |
f50e0a01 LP |
156 | } |
157 | ||
be847e82 | 158 | static int device_coldplug(Unit *u) { |
f50e0a01 LP |
159 | Device *d = DEVICE(u); |
160 | ||
161 | assert(d); | |
162 | assert(d->state == DEVICE_DEAD); | |
163 | ||
66f3fdbb | 164 | /* First, let's put the deserialized state and found mask into effect, if we have it. */ |
918e6f1c | 165 | |
66f3fdbb LP |
166 | if (d->deserialized_state < 0 || |
167 | (d->deserialized_state == d->state && | |
168 | d->deserialized_found == d->found)) | |
169 | return 0; | |
f50e0a01 | 170 | |
66f3fdbb LP |
171 | d->found = d->deserialized_found; |
172 | device_set_state(d, d->deserialized_state); | |
f50e0a01 LP |
173 | return 0; |
174 | } | |
175 | ||
66f3fdbb LP |
176 | static void device_catchup(Unit *u) { |
177 | Device *d = DEVICE(u); | |
178 | ||
179 | assert(d); | |
180 | ||
181 | /* Second, let's update the state with the enumerated state if it's different */ | |
182 | if (d->enumerated_found == d->found) | |
183 | return; | |
184 | ||
185 | device_update_found_one(d, d->enumerated_found, DEVICE_FOUND_MASK); | |
186 | } | |
187 | ||
75d0aba4 YW |
188 | static const struct { |
189 | DeviceFound flag; | |
190 | const char *name; | |
191 | } device_found_map[] = { | |
66f3fdbb LP |
192 | { DEVICE_FOUND_UDEV, "found-udev" }, |
193 | { DEVICE_FOUND_MOUNT, "found-mount" }, | |
194 | { DEVICE_FOUND_SWAP, "found-swap" }, | |
75d0aba4 YW |
195 | }; |
196 | ||
197 | static int device_found_to_string_many(DeviceFound flags, char **ret) { | |
198 | _cleanup_free_ char *s = NULL; | |
75d0aba4 YW |
199 | |
200 | assert(ret); | |
201 | ||
fe96c0f8 | 202 | for (size_t i = 0; i < ELEMENTSOF(device_found_map); i++) { |
6e0f878e | 203 | if (!FLAGS_SET(flags, device_found_map[i].flag)) |
75d0aba4 YW |
204 | continue; |
205 | ||
c2bc710b | 206 | if (!strextend_with_separator(&s, ",", device_found_map[i].name)) |
75d0aba4 YW |
207 | return -ENOMEM; |
208 | } | |
209 | ||
210 | *ret = TAKE_PTR(s); | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
215 | static int device_found_from_string_many(const char *name, DeviceFound *ret) { | |
216 | DeviceFound flags = 0; | |
217 | int r; | |
218 | ||
219 | assert(ret); | |
220 | ||
221 | for (;;) { | |
222 | _cleanup_free_ char *word = NULL; | |
223 | DeviceFound f = 0; | |
224 | unsigned i; | |
225 | ||
226 | r = extract_first_word(&name, &word, ",", 0); | |
227 | if (r < 0) | |
228 | return r; | |
229 | if (r == 0) | |
230 | break; | |
231 | ||
69ce73d1 | 232 | for (i = 0; i < ELEMENTSOF(device_found_map); i++) |
75d0aba4 YW |
233 | if (streq(word, device_found_map[i].name)) { |
234 | f = device_found_map[i].flag; | |
235 | break; | |
236 | } | |
237 | ||
238 | if (f == 0) | |
239 | return -EINVAL; | |
240 | ||
241 | flags |= f; | |
242 | } | |
243 | ||
244 | *ret = flags; | |
245 | return 0; | |
246 | } | |
247 | ||
f6200941 | 248 | static int device_serialize(Unit *u, FILE *f, FDSet *fds) { |
75d0aba4 | 249 | _cleanup_free_ char *s = NULL; |
f6200941 LP |
250 | Device *d = DEVICE(u); |
251 | ||
c73f413d | 252 | assert(d); |
f6200941 LP |
253 | assert(u); |
254 | assert(f); | |
255 | assert(fds); | |
256 | ||
d68c645b | 257 | (void) serialize_item(f, "state", device_state_to_string(d->state)); |
75d0aba4 | 258 | |
43ba7d71 | 259 | if (device_found_to_string_many(d->found, &s) >= 0) |
d68c645b | 260 | (void) serialize_item(f, "found", s); |
0108f6ec DM |
261 | |
262 | return 0; | |
f6200941 LP |
263 | } |
264 | ||
265 | static int device_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
266 | Device *d = DEVICE(u); | |
75d0aba4 | 267 | int r; |
f6200941 | 268 | |
c73f413d | 269 | assert(d); |
f6200941 LP |
270 | assert(u); |
271 | assert(key); | |
272 | assert(value); | |
273 | assert(fds); | |
274 | ||
275 | if (streq(key, "state")) { | |
276 | DeviceState state; | |
277 | ||
278 | state = device_state_from_string(value); | |
279 | if (state < 0) | |
8bb2c8c9 | 280 | log_unit_debug(u, "Failed to parse state value, ignoring: %s", value); |
f6200941 LP |
281 | else |
282 | d->deserialized_state = state; | |
918e6f1c FB |
283 | |
284 | } else if (streq(key, "found")) { | |
66f3fdbb | 285 | r = device_found_from_string_many(value, &d->deserialized_found); |
75d0aba4 | 286 | if (r < 0) |
5e1ee764 | 287 | log_unit_debug_errno(u, r, "Failed to parse found value '%s', ignoring: %m", value); |
918e6f1c | 288 | |
f6200941 | 289 | } else |
f2341e0a | 290 | log_unit_debug(u, "Unknown serialization key: %s", key); |
f6200941 LP |
291 | |
292 | return 0; | |
293 | } | |
294 | ||
f50e0a01 | 295 | static void device_dump(Unit *u, FILE *f, const char *prefix) { |
25ac040b | 296 | Device *d = DEVICE(u); |
4d86c235 | 297 | _cleanup_free_ char *s = NULL; |
5cb5a6ff | 298 | |
25ac040b | 299 | assert(d); |
5cb5a6ff | 300 | |
4d86c235 ZJS |
301 | (void) device_found_to_string_many(d->found, &s); |
302 | ||
5cb5a6ff | 303 | fprintf(f, |
25ac040b | 304 | "%sDevice State: %s\n" |
4d86c235 ZJS |
305 | "%sSysfs Path: %s\n" |
306 | "%sFound: %s\n", | |
a16e1123 | 307 | prefix, device_state_to_string(d->state), |
4d86c235 ZJS |
308 | prefix, strna(d->sysfs), |
309 | prefix, strna(s)); | |
88116909 | 310 | |
de010b0b YW |
311 | STRV_FOREACH(i, d->wants_property) |
312 | fprintf(f, "%sudev SYSTEMD_WANTS: %s\n", | |
313 | prefix, *i); | |
f50e0a01 LP |
314 | } |
315 | ||
44a6b1b6 | 316 | _pure_ static UnitActiveState device_active_state(Unit *u) { |
f50e0a01 LP |
317 | assert(u); |
318 | ||
319 | return state_translation_table[DEVICE(u)->state]; | |
25ac040b LP |
320 | } |
321 | ||
44a6b1b6 | 322 | _pure_ static const char *device_sub_state_to_string(Unit *u) { |
10a94420 LP |
323 | assert(u); |
324 | ||
a16e1123 | 325 | return device_state_to_string(DEVICE(u)->state); |
10a94420 LP |
326 | } |
327 | ||
4366e598 | 328 | static int device_update_description(Unit *u, sd_device *dev, const char *path) { |
1d4c6f5b ZJS |
329 | _cleanup_free_ char *j = NULL; |
330 | const char *model, *label, *desc; | |
628c89cc | 331 | int r; |
965e5c5d LP |
332 | |
333 | assert(u); | |
965e5c5d LP |
334 | assert(path); |
335 | ||
1d4c6f5b ZJS |
336 | desc = path; |
337 | ||
338 | if (dev && | |
339 | (sd_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE", &model) >= 0 || | |
340 | sd_device_get_property_value(dev, "ID_MODEL", &model) >= 0)) { | |
341 | desc = model; | |
965e5c5d LP |
342 | |
343 | /* Try to concatenate the device model string with a label, if there is one */ | |
4366e598 YW |
344 | if (sd_device_get_property_value(dev, "ID_FS_LABEL", &label) >= 0 || |
345 | sd_device_get_property_value(dev, "ID_PART_ENTRY_NAME", &label) >= 0 || | |
346 | sd_device_get_property_value(dev, "ID_PART_ENTRY_NUMBER", &label) >= 0) { | |
965e5c5d | 347 | |
1d4c6f5b | 348 | desc = j = strjoin(model, " ", label); |
ea8ec43b LP |
349 | if (!j) |
350 | return log_oom(); | |
1d4c6f5b ZJS |
351 | } |
352 | } | |
ea8ec43b | 353 | |
1d4c6f5b | 354 | r = unit_set_description(u, desc); |
628c89cc | 355 | if (r < 0) |
ea8ec43b | 356 | return log_unit_error_errno(u, r, "Failed to set device description: %m"); |
965e5c5d | 357 | |
ea8ec43b | 358 | return 0; |
965e5c5d LP |
359 | } |
360 | ||
4366e598 | 361 | static int device_add_udev_wants(Unit *u, sd_device *dev) { |
88116909 | 362 | _cleanup_strv_free_ char **added = NULL; |
de040543 | 363 | const char *wants, *property; |
88116909 | 364 | Device *d = DEVICE(u); |
965e5c5d LP |
365 | int r; |
366 | ||
88116909 | 367 | assert(d); |
965e5c5d LP |
368 | assert(dev); |
369 | ||
463d0d15 | 370 | property = MANAGER_IS_USER(u->manager) ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS"; |
de040543 | 371 | |
4366e598 YW |
372 | r = sd_device_get_property_value(dev, property, &wants); |
373 | if (r < 0) | |
de040543 LP |
374 | return 0; |
375 | ||
376 | for (;;) { | |
ceed8f0c | 377 | _cleanup_free_ char *word = NULL, *k = NULL; |
965e5c5d | 378 | |
4ec85141 | 379 | r = extract_first_word(&wants, &word, NULL, EXTRACT_UNQUOTE); |
ceed8f0c | 380 | if (r == 0) |
88116909 | 381 | break; |
ceed8f0c ZJS |
382 | if (r == -ENOMEM) |
383 | return log_oom(); | |
384 | if (r < 0) | |
dcebc9ba | 385 | return log_unit_error_errno(u, r, "Failed to parse property %s with value %s: %m", property, wants); |
965e5c5d | 386 | |
88116909 | 387 | if (unit_name_is_valid(word, UNIT_NAME_TEMPLATE) && d->sysfs) { |
dcebc9ba LP |
388 | _cleanup_free_ char *escaped = NULL; |
389 | ||
390 | /* If the unit name is specified as template, then automatically fill in the sysfs path of the | |
391 | * device as instance name, properly escaped. */ | |
392 | ||
88116909 | 393 | r = unit_name_path_escape(d->sysfs, &escaped); |
dcebc9ba | 394 | if (r < 0) |
88116909 | 395 | return log_unit_error_errno(u, r, "Failed to escape %s: %m", d->sysfs); |
dcebc9ba LP |
396 | |
397 | r = unit_name_replace_instance(word, escaped, &k); | |
398 | if (r < 0) | |
399 | return log_unit_error_errno(u, r, "Failed to build %s instance of template %s: %m", escaped, word); | |
400 | } else { | |
401 | /* If this is not a template, then let's mangle it so, that it becomes a valid unit name. */ | |
402 | ||
37cbc1d5 | 403 | r = unit_name_mangle(word, UNIT_NAME_MANGLE_WARN, &k); |
dcebc9ba LP |
404 | if (r < 0) |
405 | return log_unit_error_errno(u, r, "Failed to mangle unit name \"%s\": %m", word); | |
406 | } | |
965e5c5d | 407 | |
35d8c19a | 408 | r = unit_add_dependency_by_name(u, UNIT_WANTS, k, true, UNIT_DEPENDENCY_UDEV); |
965e5c5d | 409 | if (r < 0) |
de040543 | 410 | return log_unit_error_errno(u, r, "Failed to add Wants= dependency: %m"); |
88116909 | 411 | |
47e72170 | 412 | r = strv_consume(&added, TAKE_PTR(k)); |
88116909 LP |
413 | if (r < 0) |
414 | return log_oom(); | |
965e5c5d | 415 | } |
88116909 | 416 | |
de010b0b | 417 | if (d->state != DEVICE_DEAD) |
88116909 LP |
418 | /* So here's a special hack, to compensate for the fact that the udev database's reload cycles are not |
419 | * synchronized with our own reload cycles: when we detect that the SYSTEMD_WANTS property of a device | |
420 | * changes while the device unit is already up, let's manually trigger any new units listed in it not | |
5238e957 | 421 | * seen before. This typically happens during the boot-time switch root transition, as udev devices |
88116909 LP |
422 | * will generally already be up in the initrd, but SYSTEMD_WANTS properties get then added through udev |
423 | * rules only available on the host system, and thus only when the initial udev coldplug trigger runs. | |
424 | * | |
425 | * We do this only if the device has been up already when we parse this, as otherwise the usual | |
426 | * dependency logic that is run from the dead → plugged transition will trigger these deps. */ | |
88116909 LP |
427 | STRV_FOREACH(i, added) { |
428 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
429 | ||
430 | if (strv_contains(d->wants_property, *i)) /* Was this unit already listed before? */ | |
431 | continue; | |
432 | ||
50cbaba4 | 433 | r = manager_add_job_by_name(u->manager, JOB_START, *i, JOB_FAIL, NULL, &error, NULL); |
88116909 LP |
434 | if (r < 0) |
435 | log_unit_warning_errno(u, r, "Failed to enqueue SYSTEMD_WANTS= job, ignoring: %s", bus_error_message(&error, r)); | |
436 | } | |
88116909 | 437 | |
8cc53fae | 438 | return strv_free_and_replace(d->wants_property, added); |
965e5c5d LP |
439 | } |
440 | ||
4366e598 | 441 | static bool device_is_bound_by_mounts(Device *d, sd_device *dev) { |
40b1a32c | 442 | int r; |
ebc8968b FB |
443 | |
444 | assert(d); | |
445 | assert(dev); | |
446 | ||
4212fa83 YW |
447 | r = device_get_property_bool(dev, "SYSTEMD_MOUNT_DEVICE_BOUND"); |
448 | if (r < 0 && r != -ENOENT) | |
449 | log_device_warning_errno(dev, r, "Failed to parse SYSTEMD_MOUNT_DEVICE_BOUND= udev property, ignoring: %m"); | |
ebc8968b | 450 | |
4212fa83 | 451 | d->bind_mounts = r > 0; |
40b1a32c LP |
452 | |
453 | return d->bind_mounts; | |
ebc8968b FB |
454 | } |
455 | ||
86cdffd1 | 456 | static void device_upgrade_mount_deps(Unit *u) { |
ebc8968b | 457 | Unit *other; |
eef85c4a | 458 | void *v; |
ebc8968b FB |
459 | int r; |
460 | ||
eef85c4a LP |
461 | /* Let's upgrade Requires= to BindsTo= on us. (Used when SYSTEMD_MOUNT_DEVICE_BOUND is set) */ |
462 | ||
15ed3c3a | 463 | HASHMAP_FOREACH_KEY(v, other, unit_get_dependencies(u, UNIT_REQUIRED_BY)) { |
ebc8968b FB |
464 | if (other->type != UNIT_MOUNT) |
465 | continue; | |
466 | ||
eef85c4a | 467 | r = unit_add_dependency(other, UNIT_BINDS_TO, u, true, UNIT_DEPENDENCY_UDEV); |
ebc8968b | 468 | if (r < 0) |
86cdffd1 | 469 | log_unit_warning_errno(u, r, "Failed to add BindsTo= dependency between device and mount unit, ignoring: %m"); |
ebc8968b | 470 | } |
ebc8968b FB |
471 | } |
472 | ||
4366e598 | 473 | static int device_setup_unit(Manager *m, sd_device *dev, const char *path, bool main) { |
dd309fcd | 474 | _cleanup_(unit_freep) Unit *new_unit = NULL; |
628c89cc | 475 | _cleanup_free_ char *e = NULL; |
2005219f | 476 | const char *sysfs = NULL; |
dd309fcd | 477 | Unit *u; |
965e5c5d | 478 | int r; |
25ac040b LP |
479 | |
480 | assert(m); | |
965e5c5d | 481 | assert(path); |
25ac040b | 482 | |
2005219f | 483 | if (dev) { |
4366e598 | 484 | r = sd_device_get_syspath(dev, &sysfs); |
9951c8df LP |
485 | if (r < 0) |
486 | return log_device_debug_errno(dev, r, "Couldn't get syspath from device, ignoring: %m"); | |
2005219f | 487 | } |
7f275a9f | 488 | |
7410616c | 489 | r = unit_name_from_path(path, ".device", &e); |
a7fb1f2e | 490 | if (r < 0) |
ad172d19 | 491 | return log_struct_errno( |
a7fb1f2e | 492 | LOG_WARNING, r, |
ad172d19 LP |
493 | "MESSAGE_ID=" SD_MESSAGE_DEVICE_PATH_NOT_SUITABLE_STR, |
494 | "DEVICE=%s", path, | |
495 | LOG_MESSAGE("Failed to generate valid unit name from device path '%s', ignoring device: %m", path)); | |
628c89cc LP |
496 | |
497 | u = manager_get_unit(m, e); | |
38b9b72e | 498 | if (u) { |
66f3fdbb LP |
499 | /* The device unit can still be present even if the device was unplugged: a mount unit can reference it |
500 | * hence preventing the GC to have garbaged it. That's desired since the device unit may have a | |
501 | * dependency on the mount unit which was added during the loading of the later. When the device is | |
502 | * plugged the sysfs might not be initialized yet, as we serialize the device's state but do not | |
503 | * serialize the sysfs path across reloads/reexecs. Hence, when coming back from a reload/restart we | |
504 | * might have the state valid, but not the sysfs path. Hence, let's filter out conflicting devices, but | |
505 | * let's accept devices in any state with no sysfs path set. */ | |
506 | ||
507 | if (DEVICE(u)->state == DEVICE_PLUGGED && | |
508 | DEVICE(u)->sysfs && | |
509 | sysfs && | |
d85ff944 YW |
510 | !path_equal(DEVICE(u)->sysfs, sysfs)) |
511 | return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EEXIST), | |
512 | "Device %s appeared twice with different sysfs paths %s and %s, ignoring the latter.", | |
513 | e, DEVICE(u)->sysfs, sysfs); | |
25ac040b | 514 | |
5238e957 | 515 | /* Let's remove all dependencies generated due to udev properties. We'll re-add whatever is configured |
38b9b72e LP |
516 | * now below. */ |
517 | unit_remove_dependencies(u, UNIT_DEPENDENCY_UDEV); | |
dd309fcd | 518 | |
38b9b72e | 519 | } else { |
dd309fcd YW |
520 | r = unit_new_for_name(m, sizeof(Device), e, &new_unit); |
521 | if (r < 0) | |
522 | return log_device_error_errno(dev, r, "Failed to allocate device unit %s: %m", e); | |
aab14b13 | 523 | |
dd309fcd | 524 | u = new_unit; |
25ac040b | 525 | |
ee6cb288 | 526 | unit_add_to_load_queue(u); |
38b9b72e | 527 | } |
ee6cb288 | 528 | |
66f3fdbb | 529 | /* If this was created via some dependency and has not actually been seen yet ->sysfs will not be |
ee6cb288 | 530 | * initialized. Hence initialize it if necessary. */ |
2005219f MP |
531 | if (sysfs) { |
532 | r = device_set_sysfs(DEVICE(u), sysfs); | |
dd309fcd YW |
533 | if (r < 0) |
534 | return log_unit_error_errno(u, r, "Failed to set sysfs path %s: %m", sysfs); | |
ee6cb288 | 535 | |
66f3fdbb | 536 | /* The additional systemd udev properties we only interpret for the main object */ |
2005219f MP |
537 | if (main) |
538 | (void) device_add_udev_wants(u, dev); | |
539 | } | |
25ac040b | 540 | |
1d4c6f5b ZJS |
541 | (void) device_update_description(u, dev, path); |
542 | ||
75a50eb0 LP |
543 | /* So the user wants the mount units to be bound to the device but a mount unit might has been seen |
544 | * by systemd before the device appears on its radar. In this case the device unit is partially | |
545 | * initialized and includes the deps on the mount unit but at that time the "bind mounts" flag wasn't | |
546 | * present. Fix this up now. */ | |
40b1a32c | 547 | if (dev && device_is_bound_by_mounts(DEVICE(u), dev)) |
ebc8968b | 548 | device_upgrade_mount_deps(u); |
f94ea366 | 549 | |
dd309fcd | 550 | TAKE_PTR(new_unit); |
25ac040b | 551 | return 0; |
25ac040b LP |
552 | } |
553 | ||
75a50eb0 | 554 | static void device_process_new(Manager *m, sd_device *dev) { |
f1421cc6 | 555 | const char *sysfs, *dn, *alias; |
4366e598 | 556 | dev_t devnum; |
003ac9d0 | 557 | int r; |
8fe914ec LP |
558 | |
559 | assert(m); | |
560 | ||
4366e598 | 561 | if (sd_device_get_syspath(dev, &sysfs) < 0) |
75a50eb0 | 562 | return; |
8fe914ec | 563 | |
75a50eb0 LP |
564 | /* Add the main unit named after the sysfs path. If this one fails, don't bother with the rest, as |
565 | * this one shall be the main device unit the others just follow. (Compare with how | |
566 | * device_following() is implemented, see below, which looks for the sysfs device.) */ | |
567 | if (device_setup_unit(m, dev, sysfs, true) < 0) | |
568 | return; | |
8fe914ec LP |
569 | |
570 | /* Add an additional unit for the device node */ | |
4366e598 | 571 | if (sd_device_get_devname(dev, &dn) >= 0) |
628c89cc | 572 | (void) device_setup_unit(m, dev, dn, false); |
8fe914ec LP |
573 | |
574 | /* Add additional units for all symlinks */ | |
4366e598 | 575 | if (sd_device_get_devnum(dev, &devnum) >= 0) { |
8fe914ec LP |
576 | const char *p; |
577 | ||
4366e598 YW |
578 | FOREACH_DEVICE_DEVLINK(dev, p) { |
579 | struct stat st; | |
8fe914ec | 580 | |
4366e598 YW |
581 | if (PATH_STARTSWITH_SET(p, "/dev/block/", "/dev/char/")) |
582 | continue; | |
8fe914ec | 583 | |
75a50eb0 LP |
584 | /* Verify that the symlink in the FS actually belongs to this device. This is useful |
585 | * to deal with conflicting devices, e.g. when two disks want the same | |
586 | * /dev/disk/by-label/xxx link because they have the same label. We want to make sure | |
587 | * that the same device that won the symlink wins in systemd, so we check the device | |
588 | * node major/minor */ | |
4366e598 YW |
589 | if (stat(p, &st) >= 0 && |
590 | ((!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) || | |
591 | st.st_rdev != devnum)) | |
5845b46b LP |
592 | continue; |
593 | ||
4366e598 YW |
594 | (void) device_setup_unit(m, dev, p, false); |
595 | } | |
8fe914ec LP |
596 | } |
597 | ||
4366e598 YW |
598 | /* Add additional units for all explicitly configured aliases */ |
599 | if (sd_device_get_property_value(dev, "SYSTEMD_ALIAS", &alias) < 0) | |
75a50eb0 | 600 | return; |
4366e598 | 601 | |
ceed8f0c | 602 | for (;;) { |
8fb242ab | 603 | _cleanup_free_ char *word = NULL; |
8fe914ec | 604 | |
4ec85141 | 605 | r = extract_first_word(&alias, &word, NULL, EXTRACT_UNQUOTE); |
ceed8f0c | 606 | if (r == 0) |
8bb2c8c9 | 607 | break; |
ceed8f0c | 608 | if (r == -ENOMEM) |
75a50eb0 | 609 | return (void) log_oom(); |
ceed8f0c | 610 | if (r < 0) |
75a50eb0 | 611 | return (void) log_device_warning_errno(dev, r, "Failed to parse SYSTEMD_ALIAS property, ignoring: %m"); |
f1421cc6 | 612 | |
a7f8be01 | 613 | if (!path_is_absolute(word)) |
71f79b56 | 614 | log_device_warning(dev, "SYSTEMD_ALIAS is not an absolute path, ignoring: %s", word); |
a7f8be01 | 615 | else if (!path_is_normalized(word)) |
71f79b56 | 616 | log_device_warning(dev, "SYSTEMD_ALIAS is not a normalized path, ignoring: %s", word); |
a7f8be01 LP |
617 | else |
618 | (void) device_setup_unit(m, dev, word, false); | |
8fe914ec | 619 | } |
8fe914ec LP |
620 | } |
621 | ||
66f3fdbb | 622 | static void device_found_changed(Device *d, DeviceFound previous, DeviceFound now) { |
628c89cc LP |
623 | assert(d); |
624 | ||
4b58153d | 625 | /* Didn't exist before, but does now? if so, generate a new invocation ID for it */ |
66f3fdbb | 626 | if (previous == DEVICE_NOT_FOUND && now != DEVICE_NOT_FOUND) |
4b58153d LP |
627 | (void) unit_acquire_invocation_id(UNIT(d)); |
628 | ||
66f3fdbb LP |
629 | if (FLAGS_SET(now, DEVICE_FOUND_UDEV)) |
630 | /* When the device is known to udev we consider it plugged. */ | |
f6200941 | 631 | device_set_state(d, DEVICE_PLUGGED); |
66f3fdbb LP |
632 | else if (now != DEVICE_NOT_FOUND && !FLAGS_SET(previous, DEVICE_FOUND_UDEV)) |
633 | /* If the device has not been seen by udev yet, but is now referenced by the kernel, then we assume the | |
f6200941 LP |
634 | * kernel knows it now, and udev might soon too. */ |
635 | device_set_state(d, DEVICE_TENTATIVE); | |
244f8055 | 636 | else |
66f3fdbb LP |
637 | /* If nobody sees the device, or if the device was previously seen by udev and now is only referenced |
638 | * from the kernel, then we consider the device is gone, the kernel just hasn't noticed it yet. */ | |
f6200941 | 639 | device_set_state(d, DEVICE_DEAD); |
628c89cc LP |
640 | } |
641 | ||
66f3fdbb | 642 | static void device_update_found_one(Device *d, DeviceFound found, DeviceFound mask) { |
c6e892bc YW |
643 | Manager *m; |
644 | ||
66f3fdbb LP |
645 | assert(d); |
646 | ||
c6e892bc YW |
647 | m = UNIT(d)->manager; |
648 | ||
649 | if (MANAGER_IS_RUNNING(m) && (m->honor_device_enumeration || MANAGER_IS_USER(m))) { | |
66f3fdbb LP |
650 | DeviceFound n, previous; |
651 | ||
652 | /* When we are already running, then apply the new mask right-away, and trigger state changes | |
653 | * right-away */ | |
654 | ||
655 | n = (d->found & ~mask) | (found & mask); | |
656 | if (n == d->found) | |
657 | return; | |
658 | ||
659 | previous = d->found; | |
660 | d->found = n; | |
661 | ||
662 | device_found_changed(d, previous, n); | |
663 | } else | |
664 | /* We aren't running yet, let's apply the new mask to the shadow variable instead, which we'll apply as | |
665 | * soon as we catch-up with the state. */ | |
666 | d->enumerated_found = (d->enumerated_found & ~mask) | (found & mask); | |
667 | } | |
668 | ||
485ae697 | 669 | static void device_update_found_by_sysfs(Manager *m, const char *sysfs, DeviceFound found, DeviceFound mask) { |
03677889 | 670 | Device *l; |
25ac040b LP |
671 | |
672 | assert(m); | |
628c89cc | 673 | assert(sysfs); |
25ac040b | 674 | |
485ae697 LP |
675 | if (mask == 0) |
676 | return; | |
25ac040b | 677 | |
f1421cc6 | 678 | l = hashmap_get(m->devices_by_sysfs, sysfs); |
80a226b2 | 679 | LIST_FOREACH(same_sysfs, d, l) |
485ae697 | 680 | device_update_found_one(d, found, mask); |
25ac040b LP |
681 | } |
682 | ||
68695ce4 | 683 | static void device_update_found_by_name(Manager *m, const char *path, DeviceFound found, DeviceFound mask) { |
628c89cc LP |
684 | _cleanup_free_ char *e = NULL; |
685 | Unit *u; | |
7410616c | 686 | int r; |
f94ea366 LP |
687 | |
688 | assert(m); | |
628c89cc | 689 | assert(path); |
f94ea366 | 690 | |
485ae697 | 691 | if (mask == 0) |
68695ce4 | 692 | return; |
f94ea366 | 693 | |
7410616c LP |
694 | r = unit_name_from_path(path, ".device", &e); |
695 | if (r < 0) | |
68695ce4 | 696 | return (void) log_debug_errno(r, "Failed to generate unit name from device path, ignoring: %m"); |
f94ea366 | 697 | |
628c89cc LP |
698 | u = manager_get_unit(m, e); |
699 | if (!u) | |
68695ce4 | 700 | return; |
628c89cc | 701 | |
485ae697 | 702 | device_update_found_one(DEVICE(u), found, mask); |
f94ea366 LP |
703 | } |
704 | ||
4366e598 | 705 | static bool device_is_ready(sd_device *dev) { |
f1421cc6 LP |
706 | assert(dev); |
707 | ||
2efa43dc YW |
708 | if (device_is_renaming(dev) > 0) |
709 | return false; | |
710 | ||
242c1c07 LP |
711 | /* Is it really tagged as 'systemd' right now? */ |
712 | if (sd_device_has_current_tag(dev, "systemd") <= 0) | |
713 | return false; | |
714 | ||
4212fa83 | 715 | return device_get_property_bool(dev, "SYSTEMD_READY") != 0; |
f1421cc6 LP |
716 | } |
717 | ||
a7f241db LP |
718 | static Unit *device_following(Unit *u) { |
719 | Device *d = DEVICE(u); | |
03677889 | 720 | Device *first = NULL; |
a7f241db LP |
721 | |
722 | assert(d); | |
723 | ||
ac155bb8 | 724 | if (startswith(u->id, "sys-")) |
a7f241db LP |
725 | return NULL; |
726 | ||
727 | /* Make everybody follow the unit that's named after the sysfs path */ | |
bd335c96 | 728 | LIST_FOREACH(same_sysfs, other, d->same_sysfs_next) |
1124fe6f | 729 | if (startswith(UNIT(other)->id, "sys-")) |
a7f241db LP |
730 | return UNIT(other); |
731 | ||
bd335c96 | 732 | LIST_FOREACH_BACKWARDS(same_sysfs, other, d->same_sysfs_prev) { |
1124fe6f | 733 | if (startswith(UNIT(other)->id, "sys-")) |
a7f241db LP |
734 | return UNIT(other); |
735 | ||
736 | first = other; | |
737 | } | |
738 | ||
739 | return UNIT(first); | |
740 | } | |
741 | ||
f1421cc6 | 742 | static int device_following_set(Unit *u, Set **_set) { |
03677889 | 743 | Device *d = DEVICE(u); |
af4fa99d | 744 | _cleanup_set_free_ Set *set = NULL; |
6210e7fc LP |
745 | int r; |
746 | ||
747 | assert(d); | |
f1421cc6 | 748 | assert(_set); |
6210e7fc | 749 | |
f1421cc6 LP |
750 | if (LIST_JUST_US(same_sysfs, d)) { |
751 | *_set = NULL; | |
6210e7fc LP |
752 | return 0; |
753 | } | |
754 | ||
d5099efc | 755 | set = set_new(NULL); |
f1421cc6 | 756 | if (!set) |
6210e7fc LP |
757 | return -ENOMEM; |
758 | ||
bd335c96 | 759 | LIST_FOREACH(same_sysfs, other, d->same_sysfs_next) { |
f1421cc6 LP |
760 | r = set_put(set, other); |
761 | if (r < 0) | |
95f14a3e | 762 | return r; |
f1421cc6 | 763 | } |
6210e7fc | 764 | |
bd335c96 | 765 | LIST_FOREACH_BACKWARDS(same_sysfs, other, d->same_sysfs_prev) { |
f1421cc6 LP |
766 | r = set_put(set, other); |
767 | if (r < 0) | |
95f14a3e | 768 | return r; |
f1421cc6 | 769 | } |
6210e7fc | 770 | |
95f14a3e | 771 | *_set = TAKE_PTR(set); |
6210e7fc | 772 | return 1; |
6210e7fc LP |
773 | } |
774 | ||
25ac040b LP |
775 | static void device_shutdown(Manager *m) { |
776 | assert(m); | |
777 | ||
d0955f00 | 778 | m->device_monitor = sd_device_monitor_unref(m->device_monitor); |
525d3cc7 | 779 | m->devices_by_sysfs = hashmap_free(m->devices_by_sysfs); |
25ac040b LP |
780 | } |
781 | ||
ba64af90 | 782 | static void device_enumerate(Manager *m) { |
4366e598 YW |
783 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
784 | sd_device *dev; | |
718db961 | 785 | int r; |
25ac040b LP |
786 | |
787 | assert(m); | |
788 | ||
d0955f00 YW |
789 | if (!m->device_monitor) { |
790 | r = sd_device_monitor_new(&m->device_monitor); | |
791 | if (r < 0) { | |
792 | log_error_errno(r, "Failed to allocate device monitor: %m"); | |
a16e1123 LP |
793 | goto fail; |
794 | } | |
f94ea366 | 795 | |
47ae6e67 LP |
796 | /* This will fail if we are unprivileged, but that |
797 | * should not matter much, as user instances won't run | |
798 | * during boot. */ | |
d0955f00 | 799 | (void) sd_device_monitor_set_receive_buffer_size(m->device_monitor, 128*1024*1024); |
99448c1f | 800 | |
d0955f00 | 801 | r = sd_device_monitor_filter_add_match_tag(m->device_monitor, "systemd"); |
ba64af90 LP |
802 | if (r < 0) { |
803 | log_error_errno(r, "Failed to add udev tag match: %m"); | |
e1ce2c27 | 804 | goto fail; |
ba64af90 | 805 | } |
e1ce2c27 | 806 | |
deb2b734 | 807 | r = sd_device_monitor_attach_event(m->device_monitor, m->event); |
ba64af90 | 808 | if (r < 0) { |
d0955f00 | 809 | log_error_errno(r, "Failed to attach event to device monitor: %m"); |
a16e1123 | 810 | goto fail; |
ba64af90 | 811 | } |
f94ea366 | 812 | |
deb2b734 | 813 | r = sd_device_monitor_start(m->device_monitor, device_dispatch_io, m); |
ba64af90 | 814 | if (r < 0) { |
d0955f00 | 815 | log_error_errno(r, "Failed to start device monitor: %m"); |
718db961 | 816 | goto fail; |
ba64af90 | 817 | } |
a16e1123 | 818 | } |
f94ea366 | 819 | |
4366e598 | 820 | r = sd_device_enumerator_new(&e); |
ba64af90 | 821 | if (r < 0) { |
9b674e25 | 822 | log_error_errno(r, "Failed to allocate device enumerator: %m"); |
e1ce2c27 | 823 | goto fail; |
ba64af90 | 824 | } |
25ac040b | 825 | |
4366e598 | 826 | r = sd_device_enumerator_add_match_tag(e, "systemd"); |
ba64af90 | 827 | if (r < 0) { |
4366e598 | 828 | log_error_errno(r, "Failed to set tag for device enumeration: %m"); |
e1202047 | 829 | goto fail; |
ba64af90 | 830 | } |
e1202047 | 831 | |
8437c059 | 832 | FOREACH_DEVICE(e, dev) { |
628c89cc LP |
833 | const char *sysfs; |
834 | ||
628c89cc LP |
835 | if (!device_is_ready(dev)) |
836 | continue; | |
837 | ||
75a50eb0 | 838 | device_process_new(m, dev); |
4366e598 YW |
839 | |
840 | if (sd_device_get_syspath(dev, &sysfs) < 0) | |
841 | continue; | |
842 | ||
66f3fdbb | 843 | device_update_found_by_sysfs(m, sysfs, DEVICE_FOUND_UDEV, DEVICE_FOUND_UDEV); |
628c89cc | 844 | } |
25ac040b | 845 | |
ba64af90 | 846 | return; |
25ac040b LP |
847 | |
848 | fail: | |
25ac040b | 849 | device_shutdown(m); |
5cb5a6ff LP |
850 | } |
851 | ||
87934b36 | 852 | static void device_propagate_reload_by_sysfs(Manager *m, const char *sysfs) { |
03677889 | 853 | Device *l; |
87934b36 LP |
854 | int r; |
855 | ||
856 | assert(m); | |
857 | assert(sysfs); | |
858 | ||
859 | l = hashmap_get(m->devices_by_sysfs, sysfs); | |
80a226b2 | 860 | LIST_FOREACH(same_sysfs, d, l) { |
87934b36 LP |
861 | if (d->state == DEVICE_DEAD) |
862 | continue; | |
863 | ||
864 | r = manager_propagate_reload(m, UNIT(d), JOB_REPLACE, NULL); | |
865 | if (r < 0) | |
866 | log_warning_errno(r, "Failed to propagate reload, ignoring: %m"); | |
867 | } | |
868 | } | |
869 | ||
c8ad151a | 870 | static void device_remove_old_on_move(Manager *m, sd_device *dev) { |
42ebcebf | 871 | _cleanup_free_ char *syspath_old = NULL; |
87bc687a YW |
872 | const char *devpath_old; |
873 | int r; | |
874 | ||
42ebcebf YW |
875 | assert(m); |
876 | assert(dev); | |
877 | ||
87bc687a | 878 | r = sd_device_get_property_value(dev, "DEVPATH_OLD", &devpath_old); |
c8ad151a LP |
879 | if (r < 0) |
880 | return (void) log_device_debug_errno(dev, r, "Failed to get DEVPATH_OLD= property on 'move' uevent, ignoring: %m"); | |
87bc687a YW |
881 | |
882 | syspath_old = path_join("/sys", devpath_old); | |
883 | if (!syspath_old) | |
c8ad151a | 884 | return (void) log_oom(); |
87bc687a | 885 | |
87bc687a | 886 | device_update_found_by_sysfs(m, syspath_old, 0, DEVICE_FOUND_UDEV|DEVICE_FOUND_MOUNT|DEVICE_FOUND_SWAP); |
87bc687a YW |
887 | } |
888 | ||
d0955f00 | 889 | static int device_dispatch_io(sd_device_monitor *monitor, sd_device *dev, void *userdata) { |
a1130022 | 890 | sd_device_action_t action; |
718db961 | 891 | Manager *m = userdata; |
a7395c86 | 892 | const char *sysfs; |
718db961 | 893 | int r; |
f94ea366 LP |
894 | |
895 | assert(m); | |
d0955f00 | 896 | assert(dev); |
f94ea366 | 897 | |
4366e598 YW |
898 | r = sd_device_get_syspath(dev, &sysfs); |
899 | if (r < 0) { | |
71f79b56 | 900 | log_device_error_errno(dev, r, "Failed to get device sys path: %m"); |
628c89cc LP |
901 | return 0; |
902 | } | |
903 | ||
a1130022 | 904 | r = sd_device_get_action(dev, &action); |
4366e598 | 905 | if (r < 0) { |
a7395c86 | 906 | log_device_error_errno(dev, r, "Failed to get udev action: %m"); |
718db961 | 907 | return 0; |
f94ea366 LP |
908 | } |
909 | ||
a1130022 | 910 | if (!IN_SET(action, SD_DEVICE_ADD, SD_DEVICE_REMOVE, SD_DEVICE_MOVE)) |
87934b36 | 911 | device_propagate_reload_by_sysfs(m, sysfs); |
f332611a | 912 | |
a1130022 | 913 | if (action == SD_DEVICE_MOVE) |
c8ad151a | 914 | device_remove_old_on_move(m, dev); |
87bc687a | 915 | |
ae6ad21e LP |
916 | /* A change event can signal that a device is becoming ready, in particular if the device is using |
917 | * the SYSTEMD_READY logic in udev so we need to reach the else block of the following if, even for | |
918 | * change events */ | |
a1130022 | 919 | if (action == SD_DEVICE_REMOVE) { |
628c89cc | 920 | r = swap_process_device_remove(m, dev); |
9670d583 | 921 | if (r < 0) |
71f79b56 | 922 | log_device_warning_errno(dev, r, "Failed to process swap device remove event, ignoring: %m"); |
9670d583 | 923 | |
ae6ad21e LP |
924 | /* If we get notified that a device was removed by udev, then it's completely gone, hence |
925 | * unset all found bits */ | |
485ae697 | 926 | device_update_found_by_sysfs(m, sysfs, 0, DEVICE_FOUND_UDEV|DEVICE_FOUND_MOUNT|DEVICE_FOUND_SWAP); |
f1421cc6 | 927 | |
628c89cc LP |
928 | } else if (device_is_ready(dev)) { |
929 | ||
75a50eb0 | 930 | device_process_new(m, dev); |
628c89cc LP |
931 | |
932 | r = swap_process_device_new(m, dev); | |
9670d583 | 933 | if (r < 0) |
71f79b56 | 934 | log_device_warning_errno(dev, r, "Failed to process swap device new event, ignoring: %m"); |
9670d583 | 935 | |
f1421cc6 LP |
936 | manager_dispatch_load_queue(m); |
937 | ||
628c89cc | 938 | /* The device is found now, set the udev found bit */ |
485ae697 | 939 | device_update_found_by_sysfs(m, sysfs, DEVICE_FOUND_UDEV, DEVICE_FOUND_UDEV); |
ae6ad21e LP |
940 | } else |
941 | /* The device is nominally around, but not ready for us. Hence unset the udev bit, but leave | |
942 | * the rest around. */ | |
485ae697 | 943 | device_update_found_by_sysfs(m, sysfs, 0, DEVICE_FOUND_UDEV); |
f94ea366 | 944 | |
718db961 | 945 | return 0; |
f94ea366 LP |
946 | } |
947 | ||
0e38cee8 YW |
948 | static int validate_node(const char *node, sd_device **ret) { |
949 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; | |
e5ca27b7 | 950 | int r; |
628c89cc | 951 | |
34c20ee0 LP |
952 | assert(node); |
953 | assert(ret); | |
954 | ||
955 | /* Validates a device node that showed up in /proc/swaps or /proc/self/mountinfo if it makes sense for us to | |
956 | * track. Note that this validator is fine within missing device nodes, but not with badly set up ones! */ | |
957 | ||
0e38cee8 YW |
958 | r = sd_device_new_from_devname(&dev, node); |
959 | if (r == -ENODEV) { | |
34c20ee0 | 960 | *ret = NULL; |
0e38cee8 | 961 | return 0; /* good! (though missing) */ |
34c20ee0 | 962 | } |
0e38cee8 YW |
963 | if (r < 0) |
964 | return r; /* bad! */ | |
34c20ee0 | 965 | |
0e38cee8 YW |
966 | *ret = TAKE_PTR(dev); |
967 | return 1; /* good! */ | |
34c20ee0 LP |
968 | } |
969 | ||
970 | void device_found_node(Manager *m, const char *node, DeviceFound found, DeviceFound mask) { | |
628c89cc LP |
971 | assert(m); |
972 | assert(node); | |
03a94b73 | 973 | assert(!FLAGS_SET(mask, DEVICE_FOUND_UDEV)); |
628c89cc | 974 | |
f374631a | 975 | if (!udev_available()) |
485ae697 | 976 | return; |
4c6d20de | 977 | |
34c20ee0 LP |
978 | if (mask == 0) |
979 | return; | |
980 | ||
981 | /* This is called whenever we find a device referenced in /proc/swaps or /proc/self/mounts. Such a device might | |
982 | * be mounted/enabled at a time where udev has not finished probing it yet, and we thus haven't learned about | |
66f3fdbb LP |
983 | * it yet. In this case we will set the device unit to "tentative" state. |
984 | * | |
985 | * This takes a pair of DeviceFound flags parameters. The 'mask' parameter is a bit mask that indicates which | |
986 | * bits of 'found' to copy into the per-device DeviceFound flags field. Thus, this function may be used to set | |
987 | * and unset individual bits in a single call, while merging partially with previous state. */ | |
628c89cc | 988 | |
485ae697 | 989 | if ((found & mask) != 0) { |
4366e598 | 990 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
628c89cc | 991 | |
34c20ee0 LP |
992 | /* If the device is known in the kernel and newly appeared, then we'll create a device unit for it, |
993 | * under the name referenced in /proc/swaps or /proc/self/mountinfo. But first, let's validate if | |
994 | * everything is alright with the device node. */ | |
628c89cc | 995 | |
0e38cee8 | 996 | if (validate_node(node, &dev) < 0) |
34c20ee0 | 997 | return; /* Don't create a device unit for this if the device node is borked. */ |
628c89cc | 998 | |
0e38cee8 | 999 | (void) device_setup_unit(m, dev, node, false); /* 'dev' may be NULL. */ |
628c89cc LP |
1000 | } |
1001 | ||
1002 | /* Update the device unit's state, should it exist */ | |
485ae697 | 1003 | (void) device_update_found_by_name(m, node, found, mask); |
628c89cc LP |
1004 | } |
1005 | ||
ebc8968b | 1006 | bool device_shall_be_bound_by(Unit *device, Unit *u) { |
8bb2c8c9 LP |
1007 | assert(device); |
1008 | assert(u); | |
ebc8968b FB |
1009 | |
1010 | if (u->type != UNIT_MOUNT) | |
1011 | return false; | |
1012 | ||
1013 | return DEVICE(device)->bind_mounts; | |
1014 | } | |
1015 | ||
87f0e418 | 1016 | const UnitVTable device_vtable = { |
7d17cfbc | 1017 | .object_size = sizeof(Device), |
f975e971 LP |
1018 | .sections = |
1019 | "Unit\0" | |
1020 | "Device\0" | |
1021 | "Install\0", | |
5cb5a6ff | 1022 | |
c5a97ed1 LP |
1023 | .gc_jobs = true, |
1024 | ||
faf919f1 | 1025 | .init = device_init, |
034c6ed7 | 1026 | .done = device_done, |
1d4c6f5b | 1027 | .load = device_load, |
718db961 | 1028 | |
f50e0a01 | 1029 | .coldplug = device_coldplug, |
66f3fdbb | 1030 | .catchup = device_catchup, |
f50e0a01 | 1031 | |
f6200941 LP |
1032 | .serialize = device_serialize, |
1033 | .deserialize_item = device_deserialize_item, | |
1034 | ||
5cb5a6ff LP |
1035 | .dump = device_dump, |
1036 | ||
f50e0a01 | 1037 | .active_state = device_active_state, |
10a94420 | 1038 | .sub_state_to_string = device_sub_state_to_string, |
25ac040b | 1039 | |
a7f241db | 1040 | .following = device_following, |
6210e7fc | 1041 | .following_set = device_following_set, |
a7f241db | 1042 | |
f50e0a01 | 1043 | .enumerate = device_enumerate, |
c6918296 | 1044 | .shutdown = device_shutdown, |
f374631a | 1045 | .supported = udev_available, |
c6918296 MS |
1046 | |
1047 | .status_message_formats = { | |
1048 | .starting_stopping = { | |
1049 | [0] = "Expecting device %s...", | |
1050 | }, | |
1051 | .finished_start_job = { | |
1052 | [JOB_DONE] = "Found device %s.", | |
1053 | [JOB_TIMEOUT] = "Timed out waiting for device %s.", | |
1054 | }, | |
1055 | }, | |
5cb5a6ff | 1056 | }; |