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