1 | /* vi: set sw=4 ts=4: */
|
---|
2 | /*
|
---|
3 | * Mini syslogd implementation for busybox
|
---|
4 | *
|
---|
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
---|
6 | *
|
---|
7 | * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
|
---|
8 | *
|
---|
9 | * "circular buffer" Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>
|
---|
10 | *
|
---|
11 | * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
|
---|
12 | *
|
---|
13 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
14 | */
|
---|
15 |
|
---|
16 | /*
|
---|
17 | * Done in syslogd_and_logger.c:
|
---|
18 | #include "libbb.h"
|
---|
19 | #define SYSLOG_NAMES
|
---|
20 | #define SYSLOG_NAMES_CONST
|
---|
21 | #include <syslog.h>
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include <sys/un.h>
|
---|
25 | #include <sys/uio.h>
|
---|
26 |
|
---|
27 | #if ENABLE_FEATURE_REMOTE_LOG
|
---|
28 | #include <netinet/in.h>
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | #if ENABLE_FEATURE_IPC_SYSLOG
|
---|
32 | #include <sys/ipc.h>
|
---|
33 | #include <sys/sem.h>
|
---|
34 | #include <sys/shm.h>
|
---|
35 | #endif
|
---|
36 |
|
---|
37 |
|
---|
38 | #define DEBUG 0
|
---|
39 |
|
---|
40 | /* 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 |
|
---|
45 | /* Write locking does not seem to be useful either */
|
---|
46 | #undef SYSLOGD_WRLOCK
|
---|
47 |
|
---|
48 | enum {
|
---|
49 | MAX_READ = CONFIG_FEATURE_SYSLOGD_READ_BUFFER_SIZE,
|
---|
50 | DNS_WAIT_SEC = 2 * 60,
|
---|
51 | };
|
---|
52 |
|
---|
53 | /* Semaphore operation structures */
|
---|
54 | struct shbuf_ds {
|
---|
55 | int32_t size; /* size of data - 1 */
|
---|
56 | int32_t tail; /* end of message list */
|
---|
57 | char data[1]; /* data/messages */
|
---|
58 | };
|
---|
59 |
|
---|
60 | #if ENABLE_FEATURE_REMOTE_LOG
|
---|
61 | typedef struct {
|
---|
62 | int remoteFD;
|
---|
63 | unsigned last_dns_resolve;
|
---|
64 | len_and_sockaddr *remoteAddr;
|
---|
65 | const char *remoteHostname;
|
---|
66 | } remoteHost_t;
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | /* Allows us to have smaller initializer. Ugly. */
|
---|
70 | #define GLOBALS \
|
---|
71 | const char *logFilePath; \
|
---|
72 | int logFD; \
|
---|
73 | /* interval between marks in seconds */ \
|
---|
74 | /*int markInterval;*/ \
|
---|
75 | /* level of messages to be logged */ \
|
---|
76 | int logLevel; \
|
---|
77 | IF_FEATURE_ROTATE_LOGFILE( \
|
---|
78 | /* max size of file before rotation */ \
|
---|
79 | unsigned logFileSize; \
|
---|
80 | /* number of rotated message files */ \
|
---|
81 | unsigned logFileRotate; \
|
---|
82 | unsigned curFileSize; \
|
---|
83 | smallint isRegular; \
|
---|
84 | ) \
|
---|
85 | IF_FEATURE_IPC_SYSLOG( \
|
---|
86 | int shmid; /* ipc shared memory id */ \
|
---|
87 | int s_semid; /* ipc semaphore id */ \
|
---|
88 | int shm_size; \
|
---|
89 | struct sembuf SMwup[1]; \
|
---|
90 | struct sembuf SMwdn[3]; \
|
---|
91 | )
|
---|
92 |
|
---|
93 | struct init_globals {
|
---|
94 | GLOBALS
|
---|
95 | };
|
---|
96 |
|
---|
97 | struct globals {
|
---|
98 | GLOBALS
|
---|
99 |
|
---|
100 | #if ENABLE_FEATURE_REMOTE_LOG
|
---|
101 | llist_t *remoteHosts;
|
---|
102 | #endif
|
---|
103 | #if ENABLE_FEATURE_IPC_SYSLOG
|
---|
104 | struct shbuf_ds *shbuf;
|
---|
105 | #endif
|
---|
106 | time_t last_log_time;
|
---|
107 | /* localhost's name. We print only first 64 chars */
|
---|
108 | char *hostname;
|
---|
109 |
|
---|
110 | /* We recv into recvbuf... */
|
---|
111 | char recvbuf[MAX_READ * (1 + ENABLE_FEATURE_SYSLOGD_DUP)];
|
---|
112 | /* ...then copy to parsebuf, escaping control chars */
|
---|
113 | /* (can grow x2 max) */
|
---|
114 | char parsebuf[MAX_READ*2];
|
---|
115 | /* ...then sprintf into printbuf, adding timestamp (15 chars),
|
---|
116 | * host (64), fac.prio (20) to the message */
|
---|
117 | /* (growth by: 15 + 64 + 20 + delims = ~110) */
|
---|
118 | char printbuf[MAX_READ*2 + 128];
|
---|
119 | };
|
---|
120 |
|
---|
121 | static const struct init_globals init_data = {
|
---|
122 | .logFilePath = "/var/log/messages",
|
---|
123 | .logFD = -1,
|
---|
124 | #ifdef SYSLOGD_MARK
|
---|
125 | .markInterval = 20 * 60,
|
---|
126 | #endif
|
---|
127 | .logLevel = 8,
|
---|
128 | #if ENABLE_FEATURE_ROTATE_LOGFILE
|
---|
129 | .logFileSize = 200 * 1024,
|
---|
130 | .logFileRotate = 1,
|
---|
131 | #endif
|
---|
132 | #if ENABLE_FEATURE_IPC_SYSLOG
|
---|
133 | .shmid = -1,
|
---|
134 | .s_semid = -1,
|
---|
135 | .shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024), // default shm size
|
---|
136 | .SMwup = { {1, -1, IPC_NOWAIT} },
|
---|
137 | .SMwdn = { {0, 0}, {1, 0}, {1, +1} },
|
---|
138 | #endif
|
---|
139 | };
|
---|
140 |
|
---|
141 | #define G (*ptr_to_globals)
|
---|
142 | #define INIT_G() do { \
|
---|
143 | SET_PTR_TO_GLOBALS(memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data))); \
|
---|
144 | } while (0)
|
---|
145 |
|
---|
146 |
|
---|
147 | /* Options */
|
---|
148 | enum {
|
---|
149 | OPTBIT_mark = 0, // -m
|
---|
150 | OPTBIT_nofork, // -n
|
---|
151 | OPTBIT_outfile, // -O
|
---|
152 | OPTBIT_loglevel, // -l
|
---|
153 | OPTBIT_small, // -S
|
---|
154 | IF_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
|
---|
155 | IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
|
---|
156 | IF_FEATURE_REMOTE_LOG( OPTBIT_remotelog ,) // -R
|
---|
157 | IF_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L
|
---|
158 | IF_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
|
---|
159 | IF_FEATURE_SYSLOGD_DUP( OPTBIT_dup ,) // -D
|
---|
160 |
|
---|
161 | OPT_mark = 1 << OPTBIT_mark ,
|
---|
162 | OPT_nofork = 1 << OPTBIT_nofork ,
|
---|
163 | OPT_outfile = 1 << OPTBIT_outfile ,
|
---|
164 | OPT_loglevel = 1 << OPTBIT_loglevel,
|
---|
165 | OPT_small = 1 << OPTBIT_small ,
|
---|
166 | OPT_filesize = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
|
---|
167 | OPT_rotatecnt = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
|
---|
168 | OPT_remotelog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_remotelog )) + 0,
|
---|
169 | OPT_locallog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0,
|
---|
170 | OPT_circularlog = IF_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
|
---|
171 | OPT_dup = IF_FEATURE_SYSLOGD_DUP( (1 << OPTBIT_dup )) + 0,
|
---|
172 | };
|
---|
173 | #define OPTION_STR "m:nO:l:S" \
|
---|
174 | IF_FEATURE_ROTATE_LOGFILE("s:" ) \
|
---|
175 | IF_FEATURE_ROTATE_LOGFILE("b:" ) \
|
---|
176 | IF_FEATURE_REMOTE_LOG( "R:" ) \
|
---|
177 | IF_FEATURE_REMOTE_LOG( "L" ) \
|
---|
178 | IF_FEATURE_IPC_SYSLOG( "C::") \
|
---|
179 | IF_FEATURE_SYSLOGD_DUP( "D" )
|
---|
180 | #define OPTION_DECL *opt_m, *opt_l \
|
---|
181 | IF_FEATURE_ROTATE_LOGFILE(,*opt_s) \
|
---|
182 | IF_FEATURE_ROTATE_LOGFILE(,*opt_b) \
|
---|
183 | IF_FEATURE_IPC_SYSLOG( ,*opt_C = NULL)
|
---|
184 | #define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \
|
---|
185 | IF_FEATURE_ROTATE_LOGFILE(,&opt_s) \
|
---|
186 | IF_FEATURE_ROTATE_LOGFILE(,&opt_b) \
|
---|
187 | IF_FEATURE_REMOTE_LOG( ,&remoteAddrList) \
|
---|
188 | IF_FEATURE_IPC_SYSLOG( ,&opt_C)
|
---|
189 |
|
---|
190 |
|
---|
191 | /* circular buffer variables/structures */
|
---|
192 | #if ENABLE_FEATURE_IPC_SYSLOG
|
---|
193 |
|
---|
194 | #if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4
|
---|
195 | #error Sorry, you must set the syslogd buffer size to at least 4KB.
|
---|
196 | #error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE
|
---|
197 | #endif
|
---|
198 |
|
---|
199 | /* our shared key (syslogd.c and logread.c must be in sync) */
|
---|
200 | enum { KEY_ID = 0x414e4547 }; /* "GENA" */
|
---|
201 |
|
---|
202 | static void ipcsyslog_cleanup(void)
|
---|
203 | {
|
---|
204 | if (G.shmid != -1) {
|
---|
205 | shmdt(G.shbuf);
|
---|
206 | }
|
---|
207 | if (G.shmid != -1) {
|
---|
208 | shmctl(G.shmid, IPC_RMID, NULL);
|
---|
209 | }
|
---|
210 | if (G.s_semid != -1) {
|
---|
211 | semctl(G.s_semid, 0, IPC_RMID, 0);
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | static void ipcsyslog_init(void)
|
---|
216 | {
|
---|
217 | if (DEBUG)
|
---|
218 | printf("shmget(%x, %d,...)\n", (int)KEY_ID, G.shm_size);
|
---|
219 |
|
---|
220 | G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 0644);
|
---|
221 | if (G.shmid == -1) {
|
---|
222 | bb_perror_msg_and_die("shmget");
|
---|
223 | }
|
---|
224 |
|
---|
225 | G.shbuf = shmat(G.shmid, NULL, 0);
|
---|
226 | if (G.shbuf == (void*) -1L) { /* shmat has bizarre error return */
|
---|
227 | bb_perror_msg_and_die("shmat");
|
---|
228 | }
|
---|
229 |
|
---|
230 | memset(G.shbuf, 0, G.shm_size);
|
---|
231 | G.shbuf->size = G.shm_size - offsetof(struct shbuf_ds, data) - 1;
|
---|
232 | /*G.shbuf->tail = 0;*/
|
---|
233 |
|
---|
234 | // we'll trust the OS to set initial semval to 0 (let's hope)
|
---|
235 | G.s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023);
|
---|
236 | if (G.s_semid == -1) {
|
---|
237 | if (errno == EEXIST) {
|
---|
238 | G.s_semid = semget(KEY_ID, 2, 0);
|
---|
239 | if (G.s_semid != -1)
|
---|
240 | return;
|
---|
241 | }
|
---|
242 | bb_perror_msg_and_die("semget");
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | /* Write message to shared mem buffer */
|
---|
247 | static void log_to_shmem(const char *msg, int len)
|
---|
248 | {
|
---|
249 | int old_tail, new_tail;
|
---|
250 |
|
---|
251 | if (semop(G.s_semid, G.SMwdn, 3) == -1) {
|
---|
252 | bb_perror_msg_and_die("SMwdn");
|
---|
253 | }
|
---|
254 |
|
---|
255 | /* Circular Buffer Algorithm:
|
---|
256 | * --------------------------
|
---|
257 | * tail == position where to store next syslog message.
|
---|
258 | * tail's max value is (shbuf->size - 1)
|
---|
259 | * Last byte of buffer is never used and remains NUL.
|
---|
260 | */
|
---|
261 | len++; /* length with NUL included */
|
---|
262 | again:
|
---|
263 | old_tail = G.shbuf->tail;
|
---|
264 | new_tail = old_tail + len;
|
---|
265 | if (new_tail < G.shbuf->size) {
|
---|
266 | /* store message, set new tail */
|
---|
267 | memcpy(G.shbuf->data + old_tail, msg, len);
|
---|
268 | G.shbuf->tail = new_tail;
|
---|
269 | } else {
|
---|
270 | /* k == available buffer space ahead of old tail */
|
---|
271 | int k = G.shbuf->size - old_tail;
|
---|
272 | /* copy what fits to the end of buffer, and repeat */
|
---|
273 | memcpy(G.shbuf->data + old_tail, msg, k);
|
---|
274 | msg += k;
|
---|
275 | len -= k;
|
---|
276 | G.shbuf->tail = 0;
|
---|
277 | goto again;
|
---|
278 | }
|
---|
279 | if (semop(G.s_semid, G.SMwup, 1) == -1) {
|
---|
280 | bb_perror_msg_and_die("SMwup");
|
---|
281 | }
|
---|
282 | if (DEBUG)
|
---|
283 | printf("tail:%d\n", G.shbuf->tail);
|
---|
284 | }
|
---|
285 | #else
|
---|
286 | void ipcsyslog_cleanup(void);
|
---|
287 | void ipcsyslog_init(void);
|
---|
288 | void log_to_shmem(const char *msg);
|
---|
289 | #endif /* FEATURE_IPC_SYSLOG */
|
---|
290 |
|
---|
291 |
|
---|
292 | /* Print a message to the log file. */
|
---|
293 | static void log_locally(time_t now, char *msg)
|
---|
294 | {
|
---|
295 | #ifdef SYSLOGD_WRLOCK
|
---|
296 | struct flock fl;
|
---|
297 | #endif
|
---|
298 | int len = strlen(msg);
|
---|
299 |
|
---|
300 | #if ENABLE_FEATURE_IPC_SYSLOG
|
---|
301 | if ((option_mask32 & OPT_circularlog) && G.shbuf) {
|
---|
302 | log_to_shmem(msg, len);
|
---|
303 | return;
|
---|
304 | }
|
---|
305 | #endif
|
---|
306 | if (G.logFD >= 0) {
|
---|
307 | /* Reopen log file every second. This allows admin
|
---|
308 | * to delete the file and not worry about restarting us.
|
---|
309 | * This costs almost nothing since it happens
|
---|
310 | * _at most_ once a second.
|
---|
311 | */
|
---|
312 | if (!now)
|
---|
313 | now = time(NULL);
|
---|
314 | if (G.last_log_time != now) {
|
---|
315 | G.last_log_time = now;
|
---|
316 | close(G.logFD);
|
---|
317 | goto reopen;
|
---|
318 | }
|
---|
319 | } else {
|
---|
320 | reopen:
|
---|
321 | G.logFD = open(G.logFilePath, O_WRONLY | O_CREAT
|
---|
322 | | O_NOCTTY | O_APPEND | O_NONBLOCK,
|
---|
323 | 0666);
|
---|
324 | if (G.logFD < 0) {
|
---|
325 | /* cannot open logfile? - print to /dev/console then */
|
---|
326 | int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
|
---|
327 | if (fd < 0)
|
---|
328 | fd = 2; /* then stderr, dammit */
|
---|
329 | full_write(fd, msg, len);
|
---|
330 | if (fd != 2)
|
---|
331 | close(fd);
|
---|
332 | return;
|
---|
333 | }
|
---|
334 | #if ENABLE_FEATURE_ROTATE_LOGFILE
|
---|
335 | {
|
---|
336 | struct stat statf;
|
---|
337 | G.isRegular = (fstat(G.logFD, &statf) == 0 && S_ISREG(statf.st_mode));
|
---|
338 | /* bug (mostly harmless): can wrap around if file > 4gb */
|
---|
339 | G.curFileSize = statf.st_size;
|
---|
340 | }
|
---|
341 | #endif
|
---|
342 | }
|
---|
343 |
|
---|
344 | #ifdef SYSLOGD_WRLOCK
|
---|
345 | fl.l_whence = SEEK_SET;
|
---|
346 | fl.l_start = 0;
|
---|
347 | fl.l_len = 1;
|
---|
348 | fl.l_type = F_WRLCK;
|
---|
349 | fcntl(G.logFD, F_SETLKW, &fl);
|
---|
350 | #endif
|
---|
351 |
|
---|
352 | #if ENABLE_FEATURE_ROTATE_LOGFILE
|
---|
353 | if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) {
|
---|
354 | if (G.logFileRotate) { /* always 0..99 */
|
---|
355 | int i = strlen(G.logFilePath) + 3 + 1;
|
---|
356 | char oldFile[i];
|
---|
357 | char newFile[i];
|
---|
358 | i = G.logFileRotate - 1;
|
---|
359 | /* rename: f.8 -> f.9; f.7 -> f.8; ... */
|
---|
360 | while (1) {
|
---|
361 | sprintf(newFile, "%s.%d", G.logFilePath, i);
|
---|
362 | if (i == 0) break;
|
---|
363 | sprintf(oldFile, "%s.%d", G.logFilePath, --i);
|
---|
364 | /* ignore errors - file might be missing */
|
---|
365 | rename(oldFile, newFile);
|
---|
366 | }
|
---|
367 | /* newFile == "f.0" now */
|
---|
368 | rename(G.logFilePath, newFile);
|
---|
369 | #ifdef SYSLOGD_WRLOCK
|
---|
370 | fl.l_type = F_UNLCK;
|
---|
371 | fcntl(G.logFD, F_SETLKW, &fl);
|
---|
372 | #endif
|
---|
373 | close(G.logFD);
|
---|
374 | goto reopen;
|
---|
375 | }
|
---|
376 | ftruncate(G.logFD, 0);
|
---|
377 | }
|
---|
378 | G.curFileSize +=
|
---|
379 | #endif
|
---|
380 | full_write(G.logFD, msg, len);
|
---|
381 | #ifdef SYSLOGD_WRLOCK
|
---|
382 | fl.l_type = F_UNLCK;
|
---|
383 | fcntl(G.logFD, F_SETLKW, &fl);
|
---|
384 | #endif
|
---|
385 | }
|
---|
386 |
|
---|
387 | static void parse_fac_prio_20(int pri, char *res20)
|
---|
388 | {
|
---|
389 | const CODE *c_pri, *c_fac;
|
---|
390 |
|
---|
391 | if (pri != 0) {
|
---|
392 | c_fac = facilitynames;
|
---|
393 | while (c_fac->c_name) {
|
---|
394 | if (c_fac->c_val != (LOG_FAC(pri) << 3)) {
|
---|
395 | c_fac++;
|
---|
396 | continue;
|
---|
397 | }
|
---|
398 | /* facility is found, look for prio */
|
---|
399 | c_pri = prioritynames;
|
---|
400 | while (c_pri->c_name) {
|
---|
401 | if (c_pri->c_val != LOG_PRI(pri)) {
|
---|
402 | c_pri++;
|
---|
403 | continue;
|
---|
404 | }
|
---|
405 | snprintf(res20, 20, "%s.%s",
|
---|
406 | c_fac->c_name, c_pri->c_name);
|
---|
407 | return;
|
---|
408 | }
|
---|
409 | /* prio not found, bail out */
|
---|
410 | break;
|
---|
411 | }
|
---|
412 | snprintf(res20, 20, "<%d>", pri);
|
---|
413 | }
|
---|
414 | }
|
---|
415 |
|
---|
416 | /* len parameter is used only for "is there a timestamp?" check.
|
---|
417 | * NB: some callers cheat and supply len==0 when they know
|
---|
418 | * that there is no timestamp, short-circuiting the test. */
|
---|
419 | static void timestamp_and_log(int pri, char *msg, int len)
|
---|
420 | {
|
---|
421 | char *timestamp;
|
---|
422 | time_t now;
|
---|
423 |
|
---|
424 | /* Jan 18 00:11:22 msg... */
|
---|
425 | /* 01234567890123456 */
|
---|
426 | if (len < 16 || msg[3] != ' ' || msg[6] != ' '
|
---|
427 | || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
|
---|
428 | ) {
|
---|
429 | time(&now);
|
---|
430 | timestamp = ctime(&now) + 4; /* skip day of week */
|
---|
431 | } else {
|
---|
432 | now = 0;
|
---|
433 | timestamp = msg;
|
---|
434 | msg += 16;
|
---|
435 | }
|
---|
436 | timestamp[15] = '\0';
|
---|
437 |
|
---|
438 | if (option_mask32 & OPT_small)
|
---|
439 | sprintf(G.printbuf, "%s %s\n", timestamp, msg);
|
---|
440 | else {
|
---|
441 | char res[20];
|
---|
442 | parse_fac_prio_20(pri, res);
|
---|
443 | sprintf(G.printbuf, "%s %.64s %s %s\n", timestamp, G.hostname, res, msg);
|
---|
444 | }
|
---|
445 |
|
---|
446 | /* Log message locally (to file or shared mem) */
|
---|
447 | log_locally(now, G.printbuf);
|
---|
448 | }
|
---|
449 |
|
---|
450 | static void timestamp_and_log_internal(const char *msg)
|
---|
451 | {
|
---|
452 | /* -L, or no -R */
|
---|
453 | if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog))
|
---|
454 | return;
|
---|
455 | timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0);
|
---|
456 | }
|
---|
457 |
|
---|
458 | /* tmpbuf[len] is a NUL byte (set by caller), but there can be other,
|
---|
459 | * embedded NULs. Split messages on each of these NULs, parse prio,
|
---|
460 | * escape control chars and log each locally. */
|
---|
461 | static void split_escape_and_log(char *tmpbuf, int len)
|
---|
462 | {
|
---|
463 | char *p = tmpbuf;
|
---|
464 |
|
---|
465 | tmpbuf += len;
|
---|
466 | while (p < tmpbuf) {
|
---|
467 | char c;
|
---|
468 | char *q = G.parsebuf;
|
---|
469 | int pri = (LOG_USER | LOG_NOTICE);
|
---|
470 |
|
---|
471 | if (*p == '<') {
|
---|
472 | /* Parse the magic priority number */
|
---|
473 | pri = bb_strtou(p + 1, &p, 10);
|
---|
474 | if (*p == '>')
|
---|
475 | p++;
|
---|
476 | if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
|
---|
477 | pri = (LOG_USER | LOG_NOTICE);
|
---|
478 | }
|
---|
479 |
|
---|
480 | while ((c = *p++)) {
|
---|
481 | if (c == '\n')
|
---|
482 | c = ' ';
|
---|
483 | if (!(c & ~0x1f) && c != '\t') {
|
---|
484 | *q++ = '^';
|
---|
485 | c += '@'; /* ^@, ^A, ^B... */
|
---|
486 | }
|
---|
487 | *q++ = c;
|
---|
488 | }
|
---|
489 | *q = '\0';
|
---|
490 |
|
---|
491 | /* Now log it */
|
---|
492 | if (LOG_PRI(pri) < G.logLevel)
|
---|
493 | timestamp_and_log(pri, G.parsebuf, q - G.parsebuf);
|
---|
494 | }
|
---|
495 | }
|
---|
496 |
|
---|
497 | #ifdef SYSLOGD_MARK
|
---|
498 | static void do_mark(int sig)
|
---|
499 | {
|
---|
500 | if (G.markInterval) {
|
---|
501 | timestamp_and_log_internal("-- MARK --");
|
---|
502 | alarm(G.markInterval);
|
---|
503 | }
|
---|
504 | }
|
---|
505 | #endif
|
---|
506 |
|
---|
507 | /* Don't inline: prevent struct sockaddr_un to take up space on stack
|
---|
508 | * permanently */
|
---|
509 | static NOINLINE int create_socket(void)
|
---|
510 | {
|
---|
511 | struct sockaddr_un sunx;
|
---|
512 | int sock_fd;
|
---|
513 | char *dev_log_name;
|
---|
514 |
|
---|
515 | memset(&sunx, 0, sizeof(sunx));
|
---|
516 | sunx.sun_family = AF_UNIX;
|
---|
517 |
|
---|
518 | /* Unlink old /dev/log or object it points to. */
|
---|
519 | /* (if it exists, bind will fail) */
|
---|
520 | strcpy(sunx.sun_path, "/dev/log");
|
---|
521 | dev_log_name = xmalloc_follow_symlinks("/dev/log");
|
---|
522 | if (dev_log_name) {
|
---|
523 | safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
|
---|
524 | free(dev_log_name);
|
---|
525 | }
|
---|
526 | unlink(sunx.sun_path);
|
---|
527 |
|
---|
528 | sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
|
---|
529 | xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
|
---|
530 | chmod("/dev/log", 0666);
|
---|
531 |
|
---|
532 | return sock_fd;
|
---|
533 | }
|
---|
534 |
|
---|
535 | #if ENABLE_FEATURE_REMOTE_LOG
|
---|
536 | static int try_to_resolve_remote(remoteHost_t *rh)
|
---|
537 | {
|
---|
538 | if (!rh->remoteAddr) {
|
---|
539 | unsigned now = monotonic_sec();
|
---|
540 |
|
---|
541 | /* Don't resolve name too often - DNS timeouts can be big */
|
---|
542 | if ((now - rh->last_dns_resolve) < DNS_WAIT_SEC)
|
---|
543 | return -1;
|
---|
544 | rh->last_dns_resolve = now;
|
---|
545 | rh->remoteAddr = host2sockaddr(rh->remoteHostname, 514);
|
---|
546 | if (!rh->remoteAddr)
|
---|
547 | return -1;
|
---|
548 | }
|
---|
549 | return xsocket(rh->remoteAddr->u.sa.sa_family, SOCK_DGRAM, 0);
|
---|
550 | }
|
---|
551 | #endif
|
---|
552 |
|
---|
553 | static void do_syslogd(void) NORETURN;
|
---|
554 | static void do_syslogd(void)
|
---|
555 | {
|
---|
556 | int sock_fd;
|
---|
557 | #if ENABLE_FEATURE_REMOTE_LOG
|
---|
558 | llist_t *item;
|
---|
559 | #endif
|
---|
560 | #if ENABLE_FEATURE_SYSLOGD_DUP
|
---|
561 | int last_sz = -1;
|
---|
562 | char *last_buf;
|
---|
563 | char *recvbuf = G.recvbuf;
|
---|
564 | #else
|
---|
565 | #define recvbuf (G.recvbuf)
|
---|
566 | #endif
|
---|
567 |
|
---|
568 | /* Set up signal handlers (so that they interrupt read()) */
|
---|
569 | signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
|
---|
570 | signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
|
---|
571 | //signal_no_SA_RESTART_empty_mask(SIGQUIT, record_signo);
|
---|
572 | signal(SIGHUP, SIG_IGN);
|
---|
573 | #ifdef SYSLOGD_MARK
|
---|
574 | signal(SIGALRM, do_mark);
|
---|
575 | alarm(G.markInterval);
|
---|
576 | #endif
|
---|
577 | sock_fd = create_socket();
|
---|
578 |
|
---|
579 | if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
|
---|
580 | ipcsyslog_init();
|
---|
581 | }
|
---|
582 |
|
---|
583 | timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER);
|
---|
584 |
|
---|
585 | while (!bb_got_signal) {
|
---|
586 | ssize_t sz;
|
---|
587 |
|
---|
588 | #if ENABLE_FEATURE_SYSLOGD_DUP
|
---|
589 | last_buf = recvbuf;
|
---|
590 | if (recvbuf == G.recvbuf)
|
---|
591 | recvbuf = G.recvbuf + MAX_READ;
|
---|
592 | else
|
---|
593 | recvbuf = G.recvbuf;
|
---|
594 | #endif
|
---|
595 | read_again:
|
---|
596 | sz = read(sock_fd, recvbuf, MAX_READ - 1);
|
---|
597 | if (sz < 0) {
|
---|
598 | if (!bb_got_signal)
|
---|
599 | bb_perror_msg("read from /dev/log");
|
---|
600 | break;
|
---|
601 | }
|
---|
602 |
|
---|
603 | /* Drop trailing '\n' and NULs (typically there is one NUL) */
|
---|
604 | while (1) {
|
---|
605 | if (sz == 0)
|
---|
606 | goto read_again;
|
---|
607 | /* man 3 syslog says: "A trailing newline is added when needed".
|
---|
608 | * However, neither glibc nor uclibc do this:
|
---|
609 | * syslog(prio, "test") sends "test\0" to /dev/log,
|
---|
610 | * syslog(prio, "test\n") sends "test\n\0".
|
---|
611 | * IOW: newline is passed verbatim!
|
---|
612 | * I take it to mean that it's syslogd's job
|
---|
613 | * to make those look identical in the log files. */
|
---|
614 | if (recvbuf[sz-1] != '\0' && recvbuf[sz-1] != '\n')
|
---|
615 | break;
|
---|
616 | sz--;
|
---|
617 | }
|
---|
618 | #if ENABLE_FEATURE_SYSLOGD_DUP
|
---|
619 | if ((option_mask32 & OPT_dup) && (sz == last_sz))
|
---|
620 | if (memcmp(last_buf, recvbuf, sz) == 0)
|
---|
621 | continue;
|
---|
622 | last_sz = sz;
|
---|
623 | #endif
|
---|
624 | #if ENABLE_FEATURE_REMOTE_LOG
|
---|
625 | /* Stock syslogd sends it '\n'-terminated
|
---|
626 | * over network, mimic that */
|
---|
627 | recvbuf[sz] = '\n';
|
---|
628 |
|
---|
629 | /* We are not modifying log messages in any way before send */
|
---|
630 | /* Remote site cannot trust _us_ anyway and need to do validation again */
|
---|
631 | for (item = G.remoteHosts; item != NULL; item = item->link) {
|
---|
632 | remoteHost_t *rh = (remoteHost_t *)item->data;
|
---|
633 |
|
---|
634 | if (rh->remoteFD == -1) {
|
---|
635 | rh->remoteFD = try_to_resolve_remote(rh);
|
---|
636 | if (rh->remoteFD == -1)
|
---|
637 | continue;
|
---|
638 | }
|
---|
639 |
|
---|
640 | /* Send message to remote logger.
|
---|
641 | * On some errors, close and set remoteFD to -1
|
---|
642 | * so that DNS resolution is retried.
|
---|
643 | */
|
---|
644 | if (sendto(rh->remoteFD, recvbuf, sz+1,
|
---|
645 | MSG_DONTWAIT | MSG_NOSIGNAL,
|
---|
646 | &(rh->remoteAddr->u.sa), rh->remoteAddr->len) == -1
|
---|
647 | ) {
|
---|
648 | switch (errno) {
|
---|
649 | case ECONNRESET:
|
---|
650 | case ENOTCONN: /* paranoia */
|
---|
651 | case EPIPE:
|
---|
652 | close(rh->remoteFD);
|
---|
653 | rh->remoteFD = -1;
|
---|
654 | free(rh->remoteAddr);
|
---|
655 | rh->remoteAddr = NULL;
|
---|
656 | }
|
---|
657 | }
|
---|
658 | }
|
---|
659 | #endif
|
---|
660 | if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
|
---|
661 | recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */
|
---|
662 | split_escape_and_log(recvbuf, sz);
|
---|
663 | }
|
---|
664 | } /* while (!bb_got_signal) */
|
---|
665 |
|
---|
666 | timestamp_and_log_internal("syslogd exiting");
|
---|
667 | puts("syslogd exiting");
|
---|
668 | if (ENABLE_FEATURE_IPC_SYSLOG)
|
---|
669 | ipcsyslog_cleanup();
|
---|
670 | kill_myself_with_sig(bb_got_signal);
|
---|
671 | #undef recvbuf
|
---|
672 | }
|
---|
673 |
|
---|
674 | int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
675 | int syslogd_main(int argc UNUSED_PARAM, char **argv)
|
---|
676 | {
|
---|
677 | int opts;
|
---|
678 | char OPTION_DECL;
|
---|
679 | #if ENABLE_FEATURE_REMOTE_LOG
|
---|
680 | llist_t *remoteAddrList = NULL;
|
---|
681 | #endif
|
---|
682 |
|
---|
683 | INIT_G();
|
---|
684 |
|
---|
685 | /* No non-option params, -R can occur multiple times */
|
---|
686 | opt_complementary = "=0" IF_FEATURE_REMOTE_LOG(":R::");
|
---|
687 | opts = getopt32(argv, OPTION_STR, OPTION_PARAM);
|
---|
688 | #if ENABLE_FEATURE_REMOTE_LOG
|
---|
689 | while (remoteAddrList) {
|
---|
690 | remoteHost_t *rh = xzalloc(sizeof(*rh));
|
---|
691 | rh->remoteHostname = llist_pop(&remoteAddrList);
|
---|
692 | rh->remoteFD = -1;
|
---|
693 | rh->last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
|
---|
694 | llist_add_to(&G.remoteHosts, rh);
|
---|
695 | }
|
---|
696 | #endif
|
---|
697 |
|
---|
698 | #ifdef SYSLOGD_MARK
|
---|
699 | if (opts & OPT_mark) // -m
|
---|
700 | G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
|
---|
701 | #endif
|
---|
702 | //if (opts & OPT_nofork) // -n
|
---|
703 | //if (opts & OPT_outfile) // -O
|
---|
704 | if (opts & OPT_loglevel) // -l
|
---|
705 | G.logLevel = xatou_range(opt_l, 1, 8);
|
---|
706 | //if (opts & OPT_small) // -S
|
---|
707 | #if ENABLE_FEATURE_ROTATE_LOGFILE
|
---|
708 | if (opts & OPT_filesize) // -s
|
---|
709 | G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
|
---|
710 | if (opts & OPT_rotatecnt) // -b
|
---|
711 | G.logFileRotate = xatou_range(opt_b, 0, 99);
|
---|
712 | #endif
|
---|
713 | #if ENABLE_FEATURE_IPC_SYSLOG
|
---|
714 | if (opt_C) // -Cn
|
---|
715 | G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
|
---|
716 | #endif
|
---|
717 |
|
---|
718 | /* If they have not specified remote logging, then log locally */
|
---|
719 | if (ENABLE_FEATURE_REMOTE_LOG && !(opts & OPT_remotelog)) // -R
|
---|
720 | option_mask32 |= OPT_locallog;
|
---|
721 |
|
---|
722 | /* Store away localhost's name before the fork */
|
---|
723 | G.hostname = safe_gethostname();
|
---|
724 | *strchrnul(G.hostname, '.') = '\0';
|
---|
725 |
|
---|
726 | if (!(opts & OPT_nofork)) {
|
---|
727 | bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
|
---|
728 | }
|
---|
729 | //umask(0); - why??
|
---|
730 | write_pidfile("/var/run/syslogd.pid");
|
---|
731 | do_syslogd();
|
---|
732 | /* return EXIT_SUCCESS; */
|
---|
733 | }
|
---|
734 |
|
---|
735 | /* Clean up. Needed because we are included from syslogd_and_logger.c */
|
---|
736 | #undef DEBUG
|
---|
737 | #undef SYSLOGD_MARK
|
---|
738 | #undef SYSLOGD_WRLOCK
|
---|
739 | #undef G
|
---|
740 | #undef GLOBALS
|
---|
741 | #undef INIT_G
|
---|
742 | #undef OPTION_STR
|
---|
743 | #undef OPTION_DECL
|
---|
744 | #undef OPTION_PARAM
|
---|