newsyslog.c revision 210372
1130167Sgad/*-
2130167Sgad * ------+---------+---------+-------- + --------+---------+---------+---------*
3130167Sgad * This file includes significant modifications done by:
4130167Sgad * Copyright (c) 2003, 2004  - Garance Alistair Drosehn <gad@FreeBSD.org>.
5130167Sgad * All rights reserved.
6130167Sgad *
7130167Sgad * Redistribution and use in source and binary forms, with or without
8130167Sgad * modification, are permitted provided that the following conditions
9130167Sgad * are met:
10130167Sgad *   1. Redistributions of source code must retain the above copyright
11130167Sgad *      notice, this list of conditions and the following disclaimer.
12130167Sgad *   2. Redistributions in binary form must reproduce the above copyright
13130167Sgad *      notice, this list of conditions and the following disclaimer in the
14130167Sgad *      documentation and/or other materials provided with the distribution.
15130167Sgad *
16130167Sgad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17130167Sgad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18130167Sgad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19130167Sgad * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20130167Sgad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21130167Sgad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22130167Sgad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23130167Sgad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24130167Sgad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25130167Sgad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26130167Sgad * SUCH DAMAGE.
27130167Sgad *
28130167Sgad * ------+---------+---------+-------- + --------+---------+---------+---------*
29130167Sgad */
30130167Sgad
3113244Sgraichen/*
3213244Sgraichen * This file contains changes from the Open Software Foundation.
3313244Sgraichen */
3413244Sgraichen
3513244Sgraichen/*
3659004Shm * Copyright 1988, 1989 by the Massachusetts Institute of Technology
3759004Shm *
3859004Shm * Permission to use, copy, modify, and distribute this software and its
3959004Shm * documentation for any purpose and without fee is hereby granted, provided
4059004Shm * that the above copyright notice appear in all copies and that both that
4159004Shm * copyright notice and this permission notice appear in supporting
4259004Shm * documentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
4359004Shm * used in advertising or publicity pertaining to distribution of the
4459004Shm * software without specific, written prior permission. M.I.T. and the M.I.T.
4559004Shm * S.I.P.B. make no representations about the suitability of this software
4659004Shm * for any purpose.  It is provided "as is" without express or implied
4759004Shm * warranty.
4859004Shm *
4959004Shm */
5013244Sgraichen
5113244Sgraichen/*
5259004Shm * newsyslog - roll over selected logs at the appropriate time, keeping the a
5359004Shm * specified number of backup files around.
5413244Sgraichen */
5513244Sgraichen
56114601Sobrien#include <sys/cdefs.h>
57114601Sobrien__FBSDID("$FreeBSD: head/usr.sbin/newsyslog/newsyslog.c 210372 2010-07-22 11:23:18Z simon $");
5813244Sgraichen
59130045Sgad#define	OSF
6013244Sgraichen#ifndef COMPRESS_POSTFIX
61130045Sgad#define	COMPRESS_POSTFIX ".gz"
6213244Sgraichen#endif
6380638Sobrien#ifndef	BZCOMPRESS_POSTFIX
6480638Sobrien#define	BZCOMPRESS_POSTFIX ".bz2"
6580638Sobrien#endif
6613244Sgraichen
6796001Smaxim#include <sys/param.h>
68130167Sgad#include <sys/queue.h>
6996001Smaxim#include <sys/stat.h>
7096001Smaxim#include <sys/wait.h>
7196001Smaxim
72210372Ssimon#include <assert.h>
7330160Scharnier#include <ctype.h>
7430160Scharnier#include <err.h>
7595999Smaxim#include <errno.h>
76210372Ssimon#include <dirent.h>
7730160Scharnier#include <fcntl.h>
78111773Sgad#include <fnmatch.h>
79106905Ssobomax#include <glob.h>
8030160Scharnier#include <grp.h>
8143071Swollman#include <paths.h>
8230160Scharnier#include <pwd.h>
8330160Scharnier#include <signal.h>
8413244Sgraichen#include <stdio.h>
85210372Ssimon#include <libgen.h>
8613244Sgraichen#include <stdlib.h>
8713244Sgraichen#include <string.h>
8843071Swollman#include <time.h>
8916240Salex#include <unistd.h>
9013244Sgraichen
9143071Swollman#include "pathnames.h"
92119998Sgad#include "extern.h"
9343071Swollman
94111768Sgad/*
95111768Sgad * Bit-values for the 'flags' parsed from a config-file entry.
96111768Sgad */
97175837Sdelphij#define	CE_COMPACT	0x0001	/* Compact the archived log files with gzip. */
98175837Sdelphij#define	CE_BZCOMPACT	0x0002	/* Compact the archived log files with bzip2. */
99130045Sgad#define	CE_BINARY	0x0008	/* Logfile is in binary, do not add status */
100111768Sgad				/*    messages to logfile(s) when rotating. */
101130045Sgad#define	CE_NOSIGNAL	0x0010	/* There is no process to signal when */
102111768Sgad				/*    trimming this file. */
103130045Sgad#define	CE_TRIMAT	0x0020	/* trim file at a specific time. */
104130045Sgad#define	CE_GLOB		0x0040	/* name of the log is file name pattern. */
105130045Sgad#define	CE_SIGNALGROUP	0x0080	/* Signal a process-group instead of a single */
106112003Sgad				/*    process when trimming this file. */
107130045Sgad#define	CE_CREATE	0x0100	/* Create the log file if it does not exist. */
108130043Sgad#define	CE_NODUMP	0x0200	/* Set 'nodump' on newly created log file. */
10943071Swollman
110130045Sgad#define	MIN_PID         5	/* Don't touch pids lower than this */
111130045Sgad#define	MAX_PID		99999	/* was lower, see /usr/include/sys/proc.h */
112111781Sgad
113130045Sgad#define	kbytes(size)  (((size) + 1023) >> 10)
114111781Sgad
115130165Sgad#define	DEFAULT_MARKER	"<default>"
116130165Sgad#define	DEBUG_MARKER	"<debug>"
117208649Sgordon#define	INCLUDE_MARKER	"<include>"
118210372Ssimon#define	DEFAULT_TIMEFNAME_FMT	"%Y%m%dT%H%M%S"
119130165Sgad
120210372Ssimon#define	MAX_OLDLOGS 65536	/* Default maximum number of old logfiles */
121210372Ssimon
12213244Sgraichenstruct conf_entry {
123208648Sgordon	STAILQ_ENTRY(conf_entry) cf_nextp;
12459003Shm	char *log;		/* Name of the log */
12559003Shm	char *pid_file;		/* PID file */
126111772Sgad	char *r_reason;		/* The reason this file is being rotated */
127114137Sgad	int firstcreate;	/* Creating log for the first time (-C). */
128111772Sgad	int rotate;		/* Non-zero if this file should be rotated */
129130165Sgad	int fsize;		/* size found for the log file */
130111779Sgad	uid_t uid;		/* Owner of log */
131111779Sgad	gid_t gid;		/* Group of log */
13259003Shm	int numlogs;		/* Number of logs to keep */
133130165Sgad	int trsize;		/* Size cutoff to trigger trimming the log */
13459003Shm	int hours;		/* Hours between log trimming */
135120361Sgad	struct ptime_data *trim_at;	/* Specific time to do trimming */
136139655Sdelphij	unsigned int permissions;	/* File permissions on the log */
13780646Sobrien	int flags;		/* CE_COMPACT, CE_BZCOMPACT, CE_BINARY */
13859003Shm	int sig;		/* Signal to send */
139111388Sgad	int def_cfg;		/* Using the <default> rule for this file */
14013244Sgraichen};
14113244Sgraichen
142130167Sgadstruct sigwork_entry {
143130167Sgad	SLIST_ENTRY(sigwork_entry) sw_nextp;
144130167Sgad	int	 sw_signum;		/* the signal to send */
145130167Sgad	int	 sw_pidok;		/* true if pid value is valid */
146130167Sgad	pid_t	 sw_pid;		/* the process id from the PID file */
147130167Sgad	const char *sw_pidtype;		/* "daemon" or "process group" */
148130167Sgad	char	 sw_fname[1];		/* file the PID was read from */
149130167Sgad};
150130167Sgad
151130167Sgadstruct zipwork_entry {
152130167Sgad	SLIST_ENTRY(zipwork_entry) zw_nextp;
153130167Sgad	const struct conf_entry *zw_conf;	/* for chown/perm/flag info */
154130167Sgad	const struct sigwork_entry *zw_swork;	/* to know success of signal */
155130167Sgad	int	 zw_fsize;		/* size of the file to compress */
156130167Sgad	char	 zw_fname[1];		/* the file to compress */
157130167Sgad};
158130167Sgad
159208649Sgordonstruct include_entry {
160208649Sgordon	STAILQ_ENTRY(include_entry) inc_nextp;
161208649Sgordon	const char *file;	/* Name of file to process */
162208649Sgordon};
163208649Sgordon
164210372Ssimonstruct oldlog_entry {
165210372Ssimon	char *fname;		/* Filename of the log file */
166210372Ssimon	time_t t;		/* Parses timestamp of the logfile */
167210372Ssimon};
168210372Ssimon
169130165Sgadtypedef enum {
170130165Sgad	FREE_ENT, KEEP_ENT
171130165Sgad}	fk_entry;
172111388Sgad
173208648SgordonSTAILQ_HEAD(cflist, conf_entry);
174130167SgadSLIST_HEAD(swlisthead, sigwork_entry) swhead = SLIST_HEAD_INITIALIZER(swhead);
175130167SgadSLIST_HEAD(zwlisthead, zipwork_entry) zwhead = SLIST_HEAD_INITIALIZER(zwhead);
176208649SgordonSTAILQ_HEAD(ilist, include_entry);
177130167Sgad
178120361Sgadint dbg_at_times;		/* -D Show details of 'trim_at' code */
179120361Sgad
18059004Shmint archtodir = 0;		/* Archive old logfiles to other directory */
181114137Sgadint createlogs;			/* Create (non-GLOB) logfiles which do not */
182114137Sgad				/*    already exist.  1=='for entries with */
183114137Sgad				/*    C flag', 2=='for all entries'. */
18459003Shmint verbose = 0;		/* Print out what's going on */
18559003Shmint needroot = 1;		/* Root privs are necessary */
18659003Shmint noaction = 0;		/* Don't do anything, just show it */
187143106Sbrooksint norotate = 0;		/* Don't rotate */
188111768Sgadint nosignal;			/* Do not send any signals */
189202668Sdelphijint enforcepid = 0;		/* If PID file does not exist or empty, do nothing */
19059003Shmint force = 0;			/* Force the trim no matter what */
191111772Sgadint rotatereq = 0;		/* -R = Always rotate the file(s) as given */
192111772Sgad				/*    on the command (this also requires   */
193111772Sgad				/*    that a list of files *are* given on  */
194111772Sgad				/*    the run command). */
195111772Sgadchar *requestor;		/* The name given on a -R request */
196210372Ssimonchar *timefnamefmt = NULL;	/* Use time based filenames instead of .0 etc */
19759004Shmchar *archdirname;		/* Directory path to old logfiles archive */
198136127Sbrookschar *destdir = NULL;		/* Directory to treat at root for logs */
199111773Sgadconst char *conf;		/* Configuration file to use */
20059003Shm
201120361Sgadstruct ptime_data *dbg_timenow;	/* A "timenow" value set via -D option */
202120361Sgadstruct ptime_data *timenow;	/* The time to use for checking at-fields */
203120361Sgad
204159998Sgad#define	DAYTIME_LEN	16
205159998Sgadchar daytime[DAYTIME_LEN];	/* The current time in human readable form,
206159998Sgad				 * used for rotation-tracking messages. */
20771299Sjedgarchar hostname[MAXHOSTNAMELEN];	/* hostname */
20813244Sgraichen
209208648Sgordonstatic struct cflist *get_worklist(char **files);
210208648Sgordonstatic void parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p,
211208649Sgordon		    struct conf_entry *defconf_p, struct ilist *inclist);
212208649Sgordonstatic void add_to_queue(const char *fname, struct ilist *inclist);
21316240Salexstatic char *sob(char *p);
21416240Salexstatic char *son(char *p);
215119102Sgadstatic int isnumberstr(const char *);
216208649Sgordonstatic int isglobstr(const char *);
21759003Shmstatic char *missing_field(char *p, char *errline);
218130165Sgadstatic void	 change_attrs(const char *, const struct conf_entry *);
219130165Sgadstatic fk_entry	 do_entry(struct conf_entry *);
220130165Sgadstatic fk_entry	 do_rotate(const struct conf_entry *);
221130167Sgadstatic void	 do_sigwork(struct sigwork_entry *);
222130167Sgadstatic void	 do_zipwork(struct zipwork_entry *);
223130167Sgadstatic struct sigwork_entry *
224130167Sgad		 save_sigwork(const struct conf_entry *);
225130167Sgadstatic struct zipwork_entry *
226130167Sgad		 save_zipwork(const struct conf_entry *, const struct
227130167Sgad		    sigwork_entry *, int, const char *);
228130167Sgadstatic void	 set_swpid(struct sigwork_entry *, const struct conf_entry *);
229130165Sgadstatic int	 sizefile(const char *);
230208648Sgordonstatic void expand_globs(struct cflist *work_p, struct cflist *glob_p);
231208648Sgordonstatic void free_clist(struct cflist *list);
232111388Sgadstatic void free_entry(struct conf_entry *ent);
233111388Sgadstatic struct conf_entry *init_entry(const char *fname,
234111388Sgad		struct conf_entry *src_entry);
235111781Sgadstatic void parse_args(int argc, char **argv);
236119904Sgadstatic int parse_doption(const char *doption);
23780640Sobrienstatic void usage(void);
238119926Sgadstatic int log_trim(const char *logname, const struct conf_entry *log_ent);
23916240Salexstatic int age_old_log(char *file);
240130038Sgadstatic void savelog(char *from, char *to);
241114137Sgadstatic void createdir(const struct conf_entry *ent, char *dirpart);
242114137Sgadstatic void createlog(const struct conf_entry *ent);
24313244Sgraichen
244111768Sgad/*
245129975Sgad * All the following take a parameter of 'int', but expect values in the
246129975Sgad * range of unsigned char.  Define wrappers which take values of type 'char',
247129975Sgad * whether signed or unsigned, and ensure they end up in the right range.
248111768Sgad */
249129975Sgad#define	isdigitch(Anychar) isdigit((u_char)(Anychar))
250129975Sgad#define	isprintch(Anychar) isprint((u_char)(Anychar))
251129975Sgad#define	isspacech(Anychar) isspace((u_char)(Anychar))
252129975Sgad#define	tolowerch(Anychar) tolower((u_char)(Anychar))
253111768Sgad
25459004Shmint
25559004Shmmain(int argc, char **argv)
25613244Sgraichen{
257208648Sgordon	struct cflist *worklist;
258208648Sgordon	struct conf_entry *p;
259130167Sgad	struct sigwork_entry *stmp;
260130167Sgad	struct zipwork_entry *ztmp;
26125443Sache
262130167Sgad	SLIST_INIT(&swhead);
263130167Sgad	SLIST_INIT(&zwhead);
264130167Sgad
265111781Sgad	parse_args(argc, argv);
266111773Sgad	argc -= optind;
267111773Sgad	argv += optind;
268111773Sgad
26959003Shm	if (needroot && getuid() && geteuid())
27059003Shm		errx(1, "must have root privs");
271208648Sgordon	worklist = get_worklist(argv);
27259003Shm
273130165Sgad	/*
274130165Sgad	 * Rotate all the files which need to be rotated.  Note that
275130165Sgad	 * some users have *hundreds* of entries in newsyslog.conf!
276130165Sgad	 */
277208648Sgordon	while (!STAILQ_EMPTY(worklist)) {
278208648Sgordon		p = STAILQ_FIRST(worklist);
279208648Sgordon		STAILQ_REMOVE_HEAD(worklist, cf_nextp);
280208648Sgordon		if (do_entry(p) == FREE_ENT)
281208648Sgordon			free_entry(p);
28259003Shm	}
283130165Sgad
284130167Sgad	/*
285130167Sgad	 * Send signals to any processes which need a signal to tell
286130167Sgad	 * them to close and re-open the log file(s) we have rotated.
287130167Sgad	 * Note that zipwork_entries include pointers to these
288130167Sgad	 * sigwork_entry's, so we can not free the entries here.
289130167Sgad	 */
290130167Sgad	if (!SLIST_EMPTY(&swhead)) {
291130204Sgad		if (noaction || verbose)
292130167Sgad			printf("Signal all daemon process(es)...\n");
293130167Sgad		SLIST_FOREACH(stmp, &swhead, sw_nextp)
294130167Sgad			do_sigwork(stmp);
295130204Sgad		if (noaction)
296130204Sgad			printf("\tsleep 10\n");
297130204Sgad		else {
298130204Sgad			if (verbose)
299130204Sgad				printf("Pause 10 seconds to allow daemon(s)"
300130204Sgad				    " to close log file(s)\n");
301130204Sgad			sleep(10);
302130204Sgad		}
303130167Sgad	}
304130167Sgad	/*
305130167Sgad	 * Compress all files that we're expected to compress, now
306130167Sgad	 * that all processes should have closed the files which
307130167Sgad	 * have been rotated.
308130167Sgad	 */
309130167Sgad	if (!SLIST_EMPTY(&zwhead)) {
310130204Sgad		if (noaction || verbose)
311130167Sgad			printf("Compress all rotated log file(s)...\n");
312130167Sgad		while (!SLIST_EMPTY(&zwhead)) {
313130167Sgad			ztmp = SLIST_FIRST(&zwhead);
314130167Sgad			do_zipwork(ztmp);
315130167Sgad			SLIST_REMOVE_HEAD(&zwhead, zw_nextp);
316130167Sgad			free(ztmp);
317130167Sgad		}
318130167Sgad	}
319130167Sgad	/* Now free all the sigwork entries. */
320130167Sgad	while (!SLIST_EMPTY(&swhead)) {
321130167Sgad		stmp = SLIST_FIRST(&swhead);
322130167Sgad		SLIST_REMOVE_HEAD(&swhead, sw_nextp);
323130167Sgad		free(stmp);
324130167Sgad	}
325130167Sgad
32695999Smaxim	while (wait(NULL) > 0 || errno == EINTR)
32795999Smaxim		;
32859003Shm	return (0);
32913244Sgraichen}
33013244Sgraichen
331111388Sgadstatic struct conf_entry *
332111388Sgadinit_entry(const char *fname, struct conf_entry *src_entry)
333111388Sgad{
334111388Sgad	struct conf_entry *tempwork;
335111388Sgad
336111388Sgad	if (verbose > 4)
337111388Sgad		printf("\t--> [creating entry for %s]\n", fname);
338111388Sgad
339111388Sgad	tempwork = malloc(sizeof(struct conf_entry));
340111388Sgad	if (tempwork == NULL)
341111388Sgad		err(1, "malloc of conf_entry for %s", fname);
342111388Sgad
343136174Sbrooks	if (destdir == NULL || fname[0] != '/')
344136127Sbrooks		tempwork->log = strdup(fname);
345136127Sbrooks	else
346136127Sbrooks		asprintf(&tempwork->log, "%s%s", destdir, fname);
347111388Sgad	if (tempwork->log == NULL)
348111388Sgad		err(1, "strdup for %s", fname);
349111388Sgad
350111388Sgad	if (src_entry != NULL) {
351111388Sgad		tempwork->pid_file = NULL;
352111388Sgad		if (src_entry->pid_file)
353111388Sgad			tempwork->pid_file = strdup(src_entry->pid_file);
354111772Sgad		tempwork->r_reason = NULL;
355114137Sgad		tempwork->firstcreate = 0;
356111772Sgad		tempwork->rotate = 0;
357130165Sgad		tempwork->fsize = -1;
358111388Sgad		tempwork->uid = src_entry->uid;
359111388Sgad		tempwork->gid = src_entry->gid;
360111388Sgad		tempwork->numlogs = src_entry->numlogs;
361130165Sgad		tempwork->trsize = src_entry->trsize;
362111388Sgad		tempwork->hours = src_entry->hours;
363120361Sgad		tempwork->trim_at = NULL;
364120361Sgad		if (src_entry->trim_at != NULL)
365120361Sgad			tempwork->trim_at = ptime_init(src_entry->trim_at);
366111388Sgad		tempwork->permissions = src_entry->permissions;
367111388Sgad		tempwork->flags = src_entry->flags;
368111388Sgad		tempwork->sig = src_entry->sig;
369111388Sgad		tempwork->def_cfg = src_entry->def_cfg;
370111388Sgad	} else {
371111388Sgad		/* Initialize as a "do-nothing" entry */
372111388Sgad		tempwork->pid_file = NULL;
373111772Sgad		tempwork->r_reason = NULL;
374114137Sgad		tempwork->firstcreate = 0;
375111772Sgad		tempwork->rotate = 0;
376130165Sgad		tempwork->fsize = -1;
377111779Sgad		tempwork->uid = (uid_t)-1;
378111779Sgad		tempwork->gid = (gid_t)-1;
379111388Sgad		tempwork->numlogs = 1;
380130165Sgad		tempwork->trsize = -1;
381111388Sgad		tempwork->hours = -1;
382120361Sgad		tempwork->trim_at = NULL;
383111388Sgad		tempwork->permissions = 0;
384111388Sgad		tempwork->flags = 0;
385111388Sgad		tempwork->sig = SIGHUP;
386111388Sgad		tempwork->def_cfg = 0;
387111388Sgad	}
388111388Sgad
389111388Sgad	return (tempwork);
390111388Sgad}
391111388Sgad
39259004Shmstatic void
393111388Sgadfree_entry(struct conf_entry *ent)
394111388Sgad{
395111388Sgad
396111388Sgad	if (ent == NULL)
397111388Sgad		return;
398111388Sgad
399111388Sgad	if (ent->log != NULL) {
400111388Sgad		if (verbose > 4)
401111388Sgad			printf("\t--> [freeing entry for %s]\n", ent->log);
402111388Sgad		free(ent->log);
403111388Sgad		ent->log = NULL;
404111388Sgad	}
405111388Sgad
406111388Sgad	if (ent->pid_file != NULL) {
407111388Sgad		free(ent->pid_file);
408111388Sgad		ent->pid_file = NULL;
409111388Sgad	}
410111388Sgad
411111772Sgad	if (ent->r_reason != NULL) {
412111772Sgad		free(ent->r_reason);
413111772Sgad		ent->r_reason = NULL;
414111772Sgad	}
415111772Sgad
416120361Sgad	if (ent->trim_at != NULL) {
417120361Sgad		ptime_free(ent->trim_at);
418120361Sgad		ent->trim_at = NULL;
419120361Sgad	}
420120361Sgad
421111388Sgad	free(ent);
422111388Sgad}
423111388Sgad
424111388Sgadstatic void
425208648Sgordonfree_clist(struct cflist *list)
426112020Sgad{
427208648Sgordon	struct conf_entry *ent;
428112020Sgad
429208648Sgordon	while (!STAILQ_EMPTY(list)) {
430208648Sgordon		ent = STAILQ_FIRST(list);
431208648Sgordon		STAILQ_REMOVE_HEAD(list, cf_nextp);
432112020Sgad		free_entry(ent);
433112020Sgad	}
434208648Sgordon
435208648Sgordon	free(list);
436208648Sgordon	list = NULL;
437112020Sgad}
438112020Sgad
439130165Sgadstatic fk_entry
44059004Shmdo_entry(struct conf_entry * ent)
44113244Sgraichen{
442130045Sgad#define	REASON_MAX	80
443130165Sgad	int modtime;
444130165Sgad	fk_entry free_or_keep;
445120361Sgad	double diffsecs;
446111772Sgad	char temp_reason[REASON_MAX];
44759003Shm
448130165Sgad	free_or_keep = FREE_ENT;
44959003Shm	if (verbose) {
45059003Shm		if (ent->flags & CE_COMPACT)
45159003Shm			printf("%s <%dZ>: ", ent->log, ent->numlogs);
45280638Sobrien		else if (ent->flags & CE_BZCOMPACT)
45380638Sobrien			printf("%s <%dJ>: ", ent->log, ent->numlogs);
45459003Shm		else
45559003Shm			printf("%s <%d>: ", ent->log, ent->numlogs);
45659003Shm	}
457130165Sgad	ent->fsize = sizefile(ent->log);
45859003Shm	modtime = age_old_log(ent->log);
459111772Sgad	ent->rotate = 0;
460114137Sgad	ent->firstcreate = 0;
461130165Sgad	if (ent->fsize < 0) {
462114137Sgad		/*
463114137Sgad		 * If either the C flag or the -C option was specified,
464114137Sgad		 * and if we won't be creating the file, then have the
465114137Sgad		 * verbose message include a hint as to why the file
466114137Sgad		 * will not be created.
467114137Sgad		 */
468114137Sgad		temp_reason[0] = '\0';
469114137Sgad		if (createlogs > 1)
470114137Sgad			ent->firstcreate = 1;
471114137Sgad		else if ((ent->flags & CE_CREATE) && createlogs)
472114137Sgad			ent->firstcreate = 1;
473114137Sgad		else if (ent->flags & CE_CREATE)
474159998Sgad			strlcpy(temp_reason, " (no -C option)", REASON_MAX);
475114137Sgad		else if (createlogs)
476159998Sgad			strlcpy(temp_reason, " (no C flag)", REASON_MAX);
477114137Sgad
478114137Sgad		if (ent->firstcreate) {
479114137Sgad			if (verbose)
480114137Sgad				printf("does not exist -> will create.\n");
481114137Sgad			createlog(ent);
482114137Sgad		} else if (verbose) {
483114137Sgad			printf("does not exist, skipped%s.\n", temp_reason);
484114137Sgad		}
48559003Shm	} else {
486111772Sgad		if (ent->flags & CE_TRIMAT && !force && !rotatereq) {
487120361Sgad			diffsecs = ptimeget_diff(timenow, ent->trim_at);
488120361Sgad			if (diffsecs < 0.0) {
489120361Sgad				/* trim_at is some time in the future. */
490120361Sgad				if (verbose) {
491120361Sgad					ptime_adjust4dst(ent->trim_at,
492120361Sgad					    timenow);
49343071Swollman					printf("--> will trim at %s",
494120361Sgad					    ptimeget_ctime(ent->trim_at));
495120361Sgad				}
496130165Sgad				return (free_or_keep);
497120361Sgad			} else if (diffsecs >= 3600.0) {
498120361Sgad				/*
499120361Sgad				 * trim_at is more than an hour in the past,
500120361Sgad				 * so find the next valid trim_at time, and
501120361Sgad				 * tell the user what that will be.
502120361Sgad				 */
503120361Sgad				if (verbose && dbg_at_times)
504120361Sgad					printf("\n\t--> prev trim at %s\t",
505120361Sgad					    ptimeget_ctime(ent->trim_at));
506120361Sgad				if (verbose) {
507120361Sgad					ptimeset_nxtime(ent->trim_at);
508120361Sgad					printf("--> will trim at %s",
509120361Sgad					    ptimeget_ctime(ent->trim_at));
510120361Sgad				}
511130165Sgad				return (free_or_keep);
512120361Sgad			} else if (verbose && noaction && dbg_at_times) {
513120361Sgad				/*
514120361Sgad				 * If we are just debugging at-times, then
515120361Sgad				 * a detailed message is helpful.  Also
516120361Sgad				 * skip "doing" any commands, since they
517120361Sgad				 * would all be turned off by no-action.
518120361Sgad				 */
519120361Sgad				printf("\n\t--> timematch at %s",
520120361Sgad				    ptimeget_ctime(ent->trim_at));
521130165Sgad				return (free_or_keep);
52243071Swollman			} else if (verbose && ent->hours <= 0) {
52343071Swollman				printf("--> time is up\n");
52443071Swollman			}
52543071Swollman		}
526130165Sgad		if (verbose && (ent->trsize > 0))
527130165Sgad			printf("size (Kb): %d [%d] ", ent->fsize, ent->trsize);
52859003Shm		if (verbose && (ent->hours > 0))
52959003Shm			printf(" age (hr): %d [%d] ", modtime, ent->hours);
530111772Sgad
531111772Sgad		/*
532111772Sgad		 * Figure out if this logfile needs to be rotated.
533111772Sgad		 */
534111772Sgad		temp_reason[0] = '\0';
535111772Sgad		if (rotatereq) {
536111772Sgad			ent->rotate = 1;
537111772Sgad			snprintf(temp_reason, REASON_MAX, " due to -R from %s",
538111772Sgad			    requestor);
539111772Sgad		} else if (force) {
540111772Sgad			ent->rotate = 1;
541111772Sgad			snprintf(temp_reason, REASON_MAX, " due to -F request");
542130165Sgad		} else if ((ent->trsize > 0) && (ent->fsize >= ent->trsize)) {
543111772Sgad			ent->rotate = 1;
544111772Sgad			snprintf(temp_reason, REASON_MAX, " due to size>%dK",
545130165Sgad			    ent->trsize);
546111772Sgad		} else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) {
547111772Sgad			ent->rotate = 1;
548111772Sgad		} else if ((ent->hours > 0) && ((modtime >= ent->hours) ||
549111772Sgad		    (modtime < 0))) {
550111772Sgad			ent->rotate = 1;
551111772Sgad		}
552111772Sgad
553111772Sgad		/*
554111772Sgad		 * If the file needs to be rotated, then rotate it.
555111772Sgad		 */
556143106Sbrooks		if (ent->rotate && !norotate) {
557111772Sgad			if (temp_reason[0] != '\0')
558111772Sgad				ent->r_reason = strdup(temp_reason);
55959003Shm			if (verbose)
56059003Shm				printf("--> trimming log....\n");
56159003Shm			if (noaction && !verbose) {
56259003Shm				if (ent->flags & CE_COMPACT)
56359003Shm					printf("%s <%dZ>: trimming\n",
56459003Shm					    ent->log, ent->numlogs);
56580638Sobrien				else if (ent->flags & CE_BZCOMPACT)
56680638Sobrien					printf("%s <%dJ>: trimming\n",
56780638Sobrien					    ent->log, ent->numlogs);
56859003Shm				else
56959003Shm					printf("%s <%d>: trimming\n",
57059003Shm					    ent->log, ent->numlogs);
57159003Shm			}
572130165Sgad			free_or_keep = do_rotate(ent);
57359003Shm		} else {
57459003Shm			if (verbose)
57559003Shm				printf("--> skipping\n");
57659003Shm		}
57759003Shm	}
578130165Sgad	return (free_or_keep);
579111772Sgad#undef REASON_MAX
58013244Sgraichen}
58113244Sgraichen
58259004Shmstatic void
583111781Sgadparse_args(int argc, char **argv)
58413244Sgraichen{
585111781Sgad	int ch;
58659003Shm	char *p;
58713244Sgraichen
588120361Sgad	timenow = ptime_init(NULL);
589120361Sgad	ptimeset_time(timenow, time(NULL));
590159998Sgad	strlcpy(daytime, ptimeget_ctime(timenow) + 4, DAYTIME_LEN);
59113244Sgraichen
59259003Shm	/* Let's get our hostname */
593111781Sgad	(void)gethostname(hostname, sizeof(hostname));
59413244Sgraichen
59513244Sgraichen	/* Truncate domain */
596111781Sgad	if ((p = strchr(hostname, '.')) != NULL)
59713244Sgraichen		*p = '\0';
598111768Sgad
599111768Sgad	/* Parse command line options. */
600210372Ssimon	while ((ch = getopt(argc, argv, "a:d:f:nrst:vCD:FNPR:")) != -1)
601111781Sgad		switch (ch) {
60259004Shm		case 'a':
60359004Shm			archtodir++;
60459004Shm			archdirname = optarg;
60559004Shm			break;
606136127Sbrooks		case 'd':
607136127Sbrooks			destdir = optarg;
608136127Sbrooks			break;
609111768Sgad		case 'f':
610111768Sgad			conf = optarg;
611111768Sgad			break;
612111768Sgad		case 'n':
613111768Sgad			noaction++;
614111768Sgad			break;
61559003Shm		case 'r':
61659003Shm			needroot = 0;
61759003Shm			break;
618111768Sgad		case 's':
619111768Sgad			nosignal = 1;
620111768Sgad			break;
621210372Ssimon		case 't':
622210372Ssimon			if (optarg[0] == '\0' ||
623210372Ssimon			    strcmp(optarg, "DEFAULT") == 0)
624210372Ssimon				timefnamefmt = strdup(DEFAULT_TIMEFNAME_FMT);
625210372Ssimon			else
626210372Ssimon				timefnamefmt = strdup(optarg);
627210372Ssimon			break;
62859003Shm		case 'v':
62959003Shm			verbose++;
63059003Shm			break;
631114137Sgad		case 'C':
632114137Sgad			/* Useful for things like rc.diskless... */
633114137Sgad			createlogs++;
634114137Sgad			break;
635119904Sgad		case 'D':
636119904Sgad			/*
637119904Sgad			 * Set some debugging option.  The specific option
638119904Sgad			 * depends on the value of optarg.  These options
639119904Sgad			 * may come and go without notice or documentation.
640119904Sgad			 */
641119904Sgad			if (parse_doption(optarg))
642119904Sgad				break;
643119904Sgad			usage();
644119904Sgad			/* NOTREACHED */
64534584Spst		case 'F':
64634584Spst			force++;
64734584Spst			break;
648143106Sbrooks		case 'N':
649143106Sbrooks			norotate++;
650143106Sbrooks			break;
651202668Sdelphij		case 'P':
652202668Sdelphij			enforcepid++;
653202668Sdelphij			break;
654111772Sgad		case 'R':
655111772Sgad			rotatereq++;
656111772Sgad			requestor = strdup(optarg);
657111772Sgad			break;
658111781Sgad		case 'm':	/* Used by OpenBSD for "monitor mode" */
65959003Shm		default:
66059003Shm			usage();
661111768Sgad			/* NOTREACHED */
66259003Shm		}
663111772Sgad
664143106Sbrooks	if (force && norotate) {
665143106Sbrooks		warnx("Only one of -F and -N may be specified.");
666143106Sbrooks		usage();
667143106Sbrooks		/* NOTREACHED */
668143106Sbrooks	}
669143106Sbrooks
670111772Sgad	if (rotatereq) {
671111772Sgad		if (optind == argc) {
672111772Sgad			warnx("At least one filename must be given when -R is specified.");
673111772Sgad			usage();
674111772Sgad			/* NOTREACHED */
675111772Sgad		}
676111772Sgad		/* Make sure "requestor" value is safe for a syslog message. */
677111772Sgad		for (p = requestor; *p != '\0'; p++) {
678111772Sgad			if (!isprintch(*p) && (*p != '\t'))
679111772Sgad				*p = '.';
680111772Sgad		}
681111772Sgad	}
682119904Sgad
683119904Sgad	if (dbg_timenow) {
684119904Sgad		/*
685119904Sgad		 * Note that the 'daytime' variable is not changed.
686119904Sgad		 * That is only used in messages that track when a
687119904Sgad		 * logfile is rotated, and if a file *is* rotated,
688119904Sgad		 * then it will still rotated at the "real now" time.
689119904Sgad		 */
690120361Sgad		ptime_free(timenow);
691119904Sgad		timenow = dbg_timenow;
692119904Sgad		fprintf(stderr, "Debug: Running as if TimeNow is %s",
693120361Sgad		    ptimeget_ctime(dbg_timenow));
694119904Sgad	}
695119904Sgad
69643071Swollman}
69713244Sgraichen
698120361Sgad/*
699120361Sgad * These debugging options are mainly meant for developer use, such
700120361Sgad * as writing regression-tests.  They would not be needed by users
701120361Sgad * during normal operation of newsyslog...
702120361Sgad */
703119904Sgadstatic int
704119904Sgadparse_doption(const char *doption)
705119904Sgad{
706119904Sgad	const char TN[] = "TN=";
707120361Sgad	int res;
708119904Sgad
709119904Sgad	if (strncmp(doption, TN, sizeof(TN) - 1) == 0) {
710119904Sgad		/*
711120361Sgad		 * The "TimeNow" debugging option.  This might be off
712120361Sgad		 * by an hour when crossing a timezone change.
713119904Sgad		 */
714120361Sgad		dbg_timenow = ptime_init(NULL);
715120361Sgad		res = ptime_relparse(dbg_timenow, PTM_PARSE_ISO8601,
716120361Sgad		    time(NULL), doption + sizeof(TN) - 1);
717120361Sgad		if (res == -2) {
718120361Sgad			warnx("Non-existent time specified on -D %s", doption);
719120361Sgad			return (0);			/* failure */
720120361Sgad		} else if (res < 0) {
721119904Sgad			warnx("Malformed time given on -D %s", doption);
722119904Sgad			return (0);			/* failure */
723119904Sgad		}
724119904Sgad		return (1);			/* successfully parsed */
725119904Sgad
726119904Sgad	}
727119904Sgad
728120361Sgad	if (strcmp(doption, "ats") == 0) {
729120361Sgad		dbg_at_times++;
730120361Sgad		return (1);			/* successfully parsed */
731120361Sgad	}
732120361Sgad
733159968Sgad	/* XXX - This check could probably be dropped. */
734159968Sgad	if ((strcmp(doption, "neworder") == 0) || (strcmp(doption, "oldorder")
735159968Sgad	    == 0)) {
736159968Sgad		warnx("NOTE: newsyslog always uses 'neworder'.");
737130167Sgad		return (1);			/* successfully parsed */
738130167Sgad	}
739130167Sgad
740130165Sgad	warnx("Unknown -D (debug) option: '%s'", doption);
741119904Sgad	return (0);				/* failure */
742119904Sgad}
743119904Sgad
74459004Shmstatic void
74559004Shmusage(void)
74613244Sgraichen{
74780646Sobrien
74880646Sobrien	fprintf(stderr,
749143106Sbrooks	    "usage: newsyslog [-CFNnrsv] [-a directory] [-d directory] [-f config-file]\n"
750210372Ssimon	    "                 [-t timefmt ] [ [-R requestor] filename ... ]\n");
75159003Shm	exit(1);
75213244Sgraichen}
75313244Sgraichen
75459004Shm/*
755111773Sgad * Parse a configuration file and return a linked list of all the logs
756111773Sgad * which should be processed.
75713244Sgraichen */
758208648Sgordonstatic struct cflist *
759111773Sgadget_worklist(char **files)
76013244Sgraichen{
76159003Shm	FILE *f;
762111773Sgad	char **given;
763208649Sgordon	struct cflist *cmdlist, *filelist, *globlist;
764208648Sgordon	struct conf_entry *defconf, *dupent, *ent;
765208649Sgordon	struct ilist inclist;
766208649Sgordon	struct include_entry *inc;
767112020Sgad	int gmatch, fnres;
768111773Sgad
769208648Sgordon	defconf = NULL;
770208649Sgordon	STAILQ_INIT(&inclist);
771111773Sgad
772208648Sgordon	filelist = malloc(sizeof(struct cflist));
773208648Sgordon	if (filelist == NULL)
774208648Sgordon		err(1, "malloc of filelist");
775208648Sgordon	STAILQ_INIT(filelist);
776208648Sgordon	globlist = malloc(sizeof(struct cflist));
777208648Sgordon	if (globlist == NULL)
778208648Sgordon		err(1, "malloc of globlist");
779208648Sgordon	STAILQ_INIT(globlist);
780208648Sgordon
781208649Sgordon	inc = malloc(sizeof(struct include_entry));
782208649Sgordon	if (inc == NULL)
783208649Sgordon		err(1, "malloc of inc");
784208649Sgordon	inc->file = conf;
785208649Sgordon	if (inc->file == NULL)
786208649Sgordon		inc->file = _PATH_CONF;
787208649Sgordon	STAILQ_INSERT_TAIL(&inclist, inc, inc_nextp);
788111773Sgad
789208649Sgordon	STAILQ_FOREACH(inc, &inclist, inc_nextp) {
790208649Sgordon		if (strcmp(inc->file, "-") != 0)
791208649Sgordon			f = fopen(inc->file, "r");
792208649Sgordon		else {
793208649Sgordon			f = stdin;
794208649Sgordon			inc->file = "<stdin>";
795208649Sgordon		}
796208649Sgordon		if (!f)
797208649Sgordon			err(1, "%s", inc->file);
798208649Sgordon
799208649Sgordon		if (verbose)
800208649Sgordon			printf("Processing %s\n", inc->file);
801208649Sgordon		parse_file(f, filelist, globlist, defconf, &inclist);
802208649Sgordon		(void) fclose(f);
803111773Sgad	}
804111773Sgad
805111773Sgad	/*
806111773Sgad	 * All config-file information has been read in and turned into
807208648Sgordon	 * a filelist and a globlist.  If there were no specific files
808112020Sgad	 * given on the run command, then the only thing left to do is to
809112020Sgad	 * call a routine which finds all files matched by the globlist
810208648Sgordon	 * and adds them to the filelist.  Then return the worklist.
811111773Sgad	 */
812111773Sgad	if (*files == NULL) {
813208648Sgordon		expand_globs(filelist, globlist);
814208648Sgordon		free_clist(globlist);
815111773Sgad		if (defconf != NULL)
816111773Sgad			free_entry(defconf);
817208648Sgordon		return (filelist);
818111773Sgad		/* NOTREACHED */
819111773Sgad	}
820111773Sgad
821111773Sgad	/*
822111773Sgad	 * If newsyslog was given a specific list of files to process,
823111773Sgad	 * it may be that some of those files were not listed in any
824111773Sgad	 * config file.  Those unlisted files should get the default
825111773Sgad	 * rotation action.  First, create the default-rotation action
826111773Sgad	 * if none was found in a system config file.
827111773Sgad	 */
828111773Sgad	if (defconf == NULL) {
829111773Sgad		defconf = init_entry(DEFAULT_MARKER, NULL);
830111773Sgad		defconf->numlogs = 3;
831130165Sgad		defconf->trsize = 50;
832111773Sgad		defconf->permissions = S_IRUSR|S_IWUSR;
833111773Sgad	}
834111773Sgad
835111773Sgad	/*
836111773Sgad	 * If newsyslog was run with a list of specific filenames,
837111773Sgad	 * then create a new worklist which has only those files in
838111773Sgad	 * it, picking up the rotation-rules for those files from
839208648Sgordon	 * the original filelist.
840111773Sgad	 *
841111773Sgad	 * XXX - Note that this will copy multiple rules for a single
842111773Sgad	 *	logfile, if multiple entries are an exact match for
843111773Sgad	 *	that file.  That matches the historic behavior, but do
844111773Sgad	 *	we want to continue to allow it?  If so, it should
845111773Sgad	 *	probably be handled more intelligently.
846111773Sgad	 */
847208648Sgordon	cmdlist = malloc(sizeof(struct cflist));
848208648Sgordon	if (cmdlist == NULL)
849208648Sgordon		err(1, "malloc of cmdlist");
850208648Sgordon	STAILQ_INIT(cmdlist);
851208648Sgordon
852111773Sgad	for (given = files; *given; ++given) {
853111773Sgad		/*
854111773Sgad		 * First try to find exact-matches for this given file.
855111773Sgad		 */
856112020Sgad		gmatch = 0;
857208648Sgordon		STAILQ_FOREACH(ent, filelist, cf_nextp) {
858111773Sgad			if (strcmp(ent->log, *given) == 0) {
859111773Sgad				gmatch++;
860111773Sgad				dupent = init_entry(*given, ent);
861208648Sgordon				STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp);
862111773Sgad			}
863111773Sgad		}
864111773Sgad		if (gmatch) {
865111773Sgad			if (verbose > 2)
866111773Sgad				printf("\t+ Matched entry %s\n", *given);
867111773Sgad			continue;
868111773Sgad		}
869111773Sgad
870111773Sgad		/*
871111773Sgad		 * There was no exact-match for this given file, so look
872111773Sgad		 * for a "glob" entry which does match.
873111773Sgad		 */
874112020Sgad		gmatch = 0;
875112020Sgad		if (verbose > 2 && globlist != NULL)
876112020Sgad			printf("\t+ Checking globs for %s\n", *given);
877208648Sgordon		STAILQ_FOREACH(ent, globlist, cf_nextp) {
878112020Sgad			fnres = fnmatch(ent->log, *given, FNM_PATHNAME);
879112020Sgad			if (verbose > 2)
880112020Sgad				printf("\t+    = %d for pattern %s\n", fnres,
881112020Sgad				    ent->log);
882112020Sgad			if (fnres == 0) {
883111773Sgad				gmatch++;
884111773Sgad				dupent = init_entry(*given, ent);
885112020Sgad				/* This new entry is not a glob! */
886111773Sgad				dupent->flags &= ~CE_GLOB;
887208648Sgordon				STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp);
888111773Sgad				/* Only allow a match to one glob-entry */
889111773Sgad				break;
890111773Sgad			}
891111773Sgad		}
892111773Sgad		if (gmatch) {
893111773Sgad			if (verbose > 2)
894111773Sgad				printf("\t+ Matched %s via %s\n", *given,
895111773Sgad				    ent->log);
896111773Sgad			continue;
897111773Sgad		}
898111773Sgad
899111773Sgad		/*
900111773Sgad		 * This given file was not found in any config file, so
901111773Sgad		 * add a worklist item based on the default entry.
902111773Sgad		 */
903111773Sgad		if (verbose > 2)
904111773Sgad			printf("\t+ No entry matched %s  (will use %s)\n",
905111773Sgad			    *given, DEFAULT_MARKER);
906111773Sgad		dupent = init_entry(*given, defconf);
907111773Sgad		/* Mark that it was *not* found in a config file */
908111773Sgad		dupent->def_cfg = 1;
909208648Sgordon		STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp);
910111773Sgad	}
911111773Sgad
912111773Sgad	/*
913112020Sgad	 * Free all the entries in the original work list, the list of
914112020Sgad	 * glob entries, and the default entry.
915111773Sgad	 */
916208648Sgordon	free_clist(filelist);
917208648Sgordon	free_clist(globlist);
918112020Sgad	free_entry(defconf);
919111773Sgad
920112020Sgad	/* And finally, return a worklist which matches the given files. */
921208648Sgordon	return (cmdlist);
922111773Sgad}
923111773Sgad
924111773Sgad/*
925112020Sgad * Expand the list of entries with filename patterns, and add all files
926112020Sgad * which match those glob-entries onto the worklist.
927112020Sgad */
928112020Sgadstatic void
929208648Sgordonexpand_globs(struct cflist *work_p, struct cflist *glob_p)
930112020Sgad{
931161412Sdelphij	int gmatch, gres;
932161412Sdelphij	size_t i;
933112020Sgad	char *mfname;
934208648Sgordon	struct conf_entry *dupent, *ent, *globent;
935112020Sgad	glob_t pglob;
936112020Sgad	struct stat st_fm;
937112020Sgad
938112020Sgad	/*
939112020Sgad	 * The worklist contains all fully-specified (non-GLOB) names.
940112020Sgad	 *
941112020Sgad	 * Now expand the list of filename-pattern (GLOB) entries into
942112020Sgad	 * a second list, which (by definition) will only match files
943112020Sgad	 * that already exist.  Do not add a glob-related entry for any
944112020Sgad	 * file which already exists in the fully-specified list.
945112020Sgad	 */
946208648Sgordon	STAILQ_FOREACH(globent, glob_p, cf_nextp) {
947112020Sgad		gres = glob(globent->log, GLOB_NOCHECK, NULL, &pglob);
948112020Sgad		if (gres != 0) {
949112020Sgad			warn("cannot expand pattern (%d): %s", gres,
950112020Sgad			    globent->log);
951112020Sgad			continue;
952112020Sgad		}
953112020Sgad
954112020Sgad		if (verbose > 2)
955112020Sgad			printf("\t+ Expanding pattern %s\n", globent->log);
956112020Sgad		for (i = 0; i < pglob.gl_matchc; i++) {
957112020Sgad			mfname = pglob.gl_pathv[i];
958112020Sgad
959112020Sgad			/* See if this file already has a specific entry. */
960112020Sgad			gmatch = 0;
961208648Sgordon			STAILQ_FOREACH(ent, work_p, cf_nextp) {
962112020Sgad				if (strcmp(mfname, ent->log) == 0) {
963112020Sgad					gmatch++;
964112020Sgad					break;
965112020Sgad				}
966112020Sgad			}
967112020Sgad			if (gmatch)
968112020Sgad				continue;
969112020Sgad
970112020Sgad			/* Make sure the named matched is a file. */
971112020Sgad			gres = lstat(mfname, &st_fm);
972112020Sgad			if (gres != 0) {
973112020Sgad				/* Error on a file that glob() matched?!? */
974112020Sgad				warn("Skipping %s - lstat() error", mfname);
975112020Sgad				continue;
976112020Sgad			}
977112020Sgad			if (!S_ISREG(st_fm.st_mode)) {
978112020Sgad				/* We only rotate files! */
979112020Sgad				if (verbose > 2)
980112020Sgad					printf("\t+  . skipping %s (!file)\n",
981112020Sgad					    mfname);
982112020Sgad				continue;
983112020Sgad			}
984112020Sgad
985112020Sgad			if (verbose > 2)
986112020Sgad				printf("\t+  . add file %s\n", mfname);
987112020Sgad			dupent = init_entry(mfname, globent);
988112020Sgad			/* This new entry is not a glob! */
989112020Sgad			dupent->flags &= ~CE_GLOB;
990208648Sgordon
991208648Sgordon			/* Add to the worklist. */
992208648Sgordon			STAILQ_INSERT_TAIL(work_p, dupent, cf_nextp);
993112020Sgad		}
994112020Sgad		globfree(&pglob);
995112020Sgad		if (verbose > 2)
996112020Sgad			printf("\t+ Done with pattern %s\n", globent->log);
997112020Sgad	}
998112020Sgad}
999112020Sgad
1000112020Sgad/*
1001111773Sgad * Parse a configuration file and update a linked list of all the logs to
1002111773Sgad * process.
1003111773Sgad */
1004111773Sgadstatic void
1005208648Sgordonparse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p,
1006208649Sgordon    struct conf_entry *defconf_p, struct ilist *inclist)
1007111773Sgad{
100859003Shm	char line[BUFSIZ], *parse, *q;
1009107737Ssobomax	char *cp, *errline, *group;
1010208648Sgordon	struct conf_entry *working;
1011111781Sgad	struct passwd *pwd;
101259003Shm	struct group *grp;
1013208649Sgordon	glob_t pglob;
1014120361Sgad	int eol, ptm_opts, res, special;
1015208649Sgordon	size_t i;
101613244Sgraichen
1017130165Sgad	errline = NULL;
1018111773Sgad	while (fgets(line, BUFSIZ, cf)) {
1019107737Ssobomax		if ((line[0] == '\n') || (line[0] == '#') ||
1020107737Ssobomax		    (strlen(line) == 0))
102159003Shm			continue;
1022130165Sgad		if (errline != NULL)
1023130165Sgad			free(errline);
102459003Shm		errline = strdup(line);
1025107737Ssobomax		for (cp = line + 1; *cp != '\0'; cp++) {
1026107737Ssobomax			if (*cp != '#')
1027107737Ssobomax				continue;
1028107737Ssobomax			if (*(cp - 1) == '\\') {
1029107737Ssobomax				strcpy(cp - 1, cp);
1030107737Ssobomax				cp--;
1031107737Ssobomax				continue;
1032107737Ssobomax			}
1033107737Ssobomax			*cp = '\0';
1034107737Ssobomax			break;
1035107737Ssobomax		}
103660373Sdes
103760373Sdes		q = parse = missing_field(sob(line), errline);
103860373Sdes		parse = son(line);
103960373Sdes		if (!*parse)
104080646Sobrien			errx(1, "malformed line (missing fields):\n%s",
104180646Sobrien			    errline);
104260373Sdes		*parse = '\0';
104360373Sdes
1044130165Sgad		/*
1045130165Sgad		 * Allow people to set debug options via the config file.
1046208648Sgordon		 * (NOTE: debug options are undocumented, and may disappear
1047130165Sgad		 * at any time, etc).
1048130165Sgad		 */
1049130165Sgad		if (strcasecmp(DEBUG_MARKER, q) == 0) {
1050130165Sgad			q = parse = missing_field(sob(++parse), errline);
1051130165Sgad			parse = son(parse);
1052130165Sgad			if (!*parse)
1053130165Sgad				warnx("debug line specifies no option:\n%s",
1054130165Sgad				    errline);
1055130165Sgad			else {
1056130165Sgad				*parse = '\0';
1057130165Sgad				parse_doption(q);
1058130165Sgad			}
1059130165Sgad			continue;
1060208649Sgordon		} else if (strcasecmp(INCLUDE_MARKER, q) == 0) {
1061208649Sgordon			if (verbose)
1062208649Sgordon				printf("Found: %s", errline);
1063208649Sgordon			q = parse = missing_field(sob(++parse), errline);
1064208649Sgordon			parse = son(parse);
1065208649Sgordon			if (!*parse) {
1066208649Sgordon				warnx("include line missing argument:\n%s",
1067208649Sgordon				    errline);
1068208649Sgordon				continue;
1069208649Sgordon			}
1070208649Sgordon
1071208649Sgordon			*parse = '\0';
1072208649Sgordon
1073208649Sgordon			if (isglobstr(q)) {
1074208649Sgordon				res = glob(q, GLOB_NOCHECK, NULL, &pglob);
1075208649Sgordon				if (res != 0) {
1076208649Sgordon					warn("cannot expand pattern (%d): %s",
1077208649Sgordon					    res, q);
1078208649Sgordon					continue;
1079208649Sgordon				}
1080208649Sgordon
1081208649Sgordon				if (verbose > 2)
1082208649Sgordon					printf("\t+ Expanding pattern %s\n", q);
1083208649Sgordon
1084208649Sgordon				for (i = 0; i < pglob.gl_matchc; i++)
1085208649Sgordon					add_to_queue(pglob.gl_pathv[i],
1086208649Sgordon					    inclist);
1087208649Sgordon				globfree(&pglob);
1088208649Sgordon			} else
1089208649Sgordon				add_to_queue(q, inclist);
1090208649Sgordon			continue;
1091130165Sgad		}
1092130165Sgad
1093112020Sgad		special = 0;
1094111388Sgad		working = init_entry(q, NULL);
1095111388Sgad		if (strcasecmp(DEFAULT_MARKER, q) == 0) {
1096112020Sgad			special = 1;
1097208648Sgordon			if (defconf_p != NULL) {
1098111388Sgad				warnx("Ignoring duplicate entry for %s!", q);
1099111388Sgad				free_entry(working);
1100111388Sgad				continue;
1101111388Sgad			}
1102208648Sgordon			defconf_p = working;
110359003Shm		}
110413244Sgraichen
110559003Shm		q = parse = missing_field(sob(++parse), errline);
110659003Shm		parse = son(parse);
110725518Sbrian		if (!*parse)
110880646Sobrien			errx(1, "malformed line (missing fields):\n%s",
110980646Sobrien			    errline);
111059003Shm		*parse = '\0';
111159003Shm		if ((group = strchr(q, ':')) != NULL ||
111259003Shm		    (group = strrchr(q, '.')) != NULL) {
111359003Shm			*group++ = '\0';
111459003Shm			if (*q) {
1115119102Sgad				if (!(isnumberstr(q))) {
1116111781Sgad					if ((pwd = getpwnam(q)) == NULL)
111759003Shm						errx(1,
111880646Sobrien				     "error in config file; unknown user:\n%s",
111959003Shm						    errline);
1120111781Sgad					working->uid = pwd->pw_uid;
112159003Shm				} else
112259003Shm					working->uid = atoi(q);
112359003Shm			} else
1124111779Sgad				working->uid = (uid_t)-1;
112513244Sgraichen
112659003Shm			q = group;
112759003Shm			if (*q) {
1128119102Sgad				if (!(isnumberstr(q))) {
112959003Shm					if ((grp = getgrnam(q)) == NULL)
113059003Shm						errx(1,
113180646Sobrien				    "error in config file; unknown group:\n%s",
113259003Shm						    errline);
113359003Shm					working->gid = grp->gr_gid;
113459003Shm				} else
113559003Shm					working->gid = atoi(q);
113659003Shm			} else
1137111779Sgad				working->gid = (gid_t)-1;
113813244Sgraichen
113959003Shm			q = parse = missing_field(sob(++parse), errline);
114059003Shm			parse = son(parse);
114159003Shm			if (!*parse)
114280646Sobrien				errx(1, "malformed line (missing fields):\n%s",
114380646Sobrien				    errline);
114459003Shm			*parse = '\0';
1145111779Sgad		} else {
1146111779Sgad			working->uid = (uid_t)-1;
1147111779Sgad			working->gid = (gid_t)-1;
1148111779Sgad		}
114959003Shm
115059003Shm		if (!sscanf(q, "%o", &working->permissions))
115159003Shm			errx(1, "error in config file; bad permissions:\n%s",
115259003Shm			    errline);
115359003Shm
115459003Shm		q = parse = missing_field(sob(++parse), errline);
115559003Shm		parse = son(parse);
115625518Sbrian		if (!*parse)
115780646Sobrien			errx(1, "malformed line (missing fields):\n%s",
115880646Sobrien			    errline);
115959003Shm		*parse = '\0';
1160111400Sgad		if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0)
1161111400Sgad			errx(1, "error in config file; bad value for count of logs to save:\n%s",
116259003Shm			    errline);
116313244Sgraichen
116459003Shm		q = parse = missing_field(sob(++parse), errline);
116559003Shm		parse = son(parse);
116625518Sbrian		if (!*parse)
116780646Sobrien			errx(1, "malformed line (missing fields):\n%s",
116880646Sobrien			    errline);
116959003Shm		*parse = '\0';
1170114762Sgad		if (isdigitch(*q))
1171130165Sgad			working->trsize = atoi(q);
1172130165Sgad		else if (strcmp(q, "*") == 0)
1173130165Sgad			working->trsize = -1;
1174114762Sgad		else {
1175114762Sgad			warnx("Invalid value of '%s' for 'size' in line:\n%s",
1176114762Sgad			    q, errline);
1177130165Sgad			working->trsize = -1;
1178114762Sgad		}
117959003Shm
118059003Shm		working->flags = 0;
118159003Shm		q = parse = missing_field(sob(++parse), errline);
118259003Shm		parse = son(parse);
118325518Sbrian		eol = !*parse;
118459003Shm		*parse = '\0';
118543071Swollman		{
118659003Shm			char *ep;
118759003Shm			u_long ul;
118813244Sgraichen
118943071Swollman			ul = strtoul(q, &ep, 10);
119043071Swollman			if (ep == q)
119143071Swollman				working->hours = 0;
119243071Swollman			else if (*ep == '*')
119343071Swollman				working->hours = -1;
119443071Swollman			else if (ul > INT_MAX)
119543071Swollman				errx(1, "interval is too large:\n%s", errline);
119643071Swollman			else
119743071Swollman				working->hours = ul;
119843071Swollman
1199120361Sgad			if (*ep == '\0' || strcmp(ep, "*") == 0)
1200120361Sgad				goto no_trimat;
1201120361Sgad			if (*ep != '@' && *ep != '$')
120243071Swollman				errx(1, "malformed interval/at:\n%s", errline);
1203120361Sgad
1204120361Sgad			working->flags |= CE_TRIMAT;
1205120361Sgad			working->trim_at = ptime_init(NULL);
1206120361Sgad			ptm_opts = PTM_PARSE_ISO8601;
1207120361Sgad			if (*ep == '$')
1208120361Sgad				ptm_opts = PTM_PARSE_DWM;
1209120361Sgad			ptm_opts |= PTM_PARSE_MATCHDOM;
1210120361Sgad			res = ptime_relparse(working->trim_at, ptm_opts,
1211120361Sgad			    ptimeget_secs(timenow), ep + 1);
1212120361Sgad			if (res == -2)
1213120361Sgad				errx(1, "nonexistent time for 'at' value:\n%s",
1214120361Sgad				    errline);
1215120361Sgad			else if (res < 0)
1216120361Sgad				errx(1, "malformed 'at' value:\n%s", errline);
121743071Swollman		}
1218120361Sgadno_trimat:
121943071Swollman
122025518Sbrian		if (eol)
122159003Shm			q = NULL;
122225518Sbrian		else {
122359003Shm			q = parse = sob(++parse);	/* Optional field */
122459003Shm			parse = son(parse);
122559003Shm			if (!*parse)
122659003Shm				eol = 1;
122759003Shm			*parse = '\0';
122825518Sbrian		}
122925443Sache
1230111768Sgad		for (; q && *q && !isspacech(*q); q++) {
1231111768Sgad			switch (tolowerch(*q)) {
1232111768Sgad			case 'b':
123359003Shm				working->flags |= CE_BINARY;
1234111768Sgad				break;
1235114137Sgad			case 'c':
1236111768Sgad				/*
1237114137Sgad				 * XXX - 	Ick! Ugly! Remove ASAP!
1238114137Sgad				 * We want `c' and `C' for "create".  But we
1239114137Sgad				 * will temporarily treat `c' as `g', because
1240114137Sgad				 * FreeBSD releases <= 4.8 have a typo of
1241114137Sgad				 * checking  ('G' || 'c')  for CE_GLOB.
1242111768Sgad				 */
1243114137Sgad				if (*q == 'c') {
1244114137Sgad					warnx("Assuming 'g' for 'c' in flags for line:\n%s",
1245114137Sgad					    errline);
1246114137Sgad					warnx("The 'c' flag will eventually mean 'CREATE'");
1247114137Sgad					working->flags |= CE_GLOB;
1248114137Sgad					break;
1249114137Sgad				}
1250114137Sgad				working->flags |= CE_CREATE;
1251114137Sgad				break;
1252130043Sgad			case 'd':
1253130043Sgad				working->flags |= CE_NODUMP;
1254130043Sgad				break;
1255111768Sgad			case 'g':
1256106905Ssobomax				working->flags |= CE_GLOB;
1257111768Sgad				break;
1258111768Sgad			case 'j':
1259111768Sgad				working->flags |= CE_BZCOMPACT;
1260111768Sgad				break;
1261111768Sgad			case 'n':
1262111768Sgad				working->flags |= CE_NOSIGNAL;
1263111768Sgad				break;
1264112003Sgad			case 'u':
1265112003Sgad				working->flags |= CE_SIGNALGROUP;
1266112003Sgad				break;
1267111768Sgad			case 'w':
1268160560Ssobomax				/* Depreciated flag - keep for compatibility purposes */
1269111768Sgad				break;
1270111768Sgad			case 'z':
1271111768Sgad				working->flags |= CE_COMPACT;
1272111768Sgad				break;
1273111768Sgad			case '-':
1274111768Sgad				break;
1275111781Sgad			case 'f':	/* Used by OpenBSD for "CE_FOLLOW" */
1276111781Sgad			case 'm':	/* Used by OpenBSD for "CE_MONITOR" */
1277111781Sgad			case 'p':	/* Used by NetBSD  for "CE_PLAIN0" */
1278111768Sgad			default:
127980646Sobrien				errx(1, "illegal flag in config file -- %c",
128080646Sobrien				    *q);
1281111768Sgad			}
128259003Shm		}
128359003Shm
128425518Sbrian		if (eol)
128559003Shm			q = NULL;
128625518Sbrian		else {
128759003Shm			q = parse = sob(++parse);	/* Optional field */
128859003Shm			parse = son(parse);
128959003Shm			if (!*parse)
129059003Shm				eol = 1;
129159003Shm			*parse = '\0';
129225518Sbrian		}
129325443Sache
129425443Sache		working->pid_file = NULL;
129525443Sache		if (q && *q) {
129625443Sache			if (*q == '/')
129725443Sache				working->pid_file = strdup(q);
129836817Sache			else if (isdigit(*q))
129936817Sache				goto got_sig;
130059003Shm			else
130180646Sobrien				errx(1,
130280646Sobrien			"illegal pid file or signal number in config file:\n%s",
130380646Sobrien				    errline);
130425443Sache		}
130536817Sache		if (eol)
130659003Shm			q = NULL;
130736817Sache		else {
130859003Shm			q = parse = sob(++parse);	/* Optional field */
130959003Shm			*(parse = son(parse)) = '\0';
131036817Sache		}
131136817Sache
131236817Sache		working->sig = SIGHUP;
131336817Sache		if (q && *q) {
131436817Sache			if (isdigit(*q)) {
131559003Shm		got_sig:
131636817Sache				working->sig = atoi(q);
131736817Sache			} else {
131859003Shm		err_sig:
131980646Sobrien				errx(1,
132080646Sobrien				    "illegal signal number in config file:\n%s",
132180646Sobrien				    errline);
132236817Sache			}
132336817Sache			if (working->sig < 1 || working->sig >= NSIG)
132436817Sache				goto err_sig;
132536817Sache		}
1326111768Sgad
1327111768Sgad		/*
1328111768Sgad		 * Finish figuring out what pid-file to use (if any) in
1329111768Sgad		 * later processing if this logfile needs to be rotated.
1330111768Sgad		 */
1331111768Sgad		if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) {
1332111768Sgad			/*
1333111768Sgad			 * This config-entry specified 'n' for nosignal,
1334111768Sgad			 * see if it also specified an explicit pid_file.
1335111768Sgad			 * This would be a pretty pointless combination.
1336111768Sgad			 */
1337111768Sgad			if (working->pid_file != NULL) {
1338111768Sgad				warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s",
1339111768Sgad				    working->pid_file, errline);
1340111768Sgad				free(working->pid_file);
1341111768Sgad				working->pid_file = NULL;
1342111768Sgad			}
1343111768Sgad		} else if (working->pid_file == NULL) {
1344111768Sgad			/*
1345111768Sgad			 * This entry did not specify the 'n' flag, which
1346111768Sgad			 * means it should signal syslogd unless it had
1347112003Sgad			 * specified some other pid-file (and obviously the
1348112003Sgad			 * syslog pid-file will not be for a process-group).
1349112003Sgad			 * Also, we should only try to notify syslog if we
1350112003Sgad			 * are root.
1351111768Sgad			 */
1352112003Sgad			if (working->flags & CE_SIGNALGROUP) {
1353112003Sgad				warnx("Ignoring flag 'U' in line:\n%s",
1354112003Sgad				    errline);
1355112003Sgad				working->flags &= ~CE_SIGNALGROUP;
1356112003Sgad			}
1357111768Sgad			if (needroot)
1358111768Sgad				working->pid_file = strdup(_PATH_SYSLOGPID);
1359111768Sgad		}
1360111768Sgad
1361112020Sgad		/*
1362112020Sgad		 * Add this entry to the appropriate list of entries, unless
1363112020Sgad		 * it was some kind of special entry (eg: <default>).
1364112020Sgad		 */
1365112020Sgad		if (special) {
1366112020Sgad			;			/* Do not add to any list */
1367112020Sgad		} else if (working->flags & CE_GLOB) {
1368208648Sgordon			STAILQ_INSERT_TAIL(glob_p, working, cf_nextp);
1369112020Sgad		} else {
1370208648Sgordon			STAILQ_INSERT_TAIL(work_p, working, cf_nextp);
1371112020Sgad		}
1372130165Sgad	}
1373130165Sgad	if (errline != NULL)
137459003Shm		free(errline);
137513244Sgraichen}
137613244Sgraichen
137759003Shmstatic char *
137859004Shmmissing_field(char *p, char *errline)
137913244Sgraichen{
138080646Sobrien
138159003Shm	if (!p || !*p)
138259003Shm		errx(1, "missing field in config file:\n%s", errline);
138359003Shm	return (p);
138413244Sgraichen}
138513244Sgraichen
1386208649Sgordon/*
1387210372Ssimon * In our sort we return it in the reverse of what qsort normally
1388210372Ssimon * would do, as we want the newest files first.  If we have two
1389210372Ssimon * entries with the same time we don't really care about order.
1390210372Ssimon *
1391210372Ssimon * Support function for qsort() in delete_oldest_timelog().
1392210372Ssimon */
1393210372Ssimonstatic int
1394210372Ssimonoldlog_entry_compare(const void *a, const void *b)
1395210372Ssimon{
1396210372Ssimon	const struct oldlog_entry *ola = a, *olb = b;
1397210372Ssimon
1398210372Ssimon	if (ola->t > olb->t)
1399210372Ssimon		return (-1);
1400210372Ssimon	else if (ola->t < olb->t)
1401210372Ssimon		return (1);
1402210372Ssimon	else
1403210372Ssimon		return (0);
1404210372Ssimon}
1405210372Ssimon
1406210372Ssimon/*
1407210372Ssimon * Delete the oldest logfiles, when using time based filenames.
1408210372Ssimon */
1409210372Ssimonstatic void
1410210372Ssimondelete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir)
1411210372Ssimon{
1412210372Ssimon	char *logfname, *s, *dir, errbuf[80];
1413210372Ssimon	int logcnt, max_logcnt, dirfd, i;
1414210372Ssimon	struct oldlog_entry *oldlogs;
1415210372Ssimon	size_t logfname_len;
1416210372Ssimon	struct dirent *dp;
1417210372Ssimon	const char *cdir;
1418210372Ssimon	struct tm tm;
1419210372Ssimon	DIR *dirp;
1420210372Ssimon
1421210372Ssimon	oldlogs = malloc(MAX_OLDLOGS * sizeof(struct oldlog_entry));
1422210372Ssimon	max_logcnt = MAX_OLDLOGS;
1423210372Ssimon	logcnt = 0;
1424210372Ssimon
1425210372Ssimon	if (archive_dir != NULL && archive_dir[0] != '\0')
1426210372Ssimon		cdir = archive_dir;
1427210372Ssimon	else
1428210372Ssimon		if ((cdir = dirname(ent->log)) == NULL)
1429210372Ssimon			err(1, "dirname()");
1430210372Ssimon	if ((dir = strdup(cdir)) == NULL)
1431210372Ssimon		err(1, "strdup()");
1432210372Ssimon
1433210372Ssimon	if ((s = basename(ent->log)) == NULL)
1434210372Ssimon		err(1, "basename()");
1435210372Ssimon	if ((logfname = strdup(s)) == NULL)
1436210372Ssimon		err(1, "strdup()");
1437210372Ssimon	logfname_len = strlen(logfname);
1438210372Ssimon	if (strcmp(logfname, "/") == 0)
1439210372Ssimon		errx(1, "Invalid log filename - became '/'");
1440210372Ssimon
1441210372Ssimon	if (verbose > 2)
1442210372Ssimon		printf("Searching for old logs in %s\n", dir);
1443210372Ssimon
1444210372Ssimon	/* First we create a 'list' of all archived logfiles */
1445210372Ssimon	if ((dirp = opendir(dir)) == NULL)
1446210372Ssimon		err(1, "Cannot open log directory '%s'", dir);
1447210372Ssimon	dirfd = dirfd(dirp);
1448210372Ssimon	while ((dp = readdir(dirp)) != NULL) {
1449210372Ssimon		if (dp->d_type != DT_REG)
1450210372Ssimon			continue;
1451210372Ssimon
1452210372Ssimon		/* Ignore everything but files with our logfile prefix */
1453210372Ssimon		if (strncmp(dp->d_name, logfname, logfname_len) != 0)
1454210372Ssimon			continue;
1455210372Ssimon		/* Ignore the actual non-rotated logfile */
1456210372Ssimon		if (dp->d_namlen == logfname_len)
1457210372Ssimon			continue;
1458210372Ssimon		/*
1459210372Ssimon		 * Make sure we created have found a logfile, so the
1460210372Ssimon		 * postfix is valid, IE format is: '.<time>(.[bg]z)?'.
1461210372Ssimon		 */
1462210372Ssimon		if (dp->d_name[logfname_len] != '.') {
1463210372Ssimon			if (verbose)
1464210372Ssimon				printf("Ignoring %s which has unexpected "
1465210372Ssimon				    "extension '%s'\n", dp->d_name,
1466210372Ssimon				    &dp->d_name[logfname_len]);
1467210372Ssimon			continue;
1468210372Ssimon		}
1469210372Ssimon		if ((s = strptime(&dp->d_name[logfname_len + 1],
1470210372Ssimon			    timefnamefmt, &tm)) == NULL) {
1471210372Ssimon			/*
1472210372Ssimon			 * We could special case "old" sequentially
1473210372Ssimon			 * named logfiles here, but we do not as that
1474210372Ssimon			 * would require special handling to decide
1475210372Ssimon			 * which one was the oldest compared to "new"
1476210372Ssimon			 * time based logfiles.
1477210372Ssimon			 */
1478210372Ssimon			if (verbose)
1479210372Ssimon				printf("Ignoring %s which does not "
1480210372Ssimon				    "match time format\n", dp->d_name);
1481210372Ssimon			continue;
1482210372Ssimon		}
1483210372Ssimon		if (*s != '\0' && !(strcmp(s, BZCOMPRESS_POSTFIX) == 0 ||
1484210372Ssimon			strcmp(s, COMPRESS_POSTFIX) == 0))  {
1485210372Ssimon			    if (verbose)
1486210372Ssimon				printf("Ignoring %s which has unexpected "
1487210372Ssimon				    "extension '%s'\n", dp->d_name, s);
1488210372Ssimon			continue;
1489210372Ssimon		}
1490210372Ssimon
1491210372Ssimon		/*
1492210372Ssimon		 * We should now have old an old rotated logfile, so
1493210372Ssimon		 * add it to the 'list'.
1494210372Ssimon		 */
1495210372Ssimon		if ((oldlogs[logcnt].t = timegm(&tm)) == -1)
1496210372Ssimon			err(1, "Could not convert time string to time value");
1497210372Ssimon		if ((oldlogs[logcnt].fname = strdup(dp->d_name)) == NULL)
1498210372Ssimon			err(1, "strdup()");
1499210372Ssimon		logcnt++;
1500210372Ssimon
1501210372Ssimon		/*
1502210372Ssimon		 * It is very unlikely we ever run out of space in the
1503210372Ssimon		 * logfile array from the default size, but lets
1504210372Ssimon		 * handle it anyway...
1505210372Ssimon		 */
1506210372Ssimon		if (logcnt >= max_logcnt) {
1507210372Ssimon			max_logcnt *= 4;
1508210372Ssimon			/* Detect integer overflow */
1509210372Ssimon			if (max_logcnt < logcnt)
1510210372Ssimon				errx(1, "Too many old logfiles found");
1511210372Ssimon			oldlogs = realloc(oldlogs,
1512210372Ssimon			    max_logcnt * sizeof(struct oldlog_entry));
1513210372Ssimon			if (oldlogs == NULL)
1514210372Ssimon				err(1, "realloc()");
1515210372Ssimon		}
1516210372Ssimon	}
1517210372Ssimon
1518210372Ssimon	/* Second, if needed we delete oldest archived logfiles */
1519210372Ssimon	if (logcnt > 0 && logcnt >= ent->numlogs && ent->numlogs > 1) {
1520210372Ssimon		oldlogs = realloc(oldlogs, logcnt *
1521210372Ssimon		    sizeof(struct oldlog_entry));
1522210372Ssimon		if (oldlogs == NULL)
1523210372Ssimon			err(1, "realloc()");
1524210372Ssimon
1525210372Ssimon		/*
1526210372Ssimon		 * We now sort the logs in the order of newest to
1527210372Ssimon		 * oldest.  That way we can simply skip over the
1528210372Ssimon		 * number of records we want to keep.
1529210372Ssimon		 */
1530210372Ssimon		qsort(oldlogs, logcnt, sizeof(struct oldlog_entry),
1531210372Ssimon		    oldlog_entry_compare);
1532210372Ssimon		for (i = ent->numlogs - 1; i < logcnt; i++) {
1533210372Ssimon			if (noaction)
1534210372Ssimon				printf("\trm -f %s/%s\n", dir,
1535210372Ssimon				    oldlogs[i].fname);
1536210372Ssimon			else if (unlinkat(dirfd, oldlogs[i].fname, 0) != 0) {
1537210372Ssimon				snprintf(errbuf, sizeof(errbuf),
1538210372Ssimon				    "Could not delet old logfile '%s'",
1539210372Ssimon				    oldlogs[i].fname);
1540210372Ssimon				perror(errbuf);
1541210372Ssimon			}
1542210372Ssimon		}
1543210372Ssimon	} else if (verbose > 1)
1544210372Ssimon		printf("No old logs to delete for logfile %s\n", ent->log);
1545210372Ssimon
1546210372Ssimon	/* Third, cleanup */
1547210372Ssimon	closedir(dirp);
1548210372Ssimon	for (i = 0; i < logcnt; i++) {
1549210372Ssimon		assert(oldlogs[i].fname != NULL);
1550210372Ssimon		free(oldlogs[i].fname);
1551210372Ssimon	}
1552210372Ssimon	free(oldlogs);
1553210372Ssimon	free(logfname);
1554210372Ssimon	free(dir);
1555210372Ssimon}
1556210372Ssimon
1557210372Ssimon/*
1558208649Sgordon * Only add to the queue if the file hasn't already been added. This is
1559208649Sgordon * done to prevent circular include loops.
1560208649Sgordon */
1561208649Sgordonstatic void
1562208649Sgordonadd_to_queue(const char *fname, struct ilist *inclist)
1563208649Sgordon{
1564208649Sgordon	struct include_entry *inc;
1565208649Sgordon
1566208649Sgordon	STAILQ_FOREACH(inc, inclist, inc_nextp) {
1567208649Sgordon		if (strcmp(fname, inc->file) == 0) {
1568208649Sgordon			warnx("duplicate include detected: %s", fname);
1569208649Sgordon			return;
1570208649Sgordon		}
1571208649Sgordon	}
1572208649Sgordon
1573208649Sgordon	inc = malloc(sizeof(struct include_entry));
1574208649Sgordon	if (inc == NULL)
1575208649Sgordon		err(1, "malloc of inc");
1576208649Sgordon	inc->file = strdup(fname);
1577208649Sgordon
1578208649Sgordon	if (verbose > 2)
1579208649Sgordon		printf("\t+ Adding %s to the processing queue.\n", fname);
1580208649Sgordon
1581208649Sgordon	STAILQ_INSERT_TAIL(inclist, inc, inc_nextp);
1582208649Sgordon}
1583208649Sgordon
1584130165Sgadstatic fk_entry
1585130165Sgaddo_rotate(const struct conf_entry *ent)
158613244Sgraichen{
158771299Sjedgar	char dirpart[MAXPATHLEN], namepart[MAXPATHLEN];
158871299Sjedgar	char file1[MAXPATHLEN], file2[MAXPATHLEN];
158971299Sjedgar	char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
159080638Sobrien	char jfile1[MAXPATHLEN];
1591210372Ssimon	char datetimestr[30];
1592159968Sgad	int flags, numlogs_c;
1593130165Sgad	fk_entry free_or_keep;
1594159968Sgad	struct sigwork_entry *swork;
159559003Shm	struct stat st;
1596210372Ssimon	struct tm tm;
1597210372Ssimon	time_t now;
159813244Sgraichen
1599119927Sgad	flags = ent->flags;
1600130165Sgad	free_or_keep = FREE_ENT;
1601119927Sgad
160259004Shm	if (archtodir) {
160359004Shm		char *p;
160413244Sgraichen
160559004Shm		/* build complete name of archive directory into dirpart */
160659004Shm		if (*archdirname == '/') {	/* absolute */
160771299Sjedgar			strlcpy(dirpart, archdirname, sizeof(dirpart));
160859004Shm		} else {	/* relative */
160959004Shm			/* get directory part of logfile */
1610119927Sgad			strlcpy(dirpart, ent->log, sizeof(dirpart));
161159004Shm			if ((p = rindex(dirpart, '/')) == NULL)
161259004Shm				dirpart[0] = '\0';
161359004Shm			else
161459004Shm				*(p + 1) = '\0';
161571299Sjedgar			strlcat(dirpart, archdirname, sizeof(dirpart));
161659004Shm		}
161759004Shm
161859004Shm		/* check if archive directory exists, if not, create it */
161959004Shm		if (lstat(dirpart, &st))
1620114137Sgad			createdir(ent, dirpart);
162159004Shm
162259004Shm		/* get filename part of logfile */
1623119927Sgad		if ((p = rindex(ent->log, '/')) == NULL)
1624119927Sgad			strlcpy(namepart, ent->log, sizeof(namepart));
162559004Shm		else
162671299Sjedgar			strlcpy(namepart, p + 1, sizeof(namepart));
162759004Shm
162859004Shm		/* name of oldest log */
162980646Sobrien		(void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart,
1630119927Sgad		    namepart, ent->numlogs);
163159004Shm	} else {
1632210372Ssimon		/*
1633210372Ssimon		 * Tell delete_oldest_timelog() we are not using an
1634210372Ssimon		 * archive dir.
1635210372Ssimon		 */
1636210372Ssimon		dirpart[0] = '\0';
1637210372Ssimon
163859004Shm		/* name of oldest log */
1639119927Sgad		(void) snprintf(file1, sizeof(file1), "%s.%d", ent->log,
1640119927Sgad		    ent->numlogs);
1641210372Ssimon	}
1642210372Ssimon
1643210372Ssimon	/* Delete old logs */
1644210372Ssimon	if (timefnamefmt != NULL)
1645210372Ssimon		delete_oldest_timelog(ent, dirpart);
1646210372Ssimon	else {
1647210372Ssimon		/* name of oldest log */
164871299Sjedgar		(void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
164971299Sjedgar		    COMPRESS_POSTFIX);
165080638Sobrien		snprintf(jfile1, sizeof(jfile1), "%s%s", file1,
165180638Sobrien		    BZCOMPRESS_POSTFIX);
165259004Shm
1653210372Ssimon		if (noaction) {
1654210372Ssimon			printf("\trm -f %s\n", file1);
1655210372Ssimon			printf("\trm -f %s\n", zfile1);
1656210372Ssimon			printf("\trm -f %s\n", jfile1);
1657210372Ssimon		} else {
1658210372Ssimon			(void) unlink(file1);
1659210372Ssimon			(void) unlink(zfile1);
1660210372Ssimon			(void) unlink(jfile1);
1661210372Ssimon		}
166259003Shm	}
166313244Sgraichen
1664210372Ssimon	if (timefnamefmt != NULL) {
1665210372Ssimon		/* If time functions fails we can't really do any sensible */
1666210372Ssimon		if (time(&now) == (time_t)-1 ||
1667210372Ssimon		    localtime_r(&now, &tm) == NULL)
1668210372Ssimon			bzero(&tm, sizeof(tm));
1669210372Ssimon
1670210372Ssimon		strftime(datetimestr, sizeof(datetimestr), timefnamefmt, &tm);
1671210372Ssimon		if (archtodir)
1672210372Ssimon			(void) snprintf(file1, sizeof(file1), "%s/%s.%s",
1673210372Ssimon			    dirpart, namepart, datetimestr);
1674210372Ssimon		else
1675210372Ssimon			(void) snprintf(file1, sizeof(file1), "%s.%s",
1676210372Ssimon			    ent->log, datetimestr);
1677210372Ssimon
1678210372Ssimon		/* Don't run the code to move down logs */
1679210372Ssimon		numlogs_c = 0;
1680210372Ssimon	} else
1681210372Ssimon		numlogs_c = ent->numlogs;		/* copy for countdown */
1682210372Ssimon
168359003Shm	/* Move down log files */
1684119927Sgad	while (numlogs_c--) {
168559004Shm
168671299Sjedgar		(void) strlcpy(file2, file1, sizeof(file2));
168759004Shm
168859004Shm		if (archtodir)
168980646Sobrien			(void) snprintf(file1, sizeof(file1), "%s/%s.%d",
1690119927Sgad			    dirpart, namepart, numlogs_c);
169159004Shm		else
1692119927Sgad			(void) snprintf(file1, sizeof(file1), "%s.%d",
1693119927Sgad			    ent->log, numlogs_c);
169459004Shm
169571299Sjedgar		(void) strlcpy(zfile1, file1, sizeof(zfile1));
169671299Sjedgar		(void) strlcpy(zfile2, file2, sizeof(zfile2));
169759003Shm		if (lstat(file1, &st)) {
169880646Sobrien			(void) strlcat(zfile1, COMPRESS_POSTFIX,
169980646Sobrien			    sizeof(zfile1));
170080646Sobrien			(void) strlcat(zfile2, COMPRESS_POSTFIX,
170180646Sobrien			    sizeof(zfile2));
170280638Sobrien			if (lstat(zfile1, &st)) {
170380638Sobrien				strlcpy(zfile1, file1, sizeof(zfile1));
170480638Sobrien				strlcpy(zfile2, file2, sizeof(zfile2));
170580638Sobrien				strlcat(zfile1, BZCOMPRESS_POSTFIX,
170680638Sobrien				    sizeof(zfile1));
170780638Sobrien				strlcat(zfile2, BZCOMPRESS_POSTFIX,
170880638Sobrien				    sizeof(zfile2));
170980638Sobrien				if (lstat(zfile1, &st))
171080638Sobrien					continue;
171180638Sobrien			}
171259003Shm		}
1713130165Sgad		if (noaction)
1714111967Sgad			printf("\tmv %s %s\n", zfile1, zfile2);
1715130165Sgad		else {
1716130165Sgad			/* XXX - Ought to be checking for failure! */
1717130165Sgad			(void)rename(zfile1, zfile2);
171859003Shm		}
1719130165Sgad		change_attrs(zfile2, ent);
172059003Shm	}
172113244Sgraichen
1722130038Sgad	if (ent->numlogs > 0) {
1723129974Sgad		if (noaction) {
1724130038Sgad			/*
1725130038Sgad			 * Note that savelog() may succeed with using link()
1726130038Sgad			 * for the archtodir case, but there is no good way
1727130038Sgad			 * of knowing if it will when doing "noaction", so
1728130038Sgad			 * here we claim that it will have to do a copy...
1729130038Sgad			 */
1730130038Sgad			if (archtodir)
1731130038Sgad				printf("\tcp %s %s\n", ent->log, file1);
1732130038Sgad			else
1733130038Sgad				printf("\tln %s %s\n", ent->log, file1);
1734130165Sgad		} else {
1735130038Sgad			if (!(flags & CE_BINARY)) {
1736130038Sgad				/* Report the trimming to the old log */
1737130038Sgad				log_trim(ent->log, ent);
1738130038Sgad			}
1739130038Sgad			savelog(ent->log, file1);
174059004Shm		}
1741130165Sgad		change_attrs(file1, ent);
174218075Sjkh	}
174318075Sjkh
1744130038Sgad	/* Create the new log file and move it into place */
1745130038Sgad	if (noaction)
1746111781Sgad		printf("Start new log...\n");
1747130038Sgad	createlog(ent);
174825443Sache
1749111966Sgad	/*
1750130167Sgad	 * Save all signalling and file-compression to be done after log
1751130167Sgad	 * files from all entries have been rotated.  This way any one
1752130167Sgad	 * process will not be sent the same signal multiple times when
1753130167Sgad	 * multiple log files had to be rotated.
1754130167Sgad	 */
1755159968Sgad	swork = NULL;
1756159968Sgad	if (ent->pid_file != NULL)
1757159968Sgad		swork = save_sigwork(ent);
1758159968Sgad	if (ent->numlogs > 0 && (flags & (CE_COMPACT | CE_BZCOMPACT))) {
1759159968Sgad		/*
1760159968Sgad		 * The zipwork_entry will include a pointer to this
1761159968Sgad		 * conf_entry, so the conf_entry should not be freed.
1762159968Sgad		 */
1763159968Sgad		free_or_keep = KEEP_ENT;
1764159968Sgad		save_zipwork(ent, swork, ent->fsize, file1);
1765130167Sgad	}
1766130167Sgad
1767130165Sgad	return (free_or_keep);
176813244Sgraichen}
176913244Sgraichen
1770130167Sgadstatic void
1771130167Sgaddo_sigwork(struct sigwork_entry *swork)
1772130167Sgad{
1773130204Sgad	struct sigwork_entry *nextsig;
1774130204Sgad	int kres, secs;
1775130167Sgad
1776130167Sgad	if (!(swork->sw_pidok) || swork->sw_pid == 0)
1777130167Sgad		return;			/* no work to do... */
1778130167Sgad
1779130167Sgad	/*
1780130167Sgad	 * If nosignal (-s) was specified, then do not signal any process.
1781130167Sgad	 * Note that a nosignal request triggers a warning message if the
1782130167Sgad	 * rotated logfile needs to be compressed, *unless* -R was also
1783130167Sgad	 * specified.  We assume that an `-sR' request came from a process
1784130167Sgad	 * which writes to the logfile, and as such, we assume that process
1785130167Sgad	 * has already made sure the logfile is not presently in use.  This
1786130167Sgad	 * just sets swork->sw_pidok to a special value, and do_zipwork
1787130167Sgad	 * will print any necessary warning(s).
1788130167Sgad	 */
1789130167Sgad	if (nosignal) {
1790130167Sgad		if (!rotatereq)
1791130167Sgad			swork->sw_pidok = -1;
1792130167Sgad		return;
1793130167Sgad	}
1794130167Sgad
1795130204Sgad	/*
1796130204Sgad	 * Compute the pause between consecutive signals.  Use a longer
1797130204Sgad	 * sleep time if we will be sending two signals to the same
1798130204Sgad	 * deamon or process-group.
1799130204Sgad	 */
1800130204Sgad	secs = 0;
1801130204Sgad	nextsig = SLIST_NEXT(swork, sw_nextp);
1802130204Sgad	if (nextsig != NULL) {
1803130204Sgad		if (swork->sw_pid == nextsig->sw_pid)
1804130204Sgad			secs = 10;
1805130204Sgad		else
1806130204Sgad			secs = 1;
1807130204Sgad	}
1808130204Sgad
1809130167Sgad	if (noaction) {
1810130204Sgad		printf("\tkill -%d %d \t\t# %s\n", swork->sw_signum,
1811130204Sgad		    (int)swork->sw_pid, swork->sw_fname);
1812130204Sgad		if (secs > 0)
1813130204Sgad			printf("\tsleep %d\n", secs);
1814130167Sgad		return;
1815130167Sgad	}
1816130167Sgad
1817130167Sgad	kres = kill(swork->sw_pid, swork->sw_signum);
1818130167Sgad	if (kres != 0) {
1819130167Sgad		/*
1820130167Sgad		 * Assume that "no such process" (ESRCH) is something
1821130167Sgad		 * to warn about, but is not an error.  Presumably the
1822130167Sgad		 * process which writes to the rotated log file(s) is
1823130167Sgad		 * gone, in which case we should have no problem with
1824130167Sgad		 * compressing the rotated log file(s).
1825130167Sgad		 */
1826130167Sgad		if (errno != ESRCH)
1827130167Sgad			swork->sw_pidok = 0;
1828130167Sgad		warn("can't notify %s, pid %d", swork->sw_pidtype,
1829130167Sgad		    (int)swork->sw_pid);
1830130167Sgad	} else {
1831130204Sgad		if (verbose)
1832130204Sgad			printf("Notified %s pid %d = %s\n", swork->sw_pidtype,
1833130204Sgad			    (int)swork->sw_pid, swork->sw_fname);
1834130204Sgad		if (secs > 0) {
1835130204Sgad			if (verbose)
1836130204Sgad				printf("Pause %d second(s) between signals\n",
1837130204Sgad				    secs);
1838130204Sgad			sleep(secs);
1839130167Sgad		}
1840130167Sgad	}
1841130167Sgad}
1842130167Sgad
1843130167Sgadstatic void
1844130167Sgaddo_zipwork(struct zipwork_entry *zwork)
1845130167Sgad{
1846130167Sgad	const char *pgm_name, *pgm_path;
1847154566Sgad	int errsav, fcount, zstatus;
1848130167Sgad	pid_t pidzip, wpid;
1849130167Sgad	char zresult[MAXPATHLEN];
1850130167Sgad
1851130167Sgad	pgm_path = NULL;
1852130167Sgad	strlcpy(zresult, zwork->zw_fname, sizeof(zresult));
1853130167Sgad	if (zwork != NULL && zwork->zw_conf != NULL) {
1854130167Sgad		if (zwork->zw_conf->flags & CE_COMPACT) {
1855130167Sgad			pgm_path = _PATH_GZIP;
1856130167Sgad			strlcat(zresult, COMPRESS_POSTFIX, sizeof(zresult));
1857130167Sgad		} else if (zwork->zw_conf->flags & CE_BZCOMPACT) {
1858130167Sgad			pgm_path = _PATH_BZIP2;
1859130167Sgad			strlcat(zresult, BZCOMPRESS_POSTFIX, sizeof(zresult));
1860130167Sgad		}
1861130167Sgad	}
1862130167Sgad	if (pgm_path == NULL) {
1863130167Sgad		warnx("invalid entry for %s in do_zipwork", zwork->zw_fname);
1864130167Sgad		return;
1865130167Sgad	}
1866154566Sgad	pgm_name = strrchr(pgm_path, '/');
1867154566Sgad	if (pgm_name == NULL)
1868154566Sgad		pgm_name = pgm_path;
1869154566Sgad	else
1870154566Sgad		pgm_name++;
1871130167Sgad
1872130167Sgad	if (zwork->zw_swork != NULL && zwork->zw_swork->sw_pidok <= 0) {
1873130167Sgad		warnx(
1874130167Sgad		    "log %s not compressed because daemon(s) not notified",
1875130167Sgad		    zwork->zw_fname);
1876130167Sgad		change_attrs(zwork->zw_fname, zwork->zw_conf);
1877130167Sgad		return;
1878130167Sgad	}
1879130167Sgad
1880130167Sgad	if (noaction) {
1881130167Sgad		printf("\t%s %s\n", pgm_name, zwork->zw_fname);
1882130167Sgad		change_attrs(zresult, zwork->zw_conf);
1883130167Sgad		return;
1884130167Sgad	}
1885130167Sgad
1886154566Sgad	fcount = 1;
1887130167Sgad	pidzip = fork();
1888154566Sgad	while (pidzip < 0) {
1889154566Sgad		/*
1890154566Sgad		 * The fork failed.  If the failure was due to a temporary
1891154566Sgad		 * problem, then wait a short time and try it again.
1892154566Sgad		 */
1893154566Sgad		errsav = errno;
1894154566Sgad		warn("fork() for `%s %s'", pgm_name, zwork->zw_fname);
1895154566Sgad		if (errsav != EAGAIN || fcount > 5)
1896154566Sgad			errx(1, "Exiting...");
1897154566Sgad		sleep(fcount * 12);
1898154566Sgad		fcount++;
1899154566Sgad		pidzip = fork();
1900154566Sgad	}
1901154566Sgad	if (!pidzip) {
1902130167Sgad		/* The child process executes the compression command */
1903130167Sgad		execl(pgm_path, pgm_path, "-f", zwork->zw_fname, (char *)0);
1904154566Sgad		err(1, "execl(`%s -f %s')", pgm_path, zwork->zw_fname);
1905130167Sgad	}
1906130167Sgad
1907130167Sgad	wpid = waitpid(pidzip, &zstatus, 0);
1908130167Sgad	if (wpid == -1) {
1909154566Sgad		/* XXX - should this be a fatal error? */
1910130167Sgad		warn("%s: waitpid(%d)", pgm_path, pidzip);
1911130167Sgad		return;
1912130167Sgad	}
1913130167Sgad	if (!WIFEXITED(zstatus)) {
1914154566Sgad		warnx("`%s -f %s' did not terminate normally", pgm_name,
1915154566Sgad		    zwork->zw_fname);
1916130167Sgad		return;
1917130167Sgad	}
1918130167Sgad	if (WEXITSTATUS(zstatus)) {
1919154566Sgad		warnx("`%s -f %s' terminated with a non-zero status (%d)",
1920154566Sgad		    pgm_name, zwork->zw_fname, WEXITSTATUS(zstatus));
1921130167Sgad		return;
1922130167Sgad	}
1923130167Sgad
1924130167Sgad	/* Compression was successful, set file attributes on the result. */
1925130167Sgad	change_attrs(zresult, zwork->zw_conf);
1926130167Sgad}
1927130167Sgad
1928130167Sgad/*
1929130167Sgad * Save information on any process we need to signal.  Any single
1930130167Sgad * process may need to be sent different signal-values for different
1931130167Sgad * log files, but usually a single signal-value will cause the process
1932130167Sgad * to close and re-open all of it's log files.
1933130167Sgad */
1934130167Sgadstatic struct sigwork_entry *
1935130167Sgadsave_sigwork(const struct conf_entry *ent)
1936130167Sgad{
1937130167Sgad	struct sigwork_entry *sprev, *stmp;
1938130167Sgad	int ndiff;
1939130167Sgad	size_t tmpsiz;
1940130167Sgad
1941130167Sgad	sprev = NULL;
1942130167Sgad	ndiff = 1;
1943130167Sgad	SLIST_FOREACH(stmp, &swhead, sw_nextp) {
1944130167Sgad		ndiff = strcmp(ent->pid_file, stmp->sw_fname);
1945130167Sgad		if (ndiff > 0)
1946130167Sgad			break;
1947130167Sgad		if (ndiff == 0) {
1948130167Sgad			if (ent->sig == stmp->sw_signum)
1949130167Sgad				break;
1950130167Sgad			if (ent->sig > stmp->sw_signum) {
1951130167Sgad				ndiff = 1;
1952130167Sgad				break;
1953130167Sgad			}
1954130167Sgad		}
1955130167Sgad		sprev = stmp;
1956130167Sgad	}
1957130167Sgad	if (stmp != NULL && ndiff == 0)
1958130167Sgad		return (stmp);
1959130167Sgad
1960130167Sgad	tmpsiz = sizeof(struct sigwork_entry) + strlen(ent->pid_file) + 1;
1961130167Sgad	stmp = malloc(tmpsiz);
1962130167Sgad	set_swpid(stmp, ent);
1963130167Sgad	stmp->sw_signum = ent->sig;
1964130167Sgad	strcpy(stmp->sw_fname, ent->pid_file);
1965130167Sgad	if (sprev == NULL)
1966130167Sgad		SLIST_INSERT_HEAD(&swhead, stmp, sw_nextp);
1967130167Sgad	else
1968130167Sgad		SLIST_INSERT_AFTER(sprev, stmp, sw_nextp);
1969130167Sgad	return (stmp);
1970130167Sgad}
1971130167Sgad
1972130167Sgad/*
1973130167Sgad * Save information on any file we need to compress.  We may see the same
1974130167Sgad * file multiple times, so check the full list to avoid duplicates.  The
1975130167Sgad * list itself is sorted smallest-to-largest, because that's the order we
1976130167Sgad * want to compress the files.  If the partition is very low on disk space,
1977130167Sgad * then the smallest files are the most likely to compress, and compressing
1978130167Sgad * them first will free up more space for the larger files.
1979130167Sgad */
1980130167Sgadstatic struct zipwork_entry *
1981130167Sgadsave_zipwork(const struct conf_entry *ent, const struct sigwork_entry *swork,
1982130167Sgad    int zsize, const char *zipfname)
1983130167Sgad{
1984130167Sgad	struct zipwork_entry *zprev, *ztmp;
1985130167Sgad	int ndiff;
1986130167Sgad	size_t tmpsiz;
1987130167Sgad
1988130167Sgad	/* Compute the size if the caller did not know it. */
1989130167Sgad	if (zsize < 0)
1990130167Sgad		zsize = sizefile(zipfname);
1991130167Sgad
1992130167Sgad	zprev = NULL;
1993130167Sgad	ndiff = 1;
1994130167Sgad	SLIST_FOREACH(ztmp, &zwhead, zw_nextp) {
1995130707Sgad		ndiff = strcmp(zipfname, ztmp->zw_fname);
1996130167Sgad		if (ndiff == 0)
1997130167Sgad			break;
1998130167Sgad		if (zsize > ztmp->zw_fsize)
1999130167Sgad			zprev = ztmp;
2000130167Sgad	}
2001130167Sgad	if (ztmp != NULL && ndiff == 0)
2002130167Sgad		return (ztmp);
2003130167Sgad
2004130167Sgad	tmpsiz = sizeof(struct zipwork_entry) + strlen(zipfname) + 1;
2005130167Sgad	ztmp = malloc(tmpsiz);
2006130167Sgad	ztmp->zw_conf = ent;
2007130167Sgad	ztmp->zw_swork = swork;
2008130167Sgad	ztmp->zw_fsize = zsize;
2009130167Sgad	strcpy(ztmp->zw_fname, zipfname);
2010130167Sgad	if (zprev == NULL)
2011130167Sgad		SLIST_INSERT_HEAD(&zwhead, ztmp, zw_nextp);
2012130167Sgad	else
2013130167Sgad		SLIST_INSERT_AFTER(zprev, ztmp, zw_nextp);
2014130167Sgad	return (ztmp);
2015130167Sgad}
2016130167Sgad
2017130167Sgad/* Send a signal to the pid specified by pidfile */
2018130167Sgadstatic void
2019130167Sgadset_swpid(struct sigwork_entry *swork, const struct conf_entry *ent)
2020130167Sgad{
2021130167Sgad	FILE *f;
2022130167Sgad	long minok, maxok, rval;
2023130167Sgad	char *endp, *linep, line[BUFSIZ];
2024130167Sgad
2025130167Sgad	minok = MIN_PID;
2026130167Sgad	maxok = MAX_PID;
2027130167Sgad	swork->sw_pidok = 0;
2028130167Sgad	swork->sw_pid = 0;
2029130167Sgad	swork->sw_pidtype = "daemon";
2030130167Sgad	if (ent->flags & CE_SIGNALGROUP) {
2031130167Sgad		/*
2032130167Sgad		 * If we are expected to signal a process-group when
2033130167Sgad		 * rotating this logfile, then the value read in should
2034130167Sgad		 * be the negative of a valid process ID.
2035130167Sgad		 */
2036130167Sgad		minok = -MAX_PID;
2037130167Sgad		maxok = -MIN_PID;
2038130167Sgad		swork->sw_pidtype = "process-group";
2039130167Sgad	}
2040130167Sgad
2041130167Sgad	f = fopen(ent->pid_file, "r");
2042130167Sgad	if (f == NULL) {
2043202668Sdelphij		if (errno == ENOENT && enforcepid == 0) {
2044200806Sdelphij			/*
2045200806Sdelphij			 * Warn if the PID file doesn't exist, but do
2046200806Sdelphij			 * not consider it an error.  Most likely it
2047200806Sdelphij			 * means the process has been terminated,
2048200806Sdelphij			 * so it should be safe to rotate any log
2049200806Sdelphij			 * files that the process would have been using.
2050200806Sdelphij			 */
2051200806Sdelphij			swork->sw_pidok = 1;
2052200806Sdelphij			warnx("pid file doesn't exist: %s", ent->pid_file);
2053200806Sdelphij		} else
2054200806Sdelphij			warn("can't open pid file: %s", ent->pid_file);
2055130167Sgad		return;
2056130167Sgad	}
2057130167Sgad
2058130167Sgad	if (fgets(line, BUFSIZ, f) == NULL) {
2059130167Sgad		/*
2060130167Sgad		 * Warn if the PID file is empty, but do not consider
2061130167Sgad		 * it an error.  Most likely it means the process has
2062130167Sgad		 * has terminated, so it should be safe to rotate any
2063130167Sgad		 * log files that the process would have been using.
2064130167Sgad		 */
2065202668Sdelphij		if (feof(f) && enforcepid == 0) {
2066130167Sgad			swork->sw_pidok = 1;
2067130167Sgad			warnx("pid file is empty: %s", ent->pid_file);
2068130167Sgad		} else
2069130167Sgad			warn("can't read from pid file: %s", ent->pid_file);
2070130167Sgad		(void)fclose(f);
2071130167Sgad		return;
2072130167Sgad	}
2073130167Sgad	(void)fclose(f);
2074130167Sgad
2075130167Sgad	errno = 0;
2076130167Sgad	linep = line;
2077130167Sgad	while (*linep == ' ')
2078130167Sgad		linep++;
2079130167Sgad	rval = strtol(linep, &endp, 10);
2080130167Sgad	if (*endp != '\0' && !isspacech(*endp)) {
2081130167Sgad		warnx("pid file does not start with a valid number: %s",
2082130167Sgad		    ent->pid_file);
2083130167Sgad	} else if (rval < minok || rval > maxok) {
2084130167Sgad		warnx("bad value '%ld' for process number in %s",
2085130167Sgad		    rval, ent->pid_file);
2086130167Sgad		if (verbose)
2087130167Sgad			warnx("\t(expecting value between %ld and %ld)",
2088130167Sgad			    minok, maxok);
2089130167Sgad	} else {
2090130167Sgad		swork->sw_pidok = 1;
2091130167Sgad		swork->sw_pid = rval;
2092130167Sgad	}
2093130167Sgad
2094130167Sgad	return;
2095130167Sgad}
2096130167Sgad
209713244Sgraichen/* Log the fact that the logs were turned over */
209859004Shmstatic int
2099119926Sgadlog_trim(const char *logname, const struct conf_entry *log_ent)
210013244Sgraichen{
210159003Shm	FILE *f;
2102111388Sgad	const char *xtra;
210359003Shm
2104119926Sgad	if ((f = fopen(logname, "a")) == NULL)
210559003Shm		return (-1);
2106111388Sgad	xtra = "";
2107111768Sgad	if (log_ent->def_cfg)
2108111388Sgad		xtra = " using <default> rule";
2109114137Sgad	if (log_ent->firstcreate)
2110114137Sgad		fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n",
2111114137Sgad		    daytime, hostname, (int) getpid(), xtra);
2112114137Sgad	else if (log_ent->r_reason != NULL)
2113111772Sgad		fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n",
2114111772Sgad		    daytime, hostname, (int) getpid(), log_ent->r_reason, xtra);
2115111772Sgad	else
2116111772Sgad		fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n",
2117111772Sgad		    daytime, hostname, (int) getpid(), xtra);
211859003Shm	if (fclose(f) == EOF)
2119127858Scharnier		err(1, "log_trim: fclose");
212059003Shm	return (0);
212113244Sgraichen}
212213244Sgraichen
212313244Sgraichen/* Return size in kilobytes of a file */
212459004Shmstatic int
2125130165Sgadsizefile(const char *file)
212613244Sgraichen{
212759003Shm	struct stat sb;
212813244Sgraichen
212959003Shm	if (stat(file, &sb) < 0)
213059003Shm		return (-1);
213159003Shm	return (kbytes(dbtob(sb.st_blocks)));
213213244Sgraichen}
213313244Sgraichen
213413244Sgraichen/* Return the age of old log file (file.0) */
213559004Shmstatic int
213659004Shmage_old_log(char *file)
213713244Sgraichen{
213859003Shm	struct stat sb;
2139114764Sgad	char *endp;
2140114764Sgad	char tmp[MAXPATHLEN + sizeof(".0") + sizeof(COMPRESS_POSTFIX) +
2141114764Sgad		sizeof(BZCOMPRESS_POSTFIX) + 1];
214213244Sgraichen
214359004Shm	if (archtodir) {
214459004Shm		char *p;
214559004Shm
214659004Shm		/* build name of archive directory into tmp */
214759004Shm		if (*archdirname == '/') {	/* absolute */
214871299Sjedgar			strlcpy(tmp, archdirname, sizeof(tmp));
214959004Shm		} else {	/* relative */
215059004Shm			/* get directory part of logfile */
215171299Sjedgar			strlcpy(tmp, file, sizeof(tmp));
215259004Shm			if ((p = rindex(tmp, '/')) == NULL)
215359004Shm				tmp[0] = '\0';
215459004Shm			else
215559004Shm				*(p + 1) = '\0';
215671299Sjedgar			strlcat(tmp, archdirname, sizeof(tmp));
215759004Shm		}
215859004Shm
215971299Sjedgar		strlcat(tmp, "/", sizeof(tmp));
216059004Shm
216159004Shm		/* get filename part of logfile */
216259004Shm		if ((p = rindex(file, '/')) == NULL)
216371299Sjedgar			strlcat(tmp, file, sizeof(tmp));
216459004Shm		else
216571299Sjedgar			strlcat(tmp, p + 1, sizeof(tmp));
216659004Shm	} else {
216771299Sjedgar		(void) strlcpy(tmp, file, sizeof(tmp));
216859004Shm	}
216959004Shm
2170114764Sgad	strlcat(tmp, ".0", sizeof(tmp));
2171114764Sgad	if (stat(tmp, &sb) < 0) {
2172114764Sgad		/*
2173114764Sgad		 * A plain '.0' file does not exist.  Try again, first
2174114764Sgad		 * with the added suffix of '.gz', then with an added
2175114764Sgad		 * suffix of '.bz2' instead of '.gz'.
2176114764Sgad		 */
2177114764Sgad		endp = strchr(tmp, '\0');
2178114764Sgad		strlcat(tmp, COMPRESS_POSTFIX, sizeof(tmp));
2179114764Sgad		if (stat(tmp, &sb) < 0) {
2180114764Sgad			*endp = '\0';		/* Remove .gz */
2181114764Sgad			strlcat(tmp, BZCOMPRESS_POSTFIX, sizeof(tmp));
2182114764Sgad			if (stat(tmp, &sb) < 0)
2183114764Sgad				return (-1);
2184114764Sgad		}
2185114764Sgad	}
2186120361Sgad	return ((int)(ptimeget_secs(timenow) - sb.st_mtime + 1800) / 3600);
218713244Sgraichen}
218813244Sgraichen
218913244Sgraichen/* Skip Over Blanks */
2190111820Sgadstatic char *
219159004Shmsob(char *p)
219213244Sgraichen{
219359003Shm	while (p && *p && isspace(*p))
219459003Shm		p++;
219559003Shm	return (p);
219613244Sgraichen}
219713244Sgraichen
219813244Sgraichen/* Skip Over Non-Blanks */
2199111820Sgadstatic char *
220059004Shmson(char *p)
220113244Sgraichen{
220259003Shm	while (p && *p && !isspace(*p))
220359003Shm		p++;
220459003Shm	return (p);
220513244Sgraichen}
220643071Swollman
2207119102Sgad/* Check if string is actually a number */
2208119102Sgadstatic int
2209119102Sgadisnumberstr(const char *string)
2210119102Sgad{
2211119102Sgad	while (*string) {
2212119102Sgad		if (!isdigitch(*string++))
2213119102Sgad			return (0);
2214119102Sgad	}
2215119102Sgad	return (1);
2216119102Sgad}
2217119102Sgad
2218208649Sgordon/* Check if string contains a glob */
2219208649Sgordonstatic int
2220208649Sgordonisglobstr(const char *string)
2221208649Sgordon{
2222208649Sgordon	char chr;
2223208649Sgordon
2224208649Sgordon	while ((chr = *string++)) {
2225208649Sgordon		if (chr == '*' || chr == '?' || chr == '[')
2226208649Sgordon			return (1);
2227208649Sgordon	}
2228208649Sgordon	return (0);
2229208649Sgordon}
2230208649Sgordon
2231130038Sgad/*
2232130038Sgad * Save the active log file under a new name.  A link to the new name
2233130038Sgad * is the quick-and-easy way to do this.  If that fails (which it will
2234130038Sgad * if the destination is on another partition), then make a copy of
2235130038Sgad * the file to the new location.
2236130038Sgad */
223759004Shmstatic void
2238130038Sgadsavelog(char *from, char *to)
223959004Shm{
224059004Shm	FILE *src, *dst;
2241130038Sgad	int c, res;
224259004Shm
2243130038Sgad	res = link(from, to);
2244130038Sgad	if (res == 0)
2245130038Sgad		return;
2246130038Sgad
224759004Shm	if ((src = fopen(from, "r")) == NULL)
224859004Shm		err(1, "can't fopen %s for reading", from);
224959004Shm	if ((dst = fopen(to, "w")) == NULL)
225059004Shm		err(1, "can't fopen %s for writing", to);
225159004Shm
225259004Shm	while ((c = getc(src)) != EOF) {
225359004Shm		if ((putc(c, dst)) == EOF)
225459004Shm			err(1, "error writing to %s", to);
225559004Shm	}
225659004Shm
225759004Shm	if (ferror(src))
225859004Shm		err(1, "error reading from %s", from);
225959004Shm	if ((fclose(src)) != 0)
226059004Shm		err(1, "can't fclose %s", to);
226159004Shm	if ((fclose(dst)) != 0)
226259004Shm		err(1, "can't fclose %s", from);
226359004Shm}
226459004Shm
226559004Shm/* create one or more directory components of a path */
226659004Shmstatic void
2267114137Sgadcreatedir(const struct conf_entry *ent, char *dirpart)
226859004Shm{
2269111398Sgad	int res;
227059004Shm	char *s, *d;
227171299Sjedgar	char mkdirpath[MAXPATHLEN];
227259004Shm	struct stat st;
227359004Shm
227459004Shm	s = dirpart;
227559004Shm	d = mkdirpath;
227659004Shm
227759004Shm	for (;;) {
227859004Shm		*d++ = *s++;
2279111398Sgad		if (*s != '/' && *s != '\0')
2280111398Sgad			continue;
2281111398Sgad		*d = '\0';
2282111398Sgad		res = lstat(mkdirpath, &st);
2283111398Sgad		if (res != 0) {
2284111398Sgad			if (noaction) {
2285111967Sgad				printf("\tmkdir %s\n", mkdirpath);
2286111398Sgad			} else {
2287111398Sgad				res = mkdir(mkdirpath, 0755);
2288111398Sgad				if (res != 0)
2289111398Sgad					err(1, "Error on mkdir(\"%s\") for -a",
2290111398Sgad					    mkdirpath);
2291111398Sgad			}
229259004Shm		}
229359004Shm		if (*s == '\0')
229459004Shm			break;
229559004Shm	}
2296114137Sgad	if (verbose) {
2297114137Sgad		if (ent->firstcreate)
2298114137Sgad			printf("Created directory '%s' for new %s\n",
2299114137Sgad			    dirpart, ent->log);
2300114137Sgad		else
2301114137Sgad			printf("Created directory '%s' for -a\n", dirpart);
2302114137Sgad	}
230359004Shm}
230459004Shm
2305114137Sgad/*
2306114137Sgad * Create a new log file, destroying any currently-existing version
2307114137Sgad * of the log file in the process.  If the caller wants a backup copy
2308114137Sgad * of the file to exist, they should call 'link(logfile,logbackup)'
2309114137Sgad * before calling this routine.
2310114137Sgad */
2311114137Sgadvoid
2312114137Sgadcreatelog(const struct conf_entry *ent)
2313114137Sgad{
2314114137Sgad	int fd, failed;
2315114137Sgad	struct stat st;
2316114137Sgad	char *realfile, *slash, tempfile[MAXPATHLEN];
2317114137Sgad
2318114137Sgad	fd = -1;
2319114137Sgad	realfile = ent->log;
2320114137Sgad
2321114137Sgad	/*
2322114137Sgad	 * If this log file is being created for the first time (-C option),
2323114137Sgad	 * then it may also be true that the parent directory does not exist
2324114137Sgad	 * yet.  Check, and create that directory if it is missing.
2325114137Sgad	 */
2326114137Sgad	if (ent->firstcreate) {
2327114137Sgad		strlcpy(tempfile, realfile, sizeof(tempfile));
2328114137Sgad		slash = strrchr(tempfile, '/');
2329114137Sgad		if (slash != NULL) {
2330114137Sgad			*slash = '\0';
2331131581Ssobomax			failed = stat(tempfile, &st);
2332114137Sgad			if (failed && errno != ENOENT)
2333131581Ssobomax				err(1, "Error on stat(%s)", tempfile);
2334114137Sgad			if (failed)
2335114137Sgad				createdir(ent, tempfile);
2336114137Sgad			else if (!S_ISDIR(st.st_mode))
2337114137Sgad				errx(1, "%s exists but is not a directory",
2338114137Sgad				    tempfile);
2339114137Sgad		}
2340114137Sgad	}
2341114137Sgad
2342114137Sgad	/*
2343114137Sgad	 * First create an unused filename, so it can be chown'ed and
2344114137Sgad	 * chmod'ed before it is moved into the real location.  mkstemp
2345114137Sgad	 * will create the file mode=600 & owned by us.  Note that all
2346114137Sgad	 * temp files will have a suffix of '.z<something>'.
2347114137Sgad	 */
2348114137Sgad	strlcpy(tempfile, realfile, sizeof(tempfile));
2349114137Sgad	strlcat(tempfile, ".zXXXXXX", sizeof(tempfile));
2350114137Sgad	if (noaction)
2351114137Sgad		printf("\tmktemp %s\n", tempfile);
2352114137Sgad	else {
2353114137Sgad		fd = mkstemp(tempfile);
2354114137Sgad		if (fd < 0)
2355114137Sgad			err(1, "can't mkstemp logfile %s", tempfile);
2356114137Sgad
2357114137Sgad		/*
2358114137Sgad		 * Add status message to what will become the new log file.
2359114137Sgad		 */
2360114137Sgad		if (!(ent->flags & CE_BINARY)) {
2361114137Sgad			if (log_trim(tempfile, ent))
2362114137Sgad				err(1, "can't add status message to log");
2363114137Sgad		}
2364114137Sgad	}
2365114137Sgad
2366114137Sgad	/* Change the owner/group, if we are supposed to */
2367114137Sgad	if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) {
2368114137Sgad		if (noaction)
2369114137Sgad			printf("\tchown %u:%u %s\n", ent->uid, ent->gid,
2370114137Sgad			    tempfile);
2371114137Sgad		else {
2372114137Sgad			failed = fchown(fd, ent->uid, ent->gid);
2373114137Sgad			if (failed)
2374114137Sgad				err(1, "can't fchown temp file %s", tempfile);
2375114137Sgad		}
2376114137Sgad	}
2377114137Sgad
2378130043Sgad	/* Turn on NODUMP if it was requested in the config-file. */
2379130043Sgad	if (ent->flags & CE_NODUMP) {
2380130043Sgad		if (noaction)
2381130043Sgad			printf("\tchflags nodump %s\n", tempfile);
2382130043Sgad		else {
2383130043Sgad			failed = fchflags(fd, UF_NODUMP);
2384130043Sgad			if (failed) {
2385130043Sgad				warn("log_trim: fchflags(NODUMP)");
2386130043Sgad			}
2387130043Sgad		}
2388130043Sgad	}
2389130043Sgad
2390114137Sgad	/*
2391114137Sgad	 * Note that if the real logfile still exists, and if the call
2392114137Sgad	 * to rename() fails, then "neither the old file nor the new
2393114137Sgad	 * file shall be changed or created" (to quote the standard).
2394114137Sgad	 * If the call succeeds, then the file will be replaced without
2395114137Sgad	 * any window where some other process might find that the file
2396114137Sgad	 * did not exist.
2397114137Sgad	 * XXX - ? It may be that for some error conditions, we could
2398114137Sgad	 *	retry by first removing the realfile and then renaming.
2399114137Sgad	 */
2400114137Sgad	if (noaction) {
2401114137Sgad		printf("\tchmod %o %s\n", ent->permissions, tempfile);
2402114137Sgad		printf("\tmv %s %s\n", tempfile, realfile);
2403114137Sgad	} else {
2404114137Sgad		failed = fchmod(fd, ent->permissions);
2405114137Sgad		if (failed)
2406114137Sgad			err(1, "can't fchmod temp file '%s'", tempfile);
2407114137Sgad		failed = rename(tempfile, realfile);
2408114137Sgad		if (failed)
2409114137Sgad			err(1, "can't mv %s to %s", tempfile, realfile);
2410114137Sgad	}
2411114137Sgad
2412114137Sgad	if (fd >= 0)
2413114137Sgad		close(fd);
2414114137Sgad}
2415130165Sgad
2416154566Sgad/*
2417154566Sgad * Change the attributes of a given filename to what was specified in
2418154566Sgad * the newsyslog.conf entry.  This routine is only called for files
2419154566Sgad * that newsyslog expects that it has created, and thus it is a fatal
2420154566Sgad * error if this routine finds that the file does not exist.
2421154566Sgad */
2422130165Sgadstatic void
2423130165Sgadchange_attrs(const char *fname, const struct conf_entry *ent)
2424130165Sgad{
2425130165Sgad	int failed;
2426130165Sgad
2427130165Sgad	if (noaction) {
2428130165Sgad		printf("\tchmod %o %s\n", ent->permissions, fname);
2429130165Sgad
2430130165Sgad		if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
2431130165Sgad			printf("\tchown %u:%u %s\n",
2432130165Sgad			    ent->uid, ent->gid, fname);
2433130165Sgad
2434130165Sgad		if (ent->flags & CE_NODUMP)
2435130165Sgad			printf("\tchflags nodump %s\n", fname);
2436130165Sgad		return;
2437130165Sgad	}
2438130165Sgad
2439130165Sgad	failed = chmod(fname, ent->permissions);
2440154566Sgad	if (failed) {
2441154566Sgad		if (errno != EPERM)
2442154566Sgad			err(1, "chmod(%s) in change_attrs", fname);
2443154566Sgad		warn("change_attrs couldn't chmod(%s)", fname);
2444154566Sgad	}
2445130165Sgad
2446130165Sgad	if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) {
2447130165Sgad		failed = chown(fname, ent->uid, ent->gid);
2448130165Sgad		if (failed)
2449130165Sgad			warn("can't chown %s", fname);
2450130165Sgad	}
2451130165Sgad
2452130165Sgad	if (ent->flags & CE_NODUMP) {
2453130165Sgad		failed = chflags(fname, UF_NODUMP);
2454130165Sgad		if (failed)
2455130165Sgad			warn("can't chflags %s NODUMP", fname);
2456130165Sgad	}
2457130165Sgad}
2458