Fix #8: kernel messages duplicated to console

When building the sysklogd project --without-klogd we must disable the
kernel logging to console on Linux.  This fix depends on how the sysctl
setting `kernel.printk` is configured.  The patch only calls the kernel
to set console_loglevel to minimum_console_loglevel.

See the kernel docs for details:

  https://www.kernel.org/doc/Documentation/sysctl/kernel.txt

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson 2019-12-07 09:14:10 +01:00
parent a7fe2ef700
commit 8aa2060312
2 changed files with 28 additions and 0 deletions

View File

@ -385,6 +385,8 @@ int main(int argc, char *argv[])
/* Attempt to open kernel log pipe */
if (opensys(_PATH_KLOG))
warn("Kernel logging disabled, failed opening %s", _PATH_KLOG);
else
kern_console_off();
#endif
if (!Foreground) {
@ -2016,6 +2018,10 @@ void die(int signo)
free(pe);
}
#ifndef KLOGD
kern_console_on();
#endif
exit(0);
}

View File

@ -32,7 +32,12 @@
#ifndef SYSKLOGD_SYSLOGD_H_
#define SYSKLOGD_SYSLOGD_H_
#include "config.h"
#include <netdb.h> /* struct addrinfo */
#ifdef __linux__
#include <sys/klog.h> /* When building w/o klogd */
#endif
#include <sys/param.h> /* MAXHOSTNAMELEN */
#include <sys/socket.h>
#include <sys/un.h> /* struct sockaddr_un */
@ -141,6 +146,23 @@
(((d)->s6_addr32[2] ^ (a)->s6_addr32[2]) & (m)->s6_addr32[2]) == 0 && \
(((d)->s6_addr32[3] ^ (a)->s6_addr32[3]) & (m)->s6_addr32[3]) == 0 )
/*
* When building without klogd on Linux systems we use these klogctl(2)
* commands to control kernel log messages to console.
*/
#define SYSLOG_ACTION_CLOSE 0
#define SYSLOG_ACTION_OPEN 1
#define SYSLOG_ACTION_CONSOLE_OFF 6
#define SYSLOG_ACTION_CONSOLE_ON 7
#ifdef __linux__
#define kern_console_off() klogctl(SYSLOG_ACTION_CONSOLE_OFF, NULL, 0)
#define kern_console_on() klogctl(SYSLOG_ACTION_CONSOLE_ON, NULL, 0)
#else
#define kern_console_off() do { } while (0)
#define kern_console_on() do { } while (0)
#endif
/*
* Flags to logmsg().
*/