1996-01-05 09:28:11 +00:00
|
|
|
/*
|
|
|
|
* This file contains changes from the Open Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2000-04-04 08:50:01 +00:00
|
|
|
* Copyright 1988, 1989 by the Massachusetts Institute of Technology
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software and its
|
|
|
|
* documentation for any purpose and without fee is hereby granted, provided
|
|
|
|
* that the above copyright notice appear in all copies and that both that
|
|
|
|
* copyright notice and this permission notice appear in supporting
|
|
|
|
* documentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
|
|
|
|
* used in advertising or publicity pertaining to distribution of the
|
|
|
|
* software without specific, written prior permission. M.I.T. and the M.I.T.
|
|
|
|
* S.I.P.B. make no representations about the suitability of this software
|
|
|
|
* for any purpose. It is provided "as is" without express or implied
|
|
|
|
* warranty.
|
|
|
|
*
|
|
|
|
*/
|
1996-01-05 09:28:11 +00:00
|
|
|
|
|
|
|
/*
|
2000-04-04 08:50:01 +00:00
|
|
|
* newsyslog - roll over selected logs at the appropriate time, keeping the a
|
|
|
|
* specified number of backup files around.
|
1996-01-05 09:28:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-10-06 07:46:08 +00:00
|
|
|
static const char rcsid[] =
|
2000-04-04 08:38:30 +00:00
|
|
|
"$FreeBSD$";
|
2000-04-04 08:50:01 +00:00
|
|
|
#endif /* not lint */
|
1996-01-05 09:28:11 +00:00
|
|
|
|
1999-01-22 19:38:39 +00:00
|
|
|
#define OSF
|
1996-01-05 09:28:11 +00:00
|
|
|
#ifndef COMPRESS_POSTFIX
|
1999-01-22 19:38:39 +00:00
|
|
|
#define COMPRESS_POSTFIX ".gz"
|
1996-01-05 09:28:11 +00:00
|
|
|
#endif
|
|
|
|
|
1997-10-06 07:46:08 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <grp.h>
|
1999-01-22 19:38:39 +00:00
|
|
|
#include <paths.h>
|
1997-10-06 07:46:08 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <signal.h>
|
1996-01-05 09:28:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1999-01-22 19:38:39 +00:00
|
|
|
#include <time.h>
|
1996-06-08 23:32:10 +00:00
|
|
|
#include <unistd.h>
|
1996-01-05 09:28:11 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
1999-01-22 19:38:39 +00:00
|
|
|
#include "pathnames.h"
|
|
|
|
|
1996-01-05 09:28:11 +00:00
|
|
|
#define kbytes(size) (((size) + 1023) >> 10)
|
2000-04-04 08:50:01 +00:00
|
|
|
|
1996-01-05 09:28:11 +00:00
|
|
|
#ifdef _IBMR2
|
|
|
|
/* Calculates (db * DEV_BSIZE) */
|
2000-04-04 08:38:30 +00:00
|
|
|
#define dbtob(db) ((unsigned)(db) << UBSHIFT)
|
1996-01-05 09:28:11 +00:00
|
|
|
#endif
|
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
#define CE_COMPACT 1 /* Compact the achived log files */
|
|
|
|
#define CE_BINARY 2 /* Logfile is in binary, don't add */
|
2000-04-04 08:50:01 +00:00
|
|
|
/* status messages */
|
1999-01-22 19:38:39 +00:00
|
|
|
#define CE_TRIMAT 4 /* trim at a specific time */
|
|
|
|
|
1996-01-05 09:28:11 +00:00
|
|
|
#define NONE -1
|
2000-04-04 08:38:30 +00:00
|
|
|
|
1996-01-05 09:28:11 +00:00
|
|
|
struct conf_entry {
|
2000-04-04 08:38:30 +00:00
|
|
|
char *log; /* Name of the log */
|
|
|
|
char *pid_file; /* PID file */
|
|
|
|
int uid; /* Owner of log */
|
|
|
|
int gid; /* Group of log */
|
|
|
|
int numlogs; /* Number of logs to keep */
|
|
|
|
int size; /* Size cutoff to trigger trimming the log */
|
|
|
|
int hours; /* Hours between log trimming */
|
|
|
|
time_t trim_at; /* Specific time to do trimming */
|
|
|
|
int permissions; /* File permissions on the log */
|
|
|
|
int flags; /* Flags (CE_COMPACT & CE_BINARY) */
|
|
|
|
int sig; /* Signal to send */
|
|
|
|
struct conf_entry *next;/* Linked list pointer */
|
1996-01-05 09:28:11 +00:00
|
|
|
};
|
|
|
|
|
2000-04-04 08:50:01 +00:00
|
|
|
int archtodir = 0; /* Archive old logfiles to other directory */
|
2000-04-04 08:38:30 +00:00
|
|
|
int verbose = 0; /* Print out what's going on */
|
|
|
|
int needroot = 1; /* Root privs are necessary */
|
|
|
|
int noaction = 0; /* Don't do anything, just show it */
|
|
|
|
int force = 0; /* Force the trim no matter what */
|
2000-04-04 08:50:01 +00:00
|
|
|
char *archdirname; /* Directory path to old logfiles archive */
|
2000-04-04 08:38:30 +00:00
|
|
|
char *conf = _PATH_CONF; /* Configuration file to use */
|
|
|
|
time_t timenow;
|
|
|
|
|
1997-05-04 01:53:53 +00:00
|
|
|
#define MIN_PID 5
|
2000-04-04 08:38:30 +00:00
|
|
|
#define MAX_PID 99999 /* was lower, see /usr/include/sys/proc.h */
|
2001-01-20 17:36:08 +00:00
|
|
|
char hostname[MAXHOSTNAMELEN]; /* hostname */
|
2000-04-04 08:38:30 +00:00
|
|
|
char *daytime; /* timenow in human readable form */
|
1996-01-05 09:28:11 +00:00
|
|
|
|
2000-05-11 10:17:54 +00:00
|
|
|
static struct conf_entry *parse_file(char **files);
|
1996-06-08 23:32:10 +00:00
|
|
|
static char *sob(char *p);
|
|
|
|
static char *son(char *p);
|
2000-04-04 08:38:30 +00:00
|
|
|
static char *missing_field(char *p, char *errline);
|
|
|
|
static void do_entry(struct conf_entry * ent);
|
|
|
|
static void PRS(int argc, char **argv);
|
1996-06-08 23:32:10 +00:00
|
|
|
static void usage();
|
2000-04-04 08:38:30 +00:00
|
|
|
static void dotrim(char *log, char *pid_file, int numdays, int falgs, int perm, int owner_uid, int group_gid, int sig);
|
1996-06-08 23:32:10 +00:00
|
|
|
static int log_trim(char *log);
|
|
|
|
static void compress_log(char *log);
|
|
|
|
static int sizefile(char *file);
|
|
|
|
static int age_old_log(char *file);
|
1997-05-04 01:53:53 +00:00
|
|
|
static pid_t get_pid(char *pid_file);
|
2000-04-04 08:50:01 +00:00
|
|
|
static time_t parse8601(char *s);
|
|
|
|
static void movefile(char *from, char *to, int perm, int owner_uid, int group_gid);
|
|
|
|
static void createdir(char *dirpart);
|
|
|
|
static time_t parseDWM(char *s);
|
1996-06-08 23:32:10 +00:00
|
|
|
|
2000-04-04 08:50:01 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
struct conf_entry *p, *q;
|
|
|
|
|
|
|
|
PRS(argc, argv);
|
|
|
|
if (needroot && getuid() && geteuid())
|
|
|
|
errx(1, "must have root privs");
|
2000-05-11 10:17:54 +00:00
|
|
|
p = q = parse_file(argv + optind);
|
2000-04-04 08:38:30 +00:00
|
|
|
|
|
|
|
while (p) {
|
|
|
|
do_entry(p);
|
|
|
|
p = p->next;
|
|
|
|
free((char *) q);
|
|
|
|
q = p;
|
|
|
|
}
|
|
|
|
return (0);
|
1996-01-05 09:28:11 +00:00
|
|
|
}
|
|
|
|
|
2000-04-04 08:50:01 +00:00
|
|
|
static void
|
|
|
|
do_entry(struct conf_entry * ent)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
int size, modtime;
|
|
|
|
char *pid_file;
|
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
if (ent->flags & CE_COMPACT)
|
|
|
|
printf("%s <%dZ>: ", ent->log, ent->numlogs);
|
|
|
|
else
|
|
|
|
printf("%s <%d>: ", ent->log, ent->numlogs);
|
|
|
|
}
|
|
|
|
size = sizefile(ent->log);
|
|
|
|
modtime = age_old_log(ent->log);
|
|
|
|
if (size < 0) {
|
|
|
|
if (verbose)
|
|
|
|
printf("does not exist.\n");
|
|
|
|
} else {
|
1999-01-22 19:38:39 +00:00
|
|
|
if (ent->flags & CE_TRIMAT) {
|
|
|
|
if (timenow < ent->trim_at
|
2000-04-04 08:38:30 +00:00
|
|
|
|| difftime(timenow, ent->trim_at) >= 60 * 60) {
|
1999-01-22 19:38:39 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("--> will trim at %s",
|
2000-04-04 08:38:30 +00:00
|
|
|
ctime(&ent->trim_at));
|
1999-01-22 19:38:39 +00:00
|
|
|
return;
|
|
|
|
} else if (verbose && ent->hours <= 0) {
|
|
|
|
printf("--> time is up\n");
|
|
|
|
}
|
|
|
|
}
|
2000-04-04 08:38:30 +00:00
|
|
|
if (verbose && (ent->size > 0))
|
|
|
|
printf("size (Kb): %d [%d] ", size, ent->size);
|
|
|
|
if (verbose && (ent->hours > 0))
|
|
|
|
printf(" age (hr): %d [%d] ", modtime, ent->hours);
|
|
|
|
if (force || ((ent->size > 0) && (size >= ent->size)) ||
|
1999-01-22 19:38:39 +00:00
|
|
|
(ent->hours <= 0 && (ent->flags & CE_TRIMAT)) ||
|
2000-04-04 08:38:30 +00:00
|
|
|
((ent->hours > 0) && ((modtime >= ent->hours)
|
|
|
|
|| (modtime < 0)))) {
|
|
|
|
if (verbose)
|
|
|
|
printf("--> trimming log....\n");
|
|
|
|
if (noaction && !verbose) {
|
|
|
|
if (ent->flags & CE_COMPACT)
|
|
|
|
printf("%s <%dZ>: trimming\n",
|
|
|
|
ent->log, ent->numlogs);
|
|
|
|
else
|
|
|
|
printf("%s <%d>: trimming\n",
|
|
|
|
ent->log, ent->numlogs);
|
|
|
|
}
|
1998-05-10 21:13:29 +00:00
|
|
|
if (ent->pid_file) {
|
|
|
|
pid_file = ent->pid_file;
|
|
|
|
} else {
|
|
|
|
/* Only try to notify syslog if we are root */
|
|
|
|
if (needroot)
|
1999-01-22 19:38:39 +00:00
|
|
|
pid_file = _PATH_SYSLOGPID;
|
1998-05-10 21:13:29 +00:00
|
|
|
else
|
|
|
|
pid_file = NULL;
|
|
|
|
}
|
2000-04-04 08:38:30 +00:00
|
|
|
dotrim(ent->log, pid_file, ent->numlogs,
|
1998-06-09 18:24:04 +00:00
|
|
|
ent->flags, ent->permissions, ent->uid, ent->gid, ent->sig);
|
2000-04-04 08:38:30 +00:00
|
|
|
} else {
|
|
|
|
if (verbose)
|
|
|
|
printf("--> skipping\n");
|
|
|
|
}
|
|
|
|
}
|
1996-01-05 09:28:11 +00:00
|
|
|
}
|
|
|
|
|
2000-04-04 08:50:01 +00:00
|
|
|
static void
|
|
|
|
PRS(int argc, char **argv)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
int c;
|
|
|
|
char *p;
|
1996-01-05 09:28:11 +00:00
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
timenow = time((time_t *) 0);
|
|
|
|
daytime = ctime(&timenow) + 4;
|
|
|
|
daytime[15] = '\0';
|
1996-01-05 09:28:11 +00:00
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
/* Let's get our hostname */
|
|
|
|
(void) gethostname(hostname, sizeof(hostname));
|
1996-01-05 09:28:11 +00:00
|
|
|
|
|
|
|
/* Truncate domain */
|
1996-06-08 23:32:10 +00:00
|
|
|
if ((p = strchr(hostname, '.'))) {
|
1996-01-05 09:28:11 +00:00
|
|
|
*p = '\0';
|
|
|
|
}
|
2000-04-04 08:50:01 +00:00
|
|
|
while ((c = getopt(argc, argv, "nrvFf:a:t:")) != -1)
|
2000-04-04 08:38:30 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'n':
|
|
|
|
noaction++;
|
|
|
|
break;
|
2000-04-04 08:50:01 +00:00
|
|
|
case 'a':
|
|
|
|
archtodir++;
|
|
|
|
archdirname = optarg;
|
|
|
|
break;
|
2000-04-04 08:38:30 +00:00
|
|
|
case 'r':
|
|
|
|
needroot = 0;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
conf = optarg;
|
1998-05-10 21:13:29 +00:00
|
|
|
break;
|
1998-03-14 22:28:25 +00:00
|
|
|
case 'F':
|
|
|
|
force++;
|
|
|
|
break;
|
2000-04-04 08:38:30 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
1999-01-22 19:38:39 +00:00
|
|
|
}
|
1996-01-05 09:28:11 +00:00
|
|
|
|
2000-04-04 08:50:01 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2000-04-04 08:50:01 +00:00
|
|
|
fprintf(stderr, "usage: newsyslog [-Fnrv] [-f config-file] [-a directory]\n");
|
2000-04-04 08:38:30 +00:00
|
|
|
exit(1);
|
1996-01-05 09:28:11 +00:00
|
|
|
}
|
|
|
|
|
2000-04-04 08:50:01 +00:00
|
|
|
/*
|
|
|
|
* Parse a configuration file and return a linked list of all the logs to
|
|
|
|
* process
|
1996-01-05 09:28:11 +00:00
|
|
|
*/
|
2000-04-04 08:38:30 +00:00
|
|
|
static struct conf_entry *
|
2000-05-11 10:17:54 +00:00
|
|
|
parse_file(char **files)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
FILE *f;
|
|
|
|
char line[BUFSIZ], *parse, *q;
|
|
|
|
char *errline, *group;
|
2000-05-11 10:17:54 +00:00
|
|
|
char **p;
|
2000-04-04 08:38:30 +00:00
|
|
|
struct conf_entry *first = NULL;
|
|
|
|
struct conf_entry *working = NULL;
|
|
|
|
struct passwd *pass;
|
|
|
|
struct group *grp;
|
1997-05-06 23:11:06 +00:00
|
|
|
int eol;
|
1996-01-05 09:28:11 +00:00
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
if (strcmp(conf, "-"))
|
|
|
|
f = fopen(conf, "r");
|
|
|
|
else
|
|
|
|
f = stdin;
|
|
|
|
if (!f)
|
|
|
|
err(1, "%s", conf);
|
|
|
|
while (fgets(line, BUFSIZ, f)) {
|
|
|
|
if ((line[0] == '\n') || (line[0] == '#'))
|
|
|
|
continue;
|
|
|
|
errline = strdup(line);
|
2000-05-11 10:17:54 +00:00
|
|
|
|
|
|
|
q = parse = missing_field(sob(line), errline);
|
|
|
|
parse = son(line);
|
|
|
|
if (!*parse)
|
|
|
|
errx(1, "malformed line (missing fields):\n%s", errline);
|
|
|
|
*parse = '\0';
|
|
|
|
|
|
|
|
if (*files) {
|
|
|
|
for (p = files; *p; ++p)
|
|
|
|
if (strcmp(*p, q) == 0)
|
|
|
|
break;
|
|
|
|
if (!*p)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
if (!first) {
|
2001-01-20 17:36:08 +00:00
|
|
|
if ((working = (struct conf_entry *) malloc(sizeof(struct conf_entry))) == NULL)
|
|
|
|
err(1, "malloc");
|
2000-04-04 08:38:30 +00:00
|
|
|
first = working;
|
|
|
|
} else {
|
2001-01-20 17:36:08 +00:00
|
|
|
if ((working->next = (struct conf_entry *) malloc(sizeof(struct conf_entry))) == NULL)
|
|
|
|
err(1, "malloc");
|
2000-04-04 08:38:30 +00:00
|
|
|
working = working->next;
|
|
|
|
}
|
2001-01-20 17:36:08 +00:00
|
|
|
if ((working->log = strdup(q)) == NULL)
|
|
|
|
err(1, "strdup");
|
1996-01-05 09:28:11 +00:00
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
q = parse = missing_field(sob(++parse), errline);
|
|
|
|
parse = son(parse);
|
1997-05-06 23:11:06 +00:00
|
|
|
if (!*parse)
|
2000-04-04 08:38:30 +00:00
|
|
|
errx(1, "malformed line (missing fields):\n%s", errline);
|
|
|
|
*parse = '\0';
|
|
|
|
if ((group = strchr(q, ':')) != NULL ||
|
|
|
|
(group = strrchr(q, '.')) != NULL) {
|
|
|
|
*group++ = '\0';
|
|
|
|
if (*q) {
|
|
|
|
if (!(isnumber(*q))) {
|
|
|
|
if ((pass = getpwnam(q)) == NULL)
|
|
|
|
errx(1,
|
|
|
|
"error in config file; unknown user:\n%s",
|
|
|
|
errline);
|
|
|
|
working->uid = pass->pw_uid;
|
|
|
|
} else
|
|
|
|
working->uid = atoi(q);
|
|
|
|
} else
|
|
|
|
working->uid = NONE;
|
|
|
|
|
|
|
|
q = group;
|
|
|
|
if (*q) {
|
|
|
|
if (!(isnumber(*q))) {
|
|
|
|
if ((grp = getgrnam(q)) == NULL)
|
|
|
|
errx(1,
|
|
|
|
"error in config file; unknown group:\n%s",
|
|
|
|
errline);
|
|
|
|
working->gid = grp->gr_gid;
|
|
|
|
} else
|
|
|
|
working->gid = atoi(q);
|
|
|
|
} else
|
|
|
|
working->gid = NONE;
|
|
|
|
|
|
|
|
q = parse = missing_field(sob(++parse), errline);
|
|
|
|
parse = son(parse);
|
|
|
|
if (!*parse)
|
|
|
|
errx(1, "malformed line (missing fields):\n%s", errline);
|
|
|
|
*parse = '\0';
|
|
|
|
} else
|
|
|
|
working->uid = working->gid = NONE;
|
|
|
|
|
|
|
|
if (!sscanf(q, "%o", &working->permissions))
|
|
|
|
errx(1, "error in config file; bad permissions:\n%s",
|
|
|
|
errline);
|
|
|
|
|
|
|
|
q = parse = missing_field(sob(++parse), errline);
|
|
|
|
parse = son(parse);
|
1997-05-06 23:11:06 +00:00
|
|
|
if (!*parse)
|
2000-04-04 08:38:30 +00:00
|
|
|
errx(1, "malformed line (missing fields):\n%s", errline);
|
|
|
|
*parse = '\0';
|
|
|
|
if (!sscanf(q, "%d", &working->numlogs))
|
|
|
|
errx(1, "error in config file; bad number:\n%s",
|
|
|
|
errline);
|
|
|
|
|
|
|
|
q = parse = missing_field(sob(++parse), errline);
|
|
|
|
parse = son(parse);
|
1997-05-06 23:11:06 +00:00
|
|
|
if (!*parse)
|
2000-04-04 08:38:30 +00:00
|
|
|
errx(1, "malformed line (missing fields):\n%s", errline);
|
|
|
|
*parse = '\0';
|
|
|
|
if (isdigit(*q))
|
|
|
|
working->size = atoi(q);
|
|
|
|
else
|
|
|
|
working->size = -1;
|
|
|
|
|
|
|
|
working->flags = 0;
|
|
|
|
q = parse = missing_field(sob(++parse), errline);
|
|
|
|
parse = son(parse);
|
1997-05-06 23:11:06 +00:00
|
|
|
eol = !*parse;
|
2000-04-04 08:38:30 +00:00
|
|
|
*parse = '\0';
|
1999-01-22 19:38:39 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
char *ep;
|
|
|
|
u_long ul;
|
1999-01-22 19:38:39 +00:00
|
|
|
|
|
|
|
ul = strtoul(q, &ep, 10);
|
|
|
|
if (ep == q)
|
|
|
|
working->hours = 0;
|
|
|
|
else if (*ep == '*')
|
|
|
|
working->hours = -1;
|
|
|
|
else if (ul > INT_MAX)
|
|
|
|
errx(1, "interval is too large:\n%s", errline);
|
|
|
|
else
|
|
|
|
working->hours = ul;
|
|
|
|
|
2000-04-04 08:50:01 +00:00
|
|
|
if (*ep != '\0' && *ep != '@' && *ep != '*' && *ep != '$')
|
1999-01-22 19:38:39 +00:00
|
|
|
errx(1, "malformed interval/at:\n%s", errline);
|
|
|
|
if (*ep == '@') {
|
|
|
|
if ((working->trim_at = parse8601(ep + 1))
|
2000-04-04 08:38:30 +00:00
|
|
|
== (time_t) - 1)
|
1999-01-22 19:38:39 +00:00
|
|
|
errx(1, "malformed at:\n%s", errline);
|
|
|
|
working->flags |= CE_TRIMAT;
|
2000-04-04 08:50:01 +00:00
|
|
|
} else if (*ep == '$') {
|
|
|
|
if ((working->trim_at = parseDWM(ep + 1))
|
|
|
|
== (time_t) - 1)
|
|
|
|
errx(1, "malformed at:\n%s", errline);
|
|
|
|
working->flags |= CE_TRIMAT;
|
1999-01-22 19:38:39 +00:00
|
|
|
}
|
|
|
|
}
|
1996-01-05 09:28:11 +00:00
|
|
|
|
1997-05-06 23:11:06 +00:00
|
|
|
if (eol)
|
2000-04-04 08:38:30 +00:00
|
|
|
q = NULL;
|
1997-05-06 23:11:06 +00:00
|
|
|
else {
|
2000-04-04 08:38:30 +00:00
|
|
|
q = parse = sob(++parse); /* Optional field */
|
|
|
|
parse = son(parse);
|
|
|
|
if (!*parse)
|
|
|
|
eol = 1;
|
|
|
|
*parse = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
while (q && *q && !isspace(*q)) {
|
|
|
|
if ((*q == 'Z') || (*q == 'z'))
|
|
|
|
working->flags |= CE_COMPACT;
|
|
|
|
else if ((*q == 'B') || (*q == 'b'))
|
|
|
|
working->flags |= CE_BINARY;
|
|
|
|
else if (*q != '-')
|
|
|
|
errx(1, "illegal flag in config file -- %c", *q);
|
|
|
|
q++;
|
1997-05-06 23:11:06 +00:00
|
|
|
}
|
1997-05-04 01:53:53 +00:00
|
|
|
|
1997-05-06 23:11:06 +00:00
|
|
|
if (eol)
|
2000-04-04 08:38:30 +00:00
|
|
|
q = NULL;
|
1997-05-06 23:11:06 +00:00
|
|
|
else {
|
2000-04-04 08:38:30 +00:00
|
|
|
q = parse = sob(++parse); /* Optional field */
|
|
|
|
parse = son(parse);
|
|
|
|
if (!*parse)
|
|
|
|
eol = 1;
|
|
|
|
*parse = '\0';
|
1997-05-06 23:11:06 +00:00
|
|
|
}
|
1997-05-04 01:53:53 +00:00
|
|
|
|
|
|
|
working->pid_file = NULL;
|
|
|
|
if (q && *q) {
|
|
|
|
if (*q == '/')
|
|
|
|
working->pid_file = strdup(q);
|
1998-06-09 18:24:04 +00:00
|
|
|
else if (isdigit(*q))
|
|
|
|
goto got_sig;
|
2000-04-04 08:38:30 +00:00
|
|
|
else
|
|
|
|
errx(1, "illegal pid file or signal number in config file:\n%s", errline);
|
1998-06-09 18:24:04 +00:00
|
|
|
}
|
|
|
|
if (eol)
|
2000-04-04 08:38:30 +00:00
|
|
|
q = NULL;
|
1998-06-09 18:24:04 +00:00
|
|
|
else {
|
2000-04-04 08:38:30 +00:00
|
|
|
q = parse = sob(++parse); /* Optional field */
|
|
|
|
*(parse = son(parse)) = '\0';
|
1998-06-09 18:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
working->sig = SIGHUP;
|
|
|
|
if (q && *q) {
|
|
|
|
if (isdigit(*q)) {
|
2000-04-04 08:38:30 +00:00
|
|
|
got_sig:
|
1998-06-09 18:24:04 +00:00
|
|
|
working->sig = atoi(q);
|
|
|
|
} else {
|
2000-04-04 08:38:30 +00:00
|
|
|
err_sig:
|
|
|
|
errx(1, "illegal signal number in config file:\n%s", errline);
|
1998-06-09 18:24:04 +00:00
|
|
|
}
|
|
|
|
if (working->sig < 1 || working->sig >= NSIG)
|
|
|
|
goto err_sig;
|
1997-05-04 01:53:53 +00:00
|
|
|
}
|
2000-04-04 08:38:30 +00:00
|
|
|
free(errline);
|
|
|
|
}
|
|
|
|
if (working)
|
|
|
|
working->next = (struct conf_entry *) NULL;
|
|
|
|
(void) fclose(f);
|
|
|
|
return (first);
|
1996-01-05 09:28:11 +00:00
|
|
|
}
|
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
static char *
|
2000-04-04 08:50:01 +00:00
|
|
|
missing_field(char *p, char *errline)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
if (!p || !*p)
|
|
|
|
errx(1, "missing field in config file:\n%s", errline);
|
|
|
|
return (p);
|
1996-01-05 09:28:11 +00:00
|
|
|
}
|
|
|
|
|
2000-04-04 08:50:01 +00:00
|
|
|
static void
|
|
|
|
dotrim(char *log, char *pid_file, int numdays, int flags, int perm,
|
|
|
|
int owner_uid, int group_gid, int sig)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2001-01-20 17:36:08 +00:00
|
|
|
char dirpart[MAXPATHLEN], namepart[MAXPATHLEN];
|
|
|
|
char file1[MAXPATHLEN], file2[MAXPATHLEN];
|
|
|
|
char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
|
2000-04-04 08:38:30 +00:00
|
|
|
int notified, need_notification, fd, _numdays;
|
|
|
|
struct stat st;
|
|
|
|
pid_t pid;
|
1996-01-05 09:28:11 +00:00
|
|
|
|
|
|
|
#ifdef _IBMR2
|
2000-04-04 08:50:01 +00:00
|
|
|
/*
|
|
|
|
* AIX 3.1 has a broken fchown- if the owner_uid is -1, it will
|
|
|
|
* actually change it to be owned by uid -1, instead of leaving it
|
|
|
|
* as is, as it is supposed to.
|
|
|
|
*/
|
2000-04-04 08:38:30 +00:00
|
|
|
if (owner_uid == -1)
|
|
|
|
owner_uid = geteuid();
|
1996-01-05 09:28:11 +00:00
|
|
|
#endif
|
|
|
|
|
2000-04-04 08:50:01 +00:00
|
|
|
if (archtodir) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* build complete name of archive directory into dirpart */
|
|
|
|
if (*archdirname == '/') { /* absolute */
|
2001-01-20 17:36:08 +00:00
|
|
|
strlcpy(dirpart, archdirname, sizeof(dirpart));
|
2000-04-04 08:50:01 +00:00
|
|
|
} else { /* relative */
|
|
|
|
/* get directory part of logfile */
|
2001-01-20 17:36:08 +00:00
|
|
|
strlcpy(dirpart, log, sizeof(dirpart));
|
2000-04-04 08:50:01 +00:00
|
|
|
if ((p = rindex(dirpart, '/')) == NULL)
|
|
|
|
dirpart[0] = '\0';
|
|
|
|
else
|
|
|
|
*(p + 1) = '\0';
|
2001-01-20 17:36:08 +00:00
|
|
|
strlcat(dirpart, archdirname, sizeof(dirpart));
|
2000-04-04 08:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check if archive directory exists, if not, create it */
|
|
|
|
if (lstat(dirpart, &st))
|
|
|
|
createdir(dirpart);
|
|
|
|
|
|
|
|
/* get filename part of logfile */
|
|
|
|
if ((p = rindex(log, '/')) == NULL)
|
2001-01-20 17:36:08 +00:00
|
|
|
strlcpy(namepart, log, sizeof(namepart));
|
2000-04-04 08:50:01 +00:00
|
|
|
else
|
2001-01-20 17:36:08 +00:00
|
|
|
strlcpy(namepart, p + 1, sizeof(namepart));
|
2000-04-04 08:50:01 +00:00
|
|
|
|
|
|
|
/* name of oldest log */
|
2001-01-20 17:36:08 +00:00
|
|
|
(void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart, namepart, numdays);
|
|
|
|
(void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
|
|
|
|
COMPRESS_POSTFIX);
|
2000-04-04 08:50:01 +00:00
|
|
|
} else {
|
|
|
|
/* name of oldest log */
|
2001-01-20 17:36:08 +00:00
|
|
|
(void) snprintf(file1, sizeof(file1), "%s.%d", log, numdays);
|
|
|
|
(void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
|
|
|
|
COMPRESS_POSTFIX);
|
2000-04-04 08:50:01 +00:00
|
|
|
}
|
2000-04-04 08:38:30 +00:00
|
|
|
|
|
|
|
if (noaction) {
|
|
|
|
printf("rm -f %s\n", file1);
|
|
|
|
printf("rm -f %s\n", zfile1);
|
|
|
|
} else {
|
|
|
|
(void) unlink(file1);
|
|
|
|
(void) unlink(zfile1);
|
|
|
|
}
|
1996-01-05 09:28:11 +00:00
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
/* Move down log files */
|
1996-09-09 15:32:26 +00:00
|
|
|
_numdays = numdays; /* preserve */
|
2000-04-04 08:38:30 +00:00
|
|
|
while (numdays--) {
|
2000-04-04 08:50:01 +00:00
|
|
|
|
2001-01-20 17:36:08 +00:00
|
|
|
(void) strlcpy(file2, file1, sizeof(file2));
|
2000-04-04 08:50:01 +00:00
|
|
|
|
|
|
|
if (archtodir)
|
2001-01-20 17:36:08 +00:00
|
|
|
(void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart, namepart, numdays);
|
2000-04-04 08:50:01 +00:00
|
|
|
else
|
2001-01-20 17:36:08 +00:00
|
|
|
(void) snprintf(file1, sizeof(file1), "%s.%d", log, numdays);
|
2000-04-04 08:50:01 +00:00
|
|
|
|
2001-01-20 17:36:08 +00:00
|
|
|
(void) strlcpy(zfile1, file1, sizeof(zfile1));
|
|
|
|
(void) strlcpy(zfile2, file2, sizeof(zfile2));
|
2000-04-04 08:38:30 +00:00
|
|
|
if (lstat(file1, &st)) {
|
2001-01-20 17:36:08 +00:00
|
|
|
(void) strlcat(zfile1, COMPRESS_POSTFIX, sizeof(zfile1));
|
|
|
|
(void) strlcat(zfile2, COMPRESS_POSTFIX, sizeof(zfile2));
|
2000-04-04 08:38:30 +00:00
|
|
|
if (lstat(zfile1, &st))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (noaction) {
|
|
|
|
printf("mv %s %s\n", zfile1, zfile2);
|
|
|
|
printf("chmod %o %s\n", perm, zfile2);
|
|
|
|
printf("chown %d.%d %s\n",
|
|
|
|
owner_uid, group_gid, zfile2);
|
|
|
|
} else {
|
|
|
|
(void) rename(zfile1, zfile2);
|
|
|
|
(void) chmod(zfile2, perm);
|
|
|
|
(void) chown(zfile2, owner_uid, group_gid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!noaction && !(flags & CE_BINARY))
|
|
|
|
(void) log_trim(log); /* Report the trimming to the old log */
|
1996-01-05 09:28:11 +00:00
|
|
|
|
1996-09-09 15:32:26 +00:00
|
|
|
if (!_numdays) {
|
1996-09-06 06:36:07 +00:00
|
|
|
if (noaction)
|
2000-04-04 08:38:30 +00:00
|
|
|
printf("rm %s\n", log);
|
1996-09-06 06:36:07 +00:00
|
|
|
else
|
2000-04-04 08:38:30 +00:00
|
|
|
(void) unlink(log);
|
|
|
|
} else {
|
1996-09-06 06:36:07 +00:00
|
|
|
if (noaction)
|
2000-04-04 08:38:30 +00:00
|
|
|
printf("mv %s to %s\n", log, file1);
|
2000-04-04 08:50:01 +00:00
|
|
|
else {
|
|
|
|
if (archtodir)
|
|
|
|
movefile(log, file1, perm, owner_uid, group_gid);
|
|
|
|
else
|
|
|
|
(void) rename(log, file1);
|
|
|
|
}
|
1996-09-06 06:36:07 +00:00
|
|
|
}
|
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
if (noaction)
|
|
|
|
printf("Start new log...");
|
|
|
|
else {
|
|
|
|
fd = creat(log, perm);
|
|
|
|
if (fd < 0)
|
|
|
|
err(1, "can't start new log");
|
|
|
|
if (fchown(fd, owner_uid, group_gid))
|
|
|
|
err(1, "can't chmod new log file");
|
|
|
|
(void) close(fd);
|
|
|
|
if (!(flags & CE_BINARY))
|
|
|
|
if (log_trim(log)) /* Add status message */
|
|
|
|
err(1, "can't add status message to log");
|
|
|
|
}
|
|
|
|
if (noaction)
|
|
|
|
printf("chmod %o %s...\n", perm, log);
|
|
|
|
else
|
|
|
|
(void) chmod(log, perm);
|
1997-05-04 01:53:53 +00:00
|
|
|
|
|
|
|
pid = 0;
|
|
|
|
need_notification = notified = 0;
|
|
|
|
if (pid_file != NULL) {
|
|
|
|
need_notification = 1;
|
|
|
|
pid = get_pid(pid_file);
|
|
|
|
}
|
|
|
|
if (pid) {
|
|
|
|
if (noaction) {
|
|
|
|
notified = 1;
|
2000-04-04 08:38:30 +00:00
|
|
|
printf("kill -%d %d\n", sig, (int) pid);
|
|
|
|
} else if (kill(pid, sig))
|
|
|
|
warn("can't notify daemon, pid %d", (int) pid);
|
1997-05-04 01:53:53 +00:00
|
|
|
else {
|
|
|
|
notified = 1;
|
|
|
|
if (verbose)
|
2000-04-04 08:38:30 +00:00
|
|
|
printf("daemon pid %d notified\n", (int) pid);
|
1997-05-04 01:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((flags & CE_COMPACT)) {
|
|
|
|
if (need_notification && !notified)
|
2000-08-15 09:34:41 +00:00
|
|
|
warnx("log %s not compressed because daemon not notified", log);
|
1997-05-04 01:53:53 +00:00
|
|
|
else if (noaction)
|
2000-04-04 08:38:30 +00:00
|
|
|
printf("Compress %s.0\n", log);
|
1997-05-04 01:53:53 +00:00
|
|
|
else {
|
|
|
|
if (notified) {
|
|
|
|
if (verbose)
|
|
|
|
printf("small pause to allow daemon to close log\n");
|
1997-11-30 18:58:18 +00:00
|
|
|
sleep(10);
|
1997-05-04 01:53:53 +00:00
|
|
|
}
|
2000-04-04 08:50:01 +00:00
|
|
|
if (archtodir) {
|
2001-01-20 17:36:08 +00:00
|
|
|
(void) snprintf(file1, sizeof(file1), "%s/%s", dirpart, namepart);
|
2000-04-04 08:50:01 +00:00
|
|
|
compress_log(file1);
|
|
|
|
} else {
|
|
|
|
compress_log(log);
|
|
|
|
}
|
1997-05-04 01:53:53 +00:00
|
|
|
}
|
2000-04-04 08:38:30 +00:00
|
|
|
}
|
1996-01-05 09:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Log the fact that the logs were turned over */
|
2000-04-04 08:50:01 +00:00
|
|
|
static int
|
|
|
|
log_trim(char *log)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(log, "a")) == NULL)
|
|
|
|
return (-1);
|
|
|
|
fprintf(f, "%s %s newsyslog[%d]: logfile turned over\n",
|
|
|
|
daytime, hostname, (int) getpid());
|
|
|
|
if (fclose(f) == EOF)
|
|
|
|
err(1, "log_trim: fclose:");
|
|
|
|
return (0);
|
1996-01-05 09:28:11 +00:00
|
|
|
}
|
|
|
|
|
2000-04-04 08:50:01 +00:00
|
|
|
/* Fork of gzip to compress the old log file */
|
|
|
|
static void
|
|
|
|
compress_log(char *log)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
pid_t pid;
|
2001-01-20 17:36:08 +00:00
|
|
|
char tmp[MAXPATHLEN];
|
2000-04-04 08:38:30 +00:00
|
|
|
|
2001-01-20 17:36:08 +00:00
|
|
|
(void) snprintf(tmp, sizeof(tmp), "%s.0", log);
|
1997-05-04 01:53:53 +00:00
|
|
|
pid = fork();
|
2000-04-04 08:38:30 +00:00
|
|
|
if (pid < 0)
|
|
|
|
err(1, "fork");
|
|
|
|
else if (!pid) {
|
2001-07-09 09:24:06 +00:00
|
|
|
(void) execl(_PATH_GZIP, _PATH_GZIP, "-f", tmp, (char *)0);
|
1999-01-22 19:38:39 +00:00
|
|
|
err(1, _PATH_GZIP);
|
2000-04-04 08:38:30 +00:00
|
|
|
}
|
1996-01-05 09:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return size in kilobytes of a file */
|
2000-04-04 08:50:01 +00:00
|
|
|
static int
|
|
|
|
sizefile(char *file)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
struct stat sb;
|
1996-01-05 09:28:11 +00:00
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
if (stat(file, &sb) < 0)
|
|
|
|
return (-1);
|
|
|
|
return (kbytes(dbtob(sb.st_blocks)));
|
1996-01-05 09:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the age of old log file (file.0) */
|
2000-04-04 08:50:01 +00:00
|
|
|
static int
|
|
|
|
age_old_log(char *file)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
struct stat sb;
|
|
|
|
char tmp[MAXPATHLEN + sizeof(".0") + sizeof(COMPRESS_POSTFIX) + 1];
|
|
|
|
|
2000-04-04 08:50:01 +00:00
|
|
|
if (archtodir) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* build name of archive directory into tmp */
|
|
|
|
if (*archdirname == '/') { /* absolute */
|
2001-01-20 17:36:08 +00:00
|
|
|
strlcpy(tmp, archdirname, sizeof(tmp));
|
2000-04-04 08:50:01 +00:00
|
|
|
} else { /* relative */
|
|
|
|
/* get directory part of logfile */
|
2001-01-20 17:36:08 +00:00
|
|
|
strlcpy(tmp, file, sizeof(tmp));
|
2000-04-04 08:50:01 +00:00
|
|
|
if ((p = rindex(tmp, '/')) == NULL)
|
|
|
|
tmp[0] = '\0';
|
|
|
|
else
|
|
|
|
*(p + 1) = '\0';
|
2001-01-20 17:36:08 +00:00
|
|
|
strlcat(tmp, archdirname, sizeof(tmp));
|
2000-04-04 08:50:01 +00:00
|
|
|
}
|
|
|
|
|
2001-01-20 17:36:08 +00:00
|
|
|
strlcat(tmp, "/", sizeof(tmp));
|
2000-04-04 08:50:01 +00:00
|
|
|
|
|
|
|
/* get filename part of logfile */
|
|
|
|
if ((p = rindex(file, '/')) == NULL)
|
2001-01-20 17:36:08 +00:00
|
|
|
strlcat(tmp, file, sizeof(tmp));
|
2000-04-04 08:50:01 +00:00
|
|
|
else
|
2001-01-20 17:36:08 +00:00
|
|
|
strlcat(tmp, p + 1, sizeof(tmp));
|
2000-04-04 08:50:01 +00:00
|
|
|
} else {
|
2001-01-20 17:36:08 +00:00
|
|
|
(void) strlcpy(tmp, file, sizeof(tmp));
|
2000-04-04 08:50:01 +00:00
|
|
|
}
|
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
if (stat(strcat(tmp, ".0"), &sb) < 0)
|
|
|
|
if (stat(strcat(tmp, COMPRESS_POSTFIX), &sb) < 0)
|
|
|
|
return (-1);
|
|
|
|
return ((int) (timenow - sb.st_mtime + 1800) / 3600);
|
1996-01-05 09:28:11 +00:00
|
|
|
}
|
|
|
|
|
2000-04-04 08:50:01 +00:00
|
|
|
static pid_t
|
|
|
|
get_pid(char *pid_file)
|
1997-05-04 01:53:53 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
2000-04-04 08:38:30 +00:00
|
|
|
char line[BUFSIZ];
|
1997-05-04 01:53:53 +00:00
|
|
|
pid_t pid = 0;
|
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
if ((f = fopen(pid_file, "r")) == NULL)
|
1997-05-04 01:53:53 +00:00
|
|
|
warn("can't open %s pid file to restart a daemon",
|
2000-04-04 08:38:30 +00:00
|
|
|
pid_file);
|
1997-05-04 01:53:53 +00:00
|
|
|
else {
|
2000-04-04 08:38:30 +00:00
|
|
|
if (fgets(line, BUFSIZ, f)) {
|
1997-05-04 01:53:53 +00:00
|
|
|
pid = atol(line);
|
|
|
|
if (pid < MIN_PID || pid > MAX_PID) {
|
2000-04-04 08:38:30 +00:00
|
|
|
warnx("preposterous process number: %d", (int) pid);
|
1997-05-04 01:53:53 +00:00
|
|
|
pid = 0;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
warn("can't read %s pid file to restart a daemon",
|
2000-04-04 08:38:30 +00:00
|
|
|
pid_file);
|
|
|
|
(void) fclose(f);
|
1997-05-04 01:53:53 +00:00
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
1996-01-05 09:28:11 +00:00
|
|
|
|
|
|
|
/* Skip Over Blanks */
|
2000-04-04 08:38:30 +00:00
|
|
|
char *
|
2000-04-04 08:50:01 +00:00
|
|
|
sob(char *p)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
while (p && *p && isspace(*p))
|
|
|
|
p++;
|
|
|
|
return (p);
|
1996-01-05 09:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip Over Non-Blanks */
|
2000-04-04 08:38:30 +00:00
|
|
|
char *
|
2000-04-04 08:50:01 +00:00
|
|
|
son(char *p)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
while (p && *p && !isspace(*p))
|
|
|
|
p++;
|
|
|
|
return (p);
|
1996-01-05 09:28:11 +00:00
|
|
|
}
|
1999-01-22 19:38:39 +00:00
|
|
|
|
|
|
|
/*
|
2000-04-04 08:50:01 +00:00
|
|
|
* Parse a limited subset of ISO 8601. The specific format is as follows:
|
1999-01-22 19:38:39 +00:00
|
|
|
*
|
2000-04-04 08:50:01 +00:00
|
|
|
* [CC[YY[MM[DD]]]][THH[MM[SS]]] (where `T' is the literal letter)
|
1999-01-22 19:38:39 +00:00
|
|
|
*
|
2000-04-04 08:50:01 +00:00
|
|
|
* We don't accept a timezone specification; missing fields (including timezone)
|
|
|
|
* are defaulted to the current date but time zero.
|
1999-01-22 19:38:39 +00:00
|
|
|
*/
|
|
|
|
static time_t
|
2000-04-04 08:50:01 +00:00
|
|
|
parse8601(char *s)
|
1999-01-22 19:38:39 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
char *t;
|
|
|
|
struct tm tm, *tmp;
|
|
|
|
u_long ul;
|
1999-01-22 19:38:39 +00:00
|
|
|
|
|
|
|
tmp = localtime(&timenow);
|
|
|
|
tm = *tmp;
|
|
|
|
|
|
|
|
tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
|
|
|
|
|
|
|
|
ul = strtoul(s, &t, 10);
|
|
|
|
if (*t != '\0' && *t != 'T')
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
2000-04-04 08:38:30 +00:00
|
|
|
* Now t points either to the end of the string (if no time was
|
|
|
|
* provided) or to the letter `T' which separates date and time in
|
|
|
|
* ISO 8601. The pointer arithmetic is the same for either case.
|
1999-01-22 19:38:39 +00:00
|
|
|
*/
|
|
|
|
switch (t - s) {
|
|
|
|
case 8:
|
|
|
|
tm.tm_year = ((ul / 1000000) - 19) * 100;
|
|
|
|
ul = ul % 1000000;
|
|
|
|
case 6:
|
|
|
|
tm.tm_year = tm.tm_year - (tm.tm_year % 100);
|
|
|
|
tm.tm_year += ul / 10000;
|
|
|
|
ul = ul % 10000;
|
|
|
|
case 4:
|
|
|
|
tm.tm_mon = (ul / 100) - 1;
|
|
|
|
ul = ul % 100;
|
|
|
|
case 2:
|
|
|
|
tm.tm_mday = ul;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (tm.tm_year < 70 || tm.tm_mon < 0 || tm.tm_mon > 12
|
|
|
|
|| tm.tm_mday < 1 || tm.tm_mday > 31)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (*t != '\0') {
|
|
|
|
s = ++t;
|
|
|
|
ul = strtoul(s, &t, 10);
|
|
|
|
if (*t != '\0' && !isspace(*t))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
switch (t - s) {
|
|
|
|
case 6:
|
|
|
|
tm.tm_sec = ul % 100;
|
|
|
|
ul /= 100;
|
|
|
|
case 4:
|
|
|
|
tm.tm_min = ul % 100;
|
|
|
|
ul /= 100;
|
|
|
|
case 2:
|
|
|
|
tm.tm_hour = ul;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (tm.tm_sec < 0 || tm.tm_sec > 60 || tm.tm_min < 0
|
|
|
|
|| tm.tm_min > 59 || tm.tm_hour < 0 || tm.tm_hour > 23)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return mktime(&tm);
|
|
|
|
}
|
2000-04-04 08:50:01 +00:00
|
|
|
|
|
|
|
/* physically move file */
|
|
|
|
static void
|
|
|
|
movefile(char *from, char *to, int perm, int owner_uid, int group_gid)
|
|
|
|
{
|
|
|
|
FILE *src, *dst;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if ((src = fopen(from, "r")) == NULL)
|
|
|
|
err(1, "can't fopen %s for reading", from);
|
|
|
|
if ((dst = fopen(to, "w")) == NULL)
|
|
|
|
err(1, "can't fopen %s for writing", to);
|
|
|
|
if (fchown(fileno(dst), owner_uid, group_gid))
|
|
|
|
err(1, "can't fchown %s", to);
|
|
|
|
if (fchmod(fileno(dst), perm))
|
|
|
|
err(1, "can't fchmod %s", to);
|
|
|
|
|
|
|
|
while ((c = getc(src)) != EOF) {
|
|
|
|
if ((putc(c, dst)) == EOF)
|
|
|
|
err(1, "error writing to %s", to);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ferror(src))
|
|
|
|
err(1, "error reading from %s", from);
|
|
|
|
if ((fclose(src)) != 0)
|
|
|
|
err(1, "can't fclose %s", to);
|
|
|
|
if ((fclose(dst)) != 0)
|
|
|
|
err(1, "can't fclose %s", from);
|
|
|
|
if ((unlink(from)) != 0)
|
|
|
|
err(1, "can't unlink %s", from);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create one or more directory components of a path */
|
|
|
|
static void
|
|
|
|
createdir(char *dirpart)
|
|
|
|
{
|
|
|
|
char *s, *d;
|
2001-01-20 17:36:08 +00:00
|
|
|
char mkdirpath[MAXPATHLEN];
|
2000-04-04 08:50:01 +00:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
s = dirpart;
|
|
|
|
d = mkdirpath;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
*d++ = *s++;
|
|
|
|
if (*s == '/' || *s == '\0') {
|
|
|
|
*d = '\0';
|
|
|
|
if (lstat(mkdirpath, &st))
|
|
|
|
mkdir(mkdirpath, 0755);
|
|
|
|
}
|
|
|
|
if (*s == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Parse a cyclic time specification, the format is as follows:
|
|
|
|
*
|
|
|
|
* [Dhh] or [Wd[Dhh]] or [Mdd[Dhh]]
|
|
|
|
*
|
|
|
|
* to rotate a logfile cyclic at
|
|
|
|
*
|
|
|
|
* - every day (D) within a specific hour (hh) (hh = 0...23)
|
|
|
|
* - once a week (W) at a specific day (d) OR (d = 0..6, 0 = Sunday)
|
|
|
|
* - once a month (M) at a specific day (d) (d = 1..31,l|L)
|
|
|
|
*
|
|
|
|
* We don't accept a timezone specification; missing fields
|
|
|
|
* are defaulted to the current date but time zero.
|
|
|
|
*/
|
|
|
|
static time_t
|
|
|
|
parseDWM(char *s)
|
|
|
|
{
|
|
|
|
char *t;
|
|
|
|
struct tm tm, *tmp;
|
|
|
|
u_long ul;
|
|
|
|
int nd;
|
|
|
|
static int mtab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|
|
|
int WMseen = 0;
|
|
|
|
int Dseen = 0;
|
|
|
|
|
|
|
|
tmp = localtime(&timenow);
|
|
|
|
tm = *tmp;
|
|
|
|
|
|
|
|
/* set no. of days per month */
|
|
|
|
|
|
|
|
nd = mtab[tm.tm_mon];
|
|
|
|
|
|
|
|
if (tm.tm_mon == 1) {
|
|
|
|
if (((tm.tm_year + 1900) % 4 == 0) &&
|
|
|
|
((tm.tm_year + 1900) % 100 != 0) &&
|
|
|
|
((tm.tm_year + 1900) % 400 == 0)) {
|
|
|
|
nd++; /* leap year, 29 days in february */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
switch (*s) {
|
|
|
|
case 'D':
|
|
|
|
if (Dseen)
|
|
|
|
return -1;
|
|
|
|
Dseen++;
|
|
|
|
s++;
|
|
|
|
ul = strtoul(s, &t, 10);
|
|
|
|
if (ul < 0 || ul > 23)
|
|
|
|
return -1;
|
|
|
|
tm.tm_hour = ul;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'W':
|
|
|
|
if (WMseen)
|
|
|
|
return -1;
|
|
|
|
WMseen++;
|
|
|
|
s++;
|
|
|
|
ul = strtoul(s, &t, 10);
|
|
|
|
if (ul < 0 || ul > 6)
|
|
|
|
return -1;
|
|
|
|
if (ul != tm.tm_wday) {
|
|
|
|
int save;
|
|
|
|
|
|
|
|
if (ul < tm.tm_wday) {
|
|
|
|
save = 6 - tm.tm_wday;
|
|
|
|
save += (ul + 1);
|
|
|
|
} else {
|
|
|
|
save = ul - tm.tm_wday;
|
|
|
|
}
|
|
|
|
|
|
|
|
tm.tm_mday += save;
|
|
|
|
|
|
|
|
if (tm.tm_mday > nd) {
|
|
|
|
tm.tm_mon++;
|
|
|
|
tm.tm_mday = tm.tm_mday - nd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'M':
|
|
|
|
if (WMseen)
|
|
|
|
return -1;
|
|
|
|
WMseen++;
|
|
|
|
s++;
|
|
|
|
if (tolower(*s) == 'l') {
|
|
|
|
tm.tm_mday = nd;
|
|
|
|
s++;
|
|
|
|
t = s;
|
|
|
|
} else {
|
|
|
|
ul = strtoul(s, &t, 10);
|
|
|
|
if (ul < 1 || ul > 31)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (ul > nd)
|
|
|
|
return -1;
|
|
|
|
tm.tm_mday = ul;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (-1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*t == '\0' || isspace(*t))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
s = t;
|
|
|
|
}
|
|
|
|
return mktime(&tm);
|
|
|
|
}
|