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.
24 static void klogd_signal(int sig)
26 /* FYI: cmd 7 is equivalent to setting console_loglevel to 7
27 * via klogctl(8, NULL, 7). */
28 klogctl(7, NULL, 0); /* "7 -- Enable printk's to console" */
29 klogctl(0, NULL, 0); /* "0 -- Close the log. Currently a NOP" */
30 syslog(LOG_NOTICE, "klogd: exiting");
31 kill_myself_with_sig(sig);
34 #define log_buffer bb_common_bufsiz1
36 KLOGD_LOGBUF_SIZE = sizeof(log_buffer),
38 OPT_FOREGROUND = (1 << 1),
41 int klogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42 int klogd_main(int argc UNUSED_PARAM, char **argv)
49 opt = getopt32(argv, "c:n", &start);
50 if (opt & OPT_LEVEL) {
51 /* Valid levels are between 1 and 8 */
52 i = xatou_range(start, 1, 8);
54 if (!(opt & OPT_FOREGROUND)) {
55 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
58 openlog("kernel", 0, LOG_KERN);
64 signal(SIGHUP, SIG_IGN);
66 /* "Open the log. Currently a NOP" */
69 /* "printk() prints a message on the console only if it has a loglevel
70 * less than console_loglevel". Here we set console_loglevel = i. */
74 syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
76 /* Initially null terminate the buffer in case of a very long line */
77 log_buffer[KLOGD_LOGBUF_SIZE - 1] = '\0';
83 /* "2 -- Read from the log." */
84 n = klogctl(2, log_buffer + used, KLOGD_LOGBUF_SIZE-1 - used);
88 syslog(LOG_ERR, "klogd: error %d in klogctl(2): %m",
93 /* klogctl buffer parsing modelled after code in dmesg.c */
94 start = &log_buffer[0];
96 /* Process each newline-terminated line in the buffer */
98 char *newline = strchr(start, '\n');
101 /* This line is incomplete... */
102 if (start != log_buffer) {
103 /* move it to the front of the buffer */
104 strcpy(log_buffer, start);
105 /* don't log it yet */
106 used = strlen(log_buffer);
109 /* ...but buffer is full, so log it anyway */
115 /* Extract the priority */
120 /* kernel never generates multi-digit prios */
121 priority = (*start - '0');
129 syslog(priority, "%s", start);