blob: 596984e3fad50d734ee2fd293428255ca3f9e572 [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 Vlasenkob6adbf12007-05-26 19:00:18 +000016#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000017#define SYSLOG_NAMES
18#define SYSLOG_NAMES_CONST
19#include <syslog.h>
20
Eric Andersenb186d981999-12-03 09:19:54 +000021#include <paths.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000022#include <sys/un.h>
Eric Andersenced2cef2000-07-20 23:41:24 +000023#include <sys/uio.h>
Eric Andersen3843e961999-11-25 07:30:46 +000024
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000025#if ENABLE_FEATURE_REMOTE_LOG
26#include <netinet/in.h>
Denis Vlasenko4998c812007-02-14 20:51:46 +000027#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000028
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000029#if ENABLE_FEATURE_IPC_SYSLOG
30#include <sys/ipc.h>
31#include <sys/sem.h>
32#include <sys/shm.h>
33#endif
Denis Vlasenko1decd0e2006-09-30 19:17:40 +000034
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000035
36#define DEBUG 0
37
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000038/* MARK code is not very useful, is bloat, and broken:
39 * can deadlock if alarmed to make MARK while writing to IPC buffer
40 * (semaphores are down but do_mark routine tries to down them again) */
41#undef SYSLOGD_MARK
42
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +000043enum {
44 MAX_READ = 256,
45 DNS_WAIT_SEC = 2 * 60,
46};
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000047
48/* Semaphore operation structures */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000049struct shbuf_ds {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000050 int32_t size; /* size of data - 1 */
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000051 int32_t tail; /* end of message list */
52 char data[1]; /* data/messages */
53};
54
55/* Allows us to have smaller initializer. Ugly. */
56#define GLOBALS \
57 const char *logFilePath; \
58 int logFD; \
59 /* interval between marks in seconds */ \
60 /*int markInterval;*/ \
61 /* level of messages to be logged */ \
62 int logLevel; \
63USE_FEATURE_ROTATE_LOGFILE( \
64 /* max size of file before rotation */ \
65 unsigned logFileSize; \
66 /* number of rotated message files */ \
67 unsigned logFileRotate; \
68 unsigned curFileSize; \
69 smallint isRegular; \
70) \
71USE_FEATURE_REMOTE_LOG( \
72 /* udp socket for remote logging */ \
73 int remoteFD; \
74 len_and_sockaddr* remoteAddr; \
75) \
76USE_FEATURE_IPC_SYSLOG( \
77 int shmid; /* ipc shared memory id */ \
78 int s_semid; /* ipc semaphore id */ \
79 int shm_size; \
80 struct sembuf SMwup[1]; \
81 struct sembuf SMwdn[3]; \
82)
83
84struct init_globals {
85 GLOBALS
86};
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000087
88struct globals {
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000089 GLOBALS
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +000090
91#if ENABLE_FEATURE_REMOTE_LOG
92 unsigned last_dns_resolve;
93 char *remoteAddrStr;
94#endif
95
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000096#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000097 struct shbuf_ds *shbuf;
98#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000099 time_t last_log_time;
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000100 /* localhost's name. We print only first 64 chars */
101 char *hostname;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000102
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000103 /* We recv into recvbuf... */
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000104 char recvbuf[MAX_READ * (1 + ENABLE_FEATURE_SYSLOGD_DUP)];
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000105 /* ...then copy to parsebuf, escaping control chars */
106 /* (can grow x2 max) */
107 char parsebuf[MAX_READ*2];
108 /* ...then sprintf into printbuf, adding timestamp (15 chars),
109 * host (64), fac.prio (20) to the message */
110 /* (growth by: 15 + 64 + 20 + delims = ~110) */
111 char printbuf[MAX_READ*2 + 128];
112};
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000113
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000114static const struct init_globals init_data = {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000115 .logFilePath = "/var/log/messages",
116 .logFD = -1,
117#ifdef SYSLOGD_MARK
118 .markInterval = 20 * 60,
119#endif
120 .logLevel = 8,
121#if ENABLE_FEATURE_ROTATE_LOGFILE
122 .logFileSize = 200 * 1024,
123 .logFileRotate = 1,
124#endif
125#if ENABLE_FEATURE_REMOTE_LOG
126 .remoteFD = -1,
127#endif
128#if ENABLE_FEATURE_IPC_SYSLOG
129 .shmid = -1,
130 .s_semid = -1,
131 .shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024), // default shm size
132 .SMwup = { {1, -1, IPC_NOWAIT} },
133 .SMwdn = { {0, 0}, {1, 0}, {1, +1} },
134#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000135};
136
137#define G (*ptr_to_globals)
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000138#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000139 SET_PTR_TO_GLOBALS(memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data))); \
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000140} while (0)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000141
142
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000143/* Options */
Denis Vlasenko14c19402006-09-30 19:20:00 +0000144enum {
145 OPTBIT_mark = 0, // -m
146 OPTBIT_nofork, // -n
147 OPTBIT_outfile, // -O
148 OPTBIT_loglevel, // -l
149 OPTBIT_small, // -S
150 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
151 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
152 USE_FEATURE_REMOTE_LOG( OPTBIT_remote ,) // -R
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000153 USE_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L
Denis Vlasenko14c19402006-09-30 19:20:00 +0000154 USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000155 USE_FEATURE_SYSLOGD_DUP( OPTBIT_dup ,) // -D
Denis Vlasenko14c19402006-09-30 19:20:00 +0000156
157 OPT_mark = 1 << OPTBIT_mark ,
158 OPT_nofork = 1 << OPTBIT_nofork ,
159 OPT_outfile = 1 << OPTBIT_outfile ,
160 OPT_loglevel = 1 << OPTBIT_loglevel,
161 OPT_small = 1 << OPTBIT_small ,
162 OPT_filesize = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
163 OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
164 OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remote )) + 0,
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000165 OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0,
Denis Vlasenko14c19402006-09-30 19:20:00 +0000166 OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000167 OPT_dup = USE_FEATURE_SYSLOGD_DUP( (1 << OPTBIT_dup )) + 0,
Denis Vlasenko14c19402006-09-30 19:20:00 +0000168};
169#define OPTION_STR "m:nO:l:S" \
170 USE_FEATURE_ROTATE_LOGFILE("s:" ) \
171 USE_FEATURE_ROTATE_LOGFILE("b:" ) \
172 USE_FEATURE_REMOTE_LOG( "R:" ) \
173 USE_FEATURE_REMOTE_LOG( "L" ) \
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000174 USE_FEATURE_IPC_SYSLOG( "C::") \
175 USE_FEATURE_SYSLOGD_DUP( "D" )
Denis Vlasenko14c19402006-09-30 19:20:00 +0000176#define OPTION_DECL *opt_m, *opt_l \
177 USE_FEATURE_ROTATE_LOGFILE(,*opt_s) \
178 USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \
Denis Vlasenko14c19402006-09-30 19:20:00 +0000179 USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000180#define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \
Denis Vlasenko14c19402006-09-30 19:20:00 +0000181 USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \
182 USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000183 USE_FEATURE_REMOTE_LOG( ,&G.remoteAddrStr) \
Denis Vlasenko14c19402006-09-30 19:20:00 +0000184 USE_FEATURE_IPC_SYSLOG( ,&opt_C)
Eric Andersen871d93c2002-09-17 20:06:29 +0000185
Eric Andersen871d93c2002-09-17 20:06:29 +0000186
Mark Whitley6317c4b2001-03-12 22:51:50 +0000187/* circular buffer variables/structures */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000188#if ENABLE_FEATURE_IPC_SYSLOG
189
Eric Andersend4a5e252003-12-19 11:32:14 +0000190#if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4
191#error Sorry, you must set the syslogd buffer size to at least 4KB.
192#error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE
193#endif
194
Mark Whitley6317c4b2001-03-12 22:51:50 +0000195/* our shared key */
Denis Vlasenko14c19402006-09-30 19:20:00 +0000196#define KEY_ID ((long)0x414e4547) /* "GENA" */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000197
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000198static void ipcsyslog_cleanup(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000199{
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000200 if (G.shmid != -1) {
201 shmdt(G.shbuf);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000202 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000203 if (G.shmid != -1) {
204 shmctl(G.shmid, IPC_RMID, NULL);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000205 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000206 if (G.s_semid != -1) {
207 semctl(G.s_semid, 0, IPC_RMID, 0);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000208 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000209}
210
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000211static void ipcsyslog_init(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000212{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000213 if (DEBUG)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000214 printf("shmget(%lx, %d,...)\n", KEY_ID, G.shm_size);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000215
Denis Vlasenkoa1120a82007-08-14 10:27:56 +0000216 G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 0644);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000217 if (G.shmid == -1) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000218 bb_perror_msg_and_die("shmget");
219 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000220
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000221 G.shbuf = shmat(G.shmid, NULL, 0);
Denis Vlasenko4e9ca752008-01-07 15:58:02 +0000222 if (G.shbuf == (void*) -1L) { /* shmat has bizarre error return */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000223 bb_perror_msg_and_die("shmat");
224 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000225
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000226 memset(G.shbuf, 0, G.shm_size);
227 G.shbuf->size = G.shm_size - offsetof(struct shbuf_ds, data) - 1;
228 /*G.shbuf->tail = 0;*/
Mark Whitley6317c4b2001-03-12 22:51:50 +0000229
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000230 // we'll trust the OS to set initial semval to 0 (let's hope)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000231 G.s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023);
232 if (G.s_semid == -1) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000233 if (errno == EEXIST) {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000234 G.s_semid = semget(KEY_ID, 2, 0);
235 if (G.s_semid != -1)
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000236 return;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000237 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000238 bb_perror_msg_and_die("semget");
Mark Whitley6317c4b2001-03-12 22:51:50 +0000239 }
240}
241
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000242/* Write message to shared mem buffer */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000243static void log_to_shmem(const char *msg, int len)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000244{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000245 int old_tail, new_tail;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000246
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000247 if (semop(G.s_semid, G.SMwdn, 3) == -1) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000248 bb_perror_msg_and_die("SMwdn");
249 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000250
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000251 /* Circular Buffer Algorithm:
Mark Whitley6317c4b2001-03-12 22:51:50 +0000252 * --------------------------
Denis Vlasenko150f4022007-01-13 21:06:21 +0000253 * tail == position where to store next syslog message.
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000254 * tail's max value is (shbuf->size - 1)
255 * Last byte of buffer is never used and remains NUL.
Mark Whitley6317c4b2001-03-12 22:51:50 +0000256 */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000257 len++; /* length with NUL included */
258 again:
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000259 old_tail = G.shbuf->tail;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000260 new_tail = old_tail + len;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000261 if (new_tail < G.shbuf->size) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000262 /* store message, set new tail */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000263 memcpy(G.shbuf->data + old_tail, msg, len);
264 G.shbuf->tail = new_tail;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000265 } else {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000266 /* k == available buffer space ahead of old tail */
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000267 int k = G.shbuf->size - old_tail;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000268 /* copy what fits to the end of buffer, and repeat */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000269 memcpy(G.shbuf->data + old_tail, msg, k);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000270 msg += k;
271 len -= k;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000272 G.shbuf->tail = 0;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000273 goto again;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000274 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000275 if (semop(G.s_semid, G.SMwup, 1) == -1) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000276 bb_perror_msg_and_die("SMwup");
277 }
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000278 if (DEBUG)
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000279 printf("tail:%d\n", G.shbuf->tail);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000280}
Rob Landley028ba282006-08-28 20:16:42 +0000281#else
282void ipcsyslog_cleanup(void);
283void ipcsyslog_init(void);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000284void log_to_shmem(const char *msg);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000285#endif /* FEATURE_IPC_SYSLOG */
286
287
Erik Andersen983b51b2000-04-04 18:14:25 +0000288/* Print a message to the log file. */
Denis Vlasenko4dada742008-01-03 12:13:42 +0000289static void log_locally(time_t now, char *msg)
Eric Andersen3843e961999-11-25 07:30:46 +0000290{
Denis Vlasenkob8934972007-01-04 18:02:32 +0000291 struct flock fl;
292 int len = strlen(msg);
Eric Andersen3843e961999-11-25 07:30:46 +0000293
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000294#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000295 if ((option_mask32 & OPT_circularlog) && G.shbuf) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000296 log_to_shmem(msg, len);
297 return;
298 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000299#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000300 if (G.logFD >= 0) {
Denis Vlasenko4dada742008-01-03 12:13:42 +0000301 if (!now)
302 now = time(NULL);
303 if (G.last_log_time != now) {
304 G.last_log_time = now; /* reopen log file every second */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000305 close(G.logFD);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000306 goto reopen;
307 }
308 } else {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000309 reopen:
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000310 G.logFD = device_open(G.logFilePath, O_WRONLY | O_CREAT
Denis Vlasenkobd8f43d2006-09-08 17:31:55 +0000311 | O_NOCTTY | O_APPEND | O_NONBLOCK);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000312 if (G.logFD < 0) {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000313 /* cannot open logfile? - print to /dev/console then */
Denis Vlasenko70ab28f2007-11-18 05:43:05 +0000314 int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000315 if (fd < 0)
316 fd = 2; /* then stderr, dammit */
317 full_write(fd, msg, len);
318 if (fd != 2)
319 close(fd);
320 return;
321 }
322#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenkobae79482007-01-09 23:42:43 +0000323 {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000324 struct stat statf;
325 G.isRegular = (fstat(G.logFD, &statf) == 0 && S_ISREG(statf.st_mode));
326 /* bug (mostly harmless): can wrap around if file > 4gb */
327 G.curFileSize = statf.st_size;
Denis Vlasenkobae79482007-01-09 23:42:43 +0000328 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000329#endif
330 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000331
Denis Vlasenkob8934972007-01-04 18:02:32 +0000332 fl.l_whence = SEEK_SET;
333 fl.l_start = 0;
334 fl.l_len = 1;
335 fl.l_type = F_WRLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000336 fcntl(G.logFD, F_SETLKW, &fl);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000337
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000338#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000339 if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) {
340 if (G.logFileRotate) { /* always 0..99 */
341 int i = strlen(G.logFilePath) + 3 + 1;
Denis Vlasenkob8934972007-01-04 18:02:32 +0000342 char oldFile[i];
343 char newFile[i];
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000344 i = G.logFileRotate - 1;
Denis Vlasenkob8934972007-01-04 18:02:32 +0000345 /* rename: f.8 -> f.9; f.7 -> f.8; ... */
346 while (1) {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000347 sprintf(newFile, "%s.%d", G.logFilePath, i);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000348 if (i == 0) break;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000349 sprintf(oldFile, "%s.%d", G.logFilePath, --i);
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000350 xrename(oldFile, newFile);
Eric Andersen29c77f72003-10-09 09:43:18 +0000351 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000352 /* newFile == "f.0" now */
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000353 xrename(G.logFilePath, newFile);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000354 fl.l_type = F_UNLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000355 fcntl(G.logFD, F_SETLKW, &fl);
356 close(G.logFD);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000357 goto reopen;
Eric Andersen29c77f72003-10-09 09:43:18 +0000358 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000359 ftruncate(G.logFD, 0);
Eric Andersen3843e961999-11-25 07:30:46 +0000360 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000361 G.curFileSize +=
Denis Vlasenkob8934972007-01-04 18:02:32 +0000362#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000363 full_write(G.logFD, msg, len);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000364 fl.l_type = F_UNLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000365 fcntl(G.logFD, F_SETLKW, &fl);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000366}
367
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000368static void parse_fac_prio_20(int pri, char *res20)
Eric Andersen3843e961999-11-25 07:30:46 +0000369{
Denis Vlasenko3c8b5ba2007-06-04 18:23:59 +0000370 const CODE *c_pri, *c_fac;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000371
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000372 if (pri != 0) {
Denis Vlasenko14c19402006-09-30 19:20:00 +0000373 c_fac = facilitynames;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000374 while (c_fac->c_name) {
375 if (c_fac->c_val != (LOG_FAC(pri) << 3)) {
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000376 c_fac++;
377 continue;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000378 }
379 /* facility is found, look for prio */
380 c_pri = prioritynames;
381 while (c_pri->c_name) {
382 if (c_pri->c_val != LOG_PRI(pri)) {
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000383 c_pri++;
384 continue;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000385 }
386 snprintf(res20, 20, "%s.%s",
387 c_fac->c_name, c_pri->c_name);
388 return;
389 }
390 /* prio not found, bail out */
391 break;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000392 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000393 snprintf(res20, 20, "<%d>", pri);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000394 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000395}
Eric Andersen3843e961999-11-25 07:30:46 +0000396
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000397/* len parameter is used only for "is there a timestamp?" check.
Denis Vlasenko018b1552007-11-06 01:38:46 +0000398 * NB: some callers cheat and supply len==0 when they know
399 * that there is no timestamp, short-circuiting the test. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000400static void timestamp_and_log(int pri, char *msg, int len)
401{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000402 char *timestamp;
Denis Vlasenko4dada742008-01-03 12:13:42 +0000403 time_t now;
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000404
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000405 if (len < 16 || msg[3] != ' ' || msg[6] != ' '
406 || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
407 ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000408 time(&now);
Denis Vlasenko4dada742008-01-03 12:13:42 +0000409 timestamp = ctime(&now) + 4; /* skip day of week */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000410 } else {
Denis Vlasenko4dada742008-01-03 12:13:42 +0000411 now = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000412 timestamp = msg;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000413 msg += 16;
414 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000415 timestamp[15] = '\0';
Eric Andersen3843e961999-11-25 07:30:46 +0000416
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000417 if (option_mask32 & OPT_small)
418 sprintf(G.printbuf, "%s %s\n", timestamp, msg);
419 else {
420 char res[20];
421 parse_fac_prio_20(pri, res);
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000422 sprintf(G.printbuf, "%s %.64s %s %s\n", timestamp, G.hostname, res, msg);
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000423 }
Denis Vlasenko4dada742008-01-03 12:13:42 +0000424
425 /* Log message locally (to file or shared mem) */
426 log_locally(now, G.printbuf);
427}
428
429static void timestamp_and_log_internal(const char *msg)
430{
431 if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog))
432 return;
433 timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000434}
Glenn L McGrath73ebb882004-09-14 18:12:13 +0000435
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000436/* tmpbuf[len] is a NUL byte (set by caller), but there can be other,
437 * embedded NULs. Split messages on each of these NULs, parse prio,
438 * escape control chars and log each locally. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000439static void split_escape_and_log(char *tmpbuf, int len)
440{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000441 char *p = tmpbuf;
442
443 tmpbuf += len;
444 while (p < tmpbuf) {
445 char c;
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000446 char *q = G.parsebuf;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000447 int pri = (LOG_USER | LOG_NOTICE);
448
449 if (*p == '<') {
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000450 /* Parse the magic priority number */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000451 pri = bb_strtou(p + 1, &p, 10);
Denis Vlasenko018b1552007-11-06 01:38:46 +0000452 if (*p == '>')
453 p++;
454 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000455 pri = (LOG_USER | LOG_NOTICE);
Denis Vlasenko1decd0e2006-09-30 19:17:40 +0000456 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000457
458 while ((c = *p++)) {
459 if (c == '\n')
460 c = ' ';
Denis Vlasenko2ee028d2007-06-21 13:44:53 +0000461 if (!(c & ~0x1f) && c != '\t') {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000462 *q++ = '^';
463 c += '@'; /* ^@, ^A, ^B... */
464 }
465 *q++ = c;
466 }
467 *q = '\0';
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000468
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000469 /* Now log it */
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000470 if (LOG_PRI(pri) < G.logLevel)
471 timestamp_and_log(pri, G.parsebuf, q - G.parsebuf);
Eric Andersen7f94a5c2004-06-22 10:12:59 +0000472 }
Eric Andersen3843e961999-11-25 07:30:46 +0000473}
474
475static void quit_signal(int sig)
476{
Denis Vlasenko4dada742008-01-03 12:13:42 +0000477 timestamp_and_log_internal("syslogd exiting");
Denis Vlasenko1c962f22007-01-09 23:44:57 +0000478 puts("syslogd exiting");
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000479 if (ENABLE_FEATURE_IPC_SYSLOG)
480 ipcsyslog_cleanup();
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000481 kill_myself_with_sig(sig);
Eric Andersen3843e961999-11-25 07:30:46 +0000482}
483
Denis Vlasenko4998c812007-02-14 20:51:46 +0000484#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000485static void do_mark(int sig)
Eric Andersen3843e961999-11-25 07:30:46 +0000486{
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000487 if (G.markInterval) {
Denis Vlasenko4dada742008-01-03 12:13:42 +0000488 timestamp_and_log_internal("-- MARK --");
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000489 alarm(G.markInterval);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000490 }
Eric Andersen3843e961999-11-25 07:30:46 +0000491}
Denis Vlasenko4998c812007-02-14 20:51:46 +0000492#endif
Eric Andersen3843e961999-11-25 07:30:46 +0000493
Denis Vlasenkod031b202007-11-10 01:28:19 +0000494/* Don't inline: prevent struct sockaddr_un to take up space on stack
495 * permanently */
496static NOINLINE int create_socket(void)
Eric Andersen3843e961999-11-25 07:30:46 +0000497{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000498 struct sockaddr_un sunx;
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000499 int sock_fd;
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000500 char *dev_log_name;
Erik Andersenf13df372000-04-18 23:51:51 +0000501
Denis Vlasenkod031b202007-11-10 01:28:19 +0000502 memset(&sunx, 0, sizeof(sunx));
503 sunx.sun_family = AF_UNIX;
504
505 /* Unlink old /dev/log or object it points to. */
506 /* (if it exists, bind will fail) */
507 strcpy(sunx.sun_path, "/dev/log");
508 dev_log_name = xmalloc_follow_symlinks("/dev/log");
509 if (dev_log_name) {
510 safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
511 free(dev_log_name);
512 }
513 unlink(sunx.sun_path);
514
515 sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
516 xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
517 chmod("/dev/log", 0666);
518
519 return sock_fd;
520}
521
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000522#if ENABLE_FEATURE_REMOTE_LOG
523static int try_to_resolve_remote(void)
524{
525 if (!G.remoteAddr) {
526 unsigned now = monotonic_sec();
527
528 /* Don't resolve name too often - DNS timeouts can be big */
529 if ((now - G.last_dns_resolve) < DNS_WAIT_SEC)
530 return -1;
531 G.last_dns_resolve = now;
532 G.remoteAddr = host2sockaddr(G.remoteAddrStr, 514);
533 if (!G.remoteAddr)
534 return -1;
535 }
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000536 return socket(G.remoteAddr->u.sa.sa_family, SOCK_DGRAM, 0);
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000537}
538#endif
539
Denis Vlasenkod031b202007-11-10 01:28:19 +0000540static void do_syslogd(void) ATTRIBUTE_NORETURN;
541static void do_syslogd(void)
542{
543 int sock_fd;
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000544#if ENABLE_FEATURE_SYSLOGD_DUP
545 int last_sz = -1;
546 char *last_buf;
547 char *recvbuf = G.recvbuf;
548#else
549#define recvbuf (G.recvbuf)
550#endif
Denis Vlasenkod031b202007-11-10 01:28:19 +0000551
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000552 /* Set up signal handlers */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000553 bb_signals(0
554 + (1 << SIGINT)
555 + (1 << SIGTERM)
556 + (1 << SIGQUIT)
557 , quit_signal);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000558 signal(SIGHUP, SIG_IGN);
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000559 /* signal(SIGCHLD, SIG_IGN); - why? */
Denis Vlasenko4998c812007-02-14 20:51:46 +0000560#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000561 signal(SIGALRM, do_mark);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000562 alarm(G.markInterval);
Denis Vlasenko4998c812007-02-14 20:51:46 +0000563#endif
Denis Vlasenkod031b202007-11-10 01:28:19 +0000564 sock_fd = create_socket();
Eric Andersenb99df0f1999-11-24 09:04:33 +0000565
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000566 if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000567 ipcsyslog_init();
Eric Andersenea906502001-04-05 20:55:17 +0000568 }
Eric Andersenea906502001-04-05 20:55:17 +0000569
Denis Vlasenko4dada742008-01-03 12:13:42 +0000570 timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000571
Erik Andersen983b51b2000-04-04 18:14:25 +0000572 for (;;) {
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000573 size_t sz;
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000574
575#if ENABLE_FEATURE_SYSLOGD_DUP
576 last_buf = recvbuf;
577 if (recvbuf == G.recvbuf)
578 recvbuf = G.recvbuf + MAX_READ;
579 else
580 recvbuf = G.recvbuf;
581#endif
Denis Vlasenko018b1552007-11-06 01:38:46 +0000582 read_again:
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000583 sz = safe_read(sock_fd, recvbuf, MAX_READ - 1);
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000584 if (sz < 0)
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000585 bb_perror_msg_and_die("read from /dev/log");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000586
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000587 /* Drop trailing '\n' and NULs (typically there is one NUL) */
Denis Vlasenko018b1552007-11-06 01:38:46 +0000588 while (1) {
589 if (sz == 0)
590 goto read_again;
Denis Vlasenkocb12cb22007-11-06 11:34:03 +0000591 /* man 3 syslog says: "A trailing newline is added when needed".
592 * However, neither glibc nor uclibc do this:
593 * syslog(prio, "test") sends "test\0" to /dev/log,
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000594 * syslog(prio, "test\n") sends "test\n\0".
Denis Vlasenkocb12cb22007-11-06 11:34:03 +0000595 * IOW: newline is passed verbatim!
596 * I take it to mean that it's syslogd's job
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000597 * to make those look identical in the log files. */
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000598 if (recvbuf[sz-1] != '\0' && recvbuf[sz-1] != '\n')
Denis Vlasenko018b1552007-11-06 01:38:46 +0000599 break;
600 sz--;
601 }
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000602#if ENABLE_FEATURE_SYSLOGD_DUP
603 if ((option_mask32 & OPT_dup) && (sz == last_sz))
604 if (memcmp(last_buf, recvbuf, sz) == 0)
605 continue;
606 last_sz = sz;
607#endif
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000608#if ENABLE_FEATURE_REMOTE_LOG
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000609 /* We are not modifying log messages in any way before send */
610 /* Remote site cannot trust _us_ anyway and need to do validation again */
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000611 if (G.remoteAddrStr) {
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000612 if (-1 == G.remoteFD) {
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000613 G.remoteFD = try_to_resolve_remote();
614 if (-1 == G.remoteFD)
615 goto no_luck;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000616 }
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000617 /* Stock syslogd sends it '\n'-terminated
618 * over network, mimic that */
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000619 recvbuf[sz] = '\n';
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000620 /* send message to remote logger, ignore possible error */
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000621 /* TODO: on some errors, close and set G.remoteFD to -1
622 * so that DNS resolution and connect is retried? */
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000623 sendto(G.remoteFD, recvbuf, sz+1, MSG_DONTWAIT,
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000624 &G.remoteAddr->u.sa, G.remoteAddr->len);
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000625 no_luck: ;
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000626 }
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000627#endif
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000628 if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000629 recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */
630 split_escape_and_log(recvbuf, sz);
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000631 }
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000632 } /* for (;;) */
Eric Andersenb99df0f1999-11-24 09:04:33 +0000633}
634
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000635int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000636int syslogd_main(int argc, char **argv)
Eric Andersen3843e961999-11-25 07:30:46 +0000637{
Denis Vlasenko14c19402006-09-30 19:20:00 +0000638 char OPTION_DECL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000639
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000640 INIT_G();
641#if ENABLE_FEATURE_REMOTE_LOG
642 G.last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
643#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000644
Eric Andersen394cf222000-12-11 16:48:50 +0000645 /* do normal option parsing */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000646 opt_complementary = "=0"; /* no non-option params */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000647 getopt32(argv, OPTION_STR, OPTION_PARAM);
Denis Vlasenko4998c812007-02-14 20:51:46 +0000648#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000649 if (option_mask32 & OPT_mark) // -m
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000650 G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
Denis Vlasenko4998c812007-02-14 20:51:46 +0000651#endif
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000652 //if (option_mask32 & OPT_nofork) // -n
653 //if (option_mask32 & OPT_outfile) // -O
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000654 if (option_mask32 & OPT_loglevel) // -l
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000655 G.logLevel = xatou_range(opt_l, 1, 8);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000656 //if (option_mask32 & OPT_small) // -S
Denis Vlasenko14c19402006-09-30 19:20:00 +0000657#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000658 if (option_mask32 & OPT_filesize) // -s
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000659 G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000660 if (option_mask32 & OPT_rotatecnt) // -b
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000661 G.logFileRotate = xatou_range(opt_b, 0, 99);
Denis Vlasenko14c19402006-09-30 19:20:00 +0000662#endif
Denis Vlasenko14c19402006-09-30 19:20:00 +0000663#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko218f2f42007-01-24 22:02:01 +0000664 if (opt_C) // -Cn
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000665 G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
Denis Vlasenko14c19402006-09-30 19:20:00 +0000666#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000667
Eric Andersen4ed17822000-12-11 19:28:29 +0000668 /* If they have not specified remote logging, then log locally */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000669 if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_remotelog))
670 option_mask32 |= OPT_locallog;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000671
Erik Andersene49d5ec2000-02-08 19:58:47 +0000672 /* Store away localhost's name before the fork */
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000673 G.hostname = safe_gethostname();
674 *strchrnul(G.hostname, '.') = '\0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000675
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000676 if (!(option_mask32 & OPT_nofork)) {
Denis Vlasenko5a142022007-03-26 13:20:54 +0000677 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
Eric Andersen35e643b2003-07-28 07:40:39 +0000678 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000679 umask(0);
Denis Vlasenko10457b92007-03-27 22:01:31 +0000680 write_pidfile("/var/run/syslogd.pid");
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000681 do_syslogd();
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000682 /* return EXIT_SUCCESS; */
Eric Andersen3843e961999-11-25 07:30:46 +0000683}