svlogd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. Copyright (c) 2001-2006, Gerrit Pape
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. 3. The name of the author may not be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  14. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  16. EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  19. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  20. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  21. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  22. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */
  25. /* TODO: depends on runit_lib.c - review and reduce/eliminate */
  26. #include <sys/poll.h>
  27. #include <sys/file.h>
  28. #include "busybox.h"
  29. #include "runit_lib.h"
  30. static unsigned verbose;
  31. static int linemax = 1000;
  32. static int buflen = 1024;
  33. static int linelen;
  34. static char **fndir;
  35. static int fdwdir;
  36. static int wstat;
  37. static struct taia trotate;
  38. static char *line;
  39. static unsigned exitasap;
  40. static unsigned rotateasap;
  41. static unsigned reopenasap;
  42. static unsigned linecomplete = 1;
  43. static unsigned tmaxflag;
  44. static iopause_fd in;
  45. static const char *replace = "";
  46. static char repl;
  47. static struct logdir {
  48. char *btmp;
  49. /* pattern list to match, in "aa\0bb\0\cc\0\0" form */
  50. char *inst;
  51. char *processor;
  52. char *name;
  53. unsigned size;
  54. unsigned sizemax;
  55. unsigned nmax;
  56. unsigned nmin;
  57. /* int (not long) because of taia_uint() usage: */
  58. unsigned tmax;
  59. int ppid;
  60. int fddir;
  61. int fdcur;
  62. int fdlock;
  63. struct taia trotate;
  64. char fnsave[FMT_PTIME];
  65. char match;
  66. char matcherr;
  67. } *dir;
  68. static unsigned dirn = 0;
  69. #define FATAL "fatal: "
  70. #define WARNING "warning: "
  71. #define PAUSE "pausing: "
  72. #define INFO "info: "
  73. #define usage() bb_show_usage()
  74. static void fatalx(char *m0)
  75. {
  76. bb_error_msg_and_die(FATAL"%s", m0);
  77. }
  78. static void warn(char *m0) {
  79. bb_perror_msg(WARNING"%s", m0);
  80. }
  81. static void warn2(char *m0, char *m1)
  82. {
  83. bb_perror_msg(WARNING"%s: %s", m0, m1);
  84. }
  85. static void warnx(char *m0, char *m1)
  86. {
  87. bb_error_msg(WARNING"%s: %s", m0, m1);
  88. }
  89. static void pause_nomem(void)
  90. {
  91. bb_error_msg(PAUSE"out of memory"); sleep(3);
  92. }
  93. static void pause1cannot(char *m0)
  94. {
  95. bb_perror_msg(PAUSE"cannot %s", m0); sleep(3);
  96. }
  97. static void pause2cannot(char *m0, char *m1)
  98. {
  99. bb_perror_msg(PAUSE"cannot %s %s", m0, m1);
  100. sleep(3);
  101. }
  102. static char* wstrdup(const char *str)
  103. {
  104. char *s;
  105. while (!(s = strdup(str))) pause_nomem();
  106. return s;
  107. }
  108. static unsigned processorstart(struct logdir *ld)
  109. {
  110. int pid;
  111. if (!ld->processor) return 0;
  112. if (ld->ppid) {
  113. warnx("processor already running", ld->name);
  114. return 0;
  115. }
  116. while ((pid = fork()) == -1)
  117. pause2cannot("fork for processor", ld->name);
  118. if (!pid) {
  119. char *prog[4];
  120. int fd;
  121. /* child */
  122. sig_uncatch(sig_term);
  123. sig_uncatch(sig_alarm);
  124. sig_uncatch(sig_hangup);
  125. sig_unblock(sig_term);
  126. sig_unblock(sig_alarm);
  127. sig_unblock(sig_hangup);
  128. if (verbose)
  129. bb_error_msg(INFO"processing: %s/%s", ld->name, ld->fnsave);
  130. fd = xopen(ld->fnsave, O_RDONLY|O_NDELAY);
  131. if (fd_move(0, fd) == -1)
  132. bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
  133. ld->fnsave[26] = 't';
  134. fd = xopen(ld->fnsave, O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
  135. if (fd_move(1, fd) == -1)
  136. bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
  137. fd = open_read("state");
  138. if (fd == -1) {
  139. if (errno != ENOENT)
  140. bb_perror_msg_and_die(FATAL"cannot %s processor %s", "open state for", ld->name);
  141. close(xopen("state", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT));
  142. fd = xopen("state", O_RDONLY|O_NDELAY);
  143. }
  144. if (fd_move(4, fd) == -1)
  145. bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
  146. fd = xopen("newstate", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
  147. if (fd_move(5, fd) == -1)
  148. bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
  149. prog[0] = "sh";
  150. prog[1] = "-c";
  151. prog[2] = ld->processor;
  152. prog[3] = '\0';
  153. execve("/bin/sh", prog, environ);
  154. bb_perror_msg_and_die(FATAL"cannot %s processor %s", "run", ld->name);
  155. }
  156. ld->ppid = pid;
  157. return 1;
  158. }
  159. static unsigned processorstop(struct logdir *ld)
  160. {
  161. char f[28];
  162. if (ld->ppid) {
  163. sig_unblock(sig_hangup);
  164. while (wait_pid(&wstat, ld->ppid) == -1)
  165. pause2cannot("wait for processor", ld->name);
  166. sig_block(sig_hangup);
  167. ld->ppid = 0;
  168. }
  169. if (ld->fddir == -1) return 1;
  170. while (fchdir(ld->fddir) == -1)
  171. pause2cannot("change directory, want processor", ld->name);
  172. if (wait_exitcode(wstat) != 0) {
  173. warnx("processor failed, restart", ld->name);
  174. ld->fnsave[26] = 't';
  175. unlink(ld->fnsave);
  176. ld->fnsave[26] = 'u';
  177. processorstart(ld);
  178. while (fchdir(fdwdir) == -1)
  179. pause1cannot("change to initial working directory");
  180. return ld->processor ? 0 : 1;
  181. }
  182. ld->fnsave[26] = 't';
  183. memcpy(f, ld->fnsave, 26);
  184. f[26] = 's';
  185. f[27] = '\0';
  186. while (rename(ld->fnsave, f) == -1)
  187. pause2cannot("rename processed", ld->name);
  188. while (chmod(f, 0744) == -1)
  189. pause2cannot("set mode of processed", ld->name);
  190. ld->fnsave[26] = 'u';
  191. if (unlink(ld->fnsave) == -1)
  192. bb_error_msg(WARNING"cannot unlink: %s/%s", ld->name, ld->fnsave);
  193. while (rename("newstate", "state") == -1)
  194. pause2cannot("rename state", ld->name);
  195. if (verbose) bb_error_msg(INFO"processed: %s/%s", ld->name, f);
  196. while (fchdir(fdwdir) == -1)
  197. pause1cannot("change to initial working directory");
  198. return 1;
  199. }
  200. static void rmoldest(struct logdir *ld)
  201. {
  202. DIR *d;
  203. struct dirent *f;
  204. char oldest[FMT_PTIME];
  205. int n = 0;
  206. oldest[0] = 'A'; oldest[1] = oldest[27] = 0;
  207. while (!(d = opendir(".")))
  208. pause2cannot("open directory, want rotate", ld->name);
  209. errno = 0;
  210. while ((f = readdir(d))) {
  211. if ((f->d_name[0] == '@') && (strlen(f->d_name) == 27)) {
  212. if (f->d_name[26] == 't') {
  213. if (unlink(f->d_name) == -1)
  214. warn2("cannot unlink processor leftover", f->d_name);
  215. } else {
  216. ++n;
  217. if (strcmp(f->d_name, oldest) < 0)
  218. memcpy(oldest, f->d_name, 27);
  219. }
  220. errno = 0;
  221. }
  222. }
  223. if (errno) warn2("cannot read directory", ld->name);
  224. closedir(d);
  225. if (ld->nmax && (n > ld->nmax)) {
  226. if (verbose) bb_error_msg(INFO"delete: %s/%s", ld->name, oldest);
  227. if ((*oldest == '@') && (unlink(oldest) == -1))
  228. warn2("cannot unlink oldest logfile", ld->name);
  229. }
  230. }
  231. static unsigned rotate(struct logdir *ld)
  232. {
  233. struct stat st;
  234. struct taia now;
  235. if (ld->fddir == -1) {
  236. ld->tmax = 0;
  237. return 0;
  238. }
  239. if (ld->ppid)
  240. while (!processorstop(ld))
  241. /* wait */;
  242. while (fchdir(ld->fddir) == -1)
  243. pause2cannot("change directory, want rotate", ld->name);
  244. /* create new filename */
  245. ld->fnsave[25] = '.';
  246. ld->fnsave[26] = 's';
  247. if (ld->processor)
  248. ld->fnsave[26] = 'u';
  249. ld->fnsave[27] = '\0';
  250. do {
  251. taia_now(&now);
  252. fmt_taia(ld->fnsave, &now);
  253. errno = 0;
  254. } while ((stat(ld->fnsave, &st) != -1) || (errno != ENOENT));
  255. if (ld->tmax && taia_less(&ld->trotate, &now)) {
  256. taia_uint(&ld->trotate, ld->tmax);
  257. taia_add(&ld->trotate, &now, &ld->trotate);
  258. if (taia_less(&ld->trotate, &trotate))
  259. trotate = ld->trotate;
  260. }
  261. if (ld->size > 0) {
  262. while (fsync(ld->fdcur) == -1)
  263. pause2cannot("fsync current logfile", ld->name);
  264. while (fchmod(ld->fdcur, 0744) == -1)
  265. pause2cannot("set mode of current", ld->name);
  266. close(ld->fdcur);
  267. if (verbose) {
  268. bb_error_msg(INFO"rename: %s/current %s %u", ld->name,
  269. ld->fnsave, ld->size);
  270. }
  271. while (rename("current", ld->fnsave) == -1)
  272. pause2cannot("rename current", ld->name);
  273. while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1)
  274. pause2cannot("create new current", ld->name);
  275. coe(ld->fdcur);
  276. ld->size = 0;
  277. while (fchmod(ld->fdcur, 0644) == -1)
  278. pause2cannot("set mode of current", ld->name);
  279. rmoldest(ld);
  280. processorstart(ld);
  281. }
  282. while (fchdir(fdwdir) == -1)
  283. pause1cannot("change to initial working directory");
  284. return 1;
  285. }
  286. static int buffer_pwrite(int n, char *s, unsigned len)
  287. {
  288. int i;
  289. struct logdir *ld = &dir[n];
  290. if (ld->sizemax) {
  291. if (ld->size >= ld->sizemax)
  292. rotate(ld);
  293. if (len > (ld->sizemax - ld->size))
  294. len = ld->sizemax - ld->size;
  295. }
  296. while ((i = write(ld->fdcur, s, len)) == -1) {
  297. if ((errno == ENOSPC) && (ld->nmin < ld->nmax)) {
  298. DIR *d;
  299. struct dirent *f;
  300. char oldest[FMT_PTIME];
  301. int j = 0;
  302. while (fchdir(ld->fddir) == -1)
  303. pause2cannot("change directory, want remove old logfile",
  304. ld->name);
  305. oldest[0] = 'A';
  306. oldest[1] = oldest[27] = '\0';
  307. while (!(d = opendir(".")))
  308. pause2cannot("open directory, want remove old logfile",
  309. ld->name);
  310. errno = 0;
  311. while ((f = readdir(d)))
  312. if ((f->d_name[0] == '@') && (strlen(f->d_name) == 27)) {
  313. ++j;
  314. if (strcmp(f->d_name, oldest) < 0)
  315. memcpy(oldest, f->d_name, 27);
  316. }
  317. if (errno) warn2("cannot read directory, want remove old logfile",
  318. ld->name);
  319. closedir(d);
  320. errno = ENOSPC;
  321. if (j > ld->nmin) {
  322. if (*oldest == '@') {
  323. bb_error_msg(WARNING"out of disk space, delete: %s/%s",
  324. ld->name, oldest);
  325. errno = 0;
  326. if (unlink(oldest) == -1) {
  327. warn2("cannot unlink oldest logfile", ld->name);
  328. errno = ENOSPC;
  329. }
  330. while (fchdir(fdwdir) == -1)
  331. pause1cannot("change to initial working directory");
  332. }
  333. }
  334. }
  335. if (errno) pause2cannot("write to current", ld->name);
  336. }
  337. ld->size += i;
  338. if (ld->sizemax)
  339. if (s[i-1] == '\n')
  340. if (ld->size >= (ld->sizemax - linemax))
  341. rotate(ld);
  342. return i;
  343. }
  344. static void logdir_close(struct logdir *ld)
  345. {
  346. if (ld->fddir == -1)
  347. return;
  348. if (verbose)
  349. bb_error_msg(INFO"close: %s", ld->name);
  350. close(ld->fddir);
  351. ld->fddir = -1;
  352. if (ld->fdcur == -1)
  353. return; /* impossible */
  354. while (fsync(ld->fdcur) == -1)
  355. pause2cannot("fsync current logfile", ld->name);
  356. while (fchmod(ld->fdcur, 0744) == -1)
  357. pause2cannot("set mode of current", ld->name);
  358. close(ld->fdcur);
  359. ld->fdcur = -1;
  360. if (ld->fdlock == -1)
  361. return; /* impossible */
  362. close(ld->fdlock);
  363. ld->fdlock = -1;
  364. free(ld->processor);
  365. ld->processor = NULL;
  366. }
  367. static unsigned logdir_open(struct logdir *ld, const char *fn)
  368. {
  369. char buf[128];
  370. struct taia now;
  371. char *new, *s, *np;
  372. int i;
  373. struct stat st;
  374. ld->fddir = open(fn, O_RDONLY|O_NDELAY);
  375. if (ld->fddir == -1) {
  376. warn2("cannot open log directory", (char*)fn);
  377. return 0;
  378. }
  379. coe(ld->fddir);
  380. if (fchdir(ld->fddir) == -1) {
  381. logdir_close(ld);
  382. warn2("cannot change directory", (char*)fn);
  383. return 0;
  384. }
  385. ld->fdlock = open("lock", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
  386. if ((ld->fdlock == -1)
  387. || (lock_exnb(ld->fdlock) == -1)
  388. ) {
  389. logdir_close(ld);
  390. warn2("cannot lock directory", (char*)fn);
  391. while (fchdir(fdwdir) == -1)
  392. pause1cannot("change to initial working directory");
  393. return 0;
  394. }
  395. coe(ld->fdlock);
  396. ld->size = 0;
  397. ld->sizemax = 1000000;
  398. ld->nmax = ld->nmin = 10;
  399. ld->tmax = 0;
  400. ld->name = (char*)fn;
  401. ld->ppid = 0;
  402. ld->match = '+';
  403. free(ld->inst); ld->inst = NULL;
  404. free(ld->processor); ld->processor = NULL;
  405. /* read config */
  406. i = open_read_close("config", buf, sizeof(buf));
  407. if (i < 0)
  408. warn2("cannot read config", ld->name);
  409. if (i > 0) {
  410. if (verbose) bb_error_msg(INFO"read: %s/config", ld->name);
  411. s = buf;
  412. while (s) {
  413. np = strchr(s, '\n');
  414. if (np) *np++ = '\0';
  415. switch (s[0]) {
  416. case '+':
  417. case '-':
  418. case 'e':
  419. case 'E':
  420. while (1) {
  421. int l = asprintf(&new, "%s%s\n", ld->inst?:"", s);
  422. if (l >= 0 && new) break;
  423. pause_nomem();
  424. }
  425. free(ld->inst);
  426. ld->inst = new;
  427. break;
  428. case 's': {
  429. static const struct suffix_mult km_suffixes[] = {
  430. { "k", 1024 },
  431. { "m", 1024*1024 },
  432. { NULL, 0 }
  433. };
  434. ld->sizemax = xatou_sfx(&s[1], km_suffixes);
  435. break;
  436. }
  437. case 'n':
  438. ld->nmax = xatoi_u(&s[1]);
  439. break;
  440. case 'N':
  441. ld->nmin = xatoi_u(&s[1]);
  442. break;
  443. case 't': {
  444. static const struct suffix_mult mh_suffixes[] = {
  445. { "m", 60 },
  446. { "h", 60*60 },
  447. /*{ "d", 24*60*60 },*/
  448. { NULL, 0 }
  449. };
  450. ld->tmax = xatou_sfx(&s[1], mh_suffixes);
  451. if (ld->tmax) {
  452. taia_uint(&ld->trotate, ld->tmax);
  453. taia_add(&ld->trotate, &now, &ld->trotate);
  454. if (!tmaxflag || taia_less(&ld->trotate, &trotate))
  455. trotate = ld->trotate;
  456. tmaxflag = 1;
  457. }
  458. break;
  459. }
  460. case '!':
  461. if (s[1]) {
  462. free(ld->processor);
  463. ld->processor = wstrdup(s);
  464. }
  465. break;
  466. }
  467. s = np;
  468. }
  469. /* Convert "aa\nbb\ncc\n\0" to "aa\0bb\0cc\0\0" */
  470. s = ld->inst;
  471. while (s) {
  472. np = strchr(s, '\n');
  473. if (np) *np++ = '\0';
  474. s = np;
  475. }
  476. }
  477. /* open current */
  478. i = stat("current", &st);
  479. if (i != -1) {
  480. if (st.st_size && ! (st.st_mode & S_IXUSR)) {
  481. ld->fnsave[25] = '.';
  482. ld->fnsave[26] = 'u';
  483. ld->fnsave[27] = '\0';
  484. do {
  485. taia_now(&now);
  486. fmt_taia(ld->fnsave, &now);
  487. errno = 0;
  488. } while ((stat(ld->fnsave, &st) != -1) || (errno != ENOENT));
  489. while (rename("current", ld->fnsave) == -1)
  490. pause2cannot("rename current", ld->name);
  491. rmoldest(ld);
  492. i = -1;
  493. } else {
  494. /* Be paranoid: st.st_size can be not just bigger, but WIDER! */
  495. /* (bug in original svlogd. remove this comment when fixed there) */
  496. ld->size = (st.st_size > ld->sizemax) ? ld->sizemax : st.st_size;
  497. }
  498. } else {
  499. if (errno != ENOENT) {
  500. logdir_close(ld);
  501. warn2("cannot stat current", ld->name);
  502. while (fchdir(fdwdir) == -1)
  503. pause1cannot("change to initial working directory");
  504. return 0;
  505. }
  506. }
  507. while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1)
  508. pause2cannot("open current", ld->name);
  509. coe(ld->fdcur);
  510. while (fchmod(ld->fdcur, 0644) == -1)
  511. pause2cannot("set mode of current", ld->name);
  512. if (verbose) {
  513. if (i == 0) bb_error_msg(INFO"append: %s/current", ld->name);
  514. else bb_error_msg(INFO"new: %s/current", ld->name);
  515. }
  516. while (fchdir(fdwdir) == -1)
  517. pause1cannot("change to initial working directory");
  518. return 1;
  519. }
  520. static void logdirs_reopen(void)
  521. {
  522. struct taia now;
  523. int l;
  524. int ok = 0;
  525. tmaxflag = 0;
  526. taia_now(&now);
  527. for (l = 0; l < dirn; ++l) {
  528. logdir_close(&dir[l]);
  529. if (logdir_open(&dir[l], fndir[l])) ok = 1;
  530. }
  531. if (!ok) fatalx("no functional log directories");
  532. }
  533. /* Used for reading stdin */
  534. static int buffer_pread(int fd, char *s, unsigned len)
  535. {
  536. struct taia now;
  537. int i;
  538. if (rotateasap) {
  539. for (i = 0; i < dirn; ++i)
  540. rotate(dir+i);
  541. rotateasap = 0;
  542. }
  543. if (exitasap) {
  544. if (linecomplete)
  545. return 0;
  546. len = 1;
  547. }
  548. if (reopenasap) {
  549. logdirs_reopen();
  550. reopenasap = 0;
  551. }
  552. taia_now(&now);
  553. taia_uint(&trotate, 2744);
  554. taia_add(&trotate, &now, &trotate);
  555. for (i = 0; i < dirn; ++i)
  556. if (dir[i].tmax) {
  557. if (taia_less(&dir[i].trotate, &now))
  558. rotate(dir+i);
  559. if (taia_less(&dir[i].trotate, &trotate))
  560. trotate = dir[i].trotate;
  561. }
  562. while (1) {
  563. /* Comment? */
  564. sig_unblock(sig_term);
  565. sig_unblock(sig_child);
  566. sig_unblock(sig_alarm);
  567. sig_unblock(sig_hangup);
  568. iopause(&in, 1, &trotate, &now);
  569. sig_block(sig_term);
  570. sig_block(sig_child);
  571. sig_block(sig_alarm);
  572. sig_block(sig_hangup);
  573. i = safe_read(fd, s, len);
  574. if (i >= 0) break;
  575. if (errno != EAGAIN) {
  576. warn("cannot read standard input");
  577. break;
  578. }
  579. /* else: EAGAIN - normal, repeat silently */
  580. }
  581. if (i > 0) {
  582. int cnt;
  583. linecomplete = (s[i-1] == '\n');
  584. if (!repl) return i;
  585. cnt = i;
  586. while (--cnt >= 0) {
  587. char ch = *s;
  588. if (ch != '\n') {
  589. if (ch < 32 || ch > 126)
  590. *s = repl;
  591. else {
  592. int j;
  593. for (j = 0; replace[j]; ++j) {
  594. if (ch == replace[j]) {
  595. *s = repl;
  596. break;
  597. }
  598. }
  599. }
  600. }
  601. s++;
  602. }
  603. }
  604. return i;
  605. }
  606. static void sig_term_handler(int sig_no)
  607. {
  608. if (verbose)
  609. bb_error_msg(INFO"sig%s received", "term");
  610. exitasap = 1;
  611. }
  612. static void sig_child_handler(int sig_no)
  613. {
  614. int pid, l;
  615. if (verbose)
  616. bb_error_msg(INFO"sig%s received", "child");
  617. while ((pid = wait_nohang(&wstat)) > 0)
  618. for (l = 0; l < dirn; ++l)
  619. if (dir[l].ppid == pid) {
  620. dir[l].ppid = 0;
  621. processorstop(&dir[l]);
  622. break;
  623. }
  624. }
  625. static void sig_alarm_handler(int sig_no)
  626. {
  627. if (verbose)
  628. bb_error_msg(INFO"sig%s received", "alarm");
  629. rotateasap = 1;
  630. }
  631. static void sig_hangup_handler(int sig_no)
  632. {
  633. if (verbose)
  634. bb_error_msg(INFO"sig%s received", "hangup");
  635. reopenasap = 1;
  636. }
  637. static void logmatch(struct logdir *ld)
  638. {
  639. char *s;
  640. ld->match = '+';
  641. ld->matcherr = 'E';
  642. s = ld->inst;
  643. while (s && s[0]) {
  644. switch (s[0]) {
  645. case '+':
  646. case '-':
  647. if (pmatch(s+1, line, linelen))
  648. ld->match = s[0];
  649. break;
  650. case 'e':
  651. case 'E':
  652. if (pmatch(s+1, line, linelen))
  653. ld->matcherr = s[0];
  654. break;
  655. }
  656. s += strlen(s) + 1;
  657. }
  658. }
  659. int svlogd_main(int argc, char **argv)
  660. {
  661. struct taia now;
  662. char *r,*l,*b;
  663. ssize_t stdin_cnt = 0;
  664. int i;
  665. unsigned opt;
  666. unsigned timestamp = 0;
  667. opt_complementary = "tt:vv";
  668. opt = getopt32(argc, argv, "r:R:l:b:tv",
  669. &r, &replace, &l, &b, &timestamp, &verbose);
  670. if (opt & 1) { // -r
  671. repl = r[0];
  672. if (!repl || r[1]) usage();
  673. }
  674. if (opt & 2) if (!repl) repl = '_'; // -R
  675. if (opt & 4) { // -l
  676. linemax = xatou_range(l, 0, 1000);
  677. if (linemax == 0) linemax = 1000;
  678. if (linemax < 256) linemax = 256;
  679. }
  680. if (opt & 8) { // -b
  681. buflen = xatoi_u(b);
  682. if (buflen == 0) buflen = 1024;
  683. }
  684. //if (opt & 0x10) timestamp++; // -t
  685. //if (opt & 0x20) verbose++; // -v
  686. if (timestamp > 2) timestamp = 2;
  687. argv += optind;
  688. argc -= optind;
  689. dirn = argc;
  690. if (dirn <= 0) usage();
  691. if (buflen <= linemax) usage();
  692. fdwdir = xopen(".", O_RDONLY|O_NDELAY);
  693. coe(fdwdir);
  694. dir = xmalloc(dirn * sizeof(struct logdir));
  695. for (i = 0; i < dirn; ++i) {
  696. dir[i].fddir = -1;
  697. dir[i].fdcur = -1;
  698. dir[i].btmp = xmalloc(buflen);
  699. dir[i].ppid = 0;
  700. }
  701. line = xmalloc(linemax + (timestamp ? 26 : 0));
  702. fndir = argv;
  703. in.fd = 0;
  704. in.events = IOPAUSE_READ;
  705. ndelay_on(in.fd);
  706. sig_block(sig_term);
  707. sig_block(sig_child);
  708. sig_block(sig_alarm);
  709. sig_block(sig_hangup);
  710. sig_catch(sig_term, sig_term_handler);
  711. sig_catch(sig_child, sig_child_handler);
  712. sig_catch(sig_alarm, sig_alarm_handler);
  713. sig_catch(sig_hangup, sig_hangup_handler);
  714. logdirs_reopen();
  715. /* Each iteration processes one line */
  716. while (1) {
  717. int printlen;
  718. char *lineptr = line;
  719. char *np;
  720. char ch;
  721. /* Prepare timestamp if needed */
  722. if (timestamp) {
  723. char stamp[FMT_PTIME];
  724. taia_now(&now);
  725. switch (timestamp) {
  726. case 1:
  727. fmt_taia(stamp, &now);
  728. break;
  729. default: /* case 2: */
  730. fmt_ptime(stamp, &now);
  731. break;
  732. }
  733. memcpy(line, stamp, 25);
  734. line[25] = ' ';
  735. lineptr += 26;
  736. }
  737. /* lineptr[0..linemax-1] - buffer for stdin */
  738. /* (possibly has some unprocessed data from prev loop) */
  739. /* Refill the buffer if needed */
  740. np = memchr(lineptr, '\n', stdin_cnt);
  741. i = linemax - stdin_cnt; /* avail. bytes at tail */
  742. if (i >= 128 && !exitasap && !np) {
  743. int sz = buffer_pread(0, lineptr + stdin_cnt, i);
  744. if (sz <= 0) /* EOF or error on stdin */
  745. exitasap = 1;
  746. else {
  747. stdin_cnt += sz;
  748. np = memchr(lineptr, '\n', stdin_cnt);
  749. }
  750. }
  751. if (stdin_cnt <= 0 && exitasap)
  752. break;
  753. /* Search for '\n' (in fact, np already holds the result) */
  754. linelen = stdin_cnt;
  755. if (np) linelen = np - lineptr + 1;
  756. /* linelen == no of chars incl. '\n' (or == stdin_cnt) */
  757. ch = lineptr[linelen-1];
  758. printlen = linelen + (timestamp ? 26 : 0);
  759. /* write out line[0..printlen-1] to each log destination */
  760. for (i = 0; i < dirn; ++i) {
  761. struct logdir *ld = &dir[i];
  762. if (ld->fddir == -1) continue;
  763. if (ld->inst)
  764. logmatch(ld);
  765. if (ld->matcherr == 'e')
  766. full_write(2, line, printlen);
  767. if (ld->match != '+') continue;
  768. buffer_pwrite(i, line, printlen);
  769. }
  770. /* If we didn't see '\n' (long input line), */
  771. /* read/write repeatedly until we see it */
  772. while (ch != '\n') {
  773. /* lineptr is emptied now, safe to use as buffer */
  774. stdin_cnt = exitasap ? -1 : buffer_pread(0, lineptr, linemax);
  775. if (stdin_cnt <= 0) { /* EOF or error on stdin */
  776. lineptr[0] = ch = '\n';
  777. linelen = 1;
  778. exitasap = 1;
  779. stdin_cnt = 1;
  780. } else {
  781. linelen = stdin_cnt;
  782. np = memchr(lineptr, '\n', stdin_cnt);
  783. if (np) linelen = np - lineptr + 1;
  784. ch = lineptr[linelen-1];
  785. }
  786. /* linelen == no of chars incl. '\n' (or == stdin_cnt) */
  787. for (i = 0; i < dirn; ++i) {
  788. if (dir[i].fddir == -1) continue;
  789. if (dir[i].matcherr == 'e')
  790. full_write(2, lineptr, linelen);
  791. if (dir[i].match != '+') continue;
  792. buffer_pwrite(i, lineptr, linelen);
  793. }
  794. }
  795. /* Move unprocessed data to the front of line */
  796. stdin_cnt -= linelen;
  797. if (stdin_cnt > 0) /* TODO: slow if buffer is big */
  798. memmove(lineptr, &lineptr[linelen], stdin_cnt);
  799. }
  800. for (i = 0; i < dirn; ++i) {
  801. if (dir[i].ppid)
  802. while (!processorstop(&dir[i]))
  803. /* repeat */;
  804. logdir_close(&dir[i]);
  805. }
  806. _exit(0);
  807. }