mirror of
https://github.com/troglobit/finit.git
synced 2026-11-13 17:00:16 +00:00
keventd: resolve $attr{} and %s{} through the parent chain
Attribute substitution only looked at the device's own sysfs directory,
while the matching key ATTRS{} walks parents. Devices without an
attribute of their own, partitions in particular whose serial lives in
the parent disk, therefore substituted to an empty string.
This commit is contained in:
3 files changed
+52
-7
No files matched your search
+8
-7
@@ -602,12 +602,12 @@ static void subst_value(const char *src, char *buf, size_t bufsz,
|
|||||||
|
|
||||||
if (al >= sizeof(attr))
|
if (al >= sizeof(attr))
|
||||||
al = sizeof(attr) - 1;
|
al = sizeof(attr) - 1;
|
||||||
memcpy(attr, as, al);
|
memcpy(attr, as, al);
|
||||||
attr[al] = 0;
|
attr[al] = 0;
|
||||||
if (!sysfs_read_attr(ev->devpath, attr,
|
if (!sysfs_read_attr_chain(ev->devpath, attr,
|
||||||
tmp, sizeof(tmp)))
|
tmp, sizeof(tmp)))
|
||||||
append_str(buf, &pos, bufsz, tmp);
|
append_str(buf, &pos, bufsz, tmp);
|
||||||
p = ae + 1;
|
p = ae + 1;
|
||||||
} else {
|
} else {
|
||||||
buf[pos++] = '%'; buf[pos] = 0;
|
buf[pos++] = '%'; buf[pos] = 0;
|
||||||
}
|
}
|
||||||
@@ -681,7 +681,8 @@ static void subst_value(const char *src, char *buf, size_t bufsz,
|
|||||||
al = sizeof(attr) - 1;
|
al = sizeof(attr) - 1;
|
||||||
memcpy(attr, as, al);
|
memcpy(attr, as, al);
|
||||||
attr[al] = 0;
|
attr[al] = 0;
|
||||||
if (!sysfs_read_attr(ev->devpath, attr, tmp, sizeof(tmp)))
|
if (!sysfs_read_attr_chain(ev->devpath, attr,
|
||||||
|
tmp, sizeof(tmp)))
|
||||||
append_str(buf, &pos, bufsz, tmp);
|
append_str(buf, &pos, bufsz, tmp);
|
||||||
p = ae + 1;
|
p = ae + 1;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -61,6 +61,41 @@ int sysfs_read_attr(const char *devpath, const char *attr,
|
|||||||
return sysfs_read_file(path, buf, len);
|
return sysfs_read_file(path, buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct attr_ctx {
|
||||||
|
const char *attr;
|
||||||
|
char *buf;
|
||||||
|
size_t len;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int attr_cb(const char *syspath, void *data)
|
||||||
|
{
|
||||||
|
struct attr_ctx *ctx = data;
|
||||||
|
char path[PATH_MAX];
|
||||||
|
|
||||||
|
snprintf(path, sizeof(path), "%s/%s", syspath, ctx->attr);
|
||||||
|
if (sysfs_read_file(path, ctx->buf, ctx->len) == 0)
|
||||||
|
return 0; /* found, stop walking */
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sysfs_read_attr_chain(const char *devpath, const char *attr,
|
||||||
|
char *buf, size_t len)
|
||||||
|
{
|
||||||
|
struct attr_ctx ctx = { attr, buf, len };
|
||||||
|
|
||||||
|
if (!devpath || !attr || !*attr)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
/* Device itself first then walk up toward the parent devices */
|
||||||
|
if (sysfs_read_attr(devpath, attr, buf, len) == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
sysfs_parent_walk(devpath, attr_cb, &ctx);
|
||||||
|
|
||||||
|
return buf[0] ? 0 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
int sysfs_parent_walk(const char *devpath,
|
int sysfs_parent_walk(const char *devpath,
|
||||||
int (*cb)(const char *syspath, void *data),
|
int (*cb)(const char *syspath, void *data),
|
||||||
void *data)
|
void *data)
|
||||||
|
|||||||
@@ -40,6 +40,15 @@ int sysfs_read_file(const char *path, char *buf, size_t len);
|
|||||||
int sysfs_read_attr(const char *devpath, const char *attr,
|
int sysfs_read_attr(const char *devpath, const char *attr,
|
||||||
char *buf, size_t len);
|
char *buf, size_t len);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Read a sysfs attribute for the device at /sys{devpath}, falling back
|
||||||
|
* to the parent chain, partitions have no "serial" of their own, it
|
||||||
|
* lives in the parent disk's sysfs directory.
|
||||||
|
* Returns 0 on success, -1 if no level of the chain has the attribute.
|
||||||
|
*/
|
||||||
|
int sysfs_read_attr_chain(const char *devpath, const char *attr,
|
||||||
|
char *buf, size_t len);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Walk the sysfs device tree upward from devpath toward /sys/devices,
|
* Walk the sysfs device tree upward from devpath toward /sys/devices,
|
||||||
* calling cb at each level. cb receives the absolute syspath and the
|
* calling cb at each level. cb receives the absolute syspath and the
|
||||||
|
|||||||
Reference in new issue
Block a user