maint: factor out common macros of stat and printf
[coreutils.git] / src / tail.c
bloba3b46ca2d93dc95dd61805da999f4206c1147312
1 /* tail -- output the last part of file(s)
2 Copyright (C) 1989-2024 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 IDs of the processes to watch (those writing the followed
206 files, or perhaps other processes the user cares about). */
207 static int nbpids = 0;
208 static pid_t * pids = nullptr;
209 static idx_t pids_alloc;
211 /* Used to determine the buffer size when scanning backwards in a file. */
212 static idx_t page_size;
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, nullptr, 'c'},
240 {"follow", optional_argument, nullptr, LONG_FOLLOW_OPTION},
241 {"lines", required_argument, nullptr, 'n'},
242 {"max-unchanged-stats", required_argument, nullptr,
243 MAX_UNCHANGED_STATS_OPTION},
244 {"-disable-inotify", no_argument, nullptr,
245 DISABLE_INOTIFY_OPTION}, /* do not document */
246 {"pid", required_argument, nullptr, PID_OPTION},
247 {"-presume-input-pipe", no_argument, nullptr,
248 PRESUME_INPUT_PIPE_OPTION}, /* do not document */
249 {"quiet", no_argument, nullptr, 'q'},
250 {"retry", no_argument, nullptr, RETRY_OPTION},
251 {"silent", no_argument, nullptr, 'q'},
252 {"sleep-interval", required_argument, nullptr, 's'},
253 {"verbose", no_argument, nullptr, 'v'},
254 {"zero-terminated", no_argument, nullptr, 'z'},
255 {GETOPT_HELP_OPTION_DECL},
256 {GETOPT_VERSION_OPTION_DECL},
257 {nullptr, 0, nullptr, 0}
260 void
261 usage (int status)
263 if (status != EXIT_SUCCESS)
264 emit_try_help ();
265 else
267 printf (_("\
268 Usage: %s [OPTION]... [FILE]...\n\
270 program_name);
271 printf (_("\
272 Print the last %d lines of each FILE to standard output.\n\
273 With more than one FILE, precede each with a header giving the file name.\n\
274 "), DEFAULT_N_LINES);
276 emit_stdin_note ();
277 emit_mandatory_arg_note ();
279 fputs (_("\
280 -c, --bytes=[+]NUM output the last NUM bytes; or use -c +NUM to\n\
281 output starting with byte NUM of each file\n\
282 "), stdout);
283 fputs (_("\
284 -f, --follow[={name|descriptor}]\n\
285 output appended data as the file grows;\n\
286 an absent option argument means 'descriptor'\n\
287 -F same as --follow=name --retry\n\
288 "), stdout);
289 printf (_("\
290 -n, --lines=[+]NUM output the last NUM lines, instead of the last %d;\n\
291 or use -n +NUM to skip NUM-1 lines at the start\n\
293 DEFAULT_N_LINES
295 printf (_("\
296 --max-unchanged-stats=N\n\
297 with --follow=name, reopen a FILE which has not\n\
298 changed size after N (default %d) iterations\n\
299 to see if it has been unlinked or renamed\n\
300 (this is the usual case of rotated log files);\n\
301 with inotify, this option is rarely useful\n\
303 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS
305 fputs (_("\
306 --pid=PID with -f, terminate after process ID, PID dies;\n\
307 can be repeated to watch multiple processes\n\
308 -q, --quiet, --silent never output headers giving file names\n\
309 --retry keep trying to open a file if it is inaccessible\n\
310 "), stdout);
311 fputs (_("\
312 -s, --sleep-interval=N with -f, sleep for approximately N seconds\n\
313 (default 1.0) between iterations;\n\
314 with inotify and --pid=P, check process P at\n\
315 least once every N seconds\n\
316 -v, --verbose always output headers giving file names\n\
317 "), stdout);
318 fputs (_("\
319 -z, --zero-terminated line delimiter is NUL, not newline\n\
320 "), stdout);
321 fputs (HELP_OPTION_DESCRIPTION, stdout);
322 fputs (VERSION_OPTION_DESCRIPTION, stdout);
323 fputs (_("\
325 NUM may have a multiplier suffix:\n\
326 b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\
327 GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y, R, Q.\n\
328 Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\
330 "), stdout);
331 fputs (_("\
332 With --follow (-f), tail defaults to following the file descriptor, which\n\
333 means that even if a tail'ed file is renamed, tail will continue to track\n\
334 its end. This default behavior is not desirable when you really want to\n\
335 track the actual name of the file, not the file descriptor (e.g., log\n\
336 rotation). Use --follow=name in that case. That causes tail to track the\n\
337 named file in a way that accommodates renaming, removal and creation.\n\
338 "), stdout);
339 emit_ancillary_info (PROGRAM_NAME);
341 exit (status);
344 /* Ensure exit, either with SIGPIPE or EXIT_FAILURE status. */
345 static void
346 die_pipe (void)
348 raise (SIGPIPE);
349 exit (EXIT_FAILURE);
352 /* If the output has gone away, then terminate
353 as we would if we had written to this output. */
354 static void
355 check_output_alive (void)
357 if (! monitor_output)
358 return;
360 if (iopoll (-1, STDOUT_FILENO, false) == IOPOLL_BROKEN_OUTPUT)
361 die_pipe ();
364 MAYBE_UNUSED static bool
365 valid_file_spec (struct File_spec const *f)
367 /* Exactly one of the following subexpressions must be true. */
368 return ((f->fd == -1) ^ (f->errnum == 0));
371 static char const *
372 pretty_name (struct File_spec const *f)
374 return (STREQ (f->name, "-") ? _("standard input") : f->name);
377 /* Record a file F with descriptor FD, size SIZE, status ST, and
378 blocking status BLOCKING. */
380 static void
381 record_open_fd (struct File_spec *f, int fd,
382 off_t size, struct stat const *st,
383 int blocking)
385 f->fd = fd;
386 f->size = size;
387 f->mtime = get_stat_mtime (st);
388 f->dev = st->st_dev;
389 f->ino = st->st_ino;
390 f->mode = st->st_mode;
391 f->blocking = blocking;
392 f->n_unchanged_stats = 0;
393 f->ignore = false;
396 /* Close the file with descriptor FD and name FILENAME. */
398 static void
399 close_fd (int fd, char const *filename)
401 if (fd != -1 && fd != STDIN_FILENO && close (fd))
403 error (0, errno, _("closing %s (fd=%d)"), quoteaf (filename), fd);
407 static void
408 write_header (char const *pretty_filename)
410 static bool first_file = true;
412 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), pretty_filename);
413 first_file = false;
416 /* Write N_BYTES from BUFFER to stdout.
417 Exit immediately on error with a single diagnostic. */
419 static void
420 xwrite_stdout (char const *buffer, size_t n_bytes)
422 if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) < n_bytes)
424 clearerr (stdout); /* To avoid redundant close_stdout diagnostic. */
425 error (EXIT_FAILURE, errno, _("error writing %s"),
426 quoteaf ("standard output"));
430 /* Read and output N_BYTES of file PRETTY_FILENAME starting at the current
431 position in FD. If N_BYTES is COPY_TO_EOF, then copy until end of file.
432 If N_BYTES is COPY_A_BUFFER, then copy at most one buffer's worth.
433 Return the number of bytes read from the file. */
435 static uintmax_t
436 dump_remainder (bool want_header, char const *pretty_filename, int fd,
437 uintmax_t n_bytes)
439 uintmax_t n_written;
440 uintmax_t n_remaining = n_bytes;
442 n_written = 0;
443 while (true)
445 char buffer[BUFSIZ];
446 size_t n = MIN (n_remaining, BUFSIZ);
447 size_t bytes_read = safe_read (fd, buffer, n);
448 if (bytes_read == SAFE_READ_ERROR)
450 if (errno != EAGAIN)
451 error (EXIT_FAILURE, errno, _("error reading %s"),
452 quoteaf (pretty_filename));
453 break;
455 if (bytes_read == 0)
456 break;
457 if (want_header)
459 write_header (pretty_filename);
460 want_header = false;
462 xwrite_stdout (buffer, bytes_read);
463 n_written += bytes_read;
464 if (n_bytes != COPY_TO_EOF)
466 n_remaining -= bytes_read;
467 if (n_remaining == 0 || n_bytes == COPY_A_BUFFER)
468 break;
472 return n_written;
475 /* Call lseek with the specified arguments, where file descriptor FD
476 corresponds to the file, FILENAME.
477 Give a diagnostic and exit nonzero if lseek fails.
478 Otherwise, return the resulting offset. */
480 static off_t
481 xlseek (int fd, off_t offset, int whence, char const *filename)
483 off_t new_offset = lseek (fd, offset, whence);
484 char buf[INT_BUFSIZE_BOUND (offset)];
485 char *s;
487 if (0 <= new_offset)
488 return new_offset;
490 s = offtostr (offset, buf);
491 switch (whence)
493 case SEEK_SET:
494 error (EXIT_FAILURE, errno, _("%s: cannot seek to offset %s"),
495 quotef (filename), s);
496 break;
497 case SEEK_CUR:
498 error (EXIT_FAILURE, errno, _("%s: cannot seek to relative offset %s"),
499 quotef (filename), s);
500 break;
501 case SEEK_END:
502 error (EXIT_FAILURE, errno,
503 _("%s: cannot seek to end-relative offset %s"),
504 quotef (filename), s);
505 break;
506 default:
507 unreachable ();
511 /* Print the last N_LINES lines from the end of file FD.
512 Go backward through the file, reading 'BUFSIZ' bytes at a time (except
513 probably the first), until we hit the start of the file or have
514 read NUMBER newlines.
515 START_POS is the starting position of the read pointer for the file
516 associated with FD (may be nonzero).
517 END_POS is the file offset of EOF (one larger than offset of last byte).
518 Return true if successful. */
520 static bool
521 file_lines (char const *pretty_filename, int fd, struct stat const *sb,
522 uintmax_t n_lines, off_t start_pos, off_t end_pos,
523 uintmax_t *read_pos)
525 char *buffer;
526 size_t bytes_read;
527 blksize_t bufsize = BUFSIZ;
528 off_t pos = end_pos;
529 bool ok = true;
531 if (n_lines == 0)
532 return true;
534 /* Be careful with files with sizes that are a multiple of the page size,
535 as on /proc or /sys file systems these files accept seeking to within
536 the file, but then return no data when read. So use a buffer that's
537 at least PAGE_SIZE to avoid seeking within such files.
539 We could also indirectly use a large enough buffer through io_blksize()
540 however this would be less efficient in the common case, as it would
541 generally pick a larger buffer size, resulting in reading more data
542 from the end of the file. */
543 affirm (S_ISREG (sb->st_mode));
544 if (sb->st_size % page_size == 0)
545 bufsize = MAX (BUFSIZ, page_size);
547 buffer = xmalloc (bufsize);
549 /* Set 'bytes_read' to the size of the last, probably partial, buffer;
550 0 < 'bytes_read' <= 'bufsize'. */
551 bytes_read = (pos - start_pos) % bufsize;
552 if (bytes_read == 0)
553 bytes_read = bufsize;
554 /* Make 'pos' a multiple of 'bufsize' (0 if the file is short), so that all
555 reads will be on block boundaries, which might increase efficiency. */
556 pos -= bytes_read;
557 xlseek (fd, pos, SEEK_SET, pretty_filename);
558 bytes_read = safe_read (fd, buffer, bytes_read);
559 if (bytes_read == SAFE_READ_ERROR)
561 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
562 ok = false;
563 goto free_buffer;
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 == nullptr)
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 goto free_buffer;
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 goto free_buffer;
604 pos -= bufsize;
605 xlseek (fd, pos, SEEK_SET, pretty_filename);
607 bytes_read = safe_read (fd, buffer, bufsize);
608 if (bytes_read == SAFE_READ_ERROR)
610 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
611 ok = false;
612 goto free_buffer;
615 *read_pos = pos + bytes_read;
617 while (bytes_read > 0);
619 free_buffer:
620 free (buffer);
621 return ok;
624 /* Print the last N_LINES lines from the end of the standard input,
625 open for reading as pipe FD.
626 Buffer the text as a linked list of LBUFFERs, adding them as needed.
627 Return true if successful. */
629 static bool
630 pipe_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
631 uintmax_t *read_pos)
633 struct linebuffer
635 char buffer[BUFSIZ];
636 size_t nbytes;
637 size_t nlines;
638 struct linebuffer *next;
640 typedef struct linebuffer LBUFFER;
641 LBUFFER *first, *last, *tmp;
642 size_t total_lines = 0; /* Total number of newlines in all buffers. */
643 bool ok = true;
644 size_t n_read; /* Size in bytes of most recent read */
646 first = last = xmalloc (sizeof (LBUFFER));
647 first->nbytes = first->nlines = 0;
648 first->next = nullptr;
649 tmp = xmalloc (sizeof (LBUFFER));
651 /* Input is always read into a fresh buffer. */
652 while (true)
654 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
655 if (n_read == 0 || n_read == SAFE_READ_ERROR)
656 break;
657 tmp->nbytes = n_read;
658 *read_pos += n_read;
659 tmp->nlines = 0;
660 tmp->next = nullptr;
662 /* Count the number of newlines just read. */
664 char const *buffer_end = tmp->buffer + n_read;
665 char const *p = tmp->buffer;
666 while ((p = memchr (p, line_end, buffer_end - p)))
668 ++p;
669 ++tmp->nlines;
672 total_lines += tmp->nlines;
674 /* If there is enough room in the last buffer read, just append the new
675 one to it. This is because when reading from a pipe, 'n_read' can
676 often be very small. */
677 if (tmp->nbytes + last->nbytes < BUFSIZ)
679 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
680 last->nbytes += tmp->nbytes;
681 last->nlines += tmp->nlines;
683 else
685 /* If there's not enough room, link the new buffer onto the end of
686 the list, then either free up the oldest buffer for the next
687 read if that would leave enough lines, or else malloc a new one.
688 Some compaction mechanism is possible but probably not
689 worthwhile. */
690 last = last->next = tmp;
691 if (total_lines - first->nlines > n_lines)
693 tmp = first;
694 total_lines -= first->nlines;
695 first = first->next;
697 else
698 tmp = xmalloc (sizeof (LBUFFER));
702 free (tmp);
704 if (n_read == SAFE_READ_ERROR)
706 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
707 ok = false;
708 goto free_lbuffers;
711 /* If the file is empty, then bail out. */
712 if (last->nbytes == 0)
713 goto free_lbuffers;
715 /* This prevents a core dump when the pipe contains no newlines. */
716 if (n_lines == 0)
717 goto free_lbuffers;
719 /* Count the incomplete line on files that don't end with a newline. */
720 if (last->buffer[last->nbytes - 1] != line_end)
722 ++last->nlines;
723 ++total_lines;
726 /* Run through the list, printing lines. First, skip over unneeded
727 buffers. */
728 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
729 total_lines -= tmp->nlines;
731 /* Find the correct beginning, then print the rest of the file. */
733 char const *beg = tmp->buffer;
734 char const *buffer_end = tmp->buffer + tmp->nbytes;
735 if (total_lines > n_lines)
737 /* Skip 'total_lines' - 'n_lines' newlines. We made sure that
738 'total_lines' - 'n_lines' <= 'tmp->nlines'. */
739 size_t j;
740 for (j = total_lines - n_lines; j; --j)
742 beg = rawmemchr (beg, line_end);
743 ++beg;
747 xwrite_stdout (beg, buffer_end - beg);
750 for (tmp = tmp->next; tmp; tmp = tmp->next)
751 xwrite_stdout (tmp->buffer, tmp->nbytes);
753 free_lbuffers:
754 while (first)
756 tmp = first->next;
757 free (first);
758 first = tmp;
760 return ok;
763 /* Print the last N_BYTES characters from the end of FD.
764 Work even if the input is a pipe.
765 This is a stripped down version of pipe_lines.
766 Return true if successful. */
768 static bool
769 pipe_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
770 uintmax_t *read_pos)
772 struct charbuffer
774 char buffer[BUFSIZ];
775 size_t nbytes;
776 struct charbuffer *next;
778 typedef struct charbuffer CBUFFER;
779 CBUFFER *first, *last, *tmp;
780 size_t i; /* Index into buffers. */
781 size_t total_bytes = 0; /* Total characters in all buffers. */
782 bool ok = true;
783 size_t n_read;
785 first = last = xmalloc (sizeof (CBUFFER));
786 first->nbytes = 0;
787 first->next = nullptr;
788 tmp = xmalloc (sizeof (CBUFFER));
790 /* Input is always read into a fresh buffer. */
791 while (true)
793 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
794 if (n_read == 0 || n_read == SAFE_READ_ERROR)
795 break;
796 *read_pos += n_read;
797 tmp->nbytes = n_read;
798 tmp->next = nullptr;
800 total_bytes += tmp->nbytes;
801 /* If there is enough room in the last buffer read, just append the new
802 one to it. This is because when reading from a pipe, 'nbytes' can
803 often be very small. */
804 if (tmp->nbytes + last->nbytes < BUFSIZ)
806 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
807 last->nbytes += tmp->nbytes;
809 else
811 /* If there's not enough room, link the new buffer onto the end of
812 the list, then either free up the oldest buffer for the next
813 read if that would leave enough characters, or else malloc a new
814 one. Some compaction mechanism is possible but probably not
815 worthwhile. */
816 last = last->next = tmp;
817 if (total_bytes - first->nbytes > n_bytes)
819 tmp = first;
820 total_bytes -= first->nbytes;
821 first = first->next;
823 else
825 tmp = xmalloc (sizeof (CBUFFER));
830 free (tmp);
832 if (n_read == SAFE_READ_ERROR)
834 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
835 ok = false;
836 goto free_cbuffers;
839 /* Run through the list, printing characters. First, skip over unneeded
840 buffers. */
841 for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
842 total_bytes -= tmp->nbytes;
844 /* Find the correct beginning, then print the rest of the file.
845 We made sure that 'total_bytes' - 'n_bytes' <= 'tmp->nbytes'. */
846 if (total_bytes > n_bytes)
847 i = total_bytes - n_bytes;
848 else
849 i = 0;
850 xwrite_stdout (&tmp->buffer[i], tmp->nbytes - i);
852 for (tmp = tmp->next; tmp; tmp = tmp->next)
853 xwrite_stdout (tmp->buffer, tmp->nbytes);
855 free_cbuffers:
856 while (first)
858 tmp = first->next;
859 free (first);
860 first = tmp;
862 return ok;
865 /* Skip N_BYTES characters from the start of pipe FD, and print
866 any extra characters that were read beyond that.
867 Return 1 on error, 0 if ok, -1 if EOF. */
869 static int
870 start_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
871 uintmax_t *read_pos)
873 char buffer[BUFSIZ];
875 while (0 < n_bytes)
877 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
878 if (bytes_read == 0)
879 return -1;
880 if (bytes_read == SAFE_READ_ERROR)
882 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
883 return 1;
885 *read_pos += bytes_read;
886 if (bytes_read <= n_bytes)
887 n_bytes -= bytes_read;
888 else
890 size_t n_remaining = bytes_read - n_bytes;
891 /* Print extra characters if there are any. */
892 xwrite_stdout (&buffer[n_bytes], n_remaining);
893 break;
897 return 0;
900 /* Skip N_LINES lines at the start of file or pipe FD, and print
901 any extra characters that were read beyond that.
902 Return 1 on error, 0 if ok, -1 if EOF. */
904 static int
905 start_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
906 uintmax_t *read_pos)
908 if (n_lines == 0)
909 return 0;
911 while (true)
913 char buffer[BUFSIZ];
914 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
915 if (bytes_read == 0) /* EOF */
916 return -1;
917 if (bytes_read == SAFE_READ_ERROR) /* error */
919 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
920 return 1;
923 char *buffer_end = buffer + bytes_read;
925 *read_pos += bytes_read;
927 char *p = buffer;
928 while ((p = memchr (p, line_end, buffer_end - p)))
930 ++p;
931 if (--n_lines == 0)
933 if (p < buffer_end)
934 xwrite_stdout (p, buffer_end - p);
935 return 0;
941 /* Return false when FD is open on a file residing on a local file system.
942 If fstatfs fails, give a diagnostic and return true.
943 If fstatfs cannot be called, return true. */
944 static bool
945 fremote (int fd, char const *name)
947 bool remote = true; /* be conservative (poll by default). */
949 #if HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE \
950 && (defined __linux__ || defined __ANDROID__)
951 struct statfs buf;
952 int err = fstatfs (fd, &buf);
953 if (err != 0)
955 /* On at least linux-2.6.38, fstatfs fails with ENOSYS when FD
956 is open on a pipe. Treat that like a remote file. */
957 if (errno != ENOSYS)
958 error (0, errno, _("cannot determine location of %s. "
959 "reverting to polling"), quoteaf (name));
961 else
963 /* Treat unrecognized file systems as "remote", so caller polls.
964 Note README-release has instructions for syncing the internal
965 list with the latest Linux kernel file system constants. */
966 remote = is_local_fs_type (buf.f_type) <= 0;
968 #endif
970 return remote;
973 /* open/fstat F->name and handle changes. */
974 static void
975 recheck (struct File_spec *f, bool blocking)
977 struct stat new_stats;
978 bool ok = true;
979 bool is_stdin = (STREQ (f->name, "-"));
980 bool was_tailable = f->tailable;
981 int prev_errnum = f->errnum;
982 bool new_file;
983 int fd = (is_stdin
984 ? STDIN_FILENO
985 : open (f->name, O_RDONLY | (blocking ? 0 : O_NONBLOCK)));
987 affirm (valid_file_spec (f));
989 /* If the open fails because the file doesn't exist,
990 then mark the file as not tailable. */
991 f->tailable = !(reopen_inaccessible_files && fd == -1);
993 if (! disable_inotify && ! lstat (f->name, &new_stats)
994 && S_ISLNK (new_stats.st_mode))
996 /* Diagnose the edge case where a regular file is changed
997 to a symlink. We avoid inotify with symlinks since
998 it's awkward to match between symlink name and target. */
999 ok = false;
1000 f->errnum = -1;
1001 f->ignore = true;
1003 error (0, 0, _("%s has been replaced with an untailable symbolic link"),
1004 quoteaf (pretty_name (f)));
1006 else if (fd == -1 || fstat (fd, &new_stats) < 0)
1008 ok = false;
1009 f->errnum = errno;
1010 if (!f->tailable)
1012 if (was_tailable)
1014 /* FIXME-maybe: detect the case in which the file first becomes
1015 unreadable (perms), and later becomes readable again and can
1016 be seen to be the same file (dev/ino). Otherwise, tail prints
1017 the entire contents of the file when it becomes readable. */
1018 error (0, f->errnum, _("%s has become inaccessible"),
1019 quoteaf (pretty_name (f)));
1021 else
1023 /* say nothing... it's still not tailable */
1026 else if (prev_errnum != errno)
1027 error (0, errno, "%s", quotef (pretty_name (f)));
1029 else if (!IS_TAILABLE_FILE_TYPE (new_stats.st_mode))
1031 ok = false;
1032 f->errnum = -1;
1033 f->tailable = false;
1034 f->ignore = ! (reopen_inaccessible_files && follow_mode == Follow_name);
1035 if (was_tailable || prev_errnum != f->errnum)
1036 error (0, 0, _("%s has been replaced with an untailable file%s"),
1037 quoteaf (pretty_name (f)),
1038 f->ignore ? _("; giving up on this name") : "");
1040 else if ((f->remote = fremote (fd, pretty_name (f))) && ! disable_inotify)
1042 ok = false;
1043 f->errnum = -1;
1044 error (0, 0, _("%s has been replaced with an untailable remote file"),
1045 quoteaf (pretty_name (f)));
1046 f->ignore = true;
1047 f->remote = true;
1049 else
1051 f->errnum = 0;
1054 new_file = false;
1055 if (!ok)
1057 close_fd (fd, pretty_name (f));
1058 close_fd (f->fd, pretty_name (f));
1059 f->fd = -1;
1061 else if (prev_errnum && prev_errnum != ENOENT)
1063 new_file = true;
1064 affirm (f->fd == -1);
1065 error (0, 0, _("%s has become accessible"), quoteaf (pretty_name (f)));
1067 else if (f->fd == -1)
1069 /* A new file even when inodes haven't changed as <dev,inode>
1070 pairs can be reused, and we know the file was missing
1071 on the previous iteration. Note this also means the file
1072 is redisplayed in --follow=name mode if renamed away from
1073 and back to a monitored name. */
1074 new_file = true;
1076 error (0, 0,
1077 _("%s has appeared; following new file"),
1078 quoteaf (pretty_name (f)));
1080 else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
1082 /* File has been replaced (e.g., via log rotation) --
1083 tail the new one. */
1084 new_file = true;
1086 error (0, 0,
1087 _("%s has been replaced; following new file"),
1088 quoteaf (pretty_name (f)));
1090 /* Close the old one. */
1091 close_fd (f->fd, pretty_name (f));
1094 else
1096 /* No changes detected, so close new fd. */
1097 close_fd (fd, pretty_name (f));
1100 /* FIXME: When a log is rotated, daemons tend to log to the
1101 old file descriptor until the new file is present and
1102 the daemon is sent a signal. Therefore tail may miss entries
1103 being written to the old file. Perhaps we should keep
1104 the older file open and continue to monitor it until
1105 data is written to a new file. */
1106 if (new_file)
1108 /* Start at the beginning of the file. */
1109 record_open_fd (f, fd, 0, &new_stats, (is_stdin ? -1 : blocking));
1110 if (S_ISREG (new_stats.st_mode))
1111 xlseek (fd, 0, SEEK_SET, pretty_name (f));
1115 /* Return true if any of the N_FILES files in F are live, i.e., have
1116 open file descriptors, or should be checked again (see --retry).
1117 When following descriptors, checking should only continue when any
1118 of the files is not yet ignored. */
1120 static bool
1121 any_live_files (const struct File_spec *f, size_t n_files)
1123 /* In inotify mode, ignore may be set for files
1124 which may later be replaced with new files.
1125 So always consider files live in -F mode. */
1126 if (reopen_inaccessible_files && follow_mode == Follow_name)
1127 return true;
1129 for (size_t i = 0; i < n_files; i++)
1131 if (0 <= f[i].fd)
1132 return true;
1133 else
1135 if (! f[i].ignore && reopen_inaccessible_files)
1136 return true;
1140 return false;
1143 /* Determine whether all watched writers are dead.
1144 Returns true only if all processes' states can be determined,
1145 and all processes no longer exist. */
1147 static bool
1148 writers_are_dead (void)
1150 if (!nbpids)
1151 return false;
1153 for (int i = 0; i < nbpids; i++)
1155 if (kill (pids[i], 0) == 0 || errno == EPERM)
1156 return false;
1159 return true;
1162 /* Tail N_FILES files forever, or until killed.
1163 The pertinent information for each file is stored in an entry of F.
1164 Loop over each of them, doing an fstat to see if they have changed size,
1165 and an occasional open/fstat to see if any dev/ino pair has changed.
1166 If none of them have changed size in one iteration, sleep for a
1167 while and try again. Continue until the user interrupts us. */
1169 static void
1170 tail_forever (struct File_spec *f, size_t n_files, double sleep_interval)
1172 /* Use blocking I/O as an optimization, when it's easy. */
1173 bool blocking = (!nbpids && follow_mode == Follow_descriptor
1174 && n_files == 1 && f[0].fd != -1 && ! S_ISREG (f[0].mode));
1175 size_t last;
1176 bool writers_dead = false;
1178 last = n_files - 1;
1180 while (true)
1182 size_t i;
1183 bool any_input = false;
1185 for (i = 0; i < n_files; i++)
1187 int fd;
1188 char const *name;
1189 mode_t mode;
1190 struct stat stats;
1191 uintmax_t bytes_read;
1193 if (f[i].ignore)
1194 continue;
1196 if (f[i].fd < 0)
1198 recheck (&f[i], blocking);
1199 continue;
1202 fd = f[i].fd;
1203 name = pretty_name (&f[i]);
1204 mode = f[i].mode;
1206 if (f[i].blocking != blocking)
1208 int old_flags = fcntl (fd, F_GETFL);
1209 int new_flags = old_flags | (blocking ? 0 : O_NONBLOCK);
1210 if (old_flags < 0
1211 || (new_flags != old_flags
1212 && fcntl (fd, F_SETFL, new_flags) == -1))
1214 /* Don't update f[i].blocking if fcntl fails. */
1215 if (S_ISREG (f[i].mode) && errno == EPERM)
1217 /* This happens when using tail -f on a file with
1218 the append-only attribute. */
1220 else
1221 error (EXIT_FAILURE, errno,
1222 _("%s: cannot change nonblocking mode"),
1223 quotef (name));
1225 else
1226 f[i].blocking = blocking;
1229 bool read_unchanged = false;
1230 if (!f[i].blocking)
1232 if (fstat (fd, &stats) != 0)
1234 f[i].fd = -1;
1235 f[i].errnum = errno;
1236 error (0, errno, "%s", quotef (name));
1237 close (fd); /* ignore failure */
1238 continue;
1241 if (f[i].mode == stats.st_mode
1242 && (! S_ISREG (stats.st_mode) || f[i].size == stats.st_size)
1243 && timespec_cmp (f[i].mtime, get_stat_mtime (&stats)) == 0)
1245 if ((max_n_unchanged_stats_between_opens
1246 <= f[i].n_unchanged_stats++)
1247 && follow_mode == Follow_name)
1249 recheck (&f[i], f[i].blocking);
1250 f[i].n_unchanged_stats = 0;
1252 if (fd != f[i].fd || S_ISREG (stats.st_mode) || 1 < n_files)
1253 continue;
1254 else
1255 read_unchanged = true;
1258 affirm (fd == f[i].fd);
1260 /* This file has changed. Print out what we can, and
1261 then keep looping. */
1263 f[i].mtime = get_stat_mtime (&stats);
1264 f[i].mode = stats.st_mode;
1266 /* reset counter */
1267 if (! read_unchanged)
1268 f[i].n_unchanged_stats = 0;
1270 /* XXX: This is only a heuristic, as the file may have also
1271 been truncated and written to if st_size >= size
1272 (in which case we ignore new data <= size). */
1273 if (S_ISREG (mode) && stats.st_size < f[i].size)
1275 error (0, 0, _("%s: file truncated"), quotef (name));
1276 /* Assume the file was truncated to 0,
1277 and therefore output all "new" data. */
1278 xlseek (fd, 0, SEEK_SET, name);
1279 f[i].size = 0;
1282 if (i != last)
1284 if (print_headers)
1285 write_header (name);
1286 last = i;
1290 /* Don't read more than st_size on networked file systems
1291 because it was seen on glusterfs at least, that st_size
1292 may be smaller than the data read on a _subsequent_ stat call. */
1293 uintmax_t bytes_to_read;
1294 if (f[i].blocking)
1295 bytes_to_read = COPY_A_BUFFER;
1296 else if (S_ISREG (mode) && f[i].remote)
1297 bytes_to_read = stats.st_size - f[i].size;
1298 else
1299 bytes_to_read = COPY_TO_EOF;
1301 bytes_read = dump_remainder (false, name, fd, bytes_to_read);
1303 if (read_unchanged && bytes_read)
1304 f[i].n_unchanged_stats = 0;
1306 any_input |= (bytes_read != 0);
1307 f[i].size += bytes_read;
1310 if (! any_live_files (f, n_files))
1312 error (0, 0, _("no files remaining"));
1313 break;
1316 if ((!any_input || blocking) && fflush (stdout) != 0)
1317 write_error ();
1319 check_output_alive ();
1321 /* If nothing was read, sleep and/or check for dead writers. */
1322 if (!any_input)
1324 if (writers_dead)
1325 break;
1327 /* Once the writer is dead, read the files once more to
1328 avoid a race condition. */
1329 writers_dead = writers_are_dead ();
1331 if (!writers_dead && xnanosleep (sleep_interval))
1332 error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
1338 #if HAVE_INOTIFY
1340 /* Return true if any of the N_FILES files in F is remote, i.e., has
1341 an open file descriptor and is on a network file system. */
1343 static bool
1344 any_remote_file (const struct File_spec *f, size_t n_files)
1346 for (size_t i = 0; i < n_files; i++)
1347 if (0 <= f[i].fd && f[i].remote)
1348 return true;
1349 return false;
1352 /* Return true if any of the N_FILES files in F is non remote, i.e., has
1353 an open file descriptor and is not on a network file system. */
1355 static bool
1356 any_non_remote_file (const struct File_spec *f, size_t n_files)
1358 for (size_t i = 0; i < n_files; i++)
1359 if (0 <= f[i].fd && ! f[i].remote)
1360 return true;
1361 return false;
1364 /* Return true if any of the N_FILES files in F is a symlink.
1365 Note we don't worry about the edge case where "-" exists,
1366 since that will have the same consequences for inotify,
1367 which is the only context this function is currently used. */
1369 static bool
1370 any_symlinks (const struct File_spec *f, size_t n_files)
1372 struct stat st;
1373 for (size_t i = 0; i < n_files; i++)
1374 if (lstat (f[i].name, &st) == 0 && S_ISLNK (st.st_mode))
1375 return true;
1376 return false;
1379 /* Return true if any of the N_FILES files in F is not
1380 a regular file or fifo. This is used to avoid adding inotify
1381 watches on a device file for example, which inotify
1382 will accept, but not give any events for. */
1384 static bool
1385 any_non_regular_fifo (const struct File_spec *f, size_t n_files)
1387 for (size_t i = 0; i < n_files; i++)
1388 if (0 <= f[i].fd && ! S_ISREG (f[i].mode) && ! S_ISFIFO (f[i].mode))
1389 return true;
1390 return false;
1393 /* Return true if any of the N_FILES files in F represents
1394 stdin and is tailable. */
1396 static bool
1397 tailable_stdin (const struct File_spec *f, size_t n_files)
1399 for (size_t i = 0; i < n_files; i++)
1400 if (!f[i].ignore && STREQ (f[i].name, "-"))
1401 return true;
1402 return false;
1405 static size_t
1406 wd_hasher (const void *entry, size_t tabsize)
1408 const struct File_spec *spec = entry;
1409 return spec->wd % tabsize;
1412 static bool
1413 wd_comparator (const void *e1, const void *e2)
1415 const struct File_spec *spec1 = e1;
1416 const struct File_spec *spec2 = e2;
1417 return spec1->wd == spec2->wd;
1420 /* Output (new) data for FSPEC->fd.
1421 PREV_FSPEC records the last File_spec for which we output. */
1422 static void
1423 check_fspec (struct File_spec *fspec, struct File_spec **prev_fspec)
1425 struct stat stats;
1426 char const *name;
1428 if (fspec->fd == -1)
1429 return;
1431 name = pretty_name (fspec);
1433 if (fstat (fspec->fd, &stats) != 0)
1435 fspec->errnum = errno;
1436 close_fd (fspec->fd, name);
1437 fspec->fd = -1;
1438 return;
1441 /* XXX: This is only a heuristic, as the file may have also
1442 been truncated and written to if st_size >= size
1443 (in which case we ignore new data <= size).
1444 Though in the inotify case it's more likely we'll get
1445 separate events for truncate() and write(). */
1446 if (S_ISREG (fspec->mode) && stats.st_size < fspec->size)
1448 error (0, 0, _("%s: file truncated"), quotef (name));
1449 xlseek (fspec->fd, 0, SEEK_SET, name);
1450 fspec->size = 0;
1452 else if (S_ISREG (fspec->mode) && stats.st_size == fspec->size
1453 && timespec_cmp (fspec->mtime, get_stat_mtime (&stats)) == 0)
1454 return;
1456 bool want_header = print_headers && (fspec != *prev_fspec);
1458 uintmax_t bytes_read = dump_remainder (want_header, name, fspec->fd,
1459 COPY_TO_EOF);
1460 fspec->size += bytes_read;
1462 if (bytes_read)
1464 *prev_fspec = fspec;
1465 if (fflush (stdout) != 0)
1466 write_error ();
1470 /* Attempt to tail N_FILES files forever, or until killed.
1471 Check modifications using the inotify events system.
1472 Exit if finished or on fatal error; return to revert to polling. */
1473 static void
1474 tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
1475 double sleep_interval, Hash_table **wd_to_namep)
1477 # if TAIL_TEST_SLEEP
1478 /* Delay between open() and inotify_add_watch()
1479 to help trigger different cases. */
1480 xnanosleep (1000000);
1481 # endif
1482 unsigned int max_realloc = 3;
1484 /* Map an inotify watch descriptor to the name of the file it's watching. */
1485 Hash_table *wd_to_name;
1487 bool found_watchable_file = false;
1488 bool tailed_but_unwatchable = false;
1489 bool found_unwatchable_dir = false;
1490 bool no_inotify_resources = false;
1491 bool writers_dead = false;
1492 struct File_spec *prev_fspec;
1493 size_t evlen = 0;
1494 char *evbuf;
1495 size_t evbuf_off = 0;
1496 size_t len = 0;
1498 wd_to_name = hash_initialize (n_files, nullptr, wd_hasher, wd_comparator,
1499 nullptr);
1500 if (! wd_to_name)
1501 xalloc_die ();
1502 *wd_to_namep = wd_to_name;
1504 /* The events mask used with inotify on files (not directories). */
1505 uint32_t inotify_wd_mask = IN_MODIFY;
1506 /* TODO: Perhaps monitor these events in Follow_descriptor mode also,
1507 to tag reported file names with "deleted", "moved" etc. */
1508 if (follow_mode == Follow_name)
1509 inotify_wd_mask |= (IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF);
1511 /* Add an inotify watch for each watched file. If -F is specified then watch
1512 its parent directory too, in this way when they re-appear we can add them
1513 again to the watch list. */
1514 size_t i;
1515 for (i = 0; i < n_files; i++)
1517 if (!f[i].ignore)
1519 size_t fnlen = strlen (f[i].name);
1520 if (evlen < fnlen)
1521 evlen = fnlen;
1523 f[i].wd = -1;
1525 if (follow_mode == Follow_name)
1527 size_t dirlen = dir_len (f[i].name);
1528 char prev = f[i].name[dirlen];
1529 f[i].basename_start = last_component (f[i].name) - f[i].name;
1531 f[i].name[dirlen] = '\0';
1533 /* It's fine to add the same directory more than once.
1534 In that case the same watch descriptor is returned. */
1535 f[i].parent_wd = inotify_add_watch (wd, dirlen ? f[i].name : ".",
1536 (IN_CREATE | IN_DELETE
1537 | IN_MOVED_TO | IN_ATTRIB
1538 | IN_DELETE_SELF));
1540 f[i].name[dirlen] = prev;
1542 if (f[i].parent_wd < 0)
1544 if (errno != ENOSPC) /* suppress confusing error. */
1545 error (0, errno, _("cannot watch parent directory of %s"),
1546 quoteaf (f[i].name));
1547 else
1548 error (0, 0, _("inotify resources exhausted"));
1549 found_unwatchable_dir = true;
1550 /* We revert to polling below. Note invalid uses
1551 of the inotify API will still be diagnosed. */
1552 break;
1556 f[i].wd = inotify_add_watch (wd, f[i].name, inotify_wd_mask);
1558 if (f[i].wd < 0)
1560 if (f[i].fd != -1) /* already tailed. */
1561 tailed_but_unwatchable = true;
1562 if (errno == ENOSPC || errno == ENOMEM)
1564 no_inotify_resources = true;
1565 error (0, 0, _("inotify resources exhausted"));
1566 break;
1568 else if (errno != f[i].errnum)
1569 error (0, errno, _("cannot watch %s"), quoteaf (f[i].name));
1570 continue;
1573 if (hash_insert (wd_to_name, &(f[i])) == nullptr)
1574 xalloc_die ();
1576 found_watchable_file = true;
1580 /* Linux kernel 2.6.24 at least has a bug where eventually, ENOSPC is always
1581 returned by inotify_add_watch. In any case we should revert to polling
1582 when there are no inotify resources. Also a specified directory may not
1583 be currently present or accessible, so revert to polling. Also an already
1584 tailed but unwatchable due rename/unlink race, should also revert. */
1585 if (no_inotify_resources || found_unwatchable_dir
1586 || (follow_mode == Follow_descriptor && tailed_but_unwatchable))
1587 return;
1588 if (follow_mode == Follow_descriptor && !found_watchable_file)
1589 exit (EXIT_FAILURE);
1591 prev_fspec = &(f[n_files - 1]);
1593 /* Check files again. New files or data can be available since last time we
1594 checked and before they are watched by inotify. */
1595 for (i = 0; i < n_files; i++)
1597 if (! f[i].ignore)
1599 /* check for new files. */
1600 if (follow_mode == Follow_name)
1601 recheck (&(f[i]), false);
1602 else if (f[i].fd != -1)
1604 /* If the file was replaced in the small window since we tailed,
1605 then assume the watch is on the wrong item (different to
1606 that we've already produced output for), and so revert to
1607 polling the original descriptor. */
1608 struct stat stats;
1610 if (stat (f[i].name, &stats) == 0
1611 && (f[i].dev != stats.st_dev || f[i].ino != stats.st_ino))
1613 error (0, errno, _("%s was replaced"),
1614 quoteaf (pretty_name (&(f[i]))));
1615 return;
1619 /* check for new data. */
1620 check_fspec (&f[i], &prev_fspec);
1624 evlen += sizeof (struct inotify_event) + 1;
1625 evbuf = xmalloc (evlen);
1627 /* Wait for inotify events and handle them. Events on directories
1628 ensure that watched files can be re-added when following by name.
1629 This loop blocks on the 'safe_read' call until a new event is notified.
1630 But when --pid=P is specified, tail usually waits via poll. */
1631 while (true)
1633 struct File_spec *fspec;
1634 struct inotify_event *ev;
1635 void *void_ev;
1637 /* When following by name without --retry, and the last file has
1638 been unlinked or renamed-away, diagnose it and return. */
1639 if (follow_mode == Follow_name
1640 && ! reopen_inaccessible_files
1641 && hash_get_n_entries (wd_to_name) == 0)
1642 error (EXIT_FAILURE, 0, _("no files remaining"));
1644 if (len <= evbuf_off)
1646 /* Poll for inotify events. When watching a PID, ensure
1647 that a read from WD will not block indefinitely.
1648 If MONITOR_OUTPUT, also poll for a broken output pipe. */
1650 int file_change;
1651 struct pollfd pfd[2];
1654 /* How many ms to wait for changes. -1 means wait forever. */
1655 int delay = -1;
1657 if (nbpids)
1659 if (writers_dead)
1660 exit (EXIT_SUCCESS);
1662 writers_dead = writers_are_dead ();
1664 if (writers_dead || sleep_interval <= 0)
1665 delay = 0;
1666 else if (sleep_interval < INT_MAX / 1000 - 1)
1668 /* delay = ceil (sleep_interval * 1000), sans libm. */
1669 double ddelay = sleep_interval * 1000;
1670 delay = ddelay;
1671 delay += delay < ddelay;
1675 pfd[0].fd = wd;
1676 pfd[0].events = POLLIN;
1677 pfd[1].fd = STDOUT_FILENO;
1678 pfd[1].events = pfd[1].revents = 0;
1679 file_change = poll (pfd, monitor_output + 1, delay);
1681 while (file_change == 0);
1683 if (file_change < 0)
1684 error (EXIT_FAILURE, errno,
1685 _("error waiting for inotify and output events"));
1686 if (pfd[1].revents)
1687 die_pipe ();
1689 len = safe_read (wd, evbuf, evlen);
1690 evbuf_off = 0;
1692 /* For kernels prior to 2.6.21, read returns 0 when the buffer
1693 is too small. */
1694 if ((len == 0 || (len == SAFE_READ_ERROR && errno == EINVAL))
1695 && max_realloc--)
1697 len = 0;
1698 evlen *= 2;
1699 evbuf = xrealloc (evbuf, evlen);
1700 continue;
1703 if (len == 0 || len == SAFE_READ_ERROR)
1704 error (EXIT_FAILURE, errno, _("error reading inotify event"));
1707 void_ev = evbuf + evbuf_off;
1708 ev = void_ev;
1709 evbuf_off += sizeof (*ev) + ev->len;
1711 /* If a directory is deleted, IN_DELETE_SELF is emitted
1712 with ev->name of length 0.
1713 We need to catch it, otherwise it would wait forever,
1714 as wd for directory becomes inactive. Revert to polling now. */
1715 if ((ev->mask & IN_DELETE_SELF) && ! ev->len)
1717 for (i = 0; i < n_files; i++)
1719 if (ev->wd == f[i].parent_wd)
1721 error (0, 0,
1722 _("directory containing watched file was removed"));
1723 return;
1728 if (ev->len) /* event on ev->name in watched directory. */
1730 size_t j;
1731 for (j = 0; j < n_files; j++)
1733 /* With N=hundreds of frequently-changing files, this O(N^2)
1734 process might be a problem. FIXME: use a hash table? */
1735 if (f[j].parent_wd == ev->wd
1736 && STREQ (ev->name, f[j].name + f[j].basename_start))
1737 break;
1740 /* It is not a watched file. */
1741 if (j == n_files)
1742 continue;
1744 fspec = &(f[j]);
1746 int new_wd = -1;
1747 bool deleting = !! (ev->mask & IN_DELETE);
1749 if (! deleting)
1751 /* Adding the same inode again will look up any existing wd. */
1752 new_wd = inotify_add_watch (wd, f[j].name, inotify_wd_mask);
1755 if (! deleting && new_wd < 0)
1757 if (errno == ENOSPC || errno == ENOMEM)
1759 error (0, 0, _("inotify resources exhausted"));
1760 return; /* revert to polling. */
1762 else
1764 /* Can get ENOENT for a dangling symlink for example. */
1765 error (0, errno, _("cannot watch %s"), quoteaf (f[j].name));
1767 /* We'll continue below after removing the existing watch. */
1770 /* This will be false if only attributes of file change. */
1771 bool new_watch;
1772 new_watch = (! deleting) && (fspec->wd < 0 || new_wd != fspec->wd);
1774 if (new_watch)
1776 if (0 <= fspec->wd)
1778 inotify_rm_watch (wd, fspec->wd);
1779 hash_remove (wd_to_name, fspec);
1782 fspec->wd = new_wd;
1784 if (new_wd == -1)
1785 continue;
1787 /* If the file was moved then inotify will use the source file wd
1788 for the destination file. Make sure the key is not present in
1789 the table. */
1790 struct File_spec *prev = hash_remove (wd_to_name, fspec);
1791 if (prev && prev != fspec)
1793 if (follow_mode == Follow_name)
1794 recheck (prev, false);
1795 prev->wd = -1;
1796 close_fd (prev->fd, pretty_name (prev));
1799 if (hash_insert (wd_to_name, fspec) == nullptr)
1800 xalloc_die ();
1803 if (follow_mode == Follow_name)
1804 recheck (fspec, false);
1806 else
1808 struct File_spec key;
1809 key.wd = ev->wd;
1810 fspec = hash_lookup (wd_to_name, &key);
1813 if (! fspec)
1814 continue;
1816 if (ev->mask & (IN_ATTRIB | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF))
1818 /* Note for IN_MOVE_SELF (the file we're watching has
1819 been clobbered via a rename) we leave the watch
1820 in place since it may still be part of the set
1821 of watched names. */
1822 if (ev->mask & IN_DELETE_SELF)
1824 inotify_rm_watch (wd, fspec->wd);
1825 hash_remove (wd_to_name, fspec);
1828 /* Note we get IN_ATTRIB for unlink() as st_nlink decrements.
1829 The usual path is a close() done in recheck() triggers
1830 an IN_DELETE_SELF event as the inode is removed.
1831 However sometimes open() will succeed as even though
1832 st_nlink is decremented, the dentry (cache) is not updated.
1833 Thus we depend on the IN_DELETE event on the directory
1834 to trigger processing for the removed file. */
1836 recheck (fspec, false);
1838 continue;
1840 check_fspec (fspec, &prev_fspec);
1843 #endif
1845 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
1846 Return true if successful. */
1848 static bool
1849 tail_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
1850 uintmax_t *read_pos)
1852 struct stat stats;
1854 if (fstat (fd, &stats))
1856 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1857 return false;
1860 if (from_start)
1862 if (! presume_input_pipe && n_bytes <= OFF_T_MAX
1863 && ((S_ISREG (stats.st_mode)
1864 && xlseek (fd, n_bytes, SEEK_CUR, pretty_filename) >= 0)
1865 || lseek (fd, n_bytes, SEEK_CUR) != -1))
1866 *read_pos += n_bytes;
1867 else
1869 int t = start_bytes (pretty_filename, fd, n_bytes, read_pos);
1870 if (t)
1871 return t < 0;
1873 n_bytes = COPY_TO_EOF;
1875 else
1877 off_t end_pos = -1;
1878 off_t current_pos = -1;
1879 bool copy_from_current_pos = false;
1881 if (! presume_input_pipe && n_bytes <= OFF_T_MAX)
1883 if (usable_st_size (&stats))
1885 /* Use st_size only if it's so large that this is
1886 probably not a /proc or similar file, where st_size
1887 is notional. */
1888 end_pos = stats.st_size;
1889 off_t smallish_size = STP_BLKSIZE (&stats);
1890 copy_from_current_pos = smallish_size < end_pos;
1892 else
1894 current_pos = lseek (fd, -n_bytes, SEEK_END);
1895 copy_from_current_pos = current_pos != -1;
1896 if (copy_from_current_pos)
1897 end_pos = current_pos + n_bytes;
1900 if (! copy_from_current_pos)
1901 return pipe_bytes (pretty_filename, fd, n_bytes, read_pos);
1902 if (current_pos == -1)
1903 current_pos = xlseek (fd, 0, SEEK_CUR, pretty_filename);
1904 if (current_pos < end_pos)
1906 off_t bytes_remaining = end_pos - current_pos;
1908 if (n_bytes < bytes_remaining)
1910 current_pos = end_pos - n_bytes;
1911 xlseek (fd, current_pos, SEEK_SET, pretty_filename);
1914 *read_pos = current_pos;
1917 *read_pos += dump_remainder (false, pretty_filename, fd, n_bytes);
1918 return true;
1921 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
1922 Return true if successful. */
1924 static bool
1925 tail_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
1926 uintmax_t *read_pos)
1928 struct stat stats;
1930 if (fstat (fd, &stats))
1932 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1933 return false;
1936 if (from_start)
1938 int t = start_lines (pretty_filename, fd, n_lines, read_pos);
1939 if (t)
1940 return t < 0;
1941 *read_pos += dump_remainder (false, pretty_filename, fd, COPY_TO_EOF);
1943 else
1945 off_t start_pos = -1;
1946 off_t end_pos;
1948 /* Use file_lines only if FD refers to a regular file for
1949 which lseek (... SEEK_END) works. */
1950 if ( ! presume_input_pipe
1951 && S_ISREG (stats.st_mode)
1952 && (start_pos = lseek (fd, 0, SEEK_CUR)) != -1
1953 && start_pos < (end_pos = lseek (fd, 0, SEEK_END)))
1955 *read_pos = end_pos;
1956 if (end_pos != 0
1957 && ! file_lines (pretty_filename, fd, &stats, n_lines,
1958 start_pos, end_pos, read_pos))
1959 return false;
1961 else
1963 /* Under very unlikely circumstances, it is possible to reach
1964 this point after positioning the file pointer to end of file
1965 via the 'lseek (...SEEK_END)' above. In that case, reposition
1966 the file pointer back to start_pos before calling pipe_lines. */
1967 if (start_pos != -1)
1968 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
1970 return pipe_lines (pretty_filename, fd, n_lines, read_pos);
1973 return true;
1976 /* Display the last N_UNITS units of file FILENAME, open for reading
1977 via FD. Set *READ_POS to the position of the input stream pointer.
1978 *READ_POS is usually the number of bytes read and corresponds to an
1979 offset from the beginning of a file. However, it may be larger than
1980 OFF_T_MAX (as for an input pipe), and may also be larger than the
1981 number of bytes read (when an input pointer is initially not at
1982 beginning of file), and may be far greater than the number of bytes
1983 actually read for an input file that is seekable.
1984 Return true if successful. */
1986 static bool
1987 tail (char const *filename, int fd, uintmax_t n_units,
1988 uintmax_t *read_pos)
1990 *read_pos = 0;
1991 if (count_lines)
1992 return tail_lines (filename, fd, n_units, read_pos);
1993 else
1994 return tail_bytes (filename, fd, n_units, read_pos);
1997 /* Display the last N_UNITS units of the file described by F.
1998 Return true if successful. */
2000 static bool
2001 tail_file (struct File_spec *f, uintmax_t n_units)
2003 int fd;
2004 bool ok;
2006 bool is_stdin = (STREQ (f->name, "-"));
2008 if (is_stdin)
2010 have_read_stdin = true;
2011 fd = STDIN_FILENO;
2012 xset_binary_mode (STDIN_FILENO, O_BINARY);
2014 else
2015 fd = open (f->name, O_RDONLY | O_BINARY);
2017 f->tailable = !(reopen_inaccessible_files && fd == -1);
2019 if (fd == -1)
2021 if (forever)
2023 f->fd = -1;
2024 f->errnum = errno;
2025 f->ignore = ! reopen_inaccessible_files;
2026 f->ino = 0;
2027 f->dev = 0;
2029 error (0, errno, _("cannot open %s for reading"),
2030 quoteaf (pretty_name (f)));
2031 ok = false;
2033 else
2035 uintmax_t read_pos;
2037 if (print_headers)
2038 write_header (pretty_name (f));
2039 ok = tail (pretty_name (f), fd, n_units, &read_pos);
2040 if (forever)
2042 struct stat stats;
2044 #if TAIL_TEST_SLEEP
2045 /* Before the tail function provided 'read_pos', there was
2046 a race condition described in the URL below. This sleep
2047 call made the window big enough to exercise the problem. */
2048 xnanosleep (1);
2049 #endif
2050 f->errnum = ok - 1;
2051 if (fstat (fd, &stats) < 0)
2053 ok = false;
2054 f->errnum = errno;
2055 error (0, errno, _("error reading %s"),
2056 quoteaf (pretty_name (f)));
2058 else if (!IS_TAILABLE_FILE_TYPE (stats.st_mode))
2060 ok = false;
2061 f->errnum = -1;
2062 f->tailable = false;
2063 f->ignore = ! reopen_inaccessible_files;
2064 error (0, 0, _("%s: cannot follow end of this type of file%s"),
2065 quotef (pretty_name (f)),
2066 f->ignore ? _("; giving up on this name") : "");
2069 if (!ok)
2071 f->ignore = ! reopen_inaccessible_files;
2072 close_fd (fd, pretty_name (f));
2073 f->fd = -1;
2075 else
2077 /* Note: we must use read_pos here, not stats.st_size,
2078 to avoid a race condition described by Ken Raeburn:
2079 https://lists.gnu.org/r/bug-textutils/2003-05/msg00007.html */
2080 record_open_fd (f, fd, read_pos, &stats, (is_stdin ? -1 : 1));
2081 f->remote = fremote (fd, pretty_name (f));
2084 else
2086 if (!is_stdin && close (fd))
2088 error (0, errno, _("error reading %s"),
2089 quoteaf (pretty_name (f)));
2090 ok = false;
2095 return ok;
2098 /* If obsolete usage is allowed, and the command line arguments are of
2099 the obsolete form and the option string is well-formed, set
2100 *N_UNITS, the globals COUNT_LINES, FOREVER, and FROM_START, and
2101 return true. If the command line arguments are obviously incorrect
2102 (e.g., because obsolete usage is not allowed and the arguments are
2103 incorrect for non-obsolete usage), report an error and exit.
2104 Otherwise, return false and don't modify any parameter or global
2105 variable. */
2107 static bool
2108 parse_obsolete_option (int argc, char * const *argv, uintmax_t *n_units)
2110 char const *p;
2111 char const *n_string;
2112 char const *n_string_end;
2113 int default_count = DEFAULT_N_LINES;
2114 bool t_from_start;
2115 bool t_count_lines = true;
2116 bool t_forever = false;
2118 /* With the obsolete form, there is one option string and at most
2119 one file argument. Watch out for "-" and "--", though. */
2120 if (! (argc == 2
2121 || (argc == 3 && ! (argv[2][0] == '-' && argv[2][1]))
2122 || (3 <= argc && argc <= 4 && STREQ (argv[2], "--"))))
2123 return false;
2125 int posix_ver = posix2_version ();
2126 bool obsolete_usage = posix_ver < 200112;
2127 bool traditional_usage = obsolete_usage || 200809 <= posix_ver;
2128 p = argv[1];
2130 switch (*p++)
2132 default:
2133 return false;
2135 case '+':
2136 /* Leading "+" is a file name in the standard form. */
2137 if (!traditional_usage)
2138 return false;
2140 t_from_start = true;
2141 break;
2143 case '-':
2144 /* In the non-obsolete form, "-" is standard input and "-c"
2145 requires an option-argument. The obsolete multidigit options
2146 are supported as a GNU extension even when conforming to
2147 POSIX 1003.1-2001 or later, so don't complain about them. */
2148 if (!obsolete_usage && !p[p[0] == 'c'])
2149 return false;
2151 t_from_start = false;
2152 break;
2155 n_string = p;
2156 while (ISDIGIT (*p))
2157 p++;
2158 n_string_end = p;
2160 switch (*p)
2162 case 'b': default_count *= 512; FALLTHROUGH;
2163 case 'c': t_count_lines = false; FALLTHROUGH;
2164 case 'l': p++; break;
2167 if (*p == 'f')
2169 t_forever = true;
2170 ++p;
2173 if (*p)
2174 return false;
2176 if (n_string == n_string_end)
2177 *n_units = default_count;
2178 else if ((xstrtoumax (n_string, nullptr, 10, n_units, "b")
2179 & ~LONGINT_INVALID_SUFFIX_CHAR)
2180 != LONGINT_OK)
2181 error (EXIT_FAILURE, errno, "%s: %s", _("invalid number"),
2182 quote (argv[1]));
2184 /* Set globals. */
2185 from_start = t_from_start;
2186 count_lines = t_count_lines;
2187 forever = t_forever;
2189 return true;
2192 static void
2193 parse_options (int argc, char **argv,
2194 uintmax_t *n_units, enum header_mode *header_mode,
2195 double *sleep_interval)
2197 int c;
2199 while ((c = getopt_long (argc, argv, "c:n:fFqs:vz0123456789",
2200 long_options, nullptr))
2201 != -1)
2203 switch (c)
2205 case 'F':
2206 forever = true;
2207 follow_mode = Follow_name;
2208 reopen_inaccessible_files = true;
2209 break;
2211 case 'c':
2212 case 'n':
2213 count_lines = (c == 'n');
2214 if (*optarg == '+')
2215 from_start = true;
2216 else if (*optarg == '-')
2217 ++optarg;
2219 *n_units = xdectoumax (optarg, 0, UINTMAX_MAX, "bkKmMGTPEZYRQ0",
2220 count_lines
2221 ? _("invalid number of lines")
2222 : _("invalid number of bytes"), 0);
2223 break;
2225 case 'f':
2226 case LONG_FOLLOW_OPTION:
2227 forever = true;
2228 if (optarg == nullptr)
2229 follow_mode = DEFAULT_FOLLOW_MODE;
2230 else
2231 follow_mode = XARGMATCH ("--follow", optarg,
2232 follow_mode_string, follow_mode_map);
2233 break;
2235 case RETRY_OPTION:
2236 reopen_inaccessible_files = true;
2237 break;
2239 case MAX_UNCHANGED_STATS_OPTION:
2240 /* --max-unchanged-stats=N */
2241 max_n_unchanged_stats_between_opens =
2242 xdectoumax (optarg, 0, UINTMAX_MAX, "",
2243 _("invalid maximum number of unchanged stats between opens"), 0);
2244 break;
2246 case DISABLE_INOTIFY_OPTION:
2247 disable_inotify = true;
2248 break;
2250 case PID_OPTION:
2251 if (nbpids == pids_alloc)
2252 pids = xpalloc (pids, &pids_alloc, 1,
2253 MIN (INT_MAX, PTRDIFF_MAX), sizeof *pids);
2254 pids[nbpids++] = xdectoumax (optarg, 0, PID_T_MAX, "",
2255 _("invalid PID"), 0);
2256 break;
2258 case PRESUME_INPUT_PIPE_OPTION:
2259 presume_input_pipe = true;
2260 break;
2262 case 'q':
2263 *header_mode = never;
2264 break;
2266 case 's':
2268 double s;
2269 if (! (xstrtod (optarg, nullptr, &s, cl_strtod) && 0 <= s))
2270 error (EXIT_FAILURE, 0,
2271 _("invalid number of seconds: %s"), quote (optarg));
2272 *sleep_interval = s;
2274 break;
2276 case 'v':
2277 *header_mode = always;
2278 break;
2280 case 'z':
2281 line_end = '\0';
2282 break;
2284 case_GETOPT_HELP_CHAR;
2286 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
2288 case '0': case '1': case '2': case '3': case '4':
2289 case '5': case '6': case '7': case '8': case '9':
2290 error (EXIT_FAILURE, 0, _("option used in invalid context -- %c"), c);
2292 default:
2293 usage (EXIT_FAILURE);
2297 if (reopen_inaccessible_files)
2299 if (!forever)
2301 reopen_inaccessible_files = false;
2302 error (0, 0, _("warning: --retry ignored; --retry is useful"
2303 " only when following"));
2305 else if (follow_mode == Follow_descriptor)
2306 error (0, 0, _("warning: --retry only effective for the initial open"));
2309 if (nbpids && !forever)
2310 error (0, 0,
2311 _("warning: PID ignored; --pid=PID is useful only when following"));
2312 else if (nbpids && kill (pids[0], 0) != 0 && errno == ENOSYS)
2314 error (0, 0, _("warning: --pid=PID is not supported on this system"));
2315 nbpids = 0;
2316 free (pids);
2320 /* Mark as '.ignore'd each member of F that corresponds to a
2321 pipe or fifo, and return the number of non-ignored members. */
2322 static size_t
2323 ignore_fifo_and_pipe (struct File_spec *f, size_t n_files)
2325 /* When there is no FILE operand and stdin is a pipe or FIFO
2326 POSIX requires that tail ignore the -f option.
2327 Since we allow multiple FILE operands, we extend that to say: with -f,
2328 ignore any "-" operand that corresponds to a pipe or FIFO. */
2329 size_t n_viable = 0;
2331 for (size_t i = 0; i < n_files; i++)
2333 bool is_a_fifo_or_pipe =
2334 (STREQ (f[i].name, "-")
2335 && !f[i].ignore
2336 && 0 <= f[i].fd
2337 && (S_ISFIFO (f[i].mode)
2338 || (HAVE_FIFO_PIPES != 1 && isapipe (f[i].fd))));
2339 if (is_a_fifo_or_pipe)
2341 f[i].fd = -1;
2342 f[i].ignore = true;
2344 else
2345 ++n_viable;
2348 return n_viable;
2352 main (int argc, char **argv)
2354 enum header_mode header_mode = multiple_files;
2355 bool ok = true;
2356 /* If from_start, the number of items to skip before printing; otherwise,
2357 the number of items at the end of the file to print. Although the type
2358 is signed, the value is never negative. */
2359 uintmax_t n_units = DEFAULT_N_LINES;
2360 size_t n_files;
2361 char **file;
2362 struct File_spec *F;
2363 size_t i;
2364 bool obsolete_option;
2366 /* The number of seconds to sleep between iterations.
2367 During one iteration, every file name or descriptor is checked to
2368 see if it has changed. */
2369 double sleep_interval = 1.0;
2371 initialize_main (&argc, &argv);
2372 set_program_name (argv[0]);
2373 setlocale (LC_ALL, "");
2374 bindtextdomain (PACKAGE, LOCALEDIR);
2375 textdomain (PACKAGE);
2377 atexit (close_stdout);
2379 page_size = getpagesize ();
2381 have_read_stdin = false;
2383 count_lines = true;
2384 forever = from_start = print_headers = false;
2385 line_end = '\n';
2386 obsolete_option = parse_obsolete_option (argc, argv, &n_units);
2387 argc -= obsolete_option;
2388 argv += obsolete_option;
2389 parse_options (argc, argv, &n_units, &header_mode, &sleep_interval);
2391 /* To start printing with item N_UNITS from the start of the file, skip
2392 N_UNITS - 1 items. 'tail -n +0' is actually meaningless, but for Unix
2393 compatibility it's treated the same as 'tail -n +1'. */
2394 if (from_start)
2396 if (n_units)
2397 --n_units;
2400 if (optind < argc)
2402 n_files = argc - optind;
2403 file = argv + optind;
2405 else
2407 static char *dummy_stdin = (char *) "-";
2408 n_files = 1;
2409 file = &dummy_stdin;
2413 bool found_hyphen = false;
2415 for (i = 0; i < n_files; i++)
2416 if (STREQ (file[i], "-"))
2417 found_hyphen = true;
2419 /* When following by name, there must be a name. */
2420 if (found_hyphen && follow_mode == Follow_name)
2421 error (EXIT_FAILURE, 0, _("cannot follow %s by name"), quoteaf ("-"));
2423 /* When following forever, and not using simple blocking, warn if
2424 any file is '-' as the stats() used to check for input are ineffective.
2425 This is only a warning, since tail's output (before a failing seek,
2426 and that from any non-stdin files) might still be useful. */
2427 if (forever && found_hyphen)
2429 struct stat in_stat;
2430 bool blocking_stdin;
2431 blocking_stdin = (!nbpids && follow_mode == Follow_descriptor
2432 && n_files == 1 && ! fstat (STDIN_FILENO, &in_stat)
2433 && ! S_ISREG (in_stat.st_mode));
2435 if (! blocking_stdin && isatty (STDIN_FILENO))
2436 error (0, 0, _("warning: following standard input"
2437 " indefinitely is ineffective"));
2441 /* Don't read anything if we'll never output anything. */
2442 if (! n_units && ! forever && ! from_start)
2443 return EXIT_SUCCESS;
2445 F = xnmalloc (n_files, sizeof *F);
2446 for (i = 0; i < n_files; i++)
2447 F[i].name = file[i];
2449 if (header_mode == always
2450 || (header_mode == multiple_files && n_files > 1))
2451 print_headers = true;
2453 xset_binary_mode (STDOUT_FILENO, O_BINARY);
2455 for (i = 0; i < n_files; i++)
2456 ok &= tail_file (&F[i], n_units);
2458 if (forever && ignore_fifo_and_pipe (F, n_files))
2460 /* If stdout is a fifo or pipe, then monitor it
2461 so that we exit if the reader goes away. */
2462 struct stat out_stat;
2463 if (fstat (STDOUT_FILENO, &out_stat) < 0)
2464 error (EXIT_FAILURE, errno, _("standard output"));
2465 monitor_output = (S_ISFIFO (out_stat.st_mode)
2466 || (HAVE_FIFO_PIPES != 1 && isapipe (STDOUT_FILENO)));
2468 #if HAVE_INOTIFY
2469 /* tailable_stdin() checks if the user specifies stdin via "-",
2470 or implicitly by providing no arguments. If so, we won't use inotify.
2471 Technically, on systems with a working /dev/stdin, we *could*,
2472 but would it be worth it? Verifying that it's a real device
2473 and hooked up to stdin is not trivial, while reverting to
2474 non-inotify-based tail_forever is easy and portable.
2476 any_remote_file() checks if the user has specified any
2477 files that reside on remote file systems. inotify is not used
2478 in this case because it would miss any updates to the file
2479 that were not initiated from the local system.
2481 any_non_remote_file() checks if the user has specified any
2482 files that don't reside on remote file systems. inotify is not used
2483 if there are no open files, as we can't determine if those file
2484 will be on a remote file system.
2486 any_symlinks() checks if the user has specified any symbolic links.
2487 inotify is not used in this case because it returns updated _targets_
2488 which would not match the specified names. If we tried to always
2489 use the target names, then we would miss changes to the symlink itself.
2491 ok is false when one of the files specified could not be opened for
2492 reading. In this case and when following by descriptor,
2493 tail_forever_inotify() cannot be used (in its current implementation).
2495 FIXME: inotify doesn't give any notification when a new
2496 (remote) file or directory is mounted on top a watched file.
2497 When follow_mode == Follow_name we would ideally like to detect that.
2498 Note if there is a change to the original file then we'll
2499 recheck it and follow the new file, or ignore it if the
2500 file has changed to being remote.
2502 FIXME-maybe: inotify has a watch descriptor per inode, and hence with
2503 our current hash implementation will only --follow data for one
2504 of the names when multiple hardlinked files are specified, or
2505 for one name when a name is specified multiple times. */
2506 if (!disable_inotify && (tailable_stdin (F, n_files)
2507 || any_remote_file (F, n_files)
2508 || ! any_non_remote_file (F, n_files)
2509 || any_symlinks (F, n_files)
2510 || any_non_regular_fifo (F, n_files)
2511 || (!ok && follow_mode == Follow_descriptor)))
2512 disable_inotify = true;
2514 if (!disable_inotify)
2516 int wd = inotify_init ();
2517 if (0 <= wd)
2519 /* Flush any output from tail_file, now, since
2520 tail_forever_inotify flushes only after writing,
2521 not before reading. */
2522 if (fflush (stdout) != 0)
2523 write_error ();
2525 Hash_table *ht;
2526 tail_forever_inotify (wd, F, n_files, sleep_interval, &ht);
2527 hash_free (ht);
2528 close (wd);
2529 errno = 0;
2531 error (0, errno, _("inotify cannot be used, reverting to polling"));
2533 #endif
2534 disable_inotify = true;
2535 tail_forever (F, n_files, sleep_interval);
2538 if (have_read_stdin && close (STDIN_FILENO) < 0)
2539 error (EXIT_FAILURE, errno, "-");
2540 main_exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);