Commit cb096341 authored by Leigh B. Stoller's avatar Leigh B. Stoller

Fix up genlastlog to avoid Y2K like problems. Basically, syslog does

not include a year in the output, so I was using the current year to
init the tm structure. However, the log file could span from the old
year to the new year, and so all the dates could be wrong. Using Mac's
suggestion, look at the final time stamp, and if its in the future,
reset it back one year.

Also add [-a #] option to roll through the specified number of
rotation files, to make the job of reinitting all the records easier.
I ran it as "genlastlog -a 7", which makes it process logins.7.gz
through logins.0.gz, before doing the current log.
parent 7523a98c
......@@ -76,7 +76,7 @@ static int deadfl;
static void
usage(void)
{
fprintf(stderr, "Usage: %s\n", progname);
fprintf(stderr, "Usage: %s [-a]\n", progname);
exit(-1);
}
......@@ -93,11 +93,26 @@ main(int argc, char **argv)
gzFile *infp;
char buf[BUFSIZ], *bp;
struct hostent *he;
int errors = 0;
int ch, errors = 0;
int backcount = 0;
progname = argv[0];
if (argc != 1)
while ((ch = getopt(argc, argv, "a:")) != -1)
switch(ch) {
case 'a':
backcount = atoi(optarg);
break;
case 'h':
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (argc)
usage();
openlog("genlastlog", LOG_PID, LOG_USER);
......@@ -116,24 +131,27 @@ main(int argc, char **argv)
if (bp = strchr(opshostname, '.'))
*bp = 0;
sprintf(buf, "%s/%s.0.gz", USERSVAR, LOGINS);
while (backcount) {
sprintf(buf, "%s/%s.%d.gz", USERSVAR, LOGINS, backcount);
/*
* Use setjmp and timer to prevent NFS lockup.
*/
if (setjmp(deadline) == 0) {
alarm(15);
if ((infp = gzopen(buf, "r")) == NULL) {
syslog(LOG_ERR, "Opening %s: %m", buf);
errors++;
}
else {
doit(infp);
gzclose(infp);
/*
* Use setjmp and timer to prevent NFS lockup.
*/
if (setjmp(deadline) == 0) {
alarm(15);
if ((infp = gzopen(buf, "r")) == NULL) {
syslog(LOG_ERR, "Opening %s: %m", buf);
errors++;
}
else {
doit(infp);
gzclose(infp);
}
}
backcount--;
alarm(0);
}
alarm(0);
sprintf(buf, "%s/%s", USERSVAR, LOGINS);
......@@ -181,7 +199,7 @@ doit(gzFile *infp)
if (skip) {
skip = 0;
continue;
}
}
/*
* Thank dog for strptime! Convert the syslog timestamp
......@@ -194,6 +212,17 @@ doit(gzFile *infp)
}
ll_time = mktime(&tm);
/*
* If the constructed time is in the future, then we have
* year off by one (cause we are possibly looking at files
* created in the previous year). Set the year back by one,
* and redo.
*/
if (ll_time > curtime) {
tm.tm_year--;
ll_time = mktime(&tm);
}
/*
* Scanf the next part, which looks like:
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment