blob: 4217c362f140f5f6a8d2e3923faf921cfdc6e64d [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 *
Erik Andersen61677fe2000-04-13 01:18:56 +00005 * Copyright (C) 1999,2000 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 Andersen3570a342000-09-25 21:45:58 +000026#include "busybox.h"
Eric Andersen67e32302000-06-19 17:48:02 +000027#include <stdio.h>
28#include <stdlib.h>
Eric Andersen3843e961999-11-25 07:30:46 +000029#include <ctype.h>
Eric Andersenb186d981999-12-03 09:19:54 +000030#include <errno.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000031#include <fcntl.h>
32#include <netdb.h>
Eric Andersenb186d981999-12-03 09:19:54 +000033#include <paths.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000034#include <signal.h>
35#include <stdarg.h>
Eric Andersen67e32302000-06-19 17:48:02 +000036#include <time.h>
37#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 Andersen67e32302000-06-19 17:48:02 +000044
Eric Andersena15cd0b2000-06-19 18:14:20 +000045typedef unsigned int socklen_t;
46
Eric Andersen67e32302000-06-19 17:48:02 +000047#ifndef __alpha__
48# define __NR_klogctl __NR_syslog
49static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
50#else /* __alpha__ */
51#define klogctl syslog
52#endif
53
54#else
55# include <sys/klog.h>
56#endif
57
Eric Andersenb99df0f1999-11-24 09:04:33 +000058
59
Eric Andersen3843e961999-11-25 07:30:46 +000060/* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
61#define SYSLOG_NAMES
62#include <sys/syslog.h>
Eric Andersenced2cef2000-07-20 23:41:24 +000063#include <sys/uio.h>
Eric Andersen3843e961999-11-25 07:30:46 +000064
65/* Path for the file where all log messages are written */
Erik Andersen983b51b2000-04-04 18:14:25 +000066#define __LOG_FILE "/var/log/messages"
Eric Andersen3843e961999-11-25 07:30:46 +000067
Erik Andersen983b51b2000-04-04 18:14:25 +000068/* Path to the unix socket */
Erik Andersen4f3f7572000-04-28 00:18:56 +000069char lfile[BUFSIZ] = "";
Eric Andersen3843e961999-11-25 07:30:46 +000070
Erik Andersene49d5ec2000-02-08 19:58:47 +000071static char *logFilePath = __LOG_FILE;
72
Eric Andersen3843e961999-11-25 07:30:46 +000073/* interval between marks in seconds */
Erik Andersene49d5ec2000-02-08 19:58:47 +000074static int MarkInterval = 20 * 60;
75
Eric Andersen3843e961999-11-25 07:30:46 +000076/* localhost's name */
77static char LocalHostName[32];
78
Eric Andersenced2cef2000-07-20 23:41:24 +000079#ifdef BB_FEATURE_REMOTE_LOG
80#include <netinet/in.h>
81/* udp socket for logging to remote host */
Eric Andersenbf2b8ae2000-12-08 19:52:01 +000082static int remotefd = -1;
Eric Andersenced2cef2000-07-20 23:41:24 +000083/* where do we log? */
84static char *RemoteHost;
85/* what port to log to? */
Eric Andersenbf2b8ae2000-12-08 19:52:01 +000086static int RemotePort = 514;
Eric Andersenced2cef2000-07-20 23:41:24 +000087/* To remote log or not to remote log, that is the question. */
Eric Andersenbf2b8ae2000-12-08 19:52:01 +000088static int doRemoteLog = FALSE;
Eric Andersen70d09ed2000-12-11 16:24:16 +000089static int local_logging = FALSE;
Eric Andersenced2cef2000-07-20 23:41:24 +000090#endif
91
Erik Andersenc053e412000-03-21 01:31:24 +000092/* Note: There is also a function called "message()" in init.c */
Erik Andersen983b51b2000-04-04 18:14:25 +000093/* Print a message to the log file. */
Erik Andersenf13df372000-04-18 23:51:51 +000094static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
95static void message (char *fmt, ...)
Eric Andersen3843e961999-11-25 07:30:46 +000096{
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 int fd;
Erik Andersene3ed1562000-04-19 18:52:56 +000098 struct flock fl;
Erik Andersene49d5ec2000-02-08 19:58:47 +000099 va_list arguments;
Eric Andersen3843e961999-11-25 07:30:46 +0000100
Erik Andersene3ed1562000-04-19 18:52:56 +0000101 fl.l_whence = SEEK_SET;
102 fl.l_start = 0;
103 fl.l_len = 1;
104
Erik Andersenf13df372000-04-18 23:51:51 +0000105 if ((fd = device_open (logFilePath,
106 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
107 O_NONBLOCK)) >= 0) {
Erik Andersene3ed1562000-04-19 18:52:56 +0000108 fl.l_type = F_WRLCK;
109 fcntl (fd, F_SETLKW, &fl);
Erik Andersenf13df372000-04-18 23:51:51 +0000110 va_start (arguments, fmt);
111 vdprintf (fd, fmt, arguments);
112 va_end (arguments);
Erik Andersene3ed1562000-04-19 18:52:56 +0000113 fl.l_type = F_UNLCK;
114 fcntl (fd, F_SETLKW, &fl);
Erik Andersenf13df372000-04-18 23:51:51 +0000115 close (fd);
Eric Andersen3843e961999-11-25 07:30:46 +0000116 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117 /* Always send console messages to /dev/console so people will see them. */
Erik Andersenf13df372000-04-18 23:51:51 +0000118 if ((fd = device_open (_PATH_CONSOLE,
119 O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) {
120 va_start (arguments, fmt);
121 vdprintf (fd, fmt, arguments);
122 va_end (arguments);
123 close (fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 } else {
Erik Andersenf13df372000-04-18 23:51:51 +0000125 fprintf (stderr, "Bummer, can't print: ");
126 va_start (arguments, fmt);
127 vfprintf (stderr, fmt, arguments);
128 fflush (stderr);
129 va_end (arguments);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 }
Eric Andersen3843e961999-11-25 07:30:46 +0000131 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000132}
133
Erik Andersenf13df372000-04-18 23:51:51 +0000134static void logMessage (int pri, char *msg)
Eric Andersen3843e961999-11-25 07:30:46 +0000135{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136 time_t now;
137 char *timestamp;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000138 static char res[20] = "";
Erik Andersene49d5ec2000-02-08 19:58:47 +0000139 CODE *c_pri, *c_fac;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000140
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000141 if (pri != 0) {
142 for (c_fac = facilitynames;
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000143 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000144 for (c_pri = prioritynames;
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000145 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
Matt Kraai5a964f92000-12-11 20:13:37 +0000146 if (c_fac->c_name == NULL || c_pri->c_name == NULL)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000147 snprintf(res, sizeof(res), "<%d>", pri);
148 else
149 snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
150 }
Eric Andersen3843e961999-11-25 07:30:46 +0000151
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000153 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000154 time(&now);
155 timestamp = ctime(&now) + 4;
156 timestamp[15] = '\0';
157 } else {
158 timestamp = msg;
159 timestamp[15] = '\0';
160 msg += 16;
161 }
Eric Andersen3843e961999-11-25 07:30:46 +0000162
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 /* todo: supress duplicates */
Eric Andersen3843e961999-11-25 07:30:46 +0000164
Eric Andersenced2cef2000-07-20 23:41:24 +0000165#ifdef BB_FEATURE_REMOTE_LOG
166 /* send message to remote logger */
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000167 if ( -1 != remotefd){
Eric Andersenced2cef2000-07-20 23:41:24 +0000168#define IOV_COUNT 2
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000169 struct iovec iov[IOV_COUNT];
170 struct iovec *v = iov;
Eric Andersenced2cef2000-07-20 23:41:24 +0000171
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000172 bzero(&res, sizeof(res));
173 snprintf(res, sizeof(res), "<%d>", pri);
174 v->iov_base = res ;
175 v->iov_len = strlen(res);
176 v++;
Eric Andersenced2cef2000-07-20 23:41:24 +0000177
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000178 v->iov_base = msg;
179 v->iov_len = strlen(msg);
180
181 if ( -1 == writev(remotefd,iov, IOV_COUNT)){
182 error_msg_and_die("syslogd: cannot write to remote file handle on"
183 "%s:%d\n",RemoteHost,RemotePort);
184 }
185 }
186 if (local_logging == TRUE)
Eric Andersenced2cef2000-07-20 23:41:24 +0000187#endif
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000188 /* now spew out the message to wherever it is supposed to go */
189 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
Eric Andersen76fef0a2000-12-08 19:41:21 +0000190
Eric Andersenced2cef2000-07-20 23:41:24 +0000191
Eric Andersen3843e961999-11-25 07:30:46 +0000192}
193
194static void quit_signal(int sig)
195{
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000196 logMessage(0, "System log daemon exiting.");
Erik Andersen983b51b2000-04-04 18:14:25 +0000197 unlink(lfile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198 exit(TRUE);
Eric Andersen3843e961999-11-25 07:30:46 +0000199}
200
Eric Andersen3843e961999-11-25 07:30:46 +0000201static void domark(int sig)
202{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000203 if (MarkInterval > 0) {
204 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
205 alarm(MarkInterval);
206 }
Eric Andersen3843e961999-11-25 07:30:46 +0000207}
208
Pavel Roskinda10ec02000-06-07 21:08:25 +0000209#define BUFSIZE 1023
Eric Andersen67e32302000-06-19 17:48:02 +0000210static int serveConnection (int conn)
Pavel Roskinda10ec02000-06-07 21:08:25 +0000211{
212 char buf[ BUFSIZE + 1 ];
213 int n_read;
214
215 while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
216
217 int pri = (LOG_USER | LOG_NOTICE);
218 char line[ BUFSIZE + 1 ];
219 unsigned char c;
220
221 char *p = buf, *q = line;
222
223 buf[ n_read - 1 ] = '\0';
224
225 while (p && (c = *p) && q < &line[ sizeof (line) - 1 ]) {
226 if (c == '<') {
227 /* Parse the magic priority number. */
228 pri = 0;
229 while (isdigit (*(++p))) {
230 pri = 10 * pri + (*p - '0');
231 }
Eric Andersenced2cef2000-07-20 23:41:24 +0000232 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)){
Pavel Roskinda10ec02000-06-07 21:08:25 +0000233 pri = (LOG_USER | LOG_NOTICE);
Eric Andersenced2cef2000-07-20 23:41:24 +0000234 }
Pavel Roskinda10ec02000-06-07 21:08:25 +0000235 } 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 /* Now log it */
247 logMessage (pri, line);
248 }
Eric Andersen67e32302000-06-19 17:48:02 +0000249 return (0);
Pavel Roskinda10ec02000-06-07 21:08:25 +0000250}
251
Eric Andersenced2cef2000-07-20 23:41:24 +0000252
253#ifdef BB_FEATURE_REMOTE_LOG
254static void init_RemoteLog (void){
255
256 struct sockaddr_in remoteaddr;
257 struct hostent *hostinfo;
258 int len = sizeof(remoteaddr);
259
260 bzero(&remoteaddr, len);
261
262 remotefd = socket(AF_INET, SOCK_DGRAM, 0);
263
264 if (remotefd < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000265 error_msg_and_die("syslogd: cannot create socket\n");
Eric Andersenced2cef2000-07-20 23:41:24 +0000266 }
267
268 hostinfo = (struct hostent *) gethostbyname(RemoteHost);
269
270 if (!hostinfo) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000271 error_msg_and_die("syslogd: cannot resolve remote host name [%s]\n", RemoteHost);
Eric Andersenced2cef2000-07-20 23:41:24 +0000272 }
273
274 remoteaddr.sin_family = AF_INET;
275 remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
276 remoteaddr.sin_port = htons(RemotePort);
277
278 /*
279 Since we are using UDP sockets, connect just sets the default host and port
280 for future operations
281 */
282 if ( 0 != (connect(remotefd, (struct sockaddr *) &remoteaddr, len))){
Mark Whitleyf57c9442000-12-07 19:56:48 +0000283 error_msg_and_die("syslogd: cannot connect to remote host %s:%d\n", RemoteHost, RemotePort);
Eric Andersenced2cef2000-07-20 23:41:24 +0000284 }
285
286}
287#endif
288
Erik Andersen983b51b2000-04-04 18:14:25 +0000289static void doSyslogd (void) __attribute__ ((noreturn));
290static void doSyslogd (void)
Eric Andersen3843e961999-11-25 07:30:46 +0000291{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000292 struct sockaddr_un sunx;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000293 socklen_t addrLength;
294
Erik Andersenf13df372000-04-18 23:51:51 +0000295
Erik Andersen983b51b2000-04-04 18:14:25 +0000296 int sock_fd;
Erik Andersenf13df372000-04-18 23:51:51 +0000297 fd_set fds;
298
Erik Andersen4f3f7572000-04-28 00:18:56 +0000299 char lfile[BUFSIZ];
Eric Andersenb99df0f1999-11-24 09:04:33 +0000300
Erik Andersenf13df372000-04-18 23:51:51 +0000301 /* Set up signal handlers. */
Erik Andersen983b51b2000-04-04 18:14:25 +0000302 signal (SIGINT, quit_signal);
303 signal (SIGTERM, quit_signal);
304 signal (SIGQUIT, quit_signal);
305 signal (SIGHUP, SIG_IGN);
Pavel Roskind39d1202000-09-13 14:14:29 +0000306 signal (SIGCHLD, SIG_IGN);
307#ifdef SIGCLD
Erik Andersene3ed1562000-04-19 18:52:56 +0000308 signal (SIGCLD, SIG_IGN);
Pavel Roskind39d1202000-09-13 14:14:29 +0000309#endif
Erik Andersen983b51b2000-04-04 18:14:25 +0000310 signal (SIGALRM, domark);
311 alarm (MarkInterval);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000312
Erik Andersenf13df372000-04-18 23:51:51 +0000313 /* Create the syslog file so realpath() can work. */
314 close (open (_PATH_LOG, O_RDWR | O_CREAT, 0644));
315 if (realpath (_PATH_LOG, lfile) == NULL)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000316 perror_msg_and_die ("Could not resolve path to " _PATH_LOG);
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000317
Erik Andersen983b51b2000-04-04 18:14:25 +0000318 unlink (lfile);
319
Erik Andersenf13df372000-04-18 23:51:51 +0000320 memset (&sunx, 0, sizeof (sunx));
Erik Andersen983b51b2000-04-04 18:14:25 +0000321 sunx.sun_family = AF_UNIX;
Erik Andersenf13df372000-04-18 23:51:51 +0000322 strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path));
323 if ((sock_fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000324 perror_msg_and_die ("Couldn't obtain descriptor for socket " _PATH_LOG);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000325
Erik Andersen983b51b2000-04-04 18:14:25 +0000326 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
Erik Andersenf13df372000-04-18 23:51:51 +0000327 if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || (listen (sock_fd, 5)))
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000328 perror_msg_and_die ("Could not connect to socket " _PATH_LOG);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000329
Erik Andersenf13df372000-04-18 23:51:51 +0000330 if (chmod (lfile, 0666) < 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000331 perror_msg_and_die ("Could not set permission on " _PATH_LOG);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000332
Erik Andersenf13df372000-04-18 23:51:51 +0000333 FD_ZERO (&fds);
334 FD_SET (sock_fd, &fds);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000335
Eric Andersenced2cef2000-07-20 23:41:24 +0000336 #ifdef BB_FEATURE_REMOTE_LOG
337 if (doRemoteLog == TRUE){
338 init_RemoteLog();
339 }
340 #endif
341
Erik Andersen983b51b2000-04-04 18:14:25 +0000342 logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000343
Erik Andersen983b51b2000-04-04 18:14:25 +0000344 for (;;) {
Erik Andersenf13df372000-04-18 23:51:51 +0000345
346 fd_set readfds;
347 int n_ready;
348 int fd;
349
350 memcpy (&readfds, &fds, sizeof (fds));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000351
Erik Andersen983b51b2000-04-04 18:14:25 +0000352 if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
353 if (errno == EINTR) continue; /* alarm may have happened. */
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000354 perror_msg_and_die ("select error");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000355 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000356
Erik Andersenf13df372000-04-18 23:51:51 +0000357 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) {
Erik Andersen983b51b2000-04-04 18:14:25 +0000358 if (FD_ISSET (fd, &readfds)) {
Erik Andersene3ed1562000-04-19 18:52:56 +0000359
Erik Andersenf13df372000-04-18 23:51:51 +0000360 --n_ready;
Erik Andersene3ed1562000-04-19 18:52:56 +0000361
Erik Andersen983b51b2000-04-04 18:14:25 +0000362 if (fd == sock_fd) {
Erik Andersene3ed1562000-04-19 18:52:56 +0000363
364 int conn;
365 pid_t pid;
366
Erik Andersenf13df372000-04-18 23:51:51 +0000367 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000368 perror_msg_and_die ("accept error");
Erik Andersen983b51b2000-04-04 18:14:25 +0000369 }
Erik Andersena6c75222000-04-18 00:00:52 +0000370
Erik Andersene3ed1562000-04-19 18:52:56 +0000371 pid = fork();
Erik Andersen983b51b2000-04-04 18:14:25 +0000372
Erik Andersene3ed1562000-04-19 18:52:56 +0000373 if (pid < 0) {
374 perror ("syslogd: fork");
375 close (conn);
376 continue;
Erik Andersen983b51b2000-04-04 18:14:25 +0000377 }
Erik Andersene3ed1562000-04-19 18:52:56 +0000378
Eric Andersenb2fc5a02000-08-20 06:11:32 +0000379 if (pid == 0) {
Pavel Roskinda10ec02000-06-07 21:08:25 +0000380 serveConnection (conn);
Eric Andersenb2fc5a02000-08-20 06:11:32 +0000381 close (conn);
382 exit( TRUE);
383 }
Erik Andersene3ed1562000-04-19 18:52:56 +0000384 close (conn);
Erik Andersen983b51b2000-04-04 18:14:25 +0000385 }
386 }
387 }
388 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000389}
390
Eric Andersen03f4c272000-07-06 23:10:29 +0000391#ifdef BB_FEATURE_KLOGD
Eric Andersen2cb55071999-12-10 08:25:07 +0000392
Eric Andersenb186d981999-12-03 09:19:54 +0000393static void klogd_signal(int sig)
394{
Eric Andersen67e32302000-06-19 17:48:02 +0000395 klogctl(7, NULL, 0);
396 klogctl(0, 0, 0);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000397 logMessage(0, "Kernel log daemon exiting.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000398 exit(TRUE);
Eric Andersenb186d981999-12-03 09:19:54 +0000399}
400
Erik Andersen983b51b2000-04-04 18:14:25 +0000401static void doKlogd (void) __attribute__ ((noreturn));
402static void doKlogd (void)
Eric Andersenb186d981999-12-03 09:19:54 +0000403{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000404 int priority = LOG_INFO;
405 char log_buffer[4096];
Eric Andersencf7982e2000-12-11 16:31:11 +0000406 int i, n, lastc;
407 char *start;
Eric Andersenb186d981999-12-03 09:19:54 +0000408
Erik Andersene49d5ec2000-02-08 19:58:47 +0000409 /* Set up sig handlers */
410 signal(SIGINT, klogd_signal);
411 signal(SIGKILL, klogd_signal);
412 signal(SIGTERM, klogd_signal);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000413 signal(SIGHUP, SIG_IGN);
Eric Andersenced2cef2000-07-20 23:41:24 +0000414
415#ifdef BB_FEATURE_REMOTE_LOG
416 if (doRemoteLog == TRUE){
417 init_RemoteLog();
418 }
419#endif
420
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000421 logMessage(0, "klogd started: "
Erik Andersene49d5ec2000-02-08 19:58:47 +0000422 "BusyBox v" BB_VER " (" BB_BT ")");
Eric Andersenb186d981999-12-03 09:19:54 +0000423
Eric Andersencf7982e2000-12-11 16:31:11 +0000424 /* "Open the log. Currently a NOP." */
Eric Andersen67e32302000-06-19 17:48:02 +0000425 klogctl(1, NULL, 0);
Eric Andersen0ecb54a1999-12-05 23:24:55 +0000426
Erik Andersene49d5ec2000-02-08 19:58:47 +0000427 while (1) {
428 /* Use kernel syscalls */
429 memset(log_buffer, '\0', sizeof(log_buffer));
Eric Andersencf7982e2000-12-11 16:31:11 +0000430 n = klogctl(2, log_buffer, sizeof(log_buffer));
431 if (n < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000432 char message[80];
433
434 if (errno == EINTR)
435 continue;
436 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
437 "%d - %s.\n", errno, strerror(errno));
438 logMessage(LOG_SYSLOG | LOG_ERR, message);
439 exit(1);
440 }
Eric Andersencf7982e2000-12-11 16:31:11 +0000441
442 /* klogctl buffer parsing modelled after code in dmesg.c */
443 start=&log_buffer[0];
444 lastc='\0';
445 for (i=0; i<n; i++) {
446 if (lastc == '\0' && log_buffer[i] == '<') {
447 priority = 0;
448 i++;
449 while (isdigit(log_buffer[i])) {
450 priority = priority*10+(log_buffer[i]-'0');
451 i++;
452 }
453 if (log_buffer[i] == '>') i++;
454 start = &log_buffer[i];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000455 }
Eric Andersencf7982e2000-12-11 16:31:11 +0000456 if (log_buffer[i] == '\n') {
457 log_buffer[i] = '\0'; /* zero terminate this message */
458 logMessage(LOG_KERN | priority, start);
459 start = &log_buffer[i+1];
460 priority = LOG_INFO;
461 }
462 lastc = log_buffer[i];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000463 }
Eric Andersenb186d981999-12-03 09:19:54 +0000464 }
Eric Andersenb186d981999-12-03 09:19:54 +0000465
466}
467
Eric Andersen2cb55071999-12-10 08:25:07 +0000468#endif
Eric Andersenb99df0f1999-11-24 09:04:33 +0000469
Erik Andersen983b51b2000-04-04 18:14:25 +0000470static void daemon_init (char **argv, char *dz, void fn (void))
471{
472 setsid();
473 chdir ("/");
474 strncpy(argv[0], dz, strlen(argv[0]));
475 fn();
476 exit(0);
477}
478
Eric Andersen3843e961999-11-25 07:30:46 +0000479extern int syslogd_main(int argc, char **argv)
480{
Eric Andersen394cf222000-12-11 16:48:50 +0000481 int opt, pid, klogd_pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000482 int doFork = TRUE;
483
Eric Andersen03f4c272000-07-06 23:10:29 +0000484#ifdef BB_FEATURE_KLOGD
Erik Andersene49d5ec2000-02-08 19:58:47 +0000485 int startKlogd = TRUE;
Eric Andersen2cb55071999-12-10 08:25:07 +0000486#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 char *p;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000488
Eric Andersen394cf222000-12-11 16:48:50 +0000489 /* do normal option parsing */
490 while ((opt = getopt(argc, argv, "m:nKO:R:L")) > 0) {
491 switch (opt) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000492 case 'm':
Eric Andersen394cf222000-12-11 16:48:50 +0000493 MarkInterval = atoi(optarg) * 60;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000494 break;
495 case 'n':
496 doFork = FALSE;
497 break;
Eric Andersen03f4c272000-07-06 23:10:29 +0000498#ifdef BB_FEATURE_KLOGD
Erik Andersene49d5ec2000-02-08 19:58:47 +0000499 case 'K':
500 startKlogd = FALSE;
501 break;
502#endif
503 case 'O':
Eric Andersen394cf222000-12-11 16:48:50 +0000504 logFilePath = strdup(optarg);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000505 break;
Eric Andersenced2cef2000-07-20 23:41:24 +0000506#ifdef BB_FEATURE_REMOTE_LOG
507 case 'R':
Eric Andersen394cf222000-12-11 16:48:50 +0000508 RemoteHost = strdup(optarg);
509 if ( (p = strchr(RemoteHost, ':'))){
510 RemotePort = atoi(p+1);
511 *p = '\0';
512 }
513 doRemoteLog = TRUE;
514 break;
Eric Andersen70d09ed2000-12-11 16:24:16 +0000515 case 'L':
516 local_logging = TRUE;
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000517 break;
Eric Andersenced2cef2000-07-20 23:41:24 +0000518#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 default:
520 usage(syslogd_usage);
Eric Andersen3843e961999-11-25 07:30:46 +0000521 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000522 }
523
Eric Andersen4ed17822000-12-11 19:28:29 +0000524#ifdef BB_FEATURE_REMOTE_LOG
525 /* If they have not specified remote logging, then log locally */
526 if (doRemoteLog == FALSE)
527 local_logging = TRUE;
528#endif
529
Erik Andersene49d5ec2000-02-08 19:58:47 +0000530 /* Store away localhost's name before the fork */
531 gethostname(LocalHostName, sizeof(LocalHostName));
532 if ((p = strchr(LocalHostName, '.'))) {
533 *p++ = '\0';
534 }
535
Erik Andersen983b51b2000-04-04 18:14:25 +0000536 umask(0);
537
Eric Andersen03f4c272000-07-06 23:10:29 +0000538#ifdef BB_FEATURE_KLOGD
Erik Andersene2729152000-02-18 21:34:17 +0000539 /* Start up the klogd process */
540 if (startKlogd == TRUE) {
541 klogd_pid = fork();
542 if (klogd_pid == 0) {
Erik Andersen983b51b2000-04-04 18:14:25 +0000543 daemon_init (argv, "klogd", doKlogd);
Erik Andersene2729152000-02-18 21:34:17 +0000544 }
545 }
546#endif
547
Erik Andersene49d5ec2000-02-08 19:58:47 +0000548 if (doFork == TRUE) {
549 pid = fork();
550 if (pid < 0)
551 exit(pid);
552 else if (pid == 0) {
Erik Andersen983b51b2000-04-04 18:14:25 +0000553 daemon_init (argv, "syslogd", doSyslogd);
Eric Andersen3843e961999-11-25 07:30:46 +0000554 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000555 } else {
556 doSyslogd();
Eric Andersen3843e961999-11-25 07:30:46 +0000557 }
Eric Andersenb186d981999-12-03 09:19:54 +0000558
Matt Kraai3e856ce2000-12-01 02:55:13 +0000559 return EXIT_SUCCESS;
Eric Andersen3843e961999-11-25 07:30:46 +0000560}
Erik Andersen983b51b2000-04-04 18:14:25 +0000561
562/*
Erik Andersene3ed1562000-04-19 18:52:56 +0000563Local Variables
564c-file-style: "linux"
565c-basic-offset: 4
566tab-width: 4
567End:
568*/