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