diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2014-07-02 15:21:30 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2014-07-02 15:21:30 +0200 |
commit | a28c1b21e1a3770b83fe9b15f26ec50f2e14e7f6 (patch) | |
tree | 22dfabdbfd2d4ce9762f0570ad9f1daba52bdcb8 | |
parent | 98654b995bb460733d94eba9ff2ee3d746c1e344 (diff) | |
download | busybox-w32-a28c1b21e1a3770b83fe9b15f26ec50f2e14e7f6.tar.gz busybox-w32-a28c1b21e1a3770b83fe9b15f26ec50f2e14e7f6.tar.bz2 busybox-w32-a28c1b21e1a3770b83fe9b15f26ec50f2e14e7f6.zip |
syslogd: make "-O -" log to stdout
function old new delta
packed_usage 29871 29908 +37
log_locally 404 440 +36
syslogd_main 1966 1956 -10
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 73/-10) Total: 63 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | sysklogd/syslogd.c | 81 |
1 files changed, 45 insertions, 36 deletions
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index f75851085..d80447bd7 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c | |||
@@ -21,31 +21,31 @@ | |||
21 | //usage: "(this version of syslogd ignores /etc/syslog.conf)\n" | 21 | //usage: "(this version of syslogd ignores /etc/syslog.conf)\n" |
22 | //usage: ) | 22 | //usage: ) |
23 | //usage: "\n -n Run in foreground" | 23 | //usage: "\n -n Run in foreground" |
24 | //usage: "\n -O FILE Log to FILE (default:/var/log/messages)" | ||
25 | //usage: "\n -l N Log only messages more urgent than prio N (1-8)" | ||
26 | //usage: "\n -S Smaller output" | ||
27 | //usage: IF_FEATURE_ROTATE_LOGFILE( | ||
28 | //usage: "\n -s SIZE Max size (KB) before rotation (default:200KB, 0=off)" | ||
29 | //usage: "\n -b N N rotated logs to keep (default:1, max=99, 0=purge)" | ||
30 | //usage: ) | ||
31 | //usage: IF_FEATURE_REMOTE_LOG( | 24 | //usage: IF_FEATURE_REMOTE_LOG( |
32 | //usage: "\n -R HOST[:PORT] Log to HOST:PORT (default PORT:514)" | 25 | //usage: "\n -R HOST[:PORT] Log to HOST:PORT (default PORT:514)" |
33 | //usage: "\n -L Log locally and via network (default is network only if -R)" | 26 | //usage: "\n -L Log locally and via network (default is network only if -R)" |
34 | //usage: ) | 27 | //usage: ) |
35 | //usage: IF_FEATURE_SYSLOGD_DUP( | ||
36 | //usage: "\n -D Drop duplicates" | ||
37 | //usage: ) | ||
38 | //usage: IF_FEATURE_IPC_SYSLOG( | 28 | //usage: IF_FEATURE_IPC_SYSLOG( |
39 | /* NB: -Csize shouldn't have space (because size is optional) */ | 29 | /* NB: -Csize shouldn't have space (because size is optional) */ |
40 | //usage: "\n -C[size_kb] Log to shared mem buffer (use logread to read it)" | 30 | //usage: "\n -C[size_kb] Log to shared mem buffer (use logread to read it)" |
41 | //usage: ) | 31 | //usage: ) |
32 | //usage: IF_FEATURE_KMSG_SYSLOG( | ||
33 | //usage: "\n -K Log to kernel printk buffer (use dmesg to read it)" | ||
34 | //usage: ) | ||
35 | //usage: "\n -O FILE Log to FILE (default:/var/log/messages, stdout if -)" | ||
36 | //usage: IF_FEATURE_ROTATE_LOGFILE( | ||
37 | //usage: "\n -s SIZE Max size (KB) before rotation (default:200KB, 0=off)" | ||
38 | //usage: "\n -b N N rotated logs to keep (default:1, max=99, 0=purge)" | ||
39 | //usage: ) | ||
40 | //usage: "\n -l N Log only messages more urgent than prio N (1-8)" | ||
41 | //usage: "\n -S Smaller output" | ||
42 | //usage: IF_FEATURE_SYSLOGD_DUP( | ||
43 | //usage: "\n -D Drop duplicates" | ||
44 | //usage: ) | ||
42 | //usage: IF_FEATURE_SYSLOGD_CFG( | 45 | //usage: IF_FEATURE_SYSLOGD_CFG( |
43 | //usage: "\n -f FILE Use FILE as config (default:/etc/syslog.conf)" | 46 | //usage: "\n -f FILE Use FILE as config (default:/etc/syslog.conf)" |
44 | //usage: ) | 47 | //usage: ) |
45 | /* //usage: "\n -m MIN Minutes between MARK lines (default:20, 0=off)" */ | 48 | /* //usage: "\n -m MIN Minutes between MARK lines (default:20, 0=off)" */ |
46 | //usage: IF_FEATURE_KMSG_SYSLOG( | ||
47 | //usage: "\n -K Log to kernel printk buffer (use dmesg to read it)" | ||
48 | //usage: ) | ||
49 | //usage: | 49 | //usage: |
50 | //usage:#define syslogd_example_usage | 50 | //usage:#define syslogd_example_usage |
51 | //usage: "$ syslogd -R masterlog:514\n" | 51 | //usage: "$ syslogd -R masterlog:514\n" |
@@ -585,7 +585,9 @@ static void log_locally(time_t now, char *msg, logFile_t *log_file) | |||
585 | #endif | 585 | #endif |
586 | int len = strlen(msg); | 586 | int len = strlen(msg); |
587 | 587 | ||
588 | if (log_file->fd >= 0) { | 588 | /* fd can't be 0 (we connect fd 0 to /dev/log socket) */ |
589 | /* fd is 1 if "-O -" is in use */ | ||
590 | if (log_file->fd > 1) { | ||
589 | /* Reopen log file every second. This allows admin | 591 | /* Reopen log file every second. This allows admin |
590 | * to delete the file and not worry about restarting us. | 592 | * to delete the file and not worry about restarting us. |
591 | * This costs almost nothing since it happens | 593 | * This costs almost nothing since it happens |
@@ -598,29 +600,38 @@ static void log_locally(time_t now, char *msg, logFile_t *log_file) | |||
598 | close(log_file->fd); | 600 | close(log_file->fd); |
599 | goto reopen; | 601 | goto reopen; |
600 | } | 602 | } |
601 | } else { | 603 | } |
604 | else if (log_file->fd == 1) { | ||
605 | /* We are logging to stdout: do nothing */ | ||
606 | } | ||
607 | else { | ||
608 | if (LONE_DASH(log_file->path)) { | ||
609 | log_file->fd = 1; | ||
610 | /* log_file->isRegular = 0; - already is */ | ||
611 | } else { | ||
602 | reopen: | 612 | reopen: |
603 | log_file->fd = open(log_file->path, O_WRONLY | O_CREAT | 613 | log_file->fd = open(log_file->path, O_WRONLY | O_CREAT |
604 | | O_NOCTTY | O_APPEND | O_NONBLOCK, | 614 | | O_NOCTTY | O_APPEND | O_NONBLOCK, |
605 | 0666); | 615 | 0666); |
606 | if (log_file->fd < 0) { | 616 | if (log_file->fd < 0) { |
607 | /* cannot open logfile? - print to /dev/console then */ | 617 | /* cannot open logfile? - print to /dev/console then */ |
608 | int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK); | 618 | int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK); |
609 | if (fd < 0) | 619 | if (fd < 0) |
610 | fd = 2; /* then stderr, dammit */ | 620 | fd = 2; /* then stderr, dammit */ |
611 | full_write(fd, msg, len); | 621 | full_write(fd, msg, len); |
612 | if (fd != 2) | 622 | if (fd != 2) |
613 | close(fd); | 623 | close(fd); |
614 | return; | 624 | return; |
615 | } | 625 | } |
616 | #if ENABLE_FEATURE_ROTATE_LOGFILE | 626 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
617 | { | 627 | { |
618 | struct stat statf; | 628 | struct stat statf; |
619 | log_file->isRegular = (fstat(log_file->fd, &statf) == 0 && S_ISREG(statf.st_mode)); | 629 | log_file->isRegular = (fstat(log_file->fd, &statf) == 0 && S_ISREG(statf.st_mode)); |
620 | /* bug (mostly harmless): can wrap around if file > 4gb */ | 630 | /* bug (mostly harmless): can wrap around if file > 4gb */ |
621 | log_file->size = statf.st_size; | 631 | log_file->size = statf.st_size; |
622 | } | 632 | } |
623 | #endif | 633 | #endif |
634 | } | ||
624 | } | 635 | } |
625 | 636 | ||
626 | #ifdef SYSLOGD_WRLOCK | 637 | #ifdef SYSLOGD_WRLOCK |
@@ -865,7 +876,6 @@ static int try_to_resolve_remote(remoteHost_t *rh) | |||
865 | static void do_syslogd(void) NORETURN; | 876 | static void do_syslogd(void) NORETURN; |
866 | static void do_syslogd(void) | 877 | static void do_syslogd(void) |
867 | { | 878 | { |
868 | int sock_fd; | ||
869 | #if ENABLE_FEATURE_REMOTE_LOG | 879 | #if ENABLE_FEATURE_REMOTE_LOG |
870 | llist_t *item; | 880 | llist_t *item; |
871 | #endif | 881 | #endif |
@@ -886,7 +896,7 @@ static void do_syslogd(void) | |||
886 | signal(SIGALRM, do_mark); | 896 | signal(SIGALRM, do_mark); |
887 | alarm(G.markInterval); | 897 | alarm(G.markInterval); |
888 | #endif | 898 | #endif |
889 | sock_fd = create_socket(); | 899 | xmove_fd(create_socket(), STDIN_FILENO); |
890 | 900 | ||
891 | if (option_mask32 & OPT_circularlog) | 901 | if (option_mask32 & OPT_circularlog) |
892 | ipcsyslog_init(); | 902 | ipcsyslog_init(); |
@@ -907,7 +917,7 @@ static void do_syslogd(void) | |||
907 | recvbuf = G.recvbuf; | 917 | recvbuf = G.recvbuf; |
908 | #endif | 918 | #endif |
909 | read_again: | 919 | read_again: |
910 | sz = read(sock_fd, recvbuf, MAX_READ - 1); | 920 | sz = read(STDIN_FILENO, recvbuf, MAX_READ - 1); |
911 | if (sz < 0) { | 921 | if (sz < 0) { |
912 | if (!bb_got_signal) | 922 | if (!bb_got_signal) |
913 | bb_perror_msg("read from %s", _PATH_LOG); | 923 | bb_perror_msg("read from %s", _PATH_LOG); |
@@ -978,7 +988,6 @@ static void do_syslogd(void) | |||
978 | } /* while (!bb_got_signal) */ | 988 | } /* while (!bb_got_signal) */ |
979 | 989 | ||
980 | timestamp_and_log_internal("syslogd exiting"); | 990 | timestamp_and_log_internal("syslogd exiting"); |
981 | puts("syslogd exiting"); | ||
982 | remove_pidfile(CONFIG_PID_FILE_PATH "/syslogd.pid"); | 991 | remove_pidfile(CONFIG_PID_FILE_PATH "/syslogd.pid"); |
983 | ipcsyslog_cleanup(); | 992 | ipcsyslog_cleanup(); |
984 | if (option_mask32 & OPT_kmsg) | 993 | if (option_mask32 & OPT_kmsg) |