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 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include <signal.h> /* for our signal() handlers */
35 #include <string.h> /* strncpy() */
36 #include <errno.h> /* errno and friends */
39 #include <sys/syslog.h>
41 #if ! defined __GLIBC__ && ! defined __UCLIBC__
42 #include <sys/syscall.h>
43 #include <linux/unistd.h>
44 typedef unsigned int socklen_t;
47 # define __NR_klogctl __NR_syslog
48 static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
50 #define klogctl syslog
54 # include <sys/klog.h>
58 static void klogd_signal(int sig)
62 //logMessage(0, "Kernel log daemon exiting.");
63 syslog_msg(LOG_DAEMON, 0, "Kernel log daemon exiting.");
67 static void doKlogd (void) __attribute__ ((noreturn));
68 static void doKlogd (void)
70 int priority = LOG_INFO;
71 char log_buffer[4096];
75 /* Set up sig handlers */
76 signal(SIGINT, klogd_signal);
77 signal(SIGKILL, klogd_signal);
78 signal(SIGTERM, klogd_signal);
79 signal(SIGHUP, SIG_IGN);
81 /* "Open the log. Currently a NOP." */
84 syslog_msg(LOG_DAEMON, 0, "klogd started: BusyBox v" BB_VER " (" BB_BT ")");
87 /* Use kernel syscalls */
88 memset(log_buffer, '\0', sizeof(log_buffer));
89 n = klogctl(2, log_buffer, sizeof(log_buffer));
95 snprintf(message, 79, "klogd: Error return from sys_sycall: %d - %s.\n",
96 errno, strerror(errno));
97 syslog_msg(LOG_DAEMON, LOG_SYSLOG | LOG_ERR, message);
101 /* klogctl buffer parsing modelled after code in dmesg.c */
102 start=&log_buffer[0];
104 for (i=0; i<n; i++) {
105 if (lastc == '\0' && log_buffer[i] == '<') {
108 while (isdigit(log_buffer[i])) {
109 priority = priority*10+(log_buffer[i]-'0');
112 if (log_buffer[i] == '>') 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_DAEMON, LOG_KERN | priority, start);
118 start = &log_buffer[i+1];
121 lastc = log_buffer[i];
126 static void daemon_init (char **argv, char *dz, void fn (void))
128 setsid(); /* start a new session? */
129 strncpy(argv[0], dz, strlen(argv[0]));
134 extern int klogd_main(int argc, char **argv)
136 /* no options, no getopt */
140 /* do normal option parsing */
141 while ((opt = getopt(argc, argv, "n")) > 0) {
151 if (doFork == TRUE) {
156 daemon_init (argv, "klogd", doKlogd);
167 c-file-style: "linux"