10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51913Sdg199075 * Common Development and Distribution License (the "License"). 61913Sdg199075 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 221913Sdg199075 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate * 250Sstevel@tonic-gate * logadm/main.c -- main routines for logadm 260Sstevel@tonic-gate * 270Sstevel@tonic-gate * this program is 90% argument processing, 10% actions... 280Sstevel@tonic-gate */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <stdio.h> 330Sstevel@tonic-gate #include <stdlib.h> 340Sstevel@tonic-gate #include <unistd.h> 350Sstevel@tonic-gate #include <strings.h> 360Sstevel@tonic-gate #include <libintl.h> 370Sstevel@tonic-gate #include <locale.h> 380Sstevel@tonic-gate #include <sys/types.h> 390Sstevel@tonic-gate #include <sys/stat.h> 400Sstevel@tonic-gate #include <sys/wait.h> 410Sstevel@tonic-gate #include <sys/filio.h> 420Sstevel@tonic-gate #include <time.h> 430Sstevel@tonic-gate #include "err.h" 440Sstevel@tonic-gate #include "lut.h" 450Sstevel@tonic-gate #include "fn.h" 460Sstevel@tonic-gate #include "opts.h" 470Sstevel@tonic-gate #include "conf.h" 480Sstevel@tonic-gate #include "glob.h" 490Sstevel@tonic-gate #include "kw.h" 500Sstevel@tonic-gate 510Sstevel@tonic-gate /* forward declarations for functions in this file */ 520Sstevel@tonic-gate static void usage(const char *msg); 530Sstevel@tonic-gate static void commajoin(const char *lhs, void *rhs, void *arg); 540Sstevel@tonic-gate static void doaftercmd(const char *lhs, void *rhs, void *arg); 550Sstevel@tonic-gate static void dologname(struct fn *fnp, struct opts *clopts); 560Sstevel@tonic-gate static boolean_t rotatelog(struct fn *fnp, struct opts *opts); 570Sstevel@tonic-gate static void rotateto(struct fn *fnp, struct opts *opts, int n, 580Sstevel@tonic-gate struct fn *recentlog, boolean_t isgz); 591913Sdg199075 static void do_delayed_gzip(const char *lhs, void *rhs, void *arg); 600Sstevel@tonic-gate static void expirefiles(struct fn *fnp, struct opts *opts); 610Sstevel@tonic-gate static void dorm(struct opts *opts, const char *msg, struct fn *fnp); 620Sstevel@tonic-gate static void docmd(struct opts *opts, const char *msg, const char *cmd, 630Sstevel@tonic-gate const char *arg1, const char *arg2, const char *arg3); 640Sstevel@tonic-gate 650Sstevel@tonic-gate /* our configuration file, unless otherwise specified by -f */ 660Sstevel@tonic-gate static char *Default_conffile = "/etc/logadm.conf"; 670Sstevel@tonic-gate 680Sstevel@tonic-gate /* default pathnames to the commands we invoke */ 690Sstevel@tonic-gate static char *Sh = "/bin/sh"; 700Sstevel@tonic-gate static char *Mv = "/bin/mv"; 710Sstevel@tonic-gate static char *Cp = "/bin/cp"; 720Sstevel@tonic-gate static char *Rm = "/bin/rm"; 730Sstevel@tonic-gate static char *Touch = "/bin/touch"; 740Sstevel@tonic-gate static char *Chmod = "/bin/chmod"; 750Sstevel@tonic-gate static char *Chown = "/bin/chown"; 760Sstevel@tonic-gate static char *Gzip = "/bin/gzip"; 770Sstevel@tonic-gate static char *Mkdir = "/bin/mkdir"; 780Sstevel@tonic-gate 790Sstevel@tonic-gate /* return from time(0), gathered early on to avoid slewed timestamps */ 800Sstevel@tonic-gate time_t Now; 810Sstevel@tonic-gate 820Sstevel@tonic-gate /* list of before commands that have been executed */ 830Sstevel@tonic-gate static struct lut *Beforecmds; 840Sstevel@tonic-gate 850Sstevel@tonic-gate /* list of after commands to execute before exiting */ 860Sstevel@tonic-gate static struct lut *Aftercmds; 870Sstevel@tonic-gate 880Sstevel@tonic-gate /* list of conffile entry names that are considered "done" */ 890Sstevel@tonic-gate static struct lut *Donenames; 900Sstevel@tonic-gate 911913Sdg199075 /* A list of names of files to be gzipped */ 921913Sdg199075 static struct lut *Gzipnames = NULL; 931913Sdg199075 940Sstevel@tonic-gate /* table that drives argument parsing */ 950Sstevel@tonic-gate static struct optinfo Opttable[] = { 960Sstevel@tonic-gate { "e", OPTTYPE_STRING, NULL, OPTF_CLI|OPTF_CONF }, 970Sstevel@tonic-gate { "f", OPTTYPE_STRING, NULL, OPTF_CLI }, 980Sstevel@tonic-gate { "h", OPTTYPE_BOOLEAN, NULL, OPTF_CLI }, 99954Sgm149974 { "l", OPTTYPE_BOOLEAN, NULL, OPTF_CLI }, 1000Sstevel@tonic-gate { "N", OPTTYPE_BOOLEAN, NULL, OPTF_CLI|OPTF_CONF }, 1010Sstevel@tonic-gate { "n", OPTTYPE_BOOLEAN, NULL, OPTF_CLI }, 1020Sstevel@tonic-gate { "r", OPTTYPE_BOOLEAN, NULL, OPTF_CLI }, 1030Sstevel@tonic-gate { "V", OPTTYPE_BOOLEAN, NULL, OPTF_CLI }, 1040Sstevel@tonic-gate { "v", OPTTYPE_BOOLEAN, NULL, OPTF_CLI }, 1050Sstevel@tonic-gate { "w", OPTTYPE_STRING, NULL, OPTF_CLI }, 1060Sstevel@tonic-gate { "p", OPTTYPE_INT, opts_parse_seconds, OPTF_CLI|OPTF_CONF }, 1070Sstevel@tonic-gate { "P", OPTTYPE_INT, opts_parse_ctime, OPTF_CLI|OPTF_CONF }, 1080Sstevel@tonic-gate { "s", OPTTYPE_INT, opts_parse_bytes, OPTF_CLI|OPTF_CONF }, 1090Sstevel@tonic-gate { "a", OPTTYPE_STRING, NULL, OPTF_CLI|OPTF_CONF }, 1100Sstevel@tonic-gate { "b", OPTTYPE_STRING, NULL, OPTF_CLI|OPTF_CONF }, 1110Sstevel@tonic-gate { "c", OPTTYPE_BOOLEAN, NULL, OPTF_CLI|OPTF_CONF }, 1120Sstevel@tonic-gate { "g", OPTTYPE_STRING, NULL, OPTF_CLI|OPTF_CONF }, 1130Sstevel@tonic-gate { "m", OPTTYPE_INT, opts_parse_atopi, OPTF_CLI|OPTF_CONF }, 1140Sstevel@tonic-gate { "M", OPTTYPE_STRING, NULL, OPTF_CLI|OPTF_CONF }, 1150Sstevel@tonic-gate { "o", OPTTYPE_STRING, NULL, OPTF_CLI|OPTF_CONF }, 1160Sstevel@tonic-gate { "R", OPTTYPE_STRING, NULL, OPTF_CLI|OPTF_CONF }, 1170Sstevel@tonic-gate { "t", OPTTYPE_STRING, NULL, OPTF_CLI|OPTF_CONF }, 1180Sstevel@tonic-gate { "z", OPTTYPE_INT, opts_parse_atopi, OPTF_CLI|OPTF_CONF }, 1190Sstevel@tonic-gate { "A", OPTTYPE_INT, opts_parse_seconds, OPTF_CLI|OPTF_CONF }, 1200Sstevel@tonic-gate { "C", OPTTYPE_INT, opts_parse_atopi, OPTF_CLI|OPTF_CONF }, 1210Sstevel@tonic-gate { "E", OPTTYPE_STRING, NULL, OPTF_CLI|OPTF_CONF }, 1220Sstevel@tonic-gate { "S", OPTTYPE_INT, opts_parse_bytes, OPTF_CLI|OPTF_CONF }, 1230Sstevel@tonic-gate { "T", OPTTYPE_STRING, NULL, OPTF_CLI|OPTF_CONF }, 1240Sstevel@tonic-gate }; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate /* 1270Sstevel@tonic-gate * only the "fhnVv" options are allowed in the first form of this command, 1280Sstevel@tonic-gate * so this defines the list of options that are an error in they appear 1290Sstevel@tonic-gate * in the first form. In other words, it is not allowed to run logadm 1300Sstevel@tonic-gate * with any of these options unless at least one logname is also provided. 1310Sstevel@tonic-gate */ 132954Sgm149974 #define OPTIONS_NOT_FIRST_FORM "eNrwpPsabcglmoRtzACEST" 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate /* text that we spew with the -h flag */ 1350Sstevel@tonic-gate #define HELP1 \ 1360Sstevel@tonic-gate "Usage: logadm [options]\n"\ 1370Sstevel@tonic-gate " (processes all entries in /etc/logadm.conf or conffile given by -f)\n"\ 1380Sstevel@tonic-gate " or: logadm [options] logname...\n"\ 1390Sstevel@tonic-gate " (processes the given lognames)\n"\ 1400Sstevel@tonic-gate "\n"\ 1410Sstevel@tonic-gate "General options:\n"\ 1420Sstevel@tonic-gate " -e mailaddr mail errors to given address\n"\ 1430Sstevel@tonic-gate " -f conffile use conffile instead of /etc/logadm.conf\n"\ 1440Sstevel@tonic-gate " -h display help\n"\ 1450Sstevel@tonic-gate " -N not an error if log file nonexistent\n"\ 1460Sstevel@tonic-gate " -n show actions, don't perform them\n"\ 1470Sstevel@tonic-gate " -r remove logname entry from conffile\n"\ 1480Sstevel@tonic-gate " -V ensure conffile entries exist, correct\n"\ 1490Sstevel@tonic-gate " -v print info about actions happening\n"\ 1500Sstevel@tonic-gate " -w entryname write entry to config file\n"\ 1510Sstevel@tonic-gate "\n"\ 1520Sstevel@tonic-gate "Options which control when a logfile is rotated:\n"\ 1530Sstevel@tonic-gate "(default is: -s1b -p1w if no -s or -p)\n"\ 1540Sstevel@tonic-gate " -p period only rotate if period passed since last rotate\n"\ 1550Sstevel@tonic-gate " -P timestamp used to store rotation date in conffile\n"\ 1560Sstevel@tonic-gate " -s size only rotate if given size or greater\n"\ 1570Sstevel@tonic-gate "\n" 1580Sstevel@tonic-gate #define HELP2 \ 1590Sstevel@tonic-gate "Options which control how a logfile is rotated:\n"\ 1600Sstevel@tonic-gate "(default is: -t '$file.$n', owner/group/mode taken from log file)\n"\ 1610Sstevel@tonic-gate " -a cmd execute cmd after taking actions\n"\ 1620Sstevel@tonic-gate " -b cmd execute cmd before taking actions\n"\ 1630Sstevel@tonic-gate " -c copy & truncate logfile, don't rename\n"\ 1640Sstevel@tonic-gate " -g group new empty log file group\n"\ 165954Sgm149974 " -l rotate log file with local time rather than UTC\n"\ 1660Sstevel@tonic-gate " -m mode new empty log file mode\n"\ 1670Sstevel@tonic-gate " -M cmd execute cmd to rotate the log file\n"\ 1680Sstevel@tonic-gate " -o owner new empty log file owner\n"\ 1690Sstevel@tonic-gate " -R cmd run cmd on file after rotate\n"\ 1700Sstevel@tonic-gate " -t template template for naming old logs\n"\ 1710Sstevel@tonic-gate " -z count gzip old logs except most recent count\n"\ 1720Sstevel@tonic-gate "\n"\ 1730Sstevel@tonic-gate "Options which control the expiration of old logfiles:\n"\ 1740Sstevel@tonic-gate "(default is: -C10 if no -A, -C, or -S)\n"\ 1750Sstevel@tonic-gate " -A age expire logs older than age\n"\ 1760Sstevel@tonic-gate " -C count expire old logs until count remain\n"\ 1770Sstevel@tonic-gate " -E cmd run cmd on file to expire\n"\ 1780Sstevel@tonic-gate " -S size expire until space used is below size \n"\ 1790Sstevel@tonic-gate " -T pattern pattern for finding old logs\n" 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate /* 1820Sstevel@tonic-gate * main -- where it all begins 1830Sstevel@tonic-gate */ 1840Sstevel@tonic-gate /*ARGSUSED*/ 1850Sstevel@tonic-gate int 1860Sstevel@tonic-gate main(int argc, char *argv[]) 1870Sstevel@tonic-gate { 1880Sstevel@tonic-gate struct opts *clopts; /* from parsing command line */ 1890Sstevel@tonic-gate const char *conffile; /* our configuration file */ 1900Sstevel@tonic-gate struct fn_list *lognames; /* list of lognames we're processing */ 1910Sstevel@tonic-gate struct fn *fnp; 1920Sstevel@tonic-gate char *val; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 1970Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* only used if Makefiles don't define it */ 1980Sstevel@tonic-gate #endif 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate /* we only print times into the conffile, so make them uniform */ 2030Sstevel@tonic-gate (void) setlocale(LC_TIME, "C"); 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate /* give our name to error routines & skip it for arg parsing */ 2060Sstevel@tonic-gate err_init(*argv++); 2070Sstevel@tonic-gate (void) setlinebuf(stdout); 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate if (putenv("PATH=/bin")) 2100Sstevel@tonic-gate err(EF_SYS, "putenv PATH"); 2110Sstevel@tonic-gate if (putenv("TZ=UTC")) 2120Sstevel@tonic-gate err(EF_SYS, "putenv TZ"); 2130Sstevel@tonic-gate tzset(); 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate (void) umask(0); 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate Now = time(0); 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* check for (undocumented) debugging environment variables */ 2200Sstevel@tonic-gate if (val = getenv("_LOGADM_DEFAULT_CONFFILE")) 2210Sstevel@tonic-gate Default_conffile = val; 2220Sstevel@tonic-gate if (val = getenv("_LOGADM_DEBUG")) 2230Sstevel@tonic-gate Debug = atoi(val); 2240Sstevel@tonic-gate if (val = getenv("_LOGADM_SH")) 2250Sstevel@tonic-gate Sh = val; 2260Sstevel@tonic-gate if (val = getenv("_LOGADM_MV")) 2270Sstevel@tonic-gate Mv = val; 2280Sstevel@tonic-gate if (val = getenv("_LOGADM_CP")) 2290Sstevel@tonic-gate Cp = val; 2300Sstevel@tonic-gate if (val = getenv("_LOGADM_RM")) 2310Sstevel@tonic-gate Rm = val; 2320Sstevel@tonic-gate if (val = getenv("_LOGADM_TOUCH")) 2330Sstevel@tonic-gate Touch = val; 2340Sstevel@tonic-gate if (val = getenv("_LOGADM_CHMOD")) 2350Sstevel@tonic-gate Chmod = val; 2360Sstevel@tonic-gate if (val = getenv("_LOGADM_CHOWN")) 2370Sstevel@tonic-gate Chown = val; 2380Sstevel@tonic-gate if (val = getenv("_LOGADM_GZIP")) 2390Sstevel@tonic-gate Gzip = val; 2400Sstevel@tonic-gate if (val = getenv("_LOGADM_MKDIR")) 2410Sstevel@tonic-gate Mkdir = val; 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate opts_init(Opttable, sizeof (Opttable) / sizeof (struct optinfo)); 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /* parse command line arguments */ 2460Sstevel@tonic-gate if (SETJMP) 2470Sstevel@tonic-gate usage("bailing out due to command line errors"); 2480Sstevel@tonic-gate else 2490Sstevel@tonic-gate clopts = opts_parse(argv, OPTF_CLI); 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate if (Debug) { 2520Sstevel@tonic-gate (void) fprintf(stderr, "command line opts:"); 2530Sstevel@tonic-gate opts_print(clopts, stderr, NULL); 2540Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate /* 2580Sstevel@tonic-gate * There are many moods of logadm: 2590Sstevel@tonic-gate * 2600Sstevel@tonic-gate * 1. "-h" for help was given. We spew a canned help 2610Sstevel@tonic-gate * message and exit, regardless of any other options given. 2620Sstevel@tonic-gate * 2630Sstevel@tonic-gate * 2. "-r" or "-w" asking us to write to the conffile. Lots 2640Sstevel@tonic-gate * of argument checking, then we make the change to conffile 2650Sstevel@tonic-gate * and exit. (-r processing actually happens in dologname().) 2660Sstevel@tonic-gate * 2670Sstevel@tonic-gate * 3. "-V" to search/verify the conffile was given. We do 2680Sstevel@tonic-gate * the appropriate run through the conffile and exit. 2690Sstevel@tonic-gate * (-V processing actually happens in dologname().) 2700Sstevel@tonic-gate * 2710Sstevel@tonic-gate * 4. No lognames were given, so we're being asked to go through 2720Sstevel@tonic-gate * every entry in conffile. We verify that only the options 2730Sstevel@tonic-gate * that make sense for this form of the command are present 2740Sstevel@tonic-gate * and fall into the main processing loop below. 2750Sstevel@tonic-gate * 2760Sstevel@tonic-gate * 5. lognames were given, so we fall into the main processing 2770Sstevel@tonic-gate * loop below to work our way through them. 2780Sstevel@tonic-gate * 2790Sstevel@tonic-gate * The last two cases are where the option processing gets more 2800Sstevel@tonic-gate * complex. Each time around the main processing loop, we're 2810Sstevel@tonic-gate * in one of these cases: 2820Sstevel@tonic-gate * 2830Sstevel@tonic-gate * A. No cmdargs were found (we're in case 4), the entry 2840Sstevel@tonic-gate * in conffile supplies no log file names, so the entry 2850Sstevel@tonic-gate * name itself is the logfile name (or names, if it globs 2860Sstevel@tonic-gate * to multiple file names). 2870Sstevel@tonic-gate * 2880Sstevel@tonic-gate * B. No cmdargs were found (we're in case 4), the entry 2890Sstevel@tonic-gate * in conffile gives log file names that we then loop 2900Sstevel@tonic-gate * through and rotate/expire. In this case, the entry 2910Sstevel@tonic-gate * name is specifically NOT one of the log file names. 2920Sstevel@tonic-gate * 2930Sstevel@tonic-gate * C. We're going through the cmdargs (we're in case 5), 2940Sstevel@tonic-gate * the entry in conffile either doesn't exist or it exists 2950Sstevel@tonic-gate * but supplies no log file names, so the cmdarg itself 2960Sstevel@tonic-gate * is the log file name. 2970Sstevel@tonic-gate * 2980Sstevel@tonic-gate * D. We're going through the cmdargs (we're in case 5), 2990Sstevel@tonic-gate * a matching entry in conffile supplies log file names 3000Sstevel@tonic-gate * that we then loop through and rotate/expire. In this 3010Sstevel@tonic-gate * case the entry name is specifically NOT one of the log 3020Sstevel@tonic-gate * file names. 3030Sstevel@tonic-gate * 3040Sstevel@tonic-gate * As we're doing all this, any options given on the command line 3050Sstevel@tonic-gate * override any found in the conffile, and we apply the defaults 3060Sstevel@tonic-gate * for rotation conditions and expiration conditions, etc. at the 3070Sstevel@tonic-gate * last opportunity, when we're sure they haven't been overridden 3080Sstevel@tonic-gate * by an option somewhere along the way. 3090Sstevel@tonic-gate * 3100Sstevel@tonic-gate */ 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate /* help option overrides anything else */ 3130Sstevel@tonic-gate if (opts_count(clopts, "h")) { 3140Sstevel@tonic-gate (void) fputs(HELP1, stderr); 3150Sstevel@tonic-gate (void) fputs(HELP2, stderr); 3160Sstevel@tonic-gate err_done(0); 3170Sstevel@tonic-gate /*NOTREACHED*/ 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate /* detect illegal option combinations */ 3210Sstevel@tonic-gate if (opts_count(clopts, "rwV") > 1) 3220Sstevel@tonic-gate usage("Only one of -r, -w, or -V may be used at a time."); 3230Sstevel@tonic-gate if (opts_count(clopts, "cM") > 1) 3240Sstevel@tonic-gate usage("Only one of -c or -M may be used at a time."); 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate /* arrange for error output to be mailed if clopts includes -e */ 3270Sstevel@tonic-gate if (opts_count(clopts, "e")) 3280Sstevel@tonic-gate err_mailto(opts_optarg(clopts, "e")); 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate /* this implements the default conffile */ 3310Sstevel@tonic-gate if ((conffile = opts_optarg(clopts, "f")) == NULL) 3320Sstevel@tonic-gate conffile = Default_conffile; 3330Sstevel@tonic-gate if (opts_count(clopts, "v")) 3340Sstevel@tonic-gate (void) out("# loading %s\n", conffile); 3350Sstevel@tonic-gate conf_open(conffile, opts_count(clopts, "Vn") == 0); 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate /* handle conffile write option */ 3380Sstevel@tonic-gate if (opts_count(clopts, "w")) { 3390Sstevel@tonic-gate if (Debug) 3400Sstevel@tonic-gate (void) fprintf(stderr, 3410Sstevel@tonic-gate "main: add/replace conffile entry: <%s>\n", 3420Sstevel@tonic-gate opts_optarg(clopts, "w")); 3430Sstevel@tonic-gate conf_replace(opts_optarg(clopts, "w"), clopts); 3440Sstevel@tonic-gate conf_close(clopts); 3450Sstevel@tonic-gate err_done(0); 3460Sstevel@tonic-gate /*NOTREACHED*/ 3470Sstevel@tonic-gate } 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate /* 3500Sstevel@tonic-gate * lognames is either a list supplied on the command line, 3510Sstevel@tonic-gate * or every entry in the conffile if none were supplied. 3520Sstevel@tonic-gate */ 3530Sstevel@tonic-gate lognames = opts_cmdargs(clopts); 3540Sstevel@tonic-gate if (fn_list_empty(lognames)) { 3550Sstevel@tonic-gate /* 3560Sstevel@tonic-gate * being asked to do all entries in conffile 3570Sstevel@tonic-gate * 3580Sstevel@tonic-gate * check to see if any options were given that only 3590Sstevel@tonic-gate * make sense when lognames are given specifically 3600Sstevel@tonic-gate * on the command line. 3610Sstevel@tonic-gate */ 3620Sstevel@tonic-gate if (opts_count(clopts, OPTIONS_NOT_FIRST_FORM)) 3630Sstevel@tonic-gate usage("some options require logname argument"); 3640Sstevel@tonic-gate if (Debug) 3650Sstevel@tonic-gate (void) fprintf(stderr, 3660Sstevel@tonic-gate "main: run all entries in conffile\n"); 3670Sstevel@tonic-gate lognames = conf_entries(); 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate /* foreach logname... */ 3710Sstevel@tonic-gate fn_list_rewind(lognames); 3720Sstevel@tonic-gate while ((fnp = fn_list_next(lognames)) != NULL) { 3730Sstevel@tonic-gate if (lut_lookup(Donenames, fn_s(fnp)) != NULL) { 3740Sstevel@tonic-gate if (Debug) 3750Sstevel@tonic-gate (void) fprintf(stderr, 3760Sstevel@tonic-gate "main: logname already done: <%s>\n", 3770Sstevel@tonic-gate fn_s(fnp)); 3780Sstevel@tonic-gate continue; 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate if (SETJMP) 3810Sstevel@tonic-gate err(EF_FILE, "bailing out on logname \"%s\" " 3820Sstevel@tonic-gate "due to errors", fn_s(fnp)); 3830Sstevel@tonic-gate else 3840Sstevel@tonic-gate dologname(fnp, clopts); 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate /* execute any after commands */ 3880Sstevel@tonic-gate lut_walk(Aftercmds, doaftercmd, clopts); 3890Sstevel@tonic-gate 3901913Sdg199075 /* execute any gzip commands */ 3911913Sdg199075 lut_walk(Gzipnames, do_delayed_gzip, clopts); 3921913Sdg199075 lut_free(Gzipnames, free); 3931913Sdg199075 Gzipnames = NULL; 3941913Sdg199075 3950Sstevel@tonic-gate /* write out any conffile changes */ 3960Sstevel@tonic-gate conf_close(clopts); 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate err_done(0); 3990Sstevel@tonic-gate /*NOTREACHED*/ 4000Sstevel@tonic-gate return (0); /* for lint's little mind */ 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate /* spew a message, then a usage message, then exit */ 4040Sstevel@tonic-gate static void 4050Sstevel@tonic-gate usage(const char *msg) 4060Sstevel@tonic-gate { 4070Sstevel@tonic-gate if (msg) 4080Sstevel@tonic-gate err(0, "%s\nUse \"logadm -h\" for help.", msg); 4090Sstevel@tonic-gate else 4100Sstevel@tonic-gate err(EF_RAW, "Use \"logadm -h\" for help.\n"); 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate /* helper function used by doaftercmd() to join mail addrs with commas */ 4140Sstevel@tonic-gate /*ARGSUSED1*/ 4150Sstevel@tonic-gate static void 4160Sstevel@tonic-gate commajoin(const char *lhs, void *rhs, void *arg) 4170Sstevel@tonic-gate { 4180Sstevel@tonic-gate struct fn *fnp = (struct fn *)arg; 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate if (*fn_s(fnp)) 4210Sstevel@tonic-gate fn_putc(fnp, ','); 4220Sstevel@tonic-gate fn_puts(fnp, lhs); 4230Sstevel@tonic-gate } 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate /* helper function used by main() to run "after" commands */ 4260Sstevel@tonic-gate static void 4270Sstevel@tonic-gate doaftercmd(const char *lhs, void *rhs, void *arg) 4280Sstevel@tonic-gate { 4290Sstevel@tonic-gate struct opts *opts = (struct opts *)arg; 4300Sstevel@tonic-gate struct lut *addrs = (struct lut *)rhs; 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate if (addrs) { 4330Sstevel@tonic-gate struct fn *fnp = fn_new(NULL); 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate /* 4360Sstevel@tonic-gate * addrs contains list of email addrs that should get 4370Sstevel@tonic-gate * the error output when this after command is executed. 4380Sstevel@tonic-gate */ 4390Sstevel@tonic-gate lut_walk(addrs, commajoin, fnp); 4400Sstevel@tonic-gate err_mailto(fn_s(fnp)); 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate docmd(opts, "-a cmd", Sh, "-c", lhs, NULL); 4440Sstevel@tonic-gate } 4450Sstevel@tonic-gate 4461913Sdg199075 /* perform delayed gzip */ 4471913Sdg199075 4481913Sdg199075 static void 4491913Sdg199075 do_delayed_gzip(const char *lhs, void *rhs, void *arg) 4501913Sdg199075 { 4511913Sdg199075 struct opts *opts = (struct opts *)arg; 4521913Sdg199075 4531913Sdg199075 if (rhs == NULL) { 4541913Sdg199075 if (Debug) { 4551913Sdg199075 (void) fprintf(stderr, "do_delayed_gzip: not gzipping " 4561913Sdg199075 "expired file <%s>\n", lhs); 4571913Sdg199075 } 4581913Sdg199075 return; 4591913Sdg199075 } 4601913Sdg199075 docmd(opts, "compress old log (-z flag)", Gzip, "-f", lhs, NULL); 4611913Sdg199075 } 4621913Sdg199075 4631913Sdg199075 4640Sstevel@tonic-gate /* main logname processing */ 4650Sstevel@tonic-gate static void 4660Sstevel@tonic-gate dologname(struct fn *fnp, struct opts *clopts) 4670Sstevel@tonic-gate { 4680Sstevel@tonic-gate const char *logname = fn_s(fnp); 4690Sstevel@tonic-gate struct opts *cfopts; 4700Sstevel@tonic-gate struct opts *allopts; 4710Sstevel@tonic-gate struct fn_list *logfiles; 4720Sstevel@tonic-gate struct fn_list *globbedfiles; 4730Sstevel@tonic-gate struct fn *nextfnp; 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate /* look up options set by config file */ 4760Sstevel@tonic-gate cfopts = conf_opts(logname); 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate if (opts_count(clopts, "v")) 4790Sstevel@tonic-gate (void) out("# processing logname: %s\n", logname); 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate if (Debug) { 4820Sstevel@tonic-gate (void) fprintf(stderr, "dologname: logname <%s>\n", logname); 4830Sstevel@tonic-gate (void) fprintf(stderr, "conffile opts:"); 4840Sstevel@tonic-gate opts_print(cfopts, stderr, NULL); 4850Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 4860Sstevel@tonic-gate } 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate /* handle conffile lookup option */ 4890Sstevel@tonic-gate if (opts_count(clopts, "V")) { 4900Sstevel@tonic-gate /* lookup an entry in conffile */ 4910Sstevel@tonic-gate if (Debug) 4920Sstevel@tonic-gate (void) fprintf(stderr, 4930Sstevel@tonic-gate "dologname: lookup conffile entry\n"); 4940Sstevel@tonic-gate if (conf_lookup(logname)) { 4950Sstevel@tonic-gate opts_printword(logname, stdout); 4960Sstevel@tonic-gate opts_print(cfopts, stdout, NULL); 4970Sstevel@tonic-gate (void) out("\n"); 4980Sstevel@tonic-gate } else 4990Sstevel@tonic-gate err_exitcode(1); 5000Sstevel@tonic-gate return; 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate /* handle conffile removal option */ 5040Sstevel@tonic-gate if (opts_count(clopts, "r")) { 5050Sstevel@tonic-gate if (Debug) 5060Sstevel@tonic-gate (void) fprintf(stderr, 5070Sstevel@tonic-gate "dologname: remove conffile entry\n"); 5080Sstevel@tonic-gate if (conf_lookup(logname)) 5090Sstevel@tonic-gate conf_replace(logname, NULL); 5100Sstevel@tonic-gate else 5110Sstevel@tonic-gate err_exitcode(1); 5120Sstevel@tonic-gate return; 5130Sstevel@tonic-gate } 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate /* generate combined options */ 5160Sstevel@tonic-gate allopts = opts_merge(cfopts, clopts); 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate /* arrange for error output to be mailed if allopts includes -e */ 5190Sstevel@tonic-gate if (opts_count(allopts, "e")) 5200Sstevel@tonic-gate err_mailto(opts_optarg(allopts, "e")); 5210Sstevel@tonic-gate else 5220Sstevel@tonic-gate err_mailto(NULL); 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate /* this implements the default rotation rules */ 5250Sstevel@tonic-gate if (opts_count(allopts, "sp") == 0) { 5260Sstevel@tonic-gate if (opts_count(clopts, "v")) 5270Sstevel@tonic-gate (void) out( 5280Sstevel@tonic-gate "# using default rotate rules: -s1b -p1w\n"); 5290Sstevel@tonic-gate (void) opts_set(allopts, "s", "1b"); 5300Sstevel@tonic-gate (void) opts_set(allopts, "p", "1w"); 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate /* this implements the default expiration rules */ 5340Sstevel@tonic-gate if (opts_count(allopts, "ACS") == 0) { 5350Sstevel@tonic-gate if (opts_count(clopts, "v")) 5360Sstevel@tonic-gate (void) out("# using default expire rule: -C10\n"); 5370Sstevel@tonic-gate (void) opts_set(allopts, "C", "10"); 5380Sstevel@tonic-gate } 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate /* this implements the default template */ 5410Sstevel@tonic-gate if (opts_count(allopts, "t") == 0) { 5420Sstevel@tonic-gate if (opts_count(clopts, "v")) 5430Sstevel@tonic-gate (void) out("# using default template: $file.$n\n"); 5440Sstevel@tonic-gate (void) opts_set(allopts, "t", "$file.$n"); 5450Sstevel@tonic-gate } 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate if (Debug) { 5480Sstevel@tonic-gate (void) fprintf(stderr, "merged opts:"); 5490Sstevel@tonic-gate opts_print(allopts, stderr, NULL); 5500Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 5510Sstevel@tonic-gate } 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate /* 5540Sstevel@tonic-gate * if the conffile entry supplied log file names, then 5550Sstevel@tonic-gate * logname is NOT one of the log file names (it was just 5560Sstevel@tonic-gate * the entry name in conffile). 5570Sstevel@tonic-gate */ 5580Sstevel@tonic-gate logfiles = opts_cmdargs(cfopts); 5590Sstevel@tonic-gate if (Debug) { 5600Sstevel@tonic-gate (void) fprintf(stderr, "dologname: logfiles from cfopts:\n"); 5610Sstevel@tonic-gate fn_list_rewind(logfiles); 5620Sstevel@tonic-gate while ((nextfnp = fn_list_next(logfiles)) != NULL) 5630Sstevel@tonic-gate (void) fprintf(stderr, " <%s>\n", fn_s(nextfnp)); 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate if (fn_list_empty(logfiles)) 5660Sstevel@tonic-gate globbedfiles = glob_glob(fnp); 5670Sstevel@tonic-gate else 5680Sstevel@tonic-gate globbedfiles = glob_glob_list(logfiles); 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate /* go through the list produced by glob expansion */ 5710Sstevel@tonic-gate fn_list_rewind(globbedfiles); 5720Sstevel@tonic-gate while ((nextfnp = fn_list_next(globbedfiles)) != NULL) 5730Sstevel@tonic-gate if (rotatelog(nextfnp, allopts)) 5740Sstevel@tonic-gate expirefiles(nextfnp, allopts); 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate fn_list_free(globbedfiles); 5770Sstevel@tonic-gate opts_free(allopts); 5780Sstevel@tonic-gate } 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate /* absurdly long buffer lengths for holding user/group/mode strings */ 5820Sstevel@tonic-gate #define TIMESTRMAX 100 5830Sstevel@tonic-gate #define MAXATTR 100 5840Sstevel@tonic-gate 5850Sstevel@tonic-gate /* rotate a log file if necessary, returns true if ok to go on to expire step */ 5860Sstevel@tonic-gate static boolean_t 5870Sstevel@tonic-gate rotatelog(struct fn *fnp, struct opts *opts) 5880Sstevel@tonic-gate { 5890Sstevel@tonic-gate char *fname = fn_s(fnp); 5900Sstevel@tonic-gate struct stat stbuf; 5910Sstevel@tonic-gate char nowstr[TIMESTRMAX]; 5920Sstevel@tonic-gate struct fn *recentlog = fn_new(NULL); /* for -R cmd */ 5930Sstevel@tonic-gate char ownerbuf[MAXATTR]; 5940Sstevel@tonic-gate char groupbuf[MAXATTR]; 5950Sstevel@tonic-gate char modebuf[MAXATTR]; 5960Sstevel@tonic-gate const char *owner; 5970Sstevel@tonic-gate const char *group; 5980Sstevel@tonic-gate const char *mode; 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate if (Debug) 6010Sstevel@tonic-gate (void) fprintf(stderr, "rotatelog: fname <%s>\n", fname); 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate if (opts_count(opts, "p") && opts_optarg_int(opts, "p") == OPTP_NEVER) 6040Sstevel@tonic-gate return (B_TRUE); /* "-p never" forced no rotate */ 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate /* prepare the keywords */ 6070Sstevel@tonic-gate kw_init(fnp, NULL); 6080Sstevel@tonic-gate if (Debug > 1) { 6090Sstevel@tonic-gate (void) fprintf(stderr, "rotatelog keywords:\n"); 6100Sstevel@tonic-gate kw_print(stderr); 6110Sstevel@tonic-gate } 6120Sstevel@tonic-gate 6130Sstevel@tonic-gate if (lstat(fname, &stbuf) < 0) { 6140Sstevel@tonic-gate if (opts_count(opts, "N")) 6150Sstevel@tonic-gate return (1); 6160Sstevel@tonic-gate err(EF_WARN|EF_SYS, "%s", fname); 6170Sstevel@tonic-gate return (B_FALSE); 6180Sstevel@tonic-gate } 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate if ((stbuf.st_mode & S_IFMT) == S_IFLNK) { 6210Sstevel@tonic-gate err(EF_WARN, "%s is a symlink", fname); 6220Sstevel@tonic-gate return (B_FALSE); 6230Sstevel@tonic-gate } 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate if ((stbuf.st_mode & S_IFMT) != S_IFREG) { 6260Sstevel@tonic-gate err(EF_WARN, "%s is not a regular file", fname); 6270Sstevel@tonic-gate return (B_FALSE); 6280Sstevel@tonic-gate } 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate /* see if size condition is present, and return if not met */ 6310Sstevel@tonic-gate if (opts_count(opts, "s") && stbuf.st_size < opts_optarg_int(opts, "s")) 6320Sstevel@tonic-gate return (B_TRUE); 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate /* see if age condition is present, and return if not met */ 6350Sstevel@tonic-gate if (opts_count(opts, "p")) { 636*2016Sbasabi off_t when = opts_optarg_int(opts, "p"); 6370Sstevel@tonic-gate struct opts *cfopts; 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate /* unless rotate forced by "-p now", see if period has passed */ 6400Sstevel@tonic-gate if (when != OPTP_NOW) { 6410Sstevel@tonic-gate /* 6420Sstevel@tonic-gate * "when" holds the number of seconds that must have 6430Sstevel@tonic-gate * passed since the last time this log was rotated. 6440Sstevel@tonic-gate * of course, running logadm can take a little time 6450Sstevel@tonic-gate * (typically a second or two, but longer if the 6460Sstevel@tonic-gate * conffile has lots of stuff in it) and that amount 6470Sstevel@tonic-gate * of time is variable, depending on system load, etc. 6480Sstevel@tonic-gate * so we want to allow a little "slop" in the value of 6490Sstevel@tonic-gate * "when". this way, if a log should be rotated every 6500Sstevel@tonic-gate * week, and the number of seconds passed is really a 6510Sstevel@tonic-gate * few seconds short of a week, we'll go ahead and 6520Sstevel@tonic-gate * rotate the log as expected. 6530Sstevel@tonic-gate * 6540Sstevel@tonic-gate */ 6550Sstevel@tonic-gate if (when >= 60 * 60) 6560Sstevel@tonic-gate when -= 59; 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate /* 6590Sstevel@tonic-gate * last rotation is recorded as argument to -P, 6600Sstevel@tonic-gate * but if logname isn't the same as log file name 6610Sstevel@tonic-gate * then the timestamp would be recorded on a 6620Sstevel@tonic-gate * separate line in the conf file. so if we 6630Sstevel@tonic-gate * haven't seen a -P already, we check to see if 6640Sstevel@tonic-gate * it is part of a specific entry for the log 6650Sstevel@tonic-gate * file name. this handles the case where the 6660Sstevel@tonic-gate * logname is "apache", it supplies a log file 6670Sstevel@tonic-gate * name like "/var/apache/logs/[a-z]*_log", 6680Sstevel@tonic-gate * which expands to multiple file names. if one 6690Sstevel@tonic-gate * of the file names is "/var/apache/logs/access_log" 6700Sstevel@tonic-gate * the the -P will be attached to a line with that 6710Sstevel@tonic-gate * logname in the conf file. 6720Sstevel@tonic-gate */ 6730Sstevel@tonic-gate if (opts_count(opts, "P")) { 674*2016Sbasabi off_t last = opts_optarg_int(opts, "P"); 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate /* return if not enough time has passed */ 6770Sstevel@tonic-gate if (Now - last < when) 6780Sstevel@tonic-gate return (B_TRUE); 6790Sstevel@tonic-gate } else if ((cfopts = conf_opts(fname)) != NULL && 6800Sstevel@tonic-gate opts_count(cfopts, "P")) { 681*2016Sbasabi off_t last = opts_optarg_int(cfopts, "P"); 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate /* 6840Sstevel@tonic-gate * just checking this means this entry 6850Sstevel@tonic-gate * is now "done" if we're going through 6860Sstevel@tonic-gate * the entire conffile 6870Sstevel@tonic-gate */ 6880Sstevel@tonic-gate Donenames = lut_add(Donenames, fname, "1"); 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate /* return if not enough time has passed */ 6910Sstevel@tonic-gate if (Now - last < when) 6920Sstevel@tonic-gate return (B_TRUE); 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate } 6950Sstevel@tonic-gate } 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate if (Debug) 6980Sstevel@tonic-gate (void) fprintf(stderr, "rotatelog: conditions met\n"); 699954Sgm149974 if (opts_count(opts, "l")) { 700954Sgm149974 /* Change the time zone to local time zone */ 701954Sgm149974 if (putenv("TZ=")) 702954Sgm149974 err(EF_SYS, "putenv TZ"); 703954Sgm149974 tzset(); 704954Sgm149974 Now = time(0); 7050Sstevel@tonic-gate 706954Sgm149974 /* rename the log file */ 707954Sgm149974 rotateto(fnp, opts, 0, recentlog, B_FALSE); 708954Sgm149974 709954Sgm149974 /* Change the time zone to UTC */ 710954Sgm149974 if (putenv("TZ=UTC")) 711954Sgm149974 err(EF_SYS, "putenv TZ"); 712954Sgm149974 tzset(); 713954Sgm149974 Now = time(0); 714954Sgm149974 } else { 715954Sgm149974 /* rename the log file */ 716954Sgm149974 rotateto(fnp, opts, 0, recentlog, B_FALSE); 717954Sgm149974 } 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate /* determine owner, group, mode for empty log file */ 7200Sstevel@tonic-gate if (opts_count(opts, "o")) 7210Sstevel@tonic-gate (void) strlcpy(ownerbuf, opts_optarg(opts, "o"), MAXATTR); 7220Sstevel@tonic-gate else { 7230Sstevel@tonic-gate (void) snprintf(ownerbuf, MAXATTR, "%ld", stbuf.st_uid); 7240Sstevel@tonic-gate } 7250Sstevel@tonic-gate owner = ownerbuf; 7260Sstevel@tonic-gate if (opts_count(opts, "g")) 7270Sstevel@tonic-gate group = opts_optarg(opts, "g"); 7280Sstevel@tonic-gate else { 7290Sstevel@tonic-gate (void) snprintf(groupbuf, MAXATTR, "%ld", stbuf.st_gid); 7300Sstevel@tonic-gate group = groupbuf; 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate (void) strlcat(ownerbuf, ":", MAXATTR - strlen(ownerbuf)); 7330Sstevel@tonic-gate (void) strlcat(ownerbuf, group, MAXATTR - strlen(ownerbuf)); 7340Sstevel@tonic-gate if (opts_count(opts, "m")) 7350Sstevel@tonic-gate mode = opts_optarg(opts, "m"); 7360Sstevel@tonic-gate else { 7370Sstevel@tonic-gate (void) snprintf(modebuf, MAXATTR, 7380Sstevel@tonic-gate "%03lo", stbuf.st_mode & 0777); 7390Sstevel@tonic-gate mode = modebuf; 7400Sstevel@tonic-gate } 7410Sstevel@tonic-gate 7420Sstevel@tonic-gate /* create the empty log file */ 7430Sstevel@tonic-gate docmd(opts, NULL, Touch, fname, NULL, NULL); 7440Sstevel@tonic-gate docmd(opts, NULL, Chown, owner, fname, NULL); 7450Sstevel@tonic-gate docmd(opts, NULL, Chmod, mode, fname, NULL); 7460Sstevel@tonic-gate 7470Sstevel@tonic-gate /* execute post-rotation command */ 7480Sstevel@tonic-gate if (opts_count(opts, "R")) { 7490Sstevel@tonic-gate struct fn *rawcmd = fn_new(opts_optarg(opts, "R")); 7500Sstevel@tonic-gate struct fn *cmd = fn_new(NULL); 7510Sstevel@tonic-gate 7520Sstevel@tonic-gate kw_init(recentlog, NULL); 7530Sstevel@tonic-gate (void) kw_expand(rawcmd, cmd, 0, B_FALSE); 7540Sstevel@tonic-gate docmd(opts, "-R cmd", Sh, "-c", fn_s(cmd), NULL); 7550Sstevel@tonic-gate fn_free(rawcmd); 7560Sstevel@tonic-gate fn_free(cmd); 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate fn_free(recentlog); 7590Sstevel@tonic-gate 7600Sstevel@tonic-gate /* 7610Sstevel@tonic-gate * add "after" command to list of after commands. we also record 7620Sstevel@tonic-gate * the email address, if any, where the error output of the after 7630Sstevel@tonic-gate * command should be sent. if the after command is already on 7640Sstevel@tonic-gate * our list, add the email addr to the list the email addrs for 7650Sstevel@tonic-gate * that command (the after command will only be executed once, 7660Sstevel@tonic-gate * so the error output gets mailed to every address we've come 7670Sstevel@tonic-gate * across associated with this command). 7680Sstevel@tonic-gate */ 7690Sstevel@tonic-gate if (opts_count(opts, "a")) { 7700Sstevel@tonic-gate const char *cmd = opts_optarg(opts, "a"); 7710Sstevel@tonic-gate struct lut *addrs = (struct lut *)lut_lookup(Aftercmds, cmd); 7720Sstevel@tonic-gate if (opts_count(opts, "e")) 7730Sstevel@tonic-gate addrs = lut_add(addrs, opts_optarg(opts, "e"), NULL); 7740Sstevel@tonic-gate Aftercmds = lut_add(Aftercmds, opts_optarg(opts, "a"), addrs); 7750Sstevel@tonic-gate } 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate /* record the rotation date */ 7780Sstevel@tonic-gate (void) strftime(nowstr, sizeof (nowstr), 7790Sstevel@tonic-gate "%a %b %e %T %Y", gmtime(&Now)); 7800Sstevel@tonic-gate if (opts_count(opts, "v")) 7810Sstevel@tonic-gate (void) out("# recording rotation date %s for %s\n", 7820Sstevel@tonic-gate nowstr, fname); 7830Sstevel@tonic-gate conf_set(fname, "P", STRDUP(nowstr)); 7840Sstevel@tonic-gate Donenames = lut_add(Donenames, fname, "1"); 7850Sstevel@tonic-gate return (B_TRUE); 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate 7880Sstevel@tonic-gate /* rotate files "up" according to current template */ 7890Sstevel@tonic-gate static void 7900Sstevel@tonic-gate rotateto(struct fn *fnp, struct opts *opts, int n, struct fn *recentlog, 7910Sstevel@tonic-gate boolean_t isgz) 7920Sstevel@tonic-gate { 7930Sstevel@tonic-gate struct fn *template = fn_new(opts_optarg(opts, "t")); 7940Sstevel@tonic-gate struct fn *newfile = fn_new(NULL); 7950Sstevel@tonic-gate struct fn *dirname; 7960Sstevel@tonic-gate int hasn; 7970Sstevel@tonic-gate struct stat stbuf; 7980Sstevel@tonic-gate 7990Sstevel@tonic-gate /* expand template to figure out new filename */ 8000Sstevel@tonic-gate hasn = kw_expand(template, newfile, n, isgz); 8010Sstevel@tonic-gate 8020Sstevel@tonic-gate if (Debug) 8030Sstevel@tonic-gate (void) fprintf(stderr, "rotateto: %s -> %s (%d)\n", fn_s(fnp), 8040Sstevel@tonic-gate fn_s(newfile), n); 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate /* if filename is there already, rotate "up" */ 8070Sstevel@tonic-gate if (hasn && lstat(fn_s(newfile), &stbuf) != -1) 8080Sstevel@tonic-gate rotateto(newfile, opts, n + 1, recentlog, isgz); 8090Sstevel@tonic-gate else if (hasn && opts_count(opts, "z")) { 8100Sstevel@tonic-gate struct fn *gzfnp = fn_dup(newfile); 8110Sstevel@tonic-gate /* 8120Sstevel@tonic-gate * since we're compressing old files, see if we 8130Sstevel@tonic-gate * about to rotate into one. 8140Sstevel@tonic-gate */ 8150Sstevel@tonic-gate fn_puts(gzfnp, ".gz"); 8160Sstevel@tonic-gate if (lstat(fn_s(gzfnp), &stbuf) != -1) 8170Sstevel@tonic-gate rotateto(gzfnp, opts, n + 1, recentlog, B_TRUE); 8180Sstevel@tonic-gate fn_free(gzfnp); 8190Sstevel@tonic-gate } 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate /* first time through run "before" cmd if not run already */ 8220Sstevel@tonic-gate if (n == 0 && opts_count(opts, "b")) { 8230Sstevel@tonic-gate const char *cmd = opts_optarg(opts, "b"); 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate if (lut_lookup(Beforecmds, cmd) == NULL) { 8260Sstevel@tonic-gate docmd(opts, "-b cmd", Sh, "-c", cmd, NULL); 8270Sstevel@tonic-gate Beforecmds = lut_add(Beforecmds, cmd, "1"); 8280Sstevel@tonic-gate } 8290Sstevel@tonic-gate } 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate /* ensure destination directory exists */ 8320Sstevel@tonic-gate dirname = fn_dirname(newfile); 8330Sstevel@tonic-gate docmd(opts, "verify directory exists", Mkdir, "-p", 8340Sstevel@tonic-gate fn_s(dirname), NULL); 8350Sstevel@tonic-gate fn_free(dirname); 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate /* do the rename */ 8380Sstevel@tonic-gate if (opts_count(opts, "c")) { 8390Sstevel@tonic-gate docmd(opts, "rotate log file via copy (-c flag)", 8400Sstevel@tonic-gate Cp, "-fp", fn_s(fnp), fn_s(newfile)); 8410Sstevel@tonic-gate docmd(opts, "truncate log file (-c flag)", 8420Sstevel@tonic-gate Cp, "-f", "/dev/null", fn_s(fnp)); 8430Sstevel@tonic-gate } else if (n == 0 && opts_count(opts, "M")) { 8440Sstevel@tonic-gate struct fn *rawcmd = fn_new(opts_optarg(opts, "M")); 8450Sstevel@tonic-gate struct fn *cmd = fn_new(NULL); 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate /* use specified command to mv the log file */ 8480Sstevel@tonic-gate kw_init(fnp, newfile); 8490Sstevel@tonic-gate (void) kw_expand(rawcmd, cmd, 0, B_FALSE); 8500Sstevel@tonic-gate docmd(opts, "-M cmd", Sh, "-c", fn_s(cmd), NULL); 8510Sstevel@tonic-gate fn_free(rawcmd); 8520Sstevel@tonic-gate fn_free(cmd); 8530Sstevel@tonic-gate } else 8540Sstevel@tonic-gate /* common case: we call "mv" to handle the actual rename */ 8550Sstevel@tonic-gate docmd(opts, "rotate log file", Mv, "-f", 8560Sstevel@tonic-gate fn_s(fnp), fn_s(newfile)); 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate /* gzip the old log file according to -z count */ 8590Sstevel@tonic-gate if (!isgz && opts_count(opts, "z")) { 860*2016Sbasabi off_t count = opts_optarg_int(opts, "z"); 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate if (Debug) 863*2016Sbasabi (void) fprintf(stderr, "rotateto z count %lld\n", 864*2016Sbasabi count); 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate if (count <= n) { 8671913Sdg199075 8681913Sdg199075 /* 8691913Sdg199075 * Don't gzip the old log file yet - 8701913Sdg199075 * it takes too long. Just remember that we 8711913Sdg199075 * need to gzip. 8721913Sdg199075 */ 8731913Sdg199075 Gzipnames = lut_add(Gzipnames, fn_s(newfile), "1"); 8740Sstevel@tonic-gate fn_puts(newfile, ".gz"); 8750Sstevel@tonic-gate } 8760Sstevel@tonic-gate } 8770Sstevel@tonic-gate 8780Sstevel@tonic-gate /* first time through, gather interesting info for caller */ 8790Sstevel@tonic-gate if (n == 0) 8800Sstevel@tonic-gate fn_renew(recentlog, fn_s(newfile)); 8810Sstevel@tonic-gate } 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate /* expire phase of logname processing */ 8840Sstevel@tonic-gate static void 8850Sstevel@tonic-gate expirefiles(struct fn *fnp, struct opts *opts) 8860Sstevel@tonic-gate { 8870Sstevel@tonic-gate char *fname = fn_s(fnp); 8880Sstevel@tonic-gate struct fn *template; 8890Sstevel@tonic-gate struct fn *pattern; 8900Sstevel@tonic-gate struct fn_list *files; 8910Sstevel@tonic-gate struct fn *nextfnp; 892*2016Sbasabi off_t count; 893*2016Sbasabi off_t size; 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate if (Debug) 8960Sstevel@tonic-gate (void) fprintf(stderr, "expirefiles: fname <%s>\n", fname); 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate /* return if no potential expire conditions */ 8990Sstevel@tonic-gate if (opts_count(opts, "AS") == 0 && opts_optarg_int(opts, "C") == 0) 9000Sstevel@tonic-gate return; 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate kw_init(fnp, NULL); 9030Sstevel@tonic-gate if (Debug > 1) { 9040Sstevel@tonic-gate (void) fprintf(stderr, "expirefiles keywords:\n"); 9050Sstevel@tonic-gate kw_print(stderr); 9060Sstevel@tonic-gate } 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate /* see if pattern was supplied by user */ 9090Sstevel@tonic-gate if (opts_count(opts, "T")) { 9100Sstevel@tonic-gate template = fn_new(opts_optarg(opts, "T")); 9110Sstevel@tonic-gate pattern = glob_to_reglob(template); 9120Sstevel@tonic-gate } else { 9130Sstevel@tonic-gate /* nope, generate pattern based on rotation template */ 9140Sstevel@tonic-gate template = fn_new(opts_optarg(opts, "t")); 9150Sstevel@tonic-gate pattern = fn_new(NULL); 9160Sstevel@tonic-gate (void) kw_expand(template, pattern, -1, 9170Sstevel@tonic-gate opts_count(opts, "z") != 0); 9180Sstevel@tonic-gate } 9190Sstevel@tonic-gate 9200Sstevel@tonic-gate /* match all old log files (hopefully not any others as well!) */ 9210Sstevel@tonic-gate files = glob_reglob(pattern); 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate if (Debug) { 9240Sstevel@tonic-gate (void) fprintf(stderr, "expirefiles: pattern <%s>\n", 9250Sstevel@tonic-gate fn_s(pattern)); 9260Sstevel@tonic-gate fn_list_rewind(files); 9270Sstevel@tonic-gate while ((nextfnp = fn_list_next(files)) != NULL) 9280Sstevel@tonic-gate (void) fprintf(stderr, " <%s>\n", fn_s(nextfnp)); 9290Sstevel@tonic-gate } 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate /* see if count causes expiration */ 9320Sstevel@tonic-gate if ((count = opts_optarg_int(opts, "C")) > 0) { 9330Sstevel@tonic-gate int needexpire = fn_list_count(files) - count; 9340Sstevel@tonic-gate 9350Sstevel@tonic-gate if (Debug) 9360Sstevel@tonic-gate (void) fprintf(stderr, "expirefiles: needexpire %d\n", 9370Sstevel@tonic-gate needexpire); 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate while (needexpire > 0 && 9400Sstevel@tonic-gate ((nextfnp = fn_list_popoldest(files)) != NULL)) { 9410Sstevel@tonic-gate dorm(opts, "expire by count rule", nextfnp); 9420Sstevel@tonic-gate fn_free(nextfnp); 9430Sstevel@tonic-gate needexpire--; 9440Sstevel@tonic-gate } 9450Sstevel@tonic-gate } 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate /* see if total size causes expiration */ 9480Sstevel@tonic-gate if (opts_count(opts, "S") && (size = opts_optarg_int(opts, "S")) > 0) { 9490Sstevel@tonic-gate while (fn_list_totalsize(files) > size && 9500Sstevel@tonic-gate ((nextfnp = fn_list_popoldest(files)) != NULL)) { 9510Sstevel@tonic-gate dorm(opts, "expire by size rule", nextfnp); 9520Sstevel@tonic-gate fn_free(nextfnp); 9530Sstevel@tonic-gate } 9540Sstevel@tonic-gate } 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate /* see if age causes expiration */ 9570Sstevel@tonic-gate if (opts_count(opts, "A")) { 958*2016Sbasabi int mtime = (int)time(0) - (int)opts_optarg_int(opts, "A"); 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate while ((nextfnp = fn_list_popoldest(files)) != NULL) 9610Sstevel@tonic-gate if (fn_getstat(nextfnp)->st_mtime < mtime) { 9620Sstevel@tonic-gate dorm(opts, "expire by age rule", nextfnp); 9630Sstevel@tonic-gate fn_free(nextfnp); 9640Sstevel@tonic-gate } else { 9650Sstevel@tonic-gate fn_free(nextfnp); 9660Sstevel@tonic-gate break; 9670Sstevel@tonic-gate } 9680Sstevel@tonic-gate } 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate fn_free(template); 9710Sstevel@tonic-gate fn_list_free(files); 9720Sstevel@tonic-gate } 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate /* execute a command to remove an expired log file */ 9750Sstevel@tonic-gate static void 9760Sstevel@tonic-gate dorm(struct opts *opts, const char *msg, struct fn *fnp) 9770Sstevel@tonic-gate { 9780Sstevel@tonic-gate if (opts_count(opts, "E")) { 9790Sstevel@tonic-gate struct fn *rawcmd = fn_new(opts_optarg(opts, "E")); 9800Sstevel@tonic-gate struct fn *cmd = fn_new(NULL); 9810Sstevel@tonic-gate 9820Sstevel@tonic-gate /* user supplied cmd, expand $file */ 9830Sstevel@tonic-gate kw_init(fnp, NULL); 9840Sstevel@tonic-gate (void) kw_expand(rawcmd, cmd, 0, B_FALSE); 9850Sstevel@tonic-gate docmd(opts, msg, Sh, "-c", fn_s(cmd), NULL); 9860Sstevel@tonic-gate fn_free(rawcmd); 9870Sstevel@tonic-gate fn_free(cmd); 9880Sstevel@tonic-gate } else 9890Sstevel@tonic-gate docmd(opts, msg, Rm, "-f", fn_s(fnp), NULL); 9901913Sdg199075 Gzipnames = lut_add(Gzipnames, fn_s(fnp), NULL); 9910Sstevel@tonic-gate } 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate /* execute a command, producing -n and -v output as necessary */ 9940Sstevel@tonic-gate static void 9950Sstevel@tonic-gate docmd(struct opts *opts, const char *msg, const char *cmd, 9960Sstevel@tonic-gate const char *arg1, const char *arg2, const char *arg3) 9970Sstevel@tonic-gate { 9980Sstevel@tonic-gate int pid; 9990Sstevel@tonic-gate int errpipe[2]; 10000Sstevel@tonic-gate 10010Sstevel@tonic-gate /* print info about command if necessary */ 10020Sstevel@tonic-gate if (opts_count(opts, "vn")) { 10030Sstevel@tonic-gate const char *simplecmd; 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate if ((simplecmd = strrchr(cmd, '/')) == NULL) 10060Sstevel@tonic-gate simplecmd = cmd; 10070Sstevel@tonic-gate else 10080Sstevel@tonic-gate simplecmd++; 10090Sstevel@tonic-gate (void) out("%s", simplecmd); 10100Sstevel@tonic-gate if (arg1) 10110Sstevel@tonic-gate (void) out(" %s", arg1); 10120Sstevel@tonic-gate if (arg2) 10130Sstevel@tonic-gate (void) out(" %s", arg2); 10140Sstevel@tonic-gate if (arg3) 10150Sstevel@tonic-gate (void) out(" %s", arg3); 10160Sstevel@tonic-gate if (msg) 10170Sstevel@tonic-gate (void) out(" # %s", msg); 10180Sstevel@tonic-gate (void) out("\n"); 10190Sstevel@tonic-gate } 10200Sstevel@tonic-gate 10210Sstevel@tonic-gate if (opts_count(opts, "n")) 10220Sstevel@tonic-gate return; /* -n means don't really do it */ 10230Sstevel@tonic-gate 10240Sstevel@tonic-gate /* 10250Sstevel@tonic-gate * run the cmd and see if it failed. this function is *not* a 10260Sstevel@tonic-gate * generic command runner -- we depend on some knowledge we 10270Sstevel@tonic-gate * have about the commands we run. first of all, we expect 10280Sstevel@tonic-gate * errors to spew something to stderr, and that something is 10290Sstevel@tonic-gate * typically short enough to fit into a pipe so we can wait() 10300Sstevel@tonic-gate * for the command to complete and then fetch the error text 10310Sstevel@tonic-gate * from the pipe. we also expect the exit codes to make sense. 10320Sstevel@tonic-gate * notice also that we only allow a command name which is an 10330Sstevel@tonic-gate * absolute pathname, and two args must be supplied (the 10340Sstevel@tonic-gate * second may be NULL, or they may both be NULL). 10350Sstevel@tonic-gate */ 10360Sstevel@tonic-gate if (pipe(errpipe) < 0) 10370Sstevel@tonic-gate err(EF_SYS, "pipe"); 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate if ((pid = fork()) < 0) 10400Sstevel@tonic-gate err(EF_SYS, "fork"); 10410Sstevel@tonic-gate else if (pid) { 10420Sstevel@tonic-gate int wstat; 10430Sstevel@tonic-gate int count; 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate /* parent */ 10460Sstevel@tonic-gate (void) close(errpipe[1]); 10470Sstevel@tonic-gate if (waitpid(pid, &wstat, 0) < 0) 10480Sstevel@tonic-gate err(EF_SYS, "waitpid"); 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate /* check for stderr output */ 10510Sstevel@tonic-gate if (ioctl(errpipe[0], FIONREAD, &count) >= 0 && count) { 10520Sstevel@tonic-gate err(EF_WARN, "command failed: %s%s%s%s%s%s%s", 10530Sstevel@tonic-gate cmd, 10540Sstevel@tonic-gate (arg1) ? " " : "", 10550Sstevel@tonic-gate (arg1) ? arg1 : "", 10560Sstevel@tonic-gate (arg2) ? " " : "", 10570Sstevel@tonic-gate (arg2) ? arg2 : "", 10580Sstevel@tonic-gate (arg3) ? " " : "", 10590Sstevel@tonic-gate (arg3) ? arg3 : ""); 10600Sstevel@tonic-gate err_fromfd(errpipe[0]); 10610Sstevel@tonic-gate } else if (WIFSIGNALED(wstat)) 10620Sstevel@tonic-gate err(EF_WARN, 10630Sstevel@tonic-gate "command died, signal %d: %s%s%s%s%s%s%s", 10640Sstevel@tonic-gate WTERMSIG(wstat), 10650Sstevel@tonic-gate cmd, 10660Sstevel@tonic-gate (arg1) ? " " : "", 10670Sstevel@tonic-gate (arg1) ? arg1 : "", 10680Sstevel@tonic-gate (arg2) ? " " : "", 10690Sstevel@tonic-gate (arg2) ? arg2 : "", 10700Sstevel@tonic-gate (arg3) ? " " : "", 10710Sstevel@tonic-gate (arg3) ? arg3 : ""); 10720Sstevel@tonic-gate else if (WIFEXITED(wstat) && WEXITSTATUS(wstat)) 10730Sstevel@tonic-gate err(EF_WARN, 10740Sstevel@tonic-gate "command error, exit %d: %s%s%s%s%s%s%s", 10750Sstevel@tonic-gate WEXITSTATUS(wstat), 10760Sstevel@tonic-gate cmd, 10770Sstevel@tonic-gate (arg1) ? " " : "", 10780Sstevel@tonic-gate (arg1) ? arg1 : "", 10790Sstevel@tonic-gate (arg2) ? " " : "", 10800Sstevel@tonic-gate (arg2) ? arg2 : "", 10810Sstevel@tonic-gate (arg3) ? " " : "", 10820Sstevel@tonic-gate (arg3) ? arg3 : ""); 10830Sstevel@tonic-gate 10840Sstevel@tonic-gate (void) close(errpipe[0]); 10850Sstevel@tonic-gate } else { 10860Sstevel@tonic-gate /* child */ 10870Sstevel@tonic-gate (void) dup2(errpipe[1], fileno(stderr)); 10880Sstevel@tonic-gate (void) close(errpipe[0]); 10890Sstevel@tonic-gate (void) execl(cmd, cmd, arg1, arg2, arg3, 0); 10900Sstevel@tonic-gate perror(cmd); 10910Sstevel@tonic-gate _exit(1); 10920Sstevel@tonic-gate } 10930Sstevel@tonic-gate } 1094