blob: 8049fc5d1675777c5b599b1631808f03f21e491e [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3843e961999-11-25 07:30:46 +00002/*
3 * Mini syslogd implementation for busybox
4 *
Eric Andersen8ec10a92001-01-27 09:33:39 +00005 * Copyright (C) 1999,2000,2001 by Lineo, inc.
Eric Andersen3843e961999-11-25 07:30:46 +00006 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 *
Erik Andersenf13df372000-04-18 23:51:51 +00008 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
9 *
Eric Andersen3843e961999-11-25 07:30:46 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
Eric Andersenb99df0f1999-11-24 09:04:33 +000025
Eric Andersen67e32302000-06-19 17:48:02 +000026#include <stdio.h>
27#include <stdlib.h>
Eric Andersen3843e961999-11-25 07:30:46 +000028#include <ctype.h>
Eric Andersenb186d981999-12-03 09:19:54 +000029#include <errno.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000030#include <fcntl.h>
31#include <netdb.h>
Eric Andersenb186d981999-12-03 09:19:54 +000032#include <paths.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000033#include <signal.h>
34#include <stdarg.h>
Eric Andersen67e32302000-06-19 17:48:02 +000035#include <time.h>
Eric Andersened3ef502001-01-27 08:24:39 +000036#include <string.h>
Eric Andersen67e32302000-06-19 17:48:02 +000037#include <unistd.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000038#include <sys/socket.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000039#include <sys/types.h>
40#include <sys/un.h>
Erik Andersen7d6ba572000-04-19 20:02:50 +000041#include <sys/param.h>
Eric Andersenb186d981999-12-03 09:19:54 +000042
Eric Andersen999bf722000-07-09 06:59:58 +000043#if ! defined __GLIBC__ && ! defined __UCLIBC__
Eric Andersened3ef502001-01-27 08:24:39 +000044#include <sys/syscall.h>
45#include <linux/unistd.h>
Eric Andersena15cd0b2000-06-19 18:14:20 +000046typedef unsigned int socklen_t;
47
Eric Andersen67e32302000-06-19 17:48:02 +000048#ifndef __alpha__
49# define __NR_klogctl __NR_syslog
50static 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
Eric Andersencbe31da2001-02-20 06:14:08 +000058#include "busybox.h"
Eric Andersen67e32302000-06-19 17:48:02 +000059
Eric Andersenb99df0f1999-11-24 09:04:33 +000060
61
Eric Andersen3843e961999-11-25 07:30:46 +000062/* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
63#define SYSLOG_NAMES
64#include <sys/syslog.h>
Eric Andersenced2cef2000-07-20 23:41:24 +000065#include <sys/uio.h>
Eric Andersen3843e961999-11-25 07:30:46 +000066
67/* Path for the file where all log messages are written */
Erik Andersen983b51b2000-04-04 18:14:25 +000068#define __LOG_FILE "/var/log/messages"
Eric Andersen3843e961999-11-25 07:30:46 +000069
Erik Andersen983b51b2000-04-04 18:14:25 +000070/* Path to the unix socket */
Erik Andersen4f3f7572000-04-28 00:18:56 +000071char lfile[BUFSIZ] = "";
Eric Andersen3843e961999-11-25 07:30:46 +000072
Erik Andersene49d5ec2000-02-08 19:58:47 +000073static char *logFilePath = __LOG_FILE;
74
Eric Andersen3843e961999-11-25 07:30:46 +000075/* interval between marks in seconds */
Erik Andersene49d5ec2000-02-08 19:58:47 +000076static int MarkInterval = 20 * 60;
77
Eric Andersen3843e961999-11-25 07:30:46 +000078/* localhost's name */
79static char LocalHostName[32];
80
Eric Andersenced2cef2000-07-20 23:41:24 +000081#ifdef BB_FEATURE_REMOTE_LOG
82#include <netinet/in.h>
83/* udp socket for logging to remote host */
Eric Andersenbf2b8ae2000-12-08 19:52:01 +000084static int remotefd = -1;
Eric Andersenced2cef2000-07-20 23:41:24 +000085/* where do we log? */
86static char *RemoteHost;
87/* what port to log to? */
Eric Andersenbf2b8ae2000-12-08 19:52:01 +000088static int RemotePort = 514;
Eric Andersenced2cef2000-07-20 23:41:24 +000089/* To remote log or not to remote log, that is the question. */
Eric Andersenbf2b8ae2000-12-08 19:52:01 +000090static int doRemoteLog = FALSE;
Eric Andersen70d09ed2000-12-11 16:24:16 +000091static int local_logging = FALSE;
Eric Andersenced2cef2000-07-20 23:41:24 +000092#endif
93
Erik Andersenc053e412000-03-21 01:31:24 +000094/* Note: There is also a function called "message()" in init.c */
Erik Andersen983b51b2000-04-04 18:14:25 +000095/* Print a message to the log file. */
Erik Andersenf13df372000-04-18 23:51:51 +000096static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
97static void message (char *fmt, ...)
Eric Andersen3843e961999-11-25 07:30:46 +000098{
Erik Andersene49d5ec2000-02-08 19:58:47 +000099 int fd;
Erik Andersene3ed1562000-04-19 18:52:56 +0000100 struct flock fl;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101 va_list arguments;
Eric Andersen3843e961999-11-25 07:30:46 +0000102
Erik Andersene3ed1562000-04-19 18:52:56 +0000103 fl.l_whence = SEEK_SET;
104 fl.l_start = 0;
105 fl.l_len = 1;
106
Erik Andersenf13df372000-04-18 23:51:51 +0000107 if ((fd = device_open (logFilePath,
108 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
109 O_NONBLOCK)) >= 0) {
Erik Andersene3ed1562000-04-19 18:52:56 +0000110 fl.l_type = F_WRLCK;
111 fcntl (fd, F_SETLKW, &fl);
Erik Andersenf13df372000-04-18 23:51:51 +0000112 va_start (arguments, fmt);
113 vdprintf (fd, fmt, arguments);
114 va_end (arguments);
Erik Andersene3ed1562000-04-19 18:52:56 +0000115 fl.l_type = F_UNLCK;
116 fcntl (fd, F_SETLKW, &fl);
Erik Andersenf13df372000-04-18 23:51:51 +0000117 close (fd);
Eric Andersen3843e961999-11-25 07:30:46 +0000118 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 /* Always send console messages to /dev/console so people will see them. */
Erik Andersenf13df372000-04-18 23:51:51 +0000120 if ((fd = device_open (_PATH_CONSOLE,
121 O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) {
122 va_start (arguments, fmt);
123 vdprintf (fd, fmt, arguments);
124 va_end (arguments);
125 close (fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 } else {
Erik Andersenf13df372000-04-18 23:51:51 +0000127 fprintf (stderr, "Bummer, can't print: ");
128 va_start (arguments, fmt);
129 vfprintf (stderr, fmt, arguments);
130 fflush (stderr);
131 va_end (arguments);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132 }
Eric Andersen3843e961999-11-25 07:30:46 +0000133 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000134}
135
Erik Andersenf13df372000-04-18 23:51:51 +0000136static void logMessage (int pri, char *msg)
Eric Andersen3843e961999-11-25 07:30:46 +0000137{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000138 time_t now;
139 char *timestamp;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000140 static char res[20] = "";
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 CODE *c_pri, *c_fac;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000142
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000143 if (pri != 0) {
144 for (c_fac = facilitynames;
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000145 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000146 for (c_pri = prioritynames;
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000147 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
Matt Kraai5a964f92000-12-11 20:13:37 +0000148 if (c_fac->c_name == NULL || c_pri->c_name == NULL)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000149 snprintf(res, sizeof(res), "<%d>", pri);
150 else
151 snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
152 }
Eric Andersen3843e961999-11-25 07:30:46 +0000153
Erik Andersene49d5ec2000-02-08 19:58:47 +0000154 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000155 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156 time(&now);
157 timestamp = ctime(&now) + 4;
158 timestamp[15] = '\0';
159 } else {
160 timestamp = msg;
161 timestamp[15] = '\0';
162 msg += 16;
163 }
Eric Andersen3843e961999-11-25 07:30:46 +0000164
Erik Andersene49d5ec2000-02-08 19:58:47 +0000165 /* todo: supress duplicates */
Eric Andersen3843e961999-11-25 07:30:46 +0000166
Eric Andersenced2cef2000-07-20 23:41:24 +0000167#ifdef BB_FEATURE_REMOTE_LOG
168 /* send message to remote logger */
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000169 if ( -1 != remotefd){
Mark Whitley59ab0252001-01-23 22:30:04 +0000170static const int IOV_COUNT = 2;
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000171 struct iovec iov[IOV_COUNT];
172 struct iovec *v = iov;
Eric Andersenced2cef2000-07-20 23:41:24 +0000173
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000174 bzero(&res, sizeof(res));
175 snprintf(res, sizeof(res), "<%d>", pri);
176 v->iov_base = res ;
177 v->iov_len = strlen(res);
178 v++;
Eric Andersenced2cef2000-07-20 23:41:24 +0000179
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000180 v->iov_base = msg;
181 v->iov_len = strlen(msg);
182
183 if ( -1 == writev(remotefd,iov, IOV_COUNT)){
184 error_msg_and_die("syslogd: cannot write to remote file handle on"
Matt Kraaidd19c692001-01-31 19:00:21 +0000185 "%s:%d",RemoteHost,RemotePort);
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000186 }
187 }
188 if (local_logging == TRUE)
Eric Andersenced2cef2000-07-20 23:41:24 +0000189#endif
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000190 /* now spew out the message to wherever it is supposed to go */
191 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
Eric Andersen76fef0a2000-12-08 19:41:21 +0000192
Eric Andersenced2cef2000-07-20 23:41:24 +0000193
Eric Andersen3843e961999-11-25 07:30:46 +0000194}
195
196static void quit_signal(int sig)
197{
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000198 logMessage(0, "System log daemon exiting.");
Erik Andersen983b51b2000-04-04 18:14:25 +0000199 unlink(lfile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000200 exit(TRUE);
Eric Andersen3843e961999-11-25 07:30:46 +0000201}
202
Eric Andersen3843e961999-11-25 07:30:46 +0000203static void domark(int sig)
204{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 if (MarkInterval > 0) {
206 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
207 alarm(MarkInterval);
208 }
Eric Andersen3843e961999-11-25 07:30:46 +0000209}
210
Mark Whitley59ab0252001-01-23 22:30:04 +0000211static const int BUFSIZE = 1023;
Eric Andersen67e32302000-06-19 17:48:02 +0000212static int serveConnection (int conn)
Pavel Roskinda10ec02000-06-07 21:08:25 +0000213{
Eric Andersend35c2152001-01-25 23:49:09 +0000214 RESERVE_BB_BUFFER(buf, BUFSIZE + 1);
Pavel Roskinda10ec02000-06-07 21:08:25 +0000215 int n_read;
216
217 while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
218
219 int pri = (LOG_USER | LOG_NOTICE);
220 char line[ BUFSIZE + 1 ];
221 unsigned char c;
222
223 char *p = buf, *q = line;
224
225 buf[ n_read - 1 ] = '\0';
226
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 }
Eric Andersenced2cef2000-07-20 23:41:24 +0000234 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)){
Pavel Roskinda10ec02000-06-07 21:08:25 +0000235 pri = (LOG_USER | LOG_NOTICE);
Eric Andersenced2cef2000-07-20 23:41:24 +0000236 }
Pavel Roskinda10ec02000-06-07 21:08:25 +0000237 } else if (c == '\n') {
238 *q++ = ' ';
239 } else if (iscntrl (c) && (c < 0177)) {
240 *q++ = '^';
241 *q++ = c ^ 0100;
242 } else {
243 *q++ = c;
244 }
245 p++;
246 }
247 *q = '\0';
248 /* Now log it */
249 logMessage (pri, line);
250 }
Eric Andersen67e32302000-06-19 17:48:02 +0000251 return (0);
Pavel Roskinda10ec02000-06-07 21:08:25 +0000252}
253
Eric Andersenced2cef2000-07-20 23:41:24 +0000254
255#ifdef BB_FEATURE_REMOTE_LOG
256static void init_RemoteLog (void){
257
258 struct sockaddr_in remoteaddr;
259 struct hostent *hostinfo;
260 int len = sizeof(remoteaddr);
261
262 bzero(&remoteaddr, len);
263
264 remotefd = socket(AF_INET, SOCK_DGRAM, 0);
265
266 if (remotefd < 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000267 error_msg_and_die("syslogd: cannot create socket");
Eric Andersenced2cef2000-07-20 23:41:24 +0000268 }
269
270 hostinfo = (struct hostent *) gethostbyname(RemoteHost);
271
272 if (!hostinfo) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000273 error_msg_and_die("syslogd: cannot resolve remote host name [%s]", RemoteHost);
Eric Andersenced2cef2000-07-20 23:41:24 +0000274 }
275
276 remoteaddr.sin_family = AF_INET;
277 remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
278 remoteaddr.sin_port = htons(RemotePort);
279
280 /*
281 Since we are using UDP sockets, connect just sets the default host and port
282 for future operations
283 */
284 if ( 0 != (connect(remotefd, (struct sockaddr *) &remoteaddr, len))){
Matt Kraaidd19c692001-01-31 19:00:21 +0000285 error_msg_and_die("syslogd: cannot connect to remote host %s:%d", RemoteHost, RemotePort);
Eric Andersenced2cef2000-07-20 23:41:24 +0000286 }
287
288}
289#endif
290
Erik Andersen983b51b2000-04-04 18:14:25 +0000291static void doSyslogd (void) __attribute__ ((noreturn));
292static void doSyslogd (void)
Eric Andersen3843e961999-11-25 07:30:46 +0000293{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000294 struct sockaddr_un sunx;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000295 socklen_t addrLength;
296
Erik Andersenf13df372000-04-18 23:51:51 +0000297
Erik Andersen983b51b2000-04-04 18:14:25 +0000298 int sock_fd;
Erik Andersenf13df372000-04-18 23:51:51 +0000299 fd_set fds;
300
Eric Andersend35c2152001-01-25 23:49:09 +0000301 RESERVE_BB_BUFFER(lfile, BUFSIZ);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000302
Erik Andersenf13df372000-04-18 23:51:51 +0000303 /* Set up signal handlers. */
Erik Andersen983b51b2000-04-04 18:14:25 +0000304 signal (SIGINT, quit_signal);
305 signal (SIGTERM, quit_signal);
306 signal (SIGQUIT, quit_signal);
307 signal (SIGHUP, SIG_IGN);
Pavel Roskind39d1202000-09-13 14:14:29 +0000308 signal (SIGCHLD, SIG_IGN);
309#ifdef SIGCLD
Erik Andersene3ed1562000-04-19 18:52:56 +0000310 signal (SIGCLD, SIG_IGN);
Pavel Roskind39d1202000-09-13 14:14:29 +0000311#endif
Erik Andersen983b51b2000-04-04 18:14:25 +0000312 signal (SIGALRM, domark);
313 alarm (MarkInterval);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000314
Erik Andersenf13df372000-04-18 23:51:51 +0000315 /* Create the syslog file so realpath() can work. */
316 close (open (_PATH_LOG, O_RDWR | O_CREAT, 0644));
317 if (realpath (_PATH_LOG, lfile) == NULL)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000318 perror_msg_and_die ("Could not resolve path to " _PATH_LOG);
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000319
Erik Andersen983b51b2000-04-04 18:14:25 +0000320 unlink (lfile);
321
Erik Andersenf13df372000-04-18 23:51:51 +0000322 memset (&sunx, 0, sizeof (sunx));
Erik Andersen983b51b2000-04-04 18:14:25 +0000323 sunx.sun_family = AF_UNIX;
Erik Andersenf13df372000-04-18 23:51:51 +0000324 strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path));
325 if ((sock_fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000326 perror_msg_and_die ("Couldn't obtain descriptor for socket " _PATH_LOG);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000327
Erik Andersen983b51b2000-04-04 18:14:25 +0000328 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
Erik Andersenf13df372000-04-18 23:51:51 +0000329 if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || (listen (sock_fd, 5)))
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000330 perror_msg_and_die ("Could not connect to socket " _PATH_LOG);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331
Erik Andersenf13df372000-04-18 23:51:51 +0000332 if (chmod (lfile, 0666) < 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000333 perror_msg_and_die ("Could not set permission on " _PATH_LOG);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000334
Erik Andersenf13df372000-04-18 23:51:51 +0000335 FD_ZERO (&fds);
336 FD_SET (sock_fd, &fds);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000337
Eric Andersenced2cef2000-07-20 23:41:24 +0000338 #ifdef BB_FEATURE_REMOTE_LOG
339 if (doRemoteLog == TRUE){
340 init_RemoteLog();
341 }
342 #endif
343
Erik Andersen983b51b2000-04-04 18:14:25 +0000344 logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345
Erik Andersen983b51b2000-04-04 18:14:25 +0000346 for (;;) {
Erik Andersenf13df372000-04-18 23:51:51 +0000347
348 fd_set readfds;
349 int n_ready;
350 int fd;
351
352 memcpy (&readfds, &fds, sizeof (fds));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000353
Erik Andersen983b51b2000-04-04 18:14:25 +0000354 if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
355 if (errno == EINTR) continue; /* alarm may have happened. */
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000356 perror_msg_and_die ("select error");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000357 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000358
Erik Andersenf13df372000-04-18 23:51:51 +0000359 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) {
Erik Andersen983b51b2000-04-04 18:14:25 +0000360 if (FD_ISSET (fd, &readfds)) {
Erik Andersene3ed1562000-04-19 18:52:56 +0000361
Erik Andersenf13df372000-04-18 23:51:51 +0000362 --n_ready;
Erik Andersene3ed1562000-04-19 18:52:56 +0000363
Erik Andersen983b51b2000-04-04 18:14:25 +0000364 if (fd == sock_fd) {
Erik Andersene3ed1562000-04-19 18:52:56 +0000365
366 int conn;
367 pid_t pid;
368
Erik Andersenf13df372000-04-18 23:51:51 +0000369 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000370 perror_msg_and_die ("accept error");
Erik Andersen983b51b2000-04-04 18:14:25 +0000371 }
Erik Andersena6c75222000-04-18 00:00:52 +0000372
Erik Andersene3ed1562000-04-19 18:52:56 +0000373 pid = fork();
Erik Andersen983b51b2000-04-04 18:14:25 +0000374
Erik Andersene3ed1562000-04-19 18:52:56 +0000375 if (pid < 0) {
376 perror ("syslogd: fork");
377 close (conn);
378 continue;
Erik Andersen983b51b2000-04-04 18:14:25 +0000379 }
Erik Andersene3ed1562000-04-19 18:52:56 +0000380
Eric Andersenb2fc5a02000-08-20 06:11:32 +0000381 if (pid == 0) {
Pavel Roskinda10ec02000-06-07 21:08:25 +0000382 serveConnection (conn);
Eric Andersenb2fc5a02000-08-20 06:11:32 +0000383 close (conn);
384 exit( TRUE);
385 }
Erik Andersene3ed1562000-04-19 18:52:56 +0000386 close (conn);
Erik Andersen983b51b2000-04-04 18:14:25 +0000387 }
388 }
389 }
390 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000391}
392
Eric Andersen03f4c272000-07-06 23:10:29 +0000393#ifdef BB_FEATURE_KLOGD
Eric Andersen2cb55071999-12-10 08:25:07 +0000394
Eric Andersenb186d981999-12-03 09:19:54 +0000395static void klogd_signal(int sig)
396{
Eric Andersen67e32302000-06-19 17:48:02 +0000397 klogctl(7, NULL, 0);
398 klogctl(0, 0, 0);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000399 logMessage(0, "Kernel log daemon exiting.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000400 exit(TRUE);
Eric Andersenb186d981999-12-03 09:19:54 +0000401}
402
Erik Andersen983b51b2000-04-04 18:14:25 +0000403static void doKlogd (void) __attribute__ ((noreturn));
404static void doKlogd (void)
Eric Andersenb186d981999-12-03 09:19:54 +0000405{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406 int priority = LOG_INFO;
407 char log_buffer[4096];
Eric Andersencf7982e2000-12-11 16:31:11 +0000408 int i, n, lastc;
409 char *start;
Eric Andersenb186d981999-12-03 09:19:54 +0000410
Erik Andersene49d5ec2000-02-08 19:58:47 +0000411 /* Set up sig handlers */
412 signal(SIGINT, klogd_signal);
413 signal(SIGKILL, klogd_signal);
414 signal(SIGTERM, klogd_signal);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000415 signal(SIGHUP, SIG_IGN);
Eric Andersenced2cef2000-07-20 23:41:24 +0000416
417#ifdef BB_FEATURE_REMOTE_LOG
418 if (doRemoteLog == TRUE){
419 init_RemoteLog();
420 }
421#endif
422
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000423 logMessage(0, "klogd started: "
Erik Andersene49d5ec2000-02-08 19:58:47 +0000424 "BusyBox v" BB_VER " (" BB_BT ")");
Eric Andersenb186d981999-12-03 09:19:54 +0000425
Eric Andersencf7982e2000-12-11 16:31:11 +0000426 /* "Open the log. Currently a NOP." */
Eric Andersen67e32302000-06-19 17:48:02 +0000427 klogctl(1, NULL, 0);
Eric Andersen0ecb54a1999-12-05 23:24:55 +0000428
Erik Andersene49d5ec2000-02-08 19:58:47 +0000429 while (1) {
430 /* Use kernel syscalls */
431 memset(log_buffer, '\0', sizeof(log_buffer));
Eric Andersencf7982e2000-12-11 16:31:11 +0000432 n = klogctl(2, log_buffer, sizeof(log_buffer));
433 if (n < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000434 char message[80];
435
436 if (errno == EINTR)
437 continue;
438 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
439 "%d - %s.\n", errno, strerror(errno));
440 logMessage(LOG_SYSLOG | LOG_ERR, message);
441 exit(1);
442 }
Eric Andersencf7982e2000-12-11 16:31:11 +0000443
444 /* klogctl buffer parsing modelled after code in dmesg.c */
445 start=&log_buffer[0];
446 lastc='\0';
447 for (i=0; i<n; i++) {
448 if (lastc == '\0' && log_buffer[i] == '<') {
449 priority = 0;
450 i++;
451 while (isdigit(log_buffer[i])) {
452 priority = priority*10+(log_buffer[i]-'0');
453 i++;
454 }
455 if (log_buffer[i] == '>') i++;
456 start = &log_buffer[i];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000457 }
Eric Andersencf7982e2000-12-11 16:31:11 +0000458 if (log_buffer[i] == '\n') {
459 log_buffer[i] = '\0'; /* zero terminate this message */
460 logMessage(LOG_KERN | priority, start);
461 start = &log_buffer[i+1];
462 priority = LOG_INFO;
463 }
464 lastc = log_buffer[i];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000465 }
Eric Andersenb186d981999-12-03 09:19:54 +0000466 }
Eric Andersenb186d981999-12-03 09:19:54 +0000467
468}
469
Eric Andersen2cb55071999-12-10 08:25:07 +0000470#endif
Eric Andersenb99df0f1999-11-24 09:04:33 +0000471
Erik Andersen983b51b2000-04-04 18:14:25 +0000472static void daemon_init (char **argv, char *dz, void fn (void))
473{
474 setsid();
475 chdir ("/");
476 strncpy(argv[0], dz, strlen(argv[0]));
477 fn();
478 exit(0);
479}
480
Eric Andersen3843e961999-11-25 07:30:46 +0000481extern int syslogd_main(int argc, char **argv)
482{
Eric Andersen394cf222000-12-11 16:48:50 +0000483 int opt, pid, klogd_pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000484 int doFork = TRUE;
485
Eric Andersen03f4c272000-07-06 23:10:29 +0000486#ifdef BB_FEATURE_KLOGD
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 int startKlogd = TRUE;
Eric Andersen2cb55071999-12-10 08:25:07 +0000488#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000489 char *p;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000490
Eric Andersen394cf222000-12-11 16:48:50 +0000491 /* do normal option parsing */
492 while ((opt = getopt(argc, argv, "m:nKO:R:L")) > 0) {
493 switch (opt) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000494 case 'm':
Eric Andersen394cf222000-12-11 16:48:50 +0000495 MarkInterval = atoi(optarg) * 60;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496 break;
497 case 'n':
498 doFork = FALSE;
499 break;
Eric Andersen03f4c272000-07-06 23:10:29 +0000500#ifdef BB_FEATURE_KLOGD
Erik Andersene49d5ec2000-02-08 19:58:47 +0000501 case 'K':
502 startKlogd = FALSE;
503 break;
504#endif
505 case 'O':
Eric Andersen394cf222000-12-11 16:48:50 +0000506 logFilePath = strdup(optarg);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507 break;
Eric Andersenced2cef2000-07-20 23:41:24 +0000508#ifdef BB_FEATURE_REMOTE_LOG
509 case 'R':
Eric Andersen394cf222000-12-11 16:48:50 +0000510 RemoteHost = strdup(optarg);
511 if ( (p = strchr(RemoteHost, ':'))){
512 RemotePort = atoi(p+1);
513 *p = '\0';
514 }
515 doRemoteLog = TRUE;
516 break;
Eric Andersen70d09ed2000-12-11 16:24:16 +0000517 case 'L':
518 local_logging = TRUE;
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000519 break;
Eric Andersenced2cef2000-07-20 23:41:24 +0000520#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000521 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000522 show_usage();
Eric Andersen3843e961999-11-25 07:30:46 +0000523 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524 }
525
Eric Andersen4ed17822000-12-11 19:28:29 +0000526#ifdef BB_FEATURE_REMOTE_LOG
527 /* If they have not specified remote logging, then log locally */
528 if (doRemoteLog == FALSE)
529 local_logging = TRUE;
530#endif
531
Erik Andersene49d5ec2000-02-08 19:58:47 +0000532 /* Store away localhost's name before the fork */
533 gethostname(LocalHostName, sizeof(LocalHostName));
534 if ((p = strchr(LocalHostName, '.'))) {
535 *p++ = '\0';
536 }
537
Erik Andersen983b51b2000-04-04 18:14:25 +0000538 umask(0);
539
Eric Andersen03f4c272000-07-06 23:10:29 +0000540#ifdef BB_FEATURE_KLOGD
Erik Andersene2729152000-02-18 21:34:17 +0000541 /* Start up the klogd process */
542 if (startKlogd == TRUE) {
543 klogd_pid = fork();
544 if (klogd_pid == 0) {
Erik Andersen983b51b2000-04-04 18:14:25 +0000545 daemon_init (argv, "klogd", doKlogd);
Erik Andersene2729152000-02-18 21:34:17 +0000546 }
547 }
548#endif
549
Erik Andersene49d5ec2000-02-08 19:58:47 +0000550 if (doFork == TRUE) {
551 pid = fork();
552 if (pid < 0)
553 exit(pid);
554 else if (pid == 0) {
Erik Andersen983b51b2000-04-04 18:14:25 +0000555 daemon_init (argv, "syslogd", doSyslogd);
Eric Andersen3843e961999-11-25 07:30:46 +0000556 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000557 } else {
558 doSyslogd();
Eric Andersen3843e961999-11-25 07:30:46 +0000559 }
Eric Andersenb186d981999-12-03 09:19:54 +0000560
Matt Kraai3e856ce2000-12-01 02:55:13 +0000561 return EXIT_SUCCESS;
Eric Andersen3843e961999-11-25 07:30:46 +0000562}
Erik Andersen983b51b2000-04-04 18:14:25 +0000563
564/*
Erik Andersene3ed1562000-04-19 18:52:56 +0000565Local Variables
566c-file-style: "linux"
567c-basic-offset: 4
568tab-width: 4
569End:
570*/