numfmt: prefer signed types
[coreutils.git] / src / tail.c
blobf293551ea00e9b4143f296c5d5597ffd78c006dc
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 <getopt.h>
30 #include <sys/types.h>
31 #include <signal.h>
33 #include "system.h"
34 #include "argmatch.h"
35 #include "assure.h"
36 #include "cl-strtod.h"
37 #include "fcntl--.h"
38 #include "iopoll.h"
39 #include "isapipe.h"
40 #include "posixver.h"
41 #include "quote.h"
42 #include "safe-read.h"
43 #include "stat-size.h"
44 #include "stat-time.h"
45 #include "xbinary-io.h"
46 #include "xdectoint.h"
47 #include "xnanosleep.h"
48 #include "xstrtol.h"
49 #include "xstrtod.h"
51 #if HAVE_INOTIFY
52 # include "hash.h"
53 # include <poll.h>
54 # include <sys/inotify.h>
55 #endif
57 /* Linux can optimize the handling of local files. */
58 #if defined __linux__ || defined __ANDROID__
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", nullptr
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, nullptr, 'c'},
235 {"follow", optional_argument, nullptr, LONG_FOLLOW_OPTION},
236 {"lines", required_argument, nullptr, 'n'},
237 {"max-unchanged-stats", required_argument, nullptr,
238 MAX_UNCHANGED_STATS_OPTION},
239 {"-disable-inotify", no_argument, nullptr,
240 DISABLE_INOTIFY_OPTION}, /* do not document */
241 {"pid", required_argument, nullptr, PID_OPTION},
242 {"-presume-input-pipe", no_argument, nullptr,
243 PRESUME_INPUT_PIPE_OPTION}, /* do not document */
244 {"quiet", no_argument, nullptr, 'q'},
245 {"retry", no_argument, nullptr, RETRY_OPTION},
246 {"silent", no_argument, nullptr, 'q'},
247 {"sleep-interval", required_argument, nullptr, 's'},
248 {"verbose", no_argument, nullptr, 'v'},
249 {"zero-terminated", no_argument, nullptr, 'z'},
250 {GETOPT_HELP_OPTION_DECL},
251 {GETOPT_VERSION_OPTION_DECL},
252 {nullptr, 0, nullptr, 0}
255 void
256 usage (int status)
258 if (status != EXIT_SUCCESS)
259 emit_try_help ();
260 else
262 printf (_("\
263 Usage: %s [OPTION]... [FILE]...\n\
265 program_name);
266 printf (_("\
267 Print the last %d lines of each FILE to standard output.\n\
268 With more than one FILE, precede each with a header giving the file name.\n\
269 "), DEFAULT_N_LINES);
271 emit_stdin_note ();
272 emit_mandatory_arg_note ();
274 fputs (_("\
275 -c, --bytes=[+]NUM output the last NUM bytes; or use -c +NUM to\n\
276 output starting with byte NUM of each file\n\
277 "), stdout);
278 fputs (_("\
279 -f, --follow[={name|descriptor}]\n\
280 output appended data as the file grows;\n\
281 an absent option argument means 'descriptor'\n\
282 -F same as --follow=name --retry\n\
283 "), stdout);
284 printf (_("\
285 -n, --lines=[+]NUM output the last NUM lines, instead of the last %d;\n\
286 or use -n +NUM to skip NUM-1 lines at the start\n\
288 DEFAULT_N_LINES
290 printf (_("\
291 --max-unchanged-stats=N\n\
292 with --follow=name, reopen a FILE which has not\n\
293 changed size after N (default %d) iterations\n\
294 to see if it has been unlinked or renamed\n\
295 (this is the usual case of rotated log files);\n\
296 with inotify, this option is rarely useful\n\
298 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS
300 fputs (_("\
301 --pid=PID with -f, terminate after process ID, PID dies\n\
302 -q, --quiet, --silent never output headers giving file names\n\
303 --retry keep trying to open a file if it is inaccessible\n\
304 "), stdout);
305 fputs (_("\
306 -s, --sleep-interval=N with -f, sleep for approximately N seconds\n\
307 (default 1.0) between iterations;\n\
308 with inotify and --pid=P, check process P at\n\
309 least once every N seconds\n\
310 -v, --verbose always output headers giving file names\n\
311 "), stdout);
312 fputs (_("\
313 -z, --zero-terminated line delimiter is NUL, not newline\n\
314 "), stdout);
315 fputs (HELP_OPTION_DESCRIPTION, stdout);
316 fputs (VERSION_OPTION_DESCRIPTION, stdout);
317 fputs (_("\
319 NUM may have a multiplier suffix:\n\
320 b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\
321 GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y, R, Q.\n\
322 Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\
324 "), stdout);
325 fputs (_("\
326 With --follow (-f), tail defaults to following the file descriptor, which\n\
327 means that even if a tail'ed file is renamed, tail will continue to track\n\
328 its end. This default behavior is not desirable when you really want to\n\
329 track the actual name of the file, not the file descriptor (e.g., log\n\
330 rotation). Use --follow=name in that case. That causes tail to track the\n\
331 named file in a way that accommodates renaming, removal and creation.\n\
332 "), stdout);
333 emit_ancillary_info (PROGRAM_NAME);
335 exit (status);
338 /* Ensure exit, either with SIGPIPE or EXIT_FAILURE status. */
339 static void
340 die_pipe (void)
342 raise (SIGPIPE);
343 exit (EXIT_FAILURE);
346 /* If the output has gone away, then terminate
347 as we would if we had written to this output. */
348 static void
349 check_output_alive (void)
351 if (! monitor_output)
352 return;
354 if (iopoll (-1, STDOUT_FILENO, false) == IOPOLL_BROKEN_OUTPUT)
355 die_pipe ();
358 MAYBE_UNUSED static bool
359 valid_file_spec (struct File_spec const *f)
361 /* Exactly one of the following subexpressions must be true. */
362 return ((f->fd == -1) ^ (f->errnum == 0));
365 static char const *
366 pretty_name (struct File_spec const *f)
368 return (STREQ (f->name, "-") ? _("standard input") : f->name);
371 /* Record a file F with descriptor FD, size SIZE, status ST, and
372 blocking status BLOCKING. */
374 static void
375 record_open_fd (struct File_spec *f, int fd,
376 off_t size, struct stat const *st,
377 int blocking)
379 f->fd = fd;
380 f->size = size;
381 f->mtime = get_stat_mtime (st);
382 f->dev = st->st_dev;
383 f->ino = st->st_ino;
384 f->mode = st->st_mode;
385 f->blocking = blocking;
386 f->n_unchanged_stats = 0;
387 f->ignore = false;
390 /* Close the file with descriptor FD and name FILENAME. */
392 static void
393 close_fd (int fd, char const *filename)
395 if (fd != -1 && fd != STDIN_FILENO && close (fd))
397 error (0, errno, _("closing %s (fd=%d)"), quoteaf (filename), fd);
401 static void
402 write_header (char const *pretty_filename)
404 static bool first_file = true;
406 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), pretty_filename);
407 first_file = false;
410 /* Write N_BYTES from BUFFER to stdout.
411 Exit immediately on error with a single diagnostic. */
413 static void
414 xwrite_stdout (char const *buffer, size_t n_bytes)
416 if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) < n_bytes)
418 clearerr (stdout); /* To avoid redundant close_stdout diagnostic. */
419 error (EXIT_FAILURE, errno, _("error writing %s"),
420 quoteaf ("standard output"));
424 /* Read and output N_BYTES of file PRETTY_FILENAME starting at the current
425 position in FD. If N_BYTES is COPY_TO_EOF, then copy until end of file.
426 If N_BYTES is COPY_A_BUFFER, then copy at most one buffer's worth.
427 Return the number of bytes read from the file. */
429 static uintmax_t
430 dump_remainder (bool want_header, char const *pretty_filename, int fd,
431 uintmax_t n_bytes)
433 uintmax_t n_written;
434 uintmax_t n_remaining = n_bytes;
436 n_written = 0;
437 while (true)
439 char buffer[BUFSIZ];
440 size_t n = MIN (n_remaining, BUFSIZ);
441 size_t bytes_read = safe_read (fd, buffer, n);
442 if (bytes_read == SAFE_READ_ERROR)
444 if (errno != EAGAIN)
445 error (EXIT_FAILURE, errno, _("error reading %s"),
446 quoteaf (pretty_filename));
447 break;
449 if (bytes_read == 0)
450 break;
451 if (want_header)
453 write_header (pretty_filename);
454 want_header = false;
456 xwrite_stdout (buffer, bytes_read);
457 n_written += bytes_read;
458 if (n_bytes != COPY_TO_EOF)
460 n_remaining -= bytes_read;
461 if (n_remaining == 0 || n_bytes == COPY_A_BUFFER)
462 break;
466 return n_written;
469 /* Call lseek with the specified arguments, where file descriptor FD
470 corresponds to the file, FILENAME.
471 Give a diagnostic and exit nonzero if lseek fails.
472 Otherwise, return the resulting offset. */
474 static off_t
475 xlseek (int fd, off_t offset, int whence, char const *filename)
477 off_t new_offset = lseek (fd, offset, whence);
478 char buf[INT_BUFSIZE_BOUND (offset)];
479 char *s;
481 if (0 <= new_offset)
482 return new_offset;
484 s = offtostr (offset, buf);
485 switch (whence)
487 case SEEK_SET:
488 error (EXIT_FAILURE, errno, _("%s: cannot seek to offset %s"),
489 quotef (filename), s);
490 break;
491 case SEEK_CUR:
492 error (EXIT_FAILURE, errno, _("%s: cannot seek to relative offset %s"),
493 quotef (filename), s);
494 break;
495 case SEEK_END:
496 error (EXIT_FAILURE, errno,
497 _("%s: cannot seek to end-relative offset %s"),
498 quotef (filename), s);
499 break;
500 default:
501 unreachable ();
505 /* Print the last N_LINES lines from the end of file FD.
506 Go backward through the file, reading 'BUFSIZ' bytes at a time (except
507 probably the first), until we hit the start of the file or have
508 read NUMBER newlines.
509 START_POS is the starting position of the read pointer for the file
510 associated with FD (may be nonzero).
511 END_POS is the file offset of EOF (one larger than offset of last byte).
512 Return true if successful. */
514 static bool
515 file_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
516 off_t start_pos, off_t end_pos, uintmax_t *read_pos)
518 char buffer[BUFSIZ];
519 size_t bytes_read;
520 off_t pos = end_pos;
522 if (n_lines == 0)
523 return true;
525 /* Set 'bytes_read' to the size of the last, probably partial, buffer;
526 0 < 'bytes_read' <= 'BUFSIZ'. */
527 bytes_read = (pos - start_pos) % BUFSIZ;
528 if (bytes_read == 0)
529 bytes_read = BUFSIZ;
530 /* Make 'pos' a multiple of 'BUFSIZ' (0 if the file is short), so that all
531 reads will be on block boundaries, which might increase efficiency. */
532 pos -= bytes_read;
533 xlseek (fd, pos, SEEK_SET, pretty_filename);
534 bytes_read = safe_read (fd, buffer, bytes_read);
535 if (bytes_read == SAFE_READ_ERROR)
537 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
538 return false;
540 *read_pos = pos + bytes_read;
542 /* Count the incomplete line on files that don't end with a newline. */
543 if (bytes_read && buffer[bytes_read - 1] != line_end)
544 --n_lines;
548 /* Scan backward, counting the newlines in this bufferfull. */
550 size_t n = bytes_read;
551 while (n)
553 char const *nl;
554 nl = memrchr (buffer, line_end, n);
555 if (nl == nullptr)
556 break;
557 n = nl - buffer;
558 if (n_lines-- == 0)
560 /* If this newline isn't the last character in the buffer,
561 output the part that is after it. */
562 xwrite_stdout (nl + 1, bytes_read - (n + 1));
563 *read_pos += dump_remainder (false, pretty_filename, fd,
564 end_pos - (pos + bytes_read));
565 return true;
569 /* Not enough newlines in that bufferfull. */
570 if (pos == start_pos)
572 /* Not enough lines in the file; print everything from
573 start_pos to the end. */
574 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
575 *read_pos = start_pos + dump_remainder (false, pretty_filename, fd,
576 end_pos);
577 return true;
579 pos -= BUFSIZ;
580 xlseek (fd, pos, SEEK_SET, pretty_filename);
582 bytes_read = safe_read (fd, buffer, BUFSIZ);
583 if (bytes_read == SAFE_READ_ERROR)
585 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
586 return false;
589 *read_pos = pos + bytes_read;
591 while (bytes_read > 0);
593 return true;
596 /* Print the last N_LINES lines from the end of the standard input,
597 open for reading as pipe FD.
598 Buffer the text as a linked list of LBUFFERs, adding them as needed.
599 Return true if successful. */
601 static bool
602 pipe_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
603 uintmax_t *read_pos)
605 struct linebuffer
607 char buffer[BUFSIZ];
608 size_t nbytes;
609 size_t nlines;
610 struct linebuffer *next;
612 typedef struct linebuffer LBUFFER;
613 LBUFFER *first, *last, *tmp;
614 size_t total_lines = 0; /* Total number of newlines in all buffers. */
615 bool ok = true;
616 size_t n_read; /* Size in bytes of most recent read */
618 first = last = xmalloc (sizeof (LBUFFER));
619 first->nbytes = first->nlines = 0;
620 first->next = nullptr;
621 tmp = xmalloc (sizeof (LBUFFER));
623 /* Input is always read into a fresh buffer. */
624 while (true)
626 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
627 if (n_read == 0 || n_read == SAFE_READ_ERROR)
628 break;
629 tmp->nbytes = n_read;
630 *read_pos += n_read;
631 tmp->nlines = 0;
632 tmp->next = nullptr;
634 /* Count the number of newlines just read. */
636 char const *buffer_end = tmp->buffer + n_read;
637 char const *p = tmp->buffer;
638 while ((p = memchr (p, line_end, buffer_end - p)))
640 ++p;
641 ++tmp->nlines;
644 total_lines += tmp->nlines;
646 /* If there is enough room in the last buffer read, just append the new
647 one to it. This is because when reading from a pipe, 'n_read' can
648 often be very small. */
649 if (tmp->nbytes + last->nbytes < BUFSIZ)
651 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
652 last->nbytes += tmp->nbytes;
653 last->nlines += tmp->nlines;
655 else
657 /* If there's not enough room, link the new buffer onto the end of
658 the list, then either free up the oldest buffer for the next
659 read if that would leave enough lines, or else malloc a new one.
660 Some compaction mechanism is possible but probably not
661 worthwhile. */
662 last = last->next = tmp;
663 if (total_lines - first->nlines > n_lines)
665 tmp = first;
666 total_lines -= first->nlines;
667 first = first->next;
669 else
670 tmp = xmalloc (sizeof (LBUFFER));
674 free (tmp);
676 if (n_read == SAFE_READ_ERROR)
678 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
679 ok = false;
680 goto free_lbuffers;
683 /* If the file is empty, then bail out. */
684 if (last->nbytes == 0)
685 goto free_lbuffers;
687 /* This prevents a core dump when the pipe contains no newlines. */
688 if (n_lines == 0)
689 goto free_lbuffers;
691 /* Count the incomplete line on files that don't end with a newline. */
692 if (last->buffer[last->nbytes - 1] != line_end)
694 ++last->nlines;
695 ++total_lines;
698 /* Run through the list, printing lines. First, skip over unneeded
699 buffers. */
700 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
701 total_lines -= tmp->nlines;
703 /* Find the correct beginning, then print the rest of the file. */
705 char const *beg = tmp->buffer;
706 char const *buffer_end = tmp->buffer + tmp->nbytes;
707 if (total_lines > n_lines)
709 /* Skip 'total_lines' - 'n_lines' newlines. We made sure that
710 'total_lines' - 'n_lines' <= 'tmp->nlines'. */
711 size_t j;
712 for (j = total_lines - n_lines; j; --j)
714 beg = rawmemchr (beg, line_end);
715 ++beg;
719 xwrite_stdout (beg, buffer_end - beg);
722 for (tmp = tmp->next; tmp; tmp = tmp->next)
723 xwrite_stdout (tmp->buffer, tmp->nbytes);
725 free_lbuffers:
726 while (first)
728 tmp = first->next;
729 free (first);
730 first = tmp;
732 return ok;
735 /* Print the last N_BYTES characters from the end of pipe FD.
736 This is a stripped down version of pipe_lines.
737 Return true if successful. */
739 static bool
740 pipe_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
741 uintmax_t *read_pos)
743 struct charbuffer
745 char buffer[BUFSIZ];
746 size_t nbytes;
747 struct charbuffer *next;
749 typedef struct charbuffer CBUFFER;
750 CBUFFER *first, *last, *tmp;
751 size_t i; /* Index into buffers. */
752 size_t total_bytes = 0; /* Total characters in all buffers. */
753 bool ok = true;
754 size_t n_read;
756 first = last = xmalloc (sizeof (CBUFFER));
757 first->nbytes = 0;
758 first->next = nullptr;
759 tmp = xmalloc (sizeof (CBUFFER));
761 /* Input is always read into a fresh buffer. */
762 while (true)
764 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
765 if (n_read == 0 || n_read == SAFE_READ_ERROR)
766 break;
767 *read_pos += n_read;
768 tmp->nbytes = n_read;
769 tmp->next = nullptr;
771 total_bytes += tmp->nbytes;
772 /* If there is enough room in the last buffer read, just append the new
773 one to it. This is because when reading from a pipe, 'nbytes' can
774 often be very small. */
775 if (tmp->nbytes + last->nbytes < BUFSIZ)
777 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
778 last->nbytes += tmp->nbytes;
780 else
782 /* If there's not enough room, link the new buffer onto the end of
783 the list, then either free up the oldest buffer for the next
784 read if that would leave enough characters, or else malloc a new
785 one. Some compaction mechanism is possible but probably not
786 worthwhile. */
787 last = last->next = tmp;
788 if (total_bytes - first->nbytes > n_bytes)
790 tmp = first;
791 total_bytes -= first->nbytes;
792 first = first->next;
794 else
796 tmp = xmalloc (sizeof (CBUFFER));
801 free (tmp);
803 if (n_read == SAFE_READ_ERROR)
805 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
806 ok = false;
807 goto free_cbuffers;
810 /* Run through the list, printing characters. First, skip over unneeded
811 buffers. */
812 for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
813 total_bytes -= tmp->nbytes;
815 /* Find the correct beginning, then print the rest of the file.
816 We made sure that 'total_bytes' - 'n_bytes' <= 'tmp->nbytes'. */
817 if (total_bytes > n_bytes)
818 i = total_bytes - n_bytes;
819 else
820 i = 0;
821 xwrite_stdout (&tmp->buffer[i], tmp->nbytes - i);
823 for (tmp = tmp->next; tmp; tmp = tmp->next)
824 xwrite_stdout (tmp->buffer, tmp->nbytes);
826 free_cbuffers:
827 while (first)
829 tmp = first->next;
830 free (first);
831 first = tmp;
833 return ok;
836 /* Skip N_BYTES characters from the start of pipe FD, and print
837 any extra characters that were read beyond that.
838 Return 1 on error, 0 if ok, -1 if EOF. */
840 static int
841 start_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
842 uintmax_t *read_pos)
844 char buffer[BUFSIZ];
846 while (0 < n_bytes)
848 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
849 if (bytes_read == 0)
850 return -1;
851 if (bytes_read == SAFE_READ_ERROR)
853 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
854 return 1;
856 *read_pos += bytes_read;
857 if (bytes_read <= n_bytes)
858 n_bytes -= bytes_read;
859 else
861 size_t n_remaining = bytes_read - n_bytes;
862 /* Print extra characters if there are any. */
863 xwrite_stdout (&buffer[n_bytes], n_remaining);
864 break;
868 return 0;
871 /* Skip N_LINES lines at the start of file or pipe FD, and print
872 any extra characters that were read beyond that.
873 Return 1 on error, 0 if ok, -1 if EOF. */
875 static int
876 start_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
877 uintmax_t *read_pos)
879 if (n_lines == 0)
880 return 0;
882 while (true)
884 char buffer[BUFSIZ];
885 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
886 if (bytes_read == 0) /* EOF */
887 return -1;
888 if (bytes_read == SAFE_READ_ERROR) /* error */
890 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
891 return 1;
894 char *buffer_end = buffer + bytes_read;
896 *read_pos += bytes_read;
898 char *p = buffer;
899 while ((p = memchr (p, line_end, buffer_end - p)))
901 ++p;
902 if (--n_lines == 0)
904 if (p < buffer_end)
905 xwrite_stdout (p, buffer_end - p);
906 return 0;
912 /* Return false when FD is open on a file residing on a local file system.
913 If fstatfs fails, give a diagnostic and return true.
914 If fstatfs cannot be called, return true. */
915 static bool
916 fremote (int fd, char const *name)
918 bool remote = true; /* be conservative (poll by default). */
920 #if HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE \
921 && (defined __linux__ || defined __ANDROID__)
922 struct statfs buf;
923 int err = fstatfs (fd, &buf);
924 if (err != 0)
926 /* On at least linux-2.6.38, fstatfs fails with ENOSYS when FD
927 is open on a pipe. Treat that like a remote file. */
928 if (errno != ENOSYS)
929 error (0, errno, _("cannot determine location of %s. "
930 "reverting to polling"), quoteaf (name));
932 else
934 /* Treat unrecognized file systems as "remote", so caller polls.
935 Note README-release has instructions for syncing the internal
936 list with the latest Linux kernel file system constants. */
937 remote = is_local_fs_type (buf.f_type) <= 0;
939 #endif
941 return remote;
944 /* open/fstat F->name and handle changes. */
945 static void
946 recheck (struct File_spec *f, bool blocking)
948 struct stat new_stats;
949 bool ok = true;
950 bool is_stdin = (STREQ (f->name, "-"));
951 bool was_tailable = f->tailable;
952 int prev_errnum = f->errnum;
953 bool new_file;
954 int fd = (is_stdin
955 ? STDIN_FILENO
956 : open (f->name, O_RDONLY | (blocking ? 0 : O_NONBLOCK)));
958 affirm (valid_file_spec (f));
960 /* If the open fails because the file doesn't exist,
961 then mark the file as not tailable. */
962 f->tailable = !(reopen_inaccessible_files && fd == -1);
964 if (! disable_inotify && ! lstat (f->name, &new_stats)
965 && S_ISLNK (new_stats.st_mode))
967 /* Diagnose the edge case where a regular file is changed
968 to a symlink. We avoid inotify with symlinks since
969 it's awkward to match between symlink name and target. */
970 ok = false;
971 f->errnum = -1;
972 f->ignore = true;
974 error (0, 0, _("%s has been replaced with an untailable symbolic link"),
975 quoteaf (pretty_name (f)));
977 else if (fd == -1 || fstat (fd, &new_stats) < 0)
979 ok = false;
980 f->errnum = errno;
981 if (!f->tailable)
983 if (was_tailable)
985 /* FIXME-maybe: detect the case in which the file first becomes
986 unreadable (perms), and later becomes readable again and can
987 be seen to be the same file (dev/ino). Otherwise, tail prints
988 the entire contents of the file when it becomes readable. */
989 error (0, f->errnum, _("%s has become inaccessible"),
990 quoteaf (pretty_name (f)));
992 else
994 /* say nothing... it's still not tailable */
997 else if (prev_errnum != errno)
998 error (0, errno, "%s", quotef (pretty_name (f)));
1000 else if (!IS_TAILABLE_FILE_TYPE (new_stats.st_mode))
1002 ok = false;
1003 f->errnum = -1;
1004 f->tailable = false;
1005 f->ignore = ! (reopen_inaccessible_files && follow_mode == Follow_name);
1006 if (was_tailable || prev_errnum != f->errnum)
1007 error (0, 0, _("%s has been replaced with an untailable file%s"),
1008 quoteaf (pretty_name (f)),
1009 f->ignore ? _("; giving up on this name") : "");
1011 else if ((f->remote = fremote (fd, pretty_name (f))) && ! disable_inotify)
1013 ok = false;
1014 f->errnum = -1;
1015 error (0, 0, _("%s has been replaced with an untailable remote file"),
1016 quoteaf (pretty_name (f)));
1017 f->ignore = true;
1018 f->remote = true;
1020 else
1022 f->errnum = 0;
1025 new_file = false;
1026 if (!ok)
1028 close_fd (fd, pretty_name (f));
1029 close_fd (f->fd, pretty_name (f));
1030 f->fd = -1;
1032 else if (prev_errnum && prev_errnum != ENOENT)
1034 new_file = true;
1035 affirm (f->fd == -1);
1036 error (0, 0, _("%s has become accessible"), quoteaf (pretty_name (f)));
1038 else if (f->fd == -1)
1040 /* A new file even when inodes haven't changed as <dev,inode>
1041 pairs can be reused, and we know the file was missing
1042 on the previous iteration. Note this also means the file
1043 is redisplayed in --follow=name mode if renamed away from
1044 and back to a monitored name. */
1045 new_file = true;
1047 error (0, 0,
1048 _("%s has appeared; following new file"),
1049 quoteaf (pretty_name (f)));
1051 else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
1053 /* File has been replaced (e.g., via log rotation) --
1054 tail the new one. */
1055 new_file = true;
1057 error (0, 0,
1058 _("%s has been replaced; following new file"),
1059 quoteaf (pretty_name (f)));
1061 /* Close the old one. */
1062 close_fd (f->fd, pretty_name (f));
1065 else
1067 /* No changes detected, so close new fd. */
1068 close_fd (fd, pretty_name (f));
1071 /* FIXME: When a log is rotated, daemons tend to log to the
1072 old file descriptor until the new file is present and
1073 the daemon is sent a signal. Therefore tail may miss entries
1074 being written to the old file. Perhaps we should keep
1075 the older file open and continue to monitor it until
1076 data is written to a new file. */
1077 if (new_file)
1079 /* Start at the beginning of the file. */
1080 record_open_fd (f, fd, 0, &new_stats, (is_stdin ? -1 : blocking));
1081 if (S_ISREG (new_stats.st_mode))
1082 xlseek (fd, 0, SEEK_SET, pretty_name (f));
1086 /* Return true if any of the N_FILES files in F are live, i.e., have
1087 open file descriptors, or should be checked again (see --retry).
1088 When following descriptors, checking should only continue when any
1089 of the files is not yet ignored. */
1091 static bool
1092 any_live_files (const struct File_spec *f, size_t n_files)
1094 /* In inotify mode, ignore may be set for files
1095 which may later be replaced with new files.
1096 So always consider files live in -F mode. */
1097 if (reopen_inaccessible_files && follow_mode == Follow_name)
1098 return true;
1100 for (size_t i = 0; i < n_files; i++)
1102 if (0 <= f[i].fd)
1103 return true;
1104 else
1106 if (! f[i].ignore && reopen_inaccessible_files)
1107 return true;
1111 return false;
1114 /* Tail N_FILES files forever, or until killed.
1115 The pertinent information for each file is stored in an entry of F.
1116 Loop over each of them, doing an fstat to see if they have changed size,
1117 and an occasional open/fstat to see if any dev/ino pair has changed.
1118 If none of them have changed size in one iteration, sleep for a
1119 while and try again. Continue until the user interrupts us. */
1121 static void
1122 tail_forever (struct File_spec *f, size_t n_files, double sleep_interval)
1124 /* Use blocking I/O as an optimization, when it's easy. */
1125 bool blocking = (pid == 0 && follow_mode == Follow_descriptor
1126 && n_files == 1 && f[0].fd != -1 && ! S_ISREG (f[0].mode));
1127 size_t last;
1128 bool writer_is_dead = false;
1130 last = n_files - 1;
1132 while (true)
1134 size_t i;
1135 bool any_input = false;
1137 for (i = 0; i < n_files; i++)
1139 int fd;
1140 char const *name;
1141 mode_t mode;
1142 struct stat stats;
1143 uintmax_t bytes_read;
1145 if (f[i].ignore)
1146 continue;
1148 if (f[i].fd < 0)
1150 recheck (&f[i], blocking);
1151 continue;
1154 fd = f[i].fd;
1155 name = pretty_name (&f[i]);
1156 mode = f[i].mode;
1158 if (f[i].blocking != blocking)
1160 int old_flags = fcntl (fd, F_GETFL);
1161 int new_flags = old_flags | (blocking ? 0 : O_NONBLOCK);
1162 if (old_flags < 0
1163 || (new_flags != old_flags
1164 && fcntl (fd, F_SETFL, new_flags) == -1))
1166 /* Don't update f[i].blocking if fcntl fails. */
1167 if (S_ISREG (f[i].mode) && errno == EPERM)
1169 /* This happens when using tail -f on a file with
1170 the append-only attribute. */
1172 else
1173 error (EXIT_FAILURE, errno,
1174 _("%s: cannot change nonblocking mode"),
1175 quotef (name));
1177 else
1178 f[i].blocking = blocking;
1181 bool read_unchanged = false;
1182 if (!f[i].blocking)
1184 if (fstat (fd, &stats) != 0)
1186 f[i].fd = -1;
1187 f[i].errnum = errno;
1188 error (0, errno, "%s", quotef (name));
1189 close (fd); /* ignore failure */
1190 continue;
1193 if (f[i].mode == stats.st_mode
1194 && (! S_ISREG (stats.st_mode) || f[i].size == stats.st_size)
1195 && timespec_cmp (f[i].mtime, get_stat_mtime (&stats)) == 0)
1197 if ((max_n_unchanged_stats_between_opens
1198 <= f[i].n_unchanged_stats++)
1199 && follow_mode == Follow_name)
1201 recheck (&f[i], f[i].blocking);
1202 f[i].n_unchanged_stats = 0;
1204 if (fd != f[i].fd || S_ISREG (stats.st_mode) || 1 < n_files)
1205 continue;
1206 else
1207 read_unchanged = true;
1210 affirm (fd == f[i].fd);
1212 /* This file has changed. Print out what we can, and
1213 then keep looping. */
1215 f[i].mtime = get_stat_mtime (&stats);
1216 f[i].mode = stats.st_mode;
1218 /* reset counter */
1219 if (! read_unchanged)
1220 f[i].n_unchanged_stats = 0;
1222 /* XXX: This is only a heuristic, as the file may have also
1223 been truncated and written to if st_size >= size
1224 (in which case we ignore new data <= size). */
1225 if (S_ISREG (mode) && stats.st_size < f[i].size)
1227 error (0, 0, _("%s: file truncated"), quotef (name));
1228 /* Assume the file was truncated to 0,
1229 and therefore output all "new" data. */
1230 xlseek (fd, 0, SEEK_SET, name);
1231 f[i].size = 0;
1234 if (i != last)
1236 if (print_headers)
1237 write_header (name);
1238 last = i;
1242 /* Don't read more than st_size on networked file systems
1243 because it was seen on glusterfs at least, that st_size
1244 may be smaller than the data read on a _subsequent_ stat call. */
1245 uintmax_t bytes_to_read;
1246 if (f[i].blocking)
1247 bytes_to_read = COPY_A_BUFFER;
1248 else if (S_ISREG (mode) && f[i].remote)
1249 bytes_to_read = stats.st_size - f[i].size;
1250 else
1251 bytes_to_read = COPY_TO_EOF;
1253 bytes_read = dump_remainder (false, name, fd, bytes_to_read);
1255 if (read_unchanged && bytes_read)
1256 f[i].n_unchanged_stats = 0;
1258 any_input |= (bytes_read != 0);
1259 f[i].size += bytes_read;
1262 if (! any_live_files (f, n_files))
1264 error (0, 0, _("no files remaining"));
1265 break;
1268 if ((!any_input || blocking) && fflush (stdout) != 0)
1269 write_error ();
1271 check_output_alive ();
1273 /* If nothing was read, sleep and/or check for dead writers. */
1274 if (!any_input)
1276 if (writer_is_dead)
1277 break;
1279 /* Once the writer is dead, read the files once more to
1280 avoid a race condition. */
1281 writer_is_dead = (pid != 0
1282 && kill (pid, 0) != 0
1283 /* Handle the case in which you cannot send a
1284 signal to the writer, so kill fails and sets
1285 errno to EPERM. */
1286 && errno != EPERM);
1288 if (!writer_is_dead && xnanosleep (sleep_interval))
1289 error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
1295 #if HAVE_INOTIFY
1297 /* Return true if any of the N_FILES files in F is remote, i.e., has
1298 an open file descriptor and is on a network file system. */
1300 static bool
1301 any_remote_file (const struct File_spec *f, size_t n_files)
1303 for (size_t i = 0; i < n_files; i++)
1304 if (0 <= f[i].fd && f[i].remote)
1305 return true;
1306 return false;
1309 /* Return true if any of the N_FILES files in F is non remote, i.e., has
1310 an open file descriptor and is not on a network file system. */
1312 static bool
1313 any_non_remote_file (const struct File_spec *f, size_t n_files)
1315 for (size_t i = 0; i < n_files; i++)
1316 if (0 <= f[i].fd && ! f[i].remote)
1317 return true;
1318 return false;
1321 /* Return true if any of the N_FILES files in F is a symlink.
1322 Note we don't worry about the edge case where "-" exists,
1323 since that will have the same consequences for inotify,
1324 which is the only context this function is currently used. */
1326 static bool
1327 any_symlinks (const struct File_spec *f, size_t n_files)
1329 struct stat st;
1330 for (size_t i = 0; i < n_files; i++)
1331 if (lstat (f[i].name, &st) == 0 && S_ISLNK (st.st_mode))
1332 return true;
1333 return false;
1336 /* Return true if any of the N_FILES files in F is not
1337 a regular file or fifo. This is used to avoid adding inotify
1338 watches on a device file for example, which inotify
1339 will accept, but not give any events for. */
1341 static bool
1342 any_non_regular_fifo (const struct File_spec *f, size_t n_files)
1344 for (size_t i = 0; i < n_files; i++)
1345 if (0 <= f[i].fd && ! S_ISREG (f[i].mode) && ! S_ISFIFO (f[i].mode))
1346 return true;
1347 return false;
1350 /* Return true if any of the N_FILES files in F represents
1351 stdin and is tailable. */
1353 static bool
1354 tailable_stdin (const struct File_spec *f, size_t n_files)
1356 for (size_t i = 0; i < n_files; i++)
1357 if (!f[i].ignore && STREQ (f[i].name, "-"))
1358 return true;
1359 return false;
1362 static size_t
1363 wd_hasher (const void *entry, size_t tabsize)
1365 const struct File_spec *spec = entry;
1366 return spec->wd % tabsize;
1369 static bool
1370 wd_comparator (const void *e1, const void *e2)
1372 const struct File_spec *spec1 = e1;
1373 const struct File_spec *spec2 = e2;
1374 return spec1->wd == spec2->wd;
1377 /* Output (new) data for FSPEC->fd.
1378 PREV_FSPEC records the last File_spec for which we output. */
1379 static void
1380 check_fspec (struct File_spec *fspec, struct File_spec **prev_fspec)
1382 struct stat stats;
1383 char const *name;
1385 if (fspec->fd == -1)
1386 return;
1388 name = pretty_name (fspec);
1390 if (fstat (fspec->fd, &stats) != 0)
1392 fspec->errnum = errno;
1393 close_fd (fspec->fd, name);
1394 fspec->fd = -1;
1395 return;
1398 /* XXX: This is only a heuristic, as the file may have also
1399 been truncated and written to if st_size >= size
1400 (in which case we ignore new data <= size).
1401 Though in the inotify case it's more likely we'll get
1402 separate events for truncate() and write(). */
1403 if (S_ISREG (fspec->mode) && stats.st_size < fspec->size)
1405 error (0, 0, _("%s: file truncated"), quotef (name));
1406 xlseek (fspec->fd, 0, SEEK_SET, name);
1407 fspec->size = 0;
1409 else if (S_ISREG (fspec->mode) && stats.st_size == fspec->size
1410 && timespec_cmp (fspec->mtime, get_stat_mtime (&stats)) == 0)
1411 return;
1413 bool want_header = print_headers && (fspec != *prev_fspec);
1415 uintmax_t bytes_read = dump_remainder (want_header, name, fspec->fd,
1416 COPY_TO_EOF);
1417 fspec->size += bytes_read;
1419 if (bytes_read)
1421 *prev_fspec = fspec;
1422 if (fflush (stdout) != 0)
1423 write_error ();
1427 /* Attempt to tail N_FILES files forever, or until killed.
1428 Check modifications using the inotify events system.
1429 Exit if finished or on fatal error; return to revert to polling. */
1430 static void
1431 tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
1432 double sleep_interval, Hash_table **wd_to_namep)
1434 # if TAIL_TEST_SLEEP
1435 /* Delay between open() and inotify_add_watch()
1436 to help trigger different cases. */
1437 xnanosleep (1000000);
1438 # endif
1439 unsigned int max_realloc = 3;
1441 /* Map an inotify watch descriptor to the name of the file it's watching. */
1442 Hash_table *wd_to_name;
1444 bool found_watchable_file = false;
1445 bool tailed_but_unwatchable = false;
1446 bool found_unwatchable_dir = false;
1447 bool no_inotify_resources = false;
1448 bool writer_is_dead = false;
1449 struct File_spec *prev_fspec;
1450 size_t evlen = 0;
1451 char *evbuf;
1452 size_t evbuf_off = 0;
1453 size_t len = 0;
1455 wd_to_name = hash_initialize (n_files, nullptr, wd_hasher, wd_comparator,
1456 nullptr);
1457 if (! wd_to_name)
1458 xalloc_die ();
1459 *wd_to_namep = wd_to_name;
1461 /* The events mask used with inotify on files (not directories). */
1462 uint32_t inotify_wd_mask = IN_MODIFY;
1463 /* TODO: Perhaps monitor these events in Follow_descriptor mode also,
1464 to tag reported file names with "deleted", "moved" etc. */
1465 if (follow_mode == Follow_name)
1466 inotify_wd_mask |= (IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF);
1468 /* Add an inotify watch for each watched file. If -F is specified then watch
1469 its parent directory too, in this way when they re-appear we can add them
1470 again to the watch list. */
1471 size_t i;
1472 for (i = 0; i < n_files; i++)
1474 if (!f[i].ignore)
1476 size_t fnlen = strlen (f[i].name);
1477 if (evlen < fnlen)
1478 evlen = fnlen;
1480 f[i].wd = -1;
1482 if (follow_mode == Follow_name)
1484 size_t dirlen = dir_len (f[i].name);
1485 char prev = f[i].name[dirlen];
1486 f[i].basename_start = last_component (f[i].name) - f[i].name;
1488 f[i].name[dirlen] = '\0';
1490 /* It's fine to add the same directory more than once.
1491 In that case the same watch descriptor is returned. */
1492 f[i].parent_wd = inotify_add_watch (wd, dirlen ? f[i].name : ".",
1493 (IN_CREATE | IN_DELETE
1494 | IN_MOVED_TO | IN_ATTRIB
1495 | IN_DELETE_SELF));
1497 f[i].name[dirlen] = prev;
1499 if (f[i].parent_wd < 0)
1501 if (errno != ENOSPC) /* suppress confusing error. */
1502 error (0, errno, _("cannot watch parent directory of %s"),
1503 quoteaf (f[i].name));
1504 else
1505 error (0, 0, _("inotify resources exhausted"));
1506 found_unwatchable_dir = true;
1507 /* We revert to polling below. Note invalid uses
1508 of the inotify API will still be diagnosed. */
1509 break;
1513 f[i].wd = inotify_add_watch (wd, f[i].name, inotify_wd_mask);
1515 if (f[i].wd < 0)
1517 if (f[i].fd != -1) /* already tailed. */
1518 tailed_but_unwatchable = true;
1519 if (errno == ENOSPC || errno == ENOMEM)
1521 no_inotify_resources = true;
1522 error (0, 0, _("inotify resources exhausted"));
1523 break;
1525 else if (errno != f[i].errnum)
1526 error (0, errno, _("cannot watch %s"), quoteaf (f[i].name));
1527 continue;
1530 if (hash_insert (wd_to_name, &(f[i])) == nullptr)
1531 xalloc_die ();
1533 found_watchable_file = true;
1537 /* Linux kernel 2.6.24 at least has a bug where eventually, ENOSPC is always
1538 returned by inotify_add_watch. In any case we should revert to polling
1539 when there are no inotify resources. Also a specified directory may not
1540 be currently present or accessible, so revert to polling. Also an already
1541 tailed but unwatchable due rename/unlink race, should also revert. */
1542 if (no_inotify_resources || found_unwatchable_dir
1543 || (follow_mode == Follow_descriptor && tailed_but_unwatchable))
1544 return;
1545 if (follow_mode == Follow_descriptor && !found_watchable_file)
1546 exit (EXIT_FAILURE);
1548 prev_fspec = &(f[n_files - 1]);
1550 /* Check files again. New files or data can be available since last time we
1551 checked and before they are watched by inotify. */
1552 for (i = 0; i < n_files; i++)
1554 if (! f[i].ignore)
1556 /* check for new files. */
1557 if (follow_mode == Follow_name)
1558 recheck (&(f[i]), false);
1559 else if (f[i].fd != -1)
1561 /* If the file was replaced in the small window since we tailed,
1562 then assume the watch is on the wrong item (different to
1563 that we've already produced output for), and so revert to
1564 polling the original descriptor. */
1565 struct stat stats;
1567 if (stat (f[i].name, &stats) == 0
1568 && (f[i].dev != stats.st_dev || f[i].ino != stats.st_ino))
1570 error (0, errno, _("%s was replaced"),
1571 quoteaf (pretty_name (&(f[i]))));
1572 return;
1576 /* check for new data. */
1577 check_fspec (&f[i], &prev_fspec);
1581 evlen += sizeof (struct inotify_event) + 1;
1582 evbuf = xmalloc (evlen);
1584 /* Wait for inotify events and handle them. Events on directories
1585 ensure that watched files can be re-added when following by name.
1586 This loop blocks on the 'safe_read' call until a new event is notified.
1587 But when --pid=P is specified, tail usually waits via poll. */
1588 while (true)
1590 struct File_spec *fspec;
1591 struct inotify_event *ev;
1592 void *void_ev;
1594 /* When following by name without --retry, and the last file has
1595 been unlinked or renamed-away, diagnose it and return. */
1596 if (follow_mode == Follow_name
1597 && ! reopen_inaccessible_files
1598 && hash_get_n_entries (wd_to_name) == 0)
1599 error (EXIT_FAILURE, 0, _("no files remaining"));
1601 if (len <= evbuf_off)
1603 /* Poll for inotify events. When watching a PID, ensure
1604 that a read from WD will not block indefinitely.
1605 If MONITOR_OUTPUT, also poll for a broken output pipe. */
1607 int file_change;
1608 struct pollfd pfd[2];
1611 /* How many ms to wait for changes. -1 means wait forever. */
1612 int delay = -1;
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 || sleep_interval <= 0)
1622 delay = 0;
1623 else if (sleep_interval < INT_MAX / 1000 - 1)
1625 /* delay = ceil (sleep_interval * 1000), sans libm. */
1626 double ddelay = sleep_interval * 1000;
1627 delay = ddelay;
1628 delay += delay < ddelay;
1632 pfd[0].fd = wd;
1633 pfd[0].events = POLLIN;
1634 pfd[1].fd = STDOUT_FILENO;
1635 pfd[1].events = pfd[1].revents = 0;
1636 file_change = poll (pfd, monitor_output + 1, delay);
1638 while (file_change == 0);
1640 if (file_change < 0)
1641 error (EXIT_FAILURE, errno,
1642 _("error waiting for inotify and output events"));
1643 if (pfd[1].revents)
1644 die_pipe ();
1646 len = safe_read (wd, evbuf, evlen);
1647 evbuf_off = 0;
1649 /* For kernels prior to 2.6.21, read returns 0 when the buffer
1650 is too small. */
1651 if ((len == 0 || (len == SAFE_READ_ERROR && errno == EINVAL))
1652 && max_realloc--)
1654 len = 0;
1655 evlen *= 2;
1656 evbuf = xrealloc (evbuf, evlen);
1657 continue;
1660 if (len == 0 || len == SAFE_READ_ERROR)
1661 error (EXIT_FAILURE, errno, _("error reading inotify event"));
1664 void_ev = evbuf + evbuf_off;
1665 ev = void_ev;
1666 evbuf_off += sizeof (*ev) + ev->len;
1668 /* If a directory is deleted, IN_DELETE_SELF is emitted
1669 with ev->name of length 0.
1670 We need to catch it, otherwise it would wait forever,
1671 as wd for directory becomes inactive. Revert to polling now. */
1672 if ((ev->mask & IN_DELETE_SELF) && ! ev->len)
1674 for (i = 0; i < n_files; i++)
1676 if (ev->wd == f[i].parent_wd)
1678 error (0, 0,
1679 _("directory containing watched file was removed"));
1680 return;
1685 if (ev->len) /* event on ev->name in watched directory. */
1687 size_t j;
1688 for (j = 0; j < n_files; j++)
1690 /* With N=hundreds of frequently-changing files, this O(N^2)
1691 process might be a problem. FIXME: use a hash table? */
1692 if (f[j].parent_wd == ev->wd
1693 && STREQ (ev->name, f[j].name + f[j].basename_start))
1694 break;
1697 /* It is not a watched file. */
1698 if (j == n_files)
1699 continue;
1701 fspec = &(f[j]);
1703 int new_wd = -1;
1704 bool deleting = !! (ev->mask & IN_DELETE);
1706 if (! deleting)
1708 /* Adding the same inode again will look up any existing wd. */
1709 new_wd = inotify_add_watch (wd, f[j].name, inotify_wd_mask);
1712 if (! deleting && new_wd < 0)
1714 if (errno == ENOSPC || errno == ENOMEM)
1716 error (0, 0, _("inotify resources exhausted"));
1717 return; /* revert to polling. */
1719 else
1721 /* Can get ENOENT for a dangling symlink for example. */
1722 error (0, errno, _("cannot watch %s"), quoteaf (f[j].name));
1724 /* We'll continue below after removing the existing watch. */
1727 /* This will be false if only attributes of file change. */
1728 bool new_watch;
1729 new_watch = (! deleting) && (fspec->wd < 0 || new_wd != fspec->wd);
1731 if (new_watch)
1733 if (0 <= fspec->wd)
1735 inotify_rm_watch (wd, fspec->wd);
1736 hash_remove (wd_to_name, fspec);
1739 fspec->wd = new_wd;
1741 if (new_wd == -1)
1742 continue;
1744 /* If the file was moved then inotify will use the source file wd
1745 for the destination file. Make sure the key is not present in
1746 the table. */
1747 struct File_spec *prev = hash_remove (wd_to_name, fspec);
1748 if (prev && prev != fspec)
1750 if (follow_mode == Follow_name)
1751 recheck (prev, false);
1752 prev->wd = -1;
1753 close_fd (prev->fd, pretty_name (prev));
1756 if (hash_insert (wd_to_name, fspec) == nullptr)
1757 xalloc_die ();
1760 if (follow_mode == Follow_name)
1761 recheck (fspec, false);
1763 else
1765 struct File_spec key;
1766 key.wd = ev->wd;
1767 fspec = hash_lookup (wd_to_name, &key);
1770 if (! fspec)
1771 continue;
1773 if (ev->mask & (IN_ATTRIB | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF))
1775 /* Note for IN_MOVE_SELF (the file we're watching has
1776 been clobbered via a rename) we leave the watch
1777 in place since it may still be part of the set
1778 of watched names. */
1779 if (ev->mask & IN_DELETE_SELF)
1781 inotify_rm_watch (wd, fspec->wd);
1782 hash_remove (wd_to_name, fspec);
1785 /* Note we get IN_ATTRIB for unlink() as st_nlink decrements.
1786 The usual path is a close() done in recheck() triggers
1787 an IN_DELETE_SELF event as the inode is removed.
1788 However sometimes open() will succeed as even though
1789 st_nlink is decremented, the dentry (cache) is not updated.
1790 Thus we depend on the IN_DELETE event on the directory
1791 to trigger processing for the removed file. */
1793 recheck (fspec, false);
1795 continue;
1797 check_fspec (fspec, &prev_fspec);
1800 #endif
1802 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
1803 Return true if successful. */
1805 static bool
1806 tail_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
1807 uintmax_t *read_pos)
1809 struct stat stats;
1811 if (fstat (fd, &stats))
1813 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1814 return false;
1817 if (from_start)
1819 if (! presume_input_pipe && n_bytes <= OFF_T_MAX
1820 && ((S_ISREG (stats.st_mode)
1821 && xlseek (fd, n_bytes, SEEK_CUR, pretty_filename) >= 0)
1822 || lseek (fd, n_bytes, SEEK_CUR) != -1))
1823 *read_pos += n_bytes;
1824 else
1826 int t = start_bytes (pretty_filename, fd, n_bytes, read_pos);
1827 if (t)
1828 return t < 0;
1830 n_bytes = COPY_TO_EOF;
1832 else
1834 off_t end_pos = -1;
1835 off_t current_pos = -1;
1837 if (! presume_input_pipe && n_bytes <= OFF_T_MAX)
1839 if (usable_st_size (&stats))
1840 end_pos = stats.st_size;
1841 else if ((current_pos = lseek (fd, -n_bytes, SEEK_END)) != -1)
1842 end_pos = current_pos + n_bytes;
1844 if (end_pos <= (off_t) ST_BLKSIZE (stats))
1845 return pipe_bytes (pretty_filename, fd, n_bytes, read_pos);
1846 if (current_pos == -1)
1847 current_pos = xlseek (fd, 0, SEEK_CUR, pretty_filename);
1848 if (current_pos < end_pos)
1850 off_t bytes_remaining = end_pos - current_pos;
1852 if (n_bytes < bytes_remaining)
1854 current_pos = end_pos - n_bytes;
1855 xlseek (fd, current_pos, SEEK_SET, pretty_filename);
1858 *read_pos = current_pos;
1861 *read_pos += dump_remainder (false, pretty_filename, fd, n_bytes);
1862 return true;
1865 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
1866 Return true if successful. */
1868 static bool
1869 tail_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
1870 uintmax_t *read_pos)
1872 struct stat stats;
1874 if (fstat (fd, &stats))
1876 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1877 return false;
1880 if (from_start)
1882 int t = start_lines (pretty_filename, fd, n_lines, read_pos);
1883 if (t)
1884 return t < 0;
1885 *read_pos += dump_remainder (false, pretty_filename, fd, COPY_TO_EOF);
1887 else
1889 off_t start_pos = -1;
1890 off_t end_pos;
1892 /* Use file_lines only if FD refers to a regular file for
1893 which lseek (... SEEK_END) works. */
1894 if ( ! presume_input_pipe
1895 && S_ISREG (stats.st_mode)
1896 && (start_pos = lseek (fd, 0, SEEK_CUR)) != -1
1897 && start_pos < (end_pos = lseek (fd, 0, SEEK_END)))
1899 *read_pos = end_pos;
1900 if (end_pos != 0
1901 && ! file_lines (pretty_filename, fd, n_lines,
1902 start_pos, end_pos, read_pos))
1903 return false;
1905 else
1907 /* Under very unlikely circumstances, it is possible to reach
1908 this point after positioning the file pointer to end of file
1909 via the 'lseek (...SEEK_END)' above. In that case, reposition
1910 the file pointer back to start_pos before calling pipe_lines. */
1911 if (start_pos != -1)
1912 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
1914 return pipe_lines (pretty_filename, fd, n_lines, read_pos);
1917 return true;
1920 /* Display the last N_UNITS units of file FILENAME, open for reading
1921 via FD. Set *READ_POS to the position of the input stream pointer.
1922 *READ_POS is usually the number of bytes read and corresponds to an
1923 offset from the beginning of a file. However, it may be larger than
1924 OFF_T_MAX (as for an input pipe), and may also be larger than the
1925 number of bytes read (when an input pointer is initially not at
1926 beginning of file), and may be far greater than the number of bytes
1927 actually read for an input file that is seekable.
1928 Return true if successful. */
1930 static bool
1931 tail (char const *filename, int fd, uintmax_t n_units,
1932 uintmax_t *read_pos)
1934 *read_pos = 0;
1935 if (count_lines)
1936 return tail_lines (filename, fd, n_units, read_pos);
1937 else
1938 return tail_bytes (filename, fd, n_units, read_pos);
1941 /* Display the last N_UNITS units of the file described by F.
1942 Return true if successful. */
1944 static bool
1945 tail_file (struct File_spec *f, uintmax_t n_units)
1947 int fd;
1948 bool ok;
1950 bool is_stdin = (STREQ (f->name, "-"));
1952 if (is_stdin)
1954 have_read_stdin = true;
1955 fd = STDIN_FILENO;
1956 xset_binary_mode (STDIN_FILENO, O_BINARY);
1958 else
1959 fd = open (f->name, O_RDONLY | O_BINARY);
1961 f->tailable = !(reopen_inaccessible_files && fd == -1);
1963 if (fd == -1)
1965 if (forever)
1967 f->fd = -1;
1968 f->errnum = errno;
1969 f->ignore = ! reopen_inaccessible_files;
1970 f->ino = 0;
1971 f->dev = 0;
1973 error (0, errno, _("cannot open %s for reading"),
1974 quoteaf (pretty_name (f)));
1975 ok = false;
1977 else
1979 uintmax_t read_pos;
1981 if (print_headers)
1982 write_header (pretty_name (f));
1983 ok = tail (pretty_name (f), fd, n_units, &read_pos);
1984 if (forever)
1986 struct stat stats;
1988 #if TAIL_TEST_SLEEP
1989 /* Before the tail function provided 'read_pos', there was
1990 a race condition described in the URL below. This sleep
1991 call made the window big enough to exercise the problem. */
1992 xnanosleep (1);
1993 #endif
1994 f->errnum = ok - 1;
1995 if (fstat (fd, &stats) < 0)
1997 ok = false;
1998 f->errnum = errno;
1999 error (0, errno, _("error reading %s"),
2000 quoteaf (pretty_name (f)));
2002 else if (!IS_TAILABLE_FILE_TYPE (stats.st_mode))
2004 ok = false;
2005 f->errnum = -1;
2006 f->tailable = false;
2007 f->ignore = ! reopen_inaccessible_files;
2008 error (0, 0, _("%s: cannot follow end of this type of file%s"),
2009 quotef (pretty_name (f)),
2010 f->ignore ? _("; giving up on this name") : "");
2013 if (!ok)
2015 f->ignore = ! reopen_inaccessible_files;
2016 close_fd (fd, pretty_name (f));
2017 f->fd = -1;
2019 else
2021 /* Note: we must use read_pos here, not stats.st_size,
2022 to avoid a race condition described by Ken Raeburn:
2023 https://lists.gnu.org/r/bug-textutils/2003-05/msg00007.html */
2024 record_open_fd (f, fd, read_pos, &stats, (is_stdin ? -1 : 1));
2025 f->remote = fremote (fd, pretty_name (f));
2028 else
2030 if (!is_stdin && close (fd))
2032 error (0, errno, _("error reading %s"),
2033 quoteaf (pretty_name (f)));
2034 ok = false;
2039 return ok;
2042 /* If obsolete usage is allowed, and the command line arguments are of
2043 the obsolete form and the option string is well-formed, set
2044 *N_UNITS, the globals COUNT_LINES, FOREVER, and FROM_START, and
2045 return true. If the command line arguments are obviously incorrect
2046 (e.g., because obsolete usage is not allowed and the arguments are
2047 incorrect for non-obsolete usage), report an error and exit.
2048 Otherwise, return false and don't modify any parameter or global
2049 variable. */
2051 static bool
2052 parse_obsolete_option (int argc, char * const *argv, uintmax_t *n_units)
2054 char const *p;
2055 char const *n_string;
2056 char const *n_string_end;
2057 int default_count = DEFAULT_N_LINES;
2058 bool t_from_start;
2059 bool t_count_lines = true;
2060 bool t_forever = false;
2062 /* With the obsolete form, there is one option string and at most
2063 one file argument. Watch out for "-" and "--", though. */
2064 if (! (argc == 2
2065 || (argc == 3 && ! (argv[2][0] == '-' && argv[2][1]))
2066 || (3 <= argc && argc <= 4 && STREQ (argv[2], "--"))))
2067 return false;
2069 int posix_ver = posix2_version ();
2070 bool obsolete_usage = posix_ver < 200112;
2071 bool traditional_usage = obsolete_usage || 200809 <= posix_ver;
2072 p = argv[1];
2074 switch (*p++)
2076 default:
2077 return false;
2079 case '+':
2080 /* Leading "+" is a file name in the standard form. */
2081 if (!traditional_usage)
2082 return false;
2084 t_from_start = true;
2085 break;
2087 case '-':
2088 /* In the non-obsolete form, "-" is standard input and "-c"
2089 requires an option-argument. The obsolete multidigit options
2090 are supported as a GNU extension even when conforming to
2091 POSIX 1003.1-2001 or later, so don't complain about them. */
2092 if (!obsolete_usage && !p[p[0] == 'c'])
2093 return false;
2095 t_from_start = false;
2096 break;
2099 n_string = p;
2100 while (ISDIGIT (*p))
2101 p++;
2102 n_string_end = p;
2104 switch (*p)
2106 case 'b': default_count *= 512; FALLTHROUGH;
2107 case 'c': t_count_lines = false; FALLTHROUGH;
2108 case 'l': p++; break;
2111 if (*p == 'f')
2113 t_forever = true;
2114 ++p;
2117 if (*p)
2118 return false;
2120 if (n_string == n_string_end)
2121 *n_units = default_count;
2122 else if ((xstrtoumax (n_string, nullptr, 10, n_units, "b")
2123 & ~LONGINT_INVALID_SUFFIX_CHAR)
2124 != LONGINT_OK)
2125 error (EXIT_FAILURE, errno, "%s: %s", _("invalid number"),
2126 quote (argv[1]));
2128 /* Set globals. */
2129 from_start = t_from_start;
2130 count_lines = t_count_lines;
2131 forever = t_forever;
2133 return true;
2136 static void
2137 parse_options (int argc, char **argv,
2138 uintmax_t *n_units, enum header_mode *header_mode,
2139 double *sleep_interval)
2141 int c;
2143 while ((c = getopt_long (argc, argv, "c:n:fFqs:vz0123456789",
2144 long_options, nullptr))
2145 != -1)
2147 switch (c)
2149 case 'F':
2150 forever = true;
2151 follow_mode = Follow_name;
2152 reopen_inaccessible_files = true;
2153 break;
2155 case 'c':
2156 case 'n':
2157 count_lines = (c == 'n');
2158 if (*optarg == '+')
2159 from_start = true;
2160 else if (*optarg == '-')
2161 ++optarg;
2163 *n_units = xdectoumax (optarg, 0, UINTMAX_MAX, "bkKmMGTPEZYRQ0",
2164 count_lines
2165 ? _("invalid number of lines")
2166 : _("invalid number of bytes"), 0);
2167 break;
2169 case 'f':
2170 case LONG_FOLLOW_OPTION:
2171 forever = true;
2172 if (optarg == nullptr)
2173 follow_mode = DEFAULT_FOLLOW_MODE;
2174 else
2175 follow_mode = XARGMATCH ("--follow", optarg,
2176 follow_mode_string, follow_mode_map);
2177 break;
2179 case RETRY_OPTION:
2180 reopen_inaccessible_files = true;
2181 break;
2183 case MAX_UNCHANGED_STATS_OPTION:
2184 /* --max-unchanged-stats=N */
2185 max_n_unchanged_stats_between_opens =
2186 xdectoumax (optarg, 0, UINTMAX_MAX, "",
2187 _("invalid maximum number of unchanged stats between opens"), 0);
2188 break;
2190 case DISABLE_INOTIFY_OPTION:
2191 disable_inotify = true;
2192 break;
2194 case PID_OPTION:
2195 pid = xdectoumax (optarg, 0, PID_T_MAX, "", _("invalid PID"), 0);
2196 break;
2198 case PRESUME_INPUT_PIPE_OPTION:
2199 presume_input_pipe = true;
2200 break;
2202 case 'q':
2203 *header_mode = never;
2204 break;
2206 case 's':
2208 double s;
2209 if (! (xstrtod (optarg, nullptr, &s, cl_strtod) && 0 <= s))
2210 error (EXIT_FAILURE, 0,
2211 _("invalid number of seconds: %s"), quote (optarg));
2212 *sleep_interval = s;
2214 break;
2216 case 'v':
2217 *header_mode = always;
2218 break;
2220 case 'z':
2221 line_end = '\0';
2222 break;
2224 case_GETOPT_HELP_CHAR;
2226 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
2228 case '0': case '1': case '2': case '3': case '4':
2229 case '5': case '6': case '7': case '8': case '9':
2230 error (EXIT_FAILURE, 0, _("option used in invalid context -- %c"), c);
2232 default:
2233 usage (EXIT_FAILURE);
2237 if (reopen_inaccessible_files)
2239 if (!forever)
2241 reopen_inaccessible_files = false;
2242 error (0, 0, _("warning: --retry ignored; --retry is useful"
2243 " only when following"));
2245 else if (follow_mode == Follow_descriptor)
2246 error (0, 0, _("warning: --retry only effective for the initial open"));
2249 if (pid && !forever)
2250 error (0, 0,
2251 _("warning: PID ignored; --pid=PID is useful only when following"));
2252 else if (pid && kill (pid, 0) != 0 && errno == ENOSYS)
2254 error (0, 0, _("warning: --pid=PID is not supported on this system"));
2255 pid = 0;
2259 /* Mark as '.ignore'd each member of F that corresponds to a
2260 pipe or fifo, and return the number of non-ignored members. */
2261 static size_t
2262 ignore_fifo_and_pipe (struct File_spec *f, size_t n_files)
2264 /* When there is no FILE operand and stdin is a pipe or FIFO
2265 POSIX requires that tail ignore the -f option.
2266 Since we allow multiple FILE operands, we extend that to say: with -f,
2267 ignore any "-" operand that corresponds to a pipe or FIFO. */
2268 size_t n_viable = 0;
2270 for (size_t i = 0; i < n_files; i++)
2272 bool is_a_fifo_or_pipe =
2273 (STREQ (f[i].name, "-")
2274 && !f[i].ignore
2275 && 0 <= f[i].fd
2276 && (S_ISFIFO (f[i].mode)
2277 || (HAVE_FIFO_PIPES != 1 && isapipe (f[i].fd))));
2278 if (is_a_fifo_or_pipe)
2280 f[i].fd = -1;
2281 f[i].ignore = true;
2283 else
2284 ++n_viable;
2287 return n_viable;
2291 main (int argc, char **argv)
2293 enum header_mode header_mode = multiple_files;
2294 bool ok = true;
2295 /* If from_start, the number of items to skip before printing; otherwise,
2296 the number of items at the end of the file to print. Although the type
2297 is signed, the value is never negative. */
2298 uintmax_t n_units = DEFAULT_N_LINES;
2299 size_t n_files;
2300 char **file;
2301 struct File_spec *F;
2302 size_t i;
2303 bool obsolete_option;
2305 /* The number of seconds to sleep between iterations.
2306 During one iteration, every file name or descriptor is checked to
2307 see if it has changed. */
2308 double sleep_interval = 1.0;
2310 initialize_main (&argc, &argv);
2311 set_program_name (argv[0]);
2312 setlocale (LC_ALL, "");
2313 bindtextdomain (PACKAGE, LOCALEDIR);
2314 textdomain (PACKAGE);
2316 atexit (close_stdout);
2318 have_read_stdin = false;
2320 count_lines = true;
2321 forever = from_start = print_headers = false;
2322 line_end = '\n';
2323 obsolete_option = parse_obsolete_option (argc, argv, &n_units);
2324 argc -= obsolete_option;
2325 argv += obsolete_option;
2326 parse_options (argc, argv, &n_units, &header_mode, &sleep_interval);
2328 /* To start printing with item N_UNITS from the start of the file, skip
2329 N_UNITS - 1 items. 'tail -n +0' is actually meaningless, but for Unix
2330 compatibility it's treated the same as 'tail -n +1'. */
2331 if (from_start)
2333 if (n_units)
2334 --n_units;
2337 if (optind < argc)
2339 n_files = argc - optind;
2340 file = argv + optind;
2342 else
2344 static char *dummy_stdin = (char *) "-";
2345 n_files = 1;
2346 file = &dummy_stdin;
2350 bool found_hyphen = false;
2352 for (i = 0; i < n_files; i++)
2353 if (STREQ (file[i], "-"))
2354 found_hyphen = true;
2356 /* When following by name, there must be a name. */
2357 if (found_hyphen && follow_mode == Follow_name)
2358 error (EXIT_FAILURE, 0, _("cannot follow %s by name"), quoteaf ("-"));
2360 /* When following forever, and not using simple blocking, warn if
2361 any file is '-' as the stats() used to check for input are ineffective.
2362 This is only a warning, since tail's output (before a failing seek,
2363 and that from any non-stdin files) might still be useful. */
2364 if (forever && found_hyphen)
2366 struct stat in_stat;
2367 bool blocking_stdin;
2368 blocking_stdin = (pid == 0 && follow_mode == Follow_descriptor
2369 && n_files == 1 && ! fstat (STDIN_FILENO, &in_stat)
2370 && ! S_ISREG (in_stat.st_mode));
2372 if (! blocking_stdin && isatty (STDIN_FILENO))
2373 error (0, 0, _("warning: following standard input"
2374 " indefinitely is ineffective"));
2378 /* Don't read anything if we'll never output anything. */
2379 if (! n_units && ! forever && ! from_start)
2380 return EXIT_SUCCESS;
2382 F = xnmalloc (n_files, sizeof *F);
2383 for (i = 0; i < n_files; i++)
2384 F[i].name = file[i];
2386 if (header_mode == always
2387 || (header_mode == multiple_files && n_files > 1))
2388 print_headers = true;
2390 xset_binary_mode (STDOUT_FILENO, O_BINARY);
2392 for (i = 0; i < n_files; i++)
2393 ok &= tail_file (&F[i], n_units);
2395 if (forever && ignore_fifo_and_pipe (F, n_files))
2397 /* If stdout is a fifo or pipe, then monitor it
2398 so that we exit if the reader goes away. */
2399 struct stat out_stat;
2400 if (fstat (STDOUT_FILENO, &out_stat) < 0)
2401 error (EXIT_FAILURE, errno, _("standard output"));
2402 monitor_output = (S_ISFIFO (out_stat.st_mode)
2403 || (HAVE_FIFO_PIPES != 1 && isapipe (STDOUT_FILENO)));
2405 #if HAVE_INOTIFY
2406 /* tailable_stdin() checks if the user specifies stdin via "-",
2407 or implicitly by providing no arguments. If so, we won't use inotify.
2408 Technically, on systems with a working /dev/stdin, we *could*,
2409 but would it be worth it? Verifying that it's a real device
2410 and hooked up to stdin is not trivial, while reverting to
2411 non-inotify-based tail_forever is easy and portable.
2413 any_remote_file() checks if the user has specified any
2414 files that reside on remote file systems. inotify is not used
2415 in this case because it would miss any updates to the file
2416 that were not initiated from the local system.
2418 any_non_remote_file() checks if the user has specified any
2419 files that don't reside on remote file systems. inotify is not used
2420 if there are no open files, as we can't determine if those file
2421 will be on a remote file system.
2423 any_symlinks() checks if the user has specified any symbolic links.
2424 inotify is not used in this case because it returns updated _targets_
2425 which would not match the specified names. If we tried to always
2426 use the target names, then we would miss changes to the symlink itself.
2428 ok is false when one of the files specified could not be opened for
2429 reading. In this case and when following by descriptor,
2430 tail_forever_inotify() cannot be used (in its current implementation).
2432 FIXME: inotify doesn't give any notification when a new
2433 (remote) file or directory is mounted on top a watched file.
2434 When follow_mode == Follow_name we would ideally like to detect that.
2435 Note if there is a change to the original file then we'll
2436 recheck it and follow the new file, or ignore it if the
2437 file has changed to being remote.
2439 FIXME-maybe: inotify has a watch descriptor per inode, and hence with
2440 our current hash implementation will only --follow data for one
2441 of the names when multiple hardlinked files are specified, or
2442 for one name when a name is specified multiple times. */
2443 if (!disable_inotify && (tailable_stdin (F, n_files)
2444 || any_remote_file (F, n_files)
2445 || ! any_non_remote_file (F, n_files)
2446 || any_symlinks (F, n_files)
2447 || any_non_regular_fifo (F, n_files)
2448 || (!ok && follow_mode == Follow_descriptor)))
2449 disable_inotify = true;
2451 if (!disable_inotify)
2453 int wd = inotify_init ();
2454 if (0 <= wd)
2456 /* Flush any output from tail_file, now, since
2457 tail_forever_inotify flushes only after writing,
2458 not before reading. */
2459 if (fflush (stdout) != 0)
2460 write_error ();
2462 Hash_table *ht;
2463 tail_forever_inotify (wd, F, n_files, sleep_interval, &ht);
2464 hash_free (ht);
2465 close (wd);
2466 errno = 0;
2468 error (0, errno, _("inotify cannot be used, reverting to polling"));
2470 #endif
2471 disable_inotify = true;
2472 tail_forever (F, n_files, sleep_interval);
2475 if (have_read_stdin && close (STDIN_FILENO) < 0)
2476 error (EXIT_FAILURE, errno, "-");
2477 main_exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);