HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/core/device.c
Git blob: ef3c182cbb91d6ebbf697af9b7a803abcba0571f
Lines 1001–1334 of 1,334
set = set_new(NULL);
if (!set)
return -ENOMEM;
LIST_FOREACH(same_sysfs, other, d->same_sysfs_next) {
r = set_put(set, other);
if (r < 0)
return r;
}
LIST_FOREACH_BACKWARDS(same_sysfs, other, d->same_sysfs_prev) {
r = set_put(set, other);
if (r < 0)
return r;
}
*ret = TAKE_PTR(set);
return 1;
}
static void device_shutdown(Manager *m) {
assert(m);
m->device_monitor = sd_device_monitor_unref(m->device_monitor);
m->devices_by_sysfs = hashmap_free(m->devices_by_sysfs);
}
static void device_enumerate(Manager *m) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
int r;
assert(m);
if (!m->device_monitor) {
r = sd_device_monitor_new(&m->device_monitor);
if (r < 0) {
log_error_errno(r, "Failed to allocate device monitor: %m");
goto fail;
}
r = sd_device_monitor_filter_add_match_tag(m->device_monitor, "systemd");
if (r < 0) {
log_error_errno(r, "Failed to add udev tag match: %m");
goto fail;
}
r = sd_device_monitor_attach_event(m->device_monitor, m->event);
if (r < 0) {
log_error_errno(r, "Failed to attach event to device monitor: %m");
goto fail;
}
r = sd_device_monitor_start(m->device_monitor, device_dispatch_io, m);
if (r < 0) {
log_error_errno(r, "Failed to start device monitor: %m");
goto fail;
}
}
r = sd_device_enumerator_new(&e);
if (r < 0) {
log_error_errno(r, "Failed to allocate device enumerator: %m");
goto fail;
}
r = sd_device_enumerator_add_match_tag(e, "systemd");
if (r < 0) {
log_error_errno(r, "Failed to set tag for device enumeration: %m");
goto fail;
}
FOREACH_DEVICE(e, dev) {
_cleanup_set_free_ Set *ready_units = NULL, *not_ready_units = NULL;
const char *syspath;
bool processed;
Device *d;
r = sd_device_get_syspath(dev, &syspath);
if (r < 0) {
log_device_debug_errno(dev, r, "Failed to get syspath of enumerated device, ignoring: %m");
continue;
}
r = device_is_processed(dev);
if (r < 0)
log_device_debug_errno(dev, r, "Failed to check if device is processed by udevd, assuming not: %m");
processed = r > 0;
if (device_setup_units(m, dev, &ready_units, ¬_ready_units) < 0)
continue;
SET_FOREACH(d, ready_units) {
device_update_found_one(d, DEVICE_FOUND_UDEV, DEVICE_FOUND_UDEV);
/* Why we need to check the syspath here? Because the device unit may be generated by
* a devlink, and the syspath may be different from the one of the original device. */
if (path_equal(d->sysfs, syspath))
d->processed = processed;
}
SET_FOREACH(d, not_ready_units)
device_update_found_one(d, DEVICE_NOT_FOUND, DEVICE_FOUND_UDEV);
}
return;
fail:
device_shutdown(m);
}
static void device_propagate_reload(Manager *m, Device *d) {
int r;
assert(m);
assert(d);
if (d->state == DEVICE_DEAD)
return;
r = manager_propagate_reload(m, UNIT(d), JOB_REPLACE, NULL);
if (r < 0)
log_unit_warning_errno(UNIT(d), r, "Failed to propagate reload, ignoring: %m");
}
static void device_remove_old_on_move(Manager *m, sd_device *dev) {
_cleanup_free_ char *syspath_old = NULL;
const char *devpath_old;
int r;
assert(m);
assert(dev);
r = sd_device_get_property_value(dev, "DEVPATH_OLD", &devpath_old);
if (r < 0)
return (void) log_device_debug_errno(dev, r, "Failed to get DEVPATH_OLD= property on 'move' uevent, ignoring: %m");
syspath_old = path_join("/sys", devpath_old);
if (!syspath_old)
return (void) log_oom();
device_update_found_by_sysfs(m, syspath_old, DEVICE_NOT_FOUND, _DEVICE_FOUND_MASK);
}
static int device_dispatch_io(sd_device_monitor *monitor, sd_device *dev, void *userdata) {
Manager *m = ASSERT_PTR(userdata);
sd_device_action_t action;
const char *sysfs;
bool ready;
Device *d;
int r;
assert(dev);
log_device_uevent(dev, "Processing udev action");
r = sd_device_get_syspath(dev, &sysfs);
if (r < 0) {
log_device_warning_errno(dev, r, "Failed to get device syspath, ignoring: %m");
return 0;
}
r = sd_device_get_action(dev, &action);
if (r < 0) {
log_device_warning_errno(dev, r, "Failed to get udev action, ignoring: %m");
return 0;
}
log_device_debug(dev, "Got '%s' action on syspath '%s'.", device_action_to_string(action), sysfs);
if (action == SD_DEVICE_MOVE)
device_remove_old_on_move(m, dev);
/* When udevd failed to process the device, SYSTEMD_ALIAS or any other properties may contain invalid
* values. Let's refuse to handle the uevent. */
if (sd_device_get_property_value(dev, "UDEV_WORKER_FAILED", NULL) >= 0) {
int v;
if (device_get_property_int(dev, "UDEV_WORKER_ERRNO", &v) >= 0)
log_device_warning_errno(dev, v, "systemd-udevd failed to process the device, ignoring: %m");
else if (device_get_property_int(dev, "UDEV_WORKER_EXIT_STATUS", &v) >= 0)
log_device_warning(dev, "systemd-udevd failed to process the device with exit status %i, ignoring.", v);
else if (device_get_property_int(dev, "UDEV_WORKER_SIGNAL", &v) >= 0) {
const char *s;
(void) sd_device_get_property_value(dev, "UDEV_WORKER_SIGNAL_NAME", &s);
log_device_warning(dev, "systemd-udevd failed to process the device with signal %i(%s), ignoring.", v, strna(s));
} else
log_device_warning(dev, "systemd-udevd failed to process the device with unknown result, ignoring.");
return 0;
}
/* A change event can signal that a device is becoming ready, in particular if the device is using
* the SYSTEMD_READY logic in udev so we need to reach the else block of the following if, even for
* change events */
ready = device_is_ready(dev);
_cleanup_set_free_ Set *ready_units = NULL, *not_ready_units = NULL;
(void) device_setup_units(m, dev, &ready_units, ¬_ready_units);
if (action == SD_DEVICE_REMOVE) {
r = swap_process_device_remove(m, dev);
if (r < 0)
log_device_warning_errno(dev, r, "Failed to process swap device remove event, ignoring: %m");
} else if (ready) {
r = swap_process_device_new(m, dev);
if (r < 0)
log_device_warning_errno(dev, r, "Failed to process swap device new event, ignoring: %m");
}
if (!IN_SET(action, SD_DEVICE_ADD, SD_DEVICE_REMOVE, SD_DEVICE_MOVE))
SET_FOREACH(d, ready_units)
device_propagate_reload(m, d);
if (!set_isempty(ready_units))
manager_dispatch_load_queue(m);
if (action == SD_DEVICE_REMOVE)
/* If we get notified that a device was removed by udev, then it's completely gone, hence
* unset all found bits. Note this affects all .device units still point to the removed
* device. */
device_update_found_by_sysfs(m, sysfs, DEVICE_NOT_FOUND, _DEVICE_FOUND_MASK);
/* These devices are found and ready now, set the udev found bit. Note, this is also necessary to do
* on remove uevent, as some devlinks may be updated and now point to other device nodes. */
SET_FOREACH(d, ready_units)
device_update_found_one(d, DEVICE_FOUND_UDEV, DEVICE_FOUND_UDEV);
/* These devices may be nominally around, but not ready for us. Hence unset the udev bit, but leave
* the rest around. This may be redundant for remove uevent, but should be harmless. */
SET_FOREACH(d, not_ready_units)
device_update_found_one(d, DEVICE_NOT_FOUND, DEVICE_FOUND_UDEV);
return 0;
}
void device_found_node(Manager *m, const char *node, DeviceFound found, DeviceFound mask) {
int r;
assert(m);
assert(node);
assert(!FLAGS_SET(mask, DEVICE_FOUND_UDEV));
if (!udev_available())
return;
if (mask == 0)
return;
/* This is called whenever we find a device referenced in /proc/swaps or /proc/self/mounts. Such a device might
* be mounted/enabled at a time where udev has not finished probing it yet, and we thus haven't learned about
* it yet. In this case we will set the device unit to "tentative" state.
*
* This takes a pair of DeviceFound flags parameters. The 'mask' parameter is a bit mask that indicates which
* bits of 'found' to copy into the per-device DeviceFound flags field. Thus, this function may be used to set
* and unset individual bits in a single call, while merging partially with previous state. */
if ((found & mask) != 0) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
/* If the device is known in the kernel and newly appeared, then we'll create a device unit for it,
* under the name referenced in /proc/swaps or /proc/self/mountinfo. But first, let's validate if
* everything is alright with the device node. Note that we're fine with missing device nodes,
* but not with badly set up ones. */
r = sd_device_new_from_devname(&dev, node);
if (r == -ENODEV)
log_debug("Could not find device for %s, continuing without device node", node);
else if (r < 0) {
/* Reduce log noise from nodes which are not device nodes by skipping EINVAL. */
if (r != -EINVAL)
log_error_errno(r, "Failed to open %s device, ignoring: %m", node);
return;
}
(void) device_setup_unit(m, dev, node, /* main= */ false, NULL); /* 'dev' may be NULL. */
}
/* Update the device unit's state, should it exist */
device_update_found_by_name(m, node, found, mask);
}
bool device_shall_be_bound_by(Unit *device, Unit *u) {
assert(device);
assert(u);
if (u->type != UNIT_MOUNT)
return false;
return DEVICE(device)->bind_mounts;
}
const UnitVTable device_vtable = {
.object_size = sizeof(Device),
.sections =
"Unit\0"
"Device\0"
"Install\0",
.gc_jobs = true,
.init = device_init,
.done = device_done,
.load = device_load,
.coldplug = device_coldplug,
.catchup = device_catchup,
.serialize = device_serialize,
.deserialize_item = device_deserialize_item,
.dump = device_dump,
.active_state = device_active_state,
.sub_state_to_string = device_sub_state_to_string,
.following = device_following,
.following_set = device_following_set,
.enumerate = device_enumerate,
.shutdown = device_shutdown,
.supported = udev_available,
.status_message_formats = {
.starting_stopping = {
[0] = "Expecting device %s...",
[1] = "Waiting for device %s to disappear...",
},
.finished_start_job = {
[JOB_DONE] = "Found device %s.",
[JOB_TIMEOUT] = "Timed out waiting for device %s.",
},
},
};