2003-10-13 11:28:52 +04:00
|
|
|
/* $NetBSD: newsyslog.c,v 1.47 2003/10/13 07:28:52 lukem Exp $ */
|
1996-09-27 05:56:55 +04:00
|
|
|
|
1993-05-21 18:43:59 +04:00
|
|
|
/*
|
2000-07-07 14:52:41 +04:00
|
|
|
* Copyright (c) 1999, 2000 Andrew Doran <ad@NetBSD.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1993-05-21 18:43:59 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2000-07-07 14:52:41 +04:00
|
|
|
* This file contains changes from the Open Software Foundation.
|
|
|
|
*/
|
1993-05-21 18:43:59 +04:00
|
|
|
|
2000-07-07 14:52:41 +04: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.
|
|
|
|
*
|
|
|
|
*/
|
1993-05-21 18:43:59 +04:00
|
|
|
|
|
|
|
/*
|
2000-07-18 19:59:24 +04:00
|
|
|
* newsyslog(8) - a program to roll over log files provided that specified
|
2000-07-07 14:52:41 +04:00
|
|
|
* critera are met, optionally preserving a number of historical log files.
|
1993-05-21 18:43:59 +04:00
|
|
|
*/
|
|
|
|
|
1997-10-19 10:23:50 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-08-10 04:05:51 +04:00
|
|
|
#ifndef lint
|
2003-10-13 11:28:52 +04:00
|
|
|
__RCSID("$NetBSD: newsyslog.c,v 1.47 2003/10/13 07:28:52 lukem Exp $");
|
1993-08-10 04:05:51 +04:00
|
|
|
#endif /* not lint */
|
1993-05-21 18:43:59 +04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/param.h>
|
2000-08-22 18:20:17 +04:00
|
|
|
#include <sys/wait.h>
|
1993-05-21 18:43:59 +04:00
|
|
|
|
1997-10-19 10:23:50 +04:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2000-07-07 14:52:41 +04:00
|
|
|
#include <stdarg.h>
|
1997-10-19 10:23:50 +04:00
|
|
|
#include <string.h>
|
1998-04-02 14:35:26 +04:00
|
|
|
#include <time.h>
|
1997-10-19 10:23:50 +04:00
|
|
|
#include <unistd.h>
|
2000-07-07 14:52:41 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <err.h>
|
2000-07-13 15:28:50 +04:00
|
|
|
#include <paths.h>
|
2000-07-07 14:52:41 +04:00
|
|
|
|
|
|
|
#include "pathnames.h"
|
|
|
|
|
2000-07-10 15:15:07 +04:00
|
|
|
#define PRHDRINFO(x) ((void)(verbose ? printf x : 0))
|
|
|
|
#define PRINFO(x) ((void)(verbose ? printf(" ") + printf x : 0))
|
2000-07-07 14:52:41 +04:00
|
|
|
|
2002-02-11 13:57:57 +03:00
|
|
|
#define CE_COMPRESS 0x01 /* Compress the archived log files */
|
2000-07-11 15:39:46 +04:00
|
|
|
#define CE_BINARY 0x02 /* Logfile is a binary file/non-syslog */
|
|
|
|
#define CE_NOSIGNAL 0x04 /* Don't send a signal when trimmed */
|
2000-07-18 19:59:24 +04:00
|
|
|
#define CE_CREATE 0x08 /* Create log file if none exists */
|
|
|
|
#define CE_PLAIN0 0x10 /* Do not compress zero'th history file */
|
1997-10-19 10:23:50 +04:00
|
|
|
|
1993-05-21 18:43:59 +04:00
|
|
|
struct conf_entry {
|
2000-07-07 14:52:41 +04:00
|
|
|
uid_t uid; /* Owner of log */
|
|
|
|
gid_t gid; /* Group of log */
|
|
|
|
mode_t mode; /* File permissions */
|
|
|
|
int numhist; /* Number of historical logs to keep */
|
|
|
|
size_t maxsize; /* Maximum log size */
|
|
|
|
int maxage; /* Hours between log trimming */
|
2000-07-18 19:59:24 +04:00
|
|
|
time_t trimat; /* Specific trim time */
|
2000-07-07 14:52:41 +04:00
|
|
|
int flags; /* Flags (CE_*) */
|
|
|
|
int signum; /* Signal to send */
|
|
|
|
char pidfile[MAXPATHLEN]; /* File containing PID to signal */
|
|
|
|
char logfile[MAXPATHLEN]; /* Path to log file */
|
1993-05-21 18:43:59 +04:00
|
|
|
};
|
|
|
|
|
2000-09-21 14:27:34 +04:00
|
|
|
int verbose; /* Be verbose */
|
|
|
|
int noaction; /* Take no action */
|
|
|
|
int nosignal; /* Do not send signals */
|
2000-07-07 19:42:35 +04:00
|
|
|
char hostname[MAXHOSTNAMELEN + 1]; /* Hostname, stripped of domain */
|
2000-07-18 19:59:24 +04:00
|
|
|
uid_t myeuid; /* EUID we are running with */
|
2000-07-07 14:52:41 +04:00
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
int getsig(const char *);
|
|
|
|
int isnumber(const char *);
|
2000-07-07 14:52:41 +04:00
|
|
|
int main(int, char **);
|
2000-07-18 19:59:24 +04:00
|
|
|
int parse_cfgline(struct conf_entry *, FILE *, size_t *);
|
|
|
|
time_t parse_iso8601(char *);
|
|
|
|
time_t parse_dwm(char *);
|
|
|
|
int parse_userspec(const char *, struct passwd **, struct group **);
|
|
|
|
pid_t readpidfile(const char *);
|
|
|
|
void usage(void);
|
1997-10-19 10:23:50 +04:00
|
|
|
|
2000-07-11 15:39:46 +04:00
|
|
|
void log_compress(struct conf_entry *, const char *);
|
2000-07-07 14:52:41 +04:00
|
|
|
void log_create(struct conf_entry *);
|
|
|
|
void log_examine(struct conf_entry *, int);
|
|
|
|
void log_trim(struct conf_entry *);
|
|
|
|
void log_trimmed(struct conf_entry *);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Program entry point.
|
|
|
|
*/
|
1997-10-19 10:23:50 +04:00
|
|
|
int
|
2000-07-07 14:52:41 +04:00
|
|
|
main(int argc, char **argv)
|
1993-05-21 18:43:59 +04:00
|
|
|
{
|
2000-07-11 15:39:46 +04:00
|
|
|
struct conf_entry log;
|
2000-07-07 14:52:41 +04:00
|
|
|
FILE *fd;
|
|
|
|
char *p, *cfile;
|
2000-07-18 19:59:24 +04:00
|
|
|
int c, needroot, i, force;
|
2000-07-07 14:52:41 +04:00
|
|
|
size_t lineno;
|
2001-03-18 19:34:08 +03:00
|
|
|
|
2000-07-07 14:52:41 +04:00
|
|
|
force = 0;
|
|
|
|
needroot = 1;
|
|
|
|
cfile = _PATH_NEWSYSLOGCONF;
|
1993-05-21 18:43:59 +04:00
|
|
|
|
2002-06-19 20:30:31 +04:00
|
|
|
gethostname(hostname, sizeof(hostname));
|
|
|
|
hostname[sizeof(hostname) - 1] = '\0';
|
1995-01-06 22:20:20 +03:00
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* Truncate domain. */
|
2000-07-07 14:52:41 +04:00
|
|
|
if ((p = strchr(hostname, '.')) != NULL)
|
1995-01-06 22:20:20 +03:00
|
|
|
*p = '\0';
|
2000-07-07 14:52:41 +04:00
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* Parse command line options. */
|
2000-09-21 14:27:34 +04:00
|
|
|
while ((c = getopt(argc, argv, "f:nrsvF")) != -1) {
|
2000-07-07 14:52:41 +04:00
|
|
|
switch (c) {
|
2000-07-10 15:15:07 +04:00
|
|
|
case 'f':
|
|
|
|
cfile = optarg;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
noaction = 1;
|
2000-07-11 15:39:46 +04:00
|
|
|
verbose = 1;
|
2000-07-10 15:15:07 +04:00
|
|
|
break;
|
2000-07-07 14:52:41 +04:00
|
|
|
case 'r':
|
|
|
|
needroot = 0;
|
|
|
|
break;
|
2000-09-21 14:27:34 +04:00
|
|
|
case 's':
|
|
|
|
nosignal = 1;
|
|
|
|
break;
|
2000-07-07 14:52:41 +04:00
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
force = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
2000-07-10 15:15:07 +04:00
|
|
|
/* NOTREACHED */
|
2000-07-07 14:52:41 +04:00
|
|
|
}
|
1995-01-06 22:20:20 +03:00
|
|
|
}
|
1993-05-21 18:43:59 +04:00
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
myeuid = geteuid();
|
|
|
|
if (needroot && myeuid != 0)
|
2000-07-07 14:52:41 +04:00
|
|
|
errx(EXIT_FAILURE, "must be run as root");
|
1993-05-21 18:43:59 +04:00
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2000-07-07 14:52:41 +04:00
|
|
|
if (strcmp(cfile, "-") == 0)
|
|
|
|
fd = stdin;
|
|
|
|
else if ((fd = fopen(cfile, "rt")) == NULL)
|
|
|
|
err(EXIT_FAILURE, "%s", cfile);
|
2000-07-18 19:59:24 +04:00
|
|
|
|
|
|
|
for (lineno = 0; !parse_cfgline(&log, fd, &lineno);) {
|
|
|
|
/*
|
|
|
|
* If specific log files were specified, touch only
|
|
|
|
* those.
|
|
|
|
*/
|
|
|
|
if (argc != 0) {
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
if (strcmp(log.logfile, argv[i]) == 0)
|
|
|
|
break;
|
|
|
|
if (i == argc)
|
|
|
|
continue;
|
|
|
|
}
|
2000-07-11 15:39:46 +04:00
|
|
|
log_examine(&log, force);
|
2000-07-18 19:59:24 +04:00
|
|
|
}
|
2000-07-07 14:52:41 +04:00
|
|
|
|
|
|
|
if (fd != stdin)
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
/* NOTREACHED */
|
1993-05-21 18:43:59 +04:00
|
|
|
}
|
|
|
|
|
2000-07-07 14:52:41 +04:00
|
|
|
/*
|
|
|
|
* Parse a single line from the configuration file.
|
1993-05-21 18:43:59 +04:00
|
|
|
*/
|
2000-07-07 14:52:41 +04:00
|
|
|
int
|
2000-07-18 19:59:24 +04:00
|
|
|
parse_cfgline(struct conf_entry *log, FILE *fd, size_t *_lineno)
|
1993-05-21 18:43:59 +04:00
|
|
|
{
|
2000-07-07 14:52:41 +04:00
|
|
|
char *line, *q, **ap, *argv[10];
|
|
|
|
struct passwd *pw;
|
|
|
|
struct group *gr;
|
2000-07-19 11:22:53 +04:00
|
|
|
int nf, lineno, i, rv;
|
|
|
|
|
|
|
|
rv = -1;
|
|
|
|
line = NULL;
|
2000-07-11 16:06:32 +04:00
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* Place the white-space separated fields into an array. */
|
2000-07-19 11:22:53 +04:00
|
|
|
do {
|
|
|
|
if (line != NULL)
|
|
|
|
free(line);
|
|
|
|
if ((line = fparseln(fd, NULL, _lineno, NULL, 0)) == NULL)
|
|
|
|
return (rv);
|
|
|
|
lineno = (int)*_lineno;
|
|
|
|
|
|
|
|
for (ap = argv, nf = 0; (*ap = strsep(&line, " \t")) != NULL;)
|
|
|
|
if (**ap != '\0') {
|
2002-06-19 20:30:31 +04:00
|
|
|
if (++nf == sizeof(argv) / sizeof(argv[0])) {
|
2000-07-19 11:22:53 +04:00
|
|
|
warnx("config line %d: "
|
|
|
|
"too many fields", lineno);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
ap++;
|
2000-07-07 14:52:41 +04:00
|
|
|
}
|
2000-07-19 11:22:53 +04:00
|
|
|
} while (nf == 0);
|
2000-07-07 14:52:41 +04:00
|
|
|
|
|
|
|
if (nf < 6)
|
|
|
|
errx(EXIT_FAILURE, "config line %d: too few fields", lineno);
|
|
|
|
|
2002-06-19 20:30:31 +04:00
|
|
|
memset(log, 0, sizeof(*log));
|
2000-07-19 11:22:53 +04:00
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* logfile_name */
|
2000-07-07 14:52:41 +04:00
|
|
|
ap = argv;
|
2002-06-19 20:30:31 +04:00
|
|
|
strlcpy(log->logfile, *ap++, sizeof(log->logfile));
|
2000-07-18 19:59:24 +04:00
|
|
|
if (log->logfile[0] != '/')
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"config line %d: logfile must have a full path", lineno);
|
1993-05-21 18:43:59 +04:00
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* owner:group */
|
2000-07-13 15:28:50 +04:00
|
|
|
if (strchr(*ap, ':') != NULL || strchr(*ap, '.') != NULL) {
|
2000-07-18 19:59:24 +04:00
|
|
|
if (parse_userspec(*ap++, &pw, &gr)) {
|
2000-07-07 14:52:41 +04:00
|
|
|
warnx("config line %d: unknown user/group", lineno);
|
2000-07-19 11:22:53 +04:00
|
|
|
goto bad;
|
2000-07-07 14:52:41 +04:00
|
|
|
}
|
2000-07-18 19:59:24 +04:00
|
|
|
|
|
|
|
/*
|
2000-07-24 19:15:25 +04:00
|
|
|
* We may only change the file's owner as non-root.
|
2000-07-18 19:59:24 +04:00
|
|
|
*/
|
|
|
|
if (myeuid != 0) {
|
|
|
|
if (pw->pw_uid != myeuid)
|
|
|
|
errx(EXIT_FAILURE, "config line %d: user:group "
|
|
|
|
"as non-root must match current user",
|
|
|
|
lineno);
|
|
|
|
log->uid = (uid_t)-1;
|
|
|
|
} else
|
|
|
|
log->uid = pw->pw_uid;
|
2000-07-07 14:52:41 +04:00
|
|
|
log->gid = gr->gr_gid;
|
|
|
|
if (nf < 7)
|
|
|
|
errx(EXIT_FAILURE, "config line %d: too few fields",
|
|
|
|
lineno);
|
2000-07-18 19:59:24 +04:00
|
|
|
} else if (myeuid != 0) {
|
|
|
|
log->uid = (uid_t)-1;
|
|
|
|
log->gid = getegid();
|
2000-07-07 14:52:41 +04:00
|
|
|
}
|
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* mode */
|
2000-07-07 14:52:41 +04:00
|
|
|
if (sscanf(*ap++, "%o", &i) != 1) {
|
|
|
|
warnx("config line %d: bad permissions", lineno);
|
2000-07-19 11:22:53 +04:00
|
|
|
goto bad;
|
2000-07-07 14:52:41 +04:00
|
|
|
}
|
|
|
|
log->mode = (mode_t)i;
|
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* count */
|
2000-07-09 16:14:01 +04:00
|
|
|
if (sscanf(*ap++, "%d", &log->numhist) != 1) {
|
2000-07-07 14:52:41 +04:00
|
|
|
warnx("config line %d: bad log count", lineno);
|
2000-07-19 11:22:53 +04:00
|
|
|
goto bad;
|
2000-07-07 14:52:41 +04:00
|
|
|
}
|
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* size */
|
|
|
|
if (isdigit(**ap)) {
|
|
|
|
log->maxsize = (int)strtol(*ap, &q, 0);
|
|
|
|
if (*q != '\0') {
|
|
|
|
warnx("config line %d: bad log size", lineno);
|
2000-07-19 11:22:53 +04:00
|
|
|
goto bad;
|
2000-07-18 19:59:24 +04:00
|
|
|
}
|
|
|
|
} else if (**ap == '*')
|
2000-07-07 14:52:41 +04:00
|
|
|
log->maxsize = (size_t)-1;
|
|
|
|
else {
|
|
|
|
warnx("config line %d: bad log size", lineno);
|
2000-07-19 11:22:53 +04:00
|
|
|
goto bad;
|
2000-07-07 14:52:41 +04:00
|
|
|
}
|
|
|
|
ap++;
|
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* when */
|
|
|
|
log->maxage = -1;
|
|
|
|
log->trimat = (time_t)-1;
|
|
|
|
q = *ap++;
|
|
|
|
|
|
|
|
if (strcmp(q, "*") != 0) {
|
|
|
|
if (isdigit(*q))
|
|
|
|
log->maxage = (int)strtol(q, &q, 10);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* One class of periodic interval specification can follow a
|
|
|
|
* maximum age specification. Handle it.
|
|
|
|
*/
|
|
|
|
if (*q == '@') {
|
|
|
|
log->trimat = parse_iso8601(q + 1);
|
|
|
|
if (log->trimat == (time_t)-1) {
|
|
|
|
warnx("config line %d: bad trim time", lineno);
|
2000-07-19 11:22:53 +04:00
|
|
|
goto bad;
|
2000-07-18 19:59:24 +04:00
|
|
|
}
|
|
|
|
} else if (*q == '$') {
|
|
|
|
if ((log->trimat = parse_dwm(q + 1)) == (time_t)-1) {
|
|
|
|
warnx("config line %d: bad trim time", lineno);
|
2000-07-19 11:22:53 +04:00
|
|
|
goto bad;
|
2000-07-18 19:59:24 +04:00
|
|
|
}
|
|
|
|
} else if (log->maxage == -1) {
|
|
|
|
warnx("config line %d: bad log age", lineno);
|
2000-07-19 11:22:53 +04:00
|
|
|
goto bad;
|
2000-07-18 19:59:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flags */
|
2001-06-10 16:06:35 +04:00
|
|
|
log->flags = (nosignal ? CE_NOSIGNAL : 0);
|
|
|
|
|
2000-07-10 15:15:07 +04:00
|
|
|
for (q = *ap++; q != NULL && *q != '\0'; q++) {
|
2000-07-07 14:52:41 +04:00
|
|
|
switch (tolower(*q)) {
|
|
|
|
case 'b':
|
|
|
|
log->flags |= CE_BINARY;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
log->flags |= CE_CREATE;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
log->flags |= CE_NOSIGNAL;
|
|
|
|
break;
|
2000-07-11 15:39:46 +04:00
|
|
|
case 'p':
|
|
|
|
log->flags |= CE_PLAIN0;
|
|
|
|
break;
|
2000-07-07 14:52:41 +04:00
|
|
|
case 'z':
|
2000-07-11 15:39:46 +04:00
|
|
|
log->flags |= CE_COMPRESS;
|
2000-07-07 14:52:41 +04:00
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warnx("config line %d: bad flags", lineno);
|
2000-07-19 11:22:53 +04:00
|
|
|
goto bad;
|
2000-07-07 14:52:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* path_to_pidfile */
|
2000-07-07 14:52:41 +04:00
|
|
|
if (*ap != NULL && **ap == '/')
|
2002-06-19 20:30:31 +04:00
|
|
|
strlcpy(log->pidfile, *ap++, sizeof(log->pidfile));
|
2000-07-07 14:52:41 +04:00
|
|
|
else
|
|
|
|
log->pidfile[0] = '\0';
|
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* sigtype */
|
2001-03-22 20:54:54 +03:00
|
|
|
if (*ap != NULL) {
|
|
|
|
if ((log->signum = getsig(*ap++)) < 0) {
|
|
|
|
warnx("config line %d: bad signal type", lineno);
|
|
|
|
goto bad;
|
|
|
|
}
|
2000-07-07 14:52:41 +04:00
|
|
|
} else
|
|
|
|
log->signum = SIGHUP;
|
2000-07-19 11:22:53 +04:00
|
|
|
|
|
|
|
rv = 0;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
free(line);
|
|
|
|
return (rv);
|
1993-05-21 18:43:59 +04:00
|
|
|
}
|
|
|
|
|
2000-07-07 14:52:41 +04:00
|
|
|
/*
|
|
|
|
* Examine a log file. If the trim conditions are met, call log_trim() to
|
|
|
|
* trim the log file.
|
|
|
|
*/
|
1997-10-19 10:23:50 +04:00
|
|
|
void
|
2000-07-11 15:39:46 +04:00
|
|
|
log_examine(struct conf_entry *log, int force)
|
1993-05-21 18:43:59 +04:00
|
|
|
{
|
2000-07-07 14:52:41 +04:00
|
|
|
struct stat sb;
|
|
|
|
size_t size;
|
2000-07-18 19:59:24 +04:00
|
|
|
int age, trim;
|
2000-07-19 14:52:35 +04:00
|
|
|
char tmp[MAXPATHLEN];
|
|
|
|
const char *reason;
|
2000-07-07 14:52:41 +04:00
|
|
|
time_t now;
|
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
now = time(NULL);
|
|
|
|
|
2000-07-11 15:39:46 +04:00
|
|
|
PRHDRINFO(("\n%s <%d%s>: ", log->logfile, log->numhist,
|
|
|
|
(log->flags & CE_COMPRESS) != 0 ? "Z" : ""));
|
2000-07-10 15:15:07 +04:00
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/*
|
|
|
|
* stat() the logfile. If it doesn't exist and the `c' flag has
|
|
|
|
* been specified, create it. If it doesn't exist and the `c' flag
|
|
|
|
* hasn't been specified, give up.
|
|
|
|
*/
|
2000-07-11 15:39:46 +04:00
|
|
|
if (stat(log->logfile, &sb) < 0) {
|
|
|
|
if (errno == ENOENT && (log->flags & CE_CREATE) != 0) {
|
2000-07-10 15:15:07 +04:00
|
|
|
PRHDRINFO(("creating; "));
|
|
|
|
if (!noaction)
|
2000-07-11 15:39:46 +04:00
|
|
|
log_create(log);
|
2000-07-10 15:15:07 +04:00
|
|
|
else {
|
|
|
|
PRHDRINFO(("can't proceed with `-n'\n"));
|
|
|
|
return;
|
|
|
|
}
|
2000-07-11 15:39:46 +04:00
|
|
|
if (stat(log->logfile, &sb))
|
|
|
|
err(EXIT_FAILURE, "%s", log->logfile);
|
2000-07-10 15:15:07 +04:00
|
|
|
} else if (errno == ENOENT) {
|
|
|
|
PRHDRINFO(("does not exist --> skip log\n"));
|
2000-07-07 14:52:41 +04:00
|
|
|
return;
|
2000-07-10 15:15:07 +04:00
|
|
|
} else if (errno != 0)
|
2000-07-11 15:39:46 +04:00
|
|
|
err(EXIT_FAILURE, "%s", log->logfile);
|
1999-11-30 15:03:24 +03:00
|
|
|
}
|
1993-05-21 18:43:59 +04:00
|
|
|
|
2002-06-27 07:10:15 +04:00
|
|
|
if (!S_ISREG(sb.st_mode)) {
|
|
|
|
PRHDRINFO(("not a regular file --> skip log\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* Size of the log file in kB. */
|
2000-07-07 14:52:41 +04:00
|
|
|
size = ((size_t)sb.st_blocks * S_BLKSIZE) >> 10;
|
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/*
|
|
|
|
* Get the age (expressed in hours) of the current log file with
|
|
|
|
* respect to the newest historical log file.
|
|
|
|
*/
|
2002-06-19 20:30:31 +04:00
|
|
|
strlcpy(tmp, log->logfile, sizeof(tmp));
|
|
|
|
strlcat(tmp, ".0", sizeof(tmp));
|
2000-07-07 14:52:41 +04:00
|
|
|
if (stat(tmp, &sb) < 0) {
|
2002-06-19 20:30:31 +04:00
|
|
|
strlcat(tmp, ".gz", sizeof(tmp));
|
2000-07-07 14:52:41 +04:00
|
|
|
if (stat(tmp, &sb) < 0)
|
2000-07-10 15:15:07 +04:00
|
|
|
age = -1;
|
2000-07-07 14:52:41 +04:00
|
|
|
else
|
2000-07-10 15:15:07 +04:00
|
|
|
age = (int)(now - sb.st_mtime + 1800) / 3600;
|
2000-07-07 14:52:41 +04:00
|
|
|
} else
|
2000-07-10 15:15:07 +04:00
|
|
|
age = (int)(now - sb.st_mtime + 1800) / 3600;
|
2000-07-07 14:52:41 +04:00
|
|
|
|
|
|
|
/*
|
2000-07-18 19:59:24 +04:00
|
|
|
* Examine the set of given trim conditions and if any one is met,
|
|
|
|
* trim the log.
|
|
|
|
*
|
|
|
|
* Note: if `maxage' or `trimat' is used as a trim condition, we
|
|
|
|
* need at least one historical log file to determine the `age' of
|
|
|
|
* the active log file. WRT `trimat', we will trim up to one hour
|
|
|
|
* after the specific trim time has passed - we need to know if
|
|
|
|
* we've trimmed to meet that condition with a previous invocation
|
|
|
|
* of newsyslog(8).
|
2000-07-07 14:52:41 +04:00
|
|
|
*/
|
2000-07-18 19:59:24 +04:00
|
|
|
if (log->maxage >= 0 && (age >= log->maxage || age < 0)) {
|
|
|
|
trim = 1;
|
|
|
|
reason = "log age > interval";
|
|
|
|
} else if (size >= log->maxsize) {
|
|
|
|
trim = 1;
|
|
|
|
reason = "log size > size";
|
|
|
|
} else if (log->trimat != (time_t)-1 && now >= log->trimat &&
|
2001-03-18 19:34:08 +03:00
|
|
|
(age == -1 || age > 1) &&
|
|
|
|
difftime(now, log->trimat) < 60 * 60) {
|
2000-07-18 19:59:24 +04:00
|
|
|
trim = 1;
|
|
|
|
reason = "specific trim time";
|
|
|
|
} else {
|
|
|
|
trim = force;
|
|
|
|
reason = "trim forced";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trim) {
|
|
|
|
PRHDRINFO(("--> trim log (%s)\n", reason));
|
2000-07-11 15:39:46 +04:00
|
|
|
log_trim(log);
|
2000-07-07 14:52:41 +04:00
|
|
|
} else
|
2000-07-18 19:59:24 +04:00
|
|
|
PRHDRINFO(("--> skip log (trim conditions not met)\n"));
|
1993-05-21 18:43:59 +04:00
|
|
|
}
|
|
|
|
|
2000-07-07 14:52:41 +04:00
|
|
|
/*
|
|
|
|
* Trim the specified log file.
|
|
|
|
*/
|
1997-10-19 10:23:50 +04:00
|
|
|
void
|
2000-07-07 14:52:41 +04:00
|
|
|
log_trim(struct conf_entry *log)
|
1993-05-21 18:43:59 +04:00
|
|
|
{
|
2000-07-07 14:52:41 +04:00
|
|
|
char file1[MAXPATHLEN], file2[MAXPATHLEN];
|
|
|
|
int i;
|
|
|
|
struct stat st;
|
|
|
|
pid_t pid;
|
|
|
|
|
2003-10-13 11:28:52 +04:00
|
|
|
if (log->numhist != 0) {
|
|
|
|
/* Remove oldest historical log. */
|
|
|
|
snprintf(file1, sizeof(file1), "%s.%d", log->logfile,
|
|
|
|
log->numhist - 1);
|
2000-07-07 14:52:41 +04:00
|
|
|
|
2003-10-13 11:28:52 +04:00
|
|
|
PRINFO(("rm -f %s\n", file1));
|
|
|
|
if (!noaction)
|
|
|
|
unlink(file1);
|
|
|
|
strlcat(file1, ".gz", sizeof(file1));
|
|
|
|
PRINFO(("rm -f %s\n", file1));
|
|
|
|
if (!noaction)
|
|
|
|
unlink(file1);
|
|
|
|
}
|
2000-07-07 14:52:41 +04:00
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* Move down log files. */
|
2003-10-13 11:28:52 +04:00
|
|
|
for (i = log->numhist - 1; i > 0; i--) {
|
2002-06-19 20:30:31 +04:00
|
|
|
snprintf(file1, sizeof(file1), "%s.%d", log->logfile, i - 1);
|
|
|
|
snprintf(file2, sizeof(file2), "%s.%d", log->logfile, i);
|
2000-07-07 14:52:41 +04:00
|
|
|
|
|
|
|
if (lstat(file1, &st) != 0) {
|
2002-06-19 20:30:31 +04:00
|
|
|
strlcat(file1, ".gz", sizeof(file1));
|
|
|
|
strlcat(file2, ".gz", sizeof(file2));
|
2000-07-11 15:39:46 +04:00
|
|
|
if (lstat(file1, &st) != 0)
|
2000-07-07 14:52:41 +04:00
|
|
|
continue;
|
|
|
|
}
|
1993-05-21 18:43:59 +04:00
|
|
|
|
2000-07-11 15:39:46 +04:00
|
|
|
PRINFO(("mv %s %s\n", file1, file2));
|
2000-07-10 15:15:07 +04:00
|
|
|
if (!noaction)
|
2000-07-11 15:39:46 +04:00
|
|
|
if (rename(file1, file2))
|
|
|
|
err(EXIT_FAILURE, "%s", file1);
|
|
|
|
PRINFO(("chmod %o %s\n", log->mode, file2));
|
2000-07-10 15:15:07 +04:00
|
|
|
if (!noaction)
|
2000-07-11 15:39:46 +04:00
|
|
|
if (chmod(file2, log->mode))
|
|
|
|
err(EXIT_FAILURE, "%s", file2);
|
2000-07-18 19:59:24 +04:00
|
|
|
PRINFO(("chown %d:%d %s\n", log->uid, log->gid,
|
|
|
|
file2));
|
2000-07-10 15:15:07 +04:00
|
|
|
if (!noaction)
|
2000-07-11 15:39:46 +04:00
|
|
|
if (chown(file2, log->uid, log->gid))
|
2000-07-18 19:59:24 +04:00
|
|
|
err(EXIT_FAILURE, "%s", file2);
|
2000-07-11 15:39:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a historical log file isn't compressed, and 'z' has been
|
|
|
|
* specified, compress it. (This is convenient, but is also needed
|
|
|
|
* if 'p' has been specified.) It should be noted that gzip(1)
|
|
|
|
* preserves file ownership and file mode.
|
|
|
|
*/
|
2002-06-19 20:30:31 +04:00
|
|
|
if ((log->flags & CE_COMPRESS) != 0) {
|
|
|
|
for (i = (log->flags & CE_PLAIN0) != 0; i < log->numhist; i++) {
|
|
|
|
snprintf(file1, sizeof(file1), "%s.%d", log->logfile, i);
|
|
|
|
if (lstat(file1, &st) != 0)
|
|
|
|
continue;
|
|
|
|
snprintf(file2, sizeof(file2), "%s.gz", file1);
|
|
|
|
if (lstat(file2, &st) == 0)
|
|
|
|
continue;
|
|
|
|
log_compress(log, file1);
|
|
|
|
}
|
2000-07-07 14:52:41 +04:00
|
|
|
}
|
1993-05-21 18:43:59 +04:00
|
|
|
|
2000-07-10 15:15:07 +04:00
|
|
|
log_trimmed(log);
|
2000-07-07 14:52:41 +04:00
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* Create the historical log file if we're maintaining history. */
|
2000-07-07 14:52:41 +04:00
|
|
|
if (log->numhist == 0) {
|
2000-07-10 15:15:07 +04:00
|
|
|
PRINFO(("rm -f %s\n", log->logfile));
|
|
|
|
if (!noaction)
|
|
|
|
if (unlink(log->logfile))
|
|
|
|
err(EXIT_FAILURE, "%s", log->logfile);
|
2000-07-07 14:52:41 +04:00
|
|
|
} else {
|
2002-06-19 20:30:31 +04:00
|
|
|
snprintf(file1, sizeof(file1), "%s.0", log->logfile);
|
2000-07-10 15:15:07 +04:00
|
|
|
PRINFO(("mv %s %s\n", log->logfile, file1));
|
|
|
|
if (!noaction)
|
|
|
|
if (rename(log->logfile, file1))
|
|
|
|
err(EXIT_FAILURE, "%s", log->logfile);
|
2000-07-07 14:52:41 +04:00
|
|
|
}
|
|
|
|
|
2000-07-11 15:39:46 +04:00
|
|
|
PRINFO(("(create new log)\n"));
|
2000-07-07 14:52:41 +04:00
|
|
|
log_create(log);
|
2000-07-10 15:15:07 +04:00
|
|
|
log_trimmed(log);
|
2000-07-07 14:52:41 +04:00
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* Set the correct permissions on the log. */
|
2000-07-07 14:52:41 +04:00
|
|
|
PRINFO(("chmod %o %s\n", log->mode, log->logfile));
|
2000-07-10 15:15:07 +04:00
|
|
|
if (!noaction)
|
|
|
|
if (chmod(log->logfile, log->mode))
|
|
|
|
err(EXIT_FAILURE, "%s", log->logfile);
|
2000-07-07 14:52:41 +04:00
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* Do we need to signal a daemon? */
|
2001-06-10 16:06:35 +04:00
|
|
|
if ((log->flags & CE_NOSIGNAL) == 0) {
|
2000-07-07 14:52:41 +04:00
|
|
|
if (log->pidfile[0] != '\0')
|
|
|
|
pid = readpidfile(log->pidfile);
|
|
|
|
else
|
|
|
|
pid = readpidfile(_PATH_SYSLOGDPID);
|
|
|
|
|
|
|
|
if (pid != (pid_t)-1) {
|
2000-07-18 19:59:24 +04:00
|
|
|
PRINFO(("kill -%s %lu\n",
|
|
|
|
sys_signame[log->signum], (u_long)pid));
|
2000-07-10 15:15:07 +04:00
|
|
|
if (!noaction)
|
|
|
|
if (kill(pid, log->signum))
|
|
|
|
warn("kill");
|
2000-07-07 14:52:41 +04:00
|
|
|
}
|
|
|
|
}
|
2000-07-11 15:39:46 +04:00
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
/* If the newest historical log is to be compressed, do it here. */
|
2003-10-13 11:28:52 +04:00
|
|
|
if ((log->flags & (CE_PLAIN0 | CE_COMPRESS)) == CE_COMPRESS
|
|
|
|
&& log->numhist != 0) {
|
2002-06-19 20:30:31 +04:00
|
|
|
snprintf(file1, sizeof(file1), "%s.0", log->logfile);
|
2001-06-10 16:06:35 +04:00
|
|
|
if ((log->flags & CE_NOSIGNAL) == 0) {
|
|
|
|
PRINFO(("sleep for 10 seconds before compressing...\n"));
|
|
|
|
sleep(10);
|
|
|
|
}
|
2000-07-11 15:39:46 +04:00
|
|
|
log_compress(log, file1);
|
2000-07-07 14:52:41 +04:00
|
|
|
}
|
1993-05-21 18:43:59 +04:00
|
|
|
}
|
|
|
|
|
2000-07-07 14:52:41 +04:00
|
|
|
/*
|
|
|
|
* Write an entry to the log file recording the fact that it was trimmed.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
log_trimmed(struct conf_entry *log)
|
1993-05-21 18:43:59 +04:00
|
|
|
{
|
2000-07-07 14:52:41 +04:00
|
|
|
FILE *fd;
|
|
|
|
time_t now;
|
|
|
|
char *daytime;
|
|
|
|
|
2000-07-10 15:15:07 +04:00
|
|
|
if ((log->flags & CE_BINARY) != 0)
|
|
|
|
return;
|
|
|
|
PRINFO(("(append rotation notice to %s)\n", log->logfile));
|
|
|
|
if (noaction)
|
|
|
|
return;
|
|
|
|
|
2000-07-07 14:52:41 +04:00
|
|
|
if ((fd = fopen(log->logfile, "at")) == NULL)
|
|
|
|
err(EXIT_FAILURE, "%s", log->logfile);
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
daytime = ctime(&now) + 4;
|
|
|
|
daytime[15] = '\0';
|
|
|
|
|
2000-07-11 15:39:46 +04:00
|
|
|
fprintf(fd, "%s %s newsyslog[%lu]: log file turned over\n", daytime,
|
|
|
|
hostname, (u_long)getpid());
|
2000-07-07 14:52:41 +04:00
|
|
|
fclose(fd);
|
1993-05-21 18:43:59 +04:00
|
|
|
}
|
|
|
|
|
2000-07-07 14:52:41 +04:00
|
|
|
/*
|
2000-07-11 15:39:46 +04:00
|
|
|
* Create a new log file.
|
2000-07-07 14:52:41 +04:00
|
|
|
*/
|
|
|
|
void
|
2000-07-11 15:39:46 +04:00
|
|
|
log_create(struct conf_entry *log)
|
1993-05-21 18:43:59 +04:00
|
|
|
{
|
2000-07-07 14:52:41 +04:00
|
|
|
int fd;
|
|
|
|
|
2000-07-10 15:15:07 +04:00
|
|
|
if (noaction)
|
|
|
|
return;
|
|
|
|
|
2000-07-11 15:39:46 +04:00
|
|
|
if ((fd = creat(log->logfile, log->mode)) < 0)
|
|
|
|
err(EXIT_FAILURE, "%s", log->logfile);
|
|
|
|
if (fchown(fd, log->uid, log->gid) < 0)
|
|
|
|
err(EXIT_FAILURE, "%s", log->logfile);
|
2000-07-10 15:15:07 +04:00
|
|
|
close(fd);
|
1993-05-21 18:43:59 +04:00
|
|
|
}
|
|
|
|
|
2000-07-11 15:39:46 +04:00
|
|
|
/*
|
2000-07-18 19:59:24 +04:00
|
|
|
* Fork off gzip(1) to compress a log file. This routine takes an
|
|
|
|
* additional string argument (the name of the file to compress): it is also
|
|
|
|
* used to compress historical log files other than the newest.
|
2000-07-11 15:39:46 +04:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
log_compress(struct conf_entry *log, const char *fn)
|
|
|
|
{
|
2000-07-18 19:59:24 +04:00
|
|
|
char tmp[MAXPATHLEN];
|
2000-07-11 15:39:46 +04:00
|
|
|
|
|
|
|
PRINFO(("gzip %s\n", fn));
|
|
|
|
if (!noaction) {
|
2000-08-22 18:20:17 +04:00
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if ((pid = vfork()) < 0)
|
|
|
|
err(EXIT_FAILURE, "vfork");
|
2000-07-11 15:39:46 +04:00
|
|
|
else if (pid == 0) {
|
|
|
|
execl(_PATH_GZIP, "gzip", "-f", fn, NULL);
|
2000-08-22 18:20:17 +04:00
|
|
|
_exit(EXIT_FAILURE);
|
2000-07-11 15:39:46 +04:00
|
|
|
}
|
2000-08-22 18:20:17 +04:00
|
|
|
while (waitpid(pid, &status, 0) != pid);
|
|
|
|
|
|
|
|
if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
|
|
|
|
errx(EXIT_FAILURE, "gzip failed");
|
2000-07-11 15:39:46 +04:00
|
|
|
}
|
2000-07-18 19:59:24 +04:00
|
|
|
|
2002-06-19 20:30:31 +04:00
|
|
|
snprintf(tmp, sizeof(tmp), "%s.gz", fn);
|
2000-07-18 19:59:24 +04:00
|
|
|
PRINFO(("chown %d:%d %s\n", log->uid, log->gid, tmp));
|
|
|
|
if (!noaction)
|
2000-08-22 20:23:15 +04:00
|
|
|
if (chown(tmp, log->uid, log->gid))
|
2000-07-18 19:59:24 +04:00
|
|
|
err(EXIT_FAILURE, "%s", tmp);
|
2000-07-11 15:39:46 +04:00
|
|
|
}
|
|
|
|
|
2000-07-07 14:52:41 +04:00
|
|
|
/*
|
|
|
|
* Display program usage information.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
usage(void)
|
1993-05-21 18:43:59 +04:00
|
|
|
{
|
|
|
|
|
2000-07-18 19:59:24 +04:00
|
|
|
fprintf(stderr,
|
2000-09-21 14:27:34 +04:00
|
|
|
"usage: newsyslog [-nrsvF] [-f config-file] [file ...]\n");
|
2000-07-07 14:52:41 +04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
1993-05-21 18:43:59 +04:00
|
|
|
|
2000-07-07 14:52:41 +04:00
|
|
|
/*
|
|
|
|
* Return non-zero if a string represents a decimal value.
|
|
|
|
*/
|
1997-10-19 10:23:50 +04:00
|
|
|
int
|
2000-07-07 14:52:41 +04:00
|
|
|
isnumber(const char *string)
|
1993-05-21 18:43:59 +04:00
|
|
|
{
|
2000-07-07 14:52:41 +04:00
|
|
|
|
|
|
|
while (isdigit(*string))
|
|
|
|
string++;
|
|
|
|
|
|
|
|
return (*string == '\0');
|
1993-05-21 18:43:59 +04:00
|
|
|
}
|
1999-11-30 15:03:24 +03:00
|
|
|
|
2000-07-07 14:52:41 +04:00
|
|
|
/*
|
|
|
|
* Given a signal name, attempt to find the corresponding signal number.
|
|
|
|
*/
|
1999-11-30 15:03:24 +03:00
|
|
|
int
|
2000-07-07 14:52:41 +04:00
|
|
|
getsig(const char *sig)
|
1999-11-30 15:03:24 +03:00
|
|
|
{
|
2000-07-07 14:52:41 +04:00
|
|
|
char *p;
|
1999-11-30 15:03:24 +03:00
|
|
|
int n;
|
|
|
|
|
|
|
|
if (isnumber(sig)) {
|
2000-07-07 14:52:41 +04:00
|
|
|
n = (int)strtol(sig, &p, 0);
|
2000-07-10 15:15:07 +04:00
|
|
|
if (p != '\0' || n < 0 || n >= NSIG)
|
1999-11-30 15:03:24 +03:00
|
|
|
return (-1);
|
|
|
|
return (n);
|
|
|
|
}
|
|
|
|
|
2000-07-10 15:15:07 +04:00
|
|
|
if (strncasecmp(sig, "SIG", 3) == 0)
|
1999-11-30 15:03:24 +03:00
|
|
|
sig += 3;
|
2000-07-07 14:52:41 +04:00
|
|
|
for (n = 1; n < NSIG; n++)
|
|
|
|
if (strcasecmp(sys_signame[n], sig) == 0)
|
1999-11-30 15:03:24 +03:00
|
|
|
return (n);
|
|
|
|
return (-1);
|
|
|
|
}
|
2000-07-07 14:52:41 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a path to a PID file, return the PID contained within.
|
|
|
|
*/
|
|
|
|
pid_t
|
|
|
|
readpidfile(const char *file)
|
|
|
|
{
|
|
|
|
FILE *fd;
|
|
|
|
char line[BUFSIZ];
|
|
|
|
pid_t pid;
|
|
|
|
|
2000-07-10 15:15:07 +04:00
|
|
|
#ifdef notyet
|
|
|
|
if (file[0] != '/')
|
2002-06-19 20:30:31 +04:00
|
|
|
snprintf(tmp, sizeof(tmp), "%s%s", _PATH_VARRUN, file);
|
2000-07-10 15:15:07 +04:00
|
|
|
else
|
2002-06-19 20:30:31 +04:00
|
|
|
strlcpy(tmp, file, sizeof(tmp));
|
2000-07-10 15:15:07 +04:00
|
|
|
#endif
|
|
|
|
|
2000-07-07 19:42:35 +04:00
|
|
|
if ((fd = fopen(file, "rt")) == NULL) {
|
|
|
|
warn("%s", file);
|
2000-07-07 14:52:41 +04:00
|
|
|
return (-1);
|
|
|
|
}
|
2000-07-07 19:42:35 +04:00
|
|
|
|
2002-06-19 20:30:31 +04:00
|
|
|
if (fgets(line, sizeof(line) - 1, fd) != NULL) {
|
|
|
|
line[sizeof(line) - 1] = '\0';
|
2000-07-07 14:52:41 +04:00
|
|
|
pid = (pid_t)strtol(line, NULL, 0);
|
2000-07-11 15:39:46 +04:00
|
|
|
} else {
|
|
|
|
warnx("unable to read %s", file);
|
|
|
|
pid = (pid_t)-1;
|
2000-07-07 14:52:41 +04:00
|
|
|
}
|
2000-07-11 15:39:46 +04:00
|
|
|
|
2000-07-07 14:52:41 +04:00
|
|
|
fclose(fd);
|
|
|
|
return (pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a user:group specification.
|
|
|
|
*
|
2000-07-18 19:59:24 +04:00
|
|
|
* XXX This is over the top for newsyslog(8). It should be moved to libutil.
|
2000-07-07 14:52:41 +04:00
|
|
|
*/
|
|
|
|
int
|
2000-07-18 19:59:24 +04:00
|
|
|
parse_userspec(const char *name, struct passwd **pw, struct group **gr)
|
2000-07-07 14:52:41 +04:00
|
|
|
{
|
|
|
|
char buf[MAXLOGNAME * 2 + 2], *group;
|
|
|
|
|
2002-06-19 20:30:31 +04:00
|
|
|
strlcpy(buf, name, sizeof(buf));
|
2000-07-07 14:52:41 +04:00
|
|
|
*gr = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Before attempting to use '.' as a separator, see if the whole
|
2000-07-10 15:15:07 +04:00
|
|
|
* string resolves as a user name.
|
2000-07-07 14:52:41 +04:00
|
|
|
*/
|
|
|
|
if ((*pw = getpwnam(buf)) != NULL) {
|
|
|
|
*gr = getgrgid((*pw)->pw_gid);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Split the user and group name. */
|
|
|
|
if ((group = strchr(buf, ':')) != NULL ||
|
|
|
|
(group = strchr(buf, '.')) != NULL)
|
|
|
|
*group++ = '\0';
|
|
|
|
|
|
|
|
if (isnumber(buf))
|
|
|
|
*pw = getpwuid((uid_t)atoi(buf));
|
|
|
|
else
|
|
|
|
*pw = getpwnam(buf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the group. If a group wasn't specified, use the user's
|
|
|
|
* `natural' group. We get to this point even if no user was found.
|
|
|
|
* This is to allow the caller to get a better idea of what went
|
|
|
|
* wrong, if anything.
|
|
|
|
*/
|
|
|
|
if (group == NULL || *group == '\0') {
|
|
|
|
if (*pw == NULL)
|
|
|
|
return (-1);
|
|
|
|
*gr = getgrgid((*pw)->pw_gid);
|
|
|
|
} else if (isnumber(group))
|
|
|
|
*gr = getgrgid((gid_t)atoi(group));
|
|
|
|
else
|
|
|
|
*gr = getgrnam(group);
|
|
|
|
|
2000-07-10 15:15:07 +04:00
|
|
|
return (*pw != NULL && *gr != NULL ? 0 : -1);
|
2000-07-07 14:52:41 +04:00
|
|
|
}
|
2000-07-18 19:59:24 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a cyclic time specification, the format is as follows:
|
|
|
|
*
|
|
|
|
* [Dhh] or [Wd[Dhh]] or [Mdd[Dhh]]
|
|
|
|
*
|
|
|
|
* to rotate a log file 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.
|
|
|
|
*/
|
|
|
|
time_t
|
|
|
|
parse_dwm(char *s)
|
|
|
|
{
|
|
|
|
char *t;
|
|
|
|
struct tm tm, *tmp;
|
|
|
|
u_long ul;
|
|
|
|
time_t now;
|
|
|
|
static int mtab[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
|
|
|
int wmseen, dseen, nd, save;
|
|
|
|
|
|
|
|
wmseen = 0;
|
|
|
|
dseen = 0;
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
tmp = localtime(&now);
|
|
|
|
tm = *tmp;
|
|
|
|
|
|
|
|
/* Set no. of days per month */
|
|
|
|
nd = mtab[tm.tm_mon];
|
|
|
|
|
|
|
|
if (tm.tm_mon == 1 &&
|
|
|
|
((tm.tm_year + 1900) % 4 == 0) &&
|
|
|
|
((tm.tm_year + 1900) % 100 != 0) &&
|
|
|
|
((tm.tm_year + 1900) % 400 == 0))
|
|
|
|
nd++; /* leap year, 29 days in february */
|
|
|
|
tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
switch (*s) {
|
|
|
|
case 'D':
|
|
|
|
if (dseen)
|
|
|
|
return ((time_t)-1);
|
|
|
|
dseen++;
|
|
|
|
s++;
|
|
|
|
ul = strtoul(s, &t, 10);
|
|
|
|
if (ul < 0 || ul > 23)
|
|
|
|
return ((time_t)-1);
|
|
|
|
tm.tm_hour = ul;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'W':
|
|
|
|
if (wmseen)
|
|
|
|
return (-1);
|
|
|
|
wmseen++;
|
|
|
|
s++;
|
|
|
|
ul = strtoul(s, &t, 10);
|
|
|
|
if (ul < 0 || ul > 6)
|
|
|
|
return (-1);
|
|
|
|
if (ul != tm.tm_wday) {
|
|
|
|
if (ul < tm.tm_wday) {
|
|
|
|
save = 6 - tm.tm_wday;
|
|
|
|
save += (ul + 1);
|
|
|
|
} else
|
|
|
|
save = ul - tm.tm_wday;
|
|
|
|
tm.tm_mday += save;
|
|
|
|
|
|
|
|
if (tm.tm_mday > nd) {
|
|
|
|
tm.tm_mon++;
|
|
|
|
tm.tm_mday = tm.tm_mday - nd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'M':
|
|
|
|
if (wmseen)
|
|
|
|
return (-1);
|
|
|
|
wmseen++;
|
|
|
|
s++;
|
|
|
|
if (tolower(*s) == 'l') {
|
|
|
|
tm.tm_mday = nd;
|
|
|
|
s++;
|
|
|
|
t = s;
|
|
|
|
} else {
|
|
|
|
ul = strtoul(s, &t, 10);
|
|
|
|
if (ul < 1 || ul > 31)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (ul > nd)
|
|
|
|
return (-1);
|
|
|
|
tm.tm_mday = ul;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (-1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*t == '\0' || isspace(*t))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
s = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (mktime(&tm));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a limited subset of ISO 8601. The specific format is as follows:
|
|
|
|
*
|
|
|
|
* [CC[YY[MM[DD]]]][THH[MM[SS]]] (where `T' is the literal letter)
|
|
|
|
*
|
|
|
|
* We don't accept a timezone specification; missing fields (including
|
|
|
|
* timezone) are defaulted to the current date but time zero.
|
|
|
|
*/
|
|
|
|
time_t
|
|
|
|
parse_iso8601(char *s)
|
|
|
|
{
|
|
|
|
char *t;
|
|
|
|
struct tm tm, *tmp;
|
|
|
|
u_long ul;
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
tmp = localtime(&now);
|
|
|
|
tm = *tmp;
|
|
|
|
|
|
|
|
tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
|
|
|
|
|
|
|
|
ul = strtoul(s, &t, 10);
|
|
|
|
if (*t != '\0' && *t != 'T')
|
|
|
|
return ((time_t)-1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
switch (t - s) {
|
|
|
|
case 8:
|
|
|
|
tm.tm_year = ((ul / 1000000) - 19) * 100;
|
|
|
|
ul = ul % 1000000;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 6:
|
|
|
|
tm.tm_year = tm.tm_year - (tm.tm_year % 100);
|
|
|
|
tm.tm_year += ul / 10000;
|
|
|
|
ul = ul % 10000;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 4:
|
|
|
|
tm.tm_mon = (ul / 100) - 1;
|
|
|
|
ul = ul % 100;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 2:
|
|
|
|
tm.tm_mday = ul;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return ((time_t)-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
if (tm.tm_year < 70 || tm.tm_mon < 0 || tm.tm_mon > 12 ||
|
|
|
|
tm.tm_mday < 1 || tm.tm_mday > 31)
|
|
|
|
return ((time_t)-1);
|
|
|
|
|
|
|
|
if (*t != '\0') {
|
|
|
|
s = ++t;
|
|
|
|
ul = strtoul(s, &t, 10);
|
|
|
|
if (*t != '\0' && !isspace(*t))
|
|
|
|
return ((time_t)-1);
|
|
|
|
|
|
|
|
switch (t - s) {
|
|
|
|
case 6:
|
|
|
|
tm.tm_sec = ul % 100;
|
|
|
|
ul /= 100;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 4:
|
|
|
|
tm.tm_min = ul % 100;
|
|
|
|
ul /= 100;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 2:
|
|
|
|
tm.tm_hour = ul;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return ((time_t)-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
if (tm.tm_sec < 0 || tm.tm_sec > 60 || tm.tm_min < 0 ||
|
|
|
|
tm.tm_min > 59 || tm.tm_hour < 0 || tm.tm_hour > 23)
|
|
|
|
return ((time_t)-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (mktime(&tm));
|
|
|
|
}
|