build: ensure sys/select.h is included
[coreutils.git] / src / tail.c
blobfdbec07ad7395790bdff78cca10f826c4e0237a0
1 /* tail -- output the last part of file(s)
2 Copyright (C) 1989-2019 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>
34 #ifdef _AIX
35 # include <poll.h>
36 #endif
38 #include "system.h"
39 #include "argmatch.h"
40 #include "cl-strtod.h"
41 #include "die.h"
42 #include "error.h"
43 #include "fcntl--.h"
44 #include "isapipe.h"
45 #include "posixver.h"
46 #include "quote.h"
47 #include "safe-read.h"
48 #include "stat-size.h"
49 #include "stat-time.h"
50 #include "xbinary-io.h"
51 #include "xdectoint.h"
52 #include "xnanosleep.h"
53 #include "xstrtol.h"
54 #include "xstrtod.h"
56 #if HAVE_INOTIFY
57 # include "hash.h"
58 # include <sys/inotify.h>
59 #endif
61 /* Linux can optimize the handling of local files. */
62 #if defined __linux__ || defined __ANDROID__
63 # include "fs.h"
64 # include "fs-is-local.h"
65 # if HAVE_SYS_STATFS_H
66 # include <sys/statfs.h>
67 # elif HAVE_SYS_VFS_H
68 # include <sys/vfs.h>
69 # endif
70 #endif
72 /* The official name of this program (e.g., no 'g' prefix). */
73 #define PROGRAM_NAME "tail"
75 #define AUTHORS \
76 proper_name ("Paul Rubin"), \
77 proper_name ("David MacKenzie"), \
78 proper_name ("Ian Lance Taylor"), \
79 proper_name ("Jim Meyering")
81 /* Number of items to tail. */
82 #define DEFAULT_N_LINES 10
84 /* Special values for dump_remainder's N_BYTES parameter. */
85 #define COPY_TO_EOF UINTMAX_MAX
86 #define COPY_A_BUFFER (UINTMAX_MAX - 1)
88 /* FIXME: make Follow_name the default? */
89 #define DEFAULT_FOLLOW_MODE Follow_descriptor
91 enum Follow_mode
93 /* Follow the name of each file: if the file is renamed, try to reopen
94 that name and track the end of the new file if/when it's recreated.
95 This is useful for tracking logs that are occasionally rotated. */
96 Follow_name = 1,
98 /* Follow each descriptor obtained upon opening a file.
99 That means we'll continue to follow the end of a file even after
100 it has been renamed or unlinked. */
101 Follow_descriptor = 2
104 /* The types of files for which tail works. */
105 #define IS_TAILABLE_FILE_TYPE(Mode) \
106 (S_ISREG (Mode) || S_ISFIFO (Mode) || S_ISSOCK (Mode) || S_ISCHR (Mode))
108 static char const *const follow_mode_string[] =
110 "descriptor", "name", NULL
113 static enum Follow_mode const follow_mode_map[] =
115 Follow_descriptor, Follow_name,
118 struct File_spec
120 /* The actual file name, or "-" for stdin. */
121 char *name;
123 /* Attributes of the file the last time we checked. */
124 off_t size;
125 struct timespec mtime;
126 dev_t dev;
127 ino_t ino;
128 mode_t mode;
130 /* The specified name initially referred to a directory or some other
131 type for which tail isn't meaningful. Unlike for a permission problem
132 (tailable, below) once this is set, the name is not checked ever again. */
133 bool ignore;
135 /* See the description of fremote. */
136 bool remote;
138 /* A file is tailable if it exists, is readable, and is of type
139 IS_TAILABLE_FILE_TYPE. */
140 bool tailable;
142 /* File descriptor on which the file is open; -1 if it's not open. */
143 int fd;
145 /* The value of errno seen last time we checked this file. */
146 int errnum;
148 /* 1 if O_NONBLOCK is clear, 0 if set, -1 if not known. */
149 int blocking;
151 #if HAVE_INOTIFY
152 /* The watch descriptor used by inotify. */
153 int wd;
155 /* The parent directory watch descriptor. It is used only
156 * when Follow_name is used. */
157 int parent_wd;
159 /* Offset in NAME of the basename part. */
160 size_t basename_start;
161 #endif
163 /* See description of DEFAULT_MAX_N_... below. */
164 uintmax_t n_unchanged_stats;
167 /* Keep trying to open a file even if it is inaccessible when tail starts
168 or if it becomes inaccessible later -- useful only with -f. */
169 static bool reopen_inaccessible_files;
171 /* If true, interpret the numeric argument as the number of lines.
172 Otherwise, interpret it as the number of bytes. */
173 static bool count_lines;
175 /* Whether we follow the name of each file or the file descriptor
176 that is initially associated with each name. */
177 static enum Follow_mode follow_mode = Follow_descriptor;
179 /* If true, read from the ends of all specified files until killed. */
180 static bool forever;
182 /* If true, monitor output so we exit if pipe reader terminates. */
183 static bool monitor_output;
185 /* If true, count from start of file instead of end. */
186 static bool from_start;
188 /* If true, print filename headers. */
189 static bool print_headers;
191 /* Character to split lines by. */
192 static char line_end;
194 /* When to print the filename banners. */
195 enum header_mode
197 multiple_files, always, never
200 /* When tailing a file by name, if there have been this many consecutive
201 iterations for which the file has not changed, then open/fstat
202 the file to determine if that file name is still associated with the
203 same device/inode-number pair as before. This option is meaningful only
204 when following by name. --max-unchanged-stats=N */
205 #define DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS 5
206 static uintmax_t max_n_unchanged_stats_between_opens =
207 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS;
209 /* The process ID of the process (presumably on the current host)
210 that is writing to all followed files. */
211 static pid_t pid;
213 /* True if we have ever read standard input. */
214 static bool have_read_stdin;
216 /* If nonzero, skip the is-regular-file test used to determine whether
217 to use the lseek optimization. Instead, use the more general (and
218 more expensive) code unconditionally. Intended solely for testing. */
219 static bool presume_input_pipe;
221 /* If nonzero then don't use inotify even if available. */
222 static bool disable_inotify;
224 /* For long options that have no equivalent short option, use a
225 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
226 enum
228 RETRY_OPTION = CHAR_MAX + 1,
229 MAX_UNCHANGED_STATS_OPTION,
230 PID_OPTION,
231 PRESUME_INPUT_PIPE_OPTION,
232 LONG_FOLLOW_OPTION,
233 DISABLE_INOTIFY_OPTION
236 static struct option const long_options[] =
238 {"bytes", required_argument, NULL, 'c'},
239 {"follow", optional_argument, NULL, LONG_FOLLOW_OPTION},
240 {"lines", required_argument, NULL, 'n'},
241 {"max-unchanged-stats", required_argument, NULL, MAX_UNCHANGED_STATS_OPTION},
242 {"-disable-inotify", no_argument, NULL,
243 DISABLE_INOTIFY_OPTION}, /* do not document */
244 {"pid", required_argument, NULL, PID_OPTION},
245 {"-presume-input-pipe", no_argument, NULL,
246 PRESUME_INPUT_PIPE_OPTION}, /* do not document */
247 {"quiet", no_argument, NULL, 'q'},
248 {"retry", no_argument, NULL, RETRY_OPTION},
249 {"silent", no_argument, NULL, 'q'},
250 {"sleep-interval", required_argument, NULL, 's'},
251 {"verbose", no_argument, NULL, 'v'},
252 {"zero-terminated", no_argument, NULL, 'z'},
253 {GETOPT_HELP_OPTION_DECL},
254 {GETOPT_VERSION_OPTION_DECL},
255 {NULL, 0, NULL, 0}
258 void
259 usage (int status)
261 if (status != EXIT_SUCCESS)
262 emit_try_help ();
263 else
265 printf (_("\
266 Usage: %s [OPTION]... [FILE]...\n\
268 program_name);
269 printf (_("\
270 Print the last %d lines of each FILE to standard output.\n\
271 With more than one FILE, precede each with a header giving the file name.\n\
272 "), DEFAULT_N_LINES);
274 emit_stdin_note ();
275 emit_mandatory_arg_note ();
277 fputs (_("\
278 -c, --bytes=[+]NUM output the last NUM bytes; or use -c +NUM to\n\
279 output starting with byte NUM of each file\n\
280 "), stdout);
281 fputs (_("\
282 -f, --follow[={name|descriptor}]\n\
283 output appended data as the file grows;\n\
284 an absent option argument means 'descriptor'\n\
285 -F same as --follow=name --retry\n\
286 "), stdout);
287 printf (_("\
288 -n, --lines=[+]NUM output the last NUM lines, instead of the last %d;\n\
289 or use -n +NUM to output starting with line NUM\n\
290 --max-unchanged-stats=N\n\
291 with --follow=name, reopen a FILE which has not\n\
292 changed size after N (default %d) iterations\n\
293 to see if it has been unlinked or renamed\n\
294 (this is the usual case of rotated log files);\n\
295 with inotify, this option is rarely useful\n\
297 DEFAULT_N_LINES,
298 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS
300 fputs (_("\
301 --pid=PID with -f, terminate after process ID, PID dies\n\
302 -q, --quiet, --silent never output headers giving file names\n\
303 --retry keep trying to open a file if it is inaccessible\n\
304 "), stdout);
305 fputs (_("\
306 -s, --sleep-interval=N with -f, sleep for approximately N seconds\n\
307 (default 1.0) between iterations;\n\
308 with inotify and --pid=P, check process P at\n\
309 least once every N seconds\n\
310 -v, --verbose always output headers giving file names\n\
311 "), stdout);
312 fputs (_("\
313 -z, --zero-terminated line delimiter is NUL, not newline\n\
314 "), stdout);
315 fputs (HELP_OPTION_DESCRIPTION, stdout);
316 fputs (VERSION_OPTION_DESCRIPTION, stdout);
317 fputs (_("\
319 NUM may have a multiplier suffix:\n\
320 b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\
321 GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.\n\
322 Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\
324 "), stdout);
325 fputs (_("\
326 With --follow (-f), tail defaults to following the file descriptor, which\n\
327 means that even if a tail'ed file is renamed, tail will continue to track\n\
328 its end. This default behavior is not desirable when you really want to\n\
329 track the actual name of the file, not the file descriptor (e.g., log\n\
330 rotation). Use --follow=name in that case. That causes tail to track the\n\
331 named file in a way that accommodates renaming, removal and creation.\n\
332 "), stdout);
333 emit_ancillary_info (PROGRAM_NAME);
335 exit (status);
338 /* Ensure exit, either with SIGPIPE or EXIT_FAILURE status. */
339 static void ATTRIBUTE_NORETURN
340 die_pipe (void)
342 raise (SIGPIPE);
343 exit (EXIT_FAILURE);
346 /* If the output has gone away, then terminate
347 as we would if we had written to this output. */
348 static void
349 check_output_alive (void)
351 if (! monitor_output)
352 return;
354 #ifdef _AIX
355 /* select on AIX was seen to give a readable event immediately. */
356 struct pollfd pfd;
357 pfd.fd = STDOUT_FILENO;
358 pfd.events = POLLERR;
360 if (poll (&pfd, 1, 0) >= 0 && (pfd.revents & POLLERR))
361 die_pipe ();
362 #else
363 struct timeval delay;
364 delay.tv_sec = delay.tv_usec = 0;
366 fd_set rfd;
367 FD_ZERO (&rfd);
368 FD_SET (STDOUT_FILENO, &rfd);
370 /* readable event on STDOUT is equivalent to POLLERR,
371 and implies an error condition on output like broken pipe. */
372 if (select (STDOUT_FILENO + 1, &rfd, NULL, NULL, &delay) == 1)
373 die_pipe ();
374 #endif
377 static bool
378 valid_file_spec (struct File_spec const *f)
380 /* Exactly one of the following subexpressions must be true. */
381 return ((f->fd == -1) ^ (f->errnum == 0));
384 static char const *
385 pretty_name (struct File_spec const *f)
387 return (STREQ (f->name, "-") ? _("standard input") : f->name);
390 /* Record a file F with descriptor FD, size SIZE, status ST, and
391 blocking status BLOCKING. */
393 static void
394 record_open_fd (struct File_spec *f, int fd,
395 off_t size, struct stat const *st,
396 int blocking)
398 f->fd = fd;
399 f->size = size;
400 f->mtime = get_stat_mtime (st);
401 f->dev = st->st_dev;
402 f->ino = st->st_ino;
403 f->mode = st->st_mode;
404 f->blocking = blocking;
405 f->n_unchanged_stats = 0;
406 f->ignore = false;
409 /* Close the file with descriptor FD and name FILENAME. */
411 static void
412 close_fd (int fd, const char *filename)
414 if (fd != -1 && fd != STDIN_FILENO && close (fd))
416 error (0, errno, _("closing %s (fd=%d)"), quoteaf (filename), fd);
420 static void
421 write_header (const char *pretty_filename)
423 static bool first_file = true;
425 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), pretty_filename);
426 first_file = false;
429 /* Write N_BYTES from BUFFER to stdout.
430 Exit immediately on error with a single diagnostic. */
432 static void
433 xwrite_stdout (char const *buffer, size_t n_bytes)
435 if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) < n_bytes)
437 clearerr (stdout); /* To avoid redundant close_stdout diagnostic. */
438 die (EXIT_FAILURE, errno, _("error writing %s"),
439 quoteaf ("standard output"));
443 /* Read and output N_BYTES of file PRETTY_FILENAME starting at the current
444 position in FD. If N_BYTES is COPY_TO_EOF, then copy until end of file.
445 If N_BYTES is COPY_A_BUFFER, then copy at most one buffer's worth.
446 Return the number of bytes read from the file. */
448 static uintmax_t
449 dump_remainder (bool want_header, const char *pretty_filename, int fd,
450 uintmax_t n_bytes)
452 uintmax_t n_written;
453 uintmax_t n_remaining = n_bytes;
455 n_written = 0;
456 while (1)
458 char buffer[BUFSIZ];
459 size_t n = MIN (n_remaining, BUFSIZ);
460 size_t bytes_read = safe_read (fd, buffer, n);
461 if (bytes_read == SAFE_READ_ERROR)
463 if (errno != EAGAIN)
464 die (EXIT_FAILURE, errno, _("error reading %s"),
465 quoteaf (pretty_filename));
466 break;
468 if (bytes_read == 0)
469 break;
470 if (want_header)
472 write_header (pretty_filename);
473 want_header = false;
475 xwrite_stdout (buffer, bytes_read);
476 n_written += bytes_read;
477 if (n_bytes != COPY_TO_EOF)
479 n_remaining -= bytes_read;
480 if (n_remaining == 0 || n_bytes == COPY_A_BUFFER)
481 break;
485 return n_written;
488 /* Call lseek with the specified arguments, where file descriptor FD
489 corresponds to the file, FILENAME.
490 Give a diagnostic and exit nonzero if lseek fails.
491 Otherwise, return the resulting offset. */
493 static off_t
494 xlseek (int fd, off_t offset, int whence, char const *filename)
496 off_t new_offset = lseek (fd, offset, whence);
497 char buf[INT_BUFSIZE_BOUND (offset)];
498 char *s;
500 if (0 <= new_offset)
501 return new_offset;
503 s = offtostr (offset, buf);
504 switch (whence)
506 case SEEK_SET:
507 error (0, errno, _("%s: cannot seek to offset %s"),
508 quotef (filename), s);
509 break;
510 case SEEK_CUR:
511 error (0, errno, _("%s: cannot seek to relative offset %s"),
512 quotef (filename), s);
513 break;
514 case SEEK_END:
515 error (0, errno, _("%s: cannot seek to end-relative offset %s"),
516 quotef (filename), s);
517 break;
518 default:
519 abort ();
522 exit (EXIT_FAILURE);
525 /* Print the last N_LINES lines from the end of file FD.
526 Go backward through the file, reading 'BUFSIZ' bytes at a time (except
527 probably the first), until we hit the start of the file or have
528 read NUMBER newlines.
529 START_POS is the starting position of the read pointer for the file
530 associated with FD (may be nonzero).
531 END_POS is the file offset of EOF (one larger than offset of last byte).
532 Return true if successful. */
534 static bool
535 file_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
536 off_t start_pos, off_t end_pos, uintmax_t *read_pos)
538 char buffer[BUFSIZ];
539 size_t bytes_read;
540 off_t pos = end_pos;
542 if (n_lines == 0)
543 return true;
545 /* Set 'bytes_read' to the size of the last, probably partial, buffer;
546 0 < 'bytes_read' <= 'BUFSIZ'. */
547 bytes_read = (pos - start_pos) % BUFSIZ;
548 if (bytes_read == 0)
549 bytes_read = BUFSIZ;
550 /* Make 'pos' a multiple of 'BUFSIZ' (0 if the file is short), so that all
551 reads will be on block boundaries, which might increase efficiency. */
552 pos -= bytes_read;
553 xlseek (fd, pos, SEEK_SET, pretty_filename);
554 bytes_read = safe_read (fd, buffer, bytes_read);
555 if (bytes_read == SAFE_READ_ERROR)
557 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
558 return false;
560 *read_pos = pos + bytes_read;
562 /* Count the incomplete line on files that don't end with a newline. */
563 if (bytes_read && buffer[bytes_read - 1] != line_end)
564 --n_lines;
568 /* Scan backward, counting the newlines in this bufferfull. */
570 size_t n = bytes_read;
571 while (n)
573 char const *nl;
574 nl = memrchr (buffer, line_end, n);
575 if (nl == NULL)
576 break;
577 n = nl - buffer;
578 if (n_lines-- == 0)
580 /* If this newline isn't the last character in the buffer,
581 output the part that is after it. */
582 if (n != bytes_read - 1)
583 xwrite_stdout (nl + 1, bytes_read - (n + 1));
584 *read_pos += dump_remainder (false, pretty_filename, fd,
585 end_pos - (pos + bytes_read));
586 return true;
590 /* Not enough newlines in that bufferfull. */
591 if (pos == start_pos)
593 /* Not enough lines in the file; print everything from
594 start_pos to the end. */
595 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
596 *read_pos = start_pos + dump_remainder (false, pretty_filename, fd,
597 end_pos);
598 return true;
600 pos -= BUFSIZ;
601 xlseek (fd, pos, SEEK_SET, pretty_filename);
603 bytes_read = safe_read (fd, buffer, BUFSIZ);
604 if (bytes_read == SAFE_READ_ERROR)
606 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
607 return false;
610 *read_pos = pos + bytes_read;
612 while (bytes_read > 0);
614 return true;
617 /* Print the last N_LINES lines from the end of the standard input,
618 open for reading as pipe FD.
619 Buffer the text as a linked list of LBUFFERs, adding them as needed.
620 Return true if successful. */
622 static bool
623 pipe_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
624 uintmax_t *read_pos)
626 struct linebuffer
628 char buffer[BUFSIZ];
629 size_t nbytes;
630 size_t nlines;
631 struct linebuffer *next;
633 typedef struct linebuffer LBUFFER;
634 LBUFFER *first, *last, *tmp;
635 size_t total_lines = 0; /* Total number of newlines in all buffers. */
636 bool ok = true;
637 size_t n_read; /* Size in bytes of most recent read */
639 first = last = xmalloc (sizeof (LBUFFER));
640 first->nbytes = first->nlines = 0;
641 first->next = NULL;
642 tmp = xmalloc (sizeof (LBUFFER));
644 /* Input is always read into a fresh buffer. */
645 while (1)
647 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
648 if (n_read == 0 || n_read == SAFE_READ_ERROR)
649 break;
650 tmp->nbytes = n_read;
651 *read_pos += n_read;
652 tmp->nlines = 0;
653 tmp->next = NULL;
655 /* Count the number of newlines just read. */
657 char const *buffer_end = tmp->buffer + n_read;
658 char const *p = tmp->buffer;
659 while ((p = memchr (p, line_end, buffer_end - p)))
661 ++p;
662 ++tmp->nlines;
665 total_lines += tmp->nlines;
667 /* If there is enough room in the last buffer read, just append the new
668 one to it. This is because when reading from a pipe, 'n_read' can
669 often be very small. */
670 if (tmp->nbytes + last->nbytes < BUFSIZ)
672 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
673 last->nbytes += tmp->nbytes;
674 last->nlines += tmp->nlines;
676 else
678 /* If there's not enough room, link the new buffer onto the end of
679 the list, then either free up the oldest buffer for the next
680 read if that would leave enough lines, or else malloc a new one.
681 Some compaction mechanism is possible but probably not
682 worthwhile. */
683 last = last->next = tmp;
684 if (total_lines - first->nlines > n_lines)
686 tmp = first;
687 total_lines -= first->nlines;
688 first = first->next;
690 else
691 tmp = xmalloc (sizeof (LBUFFER));
695 free (tmp);
697 if (n_read == SAFE_READ_ERROR)
699 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
700 ok = false;
701 goto free_lbuffers;
704 /* If the file is empty, then bail out. */
705 if (last->nbytes == 0)
706 goto free_lbuffers;
708 /* This prevents a core dump when the pipe contains no newlines. */
709 if (n_lines == 0)
710 goto free_lbuffers;
712 /* Count the incomplete line on files that don't end with a newline. */
713 if (last->buffer[last->nbytes - 1] != line_end)
715 ++last->nlines;
716 ++total_lines;
719 /* Run through the list, printing lines. First, skip over unneeded
720 buffers. */
721 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
722 total_lines -= tmp->nlines;
724 /* Find the correct beginning, then print the rest of the file. */
726 char const *beg = tmp->buffer;
727 char const *buffer_end = tmp->buffer + tmp->nbytes;
728 if (total_lines > n_lines)
730 /* Skip 'total_lines' - 'n_lines' newlines. We made sure that
731 'total_lines' - 'n_lines' <= 'tmp->nlines'. */
732 size_t j;
733 for (j = total_lines - n_lines; j; --j)
735 beg = memchr (beg, line_end, buffer_end - beg);
736 assert (beg);
737 ++beg;
741 xwrite_stdout (beg, buffer_end - beg);
744 for (tmp = tmp->next; tmp; tmp = tmp->next)
745 xwrite_stdout (tmp->buffer, tmp->nbytes);
747 free_lbuffers:
748 while (first)
750 tmp = first->next;
751 free (first);
752 first = tmp;
754 return ok;
757 /* Print the last N_BYTES characters from the end of pipe FD.
758 This is a stripped down version of pipe_lines.
759 Return true if successful. */
761 static bool
762 pipe_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
763 uintmax_t *read_pos)
765 struct charbuffer
767 char buffer[BUFSIZ];
768 size_t nbytes;
769 struct charbuffer *next;
771 typedef struct charbuffer CBUFFER;
772 CBUFFER *first, *last, *tmp;
773 size_t i; /* Index into buffers. */
774 size_t total_bytes = 0; /* Total characters in all buffers. */
775 bool ok = true;
776 size_t n_read;
778 first = last = xmalloc (sizeof (CBUFFER));
779 first->nbytes = 0;
780 first->next = NULL;
781 tmp = xmalloc (sizeof (CBUFFER));
783 /* Input is always read into a fresh buffer. */
784 while (1)
786 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
787 if (n_read == 0 || n_read == SAFE_READ_ERROR)
788 break;
789 *read_pos += n_read;
790 tmp->nbytes = n_read;
791 tmp->next = NULL;
793 total_bytes += tmp->nbytes;
794 /* If there is enough room in the last buffer read, just append the new
795 one to it. This is because when reading from a pipe, 'nbytes' can
796 often be very small. */
797 if (tmp->nbytes + last->nbytes < BUFSIZ)
799 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
800 last->nbytes += tmp->nbytes;
802 else
804 /* If there's not enough room, link the new buffer onto the end of
805 the list, then either free up the oldest buffer for the next
806 read if that would leave enough characters, or else malloc a new
807 one. Some compaction mechanism is possible but probably not
808 worthwhile. */
809 last = last->next = tmp;
810 if (total_bytes - first->nbytes > n_bytes)
812 tmp = first;
813 total_bytes -= first->nbytes;
814 first = first->next;
816 else
818 tmp = xmalloc (sizeof (CBUFFER));
823 free (tmp);
825 if (n_read == SAFE_READ_ERROR)
827 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
828 ok = false;
829 goto free_cbuffers;
832 /* Run through the list, printing characters. First, skip over unneeded
833 buffers. */
834 for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
835 total_bytes -= tmp->nbytes;
837 /* Find the correct beginning, then print the rest of the file.
838 We made sure that 'total_bytes' - 'n_bytes' <= 'tmp->nbytes'. */
839 if (total_bytes > n_bytes)
840 i = total_bytes - n_bytes;
841 else
842 i = 0;
843 xwrite_stdout (&tmp->buffer[i], tmp->nbytes - i);
845 for (tmp = tmp->next; tmp; tmp = tmp->next)
846 xwrite_stdout (tmp->buffer, tmp->nbytes);
848 free_cbuffers:
849 while (first)
851 tmp = first->next;
852 free (first);
853 first = tmp;
855 return ok;
858 /* Skip N_BYTES characters from the start of pipe FD, and print
859 any extra characters that were read beyond that.
860 Return 1 on error, 0 if ok, -1 if EOF. */
862 static int
863 start_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
864 uintmax_t *read_pos)
866 char buffer[BUFSIZ];
868 while (0 < n_bytes)
870 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
871 if (bytes_read == 0)
872 return -1;
873 if (bytes_read == SAFE_READ_ERROR)
875 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
876 return 1;
878 *read_pos += bytes_read;
879 if (bytes_read <= n_bytes)
880 n_bytes -= bytes_read;
881 else
883 size_t n_remaining = bytes_read - n_bytes;
884 if (n_remaining)
885 xwrite_stdout (&buffer[n_bytes], n_remaining);
886 break;
890 return 0;
893 /* Skip N_LINES lines at the start of file or pipe FD, and print
894 any extra characters that were read beyond that.
895 Return 1 on error, 0 if ok, -1 if EOF. */
897 static int
898 start_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
899 uintmax_t *read_pos)
901 if (n_lines == 0)
902 return 0;
904 while (1)
906 char buffer[BUFSIZ];
907 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
908 if (bytes_read == 0) /* EOF */
909 return -1;
910 if (bytes_read == SAFE_READ_ERROR) /* error */
912 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
913 return 1;
916 char *buffer_end = buffer + bytes_read;
918 *read_pos += bytes_read;
920 char *p = buffer;
921 while ((p = memchr (p, line_end, buffer_end - p)))
923 ++p;
924 if (--n_lines == 0)
926 if (p < buffer_end)
927 xwrite_stdout (p, buffer_end - p);
928 return 0;
934 /* Return false when FD is open on a file residing on a local file system.
935 If fstatfs fails, give a diagnostic and return true.
936 If fstatfs cannot be called, return true. */
937 static bool
938 fremote (int fd, const char *name)
940 bool remote = true; /* be conservative (poll by default). */
942 #if HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE \
943 && (defined __linux__ || defined __ANDROID__)
944 struct statfs buf;
945 int err = fstatfs (fd, &buf);
946 if (err != 0)
948 /* On at least linux-2.6.38, fstatfs fails with ENOSYS when FD
949 is open on a pipe. Treat that like a remote file. */
950 if (errno != ENOSYS)
951 error (0, errno, _("cannot determine location of %s. "
952 "reverting to polling"), quoteaf (name));
954 else
956 switch (is_local_fs_type (buf.f_type))
958 case 0:
959 break;
960 case -1:
961 /* Treat unrecognized file systems as "remote", so caller polls.
962 Note README-release has instructions for syncing the internal
963 list with the latest Linux kernel file system constants. */
964 break;
965 case 1:
966 remote = false;
967 break;
968 default:
969 assert (!"unexpected return value from is_local_fs_type");
972 #endif
974 return remote;
977 /* open/fstat F->name and handle changes. */
978 static void
979 recheck (struct File_spec *f, bool blocking)
981 struct stat new_stats;
982 bool ok = true;
983 bool is_stdin = (STREQ (f->name, "-"));
984 bool was_tailable = f->tailable;
985 int prev_errnum = f->errnum;
986 bool new_file;
987 int fd = (is_stdin
988 ? STDIN_FILENO
989 : open (f->name, O_RDONLY | (blocking ? 0 : O_NONBLOCK)));
991 assert (valid_file_spec (f));
993 /* If the open fails because the file doesn't exist,
994 then mark the file as not tailable. */
995 f->tailable = !(reopen_inaccessible_files && fd == -1);
997 if (! disable_inotify && ! lstat (f->name, &new_stats)
998 && S_ISLNK (new_stats.st_mode))
1000 /* Diagnose the edge case where a regular file is changed
1001 to a symlink. We avoid inotify with symlinks since
1002 it's awkward to match between symlink name and target. */
1003 ok = false;
1004 f->errnum = -1;
1005 f->ignore = true;
1007 error (0, 0, _("%s has been replaced with an untailable symbolic link"),
1008 quoteaf (pretty_name (f)));
1010 else if (fd == -1 || fstat (fd, &new_stats) < 0)
1012 ok = false;
1013 f->errnum = errno;
1014 if (!f->tailable)
1016 if (was_tailable)
1018 /* FIXME-maybe: detect the case in which the file first becomes
1019 unreadable (perms), and later becomes readable again and can
1020 be seen to be the same file (dev/ino). Otherwise, tail prints
1021 the entire contents of the file when it becomes readable. */
1022 error (0, f->errnum, _("%s has become inaccessible"),
1023 quoteaf (pretty_name (f)));
1025 else
1027 /* say nothing... it's still not tailable */
1030 else if (prev_errnum != errno)
1031 error (0, errno, "%s", quotef (pretty_name (f)));
1033 else if (!IS_TAILABLE_FILE_TYPE (new_stats.st_mode))
1035 ok = false;
1036 f->errnum = -1;
1037 f->tailable = false;
1038 f->ignore = ! (reopen_inaccessible_files && follow_mode == Follow_name);
1039 if (was_tailable || prev_errnum != f->errnum)
1040 error (0, 0, _("%s has been replaced with an untailable file%s"),
1041 quoteaf (pretty_name (f)),
1042 f->ignore ? _("; giving up on this name") : "");
1044 else if ((f->remote = fremote (fd, pretty_name (f))) && ! disable_inotify)
1046 ok = false;
1047 f->errnum = -1;
1048 error (0, 0, _("%s has been replaced with an untailable remote file"),
1049 quoteaf (pretty_name (f)));
1050 f->ignore = true;
1051 f->remote = true;
1053 else
1055 f->errnum = 0;
1058 new_file = false;
1059 if (!ok)
1061 close_fd (fd, pretty_name (f));
1062 close_fd (f->fd, pretty_name (f));
1063 f->fd = -1;
1065 else if (prev_errnum && prev_errnum != ENOENT)
1067 new_file = true;
1068 assert (f->fd == -1);
1069 error (0, 0, _("%s has become accessible"), quoteaf (pretty_name (f)));
1071 else if (f->fd == -1)
1073 /* A new file even when inodes haven't changed as <dev,inode>
1074 pairs can be reused, and we know the file was missing
1075 on the previous iteration. Note this also means the file
1076 is redisplayed in --follow=name mode if renamed away from
1077 and back to a monitored name. */
1078 new_file = true;
1080 error (0, 0,
1081 _("%s has appeared; following new file"),
1082 quoteaf (pretty_name (f)));
1084 else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
1086 /* File has been replaced (e.g., via log rotation) --
1087 tail the new one. */
1088 new_file = true;
1090 error (0, 0,
1091 _("%s has been replaced; following new file"),
1092 quoteaf (pretty_name (f)));
1094 /* Close the old one. */
1095 close_fd (f->fd, pretty_name (f));
1098 else
1100 /* No changes detected, so close new fd. */
1101 close_fd (fd, pretty_name (f));
1104 /* FIXME: When a log is rotated, daemons tend to log to the
1105 old file descriptor until the new file is present and
1106 the daemon is sent a signal. Therefore tail may miss entries
1107 being written to the old file. Perhaps we should keep
1108 the older file open and continue to monitor it until
1109 data is written to a new file. */
1110 if (new_file)
1112 /* Start at the beginning of the file. */
1113 record_open_fd (f, fd, 0, &new_stats, (is_stdin ? -1 : blocking));
1114 xlseek (fd, 0, SEEK_SET, pretty_name (f));
1118 /* Return true if any of the N_FILES files in F are live, i.e., have
1119 open file descriptors, or should be checked again (see --retry).
1120 When following descriptors, checking should only continue when any
1121 of the files is not yet ignored. */
1123 static bool
1124 any_live_files (const struct File_spec *f, size_t n_files)
1126 /* In inotify mode, ignore may be set for files
1127 which may later be replaced with new files.
1128 So always consider files live in -F mode. */
1129 if (reopen_inaccessible_files && follow_mode == Follow_name)
1130 return true;
1132 for (size_t i = 0; i < n_files; i++)
1134 if (0 <= f[i].fd)
1135 return true;
1136 else
1138 if (! f[i].ignore && reopen_inaccessible_files)
1139 return true;
1143 return false;
1146 /* Tail N_FILES files forever, or until killed.
1147 The pertinent information for each file is stored in an entry of F.
1148 Loop over each of them, doing an fstat to see if they have changed size,
1149 and an occasional open/fstat to see if any dev/ino pair has changed.
1150 If none of them have changed size in one iteration, sleep for a
1151 while and try again. Continue until the user interrupts us. */
1153 static void
1154 tail_forever (struct File_spec *f, size_t n_files, double sleep_interval)
1156 /* Use blocking I/O as an optimization, when it's easy. */
1157 bool blocking = (pid == 0 && follow_mode == Follow_descriptor
1158 && n_files == 1 && f[0].fd != -1 && ! S_ISREG (f[0].mode));
1159 size_t last;
1160 bool writer_is_dead = false;
1162 last = n_files - 1;
1164 while (1)
1166 size_t i;
1167 bool any_input = false;
1169 for (i = 0; i < n_files; i++)
1171 int fd;
1172 char const *name;
1173 mode_t mode;
1174 struct stat stats;
1175 uintmax_t bytes_read;
1177 if (f[i].ignore)
1178 continue;
1180 if (f[i].fd < 0)
1182 recheck (&f[i], blocking);
1183 continue;
1186 fd = f[i].fd;
1187 name = pretty_name (&f[i]);
1188 mode = f[i].mode;
1190 if (f[i].blocking != blocking)
1192 int old_flags = fcntl (fd, F_GETFL);
1193 int new_flags = old_flags | (blocking ? 0 : O_NONBLOCK);
1194 if (old_flags < 0
1195 || (new_flags != old_flags
1196 && fcntl (fd, F_SETFL, new_flags) == -1))
1198 /* Don't update f[i].blocking if fcntl fails. */
1199 if (S_ISREG (f[i].mode) && errno == EPERM)
1201 /* This happens when using tail -f on a file with
1202 the append-only attribute. */
1204 else
1205 die (EXIT_FAILURE, errno,
1206 _("%s: cannot change nonblocking mode"),
1207 quotef (name));
1209 else
1210 f[i].blocking = blocking;
1213 if (!f[i].blocking)
1215 if (fstat (fd, &stats) != 0)
1217 f[i].fd = -1;
1218 f[i].errnum = errno;
1219 error (0, errno, "%s", quotef (name));
1220 close (fd); /* ignore failure */
1221 continue;
1224 if (f[i].mode == stats.st_mode
1225 && (! S_ISREG (stats.st_mode) || f[i].size == stats.st_size)
1226 && timespec_cmp (f[i].mtime, get_stat_mtime (&stats)) == 0)
1228 if ((max_n_unchanged_stats_between_opens
1229 <= f[i].n_unchanged_stats++)
1230 && follow_mode == Follow_name)
1232 recheck (&f[i], f[i].blocking);
1233 f[i].n_unchanged_stats = 0;
1235 continue;
1238 /* This file has changed. Print out what we can, and
1239 then keep looping. */
1241 f[i].mtime = get_stat_mtime (&stats);
1242 f[i].mode = stats.st_mode;
1244 /* reset counter */
1245 f[i].n_unchanged_stats = 0;
1247 /* XXX: This is only a heuristic, as the file may have also
1248 been truncated and written to if st_size >= size
1249 (in which case we ignore new data <= size). */
1250 if (S_ISREG (mode) && stats.st_size < f[i].size)
1252 error (0, 0, _("%s: file truncated"), quotef (name));
1253 /* Assume the file was truncated to 0,
1254 and therefore output all "new" data. */
1255 xlseek (fd, 0, SEEK_SET, name);
1256 f[i].size = 0;
1259 if (i != last)
1261 if (print_headers)
1262 write_header (name);
1263 last = i;
1267 /* Don't read more than st_size on networked file systems
1268 because it was seen on glusterfs at least, that st_size
1269 may be smaller than the data read on a _subsequent_ stat call. */
1270 uintmax_t bytes_to_read;
1271 if (f[i].blocking)
1272 bytes_to_read = COPY_A_BUFFER;
1273 else if (S_ISREG (mode) && f[i].remote)
1274 bytes_to_read = stats.st_size - f[i].size;
1275 else
1276 bytes_to_read = COPY_TO_EOF;
1278 bytes_read = dump_remainder (false, name, fd, bytes_to_read);
1280 any_input |= (bytes_read != 0);
1281 f[i].size += bytes_read;
1284 if (! any_live_files (f, n_files))
1286 error (0, 0, _("no files remaining"));
1287 break;
1290 if ((!any_input || blocking) && fflush (stdout) != 0)
1291 die (EXIT_FAILURE, errno, _("write error"));
1293 check_output_alive ();
1295 /* If nothing was read, sleep and/or check for dead writers. */
1296 if (!any_input)
1298 if (writer_is_dead)
1299 break;
1301 /* Once the writer is dead, read the files once more to
1302 avoid a race condition. */
1303 writer_is_dead = (pid != 0
1304 && kill (pid, 0) != 0
1305 /* Handle the case in which you cannot send a
1306 signal to the writer, so kill fails and sets
1307 errno to EPERM. */
1308 && errno != EPERM);
1310 if (!writer_is_dead && xnanosleep (sleep_interval))
1311 die (EXIT_FAILURE, errno, _("cannot read realtime clock"));
1317 #if HAVE_INOTIFY
1319 /* Return true if any of the N_FILES files in F is remote, i.e., has
1320 an open file descriptor and is on a network file system. */
1322 static bool
1323 any_remote_file (const struct File_spec *f, size_t n_files)
1325 for (size_t i = 0; i < n_files; i++)
1326 if (0 <= f[i].fd && f[i].remote)
1327 return true;
1328 return false;
1331 /* Return true if any of the N_FILES files in F is non remote, i.e., has
1332 an open file descriptor and is not on a network file system. */
1334 static bool
1335 any_non_remote_file (const struct File_spec *f, size_t n_files)
1337 for (size_t i = 0; i < n_files; i++)
1338 if (0 <= f[i].fd && ! f[i].remote)
1339 return true;
1340 return false;
1343 /* Return true if any of the N_FILES files in F is a symlink.
1344 Note we don't worry about the edge case where "-" exists,
1345 since that will have the same consequences for inotify,
1346 which is the only context this function is currently used. */
1348 static bool
1349 any_symlinks (const struct File_spec *f, size_t n_files)
1351 struct stat st;
1352 for (size_t i = 0; i < n_files; i++)
1353 if (lstat (f[i].name, &st) == 0 && S_ISLNK (st.st_mode))
1354 return true;
1355 return false;
1358 /* Return true if any of the N_FILES files in F is not
1359 a regular file or fifo. This is used to avoid adding inotify
1360 watches on a device file for example, which inotify
1361 will accept, but not give any events for. */
1363 static bool
1364 any_non_regular_fifo (const struct File_spec *f, size_t n_files)
1366 for (size_t i = 0; i < n_files; i++)
1367 if (0 <= f[i].fd && ! S_ISREG (f[i].mode) && ! S_ISFIFO (f[i].mode))
1368 return true;
1369 return false;
1372 /* Return true if any of the N_FILES files in F represents
1373 stdin and is tailable. */
1375 static bool
1376 tailable_stdin (const struct File_spec *f, size_t n_files)
1378 for (size_t i = 0; i < n_files; i++)
1379 if (!f[i].ignore && STREQ (f[i].name, "-"))
1380 return true;
1381 return false;
1384 static size_t
1385 wd_hasher (const void *entry, size_t tabsize)
1387 const struct File_spec *spec = entry;
1388 return spec->wd % tabsize;
1391 static bool
1392 wd_comparator (const void *e1, const void *e2)
1394 const struct File_spec *spec1 = e1;
1395 const struct File_spec *spec2 = e2;
1396 return spec1->wd == spec2->wd;
1399 /* Output (new) data for FSPEC->fd.
1400 PREV_FSPEC records the last File_spec for which we output. */
1401 static void
1402 check_fspec (struct File_spec *fspec, struct File_spec **prev_fspec)
1404 struct stat stats;
1405 char const *name;
1407 if (fspec->fd == -1)
1408 return;
1410 name = pretty_name (fspec);
1412 if (fstat (fspec->fd, &stats) != 0)
1414 fspec->errnum = errno;
1415 close_fd (fspec->fd, name);
1416 fspec->fd = -1;
1417 return;
1420 /* XXX: This is only a heuristic, as the file may have also
1421 been truncated and written to if st_size >= size
1422 (in which case we ignore new data <= size).
1423 Though in the inotify case it's more likely we'll get
1424 separate events for truncate() and write(). */
1425 if (S_ISREG (fspec->mode) && stats.st_size < fspec->size)
1427 error (0, 0, _("%s: file truncated"), quotef (name));
1428 xlseek (fspec->fd, 0, SEEK_SET, name);
1429 fspec->size = 0;
1431 else if (S_ISREG (fspec->mode) && stats.st_size == fspec->size
1432 && timespec_cmp (fspec->mtime, get_stat_mtime (&stats)) == 0)
1433 return;
1435 bool want_header = print_headers && (fspec != *prev_fspec);
1437 uintmax_t bytes_read = dump_remainder (want_header, name, fspec->fd,
1438 COPY_TO_EOF);
1439 fspec->size += bytes_read;
1441 if (bytes_read)
1443 *prev_fspec = fspec;
1444 if (fflush (stdout) != 0)
1445 die (EXIT_FAILURE, errno, _("write error"));
1449 /* Attempt to tail N_FILES files forever, or until killed.
1450 Check modifications using the inotify events system.
1451 Return false on error, or true to revert to polling. */
1452 static bool
1453 tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
1454 double sleep_interval)
1456 # if TAIL_TEST_SLEEP
1457 /* Delay between open() and inotify_add_watch()
1458 to help trigger different cases. */
1459 xnanosleep (1000000);
1460 # endif
1461 unsigned int max_realloc = 3;
1463 /* Map an inotify watch descriptor to the name of the file it's watching. */
1464 Hash_table *wd_to_name;
1466 bool found_watchable_file = false;
1467 bool tailed_but_unwatchable = false;
1468 bool found_unwatchable_dir = false;
1469 bool no_inotify_resources = false;
1470 bool writer_is_dead = false;
1471 struct File_spec *prev_fspec;
1472 size_t evlen = 0;
1473 char *evbuf;
1474 size_t evbuf_off = 0;
1475 size_t len = 0;
1477 wd_to_name = hash_initialize (n_files, NULL, wd_hasher, wd_comparator, NULL);
1478 if (! wd_to_name)
1479 xalloc_die ();
1481 /* The events mask used with inotify on files (not directories). */
1482 uint32_t inotify_wd_mask = IN_MODIFY;
1483 /* TODO: Perhaps monitor these events in Follow_descriptor mode also,
1484 to tag reported file names with "deleted", "moved" etc. */
1485 if (follow_mode == Follow_name)
1486 inotify_wd_mask |= (IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF);
1488 /* Add an inotify watch for each watched file. If -F is specified then watch
1489 its parent directory too, in this way when they re-appear we can add them
1490 again to the watch list. */
1491 size_t i;
1492 for (i = 0; i < n_files; i++)
1494 if (!f[i].ignore)
1496 size_t fnlen = strlen (f[i].name);
1497 if (evlen < fnlen)
1498 evlen = fnlen;
1500 f[i].wd = -1;
1502 if (follow_mode == Follow_name)
1504 size_t dirlen = dir_len (f[i].name);
1505 char prev = f[i].name[dirlen];
1506 f[i].basename_start = last_component (f[i].name) - f[i].name;
1508 f[i].name[dirlen] = '\0';
1510 /* It's fine to add the same directory more than once.
1511 In that case the same watch descriptor is returned. */
1512 f[i].parent_wd = inotify_add_watch (wd, dirlen ? f[i].name : ".",
1513 (IN_CREATE | IN_DELETE
1514 | IN_MOVED_TO | IN_ATTRIB
1515 | IN_DELETE_SELF));
1517 f[i].name[dirlen] = prev;
1519 if (f[i].parent_wd < 0)
1521 if (errno != ENOSPC) /* suppress confusing error. */
1522 error (0, errno, _("cannot watch parent directory of %s"),
1523 quoteaf (f[i].name));
1524 else
1525 error (0, 0, _("inotify resources exhausted"));
1526 found_unwatchable_dir = true;
1527 /* We revert to polling below. Note invalid uses
1528 of the inotify API will still be diagnosed. */
1529 break;
1533 f[i].wd = inotify_add_watch (wd, f[i].name, inotify_wd_mask);
1535 if (f[i].wd < 0)
1537 if (f[i].fd != -1) /* already tailed. */
1538 tailed_but_unwatchable = true;
1539 if (errno == ENOSPC || errno == ENOMEM)
1541 no_inotify_resources = true;
1542 error (0, 0, _("inotify resources exhausted"));
1543 break;
1545 else if (errno != f[i].errnum)
1546 error (0, errno, _("cannot watch %s"), quoteaf (f[i].name));
1547 continue;
1550 if (hash_insert (wd_to_name, &(f[i])) == NULL)
1551 xalloc_die ();
1553 found_watchable_file = true;
1557 /* Linux kernel 2.6.24 at least has a bug where eventually, ENOSPC is always
1558 returned by inotify_add_watch. In any case we should revert to polling
1559 when there are no inotify resources. Also a specified directory may not
1560 be currently present or accessible, so revert to polling. Also an already
1561 tailed but unwatchable due rename/unlink race, should also revert. */
1562 if (no_inotify_resources || found_unwatchable_dir
1563 || (follow_mode == Follow_descriptor && tailed_but_unwatchable))
1565 hash_free (wd_to_name);
1567 errno = 0;
1568 return true;
1570 if (follow_mode == Follow_descriptor && !found_watchable_file)
1571 return false;
1573 prev_fspec = &(f[n_files - 1]);
1575 /* Check files again. New files or data can be available since last time we
1576 checked and before they are watched by inotify. */
1577 for (i = 0; i < n_files; i++)
1579 if (! f[i].ignore)
1581 /* check for new files. */
1582 if (follow_mode == Follow_name)
1583 recheck (&(f[i]), false);
1584 else if (f[i].fd != -1)
1586 /* If the file was replaced in the small window since we tailed,
1587 then assume the watch is on the wrong item (different to
1588 that we've already produced output for), and so revert to
1589 polling the original descriptor. */
1590 struct stat stats;
1592 if (stat (f[i].name, &stats) == 0
1593 && (f[i].dev != stats.st_dev || f[i].ino != stats.st_ino))
1595 error (0, errno, _("%s was replaced"),
1596 quoteaf (pretty_name (&(f[i]))));
1597 hash_free (wd_to_name);
1599 errno = 0;
1600 return true;
1604 /* check for new data. */
1605 check_fspec (&f[i], &prev_fspec);
1609 evlen += sizeof (struct inotify_event) + 1;
1610 evbuf = xmalloc (evlen);
1612 /* Wait for inotify events and handle them. Events on directories
1613 ensure that watched files can be re-added when following by name.
1614 This loop blocks on the 'safe_read' call until a new event is notified.
1615 But when --pid=P is specified, tail usually waits via the select. */
1616 while (1)
1618 struct File_spec *fspec;
1619 struct inotify_event *ev;
1620 void *void_ev;
1622 /* When following by name without --retry, and the last file has
1623 been unlinked or renamed-away, diagnose it and return. */
1624 if (follow_mode == Follow_name
1625 && ! reopen_inaccessible_files
1626 && hash_get_n_entries (wd_to_name) == 0)
1628 error (0, 0, _("no files remaining"));
1629 return false;
1632 /* When watching a PID, ensure that a read from WD will not block
1633 indefinitely. */
1634 while (len <= evbuf_off)
1636 struct timeval delay; /* how long to wait for file changes. */
1638 if (pid)
1640 if (writer_is_dead)
1641 exit (EXIT_SUCCESS);
1643 writer_is_dead = (kill (pid, 0) != 0 && errno != EPERM);
1645 if (writer_is_dead)
1646 delay.tv_sec = delay.tv_usec = 0;
1647 else
1649 delay.tv_sec = (time_t) sleep_interval;
1650 delay.tv_usec = 1000000 * (sleep_interval - delay.tv_sec);
1654 fd_set rfd;
1655 FD_ZERO (&rfd);
1656 FD_SET (wd, &rfd);
1657 if (monitor_output)
1658 FD_SET (STDOUT_FILENO, &rfd);
1660 int file_change = select (MAX (wd, STDOUT_FILENO) + 1,
1661 &rfd, NULL, NULL, pid ? &delay: NULL);
1663 if (file_change == 0)
1664 continue;
1665 else if (file_change == -1)
1666 die (EXIT_FAILURE, errno,
1667 _("error waiting for inotify and output events"));
1668 else if (FD_ISSET (STDOUT_FILENO, &rfd))
1670 /* readable event on STDOUT is equivalent to POLLERR,
1671 and implies an error on output like broken pipe. */
1672 die_pipe ();
1674 else
1675 break;
1678 if (len <= evbuf_off)
1680 len = safe_read (wd, evbuf, evlen);
1681 evbuf_off = 0;
1683 /* For kernels prior to 2.6.21, read returns 0 when the buffer
1684 is too small. */
1685 if ((len == 0 || (len == SAFE_READ_ERROR && errno == EINVAL))
1686 && max_realloc--)
1688 len = 0;
1689 evlen *= 2;
1690 evbuf = xrealloc (evbuf, evlen);
1691 continue;
1694 if (len == 0 || len == SAFE_READ_ERROR)
1695 die (EXIT_FAILURE, errno, _("error reading inotify event"));
1698 void_ev = evbuf + evbuf_off;
1699 ev = void_ev;
1700 evbuf_off += sizeof (*ev) + ev->len;
1702 /* If a directory is deleted, IN_DELETE_SELF is emitted
1703 with ev->name of length 0.
1704 We need to catch it, otherwise it would wait forever,
1705 as wd for directory becomes inactive. Revert to polling now. */
1706 if ((ev->mask & IN_DELETE_SELF) && ! ev->len)
1708 for (i = 0; i < n_files; i++)
1710 if (ev->wd == f[i].parent_wd)
1712 hash_free (wd_to_name);
1713 error (0, 0,
1714 _("directory containing watched file was removed"));
1715 errno = 0; /* we've already diagnosed enough errno detail. */
1716 return true;
1721 if (ev->len) /* event on ev->name in watched directory. */
1723 size_t j;
1724 for (j = 0; j < n_files; j++)
1726 /* With N=hundreds of frequently-changing files, this O(N^2)
1727 process might be a problem. FIXME: use a hash table? */
1728 if (f[j].parent_wd == ev->wd
1729 && STREQ (ev->name, f[j].name + f[j].basename_start))
1730 break;
1733 /* It is not a watched file. */
1734 if (j == n_files)
1735 continue;
1737 fspec = &(f[j]);
1739 int new_wd = -1;
1740 bool deleting = !! (ev->mask & IN_DELETE);
1742 if (! deleting)
1744 /* Adding the same inode again will look up any existing wd. */
1745 new_wd = inotify_add_watch (wd, f[j].name, inotify_wd_mask);
1748 if (! deleting && new_wd < 0)
1750 if (errno == ENOSPC || errno == ENOMEM)
1752 error (0, 0, _("inotify resources exhausted"));
1753 hash_free (wd_to_name);
1754 errno = 0;
1755 return true; /* revert to polling. */
1757 else
1759 /* Can get ENOENT for a dangling symlink for example. */
1760 error (0, errno, _("cannot watch %s"), quoteaf (f[j].name));
1762 /* We'll continue below after removing the existing watch. */
1765 /* This will be false if only attributes of file change. */
1766 bool new_watch;
1767 new_watch = (! deleting) && (fspec->wd < 0 || new_wd != fspec->wd);
1769 if (new_watch)
1771 if (0 <= fspec->wd)
1773 inotify_rm_watch (wd, fspec->wd);
1774 hash_delete (wd_to_name, fspec);
1777 fspec->wd = new_wd;
1779 if (new_wd == -1)
1780 continue;
1782 /* If the file was moved then inotify will use the source file wd
1783 for the destination file. Make sure the key is not present in
1784 the table. */
1785 struct File_spec *prev = hash_delete (wd_to_name, fspec);
1786 if (prev && prev != fspec)
1788 if (follow_mode == Follow_name)
1789 recheck (prev, false);
1790 prev->wd = -1;
1791 close_fd (prev->fd, pretty_name (prev));
1794 if (hash_insert (wd_to_name, fspec) == NULL)
1795 xalloc_die ();
1798 if (follow_mode == Follow_name)
1799 recheck (fspec, false);
1801 else
1803 struct File_spec key;
1804 key.wd = ev->wd;
1805 fspec = hash_lookup (wd_to_name, &key);
1808 if (! fspec)
1809 continue;
1811 if (ev->mask & (IN_ATTRIB | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF))
1813 /* Note for IN_MOVE_SELF (the file we're watching has
1814 been clobbered via a rename) we leave the watch
1815 in place since it may still be part of the set
1816 of watched names. */
1817 if (ev->mask & IN_DELETE_SELF)
1819 inotify_rm_watch (wd, fspec->wd);
1820 hash_delete (wd_to_name, fspec);
1823 /* Note we get IN_ATTRIB for unlink() as st_nlink decrements.
1824 The usual path is a close() done in recheck() triggers
1825 an IN_DELETE_SELF event as the inode is removed.
1826 However sometimes open() will succeed as even though
1827 st_nlink is decremented, the dentry (cache) is not updated.
1828 Thus we depend on the IN_DELETE event on the directory
1829 to trigger processing for the removed file. */
1831 recheck (fspec, false);
1833 continue;
1835 check_fspec (fspec, &prev_fspec);
1838 #endif
1840 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
1841 Return true if successful. */
1843 static bool
1844 tail_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
1845 uintmax_t *read_pos)
1847 struct stat stats;
1849 if (fstat (fd, &stats))
1851 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1852 return false;
1855 if (from_start)
1857 if (! presume_input_pipe && n_bytes <= OFF_T_MAX
1858 && ((S_ISREG (stats.st_mode)
1859 && xlseek (fd, n_bytes, SEEK_CUR, pretty_filename) >= 0)
1860 || lseek (fd, n_bytes, SEEK_CUR) != -1))
1861 *read_pos += n_bytes;
1862 else
1864 int t = start_bytes (pretty_filename, fd, n_bytes, read_pos);
1865 if (t)
1866 return t < 0;
1868 n_bytes = COPY_TO_EOF;
1870 else
1872 off_t end_pos = -1;
1873 off_t current_pos = -1;
1875 if (! presume_input_pipe && n_bytes <= OFF_T_MAX)
1877 if (usable_st_size (&stats))
1878 end_pos = stats.st_size;
1879 else if ((current_pos = lseek (fd, -n_bytes, SEEK_END)) != -1)
1880 end_pos = current_pos + n_bytes;
1882 if (end_pos <= (off_t) ST_BLKSIZE (stats))
1883 return pipe_bytes (pretty_filename, fd, n_bytes, read_pos);
1884 if (current_pos == -1)
1885 current_pos = xlseek (fd, 0, SEEK_CUR, pretty_filename);
1886 if (current_pos < end_pos)
1888 off_t bytes_remaining = end_pos - current_pos;
1890 if (n_bytes < bytes_remaining)
1892 current_pos = end_pos - n_bytes;
1893 xlseek (fd, current_pos, SEEK_SET, pretty_filename);
1896 *read_pos = current_pos;
1899 *read_pos += dump_remainder (false, pretty_filename, fd, n_bytes);
1900 return true;
1903 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
1904 Return true if successful. */
1906 static bool
1907 tail_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
1908 uintmax_t *read_pos)
1910 struct stat stats;
1912 if (fstat (fd, &stats))
1914 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1915 return false;
1918 if (from_start)
1920 int t = start_lines (pretty_filename, fd, n_lines, read_pos);
1921 if (t)
1922 return t < 0;
1923 *read_pos += dump_remainder (false, pretty_filename, fd, COPY_TO_EOF);
1925 else
1927 off_t start_pos = -1;
1928 off_t end_pos;
1930 /* Use file_lines only if FD refers to a regular file for
1931 which lseek (... SEEK_END) works. */
1932 if ( ! presume_input_pipe
1933 && S_ISREG (stats.st_mode)
1934 && (start_pos = lseek (fd, 0, SEEK_CUR)) != -1
1935 && start_pos < (end_pos = lseek (fd, 0, SEEK_END)))
1937 *read_pos = end_pos;
1938 if (end_pos != 0
1939 && ! file_lines (pretty_filename, fd, n_lines,
1940 start_pos, end_pos, read_pos))
1941 return false;
1943 else
1945 /* Under very unlikely circumstances, it is possible to reach
1946 this point after positioning the file pointer to end of file
1947 via the 'lseek (...SEEK_END)' above. In that case, reposition
1948 the file pointer back to start_pos before calling pipe_lines. */
1949 if (start_pos != -1)
1950 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
1952 return pipe_lines (pretty_filename, fd, n_lines, read_pos);
1955 return true;
1958 /* Display the last N_UNITS units of file FILENAME, open for reading
1959 via FD. Set *READ_POS to the position of the input stream pointer.
1960 *READ_POS is usually the number of bytes read and corresponds to an
1961 offset from the beginning of a file. However, it may be larger than
1962 OFF_T_MAX (as for an input pipe), and may also be larger than the
1963 number of bytes read (when an input pointer is initially not at
1964 beginning of file), and may be far greater than the number of bytes
1965 actually read for an input file that is seekable.
1966 Return true if successful. */
1968 static bool
1969 tail (const char *filename, int fd, uintmax_t n_units,
1970 uintmax_t *read_pos)
1972 *read_pos = 0;
1973 if (count_lines)
1974 return tail_lines (filename, fd, n_units, read_pos);
1975 else
1976 return tail_bytes (filename, fd, n_units, read_pos);
1979 /* Display the last N_UNITS units of the file described by F.
1980 Return true if successful. */
1982 static bool
1983 tail_file (struct File_spec *f, uintmax_t n_units)
1985 int fd;
1986 bool ok;
1988 bool is_stdin = (STREQ (f->name, "-"));
1990 if (is_stdin)
1992 have_read_stdin = true;
1993 fd = STDIN_FILENO;
1994 xset_binary_mode (STDIN_FILENO, O_BINARY);
1996 else
1997 fd = open (f->name, O_RDONLY | O_BINARY);
1999 f->tailable = !(reopen_inaccessible_files && fd == -1);
2001 if (fd == -1)
2003 if (forever)
2005 f->fd = -1;
2006 f->errnum = errno;
2007 f->ignore = ! reopen_inaccessible_files;
2008 f->ino = 0;
2009 f->dev = 0;
2011 error (0, errno, _("cannot open %s for reading"),
2012 quoteaf (pretty_name (f)));
2013 ok = false;
2015 else
2017 uintmax_t read_pos;
2019 if (print_headers)
2020 write_header (pretty_name (f));
2021 ok = tail (pretty_name (f), fd, n_units, &read_pos);
2022 if (forever)
2024 struct stat stats;
2026 #if TAIL_TEST_SLEEP
2027 /* Before the tail function provided 'read_pos', there was
2028 a race condition described in the URL below. This sleep
2029 call made the window big enough to exercise the problem. */
2030 xnanosleep (1);
2031 #endif
2032 f->errnum = ok - 1;
2033 if (fstat (fd, &stats) < 0)
2035 ok = false;
2036 f->errnum = errno;
2037 error (0, errno, _("error reading %s"),
2038 quoteaf (pretty_name (f)));
2040 else if (!IS_TAILABLE_FILE_TYPE (stats.st_mode))
2042 ok = false;
2043 f->errnum = -1;
2044 f->tailable = false;
2045 f->ignore = ! reopen_inaccessible_files;
2046 error (0, 0, _("%s: cannot follow end of this type of file%s"),
2047 quotef (pretty_name (f)),
2048 f->ignore ? _("; giving up on this name") : "");
2051 if (!ok)
2053 f->ignore = ! reopen_inaccessible_files;
2054 close_fd (fd, pretty_name (f));
2055 f->fd = -1;
2057 else
2059 /* Note: we must use read_pos here, not stats.st_size,
2060 to avoid a race condition described by Ken Raeburn:
2061 https://lists.gnu.org/r/bug-textutils/2003-05/msg00007.html */
2062 record_open_fd (f, fd, read_pos, &stats, (is_stdin ? -1 : 1));
2063 f->remote = fremote (fd, pretty_name (f));
2066 else
2068 if (!is_stdin && close (fd))
2070 error (0, errno, _("error reading %s"),
2071 quoteaf (pretty_name (f)));
2072 ok = false;
2077 return ok;
2080 /* If obsolete usage is allowed, and the command line arguments are of
2081 the obsolete form and the option string is well-formed, set
2082 *N_UNITS, the globals COUNT_LINES, FOREVER, and FROM_START, and
2083 return true. If the command line arguments are obviously incorrect
2084 (e.g., because obsolete usage is not allowed and the arguments are
2085 incorrect for non-obsolete usage), report an error and exit.
2086 Otherwise, return false and don't modify any parameter or global
2087 variable. */
2089 static bool
2090 parse_obsolete_option (int argc, char * const *argv, uintmax_t *n_units)
2092 const char *p;
2093 const char *n_string;
2094 const char *n_string_end;
2095 int default_count = DEFAULT_N_LINES;
2096 bool t_from_start;
2097 bool t_count_lines = true;
2098 bool t_forever = false;
2100 /* With the obsolete form, there is one option string and at most
2101 one file argument. Watch out for "-" and "--", though. */
2102 if (! (argc == 2
2103 || (argc == 3 && ! (argv[2][0] == '-' && argv[2][1]))
2104 || (3 <= argc && argc <= 4 && STREQ (argv[2], "--"))))
2105 return false;
2107 int posix_ver = posix2_version ();
2108 bool obsolete_usage = posix_ver < 200112;
2109 bool traditional_usage = obsolete_usage || 200809 <= posix_ver;
2110 p = argv[1];
2112 switch (*p++)
2114 default:
2115 return false;
2117 case '+':
2118 /* Leading "+" is a file name in the standard form. */
2119 if (!traditional_usage)
2120 return false;
2122 t_from_start = true;
2123 break;
2125 case '-':
2126 /* In the non-obsolete form, "-" is standard input and "-c"
2127 requires an option-argument. The obsolete multidigit options
2128 are supported as a GNU extension even when conforming to
2129 POSIX 1003.1-2001 or later, so don't complain about them. */
2130 if (!obsolete_usage && !p[p[0] == 'c'])
2131 return false;
2133 t_from_start = false;
2134 break;
2137 n_string = p;
2138 while (ISDIGIT (*p))
2139 p++;
2140 n_string_end = p;
2142 switch (*p)
2144 case 'b': default_count *= 512; FALLTHROUGH;
2145 case 'c': t_count_lines = false; FALLTHROUGH;
2146 case 'l': p++; break;
2149 if (*p == 'f')
2151 t_forever = true;
2152 ++p;
2155 if (*p)
2156 return false;
2158 if (n_string == n_string_end)
2159 *n_units = default_count;
2160 else if ((xstrtoumax (n_string, NULL, 10, n_units, "b")
2161 & ~LONGINT_INVALID_SUFFIX_CHAR)
2162 != LONGINT_OK)
2164 die (EXIT_FAILURE, errno, "%s: %s", _("invalid number"),
2165 quote (argv[1]));
2168 /* Set globals. */
2169 from_start = t_from_start;
2170 count_lines = t_count_lines;
2171 forever = t_forever;
2173 return true;
2176 static void
2177 parse_options (int argc, char **argv,
2178 uintmax_t *n_units, enum header_mode *header_mode,
2179 double *sleep_interval)
2181 int c;
2183 while ((c = getopt_long (argc, argv, "c:n:fFqs:vz0123456789",
2184 long_options, NULL))
2185 != -1)
2187 switch (c)
2189 case 'F':
2190 forever = true;
2191 follow_mode = Follow_name;
2192 reopen_inaccessible_files = true;
2193 break;
2195 case 'c':
2196 case 'n':
2197 count_lines = (c == 'n');
2198 if (*optarg == '+')
2199 from_start = true;
2200 else if (*optarg == '-')
2201 ++optarg;
2203 *n_units = xdectoumax (optarg, 0, UINTMAX_MAX, "bkKmMGTPEZY0",
2204 count_lines
2205 ? _("invalid number of lines")
2206 : _("invalid number of bytes"), 0);
2207 break;
2209 case 'f':
2210 case LONG_FOLLOW_OPTION:
2211 forever = true;
2212 if (optarg == NULL)
2213 follow_mode = DEFAULT_FOLLOW_MODE;
2214 else
2215 follow_mode = XARGMATCH ("--follow", optarg,
2216 follow_mode_string, follow_mode_map);
2217 break;
2219 case RETRY_OPTION:
2220 reopen_inaccessible_files = true;
2221 break;
2223 case MAX_UNCHANGED_STATS_OPTION:
2224 /* --max-unchanged-stats=N */
2225 max_n_unchanged_stats_between_opens =
2226 xdectoumax (optarg, 0, UINTMAX_MAX, "",
2227 _("invalid maximum number of unchanged stats between opens"), 0);
2228 break;
2230 case DISABLE_INOTIFY_OPTION:
2231 disable_inotify = true;
2232 break;
2234 case PID_OPTION:
2235 pid = xdectoumax (optarg, 0, PID_T_MAX, "", _("invalid PID"), 0);
2236 break;
2238 case PRESUME_INPUT_PIPE_OPTION:
2239 presume_input_pipe = true;
2240 break;
2242 case 'q':
2243 *header_mode = never;
2244 break;
2246 case 's':
2248 double s;
2249 if (! (xstrtod (optarg, NULL, &s, cl_strtod) && 0 <= s))
2250 die (EXIT_FAILURE, 0,
2251 _("invalid number of seconds: %s"), quote (optarg));
2252 *sleep_interval = s;
2254 break;
2256 case 'v':
2257 *header_mode = always;
2258 break;
2260 case 'z':
2261 line_end = '\0';
2262 break;
2264 case_GETOPT_HELP_CHAR;
2266 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
2268 case '0': case '1': case '2': case '3': case '4':
2269 case '5': case '6': case '7': case '8': case '9':
2270 die (EXIT_FAILURE, 0, _("option used in invalid context -- %c"), c);
2272 default:
2273 usage (EXIT_FAILURE);
2277 if (reopen_inaccessible_files)
2279 if (!forever)
2281 reopen_inaccessible_files = false;
2282 error (0, 0, _("warning: --retry ignored; --retry is useful"
2283 " only when following"));
2285 else if (follow_mode == Follow_descriptor)
2286 error (0, 0, _("warning: --retry only effective for the initial open"));
2289 if (pid && !forever)
2290 error (0, 0,
2291 _("warning: PID ignored; --pid=PID is useful only when following"));
2292 else if (pid && kill (pid, 0) != 0 && errno == ENOSYS)
2294 error (0, 0, _("warning: --pid=PID is not supported on this system"));
2295 pid = 0;
2299 /* Mark as '.ignore'd each member of F that corresponds to a
2300 pipe or fifo, and return the number of non-ignored members. */
2301 static size_t
2302 ignore_fifo_and_pipe (struct File_spec *f, size_t n_files)
2304 /* When there is no FILE operand and stdin is a pipe or FIFO
2305 POSIX requires that tail ignore the -f option.
2306 Since we allow multiple FILE operands, we extend that to say: with -f,
2307 ignore any "-" operand that corresponds to a pipe or FIFO. */
2308 size_t n_viable = 0;
2310 for (size_t i = 0; i < n_files; i++)
2312 bool is_a_fifo_or_pipe =
2313 (STREQ (f[i].name, "-")
2314 && !f[i].ignore
2315 && 0 <= f[i].fd
2316 && (S_ISFIFO (f[i].mode)
2317 || (HAVE_FIFO_PIPES != 1 && isapipe (f[i].fd))));
2318 if (is_a_fifo_or_pipe)
2320 f[i].fd = -1;
2321 f[i].ignore = true;
2323 else
2324 ++n_viable;
2327 return n_viable;
2331 main (int argc, char **argv)
2333 enum header_mode header_mode = multiple_files;
2334 bool ok = true;
2335 /* If from_start, the number of items to skip before printing; otherwise,
2336 the number of items at the end of the file to print. Although the type
2337 is signed, the value is never negative. */
2338 uintmax_t n_units = DEFAULT_N_LINES;
2339 size_t n_files;
2340 char **file;
2341 struct File_spec *F;
2342 size_t i;
2343 bool obsolete_option;
2345 /* The number of seconds to sleep between iterations.
2346 During one iteration, every file name or descriptor is checked to
2347 see if it has changed. */
2348 double sleep_interval = 1.0;
2350 initialize_main (&argc, &argv);
2351 set_program_name (argv[0]);
2352 setlocale (LC_ALL, "");
2353 bindtextdomain (PACKAGE, LOCALEDIR);
2354 textdomain (PACKAGE);
2356 atexit (close_stdout);
2358 have_read_stdin = false;
2360 count_lines = true;
2361 forever = from_start = print_headers = false;
2362 line_end = '\n';
2363 obsolete_option = parse_obsolete_option (argc, argv, &n_units);
2364 argc -= obsolete_option;
2365 argv += obsolete_option;
2366 parse_options (argc, argv, &n_units, &header_mode, &sleep_interval);
2368 /* To start printing with item N_UNITS from the start of the file, skip
2369 N_UNITS - 1 items. 'tail -n +0' is actually meaningless, but for Unix
2370 compatibility it's treated the same as 'tail -n +1'. */
2371 if (from_start)
2373 if (n_units)
2374 --n_units;
2377 IF_LINT (assert (0 <= argc));
2379 if (optind < argc)
2381 n_files = argc - optind;
2382 file = argv + optind;
2384 else
2386 static char *dummy_stdin = (char *) "-";
2387 n_files = 1;
2388 file = &dummy_stdin;
2392 bool found_hyphen = false;
2394 for (i = 0; i < n_files; i++)
2395 if (STREQ (file[i], "-"))
2396 found_hyphen = true;
2398 /* When following by name, there must be a name. */
2399 if (found_hyphen && follow_mode == Follow_name)
2400 die (EXIT_FAILURE, 0, _("cannot follow %s by name"), quoteaf ("-"));
2402 /* When following forever, and not using simple blocking, warn if
2403 any file is '-' as the stats() used to check for input are ineffective.
2404 This is only a warning, since tail's output (before a failing seek,
2405 and that from any non-stdin files) might still be useful. */
2406 if (forever && found_hyphen)
2408 struct stat in_stat;
2409 bool blocking_stdin;
2410 blocking_stdin = (pid == 0 && follow_mode == Follow_descriptor
2411 && n_files == 1 && ! fstat (STDIN_FILENO, &in_stat)
2412 && ! S_ISREG (in_stat.st_mode));
2414 if (! blocking_stdin && isatty (STDIN_FILENO))
2415 error (0, 0, _("warning: following standard input"
2416 " indefinitely is ineffective"));
2420 /* Don't read anything if we'll never output anything. */
2421 if (! n_units && ! forever && ! from_start)
2422 return EXIT_SUCCESS;
2424 F = xnmalloc (n_files, sizeof *F);
2425 for (i = 0; i < n_files; i++)
2426 F[i].name = file[i];
2428 if (header_mode == always
2429 || (header_mode == multiple_files && n_files > 1))
2430 print_headers = true;
2432 xset_binary_mode (STDOUT_FILENO, O_BINARY);
2434 for (i = 0; i < n_files; i++)
2435 ok &= tail_file (&F[i], n_units);
2437 if (forever && ignore_fifo_and_pipe (F, n_files))
2439 /* If stdout is a fifo or pipe, then monitor it
2440 so that we exit if the reader goes away.
2441 Note select() on a regular file is always readable. */
2442 struct stat out_stat;
2443 if (fstat (STDOUT_FILENO, &out_stat) < 0)
2444 die (EXIT_FAILURE, errno, _("standard output"));
2445 monitor_output = (S_ISFIFO (out_stat.st_mode)
2446 || (HAVE_FIFO_PIPES != 1 && isapipe (STDOUT_FILENO)));
2448 #if HAVE_INOTIFY
2449 /* tailable_stdin() checks if the user specifies stdin via "-",
2450 or implicitly by providing no arguments. If so, we won't use inotify.
2451 Technically, on systems with a working /dev/stdin, we *could*,
2452 but would it be worth it? Verifying that it's a real device
2453 and hooked up to stdin is not trivial, while reverting to
2454 non-inotify-based tail_forever is easy and portable.
2456 any_remote_file() checks if the user has specified any
2457 files that reside on remote file systems. inotify is not used
2458 in this case because it would miss any updates to the file
2459 that were not initiated from the local system.
2461 any_non_remote_file() checks if the user has specified any
2462 files that don't reside on remote file systems. inotify is not used
2463 if there are no open files, as we can't determine if those file
2464 will be on a remote file system.
2466 any_symlinks() checks if the user has specified any symbolic links.
2467 inotify is not used in this case because it returns updated _targets_
2468 which would not match the specified names. If we tried to always
2469 use the target names, then we would miss changes to the symlink itself.
2471 ok is false when one of the files specified could not be opened for
2472 reading. In this case and when following by descriptor,
2473 tail_forever_inotify() cannot be used (in its current implementation).
2475 FIXME: inotify doesn't give any notification when a new
2476 (remote) file or directory is mounted on top a watched file.
2477 When follow_mode == Follow_name we would ideally like to detect that.
2478 Note if there is a change to the original file then we'll
2479 recheck it and follow the new file, or ignore it if the
2480 file has changed to being remote.
2482 FIXME: when using inotify, and a directory for a watched file
2483 is recreated, then we don't recheck any new file when
2484 follow_mode == Follow_name.
2486 FIXME-maybe: inotify has a watch descriptor per inode, and hence with
2487 our current hash implementation will only --follow data for one
2488 of the names when multiple hardlinked files are specified, or
2489 for one name when a name is specified multiple times. */
2490 if (!disable_inotify && (tailable_stdin (F, n_files)
2491 || any_remote_file (F, n_files)
2492 || ! any_non_remote_file (F, n_files)
2493 || any_symlinks (F, n_files)
2494 || any_non_regular_fifo (F, n_files)
2495 || (!ok && follow_mode == Follow_descriptor)))
2496 disable_inotify = true;
2498 if (!disable_inotify)
2500 int wd = inotify_init ();
2501 if (0 <= wd)
2503 /* Flush any output from tail_file, now, since
2504 tail_forever_inotify flushes only after writing,
2505 not before reading. */
2506 if (fflush (stdout) != 0)
2507 die (EXIT_FAILURE, errno, _("write error"));
2509 if (! tail_forever_inotify (wd, F, n_files, sleep_interval))
2510 return EXIT_FAILURE;
2512 error (0, errno, _("inotify cannot be used, reverting to polling"));
2514 /* Free resources as this process can be long lived,
2515 and we may have exhausted system resources above. */
2517 for (i = 0; i < n_files; i++)
2519 /* It's OK to remove the same watch multiple times,
2520 ignoring the EINVAL from redundant calls. */
2521 if (F[i].wd != -1)
2522 inotify_rm_watch (wd, F[i].wd);
2523 if (F[i].parent_wd != -1)
2524 inotify_rm_watch (wd, F[i].parent_wd);
2527 #endif
2528 disable_inotify = true;
2529 tail_forever (F, n_files, sleep_interval);
2532 IF_LINT (free (F));
2534 if (have_read_stdin && close (STDIN_FILENO) < 0)
2535 die (EXIT_FAILURE, errno, "-");
2536 return ok ? EXIT_SUCCESS : EXIT_FAILURE;