blob: 55fd8dc740c90b3a255a295a2f5526cf0521646e [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 Andersenb186d981999-12-03 09:19:54 +000043
Eric Andersen999bf722000-07-09 06:59:58 +000044#if ! defined __GLIBC__ && ! defined __UCLIBC__
Eric Andersen67e32302000-06-19 17:48:02 +000045
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
58
Eric Andersenb99df0f1999-11-24 09:04:33 +000059
60
Eric Andersen3843e961999-11-25 07:30:46 +000061/* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
62#define SYSLOG_NAMES
63#include <sys/syslog.h>
64
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
79static const char syslogd_usage[] =
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000080 "syslogd [OPTION]...\n"
81#ifndef BB_FEATURE_TRIVIAL_HELP
82 "\nLinux system and kernel (provides klogd) logging utility.\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n"
84 "Options:\n"
Pavel Roskinda10ec02000-06-07 21:08:25 +000085 "\t-m NUM\t\tInterval between MARK lines (default=20min, 0=off)\n"
86 "\t-n\t\tRun as a foreground process\n"
Eric Andersen03f4c272000-07-06 23:10:29 +000087#ifdef BB_FEATURE_KLOGD
Pavel Roskinda10ec02000-06-07 21:08:25 +000088 "\t-K\t\tDo not start up the klogd process\n"
Eric Andersen2cb55071999-12-10 08:25:07 +000089#endif
Pavel Roskinda10ec02000-06-07 21:08:25 +000090 "\t-O FILE\t\tUse an alternate log file (default=/var/log/messages)\n"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000091#endif
92 ;
Eric Andersen3843e961999-11-25 07:30:46 +000093
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;
145 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
146 for (c_pri = prioritynames;
147 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
148 if (*c_fac->c_name == '\0' || *c_pri->c_name == '\0')
149 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] != ' ' ||
155 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
156 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
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167 /* now spew out the message to wherever it is supposed to go */
168 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
Eric Andersen3843e961999-11-25 07:30:46 +0000169}
170
171static void quit_signal(int sig)
172{
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000173 logMessage(0, "System log daemon exiting.");
Erik Andersen983b51b2000-04-04 18:14:25 +0000174 unlink(lfile);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175 exit(TRUE);
Eric Andersen3843e961999-11-25 07:30:46 +0000176}
177
Eric Andersen3843e961999-11-25 07:30:46 +0000178static void domark(int sig)
179{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000180 if (MarkInterval > 0) {
181 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
182 alarm(MarkInterval);
183 }
Eric Andersen3843e961999-11-25 07:30:46 +0000184}
185
Pavel Roskinda10ec02000-06-07 21:08:25 +0000186#define BUFSIZE 1023
Eric Andersen67e32302000-06-19 17:48:02 +0000187static int serveConnection (int conn)
Pavel Roskinda10ec02000-06-07 21:08:25 +0000188{
189 char buf[ BUFSIZE + 1 ];
190 int n_read;
191
192 while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
193
194 int pri = (LOG_USER | LOG_NOTICE);
195 char line[ BUFSIZE + 1 ];
196 unsigned char c;
197
198 char *p = buf, *q = line;
199
200 buf[ n_read - 1 ] = '\0';
201
202 while (p && (c = *p) && q < &line[ sizeof (line) - 1 ]) {
203 if (c == '<') {
204 /* Parse the magic priority number. */
205 pri = 0;
206 while (isdigit (*(++p))) {
207 pri = 10 * pri + (*p - '0');
208 }
209 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
210 pri = (LOG_USER | LOG_NOTICE);
211 } else if (c == '\n') {
212 *q++ = ' ';
213 } else if (iscntrl (c) && (c < 0177)) {
214 *q++ = '^';
215 *q++ = c ^ 0100;
216 } else {
217 *q++ = c;
218 }
219 p++;
220 }
221 *q = '\0';
222 /* Now log it */
223 logMessage (pri, line);
224 }
Eric Andersen67e32302000-06-19 17:48:02 +0000225 return (0);
Pavel Roskinda10ec02000-06-07 21:08:25 +0000226}
227
Erik Andersen983b51b2000-04-04 18:14:25 +0000228static void doSyslogd (void) __attribute__ ((noreturn));
229static void doSyslogd (void)
Eric Andersen3843e961999-11-25 07:30:46 +0000230{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231 struct sockaddr_un sunx;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000232 socklen_t addrLength;
233
Erik Andersenf13df372000-04-18 23:51:51 +0000234
Erik Andersen983b51b2000-04-04 18:14:25 +0000235 int sock_fd;
Erik Andersenf13df372000-04-18 23:51:51 +0000236 fd_set fds;
237
Erik Andersen4f3f7572000-04-28 00:18:56 +0000238 char lfile[BUFSIZ];
Eric Andersenb99df0f1999-11-24 09:04:33 +0000239
Erik Andersenf13df372000-04-18 23:51:51 +0000240 /* Set up signal handlers. */
Erik Andersen983b51b2000-04-04 18:14:25 +0000241 signal (SIGINT, quit_signal);
242 signal (SIGTERM, quit_signal);
243 signal (SIGQUIT, quit_signal);
244 signal (SIGHUP, SIG_IGN);
Erik Andersene3ed1562000-04-19 18:52:56 +0000245 signal (SIGCLD, SIG_IGN);
Erik Andersen983b51b2000-04-04 18:14:25 +0000246 signal (SIGALRM, domark);
247 alarm (MarkInterval);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000248
Erik Andersenf13df372000-04-18 23:51:51 +0000249 /* Create the syslog file so realpath() can work. */
250 close (open (_PATH_LOG, O_RDWR | O_CREAT, 0644));
251 if (realpath (_PATH_LOG, lfile) == NULL)
Eric Andersen86ab8a32000-06-02 03:21:42 +0000252 fatalError ("Could not resolv path to " _PATH_LOG ": %s\n", strerror (errno));
Eric Andersen14ec6cf1999-12-05 22:17:02 +0000253
Erik Andersen983b51b2000-04-04 18:14:25 +0000254 unlink (lfile);
255
Erik Andersenf13df372000-04-18 23:51:51 +0000256 memset (&sunx, 0, sizeof (sunx));
Erik Andersen983b51b2000-04-04 18:14:25 +0000257 sunx.sun_family = AF_UNIX;
Erik Andersenf13df372000-04-18 23:51:51 +0000258 strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path));
259 if ((sock_fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
Eric Andersen86ab8a32000-06-02 03:21:42 +0000260 fatalError ("Couldn't obtain descriptor for socket " _PATH_LOG ": %s\n", strerror (errno));
Eric Andersenb99df0f1999-11-24 09:04:33 +0000261
Erik Andersen983b51b2000-04-04 18:14:25 +0000262 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
Erik Andersenf13df372000-04-18 23:51:51 +0000263 if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || (listen (sock_fd, 5)))
Eric Andersen86ab8a32000-06-02 03:21:42 +0000264 fatalError ("Could not connect to socket " _PATH_LOG ": %s\n", strerror (errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265
Erik Andersenf13df372000-04-18 23:51:51 +0000266 if (chmod (lfile, 0666) < 0)
Eric Andersen86ab8a32000-06-02 03:21:42 +0000267 fatalError ("Could not set permission on " _PATH_LOG ": %s\n", strerror (errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268
Erik Andersenf13df372000-04-18 23:51:51 +0000269 FD_ZERO (&fds);
270 FD_SET (sock_fd, &fds);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000271
Erik Andersen983b51b2000-04-04 18:14:25 +0000272 logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273
Erik Andersen983b51b2000-04-04 18:14:25 +0000274 for (;;) {
Erik Andersenf13df372000-04-18 23:51:51 +0000275
276 fd_set readfds;
277 int n_ready;
278 int fd;
279
280 memcpy (&readfds, &fds, sizeof (fds));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000281
Erik Andersen983b51b2000-04-04 18:14:25 +0000282 if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
283 if (errno == EINTR) continue; /* alarm may have happened. */
Erik Andersenf13df372000-04-18 23:51:51 +0000284 fatalError ("select error: %s\n", strerror (errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000285 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000286
Erik Andersenf13df372000-04-18 23:51:51 +0000287 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) {
Erik Andersen983b51b2000-04-04 18:14:25 +0000288 if (FD_ISSET (fd, &readfds)) {
Erik Andersene3ed1562000-04-19 18:52:56 +0000289
Erik Andersenf13df372000-04-18 23:51:51 +0000290 --n_ready;
Erik Andersene3ed1562000-04-19 18:52:56 +0000291
Erik Andersen983b51b2000-04-04 18:14:25 +0000292 if (fd == sock_fd) {
Erik Andersene3ed1562000-04-19 18:52:56 +0000293
294 int conn;
295 pid_t pid;
296
Erik Andersenf13df372000-04-18 23:51:51 +0000297 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) {
298 fatalError ("accept error: %s\n", strerror (errno));
Erik Andersen983b51b2000-04-04 18:14:25 +0000299 }
Erik Andersena6c75222000-04-18 00:00:52 +0000300
Erik Andersene3ed1562000-04-19 18:52:56 +0000301 pid = fork();
Erik Andersen983b51b2000-04-04 18:14:25 +0000302
Erik Andersene3ed1562000-04-19 18:52:56 +0000303 if (pid < 0) {
304 perror ("syslogd: fork");
305 close (conn);
306 continue;
Erik Andersen983b51b2000-04-04 18:14:25 +0000307 }
Erik Andersene3ed1562000-04-19 18:52:56 +0000308
Pavel Roskinda10ec02000-06-07 21:08:25 +0000309 if (pid == 0)
310 serveConnection (conn);
Erik Andersene3ed1562000-04-19 18:52:56 +0000311 close (conn);
Erik Andersen983b51b2000-04-04 18:14:25 +0000312 }
313 }
314 }
315 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000316}
317
Eric Andersen03f4c272000-07-06 23:10:29 +0000318#ifdef BB_FEATURE_KLOGD
Eric Andersen2cb55071999-12-10 08:25:07 +0000319
Eric Andersenb186d981999-12-03 09:19:54 +0000320static void klogd_signal(int sig)
321{
Eric Andersen67e32302000-06-19 17:48:02 +0000322 klogctl(7, NULL, 0);
323 klogctl(0, 0, 0);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000324 logMessage(0, "Kernel log daemon exiting.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000325 exit(TRUE);
Eric Andersenb186d981999-12-03 09:19:54 +0000326}
327
Erik Andersen983b51b2000-04-04 18:14:25 +0000328static void doKlogd (void) __attribute__ ((noreturn));
329static void doKlogd (void)
Eric Andersenb186d981999-12-03 09:19:54 +0000330{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 int priority = LOG_INFO;
332 char log_buffer[4096];
333 char *logp;
Eric Andersenb186d981999-12-03 09:19:54 +0000334
Erik Andersene49d5ec2000-02-08 19:58:47 +0000335 /* Set up sig handlers */
336 signal(SIGINT, klogd_signal);
337 signal(SIGKILL, klogd_signal);
338 signal(SIGTERM, klogd_signal);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000339 signal(SIGHUP, SIG_IGN);
340 logMessage(0, "klogd started: "
Erik Andersene49d5ec2000-02-08 19:58:47 +0000341 "BusyBox v" BB_VER " (" BB_BT ")");
Eric Andersenb186d981999-12-03 09:19:54 +0000342
Eric Andersen67e32302000-06-19 17:48:02 +0000343 klogctl(1, NULL, 0);
Eric Andersen0ecb54a1999-12-05 23:24:55 +0000344
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345 while (1) {
346 /* Use kernel syscalls */
347 memset(log_buffer, '\0', sizeof(log_buffer));
Eric Andersen67e32302000-06-19 17:48:02 +0000348 if (klogctl(2, log_buffer, sizeof(log_buffer)) < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000349 char message[80];
350
351 if (errno == EINTR)
352 continue;
353 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
354 "%d - %s.\n", errno, strerror(errno));
355 logMessage(LOG_SYSLOG | LOG_ERR, message);
356 exit(1);
357 }
358 logp = log_buffer;
359 if (*log_buffer == '<') {
360 switch (*(log_buffer + 1)) {
361 case '0':
362 priority = LOG_EMERG;
363 break;
364 case '1':
365 priority = LOG_ALERT;
366 break;
367 case '2':
368 priority = LOG_CRIT;
369 break;
370 case '3':
371 priority = LOG_ERR;
372 break;
373 case '4':
374 priority = LOG_WARNING;
375 break;
376 case '5':
377 priority = LOG_NOTICE;
378 break;
379 case '6':
380 priority = LOG_INFO;
381 break;
382 case '7':
383 default:
384 priority = LOG_DEBUG;
385 }
386 logp += 3;
387 }
388 logMessage(LOG_KERN | priority, logp);
Eric Andersenb186d981999-12-03 09:19:54 +0000389 }
Eric Andersenb186d981999-12-03 09:19:54 +0000390
391}
392
Eric Andersen2cb55071999-12-10 08:25:07 +0000393#endif
Eric Andersenb99df0f1999-11-24 09:04:33 +0000394
Erik Andersen983b51b2000-04-04 18:14:25 +0000395static void daemon_init (char **argv, char *dz, void fn (void))
396{
397 setsid();
398 chdir ("/");
399 strncpy(argv[0], dz, strlen(argv[0]));
400 fn();
401 exit(0);
402}
403
Eric Andersen3843e961999-11-25 07:30:46 +0000404extern int syslogd_main(int argc, char **argv)
405{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406 int pid, klogd_pid;
407 int doFork = TRUE;
408
Eric Andersen03f4c272000-07-06 23:10:29 +0000409#ifdef BB_FEATURE_KLOGD
Erik Andersene49d5ec2000-02-08 19:58:47 +0000410 int startKlogd = TRUE;
Eric Andersen2cb55071999-12-10 08:25:07 +0000411#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000412 int stopDoingThat = FALSE;
413 char *p;
414 char **argv1 = argv;
415
416 while (--argc > 0 && **(++argv1) == '-') {
417 stopDoingThat = FALSE;
418 while (stopDoingThat == FALSE && *(++(*argv1))) {
419 switch (**argv1) {
420 case 'm':
421 if (--argc == 0) {
422 usage(syslogd_usage);
423 }
424 MarkInterval = atoi(*(++argv1)) * 60;
425 break;
426 case 'n':
427 doFork = FALSE;
428 break;
Eric Andersen03f4c272000-07-06 23:10:29 +0000429#ifdef BB_FEATURE_KLOGD
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430 case 'K':
431 startKlogd = FALSE;
432 break;
433#endif
434 case 'O':
435 if (--argc == 0) {
436 usage(syslogd_usage);
437 }
438 logFilePath = *(++argv1);
439 stopDoingThat = TRUE;
440 break;
441 default:
442 usage(syslogd_usage);
443 }
Eric Andersen3843e961999-11-25 07:30:46 +0000444 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000445 }
446
Pavel Roskinda10ec02000-06-07 21:08:25 +0000447 if (argc > 0)
448 usage(syslogd_usage);
449
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 /* Store away localhost's name before the fork */
451 gethostname(LocalHostName, sizeof(LocalHostName));
452 if ((p = strchr(LocalHostName, '.'))) {
453 *p++ = '\0';
454 }
455
Erik Andersen983b51b2000-04-04 18:14:25 +0000456 umask(0);
457
Eric Andersen03f4c272000-07-06 23:10:29 +0000458#ifdef BB_FEATURE_KLOGD
Erik Andersene2729152000-02-18 21:34:17 +0000459 /* Start up the klogd process */
460 if (startKlogd == TRUE) {
461 klogd_pid = fork();
462 if (klogd_pid == 0) {
Erik Andersen983b51b2000-04-04 18:14:25 +0000463 daemon_init (argv, "klogd", doKlogd);
Erik Andersene2729152000-02-18 21:34:17 +0000464 }
465 }
466#endif
467
Erik Andersene49d5ec2000-02-08 19:58:47 +0000468 if (doFork == TRUE) {
469 pid = fork();
470 if (pid < 0)
471 exit(pid);
472 else if (pid == 0) {
Erik Andersen983b51b2000-04-04 18:14:25 +0000473 daemon_init (argv, "syslogd", doSyslogd);
Eric Andersen3843e961999-11-25 07:30:46 +0000474 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000475 } else {
476 doSyslogd();
Eric Andersen3843e961999-11-25 07:30:46 +0000477 }
Eric Andersenb186d981999-12-03 09:19:54 +0000478
Eric Andersen67e32302000-06-19 17:48:02 +0000479 return(TRUE);
Eric Andersen3843e961999-11-25 07:30:46 +0000480}
Erik Andersen983b51b2000-04-04 18:14:25 +0000481
482/*
Erik Andersene3ed1562000-04-19 18:52:56 +0000483Local Variables
484c-file-style: "linux"
485c-basic-offset: 4
486tab-width: 4
487End:
488*/