-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclogtail.c
179 lines (127 loc) · 4.81 KB
/
clogtail.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <glob.h>
#define OFFSETEXT ".offset"
#define FASSERT(x, y) if ((x) == -1) { perror((y)); exit(1); }
typedef struct {
off_t offset;
ino_t inode;
} offset_t;
long page_size, page_offset, page_align;
off_t logtail(int fd, off_t read_from, off_t read_to) {
if (read_to > 0) {
char *buf;
size_t buf_offset = read_from & page_offset;
size_t buf_size = (read_to - read_from + buf_offset + page_offset) & page_align;
buf = mmap(NULL, buf_size, PROT_READ, MAP_SHARED, fd, read_from & page_align);
if (buf == MAP_FAILED) {
perror("cannot mmap file");
return 1;
}
char *line_end, *line_start = buf + buf_offset;
while (
(read_from < read_to) &&
(line_end = memchr(line_start, '\n', read_to - read_from))
) {
size_t line_len = line_end - line_start + 1;
read_from += line_len;
write(STDOUT_FILENO, line_start, line_len);
line_start = line_end + 1;
}
int res = munmap(buf, buf_size);
FASSERT(res, "unmap file")
return read_from;
} else
return 0;
}
void usage() {
printf(
"usage:\n"
"\tclogtail [options]\n\n"
"\t\t-f <file> path to file (required)\n"
"\t\t-o <suffix> offset file suffix (optional)\n"
"\t\t-g <glob> glob to use for searching for rotated file (optional)\n\n"
);
exit(0);
}
int main (int argc, char *argv[]) {
offset_t offset_data;
int res;
page_size = sysconf(_SC_PAGESIZE);
page_offset = page_size - 1;
page_align = ~ page_offset;
char *input_fn = NULL;
char *offset_sfx = NULL;
char *glob_str = NULL;
int opt;
while ((opt = getopt(argc, argv, "f:o:g:")) != -1) {
switch (opt) {
case 'f': input_fn = optarg; break;
case 'o': offset_sfx = optarg; break;
case 'g': glob_str = optarg; break;
}
}
if (input_fn == NULL) usage();
int input_fd = open(input_fn, O_RDONLY);
FASSERT(input_fd, "file open")
struct stat input_stat;
res = fstat(input_fd, &input_stat);
FASSERT(res, "file stat")
char offset_fn[strlen(input_fn) + (offset_sfx ? strlen(offset_sfx) : sizeof(OFFSETEXT))];
strcpy(offset_fn, input_fn);
strcat(offset_fn, offset_sfx ? offset_sfx : OFFSETEXT);
res = access(offset_fn, F_OK | R_OK | W_OK );
int offset_fd = open(offset_fn, O_RDWR | O_CREAT, 0664);
FASSERT(offset_fd, "offset file open")
if (res == 0) { // we found the ".offset" file
res = read(offset_fd, &offset_data, sizeof(offset_t));
FASSERT(res, "offset file read")
if (offset_data.offset == input_stat.st_size &&
offset_data.inode == input_stat.st_ino) // no changes
return 0;
if (offset_data.inode != input_stat.st_ino) { // file rotated
if (glob_str) { // we have glob to search for rotated file
int i, found = 0;
glob_t glob_data;
struct stat search_stat;
if (!glob(glob_str, GLOB_NOSORT, NULL, &glob_data))
for (i = 0; i < glob_data.gl_pathc && !found; i++)
if(!stat(glob_data.gl_pathv[i], &search_stat))
found = (search_stat.st_ino == offset_data.inode);
if (found) {
fprintf(stderr, "file rotated, found at %s\n", glob_data.gl_pathv[i - 1]);
int globfd = open(glob_data.gl_pathv[i - 1], O_RDONLY);
FASSERT(globfd, "found file open")
logtail(globfd, offset_data.offset, search_stat.st_size);
close(globfd);
} else
fputs("warning, file rotated and was not found\n", stderr);
globfree(&glob_data);
} else
fputs("warning, file rotated and no glob specified\n", stderr);
offset_data.inode = input_stat.st_ino;
offset_data.offset = 0;
} else { // file was not rotated
if (offset_data.offset > input_stat.st_size ) {
fputs("warning, truncated file\n", stderr);
offset_data.offset = 0;
}
}
offset_data.offset = logtail(input_fd, offset_data.offset, input_stat.st_size);
} else { // no offset file
offset_data.offset = input_stat.st_size;
offset_data.inode = input_stat.st_ino;
}
close(input_fd);
lseek(offset_fd, 0, SEEK_SET);
write(offset_fd, &offset_data, sizeof(offset_t));
close(offset_fd);
return 0;
}