2006-11-16 07:57:24 +05:30
|
|
|
/*
|
|
|
|
Copyright (c) 2001-2006, Gerrit Pape
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
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
|
|
|
|
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,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2008-03-02 18:23:15 +05:30
|
|
|
/* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
|
2006-11-16 07:57:24 +05:30
|
|
|
/* TODO: depends on runit_lib.c - review and reduce/eliminate */
|
|
|
|
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/file.h>
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2006-11-16 07:57:24 +05:30
|
|
|
#include "runit_lib.h"
|
|
|
|
|
2007-08-20 22:57:40 +05:30
|
|
|
#define LESS(a,b) ((int)((unsigned)(b) - (unsigned)(a)) > 0)
|
|
|
|
|
|
|
|
#define FMT_PTIME 30
|
|
|
|
|
2007-10-06 03:41:06 +05:30
|
|
|
struct logdir {
|
2007-02-03 06:23:43 +05:30
|
|
|
////char *btmp;
|
2006-11-16 07:57:24 +05:30
|
|
|
/* pattern list to match, in "aa\0bb\0\cc\0\0" form */
|
|
|
|
char *inst;
|
|
|
|
char *processor;
|
|
|
|
char *name;
|
|
|
|
unsigned size;
|
|
|
|
unsigned sizemax;
|
|
|
|
unsigned nmax;
|
|
|
|
unsigned nmin;
|
2007-08-20 22:57:40 +05:30
|
|
|
unsigned rotate_period;
|
2006-11-16 07:57:24 +05:30
|
|
|
int ppid;
|
|
|
|
int fddir;
|
|
|
|
int fdcur;
|
2007-02-03 06:23:43 +05:30
|
|
|
FILE* filecur; ////
|
2006-11-16 07:57:24 +05:30
|
|
|
int fdlock;
|
2007-08-20 22:57:40 +05:30
|
|
|
unsigned next_rotate;
|
2006-11-16 07:57:24 +05:30
|
|
|
char fnsave[FMT_PTIME];
|
|
|
|
char match;
|
|
|
|
char matcherr;
|
2007-10-06 03:41:06 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct globals {
|
|
|
|
struct logdir *dir;
|
|
|
|
unsigned verbose;
|
|
|
|
int linemax;
|
|
|
|
////int buflen;
|
|
|
|
int linelen;
|
|
|
|
|
|
|
|
int fdwdir;
|
|
|
|
char **fndir;
|
|
|
|
int wstat;
|
|
|
|
unsigned nearest_rotate;
|
|
|
|
|
|
|
|
smallint exitasap;
|
|
|
|
smallint rotateasap;
|
|
|
|
smallint reopenasap;
|
|
|
|
smallint linecomplete;
|
|
|
|
smallint tmaxflag;
|
|
|
|
|
|
|
|
char repl;
|
|
|
|
const char *replace;
|
|
|
|
int fl_flag_0;
|
|
|
|
unsigned dirn;
|
|
|
|
|
|
|
|
sigset_t blocked_sigset;
|
|
|
|
};
|
|
|
|
#define G (*(struct globals*)ptr_to_globals)
|
|
|
|
#define dir (G.dir )
|
|
|
|
#define verbose (G.verbose )
|
|
|
|
#define linemax (G.linemax )
|
|
|
|
#define buflen (G.buflen )
|
|
|
|
#define linelen (G.linelen )
|
|
|
|
#define fndir (G.fndir )
|
|
|
|
#define fdwdir (G.fdwdir )
|
|
|
|
#define wstat (G.wstat )
|
|
|
|
#define nearest_rotate (G.nearest_rotate)
|
|
|
|
#define exitasap (G.exitasap )
|
|
|
|
#define rotateasap (G.rotateasap )
|
|
|
|
#define reopenasap (G.reopenasap )
|
|
|
|
#define linecomplete (G.linecomplete )
|
|
|
|
#define tmaxflag (G.tmaxflag )
|
|
|
|
#define repl (G.repl )
|
|
|
|
#define replace (G.replace )
|
|
|
|
#define blocked_sigset (G.blocked_sigset)
|
|
|
|
#define fl_flag_0 (G.fl_flag_0 )
|
|
|
|
#define dirn (G.dirn )
|
|
|
|
#define INIT_G() do { \
|
2008-02-28 00:11:59 +05:30
|
|
|
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
2007-10-06 03:41:06 +05:30
|
|
|
linemax = 1000; \
|
|
|
|
/*buflen = 1024;*/ \
|
|
|
|
linecomplete = 1; \
|
|
|
|
replace = ""; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define line bb_common_bufsiz1
|
|
|
|
|
2006-11-16 07:57:24 +05:30
|
|
|
|
|
|
|
#define FATAL "fatal: "
|
|
|
|
#define WARNING "warning: "
|
|
|
|
#define PAUSE "pausing: "
|
|
|
|
#define INFO "info: "
|
|
|
|
|
|
|
|
#define usage() bb_show_usage()
|
2007-01-30 04:21:58 +05:30
|
|
|
static void fatalx(const char *m0)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
|
|
|
bb_error_msg_and_die(FATAL"%s", m0);
|
|
|
|
}
|
2007-04-17 04:02:04 +05:30
|
|
|
static void warn(const char *m0)
|
|
|
|
{
|
2006-11-16 07:57:24 +05:30
|
|
|
bb_perror_msg(WARNING"%s", m0);
|
|
|
|
}
|
2007-01-30 04:21:58 +05:30
|
|
|
static void warn2(const char *m0, const char *m1)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
|
|
|
bb_perror_msg(WARNING"%s: %s", m0, m1);
|
|
|
|
}
|
2007-01-30 04:21:58 +05:30
|
|
|
static void warnx(const char *m0, const char *m1)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
|
|
|
bb_error_msg(WARNING"%s: %s", m0, m1);
|
|
|
|
}
|
|
|
|
static void pause_nomem(void)
|
|
|
|
{
|
2007-01-28 03:51:52 +05:30
|
|
|
bb_error_msg(PAUSE"out of memory");
|
|
|
|
sleep(3);
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
2007-01-30 04:21:58 +05:30
|
|
|
static void pause1cannot(const char *m0)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
2008-07-21 19:16:54 +05:30
|
|
|
bb_perror_msg(PAUSE"can't %s", m0);
|
2007-01-28 03:51:52 +05:30
|
|
|
sleep(3);
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
2007-01-30 04:21:58 +05:30
|
|
|
static void pause2cannot(const char *m0, const char *m1)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
2008-07-21 19:16:54 +05:30
|
|
|
bb_perror_msg(PAUSE"can't %s %s", m0, m1);
|
2006-11-16 07:57:24 +05:30
|
|
|
sleep(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* wstrdup(const char *str)
|
|
|
|
{
|
|
|
|
char *s;
|
2007-01-28 03:51:52 +05:30
|
|
|
while (!(s = strdup(str)))
|
|
|
|
pause_nomem();
|
2006-11-16 07:57:24 +05:30
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-08-20 22:57:40 +05:30
|
|
|
/*** ex fmt_ptime.[ch] ***/
|
|
|
|
|
|
|
|
/* NUL terminated */
|
|
|
|
static void fmt_time_human_30nul(char *s)
|
|
|
|
{
|
|
|
|
struct tm *t;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
t = 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)(tv.tv_usec)
|
|
|
|
);
|
|
|
|
/* 4+1 + 2+1 + 2+1 + 2+1 + 2+1 + 2+1 + 9 = */
|
|
|
|
/* 5 + 3 + 3 + 3 + 3 + 3 + 9 = */
|
|
|
|
/* 20 (up to '.' inclusive) + 9 (not including '\0') */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOT terminated! */
|
|
|
|
static void fmt_time_bernstein_25(char *s)
|
|
|
|
{
|
|
|
|
uint32_t pack[3];
|
|
|
|
struct timeval tv;
|
|
|
|
unsigned sec_hi;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
sec_hi = (0x400000000000000aULL + tv.tv_sec) >> 32;
|
|
|
|
tv.tv_sec = (time_t)(0x400000000000000aULL) + tv.tv_sec;
|
|
|
|
tv.tv_usec *= 1000;
|
|
|
|
/* Network order is big-endian: most significant byte first.
|
|
|
|
* This is exactly what we want here */
|
|
|
|
pack[0] = htonl(sec_hi);
|
|
|
|
pack[1] = htonl(tv.tv_sec);
|
|
|
|
pack[2] = htonl(tv.tv_usec);
|
|
|
|
*s++ = '@';
|
|
|
|
bin2hex(s, (char*)pack, 12);
|
|
|
|
}
|
|
|
|
|
2008-03-17 14:43:22 +05:30
|
|
|
static void processorstart(struct logdir *ld)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
2008-03-17 14:43:22 +05:30
|
|
|
char sv_ch;
|
2006-11-16 07:57:24 +05:30
|
|
|
int pid;
|
|
|
|
|
2008-03-17 14:43:22 +05:30
|
|
|
if (!ld->processor) return;
|
2006-11-16 07:57:24 +05:30
|
|
|
if (ld->ppid) {
|
|
|
|
warnx("processor already running", ld->name);
|
2008-03-17 14:43:22 +05:30
|
|
|
return;
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
2008-03-17 14:43:22 +05:30
|
|
|
|
|
|
|
/* vfork'ed child trashes this byte, save... */
|
|
|
|
sv_ch = ld->fnsave[26];
|
|
|
|
|
|
|
|
while ((pid = vfork()) == -1)
|
|
|
|
pause2cannot("vfork for processor", ld->name);
|
2006-11-16 07:57:24 +05:30
|
|
|
if (!pid) {
|
|
|
|
char *prog[4];
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* child */
|
2008-11-09 05:45:11 +05:30
|
|
|
/* Non-ignored signals revert to SIG_DFL on exec anyway */
|
|
|
|
/*bb_signals(0
|
2008-02-17 04:28:56 +05:30
|
|
|
+ (1 << SIGTERM)
|
|
|
|
+ (1 << SIGALRM)
|
|
|
|
+ (1 << SIGHUP)
|
2008-11-09 05:45:11 +05:30
|
|
|
, SIG_DFL);*/
|
2007-01-28 03:51:52 +05:30
|
|
|
sig_unblock(SIGTERM);
|
|
|
|
sig_unblock(SIGALRM);
|
|
|
|
sig_unblock(SIGHUP);
|
2007-01-11 22:50:00 +05:30
|
|
|
|
2006-11-16 07:57:24 +05:30
|
|
|
if (verbose)
|
|
|
|
bb_error_msg(INFO"processing: %s/%s", ld->name, ld->fnsave);
|
|
|
|
fd = xopen(ld->fnsave, O_RDONLY|O_NDELAY);
|
2007-03-26 04:51:05 +05:30
|
|
|
xmove_fd(fd, 0);
|
2008-03-17 14:43:22 +05:30
|
|
|
ld->fnsave[26] = 't'; /* <- that's why we need sv_ch! */
|
2006-11-26 21:15:17 +05:30
|
|
|
fd = xopen(ld->fnsave, O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
|
2007-03-26 04:51:05 +05:30
|
|
|
xmove_fd(fd, 1);
|
2006-11-16 07:57:24 +05:30
|
|
|
fd = open_read("state");
|
|
|
|
if (fd == -1) {
|
|
|
|
if (errno != ENOENT)
|
2008-07-21 19:16:54 +05:30
|
|
|
bb_perror_msg_and_die(FATAL"can't %s processor %s", "open state for", ld->name);
|
2006-11-26 21:15:17 +05:30
|
|
|
close(xopen("state", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT));
|
2006-11-16 07:57:24 +05:30
|
|
|
fd = xopen("state", O_RDONLY|O_NDELAY);
|
|
|
|
}
|
2007-03-26 04:51:05 +05:30
|
|
|
xmove_fd(fd, 4);
|
2006-11-26 21:15:17 +05:30
|
|
|
fd = xopen("newstate", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
|
2007-03-26 04:51:05 +05:30
|
|
|
xmove_fd(fd, 5);
|
2006-11-16 07:57:24 +05:30
|
|
|
|
2007-01-28 03:51:52 +05:30
|
|
|
// getenv("SHELL")?
|
2007-01-30 04:21:58 +05:30
|
|
|
prog[0] = (char*)"sh";
|
|
|
|
prog[1] = (char*)"-c";
|
2006-11-16 07:57:24 +05:30
|
|
|
prog[2] = ld->processor;
|
2007-03-26 04:51:05 +05:30
|
|
|
prog[3] = NULL;
|
2008-01-29 04:15:43 +05:30
|
|
|
execv("/bin/sh", prog);
|
2008-07-21 19:16:54 +05:30
|
|
|
bb_perror_msg_and_die(FATAL"can't %s processor %s", "run", ld->name);
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
2008-03-17 14:43:22 +05:30
|
|
|
ld->fnsave[26] = sv_ch; /* ...restore */
|
2006-11-16 07:57:24 +05:30
|
|
|
ld->ppid = pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned processorstop(struct logdir *ld)
|
|
|
|
{
|
|
|
|
char f[28];
|
|
|
|
|
|
|
|
if (ld->ppid) {
|
2007-01-28 03:51:52 +05:30
|
|
|
sig_unblock(SIGHUP);
|
2008-01-03 01:25:04 +05:30
|
|
|
while (safe_waitpid(ld->ppid, &wstat, 0) == -1)
|
2006-11-16 07:57:24 +05:30
|
|
|
pause2cannot("wait for processor", ld->name);
|
2007-01-28 03:51:52 +05:30
|
|
|
sig_block(SIGHUP);
|
2006-11-16 07:57:24 +05:30
|
|
|
ld->ppid = 0;
|
|
|
|
}
|
|
|
|
if (ld->fddir == -1) return 1;
|
|
|
|
while (fchdir(ld->fddir) == -1)
|
|
|
|
pause2cannot("change directory, want processor", ld->name);
|
|
|
|
if (wait_exitcode(wstat) != 0) {
|
|
|
|
warnx("processor failed, restart", ld->name);
|
|
|
|
ld->fnsave[26] = 't';
|
|
|
|
unlink(ld->fnsave);
|
|
|
|
ld->fnsave[26] = 'u';
|
|
|
|
processorstart(ld);
|
|
|
|
while (fchdir(fdwdir) == -1)
|
|
|
|
pause1cannot("change to initial working directory");
|
|
|
|
return ld->processor ? 0 : 1;
|
|
|
|
}
|
|
|
|
ld->fnsave[26] = 't';
|
|
|
|
memcpy(f, ld->fnsave, 26);
|
|
|
|
f[26] = 's';
|
|
|
|
f[27] = '\0';
|
|
|
|
while (rename(ld->fnsave, f) == -1)
|
|
|
|
pause2cannot("rename processed", ld->name);
|
|
|
|
while (chmod(f, 0744) == -1)
|
|
|
|
pause2cannot("set mode of processed", ld->name);
|
|
|
|
ld->fnsave[26] = 'u';
|
|
|
|
if (unlink(ld->fnsave) == -1)
|
2008-07-21 19:16:54 +05:30
|
|
|
bb_error_msg(WARNING"can't unlink: %s/%s", ld->name, ld->fnsave);
|
2006-11-16 07:57:24 +05:30
|
|
|
while (rename("newstate", "state") == -1)
|
|
|
|
pause2cannot("rename state", ld->name);
|
2007-01-28 03:51:52 +05:30
|
|
|
if (verbose)
|
|
|
|
bb_error_msg(INFO"processed: %s/%s", ld->name, f);
|
2006-11-16 07:57:24 +05:30
|
|
|
while (fchdir(fdwdir) == -1)
|
|
|
|
pause1cannot("change to initial working directory");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rmoldest(struct logdir *ld)
|
|
|
|
{
|
|
|
|
DIR *d;
|
|
|
|
struct dirent *f;
|
|
|
|
char oldest[FMT_PTIME];
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
oldest[0] = 'A'; oldest[1] = oldest[27] = 0;
|
|
|
|
while (!(d = opendir(".")))
|
|
|
|
pause2cannot("open directory, want rotate", ld->name);
|
|
|
|
errno = 0;
|
|
|
|
while ((f = readdir(d))) {
|
|
|
|
if ((f->d_name[0] == '@') && (strlen(f->d_name) == 27)) {
|
|
|
|
if (f->d_name[26] == 't') {
|
|
|
|
if (unlink(f->d_name) == -1)
|
2008-07-21 19:16:54 +05:30
|
|
|
warn2("can't unlink processor leftover", f->d_name);
|
2006-11-16 07:57:24 +05:30
|
|
|
} else {
|
|
|
|
++n;
|
|
|
|
if (strcmp(f->d_name, oldest) < 0)
|
|
|
|
memcpy(oldest, f->d_name, 27);
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
}
|
|
|
|
}
|
2007-01-28 03:51:52 +05:30
|
|
|
if (errno)
|
2008-07-21 19:16:54 +05:30
|
|
|
warn2("can't read directory", ld->name);
|
2006-11-16 07:57:24 +05:30
|
|
|
closedir(d);
|
|
|
|
|
|
|
|
if (ld->nmax && (n > ld->nmax)) {
|
2007-01-28 03:51:52 +05:30
|
|
|
if (verbose)
|
|
|
|
bb_error_msg(INFO"delete: %s/%s", ld->name, oldest);
|
2006-11-16 07:57:24 +05:30
|
|
|
if ((*oldest == '@') && (unlink(oldest) == -1))
|
2008-07-21 19:16:54 +05:30
|
|
|
warn2("can't unlink oldest logfile", ld->name);
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned rotate(struct logdir *ld)
|
|
|
|
{
|
|
|
|
struct stat st;
|
2007-08-20 22:57:40 +05:30
|
|
|
unsigned now;
|
2006-11-16 07:57:24 +05:30
|
|
|
|
|
|
|
if (ld->fddir == -1) {
|
2007-08-20 22:57:40 +05:30
|
|
|
ld->rotate_period = 0;
|
2006-11-16 07:57:24 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ld->ppid)
|
2006-12-26 16:12:51 +05:30
|
|
|
while (!processorstop(ld))
|
2007-08-20 22:57:40 +05:30
|
|
|
continue;
|
2006-11-16 07:57:24 +05:30
|
|
|
|
|
|
|
while (fchdir(ld->fddir) == -1)
|
|
|
|
pause2cannot("change directory, want rotate", ld->name);
|
|
|
|
|
|
|
|
/* create new filename */
|
|
|
|
ld->fnsave[25] = '.';
|
|
|
|
ld->fnsave[26] = 's';
|
|
|
|
if (ld->processor)
|
|
|
|
ld->fnsave[26] = 'u';
|
|
|
|
ld->fnsave[27] = '\0';
|
|
|
|
do {
|
2007-08-20 22:57:40 +05:30
|
|
|
fmt_time_bernstein_25(ld->fnsave);
|
2006-11-16 07:57:24 +05:30
|
|
|
errno = 0;
|
2007-01-28 03:51:52 +05:30
|
|
|
stat(ld->fnsave, &st);
|
|
|
|
} while (errno != ENOENT);
|
2006-11-16 07:57:24 +05:30
|
|
|
|
2007-08-20 22:57:40 +05:30
|
|
|
now = monotonic_sec();
|
|
|
|
if (ld->rotate_period && LESS(ld->next_rotate, now)) {
|
|
|
|
ld->next_rotate = now + ld->rotate_period;
|
|
|
|
if (LESS(ld->next_rotate, nearest_rotate))
|
|
|
|
nearest_rotate = ld->next_rotate;
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (ld->size > 0) {
|
2007-02-03 06:23:43 +05:30
|
|
|
while (fflush(ld->filecur) || fsync(ld->fdcur) == -1)
|
2006-11-16 07:57:24 +05:30
|
|
|
pause2cannot("fsync current logfile", ld->name);
|
|
|
|
while (fchmod(ld->fdcur, 0744) == -1)
|
|
|
|
pause2cannot("set mode of current", ld->name);
|
2007-02-03 06:23:43 +05:30
|
|
|
////close(ld->fdcur);
|
|
|
|
fclose(ld->filecur);
|
|
|
|
|
2006-11-16 07:57:24 +05:30
|
|
|
if (verbose) {
|
|
|
|
bb_error_msg(INFO"rename: %s/current %s %u", ld->name,
|
|
|
|
ld->fnsave, ld->size);
|
|
|
|
}
|
|
|
|
while (rename("current", ld->fnsave) == -1)
|
|
|
|
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);
|
2007-02-03 06:23:43 +05:30
|
|
|
/* we presume this cannot fail */
|
|
|
|
ld->filecur = fdopen(ld->fdcur, "a"); ////
|
|
|
|
setvbuf(ld->filecur, NULL, _IOFBF, linelen); ////
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(ld->fdcur);
|
2006-11-16 07:57:24 +05:30
|
|
|
ld->size = 0;
|
|
|
|
while (fchmod(ld->fdcur, 0644) == -1)
|
|
|
|
pause2cannot("set mode of current", ld->name);
|
|
|
|
rmoldest(ld);
|
|
|
|
processorstart(ld);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fchdir(fdwdir) == -1)
|
|
|
|
pause1cannot("change to initial working directory");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int buffer_pwrite(int n, char *s, unsigned len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct logdir *ld = &dir[n];
|
|
|
|
|
|
|
|
if (ld->sizemax) {
|
|
|
|
if (ld->size >= ld->sizemax)
|
|
|
|
rotate(ld);
|
|
|
|
if (len > (ld->sizemax - ld->size))
|
|
|
|
len = ld->sizemax - ld->size;
|
|
|
|
}
|
2007-02-03 06:23:43 +05:30
|
|
|
while (1) {
|
|
|
|
////i = full_write(ld->fdcur, s, len);
|
|
|
|
////if (i != -1) break;
|
|
|
|
i = fwrite(s, 1, len, ld->filecur);
|
|
|
|
if (i == len) break;
|
|
|
|
|
2006-11-16 07:57:24 +05:30
|
|
|
if ((errno == ENOSPC) && (ld->nmin < ld->nmax)) {
|
|
|
|
DIR *d;
|
|
|
|
struct dirent *f;
|
|
|
|
char oldest[FMT_PTIME];
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
while (fchdir(ld->fddir) == -1)
|
|
|
|
pause2cannot("change directory, want remove old logfile",
|
|
|
|
ld->name);
|
|
|
|
oldest[0] = 'A';
|
|
|
|
oldest[1] = oldest[27] = '\0';
|
|
|
|
while (!(d = opendir(".")))
|
|
|
|
pause2cannot("open directory, want remove old logfile",
|
|
|
|
ld->name);
|
|
|
|
errno = 0;
|
|
|
|
while ((f = readdir(d)))
|
|
|
|
if ((f->d_name[0] == '@') && (strlen(f->d_name) == 27)) {
|
|
|
|
++j;
|
|
|
|
if (strcmp(f->d_name, oldest) < 0)
|
|
|
|
memcpy(oldest, f->d_name, 27);
|
|
|
|
}
|
2008-07-21 19:16:54 +05:30
|
|
|
if (errno) warn2("can't read directory, want remove old logfile",
|
2006-11-16 07:57:24 +05:30
|
|
|
ld->name);
|
|
|
|
closedir(d);
|
|
|
|
errno = ENOSPC;
|
|
|
|
if (j > ld->nmin) {
|
|
|
|
if (*oldest == '@') {
|
|
|
|
bb_error_msg(WARNING"out of disk space, delete: %s/%s",
|
|
|
|
ld->name, oldest);
|
|
|
|
errno = 0;
|
|
|
|
if (unlink(oldest) == -1) {
|
2008-07-21 19:16:54 +05:30
|
|
|
warn2("can't unlink oldest logfile", ld->name);
|
2006-11-16 07:57:24 +05:30
|
|
|
errno = ENOSPC;
|
|
|
|
}
|
|
|
|
while (fchdir(fdwdir) == -1)
|
|
|
|
pause1cannot("change to initial working directory");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-01-28 03:54:59 +05:30
|
|
|
if (errno)
|
|
|
|
pause2cannot("write to current", ld->name);
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
ld->size += i;
|
|
|
|
if (ld->sizemax)
|
|
|
|
if (s[i-1] == '\n')
|
|
|
|
if (ld->size >= (ld->sizemax - linemax))
|
|
|
|
rotate(ld);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void logdir_close(struct logdir *ld)
|
|
|
|
{
|
|
|
|
if (ld->fddir == -1)
|
|
|
|
return;
|
|
|
|
if (verbose)
|
|
|
|
bb_error_msg(INFO"close: %s", ld->name);
|
|
|
|
close(ld->fddir);
|
|
|
|
ld->fddir = -1;
|
|
|
|
if (ld->fdcur == -1)
|
|
|
|
return; /* impossible */
|
2007-02-03 06:23:43 +05:30
|
|
|
while (fflush(ld->filecur) || fsync(ld->fdcur) == -1)
|
2006-11-16 07:57:24 +05:30
|
|
|
pause2cannot("fsync current logfile", ld->name);
|
|
|
|
while (fchmod(ld->fdcur, 0744) == -1)
|
|
|
|
pause2cannot("set mode of current", ld->name);
|
2007-02-03 06:23:43 +05:30
|
|
|
////close(ld->fdcur);
|
|
|
|
fclose(ld->filecur);
|
2006-11-16 07:57:24 +05:30
|
|
|
ld->fdcur = -1;
|
|
|
|
if (ld->fdlock == -1)
|
|
|
|
return; /* impossible */
|
|
|
|
close(ld->fdlock);
|
|
|
|
ld->fdlock = -1;
|
|
|
|
free(ld->processor);
|
|
|
|
ld->processor = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned logdir_open(struct logdir *ld, const char *fn)
|
|
|
|
{
|
|
|
|
char buf[128];
|
2007-08-20 22:57:40 +05:30
|
|
|
unsigned now;
|
2006-11-16 07:57:24 +05:30
|
|
|
char *new, *s, *np;
|
|
|
|
int i;
|
|
|
|
struct stat st;
|
|
|
|
|
2007-08-20 22:57:40 +05:30
|
|
|
now = monotonic_sec();
|
|
|
|
|
2006-11-16 07:57:24 +05:30
|
|
|
ld->fddir = open(fn, O_RDONLY|O_NDELAY);
|
|
|
|
if (ld->fddir == -1) {
|
2008-07-21 19:16:54 +05:30
|
|
|
warn2("can't open log directory", (char*)fn);
|
2006-11-16 07:57:24 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(ld->fddir);
|
2006-11-16 07:57:24 +05:30
|
|
|
if (fchdir(ld->fddir) == -1) {
|
|
|
|
logdir_close(ld);
|
2008-07-21 19:16:54 +05:30
|
|
|
warn2("can't change directory", (char*)fn);
|
2006-11-16 07:57:24 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ld->fdlock = open("lock", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
|
|
|
|
if ((ld->fdlock == -1)
|
|
|
|
|| (lock_exnb(ld->fdlock) == -1)
|
|
|
|
) {
|
|
|
|
logdir_close(ld);
|
2008-07-21 19:16:54 +05:30
|
|
|
warn2("can't lock directory", (char*)fn);
|
2006-11-16 07:57:24 +05:30
|
|
|
while (fchdir(fdwdir) == -1)
|
|
|
|
pause1cannot("change to initial working directory");
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(ld->fdlock);
|
2006-11-16 07:57:24 +05:30
|
|
|
|
|
|
|
ld->size = 0;
|
|
|
|
ld->sizemax = 1000000;
|
|
|
|
ld->nmax = ld->nmin = 10;
|
2007-08-20 22:57:40 +05:30
|
|
|
ld->rotate_period = 0;
|
2006-11-16 07:57:24 +05:30
|
|
|
ld->name = (char*)fn;
|
|
|
|
ld->ppid = 0;
|
|
|
|
ld->match = '+';
|
|
|
|
free(ld->inst); ld->inst = NULL;
|
|
|
|
free(ld->processor); ld->processor = NULL;
|
|
|
|
|
|
|
|
/* read config */
|
|
|
|
i = open_read_close("config", buf, sizeof(buf));
|
2007-08-03 16:28:12 +05:30
|
|
|
if (i < 0 && errno != ENOENT)
|
2007-08-21 16:48:25 +05:30
|
|
|
bb_perror_msg(WARNING"%s/config", ld->name);
|
2006-11-16 07:57:24 +05:30
|
|
|
if (i > 0) {
|
2007-08-20 22:57:40 +05:30
|
|
|
if (verbose)
|
|
|
|
bb_error_msg(INFO"read: %s/config", ld->name);
|
2006-11-16 07:57:24 +05:30
|
|
|
s = buf;
|
|
|
|
while (s) {
|
|
|
|
np = strchr(s, '\n');
|
2007-08-21 16:48:25 +05:30
|
|
|
if (np)
|
|
|
|
*np++ = '\0';
|
2006-11-16 07:57:24 +05:30
|
|
|
switch (s[0]) {
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
2007-08-21 16:48:25 +05:30
|
|
|
/* Add '\n'-terminated line to ld->inst */
|
2006-11-16 07:57:24 +05:30
|
|
|
while (1) {
|
2007-08-21 16:48:25 +05:30
|
|
|
int l = asprintf(&new, "%s%s\n", ld->inst ? : "", s);
|
|
|
|
if (l >= 0 && new)
|
|
|
|
break;
|
2006-11-16 07:57:24 +05:30
|
|
|
pause_nomem();
|
|
|
|
}
|
|
|
|
free(ld->inst);
|
|
|
|
ld->inst = new;
|
|
|
|
break;
|
|
|
|
case 's': {
|
|
|
|
static const struct suffix_mult km_suffixes[] = {
|
2007-07-27 20:36:25 +05:30
|
|
|
{ "k", 1024 },
|
|
|
|
{ "m", 1024*1024 },
|
|
|
|
{ }
|
2006-11-16 07:57:24 +05:30
|
|
|
};
|
|
|
|
ld->sizemax = xatou_sfx(&s[1], km_suffixes);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'n':
|
|
|
|
ld->nmax = xatoi_u(&s[1]);
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
ld->nmin = xatoi_u(&s[1]);
|
|
|
|
break;
|
|
|
|
case 't': {
|
|
|
|
static const struct suffix_mult mh_suffixes[] = {
|
2007-07-27 20:36:25 +05:30
|
|
|
{ "m", 60 },
|
|
|
|
{ "h", 60*60 },
|
|
|
|
/*{ "d", 24*60*60 },*/
|
|
|
|
{ }
|
2006-11-16 07:57:24 +05:30
|
|
|
};
|
2007-08-20 22:57:40 +05:30
|
|
|
ld->rotate_period = xatou_sfx(&s[1], mh_suffixes);
|
|
|
|
if (ld->rotate_period) {
|
|
|
|
ld->next_rotate = now + ld->rotate_period;
|
|
|
|
if (!tmaxflag || LESS(ld->next_rotate, nearest_rotate))
|
|
|
|
nearest_rotate = ld->next_rotate;
|
2006-11-16 07:57:24 +05:30
|
|
|
tmaxflag = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '!':
|
|
|
|
if (s[1]) {
|
|
|
|
free(ld->processor);
|
|
|
|
ld->processor = wstrdup(s);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s = np;
|
|
|
|
}
|
|
|
|
/* Convert "aa\nbb\ncc\n\0" to "aa\0bb\0cc\0\0" */
|
|
|
|
s = ld->inst;
|
|
|
|
while (s) {
|
|
|
|
np = strchr(s, '\n');
|
2007-08-21 16:48:25 +05:30
|
|
|
if (np)
|
|
|
|
*np++ = '\0';
|
2006-11-16 07:57:24 +05:30
|
|
|
s = np;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open current */
|
|
|
|
i = stat("current", &st);
|
|
|
|
if (i != -1) {
|
2007-08-21 16:48:25 +05:30
|
|
|
if (st.st_size && !(st.st_mode & S_IXUSR)) {
|
2006-11-16 07:57:24 +05:30
|
|
|
ld->fnsave[25] = '.';
|
|
|
|
ld->fnsave[26] = 'u';
|
|
|
|
ld->fnsave[27] = '\0';
|
|
|
|
do {
|
2007-08-20 22:57:40 +05:30
|
|
|
fmt_time_bernstein_25(ld->fnsave);
|
2006-11-16 07:57:24 +05:30
|
|
|
errno = 0;
|
2007-01-28 03:51:52 +05:30
|
|
|
stat(ld->fnsave, &st);
|
|
|
|
} while (errno != ENOENT);
|
2006-11-16 07:57:24 +05:30
|
|
|
while (rename("current", ld->fnsave) == -1)
|
|
|
|
pause2cannot("rename current", ld->name);
|
|
|
|
rmoldest(ld);
|
|
|
|
i = -1;
|
|
|
|
} else {
|
2007-08-21 16:48:25 +05:30
|
|
|
/* st.st_size can be not just bigger, but WIDER!
|
|
|
|
* This code is safe: if st.st_size > 4GB, we select
|
|
|
|
* ld->sizemax (because it's "unsigned") */
|
2006-11-16 07:57:24 +05:30
|
|
|
ld->size = (st.st_size > ld->sizemax) ? ld->sizemax : st.st_size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
logdir_close(ld);
|
2008-07-21 19:16:54 +05:30
|
|
|
warn2("can't stat current", ld->name);
|
2006-11-16 07:57:24 +05:30
|
|
|
while (fchdir(fdwdir) == -1)
|
|
|
|
pause1cannot("change to initial working directory");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1)
|
|
|
|
pause2cannot("open current", ld->name);
|
2007-02-03 06:23:43 +05:30
|
|
|
/* we presume this cannot fail */
|
|
|
|
ld->filecur = fdopen(ld->fdcur, "a"); ////
|
|
|
|
setvbuf(ld->filecur, NULL, _IOFBF, linelen); ////
|
|
|
|
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(ld->fdcur);
|
2006-11-16 07:57:24 +05:30
|
|
|
while (fchmod(ld->fdcur, 0644) == -1)
|
|
|
|
pause2cannot("set mode of current", ld->name);
|
2007-01-11 22:50:00 +05:30
|
|
|
|
2006-11-16 07:57:24 +05:30
|
|
|
if (verbose) {
|
|
|
|
if (i == 0) bb_error_msg(INFO"append: %s/current", ld->name);
|
|
|
|
else bb_error_msg(INFO"new: %s/current", ld->name);
|
|
|
|
}
|
2007-01-11 22:50:00 +05:30
|
|
|
|
2006-11-16 07:57:24 +05:30
|
|
|
while (fchdir(fdwdir) == -1)
|
|
|
|
pause1cannot("change to initial working directory");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void logdirs_reopen(void)
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
int ok = 0;
|
|
|
|
|
|
|
|
tmaxflag = 0;
|
|
|
|
for (l = 0; l < dirn; ++l) {
|
2007-01-11 22:50:00 +05:30
|
|
|
logdir_close(&dir[l]);
|
2007-08-20 22:57:40 +05:30
|
|
|
if (logdir_open(&dir[l], fndir[l]))
|
|
|
|
ok = 1;
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
2007-08-20 22:57:40 +05:30
|
|
|
if (!ok)
|
|
|
|
fatalx("no functional log directories");
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
|
2007-02-03 06:22:17 +05:30
|
|
|
/* Will look good in libbb one day */
|
|
|
|
static ssize_t ndelay_read(int fd, void *buf, size_t count)
|
|
|
|
{
|
|
|
|
if (!(fl_flag_0 & O_NONBLOCK))
|
|
|
|
fcntl(fd, F_SETFL, fl_flag_0 | O_NONBLOCK);
|
|
|
|
count = safe_read(fd, buf, count);
|
|
|
|
if (!(fl_flag_0 & O_NONBLOCK))
|
|
|
|
fcntl(fd, F_SETFL, fl_flag_0);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2006-11-16 07:57:24 +05:30
|
|
|
/* Used for reading stdin */
|
2007-09-27 15:39:59 +05:30
|
|
|
static int buffer_pread(/*int fd, */char *s, unsigned len)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
2007-08-20 22:57:40 +05:30
|
|
|
unsigned now;
|
|
|
|
struct pollfd input;
|
2006-11-16 07:57:24 +05:30
|
|
|
int i;
|
|
|
|
|
2007-08-20 22:57:40 +05:30
|
|
|
input.fd = 0;
|
2007-08-21 16:48:25 +05:30
|
|
|
input.events = POLLIN;
|
2006-11-16 07:57:24 +05:30
|
|
|
|
2007-05-06 07:07:21 +05:30
|
|
|
do {
|
2007-08-20 22:57:40 +05:30
|
|
|
if (rotateasap) {
|
|
|
|
for (i = 0; i < dirn; ++i)
|
|
|
|
rotate(dir + i);
|
|
|
|
rotateasap = 0;
|
|
|
|
}
|
|
|
|
if (exitasap) {
|
|
|
|
if (linecomplete)
|
|
|
|
return 0;
|
|
|
|
len = 1;
|
|
|
|
}
|
|
|
|
if (reopenasap) {
|
|
|
|
logdirs_reopen();
|
|
|
|
reopenasap = 0;
|
|
|
|
}
|
|
|
|
now = monotonic_sec();
|
|
|
|
nearest_rotate = now + (45 * 60 + 45);
|
|
|
|
for (i = 0; i < dirn; ++i) {
|
|
|
|
if (dir[i].rotate_period) {
|
|
|
|
if (LESS(dir[i].next_rotate, now))
|
|
|
|
rotate(dir + i);
|
|
|
|
if (LESS(dir[i].next_rotate, nearest_rotate))
|
|
|
|
nearest_rotate = dir[i].next_rotate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-06 03:41:06 +05:30
|
|
|
sigprocmask(SIG_UNBLOCK, &blocked_sigset, NULL);
|
2007-08-20 22:57:40 +05:30
|
|
|
i = nearest_rotate - now;
|
|
|
|
if (i > 1000000)
|
|
|
|
i = 1000000;
|
|
|
|
if (i <= 0)
|
|
|
|
i = 1;
|
|
|
|
poll(&input, 1, i * 1000);
|
2007-10-06 03:41:06 +05:30
|
|
|
sigprocmask(SIG_BLOCK, &blocked_sigset, NULL);
|
2007-08-20 22:57:40 +05:30
|
|
|
|
2008-05-19 15:18:17 +05:30
|
|
|
i = ndelay_read(STDIN_FILENO, s, len);
|
2007-08-20 22:57:40 +05:30
|
|
|
if (i >= 0)
|
|
|
|
break;
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2006-11-16 07:57:24 +05:30
|
|
|
if (errno != EAGAIN) {
|
2008-07-21 19:16:54 +05:30
|
|
|
warn("can't read standard input");
|
2006-11-16 07:57:24 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* else: EAGAIN - normal, repeat silently */
|
2007-05-06 07:07:21 +05:30
|
|
|
} while (!exitasap);
|
2006-11-16 07:57:24 +05:30
|
|
|
|
|
|
|
if (i > 0) {
|
|
|
|
int cnt;
|
|
|
|
linecomplete = (s[i-1] == '\n');
|
2007-08-20 22:57:40 +05:30
|
|
|
if (!repl)
|
|
|
|
return i;
|
2006-11-16 07:57:24 +05:30
|
|
|
|
|
|
|
cnt = i;
|
|
|
|
while (--cnt >= 0) {
|
|
|
|
char ch = *s;
|
|
|
|
if (ch != '\n') {
|
|
|
|
if (ch < 32 || ch > 126)
|
|
|
|
*s = repl;
|
|
|
|
else {
|
|
|
|
int j;
|
|
|
|
for (j = 0; replace[j]; ++j) {
|
|
|
|
if (ch == replace[j]) {
|
|
|
|
*s = repl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void sig_term_handler(int sig_no UNUSED_PARAM)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
bb_error_msg(INFO"sig%s received", "term");
|
|
|
|
exitasap = 1;
|
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void sig_child_handler(int sig_no UNUSED_PARAM)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
2008-11-07 04:09:57 +05:30
|
|
|
pid_t pid;
|
|
|
|
int l;
|
2006-11-16 07:57:24 +05:30
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
bb_error_msg(INFO"sig%s received", "child");
|
2008-01-03 01:25:04 +05:30
|
|
|
while ((pid = wait_any_nohang(&wstat)) > 0) {
|
2007-08-20 22:57:40 +05:30
|
|
|
for (l = 0; l < dirn; ++l) {
|
2006-11-16 07:57:24 +05:30
|
|
|
if (dir[l].ppid == pid) {
|
|
|
|
dir[l].ppid = 0;
|
|
|
|
processorstop(&dir[l]);
|
|
|
|
break;
|
|
|
|
}
|
2007-08-20 22:57:40 +05:30
|
|
|
}
|
|
|
|
}
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void sig_alarm_handler(int sig_no UNUSED_PARAM)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
bb_error_msg(INFO"sig%s received", "alarm");
|
|
|
|
rotateasap = 1;
|
|
|
|
}
|
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void sig_hangup_handler(int sig_no UNUSED_PARAM)
|
2006-11-16 07:57:24 +05:30
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
bb_error_msg(INFO"sig%s received", "hangup");
|
|
|
|
reopenasap = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void logmatch(struct logdir *ld)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
ld->match = '+';
|
|
|
|
ld->matcherr = 'E';
|
|
|
|
s = ld->inst;
|
|
|
|
while (s && s[0]) {
|
|
|
|
switch (s[0]) {
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
if (pmatch(s+1, line, linelen))
|
|
|
|
ld->match = s[0];
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
if (pmatch(s+1, line, linelen))
|
|
|
|
ld->matcherr = s[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s += strlen(s) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int svlogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2006-11-16 07:57:24 +05:30
|
|
|
int svlogd_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *r,*l,*b;
|
|
|
|
ssize_t stdin_cnt = 0;
|
|
|
|
int i;
|
|
|
|
unsigned opt;
|
|
|
|
unsigned timestamp = 0;
|
2007-01-28 04:45:50 +05:30
|
|
|
void* (*memRchr)(const void *, int, size_t) = memchr;
|
2006-11-16 07:57:24 +05:30
|
|
|
|
2007-10-06 03:41:06 +05:30
|
|
|
INIT_G();
|
2007-01-28 03:54:59 +05:30
|
|
|
|
2006-11-16 07:57:24 +05:30
|
|
|
opt_complementary = "tt:vv";
|
2007-08-18 21:02:12 +05:30
|
|
|
opt = getopt32(argv, "r:R:l:b:tv",
|
2006-11-16 07:57:24 +05:30
|
|
|
&r, &replace, &l, &b, ×tamp, &verbose);
|
|
|
|
if (opt & 1) { // -r
|
|
|
|
repl = r[0];
|
|
|
|
if (!repl || r[1]) usage();
|
|
|
|
}
|
|
|
|
if (opt & 2) if (!repl) repl = '_'; // -R
|
|
|
|
if (opt & 4) { // -l
|
2007-01-28 03:54:59 +05:30
|
|
|
linemax = xatou_range(l, 0, BUFSIZ-26);
|
|
|
|
if (linemax == 0) linemax = BUFSIZ-26;
|
2006-11-16 07:57:24 +05:30
|
|
|
if (linemax < 256) linemax = 256;
|
|
|
|
}
|
2007-02-03 06:23:43 +05:30
|
|
|
////if (opt & 8) { // -b
|
|
|
|
//// buflen = xatoi_u(b);
|
|
|
|
//// if (buflen == 0) buflen = 1024;
|
|
|
|
////}
|
2006-11-16 07:57:24 +05:30
|
|
|
//if (opt & 0x10) timestamp++; // -t
|
|
|
|
//if (opt & 0x20) verbose++; // -v
|
2007-01-28 03:54:59 +05:30
|
|
|
//if (timestamp > 2) timestamp = 2;
|
2006-11-16 07:57:24 +05:30
|
|
|
argv += optind;
|
|
|
|
argc -= optind;
|
|
|
|
|
|
|
|
dirn = argc;
|
|
|
|
if (dirn <= 0) usage();
|
2007-01-28 03:54:59 +05:30
|
|
|
////if (buflen <= linemax) usage();
|
2006-11-16 07:57:24 +05:30
|
|
|
fdwdir = xopen(".", O_RDONLY|O_NDELAY);
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(fdwdir);
|
2007-05-06 07:07:21 +05:30
|
|
|
dir = xzalloc(dirn * sizeof(struct logdir));
|
2006-11-16 07:57:24 +05:30
|
|
|
for (i = 0; i < dirn; ++i) {
|
|
|
|
dir[i].fddir = -1;
|
|
|
|
dir[i].fdcur = -1;
|
2007-01-28 03:54:59 +05:30
|
|
|
////dir[i].btmp = xmalloc(buflen);
|
2007-05-06 07:07:21 +05:30
|
|
|
/*dir[i].ppid = 0;*/
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
2007-01-28 03:54:59 +05:30
|
|
|
/* line = xmalloc(linemax + (timestamp ? 26 : 0)); */
|
2006-11-16 07:57:24 +05:30
|
|
|
fndir = argv;
|
2007-02-03 06:22:17 +05:30
|
|
|
/* We cannot set NONBLOCK on fd #0 permanently - this setting
|
|
|
|
* _isn't_ per-process! It is shared among all other processes
|
|
|
|
* with the same stdin */
|
2007-08-19 19:12:08 +05:30
|
|
|
fl_flag_0 = fcntl(0, F_GETFL);
|
2007-02-03 06:22:17 +05:30
|
|
|
|
2007-10-06 03:41:06 +05:30
|
|
|
sigemptyset(&blocked_sigset);
|
|
|
|
sigaddset(&blocked_sigset, SIGTERM);
|
|
|
|
sigaddset(&blocked_sigset, SIGCHLD);
|
|
|
|
sigaddset(&blocked_sigset, SIGALRM);
|
|
|
|
sigaddset(&blocked_sigset, SIGHUP);
|
|
|
|
sigprocmask(SIG_BLOCK, &blocked_sigset, NULL);
|
2008-02-17 04:28:56 +05:30
|
|
|
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);
|
2006-11-16 07:57:24 +05:30
|
|
|
|
|
|
|
logdirs_reopen();
|
|
|
|
|
2007-01-28 04:45:50 +05:30
|
|
|
/* 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;
|
|
|
|
|
2007-02-03 06:23:43 +05:30
|
|
|
setvbuf(stderr, NULL, _IOFBF, linelen);
|
|
|
|
|
2007-01-28 20:21:32 +05:30
|
|
|
/* Each iteration processes one or more lines */
|
2006-11-16 07:57:24 +05:30
|
|
|
while (1) {
|
2007-01-28 03:54:59 +05:30
|
|
|
char stamp[FMT_PTIME];
|
|
|
|
char *lineptr;
|
|
|
|
char *printptr;
|
2006-11-16 07:57:24 +05:30
|
|
|
char *np;
|
2007-01-28 03:54:59 +05:30
|
|
|
int printlen;
|
2006-11-16 07:57:24 +05:30
|
|
|
char ch;
|
|
|
|
|
2007-01-28 03:54:59 +05:30
|
|
|
lineptr = line;
|
2007-08-03 16:28:12 +05:30
|
|
|
if (timestamp)
|
2006-11-16 07:57:24 +05:30
|
|
|
lineptr += 26;
|
|
|
|
|
|
|
|
/* lineptr[0..linemax-1] - buffer for stdin */
|
|
|
|
/* (possibly has some unprocessed data from prev loop) */
|
|
|
|
|
|
|
|
/* Refill the buffer if needed */
|
2007-01-28 04:45:50 +05:30
|
|
|
np = memRchr(lineptr, '\n', stdin_cnt);
|
|
|
|
if (!np && !exitasap) {
|
|
|
|
i = linemax - stdin_cnt; /* avail. bytes at tail */
|
|
|
|
if (i >= 128) {
|
2007-09-27 15:39:59 +05:30
|
|
|
i = buffer_pread(/*0, */lineptr + stdin_cnt, i);
|
2007-01-28 04:45:50 +05:30
|
|
|
if (i <= 0) /* EOF or error on stdin */
|
|
|
|
exitasap = 1;
|
|
|
|
else {
|
|
|
|
np = memRchr(lineptr + stdin_cnt, '\n', i);
|
|
|
|
stdin_cnt += i;
|
|
|
|
}
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
if (stdin_cnt <= 0 && exitasap)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Search for '\n' (in fact, np already holds the result) */
|
|
|
|
linelen = stdin_cnt;
|
2007-01-28 03:54:59 +05:30
|
|
|
if (np) {
|
|
|
|
print_to_nl: /* NB: starting from here lineptr may point
|
|
|
|
* farther out into line[] */
|
|
|
|
linelen = np - lineptr + 1;
|
|
|
|
}
|
2006-11-16 07:57:24 +05:30
|
|
|
/* linelen == no of chars incl. '\n' (or == stdin_cnt) */
|
|
|
|
ch = lineptr[linelen-1];
|
|
|
|
|
2007-02-03 06:23:43 +05:30
|
|
|
/* Biggest performance hit was coming from the fact
|
|
|
|
* that we did not buffer writes. We were reading many lines
|
|
|
|
* in one read() above, but wrote one line per write().
|
|
|
|
* We are using stdio to fix that */
|
2007-02-03 06:22:17 +05:30
|
|
|
|
|
|
|
/* write out lineptr[0..linelen-1] to each log destination
|
|
|
|
* (or lineptr[-26..linelen-1] if timestamping) */
|
2007-01-28 03:54:59 +05:30
|
|
|
printlen = linelen;
|
|
|
|
printptr = lineptr;
|
|
|
|
if (timestamp) {
|
2007-08-03 16:28:12 +05:30
|
|
|
if (timestamp == 1)
|
2007-08-20 22:57:40 +05:30
|
|
|
fmt_time_bernstein_25(stamp);
|
2007-08-03 16:28:12 +05:30
|
|
|
else /* 2: */
|
2007-08-20 22:57:40 +05:30
|
|
|
fmt_time_human_30nul(stamp);
|
2007-01-28 03:54:59 +05:30
|
|
|
printlen += 26;
|
|
|
|
printptr -= 26;
|
|
|
|
memcpy(printptr, stamp, 25);
|
|
|
|
printptr[25] = ' ';
|
|
|
|
}
|
2006-11-16 07:57:24 +05:30
|
|
|
for (i = 0; i < dirn; ++i) {
|
|
|
|
struct logdir *ld = &dir[i];
|
|
|
|
if (ld->fddir == -1) continue;
|
|
|
|
if (ld->inst)
|
|
|
|
logmatch(ld);
|
2007-10-22 21:23:34 +05:30
|
|
|
if (ld->matcherr == 'e') {
|
|
|
|
/* runit-1.8.0 compat: if timestamping, do it on stderr too */
|
2008-05-19 15:18:17 +05:30
|
|
|
////full_write(STDERR_FILENO, printptr, printlen);
|
2007-10-22 21:23:34 +05:30
|
|
|
fwrite(printptr, 1, printlen, stderr);
|
|
|
|
}
|
2006-11-16 07:57:24 +05:30
|
|
|
if (ld->match != '+') continue;
|
2007-01-28 03:54:59 +05:30
|
|
|
buffer_pwrite(i, printptr, printlen);
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* If we didn't see '\n' (long input line), */
|
|
|
|
/* read/write repeatedly until we see it */
|
|
|
|
while (ch != '\n') {
|
|
|
|
/* lineptr is emptied now, safe to use as buffer */
|
2007-09-27 15:39:59 +05:30
|
|
|
stdin_cnt = exitasap ? -1 : buffer_pread(/*0, */lineptr, linemax);
|
2006-11-16 07:57:24 +05:30
|
|
|
if (stdin_cnt <= 0) { /* EOF or error on stdin */
|
2007-01-28 03:54:59 +05:30
|
|
|
exitasap = 1;
|
2006-11-16 07:57:24 +05:30
|
|
|
lineptr[0] = ch = '\n';
|
|
|
|
linelen = 1;
|
|
|
|
stdin_cnt = 1;
|
|
|
|
} else {
|
|
|
|
linelen = stdin_cnt;
|
2007-01-28 04:45:50 +05:30
|
|
|
np = memRchr(lineptr, '\n', stdin_cnt);
|
|
|
|
if (np)
|
|
|
|
linelen = np - lineptr + 1;
|
2006-11-16 07:57:24 +05:30
|
|
|
ch = lineptr[linelen-1];
|
|
|
|
}
|
|
|
|
/* linelen == no of chars incl. '\n' (or == stdin_cnt) */
|
|
|
|
for (i = 0; i < dirn; ++i) {
|
|
|
|
if (dir[i].fddir == -1) continue;
|
2007-10-22 21:23:34 +05:30
|
|
|
if (dir[i].matcherr == 'e') {
|
2008-05-19 15:18:17 +05:30
|
|
|
////full_write(STDERR_FILENO, lineptr, linelen);
|
2007-02-03 06:23:43 +05:30
|
|
|
fwrite(lineptr, 1, linelen, stderr);
|
2007-10-22 21:23:34 +05:30
|
|
|
}
|
2006-11-16 07:57:24 +05:30
|
|
|
if (dir[i].match != '+') continue;
|
|
|
|
buffer_pwrite(i, lineptr, linelen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stdin_cnt -= linelen;
|
2007-01-28 03:54:59 +05:30
|
|
|
if (stdin_cnt > 0) {
|
|
|
|
lineptr += linelen;
|
|
|
|
/* If we see another '\n', we don't need to read
|
|
|
|
* next piece of input: can print what we have */
|
2007-01-28 04:45:50 +05:30
|
|
|
np = memRchr(lineptr, '\n', stdin_cnt);
|
2007-01-28 03:54:59 +05:30
|
|
|
if (np)
|
|
|
|
goto print_to_nl;
|
|
|
|
/* Move unprocessed data to the front of line */
|
|
|
|
memmove((timestamp ? line+26 : line), lineptr, stdin_cnt);
|
|
|
|
}
|
2007-02-03 06:23:43 +05:30
|
|
|
fflush(NULL);////
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < dirn; ++i) {
|
|
|
|
if (dir[i].ppid)
|
|
|
|
while (!processorstop(&dir[i]))
|
|
|
|
/* repeat */;
|
|
|
|
logdir_close(&dir[i]);
|
|
|
|
}
|
2007-01-28 04:45:50 +05:30
|
|
|
return 0;
|
2006-11-16 07:57:24 +05:30
|
|
|
}
|