split: be more careful about buffer sizes
[coreutils.git] / src / tail.c
blob92534a592c8a11c315f44a8df683da88d1fb5559
1 /* tail -- output the last part of file(s)
2 Copyright (C) 1989-2023 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 <https://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 "cl-strtod.h"
37 #include "die.h"
38 #include "error.h"
39 #include "fcntl--.h"
40 #include "iopoll.h"
41 #include "isapipe.h"
42 #include "posixver.h"
43 #include "quote.h"
44 #include "safe-read.h"
45 #include "stat-size.h"
46 #include "stat-time.h"
47 #include "xbinary-io.h"
48 #include "xdectoint.h"
49 #include "xnanosleep.h"
50 #include "xstrtol.h"
51 #include "xstrtod.h"
53 #if HAVE_INOTIFY
54 # include "hash.h"
55 # include <poll.h>
56 # include <sys/inotify.h>
57 #endif
59 /* Linux can optimize the handling of local files. */
60 #if defined __linux__ || defined __ANDROID__
61 # include "fs.h"
62 # include "fs-is-local.h"
63 # if HAVE_SYS_STATFS_H
64 # include <sys/statfs.h>
65 # elif HAVE_SYS_VFS_H
66 # include <sys/vfs.h>
67 # endif
68 #endif
70 /* The official name of this program (e.g., no 'g' prefix). */
71 #define PROGRAM_NAME "tail"
73 #define AUTHORS \
74 proper_name ("Paul Rubin"), \
75 proper_name ("David MacKenzie"), \
76 proper_name ("Ian Lance Taylor"), \
77 proper_name ("Jim Meyering")
79 /* Number of items to tail. */
80 #define DEFAULT_N_LINES 10
82 /* Special values for dump_remainder's N_BYTES parameter. */
83 #define COPY_TO_EOF UINTMAX_MAX
84 #define COPY_A_BUFFER (UINTMAX_MAX - 1)
86 /* FIXME: make Follow_name the default? */
87 #define DEFAULT_FOLLOW_MODE Follow_descriptor
89 enum Follow_mode
91 /* Follow the name of each file: if the file is renamed, try to reopen
92 that name and track the end of the new file if/when it's recreated.
93 This is useful for tracking logs that are occasionally rotated. */
94 Follow_name = 1,
96 /* Follow each descriptor obtained upon opening a file.
97 That means we'll continue to follow the end of a file even after
98 it has been renamed or unlinked. */
99 Follow_descriptor = 2
102 /* The types of files for which tail works. */
103 #define IS_TAILABLE_FILE_TYPE(Mode) \
104 (S_ISREG (Mode) || S_ISFIFO (Mode) || S_ISSOCK (Mode) || S_ISCHR (Mode))
106 static char const *const follow_mode_string[] =
108 "descriptor", "name", NULL
111 static enum Follow_mode const follow_mode_map[] =
113 Follow_descriptor, Follow_name,
116 struct File_spec
118 /* The actual file name, or "-" for stdin. */
119 char *name;
121 /* Attributes of the file the last time we checked. */
122 off_t size;
123 struct timespec mtime;
124 dev_t dev;
125 ino_t ino;
126 mode_t mode;
128 /* The specified name initially referred to a directory or some other
129 type for which tail isn't meaningful. Unlike for a permission problem
130 (tailable, below) once this is set, the name is not checked ever again. */
131 bool ignore;
133 /* See the description of fremote. */
134 bool remote;
136 /* A file is tailable if it exists, is readable, and is of type
137 IS_TAILABLE_FILE_TYPE. */
138 bool tailable;
140 /* File descriptor on which the file is open; -1 if it's not open. */
141 int fd;
143 /* The value of errno seen last time we checked this file. */
144 int errnum;
146 /* 1 if O_NONBLOCK is clear, 0 if set, -1 if not known. */
147 int blocking;
149 #if HAVE_INOTIFY
150 /* The watch descriptor used by inotify. */
151 int wd;
153 /* The parent directory watch descriptor. It is used only
154 * when Follow_name is used. */
155 int parent_wd;
157 /* Offset in NAME of the basename part. */
158 size_t basename_start;
159 #endif
161 /* See description of DEFAULT_MAX_N_... below. */
162 uintmax_t n_unchanged_stats;
165 /* Keep trying to open a file even if it is inaccessible when tail starts
166 or if it becomes inaccessible later -- useful only with -f. */
167 static bool reopen_inaccessible_files;
169 /* If true, interpret the numeric argument as the number of lines.
170 Otherwise, interpret it as the number of bytes. */
171 static bool count_lines;
173 /* Whether we follow the name of each file or the file descriptor
174 that is initially associated with each name. */
175 static enum Follow_mode follow_mode = Follow_descriptor;
177 /* If true, read from the ends of all specified files until killed. */
178 static bool forever;
180 /* If true, monitor output so we exit if pipe reader terminates. */
181 static bool monitor_output;
183 /* If true, count from start of file instead of end. */
184 static bool from_start;
186 /* If true, print filename headers. */
187 static bool print_headers;
189 /* Character to split lines by. */
190 static char line_end;
192 /* When to print the filename banners. */
193 enum header_mode
195 multiple_files, always, never
198 /* When tailing a file by name, if there have been this many consecutive
199 iterations for which the file has not changed, then open/fstat
200 the file to determine if that file name is still associated with the
201 same device/inode-number pair as before. This option is meaningful only
202 when following by name. --max-unchanged-stats=N */
203 #define DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS 5
204 static uintmax_t max_n_unchanged_stats_between_opens =
205 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS;
207 /* The process ID of the process (presumably on the current host)
208 that is writing to all followed files. */
209 static pid_t pid;
211 /* True if we have ever read standard input. */
212 static bool have_read_stdin;
214 /* If nonzero, skip the is-regular-file test used to determine whether
215 to use the lseek optimization. Instead, use the more general (and
216 more expensive) code unconditionally. Intended solely for testing. */
217 static bool presume_input_pipe;
219 /* If nonzero then don't use inotify even if available. */
220 static bool disable_inotify;
222 /* For long options that have no equivalent short option, use a
223 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
224 enum
226 RETRY_OPTION = CHAR_MAX + 1,
227 MAX_UNCHANGED_STATS_OPTION,
228 PID_OPTION,
229 PRESUME_INPUT_PIPE_OPTION,
230 LONG_FOLLOW_OPTION,
231 DISABLE_INOTIFY_OPTION
234 static struct option const long_options[] =
236 {"bytes", required_argument, NULL, 'c'},
237 {"follow", optional_argument, NULL, LONG_FOLLOW_OPTION},
238 {"lines", required_argument, NULL, 'n'},
239 {"max-unchanged-stats", required_argument, NULL, MAX_UNCHANGED_STATS_OPTION},
240 {"-disable-inotify", no_argument, NULL,
241 DISABLE_INOTIFY_OPTION}, /* do not document */
242 {"pid", required_argument, NULL, PID_OPTION},
243 {"-presume-input-pipe", no_argument, NULL,
244 PRESUME_INPUT_PIPE_OPTION}, /* do not document */
245 {"quiet", no_argument, NULL, 'q'},
246 {"retry", no_argument, NULL, RETRY_OPTION},
247 {"silent", no_argument, NULL, 'q'},
248 {"sleep-interval", required_argument, NULL, 's'},
249 {"verbose", no_argument, NULL, 'v'},
250 {"zero-terminated", no_argument, NULL, 'z'},
251 {GETOPT_HELP_OPTION_DECL},
252 {GETOPT_VERSION_OPTION_DECL},
253 {NULL, 0, NULL, 0}
256 void
257 usage (int status)
259 if (status != EXIT_SUCCESS)
260 emit_try_help ();
261 else
263 printf (_("\
264 Usage: %s [OPTION]... [FILE]...\n\
266 program_name);
267 printf (_("\
268 Print the last %d lines of each FILE to standard output.\n\
269 With more than one FILE, precede each with a header giving the file name.\n\
270 "), DEFAULT_N_LINES);
272 emit_stdin_note ();
273 emit_mandatory_arg_note ();
275 fputs (_("\
276 -c, --bytes=[+]NUM output the last NUM bytes; or use -c +NUM to\n\
277 output starting with byte NUM of each file\n\
278 "), stdout);
279 fputs (_("\
280 -f, --follow[={name|descriptor}]\n\
281 output appended data as the file grows;\n\
282 an absent option argument means 'descriptor'\n\
283 -F same as --follow=name --retry\n\
284 "), stdout);
285 printf (_("\
286 -n, --lines=[+]NUM output the last NUM lines, instead of the last %d;\n\
287 or use -n +NUM to output starting with line NUM\n\
288 --max-unchanged-stats=N\n\
289 with --follow=name, reopen a FILE which has not\n\
290 changed size after N (default %d) iterations\n\
291 to see if it has been unlinked or renamed\n\
292 (this is the usual case of rotated log files);\n\
293 with inotify, this option is rarely useful\n\
295 DEFAULT_N_LINES,
296 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS
298 fputs (_("\
299 --pid=PID with -f, terminate after process ID, PID dies\n\
300 -q, --quiet, --silent never output headers giving file names\n\
301 --retry keep trying to open a file if it is inaccessible\n\
302 "), stdout);
303 fputs (_("\
304 -s, --sleep-interval=N with -f, sleep for approximately N seconds\n\
305 (default 1.0) between iterations;\n\
306 with inotify and --pid=P, check process P at\n\
307 least once every N seconds\n\
308 -v, --verbose always output headers giving file names\n\
309 "), stdout);
310 fputs (_("\
311 -z, --zero-terminated line delimiter is NUL, not newline\n\
312 "), stdout);
313 fputs (HELP_OPTION_DESCRIPTION, stdout);
314 fputs (VERSION_OPTION_DESCRIPTION, stdout);
315 fputs (_("\
317 NUM may have a multiplier suffix:\n\
318 b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\
319 GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y, R, Q.\n\
320 Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\
322 "), stdout);
323 fputs (_("\
324 With --follow (-f), tail defaults to following the file descriptor, which\n\
325 means that even if a tail'ed file is renamed, tail will continue to track\n\
326 its end. This default behavior is not desirable when you really want to\n\
327 track the actual name of the file, not the file descriptor (e.g., log\n\
328 rotation). Use --follow=name in that case. That causes tail to track the\n\
329 named file in a way that accommodates renaming, removal and creation.\n\
330 "), stdout);
331 emit_ancillary_info (PROGRAM_NAME);
333 exit (status);
336 /* Ensure exit, either with SIGPIPE or EXIT_FAILURE status. */
337 static void
338 die_pipe (void)
340 raise (SIGPIPE);
341 exit (EXIT_FAILURE);
344 /* If the output has gone away, then terminate
345 as we would if we had written to this output. */
346 static void
347 check_output_alive (void)
349 if (! monitor_output)
350 return;
352 if (iopoll (-1, STDOUT_FILENO, false) == IOPOLL_BROKEN_OUTPUT)
353 die_pipe ();
356 static bool
357 valid_file_spec (struct File_spec const *f)
359 /* Exactly one of the following subexpressions must be true. */
360 return ((f->fd == -1) ^ (f->errnum == 0));
363 static char const *
364 pretty_name (struct File_spec const *f)
366 return (STREQ (f->name, "-") ? _("standard input") : f->name);
369 /* Record a file F with descriptor FD, size SIZE, status ST, and
370 blocking status BLOCKING. */
372 static void
373 record_open_fd (struct File_spec *f, int fd,
374 off_t size, struct stat const *st,
375 int blocking)
377 f->fd = fd;
378 f->size = size;
379 f->mtime = get_stat_mtime (st);
380 f->dev = st->st_dev;
381 f->ino = st->st_ino;
382 f->mode = st->st_mode;
383 f->blocking = blocking;
384 f->n_unchanged_stats = 0;
385 f->ignore = false;
388 /* Close the file with descriptor FD and name FILENAME. */
390 static void
391 close_fd (int fd, char const *filename)
393 if (fd != -1 && fd != STDIN_FILENO && close (fd))
395 error (0, errno, _("closing %s (fd=%d)"), quoteaf (filename), fd);
399 static void
400 write_header (char const *pretty_filename)
402 static bool first_file = true;
404 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), pretty_filename);
405 first_file = false;
408 /* Write N_BYTES from BUFFER to stdout.
409 Exit immediately on error with a single diagnostic. */
411 static void
412 xwrite_stdout (char const *buffer, size_t n_bytes)
414 if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) < n_bytes)
416 clearerr (stdout); /* To avoid redundant close_stdout diagnostic. */
417 die (EXIT_FAILURE, errno, _("error writing %s"),
418 quoteaf ("standard output"));
422 /* Read and output N_BYTES of file PRETTY_FILENAME starting at the current
423 position in FD. If N_BYTES is COPY_TO_EOF, then copy until end of file.
424 If N_BYTES is COPY_A_BUFFER, then copy at most one buffer's worth.
425 Return the number of bytes read from the file. */
427 static uintmax_t
428 dump_remainder (bool want_header, char const *pretty_filename, int fd,
429 uintmax_t n_bytes)
431 uintmax_t n_written;
432 uintmax_t n_remaining = n_bytes;
434 n_written = 0;
435 while (true)
437 char buffer[BUFSIZ];
438 size_t n = MIN (n_remaining, BUFSIZ);
439 size_t bytes_read = safe_read (fd, buffer, n);
440 if (bytes_read == SAFE_READ_ERROR)
442 if (errno != EAGAIN)
443 die (EXIT_FAILURE, errno, _("error reading %s"),
444 quoteaf (pretty_filename));
445 break;
447 if (bytes_read == 0)
448 break;
449 if (want_header)
451 write_header (pretty_filename);
452 want_header = false;
454 xwrite_stdout (buffer, bytes_read);
455 n_written += bytes_read;
456 if (n_bytes != COPY_TO_EOF)
458 n_remaining -= bytes_read;
459 if (n_remaining == 0 || n_bytes == COPY_A_BUFFER)
460 break;
464 return n_written;
467 /* Call lseek with the specified arguments, where file descriptor FD
468 corresponds to the file, FILENAME.
469 Give a diagnostic and exit nonzero if lseek fails.
470 Otherwise, return the resulting offset. */
472 static off_t
473 xlseek (int fd, off_t offset, int whence, char const *filename)
475 off_t new_offset = lseek (fd, offset, whence);
476 char buf[INT_BUFSIZE_BOUND (offset)];
477 char *s;
479 if (0 <= new_offset)
480 return new_offset;
482 s = offtostr (offset, buf);
483 switch (whence)
485 case SEEK_SET:
486 error (0, errno, _("%s: cannot seek to offset %s"),
487 quotef (filename), s);
488 break;
489 case SEEK_CUR:
490 error (0, errno, _("%s: cannot seek to relative offset %s"),
491 quotef (filename), s);
492 break;
493 case SEEK_END:
494 error (0, errno, _("%s: cannot seek to end-relative offset %s"),
495 quotef (filename), s);
496 break;
497 default:
498 abort ();
501 exit (EXIT_FAILURE);
504 /* Print the last N_LINES lines from the end of file FD.
505 Go backward through the file, reading 'BUFSIZ' bytes at a time (except
506 probably the first), until we hit the start of the file or have
507 read NUMBER newlines.
508 START_POS is the starting position of the read pointer for the file
509 associated with FD (may be nonzero).
510 END_POS is the file offset of EOF (one larger than offset of last byte).
511 Return true if successful. */
513 static bool
514 file_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
515 off_t start_pos, off_t end_pos, uintmax_t *read_pos)
517 char buffer[BUFSIZ];
518 size_t bytes_read;
519 off_t pos = end_pos;
521 if (n_lines == 0)
522 return true;
524 /* Set 'bytes_read' to the size of the last, probably partial, buffer;
525 0 < 'bytes_read' <= 'BUFSIZ'. */
526 bytes_read = (pos - start_pos) % BUFSIZ;
527 if (bytes_read == 0)
528 bytes_read = BUFSIZ;
529 /* Make 'pos' a multiple of 'BUFSIZ' (0 if the file is short), so that all
530 reads will be on block boundaries, which might increase efficiency. */
531 pos -= bytes_read;
532 xlseek (fd, pos, SEEK_SET, pretty_filename);
533 bytes_read = safe_read (fd, buffer, bytes_read);
534 if (bytes_read == SAFE_READ_ERROR)
536 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
537 return false;
539 *read_pos = pos + bytes_read;
541 /* Count the incomplete line on files that don't end with a newline. */
542 if (bytes_read && buffer[bytes_read - 1] != line_end)
543 --n_lines;
547 /* Scan backward, counting the newlines in this bufferfull. */
549 size_t n = bytes_read;
550 while (n)
552 char const *nl;
553 nl = memrchr (buffer, line_end, n);
554 if (nl == NULL)
555 break;
556 n = nl - buffer;
557 if (n_lines-- == 0)
559 /* If this newline isn't the last character in the buffer,
560 output the part that is after it. */
561 xwrite_stdout (nl + 1, bytes_read - (n + 1));
562 *read_pos += dump_remainder (false, pretty_filename, fd,
563 end_pos - (pos + bytes_read));
564 return true;
568 /* Not enough newlines in that bufferfull. */
569 if (pos == start_pos)
571 /* Not enough lines in the file; print everything from
572 start_pos to the end. */
573 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
574 *read_pos = start_pos + dump_remainder (false, pretty_filename, fd,
575 end_pos);
576 return true;
578 pos -= BUFSIZ;
579 xlseek (fd, pos, SEEK_SET, pretty_filename);
581 bytes_read = safe_read (fd, buffer, BUFSIZ);
582 if (bytes_read == SAFE_READ_ERROR)
584 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
585 return false;
588 *read_pos = pos + bytes_read;
590 while (bytes_read > 0);
592 return true;
595 /* Print the last N_LINES lines from the end of the standard input,
596 open for reading as pipe FD.
597 Buffer the text as a linked list of LBUFFERs, adding them as needed.
598 Return true if successful. */
600 static bool
601 pipe_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
602 uintmax_t *read_pos)
604 struct linebuffer
606 char buffer[BUFSIZ];
607 size_t nbytes;
608 size_t nlines;
609 struct linebuffer *next;
611 typedef struct linebuffer LBUFFER;
612 LBUFFER *first, *last, *tmp;
613 size_t total_lines = 0; /* Total number of newlines in all buffers. */
614 bool ok = true;
615 size_t n_read; /* Size in bytes of most recent read */
617 first = last = xmalloc (sizeof (LBUFFER));
618 first->nbytes = first->nlines = 0;
619 first->next = NULL;
620 tmp = xmalloc (sizeof (LBUFFER));
622 /* Input is always read into a fresh buffer. */
623 while (true)
625 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
626 if (n_read == 0 || n_read == SAFE_READ_ERROR)
627 break;
628 tmp->nbytes = n_read;
629 *read_pos += n_read;
630 tmp->nlines = 0;
631 tmp->next = NULL;
633 /* Count the number of newlines just read. */
635 char const *buffer_end = tmp->buffer + n_read;
636 char const *p = tmp->buffer;
637 while ((p = memchr (p, line_end, buffer_end - p)))
639 ++p;
640 ++tmp->nlines;
643 total_lines += tmp->nlines;
645 /* If there is enough room in the last buffer read, just append the new
646 one to it. This is because when reading from a pipe, 'n_read' can
647 often be very small. */
648 if (tmp->nbytes + last->nbytes < BUFSIZ)
650 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
651 last->nbytes += tmp->nbytes;
652 last->nlines += tmp->nlines;
654 else
656 /* If there's not enough room, link the new buffer onto the end of
657 the list, then either free up the oldest buffer for the next
658 read if that would leave enough lines, or else malloc a new one.
659 Some compaction mechanism is possible but probably not
660 worthwhile. */
661 last = last->next = tmp;
662 if (total_lines - first->nlines > n_lines)
664 tmp = first;
665 total_lines -= first->nlines;
666 first = first->next;
668 else
669 tmp = xmalloc (sizeof (LBUFFER));
673 free (tmp);
675 if (n_read == SAFE_READ_ERROR)
677 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
678 ok = false;
679 goto free_lbuffers;
682 /* If the file is empty, then bail out. */
683 if (last->nbytes == 0)
684 goto free_lbuffers;
686 /* This prevents a core dump when the pipe contains no newlines. */
687 if (n_lines == 0)
688 goto free_lbuffers;
690 /* Count the incomplete line on files that don't end with a newline. */
691 if (last->buffer[last->nbytes - 1] != line_end)
693 ++last->nlines;
694 ++total_lines;
697 /* Run through the list, printing lines. First, skip over unneeded
698 buffers. */
699 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
700 total_lines -= tmp->nlines;
702 /* Find the correct beginning, then print the rest of the file. */
704 char const *beg = tmp->buffer;
705 char const *buffer_end = tmp->buffer + tmp->nbytes;
706 if (total_lines > n_lines)
708 /* Skip 'total_lines' - 'n_lines' newlines. We made sure that
709 'total_lines' - 'n_lines' <= 'tmp->nlines'. */
710 size_t j;
711 for (j = total_lines - n_lines; j; --j)
713 beg = rawmemchr (beg, line_end);
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 (char const *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 (true)
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 (char const *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 /* Print extra characters if there are any. */
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 (char const *pretty_filename, int fd, uintmax_t n_lines,
876 uintmax_t *read_pos)
878 if (n_lines == 0)
879 return 0;
881 while (true)
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, char const *name)
917 bool remote = true; /* be conservative (poll by default). */
919 #if HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE \
920 && (defined __linux__ || defined __ANDROID__)
921 struct statfs buf;
922 int err = fstatfs (fd, &buf);
923 if (err != 0)
925 /* On at least linux-2.6.38, fstatfs fails with ENOSYS when FD
926 is open on a pipe. Treat that like a remote file. */
927 if (errno != ENOSYS)
928 error (0, errno, _("cannot determine location of %s. "
929 "reverting to polling"), quoteaf (name));
931 else
933 switch (is_local_fs_type (buf.f_type))
935 case 0:
936 break;
937 case -1:
938 /* Treat unrecognized file systems as "remote", so caller polls.
939 Note README-release has instructions for syncing the internal
940 list with the latest Linux kernel file system constants. */
941 break;
942 case 1:
943 remote = false;
944 break;
945 default:
946 assert (!"unexpected return value from is_local_fs_type");
949 #endif
951 return remote;
954 /* open/fstat F->name and handle changes. */
955 static void
956 recheck (struct File_spec *f, bool blocking)
958 struct stat new_stats;
959 bool ok = true;
960 bool is_stdin = (STREQ (f->name, "-"));
961 bool was_tailable = f->tailable;
962 int prev_errnum = f->errnum;
963 bool new_file;
964 int fd = (is_stdin
965 ? STDIN_FILENO
966 : open (f->name, O_RDONLY | (blocking ? 0 : O_NONBLOCK)));
968 assert (valid_file_spec (f));
970 /* If the open fails because the file doesn't exist,
971 then mark the file as not tailable. */
972 f->tailable = !(reopen_inaccessible_files && fd == -1);
974 if (! disable_inotify && ! lstat (f->name, &new_stats)
975 && S_ISLNK (new_stats.st_mode))
977 /* Diagnose the edge case where a regular file is changed
978 to a symlink. We avoid inotify with symlinks since
979 it's awkward to match between symlink name and target. */
980 ok = false;
981 f->errnum = -1;
982 f->ignore = true;
984 error (0, 0, _("%s has been replaced with an untailable symbolic link"),
985 quoteaf (pretty_name (f)));
987 else if (fd == -1 || fstat (fd, &new_stats) < 0)
989 ok = false;
990 f->errnum = errno;
991 if (!f->tailable)
993 if (was_tailable)
995 /* FIXME-maybe: detect the case in which the file first becomes
996 unreadable (perms), and later becomes readable again and can
997 be seen to be the same file (dev/ino). Otherwise, tail prints
998 the entire contents of the file when it becomes readable. */
999 error (0, f->errnum, _("%s has become inaccessible"),
1000 quoteaf (pretty_name (f)));
1002 else
1004 /* say nothing... it's still not tailable */
1007 else if (prev_errnum != errno)
1008 error (0, errno, "%s", quotef (pretty_name (f)));
1010 else if (!IS_TAILABLE_FILE_TYPE (new_stats.st_mode))
1012 ok = false;
1013 f->errnum = -1;
1014 f->tailable = false;
1015 f->ignore = ! (reopen_inaccessible_files && follow_mode == Follow_name);
1016 if (was_tailable || prev_errnum != f->errnum)
1017 error (0, 0, _("%s has been replaced with an untailable file%s"),
1018 quoteaf (pretty_name (f)),
1019 f->ignore ? _("; giving up on this name") : "");
1021 else if ((f->remote = fremote (fd, pretty_name (f))) && ! disable_inotify)
1023 ok = false;
1024 f->errnum = -1;
1025 error (0, 0, _("%s has been replaced with an untailable remote file"),
1026 quoteaf (pretty_name (f)));
1027 f->ignore = true;
1028 f->remote = true;
1030 else
1032 f->errnum = 0;
1035 new_file = false;
1036 if (!ok)
1038 close_fd (fd, pretty_name (f));
1039 close_fd (f->fd, pretty_name (f));
1040 f->fd = -1;
1042 else if (prev_errnum && prev_errnum != ENOENT)
1044 new_file = true;
1045 assert (f->fd == -1);
1046 error (0, 0, _("%s has become accessible"), quoteaf (pretty_name (f)));
1048 else if (f->fd == -1)
1050 /* A new file even when inodes haven't changed as <dev,inode>
1051 pairs can be reused, and we know the file was missing
1052 on the previous iteration. Note this also means the file
1053 is redisplayed in --follow=name mode if renamed away from
1054 and back to a monitored name. */
1055 new_file = true;
1057 error (0, 0,
1058 _("%s has appeared; following new file"),
1059 quoteaf (pretty_name (f)));
1061 else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
1063 /* File has been replaced (e.g., via log rotation) --
1064 tail the new one. */
1065 new_file = true;
1067 error (0, 0,
1068 _("%s has been replaced; following new file"),
1069 quoteaf (pretty_name (f)));
1071 /* Close the old one. */
1072 close_fd (f->fd, pretty_name (f));
1075 else
1077 /* No changes detected, so close new fd. */
1078 close_fd (fd, pretty_name (f));
1081 /* FIXME: When a log is rotated, daemons tend to log to the
1082 old file descriptor until the new file is present and
1083 the daemon is sent a signal. Therefore tail may miss entries
1084 being written to the old file. Perhaps we should keep
1085 the older file open and continue to monitor it until
1086 data is written to a new file. */
1087 if (new_file)
1089 /* Start at the beginning of the file. */
1090 record_open_fd (f, fd, 0, &new_stats, (is_stdin ? -1 : blocking));
1091 if (S_ISREG (new_stats.st_mode))
1092 xlseek (fd, 0, SEEK_SET, pretty_name (f));
1096 /* Return true if any of the N_FILES files in F are live, i.e., have
1097 open file descriptors, or should be checked again (see --retry).
1098 When following descriptors, checking should only continue when any
1099 of the files is not yet ignored. */
1101 static bool
1102 any_live_files (const struct File_spec *f, size_t n_files)
1104 /* In inotify mode, ignore may be set for files
1105 which may later be replaced with new files.
1106 So always consider files live in -F mode. */
1107 if (reopen_inaccessible_files && follow_mode == Follow_name)
1108 return true;
1110 for (size_t i = 0; i < n_files; i++)
1112 if (0 <= f[i].fd)
1113 return true;
1114 else
1116 if (! f[i].ignore && reopen_inaccessible_files)
1117 return true;
1121 return false;
1124 /* Tail N_FILES files forever, or until killed.
1125 The pertinent information for each file is stored in an entry of F.
1126 Loop over each of them, doing an fstat to see if they have changed size,
1127 and an occasional open/fstat to see if any dev/ino pair has changed.
1128 If none of them have changed size in one iteration, sleep for a
1129 while and try again. Continue until the user interrupts us. */
1131 static void
1132 tail_forever (struct File_spec *f, size_t n_files, double sleep_interval)
1134 /* Use blocking I/O as an optimization, when it's easy. */
1135 bool blocking = (pid == 0 && follow_mode == Follow_descriptor
1136 && n_files == 1 && f[0].fd != -1 && ! S_ISREG (f[0].mode));
1137 size_t last;
1138 bool writer_is_dead = false;
1140 last = n_files - 1;
1142 while (true)
1144 size_t i;
1145 bool any_input = false;
1147 for (i = 0; i < n_files; i++)
1149 int fd;
1150 char const *name;
1151 mode_t mode;
1152 struct stat stats;
1153 uintmax_t bytes_read;
1155 if (f[i].ignore)
1156 continue;
1158 if (f[i].fd < 0)
1160 recheck (&f[i], blocking);
1161 continue;
1164 fd = f[i].fd;
1165 name = pretty_name (&f[i]);
1166 mode = f[i].mode;
1168 if (f[i].blocking != blocking)
1170 int old_flags = fcntl (fd, F_GETFL);
1171 int new_flags = old_flags | (blocking ? 0 : O_NONBLOCK);
1172 if (old_flags < 0
1173 || (new_flags != old_flags
1174 && fcntl (fd, F_SETFL, new_flags) == -1))
1176 /* Don't update f[i].blocking if fcntl fails. */
1177 if (S_ISREG (f[i].mode) && errno == EPERM)
1179 /* This happens when using tail -f on a file with
1180 the append-only attribute. */
1182 else
1183 die (EXIT_FAILURE, errno,
1184 _("%s: cannot change nonblocking mode"),
1185 quotef (name));
1187 else
1188 f[i].blocking = blocking;
1191 bool read_unchanged = false;
1192 if (!f[i].blocking)
1194 if (fstat (fd, &stats) != 0)
1196 f[i].fd = -1;
1197 f[i].errnum = errno;
1198 error (0, errno, "%s", quotef (name));
1199 close (fd); /* ignore failure */
1200 continue;
1203 if (f[i].mode == stats.st_mode
1204 && (! S_ISREG (stats.st_mode) || f[i].size == stats.st_size)
1205 && timespec_cmp (f[i].mtime, get_stat_mtime (&stats)) == 0)
1207 if ((max_n_unchanged_stats_between_opens
1208 <= f[i].n_unchanged_stats++)
1209 && follow_mode == Follow_name)
1211 recheck (&f[i], f[i].blocking);
1212 f[i].n_unchanged_stats = 0;
1214 if (fd != f[i].fd || S_ISREG (stats.st_mode) || 1 < n_files)
1215 continue;
1216 else
1217 read_unchanged = true;
1220 assert (fd == f[i].fd);
1222 /* This file has changed. Print out what we can, and
1223 then keep looping. */
1225 f[i].mtime = get_stat_mtime (&stats);
1226 f[i].mode = stats.st_mode;
1228 /* reset counter */
1229 if (! read_unchanged)
1230 f[i].n_unchanged_stats = 0;
1232 /* XXX: This is only a heuristic, as the file may have also
1233 been truncated and written to if st_size >= size
1234 (in which case we ignore new data <= size). */
1235 if (S_ISREG (mode) && stats.st_size < f[i].size)
1237 error (0, 0, _("%s: file truncated"), quotef (name));
1238 /* Assume the file was truncated to 0,
1239 and therefore output all "new" data. */
1240 xlseek (fd, 0, SEEK_SET, name);
1241 f[i].size = 0;
1244 if (i != last)
1246 if (print_headers)
1247 write_header (name);
1248 last = i;
1252 /* Don't read more than st_size on networked file systems
1253 because it was seen on glusterfs at least, that st_size
1254 may be smaller than the data read on a _subsequent_ stat call. */
1255 uintmax_t bytes_to_read;
1256 if (f[i].blocking)
1257 bytes_to_read = COPY_A_BUFFER;
1258 else if (S_ISREG (mode) && f[i].remote)
1259 bytes_to_read = stats.st_size - f[i].size;
1260 else
1261 bytes_to_read = COPY_TO_EOF;
1263 bytes_read = dump_remainder (false, name, fd, bytes_to_read);
1265 if (read_unchanged && bytes_read)
1266 f[i].n_unchanged_stats = 0;
1268 any_input |= (bytes_read != 0);
1269 f[i].size += bytes_read;
1272 if (! any_live_files (f, n_files))
1274 error (0, 0, _("no files remaining"));
1275 break;
1278 if ((!any_input || blocking) && fflush (stdout) != 0)
1279 die (EXIT_FAILURE, errno, _("write error"));
1281 check_output_alive ();
1283 /* If nothing was read, sleep and/or check for dead writers. */
1284 if (!any_input)
1286 if (writer_is_dead)
1287 break;
1289 /* Once the writer is dead, read the files once more to
1290 avoid a race condition. */
1291 writer_is_dead = (pid != 0
1292 && kill (pid, 0) != 0
1293 /* Handle the case in which you cannot send a
1294 signal to the writer, so kill fails and sets
1295 errno to EPERM. */
1296 && errno != EPERM);
1298 if (!writer_is_dead && xnanosleep (sleep_interval))
1299 die (EXIT_FAILURE, errno, _("cannot read realtime clock"));
1305 #if HAVE_INOTIFY
1307 /* Return true if any of the N_FILES files in F is remote, i.e., has
1308 an open file descriptor and is on a network file system. */
1310 static bool
1311 any_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 non remote, i.e., has
1320 an open file descriptor and is not on a network file system. */
1322 static bool
1323 any_non_remote_file (const struct File_spec *f, size_t n_files)
1325 for (size_t i = 0; i < n_files; i++)
1326 if (0 <= f[i].fd && ! f[i].remote)
1327 return true;
1328 return false;
1331 /* Return true if any of the N_FILES files in F is a symlink.
1332 Note we don't worry about the edge case where "-" exists,
1333 since that will have the same consequences for inotify,
1334 which is the only context this function is currently used. */
1336 static bool
1337 any_symlinks (const struct File_spec *f, size_t n_files)
1339 struct stat st;
1340 for (size_t i = 0; i < n_files; i++)
1341 if (lstat (f[i].name, &st) == 0 && S_ISLNK (st.st_mode))
1342 return true;
1343 return false;
1346 /* Return true if any of the N_FILES files in F is not
1347 a regular file or fifo. This is used to avoid adding inotify
1348 watches on a device file for example, which inotify
1349 will accept, but not give any events for. */
1351 static bool
1352 any_non_regular_fifo (const struct File_spec *f, size_t n_files)
1354 for (size_t i = 0; i < n_files; i++)
1355 if (0 <= f[i].fd && ! S_ISREG (f[i].mode) && ! S_ISFIFO (f[i].mode))
1356 return true;
1357 return false;
1360 /* Return true if any of the N_FILES files in F represents
1361 stdin and is tailable. */
1363 static bool
1364 tailable_stdin (const struct File_spec *f, size_t n_files)
1366 for (size_t i = 0; i < n_files; i++)
1367 if (!f[i].ignore && STREQ (f[i].name, "-"))
1368 return true;
1369 return false;
1372 static size_t
1373 wd_hasher (const void *entry, size_t tabsize)
1375 const struct File_spec *spec = entry;
1376 return spec->wd % tabsize;
1379 static bool
1380 wd_comparator (const void *e1, const void *e2)
1382 const struct File_spec *spec1 = e1;
1383 const struct File_spec *spec2 = e2;
1384 return spec1->wd == spec2->wd;
1387 /* Output (new) data for FSPEC->fd.
1388 PREV_FSPEC records the last File_spec for which we output. */
1389 static void
1390 check_fspec (struct File_spec *fspec, struct File_spec **prev_fspec)
1392 struct stat stats;
1393 char const *name;
1395 if (fspec->fd == -1)
1396 return;
1398 name = pretty_name (fspec);
1400 if (fstat (fspec->fd, &stats) != 0)
1402 fspec->errnum = errno;
1403 close_fd (fspec->fd, name);
1404 fspec->fd = -1;
1405 return;
1408 /* XXX: This is only a heuristic, as the file may have also
1409 been truncated and written to if st_size >= size
1410 (in which case we ignore new data <= size).
1411 Though in the inotify case it's more likely we'll get
1412 separate events for truncate() and write(). */
1413 if (S_ISREG (fspec->mode) && stats.st_size < fspec->size)
1415 error (0, 0, _("%s: file truncated"), quotef (name));
1416 xlseek (fspec->fd, 0, SEEK_SET, name);
1417 fspec->size = 0;
1419 else if (S_ISREG (fspec->mode) && stats.st_size == fspec->size
1420 && timespec_cmp (fspec->mtime, get_stat_mtime (&stats)) == 0)
1421 return;
1423 bool want_header = print_headers && (fspec != *prev_fspec);
1425 uintmax_t bytes_read = dump_remainder (want_header, name, fspec->fd,
1426 COPY_TO_EOF);
1427 fspec->size += bytes_read;
1429 if (bytes_read)
1431 *prev_fspec = fspec;
1432 if (fflush (stdout) != 0)
1433 die (EXIT_FAILURE, errno, _("write error"));
1437 /* Attempt to tail N_FILES files forever, or until killed.
1438 Check modifications using the inotify events system.
1439 Exit if finished or on fatal error; return to revert to polling. */
1440 static void
1441 tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
1442 double sleep_interval, Hash_table **wd_to_namep)
1444 # if TAIL_TEST_SLEEP
1445 /* Delay between open() and inotify_add_watch()
1446 to help trigger different cases. */
1447 xnanosleep (1000000);
1448 # endif
1449 unsigned int max_realloc = 3;
1451 /* Map an inotify watch descriptor to the name of the file it's watching. */
1452 Hash_table *wd_to_name;
1454 bool found_watchable_file = false;
1455 bool tailed_but_unwatchable = false;
1456 bool found_unwatchable_dir = false;
1457 bool no_inotify_resources = false;
1458 bool writer_is_dead = false;
1459 struct File_spec *prev_fspec;
1460 size_t evlen = 0;
1461 char *evbuf;
1462 size_t evbuf_off = 0;
1463 size_t len = 0;
1465 wd_to_name = hash_initialize (n_files, NULL, wd_hasher, wd_comparator, NULL);
1466 if (! wd_to_name)
1467 xalloc_die ();
1468 *wd_to_namep = wd_to_name;
1470 /* The events mask used with inotify on files (not directories). */
1471 uint32_t inotify_wd_mask = IN_MODIFY;
1472 /* TODO: Perhaps monitor these events in Follow_descriptor mode also,
1473 to tag reported file names with "deleted", "moved" etc. */
1474 if (follow_mode == Follow_name)
1475 inotify_wd_mask |= (IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF);
1477 /* Add an inotify watch for each watched file. If -F is specified then watch
1478 its parent directory too, in this way when they re-appear we can add them
1479 again to the watch list. */
1480 size_t i;
1481 for (i = 0; i < n_files; i++)
1483 if (!f[i].ignore)
1485 size_t fnlen = strlen (f[i].name);
1486 if (evlen < fnlen)
1487 evlen = fnlen;
1489 f[i].wd = -1;
1491 if (follow_mode == Follow_name)
1493 size_t dirlen = dir_len (f[i].name);
1494 char prev = f[i].name[dirlen];
1495 f[i].basename_start = last_component (f[i].name) - f[i].name;
1497 f[i].name[dirlen] = '\0';
1499 /* It's fine to add the same directory more than once.
1500 In that case the same watch descriptor is returned. */
1501 f[i].parent_wd = inotify_add_watch (wd, dirlen ? f[i].name : ".",
1502 (IN_CREATE | IN_DELETE
1503 | IN_MOVED_TO | IN_ATTRIB
1504 | IN_DELETE_SELF));
1506 f[i].name[dirlen] = prev;
1508 if (f[i].parent_wd < 0)
1510 if (errno != ENOSPC) /* suppress confusing error. */
1511 error (0, errno, _("cannot watch parent directory of %s"),
1512 quoteaf (f[i].name));
1513 else
1514 error (0, 0, _("inotify resources exhausted"));
1515 found_unwatchable_dir = true;
1516 /* We revert to polling below. Note invalid uses
1517 of the inotify API will still be diagnosed. */
1518 break;
1522 f[i].wd = inotify_add_watch (wd, f[i].name, inotify_wd_mask);
1524 if (f[i].wd < 0)
1526 if (f[i].fd != -1) /* already tailed. */
1527 tailed_but_unwatchable = true;
1528 if (errno == ENOSPC || errno == ENOMEM)
1530 no_inotify_resources = true;
1531 error (0, 0, _("inotify resources exhausted"));
1532 break;
1534 else if (errno != f[i].errnum)
1535 error (0, errno, _("cannot watch %s"), quoteaf (f[i].name));
1536 continue;
1539 if (hash_insert (wd_to_name, &(f[i])) == NULL)
1540 xalloc_die ();
1542 found_watchable_file = true;
1546 /* Linux kernel 2.6.24 at least has a bug where eventually, ENOSPC is always
1547 returned by inotify_add_watch. In any case we should revert to polling
1548 when there are no inotify resources. Also a specified directory may not
1549 be currently present or accessible, so revert to polling. Also an already
1550 tailed but unwatchable due rename/unlink race, should also revert. */
1551 if (no_inotify_resources || found_unwatchable_dir
1552 || (follow_mode == Follow_descriptor && tailed_but_unwatchable))
1553 return;
1554 if (follow_mode == Follow_descriptor && !found_watchable_file)
1555 exit (EXIT_FAILURE);
1557 prev_fspec = &(f[n_files - 1]);
1559 /* Check files again. New files or data can be available since last time we
1560 checked and before they are watched by inotify. */
1561 for (i = 0; i < n_files; i++)
1563 if (! f[i].ignore)
1565 /* check for new files. */
1566 if (follow_mode == Follow_name)
1567 recheck (&(f[i]), false);
1568 else if (f[i].fd != -1)
1570 /* If the file was replaced in the small window since we tailed,
1571 then assume the watch is on the wrong item (different to
1572 that we've already produced output for), and so revert to
1573 polling the original descriptor. */
1574 struct stat stats;
1576 if (stat (f[i].name, &stats) == 0
1577 && (f[i].dev != stats.st_dev || f[i].ino != stats.st_ino))
1579 error (0, errno, _("%s was replaced"),
1580 quoteaf (pretty_name (&(f[i]))));
1581 return;
1585 /* check for new data. */
1586 check_fspec (&f[i], &prev_fspec);
1590 evlen += sizeof (struct inotify_event) + 1;
1591 evbuf = xmalloc (evlen);
1593 /* Wait for inotify events and handle them. Events on directories
1594 ensure that watched files can be re-added when following by name.
1595 This loop blocks on the 'safe_read' call until a new event is notified.
1596 But when --pid=P is specified, tail usually waits via poll. */
1597 while (true)
1599 struct File_spec *fspec;
1600 struct inotify_event *ev;
1601 void *void_ev;
1603 /* When following by name without --retry, and the last file has
1604 been unlinked or renamed-away, diagnose it and return. */
1605 if (follow_mode == Follow_name
1606 && ! reopen_inaccessible_files
1607 && hash_get_n_entries (wd_to_name) == 0)
1608 die (EXIT_FAILURE, 0, _("no files remaining"));
1610 if (len <= evbuf_off)
1612 /* Poll for inotify events. When watching a PID, ensure
1613 that a read from WD will not block indefinitely.
1614 If MONITOR_OUTPUT, also poll for a broken output pipe. */
1616 int file_change;
1617 struct pollfd pfd[2];
1620 /* How many ms to wait for changes. -1 means wait forever. */
1621 int delay = -1;
1623 if (pid)
1625 if (writer_is_dead)
1626 exit (EXIT_SUCCESS);
1628 writer_is_dead = (kill (pid, 0) != 0 && errno != EPERM);
1630 if (writer_is_dead || sleep_interval <= 0)
1631 delay = 0;
1632 else if (sleep_interval < INT_MAX / 1000 - 1)
1634 /* delay = ceil (sleep_interval * 1000), sans libm. */
1635 double ddelay = sleep_interval * 1000;
1636 delay = ddelay;
1637 delay += delay < ddelay;
1641 pfd[0].fd = wd;
1642 pfd[0].events = POLLIN;
1643 pfd[1].fd = STDOUT_FILENO;
1644 pfd[1].events = pfd[1].revents = 0;
1645 file_change = poll (pfd, monitor_output + 1, delay);
1647 while (file_change == 0);
1649 if (file_change < 0)
1650 die (EXIT_FAILURE, errno,
1651 _("error waiting for inotify and output events"));
1652 if (pfd[1].revents)
1653 die_pipe ();
1655 len = safe_read (wd, evbuf, evlen);
1656 evbuf_off = 0;
1658 /* For kernels prior to 2.6.21, read returns 0 when the buffer
1659 is too small. */
1660 if ((len == 0 || (len == SAFE_READ_ERROR && errno == EINVAL))
1661 && max_realloc--)
1663 len = 0;
1664 evlen *= 2;
1665 evbuf = xrealloc (evbuf, evlen);
1666 continue;
1669 if (len == 0 || len == SAFE_READ_ERROR)
1670 die (EXIT_FAILURE, errno, _("error reading inotify event"));
1673 void_ev = evbuf + evbuf_off;
1674 ev = void_ev;
1675 evbuf_off += sizeof (*ev) + ev->len;
1677 /* If a directory is deleted, IN_DELETE_SELF is emitted
1678 with ev->name of length 0.
1679 We need to catch it, otherwise it would wait forever,
1680 as wd for directory becomes inactive. Revert to polling now. */
1681 if ((ev->mask & IN_DELETE_SELF) && ! ev->len)
1683 for (i = 0; i < n_files; i++)
1685 if (ev->wd == f[i].parent_wd)
1687 error (0, 0,
1688 _("directory containing watched file was removed"));
1689 return;
1694 if (ev->len) /* event on ev->name in watched directory. */
1696 size_t j;
1697 for (j = 0; j < n_files; j++)
1699 /* With N=hundreds of frequently-changing files, this O(N^2)
1700 process might be a problem. FIXME: use a hash table? */
1701 if (f[j].parent_wd == ev->wd
1702 && STREQ (ev->name, f[j].name + f[j].basename_start))
1703 break;
1706 /* It is not a watched file. */
1707 if (j == n_files)
1708 continue;
1710 fspec = &(f[j]);
1712 int new_wd = -1;
1713 bool deleting = !! (ev->mask & IN_DELETE);
1715 if (! deleting)
1717 /* Adding the same inode again will look up any existing wd. */
1718 new_wd = inotify_add_watch (wd, f[j].name, inotify_wd_mask);
1721 if (! deleting && new_wd < 0)
1723 if (errno == ENOSPC || errno == ENOMEM)
1725 error (0, 0, _("inotify resources exhausted"));
1726 return; /* revert to polling. */
1728 else
1730 /* Can get ENOENT for a dangling symlink for example. */
1731 error (0, errno, _("cannot watch %s"), quoteaf (f[j].name));
1733 /* We'll continue below after removing the existing watch. */
1736 /* This will be false if only attributes of file change. */
1737 bool new_watch;
1738 new_watch = (! deleting) && (fspec->wd < 0 || new_wd != fspec->wd);
1740 if (new_watch)
1742 if (0 <= fspec->wd)
1744 inotify_rm_watch (wd, fspec->wd);
1745 hash_remove (wd_to_name, fspec);
1748 fspec->wd = new_wd;
1750 if (new_wd == -1)
1751 continue;
1753 /* If the file was moved then inotify will use the source file wd
1754 for the destination file. Make sure the key is not present in
1755 the table. */
1756 struct File_spec *prev = hash_remove (wd_to_name, fspec);
1757 if (prev && prev != fspec)
1759 if (follow_mode == Follow_name)
1760 recheck (prev, false);
1761 prev->wd = -1;
1762 close_fd (prev->fd, pretty_name (prev));
1765 if (hash_insert (wd_to_name, fspec) == NULL)
1766 xalloc_die ();
1769 if (follow_mode == Follow_name)
1770 recheck (fspec, false);
1772 else
1774 struct File_spec key;
1775 key.wd = ev->wd;
1776 fspec = hash_lookup (wd_to_name, &key);
1779 if (! fspec)
1780 continue;
1782 if (ev->mask & (IN_ATTRIB | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF))
1784 /* Note for IN_MOVE_SELF (the file we're watching has
1785 been clobbered via a rename) we leave the watch
1786 in place since it may still be part of the set
1787 of watched names. */
1788 if (ev->mask & IN_DELETE_SELF)
1790 inotify_rm_watch (wd, fspec->wd);
1791 hash_remove (wd_to_name, fspec);
1794 /* Note we get IN_ATTRIB for unlink() as st_nlink decrements.
1795 The usual path is a close() done in recheck() triggers
1796 an IN_DELETE_SELF event as the inode is removed.
1797 However sometimes open() will succeed as even though
1798 st_nlink is decremented, the dentry (cache) is not updated.
1799 Thus we depend on the IN_DELETE event on the directory
1800 to trigger processing for the removed file. */
1802 recheck (fspec, false);
1804 continue;
1806 check_fspec (fspec, &prev_fspec);
1809 #endif
1811 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
1812 Return true if successful. */
1814 static bool
1815 tail_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
1816 uintmax_t *read_pos)
1818 struct stat stats;
1820 if (fstat (fd, &stats))
1822 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1823 return false;
1826 if (from_start)
1828 if (! presume_input_pipe && n_bytes <= OFF_T_MAX
1829 && ((S_ISREG (stats.st_mode)
1830 && xlseek (fd, n_bytes, SEEK_CUR, pretty_filename) >= 0)
1831 || lseek (fd, n_bytes, SEEK_CUR) != -1))
1832 *read_pos += n_bytes;
1833 else
1835 int t = start_bytes (pretty_filename, fd, n_bytes, read_pos);
1836 if (t)
1837 return t < 0;
1839 n_bytes = COPY_TO_EOF;
1841 else
1843 off_t end_pos = -1;
1844 off_t current_pos = -1;
1846 if (! presume_input_pipe && n_bytes <= OFF_T_MAX)
1848 if (usable_st_size (&stats))
1849 end_pos = stats.st_size;
1850 else if ((current_pos = lseek (fd, -n_bytes, SEEK_END)) != -1)
1851 end_pos = current_pos + n_bytes;
1853 if (end_pos <= (off_t) ST_BLKSIZE (stats))
1854 return pipe_bytes (pretty_filename, fd, n_bytes, read_pos);
1855 if (current_pos == -1)
1856 current_pos = xlseek (fd, 0, SEEK_CUR, pretty_filename);
1857 if (current_pos < end_pos)
1859 off_t bytes_remaining = end_pos - current_pos;
1861 if (n_bytes < bytes_remaining)
1863 current_pos = end_pos - n_bytes;
1864 xlseek (fd, current_pos, SEEK_SET, pretty_filename);
1867 *read_pos = current_pos;
1870 *read_pos += dump_remainder (false, pretty_filename, fd, n_bytes);
1871 return true;
1874 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
1875 Return true if successful. */
1877 static bool
1878 tail_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
1879 uintmax_t *read_pos)
1881 struct stat stats;
1883 if (fstat (fd, &stats))
1885 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1886 return false;
1889 if (from_start)
1891 int t = start_lines (pretty_filename, fd, n_lines, read_pos);
1892 if (t)
1893 return t < 0;
1894 *read_pos += dump_remainder (false, pretty_filename, fd, COPY_TO_EOF);
1896 else
1898 off_t start_pos = -1;
1899 off_t end_pos;
1901 /* Use file_lines only if FD refers to a regular file for
1902 which lseek (... SEEK_END) works. */
1903 if ( ! presume_input_pipe
1904 && S_ISREG (stats.st_mode)
1905 && (start_pos = lseek (fd, 0, SEEK_CUR)) != -1
1906 && start_pos < (end_pos = lseek (fd, 0, SEEK_END)))
1908 *read_pos = end_pos;
1909 if (end_pos != 0
1910 && ! file_lines (pretty_filename, fd, n_lines,
1911 start_pos, end_pos, read_pos))
1912 return false;
1914 else
1916 /* Under very unlikely circumstances, it is possible to reach
1917 this point after positioning the file pointer to end of file
1918 via the 'lseek (...SEEK_END)' above. In that case, reposition
1919 the file pointer back to start_pos before calling pipe_lines. */
1920 if (start_pos != -1)
1921 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
1923 return pipe_lines (pretty_filename, fd, n_lines, read_pos);
1926 return true;
1929 /* Display the last N_UNITS units of file FILENAME, open for reading
1930 via FD. Set *READ_POS to the position of the input stream pointer.
1931 *READ_POS is usually the number of bytes read and corresponds to an
1932 offset from the beginning of a file. However, it may be larger than
1933 OFF_T_MAX (as for an input pipe), and may also be larger than the
1934 number of bytes read (when an input pointer is initially not at
1935 beginning of file), and may be far greater than the number of bytes
1936 actually read for an input file that is seekable.
1937 Return true if successful. */
1939 static bool
1940 tail (char const *filename, int fd, uintmax_t n_units,
1941 uintmax_t *read_pos)
1943 *read_pos = 0;
1944 if (count_lines)
1945 return tail_lines (filename, fd, n_units, read_pos);
1946 else
1947 return tail_bytes (filename, fd, n_units, read_pos);
1950 /* Display the last N_UNITS units of the file described by F.
1951 Return true if successful. */
1953 static bool
1954 tail_file (struct File_spec *f, uintmax_t n_units)
1956 int fd;
1957 bool ok;
1959 bool is_stdin = (STREQ (f->name, "-"));
1961 if (is_stdin)
1963 have_read_stdin = true;
1964 fd = STDIN_FILENO;
1965 xset_binary_mode (STDIN_FILENO, O_BINARY);
1967 else
1968 fd = open (f->name, O_RDONLY | O_BINARY);
1970 f->tailable = !(reopen_inaccessible_files && fd == -1);
1972 if (fd == -1)
1974 if (forever)
1976 f->fd = -1;
1977 f->errnum = errno;
1978 f->ignore = ! reopen_inaccessible_files;
1979 f->ino = 0;
1980 f->dev = 0;
1982 error (0, errno, _("cannot open %s for reading"),
1983 quoteaf (pretty_name (f)));
1984 ok = false;
1986 else
1988 uintmax_t read_pos;
1990 if (print_headers)
1991 write_header (pretty_name (f));
1992 ok = tail (pretty_name (f), fd, n_units, &read_pos);
1993 if (forever)
1995 struct stat stats;
1997 #if TAIL_TEST_SLEEP
1998 /* Before the tail function provided 'read_pos', there was
1999 a race condition described in the URL below. This sleep
2000 call made the window big enough to exercise the problem. */
2001 xnanosleep (1);
2002 #endif
2003 f->errnum = ok - 1;
2004 if (fstat (fd, &stats) < 0)
2006 ok = false;
2007 f->errnum = errno;
2008 error (0, errno, _("error reading %s"),
2009 quoteaf (pretty_name (f)));
2011 else if (!IS_TAILABLE_FILE_TYPE (stats.st_mode))
2013 ok = false;
2014 f->errnum = -1;
2015 f->tailable = false;
2016 f->ignore = ! reopen_inaccessible_files;
2017 error (0, 0, _("%s: cannot follow end of this type of file%s"),
2018 quotef (pretty_name (f)),
2019 f->ignore ? _("; giving up on this name") : "");
2022 if (!ok)
2024 f->ignore = ! reopen_inaccessible_files;
2025 close_fd (fd, pretty_name (f));
2026 f->fd = -1;
2028 else
2030 /* Note: we must use read_pos here, not stats.st_size,
2031 to avoid a race condition described by Ken Raeburn:
2032 https://lists.gnu.org/r/bug-textutils/2003-05/msg00007.html */
2033 record_open_fd (f, fd, read_pos, &stats, (is_stdin ? -1 : 1));
2034 f->remote = fremote (fd, pretty_name (f));
2037 else
2039 if (!is_stdin && close (fd))
2041 error (0, errno, _("error reading %s"),
2042 quoteaf (pretty_name (f)));
2043 ok = false;
2048 return ok;
2051 /* If obsolete usage is allowed, and the command line arguments are of
2052 the obsolete form and the option string is well-formed, set
2053 *N_UNITS, the globals COUNT_LINES, FOREVER, and FROM_START, and
2054 return true. If the command line arguments are obviously incorrect
2055 (e.g., because obsolete usage is not allowed and the arguments are
2056 incorrect for non-obsolete usage), report an error and exit.
2057 Otherwise, return false and don't modify any parameter or global
2058 variable. */
2060 static bool
2061 parse_obsolete_option (int argc, char * const *argv, uintmax_t *n_units)
2063 char const *p;
2064 char const *n_string;
2065 char const *n_string_end;
2066 int default_count = DEFAULT_N_LINES;
2067 bool t_from_start;
2068 bool t_count_lines = true;
2069 bool t_forever = false;
2071 /* With the obsolete form, there is one option string and at most
2072 one file argument. Watch out for "-" and "--", though. */
2073 if (! (argc == 2
2074 || (argc == 3 && ! (argv[2][0] == '-' && argv[2][1]))
2075 || (3 <= argc && argc <= 4 && STREQ (argv[2], "--"))))
2076 return false;
2078 int posix_ver = posix2_version ();
2079 bool obsolete_usage = posix_ver < 200112;
2080 bool traditional_usage = obsolete_usage || 200809 <= posix_ver;
2081 p = argv[1];
2083 switch (*p++)
2085 default:
2086 return false;
2088 case '+':
2089 /* Leading "+" is a file name in the standard form. */
2090 if (!traditional_usage)
2091 return false;
2093 t_from_start = true;
2094 break;
2096 case '-':
2097 /* In the non-obsolete form, "-" is standard input and "-c"
2098 requires an option-argument. The obsolete multidigit options
2099 are supported as a GNU extension even when conforming to
2100 POSIX 1003.1-2001 or later, so don't complain about them. */
2101 if (!obsolete_usage && !p[p[0] == 'c'])
2102 return false;
2104 t_from_start = false;
2105 break;
2108 n_string = p;
2109 while (ISDIGIT (*p))
2110 p++;
2111 n_string_end = p;
2113 switch (*p)
2115 case 'b': default_count *= 512; FALLTHROUGH;
2116 case 'c': t_count_lines = false; FALLTHROUGH;
2117 case 'l': p++; break;
2120 if (*p == 'f')
2122 t_forever = true;
2123 ++p;
2126 if (*p)
2127 return false;
2129 if (n_string == n_string_end)
2130 *n_units = default_count;
2131 else if ((xstrtoumax (n_string, NULL, 10, n_units, "b")
2132 & ~LONGINT_INVALID_SUFFIX_CHAR)
2133 != LONGINT_OK)
2135 die (EXIT_FAILURE, errno, "%s: %s", _("invalid number"),
2136 quote (argv[1]));
2139 /* Set globals. */
2140 from_start = t_from_start;
2141 count_lines = t_count_lines;
2142 forever = t_forever;
2144 return true;
2147 static void
2148 parse_options (int argc, char **argv,
2149 uintmax_t *n_units, enum header_mode *header_mode,
2150 double *sleep_interval)
2152 int c;
2154 while ((c = getopt_long (argc, argv, "c:n:fFqs:vz0123456789",
2155 long_options, NULL))
2156 != -1)
2158 switch (c)
2160 case 'F':
2161 forever = true;
2162 follow_mode = Follow_name;
2163 reopen_inaccessible_files = true;
2164 break;
2166 case 'c':
2167 case 'n':
2168 count_lines = (c == 'n');
2169 if (*optarg == '+')
2170 from_start = true;
2171 else if (*optarg == '-')
2172 ++optarg;
2174 *n_units = xdectoumax (optarg, 0, UINTMAX_MAX, "bkKmMGTPEZYRQ0",
2175 count_lines
2176 ? _("invalid number of lines")
2177 : _("invalid number of bytes"), 0);
2178 break;
2180 case 'f':
2181 case LONG_FOLLOW_OPTION:
2182 forever = true;
2183 if (optarg == NULL)
2184 follow_mode = DEFAULT_FOLLOW_MODE;
2185 else
2186 follow_mode = XARGMATCH ("--follow", optarg,
2187 follow_mode_string, follow_mode_map);
2188 break;
2190 case RETRY_OPTION:
2191 reopen_inaccessible_files = true;
2192 break;
2194 case MAX_UNCHANGED_STATS_OPTION:
2195 /* --max-unchanged-stats=N */
2196 max_n_unchanged_stats_between_opens =
2197 xdectoumax (optarg, 0, UINTMAX_MAX, "",
2198 _("invalid maximum number of unchanged stats between opens"), 0);
2199 break;
2201 case DISABLE_INOTIFY_OPTION:
2202 disable_inotify = true;
2203 break;
2205 case PID_OPTION:
2206 pid = xdectoumax (optarg, 0, PID_T_MAX, "", _("invalid PID"), 0);
2207 break;
2209 case PRESUME_INPUT_PIPE_OPTION:
2210 presume_input_pipe = true;
2211 break;
2213 case 'q':
2214 *header_mode = never;
2215 break;
2217 case 's':
2219 double s;
2220 if (! (xstrtod (optarg, NULL, &s, cl_strtod) && 0 <= s))
2221 die (EXIT_FAILURE, 0,
2222 _("invalid number of seconds: %s"), quote (optarg));
2223 *sleep_interval = s;
2225 break;
2227 case 'v':
2228 *header_mode = always;
2229 break;
2231 case 'z':
2232 line_end = '\0';
2233 break;
2235 case_GETOPT_HELP_CHAR;
2237 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
2239 case '0': case '1': case '2': case '3': case '4':
2240 case '5': case '6': case '7': case '8': case '9':
2241 die (EXIT_FAILURE, 0, _("option used in invalid context -- %c"), c);
2243 default:
2244 usage (EXIT_FAILURE);
2248 if (reopen_inaccessible_files)
2250 if (!forever)
2252 reopen_inaccessible_files = false;
2253 error (0, 0, _("warning: --retry ignored; --retry is useful"
2254 " only when following"));
2256 else if (follow_mode == Follow_descriptor)
2257 error (0, 0, _("warning: --retry only effective for the initial open"));
2260 if (pid && !forever)
2261 error (0, 0,
2262 _("warning: PID ignored; --pid=PID is useful only when following"));
2263 else if (pid && kill (pid, 0) != 0 && errno == ENOSYS)
2265 error (0, 0, _("warning: --pid=PID is not supported on this system"));
2266 pid = 0;
2270 /* Mark as '.ignore'd each member of F that corresponds to a
2271 pipe or fifo, and return the number of non-ignored members. */
2272 static size_t
2273 ignore_fifo_and_pipe (struct File_spec *f, size_t n_files)
2275 /* When there is no FILE operand and stdin is a pipe or FIFO
2276 POSIX requires that tail ignore the -f option.
2277 Since we allow multiple FILE operands, we extend that to say: with -f,
2278 ignore any "-" operand that corresponds to a pipe or FIFO. */
2279 size_t n_viable = 0;
2281 for (size_t i = 0; i < n_files; i++)
2283 bool is_a_fifo_or_pipe =
2284 (STREQ (f[i].name, "-")
2285 && !f[i].ignore
2286 && 0 <= f[i].fd
2287 && (S_ISFIFO (f[i].mode)
2288 || (HAVE_FIFO_PIPES != 1 && isapipe (f[i].fd))));
2289 if (is_a_fifo_or_pipe)
2291 f[i].fd = -1;
2292 f[i].ignore = true;
2294 else
2295 ++n_viable;
2298 return n_viable;
2302 main (int argc, char **argv)
2304 enum header_mode header_mode = multiple_files;
2305 bool ok = true;
2306 /* If from_start, the number of items to skip before printing; otherwise,
2307 the number of items at the end of the file to print. Although the type
2308 is signed, the value is never negative. */
2309 uintmax_t n_units = DEFAULT_N_LINES;
2310 size_t n_files;
2311 char **file;
2312 struct File_spec *F;
2313 size_t i;
2314 bool obsolete_option;
2316 /* The number of seconds to sleep between iterations.
2317 During one iteration, every file name or descriptor is checked to
2318 see if it has changed. */
2319 double sleep_interval = 1.0;
2321 initialize_main (&argc, &argv);
2322 set_program_name (argv[0]);
2323 setlocale (LC_ALL, "");
2324 bindtextdomain (PACKAGE, LOCALEDIR);
2325 textdomain (PACKAGE);
2327 atexit (close_stdout);
2329 have_read_stdin = false;
2331 count_lines = true;
2332 forever = from_start = print_headers = false;
2333 line_end = '\n';
2334 obsolete_option = parse_obsolete_option (argc, argv, &n_units);
2335 argc -= obsolete_option;
2336 argv += obsolete_option;
2337 parse_options (argc, argv, &n_units, &header_mode, &sleep_interval);
2339 /* To start printing with item N_UNITS from the start of the file, skip
2340 N_UNITS - 1 items. 'tail -n +0' is actually meaningless, but for Unix
2341 compatibility it's treated the same as 'tail -n +1'. */
2342 if (from_start)
2344 if (n_units)
2345 --n_units;
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 struct stat out_stat;
2411 if (fstat (STDOUT_FILENO, &out_stat) < 0)
2412 die (EXIT_FAILURE, errno, _("standard output"));
2413 monitor_output = (S_ISFIFO (out_stat.st_mode)
2414 || (HAVE_FIFO_PIPES != 1 && isapipe (STDOUT_FILENO)));
2416 #if HAVE_INOTIFY
2417 /* tailable_stdin() checks if the user specifies stdin via "-",
2418 or implicitly by providing no arguments. If so, we won't use inotify.
2419 Technically, on systems with a working /dev/stdin, we *could*,
2420 but would it be worth it? Verifying that it's a real device
2421 and hooked up to stdin is not trivial, while reverting to
2422 non-inotify-based tail_forever is easy and portable.
2424 any_remote_file() checks if the user has specified any
2425 files that reside on remote file systems. inotify is not used
2426 in this case because it would miss any updates to the file
2427 that were not initiated from the local system.
2429 any_non_remote_file() checks if the user has specified any
2430 files that don't reside on remote file systems. inotify is not used
2431 if there are no open files, as we can't determine if those file
2432 will be on a remote file system.
2434 any_symlinks() checks if the user has specified any symbolic links.
2435 inotify is not used in this case because it returns updated _targets_
2436 which would not match the specified names. If we tried to always
2437 use the target names, then we would miss changes to the symlink itself.
2439 ok is false when one of the files specified could not be opened for
2440 reading. In this case and when following by descriptor,
2441 tail_forever_inotify() cannot be used (in its current implementation).
2443 FIXME: inotify doesn't give any notification when a new
2444 (remote) file or directory is mounted on top a watched file.
2445 When follow_mode == Follow_name we would ideally like to detect that.
2446 Note if there is a change to the original file then we'll
2447 recheck it and follow the new file, or ignore it if the
2448 file has changed to being remote.
2450 FIXME-maybe: inotify has a watch descriptor per inode, and hence with
2451 our current hash implementation will only --follow data for one
2452 of the names when multiple hardlinked files are specified, or
2453 for one name when a name is specified multiple times. */
2454 if (!disable_inotify && (tailable_stdin (F, n_files)
2455 || any_remote_file (F, n_files)
2456 || ! any_non_remote_file (F, n_files)
2457 || any_symlinks (F, n_files)
2458 || any_non_regular_fifo (F, n_files)
2459 || (!ok && follow_mode == Follow_descriptor)))
2460 disable_inotify = true;
2462 if (!disable_inotify)
2464 int wd = inotify_init ();
2465 if (0 <= wd)
2467 /* Flush any output from tail_file, now, since
2468 tail_forever_inotify flushes only after writing,
2469 not before reading. */
2470 if (fflush (stdout) != 0)
2471 die (EXIT_FAILURE, errno, _("write error"));
2473 Hash_table *ht;
2474 tail_forever_inotify (wd, F, n_files, sleep_interval, &ht);
2475 hash_free (ht);
2476 close (wd);
2477 errno = 0;
2479 error (0, errno, _("inotify cannot be used, reverting to polling"));
2481 #endif
2482 disable_inotify = true;
2483 tail_forever (F, n_files, sleep_interval);
2486 if (have_read_stdin && close (STDIN_FILENO) < 0)
2487 die (EXIT_FAILURE, errno, "-");
2488 main_exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);