Skip to content

Commit

Permalink
core/device: move device_update_found_one() and friends
Browse files Browse the repository at this point in the history
No functional change. Just for preparation for later commits.
  • Loading branch information
yuwata committed Apr 28, 2022
1 parent f6f5344 commit 086fcdd
Showing 1 changed file with 83 additions and 83 deletions.
166 changes: 83 additions & 83 deletions src/core/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,89 @@ _pure_ static const char *device_sub_state_to_string(Unit *u) {
return device_state_to_string(DEVICE(u)->state);
}

static void device_found_changed(Device *d, DeviceFound previous, DeviceFound now) {
assert(d);

/* Didn't exist before, but does now? if so, generate a new invocation ID for it */
if (previous == DEVICE_NOT_FOUND && now != DEVICE_NOT_FOUND)
(void) unit_acquire_invocation_id(UNIT(d));

if (FLAGS_SET(now, DEVICE_FOUND_UDEV))
/* When the device is known to udev we consider it plugged. */
device_set_state(d, DEVICE_PLUGGED);
else if (now != DEVICE_NOT_FOUND && !FLAGS_SET(previous, DEVICE_FOUND_UDEV))
/* If the device has not been seen by udev yet, but is now referenced by the kernel, then we assume the
* kernel knows it now, and udev might soon too. */
device_set_state(d, DEVICE_TENTATIVE);
else
/* If nobody sees the device, or if the device was previously seen by udev and now is only referenced
* from the kernel, then we consider the device is gone, the kernel just hasn't noticed it yet. */
device_set_state(d, DEVICE_DEAD);
}

static void device_update_found_one(Device *d, DeviceFound found, DeviceFound mask) {
Manager *m;

assert(d);

m = UNIT(d)->manager;

if (MANAGER_IS_RUNNING(m) && (m->honor_device_enumeration || MANAGER_IS_USER(m))) {
DeviceFound n, previous;

/* When we are already running, then apply the new mask right-away, and trigger state changes
* right-away */

n = (d->found & ~mask) | (found & mask);
if (n == d->found)
return;

previous = d->found;
d->found = n;

device_found_changed(d, previous, n);
} else
/* We aren't running yet, let's apply the new mask to the shadow variable instead, which we'll apply as
* soon as we catch-up with the state. */
d->enumerated_found = (d->enumerated_found & ~mask) | (found & mask);
}

static void device_update_found_by_sysfs(Manager *m, const char *sysfs, DeviceFound found, DeviceFound mask) {
Device *l;

assert(m);
assert(sysfs);

if (mask == 0)
return;

l = hashmap_get(m->devices_by_sysfs, sysfs);
LIST_FOREACH(same_sysfs, d, l)
device_update_found_one(d, found, mask);
}

static void device_update_found_by_name(Manager *m, const char *path, DeviceFound found, DeviceFound mask) {
_cleanup_free_ char *e = NULL;
Unit *u;
int r;

assert(m);
assert(path);

if (mask == 0)
return;

r = unit_name_from_path(path, ".device", &e);
if (r < 0)
return (void) log_debug_errno(r, "Failed to generate unit name from device path, ignoring: %m");

u = manager_get_unit(m, e);
if (!u)
return;

device_update_found_one(DEVICE(u), found, mask);
}

static int device_update_description(Unit *u, sd_device *dev, const char *path) {
_cleanup_free_ char *j = NULL;
const char *model, *label, *desc;
Expand Down Expand Up @@ -616,89 +699,6 @@ static void device_process_new(Manager *m, sd_device *dev) {
}
}

static void device_found_changed(Device *d, DeviceFound previous, DeviceFound now) {
assert(d);

/* Didn't exist before, but does now? if so, generate a new invocation ID for it */
if (previous == DEVICE_NOT_FOUND && now != DEVICE_NOT_FOUND)
(void) unit_acquire_invocation_id(UNIT(d));

if (FLAGS_SET(now, DEVICE_FOUND_UDEV))
/* When the device is known to udev we consider it plugged. */
device_set_state(d, DEVICE_PLUGGED);
else if (now != DEVICE_NOT_FOUND && !FLAGS_SET(previous, DEVICE_FOUND_UDEV))
/* If the device has not been seen by udev yet, but is now referenced by the kernel, then we assume the
* kernel knows it now, and udev might soon too. */
device_set_state(d, DEVICE_TENTATIVE);
else
/* If nobody sees the device, or if the device was previously seen by udev and now is only referenced
* from the kernel, then we consider the device is gone, the kernel just hasn't noticed it yet. */
device_set_state(d, DEVICE_DEAD);
}

static void device_update_found_one(Device *d, DeviceFound found, DeviceFound mask) {
Manager *m;

assert(d);

m = UNIT(d)->manager;

if (MANAGER_IS_RUNNING(m) && (m->honor_device_enumeration || MANAGER_IS_USER(m))) {
DeviceFound n, previous;

/* When we are already running, then apply the new mask right-away, and trigger state changes
* right-away */

n = (d->found & ~mask) | (found & mask);
if (n == d->found)
return;

previous = d->found;
d->found = n;

device_found_changed(d, previous, n);
} else
/* We aren't running yet, let's apply the new mask to the shadow variable instead, which we'll apply as
* soon as we catch-up with the state. */
d->enumerated_found = (d->enumerated_found & ~mask) | (found & mask);
}

static void device_update_found_by_sysfs(Manager *m, const char *sysfs, DeviceFound found, DeviceFound mask) {
Device *l;

assert(m);
assert(sysfs);

if (mask == 0)
return;

l = hashmap_get(m->devices_by_sysfs, sysfs);
LIST_FOREACH(same_sysfs, d, l)
device_update_found_one(d, found, mask);
}

static void device_update_found_by_name(Manager *m, const char *path, DeviceFound found, DeviceFound mask) {
_cleanup_free_ char *e = NULL;
Unit *u;
int r;

assert(m);
assert(path);

if (mask == 0)
return;

r = unit_name_from_path(path, ".device", &e);
if (r < 0)
return (void) log_debug_errno(r, "Failed to generate unit name from device path, ignoring: %m");

u = manager_get_unit(m, e);
if (!u)
return;

device_update_found_one(DEVICE(u), found, mask);
}

static bool device_is_ready(sd_device *dev) {
assert(dev);

Expand Down

0 comments on commit 086fcdd

Please sign in to comment.