mirror of
https://github.com/troglobit/finit.git
synced 2026-11-08 16:11:10 +00:00
keventd: add udev rules engine
Transform keventd from a power-supply monitor + basic hotplug handler into
a full udev-compatible device manager.
Rules engine (rules.c):
- Full .rules file parser covering all udev key types: ACTION, KERNEL,
SUBSYSTEM, DEVPATH, ENV, ATTR, SYSCTL, TAG, RESULT, PROGRAM, TEST,
parent-chain KERNELS/SUBSYSTEMS/ATTRS/DRIVERS, and more
- Pattern matching: plain string, fnmatch glob, and pipe-separated alternatives
- Operators: ==, !=, =, +=, -=, :=
- Assignments: NAME=, MODE=, OWNER=, GROUP=, SYMLINK+=, ENV{k}=, TAG+=, RUN+=
- IMPORT{program|file|builtin|parent|cmdline|db}=
- PROGRAM= with stdout capture for subsequent RESULT== matching
- GOTO=/LABEL= flow control
- Loads *.rules from /lib/udev/rules.d, /run/udev/rules.d, /etc/udev/rules.d
and an optional extra directory (-r DIR); reloads on SIGHUP
Builtin framework (builtin.c):
- kmod: load module by MODALIAS or explicit alias
- hwdb: match device against *.hwdb text files in udev hwdb dirs; builds
correct lookup key per subsystem — evdev:input:b*v*p*e* for input,
usb:v*p* for USB, raw modalias for PCI/platform
- path_id: build stable ID_PATH / ID_PATH_TAG from sysfs topology (PCI, USB,
ATA, NVMe, platform, ACPI, virtio)
- usb_id: read idVendor/idProduct/bcdDevice/serial from sysfs; look up
ID_VENDOR_FROM_DATABASE and ID_MODEL_FROM_DATABASE from usb.ids
(hwdata package) when available; silent fallback when absent
- input_id: classify input devices (keyboard, mouse, joystick, touchscreen,
touchpad) from evdev capability bitmasks in sysfs
- net_id: generate predictable names — MAC-based enx<mac> and PCI-slot-based
enp<bus>s<dev>[f<func>]
- blkid: probe filesystem type, UUID, and label via libblkid; sets ID_FS_*
and ID_PART_TABLE_* properties
Network interface renaming (uevent.c):
- netdev_add() renames interfaces via SIOCSIFNAME when a NAME= rule matched,
then sets the Finit dev/ condition on the final name; and any setup using
persistent interface naming via udev rules
Device node and symlink improvements (uevent.c):
- NAME=, MODE=, OWNER=, GROUP= overrides from matched rules applied at
mknod/chown time, falling back to the built-in permission table
- SYMLINK+= links from rules applied alongside built-in by-id/by-path links
Device property database (udevdb.c):
- Persist per-device E:/S:/I: records to /run/udev/data/ on ADD/CHANGE,
delete on REMOVE; IMPORT{db}= restores saved properties into event env
Build:
- libblkid (util-linux) is now required for keventd
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
17 files changed
+4451
-44
No files matched your search
+79
-6
@@ -34,6 +34,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <dirent.h>
|
||||
#include <getopt.h>
|
||||
@@ -60,6 +61,8 @@
|
||||
#endif
|
||||
|
||||
#include "keventd.h"
|
||||
#include "rules.h"
|
||||
#include "udevdb.h"
|
||||
#include "cond.h"
|
||||
#include "pid.h"
|
||||
#include "util.h"
|
||||
@@ -78,6 +81,10 @@ static int level;
|
||||
static int logon;
|
||||
static int passive; /* power-supply only, no device management */
|
||||
|
||||
static struct rule_list rules = TAILQ_HEAD_INITIALIZER(rules);
|
||||
static char *rules_dir; /* extra rules dir from -r option */
|
||||
static volatile sig_atomic_t reload_rules;
|
||||
|
||||
int debug; /* debug in other modules as well */
|
||||
|
||||
void logit(int prio, const char *fmt, ...)
|
||||
@@ -267,6 +274,27 @@ static void power_supply_change(const struct uevent *ev, char *buf, size_t len)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* True if rules have queued the kmod builtin for this event. Used to
|
||||
* suppress the direct modprobe path when 80-drivers.rules will fork
|
||||
* modprobe anyway -- otherwise coldplug runs modprobe twice per event.
|
||||
*/
|
||||
static int applied_has_kmod(const struct uevent *ev)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ev->applied.nruncmds; i++) {
|
||||
const char *cmd = ev->applied.run_cmds[i];
|
||||
|
||||
if (ev->applied.run_types[i] != RUN_BUILTIN)
|
||||
continue;
|
||||
if (!strncmp(cmd, "kmod", 4) &&
|
||||
(cmd[4] == '\0' || isspace((unsigned char)cmd[4])))
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle a single uevent from the kernel.
|
||||
*/
|
||||
@@ -282,6 +310,10 @@ static void handle_uevent(char *buf, size_t len)
|
||||
ev.subsystem ?: "", ev.devname ?: "",
|
||||
ev.major, ev.minor);
|
||||
|
||||
/* Apply udev rules before built-in device handling */
|
||||
if (!passive)
|
||||
rules_apply(&rules, &ev);
|
||||
|
||||
switch (ev.action) {
|
||||
case ACT_ADD:
|
||||
if (!passive) {
|
||||
@@ -289,14 +321,17 @@ static void handle_uevent(char *buf, size_t len)
|
||||
if (ev.firmware)
|
||||
firmware_load(&ev);
|
||||
|
||||
/* Module loading */
|
||||
if (ev.modalias)
|
||||
if (ev.modalias && !applied_has_kmod(&ev))
|
||||
modprobe_load(ev.modalias);
|
||||
|
||||
/* Create device node if we have the info */
|
||||
if (ev.major >= 0 && ev.minor >= 0 && ev.devname)
|
||||
devnode_add(&ev);
|
||||
|
||||
/* Rename network interface if NAME= rule was applied */
|
||||
if (ev.subsystem && !strcmp(ev.subsystem, "net"))
|
||||
netdev_add(&ev);
|
||||
|
||||
/* Create symlinks */
|
||||
symlink_add(&ev);
|
||||
}
|
||||
@@ -307,8 +342,9 @@ static void handle_uevent(char *buf, size_t len)
|
||||
/* Remove symlinks first */
|
||||
symlink_del(&ev);
|
||||
|
||||
/* Remove device node */
|
||||
if (ev.devname)
|
||||
/* Remove device node, mirrors devnode_add(): net
|
||||
* interfaces carry a devname but have no node. */
|
||||
if (ev.major >= 0 && ev.minor >= 0 && ev.devname)
|
||||
devnode_del(&ev);
|
||||
}
|
||||
break;
|
||||
@@ -327,6 +363,20 @@ static void handle_uevent(char *buf, size_t len)
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
/* Execute RUN+= commands from matched rules */
|
||||
if (!passive)
|
||||
rules_run_cmds(&ev);
|
||||
|
||||
/* Persist device properties to /run/udev/data/ */
|
||||
if (!passive) {
|
||||
if (ev.action == ACT_REMOVE)
|
||||
udevdb_delete(&ev);
|
||||
else
|
||||
udevdb_write(&ev);
|
||||
}
|
||||
|
||||
uevent_env_free(&ev);
|
||||
}
|
||||
|
||||
static void init_power_supply(void)
|
||||
@@ -414,10 +464,16 @@ static void shut_down(int signo)
|
||||
running = 0;
|
||||
}
|
||||
|
||||
static void sighup_handler(int signo)
|
||||
{
|
||||
(void)signo;
|
||||
reload_rules = 1;
|
||||
}
|
||||
|
||||
static int usage(int rc)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Usage: keventd [-dGhnpv] [-c] [-g GROUP]\n"
|
||||
"Usage: keventd [-dGhnpv] [-c] [-g GROUP] [-r DIR]\n"
|
||||
"\n"
|
||||
"Options:\n"
|
||||
" -c Run coldplug at startup\n"
|
||||
@@ -427,6 +483,7 @@ static int usage(int rc)
|
||||
" -h Show this help text\n"
|
||||
" -n Run in foreground (no daemon)\n"
|
||||
" -p Passive mode: power supply events only (no device management)\n"
|
||||
" -r DIR Extra rules directory (in addition to standard udev paths)\n"
|
||||
" -v Show version\n"
|
||||
"\n", REBC_DEFAULT_NLGROUP);
|
||||
|
||||
@@ -460,7 +517,7 @@ int main(int argc, char *argv[])
|
||||
* requested bits verbatim instead of masking them. */
|
||||
umask(0);
|
||||
|
||||
while ((c = getopt(argc, argv, "cdg:Ghnpv")) != -1) {
|
||||
while ((c = getopt(argc, argv, "cdg:Ghnpr:v")) != -1) {
|
||||
switch (c) {
|
||||
case 'c':
|
||||
do_coldplug = 1;
|
||||
@@ -483,6 +540,9 @@ int main(int argc, char *argv[])
|
||||
case 'p':
|
||||
passive = 1;
|
||||
break;
|
||||
case 'r':
|
||||
rules_dir = optarg;
|
||||
break;
|
||||
case 'v':
|
||||
printf("keventd v%s\n", KEVENTD_VERSION);
|
||||
return 0;
|
||||
@@ -501,6 +561,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
signal(SIGUSR1, toggle_debug);
|
||||
signal(SIGTERM, shut_down);
|
||||
signal(SIGHUP, sighup_handler);
|
||||
signal(SIGCHLD, SIG_IGN); /* Don't wait for modprobe children */
|
||||
|
||||
/* Initialize condition directories */
|
||||
@@ -539,6 +600,10 @@ int main(int argc, char *argv[])
|
||||
if (do_coldplug)
|
||||
coldplug();
|
||||
|
||||
/* Load udev rules from standard directories (and -r extra_dir) */
|
||||
if (!passive)
|
||||
rules_load_all(&rules, rules_dir);
|
||||
|
||||
pidfile(NULL);
|
||||
logit(LOG_NOTICE, "keventd v%s started, waiting for events...", KEVENTD_VERSION);
|
||||
|
||||
@@ -546,6 +611,13 @@ int main(int argc, char *argv[])
|
||||
char rebc_buf[UEVENT_BUFFER_SIZE];
|
||||
int len;
|
||||
|
||||
/* Reload rules if SIGHUP was received */
|
||||
if (reload_rules) {
|
||||
reload_rules = 0;
|
||||
rules_free(&rules);
|
||||
rules_load_all(&rules, rules_dir);
|
||||
}
|
||||
|
||||
if (-1 == poll(&pfd, 1, -1)) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
@@ -592,6 +664,7 @@ int main(int argc, char *argv[])
|
||||
if (rebc_fd != -1)
|
||||
close(rebc_fd);
|
||||
close(pfd.fd);
|
||||
rules_free(&rules);
|
||||
logit(LOG_NOTICE, "keventd shutting down");
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in new issue
Block a user