3
0

tail.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini tail implementation for busybox
  4. *
  5. * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  10. *
  11. * Pretty much rewritten to fix numerous bugs and reduce realloc() calls.
  12. * Bugs fixed (although I may have forgotten one or two... it was pretty bad)
  13. * 1) mixing printf/write without fflush()ing stdout
  14. * 2) no check that any open files are present
  15. * 3) optstring had -q taking an arg
  16. * 4) no error checking on write in some cases, and a warning even then
  17. * 5) q and s interaction bug
  18. * 6) no check for lseek error
  19. * 7) lseek attempted when count==0 even if arg was +0 (from top)
  20. */
  21. //config:config TAIL
  22. //config: bool "tail (7.1 kb)"
  23. //config: default y
  24. //config: help
  25. //config: tail is used to print the last specified number of lines
  26. //config: from files.
  27. //config:
  28. //config:config FEATURE_FANCY_TAIL
  29. //config: bool "Enable -q, -s, -v, and -F options"
  30. //config: default y
  31. //config: depends on TAIL
  32. //config: help
  33. //config: These options are provided by GNU tail, but
  34. //config: are not specified in the SUSv3 standard:
  35. //config: -q Never output headers giving file names
  36. //config: -s SEC Wait SEC seconds between reads with -f
  37. //config: -v Always output headers giving file names
  38. //config: -F Same as -f, but keep retrying
  39. //applet:IF_TAIL(APPLET(tail, BB_DIR_USR_BIN, BB_SUID_DROP))
  40. //kbuild:lib-$(CONFIG_TAIL) += tail.o
  41. /* BB_AUDIT SUSv3 compliant (need fancy for -c) */
  42. /* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
  43. /* http://www.opengroup.org/onlinepubs/007904975/utilities/tail.html */
  44. //usage:#define tail_trivial_usage
  45. //usage: "[OPTIONS] [FILE]..."
  46. //usage:#define tail_full_usage "\n\n"
  47. //usage: "Print last 10 lines of each FILE (or stdin) to stdout.\n"
  48. //usage: "With more than one FILE, precede each with a filename header.\n"
  49. //usage: "\n -f Print data as file grows"
  50. //usage: "\n -c [+]N[kbm] Print last N bytes"
  51. //usage: "\n -n N[kbm] Print last N lines"
  52. //usage: "\n -n +N[kbm] Start on Nth line and print the rest"
  53. //usage: IF_FEATURE_FANCY_TAIL(
  54. //usage: "\n -q Never print headers"
  55. //usage: "\n -s SECONDS Wait SECONDS between reads with -f"
  56. //usage: "\n -v Always print headers"
  57. //usage: "\n -F Same as -f, but keep retrying"
  58. //usage: "\n"
  59. //usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
  60. //usage: )
  61. //usage:
  62. //usage:#define tail_example_usage
  63. //usage: "$ tail -n 1 /etc/resolv.conf\n"
  64. //usage: "nameserver 10.0.0.1\n"
  65. #include "libbb.h"
  66. #include "common_bufsiz.h"
  67. struct globals {
  68. bool from_top;
  69. bool exitcode;
  70. } FIX_ALIASING;
  71. #define G (*(struct globals*)bb_common_bufsiz1)
  72. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  73. static void tail_xprint_header(const char *fmt, const char *filename)
  74. {
  75. if (fdprintf(STDOUT_FILENO, fmt, filename) < 0)
  76. bb_perror_nomsg_and_die();
  77. }
  78. static ssize_t tail_read(int fd, char *buf, size_t count)
  79. {
  80. ssize_t r;
  81. r = full_read(fd, buf, count);
  82. if (r < 0) {
  83. bb_perror_msg(bb_msg_read_error);
  84. G.exitcode = EXIT_FAILURE;
  85. }
  86. return r;
  87. }
  88. #define header_fmt_str "\n==> %s <==\n"
  89. static unsigned eat_num(const char *p)
  90. {
  91. if (*p == '-')
  92. p++;
  93. else if (*p == '+') {
  94. p++;
  95. G.from_top = 1;
  96. }
  97. return xatou_sfx(p, bkm_suffixes);
  98. }
  99. int tail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  100. int tail_main(int argc, char **argv)
  101. {
  102. unsigned count = 10;
  103. unsigned sleep_period = 1;
  104. const char *str_c, *str_n;
  105. char *tailbuf;
  106. size_t tailbufsize;
  107. unsigned header_threshhold = 1;
  108. unsigned nfiles;
  109. int i, opt;
  110. int *fds;
  111. const char *fmt;
  112. int prev_fd;
  113. INIT_G();
  114. #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
  115. /* Allow legacy syntax of an initial numeric option without -n. */
  116. if (argv[1] && (argv[1][0] == '+' || argv[1][0] == '-')
  117. && isdigit(argv[1][1])
  118. ) {
  119. count = eat_num(argv[1]);
  120. argv++;
  121. argc--;
  122. }
  123. #endif
  124. /* -s NUM, -F imlies -f */
  125. opt = getopt32(argv, IF_FEATURE_FANCY_TAIL("^")
  126. "fc:n:"IF_FEATURE_FANCY_TAIL("qs:+vF")
  127. IF_FEATURE_FANCY_TAIL("\0" "Ff"),
  128. &str_c, &str_n IF_FEATURE_FANCY_TAIL(,&sleep_period)
  129. );
  130. #define FOLLOW (opt & 0x1)
  131. #define COUNT_BYTES (opt & 0x2)
  132. //if (opt & 0x1) // -f
  133. if (opt & 0x2) count = eat_num(str_c); // -c
  134. if (opt & 0x4) count = eat_num(str_n); // -n
  135. #if ENABLE_FEATURE_FANCY_TAIL
  136. /* q: make it impossible for nfiles to be > header_threshhold */
  137. if (opt & 0x8) header_threshhold = UINT_MAX; // -q
  138. //if (opt & 0x10) // -s
  139. if (opt & 0x20) header_threshhold = 0; // -v
  140. # define FOLLOW_RETRY (opt & 0x40)
  141. #else
  142. # define FOLLOW_RETRY 0
  143. #endif
  144. argc -= optind;
  145. argv += optind;
  146. /* open all the files */
  147. fds = xmalloc(sizeof(fds[0]) * (argc + 1));
  148. if (!argv[0]) {
  149. struct stat statbuf;
  150. if (fstat(STDIN_FILENO, &statbuf) == 0
  151. && S_ISFIFO(statbuf.st_mode)
  152. ) {
  153. opt &= ~1; /* clear FOLLOW */
  154. }
  155. argv[0] = (char *) bb_msg_standard_input;
  156. }
  157. nfiles = i = 0;
  158. do {
  159. int fd = open_or_warn_stdin(argv[i]);
  160. if (fd < 0 && !FOLLOW_RETRY) {
  161. G.exitcode = EXIT_FAILURE;
  162. continue;
  163. }
  164. fds[nfiles] = fd;
  165. argv[nfiles++] = argv[i];
  166. } while (++i < argc);
  167. if (!nfiles)
  168. bb_error_msg_and_die("no files");
  169. /* prepare the buffer */
  170. tailbufsize = BUFSIZ;
  171. if (!G.from_top && COUNT_BYTES) {
  172. if (tailbufsize < count + BUFSIZ) {
  173. tailbufsize = count + BUFSIZ;
  174. }
  175. }
  176. /* tail -c1024m REGULAR_FILE doesn't really need 1G mem block.
  177. * (In fact, it doesn't need ANY memory). So delay allocation.
  178. */
  179. tailbuf = NULL;
  180. /* tail the files */
  181. fmt = header_fmt_str + 1; /* skip leading newline in the header on the first output */
  182. i = 0;
  183. do {
  184. char *buf;
  185. int taillen;
  186. int newlines_seen;
  187. unsigned seen;
  188. int nread;
  189. int fd = fds[i];
  190. if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
  191. continue; /* may happen with -F */
  192. if (nfiles > header_threshhold) {
  193. tail_xprint_header(fmt, argv[i]);
  194. fmt = header_fmt_str;
  195. }
  196. if (!G.from_top) {
  197. off_t current = lseek(fd, 0, SEEK_END);
  198. if (current > 0) {
  199. unsigned off;
  200. if (COUNT_BYTES) {
  201. /* Optimizing count-bytes case if the file is seekable.
  202. * Beware of backing up too far.
  203. * Also we exclude files with size 0 (because of /proc/xxx) */
  204. if (count == 0)
  205. continue; /* showing zero bytes is easy :) */
  206. current -= count;
  207. if (current < 0)
  208. current = 0;
  209. xlseek(fd, current, SEEK_SET);
  210. bb_copyfd_size(fd, STDOUT_FILENO, count);
  211. continue;
  212. }
  213. #if 1 /* This is technically incorrect for *LONG* strings, but very useful */
  214. /* Optimizing count-lines case if the file is seekable.
  215. * We assume the lines are <64k.
  216. * (Users complain that tail takes too long
  217. * on multi-gigabyte files) */
  218. off = (count | 0xf); /* for small counts, be more paranoid */
  219. if (off > (INT_MAX / (64*1024)))
  220. off = (INT_MAX / (64*1024));
  221. current -= off * (64*1024);
  222. if (current < 0)
  223. current = 0;
  224. xlseek(fd, current, SEEK_SET);
  225. #endif
  226. }
  227. }
  228. if (!tailbuf)
  229. tailbuf = xmalloc(tailbufsize);
  230. buf = tailbuf;
  231. taillen = 0;
  232. /* "We saw 1st line/byte".
  233. * Used only by +N code ("start from Nth", 1-based): */
  234. seen = 1;
  235. newlines_seen = 0;
  236. while ((nread = tail_read(fd, buf, tailbufsize - taillen)) > 0) {
  237. if (G.from_top) {
  238. int nwrite = nread;
  239. if (seen < count) {
  240. /* We need to skip a few more bytes/lines */
  241. if (COUNT_BYTES) {
  242. nwrite -= (count - seen);
  243. seen += nread;
  244. } else {
  245. char *s = buf;
  246. do {
  247. --nwrite;
  248. if (*s++ == '\n' && ++seen == count) {
  249. break;
  250. }
  251. } while (nwrite);
  252. }
  253. }
  254. if (nwrite > 0)
  255. xwrite(STDOUT_FILENO, buf + nread - nwrite, nwrite);
  256. } else if (count) {
  257. if (COUNT_BYTES) {
  258. taillen += nread;
  259. if (taillen > (int)count) {
  260. memmove(tailbuf, tailbuf + taillen - count, count);
  261. taillen = count;
  262. }
  263. } else {
  264. int k = nread;
  265. int newlines_in_buf = 0;
  266. do { /* count '\n' in last read */
  267. k--;
  268. if (buf[k] == '\n') {
  269. newlines_in_buf++;
  270. }
  271. } while (k);
  272. if (newlines_seen + newlines_in_buf < (int)count) {
  273. newlines_seen += newlines_in_buf;
  274. taillen += nread;
  275. } else {
  276. int extra = (buf[nread-1] != '\n');
  277. char *s;
  278. k = newlines_seen + newlines_in_buf + extra - count;
  279. s = tailbuf;
  280. while (k) {
  281. if (*s == '\n') {
  282. k--;
  283. }
  284. s++;
  285. }
  286. taillen += nread - (s - tailbuf);
  287. memmove(tailbuf, s, taillen);
  288. newlines_seen = count - extra;
  289. }
  290. if (tailbufsize < (size_t)taillen + BUFSIZ) {
  291. tailbufsize = taillen + BUFSIZ;
  292. tailbuf = xrealloc(tailbuf, tailbufsize);
  293. }
  294. }
  295. buf = tailbuf + taillen;
  296. }
  297. } /* while (tail_read() > 0) */
  298. if (!G.from_top) {
  299. xwrite(STDOUT_FILENO, tailbuf, taillen);
  300. }
  301. } while (++i < nfiles);
  302. prev_fd = fds[i-1];
  303. tailbuf = xrealloc(tailbuf, BUFSIZ);
  304. fmt = NULL;
  305. if (FOLLOW) while (1) {
  306. sleep(sleep_period);
  307. i = 0;
  308. do {
  309. int nread;
  310. const char *filename = argv[i];
  311. int fd = fds[i];
  312. if (FOLLOW_RETRY) {
  313. struct stat sbuf, fsbuf;
  314. if (fd < 0
  315. || fstat(fd, &fsbuf) < 0
  316. || stat(filename, &sbuf) < 0
  317. || fsbuf.st_dev != sbuf.st_dev
  318. || fsbuf.st_ino != sbuf.st_ino
  319. ) {
  320. int new_fd;
  321. if (fd >= 0)
  322. close(fd);
  323. new_fd = open(filename, O_RDONLY);
  324. if (new_fd >= 0) {
  325. bb_error_msg("%s has %s; following end of new file",
  326. filename, (fd < 0) ? "appeared" : "been replaced"
  327. );
  328. } else if (fd >= 0) {
  329. bb_perror_msg("%s has become inaccessible", filename);
  330. }
  331. fds[i] = fd = new_fd;
  332. }
  333. }
  334. if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
  335. continue;
  336. if (nfiles > header_threshhold) {
  337. fmt = header_fmt_str;
  338. }
  339. for (;;) {
  340. /* tail -f keeps following files even if they are truncated */
  341. struct stat sbuf;
  342. /* /proc files report zero st_size, don't lseek them */
  343. if (fstat(fd, &sbuf) == 0 && sbuf.st_size > 0) {
  344. off_t current = lseek(fd, 0, SEEK_CUR);
  345. if (sbuf.st_size < current)
  346. xlseek(fd, 0, SEEK_SET);
  347. }
  348. nread = tail_read(fd, tailbuf, BUFSIZ);
  349. if (nread <= 0)
  350. break;
  351. if (fmt && (fd != prev_fd)) {
  352. tail_xprint_header(fmt, filename);
  353. fmt = NULL;
  354. prev_fd = fd;
  355. }
  356. xwrite(STDOUT_FILENO, tailbuf, nread);
  357. }
  358. } while (++i < nfiles);
  359. } /* while (1) */
  360. if (ENABLE_FEATURE_CLEAN_UP) {
  361. free(fds);
  362. free(tailbuf);
  363. }
  364. return G.exitcode;
  365. }