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.
22 #include <signal.h> /* for our signal() handlers */
23 #include <string.h> /* strncpy() */
24 #include <errno.h> /* errno and friends */
27 #include <sys/syslog.h>
32 static void klogd_signal(int sig)
36 /* logMessage(0, "Kernel log daemon exiting."); */
37 syslog(LOG_NOTICE, "Kernel log daemon exiting.");
41 static void doKlogd(const int console_log_level) ATTRIBUTE_NORETURN;
42 static void doKlogd(const int console_log_level)
44 int priority = LOG_INFO;
45 char log_buffer[4096];
49 openlog("kernel", 0, LOG_KERN);
51 /* Set up sig handlers */
52 signal(SIGINT, klogd_signal);
53 signal(SIGKILL, klogd_signal);
54 signal(SIGTERM, klogd_signal);
55 signal(SIGHUP, SIG_IGN);
57 /* "Open the log. Currently a NOP." */
60 /* Set level of kernel console messaging.. */
61 if (console_log_level != -1)
62 klogctl(8, NULL, console_log_level);
64 syslog(LOG_NOTICE, "klogd started: %s", BB_BANNER);
67 /* Use kernel syscalls */
68 memset(log_buffer, '\0', sizeof(log_buffer));
69 n = klogctl(2, log_buffer, sizeof(log_buffer));
73 syslog(LOG_ERR, "klogd: Error return from sys_sycall: %d - %m.\n", errno);
77 /* klogctl buffer parsing modelled after code in dmesg.c */
78 start = &log_buffer[0];
80 for (i = 0; i < n; i++) {
81 if (lastc == '\0' && log_buffer[i] == '<') {
84 while (isdigit(log_buffer[i])) {
85 priority = priority * 10 + (log_buffer[i] - '0');
88 if (log_buffer[i] == '>')
90 start = &log_buffer[i];
92 if (log_buffer[i] == '\n') {
93 log_buffer[i] = '\0'; /* zero terminate this message */
94 syslog(priority, "%s", start);
95 start = &log_buffer[i + 1];
98 lastc = log_buffer[i];
104 #define OPT_FOREGROUND 2
106 int klogd_main(int argc, char **argv)
110 int console_log_level = -1;
112 /* do normal option parsing */
113 opt = bb_getopt_ulflags (argc, argv, "c:n", &c_arg);
115 if (opt & OPT_LEVEL) {
116 /* Valid levels are between 1 and 8 */
117 console_log_level = bb_xgetlarg(c_arg, 10, 1, 8);
120 if (!(opt & OPT_FOREGROUND)) {
121 #if defined(__uClinux__)
122 vfork_daemon_rexec(0, 1, argc, argv, "-n");
123 #else /* __uClinux__ */
125 #endif /* __uClinux__ */
127 doKlogd(console_log_level);
134 c-file-style: "linux"