blob: 24c721f8eb4c9915a8f41025184622ecb70396c7 [file] [log] [blame]
Eric Andersen3843e961999-11-25 07:30:46 +00001/*
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 */
Eric Andersenb99df0f1999-11-24 09:04:33 +000022
Eric Andersen3843e961999-11-25 07:30:46 +000023#include "internal.h"
Eric Andersenb99df0f1999-11-24 09:04:33 +000024#include <stdio.h>
Eric Andersen14ec6cf1999-12-05 22:17:02 +000025#include <stdarg.h>
Eric Andersenb99df0f1999-11-24 09:04:33 +000026#include <sys/socket.h>
27#include <sys/un.h>
28#include <unistd.h>
Eric Andersen3843e961999-11-25 07:30:46 +000029#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>
Eric Andersenb186d981999-12-03 09:19:54 +000036#include <sys/klog.h>
37#include <errno.h>
38#include <paths.h>
39
40#define ksyslog klogctl
41extern int ksyslog(int type, char *buf, int len);
Eric Andersenb99df0f1999-11-24 09:04:33 +000042
43
Eric Andersen3843e961999-11-25 07:30:46 +000044/* 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"
Eric Andersen3843e961999-11-25 07:30:46 +000050
51
52static char* logFilePath = __LOG_FILE;
53/* interval between marks in seconds */
54static int MarkInterval = 20*60;
55/* localhost's name */
56static char LocalHostName[32];
57
58static const char syslogd_usage[] =
59 "syslogd [OPTION]...\n\n"
Eric Andersen0ecb54a1999-12-05 23:24:55 +000060 "Linux system and kernel (provides klogd) logging utility.\n"
61 "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n"
Eric Andersen3843e961999-11-25 07:30:46 +000062 "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"
Eric Andersen2cb55071999-12-10 08:25:07 +000065#ifdef BB_KLOGD
66 "\t-K\tDo not start up the klogd process (by default syslogd spawns klogd).\n"
67#endif
Eric Andersen3843e961999-11-25 07:30:46 +000068 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n";
69
Eric Andersen3843e961999-11-25 07:30:46 +000070
71/* try to open up the specified device */
72static 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;
Eric Andersenb99df0f1999-11-24 09:04:33 +000088}
89
Eric Andersen3843e961999-11-25 07:30:46 +000090/* print a message to the log file */
91static 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. */
Eric Andersenb186d981999-12-03 09:19:54 +0000103 if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NONBLOCK)) >= 0) {
Eric Andersen3843e961999-11-25 07:30:46 +0000104 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 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000115 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000116}
117
Eric Andersen3843e961999-11-25 07:30:46 +0000118static 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;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000124
Eric Andersen3843e961999-11-25 07:30:46 +0000125 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
150static void quit_signal(int sig)
151{
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000152 logMessage(LOG_SYSLOG|LOG_INFO, "System log daemon exiting.");
153 unlink( _PATH_LOG);
Eric Andersen3843e961999-11-25 07:30:46 +0000154 exit( TRUE);
155}
156
157static void restart_signal(int sig)
158{
159 /* pretend to restart */
160 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd restarting");
161}
162
163static void domark(int sig)
164{
165 if (MarkInterval > 0) {
166 logMessage(LOG_SYSLOG|LOG_INFO, "-- MARK --");
167 signal(SIGALRM, domark);
168 alarm(MarkInterval);
169 }
170}
171
172static void doSyslogd(void)
173{
174 struct sockaddr_un sunx;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000175 int fd, conn;
176 size_t addrLength;
Eric Andersen3843e961999-11-25 07:30:46 +0000177 char buf[1024];
178 char *q, *p = buf;
179 int readSize;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000180
Eric Andersen3843e961999-11-25 07:30:46 +0000181 /* Remove any preexisting socket/file */
Eric Andersenb99df0f1999-11-24 09:04:33 +0000182 unlink(_PATH_LOG);
183
Eric Andersen3843e961999-11-25 07:30:46 +0000184 /* Set up sig handlers */
185 signal(SIGINT, quit_signal);
186 signal(SIGTERM, quit_signal);
187 signal(SIGQUIT, quit_signal);
188 signal(SIGHUP, restart_signal);
189 signal(SIGALRM, domark);
190 alarm(MarkInterval);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000191
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000192
193 unlink( _PATH_LOG);
Eric Andersen3843e961999-11-25 07:30:46 +0000194 memset(&sunx, 0, sizeof(sunx));
195 sunx.sun_family = AF_UNIX; /* Unix domain socket */
196 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000197 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) {
Eric Andersen3843e961999-11-25 07:30:46 +0000198 perror("Couldn't obtain descriptor for socket " _PATH_LOG);
199 exit( FALSE);
200 }
201
202 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
203 if ( (bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000204 (fchmod(fd, 0666) < 0) || (listen(fd, 5)) )
Eric Andersen3843e961999-11-25 07:30:46 +0000205 {
206 perror("Could not connect to socket " _PATH_LOG);
207 exit( FALSE);
208 }
209
210
Eric Andersen3843e961999-11-25 07:30:46 +0000211 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd started: "
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000212 "BusyBox v" BB_VER " (" BB_BT ")");
Eric Andersenb99df0f1999-11-24 09:04:33 +0000213
214
Eric Andersen3843e961999-11-25 07:30:46 +0000215 while ((conn = accept(fd, (struct sockaddr *) &sunx,
216 &addrLength)) >= 0)
217 {
218 while ((readSize=read(conn, buf, sizeof(buf))) > 0)
219 {
220 char line[1025];
221 unsigned char c;
222 int pri = (LOG_USER|LOG_NOTICE);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000223
Eric Andersen3843e961999-11-25 07:30:46 +0000224 memset (line, 0, sizeof(line));
225 p = buf;
226 q = line;
227 while ( p && (c = *p) && q < &line[sizeof(line) - 1]) {
228 if (c == '<') {
229 /* Parse the magic priority number */
230 pri = 0;
231 while (isdigit(*(++p))) {
232 pri = 10 * pri + (*p - '0');
233 }
234 if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
235 pri = (LOG_USER|LOG_NOTICE);
236 } else if (c == '\n') {
237 *q++ = ' ';
238 } else if (iscntrl(c)&&(c<0177)) {
239 *q++ = '^';
240 *q++ = c ^ 0100;
241 } else {
242 *q++ = c;
243 }
244 p++;
245 }
246 *q = '\0';
Eric Andersenb99df0f1999-11-24 09:04:33 +0000247
Eric Andersen3843e961999-11-25 07:30:46 +0000248 /* Now log it */
249 logMessage( pri, line);
250 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000251 close(conn);
252 }
253
Eric Andersenb99df0f1999-11-24 09:04:33 +0000254 close(fd);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000255}
256
Eric Andersen2cb55071999-12-10 08:25:07 +0000257#ifdef BB_KLOGD
258
Eric Andersenb186d981999-12-03 09:19:54 +0000259static void klogd_signal(int sig)
260{
Eric Andersen0ecb54a1999-12-05 23:24:55 +0000261 ksyslog(7, NULL, 0);
262 ksyslog(0, 0, 0);
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000263 logMessage(LOG_SYSLOG|LOG_INFO, "Kernel log daemon exiting.");
Eric Andersenb186d981999-12-03 09:19:54 +0000264 exit( TRUE);
265}
266
Eric Andersenb186d981999-12-03 09:19:54 +0000267static void doKlogd(void)
268{
269 int priority=LOG_INFO;
Eric Andersenb186d981999-12-03 09:19:54 +0000270 char log_buffer[4096];
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000271 char *logp;
Eric Andersenb186d981999-12-03 09:19:54 +0000272
273 /* Set up sig handlers */
274 signal(SIGINT, klogd_signal);
275 signal(SIGKILL, klogd_signal);
276 signal(SIGTERM, klogd_signal);
277 signal(SIGHUP, klogd_signal);
278 logMessage(LOG_SYSLOG|LOG_INFO, "klogd started: "
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000279 "BusyBox v" BB_VER " (" BB_BT ")");
Eric Andersenb186d981999-12-03 09:19:54 +0000280
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000281 ksyslog(1, NULL, 0);
Eric Andersen0ecb54a1999-12-05 23:24:55 +0000282
Eric Andersenb186d981999-12-03 09:19:54 +0000283 while (1) {
Eric Andersen0ecb54a1999-12-05 23:24:55 +0000284 /* Use kernel syscalls */
Eric Andersenb186d981999-12-03 09:19:54 +0000285 memset(log_buffer, '\0', sizeof(log_buffer));
Eric Andersenb186d981999-12-03 09:19:54 +0000286 if ( ksyslog(2, log_buffer, sizeof(log_buffer)) < 0 ) {
287 char message[80];
288 if ( errno == EINTR )
289 continue;
290 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
291 "%d - %s.\n", errno, strerror(errno));
292 logMessage(LOG_SYSLOG|LOG_ERR, message);
293 exit(1);
294 }
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000295 logp=log_buffer;
Eric Andersenb186d981999-12-03 09:19:54 +0000296 if ( *log_buffer == '<' )
297 {
298 switch ( *(log_buffer+1) )
299 {
300 case '0':
301 priority = LOG_EMERG;
302 break;
303 case '1':
304 priority = LOG_ALERT;
305 break;
306 case '2':
307 priority = LOG_CRIT;
308 break;
309 case '3':
310 priority = LOG_ERR;
311 break;
312 case '4':
313 priority = LOG_WARNING;
314 break;
315 case '5':
316 priority = LOG_NOTICE;
317 break;
318 case '6':
319 priority = LOG_INFO;
320 break;
321 case '7':
322 default:
323 priority = LOG_DEBUG;
324 }
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000325 logp+=3;
Eric Andersenb186d981999-12-03 09:19:54 +0000326 }
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000327 logMessage(LOG_KERN|priority, logp);
Eric Andersenb186d981999-12-03 09:19:54 +0000328 }
329
330}
331
Eric Andersen2cb55071999-12-10 08:25:07 +0000332#endif
Eric Andersenb99df0f1999-11-24 09:04:33 +0000333
Eric Andersen3843e961999-11-25 07:30:46 +0000334extern int syslogd_main(int argc, char **argv)
335{
Eric Andersenb186d981999-12-03 09:19:54 +0000336 int pid, klogd_pid;
Eric Andersen3843e961999-11-25 07:30:46 +0000337 int doFork = TRUE;
Eric Andersen2cb55071999-12-10 08:25:07 +0000338#ifdef BB_KLOGD
339 int startKlogd = TRUE;
340#endif
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000341 char *p;
Eric Andersenb186d981999-12-03 09:19:54 +0000342 char **argv1=argv;
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000343
Eric Andersenb186d981999-12-03 09:19:54 +0000344 while (--argc > 0 && **(++argv1) == '-') {
345 while (*(++(*argv1))) {
346 switch (**argv1) {
Eric Andersen3843e961999-11-25 07:30:46 +0000347 case 'm':
348 if (--argc == 0) {
349 usage(syslogd_usage);
350 }
Eric Andersenb186d981999-12-03 09:19:54 +0000351 MarkInterval = atoi(*(++argv1))*60;
Eric Andersen3843e961999-11-25 07:30:46 +0000352 break;
353 case 'n':
354 doFork = FALSE;
355 break;
Eric Andersen2cb55071999-12-10 08:25:07 +0000356#ifdef BB_KLOGD
357 case 'K':
358 startKlogd = FALSE;
359 break;
360#endif
Eric Andersen3843e961999-11-25 07:30:46 +0000361 case 'O':
362 if (--argc == 0) {
363 usage(syslogd_usage);
364 }
Eric Andersenb186d981999-12-03 09:19:54 +0000365 logFilePath = *(++argv1);
Eric Andersen3843e961999-11-25 07:30:46 +0000366 break;
367 default:
368 usage(syslogd_usage);
369 }
370 }
371 }
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000372
373 /* Store away localhost's name before the fork */
374 gethostname(LocalHostName, sizeof(LocalHostName));
375 if ( (p = strchr(LocalHostName, '.')) ) {
376 *p++ = '\0';
377 }
Eric Andersen3843e961999-11-25 07:30:46 +0000378
379 if (doFork == TRUE) {
380 pid = fork();
381 if ( pid < 0 )
382 exit( pid);
383 else if ( pid == 0 ) {
Eric Andersenb186d981999-12-03 09:19:54 +0000384 strncpy(argv[0], "syslogd",strlen(argv[0]));
Eric Andersen3843e961999-11-25 07:30:46 +0000385 doSyslogd();
386 }
387 } else {
388 doSyslogd();
389 }
Eric Andersenb186d981999-12-03 09:19:54 +0000390
Eric Andersen2cb55071999-12-10 08:25:07 +0000391#ifdef BB_KLOGD
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000392 /* Start up the klogd process */
Eric Andersen2cb55071999-12-10 08:25:07 +0000393 if (startKlogd == TRUE) {
394 klogd_pid = fork();
395 if (klogd_pid == 0 ) {
396 strncpy(argv[0], "klogd", strlen(argv[0]));
397 doKlogd();
398 }
Eric Andersenb186d981999-12-03 09:19:54 +0000399 }
Eric Andersen2cb55071999-12-10 08:25:07 +0000400#endif
Eric Andersenb186d981999-12-03 09:19:54 +0000401
Eric Andersen3843e961999-11-25 07:30:46 +0000402 exit( TRUE);
403}
404
Eric Andersenb99df0f1999-11-24 09:04:33 +0000405