blob: aa1218f3f18ae288b8645a97da55ebca127b1ed9 [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 Andersen3843e961999-11-25 07:30:46 +000026#include "internal.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>
39#include <sys/stat.h>
40#include <sys/types.h>
41#include <sys/un.h>
Erik Andersen7d6ba572000-04-19 20:02:50 +000042#include <sys/param.h>
Eric Andersen67e32302000-06-19 17:48:02 +000043#include <linux/unistd.h>
Eric Andersenb186d981999-12-03 09:19:54 +000044
Eric Andersen999bf722000-07-09 06:59:58 +000045#if ! defined __GLIBC__ && ! defined __UCLIBC__
Eric Andersen67e32302000-06-19 17:48:02 +000046
Eric Andersena15cd0b2000-06-19 18:14:20 +000047typedef unsigned int socklen_t;
48
Eric Andersen67e32302000-06-19 17:48:02 +000049#ifndef __alpha__
50# define __NR_klogctl __NR_syslog
51static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
52#else /* __alpha__ */
53#define klogctl syslog
54#endif
55
56#else
57# include <sys/klog.h>
58#endif
59
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>
65
66/* Path for the file where all log messages are written */
Erik Andersen983b51b2000-04-04 18:14:25 +000067#define __LOG_FILE "/var/log/messages"
Eric Andersen3843e961999-11-25 07:30:46 +000068
Erik Andersen983b51b2000-04-04 18:14:25 +000069/* Path to the unix socket */
Erik Andersen4f3f7572000-04-28 00:18:56 +000070char lfile[BUFSIZ] = "";
Eric Andersen3843e961999-11-25 07:30:46 +000071
Erik Andersene49d5ec2000-02-08 19:58:47 +000072static char *logFilePath = __LOG_FILE;
73
Eric Andersen3843e961999-11-25 07:30:46 +000074/* interval between marks in seconds */
Erik Andersene49d5ec2000-02-08 19:58:47 +000075static int MarkInterval = 20 * 60;
76
Eric Andersen3843e961999-11-25 07:30:46 +000077/* localhost's name */
78static char LocalHostName[32];
79
80static const char syslogd_usage[] =
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000081 "syslogd [OPTION]...\n"
82#ifndef BB_FEATURE_TRIVIAL_HELP
83 "\nLinux system and kernel (provides klogd) logging utility.\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n"
85 "Options:\n"
Pavel Roskinda10ec02000-06-07 21:08:25 +000086 "\t-m NUM\t\tInterval between MARK lines (default=20min, 0=off)\n"
87 "\t-n\t\tRun as a foreground process\n"
Eric Andersen03f4c272000-07-06 23:10:29 +000088#ifdef BB_FEATURE_KLOGD
Pavel Roskinda10ec02000-06-07 21:08:25 +000089 "\t-K\t\tDo not start up the klogd process\n"
Eric Andersen2cb55071999-12-10 08:25:07 +000090#endif
Pavel Roskinda10ec02000-06-07 21:08:25 +000091 "\t-O FILE\t\tUse an alternate log file (default=/var/log/messages)\n"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000092#endif
93 ;
Eric Andersen3843e961999-11-25 07:30:46 +000094
Erik Andersenc053e412000-03-21 01:31:24 +000095/* Note: There is also a function called "message()" in init.c */
Erik Andersen983b51b2000-04-04 18:14:25 +000096/* Print a message to the log file. */
Erik Andersenf13df372000-04-18 23:51:51 +000097static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
98static void message (char *fmt, ...)
Eric Andersen3843e961999-11-25 07:30:46 +000099{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 int fd;
Erik Andersene3ed1562000-04-19 18:52:56 +0000101 struct flock fl;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 va_list arguments;
Eric Andersen3843e961999-11-25 07:30:46 +0000103
Erik Andersene3ed1562000-04-19 18:52:56 +0000104 fl.l_whence = SEEK_SET;
105 fl.l_start = 0;
106 fl.l_len = 1;
107
Erik Andersenf13df372000-04-18 23:51:51 +0000108 if ((fd = device_open (logFilePath,
109 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
110 O_NONBLOCK)) >= 0) {
Erik Andersene3ed1562000-04-19 18:52:56 +0000111 fl.l_type = F_WRLCK;
112 fcntl (fd, F_SETLKW, &fl);
Erik Andersenf13df372000-04-18 23:51:51 +0000113 va_start (arguments, fmt);
114 vdprintf (fd, fmt, arguments);
115 va_end (arguments);
Erik Andersene3ed1562000-04-19 18:52:56 +0000116 fl.l_type = F_UNLCK;
117 fcntl (fd, F_SETLKW, &fl);
Erik Andersenf13df372000-04-18 23:51:51 +0000118 close (fd);
Eric Andersen3843e961999-11-25 07:30:46 +0000119 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 /* Always send console messages to /dev/console so people will see them. */
Erik Andersenf13df372000-04-18 23:51:51 +0000121 if ((fd = device_open (_PATH_CONSOLE,
122 O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) {
123 va_start (arguments, fmt);
124 vdprintf (fd, fmt, arguments);
125 va_end (arguments);
126 close (fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 } else {
Erik Andersenf13df372000-04-18 23:51:51 +0000128 fprintf (stderr, "Bummer, can't print: ");
129 va_start (arguments, fmt);
130 vfprintf (stderr, fmt, arguments);
131 fflush (stderr);
132 va_end (arguments);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000133 }
Eric Andersen3843e961999-11-25 07:30:46 +0000134 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000135}
136
Erik Andersenf13df372000-04-18 23:51:51 +0000137static void logMessage (int pri, char *msg)
Eric Andersen3843e961999-11-25 07:30:46 +0000138{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000139 time_t now;
140 char *timestamp;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000141 static char res[20] = "";
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142 CODE *c_pri, *c_fac;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000143
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000144 if (pri != 0) {
145 for (c_fac = facilitynames;
146 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
147 for (c_pri = prioritynames;
148 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
149 if (*c_fac->c_name == '\0' || *c_pri->c_name == '\0')
150 snprintf(res, sizeof(res), "<%d>", pri);
151 else
152 snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
153 }
Eric Andersen3843e961999-11-25 07:30:46 +0000154
Erik Andersene49d5ec2000-02-08 19:58:47 +0000155 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
156 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
157 time(&now);
158 timestamp = ctime(&now) + 4;
159 timestamp[15] = '\0';
160 } else {
161 timestamp = msg;
162 timestamp[15] = '\0';
163 msg += 16;
164 }
Eric Andersen3843e961999-11-25 07:30:46 +0000165
Erik Andersene49d5ec2000-02-08 19:58:47 +0000166 /* todo: supress duplicates */
Eric Andersen3843e961999-11-25 07:30:46 +0000167
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168 /* now spew out the message to wherever it is supposed to go */
169 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
Eric Andersen3843e961999-11-25 07:30:46 +0000170}
171
172static void quit_signal(int sig)
173{
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000174 logMessage(0, "System log daemon exiting.");
Erik Andersen983b51b2000-04-04 18:14:25 +0000175 unlink(lfile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000176 exit(TRUE);
Eric Andersen3843e961999-11-25 07:30:46 +0000177}
178
Eric Andersen3843e961999-11-25 07:30:46 +0000179static void domark(int sig)
180{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181 if (MarkInterval > 0) {
182 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
183 alarm(MarkInterval);
184 }
Eric Andersen3843e961999-11-25 07:30:46 +0000185}
186
Pavel Roskinda10ec02000-06-07 21:08:25 +0000187#define BUFSIZE 1023
Eric Andersen67e32302000-06-19 17:48:02 +0000188static int serveConnection (int conn)
Pavel Roskinda10ec02000-06-07 21:08:25 +0000189{
190 char buf[ BUFSIZE + 1 ];
191 int n_read;
192
193 while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
194
195 int pri = (LOG_USER | LOG_NOTICE);
196 char line[ BUFSIZE + 1 ];
197 unsigned char c;
198
199 char *p = buf, *q = line;
200
201 buf[ n_read - 1 ] = '\0';
202
203 while (p && (c = *p) && q < &line[ sizeof (line) - 1 ]) {
204 if (c == '<') {
205 /* Parse the magic priority number. */
206 pri = 0;
207 while (isdigit (*(++p))) {
208 pri = 10 * pri + (*p - '0');
209 }
210 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
211 pri = (LOG_USER | LOG_NOTICE);
212 } else if (c == '\n') {
213 *q++ = ' ';
214 } else if (iscntrl (c) && (c < 0177)) {
215 *q++ = '^';
216 *q++ = c ^ 0100;
217 } else {
218 *q++ = c;
219 }
220 p++;
221 }
222 *q = '\0';
223 /* Now log it */
224 logMessage (pri, line);
225 }
Eric Andersen67e32302000-06-19 17:48:02 +0000226 return (0);
Pavel Roskinda10ec02000-06-07 21:08:25 +0000227}
228
Erik Andersen983b51b2000-04-04 18:14:25 +0000229static void doSyslogd (void) __attribute__ ((noreturn));
230static void doSyslogd (void)
Eric Andersen3843e961999-11-25 07:30:46 +0000231{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000232 struct sockaddr_un sunx;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000233 socklen_t addrLength;
234
Erik Andersenf13df372000-04-18 23:51:51 +0000235
Erik Andersen983b51b2000-04-04 18:14:25 +0000236 int sock_fd;
Erik Andersenf13df372000-04-18 23:51:51 +0000237 fd_set fds;
238
Erik Andersen4f3f7572000-04-28 00:18:56 +0000239 char lfile[BUFSIZ];
Eric Andersenb99df0f1999-11-24 09:04:33 +0000240
Erik Andersenf13df372000-04-18 23:51:51 +0000241 /* Set up signal handlers. */
Erik Andersen983b51b2000-04-04 18:14:25 +0000242 signal (SIGINT, quit_signal);
243 signal (SIGTERM, quit_signal);
244 signal (SIGQUIT, quit_signal);
245 signal (SIGHUP, SIG_IGN);
Erik Andersene3ed1562000-04-19 18:52:56 +0000246 signal (SIGCLD, SIG_IGN);
Erik Andersen983b51b2000-04-04 18:14:25 +0000247 signal (SIGALRM, domark);
248 alarm (MarkInterval);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000249
Erik Andersenf13df372000-04-18 23:51:51 +0000250 /* Create the syslog file so realpath() can work. */
251 close (open (_PATH_LOG, O_RDWR | O_CREAT, 0644));
252 if (realpath (_PATH_LOG, lfile) == NULL)
Eric Andersen86ab8a32000-06-02 03:21:42 +0000253 fatalError ("Could not resolv path to " _PATH_LOG ": %s\n", strerror (errno));
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000254
Erik Andersen983b51b2000-04-04 18:14:25 +0000255 unlink (lfile);
256
Erik Andersenf13df372000-04-18 23:51:51 +0000257 memset (&sunx, 0, sizeof (sunx));
Erik Andersen983b51b2000-04-04 18:14:25 +0000258 sunx.sun_family = AF_UNIX;
Erik Andersenf13df372000-04-18 23:51:51 +0000259 strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path));
260 if ((sock_fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
Eric Andersen86ab8a32000-06-02 03:21:42 +0000261 fatalError ("Couldn't obtain descriptor for socket " _PATH_LOG ": %s\n", strerror (errno));
Eric Andersenb99df0f1999-11-24 09:04:33 +0000262
Erik Andersen983b51b2000-04-04 18:14:25 +0000263 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
Erik Andersenf13df372000-04-18 23:51:51 +0000264 if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || (listen (sock_fd, 5)))
Eric Andersen86ab8a32000-06-02 03:21:42 +0000265 fatalError ("Could not connect to socket " _PATH_LOG ": %s\n", strerror (errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266
Erik Andersenf13df372000-04-18 23:51:51 +0000267 if (chmod (lfile, 0666) < 0)
Eric Andersen86ab8a32000-06-02 03:21:42 +0000268 fatalError ("Could not set permission on " _PATH_LOG ": %s\n", strerror (errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000269
Erik Andersenf13df372000-04-18 23:51:51 +0000270 FD_ZERO (&fds);
271 FD_SET (sock_fd, &fds);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272
Erik Andersen983b51b2000-04-04 18:14:25 +0000273 logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274
Erik Andersen983b51b2000-04-04 18:14:25 +0000275 for (;;) {
Erik Andersenf13df372000-04-18 23:51:51 +0000276
277 fd_set readfds;
278 int n_ready;
279 int fd;
280
281 memcpy (&readfds, &fds, sizeof (fds));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000282
Erik Andersen983b51b2000-04-04 18:14:25 +0000283 if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
284 if (errno == EINTR) continue; /* alarm may have happened. */
Erik Andersenf13df372000-04-18 23:51:51 +0000285 fatalError ("select error: %s\n", strerror (errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000286 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000287
Erik Andersenf13df372000-04-18 23:51:51 +0000288 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) {
Erik Andersen983b51b2000-04-04 18:14:25 +0000289 if (FD_ISSET (fd, &readfds)) {
Erik Andersene3ed1562000-04-19 18:52:56 +0000290
Erik Andersenf13df372000-04-18 23:51:51 +0000291 --n_ready;
Erik Andersene3ed1562000-04-19 18:52:56 +0000292
Erik Andersen983b51b2000-04-04 18:14:25 +0000293 if (fd == sock_fd) {
Erik Andersene3ed1562000-04-19 18:52:56 +0000294
295 int conn;
296 pid_t pid;
297
Erik Andersenf13df372000-04-18 23:51:51 +0000298 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) {
299 fatalError ("accept error: %s\n", strerror (errno));
Erik Andersen983b51b2000-04-04 18:14:25 +0000300 }
Erik Andersena6c75222000-04-18 00:00:52 +0000301
Erik Andersene3ed1562000-04-19 18:52:56 +0000302 pid = fork();
Erik Andersen983b51b2000-04-04 18:14:25 +0000303
Erik Andersene3ed1562000-04-19 18:52:56 +0000304 if (pid < 0) {
305 perror ("syslogd: fork");
306 close (conn);
307 continue;
Erik Andersen983b51b2000-04-04 18:14:25 +0000308 }
Erik Andersene3ed1562000-04-19 18:52:56 +0000309
Pavel Roskinda10ec02000-06-07 21:08:25 +0000310 if (pid == 0)
311 serveConnection (conn);
Erik Andersene3ed1562000-04-19 18:52:56 +0000312 close (conn);
Erik Andersen983b51b2000-04-04 18:14:25 +0000313 }
314 }
315 }
316 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000317}
318
Eric Andersen03f4c272000-07-06 23:10:29 +0000319#ifdef BB_FEATURE_KLOGD
Eric Andersen2cb55071999-12-10 08:25:07 +0000320
Eric Andersenb186d981999-12-03 09:19:54 +0000321static void klogd_signal(int sig)
322{
Eric Andersen67e32302000-06-19 17:48:02 +0000323 klogctl(7, NULL, 0);
324 klogctl(0, 0, 0);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000325 logMessage(0, "Kernel log daemon exiting.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326 exit(TRUE);
Eric Andersenb186d981999-12-03 09:19:54 +0000327}
328
Erik Andersen983b51b2000-04-04 18:14:25 +0000329static void doKlogd (void) __attribute__ ((noreturn));
330static void doKlogd (void)
Eric Andersenb186d981999-12-03 09:19:54 +0000331{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000332 int priority = LOG_INFO;
333 char log_buffer[4096];
334 char *logp;
Eric Andersenb186d981999-12-03 09:19:54 +0000335
Erik Andersene49d5ec2000-02-08 19:58:47 +0000336 /* Set up sig handlers */
337 signal(SIGINT, klogd_signal);
338 signal(SIGKILL, klogd_signal);
339 signal(SIGTERM, klogd_signal);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000340 signal(SIGHUP, SIG_IGN);
341 logMessage(0, "klogd started: "
Erik Andersene49d5ec2000-02-08 19:58:47 +0000342 "BusyBox v" BB_VER " (" BB_BT ")");
Eric Andersenb186d981999-12-03 09:19:54 +0000343
Eric Andersen67e32302000-06-19 17:48:02 +0000344 klogctl(1, NULL, 0);
Eric Andersen0ecb54a1999-12-05 23:24:55 +0000345
Erik Andersene49d5ec2000-02-08 19:58:47 +0000346 while (1) {
347 /* Use kernel syscalls */
348 memset(log_buffer, '\0', sizeof(log_buffer));
Eric Andersen67e32302000-06-19 17:48:02 +0000349 if (klogctl(2, log_buffer, sizeof(log_buffer)) < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000350 char message[80];
351
352 if (errno == EINTR)
353 continue;
354 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
355 "%d - %s.\n", errno, strerror(errno));
356 logMessage(LOG_SYSLOG | LOG_ERR, message);
357 exit(1);
358 }
359 logp = log_buffer;
360 if (*log_buffer == '<') {
361 switch (*(log_buffer + 1)) {
362 case '0':
363 priority = LOG_EMERG;
364 break;
365 case '1':
366 priority = LOG_ALERT;
367 break;
368 case '2':
369 priority = LOG_CRIT;
370 break;
371 case '3':
372 priority = LOG_ERR;
373 break;
374 case '4':
375 priority = LOG_WARNING;
376 break;
377 case '5':
378 priority = LOG_NOTICE;
379 break;
380 case '6':
381 priority = LOG_INFO;
382 break;
383 case '7':
384 default:
385 priority = LOG_DEBUG;
386 }
387 logp += 3;
388 }
389 logMessage(LOG_KERN | priority, logp);
Eric Andersenb186d981999-12-03 09:19:54 +0000390 }
Eric Andersenb186d981999-12-03 09:19:54 +0000391
392}
393
Eric Andersen2cb55071999-12-10 08:25:07 +0000394#endif
Eric Andersenb99df0f1999-11-24 09:04:33 +0000395
Erik Andersen983b51b2000-04-04 18:14:25 +0000396static void daemon_init (char **argv, char *dz, void fn (void))
397{
398 setsid();
399 chdir ("/");
400 strncpy(argv[0], dz, strlen(argv[0]));
401 fn();
402 exit(0);
403}
404
Eric Andersen3843e961999-11-25 07:30:46 +0000405extern int syslogd_main(int argc, char **argv)
406{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000407 int pid, klogd_pid;
408 int doFork = TRUE;
409
Eric Andersen03f4c272000-07-06 23:10:29 +0000410#ifdef BB_FEATURE_KLOGD
Erik Andersene49d5ec2000-02-08 19:58:47 +0000411 int startKlogd = TRUE;
Eric Andersen2cb55071999-12-10 08:25:07 +0000412#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000413 int stopDoingThat = FALSE;
414 char *p;
415 char **argv1 = argv;
416
417 while (--argc > 0 && **(++argv1) == '-') {
418 stopDoingThat = FALSE;
419 while (stopDoingThat == FALSE && *(++(*argv1))) {
420 switch (**argv1) {
421 case 'm':
422 if (--argc == 0) {
423 usage(syslogd_usage);
424 }
425 MarkInterval = atoi(*(++argv1)) * 60;
426 break;
427 case 'n':
428 doFork = FALSE;
429 break;
Eric Andersen03f4c272000-07-06 23:10:29 +0000430#ifdef BB_FEATURE_KLOGD
Erik Andersene49d5ec2000-02-08 19:58:47 +0000431 case 'K':
432 startKlogd = FALSE;
433 break;
434#endif
435 case 'O':
436 if (--argc == 0) {
437 usage(syslogd_usage);
438 }
439 logFilePath = *(++argv1);
440 stopDoingThat = TRUE;
441 break;
442 default:
443 usage(syslogd_usage);
444 }
Eric Andersen3843e961999-11-25 07:30:46 +0000445 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000446 }
447
Pavel Roskinda10ec02000-06-07 21:08:25 +0000448 if (argc > 0)
449 usage(syslogd_usage);
450
Erik Andersene49d5ec2000-02-08 19:58:47 +0000451 /* Store away localhost's name before the fork */
452 gethostname(LocalHostName, sizeof(LocalHostName));
453 if ((p = strchr(LocalHostName, '.'))) {
454 *p++ = '\0';
455 }
456
Erik Andersen983b51b2000-04-04 18:14:25 +0000457 umask(0);
458
Eric Andersen03f4c272000-07-06 23:10:29 +0000459#ifdef BB_FEATURE_KLOGD
Erik Andersene2729152000-02-18 21:34:17 +0000460 /* Start up the klogd process */
461 if (startKlogd == TRUE) {
462 klogd_pid = fork();
463 if (klogd_pid == 0) {
Erik Andersen983b51b2000-04-04 18:14:25 +0000464 daemon_init (argv, "klogd", doKlogd);
Erik Andersene2729152000-02-18 21:34:17 +0000465 }
466 }
467#endif
468
Erik Andersene49d5ec2000-02-08 19:58:47 +0000469 if (doFork == TRUE) {
470 pid = fork();
471 if (pid < 0)
472 exit(pid);
473 else if (pid == 0) {
Erik Andersen983b51b2000-04-04 18:14:25 +0000474 daemon_init (argv, "syslogd", doSyslogd);
Eric Andersen3843e961999-11-25 07:30:46 +0000475 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000476 } else {
477 doSyslogd();
Eric Andersen3843e961999-11-25 07:30:46 +0000478 }
Eric Andersenb186d981999-12-03 09:19:54 +0000479
Eric Andersen67e32302000-06-19 17:48:02 +0000480 return(TRUE);
Eric Andersen3843e961999-11-25 07:30:46 +0000481}
Erik Andersen983b51b2000-04-04 18:14:25 +0000482
483/*
Erik Andersene3ed1562000-04-19 18:52:56 +0000484Local Variables
485c-file-style: "linux"
486c-basic-offset: 4
487tab-width: 4
488End:
489*/