blob: 716e41f5f319c5df2e0c6a4e7f8ec81c89bb1959 [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
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000016#include "busybox.h"
Eric Andersenb186d981999-12-03 09:19:54 +000017#include <paths.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000018#include <sys/un.h>
Eric Andersenb186d981999-12-03 09:19:54 +000019
Eric Andersen3843e961999-11-25 07:30:46 +000020/* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
21#define SYSLOG_NAMES
22#include <sys/syslog.h>
Eric Andersenced2cef2000-07-20 23:41:24 +000023#include <sys/uio.h>
Eric Andersen3843e961999-11-25 07:30:46 +000024
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000025#define DEBUG 0
26
Erik Andersen983b51b2000-04-04 18:14:25 +000027/* Path to the unix socket */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000028static char *dev_log_name;
Eric Andersen3843e961999-11-25 07:30:46 +000029
Denis Vlasenko14c19402006-09-30 19:20:00 +000030/* Path for the file where all log messages are written */
31static const char *logFilePath = "/var/log/messages";
Denis Vlasenkob8934972007-01-04 18:02:32 +000032static int logFD = -1;
Erik Andersene49d5ec2000-02-08 19:58:47 +000033
Eric Andersen3843e961999-11-25 07:30:46 +000034/* interval between marks in seconds */
Denis Vlasenkoceab8702007-01-04 17:57:54 +000035static int markInterval = 20 * 60;
Erik Andersene49d5ec2000-02-08 19:58:47 +000036
Denis Vlasenko1decd0e2006-09-30 19:17:40 +000037/* level of messages to be locally logged */
38static int logLevel = 8;
39
Eric Andersen3843e961999-11-25 07:30:46 +000040/* localhost's name */
Denis Vlasenkoceab8702007-01-04 17:57:54 +000041static char localHostName[64];
Eric Andersen3843e961999-11-25 07:30:46 +000042
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000043#if ENABLE_FEATURE_ROTATE_LOGFILE
44/* max size of message file before being rotated */
Denis Vlasenkob8934972007-01-04 18:02:32 +000045static unsigned logFileSize = 200 * 1024;
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000046/* number of rotated message files */
Denis Vlasenkob8934972007-01-04 18:02:32 +000047static unsigned logFileRotate = 1;
48static unsigned curFileSize;
49static smallint isRegular;
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000050#endif
51
Denis Vlasenkoceab8702007-01-04 17:57:54 +000052#if ENABLE_FEATURE_REMOTE_LOG
Eric Andersenced2cef2000-07-20 23:41:24 +000053#include <netinet/in.h>
54/* udp socket for logging to remote host */
Denis Vlasenkoceab8702007-01-04 17:57:54 +000055static int remoteFD = -1;
Denis Vlasenkobbe51462007-01-10 20:19:56 +000056static len_and_sockaddr* remoteAddr;
Eric Andersenced2cef2000-07-20 23:41:24 +000057#endif
58
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +000059/* We are using bb_common_bufsiz1 for buffering: */
60enum { MAX_READ = (BUFSIZ/6) & ~0xf };
Denis Vlasenko8a820b22007-01-06 22:08:53 +000061/* We recv into RECVBUF... (size: MAX_READ ~== BUFSIZ/6) */
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +000062#define RECVBUF bb_common_bufsiz1
Denis Vlasenko8a820b22007-01-06 22:08:53 +000063/* ...then copy to PARSEBUF, escaping control chars */
64/* (can grow x2 max ~== BUFSIZ/3) */
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +000065#define PARSEBUF (bb_common_bufsiz1 + MAX_READ)
Denis Vlasenko8a820b22007-01-06 22:08:53 +000066/* ...then sprintf into PRINTBUF, adding timestamp (15 chars),
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +000067 * host (64), fac.prio (20) to the message */
68/* (growth by: 15 + 64 + 20 + delims = ~110) */
Denis Vlasenko8a820b22007-01-06 22:08:53 +000069#define PRINTBUF (bb_common_bufsiz1 + 3*MAX_READ)
70/* totals: BUFSIZ == BUFSIZ/6 + BUFSIZ/3 + (BUFSIZ/3+BUFSIZ/6)
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +000071 * -- we have BUFSIZ/6 extra at the ent of PRINTBUF
72 * which covers needed ~110 extra bytes (and much more) */
Denis Vlasenkoceab8702007-01-04 17:57:54 +000073
74
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +000075/* Options */
Denis Vlasenko14c19402006-09-30 19:20:00 +000076enum {
77 OPTBIT_mark = 0, // -m
78 OPTBIT_nofork, // -n
79 OPTBIT_outfile, // -O
80 OPTBIT_loglevel, // -l
81 OPTBIT_small, // -S
82 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
83 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
84 USE_FEATURE_REMOTE_LOG( OPTBIT_remote ,) // -R
85 USE_FEATURE_REMOTE_LOG( OPTBIT_localtoo ,) // -L
86 USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
87
88 OPT_mark = 1 << OPTBIT_mark ,
89 OPT_nofork = 1 << OPTBIT_nofork ,
90 OPT_outfile = 1 << OPTBIT_outfile ,
91 OPT_loglevel = 1 << OPTBIT_loglevel,
92 OPT_small = 1 << OPTBIT_small ,
93 OPT_filesize = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
94 OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
95 OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remote )) + 0,
96 OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_localtoo )) + 0,
97 OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
98};
99#define OPTION_STR "m:nO:l:S" \
100 USE_FEATURE_ROTATE_LOGFILE("s:" ) \
101 USE_FEATURE_ROTATE_LOGFILE("b:" ) \
102 USE_FEATURE_REMOTE_LOG( "R:" ) \
103 USE_FEATURE_REMOTE_LOG( "L" ) \
104 USE_FEATURE_IPC_SYSLOG( "C::")
105#define OPTION_DECL *opt_m, *opt_l \
106 USE_FEATURE_ROTATE_LOGFILE(,*opt_s) \
107 USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \
108 USE_FEATURE_REMOTE_LOG( ,*opt_R) \
109 USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL)
110#define OPTION_PARAM &opt_m, &logFilePath, &opt_l \
111 USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \
112 USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \
113 USE_FEATURE_REMOTE_LOG( ,&opt_R) \
114 USE_FEATURE_IPC_SYSLOG( ,&opt_C)
Eric Andersen871d93c2002-09-17 20:06:29 +0000115
Eric Andersen871d93c2002-09-17 20:06:29 +0000116
Mark Whitley6317c4b2001-03-12 22:51:50 +0000117/* circular buffer variables/structures */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000118#if ENABLE_FEATURE_IPC_SYSLOG
119
Eric Andersend4a5e252003-12-19 11:32:14 +0000120
121#if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4
122#error Sorry, you must set the syslogd buffer size to at least 4KB.
123#error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE
124#endif
125
Mark Whitley6317c4b2001-03-12 22:51:50 +0000126#include <sys/ipc.h>
127#include <sys/sem.h>
128#include <sys/shm.h>
129
130/* our shared key */
Denis Vlasenko14c19402006-09-30 19:20:00 +0000131#define KEY_ID ((long)0x414e4547) /* "GENA" */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000132
133// Semaphore operation structures
134static struct shbuf_ds {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000135 int32_t size; // size of data written
136 int32_t head; // start of message list
137 int32_t tail; // end of message list
Denis Vlasenko14c19402006-09-30 19:20:00 +0000138 char data[1]; // data/messages
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000139} *shbuf; // shared memory pointer
Mark Whitley6317c4b2001-03-12 22:51:50 +0000140
Denis Vlasenko14c19402006-09-30 19:20:00 +0000141static int shmid = -1; // ipc shared memory id
142static int s_semid = -1; // ipc semaphore id
Eric Andersend4a5e252003-12-19 11:32:14 +0000143static int shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024); // default shm size
Eric Andersen871d93c2002-09-17 20:06:29 +0000144
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000145static void ipcsyslog_cleanup(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000146{
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000147 if (shmid != -1) {
Denis Vlasenko14c19402006-09-30 19:20:00 +0000148 shmdt(shbuf);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000149 }
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000150 if (shmid != -1) {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000151 shmctl(shmid, IPC_RMID, NULL);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000152 }
153 if (s_semid != -1) {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000154 semctl(s_semid, 0, IPC_RMID, 0);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000155 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000156}
157
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000158static void ipcsyslog_init(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000159{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000160 if (DEBUG)
161 printf("shmget(%lx, %d,...)\n", KEY_ID, shm_size);
162
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000163 shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023);
164 if (shmid == -1) {
165 bb_perror_msg_and_die("shmget");
166 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000167
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000168 shbuf = shmat(shmid, NULL, 0);
169 if (!shbuf) {
170 bb_perror_msg_and_die("shmat");
171 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000172
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000173 shbuf->size = shm_size - offsetof(struct shbuf_ds, data);
174 shbuf->head = shbuf->tail = 0;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000175
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000176 // we'll trust the OS to set initial semval to 0 (let's hope)
177 s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023);
178 if (s_semid == -1) {
179 if (errno == EEXIST) {
180 s_semid = semget(KEY_ID, 2, 0);
181 if (s_semid != -1)
182 return;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000183 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000184 bb_perror_msg_and_die("semget");
Mark Whitley6317c4b2001-03-12 22:51:50 +0000185 }
186}
187
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000188/* Write message to shared mem buffer */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000189static void log_to_shmem(const char *msg, int len)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000190{
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000191 /* Why libc insists on these being rw? */
192 static struct sembuf SMwup[1] = { {1, -1, IPC_NOWAIT} };
193 static struct sembuf SMwdn[3] = { {0, 0}, {1, 0}, {1, +1} };
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000194
195 int old_tail, new_tail;
196 char *c;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000197
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000198 if (semop(s_semid, SMwdn, 3) == -1) {
199 bb_perror_msg_and_die("SMwdn");
200 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000201
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000202 /* Circular Buffer Algorithm:
Mark Whitley6317c4b2001-03-12 22:51:50 +0000203 * --------------------------
Denis Vlasenko150f4022007-01-13 21:06:21 +0000204 * tail == position where to store next syslog message.
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000205 * head == position of next message to retrieve ("print").
206 * if head == tail, there is no "unprinted" messages left.
207 * head is typically advanced by separate "reader" program,
208 * but if there isn't one, we have to do it ourself.
209 * messages are NUL-separated.
Mark Whitley6317c4b2001-03-12 22:51:50 +0000210 */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000211 len++; /* length with NUL included */
212 again:
213 old_tail = shbuf->tail;
214 new_tail = old_tail + len;
215 if (new_tail < shbuf->size) {
216 /* No need to move head if shbuf->head <= old_tail,
217 * else... */
218 if (old_tail < shbuf->head && shbuf->head <= new_tail) {
219 /* ...need to move head forward */
220 c = memchr(shbuf->data + new_tail, '\0',
221 shbuf->size - new_tail);
222 if (!c) /* no NUL ahead of us, wrap around */
223 c = memchr(shbuf->data, '\0', old_tail);
224 if (!c) { /* still nothing? point to this msg... */
225 shbuf->head = old_tail;
226 } else {
227 /* convert pointer to offset + skip NUL */
228 shbuf->head = c - shbuf->data + 1;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000229 }
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000230 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000231 /* store message, set new tail */
232 memcpy(shbuf->data + old_tail, msg, len);
233 shbuf->tail = new_tail;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000234 } else {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000235 /* we need to break up the message and wrap it around */
236 /* k == available buffer space ahead of old tail */
237 int k = shbuf->size - old_tail - 1;
238 if (shbuf->head > old_tail) {
239 /* we are going to overwrite head, need to
240 * move it out of the way */
241 c = memchr(shbuf->data, '\0', old_tail);
242 if (!c) { /* nothing? point to this msg... */
243 shbuf->head = old_tail;
244 } else { /* convert pointer to offset + skip NUL */
245 shbuf->head = c - shbuf->data + 1;
246 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000247 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000248 /* copy what fits to the end of buffer, and repeat */
249 memcpy(shbuf->data + old_tail, msg, k);
250 msg += k;
251 len -= k;
252 shbuf->tail = 0;
253 goto again;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000254 }
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000255 if (semop(s_semid, SMwup, 1) == -1) {
256 bb_perror_msg_and_die("SMwup");
257 }
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000258 if (DEBUG)
259 printf("head:%d tail:%d\n", shbuf->head, shbuf->tail);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000260}
Rob Landley028ba282006-08-28 20:16:42 +0000261#else
262void ipcsyslog_cleanup(void);
263void ipcsyslog_init(void);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000264void log_to_shmem(const char *msg);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000265#endif /* FEATURE_IPC_SYSLOG */
266
267
Erik Andersen983b51b2000-04-04 18:14:25 +0000268/* Print a message to the log file. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000269static void log_locally(char *msg)
Eric Andersen3843e961999-11-25 07:30:46 +0000270{
Denis Vlasenkob8934972007-01-04 18:02:32 +0000271 static time_t last;
Denis Vlasenkob8934972007-01-04 18:02:32 +0000272 struct flock fl;
273 int len = strlen(msg);
Eric Andersen3843e961999-11-25 07:30:46 +0000274
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000275#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000276 if ((option_mask32 & OPT_circularlog) && shbuf) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000277 log_to_shmem(msg, len);
278 return;
279 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000280#endif
Denis Vlasenkob8934972007-01-04 18:02:32 +0000281 if (logFD >= 0) {
282 time_t cur;
283 time(&cur);
284 if (last != cur) {
285 last = cur; /* reopen log file every second */
286 close(logFD);
287 goto reopen;
288 }
289 } else {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000290 reopen:
291 logFD = device_open(logFilePath, O_WRONLY | O_CREAT
Denis Vlasenkobd8f43d2006-09-08 17:31:55 +0000292 | O_NOCTTY | O_APPEND | O_NONBLOCK);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000293 if (logFD < 0) {
294 /* cannot open logfile? - print to /dev/console then */
295 int fd = device_open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
296 if (fd < 0)
297 fd = 2; /* then stderr, dammit */
298 full_write(fd, msg, len);
299 if (fd != 2)
300 close(fd);
301 return;
302 }
303#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenkobae79482007-01-09 23:42:43 +0000304 {
305 struct stat statf;
306
Denis Vlasenkob8934972007-01-04 18:02:32 +0000307 isRegular = (fstat(logFD, &statf) == 0 && (statf.st_mode & S_IFREG));
308 /* bug (mostly harmless): can wrap around if file > 4gb */
309 curFileSize = statf.st_size;
Denis Vlasenkobae79482007-01-09 23:42:43 +0000310 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000311#endif
312 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000313
Denis Vlasenkob8934972007-01-04 18:02:32 +0000314 fl.l_whence = SEEK_SET;
315 fl.l_start = 0;
316 fl.l_len = 1;
317 fl.l_type = F_WRLCK;
318 fcntl(logFD, F_SETLKW, &fl);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000319
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000320#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenkob8934972007-01-04 18:02:32 +0000321 if (logFileSize && isRegular && curFileSize > logFileSize) {
322 if (logFileRotate) { /* always 0..99 */
323 int i = strlen(logFilePath) + 3 + 1;
324 char oldFile[i];
325 char newFile[i];
326 i = logFileRotate - 1;
327 /* rename: f.8 -> f.9; f.7 -> f.8; ... */
328 while (1) {
329 sprintf(newFile, "%s.%d", logFilePath, i);
330 if (i == 0) break;
331 sprintf(oldFile, "%s.%d", logFilePath, --i);
332 rename(oldFile, newFile);
Eric Andersen29c77f72003-10-09 09:43:18 +0000333 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000334 /* newFile == "f.0" now */
335 rename(logFilePath, newFile);
336 fl.l_type = F_UNLCK;
337 fcntl(logFD, F_SETLKW, &fl);
338 close(logFD);
339 goto reopen;
Eric Andersen29c77f72003-10-09 09:43:18 +0000340 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000341 ftruncate(logFD, 0);
Eric Andersen3843e961999-11-25 07:30:46 +0000342 }
Denis Vlasenkodb1edde2007-01-09 23:39:48 +0000343 curFileSize +=
Denis Vlasenkob8934972007-01-04 18:02:32 +0000344#endif
Denis Vlasenkodb1edde2007-01-09 23:39:48 +0000345 full_write(logFD, msg, len);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000346 fl.l_type = F_UNLCK;
347 fcntl(logFD, F_SETLKW, &fl);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000348}
349
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000350static void parse_fac_prio_20(int pri, char *res20)
Eric Andersen3843e961999-11-25 07:30:46 +0000351{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000352 CODE *c_pri, *c_fac;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000353
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000354 if (pri != 0) {
Denis Vlasenko14c19402006-09-30 19:20:00 +0000355 c_fac = facilitynames;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000356 while (c_fac->c_name) {
357 if (c_fac->c_val != (LOG_FAC(pri) << 3)) {
358 c_fac++; continue;
359 }
360 /* facility is found, look for prio */
361 c_pri = prioritynames;
362 while (c_pri->c_name) {
363 if (c_pri->c_val != LOG_PRI(pri)) {
364 c_pri++; continue;
365 }
366 snprintf(res20, 20, "%s.%s",
367 c_fac->c_name, c_pri->c_name);
368 return;
369 }
370 /* prio not found, bail out */
371 break;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000372 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000373 snprintf(res20, 20, "<%d>", pri);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000374 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000375}
Eric Andersen3843e961999-11-25 07:30:46 +0000376
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000377/* len parameter is used only for "is there a timestamp?" check.
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000378 * NB: some callers cheat and supply 0 when they know
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000379 * that there is no timestamp, short-cutting the test. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000380static void timestamp_and_log(int pri, char *msg, int len)
381{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000382 char *timestamp;
383
384 if (len < 16 || msg[3] != ' ' || msg[6] != ' '
385 || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
386 ) {
Denis Vlasenkobae79482007-01-09 23:42:43 +0000387 time_t now;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000388 time(&now);
389 timestamp = ctime(&now) + 4;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000390 } else {
391 timestamp = msg;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000392 msg += 16;
393 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000394 timestamp[15] = '\0';
Eric Andersen3843e961999-11-25 07:30:46 +0000395
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000396 /* Log message locally (to file or shared mem) */
397 if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
398 if (LOG_PRI(pri) < logLevel) {
399 if (option_mask32 & OPT_small)
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000400 sprintf(PRINTBUF, "%s %s\n", timestamp, msg);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000401 else {
402 char res[20];
403 parse_fac_prio_20(pri, res);
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000404 sprintf(PRINTBUF, "%s %s %s %s\n", timestamp, localHostName, res, msg);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000405 }
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000406 log_locally(PRINTBUF);
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000407 }
408 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000409}
Glenn L McGrath73ebb882004-09-14 18:12:13 +0000410
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000411static void split_escape_and_log(char *tmpbuf, int len)
412{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000413 char *p = tmpbuf;
414
415 tmpbuf += len;
416 while (p < tmpbuf) {
417 char c;
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000418 char *q = PARSEBUF;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000419 int pri = (LOG_USER | LOG_NOTICE);
420
421 if (*p == '<') {
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000422 /* Parse the magic priority number */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000423 pri = bb_strtou(p + 1, &p, 10);
424 if (*p == '>') p++;
425 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) {
426 pri = (LOG_USER | LOG_NOTICE);
427 }
Denis Vlasenko1decd0e2006-09-30 19:17:40 +0000428 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000429
430 while ((c = *p++)) {
431 if (c == '\n')
432 c = ' ';
433 if (!(c & ~0x1f)) {
434 *q++ = '^';
435 c += '@'; /* ^@, ^A, ^B... */
436 }
437 *q++ = c;
438 }
439 *q = '\0';
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000440 /* Now log it */
441 timestamp_and_log(pri, PARSEBUF, q - PARSEBUF);
Eric Andersen7f94a5c2004-06-22 10:12:59 +0000442 }
Eric Andersen3843e961999-11-25 07:30:46 +0000443}
444
445static void quit_signal(int sig)
446{
Denis Vlasenko1c962f22007-01-09 23:44:57 +0000447 timestamp_and_log(LOG_SYSLOG | LOG_INFO, "syslogd exiting", 0);
448 puts("syslogd exiting");
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000449 unlink(dev_log_name);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000450 if (ENABLE_FEATURE_IPC_SYSLOG)
451 ipcsyslog_cleanup();
Denis Vlasenko02be0f52006-09-30 19:21:24 +0000452 exit(1);
Eric Andersen3843e961999-11-25 07:30:46 +0000453}
454
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000455static void do_mark(int sig)
Eric Andersen3843e961999-11-25 07:30:46 +0000456{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000457 if (markInterval) {
458 timestamp_and_log(LOG_SYSLOG | LOG_INFO, "-- MARK --", 0);
459 alarm(markInterval);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000460 }
Eric Andersen3843e961999-11-25 07:30:46 +0000461}
462
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000463static void do_syslogd(void) ATTRIBUTE_NORETURN;
464static void do_syslogd(void)
Eric Andersen3843e961999-11-25 07:30:46 +0000465{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000466 struct sockaddr_un sunx;
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000467 socklen_t addr_len;
Erik Andersen983b51b2000-04-04 18:14:25 +0000468 int sock_fd;
Erik Andersenf13df372000-04-18 23:51:51 +0000469 fd_set fds;
470
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000471 /* Set up signal handlers */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000472 signal(SIGINT, quit_signal);
473 signal(SIGTERM, quit_signal);
474 signal(SIGQUIT, quit_signal);
475 signal(SIGHUP, SIG_IGN);
476 signal(SIGCHLD, SIG_IGN);
Pavel Roskind39d1202000-09-13 14:14:29 +0000477#ifdef SIGCLD
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000478 signal(SIGCLD, SIG_IGN);
Pavel Roskind39d1202000-09-13 14:14:29 +0000479#endif
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000480 signal(SIGALRM, do_mark);
481 alarm(markInterval);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000482
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000483 dev_log_name = xmalloc_realpath(_PATH_LOG);
484 if (!dev_log_name)
485 dev_log_name = _PATH_LOG;
486
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000487 /* Unlink old /dev/log (or object it points to) */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000488 unlink(dev_log_name);
Erik Andersen983b51b2000-04-04 18:14:25 +0000489
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000490 memset(&sunx, 0, sizeof(sunx));
Erik Andersen983b51b2000-04-04 18:14:25 +0000491 sunx.sun_family = AF_UNIX;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000492 strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
Rob Landleyd921b2e2006-08-03 15:41:12 +0000493 sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000494 addr_len = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
495 xbind(sock_fd, (struct sockaddr *) &sunx, addr_len);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000497 if (chmod(dev_log_name, 0666) < 0) {
498 bb_perror_msg_and_die("cannot set permission on %s", dev_log_name);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000499 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000500 if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000501 ipcsyslog_init();
Eric Andersenea906502001-04-05 20:55:17 +0000502 }
Eric Andersenea906502001-04-05 20:55:17 +0000503
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000504 timestamp_and_log(LOG_SYSLOG | LOG_INFO, "syslogd started: BusyBox v" BB_VER, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000505
Erik Andersen983b51b2000-04-04 18:14:25 +0000506 for (;;) {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000507 FD_ZERO(&fds);
508 FD_SET(sock_fd, &fds);
Erik Andersenf13df372000-04-18 23:51:51 +0000509
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000510 if (select(sock_fd + 1, &fds, NULL, NULL, NULL) < 0) {
Eric Andersen871d93c2002-09-17 20:06:29 +0000511 if (errno == EINTR) {
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000512 /* alarm may have happened */
Eric Andersen871d93c2002-09-17 20:06:29 +0000513 continue;
514 }
Denis Vlasenko14c19402006-09-30 19:20:00 +0000515 bb_perror_msg_and_die("select");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000516 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000517
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000518 if (FD_ISSET(sock_fd, &fds)) {
519 int i;
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000520 i = recv(sock_fd, RECVBUF, MAX_READ - 1, 0);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000521 if (i <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000522 bb_perror_msg_and_die("UNIX socket error");
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000523 /* TODO: maybe suppress duplicates? */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000524#if ENABLE_FEATURE_REMOTE_LOG
525 /* We are not modifying log messages in any way before send */
526 /* Remote site cannot trust _us_ anyway and need to do validation again */
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000527 if (remoteAddr) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000528 if (-1 == remoteFD) {
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000529 remoteFD = socket(remoteAddr->sa.sa_family, SOCK_DGRAM, 0);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000530 }
531 if (-1 != remoteFD) {
532 /* send message to remote logger, ignore possible error */
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000533 sendto(remoteFD, RECVBUF, i, MSG_DONTWAIT,
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000534 &remoteAddr->sa, remoteAddr->len);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000535 }
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000536 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000537#endif
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000538 RECVBUF[i] = '\0';
539 split_escape_and_log(RECVBUF, i);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000540 } /* FD_ISSET() */
541 } /* for */
Eric Andersenb99df0f1999-11-24 09:04:33 +0000542}
543
Rob Landleydfba7412006-03-06 20:47:33 +0000544int syslogd_main(int argc, char **argv)
Eric Andersen3843e961999-11-25 07:30:46 +0000545{
Denis Vlasenko14c19402006-09-30 19:20:00 +0000546 char OPTION_DECL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 char *p;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000548
Eric Andersen394cf222000-12-11 16:48:50 +0000549 /* do normal option parsing */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000550 opt_complementary = "=0"; /* no non-option params */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000551 getopt32(argc, argv, OPTION_STR, OPTION_PARAM);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000552 if (option_mask32 & OPT_mark) // -m
553 markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000554 //if (option_mask32 & OPT_nofork) // -n
555 //if (option_mask32 & OPT_outfile) // -O
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000556 if (option_mask32 & OPT_loglevel) // -l
557 logLevel = xatou_range(opt_l, 1, 8);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000558 //if (option_mask32 & OPT_small) // -S
Denis Vlasenko14c19402006-09-30 19:20:00 +0000559#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000560 if (option_mask32 & OPT_filesize) // -s
561 logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
562 if (option_mask32 & OPT_rotatecnt) // -b
563 logFileRotate = xatou_range(opt_b, 0, 99);
Denis Vlasenko14c19402006-09-30 19:20:00 +0000564#endif
565#if ENABLE_FEATURE_REMOTE_LOG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000566 if (option_mask32 & OPT_remotelog) { // -R
Denis Vlasenkobbe51462007-01-10 20:19:56 +0000567 remoteAddr = host2sockaddr(opt_R, 514);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000568 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000569 //if (option_mask32 & OPT_locallog) // -L
Denis Vlasenko14c19402006-09-30 19:20:00 +0000570#endif
571#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000572 if ((option_mask32 & OPT_circularlog) && opt_C) // -C
573 shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
Denis Vlasenko14c19402006-09-30 19:20:00 +0000574#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000575
Eric Andersen4ed17822000-12-11 19:28:29 +0000576 /* If they have not specified remote logging, then log locally */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000577 if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_remotelog))
578 option_mask32 |= OPT_locallog;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000579
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580 /* Store away localhost's name before the fork */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000581 gethostname(localHostName, sizeof(localHostName));
582 p = strchr(localHostName, '.');
Denis Vlasenko14c19402006-09-30 19:20:00 +0000583 if (p) {
Glenn L McGrathfe538ba2003-09-10 23:35:45 +0000584 *p = '\0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000585 }
586
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000587 if (!(option_mask32 & OPT_nofork)) {
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000588#ifdef BB_NOMMU
Russ Dilla1fece22003-12-15 21:57:44 +0000589 vfork_daemon_rexec(0, 1, argc, argv, "-n");
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000590#else
Rob Landleyd921b2e2006-08-03 15:41:12 +0000591 xdaemon(0, 1);
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000592#endif
Eric Andersen35e643b2003-07-28 07:40:39 +0000593 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000594 umask(0);
595 do_syslogd();
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000596 /* return EXIT_SUCCESS; */
Eric Andersen3843e961999-11-25 07:30:46 +0000597}