--- trunk/mkinitrd-magellan/busybox/runit/svlogd.c 2010/04/29 20:38:48 983 +++ trunk/mkinitrd-magellan/busybox/runit/svlogd.c 2010/05/30 11:32:42 984 @@ -28,6 +28,103 @@ /* Busyboxed by Denys Vlasenko */ /* TODO: depends on runit_lib.c - review and reduce/eliminate */ +/* +Config files + +On startup, and after receiving a HUP signal, svlogd checks for each +log directory log if the configuration file log/config exists, +and if so, reads the file line by line and adjusts configuration +for log as follows: + +If the line is empty, or starts with a #, it is ignored. A line +of the form + +ssize + sets the maximum file size of current when svlogd should rotate + the current log file to size bytes. Default is 1000000. + If size is zero, svlogd doesnt rotate log files + You should set size to at least (2 * len). +nnum + sets the number of old log files svlogd should maintain to num. + If svlogd sees more that num old log files in log after log file + rotation, it deletes the oldest one. Default is 10. + If num is zero, svlogd doesnt remove old log files. +Nmin + sets the minimum number of old log files svlogd should maintain + to min. min must be less than num. If min is set, and svlogd + cannot write to current because the filesystem is full, + and it sees more than min old log files, it deletes the oldest one. +ttimeout + sets the maximum age of the current log file when svlogd should + rotate the current log file to timeout seconds. If current + is timeout seconds old, and is not empty, svlogd forces log file rotation. +!processor + tells svlogd to feed each recent log file through processor + (see above) on log file rotation. By default log files are not processed. +ua.b.c.d[:port] + tells svlogd to transmit the first len characters of selected + log messages to the IP address a.b.c.d, port number port. + If port isnt set, the default port for syslog is used (514). + len can be set through the -l option, see below. If svlogd + has trouble sending udp packets, it writes error messages + to the log directory. Attention: logging through udp is unreliable, + and should be used in private networks only. +Ua.b.c.d[:port] + is the same as the u line above, but the log messages are no longer + written to the log directory, but transmitted through udp only. + Error messages from svlogd concerning sending udp packages still go + to the log directory. +pprefix + tells svlogd to prefix each line to be written to the log directory, + to standard error, or through UDP, with prefix. + +If a line starts with a -, +, e, or E, svlogd matches the first len characters +of each log message against pattern and acts accordingly: + +-pattern + the log message is deselected. ++pattern + the log message is selected. +epattern + the log message is selected to be printed to standard error. +Epattern + the log message is deselected to be printed to standard error. + +Initially each line is selected to be written to log/current. Deselected +log messages are discarded from log. Initially each line is deselected +to be written to standard err. Log messages selected for standard error +are written to standard error. + +Pattern Matching + +svlogd matches a log message against the string pattern as follows: + +pattern is applied to the log message one character by one, starting +with the first. A character not a star (*) and not a plus (+) matches itself. +A plus matches the next character in pattern in the log message one +or more times. A star before the end of pattern matches any string +in the log message that does not include the next character in pattern. +A star at the end of pattern matches any string. + +Timestamps optionally added by svlogd are not considered part +of the log message. + +An svlogd pattern is not a regular expression. For example consider +a log message like this + +2005-12-18_09:13:50.97618 tcpsvd: info: pid 1977 from 10.4.1.14 + +The following pattern doesnt match + +-*pid* + +because the first star matches up to the first p in tcpsvd, +and then the match fails because i is not s. To match this +log message, you can use a pattern like this instead + +-*: *: pid * +*/ + #include #include #include "libbb.h" @@ -72,6 +169,8 @@ int wstat; unsigned nearest_rotate; + void* (*memRchr)(const void *, int, size_t); + smallint exitasap; smallint rotateasap; smallint reopenasap; @@ -94,6 +193,7 @@ #define fndir (G.fndir ) #define fdwdir (G.fdwdir ) #define wstat (G.wstat ) +#define memRchr (G.memRchr ) #define nearest_rotate (G.nearest_rotate) #define exitasap (G.exitasap ) #define rotateasap (G.rotateasap ) @@ -121,7 +221,6 @@ #define PAUSE "pausing: " #define INFO "info: " -#define usage() bb_show_usage() static void fatalx(const char *m0) { bb_error_msg_and_die(FATAL"%s", m0); @@ -167,18 +266,18 @@ /* NUL terminated */ static void fmt_time_human_30nul(char *s) { - struct tm *t; + struct tm *ptm; struct timeval tv; gettimeofday(&tv, NULL); - t = gmtime(&(tv.tv_sec)); + ptm = gmtime(&tv.tv_sec); sprintf(s, "%04u-%02u-%02u_%02u:%02u:%02u.%06u000", - (unsigned)(1900 + t->tm_year), - (unsigned)(t->tm_mon + 1), - (unsigned)(t->tm_mday), - (unsigned)(t->tm_hour), - (unsigned)(t->tm_min), - (unsigned)(t->tm_sec), + (unsigned)(1900 + ptm->tm_year), + (unsigned)(ptm->tm_mon + 1), + (unsigned)(ptm->tm_mday), + (unsigned)(ptm->tm_hour), + (unsigned)(ptm->tm_min), + (unsigned)(ptm->tm_sec), (unsigned)(tv.tv_usec) ); /* 4+1 + 2+1 + 2+1 + 2+1 + 2+1 + 2+1 + 9 = */ @@ -223,7 +322,6 @@ while ((pid = vfork()) == -1) pause2cannot("vfork for processor", ld->name); if (!pid) { - char *prog[4]; int fd; /* child */ @@ -256,11 +354,7 @@ xmove_fd(fd, 5); // getenv("SHELL")? - prog[0] = (char*)"sh"; - prog[1] = (char*)"-c"; - prog[2] = ld->processor; - prog[3] = NULL; - execv("/bin/sh", prog); + execl("/bin/sh", "/bin/sh" + 5, "-c", ld->processor, (char*) NULL); bb_perror_msg_and_die(FATAL"can't %s processor %s", "run", ld->name); } ld->fnsave[26] = sv_ch; /* ...restore */ @@ -278,10 +372,11 @@ sig_block(SIGHUP); ld->ppid = 0; } - if (ld->fddir == -1) return 1; + if (ld->fddir == -1) + return 1; while (fchdir(ld->fddir) == -1) pause2cannot("change directory, want processor", ld->name); - if (wait_exitcode(wstat) != 0) { + if (WEXITSTATUS(wstat) != 0) { warnx("processor failed, restart", ld->name); ld->fnsave[26] = 't'; unlink(ld->fnsave); @@ -398,13 +493,14 @@ pause2cannot("rename current", ld->name); while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1) pause2cannot("create new current", ld->name); - /* we presume this cannot fail */ - ld->filecur = fdopen(ld->fdcur, "a"); //// + while ((ld->filecur = fdopen(ld->fdcur, "a")) == NULL) //// + pause2cannot("create new current", ld->name); /* very unlikely */ setvbuf(ld->filecur, NULL, _IOFBF, linelen); //// close_on_exec_on(ld->fdcur); ld->size = 0; while (fchmod(ld->fdcur, 0644) == -1) pause2cannot("set mode of current", ld->name); + rmoldest(ld); processorstart(ld); } @@ -507,7 +603,7 @@ ld->processor = NULL; } -static unsigned logdir_open(struct logdir *ld, const char *fn) +static NOINLINE unsigned logdir_open(struct logdir *ld, const char *fn) { char buf[128]; unsigned now; @@ -551,10 +647,11 @@ free(ld->processor); ld->processor = NULL; /* read config */ - i = open_read_close("config", buf, sizeof(buf)); + i = open_read_close("config", buf, sizeof(buf) - 1); if (i < 0 && errno != ENOENT) bb_perror_msg(WARNING"%s/config", ld->name); if (i > 0) { + buf[i] = '\0'; if (verbose) bb_error_msg(INFO"read: %s/config", ld->name); s = buf; @@ -567,9 +664,13 @@ case '-': case 'e': case 'E': + /* Filtering requires one-line buffering, + * resetting the "find newline" function + * accordingly */ + memRchr = memchr; /* Add '\n'-terminated line to ld->inst */ while (1) { - int l = asprintf(&new, "%s%s\n", ld->inst ? : "", s); + int l = asprintf(&new, "%s%s\n", ld->inst ? ld->inst : "", s); if (l >= 0 && new) break; pause_nomem(); @@ -581,7 +682,7 @@ static const struct suffix_mult km_suffixes[] = { { "k", 1024 }, { "m", 1024*1024 }, - { } + { "", 0 } }; ld->sizemax = xatou_sfx(&s[1], km_suffixes); break; @@ -597,7 +698,7 @@ { "m", 60 }, { "h", 60*60 }, /*{ "d", 24*60*60 },*/ - { } + { "", 0 } }; ld->rotate_period = xatou_sfx(&s[1], mh_suffixes); if (ld->rotate_period) { @@ -660,8 +761,8 @@ } while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1) pause2cannot("open current", ld->name); - /* we presume this cannot fail */ - ld->filecur = fdopen(ld->fdcur, "a"); //// + while ((ld->filecur = fdopen(ld->fdcur, "a")) == NULL) + pause2cannot("open current", ld->name); //// setvbuf(ld->filecur, NULL, _IOFBF, linelen); //// close_on_exec_on(ld->fdcur); @@ -711,7 +812,7 @@ struct pollfd input; int i; - input.fd = 0; + input.fd = STDIN_FILENO; input.events = POLLIN; do { @@ -855,12 +956,11 @@ int svlogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int svlogd_main(int argc, char **argv) { - char *r,*l,*b; + char *r, *l, *b; ssize_t stdin_cnt = 0; int i; unsigned opt; unsigned timestamp = 0; - void* (*memRchr)(const void *, int, size_t) = memchr; INIT_G(); @@ -869,13 +969,16 @@ &r, &replace, &l, &b, ×tamp, &verbose); if (opt & 1) { // -r repl = r[0]; - if (!repl || r[1]) usage(); + if (!repl || r[1]) + bb_show_usage(); } if (opt & 2) if (!repl) repl = '_'; // -R if (opt & 4) { // -l linemax = xatou_range(l, 0, BUFSIZ-26); - if (linemax == 0) linemax = BUFSIZ-26; - if (linemax < 256) linemax = 256; + if (linemax == 0) + linemax = BUFSIZ-26; + if (linemax < 256) + linemax = 256; } ////if (opt & 8) { // -b //// buflen = xatoi_u(b); @@ -888,11 +991,12 @@ argc -= optind; dirn = argc; - if (dirn <= 0) usage(); - ////if (buflen <= linemax) usage(); + if (dirn <= 0) + bb_show_usage(); + ////if (buflen <= linemax) bb_show_usage(); fdwdir = xopen(".", O_RDONLY|O_NDELAY); close_on_exec_on(fdwdir); - dir = xzalloc(dirn * sizeof(struct logdir)); + dir = xzalloc(dirn * sizeof(dir[0])); for (i = 0; i < dirn; ++i) { dir[i].fddir = -1; dir[i].fdcur = -1; @@ -912,18 +1016,19 @@ sigaddset(&blocked_sigset, SIGALRM); sigaddset(&blocked_sigset, SIGHUP); sigprocmask(SIG_BLOCK, &blocked_sigset, NULL); - bb_signals_recursive(1 << SIGTERM, sig_term_handler); - bb_signals_recursive(1 << SIGCHLD, sig_child_handler); - bb_signals_recursive(1 << SIGALRM, sig_alarm_handler); - bb_signals_recursive(1 << SIGHUP, sig_hangup_handler); - - logdirs_reopen(); + bb_signals_recursive_norestart(1 << SIGTERM, sig_term_handler); + bb_signals_recursive_norestart(1 << SIGCHLD, sig_child_handler); + bb_signals_recursive_norestart(1 << SIGALRM, sig_alarm_handler); + bb_signals_recursive_norestart(1 << SIGHUP, sig_hangup_handler); /* Without timestamps, we don't have to print each line * separately, so we can look for _last_ newline, not first, - * thus batching writes */ - if (!timestamp) - memRchr = memrchr; + * thus batching writes. If filtering is enabled in config, + * logdirs_reopen resets it to memchr. + */ + memRchr = (timestamp ? memchr : memrchr); + + logdirs_reopen(); setvbuf(stderr, NULL, _IOFBF, linelen); @@ -991,7 +1096,8 @@ } for (i = 0; i < dirn; ++i) { struct logdir *ld = &dir[i]; - if (ld->fddir == -1) continue; + if (ld->fddir == -1) + continue; if (ld->inst) logmatch(ld); if (ld->matcherr == 'e') { @@ -999,7 +1105,8 @@ ////full_write(STDERR_FILENO, printptr, printlen); fwrite(printptr, 1, printlen, stderr); } - if (ld->match != '+') continue; + if (ld->match != '+') + continue; buffer_pwrite(i, printptr, printlen); } @@ -1022,12 +1129,14 @@ } /* linelen == no of chars incl. '\n' (or == stdin_cnt) */ for (i = 0; i < dirn; ++i) { - if (dir[i].fddir == -1) continue; + if (dir[i].fddir == -1) + continue; if (dir[i].matcherr == 'e') { ////full_write(STDERR_FILENO, lineptr, linelen); fwrite(lineptr, 1, linelen, stderr); } - if (dir[i].match != '+') continue; + if (dir[i].match != '+') + continue; buffer_pwrite(i, lineptr, linelen); } } @@ -1043,13 +1152,13 @@ /* Move unprocessed data to the front of line */ memmove((timestamp ? line+26 : line), lineptr, stdin_cnt); } - fflush(NULL);//// + fflush_all();//// } for (i = 0; i < dirn; ++i) { if (dir[i].ppid) while (!processorstop(&dir[i])) - /* repeat */; + continue; logdir_close(&dir[i]); } return 0;