blob: 9e4bc63c2fd66a940828c2ddf82bbec4025a040d [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
Erik Andersen983b51b2000-04-04 18:14:25 +000025/* Path to the unix socket */
Denis Vlasenkoceab8702007-01-04 17:57:54 +000026static char dev_log_name[MAXPATHLEN];
Eric Andersen3843e961999-11-25 07:30:46 +000027
Denis Vlasenko14c19402006-09-30 19:20:00 +000028/* Path for the file where all log messages are written */
29static const char *logFilePath = "/var/log/messages";
Erik Andersene49d5ec2000-02-08 19:58:47 +000030
Denis Vlasenkoceab8702007-01-04 17:57:54 +000031#if ENABLE_FEATURE_ROTATE_LOGFILE
Eric Andersenaff114c2004-04-14 17:51:38 +000032/* max size of message file before being rotated */
Eric Andersen29c77f72003-10-09 09:43:18 +000033static int logFileSize = 200 * 1024;
Eric Andersen29c77f72003-10-09 09:43:18 +000034/* number of rotated message files */
35static int logFileRotate = 1;
36#endif
37
Eric Andersen3843e961999-11-25 07:30:46 +000038/* interval between marks in seconds */
Denis Vlasenkoceab8702007-01-04 17:57:54 +000039static int markInterval = 20 * 60;
Erik Andersene49d5ec2000-02-08 19:58:47 +000040
Denis Vlasenko1decd0e2006-09-30 19:17:40 +000041/* level of messages to be locally logged */
42static int logLevel = 8;
43
Eric Andersen3843e961999-11-25 07:30:46 +000044/* localhost's name */
Denis Vlasenkoceab8702007-01-04 17:57:54 +000045static char localHostName[64];
Eric Andersen3843e961999-11-25 07:30:46 +000046
Denis Vlasenkoceab8702007-01-04 17:57:54 +000047#if ENABLE_FEATURE_REMOTE_LOG
Eric Andersenced2cef2000-07-20 23:41:24 +000048#include <netinet/in.h>
49/* udp socket for logging to remote host */
Denis Vlasenkoceab8702007-01-04 17:57:54 +000050static int remoteFD = -1;
51static struct sockaddr_in remoteAddr;
Eric Andersenced2cef2000-07-20 23:41:24 +000052#endif
53
Denis Vlasenkoceab8702007-01-04 17:57:54 +000054
55/* NB: we may need 2x this amount on stack... */
56enum { MAX_READ = 1024 };
57
58
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +000059/* options */
Denis Vlasenko14c19402006-09-30 19:20:00 +000060/* Correct regardless of combination of CONFIG_xxx */
61enum {
62 OPTBIT_mark = 0, // -m
63 OPTBIT_nofork, // -n
64 OPTBIT_outfile, // -O
65 OPTBIT_loglevel, // -l
66 OPTBIT_small, // -S
67 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
68 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
69 USE_FEATURE_REMOTE_LOG( OPTBIT_remote ,) // -R
70 USE_FEATURE_REMOTE_LOG( OPTBIT_localtoo ,) // -L
71 USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
72
73 OPT_mark = 1 << OPTBIT_mark ,
74 OPT_nofork = 1 << OPTBIT_nofork ,
75 OPT_outfile = 1 << OPTBIT_outfile ,
76 OPT_loglevel = 1 << OPTBIT_loglevel,
77 OPT_small = 1 << OPTBIT_small ,
78 OPT_filesize = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
79 OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
80 OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remote )) + 0,
81 OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_localtoo )) + 0,
82 OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
83};
84#define OPTION_STR "m:nO:l:S" \
85 USE_FEATURE_ROTATE_LOGFILE("s:" ) \
86 USE_FEATURE_ROTATE_LOGFILE("b:" ) \
87 USE_FEATURE_REMOTE_LOG( "R:" ) \
88 USE_FEATURE_REMOTE_LOG( "L" ) \
89 USE_FEATURE_IPC_SYSLOG( "C::")
90#define OPTION_DECL *opt_m, *opt_l \
91 USE_FEATURE_ROTATE_LOGFILE(,*opt_s) \
92 USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \
93 USE_FEATURE_REMOTE_LOG( ,*opt_R) \
94 USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL)
95#define OPTION_PARAM &opt_m, &logFilePath, &opt_l \
96 USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \
97 USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \
98 USE_FEATURE_REMOTE_LOG( ,&opt_R) \
99 USE_FEATURE_IPC_SYSLOG( ,&opt_C)
Eric Andersen871d93c2002-09-17 20:06:29 +0000100
Eric Andersen871d93c2002-09-17 20:06:29 +0000101
Mark Whitley6317c4b2001-03-12 22:51:50 +0000102/* circular buffer variables/structures */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000103#if ENABLE_FEATURE_IPC_SYSLOG
104
Eric Andersend4a5e252003-12-19 11:32:14 +0000105
106#if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4
107#error Sorry, you must set the syslogd buffer size to at least 4KB.
108#error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE
109#endif
110
Mark Whitley6317c4b2001-03-12 22:51:50 +0000111#include <sys/ipc.h>
112#include <sys/sem.h>
113#include <sys/shm.h>
114
115/* our shared key */
Denis Vlasenko14c19402006-09-30 19:20:00 +0000116#define KEY_ID ((long)0x414e4547) /* "GENA" */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000117
118// Semaphore operation structures
119static struct shbuf_ds {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000120 int32_t size; // size of data written
121 int32_t head; // start of message list
122 int32_t tail; // end of message list
Denis Vlasenko14c19402006-09-30 19:20:00 +0000123 char data[1]; // data/messages
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000124} *shbuf; // shared memory pointer
Mark Whitley6317c4b2001-03-12 22:51:50 +0000125
Denis Vlasenko14c19402006-09-30 19:20:00 +0000126static int shmid = -1; // ipc shared memory id
127static int s_semid = -1; // ipc semaphore id
Eric Andersend4a5e252003-12-19 11:32:14 +0000128static int shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024); // default shm size
Eric Andersen871d93c2002-09-17 20:06:29 +0000129
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000130static void ipcsyslog_cleanup(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000131{
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000132 if (shmid != -1) {
Denis Vlasenko14c19402006-09-30 19:20:00 +0000133 shmdt(shbuf);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000134 }
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000135 if (shmid != -1) {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000136 shmctl(shmid, IPC_RMID, NULL);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000137 }
138 if (s_semid != -1) {
Mark Whitley6317c4b2001-03-12 22:51:50 +0000139 semctl(s_semid, 0, IPC_RMID, 0);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000140 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000141}
142
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000143static void ipcsyslog_init(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000144{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000145 shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023);
146 if (shmid == -1) {
147 bb_perror_msg_and_die("shmget");
148 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000149
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000150 shbuf = shmat(shmid, NULL, 0);
151 if (!shbuf) {
152 bb_perror_msg_and_die("shmat");
153 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000154
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000155 shbuf->size = shm_size - offsetof(struct shbuf_ds, data);
156 shbuf->head = shbuf->tail = 0;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000157
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000158 // we'll trust the OS to set initial semval to 0 (let's hope)
159 s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023);
160 if (s_semid == -1) {
161 if (errno == EEXIST) {
162 s_semid = semget(KEY_ID, 2, 0);
163 if (s_semid != -1)
164 return;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000165 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000166 bb_perror_msg_and_die("semget");
Mark Whitley6317c4b2001-03-12 22:51:50 +0000167 }
168}
169
170/* write message to buffer */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000171static void log_to_shmem(const char *msg, int len)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000172{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000173 static /*const*/ struct sembuf SMwup[1] = { {1, -1, IPC_NOWAIT} };
174 static /*const*/ struct sembuf SMwdn[3] = { {0, 0}, {1, 0}, {1, +1} };
175
176 int old_tail, new_tail;
177 char *c;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000178
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000179 if (semop(s_semid, SMwdn, 3) == -1) {
180 bb_perror_msg_and_die("SMwdn");
181 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000182
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000183 /* Circular Buffer Algorithm:
Mark Whitley6317c4b2001-03-12 22:51:50 +0000184 * --------------------------
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000185 * tail == position where to store next syslog message.
186 * head == position of next message to retrieve ("print").
187 * if head == tail, there is no "unprinted" messages left.
188 * head is typically advanced by separate "reader" program,
189 * but if there isn't one, we have to do it ourself.
190 * messages are NUL-separated.
Mark Whitley6317c4b2001-03-12 22:51:50 +0000191 */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000192 len++; /* length with NUL included */
193 again:
194 old_tail = shbuf->tail;
195 new_tail = old_tail + len;
196 if (new_tail < shbuf->size) {
197 /* No need to move head if shbuf->head <= old_tail,
198 * else... */
199 if (old_tail < shbuf->head && shbuf->head <= new_tail) {
200 /* ...need to move head forward */
201 c = memchr(shbuf->data + new_tail, '\0',
202 shbuf->size - new_tail);
203 if (!c) /* no NUL ahead of us, wrap around */
204 c = memchr(shbuf->data, '\0', old_tail);
205 if (!c) { /* still nothing? point to this msg... */
206 shbuf->head = old_tail;
207 } else {
208 /* convert pointer to offset + skip NUL */
209 shbuf->head = c - shbuf->data + 1;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000210 }
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000211 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000212 /* store message, set new tail */
213 memcpy(shbuf->data + old_tail, msg, len);
214 shbuf->tail = new_tail;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000215 } else {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000216 /* we need to break up the message and wrap it around */
217 /* k == available buffer space ahead of old tail */
218 int k = shbuf->size - old_tail - 1;
219 if (shbuf->head > old_tail) {
220 /* we are going to overwrite head, need to
221 * move it out of the way */
222 c = memchr(shbuf->data, '\0', old_tail);
223 if (!c) { /* nothing? point to this msg... */
224 shbuf->head = old_tail;
225 } else { /* convert pointer to offset + skip NUL */
226 shbuf->head = c - shbuf->data + 1;
227 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000228 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000229 /* copy what fits to the end of buffer, and repeat */
230 memcpy(shbuf->data + old_tail, msg, k);
231 msg += k;
232 len -= k;
233 shbuf->tail = 0;
234 goto again;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000235 }
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000236 if (semop(s_semid, SMwup, 1) == -1) {
237 bb_perror_msg_and_die("SMwup");
238 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000239}
Rob Landley028ba282006-08-28 20:16:42 +0000240#else
241void ipcsyslog_cleanup(void);
242void ipcsyslog_init(void);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000243void log_to_shmem(const char *msg);
Eric Andersen871d93c2002-09-17 20:06:29 +0000244
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000245
246#endif /* FEATURE_IPC_SYSLOG */
247
248
Erik Andersen983b51b2000-04-04 18:14:25 +0000249/* Print a message to the log file. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000250static void log_locally(char *msg)
Eric Andersen3843e961999-11-25 07:30:46 +0000251{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000252 int fd, len = strlen(msg);
Eric Andersen3843e961999-11-25 07:30:46 +0000253
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000254#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000255 if ((option_mask32 & OPT_circularlog) && shbuf) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000256 log_to_shmem(msg, len);
257 return;
258 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000259#endif
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000260
261 again:
Denis Vlasenkobd8f43d2006-09-08 17:31:55 +0000262 fd = device_open(logFilePath, O_WRONLY | O_CREAT
263 | O_NOCTTY | O_APPEND | O_NONBLOCK);
264 if (fd >= 0) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000265 struct flock fl;
266
267 fl.l_whence = SEEK_SET;
268 fl.l_start = 0;
269 fl.l_len = 1;
Erik Andersene3ed1562000-04-19 18:52:56 +0000270 fl.l_type = F_WRLCK;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000271 fcntl(fd, F_SETLKW, &fl);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000272
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000273#if ENABLE_FEATURE_ROTATE_LOGFILE
274 if (logFileSize) {
Eric Andersen29c77f72003-10-09 09:43:18 +0000275 struct stat statf;
276 int r = fstat(fd, &statf);
Denis Vlasenko14c19402006-09-30 19:20:00 +0000277 if (!r && (statf.st_mode & S_IFREG)
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000278 && (lseek(fd, 0, SEEK_END) > logFileSize)
279 ) {
280 if (logFileRotate) { /* always 0..99 */
281 int i = strlen(logFilePath) + 3 + 1;
Denis Vlasenko14c19402006-09-30 19:20:00 +0000282 char oldFile[i];
283 char newFile[i];
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000284 i = logFileRotate - 1;
285 /* rename: f.8 -> f.9; f.7 -> f.8; ... */
286 while (1) {
Eric Andersen29c77f72003-10-09 09:43:18 +0000287 sprintf(newFile, "%s.%d", logFilePath, i);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000288 if (i == 0) break;
289 sprintf(oldFile, "%s.%d", logFilePath, --i);
Eric Andersen29c77f72003-10-09 09:43:18 +0000290 rename(oldFile, newFile);
291 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000292 /* newFile == "f.0" now */
293 rename(logFilePath, newFile);
Eric Andersen29c77f72003-10-09 09:43:18 +0000294 fl.l_type = F_UNLCK;
Denis Vlasenko14c19402006-09-30 19:20:00 +0000295 fcntl(fd, F_SETLKW, &fl);
Eric Andersen29c77f72003-10-09 09:43:18 +0000296 close(fd);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000297 goto again;
Eric Andersen29c77f72003-10-09 09:43:18 +0000298 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000299 ftruncate(fd, 0);
Eric Andersen29c77f72003-10-09 09:43:18 +0000300 }
301 }
Rob Landley028ba282006-08-28 20:16:42 +0000302#endif
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000303 full_write(fd, msg, len);
Erik Andersene3ed1562000-04-19 18:52:56 +0000304 fl.l_type = F_UNLCK;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000305 fcntl(fd, F_SETLKW, &fl);
306 close(fd);
Eric Andersen3843e961999-11-25 07:30:46 +0000307 } else {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000308 /* cannot open logfile? - print to /dev/console then */
Denis Vlasenkobd8f43d2006-09-08 17:31:55 +0000309 fd = device_open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000310 if (fd < 0)
311 fd = 2; /* then stderr, dammit */
312 full_write(fd, msg, len);
313 if (fd != 2)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000314 close(fd);
Eric Andersen3843e961999-11-25 07:30:46 +0000315 }
Eric Andersenb99df0f1999-11-24 09:04:33 +0000316}
317
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000318static void parse_fac_prio_20(int pri, char *res20)
Eric Andersen3843e961999-11-25 07:30:46 +0000319{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000320 CODE *c_pri, *c_fac;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000321
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000322 if (pri != 0) {
Denis Vlasenko14c19402006-09-30 19:20:00 +0000323 c_fac = facilitynames;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000324 while (c_fac->c_name) {
325 if (c_fac->c_val != (LOG_FAC(pri) << 3)) {
326 c_fac++; continue;
327 }
328 /* facility is found, look for prio */
329 c_pri = prioritynames;
330 while (c_pri->c_name) {
331 if (c_pri->c_val != LOG_PRI(pri)) {
332 c_pri++; continue;
333 }
334 snprintf(res20, 20, "%s.%s",
335 c_fac->c_name, c_pri->c_name);
336 return;
337 }
338 /* prio not found, bail out */
339 break;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000340 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000341 snprintf(res20, 20, "<%d>", pri);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000342 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000343}
Eric Andersen3843e961999-11-25 07:30:46 +0000344
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000345/* len parameter is used only for "is there a timestamp?" check
346 * NB: some callers cheat and supply 0 when they know
347 * that there is no timestamp, short-cutting the test */
348static void timestamp_and_log(int pri, char *msg, int len)
349{
350 time_t now;
351 char *timestamp;
352
353 if (len < 16 || msg[3] != ' ' || msg[6] != ' '
354 || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
355 ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000356 time(&now);
357 timestamp = ctime(&now) + 4;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000358 } else {
359 timestamp = msg;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360 msg += 16;
361 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000362 timestamp[15] = '\0';
Eric Andersen3843e961999-11-25 07:30:46 +0000363
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000364 /* Log message locally (to file or shared mem) */
365 if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
366 if (LOG_PRI(pri) < logLevel) {
367 if (option_mask32 & OPT_small)
368 msg = xasprintf("%s %s\n", timestamp, msg);
369 else {
370 char res[20];
371 parse_fac_prio_20(pri, res);
372 msg = xasprintf("%s %s %s %s\n", timestamp, localHostName, res, msg);
373 }
374 log_locally(msg);
375 free(msg);
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000376 }
377 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000378}
Glenn L McGrath73ebb882004-09-14 18:12:13 +0000379
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000380static void split_escape_and_log(char *tmpbuf, int len)
381{
382 char line[len * 2 + 1]; /* gcc' cheap alloca */
383 char *p = tmpbuf;
384
385 tmpbuf += len;
386 while (p < tmpbuf) {
387 char c;
388 char *q = line;
389 int pri = (LOG_USER | LOG_NOTICE);
390
391 if (*p == '<') {
392 /* Parse the magic priority number. */
393 pri = bb_strtou(p + 1, &p, 10);
394 if (*p == '>') p++;
395 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) {
396 pri = (LOG_USER | LOG_NOTICE);
397 }
Denis Vlasenko1decd0e2006-09-30 19:17:40 +0000398 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000399
400 while ((c = *p++)) {
401 if (c == '\n')
402 c = ' ';
403 if (!(c & ~0x1f)) {
404 *q++ = '^';
405 c += '@'; /* ^@, ^A, ^B... */
406 }
407 *q++ = c;
408 }
409 *q = '\0';
410 /* now log it */
411 timestamp_and_log(pri, line, q - line);
Eric Andersen7f94a5c2004-06-22 10:12:59 +0000412 }
Eric Andersen3843e961999-11-25 07:30:46 +0000413}
414
415static void quit_signal(int sig)
416{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000417 timestamp_and_log(LOG_SYSLOG | LOG_INFO, "System log daemon exiting", 0);
418 puts("System log daemon exiting");
419 unlink(dev_log_name);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000420 if (ENABLE_FEATURE_IPC_SYSLOG)
421 ipcsyslog_cleanup();
Denis Vlasenko02be0f52006-09-30 19:21:24 +0000422 exit(1);
Eric Andersen3843e961999-11-25 07:30:46 +0000423}
424
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000425static void do_mark(int sig)
Eric Andersen3843e961999-11-25 07:30:46 +0000426{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000427 if (markInterval) {
428 timestamp_and_log(LOG_SYSLOG | LOG_INFO, "-- MARK --", 0);
429 alarm(markInterval);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430 }
Eric Andersen3843e961999-11-25 07:30:46 +0000431}
432
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000433static void do_syslogd(void) ATTRIBUTE_NORETURN;
434static void do_syslogd(void)
Eric Andersen3843e961999-11-25 07:30:46 +0000435{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000436 struct sockaddr_un sunx;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000437 socklen_t addrLength;
Erik Andersen983b51b2000-04-04 18:14:25 +0000438 int sock_fd;
Erik Andersenf13df372000-04-18 23:51:51 +0000439 fd_set fds;
440
Erik Andersenf13df372000-04-18 23:51:51 +0000441 /* Set up signal handlers. */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000442 signal(SIGINT, quit_signal);
443 signal(SIGTERM, quit_signal);
444 signal(SIGQUIT, quit_signal);
445 signal(SIGHUP, SIG_IGN);
446 signal(SIGCHLD, SIG_IGN);
Pavel Roskind39d1202000-09-13 14:14:29 +0000447#ifdef SIGCLD
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000448 signal(SIGCLD, SIG_IGN);
Pavel Roskind39d1202000-09-13 14:14:29 +0000449#endif
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000450 signal(SIGALRM, do_mark);
451 alarm(markInterval);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000452
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000453 /* Unlink old /dev/log (or object it points to) */
454 if (realpath(_PATH_LOG, dev_log_name) != NULL) {
455 unlink(dev_log_name);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000456 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000457
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000458 memset(&sunx, 0, sizeof(sunx));
Erik Andersen983b51b2000-04-04 18:14:25 +0000459 sunx.sun_family = AF_UNIX;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000460 strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
Rob Landleyd921b2e2006-08-03 15:41:12 +0000461 sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000462 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
463 if (bind(sock_fd, (struct sockaddr *) &sunx, addrLength) < 0) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000464 bb_perror_msg_and_die("cannot connect to socket %s", dev_log_name);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000465 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000466
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000467 if (chmod(dev_log_name, 0666) < 0) {
468 bb_perror_msg_and_die("cannot set permission on %s", dev_log_name);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000469 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000470 if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000471 ipcsyslog_init();
Eric Andersenea906502001-04-05 20:55:17 +0000472 }
Eric Andersenea906502001-04-05 20:55:17 +0000473
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000474 timestamp_and_log(LOG_SYSLOG | LOG_INFO, "syslogd started: BusyBox v" BB_VER, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000475
Erik Andersen983b51b2000-04-04 18:14:25 +0000476 for (;;) {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000477 FD_ZERO(&fds);
478 FD_SET(sock_fd, &fds);
Erik Andersenf13df372000-04-18 23:51:51 +0000479
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000480 if (select(sock_fd + 1, &fds, NULL, NULL, NULL) < 0) {
Eric Andersen871d93c2002-09-17 20:06:29 +0000481 if (errno == EINTR) {
482 /* alarm may have happened. */
483 continue;
484 }
Denis Vlasenko14c19402006-09-30 19:20:00 +0000485 bb_perror_msg_and_die("select");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000486 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000488 if (FD_ISSET(sock_fd, &fds)) {
489 int i;
"Vladimir N. Oleynik"b32b1db2005-10-15 13:49:21 +0000490#define tmpbuf bb_common_bufsiz1
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000491 i = recv(sock_fd, tmpbuf, MAX_READ, 0);
492 if (i <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000493 bb_perror_msg_and_die("UNIX socket error");
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000494 /* TODO: maybe supress duplicates? */
495#if ENABLE_FEATURE_REMOTE_LOG
496 /* We are not modifying log messages in any way before send */
497 /* Remote site cannot trust _us_ anyway and need to do validation again */
498 if (option_mask32 & OPT_remotelog) {
499 if (-1 == remoteFD) {
500 remoteFD = socket(AF_INET, SOCK_DGRAM, 0);
501 }
502 if (-1 != remoteFD) {
503 /* send message to remote logger, ignore possible error */
504 sendto(remoteFD, tmpbuf, i, MSG_DONTWAIT,
505 (struct sockaddr *) &remoteAddr,
506 sizeof(remoteAddr));
507 }
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000508 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000509#endif
510 tmpbuf[i] = '\0';
511 split_escape_and_log(tmpbuf, i);
512#undef tmpbuf
513 } /* FD_ISSET() */
514 } /* for */
Eric Andersenb99df0f1999-11-24 09:04:33 +0000515}
516
Rob Landleydfba7412006-03-06 20:47:33 +0000517int syslogd_main(int argc, char **argv)
Eric Andersen3843e961999-11-25 07:30:46 +0000518{
Denis Vlasenko14c19402006-09-30 19:20:00 +0000519 char OPTION_DECL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000520 char *p;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000521
Eric Andersen394cf222000-12-11 16:48:50 +0000522 /* do normal option parsing */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000523 getopt32(argc, argv, OPTION_STR, OPTION_PARAM);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000524 if (option_mask32 & OPT_mark) // -m
525 markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000526 //if (option_mask32 & OPT_nofork) // -n
527 //if (option_mask32 & OPT_outfile) // -O
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000528 if (option_mask32 & OPT_loglevel) // -l
529 logLevel = xatou_range(opt_l, 1, 8);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000530 //if (option_mask32 & OPT_small) // -S
Denis Vlasenko14c19402006-09-30 19:20:00 +0000531#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000532 if (option_mask32 & OPT_filesize) // -s
533 logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
534 if (option_mask32 & OPT_rotatecnt) // -b
535 logFileRotate = xatou_range(opt_b, 0, 99);
Denis Vlasenko14c19402006-09-30 19:20:00 +0000536#endif
537#if ENABLE_FEATURE_REMOTE_LOG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000538 if (option_mask32 & OPT_remotelog) { // -R
Denis Vlasenko02be0f52006-09-30 19:21:24 +0000539 int port = 514;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000540 p = strchr(opt_R, ':');
Denis Vlasenko14c19402006-09-30 19:20:00 +0000541 if (p) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000542 *p++ = '\0';
543 port = xatou16(p);
Eric Andersen3843e961999-11-25 07:30:46 +0000544 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000545 remoteAddr.sin_family = AF_INET;
Denis Vlasenko02be0f52006-09-30 19:21:24 +0000546 /* FIXME: looks ip4-specific. need to do better */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000547 remoteAddr.sin_addr = *(struct in_addr *) *(xgethostbyname(opt_R)->h_addr_list);
548 remoteAddr.sin_port = htons(port);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000549 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000550 //if (option_mask32 & OPT_locallog) // -L
Denis Vlasenko14c19402006-09-30 19:20:00 +0000551#endif
552#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000553 if ((option_mask32 & OPT_circularlog) && opt_C) // -C
554 shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
Denis Vlasenko14c19402006-09-30 19:20:00 +0000555#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000556
Eric Andersen4ed17822000-12-11 19:28:29 +0000557 /* If they have not specified remote logging, then log locally */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000558 if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_remotelog))
559 option_mask32 |= OPT_locallog;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000560
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 /* Store away localhost's name before the fork */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000562 gethostname(localHostName, sizeof(localHostName));
563 p = strchr(localHostName, '.');
Denis Vlasenko14c19402006-09-30 19:20:00 +0000564 if (p) {
Glenn L McGrathfe538ba2003-09-10 23:35:45 +0000565 *p = '\0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000566 }
567
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000568 if (!(option_mask32 & OPT_nofork)) {
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000569#ifdef BB_NOMMU
Russ Dilla1fece22003-12-15 21:57:44 +0000570 vfork_daemon_rexec(0, 1, argc, argv, "-n");
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000571#else
Rob Landleyd921b2e2006-08-03 15:41:12 +0000572 xdaemon(0, 1);
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +0000573#endif
Eric Andersen35e643b2003-07-28 07:40:39 +0000574 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000575 umask(0);
576 do_syslogd();
Eric Andersenb186d981999-12-03 09:19:54 +0000577
Matt Kraai3e856ce2000-12-01 02:55:13 +0000578 return EXIT_SUCCESS;
Eric Andersen3843e961999-11-25 07:30:46 +0000579}