Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (8 years ago)
Author:
bruno
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/runit/svlogd.c

    r1765 r2725  
    2626*/
    2727
    28 /* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */
     28/* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
    2929/* TODO: depends on runit_lib.c - review and reduce/eliminate */
     30
     31/*
     32Config files
     33
     34On startup, and after receiving a HUP signal, svlogd checks for each
     35log directory log if the configuration file log/config exists,
     36and if so, reads the file line by line and adjusts configuration
     37for log as follows:
     38
     39If the line is empty, or starts with a #, it is ignored. A line
     40of the form
     41
     42ssize
     43    sets the maximum file size of current when svlogd should rotate
     44    the current log file to size bytes. Default is 1000000.
     45    If size is zero, svlogd doesnt rotate log files
     46    You should set size to at least (2 * len).
     47nnum
     48    sets the number of old log files svlogd should maintain to num.
     49    If svlogd sees more that num old log files in log after log file
     50    rotation, it deletes the oldest one. Default is 10.
     51    If num is zero, svlogd doesnt remove old log files.
     52Nmin
     53    sets the minimum number of old log files svlogd should maintain
     54    to min. min must be less than num. If min is set, and svlogd
     55    cannot write to current because the filesystem is full,
     56    and it sees more than min old log files, it deletes the oldest one.
     57ttimeout
     58    sets the maximum age of the current log file when svlogd should
     59    rotate the current log file to timeout seconds. If current
     60    is timeout seconds old, and is not empty, svlogd forces log file rotation.
     61!processor
     62    tells svlogd to feed each recent log file through processor
     63    (see above) on log file rotation. By default log files are not processed.
     64ua.b.c.d[:port]
     65    tells svlogd to transmit the first len characters of selected
     66    log messages to the IP address a.b.c.d, port number port.
     67    If port isnt set, the default port for syslog is used (514).
     68    len can be set through the -l option, see below. If svlogd
     69    has trouble sending udp packets, it writes error messages
     70    to the log directory. Attention: logging through udp is unreliable,
     71    and should be used in private networks only.
     72Ua.b.c.d[:port]
     73    is the same as the u line above, but the log messages are no longer
     74    written to the log directory, but transmitted through udp only.
     75    Error messages from svlogd concerning sending udp packages still go
     76    to the log directory.
     77pprefix
     78    tells svlogd to prefix each line to be written to the log directory,
     79    to standard error, or through UDP, with prefix.
     80
     81If a line starts with a -, +, e, or E, svlogd matches the first len characters
     82of each log message against pattern and acts accordingly:
     83
     84-pattern
     85    the log message is deselected.
     86+pattern
     87    the log message is selected.
     88epattern
     89    the log message is selected to be printed to standard error.
     90Epattern
     91    the log message is deselected to be printed to standard error.
     92
     93Initially each line is selected to be written to log/current. Deselected
     94log messages are discarded from log. Initially each line is deselected
     95to be written to standard err. Log messages selected for standard error
     96are written to standard error.
     97
     98Pattern Matching
     99
     100svlogd matches a log message against the string pattern as follows:
     101
     102pattern is applied to the log message one character by one, starting
     103with the first. A character not a star (*) and not a plus (+) matches itself.
     104A plus matches the next character in pattern in the log message one
     105or more times. A star before the end of pattern matches any string
     106in the log message that does not include the next character in pattern.
     107A star at the end of pattern matches any string.
     108
     109Timestamps optionally added by svlogd are not considered part
     110of the log message.
     111
     112An svlogd pattern is not a regular expression. For example consider
     113a log message like this
     114
     1152005-12-18_09:13:50.97618 tcpsvd: info: pid 1977 from 10.4.1.14
     116
     117The following pattern doesnt match
     118
     119-*pid*
     120
     121because the first star matches up to the first p in tcpsvd,
     122and then the match fails because i is not s. To match this
     123log message, you can use a pattern like this instead
     124
     125-*: *: pid *
     126*/
    30127
    31128#include <sys/poll.h>
     
    38135#define FMT_PTIME 30
    39136
    40 static unsigned verbose;
    41 static int linemax = 1000;
    42 ////static int buflen = 1024;
    43 static int linelen;
    44 
    45 static char **fndir;
    46 static int fdwdir;
    47 static int wstat;
    48 static unsigned nearest_rotate;
    49 
    50 static char *line;
    51 static smallint exitasap;
    52 static smallint rotateasap;
    53 static smallint reopenasap;
    54 static smallint linecomplete = 1;
    55 
    56 static smallint tmaxflag;
    57 
    58 static char repl;
    59 static const char *replace = "";
    60 
    61 static sigset_t *blocked_sigset;
    62 static int fl_flag_0;
    63 
    64 static struct logdir {
     137struct logdir {
    65138    ////char *btmp;
    66139    /* pattern list to match, in "aa\0bb\0\cc\0\0" form */
     
    82155    char match;
    83156    char matcherr;
    84 } *dir;
    85 static unsigned dirn;
     157};
     158
     159
     160struct globals {
     161    struct logdir *dir;
     162    unsigned verbose;
     163    int linemax;
     164    ////int buflen;
     165    int linelen;
     166
     167    int fdwdir;
     168    char **fndir;
     169    int wstat;
     170    unsigned nearest_rotate;
     171
     172    void* (*memRchr)(const void *, int, size_t);
     173
     174    smallint exitasap;
     175    smallint rotateasap;
     176    smallint reopenasap;
     177    smallint linecomplete;
     178    smallint tmaxflag;
     179
     180    char repl;
     181    const char *replace;
     182    int fl_flag_0;
     183    unsigned dirn;
     184
     185    sigset_t blocked_sigset;
     186};
     187#define G (*ptr_to_globals)
     188#define dir            (G.dir           )
     189#define verbose        (G.verbose       )
     190#define linemax        (G.linemax       )
     191#define buflen         (G.buflen        )
     192#define linelen        (G.linelen       )
     193#define fndir          (G.fndir         )
     194#define fdwdir         (G.fdwdir        )
     195#define wstat          (G.wstat         )
     196#define memRchr        (G.memRchr       )
     197#define nearest_rotate (G.nearest_rotate)
     198#define exitasap       (G.exitasap      )
     199#define rotateasap     (G.rotateasap    )
     200#define reopenasap     (G.reopenasap    )
     201#define linecomplete   (G.linecomplete  )
     202#define tmaxflag       (G.tmaxflag      )
     203#define repl           (G.repl          )
     204#define replace        (G.replace       )
     205#define blocked_sigset (G.blocked_sigset)
     206#define fl_flag_0      (G.fl_flag_0     )
     207#define dirn           (G.dirn          )
     208#define INIT_G() do { \
     209    SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
     210    linemax = 1000; \
     211    /*buflen = 1024;*/ \
     212    linecomplete = 1; \
     213    replace = ""; \
     214} while (0)
     215
     216#define line bb_common_bufsiz1
     217
    86218
    87219#define FATAL "fatal: "
     
    90222#define INFO "info: "
    91223
    92 #define usage() bb_show_usage()
    93224static void fatalx(const char *m0)
    94225{
     
    114245static void pause1cannot(const char *m0)
    115246{
    116     bb_perror_msg(PAUSE"cannot %s", m0);
     247    bb_perror_msg(PAUSE"can't %s", m0);
    117248    sleep(3);
    118249}
    119250static void pause2cannot(const char *m0, const char *m1)
    120251{
    121     bb_perror_msg(PAUSE"cannot %s %s", m0, m1);
     252    bb_perror_msg(PAUSE"can't %s %s", m0, m1);
    122253    sleep(3);
    123254}
     
    131262}
    132263
     264static unsigned pmatch(const char *p, const char *s, unsigned len)
     265{
     266    for (;;) {
     267        char c = *p++;
     268        if (!c) return !len;
     269        switch (c) {
     270        case '*':
     271            c = *p;
     272            if (!c) return 1;
     273            for (;;) {
     274                if (!len) return 0;
     275                if (*s == c) break;
     276                ++s;
     277                --len;
     278            }
     279            continue;
     280        case '+':
     281            c = *p++;
     282            if (c != *s) return 0;
     283            for (;;) {
     284                if (!len) return 1;
     285                if (*s != c) break;
     286                ++s;
     287                --len;
     288            }
     289            continue;
     290            /*
     291        case '?':
     292            if (*p == '?') {
     293                if (*s != '?') return 0;
     294                ++p;
     295            }
     296            ++s; --len;
     297            continue;
     298            */
     299        default:
     300            if (!len) return 0;
     301            if (*s != c) return 0;
     302            ++s;
     303            --len;
     304            continue;
     305        }
     306    }
     307    return 0;
     308}
     309
    133310/*** ex fmt_ptime.[ch] ***/
    134311
     
    136313static void fmt_time_human_30nul(char *s)
    137314{
    138     struct tm *t;
     315    struct tm *ptm;
    139316    struct timeval tv;
    140317
    141318    gettimeofday(&tv, NULL);
    142     t = gmtime(&(tv.tv_sec));
     319    ptm = gmtime(&tv.tv_sec);
    143320    sprintf(s, "%04u-%02u-%02u_%02u:%02u:%02u.%06u000",
    144         (unsigned)(1900 + t->tm_year),
    145         (unsigned)(t->tm_mon + 1),
    146         (unsigned)(t->tm_mday),
    147         (unsigned)(t->tm_hour),
    148         (unsigned)(t->tm_min),
    149         (unsigned)(t->tm_sec),
     321        (unsigned)(1900 + ptm->tm_year),
     322        (unsigned)(ptm->tm_mon + 1),
     323        (unsigned)(ptm->tm_mday),
     324        (unsigned)(ptm->tm_hour),
     325        (unsigned)(ptm->tm_min),
     326        (unsigned)(ptm->tm_sec),
    150327        (unsigned)(tv.tv_usec)
    151328    );
     
    175352}
    176353
    177 static unsigned processorstart(struct logdir *ld)
    178 {
     354static void processorstart(struct logdir *ld)
     355{
     356    char sv_ch;
    179357    int pid;
    180358
    181     if (!ld->processor) return 0;
     359    if (!ld->processor) return;
    182360    if (ld->ppid) {
    183361        warnx("processor already running", ld->name);
    184         return 0;
    185     }
    186     while ((pid = fork()) == -1)
    187         pause2cannot("fork for processor", ld->name);
     362        return;
     363    }
     364
     365    /* vfork'ed child trashes this byte, save... */
     366    sv_ch = ld->fnsave[26];
     367
     368    while ((pid = vfork()) == -1)
     369        pause2cannot("vfork for processor", ld->name);
    188370    if (!pid) {
    189         char *prog[4];
    190371        int fd;
    191372
    192373        /* child */
    193         signal(SIGTERM, SIG_DFL);
    194         signal(SIGALRM, SIG_DFL);
    195         signal(SIGHUP, SIG_DFL);
     374        /* Non-ignored signals revert to SIG_DFL on exec anyway */
     375        /*bb_signals(0
     376            + (1 << SIGTERM)
     377            + (1 << SIGALRM)
     378            + (1 << SIGHUP)
     379            , SIG_DFL);*/
    196380        sig_unblock(SIGTERM);
    197381        sig_unblock(SIGALRM);
     
    202386        fd = xopen(ld->fnsave, O_RDONLY|O_NDELAY);
    203387        xmove_fd(fd, 0);
    204         ld->fnsave[26] = 't';
     388        ld->fnsave[26] = 't'; /* <- that's why we need sv_ch! */
    205389        fd = xopen(ld->fnsave, O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
    206390        xmove_fd(fd, 1);
    207         fd = open_read("state");
     391        fd = open("state", O_RDONLY|O_NDELAY);
    208392        if (fd == -1) {
    209393            if (errno != ENOENT)
    210                 bb_perror_msg_and_die(FATAL"cannot %s processor %s", "open state for", ld->name);
     394                bb_perror_msg_and_die(FATAL"can't %s processor %s", "open state for", ld->name);
    211395            close(xopen("state", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT));
    212396            fd = xopen("state", O_RDONLY|O_NDELAY);
     
    217401
    218402// getenv("SHELL")?
    219         prog[0] = (char*)"sh";
    220         prog[1] = (char*)"-c";
    221         prog[2] = ld->processor;
    222         prog[3] = NULL;
    223         execve("/bin/sh", prog, environ);
    224         bb_perror_msg_and_die(FATAL"cannot %s processor %s", "run", ld->name);
    225     }
     403        execl(DEFAULT_SHELL, DEFAULT_SHELL_SHORT_NAME, "-c", ld->processor, (char*) NULL);
     404        bb_perror_msg_and_die(FATAL"can't %s processor %s", "run", ld->name);
     405    }
     406    ld->fnsave[26] = sv_ch; /* ...restore */
    226407    ld->ppid = pid;
    227     return 1;
    228408}
    229409
     
    234414    if (ld->ppid) {
    235415        sig_unblock(SIGHUP);
    236         while (wait_pid(&wstat, ld->ppid) == -1)
     416        while (safe_waitpid(ld->ppid, &wstat, 0) == -1)
    237417            pause2cannot("wait for processor", ld->name);
    238418        sig_block(SIGHUP);
    239419        ld->ppid = 0;
    240420    }
    241     if (ld->fddir == -1) return 1;
     421    if (ld->fddir == -1)
     422        return 1;
    242423    while (fchdir(ld->fddir) == -1)
    243424        pause2cannot("change directory, want processor", ld->name);
    244     if (wait_exitcode(wstat) != 0) {
     425    if (WEXITSTATUS(wstat) != 0) {
    245426        warnx("processor failed, restart", ld->name);
    246427        ld->fnsave[26] = 't';
     
    262443    ld->fnsave[26] = 'u';
    263444    if (unlink(ld->fnsave) == -1)
    264         bb_error_msg(WARNING"cannot unlink: %s/%s", ld->name, ld->fnsave);
     445        bb_error_msg(WARNING"can't unlink: %s/%s", ld->name, ld->fnsave);
    265446    while (rename("newstate", "state") == -1)
    266447        pause2cannot("rename state", ld->name);
     
    287468            if (f->d_name[26] == 't') {
    288469                if (unlink(f->d_name) == -1)
    289                     warn2("cannot unlink processor leftover", f->d_name);
     470                    warn2("can't unlink processor leftover", f->d_name);
    290471            } else {
    291472                ++n;
     
    297478    }
    298479    if (errno)
    299         warn2("cannot read directory", ld->name);
     480        warn2("can't read directory", ld->name);
    300481    closedir(d);
    301482
     
    304485            bb_error_msg(INFO"delete: %s/%s", ld->name, oldest);
    305486        if ((*oldest == '@') && (unlink(oldest) == -1))
    306             warn2("cannot unlink oldest logfile", ld->name);
     487            warn2("can't unlink oldest logfile", ld->name);
    307488    }
    308489}
     
    359540        while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1)
    360541            pause2cannot("create new current", ld->name);
    361         /* we presume this cannot fail */
    362         ld->filecur = fdopen(ld->fdcur, "a"); ////
     542        while ((ld->filecur = fdopen(ld->fdcur, "a")) == NULL) ////
     543            pause2cannot("create new current", ld->name); /* very unlikely */
    363544        setvbuf(ld->filecur, NULL, _IOFBF, linelen); ////
    364         coe(ld->fdcur);
     545        close_on_exec_on(ld->fdcur);
    365546        ld->size = 0;
    366547        while (fchmod(ld->fdcur, 0644) == -1)
    367548            pause2cannot("set mode of current", ld->name);
     549
    368550        rmoldest(ld);
    369551        processorstart(ld);
     
    413595                        memcpy(oldest, f->d_name, 27);
    414596                }
    415             if (errno) warn2("cannot read directory, want remove old logfile",
     597            if (errno) warn2("can't read directory, want remove old logfile",
    416598                    ld->name);
    417599            closedir(d);
     
    423605                    errno = 0;
    424606                    if (unlink(oldest) == -1) {
    425                         warn2("cannot unlink oldest logfile", ld->name);
     607                        warn2("can't unlink oldest logfile", ld->name);
    426608                        errno = ENOSPC;
    427609                    }
     
    468650}
    469651
    470 static unsigned logdir_open(struct logdir *ld, const char *fn)
     652static NOINLINE unsigned logdir_open(struct logdir *ld, const char *fn)
    471653{
    472654    char buf[128];
     
    480662    ld->fddir = open(fn, O_RDONLY|O_NDELAY);
    481663    if (ld->fddir == -1) {
    482         warn2("cannot open log directory", (char*)fn);
     664        warn2("can't open log directory", (char*)fn);
    483665        return 0;
    484666    }
    485     coe(ld->fddir);
     667    close_on_exec_on(ld->fddir);
    486668    if (fchdir(ld->fddir) == -1) {
    487669        logdir_close(ld);
    488         warn2("cannot change directory", (char*)fn);
     670        warn2("can't change directory", (char*)fn);
    489671        return 0;
    490672    }
    491673    ld->fdlock = open("lock", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
    492674    if ((ld->fdlock == -1)
    493      || (lock_exnb(ld->fdlock) == -1)
     675     || (flock(ld->fdlock, LOCK_EX | LOCK_NB) == -1)
    494676    ) {
    495677        logdir_close(ld);
    496         warn2("cannot lock directory", (char*)fn);
     678        warn2("can't lock directory", (char*)fn);
    497679        while (fchdir(fdwdir) == -1)
    498680            pause1cannot("change to initial working directory");
    499681        return 0;
    500682    }
    501     coe(ld->fdlock);
     683    close_on_exec_on(ld->fdlock);
    502684
    503685    ld->size = 0;
     
    512694
    513695    /* read config */
    514     i = open_read_close("config", buf, sizeof(buf));
     696    i = open_read_close("config", buf, sizeof(buf) - 1);
    515697    if (i < 0 && errno != ENOENT)
    516698        bb_perror_msg(WARNING"%s/config", ld->name);
    517699    if (i > 0) {
     700        buf[i] = '\0';
    518701        if (verbose)
    519702            bb_error_msg(INFO"read: %s/config", ld->name);
     
    528711            case 'e':
    529712            case 'E':
     713                /* Filtering requires one-line buffering,
     714                 * resetting the "find newline" function
     715                 * accordingly */
     716                memRchr = memchr;
    530717                /* Add '\n'-terminated line to ld->inst */
    531718                while (1) {
    532                     int l = asprintf(&new, "%s%s\n", ld->inst ? : "", s);
     719                    int l = asprintf(&new, "%s%s\n", ld->inst ? ld->inst : "", s);
    533720                    if (l >= 0 && new)
    534721                        break;
     
    542729                    { "k", 1024 },
    543730                    { "m", 1024*1024 },
    544                     { }
     731                    { "", 0 }
    545732                };
    546733                ld->sizemax = xatou_sfx(&s[1], km_suffixes);
     
    548735            }
    549736            case 'n':
    550                 ld->nmax = xatoi_u(&s[1]);
     737                ld->nmax = xatoi_positive(&s[1]);
    551738                break;
    552739            case 'N':
    553                 ld->nmin = xatoi_u(&s[1]);
     740                ld->nmin = xatoi_positive(&s[1]);
    554741                break;
    555742            case 't': {
     
    558745                    { "h", 60*60 },
    559746                    /*{ "d", 24*60*60 },*/
    560                     { }
     747                    { "", 0 }
    561748                };
    562749                ld->rotate_period = xatou_sfx(&s[1], mh_suffixes);
     
    613800        if (errno != ENOENT) {
    614801            logdir_close(ld);
    615             warn2("cannot stat current", ld->name);
     802            warn2("can't stat current", ld->name);
    616803            while (fchdir(fdwdir) == -1)
    617804                pause1cannot("change to initial working directory");
     
    621808    while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1)
    622809        pause2cannot("open current", ld->name);
    623     /* we presume this cannot fail */
    624     ld->filecur = fdopen(ld->fdcur, "a"); ////
     810    while ((ld->filecur = fdopen(ld->fdcur, "a")) == NULL)
     811        pause2cannot("open current", ld->name); ////
    625812    setvbuf(ld->filecur, NULL, _IOFBF, linelen); ////
    626813
    627     coe(ld->fdcur);
     814    close_on_exec_on(ld->fdcur);
    628815    while (fchmod(ld->fdcur, 0644) == -1)
    629816        pause2cannot("set mode of current", ld->name);
     
    666853
    667854/* Used for reading stdin */
    668 static int buffer_pread(int fd, char *s, unsigned len)
     855static int buffer_pread(/*int fd, */char *s, unsigned len)
    669856{
    670857    unsigned now;
     
    672859    int i;
    673860
    674     input.fd = 0;
     861    input.fd = STDIN_FILENO;
    675862    input.events = POLLIN;
    676863
     
    701888        }
    702889
    703         sigprocmask(SIG_UNBLOCK, blocked_sigset, NULL);
     890        sigprocmask(SIG_UNBLOCK, &blocked_sigset, NULL);
    704891        i = nearest_rotate - now;
    705892        if (i > 1000000)
     
    708895            i = 1;
    709896        poll(&input, 1, i * 1000);
    710         sigprocmask(SIG_BLOCK, blocked_sigset, NULL);
    711 
    712         i = ndelay_read(fd, s, len);
     897        sigprocmask(SIG_BLOCK, &blocked_sigset, NULL);
     898
     899        i = ndelay_read(STDIN_FILENO, s, len);
    713900        if (i >= 0)
    714901            break;
     
    716903            continue;
    717904        if (errno != EAGAIN) {
    718             warn("cannot read standard input");
     905            warn("can't read standard input");
    719906            break;
    720907        }
     
    750937}
    751938
    752 static void sig_term_handler(int sig_no)
     939static void sig_term_handler(int sig_no UNUSED_PARAM)
    753940{
    754941    if (verbose)
     
    757944}
    758945
    759 static void sig_child_handler(int sig_no)
    760 {
    761     int pid, l;
     946static void sig_child_handler(int sig_no UNUSED_PARAM)
     947{
     948    pid_t pid;
     949    int l;
    762950
    763951    if (verbose)
    764952        bb_error_msg(INFO"sig%s received", "child");
    765     while ((pid = wait_nohang(&wstat)) > 0) {
     953    while ((pid = wait_any_nohang(&wstat)) > 0) {
    766954        for (l = 0; l < dirn; ++l) {
    767955            if (dir[l].ppid == pid) {
     
    774962}
    775963
    776 static void sig_alarm_handler(int sig_no)
     964static void sig_alarm_handler(int sig_no UNUSED_PARAM)
    777965{
    778966    if (verbose)
     
    781969}
    782970
    783 static void sig_hangup_handler(int sig_no)
     971static void sig_hangup_handler(int sig_no UNUSED_PARAM)
    784972{
    785973    if (verbose)
     
    8121000}
    8131001
    814 int svlogd_main(int argc, char **argv);
     1002int svlogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
    8151003int svlogd_main(int argc, char **argv)
    8161004{
    817     sigset_t ss;
    818     char *r,*l,*b;
     1005    char *r, *l, *b;
    8191006    ssize_t stdin_cnt = 0;
    8201007    int i;
    8211008    unsigned opt;
    8221009    unsigned timestamp = 0;
    823     void* (*memRchr)(const void *, int, size_t) = memchr;
    824 
    825 #define line bb_common_bufsiz1
     1010
     1011    INIT_G();
    8261012
    8271013    opt_complementary = "tt:vv";
     
    8301016    if (opt & 1) { // -r
    8311017        repl = r[0];
    832         if (!repl || r[1]) usage();
     1018        if (!repl || r[1])
     1019            bb_show_usage();
    8331020    }
    8341021    if (opt & 2) if (!repl) repl = '_'; // -R
    8351022    if (opt & 4) { // -l
    8361023        linemax = xatou_range(l, 0, BUFSIZ-26);
    837         if (linemax == 0) linemax = BUFSIZ-26;
    838         if (linemax < 256) linemax = 256;
     1024        if (linemax == 0)
     1025            linemax = BUFSIZ-26;
     1026        if (linemax < 256)
     1027            linemax = 256;
    8391028    }
    8401029    ////if (opt & 8) { // -b
    841     ////    buflen = xatoi_u(b);
     1030    ////    buflen = xatoi_positive(b);
    8421031    ////    if (buflen == 0) buflen = 1024;
    8431032    ////}
     
    8491038
    8501039    dirn = argc;
    851     if (dirn <= 0) usage();
    852     ////if (buflen <= linemax) usage();
     1040    if (dirn <= 0)
     1041        bb_show_usage();
     1042    ////if (buflen <= linemax) bb_show_usage();
    8531043    fdwdir = xopen(".", O_RDONLY|O_NDELAY);
    854     coe(fdwdir);
    855     dir = xzalloc(dirn * sizeof(struct logdir));
     1044    close_on_exec_on(fdwdir);
     1045    dir = xzalloc(dirn * sizeof(dir[0]));
    8561046    for (i = 0; i < dirn; ++i) {
    8571047        dir[i].fddir = -1;
     
    8671057    fl_flag_0 = fcntl(0, F_GETFL);
    8681058
    869     blocked_sigset = &ss;
    870     sigemptyset(&ss);
    871     sigaddset(&ss, SIGTERM);
    872     sigaddset(&ss, SIGCHLD);
    873     sigaddset(&ss, SIGALRM);
    874     sigaddset(&ss, SIGHUP);
    875     sigprocmask(SIG_BLOCK, &ss, NULL);
    876     sig_catch(SIGTERM, sig_term_handler);
    877     sig_catch(SIGCHLD, sig_child_handler);
    878     sig_catch(SIGALRM, sig_alarm_handler);
    879     sig_catch(SIGHUP, sig_hangup_handler);
    880 
    881     logdirs_reopen();
     1059    sigemptyset(&blocked_sigset);
     1060    sigaddset(&blocked_sigset, SIGTERM);
     1061    sigaddset(&blocked_sigset, SIGCHLD);
     1062    sigaddset(&blocked_sigset, SIGALRM);
     1063    sigaddset(&blocked_sigset, SIGHUP);
     1064    sigprocmask(SIG_BLOCK, &blocked_sigset, NULL);
     1065    bb_signals_recursive_norestart(1 << SIGTERM, sig_term_handler);
     1066    bb_signals_recursive_norestart(1 << SIGCHLD, sig_child_handler);
     1067    bb_signals_recursive_norestart(1 << SIGALRM, sig_alarm_handler);
     1068    bb_signals_recursive_norestart(1 << SIGHUP, sig_hangup_handler);
    8821069
    8831070    /* Without timestamps, we don't have to print each line
    8841071     * separately, so we can look for _last_ newline, not first,
    885      * thus batching writes */
    886     if (!timestamp)
    887         memRchr = memrchr;
     1072     * thus batching writes. If filtering is enabled in config,
     1073     * logdirs_reopen resets it to memchr.
     1074     */
     1075    memRchr = (timestamp ? memchr : memrchr);
     1076
     1077    logdirs_reopen();
    8881078
    8891079    setvbuf(stderr, NULL, _IOFBF, linelen);
     
    9101100            i = linemax - stdin_cnt; /* avail. bytes at tail */
    9111101            if (i >= 128) {
    912                 i = buffer_pread(0, lineptr + stdin_cnt, i);
     1102                i = buffer_pread(/*0, */lineptr + stdin_cnt, i);
    9131103                if (i <= 0) /* EOF or error on stdin */
    9141104                    exitasap = 1;
     
    9251115        linelen = stdin_cnt;
    9261116        if (np) {
    927  print_to_nl:       /* NB: starting from here lineptr may point
     1117 print_to_nl:
     1118            /* NB: starting from here lineptr may point
    9281119             * farther out into line[] */
    9291120            linelen = np - lineptr + 1;
     
    9531144        for (i = 0; i < dirn; ++i) {
    9541145            struct logdir *ld = &dir[i];
    955             if (ld->fddir == -1) continue;
     1146            if (ld->fddir == -1)
     1147                continue;
    9561148            if (ld->inst)
    9571149                logmatch(ld);
    958             if (ld->matcherr == 'e')
    959                 ////full_write(2, printptr, printlen);
    960                 fwrite(lineptr, 1, linelen, stderr);
    961             if (ld->match != '+') continue;
     1150            if (ld->matcherr == 'e') {
     1151                /* runit-1.8.0 compat: if timestamping, do it on stderr too */
     1152                ////full_write(STDERR_FILENO, printptr, printlen);
     1153                fwrite(printptr, 1, printlen, stderr);
     1154            }
     1155            if (ld->match != '+')
     1156                continue;
    9621157            buffer_pwrite(i, printptr, printlen);
    9631158        }
     
    9671162        while (ch != '\n') {
    9681163            /* lineptr is emptied now, safe to use as buffer */
    969             stdin_cnt = exitasap ? -1 : buffer_pread(0, lineptr, linemax);
     1164            stdin_cnt = exitasap ? -1 : buffer_pread(/*0, */lineptr, linemax);
    9701165            if (stdin_cnt <= 0) { /* EOF or error on stdin */
    9711166                exitasap = 1;
     
    9821177            /* linelen == no of chars incl. '\n' (or == stdin_cnt) */
    9831178            for (i = 0; i < dirn; ++i) {
    984                 if (dir[i].fddir == -1) continue;
    985                 if (dir[i].matcherr == 'e')
    986                     ////full_write(2, lineptr, linelen);
     1179                if (dir[i].fddir == -1)
     1180                    continue;
     1181                if (dir[i].matcherr == 'e') {
     1182                    ////full_write(STDERR_FILENO, lineptr, linelen);
    9871183                    fwrite(lineptr, 1, linelen, stderr);
    988                 if (dir[i].match != '+') continue;
     1184                }
     1185                if (dir[i].match != '+')
     1186                    continue;
    9891187                buffer_pwrite(i, lineptr, linelen);
    9901188            }
     
    10021200            memmove((timestamp ? line+26 : line), lineptr, stdin_cnt);
    10031201        }
    1004         fflush(NULL);////
     1202        fflush_all();////
    10051203    }
    10061204
     
    10081206        if (dir[i].ppid)
    10091207            while (!processorstop(&dir[i]))
    1010                 /* repeat */;
     1208                continue;
    10111209        logdir_close(&dir[i]);
    10121210    }
Note: See TracChangeset for help on using the changeset viewer.