7f7d6a142c3236407bfd1ef9f0c1ab3f6af09bb0
[oweals/busybox.git] / klogd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini klogd implementation for busybox
4  *
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.
8  *
9  * Copyright (C) 1999,2000,2001 by Lineo, inc.
10  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
11  *
12  * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
13  *
14  * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@mail.com>
15  *
16  * Maintainer: Gennady Feldman <gena01@cachier.com> as of Mar 12, 2001
17  *
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.
22  *
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.
27  *
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
31  *
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <signal.h> /* for our signal() handlers */
37 #include <string.h> /* strncpy() */
38 #include <errno.h>  /* errno and friends */
39 #include <unistd.h>
40 #include <ctype.h>
41 #include <sys/syslog.h>
42
43 #if ! defined __GLIBC__ && ! defined __UCLIBC__
44 #include <sys/syscall.h>
45 #include <linux/unistd.h>
46 typedef unsigned int socklen_t;
47
48 #ifndef __alpha__
49 # define __NR_klogctl __NR_syslog
50 static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
51 #else                                                   /* __alpha__ */
52 #define klogctl syslog
53 #endif
54
55 #else
56 # include <sys/klog.h>
57 #endif
58 #include "busybox.h"
59
60 static void klogd_signal(int sig)
61 {
62         klogctl(7, NULL, 0);
63         klogctl(0, 0, 0);
64         //logMessage(0, "Kernel log daemon exiting.");
65         syslog_msg(LOG_DAEMON, 0, "Kernel log daemon exiting.");
66         exit(TRUE);
67 }
68
69 static void doKlogd (void) __attribute__ ((noreturn));
70 static void doKlogd (void)
71 {
72         int priority = LOG_INFO;
73         char log_buffer[4096];
74         int i, n, lastc;
75         char *start;
76
77         /* Set up sig handlers */
78         signal(SIGINT, klogd_signal);
79         signal(SIGKILL, klogd_signal);
80         signal(SIGTERM, klogd_signal);
81         signal(SIGHUP, SIG_IGN);
82
83         /* "Open the log. Currently a NOP." */
84         klogctl(1, NULL, 0);
85
86         syslog_msg(LOG_DAEMON, 0, "klogd started: BusyBox v" BB_VER " (" BB_BT ")");
87
88         while (1) {
89                 /* Use kernel syscalls */
90                 memset(log_buffer, '\0', sizeof(log_buffer));
91                 n = klogctl(2, log_buffer, sizeof(log_buffer));
92                 if (n < 0) {
93                         char message[80];
94
95                         if (errno == EINTR)
96                                 continue;
97                         snprintf(message, 79, "klogd: Error return from sys_sycall: %d - %s.\n", 
98                                                                                                 errno, strerror(errno));
99                         syslog_msg(LOG_DAEMON, LOG_SYSLOG | LOG_ERR, message);
100                         exit(1);
101                 }
102
103                 /* klogctl buffer parsing modelled after code in dmesg.c */
104                 start=&log_buffer[0];
105                 lastc='\0';
106                 for (i=0; i<n; i++) {
107                         if (lastc == '\0' && log_buffer[i] == '<') {
108                                 priority = 0;
109                                 i++;
110                                 while (isdigit(log_buffer[i])) {
111                                         priority = priority*10+(log_buffer[i]-'0');
112                                         i++;
113                                 }
114                                 if (log_buffer[i] == '>') i++;
115                                 start = &log_buffer[i];
116                         }
117                         if (log_buffer[i] == '\n') {
118                                 log_buffer[i] = '\0';  /* zero terminate this message */
119                                 syslog_msg(LOG_DAEMON, LOG_KERN | priority, start);
120                                 start = &log_buffer[i+1];
121                                 priority = LOG_INFO;
122                         }
123                         lastc = log_buffer[i];
124                 }
125         }
126 }
127
128 static void daemon_init (char **argv, char *dz, void fn (void))
129 {
130         setsid(); /* start a new session? */
131         strncpy(argv[0], dz, strlen(argv[0]));
132         fn();
133         exit(0);
134 }
135
136 extern int klogd_main(int argc, char **argv)
137 {
138         /* no options, no getopt */
139         int opt, pid;
140         int doFork = TRUE;
141
142         /* do normal option parsing */
143         while ((opt = getopt(argc, argv, "n")) > 0) {
144                 switch (opt) {
145                         case 'n':
146                                 doFork = FALSE;
147                                 break;
148                         default:
149                                 show_usage();
150                 }
151         }
152
153         if (doFork == TRUE) {
154                 pid = fork();
155                 if (pid < 0)
156                         exit(pid);
157                 else if (pid == 0) {
158                         daemon_init (argv, "klogd", doKlogd);
159                 }
160         } else {
161                 doKlogd();
162         }
163         
164         return EXIT_SUCCESS;
165 }
166
167 /*
168 Local Variables
169 c-file-style: "linux"
170 c-basic-offset: 4
171 tab-width: 4
172 End:
173 */