3
0

tail.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 compliant (need fancy for -c) */
  10. /* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
  11. /* http://www.opengroup.org/onlinepubs/007904975/utilities/tail.html */
  12. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  13. *
  14. * Pretty much rewritten to fix numerous bugs and reduce realloc() calls.
  15. * Bugs fixed (although I may have forgotten one or two... it was pretty bad)
  16. * 1) mixing printf/write without fflush()ing stdout
  17. * 2) no check that any open files are present
  18. * 3) optstring had -q taking an arg
  19. * 4) no error checking on write in some cases, and a warning even then
  20. * 5) q and s interaction bug
  21. * 6) no check for lseek error
  22. * 7) lseek attempted when count==0 even if arg was +0 (from top)
  23. */
  24. #include "libbb.h"
  25. static const struct suffix_mult tail_suffixes[] = {
  26. { "b", 512 },
  27. { "k", 1024 },
  28. { "m", 1024*1024 },
  29. { "", 0 }
  30. };
  31. struct globals {
  32. bool status;
  33. } FIX_ALIASING;
  34. #define G (*(struct globals*)&bb_common_bufsiz1)
  35. static void tail_xprint_header(const char *fmt, const char *filename)
  36. {
  37. if (fdprintf(STDOUT_FILENO, fmt, filename) < 0)
  38. bb_perror_nomsg_and_die();
  39. }
  40. static ssize_t tail_read(int fd, char *buf, size_t count)
  41. {
  42. ssize_t r;
  43. off_t current;
  44. struct stat sbuf;
  45. /* /proc files report zero st_size, don't lseek them. */
  46. if (fstat(fd, &sbuf) == 0 && sbuf.st_size > 0) {
  47. current = lseek(fd, 0, SEEK_CUR);
  48. if (sbuf.st_size < current)
  49. xlseek(fd, 0, SEEK_SET);
  50. }
  51. r = full_read(fd, buf, count);
  52. if (r < 0) {
  53. bb_perror_msg(bb_msg_read_error);
  54. G.status = EXIT_FAILURE;
  55. }
  56. return r;
  57. }
  58. #define header_fmt_str "\n==> %s <==\n"
  59. static unsigned eat_num(const char *p)
  60. {
  61. if (*p == '-')
  62. p++;
  63. else if (*p == '+') {
  64. p++;
  65. G.status = 1; /* mark that we saw "+" */
  66. }
  67. return xatou_sfx(p, tail_suffixes);
  68. }
  69. int tail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  70. int tail_main(int argc, char **argv)
  71. {
  72. unsigned count = 10;
  73. unsigned sleep_period = 1;
  74. bool from_top;
  75. const char *str_c, *str_n;
  76. char *tailbuf;
  77. size_t tailbufsize;
  78. unsigned header_threshhold = 1;
  79. unsigned nfiles;
  80. int i, opt;
  81. int *fds;
  82. const char *fmt;
  83. #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
  84. /* Allow legacy syntax of an initial numeric option without -n. */
  85. if (argv[1] && (argv[1][0] == '+' || argv[1][0] == '-')
  86. && isdigit(argv[1][1])
  87. ) {
  88. count = eat_num(argv[1]);
  89. argv++;
  90. argc--;
  91. }
  92. #endif
  93. /* -s NUM, -F imlies -f */
  94. IF_FEATURE_FANCY_TAIL(opt_complementary = "s+:Ff";)
  95. opt = getopt32(argv, "fc:n:" IF_FEATURE_FANCY_TAIL("qs:vF"),
  96. &str_c, &str_n IF_FEATURE_FANCY_TAIL(,&sleep_period));
  97. #define FOLLOW (opt & 0x1)
  98. #define COUNT_BYTES (opt & 0x2)
  99. //if (opt & 0x1) // -f
  100. if (opt & 0x2) count = eat_num(str_c); // -c
  101. if (opt & 0x4) count = eat_num(str_n); // -n
  102. #if ENABLE_FEATURE_FANCY_TAIL
  103. /* q: make it impossible for nfiles to be > header_threshhold */
  104. if (opt & 0x8) header_threshhold = UINT_MAX; // -q
  105. //if (opt & 0x10) // -s
  106. if (opt & 0x20) header_threshhold = 0; // -v
  107. # define FOLLOW_RETRY (opt & 0x40)
  108. #else
  109. # define FOLLOW_RETRY 0
  110. #endif
  111. argc -= optind;
  112. argv += optind;
  113. from_top = G.status; /* 1 if there was "-c +N" or "-n +N" */
  114. G.status = EXIT_SUCCESS;
  115. /* open all the files */
  116. fds = xmalloc(sizeof(fds[0]) * (argc + 1));
  117. if (!argv[0]) {
  118. struct stat statbuf;
  119. if (fstat(STDIN_FILENO, &statbuf) == 0
  120. && S_ISFIFO(statbuf.st_mode)
  121. ) {
  122. opt &= ~1; /* clear FOLLOW */
  123. }
  124. argv[0] = (char *) bb_msg_standard_input;
  125. }
  126. nfiles = i = 0;
  127. do {
  128. int fd = open_or_warn_stdin(argv[i]);
  129. if (fd < 0 && !FOLLOW_RETRY) {
  130. G.status = EXIT_FAILURE;
  131. continue;
  132. }
  133. fds[nfiles] = fd;
  134. argv[nfiles++] = argv[i];
  135. } while (++i < argc);
  136. if (!nfiles)
  137. bb_error_msg_and_die("no files");
  138. /* prepare the buffer */
  139. tailbufsize = BUFSIZ;
  140. if (!from_top && COUNT_BYTES) {
  141. if (tailbufsize < count + BUFSIZ) {
  142. tailbufsize = count + BUFSIZ;
  143. }
  144. }
  145. tailbuf = xmalloc(tailbufsize);
  146. /* tail the files */
  147. fmt = header_fmt_str + 1; /* skip header leading newline on first output */
  148. i = 0;
  149. do {
  150. char *buf;
  151. int taillen;
  152. int newlines_seen;
  153. unsigned seen;
  154. int nread;
  155. int fd = fds[i];
  156. if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
  157. continue; /* may happen with -E */
  158. if (nfiles > header_threshhold) {
  159. tail_xprint_header(fmt, argv[i]);
  160. fmt = header_fmt_str;
  161. }
  162. if (!from_top) {
  163. off_t current = lseek(fd, 0, SEEK_END);
  164. if (current > 0) {
  165. unsigned off;
  166. if (COUNT_BYTES) {
  167. /* Optimizing count-bytes case if the file is seekable.
  168. * Beware of backing up too far.
  169. * Also we exclude files with size 0 (because of /proc/xxx) */
  170. if (count == 0)
  171. continue; /* showing zero bytes is easy :) */
  172. current -= count;
  173. if (current < 0)
  174. current = 0;
  175. xlseek(fd, current, SEEK_SET);
  176. bb_copyfd_size(fd, STDOUT_FILENO, count);
  177. continue;
  178. }
  179. #if 1 /* This is technically incorrect for *LONG* strings, but very useful */
  180. /* Optimizing count-lines case if the file is seekable.
  181. * We assume the lines are <64k.
  182. * (Users complain that tail takes too long
  183. * on multi-gigabyte files) */
  184. off = (count | 0xf); /* for small counts, be more paranoid */
  185. if (off > (INT_MAX / (64*1024)))
  186. off = (INT_MAX / (64*1024));
  187. current -= off * (64*1024);
  188. if (current < 0)
  189. current = 0;
  190. xlseek(fd, current, SEEK_SET);
  191. #endif
  192. }
  193. }
  194. buf = tailbuf;
  195. taillen = 0;
  196. /* "We saw 1st line/byte".
  197. * Used only by +N code ("start from Nth", 1-based): */
  198. seen = 1;
  199. newlines_seen = 0;
  200. while ((nread = tail_read(fd, buf, tailbufsize-taillen)) > 0) {
  201. if (from_top) {
  202. int nwrite = nread;
  203. if (seen < count) {
  204. /* We need to skip a few more bytes/lines */
  205. if (COUNT_BYTES) {
  206. nwrite -= (count - seen);
  207. seen = count;
  208. } else {
  209. char *s = buf;
  210. do {
  211. --nwrite;
  212. if (*s++ == '\n' && ++seen == count) {
  213. break;
  214. }
  215. } while (nwrite);
  216. }
  217. }
  218. if (nwrite > 0)
  219. xwrite(STDOUT_FILENO, buf + nread - nwrite, nwrite);
  220. } else if (count) {
  221. if (COUNT_BYTES) {
  222. taillen += nread;
  223. if (taillen > (int)count) {
  224. memmove(tailbuf, tailbuf + taillen - count, count);
  225. taillen = count;
  226. }
  227. } else {
  228. int k = nread;
  229. int newlines_in_buf = 0;
  230. do { /* count '\n' in last read */
  231. k--;
  232. if (buf[k] == '\n') {
  233. newlines_in_buf++;
  234. }
  235. } while (k);
  236. if (newlines_seen + newlines_in_buf < (int)count) {
  237. newlines_seen += newlines_in_buf;
  238. taillen += nread;
  239. } else {
  240. int extra = (buf[nread-1] != '\n');
  241. char *s;
  242. k = newlines_seen + newlines_in_buf + extra - count;
  243. s = tailbuf;
  244. while (k) {
  245. if (*s == '\n') {
  246. k--;
  247. }
  248. s++;
  249. }
  250. taillen += nread - (s - tailbuf);
  251. memmove(tailbuf, s, taillen);
  252. newlines_seen = count - extra;
  253. }
  254. if (tailbufsize < (size_t)taillen + BUFSIZ) {
  255. tailbufsize = taillen + BUFSIZ;
  256. tailbuf = xrealloc(tailbuf, tailbufsize);
  257. }
  258. }
  259. buf = tailbuf + taillen;
  260. }
  261. } /* while (tail_read() > 0) */
  262. if (!from_top) {
  263. xwrite(STDOUT_FILENO, tailbuf, taillen);
  264. }
  265. } while (++i < nfiles);
  266. tailbuf = xrealloc(tailbuf, BUFSIZ);
  267. fmt = NULL;
  268. if (FOLLOW) while (1) {
  269. sleep(sleep_period);
  270. i = 0;
  271. do {
  272. int nread;
  273. const char *filename = argv[i];
  274. int fd = fds[i];
  275. if (FOLLOW_RETRY) {
  276. struct stat sbuf, fsbuf;
  277. if (fd < 0
  278. || fstat(fd, &fsbuf) < 0
  279. || stat(filename, &sbuf) < 0
  280. || fsbuf.st_dev != sbuf.st_dev
  281. || fsbuf.st_ino != sbuf.st_ino
  282. ) {
  283. int new_fd;
  284. if (fd >= 0)
  285. close(fd);
  286. new_fd = open(filename, O_RDONLY);
  287. if (new_fd >= 0) {
  288. bb_error_msg("%s has %s; following end of new file",
  289. filename, (fd < 0) ? "appeared" : "been replaced"
  290. );
  291. } else if (fd >= 0) {
  292. bb_perror_msg("%s has become inaccessible", filename);
  293. }
  294. fds[i] = fd = new_fd;
  295. }
  296. }
  297. if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
  298. continue;
  299. if (nfiles > header_threshhold) {
  300. fmt = header_fmt_str;
  301. }
  302. while ((nread = tail_read(fd, tailbuf, BUFSIZ)) > 0) {
  303. if (fmt) {
  304. tail_xprint_header(fmt, filename);
  305. fmt = NULL;
  306. }
  307. xwrite(STDOUT_FILENO, tailbuf, nread);
  308. }
  309. } while (++i < nfiles);
  310. }
  311. if (ENABLE_FEATURE_CLEAN_UP) {
  312. free(fds);
  313. }
  314. return G.status;
  315. }