xref: /dragonfly/usr.sbin/newsyslog/newsyslog.c (revision cfd1aba3)
1 /*-
2  * ------+---------+---------+-------- + --------+---------+---------+---------*
3  * This file includes significant modifications done by:
4  * Copyright (c) 2003, 2004  - Garance Alistair Drosehn <gad@FreeBSD.org>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *   1. Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *   2. Redistributions in binary form must reproduce the above copyright
13  *      notice, this list of conditions and the following disclaimer in the
14  *      documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * ------+---------+---------+-------- + --------+---------+---------+---------*
29  */
30 
31 /*
32  * This file contains changes from the Open Software Foundation.
33  */
34 
35 /*
36  * Copyright 1988, 1989 by the Massachusetts Institute of Technology
37  *
38  * Permission to use, copy, modify, and distribute this software and its
39  * documentation for any purpose and without fee is hereby granted, provided
40  * that the above copyright notice appear in all copies and that both that
41  * copyright notice and this permission notice appear in supporting
42  * documentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
43  * used in advertising or publicity pertaining to distribution of the
44  * software without specific, written prior permission. M.I.T. and the M.I.T.
45  * S.I.P.B. make no representations about the suitability of this software
46  * for any purpose.  It is provided "as is" without express or implied
47  * warranty.
48  *
49  * $FreeBSD: src/usr.sbin/newsyslog/newsyslog.c,v 1.117 2011/01/31 10:57:54 mm Exp $
50  */
51 
52 /*
53  * newsyslog - roll over selected logs at the appropriate time, keeping the a
54  * specified number of backup files around.
55  */
56 
57 #define	OSF
58 
59 #include <sys/param.h>
60 #include <sys/queue.h>
61 #include <sys/stat.h>
62 #include <sys/wait.h>
63 
64 #include <assert.h>
65 #include <ctype.h>
66 #include <err.h>
67 #include <errno.h>
68 #include <dirent.h>
69 #include <fcntl.h>
70 #include <fnmatch.h>
71 #include <glob.h>
72 #include <grp.h>
73 #include <paths.h>
74 #include <pwd.h>
75 #include <signal.h>
76 #include <stdio.h>
77 #include <libgen.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <time.h>
81 #include <unistd.h>
82 
83 #include "pathnames.h"
84 #include "extern.h"
85 
86 /*
87  * Compression suffixes
88  */
89 #ifndef	COMPRESS_SUFFIX_GZ
90 #define	COMPRESS_SUFFIX_GZ	".gz"
91 #endif
92 
93 #ifndef	COMPRESS_SUFFIX_BZ2
94 #define	COMPRESS_SUFFIX_BZ2	".bz2"
95 #endif
96 
97 #ifndef	COMPRESS_SUFFIX_XZ
98 #define	COMPRESS_SUFFIX_XZ	".xz"
99 #endif
100 
101 #define	COMPRESS_SUFFIX_MAXLEN	MAX(MAX(sizeof(COMPRESS_SUFFIX_GZ),sizeof(COMPRESS_SUFFIX_BZ2)),sizeof(COMPRESS_SUFFIX_XZ))
102 
103 /*
104  * Compression types
105  */
106 #define	COMPRESS_TYPES  4	/* Number of supported compression types */
107 
108 #define	COMPRESS_NONE	0
109 #define	COMPRESS_GZIP	1
110 #define	COMPRESS_BZIP2	2
111 #define	COMPRESS_XZ	3
112 
113 /*
114  * Bit-values for the 'flags' parsed from a config-file entry.
115  */
116 #define	CE_BINARY	0x0008	/* Logfile is in binary, do not add status */
117 				/*    messages to logfile(s) when rotating. */
118 #define	CE_NOSIGNAL	0x0010	/* There is no process to signal when */
119 				/*    trimming this file. */
120 #define	CE_TRIMAT	0x0020	/* trim file at a specific time. */
121 #define	CE_GLOB		0x0040	/* name of the log is file name pattern. */
122 #define	CE_SIGNALGROUP	0x0080	/* Signal a process-group instead of a single */
123 				/*    process when trimming this file. */
124 #define	CE_CREATE	0x0100	/* Create the log file if it does not exist. */
125 #define	CE_NODUMP	0x0200	/* Set 'nodump' on newly created log file. */
126 
127 #define	MIN_PID         5	/* Don't touch pids lower than this */
128 #define	MAX_PID		99999	/* was lower, see /usr/include/sys/proc.h */
129 
130 #define	kbytes(size)  (((size) + 1023) >> 10)
131 
132 #define	DEFAULT_MARKER	"<default>"
133 #define	DEBUG_MARKER	"<debug>"
134 #define	INCLUDE_MARKER	"<include>"
135 #define	DEFAULT_TIMEFNAME_FMT	"%Y%m%dT%H%M%S"
136 
137 #define	MAX_OLDLOGS 65536	/* Default maximum number of old logfiles */
138 
139 struct compress_types {
140 	const char *flag;	/* Flag in configuration file */
141 	const char *suffix;	/* Compression suffix */
142 	const char *path;	/* Path to compression program */
143 };
144 
145 const struct compress_types compress_type[COMPRESS_TYPES] = {
146 	{ "", "", "" },					/* no compression */
147 	{ "Z", COMPRESS_SUFFIX_GZ, _PATH_GZIP },	/* gzip compression */
148 	{ "J", COMPRESS_SUFFIX_BZ2, _PATH_BZIP2 },	/* bzip2 compression */
149 	{ "X", COMPRESS_SUFFIX_XZ, _PATH_XZ }		/* xz compression */
150 };
151 
152 struct conf_entry {
153 	STAILQ_ENTRY(conf_entry) cf_nextp;
154 	char *log;		/* Name of the log */
155 	char *pid_file;		/* PID file */
156 	char *r_reason;		/* The reason this file is being rotated */
157 	int firstcreate;	/* Creating log for the first time (-C). */
158 	int rotate;		/* Non-zero if this file should be rotated */
159 	int fsize;		/* size found for the log file */
160 	uid_t uid;		/* Owner of log */
161 	gid_t gid;		/* Group of log */
162 	int numlogs;		/* Number of logs to keep */
163 	int trsize;		/* Size cutoff to trigger trimming the log */
164 	int hours;		/* Hours between log trimming */
165 	struct ptime_data *trim_at;	/* Specific time to do trimming */
166 	unsigned int permissions;	/* File permissions on the log */
167 	int flags;		/* CE_BINARY */
168 	int compress;		/* Compression */
169 	int sig;		/* Signal to send */
170 	int def_cfg;		/* Using the <default> rule for this file */
171 };
172 
173 struct sigwork_entry {
174 	SLIST_ENTRY(sigwork_entry) sw_nextp;
175 	int	 sw_signum;		/* the signal to send */
176 	int	 sw_pidok;		/* true if pid value is valid */
177 	pid_t	 sw_pid;		/* the process id from the PID file */
178 	const char *sw_pidtype;		/* "daemon" or "process group" */
179 	char	 sw_fname[1];		/* file the PID was read from */
180 };
181 
182 struct zipwork_entry {
183 	SLIST_ENTRY(zipwork_entry) zw_nextp;
184 	const struct conf_entry *zw_conf;	/* for chown/perm/flag info */
185 	const struct sigwork_entry *zw_swork;	/* to know success of signal */
186 	int	 zw_fsize;		/* size of the file to compress */
187 	char	 zw_fname[1];		/* the file to compress */
188 };
189 
190 struct include_entry {
191 	STAILQ_ENTRY(include_entry) inc_nextp;
192 	const char *file;	/* Name of file to process */
193 };
194 
195 struct oldlog_entry {
196 	char *fname;		/* Filename of the log file */
197 	time_t t;		/* Parsed timestamp of the logfile */
198 };
199 
200 typedef enum {
201 	FREE_ENT, KEEP_ENT
202 }	fk_entry;
203 
204 STAILQ_HEAD(cflist, conf_entry);
205 SLIST_HEAD(swlisthead, sigwork_entry) swhead = SLIST_HEAD_INITIALIZER(swhead);
206 SLIST_HEAD(zwlisthead, zipwork_entry) zwhead = SLIST_HEAD_INITIALIZER(zwhead);
207 STAILQ_HEAD(ilist, include_entry);
208 
209 int dbg_at_times;		/* -D Show details of 'trim_at' code */
210 
211 int archtodir = 0;		/* Archive old logfiles to other directory */
212 int createlogs;			/* Create (non-GLOB) logfiles which do not */
213 				/*    already exist.  1=='for entries with */
214 				/*    C flag', 2=='for all entries'. */
215 int verbose = 0;		/* Print out what's going on */
216 int needroot = 1;		/* Root privs are necessary */
217 int noaction = 0;		/* Don't do anything, just show it */
218 int norotate = 0;		/* Don't rotate */
219 int nosignal;			/* Do not send any signals */
220 int enforcepid = 0;		/* If PID file does not exist or empty, do nothing */
221 int force = 0;			/* Force the trim no matter what */
222 int rotatereq = 0;		/* -R = Always rotate the file(s) as given */
223 				/*    on the command (this also requires   */
224 				/*    that a list of files *are* given on  */
225 				/*    the run command). */
226 char *requestor;		/* The name given on a -R request */
227 char *timefnamefmt = NULL;	/* Use time based filenames instead of .0 etc */
228 char *archdirname;		/* Directory path to old logfiles archive */
229 char *destdir = NULL;		/* Directory to treat at root for logs */
230 const char *conf;		/* Configuration file to use */
231 
232 struct ptime_data *dbg_timenow;	/* A "timenow" value set via -D option */
233 struct ptime_data *timenow;	/* The time to use for checking at-fields */
234 
235 #define	DAYTIME_LEN	16
236 char daytime[DAYTIME_LEN];	/* The current time in human readable form,
237 				 * used for rotation-tracking messages. */
238 char hostname[MAXHOSTNAMELEN];	/* hostname */
239 
240 const char *path_syslogpid = _PATH_SYSLOGPID;
241 
242 static struct cflist *get_worklist(char **files);
243 static void parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p,
244 		    struct conf_entry *defconf_p, struct ilist *inclist);
245 static void add_to_queue(const char *fname, struct ilist *inclist);
246 static char *sob(char *p);
247 static char *son(char *p);
248 static int isnumberstr(const char *);
249 static int isglobstr(const char *);
250 static char *missing_field(char *p, char *errline);
251 static void	 change_attrs(const char *, const struct conf_entry *);
252 static const char *get_logfile_suffix(const char *logfile);
253 static fk_entry	 do_entry(struct conf_entry *);
254 static fk_entry	 do_rotate(const struct conf_entry *);
255 static void	 do_sigwork(struct sigwork_entry *);
256 static void	 do_zipwork(struct zipwork_entry *);
257 static struct sigwork_entry *
258 		 save_sigwork(const struct conf_entry *);
259 static struct zipwork_entry *
260 		 save_zipwork(const struct conf_entry *, const struct
261 		    sigwork_entry *, int, const char *);
262 static void	 set_swpid(struct sigwork_entry *, const struct conf_entry *);
263 static int	 sizefile(const char *);
264 static void expand_globs(struct cflist *work_p, struct cflist *glob_p);
265 static void free_clist(struct cflist *list);
266 static void free_entry(struct conf_entry *ent);
267 static struct conf_entry *init_entry(const char *fname,
268 		struct conf_entry *src_entry);
269 static void parse_args(int argc, char **argv);
270 static int parse_doption(const char *doption);
271 static void usage(void);
272 static int log_trim(const char *logname, const struct conf_entry *log_ent);
273 static int age_old_log(char *file);
274 static void savelog(char *from, char *to);
275 static void createdir(const struct conf_entry *ent, char *dirpart);
276 static void createlog(const struct conf_entry *ent);
277 
278 /*
279  * All the following take a parameter of 'int', but expect values in the
280  * range of unsigned char.  Define wrappers which take values of type 'char',
281  * whether signed or unsigned, and ensure they end up in the right range.
282  */
283 #define	isdigitch(Anychar) isdigit((u_char)(Anychar))
284 #define	isprintch(Anychar) isprint((u_char)(Anychar))
285 #define	isspacech(Anychar) isspace((u_char)(Anychar))
286 #define	tolowerch(Anychar) tolower((u_char)(Anychar))
287 
288 int
289 main(int argc, char **argv)
290 {
291 	struct cflist *worklist;
292 	struct conf_entry *p;
293 	struct sigwork_entry *stmp;
294 	struct zipwork_entry *ztmp;
295 
296 	SLIST_INIT(&swhead);
297 	SLIST_INIT(&zwhead);
298 
299 	parse_args(argc, argv);
300 	argc -= optind;
301 	argv += optind;
302 
303 	if (needroot && getuid() && geteuid())
304 		errx(1, "must have root privs");
305 	worklist = get_worklist(argv);
306 
307 	/*
308 	 * Rotate all the files which need to be rotated.  Note that
309 	 * some users have *hundreds* of entries in newsyslog.conf!
310 	 */
311 	while (!STAILQ_EMPTY(worklist)) {
312 		p = STAILQ_FIRST(worklist);
313 		STAILQ_REMOVE_HEAD(worklist, cf_nextp);
314 		if (do_entry(p) == FREE_ENT)
315 			free_entry(p);
316 	}
317 
318 	/*
319 	 * Send signals to any processes which need a signal to tell
320 	 * them to close and re-open the log file(s) we have rotated.
321 	 * Note that zipwork_entries include pointers to these
322 	 * sigwork_entry's, so we can not free the entries here.
323 	 */
324 	if (!SLIST_EMPTY(&swhead)) {
325 		if (noaction || verbose)
326 			printf("Signal all daemon process(es)...\n");
327 		SLIST_FOREACH(stmp, &swhead, sw_nextp)
328 			do_sigwork(stmp);
329 		if (noaction)
330 			printf("\tsleep 10\n");
331 		else {
332 			if (verbose)
333 				printf("Pause 10 seconds to allow daemon(s)"
334 				    " to close log file(s)\n");
335 			sleep(10);
336 		}
337 	}
338 	/*
339 	 * Compress all files that we're expected to compress, now
340 	 * that all processes should have closed the files which
341 	 * have been rotated.
342 	 */
343 	if (!SLIST_EMPTY(&zwhead)) {
344 		if (noaction || verbose)
345 			printf("Compress all rotated log file(s)...\n");
346 		while (!SLIST_EMPTY(&zwhead)) {
347 			ztmp = SLIST_FIRST(&zwhead);
348 			do_zipwork(ztmp);
349 			SLIST_REMOVE_HEAD(&zwhead, zw_nextp);
350 			free(ztmp);
351 		}
352 	}
353 	/* Now free all the sigwork entries. */
354 	while (!SLIST_EMPTY(&swhead)) {
355 		stmp = SLIST_FIRST(&swhead);
356 		SLIST_REMOVE_HEAD(&swhead, sw_nextp);
357 		free(stmp);
358 	}
359 
360 	while (wait(NULL) > 0 || errno == EINTR)
361 		;
362 	return (0);
363 }
364 
365 static struct conf_entry *
366 init_entry(const char *fname, struct conf_entry *src_entry)
367 {
368 	struct conf_entry *tempwork;
369 
370 	if (verbose > 4)
371 		printf("\t--> [creating entry for %s]\n", fname);
372 
373 	tempwork = malloc(sizeof(struct conf_entry));
374 	if (tempwork == NULL)
375 		err(1, "malloc of conf_entry for %s", fname);
376 
377 	if (destdir == NULL || fname[0] != '/')
378 		tempwork->log = strdup(fname);
379 	else
380 		asprintf(&tempwork->log, "%s%s", destdir, fname);
381 	if (tempwork->log == NULL)
382 		err(1, "strdup for %s", fname);
383 
384 	if (src_entry != NULL) {
385 		tempwork->pid_file = NULL;
386 		if (src_entry->pid_file)
387 			tempwork->pid_file = strdup(src_entry->pid_file);
388 		tempwork->r_reason = NULL;
389 		tempwork->firstcreate = 0;
390 		tempwork->rotate = 0;
391 		tempwork->fsize = -1;
392 		tempwork->uid = src_entry->uid;
393 		tempwork->gid = src_entry->gid;
394 		tempwork->numlogs = src_entry->numlogs;
395 		tempwork->trsize = src_entry->trsize;
396 		tempwork->hours = src_entry->hours;
397 		tempwork->trim_at = NULL;
398 		if (src_entry->trim_at != NULL)
399 			tempwork->trim_at = ptime_init(src_entry->trim_at);
400 		tempwork->permissions = src_entry->permissions;
401 		tempwork->flags = src_entry->flags;
402 		tempwork->compress = src_entry->compress;
403 		tempwork->sig = src_entry->sig;
404 		tempwork->def_cfg = src_entry->def_cfg;
405 	} else {
406 		/* Initialize as a "do-nothing" entry */
407 		tempwork->pid_file = NULL;
408 		tempwork->r_reason = NULL;
409 		tempwork->firstcreate = 0;
410 		tempwork->rotate = 0;
411 		tempwork->fsize = -1;
412 		tempwork->uid = (uid_t)-1;
413 		tempwork->gid = (gid_t)-1;
414 		tempwork->numlogs = 1;
415 		tempwork->trsize = -1;
416 		tempwork->hours = -1;
417 		tempwork->trim_at = NULL;
418 		tempwork->permissions = 0;
419 		tempwork->flags = 0;
420 		tempwork->compress = COMPRESS_NONE;
421 		tempwork->sig = SIGHUP;
422 		tempwork->def_cfg = 0;
423 	}
424 
425 	return (tempwork);
426 }
427 
428 static void
429 free_entry(struct conf_entry *ent)
430 {
431 
432 	if (ent == NULL)
433 		return;
434 
435 	if (ent->log != NULL) {
436 		if (verbose > 4)
437 			printf("\t--> [freeing entry for %s]\n", ent->log);
438 		free(ent->log);
439 		ent->log = NULL;
440 	}
441 
442 	if (ent->pid_file != NULL) {
443 		free(ent->pid_file);
444 		ent->pid_file = NULL;
445 	}
446 
447 	if (ent->r_reason != NULL) {
448 		free(ent->r_reason);
449 		ent->r_reason = NULL;
450 	}
451 
452 	if (ent->trim_at != NULL) {
453 		ptime_free(ent->trim_at);
454 		ent->trim_at = NULL;
455 	}
456 
457 	free(ent);
458 }
459 
460 static void
461 free_clist(struct cflist *list)
462 {
463 	struct conf_entry *ent;
464 
465 	while (!STAILQ_EMPTY(list)) {
466 		ent = STAILQ_FIRST(list);
467 		STAILQ_REMOVE_HEAD(list, cf_nextp);
468 		free_entry(ent);
469 	}
470 
471 	free(list);
472 	list = NULL;
473 }
474 
475 static fk_entry
476 do_entry(struct conf_entry * ent)
477 {
478 #define	REASON_MAX	80
479 	int modtime;
480 	fk_entry free_or_keep;
481 	double diffsecs;
482 	char temp_reason[REASON_MAX];
483 
484 	free_or_keep = FREE_ENT;
485 	if (verbose)
486 		printf("%s <%d%s>: ", ent->log, ent->numlogs,
487 		    compress_type[ent->compress].flag);
488 	ent->fsize = sizefile(ent->log);
489 	modtime = age_old_log(ent->log);
490 	ent->rotate = 0;
491 	ent->firstcreate = 0;
492 	if (ent->fsize < 0) {
493 		/*
494 		 * If either the C flag or the -C option was specified,
495 		 * and if we won't be creating the file, then have the
496 		 * verbose message include a hint as to why the file
497 		 * will not be created.
498 		 */
499 		temp_reason[0] = '\0';
500 		if (createlogs > 1)
501 			ent->firstcreate = 1;
502 		else if ((ent->flags & CE_CREATE) && createlogs)
503 			ent->firstcreate = 1;
504 		else if (ent->flags & CE_CREATE)
505 			strlcpy(temp_reason, " (no -C option)", REASON_MAX);
506 		else if (createlogs)
507 			strlcpy(temp_reason, " (no C flag)", REASON_MAX);
508 
509 		if (ent->firstcreate) {
510 			if (verbose)
511 				printf("does not exist -> will create.\n");
512 			createlog(ent);
513 		} else if (verbose) {
514 			printf("does not exist, skipped%s.\n", temp_reason);
515 		}
516 	} else {
517 		if (ent->flags & CE_TRIMAT && !force && !rotatereq) {
518 			diffsecs = ptimeget_diff(timenow, ent->trim_at);
519 			if (diffsecs < 0.0) {
520 				/* trim_at is some time in the future. */
521 				if (verbose) {
522 					ptime_adjust4dst(ent->trim_at,
523 					    timenow);
524 					printf("--> will trim at %s",
525 					    ptimeget_ctime(ent->trim_at));
526 				}
527 				return (free_or_keep);
528 			} else if (diffsecs >= 3600.0) {
529 				/*
530 				 * trim_at is more than an hour in the past,
531 				 * so find the next valid trim_at time, and
532 				 * tell the user what that will be.
533 				 */
534 				if (verbose && dbg_at_times)
535 					printf("\n\t--> prev trim at %s\t",
536 					    ptimeget_ctime(ent->trim_at));
537 				if (verbose) {
538 					ptimeset_nxtime(ent->trim_at);
539 					printf("--> will trim at %s",
540 					    ptimeget_ctime(ent->trim_at));
541 				}
542 				return (free_or_keep);
543 			} else if (verbose && noaction && dbg_at_times) {
544 				/*
545 				 * If we are just debugging at-times, then
546 				 * a detailed message is helpful.  Also
547 				 * skip "doing" any commands, since they
548 				 * would all be turned off by no-action.
549 				 */
550 				printf("\n\t--> timematch at %s",
551 				    ptimeget_ctime(ent->trim_at));
552 				return (free_or_keep);
553 			} else if (verbose && ent->hours <= 0) {
554 				printf("--> time is up\n");
555 			}
556 		}
557 		if (verbose && (ent->trsize > 0))
558 			printf("size (Kb): %d [%d] ", ent->fsize, ent->trsize);
559 		if (verbose && (ent->hours > 0))
560 			printf(" age (hr): %d [%d] ", modtime, ent->hours);
561 
562 		/*
563 		 * Figure out if this logfile needs to be rotated.
564 		 */
565 		temp_reason[0] = '\0';
566 		if (rotatereq) {
567 			ent->rotate = 1;
568 			snprintf(temp_reason, REASON_MAX, " due to -R from %s",
569 			    requestor);
570 		} else if (force) {
571 			ent->rotate = 1;
572 			snprintf(temp_reason, REASON_MAX, " due to -F request");
573 		} else if ((ent->trsize > 0) && (ent->fsize >= ent->trsize)) {
574 			ent->rotate = 1;
575 			snprintf(temp_reason, REASON_MAX, " due to size>%dK",
576 			    ent->trsize);
577 		} else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) {
578 			ent->rotate = 1;
579 		} else if ((ent->hours > 0) && ((modtime >= ent->hours) ||
580 		    (modtime < 0))) {
581 			ent->rotate = 1;
582 		}
583 
584 		/*
585 		 * If the file needs to be rotated, then rotate it.
586 		 */
587 		if (ent->rotate && !norotate) {
588 			if (temp_reason[0] != '\0')
589 				ent->r_reason = strdup(temp_reason);
590 			if (verbose)
591 				printf("--> trimming log....\n");
592 			if (noaction && !verbose)
593 				printf("%s <%d%s>: trimming\n", ent->log,
594 				    ent->numlogs,
595 				    compress_type[ent->compress].flag);
596 			free_or_keep = do_rotate(ent);
597 		} else {
598 			if (verbose)
599 				printf("--> skipping\n");
600 		}
601 	}
602 	return (free_or_keep);
603 #undef REASON_MAX
604 }
605 
606 static void
607 parse_args(int argc, char **argv)
608 {
609 	int ch;
610 	char *p;
611 
612 	timenow = ptime_init(NULL);
613 	ptimeset_time(timenow, time(NULL));
614 	strlcpy(daytime, ptimeget_ctime(timenow) + 4, DAYTIME_LEN);
615 
616 	/* Let's get our hostname */
617 	gethostname(hostname, sizeof(hostname));
618 
619 	/* Truncate domain */
620 	if ((p = strchr(hostname, '.')) != NULL)
621 		*p = '\0';
622 
623 	/* Parse command line options. */
624 	while ((ch = getopt(argc, argv, "a:d:f:nrst:vCD:FNPR:S:")) != -1)
625 		switch (ch) {
626 		case 'a':
627 			archtodir++;
628 			archdirname = optarg;
629 			break;
630 		case 'd':
631 			destdir = optarg;
632 			break;
633 		case 'f':
634 			conf = optarg;
635 			break;
636 		case 'n':
637 			noaction++;
638 			break;
639 		case 'r':
640 			needroot = 0;
641 			break;
642 		case 's':
643 			nosignal = 1;
644 			break;
645 		case 't':
646 			if (optarg[0] == '\0' ||
647 			    strcmp(optarg, "DEFAULT") == 0)
648 				timefnamefmt = strdup(DEFAULT_TIMEFNAME_FMT);
649 			else
650 				timefnamefmt = strdup(optarg);
651 			break;
652 		case 'v':
653 			verbose++;
654 			break;
655 		case 'C':
656 			/* Useful for things like rc.diskless... */
657 			createlogs++;
658 			break;
659 		case 'D':
660 			/*
661 			 * Set some debugging option.  The specific option
662 			 * depends on the value of optarg.  These options
663 			 * may come and go without notice or documentation.
664 			 */
665 			if (parse_doption(optarg))
666 				break;
667 			usage();
668 			/* NOTREACHED */
669 		case 'F':
670 			force++;
671 			break;
672 		case 'N':
673 			norotate++;
674 			break;
675 		case 'P':
676 			enforcepid++;
677 			break;
678 		case 'R':
679 			rotatereq++;
680 			requestor = strdup(optarg);
681 			break;
682 		case 'S':
683 			path_syslogpid = optarg;
684 			break;
685 		case 'm':	/* Used by OpenBSD for "monitor mode" */
686 		default:
687 			usage();
688 			/* NOTREACHED */
689 		}
690 
691 	if (force && norotate) {
692 		warnx("Only one of -F and -N may be specified.");
693 		usage();
694 		/* NOTREACHED */
695 	}
696 
697 	if (rotatereq) {
698 		if (optind == argc) {
699 			warnx("At least one filename must be given when -R is specified.");
700 			usage();
701 			/* NOTREACHED */
702 		}
703 		/* Make sure "requestor" value is safe for a syslog message. */
704 		for (p = requestor; *p != '\0'; p++) {
705 			if (!isprintch(*p) && (*p != '\t'))
706 				*p = '.';
707 		}
708 	}
709 
710 	if (dbg_timenow) {
711 		/*
712 		 * Note that the 'daytime' variable is not changed.
713 		 * That is only used in messages that track when a
714 		 * logfile is rotated, and if a file *is* rotated,
715 		 * then it will still rotated at the "real now" time.
716 		 */
717 		ptime_free(timenow);
718 		timenow = dbg_timenow;
719 		fprintf(stderr, "Debug: Running as if TimeNow is %s",
720 		    ptimeget_ctime(dbg_timenow));
721 	}
722 
723 }
724 
725 /*
726  * These debugging options are mainly meant for developer use, such
727  * as writing regression-tests.  They would not be needed by users
728  * during normal operation of newsyslog...
729  */
730 static int
731 parse_doption(const char *doption)
732 {
733 	const char TN[] = "TN=";
734 	int res;
735 
736 	if (strncmp(doption, TN, sizeof(TN) - 1) == 0) {
737 		/*
738 		 * The "TimeNow" debugging option.  This might be off
739 		 * by an hour when crossing a timezone change.
740 		 */
741 		dbg_timenow = ptime_init(NULL);
742 		res = ptime_relparse(dbg_timenow, PTM_PARSE_ISO8601,
743 		    time(NULL), doption + sizeof(TN) - 1);
744 		if (res == -2) {
745 			warnx("Non-existent time specified on -D %s", doption);
746 			return (0);			/* failure */
747 		} else if (res < 0) {
748 			warnx("Malformed time given on -D %s", doption);
749 			return (0);			/* failure */
750 		}
751 		return (1);			/* successfully parsed */
752 
753 	}
754 
755 	if (strcmp(doption, "ats") == 0) {
756 		dbg_at_times++;
757 		return (1);			/* successfully parsed */
758 	}
759 
760 	/* XXX - This check could probably be dropped. */
761 	if ((strcmp(doption, "neworder") == 0) || (strcmp(doption, "oldorder")
762 	    == 0)) {
763 		warnx("NOTE: newsyslog always uses 'neworder'.");
764 		return (1);			/* successfully parsed */
765 	}
766 
767 	warnx("Unknown -D (debug) option: '%s'", doption);
768 	return (0);				/* failure */
769 }
770 
771 static void
772 usage(void)
773 {
774 
775 	fprintf(stderr,
776 	    "usage: newsyslog [-CFNnrsv] [-a directory] [-d directory] [-f config-file]\n"
777 	    "                 [-S pidfile] [-t timefmt ] [ [-R requestor] filename ... ]\n");
778 	exit(1);
779 }
780 
781 /*
782  * Parse a configuration file and return a linked list of all the logs
783  * which should be processed.
784  */
785 static struct cflist *
786 get_worklist(char **files)
787 {
788 	FILE *f;
789 	char **given;
790 	struct cflist *cmdlist, *filelist, *globlist;
791 	struct conf_entry *defconf, *dupent, *ent;
792 	struct ilist inclist;
793 	struct include_entry *inc;
794 	int gmatch, fnres;
795 
796 	defconf = NULL;
797 	STAILQ_INIT(&inclist);
798 
799 	filelist = malloc(sizeof(struct cflist));
800 	if (filelist == NULL)
801 		err(1, "malloc of filelist");
802 	STAILQ_INIT(filelist);
803 	globlist = malloc(sizeof(struct cflist));
804 	if (globlist == NULL)
805 		err(1, "malloc of globlist");
806 	STAILQ_INIT(globlist);
807 
808 	inc = malloc(sizeof(struct include_entry));
809 	if (inc == NULL)
810 		err(1, "malloc of inc");
811 	inc->file = conf;
812 	if (inc->file == NULL)
813 		inc->file = _PATH_CONF;
814 	STAILQ_INSERT_TAIL(&inclist, inc, inc_nextp);
815 
816 	STAILQ_FOREACH(inc, &inclist, inc_nextp) {
817 		if (strcmp(inc->file, "-") != 0)
818 			f = fopen(inc->file, "r");
819 		else {
820 			f = stdin;
821 			inc->file = "<stdin>";
822 		}
823 		if (!f)
824 			err(1, "%s", inc->file);
825 
826 		if (verbose)
827 			printf("Processing %s\n", inc->file);
828 		parse_file(f, filelist, globlist, defconf, &inclist);
829 		fclose(f);
830 	}
831 
832 	/*
833 	 * All config-file information has been read in and turned into
834 	 * a filelist and a globlist.  If there were no specific files
835 	 * given on the run command, then the only thing left to do is to
836 	 * call a routine which finds all files matched by the globlist
837 	 * and adds them to the filelist.  Then return the worklist.
838 	 */
839 	if (*files == NULL) {
840 		expand_globs(filelist, globlist);
841 		free_clist(globlist);
842 		if (defconf != NULL)
843 			free_entry(defconf);
844 		return (filelist);
845 		/* NOTREACHED */
846 	}
847 
848 	/*
849 	 * If newsyslog was given a specific list of files to process,
850 	 * it may be that some of those files were not listed in any
851 	 * config file.  Those unlisted files should get the default
852 	 * rotation action.  First, create the default-rotation action
853 	 * if none was found in a system config file.
854 	 */
855 	if (defconf == NULL) {
856 		defconf = init_entry(DEFAULT_MARKER, NULL);
857 		defconf->numlogs = 3;
858 		defconf->trsize = 50;
859 		defconf->permissions = S_IRUSR|S_IWUSR;
860 	}
861 
862 	/*
863 	 * If newsyslog was run with a list of specific filenames,
864 	 * then create a new worklist which has only those files in
865 	 * it, picking up the rotation-rules for those files from
866 	 * the original filelist.
867 	 *
868 	 * XXX - Note that this will copy multiple rules for a single
869 	 *	logfile, if multiple entries are an exact match for
870 	 *	that file.  That matches the historic behavior, but do
871 	 *	we want to continue to allow it?  If so, it should
872 	 *	probably be handled more intelligently.
873 	 */
874 	cmdlist = malloc(sizeof(struct cflist));
875 	if (cmdlist == NULL)
876 		err(1, "malloc of cmdlist");
877 	STAILQ_INIT(cmdlist);
878 
879 	for (given = files; *given; ++given) {
880 		/*
881 		 * First try to find exact-matches for this given file.
882 		 */
883 		gmatch = 0;
884 		STAILQ_FOREACH(ent, filelist, cf_nextp) {
885 			if (strcmp(ent->log, *given) == 0) {
886 				gmatch++;
887 				dupent = init_entry(*given, ent);
888 				STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp);
889 			}
890 		}
891 		if (gmatch) {
892 			if (verbose > 2)
893 				printf("\t+ Matched entry %s\n", *given);
894 			continue;
895 		}
896 
897 		/*
898 		 * There was no exact-match for this given file, so look
899 		 * for a "glob" entry which does match.
900 		 */
901 		gmatch = 0;
902 		if (verbose > 2 && globlist != NULL)
903 			printf("\t+ Checking globs for %s\n", *given);
904 		STAILQ_FOREACH(ent, globlist, cf_nextp) {
905 			fnres = fnmatch(ent->log, *given, FNM_PATHNAME);
906 			if (verbose > 2)
907 				printf("\t+    = %d for pattern %s\n", fnres,
908 				    ent->log);
909 			if (fnres == 0) {
910 				gmatch++;
911 				dupent = init_entry(*given, ent);
912 				/* This new entry is not a glob! */
913 				dupent->flags &= ~CE_GLOB;
914 				STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp);
915 				/* Only allow a match to one glob-entry */
916 				break;
917 			}
918 		}
919 		if (gmatch) {
920 			if (verbose > 2)
921 				printf("\t+ Matched %s via %s\n", *given,
922 				    ent->log);
923 			continue;
924 		}
925 
926 		/*
927 		 * This given file was not found in any config file, so
928 		 * add a worklist item based on the default entry.
929 		 */
930 		if (verbose > 2)
931 			printf("\t+ No entry matched %s  (will use %s)\n",
932 			    *given, DEFAULT_MARKER);
933 		dupent = init_entry(*given, defconf);
934 		/* Mark that it was *not* found in a config file */
935 		dupent->def_cfg = 1;
936 		STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp);
937 	}
938 
939 	/*
940 	 * Free all the entries in the original work list, the list of
941 	 * glob entries, and the default entry.
942 	 */
943 	free_clist(filelist);
944 	free_clist(globlist);
945 	free_entry(defconf);
946 
947 	/* And finally, return a worklist which matches the given files. */
948 	return (cmdlist);
949 }
950 
951 /*
952  * Expand the list of entries with filename patterns, and add all files
953  * which match those glob-entries onto the worklist.
954  */
955 static void
956 expand_globs(struct cflist *work_p, struct cflist *glob_p)
957 {
958 	int gmatch, gres;
959 	size_t i;
960 	char *mfname;
961 	struct conf_entry *dupent, *ent, *globent;
962 	glob_t pglob;
963 	struct stat st_fm;
964 
965 	/*
966 	 * The worklist contains all fully-specified (non-GLOB) names.
967 	 *
968 	 * Now expand the list of filename-pattern (GLOB) entries into
969 	 * a second list, which (by definition) will only match files
970 	 * that already exist.  Do not add a glob-related entry for any
971 	 * file which already exists in the fully-specified list.
972 	 */
973 	STAILQ_FOREACH(globent, glob_p, cf_nextp) {
974 		gres = glob(globent->log, GLOB_NOCHECK, NULL, &pglob);
975 		if (gres != 0) {
976 			warn("cannot expand pattern (%d): %s", gres,
977 			    globent->log);
978 			continue;
979 		}
980 
981 		if (verbose > 2)
982 			printf("\t+ Expanding pattern %s\n", globent->log);
983 		for (i = 0; i < pglob.gl_matchc; i++) {
984 			mfname = pglob.gl_pathv[i];
985 
986 			/* See if this file already has a specific entry. */
987 			gmatch = 0;
988 			STAILQ_FOREACH(ent, work_p, cf_nextp) {
989 				if (strcmp(mfname, ent->log) == 0) {
990 					gmatch++;
991 					break;
992 				}
993 			}
994 			if (gmatch)
995 				continue;
996 
997 			/* Make sure the named matched is a file. */
998 			gres = lstat(mfname, &st_fm);
999 			if (gres != 0) {
1000 				/* Error on a file that glob() matched?!? */
1001 				warn("Skipping %s - lstat() error", mfname);
1002 				continue;
1003 			}
1004 			if (!S_ISREG(st_fm.st_mode)) {
1005 				/* We only rotate files! */
1006 				if (verbose > 2)
1007 					printf("\t+  . skipping %s (!file)\n",
1008 					    mfname);
1009 				continue;
1010 			}
1011 
1012 			if (verbose > 2)
1013 				printf("\t+  . add file %s\n", mfname);
1014 			dupent = init_entry(mfname, globent);
1015 			/* This new entry is not a glob! */
1016 			dupent->flags &= ~CE_GLOB;
1017 
1018 			/* Add to the worklist. */
1019 			STAILQ_INSERT_TAIL(work_p, dupent, cf_nextp);
1020 		}
1021 		globfree(&pglob);
1022 		if (verbose > 2)
1023 			printf("\t+ Done with pattern %s\n", globent->log);
1024 	}
1025 }
1026 
1027 /*
1028  * Parse a configuration file and update a linked list of all the logs to
1029  * process.
1030  */
1031 static void
1032 parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p,
1033     struct conf_entry *defconf_p, struct ilist *inclist)
1034 {
1035 	char line[BUFSIZ], *parse, *q;
1036 	char *cp, *errline, *group;
1037 	struct conf_entry *working;
1038 	struct passwd *pwd;
1039 	struct group *grp;
1040 	glob_t pglob;
1041 	int eol, ptm_opts, res, special;
1042 	size_t i;
1043 
1044 	errline = NULL;
1045 	while (fgets(line, BUFSIZ, cf)) {
1046 		if ((line[0] == '\n') || (line[0] == '#') ||
1047 		    (strlen(line) == 0))
1048 			continue;
1049 		if (errline != NULL)
1050 			free(errline);
1051 		errline = strdup(line);
1052 		for (cp = line + 1; *cp != '\0'; cp++) {
1053 			if (*cp != '#')
1054 				continue;
1055 			if (*(cp - 1) == '\\') {
1056 				strcpy(cp - 1, cp);
1057 				cp--;
1058 				continue;
1059 			}
1060 			*cp = '\0';
1061 			break;
1062 		}
1063 
1064 		q = parse = missing_field(sob(line), errline);
1065 		parse = son(line);
1066 		if (!*parse)
1067 			errx(1, "malformed line (missing fields):\n%s",
1068 			    errline);
1069 		*parse = '\0';
1070 
1071 		/*
1072 		 * Allow people to set debug options via the config file.
1073 		 * (NOTE: debug options are undocumented, and may disappear
1074 		 * at any time, etc).
1075 		 */
1076 		if (strcasecmp(DEBUG_MARKER, q) == 0) {
1077 			q = parse = missing_field(sob(++parse), errline);
1078 			parse = son(parse);
1079 			if (!*parse)
1080 				warnx("debug line specifies no option:\n%s",
1081 				    errline);
1082 			else {
1083 				*parse = '\0';
1084 				parse_doption(q);
1085 			}
1086 			continue;
1087 		} else if (strcasecmp(INCLUDE_MARKER, q) == 0) {
1088 			if (verbose)
1089 				printf("Found: %s", errline);
1090 			q = parse = missing_field(sob(++parse), errline);
1091 			parse = son(parse);
1092 			if (!*parse) {
1093 				warnx("include line missing argument:\n%s",
1094 				    errline);
1095 				continue;
1096 			}
1097 
1098 			*parse = '\0';
1099 
1100 			if (isglobstr(q)) {
1101 				res = glob(q, GLOB_NOCHECK, NULL, &pglob);
1102 				if (res != 0) {
1103 					warn("cannot expand pattern (%d): %s",
1104 					    res, q);
1105 					continue;
1106 				}
1107 
1108 				if (verbose > 2)
1109 					printf("\t+ Expanding pattern %s\n", q);
1110 
1111 				for (i = 0; i < pglob.gl_matchc; i++)
1112 					add_to_queue(pglob.gl_pathv[i],
1113 					    inclist);
1114 				globfree(&pglob);
1115 			} else
1116 				add_to_queue(q, inclist);
1117 			continue;
1118 		}
1119 
1120 		special = 0;
1121 		working = init_entry(q, NULL);
1122 		if (strcasecmp(DEFAULT_MARKER, q) == 0) {
1123 			special = 1;
1124 			if (defconf_p != NULL) {
1125 				warnx("Ignoring duplicate entry for %s!", q);
1126 				free_entry(working);
1127 				continue;
1128 			}
1129 			defconf_p = working;
1130 		}
1131 
1132 		q = parse = missing_field(sob(++parse), errline);
1133 		parse = son(parse);
1134 		if (!*parse)
1135 			errx(1, "malformed line (missing fields):\n%s",
1136 			    errline);
1137 		*parse = '\0';
1138 		if ((group = strchr(q, ':')) != NULL ||
1139 		    (group = strrchr(q, '.')) != NULL) {
1140 			*group++ = '\0';
1141 			if (*q) {
1142 				if (!(isnumberstr(q))) {
1143 					if ((pwd = getpwnam(q)) == NULL)
1144 						errx(1,
1145 				     "error in config file; unknown user:\n%s",
1146 						    errline);
1147 					working->uid = pwd->pw_uid;
1148 				} else
1149 					working->uid = atoi(q);
1150 			} else
1151 				working->uid = (uid_t)-1;
1152 
1153 			q = group;
1154 			if (*q) {
1155 				if (!(isnumberstr(q))) {
1156 					if ((grp = getgrnam(q)) == NULL)
1157 						errx(1,
1158 				    "error in config file; unknown group:\n%s",
1159 						    errline);
1160 					working->gid = grp->gr_gid;
1161 				} else
1162 					working->gid = atoi(q);
1163 			} else
1164 				working->gid = (gid_t)-1;
1165 
1166 			q = parse = missing_field(sob(++parse), errline);
1167 			parse = son(parse);
1168 			if (!*parse)
1169 				errx(1, "malformed line (missing fields):\n%s",
1170 				    errline);
1171 			*parse = '\0';
1172 		} else {
1173 			working->uid = (uid_t)-1;
1174 			working->gid = (gid_t)-1;
1175 		}
1176 
1177 		if (!sscanf(q, "%o", &working->permissions))
1178 			errx(1, "error in config file; bad permissions:\n%s",
1179 			    errline);
1180 
1181 		q = parse = missing_field(sob(++parse), errline);
1182 		parse = son(parse);
1183 		if (!*parse)
1184 			errx(1, "malformed line (missing fields):\n%s",
1185 			    errline);
1186 		*parse = '\0';
1187 		if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0)
1188 			errx(1, "error in config file; bad value for count of logs to save:\n%s",
1189 			    errline);
1190 
1191 		q = parse = missing_field(sob(++parse), errline);
1192 		parse = son(parse);
1193 		if (!*parse)
1194 			errx(1, "malformed line (missing fields):\n%s",
1195 			    errline);
1196 		*parse = '\0';
1197 		if (isdigitch(*q))
1198 			working->trsize = atoi(q);
1199 		else if (strcmp(q, "*") == 0)
1200 			working->trsize = -1;
1201 		else {
1202 			warnx("Invalid value of '%s' for 'size' in line:\n%s",
1203 			    q, errline);
1204 			working->trsize = -1;
1205 		}
1206 
1207 		working->flags = 0;
1208 		working->compress = COMPRESS_NONE;
1209 		q = parse = missing_field(sob(++parse), errline);
1210 		parse = son(parse);
1211 		eol = !*parse;
1212 		*parse = '\0';
1213 		{
1214 			char *ep;
1215 			u_long ul;
1216 
1217 			ul = strtoul(q, &ep, 10);
1218 			if (ep == q)
1219 				working->hours = 0;
1220 			else if (*ep == '*')
1221 				working->hours = -1;
1222 			else if (ul > INT_MAX)
1223 				errx(1, "interval is too large:\n%s", errline);
1224 			else
1225 				working->hours = ul;
1226 
1227 			if (*ep == '\0' || strcmp(ep, "*") == 0)
1228 				goto no_trimat;
1229 			if (*ep != '@' && *ep != '$')
1230 				errx(1, "malformed interval/at:\n%s", errline);
1231 
1232 			working->flags |= CE_TRIMAT;
1233 			working->trim_at = ptime_init(NULL);
1234 			ptm_opts = PTM_PARSE_ISO8601;
1235 			if (*ep == '$')
1236 				ptm_opts = PTM_PARSE_DWM;
1237 			ptm_opts |= PTM_PARSE_MATCHDOM;
1238 			res = ptime_relparse(working->trim_at, ptm_opts,
1239 			    ptimeget_secs(timenow), ep + 1);
1240 			if (res == -2)
1241 				errx(1, "nonexistent time for 'at' value:\n%s",
1242 				    errline);
1243 			else if (res < 0)
1244 				errx(1, "malformed 'at' value:\n%s", errline);
1245 		}
1246 no_trimat:
1247 
1248 		if (eol)
1249 			q = NULL;
1250 		else {
1251 			q = parse = sob(++parse);	/* Optional field */
1252 			parse = son(parse);
1253 			if (!*parse)
1254 				eol = 1;
1255 			*parse = '\0';
1256 		}
1257 
1258 		for (; q && *q && !isspacech(*q); q++) {
1259 			switch (tolowerch(*q)) {
1260 			case 'b':
1261 				working->flags |= CE_BINARY;
1262 				break;
1263 			case 'c':
1264 				/*
1265 				 * XXX - 	Ick! Ugly! Remove ASAP!
1266 				 * We want `c' and `C' for "create".  But we
1267 				 * will temporarily treat `c' as `g', because
1268 				 * FreeBSD releases <= 4.8 have a typo of
1269 				 * checking  ('G' || 'c')  for CE_GLOB.
1270 				 */
1271 				if (*q == 'c') {
1272 					warnx("Assuming 'g' for 'c' in flags for line:\n%s",
1273 					    errline);
1274 					warnx("The 'c' flag will eventually mean 'CREATE'");
1275 					working->flags |= CE_GLOB;
1276 					break;
1277 				}
1278 				working->flags |= CE_CREATE;
1279 				break;
1280 			case 'd':
1281 				working->flags |= CE_NODUMP;
1282 				break;
1283 			case 'g':
1284 				working->flags |= CE_GLOB;
1285 				break;
1286 			case 'j':
1287 				working->compress = COMPRESS_BZIP2;
1288 				break;
1289 			case 'n':
1290 				working->flags |= CE_NOSIGNAL;
1291 				break;
1292 			case 'u':
1293 				working->flags |= CE_SIGNALGROUP;
1294 				break;
1295 			case 'w':
1296 				/* Depreciated flag - keep for compatibility purposes */
1297 				break;
1298 			case 'x':
1299 				working->compress = COMPRESS_XZ;
1300 				break;
1301 			case 'z':
1302 				working->compress = COMPRESS_GZIP;
1303 				break;
1304 			case '-':
1305 				break;
1306 			case 'f':	/* Used by OpenBSD for "CE_FOLLOW" */
1307 			case 'm':	/* Used by OpenBSD for "CE_MONITOR" */
1308 			case 'p':	/* Used by NetBSD  for "CE_PLAIN0" */
1309 			default:
1310 				errx(1, "illegal flag in config file -- %c",
1311 				    *q);
1312 			}
1313 		}
1314 
1315 		if (eol)
1316 			q = NULL;
1317 		else {
1318 			q = parse = sob(++parse);	/* Optional field */
1319 			parse = son(parse);
1320 			if (!*parse)
1321 				eol = 1;
1322 			*parse = '\0';
1323 		}
1324 
1325 		working->pid_file = NULL;
1326 		if (q && *q) {
1327 			if (*q == '/')
1328 				working->pid_file = strdup(q);
1329 			else if (isdigit(*q))
1330 				goto got_sig;
1331 			else
1332 				errx(1,
1333 			"illegal pid file or signal number in config file:\n%s",
1334 				    errline);
1335 		}
1336 		if (eol)
1337 			q = NULL;
1338 		else {
1339 			q = parse = sob(++parse);	/* Optional field */
1340 			*(parse = son(parse)) = '\0';
1341 		}
1342 
1343 		working->sig = SIGHUP;
1344 		if (q && *q) {
1345 			if (isdigit(*q)) {
1346 		got_sig:
1347 				working->sig = atoi(q);
1348 			} else {
1349 		err_sig:
1350 				errx(1,
1351 				    "illegal signal number in config file:\n%s",
1352 				    errline);
1353 			}
1354 			if (working->sig < 1 || working->sig >= NSIG)
1355 				goto err_sig;
1356 		}
1357 
1358 		/*
1359 		 * Finish figuring out what pid-file to use (if any) in
1360 		 * later processing if this logfile needs to be rotated.
1361 		 */
1362 		if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) {
1363 			/*
1364 			 * This config-entry specified 'n' for nosignal,
1365 			 * see if it also specified an explicit pid_file.
1366 			 * This would be a pretty pointless combination.
1367 			 */
1368 			if (working->pid_file != NULL) {
1369 				warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s",
1370 				    working->pid_file, errline);
1371 				free(working->pid_file);
1372 				working->pid_file = NULL;
1373 			}
1374 		} else if (working->pid_file == NULL) {
1375 			/*
1376 			 * This entry did not specify the 'n' flag, which
1377 			 * means it should signal syslogd unless it had
1378 			 * specified some other pid-file (and obviously the
1379 			 * syslog pid-file will not be for a process-group).
1380 			 * Also, we should only try to notify syslog if we
1381 			 * are root.
1382 			 */
1383 			if (working->flags & CE_SIGNALGROUP) {
1384 				warnx("Ignoring flag 'U' in line:\n%s",
1385 				    errline);
1386 				working->flags &= ~CE_SIGNALGROUP;
1387 			}
1388 			if (needroot)
1389 				working->pid_file = strdup(path_syslogpid);
1390 		}
1391 
1392 		/*
1393 		 * Add this entry to the appropriate list of entries, unless
1394 		 * it was some kind of special entry (eg: <default>).
1395 		 */
1396 		if (special) {
1397 			;			/* Do not add to any list */
1398 		} else if (working->flags & CE_GLOB) {
1399 			STAILQ_INSERT_TAIL(glob_p, working, cf_nextp);
1400 		} else {
1401 			STAILQ_INSERT_TAIL(work_p, working, cf_nextp);
1402 		}
1403 	}
1404 	if (errline != NULL)
1405 		free(errline);
1406 }
1407 
1408 static char *
1409 missing_field(char *p, char *errline)
1410 {
1411 
1412 	if (!p || !*p)
1413 		errx(1, "missing field in config file:\n%s", errline);
1414 	return (p);
1415 }
1416 
1417 /*
1418  * In our sort we return it in the reverse of what qsort normally
1419  * would do, as we want the newest files first.  If we have two
1420  * entries with the same time we don't really care about order.
1421  *
1422  * Support function for qsort() in delete_oldest_timelog().
1423  */
1424 static int
1425 oldlog_entry_compare(const void *a, const void *b)
1426 {
1427 	const struct oldlog_entry *ola = a, *olb = b;
1428 
1429 	if (ola->t > olb->t)
1430 		return (-1);
1431 	else if (ola->t < olb->t)
1432 		return (1);
1433 	else
1434 		return (0);
1435 }
1436 
1437 /*
1438  * Delete the oldest logfiles, when using time based filenames.
1439  */
1440 static void
1441 delete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir)
1442 {
1443 	char *logfname, *s, *dir, errbuf[80];
1444 	int dirfd, i, logcnt, max_logcnt, valid;
1445 	struct oldlog_entry *oldlogs;
1446 	size_t logfname_len;
1447 	struct dirent *dp;
1448 	const char *cdir;
1449 	struct tm tm;
1450 	DIR *dirp;
1451 
1452 	oldlogs = malloc(MAX_OLDLOGS * sizeof(struct oldlog_entry));
1453 	max_logcnt = MAX_OLDLOGS;
1454 	logcnt = 0;
1455 
1456 	if (archive_dir != NULL && archive_dir[0] != '\0')
1457 		cdir = archive_dir;
1458 	else
1459 		if ((cdir = dirname(ent->log)) == NULL)
1460 			err(1, "dirname()");
1461 	if ((dir = strdup(cdir)) == NULL)
1462 		err(1, "strdup()");
1463 
1464 	if ((s = basename(ent->log)) == NULL)
1465 		err(1, "basename()");
1466 	if ((logfname = strdup(s)) == NULL)
1467 		err(1, "strdup()");
1468 	logfname_len = strlen(logfname);
1469 	if (strcmp(logfname, "/") == 0)
1470 		errx(1, "Invalid log filename - became '/'");
1471 
1472 	if (verbose > 2)
1473 		printf("Searching for old logs in %s\n", dir);
1474 
1475 	/* First we create a 'list' of all archived logfiles */
1476 	if ((dirp = opendir(dir)) == NULL)
1477 		err(1, "Cannot open log directory '%s'", dir);
1478 	dirfd = dirfd(dirp);
1479 	while ((dp = readdir(dirp)) != NULL) {
1480 		if (dp->d_type != DT_REG)
1481 			continue;
1482 
1483 		/* Ignore everything but files with our logfile prefix */
1484 		if (strncmp(dp->d_name, logfname, logfname_len) != 0)
1485 			continue;
1486 		/* Ignore the actual non-rotated logfile */
1487 		if (dp->d_namlen == logfname_len)
1488 			continue;
1489 		/*
1490 		 * Make sure we created have found a logfile, so the
1491 		 * postfix is valid, IE format is: '.<time>(.[bg]z)?'.
1492 		 */
1493 		if (dp->d_name[logfname_len] != '.') {
1494 			if (verbose)
1495 				printf("Ignoring %s which has unexpected "
1496 				    "extension '%s'\n", dp->d_name,
1497 				    &dp->d_name[logfname_len]);
1498 			continue;
1499 		}
1500 		if ((s = strptime(&dp->d_name[logfname_len + 1],
1501 			    timefnamefmt, &tm)) == NULL) {
1502 			/*
1503 			 * We could special case "old" sequentially
1504 			 * named logfiles here, but we do not as that
1505 			 * would require special handling to decide
1506 			 * which one was the oldest compared to "new"
1507 			 * time based logfiles.
1508 			 */
1509 			if (verbose)
1510 				printf("Ignoring %s which does not "
1511 				    "match time format\n", dp->d_name);
1512 			continue;
1513 		}
1514 
1515 		for (int c = 0; c < COMPRESS_TYPES; c++)
1516 			if (strcmp(s, compress_type[c].suffix) == 0)
1517 				valid = 1;
1518 		if (valid != 1) {
1519 			if (verbose)
1520 				printf("Ignoring %s which has unexpected "
1521 				    "extension '%s'\n", dp->d_name, s);
1522 			continue;
1523 		}
1524 
1525 		/*
1526 		 * We should now have old an old rotated logfile, so
1527 		 * add it to the 'list'.
1528 		 */
1529 		if ((oldlogs[logcnt].t = timegm(&tm)) == -1)
1530 			err(1, "Could not convert time string to time value");
1531 		if ((oldlogs[logcnt].fname = strdup(dp->d_name)) == NULL)
1532 			err(1, "strdup()");
1533 		logcnt++;
1534 
1535 		/*
1536 		 * It is very unlikely we ever run out of space in the
1537 		 * logfile array from the default size, but lets
1538 		 * handle it anyway...
1539 		 */
1540 		if (logcnt >= max_logcnt) {
1541 			max_logcnt *= 4;
1542 			/* Detect integer overflow */
1543 			if (max_logcnt < logcnt)
1544 				errx(1, "Too many old logfiles found");
1545 			oldlogs = realloc(oldlogs,
1546 			    max_logcnt * sizeof(struct oldlog_entry));
1547 			if (oldlogs == NULL)
1548 				err(1, "realloc()");
1549 		}
1550 	}
1551 
1552 	/* Second, if needed we delete oldest archived logfiles */
1553 	if (logcnt > 0 && logcnt >= ent->numlogs && ent->numlogs > 1) {
1554 		oldlogs = realloc(oldlogs, logcnt *
1555 		    sizeof(struct oldlog_entry));
1556 		if (oldlogs == NULL)
1557 			err(1, "realloc()");
1558 
1559 		/*
1560 		 * We now sort the logs in the order of newest to
1561 		 * oldest.  That way we can simply skip over the
1562 		 * number of records we want to keep.
1563 		 */
1564 		qsort(oldlogs, logcnt, sizeof(struct oldlog_entry),
1565 		    oldlog_entry_compare);
1566 		for (i = ent->numlogs - 1; i < logcnt; i++) {
1567 			if (noaction)
1568 				printf("\trm -f %s/%s\n", dir,
1569 				    oldlogs[i].fname);
1570 			else if (unlinkat(dirfd, oldlogs[i].fname, 0) != 0) {
1571 				snprintf(errbuf, sizeof(errbuf),
1572 				    "Could not delete old logfile '%s'",
1573 				    oldlogs[i].fname);
1574 				perror(errbuf);
1575 			}
1576 		}
1577 	} else if (verbose > 1)
1578 		printf("No old logs to delete for logfile %s\n", ent->log);
1579 
1580 	/* Third, cleanup */
1581 	closedir(dirp);
1582 	for (i = 0; i < logcnt; i++) {
1583 		assert(oldlogs[i].fname != NULL);
1584 		free(oldlogs[i].fname);
1585 	}
1586 	free(oldlogs);
1587 	free(logfname);
1588 	free(dir);
1589 }
1590 
1591 /*
1592  * Only add to the queue if the file hasn't already been added. This is
1593  * done to prevent circular include loops.
1594  */
1595 static void
1596 add_to_queue(const char *fname, struct ilist *inclist)
1597 {
1598 	struct include_entry *inc;
1599 
1600 	STAILQ_FOREACH(inc, inclist, inc_nextp) {
1601 		if (strcmp(fname, inc->file) == 0) {
1602 			warnx("duplicate include detected: %s", fname);
1603 			return;
1604 		}
1605 	}
1606 
1607 	inc = malloc(sizeof(struct include_entry));
1608 	if (inc == NULL)
1609 		err(1, "malloc of inc");
1610 	inc->file = strdup(fname);
1611 
1612 	if (verbose > 2)
1613 		printf("\t+ Adding %s to the processing queue.\n", fname);
1614 
1615 	STAILQ_INSERT_TAIL(inclist, inc, inc_nextp);
1616 }
1617 
1618 /*
1619  * Search for logfile and return its compression suffix (if supported)
1620  * The suffix detection is first-match in the order of compress_types
1621  *
1622  * Note: if logfile without suffix exists (uncompressed, COMPRESS_NONE)
1623  * a zero-length string is returned
1624  */
1625 static const char *
1626 get_logfile_suffix(const char *logfile)
1627 {
1628 	struct stat st;
1629 	char zfile[MAXPATHLEN];
1630 
1631 	for (int c = 0; c < COMPRESS_TYPES; c++) {
1632 		strlcpy(zfile, logfile, MAXPATHLEN);
1633 		strlcat(zfile, compress_type[c].suffix, MAXPATHLEN);
1634 		if (lstat(zfile, &st) == 0)
1635 			return (compress_type[c].suffix);
1636 	}
1637 	return (NULL);
1638 }
1639 
1640 static fk_entry
1641 do_rotate(const struct conf_entry *ent)
1642 {
1643 	char dirpart[MAXPATHLEN], namepart[MAXPATHLEN];
1644 	char file1[MAXPATHLEN], file2[MAXPATHLEN];
1645 	char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
1646 	const char *logfile_suffix;
1647 	char datetimestr[30];
1648 	int flags, numlogs_c;
1649 	fk_entry free_or_keep;
1650 	struct sigwork_entry *swork;
1651 	struct stat st;
1652 	struct tm tm;
1653 	time_t now;
1654 
1655 	flags = ent->flags;
1656 	free_or_keep = FREE_ENT;
1657 
1658 	if (archtodir) {
1659 		char *p;
1660 
1661 		/* build complete name of archive directory into dirpart */
1662 		if (*archdirname == '/') {	/* absolute */
1663 			strlcpy(dirpart, archdirname, sizeof(dirpart));
1664 		} else {	/* relative */
1665 			/* get directory part of logfile */
1666 			strlcpy(dirpart, ent->log, sizeof(dirpart));
1667 			if ((p = strrchr(dirpart, '/')) == NULL)
1668 				dirpart[0] = '\0';
1669 			else
1670 				*(p + 1) = '\0';
1671 			strlcat(dirpart, archdirname, sizeof(dirpart));
1672 		}
1673 
1674 		/* check if archive directory exists, if not, create it */
1675 		if (lstat(dirpart, &st))
1676 			createdir(ent, dirpart);
1677 
1678 		/* get filename part of logfile */
1679 		if ((p = strrchr(ent->log, '/')) == NULL)
1680 			strlcpy(namepart, ent->log, sizeof(namepart));
1681 		else
1682 			strlcpy(namepart, p + 1, sizeof(namepart));
1683 
1684 		/* name of oldest log */
1685 		snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart,
1686 		    namepart, ent->numlogs);
1687 	} else {
1688 		/*
1689 		 * Tell delete_oldest_timelog() we are not using an
1690 		 * archive dir.
1691 		 */
1692 		dirpart[0] = '\0';
1693 
1694 		/* name of oldest log */
1695 		snprintf(file1, sizeof(file1), "%s.%d", ent->log,
1696 		    ent->numlogs);
1697 	}
1698 
1699 	/* Delete old logs */
1700 	if (timefnamefmt != NULL)
1701 		delete_oldest_timelog(ent, dirpart);
1702 	else {
1703 		/* name of oldest log */
1704 		for (int c = 0; c < COMPRESS_TYPES; c++) {
1705 			snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
1706 			    compress_type[c].suffix);
1707 			if (noaction)
1708 				printf("\trm -f %s\n", zfile1);
1709 			else
1710 				unlink(zfile1);
1711 		}
1712 	}
1713 
1714 	if (timefnamefmt != NULL) {
1715 		/* If time functions fails we can't really do any sensible */
1716 		if (time(&now) == (time_t)-1 ||
1717 		    localtime_r(&now, &tm) == NULL)
1718 			bzero(&tm, sizeof(tm));
1719 
1720 		strftime(datetimestr, sizeof(datetimestr), timefnamefmt, &tm);
1721 		if (archtodir) {
1722 			snprintf(file1, sizeof(file1), "%s/%s.%s",
1723 			    dirpart, namepart, datetimestr);
1724 		} else {
1725 			snprintf(file1, sizeof(file1), "%s.%s",
1726 			    ent->log, datetimestr);
1727 		}
1728 
1729 		/* Don't run the code to move down logs */
1730 		numlogs_c = 0;
1731 	} else
1732 		numlogs_c = ent->numlogs;		/* copy for countdown */
1733 
1734 	/* Move down log files */
1735 	while (numlogs_c--) {
1736 
1737 		strlcpy(file2, file1, sizeof(file2));
1738 
1739 		if (archtodir)
1740 			snprintf(file1, sizeof(file1), "%s/%s.%d",
1741 			    dirpart, namepart, numlogs_c);
1742 		else
1743 			snprintf(file1, sizeof(file1), "%s.%d",
1744 			    ent->log, numlogs_c);
1745 
1746 		logfile_suffix = get_logfile_suffix(file1);
1747 		if (logfile_suffix == NULL)
1748 			continue;
1749 		strlcpy(zfile1, file1, MAXPATHLEN);
1750 		strlcpy(zfile2, file2, MAXPATHLEN);
1751 		strlcat(zfile1, logfile_suffix, MAXPATHLEN);
1752 		strlcat(zfile2, logfile_suffix, MAXPATHLEN);
1753 
1754 		if (noaction)
1755 			printf("\tmv %s %s\n", zfile1, zfile2);
1756 		else {
1757 			/* XXX - Ought to be checking for failure! */
1758 			rename(zfile1, zfile2);
1759 		}
1760 		change_attrs(zfile2, ent);
1761 	}
1762 
1763 	if (ent->numlogs > 0) {
1764 		if (noaction) {
1765 			/*
1766 			 * Note that savelog() may succeed with using link()
1767 			 * for the archtodir case, but there is no good way
1768 			 * of knowing if it will when doing "noaction", so
1769 			 * here we claim that it will have to do a copy...
1770 			 */
1771 			if (archtodir)
1772 				printf("\tcp %s %s\n", ent->log, file1);
1773 			else
1774 				printf("\tln %s %s\n", ent->log, file1);
1775 		} else {
1776 			if (!(flags & CE_BINARY)) {
1777 				/* Report the trimming to the old log */
1778 				log_trim(ent->log, ent);
1779 			}
1780 			savelog(ent->log, file1);
1781 		}
1782 		change_attrs(file1, ent);
1783 	}
1784 
1785 	/* Create the new log file and move it into place */
1786 	if (noaction)
1787 		printf("Start new log...\n");
1788 	createlog(ent);
1789 
1790 	/*
1791 	 * Save all signalling and file-compression to be done after log
1792 	 * files from all entries have been rotated.  This way any one
1793 	 * process will not be sent the same signal multiple times when
1794 	 * multiple log files had to be rotated.
1795 	 */
1796 	swork = NULL;
1797 	if (ent->pid_file != NULL)
1798 		swork = save_sigwork(ent);
1799 	if (ent->numlogs > 0 && ent->compress > COMPRESS_NONE) {
1800 		/*
1801 		 * The zipwork_entry will include a pointer to this
1802 		 * conf_entry, so the conf_entry should not be freed.
1803 		 */
1804 		free_or_keep = KEEP_ENT;
1805 		save_zipwork(ent, swork, ent->fsize, file1);
1806 	}
1807 
1808 	return (free_or_keep);
1809 }
1810 
1811 static void
1812 do_sigwork(struct sigwork_entry *swork)
1813 {
1814 	struct sigwork_entry *nextsig;
1815 	int kres, secs;
1816 
1817 	if (!(swork->sw_pidok) || swork->sw_pid == 0)
1818 		return;			/* no work to do... */
1819 
1820 	/*
1821 	 * If nosignal (-s) was specified, then do not signal any process.
1822 	 * Note that a nosignal request triggers a warning message if the
1823 	 * rotated logfile needs to be compressed, *unless* -R was also
1824 	 * specified.  We assume that an `-sR' request came from a process
1825 	 * which writes to the logfile, and as such, we assume that process
1826 	 * has already made sure the logfile is not presently in use.  This
1827 	 * just sets swork->sw_pidok to a special value, and do_zipwork
1828 	 * will print any necessary warning(s).
1829 	 */
1830 	if (nosignal) {
1831 		if (!rotatereq)
1832 			swork->sw_pidok = -1;
1833 		return;
1834 	}
1835 
1836 	/*
1837 	 * Compute the pause between consecutive signals.  Use a longer
1838 	 * sleep time if we will be sending two signals to the same
1839 	 * deamon or process-group.
1840 	 */
1841 	secs = 0;
1842 	nextsig = SLIST_NEXT(swork, sw_nextp);
1843 	if (nextsig != NULL) {
1844 		if (swork->sw_pid == nextsig->sw_pid)
1845 			secs = 10;
1846 		else
1847 			secs = 1;
1848 	}
1849 
1850 	if (noaction) {
1851 		printf("\tkill -%d %d \t\t# %s\n", swork->sw_signum,
1852 		    (int)swork->sw_pid, swork->sw_fname);
1853 		if (secs > 0)
1854 			printf("\tsleep %d\n", secs);
1855 		return;
1856 	}
1857 
1858 	kres = kill(swork->sw_pid, swork->sw_signum);
1859 	if (kres != 0) {
1860 		/*
1861 		 * Assume that "no such process" (ESRCH) is something
1862 		 * to warn about, but is not an error.  Presumably the
1863 		 * process which writes to the rotated log file(s) is
1864 		 * gone, in which case we should have no problem with
1865 		 * compressing the rotated log file(s).
1866 		 */
1867 		if (errno != ESRCH)
1868 			swork->sw_pidok = 0;
1869 		warn("can't notify %s, pid %d", swork->sw_pidtype,
1870 		    (int)swork->sw_pid);
1871 	} else {
1872 		if (verbose)
1873 			printf("Notified %s pid %d = %s\n", swork->sw_pidtype,
1874 			    (int)swork->sw_pid, swork->sw_fname);
1875 		if (secs > 0) {
1876 			if (verbose)
1877 				printf("Pause %d second(s) between signals\n",
1878 				    secs);
1879 			sleep(secs);
1880 		}
1881 	}
1882 }
1883 
1884 static void
1885 do_zipwork(struct zipwork_entry *zwork)
1886 {
1887 	const char *pgm_name, *pgm_path;
1888 	int errsav, fcount, zstatus;
1889 	pid_t pidzip, wpid;
1890 	char zresult[MAXPATHLEN];
1891 
1892 	pgm_path = NULL;
1893 	strlcpy(zresult, zwork->zw_fname, sizeof(zresult));
1894 	if (zwork != NULL && zwork->zw_conf != NULL &&
1895 	    zwork->zw_conf->compress > COMPRESS_NONE)
1896 		for (int c = 1; c < COMPRESS_TYPES; c++) {
1897 			if (zwork->zw_conf->compress == c) {
1898 				pgm_path = compress_type[c].path;
1899 				strlcat(zresult,
1900 				    compress_type[c].suffix, sizeof(zresult));
1901 				break;
1902 			}
1903 		}
1904 	if (pgm_path == NULL) {
1905 		warnx("invalid entry for %s in do_zipwork", zwork->zw_fname);
1906 		return;
1907 	}
1908 	pgm_name = strrchr(pgm_path, '/');
1909 	if (pgm_name == NULL)
1910 		pgm_name = pgm_path;
1911 	else
1912 		pgm_name++;
1913 
1914 	if (zwork->zw_swork != NULL && zwork->zw_swork->sw_pidok <= 0) {
1915 		warnx(
1916 		    "log %s not compressed because daemon(s) not notified",
1917 		    zwork->zw_fname);
1918 		change_attrs(zwork->zw_fname, zwork->zw_conf);
1919 		return;
1920 	}
1921 
1922 	if (noaction) {
1923 		printf("\t%s %s\n", pgm_name, zwork->zw_fname);
1924 		change_attrs(zresult, zwork->zw_conf);
1925 		return;
1926 	}
1927 
1928 	fcount = 1;
1929 	pidzip = fork();
1930 	while (pidzip < 0) {
1931 		/*
1932 		 * The fork failed.  If the failure was due to a temporary
1933 		 * problem, then wait a short time and try it again.
1934 		 */
1935 		errsav = errno;
1936 		warn("fork() for `%s %s'", pgm_name, zwork->zw_fname);
1937 		if (errsav != EAGAIN || fcount > 5)
1938 			errx(1, "Exiting...");
1939 		sleep(fcount * 12);
1940 		fcount++;
1941 		pidzip = fork();
1942 	}
1943 	if (!pidzip) {
1944 		/* The child process executes the compression command */
1945 		execl(pgm_path, pgm_path, "-f", zwork->zw_fname, NULL);
1946 		err(1, "execl(`%s -f %s')", pgm_path, zwork->zw_fname);
1947 	}
1948 
1949 	wpid = waitpid(pidzip, &zstatus, 0);
1950 	if (wpid == -1) {
1951 		/* XXX - should this be a fatal error? */
1952 		warn("%s: waitpid(%d)", pgm_path, pidzip);
1953 		return;
1954 	}
1955 	if (!WIFEXITED(zstatus)) {
1956 		warnx("`%s -f %s' did not terminate normally", pgm_name,
1957 		    zwork->zw_fname);
1958 		return;
1959 	}
1960 	if (WEXITSTATUS(zstatus)) {
1961 		warnx("`%s -f %s' terminated with a non-zero status (%d)",
1962 		    pgm_name, zwork->zw_fname, WEXITSTATUS(zstatus));
1963 		return;
1964 	}
1965 
1966 	/* Compression was successful, set file attributes on the result. */
1967 	change_attrs(zresult, zwork->zw_conf);
1968 }
1969 
1970 /*
1971  * Save information on any process we need to signal.  Any single
1972  * process may need to be sent different signal-values for different
1973  * log files, but usually a single signal-value will cause the process
1974  * to close and re-open all of it's log files.
1975  */
1976 static struct sigwork_entry *
1977 save_sigwork(const struct conf_entry *ent)
1978 {
1979 	struct sigwork_entry *sprev, *stmp;
1980 	int ndiff;
1981 	size_t tmpsiz;
1982 
1983 	sprev = NULL;
1984 	ndiff = 1;
1985 	SLIST_FOREACH(stmp, &swhead, sw_nextp) {
1986 		ndiff = strcmp(ent->pid_file, stmp->sw_fname);
1987 		if (ndiff > 0)
1988 			break;
1989 		if (ndiff == 0) {
1990 			if (ent->sig == stmp->sw_signum)
1991 				break;
1992 			if (ent->sig > stmp->sw_signum) {
1993 				ndiff = 1;
1994 				break;
1995 			}
1996 		}
1997 		sprev = stmp;
1998 	}
1999 	if (stmp != NULL && ndiff == 0)
2000 		return (stmp);
2001 
2002 	tmpsiz = sizeof(struct sigwork_entry) + strlen(ent->pid_file) + 1;
2003 	stmp = malloc(tmpsiz);
2004 	set_swpid(stmp, ent);
2005 	stmp->sw_signum = ent->sig;
2006 	strcpy(stmp->sw_fname, ent->pid_file);
2007 	if (sprev == NULL)
2008 		SLIST_INSERT_HEAD(&swhead, stmp, sw_nextp);
2009 	else
2010 		SLIST_INSERT_AFTER(sprev, stmp, sw_nextp);
2011 	return (stmp);
2012 }
2013 
2014 /*
2015  * Save information on any file we need to compress.  We may see the same
2016  * file multiple times, so check the full list to avoid duplicates.  The
2017  * list itself is sorted smallest-to-largest, because that's the order we
2018  * want to compress the files.  If the partition is very low on disk space,
2019  * then the smallest files are the most likely to compress, and compressing
2020  * them first will free up more space for the larger files.
2021  */
2022 static struct zipwork_entry *
2023 save_zipwork(const struct conf_entry *ent, const struct sigwork_entry *swork,
2024     int zsize, const char *zipfname)
2025 {
2026 	struct zipwork_entry *zprev, *ztmp;
2027 	int ndiff;
2028 	size_t tmpsiz;
2029 
2030 	/* Compute the size if the caller did not know it. */
2031 	if (zsize < 0)
2032 		zsize = sizefile(zipfname);
2033 
2034 	zprev = NULL;
2035 	ndiff = 1;
2036 	SLIST_FOREACH(ztmp, &zwhead, zw_nextp) {
2037 		ndiff = strcmp(zipfname, ztmp->zw_fname);
2038 		if (ndiff == 0)
2039 			break;
2040 		if (zsize > ztmp->zw_fsize)
2041 			zprev = ztmp;
2042 	}
2043 	if (ztmp != NULL && ndiff == 0)
2044 		return (ztmp);
2045 
2046 	tmpsiz = sizeof(struct zipwork_entry) + strlen(zipfname) + 1;
2047 	ztmp = malloc(tmpsiz);
2048 	ztmp->zw_conf = ent;
2049 	ztmp->zw_swork = swork;
2050 	ztmp->zw_fsize = zsize;
2051 	strcpy(ztmp->zw_fname, zipfname);
2052 	if (zprev == NULL)
2053 		SLIST_INSERT_HEAD(&zwhead, ztmp, zw_nextp);
2054 	else
2055 		SLIST_INSERT_AFTER(zprev, ztmp, zw_nextp);
2056 	return (ztmp);
2057 }
2058 
2059 /* Send a signal to the pid specified by pidfile */
2060 static void
2061 set_swpid(struct sigwork_entry *swork, const struct conf_entry *ent)
2062 {
2063 	FILE *f;
2064 	long minok, maxok, rval;
2065 	char *endp, *linep, line[BUFSIZ];
2066 
2067 	minok = MIN_PID;
2068 	maxok = MAX_PID;
2069 	swork->sw_pidok = 0;
2070 	swork->sw_pid = 0;
2071 	swork->sw_pidtype = "daemon";
2072 	if (ent->flags & CE_SIGNALGROUP) {
2073 		/*
2074 		 * If we are expected to signal a process-group when
2075 		 * rotating this logfile, then the value read in should
2076 		 * be the negative of a valid process ID.
2077 		 */
2078 		minok = -MAX_PID;
2079 		maxok = -MIN_PID;
2080 		swork->sw_pidtype = "process-group";
2081 	}
2082 
2083 	f = fopen(ent->pid_file, "r");
2084 	if (f == NULL) {
2085 		if (errno == ENOENT && enforcepid == 0) {
2086 			/*
2087 			 * Warn if the PID file doesn't exist, but do
2088 			 * not consider it an error.  Most likely it
2089 			 * means the process has been terminated,
2090 			 * so it should be safe to rotate any log
2091 			 * files that the process would have been using.
2092 			 */
2093 			swork->sw_pidok = 1;
2094 			warnx("pid file doesn't exist: %s", ent->pid_file);
2095 		} else
2096 			warn("can't open pid file: %s", ent->pid_file);
2097 		return;
2098 	}
2099 
2100 	if (fgets(line, BUFSIZ, f) == NULL) {
2101 		/*
2102 		 * Warn if the PID file is empty, but do not consider
2103 		 * it an error.  Most likely it means the process has
2104 		 * has terminated, so it should be safe to rotate any
2105 		 * log files that the process would have been using.
2106 		 */
2107 		if (feof(f) && enforcepid == 0) {
2108 			swork->sw_pidok = 1;
2109 			warnx("pid file is empty: %s", ent->pid_file);
2110 		} else
2111 			warn("can't read from pid file: %s", ent->pid_file);
2112 		fclose(f);
2113 		return;
2114 	}
2115 	fclose(f);
2116 
2117 	errno = 0;
2118 	linep = line;
2119 	while (*linep == ' ')
2120 		linep++;
2121 	rval = strtol(linep, &endp, 10);
2122 	if (*endp != '\0' && !isspacech(*endp)) {
2123 		warnx("pid file does not start with a valid number: %s",
2124 		    ent->pid_file);
2125 	} else if (rval < minok || rval > maxok) {
2126 		warnx("bad value '%ld' for process number in %s",
2127 		    rval, ent->pid_file);
2128 		if (verbose)
2129 			warnx("\t(expecting value between %ld and %ld)",
2130 			    minok, maxok);
2131 	} else {
2132 		swork->sw_pidok = 1;
2133 		swork->sw_pid = rval;
2134 	}
2135 
2136 	return;
2137 }
2138 
2139 /* Log the fact that the logs were turned over */
2140 static int
2141 log_trim(const char *logname, const struct conf_entry *log_ent)
2142 {
2143 	FILE *f;
2144 	const char *xtra;
2145 
2146 	if ((f = fopen(logname, "a")) == NULL)
2147 		return (-1);
2148 	xtra = "";
2149 	if (log_ent->def_cfg)
2150 		xtra = " using <default> rule";
2151 	if (log_ent->firstcreate)
2152 		fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n",
2153 		    daytime, hostname, (int) getpid(), xtra);
2154 	else if (log_ent->r_reason != NULL)
2155 		fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n",
2156 		    daytime, hostname, (int) getpid(), log_ent->r_reason, xtra);
2157 	else
2158 		fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n",
2159 		    daytime, hostname, (int) getpid(), xtra);
2160 	if (fclose(f) == EOF)
2161 		err(1, "log_trim: fclose");
2162 	return (0);
2163 }
2164 
2165 /* Return size in kilobytes of a file */
2166 static int
2167 sizefile(const char *file)
2168 {
2169 	struct stat sb;
2170 
2171 	if (stat(file, &sb) < 0)
2172 		return (-1);
2173 	return (kbytes(dbtob(sb.st_blocks)));
2174 }
2175 
2176 /* Return the age of old log file (file.0) */
2177 static int
2178 age_old_log(char *file)
2179 {
2180 	struct stat sb;
2181 	const char *logfile_suffix;
2182 	char tmp[MAXPATHLEN + sizeof(".0") + COMPRESS_SUFFIX_MAXLEN + 1];
2183 
2184 	if (archtodir) {
2185 		char *p;
2186 
2187 		/* build name of archive directory into tmp */
2188 		if (*archdirname == '/') {	/* absolute */
2189 			strlcpy(tmp, archdirname, sizeof(tmp));
2190 		} else {	/* relative */
2191 			/* get directory part of logfile */
2192 			strlcpy(tmp, file, sizeof(tmp));
2193 			if ((p = strrchr(tmp, '/')) == NULL)
2194 				tmp[0] = '\0';
2195 			else
2196 				*(p + 1) = '\0';
2197 			strlcat(tmp, archdirname, sizeof(tmp));
2198 		}
2199 
2200 		strlcat(tmp, "/", sizeof(tmp));
2201 
2202 		/* get filename part of logfile */
2203 		if ((p = strrchr(file, '/')) == NULL)
2204 			strlcat(tmp, file, sizeof(tmp));
2205 		else
2206 			strlcat(tmp, p + 1, sizeof(tmp));
2207 	} else {
2208 		strlcpy(tmp, file, sizeof(tmp));
2209 	}
2210 
2211 	strlcat(tmp, ".0", sizeof(tmp));
2212 	logfile_suffix = get_logfile_suffix(tmp);
2213 	if (logfile_suffix == NULL)
2214 		return (-1);
2215 	strlcat(tmp, logfile_suffix, sizeof(tmp));
2216 	if (stat(tmp, &sb) < 0)
2217 		return (-1);
2218 	return ((int)(ptimeget_secs(timenow) - sb.st_mtime + 1800) / 3600);
2219 }
2220 
2221 /* Skip Over Blanks */
2222 static char *
2223 sob(char *p)
2224 {
2225 	while (p && *p && isspace(*p))
2226 		p++;
2227 	return (p);
2228 }
2229 
2230 /* Skip Over Non-Blanks */
2231 static char *
2232 son(char *p)
2233 {
2234 	while (p && *p && !isspace(*p))
2235 		p++;
2236 	return (p);
2237 }
2238 
2239 /* Check if string is actually a number */
2240 static int
2241 isnumberstr(const char *string)
2242 {
2243 	while (*string) {
2244 		if (!isdigitch(*string++))
2245 			return (0);
2246 	}
2247 	return (1);
2248 }
2249 
2250 /* Check if string contains a glob */
2251 static int
2252 isglobstr(const char *string)
2253 {
2254 	char chr;
2255 
2256 	while ((chr = *string++)) {
2257 		if (chr == '*' || chr == '?' || chr == '[')
2258 			return (1);
2259 	}
2260 	return (0);
2261 }
2262 
2263 /*
2264  * Save the active log file under a new name.  A link to the new name
2265  * is the quick-and-easy way to do this.  If that fails (which it will
2266  * if the destination is on another partition), then make a copy of
2267  * the file to the new location.
2268  */
2269 static void
2270 savelog(char *from, char *to)
2271 {
2272 	FILE *src, *dst;
2273 	int c, res;
2274 
2275 	res = link(from, to);
2276 	if (res == 0)
2277 		return;
2278 
2279 	if ((src = fopen(from, "r")) == NULL)
2280 		err(1, "can't fopen %s for reading", from);
2281 	if ((dst = fopen(to, "w")) == NULL)
2282 		err(1, "can't fopen %s for writing", to);
2283 
2284 	while ((c = getc(src)) != EOF) {
2285 		if ((putc(c, dst)) == EOF)
2286 			err(1, "error writing to %s", to);
2287 	}
2288 
2289 	if (ferror(src))
2290 		err(1, "error reading from %s", from);
2291 	if ((fclose(src)) != 0)
2292 		err(1, "can't fclose %s", to);
2293 	if ((fclose(dst)) != 0)
2294 		err(1, "can't fclose %s", from);
2295 }
2296 
2297 /* create one or more directory components of a path */
2298 static void
2299 createdir(const struct conf_entry *ent, char *dirpart)
2300 {
2301 	int res;
2302 	char *s, *d;
2303 	char mkdirpath[MAXPATHLEN];
2304 	struct stat st;
2305 
2306 	s = dirpart;
2307 	d = mkdirpath;
2308 
2309 	for (;;) {
2310 		*d++ = *s++;
2311 		if (*s != '/' && *s != '\0')
2312 			continue;
2313 		*d = '\0';
2314 		res = lstat(mkdirpath, &st);
2315 		if (res != 0) {
2316 			if (noaction) {
2317 				printf("\tmkdir %s\n", mkdirpath);
2318 			} else {
2319 				res = mkdir(mkdirpath, 0755);
2320 				if (res != 0)
2321 					err(1, "Error on mkdir(\"%s\") for -a",
2322 					    mkdirpath);
2323 			}
2324 		}
2325 		if (*s == '\0')
2326 			break;
2327 	}
2328 	if (verbose) {
2329 		if (ent->firstcreate)
2330 			printf("Created directory '%s' for new %s\n",
2331 			    dirpart, ent->log);
2332 		else
2333 			printf("Created directory '%s' for -a\n", dirpart);
2334 	}
2335 }
2336 
2337 /*
2338  * Create a new log file, destroying any currently-existing version
2339  * of the log file in the process.  If the caller wants a backup copy
2340  * of the file to exist, they should call 'link(logfile,logbackup)'
2341  * before calling this routine.
2342  */
2343 void
2344 createlog(const struct conf_entry *ent)
2345 {
2346 	int fd, failed;
2347 	struct stat st;
2348 	char *realfile, *slash, tempfile[MAXPATHLEN];
2349 
2350 	fd = -1;
2351 	realfile = ent->log;
2352 
2353 	/*
2354 	 * If this log file is being created for the first time (-C option),
2355 	 * then it may also be true that the parent directory does not exist
2356 	 * yet.  Check, and create that directory if it is missing.
2357 	 */
2358 	if (ent->firstcreate) {
2359 		strlcpy(tempfile, realfile, sizeof(tempfile));
2360 		slash = strrchr(tempfile, '/');
2361 		if (slash != NULL) {
2362 			*slash = '\0';
2363 			failed = stat(tempfile, &st);
2364 			if (failed && errno != ENOENT)
2365 				err(1, "Error on stat(%s)", tempfile);
2366 			if (failed)
2367 				createdir(ent, tempfile);
2368 			else if (!S_ISDIR(st.st_mode))
2369 				errx(1, "%s exists but is not a directory",
2370 				    tempfile);
2371 		}
2372 	}
2373 
2374 	/*
2375 	 * First create an unused filename, so it can be chown'ed and
2376 	 * chmod'ed before it is moved into the real location.  mkstemp
2377 	 * will create the file mode=600 & owned by us.  Note that all
2378 	 * temp files will have a suffix of '.z<something>'.
2379 	 */
2380 	strlcpy(tempfile, realfile, sizeof(tempfile));
2381 	strlcat(tempfile, ".zXXXXXX", sizeof(tempfile));
2382 	if (noaction)
2383 		printf("\tmktemp %s\n", tempfile);
2384 	else {
2385 		fd = mkstemp(tempfile);
2386 		if (fd < 0)
2387 			err(1, "can't mkstemp logfile %s", tempfile);
2388 
2389 		/*
2390 		 * Add status message to what will become the new log file.
2391 		 */
2392 		if (!(ent->flags & CE_BINARY)) {
2393 			if (log_trim(tempfile, ent))
2394 				err(1, "can't add status message to log");
2395 		}
2396 	}
2397 
2398 	/* Change the owner/group, if we are supposed to */
2399 	if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) {
2400 		if (noaction)
2401 			printf("\tchown %u:%u %s\n", ent->uid, ent->gid,
2402 			    tempfile);
2403 		else {
2404 			failed = fchown(fd, ent->uid, ent->gid);
2405 			if (failed)
2406 				err(1, "can't fchown temp file %s", tempfile);
2407 		}
2408 	}
2409 
2410 	/* Turn on NODUMP if it was requested in the config-file. */
2411 	if (ent->flags & CE_NODUMP) {
2412 		if (noaction)
2413 			printf("\tchflags nodump %s\n", tempfile);
2414 		else {
2415 			failed = fchflags(fd, UF_NODUMP);
2416 			if (failed) {
2417 				warn("log_trim: fchflags(NODUMP)");
2418 			}
2419 		}
2420 	}
2421 
2422 	/*
2423 	 * Note that if the real logfile still exists, and if the call
2424 	 * to rename() fails, then "neither the old file nor the new
2425 	 * file shall be changed or created" (to quote the standard).
2426 	 * If the call succeeds, then the file will be replaced without
2427 	 * any window where some other process might find that the file
2428 	 * did not exist.
2429 	 * XXX - ? It may be that for some error conditions, we could
2430 	 *	retry by first removing the realfile and then renaming.
2431 	 */
2432 	if (noaction) {
2433 		printf("\tchmod %o %s\n", ent->permissions, tempfile);
2434 		printf("\tmv %s %s\n", tempfile, realfile);
2435 	} else {
2436 		failed = fchmod(fd, ent->permissions);
2437 		if (failed)
2438 			err(1, "can't fchmod temp file '%s'", tempfile);
2439 		failed = rename(tempfile, realfile);
2440 		if (failed)
2441 			err(1, "can't mv %s to %s", tempfile, realfile);
2442 	}
2443 
2444 	if (fd >= 0)
2445 		close(fd);
2446 }
2447 
2448 /*
2449  * Change the attributes of a given filename to what was specified in
2450  * the newsyslog.conf entry.  This routine is only called for files
2451  * that newsyslog expects that it has created, and thus it is a fatal
2452  * error if this routine finds that the file does not exist.
2453  */
2454 static void
2455 change_attrs(const char *fname, const struct conf_entry *ent)
2456 {
2457 	int failed;
2458 
2459 	if (noaction) {
2460 		printf("\tchmod %o %s\n", ent->permissions, fname);
2461 
2462 		if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
2463 			printf("\tchown %u:%u %s\n",
2464 			    ent->uid, ent->gid, fname);
2465 
2466 		if (ent->flags & CE_NODUMP)
2467 			printf("\tchflags nodump %s\n", fname);
2468 		return;
2469 	}
2470 
2471 	failed = chmod(fname, ent->permissions);
2472 	if (failed) {
2473 		if (errno != EPERM)
2474 			err(1, "chmod(%s) in change_attrs", fname);
2475 		warn("change_attrs couldn't chmod(%s)", fname);
2476 	}
2477 
2478 	if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) {
2479 		failed = chown(fname, ent->uid, ent->gid);
2480 		if (failed)
2481 			warn("can't chown %s", fname);
2482 	}
2483 
2484 	if (ent->flags & CE_NODUMP) {
2485 		failed = chflags(fname, UF_NODUMP);
2486 		if (failed)
2487 			warn("can't chflags %s NODUMP", fname);
2488 	}
2489 }
2490