-
Notifications
You must be signed in to change notification settings - Fork 1
/
einotail.c
257 lines (234 loc) · 6.98 KB
/
einotail.c
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* It's like tail.
* but with epoll and inotify.
**/
#define _XOPEN_SOURCE 600
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/inotify.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define BUFSIZE 1024
#define FIND_LINE_BUFSIZE 1024
#define MAX_EVENTS 16
#define TRAILING_LINES 10
long path_len;
char* readlink_buf;
struct fdata {
int fd;
const char *filename;
const char *full_filename;
off_t offset;
};
long max(long a, long b)
{
return (a > b) ? a : b;
}
/* Do a readlink and return a pointer to the result.
* Not reentrant.
* Kind of sketchy.
*/
char* readlink_full(const char* path)
{
char buf[path_len + 1];
ssize_t read_b;
struct stat stbuf;
strcpy(buf, path);
do {
lstat(buf, &stbuf);
read_b = readlink(path, buf, path_len);
buf[read_b] = '\0';
} while (S_ISLNK(stbuf.st_mode));
strncpy(readlink_buf, buf, read_b+1);
return readlink_buf;
}
int open_file(struct fdata* fd)
{
if (fd->fd != -1)
close(fd->fd);
if ((fd->fd = open(fd->filename, O_RDONLY)) < 0) {
perror("open");
return -1;
}
return fd->fd;
}
/* Find the offset that is either TRAILING_LINES behind the
* end of the file or is at the beginning of the file
*/
off_t find_lines_offset(struct fdata* fd, struct stat* stbuf)
{
ssize_t read_size = -1;
char buf[FIND_LINE_BUFSIZE];
int num_newlines_found = 0;
off_t offset = stbuf->st_size;
off_t init_offset = offset;
while(num_newlines_found <= TRAILING_LINES && init_offset > 0) {
char* found;
init_offset = max(init_offset - FIND_LINE_BUFSIZE, 0);
offset = init_offset;
lseek(fd->fd, offset, SEEK_SET);
if ((read_size = read(fd->fd, buf, FIND_LINE_BUFSIZE)) < 0) {
perror("find_lines_offset read");
return -1;
}
do {
found = strrchr(buf, '\n');
if (found != NULL) {
*found = '\0';
offset = (found - buf) + init_offset + 1;
num_newlines_found++;
}
} while(num_newlines_found <= TRAILING_LINES && found != NULL);
}
if (num_newlines_found < TRAILING_LINES)
return 0;
return offset;
}
int tail_file(struct fdata* fd)
{
struct stat stbuf;
ssize_t read_s;
char read_buf[BUFSIZE];
fstat(fd->fd, &stbuf);
if (fd->offset == -1) {
fd->offset = find_lines_offset(fd, &stbuf);
}
if (fd->offset > stbuf.st_size) {
fd->offset = stbuf.st_size;
}
lseek(fd->fd, fd->offset, SEEK_SET);
while (fd->offset < stbuf.st_size) {
read_s = read(fd->fd, &read_buf, BUFSIZE);
if (read_s < 0) {
perror("tail_file read");
return -1;
} else if (read_s == 0) {
break;
}
write(STDOUT_FILENO, read_buf, read_s);
fd->offset += read_s;
}
return read_s;
}
int main(int argc, char** argv)
{
int ep, in, in_watch, link_watch;
struct epoll_event ev;
struct epoll_event events[MAX_EVENTS];
struct fdata fd;
struct stat stbuf;
bool running = true;
if (argc != 2) {
fprintf(stderr, "Usage: einotail filename\n"
"Behaves like tail -F filename\n");
return 2;
}
fd.filename = argv[1];
fd.fd = -1;
fd.offset = -1;
path_len = pathconf(fd.filename, _PC_PATH_MAX);
readlink_buf = malloc(path_len * sizeof(char));
if ((ep = epoll_create(2)) == -1) {
perror("epoll_crate");
return EXIT_FAILURE;
}
ev.events = EPOLLIN;
ev.data.fd = STDIN_FILENO;
if (epoll_ctl(ep, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) {
perror("epoll_ctl: stdin");
return EXIT_FAILURE;
}
in = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
ev.events = EPOLLIN;
ev.data.fd = in;
if (epoll_ctl(ep, EPOLL_CTL_ADD, in, &ev) == -1) {
perror("epoll_ctl: inotify");
return EXIT_FAILURE;
}
lstat(fd.filename, &stbuf);
if (S_ISREG(stbuf.st_mode) && access(fd.filename, R_OK|F_OK) != 0) {
fprintf(stderr, "Unable to access %s\n", fd.filename);
return EXIT_FAILURE;
}
if (S_ISREG(stbuf.st_mode) || access(fd.filename, R_OK) == 0) {
open_file(&fd);
tail_file(&fd);
}
if (S_ISLNK(stbuf.st_mode)) {
link_watch = inotify_add_watch(in, fd.filename, IN_ALL_EVENTS|IN_DONT_FOLLOW);
in_watch = inotify_add_watch(in, readlink_full(fd.filename), IN_ALL_EVENTS);
} else {
link_watch = -1;
in_watch = inotify_add_watch(in, fd.filename, IN_ALL_EVENTS);
}
while (running) {
int num_events;
int i;
if ((num_events = epoll_wait(ep, events, MAX_EVENTS, 1000)) < 0) {
perror("epoll_wait");
return EXIT_FAILURE;
}
for (i=0; i < num_events; ++i) {
if (events[i].data.fd == in) {
struct inotify_event d;
int err;
bool remove_in = false;
bool remove_link = false;
bool reread = false;
while(1) {
if ((err = read(in, &d, sizeof(struct inotify_event))) < 1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
break;
}
else {
perror("read from inotify fd");
return EXIT_FAILURE;
}
} else if (err != sizeof(struct inotify_event)) {
fprintf(stderr, "Got %d bytes, expected %lu\n", err, sizeof(struct inotify_event));
}
if (d.wd == link_watch) {
remove_link = true;
if (!(d.mask & IN_DELETE)) {
reread = true;
remove_in = true;
}
} else {
tail_file(&fd);
}
}
if (remove_in) {
inotify_rm_watch(in, in_watch);
in_watch = inotify_add_watch(in, readlink_full(fd.filename), IN_ALL_EVENTS);
}
if (reread) {
open_file(&fd);
fd.offset = 0;
tail_file(&fd);
}
if (remove_link) {
inotify_rm_watch(in, link_watch);
link_watch = inotify_add_watch(in, fd.filename, IN_ALL_EVENTS|IN_DONT_FOLLOW);
}
} else {
char buf[1];
ssize_t dsize = read(STDIN_FILENO, &buf, 1);
if (dsize == 0) {
running = false;
break;
}
}
}
}
close(in);
close(ep);
free(readlink_buf);
return EXIT_SUCCESS;
}