xref: /titanic_52/usr/src/cmd/logadm/main.c (revision e9a193fce9f1bf8520f55929577e0175e1e7625b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50ade2cf0Sdg199075  * Common Development and Distribution License (the "License").
60ade2cf0Sdg199075  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*e9a193fcSJohn.Zolnowsky@Sun.COM  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  * logadm/main.c -- main routines for logadm
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * this program is 90% argument processing, 10% actions...
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <libintl.h>
347c478bd9Sstevel@tonic-gate #include <locale.h>
35fbe4944aSPetr Sumbera #include <fcntl.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <sys/wait.h>
397c478bd9Sstevel@tonic-gate #include <sys/filio.h>
407c478bd9Sstevel@tonic-gate #include <time.h>
41fbe4944aSPetr Sumbera #include <utime.h>
427c478bd9Sstevel@tonic-gate #include "err.h"
437c478bd9Sstevel@tonic-gate #include "lut.h"
447c478bd9Sstevel@tonic-gate #include "fn.h"
457c478bd9Sstevel@tonic-gate #include "opts.h"
467c478bd9Sstevel@tonic-gate #include "conf.h"
477c478bd9Sstevel@tonic-gate #include "glob.h"
487c478bd9Sstevel@tonic-gate #include "kw.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* forward declarations for functions in this file */
517c478bd9Sstevel@tonic-gate static void usage(const char *msg);
527c478bd9Sstevel@tonic-gate static void commajoin(const char *lhs, void *rhs, void *arg);
537c478bd9Sstevel@tonic-gate static void doaftercmd(const char *lhs, void *rhs, void *arg);
547c478bd9Sstevel@tonic-gate static void dologname(struct fn *fnp, struct opts *clopts);
557c478bd9Sstevel@tonic-gate static boolean_t rotatelog(struct fn *fnp, struct opts *opts);
567c478bd9Sstevel@tonic-gate static void rotateto(struct fn *fnp, struct opts *opts, int n,
577c478bd9Sstevel@tonic-gate     struct fn *recentlog, boolean_t isgz);
580ade2cf0Sdg199075 static void do_delayed_gzip(const char *lhs, void *rhs, void *arg);
597c478bd9Sstevel@tonic-gate static void expirefiles(struct fn *fnp, struct opts *opts);
607c478bd9Sstevel@tonic-gate static void dorm(struct opts *opts, const char *msg, struct fn *fnp);
617c478bd9Sstevel@tonic-gate static void docmd(struct opts *opts, const char *msg, const char *cmd,
627c478bd9Sstevel@tonic-gate     const char *arg1, const char *arg2, const char *arg3);
63fbe4944aSPetr Sumbera static void docopytruncate(struct opts *opts, const char *file,
64fbe4944aSPetr Sumbera     const char *file_copy);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* our configuration file, unless otherwise specified by -f */
677c478bd9Sstevel@tonic-gate static char *Default_conffile = "/etc/logadm.conf";
68*e9a193fcSJohn.Zolnowsky@Sun.COM /* our timestamps file, unless otherwise specified by -F */
69*e9a193fcSJohn.Zolnowsky@Sun.COM static char *Default_timestamps = "/var/logadm/timestamps";
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /* default pathnames to the commands we invoke */
727c478bd9Sstevel@tonic-gate static char *Sh = "/bin/sh";
737c478bd9Sstevel@tonic-gate static char *Mv = "/bin/mv";
747c478bd9Sstevel@tonic-gate static char *Rm = "/bin/rm";
757c478bd9Sstevel@tonic-gate static char *Touch = "/bin/touch";
767c478bd9Sstevel@tonic-gate static char *Chmod = "/bin/chmod";
777c478bd9Sstevel@tonic-gate static char *Chown = "/bin/chown";
787c478bd9Sstevel@tonic-gate static char *Gzip = "/bin/gzip";
797c478bd9Sstevel@tonic-gate static char *Mkdir = "/bin/mkdir";
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /* return from time(0), gathered early on to avoid slewed timestamps */
827c478bd9Sstevel@tonic-gate time_t Now;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /* list of before commands that have been executed */
857c478bd9Sstevel@tonic-gate static struct lut *Beforecmds;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /* list of after commands to execute before exiting */
887c478bd9Sstevel@tonic-gate static struct lut *Aftercmds;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* list of conffile entry names that are considered "done" */
917c478bd9Sstevel@tonic-gate static struct lut *Donenames;
927c478bd9Sstevel@tonic-gate 
930ade2cf0Sdg199075 /* A list of names of files to be gzipped */
940ade2cf0Sdg199075 static struct lut *Gzipnames = NULL;
950ade2cf0Sdg199075 
967c478bd9Sstevel@tonic-gate /*
97*e9a193fcSJohn.Zolnowsky@Sun.COM  * only the "FfhnVv" options are allowed in the first form of this command,
987c478bd9Sstevel@tonic-gate  * so this defines the list of options that are an error in they appear
997c478bd9Sstevel@tonic-gate  * in the first form.  In other words, it is not allowed to run logadm
1007c478bd9Sstevel@tonic-gate  * with any of these options unless at least one logname is also provided.
1017c478bd9Sstevel@tonic-gate  */
102636deb66Sgm149974 #define	OPTIONS_NOT_FIRST_FORM	"eNrwpPsabcglmoRtzACEST"
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /* text that we spew with the -h flag */
1057c478bd9Sstevel@tonic-gate #define	HELP1 \
1067c478bd9Sstevel@tonic-gate "Usage: logadm [options]\n"\
1077c478bd9Sstevel@tonic-gate "       (processes all entries in /etc/logadm.conf or conffile given by -f)\n"\
1087c478bd9Sstevel@tonic-gate "   or: logadm [options] logname...\n"\
1097c478bd9Sstevel@tonic-gate "       (processes the given lognames)\n"\
1107c478bd9Sstevel@tonic-gate "\n"\
1117c478bd9Sstevel@tonic-gate "General options:\n"\
1127c478bd9Sstevel@tonic-gate "        -e mailaddr     mail errors to given address\n"\
113*e9a193fcSJohn.Zolnowsky@Sun.COM "        -F timestamps   use timestamps instead of /var/logadm/timestamps\n"\
1147c478bd9Sstevel@tonic-gate "        -f conffile     use conffile instead of /etc/logadm.conf\n"\
1157c478bd9Sstevel@tonic-gate "        -h              display help\n"\
1167c478bd9Sstevel@tonic-gate "        -N              not an error if log file nonexistent\n"\
1177c478bd9Sstevel@tonic-gate "        -n              show actions, don't perform them\n"\
1187c478bd9Sstevel@tonic-gate "        -r              remove logname entry from conffile\n"\
1197c478bd9Sstevel@tonic-gate "        -V              ensure conffile entries exist, correct\n"\
1207c478bd9Sstevel@tonic-gate "        -v              print info about actions happening\n"\
1217c478bd9Sstevel@tonic-gate "        -w entryname    write entry to config file\n"\
1227c478bd9Sstevel@tonic-gate "\n"\
1237c478bd9Sstevel@tonic-gate "Options which control when a logfile is rotated:\n"\
1247c478bd9Sstevel@tonic-gate "(default is: -s1b -p1w if no -s or -p)\n"\
1257c478bd9Sstevel@tonic-gate "        -p period       only rotate if period passed since last rotate\n"\
1267c478bd9Sstevel@tonic-gate "        -P timestamp    used to store rotation date in conffile\n"\
1277c478bd9Sstevel@tonic-gate "        -s size         only rotate if given size or greater\n"\
1287c478bd9Sstevel@tonic-gate "\n"
1297c478bd9Sstevel@tonic-gate #define	HELP2 \
1307c478bd9Sstevel@tonic-gate "Options which control how a logfile is rotated:\n"\
1317c478bd9Sstevel@tonic-gate "(default is: -t '$file.$n', owner/group/mode taken from log file)\n"\
1327c478bd9Sstevel@tonic-gate "        -a cmd          execute cmd after taking actions\n"\
1337c478bd9Sstevel@tonic-gate "        -b cmd          execute cmd before taking actions\n"\
1347c478bd9Sstevel@tonic-gate "        -c              copy & truncate logfile, don't rename\n"\
1357c478bd9Sstevel@tonic-gate "        -g group        new empty log file group\n"\
136636deb66Sgm149974 "        -l              rotate log file with local time rather than UTC\n"\
1377c478bd9Sstevel@tonic-gate "        -m mode         new empty log file mode\n"\
1387c478bd9Sstevel@tonic-gate "        -M cmd          execute cmd to rotate the log file\n"\
1397c478bd9Sstevel@tonic-gate "        -o owner        new empty log file owner\n"\
1407c478bd9Sstevel@tonic-gate "        -R cmd          run cmd on file after rotate\n"\
1417c478bd9Sstevel@tonic-gate "        -t template     template for naming old logs\n"\
1427c478bd9Sstevel@tonic-gate "        -z count        gzip old logs except most recent count\n"\
1437c478bd9Sstevel@tonic-gate "\n"\
1447c478bd9Sstevel@tonic-gate "Options which control the expiration of old logfiles:\n"\
1457c478bd9Sstevel@tonic-gate "(default is: -C10 if no -A, -C, or -S)\n"\
1467c478bd9Sstevel@tonic-gate "        -A age          expire logs older than age\n"\
1477c478bd9Sstevel@tonic-gate "        -C count        expire old logs until count remain\n"\
1487c478bd9Sstevel@tonic-gate "        -E cmd          run cmd on file to expire\n"\
1497c478bd9Sstevel@tonic-gate "        -S size         expire until space used is below size \n"\
1507c478bd9Sstevel@tonic-gate "        -T pattern      pattern for finding old logs\n"
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * main -- where it all begins
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1567c478bd9Sstevel@tonic-gate int
1577c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	struct opts *clopts;		/* from parsing command line */
1607c478bd9Sstevel@tonic-gate 	const char *conffile;		/* our configuration file */
161*e9a193fcSJohn.Zolnowsky@Sun.COM 	const char *timestamps;		/* our timestamps file */
1627c478bd9Sstevel@tonic-gate 	struct fn_list *lognames;	/* list of lognames we're processing */
1637c478bd9Sstevel@tonic-gate 	struct fn *fnp;
1647c478bd9Sstevel@tonic-gate 	char *val;
165b493790cSbasabi 	char *buf;
166*e9a193fcSJohn.Zolnowsky@Sun.COM 	int status;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1717c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* only used if Makefiles don't define it */
1727c478bd9Sstevel@tonic-gate #endif
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1757c478bd9Sstevel@tonic-gate 
176*e9a193fcSJohn.Zolnowsky@Sun.COM 	/* we only print times into the timestamps file, so make them uniform */
1777c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_TIME, "C");
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/* give our name to error routines & skip it for arg parsing */
1807c478bd9Sstevel@tonic-gate 	err_init(*argv++);
1817c478bd9Sstevel@tonic-gate 	(void) setlinebuf(stdout);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (putenv("PATH=/bin"))
1847c478bd9Sstevel@tonic-gate 		err(EF_SYS, "putenv PATH");
1857c478bd9Sstevel@tonic-gate 	if (putenv("TZ=UTC"))
1867c478bd9Sstevel@tonic-gate 		err(EF_SYS, "putenv TZ");
1877c478bd9Sstevel@tonic-gate 	tzset();
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	(void) umask(0);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	Now = time(0);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	/* check for (undocumented) debugging environment variables */
1947c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_DEFAULT_CONFFILE"))
1957c478bd9Sstevel@tonic-gate 		Default_conffile = val;
196*e9a193fcSJohn.Zolnowsky@Sun.COM 	if (val = getenv("_LOGADM_DEFAULT_TIMESTAMPS"))
197*e9a193fcSJohn.Zolnowsky@Sun.COM 		Default_timestamps = val;
1987c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_DEBUG"))
1997c478bd9Sstevel@tonic-gate 		Debug = atoi(val);
2007c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_SH"))
2017c478bd9Sstevel@tonic-gate 		Sh = val;
2027c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_MV"))
2037c478bd9Sstevel@tonic-gate 		Mv = val;
2047c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_RM"))
2057c478bd9Sstevel@tonic-gate 		Rm = val;
2067c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_TOUCH"))
2077c478bd9Sstevel@tonic-gate 		Touch = val;
2087c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_CHMOD"))
2097c478bd9Sstevel@tonic-gate 		Chmod = val;
2107c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_CHOWN"))
2117c478bd9Sstevel@tonic-gate 		Chown = val;
2127c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_GZIP"))
2137c478bd9Sstevel@tonic-gate 		Gzip = val;
2147c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_MKDIR"))
2157c478bd9Sstevel@tonic-gate 		Mkdir = val;
2167c478bd9Sstevel@tonic-gate 
217*e9a193fcSJohn.Zolnowsky@Sun.COM 	opts_init(Opttable, Opttable_cnt);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/* parse command line arguments */
2207c478bd9Sstevel@tonic-gate 	if (SETJMP)
2217c478bd9Sstevel@tonic-gate 		usage("bailing out due to command line errors");
2227c478bd9Sstevel@tonic-gate 	else
223*e9a193fcSJohn.Zolnowsky@Sun.COM 		clopts = opts_parse(NULL, argv, OPTF_CLI);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if (Debug) {
2267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "command line opts:");
2277c478bd9Sstevel@tonic-gate 		opts_print(clopts, stderr, NULL);
2287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * There are many moods of logadm:
2337c478bd9Sstevel@tonic-gate 	 *
2347c478bd9Sstevel@tonic-gate 	 *	1. "-h" for help was given.  We spew a canned help
2357c478bd9Sstevel@tonic-gate 	 *	   message and exit, regardless of any other options given.
2367c478bd9Sstevel@tonic-gate 	 *
2377c478bd9Sstevel@tonic-gate 	 *	2. "-r" or "-w" asking us to write to the conffile.  Lots
2387c478bd9Sstevel@tonic-gate 	 *	   of argument checking, then we make the change to conffile
2397c478bd9Sstevel@tonic-gate 	 *	   and exit.  (-r processing actually happens in dologname().)
2407c478bd9Sstevel@tonic-gate 	 *
2417c478bd9Sstevel@tonic-gate 	 *	3. "-V" to search/verify the conffile was given.  We do
2427c478bd9Sstevel@tonic-gate 	 *	   the appropriate run through the conffile and exit.
2437c478bd9Sstevel@tonic-gate 	 *	   (-V processing actually happens in dologname().)
2447c478bd9Sstevel@tonic-gate 	 *
2457c478bd9Sstevel@tonic-gate 	 *	4. No lognames were given, so we're being asked to go through
2467c478bd9Sstevel@tonic-gate 	 *	   every entry in conffile.  We verify that only the options
2477c478bd9Sstevel@tonic-gate 	 *	   that make sense for this form of the command are present
2487c478bd9Sstevel@tonic-gate 	 *	   and fall into the main processing loop below.
2497c478bd9Sstevel@tonic-gate 	 *
2507c478bd9Sstevel@tonic-gate 	 *	5. lognames were given, so we fall into the main processing
2517c478bd9Sstevel@tonic-gate 	 *	   loop below to work our way through them.
2527c478bd9Sstevel@tonic-gate 	 *
2537c478bd9Sstevel@tonic-gate 	 * The last two cases are where the option processing gets more
2547c478bd9Sstevel@tonic-gate 	 * complex.  Each time around the main processing loop, we're
2557c478bd9Sstevel@tonic-gate 	 * in one of these cases:
2567c478bd9Sstevel@tonic-gate 	 *
2577c478bd9Sstevel@tonic-gate 	 *	A. No cmdargs were found (we're in case 4), the entry
2587c478bd9Sstevel@tonic-gate 	 *	   in conffile supplies no log file names, so the entry
2597c478bd9Sstevel@tonic-gate 	 *	   name itself is the logfile name (or names, if it globs
2607c478bd9Sstevel@tonic-gate 	 *	   to multiple file names).
2617c478bd9Sstevel@tonic-gate 	 *
2627c478bd9Sstevel@tonic-gate 	 *	B. No cmdargs were found (we're in case 4), the entry
2637c478bd9Sstevel@tonic-gate 	 *	   in conffile gives log file names that we then loop
2647c478bd9Sstevel@tonic-gate 	 *	   through and rotate/expire.  In this case, the entry
2657c478bd9Sstevel@tonic-gate 	 *	   name is specifically NOT one of the log file names.
2667c478bd9Sstevel@tonic-gate 	 *
2677c478bd9Sstevel@tonic-gate 	 *	C. We're going through the cmdargs (we're in case 5),
2687c478bd9Sstevel@tonic-gate 	 *	   the entry in conffile either doesn't exist or it exists
2697c478bd9Sstevel@tonic-gate 	 *	   but supplies no log file names, so the cmdarg itself
2707c478bd9Sstevel@tonic-gate 	 *	   is the log file name.
2717c478bd9Sstevel@tonic-gate 	 *
2727c478bd9Sstevel@tonic-gate 	 *	D. We're going through the cmdargs (we're in case 5),
2737c478bd9Sstevel@tonic-gate 	 *	   a matching entry in conffile supplies log file names
2747c478bd9Sstevel@tonic-gate 	 *	   that we then loop through and rotate/expire.  In this
2757c478bd9Sstevel@tonic-gate 	 *	   case the entry name is specifically NOT one of the log
2767c478bd9Sstevel@tonic-gate 	 *	   file names.
2777c478bd9Sstevel@tonic-gate 	 *
2787c478bd9Sstevel@tonic-gate 	 * As we're doing all this, any options given on the command line
2797c478bd9Sstevel@tonic-gate 	 * override any found in the conffile, and we apply the defaults
2807c478bd9Sstevel@tonic-gate 	 * for rotation conditions and expiration conditions, etc. at the
2817c478bd9Sstevel@tonic-gate 	 * last opportunity, when we're sure they haven't been overridden
2827c478bd9Sstevel@tonic-gate 	 * by an option somewhere along the way.
2837c478bd9Sstevel@tonic-gate 	 *
2847c478bd9Sstevel@tonic-gate 	 */
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	/* help option overrides anything else */
2877c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "h")) {
2887c478bd9Sstevel@tonic-gate 		(void) fputs(HELP1, stderr);
2897c478bd9Sstevel@tonic-gate 		(void) fputs(HELP2, stderr);
2907c478bd9Sstevel@tonic-gate 		err_done(0);
2917c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	/* detect illegal option combinations */
2957c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "rwV") > 1)
2967c478bd9Sstevel@tonic-gate 		usage("Only one of -r, -w, or -V may be used at a time.");
2977c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "cM") > 1)
2987c478bd9Sstevel@tonic-gate 		usage("Only one of -c or -M may be used at a time.");
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	/* arrange for error output to be mailed if clopts includes -e */
3017c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "e"))
3027c478bd9Sstevel@tonic-gate 		err_mailto(opts_optarg(clopts, "e"));
3037c478bd9Sstevel@tonic-gate 
304*e9a193fcSJohn.Zolnowsky@Sun.COM 	/* this implements the default conffile and timestamps */
3057c478bd9Sstevel@tonic-gate 	if ((conffile = opts_optarg(clopts, "f")) == NULL)
3067c478bd9Sstevel@tonic-gate 		conffile = Default_conffile;
307*e9a193fcSJohn.Zolnowsky@Sun.COM 	if ((timestamps = opts_optarg(clopts, "F")) == NULL)
308*e9a193fcSJohn.Zolnowsky@Sun.COM 		timestamps = Default_timestamps;
3097c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "v"))
3107c478bd9Sstevel@tonic-gate 		(void) out("# loading %s\n", conffile);
311*e9a193fcSJohn.Zolnowsky@Sun.COM 	status = conf_open(conffile, timestamps, clopts);
312*e9a193fcSJohn.Zolnowsky@Sun.COM 	if (!status && opts_count(clopts, "V"))
313*e9a193fcSJohn.Zolnowsky@Sun.COM 		err_done(0);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/* handle conffile write option */
3167c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "w")) {
3177c478bd9Sstevel@tonic-gate 		if (Debug)
3187c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3197c478bd9Sstevel@tonic-gate 			    "main: add/replace conffile entry: <%s>\n",
3207c478bd9Sstevel@tonic-gate 			    opts_optarg(clopts, "w"));
3217c478bd9Sstevel@tonic-gate 		conf_replace(opts_optarg(clopts, "w"), clopts);
3227c478bd9Sstevel@tonic-gate 		conf_close(clopts);
3237c478bd9Sstevel@tonic-gate 		err_done(0);
3247c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	/*
3287c478bd9Sstevel@tonic-gate 	 * lognames is either a list supplied on the command line,
3297c478bd9Sstevel@tonic-gate 	 * or every entry in the conffile if none were supplied.
3307c478bd9Sstevel@tonic-gate 	 */
3317c478bd9Sstevel@tonic-gate 	lognames = opts_cmdargs(clopts);
3327c478bd9Sstevel@tonic-gate 	if (fn_list_empty(lognames)) {
3337c478bd9Sstevel@tonic-gate 		/*
3347c478bd9Sstevel@tonic-gate 		 * being asked to do all entries in conffile
3357c478bd9Sstevel@tonic-gate 		 *
3367c478bd9Sstevel@tonic-gate 		 * check to see if any options were given that only
3377c478bd9Sstevel@tonic-gate 		 * make sense when lognames are given specifically
3387c478bd9Sstevel@tonic-gate 		 * on the command line.
3397c478bd9Sstevel@tonic-gate 		 */
3407c478bd9Sstevel@tonic-gate 		if (opts_count(clopts, OPTIONS_NOT_FIRST_FORM))
3417c478bd9Sstevel@tonic-gate 			usage("some options require logname argument");
3427c478bd9Sstevel@tonic-gate 		if (Debug)
3437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3447c478bd9Sstevel@tonic-gate 			    "main: run all entries in conffile\n");
3457c478bd9Sstevel@tonic-gate 		lognames = conf_entries();
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/* foreach logname... */
3497c478bd9Sstevel@tonic-gate 	fn_list_rewind(lognames);
3507c478bd9Sstevel@tonic-gate 	while ((fnp = fn_list_next(lognames)) != NULL) {
351b493790cSbasabi 		buf = fn_s(fnp);
352b493790cSbasabi 		if (buf != NULL && lut_lookup(Donenames, buf) != NULL) {
3537c478bd9Sstevel@tonic-gate 			if (Debug)
3547c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3557c478bd9Sstevel@tonic-gate 				    "main: logname already done: <%s>\n",
356b493790cSbasabi 				    buf);
3577c478bd9Sstevel@tonic-gate 			continue;
3587c478bd9Sstevel@tonic-gate 		}
359b493790cSbasabi 		if (buf != NULL && SETJMP)
3607c478bd9Sstevel@tonic-gate 			err(EF_FILE, "bailing out on logname \"%s\" "
361b493790cSbasabi 			    "due to errors", buf);
3627c478bd9Sstevel@tonic-gate 		else
3637c478bd9Sstevel@tonic-gate 			dologname(fnp, clopts);
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/* execute any after commands */
3677c478bd9Sstevel@tonic-gate 	lut_walk(Aftercmds, doaftercmd, clopts);
3687c478bd9Sstevel@tonic-gate 
3690ade2cf0Sdg199075 	/* execute any gzip commands */
3700ade2cf0Sdg199075 	lut_walk(Gzipnames, do_delayed_gzip, clopts);
3710ade2cf0Sdg199075 
3727c478bd9Sstevel@tonic-gate 	/* write out any conffile changes */
3737c478bd9Sstevel@tonic-gate 	conf_close(clopts);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	err_done(0);
3767c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3777c478bd9Sstevel@tonic-gate 	return (0);	/* for lint's little mind */
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /* spew a message, then a usage message, then exit */
3817c478bd9Sstevel@tonic-gate static void
3827c478bd9Sstevel@tonic-gate usage(const char *msg)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	if (msg)
3857c478bd9Sstevel@tonic-gate 		err(0, "%s\nUse \"logadm -h\" for help.", msg);
3867c478bd9Sstevel@tonic-gate 	else
3877c478bd9Sstevel@tonic-gate 		err(EF_RAW, "Use \"logadm -h\" for help.\n");
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate /* helper function used by doaftercmd() to join mail addrs with commas */
3917c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
3927c478bd9Sstevel@tonic-gate static void
3937c478bd9Sstevel@tonic-gate commajoin(const char *lhs, void *rhs, void *arg)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate 	struct fn *fnp = (struct fn *)arg;
396b493790cSbasabi 	char *buf;
3977c478bd9Sstevel@tonic-gate 
398b493790cSbasabi 	buf = fn_s(fnp);
399b493790cSbasabi 	if (buf != NULL && *buf)
4007c478bd9Sstevel@tonic-gate 		fn_putc(fnp, ',');
4017c478bd9Sstevel@tonic-gate 	fn_puts(fnp, lhs);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate /* helper function used by main() to run "after" commands */
4057c478bd9Sstevel@tonic-gate static void
4067c478bd9Sstevel@tonic-gate doaftercmd(const char *lhs, void *rhs, void *arg)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	struct opts *opts = (struct opts *)arg;
4097c478bd9Sstevel@tonic-gate 	struct lut *addrs = (struct lut *)rhs;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if (addrs) {
4127c478bd9Sstevel@tonic-gate 		struct fn *fnp = fn_new(NULL);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		/*
4157c478bd9Sstevel@tonic-gate 		 * addrs contains list of email addrs that should get
4167c478bd9Sstevel@tonic-gate 		 * the error output when this after command is executed.
4177c478bd9Sstevel@tonic-gate 		 */
4187c478bd9Sstevel@tonic-gate 		lut_walk(addrs, commajoin, fnp);
4197c478bd9Sstevel@tonic-gate 		err_mailto(fn_s(fnp));
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	docmd(opts, "-a cmd", Sh, "-c", lhs, NULL);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4250ade2cf0Sdg199075 /* perform delayed gzip */
4260ade2cf0Sdg199075 
4270ade2cf0Sdg199075 static void
4280ade2cf0Sdg199075 do_delayed_gzip(const char *lhs, void *rhs, void *arg)
4290ade2cf0Sdg199075 {
4300ade2cf0Sdg199075 	struct opts *opts = (struct opts *)arg;
4310ade2cf0Sdg199075 
4320ade2cf0Sdg199075 	if (rhs == NULL) {
4330ade2cf0Sdg199075 		if (Debug) {
4340ade2cf0Sdg199075 			(void) fprintf(stderr, "do_delayed_gzip: not gzipping "
4350ade2cf0Sdg199075 			    "expired file <%s>\n", lhs);
4360ade2cf0Sdg199075 		}
4370ade2cf0Sdg199075 		return;
4380ade2cf0Sdg199075 	}
4390ade2cf0Sdg199075 	docmd(opts, "compress old log (-z flag)", Gzip, "-f", lhs, NULL);
4400ade2cf0Sdg199075 }
4410ade2cf0Sdg199075 
4420ade2cf0Sdg199075 
4437c478bd9Sstevel@tonic-gate /* main logname processing */
4447c478bd9Sstevel@tonic-gate static void
4457c478bd9Sstevel@tonic-gate dologname(struct fn *fnp, struct opts *clopts)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	const char *logname = fn_s(fnp);
4487c478bd9Sstevel@tonic-gate 	struct opts *cfopts;
4497c478bd9Sstevel@tonic-gate 	struct opts *allopts;
4507c478bd9Sstevel@tonic-gate 	struct fn_list *logfiles;
4517c478bd9Sstevel@tonic-gate 	struct fn_list *globbedfiles;
4527c478bd9Sstevel@tonic-gate 	struct fn *nextfnp;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	/* look up options set by config file */
4557c478bd9Sstevel@tonic-gate 	cfopts = conf_opts(logname);
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "v"))
4587c478bd9Sstevel@tonic-gate 		(void) out("# processing logname: %s\n", logname);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if (Debug) {
461b493790cSbasabi 		if (logname != NULL)
462b493790cSbasabi 			(void) fprintf(stderr, "dologname: logname <%s>\n",
463b493790cSbasabi 			    logname);
4647c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "conffile opts:");
4657c478bd9Sstevel@tonic-gate 		opts_print(cfopts, stderr, NULL);
4667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	/* handle conffile lookup option */
4707c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "V")) {
4717c478bd9Sstevel@tonic-gate 		/* lookup an entry in conffile */
4727c478bd9Sstevel@tonic-gate 		if (Debug)
4737c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4747c478bd9Sstevel@tonic-gate 			    "dologname: lookup conffile entry\n");
4757c478bd9Sstevel@tonic-gate 		if (conf_lookup(logname)) {
4767c478bd9Sstevel@tonic-gate 			opts_printword(logname, stdout);
4777c478bd9Sstevel@tonic-gate 			opts_print(cfopts, stdout, NULL);
4787c478bd9Sstevel@tonic-gate 			(void) out("\n");
4797c478bd9Sstevel@tonic-gate 		} else
4807c478bd9Sstevel@tonic-gate 			err_exitcode(1);
4817c478bd9Sstevel@tonic-gate 		return;
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/* handle conffile removal option */
4857c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "r")) {
4867c478bd9Sstevel@tonic-gate 		if (Debug)
4877c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4887c478bd9Sstevel@tonic-gate 			    "dologname: remove conffile entry\n");
4897c478bd9Sstevel@tonic-gate 		if (conf_lookup(logname))
4907c478bd9Sstevel@tonic-gate 			conf_replace(logname, NULL);
4917c478bd9Sstevel@tonic-gate 		else
4927c478bd9Sstevel@tonic-gate 			err_exitcode(1);
4937c478bd9Sstevel@tonic-gate 		return;
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	/* generate combined options */
4977c478bd9Sstevel@tonic-gate 	allopts = opts_merge(cfopts, clopts);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	/* arrange for error output to be mailed if allopts includes -e */
5007c478bd9Sstevel@tonic-gate 	if (opts_count(allopts, "e"))
5017c478bd9Sstevel@tonic-gate 		err_mailto(opts_optarg(allopts, "e"));
5027c478bd9Sstevel@tonic-gate 	else
5037c478bd9Sstevel@tonic-gate 		err_mailto(NULL);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	/* this implements the default rotation rules */
5067c478bd9Sstevel@tonic-gate 	if (opts_count(allopts, "sp") == 0) {
5077c478bd9Sstevel@tonic-gate 		if (opts_count(clopts, "v"))
5087c478bd9Sstevel@tonic-gate 			(void) out(
5097c478bd9Sstevel@tonic-gate 			    "#     using default rotate rules: -s1b -p1w\n");
5107c478bd9Sstevel@tonic-gate 		(void) opts_set(allopts, "s", "1b");
5117c478bd9Sstevel@tonic-gate 		(void) opts_set(allopts, "p", "1w");
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	/* this implements the default expiration rules */
5157c478bd9Sstevel@tonic-gate 	if (opts_count(allopts, "ACS") == 0) {
5167c478bd9Sstevel@tonic-gate 		if (opts_count(clopts, "v"))
5177c478bd9Sstevel@tonic-gate 			(void) out("#     using default expire rule: -C10\n");
5187c478bd9Sstevel@tonic-gate 		(void) opts_set(allopts, "C", "10");
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/* this implements the default template */
5227c478bd9Sstevel@tonic-gate 	if (opts_count(allopts, "t") == 0) {
5237c478bd9Sstevel@tonic-gate 		if (opts_count(clopts, "v"))
5247c478bd9Sstevel@tonic-gate 			(void) out("#     using default template: $file.$n\n");
5257c478bd9Sstevel@tonic-gate 		(void) opts_set(allopts, "t", "$file.$n");
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	if (Debug) {
5297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "merged opts:");
5307c478bd9Sstevel@tonic-gate 		opts_print(allopts, stderr, NULL);
5317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	/*
5357c478bd9Sstevel@tonic-gate 	 * if the conffile entry supplied log file names, then
5367c478bd9Sstevel@tonic-gate 	 * logname is NOT one of the log file names (it was just
5377c478bd9Sstevel@tonic-gate 	 * the entry name in conffile).
5387c478bd9Sstevel@tonic-gate 	 */
5397c478bd9Sstevel@tonic-gate 	logfiles = opts_cmdargs(cfopts);
5407c478bd9Sstevel@tonic-gate 	if (Debug) {
541b493790cSbasabi 		char *buf;
5427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dologname: logfiles from cfopts:\n");
5437c478bd9Sstevel@tonic-gate 		fn_list_rewind(logfiles);
5447c478bd9Sstevel@tonic-gate 		while ((nextfnp = fn_list_next(logfiles)) != NULL)
545b493790cSbasabi 			buf = fn_s(nextfnp);
546b493790cSbasabi 			if (buf != NULL)
547b493790cSbasabi 				(void) fprintf(stderr, "    <%s>\n", buf);
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 	if (fn_list_empty(logfiles))
5507c478bd9Sstevel@tonic-gate 		globbedfiles = glob_glob(fnp);
5517c478bd9Sstevel@tonic-gate 	else
5527c478bd9Sstevel@tonic-gate 		globbedfiles = glob_glob_list(logfiles);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/* go through the list produced by glob expansion */
5557c478bd9Sstevel@tonic-gate 	fn_list_rewind(globbedfiles);
5567c478bd9Sstevel@tonic-gate 	while ((nextfnp = fn_list_next(globbedfiles)) != NULL)
5577c478bd9Sstevel@tonic-gate 		if (rotatelog(nextfnp, allopts))
5587c478bd9Sstevel@tonic-gate 			expirefiles(nextfnp, allopts);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	fn_list_free(globbedfiles);
5617c478bd9Sstevel@tonic-gate 	opts_free(allopts);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /* absurdly long buffer lengths for holding user/group/mode strings */
5667c478bd9Sstevel@tonic-gate #define	TIMESTRMAX	100
5677c478bd9Sstevel@tonic-gate #define	MAXATTR		100
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate /* rotate a log file if necessary, returns true if ok to go on to expire step */
5707c478bd9Sstevel@tonic-gate static boolean_t
5717c478bd9Sstevel@tonic-gate rotatelog(struct fn *fnp, struct opts *opts)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	char *fname = fn_s(fnp);
5747c478bd9Sstevel@tonic-gate 	struct stat stbuf;
5757c478bd9Sstevel@tonic-gate 	char nowstr[TIMESTRMAX];
5767c478bd9Sstevel@tonic-gate 	struct fn *recentlog = fn_new(NULL);	/* for -R cmd */
5777c478bd9Sstevel@tonic-gate 	char ownerbuf[MAXATTR];
5787c478bd9Sstevel@tonic-gate 	char groupbuf[MAXATTR];
5797c478bd9Sstevel@tonic-gate 	char modebuf[MAXATTR];
5807c478bd9Sstevel@tonic-gate 	const char *owner;
5817c478bd9Sstevel@tonic-gate 	const char *group;
5827c478bd9Sstevel@tonic-gate 	const char *mode;
5837c478bd9Sstevel@tonic-gate 
584b493790cSbasabi 	if (Debug && fname != NULL)
5857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "rotatelog: fname <%s>\n", fname);
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "p") && opts_optarg_int(opts, "p") == OPTP_NEVER)
5887c478bd9Sstevel@tonic-gate 		return (B_TRUE);	/* "-p never" forced no rotate */
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	/* prepare the keywords */
5917c478bd9Sstevel@tonic-gate 	kw_init(fnp, NULL);
5927c478bd9Sstevel@tonic-gate 	if (Debug > 1) {
5937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "rotatelog keywords:\n");
5947c478bd9Sstevel@tonic-gate 		kw_print(stderr);
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	if (lstat(fname, &stbuf) < 0) {
5987c478bd9Sstevel@tonic-gate 		if (opts_count(opts, "N"))
5997c478bd9Sstevel@tonic-gate 			return (1);
6007c478bd9Sstevel@tonic-gate 		err(EF_WARN|EF_SYS, "%s", fname);
6017c478bd9Sstevel@tonic-gate 		return (B_FALSE);
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	if ((stbuf.st_mode & S_IFMT) == S_IFLNK) {
6057c478bd9Sstevel@tonic-gate 		err(EF_WARN, "%s is a symlink", fname);
6067c478bd9Sstevel@tonic-gate 		return (B_FALSE);
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if ((stbuf.st_mode & S_IFMT) != S_IFREG) {
6107c478bd9Sstevel@tonic-gate 		err(EF_WARN, "%s is not a regular file", fname);
6117c478bd9Sstevel@tonic-gate 		return (B_FALSE);
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
6144e93ab4aSmh138676 	/* even if size condition is not met, this entry is "done" */
6154e93ab4aSmh138676 	if (opts_count(opts, "s") &&
6164e93ab4aSmh138676 	    stbuf.st_size < opts_optarg_int(opts, "s")) {
6174e93ab4aSmh138676 		Donenames = lut_add(Donenames, fname, "1");
6187c478bd9Sstevel@tonic-gate 		return (B_TRUE);
6194e93ab4aSmh138676 	}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	/* see if age condition is present, and return if not met */
6227c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "p")) {
623404720a7Sbasabi 		off_t when = opts_optarg_int(opts, "p");
6247c478bd9Sstevel@tonic-gate 		struct opts *cfopts;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 		/* unless rotate forced by "-p now", see if period has passed */
6277c478bd9Sstevel@tonic-gate 		if (when != OPTP_NOW) {
6287c478bd9Sstevel@tonic-gate 			/*
6297c478bd9Sstevel@tonic-gate 			 * "when" holds the number of seconds that must have
6307c478bd9Sstevel@tonic-gate 			 * passed since the last time this log was rotated.
6317c478bd9Sstevel@tonic-gate 			 * of course, running logadm can take a little time
6327c478bd9Sstevel@tonic-gate 			 * (typically a second or two, but longer if the
6337c478bd9Sstevel@tonic-gate 			 * conffile has lots of stuff in it) and that amount
6347c478bd9Sstevel@tonic-gate 			 * of time is variable, depending on system load, etc.
6357c478bd9Sstevel@tonic-gate 			 * so we want to allow a little "slop" in the value of
6367c478bd9Sstevel@tonic-gate 			 * "when".  this way, if a log should be rotated every
6377c478bd9Sstevel@tonic-gate 			 * week, and the number of seconds passed is really a
6387c478bd9Sstevel@tonic-gate 			 * few seconds short of a week, we'll go ahead and
6397c478bd9Sstevel@tonic-gate 			 * rotate the log as expected.
6407c478bd9Sstevel@tonic-gate 			 *
6417c478bd9Sstevel@tonic-gate 			 */
6427c478bd9Sstevel@tonic-gate 			if (when >= 60 * 60)
6437c478bd9Sstevel@tonic-gate 				when -= 59;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 			/*
6467c478bd9Sstevel@tonic-gate 			 * last rotation is recorded as argument to -P,
6477c478bd9Sstevel@tonic-gate 			 * but if logname isn't the same as log file name
6487c478bd9Sstevel@tonic-gate 			 * then the timestamp would be recorded on a
6497c478bd9Sstevel@tonic-gate 			 * separate line in the conf file.  so if we
6507c478bd9Sstevel@tonic-gate 			 * haven't seen a -P already, we check to see if
6517c478bd9Sstevel@tonic-gate 			 * it is part of a specific entry for the log
6527c478bd9Sstevel@tonic-gate 			 * file name.  this handles the case where the
6537c478bd9Sstevel@tonic-gate 			 * logname is "apache", it supplies a log file
6547c478bd9Sstevel@tonic-gate 			 * name like "/var/apache/logs/[a-z]*_log",
6557c478bd9Sstevel@tonic-gate 			 * which expands to multiple file names.  if one
6567c478bd9Sstevel@tonic-gate 			 * of the file names is "/var/apache/logs/access_log"
6577c478bd9Sstevel@tonic-gate 			 * the the -P will be attached to a line with that
6587c478bd9Sstevel@tonic-gate 			 * logname in the conf file.
6597c478bd9Sstevel@tonic-gate 			 */
6607c478bd9Sstevel@tonic-gate 			if (opts_count(opts, "P")) {
661404720a7Sbasabi 				off_t last = opts_optarg_int(opts, "P");
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 				/* return if not enough time has passed */
6647c478bd9Sstevel@tonic-gate 				if (Now - last < when)
6657c478bd9Sstevel@tonic-gate 					return (B_TRUE);
6667c478bd9Sstevel@tonic-gate 			} else if ((cfopts = conf_opts(fname)) != NULL &&
6677c478bd9Sstevel@tonic-gate 			    opts_count(cfopts, "P")) {
668404720a7Sbasabi 				off_t last = opts_optarg_int(cfopts, "P");
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 				/*
6717c478bd9Sstevel@tonic-gate 				 * just checking this means this entry
6727c478bd9Sstevel@tonic-gate 				 * is now "done" if we're going through
6737c478bd9Sstevel@tonic-gate 				 * the entire conffile
6747c478bd9Sstevel@tonic-gate 				 */
6757c478bd9Sstevel@tonic-gate 				Donenames = lut_add(Donenames, fname, "1");
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 				/* return if not enough time has passed */
6787c478bd9Sstevel@tonic-gate 				if (Now - last < when)
6797c478bd9Sstevel@tonic-gate 					return (B_TRUE);
6807c478bd9Sstevel@tonic-gate 			}
6817c478bd9Sstevel@tonic-gate 		}
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (Debug)
6857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "rotatelog: conditions met\n");
686636deb66Sgm149974 	if (opts_count(opts, "l")) {
687636deb66Sgm149974 		/* Change the time zone to local time zone */
688636deb66Sgm149974 		if (putenv("TZ="))
689636deb66Sgm149974 			err(EF_SYS, "putenv TZ");
690636deb66Sgm149974 		tzset();
691636deb66Sgm149974 		Now = time(0);
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		/* rename the log file */
6947c478bd9Sstevel@tonic-gate 		rotateto(fnp, opts, 0, recentlog, B_FALSE);
6957c478bd9Sstevel@tonic-gate 
696636deb66Sgm149974 		/* Change the time zone to UTC */
697636deb66Sgm149974 		if (putenv("TZ=UTC"))
698636deb66Sgm149974 			err(EF_SYS, "putenv TZ");
699636deb66Sgm149974 		tzset();
700636deb66Sgm149974 		Now = time(0);
701636deb66Sgm149974 	} else {
702636deb66Sgm149974 		/* rename the log file */
703636deb66Sgm149974 		rotateto(fnp, opts, 0, recentlog, B_FALSE);
704636deb66Sgm149974 	}
705636deb66Sgm149974 
7067c478bd9Sstevel@tonic-gate 	/* determine owner, group, mode for empty log file */
7077c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "o"))
7087c478bd9Sstevel@tonic-gate 		(void) strlcpy(ownerbuf, opts_optarg(opts, "o"), MAXATTR);
7097c478bd9Sstevel@tonic-gate 	else {
7107c478bd9Sstevel@tonic-gate 		(void) snprintf(ownerbuf, MAXATTR, "%ld", stbuf.st_uid);
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 	owner = ownerbuf;
7137c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "g"))
7147c478bd9Sstevel@tonic-gate 		group = opts_optarg(opts, "g");
7157c478bd9Sstevel@tonic-gate 	else {
7167c478bd9Sstevel@tonic-gate 		(void) snprintf(groupbuf, MAXATTR, "%ld", stbuf.st_gid);
7177c478bd9Sstevel@tonic-gate 		group = groupbuf;
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 	(void) strlcat(ownerbuf, ":", MAXATTR - strlen(ownerbuf));
7207c478bd9Sstevel@tonic-gate 	(void) strlcat(ownerbuf, group, MAXATTR - strlen(ownerbuf));
7217c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "m"))
7227c478bd9Sstevel@tonic-gate 		mode = opts_optarg(opts, "m");
7237c478bd9Sstevel@tonic-gate 	else {
7247c478bd9Sstevel@tonic-gate 		(void) snprintf(modebuf, MAXATTR,
7257c478bd9Sstevel@tonic-gate 		    "%03lo", stbuf.st_mode & 0777);
7267c478bd9Sstevel@tonic-gate 		mode = modebuf;
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	/* create the empty log file */
7307c478bd9Sstevel@tonic-gate 	docmd(opts, NULL, Touch, fname, NULL, NULL);
7317c478bd9Sstevel@tonic-gate 	docmd(opts, NULL, Chown, owner, fname, NULL);
7327c478bd9Sstevel@tonic-gate 	docmd(opts, NULL, Chmod, mode, fname, NULL);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	/* execute post-rotation command */
7357c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "R")) {
7367c478bd9Sstevel@tonic-gate 		struct fn *rawcmd = fn_new(opts_optarg(opts, "R"));
7377c478bd9Sstevel@tonic-gate 		struct fn *cmd = fn_new(NULL);
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		kw_init(recentlog, NULL);
7407c478bd9Sstevel@tonic-gate 		(void) kw_expand(rawcmd, cmd, 0, B_FALSE);
7417c478bd9Sstevel@tonic-gate 		docmd(opts, "-R cmd", Sh, "-c", fn_s(cmd), NULL);
7427c478bd9Sstevel@tonic-gate 		fn_free(rawcmd);
7437c478bd9Sstevel@tonic-gate 		fn_free(cmd);
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 	fn_free(recentlog);
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	/*
7487c478bd9Sstevel@tonic-gate 	 * add "after" command to list of after commands.  we also record
7497c478bd9Sstevel@tonic-gate 	 * the email address, if any, where the error output of the after
7507c478bd9Sstevel@tonic-gate 	 * command should be sent.  if the after command is already on
7517c478bd9Sstevel@tonic-gate 	 * our list, add the email addr to the list the email addrs for
7527c478bd9Sstevel@tonic-gate 	 * that command (the after command will only be executed once,
7537c478bd9Sstevel@tonic-gate 	 * so the error output gets mailed to every address we've come
7547c478bd9Sstevel@tonic-gate 	 * across associated with this command).
7557c478bd9Sstevel@tonic-gate 	 */
7567c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "a")) {
7577c478bd9Sstevel@tonic-gate 		const char *cmd = opts_optarg(opts, "a");
7587c478bd9Sstevel@tonic-gate 		struct lut *addrs = (struct lut *)lut_lookup(Aftercmds, cmd);
7597c478bd9Sstevel@tonic-gate 		if (opts_count(opts, "e"))
7607c478bd9Sstevel@tonic-gate 			addrs = lut_add(addrs, opts_optarg(opts, "e"), NULL);
7617c478bd9Sstevel@tonic-gate 		Aftercmds = lut_add(Aftercmds, opts_optarg(opts, "a"), addrs);
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	/* record the rotation date */
7657c478bd9Sstevel@tonic-gate 	(void) strftime(nowstr, sizeof (nowstr),
7667c478bd9Sstevel@tonic-gate 	    "%a %b %e %T %Y", gmtime(&Now));
767b493790cSbasabi 	if (opts_count(opts, "v") && fname != NULL)
7687c478bd9Sstevel@tonic-gate 		(void) out("#     recording rotation date %s for %s\n",
7697c478bd9Sstevel@tonic-gate 		    nowstr, fname);
7707c478bd9Sstevel@tonic-gate 	conf_set(fname, "P", STRDUP(nowstr));
7717c478bd9Sstevel@tonic-gate 	Donenames = lut_add(Donenames, fname, "1");
7727c478bd9Sstevel@tonic-gate 	return (B_TRUE);
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate /* rotate files "up" according to current template */
7767c478bd9Sstevel@tonic-gate static void
7777c478bd9Sstevel@tonic-gate rotateto(struct fn *fnp, struct opts *opts, int n, struct fn *recentlog,
7787c478bd9Sstevel@tonic-gate     boolean_t isgz)
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate 	struct fn *template = fn_new(opts_optarg(opts, "t"));
7817c478bd9Sstevel@tonic-gate 	struct fn *newfile = fn_new(NULL);
7827c478bd9Sstevel@tonic-gate 	struct fn *dirname;
7837c478bd9Sstevel@tonic-gate 	int hasn;
7847c478bd9Sstevel@tonic-gate 	struct stat stbuf;
785b493790cSbasabi 	char *buf1;
786b493790cSbasabi 	char *buf2;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	/* expand template to figure out new filename */
7897c478bd9Sstevel@tonic-gate 	hasn = kw_expand(template, newfile, n, isgz);
7907c478bd9Sstevel@tonic-gate 
791b493790cSbasabi 	buf1 = fn_s(fnp);
792b493790cSbasabi 	buf2 = fn_s(newfile);
7937c478bd9Sstevel@tonic-gate 
794b493790cSbasabi 	if (Debug)
795b493790cSbasabi 		if (buf1 != NULL && buf2 != NULL) {
796b493790cSbasabi 			(void) fprintf(stderr, "rotateto: %s -> %s (%d)\n",
797b493790cSbasabi 			    buf1, buf2, n);
798b493790cSbasabi 		}
7997c478bd9Sstevel@tonic-gate 	/* if filename is there already, rotate "up" */
800b493790cSbasabi 	if (hasn && lstat(buf2, &stbuf) != -1)
8017c478bd9Sstevel@tonic-gate 		rotateto(newfile, opts, n + 1, recentlog, isgz);
8027c478bd9Sstevel@tonic-gate 	else if (hasn && opts_count(opts, "z")) {
8037c478bd9Sstevel@tonic-gate 		struct fn *gzfnp = fn_dup(newfile);
8047c478bd9Sstevel@tonic-gate 		/*
8057c478bd9Sstevel@tonic-gate 		 * since we're compressing old files, see if we
8067c478bd9Sstevel@tonic-gate 		 * about to rotate into one.
8077c478bd9Sstevel@tonic-gate 		 */
8087c478bd9Sstevel@tonic-gate 		fn_puts(gzfnp, ".gz");
8097c478bd9Sstevel@tonic-gate 		if (lstat(fn_s(gzfnp), &stbuf) != -1)
8107c478bd9Sstevel@tonic-gate 			rotateto(gzfnp, opts, n + 1, recentlog, B_TRUE);
8117c478bd9Sstevel@tonic-gate 		fn_free(gzfnp);
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/* first time through run "before" cmd if not run already */
8157c478bd9Sstevel@tonic-gate 	if (n == 0 && opts_count(opts, "b")) {
8167c478bd9Sstevel@tonic-gate 		const char *cmd = opts_optarg(opts, "b");
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 		if (lut_lookup(Beforecmds, cmd) == NULL) {
8197c478bd9Sstevel@tonic-gate 			docmd(opts, "-b cmd", Sh, "-c", cmd, NULL);
8207c478bd9Sstevel@tonic-gate 			Beforecmds = lut_add(Beforecmds, cmd, "1");
8217c478bd9Sstevel@tonic-gate 		}
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	/* ensure destination directory exists */
8257c478bd9Sstevel@tonic-gate 	dirname = fn_dirname(newfile);
8267c478bd9Sstevel@tonic-gate 	docmd(opts, "verify directory exists", Mkdir, "-p",
8277c478bd9Sstevel@tonic-gate 	    fn_s(dirname), NULL);
8287c478bd9Sstevel@tonic-gate 	fn_free(dirname);
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	/* do the rename */
831fbe4944aSPetr Sumbera 	if (opts_count(opts, "c") != NULL) {
832fbe4944aSPetr Sumbera 		docopytruncate(opts, fn_s(fnp), fn_s(newfile));
8337c478bd9Sstevel@tonic-gate 	} else if (n == 0 && opts_count(opts, "M")) {
8347c478bd9Sstevel@tonic-gate 		struct fn *rawcmd = fn_new(opts_optarg(opts, "M"));
8357c478bd9Sstevel@tonic-gate 		struct fn *cmd = fn_new(NULL);
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 		/* use specified command to mv the log file */
8387c478bd9Sstevel@tonic-gate 		kw_init(fnp, newfile);
8397c478bd9Sstevel@tonic-gate 		(void) kw_expand(rawcmd, cmd, 0, B_FALSE);
8407c478bd9Sstevel@tonic-gate 		docmd(opts, "-M cmd", Sh, "-c", fn_s(cmd), NULL);
8417c478bd9Sstevel@tonic-gate 		fn_free(rawcmd);
8427c478bd9Sstevel@tonic-gate 		fn_free(cmd);
8437c478bd9Sstevel@tonic-gate 	} else
8447c478bd9Sstevel@tonic-gate 		/* common case: we call "mv" to handle the actual rename */
8457c478bd9Sstevel@tonic-gate 		docmd(opts, "rotate log file", Mv, "-f",
8467c478bd9Sstevel@tonic-gate 		    fn_s(fnp), fn_s(newfile));
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	/* first time through, gather interesting info for caller */
8497c478bd9Sstevel@tonic-gate 	if (n == 0)
8507c478bd9Sstevel@tonic-gate 		fn_renew(recentlog, fn_s(newfile));
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate /* expire phase of logname processing */
8547c478bd9Sstevel@tonic-gate static void
8557c478bd9Sstevel@tonic-gate expirefiles(struct fn *fnp, struct opts *opts)
8567c478bd9Sstevel@tonic-gate {
8577c478bd9Sstevel@tonic-gate 	char *fname = fn_s(fnp);
8587c478bd9Sstevel@tonic-gate 	struct fn *template;
8597c478bd9Sstevel@tonic-gate 	struct fn *pattern;
8607c478bd9Sstevel@tonic-gate 	struct fn_list *files;
8617c478bd9Sstevel@tonic-gate 	struct fn *nextfnp;
862404720a7Sbasabi 	off_t count;
863404720a7Sbasabi 	off_t size;
8647c478bd9Sstevel@tonic-gate 
865b493790cSbasabi 	if (Debug && fname != NULL)
8667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "expirefiles: fname <%s>\n", fname);
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	/* return if no potential expire conditions */
869bf8770b4Snakanon 	if (opts_count(opts, "zAS") == 0 && opts_optarg_int(opts, "C") == 0)
8707c478bd9Sstevel@tonic-gate 		return;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	kw_init(fnp, NULL);
8737c478bd9Sstevel@tonic-gate 	if (Debug > 1) {
8747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "expirefiles keywords:\n");
8757c478bd9Sstevel@tonic-gate 		kw_print(stderr);
8767c478bd9Sstevel@tonic-gate 	}
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	/* see if pattern was supplied by user */
8797c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "T")) {
8807c478bd9Sstevel@tonic-gate 		template = fn_new(opts_optarg(opts, "T"));
8817c478bd9Sstevel@tonic-gate 		pattern = glob_to_reglob(template);
8827c478bd9Sstevel@tonic-gate 	} else {
8837c478bd9Sstevel@tonic-gate 		/* nope, generate pattern based on rotation template */
8847c478bd9Sstevel@tonic-gate 		template = fn_new(opts_optarg(opts, "t"));
8857c478bd9Sstevel@tonic-gate 		pattern = fn_new(NULL);
8867c478bd9Sstevel@tonic-gate 		(void) kw_expand(template, pattern, -1,
8877c478bd9Sstevel@tonic-gate 		    opts_count(opts, "z") != 0);
8887c478bd9Sstevel@tonic-gate 	}
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	/* match all old log files (hopefully not any others as well!) */
8917c478bd9Sstevel@tonic-gate 	files = glob_reglob(pattern);
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	if (Debug) {
894b493790cSbasabi 		char *buf;
895b493790cSbasabi 
896b493790cSbasabi 		buf = fn_s(pattern);
897b493790cSbasabi 		if (buf != NULL) {
8987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "expirefiles: pattern <%s>\n",
899b493790cSbasabi 			    buf);
900b493790cSbasabi 		}
9017c478bd9Sstevel@tonic-gate 		fn_list_rewind(files);
9027c478bd9Sstevel@tonic-gate 		while ((nextfnp = fn_list_next(files)) != NULL)
903b493790cSbasabi 			buf = fn_s(nextfnp);
904b493790cSbasabi 			if (buf != NULL)
905b493790cSbasabi 				(void) fprintf(stderr, "    <%s>\n", buf);
9067c478bd9Sstevel@tonic-gate 	}
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/* see if count causes expiration */
9097c478bd9Sstevel@tonic-gate 	if ((count = opts_optarg_int(opts, "C")) > 0) {
9107c478bd9Sstevel@tonic-gate 		int needexpire = fn_list_count(files) - count;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 		if (Debug)
9137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "expirefiles: needexpire %d\n",
9147c478bd9Sstevel@tonic-gate 			    needexpire);
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 		while (needexpire > 0 &&
9177c478bd9Sstevel@tonic-gate 		    ((nextfnp = fn_list_popoldest(files)) != NULL)) {
9187c478bd9Sstevel@tonic-gate 			dorm(opts, "expire by count rule", nextfnp);
9197c478bd9Sstevel@tonic-gate 			fn_free(nextfnp);
9207c478bd9Sstevel@tonic-gate 			needexpire--;
9217c478bd9Sstevel@tonic-gate 		}
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	/* see if total size causes expiration */
9257c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "S") && (size = opts_optarg_int(opts, "S")) > 0) {
9267c478bd9Sstevel@tonic-gate 		while (fn_list_totalsize(files) > size &&
9277c478bd9Sstevel@tonic-gate 		    ((nextfnp = fn_list_popoldest(files)) != NULL)) {
9287c478bd9Sstevel@tonic-gate 			dorm(opts, "expire by size rule", nextfnp);
9297c478bd9Sstevel@tonic-gate 			fn_free(nextfnp);
9307c478bd9Sstevel@tonic-gate 		}
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	/* see if age causes expiration */
9347c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "A")) {
935404720a7Sbasabi 		int mtime = (int)time(0) - (int)opts_optarg_int(opts, "A");
9367c478bd9Sstevel@tonic-gate 
937bf8770b4Snakanon 		while ((nextfnp = fn_list_popoldest(files)) != NULL) {
9387c478bd9Sstevel@tonic-gate 			if (fn_getstat(nextfnp)->st_mtime < mtime) {
9397c478bd9Sstevel@tonic-gate 				dorm(opts, "expire by age rule", nextfnp);
9407c478bd9Sstevel@tonic-gate 				fn_free(nextfnp);
9417c478bd9Sstevel@tonic-gate 			} else {
942b0b46606Snakanon 				fn_list_addfn(files, nextfnp);
9437c478bd9Sstevel@tonic-gate 				break;
9447c478bd9Sstevel@tonic-gate 			}
9457c478bd9Sstevel@tonic-gate 		}
946bf8770b4Snakanon 	}
947bf8770b4Snakanon 
948bf8770b4Snakanon 	/* record old log files to be gzip'ed according to -z count */
949bf8770b4Snakanon 	if (opts_count(opts, "z")) {
950bf8770b4Snakanon 		int zcount = (int)opts_optarg_int(opts, "z");
951bf8770b4Snakanon 		int fcount = fn_list_count(files);
952bf8770b4Snakanon 
953bf8770b4Snakanon 		while (fcount > zcount &&
954bf8770b4Snakanon 		    (nextfnp = fn_list_popoldest(files)) != NULL) {
955bf8770b4Snakanon 			if (!fn_isgz(nextfnp)) {
956bf8770b4Snakanon 				/*
957bf8770b4Snakanon 				 * Don't gzip the old log file yet -
958bf8770b4Snakanon 				 * it takes too long. Just remember that we
959bf8770b4Snakanon 				 * need to gzip.
960bf8770b4Snakanon 				 */
961bf8770b4Snakanon 				if (Debug) {
962bf8770b4Snakanon 					(void) fprintf(stderr,
963bf8770b4Snakanon 					    "will compress %s count %d\n",
964bf8770b4Snakanon 					    fn_s(nextfnp), fcount);
965bf8770b4Snakanon 				}
966bf8770b4Snakanon 				Gzipnames = lut_add(Gzipnames,
967bf8770b4Snakanon 				    fn_s(nextfnp), "1");
968bf8770b4Snakanon 			}
969bf8770b4Snakanon 			fn_free(nextfnp);
970bf8770b4Snakanon 			fcount--;
971bf8770b4Snakanon 		}
972bf8770b4Snakanon 	}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	fn_free(template);
9757c478bd9Sstevel@tonic-gate 	fn_list_free(files);
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate /* execute a command to remove an expired log file */
9797c478bd9Sstevel@tonic-gate static void
9807c478bd9Sstevel@tonic-gate dorm(struct opts *opts, const char *msg, struct fn *fnp)
9817c478bd9Sstevel@tonic-gate {
9827c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "E")) {
9837c478bd9Sstevel@tonic-gate 		struct fn *rawcmd = fn_new(opts_optarg(opts, "E"));
9847c478bd9Sstevel@tonic-gate 		struct fn *cmd = fn_new(NULL);
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 		/* user supplied cmd, expand $file */
9877c478bd9Sstevel@tonic-gate 		kw_init(fnp, NULL);
9887c478bd9Sstevel@tonic-gate 		(void) kw_expand(rawcmd, cmd, 0, B_FALSE);
9897c478bd9Sstevel@tonic-gate 		docmd(opts, msg, Sh, "-c", fn_s(cmd), NULL);
9907c478bd9Sstevel@tonic-gate 		fn_free(rawcmd);
9917c478bd9Sstevel@tonic-gate 		fn_free(cmd);
9927c478bd9Sstevel@tonic-gate 	} else
9937c478bd9Sstevel@tonic-gate 		docmd(opts, msg, Rm, "-f", fn_s(fnp), NULL);
9940ade2cf0Sdg199075 	Gzipnames = lut_add(Gzipnames, fn_s(fnp), NULL);
9957c478bd9Sstevel@tonic-gate }
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate /* execute a command, producing -n and -v output as necessary */
9987c478bd9Sstevel@tonic-gate static void
9997c478bd9Sstevel@tonic-gate docmd(struct opts *opts, const char *msg, const char *cmd,
10007c478bd9Sstevel@tonic-gate     const char *arg1, const char *arg2, const char *arg3)
10017c478bd9Sstevel@tonic-gate {
10027c478bd9Sstevel@tonic-gate 	int pid;
10037c478bd9Sstevel@tonic-gate 	int errpipe[2];
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	/* print info about command if necessary */
10067c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "vn")) {
10077c478bd9Sstevel@tonic-gate 		const char *simplecmd;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 		if ((simplecmd = strrchr(cmd, '/')) == NULL)
10107c478bd9Sstevel@tonic-gate 			simplecmd = cmd;
10117c478bd9Sstevel@tonic-gate 		else
10127c478bd9Sstevel@tonic-gate 			simplecmd++;
10137c478bd9Sstevel@tonic-gate 		(void) out("%s", simplecmd);
10147c478bd9Sstevel@tonic-gate 		if (arg1)
10157c478bd9Sstevel@tonic-gate 			(void) out(" %s", arg1);
10167c478bd9Sstevel@tonic-gate 		if (arg2)
10177c478bd9Sstevel@tonic-gate 			(void) out(" %s", arg2);
10187c478bd9Sstevel@tonic-gate 		if (arg3)
10197c478bd9Sstevel@tonic-gate 			(void) out(" %s", arg3);
10207c478bd9Sstevel@tonic-gate 		if (msg)
10217c478bd9Sstevel@tonic-gate 			(void) out(" # %s", msg);
10227c478bd9Sstevel@tonic-gate 		(void) out("\n");
10237c478bd9Sstevel@tonic-gate 	}
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "n"))
10267c478bd9Sstevel@tonic-gate 		return;		/* -n means don't really do it */
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	/*
10297c478bd9Sstevel@tonic-gate 	 * run the cmd and see if it failed.  this function is *not* a
10307c478bd9Sstevel@tonic-gate 	 * generic command runner -- we depend on some knowledge we
10317c478bd9Sstevel@tonic-gate 	 * have about the commands we run.  first of all, we expect
10327c478bd9Sstevel@tonic-gate 	 * errors to spew something to stderr, and that something is
10337c478bd9Sstevel@tonic-gate 	 * typically short enough to fit into a pipe so we can wait()
10347c478bd9Sstevel@tonic-gate 	 * for the command to complete and then fetch the error text
10357c478bd9Sstevel@tonic-gate 	 * from the pipe.  we also expect the exit codes to make sense.
10367c478bd9Sstevel@tonic-gate 	 * notice also that we only allow a command name which is an
10377c478bd9Sstevel@tonic-gate 	 * absolute pathname, and two args must be supplied (the
10387c478bd9Sstevel@tonic-gate 	 * second may be NULL, or they may both be NULL).
10397c478bd9Sstevel@tonic-gate 	 */
10407c478bd9Sstevel@tonic-gate 	if (pipe(errpipe) < 0)
10417c478bd9Sstevel@tonic-gate 		err(EF_SYS, "pipe");
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	if ((pid = fork()) < 0)
10447c478bd9Sstevel@tonic-gate 		err(EF_SYS, "fork");
10457c478bd9Sstevel@tonic-gate 	else if (pid) {
10467c478bd9Sstevel@tonic-gate 		int wstat;
10477c478bd9Sstevel@tonic-gate 		int count;
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 		/* parent */
10507c478bd9Sstevel@tonic-gate 		(void) close(errpipe[1]);
10517c478bd9Sstevel@tonic-gate 		if (waitpid(pid, &wstat, 0) < 0)
10527c478bd9Sstevel@tonic-gate 			err(EF_SYS, "waitpid");
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 		/* check for stderr output */
10557c478bd9Sstevel@tonic-gate 		if (ioctl(errpipe[0], FIONREAD, &count) >= 0 && count) {
10567c478bd9Sstevel@tonic-gate 			err(EF_WARN, "command failed: %s%s%s%s%s%s%s",
10577c478bd9Sstevel@tonic-gate 			    cmd,
10587c478bd9Sstevel@tonic-gate 			    (arg1) ? " " : "",
10597c478bd9Sstevel@tonic-gate 			    (arg1) ? arg1 : "",
10607c478bd9Sstevel@tonic-gate 			    (arg2) ? " " : "",
10617c478bd9Sstevel@tonic-gate 			    (arg2) ? arg2 : "",
10627c478bd9Sstevel@tonic-gate 			    (arg3) ? " " : "",
10637c478bd9Sstevel@tonic-gate 			    (arg3) ? arg3 : "");
10647c478bd9Sstevel@tonic-gate 			err_fromfd(errpipe[0]);
10657c478bd9Sstevel@tonic-gate 		} else if (WIFSIGNALED(wstat))
10667c478bd9Sstevel@tonic-gate 			err(EF_WARN,
10677c478bd9Sstevel@tonic-gate 			    "command died, signal %d: %s%s%s%s%s%s%s",
10687c478bd9Sstevel@tonic-gate 			    WTERMSIG(wstat),
10697c478bd9Sstevel@tonic-gate 			    cmd,
10707c478bd9Sstevel@tonic-gate 			    (arg1) ? " " : "",
10717c478bd9Sstevel@tonic-gate 			    (arg1) ? arg1 : "",
10727c478bd9Sstevel@tonic-gate 			    (arg2) ? " " : "",
10737c478bd9Sstevel@tonic-gate 			    (arg2) ? arg2 : "",
10747c478bd9Sstevel@tonic-gate 			    (arg3) ? " " : "",
10757c478bd9Sstevel@tonic-gate 			    (arg3) ? arg3 : "");
10767c478bd9Sstevel@tonic-gate 		else if (WIFEXITED(wstat) && WEXITSTATUS(wstat))
10777c478bd9Sstevel@tonic-gate 			err(EF_WARN,
10787c478bd9Sstevel@tonic-gate 			    "command error, exit %d: %s%s%s%s%s%s%s",
10797c478bd9Sstevel@tonic-gate 			    WEXITSTATUS(wstat),
10807c478bd9Sstevel@tonic-gate 			    cmd,
10817c478bd9Sstevel@tonic-gate 			    (arg1) ? " " : "",
10827c478bd9Sstevel@tonic-gate 			    (arg1) ? arg1 : "",
10837c478bd9Sstevel@tonic-gate 			    (arg2) ? " " : "",
10847c478bd9Sstevel@tonic-gate 			    (arg2) ? arg2 : "",
10857c478bd9Sstevel@tonic-gate 			    (arg3) ? " " : "",
10867c478bd9Sstevel@tonic-gate 			    (arg3) ? arg3 : "");
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 		(void) close(errpipe[0]);
10897c478bd9Sstevel@tonic-gate 	} else {
10907c478bd9Sstevel@tonic-gate 		/* child */
10917c478bd9Sstevel@tonic-gate 		(void) dup2(errpipe[1], fileno(stderr));
10927c478bd9Sstevel@tonic-gate 		(void) close(errpipe[0]);
10937c478bd9Sstevel@tonic-gate 		(void) execl(cmd, cmd, arg1, arg2, arg3, 0);
10947c478bd9Sstevel@tonic-gate 		perror(cmd);
10957c478bd9Sstevel@tonic-gate 		_exit(1);
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate }
1098fbe4944aSPetr Sumbera 
1099fbe4944aSPetr Sumbera /* do internal atomic file copy and truncation */
1100fbe4944aSPetr Sumbera static void
1101fbe4944aSPetr Sumbera docopytruncate(struct opts *opts, const char *file, const char *file_copy)
1102fbe4944aSPetr Sumbera {
1103fbe4944aSPetr Sumbera 	int fi, fo, len;
1104fbe4944aSPetr Sumbera 	char buf[4096];
1105fbe4944aSPetr Sumbera 	struct stat s;
1106fbe4944aSPetr Sumbera 	struct utimbuf times;
1107fbe4944aSPetr Sumbera 
1108fbe4944aSPetr Sumbera 	/* print info if necessary */
1109fbe4944aSPetr Sumbera 	if (opts_count(opts, "vn") != NULL) {
1110fbe4944aSPetr Sumbera 		(void) out("# log rotation via atomic copy and truncation"
1111fbe4944aSPetr Sumbera 		    " (-c flag):\n");
1112fbe4944aSPetr Sumbera 		(void) out("# copy %s to %s\n", file, file_copy);
1113fbe4944aSPetr Sumbera 		(void) out("# truncate %s\n", file);
1114fbe4944aSPetr Sumbera 	}
1115fbe4944aSPetr Sumbera 
1116fbe4944aSPetr Sumbera 	if (opts_count(opts, "n"))
1117fbe4944aSPetr Sumbera 		return;		/* -n means don't really do it */
1118fbe4944aSPetr Sumbera 
1119fbe4944aSPetr Sumbera 	/* open log file to be rotated and remember its chmod mask */
1120fbe4944aSPetr Sumbera 	if ((fi = open(file, O_RDWR)) < 0) {
1121fbe4944aSPetr Sumbera 		err(EF_SYS, "cannot open file %s", file);
1122fbe4944aSPetr Sumbera 		return;
1123fbe4944aSPetr Sumbera 	}
1124fbe4944aSPetr Sumbera 
1125fbe4944aSPetr Sumbera 	if (fstat(fi, &s) < 0) {
1126fbe4944aSPetr Sumbera 		err(EF_SYS, "cannot access: %s", file);
1127fbe4944aSPetr Sumbera 		(void) close(fi);
1128fbe4944aSPetr Sumbera 		return;
1129fbe4944aSPetr Sumbera 	}
1130fbe4944aSPetr Sumbera 
1131fbe4944aSPetr Sumbera 	/* create new file for copy destination with correct attributes */
1132fbe4944aSPetr Sumbera 	if ((fo = open(file_copy, O_CREAT|O_APPEND|O_WRONLY, s.st_mode)) < 0) {
1133fbe4944aSPetr Sumbera 		err(EF_SYS, "cannot create file: %s", file_copy);
1134fbe4944aSPetr Sumbera 		(void) close(fi);
1135fbe4944aSPetr Sumbera 		return;
1136fbe4944aSPetr Sumbera 	}
1137fbe4944aSPetr Sumbera 
1138fbe4944aSPetr Sumbera 	(void) fchown(fo, s.st_uid, s.st_gid);
1139fbe4944aSPetr Sumbera 
1140fbe4944aSPetr Sumbera 	/* lock log file so that nobody can write into it before we are done */
1141fbe4944aSPetr Sumbera 	if (fchmod(fi, s.st_mode|S_ISGID) < 0)
1142fbe4944aSPetr Sumbera 		err(EF_SYS, "cannot set mandatory lock bit for: %s", file);
1143fbe4944aSPetr Sumbera 
1144fbe4944aSPetr Sumbera 	if (lockf(fi, F_LOCK, 0) == -1)
1145fbe4944aSPetr Sumbera 		err(EF_SYS, "cannot lock file %s", file);
1146fbe4944aSPetr Sumbera 
1147fbe4944aSPetr Sumbera 	/* do atomic copy and truncation */
1148fbe4944aSPetr Sumbera 	while ((len = read(fi, buf, sizeof (buf))) > 0)
1149fbe4944aSPetr Sumbera 		if (write(fo, buf, len) != len) {
1150fbe4944aSPetr Sumbera 			err(EF_SYS, "cannot write into file %s", file_copy);
1151fbe4944aSPetr Sumbera 			(void) lockf(fi, F_ULOCK, 0);
1152fbe4944aSPetr Sumbera 			(void) fchmod(fi, s.st_mode);
1153fbe4944aSPetr Sumbera 			(void) close(fi);
1154fbe4944aSPetr Sumbera 			(void) close(fo);
1155fbe4944aSPetr Sumbera 			(void) remove(file_copy);
1156fbe4944aSPetr Sumbera 			return;
1157fbe4944aSPetr Sumbera 		}
1158fbe4944aSPetr Sumbera 
1159fbe4944aSPetr Sumbera 	(void) ftruncate(fi, 0);
1160fbe4944aSPetr Sumbera 
1161fbe4944aSPetr Sumbera 	/* unlock log file */
1162fbe4944aSPetr Sumbera 	if (lockf(fi, F_ULOCK, 0) == -1)
1163fbe4944aSPetr Sumbera 		err(EF_SYS, "cannot unlock file %s", file);
1164fbe4944aSPetr Sumbera 
1165fbe4944aSPetr Sumbera 	if (fchmod(fi, s.st_mode) < 0)
1166fbe4944aSPetr Sumbera 		err(EF_SYS, "cannot reset mandatory lock bit for: %s", file);
1167fbe4944aSPetr Sumbera 
1168fbe4944aSPetr Sumbera 	(void) close(fi);
1169fbe4944aSPetr Sumbera 	(void) close(fo);
1170fbe4944aSPetr Sumbera 
1171fbe4944aSPetr Sumbera 	/* keep times from original file */
1172fbe4944aSPetr Sumbera 	times.actime = s.st_atime;
1173fbe4944aSPetr Sumbera 	times.modtime = s.st_mtime;
1174fbe4944aSPetr Sumbera 	(void) utime(file_copy, &times);
1175fbe4944aSPetr Sumbera }
1176