]>
Commit | Line | Data |
---|---|---|
a7334b09 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
a7334b09 | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
25ac040b | 20 | #include <errno.h> |
f94ea366 | 21 | #include <sys/epoll.h> |
25ac040b | 22 | |
b4bbcaa9 TA |
23 | #include "libudev.h" |
24 | ||
b5efdb8a | 25 | #include "alloc-util.h" |
4139c1b2 | 26 | #include "dbus-device.h" |
6bedfcbb | 27 | #include "device.h" |
07630cea | 28 | #include "log.h" |
6bedfcbb | 29 | #include "parse-util.h" |
9eb977db | 30 | #include "path-util.h" |
8fcde012 | 31 | #include "stat-util.h" |
07630cea LP |
32 | #include "string-util.h" |
33 | #include "swap.h" | |
718db961 | 34 | #include "udev-util.h" |
07630cea | 35 | #include "unit-name.h" |
9670d583 | 36 | #include "unit.h" |
5cb5a6ff | 37 | |
f50e0a01 LP |
38 | static const UnitActiveState state_translation_table[_DEVICE_STATE_MAX] = { |
39 | [DEVICE_DEAD] = UNIT_INACTIVE, | |
628c89cc LP |
40 | [DEVICE_TENTATIVE] = UNIT_ACTIVATING, |
41 | [DEVICE_PLUGGED] = UNIT_ACTIVE, | |
f50e0a01 LP |
42 | }; |
43 | ||
718db961 LP |
44 | static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
45 | ||
8fe914ec | 46 | static void device_unset_sysfs(Device *d) { |
f1421cc6 | 47 | Hashmap *devices; |
8fe914ec LP |
48 | Device *first; |
49 | ||
50 | assert(d); | |
51 | ||
a7f241db LP |
52 | if (!d->sysfs) |
53 | return; | |
54 | ||
55 | /* Remove this unit from the chain of devices which share the | |
56 | * same sysfs path. */ | |
f1421cc6 LP |
57 | devices = UNIT(d)->manager->devices_by_sysfs; |
58 | first = hashmap_get(devices, d->sysfs); | |
71fda00f | 59 | LIST_REMOVE(same_sysfs, first, d); |
8fe914ec | 60 | |
a7f241db | 61 | if (first) |
f1421cc6 | 62 | hashmap_remove_and_replace(devices, d->sysfs, first->sysfs, first); |
a7f241db | 63 | else |
f1421cc6 | 64 | hashmap_remove(devices, d->sysfs); |
a7f241db | 65 | |
a1e58e8e | 66 | d->sysfs = mfree(d->sysfs); |
8fe914ec LP |
67 | } |
68 | ||
628c89cc LP |
69 | static int device_set_sysfs(Device *d, const char *sysfs) { |
70 | Device *first; | |
71 | char *copy; | |
72 | int r; | |
73 | ||
74 | assert(d); | |
75 | ||
76 | if (streq_ptr(d->sysfs, sysfs)) | |
77 | return 0; | |
78 | ||
79 | r = hashmap_ensure_allocated(&UNIT(d)->manager->devices_by_sysfs, &string_hash_ops); | |
80 | if (r < 0) | |
81 | return r; | |
82 | ||
83 | copy = strdup(sysfs); | |
84 | if (!copy) | |
85 | return -ENOMEM; | |
86 | ||
87 | device_unset_sysfs(d); | |
88 | ||
89 | first = hashmap_get(UNIT(d)->manager->devices_by_sysfs, sysfs); | |
90 | LIST_PREPEND(same_sysfs, first, d); | |
91 | ||
92 | r = hashmap_replace(UNIT(d)->manager->devices_by_sysfs, copy, first); | |
93 | if (r < 0) { | |
94 | LIST_REMOVE(same_sysfs, first, d); | |
95 | free(copy); | |
96 | return r; | |
97 | } | |
98 | ||
99 | d->sysfs = copy; | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
faf919f1 LP |
104 | static void device_init(Unit *u) { |
105 | Device *d = DEVICE(u); | |
106 | ||
107 | assert(d); | |
1124fe6f | 108 | assert(UNIT(d)->load_state == UNIT_STUB); |
faf919f1 | 109 | |
8fe914ec LP |
110 | /* In contrast to all other unit types we timeout jobs waiting |
111 | * for devices by default. This is because they otherwise wait | |
35b8ca3a | 112 | * indefinitely for plugged in devices, something which cannot |
8fe914ec LP |
113 | * happen for the other units since their operations time out |
114 | * anyway. */ | |
f1421cc6 | 115 | u->job_timeout = u->manager->default_timeout_start_usec; |
c8f4d764 | 116 | |
f1421cc6 | 117 | u->ignore_on_isolate = true; |
faf919f1 LP |
118 | } |
119 | ||
87f0e418 LP |
120 | static void device_done(Unit *u) { |
121 | Device *d = DEVICE(u); | |
034c6ed7 LP |
122 | |
123 | assert(d); | |
e537352b | 124 | |
8fe914ec | 125 | device_unset_sysfs(d); |
e537352b LP |
126 | } |
127 | ||
f50e0a01 LP |
128 | static void device_set_state(Device *d, DeviceState state) { |
129 | DeviceState old_state; | |
130 | assert(d); | |
5cb5a6ff | 131 | |
f50e0a01 LP |
132 | old_state = d->state; |
133 | d->state = state; | |
5cb5a6ff | 134 | |
e537352b | 135 | if (state != old_state) |
f2341e0a | 136 | log_unit_debug(UNIT(d), "Changed %s -> %s", device_state_to_string(old_state), device_state_to_string(state)); |
f50e0a01 | 137 | |
e2f3b44c | 138 | unit_notify(UNIT(d), state_translation_table[old_state], state_translation_table[state], true); |
f50e0a01 LP |
139 | } |
140 | ||
be847e82 | 141 | static int device_coldplug(Unit *u) { |
f50e0a01 LP |
142 | Device *d = DEVICE(u); |
143 | ||
144 | assert(d); | |
145 | assert(d->state == DEVICE_DEAD); | |
146 | ||
628c89cc LP |
147 | if (d->found & DEVICE_FOUND_UDEV) |
148 | /* If udev says the device is around, it's around */ | |
73608ed9 | 149 | device_set_state(d, DEVICE_PLUGGED); |
f6200941 | 150 | else if (d->found != DEVICE_NOT_FOUND && d->deserialized_state != DEVICE_PLUGGED) |
628c89cc | 151 | /* If a device is found in /proc/self/mountinfo or |
f6200941 LP |
152 | * /proc/swaps, and was not yet announced via udev, |
153 | * it's "tentatively" around. */ | |
628c89cc | 154 | device_set_state(d, DEVICE_TENTATIVE); |
f50e0a01 LP |
155 | |
156 | return 0; | |
157 | } | |
158 | ||
f6200941 LP |
159 | static int device_serialize(Unit *u, FILE *f, FDSet *fds) { |
160 | Device *d = DEVICE(u); | |
161 | ||
162 | assert(u); | |
163 | assert(f); | |
164 | assert(fds); | |
165 | ||
166 | unit_serialize_item(u, f, "state", device_state_to_string(d->state)); | |
0108f6ec DM |
167 | |
168 | return 0; | |
f6200941 LP |
169 | } |
170 | ||
171 | static int device_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
172 | Device *d = DEVICE(u); | |
173 | ||
174 | assert(u); | |
175 | assert(key); | |
176 | assert(value); | |
177 | assert(fds); | |
178 | ||
179 | if (streq(key, "state")) { | |
180 | DeviceState state; | |
181 | ||
182 | state = device_state_from_string(value); | |
183 | if (state < 0) | |
f2341e0a | 184 | log_unit_debug(u, "Failed to parse state value: %s", value); |
f6200941 LP |
185 | else |
186 | d->deserialized_state = state; | |
187 | } else | |
f2341e0a | 188 | log_unit_debug(u, "Unknown serialization key: %s", key); |
f6200941 LP |
189 | |
190 | return 0; | |
191 | } | |
192 | ||
f50e0a01 | 193 | static void device_dump(Unit *u, FILE *f, const char *prefix) { |
25ac040b | 194 | Device *d = DEVICE(u); |
5cb5a6ff | 195 | |
25ac040b | 196 | assert(d); |
5cb5a6ff LP |
197 | |
198 | fprintf(f, | |
25ac040b LP |
199 | "%sDevice State: %s\n" |
200 | "%sSysfs Path: %s\n", | |
a16e1123 | 201 | prefix, device_state_to_string(d->state), |
f50e0a01 LP |
202 | prefix, strna(d->sysfs)); |
203 | } | |
204 | ||
44a6b1b6 | 205 | _pure_ static UnitActiveState device_active_state(Unit *u) { |
f50e0a01 LP |
206 | assert(u); |
207 | ||
208 | return state_translation_table[DEVICE(u)->state]; | |
25ac040b LP |
209 | } |
210 | ||
44a6b1b6 | 211 | _pure_ static const char *device_sub_state_to_string(Unit *u) { |
10a94420 LP |
212 | assert(u); |
213 | ||
a16e1123 | 214 | return device_state_to_string(DEVICE(u)->state); |
10a94420 LP |
215 | } |
216 | ||
628c89cc | 217 | static int device_update_description(Unit *u, struct udev_device *dev, const char *path) { |
965e5c5d | 218 | const char *model; |
628c89cc | 219 | int r; |
965e5c5d LP |
220 | |
221 | assert(u); | |
222 | assert(dev); | |
223 | assert(path); | |
224 | ||
225 | model = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE"); | |
226 | if (!model) | |
227 | model = udev_device_get_property_value(dev, "ID_MODEL"); | |
228 | ||
229 | if (model) { | |
230 | const char *label; | |
231 | ||
232 | /* Try to concatenate the device model string with a label, if there is one */ | |
233 | label = udev_device_get_property_value(dev, "ID_FS_LABEL"); | |
234 | if (!label) | |
235 | label = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME"); | |
236 | if (!label) | |
237 | label = udev_device_get_property_value(dev, "ID_PART_ENTRY_NUMBER"); | |
238 | ||
239 | if (label) { | |
240 | _cleanup_free_ char *j; | |
241 | ||
242 | j = strjoin(model, " ", label, NULL); | |
243 | if (j) | |
628c89cc | 244 | r = unit_set_description(u, j); |
c43b2132 TA |
245 | else |
246 | r = -ENOMEM; | |
628c89cc LP |
247 | } else |
248 | r = unit_set_description(u, model); | |
249 | } else | |
250 | r = unit_set_description(u, path); | |
965e5c5d | 251 | |
628c89cc | 252 | if (r < 0) |
f2341e0a | 253 | log_unit_error_errno(u, r, "Failed to set device description: %m"); |
965e5c5d | 254 | |
628c89cc | 255 | return r; |
965e5c5d LP |
256 | } |
257 | ||
258 | static int device_add_udev_wants(Unit *u, struct udev_device *dev) { | |
259 | const char *wants; | |
a2a5291b | 260 | const char *word, *state; |
965e5c5d LP |
261 | size_t l; |
262 | int r; | |
b2fadec6 | 263 | const char *property; |
965e5c5d LP |
264 | |
265 | assert(u); | |
266 | assert(dev); | |
267 | ||
13616ae5 | 268 | property = u->manager->running_as == MANAGER_USER ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS"; |
b2fadec6 | 269 | wants = udev_device_get_property_value(dev, property); |
965e5c5d LP |
270 | if (!wants) |
271 | return 0; | |
272 | ||
a2a5291b | 273 | FOREACH_WORD_QUOTED(word, l, wants, state) { |
965e5c5d LP |
274 | _cleanup_free_ char *n = NULL; |
275 | char e[l+1]; | |
276 | ||
a2a5291b | 277 | memcpy(e, word, l); |
965e5c5d LP |
278 | e[l] = 0; |
279 | ||
7410616c LP |
280 | r = unit_name_mangle(e, UNIT_NAME_NOGLOB, &n); |
281 | if (r < 0) | |
f2341e0a | 282 | return log_unit_error_errno(u, r, "Failed to mangle unit name: %m"); |
965e5c5d LP |
283 | |
284 | r = unit_add_dependency_by_name(u, UNIT_WANTS, n, NULL, true); | |
285 | if (r < 0) | |
f2341e0a | 286 | return log_unit_error_errno(u, r, "Failed to add wants dependency: %m"); |
965e5c5d | 287 | } |
b2fadec6 | 288 | if (!isempty(state)) |
f2341e0a | 289 | log_unit_warning(u, "Property %s on %s has trailing garbage, ignoring.", property, strna(udev_device_get_syspath(dev))); |
965e5c5d LP |
290 | |
291 | return 0; | |
292 | } | |
293 | ||
628c89cc LP |
294 | static int device_setup_unit(Manager *m, struct udev_device *dev, const char *path, bool main) { |
295 | _cleanup_free_ char *e = NULL; | |
2005219f | 296 | const char *sysfs = NULL; |
25ac040b | 297 | Unit *u = NULL; |
25ac040b | 298 | bool delete; |
965e5c5d | 299 | int r; |
25ac040b LP |
300 | |
301 | assert(m); | |
965e5c5d | 302 | assert(path); |
25ac040b | 303 | |
2005219f MP |
304 | if (dev) { |
305 | sysfs = udev_device_get_syspath(dev); | |
306 | if (!sysfs) | |
307 | return 0; | |
308 | } | |
7f275a9f | 309 | |
7410616c LP |
310 | r = unit_name_from_path(path, ".device", &e); |
311 | if (r < 0) | |
7f629b74 | 312 | return log_error_errno(r, "Failed to generate unit name from device path: %m"); |
628c89cc LP |
313 | |
314 | u = manager_get_unit(m, e); | |
25ac040b | 315 | |
ac9d396b FB |
316 | /* The device unit can still be present even if the device was |
317 | * unplugged: a mount unit can reference it hence preventing | |
318 | * the GC to have garbaged it. That's desired since the device | |
319 | * unit may have a dependency on the mount unit which was | |
320 | * added during the loading of the later. */ | |
51f21740 | 321 | if (dev && u && DEVICE(u)->state == DEVICE_PLUGGED) { |
ac9d396b FB |
322 | /* This unit is in plugged state: we're sure it's |
323 | * attached to a device. */ | |
324 | if (!path_equal(DEVICE(u)->sysfs, sysfs)) { | |
071e0b8b | 325 | log_unit_debug(u, "Dev %s appeared twice with different sysfs paths %s and %s", |
ac9d396b FB |
326 | e, DEVICE(u)->sysfs, sysfs); |
327 | return -EEXIST; | |
328 | } | |
628c89cc | 329 | } |
25ac040b | 330 | |
aab14b13 LP |
331 | if (!u) { |
332 | delete = true; | |
333 | ||
7d17cfbc MS |
334 | u = unit_new(m, sizeof(Device)); |
335 | if (!u) | |
f1421cc6 | 336 | return log_oom(); |
25ac040b | 337 | |
628c89cc | 338 | r = unit_add_name(u, e); |
7d17cfbc | 339 | if (r < 0) |
25ac040b LP |
340 | goto fail; |
341 | ||
ee6cb288 LP |
342 | unit_add_to_load_queue(u); |
343 | } else | |
344 | delete = false; | |
345 | ||
346 | /* If this was created via some dependency and has not | |
347 | * actually been seen yet ->sysfs will not be | |
348 | * initialized. Hence initialize it if necessary. */ | |
2005219f MP |
349 | if (sysfs) { |
350 | r = device_set_sysfs(DEVICE(u), sysfs); | |
351 | if (r < 0) | |
352 | goto fail; | |
ee6cb288 | 353 | |
2005219f | 354 | (void) device_update_description(u, dev, path); |
aab14b13 | 355 | |
2005219f MP |
356 | /* The additional systemd udev properties we only interpret |
357 | * for the main object */ | |
358 | if (main) | |
359 | (void) device_add_udev_wants(u, dev); | |
360 | } | |
25ac040b | 361 | |
f94ea366 | 362 | |
f1421cc6 LP |
363 | /* Note that this won't dispatch the load queue, the caller |
364 | * has to do that if needed and appropriate */ | |
365 | ||
c1e1601e | 366 | unit_add_to_dbus_queue(u); |
25ac040b LP |
367 | return 0; |
368 | ||
369 | fail: | |
f2341e0a | 370 | log_unit_warning_errno(u, r, "Failed to set up device unit: %m"); |
ee5f3479 | 371 | |
2bb9e620 | 372 | if (delete) |
25ac040b | 373 | unit_free(u); |
ee5f3479 | 374 | |
25ac040b LP |
375 | return r; |
376 | } | |
377 | ||
628c89cc | 378 | static int device_process_new(Manager *m, struct udev_device *dev) { |
f1421cc6 | 379 | const char *sysfs, *dn, *alias; |
8fe914ec | 380 | struct udev_list_entry *item = NULL, *first = NULL; |
003ac9d0 | 381 | int r; |
8fe914ec LP |
382 | |
383 | assert(m); | |
384 | ||
718db961 LP |
385 | sysfs = udev_device_get_syspath(dev); |
386 | if (!sysfs) | |
f1421cc6 | 387 | return 0; |
8fe914ec LP |
388 | |
389 | /* Add the main unit named after the sysfs path */ | |
628c89cc | 390 | r = device_setup_unit(m, dev, sysfs, true); |
003ac9d0 HH |
391 | if (r < 0) |
392 | return r; | |
8fe914ec LP |
393 | |
394 | /* Add an additional unit for the device node */ | |
f1421cc6 LP |
395 | dn = udev_device_get_devnode(dev); |
396 | if (dn) | |
628c89cc | 397 | (void) device_setup_unit(m, dev, dn, false); |
8fe914ec LP |
398 | |
399 | /* Add additional units for all symlinks */ | |
400 | first = udev_device_get_devlinks_list_entry(dev); | |
401 | udev_list_entry_foreach(item, first) { | |
402 | const char *p; | |
5845b46b | 403 | struct stat st; |
8fe914ec LP |
404 | |
405 | /* Don't bother with the /dev/block links */ | |
406 | p = udev_list_entry_get_name(item); | |
407 | ||
408 | if (path_startswith(p, "/dev/block/") || | |
409 | path_startswith(p, "/dev/char/")) | |
410 | continue; | |
411 | ||
5845b46b LP |
412 | /* Verify that the symlink in the FS actually belongs |
413 | * to this device. This is useful to deal with | |
414 | * conflicting devices, e.g. when two disks want the | |
415 | * same /dev/disk/by-label/xxx link because they have | |
416 | * the same label. We want to make sure that the same | |
417 | * device that won the symlink wins in systemd, so we | |
ee33e53a | 418 | * check the device node major/minor */ |
5845b46b LP |
419 | if (stat(p, &st) >= 0) |
420 | if ((!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) || | |
421 | st.st_rdev != udev_device_get_devnum(dev)) | |
422 | continue; | |
423 | ||
628c89cc | 424 | (void) device_setup_unit(m, dev, p, false); |
8fe914ec LP |
425 | } |
426 | ||
f1421cc6 LP |
427 | /* Add additional units for all explicitly configured |
428 | * aliases */ | |
429 | alias = udev_device_get_property_value(dev, "SYSTEMD_ALIAS"); | |
430 | if (alias) { | |
a2a5291b | 431 | const char *word, *state; |
f1421cc6 | 432 | size_t l; |
8fe914ec | 433 | |
a2a5291b | 434 | FOREACH_WORD_QUOTED(word, l, alias, state) { |
f1421cc6 | 435 | char e[l+1]; |
8fe914ec | 436 | |
a2a5291b | 437 | memcpy(e, word, l); |
f1421cc6 LP |
438 | e[l] = 0; |
439 | ||
440 | if (path_is_absolute(e)) | |
628c89cc | 441 | (void) device_setup_unit(m, dev, e, false); |
f1421cc6 LP |
442 | else |
443 | log_warning("SYSTEMD_ALIAS for %s is not an absolute path, ignoring: %s", sysfs, e); | |
444 | } | |
b2fadec6 ZJS |
445 | if (!isempty(state)) |
446 | log_warning("SYSTEMD_ALIAS for %s has trailing garbage, ignoring.", sysfs); | |
8fe914ec LP |
447 | } |
448 | ||
449 | return 0; | |
450 | } | |
451 | ||
628c89cc | 452 | static void device_update_found_one(Device *d, bool add, DeviceFound found, bool now) { |
f6200941 | 453 | DeviceFound n, previous; |
628c89cc LP |
454 | |
455 | assert(d); | |
456 | ||
457 | n = add ? (d->found | found) : (d->found & ~found); | |
458 | if (n == d->found) | |
459 | return; | |
460 | ||
f6200941 | 461 | previous = d->found; |
628c89cc LP |
462 | d->found = n; |
463 | ||
f6200941 LP |
464 | if (!now) |
465 | return; | |
466 | ||
467 | if (d->found & DEVICE_FOUND_UDEV) | |
468 | /* When the device is known to udev we consider it | |
469 | * plugged. */ | |
470 | device_set_state(d, DEVICE_PLUGGED); | |
471 | else if (d->found != DEVICE_NOT_FOUND && (previous & DEVICE_FOUND_UDEV) == 0) | |
472 | /* If the device has not been seen by udev yet, but is | |
473 | * now referenced by the kernel, then we assume the | |
474 | * kernel knows it now, and udev might soon too. */ | |
475 | device_set_state(d, DEVICE_TENTATIVE); | |
476 | else | |
477 | /* If nobody sees the device, or if the device was | |
478 | * previously seen by udev and now is only referenced | |
479 | * from the kernel, then we consider the device is | |
480 | * gone, the kernel just hasn't noticed it yet. */ | |
481 | device_set_state(d, DEVICE_DEAD); | |
628c89cc LP |
482 | } |
483 | ||
484 | static int device_update_found_by_sysfs(Manager *m, const char *sysfs, bool add, DeviceFound found, bool now) { | |
f1421cc6 | 485 | Device *d, *l; |
25ac040b LP |
486 | |
487 | assert(m); | |
628c89cc | 488 | assert(sysfs); |
25ac040b | 489 | |
628c89cc LP |
490 | if (found == DEVICE_NOT_FOUND) |
491 | return 0; | |
25ac040b | 492 | |
f1421cc6 LP |
493 | l = hashmap_get(m->devices_by_sysfs, sysfs); |
494 | LIST_FOREACH(same_sysfs, d, l) | |
628c89cc LP |
495 | device_update_found_one(d, add, found, now); |
496 | ||
497 | return 0; | |
25ac040b LP |
498 | } |
499 | ||
628c89cc LP |
500 | static int device_update_found_by_name(Manager *m, const char *path, bool add, DeviceFound found, bool now) { |
501 | _cleanup_free_ char *e = NULL; | |
502 | Unit *u; | |
7410616c | 503 | int r; |
f94ea366 LP |
504 | |
505 | assert(m); | |
628c89cc | 506 | assert(path); |
f94ea366 | 507 | |
628c89cc LP |
508 | if (found == DEVICE_NOT_FOUND) |
509 | return 0; | |
f94ea366 | 510 | |
7410616c LP |
511 | r = unit_name_from_path(path, ".device", &e); |
512 | if (r < 0) | |
513 | return log_error_errno(r, "Failed to generate unit name from device path: %m"); | |
f94ea366 | 514 | |
628c89cc LP |
515 | u = manager_get_unit(m, e); |
516 | if (!u) | |
517 | return 0; | |
518 | ||
519 | device_update_found_one(DEVICE(u), add, found, now); | |
f94ea366 LP |
520 | return 0; |
521 | } | |
522 | ||
f1421cc6 LP |
523 | static bool device_is_ready(struct udev_device *dev) { |
524 | const char *ready; | |
525 | ||
526 | assert(dev); | |
527 | ||
528 | ready = udev_device_get_property_value(dev, "SYSTEMD_READY"); | |
529 | if (!ready) | |
530 | return true; | |
531 | ||
532 | return parse_boolean(ready) != 0; | |
533 | } | |
534 | ||
a7f241db LP |
535 | static Unit *device_following(Unit *u) { |
536 | Device *d = DEVICE(u); | |
537 | Device *other, *first = NULL; | |
538 | ||
539 | assert(d); | |
540 | ||
ac155bb8 | 541 | if (startswith(u->id, "sys-")) |
a7f241db LP |
542 | return NULL; |
543 | ||
544 | /* Make everybody follow the unit that's named after the sysfs path */ | |
545 | for (other = d->same_sysfs_next; other; other = other->same_sysfs_next) | |
1124fe6f | 546 | if (startswith(UNIT(other)->id, "sys-")) |
a7f241db LP |
547 | return UNIT(other); |
548 | ||
549 | for (other = d->same_sysfs_prev; other; other = other->same_sysfs_prev) { | |
1124fe6f | 550 | if (startswith(UNIT(other)->id, "sys-")) |
a7f241db LP |
551 | return UNIT(other); |
552 | ||
553 | first = other; | |
554 | } | |
555 | ||
556 | return UNIT(first); | |
557 | } | |
558 | ||
f1421cc6 LP |
559 | static int device_following_set(Unit *u, Set **_set) { |
560 | Device *d = DEVICE(u), *other; | |
561 | Set *set; | |
6210e7fc LP |
562 | int r; |
563 | ||
564 | assert(d); | |
f1421cc6 | 565 | assert(_set); |
6210e7fc | 566 | |
f1421cc6 LP |
567 | if (LIST_JUST_US(same_sysfs, d)) { |
568 | *_set = NULL; | |
6210e7fc LP |
569 | return 0; |
570 | } | |
571 | ||
d5099efc | 572 | set = set_new(NULL); |
f1421cc6 | 573 | if (!set) |
6210e7fc LP |
574 | return -ENOMEM; |
575 | ||
f1421cc6 LP |
576 | LIST_FOREACH_AFTER(same_sysfs, other, d) { |
577 | r = set_put(set, other); | |
578 | if (r < 0) | |
6210e7fc | 579 | goto fail; |
f1421cc6 | 580 | } |
6210e7fc | 581 | |
f1421cc6 LP |
582 | LIST_FOREACH_BEFORE(same_sysfs, other, d) { |
583 | r = set_put(set, other); | |
584 | if (r < 0) | |
6210e7fc | 585 | goto fail; |
f1421cc6 | 586 | } |
6210e7fc | 587 | |
f1421cc6 | 588 | *_set = set; |
6210e7fc LP |
589 | return 1; |
590 | ||
591 | fail: | |
f1421cc6 | 592 | set_free(set); |
6210e7fc LP |
593 | return r; |
594 | } | |
595 | ||
25ac040b LP |
596 | static void device_shutdown(Manager *m) { |
597 | assert(m); | |
598 | ||
718db961 LP |
599 | m->udev_event_source = sd_event_source_unref(m->udev_event_source); |
600 | ||
a16e1123 | 601 | if (m->udev_monitor) { |
f94ea366 | 602 | udev_monitor_unref(m->udev_monitor); |
a16e1123 LP |
603 | m->udev_monitor = NULL; |
604 | } | |
f94ea366 | 605 | |
525d3cc7 | 606 | m->devices_by_sysfs = hashmap_free(m->devices_by_sysfs); |
25ac040b LP |
607 | } |
608 | ||
ba64af90 | 609 | static void device_enumerate(Manager *m) { |
718db961 | 610 | _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL; |
25ac040b | 611 | struct udev_list_entry *item = NULL, *first = NULL; |
718db961 | 612 | int r; |
25ac040b LP |
613 | |
614 | assert(m); | |
615 | ||
9670d583 | 616 | if (!m->udev_monitor) { |
718db961 LP |
617 | m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); |
618 | if (!m->udev_monitor) { | |
ba64af90 | 619 | log_oom(); |
a16e1123 LP |
620 | goto fail; |
621 | } | |
f94ea366 | 622 | |
47ae6e67 LP |
623 | /* This will fail if we are unprivileged, but that |
624 | * should not matter much, as user instances won't run | |
625 | * during boot. */ | |
8fa158e7 | 626 | (void) udev_monitor_set_receive_buffer_size(m->udev_monitor, 128*1024*1024); |
99448c1f | 627 | |
718db961 | 628 | r = udev_monitor_filter_add_match_tag(m->udev_monitor, "systemd"); |
ba64af90 LP |
629 | if (r < 0) { |
630 | log_error_errno(r, "Failed to add udev tag match: %m"); | |
e1ce2c27 | 631 | goto fail; |
ba64af90 | 632 | } |
e1ce2c27 | 633 | |
718db961 | 634 | r = udev_monitor_enable_receiving(m->udev_monitor); |
ba64af90 LP |
635 | if (r < 0) { |
636 | log_error_errno(r, "Failed to enable udev event reception: %m"); | |
a16e1123 | 637 | goto fail; |
ba64af90 | 638 | } |
f94ea366 | 639 | |
151b9b96 | 640 | 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 |
641 | if (r < 0) { |
642 | log_error_errno(r, "Failed to watch udev file descriptor: %m"); | |
718db961 | 643 | goto fail; |
ba64af90 | 644 | } |
7dfbe2e3 TG |
645 | |
646 | (void) sd_event_source_set_description(m->udev_event_source, "device"); | |
a16e1123 | 647 | } |
f94ea366 | 648 | |
718db961 LP |
649 | e = udev_enumerate_new(m->udev); |
650 | if (!e) { | |
ba64af90 | 651 | log_oom(); |
4f2d528d LP |
652 | goto fail; |
653 | } | |
718db961 LP |
654 | |
655 | r = udev_enumerate_add_match_tag(e, "systemd"); | |
ba64af90 LP |
656 | if (r < 0) { |
657 | log_error_errno(r, "Failed to create udev tag enumeration: %m"); | |
e1ce2c27 | 658 | goto fail; |
ba64af90 | 659 | } |
25ac040b | 660 | |
e1202047 | 661 | r = udev_enumerate_add_match_is_initialized(e); |
ba64af90 LP |
662 | if (r < 0) { |
663 | log_error_errno(r, "Failed to install initialization match into enumeration: %m"); | |
e1202047 | 664 | goto fail; |
ba64af90 | 665 | } |
e1202047 | 666 | |
718db961 | 667 | r = udev_enumerate_scan_devices(e); |
ba64af90 LP |
668 | if (r < 0) { |
669 | log_error_errno(r, "Failed to enumerate devices: %m"); | |
4f2d528d | 670 | goto fail; |
ba64af90 | 671 | } |
25ac040b | 672 | |
4f2d528d | 673 | first = udev_enumerate_get_list_entry(e); |
628c89cc LP |
674 | udev_list_entry_foreach(item, first) { |
675 | _cleanup_udev_device_unref_ struct udev_device *dev = NULL; | |
676 | const char *sysfs; | |
677 | ||
678 | sysfs = udev_list_entry_get_name(item); | |
679 | ||
680 | dev = udev_device_new_from_syspath(m->udev, sysfs); | |
681 | if (!dev) { | |
682 | log_oom(); | |
683 | continue; | |
684 | } | |
685 | ||
686 | if (!device_is_ready(dev)) | |
687 | continue; | |
688 | ||
689 | (void) device_process_new(m, dev); | |
690 | ||
691 | device_update_found_by_sysfs(m, sysfs, true, DEVICE_FOUND_UDEV, false); | |
692 | } | |
25ac040b | 693 | |
ba64af90 | 694 | return; |
25ac040b LP |
695 | |
696 | fail: | |
25ac040b | 697 | device_shutdown(m); |
5cb5a6ff LP |
698 | } |
699 | ||
718db961 LP |
700 | static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
701 | _cleanup_udev_device_unref_ struct udev_device *dev = NULL; | |
718db961 | 702 | Manager *m = userdata; |
628c89cc | 703 | const char *action, *sysfs; |
718db961 | 704 | int r; |
f94ea366 LP |
705 | |
706 | assert(m); | |
99448c1f | 707 | |
718db961 | 708 | if (revents != EPOLLIN) { |
99448c1f KS |
709 | static RATELIMIT_DEFINE(limit, 10*USEC_PER_SEC, 5); |
710 | ||
711 | if (!ratelimit_test(&limit)) | |
56f64d95 | 712 | log_error_errno(errno, "Failed to get udev event: %m"); |
718db961 LP |
713 | if (!(revents & EPOLLIN)) |
714 | return 0; | |
99448c1f | 715 | } |
f94ea366 | 716 | |
718db961 LP |
717 | /* |
718 | * libudev might filter-out devices which pass the bloom | |
719 | * filter, so getting NULL here is not necessarily an error. | |
720 | */ | |
721 | dev = udev_monitor_receive_device(m->udev_monitor); | |
722 | if (!dev) | |
723 | return 0; | |
f94ea366 | 724 | |
628c89cc LP |
725 | sysfs = udev_device_get_syspath(dev); |
726 | if (!sysfs) { | |
727 | log_error("Failed to get udev sys path."); | |
728 | return 0; | |
729 | } | |
730 | ||
718db961 LP |
731 | action = udev_device_get_action(dev); |
732 | if (!action) { | |
f94ea366 | 733 | log_error("Failed to get udev action string."); |
718db961 | 734 | return 0; |
f94ea366 LP |
735 | } |
736 | ||
628c89cc LP |
737 | if (streq(action, "remove")) { |
738 | r = swap_process_device_remove(m, dev); | |
9670d583 | 739 | if (r < 0) |
da927ba9 | 740 | log_error_errno(r, "Failed to process swap device remove event: %m"); |
9670d583 | 741 | |
628c89cc LP |
742 | /* If we get notified that a device was removed by |
743 | * udev, then it's completely gone, hence unset all | |
744 | * found bits */ | |
745 | device_update_found_by_sysfs(m, sysfs, false, DEVICE_FOUND_UDEV|DEVICE_FOUND_MOUNT|DEVICE_FOUND_SWAP, true); | |
f1421cc6 | 746 | |
628c89cc LP |
747 | } else if (device_is_ready(dev)) { |
748 | ||
749 | (void) device_process_new(m, dev); | |
750 | ||
751 | r = swap_process_device_new(m, dev); | |
9670d583 | 752 | if (r < 0) |
da927ba9 | 753 | log_error_errno(r, "Failed to process swap device new event: %m"); |
9670d583 | 754 | |
f1421cc6 LP |
755 | manager_dispatch_load_queue(m); |
756 | ||
628c89cc LP |
757 | /* The device is found now, set the udev found bit */ |
758 | device_update_found_by_sysfs(m, sysfs, true, DEVICE_FOUND_UDEV, true); | |
759 | ||
760 | } else { | |
761 | /* The device is nominally around, but not ready for | |
762 | * us. Hence unset the udev bit, but leave the rest | |
763 | * around. */ | |
764 | ||
765 | device_update_found_by_sysfs(m, sysfs, false, DEVICE_FOUND_UDEV, true); | |
f94ea366 LP |
766 | } |
767 | ||
718db961 | 768 | return 0; |
f94ea366 LP |
769 | } |
770 | ||
1c2e9646 | 771 | static bool device_supported(void) { |
0faacd47 | 772 | static int read_only = -1; |
0faacd47 LP |
773 | |
774 | /* If /sys is read-only we don't support device units, and any | |
775 | * attempts to start one should fail immediately. */ | |
776 | ||
777 | if (read_only < 0) | |
778 | read_only = path_is_read_only_fs("/sys"); | |
779 | ||
780 | return read_only <= 0; | |
781 | } | |
782 | ||
628c89cc LP |
783 | int device_found_node(Manager *m, const char *node, bool add, DeviceFound found, bool now) { |
784 | _cleanup_udev_device_unref_ struct udev_device *dev = NULL; | |
785 | struct stat st; | |
786 | ||
787 | assert(m); | |
788 | assert(node); | |
789 | ||
4c6d20de LP |
790 | if (!device_supported()) |
791 | return 0; | |
792 | ||
628c89cc LP |
793 | /* This is called whenever we find a device referenced in |
794 | * /proc/swaps or /proc/self/mounts. Such a device might be | |
795 | * mounted/enabled at a time where udev has not finished | |
796 | * probing it yet, and we thus haven't learned about it | |
797 | * yet. In this case we will set the device unit to | |
798 | * "tentative" state. */ | |
799 | ||
800 | if (add) { | |
801 | if (!path_startswith(node, "/dev")) | |
802 | return 0; | |
803 | ||
f6200941 LP |
804 | /* We make an extra check here, if the device node |
805 | * actually exists. If it's missing, then this is an | |
806 | * indication that device was unplugged but is still | |
807 | * referenced in /proc/swaps or | |
808 | * /proc/self/mountinfo. Note that this check doesn't | |
809 | * really cover all cases where a device might be gone | |
810 | * away, since drives that can have a medium inserted | |
811 | * will still have a device node even when the medium | |
812 | * is not there... */ | |
813 | ||
2005219f MP |
814 | if (stat(node, &st) >= 0) { |
815 | if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) | |
628c89cc LP |
816 | return 0; |
817 | ||
2005219f MP |
818 | dev = udev_device_new_from_devnum(m->udev, S_ISBLK(st.st_mode) ? 'b' : 'c', st.st_rdev); |
819 | if (!dev && errno != ENOENT) | |
820 | return log_error_errno(errno, "Failed to get udev device from devnum %u:%u: %m", major(st.st_rdev), minor(st.st_rdev)); | |
628c89cc | 821 | |
2005219f MP |
822 | } else if (errno != ENOENT) |
823 | return log_error_errno(errno, "Failed to stat device node file %s: %m", node); | |
628c89cc LP |
824 | |
825 | /* If the device is known in the kernel and newly | |
826 | * appeared, then we'll create a device unit for it, | |
827 | * under the name referenced in /proc/swaps or | |
828 | * /proc/self/mountinfo. */ | |
829 | ||
830 | (void) device_setup_unit(m, dev, node, false); | |
831 | } | |
832 | ||
833 | /* Update the device unit's state, should it exist */ | |
834 | return device_update_found_by_name(m, node, add, found, now); | |
835 | } | |
836 | ||
87f0e418 | 837 | const UnitVTable device_vtable = { |
7d17cfbc | 838 | .object_size = sizeof(Device), |
f975e971 LP |
839 | .sections = |
840 | "Unit\0" | |
841 | "Device\0" | |
842 | "Install\0", | |
5cb5a6ff | 843 | |
9e2f7c11 LP |
844 | .no_instances = true, |
845 | ||
faf919f1 | 846 | .init = device_init, |
034c6ed7 | 847 | .done = device_done, |
718db961 LP |
848 | .load = unit_load_fragment_and_dropin_optional, |
849 | ||
f50e0a01 LP |
850 | .coldplug = device_coldplug, |
851 | ||
f6200941 LP |
852 | .serialize = device_serialize, |
853 | .deserialize_item = device_deserialize_item, | |
854 | ||
5cb5a6ff LP |
855 | .dump = device_dump, |
856 | ||
f50e0a01 | 857 | .active_state = device_active_state, |
10a94420 | 858 | .sub_state_to_string = device_sub_state_to_string, |
25ac040b | 859 | |
718db961 | 860 | .bus_vtable = bus_device_vtable, |
4139c1b2 | 861 | |
a7f241db | 862 | .following = device_following, |
6210e7fc | 863 | .following_set = device_following_set, |
a7f241db | 864 | |
f50e0a01 | 865 | .enumerate = device_enumerate, |
c6918296 | 866 | .shutdown = device_shutdown, |
0faacd47 | 867 | .supported = device_supported, |
c6918296 MS |
868 | |
869 | .status_message_formats = { | |
870 | .starting_stopping = { | |
871 | [0] = "Expecting device %s...", | |
872 | }, | |
873 | .finished_start_job = { | |
874 | [JOB_DONE] = "Found device %s.", | |
875 | [JOB_TIMEOUT] = "Timed out waiting for device %s.", | |
876 | }, | |
877 | }, | |
5cb5a6ff | 878 | }; |