#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/usr.sbin/newsyslog/newsyslog.c,v 1.108 2008/01/30 22:11:59 delphij Exp $");
#define OSF
#ifndef COMPRESS_POSTFIX
#define COMPRESS_POSTFIX ".gz"
#endif
#ifndef BZCOMPRESS_POSTFIX
#define BZCOMPRESS_POSTFIX ".bz2"
#endif
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <glob.h>
#include <grp.h>
#include <paths.h>
#include <pwd.h>
#include <signal.h>
#ifdef __APPLE__
#include <spawn.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "pathnames.h"
#include "extern.h"
#define CE_COMPACT 0x0001
#define CE_BZCOMPACT 0x0002
#define CE_BINARY 0x0008
#define CE_NOSIGNAL 0x0010
#define CE_TRIMAT 0x0020
#define CE_GLOB 0x0040
#define CE_SIGNALGROUP 0x0080
#define CE_CREATE 0x0100
#define CE_NODUMP 0x0200
#define MIN_PID 5
#define MAX_PID 99999
#define kbytes(size) (((size) + 1023) >> 10)
#define DEFAULT_MARKER "<default>"
#define DEBUG_MARKER "<debug>"
struct conf_entry {
char *log;
char *pid_file;
char *r_reason;
int firstcreate;
int rotate;
int fsize;
uid_t uid;
gid_t gid;
int numlogs;
int trsize;
int hours;
struct ptime_data *trim_at;
unsigned int permissions;
int flags;
int sig;
int def_cfg;
struct conf_entry *next;
};
struct sigwork_entry {
SLIST_ENTRY(sigwork_entry) sw_nextp;
int sw_signum;
int sw_pidok;
pid_t sw_pid;
const char *sw_pidtype;
char sw_fname[1];
};
struct zipwork_entry {
SLIST_ENTRY(zipwork_entry) zw_nextp;
const struct conf_entry *zw_conf;
const struct sigwork_entry *zw_swork;
int zw_fsize;
char zw_fname[1];
};
typedef enum {
FREE_ENT, KEEP_ENT
} fk_entry;
SLIST_HEAD(swlisthead, sigwork_entry) swhead = SLIST_HEAD_INITIALIZER(swhead);
SLIST_HEAD(zwlisthead, zipwork_entry) zwhead = SLIST_HEAD_INITIALIZER(zwhead);
int dbg_at_times;
int archtodir = 0;
int createlogs;
int verbose = 0;
int needroot = 1;
int noaction = 0;
int norotate = 0;
int nosignal;
int force = 0;
int rotatereq = 0;
char *requestor;
char *archdirname;
char *destdir = NULL;
const char *conf;
struct ptime_data *dbg_timenow;
struct ptime_data *timenow;
#define DAYTIME_LEN 16
char daytime[DAYTIME_LEN];
char hostname[MAXHOSTNAMELEN];
static struct conf_entry *get_worklist(char **files);
static void parse_file(FILE *cf, const char *cfname, struct conf_entry **work_p,
struct conf_entry **glob_p, struct conf_entry **defconf_p);
static char *sob(char *p);
static char *son(char *p);
static int isnumberstr(const char *);
static char *missing_field(char *p, char *errline);
static void change_attrs(const char *, const struct conf_entry *);
static fk_entry do_entry(struct conf_entry *);
static fk_entry do_rotate(const struct conf_entry *);
static void do_sigwork(struct sigwork_entry *);
static void do_zipwork(struct zipwork_entry *);
static struct sigwork_entry *
save_sigwork(const struct conf_entry *);
static struct zipwork_entry *
save_zipwork(const struct conf_entry *, const struct
sigwork_entry *, int, const char *);
static void set_swpid(struct sigwork_entry *, const struct conf_entry *);
static int sizefile(const char *);
static void expand_globs(struct conf_entry **work_p,
struct conf_entry **glob_p);
static void free_clist(struct conf_entry **firstent);
static void free_entry(struct conf_entry *ent);
static struct conf_entry *init_entry(const char *fname,
struct conf_entry *src_entry);
static void parse_args(int argc, char **argv);
static int parse_doption(const char *doption);
static void usage(void);
static int log_trim(const char *logname, const struct conf_entry *log_ent);
static int age_old_log(char *file);
static void savelog(char *from, char *to);
static void createdir(const struct conf_entry *ent, char *dirpart);
static void createlog(const struct conf_entry *ent);
#define isdigitch(Anychar) isdigit((u_char)(Anychar))
#define isprintch(Anychar) isprint((u_char)(Anychar))
#define isspacech(Anychar) isspace((u_char)(Anychar))
#define tolowerch(Anychar) tolower((u_char)(Anychar))
int
main(int argc, char **argv)
{
fk_entry free_or_keep;
struct conf_entry *p, *q;
struct sigwork_entry *stmp;
struct zipwork_entry *ztmp;
SLIST_INIT(&swhead);
SLIST_INIT(&zwhead);
parse_args(argc, argv);
argc -= optind;
argv += optind;
if (needroot && getuid() && geteuid())
errx(1, "must have root privs");
p = q = get_worklist(argv);
while (p) {
free_or_keep = do_entry(p);
p = p->next;
if (free_or_keep == FREE_ENT)
free_entry(q);
q = p;
}
if (!SLIST_EMPTY(&swhead)) {
if (noaction || verbose)
printf("Signal all daemon process(es)...\n");
SLIST_FOREACH(stmp, &swhead, sw_nextp)
do_sigwork(stmp);
if (noaction)
printf("\tsleep 10\n");
else {
if (verbose)
printf("Pause 10 seconds to allow daemon(s)"
" to close log file(s)\n");
sleep(10);
}
}
if (!SLIST_EMPTY(&zwhead)) {
if (noaction || verbose)
printf("Compress all rotated log file(s)...\n");
while (!SLIST_EMPTY(&zwhead)) {
ztmp = SLIST_FIRST(&zwhead);
do_zipwork(ztmp);
SLIST_REMOVE_HEAD(&zwhead, zw_nextp);
free(ztmp);
}
}
while (!SLIST_EMPTY(&swhead)) {
stmp = SLIST_FIRST(&swhead);
SLIST_REMOVE_HEAD(&swhead, sw_nextp);
free(stmp);
}
while (wait(NULL) > 0 || errno == EINTR)
;
return (0);
}
static struct conf_entry *
init_entry(const char *fname, struct conf_entry *src_entry)
{
struct conf_entry *tempwork;
if (verbose > 4)
printf("\t--> [creating entry for %s]\n", fname);
tempwork = malloc(sizeof(struct conf_entry));
if (tempwork == NULL)
err(1, "malloc of conf_entry for %s", fname);
if (destdir == NULL || fname[0] != '/')
tempwork->log = strdup(fname);
else
asprintf(&tempwork->log, "%s%s", destdir, fname);
if (tempwork->log == NULL)
err(1, "strdup for %s", fname);
if (src_entry != NULL) {
tempwork->pid_file = NULL;
if (src_entry->pid_file)
tempwork->pid_file = strdup(src_entry->pid_file);
tempwork->r_reason = NULL;
tempwork->firstcreate = 0;
tempwork->rotate = 0;
tempwork->fsize = -1;
tempwork->uid = src_entry->uid;
tempwork->gid = src_entry->gid;
tempwork->numlogs = src_entry->numlogs;
tempwork->trsize = src_entry->trsize;
tempwork->hours = src_entry->hours;
tempwork->trim_at = NULL;
if (src_entry->trim_at != NULL)
tempwork->trim_at = ptime_init(src_entry->trim_at);
tempwork->permissions = src_entry->permissions;
tempwork->flags = src_entry->flags;
tempwork->sig = src_entry->sig;
tempwork->def_cfg = src_entry->def_cfg;
} else {
tempwork->pid_file = NULL;
tempwork->r_reason = NULL;
tempwork->firstcreate = 0;
tempwork->rotate = 0;
tempwork->fsize = -1;
tempwork->uid = (uid_t)-1;
tempwork->gid = (gid_t)-1;
tempwork->numlogs = 1;
tempwork->trsize = -1;
tempwork->hours = -1;
tempwork->trim_at = NULL;
tempwork->permissions = 0;
tempwork->flags = 0;
tempwork->sig = SIGHUP;
tempwork->def_cfg = 0;
}
tempwork->next = NULL;
return (tempwork);
}
static void
free_entry(struct conf_entry *ent)
{
if (ent == NULL)
return;
if (ent->log != NULL) {
if (verbose > 4)
printf("\t--> [freeing entry for %s]\n", ent->log);
free(ent->log);
ent->log = NULL;
}
if (ent->pid_file != NULL) {
free(ent->pid_file);
ent->pid_file = NULL;
}
if (ent->r_reason != NULL) {
free(ent->r_reason);
ent->r_reason = NULL;
}
if (ent->trim_at != NULL) {
ptime_free(ent->trim_at);
ent->trim_at = NULL;
}
free(ent);
}
static void
free_clist(struct conf_entry **firstent)
{
struct conf_entry *ent, *nextent;
if (firstent == NULL)
return;
ent = *firstent;
firstent = NULL;
while (ent) {
nextent = ent->next;
free_entry(ent);
ent = nextent;
}
}
static fk_entry
do_entry(struct conf_entry * ent)
{
#define REASON_MAX 80
int modtime;
fk_entry free_or_keep;
double diffsecs;
char temp_reason[REASON_MAX];
free_or_keep = FREE_ENT;
if (verbose) {
if (ent->flags & CE_COMPACT)
printf("%s <%dZ>: ", ent->log, ent->numlogs);
else if (ent->flags & CE_BZCOMPACT)
printf("%s <%dJ>: ", ent->log, ent->numlogs);
else
printf("%s <%d>: ", ent->log, ent->numlogs);
}
ent->fsize = sizefile(ent->log);
modtime = age_old_log(ent->log);
ent->rotate = 0;
ent->firstcreate = 0;
if (ent->fsize < 0) {
temp_reason[0] = '\0';
if (createlogs > 1)
ent->firstcreate = 1;
else if ((ent->flags & CE_CREATE) && createlogs)
ent->firstcreate = 1;
else if (ent->flags & CE_CREATE)
strlcpy(temp_reason, " (no -C option)", REASON_MAX);
else if (createlogs)
strlcpy(temp_reason, " (no C flag)", REASON_MAX);
if (ent->firstcreate) {
if (verbose)
printf("does not exist -> will create.\n");
createlog(ent);
} else if (verbose) {
printf("does not exist, skipped%s.\n", temp_reason);
}
} else {
if (ent->flags & CE_TRIMAT && !force && !rotatereq) {
diffsecs = ptimeget_diff(timenow, ent->trim_at);
if (diffsecs < 0.0) {
if (verbose) {
ptime_adjust4dst(ent->trim_at,
timenow);
printf("--> will trim at %s",
ptimeget_ctime(ent->trim_at));
}
return (free_or_keep);
} else if (diffsecs >= 3600.0) {
if (verbose && dbg_at_times)
printf("\n\t--> prev trim at %s\t",
ptimeget_ctime(ent->trim_at));
if (verbose) {
ptimeset_nxtime(ent->trim_at);
printf("--> will trim at %s",
ptimeget_ctime(ent->trim_at));
}
return (free_or_keep);
} else if (verbose && noaction && dbg_at_times) {
printf("\n\t--> timematch at %s",
ptimeget_ctime(ent->trim_at));
return (free_or_keep);
} else if (verbose && ent->hours <= 0) {
printf("--> time is up\n");
}
}
if (verbose && (ent->trsize > 0))
printf("size (Kb): %d [%d] ", ent->fsize, ent->trsize);
if (verbose && (ent->hours > 0))
printf(" age (hr): %d [%d] ", modtime, ent->hours);
temp_reason[0] = '\0';
if (rotatereq) {
ent->rotate = 1;
snprintf(temp_reason, REASON_MAX, " due to -R from %s",
requestor);
} else if (force) {
ent->rotate = 1;
snprintf(temp_reason, REASON_MAX, " due to -F request");
} else if ((ent->trsize > 0) && (ent->fsize >= ent->trsize)) {
ent->rotate = 1;
snprintf(temp_reason, REASON_MAX, " due to size>%dK",
ent->trsize);
} else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) {
ent->rotate = 1;
} else if ((ent->hours > 0) && ((modtime >= ent->hours) ||
(modtime < 0))) {
ent->rotate = 1;
}
if (ent->rotate && !norotate) {
if (temp_reason[0] != '\0')
ent->r_reason = strdup(temp_reason);
if (verbose)
printf("--> trimming log....\n");
if (noaction && !verbose) {
if (ent->flags & CE_COMPACT)
printf("%s <%dZ>: trimming\n",
ent->log, ent->numlogs);
else if (ent->flags & CE_BZCOMPACT)
printf("%s <%dJ>: trimming\n",
ent->log, ent->numlogs);
else
printf("%s <%d>: trimming\n",
ent->log, ent->numlogs);
}
free_or_keep = do_rotate(ent);
} else {
if (verbose)
printf("--> skipping\n");
}
}
return (free_or_keep);
#undef REASON_MAX
}
static void
parse_args(int argc, char **argv)
{
int ch;
char *p;
timenow = ptime_init(NULL);
ptimeset_time(timenow, time(NULL));
strlcpy(daytime, ptimeget_ctime(timenow) + 4, DAYTIME_LEN);
#ifdef __APPLE__
if (ptimeget_secs(timenow) == 0) {
fprintf(stderr, "Time isn't set, exiting.\n");
exit(1);
}
#endif
(void)gethostname(hostname, sizeof(hostname));
if ((p = strchr(hostname, '.')) != NULL)
*p = '\0';
while ((ch = getopt(argc, argv, "a:d:f:nrsvCD:FNR:")) != -1)
switch (ch) {
case 'a':
archtodir++;
archdirname = optarg;
break;
case 'd':
destdir = optarg;
break;
case 'f':
conf = optarg;
break;
case 'n':
noaction++;
break;
case 'r':
needroot = 0;
break;
case 's':
nosignal = 1;
break;
case 'v':
verbose++;
break;
case 'C':
createlogs++;
break;
case 'D':
if (parse_doption(optarg))
break;
usage();
case 'F':
force++;
break;
case 'N':
norotate++;
break;
case 'R':
rotatereq++;
requestor = strdup(optarg);
break;
case 'm':
default:
usage();
}
if (force && norotate) {
warnx("Only one of -F and -N may be specified.");
usage();
}
if (rotatereq) {
if (optind == argc) {
warnx("At least one filename must be given when -R is specified.");
usage();
}
for (p = requestor; *p != '\0'; p++) {
if (!isprintch(*p) && (*p != '\t'))
*p = '.';
}
}
if (dbg_timenow) {
ptime_free(timenow);
timenow = dbg_timenow;
fprintf(stderr, "Debug: Running as if TimeNow is %s",
ptimeget_ctime(dbg_timenow));
}
}
static int
parse_doption(const char *doption)
{
const char TN[] = "TN=";
int res;
if (strncmp(doption, TN, sizeof(TN) - 1) == 0) {
dbg_timenow = ptime_init(NULL);
res = ptime_relparse(dbg_timenow, PTM_PARSE_ISO8601,
time(NULL), doption + sizeof(TN) - 1);
if (res == -2) {
warnx("Non-existent time specified on -D %s", doption);
return (0);
} else if (res < 0) {
warnx("Malformed time given on -D %s", doption);
return (0);
}
return (1);
}
if (strcmp(doption, "ats") == 0) {
dbg_at_times++;
return (1);
}
if ((strcmp(doption, "neworder") == 0) || (strcmp(doption, "oldorder")
== 0)) {
warnx("NOTE: newsyslog always uses 'neworder'.");
return (1);
}
warnx("Unknown -D (debug) option: '%s'", doption);
return (0);
}
static void
usage(void)
{
fprintf(stderr,
"usage: newsyslog [-CFNnrsv] [-a directory] [-d directory] [-f config-file]\n"
" [ [-R requestor] filename ... ]\n");
exit(1);
}
static struct conf_entry *
get_worklist(char **files)
{
FILE *f;
const char *fname;
char **given;
struct conf_entry *defconf, *dupent, *ent, *firstnew;
struct conf_entry *globlist, *lastnew, *worklist;
int gmatch, fnres;
defconf = globlist = worklist = NULL;
fname = conf;
if (fname == NULL)
fname = _PATH_CONF;
if (strcmp(fname, "-") != 0)
f = fopen(fname, "r");
else {
f = stdin;
fname = "<stdin>";
}
if (!f)
err(1, "%s", fname);
parse_file(f, fname, &worklist, &globlist, &defconf);
(void) fclose(f);
#ifdef __APPLE__
if (conf == NULL) {
glob_t g;
int i;
if (glob("/etc/newsyslog.d/*.conf", GLOB_NOCHECK, NULL, &g) != 0)
err(1, "glob");
for (i = 0; i < g.gl_matchc; i++) {
fname = g.gl_pathv[i];
f = fopen(fname, "r");
if (!f)
err(1, "%s", fname);
parse_file(f, fname, &worklist, &globlist, &defconf);
(void) fclose(f);
}
globfree(&g);
}
#endif
if (*files == NULL) {
expand_globs(&worklist, &globlist);
free_clist(&globlist);
if (defconf != NULL)
free_entry(defconf);
return (worklist);
}
if (defconf == NULL) {
defconf = init_entry(DEFAULT_MARKER, NULL);
defconf->numlogs = 3;
defconf->trsize = 50;
defconf->permissions = S_IRUSR|S_IWUSR;
}
firstnew = lastnew = NULL;
for (given = files; *given; ++given) {
gmatch = 0;
for (ent = worklist; ent; ent = ent->next) {
if (strcmp(ent->log, *given) == 0) {
gmatch++;
dupent = init_entry(*given, ent);
if (!firstnew)
firstnew = dupent;
else
lastnew->next = dupent;
lastnew = dupent;
}
}
if (gmatch) {
if (verbose > 2)
printf("\t+ Matched entry %s\n", *given);
continue;
}
gmatch = 0;
if (verbose > 2 && globlist != NULL)
printf("\t+ Checking globs for %s\n", *given);
for (ent = globlist; ent; ent = ent->next) {
fnres = fnmatch(ent->log, *given, FNM_PATHNAME);
if (verbose > 2)
printf("\t+ = %d for pattern %s\n", fnres,
ent->log);
if (fnres == 0) {
gmatch++;
dupent = init_entry(*given, ent);
if (!firstnew)
firstnew = dupent;
else
lastnew->next = dupent;
lastnew = dupent;
dupent->flags &= ~CE_GLOB;
break;
}
}
if (gmatch) {
if (verbose > 2)
printf("\t+ Matched %s via %s\n", *given,
ent->log);
continue;
}
if (verbose > 2)
printf("\t+ No entry matched %s (will use %s)\n",
*given, DEFAULT_MARKER);
dupent = init_entry(*given, defconf);
if (!firstnew)
firstnew = dupent;
else
lastnew->next = dupent;
dupent->def_cfg = 1;
lastnew = dupent;
}
free_clist(&worklist);
free_clist(&globlist);
free_entry(defconf);
return (firstnew);
}
static void
expand_globs(struct conf_entry **work_p, struct conf_entry **glob_p)
{
int gmatch, gres;
size_t i;
char *mfname;
struct conf_entry *dupent, *ent, *firstmatch, *globent;
struct conf_entry *lastmatch;
glob_t pglob;
struct stat st_fm;
if ((glob_p == NULL) || (*glob_p == NULL))
return;
firstmatch = lastmatch = NULL;
for (globent = *glob_p; globent; globent = globent->next) {
gres = glob(globent->log, GLOB_NOCHECK, NULL, &pglob);
if (gres != 0) {
warn("cannot expand pattern (%d): %s", gres,
globent->log);
continue;
}
if (verbose > 2)
printf("\t+ Expanding pattern %s\n", globent->log);
for (i = 0; i < (size_t)pglob.gl_matchc; i++) {
mfname = pglob.gl_pathv[i];
gmatch = 0;
for (ent = *work_p; ent; ent = ent->next) {
if (strcmp(mfname, ent->log) == 0) {
gmatch++;
break;
}
}
if (gmatch)
continue;
gres = lstat(mfname, &st_fm);
if (gres != 0) {
warn("Skipping %s - lstat() error", mfname);
continue;
}
if (!S_ISREG(st_fm.st_mode)) {
if (verbose > 2)
printf("\t+ . skipping %s (!file)\n",
mfname);
continue;
}
if (verbose > 2)
printf("\t+ . add file %s\n", mfname);
dupent = init_entry(mfname, globent);
if (!firstmatch)
firstmatch = dupent;
else
lastmatch->next = dupent;
lastmatch = dupent;
dupent->flags &= ~CE_GLOB;
}
globfree(&pglob);
if (verbose > 2)
printf("\t+ Done with pattern %s\n", globent->log);
}
if (!*work_p)
*work_p = firstmatch;
else {
ent = *work_p;
while (ent->next)
ent = ent->next;
ent->next = firstmatch;
}
}
static void
parse_file(FILE *cf, const char *cfname, struct conf_entry **work_p,
struct conf_entry **glob_p, struct conf_entry **defconf_p)
{
char line[BUFSIZ], *parse, *q;
char *cp, *errline, *group;
struct conf_entry *lastglob, *lastwork, *working;
struct passwd *pwd;
struct group *grp;
int eol, ptm_opts, res, special;
lastglob = lastwork = NULL;
#ifdef __APPLE__
if (*glob_p) {
lastglob = *glob_p;
while (lastglob->next)
lastglob = lastglob->next;
}
if (*work_p) {
lastwork = *work_p;
while (lastwork->next)
lastwork = lastwork->next;
}
#endif
errline = NULL;
while (fgets(line, BUFSIZ, cf)) {
if ((line[0] == '\n') || (line[0] == '#') ||
(strlen(line) == 0))
continue;
if (errline != NULL)
free(errline);
errline = strdup(line);
for (cp = line + 1; *cp != '\0'; cp++) {
if (*cp != '#')
continue;
if (*(cp - 1) == '\\') {
strcpy(cp - 1, cp);
cp--;
continue;
}
*cp = '\0';
break;
}
q = parse = missing_field(sob(line), errline);
parse = son(line);
if (!*parse)
errx(1, "malformed line (missing fields):\n%s",
errline);
*parse = '\0';
if (strcasecmp(DEBUG_MARKER, q) == 0) {
q = parse = missing_field(sob(++parse), errline);
parse = son(parse);
if (!*parse)
warnx("debug line specifies no option:\n%s",
errline);
else {
*parse = '\0';
parse_doption(q);
}
continue;
}
special = 0;
working = init_entry(q, NULL);
if (strcasecmp(DEFAULT_MARKER, q) == 0) {
special = 1;
if (defconf_p == NULL) {
warnx("Ignoring entry for %s in %s!", q,
cfname);
free_entry(working);
continue;
} else if (*defconf_p != NULL) {
warnx("Ignoring duplicate entry for %s!", q);
free_entry(working);
continue;
}
*defconf_p = working;
}
q = parse = missing_field(sob(++parse), errline);
parse = son(parse);
if (!*parse)
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 (!(isnumberstr(q))) {
if ((pwd = getpwnam(q)) == NULL)
errx(1,
"error in config file; unknown user:\n%s",
errline);
working->uid = pwd->pw_uid;
} else
working->uid = atoi(q);
} else
working->uid = (uid_t)-1;
q = group;
if (*q) {
if (!(isnumberstr(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 = (gid_t)-1;
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 = (uid_t)-1;
working->gid = (gid_t)-1;
}
#ifdef __APPLE__
if (working->gid == (gid_t)-1) {
working->gid = (gid_t)80;
}
#endif
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);
if (!*parse)
errx(1, "malformed line (missing fields):\n%s",
errline);
*parse = '\0';
if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0)
errx(1, "error in config file; bad value for count of logs to save:\n%s",
errline);
q = parse = missing_field(sob(++parse), errline);
parse = son(parse);
if (!*parse)
errx(1, "malformed line (missing fields):\n%s",
errline);
*parse = '\0';
if (isdigitch(*q))
working->trsize = atoi(q);
else if (strcmp(q, "*") == 0)
working->trsize = -1;
else {
warnx("Invalid value of '%s' for 'size' in line:\n%s",
q, errline);
working->trsize = -1;
}
working->flags = 0;
q = parse = missing_field(sob(++parse), errline);
parse = son(parse);
eol = !*parse;
*parse = '\0';
{
char *ep;
u_long ul;
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;
if (*ep == '\0' || strcmp(ep, "*") == 0)
goto no_trimat;
if (*ep != '@' && *ep != '$')
errx(1, "malformed interval/at:\n%s", errline);
working->flags |= CE_TRIMAT;
working->trim_at = ptime_init(NULL);
ptm_opts = PTM_PARSE_ISO8601;
if (*ep == '$')
ptm_opts = PTM_PARSE_DWM;
ptm_opts |= PTM_PARSE_MATCHDOM;
res = ptime_relparse(working->trim_at, ptm_opts,
ptimeget_secs(timenow), ep + 1);
if (res == -2)
errx(1, "nonexistent time for 'at' value:\n%s",
errline);
else if (res < 0)
errx(1, "malformed 'at' value:\n%s", errline);
}
no_trimat:
if (eol)
q = NULL;
else {
q = parse = sob(++parse);
parse = son(parse);
if (!*parse)
eol = 1;
*parse = '\0';
}
for (; q && *q && !isspacech(*q); q++) {
switch (tolowerch(*q)) {
case 'b':
working->flags |= CE_BINARY;
break;
case 'c':
if (*q == 'c') {
warnx("Assuming 'g' for 'c' in flags for line:\n%s",
errline);
warnx("The 'c' flag will eventually mean 'CREATE'");
working->flags |= CE_GLOB;
break;
}
working->flags |= CE_CREATE;
break;
case 'd':
working->flags |= CE_NODUMP;
break;
case 'g':
working->flags |= CE_GLOB;
break;
case 'j':
working->flags |= CE_BZCOMPACT;
break;
case 'n':
working->flags |= CE_NOSIGNAL;
break;
case 'u':
working->flags |= CE_SIGNALGROUP;
break;
case 'w':
break;
case 'z':
working->flags |= CE_COMPACT;
break;
case '-':
break;
case 'f':
case 'm':
case 'p':
default:
errx(1, "illegal flag in config file -- %c",
*q);
}
}
if (eol)
q = NULL;
else {
q = parse = sob(++parse);
parse = son(parse);
if (!*parse)
eol = 1;
*parse = '\0';
}
working->pid_file = NULL;
if (q && *q) {
if (*q == '/')
working->pid_file = strdup(q);
else if (isdigit(*q))
goto got_sig;
else
errx(1,
"illegal pid file or signal number in config file:\n%s",
errline);
}
if (eol)
q = NULL;
else {
q = parse = sob(++parse);
*(parse = son(parse)) = '\0';
}
working->sig = SIGHUP;
if (q && *q) {
if (isdigit(*q)) {
got_sig:
working->sig = atoi(q);
} else {
err_sig:
errx(1,
"illegal signal number in config file:\n%s",
errline);
}
if (working->sig < 1 || working->sig >= NSIG)
goto err_sig;
}
if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) {
if (working->pid_file != NULL) {
warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s",
working->pid_file, errline);
free(working->pid_file);
working->pid_file = NULL;
}
} else if (working->pid_file == NULL) {
if (working->flags & CE_SIGNALGROUP) {
warnx("Ignoring flag 'U' in line:\n%s",
errline);
working->flags &= ~CE_SIGNALGROUP;
}
if (needroot)
working->pid_file = strdup(_PATH_SYSLOGPID);
}
if (special) {
;
} else if (working->flags & CE_GLOB) {
if (!*glob_p)
*glob_p = working;
else
lastglob->next = working;
lastglob = working;
} else {
if (!*work_p)
*work_p = working;
else
lastwork->next = working;
lastwork = working;
}
}
if (errline != NULL)
free(errline);
}
static char *
missing_field(char *p, char *errline)
{
if (!p || !*p)
errx(1, "missing field in config file:\n%s", errline);
return (p);
}
static fk_entry
do_rotate(const struct conf_entry *ent)
{
char dirpart[MAXPATHLEN], namepart[MAXPATHLEN];
char file1[MAXPATHLEN], file2[MAXPATHLEN];
char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
char jfile1[MAXPATHLEN];
int flags, numlogs_c;
fk_entry free_or_keep;
struct sigwork_entry *swork;
struct stat st;
flags = ent->flags;
free_or_keep = FREE_ENT;
if (archtodir) {
char *p;
if (*archdirname == '/') {
strlcpy(dirpart, archdirname, sizeof(dirpart));
} else {
strlcpy(dirpart, ent->log, sizeof(dirpart));
if ((p = rindex(dirpart, '/')) == NULL)
dirpart[0] = '\0';
else
*(p + 1) = '\0';
strlcat(dirpart, archdirname, sizeof(dirpart));
}
if (lstat(dirpart, &st))
createdir(ent, dirpart);
if ((p = rindex(ent->log, '/')) == NULL)
strlcpy(namepart, ent->log, sizeof(namepart));
else
strlcpy(namepart, p + 1, sizeof(namepart));
(void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart,
namepart, ent->numlogs);
(void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
COMPRESS_POSTFIX);
snprintf(jfile1, sizeof(jfile1), "%s%s", file1,
BZCOMPRESS_POSTFIX);
} else {
(void) snprintf(file1, sizeof(file1), "%s.%d", ent->log,
ent->numlogs);
(void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
COMPRESS_POSTFIX);
snprintf(jfile1, sizeof(jfile1), "%s%s", file1,
BZCOMPRESS_POSTFIX);
}
if (noaction) {
printf("\trm -f %s\n", file1);
printf("\trm -f %s\n", zfile1);
printf("\trm -f %s\n", jfile1);
} else {
(void) unlink(file1);
(void) unlink(zfile1);
(void) unlink(jfile1);
}
numlogs_c = ent->numlogs;
while (numlogs_c--) {
(void) strlcpy(file2, file1, sizeof(file2));
if (archtodir)
(void) snprintf(file1, sizeof(file1), "%s/%s.%d",
dirpart, namepart, numlogs_c);
else
(void) snprintf(file1, sizeof(file1), "%s.%d",
ent->log, numlogs_c);
(void) strlcpy(zfile1, file1, sizeof(zfile1));
(void) strlcpy(zfile2, file2, sizeof(zfile2));
if (lstat(file1, &st)) {
(void) strlcat(zfile1, COMPRESS_POSTFIX,
sizeof(zfile1));
(void) strlcat(zfile2, COMPRESS_POSTFIX,
sizeof(zfile2));
if (lstat(zfile1, &st)) {
strlcpy(zfile1, file1, sizeof(zfile1));
strlcpy(zfile2, file2, sizeof(zfile2));
strlcat(zfile1, BZCOMPRESS_POSTFIX,
sizeof(zfile1));
strlcat(zfile2, BZCOMPRESS_POSTFIX,
sizeof(zfile2));
if (lstat(zfile1, &st))
continue;
}
}
if (noaction)
printf("\tmv %s %s\n", zfile1, zfile2);
else {
(void)rename(zfile1, zfile2);
}
change_attrs(zfile2, ent);
}
if (ent->numlogs > 0) {
if (noaction) {
if (archtodir)
printf("\tcp %s %s\n", ent->log, file1);
else
printf("\tln %s %s\n", ent->log, file1);
} else {
if (!(flags & CE_BINARY)) {
log_trim(ent->log, ent);
}
savelog(ent->log, file1);
}
change_attrs(file1, ent);
}
if (noaction)
printf("Start new log...\n");
createlog(ent);
swork = NULL;
if (ent->pid_file != NULL)
swork = save_sigwork(ent);
if (ent->numlogs > 0 && (flags & (CE_COMPACT | CE_BZCOMPACT))) {
free_or_keep = KEEP_ENT;
save_zipwork(ent, swork, ent->fsize, file1);
}
return (free_or_keep);
}
static void
do_sigwork(struct sigwork_entry *swork)
{
struct sigwork_entry *nextsig;
int kres, secs;
if (!(swork->sw_pidok) || swork->sw_pid == 0)
return;
if (nosignal) {
if (!rotatereq)
swork->sw_pidok = -1;
return;
}
secs = 0;
nextsig = SLIST_NEXT(swork, sw_nextp);
if (nextsig != NULL) {
if (swork->sw_pid == nextsig->sw_pid)
secs = 10;
else
secs = 1;
}
if (noaction) {
printf("\tkill -%d %d \t\t# %s\n", swork->sw_signum,
(int)swork->sw_pid, swork->sw_fname);
if (secs > 0)
printf("\tsleep %d\n", secs);
return;
}
kres = kill(swork->sw_pid, swork->sw_signum);
if (kres != 0) {
if (errno != ESRCH)
swork->sw_pidok = 0;
warn("can't notify %s, pid %d", swork->sw_pidtype,
(int)swork->sw_pid);
} else {
if (verbose)
printf("Notified %s pid %d = %s\n", swork->sw_pidtype,
(int)swork->sw_pid, swork->sw_fname);
if (secs > 0) {
if (verbose)
printf("Pause %d second(s) between signals\n",
secs);
sleep(secs);
}
}
}
static void
do_zipwork(struct zipwork_entry *zwork)
{
const char *pgm_name, *pgm_path;
#ifdef __APPLE__
int errsav, zstatus;
#else
int errsav, fcount, zstatus;
#endif
pid_t pidzip, wpid;
char zresult[MAXPATHLEN];
pgm_path = NULL;
strlcpy(zresult, zwork->zw_fname, sizeof(zresult));
if (zwork != NULL && zwork->zw_conf != NULL) {
if (zwork->zw_conf->flags & CE_COMPACT) {
pgm_path = _PATH_GZIP;
strlcat(zresult, COMPRESS_POSTFIX, sizeof(zresult));
} else if (zwork->zw_conf->flags & CE_BZCOMPACT) {
pgm_path = _PATH_BZIP2;
strlcat(zresult, BZCOMPRESS_POSTFIX, sizeof(zresult));
}
}
if (pgm_path == NULL) {
warnx("invalid entry for %s in do_zipwork", zwork->zw_fname);
return;
}
pgm_name = strrchr(pgm_path, '/');
if (pgm_name == NULL)
pgm_name = pgm_path;
else
pgm_name++;
if (zwork->zw_swork != NULL && zwork->zw_swork->sw_pidok <= 0) {
warnx(
"log %s not compressed because daemon(s) not notified",
zwork->zw_fname);
change_attrs(zwork->zw_fname, zwork->zw_conf);
return;
}
if (noaction) {
printf("\t%s %s\n", pgm_name, zwork->zw_fname);
change_attrs(zresult, zwork->zw_conf);
return;
}
#ifdef __APPLE__
extern char **environ;
const char *args[] = { pgm_path, "-f", zwork->zw_fname, NULL };
if ((errsav = posix_spawn(&pidzip, pgm_path, NULL, NULL, (char * const *)args, environ)) != 0)
errc(1, errsav, "posix_spawn(`%s -f %s')", pgm_path, zwork->zw_fname);
#else
fcount = 1;
pidzip = fork();
while (pidzip < 0) {
errsav = errno;
warn("fork() for `%s %s'", pgm_name, zwork->zw_fname);
if (errsav != EAGAIN || fcount > 5)
errx(1, "Exiting...");
sleep(fcount * 12);
fcount++;
pidzip = fork();
}
if (!pidzip) {
execl(pgm_path, pgm_path, "-f", zwork->zw_fname, (char *)0);
err(1, "execl(`%s -f %s')", pgm_path, zwork->zw_fname);
}
#endif
wpid = waitpid(pidzip, &zstatus, 0);
if (wpid == -1) {
warn("%s: waitpid(%d)", pgm_path, pidzip);
return;
}
if (!WIFEXITED(zstatus)) {
warnx("`%s -f %s' did not terminate normally", pgm_name,
zwork->zw_fname);
return;
}
if (WEXITSTATUS(zstatus)) {
warnx("`%s -f %s' terminated with a non-zero status (%d)",
pgm_name, zwork->zw_fname, WEXITSTATUS(zstatus));
return;
}
change_attrs(zresult, zwork->zw_conf);
}
static struct sigwork_entry *
save_sigwork(const struct conf_entry *ent)
{
struct sigwork_entry *sprev, *stmp;
int ndiff;
size_t tmpsiz;
sprev = NULL;
ndiff = 1;
SLIST_FOREACH(stmp, &swhead, sw_nextp) {
ndiff = strcmp(ent->pid_file, stmp->sw_fname);
if (ndiff > 0)
break;
if (ndiff == 0) {
if (ent->sig == stmp->sw_signum)
break;
if (ent->sig > stmp->sw_signum) {
ndiff = 1;
break;
}
}
sprev = stmp;
}
if (stmp != NULL && ndiff == 0)
return (stmp);
tmpsiz = sizeof(struct sigwork_entry) + strlen(ent->pid_file) + 1;
stmp = malloc(tmpsiz);
set_swpid(stmp, ent);
stmp->sw_signum = ent->sig;
strcpy(stmp->sw_fname, ent->pid_file);
if (sprev == NULL)
SLIST_INSERT_HEAD(&swhead, stmp, sw_nextp);
else
SLIST_INSERT_AFTER(sprev, stmp, sw_nextp);
return (stmp);
}
static struct zipwork_entry *
save_zipwork(const struct conf_entry *ent, const struct sigwork_entry *swork,
int zsize, const char *zipfname)
{
struct zipwork_entry *zprev, *ztmp;
int ndiff;
size_t tmpsiz;
if (zsize < 0)
zsize = sizefile(zipfname);
zprev = NULL;
ndiff = 1;
SLIST_FOREACH(ztmp, &zwhead, zw_nextp) {
ndiff = strcmp(zipfname, ztmp->zw_fname);
if (ndiff == 0)
break;
if (zsize > ztmp->zw_fsize)
zprev = ztmp;
}
if (ztmp != NULL && ndiff == 0)
return (ztmp);
tmpsiz = sizeof(struct zipwork_entry) + strlen(zipfname) + 1;
ztmp = malloc(tmpsiz);
ztmp->zw_conf = ent;
ztmp->zw_swork = swork;
ztmp->zw_fsize = zsize;
strcpy(ztmp->zw_fname, zipfname);
if (zprev == NULL)
SLIST_INSERT_HEAD(&zwhead, ztmp, zw_nextp);
else
SLIST_INSERT_AFTER(zprev, ztmp, zw_nextp);
return (ztmp);
}
static void
set_swpid(struct sigwork_entry *swork, const struct conf_entry *ent)
{
FILE *f;
long minok, maxok, rval;
char *endp, *linep, line[BUFSIZ];
minok = MIN_PID;
maxok = MAX_PID;
swork->sw_pidok = 0;
swork->sw_pid = 0;
swork->sw_pidtype = "daemon";
if (ent->flags & CE_SIGNALGROUP) {
minok = -MAX_PID;
maxok = -MIN_PID;
swork->sw_pidtype = "process-group";
}
f = fopen(ent->pid_file, "r");
if (f == NULL) {
warn("can't open pid file: %s", ent->pid_file);
return;
}
if (fgets(line, BUFSIZ, f) == NULL) {
if (feof(f)) {
swork->sw_pidok = 1;
warnx("pid file is empty: %s", ent->pid_file);
} else
warn("can't read from pid file: %s", ent->pid_file);
(void)fclose(f);
return;
}
(void)fclose(f);
errno = 0;
linep = line;
while (*linep == ' ')
linep++;
rval = strtol(linep, &endp, 10);
if (*endp != '\0' && !isspacech(*endp)) {
warnx("pid file does not start with a valid number: %s",
ent->pid_file);
} else if (rval < minok || rval > maxok) {
warnx("bad value '%ld' for process number in %s",
rval, ent->pid_file);
if (verbose)
warnx("\t(expecting value between %ld and %ld)",
minok, maxok);
} else {
swork->sw_pidok = 1;
swork->sw_pid = rval;
}
return;
}
static int
log_trim(const char *logname, const struct conf_entry *log_ent)
{
FILE *f;
const char *xtra;
if ((f = fopen(logname, "a")) == NULL)
return (-1);
xtra = "";
if (log_ent->def_cfg)
xtra = " using <default> rule";
if (log_ent->firstcreate)
fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n",
daytime, hostname, (int) getpid(), xtra);
else if (log_ent->r_reason != NULL)
fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n",
daytime, hostname, (int) getpid(), log_ent->r_reason, xtra);
else
fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n",
daytime, hostname, (int) getpid(), xtra);
if (fclose(f) == EOF)
err(1, "log_trim: fclose");
return (0);
}
static int
sizefile(const char *file)
{
struct stat sb;
if (stat(file, &sb) < 0)
return (-1);
#ifdef __APPLE__
return (kbytes(dbtob(sb.st_blocks, DEV_BSIZE)));
#else
return (kbytes(dbtob(sb.st_blocks)));
#endif
}
static int
age_old_log(char *file)
{
struct stat sb;
char *endp;
char tmp[MAXPATHLEN + sizeof(".0") + sizeof(COMPRESS_POSTFIX) +
sizeof(BZCOMPRESS_POSTFIX) + 1];
if (archtodir) {
char *p;
if (*archdirname == '/') {
strlcpy(tmp, archdirname, sizeof(tmp));
} else {
strlcpy(tmp, file, sizeof(tmp));
if ((p = rindex(tmp, '/')) == NULL)
tmp[0] = '\0';
else
*(p + 1) = '\0';
strlcat(tmp, archdirname, sizeof(tmp));
}
strlcat(tmp, "/", sizeof(tmp));
if ((p = rindex(file, '/')) == NULL)
strlcat(tmp, file, sizeof(tmp));
else
strlcat(tmp, p + 1, sizeof(tmp));
} else {
(void) strlcpy(tmp, file, sizeof(tmp));
}
strlcat(tmp, ".0", sizeof(tmp));
if (stat(tmp, &sb) < 0) {
endp = strchr(tmp, '\0');
strlcat(tmp, COMPRESS_POSTFIX, sizeof(tmp));
if (stat(tmp, &sb) < 0) {
*endp = '\0';
strlcat(tmp, BZCOMPRESS_POSTFIX, sizeof(tmp));
if (stat(tmp, &sb) < 0)
return (-1);
}
}
return ((int)(ptimeget_secs(timenow) - sb.st_mtime + 1800) / 3600);
}
static char *
sob(char *p)
{
while (p && *p && isspace(*p))
p++;
return (p);
}
static char *
son(char *p)
{
while (p && *p && !isspace(*p))
p++;
return (p);
}
static int
isnumberstr(const char *string)
{
while (*string) {
if (!isdigitch(*string++))
return (0);
}
return (1);
}
static void
savelog(char *from, char *to)
{
FILE *src, *dst;
int c, res;
res = link(from, to);
if (res == 0)
return;
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);
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);
}
static void
createdir(const struct conf_entry *ent, char *dirpart)
{
int res;
char *s, *d;
char mkdirpath[MAXPATHLEN];
struct stat st;
s = dirpart;
d = mkdirpath;
for (;;) {
*d++ = *s++;
if (*s != '/' && *s != '\0')
continue;
*d = '\0';
res = lstat(mkdirpath, &st);
if (res != 0) {
if (noaction) {
printf("\tmkdir %s\n", mkdirpath);
} else {
res = mkdir(mkdirpath, 0755);
if (res != 0)
err(1, "Error on mkdir(\"%s\") for -a",
mkdirpath);
}
}
if (*s == '\0')
break;
}
if (verbose) {
if (ent->firstcreate)
printf("Created directory '%s' for new %s\n",
dirpart, ent->log);
else
printf("Created directory '%s' for -a\n", dirpart);
}
}
void
createlog(const struct conf_entry *ent)
{
int fd, failed;
struct stat st;
char *realfile, *slash, tempfile[MAXPATHLEN];
fd = -1;
realfile = ent->log;
if (ent->firstcreate) {
strlcpy(tempfile, realfile, sizeof(tempfile));
slash = strrchr(tempfile, '/');
if (slash != NULL) {
*slash = '\0';
failed = stat(tempfile, &st);
if (failed && errno != ENOENT)
err(1, "Error on stat(%s)", tempfile);
if (failed)
createdir(ent, tempfile);
else if (!S_ISDIR(st.st_mode))
errx(1, "%s exists but is not a directory",
tempfile);
}
}
strlcpy(tempfile, realfile, sizeof(tempfile));
strlcat(tempfile, ".zXXXXXX", sizeof(tempfile));
if (noaction)
printf("\tmktemp %s\n", tempfile);
else {
fd = mkstemp(tempfile);
if (fd < 0)
err(1, "can't mkstemp logfile %s", tempfile);
if (!(ent->flags & CE_BINARY)) {
if (log_trim(tempfile, ent))
err(1, "can't add status message to log");
}
}
if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) {
if (noaction)
printf("\tchown %u:%u %s\n", ent->uid, ent->gid,
tempfile);
else {
failed = fchown(fd, ent->uid, ent->gid);
if (failed)
err(1, "can't fchown temp file %s", tempfile);
}
}
if (ent->flags & CE_NODUMP) {
if (noaction)
printf("\tchflags nodump %s\n", tempfile);
else {
failed = fchflags(fd, UF_NODUMP);
if (failed) {
warn("log_trim: fchflags(NODUMP)");
}
}
}
if (noaction) {
printf("\tchmod %o %s\n", ent->permissions, tempfile);
printf("\tmv %s %s\n", tempfile, realfile);
} else {
failed = fchmod(fd, ent->permissions);
if (failed)
err(1, "can't fchmod temp file '%s'", tempfile);
failed = rename(tempfile, realfile);
if (failed)
err(1, "can't mv %s to %s", tempfile, realfile);
}
if (fd >= 0)
close(fd);
}
static void
change_attrs(const char *fname, const struct conf_entry *ent)
{
int failed;
if (noaction) {
printf("\tchmod %o %s\n", ent->permissions, fname);
if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
printf("\tchown %u:%u %s\n",
ent->uid, ent->gid, fname);
if (ent->flags & CE_NODUMP)
printf("\tchflags nodump %s\n", fname);
return;
}
failed = chmod(fname, ent->permissions);
if (failed) {
if (errno != EPERM)
err(1, "chmod(%s) in change_attrs", fname);
warn("change_attrs couldn't chmod(%s)", fname);
}
if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) {
failed = chown(fname, ent->uid, ent->gid);
if (failed)
warn("can't chown %s", fname);
}
if (ent->flags & CE_NODUMP) {
failed = chflags(fname, UF_NODUMP);
if (failed)
warn("can't chflags %s NODUMP", fname);
}
}