1 /* vi: set sw=4 ts=4: */
3 * Mini klogd implementation for busybox
5 * Copyright (C) 2001 by Gennady Feldman <gfeldman@cachier.com>.
6 * Changes: Made this a standalone busybox module which uses standalone
7 * syslog() client interface.
9 * Copyright (C) 1999,2000,2001 by Lineo, inc.
10 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
12 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
14 * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@mail.com>
16 * Maintainer: Gennady Feldman <gena01@cachier.com> as of Mar 12, 2001
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include <signal.h> /* for our signal() handlers */
37 #include <string.h> /* strncpy() */
38 #include <errno.h> /* errno and friends */
41 #include <sys/syslog.h>
43 #if __GNU_LIBRARY__ < 5
45 # define klogctl syslog
48 # include <sys/klog.h>
53 static void klogd_signal(int sig)
57 //logMessage(0, "Kernel log daemon exiting.");
58 syslog_msg(LOG_DAEMON, 0, "Kernel log daemon exiting.");
62 static void doKlogd (void) __attribute__ ((noreturn));
63 static void doKlogd (void)
65 int priority = LOG_INFO;
66 char log_buffer[4096];
70 /* Set up sig handlers */
71 signal(SIGINT, klogd_signal);
72 signal(SIGKILL, klogd_signal);
73 signal(SIGTERM, klogd_signal);
74 signal(SIGHUP, SIG_IGN);
76 /* "Open the log. Currently a NOP." */
79 syslog_msg(LOG_DAEMON, 0, "klogd started: " BB_BANNER);
82 /* Use kernel syscalls */
83 memset(log_buffer, '\0', sizeof(log_buffer));
84 n = klogctl(2, log_buffer, sizeof(log_buffer));
90 snprintf(message, 79, "klogd: Error return from sys_sycall: %d - %s.\n",
91 errno, strerror(errno));
92 syslog_msg(LOG_DAEMON, LOG_SYSLOG | LOG_ERR, message);
96 /* klogctl buffer parsing modelled after code in dmesg.c */
100 if (lastc == '\0' && log_buffer[i] == '<') {
103 while (isdigit(log_buffer[i])) {
104 priority = priority*10+(log_buffer[i]-'0');
107 if (log_buffer[i] == '>') i++;
108 start = &log_buffer[i];
110 if (log_buffer[i] == '\n') {
111 log_buffer[i] = '\0'; /* zero terminate this message */
112 syslog_msg(LOG_DAEMON, LOG_KERN | priority, start);
113 start = &log_buffer[i+1];
116 lastc = log_buffer[i];
121 extern int klogd_main(int argc, char **argv)
123 /* no options, no getopt */
127 /* do normal option parsing */
128 while ((opt = getopt(argc, argv, "n")) > 0) {
138 if (doFork == TRUE) {
139 if (daemon(0, 1) < 0)
140 perror_msg_and_die("daemon");
149 c-file-style: "linux"