]>
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 | |
b4bbcaa9 TA |
6 | #include "libudev.h" |
7 | ||
b5efdb8a | 8 | #include "alloc-util.h" |
88116909 | 9 | #include "bus-error.h" |
4139c1b2 | 10 | #include "dbus-device.h" |
6bedfcbb | 11 | #include "device.h" |
07630cea | 12 | #include "log.h" |
6bedfcbb | 13 | #include "parse-util.h" |
9eb977db | 14 | #include "path-util.h" |
8fcde012 | 15 | #include "stat-util.h" |
07630cea LP |
16 | #include "string-util.h" |
17 | #include "swap.h" | |
718db961 | 18 | #include "udev-util.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 | ||
628c89cc | 303 | static int device_update_description(Unit *u, struct udev_device *dev, const char *path) { |
965e5c5d | 304 | const char *model; |
628c89cc | 305 | int r; |
965e5c5d LP |
306 | |
307 | assert(u); | |
308 | assert(dev); | |
309 | assert(path); | |
310 | ||
311 | model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE"); | |
312 | if (!model) | |
313 | model = udev_device_get_property_value(dev, "ID_MODEL"); | |
314 | ||
315 | if (model) { | |
316 | const char *label; | |
317 | ||
318 | /* Try to concatenate the device model string with a label, if there is one */ | |
319 | label = udev_device_get_property_value(dev, "ID_FS_LABEL"); | |
320 | if (!label) | |
321 | label = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME"); | |
322 | if (!label) | |
323 | label = udev_device_get_property_value(dev, "ID_PART_ENTRY_NUMBER"); | |
324 | ||
325 | if (label) { | |
326 | _cleanup_free_ char *j; | |
327 | ||
605405c6 | 328 | j = strjoin(model, " ", label); |
ea8ec43b LP |
329 | if (!j) |
330 | return log_oom(); | |
331 | ||
332 | r = unit_set_description(u, j); | |
628c89cc LP |
333 | } else |
334 | r = unit_set_description(u, model); | |
335 | } else | |
336 | r = unit_set_description(u, path); | |
628c89cc | 337 | if (r < 0) |
ea8ec43b | 338 | return log_unit_error_errno(u, r, "Failed to set device description: %m"); |
965e5c5d | 339 | |
ea8ec43b | 340 | return 0; |
965e5c5d LP |
341 | } |
342 | ||
343 | static int device_add_udev_wants(Unit *u, struct udev_device *dev) { | |
88116909 | 344 | _cleanup_strv_free_ char **added = NULL; |
de040543 | 345 | const char *wants, *property; |
88116909 | 346 | Device *d = DEVICE(u); |
965e5c5d LP |
347 | int r; |
348 | ||
88116909 | 349 | assert(d); |
965e5c5d LP |
350 | assert(dev); |
351 | ||
463d0d15 | 352 | property = MANAGER_IS_USER(u->manager) ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS"; |
de040543 | 353 | |
b2fadec6 | 354 | wants = udev_device_get_property_value(dev, property); |
de040543 LP |
355 | if (!wants) |
356 | return 0; | |
357 | ||
358 | for (;;) { | |
ceed8f0c | 359 | _cleanup_free_ char *word = NULL, *k = NULL; |
965e5c5d | 360 | |
de040543 | 361 | r = extract_first_word(&wants, &word, NULL, EXTRACT_QUOTES); |
ceed8f0c | 362 | if (r == 0) |
88116909 | 363 | break; |
ceed8f0c ZJS |
364 | if (r == -ENOMEM) |
365 | return log_oom(); | |
366 | if (r < 0) | |
dcebc9ba | 367 | return log_unit_error_errno(u, r, "Failed to parse property %s with value %s: %m", property, wants); |
965e5c5d | 368 | |
88116909 | 369 | if (unit_name_is_valid(word, UNIT_NAME_TEMPLATE) && d->sysfs) { |
dcebc9ba LP |
370 | _cleanup_free_ char *escaped = NULL; |
371 | ||
372 | /* If the unit name is specified as template, then automatically fill in the sysfs path of the | |
373 | * device as instance name, properly escaped. */ | |
374 | ||
88116909 | 375 | r = unit_name_path_escape(d->sysfs, &escaped); |
dcebc9ba | 376 | if (r < 0) |
88116909 | 377 | return log_unit_error_errno(u, r, "Failed to escape %s: %m", d->sysfs); |
dcebc9ba LP |
378 | |
379 | r = unit_name_replace_instance(word, escaped, &k); | |
380 | if (r < 0) | |
381 | return log_unit_error_errno(u, r, "Failed to build %s instance of template %s: %m", escaped, word); | |
382 | } else { | |
383 | /* If this is not a template, then let's mangle it so, that it becomes a valid unit name. */ | |
384 | ||
37cbc1d5 | 385 | r = unit_name_mangle(word, UNIT_NAME_MANGLE_WARN, &k); |
dcebc9ba LP |
386 | if (r < 0) |
387 | return log_unit_error_errno(u, r, "Failed to mangle unit name \"%s\": %m", word); | |
388 | } | |
965e5c5d | 389 | |
eef85c4a | 390 | r = unit_add_dependency_by_name(u, UNIT_WANTS, k, NULL, true, UNIT_DEPENDENCY_UDEV); |
965e5c5d | 391 | if (r < 0) |
de040543 | 392 | return log_unit_error_errno(u, r, "Failed to add Wants= dependency: %m"); |
88116909 LP |
393 | |
394 | r = strv_push(&added, k); | |
395 | if (r < 0) | |
396 | return log_oom(); | |
397 | ||
398 | k = NULL; | |
965e5c5d | 399 | } |
88116909 LP |
400 | |
401 | if (d->state != DEVICE_DEAD) { | |
402 | char **i; | |
403 | ||
404 | /* So here's a special hack, to compensate for the fact that the udev database's reload cycles are not | |
405 | * synchronized with our own reload cycles: when we detect that the SYSTEMD_WANTS property of a device | |
406 | * changes while the device unit is already up, let's manually trigger any new units listed in it not | |
407 | * seen before. This typically appens during the boot-time switch root transition, as udev devices | |
408 | * will generally already be up in the initrd, but SYSTEMD_WANTS properties get then added through udev | |
409 | * rules only available on the host system, and thus only when the initial udev coldplug trigger runs. | |
410 | * | |
411 | * We do this only if the device has been up already when we parse this, as otherwise the usual | |
412 | * dependency logic that is run from the dead → plugged transition will trigger these deps. */ | |
413 | ||
414 | STRV_FOREACH(i, added) { | |
415 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
416 | ||
417 | if (strv_contains(d->wants_property, *i)) /* Was this unit already listed before? */ | |
418 | continue; | |
419 | ||
420 | r = manager_add_job_by_name(u->manager, JOB_START, *i, JOB_FAIL, &error, NULL); | |
421 | if (r < 0) | |
422 | log_unit_warning_errno(u, r, "Failed to enqueue SYSTEMD_WANTS= job, ignoring: %s", bus_error_message(&error, r)); | |
423 | } | |
424 | } | |
425 | ||
426 | strv_free(d->wants_property); | |
427 | d->wants_property = TAKE_PTR(added); | |
428 | ||
429 | return 0; | |
965e5c5d LP |
430 | } |
431 | ||
40b1a32c | 432 | static bool device_is_bound_by_mounts(Device *d, struct udev_device *dev) { |
ebc8968b | 433 | const char *bound_by; |
40b1a32c | 434 | int r; |
ebc8968b FB |
435 | |
436 | assert(d); | |
437 | assert(dev); | |
438 | ||
439 | bound_by = udev_device_get_property_value(dev, "SYSTEMD_MOUNT_DEVICE_BOUND"); | |
40b1a32c LP |
440 | if (bound_by) { |
441 | r = parse_boolean(bound_by); | |
442 | if (r < 0) | |
443 | log_warning_errno(r, "Failed to parse SYSTEMD_MOUNT_DEVICE_BOUND='%s' udev property of %s, ignoring: %m", bound_by, strna(d->sysfs)); | |
ebc8968b | 444 | |
40b1a32c LP |
445 | d->bind_mounts = r > 0; |
446 | } else | |
447 | d->bind_mounts = false; | |
448 | ||
449 | return d->bind_mounts; | |
ebc8968b FB |
450 | } |
451 | ||
86cdffd1 | 452 | static void device_upgrade_mount_deps(Unit *u) { |
ebc8968b FB |
453 | Unit *other; |
454 | Iterator i; | |
eef85c4a | 455 | void *v; |
ebc8968b FB |
456 | int r; |
457 | ||
eef85c4a LP |
458 | /* Let's upgrade Requires= to BindsTo= on us. (Used when SYSTEMD_MOUNT_DEVICE_BOUND is set) */ |
459 | ||
460 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_REQUIRED_BY], i) { | |
ebc8968b FB |
461 | if (other->type != UNIT_MOUNT) |
462 | continue; | |
463 | ||
eef85c4a | 464 | r = unit_add_dependency(other, UNIT_BINDS_TO, u, true, UNIT_DEPENDENCY_UDEV); |
ebc8968b | 465 | if (r < 0) |
86cdffd1 | 466 | log_unit_warning_errno(u, r, "Failed to add BindsTo= dependency between device and mount unit, ignoring: %m"); |
ebc8968b | 467 | } |
ebc8968b FB |
468 | } |
469 | ||
628c89cc LP |
470 | static int device_setup_unit(Manager *m, struct udev_device *dev, const char *path, bool main) { |
471 | _cleanup_free_ char *e = NULL; | |
2005219f | 472 | const char *sysfs = NULL; |
25ac040b | 473 | Unit *u = NULL; |
25ac040b | 474 | bool delete; |
965e5c5d | 475 | int r; |
25ac040b LP |
476 | |
477 | assert(m); | |
965e5c5d | 478 | assert(path); |
25ac040b | 479 | |
2005219f MP |
480 | if (dev) { |
481 | sysfs = udev_device_get_syspath(dev); | |
66f3fdbb LP |
482 | if (!sysfs) { |
483 | log_debug("Couldn't get syspath from udev device, ignoring."); | |
2005219f | 484 | return 0; |
66f3fdbb | 485 | } |
2005219f | 486 | } |
7f275a9f | 487 | |
7410616c LP |
488 | r = unit_name_from_path(path, ".device", &e); |
489 | if (r < 0) | |
7f629b74 | 490 | return log_error_errno(r, "Failed to generate unit name from device path: %m"); |
628c89cc LP |
491 | |
492 | u = manager_get_unit(m, e); | |
38b9b72e | 493 | if (u) { |
66f3fdbb LP |
494 | /* The device unit can still be present even if the device was unplugged: a mount unit can reference it |
495 | * hence preventing the GC to have garbaged it. That's desired since the device unit may have a | |
496 | * dependency on the mount unit which was added during the loading of the later. When the device is | |
497 | * plugged the sysfs might not be initialized yet, as we serialize the device's state but do not | |
498 | * serialize the sysfs path across reloads/reexecs. Hence, when coming back from a reload/restart we | |
499 | * might have the state valid, but not the sysfs path. Hence, let's filter out conflicting devices, but | |
500 | * let's accept devices in any state with no sysfs path set. */ | |
501 | ||
502 | if (DEVICE(u)->state == DEVICE_PLUGGED && | |
503 | DEVICE(u)->sysfs && | |
504 | sysfs && | |
505 | !path_equal(DEVICE(u)->sysfs, sysfs)) { | |
506 | log_unit_debug(u, "Device %s appeared twice with different sysfs paths %s and %s, ignoring the latter.", | |
507 | e, DEVICE(u)->sysfs, sysfs); | |
508 | return -EEXIST; | |
ac9d396b | 509 | } |
25ac040b | 510 | |
38b9b72e LP |
511 | delete = false; |
512 | ||
513 | /* Let's remove all dependencies generated due to udev properties. We'll readd whatever is configured | |
514 | * now below. */ | |
515 | unit_remove_dependencies(u, UNIT_DEPENDENCY_UDEV); | |
516 | } else { | |
aab14b13 LP |
517 | delete = true; |
518 | ||
a581e45a | 519 | r = unit_new_for_name(m, sizeof(Device), e, &u); |
66f3fdbb LP |
520 | if (r < 0) { |
521 | log_error_errno(r, "Failed to allocate device unit %s: %m", e); | |
25ac040b | 522 | goto fail; |
66f3fdbb | 523 | } |
25ac040b | 524 | |
ee6cb288 | 525 | unit_add_to_load_queue(u); |
38b9b72e | 526 | } |
ee6cb288 | 527 | |
66f3fdbb | 528 | /* If this was created via some dependency and has not actually been seen yet ->sysfs will not be |
ee6cb288 | 529 | * initialized. Hence initialize it if necessary. */ |
2005219f MP |
530 | if (sysfs) { |
531 | r = device_set_sysfs(DEVICE(u), sysfs); | |
66f3fdbb LP |
532 | if (r < 0) { |
533 | log_error_errno(r, "Failed to set sysfs path %s for device unit %s: %m", sysfs, e); | |
2005219f | 534 | goto fail; |
66f3fdbb | 535 | } |
ee6cb288 | 536 | |
2005219f | 537 | (void) device_update_description(u, dev, path); |
aab14b13 | 538 | |
66f3fdbb | 539 | /* The additional systemd udev properties we only interpret for the main object */ |
2005219f MP |
540 | if (main) |
541 | (void) device_add_udev_wants(u, dev); | |
542 | } | |
25ac040b | 543 | |
eef85c4a LP |
544 | /* So the user wants the mount units to be bound to the device but a mount unit might has been seen by systemd |
545 | * before the device appears on its radar. In this case the device unit is partially initialized and includes | |
546 | * the deps on the mount unit but at that time the "bind mounts" flag wasn't not present. Fix this up now. */ | |
40b1a32c | 547 | if (dev && device_is_bound_by_mounts(DEVICE(u), dev)) |
ebc8968b | 548 | device_upgrade_mount_deps(u); |
f94ea366 | 549 | |
38b9b72e | 550 | /* Note that this won't dispatch the load queue, the caller has to do that if needed and appropriate */ |
c1e1601e | 551 | unit_add_to_dbus_queue(u); |
66f3fdbb | 552 | |
25ac040b LP |
553 | return 0; |
554 | ||
555 | fail: | |
c9d5c9c0 | 556 | if (delete) |
25ac040b | 557 | unit_free(u); |
ee5f3479 | 558 | |
25ac040b LP |
559 | return r; |
560 | } | |
561 | ||
628c89cc | 562 | static int device_process_new(Manager *m, struct udev_device *dev) { |
f1421cc6 | 563 | const char *sysfs, *dn, *alias; |
8fe914ec | 564 | struct udev_list_entry *item = NULL, *first = NULL; |
003ac9d0 | 565 | int r; |
8fe914ec LP |
566 | |
567 | assert(m); | |
568 | ||
718db961 LP |
569 | sysfs = udev_device_get_syspath(dev); |
570 | if (!sysfs) | |
f1421cc6 | 571 | return 0; |
8fe914ec LP |
572 | |
573 | /* Add the main unit named after the sysfs path */ | |
628c89cc | 574 | r = device_setup_unit(m, dev, sysfs, true); |
003ac9d0 HH |
575 | if (r < 0) |
576 | return r; | |
8fe914ec LP |
577 | |
578 | /* Add an additional unit for the device node */ | |
f1421cc6 LP |
579 | dn = udev_device_get_devnode(dev); |
580 | if (dn) | |
628c89cc | 581 | (void) device_setup_unit(m, dev, dn, false); |
8fe914ec LP |
582 | |
583 | /* Add additional units for all symlinks */ | |
584 | first = udev_device_get_devlinks_list_entry(dev); | |
585 | udev_list_entry_foreach(item, first) { | |
586 | const char *p; | |
5845b46b | 587 | struct stat st; |
8fe914ec LP |
588 | |
589 | /* Don't bother with the /dev/block links */ | |
590 | p = udev_list_entry_get_name(item); | |
591 | ||
bae47ba7 | 592 | if (PATH_STARTSWITH_SET(p, "/dev/block/", "/dev/char/")) |
8fe914ec LP |
593 | continue; |
594 | ||
5845b46b LP |
595 | /* Verify that the symlink in the FS actually belongs |
596 | * to this device. This is useful to deal with | |
597 | * conflicting devices, e.g. when two disks want the | |
598 | * same /dev/disk/by-label/xxx link because they have | |
599 | * the same label. We want to make sure that the same | |
600 | * device that won the symlink wins in systemd, so we | |
ee33e53a | 601 | * check the device node major/minor */ |
5845b46b LP |
602 | if (stat(p, &st) >= 0) |
603 | if ((!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) || | |
604 | st.st_rdev != udev_device_get_devnum(dev)) | |
605 | continue; | |
606 | ||
628c89cc | 607 | (void) device_setup_unit(m, dev, p, false); |
8fe914ec LP |
608 | } |
609 | ||
f1421cc6 LP |
610 | /* Add additional units for all explicitly configured |
611 | * aliases */ | |
612 | alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS"); | |
ceed8f0c | 613 | for (;;) { |
8fb242ab | 614 | _cleanup_free_ char *word = NULL; |
8fe914ec | 615 | |
ceed8f0c ZJS |
616 | r = extract_first_word(&alias, &word, NULL, EXTRACT_QUOTES); |
617 | if (r == 0) | |
8bb2c8c9 | 618 | break; |
ceed8f0c ZJS |
619 | if (r == -ENOMEM) |
620 | return log_oom(); | |
621 | if (r < 0) | |
622 | return log_warning_errno(r, "Failed to add parse SYSTEMD_ALIAS for %s: %m", sysfs); | |
f1421cc6 | 623 | |
a7f8be01 | 624 | if (!path_is_absolute(word)) |
ceed8f0c | 625 | log_warning("SYSTEMD_ALIAS for %s is not an absolute path, ignoring: %s", sysfs, word); |
a7f8be01 LP |
626 | else if (!path_is_normalized(word)) |
627 | log_warning("SYSTEMD_ALIAS for %s is not a normalized path, ignoring: %s", sysfs, word); | |
628 | else | |
629 | (void) device_setup_unit(m, dev, word, false); | |
8fe914ec | 630 | } |
8bb2c8c9 LP |
631 | |
632 | return 0; | |
8fe914ec LP |
633 | } |
634 | ||
66f3fdbb | 635 | static void device_found_changed(Device *d, DeviceFound previous, DeviceFound now) { |
628c89cc LP |
636 | assert(d); |
637 | ||
4b58153d | 638 | /* Didn't exist before, but does now? if so, generate a new invocation ID for it */ |
66f3fdbb | 639 | if (previous == DEVICE_NOT_FOUND && now != DEVICE_NOT_FOUND) |
4b58153d LP |
640 | (void) unit_acquire_invocation_id(UNIT(d)); |
641 | ||
66f3fdbb LP |
642 | if (FLAGS_SET(now, DEVICE_FOUND_UDEV)) |
643 | /* When the device is known to udev we consider it plugged. */ | |
f6200941 | 644 | device_set_state(d, DEVICE_PLUGGED); |
66f3fdbb LP |
645 | else if (now != DEVICE_NOT_FOUND && !FLAGS_SET(previous, DEVICE_FOUND_UDEV)) |
646 | /* If the device has not been seen by udev yet, but is now referenced by the kernel, then we assume the | |
f6200941 LP |
647 | * kernel knows it now, and udev might soon too. */ |
648 | device_set_state(d, DEVICE_TENTATIVE); | |
244f8055 | 649 | else |
66f3fdbb LP |
650 | /* If nobody sees the device, or if the device was previously seen by udev and now is only referenced |
651 | * from the kernel, then we consider the device is gone, the kernel just hasn't noticed it yet. */ | |
f6200941 | 652 | device_set_state(d, DEVICE_DEAD); |
628c89cc LP |
653 | } |
654 | ||
66f3fdbb LP |
655 | static void device_update_found_one(Device *d, DeviceFound found, DeviceFound mask) { |
656 | assert(d); | |
657 | ||
658 | if (MANAGER_IS_RUNNING(UNIT(d)->manager)) { | |
659 | DeviceFound n, previous; | |
660 | ||
661 | /* When we are already running, then apply the new mask right-away, and trigger state changes | |
662 | * right-away */ | |
663 | ||
664 | n = (d->found & ~mask) | (found & mask); | |
665 | if (n == d->found) | |
666 | return; | |
667 | ||
668 | previous = d->found; | |
669 | d->found = n; | |
670 | ||
671 | device_found_changed(d, previous, n); | |
672 | } else | |
673 | /* We aren't running yet, let's apply the new mask to the shadow variable instead, which we'll apply as | |
674 | * soon as we catch-up with the state. */ | |
675 | d->enumerated_found = (d->enumerated_found & ~mask) | (found & mask); | |
676 | } | |
677 | ||
485ae697 | 678 | static void device_update_found_by_sysfs(Manager *m, const char *sysfs, DeviceFound found, DeviceFound mask) { |
cc0df6cc | 679 | Device *d, *l, *n; |
25ac040b LP |
680 | |
681 | assert(m); | |
628c89cc | 682 | assert(sysfs); |
25ac040b | 683 | |
485ae697 LP |
684 | if (mask == 0) |
685 | return; | |
25ac040b | 686 | |
f1421cc6 | 687 | l = hashmap_get(m->devices_by_sysfs, sysfs); |
cc0df6cc | 688 | LIST_FOREACH_SAFE(same_sysfs, d, n, l) |
485ae697 | 689 | device_update_found_one(d, found, mask); |
25ac040b LP |
690 | } |
691 | ||
485ae697 | 692 | static int device_update_found_by_name(Manager *m, const char *path, DeviceFound found, DeviceFound mask) { |
628c89cc LP |
693 | _cleanup_free_ char *e = NULL; |
694 | Unit *u; | |
7410616c | 695 | int r; |
f94ea366 LP |
696 | |
697 | assert(m); | |
628c89cc | 698 | assert(path); |
f94ea366 | 699 | |
485ae697 | 700 | if (mask == 0) |
628c89cc | 701 | return 0; |
f94ea366 | 702 | |
7410616c LP |
703 | r = unit_name_from_path(path, ".device", &e); |
704 | if (r < 0) | |
705 | return log_error_errno(r, "Failed to generate unit name from device path: %m"); | |
f94ea366 | 706 | |
628c89cc LP |
707 | u = manager_get_unit(m, e); |
708 | if (!u) | |
709 | return 0; | |
710 | ||
485ae697 | 711 | device_update_found_one(DEVICE(u), found, mask); |
f94ea366 LP |
712 | return 0; |
713 | } | |
714 | ||
f1421cc6 LP |
715 | static bool device_is_ready(struct udev_device *dev) { |
716 | const char *ready; | |
717 | ||
718 | assert(dev); | |
719 | ||
720 | ready = udev_device_get_property_value(dev, "SYSTEMD_READY"); | |
721 | if (!ready) | |
722 | return true; | |
723 | ||
724 | return parse_boolean(ready) != 0; | |
725 | } | |
726 | ||
a7f241db LP |
727 | static Unit *device_following(Unit *u) { |
728 | Device *d = DEVICE(u); | |
729 | Device *other, *first = NULL; | |
730 | ||
731 | assert(d); | |
732 | ||
ac155bb8 | 733 | if (startswith(u->id, "sys-")) |
a7f241db LP |
734 | return NULL; |
735 | ||
736 | /* Make everybody follow the unit that's named after the sysfs path */ | |
737 | for (other = d->same_sysfs_next; other; other = other->same_sysfs_next) | |
1124fe6f | 738 | if (startswith(UNIT(other)->id, "sys-")) |
a7f241db LP |
739 | return UNIT(other); |
740 | ||
741 | for (other = d->same_sysfs_prev; other; other = other->same_sysfs_prev) { | |
1124fe6f | 742 | if (startswith(UNIT(other)->id, "sys-")) |
a7f241db LP |
743 | return UNIT(other); |
744 | ||
745 | first = other; | |
746 | } | |
747 | ||
748 | return UNIT(first); | |
749 | } | |
750 | ||
f1421cc6 LP |
751 | static int device_following_set(Unit *u, Set **_set) { |
752 | Device *d = DEVICE(u), *other; | |
af4fa99d | 753 | _cleanup_set_free_ Set *set = NULL; |
6210e7fc LP |
754 | int r; |
755 | ||
756 | assert(d); | |
f1421cc6 | 757 | assert(_set); |
6210e7fc | 758 | |
f1421cc6 LP |
759 | if (LIST_JUST_US(same_sysfs, d)) { |
760 | *_set = NULL; | |
6210e7fc LP |
761 | return 0; |
762 | } | |
763 | ||
d5099efc | 764 | set = set_new(NULL); |
f1421cc6 | 765 | if (!set) |
6210e7fc LP |
766 | return -ENOMEM; |
767 | ||
f1421cc6 LP |
768 | LIST_FOREACH_AFTER(same_sysfs, other, d) { |
769 | r = set_put(set, other); | |
770 | if (r < 0) | |
95f14a3e | 771 | return r; |
f1421cc6 | 772 | } |
6210e7fc | 773 | |
f1421cc6 LP |
774 | LIST_FOREACH_BEFORE(same_sysfs, other, d) { |
775 | r = set_put(set, other); | |
776 | if (r < 0) | |
95f14a3e | 777 | return r; |
f1421cc6 | 778 | } |
6210e7fc | 779 | |
95f14a3e | 780 | *_set = TAKE_PTR(set); |
6210e7fc | 781 | return 1; |
6210e7fc LP |
782 | } |
783 | ||
25ac040b LP |
784 | static void device_shutdown(Manager *m) { |
785 | assert(m); | |
786 | ||
718db961 | 787 | m->udev_event_source = sd_event_source_unref(m->udev_event_source); |
66f57304 | 788 | m->udev_monitor = udev_monitor_unref(m->udev_monitor); |
525d3cc7 | 789 | m->devices_by_sysfs = hashmap_free(m->devices_by_sysfs); |
25ac040b LP |
790 | } |
791 | ||
ba64af90 | 792 | static void device_enumerate(Manager *m) { |
8e766630 | 793 | _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL; |
25ac040b | 794 | struct udev_list_entry *item = NULL, *first = NULL; |
718db961 | 795 | int r; |
25ac040b LP |
796 | |
797 | assert(m); | |
798 | ||
9670d583 | 799 | if (!m->udev_monitor) { |
718db961 LP |
800 | m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); |
801 | if (!m->udev_monitor) { | |
8bb2c8c9 | 802 | log_error_errno(errno, "Failed to allocate udev monitor: %m"); |
a16e1123 LP |
803 | goto fail; |
804 | } | |
f94ea366 | 805 | |
47ae6e67 LP |
806 | /* This will fail if we are unprivileged, but that |
807 | * should not matter much, as user instances won't run | |
808 | * during boot. */ | |
8fa158e7 | 809 | (void) udev_monitor_set_receive_buffer_size(m->udev_monitor, 128*1024*1024); |
99448c1f | 810 | |
718db961 | 811 | r = udev_monitor_filter_add_match_tag(m->udev_monitor, "systemd"); |
ba64af90 LP |
812 | if (r < 0) { |
813 | log_error_errno(r, "Failed to add udev tag match: %m"); | |
e1ce2c27 | 814 | goto fail; |
ba64af90 | 815 | } |
e1ce2c27 | 816 | |
718db961 | 817 | r = udev_monitor_enable_receiving(m->udev_monitor); |
ba64af90 LP |
818 | if (r < 0) { |
819 | log_error_errno(r, "Failed to enable udev event reception: %m"); | |
a16e1123 | 820 | goto fail; |
ba64af90 | 821 | } |
f94ea366 | 822 | |
151b9b96 | 823 | 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 |
824 | if (r < 0) { |
825 | log_error_errno(r, "Failed to watch udev file descriptor: %m"); | |
718db961 | 826 | goto fail; |
ba64af90 | 827 | } |
7dfbe2e3 TG |
828 | |
829 | (void) sd_event_source_set_description(m->udev_event_source, "device"); | |
a16e1123 | 830 | } |
f94ea366 | 831 | |
718db961 LP |
832 | e = udev_enumerate_new(m->udev); |
833 | if (!e) { | |
8bb2c8c9 | 834 | log_error_errno(errno, "Failed to alloacte udev enumerator: %m"); |
4f2d528d LP |
835 | goto fail; |
836 | } | |
718db961 LP |
837 | |
838 | r = udev_enumerate_add_match_tag(e, "systemd"); | |
ba64af90 LP |
839 | if (r < 0) { |
840 | log_error_errno(r, "Failed to create udev tag enumeration: %m"); | |
e1ce2c27 | 841 | goto fail; |
ba64af90 | 842 | } |
25ac040b | 843 | |
e1202047 | 844 | r = udev_enumerate_add_match_is_initialized(e); |
ba64af90 LP |
845 | if (r < 0) { |
846 | log_error_errno(r, "Failed to install initialization match into enumeration: %m"); | |
e1202047 | 847 | goto fail; |
ba64af90 | 848 | } |
e1202047 | 849 | |
718db961 | 850 | r = udev_enumerate_scan_devices(e); |
ba64af90 LP |
851 | if (r < 0) { |
852 | log_error_errno(r, "Failed to enumerate devices: %m"); | |
4f2d528d | 853 | goto fail; |
ba64af90 | 854 | } |
25ac040b | 855 | |
4f2d528d | 856 | first = udev_enumerate_get_list_entry(e); |
628c89cc | 857 | udev_list_entry_foreach(item, first) { |
8e766630 | 858 | _cleanup_(udev_device_unrefp) struct udev_device *dev = NULL; |
628c89cc LP |
859 | const char *sysfs; |
860 | ||
861 | sysfs = udev_list_entry_get_name(item); | |
862 | ||
863 | dev = udev_device_new_from_syspath(m->udev, sysfs); | |
864 | if (!dev) { | |
8bb2c8c9 LP |
865 | if (errno == ENOMEM) { |
866 | log_oom(); | |
867 | goto fail; | |
868 | } | |
869 | ||
870 | /* If we can't create a device, don't bother, it probably just disappeared. */ | |
871 | log_debug_errno(errno, "Failed to create udev device object for %s: %m", sysfs); | |
628c89cc LP |
872 | continue; |
873 | } | |
874 | ||
875 | if (!device_is_ready(dev)) | |
876 | continue; | |
877 | ||
878 | (void) device_process_new(m, dev); | |
66f3fdbb | 879 | device_update_found_by_sysfs(m, sysfs, DEVICE_FOUND_UDEV, DEVICE_FOUND_UDEV); |
628c89cc | 880 | } |
25ac040b | 881 | |
ba64af90 | 882 | return; |
25ac040b LP |
883 | |
884 | fail: | |
25ac040b | 885 | device_shutdown(m); |
5cb5a6ff LP |
886 | } |
887 | ||
87934b36 LP |
888 | static void device_propagate_reload_by_sysfs(Manager *m, const char *sysfs) { |
889 | Device *d, *l, *n; | |
890 | int r; | |
891 | ||
892 | assert(m); | |
893 | assert(sysfs); | |
894 | ||
895 | l = hashmap_get(m->devices_by_sysfs, sysfs); | |
896 | LIST_FOREACH_SAFE(same_sysfs, d, n, l) { | |
897 | if (d->state == DEVICE_DEAD) | |
898 | continue; | |
899 | ||
900 | r = manager_propagate_reload(m, UNIT(d), JOB_REPLACE, NULL); | |
901 | if (r < 0) | |
902 | log_warning_errno(r, "Failed to propagate reload, ignoring: %m"); | |
903 | } | |
904 | } | |
905 | ||
718db961 | 906 | static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
8e766630 | 907 | _cleanup_(udev_device_unrefp) struct udev_device *dev = NULL; |
718db961 | 908 | Manager *m = userdata; |
628c89cc | 909 | const char *action, *sysfs; |
718db961 | 910 | int r; |
f94ea366 LP |
911 | |
912 | assert(m); | |
99448c1f | 913 | |
718db961 | 914 | if (revents != EPOLLIN) { |
99448c1f KS |
915 | static RATELIMIT_DEFINE(limit, 10*USEC_PER_SEC, 5); |
916 | ||
7994ac1d | 917 | if (ratelimit_below(&limit)) |
a1bcaa07 | 918 | log_warning("Failed to get udev event"); |
718db961 LP |
919 | if (!(revents & EPOLLIN)) |
920 | return 0; | |
99448c1f | 921 | } |
f94ea366 | 922 | |
718db961 LP |
923 | /* |
924 | * libudev might filter-out devices which pass the bloom | |
925 | * filter, so getting NULL here is not necessarily an error. | |
926 | */ | |
927 | dev = udev_monitor_receive_device(m->udev_monitor); | |
928 | if (!dev) | |
929 | return 0; | |
f94ea366 | 930 | |
628c89cc LP |
931 | sysfs = udev_device_get_syspath(dev); |
932 | if (!sysfs) { | |
933 | log_error("Failed to get udev sys path."); | |
934 | return 0; | |
935 | } | |
936 | ||
718db961 LP |
937 | action = udev_device_get_action(dev); |
938 | if (!action) { | |
f94ea366 | 939 | log_error("Failed to get udev action string."); |
718db961 | 940 | return 0; |
f94ea366 LP |
941 | } |
942 | ||
87934b36 LP |
943 | if (streq(action, "change")) |
944 | device_propagate_reload_by_sysfs(m, sysfs); | |
f332611a JR |
945 | |
946 | /* A change event can signal that a device is becoming ready, in particular if | |
947 | * the device is using the SYSTEMD_READY logic in udev | |
948 | * so we need to reach the else block of the follwing if, even for change events */ | |
6d445911 | 949 | if (streq(action, "remove")) { |
628c89cc | 950 | r = swap_process_device_remove(m, dev); |
9670d583 | 951 | if (r < 0) |
8bb2c8c9 | 952 | log_warning_errno(r, "Failed to process swap device remove event, ignoring: %m"); |
9670d583 | 953 | |
628c89cc LP |
954 | /* If we get notified that a device was removed by |
955 | * udev, then it's completely gone, hence unset all | |
956 | * found bits */ | |
485ae697 | 957 | device_update_found_by_sysfs(m, sysfs, 0, DEVICE_FOUND_UDEV|DEVICE_FOUND_MOUNT|DEVICE_FOUND_SWAP); |
f1421cc6 | 958 | |
628c89cc LP |
959 | } else if (device_is_ready(dev)) { |
960 | ||
961 | (void) device_process_new(m, dev); | |
962 | ||
963 | r = swap_process_device_new(m, dev); | |
9670d583 | 964 | if (r < 0) |
8bb2c8c9 | 965 | log_warning_errno(r, "Failed to process swap device new event, ignoring: %m"); |
9670d583 | 966 | |
f1421cc6 LP |
967 | manager_dispatch_load_queue(m); |
968 | ||
628c89cc | 969 | /* The device is found now, set the udev found bit */ |
485ae697 | 970 | device_update_found_by_sysfs(m, sysfs, DEVICE_FOUND_UDEV, DEVICE_FOUND_UDEV); |
628c89cc LP |
971 | |
972 | } else { | |
973 | /* The device is nominally around, but not ready for | |
974 | * us. Hence unset the udev bit, but leave the rest | |
975 | * around. */ | |
976 | ||
485ae697 | 977 | device_update_found_by_sysfs(m, sysfs, 0, DEVICE_FOUND_UDEV); |
f94ea366 LP |
978 | } |
979 | ||
718db961 | 980 | return 0; |
f94ea366 LP |
981 | } |
982 | ||
1c2e9646 | 983 | static bool device_supported(void) { |
0faacd47 | 984 | static int read_only = -1; |
0faacd47 LP |
985 | |
986 | /* If /sys is read-only we don't support device units, and any | |
987 | * attempts to start one should fail immediately. */ | |
988 | ||
989 | if (read_only < 0) | |
990 | read_only = path_is_read_only_fs("/sys"); | |
991 | ||
992 | return read_only <= 0; | |
993 | } | |
994 | ||
34c20ee0 | 995 | static int validate_node(Manager *m, const char *node, struct udev_device **ret) { |
628c89cc | 996 | struct stat st; |
e5ca27b7 | 997 | int r; |
628c89cc | 998 | |
34c20ee0 LP |
999 | assert(m); |
1000 | assert(node); | |
1001 | assert(ret); | |
1002 | ||
1003 | /* Validates a device node that showed up in /proc/swaps or /proc/self/mountinfo if it makes sense for us to | |
1004 | * track. Note that this validator is fine within missing device nodes, but not with badly set up ones! */ | |
1005 | ||
1006 | if (!path_startswith(node, "/dev")) { | |
1007 | *ret = NULL; | |
1008 | return 0; /* bad! */ | |
1009 | } | |
1010 | ||
1011 | if (stat(node, &st) < 0) { | |
1012 | if (errno != ENOENT) | |
1013 | return log_error_errno(errno, "Failed to stat() device node file %s: %m", node); | |
1014 | ||
1015 | *ret = NULL; | |
1016 | return 1; /* good! (though missing) */ | |
1017 | ||
1018 | } else { | |
1019 | _cleanup_(udev_device_unrefp) struct udev_device *dev = NULL; | |
1020 | ||
e5ca27b7 LP |
1021 | r = udev_device_new_from_stat_rdev(m->udev, &st, &dev); |
1022 | if (r == -ENOENT) { | |
34c20ee0 LP |
1023 | *ret = NULL; |
1024 | return 1; /* good! (though missing) */ | |
e5ca27b7 LP |
1025 | } else if (r == -ENOTTY) { |
1026 | *ret = NULL; | |
1027 | return 0; /* bad! (not a device node but some other kind of file system node) */ | |
1028 | } else if (r < 0) | |
1029 | 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 |
1030 | |
1031 | *ret = TAKE_PTR(dev); | |
1032 | return 1; /* good! */ | |
1033 | } | |
1034 | } | |
1035 | ||
1036 | void device_found_node(Manager *m, const char *node, DeviceFound found, DeviceFound mask) { | |
1037 | int r; | |
1038 | ||
628c89cc LP |
1039 | assert(m); |
1040 | assert(node); | |
1041 | ||
4c6d20de | 1042 | if (!device_supported()) |
485ae697 | 1043 | return; |
4c6d20de | 1044 | |
34c20ee0 LP |
1045 | if (mask == 0) |
1046 | return; | |
1047 | ||
1048 | /* This is called whenever we find a device referenced in /proc/swaps or /proc/self/mounts. Such a device might | |
1049 | * be mounted/enabled at a time where udev has not finished probing it yet, and we thus haven't learned about | |
66f3fdbb LP |
1050 | * it yet. In this case we will set the device unit to "tentative" state. |
1051 | * | |
1052 | * This takes a pair of DeviceFound flags parameters. The 'mask' parameter is a bit mask that indicates which | |
1053 | * bits of 'found' to copy into the per-device DeviceFound flags field. Thus, this function may be used to set | |
1054 | * and unset individual bits in a single call, while merging partially with previous state. */ | |
628c89cc | 1055 | |
485ae697 | 1056 | if ((found & mask) != 0) { |
34c20ee0 | 1057 | _cleanup_(udev_device_unrefp) struct udev_device *dev = NULL; |
628c89cc | 1058 | |
34c20ee0 LP |
1059 | /* If the device is known in the kernel and newly appeared, then we'll create a device unit for it, |
1060 | * under the name referenced in /proc/swaps or /proc/self/mountinfo. But first, let's validate if | |
1061 | * everything is alright with the device node. */ | |
628c89cc | 1062 | |
34c20ee0 LP |
1063 | r = validate_node(m, node, &dev); |
1064 | if (r <= 0) | |
1065 | return; /* Don't create a device unit for this if the device node is borked. */ | |
628c89cc LP |
1066 | |
1067 | (void) device_setup_unit(m, dev, node, false); | |
1068 | } | |
1069 | ||
1070 | /* Update the device unit's state, should it exist */ | |
485ae697 | 1071 | (void) device_update_found_by_name(m, node, found, mask); |
628c89cc LP |
1072 | } |
1073 | ||
ebc8968b | 1074 | bool device_shall_be_bound_by(Unit *device, Unit *u) { |
8bb2c8c9 LP |
1075 | assert(device); |
1076 | assert(u); | |
ebc8968b FB |
1077 | |
1078 | if (u->type != UNIT_MOUNT) | |
1079 | return false; | |
1080 | ||
1081 | return DEVICE(device)->bind_mounts; | |
1082 | } | |
1083 | ||
87f0e418 | 1084 | const UnitVTable device_vtable = { |
7d17cfbc | 1085 | .object_size = sizeof(Device), |
f975e971 LP |
1086 | .sections = |
1087 | "Unit\0" | |
1088 | "Device\0" | |
1089 | "Install\0", | |
5cb5a6ff | 1090 | |
c5a97ed1 LP |
1091 | .gc_jobs = true, |
1092 | ||
faf919f1 | 1093 | .init = device_init, |
034c6ed7 | 1094 | .done = device_done, |
718db961 LP |
1095 | .load = unit_load_fragment_and_dropin_optional, |
1096 | ||
f50e0a01 | 1097 | .coldplug = device_coldplug, |
66f3fdbb | 1098 | .catchup = device_catchup, |
f50e0a01 | 1099 | |
f6200941 LP |
1100 | .serialize = device_serialize, |
1101 | .deserialize_item = device_deserialize_item, | |
1102 | ||
5cb5a6ff LP |
1103 | .dump = device_dump, |
1104 | ||
f50e0a01 | 1105 | .active_state = device_active_state, |
10a94420 | 1106 | .sub_state_to_string = device_sub_state_to_string, |
25ac040b | 1107 | |
718db961 | 1108 | .bus_vtable = bus_device_vtable, |
4139c1b2 | 1109 | |
a7f241db | 1110 | .following = device_following, |
6210e7fc | 1111 | .following_set = device_following_set, |
a7f241db | 1112 | |
f50e0a01 | 1113 | .enumerate = device_enumerate, |
c6918296 | 1114 | .shutdown = device_shutdown, |
0faacd47 | 1115 | .supported = device_supported, |
c6918296 MS |
1116 | |
1117 | .status_message_formats = { | |
1118 | .starting_stopping = { | |
1119 | [0] = "Expecting device %s...", | |
1120 | }, | |
1121 | .finished_start_job = { | |
1122 | [JOB_DONE] = "Found device %s.", | |
1123 | [JOB_TIMEOUT] = "Timed out waiting for device %s.", | |
1124 | }, | |
1125 | }, | |
5cb5a6ff | 1126 | }; |