diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-01-03 12:12:27 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-01-03 12:12:27 +0000 |
commit | 4f2e8bc7656ff64402329de5364ec4502d8007d9 (patch) | |
tree | 7f5fbe266b6763d89027c01943a01857e2eb8ef2 | |
parent | fb0eba706cccd510d99c4c5339a76dd15bc8a628 (diff) | |
download | busybox-w32-4f2e8bc7656ff64402329de5364ec4502d8007d9.tar.gz busybox-w32-4f2e8bc7656ff64402329de5364ec4502d8007d9.tar.bz2 busybox-w32-4f2e8bc7656ff64402329de5364ec4502d8007d9.zip |
syslogd: don't die if remote host's IP cannot be resolved.
retry resolutions every two minutes instead.
function old new delta
syslogd_main 865 904 +39
timestamp_and_log 324 313 -11
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 39/-11) Total: 28 bytes
-rw-r--r-- | sysklogd/syslogd.c | 100 |
1 files changed, 63 insertions, 37 deletions
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index 5998732bd..7000e9301 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c | |||
@@ -42,7 +42,10 @@ | |||
42 | * (semaphores are down but do_mark routine tries to down them again) */ | 42 | * (semaphores are down but do_mark routine tries to down them again) */ |
43 | #undef SYSLOGD_MARK | 43 | #undef SYSLOGD_MARK |
44 | 44 | ||
45 | enum { MAX_READ = 256 }; | 45 | enum { |
46 | MAX_READ = 256, | ||
47 | DNS_WAIT_SEC = 2 * 60, | ||
48 | }; | ||
46 | 49 | ||
47 | /* Semaphore operation structures */ | 50 | /* Semaphore operation structures */ |
48 | struct shbuf_ds { | 51 | struct shbuf_ds { |
@@ -86,6 +89,12 @@ struct init_globals { | |||
86 | 89 | ||
87 | struct globals { | 90 | struct globals { |
88 | GLOBALS | 91 | GLOBALS |
92 | |||
93 | #if ENABLE_FEATURE_REMOTE_LOG | ||
94 | unsigned last_dns_resolve; | ||
95 | char *remoteAddrStr; | ||
96 | #endif | ||
97 | |||
89 | #if ENABLE_FEATURE_IPC_SYSLOG | 98 | #if ENABLE_FEATURE_IPC_SYSLOG |
90 | struct shbuf_ds *shbuf; | 99 | struct shbuf_ds *shbuf; |
91 | #endif | 100 | #endif |
@@ -128,6 +137,9 @@ static const struct init_globals init_data = { | |||
128 | }; | 137 | }; |
129 | 138 | ||
130 | #define G (*ptr_to_globals) | 139 | #define G (*ptr_to_globals) |
140 | #define INIT_G() do { \ | ||
141 | PTR_TO_GLOBALS = memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data)); \ | ||
142 | } while (0) | ||
131 | 143 | ||
132 | 144 | ||
133 | /* Options */ | 145 | /* Options */ |
@@ -140,7 +152,7 @@ enum { | |||
140 | USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s | 152 | USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s |
141 | USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b | 153 | USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b |
142 | USE_FEATURE_REMOTE_LOG( OPTBIT_remote ,) // -R | 154 | USE_FEATURE_REMOTE_LOG( OPTBIT_remote ,) // -R |
143 | USE_FEATURE_REMOTE_LOG( OPTBIT_localtoo ,) // -L | 155 | USE_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L |
144 | USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C | 156 | USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C |
145 | 157 | ||
146 | OPT_mark = 1 << OPTBIT_mark , | 158 | OPT_mark = 1 << OPTBIT_mark , |
@@ -151,7 +163,7 @@ enum { | |||
151 | OPT_filesize = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0, | 163 | OPT_filesize = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0, |
152 | OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0, | 164 | OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0, |
153 | OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remote )) + 0, | 165 | OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remote )) + 0, |
154 | OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_localtoo )) + 0, | 166 | OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0, |
155 | OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0, | 167 | OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0, |
156 | }; | 168 | }; |
157 | #define OPTION_STR "m:nO:l:S" \ | 169 | #define OPTION_STR "m:nO:l:S" \ |
@@ -163,12 +175,11 @@ enum { | |||
163 | #define OPTION_DECL *opt_m, *opt_l \ | 175 | #define OPTION_DECL *opt_m, *opt_l \ |
164 | USE_FEATURE_ROTATE_LOGFILE(,*opt_s) \ | 176 | USE_FEATURE_ROTATE_LOGFILE(,*opt_s) \ |
165 | USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \ | 177 | USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \ |
166 | USE_FEATURE_REMOTE_LOG( ,*opt_R) \ | ||
167 | USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL) | 178 | USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL) |
168 | #define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \ | 179 | #define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \ |
169 | USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \ | 180 | USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \ |
170 | USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \ | 181 | USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \ |
171 | USE_FEATURE_REMOTE_LOG( ,&opt_R) \ | 182 | USE_FEATURE_REMOTE_LOG( ,&G.remoteAddrStr) \ |
172 | USE_FEATURE_IPC_SYSLOG( ,&opt_C) | 183 | USE_FEATURE_IPC_SYSLOG( ,&opt_C) |
173 | 184 | ||
174 | 185 | ||
@@ -387,6 +398,9 @@ static void timestamp_and_log(int pri, char *msg, int len) | |||
387 | { | 398 | { |
388 | char *timestamp; | 399 | char *timestamp; |
389 | 400 | ||
401 | if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog)) | ||
402 | return; | ||
403 | |||
390 | if (len < 16 || msg[3] != ' ' || msg[6] != ' ' | 404 | if (len < 16 || msg[3] != ' ' || msg[6] != ' ' |
391 | || msg[9] != ':' || msg[12] != ':' || msg[15] != ' ' | 405 | || msg[9] != ':' || msg[12] != ':' || msg[15] != ' ' |
392 | ) { | 406 | ) { |
@@ -400,18 +414,14 @@ static void timestamp_and_log(int pri, char *msg, int len) | |||
400 | timestamp[15] = '\0'; | 414 | timestamp[15] = '\0'; |
401 | 415 | ||
402 | /* Log message locally (to file or shared mem) */ | 416 | /* Log message locally (to file or shared mem) */ |
403 | if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) { | 417 | if (option_mask32 & OPT_small) |
404 | if (LOG_PRI(pri) < G.logLevel) { | 418 | sprintf(G.printbuf, "%s %s\n", timestamp, msg); |
405 | if (option_mask32 & OPT_small) | 419 | else { |
406 | sprintf(G.printbuf, "%s %s\n", timestamp, msg); | 420 | char res[20]; |
407 | else { | 421 | parse_fac_prio_20(pri, res); |
408 | char res[20]; | 422 | sprintf(G.printbuf, "%s %s %s %s\n", timestamp, G.localHostName, res, msg); |
409 | parse_fac_prio_20(pri, res); | ||
410 | sprintf(G.printbuf, "%s %s %s %s\n", timestamp, G.localHostName, res, msg); | ||
411 | } | ||
412 | log_locally(G.printbuf); | ||
413 | } | ||
414 | } | 423 | } |
424 | log_locally(G.printbuf); | ||
415 | } | 425 | } |
416 | 426 | ||
417 | static void split_escape_and_log(char *tmpbuf, int len) | 427 | static void split_escape_and_log(char *tmpbuf, int len) |
@@ -443,8 +453,10 @@ static void split_escape_and_log(char *tmpbuf, int len) | |||
443 | *q++ = c; | 453 | *q++ = c; |
444 | } | 454 | } |
445 | *q = '\0'; | 455 | *q = '\0'; |
456 | |||
446 | /* Now log it */ | 457 | /* Now log it */ |
447 | timestamp_and_log(pri, G.parsebuf, q - G.parsebuf); | 458 | if (LOG_PRI(pri) < G.logLevel) |
459 | timestamp_and_log(pri, G.parsebuf, q - G.parsebuf); | ||
448 | } | 460 | } |
449 | } | 461 | } |
450 | 462 | ||
@@ -495,6 +507,24 @@ static NOINLINE int create_socket(void) | |||
495 | return sock_fd; | 507 | return sock_fd; |
496 | } | 508 | } |
497 | 509 | ||
510 | #if ENABLE_FEATURE_REMOTE_LOG | ||
511 | static int try_to_resolve_remote(void) | ||
512 | { | ||
513 | if (!G.remoteAddr) { | ||
514 | unsigned now = monotonic_sec(); | ||
515 | |||
516 | /* Don't resolve name too often - DNS timeouts can be big */ | ||
517 | if ((now - G.last_dns_resolve) < DNS_WAIT_SEC) | ||
518 | return -1; | ||
519 | G.last_dns_resolve = now; | ||
520 | G.remoteAddr = host2sockaddr(G.remoteAddrStr, 514); | ||
521 | if (!G.remoteAddr) | ||
522 | return -1; | ||
523 | } | ||
524 | return socket(G.remoteAddr->sa.sa_family, SOCK_DGRAM, 0); | ||
525 | } | ||
526 | #endif | ||
527 | |||
498 | static void do_syslogd(void) ATTRIBUTE_NORETURN; | 528 | static void do_syslogd(void) ATTRIBUTE_NORETURN; |
499 | static void do_syslogd(void) | 529 | static void do_syslogd(void) |
500 | { | 530 | { |
@@ -505,10 +535,7 @@ static void do_syslogd(void) | |||
505 | signal(SIGTERM, quit_signal); | 535 | signal(SIGTERM, quit_signal); |
506 | signal(SIGQUIT, quit_signal); | 536 | signal(SIGQUIT, quit_signal); |
507 | signal(SIGHUP, SIG_IGN); | 537 | signal(SIGHUP, SIG_IGN); |
508 | signal(SIGCHLD, SIG_IGN); | 538 | /* signal(SIGCHLD, SIG_IGN); - why? */ |
509 | #ifdef SIGCLD | ||
510 | signal(SIGCLD, SIG_IGN); | ||
511 | #endif | ||
512 | #ifdef SYSLOGD_MARK | 539 | #ifdef SYSLOGD_MARK |
513 | signal(SIGALRM, do_mark); | 540 | signal(SIGALRM, do_mark); |
514 | alarm(G.markInterval); | 541 | alarm(G.markInterval); |
@@ -554,19 +581,21 @@ static void do_syslogd(void) | |||
554 | #if ENABLE_FEATURE_REMOTE_LOG | 581 | #if ENABLE_FEATURE_REMOTE_LOG |
555 | /* We are not modifying log messages in any way before send */ | 582 | /* We are not modifying log messages in any way before send */ |
556 | /* Remote site cannot trust _us_ anyway and need to do validation again */ | 583 | /* Remote site cannot trust _us_ anyway and need to do validation again */ |
557 | if (G.remoteAddr) { | 584 | if (G.remoteAddrStr) { |
558 | if (-1 == G.remoteFD) { | 585 | if (-1 == G.remoteFD) { |
559 | G.remoteFD = socket(G.remoteAddr->sa.sa_family, SOCK_DGRAM, 0); | 586 | G.remoteFD = try_to_resolve_remote(); |
560 | } | 587 | if (-1 == G.remoteFD) |
561 | if (-1 != G.remoteFD) { | 588 | goto no_luck; |
562 | /* send message to remote logger, ignore possible error */ | ||
563 | sendto(G.remoteFD, G.recvbuf, sz, MSG_DONTWAIT, | ||
564 | &G.remoteAddr->sa, G.remoteAddr->len); | ||
565 | } | 589 | } |
590 | /* send message to remote logger, ignore possible error */ | ||
591 | sendto(G.remoteFD, G.recvbuf, sz, MSG_DONTWAIT, | ||
592 | &G.remoteAddr->sa, G.remoteAddr->len); | ||
593 | no_luck: ; | ||
566 | } | 594 | } |
567 | #endif | 595 | #endif |
568 | split_escape_and_log(G.recvbuf, sz); | 596 | if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) |
569 | } /* for */ | 597 | split_escape_and_log(G.recvbuf, sz); |
598 | } /* for (;;) */ | ||
570 | } | 599 | } |
571 | 600 | ||
572 | int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 601 | int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
@@ -575,7 +604,10 @@ int syslogd_main(int argc, char **argv) | |||
575 | char OPTION_DECL; | 604 | char OPTION_DECL; |
576 | char *p; | 605 | char *p; |
577 | 606 | ||
578 | PTR_TO_GLOBALS = memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data)); | 607 | INIT_G(); |
608 | #if ENABLE_FEATURE_REMOTE_LOG | ||
609 | G.last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1; | ||
610 | #endif | ||
579 | 611 | ||
580 | /* do normal option parsing */ | 612 | /* do normal option parsing */ |
581 | opt_complementary = "=0"; /* no non-option params */ | 613 | opt_complementary = "=0"; /* no non-option params */ |
@@ -595,12 +627,6 @@ int syslogd_main(int argc, char **argv) | |||
595 | if (option_mask32 & OPT_rotatecnt) // -b | 627 | if (option_mask32 & OPT_rotatecnt) // -b |
596 | G.logFileRotate = xatou_range(opt_b, 0, 99); | 628 | G.logFileRotate = xatou_range(opt_b, 0, 99); |
597 | #endif | 629 | #endif |
598 | #if ENABLE_FEATURE_REMOTE_LOG | ||
599 | if (option_mask32 & OPT_remotelog) { // -R | ||
600 | G.remoteAddr = xhost2sockaddr(opt_R, 514); | ||
601 | } | ||
602 | //if (option_mask32 & OPT_locallog) // -L | ||
603 | #endif | ||
604 | #if ENABLE_FEATURE_IPC_SYSLOG | 630 | #if ENABLE_FEATURE_IPC_SYSLOG |
605 | if (opt_C) // -Cn | 631 | if (opt_C) // -Cn |
606 | G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024; | 632 | G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024; |