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