nldev

NetLink DEVice manager; a lightweight netlink frontend for mdev.
git clone git://r-36.net/nldev
Log | Files | Refs | LICENSE

commit b9f67568d14c2dead805172f0d42ad6c9f98cdf2
parent e0713f648e6bc486632c261e29d36c052c54f381
Author: Platon Ryzhikov <ihummer63@yandex.ru>
Date:   Sat, 11 Sep 2021 15:24:27 +0300

if neither subsystem nor runpath is specified, process uevents based on compiled-in config

Signed-off-by: Christoph Lohmann <20h@r-36.net>

Diffstat:
Makefile | 7+++++--
config.def.h | 5+++++
nldev.8 | 6+++++-
nldev.c | 32++++++++++++++++++++++++++++++--
4 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile @@ -18,7 +18,10 @@ options: @echo CC $< @${CC} -c ${CFLAGS} $< -${OBJ}: config.mk +${OBJ}: config.h config.mk + +config.h: + cp config.def.h $@ ${NAME}: ${OBJ} @echo CC -o $@ @@ -31,7 +34,7 @@ clean: dist: clean @echo creating dist tarball @mkdir -p ${NAME}-${VERSION} - @cp -R LICENSE Makefile README.md FIXES.md config.mk \ + @cp -R LICENSE Makefile README.md FIXES.md config.def.h config.mk \ ${SRC} ${NAME}.8 *.h ${NAME}-${VERSION} @tar -cf ${NAME}-${VERSION}.tar ${NAME}-${VERSION} @gzip ${NAME}-${VERSION}.tar diff --git a/config.def.h b/config.def.h @@ -0,0 +1,5 @@ +static const Rule rules[] = { + /* ACTION SUBSYSTEM other env variables command to run */ + { "add", NULL, "DEVNAME", "/bin/mdev" }, + { "remove", NULL, "DEVNAME", "/bin/mdev" }, +}; diff --git a/nldev.8 b/nldev.8 @@ -42,7 +42,8 @@ Turn on debug messages. This will not work in conjunction with . .It Fl f Ar subsystem This option will filter for the subsystem key in a netlink message -and compare it to the subsystem string. +and compare it to the subsystem string. On success runpath helper +will be run. . .It Fl h Show usage. @@ -58,6 +59,9 @@ Only show netlink messages from libudev. This option specifies the runpath for the helper that is launched on every received netlink event (default: /bin/mdev). .El + +If neither subsystem nor runpath is specified, uevents are processed +based on compiled-in config. . .Sh AUTHORS See the LICENSE file for the authors of this software. diff --git a/nldev.c b/nldev.c @@ -21,7 +21,17 @@ #include <linux/types.h> #include <linux/netlink.h> +typedef struct { + const char *action; /* ACTION to run rule for */ + const char *subsystem; /* SUBSYSTEM to run the rule for, NULL for any */ + const char *envvar; /* other environment variable to run rule for, NULL for any */ + const char *runpath; +} Rule; + #include "arg.h" +#include "config.h" + +#define LENGTH(X) (sizeof X / sizeof X[0]) char *argv0; int listfd = -1; @@ -199,7 +209,7 @@ main(int argc, char *argv[]) showkernel = 1; showudev = 1; subsystem = NULL; - runpath = "/bin/mdev"; + runpath = NULL; ARGBEGIN { case 'b': @@ -210,6 +220,8 @@ main(int argc, char *argv[]) break; case 'f': subsystem = EARGF(usage()); + if (!runpath) + runpath = "/bin/mdev"; break; case 'k': showudev = 0; @@ -324,7 +336,23 @@ main(int argc, char *argv[]) getenv("DEVPATH") != NULL && getenv("SUBSYSTEM") != NULL && getenv("SEQNUM") != NULL) { - child(runpath); + if (runpath) + child(runpath); + else { + for (i = 0; i < LENGTH(rules); i+=1) { + if (rules[i].action == NULL || rules[i].runpath == NULL) /* rule must have non-NULL action and runpath */ + continue; + if (strcmp(getenv("ACTION"), rules[i].action)) + continue; + if (rules[i].subsystem != NULL) + if (strcmp(getenv("SUBSYSTEM"), rules[i].subsystem)) + continue; + if (rules[i].envvar != NULL) + if (getenv(rules[i].envvar) == NULL) + continue; + child(rules[i].runpath); + } + } } }