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