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