diff options
author | Ron Yorston <rmy@pobox.com> | 2014-10-06 12:50:22 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2014-10-06 12:50:22 +0100 |
commit | b04d11dcbadda2620743a1dd923938f2f3043a38 (patch) | |
tree | 971afe425a81304b79e44122e220c7a69efe2616 /sysklogd | |
parent | 124bbf02948b7ac0babb4ead04acd1559db182d3 (diff) | |
parent | 760d035699c4a878f9109544c1d35ea0d5f6b76c (diff) | |
download | busybox-w32-b04d11dcbadda2620743a1dd923938f2f3043a38.tar.gz busybox-w32-b04d11dcbadda2620743a1dd923938f2f3043a38.tar.bz2 busybox-w32-b04d11dcbadda2620743a1dd923938f2f3043a38.zip |
Merge branch 'busybox' into merge
Diffstat (limited to 'sysklogd')
-rw-r--r-- | sysklogd/syslogd.c | 105 |
1 files changed, 60 insertions, 45 deletions
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index f75851085..266657f3b 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" |
@@ -110,6 +110,7 @@ typedef struct { | |||
110 | typedef struct logFile_t { | 110 | typedef struct logFile_t { |
111 | const char *path; | 111 | const char *path; |
112 | int fd; | 112 | int fd; |
113 | time_t last_log_time; | ||
113 | #if ENABLE_FEATURE_ROTATE_LOGFILE | 114 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
114 | unsigned size; | 115 | unsigned size; |
115 | uint8_t isRegular; | 116 | uint8_t isRegular; |
@@ -165,7 +166,6 @@ struct globals { | |||
165 | #if ENABLE_FEATURE_IPC_SYSLOG | 166 | #if ENABLE_FEATURE_IPC_SYSLOG |
166 | struct shbuf_ds *shbuf; | 167 | struct shbuf_ds *shbuf; |
167 | #endif | 168 | #endif |
168 | time_t last_log_time; | ||
169 | /* localhost's name. We print only first 64 chars */ | 169 | /* localhost's name. We print only first 64 chars */ |
170 | char *hostname; | 170 | char *hostname; |
171 | 171 | ||
@@ -569,7 +569,7 @@ static void log_to_kmsg(int pri, const char *msg) | |||
569 | */ | 569 | */ |
570 | pri &= G.primask; | 570 | pri &= G.primask; |
571 | 571 | ||
572 | write(G.kmsgfd, G.printbuf, sprintf(G.printbuf, "<%d>%s\n", pri, msg)); | 572 | full_write(G.kmsgfd, G.printbuf, sprintf(G.printbuf, "<%d>%s\n", pri, msg)); |
573 | } | 573 | } |
574 | #else | 574 | #else |
575 | static void kmsg_init(void) {} | 575 | static void kmsg_init(void) {} |
@@ -585,42 +585,54 @@ 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 | /* Reopen log file every second. This allows admin | 589 | /* fd is 1 if "-O -" is in use */ |
590 | * to delete the file and not worry about restarting us. | 590 | if (log_file->fd > 1) { |
591 | /* Reopen log files every second. This allows admin | ||
592 | * to delete the files and not worry about restarting us. | ||
591 | * This costs almost nothing since it happens | 593 | * This costs almost nothing since it happens |
592 | * _at most_ once a second. | 594 | * _at most_ once a second for each file, and happens |
595 | * only when each file is actually written. | ||
593 | */ | 596 | */ |
594 | if (!now) | 597 | if (!now) |
595 | now = time(NULL); | 598 | now = time(NULL); |
596 | if (G.last_log_time != now) { | 599 | if (log_file->last_log_time != now) { |
597 | G.last_log_time = now; | 600 | log_file->last_log_time = now; |
598 | close(log_file->fd); | 601 | close(log_file->fd); |
599 | goto reopen; | 602 | goto reopen; |
600 | } | 603 | } |
601 | } else { | 604 | } |
605 | else if (log_file->fd == 1) { | ||
606 | /* We are logging to stdout: do nothing */ | ||
607 | } | ||
608 | else { | ||
609 | if (LONE_DASH(log_file->path)) { | ||
610 | log_file->fd = 1; | ||
611 | /* log_file->isRegular = 0; - already is */ | ||
612 | } else { | ||
602 | reopen: | 613 | reopen: |
603 | log_file->fd = open(log_file->path, O_WRONLY | O_CREAT | 614 | log_file->fd = open(log_file->path, O_WRONLY | O_CREAT |
604 | | O_NOCTTY | O_APPEND | O_NONBLOCK, | 615 | | O_NOCTTY | O_APPEND | O_NONBLOCK, |
605 | 0666); | 616 | 0666); |
606 | if (log_file->fd < 0) { | 617 | if (log_file->fd < 0) { |
607 | /* cannot open logfile? - print to /dev/console then */ | 618 | /* cannot open logfile? - print to /dev/console then */ |
608 | int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK); | 619 | int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK); |
609 | if (fd < 0) | 620 | if (fd < 0) |
610 | fd = 2; /* then stderr, dammit */ | 621 | fd = 2; /* then stderr, dammit */ |
611 | full_write(fd, msg, len); | 622 | full_write(fd, msg, len); |
612 | if (fd != 2) | 623 | if (fd != 2) |
613 | close(fd); | 624 | close(fd); |
614 | return; | 625 | return; |
615 | } | 626 | } |
616 | #if ENABLE_FEATURE_ROTATE_LOGFILE | 627 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
617 | { | 628 | { |
618 | struct stat statf; | 629 | struct stat statf; |
619 | log_file->isRegular = (fstat(log_file->fd, &statf) == 0 && S_ISREG(statf.st_mode)); | 630 | log_file->isRegular = (fstat(log_file->fd, &statf) == 0 && S_ISREG(statf.st_mode)); |
620 | /* bug (mostly harmless): can wrap around if file > 4gb */ | 631 | /* bug (mostly harmless): can wrap around if file > 4gb */ |
621 | log_file->size = statf.st_size; | 632 | log_file->size = statf.st_size; |
622 | } | 633 | } |
623 | #endif | 634 | #endif |
635 | } | ||
624 | } | 636 | } |
625 | 637 | ||
626 | #ifdef SYSLOGD_WRLOCK | 638 | #ifdef SYSLOGD_WRLOCK |
@@ -667,9 +679,14 @@ static void log_locally(time_t now, char *msg, logFile_t *log_file) | |||
667 | close(log_file->fd); | 679 | close(log_file->fd); |
668 | goto reopen; | 680 | goto reopen; |
669 | } | 681 | } |
670 | log_file->size += | 682 | /* TODO: what to do on write errors ("disk full")? */ |
683 | len = full_write(log_file->fd, msg, len); | ||
684 | if (len > 0) | ||
685 | log_file->size += len; | ||
686 | #else | ||
687 | full_write(log_file->fd, msg, len); | ||
671 | #endif | 688 | #endif |
672 | full_write(log_file->fd, msg, len); | 689 | |
673 | #ifdef SYSLOGD_WRLOCK | 690 | #ifdef SYSLOGD_WRLOCK |
674 | fl.l_type = F_UNLCK; | 691 | fl.l_type = F_UNLCK; |
675 | fcntl(log_file->fd, F_SETLKW, &fl); | 692 | fcntl(log_file->fd, F_SETLKW, &fl); |
@@ -865,7 +882,6 @@ static int try_to_resolve_remote(remoteHost_t *rh) | |||
865 | static void do_syslogd(void) NORETURN; | 882 | static void do_syslogd(void) NORETURN; |
866 | static void do_syslogd(void) | 883 | static void do_syslogd(void) |
867 | { | 884 | { |
868 | int sock_fd; | ||
869 | #if ENABLE_FEATURE_REMOTE_LOG | 885 | #if ENABLE_FEATURE_REMOTE_LOG |
870 | llist_t *item; | 886 | llist_t *item; |
871 | #endif | 887 | #endif |
@@ -886,7 +902,7 @@ static void do_syslogd(void) | |||
886 | signal(SIGALRM, do_mark); | 902 | signal(SIGALRM, do_mark); |
887 | alarm(G.markInterval); | 903 | alarm(G.markInterval); |
888 | #endif | 904 | #endif |
889 | sock_fd = create_socket(); | 905 | xmove_fd(create_socket(), STDIN_FILENO); |
890 | 906 | ||
891 | if (option_mask32 & OPT_circularlog) | 907 | if (option_mask32 & OPT_circularlog) |
892 | ipcsyslog_init(); | 908 | ipcsyslog_init(); |
@@ -907,7 +923,7 @@ static void do_syslogd(void) | |||
907 | recvbuf = G.recvbuf; | 923 | recvbuf = G.recvbuf; |
908 | #endif | 924 | #endif |
909 | read_again: | 925 | read_again: |
910 | sz = read(sock_fd, recvbuf, MAX_READ - 1); | 926 | sz = read(STDIN_FILENO, recvbuf, MAX_READ - 1); |
911 | if (sz < 0) { | 927 | if (sz < 0) { |
912 | if (!bb_got_signal) | 928 | if (!bb_got_signal) |
913 | bb_perror_msg("read from %s", _PATH_LOG); | 929 | bb_perror_msg("read from %s", _PATH_LOG); |
@@ -978,7 +994,6 @@ static void do_syslogd(void) | |||
978 | } /* while (!bb_got_signal) */ | 994 | } /* while (!bb_got_signal) */ |
979 | 995 | ||
980 | timestamp_and_log_internal("syslogd exiting"); | 996 | timestamp_and_log_internal("syslogd exiting"); |
981 | puts("syslogd exiting"); | ||
982 | remove_pidfile(CONFIG_PID_FILE_PATH "/syslogd.pid"); | 997 | remove_pidfile(CONFIG_PID_FILE_PATH "/syslogd.pid"); |
983 | ipcsyslog_cleanup(); | 998 | ipcsyslog_cleanup(); |
984 | if (option_mask32 & OPT_kmsg) | 999 | if (option_mask32 & OPT_kmsg) |