blob: 37a119b3d7b2cd66e325cdb2195dcb9204373a3b [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 {
Janne Kiviluotoc897dfe2010-03-31 15:58:58 +020049 MAX_READ = CONFIG_FEATURE_SYSLOGD_READ_BUFFER_SIZE,
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +000050 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
Thomas Geuligd2f77792010-02-28 13:01:59 +010060#if ENABLE_FEATURE_REMOTE_LOG
61typedef struct {
62 int remoteFD;
63 unsigned last_dns_resolve;
64 len_and_sockaddr *remoteAddr;
65 const char *remoteHostname;
66} remoteHost_t;
67#endif
68
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000069/* Allows us to have smaller initializer. Ugly. */
70#define GLOBALS \
71 const char *logFilePath; \
72 int logFD; \
73 /* interval between marks in seconds */ \
74 /*int markInterval;*/ \
75 /* level of messages to be logged */ \
76 int logLevel; \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000077IF_FEATURE_ROTATE_LOGFILE( \
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000078 /* max size of file before rotation */ \
79 unsigned logFileSize; \
80 /* number of rotated message files */ \
81 unsigned logFileRotate; \
82 unsigned curFileSize; \
83 smallint isRegular; \
84) \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000085IF_FEATURE_IPC_SYSLOG( \
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000086 int shmid; /* ipc shared memory id */ \
87 int s_semid; /* ipc semaphore id */ \
88 int shm_size; \
89 struct sembuf SMwup[1]; \
90 struct sembuf SMwdn[3]; \
91)
92
93struct init_globals {
94 GLOBALS
95};
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000096
97struct globals {
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000098 GLOBALS
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +000099
100#if ENABLE_FEATURE_REMOTE_LOG
Thomas Geuligd2f77792010-02-28 13:01:59 +0100101 llist_t *remoteHosts;
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000102#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000103#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000104 struct shbuf_ds *shbuf;
105#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000106 time_t last_log_time;
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000107 /* localhost's name. We print only first 64 chars */
108 char *hostname;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000109
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000110 /* We recv into recvbuf... */
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000111 char recvbuf[MAX_READ * (1 + ENABLE_FEATURE_SYSLOGD_DUP)];
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000112 /* ...then copy to parsebuf, escaping control chars */
113 /* (can grow x2 max) */
114 char parsebuf[MAX_READ*2];
115 /* ...then sprintf into printbuf, adding timestamp (15 chars),
116 * host (64), fac.prio (20) to the message */
117 /* (growth by: 15 + 64 + 20 + delims = ~110) */
118 char printbuf[MAX_READ*2 + 128];
119};
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000120
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000121static const struct init_globals init_data = {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000122 .logFilePath = "/var/log/messages",
123 .logFD = -1,
124#ifdef SYSLOGD_MARK
125 .markInterval = 20 * 60,
126#endif
127 .logLevel = 8,
128#if ENABLE_FEATURE_ROTATE_LOGFILE
129 .logFileSize = 200 * 1024,
130 .logFileRotate = 1,
131#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000132#if ENABLE_FEATURE_IPC_SYSLOG
133 .shmid = -1,
134 .s_semid = -1,
135 .shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024), // default shm size
136 .SMwup = { {1, -1, IPC_NOWAIT} },
137 .SMwdn = { {0, 0}, {1, 0}, {1, +1} },
138#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000139};
140
141#define G (*ptr_to_globals)
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000142#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000143 SET_PTR_TO_GLOBALS(memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data))); \
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000144} while (0)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000145
146
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000147/* Options */
Denis Vlasenko14c19402006-09-30 19:20:00 +0000148enum {
149 OPTBIT_mark = 0, // -m
150 OPTBIT_nofork, // -n
151 OPTBIT_outfile, // -O
152 OPTBIT_loglevel, // -l
153 OPTBIT_small, // -S
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000154 IF_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
155 IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
156 IF_FEATURE_REMOTE_LOG( OPTBIT_remotelog ,) // -R
157 IF_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L
158 IF_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
159 IF_FEATURE_SYSLOGD_DUP( OPTBIT_dup ,) // -D
Denis Vlasenko14c19402006-09-30 19:20:00 +0000160
161 OPT_mark = 1 << OPTBIT_mark ,
162 OPT_nofork = 1 << OPTBIT_nofork ,
163 OPT_outfile = 1 << OPTBIT_outfile ,
164 OPT_loglevel = 1 << OPTBIT_loglevel,
165 OPT_small = 1 << OPTBIT_small ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000166 OPT_filesize = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
167 OPT_rotatecnt = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
168 OPT_remotelog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_remotelog )) + 0,
169 OPT_locallog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0,
170 OPT_circularlog = IF_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
171 OPT_dup = IF_FEATURE_SYSLOGD_DUP( (1 << OPTBIT_dup )) + 0,
Denis Vlasenko14c19402006-09-30 19:20:00 +0000172};
173#define OPTION_STR "m:nO:l:S" \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000174 IF_FEATURE_ROTATE_LOGFILE("s:" ) \
175 IF_FEATURE_ROTATE_LOGFILE("b:" ) \
176 IF_FEATURE_REMOTE_LOG( "R:" ) \
177 IF_FEATURE_REMOTE_LOG( "L" ) \
178 IF_FEATURE_IPC_SYSLOG( "C::") \
179 IF_FEATURE_SYSLOGD_DUP( "D" )
Denis Vlasenko14c19402006-09-30 19:20:00 +0000180#define OPTION_DECL *opt_m, *opt_l \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000181 IF_FEATURE_ROTATE_LOGFILE(,*opt_s) \
182 IF_FEATURE_ROTATE_LOGFILE(,*opt_b) \
183 IF_FEATURE_IPC_SYSLOG( ,*opt_C = NULL)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000184#define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000185 IF_FEATURE_ROTATE_LOGFILE(,&opt_s) \
186 IF_FEATURE_ROTATE_LOGFILE(,&opt_b) \
Thomas Geuligd2f77792010-02-28 13:01:59 +0100187 IF_FEATURE_REMOTE_LOG( ,&remoteAddrList) \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000188 IF_FEATURE_IPC_SYSLOG( ,&opt_C)
Eric Andersen871d93c2002-09-17 20:06:29 +0000189
Eric Andersen871d93c2002-09-17 20:06:29 +0000190
Mark Whitley6317c4b2001-03-12 22:51:50 +0000191/* circular buffer variables/structures */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000192#if ENABLE_FEATURE_IPC_SYSLOG
193
Eric Andersend4a5e252003-12-19 11:32:14 +0000194#if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4
195#error Sorry, you must set the syslogd buffer size to at least 4KB.
196#error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE
197#endif
198
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000199/* our shared key (syslogd.c and logread.c must be in sync) */
200enum { KEY_ID = 0x414e4547 }; /* "GENA" */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000201
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000202static void ipcsyslog_cleanup(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000203{
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000204 if (G.shmid != -1) {
205 shmdt(G.shbuf);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000206 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000207 if (G.shmid != -1) {
208 shmctl(G.shmid, IPC_RMID, NULL);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000209 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000210 if (G.s_semid != -1) {
211 semctl(G.s_semid, 0, IPC_RMID, 0);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000212 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000213}
214
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000215static void ipcsyslog_init(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000216{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000217 if (DEBUG)
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000218 printf("shmget(%x, %d,...)\n", (int)KEY_ID, G.shm_size);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000219
Denis Vlasenkoa1120a82007-08-14 10:27:56 +0000220 G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 0644);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000221 if (G.shmid == -1) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000222 bb_perror_msg_and_die("shmget");
223 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000224
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000225 G.shbuf = shmat(G.shmid, NULL, 0);
Denis Vlasenko4e9ca752008-01-07 15:58:02 +0000226 if (G.shbuf == (void*) -1L) { /* shmat has bizarre error return */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000227 bb_perror_msg_and_die("shmat");
228 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000229
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000230 memset(G.shbuf, 0, G.shm_size);
231 G.shbuf->size = G.shm_size - offsetof(struct shbuf_ds, data) - 1;
232 /*G.shbuf->tail = 0;*/
Mark Whitley6317c4b2001-03-12 22:51:50 +0000233
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000234 // we'll trust the OS to set initial semval to 0 (let's hope)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000235 G.s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023);
236 if (G.s_semid == -1) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000237 if (errno == EEXIST) {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000238 G.s_semid = semget(KEY_ID, 2, 0);
239 if (G.s_semid != -1)
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000240 return;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000241 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000242 bb_perror_msg_and_die("semget");
Mark Whitley6317c4b2001-03-12 22:51:50 +0000243 }
244}
245
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000246/* Write message to shared mem buffer */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000247static void log_to_shmem(const char *msg, int len)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000248{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000249 int old_tail, new_tail;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000250
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000251 if (semop(G.s_semid, G.SMwdn, 3) == -1) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000252 bb_perror_msg_and_die("SMwdn");
253 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000254
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000255 /* Circular Buffer Algorithm:
Mark Whitley6317c4b2001-03-12 22:51:50 +0000256 * --------------------------
Denis Vlasenko150f4022007-01-13 21:06:21 +0000257 * tail == position where to store next syslog message.
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000258 * tail's max value is (shbuf->size - 1)
259 * Last byte of buffer is never used and remains NUL.
Mark Whitley6317c4b2001-03-12 22:51:50 +0000260 */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000261 len++; /* length with NUL included */
262 again:
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000263 old_tail = G.shbuf->tail;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000264 new_tail = old_tail + len;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000265 if (new_tail < G.shbuf->size) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000266 /* store message, set new tail */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000267 memcpy(G.shbuf->data + old_tail, msg, len);
268 G.shbuf->tail = new_tail;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000269 } else {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000270 /* k == available buffer space ahead of old tail */
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000271 int k = G.shbuf->size - old_tail;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000272 /* copy what fits to the end of buffer, and repeat */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000273 memcpy(G.shbuf->data + old_tail, msg, k);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000274 msg += k;
275 len -= k;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000276 G.shbuf->tail = 0;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000277 goto again;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000278 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000279 if (semop(G.s_semid, G.SMwup, 1) == -1) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000280 bb_perror_msg_and_die("SMwup");
281 }
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000282 if (DEBUG)
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000283 printf("tail:%d\n", G.shbuf->tail);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000284}
Rob Landley028ba282006-08-28 20:16:42 +0000285#else
286void ipcsyslog_cleanup(void);
287void ipcsyslog_init(void);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000288void log_to_shmem(const char *msg);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000289#endif /* FEATURE_IPC_SYSLOG */
290
291
Erik Andersen983b51b2000-04-04 18:14:25 +0000292/* Print a message to the log file. */
Denis Vlasenko4dada742008-01-03 12:13:42 +0000293static void log_locally(time_t now, char *msg)
Eric Andersen3843e961999-11-25 07:30:46 +0000294{
Denis Vlasenko0d948202008-12-09 22:53:31 +0000295#ifdef SYSLOGD_WRLOCK
Denis Vlasenkob8934972007-01-04 18:02:32 +0000296 struct flock fl;
Denis Vlasenko0d948202008-12-09 22:53:31 +0000297#endif
Denis Vlasenkob8934972007-01-04 18:02:32 +0000298 int len = strlen(msg);
Eric Andersen3843e961999-11-25 07:30:46 +0000299
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000300#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000301 if ((option_mask32 & OPT_circularlog) && G.shbuf) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000302 log_to_shmem(msg, len);
303 return;
304 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000305#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000306 if (G.logFD >= 0) {
Denis Vlasenkod9415d62009-03-02 14:26:28 +0000307 /* Reopen log file every second. This allows admin
308 * to delete the file and not worry about restarting us.
309 * This costs almost nothing since it happens
310 * _at most_ once a second.
311 */
Denis Vlasenko4dada742008-01-03 12:13:42 +0000312 if (!now)
313 now = time(NULL);
314 if (G.last_log_time != now) {
Denis Vlasenkod9415d62009-03-02 14:26:28 +0000315 G.last_log_time = now;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000316 close(G.logFD);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000317 goto reopen;
318 }
319 } else {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000320 reopen:
Denis Vlasenkod9415d62009-03-02 14:26:28 +0000321 G.logFD = open(G.logFilePath, O_WRONLY | O_CREAT
322 | O_NOCTTY | O_APPEND | O_NONBLOCK,
323 0666);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000324 if (G.logFD < 0) {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000325 /* cannot open logfile? - print to /dev/console then */
Denis Vlasenko70ab28f2007-11-18 05:43:05 +0000326 int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000327 if (fd < 0)
328 fd = 2; /* then stderr, dammit */
329 full_write(fd, msg, len);
330 if (fd != 2)
331 close(fd);
332 return;
333 }
334#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenkobae79482007-01-09 23:42:43 +0000335 {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000336 struct stat statf;
337 G.isRegular = (fstat(G.logFD, &statf) == 0 && S_ISREG(statf.st_mode));
338 /* bug (mostly harmless): can wrap around if file > 4gb */
339 G.curFileSize = statf.st_size;
Denis Vlasenkobae79482007-01-09 23:42:43 +0000340 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000341#endif
342 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000343
Denis Vlasenko0d948202008-12-09 22:53:31 +0000344#ifdef SYSLOGD_WRLOCK
Denis Vlasenkob8934972007-01-04 18:02:32 +0000345 fl.l_whence = SEEK_SET;
346 fl.l_start = 0;
347 fl.l_len = 1;
348 fl.l_type = F_WRLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000349 fcntl(G.logFD, F_SETLKW, &fl);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000350#endif
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000351
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000352#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000353 if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) {
354 if (G.logFileRotate) { /* always 0..99 */
355 int i = strlen(G.logFilePath) + 3 + 1;
Denis Vlasenkob8934972007-01-04 18:02:32 +0000356 char oldFile[i];
357 char newFile[i];
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000358 i = G.logFileRotate - 1;
Denis Vlasenkob8934972007-01-04 18:02:32 +0000359 /* rename: f.8 -> f.9; f.7 -> f.8; ... */
360 while (1) {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000361 sprintf(newFile, "%s.%d", G.logFilePath, i);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000362 if (i == 0) break;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000363 sprintf(oldFile, "%s.%d", G.logFilePath, --i);
Denis Vlasenko69dc3252008-05-24 21:03:53 +0000364 /* ignore errors - file might be missing */
365 rename(oldFile, newFile);
Eric Andersen29c77f72003-10-09 09:43:18 +0000366 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000367 /* newFile == "f.0" now */
Denis Vlasenko69dc3252008-05-24 21:03:53 +0000368 rename(G.logFilePath, newFile);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000369#ifdef SYSLOGD_WRLOCK
Denis Vlasenkob8934972007-01-04 18:02:32 +0000370 fl.l_type = F_UNLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000371 fcntl(G.logFD, F_SETLKW, &fl);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000372#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000373 close(G.logFD);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000374 goto reopen;
Eric Andersen29c77f72003-10-09 09:43:18 +0000375 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000376 ftruncate(G.logFD, 0);
Eric Andersen3843e961999-11-25 07:30:46 +0000377 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000378 G.curFileSize +=
Denis Vlasenkob8934972007-01-04 18:02:32 +0000379#endif
Denis Vlasenko9504e442008-10-29 01:19:15 +0000380 full_write(G.logFD, msg, len);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000381#ifdef SYSLOGD_WRLOCK
Denis Vlasenkob8934972007-01-04 18:02:32 +0000382 fl.l_type = F_UNLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000383 fcntl(G.logFD, F_SETLKW, &fl);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000384#endif
Eric Andersenb99df0f1999-11-24 09:04:33 +0000385}
386
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000387static void parse_fac_prio_20(int pri, char *res20)
Eric Andersen3843e961999-11-25 07:30:46 +0000388{
Denis Vlasenko3c8b5ba2007-06-04 18:23:59 +0000389 const CODE *c_pri, *c_fac;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000390
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000391 if (pri != 0) {
Denis Vlasenko14c19402006-09-30 19:20:00 +0000392 c_fac = facilitynames;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000393 while (c_fac->c_name) {
394 if (c_fac->c_val != (LOG_FAC(pri) << 3)) {
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000395 c_fac++;
396 continue;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000397 }
398 /* facility is found, look for prio */
399 c_pri = prioritynames;
400 while (c_pri->c_name) {
401 if (c_pri->c_val != LOG_PRI(pri)) {
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000402 c_pri++;
403 continue;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000404 }
405 snprintf(res20, 20, "%s.%s",
406 c_fac->c_name, c_pri->c_name);
407 return;
408 }
409 /* prio not found, bail out */
410 break;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000411 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000412 snprintf(res20, 20, "<%d>", pri);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000413 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000414}
Eric Andersen3843e961999-11-25 07:30:46 +0000415
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000416/* len parameter is used only for "is there a timestamp?" check.
Denis Vlasenko018b1552007-11-06 01:38:46 +0000417 * NB: some callers cheat and supply len==0 when they know
418 * that there is no timestamp, short-circuiting the test. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000419static void timestamp_and_log(int pri, char *msg, int len)
420{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000421 char *timestamp;
Denis Vlasenko4dada742008-01-03 12:13:42 +0000422 time_t now;
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000423
Denis Vlasenko574c3162009-04-22 02:53:02 +0000424 /* Jan 18 00:11:22 msg... */
425 /* 01234567890123456 */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000426 if (len < 16 || msg[3] != ' ' || msg[6] != ' '
427 || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
428 ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000429 time(&now);
Denis Vlasenko4dada742008-01-03 12:13:42 +0000430 timestamp = ctime(&now) + 4; /* skip day of week */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000431 } else {
Denis Vlasenko4dada742008-01-03 12:13:42 +0000432 now = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433 timestamp = msg;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000434 msg += 16;
435 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000436 timestamp[15] = '\0';
Eric Andersen3843e961999-11-25 07:30:46 +0000437
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000438 if (option_mask32 & OPT_small)
439 sprintf(G.printbuf, "%s %s\n", timestamp, msg);
440 else {
441 char res[20];
442 parse_fac_prio_20(pri, res);
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000443 sprintf(G.printbuf, "%s %.64s %s %s\n", timestamp, G.hostname, res, msg);
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000444 }
Denis Vlasenko4dada742008-01-03 12:13:42 +0000445
446 /* Log message locally (to file or shared mem) */
447 log_locally(now, G.printbuf);
448}
449
450static void timestamp_and_log_internal(const char *msg)
451{
Denis Vlasenko0d948202008-12-09 22:53:31 +0000452 /* -L, or no -R */
Denis Vlasenko4dada742008-01-03 12:13:42 +0000453 if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog))
454 return;
455 timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000456}
Glenn L McGrath73ebb882004-09-14 18:12:13 +0000457
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000458/* tmpbuf[len] is a NUL byte (set by caller), but there can be other,
459 * embedded NULs. Split messages on each of these NULs, parse prio,
460 * escape control chars and log each locally. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000461static void split_escape_and_log(char *tmpbuf, int len)
462{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000463 char *p = tmpbuf;
464
465 tmpbuf += len;
466 while (p < tmpbuf) {
467 char c;
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000468 char *q = G.parsebuf;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000469 int pri = (LOG_USER | LOG_NOTICE);
470
471 if (*p == '<') {
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000472 /* Parse the magic priority number */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000473 pri = bb_strtou(p + 1, &p, 10);
Denis Vlasenko018b1552007-11-06 01:38:46 +0000474 if (*p == '>')
475 p++;
476 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000477 pri = (LOG_USER | LOG_NOTICE);
Denis Vlasenko1decd0e2006-09-30 19:17:40 +0000478 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000479
480 while ((c = *p++)) {
481 if (c == '\n')
482 c = ' ';
Denis Vlasenko2ee028d2007-06-21 13:44:53 +0000483 if (!(c & ~0x1f) && c != '\t') {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000484 *q++ = '^';
485 c += '@'; /* ^@, ^A, ^B... */
486 }
487 *q++ = c;
488 }
489 *q = '\0';
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000490
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000491 /* Now log it */
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000492 if (LOG_PRI(pri) < G.logLevel)
493 timestamp_and_log(pri, G.parsebuf, q - G.parsebuf);
Eric Andersen7f94a5c2004-06-22 10:12:59 +0000494 }
Eric Andersen3843e961999-11-25 07:30:46 +0000495}
496
Denis Vlasenko4998c812007-02-14 20:51:46 +0000497#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000498static void do_mark(int sig)
Eric Andersen3843e961999-11-25 07:30:46 +0000499{
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000500 if (G.markInterval) {
Denis Vlasenko4dada742008-01-03 12:13:42 +0000501 timestamp_and_log_internal("-- MARK --");
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000502 alarm(G.markInterval);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000503 }
Eric Andersen3843e961999-11-25 07:30:46 +0000504}
Denis Vlasenko4998c812007-02-14 20:51:46 +0000505#endif
Eric Andersen3843e961999-11-25 07:30:46 +0000506
Denis Vlasenkod031b202007-11-10 01:28:19 +0000507/* Don't inline: prevent struct sockaddr_un to take up space on stack
508 * permanently */
509static NOINLINE int create_socket(void)
Eric Andersen3843e961999-11-25 07:30:46 +0000510{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000511 struct sockaddr_un sunx;
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000512 int sock_fd;
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000513 char *dev_log_name;
Erik Andersenf13df372000-04-18 23:51:51 +0000514
Denis Vlasenkod031b202007-11-10 01:28:19 +0000515 memset(&sunx, 0, sizeof(sunx));
516 sunx.sun_family = AF_UNIX;
517
518 /* Unlink old /dev/log or object it points to. */
519 /* (if it exists, bind will fail) */
520 strcpy(sunx.sun_path, "/dev/log");
521 dev_log_name = xmalloc_follow_symlinks("/dev/log");
522 if (dev_log_name) {
523 safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
524 free(dev_log_name);
525 }
526 unlink(sunx.sun_path);
527
528 sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
529 xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
530 chmod("/dev/log", 0666);
531
532 return sock_fd;
533}
534
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000535#if ENABLE_FEATURE_REMOTE_LOG
Thomas Geuligd2f77792010-02-28 13:01:59 +0100536static int try_to_resolve_remote(remoteHost_t *rh)
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000537{
Thomas Geuligd2f77792010-02-28 13:01:59 +0100538 if (!rh->remoteAddr) {
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000539 unsigned now = monotonic_sec();
540
541 /* Don't resolve name too often - DNS timeouts can be big */
Thomas Geuligd2f77792010-02-28 13:01:59 +0100542 if ((now - rh->last_dns_resolve) < DNS_WAIT_SEC)
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000543 return -1;
Thomas Geuligd2f77792010-02-28 13:01:59 +0100544 rh->last_dns_resolve = now;
545 rh->remoteAddr = host2sockaddr(rh->remoteHostname, 514);
546 if (!rh->remoteAddr)
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000547 return -1;
548 }
Daniel Dickinsone74d7982010-08-03 04:26:20 +0200549 return xsocket(rh->remoteAddr->u.sa.sa_family, SOCK_DGRAM, 0);
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000550}
551#endif
552
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000553static void do_syslogd(void) NORETURN;
Denis Vlasenkod031b202007-11-10 01:28:19 +0000554static void do_syslogd(void)
555{
556 int sock_fd;
Thomas Geuligd2f77792010-02-28 13:01:59 +0100557#if ENABLE_FEATURE_REMOTE_LOG
558 llist_t *item;
559#endif
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000560#if ENABLE_FEATURE_SYSLOGD_DUP
561 int last_sz = -1;
562 char *last_buf;
563 char *recvbuf = G.recvbuf;
564#else
565#define recvbuf (G.recvbuf)
566#endif
Denis Vlasenkod031b202007-11-10 01:28:19 +0000567
Denis Vlasenko0d948202008-12-09 22:53:31 +0000568 /* Set up signal handlers (so that they interrupt read()) */
569 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
570 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
571 //signal_no_SA_RESTART_empty_mask(SIGQUIT, record_signo);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000572 signal(SIGHUP, SIG_IGN);
Denis Vlasenko4998c812007-02-14 20:51:46 +0000573#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000574 signal(SIGALRM, do_mark);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000575 alarm(G.markInterval);
Denis Vlasenko4998c812007-02-14 20:51:46 +0000576#endif
Denis Vlasenkod031b202007-11-10 01:28:19 +0000577 sock_fd = create_socket();
Eric Andersenb99df0f1999-11-24 09:04:33 +0000578
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000579 if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000580 ipcsyslog_init();
Eric Andersenea906502001-04-05 20:55:17 +0000581 }
Eric Andersenea906502001-04-05 20:55:17 +0000582
Denis Vlasenko4dada742008-01-03 12:13:42 +0000583 timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000584
Denis Vlasenko0d948202008-12-09 22:53:31 +0000585 while (!bb_got_signal) {
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000586 ssize_t sz;
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000587
588#if ENABLE_FEATURE_SYSLOGD_DUP
589 last_buf = recvbuf;
590 if (recvbuf == G.recvbuf)
591 recvbuf = G.recvbuf + MAX_READ;
592 else
593 recvbuf = G.recvbuf;
594#endif
Denis Vlasenko018b1552007-11-06 01:38:46 +0000595 read_again:
Denis Vlasenko0d948202008-12-09 22:53:31 +0000596 sz = read(sock_fd, recvbuf, MAX_READ - 1);
597 if (sz < 0) {
598 if (!bb_got_signal)
599 bb_perror_msg("read from /dev/log");
600 break;
601 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000602
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000603 /* Drop trailing '\n' and NULs (typically there is one NUL) */
Denis Vlasenko018b1552007-11-06 01:38:46 +0000604 while (1) {
605 if (sz == 0)
606 goto read_again;
Denis Vlasenkocb12cb22007-11-06 11:34:03 +0000607 /* man 3 syslog says: "A trailing newline is added when needed".
608 * However, neither glibc nor uclibc do this:
609 * syslog(prio, "test") sends "test\0" to /dev/log,
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000610 * syslog(prio, "test\n") sends "test\n\0".
Denis Vlasenkocb12cb22007-11-06 11:34:03 +0000611 * IOW: newline is passed verbatim!
612 * I take it to mean that it's syslogd's job
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000613 * to make those look identical in the log files. */
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000614 if (recvbuf[sz-1] != '\0' && recvbuf[sz-1] != '\n')
Denis Vlasenko018b1552007-11-06 01:38:46 +0000615 break;
616 sz--;
617 }
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000618#if ENABLE_FEATURE_SYSLOGD_DUP
619 if ((option_mask32 & OPT_dup) && (sz == last_sz))
620 if (memcmp(last_buf, recvbuf, sz) == 0)
621 continue;
622 last_sz = sz;
623#endif
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000624#if ENABLE_FEATURE_REMOTE_LOG
Thomas Geuligd2f77792010-02-28 13:01:59 +0100625 /* Stock syslogd sends it '\n'-terminated
626 * over network, mimic that */
627 recvbuf[sz] = '\n';
628
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000629 /* We are not modifying log messages in any way before send */
630 /* Remote site cannot trust _us_ anyway and need to do validation again */
Thomas Geuligd2f77792010-02-28 13:01:59 +0100631 for (item = G.remoteHosts; item != NULL; item = item->link) {
632 remoteHost_t *rh = (remoteHost_t *)item->data;
633
634 if (rh->remoteFD == -1) {
635 rh->remoteFD = try_to_resolve_remote(rh);
636 if (rh->remoteFD == -1)
637 continue;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000638 }
Daniel Dickinsone74d7982010-08-03 04:26:20 +0200639
640 /* Send message to remote logger.
641 * On some errors, close and set remoteFD to -1
642 * so that DNS resolution is retried.
643 */
644 if (sendto(rh->remoteFD, recvbuf, sz+1,
645 MSG_DONTWAIT | MSG_NOSIGNAL,
646 &(rh->remoteAddr->u.sa), rh->remoteAddr->len) == -1
647 ) {
648 switch (errno) {
649 case ECONNRESET:
650 case ENOTCONN: /* paranoia */
651 case EPIPE:
652 close(rh->remoteFD);
653 rh->remoteFD = -1;
654 free(rh->remoteAddr);
655 rh->remoteAddr = NULL;
656 }
657 }
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000658 }
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000659#endif
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000660 if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
Denis Vlasenkobe048f22008-02-26 20:13:52 +0000661 recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */
662 split_escape_and_log(recvbuf, sz);
Denis Vlasenko75cddd82008-02-13 09:19:14 +0000663 }
Denis Vlasenko0d948202008-12-09 22:53:31 +0000664 } /* while (!bb_got_signal) */
665
666 timestamp_and_log_internal("syslogd exiting");
667 puts("syslogd exiting");
668 if (ENABLE_FEATURE_IPC_SYSLOG)
669 ipcsyslog_cleanup();
670 kill_myself_with_sig(bb_got_signal);
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000671#undef recvbuf
Eric Andersenb99df0f1999-11-24 09:04:33 +0000672}
673
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000674int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000675int syslogd_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen3843e961999-11-25 07:30:46 +0000676{
Denis Vlasenko0d948202008-12-09 22:53:31 +0000677 int opts;
Thomas Geuligd2f77792010-02-28 13:01:59 +0100678 char OPTION_DECL;
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000679#if ENABLE_FEATURE_REMOTE_LOG
Thomas Geuligd2f77792010-02-28 13:01:59 +0100680 llist_t *remoteAddrList = NULL;
Denis Vlasenko4f2e8bc2008-01-03 12:12:27 +0000681#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000682
Thomas Geuligd2f77792010-02-28 13:01:59 +0100683 INIT_G();
684
685 /* No non-option params, -R can occur multiple times */
686 opt_complementary = "=0" IF_FEATURE_REMOTE_LOG(":R::");
Denis Vlasenko0d948202008-12-09 22:53:31 +0000687 opts = getopt32(argv, OPTION_STR, OPTION_PARAM);
Thomas Geuligd2f77792010-02-28 13:01:59 +0100688#if ENABLE_FEATURE_REMOTE_LOG
689 while (remoteAddrList) {
690 remoteHost_t *rh = xzalloc(sizeof(*rh));
691 rh->remoteHostname = llist_pop(&remoteAddrList);
692 rh->remoteFD = -1;
693 rh->last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
694 llist_add_to(&G.remoteHosts, rh);
695 }
696#endif
697
Denis Vlasenko4998c812007-02-14 20:51:46 +0000698#ifdef SYSLOGD_MARK
Denis Vlasenko0d948202008-12-09 22:53:31 +0000699 if (opts & OPT_mark) // -m
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000700 G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
Denis Vlasenko4998c812007-02-14 20:51:46 +0000701#endif
Denis Vlasenko0d948202008-12-09 22:53:31 +0000702 //if (opts & OPT_nofork) // -n
703 //if (opts & OPT_outfile) // -O
704 if (opts & OPT_loglevel) // -l
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000705 G.logLevel = xatou_range(opt_l, 1, 8);
Denis Vlasenko0d948202008-12-09 22:53:31 +0000706 //if (opts & OPT_small) // -S
Denis Vlasenko14c19402006-09-30 19:20:00 +0000707#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenko0d948202008-12-09 22:53:31 +0000708 if (opts & OPT_filesize) // -s
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000709 G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
Denis Vlasenko0d948202008-12-09 22:53:31 +0000710 if (opts & OPT_rotatecnt) // -b
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000711 G.logFileRotate = xatou_range(opt_b, 0, 99);
Denis Vlasenko14c19402006-09-30 19:20:00 +0000712#endif
Denis Vlasenko14c19402006-09-30 19:20:00 +0000713#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko218f2f42007-01-24 22:02:01 +0000714 if (opt_C) // -Cn
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000715 G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
Denis Vlasenko14c19402006-09-30 19:20:00 +0000716#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000717
Eric Andersen4ed17822000-12-11 19:28:29 +0000718 /* If they have not specified remote logging, then log locally */
Denis Vlasenko0d948202008-12-09 22:53:31 +0000719 if (ENABLE_FEATURE_REMOTE_LOG && !(opts & OPT_remotelog)) // -R
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000720 option_mask32 |= OPT_locallog;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000721
Erik Andersene49d5ec2000-02-08 19:58:47 +0000722 /* Store away localhost's name before the fork */
Denis Vlasenko6f1713f2008-02-25 23:23:58 +0000723 G.hostname = safe_gethostname();
724 *strchrnul(G.hostname, '.') = '\0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000725
Denis Vlasenko0d948202008-12-09 22:53:31 +0000726 if (!(opts & OPT_nofork)) {
Denis Vlasenko5a142022007-03-26 13:20:54 +0000727 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
Eric Andersen35e643b2003-07-28 07:40:39 +0000728 }
Denys Vlasenkoa4252422010-01-29 16:44:48 +0100729 //umask(0); - why??
Denis Vlasenko10457b92007-03-27 22:01:31 +0000730 write_pidfile("/var/run/syslogd.pid");
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000731 do_syslogd();
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000732 /* return EXIT_SUCCESS; */
Eric Andersen3843e961999-11-25 07:30:46 +0000733}
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000734
735/* Clean up. Needed because we are included from syslogd_and_logger.c */
Denis Vlasenko0d948202008-12-09 22:53:31 +0000736#undef DEBUG
737#undef SYSLOGD_MARK
738#undef SYSLOGD_WRLOCK
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +0000739#undef G
740#undef GLOBALS
741#undef INIT_G
742#undef OPTION_STR
743#undef OPTION_DECL
744#undef OPTION_PARAM