svlogd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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 input;
  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;
  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(const char *m0)
  75. {
  76. bb_error_msg_and_die(FATAL"%s", m0);
  77. }
  78. static void warn(const char *m0) {
  79. bb_perror_msg(WARNING"%s", m0);
  80. }
  81. static void warn2(const char *m0, const char *m1)
  82. {
  83. bb_perror_msg(WARNING"%s: %s", m0, m1);
  84. }
  85. static void warnx(const char *m0, const 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");
  92. sleep(3);
  93. }
  94. static void pause1cannot(const char *m0)
  95. {
  96. bb_perror_msg(PAUSE"cannot %s", m0);
  97. sleep(3);
  98. }
  99. static void pause2cannot(const char *m0, const char *m1)
  100. {
  101. bb_perror_msg(PAUSE"cannot %s %s", m0, m1);
  102. sleep(3);
  103. }
  104. static char* wstrdup(const char *str)
  105. {
  106. char *s;
  107. while (!(s = strdup(str)))
  108. pause_nomem();
  109. return s;
  110. }
  111. static unsigned processorstart(struct logdir *ld)
  112. {
  113. int pid;
  114. if (!ld->processor) return 0;
  115. if (ld->ppid) {
  116. warnx("processor already running", ld->name);
  117. return 0;
  118. }
  119. while ((pid = fork()) == -1)
  120. pause2cannot("fork for processor", ld->name);
  121. if (!pid) {
  122. char *prog[4];
  123. int fd;
  124. /* child */
  125. sig_uncatch(SIGTERM);
  126. sig_uncatch(SIGALRM);
  127. sig_uncatch(SIGHUP);
  128. sig_unblock(SIGTERM);
  129. sig_unblock(SIGALRM);
  130. sig_unblock(SIGHUP);
  131. if (verbose)
  132. bb_error_msg(INFO"processing: %s/%s", ld->name, ld->fnsave);
  133. fd = xopen(ld->fnsave, O_RDONLY|O_NDELAY);
  134. if (fd_move(0, fd) == -1)
  135. bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
  136. ld->fnsave[26] = 't';
  137. fd = xopen(ld->fnsave, O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
  138. if (fd_move(1, fd) == -1)
  139. bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
  140. fd = open_read("state");
  141. if (fd == -1) {
  142. if (errno != ENOENT)
  143. bb_perror_msg_and_die(FATAL"cannot %s processor %s", "open state for", ld->name);
  144. close(xopen("state", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT));
  145. fd = xopen("state", O_RDONLY|O_NDELAY);
  146. }
  147. if (fd_move(4, fd) == -1)
  148. bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
  149. fd = xopen("newstate", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
  150. if (fd_move(5, fd) == -1)
  151. bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
  152. // getenv("SHELL")?
  153. prog[0] = (char*)"sh";
  154. prog[1] = (char*)"-c";
  155. prog[2] = ld->processor;
  156. prog[3] = '\0';
  157. execve("/bin/sh", prog, environ);
  158. bb_perror_msg_and_die(FATAL"cannot %s processor %s", "run", ld->name);
  159. }
  160. ld->ppid = pid;
  161. return 1;
  162. }
  163. static unsigned processorstop(struct logdir *ld)
  164. {
  165. char f[28];
  166. if (ld->ppid) {
  167. sig_unblock(SIGHUP);
  168. while (wait_pid(&wstat, ld->ppid) == -1)
  169. pause2cannot("wait for processor", ld->name);
  170. sig_block(SIGHUP);
  171. ld->ppid = 0;
  172. }
  173. if (ld->fddir == -1) return 1;
  174. while (fchdir(ld->fddir) == -1)
  175. pause2cannot("change directory, want processor", ld->name);
  176. if (wait_exitcode(wstat) != 0) {
  177. warnx("processor failed, restart", ld->name);
  178. ld->fnsave[26] = 't';
  179. unlink(ld->fnsave);
  180. ld->fnsave[26] = 'u';
  181. processorstart(ld);
  182. while (fchdir(fdwdir) == -1)
  183. pause1cannot("change to initial working directory");
  184. return ld->processor ? 0 : 1;
  185. }
  186. ld->fnsave[26] = 't';
  187. memcpy(f, ld->fnsave, 26);
  188. f[26] = 's';
  189. f[27] = '\0';
  190. while (rename(ld->fnsave, f) == -1)
  191. pause2cannot("rename processed", ld->name);
  192. while (chmod(f, 0744) == -1)
  193. pause2cannot("set mode of processed", ld->name);
  194. ld->fnsave[26] = 'u';
  195. if (unlink(ld->fnsave) == -1)
  196. bb_error_msg(WARNING"cannot unlink: %s/%s", ld->name, ld->fnsave);
  197. while (rename("newstate", "state") == -1)
  198. pause2cannot("rename state", ld->name);
  199. if (verbose)
  200. bb_error_msg(INFO"processed: %s/%s", ld->name, f);
  201. while (fchdir(fdwdir) == -1)
  202. pause1cannot("change to initial working directory");
  203. return 1;
  204. }
  205. static void rmoldest(struct logdir *ld)
  206. {
  207. DIR *d;
  208. struct dirent *f;
  209. char oldest[FMT_PTIME];
  210. int n = 0;
  211. oldest[0] = 'A'; oldest[1] = oldest[27] = 0;
  212. while (!(d = opendir(".")))
  213. pause2cannot("open directory, want rotate", ld->name);
  214. errno = 0;
  215. while ((f = readdir(d))) {
  216. if ((f->d_name[0] == '@') && (strlen(f->d_name) == 27)) {
  217. if (f->d_name[26] == 't') {
  218. if (unlink(f->d_name) == -1)
  219. warn2("cannot unlink processor leftover", f->d_name);
  220. } else {
  221. ++n;
  222. if (strcmp(f->d_name, oldest) < 0)
  223. memcpy(oldest, f->d_name, 27);
  224. }
  225. errno = 0;
  226. }
  227. }
  228. if (errno)
  229. warn2("cannot read directory", ld->name);
  230. closedir(d);
  231. if (ld->nmax && (n > ld->nmax)) {
  232. if (verbose)
  233. bb_error_msg(INFO"delete: %s/%s", ld->name, oldest);
  234. if ((*oldest == '@') && (unlink(oldest) == -1))
  235. warn2("cannot unlink oldest logfile", ld->name);
  236. }
  237. }
  238. static unsigned rotate(struct logdir *ld)
  239. {
  240. struct stat st;
  241. struct taia now;
  242. if (ld->fddir == -1) {
  243. ld->tmax = 0;
  244. return 0;
  245. }
  246. if (ld->ppid)
  247. while (!processorstop(ld))
  248. /* wait */;
  249. while (fchdir(ld->fddir) == -1)
  250. pause2cannot("change directory, want rotate", ld->name);
  251. /* create new filename */
  252. ld->fnsave[25] = '.';
  253. ld->fnsave[26] = 's';
  254. if (ld->processor)
  255. ld->fnsave[26] = 'u';
  256. ld->fnsave[27] = '\0';
  257. do {
  258. taia_now(&now);
  259. fmt_taia25(ld->fnsave, &now);
  260. errno = 0;
  261. stat(ld->fnsave, &st);
  262. } while (errno != ENOENT);
  263. if (ld->tmax && taia_less(&ld->trotate, &now)) {
  264. taia_uint(&ld->trotate, ld->tmax);
  265. taia_add(&ld->trotate, &now, &ld->trotate);
  266. if (taia_less(&ld->trotate, &trotate))
  267. trotate = ld->trotate;
  268. }
  269. if (ld->size > 0) {
  270. while (fsync(ld->fdcur) == -1)
  271. pause2cannot("fsync current logfile", ld->name);
  272. while (fchmod(ld->fdcur, 0744) == -1)
  273. pause2cannot("set mode of current", ld->name);
  274. close(ld->fdcur);
  275. if (verbose) {
  276. bb_error_msg(INFO"rename: %s/current %s %u", ld->name,
  277. ld->fnsave, ld->size);
  278. }
  279. while (rename("current", ld->fnsave) == -1)
  280. pause2cannot("rename current", ld->name);
  281. while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1)
  282. pause2cannot("create new current", ld->name);
  283. coe(ld->fdcur);
  284. ld->size = 0;
  285. while (fchmod(ld->fdcur, 0644) == -1)
  286. pause2cannot("set mode of current", ld->name);
  287. rmoldest(ld);
  288. processorstart(ld);
  289. }
  290. while (fchdir(fdwdir) == -1)
  291. pause1cannot("change to initial working directory");
  292. return 1;
  293. }
  294. static int buffer_pwrite(int n, char *s, unsigned len)
  295. {
  296. int i;
  297. struct logdir *ld = &dir[n];
  298. if (ld->sizemax) {
  299. if (ld->size >= ld->sizemax)
  300. rotate(ld);
  301. if (len > (ld->sizemax - ld->size))
  302. len = ld->sizemax - ld->size;
  303. }
  304. while ((i = write(ld->fdcur, s, len)) == -1) {
  305. if ((errno == ENOSPC) && (ld->nmin < ld->nmax)) {
  306. DIR *d;
  307. struct dirent *f;
  308. char oldest[FMT_PTIME];
  309. int j = 0;
  310. while (fchdir(ld->fddir) == -1)
  311. pause2cannot("change directory, want remove old logfile",
  312. ld->name);
  313. oldest[0] = 'A';
  314. oldest[1] = oldest[27] = '\0';
  315. while (!(d = opendir(".")))
  316. pause2cannot("open directory, want remove old logfile",
  317. ld->name);
  318. errno = 0;
  319. while ((f = readdir(d)))
  320. if ((f->d_name[0] == '@') && (strlen(f->d_name) == 27)) {
  321. ++j;
  322. if (strcmp(f->d_name, oldest) < 0)
  323. memcpy(oldest, f->d_name, 27);
  324. }
  325. if (errno) warn2("cannot read directory, want remove old logfile",
  326. ld->name);
  327. closedir(d);
  328. errno = ENOSPC;
  329. if (j > ld->nmin) {
  330. if (*oldest == '@') {
  331. bb_error_msg(WARNING"out of disk space, delete: %s/%s",
  332. ld->name, oldest);
  333. errno = 0;
  334. if (unlink(oldest) == -1) {
  335. warn2("cannot unlink oldest logfile", ld->name);
  336. errno = ENOSPC;
  337. }
  338. while (fchdir(fdwdir) == -1)
  339. pause1cannot("change to initial working directory");
  340. }
  341. }
  342. }
  343. if (errno)
  344. pause2cannot("write to current", ld->name);
  345. }
  346. ld->size += i;
  347. if (ld->sizemax)
  348. if (s[i-1] == '\n')
  349. if (ld->size >= (ld->sizemax - linemax))
  350. rotate(ld);
  351. return i;
  352. }
  353. static void logdir_close(struct logdir *ld)
  354. {
  355. if (ld->fddir == -1)
  356. return;
  357. if (verbose)
  358. bb_error_msg(INFO"close: %s", ld->name);
  359. close(ld->fddir);
  360. ld->fddir = -1;
  361. if (ld->fdcur == -1)
  362. return; /* impossible */
  363. while (fsync(ld->fdcur) == -1)
  364. pause2cannot("fsync current logfile", ld->name);
  365. while (fchmod(ld->fdcur, 0744) == -1)
  366. pause2cannot("set mode of current", ld->name);
  367. close(ld->fdcur);
  368. ld->fdcur = -1;
  369. if (ld->fdlock == -1)
  370. return; /* impossible */
  371. close(ld->fdlock);
  372. ld->fdlock = -1;
  373. free(ld->processor);
  374. ld->processor = NULL;
  375. }
  376. static unsigned logdir_open(struct logdir *ld, const char *fn)
  377. {
  378. char buf[128];
  379. struct taia now;
  380. char *new, *s, *np;
  381. int i;
  382. struct stat st;
  383. ld->fddir = open(fn, O_RDONLY|O_NDELAY);
  384. if (ld->fddir == -1) {
  385. warn2("cannot open log directory", (char*)fn);
  386. return 0;
  387. }
  388. coe(ld->fddir);
  389. if (fchdir(ld->fddir) == -1) {
  390. logdir_close(ld);
  391. warn2("cannot change directory", (char*)fn);
  392. return 0;
  393. }
  394. ld->fdlock = open("lock", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
  395. if ((ld->fdlock == -1)
  396. || (lock_exnb(ld->fdlock) == -1)
  397. ) {
  398. logdir_close(ld);
  399. warn2("cannot lock directory", (char*)fn);
  400. while (fchdir(fdwdir) == -1)
  401. pause1cannot("change to initial working directory");
  402. return 0;
  403. }
  404. coe(ld->fdlock);
  405. ld->size = 0;
  406. ld->sizemax = 1000000;
  407. ld->nmax = ld->nmin = 10;
  408. ld->tmax = 0;
  409. ld->name = (char*)fn;
  410. ld->ppid = 0;
  411. ld->match = '+';
  412. free(ld->inst); ld->inst = NULL;
  413. free(ld->processor); ld->processor = NULL;
  414. /* read config */
  415. i = open_read_close("config", buf, sizeof(buf));
  416. if (i < 0)
  417. warn2("cannot read config", ld->name);
  418. if (i > 0) {
  419. if (verbose) bb_error_msg(INFO"read: %s/config", ld->name);
  420. s = buf;
  421. while (s) {
  422. np = strchr(s, '\n');
  423. if (np) *np++ = '\0';
  424. switch (s[0]) {
  425. case '+':
  426. case '-':
  427. case 'e':
  428. case 'E':
  429. while (1) {
  430. int l = asprintf(&new, "%s%s\n", ld->inst?:"", s);
  431. if (l >= 0 && new) break;
  432. pause_nomem();
  433. }
  434. free(ld->inst);
  435. ld->inst = new;
  436. break;
  437. case 's': {
  438. static const struct suffix_mult km_suffixes[] = {
  439. { "k", 1024 },
  440. { "m", 1024*1024 },
  441. { NULL, 0 }
  442. };
  443. ld->sizemax = xatou_sfx(&s[1], km_suffixes);
  444. break;
  445. }
  446. case 'n':
  447. ld->nmax = xatoi_u(&s[1]);
  448. break;
  449. case 'N':
  450. ld->nmin = xatoi_u(&s[1]);
  451. break;
  452. case 't': {
  453. static const struct suffix_mult mh_suffixes[] = {
  454. { "m", 60 },
  455. { "h", 60*60 },
  456. /*{ "d", 24*60*60 },*/
  457. { NULL, 0 }
  458. };
  459. ld->tmax = xatou_sfx(&s[1], mh_suffixes);
  460. if (ld->tmax) {
  461. taia_uint(&ld->trotate, ld->tmax);
  462. taia_add(&ld->trotate, &now, &ld->trotate);
  463. if (!tmaxflag || taia_less(&ld->trotate, &trotate))
  464. trotate = ld->trotate;
  465. tmaxflag = 1;
  466. }
  467. break;
  468. }
  469. case '!':
  470. if (s[1]) {
  471. free(ld->processor);
  472. ld->processor = wstrdup(s);
  473. }
  474. break;
  475. }
  476. s = np;
  477. }
  478. /* Convert "aa\nbb\ncc\n\0" to "aa\0bb\0cc\0\0" */
  479. s = ld->inst;
  480. while (s) {
  481. np = strchr(s, '\n');
  482. if (np) *np++ = '\0';
  483. s = np;
  484. }
  485. }
  486. /* open current */
  487. i = stat("current", &st);
  488. if (i != -1) {
  489. if (st.st_size && ! (st.st_mode & S_IXUSR)) {
  490. ld->fnsave[25] = '.';
  491. ld->fnsave[26] = 'u';
  492. ld->fnsave[27] = '\0';
  493. do {
  494. taia_now(&now);
  495. fmt_taia25(ld->fnsave, &now);
  496. errno = 0;
  497. stat(ld->fnsave, &st);
  498. } while (errno != ENOENT);
  499. while (rename("current", ld->fnsave) == -1)
  500. pause2cannot("rename current", ld->name);
  501. rmoldest(ld);
  502. i = -1;
  503. } else {
  504. /* Be paranoid: st.st_size can be not just bigger, but WIDER! */
  505. /* (bug in original svlogd. remove this comment when fixed there) */
  506. ld->size = (st.st_size > ld->sizemax) ? ld->sizemax : st.st_size;
  507. }
  508. } else {
  509. if (errno != ENOENT) {
  510. logdir_close(ld);
  511. warn2("cannot stat current", ld->name);
  512. while (fchdir(fdwdir) == -1)
  513. pause1cannot("change to initial working directory");
  514. return 0;
  515. }
  516. }
  517. while ((ld->fdcur = open("current", O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600)) == -1)
  518. pause2cannot("open current", ld->name);
  519. coe(ld->fdcur);
  520. while (fchmod(ld->fdcur, 0644) == -1)
  521. pause2cannot("set mode of current", ld->name);
  522. if (verbose) {
  523. if (i == 0) bb_error_msg(INFO"append: %s/current", ld->name);
  524. else bb_error_msg(INFO"new: %s/current", ld->name);
  525. }
  526. while (fchdir(fdwdir) == -1)
  527. pause1cannot("change to initial working directory");
  528. return 1;
  529. }
  530. static void logdirs_reopen(void)
  531. {
  532. struct taia now;
  533. int l;
  534. int ok = 0;
  535. tmaxflag = 0;
  536. taia_now(&now);
  537. for (l = 0; l < dirn; ++l) {
  538. logdir_close(&dir[l]);
  539. if (logdir_open(&dir[l], fndir[l])) ok = 1;
  540. }
  541. if (!ok) fatalx("no functional log directories");
  542. }
  543. /* Used for reading stdin */
  544. static int buffer_pread(int fd, char *s, unsigned len)
  545. {
  546. struct taia now;
  547. int i;
  548. if (rotateasap) {
  549. for (i = 0; i < dirn; ++i)
  550. rotate(dir+i);
  551. rotateasap = 0;
  552. }
  553. if (exitasap) {
  554. if (linecomplete)
  555. return 0;
  556. len = 1;
  557. }
  558. if (reopenasap) {
  559. logdirs_reopen();
  560. reopenasap = 0;
  561. }
  562. taia_now(&now);
  563. taia_uint(&trotate, 2744);
  564. taia_add(&trotate, &now, &trotate);
  565. for (i = 0; i < dirn; ++i)
  566. if (dir[i].tmax) {
  567. if (taia_less(&dir[i].trotate, &now))
  568. rotate(dir+i);
  569. if (taia_less(&dir[i].trotate, &trotate))
  570. trotate = dir[i].trotate;
  571. }
  572. while (1) {
  573. /* Comment? */
  574. sig_unblock(SIGTERM);
  575. sig_unblock(SIGCHLD);
  576. sig_unblock(SIGALRM);
  577. sig_unblock(SIGHUP);
  578. iopause(&input, 1, &trotate, &now);
  579. sig_block(SIGTERM);
  580. sig_block(SIGCHLD);
  581. sig_block(SIGALRM);
  582. sig_block(SIGHUP);
  583. i = safe_read(fd, s, len);
  584. if (i >= 0) break;
  585. if (errno != EAGAIN) {
  586. warn("cannot read standard input");
  587. break;
  588. }
  589. /* else: EAGAIN - normal, repeat silently */
  590. }
  591. if (i > 0) {
  592. int cnt;
  593. linecomplete = (s[i-1] == '\n');
  594. if (!repl) return i;
  595. cnt = i;
  596. while (--cnt >= 0) {
  597. char ch = *s;
  598. if (ch != '\n') {
  599. if (ch < 32 || ch > 126)
  600. *s = repl;
  601. else {
  602. int j;
  603. for (j = 0; replace[j]; ++j) {
  604. if (ch == replace[j]) {
  605. *s = repl;
  606. break;
  607. }
  608. }
  609. }
  610. }
  611. s++;
  612. }
  613. }
  614. return i;
  615. }
  616. static void sig_term_handler(int sig_no)
  617. {
  618. if (verbose)
  619. bb_error_msg(INFO"sig%s received", "term");
  620. exitasap = 1;
  621. }
  622. static void sig_child_handler(int sig_no)
  623. {
  624. int pid, l;
  625. if (verbose)
  626. bb_error_msg(INFO"sig%s received", "child");
  627. while ((pid = wait_nohang(&wstat)) > 0)
  628. for (l = 0; l < dirn; ++l)
  629. if (dir[l].ppid == pid) {
  630. dir[l].ppid = 0;
  631. processorstop(&dir[l]);
  632. break;
  633. }
  634. }
  635. static void sig_alarm_handler(int sig_no)
  636. {
  637. if (verbose)
  638. bb_error_msg(INFO"sig%s received", "alarm");
  639. rotateasap = 1;
  640. }
  641. static void sig_hangup_handler(int sig_no)
  642. {
  643. if (verbose)
  644. bb_error_msg(INFO"sig%s received", "hangup");
  645. reopenasap = 1;
  646. }
  647. static void logmatch(struct logdir *ld)
  648. {
  649. char *s;
  650. ld->match = '+';
  651. ld->matcherr = 'E';
  652. s = ld->inst;
  653. while (s && s[0]) {
  654. switch (s[0]) {
  655. case '+':
  656. case '-':
  657. if (pmatch(s+1, line, linelen))
  658. ld->match = s[0];
  659. break;
  660. case 'e':
  661. case 'E':
  662. if (pmatch(s+1, line, linelen))
  663. ld->matcherr = s[0];
  664. break;
  665. }
  666. s += strlen(s) + 1;
  667. }
  668. }
  669. int svlogd_main(int argc, char **argv)
  670. {
  671. struct taia now;
  672. char *r,*l,*b;
  673. ssize_t stdin_cnt = 0;
  674. int i;
  675. unsigned opt;
  676. unsigned timestamp = 0;
  677. void* (*memRchr)(const void *, int, size_t) = memchr;
  678. #define line bb_common_bufsiz1
  679. opt_complementary = "tt:vv";
  680. opt = getopt32(argc, argv, "r:R:l:b:tv",
  681. &r, &replace, &l, &b, &timestamp, &verbose);
  682. if (opt & 1) { // -r
  683. repl = r[0];
  684. if (!repl || r[1]) usage();
  685. }
  686. if (opt & 2) if (!repl) repl = '_'; // -R
  687. if (opt & 4) { // -l
  688. linemax = xatou_range(l, 0, BUFSIZ-26);
  689. if (linemax == 0) linemax = BUFSIZ-26;
  690. if (linemax < 256) linemax = 256;
  691. }
  692. //// if (opt & 8) { // -b
  693. //// buflen = xatoi_u(b);
  694. //// if (buflen == 0) buflen = 1024;
  695. //// }
  696. //if (opt & 0x10) timestamp++; // -t
  697. //if (opt & 0x20) verbose++; // -v
  698. //if (timestamp > 2) timestamp = 2;
  699. argv += optind;
  700. argc -= optind;
  701. dirn = argc;
  702. if (dirn <= 0) usage();
  703. ////if (buflen <= linemax) usage();
  704. fdwdir = xopen(".", O_RDONLY|O_NDELAY);
  705. coe(fdwdir);
  706. dir = xmalloc(dirn * sizeof(struct logdir));
  707. for (i = 0; i < dirn; ++i) {
  708. dir[i].fddir = -1;
  709. dir[i].fdcur = -1;
  710. ////dir[i].btmp = xmalloc(buflen);
  711. dir[i].ppid = 0;
  712. }
  713. /* line = xmalloc(linemax + (timestamp ? 26 : 0)); */
  714. fndir = argv;
  715. input.fd = 0;
  716. input.events = IOPAUSE_READ;
  717. /* I be damned. Linux 2.6.18: this somehow affects
  718. * OTHER processes! Konsole starts to redraw itself much slower!
  719. * This persists even after svlogd exits */
  720. ndelay_on(input.fd);
  721. sig_block(SIGTERM);
  722. sig_block(SIGCHLD);
  723. sig_block(SIGALRM);
  724. sig_block(SIGHUP);
  725. sig_catch(SIGTERM, sig_term_handler);
  726. sig_catch(SIGCHLD, sig_child_handler);
  727. sig_catch(SIGALRM, sig_alarm_handler);
  728. sig_catch(SIGHUP, sig_hangup_handler);
  729. logdirs_reopen();
  730. /* Without timestamps, we don't have to print each line
  731. * separately, so we can look for _last_ newline, not first,
  732. * thus batching writes */
  733. if (!timestamp)
  734. memRchr = memrchr;
  735. /* Each iteration processes one or more lines */
  736. while (1) {
  737. char stamp[FMT_PTIME];
  738. char *lineptr;
  739. char *printptr;
  740. char *np;
  741. int printlen;
  742. char ch;
  743. lineptr = line;
  744. /* Prepare timestamp if needed */
  745. if (timestamp) {
  746. taia_now(&now);
  747. switch (timestamp) {
  748. case 1:
  749. fmt_taia25(stamp, &now);
  750. break;
  751. default: /* case 2: */
  752. fmt_ptime30nul(stamp, &now);
  753. break;
  754. }
  755. lineptr += 26;
  756. }
  757. /* lineptr[0..linemax-1] - buffer for stdin */
  758. /* (possibly has some unprocessed data from prev loop) */
  759. /* Refill the buffer if needed */
  760. np = memRchr(lineptr, '\n', stdin_cnt);
  761. if (!np && !exitasap) {
  762. i = linemax - stdin_cnt; /* avail. bytes at tail */
  763. if (i >= 128) {
  764. i = buffer_pread(0, lineptr + stdin_cnt, i);
  765. if (i <= 0) /* EOF or error on stdin */
  766. exitasap = 1;
  767. else {
  768. np = memRchr(lineptr + stdin_cnt, '\n', i);
  769. stdin_cnt += i;
  770. }
  771. }
  772. }
  773. if (stdin_cnt <= 0 && exitasap)
  774. break;
  775. /* Search for '\n' (in fact, np already holds the result) */
  776. linelen = stdin_cnt;
  777. if (np) {
  778. print_to_nl: /* NB: starting from here lineptr may point
  779. * farther out into line[] */
  780. linelen = np - lineptr + 1;
  781. }
  782. /* linelen == no of chars incl. '\n' (or == stdin_cnt) */
  783. ch = lineptr[linelen-1];
  784. /* write out lineptr[0..linelen-1] to each log destination */
  785. /* (or lineptr[-26..linelen-1] if timestamping) */
  786. printlen = linelen;
  787. printptr = lineptr;
  788. if (timestamp) {
  789. printlen += 26;
  790. printptr -= 26;
  791. memcpy(printptr, stamp, 25);
  792. printptr[25] = ' ';
  793. }
  794. for (i = 0; i < dirn; ++i) {
  795. struct logdir *ld = &dir[i];
  796. if (ld->fddir == -1) continue;
  797. if (ld->inst)
  798. logmatch(ld);
  799. if (ld->matcherr == 'e')
  800. full_write(2, printptr, printlen);
  801. if (ld->match != '+') continue;
  802. buffer_pwrite(i, printptr, printlen);
  803. }
  804. /* If we didn't see '\n' (long input line), */
  805. /* read/write repeatedly until we see it */
  806. while (ch != '\n') {
  807. /* lineptr is emptied now, safe to use as buffer */
  808. stdin_cnt = exitasap ? -1 : buffer_pread(0, lineptr, linemax);
  809. if (stdin_cnt <= 0) { /* EOF or error on stdin */
  810. exitasap = 1;
  811. lineptr[0] = ch = '\n';
  812. linelen = 1;
  813. stdin_cnt = 1;
  814. } else {
  815. linelen = stdin_cnt;
  816. np = memRchr(lineptr, '\n', stdin_cnt);
  817. if (np)
  818. linelen = np - lineptr + 1;
  819. ch = lineptr[linelen-1];
  820. }
  821. /* linelen == no of chars incl. '\n' (or == stdin_cnt) */
  822. for (i = 0; i < dirn; ++i) {
  823. if (dir[i].fddir == -1) continue;
  824. if (dir[i].matcherr == 'e')
  825. full_write(2, lineptr, linelen);
  826. if (dir[i].match != '+') continue;
  827. buffer_pwrite(i, lineptr, linelen);
  828. }
  829. }
  830. stdin_cnt -= linelen;
  831. if (stdin_cnt > 0) {
  832. lineptr += linelen;
  833. /* If we see another '\n', we don't need to read
  834. * next piece of input: can print what we have */
  835. np = memRchr(lineptr, '\n', stdin_cnt);
  836. if (np)
  837. goto print_to_nl;
  838. /* Move unprocessed data to the front of line */
  839. memmove((timestamp ? line+26 : line), lineptr, stdin_cnt);
  840. }
  841. }
  842. for (i = 0; i < dirn; ++i) {
  843. if (dir[i].ppid)
  844. while (!processorstop(&dir[i]))
  845. /* repeat */;
  846. logdir_close(&dir[i]);
  847. }
  848. return 0;
  849. }