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