blob: 48b875ef130a07ce832bcfcb48387aeed816cdc6 [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
Eric Andersenb186d981999-12-03 09:19:54 +000024#include <paths.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000025#include <sys/un.h>
Eric Andersenced2cef2000-07-20 23:41:24 +000026#include <sys/uio.h>
Eric Andersen3843e961999-11-25 07:30:46 +000027
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000028#if ENABLE_FEATURE_REMOTE_LOG
29#include <netinet/in.h>
Denis Vlasenko4998c812007-02-14 20:51:46 +000030#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000031
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000032#if ENABLE_FEATURE_IPC_SYSLOG
33#include <sys/ipc.h>
34#include <sys/sem.h>
35#include <sys/shm.h>
36#endif
Denis Vlasenko1decd0e2006-09-30 19:17:40 +000037
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000038
39#define DEBUG 0
40
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000041/* MARK code is not very useful, is bloat, and broken:
42 * can deadlock if alarmed to make MARK while writing to IPC buffer
43 * (semaphores are down but do_mark routine tries to down them again) */
44#undef SYSLOGD_MARK
45
Denis Vlasenko0d948202008-12-09 22:53:31 +000046/* Write locking does not seem to be useful either */
47#undef SYSLOGD_WRLOCK
48
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +000049enum {
50 MAX_READ = 256,
51 DNS_WAIT_SEC = 2 * 60,
52};
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000053
54/* Semaphore operation structures */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000055struct shbuf_ds {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000056 int32_t size; /* size of data - 1 */
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000057 int32_t tail; /* end of message list */
58 char data[1]; /* data/messages */
59};
60
61/* Allows us to have smaller initializer. Ugly. */
62#define GLOBALS \
63 const char *logFilePath; \
64 int logFD; \
65 /* interval between marks in seconds */ \
66 /*int markInterval;*/ \
67 /* level of messages to be logged */ \
68 int logLevel; \
69USE_FEATURE_ROTATE_LOGFILE( \
70 /* max size of file before rotation */ \
71 unsigned logFileSize; \
72 /* number of rotated message files */ \
73 unsigned logFileRotate; \
74 unsigned curFileSize; \
75 smallint isRegular; \
76) \
77USE_FEATURE_REMOTE_LOG( \
78 /* udp socket for remote logging */ \
79 int remoteFD; \
80 len_and_sockaddr* remoteAddr; \
81) \
82USE_FEATURE_IPC_SYSLOG( \
83 int shmid; /* ipc shared memory id */ \
84 int s_semid; /* ipc semaphore id */ \
85 int shm_size; \
86 struct sembuf SMwup[1]; \
87 struct sembuf SMwdn[3]; \
88)
89
90struct init_globals {
91 GLOBALS
92};
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000093
94struct globals {
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000095 GLOBALS
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +000096
97#if ENABLE_FEATURE_REMOTE_LOG
98 unsigned last_dns_resolve;
99 char *remoteAddrStr;
100#endif
101
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000102#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000103 struct shbuf_ds *shbuf;
104#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000105 time_t last_log_time;
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000106 /* localhost's name. We print only first 64 chars */
107 char *hostname;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000108
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000109 /* We recv into recvbuf... */
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000110 char recvbuf[MAX_READ * (1 + ENABLE_FEATURE_SYSLOGD_DUP)];
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000111 /* ...then copy to parsebuf, escaping control chars */
112 /* (can grow x2 max) */
113 char parsebuf[MAX_READ*2];
114 /* ...then sprintf into printbuf, adding timestamp (15 chars),
115 * host (64), fac.prio (20) to the message */
116 /* (growth by: 15 + 64 + 20 + delims = ~110) */
117 char printbuf[MAX_READ*2 + 128];
118};
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000119
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000120static const struct init_globals init_data = {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000121 .logFilePath = "/var/log/messages",
122 .logFD = -1,
123#ifdef SYSLOGD_MARK
124 .markInterval = 20 * 60,
125#endif
126 .logLevel = 8,
127#if ENABLE_FEATURE_ROTATE_LOGFILE
128 .logFileSize = 200 * 1024,
129 .logFileRotate = 1,
130#endif
131#if ENABLE_FEATURE_REMOTE_LOG
132 .remoteFD = -1,
133#endif
134#if ENABLE_FEATURE_IPC_SYSLOG
135 .shmid = -1,
136 .s_semid = -1,
137 .shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024), // default shm size
138 .SMwup = { {1, -1, IPC_NOWAIT} },
139 .SMwdn = { {0, 0}, {1, 0}, {1, +1} },
140#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000141};
142
143#define G (*ptr_to_globals)
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000144#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000145 SET_PTR_TO_GLOBALS(memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data))); \
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000146} while (0)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000147
148
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000149/* Options */
Denis Vlasenko14c19402006-09-30 19:20:00 +0000150enum {
151 OPTBIT_mark = 0, // -m
152 OPTBIT_nofork, // -n
153 OPTBIT_outfile, // -O
154 OPTBIT_loglevel, // -l
155 OPTBIT_small, // -S
156 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
157 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
Denis Vlasenko0d948202008-12-09 22:53:31 +0000158 USE_FEATURE_REMOTE_LOG( OPTBIT_remotelog ,) // -R
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000159 USE_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L
Denis Vlasenko14c19402006-09-30 19:20:00 +0000160 USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000161 USE_FEATURE_SYSLOGD_DUP( OPTBIT_dup ,) // -D
Denis Vlasenko14c19402006-09-30 19:20:00 +0000162
163 OPT_mark = 1 << OPTBIT_mark ,
164 OPT_nofork = 1 << OPTBIT_nofork ,
165 OPT_outfile = 1 << OPTBIT_outfile ,
166 OPT_loglevel = 1 << OPTBIT_loglevel,
167 OPT_small = 1 << OPTBIT_small ,
168 OPT_filesize = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
169 OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
Denis Vlasenko0d948202008-12-09 22:53:31 +0000170 OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remotelog )) + 0,
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000171 OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0,
Denis Vlasenko14c19402006-09-30 19:20:00 +0000172 OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000173 OPT_dup = USE_FEATURE_SYSLOGD_DUP( (1 << OPTBIT_dup )) + 0,
Denis Vlasenko14c19402006-09-30 19:20:00 +0000174};
175#define OPTION_STR "m:nO:l:S" \
176 USE_FEATURE_ROTATE_LOGFILE("s:" ) \
177 USE_FEATURE_ROTATE_LOGFILE("b:" ) \
178 USE_FEATURE_REMOTE_LOG( "R:" ) \
179 USE_FEATURE_REMOTE_LOG( "L" ) \
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000180 USE_FEATURE_IPC_SYSLOG( "C::") \
181 USE_FEATURE_SYSLOGD_DUP( "D" )
Denis Vlasenko14c19402006-09-30 19:20:00 +0000182#define OPTION_DECL *opt_m, *opt_l \
183 USE_FEATURE_ROTATE_LOGFILE(,*opt_s) \
184 USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \
Denis Vlasenko14c19402006-09-30 19:20:00 +0000185 USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000186#define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \
Denis Vlasenko14c19402006-09-30 19:20:00 +0000187 USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \
188 USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000189 USE_FEATURE_REMOTE_LOG( ,&G.remoteAddrStr) \
Denis Vlasenko14c19402006-09-30 19:20:00 +0000190 USE_FEATURE_IPC_SYSLOG( ,&opt_C)
Eric Andersen871d93c2002-09-17 20:06:29 +0000191
Eric Andersen871d93c2002-09-17 20:06:29 +0000192
Mark Whitley6317c4b2001-03-12 22:51:50 +0000193/* circular buffer variables/structures */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000194#if ENABLE_FEATURE_IPC_SYSLOG
195
Eric Andersend4a5e252003-12-19 11:32:14 +0000196#if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4
197#error Sorry, you must set the syslogd buffer size to at least 4KB.
198#error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE
199#endif
200
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000201/* our shared key (syslogd.c and logread.c must be in sync) */
202enum { KEY_ID = 0x414e4547 }; /* "GENA" */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000203
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000204static void ipcsyslog_cleanup(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000205{
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000206 if (G.shmid != -1) {
207 shmdt(G.shbuf);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000208 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000209 if (G.shmid != -1) {
210 shmctl(G.shmid, IPC_RMID, NULL);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000211 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000212 if (G.s_semid != -1) {
213 semctl(G.s_semid, 0, IPC_RMID, 0);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000214 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000215}
216
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000217static void ipcsyslog_init(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000218{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000219 if (DEBUG)
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000220 printf("shmget(%x, %d,...)\n", (int)KEY_ID, G.shm_size);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000221
Denis Vlasenkoa1120a82007-08-14 10:27:56 +0000222 G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 0644);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000223 if (G.shmid == -1) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000224 bb_perror_msg_and_die("shmget");
225 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000226
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000227 G.shbuf = shmat(G.shmid, NULL, 0);
Denis Vlasenko4e9ca752008-01-07 15:58:02 +0000228 if (G.shbuf == (void*) -1L) { /* shmat has bizarre error return */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000229 bb_perror_msg_and_die("shmat");
230 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000231
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000232 memset(G.shbuf, 0, G.shm_size);
233 G.shbuf->size = G.shm_size - offsetof(struct shbuf_ds, data) - 1;
234 /*G.shbuf->tail = 0;*/
Mark Whitley6317c4b2001-03-12 22:51:50 +0000235
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000236 // we'll trust the OS to set initial semval to 0 (let's hope)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000237 G.s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023);
238 if (G.s_semid == -1) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000239 if (errno == EEXIST) {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000240 G.s_semid = semget(KEY_ID, 2, 0);
241 if (G.s_semid != -1)
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000242 return;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000243 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000244 bb_perror_msg_and_die("semget");
Mark Whitley6317c4b2001-03-12 22:51:50 +0000245 }
246}
247
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000248/* Write message to shared mem buffer */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000249static void log_to_shmem(const char *msg, int len)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000250{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000251 int old_tail, new_tail;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000252
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000253 if (semop(G.s_semid, G.SMwdn, 3) == -1) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000254 bb_perror_msg_and_die("SMwdn");
255 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000256
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000257 /* Circular Buffer Algorithm:
Mark Whitley6317c4b2001-03-12 22:51:50 +0000258 * --------------------------
Denis Vlasenko150f4022007-01-13 21:06:21 +0000259 * tail == position where to store next syslog message.
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000260 * tail's max value is (shbuf->size - 1)
261 * Last byte of buffer is never used and remains NUL.
Mark Whitley6317c4b2001-03-12 22:51:50 +0000262 */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000263 len++; /* length with NUL included */
264 again:
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000265 old_tail = G.shbuf->tail;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000266 new_tail = old_tail + len;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000267 if (new_tail < G.shbuf->size) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000268 /* store message, set new tail */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000269 memcpy(G.shbuf->data + old_tail, msg, len);
270 G.shbuf->tail = new_tail;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000271 } else {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000272 /* k == available buffer space ahead of old tail */
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000273 int k = G.shbuf->size - old_tail;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000274 /* copy what fits to the end of buffer, and repeat */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000275 memcpy(G.shbuf->data + old_tail, msg, k);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000276 msg += k;
277 len -= k;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000278 G.shbuf->tail = 0;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000279 goto again;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000280 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000281 if (semop(G.s_semid, G.SMwup, 1) == -1) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000282 bb_perror_msg_and_die("SMwup");
283 }
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000284 if (DEBUG)
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000285 printf("tail:%d\n", G.shbuf->tail);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000286}
Rob Landley028ba282006-08-28 20:16:42 +0000287#else
288void ipcsyslog_cleanup(void);
289void ipcsyslog_init(void);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000290void log_to_shmem(const char *msg);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000291#endif /* FEATURE_IPC_SYSLOG */
292
293
Erik Andersen983b51b2000-04-04 18:14:25 +0000294/* Print a message to the log file. */
Denis Vlasenko4dada742008-01-03 12:13:42 +0000295static void log_locally(time_t now, char *msg)
Eric Andersen3843e961999-11-25 07:30:46 +0000296{
Denis Vlasenko0d948202008-12-09 22:53:31 +0000297#ifdef SYSLOGD_WRLOCK
Denis Vlasenkob8934972007-01-04 18:02:32 +0000298 struct flock fl;
Denis Vlasenko0d948202008-12-09 22:53:31 +0000299#endif
Denis Vlasenkob8934972007-01-04 18:02:32 +0000300 int len = strlen(msg);
Eric Andersen3843e961999-11-25 07:30:46 +0000301
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000302#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000303 if ((option_mask32 & OPT_circularlog) && G.shbuf) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000304 log_to_shmem(msg, len);
305 return;
306 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000307#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000308 if (G.logFD >= 0) {
Denis Vlasenko4dada742008-01-03 12:13:42 +0000309 if (!now)
310 now = time(NULL);
311 if (G.last_log_time != now) {
312 G.last_log_time = now; /* reopen log file every second */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000313 close(G.logFD);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000314 goto reopen;
315 }
316 } else {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000317 reopen:
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000318 G.logFD = device_open(G.logFilePath, O_WRONLY | O_CREAT
Denis Vlasenkobd8f43d2006-09-08 17:31:55 +0000319 | O_NOCTTY | O_APPEND | O_NONBLOCK);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000320 if (G.logFD < 0) {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000321 /* cannot open logfile? - print to /dev/console then */
Denis Vlasenko70ab28f2007-11-18 05:43:05 +0000322 int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000323 if (fd < 0)
324 fd = 2; /* then stderr, dammit */
325 full_write(fd, msg, len);
326 if (fd != 2)
327 close(fd);
328 return;
329 }
330#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenkobae79482007-01-09 23:42:43 +0000331 {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000332 struct stat statf;
333 G.isRegular = (fstat(G.logFD, &statf) == 0 && S_ISREG(statf.st_mode));
334 /* bug (mostly harmless): can wrap around if file > 4gb */
335 G.curFileSize = statf.st_size;
Denis Vlasenkobae79482007-01-09 23:42:43 +0000336 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000337#endif
338 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000339
Denis Vlasenko0d948202008-12-09 22:53:31 +0000340#ifdef SYSLOGD_WRLOCK
Denis Vlasenkob8934972007-01-04 18:02:32 +0000341 fl.l_whence = SEEK_SET;
342 fl.l_start = 0;
343 fl.l_len = 1;
344 fl.l_type = F_WRLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000345 fcntl(G.logFD, F_SETLKW, &fl);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000346#endif
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000347
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000348#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000349 if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) {
350 if (G.logFileRotate) { /* always 0..99 */
351 int i = strlen(G.logFilePath) + 3 + 1;
Denis Vlasenkob8934972007-01-04 18:02:32 +0000352 char oldFile[i];
353 char newFile[i];
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000354 i = G.logFileRotate - 1;
Denis Vlasenkob8934972007-01-04 18:02:32 +0000355 /* rename: f.8 -> f.9; f.7 -> f.8; ... */
356 while (1) {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000357 sprintf(newFile, "%s.%d", G.logFilePath, i);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000358 if (i == 0) break;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000359 sprintf(oldFile, "%s.%d", G.logFilePath, --i);
Denis Vlasenko69dc3252008-05-24 21:03:53 +0000360 /* ignore errors - file might be missing */
361 rename(oldFile, newFile);
Eric Andersen29c77f72003-10-09 09:43:18 +0000362 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000363 /* newFile == "f.0" now */
Denis Vlasenko69dc3252008-05-24 21:03:53 +0000364 rename(G.logFilePath, newFile);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000365#ifdef SYSLOGD_WRLOCK
Denis Vlasenkob8934972007-01-04 18:02:32 +0000366 fl.l_type = F_UNLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000367 fcntl(G.logFD, F_SETLKW, &fl);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000368#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000369 close(G.logFD);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000370 goto reopen;
Eric Andersen29c77f72003-10-09 09:43:18 +0000371 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000372 ftruncate(G.logFD, 0);
Eric Andersen3843e961999-11-25 07:30:46 +0000373 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000374 G.curFileSize +=
Denis Vlasenkob8934972007-01-04 18:02:32 +0000375#endif
Denis Vlasenko9504e442008-10-29 01:19:15 +0000376 full_write(G.logFD, msg, len);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000377#ifdef SYSLOGD_WRLOCK
Denis Vlasenkob8934972007-01-04 18:02:32 +0000378 fl.l_type = F_UNLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000379 fcntl(G.logFD, F_SETLKW, &fl);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000380#endif
Eric Andersenb99df0f1999-11-24 09:04:33 +0000381}
382
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000383static void parse_fac_prio_20(int pri, char *res20)
Eric Andersen3843e961999-11-25 07:30:46 +0000384{
Denis Vlasenko3c8b5ba2007-06-04 18:23:59 +0000385 const CODE *c_pri, *c_fac;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000386
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000387 if (pri != 0) {
Denis Vlasenko14c19402006-09-30 19:20:00 +0000388 c_fac = facilitynames;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000389 while (c_fac->c_name) {
390 if (c_fac->c_val != (LOG_FAC(pri) << 3)) {
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000391 c_fac++;
392 continue;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000393 }
394 /* facility is found, look for prio */
395 c_pri = prioritynames;
396 while (c_pri->c_name) {
397 if (c_pri->c_val != LOG_PRI(pri)) {
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000398 c_pri++;
399 continue;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000400 }
401 snprintf(res20, 20, "%s.%s",
402 c_fac->c_name, c_pri->c_name);
403 return;
404 }
405 /* prio not found, bail out */
406 break;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000407 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000408 snprintf(res20, 20, "<%d>", pri);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000409 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000410}
Eric Andersen3843e961999-11-25 07:30:46 +0000411
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000412/* len parameter is used only for "is there a timestamp?" check.
Denis Vlasenko018b1552007-11-06 01:38:46 +0000413 * NB: some callers cheat and supply len==0 when they know
414 * that there is no timestamp, short-circuiting the test. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000415static void timestamp_and_log(int pri, char *msg, int len)
416{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000417 char *timestamp;
Denis Vlasenko4dada742008-01-03 12:13:42 +0000418 time_t now;
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000419
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000420 if (len < 16 || msg[3] != ' ' || msg[6] != ' '
421 || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
422 ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000423 time(&now);
Denis Vlasenko4dada742008-01-03 12:13:42 +0000424 timestamp = ctime(&now) + 4; /* skip day of week */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000425 } else {
Denis Vlasenko4dada742008-01-03 12:13:42 +0000426 now = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000427 timestamp = msg;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000428 msg += 16;
429 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000430 timestamp[15] = '\0';
Eric Andersen3843e961999-11-25 07:30:46 +0000431
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000432 if (option_mask32 & OPT_small)
433 sprintf(G.printbuf, "%s %s\n", timestamp, msg);
434 else {
435 char res[20];
436 parse_fac_prio_20(pri, res);
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000437 sprintf(G.printbuf, "%s %.64s %s %s\n", timestamp, G.hostname, res, msg);
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000438 }
Denis Vlasenko4dada742008-01-03 12:13:42 +0000439
440 /* Log message locally (to file or shared mem) */
441 log_locally(now, G.printbuf);
442}
443
444static void timestamp_and_log_internal(const char *msg)
445{
Denis Vlasenko0d948202008-12-09 22:53:31 +0000446 /* -L, or no -R */
Denis Vlasenko4dada742008-01-03 12:13:42 +0000447 if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog))
448 return;
449 timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000450}
Glenn L McGrath73ebb882004-09-14 18:12:13 +0000451
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000452/* tmpbuf[len] is a NUL byte (set by caller), but there can be other,
453 * embedded NULs. Split messages on each of these NULs, parse prio,
454 * escape control chars and log each locally. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000455static void split_escape_and_log(char *tmpbuf, int len)
456{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000457 char *p = tmpbuf;
458
459 tmpbuf += len;
460 while (p < tmpbuf) {
461 char c;
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000462 char *q = G.parsebuf;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000463 int pri = (LOG_USER | LOG_NOTICE);
464
465 if (*p == '<') {
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000466 /* Parse the magic priority number */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000467 pri = bb_strtou(p + 1, &p, 10);
Denis Vlasenko018b1552007-11-06 01:38:46 +0000468 if (*p == '>')
469 p++;
470 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000471 pri = (LOG_USER | LOG_NOTICE);
Denis Vlasenko1decd0e2006-09-30 19:17:40 +0000472 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000473
474 while ((c = *p++)) {
475 if (c == '\n')
476 c = ' ';
Denis Vlasenko2ee028d2007-06-21 13:44:53 +0000477 if (!(c & ~0x1f) && c != '\t') {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000478 *q++ = '^';
479 c += '@'; /* ^@, ^A, ^B... */
480 }
481 *q++ = c;
482 }
483 *q = '\0';
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000484
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000485 /* Now log it */
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000486 if (LOG_PRI(pri) < G.logLevel)
487 timestamp_and_log(pri, G.parsebuf, q - G.parsebuf);
Eric Andersen7f94a5c2004-06-22 10:12:59 +0000488 }
Eric Andersen3843e961999-11-25 07:30:46 +0000489}
490
Denis Vlasenko4998c812007-02-14 20:51:46 +0000491#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000492static void do_mark(int sig)
Eric Andersen3843e961999-11-25 07:30:46 +0000493{
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000494 if (G.markInterval) {
Denis Vlasenko4dada742008-01-03 12:13:42 +0000495 timestamp_and_log_internal("-- MARK --");
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000496 alarm(G.markInterval);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497 }
Eric Andersen3843e961999-11-25 07:30:46 +0000498}
Denis Vlasenko4998c812007-02-14 20:51:46 +0000499#endif
Eric Andersen3843e961999-11-25 07:30:46 +0000500
Denis Vlasenkod031b202007-11-10 01:28:19 +0000501/* Don't inline: prevent struct sockaddr_un to take up space on stack
502 * permanently */
503static NOINLINE int create_socket(void)
Eric Andersen3843e961999-11-25 07:30:46 +0000504{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000505 struct sockaddr_un sunx;
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000506 int sock_fd;
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000507 char *dev_log_name;
Erik Andersenf13df372000-04-18 23:51:51 +0000508
Denis Vlasenkod031b202007-11-10 01:28:19 +0000509 memset(&sunx, 0, sizeof(sunx));
510 sunx.sun_family = AF_UNIX;
511
512 /* Unlink old /dev/log or object it points to. */
513 /* (if it exists, bind will fail) */
514 strcpy(sunx.sun_path, "/dev/log");
515 dev_log_name = xmalloc_follow_symlinks("/dev/log");
516 if (dev_log_name) {
517 safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
518 free(dev_log_name);
519 }
520 unlink(sunx.sun_path);
521
522 sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
523 xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
524 chmod("/dev/log", 0666);
525
526 return sock_fd;
527}
528
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000529#if ENABLE_FEATURE_REMOTE_LOG
530static int try_to_resolve_remote(void)
531{
532 if (!G.remoteAddr) {
533 unsigned now = monotonic_sec();
534
535 /* Don't resolve name too often - DNS timeouts can be big */
536 if ((now - G.last_dns_resolve) < DNS_WAIT_SEC)
537 return -1;
538 G.last_dns_resolve = now;
539 G.remoteAddr = host2sockaddr(G.remoteAddrStr, 514);
540 if (!G.remoteAddr)
541 return -1;
542 }
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000543 return socket(G.remoteAddr->u.sa.sa_family, SOCK_DGRAM, 0);
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000544}
545#endif
546
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000547static void do_syslogd(void) NORETURN;
Denis Vlasenkod031b202007-11-10 01:28:19 +0000548static void do_syslogd(void)
549{
550 int sock_fd;
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000551#if ENABLE_FEATURE_SYSLOGD_DUP
552 int last_sz = -1;
553 char *last_buf;
554 char *recvbuf = G.recvbuf;
555#else
556#define recvbuf (G.recvbuf)
557#endif
Denis Vlasenkod031b202007-11-10 01:28:19 +0000558
Denis Vlasenko0d948202008-12-09 22:53:31 +0000559 /* Set up signal handlers (so that they interrupt read()) */
560 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
561 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
562 //signal_no_SA_RESTART_empty_mask(SIGQUIT, record_signo);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000563 signal(SIGHUP, SIG_IGN);
Denis Vlasenko4998c812007-02-14 20:51:46 +0000564#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000565 signal(SIGALRM, do_mark);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000566 alarm(G.markInterval);
Denis Vlasenko4998c812007-02-14 20:51:46 +0000567#endif
Denis Vlasenkod031b202007-11-10 01:28:19 +0000568 sock_fd = create_socket();
Eric Andersenb99df0f1999-11-24 09:04:33 +0000569
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000570 if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000571 ipcsyslog_init();
Eric Andersenea906502001-04-05 20:55:17 +0000572 }
Eric Andersenea906502001-04-05 20:55:17 +0000573
Denis Vlasenko4dada742008-01-03 12:13:42 +0000574 timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000575
Denis Vlasenko0d948202008-12-09 22:53:31 +0000576 while (!bb_got_signal) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000577 ssize_t sz;
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000578
579#if ENABLE_FEATURE_SYSLOGD_DUP
580 last_buf = recvbuf;
581 if (recvbuf == G.recvbuf)
582 recvbuf = G.recvbuf + MAX_READ;
583 else
584 recvbuf = G.recvbuf;
585#endif
Denis Vlasenko018b1552007-11-06 01:38:46 +0000586 read_again:
Denis Vlasenko0d948202008-12-09 22:53:31 +0000587 sz = read(sock_fd, recvbuf, MAX_READ - 1);
588 if (sz < 0) {
589 if (!bb_got_signal)
590 bb_perror_msg("read from /dev/log");
591 break;
592 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000593
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000594 /* Drop trailing '\n' and NULs (typically there is one NUL) */
Denis Vlasenko018b1552007-11-06 01:38:46 +0000595 while (1) {
596 if (sz == 0)
597 goto read_again;
Denis Vlasenkocb12cb22007-11-06 11:34:03 +0000598 /* man 3 syslog says: "A trailing newline is added when needed".
599 * However, neither glibc nor uclibc do this:
600 * syslog(prio, "test") sends "test\0" to /dev/log,
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000601 * syslog(prio, "test\n") sends "test\n\0".
Denis Vlasenkocb12cb22007-11-06 11:34:03 +0000602 * IOW: newline is passed verbatim!
603 * I take it to mean that it's syslogd's job
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000604 * to make those look identical in the log files. */
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000605 if (recvbuf[sz-1] != '\0' && recvbuf[sz-1] != '\n')
Denis Vlasenko018b1552007-11-06 01:38:46 +0000606 break;
607 sz--;
608 }
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000609#if ENABLE_FEATURE_SYSLOGD_DUP
610 if ((option_mask32 & OPT_dup) && (sz == last_sz))
611 if (memcmp(last_buf, recvbuf, sz) == 0)
612 continue;
613 last_sz = sz;
614#endif
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000615#if ENABLE_FEATURE_REMOTE_LOG
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000616 /* We are not modifying log messages in any way before send */
617 /* Remote site cannot trust _us_ anyway and need to do validation again */
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000618 if (G.remoteAddrStr) {
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000619 if (-1 == G.remoteFD) {
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000620 G.remoteFD = try_to_resolve_remote();
621 if (-1 == G.remoteFD)
622 goto no_luck;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000623 }
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000624 /* Stock syslogd sends it '\n'-terminated
625 * over network, mimic that */
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000626 recvbuf[sz] = '\n';
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000627 /* send message to remote logger, ignore possible error */
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000628 /* TODO: on some errors, close and set G.remoteFD to -1
629 * so that DNS resolution and connect is retried? */
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000630 sendto(G.remoteFD, recvbuf, sz+1, MSG_DONTWAIT,
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000631 &G.remoteAddr->u.sa, G.remoteAddr->len);
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000632 no_luck: ;
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000633 }
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000634#endif
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000635 if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000636 recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */
637 split_escape_and_log(recvbuf, sz);
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000638 }
Denis Vlasenko0d948202008-12-09 22:53:31 +0000639 } /* while (!bb_got_signal) */
640
641 timestamp_and_log_internal("syslogd exiting");
642 puts("syslogd exiting");
643 if (ENABLE_FEATURE_IPC_SYSLOG)
644 ipcsyslog_cleanup();
645 kill_myself_with_sig(bb_got_signal);
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000646#undef recvbuf
Eric Andersenb99df0f1999-11-24 09:04:33 +0000647}
648
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000649int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000650int syslogd_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen3843e961999-11-25 07:30:46 +0000651{
Denis Vlasenko14c19402006-09-30 19:20:00 +0000652 char OPTION_DECL;
Denis Vlasenko0d948202008-12-09 22:53:31 +0000653 int opts;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000654
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000655 INIT_G();
656#if ENABLE_FEATURE_REMOTE_LOG
657 G.last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
658#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000659
Eric Andersen394cf222000-12-11 16:48:50 +0000660 /* do normal option parsing */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000661 opt_complementary = "=0"; /* no non-option params */
Denis Vlasenko0d948202008-12-09 22:53:31 +0000662 opts = getopt32(argv, OPTION_STR, OPTION_PARAM);
Denis Vlasenko4998c812007-02-14 20:51:46 +0000663#ifdef SYSLOGD_MARK
Denis Vlasenko0d948202008-12-09 22:53:31 +0000664 if (opts & OPT_mark) // -m
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000665 G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
Denis Vlasenko4998c812007-02-14 20:51:46 +0000666#endif
Denis Vlasenko0d948202008-12-09 22:53:31 +0000667 //if (opts & OPT_nofork) // -n
668 //if (opts & OPT_outfile) // -O
669 if (opts & OPT_loglevel) // -l
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000670 G.logLevel = xatou_range(opt_l, 1, 8);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000671 //if (opts & OPT_small) // -S
Denis Vlasenko14c19402006-09-30 19:20:00 +0000672#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenko0d948202008-12-09 22:53:31 +0000673 if (opts & OPT_filesize) // -s
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000674 G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
Denis Vlasenko0d948202008-12-09 22:53:31 +0000675 if (opts & OPT_rotatecnt) // -b
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000676 G.logFileRotate = xatou_range(opt_b, 0, 99);
Denis Vlasenko14c19402006-09-30 19:20:00 +0000677#endif
Denis Vlasenko14c19402006-09-30 19:20:00 +0000678#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko218f2f42007-01-24 22:02:01 +0000679 if (opt_C) // -Cn
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000680 G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
Denis Vlasenko14c19402006-09-30 19:20:00 +0000681#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000682
Eric Andersen4ed17822000-12-11 19:28:29 +0000683 /* If they have not specified remote logging, then log locally */
Denis Vlasenko0d948202008-12-09 22:53:31 +0000684 if (ENABLE_FEATURE_REMOTE_LOG && !(opts & OPT_remotelog)) // -R
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000685 option_mask32 |= OPT_locallog;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000686
Erik Andersene49d5ec2000-02-08 19:58:47 +0000687 /* Store away localhost's name before the fork */
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000688 G.hostname = safe_gethostname();
689 *strchrnul(G.hostname, '.') = '\0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000690
Denis Vlasenko0d948202008-12-09 22:53:31 +0000691 if (!(opts & OPT_nofork)) {
Denis Vlasenko5a142022007-03-26 13:20:54 +0000692 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
Eric Andersen35e643b2003-07-28 07:40:39 +0000693 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000694 umask(0);
Denis Vlasenko10457b92007-03-27 22:01:31 +0000695 write_pidfile("/var/run/syslogd.pid");
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000696 do_syslogd();
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000697 /* return EXIT_SUCCESS; */
Eric Andersen3843e961999-11-25 07:30:46 +0000698}
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000699
700/* Clean up. Needed because we are included from syslogd_and_logger.c */
Denis Vlasenko0d948202008-12-09 22:53:31 +0000701#undef DEBUG
702#undef SYSLOGD_MARK
703#undef SYSLOGD_WRLOCK
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000704#undef G
705#undef GLOBALS
706#undef INIT_G
707#undef OPTION_STR
708#undef OPTION_DECL
709#undef OPTION_PARAM