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 i = i; /* silence gcc */
43 /* do normal option parsing */
44 getopt32(argc, argv, "c:n", &start);
46 if (option_mask32 & OPT_LEVEL) {
47 /* Valid levels are between 1 and 8 */
48 i = xatoul_range(start, 1, 8);
51 if (!(option_mask32 & OPT_FOREGROUND)) {
53 vfork_daemon_rexec(0, 1, argc, argv, "-n");
59 openlog("kernel", 0, LOG_KERN);
61 /* Set up sig handlers */
62 signal(SIGINT, klogd_signal);
63 signal(SIGKILL, klogd_signal);
64 signal(SIGTERM, klogd_signal);
65 signal(SIGHUP, SIG_IGN);
67 /* "Open the log. Currently a NOP." */
70 /* Set level of kernel console messaging.. */
71 if (option_mask32 & OPT_LEVEL)
74 syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
81 /* Use kernel syscalls */
82 memset(log_buffer, '\0', KLOGD_LOGBUF_SIZE);
83 /* It will be null-terminted */
84 n = klogctl(2, log_buffer, KLOGD_LOGBUF_SIZE - 1);
88 syslog(LOG_ERR, "klogd: error from klogctl(2): %d - %m",
93 /* klogctl buffer parsing modelled after code in dmesg.c */
94 start = &log_buffer[0];
97 for (i = 0; i < n; i++) {
98 if (lastc == '\0' && log_buffer[i] == '<') {
100 // kernel never ganerates multi-digit prios
102 //while (log_buffer[i] >= '0' && log_buffer[i] <= '9') {
103 // priority = priority * 10 + (log_buffer[i] - '0');
106 if (isdigit(log_buffer[i])) {
107 priority = (log_buffer[i] - '0');
110 if (log_buffer[i] == '>')
112 start = &log_buffer[i];
114 if (log_buffer[i] == '\n') {
115 log_buffer[i] = '\0'; /* zero terminate this message */
116 syslog(priority, "%s", start);
117 start = &log_buffer[i + 1];
120 lastc = log_buffer[i];
123 if (ENABLE_FEATURE_CLEAN_UP)
124 RELEASE_CONFIG_BUFFER(log_buffer);