-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathretail.c
476 lines (403 loc) · 12.1 KB
/
retail.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
* retail.c -- ASCII file tail program that remembers last position.
*
* Copyright (c) 1996 Craig H. Rowland <[email protected]>
* Copyright (c) Ross Moffatt <[email protected]>
* Copyright (c) 2014 Mark Bucciarelli <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* Version 2.1 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <sys/stat.h>
#include <dirent.h>
#include <err.h>
#include <libgen.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <zlib.h>
/*
* PATH_MAX seems broken, here's a choice quote from
*
* http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
*
* Since a path can be longer than PATH_MAX, the define is useless,
* writing code based off of it is wrong, and the functions that require
* it are broken.
*
* We set it to 1024 (OpenBSD's value).
*/
#define MY_PATH_MAX 1024
#define BUFSZ 4096
#define USAGE "Usage: retail [-o <offset filename>] <log filename>"
/*
* The regular basename behaves differently on different operating systems.
*/
static char *
mybasename(const char *logfn)
{
static char buf [MY_PATH_MAX] = {0};
if (!logfn || !strlen(logfn))
return buf;
if (strlen(logfn) > MY_PATH_MAX - 1)
errx(EXIT_FAILURE, "can't get basename, filename too long: '%s'", logfn);
strcpy(buf, logfn);
return basename(buf);
}
/*
* Same for dirname
*/
static char *
mydirname(const char *logfn)
{
static char buf [MY_PATH_MAX] = {0};
if (!logfn || !strlen(logfn))
return buf;
if (strlen(logfn) > MY_PATH_MAX - 1)
errx(EXIT_FAILURE, "can't get dirname, filename too long: '%s'", logfn);
strcpy(buf, logfn);
return dirname(buf);
}
static char *
build_offsetfn(const char *logfn, char *offsetfn)
{
static char rval[MY_PATH_MAX] = {0};
size_t sz = 0;
/*
* The offset filename is given.
*/
if (offsetfn && strlen(offsetfn) && offsetfn[strlen(offsetfn) - 1] != '/') {
if (strlen(offsetfn) > MY_PATH_MAX - 1)
errx(EXIT_FAILURE, "offset filename is too long");
strcpy(rval, offsetfn);
}
/*
* The offset filename is given
* and is a directory.
*/
else if (offsetfn && strlen(offsetfn) && offsetfn[strlen(offsetfn) - 1] == '/') {
if (!logfn || strlen(logfn) == 0)
errx(EXIT_FAILURE, "log filename is empty");
sz = strlen(offsetfn) + strlen("offset.") + strlen(mybasename(logfn));
if (sz > MY_PATH_MAX - 1)
errx(EXIT_FAILURE, "offset filename is too long");
strcpy(rval, offsetfn);
strcat(rval, "offset.");
strcat(rval, mybasename(logfn));
}
/*
* No offset filename specified.
*/
else if (!offsetfn || !strlen(offsetfn)) {
sz = strlen(mydirname(logfn)) + strlen("/offset.") + strlen(mybasename(logfn));
if (sz > MY_PATH_MAX - 1)
errx(EXIT_FAILURE, "offset filename is too long");
strcpy(rval, mydirname(logfn));
strcat(rval, "/offset.");
strcat(rval, mybasename(logfn));
}
else
errx(EXIT_FAILURE, "logic error in build_offsetfn()");
return rval;
}
struct conditional_data {
ino_t loginode;
ino_t otherinode;
long long mostrecent_mtime;
long long other_mtime;
const char *logfn;
const char *otherfn;
};
/*
* A conditional is a function that defines what the last log is.
*/
typedef int (*conditional) (const struct conditional_data *);
static int
sameinode(const struct conditional_data *p)
{
return p->loginode == p->otherinode
&& strncmp(p->otherfn, p->logfn, strlen(p->logfn)) == 0;
}
static int
mostrecent(const struct conditional_data *p)
{
return strncmp(p->otherfn, p->logfn, strlen(p->logfn)) == 0
&& p->other_mtime > p->mostrecent_mtime;
}
static int
mostrecentgz(const struct conditional_data *p)
{
return strncmp(p->otherfn, p->logfn, strlen(p->logfn)) == 0
&& p->other_mtime > p->mostrecent_mtime
&& strlen(p->otherfn) > 2
&& !strcmp(p->otherfn + strlen(p->otherfn) - 3, ".gz");
}
/*
* Find the rotated log.
*/
static char *
find_lastlog(const char *logfn, ino_t logino, conditional update_lastlog)
{
static char rval[MY_PATH_MAX] = {0};
char fn [MY_PATH_MAX] = {0};
struct conditional_data state;
struct dirent *ep = 0;
struct stat fstat;
DIR *dp;
char *dir = 0;
char *base = 0;
size_t sz = 0;
dir = mydirname(logfn);
base = mybasename(logfn);
if (NULL == (dp = opendir(dir)))
err(EXIT_FAILURE, "can't open directory '%s'", dir, NULL);
memset(&state, 0, sizeof(state));
state.logfn = base;
state.mostrecent_mtime = 0;
state.loginode = logino;
while ((ep = readdir(dp))) {
if (!strcmp(ep->d_name, ".")
|| !strcmp(ep->d_name, "..")
|| strlen(ep->d_name) <= strlen(base))
continue;
sz = strlen(dir) + strlen(ep->d_name) + 2;
if (sz > sizeof(fn))
errx(EXIT_FAILURE, "filename too big: '%s/%s'", dir, ep->d_name);
strcpy(fn, dir);
strcat(fn, "/");
strcat(fn, ep->d_name);
if ((stat(fn, &fstat)) != 0)
err(EXIT_FAILURE, "can't stat '%s'", fn);
state.otherfn = ep->d_name;
state.other_mtime = fstat.st_mtime;
state.otherinode = fstat.st_ino;
/*
If this is a "more recent" log file,
update current pointer to the last
log. I put "more recent" in quotes
because the specific conditional
depends on what kind of rotation
was used.
*/
if ((*update_lastlog) (&state)) {
state.mostrecent_mtime = fstat.st_mtime;
strcpy(rval, fn);
}
}
if (-1 == closedir(dp))
err(EXIT_FAILURE, "can't close directory '%s'", dir);
return rval;
}
__attribute__((format(printf, 2, 3)))
__attribute__((noreturn))
static void
gzdie (gzFile * fp, const char *fmt,...)
{
va_list args;
const char *emsg = 0;
int emsgno;
emsg = gzerror(fp, &emsgno);
va_start(args, fmt);
if (emsgno == Z_ERRNO)
verr(EXIT_FAILURE, fmt, args);
else
verrx(EXIT_FAILURE, fmt, args);
va_end(args);
}
/*
* Given a filename and an offset, print all bytes after offset to stdout.
* Works for gzipped and ungzipped files.
*/
static z_off_t
dump_changes(const char *fn, const z_off_t pos)
{
char buf [BUFSZ] = {0};
z_off_t rval = 0;
gzFile *fp = 0;
int charsread = 0;
int rc = 0;
syslog(LOG_DEBUG, "dump_changes('%s', %lu)", fn, pos);
if (NULL == (fp = gzopen(fn, "rb")))
err(EXIT_FAILURE, "can't dump changes in '%s'", fn);
if (-1 == gzseek(fp, pos, SEEK_SET))
gzdie(fp, "can't gzseek to %ld in %s", pos, fn);
do {
buf[0] = 0;
charsread = gzread(fp, buf, BUFSZ);
if (charsread < 0)
gzdie(fp, "can't gzread from %s", fn);
rval += (unsigned int)charsread;
if ((unsigned int)charsread != fwrite(buf, 1, (unsigned int)charsread, stdout))
err(EXIT_FAILURE, "error writing changes to stdout");
} while (charsread == BUFSZ);
rc = gzclose(fp);
/*
* On Ubuntu Lucid, the zlib.h in zlib1g-dev returns Z_BUF_ERROR
* when gzclose'ing an empty file.
*
* This behavior does not manifest on OSX.
*/
if (!(rc == 0 || (rc == Z_BUF_ERROR && rval == 0)))
err(EXIT_FAILURE, "failed to close '%s'", fn);
syslog(LOG_DEBUG, "dump_changes returns %lu (bytes read)", rval);
return rval;
}
/*
* Output any new lines added to log since last run,
* and update offset.
*/
static int
check_log(const char *logfn, const char *offsetfn)
{
FILE *logfp,
*offsetfp;
struct stat logfstat;
char *lastlog = 0;
z_off_t lastoffset;
ino_t lastinode = 0;
z_off_t lastsize = 0;
/*
* Check if the file exists in specified directory. Open as
* a binary in case the user reads in non-text files.
*/
if ((logfp = fopen(logfn, "rb")) == NULL)
err(EXIT_FAILURE, "can't check log '%s'", logfn);
if ((stat(logfn, &logfstat)) != 0)
err(EXIT_FAILURE, "can't stat '%s'", logfn);
syslog(LOG_DEBUG, "check_log: st_size of '%s' = %lld", logfn, (long long int)logfstat.st_size);
/*
* If we are on a 32-bit system,
* exit if the file is too big.
* XXX: Can this really happen? -- Mark
*/
if ((sizeof(fpos_t) == 4) || sizeof(logfstat.st_size) == 4) {
if ((logfstat.st_size > 2147483646) || (logfstat.st_size < 0))
errx(EXIT_FAILURE, "log file, %s, is too large at %lld bytes.\n",
logfn, (long long)logfstat.st_size);
}
/*
* Load offset data.
*/
if ((offsetfp = fopen(offsetfn, "rb")) != NULL) {
if (1 != fread(&lastinode, sizeof(lastinode), 1, offsetfp))
errx(EXIT_FAILURE, "error reading last inode from '%s'", offsetfn);
if (1 != fread(&lastoffset, sizeof(lastoffset), 1, offsetfp))
errx(EXIT_FAILURE, "error reading last offset from '%s'", offsetfn);
if (1 != fread(&lastsize, sizeof(lastsize), 1, offsetfp))
errx(EXIT_FAILURE, "error reading last size from '%s'", offsetfn);
if (0 != fclose(offsetfp))
err(EXIT_FAILURE, "can't close '%s'", offsetfn);
if (lastoffset > lastsize)
errx(EXIT_FAILURE, "last offset (%ld) greater than last size (%ld) in '%s'",
lastoffset, lastsize, offsetfn);
}
else {
lastoffset = 0;
lastinode = logfstat.st_ino;
}
/*
* If the current file lastinode is the same,
* but the file lastsize has
* grown SMALLER than the last time we checked,
* then assume the log file has been rolled
* via a copy and delete.
*/
if ((lastinode == logfstat.st_ino) && (lastsize > logfstat.st_size)) {
lastlog = find_lastlog(logfn, lastinode, &mostrecent);
/*
* If we can't find the old log file,
* reset the last offset to zero
* so we dump the entire current log.
*/
if (!lastlog || !strlen(lastlog))
lastoffset = 0;
}
/*
* If the lastinode of the current log file
* is different than the one store in the offset file,
* then assume the log file was rotated
* by a mv and then recreated.
*/
else if (lastinode != logfstat.st_ino) {
lastlog = find_lastlog(logfn, lastinode, &sameinode);
if (!lastlog || !strlen(lastlog))
lastlog = find_lastlog(logfn, lastinode, &mostrecentgz);
if (!lastlog || !strlen(lastlog))
lastoffset = 0;
}
else
lastlog = 0;
if (lastlog && strlen(lastlog)) {
dump_changes(lastlog, lastoffset);
lastoffset = 0;
}
lastoffset += dump_changes(logfn, lastoffset);
if (lastoffset > logfstat.st_size) {
syslog(LOG_DEBUG, "offset > st_size (%lu > %lld), setting size to offset", lastoffset, (long long int)logfstat.st_size);
logfstat.st_size = lastoffset;
}
/*
* Write the new offset data.
*/
if ((offsetfp = fopen(offsetfn, "w")) == NULL)
err(EXIT_FAILURE, "can't write offset to '%s'", offsetfn);
if ((chmod(offsetfn, 00660)) != 0)
err(EXIT_FAILURE, "Cannot set permissions on file %s", offsetfn);
if (1 != fwrite(&logfstat.st_ino, sizeof(logfstat.st_ino), 1, offsetfp))
err(EXIT_FAILURE, "can't write last inode to '%s'", offsetfn);
if (1 != fwrite(&lastoffset, sizeof(lastoffset), 1, offsetfp))
err(EXIT_FAILURE, "can't write last offset to '%s'", offsetfn);
if (1 != fwrite(&logfstat.st_size, sizeof(logfstat.st_size), 1, offsetfp))
err(EXIT_FAILURE, "can't write last size to '%s'", offsetfn);
if (0 != fclose(offsetfp))
err(EXIT_FAILURE, "can't close '%s'", offsetfn);
return (0);
}
int
main(int argc, char *argv[])
{
char logfn [MY_PATH_MAX] = {0};
char *offsetfn = 0;
char *p;
openlog("retail", LOG_PID, LOG_USER);
switch (argc) {
case 2:
p = argv[1];
if (*p == '-')
errx(EXIT_FAILURE, USAGE);
if (strlen(argv[1]) < MY_PATH_MAX - 1)
strcpy(logfn, argv[1]);
else
errx(EXIT_FAILURE, "log file name too long");
offsetfn = build_offsetfn(argv[1], NULL);
break;
case 4:
p = argv[1];
if (*p != '-' || (*p && *(p + 1) != 'o'))
errx(EXIT_FAILURE, USAGE);
if (strlen(argv[3]) < MY_PATH_MAX - 1)
strcpy(logfn, argv[3]);
else
errx(EXIT_FAILURE, "log file name too long");
offsetfn = build_offsetfn(argv[3], argv[2]);
break;
default:
errx(EXIT_FAILURE, USAGE);
}
check_log(logfn, offsetfn);
closelog();
return 0;
}