1 /* tail -- output the last part of file(s)
2 Copyright (C) 1989-2023 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
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.
20
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>. */
25
26 #include <config.h>
27
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <sys/types.h>
31 #include <signal.h>
32
33 #include "system.h"
34 #include "argmatch.h"
35 #include "assure.h"
36 #include "cl-strtod.h"
37 #include "fcntl--.h"
38 #include "iopoll.h"
39 #include "isapipe.h"
40 #include "posixver.h"
41 #include "quote.h"
42 #include "safe-read.h"
43 #include "stat-size.h"
44 #include "stat-time.h"
45 #include "xbinary-io.h"
46 #include "xdectoint.h"
47 #include "xnanosleep.h"
48 #include "xstrtol.h"
49 #include "xstrtod.h"
50
51 #if HAVE_INOTIFY
52 # include "hash.h"
53 # include <poll.h>
54 # include <sys/inotify.h>
55 #endif
56
57 /* Linux can optimize the handling of local files. */
58 #if defined __linux__ || defined __ANDROID__
59 # include "fs.h"
60 # include "fs-is-local.h"
61 # if HAVE_SYS_STATFS_H
62 # include <sys/statfs.h>
63 # elif HAVE_SYS_VFS_H
64 # include <sys/vfs.h>
65 # endif
66 #endif
67
68 /* The official name of this program (e.g., no 'g' prefix). */
69 #define PROGRAM_NAME "tail"
70
71 #define AUTHORS \
72 proper_name ("Paul Rubin"), \
73 proper_name ("David MacKenzie"), \
74 proper_name ("Ian Lance Taylor"), \
75 proper_name ("Jim Meyering")
76
77 /* Number of items to tail. */
78 #define DEFAULT_N_LINES 10
79
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)
83
84 /* FIXME: make Follow_name the default? */
85 #define DEFAULT_FOLLOW_MODE Follow_descriptor
86
87 enum Follow_mode
88 {
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,
93
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
98 };
99
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))
103
104 static char const *const follow_mode_string[] =
105 {
106 "descriptor", "name", nullptr
107 };
108
109 static enum Follow_mode const follow_mode_map[] =
110 {
111 Follow_descriptor, Follow_name,
112 };
113
114 struct File_spec
115 {
116 /* The actual file name, or "-" for stdin. */
117 char *name;
118
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;
125
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;
130
131 /* See the description of fremote. */
132 bool remote;
133
134 /* A file is tailable if it exists, is readable, and is of type
135 IS_TAILABLE_FILE_TYPE. */
136 bool tailable;
137
138 /* File descriptor on which the file is open; -1 if it's not open. */
139 int fd;
140
141 /* The value of errno seen last time we checked this file. */
142 int errnum;
143
144 /* 1 if O_NONBLOCK is clear, 0 if set, -1 if not known. */
145 int blocking;
146
147 #if HAVE_INOTIFY
148 /* The watch descriptor used by inotify. */
149 int wd;
150
151 /* The parent directory watch descriptor. It is used only
152 * when Follow_name is used. */
153 int parent_wd;
154
155 /* Offset in NAME of the basename part. */
156 size_t basename_start;
157 #endif
158
159 /* See description of DEFAULT_MAX_N_... below. */
160 uintmax_t n_unchanged_stats;
161 };
162
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;
166
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;
170
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;
174
175 /* If true, read from the ends of all specified files until killed. */
176 static bool forever;
177
178 /* If true, monitor output so we exit if pipe reader terminates. */
179 static bool monitor_output;
180
181 /* If true, count from start of file instead of end. */
182 static bool from_start;
183
184 /* If true, print filename headers. */
185 static bool print_headers;
186
187 /* Character to split lines by. */
188 static char line_end;
189
190 /* When to print the filename banners. */
191 enum header_mode
192 {
193 multiple_files, always, never
194 };
195
196 /* When tailing a file by name, if there have been this many consecutive
197 iterations for which the file has not changed, then open/fstat
198 the file to determine if that file name is still associated with the
199 same device/inode-number pair as before. This option is meaningful only
200 when following by name. --max-unchanged-stats=N */
201 #define DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS 5
202 static uintmax_t max_n_unchanged_stats_between_opens =
203 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS;
204
205 /* The process IDs of the processes to watch (those writing the followed
206 files, or perhaps other processes the user cares about). */
207 static int nbpids = 0;
208 static pid_t * pids = nullptr;
209 static idx_t pids_alloc;
210
211 /* True if we have ever read standard input. */
212 static bool have_read_stdin;
213
214 /* If nonzero, skip the is-regular-file test used to determine whether
215 to use the lseek optimization. Instead, use the more general (and
216 more expensive) code unconditionally. Intended solely for testing. */
217 static bool presume_input_pipe;
218
219 /* If nonzero then don't use inotify even if available. */
220 static bool disable_inotify;
221
222 /* For long options that have no equivalent short option, use a
223 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
224 enum
225 {
226 RETRY_OPTION = CHAR_MAX + 1,
227 MAX_UNCHANGED_STATS_OPTION,
228 PID_OPTION,
229 PRESUME_INPUT_PIPE_OPTION,
230 LONG_FOLLOW_OPTION,
231 DISABLE_INOTIFY_OPTION
232 };
233
234 static struct option const long_options[] =
235 {
236 {"bytes", required_argument, nullptr, 'c'},
237 {"follow", optional_argument, nullptr, LONG_FOLLOW_OPTION},
238 {"lines", required_argument, nullptr, 'n'},
239 {"max-unchanged-stats", required_argument, nullptr,
240 MAX_UNCHANGED_STATS_OPTION},
241 {"-disable-inotify", no_argument, nullptr,
242 DISABLE_INOTIFY_OPTION}, /* do not document */
243 {"pid", required_argument, nullptr, PID_OPTION},
244 {"-presume-input-pipe", no_argument, nullptr,
245 PRESUME_INPUT_PIPE_OPTION}, /* do not document */
246 {"quiet", no_argument, nullptr, 'q'},
247 {"retry", no_argument, nullptr, RETRY_OPTION},
248 {"silent", no_argument, nullptr, 'q'},
249 {"sleep-interval", required_argument, nullptr, 's'},
250 {"verbose", no_argument, nullptr, 'v'},
251 {"zero-terminated", no_argument, nullptr, 'z'},
252 {GETOPT_HELP_OPTION_DECL},
253 {GETOPT_VERSION_OPTION_DECL},
254 {nullptr, 0, nullptr, 0}
255 };
256
257 void
usage(int status)258 usage (int status)
259 {
260 if (status != EXIT_SUCCESS)
261 emit_try_help ();
262 else
263 {
264 printf (_("\
265 Usage: %s [OPTION]... [FILE]...\n\
266 "),
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);
272
273 emit_stdin_note ();
274 emit_mandatory_arg_note ();
275
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 skip NUM-1 lines at the start\n\
289 "),
290 DEFAULT_N_LINES
291 );
292 printf (_("\
293 --max-unchanged-stats=N\n\
294 with --follow=name, reopen a FILE which has not\n\
295 changed size after N (default %d) iterations\n\
296 to see if it has been unlinked or renamed\n\
297 (this is the usual case of rotated log files);\n\
298 with inotify, this option is rarely useful\n\
299 "),
300 DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS
301 );
302 fputs (_("\
303 --pid=PID with -f, terminate after process ID, PID dies;\n\
304 can be repeated to watch multiple processes\n\
305 -q, --quiet, --silent never output headers giving file names\n\
306 --retry keep trying to open a file if it is inaccessible\n\
307 "), stdout);
308 fputs (_("\
309 -s, --sleep-interval=N with -f, sleep for approximately N seconds\n\
310 (default 1.0) between iterations;\n\
311 with inotify and --pid=P, check process P at\n\
312 least once every N seconds\n\
313 -v, --verbose always output headers giving file names\n\
314 "), stdout);
315 fputs (_("\
316 -z, --zero-terminated line delimiter is NUL, not newline\n\
317 "), stdout);
318 fputs (HELP_OPTION_DESCRIPTION, stdout);
319 fputs (VERSION_OPTION_DESCRIPTION, stdout);
320 fputs (_("\
321 \n\
322 NUM may have a multiplier suffix:\n\
323 b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\
324 GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y, R, Q.\n\
325 Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\
326 \n\
327 "), stdout);
328 fputs (_("\
329 With --follow (-f), tail defaults to following the file descriptor, which\n\
330 means that even if a tail'ed file is renamed, tail will continue to track\n\
331 its end. This default behavior is not desirable when you really want to\n\
332 track the actual name of the file, not the file descriptor (e.g., log\n\
333 rotation). Use --follow=name in that case. That causes tail to track the\n\
334 named file in a way that accommodates renaming, removal and creation.\n\
335 "), stdout);
336 emit_ancillary_info (PROGRAM_NAME);
337 }
338 exit (status);
339 }
340
341 /* Ensure exit, either with SIGPIPE or EXIT_FAILURE status. */
342 static void
die_pipe(void)343 die_pipe (void)
344 {
345 raise (SIGPIPE);
346 exit (EXIT_FAILURE);
347 }
348
349 /* If the output has gone away, then terminate
350 as we would if we had written to this output. */
351 static void
check_output_alive(void)352 check_output_alive (void)
353 {
354 if (! monitor_output)
355 return;
356
357 if (iopoll (-1, STDOUT_FILENO, false) == IOPOLL_BROKEN_OUTPUT)
358 die_pipe ();
359 }
360
361 MAYBE_UNUSED static bool
valid_file_spec(struct File_spec const * f)362 valid_file_spec (struct File_spec const *f)
363 {
364 /* Exactly one of the following subexpressions must be true. */
365 return ((f->fd == -1) ^ (f->errnum == 0));
366 }
367
368 static char const *
pretty_name(struct File_spec const * f)369 pretty_name (struct File_spec const *f)
370 {
371 return (STREQ (f->name, "-") ? _("standard input") : f->name);
372 }
373
374 /* Record a file F with descriptor FD, size SIZE, status ST, and
375 blocking status BLOCKING. */
376
377 static void
record_open_fd(struct File_spec * f,int fd,off_t size,struct stat const * st,int blocking)378 record_open_fd (struct File_spec *f, int fd,
379 off_t size, struct stat const *st,
380 int blocking)
381 {
382 f->fd = fd;
383 f->size = size;
384 f->mtime = get_stat_mtime (st);
385 f->dev = st->st_dev;
386 f->ino = st->st_ino;
387 f->mode = st->st_mode;
388 f->blocking = blocking;
389 f->n_unchanged_stats = 0;
390 f->ignore = false;
391 }
392
393 /* Close the file with descriptor FD and name FILENAME. */
394
395 static void
close_fd(int fd,char const * filename)396 close_fd (int fd, char const *filename)
397 {
398 if (fd != -1 && fd != STDIN_FILENO && close (fd))
399 {
400 error (0, errno, _("closing %s (fd=%d)"), quoteaf (filename), fd);
401 }
402 }
403
404 static void
write_header(char const * pretty_filename)405 write_header (char const *pretty_filename)
406 {
407 static bool first_file = true;
408
409 printf ("%s==> %s <==\n", (first_file ? "" : "\n"), pretty_filename);
410 first_file = false;
411 }
412
413 /* Write N_BYTES from BUFFER to stdout.
414 Exit immediately on error with a single diagnostic. */
415
416 static void
xwrite_stdout(char const * buffer,size_t n_bytes)417 xwrite_stdout (char const *buffer, size_t n_bytes)
418 {
419 if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) < n_bytes)
420 {
421 clearerr (stdout); /* To avoid redundant close_stdout diagnostic. */
422 error (EXIT_FAILURE, errno, _("error writing %s"),
423 quoteaf ("standard output"));
424 }
425 }
426
427 /* Read and output N_BYTES of file PRETTY_FILENAME starting at the current
428 position in FD. If N_BYTES is COPY_TO_EOF, then copy until end of file.
429 If N_BYTES is COPY_A_BUFFER, then copy at most one buffer's worth.
430 Return the number of bytes read from the file. */
431
432 static uintmax_t
dump_remainder(bool want_header,char const * pretty_filename,int fd,uintmax_t n_bytes)433 dump_remainder (bool want_header, char const *pretty_filename, int fd,
434 uintmax_t n_bytes)
435 {
436 uintmax_t n_written;
437 uintmax_t n_remaining = n_bytes;
438
439 n_written = 0;
440 while (true)
441 {
442 char buffer[BUFSIZ];
443 size_t n = MIN (n_remaining, BUFSIZ);
444 size_t bytes_read = safe_read (fd, buffer, n);
445 if (bytes_read == SAFE_READ_ERROR)
446 {
447 if (errno != EAGAIN)
448 error (EXIT_FAILURE, errno, _("error reading %s"),
449 quoteaf (pretty_filename));
450 break;
451 }
452 if (bytes_read == 0)
453 break;
454 if (want_header)
455 {
456 write_header (pretty_filename);
457 want_header = false;
458 }
459 xwrite_stdout (buffer, bytes_read);
460 n_written += bytes_read;
461 if (n_bytes != COPY_TO_EOF)
462 {
463 n_remaining -= bytes_read;
464 if (n_remaining == 0 || n_bytes == COPY_A_BUFFER)
465 break;
466 }
467 }
468
469 return n_written;
470 }
471
472 /* Call lseek with the specified arguments, where file descriptor FD
473 corresponds to the file, FILENAME.
474 Give a diagnostic and exit nonzero if lseek fails.
475 Otherwise, return the resulting offset. */
476
477 static off_t
xlseek(int fd,off_t offset,int whence,char const * filename)478 xlseek (int fd, off_t offset, int whence, char const *filename)
479 {
480 off_t new_offset = lseek (fd, offset, whence);
481 char buf[INT_BUFSIZE_BOUND (offset)];
482 char *s;
483
484 if (0 <= new_offset)
485 return new_offset;
486
487 s = offtostr (offset, buf);
488 switch (whence)
489 {
490 case SEEK_SET:
491 error (EXIT_FAILURE, errno, _("%s: cannot seek to offset %s"),
492 quotef (filename), s);
493 break;
494 case SEEK_CUR:
495 error (EXIT_FAILURE, errno, _("%s: cannot seek to relative offset %s"),
496 quotef (filename), s);
497 break;
498 case SEEK_END:
499 error (EXIT_FAILURE, errno,
500 _("%s: cannot seek to end-relative offset %s"),
501 quotef (filename), s);
502 break;
503 default:
504 unreachable ();
505 }
506 }
507
508 /* Print the last N_LINES lines from the end of file FD.
509 Go backward through the file, reading 'BUFSIZ' bytes at a time (except
510 probably the first), until we hit the start of the file or have
511 read NUMBER newlines.
512 START_POS is the starting position of the read pointer for the file
513 associated with FD (may be nonzero).
514 END_POS is the file offset of EOF (one larger than offset of last byte).
515 Return true if successful. */
516
517 static bool
file_lines(char const * pretty_filename,int fd,uintmax_t n_lines,off_t start_pos,off_t end_pos,uintmax_t * read_pos)518 file_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
519 off_t start_pos, off_t end_pos, uintmax_t *read_pos)
520 {
521 char buffer[BUFSIZ];
522 size_t bytes_read;
523 off_t pos = end_pos;
524
525 if (n_lines == 0)
526 return true;
527
528 /* Set 'bytes_read' to the size of the last, probably partial, buffer;
529 0 < 'bytes_read' <= 'BUFSIZ'. */
530 bytes_read = (pos - start_pos) % BUFSIZ;
531 if (bytes_read == 0)
532 bytes_read = BUFSIZ;
533 /* Make 'pos' a multiple of 'BUFSIZ' (0 if the file is short), so that all
534 reads will be on block boundaries, which might increase efficiency. */
535 pos -= bytes_read;
536 xlseek (fd, pos, SEEK_SET, pretty_filename);
537 bytes_read = safe_read (fd, buffer, bytes_read);
538 if (bytes_read == SAFE_READ_ERROR)
539 {
540 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
541 return false;
542 }
543 *read_pos = pos + bytes_read;
544
545 /* Count the incomplete line on files that don't end with a newline. */
546 if (bytes_read && buffer[bytes_read - 1] != line_end)
547 --n_lines;
548
549 do
550 {
551 /* Scan backward, counting the newlines in this bufferfull. */
552
553 size_t n = bytes_read;
554 while (n)
555 {
556 char const *nl;
557 nl = memrchr (buffer, line_end, n);
558 if (nl == nullptr)
559 break;
560 n = nl - buffer;
561 if (n_lines-- == 0)
562 {
563 /* If this newline isn't the last character in the buffer,
564 output the part that is after it. */
565 xwrite_stdout (nl + 1, bytes_read - (n + 1));
566 *read_pos += dump_remainder (false, pretty_filename, fd,
567 end_pos - (pos + bytes_read));
568 return true;
569 }
570 }
571
572 /* Not enough newlines in that bufferfull. */
573 if (pos == start_pos)
574 {
575 /* Not enough lines in the file; print everything from
576 start_pos to the end. */
577 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
578 *read_pos = start_pos + dump_remainder (false, pretty_filename, fd,
579 end_pos);
580 return true;
581 }
582 pos -= BUFSIZ;
583 xlseek (fd, pos, SEEK_SET, pretty_filename);
584
585 bytes_read = safe_read (fd, buffer, BUFSIZ);
586 if (bytes_read == SAFE_READ_ERROR)
587 {
588 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
589 return false;
590 }
591
592 *read_pos = pos + bytes_read;
593 }
594 while (bytes_read > 0);
595
596 return true;
597 }
598
599 /* Print the last N_LINES lines from the end of the standard input,
600 open for reading as pipe FD.
601 Buffer the text as a linked list of LBUFFERs, adding them as needed.
602 Return true if successful. */
603
604 static bool
pipe_lines(char const * pretty_filename,int fd,uintmax_t n_lines,uintmax_t * read_pos)605 pipe_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
606 uintmax_t *read_pos)
607 {
608 struct linebuffer
609 {
610 char buffer[BUFSIZ];
611 size_t nbytes;
612 size_t nlines;
613 struct linebuffer *next;
614 };
615 typedef struct linebuffer LBUFFER;
616 LBUFFER *first, *last, *tmp;
617 size_t total_lines = 0; /* Total number of newlines in all buffers. */
618 bool ok = true;
619 size_t n_read; /* Size in bytes of most recent read */
620
621 first = last = xmalloc (sizeof (LBUFFER));
622 first->nbytes = first->nlines = 0;
623 first->next = nullptr;
624 tmp = xmalloc (sizeof (LBUFFER));
625
626 /* Input is always read into a fresh buffer. */
627 while (true)
628 {
629 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
630 if (n_read == 0 || n_read == SAFE_READ_ERROR)
631 break;
632 tmp->nbytes = n_read;
633 *read_pos += n_read;
634 tmp->nlines = 0;
635 tmp->next = nullptr;
636
637 /* Count the number of newlines just read. */
638 {
639 char const *buffer_end = tmp->buffer + n_read;
640 char const *p = tmp->buffer;
641 while ((p = memchr (p, line_end, buffer_end - p)))
642 {
643 ++p;
644 ++tmp->nlines;
645 }
646 }
647 total_lines += tmp->nlines;
648
649 /* If there is enough room in the last buffer read, just append the new
650 one to it. This is because when reading from a pipe, 'n_read' can
651 often be very small. */
652 if (tmp->nbytes + last->nbytes < BUFSIZ)
653 {
654 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
655 last->nbytes += tmp->nbytes;
656 last->nlines += tmp->nlines;
657 }
658 else
659 {
660 /* If there's not enough room, link the new buffer onto the end of
661 the list, then either free up the oldest buffer for the next
662 read if that would leave enough lines, or else malloc a new one.
663 Some compaction mechanism is possible but probably not
664 worthwhile. */
665 last = last->next = tmp;
666 if (total_lines - first->nlines > n_lines)
667 {
668 tmp = first;
669 total_lines -= first->nlines;
670 first = first->next;
671 }
672 else
673 tmp = xmalloc (sizeof (LBUFFER));
674 }
675 }
676
677 free (tmp);
678
679 if (n_read == SAFE_READ_ERROR)
680 {
681 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
682 ok = false;
683 goto free_lbuffers;
684 }
685
686 /* If the file is empty, then bail out. */
687 if (last->nbytes == 0)
688 goto free_lbuffers;
689
690 /* This prevents a core dump when the pipe contains no newlines. */
691 if (n_lines == 0)
692 goto free_lbuffers;
693
694 /* Count the incomplete line on files that don't end with a newline. */
695 if (last->buffer[last->nbytes - 1] != line_end)
696 {
697 ++last->nlines;
698 ++total_lines;
699 }
700
701 /* Run through the list, printing lines. First, skip over unneeded
702 buffers. */
703 for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
704 total_lines -= tmp->nlines;
705
706 /* Find the correct beginning, then print the rest of the file. */
707 {
708 char const *beg = tmp->buffer;
709 char const *buffer_end = tmp->buffer + tmp->nbytes;
710 if (total_lines > n_lines)
711 {
712 /* Skip 'total_lines' - 'n_lines' newlines. We made sure that
713 'total_lines' - 'n_lines' <= 'tmp->nlines'. */
714 size_t j;
715 for (j = total_lines - n_lines; j; --j)
716 {
717 beg = rawmemchr (beg, line_end);
718 ++beg;
719 }
720 }
721
722 xwrite_stdout (beg, buffer_end - beg);
723 }
724
725 for (tmp = tmp->next; tmp; tmp = tmp->next)
726 xwrite_stdout (tmp->buffer, tmp->nbytes);
727
728 free_lbuffers:
729 while (first)
730 {
731 tmp = first->next;
732 free (first);
733 first = tmp;
734 }
735 return ok;
736 }
737
738 /* Print the last N_BYTES characters from the end of pipe FD.
739 This is a stripped down version of pipe_lines.
740 Return true if successful. */
741
742 static bool
pipe_bytes(char const * pretty_filename,int fd,uintmax_t n_bytes,uintmax_t * read_pos)743 pipe_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
744 uintmax_t *read_pos)
745 {
746 struct charbuffer
747 {
748 char buffer[BUFSIZ];
749 size_t nbytes;
750 struct charbuffer *next;
751 };
752 typedef struct charbuffer CBUFFER;
753 CBUFFER *first, *last, *tmp;
754 size_t i; /* Index into buffers. */
755 size_t total_bytes = 0; /* Total characters in all buffers. */
756 bool ok = true;
757 size_t n_read;
758
759 first = last = xmalloc (sizeof (CBUFFER));
760 first->nbytes = 0;
761 first->next = nullptr;
762 tmp = xmalloc (sizeof (CBUFFER));
763
764 /* Input is always read into a fresh buffer. */
765 while (true)
766 {
767 n_read = safe_read (fd, tmp->buffer, BUFSIZ);
768 if (n_read == 0 || n_read == SAFE_READ_ERROR)
769 break;
770 *read_pos += n_read;
771 tmp->nbytes = n_read;
772 tmp->next = nullptr;
773
774 total_bytes += tmp->nbytes;
775 /* If there is enough room in the last buffer read, just append the new
776 one to it. This is because when reading from a pipe, 'nbytes' can
777 often be very small. */
778 if (tmp->nbytes + last->nbytes < BUFSIZ)
779 {
780 memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
781 last->nbytes += tmp->nbytes;
782 }
783 else
784 {
785 /* If there's not enough room, link the new buffer onto the end of
786 the list, then either free up the oldest buffer for the next
787 read if that would leave enough characters, or else malloc a new
788 one. Some compaction mechanism is possible but probably not
789 worthwhile. */
790 last = last->next = tmp;
791 if (total_bytes - first->nbytes > n_bytes)
792 {
793 tmp = first;
794 total_bytes -= first->nbytes;
795 first = first->next;
796 }
797 else
798 {
799 tmp = xmalloc (sizeof (CBUFFER));
800 }
801 }
802 }
803
804 free (tmp);
805
806 if (n_read == SAFE_READ_ERROR)
807 {
808 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
809 ok = false;
810 goto free_cbuffers;
811 }
812
813 /* Run through the list, printing characters. First, skip over unneeded
814 buffers. */
815 for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
816 total_bytes -= tmp->nbytes;
817
818 /* Find the correct beginning, then print the rest of the file.
819 We made sure that 'total_bytes' - 'n_bytes' <= 'tmp->nbytes'. */
820 if (total_bytes > n_bytes)
821 i = total_bytes - n_bytes;
822 else
823 i = 0;
824 xwrite_stdout (&tmp->buffer[i], tmp->nbytes - i);
825
826 for (tmp = tmp->next; tmp; tmp = tmp->next)
827 xwrite_stdout (tmp->buffer, tmp->nbytes);
828
829 free_cbuffers:
830 while (first)
831 {
832 tmp = first->next;
833 free (first);
834 first = tmp;
835 }
836 return ok;
837 }
838
839 /* Skip N_BYTES characters from the start of pipe FD, and print
840 any extra characters that were read beyond that.
841 Return 1 on error, 0 if ok, -1 if EOF. */
842
843 static int
start_bytes(char const * pretty_filename,int fd,uintmax_t n_bytes,uintmax_t * read_pos)844 start_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
845 uintmax_t *read_pos)
846 {
847 char buffer[BUFSIZ];
848
849 while (0 < n_bytes)
850 {
851 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
852 if (bytes_read == 0)
853 return -1;
854 if (bytes_read == SAFE_READ_ERROR)
855 {
856 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
857 return 1;
858 }
859 *read_pos += bytes_read;
860 if (bytes_read <= n_bytes)
861 n_bytes -= bytes_read;
862 else
863 {
864 size_t n_remaining = bytes_read - n_bytes;
865 /* Print extra characters if there are any. */
866 xwrite_stdout (&buffer[n_bytes], n_remaining);
867 break;
868 }
869 }
870
871 return 0;
872 }
873
874 /* Skip N_LINES lines at the start of file or pipe FD, and print
875 any extra characters that were read beyond that.
876 Return 1 on error, 0 if ok, -1 if EOF. */
877
878 static int
start_lines(char const * pretty_filename,int fd,uintmax_t n_lines,uintmax_t * read_pos)879 start_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
880 uintmax_t *read_pos)
881 {
882 if (n_lines == 0)
883 return 0;
884
885 while (true)
886 {
887 char buffer[BUFSIZ];
888 size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
889 if (bytes_read == 0) /* EOF */
890 return -1;
891 if (bytes_read == SAFE_READ_ERROR) /* error */
892 {
893 error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
894 return 1;
895 }
896
897 char *buffer_end = buffer + bytes_read;
898
899 *read_pos += bytes_read;
900
901 char *p = buffer;
902 while ((p = memchr (p, line_end, buffer_end - p)))
903 {
904 ++p;
905 if (--n_lines == 0)
906 {
907 if (p < buffer_end)
908 xwrite_stdout (p, buffer_end - p);
909 return 0;
910 }
911 }
912 }
913 }
914
915 /* Return false when FD is open on a file residing on a local file system.
916 If fstatfs fails, give a diagnostic and return true.
917 If fstatfs cannot be called, return true. */
918 static bool
fremote(int fd,char const * name)919 fremote (int fd, char const *name)
920 {
921 bool remote = true; /* be conservative (poll by default). */
922
923 #if HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE \
924 && (defined __linux__ || defined __ANDROID__)
925 struct statfs buf;
926 int err = fstatfs (fd, &buf);
927 if (err != 0)
928 {
929 /* On at least linux-2.6.38, fstatfs fails with ENOSYS when FD
930 is open on a pipe. Treat that like a remote file. */
931 if (errno != ENOSYS)
932 error (0, errno, _("cannot determine location of %s. "
933 "reverting to polling"), quoteaf (name));
934 }
935 else
936 {
937 /* Treat unrecognized file systems as "remote", so caller polls.
938 Note README-release has instructions for syncing the internal
939 list with the latest Linux kernel file system constants. */
940 remote = is_local_fs_type (buf.f_type) <= 0;
941 }
942 #endif
943
944 return remote;
945 }
946
947 /* open/fstat F->name and handle changes. */
948 static void
recheck(struct File_spec * f,bool blocking)949 recheck (struct File_spec *f, bool blocking)
950 {
951 struct stat new_stats;
952 bool ok = true;
953 bool is_stdin = (STREQ (f->name, "-"));
954 bool was_tailable = f->tailable;
955 int prev_errnum = f->errnum;
956 bool new_file;
957 int fd = (is_stdin
958 ? STDIN_FILENO
959 : open (f->name, O_RDONLY | (blocking ? 0 : O_NONBLOCK)));
960
961 affirm (valid_file_spec (f));
962
963 /* If the open fails because the file doesn't exist,
964 then mark the file as not tailable. */
965 f->tailable = !(reopen_inaccessible_files && fd == -1);
966
967 if (! disable_inotify && ! lstat (f->name, &new_stats)
968 && S_ISLNK (new_stats.st_mode))
969 {
970 /* Diagnose the edge case where a regular file is changed
971 to a symlink. We avoid inotify with symlinks since
972 it's awkward to match between symlink name and target. */
973 ok = false;
974 f->errnum = -1;
975 f->ignore = true;
976
977 error (0, 0, _("%s has been replaced with an untailable symbolic link"),
978 quoteaf (pretty_name (f)));
979 }
980 else if (fd == -1 || fstat (fd, &new_stats) < 0)
981 {
982 ok = false;
983 f->errnum = errno;
984 if (!f->tailable)
985 {
986 if (was_tailable)
987 {
988 /* FIXME-maybe: detect the case in which the file first becomes
989 unreadable (perms), and later becomes readable again and can
990 be seen to be the same file (dev/ino). Otherwise, tail prints
991 the entire contents of the file when it becomes readable. */
992 error (0, f->errnum, _("%s has become inaccessible"),
993 quoteaf (pretty_name (f)));
994 }
995 else
996 {
997 /* say nothing... it's still not tailable */
998 }
999 }
1000 else if (prev_errnum != errno)
1001 error (0, errno, "%s", quotef (pretty_name (f)));
1002 }
1003 else if (!IS_TAILABLE_FILE_TYPE (new_stats.st_mode))
1004 {
1005 ok = false;
1006 f->errnum = -1;
1007 f->tailable = false;
1008 f->ignore = ! (reopen_inaccessible_files && follow_mode == Follow_name);
1009 if (was_tailable || prev_errnum != f->errnum)
1010 error (0, 0, _("%s has been replaced with an untailable file%s"),
1011 quoteaf (pretty_name (f)),
1012 f->ignore ? _("; giving up on this name") : "");
1013 }
1014 else if ((f->remote = fremote (fd, pretty_name (f))) && ! disable_inotify)
1015 {
1016 ok = false;
1017 f->errnum = -1;
1018 error (0, 0, _("%s has been replaced with an untailable remote file"),
1019 quoteaf (pretty_name (f)));
1020 f->ignore = true;
1021 f->remote = true;
1022 }
1023 else
1024 {
1025 f->errnum = 0;
1026 }
1027
1028 new_file = false;
1029 if (!ok)
1030 {
1031 close_fd (fd, pretty_name (f));
1032 close_fd (f->fd, pretty_name (f));
1033 f->fd = -1;
1034 }
1035 else if (prev_errnum && prev_errnum != ENOENT)
1036 {
1037 new_file = true;
1038 affirm (f->fd == -1);
1039 error (0, 0, _("%s has become accessible"), quoteaf (pretty_name (f)));
1040 }
1041 else if (f->fd == -1)
1042 {
1043 /* A new file even when inodes haven't changed as <dev,inode>
1044 pairs can be reused, and we know the file was missing
1045 on the previous iteration. Note this also means the file
1046 is redisplayed in --follow=name mode if renamed away from
1047 and back to a monitored name. */
1048 new_file = true;
1049
1050 error (0, 0,
1051 _("%s has appeared; following new file"),
1052 quoteaf (pretty_name (f)));
1053 }
1054 else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
1055 {
1056 /* File has been replaced (e.g., via log rotation) --
1057 tail the new one. */
1058 new_file = true;
1059
1060 error (0, 0,
1061 _("%s has been replaced; following new file"),
1062 quoteaf (pretty_name (f)));
1063
1064 /* Close the old one. */
1065 close_fd (f->fd, pretty_name (f));
1066
1067 }
1068 else
1069 {
1070 /* No changes detected, so close new fd. */
1071 close_fd (fd, pretty_name (f));
1072 }
1073
1074 /* FIXME: When a log is rotated, daemons tend to log to the
1075 old file descriptor until the new file is present and
1076 the daemon is sent a signal. Therefore tail may miss entries
1077 being written to the old file. Perhaps we should keep
1078 the older file open and continue to monitor it until
1079 data is written to a new file. */
1080 if (new_file)
1081 {
1082 /* Start at the beginning of the file. */
1083 record_open_fd (f, fd, 0, &new_stats, (is_stdin ? -1 : blocking));
1084 if (S_ISREG (new_stats.st_mode))
1085 xlseek (fd, 0, SEEK_SET, pretty_name (f));
1086 }
1087 }
1088
1089 /* Return true if any of the N_FILES files in F are live, i.e., have
1090 open file descriptors, or should be checked again (see --retry).
1091 When following descriptors, checking should only continue when any
1092 of the files is not yet ignored. */
1093
1094 static bool
any_live_files(const struct File_spec * f,size_t n_files)1095 any_live_files (const struct File_spec *f, size_t n_files)
1096 {
1097 /* In inotify mode, ignore may be set for files
1098 which may later be replaced with new files.
1099 So always consider files live in -F mode. */
1100 if (reopen_inaccessible_files && follow_mode == Follow_name)
1101 return true;
1102
1103 for (size_t i = 0; i < n_files; i++)
1104 {
1105 if (0 <= f[i].fd)
1106 return true;
1107 else
1108 {
1109 if (! f[i].ignore && reopen_inaccessible_files)
1110 return true;
1111 }
1112 }
1113
1114 return false;
1115 }
1116
1117 /* Determine whether all watched writers are dead.
1118 Returns true only if all processes' states can be determined,
1119 and all processes no longer exist. */
1120
1121 static bool
writers_are_dead(void)1122 writers_are_dead (void)
1123 {
1124 if (!nbpids)
1125 return false;
1126
1127 for (int i = 0; i < nbpids; i++)
1128 {
1129 if (kill (pids[i], 0) == 0 || errno == EPERM)
1130 return false;
1131 }
1132
1133 return true;
1134 }
1135
1136 /* Tail N_FILES files forever, or until killed.
1137 The pertinent information for each file is stored in an entry of F.
1138 Loop over each of them, doing an fstat to see if they have changed size,
1139 and an occasional open/fstat to see if any dev/ino pair has changed.
1140 If none of them have changed size in one iteration, sleep for a
1141 while and try again. Continue until the user interrupts us. */
1142
1143 static void
tail_forever(struct File_spec * f,size_t n_files,double sleep_interval)1144 tail_forever (struct File_spec *f, size_t n_files, double sleep_interval)
1145 {
1146 /* Use blocking I/O as an optimization, when it's easy. */
1147 bool blocking = (!nbpids && follow_mode == Follow_descriptor
1148 && n_files == 1 && f[0].fd != -1 && ! S_ISREG (f[0].mode));
1149 size_t last;
1150 bool writers_dead = false;
1151
1152 last = n_files - 1;
1153
1154 while (true)
1155 {
1156 size_t i;
1157 bool any_input = false;
1158
1159 for (i = 0; i < n_files; i++)
1160 {
1161 int fd;
1162 char const *name;
1163 mode_t mode;
1164 struct stat stats;
1165 uintmax_t bytes_read;
1166
1167 if (f[i].ignore)
1168 continue;
1169
1170 if (f[i].fd < 0)
1171 {
1172 recheck (&f[i], blocking);
1173 continue;
1174 }
1175
1176 fd = f[i].fd;
1177 name = pretty_name (&f[i]);
1178 mode = f[i].mode;
1179
1180 if (f[i].blocking != blocking)
1181 {
1182 int old_flags = fcntl (fd, F_GETFL);
1183 int new_flags = old_flags | (blocking ? 0 : O_NONBLOCK);
1184 if (old_flags < 0
1185 || (new_flags != old_flags
1186 && fcntl (fd, F_SETFL, new_flags) == -1))
1187 {
1188 /* Don't update f[i].blocking if fcntl fails. */
1189 if (S_ISREG (f[i].mode) && errno == EPERM)
1190 {
1191 /* This happens when using tail -f on a file with
1192 the append-only attribute. */
1193 }
1194 else
1195 error (EXIT_FAILURE, errno,
1196 _("%s: cannot change nonblocking mode"),
1197 quotef (name));
1198 }
1199 else
1200 f[i].blocking = blocking;
1201 }
1202
1203 bool read_unchanged = false;
1204 if (!f[i].blocking)
1205 {
1206 if (fstat (fd, &stats) != 0)
1207 {
1208 f[i].fd = -1;
1209 f[i].errnum = errno;
1210 error (0, errno, "%s", quotef (name));
1211 close (fd); /* ignore failure */
1212 continue;
1213 }
1214
1215 if (f[i].mode == stats.st_mode
1216 && (! S_ISREG (stats.st_mode) || f[i].size == stats.st_size)
1217 && timespec_cmp (f[i].mtime, get_stat_mtime (&stats)) == 0)
1218 {
1219 if ((max_n_unchanged_stats_between_opens
1220 <= f[i].n_unchanged_stats++)
1221 && follow_mode == Follow_name)
1222 {
1223 recheck (&f[i], f[i].blocking);
1224 f[i].n_unchanged_stats = 0;
1225 }
1226 if (fd != f[i].fd || S_ISREG (stats.st_mode) || 1 < n_files)
1227 continue;
1228 else
1229 read_unchanged = true;
1230 }
1231
1232 affirm (fd == f[i].fd);
1233
1234 /* This file has changed. Print out what we can, and
1235 then keep looping. */
1236
1237 f[i].mtime = get_stat_mtime (&stats);
1238 f[i].mode = stats.st_mode;
1239
1240 /* reset counter */
1241 if (! read_unchanged)
1242 f[i].n_unchanged_stats = 0;
1243
1244 /* XXX: This is only a heuristic, as the file may have also
1245 been truncated and written to if st_size >= size
1246 (in which case we ignore new data <= size). */
1247 if (S_ISREG (mode) && stats.st_size < f[i].size)
1248 {
1249 error (0, 0, _("%s: file truncated"), quotef (name));
1250 /* Assume the file was truncated to 0,
1251 and therefore output all "new" data. */
1252 xlseek (fd, 0, SEEK_SET, name);
1253 f[i].size = 0;
1254 }
1255
1256 if (i != last)
1257 {
1258 if (print_headers)
1259 write_header (name);
1260 last = i;
1261 }
1262 }
1263
1264 /* Don't read more than st_size on networked file systems
1265 because it was seen on glusterfs at least, that st_size
1266 may be smaller than the data read on a _subsequent_ stat call. */
1267 uintmax_t bytes_to_read;
1268 if (f[i].blocking)
1269 bytes_to_read = COPY_A_BUFFER;
1270 else if (S_ISREG (mode) && f[i].remote)
1271 bytes_to_read = stats.st_size - f[i].size;
1272 else
1273 bytes_to_read = COPY_TO_EOF;
1274
1275 bytes_read = dump_remainder (false, name, fd, bytes_to_read);
1276
1277 if (read_unchanged && bytes_read)
1278 f[i].n_unchanged_stats = 0;
1279
1280 any_input |= (bytes_read != 0);
1281 f[i].size += bytes_read;
1282 }
1283
1284 if (! any_live_files (f, n_files))
1285 {
1286 error (0, 0, _("no files remaining"));
1287 break;
1288 }
1289
1290 if ((!any_input || blocking) && fflush (stdout) != 0)
1291 write_error ();
1292
1293 check_output_alive ();
1294
1295 /* If nothing was read, sleep and/or check for dead writers. */
1296 if (!any_input)
1297 {
1298 if (writers_dead)
1299 break;
1300
1301 /* Once the writer is dead, read the files once more to
1302 avoid a race condition. */
1303 writers_dead = writers_are_dead ();
1304
1305 if (!writers_dead && xnanosleep (sleep_interval))
1306 error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
1307
1308 }
1309 }
1310 }
1311
1312 #if HAVE_INOTIFY
1313
1314 /* Return true if any of the N_FILES files in F is remote, i.e., has
1315 an open file descriptor and is on a network file system. */
1316
1317 static bool
any_remote_file(const struct File_spec * f,size_t n_files)1318 any_remote_file (const struct File_spec *f, size_t n_files)
1319 {
1320 for (size_t i = 0; i < n_files; i++)
1321 if (0 <= f[i].fd && f[i].remote)
1322 return true;
1323 return false;
1324 }
1325
1326 /* Return true if any of the N_FILES files in F is non remote, i.e., has
1327 an open file descriptor and is not on a network file system. */
1328
1329 static bool
any_non_remote_file(const struct File_spec * f,size_t n_files)1330 any_non_remote_file (const struct File_spec *f, size_t n_files)
1331 {
1332 for (size_t i = 0; i < n_files; i++)
1333 if (0 <= f[i].fd && ! f[i].remote)
1334 return true;
1335 return false;
1336 }
1337
1338 /* Return true if any of the N_FILES files in F is a symlink.
1339 Note we don't worry about the edge case where "-" exists,
1340 since that will have the same consequences for inotify,
1341 which is the only context this function is currently used. */
1342
1343 static bool
any_symlinks(const struct File_spec * f,size_t n_files)1344 any_symlinks (const struct File_spec *f, size_t n_files)
1345 {
1346 struct stat st;
1347 for (size_t i = 0; i < n_files; i++)
1348 if (lstat (f[i].name, &st) == 0 && S_ISLNK (st.st_mode))
1349 return true;
1350 return false;
1351 }
1352
1353 /* Return true if any of the N_FILES files in F is not
1354 a regular file or fifo. This is used to avoid adding inotify
1355 watches on a device file for example, which inotify
1356 will accept, but not give any events for. */
1357
1358 static bool
any_non_regular_fifo(const struct File_spec * f,size_t n_files)1359 any_non_regular_fifo (const struct File_spec *f, size_t n_files)
1360 {
1361 for (size_t i = 0; i < n_files; i++)
1362 if (0 <= f[i].fd && ! S_ISREG (f[i].mode) && ! S_ISFIFO (f[i].mode))
1363 return true;
1364 return false;
1365 }
1366
1367 /* Return true if any of the N_FILES files in F represents
1368 stdin and is tailable. */
1369
1370 static bool
tailable_stdin(const struct File_spec * f,size_t n_files)1371 tailable_stdin (const struct File_spec *f, size_t n_files)
1372 {
1373 for (size_t i = 0; i < n_files; i++)
1374 if (!f[i].ignore && STREQ (f[i].name, "-"))
1375 return true;
1376 return false;
1377 }
1378
1379 static size_t
wd_hasher(const void * entry,size_t tabsize)1380 wd_hasher (const void *entry, size_t tabsize)
1381 {
1382 const struct File_spec *spec = entry;
1383 return spec->wd % tabsize;
1384 }
1385
1386 static bool
wd_comparator(const void * e1,const void * e2)1387 wd_comparator (const void *e1, const void *e2)
1388 {
1389 const struct File_spec *spec1 = e1;
1390 const struct File_spec *spec2 = e2;
1391 return spec1->wd == spec2->wd;
1392 }
1393
1394 /* Output (new) data for FSPEC->fd.
1395 PREV_FSPEC records the last File_spec for which we output. */
1396 static void
check_fspec(struct File_spec * fspec,struct File_spec ** prev_fspec)1397 check_fspec (struct File_spec *fspec, struct File_spec **prev_fspec)
1398 {
1399 struct stat stats;
1400 char const *name;
1401
1402 if (fspec->fd == -1)
1403 return;
1404
1405 name = pretty_name (fspec);
1406
1407 if (fstat (fspec->fd, &stats) != 0)
1408 {
1409 fspec->errnum = errno;
1410 close_fd (fspec->fd, name);
1411 fspec->fd = -1;
1412 return;
1413 }
1414
1415 /* XXX: This is only a heuristic, as the file may have also
1416 been truncated and written to if st_size >= size
1417 (in which case we ignore new data <= size).
1418 Though in the inotify case it's more likely we'll get
1419 separate events for truncate() and write(). */
1420 if (S_ISREG (fspec->mode) && stats.st_size < fspec->size)
1421 {
1422 error (0, 0, _("%s: file truncated"), quotef (name));
1423 xlseek (fspec->fd, 0, SEEK_SET, name);
1424 fspec->size = 0;
1425 }
1426 else if (S_ISREG (fspec->mode) && stats.st_size == fspec->size
1427 && timespec_cmp (fspec->mtime, get_stat_mtime (&stats)) == 0)
1428 return;
1429
1430 bool want_header = print_headers && (fspec != *prev_fspec);
1431
1432 uintmax_t bytes_read = dump_remainder (want_header, name, fspec->fd,
1433 COPY_TO_EOF);
1434 fspec->size += bytes_read;
1435
1436 if (bytes_read)
1437 {
1438 *prev_fspec = fspec;
1439 if (fflush (stdout) != 0)
1440 write_error ();
1441 }
1442 }
1443
1444 /* Attempt to tail N_FILES files forever, or until killed.
1445 Check modifications using the inotify events system.
1446 Exit if finished or on fatal error; return to revert to polling. */
1447 static void
tail_forever_inotify(int wd,struct File_spec * f,size_t n_files,double sleep_interval,Hash_table ** wd_to_namep)1448 tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
1449 double sleep_interval, Hash_table **wd_to_namep)
1450 {
1451 # if TAIL_TEST_SLEEP
1452 /* Delay between open() and inotify_add_watch()
1453 to help trigger different cases. */
1454 xnanosleep (1000000);
1455 # endif
1456 unsigned int max_realloc = 3;
1457
1458 /* Map an inotify watch descriptor to the name of the file it's watching. */
1459 Hash_table *wd_to_name;
1460
1461 bool found_watchable_file = false;
1462 bool tailed_but_unwatchable = false;
1463 bool found_unwatchable_dir = false;
1464 bool no_inotify_resources = false;
1465 bool writers_dead = false;
1466 struct File_spec *prev_fspec;
1467 size_t evlen = 0;
1468 char *evbuf;
1469 size_t evbuf_off = 0;
1470 size_t len = 0;
1471
1472 wd_to_name = hash_initialize (n_files, nullptr, wd_hasher, wd_comparator,
1473 nullptr);
1474 if (! wd_to_name)
1475 xalloc_die ();
1476 *wd_to_namep = wd_to_name;
1477
1478 /* The events mask used with inotify on files (not directories). */
1479 uint32_t inotify_wd_mask = IN_MODIFY;
1480 /* TODO: Perhaps monitor these events in Follow_descriptor mode also,
1481 to tag reported file names with "deleted", "moved" etc. */
1482 if (follow_mode == Follow_name)
1483 inotify_wd_mask |= (IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF);
1484
1485 /* Add an inotify watch for each watched file. If -F is specified then watch
1486 its parent directory too, in this way when they re-appear we can add them
1487 again to the watch list. */
1488 size_t i;
1489 for (i = 0; i < n_files; i++)
1490 {
1491 if (!f[i].ignore)
1492 {
1493 size_t fnlen = strlen (f[i].name);
1494 if (evlen < fnlen)
1495 evlen = fnlen;
1496
1497 f[i].wd = -1;
1498
1499 if (follow_mode == Follow_name)
1500 {
1501 size_t dirlen = dir_len (f[i].name);
1502 char prev = f[i].name[dirlen];
1503 f[i].basename_start = last_component (f[i].name) - f[i].name;
1504
1505 f[i].name[dirlen] = '\0';
1506
1507 /* It's fine to add the same directory more than once.
1508 In that case the same watch descriptor is returned. */
1509 f[i].parent_wd = inotify_add_watch (wd, dirlen ? f[i].name : ".",
1510 (IN_CREATE | IN_DELETE
1511 | IN_MOVED_TO | IN_ATTRIB
1512 | IN_DELETE_SELF));
1513
1514 f[i].name[dirlen] = prev;
1515
1516 if (f[i].parent_wd < 0)
1517 {
1518 if (errno != ENOSPC) /* suppress confusing error. */
1519 error (0, errno, _("cannot watch parent directory of %s"),
1520 quoteaf (f[i].name));
1521 else
1522 error (0, 0, _("inotify resources exhausted"));
1523 found_unwatchable_dir = true;
1524 /* We revert to polling below. Note invalid uses
1525 of the inotify API will still be diagnosed. */
1526 break;
1527 }
1528 }
1529
1530 f[i].wd = inotify_add_watch (wd, f[i].name, inotify_wd_mask);
1531
1532 if (f[i].wd < 0)
1533 {
1534 if (f[i].fd != -1) /* already tailed. */
1535 tailed_but_unwatchable = true;
1536 if (errno == ENOSPC || errno == ENOMEM)
1537 {
1538 no_inotify_resources = true;
1539 error (0, 0, _("inotify resources exhausted"));
1540 break;
1541 }
1542 else if (errno != f[i].errnum)
1543 error (0, errno, _("cannot watch %s"), quoteaf (f[i].name));
1544 continue;
1545 }
1546
1547 if (hash_insert (wd_to_name, &(f[i])) == nullptr)
1548 xalloc_die ();
1549
1550 found_watchable_file = true;
1551 }
1552 }
1553
1554 /* Linux kernel 2.6.24 at least has a bug where eventually, ENOSPC is always
1555 returned by inotify_add_watch. In any case we should revert to polling
1556 when there are no inotify resources. Also a specified directory may not
1557 be currently present or accessible, so revert to polling. Also an already
1558 tailed but unwatchable due rename/unlink race, should also revert. */
1559 if (no_inotify_resources || found_unwatchable_dir
1560 || (follow_mode == Follow_descriptor && tailed_but_unwatchable))
1561 return;
1562 if (follow_mode == Follow_descriptor && !found_watchable_file)
1563 exit (EXIT_FAILURE);
1564
1565 prev_fspec = &(f[n_files - 1]);
1566
1567 /* Check files again. New files or data can be available since last time we
1568 checked and before they are watched by inotify. */
1569 for (i = 0; i < n_files; i++)
1570 {
1571 if (! f[i].ignore)
1572 {
1573 /* check for new files. */
1574 if (follow_mode == Follow_name)
1575 recheck (&(f[i]), false);
1576 else if (f[i].fd != -1)
1577 {
1578 /* If the file was replaced in the small window since we tailed,
1579 then assume the watch is on the wrong item (different to
1580 that we've already produced output for), and so revert to
1581 polling the original descriptor. */
1582 struct stat stats;
1583
1584 if (stat (f[i].name, &stats) == 0
1585 && (f[i].dev != stats.st_dev || f[i].ino != stats.st_ino))
1586 {
1587 error (0, errno, _("%s was replaced"),
1588 quoteaf (pretty_name (&(f[i]))));
1589 return;
1590 }
1591 }
1592
1593 /* check for new data. */
1594 check_fspec (&f[i], &prev_fspec);
1595 }
1596 }
1597
1598 evlen += sizeof (struct inotify_event) + 1;
1599 evbuf = xmalloc (evlen);
1600
1601 /* Wait for inotify events and handle them. Events on directories
1602 ensure that watched files can be re-added when following by name.
1603 This loop blocks on the 'safe_read' call until a new event is notified.
1604 But when --pid=P is specified, tail usually waits via poll. */
1605 while (true)
1606 {
1607 struct File_spec *fspec;
1608 struct inotify_event *ev;
1609 void *void_ev;
1610
1611 /* When following by name without --retry, and the last file has
1612 been unlinked or renamed-away, diagnose it and return. */
1613 if (follow_mode == Follow_name
1614 && ! reopen_inaccessible_files
1615 && hash_get_n_entries (wd_to_name) == 0)
1616 error (EXIT_FAILURE, 0, _("no files remaining"));
1617
1618 if (len <= evbuf_off)
1619 {
1620 /* Poll for inotify events. When watching a PID, ensure
1621 that a read from WD will not block indefinitely.
1622 If MONITOR_OUTPUT, also poll for a broken output pipe. */
1623
1624 int file_change;
1625 struct pollfd pfd[2];
1626 do
1627 {
1628 /* How many ms to wait for changes. -1 means wait forever. */
1629 int delay = -1;
1630
1631 if (nbpids)
1632 {
1633 if (writers_dead)
1634 exit (EXIT_SUCCESS);
1635
1636 writers_dead = writers_are_dead ();
1637
1638 if (writers_dead || sleep_interval <= 0)
1639 delay = 0;
1640 else if (sleep_interval < INT_MAX / 1000 - 1)
1641 {
1642 /* delay = ceil (sleep_interval * 1000), sans libm. */
1643 double ddelay = sleep_interval * 1000;
1644 delay = ddelay;
1645 delay += delay < ddelay;
1646 }
1647 }
1648
1649 pfd[0].fd = wd;
1650 pfd[0].events = POLLIN;
1651 pfd[1].fd = STDOUT_FILENO;
1652 pfd[1].events = pfd[1].revents = 0;
1653 file_change = poll (pfd, monitor_output + 1, delay);
1654 }
1655 while (file_change == 0);
1656
1657 if (file_change < 0)
1658 error (EXIT_FAILURE, errno,
1659 _("error waiting for inotify and output events"));
1660 if (pfd[1].revents)
1661 die_pipe ();
1662
1663 len = safe_read (wd, evbuf, evlen);
1664 evbuf_off = 0;
1665
1666 /* For kernels prior to 2.6.21, read returns 0 when the buffer
1667 is too small. */
1668 if ((len == 0 || (len == SAFE_READ_ERROR && errno == EINVAL))
1669 && max_realloc--)
1670 {
1671 len = 0;
1672 evlen *= 2;
1673 evbuf = xrealloc (evbuf, evlen);
1674 continue;
1675 }
1676
1677 if (len == 0 || len == SAFE_READ_ERROR)
1678 error (EXIT_FAILURE, errno, _("error reading inotify event"));
1679 }
1680
1681 void_ev = evbuf + evbuf_off;
1682 ev = void_ev;
1683 evbuf_off += sizeof (*ev) + ev->len;
1684
1685 /* If a directory is deleted, IN_DELETE_SELF is emitted
1686 with ev->name of length 0.
1687 We need to catch it, otherwise it would wait forever,
1688 as wd for directory becomes inactive. Revert to polling now. */
1689 if ((ev->mask & IN_DELETE_SELF) && ! ev->len)
1690 {
1691 for (i = 0; i < n_files; i++)
1692 {
1693 if (ev->wd == f[i].parent_wd)
1694 {
1695 error (0, 0,
1696 _("directory containing watched file was removed"));
1697 return;
1698 }
1699 }
1700 }
1701
1702 if (ev->len) /* event on ev->name in watched directory. */
1703 {
1704 size_t j;
1705 for (j = 0; j < n_files; j++)
1706 {
1707 /* With N=hundreds of frequently-changing files, this O(N^2)
1708 process might be a problem. FIXME: use a hash table? */
1709 if (f[j].parent_wd == ev->wd
1710 && STREQ (ev->name, f[j].name + f[j].basename_start))
1711 break;
1712 }
1713
1714 /* It is not a watched file. */
1715 if (j == n_files)
1716 continue;
1717
1718 fspec = &(f[j]);
1719
1720 int new_wd = -1;
1721 bool deleting = !! (ev->mask & IN_DELETE);
1722
1723 if (! deleting)
1724 {
1725 /* Adding the same inode again will look up any existing wd. */
1726 new_wd = inotify_add_watch (wd, f[j].name, inotify_wd_mask);
1727 }
1728
1729 if (! deleting && new_wd < 0)
1730 {
1731 if (errno == ENOSPC || errno == ENOMEM)
1732 {
1733 error (0, 0, _("inotify resources exhausted"));
1734 return; /* revert to polling. */
1735 }
1736 else
1737 {
1738 /* Can get ENOENT for a dangling symlink for example. */
1739 error (0, errno, _("cannot watch %s"), quoteaf (f[j].name));
1740 }
1741 /* We'll continue below after removing the existing watch. */
1742 }
1743
1744 /* This will be false if only attributes of file change. */
1745 bool new_watch;
1746 new_watch = (! deleting) && (fspec->wd < 0 || new_wd != fspec->wd);
1747
1748 if (new_watch)
1749 {
1750 if (0 <= fspec->wd)
1751 {
1752 inotify_rm_watch (wd, fspec->wd);
1753 hash_remove (wd_to_name, fspec);
1754 }
1755
1756 fspec->wd = new_wd;
1757
1758 if (new_wd == -1)
1759 continue;
1760
1761 /* If the file was moved then inotify will use the source file wd
1762 for the destination file. Make sure the key is not present in
1763 the table. */
1764 struct File_spec *prev = hash_remove (wd_to_name, fspec);
1765 if (prev && prev != fspec)
1766 {
1767 if (follow_mode == Follow_name)
1768 recheck (prev, false);
1769 prev->wd = -1;
1770 close_fd (prev->fd, pretty_name (prev));
1771 }
1772
1773 if (hash_insert (wd_to_name, fspec) == nullptr)
1774 xalloc_die ();
1775 }
1776
1777 if (follow_mode == Follow_name)
1778 recheck (fspec, false);
1779 }
1780 else
1781 {
1782 struct File_spec key;
1783 key.wd = ev->wd;
1784 fspec = hash_lookup (wd_to_name, &key);
1785 }
1786
1787 if (! fspec)
1788 continue;
1789
1790 if (ev->mask & (IN_ATTRIB | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF))
1791 {
1792 /* Note for IN_MOVE_SELF (the file we're watching has
1793 been clobbered via a rename) we leave the watch
1794 in place since it may still be part of the set
1795 of watched names. */
1796 if (ev->mask & IN_DELETE_SELF)
1797 {
1798 inotify_rm_watch (wd, fspec->wd);
1799 hash_remove (wd_to_name, fspec);
1800 }
1801
1802 /* Note we get IN_ATTRIB for unlink() as st_nlink decrements.
1803 The usual path is a close() done in recheck() triggers
1804 an IN_DELETE_SELF event as the inode is removed.
1805 However sometimes open() will succeed as even though
1806 st_nlink is decremented, the dentry (cache) is not updated.
1807 Thus we depend on the IN_DELETE event on the directory
1808 to trigger processing for the removed file. */
1809
1810 recheck (fspec, false);
1811
1812 continue;
1813 }
1814 check_fspec (fspec, &prev_fspec);
1815 }
1816 }
1817 #endif
1818
1819 /* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
1820 Return true if successful. */
1821
1822 static bool
tail_bytes(char const * pretty_filename,int fd,uintmax_t n_bytes,uintmax_t * read_pos)1823 tail_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
1824 uintmax_t *read_pos)
1825 {
1826 struct stat stats;
1827
1828 if (fstat (fd, &stats))
1829 {
1830 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1831 return false;
1832 }
1833
1834 if (from_start)
1835 {
1836 if (! presume_input_pipe && n_bytes <= OFF_T_MAX
1837 && ((S_ISREG (stats.st_mode)
1838 && xlseek (fd, n_bytes, SEEK_CUR, pretty_filename) >= 0)
1839 || lseek (fd, n_bytes, SEEK_CUR) != -1))
1840 *read_pos += n_bytes;
1841 else
1842 {
1843 int t = start_bytes (pretty_filename, fd, n_bytes, read_pos);
1844 if (t)
1845 return t < 0;
1846 }
1847 n_bytes = COPY_TO_EOF;
1848 }
1849 else
1850 {
1851 off_t end_pos = -1;
1852 off_t current_pos = -1;
1853
1854 if (! presume_input_pipe && n_bytes <= OFF_T_MAX)
1855 {
1856 if (usable_st_size (&stats))
1857 end_pos = stats.st_size;
1858 else if ((current_pos = lseek (fd, -n_bytes, SEEK_END)) != -1)
1859 end_pos = current_pos + n_bytes;
1860 }
1861 if (end_pos <= (off_t) STP_BLKSIZE (&stats))
1862 return pipe_bytes (pretty_filename, fd, n_bytes, read_pos);
1863 if (current_pos == -1)
1864 current_pos = xlseek (fd, 0, SEEK_CUR, pretty_filename);
1865 if (current_pos < end_pos)
1866 {
1867 off_t bytes_remaining = end_pos - current_pos;
1868
1869 if (n_bytes < bytes_remaining)
1870 {
1871 current_pos = end_pos - n_bytes;
1872 xlseek (fd, current_pos, SEEK_SET, pretty_filename);
1873 }
1874 }
1875 *read_pos = current_pos;
1876 }
1877
1878 *read_pos += dump_remainder (false, pretty_filename, fd, n_bytes);
1879 return true;
1880 }
1881
1882 /* Output the last N_LINES lines of file FILENAME open for reading in FD.
1883 Return true if successful. */
1884
1885 static bool
tail_lines(char const * pretty_filename,int fd,uintmax_t n_lines,uintmax_t * read_pos)1886 tail_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
1887 uintmax_t *read_pos)
1888 {
1889 struct stat stats;
1890
1891 if (fstat (fd, &stats))
1892 {
1893 error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename));
1894 return false;
1895 }
1896
1897 if (from_start)
1898 {
1899 int t = start_lines (pretty_filename, fd, n_lines, read_pos);
1900 if (t)
1901 return t < 0;
1902 *read_pos += dump_remainder (false, pretty_filename, fd, COPY_TO_EOF);
1903 }
1904 else
1905 {
1906 off_t start_pos = -1;
1907 off_t end_pos;
1908
1909 /* Use file_lines only if FD refers to a regular file for
1910 which lseek (... SEEK_END) works. */
1911 if ( ! presume_input_pipe
1912 && S_ISREG (stats.st_mode)
1913 && (start_pos = lseek (fd, 0, SEEK_CUR)) != -1
1914 && start_pos < (end_pos = lseek (fd, 0, SEEK_END)))
1915 {
1916 *read_pos = end_pos;
1917 if (end_pos != 0
1918 && ! file_lines (pretty_filename, fd, n_lines,
1919 start_pos, end_pos, read_pos))
1920 return false;
1921 }
1922 else
1923 {
1924 /* Under very unlikely circumstances, it is possible to reach
1925 this point after positioning the file pointer to end of file
1926 via the 'lseek (...SEEK_END)' above. In that case, reposition
1927 the file pointer back to start_pos before calling pipe_lines. */
1928 if (start_pos != -1)
1929 xlseek (fd, start_pos, SEEK_SET, pretty_filename);
1930
1931 return pipe_lines (pretty_filename, fd, n_lines, read_pos);
1932 }
1933 }
1934 return true;
1935 }
1936
1937 /* Display the last N_UNITS units of file FILENAME, open for reading
1938 via FD. Set *READ_POS to the position of the input stream pointer.
1939 *READ_POS is usually the number of bytes read and corresponds to an
1940 offset from the beginning of a file. However, it may be larger than
1941 OFF_T_MAX (as for an input pipe), and may also be larger than the
1942 number of bytes read (when an input pointer is initially not at
1943 beginning of file), and may be far greater than the number of bytes
1944 actually read for an input file that is seekable.
1945 Return true if successful. */
1946
1947 static bool
tail(char const * filename,int fd,uintmax_t n_units,uintmax_t * read_pos)1948 tail (char const *filename, int fd, uintmax_t n_units,
1949 uintmax_t *read_pos)
1950 {
1951 *read_pos = 0;
1952 if (count_lines)
1953 return tail_lines (filename, fd, n_units, read_pos);
1954 else
1955 return tail_bytes (filename, fd, n_units, read_pos);
1956 }
1957
1958 /* Display the last N_UNITS units of the file described by F.
1959 Return true if successful. */
1960
1961 static bool
tail_file(struct File_spec * f,uintmax_t n_units)1962 tail_file (struct File_spec *f, uintmax_t n_units)
1963 {
1964 int fd;
1965 bool ok;
1966
1967 bool is_stdin = (STREQ (f->name, "-"));
1968
1969 if (is_stdin)
1970 {
1971 have_read_stdin = true;
1972 fd = STDIN_FILENO;
1973 xset_binary_mode (STDIN_FILENO, O_BINARY);
1974 }
1975 else
1976 fd = open (f->name, O_RDONLY | O_BINARY);
1977
1978 f->tailable = !(reopen_inaccessible_files && fd == -1);
1979
1980 if (fd == -1)
1981 {
1982 if (forever)
1983 {
1984 f->fd = -1;
1985 f->errnum = errno;
1986 f->ignore = ! reopen_inaccessible_files;
1987 f->ino = 0;
1988 f->dev = 0;
1989 }
1990 error (0, errno, _("cannot open %s for reading"),
1991 quoteaf (pretty_name (f)));
1992 ok = false;
1993 }
1994 else
1995 {
1996 uintmax_t read_pos;
1997
1998 if (print_headers)
1999 write_header (pretty_name (f));
2000 ok = tail (pretty_name (f), fd, n_units, &read_pos);
2001 if (forever)
2002 {
2003 struct stat stats;
2004
2005 #if TAIL_TEST_SLEEP
2006 /* Before the tail function provided 'read_pos', there was
2007 a race condition described in the URL below. This sleep
2008 call made the window big enough to exercise the problem. */
2009 xnanosleep (1);
2010 #endif
2011 f->errnum = ok - 1;
2012 if (fstat (fd, &stats) < 0)
2013 {
2014 ok = false;
2015 f->errnum = errno;
2016 error (0, errno, _("error reading %s"),
2017 quoteaf (pretty_name (f)));
2018 }
2019 else if (!IS_TAILABLE_FILE_TYPE (stats.st_mode))
2020 {
2021 ok = false;
2022 f->errnum = -1;
2023 f->tailable = false;
2024 f->ignore = ! reopen_inaccessible_files;
2025 error (0, 0, _("%s: cannot follow end of this type of file%s"),
2026 quotef (pretty_name (f)),
2027 f->ignore ? _("; giving up on this name") : "");
2028 }
2029
2030 if (!ok)
2031 {
2032 f->ignore = ! reopen_inaccessible_files;
2033 close_fd (fd, pretty_name (f));
2034 f->fd = -1;
2035 }
2036 else
2037 {
2038 /* Note: we must use read_pos here, not stats.st_size,
2039 to avoid a race condition described by Ken Raeburn:
2040 https://lists.gnu.org/r/bug-textutils/2003-05/msg00007.html */
2041 record_open_fd (f, fd, read_pos, &stats, (is_stdin ? -1 : 1));
2042 f->remote = fremote (fd, pretty_name (f));
2043 }
2044 }
2045 else
2046 {
2047 if (!is_stdin && close (fd))
2048 {
2049 error (0, errno, _("error reading %s"),
2050 quoteaf (pretty_name (f)));
2051 ok = false;
2052 }
2053 }
2054 }
2055
2056 return ok;
2057 }
2058
2059 /* If obsolete usage is allowed, and the command line arguments are of
2060 the obsolete form and the option string is well-formed, set
2061 *N_UNITS, the globals COUNT_LINES, FOREVER, and FROM_START, and
2062 return true. If the command line arguments are obviously incorrect
2063 (e.g., because obsolete usage is not allowed and the arguments are
2064 incorrect for non-obsolete usage), report an error and exit.
2065 Otherwise, return false and don't modify any parameter or global
2066 variable. */
2067
2068 static bool
parse_obsolete_option(int argc,char * const * argv,uintmax_t * n_units)2069 parse_obsolete_option (int argc, char * const *argv, uintmax_t *n_units)
2070 {
2071 char const *p;
2072 char const *n_string;
2073 char const *n_string_end;
2074 int default_count = DEFAULT_N_LINES;
2075 bool t_from_start;
2076 bool t_count_lines = true;
2077 bool t_forever = false;
2078
2079 /* With the obsolete form, there is one option string and at most
2080 one file argument. Watch out for "-" and "--", though. */
2081 if (! (argc == 2
2082 || (argc == 3 && ! (argv[2][0] == '-' && argv[2][1]))
2083 || (3 <= argc && argc <= 4 && STREQ (argv[2], "--"))))
2084 return false;
2085
2086 int posix_ver = posix2_version ();
2087 bool obsolete_usage = posix_ver < 200112;
2088 bool traditional_usage = obsolete_usage || 200809 <= posix_ver;
2089 p = argv[1];
2090
2091 switch (*p++)
2092 {
2093 default:
2094 return false;
2095
2096 case '+':
2097 /* Leading "+" is a file name in the standard form. */
2098 if (!traditional_usage)
2099 return false;
2100
2101 t_from_start = true;
2102 break;
2103
2104 case '-':
2105 /* In the non-obsolete form, "-" is standard input and "-c"
2106 requires an option-argument. The obsolete multidigit options
2107 are supported as a GNU extension even when conforming to
2108 POSIX 1003.1-2001 or later, so don't complain about them. */
2109 if (!obsolete_usage && !p[p[0] == 'c'])
2110 return false;
2111
2112 t_from_start = false;
2113 break;
2114 }
2115
2116 n_string = p;
2117 while (ISDIGIT (*p))
2118 p++;
2119 n_string_end = p;
2120
2121 switch (*p)
2122 {
2123 case 'b': default_count *= 512; FALLTHROUGH;
2124 case 'c': t_count_lines = false; FALLTHROUGH;
2125 case 'l': p++; break;
2126 }
2127
2128 if (*p == 'f')
2129 {
2130 t_forever = true;
2131 ++p;
2132 }
2133
2134 if (*p)
2135 return false;
2136
2137 if (n_string == n_string_end)
2138 *n_units = default_count;
2139 else if ((xstrtoumax (n_string, nullptr, 10, n_units, "b")
2140 & ~LONGINT_INVALID_SUFFIX_CHAR)
2141 != LONGINT_OK)
2142 error (EXIT_FAILURE, errno, "%s: %s", _("invalid number"),
2143 quote (argv[1]));
2144
2145 /* Set globals. */
2146 from_start = t_from_start;
2147 count_lines = t_count_lines;
2148 forever = t_forever;
2149
2150 return true;
2151 }
2152
2153 static void
parse_options(int argc,char ** argv,uintmax_t * n_units,enum header_mode * header_mode,double * sleep_interval)2154 parse_options (int argc, char **argv,
2155 uintmax_t *n_units, enum header_mode *header_mode,
2156 double *sleep_interval)
2157 {
2158 int c;
2159
2160 while ((c = getopt_long (argc, argv, "c:n:fFqs:vz0123456789",
2161 long_options, nullptr))
2162 != -1)
2163 {
2164 switch (c)
2165 {
2166 case 'F':
2167 forever = true;
2168 follow_mode = Follow_name;
2169 reopen_inaccessible_files = true;
2170 break;
2171
2172 case 'c':
2173 case 'n':
2174 count_lines = (c == 'n');
2175 if (*optarg == '+')
2176 from_start = true;
2177 else if (*optarg == '-')
2178 ++optarg;
2179
2180 *n_units = xdectoumax (optarg, 0, UINTMAX_MAX, "bkKmMGTPEZYRQ0",
2181 count_lines
2182 ? _("invalid number of lines")
2183 : _("invalid number of bytes"), 0);
2184 break;
2185
2186 case 'f':
2187 case LONG_FOLLOW_OPTION:
2188 forever = true;
2189 if (optarg == nullptr)
2190 follow_mode = DEFAULT_FOLLOW_MODE;
2191 else
2192 follow_mode = XARGMATCH ("--follow", optarg,
2193 follow_mode_string, follow_mode_map);
2194 break;
2195
2196 case RETRY_OPTION:
2197 reopen_inaccessible_files = true;
2198 break;
2199
2200 case MAX_UNCHANGED_STATS_OPTION:
2201 /* --max-unchanged-stats=N */
2202 max_n_unchanged_stats_between_opens =
2203 xdectoumax (optarg, 0, UINTMAX_MAX, "",
2204 _("invalid maximum number of unchanged stats between opens"), 0);
2205 break;
2206
2207 case DISABLE_INOTIFY_OPTION:
2208 disable_inotify = true;
2209 break;
2210
2211 case PID_OPTION:
2212 if (nbpids == pids_alloc)
2213 pids = xpalloc (pids, &pids_alloc, 1,
2214 MIN (INT_MAX, PTRDIFF_MAX), sizeof *pids);
2215 pids[nbpids++] = xdectoumax (optarg, 0, PID_T_MAX, "",
2216 _("invalid PID"), 0);
2217 break;
2218
2219 case PRESUME_INPUT_PIPE_OPTION:
2220 presume_input_pipe = true;
2221 break;
2222
2223 case 'q':
2224 *header_mode = never;
2225 break;
2226
2227 case 's':
2228 {
2229 double s;
2230 if (! (xstrtod (optarg, nullptr, &s, cl_strtod) && 0 <= s))
2231 error (EXIT_FAILURE, 0,
2232 _("invalid number of seconds: %s"), quote (optarg));
2233 *sleep_interval = s;
2234 }
2235 break;
2236
2237 case 'v':
2238 *header_mode = always;
2239 break;
2240
2241 case 'z':
2242 line_end = '\0';
2243 break;
2244
2245 case_GETOPT_HELP_CHAR;
2246
2247 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
2248
2249 case '0': case '1': case '2': case '3': case '4':
2250 case '5': case '6': case '7': case '8': case '9':
2251 error (EXIT_FAILURE, 0, _("option used in invalid context -- %c"), c);
2252
2253 default:
2254 usage (EXIT_FAILURE);
2255 }
2256 }
2257
2258 if (reopen_inaccessible_files)
2259 {
2260 if (!forever)
2261 {
2262 reopen_inaccessible_files = false;
2263 error (0, 0, _("warning: --retry ignored; --retry is useful"
2264 " only when following"));
2265 }
2266 else if (follow_mode == Follow_descriptor)
2267 error (0, 0, _("warning: --retry only effective for the initial open"));
2268 }
2269
2270 if (nbpids && !forever)
2271 error (0, 0,
2272 _("warning: PID ignored; --pid=PID is useful only when following"));
2273 else if (nbpids && kill (pids[0], 0) != 0 && errno == ENOSYS)
2274 {
2275 error (0, 0, _("warning: --pid=PID is not supported on this system"));
2276 nbpids = 0;
2277 free (pids);
2278 }
2279 }
2280
2281 /* Mark as '.ignore'd each member of F that corresponds to a
2282 pipe or fifo, and return the number of non-ignored members. */
2283 static size_t
ignore_fifo_and_pipe(struct File_spec * f,size_t n_files)2284 ignore_fifo_and_pipe (struct File_spec *f, size_t n_files)
2285 {
2286 /* When there is no FILE operand and stdin is a pipe or FIFO
2287 POSIX requires that tail ignore the -f option.
2288 Since we allow multiple FILE operands, we extend that to say: with -f,
2289 ignore any "-" operand that corresponds to a pipe or FIFO. */
2290 size_t n_viable = 0;
2291
2292 for (size_t i = 0; i < n_files; i++)
2293 {
2294 bool is_a_fifo_or_pipe =
2295 (STREQ (f[i].name, "-")
2296 && !f[i].ignore
2297 && 0 <= f[i].fd
2298 && (S_ISFIFO (f[i].mode)
2299 || (HAVE_FIFO_PIPES != 1 && isapipe (f[i].fd))));
2300 if (is_a_fifo_or_pipe)
2301 {
2302 f[i].fd = -1;
2303 f[i].ignore = true;
2304 }
2305 else
2306 ++n_viable;
2307 }
2308
2309 return n_viable;
2310 }
2311
2312 int
main(int argc,char ** argv)2313 main (int argc, char **argv)
2314 {
2315 enum header_mode header_mode = multiple_files;
2316 bool ok = true;
2317 /* If from_start, the number of items to skip before printing; otherwise,
2318 the number of items at the end of the file to print. Although the type
2319 is signed, the value is never negative. */
2320 uintmax_t n_units = DEFAULT_N_LINES;
2321 size_t n_files;
2322 char **file;
2323 struct File_spec *F;
2324 size_t i;
2325 bool obsolete_option;
2326
2327 /* The number of seconds to sleep between iterations.
2328 During one iteration, every file name or descriptor is checked to
2329 see if it has changed. */
2330 double sleep_interval = 1.0;
2331
2332 initialize_main (&argc, &argv);
2333 set_program_name (argv[0]);
2334 setlocale (LC_ALL, "");
2335 bindtextdomain (PACKAGE, LOCALEDIR);
2336 textdomain (PACKAGE);
2337
2338 atexit (close_stdout);
2339
2340 have_read_stdin = false;
2341
2342 count_lines = true;
2343 forever = from_start = print_headers = false;
2344 line_end = '\n';
2345 obsolete_option = parse_obsolete_option (argc, argv, &n_units);
2346 argc -= obsolete_option;
2347 argv += obsolete_option;
2348 parse_options (argc, argv, &n_units, &header_mode, &sleep_interval);
2349
2350 /* To start printing with item N_UNITS from the start of the file, skip
2351 N_UNITS - 1 items. 'tail -n +0' is actually meaningless, but for Unix
2352 compatibility it's treated the same as 'tail -n +1'. */
2353 if (from_start)
2354 {
2355 if (n_units)
2356 --n_units;
2357 }
2358
2359 if (optind < argc)
2360 {
2361 n_files = argc - optind;
2362 file = argv + optind;
2363 }
2364 else
2365 {
2366 static char *dummy_stdin = (char *) "-";
2367 n_files = 1;
2368 file = &dummy_stdin;
2369 }
2370
2371 {
2372 bool found_hyphen = false;
2373
2374 for (i = 0; i < n_files; i++)
2375 if (STREQ (file[i], "-"))
2376 found_hyphen = true;
2377
2378 /* When following by name, there must be a name. */
2379 if (found_hyphen && follow_mode == Follow_name)
2380 error (EXIT_FAILURE, 0, _("cannot follow %s by name"), quoteaf ("-"));
2381
2382 /* When following forever, and not using simple blocking, warn if
2383 any file is '-' as the stats() used to check for input are ineffective.
2384 This is only a warning, since tail's output (before a failing seek,
2385 and that from any non-stdin files) might still be useful. */
2386 if (forever && found_hyphen)
2387 {
2388 struct stat in_stat;
2389 bool blocking_stdin;
2390 blocking_stdin = (!nbpids && follow_mode == Follow_descriptor
2391 && n_files == 1 && ! fstat (STDIN_FILENO, &in_stat)
2392 && ! S_ISREG (in_stat.st_mode));
2393
2394 if (! blocking_stdin && isatty (STDIN_FILENO))
2395 error (0, 0, _("warning: following standard input"
2396 " indefinitely is ineffective"));
2397 }
2398 }
2399
2400 /* Don't read anything if we'll never output anything. */
2401 if (! n_units && ! forever && ! from_start)
2402 return EXIT_SUCCESS;
2403
2404 F = xnmalloc (n_files, sizeof *F);
2405 for (i = 0; i < n_files; i++)
2406 F[i].name = file[i];
2407
2408 if (header_mode == always
2409 || (header_mode == multiple_files && n_files > 1))
2410 print_headers = true;
2411
2412 xset_binary_mode (STDOUT_FILENO, O_BINARY);
2413
2414 for (i = 0; i < n_files; i++)
2415 ok &= tail_file (&F[i], n_units);
2416
2417 if (forever && ignore_fifo_and_pipe (F, n_files))
2418 {
2419 /* If stdout is a fifo or pipe, then monitor it
2420 so that we exit if the reader goes away. */
2421 struct stat out_stat;
2422 if (fstat (STDOUT_FILENO, &out_stat) < 0)
2423 error (EXIT_FAILURE, errno, _("standard output"));
2424 monitor_output = (S_ISFIFO (out_stat.st_mode)
2425 || (HAVE_FIFO_PIPES != 1 && isapipe (STDOUT_FILENO)));
2426
2427 #if HAVE_INOTIFY
2428 /* tailable_stdin() checks if the user specifies stdin via "-",
2429 or implicitly by providing no arguments. If so, we won't use inotify.
2430 Technically, on systems with a working /dev/stdin, we *could*,
2431 but would it be worth it? Verifying that it's a real device
2432 and hooked up to stdin is not trivial, while reverting to
2433 non-inotify-based tail_forever is easy and portable.
2434
2435 any_remote_file() checks if the user has specified any
2436 files that reside on remote file systems. inotify is not used
2437 in this case because it would miss any updates to the file
2438 that were not initiated from the local system.
2439
2440 any_non_remote_file() checks if the user has specified any
2441 files that don't reside on remote file systems. inotify is not used
2442 if there are no open files, as we can't determine if those file
2443 will be on a remote file system.
2444
2445 any_symlinks() checks if the user has specified any symbolic links.
2446 inotify is not used in this case because it returns updated _targets_
2447 which would not match the specified names. If we tried to always
2448 use the target names, then we would miss changes to the symlink itself.
2449
2450 ok is false when one of the files specified could not be opened for
2451 reading. In this case and when following by descriptor,
2452 tail_forever_inotify() cannot be used (in its current implementation).
2453
2454 FIXME: inotify doesn't give any notification when a new
2455 (remote) file or directory is mounted on top a watched file.
2456 When follow_mode == Follow_name we would ideally like to detect that.
2457 Note if there is a change to the original file then we'll
2458 recheck it and follow the new file, or ignore it if the
2459 file has changed to being remote.
2460
2461 FIXME-maybe: inotify has a watch descriptor per inode, and hence with
2462 our current hash implementation will only --follow data for one
2463 of the names when multiple hardlinked files are specified, or
2464 for one name when a name is specified multiple times. */
2465 if (!disable_inotify && (tailable_stdin (F, n_files)
2466 || any_remote_file (F, n_files)
2467 || ! any_non_remote_file (F, n_files)
2468 || any_symlinks (F, n_files)
2469 || any_non_regular_fifo (F, n_files)
2470 || (!ok && follow_mode == Follow_descriptor)))
2471 disable_inotify = true;
2472
2473 if (!disable_inotify)
2474 {
2475 int wd = inotify_init ();
2476 if (0 <= wd)
2477 {
2478 /* Flush any output from tail_file, now, since
2479 tail_forever_inotify flushes only after writing,
2480 not before reading. */
2481 if (fflush (stdout) != 0)
2482 write_error ();
2483
2484 Hash_table *ht;
2485 tail_forever_inotify (wd, F, n_files, sleep_interval, &ht);
2486 hash_free (ht);
2487 close (wd);
2488 errno = 0;
2489 }
2490 error (0, errno, _("inotify cannot be used, reverting to polling"));
2491 }
2492 #endif
2493 disable_inotify = true;
2494 tail_forever (F, n_files, sleep_interval);
2495 }
2496
2497 if (have_read_stdin && close (STDIN_FILENO) < 0)
2498 error (EXIT_FAILURE, errno, "-");
2499 main_exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
2500 }
2501