xref: /titanic_52/usr/src/cmd/logadm/main.c (revision 0ade2cf005fcaecc5255dacf7d76683de855a9da)
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
5*0ade2cf0Sdg199075  * Common Development and Distribution License (the "License").
6*0ade2cf0Sdg199075  * 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*0ade2cf0Sdg199075  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * logadm/main.c -- main routines for logadm
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * this program is 90% argument processing, 10% actions...
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <strings.h>
367c478bd9Sstevel@tonic-gate #include <libintl.h>
377c478bd9Sstevel@tonic-gate #include <locale.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <sys/wait.h>
417c478bd9Sstevel@tonic-gate #include <sys/filio.h>
427c478bd9Sstevel@tonic-gate #include <time.h>
437c478bd9Sstevel@tonic-gate #include "err.h"
447c478bd9Sstevel@tonic-gate #include "lut.h"
457c478bd9Sstevel@tonic-gate #include "fn.h"
467c478bd9Sstevel@tonic-gate #include "opts.h"
477c478bd9Sstevel@tonic-gate #include "conf.h"
487c478bd9Sstevel@tonic-gate #include "glob.h"
497c478bd9Sstevel@tonic-gate #include "kw.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /* forward declarations for functions in this file */
527c478bd9Sstevel@tonic-gate static void usage(const char *msg);
537c478bd9Sstevel@tonic-gate static void commajoin(const char *lhs, void *rhs, void *arg);
547c478bd9Sstevel@tonic-gate static void doaftercmd(const char *lhs, void *rhs, void *arg);
557c478bd9Sstevel@tonic-gate static void dologname(struct fn *fnp, struct opts *clopts);
567c478bd9Sstevel@tonic-gate static boolean_t rotatelog(struct fn *fnp, struct opts *opts);
577c478bd9Sstevel@tonic-gate static void rotateto(struct fn *fnp, struct opts *opts, int n,
587c478bd9Sstevel@tonic-gate     struct fn *recentlog, boolean_t isgz);
59*0ade2cf0Sdg199075 static void do_delayed_gzip(const char *lhs, void *rhs, void *arg);
607c478bd9Sstevel@tonic-gate static void expirefiles(struct fn *fnp, struct opts *opts);
617c478bd9Sstevel@tonic-gate static void dorm(struct opts *opts, const char *msg, struct fn *fnp);
627c478bd9Sstevel@tonic-gate static void docmd(struct opts *opts, const char *msg, const char *cmd,
637c478bd9Sstevel@tonic-gate     const char *arg1, const char *arg2, const char *arg3);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /* our configuration file, unless otherwise specified by -f */
667c478bd9Sstevel@tonic-gate static char *Default_conffile = "/etc/logadm.conf";
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* default pathnames to the commands we invoke */
697c478bd9Sstevel@tonic-gate static char *Sh = "/bin/sh";
707c478bd9Sstevel@tonic-gate static char *Mv = "/bin/mv";
717c478bd9Sstevel@tonic-gate static char *Cp = "/bin/cp";
727c478bd9Sstevel@tonic-gate static char *Rm = "/bin/rm";
737c478bd9Sstevel@tonic-gate static char *Touch = "/bin/touch";
747c478bd9Sstevel@tonic-gate static char *Chmod = "/bin/chmod";
757c478bd9Sstevel@tonic-gate static char *Chown = "/bin/chown";
767c478bd9Sstevel@tonic-gate static char *Gzip = "/bin/gzip";
777c478bd9Sstevel@tonic-gate static char *Mkdir = "/bin/mkdir";
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /* return from time(0), gathered early on to avoid slewed timestamps */
807c478bd9Sstevel@tonic-gate time_t Now;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /* list of before commands that have been executed */
837c478bd9Sstevel@tonic-gate static struct lut *Beforecmds;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /* list of after commands to execute before exiting */
867c478bd9Sstevel@tonic-gate static struct lut *Aftercmds;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /* list of conffile entry names that are considered "done" */
897c478bd9Sstevel@tonic-gate static struct lut *Donenames;
907c478bd9Sstevel@tonic-gate 
91*0ade2cf0Sdg199075 /* A list of names of files to be gzipped */
92*0ade2cf0Sdg199075 static struct lut *Gzipnames = NULL;
93*0ade2cf0Sdg199075 
947c478bd9Sstevel@tonic-gate /* table that drives argument parsing */
957c478bd9Sstevel@tonic-gate static struct optinfo Opttable[] = {
967c478bd9Sstevel@tonic-gate 	{ "e", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
977c478bd9Sstevel@tonic-gate 	{ "f", OPTTYPE_STRING,	NULL,			OPTF_CLI },
987c478bd9Sstevel@tonic-gate 	{ "h", OPTTYPE_BOOLEAN,	NULL,			OPTF_CLI },
99636deb66Sgm149974 	{ "l", OPTTYPE_BOOLEAN, NULL,			OPTF_CLI },
1007c478bd9Sstevel@tonic-gate 	{ "N", OPTTYPE_BOOLEAN,	NULL,			OPTF_CLI|OPTF_CONF },
1017c478bd9Sstevel@tonic-gate 	{ "n", OPTTYPE_BOOLEAN,	NULL,			OPTF_CLI },
1027c478bd9Sstevel@tonic-gate 	{ "r", OPTTYPE_BOOLEAN,	NULL,			OPTF_CLI },
1037c478bd9Sstevel@tonic-gate 	{ "V", OPTTYPE_BOOLEAN,	NULL,			OPTF_CLI },
1047c478bd9Sstevel@tonic-gate 	{ "v", OPTTYPE_BOOLEAN,	NULL,			OPTF_CLI },
1057c478bd9Sstevel@tonic-gate 	{ "w", OPTTYPE_STRING,	NULL,			OPTF_CLI },
1067c478bd9Sstevel@tonic-gate 	{ "p", OPTTYPE_INT,	opts_parse_seconds,	OPTF_CLI|OPTF_CONF },
1077c478bd9Sstevel@tonic-gate 	{ "P", OPTTYPE_INT,	opts_parse_ctime,	OPTF_CLI|OPTF_CONF },
1087c478bd9Sstevel@tonic-gate 	{ "s", OPTTYPE_INT,	opts_parse_bytes,	OPTF_CLI|OPTF_CONF },
1097c478bd9Sstevel@tonic-gate 	{ "a", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
1107c478bd9Sstevel@tonic-gate 	{ "b", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
1117c478bd9Sstevel@tonic-gate 	{ "c", OPTTYPE_BOOLEAN, NULL,			OPTF_CLI|OPTF_CONF },
1127c478bd9Sstevel@tonic-gate 	{ "g", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
1137c478bd9Sstevel@tonic-gate 	{ "m", OPTTYPE_INT,	opts_parse_atopi,	OPTF_CLI|OPTF_CONF },
1147c478bd9Sstevel@tonic-gate 	{ "M", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
1157c478bd9Sstevel@tonic-gate 	{ "o", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
1167c478bd9Sstevel@tonic-gate 	{ "R", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
1177c478bd9Sstevel@tonic-gate 	{ "t", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
1187c478bd9Sstevel@tonic-gate 	{ "z", OPTTYPE_INT,	opts_parse_atopi,	OPTF_CLI|OPTF_CONF },
1197c478bd9Sstevel@tonic-gate 	{ "A", OPTTYPE_INT,	opts_parse_seconds,	OPTF_CLI|OPTF_CONF },
1207c478bd9Sstevel@tonic-gate 	{ "C", OPTTYPE_INT,	opts_parse_atopi,	OPTF_CLI|OPTF_CONF },
1217c478bd9Sstevel@tonic-gate 	{ "E", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
1227c478bd9Sstevel@tonic-gate 	{ "S", OPTTYPE_INT,	opts_parse_bytes,	OPTF_CLI|OPTF_CONF },
1237c478bd9Sstevel@tonic-gate 	{ "T", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
1247c478bd9Sstevel@tonic-gate };
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * only the "fhnVv" options are allowed in the first form of this command,
1287c478bd9Sstevel@tonic-gate  * so this defines the list of options that are an error in they appear
1297c478bd9Sstevel@tonic-gate  * in the first form.  In other words, it is not allowed to run logadm
1307c478bd9Sstevel@tonic-gate  * with any of these options unless at least one logname is also provided.
1317c478bd9Sstevel@tonic-gate  */
132636deb66Sgm149974 #define	OPTIONS_NOT_FIRST_FORM	"eNrwpPsabcglmoRtzACEST"
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /* text that we spew with the -h flag */
1357c478bd9Sstevel@tonic-gate #define	HELP1 \
1367c478bd9Sstevel@tonic-gate "Usage: logadm [options]\n"\
1377c478bd9Sstevel@tonic-gate "       (processes all entries in /etc/logadm.conf or conffile given by -f)\n"\
1387c478bd9Sstevel@tonic-gate "   or: logadm [options] logname...\n"\
1397c478bd9Sstevel@tonic-gate "       (processes the given lognames)\n"\
1407c478bd9Sstevel@tonic-gate "\n"\
1417c478bd9Sstevel@tonic-gate "General options:\n"\
1427c478bd9Sstevel@tonic-gate "        -e mailaddr     mail errors to given address\n"\
1437c478bd9Sstevel@tonic-gate "        -f conffile     use conffile instead of /etc/logadm.conf\n"\
1447c478bd9Sstevel@tonic-gate "        -h              display help\n"\
1457c478bd9Sstevel@tonic-gate "        -N              not an error if log file nonexistent\n"\
1467c478bd9Sstevel@tonic-gate "        -n              show actions, don't perform them\n"\
1477c478bd9Sstevel@tonic-gate "        -r              remove logname entry from conffile\n"\
1487c478bd9Sstevel@tonic-gate "        -V              ensure conffile entries exist, correct\n"\
1497c478bd9Sstevel@tonic-gate "        -v              print info about actions happening\n"\
1507c478bd9Sstevel@tonic-gate "        -w entryname    write entry to config file\n"\
1517c478bd9Sstevel@tonic-gate "\n"\
1527c478bd9Sstevel@tonic-gate "Options which control when a logfile is rotated:\n"\
1537c478bd9Sstevel@tonic-gate "(default is: -s1b -p1w if no -s or -p)\n"\
1547c478bd9Sstevel@tonic-gate "        -p period       only rotate if period passed since last rotate\n"\
1557c478bd9Sstevel@tonic-gate "        -P timestamp    used to store rotation date in conffile\n"\
1567c478bd9Sstevel@tonic-gate "        -s size         only rotate if given size or greater\n"\
1577c478bd9Sstevel@tonic-gate "\n"
1587c478bd9Sstevel@tonic-gate #define	HELP2 \
1597c478bd9Sstevel@tonic-gate "Options which control how a logfile is rotated:\n"\
1607c478bd9Sstevel@tonic-gate "(default is: -t '$file.$n', owner/group/mode taken from log file)\n"\
1617c478bd9Sstevel@tonic-gate "        -a cmd          execute cmd after taking actions\n"\
1627c478bd9Sstevel@tonic-gate "        -b cmd          execute cmd before taking actions\n"\
1637c478bd9Sstevel@tonic-gate "        -c              copy & truncate logfile, don't rename\n"\
1647c478bd9Sstevel@tonic-gate "        -g group        new empty log file group\n"\
165636deb66Sgm149974 "        -l              rotate log file with local time rather than UTC\n"\
1667c478bd9Sstevel@tonic-gate "        -m mode         new empty log file mode\n"\
1677c478bd9Sstevel@tonic-gate "        -M cmd          execute cmd to rotate the log file\n"\
1687c478bd9Sstevel@tonic-gate "        -o owner        new empty log file owner\n"\
1697c478bd9Sstevel@tonic-gate "        -R cmd          run cmd on file after rotate\n"\
1707c478bd9Sstevel@tonic-gate "        -t template     template for naming old logs\n"\
1717c478bd9Sstevel@tonic-gate "        -z count        gzip old logs except most recent count\n"\
1727c478bd9Sstevel@tonic-gate "\n"\
1737c478bd9Sstevel@tonic-gate "Options which control the expiration of old logfiles:\n"\
1747c478bd9Sstevel@tonic-gate "(default is: -C10 if no -A, -C, or -S)\n"\
1757c478bd9Sstevel@tonic-gate "        -A age          expire logs older than age\n"\
1767c478bd9Sstevel@tonic-gate "        -C count        expire old logs until count remain\n"\
1777c478bd9Sstevel@tonic-gate "        -E cmd          run cmd on file to expire\n"\
1787c478bd9Sstevel@tonic-gate "        -S size         expire until space used is below size \n"\
1797c478bd9Sstevel@tonic-gate "        -T pattern      pattern for finding old logs\n"
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * main -- where it all begins
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1857c478bd9Sstevel@tonic-gate int
1867c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	struct opts *clopts;		/* from parsing command line */
1897c478bd9Sstevel@tonic-gate 	const char *conffile;		/* our configuration file */
1907c478bd9Sstevel@tonic-gate 	struct fn_list *lognames;	/* list of lognames we're processing */
1917c478bd9Sstevel@tonic-gate 	struct fn *fnp;
1927c478bd9Sstevel@tonic-gate 	char *val;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1977c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* only used if Makefiles don't define it */
1987c478bd9Sstevel@tonic-gate #endif
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	/* we only print times into the conffile, so make them uniform */
2037c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_TIME, "C");
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/* give our name to error routines & skip it for arg parsing */
2067c478bd9Sstevel@tonic-gate 	err_init(*argv++);
2077c478bd9Sstevel@tonic-gate 	(void) setlinebuf(stdout);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if (putenv("PATH=/bin"))
2107c478bd9Sstevel@tonic-gate 		err(EF_SYS, "putenv PATH");
2117c478bd9Sstevel@tonic-gate 	if (putenv("TZ=UTC"))
2127c478bd9Sstevel@tonic-gate 		err(EF_SYS, "putenv TZ");
2137c478bd9Sstevel@tonic-gate 	tzset();
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	(void) umask(0);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	Now = time(0);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/* check for (undocumented) debugging environment variables */
2207c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_DEFAULT_CONFFILE"))
2217c478bd9Sstevel@tonic-gate 		Default_conffile = val;
2227c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_DEBUG"))
2237c478bd9Sstevel@tonic-gate 		Debug = atoi(val);
2247c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_SH"))
2257c478bd9Sstevel@tonic-gate 		Sh = val;
2267c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_MV"))
2277c478bd9Sstevel@tonic-gate 		Mv = val;
2287c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_CP"))
2297c478bd9Sstevel@tonic-gate 		Cp = val;
2307c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_RM"))
2317c478bd9Sstevel@tonic-gate 		Rm = val;
2327c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_TOUCH"))
2337c478bd9Sstevel@tonic-gate 		Touch = val;
2347c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_CHMOD"))
2357c478bd9Sstevel@tonic-gate 		Chmod = val;
2367c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_CHOWN"))
2377c478bd9Sstevel@tonic-gate 		Chown = val;
2387c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_GZIP"))
2397c478bd9Sstevel@tonic-gate 		Gzip = val;
2407c478bd9Sstevel@tonic-gate 	if (val = getenv("_LOGADM_MKDIR"))
2417c478bd9Sstevel@tonic-gate 		Mkdir = val;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	opts_init(Opttable, sizeof (Opttable) / sizeof (struct optinfo));
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/* parse command line arguments */
2467c478bd9Sstevel@tonic-gate 	if (SETJMP)
2477c478bd9Sstevel@tonic-gate 		usage("bailing out due to command line errors");
2487c478bd9Sstevel@tonic-gate 	else
2497c478bd9Sstevel@tonic-gate 		clopts = opts_parse(argv, OPTF_CLI);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (Debug) {
2527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "command line opts:");
2537c478bd9Sstevel@tonic-gate 		opts_print(clopts, stderr, NULL);
2547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/*
2587c478bd9Sstevel@tonic-gate 	 * There are many moods of logadm:
2597c478bd9Sstevel@tonic-gate 	 *
2607c478bd9Sstevel@tonic-gate 	 *	1. "-h" for help was given.  We spew a canned help
2617c478bd9Sstevel@tonic-gate 	 *	   message and exit, regardless of any other options given.
2627c478bd9Sstevel@tonic-gate 	 *
2637c478bd9Sstevel@tonic-gate 	 *	2. "-r" or "-w" asking us to write to the conffile.  Lots
2647c478bd9Sstevel@tonic-gate 	 *	   of argument checking, then we make the change to conffile
2657c478bd9Sstevel@tonic-gate 	 *	   and exit.  (-r processing actually happens in dologname().)
2667c478bd9Sstevel@tonic-gate 	 *
2677c478bd9Sstevel@tonic-gate 	 *	3. "-V" to search/verify the conffile was given.  We do
2687c478bd9Sstevel@tonic-gate 	 *	   the appropriate run through the conffile and exit.
2697c478bd9Sstevel@tonic-gate 	 *	   (-V processing actually happens in dologname().)
2707c478bd9Sstevel@tonic-gate 	 *
2717c478bd9Sstevel@tonic-gate 	 *	4. No lognames were given, so we're being asked to go through
2727c478bd9Sstevel@tonic-gate 	 *	   every entry in conffile.  We verify that only the options
2737c478bd9Sstevel@tonic-gate 	 *	   that make sense for this form of the command are present
2747c478bd9Sstevel@tonic-gate 	 *	   and fall into the main processing loop below.
2757c478bd9Sstevel@tonic-gate 	 *
2767c478bd9Sstevel@tonic-gate 	 *	5. lognames were given, so we fall into the main processing
2777c478bd9Sstevel@tonic-gate 	 *	   loop below to work our way through them.
2787c478bd9Sstevel@tonic-gate 	 *
2797c478bd9Sstevel@tonic-gate 	 * The last two cases are where the option processing gets more
2807c478bd9Sstevel@tonic-gate 	 * complex.  Each time around the main processing loop, we're
2817c478bd9Sstevel@tonic-gate 	 * in one of these cases:
2827c478bd9Sstevel@tonic-gate 	 *
2837c478bd9Sstevel@tonic-gate 	 *	A. No cmdargs were found (we're in case 4), the entry
2847c478bd9Sstevel@tonic-gate 	 *	   in conffile supplies no log file names, so the entry
2857c478bd9Sstevel@tonic-gate 	 *	   name itself is the logfile name (or names, if it globs
2867c478bd9Sstevel@tonic-gate 	 *	   to multiple file names).
2877c478bd9Sstevel@tonic-gate 	 *
2887c478bd9Sstevel@tonic-gate 	 *	B. No cmdargs were found (we're in case 4), the entry
2897c478bd9Sstevel@tonic-gate 	 *	   in conffile gives log file names that we then loop
2907c478bd9Sstevel@tonic-gate 	 *	   through and rotate/expire.  In this case, the entry
2917c478bd9Sstevel@tonic-gate 	 *	   name is specifically NOT one of the log file names.
2927c478bd9Sstevel@tonic-gate 	 *
2937c478bd9Sstevel@tonic-gate 	 *	C. We're going through the cmdargs (we're in case 5),
2947c478bd9Sstevel@tonic-gate 	 *	   the entry in conffile either doesn't exist or it exists
2957c478bd9Sstevel@tonic-gate 	 *	   but supplies no log file names, so the cmdarg itself
2967c478bd9Sstevel@tonic-gate 	 *	   is the log file name.
2977c478bd9Sstevel@tonic-gate 	 *
2987c478bd9Sstevel@tonic-gate 	 *	D. We're going through the cmdargs (we're in case 5),
2997c478bd9Sstevel@tonic-gate 	 *	   a matching entry in conffile supplies log file names
3007c478bd9Sstevel@tonic-gate 	 *	   that we then loop through and rotate/expire.  In this
3017c478bd9Sstevel@tonic-gate 	 *	   case the entry name is specifically NOT one of the log
3027c478bd9Sstevel@tonic-gate 	 *	   file names.
3037c478bd9Sstevel@tonic-gate 	 *
3047c478bd9Sstevel@tonic-gate 	 * As we're doing all this, any options given on the command line
3057c478bd9Sstevel@tonic-gate 	 * override any found in the conffile, and we apply the defaults
3067c478bd9Sstevel@tonic-gate 	 * for rotation conditions and expiration conditions, etc. at the
3077c478bd9Sstevel@tonic-gate 	 * last opportunity, when we're sure they haven't been overridden
3087c478bd9Sstevel@tonic-gate 	 * by an option somewhere along the way.
3097c478bd9Sstevel@tonic-gate 	 *
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/* help option overrides anything else */
3137c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "h")) {
3147c478bd9Sstevel@tonic-gate 		(void) fputs(HELP1, stderr);
3157c478bd9Sstevel@tonic-gate 		(void) fputs(HELP2, stderr);
3167c478bd9Sstevel@tonic-gate 		err_done(0);
3177c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/* detect illegal option combinations */
3217c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "rwV") > 1)
3227c478bd9Sstevel@tonic-gate 		usage("Only one of -r, -w, or -V may be used at a time.");
3237c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "cM") > 1)
3247c478bd9Sstevel@tonic-gate 		usage("Only one of -c or -M may be used at a time.");
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/* arrange for error output to be mailed if clopts includes -e */
3277c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "e"))
3287c478bd9Sstevel@tonic-gate 		err_mailto(opts_optarg(clopts, "e"));
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/* this implements the default conffile */
3317c478bd9Sstevel@tonic-gate 	if ((conffile = opts_optarg(clopts, "f")) == NULL)
3327c478bd9Sstevel@tonic-gate 		conffile = Default_conffile;
3337c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "v"))
3347c478bd9Sstevel@tonic-gate 		(void) out("# loading %s\n", conffile);
3357c478bd9Sstevel@tonic-gate 	conf_open(conffile, opts_count(clopts, "Vn") == 0);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/* handle conffile write option */
3387c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "w")) {
3397c478bd9Sstevel@tonic-gate 		if (Debug)
3407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3417c478bd9Sstevel@tonic-gate 			    "main: add/replace conffile entry: <%s>\n",
3427c478bd9Sstevel@tonic-gate 			    opts_optarg(clopts, "w"));
3437c478bd9Sstevel@tonic-gate 		conf_replace(opts_optarg(clopts, "w"), clopts);
3447c478bd9Sstevel@tonic-gate 		conf_close(clopts);
3457c478bd9Sstevel@tonic-gate 		err_done(0);
3467c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * lognames is either a list supplied on the command line,
3517c478bd9Sstevel@tonic-gate 	 * or every entry in the conffile if none were supplied.
3527c478bd9Sstevel@tonic-gate 	 */
3537c478bd9Sstevel@tonic-gate 	lognames = opts_cmdargs(clopts);
3547c478bd9Sstevel@tonic-gate 	if (fn_list_empty(lognames)) {
3557c478bd9Sstevel@tonic-gate 		/*
3567c478bd9Sstevel@tonic-gate 		 * being asked to do all entries in conffile
3577c478bd9Sstevel@tonic-gate 		 *
3587c478bd9Sstevel@tonic-gate 		 * check to see if any options were given that only
3597c478bd9Sstevel@tonic-gate 		 * make sense when lognames are given specifically
3607c478bd9Sstevel@tonic-gate 		 * on the command line.
3617c478bd9Sstevel@tonic-gate 		 */
3627c478bd9Sstevel@tonic-gate 		if (opts_count(clopts, OPTIONS_NOT_FIRST_FORM))
3637c478bd9Sstevel@tonic-gate 			usage("some options require logname argument");
3647c478bd9Sstevel@tonic-gate 		if (Debug)
3657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3667c478bd9Sstevel@tonic-gate 			    "main: run all entries in conffile\n");
3677c478bd9Sstevel@tonic-gate 		lognames = conf_entries();
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	/* foreach logname... */
3717c478bd9Sstevel@tonic-gate 	fn_list_rewind(lognames);
3727c478bd9Sstevel@tonic-gate 	while ((fnp = fn_list_next(lognames)) != NULL) {
3737c478bd9Sstevel@tonic-gate 		if (lut_lookup(Donenames, fn_s(fnp)) != NULL) {
3747c478bd9Sstevel@tonic-gate 			if (Debug)
3757c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3767c478bd9Sstevel@tonic-gate 				    "main: logname already done: <%s>\n",
3777c478bd9Sstevel@tonic-gate 				    fn_s(fnp));
3787c478bd9Sstevel@tonic-gate 			continue;
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 		if (SETJMP)
3817c478bd9Sstevel@tonic-gate 			err(EF_FILE, "bailing out on logname \"%s\" "
3827c478bd9Sstevel@tonic-gate 			    "due to errors", fn_s(fnp));
3837c478bd9Sstevel@tonic-gate 		else
3847c478bd9Sstevel@tonic-gate 			dologname(fnp, clopts);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/* execute any after commands */
3887c478bd9Sstevel@tonic-gate 	lut_walk(Aftercmds, doaftercmd, clopts);
3897c478bd9Sstevel@tonic-gate 
390*0ade2cf0Sdg199075 	/* execute any gzip commands */
391*0ade2cf0Sdg199075 	lut_walk(Gzipnames, do_delayed_gzip, clopts);
392*0ade2cf0Sdg199075 	lut_free(Gzipnames, free);
393*0ade2cf0Sdg199075 	Gzipnames = NULL;
394*0ade2cf0Sdg199075 
3957c478bd9Sstevel@tonic-gate 	/* write out any conffile changes */
3967c478bd9Sstevel@tonic-gate 	conf_close(clopts);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	err_done(0);
3997c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
4007c478bd9Sstevel@tonic-gate 	return (0);	/* for lint's little mind */
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate /* spew a message, then a usage message, then exit */
4047c478bd9Sstevel@tonic-gate static void
4057c478bd9Sstevel@tonic-gate usage(const char *msg)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	if (msg)
4087c478bd9Sstevel@tonic-gate 		err(0, "%s\nUse \"logadm -h\" for help.", msg);
4097c478bd9Sstevel@tonic-gate 	else
4107c478bd9Sstevel@tonic-gate 		err(EF_RAW, "Use \"logadm -h\" for help.\n");
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate /* helper function used by doaftercmd() to join mail addrs with commas */
4147c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
4157c478bd9Sstevel@tonic-gate static void
4167c478bd9Sstevel@tonic-gate commajoin(const char *lhs, void *rhs, void *arg)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	struct fn *fnp = (struct fn *)arg;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if (*fn_s(fnp))
4217c478bd9Sstevel@tonic-gate 		fn_putc(fnp, ',');
4227c478bd9Sstevel@tonic-gate 	fn_puts(fnp, lhs);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate /* helper function used by main() to run "after" commands */
4267c478bd9Sstevel@tonic-gate static void
4277c478bd9Sstevel@tonic-gate doaftercmd(const char *lhs, void *rhs, void *arg)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 	struct opts *opts = (struct opts *)arg;
4307c478bd9Sstevel@tonic-gate 	struct lut *addrs = (struct lut *)rhs;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	if (addrs) {
4337c478bd9Sstevel@tonic-gate 		struct fn *fnp = fn_new(NULL);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		/*
4367c478bd9Sstevel@tonic-gate 		 * addrs contains list of email addrs that should get
4377c478bd9Sstevel@tonic-gate 		 * the error output when this after command is executed.
4387c478bd9Sstevel@tonic-gate 		 */
4397c478bd9Sstevel@tonic-gate 		lut_walk(addrs, commajoin, fnp);
4407c478bd9Sstevel@tonic-gate 		err_mailto(fn_s(fnp));
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	docmd(opts, "-a cmd", Sh, "-c", lhs, NULL);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
446*0ade2cf0Sdg199075 /* perform delayed gzip */
447*0ade2cf0Sdg199075 
448*0ade2cf0Sdg199075 static void
449*0ade2cf0Sdg199075 do_delayed_gzip(const char *lhs, void *rhs, void *arg)
450*0ade2cf0Sdg199075 {
451*0ade2cf0Sdg199075 	struct opts *opts = (struct opts *)arg;
452*0ade2cf0Sdg199075 
453*0ade2cf0Sdg199075 	if (rhs == NULL) {
454*0ade2cf0Sdg199075 		if (Debug) {
455*0ade2cf0Sdg199075 			(void) fprintf(stderr, "do_delayed_gzip: not gzipping "
456*0ade2cf0Sdg199075 				"expired file <%s>\n", lhs);
457*0ade2cf0Sdg199075 		}
458*0ade2cf0Sdg199075 		return;
459*0ade2cf0Sdg199075 	}
460*0ade2cf0Sdg199075 	docmd(opts, "compress old log (-z flag)", Gzip, "-f", lhs, NULL);
461*0ade2cf0Sdg199075 }
462*0ade2cf0Sdg199075 
463*0ade2cf0Sdg199075 
4647c478bd9Sstevel@tonic-gate /* main logname processing */
4657c478bd9Sstevel@tonic-gate static void
4667c478bd9Sstevel@tonic-gate dologname(struct fn *fnp, struct opts *clopts)
4677c478bd9Sstevel@tonic-gate {
4687c478bd9Sstevel@tonic-gate 	const char *logname = fn_s(fnp);
4697c478bd9Sstevel@tonic-gate 	struct opts *cfopts;
4707c478bd9Sstevel@tonic-gate 	struct opts *allopts;
4717c478bd9Sstevel@tonic-gate 	struct fn_list *logfiles;
4727c478bd9Sstevel@tonic-gate 	struct fn_list *globbedfiles;
4737c478bd9Sstevel@tonic-gate 	struct fn *nextfnp;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/* look up options set by config file */
4767c478bd9Sstevel@tonic-gate 	cfopts = conf_opts(logname);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "v"))
4797c478bd9Sstevel@tonic-gate 		(void) out("# processing logname: %s\n", logname);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (Debug) {
4827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dologname: logname <%s>\n", logname);
4837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "conffile opts:");
4847c478bd9Sstevel@tonic-gate 		opts_print(cfopts, stderr, NULL);
4857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	/* handle conffile lookup option */
4897c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "V")) {
4907c478bd9Sstevel@tonic-gate 		/* lookup an entry in conffile */
4917c478bd9Sstevel@tonic-gate 		if (Debug)
4927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4937c478bd9Sstevel@tonic-gate 			    "dologname: lookup conffile entry\n");
4947c478bd9Sstevel@tonic-gate 		if (conf_lookup(logname)) {
4957c478bd9Sstevel@tonic-gate 			opts_printword(logname, stdout);
4967c478bd9Sstevel@tonic-gate 			opts_print(cfopts, stdout, NULL);
4977c478bd9Sstevel@tonic-gate 			(void) out("\n");
4987c478bd9Sstevel@tonic-gate 		} else
4997c478bd9Sstevel@tonic-gate 			err_exitcode(1);
5007c478bd9Sstevel@tonic-gate 		return;
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	/* handle conffile removal option */
5047c478bd9Sstevel@tonic-gate 	if (opts_count(clopts, "r")) {
5057c478bd9Sstevel@tonic-gate 		if (Debug)
5067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5077c478bd9Sstevel@tonic-gate 			    "dologname: remove conffile entry\n");
5087c478bd9Sstevel@tonic-gate 		if (conf_lookup(logname))
5097c478bd9Sstevel@tonic-gate 			conf_replace(logname, NULL);
5107c478bd9Sstevel@tonic-gate 		else
5117c478bd9Sstevel@tonic-gate 			err_exitcode(1);
5127c478bd9Sstevel@tonic-gate 		return;
5137c478bd9Sstevel@tonic-gate 	}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	/* generate combined options */
5167c478bd9Sstevel@tonic-gate 	allopts = opts_merge(cfopts, clopts);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	/* arrange for error output to be mailed if allopts includes -e */
5197c478bd9Sstevel@tonic-gate 	if (opts_count(allopts, "e"))
5207c478bd9Sstevel@tonic-gate 		err_mailto(opts_optarg(allopts, "e"));
5217c478bd9Sstevel@tonic-gate 	else
5227c478bd9Sstevel@tonic-gate 		err_mailto(NULL);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/* this implements the default rotation rules */
5257c478bd9Sstevel@tonic-gate 	if (opts_count(allopts, "sp") == 0) {
5267c478bd9Sstevel@tonic-gate 		if (opts_count(clopts, "v"))
5277c478bd9Sstevel@tonic-gate 			(void) out(
5287c478bd9Sstevel@tonic-gate 			    "#     using default rotate rules: -s1b -p1w\n");
5297c478bd9Sstevel@tonic-gate 		(void) opts_set(allopts, "s", "1b");
5307c478bd9Sstevel@tonic-gate 		(void) opts_set(allopts, "p", "1w");
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/* this implements the default expiration rules */
5347c478bd9Sstevel@tonic-gate 	if (opts_count(allopts, "ACS") == 0) {
5357c478bd9Sstevel@tonic-gate 		if (opts_count(clopts, "v"))
5367c478bd9Sstevel@tonic-gate 			(void) out("#     using default expire rule: -C10\n");
5377c478bd9Sstevel@tonic-gate 		(void) opts_set(allopts, "C", "10");
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/* this implements the default template */
5417c478bd9Sstevel@tonic-gate 	if (opts_count(allopts, "t") == 0) {
5427c478bd9Sstevel@tonic-gate 		if (opts_count(clopts, "v"))
5437c478bd9Sstevel@tonic-gate 			(void) out("#     using default template: $file.$n\n");
5447c478bd9Sstevel@tonic-gate 		(void) opts_set(allopts, "t", "$file.$n");
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if (Debug) {
5487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "merged opts:");
5497c478bd9Sstevel@tonic-gate 		opts_print(allopts, stderr, NULL);
5507c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	/*
5547c478bd9Sstevel@tonic-gate 	 * if the conffile entry supplied log file names, then
5557c478bd9Sstevel@tonic-gate 	 * logname is NOT one of the log file names (it was just
5567c478bd9Sstevel@tonic-gate 	 * the entry name in conffile).
5577c478bd9Sstevel@tonic-gate 	 */
5587c478bd9Sstevel@tonic-gate 	logfiles = opts_cmdargs(cfopts);
5597c478bd9Sstevel@tonic-gate 	if (Debug) {
5607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dologname: logfiles from cfopts:\n");
5617c478bd9Sstevel@tonic-gate 		fn_list_rewind(logfiles);
5627c478bd9Sstevel@tonic-gate 		while ((nextfnp = fn_list_next(logfiles)) != NULL)
5637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "    <%s>\n", fn_s(nextfnp));
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 	if (fn_list_empty(logfiles))
5667c478bd9Sstevel@tonic-gate 		globbedfiles = glob_glob(fnp);
5677c478bd9Sstevel@tonic-gate 	else
5687c478bd9Sstevel@tonic-gate 		globbedfiles = glob_glob_list(logfiles);
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	/* go through the list produced by glob expansion */
5717c478bd9Sstevel@tonic-gate 	fn_list_rewind(globbedfiles);
5727c478bd9Sstevel@tonic-gate 	while ((nextfnp = fn_list_next(globbedfiles)) != NULL)
5737c478bd9Sstevel@tonic-gate 		if (rotatelog(nextfnp, allopts))
5747c478bd9Sstevel@tonic-gate 			expirefiles(nextfnp, allopts);
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	fn_list_free(globbedfiles);
5777c478bd9Sstevel@tonic-gate 	opts_free(allopts);
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate /* absurdly long buffer lengths for holding user/group/mode strings */
5827c478bd9Sstevel@tonic-gate #define	TIMESTRMAX	100
5837c478bd9Sstevel@tonic-gate #define	MAXATTR		100
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate /* rotate a log file if necessary, returns true if ok to go on to expire step */
5867c478bd9Sstevel@tonic-gate static boolean_t
5877c478bd9Sstevel@tonic-gate rotatelog(struct fn *fnp, struct opts *opts)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	char *fname = fn_s(fnp);
5907c478bd9Sstevel@tonic-gate 	struct stat stbuf;
5917c478bd9Sstevel@tonic-gate 	char nowstr[TIMESTRMAX];
5927c478bd9Sstevel@tonic-gate 	struct fn *recentlog = fn_new(NULL);	/* for -R cmd */
5937c478bd9Sstevel@tonic-gate 	char ownerbuf[MAXATTR];
5947c478bd9Sstevel@tonic-gate 	char groupbuf[MAXATTR];
5957c478bd9Sstevel@tonic-gate 	char modebuf[MAXATTR];
5967c478bd9Sstevel@tonic-gate 	const char *owner;
5977c478bd9Sstevel@tonic-gate 	const char *group;
5987c478bd9Sstevel@tonic-gate 	const char *mode;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (Debug)
6017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "rotatelog: fname <%s>\n", fname);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "p") && opts_optarg_int(opts, "p") == OPTP_NEVER)
6047c478bd9Sstevel@tonic-gate 		return (B_TRUE);	/* "-p never" forced no rotate */
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	/* prepare the keywords */
6077c478bd9Sstevel@tonic-gate 	kw_init(fnp, NULL);
6087c478bd9Sstevel@tonic-gate 	if (Debug > 1) {
6097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "rotatelog keywords:\n");
6107c478bd9Sstevel@tonic-gate 		kw_print(stderr);
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	if (lstat(fname, &stbuf) < 0) {
6147c478bd9Sstevel@tonic-gate 		if (opts_count(opts, "N"))
6157c478bd9Sstevel@tonic-gate 			return (1);
6167c478bd9Sstevel@tonic-gate 		err(EF_WARN|EF_SYS, "%s", fname);
6177c478bd9Sstevel@tonic-gate 		return (B_FALSE);
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	if ((stbuf.st_mode & S_IFMT) == S_IFLNK) {
6217c478bd9Sstevel@tonic-gate 		err(EF_WARN, "%s is a symlink", fname);
6227c478bd9Sstevel@tonic-gate 		return (B_FALSE);
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	if ((stbuf.st_mode & S_IFMT) != S_IFREG) {
6267c478bd9Sstevel@tonic-gate 		err(EF_WARN, "%s is not a regular file", fname);
6277c478bd9Sstevel@tonic-gate 		return (B_FALSE);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	/* see if size condition is present, and return if not met */
6317c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "s") && stbuf.st_size < opts_optarg_int(opts, "s"))
6327c478bd9Sstevel@tonic-gate 		return (B_TRUE);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	/* see if age condition is present, and return if not met */
6357c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "p")) {
6367c478bd9Sstevel@tonic-gate 		int when = opts_optarg_int(opts, "p");
6377c478bd9Sstevel@tonic-gate 		struct opts *cfopts;
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		/* unless rotate forced by "-p now", see if period has passed */
6407c478bd9Sstevel@tonic-gate 		if (when != OPTP_NOW) {
6417c478bd9Sstevel@tonic-gate 			/*
6427c478bd9Sstevel@tonic-gate 			 * "when" holds the number of seconds that must have
6437c478bd9Sstevel@tonic-gate 			 * passed since the last time this log was rotated.
6447c478bd9Sstevel@tonic-gate 			 * of course, running logadm can take a little time
6457c478bd9Sstevel@tonic-gate 			 * (typically a second or two, but longer if the
6467c478bd9Sstevel@tonic-gate 			 * conffile has lots of stuff in it) and that amount
6477c478bd9Sstevel@tonic-gate 			 * of time is variable, depending on system load, etc.
6487c478bd9Sstevel@tonic-gate 			 * so we want to allow a little "slop" in the value of
6497c478bd9Sstevel@tonic-gate 			 * "when".  this way, if a log should be rotated every
6507c478bd9Sstevel@tonic-gate 			 * week, and the number of seconds passed is really a
6517c478bd9Sstevel@tonic-gate 			 * few seconds short of a week, we'll go ahead and
6527c478bd9Sstevel@tonic-gate 			 * rotate the log as expected.
6537c478bd9Sstevel@tonic-gate 			 *
6547c478bd9Sstevel@tonic-gate 			 */
6557c478bd9Sstevel@tonic-gate 			if (when >= 60 * 60)
6567c478bd9Sstevel@tonic-gate 				when -= 59;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 			/*
6597c478bd9Sstevel@tonic-gate 			 * last rotation is recorded as argument to -P,
6607c478bd9Sstevel@tonic-gate 			 * but if logname isn't the same as log file name
6617c478bd9Sstevel@tonic-gate 			 * then the timestamp would be recorded on a
6627c478bd9Sstevel@tonic-gate 			 * separate line in the conf file.  so if we
6637c478bd9Sstevel@tonic-gate 			 * haven't seen a -P already, we check to see if
6647c478bd9Sstevel@tonic-gate 			 * it is part of a specific entry for the log
6657c478bd9Sstevel@tonic-gate 			 * file name.  this handles the case where the
6667c478bd9Sstevel@tonic-gate 			 * logname is "apache", it supplies a log file
6677c478bd9Sstevel@tonic-gate 			 * name like "/var/apache/logs/[a-z]*_log",
6687c478bd9Sstevel@tonic-gate 			 * which expands to multiple file names.  if one
6697c478bd9Sstevel@tonic-gate 			 * of the file names is "/var/apache/logs/access_log"
6707c478bd9Sstevel@tonic-gate 			 * the the -P will be attached to a line with that
6717c478bd9Sstevel@tonic-gate 			 * logname in the conf file.
6727c478bd9Sstevel@tonic-gate 			 */
6737c478bd9Sstevel@tonic-gate 			if (opts_count(opts, "P")) {
6747c478bd9Sstevel@tonic-gate 				int last = opts_optarg_int(opts, "P");
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 				/* return if not enough time has passed */
6777c478bd9Sstevel@tonic-gate 				if (Now - last < when)
6787c478bd9Sstevel@tonic-gate 					return (B_TRUE);
6797c478bd9Sstevel@tonic-gate 			} else if ((cfopts = conf_opts(fname)) != NULL &&
6807c478bd9Sstevel@tonic-gate 			    opts_count(cfopts, "P")) {
6817c478bd9Sstevel@tonic-gate 				int last = opts_optarg_int(cfopts, "P");
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 				/*
6847c478bd9Sstevel@tonic-gate 				 * just checking this means this entry
6857c478bd9Sstevel@tonic-gate 				 * is now "done" if we're going through
6867c478bd9Sstevel@tonic-gate 				 * the entire conffile
6877c478bd9Sstevel@tonic-gate 				 */
6887c478bd9Sstevel@tonic-gate 				Donenames = lut_add(Donenames, fname, "1");
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 				/* return if not enough time has passed */
6917c478bd9Sstevel@tonic-gate 				if (Now - last < when)
6927c478bd9Sstevel@tonic-gate 					return (B_TRUE);
6937c478bd9Sstevel@tonic-gate 			}
6947c478bd9Sstevel@tonic-gate 		}
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	if (Debug)
6987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "rotatelog: conditions met\n");
699636deb66Sgm149974 	if (opts_count(opts, "l")) {
700636deb66Sgm149974 		/* Change the time zone to local time zone */
701636deb66Sgm149974 		if (putenv("TZ="))
702636deb66Sgm149974 			err(EF_SYS, "putenv TZ");
703636deb66Sgm149974 		tzset();
704636deb66Sgm149974 		Now = time(0);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 		/* rename the log file */
7077c478bd9Sstevel@tonic-gate 		rotateto(fnp, opts, 0, recentlog, B_FALSE);
7087c478bd9Sstevel@tonic-gate 
709636deb66Sgm149974 		/* Change the time zone to UTC */
710636deb66Sgm149974 		if (putenv("TZ=UTC"))
711636deb66Sgm149974 			err(EF_SYS, "putenv TZ");
712636deb66Sgm149974 		tzset();
713636deb66Sgm149974 		Now = time(0);
714636deb66Sgm149974 	} else {
715636deb66Sgm149974 		/* rename the log file */
716636deb66Sgm149974 		rotateto(fnp, opts, 0, recentlog, B_FALSE);
717636deb66Sgm149974 	}
718636deb66Sgm149974 
7197c478bd9Sstevel@tonic-gate 	/* determine owner, group, mode for empty log file */
7207c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "o"))
7217c478bd9Sstevel@tonic-gate 		(void) strlcpy(ownerbuf, opts_optarg(opts, "o"), MAXATTR);
7227c478bd9Sstevel@tonic-gate 	else {
7237c478bd9Sstevel@tonic-gate 		(void) snprintf(ownerbuf, MAXATTR, "%ld", stbuf.st_uid);
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 	owner = ownerbuf;
7267c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "g"))
7277c478bd9Sstevel@tonic-gate 		group = opts_optarg(opts, "g");
7287c478bd9Sstevel@tonic-gate 	else {
7297c478bd9Sstevel@tonic-gate 		(void) snprintf(groupbuf, MAXATTR, "%ld", stbuf.st_gid);
7307c478bd9Sstevel@tonic-gate 		group = groupbuf;
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 	(void) strlcat(ownerbuf, ":", MAXATTR - strlen(ownerbuf));
7337c478bd9Sstevel@tonic-gate 	(void) strlcat(ownerbuf, group, MAXATTR - strlen(ownerbuf));
7347c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "m"))
7357c478bd9Sstevel@tonic-gate 		mode = opts_optarg(opts, "m");
7367c478bd9Sstevel@tonic-gate 	else {
7377c478bd9Sstevel@tonic-gate 		(void) snprintf(modebuf, MAXATTR,
7387c478bd9Sstevel@tonic-gate 		    "%03lo", stbuf.st_mode & 0777);
7397c478bd9Sstevel@tonic-gate 		mode = modebuf;
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	/* create the empty log file */
7437c478bd9Sstevel@tonic-gate 	docmd(opts, NULL, Touch, fname, NULL, NULL);
7447c478bd9Sstevel@tonic-gate 	docmd(opts, NULL, Chown, owner, fname, NULL);
7457c478bd9Sstevel@tonic-gate 	docmd(opts, NULL, Chmod, mode, fname, NULL);
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	/* execute post-rotation command */
7487c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "R")) {
7497c478bd9Sstevel@tonic-gate 		struct fn *rawcmd = fn_new(opts_optarg(opts, "R"));
7507c478bd9Sstevel@tonic-gate 		struct fn *cmd = fn_new(NULL);
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 		kw_init(recentlog, NULL);
7537c478bd9Sstevel@tonic-gate 		(void) kw_expand(rawcmd, cmd, 0, B_FALSE);
7547c478bd9Sstevel@tonic-gate 		docmd(opts, "-R cmd", Sh, "-c", fn_s(cmd), NULL);
7557c478bd9Sstevel@tonic-gate 		fn_free(rawcmd);
7567c478bd9Sstevel@tonic-gate 		fn_free(cmd);
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 	fn_free(recentlog);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	/*
7617c478bd9Sstevel@tonic-gate 	 * add "after" command to list of after commands.  we also record
7627c478bd9Sstevel@tonic-gate 	 * the email address, if any, where the error output of the after
7637c478bd9Sstevel@tonic-gate 	 * command should be sent.  if the after command is already on
7647c478bd9Sstevel@tonic-gate 	 * our list, add the email addr to the list the email addrs for
7657c478bd9Sstevel@tonic-gate 	 * that command (the after command will only be executed once,
7667c478bd9Sstevel@tonic-gate 	 * so the error output gets mailed to every address we've come
7677c478bd9Sstevel@tonic-gate 	 * across associated with this command).
7687c478bd9Sstevel@tonic-gate 	 */
7697c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "a")) {
7707c478bd9Sstevel@tonic-gate 		const char *cmd = opts_optarg(opts, "a");
7717c478bd9Sstevel@tonic-gate 		struct lut *addrs = (struct lut *)lut_lookup(Aftercmds, cmd);
7727c478bd9Sstevel@tonic-gate 		if (opts_count(opts, "e"))
7737c478bd9Sstevel@tonic-gate 			addrs = lut_add(addrs, opts_optarg(opts, "e"), NULL);
7747c478bd9Sstevel@tonic-gate 		Aftercmds = lut_add(Aftercmds, opts_optarg(opts, "a"), addrs);
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	/* record the rotation date */
7787c478bd9Sstevel@tonic-gate 	(void) strftime(nowstr, sizeof (nowstr),
7797c478bd9Sstevel@tonic-gate 	    "%a %b %e %T %Y", gmtime(&Now));
7807c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "v"))
7817c478bd9Sstevel@tonic-gate 		(void) out("#     recording rotation date %s for %s\n",
7827c478bd9Sstevel@tonic-gate 		    nowstr, fname);
7837c478bd9Sstevel@tonic-gate 	conf_set(fname, "P", STRDUP(nowstr));
7847c478bd9Sstevel@tonic-gate 	Donenames = lut_add(Donenames, fname, "1");
7857c478bd9Sstevel@tonic-gate 	return (B_TRUE);
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate /* rotate files "up" according to current template */
7897c478bd9Sstevel@tonic-gate static void
7907c478bd9Sstevel@tonic-gate rotateto(struct fn *fnp, struct opts *opts, int n, struct fn *recentlog,
7917c478bd9Sstevel@tonic-gate     boolean_t isgz)
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate 	struct fn *template = fn_new(opts_optarg(opts, "t"));
7947c478bd9Sstevel@tonic-gate 	struct fn *newfile = fn_new(NULL);
7957c478bd9Sstevel@tonic-gate 	struct fn *dirname;
7967c478bd9Sstevel@tonic-gate 	int hasn;
7977c478bd9Sstevel@tonic-gate 	struct stat stbuf;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	/* expand template to figure out new filename */
8007c478bd9Sstevel@tonic-gate 	hasn = kw_expand(template, newfile, n, isgz);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	if (Debug)
8037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "rotateto: %s -> %s (%d)\n", fn_s(fnp),
8047c478bd9Sstevel@tonic-gate 		    fn_s(newfile), n);
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	/* if filename is there already, rotate "up" */
8077c478bd9Sstevel@tonic-gate 	if (hasn && lstat(fn_s(newfile), &stbuf) != -1)
8087c478bd9Sstevel@tonic-gate 		rotateto(newfile, opts, n + 1, recentlog, isgz);
8097c478bd9Sstevel@tonic-gate 	else if (hasn && opts_count(opts, "z")) {
8107c478bd9Sstevel@tonic-gate 		struct fn *gzfnp = fn_dup(newfile);
8117c478bd9Sstevel@tonic-gate 		/*
8127c478bd9Sstevel@tonic-gate 		 * since we're compressing old files, see if we
8137c478bd9Sstevel@tonic-gate 		 * about to rotate into one.
8147c478bd9Sstevel@tonic-gate 		 */
8157c478bd9Sstevel@tonic-gate 		fn_puts(gzfnp, ".gz");
8167c478bd9Sstevel@tonic-gate 		if (lstat(fn_s(gzfnp), &stbuf) != -1)
8177c478bd9Sstevel@tonic-gate 			rotateto(gzfnp, opts, n + 1, recentlog, B_TRUE);
8187c478bd9Sstevel@tonic-gate 		fn_free(gzfnp);
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	/* first time through run "before" cmd if not run already */
8227c478bd9Sstevel@tonic-gate 	if (n == 0 && opts_count(opts, "b")) {
8237c478bd9Sstevel@tonic-gate 		const char *cmd = opts_optarg(opts, "b");
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 		if (lut_lookup(Beforecmds, cmd) == NULL) {
8267c478bd9Sstevel@tonic-gate 			docmd(opts, "-b cmd", Sh, "-c", cmd, NULL);
8277c478bd9Sstevel@tonic-gate 			Beforecmds = lut_add(Beforecmds, cmd, "1");
8287c478bd9Sstevel@tonic-gate 		}
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	/* ensure destination directory exists */
8327c478bd9Sstevel@tonic-gate 	dirname = fn_dirname(newfile);
8337c478bd9Sstevel@tonic-gate 	docmd(opts, "verify directory exists", Mkdir, "-p",
8347c478bd9Sstevel@tonic-gate 	    fn_s(dirname), NULL);
8357c478bd9Sstevel@tonic-gate 	fn_free(dirname);
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	/* do the rename */
8387c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "c")) {
8397c478bd9Sstevel@tonic-gate 		docmd(opts, "rotate log file via copy (-c flag)",
8407c478bd9Sstevel@tonic-gate 		    Cp, "-fp", fn_s(fnp), fn_s(newfile));
8417c478bd9Sstevel@tonic-gate 		docmd(opts, "truncate log file (-c flag)",
8427c478bd9Sstevel@tonic-gate 		    Cp, "-f", "/dev/null", fn_s(fnp));
8437c478bd9Sstevel@tonic-gate 	} else if (n == 0 && opts_count(opts, "M")) {
8447c478bd9Sstevel@tonic-gate 		struct fn *rawcmd = fn_new(opts_optarg(opts, "M"));
8457c478bd9Sstevel@tonic-gate 		struct fn *cmd = fn_new(NULL);
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 		/* use specified command to mv the log file */
8487c478bd9Sstevel@tonic-gate 		kw_init(fnp, newfile);
8497c478bd9Sstevel@tonic-gate 		(void) kw_expand(rawcmd, cmd, 0, B_FALSE);
8507c478bd9Sstevel@tonic-gate 		docmd(opts, "-M cmd", Sh, "-c", fn_s(cmd), NULL);
8517c478bd9Sstevel@tonic-gate 		fn_free(rawcmd);
8527c478bd9Sstevel@tonic-gate 		fn_free(cmd);
8537c478bd9Sstevel@tonic-gate 	} else
8547c478bd9Sstevel@tonic-gate 		/* common case: we call "mv" to handle the actual rename */
8557c478bd9Sstevel@tonic-gate 		docmd(opts, "rotate log file", Mv, "-f",
8567c478bd9Sstevel@tonic-gate 		    fn_s(fnp), fn_s(newfile));
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/* gzip the old log file  according to -z count */
8597c478bd9Sstevel@tonic-gate 	if (!isgz && opts_count(opts, "z")) {
8607c478bd9Sstevel@tonic-gate 		int count = opts_optarg_int(opts, "z");
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 		if (Debug)
8637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "rotateto z count %d\n", count);
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 		if (count <= n) {
866*0ade2cf0Sdg199075 
867*0ade2cf0Sdg199075 			/*
868*0ade2cf0Sdg199075 			 * Don't gzip the old log file yet -
869*0ade2cf0Sdg199075 			 * it takes too long. Just remember that we
870*0ade2cf0Sdg199075 			 * need to gzip.
871*0ade2cf0Sdg199075 			 */
872*0ade2cf0Sdg199075 			Gzipnames = lut_add(Gzipnames, fn_s(newfile), "1");
8737c478bd9Sstevel@tonic-gate 			fn_puts(newfile, ".gz");
8747c478bd9Sstevel@tonic-gate 		}
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	/* first time through, gather interesting info for caller */
8787c478bd9Sstevel@tonic-gate 	if (n == 0)
8797c478bd9Sstevel@tonic-gate 		fn_renew(recentlog, fn_s(newfile));
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate /* expire phase of logname processing */
8837c478bd9Sstevel@tonic-gate static void
8847c478bd9Sstevel@tonic-gate expirefiles(struct fn *fnp, struct opts *opts)
8857c478bd9Sstevel@tonic-gate {
8867c478bd9Sstevel@tonic-gate 	char *fname = fn_s(fnp);
8877c478bd9Sstevel@tonic-gate 	struct fn *template;
8887c478bd9Sstevel@tonic-gate 	struct fn *pattern;
8897c478bd9Sstevel@tonic-gate 	struct fn_list *files;
8907c478bd9Sstevel@tonic-gate 	struct fn *nextfnp;
8917c478bd9Sstevel@tonic-gate 	int count;
8927c478bd9Sstevel@tonic-gate 	size_t size;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	if (Debug)
8957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "expirefiles: fname <%s>\n", fname);
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	/* return if no potential expire conditions */
8987c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "AS") == 0 && opts_optarg_int(opts, "C") == 0)
8997c478bd9Sstevel@tonic-gate 		return;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	kw_init(fnp, NULL);
9027c478bd9Sstevel@tonic-gate 	if (Debug > 1) {
9037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "expirefiles keywords:\n");
9047c478bd9Sstevel@tonic-gate 		kw_print(stderr);
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	/* see if pattern was supplied by user */
9087c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "T")) {
9097c478bd9Sstevel@tonic-gate 		template = fn_new(opts_optarg(opts, "T"));
9107c478bd9Sstevel@tonic-gate 		pattern = glob_to_reglob(template);
9117c478bd9Sstevel@tonic-gate 	} else {
9127c478bd9Sstevel@tonic-gate 		/* nope, generate pattern based on rotation template */
9137c478bd9Sstevel@tonic-gate 		template = fn_new(opts_optarg(opts, "t"));
9147c478bd9Sstevel@tonic-gate 		pattern = fn_new(NULL);
9157c478bd9Sstevel@tonic-gate 		(void) kw_expand(template, pattern, -1,
9167c478bd9Sstevel@tonic-gate 		    opts_count(opts, "z") != 0);
9177c478bd9Sstevel@tonic-gate 	}
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	/* match all old log files (hopefully not any others as well!) */
9207c478bd9Sstevel@tonic-gate 	files = glob_reglob(pattern);
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	if (Debug) {
9237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "expirefiles: pattern <%s>\n",
9247c478bd9Sstevel@tonic-gate 		    fn_s(pattern));
9257c478bd9Sstevel@tonic-gate 		fn_list_rewind(files);
9267c478bd9Sstevel@tonic-gate 		while ((nextfnp = fn_list_next(files)) != NULL)
9277c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "    <%s>\n", fn_s(nextfnp));
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	/* see if count causes expiration */
9317c478bd9Sstevel@tonic-gate 	if ((count = opts_optarg_int(opts, "C")) > 0) {
9327c478bd9Sstevel@tonic-gate 		int needexpire = fn_list_count(files) - count;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 		if (Debug)
9357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "expirefiles: needexpire %d\n",
9367c478bd9Sstevel@tonic-gate 			    needexpire);
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 		while (needexpire > 0 &&
9397c478bd9Sstevel@tonic-gate 		    ((nextfnp = fn_list_popoldest(files)) != NULL)) {
9407c478bd9Sstevel@tonic-gate 			dorm(opts, "expire by count rule", nextfnp);
9417c478bd9Sstevel@tonic-gate 			fn_free(nextfnp);
9427c478bd9Sstevel@tonic-gate 			needexpire--;
9437c478bd9Sstevel@tonic-gate 		}
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	/* see if total size causes expiration */
9477c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "S") && (size = opts_optarg_int(opts, "S")) > 0) {
9487c478bd9Sstevel@tonic-gate 		while (fn_list_totalsize(files) > size &&
9497c478bd9Sstevel@tonic-gate 		    ((nextfnp = fn_list_popoldest(files)) != NULL)) {
9507c478bd9Sstevel@tonic-gate 				dorm(opts, "expire by size rule", nextfnp);
9517c478bd9Sstevel@tonic-gate 				fn_free(nextfnp);
9527c478bd9Sstevel@tonic-gate 		}
9537c478bd9Sstevel@tonic-gate 	}
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	/* see if age causes expiration */
9567c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "A")) {
9577c478bd9Sstevel@tonic-gate 		int mtime = (int)time(0) - opts_optarg_int(opts, "A");
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 		while ((nextfnp = fn_list_popoldest(files)) != NULL)
9607c478bd9Sstevel@tonic-gate 			if (fn_getstat(nextfnp)->st_mtime < mtime) {
9617c478bd9Sstevel@tonic-gate 				dorm(opts, "expire by age rule", nextfnp);
9627c478bd9Sstevel@tonic-gate 				fn_free(nextfnp);
9637c478bd9Sstevel@tonic-gate 			} else {
9647c478bd9Sstevel@tonic-gate 				fn_free(nextfnp);
9657c478bd9Sstevel@tonic-gate 				break;
9667c478bd9Sstevel@tonic-gate 			}
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	fn_free(template);
9707c478bd9Sstevel@tonic-gate 	fn_list_free(files);
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate /* execute a command to remove an expired log file */
9747c478bd9Sstevel@tonic-gate static void
9757c478bd9Sstevel@tonic-gate dorm(struct opts *opts, const char *msg, struct fn *fnp)
9767c478bd9Sstevel@tonic-gate {
9777c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "E")) {
9787c478bd9Sstevel@tonic-gate 		struct fn *rawcmd = fn_new(opts_optarg(opts, "E"));
9797c478bd9Sstevel@tonic-gate 		struct fn *cmd = fn_new(NULL);
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 		/* user supplied cmd, expand $file */
9827c478bd9Sstevel@tonic-gate 		kw_init(fnp, NULL);
9837c478bd9Sstevel@tonic-gate 		(void) kw_expand(rawcmd, cmd, 0, B_FALSE);
9847c478bd9Sstevel@tonic-gate 		docmd(opts, msg, Sh, "-c", fn_s(cmd), NULL);
9857c478bd9Sstevel@tonic-gate 		fn_free(rawcmd);
9867c478bd9Sstevel@tonic-gate 		fn_free(cmd);
9877c478bd9Sstevel@tonic-gate 	} else
9887c478bd9Sstevel@tonic-gate 		docmd(opts, msg, Rm, "-f", fn_s(fnp), NULL);
989*0ade2cf0Sdg199075 	Gzipnames = lut_add(Gzipnames, fn_s(fnp), NULL);
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate /* execute a command, producing -n and -v output as necessary */
9937c478bd9Sstevel@tonic-gate static void
9947c478bd9Sstevel@tonic-gate docmd(struct opts *opts, const char *msg, const char *cmd,
9957c478bd9Sstevel@tonic-gate     const char *arg1, const char *arg2, const char *arg3)
9967c478bd9Sstevel@tonic-gate {
9977c478bd9Sstevel@tonic-gate 	int pid;
9987c478bd9Sstevel@tonic-gate 	int errpipe[2];
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	/* print info about command if necessary */
10017c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "vn")) {
10027c478bd9Sstevel@tonic-gate 		const char *simplecmd;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 		if ((simplecmd = strrchr(cmd, '/')) == NULL)
10057c478bd9Sstevel@tonic-gate 			simplecmd = cmd;
10067c478bd9Sstevel@tonic-gate 		else
10077c478bd9Sstevel@tonic-gate 			simplecmd++;
10087c478bd9Sstevel@tonic-gate 		(void) out("%s", simplecmd);
10097c478bd9Sstevel@tonic-gate 		if (arg1)
10107c478bd9Sstevel@tonic-gate 			(void) out(" %s", arg1);
10117c478bd9Sstevel@tonic-gate 		if (arg2)
10127c478bd9Sstevel@tonic-gate 			(void) out(" %s", arg2);
10137c478bd9Sstevel@tonic-gate 		if (arg3)
10147c478bd9Sstevel@tonic-gate 			(void) out(" %s", arg3);
10157c478bd9Sstevel@tonic-gate 		if (msg)
10167c478bd9Sstevel@tonic-gate 			(void) out(" # %s", msg);
10177c478bd9Sstevel@tonic-gate 		(void) out("\n");
10187c478bd9Sstevel@tonic-gate 	}
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	if (opts_count(opts, "n"))
10217c478bd9Sstevel@tonic-gate 		return;		/* -n means don't really do it */
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	/*
10247c478bd9Sstevel@tonic-gate 	 * run the cmd and see if it failed.  this function is *not* a
10257c478bd9Sstevel@tonic-gate 	 * generic command runner -- we depend on some knowledge we
10267c478bd9Sstevel@tonic-gate 	 * have about the commands we run.  first of all, we expect
10277c478bd9Sstevel@tonic-gate 	 * errors to spew something to stderr, and that something is
10287c478bd9Sstevel@tonic-gate 	 * typically short enough to fit into a pipe so we can wait()
10297c478bd9Sstevel@tonic-gate 	 * for the command to complete and then fetch the error text
10307c478bd9Sstevel@tonic-gate 	 * from the pipe.  we also expect the exit codes to make sense.
10317c478bd9Sstevel@tonic-gate 	 * notice also that we only allow a command name which is an
10327c478bd9Sstevel@tonic-gate 	 * absolute pathname, and two args must be supplied (the
10337c478bd9Sstevel@tonic-gate 	 * second may be NULL, or they may both be NULL).
10347c478bd9Sstevel@tonic-gate 	 */
10357c478bd9Sstevel@tonic-gate 	if (pipe(errpipe) < 0)
10367c478bd9Sstevel@tonic-gate 		err(EF_SYS, "pipe");
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	if ((pid = fork()) < 0)
10397c478bd9Sstevel@tonic-gate 		err(EF_SYS, "fork");
10407c478bd9Sstevel@tonic-gate 	else if (pid) {
10417c478bd9Sstevel@tonic-gate 		int wstat;
10427c478bd9Sstevel@tonic-gate 		int count;
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 		/* parent */
10457c478bd9Sstevel@tonic-gate 		(void) close(errpipe[1]);
10467c478bd9Sstevel@tonic-gate 		if (waitpid(pid, &wstat, 0) < 0)
10477c478bd9Sstevel@tonic-gate 			err(EF_SYS, "waitpid");
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 		/* check for stderr output */
10507c478bd9Sstevel@tonic-gate 		if (ioctl(errpipe[0], FIONREAD, &count) >= 0 && count) {
10517c478bd9Sstevel@tonic-gate 			err(EF_WARN, "command failed: %s%s%s%s%s%s%s",
10527c478bd9Sstevel@tonic-gate 				cmd,
10537c478bd9Sstevel@tonic-gate 				(arg1) ? " " : "",
10547c478bd9Sstevel@tonic-gate 				(arg1) ? arg1 : "",
10557c478bd9Sstevel@tonic-gate 				(arg2) ? " " : "",
10567c478bd9Sstevel@tonic-gate 				(arg2) ? arg2 : "",
10577c478bd9Sstevel@tonic-gate 				(arg3) ? " " : "",
10587c478bd9Sstevel@tonic-gate 				(arg3) ? arg3 : "");
10597c478bd9Sstevel@tonic-gate 			err_fromfd(errpipe[0]);
10607c478bd9Sstevel@tonic-gate 		} else if (WIFSIGNALED(wstat))
10617c478bd9Sstevel@tonic-gate 			err(EF_WARN,
10627c478bd9Sstevel@tonic-gate 			    "command died, signal %d: %s%s%s%s%s%s%s",
10637c478bd9Sstevel@tonic-gate 				WTERMSIG(wstat),
10647c478bd9Sstevel@tonic-gate 				cmd,
10657c478bd9Sstevel@tonic-gate 				(arg1) ? " " : "",
10667c478bd9Sstevel@tonic-gate 				(arg1) ? arg1 : "",
10677c478bd9Sstevel@tonic-gate 				(arg2) ? " " : "",
10687c478bd9Sstevel@tonic-gate 				(arg2) ? arg2 : "",
10697c478bd9Sstevel@tonic-gate 				(arg3) ? " " : "",
10707c478bd9Sstevel@tonic-gate 				(arg3) ? arg3 : "");
10717c478bd9Sstevel@tonic-gate 		else if (WIFEXITED(wstat) && WEXITSTATUS(wstat))
10727c478bd9Sstevel@tonic-gate 			err(EF_WARN,
10737c478bd9Sstevel@tonic-gate 			    "command error, exit %d: %s%s%s%s%s%s%s",
10747c478bd9Sstevel@tonic-gate 				WEXITSTATUS(wstat),
10757c478bd9Sstevel@tonic-gate 				cmd,
10767c478bd9Sstevel@tonic-gate 				(arg1) ? " " : "",
10777c478bd9Sstevel@tonic-gate 				(arg1) ? arg1 : "",
10787c478bd9Sstevel@tonic-gate 				(arg2) ? " " : "",
10797c478bd9Sstevel@tonic-gate 				(arg2) ? arg2 : "",
10807c478bd9Sstevel@tonic-gate 				(arg3) ? " " : "",
10817c478bd9Sstevel@tonic-gate 				(arg3) ? arg3 : "");
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 		(void) close(errpipe[0]);
10847c478bd9Sstevel@tonic-gate 	} else {
10857c478bd9Sstevel@tonic-gate 		/* child */
10867c478bd9Sstevel@tonic-gate 		(void) dup2(errpipe[1], fileno(stderr));
10877c478bd9Sstevel@tonic-gate 		(void) close(errpipe[0]);
10887c478bd9Sstevel@tonic-gate 		(void) execl(cmd, cmd, arg1, arg2, arg3, 0);
10897c478bd9Sstevel@tonic-gate 		perror(cmd);
10907c478bd9Sstevel@tonic-gate 		_exit(1);
10917c478bd9Sstevel@tonic-gate 	}
10927c478bd9Sstevel@tonic-gate }
1093