1 /* tail -- output the last part of file(s)
2 Copyright (C) 1989, 90, 91, 1995-2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Can display any amount of data, unlike the Unix version, which uses
19 a fixed size buffer and therefore can only deliver a limited number
22 Original version by Paul Rubin <phr@ocf.berkeley.edu>.
23 Extensions by David MacKenzie <djm@gnu.ai.mit.edu>.
24 tail -f for multiple files by Ian Lance Taylor <ian@airs.com>. */
31 #include <sys/types.h>
42 #include "safe-read.h"
43 #include "xnanosleep.h"
47 /* The official name of this program (e.g., no `g' prefix). */
48 #define PROGRAM_NAME "tail"
51 "Paul Rubin", "David MacKenzie, Ian Lance Taylor", "Jim Meyering"
54 /* Some systems don't have ENOSYS -- this should be a big enough
55 value that no valid errno value will match it. */
59 /* Number of items to tail. */
60 #define DEFAULT_N_LINES 10
62 /* Special values for dump_remainder's N_BYTES parameter. */
63 #define COPY_TO_EOF UINTMAX_MAX
64 #define COPY_A_BUFFER (UINTMAX_MAX - 1)
66 /* FIXME: make Follow_name the default? */
67 #define DEFAULT_FOLLOW_MODE Follow_descriptor
71 /* Follow the name of each file: if the file is renamed, try to reopen
72 that name and track the end of the new file if/when it's recreated.
73 This is useful for tracking logs that are occasionally rotated. */
76 /* Follow each descriptor obtained upon opening a file.
77 That means we'll continue to follow the end of a file even after
78 it has been renamed or unlinked. */
82 /* On Darwin 7.7, when reading from a command-line pipe, standard
83 input is of type S_ISSOCK. Everywhere else it's S_ISFIFO. */
84 #define IS_PIPE_LIKE_FILE_TYPE(Mode) \
85 (S_ISFIFO (Mode) || S_ISSOCK (Mode))
87 /* The types of files for which tail works. */
88 #define IS_TAILABLE_FILE_TYPE(Mode) \
89 (S_ISREG (Mode) || IS_PIPE_LIKE_FILE_TYPE (Mode) || S_ISCHR (Mode))
91 static char const *const follow_mode_string[] =
93 "descriptor", "name", NULL
96 static enum Follow_mode const follow_mode_map[] =
98 Follow_descriptor, Follow_name,
103 /* The actual file name, or "-" for stdin. */
106 /* File descriptor on which the file is open; -1 if it's not open. */
109 /* Attributes of the file the last time we checked. */
111 struct timespec mtime;
116 /* 1 if O_NONBLOCK is clear, 0 if set, -1 if not known. */
119 /* The specified name initially referred to a directory or some other
120 type for which tail isn't meaningful. Unlike for a permission problem
121 (tailable, below) once this is set, the name is not checked ever again. */
124 /* See description of DEFAULT_MAX_N_... below. */
125 uintmax_t n_unchanged_stats;
127 /* A file is tailable if it exists, is readable, and is of type
128 IS_TAILABLE_FILE_TYPE. */
131 /* The value of errno seen last time we checked this file. */
136 /* Keep trying to open a file even if it is inaccessible when tail starts
137 or if it becomes inaccessible later -- useful only with -f. */
138 static bool reopen_inaccessible_files;
140 /* If true, interpret the numeric argument as the number of lines.
141 Otherwise, interpret it as the number of bytes. */
142 static bool count_lines;
144 /* Whether we follow the name of each file or the file descriptor
145 that is initially associated with each name. */
146 static enum Follow_mode follow_mode = Follow_descriptor;
148 /* If true, read from the ends of all specified files until killed. */
151 /* If true, count from start of file instead of end. */
152 static bool from_start;
154 /* If true, print filename headers. */
155 static bool print_headers;
157 /* When to print the filename banners. */
160 multiple_files, always, never
163 /* When tailing a file by name, if there have been this many consecutive
164 iterations for which the file has not changed, then open/fstat
165 the file to determine if that file name is still associated with the
166 same device/inode-number pair as before. This option is meaningful only
167 when following by name. --max-unchanged-stats=N */
168 #define DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS 5
169 static uintmax_t max_n_unchanged_stats_between_opens =
170 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS;
172 /* The name this program was run with. */
175 /* The process ID of the process (presumably on the current host)
176 that is writing to all followed files. */
179 /* True if we have ever read standard input. */
180 static bool have_read_stdin;
182 /* If nonzero, skip the is-regular-file test used to determine whether
183 to use the lseek optimization. Instead, use the more general (and
184 more expensive) code unconditionally. Intended solely for testing. */
185 static bool presume_input_pipe;
187 /* For long options that have no equivalent short option, use a
188 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
191 RETRY_OPTION = CHAR_MAX + 1,
192 ALLOW_MISSING_OPTION, /* deprecated, FIXME: remove in late 2004 */
193 MAX_UNCHANGED_STATS_OPTION,
195 PRESUME_INPUT_PIPE_OPTION,
199 static struct option const long_options[] =
201 /* FIXME: remove in 2005 --allow-missing is deprecated; use --retry instead */
202 {"allow-missing", no_argument, NULL, ALLOW_MISSING_OPTION},
203 {"bytes", required_argument, NULL, 'c'},
204 {"follow", optional_argument, NULL, LONG_FOLLOW_OPTION},
205 {"lines", required_argument, NULL, 'n'},
206 {"max-unchanged-stats", required_argument, NULL, MAX_UNCHANGED_STATS_OPTION},
207 {"pid", required_argument, NULL, PID_OPTION},
208 {"presume-input-pipe", no_argument, NULL,
209 PRESUME_INPUT_PIPE_OPTION}, /* do not document */
210 {"quiet", no_argument, NULL, 'q'},
211 {"retry", no_argument, NULL, RETRY_OPTION},
212 {"silent", no_argument, NULL, 'q'},
213 {"sleep-interval", required_argument, NULL, 's'},
214 {"verbose", no_argument, NULL, 'v'},
215 {GETOPT_HELP_OPTION_DECL},
216 {GETOPT_VERSION_OPTION_DECL},
223 if (status != EXIT_SUCCESS)
224 fprintf (stderr, _("Try `%s --help' for more information.\n"),
229 Usage: %s [OPTION]... [FILE]...\n\
233 Print the last %d lines of each FILE to standard output.\n\
234 With more than one FILE, precede each with a header giving the file name.\n\
235 With no FILE, or when FILE is -, read standard input.\n\
237 "), DEFAULT_N_LINES);
239 Mandatory arguments to long options are mandatory for short options too.\n\
242 --retry keep trying to open a file even if it is\n\
243 inaccessible when tail starts or if it becomes\n\
244 inaccessible later; useful when following by name,\n\
245 i.e., with --follow=name\n\
246 -c, --bytes=N output the last N bytes\n\
249 -f, --follow[={name|descriptor}]\n\
250 output appended data as the file grows;\n\
251 -f, --follow, and --follow=descriptor are\n\
253 -F same as --follow=name --retry\n\
256 -n, --lines=N output the last N lines, instead of the last %d\n\
257 --max-unchanged-stats=N\n\
258 with --follow=name, reopen a FILE which has not\n\
259 changed size after N (default %d) iterations\n\
260 to see if it has been unlinked or renamed\n\
261 (this is the usual case of rotated log files)\n\
264 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS
267 --pid=PID with -f, terminate after process ID, PID dies\n\
268 -q, --quiet, --silent never output headers giving file names\n\
269 -s, --sleep-interval=S with -f, sleep for approximately S seconds\n\
270 (default 1.0) between iterations.\n\
271 -v, --verbose always output headers giving file names\n\
273 fputs (HELP_OPTION_DESCRIPTION, stdout);
274 fputs (VERSION_OPTION_DESCRIPTION, stdout);
277 If the first character of N (the number of bytes or lines) is a `+',\n\
278 print beginning with the Nth item from the start of each file, otherwise,\n\
279 print the last N items in the file. N may have a multiplier suffix:\n\
280 b 512, k 1024, m 1024*1024.\n\
284 With --follow (-f), tail defaults to following the file descriptor, which\n\
285 means that even if a tail'ed file is renamed, tail will continue to track\n\
289 This default behavior is not desirable when you really want to\n\
290 track the actual name of the file, not the file descriptor (e.g., log\n\
291 rotation). Use --follow=name in that case. That causes tail to track the\n\
292 named file by reopening it periodically to see if it has been removed and\n\
293 recreated by some other program.\n\
295 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
301 valid_file_spec (struct File_spec const *f)
303 /* Exactly one of the following subexpressions must be true. */
304 return ((f->fd == -1) ^ (f->errnum == 0));
308 pretty_name (struct File_spec const *f)
310 return (STREQ (f->name, "-") ? "standard input" : f->name);
314 xwrite_stdout (char const *buffer, size_t n_bytes)
316 if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) == 0)
317 error (EXIT_FAILURE, errno, _("write error"));
320 /* Record a file F with descriptor FD, size SIZE, status ST, and
321 blocking status BLOCKING. */
324 record_open_fd (struct File_spec *f, int fd,
325 off_t size, struct stat const *st,
330 f->mtime.tv_sec = st->st_mtime;
331 f->mtime.tv_nsec = TIMESPEC_NS (st->st_mtim);
334 f->mode = st->st_mode;
335 f->blocking = blocking;
336 f->n_unchanged_stats = 0;
340 /* Close the file with descriptor FD and name FILENAME. */
343 close_fd (int fd, const char *filename)
345 if (fd != -1 && fd != STDIN_FILENO && close (fd))
347 error (0, errno, _("closing %s (fd=%d)"), filename, fd);
352 write_header (const char *pretty_filename)
354 static bool first_file = true;
356 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), pretty_filename);
360 /* Read and output N_BYTES of file PRETTY_FILENAME starting at the current
361 position in FD. If N_BYTES is COPY_TO_EOF, then copy until end of file.
362 If N_BYTES is COPY_A_BUFFER, then copy at most one buffer's worth.
363 Return the number of bytes read from the file. */
366 dump_remainder (const char *pretty_filename, int fd, uintmax_t n_bytes)
369 uintmax_t n_remaining = n_bytes;
375 size_t n = MIN (n_remaining, BUFSIZ);
376 size_t bytes_read = safe_read (fd, buffer, n);
377 if (bytes_read == SAFE_READ_ERROR)
380 error (EXIT_FAILURE, errno, _("error reading %s"),
381 quote (pretty_filename));
386 xwrite_stdout (buffer, bytes_read);
387 n_written += bytes_read;
388 if (n_bytes != COPY_TO_EOF)
390 n_remaining -= bytes_read;
391 if (n_remaining == 0 || n_bytes == COPY_A_BUFFER)
399 /* Call lseek with the specified arguments, where file descriptor FD
400 corresponds to the file, FILENAME.
401 Give a diagnostic and exit nonzero if lseek fails.
402 Otherwise, return the resulting offset. */
405 xlseek (int fd, off_t offset, int whence, char const *filename)
407 off_t new_offset = lseek (fd, offset, whence);
408 char buf[INT_BUFSIZE_BOUND (off_t)];
414 s = offtostr (offset, buf);
418 error (0, errno, _("%s: cannot seek to offset %s"),
422 error (0, errno, _("%s: cannot seek to relative offset %s"),
426 error (0, errno, _("%s: cannot seek to end-relative offset %s"),
436 /* Print the last N_LINES lines from the end of file FD.
437 Go backward through the file, reading `BUFSIZ' bytes at a time (except
438 probably the first), until we hit the start of the file or have
439 read NUMBER newlines.
440 START_POS is the starting position of the read pointer for the file
441 associated with FD (may be nonzero).
442 END_POS is the file offset of EOF (one larger than offset of last byte).
443 Return true if successful. */
446 file_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
447 off_t start_pos, off_t end_pos, uintmax_t *read_pos)
456 /* Set `bytes_read' to the size of the last, probably partial, buffer;
457 0 < `bytes_read' <= `BUFSIZ'. */
458 bytes_read = (pos - start_pos) % BUFSIZ;
461 /* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
462 reads will be on block boundaries, which might increase efficiency. */
464 xlseek (fd, pos, SEEK_SET, pretty_filename);
465 bytes_read = safe_read (fd, buffer, bytes_read);
466 if (bytes_read == SAFE_READ_ERROR)
468 error (0, errno, _("error reading %s"), quote (pretty_filename));
471 *read_pos = pos + bytes_read;
473 /* Count the incomplete line on files that don't end with a newline. */
474 if (bytes_read && buffer[bytes_read - 1] != '\n')
479 /* Scan backward, counting the newlines in this bufferfull. */
481 size_t n = bytes_read;
485 nl = memrchr (buffer, '\n', n);
491 /* If this newline isn't the last character in the buffer,
492 output the part that is after it. */
493 if (n != bytes_read - 1)
494 xwrite_stdout (nl + 1, bytes_read - (n + 1));
495 *read_pos += dump_remainder (pretty_filename, fd,
496 end_pos - (pos + bytes_read));
501 /* Not enough newlines in that bufferfull. */
502 if (pos == start_pos)
504 /* Not enough lines in the file; print everything from
505 start_pos to the end. */
506 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
507 *read_pos = start_pos + dump_remainder (pretty_filename, fd,
512 xlseek (fd, pos, SEEK_SET, pretty_filename);
514 bytes_read = safe_read (fd, buffer, BUFSIZ);
515 if (bytes_read == SAFE_READ_ERROR)
517 error (0, errno, _("error reading %s"), quote (pretty_filename));
521 *read_pos = pos + bytes_read;
523 while (bytes_read > 0);
528 /* Print the last N_LINES lines from the end of the standard input,
529 open for reading as pipe FD.
530 Buffer the text as a linked list of LBUFFERs, adding them as needed.
531 Return true if successful. */
534 pipe_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
542 struct linebuffer *next;
544 typedef struct linebuffer LBUFFER;
545 LBUFFER *first, *last, *tmp;
546 size_t total_lines = 0; /* Total number of newlines in all buffers. */
548 size_t n_read; /* Size in bytes of most recent read */
550 first = last = xmalloc (sizeof (LBUFFER));
551 first->nbytes = first->nlines = 0;
553 tmp = xmalloc (sizeof (LBUFFER));
555 /* Input is always read into a fresh buffer. */
558 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
559 if (n_read == 0 || n_read == SAFE_READ_ERROR)
561 tmp->nbytes = n_read;
566 /* Count the number of newlines just read. */
568 char const *buffer_end = tmp->buffer + n_read;
569 char const *p = tmp->buffer;
570 while ((p = memchr (p, '\n', buffer_end - p)))
576 total_lines += tmp->nlines;
578 /* If there is enough room in the last buffer read, just append the new
579 one to it. This is because when reading from a pipe, `n_read' can
580 often be very small. */
581 if (tmp->nbytes + last->nbytes < BUFSIZ)
583 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
584 last->nbytes += tmp->nbytes;
585 last->nlines += tmp->nlines;
589 /* If there's not enough room, link the new buffer onto the end of
590 the list, then either free up the oldest buffer for the next
591 read if that would leave enough lines, or else malloc a new one.
592 Some compaction mechanism is possible but probably not
594 last = last->next = tmp;
595 if (total_lines - first->nlines > n_lines)
598 total_lines -= first->nlines;
602 tmp = xmalloc (sizeof (LBUFFER));
608 if (n_read == SAFE_READ_ERROR)
610 error (0, errno, _("error reading %s"), quote (pretty_filename));
615 /* If the file is empty, then bail out. */
616 if (last->nbytes == 0)
619 /* This prevents a core dump when the pipe contains no newlines. */
623 /* Count the incomplete line on files that don't end with a newline. */
624 if (last->buffer[last->nbytes - 1] != '\n')
630 /* Run through the list, printing lines. First, skip over unneeded
632 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
633 total_lines -= tmp->nlines;
635 /* Find the correct beginning, then print the rest of the file. */
637 char const *beg = tmp->buffer;
638 char const *buffer_end = tmp->buffer + tmp->nbytes;
639 if (total_lines > n_lines)
641 /* Skip `total_lines' - `n_lines' newlines. We made sure that
642 `total_lines' - `n_lines' <= `tmp->nlines'. */
644 for (j = total_lines - n_lines; j; --j)
646 beg = memchr (beg, '\n', buffer_end - beg);
652 xwrite_stdout (beg, buffer_end - beg);
655 for (tmp = tmp->next; tmp; tmp = tmp->next)
656 xwrite_stdout (tmp->buffer, tmp->nbytes);
668 /* Print the last N_BYTES characters from the end of pipe FD.
669 This is a stripped down version of pipe_lines.
670 Return true if successful. */
673 pipe_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
680 struct charbuffer *next;
682 typedef struct charbuffer CBUFFER;
683 CBUFFER *first, *last, *tmp;
684 size_t i; /* Index into buffers. */
685 size_t total_bytes = 0; /* Total characters in all buffers. */
689 first = last = xmalloc (sizeof (CBUFFER));
692 tmp = xmalloc (sizeof (CBUFFER));
694 /* Input is always read into a fresh buffer. */
697 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
698 if (n_read == 0 || n_read == SAFE_READ_ERROR)
701 tmp->nbytes = n_read;
704 total_bytes += tmp->nbytes;
705 /* If there is enough room in the last buffer read, just append the new
706 one to it. This is because when reading from a pipe, `nbytes' can
707 often be very small. */
708 if (tmp->nbytes + last->nbytes < BUFSIZ)
710 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
711 last->nbytes += tmp->nbytes;
715 /* If there's not enough room, link the new buffer onto the end of
716 the list, then either free up the oldest buffer for the next
717 read if that would leave enough characters, or else malloc a new
718 one. Some compaction mechanism is possible but probably not
720 last = last->next = tmp;
721 if (total_bytes - first->nbytes > n_bytes)
724 total_bytes -= first->nbytes;
729 tmp = xmalloc (sizeof (CBUFFER));
736 if (n_read == SAFE_READ_ERROR)
738 error (0, errno, _("error reading %s"), quote (pretty_filename));
743 /* Run through the list, printing characters. First, skip over unneeded
745 for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
746 total_bytes -= tmp->nbytes;
748 /* Find the correct beginning, then print the rest of the file.
749 We made sure that `total_bytes' - `n_bytes' <= `tmp->nbytes'. */
750 if (total_bytes > n_bytes)
751 i = total_bytes - n_bytes;
754 xwrite_stdout (&tmp->buffer[i], tmp->nbytes - i);
756 for (tmp = tmp->next; tmp; tmp = tmp->next)
757 xwrite_stdout (tmp->buffer, tmp->nbytes);
769 /* Skip N_BYTES characters from the start of pipe FD, and print
770 any extra characters that were read beyond that.
771 Return 1 on error, 0 if ok, -1 if EOF. */
774 start_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
781 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
784 if (bytes_read == SAFE_READ_ERROR)
786 error (0, errno, _("error reading %s"), quote (pretty_filename));
789 read_pos += bytes_read;
790 if (bytes_read <= n_bytes)
791 n_bytes -= bytes_read;
794 size_t n_remaining = bytes_read - n_bytes;
796 xwrite_stdout (&buffer[n_bytes], n_remaining);
804 /* Skip N_LINES lines at the start of file or pipe FD, and print
805 any extra characters that were read beyond that.
806 Return 1 on error, 0 if ok, -1 if EOF. */
809 start_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
819 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
820 char *buffer_end = buffer + bytes_read;
821 if (bytes_read == 0) /* EOF */
823 if (bytes_read == SAFE_READ_ERROR) /* error */
825 error (0, errno, _("error reading %s"), quote (pretty_filename));
829 *read_pos += bytes_read;
831 while ((p = memchr (p, '\n', buffer_end - p)))
837 xwrite_stdout (p, buffer_end - p);
844 /* FIXME: describe */
847 recheck (struct File_spec *f, bool blocking)
849 /* open/fstat the file and announce if dev/ino have changed */
850 struct stat new_stats;
852 bool is_stdin = (STREQ (f->name, "-"));
853 bool was_tailable = f->tailable;
854 int prev_errnum = f->errnum;
858 : open (f->name, O_RDONLY | (blocking ? 0 : O_NONBLOCK)));
860 assert (valid_file_spec (f));
862 /* If the open fails because the file doesn't exist,
863 then mark the file as not tailable. */
864 f->tailable = !(reopen_inaccessible_files && fd == -1);
866 if (fd == -1 || fstat (fd, &new_stats) < 0)
874 /* FIXME-maybe: detect the case in which the file first becomes
875 unreadable (perms), and later becomes readable again and can
876 be seen to be the same file (dev/ino). Otherwise, tail prints
877 the entire contents of the file when it becomes readable. */
878 error (0, f->errnum, _("%s has become inaccessible"),
879 quote (pretty_name (f)));
883 /* say nothing... it's still not tailable */
886 else if (prev_errnum != errno)
888 error (0, errno, "%s", pretty_name (f));
891 else if (!IS_TAILABLE_FILE_TYPE (new_stats.st_mode))
895 error (0, 0, _("%s has been replaced with an untailable file;\
896 giving up on this name"),
897 quote (pretty_name (f)));
908 close_fd (fd, pretty_name (f));
909 close_fd (f->fd, pretty_name (f));
912 else if (prev_errnum && prev_errnum != ENOENT)
915 assert (f->fd == -1);
916 error (0, 0, _("%s has become accessible"), quote (pretty_name (f)));
918 else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
924 _("%s has appeared; following end of new file"),
925 quote (pretty_name (f)));
929 /* Close the old one. */
930 close_fd (f->fd, pretty_name (f));
932 /* File has been replaced (e.g., via log rotation) --
935 _("%s has been replaced; following end of new file"),
936 quote (pretty_name (f)));
943 /* This happens when one iteration finds the file missing,
944 then the preceding <dev,inode> pair is reused as the
945 file is recreated. */
950 close_fd (fd, pretty_name (f));
956 /* Start at the beginning of the file. */
957 record_open_fd (f, fd, 0, &new_stats, (is_stdin ? -1 : blocking));
958 xlseek (fd, 0, SEEK_SET, pretty_name (f));
962 /* Return true if any of the N_FILES files in F are live, i.e., have
963 open file descriptors. */
966 any_live_files (const struct File_spec *f, int n_files)
970 for (i = 0; i < n_files; i++)
976 /* Tail NFILES files forever, or until killed.
977 The pertinent information for each file is stored in an entry of F.
978 Loop over each of them, doing an fstat to see if they have changed size,
979 and an occasional open/fstat to see if any dev/ino pair has changed.
980 If none of them have changed size in one iteration, sleep for a
981 while and try again. Continue until the user interrupts us. */
984 tail_forever (struct File_spec *f, int nfiles, double sleep_interval)
986 /* Use blocking I/O as an optimization, when it's easy. */
987 bool blocking = (pid == 0 && follow_mode == Follow_descriptor
988 && nfiles == 1 && ! S_ISREG (f[0].mode));
990 bool writer_is_dead = false;
997 bool any_input = false;
999 for (i = 0; i < nfiles; i++)
1005 uintmax_t bytes_read;
1012 recheck (&f[i], blocking);
1017 name = pretty_name (&f[i]);
1020 if (f[i].blocking != blocking)
1022 int old_flags = fcntl (fd, F_GETFL);
1023 int new_flags = old_flags | (blocking ? 0 : O_NONBLOCK);
1025 || (new_flags != old_flags
1026 && fcntl (fd, F_SETFL, new_flags) == -1))
1027 error (EXIT_FAILURE, errno,
1028 _("%s: cannot change nonblocking mode"), name);
1029 f[i].blocking = blocking;
1034 if (fstat (fd, &stats) != 0)
1037 f[i].errnum = errno;
1038 error (0, errno, "%s", name);
1042 if ((! S_ISREG (stats.st_mode) || f[i].size == stats.st_size)
1043 && f[i].mtime.tv_sec == stats.st_mtime
1044 && f[i].mtime.tv_nsec == TIMESPEC_NS (stats.st_mtim)
1045 && f[i].mode == stats.st_mode)
1047 if ((max_n_unchanged_stats_between_opens
1048 <= f[i].n_unchanged_stats++)
1049 && follow_mode == Follow_name)
1051 recheck (&f[i], blocking);
1052 f[i].n_unchanged_stats = 0;
1057 /* This file has changed. Print out what we can, and
1058 then keep looping. */
1060 f[i].mtime.tv_sec = stats.st_mtime;
1061 f[i].mtime.tv_nsec = TIMESPEC_NS (stats.st_mtim);
1062 f[i].mode = stats.st_mode;
1065 f[i].n_unchanged_stats = 0;
1067 if (S_ISREG (mode) && stats.st_size < f[i].size)
1069 error (0, 0, _("%s: file truncated"), name);
1071 xlseek (fd, stats.st_size, SEEK_SET, name);
1072 f[i].size = stats.st_size;
1079 write_header (name);
1084 bytes_read = dump_remainder (name, fd,
1085 blocking ? COPY_A_BUFFER : COPY_TO_EOF);
1086 any_input |= (bytes_read != 0);
1087 f[i].size += bytes_read;
1090 if (! any_live_files (f, nfiles) && ! reopen_inaccessible_files)
1092 error (0, 0, _("no files remaining"));
1096 if ((!any_input | blocking) && fflush (stdout) != 0)
1097 error (EXIT_FAILURE, errno, _("write error"));
1099 /* If nothing was read, sleep and/or check for dead writers. */
1105 if (xnanosleep (sleep_interval))
1106 error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
1108 /* Once the writer is dead, read the files once more to
1109 avoid a race condition. */
1110 writer_is_dead = (pid != 0
1111 && kill (pid, 0) != 0
1112 /* Handle the case in which you cannot send a
1113 signal to the writer, so kill fails and sets
1120 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
1121 Return true if successful. */
1124 tail_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
1125 uintmax_t *read_pos)
1129 if (fstat (fd, &stats))
1131 error (0, errno, _("cannot fstat %s"), quote (pretty_filename));
1137 if ( ! presume_input_pipe
1138 && S_ISREG (stats.st_mode) && n_bytes <= OFF_T_MAX)
1140 xlseek (fd, n_bytes, SEEK_CUR, pretty_filename);
1141 *read_pos += n_bytes;
1145 int t = start_bytes (pretty_filename, fd, n_bytes, read_pos);
1149 *read_pos += dump_remainder (pretty_filename, fd, COPY_TO_EOF);
1153 if ( ! presume_input_pipe
1154 && S_ISREG (stats.st_mode) && n_bytes <= OFF_T_MAX)
1156 off_t current_pos = xlseek (fd, 0, SEEK_CUR, pretty_filename);
1157 off_t end_pos = xlseek (fd, 0, SEEK_END, pretty_filename);
1158 off_t diff = end_pos - current_pos;
1159 /* Be careful here. The current position may actually be
1160 beyond the end of the file. */
1161 off_t bytes_remaining = (diff = end_pos - current_pos) < 0 ? 0 : diff;
1164 if (bytes_remaining <= nb)
1166 /* From the current position to end of file, there are no
1167 more bytes than have been requested. So reposition the
1168 file pointer to the incoming current position and print
1169 everything after that. */
1170 *read_pos = xlseek (fd, current_pos, SEEK_SET, pretty_filename);
1174 /* There are more bytes remaining than were requested.
1176 *read_pos = xlseek (fd, -nb, SEEK_END, pretty_filename);
1178 *read_pos += dump_remainder (pretty_filename, fd, n_bytes);
1181 return pipe_bytes (pretty_filename, fd, n_bytes, read_pos);
1186 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
1187 Return true if successful. */
1190 tail_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
1191 uintmax_t *read_pos)
1195 if (fstat (fd, &stats))
1197 error (0, errno, _("cannot fstat %s"), quote (pretty_filename));
1203 int t = start_lines (pretty_filename, fd, n_lines, read_pos);
1206 *read_pos += dump_remainder (pretty_filename, fd, COPY_TO_EOF);
1210 off_t start_pos = -1;
1213 /* Use file_lines only if FD refers to a regular file for
1214 which lseek (... SEEK_END) works. */
1215 if ( ! presume_input_pipe
1216 && S_ISREG (stats.st_mode)
1217 && (start_pos = lseek (fd, 0, SEEK_CUR)) != -1
1218 && start_pos < (end_pos = lseek (fd, 0, SEEK_END)))
1220 *read_pos = end_pos;
1222 && ! file_lines (pretty_filename, fd, n_lines,
1223 start_pos, end_pos, read_pos))
1228 /* Under very unlikely circumstances, it is possible to reach
1229 this point after positioning the file pointer to end of file
1230 via the `lseek (...SEEK_END)' above. In that case, reposition
1231 the file pointer back to start_pos before calling pipe_lines. */
1232 if (start_pos != -1)
1233 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
1235 return pipe_lines (pretty_filename, fd, n_lines, read_pos);
1241 /* Display the last N_UNITS units of file FILENAME, open for reading
1242 via FD. Set *READ_POS to the position of the input stream pointer.
1243 *READ_POS is usually the number of bytes read and corresponds to an
1244 offset from the beginning of a file. However, it may be larger than
1245 OFF_T_MAX (as for an input pipe), and may also be larger than the
1246 number of bytes read (when an input pointer is initially not at
1247 beginning of file), and may be far greater than the number of bytes
1248 actually read for an input file that is seekable.
1249 Return true if successful. */
1252 tail (const char *filename, int fd, uintmax_t n_units,
1253 uintmax_t *read_pos)
1257 return tail_lines (filename, fd, n_units, read_pos);
1259 return tail_bytes (filename, fd, n_units, read_pos);
1262 /* Display the last N_UNITS units of the file described by F.
1263 Return true if successful. */
1266 tail_file (struct File_spec *f, uintmax_t n_units)
1271 bool is_stdin = (STREQ (f->name, "-"));
1275 have_read_stdin = true;
1277 if (O_BINARY && ! isatty (STDIN_FILENO))
1278 freopen (NULL, "rb", stdin);
1281 fd = open (f->name, O_RDONLY | O_BINARY);
1283 f->tailable = !(reopen_inaccessible_files && fd == -1);
1295 error (0, errno, _("cannot open %s for reading"),
1296 quote (pretty_name (f)));
1304 write_header (pretty_name (f));
1305 ok = tail (pretty_name (f), fd, n_units, &read_pos);
1310 #if TEST_RACE_BETWEEN_FINAL_READ_AND_INITIAL_FSTAT
1311 /* Before the tail function provided `read_pos', there was
1312 a race condition described in the URL below. This sleep
1313 call made the window big enough to exercise the problem. */
1317 if (fstat (fd, &stats) < 0)
1321 error (0, errno, _("error reading %s"), quote (pretty_name (f)));
1323 else if (!IS_TAILABLE_FILE_TYPE (stats.st_mode))
1325 error (0, 0, _("%s: cannot follow end of this type of file;\
1326 giving up on this name"),
1335 close_fd (fd, pretty_name (f));
1340 /* Note: we must use read_pos here, not stats.st_size,
1341 to avoid a race condition described by Ken Raeburn:
1342 http://mail.gnu.org/archive/html/bug-textutils/2003-05/msg00007.html */
1343 record_open_fd (f, fd, read_pos, &stats, (is_stdin ? -1 : 1));
1348 if (!is_stdin && close (fd))
1350 error (0, errno, _("error reading %s"), quote (pretty_name (f)));
1359 /* If obsolete usage is allowed, and the command line arguments are of
1360 the obsolete form and the option string is well-formed, set
1361 *N_UNITS, the globals COUNT_LINES, FOREVER, and FROM_START, and
1362 return true. If the command line arguments are obviously incorrect
1363 (e.g., because obsolete usage is not allowed and the arguments are
1364 incorrect for non-obsolete usage), report an error and exit.
1365 Otherwise, return false and don't modify any parameter or global
1369 parse_obsolete_option (int argc, char * const *argv, uintmax_t *n_units)
1371 const char *p = argv[1];
1372 const char *n_string;
1373 const char *n_string_end;
1374 bool obsolete_usage;
1375 int default_count = DEFAULT_N_LINES;
1377 bool t_count_lines = true;
1378 bool t_forever = false;
1380 /* With the obsolete form, there is one option string and
1381 (technically) at most one file argument. But we allow two or more
1386 obsolete_usage = (posix2_version () < 200112);
1394 /* Leading "+" is a file name in the non-obsolete form. */
1395 if (!obsolete_usage)
1398 t_from_start = true;
1402 /* In the non-obsolete form, "-" is standard input and "-c"
1403 requires an option-argument. The obsolete multidigit options
1404 are supported as a GNU extension even when conforming to
1405 POSIX 1003.1-2001, so don't complain about them. */
1406 if (!obsolete_usage && !p[p[0] == 'c'])
1409 t_from_start = false;
1414 while (ISDIGIT (*p))
1420 case 'b': default_count *= 512; /* Fall through. */
1421 case 'c': t_count_lines = false; /* Fall through. */
1422 case 'l': p++; break;
1434 if (n_string == n_string_end)
1435 *n_units = default_count;
1436 else if ((xstrtoumax (n_string, NULL, 10, n_units, "b")
1437 & ~LONGINT_INVALID_SUFFIX_CHAR)
1439 error (EXIT_FAILURE, 0, _("number in %s is too large"), quote (argv[1]));
1442 from_start = t_from_start;
1443 count_lines = t_count_lines;
1444 forever = t_forever;
1450 parse_options (int argc, char **argv,
1451 uintmax_t *n_units, enum header_mode *header_mode,
1452 double *sleep_interval)
1456 while ((c = getopt_long (argc, argv, "c:n:fFqs:v", long_options, NULL))
1463 follow_mode = Follow_name;
1464 reopen_inaccessible_files = true;
1469 count_lines = (c == 'n');
1472 else if (*optarg == '-')
1477 s_err = xstrtoumax (optarg, NULL, 10, n_units, "bkm");
1478 if (s_err != LONGINT_OK)
1480 error (EXIT_FAILURE, 0, "%s: %s", optarg,
1482 ? _("invalid number of lines")
1483 : _("invalid number of bytes")));
1489 case LONG_FOLLOW_OPTION:
1492 follow_mode = DEFAULT_FOLLOW_MODE;
1494 follow_mode = XARGMATCH ("--follow", optarg,
1495 follow_mode_string, follow_mode_map);
1498 case ALLOW_MISSING_OPTION:
1500 _("the --allow-missing option is deprecated; use --retry instead"));
1503 reopen_inaccessible_files = true;
1506 case MAX_UNCHANGED_STATS_OPTION:
1507 /* --max-unchanged-stats=N */
1508 if (xstrtoumax (optarg, NULL, 10,
1509 &max_n_unchanged_stats_between_opens,
1513 error (EXIT_FAILURE, 0,
1514 _("%s: invalid maximum number of unchanged stats between opens"),
1522 unsigned long int tmp_ulong;
1523 s_err = xstrtoul (optarg, NULL, 10, &tmp_ulong, "");
1524 if (s_err != LONGINT_OK || tmp_ulong > PID_T_MAX)
1526 error (EXIT_FAILURE, 0, _("%s: invalid PID"), optarg);
1532 case PRESUME_INPUT_PIPE_OPTION:
1533 presume_input_pipe = true;
1537 *header_mode = never;
1543 if (! (xstrtod (optarg, NULL, &s, c_strtod) && 0 <= s))
1544 error (EXIT_FAILURE, 0,
1545 _("%s: invalid number of seconds"), optarg);
1546 *sleep_interval = s;
1551 *header_mode = always;
1554 case_GETOPT_HELP_CHAR;
1556 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1559 usage (EXIT_FAILURE);
1563 if (reopen_inaccessible_files && follow_mode != Follow_name)
1564 error (0, 0, _("warning: --retry is useful only when following by name"));
1566 if (pid && !forever)
1568 _("warning: PID ignored; --pid=PID is useful only when following"));
1569 else if (pid && kill (pid, 0) != 0 && errno == ENOSYS)
1571 error (0, 0, _("warning: --pid=PID is not supported on this system"));
1577 main (int argc, char **argv)
1579 enum header_mode header_mode = multiple_files;
1581 /* If from_start, the number of items to skip before printing; otherwise,
1582 the number of items at the end of the file to print. Although the type
1583 is signed, the value is never negative. */
1584 uintmax_t n_units = DEFAULT_N_LINES;
1587 struct File_spec *F;
1589 bool obsolete_option;
1591 /* The number of seconds to sleep between iterations.
1592 During one iteration, every file name or descriptor is checked to
1593 see if it has changed. */
1594 double sleep_interval = 1.0;
1596 initialize_main (&argc, &argv);
1597 program_name = argv[0];
1598 setlocale (LC_ALL, "");
1599 bindtextdomain (PACKAGE, LOCALEDIR);
1600 textdomain (PACKAGE);
1602 atexit (close_stdout);
1604 have_read_stdin = false;
1607 forever = from_start = print_headers = false;
1608 obsolete_option = parse_obsolete_option (argc, argv, &n_units);
1609 argc -= obsolete_option;
1610 argv += obsolete_option;
1611 parse_options (argc, argv, &n_units, &header_mode, &sleep_interval);
1613 /* To start printing with item N_UNITS from the start of the file, skip
1614 N_UNITS - 1 items. `tail -n +0' is actually meaningless, but for Unix
1615 compatibility it's treated the same as `tail -n +1'. */
1624 n_files = argc - optind;
1625 file = argv + optind;
1629 static char *dummy_stdin = "-";
1631 file = &dummy_stdin;
1633 /* POSIX says that -f is ignored if no file operand is specified
1634 and standard input is a pipe. */
1638 if (fstat (STDIN_FILENO, &stats) == 0
1639 && IS_PIPE_LIKE_FILE_TYPE (stats.st_mode))
1645 bool found_hyphen = false;
1647 for (i = 0; i < n_files; i++)
1648 if (STREQ (file[i], "-"))
1649 found_hyphen = true;
1651 /* When following by name, there must be a name. */
1652 if (found_hyphen && follow_mode == Follow_name)
1653 error (EXIT_FAILURE, 0, _("cannot follow %s by name"), quote ("-"));
1655 /* When following forever, warn if any file is `-'.
1656 This is only a warning, since tail's output (before a failing seek,
1657 and that from any non-stdin files) might still be useful. */
1658 if (forever && found_hyphen && isatty (STDIN_FILENO))
1659 error (0, 0, _("warning: following standard input"
1660 " indefinitely is ineffective"));
1663 F = xnmalloc (n_files, sizeof *F);
1664 for (i = 0; i < n_files; i++)
1665 F[i].name = file[i];
1667 if (header_mode == always
1668 || (header_mode == multiple_files && n_files > 1))
1669 print_headers = true;
1671 if (O_BINARY && ! isatty (STDOUT_FILENO))
1672 freopen (NULL, "wb", stdout);
1674 for (i = 0; i < n_files; i++)
1675 ok &= tail_file (&F[i], n_units);
1678 tail_forever (F, n_files, sleep_interval);
1680 if (have_read_stdin && close (STDIN_FILENO) < 0)
1681 error (EXIT_FAILURE, errno, "-");
1682 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);