Bug Summary

File:tail.c
Warning:line 2320, column 36
Dereference of null pointer (loaded from variable 'xtmp')

Annotated Source Code

Press '?' to see keyboard shortcuts

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