From 367a2597c35140fb325e2f94235961257130dfe1 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 30 Apr 2022 02:35:16 +0900 Subject: [PATCH] core/device: store the original path The unit name may be hashed. Hence, we cannot obtain the original path from the unit name. The path will be used in the later commits. --- src/core/device.c | 21 ++++++++++++++++++++- src/core/device.h | 1 + 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/core/device.c b/src/core/device.c index e3c9ada23f..2defc4d128 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -116,6 +116,7 @@ static void device_done(Unit *u) { device_unset_sysfs(d); d->wants_property = strv_free(d->wants_property); + d->path = mfree(d->path); } static int device_load(Unit *u) { @@ -293,6 +294,9 @@ static int device_serialize(Unit *u, FILE *f, FDSet *fds) { assert(f); assert(fds); + if (d->path) + (void) serialize_item(f, "path", d->path); + (void) serialize_item(f, "state", device_state_to_string(d->state)); if (device_found_to_string_many(d->found, &s) >= 0) @@ -311,7 +315,14 @@ static int device_deserialize_item(Unit *u, const char *key, const char *value, assert(value); assert(fds); - if (streq(key, "state")) { + if (streq(key, "path")) { + if (!d->path) { + d->path = strdup(value); + if (!d->path) + log_oom_debug(); + } + + } else if (streq(key, "state")) { DeviceState state; state = device_state_from_string(value); @@ -341,9 +352,11 @@ static void device_dump(Unit *u, FILE *f, const char *prefix) { fprintf(f, "%sDevice State: %s\n" + "%sDevice Path: %s\n" "%sSysfs Path: %s\n" "%sFound: %s\n", prefix, device_state_to_string(d->state), + prefix, strna(d->path), prefix, strna(d->sysfs), prefix, strna(s)); @@ -566,6 +579,12 @@ static int device_setup_unit(Manager *m, sd_device *dev, const char *path, bool unit_add_to_load_queue(u); } + if (!DEVICE(u)->path) { + DEVICE(u)->path = strdup(path); + if (!DEVICE(u)->path) + return log_oom(); + } + /* If this was created via some dependency and has not actually been seen yet ->sysfs will not be * initialized. Hence initialize it if necessary. */ if (sysfs) { diff --git a/src/core/device.h b/src/core/device.h index dfe8a13aff..7584bc70c4 100644 --- a/src/core/device.h +++ b/src/core/device.h @@ -21,6 +21,7 @@ struct Device { Unit meta; char *sysfs; + char *path; /* syspath, device node, alias, or devlink */ /* In order to be able to distinguish dependencies on different device nodes we might end up creating multiple * devices for the same sysfs path. We chain them up here. */ -- 2.25.1