blob: 754fa44b3ad8029915bd739b65cfc0bb0c4594d9 [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 Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen3843e961999-11-25 07:30:46 +00006 *
Erik Andersenf13df372000-04-18 23:51:51 +00007 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
8 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +00009 * "circular buffer" Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>
Mark Whitley6317c4b2001-03-12 22:51:50 +000010 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +000011 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
Mark Whitley6bff9cc2001-03-12 23:41:34 +000012 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000013 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen3843e961999-11-25 07:30:46 +000014 */
Eric Andersenb99df0f1999-11-24 09:04:33 +000015
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +000016/*
17 * Done in syslogd_and_logger.c:
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000019#define SYSLOG_NAMES
20#define SYSLOG_NAMES_CONST
21#include <syslog.h>
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +000022*/
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000023
Erik Andersen983b51b2000-04-04 18:14:25 +000024#include <sys/un.h>
Eric Andersenced2cef2000-07-20 23:41:24 +000025#include <sys/uio.h>
Eric Andersen3843e961999-11-25 07:30:46 +000026
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000027#if ENABLE_FEATURE_REMOTE_LOG
28#include <netinet/in.h>
Denis Vlasenko4998c812007-02-14 20:51:46 +000029#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000030
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000031#if ENABLE_FEATURE_IPC_SYSLOG
32#include <sys/ipc.h>
33#include <sys/sem.h>
34#include <sys/shm.h>
35#endif
Denis Vlasenko1decd0e2006-09-30 19:17:40 +000036
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000037
38#define DEBUG 0
39
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000040/* MARK code is not very useful, is bloat, and broken:
41 * can deadlock if alarmed to make MARK while writing to IPC buffer
42 * (semaphores are down but do_mark routine tries to down them again) */
43#undef SYSLOGD_MARK
44
Denis Vlasenko0d948202008-12-09 22:53:31 +000045/* Write locking does not seem to be useful either */
46#undef SYSLOGD_WRLOCK
47
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +000048enum {
49 MAX_READ = 256,
50 DNS_WAIT_SEC = 2 * 60,
51};
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000052
53/* Semaphore operation structures */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000054struct shbuf_ds {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000055 int32_t size; /* size of data - 1 */
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000056 int32_t tail; /* end of message list */
57 char data[1]; /* data/messages */
58};
59
60/* Allows us to have smaller initializer. Ugly. */
61#define GLOBALS \
62 const char *logFilePath; \
63 int logFD; \
64 /* interval between marks in seconds */ \
65 /*int markInterval;*/ \
66 /* level of messages to be logged */ \
67 int logLevel; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000068IF_FEATURE_ROTATE_LOGFILE( \
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000069 /* max size of file before rotation */ \
70 unsigned logFileSize; \
71 /* number of rotated message files */ \
72 unsigned logFileRotate; \
73 unsigned curFileSize; \
74 smallint isRegular; \
75) \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000076IF_FEATURE_REMOTE_LOG( \
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000077 /* udp socket for remote logging */ \
78 int remoteFD; \
79 len_and_sockaddr* remoteAddr; \
80) \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000081IF_FEATURE_IPC_SYSLOG( \
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000082 int shmid; /* ipc shared memory id */ \
83 int s_semid; /* ipc semaphore id */ \
84 int shm_size; \
85 struct sembuf SMwup[1]; \
86 struct sembuf SMwdn[3]; \
87)
88
89struct init_globals {
90 GLOBALS
91};
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000092
93struct globals {
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000094 GLOBALS
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +000095
96#if ENABLE_FEATURE_REMOTE_LOG
97 unsigned last_dns_resolve;
98 char *remoteAddrStr;
99#endif
100
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000101#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000102 struct shbuf_ds *shbuf;
103#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000104 time_t last_log_time;
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000105 /* localhost's name. We print only first 64 chars */
106 char *hostname;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000107
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000108 /* We recv into recvbuf... */
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000109 char recvbuf[MAX_READ * (1 + ENABLE_FEATURE_SYSLOGD_DUP)];
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000110 /* ...then copy to parsebuf, escaping control chars */
111 /* (can grow x2 max) */
112 char parsebuf[MAX_READ*2];
113 /* ...then sprintf into printbuf, adding timestamp (15 chars),
114 * host (64), fac.prio (20) to the message */
115 /* (growth by: 15 + 64 + 20 + delims = ~110) */
116 char printbuf[MAX_READ*2 + 128];
117};
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000118
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000119static const struct init_globals init_data = {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000120 .logFilePath = "/var/log/messages",
121 .logFD = -1,
122#ifdef SYSLOGD_MARK
123 .markInterval = 20 * 60,
124#endif
125 .logLevel = 8,
126#if ENABLE_FEATURE_ROTATE_LOGFILE
127 .logFileSize = 200 * 1024,
128 .logFileRotate = 1,
129#endif
130#if ENABLE_FEATURE_REMOTE_LOG
131 .remoteFD = -1,
132#endif
133#if ENABLE_FEATURE_IPC_SYSLOG
134 .shmid = -1,
135 .s_semid = -1,
136 .shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024), // default shm size
137 .SMwup = { {1, -1, IPC_NOWAIT} },
138 .SMwdn = { {0, 0}, {1, 0}, {1, +1} },
139#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000140};
141
142#define G (*ptr_to_globals)
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000143#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000144 SET_PTR_TO_GLOBALS(memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data))); \
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000145} while (0)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000146
147
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000148/* Options */
Denis Vlasenko14c19402006-09-30 19:20:00 +0000149enum {
150 OPTBIT_mark = 0, // -m
151 OPTBIT_nofork, // -n
152 OPTBIT_outfile, // -O
153 OPTBIT_loglevel, // -l
154 OPTBIT_small, // -S
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000155 IF_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
156 IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
157 IF_FEATURE_REMOTE_LOG( OPTBIT_remotelog ,) // -R
158 IF_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L
159 IF_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
160 IF_FEATURE_SYSLOGD_DUP( OPTBIT_dup ,) // -D
Denis Vlasenko14c19402006-09-30 19:20:00 +0000161
162 OPT_mark = 1 << OPTBIT_mark ,
163 OPT_nofork = 1 << OPTBIT_nofork ,
164 OPT_outfile = 1 << OPTBIT_outfile ,
165 OPT_loglevel = 1 << OPTBIT_loglevel,
166 OPT_small = 1 << OPTBIT_small ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000167 OPT_filesize = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
168 OPT_rotatecnt = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
169 OPT_remotelog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_remotelog )) + 0,
170 OPT_locallog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0,
171 OPT_circularlog = IF_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
172 OPT_dup = IF_FEATURE_SYSLOGD_DUP( (1 << OPTBIT_dup )) + 0,
Denis Vlasenko14c19402006-09-30 19:20:00 +0000173};
174#define OPTION_STR "m:nO:l:S" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000175 IF_FEATURE_ROTATE_LOGFILE("s:" ) \
176 IF_FEATURE_ROTATE_LOGFILE("b:" ) \
177 IF_FEATURE_REMOTE_LOG( "R:" ) \
178 IF_FEATURE_REMOTE_LOG( "L" ) \
179 IF_FEATURE_IPC_SYSLOG( "C::") \
180 IF_FEATURE_SYSLOGD_DUP( "D" )
Denis Vlasenko14c19402006-09-30 19:20:00 +0000181#define OPTION_DECL *opt_m, *opt_l \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000182 IF_FEATURE_ROTATE_LOGFILE(,*opt_s) \
183 IF_FEATURE_ROTATE_LOGFILE(,*opt_b) \
184 IF_FEATURE_IPC_SYSLOG( ,*opt_C = NULL)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000185#define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000186 IF_FEATURE_ROTATE_LOGFILE(,&opt_s) \
187 IF_FEATURE_ROTATE_LOGFILE(,&opt_b) \
188 IF_FEATURE_REMOTE_LOG( ,&G.remoteAddrStr) \
189 IF_FEATURE_IPC_SYSLOG( ,&opt_C)
Eric Andersen871d93c2002-09-17 20:06:29 +0000190
Eric Andersen871d93c2002-09-17 20:06:29 +0000191
Mark Whitley6317c4b2001-03-12 22:51:50 +0000192/* circular buffer variables/structures */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000193#if ENABLE_FEATURE_IPC_SYSLOG
194
Eric Andersend4a5e252003-12-19 11:32:14 +0000195#if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4
196#error Sorry, you must set the syslogd buffer size to at least 4KB.
197#error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE
198#endif
199
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000200/* our shared key (syslogd.c and logread.c must be in sync) */
201enum { KEY_ID = 0x414e4547 }; /* "GENA" */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000202
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000203static void ipcsyslog_cleanup(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000204{
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000205 if (G.shmid != -1) {
206 shmdt(G.shbuf);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000207 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000208 if (G.shmid != -1) {
209 shmctl(G.shmid, IPC_RMID, NULL);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000210 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000211 if (G.s_semid != -1) {
212 semctl(G.s_semid, 0, IPC_RMID, 0);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000213 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000214}
215
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000216static void ipcsyslog_init(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000217{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000218 if (DEBUG)
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000219 printf("shmget(%x, %d,...)\n", (int)KEY_ID, G.shm_size);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000220
Denis Vlasenkoa1120a82007-08-14 10:27:56 +0000221 G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 0644);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000222 if (G.shmid == -1) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000223 bb_perror_msg_and_die("shmget");
224 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000225
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000226 G.shbuf = shmat(G.shmid, NULL, 0);
Denis Vlasenko4e9ca752008-01-07 15:58:02 +0000227 if (G.shbuf == (void*) -1L) { /* shmat has bizarre error return */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000228 bb_perror_msg_and_die("shmat");
229 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000230
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000231 memset(G.shbuf, 0, G.shm_size);
232 G.shbuf->size = G.shm_size - offsetof(struct shbuf_ds, data) - 1;
233 /*G.shbuf->tail = 0;*/
Mark Whitley6317c4b2001-03-12 22:51:50 +0000234
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000235 // we'll trust the OS to set initial semval to 0 (let's hope)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000236 G.s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023);
237 if (G.s_semid == -1) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000238 if (errno == EEXIST) {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000239 G.s_semid = semget(KEY_ID, 2, 0);
240 if (G.s_semid != -1)
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000241 return;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000242 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000243 bb_perror_msg_and_die("semget");
Mark Whitley6317c4b2001-03-12 22:51:50 +0000244 }
245}
246
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000247/* Write message to shared mem buffer */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000248static void log_to_shmem(const char *msg, int len)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000249{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000250 int old_tail, new_tail;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000251
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000252 if (semop(G.s_semid, G.SMwdn, 3) == -1) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000253 bb_perror_msg_and_die("SMwdn");
254 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000255
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000256 /* Circular Buffer Algorithm:
Mark Whitley6317c4b2001-03-12 22:51:50 +0000257 * --------------------------
Denis Vlasenko150f4022007-01-13 21:06:21 +0000258 * tail == position where to store next syslog message.
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000259 * tail's max value is (shbuf->size - 1)
260 * Last byte of buffer is never used and remains NUL.
Mark Whitley6317c4b2001-03-12 22:51:50 +0000261 */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000262 len++; /* length with NUL included */
263 again:
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000264 old_tail = G.shbuf->tail;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000265 new_tail = old_tail + len;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000266 if (new_tail < G.shbuf->size) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000267 /* store message, set new tail */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000268 memcpy(G.shbuf->data + old_tail, msg, len);
269 G.shbuf->tail = new_tail;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000270 } else {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000271 /* k == available buffer space ahead of old tail */
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000272 int k = G.shbuf->size - old_tail;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000273 /* copy what fits to the end of buffer, and repeat */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000274 memcpy(G.shbuf->data + old_tail, msg, k);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000275 msg += k;
276 len -= k;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000277 G.shbuf->tail = 0;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000278 goto again;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000279 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000280 if (semop(G.s_semid, G.SMwup, 1) == -1) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000281 bb_perror_msg_and_die("SMwup");
282 }
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000283 if (DEBUG)
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000284 printf("tail:%d\n", G.shbuf->tail);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000285}
Rob Landley028ba282006-08-28 20:16:42 +0000286#else
287void ipcsyslog_cleanup(void);
288void ipcsyslog_init(void);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000289void log_to_shmem(const char *msg);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000290#endif /* FEATURE_IPC_SYSLOG */
291
292
Erik Andersen983b51b2000-04-04 18:14:25 +0000293/* Print a message to the log file. */
Denis Vlasenko4dada742008-01-03 12:13:42 +0000294static void log_locally(time_t now, char *msg)
Eric Andersen3843e961999-11-25 07:30:46 +0000295{
Denis Vlasenko0d948202008-12-09 22:53:31 +0000296#ifdef SYSLOGD_WRLOCK
Denis Vlasenkob8934972007-01-04 18:02:32 +0000297 struct flock fl;
Denis Vlasenko0d948202008-12-09 22:53:31 +0000298#endif
Denis Vlasenkob8934972007-01-04 18:02:32 +0000299 int len = strlen(msg);
Eric Andersen3843e961999-11-25 07:30:46 +0000300
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000301#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000302 if ((option_mask32 & OPT_circularlog) && G.shbuf) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000303 log_to_shmem(msg, len);
304 return;
305 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000306#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000307 if (G.logFD >= 0) {
Denis Vlasenkod9415d62009-03-02 14:26:28 +0000308 /* Reopen log file every second. This allows admin
309 * to delete the file and not worry about restarting us.
310 * This costs almost nothing since it happens
311 * _at most_ once a second.
312 */
Denis Vlasenko4dada742008-01-03 12:13:42 +0000313 if (!now)
314 now = time(NULL);
315 if (G.last_log_time != now) {
Denis Vlasenkod9415d62009-03-02 14:26:28 +0000316 G.last_log_time = now;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000317 close(G.logFD);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000318 goto reopen;
319 }
320 } else {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000321 reopen:
Denis Vlasenkod9415d62009-03-02 14:26:28 +0000322 G.logFD = open(G.logFilePath, O_WRONLY | O_CREAT
323 | O_NOCTTY | O_APPEND | O_NONBLOCK,
324 0666);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000325 if (G.logFD < 0) {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000326 /* cannot open logfile? - print to /dev/console then */
Denis Vlasenko70ab28f2007-11-18 05:43:05 +0000327 int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000328 if (fd < 0)
329 fd = 2; /* then stderr, dammit */
330 full_write(fd, msg, len);
331 if (fd != 2)
332 close(fd);
333 return;
334 }
335#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenkobae79482007-01-09 23:42:43 +0000336 {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000337 struct stat statf;
338 G.isRegular = (fstat(G.logFD, &statf) == 0 && S_ISREG(statf.st_mode));
339 /* bug (mostly harmless): can wrap around if file > 4gb */
340 G.curFileSize = statf.st_size;
Denis Vlasenkobae79482007-01-09 23:42:43 +0000341 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000342#endif
343 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000344
Denis Vlasenko0d948202008-12-09 22:53:31 +0000345#ifdef SYSLOGD_WRLOCK
Denis Vlasenkob8934972007-01-04 18:02:32 +0000346 fl.l_whence = SEEK_SET;
347 fl.l_start = 0;
348 fl.l_len = 1;
349 fl.l_type = F_WRLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000350 fcntl(G.logFD, F_SETLKW, &fl);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000351#endif
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000352
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000353#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000354 if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) {
355 if (G.logFileRotate) { /* always 0..99 */
356 int i = strlen(G.logFilePath) + 3 + 1;
Denis Vlasenkob8934972007-01-04 18:02:32 +0000357 char oldFile[i];
358 char newFile[i];
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000359 i = G.logFileRotate - 1;
Denis Vlasenkob8934972007-01-04 18:02:32 +0000360 /* rename: f.8 -> f.9; f.7 -> f.8; ... */
361 while (1) {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000362 sprintf(newFile, "%s.%d", G.logFilePath, i);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000363 if (i == 0) break;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000364 sprintf(oldFile, "%s.%d", G.logFilePath, --i);
Denis Vlasenko69dc3252008-05-24 21:03:53 +0000365 /* ignore errors - file might be missing */
366 rename(oldFile, newFile);
Eric Andersen29c77f72003-10-09 09:43:18 +0000367 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000368 /* newFile == "f.0" now */
Denis Vlasenko69dc3252008-05-24 21:03:53 +0000369 rename(G.logFilePath, newFile);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000370#ifdef SYSLOGD_WRLOCK
Denis Vlasenkob8934972007-01-04 18:02:32 +0000371 fl.l_type = F_UNLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000372 fcntl(G.logFD, F_SETLKW, &fl);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000373#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000374 close(G.logFD);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000375 goto reopen;
Eric Andersen29c77f72003-10-09 09:43:18 +0000376 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000377 ftruncate(G.logFD, 0);
Eric Andersen3843e961999-11-25 07:30:46 +0000378 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000379 G.curFileSize +=
Denis Vlasenkob8934972007-01-04 18:02:32 +0000380#endif
Denis Vlasenko9504e442008-10-29 01:19:15 +0000381 full_write(G.logFD, msg, len);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000382#ifdef SYSLOGD_WRLOCK
Denis Vlasenkob8934972007-01-04 18:02:32 +0000383 fl.l_type = F_UNLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000384 fcntl(G.logFD, F_SETLKW, &fl);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000385#endif
Eric Andersenb99df0f1999-11-24 09:04:33 +0000386}
387
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000388static void parse_fac_prio_20(int pri, char *res20)
Eric Andersen3843e961999-11-25 07:30:46 +0000389{
Denis Vlasenko3c8b5ba2007-06-04 18:23:59 +0000390 const CODE *c_pri, *c_fac;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000391
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000392 if (pri != 0) {
Denis Vlasenko14c19402006-09-30 19:20:00 +0000393 c_fac = facilitynames;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000394 while (c_fac->c_name) {
395 if (c_fac->c_val != (LOG_FAC(pri) << 3)) {
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000396 c_fac++;
397 continue;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000398 }
399 /* facility is found, look for prio */
400 c_pri = prioritynames;
401 while (c_pri->c_name) {
402 if (c_pri->c_val != LOG_PRI(pri)) {
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000403 c_pri++;
404 continue;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000405 }
406 snprintf(res20, 20, "%s.%s",
407 c_fac->c_name, c_pri->c_name);
408 return;
409 }
410 /* prio not found, bail out */
411 break;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000412 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000413 snprintf(res20, 20, "<%d>", pri);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000414 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000415}
Eric Andersen3843e961999-11-25 07:30:46 +0000416
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000417/* len parameter is used only for "is there a timestamp?" check.
Denis Vlasenko018b1552007-11-06 01:38:46 +0000418 * NB: some callers cheat and supply len==0 when they know
419 * that there is no timestamp, short-circuiting the test. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000420static void timestamp_and_log(int pri, char *msg, int len)
421{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000422 char *timestamp;
Denis Vlasenko4dada742008-01-03 12:13:42 +0000423 time_t now;
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000424
Denis Vlasenko574c3162009-04-22 02:53:02 +0000425 /* Jan 18 00:11:22 msg... */
426 /* 01234567890123456 */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000427 if (len < 16 || msg[3] != ' ' || msg[6] != ' '
428 || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
429 ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430 time(&now);
Denis Vlasenko4dada742008-01-03 12:13:42 +0000431 timestamp = ctime(&now) + 4; /* skip day of week */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000432 } else {
Denis Vlasenko4dada742008-01-03 12:13:42 +0000433 now = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000434 timestamp = msg;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000435 msg += 16;
436 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000437 timestamp[15] = '\0';
Eric Andersen3843e961999-11-25 07:30:46 +0000438
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000439 if (option_mask32 & OPT_small)
440 sprintf(G.printbuf, "%s %s\n", timestamp, msg);
441 else {
442 char res[20];
443 parse_fac_prio_20(pri, res);
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000444 sprintf(G.printbuf, "%s %.64s %s %s\n", timestamp, G.hostname, res, msg);
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000445 }
Denis Vlasenko4dada742008-01-03 12:13:42 +0000446
447 /* Log message locally (to file or shared mem) */
448 log_locally(now, G.printbuf);
449}
450
451static void timestamp_and_log_internal(const char *msg)
452{
Denis Vlasenko0d948202008-12-09 22:53:31 +0000453 /* -L, or no -R */
Denis Vlasenko4dada742008-01-03 12:13:42 +0000454 if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog))
455 return;
456 timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000457}
Glenn L McGrath73ebb882004-09-14 18:12:13 +0000458
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000459/* tmpbuf[len] is a NUL byte (set by caller), but there can be other,
460 * embedded NULs. Split messages on each of these NULs, parse prio,
461 * escape control chars and log each locally. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000462static void split_escape_and_log(char *tmpbuf, int len)
463{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000464 char *p = tmpbuf;
465
466 tmpbuf += len;
467 while (p < tmpbuf) {
468 char c;
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000469 char *q = G.parsebuf;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000470 int pri = (LOG_USER | LOG_NOTICE);
471
472 if (*p == '<') {
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000473 /* Parse the magic priority number */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000474 pri = bb_strtou(p + 1, &p, 10);
Denis Vlasenko018b1552007-11-06 01:38:46 +0000475 if (*p == '>')
476 p++;
477 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000478 pri = (LOG_USER | LOG_NOTICE);
Denis Vlasenko1decd0e2006-09-30 19:17:40 +0000479 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000480
481 while ((c = *p++)) {
482 if (c == '\n')
483 c = ' ';
Denis Vlasenko2ee028d2007-06-21 13:44:53 +0000484 if (!(c & ~0x1f) && c != '\t') {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000485 *q++ = '^';
486 c += '@'; /* ^@, ^A, ^B... */
487 }
488 *q++ = c;
489 }
490 *q = '\0';
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000491
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000492 /* Now log it */
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000493 if (LOG_PRI(pri) < G.logLevel)
494 timestamp_and_log(pri, G.parsebuf, q - G.parsebuf);
Eric Andersen7f94a5c2004-06-22 10:12:59 +0000495 }
Eric Andersen3843e961999-11-25 07:30:46 +0000496}
497
Denis Vlasenko4998c812007-02-14 20:51:46 +0000498#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000499static void do_mark(int sig)
Eric Andersen3843e961999-11-25 07:30:46 +0000500{
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000501 if (G.markInterval) {
Denis Vlasenko4dada742008-01-03 12:13:42 +0000502 timestamp_and_log_internal("-- MARK --");
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000503 alarm(G.markInterval);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000504 }
Eric Andersen3843e961999-11-25 07:30:46 +0000505}
Denis Vlasenko4998c812007-02-14 20:51:46 +0000506#endif
Eric Andersen3843e961999-11-25 07:30:46 +0000507
Denis Vlasenkod031b202007-11-10 01:28:19 +0000508/* Don't inline: prevent struct sockaddr_un to take up space on stack
509 * permanently */
510static NOINLINE int create_socket(void)
Eric Andersen3843e961999-11-25 07:30:46 +0000511{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512 struct sockaddr_un sunx;
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000513 int sock_fd;
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000514 char *dev_log_name;
Erik Andersenf13df372000-04-18 23:51:51 +0000515
Denis Vlasenkod031b202007-11-10 01:28:19 +0000516 memset(&sunx, 0, sizeof(sunx));
517 sunx.sun_family = AF_UNIX;
518
519 /* Unlink old /dev/log or object it points to. */
520 /* (if it exists, bind will fail) */
521 strcpy(sunx.sun_path, "/dev/log");
522 dev_log_name = xmalloc_follow_symlinks("/dev/log");
523 if (dev_log_name) {
524 safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
525 free(dev_log_name);
526 }
527 unlink(sunx.sun_path);
528
529 sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
530 xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
531 chmod("/dev/log", 0666);
532
533 return sock_fd;
534}
535
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000536#if ENABLE_FEATURE_REMOTE_LOG
537static int try_to_resolve_remote(void)
538{
539 if (!G.remoteAddr) {
540 unsigned now = monotonic_sec();
541
542 /* Don't resolve name too often - DNS timeouts can be big */
543 if ((now - G.last_dns_resolve) < DNS_WAIT_SEC)
544 return -1;
545 G.last_dns_resolve = now;
546 G.remoteAddr = host2sockaddr(G.remoteAddrStr, 514);
547 if (!G.remoteAddr)
548 return -1;
549 }
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000550 return socket(G.remoteAddr->u.sa.sa_family, SOCK_DGRAM, 0);
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000551}
552#endif
553
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000554static void do_syslogd(void) NORETURN;
Denis Vlasenkod031b202007-11-10 01:28:19 +0000555static void do_syslogd(void)
556{
557 int sock_fd;
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000558#if ENABLE_FEATURE_SYSLOGD_DUP
559 int last_sz = -1;
560 char *last_buf;
561 char *recvbuf = G.recvbuf;
562#else
563#define recvbuf (G.recvbuf)
564#endif
Denis Vlasenkod031b202007-11-10 01:28:19 +0000565
Denis Vlasenko0d948202008-12-09 22:53:31 +0000566 /* Set up signal handlers (so that they interrupt read()) */
567 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
568 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
569 //signal_no_SA_RESTART_empty_mask(SIGQUIT, record_signo);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000570 signal(SIGHUP, SIG_IGN);
Denis Vlasenko4998c812007-02-14 20:51:46 +0000571#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000572 signal(SIGALRM, do_mark);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000573 alarm(G.markInterval);
Denis Vlasenko4998c812007-02-14 20:51:46 +0000574#endif
Denis Vlasenkod031b202007-11-10 01:28:19 +0000575 sock_fd = create_socket();
Eric Andersenb99df0f1999-11-24 09:04:33 +0000576
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000577 if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000578 ipcsyslog_init();
Eric Andersenea906502001-04-05 20:55:17 +0000579 }
Eric Andersenea906502001-04-05 20:55:17 +0000580
Denis Vlasenko4dada742008-01-03 12:13:42 +0000581 timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000582
Denis Vlasenko0d948202008-12-09 22:53:31 +0000583 while (!bb_got_signal) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000584 ssize_t sz;
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000585
586#if ENABLE_FEATURE_SYSLOGD_DUP
587 last_buf = recvbuf;
588 if (recvbuf == G.recvbuf)
589 recvbuf = G.recvbuf + MAX_READ;
590 else
591 recvbuf = G.recvbuf;
592#endif
Denis Vlasenko018b1552007-11-06 01:38:46 +0000593 read_again:
Denis Vlasenko0d948202008-12-09 22:53:31 +0000594 sz = read(sock_fd, recvbuf, MAX_READ - 1);
595 if (sz < 0) {
596 if (!bb_got_signal)
597 bb_perror_msg("read from /dev/log");
598 break;
599 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000600
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000601 /* Drop trailing '\n' and NULs (typically there is one NUL) */
Denis Vlasenko018b1552007-11-06 01:38:46 +0000602 while (1) {
603 if (sz == 0)
604 goto read_again;
Denis Vlasenkocb12cb22007-11-06 11:34:03 +0000605 /* man 3 syslog says: "A trailing newline is added when needed".
606 * However, neither glibc nor uclibc do this:
607 * syslog(prio, "test") sends "test\0" to /dev/log,
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000608 * syslog(prio, "test\n") sends "test\n\0".
Denis Vlasenkocb12cb22007-11-06 11:34:03 +0000609 * IOW: newline is passed verbatim!
610 * I take it to mean that it's syslogd's job
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000611 * to make those look identical in the log files. */
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000612 if (recvbuf[sz-1] != '\0' && recvbuf[sz-1] != '\n')
Denis Vlasenko018b1552007-11-06 01:38:46 +0000613 break;
614 sz--;
615 }
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000616#if ENABLE_FEATURE_SYSLOGD_DUP
617 if ((option_mask32 & OPT_dup) && (sz == last_sz))
618 if (memcmp(last_buf, recvbuf, sz) == 0)
619 continue;
620 last_sz = sz;
621#endif
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000622#if ENABLE_FEATURE_REMOTE_LOG
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000623 /* We are not modifying log messages in any way before send */
624 /* Remote site cannot trust _us_ anyway and need to do validation again */
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000625 if (G.remoteAddrStr) {
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000626 if (-1 == G.remoteFD) {
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000627 G.remoteFD = try_to_resolve_remote();
628 if (-1 == G.remoteFD)
629 goto no_luck;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000630 }
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000631 /* Stock syslogd sends it '\n'-terminated
632 * over network, mimic that */
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000633 recvbuf[sz] = '\n';
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000634 /* send message to remote logger, ignore possible error */
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000635 /* TODO: on some errors, close and set G.remoteFD to -1
636 * so that DNS resolution and connect is retried? */
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000637 sendto(G.remoteFD, recvbuf, sz+1, MSG_DONTWAIT,
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000638 &G.remoteAddr->u.sa, G.remoteAddr->len);
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000639 no_luck: ;
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000640 }
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000641#endif
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000642 if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000643 recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */
644 split_escape_and_log(recvbuf, sz);
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000645 }
Denis Vlasenko0d948202008-12-09 22:53:31 +0000646 } /* while (!bb_got_signal) */
647
648 timestamp_and_log_internal("syslogd exiting");
649 puts("syslogd exiting");
650 if (ENABLE_FEATURE_IPC_SYSLOG)
651 ipcsyslog_cleanup();
652 kill_myself_with_sig(bb_got_signal);
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000653#undef recvbuf
Eric Andersenb99df0f1999-11-24 09:04:33 +0000654}
655
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000656int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000657int syslogd_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen3843e961999-11-25 07:30:46 +0000658{
Denis Vlasenko14c19402006-09-30 19:20:00 +0000659 char OPTION_DECL;
Denis Vlasenko0d948202008-12-09 22:53:31 +0000660 int opts;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000661
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000662 INIT_G();
663#if ENABLE_FEATURE_REMOTE_LOG
664 G.last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
665#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000666
Eric Andersen394cf222000-12-11 16:48:50 +0000667 /* do normal option parsing */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000668 opt_complementary = "=0"; /* no non-option params */
Denis Vlasenko0d948202008-12-09 22:53:31 +0000669 opts = getopt32(argv, OPTION_STR, OPTION_PARAM);
Denis Vlasenko4998c812007-02-14 20:51:46 +0000670#ifdef SYSLOGD_MARK
Denis Vlasenko0d948202008-12-09 22:53:31 +0000671 if (opts & OPT_mark) // -m
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000672 G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
Denis Vlasenko4998c812007-02-14 20:51:46 +0000673#endif
Denis Vlasenko0d948202008-12-09 22:53:31 +0000674 //if (opts & OPT_nofork) // -n
675 //if (opts & OPT_outfile) // -O
676 if (opts & OPT_loglevel) // -l
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000677 G.logLevel = xatou_range(opt_l, 1, 8);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000678 //if (opts & OPT_small) // -S
Denis Vlasenko14c19402006-09-30 19:20:00 +0000679#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenko0d948202008-12-09 22:53:31 +0000680 if (opts & OPT_filesize) // -s
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000681 G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
Denis Vlasenko0d948202008-12-09 22:53:31 +0000682 if (opts & OPT_rotatecnt) // -b
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000683 G.logFileRotate = xatou_range(opt_b, 0, 99);
Denis Vlasenko14c19402006-09-30 19:20:00 +0000684#endif
Denis Vlasenko14c19402006-09-30 19:20:00 +0000685#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko218f2f42007-01-24 22:02:01 +0000686 if (opt_C) // -Cn
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000687 G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
Denis Vlasenko14c19402006-09-30 19:20:00 +0000688#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000689
Eric Andersen4ed17822000-12-11 19:28:29 +0000690 /* If they have not specified remote logging, then log locally */
Denis Vlasenko0d948202008-12-09 22:53:31 +0000691 if (ENABLE_FEATURE_REMOTE_LOG && !(opts & OPT_remotelog)) // -R
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000692 option_mask32 |= OPT_locallog;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000693
Erik Andersene49d5ec2000-02-08 19:58:47 +0000694 /* Store away localhost's name before the fork */
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000695 G.hostname = safe_gethostname();
696 *strchrnul(G.hostname, '.') = '\0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000697
Denis Vlasenko0d948202008-12-09 22:53:31 +0000698 if (!(opts & OPT_nofork)) {
Denis Vlasenko5a142022007-03-26 13:20:54 +0000699 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
Eric Andersen35e643b2003-07-28 07:40:39 +0000700 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000701 umask(0);
Denis Vlasenko10457b92007-03-27 22:01:31 +0000702 write_pidfile("/var/run/syslogd.pid");
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000703 do_syslogd();
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000704 /* return EXIT_SUCCESS; */
Eric Andersen3843e961999-11-25 07:30:46 +0000705}
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000706
707/* Clean up. Needed because we are included from syslogd_and_logger.c */
Denis Vlasenko0d948202008-12-09 22:53:31 +0000708#undef DEBUG
709#undef SYSLOGD_MARK
710#undef SYSLOGD_WRLOCK
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000711#undef G
712#undef GLOBALS
713#undef INIT_G
714#undef OPTION_STR
715#undef OPTION_DECL
716#undef OPTION_PARAM