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
|
2001-07-30 15:17:17 +00:00
|
|
|
#ifndef BZCOMPRESS_POSTFIX
|
|
|
|
#define BZCOMPRESS_POSTFIX ".bz2"
|
|
|
|
#endif
|
1996-01-05 09:28:11 +00:00
|
|
|
|
2002-05-03 21:03:37 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
1997-10-06 07:46:08 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
2002-05-03 20:53:37 +00:00
|
|
|
#include <errno.h>
|
1997-10-06 07:46:08 +00:00
|
|
|
#include <fcntl.h>
|
2003-03-03 00:01:51 +00:00
|
|
|
#include <fnmatch.h>
|
2002-11-14 08:43:07 +00:00
|
|
|
#include <glob.h>
|
1997-10-06 07:46:08 +00:00
|
|
|
#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
|
|
|
|
1999-01-22 19:38:39 +00:00
|
|
|
#include "pathnames.h"
|
|
|
|
|
2003-03-02 22:05:17 +00:00
|
|
|
/*
|
|
|
|
* Bit-values for the 'flags' parsed from a config-file entry.
|
|
|
|
*/
|
|
|
|
#define CE_COMPACT 0x0001 /* Compact the achived log files with gzip. */
|
|
|
|
#define CE_BZCOMPACT 0x0002 /* Compact the achived log files with bzip2. */
|
2003-03-03 02:53:35 +00:00
|
|
|
#define CE_COMPACTWAIT 0x0004 /* wait until compressing one file finishes */
|
2003-03-02 22:05:17 +00:00
|
|
|
/* before starting the next step. */
|
|
|
|
#define CE_BINARY 0x0008 /* Logfile is in binary, do not add status */
|
|
|
|
/* messages to logfile(s) when rotating. */
|
2003-03-03 02:53:35 +00:00
|
|
|
#define CE_NOSIGNAL 0x0010 /* There is no process to signal when */
|
2003-03-02 22:05:17 +00:00
|
|
|
/* trimming this file. */
|
2003-03-03 02:53:35 +00:00
|
|
|
#define CE_TRIMAT 0x0020 /* trim file at a specific time. */
|
|
|
|
#define CE_GLOB 0x0040 /* name of the log is file name pattern. */
|
|
|
|
|
|
|
|
#define MIN_PID 5 /* Don't touch pids lower than this */
|
|
|
|
#define MAX_PID 99999 /* was lower, see /usr/include/sys/proc.h */
|
|
|
|
|
|
|
|
#define kbytes(size) (((size) + 1023) >> 10)
|
1999-01-22 19:38:39 +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 */
|
2003-03-02 23:23:11 +00:00
|
|
|
char *r_reason; /* The reason this file is being rotated */
|
|
|
|
int rotate; /* Non-zero if this file should be rotated */
|
2003-03-03 01:23:34 +00:00
|
|
|
uid_t uid; /* Owner of log */
|
|
|
|
gid_t gid; /* Group of log */
|
2000-04-04 08:38:30 +00:00
|
|
|
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 */
|
2001-07-30 16:27:16 +00:00
|
|
|
int flags; /* CE_COMPACT, CE_BZCOMPACT, CE_BINARY */
|
2000-04-04 08:38:30 +00:00
|
|
|
int sig; /* Signal to send */
|
2003-02-24 00:51:41 +00:00
|
|
|
int def_cfg; /* Using the <default> rule for this file */
|
2000-04-04 08:38:30 +00:00
|
|
|
struct conf_entry *next;/* Linked list pointer */
|
1996-01-05 09:28:11 +00:00
|
|
|
};
|
|
|
|
|
2003-02-24 00:51:41 +00:00
|
|
|
#define DEFAULT_MARKER "<default>"
|
|
|
|
|
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 */
|
2003-03-02 22:05:17 +00:00
|
|
|
int nosignal; /* Do not send any signals */
|
2000-04-04 08:38:30 +00:00
|
|
|
int force = 0; /* Force the trim no matter what */
|
2003-03-02 23:23:11 +00:00
|
|
|
int rotatereq = 0; /* -R = Always rotate the file(s) as given */
|
|
|
|
/* on the command (this also requires */
|
|
|
|
/* that a list of files *are* given on */
|
|
|
|
/* the run command). */
|
|
|
|
char *requestor; /* The name given on a -R request */
|
2000-04-04 08:50:01 +00:00
|
|
|
char *archdirname; /* Directory path to old logfiles archive */
|
2003-03-03 00:01:51 +00:00
|
|
|
const char *conf; /* Configuration file to use */
|
2000-04-04 08:38:30 +00:00
|
|
|
time_t timenow;
|
|
|
|
|
2001-01-20 17:36:08 +00:00
|
|
|
char hostname[MAXHOSTNAMELEN]; /* hostname */
|
2002-12-21 22:27:26 +00:00
|
|
|
char daytime[16]; /* timenow in human readable form */
|
1996-01-05 09:28:11 +00:00
|
|
|
|
2003-03-03 00:01:51 +00:00
|
|
|
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 **defconf_p);
|
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);
|
2003-02-24 00:51:41 +00:00
|
|
|
static void free_entry(struct conf_entry *ent);
|
|
|
|
static struct conf_entry *init_entry(const char *fname,
|
|
|
|
struct conf_entry *src_entry);
|
2003-03-03 02:53:35 +00:00
|
|
|
static void parse_args(int argc, char **argv);
|
2001-07-30 15:41:20 +00:00
|
|
|
static void usage(void);
|
2003-03-03 01:23:34 +00:00
|
|
|
static void dotrim(const struct conf_entry *ent, char *log,
|
2003-03-03 01:58:33 +00:00
|
|
|
int numdays, int flags);
|
2003-03-02 22:05:17 +00:00
|
|
|
static int log_trim(const char *log, const struct conf_entry *log_ent);
|
2002-12-15 19:28:02 +00:00
|
|
|
static void compress_log(char *log, int dowait);
|
|
|
|
static void bzcompress_log(char *log, int dowait);
|
1996-06-08 23:32:10 +00:00
|
|
|
static int sizefile(char *file);
|
|
|
|
static int age_old_log(char *file);
|
2001-07-30 15:41:20 +00:00
|
|
|
static pid_t get_pid(const char *pid_file);
|
2002-04-02 12:03:16 +00:00
|
|
|
static time_t parse8601(char *s, char *errline);
|
2003-03-03 01:23:34 +00:00
|
|
|
static void movefile(char *from, char *to, int perm, uid_t owner_uid,
|
|
|
|
gid_t group_gid);
|
2000-04-04 08:50:01 +00:00
|
|
|
static void createdir(char *dirpart);
|
2002-04-02 12:03:16 +00:00
|
|
|
static time_t parseDWM(char *s, char *errline);
|
1996-06-08 23:32:10 +00:00
|
|
|
|
2003-03-02 22:05:17 +00:00
|
|
|
/*
|
|
|
|
* All the following are defined to work on an 'int', in the
|
|
|
|
* range 0 to 255, plus EOF. Define wrappers which can take
|
|
|
|
* values of type 'char', either signed or unsigned.
|
|
|
|
*/
|
2003-03-02 23:23:11 +00:00
|
|
|
#define isprintch(Anychar) isprint(((int) Anychar) & 255)
|
2003-03-02 22:05:17 +00:00
|
|
|
#define isspacech(Anychar) isspace(((int) Anychar) & 255)
|
|
|
|
#define tolowerch(Anychar) tolower(((int) Anychar) & 255)
|
|
|
|
|
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;
|
2003-02-26 03:20:38 +00:00
|
|
|
char *savglob;
|
2002-11-14 08:43:07 +00:00
|
|
|
glob_t pglob;
|
|
|
|
int i;
|
2000-04-04 08:38:30 +00:00
|
|
|
|
2003-03-03 02:53:35 +00:00
|
|
|
parse_args(argc, argv);
|
2003-03-03 00:01:51 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
if (needroot && getuid() && geteuid())
|
|
|
|
errx(1, "must have root privs");
|
2003-03-03 00:01:51 +00:00
|
|
|
p = q = get_worklist(argv);
|
2000-04-04 08:38:30 +00:00
|
|
|
|
|
|
|
while (p) {
|
2002-11-14 08:43:07 +00:00
|
|
|
if ((p->flags & CE_GLOB) == 0) {
|
|
|
|
do_entry(p);
|
|
|
|
} else {
|
2003-03-03 00:01:51 +00:00
|
|
|
if (verbose > 2)
|
|
|
|
printf("\t+ Processing pattern %s\n", p->log);
|
2002-11-14 08:43:07 +00:00
|
|
|
if (glob(p->log, GLOB_NOCHECK, NULL, &pglob) != 0) {
|
|
|
|
warn("can't expand pattern: %s", p->log);
|
|
|
|
} else {
|
2003-02-26 03:20:38 +00:00
|
|
|
savglob = p->log;
|
2002-11-14 08:43:07 +00:00
|
|
|
for (i = 0; i < pglob.gl_matchc; i++) {
|
|
|
|
p->log = pglob.gl_pathv[i];
|
|
|
|
do_entry(p);
|
|
|
|
}
|
|
|
|
globfree(&pglob);
|
2003-02-26 03:20:38 +00:00
|
|
|
p->log = savglob;
|
2003-03-03 00:01:51 +00:00
|
|
|
if (verbose > 2)
|
|
|
|
printf("\t+ Done with pattern\n");
|
2002-11-14 08:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
2000-04-04 08:38:30 +00:00
|
|
|
p = p->next;
|
2003-02-24 00:51:41 +00:00
|
|
|
free_entry(q);
|
2000-04-04 08:38:30 +00:00
|
|
|
q = p;
|
|
|
|
}
|
2002-05-03 20:53:37 +00:00
|
|
|
while (wait(NULL) > 0 || errno == EINTR)
|
|
|
|
;
|
2000-04-04 08:38:30 +00:00
|
|
|
return (0);
|
1996-01-05 09:28:11 +00:00
|
|
|
}
|
|
|
|
|
2003-02-24 00:51:41 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
tempwork->log = strdup(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);
|
2003-03-02 23:23:11 +00:00
|
|
|
tempwork->r_reason = NULL;
|
|
|
|
tempwork->rotate = 0;
|
2003-02-24 00:51:41 +00:00
|
|
|
tempwork->uid = src_entry->uid;
|
|
|
|
tempwork->gid = src_entry->gid;
|
|
|
|
tempwork->numlogs = src_entry->numlogs;
|
|
|
|
tempwork->size = src_entry->size;
|
|
|
|
tempwork->hours = src_entry->hours;
|
|
|
|
tempwork->trim_at = 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 {
|
|
|
|
/* Initialize as a "do-nothing" entry */
|
|
|
|
tempwork->pid_file = NULL;
|
2003-03-02 23:23:11 +00:00
|
|
|
tempwork->r_reason = NULL;
|
|
|
|
tempwork->rotate = 0;
|
2003-03-03 01:23:34 +00:00
|
|
|
tempwork->uid = (uid_t)-1;
|
|
|
|
tempwork->gid = (gid_t)-1;
|
2003-02-24 00:51:41 +00:00
|
|
|
tempwork->numlogs = 1;
|
|
|
|
tempwork->size = -1;
|
|
|
|
tempwork->hours = -1;
|
|
|
|
tempwork->trim_at = (time_t)0;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-03-02 23:23:11 +00:00
|
|
|
if (ent->r_reason != NULL) {
|
|
|
|
free(ent->r_reason);
|
|
|
|
ent->r_reason = NULL;
|
|
|
|
}
|
|
|
|
|
2003-02-24 00:51:41 +00:00
|
|
|
free(ent);
|
|
|
|
}
|
|
|
|
|
2000-04-04 08:50:01 +00:00
|
|
|
static void
|
|
|
|
do_entry(struct conf_entry * ent)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2003-03-02 23:23:11 +00:00
|
|
|
#define REASON_MAX 80
|
2000-04-04 08:38:30 +00:00
|
|
|
int size, modtime;
|
2003-03-02 23:23:11 +00:00
|
|
|
char temp_reason[REASON_MAX];
|
2000-04-04 08:38:30 +00:00
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
if (ent->flags & CE_COMPACT)
|
|
|
|
printf("%s <%dZ>: ", ent->log, ent->numlogs);
|
2001-07-30 15:17:17 +00:00
|
|
|
else if (ent->flags & CE_BZCOMPACT)
|
|
|
|
printf("%s <%dJ>: ", ent->log, ent->numlogs);
|
2000-04-04 08:38:30 +00:00
|
|
|
else
|
|
|
|
printf("%s <%d>: ", ent->log, ent->numlogs);
|
|
|
|
}
|
|
|
|
size = sizefile(ent->log);
|
|
|
|
modtime = age_old_log(ent->log);
|
2003-03-02 23:23:11 +00:00
|
|
|
ent->rotate = 0;
|
2000-04-04 08:38:30 +00:00
|
|
|
if (size < 0) {
|
|
|
|
if (verbose)
|
|
|
|
printf("does not exist.\n");
|
|
|
|
} else {
|
2003-03-02 23:23:11 +00:00
|
|
|
if (ent->flags & CE_TRIMAT && !force && !rotatereq) {
|
1999-01-22 19:38:39 +00:00
|
|
|
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);
|
2003-03-02 23:23:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out if this logfile needs to be rotated.
|
|
|
|
*/
|
|
|
|
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->size > 0) && (size >= ent->size)) {
|
|
|
|
ent->rotate = 1;
|
|
|
|
snprintf(temp_reason, REASON_MAX, " due to size>%dK",
|
|
|
|
ent->size);
|
|
|
|
} 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 the file needs to be rotated, then rotate it.
|
|
|
|
*/
|
|
|
|
if (ent->rotate) {
|
|
|
|
if (temp_reason[0] != '\0')
|
|
|
|
ent->r_reason = strdup(temp_reason);
|
2000-04-04 08:38:30 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("--> trimming log....\n");
|
|
|
|
if (noaction && !verbose) {
|
|
|
|
if (ent->flags & CE_COMPACT)
|
|
|
|
printf("%s <%dZ>: trimming\n",
|
|
|
|
ent->log, ent->numlogs);
|
2001-07-30 15:17:17 +00:00
|
|
|
else if (ent->flags & CE_BZCOMPACT)
|
|
|
|
printf("%s <%dJ>: trimming\n",
|
|
|
|
ent->log, ent->numlogs);
|
2000-04-04 08:38:30 +00:00
|
|
|
else
|
|
|
|
printf("%s <%d>: trimming\n",
|
|
|
|
ent->log, ent->numlogs);
|
|
|
|
}
|
2003-03-03 01:58:33 +00:00
|
|
|
dotrim(ent, ent->log, ent->numlogs, ent->flags);
|
2000-04-04 08:38:30 +00:00
|
|
|
} else {
|
|
|
|
if (verbose)
|
|
|
|
printf("--> skipping\n");
|
|
|
|
}
|
|
|
|
}
|
2003-03-02 23:23:11 +00:00
|
|
|
#undef REASON_MAX
|
1996-01-05 09:28:11 +00:00
|
|
|
}
|
|
|
|
|
2000-04-04 08:50:01 +00:00
|
|
|
static void
|
2003-03-03 02:53:35 +00:00
|
|
|
parse_args(int argc, char **argv)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2003-03-03 02:53:35 +00:00
|
|
|
int ch;
|
2000-04-04 08:38:30 +00:00
|
|
|
char *p;
|
1996-01-05 09:28:11 +00:00
|
|
|
|
2003-03-03 02:53:35 +00:00
|
|
|
timenow = time(NULL);
|
2002-12-21 22:27:26 +00:00
|
|
|
(void)strncpy(daytime, ctime(&timenow) + 4, 15);
|
2000-04-04 08:38:30 +00:00
|
|
|
daytime[15] = '\0';
|
1996-01-05 09:28:11 +00:00
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
/* Let's get our hostname */
|
2003-03-03 02:53:35 +00:00
|
|
|
(void)gethostname(hostname, sizeof(hostname));
|
1996-01-05 09:28:11 +00:00
|
|
|
|
|
|
|
/* Truncate domain */
|
2003-03-03 02:53:35 +00:00
|
|
|
if ((p = strchr(hostname, '.')) != NULL)
|
1996-01-05 09:28:11 +00:00
|
|
|
*p = '\0';
|
2003-03-02 22:05:17 +00:00
|
|
|
|
|
|
|
/* Parse command line options. */
|
2003-03-03 02:53:35 +00:00
|
|
|
while ((ch = getopt(argc, argv, "a:f:nrsvFR:")) != -1)
|
|
|
|
switch (ch) {
|
2000-04-04 08:50:01 +00:00
|
|
|
case 'a':
|
|
|
|
archtodir++;
|
|
|
|
archdirname = optarg;
|
|
|
|
break;
|
2003-03-02 22:05:17 +00:00
|
|
|
case 'f':
|
|
|
|
conf = optarg;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
noaction++;
|
|
|
|
break;
|
2000-04-04 08:38:30 +00:00
|
|
|
case 'r':
|
|
|
|
needroot = 0;
|
|
|
|
break;
|
2003-03-02 22:05:17 +00:00
|
|
|
case 's':
|
|
|
|
nosignal = 1;
|
|
|
|
break;
|
2000-04-04 08:38:30 +00:00
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
1998-03-14 22:28:25 +00:00
|
|
|
case 'F':
|
|
|
|
force++;
|
|
|
|
break;
|
2003-03-02 23:23:11 +00:00
|
|
|
case 'R':
|
|
|
|
rotatereq++;
|
|
|
|
requestor = strdup(optarg);
|
|
|
|
break;
|
2003-03-03 02:53:35 +00:00
|
|
|
case 'm': /* Used by OpenBSD for "monitor mode" */
|
2000-04-04 08:38:30 +00:00
|
|
|
default:
|
|
|
|
usage();
|
2003-03-02 22:05:17 +00:00
|
|
|
/* NOTREACHED */
|
2000-04-04 08:38:30 +00:00
|
|
|
}
|
2003-03-02 23:23:11 +00:00
|
|
|
|
|
|
|
if (rotatereq) {
|
|
|
|
if (optind == argc) {
|
|
|
|
warnx("At least one filename must be given when -R is specified.");
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
/* Make sure "requestor" value is safe for a syslog message. */
|
|
|
|
for (p = requestor; *p != '\0'; p++) {
|
|
|
|
if (!isprintch(*p) && (*p != '\t'))
|
|
|
|
*p = '.';
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2001-07-30 16:27:16 +00:00
|
|
|
|
|
|
|
fprintf(stderr,
|
2003-03-02 23:23:11 +00:00
|
|
|
"usage: newsyslog [-Fnrsv] [-a directory] [-f config-file]\n"
|
|
|
|
" [ [-R requestor] filename ... ]\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
|
|
|
/*
|
2003-03-03 00:01:51 +00:00
|
|
|
* Parse a configuration file and return a linked list of all the logs
|
|
|
|
* which should be processed.
|
1996-01-05 09:28:11 +00:00
|
|
|
*/
|
2000-04-04 08:38:30 +00:00
|
|
|
static struct conf_entry *
|
2003-03-03 00:01:51 +00:00
|
|
|
get_worklist(char **files)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
FILE *f;
|
2003-03-03 00:01:51 +00:00
|
|
|
const char *fname;
|
|
|
|
char **given;
|
|
|
|
struct conf_entry *defconf, *dupent, *ent, *firstnew;
|
|
|
|
struct conf_entry *newlist, *worklist;
|
|
|
|
int gmatch;
|
|
|
|
|
|
|
|
defconf = 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", conf);
|
|
|
|
|
|
|
|
parse_file(f, fname, &worklist, &defconf);
|
|
|
|
(void) fclose(f);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All config-file information has been read in and turned into
|
|
|
|
* a worklist. If there were no specific files given on the run
|
|
|
|
* command, then the work of this routine is done.
|
|
|
|
*/
|
|
|
|
if (*files == NULL) {
|
|
|
|
if (defconf != NULL)
|
|
|
|
free_entry(defconf);
|
|
|
|
return (worklist);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If newsyslog was given a specific list of files to process,
|
|
|
|
* it may be that some of those files were not listed in any
|
|
|
|
* config file. Those unlisted files should get the default
|
|
|
|
* rotation action. First, create the default-rotation action
|
|
|
|
* if none was found in a system config file.
|
|
|
|
*/
|
|
|
|
if (defconf == NULL) {
|
|
|
|
defconf = init_entry(DEFAULT_MARKER, NULL);
|
|
|
|
defconf->numlogs = 3;
|
|
|
|
defconf->size = 50;
|
|
|
|
defconf->permissions = S_IRUSR|S_IWUSR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If newsyslog was run with a list of specific filenames,
|
|
|
|
* then create a new worklist which has only those files in
|
|
|
|
* it, picking up the rotation-rules for those files from
|
|
|
|
* the original worklist.
|
|
|
|
*
|
|
|
|
* XXX - Note that this will copy multiple rules for a single
|
|
|
|
* logfile, if multiple entries are an exact match for
|
|
|
|
* that file. That matches the historic behavior, but do
|
|
|
|
* we want to continue to allow it? If so, it should
|
|
|
|
* probably be handled more intelligently.
|
|
|
|
*/
|
|
|
|
firstnew = newlist = NULL;
|
|
|
|
for (given = files; *given; ++given) {
|
|
|
|
gmatch = 0;
|
|
|
|
/*
|
|
|
|
* First try to find exact-matches for this given file.
|
|
|
|
*/
|
|
|
|
for (ent = worklist; ent; ent = ent->next) {
|
|
|
|
if ((ent->flags & CE_GLOB) != 0)
|
|
|
|
continue;
|
|
|
|
if (strcmp(ent->log, *given) == 0) {
|
|
|
|
gmatch++;
|
|
|
|
dupent = init_entry(*given, ent);
|
|
|
|
if (!firstnew)
|
|
|
|
firstnew = dupent;
|
|
|
|
else
|
|
|
|
newlist->next = dupent;
|
|
|
|
newlist = dupent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gmatch) {
|
|
|
|
if (verbose > 2)
|
|
|
|
printf("\t+ Matched entry %s\n", *given);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There was no exact-match for this given file, so look
|
|
|
|
* for a "glob" entry which does match.
|
|
|
|
*/
|
|
|
|
for (ent = worklist; ent; ent = ent->next) {
|
|
|
|
if ((ent->flags & CE_GLOB) == 0)
|
|
|
|
continue;
|
|
|
|
if (fnmatch(ent->log, *given, FNM_PATHNAME) == 0) {
|
|
|
|
gmatch++;
|
|
|
|
dupent = init_entry(*given, ent);
|
|
|
|
if (!firstnew)
|
|
|
|
firstnew = dupent;
|
|
|
|
else
|
|
|
|
newlist->next = dupent;
|
|
|
|
newlist = dupent;
|
|
|
|
/* This work entry is *not* a glob! */
|
|
|
|
dupent->flags &= ~CE_GLOB;
|
|
|
|
/* Only allow a match to one glob-entry */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gmatch) {
|
|
|
|
if (verbose > 2)
|
|
|
|
printf("\t+ Matched %s via %s\n", *given,
|
|
|
|
ent->log);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This given file was not found in any config file, so
|
|
|
|
* add a worklist item based on the default entry.
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
newlist->next = dupent;
|
|
|
|
/* Mark that it was *not* found in a config file */
|
|
|
|
dupent->def_cfg = 1;
|
|
|
|
newlist = dupent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free all the entries in the original work list, and then
|
|
|
|
* return the new work list.
|
|
|
|
*/
|
|
|
|
while (worklist) {
|
|
|
|
ent = worklist->next;
|
|
|
|
free_entry(worklist);
|
|
|
|
worklist = ent;
|
|
|
|
}
|
|
|
|
|
|
|
|
free_entry(defconf);
|
|
|
|
return (newlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a configuration file and update a linked list of all the logs to
|
|
|
|
* process.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
parse_file(FILE *cf, const char *cfname, struct conf_entry **work_p,
|
|
|
|
struct conf_entry **defconf_p)
|
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
char line[BUFSIZ], *parse, *q;
|
2002-12-11 01:19:56 +00:00
|
|
|
char *cp, *errline, *group;
|
2003-03-03 00:01:51 +00:00
|
|
|
struct conf_entry *working, *worklist;
|
2003-03-03 02:53:35 +00:00
|
|
|
struct passwd *pwd;
|
2000-04-04 08:38:30 +00:00
|
|
|
struct group *grp;
|
1997-05-06 23:11:06 +00:00
|
|
|
int eol;
|
1996-01-05 09:28:11 +00:00
|
|
|
|
2003-03-03 00:01:51 +00:00
|
|
|
/*
|
|
|
|
* XXX - for now, assume that only one config file will be read,
|
|
|
|
* ie, this routine is only called one time.
|
|
|
|
*/
|
|
|
|
worklist = NULL;
|
2001-07-30 16:27:16 +00:00
|
|
|
|
2003-03-03 00:01:51 +00:00
|
|
|
while (fgets(line, BUFSIZ, cf)) {
|
2002-12-11 01:19:56 +00:00
|
|
|
if ((line[0] == '\n') || (line[0] == '#') ||
|
|
|
|
(strlen(line) == 0))
|
2000-04-04 08:38:30 +00:00
|
|
|
continue;
|
|
|
|
errline = strdup(line);
|
2002-12-11 01:19:56 +00:00
|
|
|
for (cp = line + 1; *cp != '\0'; cp++) {
|
|
|
|
if (*cp != '#')
|
|
|
|
continue;
|
|
|
|
if (*(cp - 1) == '\\') {
|
|
|
|
strcpy(cp - 1, cp);
|
|
|
|
cp--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*cp = '\0';
|
|
|
|
break;
|
|
|
|
}
|
2000-05-11 10:17:54 +00:00
|
|
|
|
|
|
|
q = parse = missing_field(sob(line), errline);
|
|
|
|
parse = son(line);
|
|
|
|
if (!*parse)
|
2001-07-30 16:27:16 +00:00
|
|
|
errx(1, "malformed line (missing fields):\n%s",
|
|
|
|
errline);
|
2000-05-11 10:17:54 +00:00
|
|
|
*parse = '\0';
|
|
|
|
|
2003-02-24 00:51:41 +00:00
|
|
|
working = init_entry(q, NULL);
|
|
|
|
if (strcasecmp(DEFAULT_MARKER, q) == 0) {
|
2003-03-03 00:01:51 +00:00
|
|
|
if (defconf_p == NULL) {
|
|
|
|
warnx("Ignoring entry for %s in %s!", q,
|
|
|
|
cfname);
|
|
|
|
free_entry(working);
|
|
|
|
continue;
|
|
|
|
} else if (*defconf_p != NULL) {
|
2003-02-24 00:51:41 +00:00
|
|
|
warnx("Ignoring duplicate entry for %s!", q);
|
|
|
|
free_entry(working);
|
|
|
|
continue;
|
|
|
|
}
|
2003-03-03 00:01:51 +00:00
|
|
|
*defconf_p = working;
|
2000-04-04 08:38:30 +00:00
|
|
|
} else {
|
2003-03-03 00:01:51 +00:00
|
|
|
if (!*work_p)
|
|
|
|
*work_p = working;
|
2003-02-24 00:51:41 +00:00
|
|
|
else
|
|
|
|
worklist->next = working;
|
|
|
|
worklist = working;
|
2000-04-04 08:38:30 +00:00
|
|
|
}
|
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)
|
2001-07-30 16:27:16 +00:00
|
|
|
errx(1, "malformed line (missing fields):\n%s",
|
|
|
|
errline);
|
2000-04-04 08:38:30 +00:00
|
|
|
*parse = '\0';
|
|
|
|
if ((group = strchr(q, ':')) != NULL ||
|
|
|
|
(group = strrchr(q, '.')) != NULL) {
|
|
|
|
*group++ = '\0';
|
|
|
|
if (*q) {
|
|
|
|
if (!(isnumber(*q))) {
|
2003-03-03 02:53:35 +00:00
|
|
|
if ((pwd = getpwnam(q)) == NULL)
|
2000-04-04 08:38:30 +00:00
|
|
|
errx(1,
|
2001-07-30 16:27:16 +00:00
|
|
|
"error in config file; unknown user:\n%s",
|
2000-04-04 08:38:30 +00:00
|
|
|
errline);
|
2003-03-03 02:53:35 +00:00
|
|
|
working->uid = pwd->pw_uid;
|
2000-04-04 08:38:30 +00:00
|
|
|
} else
|
|
|
|
working->uid = atoi(q);
|
|
|
|
} else
|
2003-03-03 01:23:34 +00:00
|
|
|
working->uid = (uid_t)-1;
|
2000-04-04 08:38:30 +00:00
|
|
|
|
|
|
|
q = group;
|
|
|
|
if (*q) {
|
|
|
|
if (!(isnumber(*q))) {
|
|
|
|
if ((grp = getgrnam(q)) == NULL)
|
|
|
|
errx(1,
|
2001-07-30 16:27:16 +00:00
|
|
|
"error in config file; unknown group:\n%s",
|
2000-04-04 08:38:30 +00:00
|
|
|
errline);
|
|
|
|
working->gid = grp->gr_gid;
|
|
|
|
} else
|
|
|
|
working->gid = atoi(q);
|
|
|
|
} else
|
2003-03-03 01:23:34 +00:00
|
|
|
working->gid = (gid_t)-1;
|
2000-04-04 08:38:30 +00:00
|
|
|
|
|
|
|
q = parse = missing_field(sob(++parse), errline);
|
|
|
|
parse = son(parse);
|
|
|
|
if (!*parse)
|
2001-07-30 16:27:16 +00:00
|
|
|
errx(1, "malformed line (missing fields):\n%s",
|
|
|
|
errline);
|
2000-04-04 08:38:30 +00:00
|
|
|
*parse = '\0';
|
2003-03-03 01:23:34 +00:00
|
|
|
} else {
|
|
|
|
working->uid = (uid_t)-1;
|
|
|
|
working->gid = (gid_t)-1;
|
|
|
|
}
|
2000-04-04 08:38:30 +00:00
|
|
|
|
|
|
|
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)
|
2001-07-30 16:27:16 +00:00
|
|
|
errx(1, "malformed line (missing fields):\n%s",
|
|
|
|
errline);
|
2000-04-04 08:38:30 +00:00
|
|
|
*parse = '\0';
|
2003-02-24 02:28:46 +00:00
|
|
|
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",
|
2000-04-04 08:38:30 +00:00
|
|
|
errline);
|
|
|
|
|
|
|
|
q = parse = missing_field(sob(++parse), errline);
|
|
|
|
parse = son(parse);
|
1997-05-06 23:11:06 +00:00
|
|
|
if (!*parse)
|
2001-07-30 16:27:16 +00:00
|
|
|
errx(1, "malformed line (missing fields):\n%s",
|
|
|
|
errline);
|
2000-04-04 08:38:30 +00:00
|
|
|
*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;
|
|
|
|
|
2001-07-30 16:27:16 +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 == '@') {
|
2002-04-02 12:03:16 +00:00
|
|
|
if ((working->trim_at = parse8601(ep + 1, errline))
|
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 == '$') {
|
2002-04-02 12:03:16 +00:00
|
|
|
if ((working->trim_at = parseDWM(ep + 1, errline))
|
2000-04-04 08:50:01 +00:00
|
|
|
== (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';
|
|
|
|
}
|
|
|
|
|
2003-03-02 22:05:17 +00:00
|
|
|
for (; q && *q && !isspacech(*q); q++) {
|
|
|
|
switch (tolowerch(*q)) {
|
|
|
|
case 'b':
|
2000-04-04 08:38:30 +00:00
|
|
|
working->flags |= CE_BINARY;
|
2003-03-02 22:05:17 +00:00
|
|
|
break;
|
2003-03-03 02:53:35 +00:00
|
|
|
case 'c': /* Used by NetBSD for "CE_CREATE" */
|
2003-03-02 22:05:17 +00:00
|
|
|
/*
|
|
|
|
* netbsd uses 'c' for "create". We will
|
|
|
|
* temporarily accept it for 'g', because
|
|
|
|
* earlier freebsd versions had a typo
|
|
|
|
* of ('G' || 'c')...
|
|
|
|
*/
|
|
|
|
warnx("Assuming 'g' for 'c' in flags for line:\n%s",
|
|
|
|
errline);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 'g':
|
2002-11-14 08:43:07 +00:00
|
|
|
working->flags |= CE_GLOB;
|
2003-03-02 22:05:17 +00:00
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
working->flags |= CE_BZCOMPACT;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
working->flags |= CE_NOSIGNAL;
|
|
|
|
break;
|
|
|
|
case 'w':
|
2002-12-15 19:28:02 +00:00
|
|
|
working->flags |= CE_COMPACTWAIT;
|
2003-03-02 22:05:17 +00:00
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
working->flags |= CE_COMPACT;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
break;
|
2003-03-03 02:53:35 +00:00
|
|
|
case 'f': /* Used by OpenBSD for "CE_FOLLOW" */
|
|
|
|
case 'm': /* Used by OpenBSD for "CE_MONITOR" */
|
|
|
|
case 'p': /* Used by NetBSD for "CE_PLAIN0" */
|
2003-03-02 22:05:17 +00:00
|
|
|
default:
|
2001-07-30 16:27:16 +00:00
|
|
|
errx(1, "illegal flag in config file -- %c",
|
|
|
|
*q);
|
2003-03-02 22:05:17 +00:00
|
|
|
}
|
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
|
2001-07-30 16:27:16 +00:00
|
|
|
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:
|
2001-07-30 16:27:16 +00:00
|
|
|
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
|
|
|
}
|
2003-03-02 22:05:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Finish figuring out what pid-file to use (if any) in
|
|
|
|
* later processing if this logfile needs to be rotated.
|
|
|
|
*/
|
|
|
|
if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) {
|
|
|
|
/*
|
|
|
|
* This config-entry specified 'n' for nosignal,
|
|
|
|
* see if it also specified an explicit pid_file.
|
|
|
|
* This would be a pretty pointless combination.
|
|
|
|
*/
|
|
|
|
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 (nosignal) {
|
|
|
|
/*
|
|
|
|
* While this entry might usually signal some
|
|
|
|
* process via the pid-file, newsyslog was run
|
|
|
|
* with '-s', so quietly ignore the pid-file.
|
|
|
|
*/
|
|
|
|
if (working->pid_file != NULL) {
|
|
|
|
free(working->pid_file);
|
|
|
|
working->pid_file = NULL;
|
|
|
|
}
|
|
|
|
} else if (working->pid_file == NULL) {
|
|
|
|
/*
|
|
|
|
* This entry did not specify the 'n' flag, which
|
|
|
|
* means it should signal syslogd unless it had
|
|
|
|
* specified some other pid-file. But we only
|
|
|
|
* try to notify syslog if we are root
|
|
|
|
*/
|
|
|
|
if (needroot)
|
|
|
|
working->pid_file = strdup(_PATH_SYSLOGPID);
|
|
|
|
}
|
|
|
|
|
2000-04-04 08:38:30 +00:00
|
|
|
free(errline);
|
2003-03-02 22:05:17 +00:00
|
|
|
errline = NULL;
|
2000-04-04 08:38:30 +00:00
|
|
|
}
|
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
|
|
|
{
|
2001-07-30 16:27:16 +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
|
2003-03-03 01:58:33 +00:00
|
|
|
dotrim(const struct conf_entry *ent, char *log, int numdays, int flags)
|
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];
|
2001-07-30 15:17:17 +00:00
|
|
|
char jfile1[MAXPATHLEN];
|
2002-04-10 10:38:44 +00:00
|
|
|
char tfile[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
|
|
|
|
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-07-30 16:27:16 +00:00
|
|
|
(void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart,
|
|
|
|
namepart, numdays);
|
2001-01-20 17:36:08 +00:00
|
|
|
(void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
|
|
|
|
COMPRESS_POSTFIX);
|
2001-07-30 15:17:17 +00:00
|
|
|
snprintf(jfile1, sizeof(jfile1), "%s%s", file1,
|
|
|
|
BZCOMPRESS_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);
|
2001-07-30 15:17:17 +00:00
|
|
|
snprintf(jfile1, sizeof(jfile1), "%s%s", file1,
|
|
|
|
BZCOMPRESS_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);
|
2001-07-30 15:17:17 +00:00
|
|
|
printf("rm -f %s\n", jfile1);
|
2000-04-04 08:38:30 +00:00
|
|
|
} else {
|
|
|
|
(void) unlink(file1);
|
|
|
|
(void) unlink(zfile1);
|
2001-07-30 15:17:17 +00:00
|
|
|
(void) unlink(jfile1);
|
2000-04-04 08:38:30 +00:00
|
|
|
}
|
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-07-30 16:27:16 +00:00
|
|
|
(void) snprintf(file1, sizeof(file1), "%s/%s.%d",
|
|
|
|
dirpart, namepart, numdays);
|
2000-04-04 08:50:01 +00:00
|
|
|
else
|
2001-07-30 16:27:16 +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-07-30 16:27:16 +00:00
|
|
|
(void) strlcat(zfile1, COMPRESS_POSTFIX,
|
|
|
|
sizeof(zfile1));
|
|
|
|
(void) strlcat(zfile2, COMPRESS_POSTFIX,
|
|
|
|
sizeof(zfile2));
|
2001-07-30 15:17:17 +00:00
|
|
|
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;
|
|
|
|
}
|
2000-04-04 08:38:30 +00:00
|
|
|
}
|
|
|
|
if (noaction) {
|
|
|
|
printf("mv %s %s\n", zfile1, zfile2);
|
2003-03-03 01:58:33 +00:00
|
|
|
printf("chmod %o %s\n", ent->permissions, zfile2);
|
2003-03-03 01:23:34 +00:00
|
|
|
if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
|
|
|
|
printf("chown %u:%u %s\n",
|
|
|
|
ent->uid, ent->gid, zfile2);
|
2000-04-04 08:38:30 +00:00
|
|
|
} else {
|
|
|
|
(void) rename(zfile1, zfile2);
|
2003-03-03 01:58:33 +00:00
|
|
|
if (chmod(zfile2, ent->permissions))
|
|
|
|
warn("can't chmod %s", file2);
|
2003-03-03 01:23:34 +00:00
|
|
|
if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
|
|
|
|
if (chown(zfile2, ent->uid, ent->gid))
|
|
|
|
warn("can't chown %s", zfile2);
|
2000-04-04 08:38:30 +00:00
|
|
|
}
|
|
|
|
}
|
2003-02-24 00:51:41 +00:00
|
|
|
if (!noaction && !(flags & CE_BINARY)) {
|
|
|
|
/* Report the trimming to the old log */
|
2003-03-03 01:23:34 +00:00
|
|
|
(void) log_trim(log, ent);
|
2003-02-24 00:51:41 +00:00
|
|
|
}
|
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)
|
2003-03-03 01:58:33 +00:00
|
|
|
movefile(log, file1, ent->permissions, ent->uid,
|
2003-03-03 01:23:34 +00:00
|
|
|
ent->gid);
|
2000-04-04 08:50:01 +00:00
|
|
|
else
|
|
|
|
(void) rename(log, file1);
|
|
|
|
}
|
1996-09-06 06:36:07 +00:00
|
|
|
}
|
|
|
|
|
2003-03-03 02:53:35 +00:00
|
|
|
/* Now move the new log file into place */
|
2000-04-04 08:38:30 +00:00
|
|
|
if (noaction)
|
2003-03-03 02:53:35 +00:00
|
|
|
printf("Start new log...\n");
|
2000-04-04 08:38:30 +00:00
|
|
|
else {
|
2002-04-10 10:38:44 +00:00
|
|
|
strlcpy(tfile, log, sizeof(tfile));
|
|
|
|
strlcat(tfile, ".XXXXXX", sizeof(tfile));
|
|
|
|
mkstemp(tfile);
|
2003-03-03 01:58:33 +00:00
|
|
|
fd = creat(tfile, ent->permissions);
|
2000-04-04 08:38:30 +00:00
|
|
|
if (fd < 0)
|
|
|
|
err(1, "can't start new log");
|
2003-03-03 01:23:34 +00:00
|
|
|
if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
|
|
|
|
if (fchown(fd, ent->uid, ent->gid))
|
|
|
|
err(1, "can't chown new log file");
|
2000-04-04 08:38:30 +00:00
|
|
|
(void) close(fd);
|
2003-02-24 00:51:41 +00:00
|
|
|
if (!(flags & CE_BINARY)) {
|
|
|
|
/* Add status message to new log file */
|
2003-03-03 01:23:34 +00:00
|
|
|
if (log_trim(tfile, ent))
|
2000-04-04 08:38:30 +00:00
|
|
|
err(1, "can't add status message to log");
|
2003-02-24 00:51:41 +00:00
|
|
|
}
|
2000-04-04 08:38:30 +00:00
|
|
|
}
|
|
|
|
if (noaction)
|
2003-03-03 01:58:33 +00:00
|
|
|
printf("chmod %o %s...\n", ent->permissions, log);
|
2002-04-10 10:38:44 +00:00
|
|
|
else {
|
2003-03-03 01:58:33 +00:00
|
|
|
(void) chmod(tfile, ent->permissions);
|
2002-04-10 10:38:44 +00:00
|
|
|
if (rename(tfile, log) < 0) {
|
|
|
|
err(1, "can't start new log");
|
|
|
|
(void) unlink(tfile);
|
|
|
|
}
|
|
|
|
}
|
1997-05-04 01:53:53 +00:00
|
|
|
|
|
|
|
pid = 0;
|
|
|
|
need_notification = notified = 0;
|
2003-03-03 01:23:34 +00:00
|
|
|
if (ent->pid_file != NULL) {
|
1997-05-04 01:53:53 +00:00
|
|
|
need_notification = 1;
|
2003-03-03 01:23:34 +00:00
|
|
|
pid = get_pid(ent->pid_file);
|
1997-05-04 01:53:53 +00:00
|
|
|
}
|
|
|
|
if (pid) {
|
|
|
|
if (noaction) {
|
|
|
|
notified = 1;
|
2003-03-03 01:58:33 +00:00
|
|
|
printf("kill -%d %d\n", ent->sig, (int) pid);
|
|
|
|
} else if (kill(pid, ent->sig))
|
2000-04-04 08:38:30 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2001-07-30 15:17:17 +00:00
|
|
|
if ((flags & CE_COMPACT) || (flags & CE_BZCOMPACT)) {
|
1997-05-04 01:53:53 +00:00
|
|
|
if (need_notification && !notified)
|
2001-07-30 16:27:16 +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-07-30 16:27:16 +00:00
|
|
|
(void) snprintf(file1, sizeof(file1), "%s/%s",
|
|
|
|
dirpart, namepart);
|
2001-07-30 15:17:17 +00:00
|
|
|
if (flags & CE_COMPACT)
|
2002-12-15 19:28:02 +00:00
|
|
|
compress_log(file1,
|
|
|
|
flags & CE_COMPACTWAIT);
|
2001-07-30 15:17:17 +00:00
|
|
|
else if (flags & CE_BZCOMPACT)
|
2002-12-15 19:28:02 +00:00
|
|
|
bzcompress_log(file1,
|
|
|
|
flags & CE_COMPACTWAIT);
|
2000-04-04 08:50:01 +00:00
|
|
|
} else {
|
2001-07-30 15:17:17 +00:00
|
|
|
if (flags & CE_COMPACT)
|
2002-12-15 19:28:02 +00:00
|
|
|
compress_log(log,
|
|
|
|
flags & CE_COMPACTWAIT);
|
2001-07-30 15:17:17 +00:00
|
|
|
else if (flags & CE_BZCOMPACT)
|
2002-12-15 19:28:02 +00:00
|
|
|
bzcompress_log(log,
|
|
|
|
flags & CE_COMPACTWAIT);
|
2000-04-04 08:50:01 +00:00
|
|
|
}
|
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
|
2003-03-02 22:05:17 +00:00
|
|
|
log_trim(const char *log, const struct conf_entry *log_ent)
|
1996-01-05 09:28:11 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
FILE *f;
|
2003-02-24 00:51:41 +00:00
|
|
|
const char *xtra;
|
2000-04-04 08:38:30 +00:00
|
|
|
|
|
|
|
if ((f = fopen(log, "a")) == NULL)
|
|
|
|
return (-1);
|
2003-02-24 00:51:41 +00:00
|
|
|
xtra = "";
|
2003-03-02 22:05:17 +00:00
|
|
|
if (log_ent->def_cfg)
|
2003-02-24 00:51:41 +00:00
|
|
|
xtra = " using <default> rule";
|
2003-03-02 23:23:11 +00:00
|
|
|
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);
|
2000-04-04 08:38:30 +00:00
|
|
|
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
|
2002-12-15 19:28:02 +00:00
|
|
|
compress_log(char *log, int dowait)
|
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
|
|
|
|
2002-12-15 19:28:02 +00:00
|
|
|
while (dowait && (wait(NULL) > 0 || errno == EINTR))
|
|
|
|
;
|
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)
|
2001-07-30 15:17:17 +00:00
|
|
|
err(1, "gzip fork");
|
2000-04-04 08:38:30 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2001-07-30 15:17:17 +00:00
|
|
|
/* Fork of bzip2 to compress the old log file */
|
|
|
|
static void
|
2002-12-15 19:28:02 +00:00
|
|
|
bzcompress_log(char *log, int dowait)
|
2001-07-30 15:17:17 +00:00
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
char tmp[MAXPATHLEN];
|
|
|
|
|
2002-12-15 19:28:02 +00:00
|
|
|
while (dowait && (wait(NULL) > 0 || errno == EINTR))
|
|
|
|
;
|
2001-07-30 15:17:17 +00:00
|
|
|
snprintf(tmp, sizeof(tmp), "%s.0", log);
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0)
|
|
|
|
err(1, "bzip2 fork");
|
|
|
|
else if (!pid) {
|
2001-11-14 17:00:58 +00:00
|
|
|
execl(_PATH_BZIP2, _PATH_BZIP2, "-f", tmp, (char *)0);
|
2001-07-30 15:17:17 +00:00
|
|
|
err(1, _PATH_BZIP2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2003-03-03 02:53:35 +00:00
|
|
|
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
|
2001-07-30 15:41:20 +00:00
|
|
|
get_pid(const 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) {
|
2001-07-30 16:27:16 +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
|
|
|
}
|
2003-02-24 01:05:05 +00:00
|
|
|
return (pid);
|
1997-05-04 01:53:53 +00:00
|
|
|
}
|
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
|
2002-04-02 12:03:16 +00:00
|
|
|
parse8601(char *s, char *errline)
|
1999-01-22 19:38:39 +00:00
|
|
|
{
|
2000-04-04 08:38:30 +00:00
|
|
|
char *t;
|
2002-04-02 12:03:16 +00:00
|
|
|
time_t tsecs;
|
2000-04-04 08:38:30 +00:00
|
|
|
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')
|
2003-02-24 01:05:05 +00:00
|
|
|
return (-1);
|
1999-01-22 19:38:39 +00:00
|
|
|
|
|
|
|
/*
|
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:
|
2001-07-30 18:27:21 +00:00
|
|
|
tm.tm_year -= tm.tm_year % 100;
|
1999-01-22 19:38:39 +00:00
|
|
|
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:
|
2003-02-24 01:05:05 +00:00
|
|
|
return (-1);
|
1999-01-22 19:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (tm.tm_year < 70 || tm.tm_mon < 0 || tm.tm_mon > 12
|
|
|
|
|| tm.tm_mday < 1 || tm.tm_mday > 31)
|
2003-02-24 01:05:05 +00:00
|
|
|
return (-1);
|
1999-01-22 19:38:39 +00:00
|
|
|
|
|
|
|
if (*t != '\0') {
|
|
|
|
s = ++t;
|
|
|
|
ul = strtoul(s, &t, 10);
|
|
|
|
if (*t != '\0' && !isspace(*t))
|
2003-02-24 01:05:05 +00:00
|
|
|
return (-1);
|
1999-01-22 19:38:39 +00:00
|
|
|
|
|
|
|
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:
|
2003-02-24 01:05:05 +00:00
|
|
|
return (-1);
|
1999-01-22 19:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
2003-02-24 01:05:05 +00:00
|
|
|
return (-1);
|
1999-01-22 19:38:39 +00:00
|
|
|
}
|
2002-04-02 12:03:16 +00:00
|
|
|
if ((tsecs = mktime(&tm)) == -1)
|
2002-07-01 13:18:52 +00:00
|
|
|
errx(1, "nonexistent time:\n%s", errline);
|
2003-02-24 01:05:05 +00:00
|
|
|
return (tsecs);
|
1999-01-22 19:38:39 +00:00
|
|
|
}
|
2000-04-04 08:50:01 +00:00
|
|
|
|
|
|
|
/* physically move file */
|
|
|
|
static void
|
2003-03-03 01:23:34 +00:00
|
|
|
movefile(char *from, char *to, int perm, uid_t owner_uid, gid_t group_gid)
|
2000-04-04 08:50:01 +00:00
|
|
|
{
|
|
|
|
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);
|
2003-03-03 01:23:34 +00:00
|
|
|
if (owner_uid != (uid_t)-1 || group_gid != (gid_t)-1) {
|
|
|
|
if (fchown(fileno(dst), owner_uid, group_gid))
|
|
|
|
err(1, "can't fchown %s", to);
|
|
|
|
}
|
2000-04-04 08:50:01 +00:00
|
|
|
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)
|
|
|
|
{
|
2003-02-24 02:09:02 +00:00
|
|
|
int res;
|
2000-04-04 08:50:01 +00:00
|
|
|
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++;
|
2003-02-24 02:09:02 +00:00
|
|
|
if (*s != '/' && *s != '\0')
|
|
|
|
continue;
|
|
|
|
*d = '\0';
|
|
|
|
res = lstat(mkdirpath, &st);
|
|
|
|
if (res != 0) {
|
|
|
|
if (noaction) {
|
|
|
|
printf("mkdir %s\n", mkdirpath);
|
|
|
|
} else {
|
|
|
|
res = mkdir(mkdirpath, 0755);
|
|
|
|
if (res != 0)
|
|
|
|
err(1, "Error on mkdir(\"%s\") for -a",
|
|
|
|
mkdirpath);
|
|
|
|
}
|
2000-04-04 08:50:01 +00:00
|
|
|
}
|
|
|
|
if (*s == '\0')
|
|
|
|
break;
|
|
|
|
}
|
2003-02-24 02:09:02 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("created directory '%s' for -a\n", dirpart);
|
2000-04-04 08:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* 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
|
2002-04-02 12:03:16 +00:00
|
|
|
parseDWM(char *s, char *errline)
|
2000-04-04 08:50:01 +00:00
|
|
|
{
|
|
|
|
char *t;
|
2002-04-02 12:03:16 +00:00
|
|
|
time_t tsecs;
|
2000-04-04 08:50:01 +00:00
|
|
|
struct tm tm, *tmp;
|
2001-07-31 16:25:55 +00:00
|
|
|
long l;
|
2000-04-04 08:50:01 +00:00
|
|
|
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)
|
2003-02-24 01:05:05 +00:00
|
|
|
return (-1);
|
2000-04-04 08:50:01 +00:00
|
|
|
Dseen++;
|
|
|
|
s++;
|
2001-07-31 16:25:55 +00:00
|
|
|
l = strtol(s, &t, 10);
|
|
|
|
if (l < 0 || l > 23)
|
2003-02-24 01:05:05 +00:00
|
|
|
return (-1);
|
2001-07-31 16:25:55 +00:00
|
|
|
tm.tm_hour = l;
|
2000-04-04 08:50:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'W':
|
|
|
|
if (WMseen)
|
2003-02-24 01:05:05 +00:00
|
|
|
return (-1);
|
2000-04-04 08:50:01 +00:00
|
|
|
WMseen++;
|
|
|
|
s++;
|
2001-07-31 16:25:55 +00:00
|
|
|
l = strtol(s, &t, 10);
|
|
|
|
if (l < 0 || l > 6)
|
2003-02-24 01:05:05 +00:00
|
|
|
return (-1);
|
2001-07-31 16:25:55 +00:00
|
|
|
if (l != tm.tm_wday) {
|
2000-04-04 08:50:01 +00:00
|
|
|
int save;
|
|
|
|
|
2001-07-31 16:25:55 +00:00
|
|
|
if (l < tm.tm_wday) {
|
2000-04-04 08:50:01 +00:00
|
|
|
save = 6 - tm.tm_wday;
|
2001-07-31 16:25:55 +00:00
|
|
|
save += (l + 1);
|
2000-04-04 08:50:01 +00:00
|
|
|
} else {
|
2001-07-31 16:25:55 +00:00
|
|
|
save = l - tm.tm_wday;
|
2000-04-04 08:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tm.tm_mday += save;
|
|
|
|
|
|
|
|
if (tm.tm_mday > nd) {
|
|
|
|
tm.tm_mon++;
|
|
|
|
tm.tm_mday = tm.tm_mday - nd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'M':
|
|
|
|
if (WMseen)
|
2003-02-24 01:05:05 +00:00
|
|
|
return (-1);
|
2000-04-04 08:50:01 +00:00
|
|
|
WMseen++;
|
|
|
|
s++;
|
|
|
|
if (tolower(*s) == 'l') {
|
|
|
|
tm.tm_mday = nd;
|
|
|
|
s++;
|
|
|
|
t = s;
|
|
|
|
} else {
|
2001-07-31 16:25:55 +00:00
|
|
|
l = strtol(s, &t, 10);
|
|
|
|
if (l < 1 || l > 31)
|
2003-02-24 01:05:05 +00:00
|
|
|
return (-1);
|
2000-04-04 08:50:01 +00:00
|
|
|
|
2001-07-31 16:25:55 +00:00
|
|
|
if (l > nd)
|
2003-02-24 01:05:05 +00:00
|
|
|
return (-1);
|
2001-07-31 16:25:55 +00:00
|
|
|
tm.tm_mday = l;
|
2000-04-04 08:50:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (-1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*t == '\0' || isspace(*t))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
s = t;
|
|
|
|
}
|
2002-04-02 12:03:16 +00:00
|
|
|
if ((tsecs = mktime(&tm)) == -1)
|
2002-07-01 13:18:52 +00:00
|
|
|
errx(1, "nonexistent time:\n%s", errline);
|
2003-02-24 01:05:05 +00:00
|
|
|
return (tsecs);
|
2000-04-04 08:50:01 +00:00
|
|
|
}
|