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 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include <signal.h> /* for our signal() handlers */
36 #include <string.h> /* strncpy() */
37 #include <errno.h> /* errno and friends */
40 #include <sys/syslog.h>
45 static void klogd_signal(int sig)
49 /* logMessage(0, "Kernel log daemon exiting."); */
50 syslog(LOG_NOTICE, "Kernel log daemon exiting.");
54 static void doKlogd(const int console_log_level) __attribute__ ((noreturn));
55 static void doKlogd(const int console_log_level)
57 int priority = LOG_INFO;
58 char log_buffer[4096];
62 openlog("kernel", 0, LOG_KERN);
64 /* Set up sig handlers */
65 signal(SIGINT, klogd_signal);
66 signal(SIGKILL, klogd_signal);
67 signal(SIGTERM, klogd_signal);
68 signal(SIGHUP, SIG_IGN);
70 /* "Open the log. Currently a NOP." */
73 /* Set level of kernel console messaging.. */
74 if (console_log_level != -1)
75 klogctl(8, NULL, console_log_level);
77 syslog(LOG_NOTICE, "klogd started: " BB_BANNER);
80 /* Use kernel syscalls */
81 memset(log_buffer, '\0', sizeof(log_buffer));
82 n = klogctl(2, log_buffer, sizeof(log_buffer));
86 syslog(LOG_ERR, "klogd: Error return from sys_sycall: %d - %m.\n", errno);
90 /* klogctl buffer parsing modelled after code in dmesg.c */
91 start = &log_buffer[0];
93 for (i = 0; i < n; i++) {
94 if (lastc == '\0' && log_buffer[i] == '<') {
97 while (isdigit(log_buffer[i])) {
98 priority = priority * 10 + (log_buffer[i] - '0');
101 if (log_buffer[i] == '>')
103 start = &log_buffer[i];
105 if (log_buffer[i] == '\n') {
106 log_buffer[i] = '\0'; /* zero terminate this message */
107 syslog(priority, "%s", start);
108 start = &log_buffer[i + 1];
111 lastc = log_buffer[i];
117 #define OPT_FOREGROUND 2
119 extern int klogd_main(int argc, char **argv)
123 int console_log_level = -1;
125 /* do normal option parsing */
126 opt = bb_getopt_ulflags (argc, argv, "c:n", &c_arg);
128 if (opt & OPT_LEVEL) {
129 /* Valid levels are between 1 and 8 */
130 console_log_level = bb_xgetlarg(c_arg, 10, 1, 8);
133 if (!(opt & OPT_FOREGROUND)) {
134 #if defined(__uClinux__)
135 vfork_daemon_rexec(0, 1, argc, argv, "-n");
136 #else /* __uClinux__ */
137 if (daemon(0, 1) < 0)
138 bb_perror_msg_and_die("daemon");
139 #endif /* __uClinux__ */
141 doKlogd(console_log_level);
148 c-file-style: "linux"