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:
Paweł committed 2026-10-14 10:30:17 +02:00
1 parent 530d6703f1
commit a22a5fae7c
3 files changed
+52 -7

No files matched your search

+8 -7
View File
@@ -602,12 +602,12 @@ static void subst_value(const char *src, char *buf, size_t bufsz,
if (al >= sizeof(attr))
al = sizeof(attr) - 1;
memcpy(attr, as, al);
attr[al] = 0;
if (!sysfs_read_attr(ev->devpath, attr,
tmp, sizeof(tmp)))
append_str(buf, &pos, bufsz, tmp);
p = ae + 1;
memcpy(attr, as, al);
attr[al] = 0;
if (!sysfs_read_attr_chain(ev->devpath, attr,
tmp, sizeof(tmp)))
append_str(buf, &pos, bufsz, tmp);
p = ae + 1;
} else {
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;
memcpy(attr, as, al);
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);
p = ae + 1;
} else {
+35
View File
@@ -61,6 +61,41 @@ int sysfs_read_attr(const char *devpath, const char *attr,
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 (*cb)(const char *syspath, void *data),
void *data)
+9
View File
@@ -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,
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,
* calling cb at each level. cb receives the absolute syspath and the