Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2001-2006, Gerrit Pape |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, 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 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Denis Vlasenko | d18f52b | 2008-03-02 12:53:15 +0000 | [diff] [blame] | 28 | /* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */ |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 29 | /* TODO: depends on runit_lib.c - review and reduce/eliminate */ |
| 30 | |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 31 | /* |
| 32 | Config files |
| 33 | |
| 34 | On startup, and after receiving a HUP signal, svlogd checks for each |
| 35 | log directory log if the configuration file log/config exists, |
| 36 | and if so, reads the file line by line and adjusts configuration |
| 37 | for log as follows: |
| 38 | |
| 39 | If the line is empty, or starts with a #, it is ignored. A line |
| 40 | of the form |
| 41 | |
| 42 | ssize |
| 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). |
| 47 | nnum |
| 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. |
| 52 | Nmin |
| 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. |
| 57 | ttimeout |
| 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. |
| 64 | ua.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. |
| 72 | Ua.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. |
| 77 | pprefix |
| 78 | tells svlogd to prefix each line to be written to the log directory, |
| 79 | to standard error, or through UDP, with prefix. |
| 80 | |
| 81 | If a line starts with a -, +, e, or E, svlogd matches the first len characters |
| 82 | of 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. |
| 88 | epattern |
| 89 | the log message is selected to be printed to standard error. |
| 90 | Epattern |
| 91 | the log message is deselected to be printed to standard error. |
| 92 | |
| 93 | Initially each line is selected to be written to log/current. Deselected |
| 94 | log messages are discarded from log. Initially each line is deselected |
| 95 | to be written to standard err. Log messages selected for standard error |
| 96 | are written to standard error. |
| 97 | |
| 98 | Pattern Matching |
| 99 | |
| 100 | svlogd matches a log message against the string pattern as follows: |
| 101 | |
| 102 | pattern is applied to the log message one character by one, starting |
| 103 | with the first. A character not a star (*) and not a plus (+) matches itself. |
| 104 | A plus matches the next character in pattern in the log message one |
| 105 | or more times. A star before the end of pattern matches any string |
| 106 | in the log message that does not include the next character in pattern. |
| 107 | A star at the end of pattern matches any string. |
| 108 | |
| 109 | Timestamps optionally added by svlogd are not considered part |
| 110 | of the log message. |
| 111 | |
| 112 | An svlogd pattern is not a regular expression. For example consider |
| 113 | a log message like this |
| 114 | |
| 115 | 2005-12-18_09:13:50.97618 tcpsvd: info: pid 1977 from 10.4.1.14 |
| 116 | |
| 117 | The following pattern doesnt match |
| 118 | |
| 119 | -*pid* |
| 120 | |
| 121 | because the first star matches up to the first p in tcpsvd, |
| 122 | and then the match fails because i is not s. To match this |
| 123 | log message, you can use a pattern like this instead |
| 124 | |
| 125 | -*: *: pid * |
| 126 | */ |
| 127 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 128 | #include <sys/poll.h> |
| 129 | #include <sys/file.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 130 | #include "libbb.h" |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 131 | #include "runit_lib.h" |
| 132 | |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 133 | #define LESS(a,b) ((int)((unsigned)(b) - (unsigned)(a)) > 0) |
| 134 | |
| 135 | #define FMT_PTIME 30 |
| 136 | |
Denis Vlasenko | 339936b | 2007-10-05 22:11:06 +0000 | [diff] [blame] | 137 | struct logdir { |
Denis Vlasenko | 6439290 | 2007-02-03 00:53:43 +0000 | [diff] [blame] | 138 | ////char *btmp; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 139 | /* pattern list to match, in "aa\0bb\0\cc\0\0" form */ |
| 140 | char *inst; |
| 141 | char *processor; |
| 142 | char *name; |
| 143 | unsigned size; |
| 144 | unsigned sizemax; |
| 145 | unsigned nmax; |
| 146 | unsigned nmin; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 147 | unsigned rotate_period; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 148 | int ppid; |
| 149 | int fddir; |
| 150 | int fdcur; |
Denis Vlasenko | 6439290 | 2007-02-03 00:53:43 +0000 | [diff] [blame] | 151 | FILE* filecur; //// |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 152 | int fdlock; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 153 | unsigned next_rotate; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 154 | char fnsave[FMT_PTIME]; |
| 155 | char match; |
| 156 | char matcherr; |
Denis Vlasenko | 339936b | 2007-10-05 22:11:06 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | |
| 160 | struct globals { |
| 161 | struct logdir *dir; |
| 162 | unsigned verbose; |
| 163 | int linemax; |
| 164 | ////int buflen; |
| 165 | int linelen; |
| 166 | |
| 167 | int fdwdir; |
| 168 | char **fndir; |
| 169 | int wstat; |
| 170 | unsigned nearest_rotate; |
| 171 | |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 172 | void* (*memRchr)(const void *, int, size_t); |
| 173 | |
Denis Vlasenko | 339936b | 2007-10-05 22:11:06 +0000 | [diff] [blame] | 174 | smallint exitasap; |
| 175 | smallint rotateasap; |
| 176 | smallint reopenasap; |
| 177 | smallint linecomplete; |
| 178 | smallint tmaxflag; |
| 179 | |
| 180 | char repl; |
| 181 | const char *replace; |
| 182 | int fl_flag_0; |
| 183 | unsigned dirn; |
| 184 | |
| 185 | sigset_t blocked_sigset; |
| 186 | }; |
| 187 | #define G (*(struct globals*)ptr_to_globals) |
| 188 | #define dir (G.dir ) |
| 189 | #define verbose (G.verbose ) |
| 190 | #define linemax (G.linemax ) |
| 191 | #define buflen (G.buflen ) |
| 192 | #define linelen (G.linelen ) |
| 193 | #define fndir (G.fndir ) |
| 194 | #define fdwdir (G.fdwdir ) |
| 195 | #define wstat (G.wstat ) |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 196 | #define memRchr (G.memRchr ) |
Denis Vlasenko | 339936b | 2007-10-05 22:11:06 +0000 | [diff] [blame] | 197 | #define nearest_rotate (G.nearest_rotate) |
| 198 | #define exitasap (G.exitasap ) |
| 199 | #define rotateasap (G.rotateasap ) |
| 200 | #define reopenasap (G.reopenasap ) |
| 201 | #define linecomplete (G.linecomplete ) |
| 202 | #define tmaxflag (G.tmaxflag ) |
| 203 | #define repl (G.repl ) |
| 204 | #define replace (G.replace ) |
| 205 | #define blocked_sigset (G.blocked_sigset) |
| 206 | #define fl_flag_0 (G.fl_flag_0 ) |
| 207 | #define dirn (G.dirn ) |
| 208 | #define INIT_G() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 209 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denis Vlasenko | 339936b | 2007-10-05 22:11:06 +0000 | [diff] [blame] | 210 | linemax = 1000; \ |
| 211 | /*buflen = 1024;*/ \ |
| 212 | linecomplete = 1; \ |
| 213 | replace = ""; \ |
| 214 | } while (0) |
| 215 | |
| 216 | #define line bb_common_bufsiz1 |
| 217 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 218 | |
| 219 | #define FATAL "fatal: " |
| 220 | #define WARNING "warning: " |
| 221 | #define PAUSE "pausing: " |
| 222 | #define INFO "info: " |
| 223 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 224 | static void fatalx(const char *m0) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 225 | { |
| 226 | bb_error_msg_and_die(FATAL"%s", m0); |
| 227 | } |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 228 | static void warn(const char *m0) |
| 229 | { |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 230 | bb_perror_msg(WARNING"%s", m0); |
| 231 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 232 | static void warn2(const char *m0, const char *m1) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 233 | { |
| 234 | bb_perror_msg(WARNING"%s: %s", m0, m1); |
| 235 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 236 | static void warnx(const char *m0, const char *m1) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 237 | { |
| 238 | bb_error_msg(WARNING"%s: %s", m0, m1); |
| 239 | } |
| 240 | static void pause_nomem(void) |
| 241 | { |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 242 | bb_error_msg(PAUSE"out of memory"); |
| 243 | sleep(3); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 244 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 245 | static void pause1cannot(const char *m0) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 246 | { |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 247 | bb_perror_msg(PAUSE"can't %s", m0); |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 248 | sleep(3); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 249 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 250 | static void pause2cannot(const char *m0, const char *m1) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 251 | { |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 252 | bb_perror_msg(PAUSE"can't %s %s", m0, m1); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 253 | sleep(3); |
| 254 | } |
| 255 | |
| 256 | static char* wstrdup(const char *str) |
| 257 | { |
| 258 | char *s; |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 259 | while (!(s = strdup(str))) |
| 260 | pause_nomem(); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 261 | return s; |
| 262 | } |
| 263 | |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 264 | /*** ex fmt_ptime.[ch] ***/ |
| 265 | |
| 266 | /* NUL terminated */ |
| 267 | static void fmt_time_human_30nul(char *s) |
| 268 | { |
| 269 | struct tm *t; |
| 270 | struct timeval tv; |
| 271 | |
| 272 | gettimeofday(&tv, NULL); |
| 273 | t = gmtime(&(tv.tv_sec)); |
| 274 | sprintf(s, "%04u-%02u-%02u_%02u:%02u:%02u.%06u000", |
| 275 | (unsigned)(1900 + t->tm_year), |
| 276 | (unsigned)(t->tm_mon + 1), |
| 277 | (unsigned)(t->tm_mday), |
| 278 | (unsigned)(t->tm_hour), |
| 279 | (unsigned)(t->tm_min), |
| 280 | (unsigned)(t->tm_sec), |
| 281 | (unsigned)(tv.tv_usec) |
| 282 | ); |
| 283 | /* 4+1 + 2+1 + 2+1 + 2+1 + 2+1 + 2+1 + 9 = */ |
| 284 | /* 5 + 3 + 3 + 3 + 3 + 3 + 9 = */ |
| 285 | /* 20 (up to '.' inclusive) + 9 (not including '\0') */ |
| 286 | } |
| 287 | |
| 288 | /* NOT terminated! */ |
| 289 | static void fmt_time_bernstein_25(char *s) |
| 290 | { |
| 291 | uint32_t pack[3]; |
| 292 | struct timeval tv; |
| 293 | unsigned sec_hi; |
| 294 | |
| 295 | gettimeofday(&tv, NULL); |
| 296 | sec_hi = (0x400000000000000aULL + tv.tv_sec) >> 32; |
| 297 | tv.tv_sec = (time_t)(0x400000000000000aULL) + tv.tv_sec; |
| 298 | tv.tv_usec *= 1000; |
| 299 | /* Network order is big-endian: most significant byte first. |
| 300 | * This is exactly what we want here */ |
| 301 | pack[0] = htonl(sec_hi); |
| 302 | pack[1] = htonl(tv.tv_sec); |
| 303 | pack[2] = htonl(tv.tv_usec); |
| 304 | *s++ = '@'; |
| 305 | bin2hex(s, (char*)pack, 12); |
| 306 | } |
| 307 | |
Denis Vlasenko | 4ee7cd4 | 2008-03-17 09:13:22 +0000 | [diff] [blame] | 308 | static void processorstart(struct logdir *ld) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 309 | { |
Denis Vlasenko | 4ee7cd4 | 2008-03-17 09:13:22 +0000 | [diff] [blame] | 310 | char sv_ch; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 311 | int pid; |
| 312 | |
Denis Vlasenko | 4ee7cd4 | 2008-03-17 09:13:22 +0000 | [diff] [blame] | 313 | if (!ld->processor) return; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 314 | if (ld->ppid) { |
| 315 | warnx("processor already running", ld->name); |
Denis Vlasenko | 4ee7cd4 | 2008-03-17 09:13:22 +0000 | [diff] [blame] | 316 | return; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 317 | } |
Denis Vlasenko | 4ee7cd4 | 2008-03-17 09:13:22 +0000 | [diff] [blame] | 318 | |
| 319 | /* vfork'ed child trashes this byte, save... */ |
| 320 | sv_ch = ld->fnsave[26]; |
| 321 | |
| 322 | while ((pid = vfork()) == -1) |
| 323 | pause2cannot("vfork for processor", ld->name); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 324 | if (!pid) { |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 325 | int fd; |
| 326 | |
| 327 | /* child */ |
Denis Vlasenko | 3fa36e2 | 2008-11-09 00:15:11 +0000 | [diff] [blame] | 328 | /* Non-ignored signals revert to SIG_DFL on exec anyway */ |
| 329 | /*bb_signals(0 |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 330 | + (1 << SIGTERM) |
| 331 | + (1 << SIGALRM) |
| 332 | + (1 << SIGHUP) |
Denis Vlasenko | 3fa36e2 | 2008-11-09 00:15:11 +0000 | [diff] [blame] | 333 | , SIG_DFL);*/ |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 334 | sig_unblock(SIGTERM); |
| 335 | sig_unblock(SIGALRM); |
| 336 | sig_unblock(SIGHUP); |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 337 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 338 | if (verbose) |
| 339 | bb_error_msg(INFO"processing: %s/%s", ld->name, ld->fnsave); |
| 340 | fd = xopen(ld->fnsave, O_RDONLY|O_NDELAY); |
Denis Vlasenko | cad04ef | 2007-03-25 23:21:05 +0000 | [diff] [blame] | 341 | xmove_fd(fd, 0); |
Denis Vlasenko | 4ee7cd4 | 2008-03-17 09:13:22 +0000 | [diff] [blame] | 342 | ld->fnsave[26] = 't'; /* <- that's why we need sv_ch! */ |
Denis Vlasenko | cf749bc | 2006-11-26 15:45:17 +0000 | [diff] [blame] | 343 | fd = xopen(ld->fnsave, O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT); |
Denis Vlasenko | cad04ef | 2007-03-25 23:21:05 +0000 | [diff] [blame] | 344 | xmove_fd(fd, 1); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 345 | fd = open_read("state"); |
| 346 | if (fd == -1) { |
| 347 | if (errno != ENOENT) |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 348 | bb_perror_msg_and_die(FATAL"can't %s processor %s", "open state for", ld->name); |
Denis Vlasenko | cf749bc | 2006-11-26 15:45:17 +0000 | [diff] [blame] | 349 | close(xopen("state", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT)); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 350 | fd = xopen("state", O_RDONLY|O_NDELAY); |
| 351 | } |
Denis Vlasenko | cad04ef | 2007-03-25 23:21:05 +0000 | [diff] [blame] | 352 | xmove_fd(fd, 4); |
Denis Vlasenko | cf749bc | 2006-11-26 15:45:17 +0000 | [diff] [blame] | 353 | fd = xopen("newstate", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT); |
Denis Vlasenko | cad04ef | 2007-03-25 23:21:05 +0000 | [diff] [blame] | 354 | xmove_fd(fd, 5); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 355 | |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 356 | // getenv("SHELL")? |
Denys Vlasenko | c5d07fb | 2009-07-03 18:31:23 +0200 | [diff] [blame] | 357 | execl("/bin/sh", "/bin/sh" + 5, "-c", ld->processor, (char*) NULL); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 358 | bb_perror_msg_and_die(FATAL"can't %s processor %s", "run", ld->name); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 359 | } |
Denis Vlasenko | 4ee7cd4 | 2008-03-17 09:13:22 +0000 | [diff] [blame] | 360 | ld->fnsave[26] = sv_ch; /* ...restore */ |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 361 | ld->ppid = pid; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static unsigned processorstop(struct logdir *ld) |
| 365 | { |
| 366 | char f[28]; |
| 367 | |
| 368 | if (ld->ppid) { |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 369 | sig_unblock(SIGHUP); |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 370 | while (safe_waitpid(ld->ppid, &wstat, 0) == -1) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 371 | pause2cannot("wait for processor", ld->name); |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 372 | sig_block(SIGHUP); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 373 | ld->ppid = 0; |
| 374 | } |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 375 | if (ld->fddir == -1) |
| 376 | return 1; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 377 | while (fchdir(ld->fddir) == -1) |
| 378 | pause2cannot("change directory, want processor", ld->name); |
Denys Vlasenko | 8f24f98 | 2009-06-07 16:02:00 +0200 | [diff] [blame] | 379 | if (WEXITSTATUS(wstat) != 0) { |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 380 | warnx("processor failed, restart", ld->name); |
| 381 | ld->fnsave[26] = 't'; |
| 382 | unlink(ld->fnsave); |
| 383 | ld->fnsave[26] = 'u'; |
| 384 | processorstart(ld); |
| 385 | while (fchdir(fdwdir) == -1) |
| 386 | pause1cannot("change to initial working directory"); |
| 387 | return ld->processor ? 0 : 1; |
| 388 | } |
| 389 | ld->fnsave[26] = 't'; |
| 390 | memcpy(f, ld->fnsave, 26); |
| 391 | f[26] = 's'; |
| 392 | f[27] = '\0'; |
| 393 | while (rename(ld->fnsave, f) == -1) |
| 394 | pause2cannot("rename processed", ld->name); |
| 395 | while (chmod(f, 0744) == -1) |
| 396 | pause2cannot("set mode of processed", ld->name); |
| 397 | ld->fnsave[26] = 'u'; |
| 398 | if (unlink(ld->fnsave) == -1) |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 399 | bb_error_msg(WARNING"can't unlink: %s/%s", ld->name, ld->fnsave); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 400 | while (rename("newstate", "state") == -1) |
| 401 | pause2cannot("rename state", ld->name); |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 402 | if (verbose) |
| 403 | bb_error_msg(INFO"processed: %s/%s", ld->name, f); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 404 | while (fchdir(fdwdir) == -1) |
| 405 | pause1cannot("change to initial working directory"); |
| 406 | return 1; |
| 407 | } |
| 408 | |
| 409 | static void rmoldest(struct logdir *ld) |
| 410 | { |
| 411 | DIR *d; |
| 412 | struct dirent *f; |
| 413 | char oldest[FMT_PTIME]; |
| 414 | int n = 0; |
| 415 | |
| 416 | oldest[0] = 'A'; oldest[1] = oldest[27] = 0; |
| 417 | while (!(d = opendir("."))) |
| 418 | pause2cannot("open directory, want rotate", ld->name); |
| 419 | errno = 0; |
| 420 | while ((f = readdir(d))) { |
| 421 | if ((f->d_name[0] == '@') && (strlen(f->d_name) == 27)) { |
| 422 | if (f->d_name[26] == 't') { |
| 423 | if (unlink(f->d_name) == -1) |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 424 | warn2("can't unlink processor leftover", f->d_name); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 425 | } else { |
| 426 | ++n; |
| 427 | if (strcmp(f->d_name, oldest) < 0) |
| 428 | memcpy(oldest, f->d_name, 27); |
| 429 | } |
| 430 | errno = 0; |
| 431 | } |
| 432 | } |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 433 | if (errno) |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 434 | warn2("can't read directory", ld->name); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 435 | closedir(d); |
| 436 | |
| 437 | if (ld->nmax && (n > ld->nmax)) { |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 438 | if (verbose) |
| 439 | bb_error_msg(INFO"delete: %s/%s", ld->name, oldest); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 440 | if ((*oldest == '@') && (unlink(oldest) == -1)) |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 441 | warn2("can't unlink oldest logfile", ld->name); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
| 445 | static unsigned rotate(struct logdir *ld) |
| 446 | { |
| 447 | struct stat st; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 448 | unsigned now; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 449 | |
| 450 | if (ld->fddir == -1) { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 451 | ld->rotate_period = 0; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | if (ld->ppid) |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 455 | while (!processorstop(ld)) |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 456 | continue; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 457 | |
| 458 | while (fchdir(ld->fddir) == -1) |
| 459 | pause2cannot("change directory, want rotate", ld->name); |
| 460 | |
| 461 | /* create new filename */ |
| 462 | ld->fnsave[25] = '.'; |
| 463 | ld->fnsave[26] = 's'; |
| 464 | if (ld->processor) |
| 465 | ld->fnsave[26] = 'u'; |
| 466 | ld->fnsave[27] = '\0'; |
| 467 | do { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 468 | fmt_time_bernstein_25(ld->fnsave); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 469 | errno = 0; |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 470 | stat(ld->fnsave, &st); |
| 471 | } while (errno != ENOENT); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 472 | |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 473 | now = monotonic_sec(); |
| 474 | if (ld->rotate_period && LESS(ld->next_rotate, now)) { |
| 475 | ld->next_rotate = now + ld->rotate_period; |
| 476 | if (LESS(ld->next_rotate, nearest_rotate)) |
| 477 | nearest_rotate = ld->next_rotate; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | if (ld->size > 0) { |
Denis Vlasenko | 6439290 | 2007-02-03 00:53:43 +0000 | [diff] [blame] | 481 | while (fflush(ld->filecur) || fsync(ld->fdcur) == -1) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 482 | pause2cannot("fsync current logfile", ld->name); |
| 483 | while (fchmod(ld->fdcur, 0744) == -1) |
| 484 | pause2cannot("set mode of current", ld->name); |
Denis Vlasenko | 6439290 | 2007-02-03 00:53:43 +0000 | [diff] [blame] | 485 | ////close(ld->fdcur); |
| 486 | fclose(ld->filecur); |
| 487 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 488 | if (verbose) { |
| 489 | bb_error_msg(INFO"rename: %s/current %s %u", ld->name, |
| 490 | ld->fnsave, ld->size); |
| 491 | } |
| 492 | while (rename("current", ld->fnsave) == -1) |
| 493 | pause2cannot("rename current", ld->name); |
| 494 | while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1) |
| 495 | pause2cannot("create new current", ld->name); |
Denys Vlasenko | c5d07fb | 2009-07-03 18:31:23 +0200 | [diff] [blame] | 496 | while ((ld->filecur = fdopen(ld->fdcur, "a")) == NULL) //// |
| 497 | pause2cannot("create new current", ld->name); /* very unlikely */ |
Denis Vlasenko | 6439290 | 2007-02-03 00:53:43 +0000 | [diff] [blame] | 498 | setvbuf(ld->filecur, NULL, _IOFBF, linelen); //// |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 499 | close_on_exec_on(ld->fdcur); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 500 | ld->size = 0; |
| 501 | while (fchmod(ld->fdcur, 0644) == -1) |
| 502 | pause2cannot("set mode of current", ld->name); |
Denys Vlasenko | c5d07fb | 2009-07-03 18:31:23 +0200 | [diff] [blame] | 503 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 504 | rmoldest(ld); |
| 505 | processorstart(ld); |
| 506 | } |
| 507 | |
| 508 | while (fchdir(fdwdir) == -1) |
| 509 | pause1cannot("change to initial working directory"); |
| 510 | return 1; |
| 511 | } |
| 512 | |
| 513 | static int buffer_pwrite(int n, char *s, unsigned len) |
| 514 | { |
| 515 | int i; |
| 516 | struct logdir *ld = &dir[n]; |
| 517 | |
| 518 | if (ld->sizemax) { |
| 519 | if (ld->size >= ld->sizemax) |
| 520 | rotate(ld); |
| 521 | if (len > (ld->sizemax - ld->size)) |
| 522 | len = ld->sizemax - ld->size; |
| 523 | } |
Denis Vlasenko | 6439290 | 2007-02-03 00:53:43 +0000 | [diff] [blame] | 524 | while (1) { |
| 525 | ////i = full_write(ld->fdcur, s, len); |
| 526 | ////if (i != -1) break; |
| 527 | i = fwrite(s, 1, len, ld->filecur); |
| 528 | if (i == len) break; |
| 529 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 530 | if ((errno == ENOSPC) && (ld->nmin < ld->nmax)) { |
| 531 | DIR *d; |
| 532 | struct dirent *f; |
| 533 | char oldest[FMT_PTIME]; |
| 534 | int j = 0; |
| 535 | |
| 536 | while (fchdir(ld->fddir) == -1) |
| 537 | pause2cannot("change directory, want remove old logfile", |
| 538 | ld->name); |
| 539 | oldest[0] = 'A'; |
| 540 | oldest[1] = oldest[27] = '\0'; |
| 541 | while (!(d = opendir("."))) |
| 542 | pause2cannot("open directory, want remove old logfile", |
| 543 | ld->name); |
| 544 | errno = 0; |
| 545 | while ((f = readdir(d))) |
| 546 | if ((f->d_name[0] == '@') && (strlen(f->d_name) == 27)) { |
| 547 | ++j; |
| 548 | if (strcmp(f->d_name, oldest) < 0) |
| 549 | memcpy(oldest, f->d_name, 27); |
| 550 | } |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 551 | if (errno) warn2("can't read directory, want remove old logfile", |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 552 | ld->name); |
| 553 | closedir(d); |
| 554 | errno = ENOSPC; |
| 555 | if (j > ld->nmin) { |
| 556 | if (*oldest == '@') { |
| 557 | bb_error_msg(WARNING"out of disk space, delete: %s/%s", |
| 558 | ld->name, oldest); |
| 559 | errno = 0; |
| 560 | if (unlink(oldest) == -1) { |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 561 | warn2("can't unlink oldest logfile", ld->name); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 562 | errno = ENOSPC; |
| 563 | } |
| 564 | while (fchdir(fdwdir) == -1) |
| 565 | pause1cannot("change to initial working directory"); |
| 566 | } |
| 567 | } |
| 568 | } |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 569 | if (errno) |
| 570 | pause2cannot("write to current", ld->name); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | ld->size += i; |
| 574 | if (ld->sizemax) |
| 575 | if (s[i-1] == '\n') |
| 576 | if (ld->size >= (ld->sizemax - linemax)) |
| 577 | rotate(ld); |
| 578 | return i; |
| 579 | } |
| 580 | |
| 581 | static void logdir_close(struct logdir *ld) |
| 582 | { |
| 583 | if (ld->fddir == -1) |
| 584 | return; |
| 585 | if (verbose) |
| 586 | bb_error_msg(INFO"close: %s", ld->name); |
| 587 | close(ld->fddir); |
| 588 | ld->fddir = -1; |
| 589 | if (ld->fdcur == -1) |
| 590 | return; /* impossible */ |
Denis Vlasenko | 6439290 | 2007-02-03 00:53:43 +0000 | [diff] [blame] | 591 | while (fflush(ld->filecur) || fsync(ld->fdcur) == -1) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 592 | pause2cannot("fsync current logfile", ld->name); |
| 593 | while (fchmod(ld->fdcur, 0744) == -1) |
| 594 | pause2cannot("set mode of current", ld->name); |
Denis Vlasenko | 6439290 | 2007-02-03 00:53:43 +0000 | [diff] [blame] | 595 | ////close(ld->fdcur); |
| 596 | fclose(ld->filecur); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 597 | ld->fdcur = -1; |
| 598 | if (ld->fdlock == -1) |
| 599 | return; /* impossible */ |
| 600 | close(ld->fdlock); |
| 601 | ld->fdlock = -1; |
| 602 | free(ld->processor); |
| 603 | ld->processor = NULL; |
| 604 | } |
| 605 | |
Denys Vlasenko | a7bb3c1 | 2009-10-08 12:28:08 +0200 | [diff] [blame^] | 606 | static NOINLINE unsigned logdir_open(struct logdir *ld, const char *fn) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 607 | { |
| 608 | char buf[128]; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 609 | unsigned now; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 610 | char *new, *s, *np; |
| 611 | int i; |
| 612 | struct stat st; |
| 613 | |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 614 | now = monotonic_sec(); |
| 615 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 616 | ld->fddir = open(fn, O_RDONLY|O_NDELAY); |
| 617 | if (ld->fddir == -1) { |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 618 | warn2("can't open log directory", (char*)fn); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 619 | return 0; |
| 620 | } |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 621 | close_on_exec_on(ld->fddir); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 622 | if (fchdir(ld->fddir) == -1) { |
| 623 | logdir_close(ld); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 624 | warn2("can't change directory", (char*)fn); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 625 | return 0; |
| 626 | } |
| 627 | ld->fdlock = open("lock", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600); |
| 628 | if ((ld->fdlock == -1) |
| 629 | || (lock_exnb(ld->fdlock) == -1) |
| 630 | ) { |
| 631 | logdir_close(ld); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 632 | warn2("can't lock directory", (char*)fn); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 633 | while (fchdir(fdwdir) == -1) |
| 634 | pause1cannot("change to initial working directory"); |
| 635 | return 0; |
| 636 | } |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 637 | close_on_exec_on(ld->fdlock); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 638 | |
| 639 | ld->size = 0; |
| 640 | ld->sizemax = 1000000; |
| 641 | ld->nmax = ld->nmin = 10; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 642 | ld->rotate_period = 0; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 643 | ld->name = (char*)fn; |
| 644 | ld->ppid = 0; |
| 645 | ld->match = '+'; |
| 646 | free(ld->inst); ld->inst = NULL; |
| 647 | free(ld->processor); ld->processor = NULL; |
| 648 | |
| 649 | /* read config */ |
Denys Vlasenko | c5d07fb | 2009-07-03 18:31:23 +0200 | [diff] [blame] | 650 | i = open_read_close("config", buf, sizeof(buf) - 1); |
Denis Vlasenko | f223efb | 2007-08-03 10:58:12 +0000 | [diff] [blame] | 651 | if (i < 0 && errno != ENOENT) |
Denis Vlasenko | 72b6a65 | 2007-08-21 11:18:25 +0000 | [diff] [blame] | 652 | bb_perror_msg(WARNING"%s/config", ld->name); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 653 | if (i > 0) { |
Denys Vlasenko | c5d07fb | 2009-07-03 18:31:23 +0200 | [diff] [blame] | 654 | buf[i] = '\0'; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 655 | if (verbose) |
| 656 | bb_error_msg(INFO"read: %s/config", ld->name); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 657 | s = buf; |
| 658 | while (s) { |
| 659 | np = strchr(s, '\n'); |
Denis Vlasenko | 72b6a65 | 2007-08-21 11:18:25 +0000 | [diff] [blame] | 660 | if (np) |
| 661 | *np++ = '\0'; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 662 | switch (s[0]) { |
| 663 | case '+': |
| 664 | case '-': |
| 665 | case 'e': |
| 666 | case 'E': |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 667 | /* Filtering requires one-line buffering, |
| 668 | * resetting the "find newline" function |
| 669 | * accordingly */ |
| 670 | memRchr = memchr; |
Denis Vlasenko | 72b6a65 | 2007-08-21 11:18:25 +0000 | [diff] [blame] | 671 | /* Add '\n'-terminated line to ld->inst */ |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 672 | while (1) { |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 673 | int l = asprintf(&new, "%s%s\n", ld->inst ? ld->inst : "", s); |
Denis Vlasenko | 72b6a65 | 2007-08-21 11:18:25 +0000 | [diff] [blame] | 674 | if (l >= 0 && new) |
| 675 | break; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 676 | pause_nomem(); |
| 677 | } |
| 678 | free(ld->inst); |
| 679 | ld->inst = new; |
| 680 | break; |
| 681 | case 's': { |
| 682 | static const struct suffix_mult km_suffixes[] = { |
Denis Vlasenko | f868963 | 2007-07-27 15:06:25 +0000 | [diff] [blame] | 683 | { "k", 1024 }, |
| 684 | { "m", 1024*1024 }, |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 685 | { "", 0 } |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 686 | }; |
| 687 | ld->sizemax = xatou_sfx(&s[1], km_suffixes); |
| 688 | break; |
| 689 | } |
| 690 | case 'n': |
| 691 | ld->nmax = xatoi_u(&s[1]); |
| 692 | break; |
| 693 | case 'N': |
| 694 | ld->nmin = xatoi_u(&s[1]); |
| 695 | break; |
| 696 | case 't': { |
| 697 | static const struct suffix_mult mh_suffixes[] = { |
Denis Vlasenko | f868963 | 2007-07-27 15:06:25 +0000 | [diff] [blame] | 698 | { "m", 60 }, |
| 699 | { "h", 60*60 }, |
| 700 | /*{ "d", 24*60*60 },*/ |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 701 | { "", 0 } |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 702 | }; |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 703 | ld->rotate_period = xatou_sfx(&s[1], mh_suffixes); |
| 704 | if (ld->rotate_period) { |
| 705 | ld->next_rotate = now + ld->rotate_period; |
| 706 | if (!tmaxflag || LESS(ld->next_rotate, nearest_rotate)) |
| 707 | nearest_rotate = ld->next_rotate; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 708 | tmaxflag = 1; |
| 709 | } |
| 710 | break; |
| 711 | } |
| 712 | case '!': |
| 713 | if (s[1]) { |
| 714 | free(ld->processor); |
| 715 | ld->processor = wstrdup(s); |
| 716 | } |
| 717 | break; |
| 718 | } |
| 719 | s = np; |
| 720 | } |
| 721 | /* Convert "aa\nbb\ncc\n\0" to "aa\0bb\0cc\0\0" */ |
| 722 | s = ld->inst; |
| 723 | while (s) { |
| 724 | np = strchr(s, '\n'); |
Denis Vlasenko | 72b6a65 | 2007-08-21 11:18:25 +0000 | [diff] [blame] | 725 | if (np) |
| 726 | *np++ = '\0'; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 727 | s = np; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /* open current */ |
| 732 | i = stat("current", &st); |
| 733 | if (i != -1) { |
Denis Vlasenko | 72b6a65 | 2007-08-21 11:18:25 +0000 | [diff] [blame] | 734 | if (st.st_size && !(st.st_mode & S_IXUSR)) { |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 735 | ld->fnsave[25] = '.'; |
| 736 | ld->fnsave[26] = 'u'; |
| 737 | ld->fnsave[27] = '\0'; |
| 738 | do { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 739 | fmt_time_bernstein_25(ld->fnsave); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 740 | errno = 0; |
Denis Vlasenko | 8c78395 | 2007-01-27 22:21:52 +0000 | [diff] [blame] | 741 | stat(ld->fnsave, &st); |
| 742 | } while (errno != ENOENT); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 743 | while (rename("current", ld->fnsave) == -1) |
| 744 | pause2cannot("rename current", ld->name); |
| 745 | rmoldest(ld); |
| 746 | i = -1; |
| 747 | } else { |
Denis Vlasenko | 72b6a65 | 2007-08-21 11:18:25 +0000 | [diff] [blame] | 748 | /* st.st_size can be not just bigger, but WIDER! |
| 749 | * This code is safe: if st.st_size > 4GB, we select |
| 750 | * ld->sizemax (because it's "unsigned") */ |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 751 | ld->size = (st.st_size > ld->sizemax) ? ld->sizemax : st.st_size; |
| 752 | } |
| 753 | } else { |
| 754 | if (errno != ENOENT) { |
| 755 | logdir_close(ld); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 756 | warn2("can't stat current", ld->name); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 757 | while (fchdir(fdwdir) == -1) |
| 758 | pause1cannot("change to initial working directory"); |
| 759 | return 0; |
| 760 | } |
| 761 | } |
| 762 | while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1) |
| 763 | pause2cannot("open current", ld->name); |
Denis Vlasenko | 6439290 | 2007-02-03 00:53:43 +0000 | [diff] [blame] | 764 | /* we presume this cannot fail */ |
| 765 | ld->filecur = fdopen(ld->fdcur, "a"); //// |
| 766 | setvbuf(ld->filecur, NULL, _IOFBF, linelen); //// |
| 767 | |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 768 | close_on_exec_on(ld->fdcur); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 769 | while (fchmod(ld->fdcur, 0644) == -1) |
| 770 | pause2cannot("set mode of current", ld->name); |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 771 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 772 | if (verbose) { |
| 773 | if (i == 0) bb_error_msg(INFO"append: %s/current", ld->name); |
| 774 | else bb_error_msg(INFO"new: %s/current", ld->name); |
| 775 | } |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 776 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 777 | while (fchdir(fdwdir) == -1) |
| 778 | pause1cannot("change to initial working directory"); |
| 779 | return 1; |
| 780 | } |
| 781 | |
| 782 | static void logdirs_reopen(void) |
| 783 | { |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 784 | int l; |
| 785 | int ok = 0; |
| 786 | |
| 787 | tmaxflag = 0; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 788 | for (l = 0; l < dirn; ++l) { |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 789 | logdir_close(&dir[l]); |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 790 | if (logdir_open(&dir[l], fndir[l])) |
| 791 | ok = 1; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 792 | } |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 793 | if (!ok) |
| 794 | fatalx("no functional log directories"); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Denis Vlasenko | 4f8d27f | 2007-02-03 00:52:17 +0000 | [diff] [blame] | 797 | /* Will look good in libbb one day */ |
| 798 | static ssize_t ndelay_read(int fd, void *buf, size_t count) |
| 799 | { |
| 800 | if (!(fl_flag_0 & O_NONBLOCK)) |
| 801 | fcntl(fd, F_SETFL, fl_flag_0 | O_NONBLOCK); |
| 802 | count = safe_read(fd, buf, count); |
| 803 | if (!(fl_flag_0 & O_NONBLOCK)) |
| 804 | fcntl(fd, F_SETFL, fl_flag_0); |
| 805 | return count; |
| 806 | } |
| 807 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 808 | /* Used for reading stdin */ |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 809 | static int buffer_pread(/*int fd, */char *s, unsigned len) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 810 | { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 811 | unsigned now; |
| 812 | struct pollfd input; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 813 | int i; |
| 814 | |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 815 | input.fd = STDIN_FILENO; |
Denis Vlasenko | 72b6a65 | 2007-08-21 11:18:25 +0000 | [diff] [blame] | 816 | input.events = POLLIN; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 817 | |
Denis Vlasenko | b952835 | 2007-05-06 01:37:21 +0000 | [diff] [blame] | 818 | do { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 819 | if (rotateasap) { |
| 820 | for (i = 0; i < dirn; ++i) |
| 821 | rotate(dir + i); |
| 822 | rotateasap = 0; |
| 823 | } |
| 824 | if (exitasap) { |
| 825 | if (linecomplete) |
| 826 | return 0; |
| 827 | len = 1; |
| 828 | } |
| 829 | if (reopenasap) { |
| 830 | logdirs_reopen(); |
| 831 | reopenasap = 0; |
| 832 | } |
| 833 | now = monotonic_sec(); |
| 834 | nearest_rotate = now + (45 * 60 + 45); |
| 835 | for (i = 0; i < dirn; ++i) { |
| 836 | if (dir[i].rotate_period) { |
| 837 | if (LESS(dir[i].next_rotate, now)) |
| 838 | rotate(dir + i); |
| 839 | if (LESS(dir[i].next_rotate, nearest_rotate)) |
| 840 | nearest_rotate = dir[i].next_rotate; |
| 841 | } |
| 842 | } |
| 843 | |
Denis Vlasenko | 339936b | 2007-10-05 22:11:06 +0000 | [diff] [blame] | 844 | sigprocmask(SIG_UNBLOCK, &blocked_sigset, NULL); |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 845 | i = nearest_rotate - now; |
| 846 | if (i > 1000000) |
| 847 | i = 1000000; |
| 848 | if (i <= 0) |
| 849 | i = 1; |
| 850 | poll(&input, 1, i * 1000); |
Denis Vlasenko | 339936b | 2007-10-05 22:11:06 +0000 | [diff] [blame] | 851 | sigprocmask(SIG_BLOCK, &blocked_sigset, NULL); |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 852 | |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 853 | i = ndelay_read(STDIN_FILENO, s, len); |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 854 | if (i >= 0) |
| 855 | break; |
| 856 | if (errno == EINTR) |
| 857 | continue; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 858 | if (errno != EAGAIN) { |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 859 | warn("can't read standard input"); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 860 | break; |
| 861 | } |
| 862 | /* else: EAGAIN - normal, repeat silently */ |
Denis Vlasenko | b952835 | 2007-05-06 01:37:21 +0000 | [diff] [blame] | 863 | } while (!exitasap); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 864 | |
| 865 | if (i > 0) { |
| 866 | int cnt; |
| 867 | linecomplete = (s[i-1] == '\n'); |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 868 | if (!repl) |
| 869 | return i; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 870 | |
| 871 | cnt = i; |
| 872 | while (--cnt >= 0) { |
| 873 | char ch = *s; |
| 874 | if (ch != '\n') { |
| 875 | if (ch < 32 || ch > 126) |
| 876 | *s = repl; |
| 877 | else { |
| 878 | int j; |
| 879 | for (j = 0; replace[j]; ++j) { |
| 880 | if (ch == replace[j]) { |
| 881 | *s = repl; |
| 882 | break; |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | s++; |
| 888 | } |
| 889 | } |
| 890 | return i; |
| 891 | } |
| 892 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 893 | static void sig_term_handler(int sig_no UNUSED_PARAM) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 894 | { |
| 895 | if (verbose) |
| 896 | bb_error_msg(INFO"sig%s received", "term"); |
| 897 | exitasap = 1; |
| 898 | } |
| 899 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 900 | static void sig_child_handler(int sig_no UNUSED_PARAM) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 901 | { |
Denis Vlasenko | 3854c5d | 2008-11-06 22:39:57 +0000 | [diff] [blame] | 902 | pid_t pid; |
| 903 | int l; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 904 | |
| 905 | if (verbose) |
| 906 | bb_error_msg(INFO"sig%s received", "child"); |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 907 | while ((pid = wait_any_nohang(&wstat)) > 0) { |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 908 | for (l = 0; l < dirn; ++l) { |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 909 | if (dir[l].ppid == pid) { |
| 910 | dir[l].ppid = 0; |
| 911 | processorstop(&dir[l]); |
| 912 | break; |
| 913 | } |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 914 | } |
| 915 | } |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 918 | static void sig_alarm_handler(int sig_no UNUSED_PARAM) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 919 | { |
| 920 | if (verbose) |
| 921 | bb_error_msg(INFO"sig%s received", "alarm"); |
| 922 | rotateasap = 1; |
| 923 | } |
| 924 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 925 | static void sig_hangup_handler(int sig_no UNUSED_PARAM) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 926 | { |
| 927 | if (verbose) |
| 928 | bb_error_msg(INFO"sig%s received", "hangup"); |
| 929 | reopenasap = 1; |
| 930 | } |
| 931 | |
| 932 | static void logmatch(struct logdir *ld) |
| 933 | { |
| 934 | char *s; |
| 935 | |
| 936 | ld->match = '+'; |
| 937 | ld->matcherr = 'E'; |
| 938 | s = ld->inst; |
| 939 | while (s && s[0]) { |
| 940 | switch (s[0]) { |
| 941 | case '+': |
| 942 | case '-': |
| 943 | if (pmatch(s+1, line, linelen)) |
| 944 | ld->match = s[0]; |
| 945 | break; |
| 946 | case 'e': |
| 947 | case 'E': |
| 948 | if (pmatch(s+1, line, linelen)) |
| 949 | ld->matcherr = s[0]; |
| 950 | break; |
| 951 | } |
| 952 | s += strlen(s) + 1; |
| 953 | } |
| 954 | } |
| 955 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 956 | int svlogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 957 | int svlogd_main(int argc, char **argv) |
| 958 | { |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 959 | char *r, *l, *b; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 960 | ssize_t stdin_cnt = 0; |
| 961 | int i; |
| 962 | unsigned opt; |
| 963 | unsigned timestamp = 0; |
| 964 | |
Denis Vlasenko | 339936b | 2007-10-05 22:11:06 +0000 | [diff] [blame] | 965 | INIT_G(); |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 966 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 967 | opt_complementary = "tt:vv"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 968 | opt = getopt32(argv, "r:R:l:b:tv", |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 969 | &r, &replace, &l, &b, ×tamp, &verbose); |
| 970 | if (opt & 1) { // -r |
| 971 | repl = r[0]; |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 972 | if (!repl || r[1]) |
| 973 | bb_show_usage(); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 974 | } |
| 975 | if (opt & 2) if (!repl) repl = '_'; // -R |
| 976 | if (opt & 4) { // -l |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 977 | linemax = xatou_range(l, 0, BUFSIZ-26); |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 978 | if (linemax == 0) |
| 979 | linemax = BUFSIZ-26; |
| 980 | if (linemax < 256) |
| 981 | linemax = 256; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 982 | } |
Denis Vlasenko | 6439290 | 2007-02-03 00:53:43 +0000 | [diff] [blame] | 983 | ////if (opt & 8) { // -b |
| 984 | //// buflen = xatoi_u(b); |
| 985 | //// if (buflen == 0) buflen = 1024; |
| 986 | ////} |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 987 | //if (opt & 0x10) timestamp++; // -t |
| 988 | //if (opt & 0x20) verbose++; // -v |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 989 | //if (timestamp > 2) timestamp = 2; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 990 | argv += optind; |
| 991 | argc -= optind; |
| 992 | |
| 993 | dirn = argc; |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 994 | if (dirn <= 0) |
| 995 | bb_show_usage(); |
| 996 | ////if (buflen <= linemax) bb_show_usage(); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 997 | fdwdir = xopen(".", O_RDONLY|O_NDELAY); |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 998 | close_on_exec_on(fdwdir); |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 999 | dir = xzalloc(dirn * sizeof(dir[0])); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1000 | for (i = 0; i < dirn; ++i) { |
| 1001 | dir[i].fddir = -1; |
| 1002 | dir[i].fdcur = -1; |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 1003 | ////dir[i].btmp = xmalloc(buflen); |
Denis Vlasenko | b952835 | 2007-05-06 01:37:21 +0000 | [diff] [blame] | 1004 | /*dir[i].ppid = 0;*/ |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1005 | } |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 1006 | /* line = xmalloc(linemax + (timestamp ? 26 : 0)); */ |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1007 | fndir = argv; |
Denis Vlasenko | 4f8d27f | 2007-02-03 00:52:17 +0000 | [diff] [blame] | 1008 | /* We cannot set NONBLOCK on fd #0 permanently - this setting |
| 1009 | * _isn't_ per-process! It is shared among all other processes |
| 1010 | * with the same stdin */ |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 1011 | fl_flag_0 = fcntl(0, F_GETFL); |
Denis Vlasenko | 4f8d27f | 2007-02-03 00:52:17 +0000 | [diff] [blame] | 1012 | |
Denis Vlasenko | 339936b | 2007-10-05 22:11:06 +0000 | [diff] [blame] | 1013 | sigemptyset(&blocked_sigset); |
| 1014 | sigaddset(&blocked_sigset, SIGTERM); |
| 1015 | sigaddset(&blocked_sigset, SIGCHLD); |
| 1016 | sigaddset(&blocked_sigset, SIGALRM); |
| 1017 | sigaddset(&blocked_sigset, SIGHUP); |
| 1018 | sigprocmask(SIG_BLOCK, &blocked_sigset, NULL); |
Denis Vlasenko | cab28aa | 2009-01-31 01:02:07 +0000 | [diff] [blame] | 1019 | bb_signals_recursive_norestart(1 << SIGTERM, sig_term_handler); |
| 1020 | bb_signals_recursive_norestart(1 << SIGCHLD, sig_child_handler); |
| 1021 | bb_signals_recursive_norestart(1 << SIGALRM, sig_alarm_handler); |
| 1022 | bb_signals_recursive_norestart(1 << SIGHUP, sig_hangup_handler); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1023 | |
Denis Vlasenko | eeafc1a | 2007-01-27 23:15:50 +0000 | [diff] [blame] | 1024 | /* Without timestamps, we don't have to print each line |
| 1025 | * separately, so we can look for _last_ newline, not first, |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 1026 | * thus batching writes. If filtering is enabled in config, |
| 1027 | * logdirs_reopen resets it to memchr. |
| 1028 | */ |
| 1029 | memRchr = (timestamp ? memchr : memrchr); |
| 1030 | |
| 1031 | logdirs_reopen(); |
Denis Vlasenko | eeafc1a | 2007-01-27 23:15:50 +0000 | [diff] [blame] | 1032 | |
Denis Vlasenko | 6439290 | 2007-02-03 00:53:43 +0000 | [diff] [blame] | 1033 | setvbuf(stderr, NULL, _IOFBF, linelen); |
| 1034 | |
Denis Vlasenko | 4e1715f | 2007-01-28 14:51:32 +0000 | [diff] [blame] | 1035 | /* Each iteration processes one or more lines */ |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1036 | while (1) { |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 1037 | char stamp[FMT_PTIME]; |
| 1038 | char *lineptr; |
| 1039 | char *printptr; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1040 | char *np; |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 1041 | int printlen; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1042 | char ch; |
| 1043 | |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 1044 | lineptr = line; |
Denis Vlasenko | f223efb | 2007-08-03 10:58:12 +0000 | [diff] [blame] | 1045 | if (timestamp) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1046 | lineptr += 26; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1047 | |
| 1048 | /* lineptr[0..linemax-1] - buffer for stdin */ |
| 1049 | /* (possibly has some unprocessed data from prev loop) */ |
| 1050 | |
| 1051 | /* Refill the buffer if needed */ |
Denis Vlasenko | eeafc1a | 2007-01-27 23:15:50 +0000 | [diff] [blame] | 1052 | np = memRchr(lineptr, '\n', stdin_cnt); |
| 1053 | if (!np && !exitasap) { |
| 1054 | i = linemax - stdin_cnt; /* avail. bytes at tail */ |
| 1055 | if (i >= 128) { |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 1056 | i = buffer_pread(/*0, */lineptr + stdin_cnt, i); |
Denis Vlasenko | eeafc1a | 2007-01-27 23:15:50 +0000 | [diff] [blame] | 1057 | if (i <= 0) /* EOF or error on stdin */ |
| 1058 | exitasap = 1; |
| 1059 | else { |
| 1060 | np = memRchr(lineptr + stdin_cnt, '\n', i); |
| 1061 | stdin_cnt += i; |
| 1062 | } |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1063 | } |
| 1064 | } |
| 1065 | if (stdin_cnt <= 0 && exitasap) |
| 1066 | break; |
| 1067 | |
| 1068 | /* Search for '\n' (in fact, np already holds the result) */ |
| 1069 | linelen = stdin_cnt; |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 1070 | if (np) { |
| 1071 | print_to_nl: /* NB: starting from here lineptr may point |
| 1072 | * farther out into line[] */ |
| 1073 | linelen = np - lineptr + 1; |
| 1074 | } |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1075 | /* linelen == no of chars incl. '\n' (or == stdin_cnt) */ |
| 1076 | ch = lineptr[linelen-1]; |
| 1077 | |
Denis Vlasenko | 6439290 | 2007-02-03 00:53:43 +0000 | [diff] [blame] | 1078 | /* Biggest performance hit was coming from the fact |
| 1079 | * that we did not buffer writes. We were reading many lines |
| 1080 | * in one read() above, but wrote one line per write(). |
| 1081 | * We are using stdio to fix that */ |
Denis Vlasenko | 4f8d27f | 2007-02-03 00:52:17 +0000 | [diff] [blame] | 1082 | |
| 1083 | /* write out lineptr[0..linelen-1] to each log destination |
| 1084 | * (or lineptr[-26..linelen-1] if timestamping) */ |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 1085 | printlen = linelen; |
| 1086 | printptr = lineptr; |
| 1087 | if (timestamp) { |
Denis Vlasenko | f223efb | 2007-08-03 10:58:12 +0000 | [diff] [blame] | 1088 | if (timestamp == 1) |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 1089 | fmt_time_bernstein_25(stamp); |
Denis Vlasenko | f223efb | 2007-08-03 10:58:12 +0000 | [diff] [blame] | 1090 | else /* 2: */ |
Denis Vlasenko | 45946f8 | 2007-08-20 17:27:40 +0000 | [diff] [blame] | 1091 | fmt_time_human_30nul(stamp); |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 1092 | printlen += 26; |
| 1093 | printptr -= 26; |
| 1094 | memcpy(printptr, stamp, 25); |
| 1095 | printptr[25] = ' '; |
| 1096 | } |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1097 | for (i = 0; i < dirn; ++i) { |
| 1098 | struct logdir *ld = &dir[i]; |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 1099 | if (ld->fddir == -1) |
| 1100 | continue; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1101 | if (ld->inst) |
| 1102 | logmatch(ld); |
Denis Vlasenko | 7ab5e3d | 2007-10-22 15:53:34 +0000 | [diff] [blame] | 1103 | if (ld->matcherr == 'e') { |
| 1104 | /* runit-1.8.0 compat: if timestamping, do it on stderr too */ |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1105 | ////full_write(STDERR_FILENO, printptr, printlen); |
Denis Vlasenko | 7ab5e3d | 2007-10-22 15:53:34 +0000 | [diff] [blame] | 1106 | fwrite(printptr, 1, printlen, stderr); |
| 1107 | } |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 1108 | if (ld->match != '+') |
| 1109 | continue; |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 1110 | buffer_pwrite(i, printptr, printlen); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | /* If we didn't see '\n' (long input line), */ |
| 1114 | /* read/write repeatedly until we see it */ |
| 1115 | while (ch != '\n') { |
| 1116 | /* lineptr is emptied now, safe to use as buffer */ |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 1117 | stdin_cnt = exitasap ? -1 : buffer_pread(/*0, */lineptr, linemax); |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1118 | if (stdin_cnt <= 0) { /* EOF or error on stdin */ |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 1119 | exitasap = 1; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1120 | lineptr[0] = ch = '\n'; |
| 1121 | linelen = 1; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1122 | stdin_cnt = 1; |
| 1123 | } else { |
| 1124 | linelen = stdin_cnt; |
Denis Vlasenko | eeafc1a | 2007-01-27 23:15:50 +0000 | [diff] [blame] | 1125 | np = memRchr(lineptr, '\n', stdin_cnt); |
| 1126 | if (np) |
| 1127 | linelen = np - lineptr + 1; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1128 | ch = lineptr[linelen-1]; |
| 1129 | } |
| 1130 | /* linelen == no of chars incl. '\n' (or == stdin_cnt) */ |
| 1131 | for (i = 0; i < dirn; ++i) { |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 1132 | if (dir[i].fddir == -1) |
| 1133 | continue; |
Denis Vlasenko | 7ab5e3d | 2007-10-22 15:53:34 +0000 | [diff] [blame] | 1134 | if (dir[i].matcherr == 'e') { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1135 | ////full_write(STDERR_FILENO, lineptr, linelen); |
Denis Vlasenko | 6439290 | 2007-02-03 00:53:43 +0000 | [diff] [blame] | 1136 | fwrite(lineptr, 1, linelen, stderr); |
Denis Vlasenko | 7ab5e3d | 2007-10-22 15:53:34 +0000 | [diff] [blame] | 1137 | } |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 1138 | if (dir[i].match != '+') |
| 1139 | continue; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1140 | buffer_pwrite(i, lineptr, linelen); |
| 1141 | } |
| 1142 | } |
| 1143 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1144 | stdin_cnt -= linelen; |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 1145 | if (stdin_cnt > 0) { |
| 1146 | lineptr += linelen; |
| 1147 | /* If we see another '\n', we don't need to read |
| 1148 | * next piece of input: can print what we have */ |
Denis Vlasenko | eeafc1a | 2007-01-27 23:15:50 +0000 | [diff] [blame] | 1149 | np = memRchr(lineptr, '\n', stdin_cnt); |
Denis Vlasenko | ca549c5 | 2007-01-27 22:24:59 +0000 | [diff] [blame] | 1150 | if (np) |
| 1151 | goto print_to_nl; |
| 1152 | /* Move unprocessed data to the front of line */ |
| 1153 | memmove((timestamp ? line+26 : line), lineptr, stdin_cnt); |
| 1154 | } |
Denis Vlasenko | 6439290 | 2007-02-03 00:53:43 +0000 | [diff] [blame] | 1155 | fflush(NULL);//// |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | for (i = 0; i < dirn; ++i) { |
| 1159 | if (dir[i].ppid) |
| 1160 | while (!processorstop(&dir[i])) |
Denys Vlasenko | aebb742 | 2009-08-02 00:55:49 +0200 | [diff] [blame] | 1161 | continue; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1162 | logdir_close(&dir[i]); |
| 1163 | } |
Denis Vlasenko | eeafc1a | 2007-01-27 23:15:50 +0000 | [diff] [blame] | 1164 | return 0; |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 1165 | } |