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