1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/* tailf.c -- tail a log file and then follow it
* Created: Tue Jan 9 15:49:21 1996 by faith@acm.org
* Copyright 1996, 2003 Rickard E. Faith (faith@acm.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* less -F and tail -f cause a disk access every five seconds. This
* program avoids this problem by waiting for the file size to change.
* Hence, the file is not accessed, and the access time does not need to be
* flushed back to disk. This is sort of a "stealth" tail.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <err.h>
#ifdef HAVE_INOTIFY_INIT
#include <sys/inotify.h>
#endif
#include "nls.h"
#define DEFAULT_LINES 10
static void
tailf(const char *filename, int lines)
{
char *buf, *p;
int head = 0;
int tail = 0;
FILE *str;
int i;
if (!(str = fopen(filename, "r")))
err(EXIT_FAILURE, _("cannot open \"%s\" for read"), filename);
buf = malloc(lines * BUFSIZ);
p = buf;
while (fgets(p, BUFSIZ, str)) {
if (++tail >= lines) {
tail = 0;
head = 1;
}
p = buf + (tail * BUFSIZ);
}
if (head) {
for (i = tail; i < lines; i++)
fputs(buf + (i * BUFSIZ), stdout);
for (i = 0; i < tail; i++)
fputs(buf + (i * BUFSIZ), stdout);
} else {
for (i = head; i < tail; i++)
fputs(buf + (i * BUFSIZ), stdout);
}
fflush(stdout);
free(buf);
fclose(str);
}
static void
roll_file(const char *filename, off_t *size)
{
char buf[BUFSIZ];
int fd;
struct stat st;
if (!(fd = open(filename, O_RDONLY)))
err(EXIT_FAILURE, _("cannot open \"%s\" for read"), filename);
if (fstat(fd, &st) == -1)
err(EXIT_FAILURE, _("cannot stat \"%s\""), filename);
if (st.st_size == *size) {
close(fd);
return;
}
if (lseek(fd, *size, SEEK_SET) != (off_t)-1) {
ssize_t rc, wc;
while ((rc = read(fd, buf, sizeof(buf))) > 0) {
wc = write(STDOUT_FILENO, buf, rc);
if (rc != wc)
warnx(_("incomplete write to \"%s\" (written %ld, expected %ld)\n"),
filename, wc, rc);
}
fflush(stdout);
}
close(fd);
*size = st.st_size;
}
static void
watch_file(const char *filename, off_t *size)
{
do {
roll_file(filename, size);
usleep(250000);
} while(1);
}
#ifdef HAVE_INOTIFY_INIT
#define EVENTS (IN_MODIFY|IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT)
#define NEVENTS 4
static int
watch_file_inotify(const char *filename, off_t *size)
{
char buf[ NEVENTS * sizeof(struct inotify_event) ];
int fd, ffd, e;
ssize_t len;
fd = inotify_init();
if (fd == -1)
return 0;
ffd = inotify_add_watch(fd, filename, EVENTS);
while (ffd >= 0) {
len = read(fd, buf, sizeof(buf));
if (len < 0 && (errno == EINTR || errno == EAGAIN))
continue;
if (len < 0)
err(EXIT_FAILURE, "%s: cannot read inotify events", filename);
for (e = 0; e < len; ) {
struct inotify_event *ev = (struct inotify_event *) &buf[e];
if (ev->mask & IN_MODIFY)
roll_file(filename, size);
else {
close(ffd);
ffd = -1;
break;
}
e += sizeof(struct inotify_event) + ev->len;
}
}
close(fd);
return 1;
}
#endif /* HAVE_INOTIFY_INIT */
int main(int argc, char **argv)
{
const char *filename;
int lines = DEFAULT_LINES;
struct stat st;
off_t size = 0;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
argc--;
argv++;
for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
if (!strcmp(*argv, "-n") || !strcmp(*argv, "--lines")) {
argc--; argv++;
if (argc > 0 && (lines = atoi(argv[0])) <= 0)
errx(EXIT_FAILURE, _("invalid number of lines"));
}
else if (isdigit(argv[0][1])) {
if ((lines = atoi(*argv + 1)) <= 0)
errx(EXIT_FAILURE, _("invalid number of lines"));
}
else
errx(EXIT_FAILURE, _("invalid option"));
}
if (argc != 1)
errx(EXIT_FAILURE, _("usage: tailf [-n N | -N] logfile"));
filename = argv[0];
if (stat(filename, &st) != 0)
err(EXIT_FAILURE, _("cannot stat \"%s\""), filename);
size = st.st_size;;
tailf(filename, lines);
#ifdef HAVE_INOTIFY_INIT
if (!watch_file_inotify(filename, &size))
#endif
watch_file(filename, &size);
return EXIT_SUCCESS;
}
|