svc: remove superfluout INIT_G()
[oweals/busybox.git] / runit / svlogd.c
index 052806c251ba9f15bcc9653a6dedb95bcf0d829e..831873d86fcef75aa4987db07c9889027f41e837 100644 (file)
@@ -13,7 +13,7 @@ modification, are permitted provided that the following conditions are met:
    3. The name of the author may not be used to endorse or promote products
       derived from this software without specific prior written permission.
 
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
@@ -26,7 +26,6 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
 /* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
-/* TODO: depends on runit_lib.c - review and reduce/eliminate */
 
 /*
 Config files
@@ -125,9 +124,38 @@ log message, you can use a pattern like this instead
 -*: *: pid *
 */
 
-#include <sys/poll.h>
+//config:config SVLOGD
+//config:      bool "svlogd (15 kb)"
+//config:      default y
+//config:      help
+//config:      svlogd continuously reads log data from its standard input, optionally
+//config:      filters log messages, and writes the data to one or more automatically
+//config:      rotated logs.
+
+//applet:IF_SVLOGD(APPLET(svlogd, BB_DIR_USR_SBIN, BB_SUID_DROP))
+
+//kbuild:lib-$(CONFIG_SVLOGD) += svlogd.o
+
+//usage:#define svlogd_trivial_usage
+//usage:       "[-tttv] [-r C] [-R CHARS] [-l MATCHLEN] [-b BUFLEN] DIR..."
+//usage:#define svlogd_full_usage "\n\n"
+//usage:       "Read log data from stdin and write to rotated log files in DIRs"
+//usage:   "\n"
+//usage:   "\n""DIR/config file modifies behavior:"
+//usage:   "\n""sSIZE - when to rotate logs"
+//usage:   "\n""nNUM - number of files to retain"
+/*usage:   "\n""NNUM - min number files to retain" - confusing */
+/*usage:   "\n""tSEC - rotate file if it get SEC seconds old" - confusing */
+//usage:   "\n""!PROG - process rotated log with PROG"
+/*usage:   "\n""uIPADDR - send log over UDP" - unsupported */
+/*usage:   "\n""UIPADDR - send log over UDP and DONT log" - unsupported */
+/*usage:   "\n""pPFX - prefix each line with PFX" - unsupported */
+//usage:   "\n""+,-PATTERN - (de)select line for logging"
+//usage:   "\n""E,ePATTERN - (de)select line for stderr"
+
 #include <sys/file.h>
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "runit_lib.h"
 
 #define LESS(a,b) ((int)((unsigned)(b) - (unsigned)(a)) > 0)
@@ -170,6 +198,7 @@ struct globals {
        unsigned nearest_rotate;
 
        void* (*memRchr)(const void *, int, size_t);
+       char *shell;
 
        smallint exitasap;
        smallint rotateasap;
@@ -205,7 +234,9 @@ struct globals {
 #define blocked_sigset (G.blocked_sigset)
 #define fl_flag_0      (G.fl_flag_0     )
 #define dirn           (G.dirn          )
+#define line bb_common_bufsiz1
 #define INIT_G() do { \
+       setup_common_bufsiz(); \
        SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
        linemax = 1000; \
        /*buflen = 1024;*/ \
@@ -213,8 +244,6 @@ struct globals {
        replace = ""; \
 } while (0)
 
-#define line bb_common_bufsiz1
-
 
 #define FATAL "fatal: "
 #define WARNING "warning: "
@@ -310,17 +339,18 @@ static unsigned pmatch(const char *p, const char *s, unsigned len)
 /*** ex fmt_ptime.[ch] ***/
 
 /* NUL terminated */
-static void fmt_time_human_30nul(char *s)
+static void fmt_time_human_30nul(char *s, char dt_delim)
 {
        struct tm *ptm;
        struct timeval tv;
 
        gettimeofday(&tv, NULL);
        ptm = gmtime(&tv.tv_sec);
-       sprintf(s, "%04u-%02u-%02u_%02u:%02u:%02u.%06u000",
+       sprintf(s, "%04u-%02u-%02u%c%02u:%02u:%02u.%06u000",
                (unsigned)(1900 + ptm->tm_year),
                (unsigned)(ptm->tm_mon + 1),
                (unsigned)(ptm->tm_mday),
+               dt_delim,
                (unsigned)(ptm->tm_hour),
                (unsigned)(ptm->tm_min),
                (unsigned)(ptm->tm_sec),
@@ -365,6 +395,9 @@ static void processorstart(struct logdir *ld)
        /* vfork'ed child trashes this byte, save... */
        sv_ch = ld->fnsave[26];
 
+       if (!G.shell)
+               G.shell = xstrdup(get_shell_name());
+
        while ((pid = vfork()) == -1)
                pause2cannot("vfork for processor", ld->name);
        if (!pid) {
@@ -399,8 +432,7 @@ static void processorstart(struct logdir *ld)
                fd = xopen("newstate", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
                xmove_fd(fd, 5);
 
-// getenv("SHELL")?
-               execl(DEFAULT_SHELL, DEFAULT_SHELL_SHORT_NAME, "-c", ld->processor, (char*) NULL);
+               execl(G.shell, G.shell, "-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 */
@@ -581,12 +613,12 @@ static int buffer_pwrite(int n, char *s, unsigned len)
 
                        while (fchdir(ld->fddir) == -1)
                                pause2cannot("change directory, want remove old logfile",
-                                                        ld->name);
+                                                       ld->name);
                        oldest[0] = 'A';
                        oldest[1] = oldest[27] = '\0';
                        while (!(d = opendir(".")))
                                pause2cannot("open directory, want remove old logfile",
-                                                        ld->name);
+                                                       ld->name);
                        errno = 0;
                        while ((f = readdir(d)))
                                if ((f->d_name[0] == '@') && (strlen(f->d_name) == 27)) {
@@ -725,11 +757,6 @@ static NOINLINE unsigned logdir_open(struct logdir *ld, const char *fn)
                                ld->inst = new;
                                break;
                        case 's': {
-                               static const struct suffix_mult km_suffixes[] = {
-                                       { "k", 1024 },
-                                       { "m", 1024*1024 },
-                                       { "", 0 }
-                               };
                                ld->sizemax = xatou_sfx(&s[1], km_suffixes);
                                break;
                        }
@@ -758,7 +785,7 @@ static NOINLINE unsigned logdir_open(struct logdir *ld, const char *fn)
                        case '!':
                                if (s[1]) {
                                        free(ld->processor);
-                                       ld->processor = wstrdup(s);
+                                       ld->processor = wstrdup(&s[1]);
                                }
                                break;
                        }
@@ -1020,9 +1047,9 @@ int svlogd_main(int argc, char **argv)
        }
        if (opt & 2) if (!repl) repl = '_'; // -R
        if (opt & 4) { // -l
-               linemax = xatou_range(l, 0, BUFSIZ-26);
+               linemax = xatou_range(l, 0, COMMON_BUFSIZE-26);
                if (linemax == 0)
-                       linemax = BUFSIZ-26;
+                       linemax = COMMON_BUFSIZE-26;
                if (linemax < 256)
                        linemax = 256;
        }
@@ -1114,7 +1141,8 @@ int svlogd_main(int argc, char **argv)
                /* Search for '\n' (in fact, np already holds the result) */
                linelen = stdin_cnt;
                if (np) {
- print_to_nl:          /* NB: starting from here lineptr may point
+ print_to_nl:
+                       /* NB: starting from here lineptr may point
                         * farther out into line[] */
                        linelen = np - lineptr + 1;
                }
@@ -1133,8 +1161,8 @@ int svlogd_main(int argc, char **argv)
                if (timestamp) {
                        if (timestamp == 1)
                                fmt_time_bernstein_25(stamp);
-                       else /* 2: */
-                               fmt_time_human_30nul(stamp);
+                       else /* 2+: */
+                               fmt_time_human_30nul(stamp, timestamp == 2 ? '_' : 'T');
                        printlen += 26;
                        printptr -= 26;
                        memcpy(printptr, stamp, 25);