Add in sample inittab file
[oweals/busybox.git] / syslogd.c
1 /*
2  * Mini syslogd implementation for busybox
3  *
4  * Copyright (C) 1999 by Lineo, inc.
5  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  */
22
23 #include "internal.h"
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <sys/socket.h>
27 #include <sys/un.h>
28 #include <unistd.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <ctype.h>
35 #include <netdb.h>
36 #include <sys/klog.h>
37 #include <errno.h>
38 #include <paths.h>
39
40 #define ksyslog klogctl
41 extern int ksyslog(int type, char *buf, int len);
42
43
44 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
45 #define SYSLOG_NAMES
46 #include <sys/syslog.h>
47
48 /* Path for the file where all log messages are written */
49 #define __LOG_FILE              "/var/log/messages"
50
51
52 static char* logFilePath = __LOG_FILE;
53 /* interval between marks in seconds */
54 static int MarkInterval = 20*60;
55 /* localhost's name */
56 static char LocalHostName[32];
57
58 static const char syslogd_usage[] =
59     "syslogd [OPTION]...\n\n"
60     "Linux system and kernel (provides klogd) logging utility.\n"
61     "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n"
62     "Options:\n"
63     "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n"
64     "\t-n\tDo not fork into the background (for when run by init)\n"
65 #ifdef BB_KLOGD
66     "\t-K\tDo not start up the klogd process (by default syslogd spawns klogd).\n"
67 #endif
68     "\t-O\tSpecify an alternate log file.  default=/var/log/messages\n";
69
70
71 /* try to open up the specified device */
72 static int device_open(char *device, int mode)
73 {
74     int m, f, fd = -1;
75
76     m = mode | O_NONBLOCK;
77
78     /* Retry up to 5 times */
79     for (f = 0; f < 5; f++)
80         if ((fd = open(device, m)) >= 0)
81             break;
82     if (fd < 0)
83         return fd;
84     /* Reset original flags. */
85     if (m != mode)
86         fcntl(fd, F_SETFL, mode);
87     return fd;
88 }
89
90 /* print a message to the log file */
91 static void message(char *fmt, ...)
92 {
93     int fd;
94     va_list arguments;
95
96     if ((fd = device_open(logFilePath, O_WRONLY|O_CREAT|O_NOCTTY|O_APPEND|O_NONBLOCK)) >= 0) {
97         va_start(arguments, fmt);
98         vdprintf(fd, fmt, arguments);
99         va_end(arguments);
100         close(fd);
101     } else {
102         /* Always send console messages to /dev/console so people will see them. */
103         if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NONBLOCK)) >= 0) {
104             va_start(arguments, fmt);
105             vdprintf(fd, fmt, arguments);
106             va_end(arguments);
107             close(fd);
108         } else {
109             fprintf(stderr, "Bummer, can't print: ");
110             va_start(arguments, fmt);
111             vfprintf(stderr, fmt, arguments);
112             fflush(stderr);
113             va_end(arguments);
114         }
115     }
116 }
117
118 static void logMessage( int pri, char* msg) 
119 {
120     time_t  now;
121     char *timestamp;
122     static char res[20];
123     CODE *c_pri, *c_fac;
124
125     for (c_fac=facilitynames; c_fac->c_name && !(c_fac->c_val==LOG_FAC(pri)<<3); c_fac++);
126     for (c_pri=prioritynames; c_pri->c_name && !(c_pri->c_val==LOG_PRI(pri)); c_pri++);
127     if (*c_fac->c_name=='\0' || *c_pri->c_name=='\0')
128         snprintf (res, sizeof(res), "<%d>", pri);
129     else
130         snprintf (res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
131
132     if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' || 
133             msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') 
134     {
135         time(&now);
136         timestamp = ctime(&now) + 4;
137         timestamp[15] = '\0';
138     } else {
139         timestamp = msg;
140         timestamp[15] = '\0';
141         msg += 16;
142     }
143
144     /* todo: supress duplicates */
145
146     /* now spew out the message to wherever it is supposed to go */
147     message( "%s %s %s %s\n", timestamp, LocalHostName, res, msg);
148 }
149
150 static void quit_signal(int sig)
151 {
152     logMessage(LOG_SYSLOG|LOG_INFO, "System log daemon exiting.");
153     unlink( _PATH_LOG);
154     exit( TRUE);
155 }
156
157 static void restart_signal(int sig)
158 {
159     /* pretend to restart */
160     logMessage(LOG_SYSLOG|LOG_INFO, "syslogd restarting");
161 }
162
163 static void domark(int sig)
164 {
165     if (MarkInterval > 0) {
166         logMessage(LOG_SYSLOG|LOG_INFO, "-- MARK --");
167         alarm(MarkInterval);
168     }
169 }
170
171 static void doSyslogd(void)
172 {
173     struct sockaddr_un sunx;
174     int fd, conn;
175     size_t addrLength;
176     char buf[1024];
177     char *q, *p = buf;
178     int readSize;
179
180     /* Remove any preexisting socket/file */
181     unlink(_PATH_LOG);
182
183     /* Set up sig handlers */
184     signal(SIGINT,  quit_signal);
185     signal(SIGTERM, quit_signal);
186     signal(SIGQUIT, quit_signal);
187     signal(SIGHUP,  restart_signal);
188     signal(SIGALRM, domark);
189     alarm(MarkInterval);
190
191
192     unlink( _PATH_LOG);
193     memset(&sunx, 0, sizeof(sunx));
194     sunx.sun_family = AF_UNIX;  /* Unix domain socket */
195     strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
196     if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) {
197         perror("Couldn't obtain descriptor for socket " _PATH_LOG);
198         exit( FALSE);
199     }
200
201     addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
202     if ( (bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
203             (fchmod(fd, 0666) < 0) || (listen(fd, 5)) ) 
204     {
205         perror("Could not connect to socket " _PATH_LOG);
206         exit( FALSE);
207     }
208     
209     
210     logMessage(LOG_SYSLOG|LOG_INFO, "syslogd started: "
211             "BusyBox v" BB_VER " (" BB_BT ")");
212
213
214     while ((conn = accept(fd, (struct sockaddr *) &sunx, 
215                           &addrLength)) >= 0) 
216     {
217         while ((readSize=read(conn, buf, sizeof(buf))) > 0)
218         {
219             char line[1025];
220             unsigned char c;
221             int pri = (LOG_USER|LOG_NOTICE);
222
223             memset (line, 0, sizeof(line));
224             p = buf;
225             q = line;
226             while ( p && (c = *p) && q < &line[sizeof(line) - 1]) {
227                 if (c == '<') {
228                     /* Parse the magic priority number */
229                     pri = 0;
230                     while (isdigit(*(++p))) {
231                         pri = 10 * pri + (*p - '0');
232                     }
233                     if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
234                         pri = (LOG_USER|LOG_NOTICE);
235                 } else if (c == '\n') {
236                     *q++ = ' ';
237                 } else if (iscntrl(c)&&(c<0177)) {
238                     *q++ = '^';
239                     *q++ = c ^ 0100;
240                 } else {
241                     *q++ = c;
242                 }
243                 p++;
244             }
245             *q = '\0';
246
247             /* Now log it */
248             logMessage( pri, line);
249         }
250         close(conn);
251     }
252
253     close(fd);
254 }
255
256 #ifdef BB_KLOGD
257
258 static void klogd_signal(int sig)
259 {
260     ksyslog(7, NULL, 0);
261     ksyslog(0, 0, 0);
262     logMessage(LOG_SYSLOG|LOG_INFO, "Kernel log daemon exiting.");
263     exit( TRUE);
264 }
265
266 static void doKlogd(void)
267 {
268     int priority=LOG_INFO;
269     char log_buffer[4096];
270     char *logp;
271
272     /* Set up sig handlers */
273     signal(SIGINT,  klogd_signal);
274     signal(SIGKILL, klogd_signal);
275     signal(SIGTERM, klogd_signal);
276     signal(SIGHUP,  klogd_signal);
277     logMessage(LOG_SYSLOG|LOG_INFO, "klogd started: "
278             "BusyBox v" BB_VER " (" BB_BT ")");
279
280     ksyslog(1, NULL, 0);
281
282     while (1) {
283         /* Use kernel syscalls */
284         memset(log_buffer, '\0', sizeof(log_buffer));
285         if ( ksyslog(2, log_buffer, sizeof(log_buffer)) < 0 ) {
286             char message[80];
287             if ( errno == EINTR )
288                 continue;
289             snprintf(message, 79, "klogd: Error return from sys_sycall: " \
290                     "%d - %s.\n", errno, strerror(errno));
291             logMessage(LOG_SYSLOG|LOG_ERR, message);
292             exit(1);
293         }
294         logp=log_buffer;
295         if ( *log_buffer == '<' )
296         {
297             switch ( *(log_buffer+1) )
298             {
299                 case '0':
300                     priority = LOG_EMERG;
301                     break;
302                 case '1':
303                     priority = LOG_ALERT;
304                     break;
305                 case '2':
306                     priority = LOG_CRIT;
307                     break;
308                 case '3':
309                     priority = LOG_ERR;
310                     break;
311                 case '4':
312                     priority = LOG_WARNING;
313                     break;
314                 case '5':
315                     priority = LOG_NOTICE;
316                     break;
317                 case '6':
318                     priority = LOG_INFO;
319                     break;
320                 case '7':
321                 default:
322                     priority = LOG_DEBUG;
323             }
324             logp+=3;
325         }
326         logMessage(LOG_KERN|priority, logp);
327     }
328
329 }
330
331 #endif
332
333 extern int syslogd_main(int argc, char **argv)
334 {
335     int pid, klogd_pid;
336     int doFork = TRUE;
337 #ifdef BB_KLOGD
338     int startKlogd = TRUE;
339 #endif
340     char *p;
341     char **argv1=argv;
342     
343     while (--argc > 0 && **(++argv1) == '-') {
344         while (*(++(*argv1))) {
345             switch (**argv1) {
346             case 'm':
347                 if (--argc == 0) {
348                     usage(syslogd_usage);
349                 }
350                 MarkInterval = atoi(*(++argv1))*60;
351                 break;
352             case 'n':
353                 doFork = FALSE;
354                 break;
355 #ifdef BB_KLOGD
356             case 'K':
357                 startKlogd = FALSE;
358                 break;
359 #endif
360             case 'O':
361                 if (--argc == 0) {
362                     usage(syslogd_usage);
363                 }
364                 logFilePath = *(++argv1);
365                 break;
366             default:
367                 usage(syslogd_usage);
368             }
369         }
370     }
371     
372     /* Store away localhost's name before the fork */
373     gethostname(LocalHostName, sizeof(LocalHostName));
374     if ( (p = strchr(LocalHostName, '.')) ) {
375         *p++ = '\0';
376     }
377
378     if (doFork == TRUE) {
379         pid = fork();
380         if ( pid < 0 )
381             exit( pid);
382         else if ( pid == 0 ) {
383             strncpy(argv[0], "syslogd",strlen(argv[0]));
384             doSyslogd();
385         }
386     } else {
387         doSyslogd();
388     }
389
390 #ifdef BB_KLOGD
391     /* Start up the klogd process */
392     if (startKlogd == TRUE) {
393         klogd_pid = fork();
394         if (klogd_pid == 0 ) {
395                 strncpy(argv[0], "klogd", strlen(argv[0]));
396                 doKlogd();
397         }
398     }
399 #endif
400
401     exit( TRUE);
402 }
403
404