log2json 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -0
- data/Gemfile.lock +24 -0
- data/README +66 -0
- data/bin/lines2redis +73 -0
- data/bin/nginxlog2json +58 -0
- data/bin/redis2es +146 -0
- data/bin/syslog2json +23 -0
- data/bin/tail +0 -0
- data/bin/tail-log +7 -0
- data/bin/tail-log.sh +67 -0
- data/bin/track-tails +54 -0
- data/lib/log2json.rb +217 -0
- data/lib/log2json/filters/base.patterns +93 -0
- data/lib/log2json/filters/nginx_access.rb +46 -0
- data/lib/log2json/filters/syslog.rb +62 -0
- data/lib/log2json/railslogger.rb +96 -0
- data/log2json.gemspec +18 -0
- data/src/coreutils-8.13_tail.patch +9 -0
- data/src/tail.c +2224 -0
- metadata +192 -0
@@ -0,0 +1,96 @@
|
|
1
|
+
# Use it like this in your config/environments/{staging,production}.rb:
|
2
|
+
#
|
3
|
+
# config.logger = ::Log2Json::create_custom_ralis_logger('/tmp/alternative.log', config)
|
4
|
+
#
|
5
|
+
# Also, in unicorn.rb, add it like this:
|
6
|
+
#
|
7
|
+
# logger ::Log2Json::create_custom_unicorn_logger('/tmp/alternative.log', self)
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'logger'
|
11
|
+
|
12
|
+
module Log2Json
|
13
|
+
|
14
|
+
def self.create_custom_logger(to_path)
|
15
|
+
logger = ::Logger.new(to_path)
|
16
|
+
logger.formatter = proc do |severity, datetime, progname, msg|
|
17
|
+
"#{datetime.strftime('%Y-%m-%dT%H:%M:%S%z')}: [#{severity}] #{$$} #{msg.gsub(/\n/, '#012')}\n"
|
18
|
+
end
|
19
|
+
logger
|
20
|
+
end
|
21
|
+
|
22
|
+
# Create a custom logger that's just like the default Rails logger but
|
23
|
+
# additionally logs to another file that has its own formatting for easier
|
24
|
+
# parsing by a log2json log monitoring script.
|
25
|
+
#
|
26
|
+
def self.create_custom_rails_logger(to_path, config)
|
27
|
+
# Do what railties' bootstrap.rb does to initialize a default logger for a Rails app.
|
28
|
+
path = config.paths["log"].first
|
29
|
+
unless File.exist? File.dirname path
|
30
|
+
FileUtils.mkdir_p File.dirname path
|
31
|
+
end
|
32
|
+
f = File.open path, 'a'
|
33
|
+
f.binmode
|
34
|
+
f.sync = true # make sure every write flushes
|
35
|
+
|
36
|
+
logger = ActiveSupport::TaggedLogging.new(
|
37
|
+
ActiveSupport::BufferedLogger.new(f)
|
38
|
+
)
|
39
|
+
logger.level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase)
|
40
|
+
|
41
|
+
logger.extend(::Log2Json::Logger.broadcast(
|
42
|
+
::Log2Json::create_custom_logger(to_path)))
|
43
|
+
end
|
44
|
+
|
45
|
+
# Simiar to the custom rails logger, but for unicorn.
|
46
|
+
#
|
47
|
+
def self.create_custom_unicorn_logger(to_path, config)
|
48
|
+
logger = ::Logger.new(config.set[:stderr_path])
|
49
|
+
logger.extend(::Log2Json::Logger.broadcast(
|
50
|
+
::Log2Json::create_custom_logger(to_path)))
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
# Code stolen from activesupport-4.0.0
|
55
|
+
class Logger < ::Logger
|
56
|
+
|
57
|
+
# Broadcasts logs to multiple loggers.
|
58
|
+
def self.broadcast(logger) # :nodoc:
|
59
|
+
Module.new do
|
60
|
+
define_method(:add) do |*args, &block|
|
61
|
+
logger.add(*args, &block)
|
62
|
+
super(*args, &block)
|
63
|
+
end
|
64
|
+
|
65
|
+
define_method(:<<) do |x|
|
66
|
+
logger << x
|
67
|
+
super(x)
|
68
|
+
end
|
69
|
+
|
70
|
+
define_method(:close) do
|
71
|
+
logger.close
|
72
|
+
super()
|
73
|
+
end
|
74
|
+
|
75
|
+
define_method(:progname=) do |name|
|
76
|
+
logger.progname = name
|
77
|
+
super(name)
|
78
|
+
end
|
79
|
+
|
80
|
+
define_method(:formatter=) do |formatter|
|
81
|
+
logger.formatter = formatter
|
82
|
+
super(formatter)
|
83
|
+
end
|
84
|
+
|
85
|
+
define_method(:level=) do |level|
|
86
|
+
logger.level = level
|
87
|
+
super(level)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
|
data/log2json.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'log2json'
|
3
|
+
s.version = '0.1.5'
|
4
|
+
s.summary = "Read, filter and ship logs. ie, poor man's roll-your-own, light-weight logstash replacement."
|
5
|
+
s.description = IO.read(File.join(File.dirname(__FILE__), 'README'))
|
6
|
+
s.authors = ['Jack Kuan']
|
7
|
+
s.email = 'jack.kuan@thescore.com'
|
8
|
+
s.files = `git ls-files`.split(/\n/).drop_while { |x| x =~ /\.gitignore$/ }
|
9
|
+
s.executables = ['tail-log', 'track-tails', 'syslog2json', 'nginxlog2json', 'lines2redis', 'redis2es']
|
10
|
+
|
11
|
+
# Relaxing the restriction so that 'railslogger.rb' in this gem can be used by projects that run on ree.
|
12
|
+
# s.required_ruby_version = '>= 1.9.3'
|
13
|
+
|
14
|
+
s.add_runtime_dependency 'jls-grok', '~> 0.10.10'
|
15
|
+
s.add_runtime_dependency 'redis', '~> 3.0.2'
|
16
|
+
s.add_runtime_dependency 'persistent_http', '~> 1.0.5'
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
1049a1050,1051
|
2
|
+
> if (print_headers)
|
3
|
+
> printf ("==> %s <== [new_file]\n", pretty_name (f));
|
4
|
+
1171a1174,1175
|
5
|
+
> if (print_headers)
|
6
|
+
> printf ("==> %s <== [truncated]\n", name);
|
7
|
+
1286a1291,1292
|
8
|
+
> if (print_headers)
|
9
|
+
> printf ("==> %s <== [truncated]\n", name);
|
data/src/tail.c
ADDED
@@ -0,0 +1,2224 @@
|
|
1
|
+
/* tail -- output the last part of file(s)
|
2
|
+
Copyright (C) 1989-1991, 1995-2006, 2008-2011 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 <http://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/types.h>
|
32
|
+
#include <signal.h>
|
33
|
+
|
34
|
+
#include "system.h"
|
35
|
+
#include "argmatch.h"
|
36
|
+
#include "c-strtod.h"
|
37
|
+
#include "error.h"
|
38
|
+
#include "fcntl--.h"
|
39
|
+
#include "isapipe.h"
|
40
|
+
#include "posixver.h"
|
41
|
+
#include "quote.h"
|
42
|
+
#include "safe-read.h"
|
43
|
+
#include "stat-time.h"
|
44
|
+
#include "xfreopen.h"
|
45
|
+
#include "xnanosleep.h"
|
46
|
+
#include "xstrtol.h"
|
47
|
+
#include "xstrtod.h"
|
48
|
+
|
49
|
+
#if HAVE_INOTIFY
|
50
|
+
# include "hash.h"
|
51
|
+
# include <sys/inotify.h>
|
52
|
+
/* `select' is used by tail_forever_inotify. */
|
53
|
+
# include <sys/select.h>
|
54
|
+
|
55
|
+
/* inotify needs to know if a file is local. */
|
56
|
+
# include "fs.h"
|
57
|
+
# if HAVE_SYS_STATFS_H
|
58
|
+
# include <sys/statfs.h>
|
59
|
+
# elif HAVE_SYS_VFS_H
|
60
|
+
# include <sys/vfs.h>
|
61
|
+
# endif
|
62
|
+
#endif
|
63
|
+
|
64
|
+
/* The official name of this program (e.g., no `g' prefix). */
|
65
|
+
#define PROGRAM_NAME "tail"
|
66
|
+
|
67
|
+
#define AUTHORS \
|
68
|
+
proper_name ("Paul Rubin"), \
|
69
|
+
proper_name ("David MacKenzie"), \
|
70
|
+
proper_name ("Ian Lance Taylor"), \
|
71
|
+
proper_name ("Jim Meyering")
|
72
|
+
|
73
|
+
/* Number of items to tail. */
|
74
|
+
#define DEFAULT_N_LINES 10
|
75
|
+
|
76
|
+
/* Special values for dump_remainder's N_BYTES parameter. */
|
77
|
+
#define COPY_TO_EOF UINTMAX_MAX
|
78
|
+
#define COPY_A_BUFFER (UINTMAX_MAX - 1)
|
79
|
+
|
80
|
+
/* FIXME: make Follow_name the default? */
|
81
|
+
#define DEFAULT_FOLLOW_MODE Follow_descriptor
|
82
|
+
|
83
|
+
enum Follow_mode
|
84
|
+
{
|
85
|
+
/* Follow the name of each file: if the file is renamed, try to reopen
|
86
|
+
that name and track the end of the new file if/when it's recreated.
|
87
|
+
This is useful for tracking logs that are occasionally rotated. */
|
88
|
+
Follow_name = 1,
|
89
|
+
|
90
|
+
/* Follow each descriptor obtained upon opening a file.
|
91
|
+
That means we'll continue to follow the end of a file even after
|
92
|
+
it has been renamed or unlinked. */
|
93
|
+
Follow_descriptor = 2
|
94
|
+
};
|
95
|
+
|
96
|
+
/* The types of files for which tail works. */
|
97
|
+
#define IS_TAILABLE_FILE_TYPE(Mode) \
|
98
|
+
(S_ISREG (Mode) || S_ISFIFO (Mode) || S_ISSOCK (Mode) || S_ISCHR (Mode))
|
99
|
+
|
100
|
+
static char const *const follow_mode_string[] =
|
101
|
+
{
|
102
|
+
"descriptor", "name", NULL
|
103
|
+
};
|
104
|
+
|
105
|
+
static enum Follow_mode const follow_mode_map[] =
|
106
|
+
{
|
107
|
+
Follow_descriptor, Follow_name,
|
108
|
+
};
|
109
|
+
|
110
|
+
struct File_spec
|
111
|
+
{
|
112
|
+
/* The actual file name, or "-" for stdin. */
|
113
|
+
char *name;
|
114
|
+
|
115
|
+
/* Attributes of the file the last time we checked. */
|
116
|
+
off_t size;
|
117
|
+
struct timespec mtime;
|
118
|
+
dev_t dev;
|
119
|
+
ino_t ino;
|
120
|
+
mode_t mode;
|
121
|
+
|
122
|
+
/* The specified name initially referred to a directory or some other
|
123
|
+
type for which tail isn't meaningful. Unlike for a permission problem
|
124
|
+
(tailable, below) once this is set, the name is not checked ever again. */
|
125
|
+
bool ignore;
|
126
|
+
|
127
|
+
/* See the description of fremote. */
|
128
|
+
bool remote;
|
129
|
+
|
130
|
+
/* A file is tailable if it exists, is readable, and is of type
|
131
|
+
IS_TAILABLE_FILE_TYPE. */
|
132
|
+
bool tailable;
|
133
|
+
|
134
|
+
/* File descriptor on which the file is open; -1 if it's not open. */
|
135
|
+
int fd;
|
136
|
+
|
137
|
+
/* The value of errno seen last time we checked this file. */
|
138
|
+
int errnum;
|
139
|
+
|
140
|
+
/* 1 if O_NONBLOCK is clear, 0 if set, -1 if not known. */
|
141
|
+
int blocking;
|
142
|
+
|
143
|
+
#if HAVE_INOTIFY
|
144
|
+
/* The watch descriptor used by inotify. */
|
145
|
+
int wd;
|
146
|
+
|
147
|
+
/* The parent directory watch descriptor. It is used only
|
148
|
+
* when Follow_name is used. */
|
149
|
+
int parent_wd;
|
150
|
+
|
151
|
+
/* Offset in NAME of the basename part. */
|
152
|
+
size_t basename_start;
|
153
|
+
#endif
|
154
|
+
|
155
|
+
/* See description of DEFAULT_MAX_N_... below. */
|
156
|
+
uintmax_t n_unchanged_stats;
|
157
|
+
};
|
158
|
+
|
159
|
+
#if HAVE_INOTIFY
|
160
|
+
/* The events mask used with inotify on files. This mask is not used on
|
161
|
+
directories. */
|
162
|
+
static const uint32_t inotify_wd_mask = (IN_MODIFY | IN_ATTRIB
|
163
|
+
| IN_DELETE_SELF | IN_MOVE_SELF);
|
164
|
+
#endif
|
165
|
+
|
166
|
+
/* Keep trying to open a file even if it is inaccessible when tail starts
|
167
|
+
or if it becomes inaccessible later -- useful only with -f. */
|
168
|
+
static bool reopen_inaccessible_files;
|
169
|
+
|
170
|
+
/* If true, interpret the numeric argument as the number of lines.
|
171
|
+
Otherwise, interpret it as the number of bytes. */
|
172
|
+
static bool count_lines;
|
173
|
+
|
174
|
+
/* Whether we follow the name of each file or the file descriptor
|
175
|
+
that is initially associated with each name. */
|
176
|
+
static enum Follow_mode follow_mode = Follow_descriptor;
|
177
|
+
|
178
|
+
/* If true, read from the ends of all specified files until killed. */
|
179
|
+
static bool forever;
|
180
|
+
|
181
|
+
/* If true, count from start of file instead of end. */
|
182
|
+
static bool from_start;
|
183
|
+
|
184
|
+
/* If true, print filename headers. */
|
185
|
+
static bool print_headers;
|
186
|
+
|
187
|
+
/* When to print the filename banners. */
|
188
|
+
enum header_mode
|
189
|
+
{
|
190
|
+
multiple_files, always, never
|
191
|
+
};
|
192
|
+
|
193
|
+
/* When tailing a file by name, if there have been this many consecutive
|
194
|
+
iterations for which the file has not changed, then open/fstat
|
195
|
+
the file to determine if that file name is still associated with the
|
196
|
+
same device/inode-number pair as before. This option is meaningful only
|
197
|
+
when following by name. --max-unchanged-stats=N */
|
198
|
+
#define DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS 5
|
199
|
+
static uintmax_t max_n_unchanged_stats_between_opens =
|
200
|
+
DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS;
|
201
|
+
|
202
|
+
/* The process ID of the process (presumably on the current host)
|
203
|
+
that is writing to all followed files. */
|
204
|
+
static pid_t pid;
|
205
|
+
|
206
|
+
/* True if we have ever read standard input. */
|
207
|
+
static bool have_read_stdin;
|
208
|
+
|
209
|
+
/* If nonzero, skip the is-regular-file test used to determine whether
|
210
|
+
to use the lseek optimization. Instead, use the more general (and
|
211
|
+
more expensive) code unconditionally. Intended solely for testing. */
|
212
|
+
static bool presume_input_pipe;
|
213
|
+
|
214
|
+
/* If nonzero then don't use inotify even if available. */
|
215
|
+
static bool disable_inotify;
|
216
|
+
|
217
|
+
/* For long options that have no equivalent short option, use a
|
218
|
+
non-character as a pseudo short option, starting with CHAR_MAX + 1. */
|
219
|
+
enum
|
220
|
+
{
|
221
|
+
RETRY_OPTION = CHAR_MAX + 1,
|
222
|
+
MAX_UNCHANGED_STATS_OPTION,
|
223
|
+
PID_OPTION,
|
224
|
+
PRESUME_INPUT_PIPE_OPTION,
|
225
|
+
LONG_FOLLOW_OPTION,
|
226
|
+
DISABLE_INOTIFY_OPTION
|
227
|
+
};
|
228
|
+
|
229
|
+
static struct option const long_options[] =
|
230
|
+
{
|
231
|
+
{"bytes", required_argument, NULL, 'c'},
|
232
|
+
{"follow", optional_argument, NULL, LONG_FOLLOW_OPTION},
|
233
|
+
{"lines", required_argument, NULL, 'n'},
|
234
|
+
{"max-unchanged-stats", required_argument, NULL, MAX_UNCHANGED_STATS_OPTION},
|
235
|
+
{"-disable-inotify", no_argument, NULL,
|
236
|
+
DISABLE_INOTIFY_OPTION}, /* do not document */
|
237
|
+
{"pid", required_argument, NULL, PID_OPTION},
|
238
|
+
{"-presume-input-pipe", no_argument, NULL,
|
239
|
+
PRESUME_INPUT_PIPE_OPTION}, /* do not document */
|
240
|
+
{"quiet", no_argument, NULL, 'q'},
|
241
|
+
{"retry", no_argument, NULL, RETRY_OPTION},
|
242
|
+
{"silent", no_argument, NULL, 'q'},
|
243
|
+
{"sleep-interval", required_argument, NULL, 's'},
|
244
|
+
{"verbose", no_argument, NULL, 'v'},
|
245
|
+
{GETOPT_HELP_OPTION_DECL},
|
246
|
+
{GETOPT_VERSION_OPTION_DECL},
|
247
|
+
{NULL, 0, NULL, 0}
|
248
|
+
};
|
249
|
+
|
250
|
+
void
|
251
|
+
usage (int status)
|
252
|
+
{
|
253
|
+
if (status != EXIT_SUCCESS)
|
254
|
+
fprintf (stderr, _("Try `%s --help' for more information.\n"),
|
255
|
+
program_name);
|
256
|
+
else
|
257
|
+
{
|
258
|
+
printf (_("\
|
259
|
+
Usage: %s [OPTION]... [FILE]...\n\
|
260
|
+
"),
|
261
|
+
program_name);
|
262
|
+
printf (_("\
|
263
|
+
Print the last %d lines of each FILE to standard output.\n\
|
264
|
+
With more than one FILE, precede each with a header giving the file name.\n\
|
265
|
+
With no FILE, or when FILE is -, read standard input.\n\
|
266
|
+
\n\
|
267
|
+
"), DEFAULT_N_LINES);
|
268
|
+
fputs (_("\
|
269
|
+
Mandatory arguments to long options are mandatory for short options too.\n\
|
270
|
+
"), stdout);
|
271
|
+
fputs (_("\
|
272
|
+
-c, --bytes=K output the last K bytes; alternatively, use -c +K\n\
|
273
|
+
to output bytes starting with the Kth of each file\n\
|
274
|
+
"), stdout);
|
275
|
+
fputs (_("\
|
276
|
+
-f, --follow[={name|descriptor}]\n\
|
277
|
+
output appended data as the file grows;\n\
|
278
|
+
-f, --follow, and --follow=descriptor are\n\
|
279
|
+
equivalent\n\
|
280
|
+
-F same as --follow=name --retry\n\
|
281
|
+
"), stdout);
|
282
|
+
printf (_("\
|
283
|
+
-n, --lines=K output the last K lines, instead of the last %d;\n\
|
284
|
+
or use -n +K to output lines starting with the Kth\n\
|
285
|
+
--max-unchanged-stats=N\n\
|
286
|
+
with --follow=name, reopen a FILE which has not\n\
|
287
|
+
changed size after N (default %d) iterations\n\
|
288
|
+
to see if it has been unlinked or renamed\n\
|
289
|
+
(this is the usual case of rotated log files).\n\
|
290
|
+
With inotify, this option is rarely useful.\n\
|
291
|
+
"),
|
292
|
+
DEFAULT_N_LINES,
|
293
|
+
DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS
|
294
|
+
);
|
295
|
+
fputs (_("\
|
296
|
+
--pid=PID with -f, terminate after process ID, PID dies\n\
|
297
|
+
-q, --quiet, --silent never output headers giving file names\n\
|
298
|
+
--retry keep trying to open a file even when it is or\n\
|
299
|
+
becomes inaccessible; useful when following by\n\
|
300
|
+
name, i.e., with --follow=name\n\
|
301
|
+
"), stdout);
|
302
|
+
fputs (_("\
|
303
|
+
-s, --sleep-interval=N with -f, sleep for approximately N seconds\n\
|
304
|
+
(default 1.0) between iterations.\n\
|
305
|
+
With inotify and --pid=P, check process P at\n\
|
306
|
+
least once every N seconds.\n\
|
307
|
+
-v, --verbose always output headers giving file names\n\
|
308
|
+
"), stdout);
|
309
|
+
fputs (HELP_OPTION_DESCRIPTION, stdout);
|
310
|
+
fputs (VERSION_OPTION_DESCRIPTION, stdout);
|
311
|
+
fputs (_("\
|
312
|
+
\n\
|
313
|
+
If the first character of K (the number of bytes or lines) is a `+',\n\
|
314
|
+
print beginning with the Kth item from the start of each file, otherwise,\n\
|
315
|
+
print the last K items in the file. K may have a multiplier suffix:\n\
|
316
|
+
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\
|
317
|
+
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.\n\
|
318
|
+
\n\
|
319
|
+
"), stdout);
|
320
|
+
fputs (_("\
|
321
|
+
With --follow (-f), tail defaults to following the file descriptor, which\n\
|
322
|
+
means that even if a tail'ed file is renamed, tail will continue to track\n\
|
323
|
+
its end. This default behavior is not desirable when you really want to\n\
|
324
|
+
track the actual name of the file, not the file descriptor (e.g., log\n\
|
325
|
+
rotation). Use --follow=name in that case. That causes tail to track the\n\
|
326
|
+
named file in a way that accommodates renaming, removal and creation.\n\
|
327
|
+
"), stdout);
|
328
|
+
emit_ancillary_info ();
|
329
|
+
}
|
330
|
+
exit (status);
|
331
|
+
}
|
332
|
+
|
333
|
+
static bool
|
334
|
+
valid_file_spec (struct File_spec const *f)
|
335
|
+
{
|
336
|
+
/* Exactly one of the following subexpressions must be true. */
|
337
|
+
return ((f->fd == -1) ^ (f->errnum == 0));
|
338
|
+
}
|
339
|
+
|
340
|
+
static char const *
|
341
|
+
pretty_name (struct File_spec const *f)
|
342
|
+
{
|
343
|
+
return (STREQ (f->name, "-") ? _("standard input") : f->name);
|
344
|
+
}
|
345
|
+
|
346
|
+
static void
|
347
|
+
xwrite_stdout (char const *buffer, size_t n_bytes)
|
348
|
+
{
|
349
|
+
if (n_bytes > 0 && fwrite (buffer, 1, n_bytes, stdout) == 0)
|
350
|
+
error (EXIT_FAILURE, errno, _("write error"));
|
351
|
+
}
|
352
|
+
|
353
|
+
/* Record a file F with descriptor FD, size SIZE, status ST, and
|
354
|
+
blocking status BLOCKING. */
|
355
|
+
|
356
|
+
static void
|
357
|
+
record_open_fd (struct File_spec *f, int fd,
|
358
|
+
off_t size, struct stat const *st,
|
359
|
+
int blocking)
|
360
|
+
{
|
361
|
+
f->fd = fd;
|
362
|
+
f->size = size;
|
363
|
+
f->mtime = get_stat_mtime (st);
|
364
|
+
f->dev = st->st_dev;
|
365
|
+
f->ino = st->st_ino;
|
366
|
+
f->mode = st->st_mode;
|
367
|
+
f->blocking = blocking;
|
368
|
+
f->n_unchanged_stats = 0;
|
369
|
+
f->ignore = false;
|
370
|
+
}
|
371
|
+
|
372
|
+
/* Close the file with descriptor FD and name FILENAME. */
|
373
|
+
|
374
|
+
static void
|
375
|
+
close_fd (int fd, const char *filename)
|
376
|
+
{
|
377
|
+
if (fd != -1 && fd != STDIN_FILENO && close (fd))
|
378
|
+
{
|
379
|
+
error (0, errno, _("closing %s (fd=%d)"), filename, fd);
|
380
|
+
}
|
381
|
+
}
|
382
|
+
|
383
|
+
static void
|
384
|
+
write_header (const char *pretty_filename)
|
385
|
+
{
|
386
|
+
static bool first_file = true;
|
387
|
+
|
388
|
+
printf ("%s==> %s <==\n", (first_file ? "" : "\n"), pretty_filename);
|
389
|
+
first_file = false;
|
390
|
+
}
|
391
|
+
|
392
|
+
/* Read and output N_BYTES of file PRETTY_FILENAME starting at the current
|
393
|
+
position in FD. If N_BYTES is COPY_TO_EOF, then copy until end of file.
|
394
|
+
If N_BYTES is COPY_A_BUFFER, then copy at most one buffer's worth.
|
395
|
+
Return the number of bytes read from the file. */
|
396
|
+
|
397
|
+
static uintmax_t
|
398
|
+
dump_remainder (const char *pretty_filename, int fd, uintmax_t n_bytes)
|
399
|
+
{
|
400
|
+
uintmax_t n_written;
|
401
|
+
uintmax_t n_remaining = n_bytes;
|
402
|
+
|
403
|
+
n_written = 0;
|
404
|
+
while (1)
|
405
|
+
{
|
406
|
+
char buffer[BUFSIZ];
|
407
|
+
size_t n = MIN (n_remaining, BUFSIZ);
|
408
|
+
size_t bytes_read = safe_read (fd, buffer, n);
|
409
|
+
if (bytes_read == SAFE_READ_ERROR)
|
410
|
+
{
|
411
|
+
if (errno != EAGAIN)
|
412
|
+
error (EXIT_FAILURE, errno, _("error reading %s"),
|
413
|
+
quote (pretty_filename));
|
414
|
+
break;
|
415
|
+
}
|
416
|
+
if (bytes_read == 0)
|
417
|
+
break;
|
418
|
+
xwrite_stdout (buffer, bytes_read);
|
419
|
+
n_written += bytes_read;
|
420
|
+
if (n_bytes != COPY_TO_EOF)
|
421
|
+
{
|
422
|
+
n_remaining -= bytes_read;
|
423
|
+
if (n_remaining == 0 || n_bytes == COPY_A_BUFFER)
|
424
|
+
break;
|
425
|
+
}
|
426
|
+
}
|
427
|
+
|
428
|
+
return n_written;
|
429
|
+
}
|
430
|
+
|
431
|
+
/* Call lseek with the specified arguments, where file descriptor FD
|
432
|
+
corresponds to the file, FILENAME.
|
433
|
+
Give a diagnostic and exit nonzero if lseek fails.
|
434
|
+
Otherwise, return the resulting offset. */
|
435
|
+
|
436
|
+
static off_t
|
437
|
+
xlseek (int fd, off_t offset, int whence, char const *filename)
|
438
|
+
{
|
439
|
+
off_t new_offset = lseek (fd, offset, whence);
|
440
|
+
char buf[INT_BUFSIZE_BOUND (offset)];
|
441
|
+
char *s;
|
442
|
+
|
443
|
+
if (0 <= new_offset)
|
444
|
+
return new_offset;
|
445
|
+
|
446
|
+
s = offtostr (offset, buf);
|
447
|
+
switch (whence)
|
448
|
+
{
|
449
|
+
case SEEK_SET:
|
450
|
+
error (0, errno, _("%s: cannot seek to offset %s"),
|
451
|
+
filename, s);
|
452
|
+
break;
|
453
|
+
case SEEK_CUR:
|
454
|
+
error (0, errno, _("%s: cannot seek to relative offset %s"),
|
455
|
+
filename, s);
|
456
|
+
break;
|
457
|
+
case SEEK_END:
|
458
|
+
error (0, errno, _("%s: cannot seek to end-relative offset %s"),
|
459
|
+
filename, s);
|
460
|
+
break;
|
461
|
+
default:
|
462
|
+
abort ();
|
463
|
+
}
|
464
|
+
|
465
|
+
exit (EXIT_FAILURE);
|
466
|
+
}
|
467
|
+
|
468
|
+
/* Print the last N_LINES lines from the end of file FD.
|
469
|
+
Go backward through the file, reading `BUFSIZ' bytes at a time (except
|
470
|
+
probably the first), until we hit the start of the file or have
|
471
|
+
read NUMBER newlines.
|
472
|
+
START_POS is the starting position of the read pointer for the file
|
473
|
+
associated with FD (may be nonzero).
|
474
|
+
END_POS is the file offset of EOF (one larger than offset of last byte).
|
475
|
+
Return true if successful. */
|
476
|
+
|
477
|
+
static bool
|
478
|
+
file_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
|
479
|
+
off_t start_pos, off_t end_pos, uintmax_t *read_pos)
|
480
|
+
{
|
481
|
+
char buffer[BUFSIZ];
|
482
|
+
size_t bytes_read;
|
483
|
+
off_t pos = end_pos;
|
484
|
+
|
485
|
+
if (n_lines == 0)
|
486
|
+
return true;
|
487
|
+
|
488
|
+
/* Set `bytes_read' to the size of the last, probably partial, buffer;
|
489
|
+
0 < `bytes_read' <= `BUFSIZ'. */
|
490
|
+
bytes_read = (pos - start_pos) % BUFSIZ;
|
491
|
+
if (bytes_read == 0)
|
492
|
+
bytes_read = BUFSIZ;
|
493
|
+
/* Make `pos' a multiple of `BUFSIZ' (0 if the file is short), so that all
|
494
|
+
reads will be on block boundaries, which might increase efficiency. */
|
495
|
+
pos -= bytes_read;
|
496
|
+
xlseek (fd, pos, SEEK_SET, pretty_filename);
|
497
|
+
bytes_read = safe_read (fd, buffer, bytes_read);
|
498
|
+
if (bytes_read == SAFE_READ_ERROR)
|
499
|
+
{
|
500
|
+
error (0, errno, _("error reading %s"), quote (pretty_filename));
|
501
|
+
return false;
|
502
|
+
}
|
503
|
+
*read_pos = pos + bytes_read;
|
504
|
+
|
505
|
+
/* Count the incomplete line on files that don't end with a newline. */
|
506
|
+
if (bytes_read && buffer[bytes_read - 1] != '\n')
|
507
|
+
--n_lines;
|
508
|
+
|
509
|
+
do
|
510
|
+
{
|
511
|
+
/* Scan backward, counting the newlines in this bufferfull. */
|
512
|
+
|
513
|
+
size_t n = bytes_read;
|
514
|
+
while (n)
|
515
|
+
{
|
516
|
+
char const *nl;
|
517
|
+
nl = memrchr (buffer, '\n', n);
|
518
|
+
if (nl == NULL)
|
519
|
+
break;
|
520
|
+
n = nl - buffer;
|
521
|
+
if (n_lines-- == 0)
|
522
|
+
{
|
523
|
+
/* If this newline isn't the last character in the buffer,
|
524
|
+
output the part that is after it. */
|
525
|
+
if (n != bytes_read - 1)
|
526
|
+
xwrite_stdout (nl + 1, bytes_read - (n + 1));
|
527
|
+
*read_pos += dump_remainder (pretty_filename, fd,
|
528
|
+
end_pos - (pos + bytes_read));
|
529
|
+
return true;
|
530
|
+
}
|
531
|
+
}
|
532
|
+
|
533
|
+
/* Not enough newlines in that bufferfull. */
|
534
|
+
if (pos == start_pos)
|
535
|
+
{
|
536
|
+
/* Not enough lines in the file; print everything from
|
537
|
+
start_pos to the end. */
|
538
|
+
xlseek (fd, start_pos, SEEK_SET, pretty_filename);
|
539
|
+
*read_pos = start_pos + dump_remainder (pretty_filename, fd,
|
540
|
+
end_pos);
|
541
|
+
return true;
|
542
|
+
}
|
543
|
+
pos -= BUFSIZ;
|
544
|
+
xlseek (fd, pos, SEEK_SET, pretty_filename);
|
545
|
+
|
546
|
+
bytes_read = safe_read (fd, buffer, BUFSIZ);
|
547
|
+
if (bytes_read == SAFE_READ_ERROR)
|
548
|
+
{
|
549
|
+
error (0, errno, _("error reading %s"), quote (pretty_filename));
|
550
|
+
return false;
|
551
|
+
}
|
552
|
+
|
553
|
+
*read_pos = pos + bytes_read;
|
554
|
+
}
|
555
|
+
while (bytes_read > 0);
|
556
|
+
|
557
|
+
return true;
|
558
|
+
}
|
559
|
+
|
560
|
+
/* Print the last N_LINES lines from the end of the standard input,
|
561
|
+
open for reading as pipe FD.
|
562
|
+
Buffer the text as a linked list of LBUFFERs, adding them as needed.
|
563
|
+
Return true if successful. */
|
564
|
+
|
565
|
+
static bool
|
566
|
+
pipe_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
|
567
|
+
uintmax_t *read_pos)
|
568
|
+
{
|
569
|
+
struct linebuffer
|
570
|
+
{
|
571
|
+
char buffer[BUFSIZ];
|
572
|
+
size_t nbytes;
|
573
|
+
size_t nlines;
|
574
|
+
struct linebuffer *next;
|
575
|
+
};
|
576
|
+
typedef struct linebuffer LBUFFER;
|
577
|
+
LBUFFER *first, *last, *tmp;
|
578
|
+
size_t total_lines = 0; /* Total number of newlines in all buffers. */
|
579
|
+
bool ok = true;
|
580
|
+
size_t n_read; /* Size in bytes of most recent read */
|
581
|
+
|
582
|
+
first = last = xmalloc (sizeof (LBUFFER));
|
583
|
+
first->nbytes = first->nlines = 0;
|
584
|
+
first->next = NULL;
|
585
|
+
tmp = xmalloc (sizeof (LBUFFER));
|
586
|
+
|
587
|
+
/* Input is always read into a fresh buffer. */
|
588
|
+
while (1)
|
589
|
+
{
|
590
|
+
n_read = safe_read (fd, tmp->buffer, BUFSIZ);
|
591
|
+
if (n_read == 0 || n_read == SAFE_READ_ERROR)
|
592
|
+
break;
|
593
|
+
tmp->nbytes = n_read;
|
594
|
+
*read_pos += n_read;
|
595
|
+
tmp->nlines = 0;
|
596
|
+
tmp->next = NULL;
|
597
|
+
|
598
|
+
/* Count the number of newlines just read. */
|
599
|
+
{
|
600
|
+
char const *buffer_end = tmp->buffer + n_read;
|
601
|
+
char const *p = tmp->buffer;
|
602
|
+
while ((p = memchr (p, '\n', buffer_end - p)))
|
603
|
+
{
|
604
|
+
++p;
|
605
|
+
++tmp->nlines;
|
606
|
+
}
|
607
|
+
}
|
608
|
+
total_lines += tmp->nlines;
|
609
|
+
|
610
|
+
/* If there is enough room in the last buffer read, just append the new
|
611
|
+
one to it. This is because when reading from a pipe, `n_read' can
|
612
|
+
often be very small. */
|
613
|
+
if (tmp->nbytes + last->nbytes < BUFSIZ)
|
614
|
+
{
|
615
|
+
memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
|
616
|
+
last->nbytes += tmp->nbytes;
|
617
|
+
last->nlines += tmp->nlines;
|
618
|
+
}
|
619
|
+
else
|
620
|
+
{
|
621
|
+
/* If there's not enough room, link the new buffer onto the end of
|
622
|
+
the list, then either free up the oldest buffer for the next
|
623
|
+
read if that would leave enough lines, or else malloc a new one.
|
624
|
+
Some compaction mechanism is possible but probably not
|
625
|
+
worthwhile. */
|
626
|
+
last = last->next = tmp;
|
627
|
+
if (total_lines - first->nlines > n_lines)
|
628
|
+
{
|
629
|
+
tmp = first;
|
630
|
+
total_lines -= first->nlines;
|
631
|
+
first = first->next;
|
632
|
+
}
|
633
|
+
else
|
634
|
+
tmp = xmalloc (sizeof (LBUFFER));
|
635
|
+
}
|
636
|
+
}
|
637
|
+
|
638
|
+
free (tmp);
|
639
|
+
|
640
|
+
if (n_read == SAFE_READ_ERROR)
|
641
|
+
{
|
642
|
+
error (0, errno, _("error reading %s"), quote (pretty_filename));
|
643
|
+
ok = false;
|
644
|
+
goto free_lbuffers;
|
645
|
+
}
|
646
|
+
|
647
|
+
/* If the file is empty, then bail out. */
|
648
|
+
if (last->nbytes == 0)
|
649
|
+
goto free_lbuffers;
|
650
|
+
|
651
|
+
/* This prevents a core dump when the pipe contains no newlines. */
|
652
|
+
if (n_lines == 0)
|
653
|
+
goto free_lbuffers;
|
654
|
+
|
655
|
+
/* Count the incomplete line on files that don't end with a newline. */
|
656
|
+
if (last->buffer[last->nbytes - 1] != '\n')
|
657
|
+
{
|
658
|
+
++last->nlines;
|
659
|
+
++total_lines;
|
660
|
+
}
|
661
|
+
|
662
|
+
/* Run through the list, printing lines. First, skip over unneeded
|
663
|
+
buffers. */
|
664
|
+
for (tmp = first; total_lines - tmp->nlines > n_lines; tmp = tmp->next)
|
665
|
+
total_lines -= tmp->nlines;
|
666
|
+
|
667
|
+
/* Find the correct beginning, then print the rest of the file. */
|
668
|
+
{
|
669
|
+
char const *beg = tmp->buffer;
|
670
|
+
char const *buffer_end = tmp->buffer + tmp->nbytes;
|
671
|
+
if (total_lines > n_lines)
|
672
|
+
{
|
673
|
+
/* Skip `total_lines' - `n_lines' newlines. We made sure that
|
674
|
+
`total_lines' - `n_lines' <= `tmp->nlines'. */
|
675
|
+
size_t j;
|
676
|
+
for (j = total_lines - n_lines; j; --j)
|
677
|
+
{
|
678
|
+
beg = memchr (beg, '\n', buffer_end - beg);
|
679
|
+
assert (beg);
|
680
|
+
++beg;
|
681
|
+
}
|
682
|
+
}
|
683
|
+
|
684
|
+
xwrite_stdout (beg, buffer_end - beg);
|
685
|
+
}
|
686
|
+
|
687
|
+
for (tmp = tmp->next; tmp; tmp = tmp->next)
|
688
|
+
xwrite_stdout (tmp->buffer, tmp->nbytes);
|
689
|
+
|
690
|
+
free_lbuffers:
|
691
|
+
while (first)
|
692
|
+
{
|
693
|
+
tmp = first->next;
|
694
|
+
free (first);
|
695
|
+
first = tmp;
|
696
|
+
}
|
697
|
+
return ok;
|
698
|
+
}
|
699
|
+
|
700
|
+
/* Print the last N_BYTES characters from the end of pipe FD.
|
701
|
+
This is a stripped down version of pipe_lines.
|
702
|
+
Return true if successful. */
|
703
|
+
|
704
|
+
static bool
|
705
|
+
pipe_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
|
706
|
+
uintmax_t *read_pos)
|
707
|
+
{
|
708
|
+
struct charbuffer
|
709
|
+
{
|
710
|
+
char buffer[BUFSIZ];
|
711
|
+
size_t nbytes;
|
712
|
+
struct charbuffer *next;
|
713
|
+
};
|
714
|
+
typedef struct charbuffer CBUFFER;
|
715
|
+
CBUFFER *first, *last, *tmp;
|
716
|
+
size_t i; /* Index into buffers. */
|
717
|
+
size_t total_bytes = 0; /* Total characters in all buffers. */
|
718
|
+
bool ok = true;
|
719
|
+
size_t n_read;
|
720
|
+
|
721
|
+
first = last = xmalloc (sizeof (CBUFFER));
|
722
|
+
first->nbytes = 0;
|
723
|
+
first->next = NULL;
|
724
|
+
tmp = xmalloc (sizeof (CBUFFER));
|
725
|
+
|
726
|
+
/* Input is always read into a fresh buffer. */
|
727
|
+
while (1)
|
728
|
+
{
|
729
|
+
n_read = safe_read (fd, tmp->buffer, BUFSIZ);
|
730
|
+
if (n_read == 0 || n_read == SAFE_READ_ERROR)
|
731
|
+
break;
|
732
|
+
*read_pos += n_read;
|
733
|
+
tmp->nbytes = n_read;
|
734
|
+
tmp->next = NULL;
|
735
|
+
|
736
|
+
total_bytes += tmp->nbytes;
|
737
|
+
/* If there is enough room in the last buffer read, just append the new
|
738
|
+
one to it. This is because when reading from a pipe, `nbytes' can
|
739
|
+
often be very small. */
|
740
|
+
if (tmp->nbytes + last->nbytes < BUFSIZ)
|
741
|
+
{
|
742
|
+
memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
|
743
|
+
last->nbytes += tmp->nbytes;
|
744
|
+
}
|
745
|
+
else
|
746
|
+
{
|
747
|
+
/* If there's not enough room, link the new buffer onto the end of
|
748
|
+
the list, then either free up the oldest buffer for the next
|
749
|
+
read if that would leave enough characters, or else malloc a new
|
750
|
+
one. Some compaction mechanism is possible but probably not
|
751
|
+
worthwhile. */
|
752
|
+
last = last->next = tmp;
|
753
|
+
if (total_bytes - first->nbytes > n_bytes)
|
754
|
+
{
|
755
|
+
tmp = first;
|
756
|
+
total_bytes -= first->nbytes;
|
757
|
+
first = first->next;
|
758
|
+
}
|
759
|
+
else
|
760
|
+
{
|
761
|
+
tmp = xmalloc (sizeof (CBUFFER));
|
762
|
+
}
|
763
|
+
}
|
764
|
+
}
|
765
|
+
|
766
|
+
free (tmp);
|
767
|
+
|
768
|
+
if (n_read == SAFE_READ_ERROR)
|
769
|
+
{
|
770
|
+
error (0, errno, _("error reading %s"), quote (pretty_filename));
|
771
|
+
ok = false;
|
772
|
+
goto free_cbuffers;
|
773
|
+
}
|
774
|
+
|
775
|
+
/* Run through the list, printing characters. First, skip over unneeded
|
776
|
+
buffers. */
|
777
|
+
for (tmp = first; total_bytes - tmp->nbytes > n_bytes; tmp = tmp->next)
|
778
|
+
total_bytes -= tmp->nbytes;
|
779
|
+
|
780
|
+
/* Find the correct beginning, then print the rest of the file.
|
781
|
+
We made sure that `total_bytes' - `n_bytes' <= `tmp->nbytes'. */
|
782
|
+
if (total_bytes > n_bytes)
|
783
|
+
i = total_bytes - n_bytes;
|
784
|
+
else
|
785
|
+
i = 0;
|
786
|
+
xwrite_stdout (&tmp->buffer[i], tmp->nbytes - i);
|
787
|
+
|
788
|
+
for (tmp = tmp->next; tmp; tmp = tmp->next)
|
789
|
+
xwrite_stdout (tmp->buffer, tmp->nbytes);
|
790
|
+
|
791
|
+
free_cbuffers:
|
792
|
+
while (first)
|
793
|
+
{
|
794
|
+
tmp = first->next;
|
795
|
+
free (first);
|
796
|
+
first = tmp;
|
797
|
+
}
|
798
|
+
return ok;
|
799
|
+
}
|
800
|
+
|
801
|
+
/* Skip N_BYTES characters from the start of pipe FD, and print
|
802
|
+
any extra characters that were read beyond that.
|
803
|
+
Return 1 on error, 0 if ok, -1 if EOF. */
|
804
|
+
|
805
|
+
static int
|
806
|
+
start_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
|
807
|
+
uintmax_t *read_pos)
|
808
|
+
{
|
809
|
+
char buffer[BUFSIZ];
|
810
|
+
|
811
|
+
while (0 < n_bytes)
|
812
|
+
{
|
813
|
+
size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
|
814
|
+
if (bytes_read == 0)
|
815
|
+
return -1;
|
816
|
+
if (bytes_read == SAFE_READ_ERROR)
|
817
|
+
{
|
818
|
+
error (0, errno, _("error reading %s"), quote (pretty_filename));
|
819
|
+
return 1;
|
820
|
+
}
|
821
|
+
*read_pos += bytes_read;
|
822
|
+
if (bytes_read <= n_bytes)
|
823
|
+
n_bytes -= bytes_read;
|
824
|
+
else
|
825
|
+
{
|
826
|
+
size_t n_remaining = bytes_read - n_bytes;
|
827
|
+
if (n_remaining)
|
828
|
+
xwrite_stdout (&buffer[n_bytes], n_remaining);
|
829
|
+
break;
|
830
|
+
}
|
831
|
+
}
|
832
|
+
|
833
|
+
return 0;
|
834
|
+
}
|
835
|
+
|
836
|
+
/* Skip N_LINES lines at the start of file or pipe FD, and print
|
837
|
+
any extra characters that were read beyond that.
|
838
|
+
Return 1 on error, 0 if ok, -1 if EOF. */
|
839
|
+
|
840
|
+
static int
|
841
|
+
start_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
|
842
|
+
uintmax_t *read_pos)
|
843
|
+
{
|
844
|
+
if (n_lines == 0)
|
845
|
+
return 0;
|
846
|
+
|
847
|
+
while (1)
|
848
|
+
{
|
849
|
+
char buffer[BUFSIZ];
|
850
|
+
char *p = buffer;
|
851
|
+
size_t bytes_read = safe_read (fd, buffer, BUFSIZ);
|
852
|
+
char *buffer_end = buffer + bytes_read;
|
853
|
+
if (bytes_read == 0) /* EOF */
|
854
|
+
return -1;
|
855
|
+
if (bytes_read == SAFE_READ_ERROR) /* error */
|
856
|
+
{
|
857
|
+
error (0, errno, _("error reading %s"), quote (pretty_filename));
|
858
|
+
return 1;
|
859
|
+
}
|
860
|
+
|
861
|
+
*read_pos += bytes_read;
|
862
|
+
|
863
|
+
while ((p = memchr (p, '\n', buffer_end - p)))
|
864
|
+
{
|
865
|
+
++p;
|
866
|
+
if (--n_lines == 0)
|
867
|
+
{
|
868
|
+
if (p < buffer_end)
|
869
|
+
xwrite_stdout (p, buffer_end - p);
|
870
|
+
return 0;
|
871
|
+
}
|
872
|
+
}
|
873
|
+
}
|
874
|
+
}
|
875
|
+
|
876
|
+
#if HAVE_INOTIFY
|
877
|
+
/* Without inotify support, always return false. Otherwise, return false
|
878
|
+
when FD is open on a file known to reside on a local file system.
|
879
|
+
If fstatfs fails, give a diagnostic and return true.
|
880
|
+
If fstatfs cannot be called, return true. */
|
881
|
+
static bool
|
882
|
+
fremote (int fd, const char *name)
|
883
|
+
{
|
884
|
+
bool remote = true; /* be conservative (poll by default). */
|
885
|
+
|
886
|
+
# if HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE && defined __linux__
|
887
|
+
struct statfs buf;
|
888
|
+
int err = fstatfs (fd, &buf);
|
889
|
+
if (err != 0)
|
890
|
+
{
|
891
|
+
/* On at least linux-2.6.38, fstatfs fails with ENOSYS when FD
|
892
|
+
is open on a pipe. Treat that like a remote file. */
|
893
|
+
if (errno != ENOSYS)
|
894
|
+
error (0, errno, _("cannot determine location of %s. "
|
895
|
+
"reverting to polling"), quote (name));
|
896
|
+
}
|
897
|
+
else
|
898
|
+
{
|
899
|
+
switch (buf.f_type)
|
900
|
+
{
|
901
|
+
case S_MAGIC_AFS:
|
902
|
+
case S_MAGIC_CIFS:
|
903
|
+
case S_MAGIC_CODA:
|
904
|
+
case S_MAGIC_FUSEBLK:
|
905
|
+
case S_MAGIC_FUSECTL:
|
906
|
+
case S_MAGIC_GFS:
|
907
|
+
case S_MAGIC_KAFS:
|
908
|
+
case S_MAGIC_LUSTRE:
|
909
|
+
case S_MAGIC_NCP:
|
910
|
+
case S_MAGIC_NFS:
|
911
|
+
case S_MAGIC_NFSD:
|
912
|
+
case S_MAGIC_OCFS2:
|
913
|
+
case S_MAGIC_SMB:
|
914
|
+
break;
|
915
|
+
default:
|
916
|
+
remote = false;
|
917
|
+
}
|
918
|
+
}
|
919
|
+
# endif
|
920
|
+
|
921
|
+
return remote;
|
922
|
+
}
|
923
|
+
#else
|
924
|
+
/* Without inotify support, whether a file is remote is irrelevant.
|
925
|
+
Always return "false" in that case. */
|
926
|
+
# define fremote(fd, name) false
|
927
|
+
#endif
|
928
|
+
|
929
|
+
/* FIXME: describe */
|
930
|
+
|
931
|
+
static void
|
932
|
+
recheck (struct File_spec *f, bool blocking)
|
933
|
+
{
|
934
|
+
/* open/fstat the file and announce if dev/ino have changed */
|
935
|
+
struct stat new_stats;
|
936
|
+
bool ok = true;
|
937
|
+
bool is_stdin = (STREQ (f->name, "-"));
|
938
|
+
bool was_tailable = f->tailable;
|
939
|
+
int prev_errnum = f->errnum;
|
940
|
+
bool new_file;
|
941
|
+
int fd = (is_stdin
|
942
|
+
? STDIN_FILENO
|
943
|
+
: open (f->name, O_RDONLY | (blocking ? 0 : O_NONBLOCK)));
|
944
|
+
|
945
|
+
assert (valid_file_spec (f));
|
946
|
+
|
947
|
+
/* If the open fails because the file doesn't exist,
|
948
|
+
then mark the file as not tailable. */
|
949
|
+
f->tailable = !(reopen_inaccessible_files && fd == -1);
|
950
|
+
|
951
|
+
if (fd == -1 || fstat (fd, &new_stats) < 0)
|
952
|
+
{
|
953
|
+
ok = false;
|
954
|
+
f->errnum = errno;
|
955
|
+
if (!f->tailable)
|
956
|
+
{
|
957
|
+
if (was_tailable)
|
958
|
+
{
|
959
|
+
/* FIXME-maybe: detect the case in which the file first becomes
|
960
|
+
unreadable (perms), and later becomes readable again and can
|
961
|
+
be seen to be the same file (dev/ino). Otherwise, tail prints
|
962
|
+
the entire contents of the file when it becomes readable. */
|
963
|
+
error (0, f->errnum, _("%s has become inaccessible"),
|
964
|
+
quote (pretty_name (f)));
|
965
|
+
}
|
966
|
+
else
|
967
|
+
{
|
968
|
+
/* say nothing... it's still not tailable */
|
969
|
+
}
|
970
|
+
}
|
971
|
+
else if (prev_errnum != errno)
|
972
|
+
{
|
973
|
+
error (0, errno, "%s", pretty_name (f));
|
974
|
+
}
|
975
|
+
}
|
976
|
+
else if (!IS_TAILABLE_FILE_TYPE (new_stats.st_mode))
|
977
|
+
{
|
978
|
+
ok = false;
|
979
|
+
f->errnum = -1;
|
980
|
+
error (0, 0, _("%s has been replaced with an untailable file;\
|
981
|
+
giving up on this name"),
|
982
|
+
quote (pretty_name (f)));
|
983
|
+
f->ignore = true;
|
984
|
+
}
|
985
|
+
else if (!disable_inotify && fremote (fd, pretty_name (f)))
|
986
|
+
{
|
987
|
+
ok = false;
|
988
|
+
f->errnum = -1;
|
989
|
+
error (0, 0, _("%s has been replaced with a remote file. "
|
990
|
+
"giving up on this name"), quote (pretty_name (f)));
|
991
|
+
f->ignore = true;
|
992
|
+
f->remote = true;
|
993
|
+
}
|
994
|
+
else
|
995
|
+
{
|
996
|
+
f->errnum = 0;
|
997
|
+
}
|
998
|
+
|
999
|
+
new_file = false;
|
1000
|
+
if (!ok)
|
1001
|
+
{
|
1002
|
+
close_fd (fd, pretty_name (f));
|
1003
|
+
close_fd (f->fd, pretty_name (f));
|
1004
|
+
f->fd = -1;
|
1005
|
+
}
|
1006
|
+
else if (prev_errnum && prev_errnum != ENOENT)
|
1007
|
+
{
|
1008
|
+
new_file = true;
|
1009
|
+
assert (f->fd == -1);
|
1010
|
+
error (0, 0, _("%s has become accessible"), quote (pretty_name (f)));
|
1011
|
+
}
|
1012
|
+
else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
|
1013
|
+
{
|
1014
|
+
new_file = true;
|
1015
|
+
if (f->fd == -1)
|
1016
|
+
{
|
1017
|
+
error (0, 0,
|
1018
|
+
_("%s has appeared; following end of new file"),
|
1019
|
+
quote (pretty_name (f)));
|
1020
|
+
}
|
1021
|
+
else
|
1022
|
+
{
|
1023
|
+
/* Close the old one. */
|
1024
|
+
close_fd (f->fd, pretty_name (f));
|
1025
|
+
|
1026
|
+
/* File has been replaced (e.g., via log rotation) --
|
1027
|
+
tail the new one. */
|
1028
|
+
error (0, 0,
|
1029
|
+
_("%s has been replaced; following end of new file"),
|
1030
|
+
quote (pretty_name (f)));
|
1031
|
+
}
|
1032
|
+
}
|
1033
|
+
else
|
1034
|
+
{
|
1035
|
+
if (f->fd == -1)
|
1036
|
+
{
|
1037
|
+
/* This happens when one iteration finds the file missing,
|
1038
|
+
then the preceding <dev,inode> pair is reused as the
|
1039
|
+
file is recreated. */
|
1040
|
+
new_file = true;
|
1041
|
+
}
|
1042
|
+
else
|
1043
|
+
{
|
1044
|
+
close_fd (fd, pretty_name (f));
|
1045
|
+
}
|
1046
|
+
}
|
1047
|
+
|
1048
|
+
if (new_file)
|
1049
|
+
{
|
1050
|
+
if (print_headers)
|
1051
|
+
printf ("==> %s <== [new_file]\n", pretty_name (f));
|
1052
|
+
/* Start at the beginning of the file. */
|
1053
|
+
record_open_fd (f, fd, 0, &new_stats, (is_stdin ? -1 : blocking));
|
1054
|
+
xlseek (fd, 0, SEEK_SET, pretty_name (f));
|
1055
|
+
}
|
1056
|
+
}
|
1057
|
+
|
1058
|
+
/* Return true if any of the N_FILES files in F are live, i.e., have
|
1059
|
+
open file descriptors. */
|
1060
|
+
|
1061
|
+
static bool
|
1062
|
+
any_live_files (const struct File_spec *f, size_t n_files)
|
1063
|
+
{
|
1064
|
+
size_t i;
|
1065
|
+
|
1066
|
+
for (i = 0; i < n_files; i++)
|
1067
|
+
if (0 <= f[i].fd)
|
1068
|
+
return true;
|
1069
|
+
return false;
|
1070
|
+
}
|
1071
|
+
|
1072
|
+
/* Tail N_FILES files forever, or until killed.
|
1073
|
+
The pertinent information for each file is stored in an entry of F.
|
1074
|
+
Loop over each of them, doing an fstat to see if they have changed size,
|
1075
|
+
and an occasional open/fstat to see if any dev/ino pair has changed.
|
1076
|
+
If none of them have changed size in one iteration, sleep for a
|
1077
|
+
while and try again. Continue until the user interrupts us. */
|
1078
|
+
|
1079
|
+
static void
|
1080
|
+
tail_forever (struct File_spec *f, size_t n_files, double sleep_interval)
|
1081
|
+
{
|
1082
|
+
/* Use blocking I/O as an optimization, when it's easy. */
|
1083
|
+
bool blocking = (pid == 0 && follow_mode == Follow_descriptor
|
1084
|
+
&& n_files == 1 && ! S_ISREG (f[0].mode));
|
1085
|
+
size_t last;
|
1086
|
+
bool writer_is_dead = false;
|
1087
|
+
|
1088
|
+
last = n_files - 1;
|
1089
|
+
|
1090
|
+
while (1)
|
1091
|
+
{
|
1092
|
+
size_t i;
|
1093
|
+
bool any_input = false;
|
1094
|
+
|
1095
|
+
for (i = 0; i < n_files; i++)
|
1096
|
+
{
|
1097
|
+
int fd;
|
1098
|
+
char const *name;
|
1099
|
+
mode_t mode;
|
1100
|
+
struct stat stats;
|
1101
|
+
uintmax_t bytes_read;
|
1102
|
+
|
1103
|
+
if (f[i].ignore)
|
1104
|
+
continue;
|
1105
|
+
|
1106
|
+
if (f[i].fd < 0)
|
1107
|
+
{
|
1108
|
+
recheck (&f[i], blocking);
|
1109
|
+
continue;
|
1110
|
+
}
|
1111
|
+
|
1112
|
+
fd = f[i].fd;
|
1113
|
+
name = pretty_name (&f[i]);
|
1114
|
+
mode = f[i].mode;
|
1115
|
+
|
1116
|
+
if (f[i].blocking != blocking)
|
1117
|
+
{
|
1118
|
+
int old_flags = fcntl (fd, F_GETFL);
|
1119
|
+
int new_flags = old_flags | (blocking ? 0 : O_NONBLOCK);
|
1120
|
+
if (old_flags < 0
|
1121
|
+
|| (new_flags != old_flags
|
1122
|
+
&& fcntl (fd, F_SETFL, new_flags) == -1))
|
1123
|
+
{
|
1124
|
+
/* Don't update f[i].blocking if fcntl fails. */
|
1125
|
+
if (S_ISREG (f[i].mode) && errno == EPERM)
|
1126
|
+
{
|
1127
|
+
/* This happens when using tail -f on a file with
|
1128
|
+
the append-only attribute. */
|
1129
|
+
}
|
1130
|
+
else
|
1131
|
+
error (EXIT_FAILURE, errno,
|
1132
|
+
_("%s: cannot change nonblocking mode"), name);
|
1133
|
+
}
|
1134
|
+
else
|
1135
|
+
f[i].blocking = blocking;
|
1136
|
+
}
|
1137
|
+
|
1138
|
+
if (!f[i].blocking)
|
1139
|
+
{
|
1140
|
+
if (fstat (fd, &stats) != 0)
|
1141
|
+
{
|
1142
|
+
f[i].fd = -1;
|
1143
|
+
f[i].errnum = errno;
|
1144
|
+
error (0, errno, "%s", name);
|
1145
|
+
continue;
|
1146
|
+
}
|
1147
|
+
|
1148
|
+
if (f[i].mode == stats.st_mode
|
1149
|
+
&& (! S_ISREG (stats.st_mode) || f[i].size == stats.st_size)
|
1150
|
+
&& timespec_cmp (f[i].mtime, get_stat_mtime (&stats)) == 0)
|
1151
|
+
{
|
1152
|
+
if ((max_n_unchanged_stats_between_opens
|
1153
|
+
<= f[i].n_unchanged_stats++)
|
1154
|
+
&& follow_mode == Follow_name)
|
1155
|
+
{
|
1156
|
+
recheck (&f[i], f[i].blocking);
|
1157
|
+
f[i].n_unchanged_stats = 0;
|
1158
|
+
}
|
1159
|
+
continue;
|
1160
|
+
}
|
1161
|
+
|
1162
|
+
/* This file has changed. Print out what we can, and
|
1163
|
+
then keep looping. */
|
1164
|
+
|
1165
|
+
f[i].mtime = get_stat_mtime (&stats);
|
1166
|
+
f[i].mode = stats.st_mode;
|
1167
|
+
|
1168
|
+
/* reset counter */
|
1169
|
+
f[i].n_unchanged_stats = 0;
|
1170
|
+
|
1171
|
+
if (S_ISREG (mode) && stats.st_size < f[i].size)
|
1172
|
+
{
|
1173
|
+
error (0, 0, _("%s: file truncated"), name);
|
1174
|
+
if (print_headers)
|
1175
|
+
printf ("==> %s <== [truncated]\n", name);
|
1176
|
+
last = i;
|
1177
|
+
xlseek (fd, stats.st_size, SEEK_SET, name);
|
1178
|
+
f[i].size = stats.st_size;
|
1179
|
+
continue;
|
1180
|
+
}
|
1181
|
+
|
1182
|
+
if (i != last)
|
1183
|
+
{
|
1184
|
+
if (print_headers)
|
1185
|
+
write_header (name);
|
1186
|
+
last = i;
|
1187
|
+
}
|
1188
|
+
}
|
1189
|
+
|
1190
|
+
bytes_read = dump_remainder (name, fd,
|
1191
|
+
(f[i].blocking
|
1192
|
+
? COPY_A_BUFFER : COPY_TO_EOF));
|
1193
|
+
any_input |= (bytes_read != 0);
|
1194
|
+
f[i].size += bytes_read;
|
1195
|
+
}
|
1196
|
+
|
1197
|
+
if (! any_live_files (f, n_files) && ! reopen_inaccessible_files)
|
1198
|
+
{
|
1199
|
+
error (0, 0, _("no files remaining"));
|
1200
|
+
break;
|
1201
|
+
}
|
1202
|
+
|
1203
|
+
if ((!any_input || blocking) && fflush (stdout) != 0)
|
1204
|
+
error (EXIT_FAILURE, errno, _("write error"));
|
1205
|
+
|
1206
|
+
/* If nothing was read, sleep and/or check for dead writers. */
|
1207
|
+
if (!any_input)
|
1208
|
+
{
|
1209
|
+
if (writer_is_dead)
|
1210
|
+
break;
|
1211
|
+
|
1212
|
+
/* Once the writer is dead, read the files once more to
|
1213
|
+
avoid a race condition. */
|
1214
|
+
writer_is_dead = (pid != 0
|
1215
|
+
&& kill (pid, 0) != 0
|
1216
|
+
/* Handle the case in which you cannot send a
|
1217
|
+
signal to the writer, so kill fails and sets
|
1218
|
+
errno to EPERM. */
|
1219
|
+
&& errno != EPERM);
|
1220
|
+
|
1221
|
+
if (!writer_is_dead && xnanosleep (sleep_interval))
|
1222
|
+
error (EXIT_FAILURE, errno, _("cannot read realtime clock"));
|
1223
|
+
|
1224
|
+
}
|
1225
|
+
}
|
1226
|
+
}
|
1227
|
+
|
1228
|
+
#if HAVE_INOTIFY
|
1229
|
+
|
1230
|
+
/* Return true if any of the N_FILES files in F is remote, i.e., has
|
1231
|
+
an open file descriptor and is on a network file system. */
|
1232
|
+
|
1233
|
+
static bool
|
1234
|
+
any_remote_file (const struct File_spec *f, size_t n_files)
|
1235
|
+
{
|
1236
|
+
size_t i;
|
1237
|
+
|
1238
|
+
for (i = 0; i < n_files; i++)
|
1239
|
+
if (0 <= f[i].fd && f[i].remote)
|
1240
|
+
return true;
|
1241
|
+
return false;
|
1242
|
+
}
|
1243
|
+
|
1244
|
+
/* Return true if any of the N_FILES files in F represents
|
1245
|
+
stdin and is tailable. */
|
1246
|
+
|
1247
|
+
static bool
|
1248
|
+
tailable_stdin (const struct File_spec *f, size_t n_files)
|
1249
|
+
{
|
1250
|
+
size_t i;
|
1251
|
+
|
1252
|
+
for (i = 0; i < n_files; i++)
|
1253
|
+
if (!f[i].ignore && STREQ (f[i].name, "-"))
|
1254
|
+
return true;
|
1255
|
+
return false;
|
1256
|
+
}
|
1257
|
+
|
1258
|
+
static size_t
|
1259
|
+
wd_hasher (const void *entry, size_t tabsize)
|
1260
|
+
{
|
1261
|
+
const struct File_spec *spec = entry;
|
1262
|
+
return spec->wd % tabsize;
|
1263
|
+
}
|
1264
|
+
|
1265
|
+
static bool
|
1266
|
+
wd_comparator (const void *e1, const void *e2)
|
1267
|
+
{
|
1268
|
+
const struct File_spec *spec1 = e1;
|
1269
|
+
const struct File_spec *spec2 = e2;
|
1270
|
+
return spec1->wd == spec2->wd;
|
1271
|
+
}
|
1272
|
+
|
1273
|
+
/* Helper function used by `tail_forever_inotify'. */
|
1274
|
+
static void
|
1275
|
+
check_fspec (struct File_spec *fspec, int wd, int *prev_wd)
|
1276
|
+
{
|
1277
|
+
struct stat stats;
|
1278
|
+
char const *name = pretty_name (fspec);
|
1279
|
+
|
1280
|
+
if (fstat (fspec->fd, &stats) != 0)
|
1281
|
+
{
|
1282
|
+
close_fd (fspec->fd, name);
|
1283
|
+
fspec->fd = -1;
|
1284
|
+
fspec->errnum = errno;
|
1285
|
+
return;
|
1286
|
+
}
|
1287
|
+
|
1288
|
+
if (S_ISREG (fspec->mode) && stats.st_size < fspec->size)
|
1289
|
+
{
|
1290
|
+
error (0, 0, _("%s: file truncated"), name);
|
1291
|
+
if (print_headers)
|
1292
|
+
printf ("==> %s <== [truncated]\n", name);
|
1293
|
+
*prev_wd = wd;
|
1294
|
+
xlseek (fspec->fd, stats.st_size, SEEK_SET, name);
|
1295
|
+
fspec->size = stats.st_size;
|
1296
|
+
}
|
1297
|
+
else if (S_ISREG (fspec->mode) && stats.st_size == fspec->size
|
1298
|
+
&& timespec_cmp (fspec->mtime, get_stat_mtime (&stats)) == 0)
|
1299
|
+
return;
|
1300
|
+
|
1301
|
+
if (wd != *prev_wd)
|
1302
|
+
{
|
1303
|
+
if (print_headers)
|
1304
|
+
write_header (name);
|
1305
|
+
*prev_wd = wd;
|
1306
|
+
}
|
1307
|
+
|
1308
|
+
uintmax_t bytes_read = dump_remainder (name, fspec->fd, COPY_TO_EOF);
|
1309
|
+
fspec->size += bytes_read;
|
1310
|
+
|
1311
|
+
if (fflush (stdout) != 0)
|
1312
|
+
error (EXIT_FAILURE, errno, _("write error"));
|
1313
|
+
}
|
1314
|
+
|
1315
|
+
/* Attempt to tail N_FILES files forever, or until killed.
|
1316
|
+
Check modifications using the inotify events system.
|
1317
|
+
Return false on error, or true to revert to polling. */
|
1318
|
+
static bool
|
1319
|
+
tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
|
1320
|
+
double sleep_interval)
|
1321
|
+
{
|
1322
|
+
unsigned int max_realloc = 3;
|
1323
|
+
|
1324
|
+
/* Map an inotify watch descriptor to the name of the file it's watching. */
|
1325
|
+
Hash_table *wd_to_name;
|
1326
|
+
|
1327
|
+
bool found_watchable_file = false;
|
1328
|
+
bool found_unwatchable_dir = false;
|
1329
|
+
bool no_inotify_resources = false;
|
1330
|
+
bool writer_is_dead = false;
|
1331
|
+
int prev_wd;
|
1332
|
+
size_t evlen = 0;
|
1333
|
+
char *evbuf;
|
1334
|
+
size_t evbuf_off = 0;
|
1335
|
+
size_t len = 0;
|
1336
|
+
|
1337
|
+
wd_to_name = hash_initialize (n_files, NULL, wd_hasher, wd_comparator, NULL);
|
1338
|
+
if (! wd_to_name)
|
1339
|
+
xalloc_die ();
|
1340
|
+
|
1341
|
+
/* Add an inotify watch for each watched file. If -F is specified then watch
|
1342
|
+
its parent directory too, in this way when they re-appear we can add them
|
1343
|
+
again to the watch list. */
|
1344
|
+
size_t i;
|
1345
|
+
for (i = 0; i < n_files; i++)
|
1346
|
+
{
|
1347
|
+
if (!f[i].ignore)
|
1348
|
+
{
|
1349
|
+
size_t fnlen = strlen (f[i].name);
|
1350
|
+
if (evlen < fnlen)
|
1351
|
+
evlen = fnlen;
|
1352
|
+
|
1353
|
+
f[i].wd = -1;
|
1354
|
+
|
1355
|
+
if (follow_mode == Follow_name)
|
1356
|
+
{
|
1357
|
+
size_t dirlen = dir_len (f[i].name);
|
1358
|
+
char prev = f[i].name[dirlen];
|
1359
|
+
f[i].basename_start = last_component (f[i].name) - f[i].name;
|
1360
|
+
|
1361
|
+
f[i].name[dirlen] = '\0';
|
1362
|
+
|
1363
|
+
/* It's fine to add the same directory more than once.
|
1364
|
+
In that case the same watch descriptor is returned. */
|
1365
|
+
f[i].parent_wd = inotify_add_watch (wd, dirlen ? f[i].name : ".",
|
1366
|
+
(IN_CREATE | IN_MOVED_TO
|
1367
|
+
| IN_ATTRIB));
|
1368
|
+
|
1369
|
+
f[i].name[dirlen] = prev;
|
1370
|
+
|
1371
|
+
if (f[i].parent_wd < 0)
|
1372
|
+
{
|
1373
|
+
if (errno != ENOSPC) /* suppress confusing error. */
|
1374
|
+
error (0, errno, _("cannot watch parent directory of %s"),
|
1375
|
+
quote (f[i].name));
|
1376
|
+
else
|
1377
|
+
error (0, 0, _("inotify resources exhausted"));
|
1378
|
+
found_unwatchable_dir = true;
|
1379
|
+
/* We revert to polling below. Note invalid uses
|
1380
|
+
of the inotify API will still be diagnosed. */
|
1381
|
+
break;
|
1382
|
+
}
|
1383
|
+
}
|
1384
|
+
|
1385
|
+
f[i].wd = inotify_add_watch (wd, f[i].name, inotify_wd_mask);
|
1386
|
+
|
1387
|
+
if (f[i].wd < 0)
|
1388
|
+
{
|
1389
|
+
if (errno == ENOSPC)
|
1390
|
+
{
|
1391
|
+
no_inotify_resources = true;
|
1392
|
+
error (0, 0, _("inotify resources exhausted"));
|
1393
|
+
}
|
1394
|
+
else if (errno != f[i].errnum)
|
1395
|
+
error (0, errno, _("cannot watch %s"), quote (f[i].name));
|
1396
|
+
continue;
|
1397
|
+
}
|
1398
|
+
|
1399
|
+
if (hash_insert (wd_to_name, &(f[i])) == NULL)
|
1400
|
+
xalloc_die ();
|
1401
|
+
|
1402
|
+
found_watchable_file = true;
|
1403
|
+
}
|
1404
|
+
}
|
1405
|
+
|
1406
|
+
/* Linux kernel 2.6.24 at least has a bug where eventually, ENOSPC is always
|
1407
|
+
returned by inotify_add_watch. In any case we should revert to polling
|
1408
|
+
when there are no inotify resources. Also a specified directory may not
|
1409
|
+
be currently present or accessible, so revert to polling. */
|
1410
|
+
if (no_inotify_resources || found_unwatchable_dir)
|
1411
|
+
{
|
1412
|
+
/* FIXME: release hash and inotify resources allocated above. */
|
1413
|
+
errno = 0;
|
1414
|
+
return true;
|
1415
|
+
}
|
1416
|
+
if (follow_mode == Follow_descriptor && !found_watchable_file)
|
1417
|
+
return false;
|
1418
|
+
|
1419
|
+
prev_wd = f[n_files - 1].wd;
|
1420
|
+
|
1421
|
+
/* Check files again. New data can be available since last time we checked
|
1422
|
+
and before they are watched by inotify. */
|
1423
|
+
for (i = 0; i < n_files; i++)
|
1424
|
+
{
|
1425
|
+
if (!f[i].ignore)
|
1426
|
+
check_fspec (&f[i], f[i].wd, &prev_wd);
|
1427
|
+
}
|
1428
|
+
|
1429
|
+
evlen += sizeof (struct inotify_event) + 1;
|
1430
|
+
evbuf = xmalloc (evlen);
|
1431
|
+
|
1432
|
+
/* Wait for inotify events and handle them. Events on directories
|
1433
|
+
ensure that watched files can be re-added when following by name.
|
1434
|
+
This loop blocks on the `safe_read' call until a new event is notified.
|
1435
|
+
But when --pid=P is specified, tail usually waits via the select. */
|
1436
|
+
while (1)
|
1437
|
+
{
|
1438
|
+
struct File_spec *fspec;
|
1439
|
+
struct inotify_event *ev;
|
1440
|
+
|
1441
|
+
/* When following by name without --retry, and the last file has
|
1442
|
+
been unlinked or renamed-away, diagnose it and return. */
|
1443
|
+
if (follow_mode == Follow_name
|
1444
|
+
&& ! reopen_inaccessible_files
|
1445
|
+
&& hash_get_n_entries (wd_to_name) == 0)
|
1446
|
+
{
|
1447
|
+
error (0, 0, _("no files remaining"));
|
1448
|
+
return false;
|
1449
|
+
}
|
1450
|
+
|
1451
|
+
/* When watching a PID, ensure that a read from WD will not block
|
1452
|
+
indefinitely. */
|
1453
|
+
if (pid)
|
1454
|
+
{
|
1455
|
+
if (writer_is_dead)
|
1456
|
+
exit (EXIT_SUCCESS);
|
1457
|
+
|
1458
|
+
writer_is_dead = (kill (pid, 0) != 0 && errno != EPERM);
|
1459
|
+
|
1460
|
+
struct timeval delay; /* how long to wait for file changes. */
|
1461
|
+
if (writer_is_dead)
|
1462
|
+
delay.tv_sec = delay.tv_usec = 0;
|
1463
|
+
else
|
1464
|
+
{
|
1465
|
+
delay.tv_sec = (time_t) sleep_interval;
|
1466
|
+
delay.tv_usec = 1000000 * (sleep_interval - delay.tv_sec);
|
1467
|
+
}
|
1468
|
+
|
1469
|
+
fd_set rfd;
|
1470
|
+
FD_ZERO (&rfd);
|
1471
|
+
FD_SET (wd, &rfd);
|
1472
|
+
|
1473
|
+
int file_change = select (wd + 1, &rfd, NULL, NULL, &delay);
|
1474
|
+
|
1475
|
+
if (file_change == 0)
|
1476
|
+
continue;
|
1477
|
+
else if (file_change == -1)
|
1478
|
+
error (EXIT_FAILURE, errno, _("error monitoring inotify event"));
|
1479
|
+
}
|
1480
|
+
|
1481
|
+
if (len <= evbuf_off)
|
1482
|
+
{
|
1483
|
+
len = safe_read (wd, evbuf, evlen);
|
1484
|
+
evbuf_off = 0;
|
1485
|
+
|
1486
|
+
/* For kernels prior to 2.6.21, read returns 0 when the buffer
|
1487
|
+
is too small. */
|
1488
|
+
if ((len == 0 || (len == SAFE_READ_ERROR && errno == EINVAL))
|
1489
|
+
&& max_realloc--)
|
1490
|
+
{
|
1491
|
+
len = 0;
|
1492
|
+
evlen *= 2;
|
1493
|
+
evbuf = xrealloc (evbuf, evlen);
|
1494
|
+
continue;
|
1495
|
+
}
|
1496
|
+
|
1497
|
+
if (len == 0 || len == SAFE_READ_ERROR)
|
1498
|
+
error (EXIT_FAILURE, errno, _("error reading inotify event"));
|
1499
|
+
}
|
1500
|
+
|
1501
|
+
ev = (struct inotify_event *) (evbuf + evbuf_off);
|
1502
|
+
evbuf_off += sizeof (*ev) + ev->len;
|
1503
|
+
|
1504
|
+
if (ev->len) /* event on ev->name in watched directory */
|
1505
|
+
{
|
1506
|
+
size_t j;
|
1507
|
+
for (j = 0; j < n_files; j++)
|
1508
|
+
{
|
1509
|
+
/* With N=hundreds of frequently-changing files, this O(N^2)
|
1510
|
+
process might be a problem. FIXME: use a hash table? */
|
1511
|
+
if (f[j].parent_wd == ev->wd
|
1512
|
+
&& STREQ (ev->name, f[j].name + f[j].basename_start))
|
1513
|
+
break;
|
1514
|
+
}
|
1515
|
+
|
1516
|
+
/* It is not a watched file. */
|
1517
|
+
if (j == n_files)
|
1518
|
+
continue;
|
1519
|
+
|
1520
|
+
/* It's fine to add the same file more than once. */
|
1521
|
+
int new_wd = inotify_add_watch (wd, f[j].name, inotify_wd_mask);
|
1522
|
+
if (new_wd < 0)
|
1523
|
+
{
|
1524
|
+
error (0, errno, _("cannot watch %s"), quote (f[j].name));
|
1525
|
+
continue;
|
1526
|
+
}
|
1527
|
+
|
1528
|
+
fspec = &(f[j]);
|
1529
|
+
|
1530
|
+
/* Remove `fspec' and re-add it using `new_fd' as its key. */
|
1531
|
+
hash_delete (wd_to_name, fspec);
|
1532
|
+
fspec->wd = new_wd;
|
1533
|
+
|
1534
|
+
/* If the file was moved then inotify will use the source file wd for
|
1535
|
+
the destination file. Make sure the key is not present in the
|
1536
|
+
table. */
|
1537
|
+
struct File_spec *prev = hash_delete (wd_to_name, fspec);
|
1538
|
+
if (prev && prev != fspec)
|
1539
|
+
{
|
1540
|
+
if (follow_mode == Follow_name)
|
1541
|
+
recheck (prev, false);
|
1542
|
+
prev->wd = -1;
|
1543
|
+
close_fd (prev->fd, pretty_name (prev));
|
1544
|
+
}
|
1545
|
+
|
1546
|
+
if (hash_insert (wd_to_name, fspec) == NULL)
|
1547
|
+
xalloc_die ();
|
1548
|
+
|
1549
|
+
if (follow_mode == Follow_name)
|
1550
|
+
recheck (fspec, false);
|
1551
|
+
}
|
1552
|
+
else
|
1553
|
+
{
|
1554
|
+
struct File_spec key;
|
1555
|
+
key.wd = ev->wd;
|
1556
|
+
fspec = hash_lookup (wd_to_name, &key);
|
1557
|
+
}
|
1558
|
+
|
1559
|
+
if (! fspec)
|
1560
|
+
continue;
|
1561
|
+
|
1562
|
+
if (ev->mask & (IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF))
|
1563
|
+
{
|
1564
|
+
/* For IN_DELETE_SELF, we always want to remove the watch.
|
1565
|
+
However, for IN_MOVE_SELF (the file we're watching has
|
1566
|
+
been clobbered via a rename), when tailing by NAME, we
|
1567
|
+
must continue to watch the file. It's only when following
|
1568
|
+
by file descriptor that we must remove the watch. */
|
1569
|
+
if ((ev->mask & IN_DELETE_SELF)
|
1570
|
+
|| ((ev->mask & IN_MOVE_SELF)
|
1571
|
+
&& follow_mode == Follow_descriptor))
|
1572
|
+
{
|
1573
|
+
inotify_rm_watch (wd, fspec->wd);
|
1574
|
+
hash_delete (wd_to_name, fspec);
|
1575
|
+
}
|
1576
|
+
if (follow_mode == Follow_name)
|
1577
|
+
recheck (fspec, false);
|
1578
|
+
|
1579
|
+
continue;
|
1580
|
+
}
|
1581
|
+
check_fspec (fspec, ev->wd, &prev_wd);
|
1582
|
+
}
|
1583
|
+
}
|
1584
|
+
#endif
|
1585
|
+
|
1586
|
+
/* Output the last N_BYTES bytes of file FILENAME open for reading in FD.
|
1587
|
+
Return true if successful. */
|
1588
|
+
|
1589
|
+
static bool
|
1590
|
+
tail_bytes (const char *pretty_filename, int fd, uintmax_t n_bytes,
|
1591
|
+
uintmax_t *read_pos)
|
1592
|
+
{
|
1593
|
+
struct stat stats;
|
1594
|
+
|
1595
|
+
if (fstat (fd, &stats))
|
1596
|
+
{
|
1597
|
+
error (0, errno, _("cannot fstat %s"), quote (pretty_filename));
|
1598
|
+
return false;
|
1599
|
+
}
|
1600
|
+
|
1601
|
+
if (from_start)
|
1602
|
+
{
|
1603
|
+
if ( ! presume_input_pipe
|
1604
|
+
&& S_ISREG (stats.st_mode) && n_bytes <= OFF_T_MAX)
|
1605
|
+
{
|
1606
|
+
xlseek (fd, n_bytes, SEEK_CUR, pretty_filename);
|
1607
|
+
*read_pos += n_bytes;
|
1608
|
+
}
|
1609
|
+
else
|
1610
|
+
{
|
1611
|
+
int t = start_bytes (pretty_filename, fd, n_bytes, read_pos);
|
1612
|
+
if (t)
|
1613
|
+
return t < 0;
|
1614
|
+
}
|
1615
|
+
*read_pos += dump_remainder (pretty_filename, fd, COPY_TO_EOF);
|
1616
|
+
}
|
1617
|
+
else
|
1618
|
+
{
|
1619
|
+
if ( ! presume_input_pipe
|
1620
|
+
&& S_ISREG (stats.st_mode) && n_bytes <= OFF_T_MAX)
|
1621
|
+
{
|
1622
|
+
off_t current_pos = xlseek (fd, 0, SEEK_CUR, pretty_filename);
|
1623
|
+
off_t end_pos = xlseek (fd, 0, SEEK_END, pretty_filename);
|
1624
|
+
off_t diff = end_pos - current_pos;
|
1625
|
+
/* Be careful here. The current position may actually be
|
1626
|
+
beyond the end of the file. */
|
1627
|
+
off_t bytes_remaining = diff < 0 ? 0 : diff;
|
1628
|
+
off_t nb = n_bytes;
|
1629
|
+
|
1630
|
+
if (bytes_remaining <= nb)
|
1631
|
+
{
|
1632
|
+
/* From the current position to end of file, there are no
|
1633
|
+
more bytes than have been requested. So reposition the
|
1634
|
+
file pointer to the incoming current position and print
|
1635
|
+
everything after that. */
|
1636
|
+
*read_pos = xlseek (fd, current_pos, SEEK_SET, pretty_filename);
|
1637
|
+
}
|
1638
|
+
else
|
1639
|
+
{
|
1640
|
+
/* There are more bytes remaining than were requested.
|
1641
|
+
Back up. */
|
1642
|
+
*read_pos = xlseek (fd, -nb, SEEK_END, pretty_filename);
|
1643
|
+
}
|
1644
|
+
*read_pos += dump_remainder (pretty_filename, fd, n_bytes);
|
1645
|
+
}
|
1646
|
+
else
|
1647
|
+
return pipe_bytes (pretty_filename, fd, n_bytes, read_pos);
|
1648
|
+
}
|
1649
|
+
return true;
|
1650
|
+
}
|
1651
|
+
|
1652
|
+
/* Output the last N_LINES lines of file FILENAME open for reading in FD.
|
1653
|
+
Return true if successful. */
|
1654
|
+
|
1655
|
+
static bool
|
1656
|
+
tail_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
|
1657
|
+
uintmax_t *read_pos)
|
1658
|
+
{
|
1659
|
+
struct stat stats;
|
1660
|
+
|
1661
|
+
if (fstat (fd, &stats))
|
1662
|
+
{
|
1663
|
+
error (0, errno, _("cannot fstat %s"), quote (pretty_filename));
|
1664
|
+
return false;
|
1665
|
+
}
|
1666
|
+
|
1667
|
+
if (from_start)
|
1668
|
+
{
|
1669
|
+
int t = start_lines (pretty_filename, fd, n_lines, read_pos);
|
1670
|
+
if (t)
|
1671
|
+
return t < 0;
|
1672
|
+
*read_pos += dump_remainder (pretty_filename, fd, COPY_TO_EOF);
|
1673
|
+
}
|
1674
|
+
else
|
1675
|
+
{
|
1676
|
+
off_t start_pos = -1;
|
1677
|
+
off_t end_pos;
|
1678
|
+
|
1679
|
+
/* Use file_lines only if FD refers to a regular file for
|
1680
|
+
which lseek (... SEEK_END) works. */
|
1681
|
+
if ( ! presume_input_pipe
|
1682
|
+
&& S_ISREG (stats.st_mode)
|
1683
|
+
&& (start_pos = lseek (fd, 0, SEEK_CUR)) != -1
|
1684
|
+
&& start_pos < (end_pos = lseek (fd, 0, SEEK_END)))
|
1685
|
+
{
|
1686
|
+
*read_pos = end_pos;
|
1687
|
+
if (end_pos != 0
|
1688
|
+
&& ! file_lines (pretty_filename, fd, n_lines,
|
1689
|
+
start_pos, end_pos, read_pos))
|
1690
|
+
return false;
|
1691
|
+
}
|
1692
|
+
else
|
1693
|
+
{
|
1694
|
+
/* Under very unlikely circumstances, it is possible to reach
|
1695
|
+
this point after positioning the file pointer to end of file
|
1696
|
+
via the `lseek (...SEEK_END)' above. In that case, reposition
|
1697
|
+
the file pointer back to start_pos before calling pipe_lines. */
|
1698
|
+
if (start_pos != -1)
|
1699
|
+
xlseek (fd, start_pos, SEEK_SET, pretty_filename);
|
1700
|
+
|
1701
|
+
return pipe_lines (pretty_filename, fd, n_lines, read_pos);
|
1702
|
+
}
|
1703
|
+
}
|
1704
|
+
return true;
|
1705
|
+
}
|
1706
|
+
|
1707
|
+
/* Display the last N_UNITS units of file FILENAME, open for reading
|
1708
|
+
via FD. Set *READ_POS to the position of the input stream pointer.
|
1709
|
+
*READ_POS is usually the number of bytes read and corresponds to an
|
1710
|
+
offset from the beginning of a file. However, it may be larger than
|
1711
|
+
OFF_T_MAX (as for an input pipe), and may also be larger than the
|
1712
|
+
number of bytes read (when an input pointer is initially not at
|
1713
|
+
beginning of file), and may be far greater than the number of bytes
|
1714
|
+
actually read for an input file that is seekable.
|
1715
|
+
Return true if successful. */
|
1716
|
+
|
1717
|
+
static bool
|
1718
|
+
tail (const char *filename, int fd, uintmax_t n_units,
|
1719
|
+
uintmax_t *read_pos)
|
1720
|
+
{
|
1721
|
+
*read_pos = 0;
|
1722
|
+
if (count_lines)
|
1723
|
+
return tail_lines (filename, fd, n_units, read_pos);
|
1724
|
+
else
|
1725
|
+
return tail_bytes (filename, fd, n_units, read_pos);
|
1726
|
+
}
|
1727
|
+
|
1728
|
+
/* Display the last N_UNITS units of the file described by F.
|
1729
|
+
Return true if successful. */
|
1730
|
+
|
1731
|
+
static bool
|
1732
|
+
tail_file (struct File_spec *f, uintmax_t n_units)
|
1733
|
+
{
|
1734
|
+
int fd;
|
1735
|
+
bool ok;
|
1736
|
+
|
1737
|
+
bool is_stdin = (STREQ (f->name, "-"));
|
1738
|
+
|
1739
|
+
if (is_stdin)
|
1740
|
+
{
|
1741
|
+
have_read_stdin = true;
|
1742
|
+
fd = STDIN_FILENO;
|
1743
|
+
if (O_BINARY && ! isatty (STDIN_FILENO))
|
1744
|
+
xfreopen (NULL, "rb", stdin);
|
1745
|
+
}
|
1746
|
+
else
|
1747
|
+
fd = open (f->name, O_RDONLY | O_BINARY);
|
1748
|
+
|
1749
|
+
f->tailable = !(reopen_inaccessible_files && fd == -1);
|
1750
|
+
|
1751
|
+
if (fd == -1)
|
1752
|
+
{
|
1753
|
+
if (forever)
|
1754
|
+
{
|
1755
|
+
f->fd = -1;
|
1756
|
+
f->errnum = errno;
|
1757
|
+
f->ignore = false;
|
1758
|
+
f->ino = 0;
|
1759
|
+
f->dev = 0;
|
1760
|
+
}
|
1761
|
+
error (0, errno, _("cannot open %s for reading"),
|
1762
|
+
quote (pretty_name (f)));
|
1763
|
+
ok = false;
|
1764
|
+
}
|
1765
|
+
else
|
1766
|
+
{
|
1767
|
+
uintmax_t read_pos;
|
1768
|
+
|
1769
|
+
if (print_headers)
|
1770
|
+
write_header (pretty_name (f));
|
1771
|
+
ok = tail (pretty_name (f), fd, n_units, &read_pos);
|
1772
|
+
if (forever)
|
1773
|
+
{
|
1774
|
+
struct stat stats;
|
1775
|
+
|
1776
|
+
#if TEST_RACE_BETWEEN_FINAL_READ_AND_INITIAL_FSTAT
|
1777
|
+
/* Before the tail function provided `read_pos', there was
|
1778
|
+
a race condition described in the URL below. This sleep
|
1779
|
+
call made the window big enough to exercise the problem. */
|
1780
|
+
xnanosleep (1);
|
1781
|
+
#endif
|
1782
|
+
f->errnum = ok - 1;
|
1783
|
+
if (fstat (fd, &stats) < 0)
|
1784
|
+
{
|
1785
|
+
ok = false;
|
1786
|
+
f->errnum = errno;
|
1787
|
+
error (0, errno, _("error reading %s"), quote (pretty_name (f)));
|
1788
|
+
}
|
1789
|
+
else if (!IS_TAILABLE_FILE_TYPE (stats.st_mode))
|
1790
|
+
{
|
1791
|
+
error (0, 0, _("%s: cannot follow end of this type of file;\
|
1792
|
+
giving up on this name"),
|
1793
|
+
pretty_name (f));
|
1794
|
+
ok = false;
|
1795
|
+
f->errnum = -1;
|
1796
|
+
f->ignore = true;
|
1797
|
+
}
|
1798
|
+
|
1799
|
+
if (!ok)
|
1800
|
+
{
|
1801
|
+
close_fd (fd, pretty_name (f));
|
1802
|
+
f->fd = -1;
|
1803
|
+
}
|
1804
|
+
else
|
1805
|
+
{
|
1806
|
+
/* Note: we must use read_pos here, not stats.st_size,
|
1807
|
+
to avoid a race condition described by Ken Raeburn:
|
1808
|
+
http://mail.gnu.org/archive/html/bug-textutils/2003-05/msg00007.html */
|
1809
|
+
record_open_fd (f, fd, read_pos, &stats, (is_stdin ? -1 : 1));
|
1810
|
+
f->remote = fremote (fd, pretty_name (f));
|
1811
|
+
}
|
1812
|
+
}
|
1813
|
+
else
|
1814
|
+
{
|
1815
|
+
if (!is_stdin && close (fd))
|
1816
|
+
{
|
1817
|
+
error (0, errno, _("error reading %s"), quote (pretty_name (f)));
|
1818
|
+
ok = false;
|
1819
|
+
}
|
1820
|
+
}
|
1821
|
+
}
|
1822
|
+
|
1823
|
+
return ok;
|
1824
|
+
}
|
1825
|
+
|
1826
|
+
/* If obsolete usage is allowed, and the command line arguments are of
|
1827
|
+
the obsolete form and the option string is well-formed, set
|
1828
|
+
*N_UNITS, the globals COUNT_LINES, FOREVER, and FROM_START, and
|
1829
|
+
return true. If the command line arguments are obviously incorrect
|
1830
|
+
(e.g., because obsolete usage is not allowed and the arguments are
|
1831
|
+
incorrect for non-obsolete usage), report an error and exit.
|
1832
|
+
Otherwise, return false and don't modify any parameter or global
|
1833
|
+
variable. */
|
1834
|
+
|
1835
|
+
static bool
|
1836
|
+
parse_obsolete_option (int argc, char * const *argv, uintmax_t *n_units)
|
1837
|
+
{
|
1838
|
+
const char *p;
|
1839
|
+
const char *n_string;
|
1840
|
+
const char *n_string_end;
|
1841
|
+
bool obsolete_usage;
|
1842
|
+
int default_count = DEFAULT_N_LINES;
|
1843
|
+
bool t_from_start;
|
1844
|
+
bool t_count_lines = true;
|
1845
|
+
bool t_forever = false;
|
1846
|
+
|
1847
|
+
/* With the obsolete form, there is one option string and at most
|
1848
|
+
one file argument. Watch out for "-" and "--", though. */
|
1849
|
+
if (! (argc == 2
|
1850
|
+
|| (argc == 3 && ! (argv[2][0] == '-' && argv[2][1]))
|
1851
|
+
|| (3 <= argc && argc <= 4 && STREQ (argv[2], "--"))))
|
1852
|
+
return false;
|
1853
|
+
|
1854
|
+
obsolete_usage = (posix2_version () < 200112);
|
1855
|
+
p = argv[1];
|
1856
|
+
|
1857
|
+
switch (*p++)
|
1858
|
+
{
|
1859
|
+
default:
|
1860
|
+
return false;
|
1861
|
+
|
1862
|
+
case '+':
|
1863
|
+
/* Leading "+" is a file name in the non-obsolete form. */
|
1864
|
+
if (!obsolete_usage)
|
1865
|
+
return false;
|
1866
|
+
|
1867
|
+
t_from_start = true;
|
1868
|
+
break;
|
1869
|
+
|
1870
|
+
case '-':
|
1871
|
+
/* In the non-obsolete form, "-" is standard input and "-c"
|
1872
|
+
requires an option-argument. The obsolete multidigit options
|
1873
|
+
are supported as a GNU extension even when conforming to
|
1874
|
+
POSIX 1003.1-2001, so don't complain about them. */
|
1875
|
+
if (!obsolete_usage && !p[p[0] == 'c'])
|
1876
|
+
return false;
|
1877
|
+
|
1878
|
+
t_from_start = false;
|
1879
|
+
break;
|
1880
|
+
}
|
1881
|
+
|
1882
|
+
n_string = p;
|
1883
|
+
while (ISDIGIT (*p))
|
1884
|
+
p++;
|
1885
|
+
n_string_end = p;
|
1886
|
+
|
1887
|
+
switch (*p)
|
1888
|
+
{
|
1889
|
+
case 'b': default_count *= 512; /* Fall through. */
|
1890
|
+
case 'c': t_count_lines = false; /* Fall through. */
|
1891
|
+
case 'l': p++; break;
|
1892
|
+
}
|
1893
|
+
|
1894
|
+
if (*p == 'f')
|
1895
|
+
{
|
1896
|
+
t_forever = true;
|
1897
|
+
++p;
|
1898
|
+
}
|
1899
|
+
|
1900
|
+
if (*p)
|
1901
|
+
return false;
|
1902
|
+
|
1903
|
+
if (n_string == n_string_end)
|
1904
|
+
*n_units = default_count;
|
1905
|
+
else if ((xstrtoumax (n_string, NULL, 10, n_units, "b")
|
1906
|
+
& ~LONGINT_INVALID_SUFFIX_CHAR)
|
1907
|
+
!= LONGINT_OK)
|
1908
|
+
error (EXIT_FAILURE, 0, _("number in %s is too large"), quote (argv[1]));
|
1909
|
+
|
1910
|
+
/* Set globals. */
|
1911
|
+
from_start = t_from_start;
|
1912
|
+
count_lines = t_count_lines;
|
1913
|
+
forever = t_forever;
|
1914
|
+
|
1915
|
+
return true;
|
1916
|
+
}
|
1917
|
+
|
1918
|
+
static void
|
1919
|
+
parse_options (int argc, char **argv,
|
1920
|
+
uintmax_t *n_units, enum header_mode *header_mode,
|
1921
|
+
double *sleep_interval)
|
1922
|
+
{
|
1923
|
+
int c;
|
1924
|
+
|
1925
|
+
while ((c = getopt_long (argc, argv, "c:n:fFqs:v0123456789",
|
1926
|
+
long_options, NULL))
|
1927
|
+
!= -1)
|
1928
|
+
{
|
1929
|
+
switch (c)
|
1930
|
+
{
|
1931
|
+
case 'F':
|
1932
|
+
forever = true;
|
1933
|
+
follow_mode = Follow_name;
|
1934
|
+
reopen_inaccessible_files = true;
|
1935
|
+
break;
|
1936
|
+
|
1937
|
+
case 'c':
|
1938
|
+
case 'n':
|
1939
|
+
count_lines = (c == 'n');
|
1940
|
+
if (*optarg == '+')
|
1941
|
+
from_start = true;
|
1942
|
+
else if (*optarg == '-')
|
1943
|
+
++optarg;
|
1944
|
+
|
1945
|
+
{
|
1946
|
+
strtol_error s_err;
|
1947
|
+
s_err = xstrtoumax (optarg, NULL, 10, n_units, "bkKmMGTPEZY0");
|
1948
|
+
if (s_err != LONGINT_OK)
|
1949
|
+
{
|
1950
|
+
error (EXIT_FAILURE, 0, "%s: %s", optarg,
|
1951
|
+
(c == 'n'
|
1952
|
+
? _("invalid number of lines")
|
1953
|
+
: _("invalid number of bytes")));
|
1954
|
+
}
|
1955
|
+
}
|
1956
|
+
break;
|
1957
|
+
|
1958
|
+
case 'f':
|
1959
|
+
case LONG_FOLLOW_OPTION:
|
1960
|
+
forever = true;
|
1961
|
+
if (optarg == NULL)
|
1962
|
+
follow_mode = DEFAULT_FOLLOW_MODE;
|
1963
|
+
else
|
1964
|
+
follow_mode = XARGMATCH ("--follow", optarg,
|
1965
|
+
follow_mode_string, follow_mode_map);
|
1966
|
+
break;
|
1967
|
+
|
1968
|
+
case RETRY_OPTION:
|
1969
|
+
reopen_inaccessible_files = true;
|
1970
|
+
break;
|
1971
|
+
|
1972
|
+
case MAX_UNCHANGED_STATS_OPTION:
|
1973
|
+
/* --max-unchanged-stats=N */
|
1974
|
+
if (xstrtoumax (optarg, NULL, 10,
|
1975
|
+
&max_n_unchanged_stats_between_opens,
|
1976
|
+
"")
|
1977
|
+
!= LONGINT_OK)
|
1978
|
+
{
|
1979
|
+
error (EXIT_FAILURE, 0,
|
1980
|
+
_("%s: invalid maximum number of unchanged stats between opens"),
|
1981
|
+
optarg);
|
1982
|
+
}
|
1983
|
+
break;
|
1984
|
+
|
1985
|
+
case DISABLE_INOTIFY_OPTION:
|
1986
|
+
disable_inotify = true;
|
1987
|
+
break;
|
1988
|
+
|
1989
|
+
case PID_OPTION:
|
1990
|
+
{
|
1991
|
+
strtol_error s_err;
|
1992
|
+
unsigned long int tmp_ulong;
|
1993
|
+
s_err = xstrtoul (optarg, NULL, 10, &tmp_ulong, "");
|
1994
|
+
if (s_err != LONGINT_OK || tmp_ulong > PID_T_MAX)
|
1995
|
+
{
|
1996
|
+
error (EXIT_FAILURE, 0, _("%s: invalid PID"), optarg);
|
1997
|
+
}
|
1998
|
+
pid = tmp_ulong;
|
1999
|
+
}
|
2000
|
+
break;
|
2001
|
+
|
2002
|
+
case PRESUME_INPUT_PIPE_OPTION:
|
2003
|
+
presume_input_pipe = true;
|
2004
|
+
break;
|
2005
|
+
|
2006
|
+
case 'q':
|
2007
|
+
*header_mode = never;
|
2008
|
+
break;
|
2009
|
+
|
2010
|
+
case 's':
|
2011
|
+
{
|
2012
|
+
double s;
|
2013
|
+
if (! (xstrtod (optarg, NULL, &s, c_strtod) && 0 <= s))
|
2014
|
+
error (EXIT_FAILURE, 0,
|
2015
|
+
_("%s: invalid number of seconds"), optarg);
|
2016
|
+
*sleep_interval = s;
|
2017
|
+
}
|
2018
|
+
break;
|
2019
|
+
|
2020
|
+
case 'v':
|
2021
|
+
*header_mode = always;
|
2022
|
+
break;
|
2023
|
+
|
2024
|
+
case_GETOPT_HELP_CHAR;
|
2025
|
+
|
2026
|
+
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
|
2027
|
+
|
2028
|
+
case '0': case '1': case '2': case '3': case '4':
|
2029
|
+
case '5': case '6': case '7': case '8': case '9':
|
2030
|
+
error (EXIT_FAILURE, 0,
|
2031
|
+
_("option used in invalid context -- %c"), c);
|
2032
|
+
|
2033
|
+
default:
|
2034
|
+
usage (EXIT_FAILURE);
|
2035
|
+
}
|
2036
|
+
}
|
2037
|
+
|
2038
|
+
if (reopen_inaccessible_files && follow_mode != Follow_name)
|
2039
|
+
error (0, 0, _("warning: --retry is useful mainly when following by name"));
|
2040
|
+
|
2041
|
+
if (pid && !forever)
|
2042
|
+
error (0, 0,
|
2043
|
+
_("warning: PID ignored; --pid=PID is useful only when following"));
|
2044
|
+
else if (pid && kill (pid, 0) != 0 && errno == ENOSYS)
|
2045
|
+
{
|
2046
|
+
error (0, 0, _("warning: --pid=PID is not supported on this system"));
|
2047
|
+
pid = 0;
|
2048
|
+
}
|
2049
|
+
}
|
2050
|
+
|
2051
|
+
/* Mark as '.ignore'd each member of F that corresponds to a
|
2052
|
+
pipe or fifo, and return the number of non-ignored members. */
|
2053
|
+
static size_t
|
2054
|
+
ignore_fifo_and_pipe (struct File_spec *f, size_t n_files)
|
2055
|
+
{
|
2056
|
+
/* When there is no FILE operand and stdin is a pipe or FIFO
|
2057
|
+
POSIX requires that tail ignore the -f option.
|
2058
|
+
Since we allow multiple FILE operands, we extend that to say: with -f,
|
2059
|
+
ignore any "-" operand that corresponds to a pipe or FIFO. */
|
2060
|
+
size_t n_viable = 0;
|
2061
|
+
|
2062
|
+
size_t i;
|
2063
|
+
for (i = 0; i < n_files; i++)
|
2064
|
+
{
|
2065
|
+
bool is_a_fifo_or_pipe =
|
2066
|
+
(STREQ (f[i].name, "-")
|
2067
|
+
&& !f[i].ignore
|
2068
|
+
&& 0 <= f[i].fd
|
2069
|
+
&& (S_ISFIFO (f[i].mode)
|
2070
|
+
|| (HAVE_FIFO_PIPES != 1 && isapipe (f[i].fd))));
|
2071
|
+
if (is_a_fifo_or_pipe)
|
2072
|
+
f[i].ignore = true;
|
2073
|
+
else
|
2074
|
+
++n_viable;
|
2075
|
+
}
|
2076
|
+
|
2077
|
+
return n_viable;
|
2078
|
+
}
|
2079
|
+
|
2080
|
+
int
|
2081
|
+
main (int argc, char **argv)
|
2082
|
+
{
|
2083
|
+
enum header_mode header_mode = multiple_files;
|
2084
|
+
bool ok = true;
|
2085
|
+
/* If from_start, the number of items to skip before printing; otherwise,
|
2086
|
+
the number of items at the end of the file to print. Although the type
|
2087
|
+
is signed, the value is never negative. */
|
2088
|
+
uintmax_t n_units = DEFAULT_N_LINES;
|
2089
|
+
size_t n_files;
|
2090
|
+
char **file;
|
2091
|
+
struct File_spec *F;
|
2092
|
+
size_t i;
|
2093
|
+
bool obsolete_option;
|
2094
|
+
|
2095
|
+
/* The number of seconds to sleep between iterations.
|
2096
|
+
During one iteration, every file name or descriptor is checked to
|
2097
|
+
see if it has changed. */
|
2098
|
+
double sleep_interval = 1.0;
|
2099
|
+
|
2100
|
+
initialize_main (&argc, &argv);
|
2101
|
+
set_program_name (argv[0]);
|
2102
|
+
setlocale (LC_ALL, "");
|
2103
|
+
bindtextdomain (PACKAGE, LOCALEDIR);
|
2104
|
+
textdomain (PACKAGE);
|
2105
|
+
|
2106
|
+
atexit (close_stdout);
|
2107
|
+
|
2108
|
+
have_read_stdin = false;
|
2109
|
+
|
2110
|
+
count_lines = true;
|
2111
|
+
forever = from_start = print_headers = false;
|
2112
|
+
obsolete_option = parse_obsolete_option (argc, argv, &n_units);
|
2113
|
+
argc -= obsolete_option;
|
2114
|
+
argv += obsolete_option;
|
2115
|
+
parse_options (argc, argv, &n_units, &header_mode, &sleep_interval);
|
2116
|
+
|
2117
|
+
/* To start printing with item N_UNITS from the start of the file, skip
|
2118
|
+
N_UNITS - 1 items. `tail -n +0' is actually meaningless, but for Unix
|
2119
|
+
compatibility it's treated the same as `tail -n +1'. */
|
2120
|
+
if (from_start)
|
2121
|
+
{
|
2122
|
+
if (n_units)
|
2123
|
+
--n_units;
|
2124
|
+
}
|
2125
|
+
|
2126
|
+
if (optind < argc)
|
2127
|
+
{
|
2128
|
+
n_files = argc - optind;
|
2129
|
+
file = argv + optind;
|
2130
|
+
}
|
2131
|
+
else
|
2132
|
+
{
|
2133
|
+
static char *dummy_stdin = (char *) "-";
|
2134
|
+
n_files = 1;
|
2135
|
+
file = &dummy_stdin;
|
2136
|
+
}
|
2137
|
+
|
2138
|
+
{
|
2139
|
+
bool found_hyphen = false;
|
2140
|
+
|
2141
|
+
for (i = 0; i < n_files; i++)
|
2142
|
+
if (STREQ (file[i], "-"))
|
2143
|
+
found_hyphen = true;
|
2144
|
+
|
2145
|
+
/* When following by name, there must be a name. */
|
2146
|
+
if (found_hyphen && follow_mode == Follow_name)
|
2147
|
+
error (EXIT_FAILURE, 0, _("cannot follow %s by name"), quote ("-"));
|
2148
|
+
|
2149
|
+
/* When following forever, warn if any file is `-'.
|
2150
|
+
This is only a warning, since tail's output (before a failing seek,
|
2151
|
+
and that from any non-stdin files) might still be useful. */
|
2152
|
+
if (forever && found_hyphen && isatty (STDIN_FILENO))
|
2153
|
+
error (0, 0, _("warning: following standard input"
|
2154
|
+
" indefinitely is ineffective"));
|
2155
|
+
}
|
2156
|
+
|
2157
|
+
F = xnmalloc (n_files, sizeof *F);
|
2158
|
+
for (i = 0; i < n_files; i++)
|
2159
|
+
F[i].name = file[i];
|
2160
|
+
|
2161
|
+
if (header_mode == always
|
2162
|
+
|| (header_mode == multiple_files && n_files > 1))
|
2163
|
+
print_headers = true;
|
2164
|
+
|
2165
|
+
if (O_BINARY && ! isatty (STDOUT_FILENO))
|
2166
|
+
xfreopen (NULL, "wb", stdout);
|
2167
|
+
|
2168
|
+
for (i = 0; i < n_files; i++)
|
2169
|
+
ok &= tail_file (&F[i], n_units);
|
2170
|
+
|
2171
|
+
if (forever && ignore_fifo_and_pipe (F, n_files))
|
2172
|
+
{
|
2173
|
+
#if HAVE_INOTIFY
|
2174
|
+
/* tailable_stdin() checks if the user specifies stdin via "-",
|
2175
|
+
or implicitly by providing no arguments. If so, we won't use inotify.
|
2176
|
+
Technically, on systems with a working /dev/stdin, we *could*,
|
2177
|
+
but would it be worth it? Verifying that it's a real device
|
2178
|
+
and hooked up to stdin is not trivial, while reverting to
|
2179
|
+
non-inotify-based tail_forever is easy and portable.
|
2180
|
+
|
2181
|
+
any_remote_file() checks if the user has specified any
|
2182
|
+
files that reside on remote file systems. inotify is not used
|
2183
|
+
in this case because it would miss any updates to the file
|
2184
|
+
that were not initiated from the local system.
|
2185
|
+
|
2186
|
+
FIXME: inotify doesn't give any notification when a new
|
2187
|
+
(remote) file or directory is mounted on top a watched file.
|
2188
|
+
When follow_mode == Follow_name we would ideally like to detect that.
|
2189
|
+
Note if there is a change to the original file then we'll
|
2190
|
+
recheck it and follow the new file, or ignore it if the
|
2191
|
+
file has changed to being remote.
|
2192
|
+
|
2193
|
+
FIXME: when using inotify, and a directory for a watched file
|
2194
|
+
is recreated, then we don't recheck any new file when
|
2195
|
+
follow_mode == Follow_name */
|
2196
|
+
if (!disable_inotify && (tailable_stdin (F, n_files)
|
2197
|
+
|| any_remote_file (F, n_files)))
|
2198
|
+
disable_inotify = true;
|
2199
|
+
|
2200
|
+
if (!disable_inotify)
|
2201
|
+
{
|
2202
|
+
int wd = inotify_init ();
|
2203
|
+
if (0 <= wd)
|
2204
|
+
{
|
2205
|
+
/* Flush any output from tail_file, now, since
|
2206
|
+
tail_forever_inotify flushes only after writing,
|
2207
|
+
not before reading. */
|
2208
|
+
if (fflush (stdout) != 0)
|
2209
|
+
error (EXIT_FAILURE, errno, _("write error"));
|
2210
|
+
|
2211
|
+
if (!tail_forever_inotify (wd, F, n_files, sleep_interval))
|
2212
|
+
exit (EXIT_FAILURE);
|
2213
|
+
}
|
2214
|
+
error (0, errno, _("inotify cannot be used, reverting to polling"));
|
2215
|
+
}
|
2216
|
+
#endif
|
2217
|
+
disable_inotify = true;
|
2218
|
+
tail_forever (F, n_files, sleep_interval);
|
2219
|
+
}
|
2220
|
+
|
2221
|
+
if (have_read_stdin && close (STDIN_FILENO) < 0)
|
2222
|
+
error (EXIT_FAILURE, errno, "-");
|
2223
|
+
exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
|
2224
|
+
}
|