svlogd.c 32 KB

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