From 47986995dec30eec382ebad67d0d59907fe6df23 Mon Sep 17 00:00:00 2001 From: Leonid Krivoshein Date: Mon, 25 Sep 2023 00:07:27 +0300 Subject: [PATCH 1/1] udev.c: redirect udev messages to /var/log/udevd.log --- udev.c | 55 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/udev.c b/udev.c index 0c211a9..e64dd0a 100644 --- a/udev.c +++ b/udev.c @@ -19,13 +19,14 @@ * */ +#define _GNU_SOURCE #include #include #include #include +#include #include #include -#include #include #include #include @@ -49,17 +50,7 @@ extern char * const env[]; static pid_t udevd_pid; -static void spawn_wait(char * const command[], const char *message) -{ - int status = 0; - - if (waitpid(spawn(command), &status, 0) < 0 || !(WIFEXITED(status))) { - warn(message); - } -} - - -static void spawn_silent(char * const command[]) +static pid_t spawn_silent(char * const command[], const char *log) { int fd = 0; pid_t pid = fork(); @@ -67,19 +58,31 @@ static void spawn_silent(char * const command[]) if (pid < 0) fatal(command[0]); else if (pid == 0) { - fd = open("/dev/null", O_WRONLY, 0666); - if (fd >= 0) { - dup2(fd, 1); - dup2(fd, 2); - close(fd); - } + if (log) + fd = creat(log, 0640); + else + fd = open(log = "/dev/null", O_WRONLY, 0644); + if (fd < 0) + fatal(log); + close(1); + close(2); + dup3(fd, 1, O_CLOEXEC); + dup3(1, 2, O_CLOEXEC); + close(fd); execve(command[0], &command[1], env); - perror(command[0]); - exit(1); + fatal(command[0]); } - if (waitpid(pid, &fd, 0) >= 0) { - fd = WIFEXITED(fd); + return pid; +} + + +static void spawn_wait(char * const command[], const char *message) +{ + int status = 0; + + if (waitpid(spawn_silent(command, NULL), &status, 0) < 0 || !(WIFEXITED(status))) { + warn(message); } } @@ -105,16 +108,16 @@ void udev_start(void) printf("Spawning udevd... "); - udevd_pid = spawn(udevd); + udevd_pid = spawn_silent(udevd, "/var/log/udevd.log"); spawn_wait(priority, "udev_priority"); spawn_wait(startup, "udev_startup"); - spawn_silent(reload); + spawn_silent(reload, NULL); if (mkdir_dev(".udev/queue") < 0) fatal("cannot create /dev/.udev/queue"); - spawn_silent(subsys); - spawn_silent(devices); + spawn_silent(subsys, NULL); + spawn_silent(devices, NULL); udev_settle(); printf("done\n"); -- 2.34.1