keventd: add support for toggling debug messages w/ SIGUSR1

Useful to debug system events after the system has started up and you
don't want to rebuild Finit to add the `-d` command line flag.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2022-04-11 22:38:35 +02:00
1 parent 014e457fda
commit c24855d4d5
1 file changed
+41 -5
+41 -5
View File
@@ -24,6 +24,7 @@
#include <errno.h> #include <errno.h>
#include <dirent.h> #include <dirent.h>
#include <poll.h> #include <poll.h>
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
@@ -52,6 +53,10 @@
static int num_ac_online; static int num_ac_online;
static int num_ac; static int num_ac;
static int running = 1;
static int level;
static int logon;
int debug; /* debug in other modules as well */ int debug; /* debug in other modules as well */
void logit(int prio, const char *fmt, ...) void logit(int prio, const char *fmt, ...)
@@ -59,9 +64,9 @@ void logit(int prio, const char *fmt, ...)
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
if (!debug) if (logon)
vsyslog(prio, fmt, ap); vsyslog(prio, fmt, ap);
else { else if (prio <= level) {
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
@@ -179,6 +184,26 @@ static void init(void)
sys_cond("pwr/ac", 1); sys_cond("pwr/ac", 1);
} }
static void set_logging(int prio)
{
setlogmask(LOG_UPTO(prio));
level = prio;
}
static void toggle_debug(int signo)
{
(void)signo;
debug ^= 1;
set_logging(debug ? LOG_DEBUG : LOG_NOTICE);
}
static void shut_down(int signo)
{
(void)signo;
running = 0;
}
/* /*
* Started by Finit as soon as possible when base filesystem is up, * Started by Finit as soon as possible when base filesystem is up,
* modules have been probed, or insmodded from /etc/finit.conf, so by * modules have been probed, or insmodded from /etc/finit.conf, so by
@@ -198,10 +223,13 @@ int main(int argc, char *argv[])
} }
if (!debug) { if (!debug) {
openlog("keventd", LOG_CONS | LOG_PID, LOG_DAEMON); openlog("keventd", LOG_PID, LOG_DAEMON);
setlogmask(LOG_UPTO(LOG_NOTICE)); set_logging(LOG_NOTICE);
logon = 1;
} }
signal(SIGUSR1, toggle_debug);
signal(SIGTERM, shut_down);
init(); init();
pfd.events = POLLIN; pfd.events = POLLIN;
@@ -216,11 +244,18 @@ int main(int argc, char *argv[])
panic("bind failed"); panic("bind failed");
logit(LOG_DEBUG, "Waiting for events ..."); logit(LOG_DEBUG, "Waiting for events ...");
while (-1 != poll(&pfd, 1, -1)) { while (running) {
char *path = NULL; char *path = NULL;
int ac = 0; int ac = 0;
int i, len; int i, len;
if (-1 == poll(&pfd, 1, -1)) {
if (errno == EINTR)
continue;
break;
}
len = recv(pfd.fd, buf, sizeof(buf), MSG_DONTWAIT); len = recv(pfd.fd, buf, sizeof(buf), MSG_DONTWAIT);
if (len == -1) { if (len == -1) {
switch (errno) { switch (errno) {
@@ -235,6 +270,7 @@ int main(int argc, char *argv[])
} }
} }
buf[len] = 0; buf[len] = 0;
logit(LOG_DEBUG, "%s", buf);
/* skip libusb events, focus on kernel events/changes */ /* skip libusb events, focus on kernel events/changes */
if (strncmp(buf, "change@", 7)) if (strncmp(buf, "change@", 7))