Applied patch from Gennady Feldman to split up syslogd.c into syslogd and
[oweals/busybox.git] / sysklogd / 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  * 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.
20  *
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.
25  *
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
29  *
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <signal.h> /* for our signal() handlers */
35 #include <string.h> /* strncpy() */
36 #include <errno.h>  /* errno and friends */
37 #include <unistd.h>
38 #include <ctype.h>
39 #include <sys/syslog.h>
40
41 #if ! defined __GLIBC__ && ! defined __UCLIBC__
42 #include <sys/syscall.h>
43 #include <linux/unistd.h>
44 typedef unsigned int socklen_t;
45
46 #ifndef __alpha__
47 # define __NR_klogctl __NR_syslog
48 static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
49 #else                                                   /* __alpha__ */
50 #define klogctl syslog
51 #endif
52
53 #else
54 # include <sys/klog.h>
55 #endif
56 #include "busybox.h"
57
58 static void klogd_signal(int sig)
59 {
60         klogctl(7, NULL, 0);
61         klogctl(0, 0, 0);
62         //logMessage(0, "Kernel log daemon exiting.");
63         syslog_msg(LOG_DAEMON, 0, "Kernel log daemon exiting.");
64         exit(TRUE);
65 }
66
67 static void doKlogd (void) __attribute__ ((noreturn));
68 static void doKlogd (void)
69 {
70         int priority = LOG_INFO;
71         char log_buffer[4096];
72         int i, n, lastc;
73         char *start;
74
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);
80
81         /* "Open the log. Currently a NOP." */
82         klogctl(1, NULL, 0);
83
84         syslog_msg(LOG_DAEMON, 0, "klogd started: BusyBox v" BB_VER " (" BB_BT ")");
85
86         while (1) {
87                 /* Use kernel syscalls */
88                 memset(log_buffer, '\0', sizeof(log_buffer));
89                 n = klogctl(2, log_buffer, sizeof(log_buffer));
90                 if (n < 0) {
91                         char message[80];
92
93                         if (errno == EINTR)
94                                 continue;
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);
98                         exit(1);
99                 }
100
101                 /* klogctl buffer parsing modelled after code in dmesg.c */
102                 start=&log_buffer[0];
103                 lastc='\0';
104                 for (i=0; i<n; i++) {
105                         if (lastc == '\0' && log_buffer[i] == '<') {
106                                 priority = 0;
107                                 i++;
108                                 while (isdigit(log_buffer[i])) {
109                                         priority = priority*10+(log_buffer[i]-'0');
110                                         i++;
111                                 }
112                                 if (log_buffer[i] == '>') i++;
113                                 start = &log_buffer[i];
114                         }
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];
119                                 priority = LOG_INFO;
120                         }
121                         lastc = log_buffer[i];
122                 }
123         }
124 }
125
126 static void daemon_init (char **argv, char *dz, void fn (void))
127 {
128         setsid(); /* start a new session? */
129         strncpy(argv[0], dz, strlen(argv[0]));
130         fn();
131         exit(0);
132 }
133
134 extern int klogd_main(int argc, char **argv)
135 {
136         /* no options, no getopt */
137         int opt, pid;
138         int doFork = TRUE;
139
140         /* do normal option parsing */
141         while ((opt = getopt(argc, argv, "n")) > 0) {
142                 switch (opt) {
143                         case 'n':
144                                 doFork = FALSE;
145                                 break;
146                         default:
147                                 show_usage();
148                 }
149         }
150
151         if (doFork == TRUE) {
152                 pid = fork();
153                 if (pid < 0)
154                         exit(pid);
155                 else if (pid == 0) {
156                         daemon_init (argv, "klogd", doKlogd);
157                 }
158         } else {
159                 doKlogd();
160         }
161         
162         return EXIT_SUCCESS;
163 }
164
165 /*
166 Local Variables
167 c-file-style: "linux"
168 c-basic-offset: 4
169 tab-width: 4
170 End:
171 */