printf,seq,sleep,tail,timeout: accept current-locale floats
[coreutils.git] / src / tail.c
blobdee827bc8f662746dd0d2de1c95aa2eed8ae763f
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/types.h>
32 #include <signal.h>
33 #ifdef _AIX
34 # include <poll.h>
35 #endif
37 #include "system.h"
38 #include "argmatch.h"
39 #include "cl-strtod.h"
40 #include "die.h"
41 #include "error.h"
42 #include "fcntl--.h"
43 #include "isapipe.h"
44 #include "posixver.h"
45 #include "quote.h"
46 #include "safe-read.h"
47 #include "stat-size.h"
48 #include "stat-time.h"
49 #include "xbinary-io.h"
50 #include "xdectoint.h"
51 #include "xnanosleep.h"
52 #include "xstrtol.h"
53 #include "xstrtod.h"
55 #if HAVE_INOTIFY
56 # include "hash.h"
57 # include <sys/inotify.h>
58 /* 'select' is used by tail_forever_inotify. */
59 # include <sys/select.h>
61 /* inotify needs to know if a file is local. */
62 # include "fs.h"
63 # include "fs-is-local.h"
64 # if HAVE_SYS_STATFS_H
65 # include <sys/statfs.h>
66 # elif HAVE_SYS_VFS_H
67 # include <sys/vfs.h>
68 # endif
69 #endif
71 /* The official name of this program (e.g., no 'g' prefix). */
72 #define PROGRAM_NAME "tail"
74 #define AUTHORS \
75 proper_name ("Paul Rubin"), \
76 proper_name ("David MacKenzie"), \
77 proper_name ("Ian Lance Taylor"), \
78 proper_name ("Jim Meyering")
80 /* Number of items to tail. */
81 #define DEFAULT_N_LINES 10
83 /* Special values for dump_remainder's N_BYTES parameter. */
84 #define COPY_TO_EOF UINTMAX_MAX
85 #define COPY_A_BUFFER (UINTMAX_MAX - 1)
87 /* FIXME: make Follow_name the default? */
88 #define DEFAULT_FOLLOW_MODE Follow_descriptor
90 enum Follow_mode
92 /* Follow the name of each file: if the file is renamed, try to reopen
93 that name and track the end of the new file if/when it's recreated.
94 This is useful for tracking logs that are occasionally rotated. */
95 Follow_name = 1,
97 /* Follow each descriptor obtained upon opening a file.
98 That means we'll continue to follow the end of a file even after
99 it has been renamed or unlinked. */
100 Follow_descriptor = 2
103 /* The types of files for which tail works. */
104 #define IS_TAILABLE_FILE_TYPE(Mode) \
105 (S_ISREG (Mode) || S_ISFIFO (Mode) || S_ISSOCK (Mode) || S_ISCHR (Mode))
107 static char const *const follow_mode_string[] =
109 "descriptor", "name", NULL
112 static enum Follow_mode const follow_mode_map[] =
114 Follow_descriptor, Follow_name,
117 struct File_spec
119 /* The actual file name, or "-" for stdin. */
120 char *name;
122 /* Attributes of the file the last time we checked. */
123 off_t size;
124 struct timespec mtime;
125 dev_t dev;
126 ino_t ino;
127 mode_t mode;
129 /* The specified name initially referred to a directory or some other
130 type for which tail isn't meaningful. Unlike for a permission problem
131 (tailable, below) once this is set, the name is not checked ever again. */
132 bool ignore;
134 /* See the description of fremote. */
135 bool remote;
137 /* A file is tailable if it exists, is readable, and is of type
138 IS_TAILABLE_FILE_TYPE. */
139 bool tailable;
141 /* File descriptor on which the file is open; -1 if it's not open. */
142 int fd;
144 /* The value of errno seen last time we checked this file. */
145 int errnum;
147 /* 1 if O_NONBLOCK is clear, 0 if set, -1 if not known. */
148 int blocking;
150 #if HAVE_INOTIFY
151 /* The watch descriptor used by inotify. */
152 int wd;
154 /* The parent directory watch descriptor. It is used only
155 * when Follow_name is used. */
156 int parent_wd;
158 /* Offset in NAME of the basename part. */
159 size_t basename_start;
160 #endif
162 /* See description of DEFAULT_MAX_N_... below. */
163 uintmax_t n_unchanged_stats;
166 /* Keep trying to open a file even if it is inaccessible when tail starts
167 or if it becomes inaccessible later -- useful only with -f. */
168 static bool reopen_inaccessible_files;
170 /* If true, interpret the numeric argument as the number of lines.
171 Otherwise, interpret it as the number of bytes. */
172 static bool count_lines;
174 /* Whether we follow the name of each file or the file descriptor
175 that is initially associated with each name. */
176 static enum Follow_mode follow_mode = Follow_descriptor;
178 /* If true, read from the ends of all specified files until killed. */
179 static bool forever;
181 /* If true, monitor output so we exit if pipe reader terminates. */
182 static bool monitor_output;
184 /* If true, count from start of file instead of end. */
185 static bool from_start;
187 /* If true, print filename headers. */
188 static bool print_headers;
190 /* Character to split lines by. */
191 static char line_end;
193 /* When to print the filename banners. */
194 enum header_mode
196 multiple_files, always, never
199 /* When tailing a file by name, if there have been this many consecutive
200 iterations for which the file has not changed, then open/fstat
201 the file to determine if that file name is still associated with the
202 same device/inode-number pair as before. This option is meaningful only
203 when following by name. --max-unchanged-stats=N */
204 #define DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS 5
205 static uintmax_t max_n_unchanged_stats_between_opens =
206 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS;
208 /* The process ID of the process (presumably on the current host)
209 that is writing to all followed files. */
210 static pid_t pid;
212 /* True if we have ever read standard input. */
213 static bool have_read_stdin;
215 /* If nonzero, skip the is-regular-file test used to determine whether
216 to use the lseek optimization. Instead, use the more general (and
217 more expensive) code unconditionally. Intended solely for testing. */
218 static bool presume_input_pipe;
220 /* If nonzero then don't use inotify even if available. */
221 static bool disable_inotify;
223 /* For long options that have no equivalent short option, use a
224 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
225 enum
227 RETRY_OPTION = CHAR_MAX + 1,
228 MAX_UNCHANGED_STATS_OPTION,
229 PID_OPTION,
230 PRESUME_INPUT_PIPE_OPTION,
231 LONG_FOLLOW_OPTION,
232 DISABLE_INOTIFY_OPTION
235 static struct option const long_options[] =
237 {"bytes", required_argument, NULL, 'c'},
238 {"follow", optional_argument, NULL, LONG_FOLLOW_OPTION},
239 {"lines", required_argument, NULL, 'n'},
240 {"max-unchanged-stats", required_argument, NULL, MAX_UNCHANGED_STATS_OPTION},
241 {"-disable-inotify", no_argument, NULL,
242 DISABLE_INOTIFY_OPTION}, /* do not document */
243 {"pid", required_argument, NULL, PID_OPTION},
244 {"-presume-input-pipe", no_argument, NULL,
245 PRESUME_INPUT_PIPE_OPTION}, /* do not document */
246 {"quiet", no_argument, NULL, 'q'},
247 {"retry", no_argument, NULL, RETRY_OPTION},
248 {"silent", no_argument, NULL, 'q'},
249 {"sleep-interval", required_argument, NULL, 's'},
250 {"verbose", no_argument, NULL, 'v'},
251 {"zero-terminated", no_argument, NULL, 'z'},
252 {GETOPT_HELP_OPTION_DECL},
253 {GETOPT_VERSION_OPTION_DECL},
254 {NULL, 0, NULL, 0}
257 void
258 usage (int status)
260 if (status != EXIT_SUCCESS)
261 emit_try_help ();
262 else
264 printf (_("\
265 Usage: %s [OPTION]... [FILE]...\n\
267 program_name);
268 printf (_("\
269 Print the last %d lines of each FILE to standard output.\n\
270 With more than one FILE, precede each with a header giving the file name.\n\
271 "), DEFAULT_N_LINES);
273 emit_stdin_note ();
274 emit_mandatory_arg_note ();
276 fputs (_("\
277 -c, --bytes=[+]NUM output the last NUM bytes; or use -c +NUM to\n\
278 output starting with byte NUM of each file\n\
279 "), stdout);
280 fputs (_("\
281 -f, --follow[={name|descriptor}]\n\
282 output appended data as the file grows;\n\
283 an absent option argument means 'descriptor'\n\
284 -F same as --follow=name --retry\n\
285 "), stdout);
286 printf (_("\
287 -n, --lines=[+]NUM output the last NUM lines, instead of the last %d;\n\
288 or use -n +NUM to output starting with line NUM\n\
289 --max-unchanged-stats=N\n\
290 with --follow=name, reopen a FILE which has not\n\
291 changed size after N (default %d) iterations\n\
292 to see if it has been unlinked or renamed\n\
293 (this is the usual case of rotated log files);\n\
294 with inotify, this option is rarely useful\n\
296 DEFAULT_N_LINES,
297 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS
299 fputs (_("\
300 --pid=PID with -f, terminate after process ID, PID dies\n\
301 -q, --quiet, --silent never output headers giving file names\n\
302 --retry keep trying to open a file if it is inaccessible\n\
303 "), stdout);
304 fputs (_("\
305 -s, --sleep-interval=N with -f, sleep for approximately N seconds\n\
306 (default 1.0) between iterations;\n\
307 with inotify and --pid=P, check process P at\n\
308 least once every N seconds\n\
309 -v, --verbose always output headers giving file names\n\
310 "), stdout);
311 fputs (_("\
312 -z, --zero-terminated line delimiter is NUL, not newline\n\
313 "), stdout);
314 fputs (HELP_OPTION_DESCRIPTION, stdout);
315 fputs (VERSION_OPTION_DESCRIPTION, stdout);
316 fputs (_("\
318 NUM may have a multiplier suffix:\n\
319 b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\
320 GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.\n\
321 Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\
323 "), stdout);
324 fputs (_("\
325 With --follow (-f), tail defaults to following the file descriptor, which\n\
326 means that even if a tail'ed file is renamed, tail will continue to track\n\
327 its end. This default behavior is not desirable when you really want to\n\
328 track the actual name of the file, not the file descriptor (e.g., log\n\
329 rotation). Use --follow=name in that case. That causes tail to track the\n\
330 named file in a way that accommodates renaming, removal and creation.\n\
331 "), stdout);
332 emit_ancillary_info (PROGRAM_NAME);
334 exit (status);
337 /* Ensure exit, either with SIGPIPE or EXIT_FAILURE status. */
338 static void ATTRIBUTE_NORETURN
339 die_pipe (void)
341 raise (SIGPIPE);
342 exit (EXIT_FAILURE);
345 /* If the output has gone away, then terminate
346 as we would if we had written to this output. */
347 static void
348 check_output_alive (void)
350 if (! monitor_output)
351 return;
353 #ifdef _AIX
354 /* select on AIX was seen to give a readable event immediately. */
355 struct pollfd pfd;
356 pfd.fd = STDOUT_FILENO;
357 pfd.events = POLLERR;
359 if (poll (&pfd, 1, 0) >= 0 && (pfd.revents & POLLERR))
360 die_pipe ();
361 #else
362 struct timeval delay;
363 delay.tv_sec = delay.tv_usec = 0;
365 fd_set rfd;
366 FD_ZERO (&rfd);
367 FD_SET (STDOUT_FILENO, &rfd);
369 /* readable event on STDOUT is equivalent to POLLERR,
370 and implies an error condition on output like broken pipe. */
371 if (select (STDOUT_FILENO + 1, &rfd, NULL, NULL, &delay) == 1)
372 die_pipe ();
373 #endif
376 static bool
377 valid_file_spec (struct File_spec const *f)
379 /* Exactly one of the following subexpressions must be true. */
380 return ((f->fd == -1) ^ (f->errnum == 0));
383 static char const *
384 pretty_name (struct File_spec const *f)
386 return (STREQ (f->name, "-") ? _("standard input") : f->name);
389 /* Record a file F with descriptor FD, size SIZE, status ST, and
390 blocking status BLOCKING. */
392 static void
393 record_open_fd (struct File_spec *f, int fd,
394 off_t size, struct stat const *st,
395 int blocking)
397 f->fd = fd;
398 f->size = size;
399 f->mtime = get_stat_mtime (st);
400 f->dev = st->st_dev;
401 f->ino = st->st_ino;
402 f->mode = st->st_mode;
403 f->blocking = blocking;
404 f->n_unchanged_stats = 0;
405 f->ignore = false;
408 /* Close the file with descriptor FD and name FILENAME. */
410 static void
411 close_fd (int fd, const char *filename)
413 if (fd != -1 && fd != STDIN_FILENO && close (fd))
415 error (0, errno, _("closing %s (fd=%d)"), quoteaf (filename), fd);
419 static void
420 write_header (const char *pretty_filename)
422 static bool first_file = true;
424 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), pretty_filename);
425 first_file = false;
428 /* Write N_BYTES from BUFFER to stdout.
429 Exit immediately on error with a single diagnostic. */
431 static void
432 xwrite_stdout (char const *buffer, size_t n_bytes)
434 if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) < n_bytes)
436 clearerr (stdout); /* To avoid redundant close_stdout diagnostic. */
437 die (EXIT_FAILURE, errno, _("error writing %s"),
438 quoteaf ("standard output"));
442 /* Read and output N_BYTES of file PRETTY_FILENAME starting at the current
443 position in FD. If N_BYTES is COPY_TO_EOF, then copy until end of file.
444 If N_BYTES is COPY_A_BUFFER, then copy at most one buffer's worth.
445 Return the number of bytes read from the file. */
447 static uintmax_t
448 dump_remainder (bool want_header, const char *pretty_filename, int fd,
449 uintmax_t n_bytes)
451 uintmax_t n_written;
452 uintmax_t n_remaining = n_bytes;
454 n_written = 0;
455 while (1)
457 char buffer[BUFSIZ];
458 size_t n = MIN (n_remaining, BUFSIZ);
459 size_t bytes_read = safe_read (fd, buffer, n);
460 if (bytes_read == SAFE_READ_ERROR)
462 if (errno != EAGAIN)
463 die (EXIT_FAILURE, errno, _("error reading %s"),
464 quoteaf (pretty_filename));
465 break;
467 if (bytes_read == 0)
468 break;
469 if (want_header)
471 write_header (pretty_filename);
472 want_header = false;
474 xwrite_stdout (buffer, bytes_read);
475 n_written += bytes_read;
476 if (n_bytes != COPY_TO_EOF)
478 n_remaining -= bytes_read;
479 if (n_remaining == 0 || n_bytes == COPY_A_BUFFER)
480 break;
484 return n_written;
487 /* Call lseek with the specified arguments, where file descriptor FD
488 corresponds to the file, FILENAME.
489 Give a diagnostic and exit nonzero if lseek fails.
490 Otherwise, return the resulting offset. */
492 static off_t
493 xlseek (int fd, off_t offset, int whence, char const *filename)
495 off_t new_offset = lseek (fd, offset, whence);
496 char buf[INT_BUFSIZE_BOUND (offset)];
497 char *s;
499 if (0 <= new_offset)
500 return new_offset;
502 s = offtostr (offset, buf);
503 switch (whence)
505 case SEEK_SET:
506 error (0, errno, _("%s: cannot seek to offset %s"),
507 quotef (filename), s);
508 break;
509 case SEEK_CUR:
510 error (0, errno, _("%s: cannot seek to relative offset %s"),
511 quotef (filename), s);
512 break;
513 case SEEK_END:
514 error (0, errno, _("%s: cannot seek to end-relative offset %s"),
515 quotef (filename), s);
516 break;
517 default:
518 abort ();
521 exit (EXIT_FAILURE);
524 /* Print the last N_LINES lines from the end of file FD.
525 Go backward through the file, reading 'BUFSIZ' bytes at a time (except
526 probably the first), until we hit the start of the file or have
527 read NUMBER newlines.
528 START_POS is the starting position of the read pointer for the file
529 associated with FD (may be nonzero).
530 END_POS is the file offset of EOF (one larger than offset of last byte).
531 Return true if successful. */
533 static bool
534 file_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
535 off_t start_pos, off_t end_pos, uintmax_t *read_pos)
537 char buffer[BUFSIZ];
538 size_t bytes_read;
539 off_t pos = end_pos;
541 if (n_lines == 0)
542 return true;
544 /* Set 'bytes_read' to the size of the last, probably partial, buffer;
545 0 < 'bytes_read' <= 'BUFSIZ'. */
546 bytes_read = (pos - start_pos) % BUFSIZ;
547 if (bytes_read == 0)
548 bytes_read = BUFSIZ;
549 /* Make 'pos' a multiple of 'BUFSIZ' (0 if the file is short), so that all
550 reads will be on block boundaries, which might increase efficiency. */
551 pos -= bytes_read;
552 xlseek (fd, pos, SEEK_SET, pretty_filename);
553 bytes_read = safe_read (fd, buffer, bytes_read);
554 if (bytes_read == SAFE_READ_ERROR)
556 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
557 return false;
559 *read_pos = pos + bytes_read;
561 /* Count the incomplete line on files that don't end with a newline. */
562 if (bytes_read && buffer[bytes_read - 1] != line_end)
563 --n_lines;
567 /* Scan backward, counting the newlines in this bufferfull. */
569 size_t n = bytes_read;
570 while (n)
572 char const *nl;
573 nl = memrchr (buffer, line_end, n);
574 if (nl == NULL)
575 break;
576 n = nl - buffer;
577 if (n_lines-- == 0)
579 /* If this newline isn't the last character in the buffer,
580 output the part that is after it. */
581 if (n != bytes_read - 1)
582 xwrite_stdout (nl + 1, bytes_read - (n + 1));
583 *read_pos += dump_remainder (false, pretty_filename, fd,
584 end_pos - (pos + bytes_read));
585 return true;
589 /* Not enough newlines in that bufferfull. */
590 if (pos == start_pos)
592 /* Not enough lines in the file; print everything from
593 start_pos to the end. */
594 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
595 *read_pos = start_pos + dump_remainder (false, pretty_filename, fd,
596 end_pos);
597 return true;
599 pos -= BUFSIZ;
600 xlseek (fd, pos, SEEK_SET, pretty_filename);
602 bytes_read = safe_read (fd, buffer, BUFSIZ);
603 if (bytes_read == SAFE_READ_ERROR)
605 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
606 return false;
609 *read_pos = pos + bytes_read;
611 while (bytes_read > 0);
613 return true;
616 /* Print the last N_LINES lines from the end of the standard input,
617 open for reading as pipe FD.
618 Buffer the text as a linked list of LBUFFERs, adding them as needed.
619 Return true if successful. */
621 static bool
622 pipe_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
623 uintmax_t *read_pos)
625 struct linebuffer
627 char buffer[BUFSIZ];
628 size_t nbytes;
629 size_t nlines;
630 struct linebuffer *next;
632 typedef struct linebuffer LBUFFER;
633 LBUFFER *first, *last, *tmp;
634 size_t total_lines = 0; /* Total number of newlines in all buffers. */
635 bool ok = true;
636 size_t n_read; /* Size in bytes of most recent read */
638 first = last = xmalloc (sizeof (LBUFFER));
639 first->nbytes = first->nlines = 0;
640 first->next = NULL;
641 tmp = xmalloc (sizeof (LBUFFER));
643 /* Input is always read into a fresh buffer. */
644 while (1)
646 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
647 if (n_read == 0 || n_read == SAFE_READ_ERROR)
648 break;
649 tmp->nbytes = n_read;
650 *read_pos += n_read;
651 tmp->nlines = 0;
652 tmp->next = NULL;
654 /* Count the number of newlines just read. */
656 char const *buffer_end = tmp->buffer + n_read;
657 char const *p = tmp->buffer;
658 while ((p = memchr (p, line_end, buffer_end - p)))
660 ++p;
661 ++tmp->nlines;
664 total_lines += tmp->nlines;
666 /* If there is enough room in the last buffer read, just append the new
667 one to it. This is because when reading from a pipe, 'n_read' can
668 often be very small. */
669 if (tmp->nbytes + last->nbytes < BUFSIZ)
671 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
672 last->nbytes += tmp->nbytes;
673 last->nlines += tmp->nlines;
675 else
677 /* If there's not enough room, link the new buffer onto the end of
678 the list, then either free up the oldest buffer for the next
679 read if that would leave enough lines, or else malloc a new one.
680 Some compaction mechanism is possible but probably not
681 worthwhile. */
682 last = last->next = tmp;
683 if (total_lines - first->nlines > n_lines)
685 tmp = first;
686 total_lines -= first->nlines;
687 first = first->next;
689 else
690 tmp = xmalloc (sizeof (LBUFFER));
694 free (tmp);
696 if (n_read == SAFE_READ_ERROR)
698 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
699 ok = false;
700 goto free_lbuffers;
703 /* If the file is empty, then bail out. */
704 if (last->nbytes == 0)
705 goto free_lbuffers;
707 /* This prevents a core dump when the pipe contains no newlines. */
708 if (n_lines == 0)
709 goto free_lbuffers;
711 /* Count the incomplete line on files that don't end with a newline. */
712 if (last->buffer[last->nbytes - 1] != line_end)
714 ++last->nlines;
715 ++total_lines;
718 /* Run through the list, printing lines. First, skip over unneeded
719 buffers. */
720 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
721 total_lines -= tmp->nlines;
723 /* Find the correct beginning, then print the rest of the file. */
725 char const *beg = tmp->buffer;
726 char const *buffer_end = tmp->buffer + tmp->nbytes;
727 if (total_lines > n_lines)
729 /* Skip 'total_lines' - 'n_lines' newlines. We made sure that
730 'total_lines' - 'n_lines' <= 'tmp->nlines'. */
731 size_t j;
732 for (j = total_lines - n_lines; j; --j)
734 beg = memchr (beg, line_end, buffer_end - beg);
735 assert (beg);
736 ++beg;
740 xwrite_stdout (beg, buffer_end - beg);
743 for (tmp = tmp->next; tmp; tmp = tmp->next)
744 xwrite_stdout (tmp->buffer, tmp->nbytes);
746 free_lbuffers:
747 while (first)
749 tmp = first->next;
750 free (first);
751 first = tmp;
753 return ok;
756 /* Print the last N_BYTES characters from the end of pipe FD.
757 This is a stripped down version of pipe_lines.
758 Return true if successful. */
760 static bool
761 pipe_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
762 uintmax_t *read_pos)
764 struct charbuffer
766 char buffer[BUFSIZ];
767 size_t nbytes;
768 struct charbuffer *next;
770 typedef struct charbuffer CBUFFER;
771 CBUFFER *first, *last, *tmp;
772 size_t i; /* Index into buffers. */
773 size_t total_bytes = 0; /* Total characters in all buffers. */
774 bool ok = true;
775 size_t n_read;
777 first = last = xmalloc (sizeof (CBUFFER));
778 first->nbytes = 0;
779 first->next = NULL;
780 tmp = xmalloc (sizeof (CBUFFER));
782 /* Input is always read into a fresh buffer. */
783 while (1)
785 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
786 if (n_read == 0 || n_read == SAFE_READ_ERROR)
787 break;
788 *read_pos += n_read;
789 tmp->nbytes = n_read;
790 tmp->next = NULL;
792 total_bytes += tmp->nbytes;
793 /* If there is enough room in the last buffer read, just append the new
794 one to it. This is because when reading from a pipe, 'nbytes' can
795 often be very small. */
796 if (tmp->nbytes + last->nbytes < BUFSIZ)
798 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
799 last->nbytes += tmp->nbytes;
801 else
803 /* If there's not enough room, link the new buffer onto the end of
804 the list, then either free up the oldest buffer for the next
805 read if that would leave enough characters, or else malloc a new
806 one. Some compaction mechanism is possible but probably not
807 worthwhile. */
808 last = last->next = tmp;
809 if (total_bytes - first->nbytes > n_bytes)
811 tmp = first;
812 total_bytes -= first->nbytes;
813 first = first->next;
815 else
817 tmp = xmalloc (sizeof (CBUFFER));
822 free (tmp);
824 if (n_read == SAFE_READ_ERROR)
826 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
827 ok = false;
828 goto free_cbuffers;
831 /* Run through the list, printing characters. First, skip over unneeded
832 buffers. */
833 for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
834 total_bytes -= tmp->nbytes;
836 /* Find the correct beginning, then print the rest of the file.
837 We made sure that 'total_bytes' - 'n_bytes' <= 'tmp->nbytes'. */
838 if (total_bytes > n_bytes)
839 i = total_bytes - n_bytes;
840 else
841 i = 0;
842 xwrite_stdout (&tmp->buffer[i], tmp->nbytes - i);
844 for (tmp = tmp->next; tmp; tmp = tmp->next)
845 xwrite_stdout (tmp->buffer, tmp->nbytes);
847 free_cbuffers:
848 while (first)
850 tmp = first->next;
851 free (first);
852 first = tmp;
854 return ok;
857 /* Skip N_BYTES characters from the start of pipe FD, and print
858 any extra characters that were read beyond that.
859 Return 1 on error, 0 if ok, -1 if EOF. */
861 static int
862 start_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
863 uintmax_t *read_pos)
865 char buffer[BUFSIZ];
867 while (0 < n_bytes)
869 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
870 if (bytes_read == 0)
871 return -1;
872 if (bytes_read == SAFE_READ_ERROR)
874 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
875 return 1;
877 *read_pos += bytes_read;
878 if (bytes_read <= n_bytes)
879 n_bytes -= bytes_read;
880 else
882 size_t n_remaining = bytes_read - n_bytes;
883 if (n_remaining)
884 xwrite_stdout (&buffer[n_bytes], n_remaining);
885 break;
889 return 0;
892 /* Skip N_LINES lines at the start of file or pipe FD, and print
893 any extra characters that were read beyond that.
894 Return 1 on error, 0 if ok, -1 if EOF. */
896 static int
897 start_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
898 uintmax_t *read_pos)
900 if (n_lines == 0)
901 return 0;
903 while (1)
905 char buffer[BUFSIZ];
906 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
907 if (bytes_read == 0) /* EOF */
908 return -1;
909 if (bytes_read == SAFE_READ_ERROR) /* error */
911 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
912 return 1;
915 char *buffer_end = buffer + bytes_read;
917 *read_pos += bytes_read;
919 char *p = buffer;
920 while ((p = memchr (p, line_end, buffer_end - p)))
922 ++p;
923 if (--n_lines == 0)
925 if (p < buffer_end)
926 xwrite_stdout (p, buffer_end - p);
927 return 0;
933 /* Return false when FD is open on a file residing on a local file system.
934 If fstatfs fails, give a diagnostic and return true.
935 If fstatfs cannot be called, return true. */
936 static bool
937 fremote (int fd, const char *name)
939 bool remote = true; /* be conservative (poll by default). */
941 #if HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE && defined __linux__
942 struct statfs buf;
943 int err = fstatfs (fd, &buf);
944 if (err != 0)
946 /* On at least linux-2.6.38, fstatfs fails with ENOSYS when FD
947 is open on a pipe. Treat that like a remote file. */
948 if (errno != ENOSYS)
949 error (0, errno, _("cannot determine location of %s. "
950 "reverting to polling"), quoteaf (name));
952 else
954 switch (is_local_fs_type (buf.f_type))
956 case 0:
957 break;
958 case -1:
959 /* Treat unrecognized file systems as "remote", so caller polls.
960 Note README-release has instructions for syncing the internal
961 list with the latest Linux kernel file system constants. */
962 break;
963 case 1:
964 remote = false;
965 break;
966 default:
967 assert (!"unexpected return value from is_local_fs_type");
970 #endif
972 return remote;
975 /* open/fstat F->name and handle changes. */
976 static void
977 recheck (struct File_spec *f, bool blocking)
979 struct stat new_stats;
980 bool ok = true;
981 bool is_stdin = (STREQ (f->name, "-"));
982 bool was_tailable = f->tailable;
983 int prev_errnum = f->errnum;
984 bool new_file;
985 int fd = (is_stdin
986 ? STDIN_FILENO
987 : open (f->name, O_RDONLY | (blocking ? 0 : O_NONBLOCK)));
989 assert (valid_file_spec (f));
991 /* If the open fails because the file doesn't exist,
992 then mark the file as not tailable. */
993 f->tailable = !(reopen_inaccessible_files && fd == -1);
995 if (! disable_inotify && ! lstat (f->name, &new_stats)
996 && S_ISLNK (new_stats.st_mode))
998 /* Diagnose the edge case where a regular file is changed
999 to a symlink. We avoid inotify with symlinks since
1000 it's awkward to match between symlink name and target. */
1001 ok = false;
1002 f->errnum = -1;
1003 f->ignore = true;
1005 error (0, 0, _("%s has been replaced with an untailable symbolic link"),
1006 quoteaf (pretty_name (f)));
1008 else if (fd == -1 || fstat (fd, &new_stats) < 0)
1010 ok = false;
1011 f->errnum = errno;
1012 if (!f->tailable)
1014 if (was_tailable)
1016 /* FIXME-maybe: detect the case in which the file first becomes
1017 unreadable (perms), and later becomes readable again and can
1018 be seen to be the same file (dev/ino). Otherwise, tail prints
1019 the entire contents of the file when it becomes readable. */
1020 error (0, f->errnum, _("%s has become inaccessible"),
1021 quoteaf (pretty_name (f)));
1023 else
1025 /* say nothing... it's still not tailable */
1028 else if (prev_errnum != errno)
1029 error (0, errno, "%s", quotef (pretty_name (f)));
1031 else if (!IS_TAILABLE_FILE_TYPE (new_stats.st_mode))
1033 ok = false;
1034 f->errnum = -1;
1035 f->tailable = false;
1036 f->ignore = ! (reopen_inaccessible_files && follow_mode == Follow_name);
1037 if (was_tailable || prev_errnum != f->errnum)
1038 error (0, 0, _("%s has been replaced with an untailable file%s"),
1039 quoteaf (pretty_name (f)),
1040 f->ignore ? _("; giving up on this name") : "");
1042 else if ((f->remote = fremote (fd, pretty_name (f))) && ! disable_inotify)
1044 ok = false;
1045 f->errnum = -1;
1046 error (0, 0, _("%s has been replaced with an untailable remote file"),
1047 quoteaf (pretty_name (f)));
1048 f->ignore = true;
1049 f->remote = true;
1051 else
1053 f->errnum = 0;
1056 new_file = false;
1057 if (!ok)
1059 close_fd (fd, pretty_name (f));
1060 close_fd (f->fd, pretty_name (f));
1061 f->fd = -1;
1063 else if (prev_errnum && prev_errnum != ENOENT)
1065 new_file = true;
1066 assert (f->fd == -1);
1067 error (0, 0, _("%s has become accessible"), quoteaf (pretty_name (f)));
1069 else if (f->fd == -1)
1071 /* A new file even when inodes haven't changed as <dev,inode>
1072 pairs can be reused, and we know the file was missing
1073 on the previous iteration. Note this also means the file
1074 is redisplayed in --follow=name mode if renamed away from
1075 and back to a monitored name. */
1076 new_file = true;
1078 error (0, 0,
1079 _("%s has appeared; following new file"),
1080 quoteaf (pretty_name (f)));
1082 else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
1084 /* File has been replaced (e.g., via log rotation) --
1085 tail the new one. */
1086 new_file = true;
1088 error (0, 0,
1089 _("%s has been replaced; following new file"),
1090 quoteaf (pretty_name (f)));
1092 /* Close the old one. */
1093 close_fd (f->fd, pretty_name (f));
1096 else
1098 /* No changes detected, so close new fd. */
1099 close_fd (fd, pretty_name (f));
1102 /* FIXME: When a log is rotated, daemons tend to log to the
1103 old file descriptor until the new file is present and
1104 the daemon is sent a signal. Therefore tail may miss entries
1105 being written to the old file. Perhaps we should keep
1106 the older file open and continue to monitor it until
1107 data is written to a new file. */
1108 if (new_file)
1110 /* Start at the beginning of the file. */
1111 record_open_fd (f, fd, 0, &new_stats, (is_stdin ? -1 : blocking));
1112 xlseek (fd, 0, SEEK_SET, pretty_name (f));
1116 /* Return true if any of the N_FILES files in F are live, i.e., have
1117 open file descriptors, or should be checked again (see --retry).
1118 When following descriptors, checking should only continue when any
1119 of the files is not yet ignored. */
1121 static bool
1122 any_live_files (const struct File_spec *f, size_t n_files)
1124 /* In inotify mode, ignore may be set for files
1125 which may later be replaced with new files.
1126 So always consider files live in -F mode. */
1127 if (reopen_inaccessible_files && follow_mode == Follow_name)
1128 return true;
1130 for (size_t i = 0; i < n_files; i++)
1132 if (0 <= f[i].fd)
1133 return true;
1134 else
1136 if (! f[i].ignore && reopen_inaccessible_files)
1137 return true;
1141 return false;
1144 /* Tail N_FILES files forever, or until killed.
1145 The pertinent information for each file is stored in an entry of F.
1146 Loop over each of them, doing an fstat to see if they have changed size,
1147 and an occasional open/fstat to see if any dev/ino pair has changed.
1148 If none of them have changed size in one iteration, sleep for a
1149 while and try again. Continue until the user interrupts us. */
1151 static void
1152 tail_forever (struct File_spec *f, size_t n_files, double sleep_interval)
1154 /* Use blocking I/O as an optimization, when it's easy. */
1155 bool blocking = (pid == 0 && follow_mode == Follow_descriptor
1156 && n_files == 1 && f[0].fd != -1 && ! S_ISREG (f[0].mode));
1157 size_t last;
1158 bool writer_is_dead = false;
1160 last = n_files - 1;
1162 while (1)
1164 size_t i;
1165 bool any_input = false;
1167 for (i = 0; i < n_files; i++)
1169 int fd;
1170 char const *name;
1171 mode_t mode;
1172 struct stat stats;
1173 uintmax_t bytes_read;
1175 if (f[i].ignore)
1176 continue;
1178 if (f[i].fd < 0)
1180 recheck (&f[i], blocking);
1181 continue;
1184 fd = f[i].fd;
1185 name = pretty_name (&f[i]);
1186 mode = f[i].mode;
1188 if (f[i].blocking != blocking)
1190 int old_flags = fcntl (fd, F_GETFL);
1191 int new_flags = old_flags | (blocking ? 0 : O_NONBLOCK);
1192 if (old_flags < 0
1193 || (new_flags != old_flags
1194 && fcntl (fd, F_SETFL, new_flags) == -1))
1196 /* Don't update f[i].blocking if fcntl fails. */
1197 if (S_ISREG (f[i].mode) && errno == EPERM)
1199 /* This happens when using tail -f on a file with
1200 the append-only attribute. */
1202 else
1203 die (EXIT_FAILURE, errno,
1204 _("%s: cannot change nonblocking mode"),
1205 quotef (name));
1207 else
1208 f[i].blocking = blocking;
1211 if (!f[i].blocking)
1213 if (fstat (fd, &stats) != 0)
1215 f[i].fd = -1;
1216 f[i].errnum = errno;
1217 error (0, errno, "%s", quotef (name));
1218 close (fd); /* ignore failure */
1219 continue;
1222 if (f[i].mode == stats.st_mode
1223 && (! S_ISREG (stats.st_mode) || f[i].size == stats.st_size)
1224 && timespec_cmp (f[i].mtime, get_stat_mtime (&stats)) == 0)
1226 if ((max_n_unchanged_stats_between_opens
1227 <= f[i].n_unchanged_stats++)
1228 && follow_mode == Follow_name)
1230 recheck (&f[i], f[i].blocking);
1231 f[i].n_unchanged_stats = 0;
1233 continue;
1236 /* This file has changed. Print out what we can, and
1237 then keep looping. */
1239 f[i].mtime = get_stat_mtime (&stats);
1240 f[i].mode = stats.st_mode;
1242 /* reset counter */
1243 f[i].n_unchanged_stats = 0;
1245 /* XXX: This is only a heuristic, as the file may have also
1246 been truncated and written to if st_size >= size
1247 (in which case we ignore new data <= size). */
1248 if (S_ISREG (mode) && stats.st_size < f[i].size)
1250 error (0, 0, _("%s: file truncated"), quotef (name));
1251 /* Assume the file was truncated to 0,
1252 and therefore output all "new" data. */
1253 xlseek (fd, 0, SEEK_SET, name);
1254 f[i].size = 0;
1257 if (i != last)
1259 if (print_headers)
1260 write_header (name);
1261 last = i;
1265 /* Don't read more than st_size on networked file systems
1266 because it was seen on glusterfs at least, that st_size
1267 may be smaller than the data read on a _subsequent_ stat call. */
1268 uintmax_t bytes_to_read;
1269 if (f[i].blocking)
1270 bytes_to_read = COPY_A_BUFFER;
1271 else if (S_ISREG (mode) && f[i].remote)
1272 bytes_to_read = stats.st_size - f[i].size;
1273 else
1274 bytes_to_read = COPY_TO_EOF;
1276 bytes_read = dump_remainder (false, name, fd, bytes_to_read);
1278 any_input |= (bytes_read != 0);
1279 f[i].size += bytes_read;
1282 if (! any_live_files (f, n_files))
1284 error (0, 0, _("no files remaining"));
1285 break;
1288 if ((!any_input || blocking) && fflush (stdout) != 0)
1289 die (EXIT_FAILURE, errno, _("write error"));
1291 check_output_alive ();
1293 /* If nothing was read, sleep and/or check for dead writers. */
1294 if (!any_input)
1296 if (writer_is_dead)
1297 break;
1299 /* Once the writer is dead, read the files once more to
1300 avoid a race condition. */
1301 writer_is_dead = (pid != 0
1302 && kill (pid, 0) != 0
1303 /* Handle the case in which you cannot send a
1304 signal to the writer, so kill fails and sets
1305 errno to EPERM. */
1306 && errno != EPERM);
1308 if (!writer_is_dead && xnanosleep (sleep_interval))
1309 die (EXIT_FAILURE, errno, _("cannot read realtime clock"));
1315 #if HAVE_INOTIFY
1317 /* Return true if any of the N_FILES files in F is remote, i.e., has
1318 an open file descriptor and is on a network file system. */
1320 static bool
1321 any_remote_file (const struct File_spec *f, size_t n_files)
1323 for (size_t i = 0; i < n_files; i++)
1324 if (0 <= f[i].fd && f[i].remote)
1325 return true;
1326 return false;
1329 /* Return true if any of the N_FILES files in F is non remote, i.e., has
1330 an open file descriptor and is not on a network file system. */
1332 static bool
1333 any_non_remote_file (const struct File_spec *f, size_t n_files)
1335 for (size_t i = 0; i < n_files; i++)
1336 if (0 <= f[i].fd && ! f[i].remote)
1337 return true;
1338 return false;
1341 /* Return true if any of the N_FILES files in F is a symlink.
1342 Note we don't worry about the edge case where "-" exists,
1343 since that will have the same consequences for inotify,
1344 which is the only context this function is currently used. */
1346 static bool
1347 any_symlinks (const struct File_spec *f, size_t n_files)
1349 struct stat st;
1350 for (size_t i = 0; i < n_files; i++)
1351 if (lstat (f[i].name, &st) == 0 && S_ISLNK (st.st_mode))
1352 return true;
1353 return false;
1356 /* Return true if any of the N_FILES files in F is not
1357 a regular file or fifo. This is used to avoid adding inotify
1358 watches on a device file for example, which inotify
1359 will accept, but not give any events for. */
1361 static bool
1362 any_non_regular_fifo (const struct File_spec *f, size_t n_files)
1364 for (size_t i = 0; i < n_files; i++)
1365 if (0 <= f[i].fd && ! S_ISREG (f[i].mode) && ! S_ISFIFO (f[i].mode))
1366 return true;
1367 return false;
1370 /* Return true if any of the N_FILES files in F represents
1371 stdin and is tailable. */
1373 static bool
1374 tailable_stdin (const struct File_spec *f, size_t n_files)
1376 for (size_t i = 0; i < n_files; i++)
1377 if (!f[i].ignore && STREQ (f[i].name, "-"))
1378 return true;
1379 return false;
1382 static size_t
1383 wd_hasher (const void *entry, size_t tabsize)
1385 const struct File_spec *spec = entry;
1386 return spec->wd % tabsize;
1389 static bool
1390 wd_comparator (const void *e1, const void *e2)
1392 const struct File_spec *spec1 = e1;
1393 const struct File_spec *spec2 = e2;
1394 return spec1->wd == spec2->wd;
1397 /* Output (new) data for FSPEC->fd.
1398 PREV_FSPEC records the last File_spec for which we output. */
1399 static void
1400 check_fspec (struct File_spec *fspec, struct File_spec **prev_fspec)
1402 struct stat stats;
1403 char const *name;
1405 if (fspec->fd == -1)
1406 return;
1408 name = pretty_name (fspec);
1410 if (fstat (fspec->fd, &stats) != 0)
1412 fspec->errnum = errno;
1413 close_fd (fspec->fd, name);
1414 fspec->fd = -1;
1415 return;
1418 /* XXX: This is only a heuristic, as the file may have also
1419 been truncated and written to if st_size >= size
1420 (in which case we ignore new data <= size).
1421 Though in the inotify case it's more likely we'll get
1422 separate events for truncate() and write(). */
1423 if (S_ISREG (fspec->mode) && stats.st_size < fspec->size)
1425 error (0, 0, _("%s: file truncated"), quotef (name));
1426 xlseek (fspec->fd, 0, SEEK_SET, name);
1427 fspec->size = 0;
1429 else if (S_ISREG (fspec->mode) && stats.st_size == fspec->size
1430 && timespec_cmp (fspec->mtime, get_stat_mtime (&stats)) == 0)
1431 return;
1433 bool want_header = print_headers && (fspec != *prev_fspec);
1435 uintmax_t bytes_read = dump_remainder (want_header, name, fspec->fd,
1436 COPY_TO_EOF);
1437 fspec->size += bytes_read;
1439 if (bytes_read)
1441 *prev_fspec = fspec;
1442 if (fflush (stdout) != 0)
1443 die (EXIT_FAILURE, errno, _("write error"));
1447 /* Attempt to tail N_FILES files forever, or until killed.
1448 Check modifications using the inotify events system.
1449 Return false on error, or true to revert to polling. */
1450 static bool
1451 tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
1452 double sleep_interval)
1454 # if TAIL_TEST_SLEEP
1455 /* Delay between open() and inotify_add_watch()
1456 to help trigger different cases. */
1457 xnanosleep (1000000);
1458 # endif
1459 unsigned int max_realloc = 3;
1461 /* Map an inotify watch descriptor to the name of the file it's watching. */
1462 Hash_table *wd_to_name;
1464 bool found_watchable_file = false;
1465 bool tailed_but_unwatchable = false;
1466 bool found_unwatchable_dir = false;
1467 bool no_inotify_resources = false;
1468 bool writer_is_dead = false;
1469 struct File_spec *prev_fspec;
1470 size_t evlen = 0;
1471 char *evbuf;
1472 size_t evbuf_off = 0;
1473 size_t len = 0;
1475 wd_to_name = hash_initialize (n_files, NULL, wd_hasher, wd_comparator, NULL);
1476 if (! wd_to_name)
1477 xalloc_die ();
1479 /* The events mask used with inotify on files (not directories). */
1480 uint32_t inotify_wd_mask = IN_MODIFY;
1481 /* TODO: Perhaps monitor these events in Follow_descriptor mode also,
1482 to tag reported file names with "deleted", "moved" etc. */
1483 if (follow_mode == Follow_name)
1484 inotify_wd_mask |= (IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF);
1486 /* Add an inotify watch for each watched file. If -F is specified then watch
1487 its parent directory too, in this way when they re-appear we can add them
1488 again to the watch list. */
1489 size_t i;
1490 for (i = 0; i < n_files; i++)
1492 if (!f[i].ignore)
1494 size_t fnlen = strlen (f[i].name);
1495 if (evlen < fnlen)
1496 evlen = fnlen;
1498 f[i].wd = -1;
1500 if (follow_mode == Follow_name)
1502 size_t dirlen = dir_len (f[i].name);
1503 char prev = f[i].name[dirlen];
1504 f[i].basename_start = last_component (f[i].name) - f[i].name;
1506 f[i].name[dirlen] = '\0';
1508 /* It's fine to add the same directory more than once.
1509 In that case the same watch descriptor is returned. */
1510 f[i].parent_wd = inotify_add_watch (wd, dirlen ? f[i].name : ".",
1511 (IN_CREATE | IN_DELETE
1512 | IN_MOVED_TO | IN_ATTRIB
1513 | IN_DELETE_SELF));
1515 f[i].name[dirlen] = prev;
1517 if (f[i].parent_wd < 0)
1519 if (errno != ENOSPC) /* suppress confusing error. */
1520 error (0, errno, _("cannot watch parent directory of %s"),
1521 quoteaf (f[i].name));
1522 else
1523 error (0, 0, _("inotify resources exhausted"));
1524 found_unwatchable_dir = true;
1525 /* We revert to polling below. Note invalid uses
1526 of the inotify API will still be diagnosed. */
1527 break;
1531 f[i].wd = inotify_add_watch (wd, f[i].name, inotify_wd_mask);
1533 if (f[i].wd < 0)
1535 if (f[i].fd != -1) /* already tailed. */
1536 tailed_but_unwatchable = true;
1537 if (errno == ENOSPC || errno == ENOMEM)
1539 no_inotify_resources = true;
1540 error (0, 0, _("inotify resources exhausted"));
1541 break;
1543 else if (errno != f[i].errnum)
1544 error (0, errno, _("cannot watch %s"), quoteaf (f[i].name));
1545 continue;
1548 if (hash_insert (wd_to_name, &(f[i])) == NULL)
1549 xalloc_die ();
1551 found_watchable_file = true;
1555 /* Linux kernel 2.6.24 at least has a bug where eventually, ENOSPC is always
1556 returned by inotify_add_watch. In any case we should revert to polling
1557 when there are no inotify resources. Also a specified directory may not
1558 be currently present or accessible, so revert to polling. Also an already
1559 tailed but unwatchable due rename/unlink race, should also revert. */
1560 if (no_inotify_resources || found_unwatchable_dir
1561 || (follow_mode == Follow_descriptor && tailed_but_unwatchable))
1563 hash_free (wd_to_name);
1565 errno = 0;
1566 return true;
1568 if (follow_mode == Follow_descriptor && !found_watchable_file)
1569 return false;
1571 prev_fspec = &(f[n_files - 1]);
1573 /* Check files again. New files or data can be available since last time we
1574 checked and before they are watched by inotify. */
1575 for (i = 0; i < n_files; i++)
1577 if (! f[i].ignore)
1579 /* check for new files. */
1580 if (follow_mode == Follow_name)
1581 recheck (&(f[i]), false);
1582 else if (f[i].fd != -1)
1584 /* If the file was replaced in the small window since we tailed,
1585 then assume the watch is on the wrong item (different to
1586 that we've already produced output for), and so revert to
1587 polling the original descriptor. */
1588 struct stat stats;
1590 if (stat (f[i].name, &stats) == 0
1591 && (f[i].dev != stats.st_dev || f[i].ino != stats.st_ino))
1593 error (0, errno, _("%s was replaced"),
1594 quoteaf (pretty_name (&(f[i]))));
1595 hash_free (wd_to_name);
1597 errno = 0;
1598 return true;
1602 /* check for new data. */
1603 check_fspec (&f[i], &prev_fspec);
1607 evlen += sizeof (struct inotify_event) + 1;
1608 evbuf = xmalloc (evlen);
1610 /* Wait for inotify events and handle them. Events on directories
1611 ensure that watched files can be re-added when following by name.
1612 This loop blocks on the 'safe_read' call until a new event is notified.
1613 But when --pid=P is specified, tail usually waits via the select. */
1614 while (1)
1616 struct File_spec *fspec;
1617 struct inotify_event *ev;
1618 void *void_ev;
1620 /* When following by name without --retry, and the last file has
1621 been unlinked or renamed-away, diagnose it and return. */
1622 if (follow_mode == Follow_name
1623 && ! reopen_inaccessible_files
1624 && hash_get_n_entries (wd_to_name) == 0)
1626 error (0, 0, _("no files remaining"));
1627 return false;
1630 /* When watching a PID, ensure that a read from WD will not block
1631 indefinitely. */
1632 while (len <= evbuf_off)
1634 struct timeval delay; /* how long to wait for file changes. */
1636 if (pid)
1638 if (writer_is_dead)
1639 exit (EXIT_SUCCESS);
1641 writer_is_dead = (kill (pid, 0) != 0 && errno != EPERM);
1643 if (writer_is_dead)
1644 delay.tv_sec = delay.tv_usec = 0;
1645 else
1647 delay.tv_sec = (time_t) sleep_interval;
1648 delay.tv_usec = 1000000 * (sleep_interval - delay.tv_sec);
1652 fd_set rfd;
1653 FD_ZERO (&rfd);
1654 FD_SET (wd, &rfd);
1655 if (monitor_output)
1656 FD_SET (STDOUT_FILENO, &rfd);
1658 int file_change = select (MAX (wd, STDOUT_FILENO) + 1,
1659 &rfd, NULL, NULL, pid ? &delay: NULL);
1661 if (file_change == 0)
1662 continue;
1663 else if (file_change == -1)
1664 die (EXIT_FAILURE, errno,
1665 _("error waiting for inotify and output events"));
1666 else if (FD_ISSET (STDOUT_FILENO, &rfd))
1668 /* readable event on STDOUT is equivalent to POLLERR,
1669 and implies an error on output like broken pipe. */
1670 die_pipe ();
1672 else
1673 break;
1676 if (len <= evbuf_off)
1678 len = safe_read (wd, evbuf, evlen);
1679 evbuf_off = 0;
1681 /* For kernels prior to 2.6.21, read returns 0 when the buffer
1682 is too small. */
1683 if ((len == 0 || (len == SAFE_READ_ERROR && errno == EINVAL))
1684 && max_realloc--)
1686 len = 0;
1687 evlen *= 2;
1688 evbuf = xrealloc (evbuf, evlen);
1689 continue;
1692 if (len == 0 || len == SAFE_READ_ERROR)
1693 die (EXIT_FAILURE, errno, _("error reading inotify event"));
1696 void_ev = evbuf + evbuf_off;
1697 ev = void_ev;
1698 evbuf_off += sizeof (*ev) + ev->len;
1700 /* If a directory is deleted, IN_DELETE_SELF is emitted
1701 with ev->name of length 0.
1702 We need to catch it, otherwise it would wait forever,
1703 as wd for directory becomes inactive. Revert to polling now. */
1704 if ((ev->mask & IN_DELETE_SELF) && ! ev->len)
1706 for (i = 0; i < n_files; i++)
1708 if (ev->wd == f[i].parent_wd)
1710 hash_free (wd_to_name);
1711 error (0, 0,
1712 _("directory containing watched file was removed"));
1713 errno = 0; /* we've already diagnosed enough errno detail. */
1714 return true;
1719 if (ev->len) /* event on ev->name in watched directory. */
1721 size_t j;
1722 for (j = 0; j < n_files; j++)
1724 /* With N=hundreds of frequently-changing files, this O(N^2)
1725 process might be a problem. FIXME: use a hash table? */
1726 if (f[j].parent_wd == ev->wd
1727 && STREQ (ev->name, f[j].name + f[j].basename_start))
1728 break;
1731 /* It is not a watched file. */
1732 if (j == n_files)
1733 continue;
1735 fspec = &(f[j]);
1737 int new_wd = -1;
1738 bool deleting = !! (ev->mask & IN_DELETE);
1740 if (! deleting)
1742 /* Adding the same inode again will look up any existing wd. */
1743 new_wd = inotify_add_watch (wd, f[j].name, inotify_wd_mask);
1746 if (! deleting && new_wd < 0)
1748 if (errno == ENOSPC || errno == ENOMEM)
1750 error (0, 0, _("inotify resources exhausted"));
1751 hash_free (wd_to_name);
1752 errno = 0;
1753 return true; /* revert to polling. */
1755 else
1757 /* Can get ENOENT for a dangling symlink for example. */
1758 error (0, errno, _("cannot watch %s"), quoteaf (f[j].name));
1760 /* We'll continue below after removing the existing watch. */
1763 /* This will be false if only attributes of file change. */
1764 bool new_watch;
1765 new_watch = (! deleting) && (fspec->wd < 0 || new_wd != fspec->wd);
1767 if (new_watch)
1769 if (0 <= fspec->wd)
1771 inotify_rm_watch (wd, fspec->wd);
1772 hash_delete (wd_to_name, fspec);
1775 fspec->wd = new_wd;
1777 if (new_wd == -1)
1778 continue;
1780 /* If the file was moved then inotify will use the source file wd
1781 for the destination file. Make sure the key is not present in
1782 the table. */
1783 struct File_spec *prev = hash_delete (wd_to_name, fspec);
1784 if (prev && prev != fspec)
1786 if (follow_mode == Follow_name)
1787 recheck (prev, false);
1788 prev->wd = -1;
1789 close_fd (prev->fd, pretty_name (prev));
1792 if (hash_insert (wd_to_name, fspec) == NULL)
1793 xalloc_die ();
1796 if (follow_mode == Follow_name)
1797 recheck (fspec, false);
1799 else
1801 struct File_spec key;
1802 key.wd = ev->wd;
1803 fspec = hash_lookup (wd_to_name, &key);
1806 if (! fspec)
1807 continue;
1809 if (ev->mask & (IN_ATTRIB | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF))
1811 /* Note for IN_MOVE_SELF (the file we're watching has
1812 been clobbered via a rename) we leave the watch
1813 in place since it may still be part of the set
1814 of watched names. */
1815 if (ev->mask & IN_DELETE_SELF)
1817 inotify_rm_watch (wd, fspec->wd);
1818 hash_delete (wd_to_name, fspec);
1821 /* Note we get IN_ATTRIB for unlink() as st_nlink decrements.
1822 The usual path is a close() done in recheck() triggers
1823 an IN_DELETE_SELF event as the inode is removed.
1824 However sometimes open() will succeed as even though
1825 st_nlink is decremented, the dentry (cache) is not updated.
1826 Thus we depend on the IN_DELETE event on the directory
1827 to trigger processing for the removed file. */
1829 recheck (fspec, false);
1831 continue;
1833 check_fspec (fspec, &prev_fspec);
1836 #endif
1838 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
1839 Return true if successful. */
1841 static bool
1842 tail_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
1843 uintmax_t *read_pos)
1845 struct stat stats;
1847 if (fstat (fd, &stats))
1849 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1850 return false;
1853 if (from_start)
1855 if (! presume_input_pipe && n_bytes <= OFF_T_MAX
1856 && ((S_ISREG (stats.st_mode)
1857 && xlseek (fd, n_bytes, SEEK_CUR, pretty_filename) >= 0)
1858 || lseek (fd, n_bytes, SEEK_CUR) != -1))
1859 *read_pos += n_bytes;
1860 else
1862 int t = start_bytes (pretty_filename, fd, n_bytes, read_pos);
1863 if (t)
1864 return t < 0;
1866 n_bytes = COPY_TO_EOF;
1868 else
1870 off_t end_pos = -1;
1871 off_t current_pos = -1;
1873 if (! presume_input_pipe && n_bytes <= OFF_T_MAX)
1875 if (usable_st_size (&stats))
1876 end_pos = stats.st_size;
1877 else if ((current_pos = lseek (fd, -n_bytes, SEEK_END)) != -1)
1878 end_pos = current_pos + n_bytes;
1880 if (end_pos <= (off_t) ST_BLKSIZE (stats))
1881 return pipe_bytes (pretty_filename, fd, n_bytes, read_pos);
1882 if (current_pos == -1)
1883 current_pos = xlseek (fd, 0, SEEK_CUR, pretty_filename);
1884 if (current_pos < end_pos)
1886 off_t bytes_remaining = end_pos - current_pos;
1888 if (n_bytes < bytes_remaining)
1890 current_pos = end_pos - n_bytes;
1891 xlseek (fd, current_pos, SEEK_SET, pretty_filename);
1894 *read_pos = current_pos;
1897 *read_pos += dump_remainder (false, pretty_filename, fd, n_bytes);
1898 return true;
1901 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
1902 Return true if successful. */
1904 static bool
1905 tail_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
1906 uintmax_t *read_pos)
1908 struct stat stats;
1910 if (fstat (fd, &stats))
1912 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1913 return false;
1916 if (from_start)
1918 int t = start_lines (pretty_filename, fd, n_lines, read_pos);
1919 if (t)
1920 return t < 0;
1921 *read_pos += dump_remainder (false, pretty_filename, fd, COPY_TO_EOF);
1923 else
1925 off_t start_pos = -1;
1926 off_t end_pos;
1928 /* Use file_lines only if FD refers to a regular file for
1929 which lseek (... SEEK_END) works. */
1930 if ( ! presume_input_pipe
1931 && S_ISREG (stats.st_mode)
1932 && (start_pos = lseek (fd, 0, SEEK_CUR)) != -1
1933 && start_pos < (end_pos = lseek (fd, 0, SEEK_END)))
1935 *read_pos = end_pos;
1936 if (end_pos != 0
1937 && ! file_lines (pretty_filename, fd, n_lines,
1938 start_pos, end_pos, read_pos))
1939 return false;
1941 else
1943 /* Under very unlikely circumstances, it is possible to reach
1944 this point after positioning the file pointer to end of file
1945 via the 'lseek (...SEEK_END)' above. In that case, reposition
1946 the file pointer back to start_pos before calling pipe_lines. */
1947 if (start_pos != -1)
1948 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
1950 return pipe_lines (pretty_filename, fd, n_lines, read_pos);
1953 return true;
1956 /* Display the last N_UNITS units of file FILENAME, open for reading
1957 via FD. Set *READ_POS to the position of the input stream pointer.
1958 *READ_POS is usually the number of bytes read and corresponds to an
1959 offset from the beginning of a file. However, it may be larger than
1960 OFF_T_MAX (as for an input pipe), and may also be larger than the
1961 number of bytes read (when an input pointer is initially not at
1962 beginning of file), and may be far greater than the number of bytes
1963 actually read for an input file that is seekable.
1964 Return true if successful. */
1966 static bool
1967 tail (const char *filename, int fd, uintmax_t n_units,
1968 uintmax_t *read_pos)
1970 *read_pos = 0;
1971 if (count_lines)
1972 return tail_lines (filename, fd, n_units, read_pos);
1973 else
1974 return tail_bytes (filename, fd, n_units, read_pos);
1977 /* Display the last N_UNITS units of the file described by F.
1978 Return true if successful. */
1980 static bool
1981 tail_file (struct File_spec *f, uintmax_t n_units)
1983 int fd;
1984 bool ok;
1986 bool is_stdin = (STREQ (f->name, "-"));
1988 if (is_stdin)
1990 have_read_stdin = true;
1991 fd = STDIN_FILENO;
1992 xset_binary_mode (STDIN_FILENO, O_BINARY);
1994 else
1995 fd = open (f->name, O_RDONLY | O_BINARY);
1997 f->tailable = !(reopen_inaccessible_files && fd == -1);
1999 if (fd == -1)
2001 if (forever)
2003 f->fd = -1;
2004 f->errnum = errno;
2005 f->ignore = ! reopen_inaccessible_files;
2006 f->ino = 0;
2007 f->dev = 0;
2009 error (0, errno, _("cannot open %s for reading"),
2010 quoteaf (pretty_name (f)));
2011 ok = false;
2013 else
2015 uintmax_t read_pos;
2017 if (print_headers)
2018 write_header (pretty_name (f));
2019 ok = tail (pretty_name (f), fd, n_units, &read_pos);
2020 if (forever)
2022 struct stat stats;
2024 #if TAIL_TEST_SLEEP
2025 /* Before the tail function provided 'read_pos', there was
2026 a race condition described in the URL below. This sleep
2027 call made the window big enough to exercise the problem. */
2028 xnanosleep (1);
2029 #endif
2030 f->errnum = ok - 1;
2031 if (fstat (fd, &stats) < 0)
2033 ok = false;
2034 f->errnum = errno;
2035 error (0, errno, _("error reading %s"),
2036 quoteaf (pretty_name (f)));
2038 else if (!IS_TAILABLE_FILE_TYPE (stats.st_mode))
2040 ok = false;
2041 f->errnum = -1;
2042 f->tailable = false;
2043 f->ignore = ! reopen_inaccessible_files;
2044 error (0, 0, _("%s: cannot follow end of this type of file%s"),
2045 quotef (pretty_name (f)),
2046 f->ignore ? _("; giving up on this name") : "");
2049 if (!ok)
2051 f->ignore = ! reopen_inaccessible_files;
2052 close_fd (fd, pretty_name (f));
2053 f->fd = -1;
2055 else
2057 /* Note: we must use read_pos here, not stats.st_size,
2058 to avoid a race condition described by Ken Raeburn:
2059 https://lists.gnu.org/r/bug-textutils/2003-05/msg00007.html */
2060 record_open_fd (f, fd, read_pos, &stats, (is_stdin ? -1 : 1));
2061 f->remote = fremote (fd, pretty_name (f));
2064 else
2066 if (!is_stdin && close (fd))
2068 error (0, errno, _("error reading %s"),
2069 quoteaf (pretty_name (f)));
2070 ok = false;
2075 return ok;
2078 /* If obsolete usage is allowed, and the command line arguments are of
2079 the obsolete form and the option string is well-formed, set
2080 *N_UNITS, the globals COUNT_LINES, FOREVER, and FROM_START, and
2081 return true. If the command line arguments are obviously incorrect
2082 (e.g., because obsolete usage is not allowed and the arguments are
2083 incorrect for non-obsolete usage), report an error and exit.
2084 Otherwise, return false and don't modify any parameter or global
2085 variable. */
2087 static bool
2088 parse_obsolete_option (int argc, char * const *argv, uintmax_t *n_units)
2090 const char *p;
2091 const char *n_string;
2092 const char *n_string_end;
2093 int default_count = DEFAULT_N_LINES;
2094 bool t_from_start;
2095 bool t_count_lines = true;
2096 bool t_forever = false;
2098 /* With the obsolete form, there is one option string and at most
2099 one file argument. Watch out for "-" and "--", though. */
2100 if (! (argc == 2
2101 || (argc == 3 && ! (argv[2][0] == '-' && argv[2][1]))
2102 || (3 <= argc && argc <= 4 && STREQ (argv[2], "--"))))
2103 return false;
2105 int posix_ver = posix2_version ();
2106 bool obsolete_usage = posix_ver < 200112;
2107 bool traditional_usage = obsolete_usage || 200809 <= posix_ver;
2108 p = argv[1];
2110 switch (*p++)
2112 default:
2113 return false;
2115 case '+':
2116 /* Leading "+" is a file name in the standard form. */
2117 if (!traditional_usage)
2118 return false;
2120 t_from_start = true;
2121 break;
2123 case '-':
2124 /* In the non-obsolete form, "-" is standard input and "-c"
2125 requires an option-argument. The obsolete multidigit options
2126 are supported as a GNU extension even when conforming to
2127 POSIX 1003.1-2001 or later, so don't complain about them. */
2128 if (!obsolete_usage && !p[p[0] == 'c'])
2129 return false;
2131 t_from_start = false;
2132 break;
2135 n_string = p;
2136 while (ISDIGIT (*p))
2137 p++;
2138 n_string_end = p;
2140 switch (*p)
2142 case 'b': default_count *= 512; FALLTHROUGH;
2143 case 'c': t_count_lines = false; FALLTHROUGH;
2144 case 'l': p++; break;
2147 if (*p == 'f')
2149 t_forever = true;
2150 ++p;
2153 if (*p)
2154 return false;
2156 if (n_string == n_string_end)
2157 *n_units = default_count;
2158 else if ((xstrtoumax (n_string, NULL, 10, n_units, "b")
2159 & ~LONGINT_INVALID_SUFFIX_CHAR)
2160 != LONGINT_OK)
2162 die (EXIT_FAILURE, errno, "%s: %s", _("invalid number"),
2163 quote (argv[1]));
2166 /* Set globals. */
2167 from_start = t_from_start;
2168 count_lines = t_count_lines;
2169 forever = t_forever;
2171 return true;
2174 static void
2175 parse_options (int argc, char **argv,
2176 uintmax_t *n_units, enum header_mode *header_mode,
2177 double *sleep_interval)
2179 int c;
2181 while ((c = getopt_long (argc, argv, "c:n:fFqs:vz0123456789",
2182 long_options, NULL))
2183 != -1)
2185 switch (c)
2187 case 'F':
2188 forever = true;
2189 follow_mode = Follow_name;
2190 reopen_inaccessible_files = true;
2191 break;
2193 case 'c':
2194 case 'n':
2195 count_lines = (c == 'n');
2196 if (*optarg == '+')
2197 from_start = true;
2198 else if (*optarg == '-')
2199 ++optarg;
2201 *n_units = xdectoumax (optarg, 0, UINTMAX_MAX, "bkKmMGTPEZY0",
2202 count_lines
2203 ? _("invalid number of lines")
2204 : _("invalid number of bytes"), 0);
2205 break;
2207 case 'f':
2208 case LONG_FOLLOW_OPTION:
2209 forever = true;
2210 if (optarg == NULL)
2211 follow_mode = DEFAULT_FOLLOW_MODE;
2212 else
2213 follow_mode = XARGMATCH ("--follow", optarg,
2214 follow_mode_string, follow_mode_map);
2215 break;
2217 case RETRY_OPTION:
2218 reopen_inaccessible_files = true;
2219 break;
2221 case MAX_UNCHANGED_STATS_OPTION:
2222 /* --max-unchanged-stats=N */
2223 max_n_unchanged_stats_between_opens =
2224 xdectoumax (optarg, 0, UINTMAX_MAX, "",
2225 _("invalid maximum number of unchanged stats between opens"), 0);
2226 break;
2228 case DISABLE_INOTIFY_OPTION:
2229 disable_inotify = true;
2230 break;
2232 case PID_OPTION:
2233 pid = xdectoumax (optarg, 0, PID_T_MAX, "", _("invalid PID"), 0);
2234 break;
2236 case PRESUME_INPUT_PIPE_OPTION:
2237 presume_input_pipe = true;
2238 break;
2240 case 'q':
2241 *header_mode = never;
2242 break;
2244 case 's':
2246 double s;
2247 if (! (xstrtod (optarg, NULL, &s, cl_strtod) && 0 <= s))
2248 die (EXIT_FAILURE, 0,
2249 _("invalid number of seconds: %s"), quote (optarg));
2250 *sleep_interval = s;
2252 break;
2254 case 'v':
2255 *header_mode = always;
2256 break;
2258 case 'z':
2259 line_end = '\0';
2260 break;
2262 case_GETOPT_HELP_CHAR;
2264 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
2266 case '0': case '1': case '2': case '3': case '4':
2267 case '5': case '6': case '7': case '8': case '9':
2268 die (EXIT_FAILURE, 0, _("option used in invalid context -- %c"), c);
2270 default:
2271 usage (EXIT_FAILURE);
2275 if (reopen_inaccessible_files)
2277 if (!forever)
2279 reopen_inaccessible_files = false;
2280 error (0, 0, _("warning: --retry ignored; --retry is useful"
2281 " only when following"));
2283 else if (follow_mode == Follow_descriptor)
2284 error (0, 0, _("warning: --retry only effective for the initial open"));
2287 if (pid && !forever)
2288 error (0, 0,
2289 _("warning: PID ignored; --pid=PID is useful only when following"));
2290 else if (pid && kill (pid, 0) != 0 && errno == ENOSYS)
2292 error (0, 0, _("warning: --pid=PID is not supported on this system"));
2293 pid = 0;
2297 /* Mark as '.ignore'd each member of F that corresponds to a
2298 pipe or fifo, and return the number of non-ignored members. */
2299 static size_t
2300 ignore_fifo_and_pipe (struct File_spec *f, size_t n_files)
2302 /* When there is no FILE operand and stdin is a pipe or FIFO
2303 POSIX requires that tail ignore the -f option.
2304 Since we allow multiple FILE operands, we extend that to say: with -f,
2305 ignore any "-" operand that corresponds to a pipe or FIFO. */
2306 size_t n_viable = 0;
2308 for (size_t i = 0; i < n_files; i++)
2310 bool is_a_fifo_or_pipe =
2311 (STREQ (f[i].name, "-")
2312 && !f[i].ignore
2313 && 0 <= f[i].fd
2314 && (S_ISFIFO (f[i].mode)
2315 || (HAVE_FIFO_PIPES != 1 && isapipe (f[i].fd))));
2316 if (is_a_fifo_or_pipe)
2318 f[i].fd = -1;
2319 f[i].ignore = true;
2321 else
2322 ++n_viable;
2325 return n_viable;
2329 main (int argc, char **argv)
2331 enum header_mode header_mode = multiple_files;
2332 bool ok = true;
2333 /* If from_start, the number of items to skip before printing; otherwise,
2334 the number of items at the end of the file to print. Although the type
2335 is signed, the value is never negative. */
2336 uintmax_t n_units = DEFAULT_N_LINES;
2337 size_t n_files;
2338 char **file;
2339 struct File_spec *F;
2340 size_t i;
2341 bool obsolete_option;
2343 /* The number of seconds to sleep between iterations.
2344 During one iteration, every file name or descriptor is checked to
2345 see if it has changed. */
2346 double sleep_interval = 1.0;
2348 initialize_main (&argc, &argv);
2349 set_program_name (argv[0]);
2350 setlocale (LC_ALL, "");
2351 bindtextdomain (PACKAGE, LOCALEDIR);
2352 textdomain (PACKAGE);
2354 atexit (close_stdout);
2356 have_read_stdin = false;
2358 count_lines = true;
2359 forever = from_start = print_headers = false;
2360 line_end = '\n';
2361 obsolete_option = parse_obsolete_option (argc, argv, &n_units);
2362 argc -= obsolete_option;
2363 argv += obsolete_option;
2364 parse_options (argc, argv, &n_units, &header_mode, &sleep_interval);
2366 /* To start printing with item N_UNITS from the start of the file, skip
2367 N_UNITS - 1 items. 'tail -n +0' is actually meaningless, but for Unix
2368 compatibility it's treated the same as 'tail -n +1'. */
2369 if (from_start)
2371 if (n_units)
2372 --n_units;
2375 IF_LINT (assert (0 <= argc));
2377 if (optind < argc)
2379 n_files = argc - optind;
2380 file = argv + optind;
2382 else
2384 static char *dummy_stdin = (char *) "-";
2385 n_files = 1;
2386 file = &dummy_stdin;
2390 bool found_hyphen = false;
2392 for (i = 0; i < n_files; i++)
2393 if (STREQ (file[i], "-"))
2394 found_hyphen = true;
2396 /* When following by name, there must be a name. */
2397 if (found_hyphen && follow_mode == Follow_name)
2398 die (EXIT_FAILURE, 0, _("cannot follow %s by name"), quoteaf ("-"));
2400 /* When following forever, and not using simple blocking, warn if
2401 any file is '-' as the stats() used to check for input are ineffective.
2402 This is only a warning, since tail's output (before a failing seek,
2403 and that from any non-stdin files) might still be useful. */
2404 if (forever && found_hyphen)
2406 struct stat in_stat;
2407 bool blocking_stdin;
2408 blocking_stdin = (pid == 0 && follow_mode == Follow_descriptor
2409 && n_files == 1 && ! fstat (STDIN_FILENO, &in_stat)
2410 && ! S_ISREG (in_stat.st_mode));
2412 if (! blocking_stdin && isatty (STDIN_FILENO))
2413 error (0, 0, _("warning: following standard input"
2414 " indefinitely is ineffective"));
2418 /* Don't read anything if we'll never output anything. */
2419 if (! n_units && ! forever && ! from_start)
2420 return EXIT_SUCCESS;
2422 F = xnmalloc (n_files, sizeof *F);
2423 for (i = 0; i < n_files; i++)
2424 F[i].name = file[i];
2426 if (header_mode == always
2427 || (header_mode == multiple_files && n_files > 1))
2428 print_headers = true;
2430 xset_binary_mode (STDOUT_FILENO, O_BINARY);
2432 for (i = 0; i < n_files; i++)
2433 ok &= tail_file (&F[i], n_units);
2435 if (forever && ignore_fifo_and_pipe (F, n_files))
2437 /* If stdout is a fifo or pipe, then monitor it
2438 so that we exit if the reader goes away.
2439 Note select() on a regular file is always readable. */
2440 struct stat out_stat;
2441 if (fstat (STDOUT_FILENO, &out_stat) < 0)
2442 die (EXIT_FAILURE, errno, _("standard output"));
2443 monitor_output = (S_ISFIFO (out_stat.st_mode)
2444 || (HAVE_FIFO_PIPES != 1 && isapipe (STDOUT_FILENO)));
2446 #if HAVE_INOTIFY
2447 /* tailable_stdin() checks if the user specifies stdin via "-",
2448 or implicitly by providing no arguments. If so, we won't use inotify.
2449 Technically, on systems with a working /dev/stdin, we *could*,
2450 but would it be worth it? Verifying that it's a real device
2451 and hooked up to stdin is not trivial, while reverting to
2452 non-inotify-based tail_forever is easy and portable.
2454 any_remote_file() checks if the user has specified any
2455 files that reside on remote file systems. inotify is not used
2456 in this case because it would miss any updates to the file
2457 that were not initiated from the local system.
2459 any_non_remote_file() checks if the user has specified any
2460 files that don't reside on remote file systems. inotify is not used
2461 if there are no open files, as we can't determine if those file
2462 will be on a remote file system.
2464 any_symlinks() checks if the user has specified any symbolic links.
2465 inotify is not used in this case because it returns updated _targets_
2466 which would not match the specified names. If we tried to always
2467 use the target names, then we would miss changes to the symlink itself.
2469 ok is false when one of the files specified could not be opened for
2470 reading. In this case and when following by descriptor,
2471 tail_forever_inotify() cannot be used (in its current implementation).
2473 FIXME: inotify doesn't give any notification when a new
2474 (remote) file or directory is mounted on top a watched file.
2475 When follow_mode == Follow_name we would ideally like to detect that.
2476 Note if there is a change to the original file then we'll
2477 recheck it and follow the new file, or ignore it if the
2478 file has changed to being remote.
2480 FIXME: when using inotify, and a directory for a watched file
2481 is recreated, then we don't recheck any new file when
2482 follow_mode == Follow_name.
2484 FIXME-maybe: inotify has a watch descriptor per inode, and hence with
2485 our current hash implementation will only --follow data for one
2486 of the names when multiple hardlinked files are specified, or
2487 for one name when a name is specified multiple times. */
2488 if (!disable_inotify && (tailable_stdin (F, n_files)
2489 || any_remote_file (F, n_files)
2490 || ! any_non_remote_file (F, n_files)
2491 || any_symlinks (F, n_files)
2492 || any_non_regular_fifo (F, n_files)
2493 || (!ok && follow_mode == Follow_descriptor)))
2494 disable_inotify = true;
2496 if (!disable_inotify)
2498 int wd = inotify_init ();
2499 if (0 <= wd)
2501 /* Flush any output from tail_file, now, since
2502 tail_forever_inotify flushes only after writing,
2503 not before reading. */
2504 if (fflush (stdout) != 0)
2505 die (EXIT_FAILURE, errno, _("write error"));
2507 if (! tail_forever_inotify (wd, F, n_files, sleep_interval))
2508 return EXIT_FAILURE;
2510 error (0, errno, _("inotify cannot be used, reverting to polling"));
2512 /* Free resources as this process can be long lived,
2513 and we may have exhausted system resources above. */
2515 for (i = 0; i < n_files; i++)
2517 /* It's OK to remove the same watch multiple times,
2518 ignoring the EINVAL from redundant calls. */
2519 if (F[i].wd != -1)
2520 inotify_rm_watch (wd, F[i].wd);
2521 if (F[i].parent_wd != -1)
2522 inotify_rm_watch (wd, F[i].parent_wd);
2525 #endif
2526 disable_inotify = true;
2527 tail_forever (F, n_files, sleep_interval);
2530 IF_LINT (free (F));
2532 if (have_read_stdin && close (STDIN_FILENO) < 0)
2533 die (EXIT_FAILURE, errno, "-");
2534 return ok ? EXIT_SUCCESS : EXIT_FAILURE;