summaryrefslogtreecommitdiff
path: root/sysklogd/syslogd.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-12-09 22:53:31 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-12-09 22:53:31 +0000
commit0d94820adf87b752c338c194a7291dcf1b96fc76 (patch)
tree59d71afa998d643835a2b8eba6fd72ddf78cc46a /sysklogd/syslogd.c
parent1bcdcd2ef0c1fda6b197ead493f9413f0d2ecfc2 (diff)
downloadbusybox-w32-0d94820adf87b752c338c194a7291dcf1b96fc76.tar.gz
busybox-w32-0d94820adf87b752c338c194a7291dcf1b96fc76.tar.bz2
busybox-w32-0d94820adf87b752c338c194a7291dcf1b96fc76.zip
syslogd: comment out file locking;
make signal handling syncronous (old was racy) function old new delta syslogd_main 963 1090 +127 quit_signal 96 - -96 log_locally 743 595 -148 ------------------------------------------------------------------------------ (add/remove: 0/1 grow/shrink: 1/1 up/down: 127/-244) Total: -117 bytes
Diffstat (limited to 'sysklogd/syslogd.c')
-rw-r--r--sysklogd/syslogd.c79
1 files changed, 46 insertions, 33 deletions
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 38ea3d7ff..48b875ef1 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -43,6 +43,9 @@
43 * (semaphores are down but do_mark routine tries to down them again) */ 43 * (semaphores are down but do_mark routine tries to down them again) */
44#undef SYSLOGD_MARK 44#undef SYSLOGD_MARK
45 45
46/* Write locking does not seem to be useful either */
47#undef SYSLOGD_WRLOCK
48
46enum { 49enum {
47 MAX_READ = 256, 50 MAX_READ = 256,
48 DNS_WAIT_SEC = 2 * 60, 51 DNS_WAIT_SEC = 2 * 60,
@@ -152,7 +155,7 @@ enum {
152 OPTBIT_small, // -S 155 OPTBIT_small, // -S
153 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s 156 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s
154 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b 157 USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b
155 USE_FEATURE_REMOTE_LOG( OPTBIT_remote ,) // -R 158 USE_FEATURE_REMOTE_LOG( OPTBIT_remotelog ,) // -R
156 USE_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L 159 USE_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L
157 USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C 160 USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C
158 USE_FEATURE_SYSLOGD_DUP( OPTBIT_dup ,) // -D 161 USE_FEATURE_SYSLOGD_DUP( OPTBIT_dup ,) // -D
@@ -164,7 +167,7 @@ enum {
164 OPT_small = 1 << OPTBIT_small , 167 OPT_small = 1 << OPTBIT_small ,
165 OPT_filesize = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0, 168 OPT_filesize = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0,
166 OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0, 169 OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0,
167 OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remote )) + 0, 170 OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remotelog )) + 0,
168 OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0, 171 OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0,
169 OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0, 172 OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0,
170 OPT_dup = USE_FEATURE_SYSLOGD_DUP( (1 << OPTBIT_dup )) + 0, 173 OPT_dup = USE_FEATURE_SYSLOGD_DUP( (1 << OPTBIT_dup )) + 0,
@@ -291,7 +294,9 @@ void log_to_shmem(const char *msg);
291/* Print a message to the log file. */ 294/* Print a message to the log file. */
292static void log_locally(time_t now, char *msg) 295static void log_locally(time_t now, char *msg)
293{ 296{
297#ifdef SYSLOGD_WRLOCK
294 struct flock fl; 298 struct flock fl;
299#endif
295 int len = strlen(msg); 300 int len = strlen(msg);
296 301
297#if ENABLE_FEATURE_IPC_SYSLOG 302#if ENABLE_FEATURE_IPC_SYSLOG
@@ -332,11 +337,13 @@ static void log_locally(time_t now, char *msg)
332#endif 337#endif
333 } 338 }
334 339
340#ifdef SYSLOGD_WRLOCK
335 fl.l_whence = SEEK_SET; 341 fl.l_whence = SEEK_SET;
336 fl.l_start = 0; 342 fl.l_start = 0;
337 fl.l_len = 1; 343 fl.l_len = 1;
338 fl.l_type = F_WRLCK; 344 fl.l_type = F_WRLCK;
339 fcntl(G.logFD, F_SETLKW, &fl); 345 fcntl(G.logFD, F_SETLKW, &fl);
346#endif
340 347
341#if ENABLE_FEATURE_ROTATE_LOGFILE 348#if ENABLE_FEATURE_ROTATE_LOGFILE
342 if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) { 349 if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) {
@@ -355,8 +362,10 @@ static void log_locally(time_t now, char *msg)
355 } 362 }
356 /* newFile == "f.0" now */ 363 /* newFile == "f.0" now */
357 rename(G.logFilePath, newFile); 364 rename(G.logFilePath, newFile);
365#ifdef SYSLOGD_WRLOCK
358 fl.l_type = F_UNLCK; 366 fl.l_type = F_UNLCK;
359 fcntl(G.logFD, F_SETLKW, &fl); 367 fcntl(G.logFD, F_SETLKW, &fl);
368#endif
360 close(G.logFD); 369 close(G.logFD);
361 goto reopen; 370 goto reopen;
362 } 371 }
@@ -365,8 +374,10 @@ static void log_locally(time_t now, char *msg)
365 G.curFileSize += 374 G.curFileSize +=
366#endif 375#endif
367 full_write(G.logFD, msg, len); 376 full_write(G.logFD, msg, len);
377#ifdef SYSLOGD_WRLOCK
368 fl.l_type = F_UNLCK; 378 fl.l_type = F_UNLCK;
369 fcntl(G.logFD, F_SETLKW, &fl); 379 fcntl(G.logFD, F_SETLKW, &fl);
380#endif
370} 381}
371 382
372static void parse_fac_prio_20(int pri, char *res20) 383static void parse_fac_prio_20(int pri, char *res20)
@@ -432,6 +443,7 @@ static void timestamp_and_log(int pri, char *msg, int len)
432 443
433static void timestamp_and_log_internal(const char *msg) 444static void timestamp_and_log_internal(const char *msg)
434{ 445{
446 /* -L, or no -R */
435 if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog)) 447 if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog))
436 return; 448 return;
437 timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0); 449 timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0);
@@ -476,15 +488,6 @@ static void split_escape_and_log(char *tmpbuf, int len)
476 } 488 }
477} 489}
478 490
479static void quit_signal(int sig)
480{
481 timestamp_and_log_internal("syslogd exiting");
482 puts("syslogd exiting");
483 if (ENABLE_FEATURE_IPC_SYSLOG)
484 ipcsyslog_cleanup();
485 kill_myself_with_sig(sig);
486}
487
488#ifdef SYSLOGD_MARK 491#ifdef SYSLOGD_MARK
489static void do_mark(int sig) 492static void do_mark(int sig)
490{ 493{
@@ -553,14 +556,11 @@ static void do_syslogd(void)
553#define recvbuf (G.recvbuf) 556#define recvbuf (G.recvbuf)
554#endif 557#endif
555 558
556 /* Set up signal handlers */ 559 /* Set up signal handlers (so that they interrupt read()) */
557 bb_signals(0 560 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
558 + (1 << SIGINT) 561 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
559 + (1 << SIGTERM) 562 //signal_no_SA_RESTART_empty_mask(SIGQUIT, record_signo);
560 + (1 << SIGQUIT)
561 , quit_signal);
562 signal(SIGHUP, SIG_IGN); 563 signal(SIGHUP, SIG_IGN);
563 /* signal(SIGCHLD, SIG_IGN); - why? */
564#ifdef SYSLOGD_MARK 564#ifdef SYSLOGD_MARK
565 signal(SIGALRM, do_mark); 565 signal(SIGALRM, do_mark);
566 alarm(G.markInterval); 566 alarm(G.markInterval);
@@ -573,7 +573,7 @@ static void do_syslogd(void)
573 573
574 timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER); 574 timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER);
575 575
576 for (;;) { 576 while (!bb_got_signal) {
577 ssize_t sz; 577 ssize_t sz;
578 578
579#if ENABLE_FEATURE_SYSLOGD_DUP 579#if ENABLE_FEATURE_SYSLOGD_DUP
@@ -584,9 +584,12 @@ static void do_syslogd(void)
584 recvbuf = G.recvbuf; 584 recvbuf = G.recvbuf;
585#endif 585#endif
586 read_again: 586 read_again:
587 sz = safe_read(sock_fd, recvbuf, MAX_READ - 1); 587 sz = read(sock_fd, recvbuf, MAX_READ - 1);
588 if (sz < 0) 588 if (sz < 0) {
589 bb_perror_msg_and_die("read from /dev/log"); 589 if (!bb_got_signal)
590 bb_perror_msg("read from /dev/log");
591 break;
592 }
590 593
591 /* Drop trailing '\n' and NULs (typically there is one NUL) */ 594 /* Drop trailing '\n' and NULs (typically there is one NUL) */
592 while (1) { 595 while (1) {
@@ -633,7 +636,13 @@ static void do_syslogd(void)
633 recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */ 636 recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */
634 split_escape_and_log(recvbuf, sz); 637 split_escape_and_log(recvbuf, sz);
635 } 638 }
636 } /* for (;;) */ 639 } /* while (!bb_got_signal) */
640
641 timestamp_and_log_internal("syslogd exiting");
642 puts("syslogd exiting");
643 if (ENABLE_FEATURE_IPC_SYSLOG)
644 ipcsyslog_cleanup();
645 kill_myself_with_sig(bb_got_signal);
637#undef recvbuf 646#undef recvbuf
638} 647}
639 648
@@ -641,6 +650,7 @@ int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
641int syslogd_main(int argc UNUSED_PARAM, char **argv) 650int syslogd_main(int argc UNUSED_PARAM, char **argv)
642{ 651{
643 char OPTION_DECL; 652 char OPTION_DECL;
653 int opts;
644 654
645 INIT_G(); 655 INIT_G();
646#if ENABLE_FEATURE_REMOTE_LOG 656#if ENABLE_FEATURE_REMOTE_LOG
@@ -649,20 +659,20 @@ int syslogd_main(int argc UNUSED_PARAM, char **argv)
649 659
650 /* do normal option parsing */ 660 /* do normal option parsing */
651 opt_complementary = "=0"; /* no non-option params */ 661 opt_complementary = "=0"; /* no non-option params */
652 getopt32(argv, OPTION_STR, OPTION_PARAM); 662 opts = getopt32(argv, OPTION_STR, OPTION_PARAM);
653#ifdef SYSLOGD_MARK 663#ifdef SYSLOGD_MARK
654 if (option_mask32 & OPT_mark) // -m 664 if (opts & OPT_mark) // -m
655 G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60; 665 G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
656#endif 666#endif
657 //if (option_mask32 & OPT_nofork) // -n 667 //if (opts & OPT_nofork) // -n
658 //if (option_mask32 & OPT_outfile) // -O 668 //if (opts & OPT_outfile) // -O
659 if (option_mask32 & OPT_loglevel) // -l 669 if (opts & OPT_loglevel) // -l
660 G.logLevel = xatou_range(opt_l, 1, 8); 670 G.logLevel = xatou_range(opt_l, 1, 8);
661 //if (option_mask32 & OPT_small) // -S 671 //if (opts & OPT_small) // -S
662#if ENABLE_FEATURE_ROTATE_LOGFILE 672#if ENABLE_FEATURE_ROTATE_LOGFILE
663 if (option_mask32 & OPT_filesize) // -s 673 if (opts & OPT_filesize) // -s
664 G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024; 674 G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
665 if (option_mask32 & OPT_rotatecnt) // -b 675 if (opts & OPT_rotatecnt) // -b
666 G.logFileRotate = xatou_range(opt_b, 0, 99); 676 G.logFileRotate = xatou_range(opt_b, 0, 99);
667#endif 677#endif
668#if ENABLE_FEATURE_IPC_SYSLOG 678#if ENABLE_FEATURE_IPC_SYSLOG
@@ -671,14 +681,14 @@ int syslogd_main(int argc UNUSED_PARAM, char **argv)
671#endif 681#endif
672 682
673 /* If they have not specified remote logging, then log locally */ 683 /* If they have not specified remote logging, then log locally */
674 if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_remotelog)) 684 if (ENABLE_FEATURE_REMOTE_LOG && !(opts & OPT_remotelog)) // -R
675 option_mask32 |= OPT_locallog; 685 option_mask32 |= OPT_locallog;
676 686
677 /* Store away localhost's name before the fork */ 687 /* Store away localhost's name before the fork */
678 G.hostname = safe_gethostname(); 688 G.hostname = safe_gethostname();
679 *strchrnul(G.hostname, '.') = '\0'; 689 *strchrnul(G.hostname, '.') = '\0';
680 690
681 if (!(option_mask32 & OPT_nofork)) { 691 if (!(opts & OPT_nofork)) {
682 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); 692 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
683 } 693 }
684 umask(0); 694 umask(0);
@@ -688,6 +698,9 @@ int syslogd_main(int argc UNUSED_PARAM, char **argv)
688} 698}
689 699
690/* Clean up. Needed because we are included from syslogd_and_logger.c */ 700/* Clean up. Needed because we are included from syslogd_and_logger.c */
701#undef DEBUG
702#undef SYSLOGD_MARK
703#undef SYSLOGD_WRLOCK
691#undef G 704#undef G
692#undef GLOBALS 705#undef GLOBALS
693#undef INIT_G 706#undef INIT_G