From 823bb4cf2a736f9bfbc9a098b8d7380e0b9ed54b Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Tue, 12 Nov 2019 15:51:33 +0100 Subject: [PATCH] Support for building the project w/o separate klogd (default) Signed-off-by: Joachim Nilsson --- configure.ac | 60 +++++++++++++++++++++++++++++++++-------- man/Makefile.am | 6 ++++- src/Makefile.am | 8 ++++-- src/socket.c | 14 +++++----- src/syslogd.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++--- src/syslogd.h | 4 +++ 6 files changed, 140 insertions(+), 23 deletions(-) diff --git a/configure.ac b/configure.ac index c927546..63bd764 100644 --- a/configure.ac +++ b/configure.ac @@ -49,14 +49,18 @@ AC_CONFIG_LIBOBJ_DIR([lib]) AC_CHECK_FUNCS([getprogname strtobytes]) # Command line options +AC_ARG_WITH(klogd, + AS_HELP_STRING([--with-klogd], [Build a separate klogd, default: disabled]), + [klogd=$withval], [klogd='no']) + +AC_ARG_WITH(klogd-delay, + AS_HELP_STRING([--with-klogd-delay=SEC], [When klogd is started at the same time as syslogd, default: 0]), + [klogd_delay=$withval], [klogd_delay='no']) + AC_ARG_WITH(suspend-time, AS_HELP_STRING([--with-suspend-time=SEC], [Retry timeout for remote syslogd servers, default: 180]), [suspend_time=$withval], [suspend_time='no']) -AC_ARG_WITH(klogd-delay, - AS_HELP_STRING([--with-klogd-delay=SEC], [when started at the same time as syslogd, default: 0]), - [klogd_delay=$withval], [klogd_delay='no']) - AC_ARG_WITH(syslogd-pidfile, AS_HELP_STRING([--with-syslogd-pidfile=FILE], [custom PID file, default: syslogd.pid]), [syslogd_pidfile=$withval], [syslogd_pidfile='no']) @@ -65,23 +69,32 @@ AC_ARG_WITH(systemd, [AS_HELP_STRING([--with-systemd=DIR], [Directory for systemd service files])],, [with_systemd=auto]) -AS_IF([test "x$suspend_time" != "xno"],[ - AS_IF([test "x$suspend_time" = "xyes"],[ - AC_MSG_ERROR([Must supply argument])]) - ] - AC_DEFINE_UNQUOTED(INET_SUSPEND_TIME, $suspend_time, [Retry timeout for remote syslgod servers, default: 180])) +AS_IF([test "x$klogd" != "xno"], + with_klogd="yes" + AC_DEFINE(KLOGD, 1, [Build with klogd, default: built-in /dev/kmsg support in syslogd]), + with_klogd="no") +AM_CONDITIONAL([ENABLE_KLOGD], [test "x$with_klogd" != "xno"]) AS_IF([test "x$klogd_delay" != "xno"],[ AS_IF([test "x$klogd_delay" = "xyes"],[ AC_MSG_ERROR([Must supply argument])]) ] - AC_DEFINE_UNQUOTED(KLOGD_DELAY, $klogd_delay, [Delay klogd startup N seconds, default: 0])) + AC_DEFINE_UNQUOTED(KLOGD_DELAY, $klogd_delay, [Delay klogd startup N seconds, default: 0]), + klogd_delay=0) + +AS_IF([test "x$suspend_time" != "xno"],[ + AS_IF([test "x$suspend_time" = "xyes"],[ + AC_MSG_ERROR([Must supply argument])]) + ] + AC_DEFINE_UNQUOTED(INET_SUSPEND_TIME, $suspend_time, [Retry timeout for remote syslgod servers, default: 180]), + suspend_time=180) AS_IF([test "x$syslogd_pidfile" != "xno"],[ AS_IF([test "x$syslogd_pidfile" = "xyes"],[ AC_MSG_ERROR([Must supply argument])]) ] - AC_DEFINE_UNQUOTED(SYSLOGD_PIDNAME, "$syslogd_pidfile", [Custom syslogd PID file])) + AC_DEFINE_UNQUOTED(SYSLOGD_PIDNAME, "$syslogd_pidfile", [Custom syslogd PID file]), + syslogd_pidfile="syslogd.pid") # Check where to install the systemd .service file AS_IF([test "x$with_systemd" = "xyes" -o "x$with_systemd" = "xauto"], [ @@ -105,3 +118,28 @@ SBINDIR=`eval echo $SBINDIR` AC_SUBST(SBINDIR) AC_OUTPUT + +cat <ai.ai_addr = calloc(1, sizeof(struct sockaddr_un)); - if (!entry->ai.ai_addr) - goto eaddr; + if (ai) { + entry->ai.ai_addr = calloc(1, sizeof(struct sockaddr_un)); + if (!entry->ai.ai_addr) + goto eaddr; - entry->ai = *ai; - *entry->ai.ai_addr = *ai->ai_addr; + entry->ai = *ai; + *entry->ai.ai_addr = *ai->ai_addr; + } entry->sd = sd; entry->cb = cb; diff --git a/src/syslogd.c b/src/syslogd.c index ef0afc3..a61ad1b 100644 --- a/src/syslogd.c +++ b/src/syslogd.c @@ -155,7 +155,10 @@ static SIMPLEQ_HEAD(, peer) pqueue = SIMPLEQ_HEAD_INITIALIZER(pqueue); /* Function prototypes. */ void untty(void); static void parsemsg(const char *from, char *msg); -void printsys(char *msg); +#ifndef KLOGD +static int opensys(const char *file); +static void printsys(char *msg); +#endif static void logmsg(struct buf_msg *buffer); static void fprintlog(struct filed *f, struct buf_msg *buffer); void endtty(); @@ -237,7 +240,7 @@ int main(int argc, char *argv[]) int pflag = 0, bflag = 0; int ch; -#ifndef WITHOUT_KLOGD +#ifdef KLOGD /* * When building with klogd enabled this works around filtering * of LOG_KERN messages in parsemsg(). Otherwise it needs to be @@ -349,6 +352,12 @@ int main(int argc, char *argv[]) .pe_mode = 0666, }); +#ifndef KLOGD + /* Attempt to open kernel log pipe */ + if (opensys(_PATH_KLOG)) + err(1, "Faield opening %s", _PATH_KLOG); +#endif + if ((!Foreground) && (!Debug)) { signal(SIGTERM, doexit); chdir("/"); @@ -456,6 +465,61 @@ int main(int argc, char *argv[]) } } +#ifndef KLOGD +/* + * Read /dev/klog while data are available, split into lines. + */ +static void kernel_cb(int fd, void *arg) +{ + char *p, *q, line[MAXLINE + 1]; + int len, i; + + len = 0; + for (;;) { + i = read(fd, line + len, MAXLINE - 1 - len); + if (i > 0) { + line[i + len] = '\0'; + } else { + if (i < 0 && errno != EINTR && errno != EAGAIN) { + ERR("klog"); + socket_close(fd); + } + break; + } + + for (p = line; (q = strchr(p, '\n')) != NULL; p = q + 1) { + *q = '\0'; + printsys(p); + } + len = strlen(p); + if (len >= MAXLINE - 1) { + printsys(p); + len = 0; + } + if (len > 0) + memmove(line, p, len + 1); + } + if (len > 0) + printsys(line); +} + +static int opensys(const char *file) +{ + int fd; + + fd = open(file, O_RDONLY | O_NONBLOCK | O_CLOEXEC, 0); + if (fd < 0) + return 1; + + if (socket_register(fd, NULL, kernel_cb, NULL) < 0) { + close(fd); + return 1; + } + + return 0; +} +#endif + static void unix_cb(int sd, void *arg) { ssize_t msglen; @@ -995,7 +1059,8 @@ void printsys(char *msg) lp = line; for (p = msg; *p != '\0';) { memset(&buffer, 0, sizeof(buffer)); - buffer.app_name = "vmunix"; + buffer.app_name = "kernel"; + buffer.hostname = LocalHostName; buffer.pri = DEFSPRI; buffer.msg = line; diff --git a/src/syslogd.h b/src/syslogd.h index 13b3f62..a99ccf0 100644 --- a/src/syslogd.h +++ b/src/syslogd.h @@ -96,6 +96,10 @@ #define _PATH_LOG "/dev/log" #endif +#ifndef _PATH_KLOG +#define _PATH_KLOG "/proc/kmsg" +#endif + #ifdef UT_NAMESIZE #define UNAMESZ UT_NAMESIZE /* length of a login name */ #else