tail: with --pid, ensure all inotify events are processed
[coreutils.git] / src / tail.c
blob3582321bce882b3e731871bcf87cd5bb09e5c07a
1 /* tail -- output the last part of file(s)
2 Copyright (C) 1989-2017 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 <http://www.gnu.org/licenses/>. */
17 /* Can display any amount of data, unlike the Unix version, which uses
18 a fixed size buffer and therefore can only deliver a limited number
19 of lines.
21 Original version by Paul Rubin <phr@ocf.berkeley.edu>.
22 Extensions by David MacKenzie <djm@gnu.ai.mit.edu>.
23 tail -f for multiple files by Ian Lance Taylor <ian@airs.com>.
24 inotify back-end by Giuseppe Scrivano <gscrivano@gnu.org>. */
26 #include <config.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <getopt.h>
31 #include <sys/types.h>
32 #include <signal.h>
34 #include "system.h"
35 #include "argmatch.h"
36 #include "c-strtod.h"
37 #include "die.h"
38 #include "error.h"
39 #include "fcntl--.h"
40 #include "isapipe.h"
41 #include "posixver.h"
42 #include "quote.h"
43 #include "safe-read.h"
44 #include "stat-size.h"
45 #include "stat-time.h"
46 #include "xbinary-io.h"
47 #include "xdectoint.h"
48 #include "xnanosleep.h"
49 #include "xstrtol.h"
50 #include "xstrtod.h"
52 #if HAVE_INOTIFY
53 # include "hash.h"
54 # include <sys/inotify.h>
55 /* 'select' is used by tail_forever_inotify. */
56 # include <sys/select.h>
58 /* inotify needs to know if a file is local. */
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", NULL
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, count from start of file instead of end. */
179 static bool from_start;
181 /* If true, print filename headers. */
182 static bool print_headers;
184 /* Character to split lines by. */
185 static char line_end;
187 /* When to print the filename banners. */
188 enum header_mode
190 multiple_files, always, never
193 /* When tailing a file by name, if there have been this many consecutive
194 iterations for which the file has not changed, then open/fstat
195 the file to determine if that file name is still associated with the
196 same device/inode-number pair as before. This option is meaningful only
197 when following by name. --max-unchanged-stats=N */
198 #define DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS 5
199 static uintmax_t max_n_unchanged_stats_between_opens =
200 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS;
202 /* The process ID of the process (presumably on the current host)
203 that is writing to all followed files. */
204 static pid_t pid;
206 /* True if we have ever read standard input. */
207 static bool have_read_stdin;
209 /* If nonzero, skip the is-regular-file test used to determine whether
210 to use the lseek optimization. Instead, use the more general (and
211 more expensive) code unconditionally. Intended solely for testing. */
212 static bool presume_input_pipe;
214 /* If nonzero then don't use inotify even if available. */
215 static bool disable_inotify;
217 /* For long options that have no equivalent short option, use a
218 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
219 enum
221 RETRY_OPTION = CHAR_MAX + 1,
222 MAX_UNCHANGED_STATS_OPTION,
223 PID_OPTION,
224 PRESUME_INPUT_PIPE_OPTION,
225 LONG_FOLLOW_OPTION,
226 DISABLE_INOTIFY_OPTION
229 static struct option const long_options[] =
231 {"bytes", required_argument, NULL, 'c'},
232 {"follow", optional_argument, NULL, LONG_FOLLOW_OPTION},
233 {"lines", required_argument, NULL, 'n'},
234 {"max-unchanged-stats", required_argument, NULL, MAX_UNCHANGED_STATS_OPTION},
235 {"-disable-inotify", no_argument, NULL,
236 DISABLE_INOTIFY_OPTION}, /* do not document */
237 {"pid", required_argument, NULL, PID_OPTION},
238 {"-presume-input-pipe", no_argument, NULL,
239 PRESUME_INPUT_PIPE_OPTION}, /* do not document */
240 {"quiet", no_argument, NULL, 'q'},
241 {"retry", no_argument, NULL, RETRY_OPTION},
242 {"silent", no_argument, NULL, 'q'},
243 {"sleep-interval", required_argument, NULL, 's'},
244 {"verbose", no_argument, NULL, 'v'},
245 {"zero-terminated", no_argument, NULL, 'z'},
246 {GETOPT_HELP_OPTION_DECL},
247 {GETOPT_VERSION_OPTION_DECL},
248 {NULL, 0, NULL, 0}
251 void
252 usage (int status)
254 if (status != EXIT_SUCCESS)
255 emit_try_help ();
256 else
258 printf (_("\
259 Usage: %s [OPTION]... [FILE]...\n\
261 program_name);
262 printf (_("\
263 Print the last %d lines of each FILE to standard output.\n\
264 With more than one FILE, precede each with a header giving the file name.\n\
265 "), DEFAULT_N_LINES);
267 emit_stdin_note ();
268 emit_mandatory_arg_note ();
270 fputs (_("\
271 -c, --bytes=[+]NUM output the last NUM bytes; or use -c +NUM to\n\
272 output starting with byte NUM of each file\n\
273 "), stdout);
274 fputs (_("\
275 -f, --follow[={name|descriptor}]\n\
276 output appended data as the file grows;\n\
277 an absent option argument means 'descriptor'\n\
278 -F same as --follow=name --retry\n\
279 "), stdout);
280 printf (_("\
281 -n, --lines=[+]NUM output the last NUM lines, instead of the last %d;\n\
282 or use -n +NUM to output starting with line NUM\n\
283 --max-unchanged-stats=N\n\
284 with --follow=name, reopen a FILE which has not\n\
285 changed size after N (default %d) iterations\n\
286 to see if it has been unlinked or renamed\n\
287 (this is the usual case of rotated log files);\n\
288 with inotify, this option is rarely useful\n\
290 DEFAULT_N_LINES,
291 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS
293 fputs (_("\
294 --pid=PID with -f, terminate after process ID, PID dies\n\
295 -q, --quiet, --silent never output headers giving file names\n\
296 --retry keep trying to open a file if it is inaccessible\n\
297 "), stdout);
298 fputs (_("\
299 -s, --sleep-interval=N with -f, sleep for approximately N seconds\n\
300 (default 1.0) between iterations;\n\
301 with inotify and --pid=P, check process P at\n\
302 least once every N seconds\n\
303 -v, --verbose always output headers giving file names\n\
304 "), stdout);
305 fputs (_("\
306 -z, --zero-terminated line delimiter is NUL, not newline\n\
307 "), stdout);
308 fputs (HELP_OPTION_DESCRIPTION, stdout);
309 fputs (VERSION_OPTION_DESCRIPTION, stdout);
310 fputs (_("\
312 NUM may have a multiplier suffix:\n\
313 b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\
314 GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.\n\
316 "), stdout);
317 fputs (_("\
318 With --follow (-f), tail defaults to following the file descriptor, which\n\
319 means that even if a tail'ed file is renamed, tail will continue to track\n\
320 its end. This default behavior is not desirable when you really want to\n\
321 track the actual name of the file, not the file descriptor (e.g., log\n\
322 rotation). Use --follow=name in that case. That causes tail to track the\n\
323 named file in a way that accommodates renaming, removal and creation.\n\
324 "), stdout);
325 emit_ancillary_info (PROGRAM_NAME);
327 exit (status);
330 static bool
331 valid_file_spec (struct File_spec const *f)
333 /* Exactly one of the following subexpressions must be true. */
334 return ((f->fd == -1) ^ (f->errnum == 0));
337 static char const *
338 pretty_name (struct File_spec const *f)
340 return (STREQ (f->name, "-") ? _("standard input") : f->name);
343 /* Record a file F with descriptor FD, size SIZE, status ST, and
344 blocking status BLOCKING. */
346 static void
347 record_open_fd (struct File_spec *f, int fd,
348 off_t size, struct stat const *st,
349 int blocking)
351 f->fd = fd;
352 f->size = size;
353 f->mtime = get_stat_mtime (st);
354 f->dev = st->st_dev;
355 f->ino = st->st_ino;
356 f->mode = st->st_mode;
357 f->blocking = blocking;
358 f->n_unchanged_stats = 0;
359 f->ignore = false;
362 /* Close the file with descriptor FD and name FILENAME. */
364 static void
365 close_fd (int fd, const char *filename)
367 if (fd != -1 && fd != STDIN_FILENO && close (fd))
369 error (0, errno, _("closing %s (fd=%d)"), quoteaf (filename), fd);
373 static void
374 write_header (const char *pretty_filename)
376 static bool first_file = true;
378 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), pretty_filename);
379 first_file = false;
382 /* Write N_BYTES from BUFFER to stdout.
383 Exit immediately on error with a single diagnostic. */
385 static void
386 xwrite_stdout (char const *buffer, size_t n_bytes)
388 if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) < n_bytes)
390 clearerr (stdout); /* To avoid redundant close_stdout diagnostic. */
391 die (EXIT_FAILURE, errno, _("error writing %s"),
392 quoteaf ("standard output"));
396 /* Read and output N_BYTES of file PRETTY_FILENAME starting at the current
397 position in FD. If N_BYTES is COPY_TO_EOF, then copy until end of file.
398 If N_BYTES is COPY_A_BUFFER, then copy at most one buffer's worth.
399 Return the number of bytes read from the file. */
401 static uintmax_t
402 dump_remainder (bool want_header, const char *pretty_filename, int fd,
403 uintmax_t n_bytes)
405 uintmax_t n_written;
406 uintmax_t n_remaining = n_bytes;
408 n_written = 0;
409 while (1)
411 char buffer[BUFSIZ];
412 size_t n = MIN (n_remaining, BUFSIZ);
413 size_t bytes_read = safe_read (fd, buffer, n);
414 if (bytes_read == SAFE_READ_ERROR)
416 if (errno != EAGAIN)
417 die (EXIT_FAILURE, errno, _("error reading %s"),
418 quoteaf (pretty_filename));
419 break;
421 if (bytes_read == 0)
422 break;
423 if (want_header)
425 write_header (pretty_filename);
426 want_header = false;
428 xwrite_stdout (buffer, bytes_read);
429 n_written += bytes_read;
430 if (n_bytes != COPY_TO_EOF)
432 n_remaining -= bytes_read;
433 if (n_remaining == 0 || n_bytes == COPY_A_BUFFER)
434 break;
438 return n_written;
441 /* Call lseek with the specified arguments, where file descriptor FD
442 corresponds to the file, FILENAME.
443 Give a diagnostic and exit nonzero if lseek fails.
444 Otherwise, return the resulting offset. */
446 static off_t
447 xlseek (int fd, off_t offset, int whence, char const *filename)
449 off_t new_offset = lseek (fd, offset, whence);
450 char buf[INT_BUFSIZE_BOUND (offset)];
451 char *s;
453 if (0 <= new_offset)
454 return new_offset;
456 s = offtostr (offset, buf);
457 switch (whence)
459 case SEEK_SET:
460 error (0, errno, _("%s: cannot seek to offset %s"),
461 quotef (filename), s);
462 break;
463 case SEEK_CUR:
464 error (0, errno, _("%s: cannot seek to relative offset %s"),
465 quotef (filename), s);
466 break;
467 case SEEK_END:
468 error (0, errno, _("%s: cannot seek to end-relative offset %s"),
469 quotef (filename), s);
470 break;
471 default:
472 abort ();
475 exit (EXIT_FAILURE);
478 /* Print the last N_LINES lines from the end of file FD.
479 Go backward through the file, reading 'BUFSIZ' bytes at a time (except
480 probably the first), until we hit the start of the file or have
481 read NUMBER newlines.
482 START_POS is the starting position of the read pointer for the file
483 associated with FD (may be nonzero).
484 END_POS is the file offset of EOF (one larger than offset of last byte).
485 Return true if successful. */
487 static bool
488 file_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
489 off_t start_pos, off_t end_pos, uintmax_t *read_pos)
491 char buffer[BUFSIZ];
492 size_t bytes_read;
493 off_t pos = end_pos;
495 if (n_lines == 0)
496 return true;
498 /* Set 'bytes_read' to the size of the last, probably partial, buffer;
499 0 < 'bytes_read' <= 'BUFSIZ'. */
500 bytes_read = (pos - start_pos) % BUFSIZ;
501 if (bytes_read == 0)
502 bytes_read = BUFSIZ;
503 /* Make 'pos' a multiple of 'BUFSIZ' (0 if the file is short), so that all
504 reads will be on block boundaries, which might increase efficiency. */
505 pos -= bytes_read;
506 xlseek (fd, pos, SEEK_SET, pretty_filename);
507 bytes_read = safe_read (fd, buffer, bytes_read);
508 if (bytes_read == SAFE_READ_ERROR)
510 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
511 return false;
513 *read_pos = pos + bytes_read;
515 /* Count the incomplete line on files that don't end with a newline. */
516 if (bytes_read && buffer[bytes_read - 1] != line_end)
517 --n_lines;
521 /* Scan backward, counting the newlines in this bufferfull. */
523 size_t n = bytes_read;
524 while (n)
526 char const *nl;
527 nl = memrchr (buffer, line_end, n);
528 if (nl == NULL)
529 break;
530 n = nl - buffer;
531 if (n_lines-- == 0)
533 /* If this newline isn't the last character in the buffer,
534 output the part that is after it. */
535 if (n != bytes_read - 1)
536 xwrite_stdout (nl + 1, bytes_read - (n + 1));
537 *read_pos += dump_remainder (false, pretty_filename, fd,
538 end_pos - (pos + bytes_read));
539 return true;
543 /* Not enough newlines in that bufferfull. */
544 if (pos == start_pos)
546 /* Not enough lines in the file; print everything from
547 start_pos to the end. */
548 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
549 *read_pos = start_pos + dump_remainder (false, pretty_filename, fd,
550 end_pos);
551 return true;
553 pos -= BUFSIZ;
554 xlseek (fd, pos, SEEK_SET, pretty_filename);
556 bytes_read = safe_read (fd, buffer, BUFSIZ);
557 if (bytes_read == SAFE_READ_ERROR)
559 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
560 return false;
563 *read_pos = pos + bytes_read;
565 while (bytes_read > 0);
567 return true;
570 /* Print the last N_LINES lines from the end of the standard input,
571 open for reading as pipe FD.
572 Buffer the text as a linked list of LBUFFERs, adding them as needed.
573 Return true if successful. */
575 static bool
576 pipe_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
577 uintmax_t *read_pos)
579 struct linebuffer
581 char buffer[BUFSIZ];
582 size_t nbytes;
583 size_t nlines;
584 struct linebuffer *next;
586 typedef struct linebuffer LBUFFER;
587 LBUFFER *first, *last, *tmp;
588 size_t total_lines = 0; /* Total number of newlines in all buffers. */
589 bool ok = true;
590 size_t n_read; /* Size in bytes of most recent read */
592 first = last = xmalloc (sizeof (LBUFFER));
593 first->nbytes = first->nlines = 0;
594 first->next = NULL;
595 tmp = xmalloc (sizeof (LBUFFER));
597 /* Input is always read into a fresh buffer. */
598 while (1)
600 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
601 if (n_read == 0 || n_read == SAFE_READ_ERROR)
602 break;
603 tmp->nbytes = n_read;
604 *read_pos += n_read;
605 tmp->nlines = 0;
606 tmp->next = NULL;
608 /* Count the number of newlines just read. */
610 char const *buffer_end = tmp->buffer + n_read;
611 char const *p = tmp->buffer;
612 while ((p = memchr (p, line_end, buffer_end - p)))
614 ++p;
615 ++tmp->nlines;
618 total_lines += tmp->nlines;
620 /* If there is enough room in the last buffer read, just append the new
621 one to it. This is because when reading from a pipe, 'n_read' can
622 often be very small. */
623 if (tmp->nbytes + last->nbytes < BUFSIZ)
625 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
626 last->nbytes += tmp->nbytes;
627 last->nlines += tmp->nlines;
629 else
631 /* If there's not enough room, link the new buffer onto the end of
632 the list, then either free up the oldest buffer for the next
633 read if that would leave enough lines, or else malloc a new one.
634 Some compaction mechanism is possible but probably not
635 worthwhile. */
636 last = last->next = tmp;
637 if (total_lines - first->nlines > n_lines)
639 tmp = first;
640 total_lines -= first->nlines;
641 first = first->next;
643 else
644 tmp = xmalloc (sizeof (LBUFFER));
648 free (tmp);
650 if (n_read == SAFE_READ_ERROR)
652 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
653 ok = false;
654 goto free_lbuffers;
657 /* If the file is empty, then bail out. */
658 if (last->nbytes == 0)
659 goto free_lbuffers;
661 /* This prevents a core dump when the pipe contains no newlines. */
662 if (n_lines == 0)
663 goto free_lbuffers;
665 /* Count the incomplete line on files that don't end with a newline. */
666 if (last->buffer[last->nbytes - 1] != line_end)
668 ++last->nlines;
669 ++total_lines;
672 /* Run through the list, printing lines. First, skip over unneeded
673 buffers. */
674 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
675 total_lines -= tmp->nlines;
677 /* Find the correct beginning, then print the rest of the file. */
679 char const *beg = tmp->buffer;
680 char const *buffer_end = tmp->buffer + tmp->nbytes;
681 if (total_lines > n_lines)
683 /* Skip 'total_lines' - 'n_lines' newlines. We made sure that
684 'total_lines' - 'n_lines' <= 'tmp->nlines'. */
685 size_t j;
686 for (j = total_lines - n_lines; j; --j)
688 beg = memchr (beg, line_end, buffer_end - beg);
689 assert (beg);
690 ++beg;
694 xwrite_stdout (beg, buffer_end - beg);
697 for (tmp = tmp->next; tmp; tmp = tmp->next)
698 xwrite_stdout (tmp->buffer, tmp->nbytes);
700 free_lbuffers:
701 while (first)
703 tmp = first->next;
704 free (first);
705 first = tmp;
707 return ok;
710 /* Print the last N_BYTES characters from the end of pipe FD.
711 This is a stripped down version of pipe_lines.
712 Return true if successful. */
714 static bool
715 pipe_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
716 uintmax_t *read_pos)
718 struct charbuffer
720 char buffer[BUFSIZ];
721 size_t nbytes;
722 struct charbuffer *next;
724 typedef struct charbuffer CBUFFER;
725 CBUFFER *first, *last, *tmp;
726 size_t i; /* Index into buffers. */
727 size_t total_bytes = 0; /* Total characters in all buffers. */
728 bool ok = true;
729 size_t n_read;
731 first = last = xmalloc (sizeof (CBUFFER));
732 first->nbytes = 0;
733 first->next = NULL;
734 tmp = xmalloc (sizeof (CBUFFER));
736 /* Input is always read into a fresh buffer. */
737 while (1)
739 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
740 if (n_read == 0 || n_read == SAFE_READ_ERROR)
741 break;
742 *read_pos += n_read;
743 tmp->nbytes = n_read;
744 tmp->next = NULL;
746 total_bytes += tmp->nbytes;
747 /* If there is enough room in the last buffer read, just append the new
748 one to it. This is because when reading from a pipe, 'nbytes' can
749 often be very small. */
750 if (tmp->nbytes + last->nbytes < BUFSIZ)
752 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
753 last->nbytes += tmp->nbytes;
755 else
757 /* If there's not enough room, link the new buffer onto the end of
758 the list, then either free up the oldest buffer for the next
759 read if that would leave enough characters, or else malloc a new
760 one. Some compaction mechanism is possible but probably not
761 worthwhile. */
762 last = last->next = tmp;
763 if (total_bytes - first->nbytes > n_bytes)
765 tmp = first;
766 total_bytes -= first->nbytes;
767 first = first->next;
769 else
771 tmp = xmalloc (sizeof (CBUFFER));
776 free (tmp);
778 if (n_read == SAFE_READ_ERROR)
780 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
781 ok = false;
782 goto free_cbuffers;
785 /* Run through the list, printing characters. First, skip over unneeded
786 buffers. */
787 for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
788 total_bytes -= tmp->nbytes;
790 /* Find the correct beginning, then print the rest of the file.
791 We made sure that 'total_bytes' - 'n_bytes' <= 'tmp->nbytes'. */
792 if (total_bytes > n_bytes)
793 i = total_bytes - n_bytes;
794 else
795 i = 0;
796 xwrite_stdout (&tmp->buffer[i], tmp->nbytes - i);
798 for (tmp = tmp->next; tmp; tmp = tmp->next)
799 xwrite_stdout (tmp->buffer, tmp->nbytes);
801 free_cbuffers:
802 while (first)
804 tmp = first->next;
805 free (first);
806 first = tmp;
808 return ok;
811 /* Skip N_BYTES characters from the start of pipe FD, and print
812 any extra characters that were read beyond that.
813 Return 1 on error, 0 if ok, -1 if EOF. */
815 static int
816 start_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
817 uintmax_t *read_pos)
819 char buffer[BUFSIZ];
821 while (0 < n_bytes)
823 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
824 if (bytes_read == 0)
825 return -1;
826 if (bytes_read == SAFE_READ_ERROR)
828 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
829 return 1;
831 *read_pos += bytes_read;
832 if (bytes_read <= n_bytes)
833 n_bytes -= bytes_read;
834 else
836 size_t n_remaining = bytes_read - n_bytes;
837 if (n_remaining)
838 xwrite_stdout (&buffer[n_bytes], n_remaining);
839 break;
843 return 0;
846 /* Skip N_LINES lines at the start of file or pipe FD, and print
847 any extra characters that were read beyond that.
848 Return 1 on error, 0 if ok, -1 if EOF. */
850 static int
851 start_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
852 uintmax_t *read_pos)
854 if (n_lines == 0)
855 return 0;
857 while (1)
859 char buffer[BUFSIZ];
860 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
861 if (bytes_read == 0) /* EOF */
862 return -1;
863 if (bytes_read == SAFE_READ_ERROR) /* error */
865 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
866 return 1;
869 char *buffer_end = buffer + bytes_read;
871 *read_pos += bytes_read;
873 char *p = buffer;
874 while ((p = memchr (p, line_end, buffer_end - p)))
876 ++p;
877 if (--n_lines == 0)
879 if (p < buffer_end)
880 xwrite_stdout (p, buffer_end - p);
881 return 0;
887 /* Return false when FD is open on a file residing on a local file system.
888 If fstatfs fails, give a diagnostic and return true.
889 If fstatfs cannot be called, return true. */
890 static bool
891 fremote (int fd, const char *name)
893 bool remote = true; /* be conservative (poll by default). */
895 #if HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE && defined __linux__
896 struct statfs buf;
897 int err = fstatfs (fd, &buf);
898 if (err != 0)
900 /* On at least linux-2.6.38, fstatfs fails with ENOSYS when FD
901 is open on a pipe. Treat that like a remote file. */
902 if (errno != ENOSYS)
903 error (0, errno, _("cannot determine location of %s. "
904 "reverting to polling"), quoteaf (name));
906 else
908 switch (is_local_fs_type (buf.f_type))
910 case 0:
911 break;
912 case -1:
913 /* Treat unrecognized file systems as "remote", so caller polls.
914 Note README-release has instructions for syncing the internal
915 list with the latest Linux kernel file system constants. */
916 break;
917 case 1:
918 remote = false;
919 break;
920 default:
921 assert (!"unexpected return value from is_local_fs_type");
924 #endif
926 return remote;
929 /* open/fstat F->name and handle changes. */
930 static void
931 recheck (struct File_spec *f, bool blocking)
933 struct stat new_stats;
934 bool ok = true;
935 bool is_stdin = (STREQ (f->name, "-"));
936 bool was_tailable = f->tailable;
937 int prev_errnum = f->errnum;
938 bool new_file;
939 int fd = (is_stdin
940 ? STDIN_FILENO
941 : open (f->name, O_RDONLY | (blocking ? 0 : O_NONBLOCK)));
943 assert (valid_file_spec (f));
945 /* If the open fails because the file doesn't exist,
946 then mark the file as not tailable. */
947 f->tailable = !(reopen_inaccessible_files && fd == -1);
949 if (! disable_inotify && ! lstat (f->name, &new_stats)
950 && S_ISLNK (new_stats.st_mode))
952 /* Diagnose the edge case where a regular file is changed
953 to a symlink. We avoid inotify with symlinks since
954 it's awkward to match between symlink name and target. */
955 ok = false;
956 f->errnum = -1;
957 f->ignore = true;
959 error (0, 0, _("%s has been replaced with an untailable symbolic link"),
960 quoteaf (pretty_name (f)));
962 else if (fd == -1 || fstat (fd, &new_stats) < 0)
964 ok = false;
965 f->errnum = errno;
966 if (!f->tailable)
968 if (was_tailable)
970 /* FIXME-maybe: detect the case in which the file first becomes
971 unreadable (perms), and later becomes readable again and can
972 be seen to be the same file (dev/ino). Otherwise, tail prints
973 the entire contents of the file when it becomes readable. */
974 error (0, f->errnum, _("%s has become inaccessible"),
975 quoteaf (pretty_name (f)));
977 else
979 /* say nothing... it's still not tailable */
982 else if (prev_errnum != errno)
983 error (0, errno, "%s", quotef (pretty_name (f)));
985 else if (!IS_TAILABLE_FILE_TYPE (new_stats.st_mode))
987 ok = false;
988 f->errnum = -1;
989 f->tailable = false;
990 f->ignore = ! (reopen_inaccessible_files && follow_mode == Follow_name);
991 if (was_tailable || prev_errnum != f->errnum)
992 error (0, 0, _("%s has been replaced with an untailable file%s"),
993 quoteaf (pretty_name (f)),
994 f->ignore ? _("; giving up on this name") : "");
996 else if ((f->remote = fremote (fd, pretty_name (f))) && ! disable_inotify)
998 ok = false;
999 f->errnum = -1;
1000 error (0, 0, _("%s has been replaced with an untailable remote file"),
1001 quoteaf (pretty_name (f)));
1002 f->ignore = true;
1003 f->remote = true;
1005 else
1007 f->errnum = 0;
1010 new_file = false;
1011 if (!ok)
1013 close_fd (fd, pretty_name (f));
1014 close_fd (f->fd, pretty_name (f));
1015 f->fd = -1;
1017 else if (prev_errnum && prev_errnum != ENOENT)
1019 new_file = true;
1020 assert (f->fd == -1);
1021 error (0, 0, _("%s has become accessible"), quoteaf (pretty_name (f)));
1023 else if (f->fd == -1)
1025 /* A new file even when inodes haven't changed as <dev,inode>
1026 pairs can be reused, and we know the file was missing
1027 on the previous iteration. Note this also means the file
1028 is redisplayed in --follow=name mode if renamed away from
1029 and back to a monitored name. */
1030 new_file = true;
1032 error (0, 0,
1033 _("%s has appeared; following new file"),
1034 quoteaf (pretty_name (f)));
1036 else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
1038 /* File has been replaced (e.g., via log rotation) --
1039 tail the new one. */
1040 new_file = true;
1042 error (0, 0,
1043 _("%s has been replaced; following new file"),
1044 quoteaf (pretty_name (f)));
1046 /* Close the old one. */
1047 close_fd (f->fd, pretty_name (f));
1050 else
1052 /* No changes detected, so close new fd. */
1053 close_fd (fd, pretty_name (f));
1056 /* FIXME: When a log is rotated, daemons tend to log to the
1057 old file descriptor until the new file is present and
1058 the daemon is sent a signal. Therefore tail may miss entries
1059 being written to the old file. Perhaps we should keep
1060 the older file open and continue to monitor it until
1061 data is written to a new file. */
1062 if (new_file)
1064 /* Start at the beginning of the file. */
1065 record_open_fd (f, fd, 0, &new_stats, (is_stdin ? -1 : blocking));
1066 xlseek (fd, 0, SEEK_SET, pretty_name (f));
1070 /* Return true if any of the N_FILES files in F are live, i.e., have
1071 open file descriptors, or should be checked again (see --retry).
1072 When following descriptors, checking should only continue when any
1073 of the files is not yet ignored. */
1075 static bool
1076 any_live_files (const struct File_spec *f, size_t n_files)
1078 size_t i;
1080 /* In inotify mode, ignore may be set for files
1081 which may later be replaced with new files.
1082 So always consider files live in -F mode. */
1083 if (reopen_inaccessible_files && follow_mode == Follow_name)
1084 return true;
1086 for (i = 0; i < n_files; i++)
1088 if (0 <= f[i].fd)
1089 return true;
1090 else
1092 if (! f[i].ignore && reopen_inaccessible_files)
1093 return true;
1097 return false;
1100 /* Tail N_FILES files forever, or until killed.
1101 The pertinent information for each file is stored in an entry of F.
1102 Loop over each of them, doing an fstat to see if they have changed size,
1103 and an occasional open/fstat to see if any dev/ino pair has changed.
1104 If none of them have changed size in one iteration, sleep for a
1105 while and try again. Continue until the user interrupts us. */
1107 static void
1108 tail_forever (struct File_spec *f, size_t n_files, double sleep_interval)
1110 /* Use blocking I/O as an optimization, when it's easy. */
1111 bool blocking = (pid == 0 && follow_mode == Follow_descriptor
1112 && n_files == 1 && f[0].fd != -1 && ! S_ISREG (f[0].mode));
1113 size_t last;
1114 bool writer_is_dead = false;
1116 last = n_files - 1;
1118 while (1)
1120 size_t i;
1121 bool any_input = false;
1123 for (i = 0; i < n_files; i++)
1125 int fd;
1126 char const *name;
1127 mode_t mode;
1128 struct stat stats;
1129 uintmax_t bytes_read;
1131 if (f[i].ignore)
1132 continue;
1134 if (f[i].fd < 0)
1136 recheck (&f[i], blocking);
1137 continue;
1140 fd = f[i].fd;
1141 name = pretty_name (&f[i]);
1142 mode = f[i].mode;
1144 if (f[i].blocking != blocking)
1146 int old_flags = fcntl (fd, F_GETFL);
1147 int new_flags = old_flags | (blocking ? 0 : O_NONBLOCK);
1148 if (old_flags < 0
1149 || (new_flags != old_flags
1150 && fcntl (fd, F_SETFL, new_flags) == -1))
1152 /* Don't update f[i].blocking if fcntl fails. */
1153 if (S_ISREG (f[i].mode) && errno == EPERM)
1155 /* This happens when using tail -f on a file with
1156 the append-only attribute. */
1158 else
1159 die (EXIT_FAILURE, errno,
1160 _("%s: cannot change nonblocking mode"),
1161 quotef (name));
1163 else
1164 f[i].blocking = blocking;
1167 if (!f[i].blocking)
1169 if (fstat (fd, &stats) != 0)
1171 f[i].fd = -1;
1172 f[i].errnum = errno;
1173 error (0, errno, "%s", quotef (name));
1174 close (fd); /* ignore failure */
1175 continue;
1178 if (f[i].mode == stats.st_mode
1179 && (! S_ISREG (stats.st_mode) || f[i].size == stats.st_size)
1180 && timespec_cmp (f[i].mtime, get_stat_mtime (&stats)) == 0)
1182 if ((max_n_unchanged_stats_between_opens
1183 <= f[i].n_unchanged_stats++)
1184 && follow_mode == Follow_name)
1186 recheck (&f[i], f[i].blocking);
1187 f[i].n_unchanged_stats = 0;
1189 continue;
1192 /* This file has changed. Print out what we can, and
1193 then keep looping. */
1195 f[i].mtime = get_stat_mtime (&stats);
1196 f[i].mode = stats.st_mode;
1198 /* reset counter */
1199 f[i].n_unchanged_stats = 0;
1201 /* XXX: This is only a heuristic, as the file may have also
1202 been truncated and written to if st_size >= size
1203 (in which case we ignore new data <= size). */
1204 if (S_ISREG (mode) && stats.st_size < f[i].size)
1206 error (0, 0, _("%s: file truncated"), quotef (name));
1207 /* Assume the file was truncated to 0,
1208 and therefore output all "new" data. */
1209 xlseek (fd, 0, SEEK_SET, name);
1210 f[i].size = 0;
1213 if (i != last)
1215 if (print_headers)
1216 write_header (name);
1217 last = i;
1221 /* Don't read more than st_size on networked file systems
1222 because it was seen on glusterfs at least, that st_size
1223 may be smaller than the data read on a _subsequent_ stat call. */
1224 uintmax_t bytes_to_read;
1225 if (f[i].blocking)
1226 bytes_to_read = COPY_A_BUFFER;
1227 else if (S_ISREG (mode) && f[i].remote)
1228 bytes_to_read = stats.st_size - f[i].size;
1229 else
1230 bytes_to_read = COPY_TO_EOF;
1232 bytes_read = dump_remainder (false, name, fd, bytes_to_read);
1234 any_input |= (bytes_read != 0);
1235 f[i].size += bytes_read;
1238 if (! any_live_files (f, n_files))
1240 error (0, 0, _("no files remaining"));
1241 break;
1244 if ((!any_input || blocking) && fflush (stdout) != 0)
1245 die (EXIT_FAILURE, errno, _("write error"));
1247 /* If nothing was read, sleep and/or check for dead writers. */
1248 if (!any_input)
1250 if (writer_is_dead)
1251 break;
1253 /* Once the writer is dead, read the files once more to
1254 avoid a race condition. */
1255 writer_is_dead = (pid != 0
1256 && kill (pid, 0) != 0
1257 /* Handle the case in which you cannot send a
1258 signal to the writer, so kill fails and sets
1259 errno to EPERM. */
1260 && errno != EPERM);
1262 if (!writer_is_dead && xnanosleep (sleep_interval))
1263 die (EXIT_FAILURE, errno, _("cannot read realtime clock"));
1269 #if HAVE_INOTIFY
1271 /* Return true if any of the N_FILES files in F is remote, i.e., has
1272 an open file descriptor and is on a network file system. */
1274 static bool
1275 any_remote_file (const struct File_spec *f, size_t n_files)
1277 size_t i;
1279 for (i = 0; i < n_files; i++)
1280 if (0 <= f[i].fd && f[i].remote)
1281 return true;
1282 return false;
1285 /* Return true if any of the N_FILES files in F is non remote, i.e., has
1286 an open file descriptor and is not on a network file system. */
1288 static bool
1289 any_non_remote_file (const struct File_spec *f, size_t n_files)
1291 size_t i;
1293 for (i = 0; i < n_files; i++)
1294 if (0 <= f[i].fd && ! f[i].remote)
1295 return true;
1296 return false;
1299 /* Return true if any of the N_FILES files in F is a symlink.
1300 Note we don't worry about the edge case where "-" exists,
1301 since that will have the same consequences for inotify,
1302 which is the only context this function is currently used. */
1304 static bool
1305 any_symlinks (const struct File_spec *f, size_t n_files)
1307 size_t i;
1309 struct stat st;
1310 for (i = 0; i < n_files; i++)
1311 if (lstat (f[i].name, &st) == 0 && S_ISLNK (st.st_mode))
1312 return true;
1313 return false;
1316 /* Return true if any of the N_FILES files in F represents
1317 stdin and is tailable. */
1319 static bool
1320 tailable_stdin (const struct File_spec *f, size_t n_files)
1322 size_t i;
1324 for (i = 0; i < n_files; i++)
1325 if (!f[i].ignore && STREQ (f[i].name, "-"))
1326 return true;
1327 return false;
1330 static size_t
1331 wd_hasher (const void *entry, size_t tabsize)
1333 const struct File_spec *spec = entry;
1334 return spec->wd % tabsize;
1337 static bool
1338 wd_comparator (const void *e1, const void *e2)
1340 const struct File_spec *spec1 = e1;
1341 const struct File_spec *spec2 = e2;
1342 return spec1->wd == spec2->wd;
1345 /* Output (new) data for FSPEC->fd.
1346 PREV_FSPEC records the last File_spec for which we output. */
1347 static void
1348 check_fspec (struct File_spec *fspec, struct File_spec **prev_fspec)
1350 struct stat stats;
1351 char const *name;
1353 if (fspec->fd == -1)
1354 return;
1356 name = pretty_name (fspec);
1358 if (fstat (fspec->fd, &stats) != 0)
1360 fspec->errnum = errno;
1361 close_fd (fspec->fd, name);
1362 fspec->fd = -1;
1363 return;
1366 /* XXX: This is only a heuristic, as the file may have also
1367 been truncated and written to if st_size >= size
1368 (in which case we ignore new data <= size).
1369 Though in the inotify case it's more likely we'll get
1370 separate events for truncate() and write(). */
1371 if (S_ISREG (fspec->mode) && stats.st_size < fspec->size)
1373 error (0, 0, _("%s: file truncated"), quotef (name));
1374 xlseek (fspec->fd, 0, SEEK_SET, name);
1375 fspec->size = 0;
1377 else if (S_ISREG (fspec->mode) && stats.st_size == fspec->size
1378 && timespec_cmp (fspec->mtime, get_stat_mtime (&stats)) == 0)
1379 return;
1381 bool want_header = print_headers && (fspec != *prev_fspec);
1383 uintmax_t bytes_read = dump_remainder (want_header, name, fspec->fd,
1384 COPY_TO_EOF);
1385 fspec->size += bytes_read;
1387 if (bytes_read)
1389 *prev_fspec = fspec;
1390 if (fflush (stdout) != 0)
1391 die (EXIT_FAILURE, errno, _("write error"));
1395 /* Attempt to tail N_FILES files forever, or until killed.
1396 Check modifications using the inotify events system.
1397 Return false on error, or true to revert to polling. */
1398 static bool
1399 tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
1400 double sleep_interval)
1402 # if TAIL_TEST_SLEEP
1403 /* Delay between open() and inotify_add_watch()
1404 to help trigger different cases. */
1405 xnanosleep (1000000);
1406 # endif
1407 unsigned int max_realloc = 3;
1409 /* Map an inotify watch descriptor to the name of the file it's watching. */
1410 Hash_table *wd_to_name;
1412 bool found_watchable_file = false;
1413 bool tailed_but_unwatchable = false;
1414 bool found_unwatchable_dir = false;
1415 bool no_inotify_resources = false;
1416 bool writer_is_dead = false;
1417 struct File_spec *prev_fspec;
1418 size_t evlen = 0;
1419 char *evbuf;
1420 size_t evbuf_off = 0;
1421 size_t len = 0;
1423 wd_to_name = hash_initialize (n_files, NULL, wd_hasher, wd_comparator, NULL);
1424 if (! wd_to_name)
1425 xalloc_die ();
1427 /* The events mask used with inotify on files (not directories). */
1428 uint32_t inotify_wd_mask = IN_MODIFY;
1429 /* TODO: Perhaps monitor these events in Follow_descriptor mode also,
1430 to tag reported file names with "deleted", "moved" etc. */
1431 if (follow_mode == Follow_name)
1432 inotify_wd_mask |= (IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF);
1434 /* Add an inotify watch for each watched file. If -F is specified then watch
1435 its parent directory too, in this way when they re-appear we can add them
1436 again to the watch list. */
1437 size_t i;
1438 for (i = 0; i < n_files; i++)
1440 if (!f[i].ignore)
1442 size_t fnlen = strlen (f[i].name);
1443 if (evlen < fnlen)
1444 evlen = fnlen;
1446 f[i].wd = -1;
1448 if (follow_mode == Follow_name)
1450 size_t dirlen = dir_len (f[i].name);
1451 char prev = f[i].name[dirlen];
1452 f[i].basename_start = last_component (f[i].name) - f[i].name;
1454 f[i].name[dirlen] = '\0';
1456 /* It's fine to add the same directory more than once.
1457 In that case the same watch descriptor is returned. */
1458 f[i].parent_wd = inotify_add_watch (wd, dirlen ? f[i].name : ".",
1459 (IN_CREATE | IN_DELETE
1460 | IN_MOVED_TO | IN_ATTRIB
1461 | IN_DELETE_SELF));
1463 f[i].name[dirlen] = prev;
1465 if (f[i].parent_wd < 0)
1467 if (errno != ENOSPC) /* suppress confusing error. */
1468 error (0, errno, _("cannot watch parent directory of %s"),
1469 quoteaf (f[i].name));
1470 else
1471 error (0, 0, _("inotify resources exhausted"));
1472 found_unwatchable_dir = true;
1473 /* We revert to polling below. Note invalid uses
1474 of the inotify API will still be diagnosed. */
1475 break;
1479 f[i].wd = inotify_add_watch (wd, f[i].name, inotify_wd_mask);
1481 if (f[i].wd < 0)
1483 if (f[i].fd != -1) /* already tailed. */
1484 tailed_but_unwatchable = true;
1485 if (errno == ENOSPC || errno == ENOMEM)
1487 no_inotify_resources = true;
1488 error (0, 0, _("inotify resources exhausted"));
1489 break;
1491 else if (errno != f[i].errnum)
1492 error (0, errno, _("cannot watch %s"), quoteaf (f[i].name));
1493 continue;
1496 if (hash_insert (wd_to_name, &(f[i])) == NULL)
1497 xalloc_die ();
1499 found_watchable_file = true;
1503 /* Linux kernel 2.6.24 at least has a bug where eventually, ENOSPC is always
1504 returned by inotify_add_watch. In any case we should revert to polling
1505 when there are no inotify resources. Also a specified directory may not
1506 be currently present or accessible, so revert to polling. Also an already
1507 tailed but unwatchable due rename/unlink race, should also revert. */
1508 if (no_inotify_resources || found_unwatchable_dir
1509 || (follow_mode == Follow_descriptor && tailed_but_unwatchable))
1511 hash_free (wd_to_name);
1513 errno = 0;
1514 return true;
1516 if (follow_mode == Follow_descriptor && !found_watchable_file)
1517 return false;
1519 prev_fspec = &(f[n_files - 1]);
1521 /* Check files again. New files or data can be available since last time we
1522 checked and before they are watched by inotify. */
1523 for (i = 0; i < n_files; i++)
1525 if (! f[i].ignore)
1527 /* check for new files. */
1528 if (follow_mode == Follow_name)
1529 recheck (&(f[i]), false);
1530 else if (f[i].fd != -1)
1532 /* If the file was replaced in the small window since we tailed,
1533 then assume the watch is on the wrong item (different to
1534 that we've already produced output for), and so revert to
1535 polling the original descriptor. */
1536 struct stat stats;
1538 if (stat (f[i].name, &stats) == 0
1539 && (f[i].dev != stats.st_dev || f[i].ino != stats.st_ino))
1541 error (0, errno, _("%s was replaced"),
1542 quoteaf (pretty_name (&(f[i]))));
1543 hash_free (wd_to_name);
1545 errno = 0;
1546 return true;
1550 /* check for new data. */
1551 check_fspec (&f[i], &prev_fspec);
1555 evlen += sizeof (struct inotify_event) + 1;
1556 evbuf = xmalloc (evlen);
1558 /* Wait for inotify events and handle them. Events on directories
1559 ensure that watched files can be re-added when following by name.
1560 This loop blocks on the 'safe_read' call until a new event is notified.
1561 But when --pid=P is specified, tail usually waits via the select. */
1562 while (1)
1564 struct File_spec *fspec;
1565 struct inotify_event *ev;
1566 void *void_ev;
1568 /* When following by name without --retry, and the last file has
1569 been unlinked or renamed-away, diagnose it and return. */
1570 if (follow_mode == Follow_name
1571 && ! reopen_inaccessible_files
1572 && hash_get_n_entries (wd_to_name) == 0)
1574 error (0, 0, _("no files remaining"));
1575 return false;
1578 /* When watching a PID, ensure that a read from WD will not block
1579 indefinitely. */
1580 if (pid && (len <= evbuf_off))
1582 if (writer_is_dead)
1583 exit (EXIT_SUCCESS);
1585 writer_is_dead = (kill (pid, 0) != 0 && errno != EPERM);
1587 struct timeval delay; /* how long to wait for file changes. */
1588 if (writer_is_dead)
1589 delay.tv_sec = delay.tv_usec = 0;
1590 else
1592 delay.tv_sec = (time_t) sleep_interval;
1593 delay.tv_usec = 1000000 * (sleep_interval - delay.tv_sec);
1596 fd_set rfd;
1597 FD_ZERO (&rfd);
1598 FD_SET (wd, &rfd);
1600 int file_change = select (wd + 1, &rfd, NULL, NULL, &delay);
1602 if (file_change == 0)
1603 continue;
1604 else if (file_change == -1)
1605 die (EXIT_FAILURE, errno, _("error monitoring inotify event"));
1608 if (len <= evbuf_off)
1610 len = safe_read (wd, evbuf, evlen);
1611 evbuf_off = 0;
1613 /* For kernels prior to 2.6.21, read returns 0 when the buffer
1614 is too small. */
1615 if ((len == 0 || (len == SAFE_READ_ERROR && errno == EINVAL))
1616 && max_realloc--)
1618 len = 0;
1619 evlen *= 2;
1620 evbuf = xrealloc (evbuf, evlen);
1621 continue;
1624 if (len == 0 || len == SAFE_READ_ERROR)
1625 die (EXIT_FAILURE, errno, _("error reading inotify event"));
1628 void_ev = evbuf + evbuf_off;
1629 ev = void_ev;
1630 evbuf_off += sizeof (*ev) + ev->len;
1632 /* If a directory is deleted, IN_DELETE_SELF is emitted
1633 with ev->name of length 0.
1634 We need to catch it, otherwise it would wait forever,
1635 as wd for directory becomes inactive. Revert to polling now. */
1636 if ((ev->mask & IN_DELETE_SELF) && ! ev->len)
1638 for (i = 0; i < n_files; i++)
1640 if (ev->wd == f[i].parent_wd)
1642 hash_free (wd_to_name);
1643 error (0, 0,
1644 _("directory containing watched file was removed"));
1645 errno = 0; /* we've already diagnosed enough errno detail. */
1646 return true;
1651 if (ev->len) /* event on ev->name in watched directory. */
1653 size_t j;
1654 for (j = 0; j < n_files; j++)
1656 /* With N=hundreds of frequently-changing files, this O(N^2)
1657 process might be a problem. FIXME: use a hash table? */
1658 if (f[j].parent_wd == ev->wd
1659 && STREQ (ev->name, f[j].name + f[j].basename_start))
1660 break;
1663 /* It is not a watched file. */
1664 if (j == n_files)
1665 continue;
1667 fspec = &(f[j]);
1669 int new_wd = -1;
1670 bool deleting = !! (ev->mask & IN_DELETE);
1672 if (! deleting)
1674 /* Adding the same inode again will look up any existing wd. */
1675 new_wd = inotify_add_watch (wd, f[j].name, inotify_wd_mask);
1678 if (! deleting && new_wd < 0)
1680 if (errno == ENOSPC || errno == ENOMEM)
1682 error (0, 0, _("inotify resources exhausted"));
1683 hash_free (wd_to_name);
1684 errno = 0;
1685 return true; /* revert to polling. */
1687 else
1689 /* Can get ENOENT for a dangling symlink for example. */
1690 error (0, errno, _("cannot watch %s"), quoteaf (f[j].name));
1692 /* We'll continue below after removing the existing watch. */
1695 /* This will be false if only attributes of file change. */
1696 bool new_watch;
1697 new_watch = (! deleting) && (fspec->wd < 0 || new_wd != fspec->wd);
1699 if (new_watch)
1701 if (0 <= fspec->wd)
1703 inotify_rm_watch (wd, fspec->wd);
1704 hash_delete (wd_to_name, fspec);
1707 fspec->wd = new_wd;
1709 if (new_wd == -1)
1710 continue;
1712 /* If the file was moved then inotify will use the source file wd
1713 for the destination file. Make sure the key is not present in
1714 the table. */
1715 struct File_spec *prev = hash_delete (wd_to_name, fspec);
1716 if (prev && prev != fspec)
1718 if (follow_mode == Follow_name)
1719 recheck (prev, false);
1720 prev->wd = -1;
1721 close_fd (prev->fd, pretty_name (prev));
1724 if (hash_insert (wd_to_name, fspec) == NULL)
1725 xalloc_die ();
1728 if (follow_mode == Follow_name)
1729 recheck (fspec, false);
1731 else
1733 struct File_spec key;
1734 key.wd = ev->wd;
1735 fspec = hash_lookup (wd_to_name, &key);
1738 if (! fspec)
1739 continue;
1741 if (ev->mask & (IN_ATTRIB | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF))
1743 /* Note for IN_MOVE_SELF (the file we're watching has
1744 been clobbered via a rename) we leave the watch
1745 in place since it may still be part of the set
1746 of watched names. */
1747 if (ev->mask & IN_DELETE_SELF)
1749 inotify_rm_watch (wd, fspec->wd);
1750 hash_delete (wd_to_name, fspec);
1753 /* Note we get IN_ATTRIB for unlink() as st_nlink decrements.
1754 The usual path is a close() done in recheck() triggers
1755 an IN_DELETE_SELF event as the inode is removed.
1756 However sometimes open() will succeed as even though
1757 st_nlink is decremented, the dentry (cache) is not updated.
1758 Thus we depend on the IN_DELETE event on the directory
1759 to trigger processing for the removed file. */
1761 recheck (fspec, false);
1763 continue;
1765 check_fspec (fspec, &prev_fspec);
1768 #endif
1770 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
1771 Return true if successful. */
1773 static bool
1774 tail_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
1775 uintmax_t *read_pos)
1777 struct stat stats;
1779 if (fstat (fd, &stats))
1781 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1782 return false;
1785 if (from_start)
1787 if ( ! presume_input_pipe
1788 && S_ISREG (stats.st_mode) && n_bytes <= OFF_T_MAX)
1790 xlseek (fd, n_bytes, SEEK_CUR, pretty_filename);
1791 *read_pos += n_bytes;
1793 else
1795 int t = start_bytes (pretty_filename, fd, n_bytes, read_pos);
1796 if (t)
1797 return t < 0;
1799 n_bytes = COPY_TO_EOF;
1801 else
1803 off_t end_pos = ((! presume_input_pipe && usable_st_size (&stats)
1804 && n_bytes <= OFF_T_MAX)
1805 ? stats.st_size : -1);
1806 if (end_pos <= ST_BLKSIZE (stats))
1807 return pipe_bytes (pretty_filename, fd, n_bytes, read_pos);
1808 off_t current_pos = xlseek (fd, 0, SEEK_CUR, pretty_filename);
1809 if (current_pos < end_pos)
1811 off_t bytes_remaining = end_pos - current_pos;
1813 if (n_bytes < bytes_remaining)
1815 current_pos = end_pos - n_bytes;
1816 xlseek (fd, current_pos, SEEK_SET, pretty_filename);
1819 *read_pos = current_pos;
1822 *read_pos += dump_remainder (false, pretty_filename, fd, n_bytes);
1823 return true;
1826 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
1827 Return true if successful. */
1829 static bool
1830 tail_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
1831 uintmax_t *read_pos)
1833 struct stat stats;
1835 if (fstat (fd, &stats))
1837 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1838 return false;
1841 if (from_start)
1843 int t = start_lines (pretty_filename, fd, n_lines, read_pos);
1844 if (t)
1845 return t < 0;
1846 *read_pos += dump_remainder (false, pretty_filename, fd, COPY_TO_EOF);
1848 else
1850 off_t start_pos = -1;
1851 off_t end_pos;
1853 /* Use file_lines only if FD refers to a regular file for
1854 which lseek (... SEEK_END) works. */
1855 if ( ! presume_input_pipe
1856 && S_ISREG (stats.st_mode)
1857 && (start_pos = lseek (fd, 0, SEEK_CUR)) != -1
1858 && start_pos < (end_pos = lseek (fd, 0, SEEK_END)))
1860 *read_pos = end_pos;
1861 if (end_pos != 0
1862 && ! file_lines (pretty_filename, fd, n_lines,
1863 start_pos, end_pos, read_pos))
1864 return false;
1866 else
1868 /* Under very unlikely circumstances, it is possible to reach
1869 this point after positioning the file pointer to end of file
1870 via the 'lseek (...SEEK_END)' above. In that case, reposition
1871 the file pointer back to start_pos before calling pipe_lines. */
1872 if (start_pos != -1)
1873 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
1875 return pipe_lines (pretty_filename, fd, n_lines, read_pos);
1878 return true;
1881 /* Display the last N_UNITS units of file FILENAME, open for reading
1882 via FD. Set *READ_POS to the position of the input stream pointer.
1883 *READ_POS is usually the number of bytes read and corresponds to an
1884 offset from the beginning of a file. However, it may be larger than
1885 OFF_T_MAX (as for an input pipe), and may also be larger than the
1886 number of bytes read (when an input pointer is initially not at
1887 beginning of file), and may be far greater than the number of bytes
1888 actually read for an input file that is seekable.
1889 Return true if successful. */
1891 static bool
1892 tail (const char *filename, int fd, uintmax_t n_units,
1893 uintmax_t *read_pos)
1895 *read_pos = 0;
1896 if (count_lines)
1897 return tail_lines (filename, fd, n_units, read_pos);
1898 else
1899 return tail_bytes (filename, fd, n_units, read_pos);
1902 /* Display the last N_UNITS units of the file described by F.
1903 Return true if successful. */
1905 static bool
1906 tail_file (struct File_spec *f, uintmax_t n_units)
1908 int fd;
1909 bool ok;
1911 bool is_stdin = (STREQ (f->name, "-"));
1913 if (is_stdin)
1915 have_read_stdin = true;
1916 fd = STDIN_FILENO;
1917 xset_binary_mode (STDIN_FILENO, O_BINARY);
1919 else
1920 fd = open (f->name, O_RDONLY | O_BINARY);
1922 f->tailable = !(reopen_inaccessible_files && fd == -1);
1924 if (fd == -1)
1926 if (forever)
1928 f->fd = -1;
1929 f->errnum = errno;
1930 f->ignore = ! reopen_inaccessible_files;
1931 f->ino = 0;
1932 f->dev = 0;
1934 error (0, errno, _("cannot open %s for reading"),
1935 quoteaf (pretty_name (f)));
1936 ok = false;
1938 else
1940 uintmax_t read_pos;
1942 if (print_headers)
1943 write_header (pretty_name (f));
1944 ok = tail (pretty_name (f), fd, n_units, &read_pos);
1945 if (forever)
1947 struct stat stats;
1949 #if TAIL_TEST_SLEEP
1950 /* Before the tail function provided 'read_pos', there was
1951 a race condition described in the URL below. This sleep
1952 call made the window big enough to exercise the problem. */
1953 xnanosleep (1);
1954 #endif
1955 f->errnum = ok - 1;
1956 if (fstat (fd, &stats) < 0)
1958 ok = false;
1959 f->errnum = errno;
1960 error (0, errno, _("error reading %s"),
1961 quoteaf (pretty_name (f)));
1963 else if (!IS_TAILABLE_FILE_TYPE (stats.st_mode))
1965 ok = false;
1966 f->errnum = -1;
1967 f->tailable = false;
1968 f->ignore = ! reopen_inaccessible_files;
1969 error (0, 0, _("%s: cannot follow end of this type of file%s"),
1970 quotef (pretty_name (f)),
1971 f->ignore ? _("; giving up on this name") : "");
1974 if (!ok)
1976 f->ignore = ! reopen_inaccessible_files;
1977 close_fd (fd, pretty_name (f));
1978 f->fd = -1;
1980 else
1982 /* Note: we must use read_pos here, not stats.st_size,
1983 to avoid a race condition described by Ken Raeburn:
1984 http://lists.gnu.org/archive/html/bug-textutils/2003-05/msg00007.html */
1985 record_open_fd (f, fd, read_pos, &stats, (is_stdin ? -1 : 1));
1986 f->remote = fremote (fd, pretty_name (f));
1989 else
1991 if (!is_stdin && close (fd))
1993 error (0, errno, _("error reading %s"),
1994 quoteaf (pretty_name (f)));
1995 ok = false;
2000 return ok;
2003 /* If obsolete usage is allowed, and the command line arguments are of
2004 the obsolete form and the option string is well-formed, set
2005 *N_UNITS, the globals COUNT_LINES, FOREVER, and FROM_START, and
2006 return true. If the command line arguments are obviously incorrect
2007 (e.g., because obsolete usage is not allowed and the arguments are
2008 incorrect for non-obsolete usage), report an error and exit.
2009 Otherwise, return false and don't modify any parameter or global
2010 variable. */
2012 static bool
2013 parse_obsolete_option (int argc, char * const *argv, uintmax_t *n_units)
2015 const char *p;
2016 const char *n_string;
2017 const char *n_string_end;
2018 int default_count = DEFAULT_N_LINES;
2019 bool t_from_start;
2020 bool t_count_lines = true;
2021 bool t_forever = false;
2023 /* With the obsolete form, there is one option string and at most
2024 one file argument. Watch out for "-" and "--", though. */
2025 if (! (argc == 2
2026 || (argc == 3 && ! (argv[2][0] == '-' && argv[2][1]))
2027 || (3 <= argc && argc <= 4 && STREQ (argv[2], "--"))))
2028 return false;
2030 int posix_ver = posix2_version ();
2031 bool obsolete_usage = posix_ver < 200112;
2032 bool traditional_usage = obsolete_usage || 200809 <= posix_ver;
2033 p = argv[1];
2035 switch (*p++)
2037 default:
2038 return false;
2040 case '+':
2041 /* Leading "+" is a file name in the standard form. */
2042 if (!traditional_usage)
2043 return false;
2045 t_from_start = true;
2046 break;
2048 case '-':
2049 /* In the non-obsolete form, "-" is standard input and "-c"
2050 requires an option-argument. The obsolete multidigit options
2051 are supported as a GNU extension even when conforming to
2052 POSIX 1003.1-2001 or later, so don't complain about them. */
2053 if (!obsolete_usage && !p[p[0] == 'c'])
2054 return false;
2056 t_from_start = false;
2057 break;
2060 n_string = p;
2061 while (ISDIGIT (*p))
2062 p++;
2063 n_string_end = p;
2065 switch (*p)
2067 case 'b': default_count *= 512; /* Fall through. */
2068 case 'c': t_count_lines = false; /* Fall through. */
2069 case 'l': p++; break;
2072 if (*p == 'f')
2074 t_forever = true;
2075 ++p;
2078 if (*p)
2079 return false;
2081 if (n_string == n_string_end)
2082 *n_units = default_count;
2083 else if ((xstrtoumax (n_string, NULL, 10, n_units, "b")
2084 & ~LONGINT_INVALID_SUFFIX_CHAR)
2085 != LONGINT_OK)
2087 die (EXIT_FAILURE, errno, "%s: %s", _("invalid number"),
2088 quote (argv[1]));
2091 /* Set globals. */
2092 from_start = t_from_start;
2093 count_lines = t_count_lines;
2094 forever = t_forever;
2096 return true;
2099 static void
2100 parse_options (int argc, char **argv,
2101 uintmax_t *n_units, enum header_mode *header_mode,
2102 double *sleep_interval)
2104 int c;
2106 while ((c = getopt_long (argc, argv, "c:n:fFqs:vz0123456789",
2107 long_options, NULL))
2108 != -1)
2110 switch (c)
2112 case 'F':
2113 forever = true;
2114 follow_mode = Follow_name;
2115 reopen_inaccessible_files = true;
2116 break;
2118 case 'c':
2119 case 'n':
2120 count_lines = (c == 'n');
2121 if (*optarg == '+')
2122 from_start = true;
2123 else if (*optarg == '-')
2124 ++optarg;
2126 *n_units = xdectoumax (optarg, 0, UINTMAX_MAX, "bkKmMGTPEZY0",
2127 count_lines
2128 ? _("invalid number of lines")
2129 : _("invalid number of bytes"), 0);
2130 break;
2132 case 'f':
2133 case LONG_FOLLOW_OPTION:
2134 forever = true;
2135 if (optarg == NULL)
2136 follow_mode = DEFAULT_FOLLOW_MODE;
2137 else
2138 follow_mode = XARGMATCH ("--follow", optarg,
2139 follow_mode_string, follow_mode_map);
2140 break;
2142 case RETRY_OPTION:
2143 reopen_inaccessible_files = true;
2144 break;
2146 case MAX_UNCHANGED_STATS_OPTION:
2147 /* --max-unchanged-stats=N */
2148 max_n_unchanged_stats_between_opens =
2149 xdectoumax (optarg, 0, UINTMAX_MAX, "",
2150 _("invalid maximum number of unchanged stats between opens"), 0);
2151 break;
2153 case DISABLE_INOTIFY_OPTION:
2154 disable_inotify = true;
2155 break;
2157 case PID_OPTION:
2158 pid = xdectoumax (optarg, 0, PID_T_MAX, "", _("invalid PID"), 0);
2159 break;
2161 case PRESUME_INPUT_PIPE_OPTION:
2162 presume_input_pipe = true;
2163 break;
2165 case 'q':
2166 *header_mode = never;
2167 break;
2169 case 's':
2171 double s;
2172 if (! (xstrtod (optarg, NULL, &s, c_strtod) && 0 <= s))
2173 die (EXIT_FAILURE, 0,
2174 _("invalid number of seconds: %s"), quote (optarg));
2175 *sleep_interval = s;
2177 break;
2179 case 'v':
2180 *header_mode = always;
2181 break;
2183 case 'z':
2184 line_end = '\0';
2185 break;
2187 case_GETOPT_HELP_CHAR;
2189 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
2191 case '0': case '1': case '2': case '3': case '4':
2192 case '5': case '6': case '7': case '8': case '9':
2193 die (EXIT_FAILURE, 0, _("option used in invalid context -- %c"), c);
2195 default:
2196 usage (EXIT_FAILURE);
2200 if (reopen_inaccessible_files)
2202 if (!forever)
2204 reopen_inaccessible_files = false;
2205 error (0, 0, _("warning: --retry ignored; --retry is useful"
2206 " only when following"));
2208 else if (follow_mode == Follow_descriptor)
2209 error (0, 0, _("warning: --retry only effective for the initial open"));
2212 if (pid && !forever)
2213 error (0, 0,
2214 _("warning: PID ignored; --pid=PID is useful only when following"));
2215 else if (pid && kill (pid, 0) != 0 && errno == ENOSYS)
2217 error (0, 0, _("warning: --pid=PID is not supported on this system"));
2218 pid = 0;
2222 /* Mark as '.ignore'd each member of F that corresponds to a
2223 pipe or fifo, and return the number of non-ignored members. */
2224 static size_t
2225 ignore_fifo_and_pipe (struct File_spec *f, size_t n_files)
2227 /* When there is no FILE operand and stdin is a pipe or FIFO
2228 POSIX requires that tail ignore the -f option.
2229 Since we allow multiple FILE operands, we extend that to say: with -f,
2230 ignore any "-" operand that corresponds to a pipe or FIFO. */
2231 size_t n_viable = 0;
2233 size_t i;
2234 for (i = 0; i < n_files; i++)
2236 bool is_a_fifo_or_pipe =
2237 (STREQ (f[i].name, "-")
2238 && !f[i].ignore
2239 && 0 <= f[i].fd
2240 && (S_ISFIFO (f[i].mode)
2241 || (HAVE_FIFO_PIPES != 1 && isapipe (f[i].fd))));
2242 if (is_a_fifo_or_pipe)
2244 f[i].fd = -1;
2245 f[i].ignore = true;
2247 else
2248 ++n_viable;
2251 return n_viable;
2255 main (int argc, char **argv)
2257 enum header_mode header_mode = multiple_files;
2258 bool ok = true;
2259 /* If from_start, the number of items to skip before printing; otherwise,
2260 the number of items at the end of the file to print. Although the type
2261 is signed, the value is never negative. */
2262 uintmax_t n_units = DEFAULT_N_LINES;
2263 size_t n_files;
2264 char **file;
2265 struct File_spec *F;
2266 size_t i;
2267 bool obsolete_option;
2269 /* The number of seconds to sleep between iterations.
2270 During one iteration, every file name or descriptor is checked to
2271 see if it has changed. */
2272 double sleep_interval = 1.0;
2274 initialize_main (&argc, &argv);
2275 set_program_name (argv[0]);
2276 setlocale (LC_ALL, "");
2277 bindtextdomain (PACKAGE, LOCALEDIR);
2278 textdomain (PACKAGE);
2280 atexit (close_stdout);
2282 have_read_stdin = false;
2284 count_lines = true;
2285 forever = from_start = print_headers = false;
2286 line_end = '\n';
2287 obsolete_option = parse_obsolete_option (argc, argv, &n_units);
2288 argc -= obsolete_option;
2289 argv += obsolete_option;
2290 parse_options (argc, argv, &n_units, &header_mode, &sleep_interval);
2292 /* To start printing with item N_UNITS from the start of the file, skip
2293 N_UNITS - 1 items. 'tail -n +0' is actually meaningless, but for Unix
2294 compatibility it's treated the same as 'tail -n +1'. */
2295 if (from_start)
2297 if (n_units)
2298 --n_units;
2301 IF_LINT (assert (0 <= argc));
2303 if (optind < argc)
2305 n_files = argc - optind;
2306 file = argv + optind;
2308 else
2310 static char *dummy_stdin = (char *) "-";
2311 n_files = 1;
2312 file = &dummy_stdin;
2316 bool found_hyphen = false;
2318 for (i = 0; i < n_files; i++)
2319 if (STREQ (file[i], "-"))
2320 found_hyphen = true;
2322 /* When following by name, there must be a name. */
2323 if (found_hyphen && follow_mode == Follow_name)
2324 die (EXIT_FAILURE, 0, _("cannot follow %s by name"), quoteaf ("-"));
2326 /* When following forever, warn if any file is '-'.
2327 This is only a warning, since tail's output (before a failing seek,
2328 and that from any non-stdin files) might still be useful. */
2329 if (forever && found_hyphen && isatty (STDIN_FILENO))
2330 error (0, 0, _("warning: following standard input"
2331 " indefinitely is ineffective"));
2334 /* Don't read anything if we'll never output anything. */
2335 if (! n_units && ! forever && ! from_start)
2336 return EXIT_SUCCESS;
2338 F = xnmalloc (n_files, sizeof *F);
2339 for (i = 0; i < n_files; i++)
2340 F[i].name = file[i];
2342 if (header_mode == always
2343 || (header_mode == multiple_files && n_files > 1))
2344 print_headers = true;
2346 xset_binary_mode (STDOUT_FILENO, O_BINARY);
2348 for (i = 0; i < n_files; i++)
2349 ok &= tail_file (&F[i], n_units);
2351 if (forever && ignore_fifo_and_pipe (F, n_files))
2353 #if HAVE_INOTIFY
2354 /* tailable_stdin() checks if the user specifies stdin via "-",
2355 or implicitly by providing no arguments. If so, we won't use inotify.
2356 Technically, on systems with a working /dev/stdin, we *could*,
2357 but would it be worth it? Verifying that it's a real device
2358 and hooked up to stdin is not trivial, while reverting to
2359 non-inotify-based tail_forever is easy and portable.
2361 any_remote_file() checks if the user has specified any
2362 files that reside on remote file systems. inotify is not used
2363 in this case because it would miss any updates to the file
2364 that were not initiated from the local system.
2366 any_non_remote_file() checks if the user has specified any
2367 files that don't reside on remote file systems. inotify is not used
2368 if there are no open files, as we can't determine if those file
2369 will be on a remote file system.
2371 any_symlinks() checks if the user has specified any symbolic links.
2372 inotify is not used in this case because it returns updated _targets_
2373 which would not match the specified names. If we tried to always
2374 use the target names, then we would miss changes to the symlink itself.
2376 ok is false when one of the files specified could not be opened for
2377 reading. In this case and when following by descriptor,
2378 tail_forever_inotify() cannot be used (in its current implementation).
2380 FIXME: inotify doesn't give any notification when a new
2381 (remote) file or directory is mounted on top a watched file.
2382 When follow_mode == Follow_name we would ideally like to detect that.
2383 Note if there is a change to the original file then we'll
2384 recheck it and follow the new file, or ignore it if the
2385 file has changed to being remote.
2387 FIXME: when using inotify, and a directory for a watched file
2388 is recreated, then we don't recheck any new file when
2389 follow_mode == Follow_name.
2391 FIXME-maybe: inotify has a watch descriptor per inode, and hence with
2392 our current hash implementation will only --follow data for one
2393 of the names when multiple hardlinked files are specified, or
2394 for one name when a name is specified multiple times. */
2395 if (!disable_inotify && (tailable_stdin (F, n_files)
2396 || any_remote_file (F, n_files)
2397 || ! any_non_remote_file (F, n_files)
2398 || any_symlinks (F, n_files)
2399 || (!ok && follow_mode == Follow_descriptor)))
2400 disable_inotify = true;
2402 if (!disable_inotify)
2404 int wd = inotify_init ();
2405 if (0 <= wd)
2407 /* Flush any output from tail_file, now, since
2408 tail_forever_inotify flushes only after writing,
2409 not before reading. */
2410 if (fflush (stdout) != 0)
2411 die (EXIT_FAILURE, errno, _("write error"));
2413 if (! tail_forever_inotify (wd, F, n_files, sleep_interval))
2414 return EXIT_FAILURE;
2416 error (0, errno, _("inotify cannot be used, reverting to polling"));
2418 /* Free resources as this process can be long lived,
2419 and we may have exhausted system resources above. */
2421 for (i = 0; i < n_files; i++)
2423 /* It's OK to remove the same watch multiple times,
2424 ignoring the EINVAL from redundant calls. */
2425 if (F[i].wd != -1)
2426 inotify_rm_watch (wd, F[i].wd);
2427 if (F[i].parent_wd != -1)
2428 inotify_rm_watch (wd, F[i].parent_wd);
2431 #endif
2432 disable_inotify = true;
2433 tail_forever (F, n_files, sleep_interval);
2436 IF_LINT (free (F));
2438 if (have_read_stdin && close (STDIN_FILENO) < 0)
2439 die (EXIT_FAILURE, errno, "-");
2440 return ok ? EXIT_SUCCESS : EXIT_FAILURE;