/* tail -- output the last part of file(s) This is the tail utility
Copyright (C) 1989-2018 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */ The GNUv3 license
/* Can display any amount of data, unlike the Unix version, which uses
a fixed size buffer and therefore can only deliver a limited number
of lines.
Original version by Paul Rubin <phr@ocf.berkeley.edu>.
Extensions by David MacKenzie <djm@gnu.ai.mit.edu>.
tail -f for multiple files by Ian Lance Taylor <ian@airs.com>.
inotify back-end by Giuseppe Scrivano <gscrivano@gnu.org>. */
#include <config.h> Provides system specific information
#include <stdio.h> Provides standard I/O capability
#include <assert.h> ...!includes auto-comment...
#include <getopt.h> ...!includes auto-comment...
#include <sys/types.h> Provides system data types
#include <signal.h> ...!includes auto-comment...
#include "system.h" ...!includes auto-comment...
#include "argmatch.h" ...!includes auto-comment...
#include "c-strtod.h" ...!includes auto-comment...
#include "die.h" ...!includes auto-comment...
#include "error.h" ...!includes auto-comment...
#include "fcntl--.h" ...!includes auto-comment...
#include "isapipe.h" ...!includes auto-comment...
#include "posixver.h" ...!includes auto-comment...
#include "quote.h" ...!includes auto-comment...
#include "safe-read.h" ...!includes auto-comment...
#include "stat-size.h" ...!includes auto-comment...
#include "stat-time.h" ...!includes auto-comment...
#include "xbinary-io.h" ...!includes auto-comment...
#include "xdectoint.h" ...!includes auto-comment...
#include "xnanosleep.h" ...!includes auto-comment...
#include "xstrtol.h" ...!includes auto-comment...
#include "xstrtod.h" ...!includes auto-comment...
#if HAVE_INOTIFY Line 52
# include "hash.h" ...!includes auto-comment...
# include <sys/inotify.h> Line 54
/* 'select' is used by tail_forever_inotify. */
# include <sys/select.h> Line 56
/* inotify needs to know if a file is local. */
# include "fs.h" ...!includes auto-comment...
# include "fs-is-local.h" Line 60
# if HAVE_SYS_STATFS_H Line 61
# include <sys/statfs.h> ...!includes auto-comment...
# elif HAVE_SYS_VFS_H Line 63
# include <sys/vfs.h> ...!includes auto-comment...
# endif Line 65
#endif Line 66
/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "tail" Line 69
#define AUTHORS \ Line 71
proper_name ("Paul Rubin"), \ Line 72
proper_name ("David MacKenzie"), \ Line 73
proper_name ("Ian Lance Taylor"), \ Line 74
proper_name ("Jim Meyering") Line 75
/* Number of items to tail. */
#define DEFAULT_N_LINES 10 Line 78
/* Special values for dump_remainder's N_BYTES parameter. */
#define COPY_TO_EOF UINTMAX_MAX Line 81
#define COPY_A_BUFFER (UINTMAX_MAX - 1) Line 82
/* FIXME: make Follow_name the default? */
#define DEFAULT_FOLLOW_MODE Follow_descriptor Line 85
enum Follow_mode Line 87
{
/* Follow the name of each file: if the file is renamed, try to reopen
that name and track the end of the new file if/when it's recreated.
This is useful for tracking logs that are occasionally rotated. */
Follow_name = 1, Line 92
/* Follow each descriptor obtained upon opening a file.
That means we'll continue to follow the end of a file even after
it has been renamed or unlinked. */
Follow_descriptor = 2 Line 97
};
/* The types of files for which tail works. */
#define IS_TAILABLE_FILE_TYPE(Mode) \ Line 101
(S_ISREG (Mode) || S_ISFIFO (Mode) || S_ISSOCK (Mode) || S_ISCHR (Mode)) Line 102
static char const *const follow_mode_string[] = Line 104
{
"descriptor", "name", NULL Line 106
}; Block 2
static enum Follow_mode const follow_mode_map[] = Line 109
{
Follow_descriptor, Follow_name, Line 111
}; Block 3
struct File_spec Line 114
{
/* The actual file name, or "-" for stdin. */
char *name; Line 117
/* Attributes of the file the last time we checked. */
off_t size; Line 120
struct timespec mtime; Line 121
dev_t dev; Line 122
ino_t ino; Line 123
mode_t mode; Line 124
/* The specified name initially referred to a directory or some other
type for which tail isn't meaningful. Unlike for a permission problem
(tailable, below) once this is set, the name is not checked ever again. */
bool ignore; Line 129
/* See the description of fremote. */
bool remote; Line 132
/* A file is tailable if it exists, is readable, and is of type
IS_TAILABLE_FILE_TYPE. */
bool tailable; Line 136
/* File descriptor on which the file is open; -1 if it's not open. */
int fd; Line 139
/* The value of errno seen last time we checked this file. */
int errnum; Line 142
/* 1 if O_NONBLOCK is clear, 0 if set, -1 if not known. */
int blocking; Line 145
#if HAVE_INOTIFY Line 147
/* The watch descriptor used by inotify. */
int wd; Line 149
/* The parent directory watch descriptor. It is used only
* when Follow_name is used. */
int parent_wd; Line 153
/* Offset in NAME of the basename part. */
size_t basename_start; Line 156
#endif Line 157
/* See description of DEFAULT_MAX_N_... below. */
uintmax_t n_unchanged_stats; Line 160
};
/* Keep trying to open a file even if it is inaccessible when tail starts
or if it becomes inaccessible later -- useful only with -f. */
static bool reopen_inaccessible_files; Line 165
/* If true, interpret the numeric argument as the number of lines.
Otherwise, interpret it as the number of bytes. */
static bool count_lines; Line 169
/* Whether we follow the name of each file or the file descriptor
that is initially associated with each name. */
static enum Follow_mode follow_mode = Follow_descriptor; Line 173
/* If true, read from the ends of all specified files until killed. */
static bool forever; Line 176
/* If true, monitor output so we exit if pipe reader terminates. */
static bool monitor_output; Line 179
/* If true, count from start of file instead of end. */
static bool from_start; Line 182
/* If true, print filename headers. */
static bool print_headers; Line 185
/* Character to split lines by. */
static char line_end; Line 188
/* When to print the filename banners. */
enum header_mode Line 191
{
multiple_files, always, never Line 193
}; Block 5
/* When tailing a file by name, if there have been this many consecutive
iterations for which the file has not changed, then open/fstat
the file to determine if that file name is still associated with the
same device/inode-number pair as before. This option is meaningful only
when following by name. --max-unchanged-stats=N */
#define DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS 5 Line 201
static uintmax_t max_n_unchanged_stats_between_opens = Line 202
DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS; Line 203
/* The process ID of the process (presumably on the current host)
that is writing to all followed files. */
static pid_t pid; Line 207
/* True if we have ever read standard input. */
static bool have_read_stdin; Line 210
/* If nonzero, skip the is-regular-file test used to determine whether
to use the lseek optimization. Instead, use the more general (and
more expensive) code unconditionally. Intended solely for testing. */
static bool presume_input_pipe; Line 215
/* If nonzero then don't use inotify even if available. */
static bool disable_inotify; Line 218
/* For long options that have no equivalent short option, use a
non-character as a pseudo short option, starting with CHAR_MAX + 1. */
enum Line 222
{
RETRY_OPTION = CHAR_MAX + 1, Line 224
MAX_UNCHANGED_STATS_OPTION, Line 225
PID_OPTION, Line 226
PRESUME_INPUT_PIPE_OPTION, Line 227
LONG_FOLLOW_OPTION, Line 228
DISABLE_INOTIFY_OPTION Line 229
}; Block 6
static struct option const long_options[] = Line 232
{
{"bytes", required_argument, NULL, 'c'}, Line 234
{"follow", optional_argument, NULL, LONG_FOLLOW_OPTION}, Line 235
{"lines", required_argument, NULL, 'n'}, Line 236
{"max-unchanged-stats", required_argument, NULL, MAX_UNCHANGED_STATS_OPTION}, Line 237
{"-disable-inotify", no_argument, NULL, Line 238
DISABLE_INOTIFY_OPTION}, /* do not document */ Line 239
{"pid", required_argument, NULL, PID_OPTION}, Line 240
{"-presume-input-pipe", no_argument, NULL, Line 241
PRESUME_INPUT_PIPE_OPTION}, /* do not document */ Line 242
{"quiet", no_argument, NULL, 'q'}, Line 243
{"retry", no_argument, NULL, RETRY_OPTION}, Line 244
{"silent", no_argument, NULL, 'q'}, Line 245
{"sleep-interval", required_argument, NULL, 's'}, Line 246
{"verbose", no_argument, NULL, 'v'}, Line 247
{"zero-terminated", no_argument, NULL, 'z'}, Line 248
{GETOPT_HELP_OPTION_DECL}, Line 249
{GETOPT_VERSION_OPTION_DECL}, Line 250
{NULL, 0, NULL, 0} Line 251
}; Block 7
void Line 254
usage (int status) Line 255
{
if (status != EXIT_SUCCESS) Line 257
emit_try_help (); ...!common auto-comment...
else Line 259
{
printf (_("\ Line 261
Usage: %s [OPTION]... [FILE]...\n\ Line 262
"), Line 263
program_name); Line 264
printf (_("\ Line 265
Print the last %d lines of each FILE to standard output.\n\ Line 266
With more than one FILE, precede each with a header giving the file name.\n\ Line 267
"), DEFAULT_N_LINES); Line 268
emit_stdin_note (); ...!common auto-comment...
emit_mandatory_arg_note (); ...!common auto-comment...
fputs (_("\ Line 273
-c, --bytes=[+]NUM output the last NUM bytes; or use -c +NUM to\n\ Line 274
output starting with byte NUM of each file\n\ Line 275
"), stdout); Line 276
fputs (_("\ Line 277
-f, --follow[={name|descriptor}]\n\ Line 278
output appended data as the file grows;\n\ Line 279
an absent option argument means 'descriptor'\n\ Line 280
-F same as --follow=name --retry\n\ Line 281
"), stdout); Line 282
printf (_("\ Line 283
-n, --lines=[+]NUM output the last NUM lines, instead of the last %d;\n\Line 284
or use -n +NUM to output starting with line NUM\n\ Line 285
--max-unchanged-stats=N\n\ Line 286
with --follow=name, reopen a FILE which has not\n\ Line 287
changed size after N (default %d) iterations\n\ Line 288
to see if it has been unlinked or renamed\n\ Line 289
(this is the usual case of rotated log files);\n\ Line 290
with inotify, this option is rarely useful\n\ Line 291
"), Line 292
DEFAULT_N_LINES, Line 293
DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS Line 294
);
fputs (_("\ Line 296
--pid=PID with -f, terminate after process ID, PID dies\n\ Line 297
-q, --quiet, --silent never output headers giving file names\n\ Line 298
--retry keep trying to open a file if it is inaccessible\n\ Line 299
"), stdout); Line 300
fputs (_("\ Line 301
-s, --sleep-interval=N with -f, sleep for approximately N seconds\n\ Line 302
(default 1.0) between iterations;\n\ Line 303
with inotify and --pid=P, check process P at\n\ Line 304
least once every N seconds\n\ Line 305
-v, --verbose always output headers giving file names\n\ Line 306
"), stdout); Line 307
fputs (_("\ Line 308
-z, --zero-terminated line delimiter is NUL, not newline\n\ Line 309
"), stdout); Line 310
fputs (HELP_OPTION_DESCRIPTION, stdout); Line 311
fputs (VERSION_OPTION_DESCRIPTION, stdout); Line 312
fputs (_("\ Line 313
\n\
NUM may have a multiplier suffix:\n\ Line 315
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\ Line 316
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.\n\ Line 317
Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\ Line 318
\n\
"), stdout); Line 320
fputs (_("\ Line 321
With --follow (-f), tail defaults to following the file descriptor, which\n\ Line 322
means that even if a tail'ed file is renamed, tail will continue to track\n\ Line 323
its end. This default behavior is not desirable when you really want to\n\ Line 324
track the actual name of the file, not the file descriptor (e.g., log\n\ Line 325
rotation). Use --follow=name in that case. That causes tail to track the\n\ Line 326
named file in a way that accommodates renaming, removal and creation.\n\ Line 327
"), stdout); Line 328
emit_ancillary_info (PROGRAM_NAME); Line 329
}
exit (status); Line 331
} Block 8
/* If the output has gone away, then terminate
as we would if we had written to this output. */
static void Line 336
check_output_alive (void) Line 337
{
if (! monitor_output) Line 339
return; Line 340
struct timeval delay; Line 342
delay.tv_sec = delay.tv_usec = 0; Line 343
fd_set rfd; Line 345
FD_ZERO (&rfd); Line 346
FD_SET (STDOUT_FILENO, &rfd); Line 347
/* readable event on STDOUT is equivalent to POLLERR,
and implies an error condition on output like broken pipe. */
if (select (STDOUT_FILENO + 1, &rfd, NULL, NULL, &delay) == 1) Line 351
raise (SIGPIPE); Line 352
} Block 9
static bool Line 355
valid_file_spec (struct File_spec const *f) Line 356
{
/* Exactly one of the following subexpressions must be true. */
return ((f->fd == -1) ^ (f->errnum == 0)); Line 359
}
static char const * Line 362
pretty_name (struct File_spec const *f) Line 363
{
return (STREQ (f->name, "-") ? _("standard input") : f->name); Line 365
} Block 11
/* Record a file F with descriptor FD, size SIZE, status ST, and
blocking status BLOCKING. */
static void Line 371
record_open_fd (struct File_spec *f, int fd, Line 372
off_t size, struct stat const *st, Line 373
int blocking) Line 374
{
f->fd = fd; Line 376
f->size = size; Line 377
f->mtime = get_stat_mtime (st); Line 378
f->dev = st->st_dev; Line 379
f->ino = st->st_ino; Line 380
f->mode = st->st_mode; Line 381
f->blocking = blocking; Line 382
f->n_unchanged_stats = 0; Line 383
f->ignore = false; Line 384
} Block 12
/* Close the file with descriptor FD and name FILENAME. */
static void Line 389
close_fd (int fd, const char *filename) Line 390
{
if (fd != -1 && fd != STDIN_FILENO && close (fd)) Line 392...!syscalls auto-comment...
{
error (0, errno, _("closing %s (fd=%d)"), quoteaf (filename), fd); Line 394
}
} Block 13
static void Line 398
write_header (const char *pretty_filename) Line 399
{
static bool first_file = true; Line 401
printf ("%s==> %s <==\n", (first_file ? "" : "\n"), pretty_filename); Line 403
first_file = false; Line 404
} Block 14
/* Write N_BYTES from BUFFER to stdout.
Exit immediately on error with a single diagnostic. */
static void Line 410
xwrite_stdout (char const *buffer, size_t n_bytes) Line 411
{
if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) < n_bytes) Line 413...!syscalls auto-comment...
{
clearerr (stdout); /* To avoid redundant close_stdout diagnostic. */ Line 415
die (EXIT_FAILURE, errno, _("error writing %s"), Line 416
quoteaf ("standard output")); Line 417
}
} Block 15
/* Read and output N_BYTES of file PRETTY_FILENAME starting at the current
position in FD. If N_BYTES is COPY_TO_EOF, then copy until end of file.
If N_BYTES is COPY_A_BUFFER, then copy at most one buffer's worth.
Return the number of bytes read from the file. */
static uintmax_t Line 426
dump_remainder (bool want_header, const char *pretty_filename, int fd, Line 427
uintmax_t n_bytes) Line 428
{
uintmax_t n_written; Line 430
uintmax_t n_remaining = n_bytes; Line 431
n_written = 0; Line 433
while (1) Line 434
{
char buffer[BUFSIZ]; Line 436
size_t n = MIN (n_remaining, BUFSIZ); Line 437
size_t bytes_read = safe_read (fd, buffer, n); Line 438...!syscalls auto-comment...
if (bytes_read == SAFE_READ_ERROR) Line 439
{
if (errno != EAGAIN) Line 441
die (EXIT_FAILURE, errno, _("error reading %s"), Line 442
quoteaf (pretty_filename)); Line 443
break; Line 444
}
if (bytes_read == 0) Line 446
break; Line 447
if (want_header) Line 448
{
write_header (pretty_filename); Line 450
want_header = false; Line 451
}
xwrite_stdout (buffer, bytes_read); Line 453
n_written += bytes_read; Line 454
if (n_bytes != COPY_TO_EOF) Line 455
{
n_remaining -= bytes_read; Line 457
if (n_remaining == 0 || n_bytes == COPY_A_BUFFER) Line 458
break; Line 459
}
}
return n_written; Line 463
} Block 16
/* Call lseek with the specified arguments, where file descriptor FD
corresponds to the file, FILENAME.
Give a diagnostic and exit nonzero if lseek fails.
Otherwise, return the resulting offset. */
static off_t Line 471
xlseek (int fd, off_t offset, int whence, char const *filename) Line 472
{
off_t new_offset = lseek (fd, offset, whence); Line 474
char buf[INT_BUFSIZE_BOUND (offset)]; Line 475
char *s; Line 476
if (0 <= new_offset) Line 478
return new_offset; Line 479
s = offtostr (offset, buf); Line 481
switch (whence) Line 482
{
case SEEK_SET: Line 484
error (0, errno, _("%s: cannot seek to offset %s"), Line 485
quotef (filename), s); Line 486
break; Line 487
case SEEK_CUR: Line 488
error (0, errno, _("%s: cannot seek to relative offset %s"), Line 489
quotef (filename), s); Line 490
break; Line 491
case SEEK_END: Line 492
error (0, errno, _("%s: cannot seek to end-relative offset %s"), Line 493
quotef (filename), s); Line 494
break; Line 495
default: Line 496
abort (); ...!common auto-comment...
}
exit (EXIT_FAILURE); Line 500
} Block 17
/* Print the last N_LINES lines from the end of file FD.
Go backward through the file, reading 'BUFSIZ' bytes at a time (except
probably the first), until we hit the start of the file or have
read NUMBER newlines.
START_POS is the starting position of the read pointer for the file
associated with FD (may be nonzero).
END_POS is the file offset of EOF (one larger than offset of last byte).
Return true if successful. */
static bool Line 512
file_lines (const char *pretty_filename, int fd, uintmax_t n_lines, Line 513
off_t start_pos, off_t end_pos, uintmax_t *read_pos) Line 514
{
char buffer[BUFSIZ]; Line 516
size_t bytes_read; Line 517
off_t pos = end_pos; Line 518
if (n_lines == 0) Line 520
return true; Line 521
/* Set 'bytes_read' to the size of the last, probably partial, buffer;
0 < 'bytes_read' <= 'BUFSIZ'. */
bytes_read = (pos - start_pos) % BUFSIZ; Line 525
if (bytes_read == 0) Line 526
bytes_read = BUFSIZ; Line 527
/* Make 'pos' a multiple of 'BUFSIZ' (0 if the file is short), so that all
reads will be on block boundaries, which might increase efficiency. */
pos -= bytes_read; Line 530
xlseek (fd, pos, SEEK_SET, pretty_filename); Line 531
bytes_read = safe_read (fd, buffer, bytes_read); Line 532...!syscalls auto-comment...
if (bytes_read == SAFE_READ_ERROR) Line 533
{
error (0, errno, _("error reading %s"), quoteaf (pretty_filename)); Line 535
return false; Line 536
}
*read_pos = pos + bytes_read; Line 538
/* Count the incomplete line on files that don't end with a newline. */
if (bytes_read && buffer[bytes_read - 1] != line_end) Line 541
--n_lines; Line 542
do
{
/* Scan backward, counting the newlines in this bufferfull. */
size_t n = bytes_read; Line 548
while (n) Line 549
{
char const *nl; Line 551
nl = memrchr (buffer, line_end, n); Line 552
if (nl == NULL) Line 553
break; Line 554
n = nl - buffer; Line 555
if (n_lines-- == 0) Line 556
{
/* If this newline isn't the last character in the buffer,
output the part that is after it. */
if (n != bytes_read - 1) Line 560
xwrite_stdout (nl + 1, bytes_read - (n + 1)); Line 561
*read_pos += dump_remainder (false, pretty_filename, fd, Line 562
end_pos - (pos + bytes_read)); Line 563
return true; Line 564
}
}
/* Not enough newlines in that bufferfull. */
if (pos == start_pos) Line 569
{
/* Not enough lines in the file; print everything from
start_pos to the end. */
xlseek (fd, start_pos, SEEK_SET, pretty_filename); Line 573
*read_pos = start_pos + dump_remainder (false, pretty_filename, fd, Line 574
end_pos); Line 575
return true; Line 576
}
pos -= BUFSIZ; Line 578
xlseek (fd, pos, SEEK_SET, pretty_filename); Line 579
bytes_read = safe_read (fd, buffer, BUFSIZ); Line 581...!syscalls auto-comment...
if (bytes_read == SAFE_READ_ERROR) Line 582
{
error (0, errno, _("error reading %s"), quoteaf (pretty_filename)); Line 584
return false; Line 585
}
*read_pos = pos + bytes_read; Line 588
}
while (bytes_read > 0); Line 590
return true; Line 592
} Block 18
/* Print the last N_LINES lines from the end of the standard input,
open for reading as pipe FD.
Buffer the text as a linked list of LBUFFERs, adding them as needed.
Return true if successful. */
static bool Line 600
pipe_lines (const char *pretty_filename, int fd, uintmax_t n_lines, Line 601
uintmax_t *read_pos) Line 602
{
struct linebuffer Line 604
{
char buffer[BUFSIZ]; Line 606
size_t nbytes; Line 607
size_t nlines; Line 608
struct linebuffer *next; Line 609
};
typedef struct linebuffer LBUFFER; Line 611
LBUFFER *first, *last, *tmp; Line 612
size_t total_lines = 0; /* Total number of newlines in all buffers. */ Line 613
bool ok = true; Line 614
size_t n_read; /* Size in bytes of most recent read */ Line 615
first = last = xmalloc (sizeof (LBUFFER)); Line 617
first->nbytes = first->nlines = 0; Line 618
first->next = NULL; Line 619
tmp = xmalloc (sizeof (LBUFFER)); Line 620
/* Input is always read into a fresh buffer. */
while (1) Line 623
{
n_read = safe_read (fd, tmp->buffer, BUFSIZ); Line 625...!syscalls auto-comment...
if (n_read == 0 || n_read == SAFE_READ_ERROR) Line 626
break; Line 627
tmp->nbytes = n_read; Line 628
*read_pos += n_read; Line 629
tmp->nlines = 0; Line 630
tmp->next = NULL; Line 631
/* Count the number of newlines just read. */
{
char const *buffer_end = tmp->buffer + n_read; Line 635
char const *p = tmp->buffer; Line 636
while ((p = memchr (p, line_end, buffer_end - p))) Line 637
{
++p; Line 639
++tmp->nlines; Line 640
}
}
total_lines += tmp->nlines; Line 643
/* If there is enough room in the last buffer read, just append the new
one to it. This is because when reading from a pipe, 'n_read' can
often be very small. */
if (tmp->nbytes + last->nbytes < BUFSIZ) Line 648
{
memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes); Line 650
last->nbytes += tmp->nbytes; Line 651
last->nlines += tmp->nlines; Line 652
}
else Line 654
{
/* If there's not enough room, link the new buffer onto the end of
the list, then either free up the oldest buffer for the next
read if that would leave enough lines, or else malloc a new one.
Some compaction mechanism is possible but probably not
worthwhile. */
last = last->next = tmp; Line 661
if (total_lines - first->nlines > n_lines) Line 662
{
tmp = first; Line 664
total_lines -= first->nlines; Line 665
first = first->next; Line 666
}
else Line 668
tmp = xmalloc (sizeof (LBUFFER)); Line 669
}
}
free (tmp); Line 673
if (n_read == SAFE_READ_ERROR) Line 675
{
error (0, errno, _("error reading %s"), quoteaf (pretty_filename)); Line 677
ok = false; Line 678
goto free_lbuffers; Line 679
}
/* If the file is empty, then bail out. */
if (last->nbytes == 0) Line 683
goto free_lbuffers; Line 684
/* This prevents a core dump when the pipe contains no newlines. */
if (n_lines == 0) Line 687
goto free_lbuffers; Line 688
/* Count the incomplete line on files that don't end with a newline. */
if (last->buffer[last->nbytes - 1] != line_end) Line 691
{
++last->nlines; Line 693
++total_lines; Line 694
}
/* Run through the list, printing lines. First, skip over unneeded
buffers. */
for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next) Line 699
total_lines -= tmp->nlines; Line 700
/* Find the correct beginning, then print the rest of the file. */
{
char const *beg = tmp->buffer; Line 704
char const *buffer_end = tmp->buffer + tmp->nbytes; Line 705
if (total_lines > n_lines) Line 706
{
/* Skip 'total_lines' - 'n_lines' newlines. We made sure that
'total_lines' - 'n_lines' <= 'tmp->nlines'. */
size_t j; Line 710
for (j = total_lines - n_lines; j; --j) Line 711
{
beg = memchr (beg, line_end, buffer_end - beg); Line 713
assert (beg); Line 714
++beg; Line 715
}
}
xwrite_stdout (beg, buffer_end - beg); Line 719
}
for (tmp = tmp->next; tmp; tmp = tmp->next) Line 722
xwrite_stdout (tmp->buffer, tmp->nbytes); Line 723
free_lbuffers: Line 725
while (first) Line 726
{
tmp = first->next; Line 728
free (first); Line 729
first = tmp; Line 730
}
return ok; Line 732
} Block 19
/* Print the last N_BYTES characters from the end of pipe FD.
This is a stripped down version of pipe_lines.
Return true if successful. */
static bool Line 739
pipe_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes, Line 740
uintmax_t *read_pos) Line 741
{
struct charbuffer Line 743
{
char buffer[BUFSIZ]; Line 745
size_t nbytes; Line 746
struct charbuffer *next; Line 747
};
typedef struct charbuffer CBUFFER; Line 749
CBUFFER *first, *last, *tmp; Line 750
size_t i; /* Index into buffers. */ Line 751
size_t total_bytes = 0; /* Total characters in all buffers. */ Line 752
bool ok = true; Line 753
size_t n_read; Line 754
first = last = xmalloc (sizeof (CBUFFER)); Line 756
first->nbytes = 0; Line 757
first->next = NULL; Line 758
tmp = xmalloc (sizeof (CBUFFER)); Line 759
/* Input is always read into a fresh buffer. */
while (1) Line 762
{
n_read = safe_read (fd, tmp->buffer, BUFSIZ); Line 764...!syscalls auto-comment...
if (n_read == 0 || n_read == SAFE_READ_ERROR) Line 765
break; Line 766
*read_pos += n_read; Line 767
tmp->nbytes = n_read; Line 768
tmp->next = NULL; Line 769
total_bytes += tmp->nbytes; Line 771
/* If there is enough room in the last buffer read, just append the new
one to it. This is because when reading from a pipe, 'nbytes' can
often be very small. */
if (tmp->nbytes + last->nbytes < BUFSIZ) Line 775
{
memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes); Line 777
last->nbytes += tmp->nbytes; Line 778
}
else Line 780
{
/* If there's not enough room, link the new buffer onto the end of
the list, then either free up the oldest buffer for the next
read if that would leave enough characters, or else malloc a new
one. Some compaction mechanism is possible but probably not
worthwhile. */
last = last->next = tmp; Line 787
if (total_bytes - first->nbytes > n_bytes) Line 788
{
tmp = first; Line 790
total_bytes -= first->nbytes; Line 791
first = first->next; Line 792
}
else Line 794
{
tmp = xmalloc (sizeof (CBUFFER)); Line 796
}
}
}
free (tmp); Line 801
if (n_read == SAFE_READ_ERROR) Line 803
{
error (0, errno, _("error reading %s"), quoteaf (pretty_filename)); Line 805
ok = false; Line 806
goto free_cbuffers; Line 807
}
/* Run through the list, printing characters. First, skip over unneeded
buffers. */
for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next) Line 812
total_bytes -= tmp->nbytes; Line 813
/* Find the correct beginning, then print the rest of the file.
We made sure that 'total_bytes' - 'n_bytes' <= 'tmp->nbytes'. */
if (total_bytes > n_bytes) Line 817
i = total_bytes - n_bytes; Line 818
else Line 819
i = 0; Line 820
xwrite_stdout (&tmp->buffer[i], tmp->nbytes - i); Line 821
for (tmp = tmp->next; tmp; tmp = tmp->next) Line 823
xwrite_stdout (tmp->buffer, tmp->nbytes); Line 824
free_cbuffers: Line 826
while (first) Line 827
{
tmp = first->next; Line 829
free (first); Line 830
first = tmp; Line 831
}
return ok; Line 833
} Block 20
/* Skip N_BYTES characters from the start of pipe FD, and print
any extra characters that were read beyond that.
Return 1 on error, 0 if ok, -1 if EOF. */
static int Line 840
start_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes, Line 841
uintmax_t *read_pos) Line 842
{
char buffer[BUFSIZ]; Line 844
while (0 < n_bytes) Line 846
{
size_t bytes_read = safe_read (fd, buffer, BUFSIZ); Line 848...!syscalls auto-comment...
if (bytes_read == 0) Line 849
return -1; Line 850
if (bytes_read == SAFE_READ_ERROR) Line 851
{
error (0, errno, _("error reading %s"), quoteaf (pretty_filename)); Line 853
return 1; Line 854
}
*read_pos += bytes_read; Line 856
if (bytes_read <= n_bytes) Line 857
n_bytes -= bytes_read; Line 858
else Line 859
{
size_t n_remaining = bytes_read - n_bytes; Line 861
if (n_remaining) Line 862
xwrite_stdout (&buffer[n_bytes], n_remaining); Line 863
break; Line 864
}
}
return 0; Line 868
} Block 21
/* Skip N_LINES lines at the start of file or pipe FD, and print
any extra characters that were read beyond that.
Return 1 on error, 0 if ok, -1 if EOF. */
static int Line 875
start_lines (const char *pretty_filename, int fd, uintmax_t n_lines, Line 876
uintmax_t *read_pos) Line 877
{
if (n_lines == 0) Line 879
return 0; Line 880
while (1) Line 882
{
char buffer[BUFSIZ]; Line 884
size_t bytes_read = safe_read (fd, buffer, BUFSIZ); Line 885...!syscalls auto-comment...
if (bytes_read == 0) /* EOF */ Line 886
return -1; Line 887
if (bytes_read == SAFE_READ_ERROR) /* error */ Line 888
{
error (0, errno, _("error reading %s"), quoteaf (pretty_filename)); Line 890
return 1; Line 891
}
char *buffer_end = buffer + bytes_read; Line 894
*read_pos += bytes_read; Line 896
char *p = buffer; Line 898
while ((p = memchr (p, line_end, buffer_end - p))) Line 899
{
++p; Line 901
if (--n_lines == 0) Line 902
{
if (p < buffer_end) Line 904
xwrite_stdout (p, buffer_end - p); Line 905
return 0; Line 906
}
}
}
} Block 22
/* Return false when FD is open on a file residing on a local file system.
If fstatfs fails, give a diagnostic and return true.
If fstatfs cannot be called, return true. */
static bool Line 915
fremote (int fd, const char *name) Line 916
{
bool remote = true; /* be conservative (poll by default). */ Line 918
#if HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE && defined __linux__ Line 920
struct statfs buf; Line 921
int err = fstatfs (fd, &buf); Line 922
if (err != 0) Line 923
{
/* On at least linux-2.6.38, fstatfs fails with ENOSYS when FD
is open on a pipe. Treat that like a remote file. */
if (errno != ENOSYS) Line 927
error (0, errno, _("cannot determine location of %s. " Line 928
"reverting to polling"), quoteaf (name)); Line 929
}
else Line 931
{
switch (is_local_fs_type (buf.f_type)) Line 933
{
case 0: Line 935
break; Line 936
case -1: Line 937
/* Treat unrecognized file systems as "remote", so caller polls.
Note README-release has instructions for syncing the internal
list with the latest Linux kernel file system constants. */
break; Line 941
case 1: Line 942
remote = false; Line 943
break; Line 944
default: Line 945
assert (!"unexpected return value from is_local_fs_type"); Line 946
}
}
#endif Line 949
return remote; Line 951
} Block 23
/* open/fstat F->name and handle changes. */
static void Line 955
recheck (struct File_spec *f, bool blocking) Line 956
{
struct stat new_stats; Line 958
bool ok = true; Line 959
bool is_stdin = (STREQ (f->name, "-")); Line 960
bool was_tailable = f->tailable; Line 961
int prev_errnum = f->errnum; Line 962
bool new_file; Line 963
int fd = (is_stdin Line 964
? STDIN_FILENO Line 965
: open (f->name, O_RDONLY | (blocking ? 0 : O_NONBLOCK))); Line 966...!syscalls auto-comment...
assert (valid_file_spec (f)); Line 968
/* If the open fails because the file doesn't exist,
then mark the file as not tailable. */
f->tailable = !(reopen_inaccessible_files && fd == -1); Line 972
if (! disable_inotify && ! lstat (f->name, &new_stats) Line 974...!syscalls auto-comment...
&& S_ISLNK (new_stats.st_mode)) Line 975
{
/* Diagnose the edge case where a regular file is changed
to a symlink. We avoid inotify with symlinks since
it's awkward to match between symlink name and target. */
ok = false; Line 980
f->errnum = -1; Line 981
f->ignore = true; Line 982
error (0, 0, _("%s has been replaced with an untailable symbolic link"), Line 984
quoteaf (pretty_name (f))); Line 985
}
else if (fd == -1 || fstat (fd, &new_stats) < 0) Line 987...!syscalls auto-comment......!syscalls auto-comment...
{
ok = false; Line 989
f->errnum = errno; Line 990
if (!f->tailable) Line 991
{
if (was_tailable) Line 993
{
/* FIXME-maybe: detect the case in which the file first becomes
unreadable (perms), and later becomes readable again and can
be seen to be the same file (dev/ino). Otherwise, tail prints
the entire contents of the file when it becomes readable. */
error (0, f->errnum, _("%s has become inaccessible"), Line 999
quoteaf (pretty_name (f))); Line 1000
}
else Line 1002
{
/* say nothing... it's still not tailable */
}
}
else if (prev_errnum != errno) Line 1007
error (0, errno, "%s", quotef (pretty_name (f))); Line 1008
}
else if (!IS_TAILABLE_FILE_TYPE (new_stats.st_mode)) Line 1010
{
ok = false; Line 1012
f->errnum = -1; Line 1013
f->tailable = false; Line 1014
f->ignore = ! (reopen_inaccessible_files && follow_mode == Follow_name); Line 1015
if (was_tailable || prev_errnum != f->errnum) Line 1016
error (0, 0, _("%s has been replaced with an untailable file%s"), Line 1017
quoteaf (pretty_name (f)), Line 1018
f->ignore ? _("; giving up on this name") : ""); Line 1019
}
else if ((f->remote = fremote (fd, pretty_name (f))) && ! disable_inotify) Line 1021
{
ok = false; Line 1023
f->errnum = -1; Line 1024
error (0, 0, _("%s has been replaced with an untailable remote file"), Line 1025
quoteaf (pretty_name (f))); Line 1026
f->ignore = true; Line 1027
f->remote = true; Line 1028
}
else Line 1030
{
f->errnum = 0; Line 1032
}
new_file = false; Line 1035
if (!ok) Line 1036
{
close_fd (fd, pretty_name (f)); Line 1038
close_fd (f->fd, pretty_name (f)); Line 1039
f->fd = -1; Line 1040
}
else if (prev_errnum && prev_errnum != ENOENT) Line 1042
{
new_file = true; Line 1044
assert (f->fd == -1); Line 1045
error (0, 0, _("%s has become accessible"), quoteaf (pretty_name (f))); Line 1046
}
else if (f->fd == -1) Line 1048
{
/* A new file even when inodes haven't changed as <dev,inode>
pairs can be reused, and we know the file was missing
on the previous iteration. Note this also means the file
is redisplayed in --follow=name mode if renamed away from
and back to a monitored name. */
new_file = true; Line 1055
error (0, 0, Line 1057
_("%s has appeared; following new file"), Line 1058
quoteaf (pretty_name (f))); Line 1059
}
else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev) Line 1061
{
/* File has been replaced (e.g., via log rotation) --
tail the new one. */
new_file = true; Line 1065
error (0, 0, Line 1067
_("%s has been replaced; following new file"), Line 1068
quoteaf (pretty_name (f))); Line 1069
/* Close the old one. */
close_fd (f->fd, pretty_name (f)); Line 1072
}
else Line 1075
{
/* No changes detected, so close new fd. */
close_fd (fd, pretty_name (f)); Line 1078
}
/* FIXME: When a log is rotated, daemons tend to log to the
old file descriptor until the new file is present and
the daemon is sent a signal. Therefore tail may miss entries
being written to the old file. Perhaps we should keep
the older file open and continue to monitor it until
data is written to a new file. */
if (new_file) Line 1087
{
/* Start at the beginning of the file. */
record_open_fd (f, fd, 0, &new_stats, (is_stdin ? -1 : blocking)); Line 1090
xlseek (fd, 0, SEEK_SET, pretty_name (f)); Line 1091
}
} Block 24
/* Return true if any of the N_FILES files in F are live, i.e., have
open file descriptors, or should be checked again (see --retry).
When following descriptors, checking should only continue when any
of the files is not yet ignored. */
static bool Line 1100
any_live_files (const struct File_spec *f, size_t n_files) Line 1101
{
/* In inotify mode, ignore may be set for files
which may later be replaced with new files.
So always consider files live in -F mode. */
if (reopen_inaccessible_files && follow_mode == Follow_name) Line 1106
return true; Line 1107
for (size_t i = 0; i < n_files; i++) Line 1109
{
if (0 <= f[i].fd) Line 1111
return true; Line 1112
else Line 1113
{
if (! f[i].ignore && reopen_inaccessible_files) Line 1115
return true; Line 1116
}
}
return false; Line 1120
}
/* Tail N_FILES files forever, or until killed.
The pertinent information for each file is stored in an entry of F.
Loop over each of them, doing an fstat to see if they have changed size,
and an occasional open/fstat to see if any dev/ino pair has changed.
If none of them have changed size in one iteration, sleep for a
while and try again. Continue until the user interrupts us. */
static void Line 1130
tail_forever (struct File_spec *f, size_t n_files, double sleep_interval) Line 1131
{
/* Use blocking I/O as an optimization, when it's easy. */
bool blocking = (pid == 0 && follow_mode == Follow_descriptor Line 1134
&& n_files == 1 && f[0].fd != -1 && ! S_ISREG (f[0].mode)); Line 1135
size_t last; Line 1136
bool writer_is_dead = false; Line 1137
last = n_files - 1; Line 1139
while (1) Line 1141
{
size_t i; Line 1143
bool any_input = false; Line 1144
for (i = 0; i < n_files; i++) Line 1146
{
int fd; Line 1148
char const *name; Line 1149
mode_t mode; Line 1150
struct stat stats; Line 1151
uintmax_t bytes_read; Line 1152
if (f[i].ignore) Line 1154
continue; Line 1155
if (f[i].fd < 0) Line 1157
{
recheck (&f[i], blocking); Line 1159
continue; Line 1160
}
fd = f[i].fd; Line 1163
name = pretty_name (&f[i]); Line 1164
mode = f[i].mode; Line 1165
if (f[i].blocking != blocking) Line 1167
{
int old_flags = fcntl (fd, F_GETFL); Line 1169...!syscalls auto-comment...
int new_flags = old_flags | (blocking ? 0 : O_NONBLOCK); Line 1170
if (old_flags < 0 Line 1171
|| (new_flags != old_flags Line 1172
&& fcntl (fd, F_SETFL, new_flags) == -1)) Line 1173...!syscalls auto-comment...
{
/* Don't update f[i].blocking if fcntl fails. */
if (S_ISREG (f[i].mode) && errno == EPERM) Line 1176
{
/* This happens when using tail -f on a file with
the append-only attribute. */
}
else Line 1181
die (EXIT_FAILURE, errno, Line 1182
_("%s: cannot change nonblocking mode"), Line 1183
quotef (name)); Line 1184
}
else Line 1186
f[i].blocking = blocking; Line 1187
}
if (!f[i].blocking) Line 1190
{
if (fstat (fd, &stats) != 0) Line 1192...!syscalls auto-comment......!syscalls auto-comment...
{
f[i].fd = -1; Line 1194
f[i].errnum = errno; Line 1195
error (0, errno, "%s", quotef (name)); Line 1196
close (fd); /* ignore failure */ Line 1197...!syscalls auto-comment...
continue; Line 1198
}
if (f[i].mode == stats.st_mode Line 1201
&& (! S_ISREG (stats.st_mode) || f[i].size == stats.st_size) Line 1202
&& timespec_cmp (f[i].mtime, get_stat_mtime (&stats)) == 0) Line 1203
{
if ((max_n_unchanged_stats_between_opens Line 1205
<= f[i].n_unchanged_stats++) Line 1206
&& follow_mode == Follow_name) Line 1207
{
recheck (&f[i], f[i].blocking); Line 1209
f[i].n_unchanged_stats = 0; Line 1210
}
continue; Line 1212
}
/* This file has changed. Print out what we can, and
then keep looping. */
f[i].mtime = get_stat_mtime (&stats); Line 1218
f[i].mode = stats.st_mode; Line 1219
/* reset counter */
f[i].n_unchanged_stats = 0; Line 1222
/* XXX: This is only a heuristic, as the file may have also
been truncated and written to if st_size >= size
(in which case we ignore new data <= size). */
if (S_ISREG (mode) && stats.st_size < f[i].size) Line 1227
{
error (0, 0, _("%s: file truncated"), quotef (name)); Line 1229
/* Assume the file was truncated to 0,
and therefore output all "new" data. */
xlseek (fd, 0, SEEK_SET, name); Line 1232
f[i].size = 0; Line 1233
}
if (i != last) Line 1236
{
if (print_headers) Line 1238
write_header (name); Line 1239
last = i; Line 1240
}
}
/* Don't read more than st_size on networked file systems
because it was seen on glusterfs at least, that st_size
may be smaller than the data read on a _subsequent_ stat call. */
uintmax_t bytes_to_read; Line 1247
if (f[i].blocking) Line 1248
bytes_to_read = COPY_A_BUFFER; Line 1249
else if (S_ISREG (mode) && f[i].remote) Line 1250
bytes_to_read = stats.st_size - f[i].size; Line 1251
else Line 1252
bytes_to_read = COPY_TO_EOF; Line 1253
bytes_read = dump_remainder (false, name, fd, bytes_to_read); Line 1255
any_input |= (bytes_read != 0); Line 1257
f[i].size += bytes_read; Line 1258
}
if (! any_live_files (f, n_files)) Line 1261
{
error (0, 0, _("no files remaining")); Line 1263
break; Line 1264
}
if ((!any_input || blocking) && fflush (stdout) != 0) Line 1267
die (EXIT_FAILURE, errno, _("write error")); Line 1268
check_output_alive (); Line 1270
/* If nothing was read, sleep and/or check for dead writers. */
if (!any_input) Line 1273
{
if (writer_is_dead) Line 1275
break; Line 1276
/* Once the writer is dead, read the files once more to
avoid a race condition. */
writer_is_dead = (pid != 0 Line 1280
&& kill (pid, 0) != 0 Line 1281
/* Handle the case in which you cannot send a
signal to the writer, so kill fails and sets
errno to EPERM. */
&& errno != EPERM); Line 1285
if (!writer_is_dead && xnanosleep (sleep_interval)) Line 1287
die (EXIT_FAILURE, errno, _("cannot read realtime clock")); Line 1288
}
}
}
#if HAVE_INOTIFY Line 1294
/* Return true if any of the N_FILES files in F is remote, i.e., has
an open file descriptor and is on a network file system. */
static bool Line 1299
any_remote_file (const struct File_spec *f, size_t n_files) Line 1300
{
for (size_t i = 0; i < n_files; i++) Line 1302
if (0 <= f[i].fd && f[i].remote) Line 1303
return true; Line 1304
return false; Line 1305
} Block 27
/* Return true if any of the N_FILES files in F is non remote, i.e., has
an open file descriptor and is not on a network file system. */
static bool Line 1311
any_non_remote_file (const struct File_spec *f, size_t n_files) Line 1312
{
for (size_t i = 0; i < n_files; i++) Line 1314
if (0 <= f[i].fd && ! f[i].remote) Line 1315
return true; Line 1316
return false; Line 1317
} Block 28
/* Return true if any of the N_FILES files in F is a symlink.
Note we don't worry about the edge case where "-" exists,
since that will have the same consequences for inotify,
which is the only context this function is currently used. */
static bool Line 1325
any_symlinks (const struct File_spec *f, size_t n_files) Line 1326
{
struct stat st; Line 1328
for (size_t i = 0; i < n_files; i++) Line 1329
if (lstat (f[i].name, &st) == 0 && S_ISLNK (st.st_mode)) Line 1330...!syscalls auto-comment...
return true; Line 1331
return false; Line 1332
} Block 29
/* Return true if any of the N_FILES files in F is not
a regular file or fifo. This is used to avoid adding inotify
watches on a device file for example, which inotify
will accept, but not give any events for. */
static bool Line 1340
any_non_regular_fifo (const struct File_spec *f, size_t n_files) Line 1341
{
for (size_t i = 0; i < n_files; i++) Line 1343
if (0 <= f[i].fd && ! S_ISREG (f[i].mode) && ! S_ISFIFO (f[i].mode)) Line 1344
return true; Line 1345
return false; Line 1346
} Block 30
/* Return true if any of the N_FILES files in F represents
stdin and is tailable. */
static bool Line 1352
tailable_stdin (const struct File_spec *f, size_t n_files) Line 1353
{
for (size_t i = 0; i < n_files; i++) Line 1355
if (!f[i].ignore && STREQ (f[i].name, "-")) Line 1356
return true; Line 1357
return false; Line 1358
} Block 31
static size_t Line 1361
wd_hasher (const void *entry, size_t tabsize) Line 1362
{
const struct File_spec *spec = entry; Line 1364
return spec->wd % tabsize; Line 1365
} Block 32
static bool Line 1368
wd_comparator (const void *e1, const void *e2) Line 1369
{
const struct File_spec *spec1 = e1; Line 1371
const struct File_spec *spec2 = e2; Line 1372
return spec1->wd == spec2->wd; Line 1373
} Block 33
/* Output (new) data for FSPEC->fd.
PREV_FSPEC records the last File_spec for which we output. */
static void Line 1378
check_fspec (struct File_spec *fspec, struct File_spec **prev_fspec) Line 1379
{
struct stat stats; Line 1381
char const *name; Line 1382
if (fspec->fd == -1) Line 1384
return; Line 1385
name = pretty_name (fspec); Line 1387
if (fstat (fspec->fd, &stats) != 0) Line 1389...!syscalls auto-comment......!syscalls auto-comment...
{
fspec->errnum = errno; Line 1391
close_fd (fspec->fd, name); Line 1392
fspec->fd = -1; Line 1393
return; Line 1394
}
/* XXX: This is only a heuristic, as the file may have also
been truncated and written to if st_size >= size
(in which case we ignore new data <= size).
Though in the inotify case it's more likely we'll get
separate events for truncate() and write(). */
if (S_ISREG (fspec->mode) && stats.st_size < fspec->size) Line 1402
{
error (0, 0, _("%s: file truncated"), quotef (name)); Line 1404
xlseek (fspec->fd, 0, SEEK_SET, name); Line 1405
fspec->size = 0; Line 1406
}
else if (S_ISREG (fspec->mode) && stats.st_size == fspec->size Line 1408
&& timespec_cmp (fspec->mtime, get_stat_mtime (&stats)) == 0) Line 1409
return; Line 1410
bool want_header = print_headers && (fspec != *prev_fspec); Line 1412
uintmax_t bytes_read = dump_remainder (want_header, name, fspec->fd, Line 1414
COPY_TO_EOF); Line 1415
fspec->size += bytes_read; Line 1416
if (bytes_read) Line 1418
{
*prev_fspec = fspec; Line 1420
if (fflush (stdout) != 0) Line 1421
die (EXIT_FAILURE, errno, _("write error")); Line 1422
}
} Block 34
/* Attempt to tail N_FILES files forever, or until killed.
Check modifications using the inotify events system.
Return false on error, or true to revert to polling. */
static bool Line 1429
tail_forever_inotify (int wd, struct File_spec *f, size_t n_files, Line 1430
double sleep_interval) Line 1431
{
# if TAIL_TEST_SLEEP Line 1433
/* Delay between open() and inotify_add_watch()
to help trigger different cases. */
xnanosleep (1000000); Line 1436
# endif Line 1437
unsigned int max_realloc = 3; Line 1438
/* Map an inotify watch descriptor to the name of the file it's watching. */
Hash_table *wd_to_name; Line 1441
bool found_watchable_file = false; Line 1443
bool tailed_but_unwatchable = false; Line 1444
bool found_unwatchable_dir = false; Line 1445
bool no_inotify_resources = false; Line 1446
bool writer_is_dead = false; Line 1447
struct File_spec *prev_fspec; Line 1448
size_t evlen = 0; Line 1449
char *evbuf; Line 1450
size_t evbuf_off = 0; Line 1451
size_t len = 0; Line 1452
wd_to_name = hash_initialize (n_files, NULL, wd_hasher, wd_comparator, NULL); Line 1454
if (! wd_to_name) Line 1455
xalloc_die (); ...!common auto-comment...
/* The events mask used with inotify on files (not directories). */
uint32_t inotify_wd_mask = IN_MODIFY; Line 1459
/* TODO: Perhaps monitor these events in Follow_descriptor mode also,
to tag reported file names with "deleted", "moved" etc. */
if (follow_mode == Follow_name) Line 1462
inotify_wd_mask |= (IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF); Line 1463
/* Add an inotify watch for each watched file. If -F is specified then watch
its parent directory too, in this way when they re-appear we can add them
again to the watch list. */
size_t i; Line 1468
for (i = 0; i < n_files; i++) Line 1469
{
if (!f[i].ignore) Line 1471
{
size_t fnlen = strlen (f[i].name); Line 1473
if (evlen < fnlen) Line 1474
evlen = fnlen; Line 1475
f[i].wd = -1; Line 1477
if (follow_mode == Follow_name) Line 1479
{
size_t dirlen = dir_len (f[i].name); Line 1481
char prev = f[i].name[dirlen]; Line 1482
f[i].basename_start = last_component (f[i].name) - f[i].name; Line 1483
f[i].name[dirlen] = '\0'; Line 1485
/* It's fine to add the same directory more than once.
In that case the same watch descriptor is returned. */
f[i].parent_wd = inotify_add_watch (wd, dirlen ? f[i].name : ".", Line 1489
(IN_CREATE | IN_DELETE Line 1490
| IN_MOVED_TO | IN_ATTRIB Line 1491
| IN_DELETE_SELF)); Line 1492
f[i].name[dirlen] = prev; Line 1494
if (f[i].parent_wd < 0) Line 1496
{
if (errno != ENOSPC) /* suppress confusing error. */ Line 1498
error (0, errno, _("cannot watch parent directory of %s"), Line 1499
quoteaf (f[i].name)); Line 1500
else Line 1501
error (0, 0, _("inotify resources exhausted")); Line 1502
found_unwatchable_dir = true; Line 1503
/* We revert to polling below. Note invalid uses
of the inotify API will still be diagnosed. */
break; Line 1506
}
}
f[i].wd = inotify_add_watch (wd, f[i].name, inotify_wd_mask); Line 1510
if (f[i].wd < 0) Line 1512
{
if (f[i].fd != -1) /* already tailed. */ Line 1514
tailed_but_unwatchable = true; Line 1515
if (errno == ENOSPC || errno == ENOMEM) Line 1516
{
no_inotify_resources = true; Line 1518
error (0, 0, _("inotify resources exhausted")); Line 1519
break; Line 1520
}
else if (errno != f[i].errnum) Line 1522
error (0, errno, _("cannot watch %s"), quoteaf (f[i].name)); Line 1523
continue; Line 1524
}
if (hash_insert (wd_to_name, &(f[i])) == NULL) Line 1527
xalloc_die (); ...!common auto-comment...
found_watchable_file = true; Line 1530
}
}
/* Linux kernel 2.6.24 at least has a bug where eventually, ENOSPC is always
returned by inotify_add_watch. In any case we should revert to polling
when there are no inotify resources. Also a specified directory may not
be currently present or accessible, so revert to polling. Also an already
tailed but unwatchable due rename/unlink race, should also revert. */
if (no_inotify_resources || found_unwatchable_dir Line 1539
|| (follow_mode == Follow_descriptor && tailed_but_unwatchable)) Line 1540
{
hash_free (wd_to_name); Line 1542
errno = 0; Line 1544
return true; Line 1545
}
if (follow_mode == Follow_descriptor && !found_watchable_file) Line 1547
return false; Line 1548
prev_fspec = &(f[n_files - 1]); Line 1550
/* Check files again. New files or data can be available since last time we
checked and before they are watched by inotify. */
for (i = 0; i < n_files; i++) Line 1554
{
if (! f[i].ignore) Line 1556
{
/* check for new files. */
if (follow_mode == Follow_name) Line 1559
recheck (&(f[i]), false); Line 1560
else if (f[i].fd != -1) Line 1561
{
/* If the file was replaced in the small window since we tailed,
then assume the watch is on the wrong item (different to
that we've already produced output for), and so revert to
polling the original descriptor. */
struct stat stats; Line 1567
if (stat (f[i].name, &stats) == 0 Line 1569...!syscalls auto-comment...
&& (f[i].dev != stats.st_dev || f[i].ino != stats.st_ino)) Line 1570
{
error (0, errno, _("%s was replaced"), Line 1572
quoteaf (pretty_name (&(f[i])))); Line 1573
hash_free (wd_to_name); Line 1574
errno = 0; Line 1576
return true; Line 1577
}
}
/* check for new data. */
check_fspec (&f[i], &prev_fspec); Line 1582
}
}
evlen += sizeof (struct inotify_event) + 1; Line 1586
evbuf = xmalloc (evlen); Line 1587
/* Wait for inotify events and handle them. Events on directories
ensure that watched files can be re-added when following by name.
This loop blocks on the 'safe_read' call until a new event is notified.
But when --pid=P is specified, tail usually waits via the select. */
while (1) Line 1593
{
struct File_spec *fspec; Line 1595
struct inotify_event *ev; Line 1596
void *void_ev; Line 1597
/* When following by name without --retry, and the last file has
been unlinked or renamed-away, diagnose it and return. */
if (follow_mode == Follow_name Line 1601
&& ! reopen_inaccessible_files Line 1602
&& hash_get_n_entries (wd_to_name) == 0) Line 1603
{
error (0, 0, _("no files remaining")); Line 1605
return false; Line 1606
}
/* When watching a PID, ensure that a read from WD will not block
indefinitely. */
while (len <= evbuf_off) Line 1611
{
struct timeval delay; /* how long to wait for file changes. */ Line 1613
if (pid) Line 1615
{
if (writer_is_dead) Line 1617
exit (EXIT_SUCCESS); Line 1618
writer_is_dead = (kill (pid, 0) != 0 && errno != EPERM); Line 1620
if (writer_is_dead) Line 1622
delay.tv_sec = delay.tv_usec = 0; Line 1623
else Line 1624
{
delay.tv_sec = (time_t) sleep_interval; Line 1626
delay.tv_usec = 1000000 * (sleep_interval - delay.tv_sec); Line 1627
}
}
fd_set rfd; Line 1631
FD_ZERO (&rfd); Line 1632
FD_SET (wd, &rfd); Line 1633
if (monitor_output) Line 1634
FD_SET (STDOUT_FILENO, &rfd); Line 1635
int file_change = select (MAX (wd, STDOUT_FILENO) + 1, Line 1637
&rfd, NULL, NULL, pid ? &delay: NULL); Line 1638
if (file_change == 0) Line 1640
continue; Line 1641
else if (file_change == -1) Line 1642
die (EXIT_FAILURE, errno, Line 1643
_("error waiting for inotify and output events")); Line 1644
else if (FD_ISSET (STDOUT_FILENO, &rfd)) Line 1645
{
/* readable event on STDOUT is equivalent to POLLERR,
and implies an error on output like broken pipe. */
raise (SIGPIPE); Line 1649
}
else Line 1651
break; Line 1652
}
if (len <= evbuf_off) Line 1655
{
len = safe_read (wd, evbuf, evlen); Line 1657...!syscalls auto-comment...
evbuf_off = 0; Line 1658
/* For kernels prior to 2.6.21, read returns 0 when the buffer
is too small. */
if ((len == 0 || (len == SAFE_READ_ERROR && errno == EINVAL)) Line 1662
&& max_realloc--) Line 1663
{
len = 0; Line 1665
evlen *= 2; Line 1666
evbuf = xrealloc (evbuf, evlen); Line 1667
continue; Line 1668
}
if (len == 0 || len == SAFE_READ_ERROR) Line 1671
die (EXIT_FAILURE, errno, _("error reading inotify event")); Line 1672
}
void_ev = evbuf + evbuf_off; Line 1675
ev = void_ev; Line 1676
evbuf_off += sizeof (*ev) + ev->len; Line 1677
/* If a directory is deleted, IN_DELETE_SELF is emitted
with ev->name of length 0.
We need to catch it, otherwise it would wait forever,
as wd for directory becomes inactive. Revert to polling now. */
if ((ev->mask & IN_DELETE_SELF) && ! ev->len) Line 1683
{
for (i = 0; i < n_files; i++) Line 1685
{
if (ev->wd == f[i].parent_wd) Line 1687
{
hash_free (wd_to_name); Line 1689
error (0, 0, Line 1690
_("directory containing watched file was removed")); Line 1691
errno = 0; /* we've already diagnosed enough errno detail. */Line 1692
return true; Line 1693
}
}
}
if (ev->len) /* event on ev->name in watched directory. */ Line 1698
{
size_t j; Line 1700
for (j = 0; j < n_files; j++) Line 1701
{
/* With N=hundreds of frequently-changing files, this O(N^2)
process might be a problem. FIXME: use a hash table? */
if (f[j].parent_wd == ev->wd Line 1705
&& STREQ (ev->name, f[j].name + f[j].basename_start)) Line 1706
break; Line 1707
}
/* It is not a watched file. */
if (j == n_files) Line 1711
continue; Line 1712
fspec = &(f[j]); Line 1714
int new_wd = -1; Line 1716
bool deleting = !! (ev->mask & IN_DELETE); Line 1717
if (! deleting) Line 1719
{
/* Adding the same inode again will look up any existing wd. */
new_wd = inotify_add_watch (wd, f[j].name, inotify_wd_mask); Line 1722
}
if (! deleting && new_wd < 0) Line 1725
{
if (errno == ENOSPC || errno == ENOMEM) Line 1727
{
error (0, 0, _("inotify resources exhausted")); Line 1729
hash_free (wd_to_name); Line 1730
errno = 0; Line 1731
return true; /* revert to polling. */ Line 1732
}
else Line 1734
{
/* Can get ENOENT for a dangling symlink for example. */
error (0, errno, _("cannot watch %s"), quoteaf (f[j].name)); Line 1737
}
/* We'll continue below after removing the existing watch. */
}
/* This will be false if only attributes of file change. */
bool new_watch; Line 1743
new_watch = (! deleting) && (fspec->wd < 0 || new_wd != fspec->wd); Line 1744
if (new_watch) Line 1746
{
if (0 <= fspec->wd) Line 1748
{
inotify_rm_watch (wd, fspec->wd); Line 1750
hash_delete (wd_to_name, fspec); Line 1751
}
fspec->wd = new_wd; Line 1754
if (new_wd == -1) Line 1756
continue; Line 1757
/* If the file was moved then inotify will use the source file wd
for the destination file. Make sure the key is not present in
the table. */
struct File_spec *prev = hash_delete (wd_to_name, fspec); Line 1762
if (prev && prev != fspec) Line 1763
{
if (follow_mode == Follow_name) Line 1765
recheck (prev, false); Line 1766
prev->wd = -1; Line 1767
close_fd (prev->fd, pretty_name (prev)); Line 1768
}
if (hash_insert (wd_to_name, fspec) == NULL) Line 1771
xalloc_die (); ...!common auto-comment...
}
if (follow_mode == Follow_name) Line 1775
recheck (fspec, false); Line 1776
}
else Line 1778
{
struct File_spec key; Line 1780
key.wd = ev->wd; Line 1781
fspec = hash_lookup (wd_to_name, &key); Line 1782
}
if (! fspec) Line 1785
continue; Line 1786
if (ev->mask & (IN_ATTRIB | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF)) Line 1788
{
/* Note for IN_MOVE_SELF (the file we're watching has
been clobbered via a rename) we leave the watch
in place since it may still be part of the set
of watched names. */
if (ev->mask & IN_DELETE_SELF) Line 1794
{
inotify_rm_watch (wd, fspec->wd); Line 1796
hash_delete (wd_to_name, fspec); Line 1797
}
/* Note we get IN_ATTRIB for unlink() as st_nlink decrements.
The usual path is a close() done in recheck() triggers
an IN_DELETE_SELF event as the inode is removed.
However sometimes open() will succeed as even though
st_nlink is decremented, the dentry (cache) is not updated.
Thus we depend on the IN_DELETE event on the directory
to trigger processing for the removed file. */
recheck (fspec, false); Line 1808
continue; Line 1810
}
check_fspec (fspec, &prev_fspec); Line 1812
}
} Block 35
#endif Line 1815
/* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
Return true if successful. */
static bool Line 1820
tail_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes, Line 1821
uintmax_t *read_pos) Line 1822
{
struct stat stats; Line 1824
if (fstat (fd, &stats)) Line 1826...!syscalls auto-comment......!syscalls auto-comment...
{
error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename)); Line 1828
return false; Line 1829
}
if (from_start) Line 1832
{
if (! presume_input_pipe && n_bytes <= OFF_T_MAX Line 1834
&& ((S_ISREG (stats.st_mode) Line 1835
&& xlseek (fd, n_bytes, SEEK_CUR, pretty_filename) >= 0) Line 1836
|| lseek (fd, n_bytes, SEEK_CUR) != -1)) Line 1837
*read_pos += n_bytes; Line 1838
else Line 1839
{
int t = start_bytes (pretty_filename, fd, n_bytes, read_pos); Line 1841
if (t) Line 1842
return t < 0; Line 1843
}
n_bytes = COPY_TO_EOF; Line 1845
}
else Line 1847
{
off_t end_pos = -1; Line 1849
off_t current_pos = -1; Line 1850
if (! presume_input_pipe && n_bytes <= OFF_T_MAX) Line 1852
{
if (usable_st_size (&stats)) Line 1854
end_pos = stats.st_size; Line 1855
else if ((current_pos = lseek (fd, -n_bytes, SEEK_END)) != -1) Line 1856
end_pos = current_pos + n_bytes; Line 1857
}
if (end_pos <= (off_t) ST_BLKSIZE (stats)) Line 1859
return pipe_bytes (pretty_filename, fd, n_bytes, read_pos); Line 1860
if (current_pos == -1) Line 1861
current_pos = xlseek (fd, 0, SEEK_CUR, pretty_filename); Line 1862
if (current_pos < end_pos) Line 1863
{
off_t bytes_remaining = end_pos - current_pos; Line 1865
if (n_bytes < bytes_remaining) Line 1867
{
current_pos = end_pos - n_bytes; Line 1869
xlseek (fd, current_pos, SEEK_SET, pretty_filename); Line 1870
}
}
*read_pos = current_pos; Line 1873
}
*read_pos += dump_remainder (false, pretty_filename, fd, n_bytes); Line 1876
return true; Line 1877
} Block 36
/* Output the last N_LINES lines of file FILENAME open for reading in FD.
Return true if successful. */
static bool Line 1883
tail_lines (const char *pretty_filename, int fd, uintmax_t n_lines, Line 1884
uintmax_t *read_pos) Line 1885
{
struct stat stats; Line 1887
if (fstat (fd, &stats)) Line 1889...!syscalls auto-comment......!syscalls auto-comment...
{
error (0, errno, _("cannot fstat %s"), quoteaf (pretty_filename)); Line 1891
return false; Line 1892
}
if (from_start) Line 1895
{
int t = start_lines (pretty_filename, fd, n_lines, read_pos); Line 1897
if (t) Line 1898
return t < 0; Line 1899
*read_pos += dump_remainder (false, pretty_filename, fd, COPY_TO_EOF); Line 1900
}
else Line 1902
{
off_t start_pos = -1; Line 1904
off_t end_pos; Line 1905
/* Use file_lines only if FD refers to a regular file for
which lseek (... SEEK_END) works. */
if ( ! presume_input_pipe Line 1909
&& S_ISREG (stats.st_mode) Line 1910
&& (start_pos = lseek (fd, 0, SEEK_CUR)) != -1 Line 1911
&& start_pos < (end_pos = lseek (fd, 0, SEEK_END))) Line 1912
{
*read_pos = end_pos; Line 1914
if (end_pos != 0 Line 1915
&& ! file_lines (pretty_filename, fd, n_lines, Line 1916
start_pos, end_pos, read_pos)) Line 1917
return false; Line 1918
}
else Line 1920
{
/* Under very unlikely circumstances, it is possible to reach
this point after positioning the file pointer to end of file
via the 'lseek (...SEEK_END)' above. In that case, reposition
the file pointer back to start_pos before calling pipe_lines. */
if (start_pos != -1) Line 1926
xlseek (fd, start_pos, SEEK_SET, pretty_filename); Line 1927
return pipe_lines (pretty_filename, fd, n_lines, read_pos); Line 1929
}
}
return true; Line 1932
} Block 37
/* Display the last N_UNITS units of file FILENAME, open for reading
via FD. Set *READ_POS to the position of the input stream pointer.
*READ_POS is usually the number of bytes read and corresponds to an
offset from the beginning of a file. However, it may be larger than
OFF_T_MAX (as for an input pipe), and may also be larger than the
number of bytes read (when an input pointer is initially not at
beginning of file), and may be far greater than the number of bytes
actually read for an input file that is seekable.
Return true if successful. */
static bool Line 1945
tail (const char *filename, int fd, uintmax_t n_units, Line 1946
uintmax_t *read_pos) Line 1947
{
*read_pos = 0; Line 1949
if (count_lines) Line 1950
return tail_lines (filename, fd, n_units, read_pos); Line 1951
else Line 1952
return tail_bytes (filename, fd, n_units, read_pos); Line 1953
} Block 38
/* Display the last N_UNITS units of the file described by F.
Return true if successful. */
static bool Line 1959
tail_file (struct File_spec *f, uintmax_t n_units) Line 1960
{
int fd; Line 1962
bool ok; Line 1963
bool is_stdin = (STREQ (f->name, "-")); Line 1965
if (is_stdin) Line 1967
{
have_read_stdin = true; Line 1969
fd = STDIN_FILENO; Line 1970
xset_binary_mode (STDIN_FILENO, O_BINARY); Line 1971
}
else Line 1973
fd = open (f->name, O_RDONLY | O_BINARY); Line 1974...!syscalls auto-comment...
f->tailable = !(reopen_inaccessible_files && fd == -1); Line 1976
if (fd == -1) Line 1978
{
if (forever) Line 1980
{
f->fd = -1; Line 1982
f->errnum = errno; Line 1983
f->ignore = ! reopen_inaccessible_files; Line 1984
f->ino = 0; Line 1985
f->dev = 0; Line 1986
}
error (0, errno, _("cannot open %s for reading"), Line 1988
quoteaf (pretty_name (f))); Line 1989
ok = false; Line 1990
}
else Line 1992
{
uintmax_t read_pos; Line 1994
if (print_headers) Line 1996
write_header (pretty_name (f)); Line 1997
ok = tail (pretty_name (f), fd, n_units, &read_pos); Line 1998
if (forever) Line 1999
{
struct stat stats; Line 2001
#if TAIL_TEST_SLEEP Line 2003
/* Before the tail function provided 'read_pos', there was
a race condition described in the URL below. This sleep
call made the window big enough to exercise the problem. */
xnanosleep (1); Line 2007
#endif Line 2008
f->errnum = ok - 1; Line 2009
if (fstat (fd, &stats) < 0) Line 2010...!syscalls auto-comment......!syscalls auto-comment...
{
ok = false; Line 2012
f->errnum = errno; Line 2013
error (0, errno, _("error reading %s"), Line 2014
quoteaf (pretty_name (f))); Line 2015
}
else if (!IS_TAILABLE_FILE_TYPE (stats.st_mode)) Line 2017
{
ok = false; Line 2019
f->errnum = -1; Line 2020
f->tailable = false; Line 2021
f->ignore = ! reopen_inaccessible_files; Line 2022
error (0, 0, _("%s: cannot follow end of this type of file%s"), Line 2023
quotef (pretty_name (f)), Line 2024
f->ignore ? _("; giving up on this name") : ""); Line 2025
}
if (!ok) Line 2028
{
f->ignore = ! reopen_inaccessible_files; Line 2030
close_fd (fd, pretty_name (f)); Line 2031
f->fd = -1; Line 2032
}
else Line 2034
{
/* Note: we must use read_pos here, not stats.st_size,
to avoid a race condition described by Ken Raeburn:
https://lists.gnu.org/r/bug-textutils/2003-05/msg00007.html */
record_open_fd (f, fd, read_pos, &stats, (is_stdin ? -1 : 1)); Line 2039
f->remote = fremote (fd, pretty_name (f)); Line 2040
}
}
else Line 2043
{
if (!is_stdin && close (fd)) Line 2045...!syscalls auto-comment...
{
error (0, errno, _("error reading %s"), Line 2047
quoteaf (pretty_name (f))); Line 2048
ok = false; Line 2049
}
}
}
return ok; Line 2054
} Block 39
/* If obsolete usage is allowed, and the command line arguments are of
the obsolete form and the option string is well-formed, set
*N_UNITS, the globals COUNT_LINES, FOREVER, and FROM_START, and
return true. If the command line arguments are obviously incorrect
(e.g., because obsolete usage is not allowed and the arguments are
incorrect for non-obsolete usage), report an error and exit.
Otherwise, return false and don't modify any parameter or global
variable. */
static bool Line 2066
parse_obsolete_option (int argc, char * const *argv, uintmax_t *n_units) Line 2067
{
const char *p; Line 2069
const char *n_string; Line 2070
const char *n_string_end; Line 2071
int default_count = DEFAULT_N_LINES; Line 2072
bool t_from_start; Line 2073
bool t_count_lines = true; Line 2074
bool t_forever = false; Line 2075
/* With the obsolete form, there is one option string and at most
one file argument. Watch out for "-" and "--", though. */
if (! (argc == 2 Line 2079
|| (argc == 3 && ! (argv[2][0] == '-' && argv[2][1])) Line 2080
|| (3 <= argc && argc <= 4 && STREQ (argv[2], "--")))) Line 2081
return false; Line 2082
int posix_ver = posix2_version (); Line 2084
bool obsolete_usage = posix_ver < 200112; Line 2085
bool traditional_usage = obsolete_usage || 200809 <= posix_ver; Line 2086
p = argv[1]; Line 2087
switch (*p++) Line 2089
{
default: Line 2091
return false; Line 2092
case '+': Line 2094
/* Leading "+" is a file name in the standard form. */
if (!traditional_usage) Line 2096
return false; Line 2097
t_from_start = true; Line 2099
break; Line 2100
case '-': Line 2102
/* In the non-obsolete form, "-" is standard input and "-c"
requires an option-argument. The obsolete multidigit options
are supported as a GNU extension even when conforming to
POSIX 1003.1-2001 or later, so don't complain about them. */
if (!obsolete_usage && !p[p[0] == 'c']) Line 2107
return false; Line 2108
t_from_start = false; Line 2110
break; Line 2111
}
n_string = p; Line 2114
while (ISDIGIT (*p)) Line 2115
p++; Line 2116
n_string_end = p; Line 2117
switch (*p) Line 2119
{
case 'b': default_count *= 512; FALLTHROUGH; Line 2121
case 'c': t_count_lines = false; FALLTHROUGH; Line 2122
case 'l': p++; break; Line 2123
}
if (*p == 'f') Line 2126
{
t_forever = true; Line 2128
++p; Line 2129
}
if (*p) Line 2132
return false; Line 2133
if (n_string == n_string_end) Line 2135
*n_units = default_count; Line 2136
else if ((xstrtoumax (n_string, NULL, 10, n_units, "b") Line 2137
& ~LONGINT_INVALID_SUFFIX_CHAR) Line 2138
!= LONGINT_OK) Line 2139
{
die (EXIT_FAILURE, errno, "%s: %s", _("invalid number"), Line 2141
quote (argv[1])); Line 2142
}
/* Set globals. */
from_start = t_from_start; Line 2146
count_lines = t_count_lines; Line 2147
forever = t_forever; Line 2148
return true; Line 2150
} Block 40
static void Line 2153
parse_options (int argc, char **argv, Line 2154
uintmax_t *n_units, enum header_mode *header_mode, Line 2155
double *sleep_interval) Line 2156
{
int c; Line 2158
while ((c = getopt_long (argc, argv, "c:n:fFqs:vz0123456789", Line 2160
long_options, NULL)) Line 2161
!= -1) Line 2162
{
switch (c) Line 2164
{
case 'F': Line 2166
forever = true; Line 2167
follow_mode = Follow_name; Line 2168
reopen_inaccessible_files = true; Line 2169
break; Line 2170
case 'c': Line 2172
case 'n': Line 2173
count_lines = (c == 'n'); Line 2174
if (*optarg == '+') Line 2175
from_start = true; Line 2176
else if (*optarg == '-') Line 2177
++optarg; Line 2178
*n_units = xdectoumax (optarg, 0, UINTMAX_MAX, "bkKmMGTPEZY0", Line 2180
count_lines Line 2181
? _("invalid number of lines") Line 2182
: _("invalid number of bytes"), 0); Line 2183
break; Line 2184
case 'f': Line 2186
case LONG_FOLLOW_OPTION: Line 2187
forever = true; Line 2188
if (optarg == NULL) Line 2189
follow_mode = DEFAULT_FOLLOW_MODE; Line 2190
else Line 2191
follow_mode = XARGMATCH ("--follow", optarg, Line 2192
follow_mode_string, follow_mode_map); Line 2193
break; Line 2194
case RETRY_OPTION: Line 2196
reopen_inaccessible_files = true; Line 2197
break; Line 2198
case MAX_UNCHANGED_STATS_OPTION: Line 2200
/* --max-unchanged-stats=N */
max_n_unchanged_stats_between_opens = Line 2202
xdectoumax (optarg, 0, UINTMAX_MAX, "", Line 2203
_("invalid maximum number of unchanged stats between opens"), 0); Line 2204
break; Line 2205
case DISABLE_INOTIFY_OPTION: Line 2207
disable_inotify = true; Line 2208
break; Line 2209
case PID_OPTION: Line 2211
pid = xdectoumax (optarg, 0, PID_T_MAX, "", _("invalid PID"), 0); Line 2212
break; Line 2213
case PRESUME_INPUT_PIPE_OPTION: Line 2215
presume_input_pipe = true; Line 2216
break; Line 2217
case 'q': Line 2219
*header_mode = never; Line 2220
break; Line 2221
case 's': Line 2223
{
double s; Line 2225
if (! (xstrtod (optarg, NULL, &s, c_strtod) && 0 <= s)) Line 2226
die (EXIT_FAILURE, 0, Line 2227
_("invalid number of seconds: %s"), quote (optarg)); Line 2228
*sleep_interval = s; Line 2229
}
break; Line 2231
case 'v': Line 2233
*header_mode = always; Line 2234
break; Line 2235
case 'z': Line 2237
line_end = '\0'; Line 2238
break; Line 2239
case_GETOPT_HELP_CHAR; Line 2241
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); Line 2243
case '0': case '1': case '2': case '3': case '4': Line 2245
case '5': case '6': case '7': case '8': case '9': Line 2246
die (EXIT_FAILURE, 0, _("option used in invalid context -- %c"), c); Line 2247
default: Line 2249
usage (EXIT_FAILURE); Line 2250
}
}
if (reopen_inaccessible_files) Line 2254
{
if (!forever) Line 2256
{
reopen_inaccessible_files = false; Line 2258
error (0, 0, _("warning: --retry ignored; --retry is useful" Line 2259
" only when following")); Line 2260
}
else if (follow_mode == Follow_descriptor) Line 2262
error (0, 0, _("warning: --retry only effective for the initial open"));Line 2263
}
if (pid && !forever) Line 2266
error (0, 0, Line 2267
_("warning: PID ignored; --pid=PID is useful only when following")); Line 2268
else if (pid && kill (pid, 0) != 0 && errno == ENOSYS) Line 2269
{
error (0, 0, _("warning: --pid=PID is not supported on this system")); Line 2271
pid = 0; Line 2272
}
} Block 41
/* Mark as '.ignore'd each member of F that corresponds to a
pipe or fifo, and return the number of non-ignored members. */
static size_t Line 2278
ignore_fifo_and_pipe (struct File_spec *f, size_t n_files) Line 2279
{
/* When there is no FILE operand and stdin is a pipe or FIFO
POSIX requires that tail ignore the -f option.
Since we allow multiple FILE operands, we extend that to say: with -f,
ignore any "-" operand that corresponds to a pipe or FIFO. */
size_t n_viable = 0; Line 2285
for (size_t i = 0; i < n_files; i++) Line 2287
{
bool is_a_fifo_or_pipe = Line 2289
(STREQ (f[i].name, "-") Line 2290
&& !f[i].ignore Line 2291
&& 0 <= f[i].fd Line 2292
&& (S_ISFIFO (f[i].mode) Line 2293
|| (HAVE_FIFO_PIPES != 1 && isapipe (f[i].fd)))); Line 2294
if (is_a_fifo_or_pipe) Line 2295
{
f[i].fd = -1; Line 2297
f[i].ignore = true; Line 2298
}
else Line 2300
++n_viable; Line 2301
}
return n_viable; Line 2304
}
int
main (int argc, char **argv) Line 2308
{
enum header_mode header_mode = multiple_files; Line 2310
bool ok = true; Line 2311
/* If from_start, the number of items to skip before printing; otherwise,
the number of items at the end of the file to print. Although the type
is signed, the value is never negative. */
uintmax_t n_units = DEFAULT_N_LINES; Line 2315
size_t n_files; Line 2316
char **file; Line 2317
struct File_spec *F; Line 2318
size_t i; Line 2319
bool obsolete_option; Line 2320
/* The number of seconds to sleep between iterations.
During one iteration, every file name or descriptor is checked to
see if it has changed. */
double sleep_interval = 1.0; Line 2325
initialize_main (&argc, &argv); VMS-specific entry point handling wildcard expansion
set_program_name (argv[0]); Retains program name and discards path
setlocale (LC_ALL, ""); Sets up internationalization (i18n)
bindtextdomain (PACKAGE, LOCALEDIR); Assigns i18n directorySets text domain for _() [gettext()] function
textdomain (PACKAGE); Sets text domain for _() [gettext()] function
atexit (close_stdout); Close stdout on exit (see gnulib)
have_read_stdin = false; Line 2335
count_lines = true; Line 2337
forever = from_start = print_headers = false; Line 2338
line_end = '\n'; Line 2339
obsolete_option = parse_obsolete_option (argc, argv, &n_units); Line 2340
argc -= obsolete_option; Line 2341
argv += obsolete_option; Line 2342
parse_options (argc, argv, &n_units, &header_mode, &sleep_interval); Line 2343
/* To start printing with item N_UNITS from the start of the file, skip
N_UNITS - 1 items. 'tail -n +0' is actually meaningless, but for Unix
compatibility it's treated the same as 'tail -n +1'. */
if (from_start) Line 2348
{
if (n_units) Line 2350
--n_units; Line 2351
}
IF_LINT (assert (0 <= argc)); Line 2354
if (optind < argc) Line 2356
{
n_files = argc - optind; Line 2358
file = argv + optind; Line 2359
}
else Line 2361
{
static char *dummy_stdin = (char *) "-"; Line 2363
n_files = 1; Line 2364
file = &dummy_stdin; Line 2365
}
{
bool found_hyphen = false; Line 2369
for (i = 0; i < n_files; i++) Line 2371
if (STREQ (file[i], "-")) Line 2372
found_hyphen = true; Line 2373
/* When following by name, there must be a name. */
if (found_hyphen && follow_mode == Follow_name) Line 2376
die (EXIT_FAILURE, 0, _("cannot follow %s by name"), quoteaf ("-")); Line 2377
/* When following forever, and not using simple blocking, warn if
any file is '-' as the stats() used to check for input are ineffective.
This is only a warning, since tail's output (before a failing seek,
and that from any non-stdin files) might still be useful. */
if (forever && found_hyphen) Line 2383
{
struct stat in_stat; Line 2385
bool blocking_stdin; Line 2386
blocking_stdin = (pid == 0 && follow_mode == Follow_descriptor Line 2387
&& n_files == 1 && ! fstat (STDIN_FILENO, &in_stat) Line 2388...!syscalls auto-comment......!syscalls auto-comment...
&& ! S_ISREG (in_stat.st_mode)); Line 2389
if (! blocking_stdin && isatty (STDIN_FILENO)) Line 2391
error (0, 0, _("warning: following standard input" Line 2392
" indefinitely is ineffective")); Line 2393
}
}
/* Don't read anything if we'll never output anything. */
if (! n_units && ! forever && ! from_start) Line 2398
return EXIT_SUCCESS; Line 2399
F = xnmalloc (n_files, sizeof *F); Line 2401
for (i = 0; i < n_files; i++) Line 2402
F[i].name = file[i]; Line 2403
if (header_mode == always Line 2405
|| (header_mode == multiple_files && n_files > 1)) Line 2406
print_headers = true; Line 2407
xset_binary_mode (STDOUT_FILENO, O_BINARY); Line 2409
for (i = 0; i < n_files; i++) Line 2411
ok &= tail_file (&F[i], n_units); Line 2412
if (forever && ignore_fifo_and_pipe (F, n_files)) Line 2414
{
/* If stdout is a fifo or pipe, then monitor it
so that we exit if the reader goes away.
Note select() on a regular file is always readable. */
struct stat out_stat; Line 2419
if (fstat (STDOUT_FILENO, &out_stat) < 0) Line 2420...!syscalls auto-comment......!syscalls auto-comment...
die (EXIT_FAILURE, errno, _("standard output")); Line 2421
monitor_output = (S_ISFIFO (out_stat.st_mode) Line 2422
|| (HAVE_FIFO_PIPES != 1 && isapipe (STDOUT_FILENO))); Line 2423
#if HAVE_INOTIFY Line 2425
/* tailable_stdin() checks if the user specifies stdin via "-",
or implicitly by providing no arguments. If so, we won't use inotify.
Technically, on systems with a working /dev/stdin, we *could*,
but would it be worth it? Verifying that it's a real device
and hooked up to stdin is not trivial, while reverting to
non-inotify-based tail_forever is easy and portable.
any_remote_file() checks if the user has specified any
files that reside on remote file systems. inotify is not used
in this case because it would miss any updates to the file
that were not initiated from the local system.
any_non_remote_file() checks if the user has specified any
files that don't reside on remote file systems. inotify is not used
if there are no open files, as we can't determine if those file
will be on a remote file system.
any_symlinks() checks if the user has specified any symbolic links.
inotify is not used in this case because it returns updated _targets_
which would not match the specified names. If we tried to always
use the target names, then we would miss changes to the symlink itself.
ok is false when one of the files specified could not be opened for
reading. In this case and when following by descriptor,
tail_forever_inotify() cannot be used (in its current implementation).
FIXME: inotify doesn't give any notification when a new
(remote) file or directory is mounted on top a watched file.
When follow_mode == Follow_name we would ideally like to detect that.
Note if there is a change to the original file then we'll
recheck it and follow the new file, or ignore it if the
file has changed to being remote.
FIXME: when using inotify, and a directory for a watched file
is recreated, then we don't recheck any new file when
follow_mode == Follow_name.
FIXME-maybe: inotify has a watch descriptor per inode, and hence with
our current hash implementation will only --follow data for one
of the names when multiple hardlinked files are specified, or
for one name when a name is specified multiple times. */
if (!disable_inotify && (tailable_stdin (F, n_files) Line 2467
|| any_remote_file (F, n_files) Line 2468
|| ! any_non_remote_file (F, n_files) Line 2469
|| any_symlinks (F, n_files) Line 2470
|| any_non_regular_fifo (F, n_files) Line 2471
|| (!ok && follow_mode == Follow_descriptor))) Line 2472
disable_inotify = true; Line 2473
if (!disable_inotify) Line 2475
{
int wd = inotify_init (); Line 2477
if (0 <= wd) Line 2478
{
/* Flush any output from tail_file, now, since
tail_forever_inotify flushes only after writing,
not before reading. */
if (fflush (stdout) != 0) Line 2483
die (EXIT_FAILURE, errno, _("write error")); Line 2484
if (! tail_forever_inotify (wd, F, n_files, sleep_interval)) Line 2486
return EXIT_FAILURE; Line 2487
}
error (0, errno, _("inotify cannot be used, reverting to polling")); Line 2489
/* Free resources as this process can be long lived,
and we may have exhausted system resources above. */
for (i = 0; i < n_files; i++) Line 2494
{
/* It's OK to remove the same watch multiple times,
ignoring the EINVAL from redundant calls. */
if (F[i].wd != -1) Line 2498
inotify_rm_watch (wd, F[i].wd); Line 2499
if (F[i].parent_wd != -1) Line 2500
inotify_rm_watch (wd, F[i].parent_wd); Line 2501
}
}
#endif Line 2504
disable_inotify = true; Line 2505
tail_forever (F, n_files, sleep_interval); Line 2506
}
IF_LINT (free (F)); Line 2509
if (have_read_stdin && close (STDIN_FILENO) < 0) Line 2511...!syscalls auto-comment...
die (EXIT_FAILURE, errno, "-"); Line 2512
return ok ? EXIT_SUCCESS : EXIT_FAILURE; Line 2513
} Block 43