dd: synchronize output after write errors
[coreutils.git] / src / tail.c
blobfe564797e98765b416139c95ee067da69d975672
1 /* tail -- output the last part of file(s)
2 Copyright (C) 1989-2022 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/select.h>
32 #include <sys/types.h>
33 #include <signal.h>
35 #include "system.h"
36 #include "argmatch.h"
37 #include "cl-strtod.h"
38 #include "die.h"
39 #include "error.h"
40 #include "fcntl--.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 <sys/inotify.h>
56 #endif
58 #if defined _AIX || HAVE_INOTIFY
59 # include <poll.h>
60 #endif
62 /* Linux can optimize the handling of local files. */
63 #if defined __linux__ || defined __ANDROID__
64 # include "fs.h"
65 # include "fs-is-local.h"
66 # if HAVE_SYS_STATFS_H
67 # include <sys/statfs.h>
68 # elif HAVE_SYS_VFS_H
69 # include <sys/vfs.h>
70 # endif
71 #endif
73 /* The official name of this program (e.g., no 'g' prefix). */
74 #define PROGRAM_NAME "tail"
76 #define AUTHORS \
77 proper_name ("Paul Rubin"), \
78 proper_name ("David MacKenzie"), \
79 proper_name ("Ian Lance Taylor"), \
80 proper_name ("Jim Meyering")
82 /* Number of items to tail. */
83 #define DEFAULT_N_LINES 10
85 /* Special values for dump_remainder's N_BYTES parameter. */
86 #define COPY_TO_EOF UINTMAX_MAX
87 #define COPY_A_BUFFER (UINTMAX_MAX - 1)
89 /* FIXME: make Follow_name the default? */
90 #define DEFAULT_FOLLOW_MODE Follow_descriptor
92 enum Follow_mode
94 /* Follow the name of each file: if the file is renamed, try to reopen
95 that name and track the end of the new file if/when it's recreated.
96 This is useful for tracking logs that are occasionally rotated. */
97 Follow_name = 1,
99 /* Follow each descriptor obtained upon opening a file.
100 That means we'll continue to follow the end of a file even after
101 it has been renamed or unlinked. */
102 Follow_descriptor = 2
105 /* The types of files for which tail works. */
106 #define IS_TAILABLE_FILE_TYPE(Mode) \
107 (S_ISREG (Mode) || S_ISFIFO (Mode) || S_ISSOCK (Mode) || S_ISCHR (Mode))
109 static char const *const follow_mode_string[] =
111 "descriptor", "name", NULL
114 static enum Follow_mode const follow_mode_map[] =
116 Follow_descriptor, Follow_name,
119 struct File_spec
121 /* The actual file name, or "-" for stdin. */
122 char *name;
124 /* Attributes of the file the last time we checked. */
125 off_t size;
126 struct timespec mtime;
127 dev_t dev;
128 ino_t ino;
129 mode_t mode;
131 /* The specified name initially referred to a directory or some other
132 type for which tail isn't meaningful. Unlike for a permission problem
133 (tailable, below) once this is set, the name is not checked ever again. */
134 bool ignore;
136 /* See the description of fremote. */
137 bool remote;
139 /* A file is tailable if it exists, is readable, and is of type
140 IS_TAILABLE_FILE_TYPE. */
141 bool tailable;
143 /* File descriptor on which the file is open; -1 if it's not open. */
144 int fd;
146 /* The value of errno seen last time we checked this file. */
147 int errnum;
149 /* 1 if O_NONBLOCK is clear, 0 if set, -1 if not known. */
150 int blocking;
152 #if HAVE_INOTIFY
153 /* The watch descriptor used by inotify. */
154 int wd;
156 /* The parent directory watch descriptor. It is used only
157 * when Follow_name is used. */
158 int parent_wd;
160 /* Offset in NAME of the basename part. */
161 size_t basename_start;
162 #endif
164 /* See description of DEFAULT_MAX_N_... below. */
165 uintmax_t n_unchanged_stats;
168 /* Keep trying to open a file even if it is inaccessible when tail starts
169 or if it becomes inaccessible later -- useful only with -f. */
170 static bool reopen_inaccessible_files;
172 /* If true, interpret the numeric argument as the number of lines.
173 Otherwise, interpret it as the number of bytes. */
174 static bool count_lines;
176 /* Whether we follow the name of each file or the file descriptor
177 that is initially associated with each name. */
178 static enum Follow_mode follow_mode = Follow_descriptor;
180 /* If true, read from the ends of all specified files until killed. */
181 static bool forever;
183 /* If true, monitor output so we exit if pipe reader terminates. */
184 static bool monitor_output;
186 /* If true, count from start of file instead of end. */
187 static bool from_start;
189 /* If true, print filename headers. */
190 static bool print_headers;
192 /* Character to split lines by. */
193 static char line_end;
195 /* When to print the filename banners. */
196 enum header_mode
198 multiple_files, always, never
201 /* When tailing a file by name, if there have been this many consecutive
202 iterations for which the file has not changed, then open/fstat
203 the file to determine if that file name is still associated with the
204 same device/inode-number pair as before. This option is meaningful only
205 when following by name. --max-unchanged-stats=N */
206 #define DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS 5
207 static uintmax_t max_n_unchanged_stats_between_opens =
208 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS;
210 /* The process ID of the process (presumably on the current host)
211 that is writing to all followed files. */
212 static pid_t pid;
214 /* True if we have ever read standard input. */
215 static bool have_read_stdin;
217 /* If nonzero, skip the is-regular-file test used to determine whether
218 to use the lseek optimization. Instead, use the more general (and
219 more expensive) code unconditionally. Intended solely for testing. */
220 static bool presume_input_pipe;
222 /* If nonzero then don't use inotify even if available. */
223 static bool disable_inotify;
225 /* For long options that have no equivalent short option, use a
226 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
227 enum
229 RETRY_OPTION = CHAR_MAX + 1,
230 MAX_UNCHANGED_STATS_OPTION,
231 PID_OPTION,
232 PRESUME_INPUT_PIPE_OPTION,
233 LONG_FOLLOW_OPTION,
234 DISABLE_INOTIFY_OPTION
237 static struct option const long_options[] =
239 {"bytes", required_argument, NULL, 'c'},
240 {"follow", optional_argument, NULL, LONG_FOLLOW_OPTION},
241 {"lines", required_argument, NULL, 'n'},
242 {"max-unchanged-stats", required_argument, NULL, MAX_UNCHANGED_STATS_OPTION},
243 {"-disable-inotify", no_argument, NULL,
244 DISABLE_INOTIFY_OPTION}, /* do not document */
245 {"pid", required_argument, NULL, PID_OPTION},
246 {"-presume-input-pipe", no_argument, NULL,
247 PRESUME_INPUT_PIPE_OPTION}, /* do not document */
248 {"quiet", no_argument, NULL, 'q'},
249 {"retry", no_argument, NULL, RETRY_OPTION},
250 {"silent", no_argument, NULL, 'q'},
251 {"sleep-interval", required_argument, NULL, 's'},
252 {"verbose", no_argument, NULL, 'v'},
253 {"zero-terminated", no_argument, NULL, 'z'},
254 {GETOPT_HELP_OPTION_DECL},
255 {GETOPT_VERSION_OPTION_DECL},
256 {NULL, 0, NULL, 0}
259 void
260 usage (int status)
262 if (status != EXIT_SUCCESS)
263 emit_try_help ();
264 else
266 printf (_("\
267 Usage: %s [OPTION]... [FILE]...\n\
269 program_name);
270 printf (_("\
271 Print the last %d lines of each FILE to standard output.\n\
272 With more than one FILE, precede each with a header giving the file name.\n\
273 "), DEFAULT_N_LINES);
275 emit_stdin_note ();
276 emit_mandatory_arg_note ();
278 fputs (_("\
279 -c, --bytes=[+]NUM output the last NUM bytes; or use -c +NUM to\n\
280 output starting with byte NUM of each file\n\
281 "), stdout);
282 fputs (_("\
283 -f, --follow[={name|descriptor}]\n\
284 output appended data as the file grows;\n\
285 an absent option argument means 'descriptor'\n\
286 -F same as --follow=name --retry\n\
287 "), stdout);
288 printf (_("\
289 -n, --lines=[+]NUM output the last NUM lines, instead of the last %d;\n\
290 or use -n +NUM to output starting with line NUM\n\
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_N_LINES,
299 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS
301 fputs (_("\
302 --pid=PID with -f, terminate after process ID, PID dies\n\
303 -q, --quiet, --silent never output headers giving file names\n\
304 --retry keep trying to open a file if it is inaccessible\n\
305 "), stdout);
306 fputs (_("\
307 -s, --sleep-interval=N with -f, sleep for approximately N seconds\n\
308 (default 1.0) between iterations;\n\
309 with inotify and --pid=P, check process P at\n\
310 least once every N seconds\n\
311 -v, --verbose always output headers giving file names\n\
312 "), stdout);
313 fputs (_("\
314 -z, --zero-terminated line delimiter is NUL, not newline\n\
315 "), stdout);
316 fputs (HELP_OPTION_DESCRIPTION, stdout);
317 fputs (VERSION_OPTION_DESCRIPTION, stdout);
318 fputs (_("\
320 NUM may have a multiplier suffix:\n\
321 b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\
322 GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.\n\
323 Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\
325 "), stdout);
326 fputs (_("\
327 With --follow (-f), tail defaults to following the file descriptor, which\n\
328 means that even if a tail'ed file is renamed, tail will continue to track\n\
329 its end. This default behavior is not desirable when you really want to\n\
330 track the actual name of the file, not the file descriptor (e.g., log\n\
331 rotation). Use --follow=name in that case. That causes tail to track the\n\
332 named file in a way that accommodates renaming, removal and creation.\n\
333 "), stdout);
334 emit_ancillary_info (PROGRAM_NAME);
336 exit (status);
339 /* Ensure exit, either with SIGPIPE or EXIT_FAILURE status. */
340 static void
341 die_pipe (void)
343 raise (SIGPIPE);
344 exit (EXIT_FAILURE);
347 /* If the output has gone away, then terminate
348 as we would if we had written to this output. */
349 static void
350 check_output_alive (void)
352 if (! monitor_output)
353 return;
355 /* Use 'poll' on AIX (where 'select' was seen to give a readable
356 event immediately) or if using inotify (which relies on 'poll'
357 anyway). Otherwise, use 'select' as it's more portable;
358 'poll' doesn't work for this application on macOS. */
359 #if defined _AIX || HAVE_INOTIFY
360 struct pollfd pfd;
361 pfd.fd = STDOUT_FILENO;
362 pfd.events = POLLERR;
364 if (poll (&pfd, 1, 0) >= 0 && (pfd.revents & POLLERR))
365 die_pipe ();
366 #else
367 struct timeval delay;
368 delay.tv_sec = delay.tv_usec = 0;
370 fd_set rfd;
371 FD_ZERO (&rfd);
372 FD_SET (STDOUT_FILENO, &rfd);
374 /* readable event on STDOUT is equivalent to POLLERR,
375 and implies an error condition on output like broken pipe. */
376 if (select (STDOUT_FILENO + 1, &rfd, NULL, NULL, &delay) == 1)
377 die_pipe ();
378 #endif
382 static bool
383 valid_file_spec (struct File_spec const *f)
385 /* Exactly one of the following subexpressions must be true. */
386 return ((f->fd == -1) ^ (f->errnum == 0));
389 static char const *
390 pretty_name (struct File_spec const *f)
392 return (STREQ (f->name, "-") ? _("standard input") : f->name);
395 /* Record a file F with descriptor FD, size SIZE, status ST, and
396 blocking status BLOCKING. */
398 static void
399 record_open_fd (struct File_spec *f, int fd,
400 off_t size, struct stat const *st,
401 int blocking)
403 f->fd = fd;
404 f->size = size;
405 f->mtime = get_stat_mtime (st);
406 f->dev = st->st_dev;
407 f->ino = st->st_ino;
408 f->mode = st->st_mode;
409 f->blocking = blocking;
410 f->n_unchanged_stats = 0;
411 f->ignore = false;
414 /* Close the file with descriptor FD and name FILENAME. */
416 static void
417 close_fd (int fd, char const *filename)
419 if (fd != -1 && fd != STDIN_FILENO && close (fd))
421 error (0, errno, _("closing %s (fd=%d)"), quoteaf (filename), fd);
425 static void
426 write_header (char const *pretty_filename)
428 static bool first_file = true;
430 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), pretty_filename);
431 first_file = false;
434 /* Write N_BYTES from BUFFER to stdout.
435 Exit immediately on error with a single diagnostic. */
437 static void
438 xwrite_stdout (char const *buffer, size_t n_bytes)
440 if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) < n_bytes)
442 clearerr (stdout); /* To avoid redundant close_stdout diagnostic. */
443 die (EXIT_FAILURE, errno, _("error writing %s"),
444 quoteaf ("standard output"));
448 /* Read and output N_BYTES of file PRETTY_FILENAME starting at the current
449 position in FD. If N_BYTES is COPY_TO_EOF, then copy until end of file.
450 If N_BYTES is COPY_A_BUFFER, then copy at most one buffer's worth.
451 Return the number of bytes read from the file. */
453 static uintmax_t
454 dump_remainder (bool want_header, char const *pretty_filename, int fd,
455 uintmax_t n_bytes)
457 uintmax_t n_written;
458 uintmax_t n_remaining = n_bytes;
460 n_written = 0;
461 while (true)
463 char buffer[BUFSIZ];
464 size_t n = MIN (n_remaining, BUFSIZ);
465 size_t bytes_read = safe_read (fd, buffer, n);
466 if (bytes_read == SAFE_READ_ERROR)
468 if (errno != EAGAIN)
469 die (EXIT_FAILURE, errno, _("error reading %s"),
470 quoteaf (pretty_filename));
471 break;
473 if (bytes_read == 0)
474 break;
475 if (want_header)
477 write_header (pretty_filename);
478 want_header = false;
480 xwrite_stdout (buffer, bytes_read);
481 n_written += bytes_read;
482 if (n_bytes != COPY_TO_EOF)
484 n_remaining -= bytes_read;
485 if (n_remaining == 0 || n_bytes == COPY_A_BUFFER)
486 break;
490 return n_written;
493 /* Call lseek with the specified arguments, where file descriptor FD
494 corresponds to the file, FILENAME.
495 Give a diagnostic and exit nonzero if lseek fails.
496 Otherwise, return the resulting offset. */
498 static off_t
499 xlseek (int fd, off_t offset, int whence, char const *filename)
501 off_t new_offset = lseek (fd, offset, whence);
502 char buf[INT_BUFSIZE_BOUND (offset)];
503 char *s;
505 if (0 <= new_offset)
506 return new_offset;
508 s = offtostr (offset, buf);
509 switch (whence)
511 case SEEK_SET:
512 error (0, errno, _("%s: cannot seek to offset %s"),
513 quotef (filename), s);
514 break;
515 case SEEK_CUR:
516 error (0, errno, _("%s: cannot seek to relative offset %s"),
517 quotef (filename), s);
518 break;
519 case SEEK_END:
520 error (0, errno, _("%s: cannot seek to end-relative offset %s"),
521 quotef (filename), s);
522 break;
523 default:
524 abort ();
527 exit (EXIT_FAILURE);
530 /* Print the last N_LINES lines from the end of file FD.
531 Go backward through the file, reading 'BUFSIZ' bytes at a time (except
532 probably the first), until we hit the start of the file or have
533 read NUMBER newlines.
534 START_POS is the starting position of the read pointer for the file
535 associated with FD (may be nonzero).
536 END_POS is the file offset of EOF (one larger than offset of last byte).
537 Return true if successful. */
539 static bool
540 file_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
541 off_t start_pos, off_t end_pos, uintmax_t *read_pos)
543 char buffer[BUFSIZ];
544 size_t bytes_read;
545 off_t pos = end_pos;
547 if (n_lines == 0)
548 return true;
550 /* Set 'bytes_read' to the size of the last, probably partial, buffer;
551 0 < 'bytes_read' <= 'BUFSIZ'. */
552 bytes_read = (pos - start_pos) % BUFSIZ;
553 if (bytes_read == 0)
554 bytes_read = BUFSIZ;
555 /* Make 'pos' a multiple of 'BUFSIZ' (0 if the file is short), so that all
556 reads will be on block boundaries, which might increase efficiency. */
557 pos -= bytes_read;
558 xlseek (fd, pos, SEEK_SET, pretty_filename);
559 bytes_read = safe_read (fd, buffer, bytes_read);
560 if (bytes_read == SAFE_READ_ERROR)
562 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
563 return false;
565 *read_pos = pos + bytes_read;
567 /* Count the incomplete line on files that don't end with a newline. */
568 if (bytes_read && buffer[bytes_read - 1] != line_end)
569 --n_lines;
573 /* Scan backward, counting the newlines in this bufferfull. */
575 size_t n = bytes_read;
576 while (n)
578 char const *nl;
579 nl = memrchr (buffer, line_end, n);
580 if (nl == NULL)
581 break;
582 n = nl - buffer;
583 if (n_lines-- == 0)
585 /* If this newline isn't the last character in the buffer,
586 output the part that is after it. */
587 xwrite_stdout (nl + 1, bytes_read - (n + 1));
588 *read_pos += dump_remainder (false, pretty_filename, fd,
589 end_pos - (pos + bytes_read));
590 return true;
594 /* Not enough newlines in that bufferfull. */
595 if (pos == start_pos)
597 /* Not enough lines in the file; print everything from
598 start_pos to the end. */
599 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
600 *read_pos = start_pos + dump_remainder (false, pretty_filename, fd,
601 end_pos);
602 return true;
604 pos -= BUFSIZ;
605 xlseek (fd, pos, SEEK_SET, pretty_filename);
607 bytes_read = safe_read (fd, buffer, BUFSIZ);
608 if (bytes_read == SAFE_READ_ERROR)
610 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
611 return false;
614 *read_pos = pos + bytes_read;
616 while (bytes_read > 0);
618 return true;
621 /* Print the last N_LINES lines from the end of the standard input,
622 open for reading as pipe FD.
623 Buffer the text as a linked list of LBUFFERs, adding them as needed.
624 Return true if successful. */
626 static bool
627 pipe_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
628 uintmax_t *read_pos)
630 struct linebuffer
632 char buffer[BUFSIZ];
633 size_t nbytes;
634 size_t nlines;
635 struct linebuffer *next;
637 typedef struct linebuffer LBUFFER;
638 LBUFFER *first, *last, *tmp;
639 size_t total_lines = 0; /* Total number of newlines in all buffers. */
640 bool ok = true;
641 size_t n_read; /* Size in bytes of most recent read */
643 first = last = xmalloc (sizeof (LBUFFER));
644 first->nbytes = first->nlines = 0;
645 first->next = NULL;
646 tmp = xmalloc (sizeof (LBUFFER));
648 /* Input is always read into a fresh buffer. */
649 while (true)
651 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
652 if (n_read == 0 || n_read == SAFE_READ_ERROR)
653 break;
654 tmp->nbytes = n_read;
655 *read_pos += n_read;
656 tmp->nlines = 0;
657 tmp->next = NULL;
659 /* Count the number of newlines just read. */
661 char const *buffer_end = tmp->buffer + n_read;
662 char const *p = tmp->buffer;
663 while ((p = memchr (p, line_end, buffer_end - p)))
665 ++p;
666 ++tmp->nlines;
669 total_lines += tmp->nlines;
671 /* If there is enough room in the last buffer read, just append the new
672 one to it. This is because when reading from a pipe, 'n_read' can
673 often be very small. */
674 if (tmp->nbytes + last->nbytes < BUFSIZ)
676 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
677 last->nbytes += tmp->nbytes;
678 last->nlines += tmp->nlines;
680 else
682 /* If there's not enough room, link the new buffer onto the end of
683 the list, then either free up the oldest buffer for the next
684 read if that would leave enough lines, or else malloc a new one.
685 Some compaction mechanism is possible but probably not
686 worthwhile. */
687 last = last->next = tmp;
688 if (total_lines - first->nlines > n_lines)
690 tmp = first;
691 total_lines -= first->nlines;
692 first = first->next;
694 else
695 tmp = xmalloc (sizeof (LBUFFER));
699 free (tmp);
701 if (n_read == SAFE_READ_ERROR)
703 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
704 ok = false;
705 goto free_lbuffers;
708 /* If the file is empty, then bail out. */
709 if (last->nbytes == 0)
710 goto free_lbuffers;
712 /* This prevents a core dump when the pipe contains no newlines. */
713 if (n_lines == 0)
714 goto free_lbuffers;
716 /* Count the incomplete line on files that don't end with a newline. */
717 if (last->buffer[last->nbytes - 1] != line_end)
719 ++last->nlines;
720 ++total_lines;
723 /* Run through the list, printing lines. First, skip over unneeded
724 buffers. */
725 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
726 total_lines -= tmp->nlines;
728 /* Find the correct beginning, then print the rest of the file. */
730 char const *beg = tmp->buffer;
731 char const *buffer_end = tmp->buffer + tmp->nbytes;
732 if (total_lines > n_lines)
734 /* Skip 'total_lines' - 'n_lines' newlines. We made sure that
735 'total_lines' - 'n_lines' <= 'tmp->nlines'. */
736 size_t j;
737 for (j = total_lines - n_lines; j; --j)
739 beg = rawmemchr (beg, line_end);
740 ++beg;
744 xwrite_stdout (beg, buffer_end - beg);
747 for (tmp = tmp->next; tmp; tmp = tmp->next)
748 xwrite_stdout (tmp->buffer, tmp->nbytes);
750 free_lbuffers:
751 while (first)
753 tmp = first->next;
754 free (first);
755 first = tmp;
757 return ok;
760 /* Print the last N_BYTES characters from the end of pipe FD.
761 This is a stripped down version of pipe_lines.
762 Return true if successful. */
764 static bool
765 pipe_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
766 uintmax_t *read_pos)
768 struct charbuffer
770 char buffer[BUFSIZ];
771 size_t nbytes;
772 struct charbuffer *next;
774 typedef struct charbuffer CBUFFER;
775 CBUFFER *first, *last, *tmp;
776 size_t i; /* Index into buffers. */
777 size_t total_bytes = 0; /* Total characters in all buffers. */
778 bool ok = true;
779 size_t n_read;
781 first = last = xmalloc (sizeof (CBUFFER));
782 first->nbytes = 0;
783 first->next = NULL;
784 tmp = xmalloc (sizeof (CBUFFER));
786 /* Input is always read into a fresh buffer. */
787 while (true)
789 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
790 if (n_read == 0 || n_read == SAFE_READ_ERROR)
791 break;
792 *read_pos += n_read;
793 tmp->nbytes = n_read;
794 tmp->next = NULL;
796 total_bytes += tmp->nbytes;
797 /* If there is enough room in the last buffer read, just append the new
798 one to it. This is because when reading from a pipe, 'nbytes' can
799 often be very small. */
800 if (tmp->nbytes + last->nbytes < BUFSIZ)
802 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
803 last->nbytes += tmp->nbytes;
805 else
807 /* If there's not enough room, link the new buffer onto the end of
808 the list, then either free up the oldest buffer for the next
809 read if that would leave enough characters, or else malloc a new
810 one. Some compaction mechanism is possible but probably not
811 worthwhile. */
812 last = last->next = tmp;
813 if (total_bytes - first->nbytes > n_bytes)
815 tmp = first;
816 total_bytes -= first->nbytes;
817 first = first->next;
819 else
821 tmp = xmalloc (sizeof (CBUFFER));
826 free (tmp);
828 if (n_read == SAFE_READ_ERROR)
830 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
831 ok = false;
832 goto free_cbuffers;
835 /* Run through the list, printing characters. First, skip over unneeded
836 buffers. */
837 for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
838 total_bytes -= tmp->nbytes;
840 /* Find the correct beginning, then print the rest of the file.
841 We made sure that 'total_bytes' - 'n_bytes' <= 'tmp->nbytes'. */
842 if (total_bytes > n_bytes)
843 i = total_bytes - n_bytes;
844 else
845 i = 0;
846 xwrite_stdout (&tmp->buffer[i], tmp->nbytes - i);
848 for (tmp = tmp->next; tmp; tmp = tmp->next)
849 xwrite_stdout (tmp->buffer, tmp->nbytes);
851 free_cbuffers:
852 while (first)
854 tmp = first->next;
855 free (first);
856 first = tmp;
858 return ok;
861 /* Skip N_BYTES characters from the start of pipe FD, and print
862 any extra characters that were read beyond that.
863 Return 1 on error, 0 if ok, -1 if EOF. */
865 static int
866 start_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
867 uintmax_t *read_pos)
869 char buffer[BUFSIZ];
871 while (0 < n_bytes)
873 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
874 if (bytes_read == 0)
875 return -1;
876 if (bytes_read == SAFE_READ_ERROR)
878 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
879 return 1;
881 *read_pos += bytes_read;
882 if (bytes_read <= n_bytes)
883 n_bytes -= bytes_read;
884 else
886 size_t n_remaining = bytes_read - n_bytes;
887 /* Print extra characters if there are any. */
888 xwrite_stdout (&buffer[n_bytes], n_remaining);
889 break;
893 return 0;
896 /* Skip N_LINES lines at the start of file or pipe FD, and print
897 any extra characters that were read beyond that.
898 Return 1 on error, 0 if ok, -1 if EOF. */
900 static int
901 start_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
902 uintmax_t *read_pos)
904 if (n_lines == 0)
905 return 0;
907 while (true)
909 char buffer[BUFSIZ];
910 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
911 if (bytes_read == 0) /* EOF */
912 return -1;
913 if (bytes_read == SAFE_READ_ERROR) /* error */
915 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
916 return 1;
919 char *buffer_end = buffer + bytes_read;
921 *read_pos += bytes_read;
923 char *p = buffer;
924 while ((p = memchr (p, line_end, buffer_end - p)))
926 ++p;
927 if (--n_lines == 0)
929 if (p < buffer_end)
930 xwrite_stdout (p, buffer_end - p);
931 return 0;
937 /* Return false when FD is open on a file residing on a local file system.
938 If fstatfs fails, give a diagnostic and return true.
939 If fstatfs cannot be called, return true. */
940 static bool
941 fremote (int fd, char const *name)
943 bool remote = true; /* be conservative (poll by default). */
945 #if HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE \
946 && (defined __linux__ || defined __ANDROID__)
947 struct statfs buf;
948 int err = fstatfs (fd, &buf);
949 if (err != 0)
951 /* On at least linux-2.6.38, fstatfs fails with ENOSYS when FD
952 is open on a pipe. Treat that like a remote file. */
953 if (errno != ENOSYS)
954 error (0, errno, _("cannot determine location of %s. "
955 "reverting to polling"), quoteaf (name));
957 else
959 switch (is_local_fs_type (buf.f_type))
961 case 0:
962 break;
963 case -1:
964 /* Treat unrecognized file systems as "remote", so caller polls.
965 Note README-release has instructions for syncing the internal
966 list with the latest Linux kernel file system constants. */
967 break;
968 case 1:
969 remote = false;
970 break;
971 default:
972 assert (!"unexpected return value from is_local_fs_type");
975 #endif
977 return remote;
980 /* open/fstat F->name and handle changes. */
981 static void
982 recheck (struct File_spec *f, bool blocking)
984 struct stat new_stats;
985 bool ok = true;
986 bool is_stdin = (STREQ (f->name, "-"));
987 bool was_tailable = f->tailable;
988 int prev_errnum = f->errnum;
989 bool new_file;
990 int fd = (is_stdin
991 ? STDIN_FILENO
992 : open (f->name, O_RDONLY | (blocking ? 0 : O_NONBLOCK)));
994 assert (valid_file_spec (f));
996 /* If the open fails because the file doesn't exist,
997 then mark the file as not tailable. */
998 f->tailable = !(reopen_inaccessible_files && fd == -1);
1000 if (! disable_inotify && ! lstat (f->name, &new_stats)
1001 && S_ISLNK (new_stats.st_mode))
1003 /* Diagnose the edge case where a regular file is changed
1004 to a symlink. We avoid inotify with symlinks since
1005 it's awkward to match between symlink name and target. */
1006 ok = false;
1007 f->errnum = -1;
1008 f->ignore = true;
1010 error (0, 0, _("%s has been replaced with an untailable symbolic link"),
1011 quoteaf (pretty_name (f)));
1013 else if (fd == -1 || fstat (fd, &new_stats) < 0)
1015 ok = false;
1016 f->errnum = errno;
1017 if (!f->tailable)
1019 if (was_tailable)
1021 /* FIXME-maybe: detect the case in which the file first becomes
1022 unreadable (perms), and later becomes readable again and can
1023 be seen to be the same file (dev/ino). Otherwise, tail prints
1024 the entire contents of the file when it becomes readable. */
1025 error (0, f->errnum, _("%s has become inaccessible"),
1026 quoteaf (pretty_name (f)));
1028 else
1030 /* say nothing... it's still not tailable */
1033 else if (prev_errnum != errno)
1034 error (0, errno, "%s", quotef (pretty_name (f)));
1036 else if (!IS_TAILABLE_FILE_TYPE (new_stats.st_mode))
1038 ok = false;
1039 f->errnum = -1;
1040 f->tailable = false;
1041 f->ignore = ! (reopen_inaccessible_files && follow_mode == Follow_name);
1042 if (was_tailable || prev_errnum != f->errnum)
1043 error (0, 0, _("%s has been replaced with an untailable file%s"),
1044 quoteaf (pretty_name (f)),
1045 f->ignore ? _("; giving up on this name") : "");
1047 else if ((f->remote = fremote (fd, pretty_name (f))) && ! disable_inotify)
1049 ok = false;
1050 f->errnum = -1;
1051 error (0, 0, _("%s has been replaced with an untailable remote file"),
1052 quoteaf (pretty_name (f)));
1053 f->ignore = true;
1054 f->remote = true;
1056 else
1058 f->errnum = 0;
1061 new_file = false;
1062 if (!ok)
1064 close_fd (fd, pretty_name (f));
1065 close_fd (f->fd, pretty_name (f));
1066 f->fd = -1;
1068 else if (prev_errnum && prev_errnum != ENOENT)
1070 new_file = true;
1071 assert (f->fd == -1);
1072 error (0, 0, _("%s has become accessible"), quoteaf (pretty_name (f)));
1074 else if (f->fd == -1)
1076 /* A new file even when inodes haven't changed as <dev,inode>
1077 pairs can be reused, and we know the file was missing
1078 on the previous iteration. Note this also means the file
1079 is redisplayed in --follow=name mode if renamed away from
1080 and back to a monitored name. */
1081 new_file = true;
1083 error (0, 0,
1084 _("%s has appeared; following new file"),
1085 quoteaf (pretty_name (f)));
1087 else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
1089 /* File has been replaced (e.g., via log rotation) --
1090 tail the new one. */
1091 new_file = true;
1093 error (0, 0,
1094 _("%s has been replaced; following new file"),
1095 quoteaf (pretty_name (f)));
1097 /* Close the old one. */
1098 close_fd (f->fd, pretty_name (f));
1101 else
1103 /* No changes detected, so close new fd. */
1104 close_fd (fd, pretty_name (f));
1107 /* FIXME: When a log is rotated, daemons tend to log to the
1108 old file descriptor until the new file is present and
1109 the daemon is sent a signal. Therefore tail may miss entries
1110 being written to the old file. Perhaps we should keep
1111 the older file open and continue to monitor it until
1112 data is written to a new file. */
1113 if (new_file)
1115 /* Start at the beginning of the file. */
1116 record_open_fd (f, fd, 0, &new_stats, (is_stdin ? -1 : blocking));
1117 xlseek (fd, 0, SEEK_SET, pretty_name (f));
1121 /* Return true if any of the N_FILES files in F are live, i.e., have
1122 open file descriptors, or should be checked again (see --retry).
1123 When following descriptors, checking should only continue when any
1124 of the files is not yet ignored. */
1126 static bool
1127 any_live_files (const struct File_spec *f, size_t n_files)
1129 /* In inotify mode, ignore may be set for files
1130 which may later be replaced with new files.
1131 So always consider files live in -F mode. */
1132 if (reopen_inaccessible_files && follow_mode == Follow_name)
1133 return true;
1135 for (size_t i = 0; i < n_files; i++)
1137 if (0 <= f[i].fd)
1138 return true;
1139 else
1141 if (! f[i].ignore && reopen_inaccessible_files)
1142 return true;
1146 return false;
1149 /* Tail N_FILES files forever, or until killed.
1150 The pertinent information for each file is stored in an entry of F.
1151 Loop over each of them, doing an fstat to see if they have changed size,
1152 and an occasional open/fstat to see if any dev/ino pair has changed.
1153 If none of them have changed size in one iteration, sleep for a
1154 while and try again. Continue until the user interrupts us. */
1156 static void
1157 tail_forever (struct File_spec *f, size_t n_files, double sleep_interval)
1159 /* Use blocking I/O as an optimization, when it's easy. */
1160 bool blocking = (pid == 0 && follow_mode == Follow_descriptor
1161 && n_files == 1 && f[0].fd != -1 && ! S_ISREG (f[0].mode));
1162 size_t last;
1163 bool writer_is_dead = false;
1165 last = n_files - 1;
1167 while (true)
1169 size_t i;
1170 bool any_input = false;
1172 for (i = 0; i < n_files; i++)
1174 int fd;
1175 char const *name;
1176 mode_t mode;
1177 struct stat stats;
1178 uintmax_t bytes_read;
1180 if (f[i].ignore)
1181 continue;
1183 if (f[i].fd < 0)
1185 recheck (&f[i], blocking);
1186 continue;
1189 fd = f[i].fd;
1190 name = pretty_name (&f[i]);
1191 mode = f[i].mode;
1193 if (f[i].blocking != blocking)
1195 int old_flags = fcntl (fd, F_GETFL);
1196 int new_flags = old_flags | (blocking ? 0 : O_NONBLOCK);
1197 if (old_flags < 0
1198 || (new_flags != old_flags
1199 && fcntl (fd, F_SETFL, new_flags) == -1))
1201 /* Don't update f[i].blocking if fcntl fails. */
1202 if (S_ISREG (f[i].mode) && errno == EPERM)
1204 /* This happens when using tail -f on a file with
1205 the append-only attribute. */
1207 else
1208 die (EXIT_FAILURE, errno,
1209 _("%s: cannot change nonblocking mode"),
1210 quotef (name));
1212 else
1213 f[i].blocking = blocking;
1216 if (!f[i].blocking)
1218 if (fstat (fd, &stats) != 0)
1220 f[i].fd = -1;
1221 f[i].errnum = errno;
1222 error (0, errno, "%s", quotef (name));
1223 close (fd); /* ignore failure */
1224 continue;
1227 if (f[i].mode == stats.st_mode
1228 && (! S_ISREG (stats.st_mode) || f[i].size == stats.st_size)
1229 && timespec_cmp (f[i].mtime, get_stat_mtime (&stats)) == 0)
1231 if ((max_n_unchanged_stats_between_opens
1232 <= f[i].n_unchanged_stats++)
1233 && follow_mode == Follow_name)
1235 recheck (&f[i], f[i].blocking);
1236 f[i].n_unchanged_stats = 0;
1238 continue;
1241 /* This file has changed. Print out what we can, and
1242 then keep looping. */
1244 f[i].mtime = get_stat_mtime (&stats);
1245 f[i].mode = stats.st_mode;
1247 /* reset counter */
1248 f[i].n_unchanged_stats = 0;
1250 /* XXX: This is only a heuristic, as the file may have also
1251 been truncated and written to if st_size >= size
1252 (in which case we ignore new data <= size). */
1253 if (S_ISREG (mode) && stats.st_size < f[i].size)
1255 error (0, 0, _("%s: file truncated"), quotef (name));
1256 /* Assume the file was truncated to 0,
1257 and therefore output all "new" data. */
1258 xlseek (fd, 0, SEEK_SET, name);
1259 f[i].size = 0;
1262 if (i != last)
1264 if (print_headers)
1265 write_header (name);
1266 last = i;
1270 /* Don't read more than st_size on networked file systems
1271 because it was seen on glusterfs at least, that st_size
1272 may be smaller than the data read on a _subsequent_ stat call. */
1273 uintmax_t bytes_to_read;
1274 if (f[i].blocking)
1275 bytes_to_read = COPY_A_BUFFER;
1276 else if (S_ISREG (mode) && f[i].remote)
1277 bytes_to_read = stats.st_size - f[i].size;
1278 else
1279 bytes_to_read = COPY_TO_EOF;
1281 bytes_read = dump_remainder (false, name, fd, bytes_to_read);
1283 any_input |= (bytes_read != 0);
1284 f[i].size += bytes_read;
1287 if (! any_live_files (f, n_files))
1289 error (0, 0, _("no files remaining"));
1290 break;
1293 if ((!any_input || blocking) && fflush (stdout) != 0)
1294 die (EXIT_FAILURE, errno, _("write error"));
1296 check_output_alive ();
1298 /* If nothing was read, sleep and/or check for dead writers. */
1299 if (!any_input)
1301 if (writer_is_dead)
1302 break;
1304 /* Once the writer is dead, read the files once more to
1305 avoid a race condition. */
1306 writer_is_dead = (pid != 0
1307 && kill (pid, 0) != 0
1308 /* Handle the case in which you cannot send a
1309 signal to the writer, so kill fails and sets
1310 errno to EPERM. */
1311 && errno != EPERM);
1313 if (!writer_is_dead && xnanosleep (sleep_interval))
1314 die (EXIT_FAILURE, errno, _("cannot read realtime clock"));
1320 #if HAVE_INOTIFY
1322 /* Return true if any of the N_FILES files in F is remote, i.e., has
1323 an open file descriptor and is on a network file system. */
1325 static bool
1326 any_remote_file (const struct File_spec *f, size_t n_files)
1328 for (size_t i = 0; i < n_files; i++)
1329 if (0 <= f[i].fd && f[i].remote)
1330 return true;
1331 return false;
1334 /* Return true if any of the N_FILES files in F is non remote, i.e., has
1335 an open file descriptor and is not on a network file system. */
1337 static bool
1338 any_non_remote_file (const struct File_spec *f, size_t n_files)
1340 for (size_t i = 0; i < n_files; i++)
1341 if (0 <= f[i].fd && ! f[i].remote)
1342 return true;
1343 return false;
1346 /* Return true if any of the N_FILES files in F is a symlink.
1347 Note we don't worry about the edge case where "-" exists,
1348 since that will have the same consequences for inotify,
1349 which is the only context this function is currently used. */
1351 static bool
1352 any_symlinks (const struct File_spec *f, size_t n_files)
1354 struct stat st;
1355 for (size_t i = 0; i < n_files; i++)
1356 if (lstat (f[i].name, &st) == 0 && S_ISLNK (st.st_mode))
1357 return true;
1358 return false;
1361 /* Return true if any of the N_FILES files in F is not
1362 a regular file or fifo. This is used to avoid adding inotify
1363 watches on a device file for example, which inotify
1364 will accept, but not give any events for. */
1366 static bool
1367 any_non_regular_fifo (const struct File_spec *f, size_t n_files)
1369 for (size_t i = 0; i < n_files; i++)
1370 if (0 <= f[i].fd && ! S_ISREG (f[i].mode) && ! S_ISFIFO (f[i].mode))
1371 return true;
1372 return false;
1375 /* Return true if any of the N_FILES files in F represents
1376 stdin and is tailable. */
1378 static bool
1379 tailable_stdin (const struct File_spec *f, size_t n_files)
1381 for (size_t i = 0; i < n_files; i++)
1382 if (!f[i].ignore && STREQ (f[i].name, "-"))
1383 return true;
1384 return false;
1387 static size_t
1388 wd_hasher (const void *entry, size_t tabsize)
1390 const struct File_spec *spec = entry;
1391 return spec->wd % tabsize;
1394 static bool
1395 wd_comparator (const void *e1, const void *e2)
1397 const struct File_spec *spec1 = e1;
1398 const struct File_spec *spec2 = e2;
1399 return spec1->wd == spec2->wd;
1402 /* Output (new) data for FSPEC->fd.
1403 PREV_FSPEC records the last File_spec for which we output. */
1404 static void
1405 check_fspec (struct File_spec *fspec, struct File_spec **prev_fspec)
1407 struct stat stats;
1408 char const *name;
1410 if (fspec->fd == -1)
1411 return;
1413 name = pretty_name (fspec);
1415 if (fstat (fspec->fd, &stats) != 0)
1417 fspec->errnum = errno;
1418 close_fd (fspec->fd, name);
1419 fspec->fd = -1;
1420 return;
1423 /* XXX: This is only a heuristic, as the file may have also
1424 been truncated and written to if st_size >= size
1425 (in which case we ignore new data <= size).
1426 Though in the inotify case it's more likely we'll get
1427 separate events for truncate() and write(). */
1428 if (S_ISREG (fspec->mode) && stats.st_size < fspec->size)
1430 error (0, 0, _("%s: file truncated"), quotef (name));
1431 xlseek (fspec->fd, 0, SEEK_SET, name);
1432 fspec->size = 0;
1434 else if (S_ISREG (fspec->mode) && stats.st_size == fspec->size
1435 && timespec_cmp (fspec->mtime, get_stat_mtime (&stats)) == 0)
1436 return;
1438 bool want_header = print_headers && (fspec != *prev_fspec);
1440 uintmax_t bytes_read = dump_remainder (want_header, name, fspec->fd,
1441 COPY_TO_EOF);
1442 fspec->size += bytes_read;
1444 if (bytes_read)
1446 *prev_fspec = fspec;
1447 if (fflush (stdout) != 0)
1448 die (EXIT_FAILURE, errno, _("write error"));
1452 /* Attempt to tail N_FILES files forever, or until killed.
1453 Check modifications using the inotify events system.
1454 Return false on error, or true to revert to polling. */
1455 static bool
1456 tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
1457 double sleep_interval)
1459 # if TAIL_TEST_SLEEP
1460 /* Delay between open() and inotify_add_watch()
1461 to help trigger different cases. */
1462 xnanosleep (1000000);
1463 # endif
1464 unsigned int max_realloc = 3;
1466 /* Map an inotify watch descriptor to the name of the file it's watching. */
1467 Hash_table *wd_to_name;
1469 bool found_watchable_file = false;
1470 bool tailed_but_unwatchable = false;
1471 bool found_unwatchable_dir = false;
1472 bool no_inotify_resources = false;
1473 bool writer_is_dead = false;
1474 struct File_spec *prev_fspec;
1475 size_t evlen = 0;
1476 char *evbuf;
1477 size_t evbuf_off = 0;
1478 size_t len = 0;
1480 wd_to_name = hash_initialize (n_files, NULL, wd_hasher, wd_comparator, NULL);
1481 if (! wd_to_name)
1482 xalloc_die ();
1484 /* The events mask used with inotify on files (not directories). */
1485 uint32_t inotify_wd_mask = IN_MODIFY;
1486 /* TODO: Perhaps monitor these events in Follow_descriptor mode also,
1487 to tag reported file names with "deleted", "moved" etc. */
1488 if (follow_mode == Follow_name)
1489 inotify_wd_mask |= (IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF);
1491 /* Add an inotify watch for each watched file. If -F is specified then watch
1492 its parent directory too, in this way when they re-appear we can add them
1493 again to the watch list. */
1494 size_t i;
1495 for (i = 0; i < n_files; i++)
1497 if (!f[i].ignore)
1499 size_t fnlen = strlen (f[i].name);
1500 if (evlen < fnlen)
1501 evlen = fnlen;
1503 f[i].wd = -1;
1505 if (follow_mode == Follow_name)
1507 size_t dirlen = dir_len (f[i].name);
1508 char prev = f[i].name[dirlen];
1509 f[i].basename_start = last_component (f[i].name) - f[i].name;
1511 f[i].name[dirlen] = '\0';
1513 /* It's fine to add the same directory more than once.
1514 In that case the same watch descriptor is returned. */
1515 f[i].parent_wd = inotify_add_watch (wd, dirlen ? f[i].name : ".",
1516 (IN_CREATE | IN_DELETE
1517 | IN_MOVED_TO | IN_ATTRIB
1518 | IN_DELETE_SELF));
1520 f[i].name[dirlen] = prev;
1522 if (f[i].parent_wd < 0)
1524 if (errno != ENOSPC) /* suppress confusing error. */
1525 error (0, errno, _("cannot watch parent directory of %s"),
1526 quoteaf (f[i].name));
1527 else
1528 error (0, 0, _("inotify resources exhausted"));
1529 found_unwatchable_dir = true;
1530 /* We revert to polling below. Note invalid uses
1531 of the inotify API will still be diagnosed. */
1532 break;
1536 f[i].wd = inotify_add_watch (wd, f[i].name, inotify_wd_mask);
1538 if (f[i].wd < 0)
1540 if (f[i].fd != -1) /* already tailed. */
1541 tailed_but_unwatchable = true;
1542 if (errno == ENOSPC || errno == ENOMEM)
1544 no_inotify_resources = true;
1545 error (0, 0, _("inotify resources exhausted"));
1546 break;
1548 else if (errno != f[i].errnum)
1549 error (0, errno, _("cannot watch %s"), quoteaf (f[i].name));
1550 continue;
1553 if (hash_insert (wd_to_name, &(f[i])) == NULL)
1554 xalloc_die ();
1556 found_watchable_file = true;
1560 /* Linux kernel 2.6.24 at least has a bug where eventually, ENOSPC is always
1561 returned by inotify_add_watch. In any case we should revert to polling
1562 when there are no inotify resources. Also a specified directory may not
1563 be currently present or accessible, so revert to polling. Also an already
1564 tailed but unwatchable due rename/unlink race, should also revert. */
1565 if (no_inotify_resources || found_unwatchable_dir
1566 || (follow_mode == Follow_descriptor && tailed_but_unwatchable))
1568 hash_free (wd_to_name);
1570 errno = 0;
1571 return true;
1573 if (follow_mode == Follow_descriptor && !found_watchable_file)
1575 # ifdef lint
1576 hash_free (wd_to_name);
1577 # endif
1578 return false;
1581 prev_fspec = &(f[n_files - 1]);
1583 /* Check files again. New files or data can be available since last time we
1584 checked and before they are watched by inotify. */
1585 for (i = 0; i < n_files; i++)
1587 if (! f[i].ignore)
1589 /* check for new files. */
1590 if (follow_mode == Follow_name)
1591 recheck (&(f[i]), false);
1592 else if (f[i].fd != -1)
1594 /* If the file was replaced in the small window since we tailed,
1595 then assume the watch is on the wrong item (different to
1596 that we've already produced output for), and so revert to
1597 polling the original descriptor. */
1598 struct stat stats;
1600 if (stat (f[i].name, &stats) == 0
1601 && (f[i].dev != stats.st_dev || f[i].ino != stats.st_ino))
1603 error (0, errno, _("%s was replaced"),
1604 quoteaf (pretty_name (&(f[i]))));
1605 hash_free (wd_to_name);
1607 errno = 0;
1608 return true;
1612 /* check for new data. */
1613 check_fspec (&f[i], &prev_fspec);
1617 evlen += sizeof (struct inotify_event) + 1;
1618 evbuf = xmalloc (evlen);
1620 /* Wait for inotify events and handle them. Events on directories
1621 ensure that watched files can be re-added when following by name.
1622 This loop blocks on the 'safe_read' call until a new event is notified.
1623 But when --pid=P is specified, tail usually waits via poll. */
1624 while (true)
1626 struct File_spec *fspec;
1627 struct inotify_event *ev;
1628 void *void_ev;
1630 /* When following by name without --retry, and the last file has
1631 been unlinked or renamed-away, diagnose it and return. */
1632 if (follow_mode == Follow_name
1633 && ! reopen_inaccessible_files
1634 && hash_get_n_entries (wd_to_name) == 0)
1636 error (0, 0, _("no files remaining"));
1637 # ifdef lint
1638 hash_free (wd_to_name);
1639 # endif
1640 return false;
1643 if (len <= evbuf_off)
1645 /* Poll for inotify events. When watching a PID, ensure
1646 that a read from WD will not block indefinitely.
1647 If MONITOR_OUTPUT, also poll for a broken output pipe. */
1649 int file_change;
1650 struct pollfd pfd[2];
1653 /* How many ms to wait for changes. -1 means wait forever. */
1654 int delay = -1;
1656 if (pid)
1658 if (writer_is_dead)
1659 exit (EXIT_SUCCESS);
1661 writer_is_dead = (kill (pid, 0) != 0 && errno != EPERM);
1663 if (writer_is_dead || sleep_interval <= 0)
1664 delay = 0;
1665 else if (sleep_interval < INT_MAX / 1000 - 1)
1667 /* delay = ceil (sleep_interval * 1000), sans libm. */
1668 double ddelay = sleep_interval * 1000;
1669 delay = ddelay;
1670 delay += delay < ddelay;
1674 pfd[0].fd = wd;
1675 pfd[0].events = POLLIN;
1676 pfd[1].fd = STDOUT_FILENO;
1677 pfd[1].events = pfd[1].revents = 0;
1678 file_change = poll (pfd, monitor_output + 1, delay);
1680 while (file_change == 0);
1682 if (file_change < 0)
1683 die (EXIT_FAILURE, errno,
1684 _("error waiting for inotify and output events"));
1685 if (pfd[1].revents)
1686 die_pipe ();
1688 len = safe_read (wd, evbuf, evlen);
1689 evbuf_off = 0;
1691 /* For kernels prior to 2.6.21, read returns 0 when the buffer
1692 is too small. */
1693 if ((len == 0 || (len == SAFE_READ_ERROR && errno == EINVAL))
1694 && max_realloc--)
1696 len = 0;
1697 evlen *= 2;
1698 evbuf = xrealloc (evbuf, evlen);
1699 continue;
1702 if (len == 0 || len == SAFE_READ_ERROR)
1703 die (EXIT_FAILURE, errno, _("error reading inotify event"));
1706 void_ev = evbuf + evbuf_off;
1707 ev = void_ev;
1708 evbuf_off += sizeof (*ev) + ev->len;
1710 /* If a directory is deleted, IN_DELETE_SELF is emitted
1711 with ev->name of length 0.
1712 We need to catch it, otherwise it would wait forever,
1713 as wd for directory becomes inactive. Revert to polling now. */
1714 if ((ev->mask & IN_DELETE_SELF) && ! ev->len)
1716 for (i = 0; i < n_files; i++)
1718 if (ev->wd == f[i].parent_wd)
1720 hash_free (wd_to_name);
1721 error (0, 0,
1722 _("directory containing watched file was removed"));
1723 errno = 0; /* we've already diagnosed enough errno detail. */
1724 return true;
1729 if (ev->len) /* event on ev->name in watched directory. */
1731 size_t j;
1732 for (j = 0; j < n_files; j++)
1734 /* With N=hundreds of frequently-changing files, this O(N^2)
1735 process might be a problem. FIXME: use a hash table? */
1736 if (f[j].parent_wd == ev->wd
1737 && STREQ (ev->name, f[j].name + f[j].basename_start))
1738 break;
1741 /* It is not a watched file. */
1742 if (j == n_files)
1743 continue;
1745 fspec = &(f[j]);
1747 int new_wd = -1;
1748 bool deleting = !! (ev->mask & IN_DELETE);
1750 if (! deleting)
1752 /* Adding the same inode again will look up any existing wd. */
1753 new_wd = inotify_add_watch (wd, f[j].name, inotify_wd_mask);
1756 if (! deleting && new_wd < 0)
1758 if (errno == ENOSPC || errno == ENOMEM)
1760 error (0, 0, _("inotify resources exhausted"));
1761 hash_free (wd_to_name);
1762 errno = 0;
1763 return true; /* revert to polling. */
1765 else
1767 /* Can get ENOENT for a dangling symlink for example. */
1768 error (0, errno, _("cannot watch %s"), quoteaf (f[j].name));
1770 /* We'll continue below after removing the existing watch. */
1773 /* This will be false if only attributes of file change. */
1774 bool new_watch;
1775 new_watch = (! deleting) && (fspec->wd < 0 || new_wd != fspec->wd);
1777 if (new_watch)
1779 if (0 <= fspec->wd)
1781 inotify_rm_watch (wd, fspec->wd);
1782 hash_remove (wd_to_name, fspec);
1785 fspec->wd = new_wd;
1787 if (new_wd == -1)
1788 continue;
1790 /* If the file was moved then inotify will use the source file wd
1791 for the destination file. Make sure the key is not present in
1792 the table. */
1793 struct File_spec *prev = hash_remove (wd_to_name, fspec);
1794 if (prev && prev != fspec)
1796 if (follow_mode == Follow_name)
1797 recheck (prev, false);
1798 prev->wd = -1;
1799 close_fd (prev->fd, pretty_name (prev));
1802 if (hash_insert (wd_to_name, fspec) == NULL)
1803 xalloc_die ();
1806 if (follow_mode == Follow_name)
1807 recheck (fspec, false);
1809 else
1811 struct File_spec key;
1812 key.wd = ev->wd;
1813 fspec = hash_lookup (wd_to_name, &key);
1816 if (! fspec)
1817 continue;
1819 if (ev->mask & (IN_ATTRIB | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF))
1821 /* Note for IN_MOVE_SELF (the file we're watching has
1822 been clobbered via a rename) we leave the watch
1823 in place since it may still be part of the set
1824 of watched names. */
1825 if (ev->mask & IN_DELETE_SELF)
1827 inotify_rm_watch (wd, fspec->wd);
1828 hash_remove (wd_to_name, fspec);
1831 /* Note we get IN_ATTRIB for unlink() as st_nlink decrements.
1832 The usual path is a close() done in recheck() triggers
1833 an IN_DELETE_SELF event as the inode is removed.
1834 However sometimes open() will succeed as even though
1835 st_nlink is decremented, the dentry (cache) is not updated.
1836 Thus we depend on the IN_DELETE event on the directory
1837 to trigger processing for the removed file. */
1839 recheck (fspec, false);
1841 continue;
1843 check_fspec (fspec, &prev_fspec);
1846 #endif
1848 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
1849 Return true if successful. */
1851 static bool
1852 tail_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
1853 uintmax_t *read_pos)
1855 struct stat stats;
1857 if (fstat (fd, &stats))
1859 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1860 return false;
1863 if (from_start)
1865 if (! presume_input_pipe && n_bytes <= OFF_T_MAX
1866 && ((S_ISREG (stats.st_mode)
1867 && xlseek (fd, n_bytes, SEEK_CUR, pretty_filename) >= 0)
1868 || lseek (fd, n_bytes, SEEK_CUR) != -1))
1869 *read_pos += n_bytes;
1870 else
1872 int t = start_bytes (pretty_filename, fd, n_bytes, read_pos);
1873 if (t)
1874 return t < 0;
1876 n_bytes = COPY_TO_EOF;
1878 else
1880 off_t end_pos = -1;
1881 off_t current_pos = -1;
1883 if (! presume_input_pipe && n_bytes <= OFF_T_MAX)
1885 if (usable_st_size (&stats))
1886 end_pos = stats.st_size;
1887 else if ((current_pos = lseek (fd, -n_bytes, SEEK_END)) != -1)
1888 end_pos = current_pos + n_bytes;
1890 if (end_pos <= (off_t) ST_BLKSIZE (stats))
1891 return pipe_bytes (pretty_filename, fd, n_bytes, read_pos);
1892 if (current_pos == -1)
1893 current_pos = xlseek (fd, 0, SEEK_CUR, pretty_filename);
1894 if (current_pos < end_pos)
1896 off_t bytes_remaining = end_pos - current_pos;
1898 if (n_bytes < bytes_remaining)
1900 current_pos = end_pos - n_bytes;
1901 xlseek (fd, current_pos, SEEK_SET, pretty_filename);
1904 *read_pos = current_pos;
1907 *read_pos += dump_remainder (false, pretty_filename, fd, n_bytes);
1908 return true;
1911 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
1912 Return true if successful. */
1914 static bool
1915 tail_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
1916 uintmax_t *read_pos)
1918 struct stat stats;
1920 if (fstat (fd, &stats))
1922 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1923 return false;
1926 if (from_start)
1928 int t = start_lines (pretty_filename, fd, n_lines, read_pos);
1929 if (t)
1930 return t < 0;
1931 *read_pos += dump_remainder (false, pretty_filename, fd, COPY_TO_EOF);
1933 else
1935 off_t start_pos = -1;
1936 off_t end_pos;
1938 /* Use file_lines only if FD refers to a regular file for
1939 which lseek (... SEEK_END) works. */
1940 if ( ! presume_input_pipe
1941 && S_ISREG (stats.st_mode)
1942 && (start_pos = lseek (fd, 0, SEEK_CUR)) != -1
1943 && start_pos < (end_pos = lseek (fd, 0, SEEK_END)))
1945 *read_pos = end_pos;
1946 if (end_pos != 0
1947 && ! file_lines (pretty_filename, fd, n_lines,
1948 start_pos, end_pos, read_pos))
1949 return false;
1951 else
1953 /* Under very unlikely circumstances, it is possible to reach
1954 this point after positioning the file pointer to end of file
1955 via the 'lseek (...SEEK_END)' above. In that case, reposition
1956 the file pointer back to start_pos before calling pipe_lines. */
1957 if (start_pos != -1)
1958 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
1960 return pipe_lines (pretty_filename, fd, n_lines, read_pos);
1963 return true;
1966 /* Display the last N_UNITS units of file FILENAME, open for reading
1967 via FD. Set *READ_POS to the position of the input stream pointer.
1968 *READ_POS is usually the number of bytes read and corresponds to an
1969 offset from the beginning of a file. However, it may be larger than
1970 OFF_T_MAX (as for an input pipe), and may also be larger than the
1971 number of bytes read (when an input pointer is initially not at
1972 beginning of file), and may be far greater than the number of bytes
1973 actually read for an input file that is seekable.
1974 Return true if successful. */
1976 static bool
1977 tail (char const *filename, int fd, uintmax_t n_units,
1978 uintmax_t *read_pos)
1980 *read_pos = 0;
1981 if (count_lines)
1982 return tail_lines (filename, fd, n_units, read_pos);
1983 else
1984 return tail_bytes (filename, fd, n_units, read_pos);
1987 /* Display the last N_UNITS units of the file described by F.
1988 Return true if successful. */
1990 static bool
1991 tail_file (struct File_spec *f, uintmax_t n_units)
1993 int fd;
1994 bool ok;
1996 bool is_stdin = (STREQ (f->name, "-"));
1998 if (is_stdin)
2000 have_read_stdin = true;
2001 fd = STDIN_FILENO;
2002 xset_binary_mode (STDIN_FILENO, O_BINARY);
2004 else
2005 fd = open (f->name, O_RDONLY | O_BINARY);
2007 f->tailable = !(reopen_inaccessible_files && fd == -1);
2009 if (fd == -1)
2011 if (forever)
2013 f->fd = -1;
2014 f->errnum = errno;
2015 f->ignore = ! reopen_inaccessible_files;
2016 f->ino = 0;
2017 f->dev = 0;
2019 error (0, errno, _("cannot open %s for reading"),
2020 quoteaf (pretty_name (f)));
2021 ok = false;
2023 else
2025 uintmax_t read_pos;
2027 if (print_headers)
2028 write_header (pretty_name (f));
2029 ok = tail (pretty_name (f), fd, n_units, &read_pos);
2030 if (forever)
2032 struct stat stats;
2034 #if TAIL_TEST_SLEEP
2035 /* Before the tail function provided 'read_pos', there was
2036 a race condition described in the URL below. This sleep
2037 call made the window big enough to exercise the problem. */
2038 xnanosleep (1);
2039 #endif
2040 f->errnum = ok - 1;
2041 if (fstat (fd, &stats) < 0)
2043 ok = false;
2044 f->errnum = errno;
2045 error (0, errno, _("error reading %s"),
2046 quoteaf (pretty_name (f)));
2048 else if (!IS_TAILABLE_FILE_TYPE (stats.st_mode))
2050 ok = false;
2051 f->errnum = -1;
2052 f->tailable = false;
2053 f->ignore = ! reopen_inaccessible_files;
2054 error (0, 0, _("%s: cannot follow end of this type of file%s"),
2055 quotef (pretty_name (f)),
2056 f->ignore ? _("; giving up on this name") : "");
2059 if (!ok)
2061 f->ignore = ! reopen_inaccessible_files;
2062 close_fd (fd, pretty_name (f));
2063 f->fd = -1;
2065 else
2067 /* Note: we must use read_pos here, not stats.st_size,
2068 to avoid a race condition described by Ken Raeburn:
2069 https://lists.gnu.org/r/bug-textutils/2003-05/msg00007.html */
2070 record_open_fd (f, fd, read_pos, &stats, (is_stdin ? -1 : 1));
2071 f->remote = fremote (fd, pretty_name (f));
2074 else
2076 if (!is_stdin && close (fd))
2078 error (0, errno, _("error reading %s"),
2079 quoteaf (pretty_name (f)));
2080 ok = false;
2085 return ok;
2088 /* If obsolete usage is allowed, and the command line arguments are of
2089 the obsolete form and the option string is well-formed, set
2090 *N_UNITS, the globals COUNT_LINES, FOREVER, and FROM_START, and
2091 return true. If the command line arguments are obviously incorrect
2092 (e.g., because obsolete usage is not allowed and the arguments are
2093 incorrect for non-obsolete usage), report an error and exit.
2094 Otherwise, return false and don't modify any parameter or global
2095 variable. */
2097 static bool
2098 parse_obsolete_option (int argc, char * const *argv, uintmax_t *n_units)
2100 char const *p;
2101 char const *n_string;
2102 char const *n_string_end;
2103 int default_count = DEFAULT_N_LINES;
2104 bool t_from_start;
2105 bool t_count_lines = true;
2106 bool t_forever = false;
2108 /* With the obsolete form, there is one option string and at most
2109 one file argument. Watch out for "-" and "--", though. */
2110 if (! (argc == 2
2111 || (argc == 3 && ! (argv[2][0] == '-' && argv[2][1]))
2112 || (3 <= argc && argc <= 4 && STREQ (argv[2], "--"))))
2113 return false;
2115 int posix_ver = posix2_version ();
2116 bool obsolete_usage = posix_ver < 200112;
2117 bool traditional_usage = obsolete_usage || 200809 <= posix_ver;
2118 p = argv[1];
2120 switch (*p++)
2122 default:
2123 return false;
2125 case '+':
2126 /* Leading "+" is a file name in the standard form. */
2127 if (!traditional_usage)
2128 return false;
2130 t_from_start = true;
2131 break;
2133 case '-':
2134 /* In the non-obsolete form, "-" is standard input and "-c"
2135 requires an option-argument. The obsolete multidigit options
2136 are supported as a GNU extension even when conforming to
2137 POSIX 1003.1-2001 or later, so don't complain about them. */
2138 if (!obsolete_usage && !p[p[0] == 'c'])
2139 return false;
2141 t_from_start = false;
2142 break;
2145 n_string = p;
2146 while (ISDIGIT (*p))
2147 p++;
2148 n_string_end = p;
2150 switch (*p)
2152 case 'b': default_count *= 512; FALLTHROUGH;
2153 case 'c': t_count_lines = false; FALLTHROUGH;
2154 case 'l': p++; break;
2157 if (*p == 'f')
2159 t_forever = true;
2160 ++p;
2163 if (*p)
2164 return false;
2166 if (n_string == n_string_end)
2167 *n_units = default_count;
2168 else if ((xstrtoumax (n_string, NULL, 10, n_units, "b")
2169 & ~LONGINT_INVALID_SUFFIX_CHAR)
2170 != LONGINT_OK)
2172 die (EXIT_FAILURE, errno, "%s: %s", _("invalid number"),
2173 quote (argv[1]));
2176 /* Set globals. */
2177 from_start = t_from_start;
2178 count_lines = t_count_lines;
2179 forever = t_forever;
2181 return true;
2184 static void
2185 parse_options (int argc, char **argv,
2186 uintmax_t *n_units, enum header_mode *header_mode,
2187 double *sleep_interval)
2189 int c;
2191 while ((c = getopt_long (argc, argv, "c:n:fFqs:vz0123456789",
2192 long_options, NULL))
2193 != -1)
2195 switch (c)
2197 case 'F':
2198 forever = true;
2199 follow_mode = Follow_name;
2200 reopen_inaccessible_files = true;
2201 break;
2203 case 'c':
2204 case 'n':
2205 count_lines = (c == 'n');
2206 if (*optarg == '+')
2207 from_start = true;
2208 else if (*optarg == '-')
2209 ++optarg;
2211 *n_units = xdectoumax (optarg, 0, UINTMAX_MAX, "bkKmMGTPEZY0",
2212 count_lines
2213 ? _("invalid number of lines")
2214 : _("invalid number of bytes"), 0);
2215 break;
2217 case 'f':
2218 case LONG_FOLLOW_OPTION:
2219 forever = true;
2220 if (optarg == NULL)
2221 follow_mode = DEFAULT_FOLLOW_MODE;
2222 else
2223 follow_mode = XARGMATCH ("--follow", optarg,
2224 follow_mode_string, follow_mode_map);
2225 break;
2227 case RETRY_OPTION:
2228 reopen_inaccessible_files = true;
2229 break;
2231 case MAX_UNCHANGED_STATS_OPTION:
2232 /* --max-unchanged-stats=N */
2233 max_n_unchanged_stats_between_opens =
2234 xdectoumax (optarg, 0, UINTMAX_MAX, "",
2235 _("invalid maximum number of unchanged stats between opens"), 0);
2236 break;
2238 case DISABLE_INOTIFY_OPTION:
2239 disable_inotify = true;
2240 break;
2242 case PID_OPTION:
2243 pid = xdectoumax (optarg, 0, PID_T_MAX, "", _("invalid PID"), 0);
2244 break;
2246 case PRESUME_INPUT_PIPE_OPTION:
2247 presume_input_pipe = true;
2248 break;
2250 case 'q':
2251 *header_mode = never;
2252 break;
2254 case 's':
2256 double s;
2257 if (! (xstrtod (optarg, NULL, &s, cl_strtod) && 0 <= s))
2258 die (EXIT_FAILURE, 0,
2259 _("invalid number of seconds: %s"), quote (optarg));
2260 *sleep_interval = s;
2262 break;
2264 case 'v':
2265 *header_mode = always;
2266 break;
2268 case 'z':
2269 line_end = '\0';
2270 break;
2272 case_GETOPT_HELP_CHAR;
2274 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
2276 case '0': case '1': case '2': case '3': case '4':
2277 case '5': case '6': case '7': case '8': case '9':
2278 die (EXIT_FAILURE, 0, _("option used in invalid context -- %c"), c);
2280 default:
2281 usage (EXIT_FAILURE);
2285 if (reopen_inaccessible_files)
2287 if (!forever)
2289 reopen_inaccessible_files = false;
2290 error (0, 0, _("warning: --retry ignored; --retry is useful"
2291 " only when following"));
2293 else if (follow_mode == Follow_descriptor)
2294 error (0, 0, _("warning: --retry only effective for the initial open"));
2297 if (pid && !forever)
2298 error (0, 0,
2299 _("warning: PID ignored; --pid=PID is useful only when following"));
2300 else if (pid && kill (pid, 0) != 0 && errno == ENOSYS)
2302 error (0, 0, _("warning: --pid=PID is not supported on this system"));
2303 pid = 0;
2307 /* Mark as '.ignore'd each member of F that corresponds to a
2308 pipe or fifo, and return the number of non-ignored members. */
2309 static size_t
2310 ignore_fifo_and_pipe (struct File_spec *f, size_t n_files)
2312 /* When there is no FILE operand and stdin is a pipe or FIFO
2313 POSIX requires that tail ignore the -f option.
2314 Since we allow multiple FILE operands, we extend that to say: with -f,
2315 ignore any "-" operand that corresponds to a pipe or FIFO. */
2316 size_t n_viable = 0;
2318 for (size_t i = 0; i < n_files; i++)
2320 bool is_a_fifo_or_pipe =
2321 (STREQ (f[i].name, "-")
2322 && !f[i].ignore
2323 && 0 <= f[i].fd
2324 && (S_ISFIFO (f[i].mode)
2325 || (HAVE_FIFO_PIPES != 1 && isapipe (f[i].fd))));
2326 if (is_a_fifo_or_pipe)
2328 f[i].fd = -1;
2329 f[i].ignore = true;
2331 else
2332 ++n_viable;
2335 return n_viable;
2339 main (int argc, char **argv)
2341 enum header_mode header_mode = multiple_files;
2342 bool ok = true;
2343 /* If from_start, the number of items to skip before printing; otherwise,
2344 the number of items at the end of the file to print. Although the type
2345 is signed, the value is never negative. */
2346 uintmax_t n_units = DEFAULT_N_LINES;
2347 size_t n_files;
2348 char **file;
2349 struct File_spec *F;
2350 size_t i;
2351 bool obsolete_option;
2353 /* The number of seconds to sleep between iterations.
2354 During one iteration, every file name or descriptor is checked to
2355 see if it has changed. */
2356 double sleep_interval = 1.0;
2358 initialize_main (&argc, &argv);
2359 set_program_name (argv[0]);
2360 setlocale (LC_ALL, "");
2361 bindtextdomain (PACKAGE, LOCALEDIR);
2362 textdomain (PACKAGE);
2364 atexit (close_stdout);
2366 have_read_stdin = false;
2368 count_lines = true;
2369 forever = from_start = print_headers = false;
2370 line_end = '\n';
2371 obsolete_option = parse_obsolete_option (argc, argv, &n_units);
2372 argc -= obsolete_option;
2373 argv += obsolete_option;
2374 parse_options (argc, argv, &n_units, &header_mode, &sleep_interval);
2376 /* To start printing with item N_UNITS from the start of the file, skip
2377 N_UNITS - 1 items. 'tail -n +0' is actually meaningless, but for Unix
2378 compatibility it's treated the same as 'tail -n +1'. */
2379 if (from_start)
2381 if (n_units)
2382 --n_units;
2385 IF_LINT (assert (0 <= argc));
2387 if (optind < argc)
2389 n_files = argc - optind;
2390 file = argv + optind;
2392 else
2394 static char *dummy_stdin = (char *) "-";
2395 n_files = 1;
2396 file = &dummy_stdin;
2400 bool found_hyphen = false;
2402 for (i = 0; i < n_files; i++)
2403 if (STREQ (file[i], "-"))
2404 found_hyphen = true;
2406 /* When following by name, there must be a name. */
2407 if (found_hyphen && follow_mode == Follow_name)
2408 die (EXIT_FAILURE, 0, _("cannot follow %s by name"), quoteaf ("-"));
2410 /* When following forever, and not using simple blocking, warn if
2411 any file is '-' as the stats() used to check for input are ineffective.
2412 This is only a warning, since tail's output (before a failing seek,
2413 and that from any non-stdin files) might still be useful. */
2414 if (forever && found_hyphen)
2416 struct stat in_stat;
2417 bool blocking_stdin;
2418 blocking_stdin = (pid == 0 && follow_mode == Follow_descriptor
2419 && n_files == 1 && ! fstat (STDIN_FILENO, &in_stat)
2420 && ! S_ISREG (in_stat.st_mode));
2422 if (! blocking_stdin && isatty (STDIN_FILENO))
2423 error (0, 0, _("warning: following standard input"
2424 " indefinitely is ineffective"));
2428 /* Don't read anything if we'll never output anything. */
2429 if (! n_units && ! forever && ! from_start)
2430 return EXIT_SUCCESS;
2432 F = xnmalloc (n_files, sizeof *F);
2433 for (i = 0; i < n_files; i++)
2434 F[i].name = file[i];
2436 if (header_mode == always
2437 || (header_mode == multiple_files && n_files > 1))
2438 print_headers = true;
2440 xset_binary_mode (STDOUT_FILENO, O_BINARY);
2442 for (i = 0; i < n_files; i++)
2443 ok &= tail_file (&F[i], n_units);
2445 if (forever && ignore_fifo_and_pipe (F, n_files))
2447 /* If stdout is a fifo or pipe, then monitor it
2448 so that we exit if the reader goes away. */
2449 struct stat out_stat;
2450 if (fstat (STDOUT_FILENO, &out_stat) < 0)
2451 die (EXIT_FAILURE, errno, _("standard output"));
2452 monitor_output = (S_ISFIFO (out_stat.st_mode)
2453 || (HAVE_FIFO_PIPES != 1 && isapipe (STDOUT_FILENO)));
2455 #if HAVE_INOTIFY
2456 /* tailable_stdin() checks if the user specifies stdin via "-",
2457 or implicitly by providing no arguments. If so, we won't use inotify.
2458 Technically, on systems with a working /dev/stdin, we *could*,
2459 but would it be worth it? Verifying that it's a real device
2460 and hooked up to stdin is not trivial, while reverting to
2461 non-inotify-based tail_forever is easy and portable.
2463 any_remote_file() checks if the user has specified any
2464 files that reside on remote file systems. inotify is not used
2465 in this case because it would miss any updates to the file
2466 that were not initiated from the local system.
2468 any_non_remote_file() checks if the user has specified any
2469 files that don't reside on remote file systems. inotify is not used
2470 if there are no open files, as we can't determine if those file
2471 will be on a remote file system.
2473 any_symlinks() checks if the user has specified any symbolic links.
2474 inotify is not used in this case because it returns updated _targets_
2475 which would not match the specified names. If we tried to always
2476 use the target names, then we would miss changes to the symlink itself.
2478 ok is false when one of the files specified could not be opened for
2479 reading. In this case and when following by descriptor,
2480 tail_forever_inotify() cannot be used (in its current implementation).
2482 FIXME: inotify doesn't give any notification when a new
2483 (remote) file or directory is mounted on top a watched file.
2484 When follow_mode == Follow_name we would ideally like to detect that.
2485 Note if there is a change to the original file then we'll
2486 recheck it and follow the new file, or ignore it if the
2487 file has changed to being remote.
2489 FIXME-maybe: inotify has a watch descriptor per inode, and hence with
2490 our current hash implementation will only --follow data for one
2491 of the names when multiple hardlinked files are specified, or
2492 for one name when a name is specified multiple times. */
2493 if (!disable_inotify && (tailable_stdin (F, n_files)
2494 || any_remote_file (F, n_files)
2495 || ! any_non_remote_file (F, n_files)
2496 || any_symlinks (F, n_files)
2497 || any_non_regular_fifo (F, n_files)
2498 || (!ok && follow_mode == Follow_descriptor)))
2499 disable_inotify = true;
2501 if (!disable_inotify)
2503 int wd = inotify_init ();
2504 if (0 <= wd)
2506 /* Flush any output from tail_file, now, since
2507 tail_forever_inotify flushes only after writing,
2508 not before reading. */
2509 if (fflush (stdout) != 0)
2510 die (EXIT_FAILURE, errno, _("write error"));
2512 if (! tail_forever_inotify (wd, F, n_files, sleep_interval))
2513 return EXIT_FAILURE;
2515 error (0, errno, _("inotify cannot be used, reverting to polling"));
2517 /* Free resources as this process can be long lived,
2518 and we may have exhausted system resources above. */
2520 for (i = 0; i < n_files; i++)
2522 /* It's OK to remove the same watch multiple times,
2523 ignoring the EINVAL from redundant calls. */
2524 if (F[i].wd != -1)
2525 inotify_rm_watch (wd, F[i].wd);
2526 if (F[i].parent_wd != -1)
2527 inotify_rm_watch (wd, F[i].parent_wd);
2530 #endif
2531 disable_inotify = true;
2532 tail_forever (F, n_files, sleep_interval);
2535 IF_LINT (free (F));
2537 if (have_read_stdin && close (STDIN_FILENO) < 0)
2538 die (EXIT_FAILURE, errno, "-");
2539 return ok ? EXIT_SUCCESS : EXIT_FAILURE;