busybox/coreutils/tail.c
<<
>>
Prefs
   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 (6.8 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
  40//applet:IF_TAIL(APPLET(tail, BB_DIR_USR_BIN, BB_SUID_DROP))
  41
  42//kbuild:lib-$(CONFIG_TAIL) += tail.o
  43
  44/* BB_AUDIT SUSv3 compliant (need fancy for -c) */
  45/* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
  46/* http://www.opengroup.org/onlinepubs/007904975/utilities/tail.html */
  47
  48//usage:#define tail_trivial_usage
  49//usage:       "[OPTIONS] [FILE]..."
  50//usage:#define tail_full_usage "\n\n"
  51//usage:       "Print last 10 lines of FILEs (or stdin) to.\n"
  52//usage:       "With more than one FILE, precede each with a filename header.\n"
  53//usage:     "\n        -c [+]N[bkm]    Print last N bytes"
  54//usage:     "\n        -n N[bkm]       Print last N lines"
  55//usage:     "\n        -n +N[bkm]      Start on Nth line and print the rest"
  56//usage:     "\n                        (b:*512 k:*1024 m:*1024^2)"
  57//usage:        IF_FEATURE_FANCY_TAIL(
  58//usage:     "\n        -q              Never print headers"
  59//usage:     "\n        -v              Always print headers"
  60//usage:        )
  61//usage:     "\n        -f              Print data as file grows"
  62//usage:        IF_FEATURE_FANCY_TAIL(
  63//usage:     "\n        -F              Same as -f, but keep retrying"
  64//usage:     "\n        -s SECONDS      Wait SECONDS between reads with -f"
  65//usage:        )
  66//usage:
  67//usage:#define tail_example_usage
  68//usage:       "$ tail -n 1 /etc/resolv.conf\n"
  69//usage:       "nameserver 10.0.0.1\n"
  70
  71#include "libbb.h"
  72#include "common_bufsiz.h"
  73
  74struct globals {
  75        bool from_top;
  76        bool exitcode;
  77} FIX_ALIASING;
  78#define G (*(struct globals*)bb_common_bufsiz1)
  79#define INIT_G() do { setup_common_bufsiz(); } while (0)
  80
  81static void tail_xprint_header(const char *fmt, const char *filename)
  82{
  83        if (fdprintf(STDOUT_FILENO, fmt, filename) < 0)
  84                bb_perror_nomsg_and_die();
  85}
  86
  87static ssize_t tail_read(int fd, char *buf, size_t count)
  88{
  89        ssize_t r;
  90
  91        r = full_read(fd, buf, count);
  92        if (r < 0) {
  93                bb_simple_perror_msg(bb_msg_read_error);
  94                G.exitcode = EXIT_FAILURE;
  95        }
  96
  97        return r;
  98}
  99
 100#define header_fmt_str "\n==> %s <==\n"
 101
 102static unsigned eat_num(const char *p)
 103{
 104        if (*p == '-')
 105                p++;
 106        else if (*p == '+') {
 107                p++;
 108                G.from_top = 1;
 109        }
 110        return xatou_sfx(p, bkm_suffixes);
 111}
 112
 113int tail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 114int tail_main(int argc, char **argv)
 115{
 116        unsigned count = 10;
 117        unsigned sleep_period = 1;
 118        const char *str_c, *str_n;
 119
 120        char *tailbuf;
 121        size_t tailbufsize;
 122        unsigned header_threshold = 1;
 123        unsigned nfiles;
 124        int i, opt;
 125
 126        int *fds;
 127        const char *fmt;
 128        int prev_fd;
 129
 130        INIT_G();
 131
 132#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
 133        /* Allow legacy syntax of an initial numeric option without -n. */
 134        if (argv[1] && (argv[1][0] == '+' || argv[1][0] == '-')
 135         && isdigit(argv[1][1])
 136        ) {
 137                count = eat_num(argv[1]);
 138                argv++;
 139                argc--;
 140        }
 141#endif
 142
 143        /* -s NUM, -F imlies -f */
 144        opt = getopt32(argv, IF_FEATURE_FANCY_TAIL("^")
 145                        "fc:n:"IF_FEATURE_FANCY_TAIL("qs:+vF")
 146                        IF_FEATURE_FANCY_TAIL("\0" "Ff"),
 147                        &str_c, &str_n IF_FEATURE_FANCY_TAIL(,&sleep_period)
 148        );
 149#define FOLLOW (opt & 0x1)
 150#define COUNT_BYTES (opt & 0x2)
 151        //if (opt & 0x1) // -f
 152        if (opt & 0x2) count = eat_num(str_c); // -c
 153        if (opt & 0x4) count = eat_num(str_n); // -n
 154#if ENABLE_FEATURE_FANCY_TAIL
 155        /* q: make it impossible for nfiles to be > header_threshold */
 156        if (opt & 0x8) header_threshold = UINT_MAX; // -q
 157        //if (opt & 0x10) // -s
 158        if (opt & 0x20) header_threshold = 0; // -v
 159# define FOLLOW_RETRY (opt & 0x40)
 160#else
 161# define FOLLOW_RETRY 0
 162#endif
 163        argc -= optind;
 164        argv += optind;
 165
 166        /* open all the files */
 167        fds = xmalloc(sizeof(fds[0]) * (argc + 1));
 168        if (!argv[0]) {
 169                struct stat statbuf;
 170
 171                if (fstat(STDIN_FILENO, &statbuf) == 0
 172                 && S_ISFIFO(statbuf.st_mode)
 173                ) {
 174                        opt &= ~1; /* clear FOLLOW */
 175                }
 176                argv[0] = (char *) bb_msg_standard_input;
 177        }
 178        nfiles = i = 0;
 179        do {
 180                int fd = open_or_warn_stdin(argv[i]);
 181                if (fd < 0 && !FOLLOW_RETRY) {
 182                        G.exitcode = EXIT_FAILURE;
 183                        continue;
 184                }
 185                fds[nfiles] = fd;
 186                argv[nfiles++] = argv[i];
 187        } while (++i < argc);
 188
 189        if (!nfiles)
 190                bb_simple_error_msg_and_die("no files");
 191
 192        /* prepare the buffer */
 193        tailbufsize = BUFSIZ;
 194        if (!G.from_top && COUNT_BYTES) {
 195                if (tailbufsize < count + BUFSIZ) {
 196                        tailbufsize = count + BUFSIZ;
 197                }
 198        }
 199        /* tail -c1024m REGULAR_FILE doesn't really need 1G mem block.
 200         * (In fact, it doesn't need ANY memory). So delay allocation.
 201         */
 202        tailbuf = NULL;
 203
 204        /* tail the files */
 205
 206        fmt = header_fmt_str + 1; /* skip leading newline in the header on the first output */
 207        i = 0;
 208        do {
 209                char *buf;
 210                int taillen;
 211                int newlines_seen;
 212                unsigned seen;
 213                int nread;
 214                int fd = fds[i];
 215
 216                if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
 217                        continue; /* may happen with -F */
 218
 219                if (nfiles > header_threshold) {
 220                        tail_xprint_header(fmt, argv[i]);
 221                        fmt = header_fmt_str;
 222                }
 223
 224                if (!G.from_top) {
 225                        off_t current = lseek(fd, 0, SEEK_END);
 226                        if (current > 0) {
 227                                unsigned off;
 228                                if (COUNT_BYTES) {
 229                                /* Optimizing count-bytes case if the file is seekable.
 230                                 * Beware of backing up too far.
 231                                 * Also we exclude files with size 0 (because of /proc/xxx) */
 232                                        if (count == 0)
 233                                                continue; /* showing zero bytes is easy :) */
 234                                        current -= count;
 235                                        if (current < 0)
 236                                                current = 0;
 237                                        xlseek(fd, current, SEEK_SET);
 238                                        bb_copyfd_size(fd, STDOUT_FILENO, count);
 239                                        continue;
 240                                }
 241#if 1 /* This is technically incorrect for *LONG* strings, but very useful */
 242                                /* Optimizing count-lines case if the file is seekable.
 243                                 * We assume the lines are <64k.
 244                                 * (Users complain that tail takes too long
 245                                 * on multi-gigabyte files) */
 246                                off = (count | 0xf); /* for small counts, be more paranoid */
 247                                if (off > (INT_MAX / (64*1024)))
 248                                        off = (INT_MAX / (64*1024));
 249                                current -= off * (64*1024);
 250                                if (current < 0)
 251                                        current = 0;
 252                                xlseek(fd, current, SEEK_SET);
 253#endif
 254                        }
 255                }
 256
 257                if (!tailbuf)
 258                        tailbuf = xmalloc(tailbufsize);
 259
 260                buf = tailbuf;
 261                taillen = 0;
 262                /* "We saw 1st line/byte".
 263                 * Used only by +N code ("start from Nth", 1-based): */
 264                seen = 1;
 265                newlines_seen = 0;
 266                while ((nread = tail_read(fd, buf, tailbufsize - taillen)) > 0) {
 267                        if (G.from_top) {
 268                                int nwrite = nread;
 269                                if (seen < count) {
 270                                        /* We need to skip a few more bytes/lines */
 271                                        if (COUNT_BYTES) {
 272                                                nwrite -= (count - seen);
 273                                                seen += nread;
 274                                        } else {
 275                                                char *s = buf;
 276                                                do {
 277                                                        --nwrite;
 278                                                        if (*s++ == '\n' && ++seen == count) {
 279                                                                break;
 280                                                        }
 281                                                } while (nwrite);
 282                                        }
 283                                }
 284                                if (nwrite > 0)
 285                                        xwrite(STDOUT_FILENO, buf + nread - nwrite, nwrite);
 286                        } else if (count) {
 287                                if (COUNT_BYTES) {
 288                                        taillen += nread;
 289                                        if (taillen > (int)count) {
 290                                                memmove(tailbuf, tailbuf + taillen - count, count);
 291                                                taillen = count;
 292                                        }
 293                                } else {
 294                                        int k = nread;
 295                                        int newlines_in_buf = 0;
 296
 297                                        do { /* count '\n' in last read */
 298                                                k--;
 299                                                if (buf[k] == '\n') {
 300                                                        newlines_in_buf++;
 301                                                }
 302                                        } while (k);
 303
 304                                        if (newlines_seen + newlines_in_buf < (int)count) {
 305                                                newlines_seen += newlines_in_buf;
 306                                                taillen += nread;
 307                                        } else {
 308                                                int extra = (buf[nread-1] != '\n');
 309                                                char *s;
 310
 311                                                k = newlines_seen + newlines_in_buf + extra - count;
 312                                                s = tailbuf;
 313                                                while (k) {
 314                                                        if (*s == '\n') {
 315                                                                k--;
 316                                                        }
 317                                                        s++;
 318                                                }
 319                                                taillen += nread - (s - tailbuf);
 320                                                memmove(tailbuf, s, taillen);
 321                                                newlines_seen = count - extra;
 322                                        }
 323                                        if (tailbufsize < (size_t)taillen + BUFSIZ) {
 324                                                tailbufsize = taillen + BUFSIZ;
 325                                                tailbuf = xrealloc(tailbuf, tailbufsize);
 326                                        }
 327                                }
 328                                buf = tailbuf + taillen;
 329                        }
 330                } /* while (tail_read() > 0) */
 331                if (!G.from_top) {
 332                        xwrite(STDOUT_FILENO, tailbuf, taillen);
 333                }
 334        } while (++i < nfiles);
 335        prev_fd = fds[i-1];
 336
 337        tailbuf = xrealloc(tailbuf, BUFSIZ);
 338
 339        fmt = NULL;
 340
 341        if (FOLLOW) while (1) {
 342                sleep(sleep_period);
 343
 344                i = 0;
 345                do {
 346                        int nread;
 347                        const char *filename = argv[i];
 348                        int fd = fds[i];
 349                        int new_fd = -1;
 350                        struct stat sbuf;
 351
 352                        if (FOLLOW_RETRY) {
 353                                struct stat fsbuf;
 354
 355                                if (fd < 0
 356                                 || fstat(fd, &fsbuf) < 0
 357                                 || stat(filename, &sbuf) < 0
 358                                 || fsbuf.st_dev != sbuf.st_dev
 359                                 || fsbuf.st_ino != sbuf.st_ino
 360                                ) {
 361                                        /* Looks like file has been created/renamed/deleted */
 362                                        new_fd = open(filename, O_RDONLY);
 363                                        if (new_fd >= 0) {
 364                                                bb_error_msg("%s has %s; following end of new file",
 365                                                        filename, (fd < 0) ? "appeared" : "been replaced"
 366                                                );
 367                                                if (fd < 0) {
 368                                                        /* No previously open fd for this file,
 369                                                         * start using new_fd immediately. */
 370                                                        fds[i] = fd = new_fd;
 371                                                        new_fd = -1;
 372                                                }
 373                                        } else if (fd >= 0) {
 374                                                bb_perror_msg("%s has been renamed or deleted", filename);
 375                                        }
 376                                }
 377                        }
 378                        if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
 379                                continue;
 380                        if (nfiles > header_threshold) {
 381                                fmt = header_fmt_str;
 382                        }
 383                        for (;;) {
 384                                /* tail -f keeps following files even if they are truncated */
 385                                /* /proc files report zero st_size, don't lseek them */
 386                                if (fstat(fd, &sbuf) == 0
 387                                 /* && S_ISREG(sbuf.st_mode) TODO? */
 388                                 && sbuf.st_size > 0
 389                                ) {
 390                                        off_t current = lseek(fd, 0, SEEK_CUR);
 391                                        if (sbuf.st_size < current) {
 392                                                //bb_perror_msg("%s: file truncated", filename); - says coreutils 8.32
 393                                                xlseek(fd, 0, SEEK_SET);
 394                                        }
 395                                }
 396
 397                                nread = tail_read(fd, tailbuf, BUFSIZ);
 398                                if (nread <= 0) {
 399                                        if (new_fd < 0)
 400                                                break;
 401                                        /* Switch to "tail -F"ing the new file */
 402                                        xmove_fd(new_fd, fd);
 403                                        new_fd = -1;
 404                                        continue;
 405                                }
 406                                if (fmt && (fd != prev_fd)) {
 407                                        tail_xprint_header(fmt, filename);
 408                                        fmt = NULL;
 409                                        prev_fd = fd;
 410                                }
 411                                xwrite(STDOUT_FILENO, tailbuf, nread);
 412                        }
 413                } while (++i < nfiles);
 414        } /* while (1) */
 415
 416        if (ENABLE_FEATURE_CLEAN_UP) {
 417                free(fds);
 418                free(tailbuf);
 419        }
 420        return G.exitcode;
 421}
 422