blob: 5998732bd9d342bd5341832231a7c00266f00f74 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3843e961999-11-25 07:30:46 +00002/*
3 * Mini syslogd implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen3843e961999-11-25 07:30:46 +00006 *
Erik Andersenf13df372000-04-18 23:51:51 +00007 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
8 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +00009 * "circular buffer" Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>
Mark Whitley6317c4b2001-03-12 22:51:50 +000010 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +000011 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
Mark Whitley6bff9cc2001-03-12 23:41:34 +000012 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000013 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen3843e961999-11-25 07:30:46 +000014 */
Eric Andersenb99df0f1999-11-24 09:04:33 +000015
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000016#include "libbb.h"
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
Denis Vlasenko3c8b5ba2007-06-04 18:23:59 +000020/* SYSLOG_NAMES defined to pull prioritynames[] and facilitynames[]
21 * from syslog.h. Grrrr - glibc puts those in _rwdata_! :( */
Eric Andersen3843e961999-11-25 07:30:46 +000022#define SYSLOG_NAMES
Denis Vlasenko3c8b5ba2007-06-04 18:23:59 +000023#define SYSLOG_NAMES_CONST /* uclibc is saner :) */
Eric Andersen3843e961999-11-25 07:30:46 +000024#include <sys/syslog.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
45enum { MAX_READ = 256 };
46
47/* Semaphore operation structures */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000048struct shbuf_ds {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000049 int32_t size; /* size of data - 1 */
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000050 int32_t tail; /* end of message list */
51 char data[1]; /* data/messages */
52};
53
54/* Allows us to have smaller initializer. Ugly. */
55#define GLOBALS \
56 const char *logFilePath; \
57 int logFD; \
58 /* interval between marks in seconds */ \
59 /*int markInterval;*/ \
60 /* level of messages to be logged */ \
61 int logLevel; \
62USE_FEATURE_ROTATE_LOGFILE( \
63 /* max size of file before rotation */ \
64 unsigned logFileSize; \
65 /* number of rotated message files */ \
66 unsigned logFileRotate; \
67 unsigned curFileSize; \
68 smallint isRegular; \
69) \
70USE_FEATURE_REMOTE_LOG( \
71 /* udp socket for remote logging */ \
72 int remoteFD; \
73 len_and_sockaddr* remoteAddr; \
74) \
75USE_FEATURE_IPC_SYSLOG( \
76 int shmid; /* ipc shared memory id */ \
77 int s_semid; /* ipc semaphore id */ \
78 int shm_size; \
79 struct sembuf SMwup[1]; \
80 struct sembuf SMwdn[3]; \
81)
82
83struct init_globals {
84 GLOBALS
85};
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000086
87struct globals {
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000088 GLOBALS
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000089#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000090 struct shbuf_ds *shbuf;
91#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +000092 time_t last_log_time;
93 /* localhost's name */
94 char localHostName[64];
95
Denis Vlasenko4f93cde2007-03-20 20:03:03 +000096 /* We recv into recvbuf... */
97 char recvbuf[MAX_READ];
98 /* ...then copy to parsebuf, escaping control chars */
99 /* (can grow x2 max) */
100 char parsebuf[MAX_READ*2];
101 /* ...then sprintf into printbuf, adding timestamp (15 chars),
102 * host (64), fac.prio (20) to the message */
103 /* (growth by: 15 + 64 + 20 + delims = ~110) */
104 char printbuf[MAX_READ*2 + 128];
105};
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000106
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000107static const struct init_globals init_data = {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000108 .logFilePath = "/var/log/messages",
109 .logFD = -1,
110#ifdef SYSLOGD_MARK
111 .markInterval = 20 * 60,
112#endif
113 .logLevel = 8,
114#if ENABLE_FEATURE_ROTATE_LOGFILE
115 .logFileSize = 200 * 1024,
116 .logFileRotate = 1,
117#endif
118#if ENABLE_FEATURE_REMOTE_LOG
119 .remoteFD = -1,
120#endif
121#if ENABLE_FEATURE_IPC_SYSLOG
122 .shmid = -1,
123 .s_semid = -1,
124 .shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024), // default shm size
125 .SMwup = { {1, -1, IPC_NOWAIT} },
126 .SMwdn = { {0, 0}, {1, 0}, {1, +1} },
127#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000128};
129
130#define G (*ptr_to_globals)
131
132
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000133/* Options */
Denis Vlasenko14c19402006-09-30 19:20:00 +0000134enum {
135 OPTBIT_mark = 0, // -m
136 OPTBIT_nofork, // -n
137 OPTBIT_outfile, // -O
138 OPTBIT_loglevel, // -l
139 OPTBIT_small, // -S
140 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
141 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
142 USE_FEATURE_REMOTE_LOG( OPTBIT_remote ,) // -R
143 USE_FEATURE_REMOTE_LOG( OPTBIT_localtoo ,) // -L
144 USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
145
146 OPT_mark = 1 << OPTBIT_mark ,
147 OPT_nofork = 1 << OPTBIT_nofork ,
148 OPT_outfile = 1 << OPTBIT_outfile ,
149 OPT_loglevel = 1 << OPTBIT_loglevel,
150 OPT_small = 1 << OPTBIT_small ,
151 OPT_filesize = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
152 OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
153 OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remote )) + 0,
154 OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_localtoo )) + 0,
155 OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
156};
157#define OPTION_STR "m:nO:l:S" \
158 USE_FEATURE_ROTATE_LOGFILE("s:" ) \
159 USE_FEATURE_ROTATE_LOGFILE("b:" ) \
160 USE_FEATURE_REMOTE_LOG( "R:" ) \
161 USE_FEATURE_REMOTE_LOG( "L" ) \
162 USE_FEATURE_IPC_SYSLOG( "C::")
163#define OPTION_DECL *opt_m, *opt_l \
164 USE_FEATURE_ROTATE_LOGFILE(,*opt_s) \
165 USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \
166 USE_FEATURE_REMOTE_LOG( ,*opt_R) \
167 USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000168#define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \
Denis Vlasenko14c19402006-09-30 19:20:00 +0000169 USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \
170 USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \
171 USE_FEATURE_REMOTE_LOG( ,&opt_R) \
172 USE_FEATURE_IPC_SYSLOG( ,&opt_C)
Eric Andersen871d93c2002-09-17 20:06:29 +0000173
Eric Andersen871d93c2002-09-17 20:06:29 +0000174
Mark Whitley6317c4b2001-03-12 22:51:50 +0000175/* circular buffer variables/structures */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000176#if ENABLE_FEATURE_IPC_SYSLOG
177
Eric Andersend4a5e252003-12-19 11:32:14 +0000178#if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4
179#error Sorry, you must set the syslogd buffer size to at least 4KB.
180#error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE
181#endif
182
Mark Whitley6317c4b2001-03-12 22:51:50 +0000183/* our shared key */
Denis Vlasenko14c19402006-09-30 19:20:00 +0000184#define KEY_ID ((long)0x414e4547) /* "GENA" */
Mark Whitley6317c4b2001-03-12 22:51:50 +0000185
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000186static void ipcsyslog_cleanup(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000187{
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000188 if (G.shmid != -1) {
189 shmdt(G.shbuf);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000190 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000191 if (G.shmid != -1) {
192 shmctl(G.shmid, IPC_RMID, NULL);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000193 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000194 if (G.s_semid != -1) {
195 semctl(G.s_semid, 0, IPC_RMID, 0);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000196 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000197}
198
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000199static void ipcsyslog_init(void)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000200{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000201 if (DEBUG)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000202 printf("shmget(%lx, %d,...)\n", KEY_ID, G.shm_size);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000203
Denis Vlasenkoa1120a82007-08-14 10:27:56 +0000204 G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 0644);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000205 if (G.shmid == -1) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000206 bb_perror_msg_and_die("shmget");
207 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000208
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000209 G.shbuf = shmat(G.shmid, NULL, 0);
210 if (!G.shbuf) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000211 bb_perror_msg_and_die("shmat");
212 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000213
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000214 memset(G.shbuf, 0, G.shm_size);
215 G.shbuf->size = G.shm_size - offsetof(struct shbuf_ds, data) - 1;
216 /*G.shbuf->tail = 0;*/
Mark Whitley6317c4b2001-03-12 22:51:50 +0000217
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000218 // we'll trust the OS to set initial semval to 0 (let's hope)
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000219 G.s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023);
220 if (G.s_semid == -1) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000221 if (errno == EEXIST) {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000222 G.s_semid = semget(KEY_ID, 2, 0);
223 if (G.s_semid != -1)
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000224 return;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000225 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000226 bb_perror_msg_and_die("semget");
Mark Whitley6317c4b2001-03-12 22:51:50 +0000227 }
228}
229
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000230/* Write message to shared mem buffer */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000231static void log_to_shmem(const char *msg, int len)
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000232{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000233 int old_tail, new_tail;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000234
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000235 if (semop(G.s_semid, G.SMwdn, 3) == -1) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000236 bb_perror_msg_and_die("SMwdn");
237 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000238
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000239 /* Circular Buffer Algorithm:
Mark Whitley6317c4b2001-03-12 22:51:50 +0000240 * --------------------------
Denis Vlasenko150f4022007-01-13 21:06:21 +0000241 * tail == position where to store next syslog message.
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000242 * tail's max value is (shbuf->size - 1)
243 * Last byte of buffer is never used and remains NUL.
Mark Whitley6317c4b2001-03-12 22:51:50 +0000244 */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000245 len++; /* length with NUL included */
246 again:
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000247 old_tail = G.shbuf->tail;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000248 new_tail = old_tail + len;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000249 if (new_tail < G.shbuf->size) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000250 /* store message, set new tail */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000251 memcpy(G.shbuf->data + old_tail, msg, len);
252 G.shbuf->tail = new_tail;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000253 } else {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000254 /* k == available buffer space ahead of old tail */
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000255 int k = G.shbuf->size - old_tail;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000256 /* copy what fits to the end of buffer, and repeat */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000257 memcpy(G.shbuf->data + old_tail, msg, k);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000258 msg += k;
259 len -= k;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000260 G.shbuf->tail = 0;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000261 goto again;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000262 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000263 if (semop(G.s_semid, G.SMwup, 1) == -1) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000264 bb_perror_msg_and_die("SMwup");
265 }
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000266 if (DEBUG)
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000267 printf("tail:%d\n", G.shbuf->tail);
Mark Whitley6317c4b2001-03-12 22:51:50 +0000268}
Rob Landley028ba282006-08-28 20:16:42 +0000269#else
270void ipcsyslog_cleanup(void);
271void ipcsyslog_init(void);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000272void log_to_shmem(const char *msg);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000273#endif /* FEATURE_IPC_SYSLOG */
274
275
Erik Andersen983b51b2000-04-04 18:14:25 +0000276/* Print a message to the log file. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000277static void log_locally(char *msg)
Eric Andersen3843e961999-11-25 07:30:46 +0000278{
Denis Vlasenkob8934972007-01-04 18:02:32 +0000279 struct flock fl;
280 int len = strlen(msg);
Eric Andersen3843e961999-11-25 07:30:46 +0000281
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000282#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000283 if ((option_mask32 & OPT_circularlog) && G.shbuf) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000284 log_to_shmem(msg, len);
285 return;
286 }
Mark Whitley6317c4b2001-03-12 22:51:50 +0000287#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000288 if (G.logFD >= 0) {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000289 time_t cur;
290 time(&cur);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000291 if (G.last_log_time != cur) {
292 G.last_log_time = cur; /* reopen log file every second */
293 close(G.logFD);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000294 goto reopen;
295 }
296 } else {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000297 reopen:
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000298 G.logFD = device_open(G.logFilePath, O_WRONLY | O_CREAT
Denis Vlasenkobd8f43d2006-09-08 17:31:55 +0000299 | O_NOCTTY | O_APPEND | O_NONBLOCK);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000300 if (G.logFD < 0) {
Denis Vlasenkob8934972007-01-04 18:02:32 +0000301 /* cannot open logfile? - print to /dev/console then */
Denis Vlasenko70ab28f2007-11-18 05:43:05 +0000302 int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000303 if (fd < 0)
304 fd = 2; /* then stderr, dammit */
305 full_write(fd, msg, len);
306 if (fd != 2)
307 close(fd);
308 return;
309 }
310#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenkobae79482007-01-09 23:42:43 +0000311 {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000312 struct stat statf;
313 G.isRegular = (fstat(G.logFD, &statf) == 0 && S_ISREG(statf.st_mode));
314 /* bug (mostly harmless): can wrap around if file > 4gb */
315 G.curFileSize = statf.st_size;
Denis Vlasenkobae79482007-01-09 23:42:43 +0000316 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000317#endif
318 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000319
Denis Vlasenkob8934972007-01-04 18:02:32 +0000320 fl.l_whence = SEEK_SET;
321 fl.l_start = 0;
322 fl.l_len = 1;
323 fl.l_type = F_WRLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000324 fcntl(G.logFD, F_SETLKW, &fl);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000325
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000326#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000327 if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) {
328 if (G.logFileRotate) { /* always 0..99 */
329 int i = strlen(G.logFilePath) + 3 + 1;
Denis Vlasenkob8934972007-01-04 18:02:32 +0000330 char oldFile[i];
331 char newFile[i];
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000332 i = G.logFileRotate - 1;
Denis Vlasenkob8934972007-01-04 18:02:32 +0000333 /* rename: f.8 -> f.9; f.7 -> f.8; ... */
334 while (1) {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000335 sprintf(newFile, "%s.%d", G.logFilePath, i);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000336 if (i == 0) break;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000337 sprintf(oldFile, "%s.%d", G.logFilePath, --i);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000338 rename(oldFile, newFile);
Eric Andersen29c77f72003-10-09 09:43:18 +0000339 }
Denis Vlasenkob8934972007-01-04 18:02:32 +0000340 /* newFile == "f.0" now */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000341 rename(G.logFilePath, newFile);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000342 fl.l_type = F_UNLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000343 fcntl(G.logFD, F_SETLKW, &fl);
344 close(G.logFD);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000345 goto reopen;
Eric Andersen29c77f72003-10-09 09:43:18 +0000346 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000347 ftruncate(G.logFD, 0);
Eric Andersen3843e961999-11-25 07:30:46 +0000348 }
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000349 G.curFileSize +=
Denis Vlasenkob8934972007-01-04 18:02:32 +0000350#endif
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000351 full_write(G.logFD, msg, len);
Denis Vlasenkob8934972007-01-04 18:02:32 +0000352 fl.l_type = F_UNLCK;
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000353 fcntl(G.logFD, F_SETLKW, &fl);
Eric Andersenb99df0f1999-11-24 09:04:33 +0000354}
355
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000356static void parse_fac_prio_20(int pri, char *res20)
Eric Andersen3843e961999-11-25 07:30:46 +0000357{
Denis Vlasenko3c8b5ba2007-06-04 18:23:59 +0000358 const CODE *c_pri, *c_fac;
Eric Andersenb99df0f1999-11-24 09:04:33 +0000359
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000360 if (pri != 0) {
Denis Vlasenko14c19402006-09-30 19:20:00 +0000361 c_fac = facilitynames;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000362 while (c_fac->c_name) {
363 if (c_fac->c_val != (LOG_FAC(pri) << 3)) {
364 c_fac++; continue;
365 }
366 /* facility is found, look for prio */
367 c_pri = prioritynames;
368 while (c_pri->c_name) {
369 if (c_pri->c_val != LOG_PRI(pri)) {
370 c_pri++; continue;
371 }
372 snprintf(res20, 20, "%s.%s",
373 c_fac->c_name, c_pri->c_name);
374 return;
375 }
376 /* prio not found, bail out */
377 break;
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000378 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000379 snprintf(res20, 20, "<%d>", pri);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000380 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000381}
Eric Andersen3843e961999-11-25 07:30:46 +0000382
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000383/* len parameter is used only for "is there a timestamp?" check.
Denis Vlasenko018b1552007-11-06 01:38:46 +0000384 * NB: some callers cheat and supply len==0 when they know
385 * that there is no timestamp, short-circuiting the test. */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000386static void timestamp_and_log(int pri, char *msg, int len)
387{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000388 char *timestamp;
389
390 if (len < 16 || msg[3] != ' ' || msg[6] != ' '
391 || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
392 ) {
Denis Vlasenkobae79482007-01-09 23:42:43 +0000393 time_t now;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000394 time(&now);
395 timestamp = ctime(&now) + 4;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000396 } else {
397 timestamp = msg;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000398 msg += 16;
399 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000400 timestamp[15] = '\0';
Eric Andersen3843e961999-11-25 07:30:46 +0000401
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000402 /* Log message locally (to file or shared mem) */
403 if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000404 if (LOG_PRI(pri) < G.logLevel) {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000405 if (option_mask32 & OPT_small)
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000406 sprintf(G.printbuf, "%s %s\n", timestamp, msg);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000407 else {
408 char res[20];
409 parse_fac_prio_20(pri, res);
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000410 sprintf(G.printbuf, "%s %s %s %s\n", timestamp, G.localHostName, res, msg);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000411 }
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000412 log_locally(G.printbuf);
Eric Andersenbf2b8ae2000-12-08 19:52:01 +0000413 }
414 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000415}
Glenn L McGrath73ebb882004-09-14 18:12:13 +0000416
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000417static void split_escape_and_log(char *tmpbuf, int len)
418{
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000419 char *p = tmpbuf;
420
421 tmpbuf += len;
422 while (p < tmpbuf) {
423 char c;
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000424 char *q = G.parsebuf;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000425 int pri = (LOG_USER | LOG_NOTICE);
426
427 if (*p == '<') {
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000428 /* Parse the magic priority number */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000429 pri = bb_strtou(p + 1, &p, 10);
Denis Vlasenko018b1552007-11-06 01:38:46 +0000430 if (*p == '>')
431 p++;
432 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000433 pri = (LOG_USER | LOG_NOTICE);
Denis Vlasenko1decd0e2006-09-30 19:17:40 +0000434 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000435
436 while ((c = *p++)) {
437 if (c == '\n')
438 c = ' ';
Denis Vlasenko2ee028d2007-06-21 13:44:53 +0000439 if (!(c & ~0x1f) && c != '\t') {
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000440 *q++ = '^';
441 c += '@'; /* ^@, ^A, ^B... */
442 }
443 *q++ = c;
444 }
445 *q = '\0';
Denis Vlasenkoa0e2a0a2007-01-04 21:22:11 +0000446 /* Now log it */
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000447 timestamp_and_log(pri, G.parsebuf, q - G.parsebuf);
Eric Andersen7f94a5c2004-06-22 10:12:59 +0000448 }
Eric Andersen3843e961999-11-25 07:30:46 +0000449}
450
451static void quit_signal(int sig)
452{
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000453 timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)"syslogd exiting", 0);
Denis Vlasenko1c962f22007-01-09 23:44:57 +0000454 puts("syslogd exiting");
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000455 if (ENABLE_FEATURE_IPC_SYSLOG)
456 ipcsyslog_cleanup();
Denis Vlasenko02be0f52006-09-30 19:21:24 +0000457 exit(1);
Eric Andersen3843e961999-11-25 07:30:46 +0000458}
459
Denis Vlasenko4998c812007-02-14 20:51:46 +0000460#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000461static void do_mark(int sig)
Eric Andersen3843e961999-11-25 07:30:46 +0000462{
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000463 if (G.markInterval) {
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000464 timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)"-- MARK --", 0);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000465 alarm(G.markInterval);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000466 }
Eric Andersen3843e961999-11-25 07:30:46 +0000467}
Denis Vlasenko4998c812007-02-14 20:51:46 +0000468#endif
Eric Andersen3843e961999-11-25 07:30:46 +0000469
Denis Vlasenkod031b202007-11-10 01:28:19 +0000470/* Don't inline: prevent struct sockaddr_un to take up space on stack
471 * permanently */
472static NOINLINE int create_socket(void)
Eric Andersen3843e961999-11-25 07:30:46 +0000473{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000474 struct sockaddr_un sunx;
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000475 int sock_fd;
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000476 char *dev_log_name;
Erik Andersenf13df372000-04-18 23:51:51 +0000477
Denis Vlasenkod031b202007-11-10 01:28:19 +0000478 memset(&sunx, 0, sizeof(sunx));
479 sunx.sun_family = AF_UNIX;
480
481 /* Unlink old /dev/log or object it points to. */
482 /* (if it exists, bind will fail) */
483 strcpy(sunx.sun_path, "/dev/log");
484 dev_log_name = xmalloc_follow_symlinks("/dev/log");
485 if (dev_log_name) {
486 safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
487 free(dev_log_name);
488 }
489 unlink(sunx.sun_path);
490
491 sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
492 xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
493 chmod("/dev/log", 0666);
494
495 return sock_fd;
496}
497
498static void do_syslogd(void) ATTRIBUTE_NORETURN;
499static void do_syslogd(void)
500{
501 int sock_fd;
502
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000503 /* Set up signal handlers */
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000504 signal(SIGINT, quit_signal);
505 signal(SIGTERM, quit_signal);
506 signal(SIGQUIT, quit_signal);
507 signal(SIGHUP, SIG_IGN);
508 signal(SIGCHLD, SIG_IGN);
Pavel Roskind39d1202000-09-13 14:14:29 +0000509#ifdef SIGCLD
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000510 signal(SIGCLD, SIG_IGN);
Pavel Roskind39d1202000-09-13 14:14:29 +0000511#endif
Denis Vlasenko4998c812007-02-14 20:51:46 +0000512#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000513 signal(SIGALRM, do_mark);
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000514 alarm(G.markInterval);
Denis Vlasenko4998c812007-02-14 20:51:46 +0000515#endif
Denis Vlasenkod031b202007-11-10 01:28:19 +0000516 sock_fd = create_socket();
Eric Andersenb99df0f1999-11-24 09:04:33 +0000517
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000518 if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000519 ipcsyslog_init();
Eric Andersenea906502001-04-05 20:55:17 +0000520 }
Eric Andersenea906502001-04-05 20:55:17 +0000521
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000522 timestamp_and_log(LOG_SYSLOG | LOG_INFO,
523 (char*)"syslogd started: BusyBox v" BB_VER, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524
Erik Andersen983b51b2000-04-04 18:14:25 +0000525 for (;;) {
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000526 size_t sz;
Denis Vlasenko018b1552007-11-06 01:38:46 +0000527 read_again:
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +0000528 sz = safe_read(sock_fd, G.recvbuf, MAX_READ - 1);
Denis Vlasenko018b1552007-11-06 01:38:46 +0000529 if (sz < 0) {
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000530 bb_perror_msg_and_die("read from /dev/log");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000531 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000532
Denis Vlasenko018b1552007-11-06 01:38:46 +0000533 /* Drop trailing NULs (typically there is one NUL) */
534 while (1) {
535 if (sz == 0)
536 goto read_again;
Denis Vlasenkocb12cb22007-11-06 11:34:03 +0000537 /* man 3 syslog says: "A trailing newline is added when needed".
538 * However, neither glibc nor uclibc do this:
539 * syslog(prio, "test") sends "test\0" to /dev/log,
540 * syslog(prio, "test\n") sends "test\n\0",
541 * IOW: newline is passed verbatim!
542 * I take it to mean that it's syslogd's job
543 * to make those look identical in the log files */
544 if (G.recvbuf[sz-1] && G.recvbuf[sz-1] != '\n')
Denis Vlasenko018b1552007-11-06 01:38:46 +0000545 break;
546 sz--;
547 }
Denis Vlasenkocb12cb22007-11-06 11:34:03 +0000548 /* Maybe we need to add '\n' here, not later?
549 * It looks like stock syslogd does send '\n' over network,
550 * but we do not (see sendto below) */
Denis Vlasenko018b1552007-11-06 01:38:46 +0000551 G.recvbuf[sz] = '\0'; /* make sure it *is* NUL terminated */
552
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000553 /* TODO: maybe suppress duplicates? */
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000554#if ENABLE_FEATURE_REMOTE_LOG
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000555 /* We are not modifying log messages in any way before send */
556 /* Remote site cannot trust _us_ anyway and need to do validation again */
557 if (G.remoteAddr) {
558 if (-1 == G.remoteFD) {
559 G.remoteFD = socket(G.remoteAddr->sa.sa_family, SOCK_DGRAM, 0);
Glenn L McGrath912d8f42002-11-10 22:46:45 +0000560 }
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000561 if (-1 != G.remoteFD) {
562 /* send message to remote logger, ignore possible error */
563 sendto(G.remoteFD, G.recvbuf, sz, MSG_DONTWAIT,
564 &G.remoteAddr->sa, G.remoteAddr->len);
565 }
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000566 }
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000567#endif
Denis Vlasenkod7ecd862007-09-07 13:53:32 +0000568 split_escape_and_log(G.recvbuf, sz);
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000569 } /* for */
Eric Andersenb99df0f1999-11-24 09:04:33 +0000570}
571
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000572int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000573int syslogd_main(int argc, char **argv)
Eric Andersen3843e961999-11-25 07:30:46 +0000574{
Denis Vlasenko14c19402006-09-30 19:20:00 +0000575 char OPTION_DECL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000576 char *p;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000577
Denis Vlasenko4f93cde2007-03-20 20:03:03 +0000578 PTR_TO_GLOBALS = memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data));
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000579
Eric Andersen394cf222000-12-11 16:48:50 +0000580 /* do normal option parsing */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000581 opt_complementary = "=0"; /* no non-option params */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000582 getopt32(argv, OPTION_STR, OPTION_PARAM);
Denis Vlasenko4998c812007-02-14 20:51:46 +0000583#ifdef SYSLOGD_MARK
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000584 if (option_mask32 & OPT_mark) // -m
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000585 G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
Denis Vlasenko4998c812007-02-14 20:51:46 +0000586#endif
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000587 //if (option_mask32 & OPT_nofork) // -n
588 //if (option_mask32 & OPT_outfile) // -O
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000589 if (option_mask32 & OPT_loglevel) // -l
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000590 G.logLevel = xatou_range(opt_l, 1, 8);
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000591 //if (option_mask32 & OPT_small) // -S
Denis Vlasenko14c19402006-09-30 19:20:00 +0000592#if ENABLE_FEATURE_ROTATE_LOGFILE
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000593 if (option_mask32 & OPT_filesize) // -s
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000594 G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000595 if (option_mask32 & OPT_rotatecnt) // -b
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000596 G.logFileRotate = xatou_range(opt_b, 0, 99);
Denis Vlasenko14c19402006-09-30 19:20:00 +0000597#endif
598#if ENABLE_FEATURE_REMOTE_LOG
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000599 if (option_mask32 & OPT_remotelog) { // -R
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000600 G.remoteAddr = xhost2sockaddr(opt_R, 514);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000601 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000602 //if (option_mask32 & OPT_locallog) // -L
Denis Vlasenko14c19402006-09-30 19:20:00 +0000603#endif
604#if ENABLE_FEATURE_IPC_SYSLOG
Denis Vlasenko218f2f42007-01-24 22:02:01 +0000605 if (opt_C) // -Cn
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000606 G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
Denis Vlasenko14c19402006-09-30 19:20:00 +0000607#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000608
Eric Andersen4ed17822000-12-11 19:28:29 +0000609 /* If they have not specified remote logging, then log locally */
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000610 if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_remotelog))
611 option_mask32 |= OPT_locallog;
Mark Whitley6317c4b2001-03-12 22:51:50 +0000612
Erik Andersene49d5ec2000-02-08 19:58:47 +0000613 /* Store away localhost's name before the fork */
Denis Vlasenko5e892ba2007-03-15 19:50:46 +0000614 gethostname(G.localHostName, sizeof(G.localHostName));
615 p = strchr(G.localHostName, '.');
Denis Vlasenko14c19402006-09-30 19:20:00 +0000616 if (p) {
Glenn L McGrathfe538ba2003-09-10 23:35:45 +0000617 *p = '\0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000618 }
619
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000620 if (!(option_mask32 & OPT_nofork)) {
Denis Vlasenko5a142022007-03-26 13:20:54 +0000621 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
Eric Andersen35e643b2003-07-28 07:40:39 +0000622 }
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000623 umask(0);
Denis Vlasenko10457b92007-03-27 22:01:31 +0000624 write_pidfile("/var/run/syslogd.pid");
Denis Vlasenkoceab8702007-01-04 17:57:54 +0000625 do_syslogd();
Denis Vlasenko8a820b22007-01-06 22:08:53 +0000626 /* return EXIT_SUCCESS; */
Eric Andersen3843e961999-11-25 07:30:46 +0000627}