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-2003 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>
42 #if __GNU_LIBRARY__ < 5
44 # define klogctl syslog
47 # include <sys/klog.h>
52 static void klogd_signal(int sig)
56 /* logMessage(0, "Kernel log daemon exiting."); */
57 syslog_msg(LOG_SYSLOG, LOG_NOTICE, "Kernel log daemon exiting.");
61 static void doKlogd(const char console_log_level) __attribute__ ((noreturn));
62 static void doKlogd(const char console_log_level)
64 int priority = LOG_INFO;
65 char log_buffer[4096];
69 /* Set up sig handlers */
70 signal(SIGINT, klogd_signal);
71 signal(SIGKILL, klogd_signal);
72 signal(SIGTERM, klogd_signal);
73 signal(SIGHUP, SIG_IGN);
75 /* "Open the log. Currently a NOP." */
78 /* Set level of kernel console messaging.. */
79 if (console_log_level)
80 klogctl(8, NULL, console_log_level);
82 syslog_msg(LOG_SYSLOG, LOG_NOTICE, "klogd started: " BB_BANNER);
85 /* Use kernel syscalls */
86 memset(log_buffer, '\0', sizeof(log_buffer));
87 n = klogctl(2, log_buffer, sizeof(log_buffer));
94 "klogd: Error return from sys_sycall: %d - %s.\n", errno,
96 syslog_msg(LOG_SYSLOG, LOG_ERR, message);
100 /* klogctl buffer parsing modelled after code in dmesg.c */
101 start = &log_buffer[0];
103 for (i = 0; i < n; i++) {
104 if (lastc == '\0' && log_buffer[i] == '<') {
107 while (isdigit(log_buffer[i])) {
108 priority = priority * 10 + (log_buffer[i] - '0');
111 if (log_buffer[i] == '>')
113 start = &log_buffer[i];
115 if (log_buffer[i] == '\n') {
116 log_buffer[i] = '\0'; /* zero terminate this message */
117 syslog_msg(LOG_KERN, priority, start);
118 start = &log_buffer[i + 1];
121 lastc = log_buffer[i];
126 extern int klogd_main(int argc, char **argv)
128 /* no options, no getopt */
131 unsigned char console_log_level = 7;
133 /* do normal option parsing */
134 while ((opt = getopt(argc, argv, "c:n")) > 0) {
137 if ((optarg == NULL) || (optarg[1] != '\0')) {
140 /* Valid levels are between 1 and 8 */
141 console_log_level = *optarg - '1';
142 if (console_log_level > 7) {
157 #if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
158 if (daemon(0, 1) < 0)
159 bb_perror_msg_and_die("daemon");
161 bb_error_msg_and_die("daemon not supported");
164 doKlogd(console_log_level);
171 c-file-style: "linux"