1 /* vi: set sw=4 ts=4: */
3 * Mini syslogd implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
9 * "circular buffer" Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>
11 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
13 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
17 * Done in syslogd_and_logger.c:
20 #define SYSLOG_NAMES_CONST
27 #if ENABLE_FEATURE_REMOTE_LOG
28 #include <netinet/in.h>
31 #if ENABLE_FEATURE_IPC_SYSLOG
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) */
45 /* Write locking does not seem to be useful either */
49 MAX_READ = CONFIG_FEATURE_SYSLOGD_READ_BUFFER_SIZE,
50 DNS_WAIT_SEC = 2 * 60,
53 /* Semaphore operation structures */
55 int32_t size; /* size of data - 1 */
56 int32_t tail; /* end of message list */
57 char data[1]; /* data/messages */
60 #if ENABLE_FEATURE_REMOTE_LOG
63 unsigned last_dns_resolve;
64 len_and_sockaddr *remoteAddr;
65 const char *remoteHostname;
69 typedef struct logFile_t {
72 #if ENABLE_FEATURE_ROTATE_LOGFILE
78 #if ENABLE_FEATURE_SYSLOGD_CFG
79 typedef struct logRule_t {
80 uint8_t enabled_facility_priomap[LOG_NFACILITIES];
81 struct logFile_t *file;
82 struct logRule_t *next;
86 /* Allows us to have smaller initializer. Ugly. */
89 /* interval between marks in seconds */ \
90 /*int markInterval;*/ \
91 /* level of messages to be logged */ \
93 IF_FEATURE_ROTATE_LOGFILE( \
94 /* max size of file before rotation */ \
95 unsigned logFileSize; \
96 /* number of rotated message files */ \
97 unsigned logFileRotate; \
99 IF_FEATURE_IPC_SYSLOG( \
100 int shmid; /* ipc shared memory id */ \
101 int s_semid; /* ipc semaphore id */ \
103 struct sembuf SMwup[1]; \
104 struct sembuf SMwdn[3]; \
106 IF_FEATURE_SYSLOGD_CFG( \
107 logRule_t *log_rules; \
110 struct init_globals {
117 #if ENABLE_FEATURE_REMOTE_LOG
118 llist_t *remoteHosts;
120 #if ENABLE_FEATURE_IPC_SYSLOG
121 struct shbuf_ds *shbuf;
123 time_t last_log_time;
124 /* localhost's name. We print only first 64 chars */
127 /* We recv into recvbuf... */
128 char recvbuf[MAX_READ * (1 + ENABLE_FEATURE_SYSLOGD_DUP)];
129 /* ...then copy to parsebuf, escaping control chars */
130 /* (can grow x2 max) */
131 char parsebuf[MAX_READ*2];
132 /* ...then sprintf into printbuf, adding timestamp (15 chars),
133 * host (64), fac.prio (20) to the message */
134 /* (growth by: 15 + 64 + 20 + delims = ~110) */
135 char printbuf[MAX_READ*2 + 128];
138 static const struct init_globals init_data = {
140 .path = "/var/log/messages",
144 .markInterval = 20 * 60,
147 #if ENABLE_FEATURE_ROTATE_LOGFILE
148 .logFileSize = 200 * 1024,
151 #if ENABLE_FEATURE_IPC_SYSLOG
154 .shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024), /* default shm size */
155 .SMwup = { {1, -1, IPC_NOWAIT} },
156 .SMwdn = { {0, 0}, {1, 0}, {1, +1} },
160 #define G (*ptr_to_globals)
161 #define INIT_G() do { \
162 SET_PTR_TO_GLOBALS(memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data))); \
168 OPTBIT_mark = 0, // -m
170 OPTBIT_outfile, // -O
171 OPTBIT_loglevel, // -l
173 IF_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
174 IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
175 IF_FEATURE_REMOTE_LOG( OPTBIT_remotelog ,) // -R
176 IF_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L
177 IF_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
178 IF_FEATURE_SYSLOGD_DUP( OPTBIT_dup ,) // -D
179 IF_FEATURE_SYSLOGD_CFG( OPTBIT_cfg ,) // -f
181 OPT_mark = 1 << OPTBIT_mark ,
182 OPT_nofork = 1 << OPTBIT_nofork ,
183 OPT_outfile = 1 << OPTBIT_outfile ,
184 OPT_loglevel = 1 << OPTBIT_loglevel,
185 OPT_small = 1 << OPTBIT_small ,
186 OPT_filesize = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
187 OPT_rotatecnt = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
188 OPT_remotelog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_remotelog )) + 0,
189 OPT_locallog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0,
190 OPT_circularlog = IF_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
191 OPT_dup = IF_FEATURE_SYSLOGD_DUP( (1 << OPTBIT_dup )) + 0,
192 OPT_cfg = IF_FEATURE_SYSLOGD_CFG( (1 << OPTBIT_cfg )) + 0,
194 #define OPTION_STR "m:nO:l:S" \
195 IF_FEATURE_ROTATE_LOGFILE("s:" ) \
196 IF_FEATURE_ROTATE_LOGFILE("b:" ) \
197 IF_FEATURE_REMOTE_LOG( "R:" ) \
198 IF_FEATURE_REMOTE_LOG( "L" ) \
199 IF_FEATURE_IPC_SYSLOG( "C::") \
200 IF_FEATURE_SYSLOGD_DUP( "D" ) \
201 IF_FEATURE_SYSLOGD_CFG( "f:" )
202 #define OPTION_DECL *opt_m, *opt_l \
203 IF_FEATURE_ROTATE_LOGFILE(,*opt_s) \
204 IF_FEATURE_ROTATE_LOGFILE(,*opt_b) \
205 IF_FEATURE_IPC_SYSLOG( ,*opt_C = NULL) \
206 IF_FEATURE_SYSLOGD_CFG( ,*opt_f = NULL)
207 #define OPTION_PARAM &opt_m, &(G.logFile.path), &opt_l \
208 IF_FEATURE_ROTATE_LOGFILE(,&opt_s) \
209 IF_FEATURE_ROTATE_LOGFILE(,&opt_b) \
210 IF_FEATURE_REMOTE_LOG( ,&remoteAddrList) \
211 IF_FEATURE_IPC_SYSLOG( ,&opt_C) \
212 IF_FEATURE_SYSLOGD_CFG( ,&opt_f)
215 #if ENABLE_FEATURE_SYSLOGD_CFG
216 static const CODE* find_by_name(char *name, const CODE* c_set)
218 for (; c_set->c_name; c_set++) {
219 if (strcmp(name, c_set->c_name) == 0)
225 static const CODE* find_by_val(int val, const CODE* c_set)
227 for (; c_set->c_name; c_set++) {
228 if (c_set->c_val == val)
234 #if ENABLE_FEATURE_SYSLOGD_CFG
235 static void parse_syslogdcfg(const char *file)
239 /* tok[0] set of selectors */
240 /* tok[1] file name */
241 /* tok[2] has to be NULL */
245 parser = config_open2(file ? file : "/etc/syslog.conf",
246 file ? xfopen_for_read : fopen_or_warn_stdin);
248 /* didn't find default /etc/syslog.conf */
249 /* proceed as if we built busybox without config support */
252 /* use ptr to ptr to avoid checking whether head was initialized */
253 pp_rule = &G.log_rules;
254 /* iterate through lines of config, skipping comments */
255 while (config_read(parser, tok, 3, 2, "# \t", PARSE_NORMAL | PARSE_MIN_DIE)) {
259 /* unexpected trailing token? */
265 cur_rule = *pp_rule = xzalloc(sizeof(*cur_rule));
267 cur_selector = tok[0];
268 /* iterate through selectors: "kern.info;kern.!err;..." */
272 uint8_t negated_prio; /* "kern.!err" */
273 uint8_t single_prio; /* "kern.=err" */
274 uint32_t facmap; /* bitmap of enabled facilities */
275 uint8_t primap; /* bitmap of enabled priorities */
278 next_selector = strchr(cur_selector, ';');
280 *next_selector++ = '\0';
282 t = strchr(cur_selector, '.');
287 *t++ = '\0'; /* separate facility from priority */
302 primap = 0xff; /* all 8 log levels enabled */
305 code = find_by_name(t, prioritynames);
309 priority = code->c_val;
310 if (priority == INTERNAL_NOPRI) {
311 /* ensure we take "enabled_facility_priomap[fac] &= 0" branch below */
314 priority = 1 << priority;
327 if (*cur_selector == '*')
328 facmap = (1<<LOG_NFACILITIES) - 1;
333 /* iterate through facilities: "kern,daemon.<priospec>" */
335 next_facility = strchr(t, ',');
337 *next_facility++ = '\0';
338 code = find_by_name(t, facilitynames);
341 /* "mark" is not a real facility, skip it */
342 if (code->c_val != INTERNAL_MARK)
343 facmap |= 1<<(LOG_FAC(code->c_val));
348 /* merge result with previous selectors */
349 for (i = 0; i < LOG_NFACILITIES; ++i) {
350 if (!(facmap & (1<<i)))
353 cur_rule->enabled_facility_priomap[i] &= primap;
355 cur_rule->enabled_facility_priomap[i] |= primap;
358 cur_selector = next_selector;
359 } while (cur_selector);
361 /* check whether current file name was mentioned in previous rules.
362 * temporarily use cur_rule as iterator, but *pp_rule still points to
363 * currently processing rule entry.
364 * NOTE: *pp_rule points to the current (and last in the list) rule.
366 for (cur_rule = G.log_rules; cur_rule != *pp_rule; cur_rule = cur_rule->next) {
367 if (strcmp(cur_rule->file->path, tok[1]) == 0) {
368 /* found - reuse the same file structure */
369 (*pp_rule)->file = cur_rule->file;
374 cur_rule->file = xzalloc(sizeof(*cur_rule->file));
375 cur_rule->file->fd = -1;
376 cur_rule->file->path = xstrdup(tok[1]);
378 pp_rule = &cur_rule->next;
380 config_close(parser);
384 bb_error_msg_and_die("bad line %d: wrong token '%s'", parser->lineno, t);
388 /* circular buffer variables/structures */
389 #if ENABLE_FEATURE_IPC_SYSLOG
391 #if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4
392 #error Sorry, you must set the syslogd buffer size to at least 4KB.
393 #error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE
396 /* our shared key (syslogd.c and logread.c must be in sync) */
397 enum { KEY_ID = 0x414e4547 }; /* "GENA" */
399 static void ipcsyslog_cleanup(void)
405 shmctl(G.shmid, IPC_RMID, NULL);
407 if (G.s_semid != -1) {
408 semctl(G.s_semid, 0, IPC_RMID, 0);
412 static void ipcsyslog_init(void)
415 printf("shmget(%x, %d,...)\n", (int)KEY_ID, G.shm_size);
417 G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 0644);
419 bb_perror_msg_and_die("shmget");
422 G.shbuf = shmat(G.shmid, NULL, 0);
423 if (G.shbuf == (void*) -1L) { /* shmat has bizarre error return */
424 bb_perror_msg_and_die("shmat");
427 memset(G.shbuf, 0, G.shm_size);
428 G.shbuf->size = G.shm_size - offsetof(struct shbuf_ds, data) - 1;
429 /*G.shbuf->tail = 0;*/
431 /* we'll trust the OS to set initial semval to 0 (let's hope) */
432 G.s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023);
433 if (G.s_semid == -1) {
434 if (errno == EEXIST) {
435 G.s_semid = semget(KEY_ID, 2, 0);
439 bb_perror_msg_and_die("semget");
443 /* Write message to shared mem buffer */
444 static void log_to_shmem(const char *msg)
446 int old_tail, new_tail;
449 if (semop(G.s_semid, G.SMwdn, 3) == -1) {
450 bb_perror_msg_and_die("SMwdn");
453 /* Circular Buffer Algorithm:
454 * --------------------------
455 * tail == position where to store next syslog message.
456 * tail's max value is (shbuf->size - 1)
457 * Last byte of buffer is never used and remains NUL.
459 len = strlen(msg) + 1; /* length with NUL included */
461 old_tail = G.shbuf->tail;
462 new_tail = old_tail + len;
463 if (new_tail < G.shbuf->size) {
464 /* store message, set new tail */
465 memcpy(G.shbuf->data + old_tail, msg, len);
466 G.shbuf->tail = new_tail;
468 /* k == available buffer space ahead of old tail */
469 int k = G.shbuf->size - old_tail;
470 /* copy what fits to the end of buffer, and repeat */
471 memcpy(G.shbuf->data + old_tail, msg, k);
477 if (semop(G.s_semid, G.SMwup, 1) == -1) {
478 bb_perror_msg_and_die("SMwup");
481 printf("tail:%d\n", G.shbuf->tail);
484 void ipcsyslog_cleanup(void);
485 void ipcsyslog_init(void);
486 void log_to_shmem(const char *msg);
487 #endif /* FEATURE_IPC_SYSLOG */
489 /* Print a message to the log file. */
490 static void log_locally(time_t now, char *msg, logFile_t *log_file)
492 #ifdef SYSLOGD_WRLOCK
495 int len = strlen(msg);
497 if (log_file->fd >= 0) {
498 /* Reopen log file every second. This allows admin
499 * to delete the file and not worry about restarting us.
500 * This costs almost nothing since it happens
501 * _at most_ once a second.
505 if (G.last_log_time != now) {
506 G.last_log_time = now;
512 log_file->fd = open(log_file->path, O_WRONLY | O_CREAT
513 | O_NOCTTY | O_APPEND | O_NONBLOCK,
515 if (log_file->fd < 0) {
516 /* cannot open logfile? - print to /dev/console then */
517 int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
519 fd = 2; /* then stderr, dammit */
520 full_write(fd, msg, len);
525 #if ENABLE_FEATURE_ROTATE_LOGFILE
528 log_file->isRegular = (fstat(log_file->fd, &statf) == 0 && S_ISREG(statf.st_mode));
529 /* bug (mostly harmless): can wrap around if file > 4gb */
530 log_file->size = statf.st_size;
535 #ifdef SYSLOGD_WRLOCK
536 fl.l_whence = SEEK_SET;
540 fcntl(log_file->fd, F_SETLKW, &fl);
543 #if ENABLE_FEATURE_ROTATE_LOGFILE
544 if (G.logFileSize && log_file->isRegular && log_file->size > G.logFileSize) {
545 if (G.logFileRotate) { /* always 0..99 */
546 int i = strlen(log_file->path) + 3 + 1;
549 i = G.logFileRotate - 1;
550 /* rename: f.8 -> f.9; f.7 -> f.8; ... */
552 sprintf(newFile, "%s.%d", log_file->path, i);
554 sprintf(oldFile, "%s.%d", log_file->path, --i);
555 /* ignore errors - file might be missing */
556 rename(oldFile, newFile);
558 /* newFile == "f.0" now */
559 rename(log_file->path, newFile);
560 #ifdef SYSLOGD_WRLOCK
562 fcntl(log_file->fd, F_SETLKW, &fl);
567 ftruncate(log_file->fd, 0);
571 full_write(log_file->fd, msg, len);
572 #ifdef SYSLOGD_WRLOCK
574 fcntl(log_file->fd, F_SETLKW, &fl);
578 static void parse_fac_prio_20(int pri, char *res20)
580 const CODE *c_pri, *c_fac;
582 c_fac = find_by_val(LOG_FAC(pri) << 3, facilitynames);
584 c_pri = find_by_val(LOG_PRI(pri), prioritynames);
586 snprintf(res20, 20, "%s.%s", c_fac->c_name, c_pri->c_name);
590 snprintf(res20, 20, "<%d>", pri);
593 /* len parameter is used only for "is there a timestamp?" check.
594 * NB: some callers cheat and supply len==0 when they know
595 * that there is no timestamp, short-circuiting the test. */
596 static void timestamp_and_log(int pri, char *msg, int len)
601 /* Jan 18 00:11:22 msg... */
602 /* 01234567890123456 */
603 if (len < 16 || msg[3] != ' ' || msg[6] != ' '
604 || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
607 timestamp = ctime(&now) + 4; /* skip day of week */
613 timestamp[15] = '\0';
615 if (option_mask32 & OPT_small)
616 sprintf(G.printbuf, "%s %s\n", timestamp, msg);
619 parse_fac_prio_20(pri, res);
620 sprintf(G.printbuf, "%s %.64s %s %s\n", timestamp, G.hostname, res, msg);
623 /* Log message locally (to file or shared mem) */
624 #if ENABLE_FEATURE_SYSLOGD_CFG
628 uint8_t facility = LOG_FAC(pri);
629 uint8_t prio_bit = 1 << LOG_PRI(pri);
631 for (rule = G.log_rules; rule; rule = rule->next) {
632 if (rule->enabled_facility_priomap[facility] & prio_bit) {
633 log_locally(now, G.printbuf, rule->file);
641 if (LOG_PRI(pri) < G.logLevel) {
642 #if ENABLE_FEATURE_IPC_SYSLOG
643 if ((option_mask32 & OPT_circularlog) && G.shbuf) {
648 log_locally(now, G.printbuf, &G.logFile);
652 static void timestamp_and_log_internal(const char *msg)
655 if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog))
657 timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0);
660 /* tmpbuf[len] is a NUL byte (set by caller), but there can be other,
661 * embedded NULs. Split messages on each of these NULs, parse prio,
662 * escape control chars and log each locally. */
663 static void split_escape_and_log(char *tmpbuf, int len)
670 char *q = G.parsebuf;
671 int pri = (LOG_USER | LOG_NOTICE);
674 /* Parse the magic priority number */
675 pri = bb_strtou(p + 1, &p, 10);
678 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
679 pri = (LOG_USER | LOG_NOTICE);
685 if (!(c & ~0x1f) && c != '\t') {
687 c += '@'; /* ^@, ^A, ^B... */
694 timestamp_and_log(pri, G.parsebuf, q - G.parsebuf);
699 static void do_mark(int sig)
701 if (G.markInterval) {
702 timestamp_and_log_internal("-- MARK --");
703 alarm(G.markInterval);
708 /* Don't inline: prevent struct sockaddr_un to take up space on stack
710 static NOINLINE int create_socket(void)
712 struct sockaddr_un sunx;
716 #if ENABLE_FEATURE_SYSTEMD
717 if (sd_listen_fds() == 1)
718 return SD_LISTEN_FDS_START;
721 memset(&sunx, 0, sizeof(sunx));
722 sunx.sun_family = AF_UNIX;
724 /* Unlink old /dev/log or object it points to. */
725 /* (if it exists, bind will fail) */
726 strcpy(sunx.sun_path, "/dev/log");
727 dev_log_name = xmalloc_follow_symlinks("/dev/log");
729 safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
732 unlink(sunx.sun_path);
734 sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
735 xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
736 chmod("/dev/log", 0666);
741 #if ENABLE_FEATURE_REMOTE_LOG
742 static int try_to_resolve_remote(remoteHost_t *rh)
744 if (!rh->remoteAddr) {
745 unsigned now = monotonic_sec();
747 /* Don't resolve name too often - DNS timeouts can be big */
748 if ((now - rh->last_dns_resolve) < DNS_WAIT_SEC)
750 rh->last_dns_resolve = now;
751 rh->remoteAddr = host2sockaddr(rh->remoteHostname, 514);
755 return xsocket(rh->remoteAddr->u.sa.sa_family, SOCK_DGRAM, 0);
759 static void do_syslogd(void) NORETURN;
760 static void do_syslogd(void)
763 #if ENABLE_FEATURE_REMOTE_LOG
766 #if ENABLE_FEATURE_SYSLOGD_DUP
769 char *recvbuf = G.recvbuf;
771 #define recvbuf (G.recvbuf)
774 /* Set up signal handlers (so that they interrupt read()) */
775 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
776 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
777 //signal_no_SA_RESTART_empty_mask(SIGQUIT, record_signo);
778 signal(SIGHUP, SIG_IGN);
780 signal(SIGALRM, do_mark);
781 alarm(G.markInterval);
783 sock_fd = create_socket();
785 if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
789 timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER);
791 while (!bb_got_signal) {
794 #if ENABLE_FEATURE_SYSLOGD_DUP
796 if (recvbuf == G.recvbuf)
797 recvbuf = G.recvbuf + MAX_READ;
802 sz = read(sock_fd, recvbuf, MAX_READ - 1);
805 bb_perror_msg("read from /dev/log");
809 /* Drop trailing '\n' and NULs (typically there is one NUL) */
813 /* man 3 syslog says: "A trailing newline is added when needed".
814 * However, neither glibc nor uclibc do this:
815 * syslog(prio, "test") sends "test\0" to /dev/log,
816 * syslog(prio, "test\n") sends "test\n\0".
817 * IOW: newline is passed verbatim!
818 * I take it to mean that it's syslogd's job
819 * to make those look identical in the log files. */
820 if (recvbuf[sz-1] != '\0' && recvbuf[sz-1] != '\n')
824 #if ENABLE_FEATURE_SYSLOGD_DUP
825 if ((option_mask32 & OPT_dup) && (sz == last_sz))
826 if (memcmp(last_buf, recvbuf, sz) == 0)
830 #if ENABLE_FEATURE_REMOTE_LOG
831 /* Stock syslogd sends it '\n'-terminated
832 * over network, mimic that */
835 /* We are not modifying log messages in any way before send */
836 /* Remote site cannot trust _us_ anyway and need to do validation again */
837 for (item = G.remoteHosts; item != NULL; item = item->link) {
838 remoteHost_t *rh = (remoteHost_t *)item->data;
840 if (rh->remoteFD == -1) {
841 rh->remoteFD = try_to_resolve_remote(rh);
842 if (rh->remoteFD == -1)
846 /* Send message to remote logger.
847 * On some errors, close and set remoteFD to -1
848 * so that DNS resolution is retried.
850 if (sendto(rh->remoteFD, recvbuf, sz+1,
851 MSG_DONTWAIT | MSG_NOSIGNAL,
852 &(rh->remoteAddr->u.sa), rh->remoteAddr->len) == -1
856 case ENOTCONN: /* paranoia */
860 free(rh->remoteAddr);
861 rh->remoteAddr = NULL;
866 if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
867 recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */
868 split_escape_and_log(recvbuf, sz);
870 } /* while (!bb_got_signal) */
872 timestamp_and_log_internal("syslogd exiting");
873 puts("syslogd exiting");
874 if (ENABLE_FEATURE_IPC_SYSLOG)
876 kill_myself_with_sig(bb_got_signal);
880 int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
881 int syslogd_main(int argc UNUSED_PARAM, char **argv)
885 #if ENABLE_FEATURE_REMOTE_LOG
886 llist_t *remoteAddrList = NULL;
891 /* No non-option params, -R can occur multiple times */
892 opt_complementary = "=0" IF_FEATURE_REMOTE_LOG(":R::");
893 opts = getopt32(argv, OPTION_STR, OPTION_PARAM);
894 #if ENABLE_FEATURE_REMOTE_LOG
895 while (remoteAddrList) {
896 remoteHost_t *rh = xzalloc(sizeof(*rh));
897 rh->remoteHostname = llist_pop(&remoteAddrList);
899 rh->last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
900 llist_add_to(&G.remoteHosts, rh);
905 if (opts & OPT_mark) // -m
906 G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
908 //if (opts & OPT_nofork) // -n
909 //if (opts & OPT_outfile) // -O
910 if (opts & OPT_loglevel) // -l
911 G.logLevel = xatou_range(opt_l, 1, 8);
912 //if (opts & OPT_small) // -S
913 #if ENABLE_FEATURE_ROTATE_LOGFILE
914 if (opts & OPT_filesize) // -s
915 G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
916 if (opts & OPT_rotatecnt) // -b
917 G.logFileRotate = xatou_range(opt_b, 0, 99);
919 #if ENABLE_FEATURE_IPC_SYSLOG
921 G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
923 /* If they have not specified remote logging, then log locally */
924 if (ENABLE_FEATURE_REMOTE_LOG && !(opts & OPT_remotelog)) // -R
925 option_mask32 |= OPT_locallog;
926 #if ENABLE_FEATURE_SYSLOGD_CFG
927 parse_syslogdcfg(opt_f);
930 /* Store away localhost's name before the fork */
931 G.hostname = safe_gethostname();
932 *strchrnul(G.hostname, '.') = '\0';
934 if (!(opts & OPT_nofork)) {
935 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
938 write_pidfile("/var/run/syslogd.pid");
940 /* return EXIT_SUCCESS; */
943 /* Clean up. Needed because we are included from syslogd_and_logger.c */
946 #undef SYSLOGD_WRLOCK