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)
48 opt = getopt32(argv, "c:n", &start);
49 if (opt & OPT_LEVEL) {
50 /* Valid levels are between 1 and 8 */
51 i = xatou_range(start, 1, 8);
53 if (!(opt & OPT_FOREGROUND)) {
54 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
57 openlog("kernel", 0, LOG_KERN);
63 signal(SIGHUP, SIG_IGN);
65 /* "Open the log. Currently a NOP" */
68 /* "printk() prints a message on the console only if it has a loglevel
69 * less than console_loglevel". Here we set console_loglevel = i. */
73 syslog(LOG_NOTICE, "klogd started: %s", bb_banner);
75 /* Note: this code does not detect incomplete messages
76 * (messages not ending with '\n' or just when kernel
77 * generates too many messages for us to keep up)
78 * and will split them in two separate lines */
83 /* "2 -- Read from the log." */
84 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
88 syslog(LOG_ERR, "klogd: error %d in klogctl(2): %m",
96 start = &log_buffer[i];
97 if (log_buffer[i] == '<') {
99 // kernel never ganerates multi-digit prios
101 //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
102 // priority = priority * 10 + (log_buffer[i] - '0');
105 if (isdigit(log_buffer[i])) {
106 priority = (log_buffer[i] - '0');
109 if (log_buffer[i] == '>')
111 start = &log_buffer[i];
113 while (log_buffer[i] != '\n')
115 log_buffer[i] = '\0';
116 syslog(priority, "%s", start);