ls: support --hyperlink to output file:// URIs
[coreutils.git] / src / tail.c
blob316e72ec5eaa1db61cfba9070699b3b41944ad2f
1 /* tail -- output the last part of file(s)
2 Copyright (C) 1989-2017 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 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* Can display any amount of data, unlike the Unix version, which uses
18 a fixed size buffer and therefore can only deliver a limited number
19 of lines.
21 Original version by Paul Rubin <phr@ocf.berkeley.edu>.
22 Extensions by David MacKenzie <djm@gnu.ai.mit.edu>.
23 tail -f for multiple files by Ian Lance Taylor <ian@airs.com>.
24 inotify back-end by Giuseppe Scrivano <gscrivano@gnu.org>. */
26 #include <config.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <getopt.h>
31 #include <sys/types.h>
32 #include <signal.h>
34 #include "system.h"
35 #include "argmatch.h"
36 #include "c-strtod.h"
37 #include "die.h"
38 #include "error.h"
39 #include "fcntl--.h"
40 #include "isapipe.h"
41 #include "posixver.h"
42 #include "quote.h"
43 #include "safe-read.h"
44 #include "stat-size.h"
45 #include "stat-time.h"
46 #include "xbinary-io.h"
47 #include "xdectoint.h"
48 #include "xnanosleep.h"
49 #include "xstrtol.h"
50 #include "xstrtod.h"
52 #if HAVE_INOTIFY
53 # include "hash.h"
54 # include <sys/inotify.h>
55 /* 'select' is used by tail_forever_inotify. */
56 # include <sys/select.h>
58 /* inotify needs to know if a file is local. */
59 # include "fs.h"
60 # include "fs-is-local.h"
61 # if HAVE_SYS_STATFS_H
62 # include <sys/statfs.h>
63 # elif HAVE_SYS_VFS_H
64 # include <sys/vfs.h>
65 # endif
66 #endif
68 /* The official name of this program (e.g., no 'g' prefix). */
69 #define PROGRAM_NAME "tail"
71 #define AUTHORS \
72 proper_name ("Paul Rubin"), \
73 proper_name ("David MacKenzie"), \
74 proper_name ("Ian Lance Taylor"), \
75 proper_name ("Jim Meyering")
77 /* Number of items to tail. */
78 #define DEFAULT_N_LINES 10
80 /* Special values for dump_remainder's N_BYTES parameter. */
81 #define COPY_TO_EOF UINTMAX_MAX
82 #define COPY_A_BUFFER (UINTMAX_MAX - 1)
84 /* FIXME: make Follow_name the default? */
85 #define DEFAULT_FOLLOW_MODE Follow_descriptor
87 enum Follow_mode
89 /* Follow the name of each file: if the file is renamed, try to reopen
90 that name and track the end of the new file if/when it's recreated.
91 This is useful for tracking logs that are occasionally rotated. */
92 Follow_name = 1,
94 /* Follow each descriptor obtained upon opening a file.
95 That means we'll continue to follow the end of a file even after
96 it has been renamed or unlinked. */
97 Follow_descriptor = 2
100 /* The types of files for which tail works. */
101 #define IS_TAILABLE_FILE_TYPE(Mode) \
102 (S_ISREG (Mode) || S_ISFIFO (Mode) || S_ISSOCK (Mode) || S_ISCHR (Mode))
104 static char const *const follow_mode_string[] =
106 "descriptor", "name", NULL
109 static enum Follow_mode const follow_mode_map[] =
111 Follow_descriptor, Follow_name,
114 struct File_spec
116 /* The actual file name, or "-" for stdin. */
117 char *name;
119 /* Attributes of the file the last time we checked. */
120 off_t size;
121 struct timespec mtime;
122 dev_t dev;
123 ino_t ino;
124 mode_t mode;
126 /* The specified name initially referred to a directory or some other
127 type for which tail isn't meaningful. Unlike for a permission problem
128 (tailable, below) once this is set, the name is not checked ever again. */
129 bool ignore;
131 /* See the description of fremote. */
132 bool remote;
134 /* A file is tailable if it exists, is readable, and is of type
135 IS_TAILABLE_FILE_TYPE. */
136 bool tailable;
138 /* File descriptor on which the file is open; -1 if it's not open. */
139 int fd;
141 /* The value of errno seen last time we checked this file. */
142 int errnum;
144 /* 1 if O_NONBLOCK is clear, 0 if set, -1 if not known. */
145 int blocking;
147 #if HAVE_INOTIFY
148 /* The watch descriptor used by inotify. */
149 int wd;
151 /* The parent directory watch descriptor. It is used only
152 * when Follow_name is used. */
153 int parent_wd;
155 /* Offset in NAME of the basename part. */
156 size_t basename_start;
157 #endif
159 /* See description of DEFAULT_MAX_N_... below. */
160 uintmax_t n_unchanged_stats;
163 /* Keep trying to open a file even if it is inaccessible when tail starts
164 or if it becomes inaccessible later -- useful only with -f. */
165 static bool reopen_inaccessible_files;
167 /* If true, interpret the numeric argument as the number of lines.
168 Otherwise, interpret it as the number of bytes. */
169 static bool count_lines;
171 /* Whether we follow the name of each file or the file descriptor
172 that is initially associated with each name. */
173 static enum Follow_mode follow_mode = Follow_descriptor;
175 /* If true, read from the ends of all specified files until killed. */
176 static bool forever;
178 /* If true, monitor output so we exit if pipe reader terminates. */
179 static bool monitor_output;
181 /* If true, count from start of file instead of end. */
182 static bool from_start;
184 /* If true, print filename headers. */
185 static bool print_headers;
187 /* Character to split lines by. */
188 static char line_end;
190 /* When to print the filename banners. */
191 enum header_mode
193 multiple_files, always, never
196 /* When tailing a file by name, if there have been this many consecutive
197 iterations for which the file has not changed, then open/fstat
198 the file to determine if that file name is still associated with the
199 same device/inode-number pair as before. This option is meaningful only
200 when following by name. --max-unchanged-stats=N */
201 #define DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS 5
202 static uintmax_t max_n_unchanged_stats_between_opens =
203 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS;
205 /* The process ID of the process (presumably on the current host)
206 that is writing to all followed files. */
207 static pid_t pid;
209 /* True if we have ever read standard input. */
210 static bool have_read_stdin;
212 /* If nonzero, skip the is-regular-file test used to determine whether
213 to use the lseek optimization. Instead, use the more general (and
214 more expensive) code unconditionally. Intended solely for testing. */
215 static bool presume_input_pipe;
217 /* If nonzero then don't use inotify even if available. */
218 static bool disable_inotify;
220 /* For long options that have no equivalent short option, use a
221 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
222 enum
224 RETRY_OPTION = CHAR_MAX + 1,
225 MAX_UNCHANGED_STATS_OPTION,
226 PID_OPTION,
227 PRESUME_INPUT_PIPE_OPTION,
228 LONG_FOLLOW_OPTION,
229 DISABLE_INOTIFY_OPTION
232 static struct option const long_options[] =
234 {"bytes", required_argument, NULL, 'c'},
235 {"follow", optional_argument, NULL, LONG_FOLLOW_OPTION},
236 {"lines", required_argument, NULL, 'n'},
237 {"max-unchanged-stats", required_argument, NULL, MAX_UNCHANGED_STATS_OPTION},
238 {"-disable-inotify", no_argument, NULL,
239 DISABLE_INOTIFY_OPTION}, /* do not document */
240 {"pid", required_argument, NULL, PID_OPTION},
241 {"-presume-input-pipe", no_argument, NULL,
242 PRESUME_INPUT_PIPE_OPTION}, /* do not document */
243 {"quiet", no_argument, NULL, 'q'},
244 {"retry", no_argument, NULL, RETRY_OPTION},
245 {"silent", no_argument, NULL, 'q'},
246 {"sleep-interval", required_argument, NULL, 's'},
247 {"verbose", no_argument, NULL, 'v'},
248 {"zero-terminated", no_argument, NULL, 'z'},
249 {GETOPT_HELP_OPTION_DECL},
250 {GETOPT_VERSION_OPTION_DECL},
251 {NULL, 0, NULL, 0}
254 void
255 usage (int status)
257 if (status != EXIT_SUCCESS)
258 emit_try_help ();
259 else
261 printf (_("\
262 Usage: %s [OPTION]... [FILE]...\n\
264 program_name);
265 printf (_("\
266 Print the last %d lines of each FILE to standard output.\n\
267 With more than one FILE, precede each with a header giving the file name.\n\
268 "), DEFAULT_N_LINES);
270 emit_stdin_note ();
271 emit_mandatory_arg_note ();
273 fputs (_("\
274 -c, --bytes=[+]NUM output the last NUM bytes; or use -c +NUM to\n\
275 output starting with byte NUM of each file\n\
276 "), stdout);
277 fputs (_("\
278 -f, --follow[={name|descriptor}]\n\
279 output appended data as the file grows;\n\
280 an absent option argument means 'descriptor'\n\
281 -F same as --follow=name --retry\n\
282 "), stdout);
283 printf (_("\
284 -n, --lines=[+]NUM output the last NUM lines, instead of the last %d;\n\
285 or use -n +NUM to output starting with line NUM\n\
286 --max-unchanged-stats=N\n\
287 with --follow=name, reopen a FILE which has not\n\
288 changed size after N (default %d) iterations\n\
289 to see if it has been unlinked or renamed\n\
290 (this is the usual case of rotated log files);\n\
291 with inotify, this option is rarely useful\n\
293 DEFAULT_N_LINES,
294 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS
296 fputs (_("\
297 --pid=PID with -f, terminate after process ID, PID dies\n\
298 -q, --quiet, --silent never output headers giving file names\n\
299 --retry keep trying to open a file if it is inaccessible\n\
300 "), stdout);
301 fputs (_("\
302 -s, --sleep-interval=N with -f, sleep for approximately N seconds\n\
303 (default 1.0) between iterations;\n\
304 with inotify and --pid=P, check process P at\n\
305 least once every N seconds\n\
306 -v, --verbose always output headers giving file names\n\
307 "), stdout);
308 fputs (_("\
309 -z, --zero-terminated line delimiter is NUL, not newline\n\
310 "), stdout);
311 fputs (HELP_OPTION_DESCRIPTION, stdout);
312 fputs (VERSION_OPTION_DESCRIPTION, stdout);
313 fputs (_("\
315 NUM may have a multiplier suffix:\n\
316 b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\
317 GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.\n\
319 "), stdout);
320 fputs (_("\
321 With --follow (-f), tail defaults to following the file descriptor, which\n\
322 means that even if a tail'ed file is renamed, tail will continue to track\n\
323 its end. This default behavior is not desirable when you really want to\n\
324 track the actual name of the file, not the file descriptor (e.g., log\n\
325 rotation). Use --follow=name in that case. That causes tail to track the\n\
326 named file in a way that accommodates renaming, removal and creation.\n\
327 "), stdout);
328 emit_ancillary_info (PROGRAM_NAME);
330 exit (status);
333 /* If the output has gone away, then terminate
334 as we would if we had written to this output. */
335 static void
336 check_output_alive (void)
338 if (! monitor_output)
339 return;
341 struct timeval delay;
342 delay.tv_sec = delay.tv_usec = 0;
344 fd_set rfd;
345 FD_ZERO (&rfd);
346 FD_SET (STDOUT_FILENO, &rfd);
348 /* readable event on STDOUT is equivalent to POLLERR,
349 and implies an error condition on output like broken pipe. */
350 if (select (STDOUT_FILENO + 1, &rfd, NULL, NULL, &delay) == 1)
351 raise (SIGPIPE);
354 static bool
355 valid_file_spec (struct File_spec const *f)
357 /* Exactly one of the following subexpressions must be true. */
358 return ((f->fd == -1) ^ (f->errnum == 0));
361 static char const *
362 pretty_name (struct File_spec const *f)
364 return (STREQ (f->name, "-") ? _("standard input") : f->name);
367 /* Record a file F with descriptor FD, size SIZE, status ST, and
368 blocking status BLOCKING. */
370 static void
371 record_open_fd (struct File_spec *f, int fd,
372 off_t size, struct stat const *st,
373 int blocking)
375 f->fd = fd;
376 f->size = size;
377 f->mtime = get_stat_mtime (st);
378 f->dev = st->st_dev;
379 f->ino = st->st_ino;
380 f->mode = st->st_mode;
381 f->blocking = blocking;
382 f->n_unchanged_stats = 0;
383 f->ignore = false;
386 /* Close the file with descriptor FD and name FILENAME. */
388 static void
389 close_fd (int fd, const char *filename)
391 if (fd != -1 && fd != STDIN_FILENO && close (fd))
393 error (0, errno, _("closing %s (fd=%d)"), quoteaf (filename), fd);
397 static void
398 write_header (const char *pretty_filename)
400 static bool first_file = true;
402 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), pretty_filename);
403 first_file = false;
406 /* Write N_BYTES from BUFFER to stdout.
407 Exit immediately on error with a single diagnostic. */
409 static void
410 xwrite_stdout (char const *buffer, size_t n_bytes)
412 if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) < n_bytes)
414 clearerr (stdout); /* To avoid redundant close_stdout diagnostic. */
415 die (EXIT_FAILURE, errno, _("error writing %s"),
416 quoteaf ("standard output"));
420 /* Read and output N_BYTES of file PRETTY_FILENAME starting at the current
421 position in FD. If N_BYTES is COPY_TO_EOF, then copy until end of file.
422 If N_BYTES is COPY_A_BUFFER, then copy at most one buffer's worth.
423 Return the number of bytes read from the file. */
425 static uintmax_t
426 dump_remainder (bool want_header, const char *pretty_filename, int fd,
427 uintmax_t n_bytes)
429 uintmax_t n_written;
430 uintmax_t n_remaining = n_bytes;
432 n_written = 0;
433 while (1)
435 char buffer[BUFSIZ];
436 size_t n = MIN (n_remaining, BUFSIZ);
437 size_t bytes_read = safe_read (fd, buffer, n);
438 if (bytes_read == SAFE_READ_ERROR)
440 if (errno != EAGAIN)
441 die (EXIT_FAILURE, errno, _("error reading %s"),
442 quoteaf (pretty_filename));
443 break;
445 if (bytes_read == 0)
446 break;
447 if (want_header)
449 write_header (pretty_filename);
450 want_header = false;
452 xwrite_stdout (buffer, bytes_read);
453 n_written += bytes_read;
454 if (n_bytes != COPY_TO_EOF)
456 n_remaining -= bytes_read;
457 if (n_remaining == 0 || n_bytes == COPY_A_BUFFER)
458 break;
462 return n_written;
465 /* Call lseek with the specified arguments, where file descriptor FD
466 corresponds to the file, FILENAME.
467 Give a diagnostic and exit nonzero if lseek fails.
468 Otherwise, return the resulting offset. */
470 static off_t
471 xlseek (int fd, off_t offset, int whence, char const *filename)
473 off_t new_offset = lseek (fd, offset, whence);
474 char buf[INT_BUFSIZE_BOUND (offset)];
475 char *s;
477 if (0 <= new_offset)
478 return new_offset;
480 s = offtostr (offset, buf);
481 switch (whence)
483 case SEEK_SET:
484 error (0, errno, _("%s: cannot seek to offset %s"),
485 quotef (filename), s);
486 break;
487 case SEEK_CUR:
488 error (0, errno, _("%s: cannot seek to relative offset %s"),
489 quotef (filename), s);
490 break;
491 case SEEK_END:
492 error (0, errno, _("%s: cannot seek to end-relative offset %s"),
493 quotef (filename), s);
494 break;
495 default:
496 abort ();
499 exit (EXIT_FAILURE);
502 /* Print the last N_LINES lines from the end of file FD.
503 Go backward through the file, reading 'BUFSIZ' bytes at a time (except
504 probably the first), until we hit the start of the file or have
505 read NUMBER newlines.
506 START_POS is the starting position of the read pointer for the file
507 associated with FD (may be nonzero).
508 END_POS is the file offset of EOF (one larger than offset of last byte).
509 Return true if successful. */
511 static bool
512 file_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
513 off_t start_pos, off_t end_pos, uintmax_t *read_pos)
515 char buffer[BUFSIZ];
516 size_t bytes_read;
517 off_t pos = end_pos;
519 if (n_lines == 0)
520 return true;
522 /* Set 'bytes_read' to the size of the last, probably partial, buffer;
523 0 < 'bytes_read' <= 'BUFSIZ'. */
524 bytes_read = (pos - start_pos) % BUFSIZ;
525 if (bytes_read == 0)
526 bytes_read = BUFSIZ;
527 /* Make 'pos' a multiple of 'BUFSIZ' (0 if the file is short), so that all
528 reads will be on block boundaries, which might increase efficiency. */
529 pos -= bytes_read;
530 xlseek (fd, pos, SEEK_SET, pretty_filename);
531 bytes_read = safe_read (fd, buffer, bytes_read);
532 if (bytes_read == SAFE_READ_ERROR)
534 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
535 return false;
537 *read_pos = pos + bytes_read;
539 /* Count the incomplete line on files that don't end with a newline. */
540 if (bytes_read && buffer[bytes_read - 1] != line_end)
541 --n_lines;
545 /* Scan backward, counting the newlines in this bufferfull. */
547 size_t n = bytes_read;
548 while (n)
550 char const *nl;
551 nl = memrchr (buffer, line_end, n);
552 if (nl == NULL)
553 break;
554 n = nl - buffer;
555 if (n_lines-- == 0)
557 /* If this newline isn't the last character in the buffer,
558 output the part that is after it. */
559 if (n != bytes_read - 1)
560 xwrite_stdout (nl + 1, bytes_read - (n + 1));
561 *read_pos += dump_remainder (false, pretty_filename, fd,
562 end_pos - (pos + bytes_read));
563 return true;
567 /* Not enough newlines in that bufferfull. */
568 if (pos == start_pos)
570 /* Not enough lines in the file; print everything from
571 start_pos to the end. */
572 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
573 *read_pos = start_pos + dump_remainder (false, pretty_filename, fd,
574 end_pos);
575 return true;
577 pos -= BUFSIZ;
578 xlseek (fd, pos, SEEK_SET, pretty_filename);
580 bytes_read = safe_read (fd, buffer, BUFSIZ);
581 if (bytes_read == SAFE_READ_ERROR)
583 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
584 return false;
587 *read_pos = pos + bytes_read;
589 while (bytes_read > 0);
591 return true;
594 /* Print the last N_LINES lines from the end of the standard input,
595 open for reading as pipe FD.
596 Buffer the text as a linked list of LBUFFERs, adding them as needed.
597 Return true if successful. */
599 static bool
600 pipe_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
601 uintmax_t *read_pos)
603 struct linebuffer
605 char buffer[BUFSIZ];
606 size_t nbytes;
607 size_t nlines;
608 struct linebuffer *next;
610 typedef struct linebuffer LBUFFER;
611 LBUFFER *first, *last, *tmp;
612 size_t total_lines = 0; /* Total number of newlines in all buffers. */
613 bool ok = true;
614 size_t n_read; /* Size in bytes of most recent read */
616 first = last = xmalloc (sizeof (LBUFFER));
617 first->nbytes = first->nlines = 0;
618 first->next = NULL;
619 tmp = xmalloc (sizeof (LBUFFER));
621 /* Input is always read into a fresh buffer. */
622 while (1)
624 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
625 if (n_read == 0 || n_read == SAFE_READ_ERROR)
626 break;
627 tmp->nbytes = n_read;
628 *read_pos += n_read;
629 tmp->nlines = 0;
630 tmp->next = NULL;
632 /* Count the number of newlines just read. */
634 char const *buffer_end = tmp->buffer + n_read;
635 char const *p = tmp->buffer;
636 while ((p = memchr (p, line_end, buffer_end - p)))
638 ++p;
639 ++tmp->nlines;
642 total_lines += tmp->nlines;
644 /* If there is enough room in the last buffer read, just append the new
645 one to it. This is because when reading from a pipe, 'n_read' can
646 often be very small. */
647 if (tmp->nbytes + last->nbytes < BUFSIZ)
649 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
650 last->nbytes += tmp->nbytes;
651 last->nlines += tmp->nlines;
653 else
655 /* If there's not enough room, link the new buffer onto the end of
656 the list, then either free up the oldest buffer for the next
657 read if that would leave enough lines, or else malloc a new one.
658 Some compaction mechanism is possible but probably not
659 worthwhile. */
660 last = last->next = tmp;
661 if (total_lines - first->nlines > n_lines)
663 tmp = first;
664 total_lines -= first->nlines;
665 first = first->next;
667 else
668 tmp = xmalloc (sizeof (LBUFFER));
672 free (tmp);
674 if (n_read == SAFE_READ_ERROR)
676 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
677 ok = false;
678 goto free_lbuffers;
681 /* If the file is empty, then bail out. */
682 if (last->nbytes == 0)
683 goto free_lbuffers;
685 /* This prevents a core dump when the pipe contains no newlines. */
686 if (n_lines == 0)
687 goto free_lbuffers;
689 /* Count the incomplete line on files that don't end with a newline. */
690 if (last->buffer[last->nbytes - 1] != line_end)
692 ++last->nlines;
693 ++total_lines;
696 /* Run through the list, printing lines. First, skip over unneeded
697 buffers. */
698 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
699 total_lines -= tmp->nlines;
701 /* Find the correct beginning, then print the rest of the file. */
703 char const *beg = tmp->buffer;
704 char const *buffer_end = tmp->buffer + tmp->nbytes;
705 if (total_lines > n_lines)
707 /* Skip 'total_lines' - 'n_lines' newlines. We made sure that
708 'total_lines' - 'n_lines' <= 'tmp->nlines'. */
709 size_t j;
710 for (j = total_lines - n_lines; j; --j)
712 beg = memchr (beg, line_end, buffer_end - beg);
713 assert (beg);
714 ++beg;
718 xwrite_stdout (beg, buffer_end - beg);
721 for (tmp = tmp->next; tmp; tmp = tmp->next)
722 xwrite_stdout (tmp->buffer, tmp->nbytes);
724 free_lbuffers:
725 while (first)
727 tmp = first->next;
728 free (first);
729 first = tmp;
731 return ok;
734 /* Print the last N_BYTES characters from the end of pipe FD.
735 This is a stripped down version of pipe_lines.
736 Return true if successful. */
738 static bool
739 pipe_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
740 uintmax_t *read_pos)
742 struct charbuffer
744 char buffer[BUFSIZ];
745 size_t nbytes;
746 struct charbuffer *next;
748 typedef struct charbuffer CBUFFER;
749 CBUFFER *first, *last, *tmp;
750 size_t i; /* Index into buffers. */
751 size_t total_bytes = 0; /* Total characters in all buffers. */
752 bool ok = true;
753 size_t n_read;
755 first = last = xmalloc (sizeof (CBUFFER));
756 first->nbytes = 0;
757 first->next = NULL;
758 tmp = xmalloc (sizeof (CBUFFER));
760 /* Input is always read into a fresh buffer. */
761 while (1)
763 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
764 if (n_read == 0 || n_read == SAFE_READ_ERROR)
765 break;
766 *read_pos += n_read;
767 tmp->nbytes = n_read;
768 tmp->next = NULL;
770 total_bytes += tmp->nbytes;
771 /* If there is enough room in the last buffer read, just append the new
772 one to it. This is because when reading from a pipe, 'nbytes' can
773 often be very small. */
774 if (tmp->nbytes + last->nbytes < BUFSIZ)
776 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
777 last->nbytes += tmp->nbytes;
779 else
781 /* If there's not enough room, link the new buffer onto the end of
782 the list, then either free up the oldest buffer for the next
783 read if that would leave enough characters, or else malloc a new
784 one. Some compaction mechanism is possible but probably not
785 worthwhile. */
786 last = last->next = tmp;
787 if (total_bytes - first->nbytes > n_bytes)
789 tmp = first;
790 total_bytes -= first->nbytes;
791 first = first->next;
793 else
795 tmp = xmalloc (sizeof (CBUFFER));
800 free (tmp);
802 if (n_read == SAFE_READ_ERROR)
804 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
805 ok = false;
806 goto free_cbuffers;
809 /* Run through the list, printing characters. First, skip over unneeded
810 buffers. */
811 for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
812 total_bytes -= tmp->nbytes;
814 /* Find the correct beginning, then print the rest of the file.
815 We made sure that 'total_bytes' - 'n_bytes' <= 'tmp->nbytes'. */
816 if (total_bytes > n_bytes)
817 i = total_bytes - n_bytes;
818 else
819 i = 0;
820 xwrite_stdout (&tmp->buffer[i], tmp->nbytes - i);
822 for (tmp = tmp->next; tmp; tmp = tmp->next)
823 xwrite_stdout (tmp->buffer, tmp->nbytes);
825 free_cbuffers:
826 while (first)
828 tmp = first->next;
829 free (first);
830 first = tmp;
832 return ok;
835 /* Skip N_BYTES characters from the start of pipe FD, and print
836 any extra characters that were read beyond that.
837 Return 1 on error, 0 if ok, -1 if EOF. */
839 static int
840 start_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
841 uintmax_t *read_pos)
843 char buffer[BUFSIZ];
845 while (0 < n_bytes)
847 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
848 if (bytes_read == 0)
849 return -1;
850 if (bytes_read == SAFE_READ_ERROR)
852 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
853 return 1;
855 *read_pos += bytes_read;
856 if (bytes_read <= n_bytes)
857 n_bytes -= bytes_read;
858 else
860 size_t n_remaining = bytes_read - n_bytes;
861 if (n_remaining)
862 xwrite_stdout (&buffer[n_bytes], n_remaining);
863 break;
867 return 0;
870 /* Skip N_LINES lines at the start of file or pipe FD, and print
871 any extra characters that were read beyond that.
872 Return 1 on error, 0 if ok, -1 if EOF. */
874 static int
875 start_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
876 uintmax_t *read_pos)
878 if (n_lines == 0)
879 return 0;
881 while (1)
883 char buffer[BUFSIZ];
884 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
885 if (bytes_read == 0) /* EOF */
886 return -1;
887 if (bytes_read == SAFE_READ_ERROR) /* error */
889 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
890 return 1;
893 char *buffer_end = buffer + bytes_read;
895 *read_pos += bytes_read;
897 char *p = buffer;
898 while ((p = memchr (p, line_end, buffer_end - p)))
900 ++p;
901 if (--n_lines == 0)
903 if (p < buffer_end)
904 xwrite_stdout (p, buffer_end - p);
905 return 0;
911 /* Return false when FD is open on a file residing on a local file system.
912 If fstatfs fails, give a diagnostic and return true.
913 If fstatfs cannot be called, return true. */
914 static bool
915 fremote (int fd, const char *name)
917 bool remote = true; /* be conservative (poll by default). */
919 #if HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE && defined __linux__
920 struct statfs buf;
921 int err = fstatfs (fd, &buf);
922 if (err != 0)
924 /* On at least linux-2.6.38, fstatfs fails with ENOSYS when FD
925 is open on a pipe. Treat that like a remote file. */
926 if (errno != ENOSYS)
927 error (0, errno, _("cannot determine location of %s. "
928 "reverting to polling"), quoteaf (name));
930 else
932 switch (is_local_fs_type (buf.f_type))
934 case 0:
935 break;
936 case -1:
937 /* Treat unrecognized file systems as "remote", so caller polls.
938 Note README-release has instructions for syncing the internal
939 list with the latest Linux kernel file system constants. */
940 break;
941 case 1:
942 remote = false;
943 break;
944 default:
945 assert (!"unexpected return value from is_local_fs_type");
948 #endif
950 return remote;
953 /* open/fstat F->name and handle changes. */
954 static void
955 recheck (struct File_spec *f, bool blocking)
957 struct stat new_stats;
958 bool ok = true;
959 bool is_stdin = (STREQ (f->name, "-"));
960 bool was_tailable = f->tailable;
961 int prev_errnum = f->errnum;
962 bool new_file;
963 int fd = (is_stdin
964 ? STDIN_FILENO
965 : open (f->name, O_RDONLY | (blocking ? 0 : O_NONBLOCK)));
967 assert (valid_file_spec (f));
969 /* If the open fails because the file doesn't exist,
970 then mark the file as not tailable. */
971 f->tailable = !(reopen_inaccessible_files && fd == -1);
973 if (! disable_inotify && ! lstat (f->name, &new_stats)
974 && S_ISLNK (new_stats.st_mode))
976 /* Diagnose the edge case where a regular file is changed
977 to a symlink. We avoid inotify with symlinks since
978 it's awkward to match between symlink name and target. */
979 ok = false;
980 f->errnum = -1;
981 f->ignore = true;
983 error (0, 0, _("%s has been replaced with an untailable symbolic link"),
984 quoteaf (pretty_name (f)));
986 else if (fd == -1 || fstat (fd, &new_stats) < 0)
988 ok = false;
989 f->errnum = errno;
990 if (!f->tailable)
992 if (was_tailable)
994 /* FIXME-maybe: detect the case in which the file first becomes
995 unreadable (perms), and later becomes readable again and can
996 be seen to be the same file (dev/ino). Otherwise, tail prints
997 the entire contents of the file when it becomes readable. */
998 error (0, f->errnum, _("%s has become inaccessible"),
999 quoteaf (pretty_name (f)));
1001 else
1003 /* say nothing... it's still not tailable */
1006 else if (prev_errnum != errno)
1007 error (0, errno, "%s", quotef (pretty_name (f)));
1009 else if (!IS_TAILABLE_FILE_TYPE (new_stats.st_mode))
1011 ok = false;
1012 f->errnum = -1;
1013 f->tailable = false;
1014 f->ignore = ! (reopen_inaccessible_files && follow_mode == Follow_name);
1015 if (was_tailable || prev_errnum != f->errnum)
1016 error (0, 0, _("%s has been replaced with an untailable file%s"),
1017 quoteaf (pretty_name (f)),
1018 f->ignore ? _("; giving up on this name") : "");
1020 else if ((f->remote = fremote (fd, pretty_name (f))) && ! disable_inotify)
1022 ok = false;
1023 f->errnum = -1;
1024 error (0, 0, _("%s has been replaced with an untailable remote file"),
1025 quoteaf (pretty_name (f)));
1026 f->ignore = true;
1027 f->remote = true;
1029 else
1031 f->errnum = 0;
1034 new_file = false;
1035 if (!ok)
1037 close_fd (fd, pretty_name (f));
1038 close_fd (f->fd, pretty_name (f));
1039 f->fd = -1;
1041 else if (prev_errnum && prev_errnum != ENOENT)
1043 new_file = true;
1044 assert (f->fd == -1);
1045 error (0, 0, _("%s has become accessible"), quoteaf (pretty_name (f)));
1047 else if (f->fd == -1)
1049 /* A new file even when inodes haven't changed as <dev,inode>
1050 pairs can be reused, and we know the file was missing
1051 on the previous iteration. Note this also means the file
1052 is redisplayed in --follow=name mode if renamed away from
1053 and back to a monitored name. */
1054 new_file = true;
1056 error (0, 0,
1057 _("%s has appeared; following new file"),
1058 quoteaf (pretty_name (f)));
1060 else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
1062 /* File has been replaced (e.g., via log rotation) --
1063 tail the new one. */
1064 new_file = true;
1066 error (0, 0,
1067 _("%s has been replaced; following new file"),
1068 quoteaf (pretty_name (f)));
1070 /* Close the old one. */
1071 close_fd (f->fd, pretty_name (f));
1074 else
1076 /* No changes detected, so close new fd. */
1077 close_fd (fd, pretty_name (f));
1080 /* FIXME: When a log is rotated, daemons tend to log to the
1081 old file descriptor until the new file is present and
1082 the daemon is sent a signal. Therefore tail may miss entries
1083 being written to the old file. Perhaps we should keep
1084 the older file open and continue to monitor it until
1085 data is written to a new file. */
1086 if (new_file)
1088 /* Start at the beginning of the file. */
1089 record_open_fd (f, fd, 0, &new_stats, (is_stdin ? -1 : blocking));
1090 xlseek (fd, 0, SEEK_SET, pretty_name (f));
1094 /* Return true if any of the N_FILES files in F are live, i.e., have
1095 open file descriptors, or should be checked again (see --retry).
1096 When following descriptors, checking should only continue when any
1097 of the files is not yet ignored. */
1099 static bool
1100 any_live_files (const struct File_spec *f, size_t n_files)
1102 /* In inotify mode, ignore may be set for files
1103 which may later be replaced with new files.
1104 So always consider files live in -F mode. */
1105 if (reopen_inaccessible_files && follow_mode == Follow_name)
1106 return true;
1108 for (size_t i = 0; i < n_files; i++)
1110 if (0 <= f[i].fd)
1111 return true;
1112 else
1114 if (! f[i].ignore && reopen_inaccessible_files)
1115 return true;
1119 return false;
1122 /* Tail N_FILES files forever, or until killed.
1123 The pertinent information for each file is stored in an entry of F.
1124 Loop over each of them, doing an fstat to see if they have changed size,
1125 and an occasional open/fstat to see if any dev/ino pair has changed.
1126 If none of them have changed size in one iteration, sleep for a
1127 while and try again. Continue until the user interrupts us. */
1129 static void
1130 tail_forever (struct File_spec *f, size_t n_files, double sleep_interval)
1132 /* Use blocking I/O as an optimization, when it's easy. */
1133 bool blocking = (pid == 0 && follow_mode == Follow_descriptor
1134 && n_files == 1 && f[0].fd != -1 && ! S_ISREG (f[0].mode));
1135 size_t last;
1136 bool writer_is_dead = false;
1138 last = n_files - 1;
1140 while (1)
1142 size_t i;
1143 bool any_input = false;
1145 for (i = 0; i < n_files; i++)
1147 int fd;
1148 char const *name;
1149 mode_t mode;
1150 struct stat stats;
1151 uintmax_t bytes_read;
1153 if (f[i].ignore)
1154 continue;
1156 if (f[i].fd < 0)
1158 recheck (&f[i], blocking);
1159 continue;
1162 fd = f[i].fd;
1163 name = pretty_name (&f[i]);
1164 mode = f[i].mode;
1166 if (f[i].blocking != blocking)
1168 int old_flags = fcntl (fd, F_GETFL);
1169 int new_flags = old_flags | (blocking ? 0 : O_NONBLOCK);
1170 if (old_flags < 0
1171 || (new_flags != old_flags
1172 && fcntl (fd, F_SETFL, new_flags) == -1))
1174 /* Don't update f[i].blocking if fcntl fails. */
1175 if (S_ISREG (f[i].mode) && errno == EPERM)
1177 /* This happens when using tail -f on a file with
1178 the append-only attribute. */
1180 else
1181 die (EXIT_FAILURE, errno,
1182 _("%s: cannot change nonblocking mode"),
1183 quotef (name));
1185 else
1186 f[i].blocking = blocking;
1189 if (!f[i].blocking)
1191 if (fstat (fd, &stats) != 0)
1193 f[i].fd = -1;
1194 f[i].errnum = errno;
1195 error (0, errno, "%s", quotef (name));
1196 close (fd); /* ignore failure */
1197 continue;
1200 if (f[i].mode == stats.st_mode
1201 && (! S_ISREG (stats.st_mode) || f[i].size == stats.st_size)
1202 && timespec_cmp (f[i].mtime, get_stat_mtime (&stats)) == 0)
1204 if ((max_n_unchanged_stats_between_opens
1205 <= f[i].n_unchanged_stats++)
1206 && follow_mode == Follow_name)
1208 recheck (&f[i], f[i].blocking);
1209 f[i].n_unchanged_stats = 0;
1211 continue;
1214 /* This file has changed. Print out what we can, and
1215 then keep looping. */
1217 f[i].mtime = get_stat_mtime (&stats);
1218 f[i].mode = stats.st_mode;
1220 /* reset counter */
1221 f[i].n_unchanged_stats = 0;
1223 /* XXX: This is only a heuristic, as the file may have also
1224 been truncated and written to if st_size >= size
1225 (in which case we ignore new data <= size). */
1226 if (S_ISREG (mode) && stats.st_size < f[i].size)
1228 error (0, 0, _("%s: file truncated"), quotef (name));
1229 /* Assume the file was truncated to 0,
1230 and therefore output all "new" data. */
1231 xlseek (fd, 0, SEEK_SET, name);
1232 f[i].size = 0;
1235 if (i != last)
1237 if (print_headers)
1238 write_header (name);
1239 last = i;
1243 /* Don't read more than st_size on networked file systems
1244 because it was seen on glusterfs at least, that st_size
1245 may be smaller than the data read on a _subsequent_ stat call. */
1246 uintmax_t bytes_to_read;
1247 if (f[i].blocking)
1248 bytes_to_read = COPY_A_BUFFER;
1249 else if (S_ISREG (mode) && f[i].remote)
1250 bytes_to_read = stats.st_size - f[i].size;
1251 else
1252 bytes_to_read = COPY_TO_EOF;
1254 bytes_read = dump_remainder (false, name, fd, bytes_to_read);
1256 any_input |= (bytes_read != 0);
1257 f[i].size += bytes_read;
1260 if (! any_live_files (f, n_files))
1262 error (0, 0, _("no files remaining"));
1263 break;
1266 if ((!any_input || blocking) && fflush (stdout) != 0)
1267 die (EXIT_FAILURE, errno, _("write error"));
1269 check_output_alive ();
1271 /* If nothing was read, sleep and/or check for dead writers. */
1272 if (!any_input)
1274 if (writer_is_dead)
1275 break;
1277 /* Once the writer is dead, read the files once more to
1278 avoid a race condition. */
1279 writer_is_dead = (pid != 0
1280 && kill (pid, 0) != 0
1281 /* Handle the case in which you cannot send a
1282 signal to the writer, so kill fails and sets
1283 errno to EPERM. */
1284 && errno != EPERM);
1286 if (!writer_is_dead && xnanosleep (sleep_interval))
1287 die (EXIT_FAILURE, errno, _("cannot read realtime clock"));
1293 #if HAVE_INOTIFY
1295 /* Return true if any of the N_FILES files in F is remote, i.e., has
1296 an open file descriptor and is on a network file system. */
1298 static bool
1299 any_remote_file (const struct File_spec *f, size_t n_files)
1301 for (size_t i = 0; i < n_files; i++)
1302 if (0 <= f[i].fd && f[i].remote)
1303 return true;
1304 return false;
1307 /* Return true if any of the N_FILES files in F is non remote, i.e., has
1308 an open file descriptor and is not on a network file system. */
1310 static bool
1311 any_non_remote_file (const struct File_spec *f, size_t n_files)
1313 for (size_t i = 0; i < n_files; i++)
1314 if (0 <= f[i].fd && ! f[i].remote)
1315 return true;
1316 return false;
1319 /* Return true if any of the N_FILES files in F is a symlink.
1320 Note we don't worry about the edge case where "-" exists,
1321 since that will have the same consequences for inotify,
1322 which is the only context this function is currently used. */
1324 static bool
1325 any_symlinks (const struct File_spec *f, size_t n_files)
1327 struct stat st;
1328 for (size_t i = 0; i < n_files; i++)
1329 if (lstat (f[i].name, &st) == 0 && S_ISLNK (st.st_mode))
1330 return true;
1331 return false;
1334 /* Return true if any of the N_FILES files in F is not
1335 a regular file or fifo. This is used to avoid adding inotify
1336 watches on a device file for example, which inotify
1337 will accept, but not give any events for. */
1339 static bool
1340 any_non_regular_fifo (const struct File_spec *f, size_t n_files)
1342 for (size_t i = 0; i < n_files; i++)
1343 if (0 <= f[i].fd && ! S_ISREG (f[i].mode) && ! S_ISFIFO (f[i].mode))
1344 return true;
1345 return false;
1348 /* Return true if any of the N_FILES files in F represents
1349 stdin and is tailable. */
1351 static bool
1352 tailable_stdin (const struct File_spec *f, size_t n_files)
1354 for (size_t i = 0; i < n_files; i++)
1355 if (!f[i].ignore && STREQ (f[i].name, "-"))
1356 return true;
1357 return false;
1360 static size_t
1361 wd_hasher (const void *entry, size_t tabsize)
1363 const struct File_spec *spec = entry;
1364 return spec->wd % tabsize;
1367 static bool
1368 wd_comparator (const void *e1, const void *e2)
1370 const struct File_spec *spec1 = e1;
1371 const struct File_spec *spec2 = e2;
1372 return spec1->wd == spec2->wd;
1375 /* Output (new) data for FSPEC->fd.
1376 PREV_FSPEC records the last File_spec for which we output. */
1377 static void
1378 check_fspec (struct File_spec *fspec, struct File_spec **prev_fspec)
1380 struct stat stats;
1381 char const *name;
1383 if (fspec->fd == -1)
1384 return;
1386 name = pretty_name (fspec);
1388 if (fstat (fspec->fd, &stats) != 0)
1390 fspec->errnum = errno;
1391 close_fd (fspec->fd, name);
1392 fspec->fd = -1;
1393 return;
1396 /* XXX: This is only a heuristic, as the file may have also
1397 been truncated and written to if st_size >= size
1398 (in which case we ignore new data <= size).
1399 Though in the inotify case it's more likely we'll get
1400 separate events for truncate() and write(). */
1401 if (S_ISREG (fspec->mode) && stats.st_size < fspec->size)
1403 error (0, 0, _("%s: file truncated"), quotef (name));
1404 xlseek (fspec->fd, 0, SEEK_SET, name);
1405 fspec->size = 0;
1407 else if (S_ISREG (fspec->mode) && stats.st_size == fspec->size
1408 && timespec_cmp (fspec->mtime, get_stat_mtime (&stats)) == 0)
1409 return;
1411 bool want_header = print_headers && (fspec != *prev_fspec);
1413 uintmax_t bytes_read = dump_remainder (want_header, name, fspec->fd,
1414 COPY_TO_EOF);
1415 fspec->size += bytes_read;
1417 if (bytes_read)
1419 *prev_fspec = fspec;
1420 if (fflush (stdout) != 0)
1421 die (EXIT_FAILURE, errno, _("write error"));
1425 /* Attempt to tail N_FILES files forever, or until killed.
1426 Check modifications using the inotify events system.
1427 Return false on error, or true to revert to polling. */
1428 static bool
1429 tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
1430 double sleep_interval)
1432 # if TAIL_TEST_SLEEP
1433 /* Delay between open() and inotify_add_watch()
1434 to help trigger different cases. */
1435 xnanosleep (1000000);
1436 # endif
1437 unsigned int max_realloc = 3;
1439 /* Map an inotify watch descriptor to the name of the file it's watching. */
1440 Hash_table *wd_to_name;
1442 bool found_watchable_file = false;
1443 bool tailed_but_unwatchable = false;
1444 bool found_unwatchable_dir = false;
1445 bool no_inotify_resources = false;
1446 bool writer_is_dead = false;
1447 struct File_spec *prev_fspec;
1448 size_t evlen = 0;
1449 char *evbuf;
1450 size_t evbuf_off = 0;
1451 size_t len = 0;
1453 wd_to_name = hash_initialize (n_files, NULL, wd_hasher, wd_comparator, NULL);
1454 if (! wd_to_name)
1455 xalloc_die ();
1457 /* The events mask used with inotify on files (not directories). */
1458 uint32_t inotify_wd_mask = IN_MODIFY;
1459 /* TODO: Perhaps monitor these events in Follow_descriptor mode also,
1460 to tag reported file names with "deleted", "moved" etc. */
1461 if (follow_mode == Follow_name)
1462 inotify_wd_mask |= (IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF);
1464 /* Add an inotify watch for each watched file. If -F is specified then watch
1465 its parent directory too, in this way when they re-appear we can add them
1466 again to the watch list. */
1467 size_t i;
1468 for (i = 0; i < n_files; i++)
1470 if (!f[i].ignore)
1472 size_t fnlen = strlen (f[i].name);
1473 if (evlen < fnlen)
1474 evlen = fnlen;
1476 f[i].wd = -1;
1478 if (follow_mode == Follow_name)
1480 size_t dirlen = dir_len (f[i].name);
1481 char prev = f[i].name[dirlen];
1482 f[i].basename_start = last_component (f[i].name) - f[i].name;
1484 f[i].name[dirlen] = '\0';
1486 /* It's fine to add the same directory more than once.
1487 In that case the same watch descriptor is returned. */
1488 f[i].parent_wd = inotify_add_watch (wd, dirlen ? f[i].name : ".",
1489 (IN_CREATE | IN_DELETE
1490 | IN_MOVED_TO | IN_ATTRIB
1491 | IN_DELETE_SELF));
1493 f[i].name[dirlen] = prev;
1495 if (f[i].parent_wd < 0)
1497 if (errno != ENOSPC) /* suppress confusing error. */
1498 error (0, errno, _("cannot watch parent directory of %s"),
1499 quoteaf (f[i].name));
1500 else
1501 error (0, 0, _("inotify resources exhausted"));
1502 found_unwatchable_dir = true;
1503 /* We revert to polling below. Note invalid uses
1504 of the inotify API will still be diagnosed. */
1505 break;
1509 f[i].wd = inotify_add_watch (wd, f[i].name, inotify_wd_mask);
1511 if (f[i].wd < 0)
1513 if (f[i].fd != -1) /* already tailed. */
1514 tailed_but_unwatchable = true;
1515 if (errno == ENOSPC || errno == ENOMEM)
1517 no_inotify_resources = true;
1518 error (0, 0, _("inotify resources exhausted"));
1519 break;
1521 else if (errno != f[i].errnum)
1522 error (0, errno, _("cannot watch %s"), quoteaf (f[i].name));
1523 continue;
1526 if (hash_insert (wd_to_name, &(f[i])) == NULL)
1527 xalloc_die ();
1529 found_watchable_file = true;
1533 /* Linux kernel 2.6.24 at least has a bug where eventually, ENOSPC is always
1534 returned by inotify_add_watch. In any case we should revert to polling
1535 when there are no inotify resources. Also a specified directory may not
1536 be currently present or accessible, so revert to polling. Also an already
1537 tailed but unwatchable due rename/unlink race, should also revert. */
1538 if (no_inotify_resources || found_unwatchable_dir
1539 || (follow_mode == Follow_descriptor && tailed_but_unwatchable))
1541 hash_free (wd_to_name);
1543 errno = 0;
1544 return true;
1546 if (follow_mode == Follow_descriptor && !found_watchable_file)
1547 return false;
1549 prev_fspec = &(f[n_files - 1]);
1551 /* Check files again. New files or data can be available since last time we
1552 checked and before they are watched by inotify. */
1553 for (i = 0; i < n_files; i++)
1555 if (! f[i].ignore)
1557 /* check for new files. */
1558 if (follow_mode == Follow_name)
1559 recheck (&(f[i]), false);
1560 else if (f[i].fd != -1)
1562 /* If the file was replaced in the small window since we tailed,
1563 then assume the watch is on the wrong item (different to
1564 that we've already produced output for), and so revert to
1565 polling the original descriptor. */
1566 struct stat stats;
1568 if (stat (f[i].name, &stats) == 0
1569 && (f[i].dev != stats.st_dev || f[i].ino != stats.st_ino))
1571 error (0, errno, _("%s was replaced"),
1572 quoteaf (pretty_name (&(f[i]))));
1573 hash_free (wd_to_name);
1575 errno = 0;
1576 return true;
1580 /* check for new data. */
1581 check_fspec (&f[i], &prev_fspec);
1585 evlen += sizeof (struct inotify_event) + 1;
1586 evbuf = xmalloc (evlen);
1588 /* Wait for inotify events and handle them. Events on directories
1589 ensure that watched files can be re-added when following by name.
1590 This loop blocks on the 'safe_read' call until a new event is notified.
1591 But when --pid=P is specified, tail usually waits via the select. */
1592 while (1)
1594 struct File_spec *fspec;
1595 struct inotify_event *ev;
1596 void *void_ev;
1598 /* When following by name without --retry, and the last file has
1599 been unlinked or renamed-away, diagnose it and return. */
1600 if (follow_mode == Follow_name
1601 && ! reopen_inaccessible_files
1602 && hash_get_n_entries (wd_to_name) == 0)
1604 error (0, 0, _("no files remaining"));
1605 return false;
1608 /* When watching a PID, ensure that a read from WD will not block
1609 indefinitely. */
1610 while (len <= evbuf_off)
1612 struct timeval delay; /* how long to wait for file changes. */
1614 if (pid)
1616 if (writer_is_dead)
1617 exit (EXIT_SUCCESS);
1619 writer_is_dead = (kill (pid, 0) != 0 && errno != EPERM);
1621 if (writer_is_dead)
1622 delay.tv_sec = delay.tv_usec = 0;
1623 else
1625 delay.tv_sec = (time_t) sleep_interval;
1626 delay.tv_usec = 1000000 * (sleep_interval - delay.tv_sec);
1630 fd_set rfd;
1631 FD_ZERO (&rfd);
1632 FD_SET (wd, &rfd);
1633 if (monitor_output)
1634 FD_SET (STDOUT_FILENO, &rfd);
1636 int file_change = select (MAX (wd, STDOUT_FILENO) + 1,
1637 &rfd, NULL, NULL, pid ? &delay: NULL);
1639 if (file_change == 0)
1640 continue;
1641 else if (file_change == -1)
1642 die (EXIT_FAILURE, errno,
1643 _("error waiting for inotify and output events"));
1644 else if (FD_ISSET (STDOUT_FILENO, &rfd))
1646 /* readable event on STDOUT is equivalent to POLLERR,
1647 and implies an error on output like broken pipe. */
1648 raise (SIGPIPE);
1650 else
1651 break;
1654 if (len <= evbuf_off)
1656 len = safe_read (wd, evbuf, evlen);
1657 evbuf_off = 0;
1659 /* For kernels prior to 2.6.21, read returns 0 when the buffer
1660 is too small. */
1661 if ((len == 0 || (len == SAFE_READ_ERROR && errno == EINVAL))
1662 && max_realloc--)
1664 len = 0;
1665 evlen *= 2;
1666 evbuf = xrealloc (evbuf, evlen);
1667 continue;
1670 if (len == 0 || len == SAFE_READ_ERROR)
1671 die (EXIT_FAILURE, errno, _("error reading inotify event"));
1674 void_ev = evbuf + evbuf_off;
1675 ev = void_ev;
1676 evbuf_off += sizeof (*ev) + ev->len;
1678 /* If a directory is deleted, IN_DELETE_SELF is emitted
1679 with ev->name of length 0.
1680 We need to catch it, otherwise it would wait forever,
1681 as wd for directory becomes inactive. Revert to polling now. */
1682 if ((ev->mask & IN_DELETE_SELF) && ! ev->len)
1684 for (i = 0; i < n_files; i++)
1686 if (ev->wd == f[i].parent_wd)
1688 hash_free (wd_to_name);
1689 error (0, 0,
1690 _("directory containing watched file was removed"));
1691 errno = 0; /* we've already diagnosed enough errno detail. */
1692 return true;
1697 if (ev->len) /* event on ev->name in watched directory. */
1699 size_t j;
1700 for (j = 0; j < n_files; j++)
1702 /* With N=hundreds of frequently-changing files, this O(N^2)
1703 process might be a problem. FIXME: use a hash table? */
1704 if (f[j].parent_wd == ev->wd
1705 && STREQ (ev->name, f[j].name + f[j].basename_start))
1706 break;
1709 /* It is not a watched file. */
1710 if (j == n_files)
1711 continue;
1713 fspec = &(f[j]);
1715 int new_wd = -1;
1716 bool deleting = !! (ev->mask & IN_DELETE);
1718 if (! deleting)
1720 /* Adding the same inode again will look up any existing wd. */
1721 new_wd = inotify_add_watch (wd, f[j].name, inotify_wd_mask);
1724 if (! deleting && new_wd < 0)
1726 if (errno == ENOSPC || errno == ENOMEM)
1728 error (0, 0, _("inotify resources exhausted"));
1729 hash_free (wd_to_name);
1730 errno = 0;
1731 return true; /* revert to polling. */
1733 else
1735 /* Can get ENOENT for a dangling symlink for example. */
1736 error (0, errno, _("cannot watch %s"), quoteaf (f[j].name));
1738 /* We'll continue below after removing the existing watch. */
1741 /* This will be false if only attributes of file change. */
1742 bool new_watch;
1743 new_watch = (! deleting) && (fspec->wd < 0 || new_wd != fspec->wd);
1745 if (new_watch)
1747 if (0 <= fspec->wd)
1749 inotify_rm_watch (wd, fspec->wd);
1750 hash_delete (wd_to_name, fspec);
1753 fspec->wd = new_wd;
1755 if (new_wd == -1)
1756 continue;
1758 /* If the file was moved then inotify will use the source file wd
1759 for the destination file. Make sure the key is not present in
1760 the table. */
1761 struct File_spec *prev = hash_delete (wd_to_name, fspec);
1762 if (prev && prev != fspec)
1764 if (follow_mode == Follow_name)
1765 recheck (prev, false);
1766 prev->wd = -1;
1767 close_fd (prev->fd, pretty_name (prev));
1770 if (hash_insert (wd_to_name, fspec) == NULL)
1771 xalloc_die ();
1774 if (follow_mode == Follow_name)
1775 recheck (fspec, false);
1777 else
1779 struct File_spec key;
1780 key.wd = ev->wd;
1781 fspec = hash_lookup (wd_to_name, &key);
1784 if (! fspec)
1785 continue;
1787 if (ev->mask & (IN_ATTRIB | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF))
1789 /* Note for IN_MOVE_SELF (the file we're watching has
1790 been clobbered via a rename) we leave the watch
1791 in place since it may still be part of the set
1792 of watched names. */
1793 if (ev->mask & IN_DELETE_SELF)
1795 inotify_rm_watch (wd, fspec->wd);
1796 hash_delete (wd_to_name, fspec);
1799 /* Note we get IN_ATTRIB for unlink() as st_nlink decrements.
1800 The usual path is a close() done in recheck() triggers
1801 an IN_DELETE_SELF event as the inode is removed.
1802 However sometimes open() will succeed as even though
1803 st_nlink is decremented, the dentry (cache) is not updated.
1804 Thus we depend on the IN_DELETE event on the directory
1805 to trigger processing for the removed file. */
1807 recheck (fspec, false);
1809 continue;
1811 check_fspec (fspec, &prev_fspec);
1814 #endif
1816 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
1817 Return true if successful. */
1819 static bool
1820 tail_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
1821 uintmax_t *read_pos)
1823 struct stat stats;
1825 if (fstat (fd, &stats))
1827 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1828 return false;
1831 if (from_start)
1833 if ( ! presume_input_pipe
1834 && S_ISREG (stats.st_mode) && n_bytes <= OFF_T_MAX)
1836 xlseek (fd, n_bytes, SEEK_CUR, pretty_filename);
1837 *read_pos += n_bytes;
1839 else
1841 int t = start_bytes (pretty_filename, fd, n_bytes, read_pos);
1842 if (t)
1843 return t < 0;
1845 n_bytes = COPY_TO_EOF;
1847 else
1849 off_t end_pos = ((! presume_input_pipe && usable_st_size (&stats)
1850 && n_bytes <= OFF_T_MAX)
1851 ? stats.st_size : -1);
1852 if (end_pos <= ST_BLKSIZE (stats))
1853 return pipe_bytes (pretty_filename, fd, n_bytes, read_pos);
1854 off_t current_pos = xlseek (fd, 0, SEEK_CUR, pretty_filename);
1855 if (current_pos < end_pos)
1857 off_t bytes_remaining = end_pos - current_pos;
1859 if (n_bytes < bytes_remaining)
1861 current_pos = end_pos - n_bytes;
1862 xlseek (fd, current_pos, SEEK_SET, pretty_filename);
1865 *read_pos = current_pos;
1868 *read_pos += dump_remainder (false, pretty_filename, fd, n_bytes);
1869 return true;
1872 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
1873 Return true if successful. */
1875 static bool
1876 tail_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
1877 uintmax_t *read_pos)
1879 struct stat stats;
1881 if (fstat (fd, &stats))
1883 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1884 return false;
1887 if (from_start)
1889 int t = start_lines (pretty_filename, fd, n_lines, read_pos);
1890 if (t)
1891 return t < 0;
1892 *read_pos += dump_remainder (false, pretty_filename, fd, COPY_TO_EOF);
1894 else
1896 off_t start_pos = -1;
1897 off_t end_pos;
1899 /* Use file_lines only if FD refers to a regular file for
1900 which lseek (... SEEK_END) works. */
1901 if ( ! presume_input_pipe
1902 && S_ISREG (stats.st_mode)
1903 && (start_pos = lseek (fd, 0, SEEK_CUR)) != -1
1904 && start_pos < (end_pos = lseek (fd, 0, SEEK_END)))
1906 *read_pos = end_pos;
1907 if (end_pos != 0
1908 && ! file_lines (pretty_filename, fd, n_lines,
1909 start_pos, end_pos, read_pos))
1910 return false;
1912 else
1914 /* Under very unlikely circumstances, it is possible to reach
1915 this point after positioning the file pointer to end of file
1916 via the 'lseek (...SEEK_END)' above. In that case, reposition
1917 the file pointer back to start_pos before calling pipe_lines. */
1918 if (start_pos != -1)
1919 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
1921 return pipe_lines (pretty_filename, fd, n_lines, read_pos);
1924 return true;
1927 /* Display the last N_UNITS units of file FILENAME, open for reading
1928 via FD. Set *READ_POS to the position of the input stream pointer.
1929 *READ_POS is usually the number of bytes read and corresponds to an
1930 offset from the beginning of a file. However, it may be larger than
1931 OFF_T_MAX (as for an input pipe), and may also be larger than the
1932 number of bytes read (when an input pointer is initially not at
1933 beginning of file), and may be far greater than the number of bytes
1934 actually read for an input file that is seekable.
1935 Return true if successful. */
1937 static bool
1938 tail (const char *filename, int fd, uintmax_t n_units,
1939 uintmax_t *read_pos)
1941 *read_pos = 0;
1942 if (count_lines)
1943 return tail_lines (filename, fd, n_units, read_pos);
1944 else
1945 return tail_bytes (filename, fd, n_units, read_pos);
1948 /* Display the last N_UNITS units of the file described by F.
1949 Return true if successful. */
1951 static bool
1952 tail_file (struct File_spec *f, uintmax_t n_units)
1954 int fd;
1955 bool ok;
1957 bool is_stdin = (STREQ (f->name, "-"));
1959 if (is_stdin)
1961 have_read_stdin = true;
1962 fd = STDIN_FILENO;
1963 xset_binary_mode (STDIN_FILENO, O_BINARY);
1965 else
1966 fd = open (f->name, O_RDONLY | O_BINARY);
1968 f->tailable = !(reopen_inaccessible_files && fd == -1);
1970 if (fd == -1)
1972 if (forever)
1974 f->fd = -1;
1975 f->errnum = errno;
1976 f->ignore = ! reopen_inaccessible_files;
1977 f->ino = 0;
1978 f->dev = 0;
1980 error (0, errno, _("cannot open %s for reading"),
1981 quoteaf (pretty_name (f)));
1982 ok = false;
1984 else
1986 uintmax_t read_pos;
1988 if (print_headers)
1989 write_header (pretty_name (f));
1990 ok = tail (pretty_name (f), fd, n_units, &read_pos);
1991 if (forever)
1993 struct stat stats;
1995 #if TAIL_TEST_SLEEP
1996 /* Before the tail function provided 'read_pos', there was
1997 a race condition described in the URL below. This sleep
1998 call made the window big enough to exercise the problem. */
1999 xnanosleep (1);
2000 #endif
2001 f->errnum = ok - 1;
2002 if (fstat (fd, &stats) < 0)
2004 ok = false;
2005 f->errnum = errno;
2006 error (0, errno, _("error reading %s"),
2007 quoteaf (pretty_name (f)));
2009 else if (!IS_TAILABLE_FILE_TYPE (stats.st_mode))
2011 ok = false;
2012 f->errnum = -1;
2013 f->tailable = false;
2014 f->ignore = ! reopen_inaccessible_files;
2015 error (0, 0, _("%s: cannot follow end of this type of file%s"),
2016 quotef (pretty_name (f)),
2017 f->ignore ? _("; giving up on this name") : "");
2020 if (!ok)
2022 f->ignore = ! reopen_inaccessible_files;
2023 close_fd (fd, pretty_name (f));
2024 f->fd = -1;
2026 else
2028 /* Note: we must use read_pos here, not stats.st_size,
2029 to avoid a race condition described by Ken Raeburn:
2030 http://lists.gnu.org/archive/html/bug-textutils/2003-05/msg00007.html */
2031 record_open_fd (f, fd, read_pos, &stats, (is_stdin ? -1 : 1));
2032 f->remote = fremote (fd, pretty_name (f));
2035 else
2037 if (!is_stdin && close (fd))
2039 error (0, errno, _("error reading %s"),
2040 quoteaf (pretty_name (f)));
2041 ok = false;
2046 return ok;
2049 /* If obsolete usage is allowed, and the command line arguments are of
2050 the obsolete form and the option string is well-formed, set
2051 *N_UNITS, the globals COUNT_LINES, FOREVER, and FROM_START, and
2052 return true. If the command line arguments are obviously incorrect
2053 (e.g., because obsolete usage is not allowed and the arguments are
2054 incorrect for non-obsolete usage), report an error and exit.
2055 Otherwise, return false and don't modify any parameter or global
2056 variable. */
2058 static bool
2059 parse_obsolete_option (int argc, char * const *argv, uintmax_t *n_units)
2061 const char *p;
2062 const char *n_string;
2063 const char *n_string_end;
2064 int default_count = DEFAULT_N_LINES;
2065 bool t_from_start;
2066 bool t_count_lines = true;
2067 bool t_forever = false;
2069 /* With the obsolete form, there is one option string and at most
2070 one file argument. Watch out for "-" and "--", though. */
2071 if (! (argc == 2
2072 || (argc == 3 && ! (argv[2][0] == '-' && argv[2][1]))
2073 || (3 <= argc && argc <= 4 && STREQ (argv[2], "--"))))
2074 return false;
2076 int posix_ver = posix2_version ();
2077 bool obsolete_usage = posix_ver < 200112;
2078 bool traditional_usage = obsolete_usage || 200809 <= posix_ver;
2079 p = argv[1];
2081 switch (*p++)
2083 default:
2084 return false;
2086 case '+':
2087 /* Leading "+" is a file name in the standard form. */
2088 if (!traditional_usage)
2089 return false;
2091 t_from_start = true;
2092 break;
2094 case '-':
2095 /* In the non-obsolete form, "-" is standard input and "-c"
2096 requires an option-argument. The obsolete multidigit options
2097 are supported as a GNU extension even when conforming to
2098 POSIX 1003.1-2001 or later, so don't complain about them. */
2099 if (!obsolete_usage && !p[p[0] == 'c'])
2100 return false;
2102 t_from_start = false;
2103 break;
2106 n_string = p;
2107 while (ISDIGIT (*p))
2108 p++;
2109 n_string_end = p;
2111 switch (*p)
2113 case 'b': default_count *= 512; FALLTHROUGH;
2114 case 'c': t_count_lines = false; FALLTHROUGH;
2115 case 'l': p++; break;
2118 if (*p == 'f')
2120 t_forever = true;
2121 ++p;
2124 if (*p)
2125 return false;
2127 if (n_string == n_string_end)
2128 *n_units = default_count;
2129 else if ((xstrtoumax (n_string, NULL, 10, n_units, "b")
2130 & ~LONGINT_INVALID_SUFFIX_CHAR)
2131 != LONGINT_OK)
2133 die (EXIT_FAILURE, errno, "%s: %s", _("invalid number"),
2134 quote (argv[1]));
2137 /* Set globals. */
2138 from_start = t_from_start;
2139 count_lines = t_count_lines;
2140 forever = t_forever;
2142 return true;
2145 static void
2146 parse_options (int argc, char **argv,
2147 uintmax_t *n_units, enum header_mode *header_mode,
2148 double *sleep_interval)
2150 int c;
2152 while ((c = getopt_long (argc, argv, "c:n:fFqs:vz0123456789",
2153 long_options, NULL))
2154 != -1)
2156 switch (c)
2158 case 'F':
2159 forever = true;
2160 follow_mode = Follow_name;
2161 reopen_inaccessible_files = true;
2162 break;
2164 case 'c':
2165 case 'n':
2166 count_lines = (c == 'n');
2167 if (*optarg == '+')
2168 from_start = true;
2169 else if (*optarg == '-')
2170 ++optarg;
2172 *n_units = xdectoumax (optarg, 0, UINTMAX_MAX, "bkKmMGTPEZY0",
2173 count_lines
2174 ? _("invalid number of lines")
2175 : _("invalid number of bytes"), 0);
2176 break;
2178 case 'f':
2179 case LONG_FOLLOW_OPTION:
2180 forever = true;
2181 if (optarg == NULL)
2182 follow_mode = DEFAULT_FOLLOW_MODE;
2183 else
2184 follow_mode = XARGMATCH ("--follow", optarg,
2185 follow_mode_string, follow_mode_map);
2186 break;
2188 case RETRY_OPTION:
2189 reopen_inaccessible_files = true;
2190 break;
2192 case MAX_UNCHANGED_STATS_OPTION:
2193 /* --max-unchanged-stats=N */
2194 max_n_unchanged_stats_between_opens =
2195 xdectoumax (optarg, 0, UINTMAX_MAX, "",
2196 _("invalid maximum number of unchanged stats between opens"), 0);
2197 break;
2199 case DISABLE_INOTIFY_OPTION:
2200 disable_inotify = true;
2201 break;
2203 case PID_OPTION:
2204 pid = xdectoumax (optarg, 0, PID_T_MAX, "", _("invalid PID"), 0);
2205 break;
2207 case PRESUME_INPUT_PIPE_OPTION:
2208 presume_input_pipe = true;
2209 break;
2211 case 'q':
2212 *header_mode = never;
2213 break;
2215 case 's':
2217 double s;
2218 if (! (xstrtod (optarg, NULL, &s, c_strtod) && 0 <= s))
2219 die (EXIT_FAILURE, 0,
2220 _("invalid number of seconds: %s"), quote (optarg));
2221 *sleep_interval = s;
2223 break;
2225 case 'v':
2226 *header_mode = always;
2227 break;
2229 case 'z':
2230 line_end = '\0';
2231 break;
2233 case_GETOPT_HELP_CHAR;
2235 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
2237 case '0': case '1': case '2': case '3': case '4':
2238 case '5': case '6': case '7': case '8': case '9':
2239 die (EXIT_FAILURE, 0, _("option used in invalid context -- %c"), c);
2241 default:
2242 usage (EXIT_FAILURE);
2246 if (reopen_inaccessible_files)
2248 if (!forever)
2250 reopen_inaccessible_files = false;
2251 error (0, 0, _("warning: --retry ignored; --retry is useful"
2252 " only when following"));
2254 else if (follow_mode == Follow_descriptor)
2255 error (0, 0, _("warning: --retry only effective for the initial open"));
2258 if (pid && !forever)
2259 error (0, 0,
2260 _("warning: PID ignored; --pid=PID is useful only when following"));
2261 else if (pid && kill (pid, 0) != 0 && errno == ENOSYS)
2263 error (0, 0, _("warning: --pid=PID is not supported on this system"));
2264 pid = 0;
2268 /* Mark as '.ignore'd each member of F that corresponds to a
2269 pipe or fifo, and return the number of non-ignored members. */
2270 static size_t
2271 ignore_fifo_and_pipe (struct File_spec *f, size_t n_files)
2273 /* When there is no FILE operand and stdin is a pipe or FIFO
2274 POSIX requires that tail ignore the -f option.
2275 Since we allow multiple FILE operands, we extend that to say: with -f,
2276 ignore any "-" operand that corresponds to a pipe or FIFO. */
2277 size_t n_viable = 0;
2279 for (size_t i = 0; i < n_files; i++)
2281 bool is_a_fifo_or_pipe =
2282 (STREQ (f[i].name, "-")
2283 && !f[i].ignore
2284 && 0 <= f[i].fd
2285 && (S_ISFIFO (f[i].mode)
2286 || (HAVE_FIFO_PIPES != 1 && isapipe (f[i].fd))));
2287 if (is_a_fifo_or_pipe)
2289 f[i].fd = -1;
2290 f[i].ignore = true;
2292 else
2293 ++n_viable;
2296 return n_viable;
2300 main (int argc, char **argv)
2302 enum header_mode header_mode = multiple_files;
2303 bool ok = true;
2304 /* If from_start, the number of items to skip before printing; otherwise,
2305 the number of items at the end of the file to print. Although the type
2306 is signed, the value is never negative. */
2307 uintmax_t n_units = DEFAULT_N_LINES;
2308 size_t n_files;
2309 char **file;
2310 struct File_spec *F;
2311 size_t i;
2312 bool obsolete_option;
2314 /* The number of seconds to sleep between iterations.
2315 During one iteration, every file name or descriptor is checked to
2316 see if it has changed. */
2317 double sleep_interval = 1.0;
2319 initialize_main (&argc, &argv);
2320 set_program_name (argv[0]);
2321 setlocale (LC_ALL, "");
2322 bindtextdomain (PACKAGE, LOCALEDIR);
2323 textdomain (PACKAGE);
2325 atexit (close_stdout);
2327 have_read_stdin = false;
2329 count_lines = true;
2330 forever = from_start = print_headers = false;
2331 line_end = '\n';
2332 obsolete_option = parse_obsolete_option (argc, argv, &n_units);
2333 argc -= obsolete_option;
2334 argv += obsolete_option;
2335 parse_options (argc, argv, &n_units, &header_mode, &sleep_interval);
2337 /* To start printing with item N_UNITS from the start of the file, skip
2338 N_UNITS - 1 items. 'tail -n +0' is actually meaningless, but for Unix
2339 compatibility it's treated the same as 'tail -n +1'. */
2340 if (from_start)
2342 if (n_units)
2343 --n_units;
2346 IF_LINT (assert (0 <= argc));
2348 if (optind < argc)
2350 n_files = argc - optind;
2351 file = argv + optind;
2353 else
2355 static char *dummy_stdin = (char *) "-";
2356 n_files = 1;
2357 file = &dummy_stdin;
2361 bool found_hyphen = false;
2363 for (i = 0; i < n_files; i++)
2364 if (STREQ (file[i], "-"))
2365 found_hyphen = true;
2367 /* When following by name, there must be a name. */
2368 if (found_hyphen && follow_mode == Follow_name)
2369 die (EXIT_FAILURE, 0, _("cannot follow %s by name"), quoteaf ("-"));
2371 /* When following forever, and not using simple blocking, warn if
2372 any file is '-' as the stats() used to check for input are ineffective.
2373 This is only a warning, since tail's output (before a failing seek,
2374 and that from any non-stdin files) might still be useful. */
2375 if (forever && found_hyphen)
2377 struct stat in_stat;
2378 bool blocking_stdin;
2379 blocking_stdin = (pid == 0 && follow_mode == Follow_descriptor
2380 && n_files == 1 && ! fstat (STDIN_FILENO, &in_stat)
2381 && ! S_ISREG (in_stat.st_mode));
2383 if (! blocking_stdin && isatty (STDIN_FILENO))
2384 error (0, 0, _("warning: following standard input"
2385 " indefinitely is ineffective"));
2389 /* Don't read anything if we'll never output anything. */
2390 if (! n_units && ! forever && ! from_start)
2391 return EXIT_SUCCESS;
2393 F = xnmalloc (n_files, sizeof *F);
2394 for (i = 0; i < n_files; i++)
2395 F[i].name = file[i];
2397 if (header_mode == always
2398 || (header_mode == multiple_files && n_files > 1))
2399 print_headers = true;
2401 xset_binary_mode (STDOUT_FILENO, O_BINARY);
2403 for (i = 0; i < n_files; i++)
2404 ok &= tail_file (&F[i], n_units);
2406 if (forever && ignore_fifo_and_pipe (F, n_files))
2408 /* If stdout is a fifo or pipe, then monitor it
2409 so that we exit if the reader goes away.
2410 Note select() on a regular file is always readable. */
2411 struct stat out_stat;
2412 if (fstat (STDOUT_FILENO, &out_stat) < 0)
2413 die (EXIT_FAILURE, errno, _("standard output"));
2414 monitor_output = (S_ISFIFO (out_stat.st_mode)
2415 || (HAVE_FIFO_PIPES != 1 && isapipe (STDOUT_FILENO)));
2417 #if HAVE_INOTIFY
2418 /* tailable_stdin() checks if the user specifies stdin via "-",
2419 or implicitly by providing no arguments. If so, we won't use inotify.
2420 Technically, on systems with a working /dev/stdin, we *could*,
2421 but would it be worth it? Verifying that it's a real device
2422 and hooked up to stdin is not trivial, while reverting to
2423 non-inotify-based tail_forever is easy and portable.
2425 any_remote_file() checks if the user has specified any
2426 files that reside on remote file systems. inotify is not used
2427 in this case because it would miss any updates to the file
2428 that were not initiated from the local system.
2430 any_non_remote_file() checks if the user has specified any
2431 files that don't reside on remote file systems. inotify is not used
2432 if there are no open files, as we can't determine if those file
2433 will be on a remote file system.
2435 any_symlinks() checks if the user has specified any symbolic links.
2436 inotify is not used in this case because it returns updated _targets_
2437 which would not match the specified names. If we tried to always
2438 use the target names, then we would miss changes to the symlink itself.
2440 ok is false when one of the files specified could not be opened for
2441 reading. In this case and when following by descriptor,
2442 tail_forever_inotify() cannot be used (in its current implementation).
2444 FIXME: inotify doesn't give any notification when a new
2445 (remote) file or directory is mounted on top a watched file.
2446 When follow_mode == Follow_name we would ideally like to detect that.
2447 Note if there is a change to the original file then we'll
2448 recheck it and follow the new file, or ignore it if the
2449 file has changed to being remote.
2451 FIXME: when using inotify, and a directory for a watched file
2452 is recreated, then we don't recheck any new file when
2453 follow_mode == Follow_name.
2455 FIXME-maybe: inotify has a watch descriptor per inode, and hence with
2456 our current hash implementation will only --follow data for one
2457 of the names when multiple hardlinked files are specified, or
2458 for one name when a name is specified multiple times. */
2459 if (!disable_inotify && (tailable_stdin (F, n_files)
2460 || any_remote_file (F, n_files)
2461 || ! any_non_remote_file (F, n_files)
2462 || any_symlinks (F, n_files)
2463 || any_non_regular_fifo (F, n_files)
2464 || (!ok && follow_mode == Follow_descriptor)))
2465 disable_inotify = true;
2467 if (!disable_inotify)
2469 int wd = inotify_init ();
2470 if (0 <= wd)
2472 /* Flush any output from tail_file, now, since
2473 tail_forever_inotify flushes only after writing,
2474 not before reading. */
2475 if (fflush (stdout) != 0)
2476 die (EXIT_FAILURE, errno, _("write error"));
2478 if (! tail_forever_inotify (wd, F, n_files, sleep_interval))
2479 return EXIT_FAILURE;
2481 error (0, errno, _("inotify cannot be used, reverting to polling"));
2483 /* Free resources as this process can be long lived,
2484 and we may have exhausted system resources above. */
2486 for (i = 0; i < n_files; i++)
2488 /* It's OK to remove the same watch multiple times,
2489 ignoring the EINVAL from redundant calls. */
2490 if (F[i].wd != -1)
2491 inotify_rm_watch (wd, F[i].wd);
2492 if (F[i].parent_wd != -1)
2493 inotify_rm_watch (wd, F[i].parent_wd);
2496 #endif
2497 disable_inotify = true;
2498 tail_forever (F, n_files, sleep_interval);
2501 IF_LINT (free (F));
2503 if (have_read_stdin && close (STDIN_FILENO) < 0)
2504 die (EXIT_FAILURE, errno, "-");
2505 return ok ? EXIT_SUCCESS : EXIT_FAILURE;