1 /* vi: set sw=4 ts=4: */
3 * Mini klogd implementation for busybox
5 * Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>.
6 * Changes: Made this a standalone busybox module which uses standalone
7 * syslog() client interface.
9 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
11 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
13 * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
15 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
17 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
21 #include <sys/syslog.h>
24 static void klogd_signal(int sig ATTRIBUTE_UNUSED)
28 syslog(LOG_NOTICE, "Kernel log daemon exiting");
33 #define OPT_FOREGROUND 2
35 #define KLOGD_LOGBUF_SIZE 4096
37 int klogd_main(int argc, char **argv)
39 RESERVE_CONFIG_BUFFER(log_buffer, KLOGD_LOGBUF_SIZE);
40 int console_log_level = console_log_level; /* for gcc */
41 int priority = LOG_INFO;
45 /* do normal option parsing */
46 n = getopt32(argc, argv, "c:n", &start);
49 /* Valid levels are between 1 and 8 */
50 console_log_level = xatoul_range(start, 1, 8);
53 if (!(n & OPT_FOREGROUND)) {
55 vfork_daemon_rexec(0, 1, argc, argv, "-n");
61 openlog("kernel", 0, LOG_KERN);
63 /* Set up sig handlers */
64 signal(SIGINT, klogd_signal);
65 signal(SIGKILL, klogd_signal);
66 signal(SIGTERM, klogd_signal);
67 signal(SIGHUP, SIG_IGN);
69 /* "Open the log. Currently a NOP." */
72 /* Set level of kernel console messaging.. */
74 klogctl(8, NULL, console_log_level);
76 syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
79 /* Use kernel syscalls */
80 memset(log_buffer, '\0', KLOGD_LOGBUF_SIZE);
81 /* It will be null-terminted */
82 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
86 syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
91 /* klogctl buffer parsing modelled after code in dmesg.c */
92 start = &log_buffer[0];
94 for (i = 0; i < n; i++) {
95 if (lastc == '\0' && log_buffer[i] == '<') {
97 // kernel never ganerates multi-digit prios
99 //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
100 // priority = priority * 10 + (log_buffer[i] - '0');
103 if (isdigit(log_buffer[i])) {
104 priority = (log_buffer[i] - '0');
107 if (log_buffer[i] == '>')
109 start = &log_buffer[i];
111 if (log_buffer[i] == '\n') {
112 log_buffer[i] = '\0'; /* zero terminate this message */
113 syslog(priority, "%s", start);
114 start = &log_buffer[i + 1];
117 lastc = log_buffer[i];
120 if (ENABLE_FEATURE_CLEAN_UP)
121 RELEASE_CONFIG_BUFFER(log_buffer);