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