blob: b0ba21bb638fa2be413b5a8cf2877e6362e2546a [file] [log] [blame]
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001/*
2Copyright (c) 2001-2006, Gerrit Pape
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
Denis Vlasenkod18f52b2008-03-02 12:53:15 +000028/* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
Denis Vlasenko83ea6432006-11-16 02:27:24 +000029/* TODO: depends on runit_lib.c - review and reduce/eliminate */
30
Denys Vlasenkoaebb7422009-08-02 00:55:49 +020031/*
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*/
127
Denys Vlasenko3a0f6902011-01-22 00:38:24 +0100128//usage:#define svlogd_trivial_usage
129//usage: "[-ttv] [-r C] [-R CHARS] [-l MATCHLEN] [-b BUFLEN] DIR..."
130//usage:#define svlogd_full_usage "\n\n"
131//usage: "Continuously read log data from stdin and write to rotated log files in DIRs"
132//usage: "\n"
133//usage: "\n""DIR/config file modifies behavior:"
134//usage: "\n""sSIZE - when to rotate logs"
135//usage: "\n""nNUM - number of files to retain"
136/*usage: "\n""NNUM - min number files to retain" - confusing */
137/*usage: "\n""tSEC - rotate file if it get SEC seconds old" - confusing */
138//usage: "\n""!PROG - process rotated log with PROG"
139/*usage: "\n""uIPADDR - send log over UDP" - unsupported */
140/*usage: "\n""UIPADDR - send log over UDP and DONT log" - unsupported */
141/*usage: "\n""pPFX - prefix each line with PFX" - unsupported */
142//usage: "\n""+,-PATTERN - (de)select line for logging"
143//usage: "\n""E,ePATTERN - (de)select line for stderr"
144
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000145#include <sys/poll.h>
146#include <sys/file.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000147#include "libbb.h"
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000148#include "runit_lib.h"
149
Denis Vlasenko45946f82007-08-20 17:27:40 +0000150#define LESS(a,b) ((int)((unsigned)(b) - (unsigned)(a)) > 0)
151
152#define FMT_PTIME 30
153
Denis Vlasenko339936b2007-10-05 22:11:06 +0000154struct logdir {
Denis Vlasenko64392902007-02-03 00:53:43 +0000155 ////char *btmp;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000156 /* pattern list to match, in "aa\0bb\0\cc\0\0" form */
157 char *inst;
158 char *processor;
159 char *name;
160 unsigned size;
161 unsigned sizemax;
162 unsigned nmax;
163 unsigned nmin;
Denis Vlasenko45946f82007-08-20 17:27:40 +0000164 unsigned rotate_period;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000165 int ppid;
166 int fddir;
167 int fdcur;
Denis Vlasenko64392902007-02-03 00:53:43 +0000168 FILE* filecur; ////
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000169 int fdlock;
Denis Vlasenko45946f82007-08-20 17:27:40 +0000170 unsigned next_rotate;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000171 char fnsave[FMT_PTIME];
172 char match;
173 char matcherr;
Denis Vlasenko339936b2007-10-05 22:11:06 +0000174};
175
176
177struct globals {
178 struct logdir *dir;
179 unsigned verbose;
180 int linemax;
181 ////int buflen;
182 int linelen;
183
184 int fdwdir;
185 char **fndir;
186 int wstat;
187 unsigned nearest_rotate;
188
Denys Vlasenkoaebb7422009-08-02 00:55:49 +0200189 void* (*memRchr)(const void *, int, size_t);
Denys Vlasenko681efe22011-03-08 21:00:36 +0100190 char *shell;
Denys Vlasenkoaebb7422009-08-02 00:55:49 +0200191
Denis Vlasenko339936b2007-10-05 22:11:06 +0000192 smallint exitasap;
193 smallint rotateasap;
194 smallint reopenasap;
195 smallint linecomplete;
196 smallint tmaxflag;
197
198 char repl;
199 const char *replace;
200 int fl_flag_0;
201 unsigned dirn;
202
203 sigset_t blocked_sigset;
204};
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100205#define G (*ptr_to_globals)
Denis Vlasenko339936b2007-10-05 22:11:06 +0000206#define dir (G.dir )
207#define verbose (G.verbose )
208#define linemax (G.linemax )
209#define buflen (G.buflen )
210#define linelen (G.linelen )
211#define fndir (G.fndir )
212#define fdwdir (G.fdwdir )
213#define wstat (G.wstat )
Denys Vlasenkoaebb7422009-08-02 00:55:49 +0200214#define memRchr (G.memRchr )
Denis Vlasenko339936b2007-10-05 22:11:06 +0000215#define nearest_rotate (G.nearest_rotate)
216#define exitasap (G.exitasap )
217#define rotateasap (G.rotateasap )
218#define reopenasap (G.reopenasap )
219#define linecomplete (G.linecomplete )
220#define tmaxflag (G.tmaxflag )
221#define repl (G.repl )
222#define replace (G.replace )
223#define blocked_sigset (G.blocked_sigset)
224#define fl_flag_0 (G.fl_flag_0 )
225#define dirn (G.dirn )
226#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000227 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenko339936b2007-10-05 22:11:06 +0000228 linemax = 1000; \
229 /*buflen = 1024;*/ \
230 linecomplete = 1; \
231 replace = ""; \
232} while (0)
233
234#define line bb_common_bufsiz1
235
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000236
237#define FATAL "fatal: "
238#define WARNING "warning: "
239#define PAUSE "pausing: "
240#define INFO "info: "
241
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000242static void fatalx(const char *m0)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000243{
244 bb_error_msg_and_die(FATAL"%s", m0);
245}
Denis Vlasenkoac678ec2007-04-16 22:32:04 +0000246static void warn(const char *m0)
247{
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000248 bb_perror_msg(WARNING"%s", m0);
249}
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000250static void warn2(const char *m0, const char *m1)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000251{
252 bb_perror_msg(WARNING"%s: %s", m0, m1);
253}
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000254static void warnx(const char *m0, const char *m1)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000255{
256 bb_error_msg(WARNING"%s: %s", m0, m1);
257}
258static void pause_nomem(void)
259{
Denis Vlasenko8c783952007-01-27 22:21:52 +0000260 bb_error_msg(PAUSE"out of memory");
261 sleep(3);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000262}
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000263static void pause1cannot(const char *m0)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000264{
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000265 bb_perror_msg(PAUSE"can't %s", m0);
Denis Vlasenko8c783952007-01-27 22:21:52 +0000266 sleep(3);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000267}
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000268static void pause2cannot(const char *m0, const char *m1)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000269{
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000270 bb_perror_msg(PAUSE"can't %s %s", m0, m1);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000271 sleep(3);
272}
273
274static char* wstrdup(const char *str)
275{
276 char *s;
Denis Vlasenko8c783952007-01-27 22:21:52 +0000277 while (!(s = strdup(str)))
278 pause_nomem();
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000279 return s;
280}
281
Denys Vlasenko05e86052010-10-13 12:53:27 +0200282static unsigned pmatch(const char *p, const char *s, unsigned len)
283{
284 for (;;) {
285 char c = *p++;
286 if (!c) return !len;
287 switch (c) {
288 case '*':
289 c = *p;
290 if (!c) return 1;
291 for (;;) {
292 if (!len) return 0;
293 if (*s == c) break;
294 ++s;
295 --len;
296 }
297 continue;
298 case '+':
299 c = *p++;
300 if (c != *s) return 0;
301 for (;;) {
302 if (!len) return 1;
303 if (*s != c) break;
304 ++s;
305 --len;
306 }
307 continue;
308 /*
309 case '?':
310 if (*p == '?') {
311 if (*s != '?') return 0;
312 ++p;
313 }
314 ++s; --len;
315 continue;
316 */
317 default:
318 if (!len) return 0;
319 if (*s != c) return 0;
320 ++s;
321 --len;
322 continue;
323 }
324 }
325 return 0;
326}
327
Denis Vlasenko45946f82007-08-20 17:27:40 +0000328/*** ex fmt_ptime.[ch] ***/
329
330/* NUL terminated */
331static void fmt_time_human_30nul(char *s)
332{
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100333 struct tm *ptm;
Denis Vlasenko45946f82007-08-20 17:27:40 +0000334 struct timeval tv;
335
336 gettimeofday(&tv, NULL);
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100337 ptm = gmtime(&tv.tv_sec);
Denis Vlasenko45946f82007-08-20 17:27:40 +0000338 sprintf(s, "%04u-%02u-%02u_%02u:%02u:%02u.%06u000",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100339 (unsigned)(1900 + ptm->tm_year),
340 (unsigned)(ptm->tm_mon + 1),
341 (unsigned)(ptm->tm_mday),
342 (unsigned)(ptm->tm_hour),
343 (unsigned)(ptm->tm_min),
344 (unsigned)(ptm->tm_sec),
Denis Vlasenko45946f82007-08-20 17:27:40 +0000345 (unsigned)(tv.tv_usec)
346 );
347 /* 4+1 + 2+1 + 2+1 + 2+1 + 2+1 + 2+1 + 9 = */
348 /* 5 + 3 + 3 + 3 + 3 + 3 + 9 = */
349 /* 20 (up to '.' inclusive) + 9 (not including '\0') */
350}
351
352/* NOT terminated! */
353static void fmt_time_bernstein_25(char *s)
354{
355 uint32_t pack[3];
356 struct timeval tv;
357 unsigned sec_hi;
358
359 gettimeofday(&tv, NULL);
360 sec_hi = (0x400000000000000aULL + tv.tv_sec) >> 32;
361 tv.tv_sec = (time_t)(0x400000000000000aULL) + tv.tv_sec;
362 tv.tv_usec *= 1000;
363 /* Network order is big-endian: most significant byte first.
364 * This is exactly what we want here */
365 pack[0] = htonl(sec_hi);
366 pack[1] = htonl(tv.tv_sec);
367 pack[2] = htonl(tv.tv_usec);
368 *s++ = '@';
369 bin2hex(s, (char*)pack, 12);
370}
371
Denis Vlasenko4ee7cd42008-03-17 09:13:22 +0000372static void processorstart(struct logdir *ld)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000373{
Denis Vlasenko4ee7cd42008-03-17 09:13:22 +0000374 char sv_ch;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000375 int pid;
376
Denis Vlasenko4ee7cd42008-03-17 09:13:22 +0000377 if (!ld->processor) return;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000378 if (ld->ppid) {
379 warnx("processor already running", ld->name);
Denis Vlasenko4ee7cd42008-03-17 09:13:22 +0000380 return;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000381 }
Denis Vlasenko4ee7cd42008-03-17 09:13:22 +0000382
383 /* vfork'ed child trashes this byte, save... */
384 sv_ch = ld->fnsave[26];
385
Denys Vlasenko681efe22011-03-08 21:00:36 +0100386 if (!G.shell)
387 G.shell = xstrdup(get_shell_name());
388
Denis Vlasenko4ee7cd42008-03-17 09:13:22 +0000389 while ((pid = vfork()) == -1)
390 pause2cannot("vfork for processor", ld->name);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000391 if (!pid) {
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000392 int fd;
393
394 /* child */
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000395 /* Non-ignored signals revert to SIG_DFL on exec anyway */
396 /*bb_signals(0
Denis Vlasenko25591c32008-02-16 22:58:56 +0000397 + (1 << SIGTERM)
398 + (1 << SIGALRM)
399 + (1 << SIGHUP)
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000400 , SIG_DFL);*/
Denis Vlasenko8c783952007-01-27 22:21:52 +0000401 sig_unblock(SIGTERM);
402 sig_unblock(SIGALRM);
403 sig_unblock(SIGHUP);
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000404
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000405 if (verbose)
406 bb_error_msg(INFO"processing: %s/%s", ld->name, ld->fnsave);
407 fd = xopen(ld->fnsave, O_RDONLY|O_NDELAY);
Denis Vlasenkocad04ef2007-03-25 23:21:05 +0000408 xmove_fd(fd, 0);
Denis Vlasenko4ee7cd42008-03-17 09:13:22 +0000409 ld->fnsave[26] = 't'; /* <- that's why we need sv_ch! */
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000410 fd = xopen(ld->fnsave, O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
Denis Vlasenkocad04ef2007-03-25 23:21:05 +0000411 xmove_fd(fd, 1);
Denys Vlasenko05e86052010-10-13 12:53:27 +0200412 fd = open("state", O_RDONLY|O_NDELAY);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000413 if (fd == -1) {
414 if (errno != ENOENT)
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000415 bb_perror_msg_and_die(FATAL"can't %s processor %s", "open state for", ld->name);
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000416 close(xopen("state", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT));
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000417 fd = xopen("state", O_RDONLY|O_NDELAY);
418 }
Denis Vlasenkocad04ef2007-03-25 23:21:05 +0000419 xmove_fd(fd, 4);
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000420 fd = xopen("newstate", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
Denis Vlasenkocad04ef2007-03-25 23:21:05 +0000421 xmove_fd(fd, 5);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000422
Denys Vlasenko681efe22011-03-08 21:00:36 +0100423 execl(G.shell, G.shell, "-c", ld->processor, (char*) NULL);
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000424 bb_perror_msg_and_die(FATAL"can't %s processor %s", "run", ld->name);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000425 }
Denis Vlasenko4ee7cd42008-03-17 09:13:22 +0000426 ld->fnsave[26] = sv_ch; /* ...restore */
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000427 ld->ppid = pid;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000428}
429
430static unsigned processorstop(struct logdir *ld)
431{
432 char f[28];
433
434 if (ld->ppid) {
Denis Vlasenko8c783952007-01-27 22:21:52 +0000435 sig_unblock(SIGHUP);
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000436 while (safe_waitpid(ld->ppid, &wstat, 0) == -1)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000437 pause2cannot("wait for processor", ld->name);
Denis Vlasenko8c783952007-01-27 22:21:52 +0000438 sig_block(SIGHUP);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000439 ld->ppid = 0;
440 }
Denys Vlasenkoaebb7422009-08-02 00:55:49 +0200441 if (ld->fddir == -1)
442 return 1;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000443 while (fchdir(ld->fddir) == -1)
444 pause2cannot("change directory, want processor", ld->name);
Denys Vlasenko8f24f982009-06-07 16:02:00 +0200445 if (WEXITSTATUS(wstat) != 0) {
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000446 warnx("processor failed, restart", ld->name);
447 ld->fnsave[26] = 't';
448 unlink(ld->fnsave);
449 ld->fnsave[26] = 'u';
450 processorstart(ld);
451 while (fchdir(fdwdir) == -1)
452 pause1cannot("change to initial working directory");
453 return ld->processor ? 0 : 1;
454 }
455 ld->fnsave[26] = 't';
456 memcpy(f, ld->fnsave, 26);
457 f[26] = 's';
458 f[27] = '\0';
459 while (rename(ld->fnsave, f) == -1)
460 pause2cannot("rename processed", ld->name);
461 while (chmod(f, 0744) == -1)
462 pause2cannot("set mode of processed", ld->name);
463 ld->fnsave[26] = 'u';
464 if (unlink(ld->fnsave) == -1)
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000465 bb_error_msg(WARNING"can't unlink: %s/%s", ld->name, ld->fnsave);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000466 while (rename("newstate", "state") == -1)
467 pause2cannot("rename state", ld->name);
Denis Vlasenko8c783952007-01-27 22:21:52 +0000468 if (verbose)
469 bb_error_msg(INFO"processed: %s/%s", ld->name, f);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000470 while (fchdir(fdwdir) == -1)
471 pause1cannot("change to initial working directory");
472 return 1;
473}
474
475static void rmoldest(struct logdir *ld)
476{
477 DIR *d;
478 struct dirent *f;
479 char oldest[FMT_PTIME];
480 int n = 0;
481
482 oldest[0] = 'A'; oldest[1] = oldest[27] = 0;
483 while (!(d = opendir(".")))
484 pause2cannot("open directory, want rotate", ld->name);
485 errno = 0;
486 while ((f = readdir(d))) {
487 if ((f->d_name[0] == '@') && (strlen(f->d_name) == 27)) {
488 if (f->d_name[26] == 't') {
489 if (unlink(f->d_name) == -1)
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000490 warn2("can't unlink processor leftover", f->d_name);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000491 } else {
492 ++n;
493 if (strcmp(f->d_name, oldest) < 0)
494 memcpy(oldest, f->d_name, 27);
495 }
496 errno = 0;
497 }
498 }
Denis Vlasenko8c783952007-01-27 22:21:52 +0000499 if (errno)
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000500 warn2("can't read directory", ld->name);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000501 closedir(d);
502
503 if (ld->nmax && (n > ld->nmax)) {
Denis Vlasenko8c783952007-01-27 22:21:52 +0000504 if (verbose)
505 bb_error_msg(INFO"delete: %s/%s", ld->name, oldest);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000506 if ((*oldest == '@') && (unlink(oldest) == -1))
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000507 warn2("can't unlink oldest logfile", ld->name);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000508 }
509}
510
511static unsigned rotate(struct logdir *ld)
512{
513 struct stat st;
Denis Vlasenko45946f82007-08-20 17:27:40 +0000514 unsigned now;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000515
516 if (ld->fddir == -1) {
Denis Vlasenko45946f82007-08-20 17:27:40 +0000517 ld->rotate_period = 0;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000518 return 0;
519 }
520 if (ld->ppid)
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000521 while (!processorstop(ld))
Denis Vlasenko45946f82007-08-20 17:27:40 +0000522 continue;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000523
524 while (fchdir(ld->fddir) == -1)
525 pause2cannot("change directory, want rotate", ld->name);
526
527 /* create new filename */
528 ld->fnsave[25] = '.';
529 ld->fnsave[26] = 's';
530 if (ld->processor)
531 ld->fnsave[26] = 'u';
532 ld->fnsave[27] = '\0';
533 do {
Denis Vlasenko45946f82007-08-20 17:27:40 +0000534 fmt_time_bernstein_25(ld->fnsave);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000535 errno = 0;
Denis Vlasenko8c783952007-01-27 22:21:52 +0000536 stat(ld->fnsave, &st);
537 } while (errno != ENOENT);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000538
Denis Vlasenko45946f82007-08-20 17:27:40 +0000539 now = monotonic_sec();
540 if (ld->rotate_period && LESS(ld->next_rotate, now)) {
541 ld->next_rotate = now + ld->rotate_period;
542 if (LESS(ld->next_rotate, nearest_rotate))
543 nearest_rotate = ld->next_rotate;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000544 }
545
546 if (ld->size > 0) {
Denis Vlasenko64392902007-02-03 00:53:43 +0000547 while (fflush(ld->filecur) || fsync(ld->fdcur) == -1)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000548 pause2cannot("fsync current logfile", ld->name);
549 while (fchmod(ld->fdcur, 0744) == -1)
550 pause2cannot("set mode of current", ld->name);
Denis Vlasenko64392902007-02-03 00:53:43 +0000551 ////close(ld->fdcur);
552 fclose(ld->filecur);
553
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000554 if (verbose) {
555 bb_error_msg(INFO"rename: %s/current %s %u", ld->name,
556 ld->fnsave, ld->size);
557 }
558 while (rename("current", ld->fnsave) == -1)
559 pause2cannot("rename current", ld->name);
560 while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1)
561 pause2cannot("create new current", ld->name);
Denys Vlasenkoc5d07fb2009-07-03 18:31:23 +0200562 while ((ld->filecur = fdopen(ld->fdcur, "a")) == NULL) ////
563 pause2cannot("create new current", ld->name); /* very unlikely */
Denis Vlasenko64392902007-02-03 00:53:43 +0000564 setvbuf(ld->filecur, NULL, _IOFBF, linelen); ////
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000565 close_on_exec_on(ld->fdcur);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000566 ld->size = 0;
567 while (fchmod(ld->fdcur, 0644) == -1)
568 pause2cannot("set mode of current", ld->name);
Denys Vlasenkoc5d07fb2009-07-03 18:31:23 +0200569
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000570 rmoldest(ld);
571 processorstart(ld);
572 }
573
574 while (fchdir(fdwdir) == -1)
575 pause1cannot("change to initial working directory");
576 return 1;
577}
578
579static int buffer_pwrite(int n, char *s, unsigned len)
580{
581 int i;
582 struct logdir *ld = &dir[n];
583
584 if (ld->sizemax) {
585 if (ld->size >= ld->sizemax)
586 rotate(ld);
587 if (len > (ld->sizemax - ld->size))
588 len = ld->sizemax - ld->size;
589 }
Denis Vlasenko64392902007-02-03 00:53:43 +0000590 while (1) {
591 ////i = full_write(ld->fdcur, s, len);
592 ////if (i != -1) break;
593 i = fwrite(s, 1, len, ld->filecur);
594 if (i == len) break;
595
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000596 if ((errno == ENOSPC) && (ld->nmin < ld->nmax)) {
597 DIR *d;
598 struct dirent *f;
599 char oldest[FMT_PTIME];
600 int j = 0;
601
602 while (fchdir(ld->fddir) == -1)
603 pause2cannot("change directory, want remove old logfile",
604 ld->name);
605 oldest[0] = 'A';
606 oldest[1] = oldest[27] = '\0';
607 while (!(d = opendir(".")))
608 pause2cannot("open directory, want remove old logfile",
609 ld->name);
610 errno = 0;
611 while ((f = readdir(d)))
612 if ((f->d_name[0] == '@') && (strlen(f->d_name) == 27)) {
613 ++j;
614 if (strcmp(f->d_name, oldest) < 0)
615 memcpy(oldest, f->d_name, 27);
616 }
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000617 if (errno) warn2("can't read directory, want remove old logfile",
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000618 ld->name);
619 closedir(d);
620 errno = ENOSPC;
621 if (j > ld->nmin) {
622 if (*oldest == '@') {
623 bb_error_msg(WARNING"out of disk space, delete: %s/%s",
624 ld->name, oldest);
625 errno = 0;
626 if (unlink(oldest) == -1) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000627 warn2("can't unlink oldest logfile", ld->name);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000628 errno = ENOSPC;
629 }
630 while (fchdir(fdwdir) == -1)
631 pause1cannot("change to initial working directory");
632 }
633 }
634 }
Denis Vlasenkoca549c52007-01-27 22:24:59 +0000635 if (errno)
636 pause2cannot("write to current", ld->name);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000637 }
638
639 ld->size += i;
640 if (ld->sizemax)
641 if (s[i-1] == '\n')
642 if (ld->size >= (ld->sizemax - linemax))
643 rotate(ld);
644 return i;
645}
646
647static void logdir_close(struct logdir *ld)
648{
649 if (ld->fddir == -1)
650 return;
651 if (verbose)
652 bb_error_msg(INFO"close: %s", ld->name);
653 close(ld->fddir);
654 ld->fddir = -1;
655 if (ld->fdcur == -1)
656 return; /* impossible */
Denis Vlasenko64392902007-02-03 00:53:43 +0000657 while (fflush(ld->filecur) || fsync(ld->fdcur) == -1)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000658 pause2cannot("fsync current logfile", ld->name);
659 while (fchmod(ld->fdcur, 0744) == -1)
660 pause2cannot("set mode of current", ld->name);
Denis Vlasenko64392902007-02-03 00:53:43 +0000661 ////close(ld->fdcur);
662 fclose(ld->filecur);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000663 ld->fdcur = -1;
664 if (ld->fdlock == -1)
665 return; /* impossible */
666 close(ld->fdlock);
667 ld->fdlock = -1;
668 free(ld->processor);
669 ld->processor = NULL;
670}
671
Denys Vlasenkoa7bb3c12009-10-08 12:28:08 +0200672static NOINLINE unsigned logdir_open(struct logdir *ld, const char *fn)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000673{
674 char buf[128];
Denis Vlasenko45946f82007-08-20 17:27:40 +0000675 unsigned now;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000676 char *new, *s, *np;
677 int i;
678 struct stat st;
679
Denis Vlasenko45946f82007-08-20 17:27:40 +0000680 now = monotonic_sec();
681
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000682 ld->fddir = open(fn, O_RDONLY|O_NDELAY);
683 if (ld->fddir == -1) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000684 warn2("can't open log directory", (char*)fn);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000685 return 0;
686 }
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000687 close_on_exec_on(ld->fddir);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000688 if (fchdir(ld->fddir) == -1) {
689 logdir_close(ld);
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000690 warn2("can't change directory", (char*)fn);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000691 return 0;
692 }
693 ld->fdlock = open("lock", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
694 if ((ld->fdlock == -1)
Denys Vlasenko05e86052010-10-13 12:53:27 +0200695 || (flock(ld->fdlock, LOCK_EX | LOCK_NB) == -1)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000696 ) {
697 logdir_close(ld);
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000698 warn2("can't lock directory", (char*)fn);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000699 while (fchdir(fdwdir) == -1)
700 pause1cannot("change to initial working directory");
701 return 0;
702 }
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000703 close_on_exec_on(ld->fdlock);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000704
705 ld->size = 0;
706 ld->sizemax = 1000000;
707 ld->nmax = ld->nmin = 10;
Denis Vlasenko45946f82007-08-20 17:27:40 +0000708 ld->rotate_period = 0;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000709 ld->name = (char*)fn;
710 ld->ppid = 0;
711 ld->match = '+';
712 free(ld->inst); ld->inst = NULL;
713 free(ld->processor); ld->processor = NULL;
714
715 /* read config */
Denys Vlasenkoc5d07fb2009-07-03 18:31:23 +0200716 i = open_read_close("config", buf, sizeof(buf) - 1);
Denis Vlasenkof223efb2007-08-03 10:58:12 +0000717 if (i < 0 && errno != ENOENT)
Denis Vlasenko72b6a652007-08-21 11:18:25 +0000718 bb_perror_msg(WARNING"%s/config", ld->name);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000719 if (i > 0) {
Denys Vlasenkoc5d07fb2009-07-03 18:31:23 +0200720 buf[i] = '\0';
Denis Vlasenko45946f82007-08-20 17:27:40 +0000721 if (verbose)
722 bb_error_msg(INFO"read: %s/config", ld->name);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000723 s = buf;
724 while (s) {
725 np = strchr(s, '\n');
Denis Vlasenko72b6a652007-08-21 11:18:25 +0000726 if (np)
727 *np++ = '\0';
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000728 switch (s[0]) {
729 case '+':
730 case '-':
731 case 'e':
732 case 'E':
Denys Vlasenkoaebb7422009-08-02 00:55:49 +0200733 /* Filtering requires one-line buffering,
734 * resetting the "find newline" function
735 * accordingly */
736 memRchr = memchr;
Denis Vlasenko72b6a652007-08-21 11:18:25 +0000737 /* Add '\n'-terminated line to ld->inst */
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000738 while (1) {
Denys Vlasenko90a99042009-09-06 02:36:23 +0200739 int l = asprintf(&new, "%s%s\n", ld->inst ? ld->inst : "", s);
Denis Vlasenko72b6a652007-08-21 11:18:25 +0000740 if (l >= 0 && new)
741 break;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000742 pause_nomem();
743 }
744 free(ld->inst);
745 ld->inst = new;
746 break;
747 case 's': {
748 static const struct suffix_mult km_suffixes[] = {
Denis Vlasenkof8689632007-07-27 15:06:25 +0000749 { "k", 1024 },
750 { "m", 1024*1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200751 { "", 0 }
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000752 };
753 ld->sizemax = xatou_sfx(&s[1], km_suffixes);
754 break;
755 }
756 case 'n':
Denys Vlasenko77832482010-08-12 14:14:45 +0200757 ld->nmax = xatoi_positive(&s[1]);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000758 break;
759 case 'N':
Denys Vlasenko77832482010-08-12 14:14:45 +0200760 ld->nmin = xatoi_positive(&s[1]);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000761 break;
762 case 't': {
763 static const struct suffix_mult mh_suffixes[] = {
Denis Vlasenkof8689632007-07-27 15:06:25 +0000764 { "m", 60 },
765 { "h", 60*60 },
766 /*{ "d", 24*60*60 },*/
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200767 { "", 0 }
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000768 };
Denis Vlasenko45946f82007-08-20 17:27:40 +0000769 ld->rotate_period = xatou_sfx(&s[1], mh_suffixes);
770 if (ld->rotate_period) {
771 ld->next_rotate = now + ld->rotate_period;
772 if (!tmaxflag || LESS(ld->next_rotate, nearest_rotate))
773 nearest_rotate = ld->next_rotate;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000774 tmaxflag = 1;
775 }
776 break;
777 }
778 case '!':
779 if (s[1]) {
780 free(ld->processor);
781 ld->processor = wstrdup(s);
782 }
783 break;
784 }
785 s = np;
786 }
787 /* Convert "aa\nbb\ncc\n\0" to "aa\0bb\0cc\0\0" */
788 s = ld->inst;
789 while (s) {
790 np = strchr(s, '\n');
Denis Vlasenko72b6a652007-08-21 11:18:25 +0000791 if (np)
792 *np++ = '\0';
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000793 s = np;
794 }
795 }
796
797 /* open current */
798 i = stat("current", &st);
799 if (i != -1) {
Denis Vlasenko72b6a652007-08-21 11:18:25 +0000800 if (st.st_size && !(st.st_mode & S_IXUSR)) {
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000801 ld->fnsave[25] = '.';
802 ld->fnsave[26] = 'u';
803 ld->fnsave[27] = '\0';
804 do {
Denis Vlasenko45946f82007-08-20 17:27:40 +0000805 fmt_time_bernstein_25(ld->fnsave);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000806 errno = 0;
Denis Vlasenko8c783952007-01-27 22:21:52 +0000807 stat(ld->fnsave, &st);
808 } while (errno != ENOENT);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000809 while (rename("current", ld->fnsave) == -1)
810 pause2cannot("rename current", ld->name);
811 rmoldest(ld);
812 i = -1;
813 } else {
Denis Vlasenko72b6a652007-08-21 11:18:25 +0000814 /* st.st_size can be not just bigger, but WIDER!
815 * This code is safe: if st.st_size > 4GB, we select
816 * ld->sizemax (because it's "unsigned") */
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000817 ld->size = (st.st_size > ld->sizemax) ? ld->sizemax : st.st_size;
818 }
819 } else {
820 if (errno != ENOENT) {
821 logdir_close(ld);
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000822 warn2("can't stat current", ld->name);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000823 while (fchdir(fdwdir) == -1)
824 pause1cannot("change to initial working directory");
825 return 0;
826 }
827 }
828 while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1)
829 pause2cannot("open current", ld->name);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100830 while ((ld->filecur = fdopen(ld->fdcur, "a")) == NULL)
831 pause2cannot("open current", ld->name); ////
Denis Vlasenko64392902007-02-03 00:53:43 +0000832 setvbuf(ld->filecur, NULL, _IOFBF, linelen); ////
833
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000834 close_on_exec_on(ld->fdcur);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000835 while (fchmod(ld->fdcur, 0644) == -1)
836 pause2cannot("set mode of current", ld->name);
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000837
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000838 if (verbose) {
839 if (i == 0) bb_error_msg(INFO"append: %s/current", ld->name);
840 else bb_error_msg(INFO"new: %s/current", ld->name);
841 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000842
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000843 while (fchdir(fdwdir) == -1)
844 pause1cannot("change to initial working directory");
845 return 1;
846}
847
848static void logdirs_reopen(void)
849{
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000850 int l;
851 int ok = 0;
852
853 tmaxflag = 0;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000854 for (l = 0; l < dirn; ++l) {
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000855 logdir_close(&dir[l]);
Denis Vlasenko45946f82007-08-20 17:27:40 +0000856 if (logdir_open(&dir[l], fndir[l]))
857 ok = 1;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000858 }
Denis Vlasenko45946f82007-08-20 17:27:40 +0000859 if (!ok)
860 fatalx("no functional log directories");
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000861}
862
Denis Vlasenko4f8d27f2007-02-03 00:52:17 +0000863/* Will look good in libbb one day */
864static ssize_t ndelay_read(int fd, void *buf, size_t count)
865{
866 if (!(fl_flag_0 & O_NONBLOCK))
867 fcntl(fd, F_SETFL, fl_flag_0 | O_NONBLOCK);
868 count = safe_read(fd, buf, count);
869 if (!(fl_flag_0 & O_NONBLOCK))
870 fcntl(fd, F_SETFL, fl_flag_0);
871 return count;
872}
873
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000874/* Used for reading stdin */
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000875static int buffer_pread(/*int fd, */char *s, unsigned len)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000876{
Denis Vlasenko45946f82007-08-20 17:27:40 +0000877 unsigned now;
878 struct pollfd input;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000879 int i;
880
Denys Vlasenkoaebb7422009-08-02 00:55:49 +0200881 input.fd = STDIN_FILENO;
Denis Vlasenko72b6a652007-08-21 11:18:25 +0000882 input.events = POLLIN;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000883
Denis Vlasenkob9528352007-05-06 01:37:21 +0000884 do {
Denis Vlasenko45946f82007-08-20 17:27:40 +0000885 if (rotateasap) {
886 for (i = 0; i < dirn; ++i)
887 rotate(dir + i);
888 rotateasap = 0;
889 }
890 if (exitasap) {
891 if (linecomplete)
892 return 0;
893 len = 1;
894 }
895 if (reopenasap) {
896 logdirs_reopen();
897 reopenasap = 0;
898 }
899 now = monotonic_sec();
900 nearest_rotate = now + (45 * 60 + 45);
901 for (i = 0; i < dirn; ++i) {
902 if (dir[i].rotate_period) {
903 if (LESS(dir[i].next_rotate, now))
904 rotate(dir + i);
905 if (LESS(dir[i].next_rotate, nearest_rotate))
906 nearest_rotate = dir[i].next_rotate;
907 }
908 }
909
Denis Vlasenko339936b2007-10-05 22:11:06 +0000910 sigprocmask(SIG_UNBLOCK, &blocked_sigset, NULL);
Denis Vlasenko45946f82007-08-20 17:27:40 +0000911 i = nearest_rotate - now;
912 if (i > 1000000)
913 i = 1000000;
914 if (i <= 0)
915 i = 1;
916 poll(&input, 1, i * 1000);
Denis Vlasenko339936b2007-10-05 22:11:06 +0000917 sigprocmask(SIG_BLOCK, &blocked_sigset, NULL);
Denis Vlasenko45946f82007-08-20 17:27:40 +0000918
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000919 i = ndelay_read(STDIN_FILENO, s, len);
Denis Vlasenko45946f82007-08-20 17:27:40 +0000920 if (i >= 0)
921 break;
922 if (errno == EINTR)
923 continue;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000924 if (errno != EAGAIN) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000925 warn("can't read standard input");
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000926 break;
927 }
928 /* else: EAGAIN - normal, repeat silently */
Denis Vlasenkob9528352007-05-06 01:37:21 +0000929 } while (!exitasap);
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000930
931 if (i > 0) {
932 int cnt;
933 linecomplete = (s[i-1] == '\n');
Denis Vlasenko45946f82007-08-20 17:27:40 +0000934 if (!repl)
935 return i;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000936
937 cnt = i;
938 while (--cnt >= 0) {
939 char ch = *s;
940 if (ch != '\n') {
941 if (ch < 32 || ch > 126)
942 *s = repl;
943 else {
944 int j;
945 for (j = 0; replace[j]; ++j) {
946 if (ch == replace[j]) {
947 *s = repl;
948 break;
949 }
950 }
951 }
952 }
953 s++;
954 }
955 }
956 return i;
957}
958
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000959static void sig_term_handler(int sig_no UNUSED_PARAM)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000960{
961 if (verbose)
962 bb_error_msg(INFO"sig%s received", "term");
963 exitasap = 1;
964}
965
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000966static void sig_child_handler(int sig_no UNUSED_PARAM)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000967{
Denis Vlasenko3854c5d2008-11-06 22:39:57 +0000968 pid_t pid;
969 int l;
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000970
971 if (verbose)
972 bb_error_msg(INFO"sig%s received", "child");
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000973 while ((pid = wait_any_nohang(&wstat)) > 0) {
Denis Vlasenko45946f82007-08-20 17:27:40 +0000974 for (l = 0; l < dirn; ++l) {
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000975 if (dir[l].ppid == pid) {
976 dir[l].ppid = 0;
977 processorstop(&dir[l]);
978 break;
979 }
Denis Vlasenko45946f82007-08-20 17:27:40 +0000980 }
981 }
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000982}
983
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000984static void sig_alarm_handler(int sig_no UNUSED_PARAM)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000985{
986 if (verbose)
987 bb_error_msg(INFO"sig%s received", "alarm");
988 rotateasap = 1;
989}
990
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000991static void sig_hangup_handler(int sig_no UNUSED_PARAM)
Denis Vlasenko83ea6432006-11-16 02:27:24 +0000992{
993 if (verbose)
994 bb_error_msg(INFO"sig%s received", "hangup");
995 reopenasap = 1;
996}
997
998static void logmatch(struct logdir *ld)
999{
1000 char *s;
1001
1002 ld->match = '+';
1003 ld->matcherr = 'E';
1004 s = ld->inst;
1005 while (s && s[0]) {
1006 switch (s[0]) {
1007 case '+':
1008 case '-':
1009 if (pmatch(s+1, line, linelen))
1010 ld->match = s[0];
1011 break;
1012 case 'e':
1013 case 'E':
1014 if (pmatch(s+1, line, linelen))
1015 ld->matcherr = s[0];
1016 break;
1017 }
1018 s += strlen(s) + 1;
1019 }
1020}
1021
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001022int svlogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001023int svlogd_main(int argc, char **argv)
1024{
Denys Vlasenkoaebb7422009-08-02 00:55:49 +02001025 char *r, *l, *b;
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001026 ssize_t stdin_cnt = 0;
1027 int i;
1028 unsigned opt;
1029 unsigned timestamp = 0;
1030
Denis Vlasenko339936b2007-10-05 22:11:06 +00001031 INIT_G();
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001032
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001033 opt_complementary = "tt:vv";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00001034 opt = getopt32(argv, "r:R:l:b:tv",
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001035 &r, &replace, &l, &b, &timestamp, &verbose);
1036 if (opt & 1) { // -r
1037 repl = r[0];
Denys Vlasenkoaebb7422009-08-02 00:55:49 +02001038 if (!repl || r[1])
1039 bb_show_usage();
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001040 }
1041 if (opt & 2) if (!repl) repl = '_'; // -R
1042 if (opt & 4) { // -l
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001043 linemax = xatou_range(l, 0, BUFSIZ-26);
Denys Vlasenkoaebb7422009-08-02 00:55:49 +02001044 if (linemax == 0)
1045 linemax = BUFSIZ-26;
1046 if (linemax < 256)
1047 linemax = 256;
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001048 }
Denis Vlasenko64392902007-02-03 00:53:43 +00001049 ////if (opt & 8) { // -b
Denys Vlasenko77832482010-08-12 14:14:45 +02001050 //// buflen = xatoi_positive(b);
Denis Vlasenko64392902007-02-03 00:53:43 +00001051 //// if (buflen == 0) buflen = 1024;
1052 ////}
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001053 //if (opt & 0x10) timestamp++; // -t
1054 //if (opt & 0x20) verbose++; // -v
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001055 //if (timestamp > 2) timestamp = 2;
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001056 argv += optind;
1057 argc -= optind;
1058
1059 dirn = argc;
Denys Vlasenkoaebb7422009-08-02 00:55:49 +02001060 if (dirn <= 0)
1061 bb_show_usage();
1062 ////if (buflen <= linemax) bb_show_usage();
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001063 fdwdir = xopen(".", O_RDONLY|O_NDELAY);
Denis Vlasenko96e1b382007-09-30 23:50:48 +00001064 close_on_exec_on(fdwdir);
Denys Vlasenkoaebb7422009-08-02 00:55:49 +02001065 dir = xzalloc(dirn * sizeof(dir[0]));
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001066 for (i = 0; i < dirn; ++i) {
1067 dir[i].fddir = -1;
1068 dir[i].fdcur = -1;
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001069 ////dir[i].btmp = xmalloc(buflen);
Denis Vlasenkob9528352007-05-06 01:37:21 +00001070 /*dir[i].ppid = 0;*/
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001071 }
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001072 /* line = xmalloc(linemax + (timestamp ? 26 : 0)); */
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001073 fndir = argv;
Denis Vlasenko4f8d27f2007-02-03 00:52:17 +00001074 /* We cannot set NONBLOCK on fd #0 permanently - this setting
1075 * _isn't_ per-process! It is shared among all other processes
1076 * with the same stdin */
Denis Vlasenkod37f2222007-08-19 13:42:08 +00001077 fl_flag_0 = fcntl(0, F_GETFL);
Denis Vlasenko4f8d27f2007-02-03 00:52:17 +00001078
Denis Vlasenko339936b2007-10-05 22:11:06 +00001079 sigemptyset(&blocked_sigset);
1080 sigaddset(&blocked_sigset, SIGTERM);
1081 sigaddset(&blocked_sigset, SIGCHLD);
1082 sigaddset(&blocked_sigset, SIGALRM);
1083 sigaddset(&blocked_sigset, SIGHUP);
1084 sigprocmask(SIG_BLOCK, &blocked_sigset, NULL);
Denis Vlasenkocab28aa2009-01-31 01:02:07 +00001085 bb_signals_recursive_norestart(1 << SIGTERM, sig_term_handler);
1086 bb_signals_recursive_norestart(1 << SIGCHLD, sig_child_handler);
1087 bb_signals_recursive_norestart(1 << SIGALRM, sig_alarm_handler);
1088 bb_signals_recursive_norestart(1 << SIGHUP, sig_hangup_handler);
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001089
Denis Vlasenkoeeafc1a2007-01-27 23:15:50 +00001090 /* Without timestamps, we don't have to print each line
1091 * separately, so we can look for _last_ newline, not first,
Denys Vlasenkoaebb7422009-08-02 00:55:49 +02001092 * thus batching writes. If filtering is enabled in config,
1093 * logdirs_reopen resets it to memchr.
1094 */
1095 memRchr = (timestamp ? memchr : memrchr);
1096
1097 logdirs_reopen();
Denis Vlasenkoeeafc1a2007-01-27 23:15:50 +00001098
Denis Vlasenko64392902007-02-03 00:53:43 +00001099 setvbuf(stderr, NULL, _IOFBF, linelen);
1100
Denis Vlasenko4e1715f2007-01-28 14:51:32 +00001101 /* Each iteration processes one or more lines */
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001102 while (1) {
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001103 char stamp[FMT_PTIME];
1104 char *lineptr;
1105 char *printptr;
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001106 char *np;
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001107 int printlen;
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001108 char ch;
1109
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001110 lineptr = line;
Denis Vlasenkof223efb2007-08-03 10:58:12 +00001111 if (timestamp)
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001112 lineptr += 26;
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001113
1114 /* lineptr[0..linemax-1] - buffer for stdin */
1115 /* (possibly has some unprocessed data from prev loop) */
1116
1117 /* Refill the buffer if needed */
Denis Vlasenkoeeafc1a2007-01-27 23:15:50 +00001118 np = memRchr(lineptr, '\n', stdin_cnt);
1119 if (!np && !exitasap) {
1120 i = linemax - stdin_cnt; /* avail. bytes at tail */
1121 if (i >= 128) {
Denis Vlasenko5d61e712007-09-27 10:09:59 +00001122 i = buffer_pread(/*0, */lineptr + stdin_cnt, i);
Denis Vlasenkoeeafc1a2007-01-27 23:15:50 +00001123 if (i <= 0) /* EOF or error on stdin */
1124 exitasap = 1;
1125 else {
1126 np = memRchr(lineptr + stdin_cnt, '\n', i);
1127 stdin_cnt += i;
1128 }
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001129 }
1130 }
1131 if (stdin_cnt <= 0 && exitasap)
1132 break;
1133
1134 /* Search for '\n' (in fact, np already holds the result) */
1135 linelen = stdin_cnt;
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001136 if (np) {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +02001137 print_to_nl:
1138 /* NB: starting from here lineptr may point
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001139 * farther out into line[] */
1140 linelen = np - lineptr + 1;
1141 }
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001142 /* linelen == no of chars incl. '\n' (or == stdin_cnt) */
1143 ch = lineptr[linelen-1];
1144
Denis Vlasenko64392902007-02-03 00:53:43 +00001145 /* Biggest performance hit was coming from the fact
1146 * that we did not buffer writes. We were reading many lines
1147 * in one read() above, but wrote one line per write().
1148 * We are using stdio to fix that */
Denis Vlasenko4f8d27f2007-02-03 00:52:17 +00001149
1150 /* write out lineptr[0..linelen-1] to each log destination
1151 * (or lineptr[-26..linelen-1] if timestamping) */
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001152 printlen = linelen;
1153 printptr = lineptr;
1154 if (timestamp) {
Denis Vlasenkof223efb2007-08-03 10:58:12 +00001155 if (timestamp == 1)
Denis Vlasenko45946f82007-08-20 17:27:40 +00001156 fmt_time_bernstein_25(stamp);
Denis Vlasenkof223efb2007-08-03 10:58:12 +00001157 else /* 2: */
Denis Vlasenko45946f82007-08-20 17:27:40 +00001158 fmt_time_human_30nul(stamp);
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001159 printlen += 26;
1160 printptr -= 26;
1161 memcpy(printptr, stamp, 25);
1162 printptr[25] = ' ';
1163 }
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001164 for (i = 0; i < dirn; ++i) {
1165 struct logdir *ld = &dir[i];
Denys Vlasenkoaebb7422009-08-02 00:55:49 +02001166 if (ld->fddir == -1)
1167 continue;
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001168 if (ld->inst)
1169 logmatch(ld);
Denis Vlasenko7ab5e3d2007-10-22 15:53:34 +00001170 if (ld->matcherr == 'e') {
1171 /* runit-1.8.0 compat: if timestamping, do it on stderr too */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +00001172 ////full_write(STDERR_FILENO, printptr, printlen);
Denis Vlasenko7ab5e3d2007-10-22 15:53:34 +00001173 fwrite(printptr, 1, printlen, stderr);
1174 }
Denys Vlasenkoaebb7422009-08-02 00:55:49 +02001175 if (ld->match != '+')
1176 continue;
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001177 buffer_pwrite(i, printptr, printlen);
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001178 }
1179
1180 /* If we didn't see '\n' (long input line), */
1181 /* read/write repeatedly until we see it */
1182 while (ch != '\n') {
1183 /* lineptr is emptied now, safe to use as buffer */
Denis Vlasenko5d61e712007-09-27 10:09:59 +00001184 stdin_cnt = exitasap ? -1 : buffer_pread(/*0, */lineptr, linemax);
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001185 if (stdin_cnt <= 0) { /* EOF or error on stdin */
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001186 exitasap = 1;
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001187 lineptr[0] = ch = '\n';
1188 linelen = 1;
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001189 stdin_cnt = 1;
1190 } else {
1191 linelen = stdin_cnt;
Denis Vlasenkoeeafc1a2007-01-27 23:15:50 +00001192 np = memRchr(lineptr, '\n', stdin_cnt);
1193 if (np)
1194 linelen = np - lineptr + 1;
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001195 ch = lineptr[linelen-1];
1196 }
1197 /* linelen == no of chars incl. '\n' (or == stdin_cnt) */
1198 for (i = 0; i < dirn; ++i) {
Denys Vlasenkoaebb7422009-08-02 00:55:49 +02001199 if (dir[i].fddir == -1)
1200 continue;
Denis Vlasenko7ab5e3d2007-10-22 15:53:34 +00001201 if (dir[i].matcherr == 'e') {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +00001202 ////full_write(STDERR_FILENO, lineptr, linelen);
Denis Vlasenko64392902007-02-03 00:53:43 +00001203 fwrite(lineptr, 1, linelen, stderr);
Denis Vlasenko7ab5e3d2007-10-22 15:53:34 +00001204 }
Denys Vlasenkoaebb7422009-08-02 00:55:49 +02001205 if (dir[i].match != '+')
1206 continue;
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001207 buffer_pwrite(i, lineptr, linelen);
1208 }
1209 }
1210
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001211 stdin_cnt -= linelen;
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001212 if (stdin_cnt > 0) {
1213 lineptr += linelen;
1214 /* If we see another '\n', we don't need to read
1215 * next piece of input: can print what we have */
Denis Vlasenkoeeafc1a2007-01-27 23:15:50 +00001216 np = memRchr(lineptr, '\n', stdin_cnt);
Denis Vlasenkoca549c52007-01-27 22:24:59 +00001217 if (np)
1218 goto print_to_nl;
1219 /* Move unprocessed data to the front of line */
1220 memmove((timestamp ? line+26 : line), lineptr, stdin_cnt);
1221 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001222 fflush_all();////
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001223 }
1224
1225 for (i = 0; i < dirn; ++i) {
1226 if (dir[i].ppid)
1227 while (!processorstop(&dir[i]))
Denys Vlasenkoaebb7422009-08-02 00:55:49 +02001228 continue;
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001229 logdir_close(&dir[i]);
1230 }
Denis Vlasenkoeeafc1a2007-01-27 23:15:50 +00001231 return 0;
Denis Vlasenko83ea6432006-11-16 02:27:24 +00001232}