Skip to content

Commit

Permalink
Fix #72: loss of raw kernel log messages to console
Browse files Browse the repository at this point in the history
This patch adds a command line flag `-l` to keep kernel logs to console.
A feature requested by embedded Linux users which often navigate issues
by console output.

With properly configured kernel logging, e.g., `quiet`, only error and
above in severity is logged by the kernel directly to the console.  So
for most users this would be a useful behavior.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
  • Loading branch information
troglobit committed Apr 24, 2024
1 parent bfd3e54 commit 1727f7a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
18 changes: 17 additions & 1 deletion man/syslogd.8
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
.Nd log systems messages
.Sh SYNOPSIS
.Nm
.Op Fl ?468AcdFHKknsTtv
.Op Fl ?468AcdFHKklnsTtv
.Op Fl a Ar addr[/len][:port]
.Op Fl a Ar name[:port]
.Op Fl b Ar addr[:port]
Expand Down Expand Up @@ -292,6 +292,22 @@ Usually the
.Dq kern
facility is reserved for messages read directly from
.Pa /dev/kmsg .
.It Fl l
Keep kernel console logging. By default
.Nm
call
.Xr klogctl 2
to disable the kernel's logging to console after having opened
.Pa /dev/kmsg .
With this option the kernel's log level can be adjusted using
.Xr sysctl 8 ,
or the kernel command line, to suit your logging needs to the console.
.Pp
Please note, this does not affect logging of kernel messages, see
.Fl K ,
only what the kernel logs to
.Pa /dev/console .
Also, this is only applicable to Linux.
.It Fl m Ar interval
Select the number of minutes between
.Dq mark
Expand Down
10 changes: 9 additions & 1 deletion src/syslogd.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ static int RemoteHostname; /* Log remote hostname from the message */
static int KernLog = 1; /* Track kernel logs by default */
static int KeepKernFac; /* Keep remotely logged kernel facility */
static int KeepKernTime; /* Keep kernel timestamp, evern after initial read */
static int KeepKernConsole; /* Keep kernel logging to console */

static off_t RotateSz = 0; /* Max file size (bytes) before rotating, disabled by default */
static int RotateCnt = 5; /* Max number (count) of log files to keep, set with -c <NUM> */
Expand Down Expand Up @@ -363,6 +364,9 @@ int usage(int code)
" -H Use hostname from message instead of address for remote messages\n"
" -K Disable kernel logging, useful in container use-cases\n"
" -k Allow logging with facility 'kernel', otherwise remapped to 'user'\n"
#ifdef __linux__
" -l Keep kernel logging to console, use sysctl to adjust kernel.printk\n"
#endif
" -m MINS Interval between MARK messages, 0 to disable, default: 20 min\n"
" -n Disable DNS query for every request\n"
" -P FILE File to store the process ID, default: %s\n"
Expand Down Expand Up @@ -397,7 +401,7 @@ int main(int argc, char *argv[])
char *ptr;
int ch;

while ((ch = getopt(argc, argv, "468Aa:b:C:cdHFf:Kkm:nP:p:r:sTtv?")) != EOF) {
while ((ch = getopt(argc, argv, "468Aa:b:C:cdHFf:Kklm:nP:p:r:sTtv?")) != EOF) {
switch ((char)ch) {
case '4':
family = PF_INET;
Expand Down Expand Up @@ -464,6 +468,10 @@ int main(int argc, char *argv[])
KeepKernFac = 1;
break;

case 'l':
KeepKernConsole = 1;
break;

case 'm': /* mark interval */
MarkInterval = atoi(optarg) * 60;
break;
Expand Down
4 changes: 2 additions & 2 deletions src/syslogd.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@
#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)
#define kern_console_off() if (!KeepKernConsole) klogctl(SYSLOG_ACTION_CONSOLE_OFF, NULL, 0)
#define kern_console_on() if (!KeepKernConsole) klogctl(SYSLOG_ACTION_CONSOLE_ON, NULL, 0)
#else
#define kern_console_off() do { } while (0)
#define kern_console_on() do { } while (0)
Expand Down

0 comments on commit 1727f7a

Please sign in to comment.