newsyslog.c revision 210407
1130167Sgad/*- 2130167Sgad * ------+---------+---------+-------- + --------+---------+---------+---------* 3130167Sgad * This file includes significant modifications done by: 4130167Sgad * Copyright (c) 2003, 2004 - Garance Alistair Drosehn <gad@FreeBSD.org>. 5130167Sgad * All rights reserved. 6130167Sgad * 7130167Sgad * Redistribution and use in source and binary forms, with or without 8130167Sgad * modification, are permitted provided that the following conditions 9130167Sgad * are met: 10130167Sgad * 1. Redistributions of source code must retain the above copyright 11130167Sgad * notice, this list of conditions and the following disclaimer. 12130167Sgad * 2. Redistributions in binary form must reproduce the above copyright 13130167Sgad * notice, this list of conditions and the following disclaimer in the 14130167Sgad * documentation and/or other materials provided with the distribution. 15130167Sgad * 16130167Sgad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17130167Sgad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18130167Sgad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19130167Sgad * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20130167Sgad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21130167Sgad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22130167Sgad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23130167Sgad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24130167Sgad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25130167Sgad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26130167Sgad * SUCH DAMAGE. 27130167Sgad * 28130167Sgad * ------+---------+---------+-------- + --------+---------+---------+---------* 29130167Sgad */ 30130167Sgad 3113244Sgraichen/* 3213244Sgraichen * This file contains changes from the Open Software Foundation. 3313244Sgraichen */ 3413244Sgraichen 3513244Sgraichen/* 3659004Shm * Copyright 1988, 1989 by the Massachusetts Institute of Technology 3759004Shm * 3859004Shm * Permission to use, copy, modify, and distribute this software and its 3959004Shm * documentation for any purpose and without fee is hereby granted, provided 4059004Shm * that the above copyright notice appear in all copies and that both that 4159004Shm * copyright notice and this permission notice appear in supporting 4259004Shm * documentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be 4359004Shm * used in advertising or publicity pertaining to distribution of the 4459004Shm * software without specific, written prior permission. M.I.T. and the M.I.T. 4559004Shm * S.I.P.B. make no representations about the suitability of this software 4659004Shm * for any purpose. It is provided "as is" without express or implied 4759004Shm * warranty. 4859004Shm * 4959004Shm */ 5013244Sgraichen 5113244Sgraichen/* 5259004Shm * newsyslog - roll over selected logs at the appropriate time, keeping the a 5359004Shm * specified number of backup files around. 5413244Sgraichen */ 5513244Sgraichen 56114601Sobrien#include <sys/cdefs.h> 57114601Sobrien__FBSDID("$FreeBSD: head/usr.sbin/newsyslog/newsyslog.c 210407 2010-07-23 09:11:47Z brian $"); 5813244Sgraichen 59130045Sgad#define OSF 6013244Sgraichen#ifndef COMPRESS_POSTFIX 61130045Sgad#define COMPRESS_POSTFIX ".gz" 6213244Sgraichen#endif 6380638Sobrien#ifndef BZCOMPRESS_POSTFIX 6480638Sobrien#define BZCOMPRESS_POSTFIX ".bz2" 6580638Sobrien#endif 6613244Sgraichen 6796001Smaxim#include <sys/param.h> 68130167Sgad#include <sys/queue.h> 6996001Smaxim#include <sys/stat.h> 7096001Smaxim#include <sys/wait.h> 7196001Smaxim 72210372Ssimon#include <assert.h> 7330160Scharnier#include <ctype.h> 7430160Scharnier#include <err.h> 7595999Smaxim#include <errno.h> 76210372Ssimon#include <dirent.h> 7730160Scharnier#include <fcntl.h> 78111773Sgad#include <fnmatch.h> 79106905Ssobomax#include <glob.h> 8030160Scharnier#include <grp.h> 8143071Swollman#include <paths.h> 8230160Scharnier#include <pwd.h> 8330160Scharnier#include <signal.h> 8413244Sgraichen#include <stdio.h> 85210372Ssimon#include <libgen.h> 8613244Sgraichen#include <stdlib.h> 8713244Sgraichen#include <string.h> 8843071Swollman#include <time.h> 8916240Salex#include <unistd.h> 9013244Sgraichen 9143071Swollman#include "pathnames.h" 92119998Sgad#include "extern.h" 9343071Swollman 94111768Sgad/* 95111768Sgad * Bit-values for the 'flags' parsed from a config-file entry. 96111768Sgad */ 97175837Sdelphij#define CE_COMPACT 0x0001 /* Compact the archived log files with gzip. */ 98175837Sdelphij#define CE_BZCOMPACT 0x0002 /* Compact the archived log files with bzip2. */ 99130045Sgad#define CE_BINARY 0x0008 /* Logfile is in binary, do not add status */ 100111768Sgad /* messages to logfile(s) when rotating. */ 101130045Sgad#define CE_NOSIGNAL 0x0010 /* There is no process to signal when */ 102111768Sgad /* trimming this file. */ 103130045Sgad#define CE_TRIMAT 0x0020 /* trim file at a specific time. */ 104130045Sgad#define CE_GLOB 0x0040 /* name of the log is file name pattern. */ 105130045Sgad#define CE_SIGNALGROUP 0x0080 /* Signal a process-group instead of a single */ 106112003Sgad /* process when trimming this file. */ 107130045Sgad#define CE_CREATE 0x0100 /* Create the log file if it does not exist. */ 108130043Sgad#define CE_NODUMP 0x0200 /* Set 'nodump' on newly created log file. */ 10943071Swollman 110130045Sgad#define MIN_PID 5 /* Don't touch pids lower than this */ 111130045Sgad#define MAX_PID 99999 /* was lower, see /usr/include/sys/proc.h */ 112111781Sgad 113130045Sgad#define kbytes(size) (((size) + 1023) >> 10) 114111781Sgad 115130165Sgad#define DEFAULT_MARKER "<default>" 116130165Sgad#define DEBUG_MARKER "<debug>" 117208649Sgordon#define INCLUDE_MARKER "<include>" 118210372Ssimon#define DEFAULT_TIMEFNAME_FMT "%Y%m%dT%H%M%S" 119130165Sgad 120210372Ssimon#define MAX_OLDLOGS 65536 /* Default maximum number of old logfiles */ 121210372Ssimon 12213244Sgraichenstruct conf_entry { 123208648Sgordon STAILQ_ENTRY(conf_entry) cf_nextp; 12459003Shm char *log; /* Name of the log */ 12559003Shm char *pid_file; /* PID file */ 126111772Sgad char *r_reason; /* The reason this file is being rotated */ 127114137Sgad int firstcreate; /* Creating log for the first time (-C). */ 128111772Sgad int rotate; /* Non-zero if this file should be rotated */ 129130165Sgad int fsize; /* size found for the log file */ 130111779Sgad uid_t uid; /* Owner of log */ 131111779Sgad gid_t gid; /* Group of log */ 13259003Shm int numlogs; /* Number of logs to keep */ 133130165Sgad int trsize; /* Size cutoff to trigger trimming the log */ 13459003Shm int hours; /* Hours between log trimming */ 135120361Sgad struct ptime_data *trim_at; /* Specific time to do trimming */ 136139655Sdelphij unsigned int permissions; /* File permissions on the log */ 13780646Sobrien int flags; /* CE_COMPACT, CE_BZCOMPACT, CE_BINARY */ 13859003Shm int sig; /* Signal to send */ 139111388Sgad int def_cfg; /* Using the <default> rule for this file */ 14013244Sgraichen}; 14113244Sgraichen 142130167Sgadstruct sigwork_entry { 143130167Sgad SLIST_ENTRY(sigwork_entry) sw_nextp; 144130167Sgad int sw_signum; /* the signal to send */ 145130167Sgad int sw_pidok; /* true if pid value is valid */ 146130167Sgad pid_t sw_pid; /* the process id from the PID file */ 147130167Sgad const char *sw_pidtype; /* "daemon" or "process group" */ 148130167Sgad char sw_fname[1]; /* file the PID was read from */ 149130167Sgad}; 150130167Sgad 151130167Sgadstruct zipwork_entry { 152130167Sgad SLIST_ENTRY(zipwork_entry) zw_nextp; 153130167Sgad const struct conf_entry *zw_conf; /* for chown/perm/flag info */ 154130167Sgad const struct sigwork_entry *zw_swork; /* to know success of signal */ 155130167Sgad int zw_fsize; /* size of the file to compress */ 156130167Sgad char zw_fname[1]; /* the file to compress */ 157130167Sgad}; 158130167Sgad 159208649Sgordonstruct include_entry { 160208649Sgordon STAILQ_ENTRY(include_entry) inc_nextp; 161208649Sgordon const char *file; /* Name of file to process */ 162208649Sgordon}; 163208649Sgordon 164210372Ssimonstruct oldlog_entry { 165210372Ssimon char *fname; /* Filename of the log file */ 166210372Ssimon time_t t; /* Parses timestamp of the logfile */ 167210372Ssimon}; 168210372Ssimon 169130165Sgadtypedef enum { 170130165Sgad FREE_ENT, KEEP_ENT 171130165Sgad} fk_entry; 172111388Sgad 173208648SgordonSTAILQ_HEAD(cflist, conf_entry); 174130167SgadSLIST_HEAD(swlisthead, sigwork_entry) swhead = SLIST_HEAD_INITIALIZER(swhead); 175130167SgadSLIST_HEAD(zwlisthead, zipwork_entry) zwhead = SLIST_HEAD_INITIALIZER(zwhead); 176208649SgordonSTAILQ_HEAD(ilist, include_entry); 177130167Sgad 178120361Sgadint dbg_at_times; /* -D Show details of 'trim_at' code */ 179120361Sgad 18059004Shmint archtodir = 0; /* Archive old logfiles to other directory */ 181114137Sgadint createlogs; /* Create (non-GLOB) logfiles which do not */ 182114137Sgad /* already exist. 1=='for entries with */ 183114137Sgad /* C flag', 2=='for all entries'. */ 18459003Shmint verbose = 0; /* Print out what's going on */ 18559003Shmint needroot = 1; /* Root privs are necessary */ 18659003Shmint noaction = 0; /* Don't do anything, just show it */ 187143106Sbrooksint norotate = 0; /* Don't rotate */ 188111768Sgadint nosignal; /* Do not send any signals */ 189202668Sdelphijint enforcepid = 0; /* If PID file does not exist or empty, do nothing */ 19059003Shmint force = 0; /* Force the trim no matter what */ 191111772Sgadint rotatereq = 0; /* -R = Always rotate the file(s) as given */ 192111772Sgad /* on the command (this also requires */ 193111772Sgad /* that a list of files *are* given on */ 194111772Sgad /* the run command). */ 195111772Sgadchar *requestor; /* The name given on a -R request */ 196210372Ssimonchar *timefnamefmt = NULL; /* Use time based filenames instead of .0 etc */ 19759004Shmchar *archdirname; /* Directory path to old logfiles archive */ 198136127Sbrookschar *destdir = NULL; /* Directory to treat at root for logs */ 199111773Sgadconst char *conf; /* Configuration file to use */ 20059003Shm 201120361Sgadstruct ptime_data *dbg_timenow; /* A "timenow" value set via -D option */ 202120361Sgadstruct ptime_data *timenow; /* The time to use for checking at-fields */ 203120361Sgad 204159998Sgad#define DAYTIME_LEN 16 205159998Sgadchar daytime[DAYTIME_LEN]; /* The current time in human readable form, 206159998Sgad * used for rotation-tracking messages. */ 20771299Sjedgarchar hostname[MAXHOSTNAMELEN]; /* hostname */ 20813244Sgraichen 209210407Sbrianconst char *path_syslogpid = _PATH_SYSLOGPID; 210210407Sbrian 211208648Sgordonstatic struct cflist *get_worklist(char **files); 212208648Sgordonstatic void parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p, 213208649Sgordon struct conf_entry *defconf_p, struct ilist *inclist); 214208649Sgordonstatic void add_to_queue(const char *fname, struct ilist *inclist); 21516240Salexstatic char *sob(char *p); 21616240Salexstatic char *son(char *p); 217119102Sgadstatic int isnumberstr(const char *); 218208649Sgordonstatic int isglobstr(const char *); 21959003Shmstatic char *missing_field(char *p, char *errline); 220130165Sgadstatic void change_attrs(const char *, const struct conf_entry *); 221130165Sgadstatic fk_entry do_entry(struct conf_entry *); 222130165Sgadstatic fk_entry do_rotate(const struct conf_entry *); 223130167Sgadstatic void do_sigwork(struct sigwork_entry *); 224130167Sgadstatic void do_zipwork(struct zipwork_entry *); 225130167Sgadstatic struct sigwork_entry * 226130167Sgad save_sigwork(const struct conf_entry *); 227130167Sgadstatic struct zipwork_entry * 228130167Sgad save_zipwork(const struct conf_entry *, const struct 229130167Sgad sigwork_entry *, int, const char *); 230130167Sgadstatic void set_swpid(struct sigwork_entry *, const struct conf_entry *); 231130165Sgadstatic int sizefile(const char *); 232208648Sgordonstatic void expand_globs(struct cflist *work_p, struct cflist *glob_p); 233208648Sgordonstatic void free_clist(struct cflist *list); 234111388Sgadstatic void free_entry(struct conf_entry *ent); 235111388Sgadstatic struct conf_entry *init_entry(const char *fname, 236111388Sgad struct conf_entry *src_entry); 237111781Sgadstatic void parse_args(int argc, char **argv); 238119904Sgadstatic int parse_doption(const char *doption); 23980640Sobrienstatic void usage(void); 240119926Sgadstatic int log_trim(const char *logname, const struct conf_entry *log_ent); 24116240Salexstatic int age_old_log(char *file); 242130038Sgadstatic void savelog(char *from, char *to); 243114137Sgadstatic void createdir(const struct conf_entry *ent, char *dirpart); 244114137Sgadstatic void createlog(const struct conf_entry *ent); 24513244Sgraichen 246111768Sgad/* 247129975Sgad * All the following take a parameter of 'int', but expect values in the 248129975Sgad * range of unsigned char. Define wrappers which take values of type 'char', 249129975Sgad * whether signed or unsigned, and ensure they end up in the right range. 250111768Sgad */ 251129975Sgad#define isdigitch(Anychar) isdigit((u_char)(Anychar)) 252129975Sgad#define isprintch(Anychar) isprint((u_char)(Anychar)) 253129975Sgad#define isspacech(Anychar) isspace((u_char)(Anychar)) 254129975Sgad#define tolowerch(Anychar) tolower((u_char)(Anychar)) 255111768Sgad 25659004Shmint 25759004Shmmain(int argc, char **argv) 25813244Sgraichen{ 259208648Sgordon struct cflist *worklist; 260208648Sgordon struct conf_entry *p; 261130167Sgad struct sigwork_entry *stmp; 262130167Sgad struct zipwork_entry *ztmp; 26325443Sache 264130167Sgad SLIST_INIT(&swhead); 265130167Sgad SLIST_INIT(&zwhead); 266130167Sgad 267111781Sgad parse_args(argc, argv); 268111773Sgad argc -= optind; 269111773Sgad argv += optind; 270111773Sgad 27159003Shm if (needroot && getuid() && geteuid()) 27259003Shm errx(1, "must have root privs"); 273208648Sgordon worklist = get_worklist(argv); 27459003Shm 275130165Sgad /* 276130165Sgad * Rotate all the files which need to be rotated. Note that 277130165Sgad * some users have *hundreds* of entries in newsyslog.conf! 278130165Sgad */ 279208648Sgordon while (!STAILQ_EMPTY(worklist)) { 280208648Sgordon p = STAILQ_FIRST(worklist); 281208648Sgordon STAILQ_REMOVE_HEAD(worklist, cf_nextp); 282208648Sgordon if (do_entry(p) == FREE_ENT) 283208648Sgordon free_entry(p); 28459003Shm } 285130165Sgad 286130167Sgad /* 287130167Sgad * Send signals to any processes which need a signal to tell 288130167Sgad * them to close and re-open the log file(s) we have rotated. 289130167Sgad * Note that zipwork_entries include pointers to these 290130167Sgad * sigwork_entry's, so we can not free the entries here. 291130167Sgad */ 292130167Sgad if (!SLIST_EMPTY(&swhead)) { 293130204Sgad if (noaction || verbose) 294130167Sgad printf("Signal all daemon process(es)...\n"); 295130167Sgad SLIST_FOREACH(stmp, &swhead, sw_nextp) 296130167Sgad do_sigwork(stmp); 297130204Sgad if (noaction) 298130204Sgad printf("\tsleep 10\n"); 299130204Sgad else { 300130204Sgad if (verbose) 301130204Sgad printf("Pause 10 seconds to allow daemon(s)" 302130204Sgad " to close log file(s)\n"); 303130204Sgad sleep(10); 304130204Sgad } 305130167Sgad } 306130167Sgad /* 307130167Sgad * Compress all files that we're expected to compress, now 308130167Sgad * that all processes should have closed the files which 309130167Sgad * have been rotated. 310130167Sgad */ 311130167Sgad if (!SLIST_EMPTY(&zwhead)) { 312130204Sgad if (noaction || verbose) 313130167Sgad printf("Compress all rotated log file(s)...\n"); 314130167Sgad while (!SLIST_EMPTY(&zwhead)) { 315130167Sgad ztmp = SLIST_FIRST(&zwhead); 316130167Sgad do_zipwork(ztmp); 317130167Sgad SLIST_REMOVE_HEAD(&zwhead, zw_nextp); 318130167Sgad free(ztmp); 319130167Sgad } 320130167Sgad } 321130167Sgad /* Now free all the sigwork entries. */ 322130167Sgad while (!SLIST_EMPTY(&swhead)) { 323130167Sgad stmp = SLIST_FIRST(&swhead); 324130167Sgad SLIST_REMOVE_HEAD(&swhead, sw_nextp); 325130167Sgad free(stmp); 326130167Sgad } 327130167Sgad 32895999Smaxim while (wait(NULL) > 0 || errno == EINTR) 32995999Smaxim ; 33059003Shm return (0); 33113244Sgraichen} 33213244Sgraichen 333111388Sgadstatic struct conf_entry * 334111388Sgadinit_entry(const char *fname, struct conf_entry *src_entry) 335111388Sgad{ 336111388Sgad struct conf_entry *tempwork; 337111388Sgad 338111388Sgad if (verbose > 4) 339111388Sgad printf("\t--> [creating entry for %s]\n", fname); 340111388Sgad 341111388Sgad tempwork = malloc(sizeof(struct conf_entry)); 342111388Sgad if (tempwork == NULL) 343111388Sgad err(1, "malloc of conf_entry for %s", fname); 344111388Sgad 345136174Sbrooks if (destdir == NULL || fname[0] != '/') 346136127Sbrooks tempwork->log = strdup(fname); 347136127Sbrooks else 348136127Sbrooks asprintf(&tempwork->log, "%s%s", destdir, fname); 349111388Sgad if (tempwork->log == NULL) 350111388Sgad err(1, "strdup for %s", fname); 351111388Sgad 352111388Sgad if (src_entry != NULL) { 353111388Sgad tempwork->pid_file = NULL; 354111388Sgad if (src_entry->pid_file) 355111388Sgad tempwork->pid_file = strdup(src_entry->pid_file); 356111772Sgad tempwork->r_reason = NULL; 357114137Sgad tempwork->firstcreate = 0; 358111772Sgad tempwork->rotate = 0; 359130165Sgad tempwork->fsize = -1; 360111388Sgad tempwork->uid = src_entry->uid; 361111388Sgad tempwork->gid = src_entry->gid; 362111388Sgad tempwork->numlogs = src_entry->numlogs; 363130165Sgad tempwork->trsize = src_entry->trsize; 364111388Sgad tempwork->hours = src_entry->hours; 365120361Sgad tempwork->trim_at = NULL; 366120361Sgad if (src_entry->trim_at != NULL) 367120361Sgad tempwork->trim_at = ptime_init(src_entry->trim_at); 368111388Sgad tempwork->permissions = src_entry->permissions; 369111388Sgad tempwork->flags = src_entry->flags; 370111388Sgad tempwork->sig = src_entry->sig; 371111388Sgad tempwork->def_cfg = src_entry->def_cfg; 372111388Sgad } else { 373111388Sgad /* Initialize as a "do-nothing" entry */ 374111388Sgad tempwork->pid_file = NULL; 375111772Sgad tempwork->r_reason = NULL; 376114137Sgad tempwork->firstcreate = 0; 377111772Sgad tempwork->rotate = 0; 378130165Sgad tempwork->fsize = -1; 379111779Sgad tempwork->uid = (uid_t)-1; 380111779Sgad tempwork->gid = (gid_t)-1; 381111388Sgad tempwork->numlogs = 1; 382130165Sgad tempwork->trsize = -1; 383111388Sgad tempwork->hours = -1; 384120361Sgad tempwork->trim_at = NULL; 385111388Sgad tempwork->permissions = 0; 386111388Sgad tempwork->flags = 0; 387111388Sgad tempwork->sig = SIGHUP; 388111388Sgad tempwork->def_cfg = 0; 389111388Sgad } 390111388Sgad 391111388Sgad return (tempwork); 392111388Sgad} 393111388Sgad 39459004Shmstatic void 395111388Sgadfree_entry(struct conf_entry *ent) 396111388Sgad{ 397111388Sgad 398111388Sgad if (ent == NULL) 399111388Sgad return; 400111388Sgad 401111388Sgad if (ent->log != NULL) { 402111388Sgad if (verbose > 4) 403111388Sgad printf("\t--> [freeing entry for %s]\n", ent->log); 404111388Sgad free(ent->log); 405111388Sgad ent->log = NULL; 406111388Sgad } 407111388Sgad 408111388Sgad if (ent->pid_file != NULL) { 409111388Sgad free(ent->pid_file); 410111388Sgad ent->pid_file = NULL; 411111388Sgad } 412111388Sgad 413111772Sgad if (ent->r_reason != NULL) { 414111772Sgad free(ent->r_reason); 415111772Sgad ent->r_reason = NULL; 416111772Sgad } 417111772Sgad 418120361Sgad if (ent->trim_at != NULL) { 419120361Sgad ptime_free(ent->trim_at); 420120361Sgad ent->trim_at = NULL; 421120361Sgad } 422120361Sgad 423111388Sgad free(ent); 424111388Sgad} 425111388Sgad 426111388Sgadstatic void 427208648Sgordonfree_clist(struct cflist *list) 428112020Sgad{ 429208648Sgordon struct conf_entry *ent; 430112020Sgad 431208648Sgordon while (!STAILQ_EMPTY(list)) { 432208648Sgordon ent = STAILQ_FIRST(list); 433208648Sgordon STAILQ_REMOVE_HEAD(list, cf_nextp); 434112020Sgad free_entry(ent); 435112020Sgad } 436208648Sgordon 437208648Sgordon free(list); 438208648Sgordon list = NULL; 439112020Sgad} 440112020Sgad 441130165Sgadstatic fk_entry 44259004Shmdo_entry(struct conf_entry * ent) 44313244Sgraichen{ 444130045Sgad#define REASON_MAX 80 445130165Sgad int modtime; 446130165Sgad fk_entry free_or_keep; 447120361Sgad double diffsecs; 448111772Sgad char temp_reason[REASON_MAX]; 44959003Shm 450130165Sgad free_or_keep = FREE_ENT; 45159003Shm if (verbose) { 45259003Shm if (ent->flags & CE_COMPACT) 45359003Shm printf("%s <%dZ>: ", ent->log, ent->numlogs); 45480638Sobrien else if (ent->flags & CE_BZCOMPACT) 45580638Sobrien printf("%s <%dJ>: ", ent->log, ent->numlogs); 45659003Shm else 45759003Shm printf("%s <%d>: ", ent->log, ent->numlogs); 45859003Shm } 459130165Sgad ent->fsize = sizefile(ent->log); 46059003Shm modtime = age_old_log(ent->log); 461111772Sgad ent->rotate = 0; 462114137Sgad ent->firstcreate = 0; 463130165Sgad if (ent->fsize < 0) { 464114137Sgad /* 465114137Sgad * If either the C flag or the -C option was specified, 466114137Sgad * and if we won't be creating the file, then have the 467114137Sgad * verbose message include a hint as to why the file 468114137Sgad * will not be created. 469114137Sgad */ 470114137Sgad temp_reason[0] = '\0'; 471114137Sgad if (createlogs > 1) 472114137Sgad ent->firstcreate = 1; 473114137Sgad else if ((ent->flags & CE_CREATE) && createlogs) 474114137Sgad ent->firstcreate = 1; 475114137Sgad else if (ent->flags & CE_CREATE) 476159998Sgad strlcpy(temp_reason, " (no -C option)", REASON_MAX); 477114137Sgad else if (createlogs) 478159998Sgad strlcpy(temp_reason, " (no C flag)", REASON_MAX); 479114137Sgad 480114137Sgad if (ent->firstcreate) { 481114137Sgad if (verbose) 482114137Sgad printf("does not exist -> will create.\n"); 483114137Sgad createlog(ent); 484114137Sgad } else if (verbose) { 485114137Sgad printf("does not exist, skipped%s.\n", temp_reason); 486114137Sgad } 48759003Shm } else { 488111772Sgad if (ent->flags & CE_TRIMAT && !force && !rotatereq) { 489120361Sgad diffsecs = ptimeget_diff(timenow, ent->trim_at); 490120361Sgad if (diffsecs < 0.0) { 491120361Sgad /* trim_at is some time in the future. */ 492120361Sgad if (verbose) { 493120361Sgad ptime_adjust4dst(ent->trim_at, 494120361Sgad timenow); 49543071Swollman printf("--> will trim at %s", 496120361Sgad ptimeget_ctime(ent->trim_at)); 497120361Sgad } 498130165Sgad return (free_or_keep); 499120361Sgad } else if (diffsecs >= 3600.0) { 500120361Sgad /* 501120361Sgad * trim_at is more than an hour in the past, 502120361Sgad * so find the next valid trim_at time, and 503120361Sgad * tell the user what that will be. 504120361Sgad */ 505120361Sgad if (verbose && dbg_at_times) 506120361Sgad printf("\n\t--> prev trim at %s\t", 507120361Sgad ptimeget_ctime(ent->trim_at)); 508120361Sgad if (verbose) { 509120361Sgad ptimeset_nxtime(ent->trim_at); 510120361Sgad printf("--> will trim at %s", 511120361Sgad ptimeget_ctime(ent->trim_at)); 512120361Sgad } 513130165Sgad return (free_or_keep); 514120361Sgad } else if (verbose && noaction && dbg_at_times) { 515120361Sgad /* 516120361Sgad * If we are just debugging at-times, then 517120361Sgad * a detailed message is helpful. Also 518120361Sgad * skip "doing" any commands, since they 519120361Sgad * would all be turned off by no-action. 520120361Sgad */ 521120361Sgad printf("\n\t--> timematch at %s", 522120361Sgad ptimeget_ctime(ent->trim_at)); 523130165Sgad return (free_or_keep); 52443071Swollman } else if (verbose && ent->hours <= 0) { 52543071Swollman printf("--> time is up\n"); 52643071Swollman } 52743071Swollman } 528130165Sgad if (verbose && (ent->trsize > 0)) 529130165Sgad printf("size (Kb): %d [%d] ", ent->fsize, ent->trsize); 53059003Shm if (verbose && (ent->hours > 0)) 53159003Shm printf(" age (hr): %d [%d] ", modtime, ent->hours); 532111772Sgad 533111772Sgad /* 534111772Sgad * Figure out if this logfile needs to be rotated. 535111772Sgad */ 536111772Sgad temp_reason[0] = '\0'; 537111772Sgad if (rotatereq) { 538111772Sgad ent->rotate = 1; 539111772Sgad snprintf(temp_reason, REASON_MAX, " due to -R from %s", 540111772Sgad requestor); 541111772Sgad } else if (force) { 542111772Sgad ent->rotate = 1; 543111772Sgad snprintf(temp_reason, REASON_MAX, " due to -F request"); 544130165Sgad } else if ((ent->trsize > 0) && (ent->fsize >= ent->trsize)) { 545111772Sgad ent->rotate = 1; 546111772Sgad snprintf(temp_reason, REASON_MAX, " due to size>%dK", 547130165Sgad ent->trsize); 548111772Sgad } else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) { 549111772Sgad ent->rotate = 1; 550111772Sgad } else if ((ent->hours > 0) && ((modtime >= ent->hours) || 551111772Sgad (modtime < 0))) { 552111772Sgad ent->rotate = 1; 553111772Sgad } 554111772Sgad 555111772Sgad /* 556111772Sgad * If the file needs to be rotated, then rotate it. 557111772Sgad */ 558143106Sbrooks if (ent->rotate && !norotate) { 559111772Sgad if (temp_reason[0] != '\0') 560111772Sgad ent->r_reason = strdup(temp_reason); 56159003Shm if (verbose) 56259003Shm printf("--> trimming log....\n"); 56359003Shm if (noaction && !verbose) { 56459003Shm if (ent->flags & CE_COMPACT) 56559003Shm printf("%s <%dZ>: trimming\n", 56659003Shm ent->log, ent->numlogs); 56780638Sobrien else if (ent->flags & CE_BZCOMPACT) 56880638Sobrien printf("%s <%dJ>: trimming\n", 56980638Sobrien ent->log, ent->numlogs); 57059003Shm else 57159003Shm printf("%s <%d>: trimming\n", 57259003Shm ent->log, ent->numlogs); 57359003Shm } 574130165Sgad free_or_keep = do_rotate(ent); 57559003Shm } else { 57659003Shm if (verbose) 57759003Shm printf("--> skipping\n"); 57859003Shm } 57959003Shm } 580130165Sgad return (free_or_keep); 581111772Sgad#undef REASON_MAX 58213244Sgraichen} 58313244Sgraichen 58459004Shmstatic void 585111781Sgadparse_args(int argc, char **argv) 58613244Sgraichen{ 587111781Sgad int ch; 58859003Shm char *p; 58913244Sgraichen 590120361Sgad timenow = ptime_init(NULL); 591120361Sgad ptimeset_time(timenow, time(NULL)); 592159998Sgad strlcpy(daytime, ptimeget_ctime(timenow) + 4, DAYTIME_LEN); 59313244Sgraichen 59459003Shm /* Let's get our hostname */ 595111781Sgad (void)gethostname(hostname, sizeof(hostname)); 59613244Sgraichen 59713244Sgraichen /* Truncate domain */ 598111781Sgad if ((p = strchr(hostname, '.')) != NULL) 59913244Sgraichen *p = '\0'; 600111768Sgad 601111768Sgad /* Parse command line options. */ 602210372Ssimon while ((ch = getopt(argc, argv, "a:d:f:nrst:vCD:FNPR:")) != -1) 603111781Sgad switch (ch) { 60459004Shm case 'a': 60559004Shm archtodir++; 60659004Shm archdirname = optarg; 60759004Shm break; 608136127Sbrooks case 'd': 609136127Sbrooks destdir = optarg; 610136127Sbrooks break; 611111768Sgad case 'f': 612111768Sgad conf = optarg; 613111768Sgad break; 614111768Sgad case 'n': 615111768Sgad noaction++; 616111768Sgad break; 61759003Shm case 'r': 61859003Shm needroot = 0; 61959003Shm break; 620111768Sgad case 's': 621111768Sgad nosignal = 1; 622111768Sgad break; 623210372Ssimon case 't': 624210372Ssimon if (optarg[0] == '\0' || 625210372Ssimon strcmp(optarg, "DEFAULT") == 0) 626210372Ssimon timefnamefmt = strdup(DEFAULT_TIMEFNAME_FMT); 627210372Ssimon else 628210372Ssimon timefnamefmt = strdup(optarg); 629210372Ssimon break; 63059003Shm case 'v': 63159003Shm verbose++; 63259003Shm break; 633114137Sgad case 'C': 634114137Sgad /* Useful for things like rc.diskless... */ 635114137Sgad createlogs++; 636114137Sgad break; 637119904Sgad case 'D': 638119904Sgad /* 639119904Sgad * Set some debugging option. The specific option 640119904Sgad * depends on the value of optarg. These options 641119904Sgad * may come and go without notice or documentation. 642119904Sgad */ 643119904Sgad if (parse_doption(optarg)) 644119904Sgad break; 645119904Sgad usage(); 646119904Sgad /* NOTREACHED */ 64734584Spst case 'F': 64834584Spst force++; 64934584Spst break; 650143106Sbrooks case 'N': 651143106Sbrooks norotate++; 652143106Sbrooks break; 653202668Sdelphij case 'P': 654202668Sdelphij enforcepid++; 655202668Sdelphij break; 656111772Sgad case 'R': 657111772Sgad rotatereq++; 658111772Sgad requestor = strdup(optarg); 659111772Sgad break; 660210407Sbrian case 'S': 661210407Sbrian path_syslogpid = optarg; 662210407Sbrian break; 663111781Sgad case 'm': /* Used by OpenBSD for "monitor mode" */ 66459003Shm default: 66559003Shm usage(); 666111768Sgad /* NOTREACHED */ 66759003Shm } 668111772Sgad 669143106Sbrooks if (force && norotate) { 670143106Sbrooks warnx("Only one of -F and -N may be specified."); 671143106Sbrooks usage(); 672143106Sbrooks /* NOTREACHED */ 673143106Sbrooks } 674143106Sbrooks 675111772Sgad if (rotatereq) { 676111772Sgad if (optind == argc) { 677111772Sgad warnx("At least one filename must be given when -R is specified."); 678111772Sgad usage(); 679111772Sgad /* NOTREACHED */ 680111772Sgad } 681111772Sgad /* Make sure "requestor" value is safe for a syslog message. */ 682111772Sgad for (p = requestor; *p != '\0'; p++) { 683111772Sgad if (!isprintch(*p) && (*p != '\t')) 684111772Sgad *p = '.'; 685111772Sgad } 686111772Sgad } 687119904Sgad 688119904Sgad if (dbg_timenow) { 689119904Sgad /* 690119904Sgad * Note that the 'daytime' variable is not changed. 691119904Sgad * That is only used in messages that track when a 692119904Sgad * logfile is rotated, and if a file *is* rotated, 693119904Sgad * then it will still rotated at the "real now" time. 694119904Sgad */ 695120361Sgad ptime_free(timenow); 696119904Sgad timenow = dbg_timenow; 697119904Sgad fprintf(stderr, "Debug: Running as if TimeNow is %s", 698120361Sgad ptimeget_ctime(dbg_timenow)); 699119904Sgad } 700119904Sgad 70143071Swollman} 70213244Sgraichen 703120361Sgad/* 704120361Sgad * These debugging options are mainly meant for developer use, such 705120361Sgad * as writing regression-tests. They would not be needed by users 706120361Sgad * during normal operation of newsyslog... 707120361Sgad */ 708119904Sgadstatic int 709119904Sgadparse_doption(const char *doption) 710119904Sgad{ 711119904Sgad const char TN[] = "TN="; 712120361Sgad int res; 713119904Sgad 714119904Sgad if (strncmp(doption, TN, sizeof(TN) - 1) == 0) { 715119904Sgad /* 716120361Sgad * The "TimeNow" debugging option. This might be off 717120361Sgad * by an hour when crossing a timezone change. 718119904Sgad */ 719120361Sgad dbg_timenow = ptime_init(NULL); 720120361Sgad res = ptime_relparse(dbg_timenow, PTM_PARSE_ISO8601, 721120361Sgad time(NULL), doption + sizeof(TN) - 1); 722120361Sgad if (res == -2) { 723120361Sgad warnx("Non-existent time specified on -D %s", doption); 724120361Sgad return (0); /* failure */ 725120361Sgad } else if (res < 0) { 726119904Sgad warnx("Malformed time given on -D %s", doption); 727119904Sgad return (0); /* failure */ 728119904Sgad } 729119904Sgad return (1); /* successfully parsed */ 730119904Sgad 731119904Sgad } 732119904Sgad 733120361Sgad if (strcmp(doption, "ats") == 0) { 734120361Sgad dbg_at_times++; 735120361Sgad return (1); /* successfully parsed */ 736120361Sgad } 737120361Sgad 738159968Sgad /* XXX - This check could probably be dropped. */ 739159968Sgad if ((strcmp(doption, "neworder") == 0) || (strcmp(doption, "oldorder") 740159968Sgad == 0)) { 741159968Sgad warnx("NOTE: newsyslog always uses 'neworder'."); 742130167Sgad return (1); /* successfully parsed */ 743130167Sgad } 744130167Sgad 745130165Sgad warnx("Unknown -D (debug) option: '%s'", doption); 746119904Sgad return (0); /* failure */ 747119904Sgad} 748119904Sgad 74959004Shmstatic void 75059004Shmusage(void) 75113244Sgraichen{ 75280646Sobrien 75380646Sobrien fprintf(stderr, 754143106Sbrooks "usage: newsyslog [-CFNnrsv] [-a directory] [-d directory] [-f config-file]\n" 755210407Sbrian " [-S pidfile] [-t timefmt ] [ [-R requestor] filename ... ]\n"); 75659003Shm exit(1); 75713244Sgraichen} 75813244Sgraichen 75959004Shm/* 760111773Sgad * Parse a configuration file and return a linked list of all the logs 761111773Sgad * which should be processed. 76213244Sgraichen */ 763208648Sgordonstatic struct cflist * 764111773Sgadget_worklist(char **files) 76513244Sgraichen{ 76659003Shm FILE *f; 767111773Sgad char **given; 768208649Sgordon struct cflist *cmdlist, *filelist, *globlist; 769208648Sgordon struct conf_entry *defconf, *dupent, *ent; 770208649Sgordon struct ilist inclist; 771208649Sgordon struct include_entry *inc; 772112020Sgad int gmatch, fnres; 773111773Sgad 774208648Sgordon defconf = NULL; 775208649Sgordon STAILQ_INIT(&inclist); 776111773Sgad 777208648Sgordon filelist = malloc(sizeof(struct cflist)); 778208648Sgordon if (filelist == NULL) 779208648Sgordon err(1, "malloc of filelist"); 780208648Sgordon STAILQ_INIT(filelist); 781208648Sgordon globlist = malloc(sizeof(struct cflist)); 782208648Sgordon if (globlist == NULL) 783208648Sgordon err(1, "malloc of globlist"); 784208648Sgordon STAILQ_INIT(globlist); 785208648Sgordon 786208649Sgordon inc = malloc(sizeof(struct include_entry)); 787208649Sgordon if (inc == NULL) 788208649Sgordon err(1, "malloc of inc"); 789208649Sgordon inc->file = conf; 790208649Sgordon if (inc->file == NULL) 791208649Sgordon inc->file = _PATH_CONF; 792208649Sgordon STAILQ_INSERT_TAIL(&inclist, inc, inc_nextp); 793111773Sgad 794208649Sgordon STAILQ_FOREACH(inc, &inclist, inc_nextp) { 795208649Sgordon if (strcmp(inc->file, "-") != 0) 796208649Sgordon f = fopen(inc->file, "r"); 797208649Sgordon else { 798208649Sgordon f = stdin; 799208649Sgordon inc->file = "<stdin>"; 800208649Sgordon } 801208649Sgordon if (!f) 802208649Sgordon err(1, "%s", inc->file); 803208649Sgordon 804208649Sgordon if (verbose) 805208649Sgordon printf("Processing %s\n", inc->file); 806208649Sgordon parse_file(f, filelist, globlist, defconf, &inclist); 807208649Sgordon (void) fclose(f); 808111773Sgad } 809111773Sgad 810111773Sgad /* 811111773Sgad * All config-file information has been read in and turned into 812208648Sgordon * a filelist and a globlist. If there were no specific files 813112020Sgad * given on the run command, then the only thing left to do is to 814112020Sgad * call a routine which finds all files matched by the globlist 815208648Sgordon * and adds them to the filelist. Then return the worklist. 816111773Sgad */ 817111773Sgad if (*files == NULL) { 818208648Sgordon expand_globs(filelist, globlist); 819208648Sgordon free_clist(globlist); 820111773Sgad if (defconf != NULL) 821111773Sgad free_entry(defconf); 822208648Sgordon return (filelist); 823111773Sgad /* NOTREACHED */ 824111773Sgad } 825111773Sgad 826111773Sgad /* 827111773Sgad * If newsyslog was given a specific list of files to process, 828111773Sgad * it may be that some of those files were not listed in any 829111773Sgad * config file. Those unlisted files should get the default 830111773Sgad * rotation action. First, create the default-rotation action 831111773Sgad * if none was found in a system config file. 832111773Sgad */ 833111773Sgad if (defconf == NULL) { 834111773Sgad defconf = init_entry(DEFAULT_MARKER, NULL); 835111773Sgad defconf->numlogs = 3; 836130165Sgad defconf->trsize = 50; 837111773Sgad defconf->permissions = S_IRUSR|S_IWUSR; 838111773Sgad } 839111773Sgad 840111773Sgad /* 841111773Sgad * If newsyslog was run with a list of specific filenames, 842111773Sgad * then create a new worklist which has only those files in 843111773Sgad * it, picking up the rotation-rules for those files from 844208648Sgordon * the original filelist. 845111773Sgad * 846111773Sgad * XXX - Note that this will copy multiple rules for a single 847111773Sgad * logfile, if multiple entries are an exact match for 848111773Sgad * that file. That matches the historic behavior, but do 849111773Sgad * we want to continue to allow it? If so, it should 850111773Sgad * probably be handled more intelligently. 851111773Sgad */ 852208648Sgordon cmdlist = malloc(sizeof(struct cflist)); 853208648Sgordon if (cmdlist == NULL) 854208648Sgordon err(1, "malloc of cmdlist"); 855208648Sgordon STAILQ_INIT(cmdlist); 856208648Sgordon 857111773Sgad for (given = files; *given; ++given) { 858111773Sgad /* 859111773Sgad * First try to find exact-matches for this given file. 860111773Sgad */ 861112020Sgad gmatch = 0; 862208648Sgordon STAILQ_FOREACH(ent, filelist, cf_nextp) { 863111773Sgad if (strcmp(ent->log, *given) == 0) { 864111773Sgad gmatch++; 865111773Sgad dupent = init_entry(*given, ent); 866208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 867111773Sgad } 868111773Sgad } 869111773Sgad if (gmatch) { 870111773Sgad if (verbose > 2) 871111773Sgad printf("\t+ Matched entry %s\n", *given); 872111773Sgad continue; 873111773Sgad } 874111773Sgad 875111773Sgad /* 876111773Sgad * There was no exact-match for this given file, so look 877111773Sgad * for a "glob" entry which does match. 878111773Sgad */ 879112020Sgad gmatch = 0; 880112020Sgad if (verbose > 2 && globlist != NULL) 881112020Sgad printf("\t+ Checking globs for %s\n", *given); 882208648Sgordon STAILQ_FOREACH(ent, globlist, cf_nextp) { 883112020Sgad fnres = fnmatch(ent->log, *given, FNM_PATHNAME); 884112020Sgad if (verbose > 2) 885112020Sgad printf("\t+ = %d for pattern %s\n", fnres, 886112020Sgad ent->log); 887112020Sgad if (fnres == 0) { 888111773Sgad gmatch++; 889111773Sgad dupent = init_entry(*given, ent); 890112020Sgad /* This new entry is not a glob! */ 891111773Sgad dupent->flags &= ~CE_GLOB; 892208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 893111773Sgad /* Only allow a match to one glob-entry */ 894111773Sgad break; 895111773Sgad } 896111773Sgad } 897111773Sgad if (gmatch) { 898111773Sgad if (verbose > 2) 899111773Sgad printf("\t+ Matched %s via %s\n", *given, 900111773Sgad ent->log); 901111773Sgad continue; 902111773Sgad } 903111773Sgad 904111773Sgad /* 905111773Sgad * This given file was not found in any config file, so 906111773Sgad * add a worklist item based on the default entry. 907111773Sgad */ 908111773Sgad if (verbose > 2) 909111773Sgad printf("\t+ No entry matched %s (will use %s)\n", 910111773Sgad *given, DEFAULT_MARKER); 911111773Sgad dupent = init_entry(*given, defconf); 912111773Sgad /* Mark that it was *not* found in a config file */ 913111773Sgad dupent->def_cfg = 1; 914208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 915111773Sgad } 916111773Sgad 917111773Sgad /* 918112020Sgad * Free all the entries in the original work list, the list of 919112020Sgad * glob entries, and the default entry. 920111773Sgad */ 921208648Sgordon free_clist(filelist); 922208648Sgordon free_clist(globlist); 923112020Sgad free_entry(defconf); 924111773Sgad 925112020Sgad /* And finally, return a worklist which matches the given files. */ 926208648Sgordon return (cmdlist); 927111773Sgad} 928111773Sgad 929111773Sgad/* 930112020Sgad * Expand the list of entries with filename patterns, and add all files 931112020Sgad * which match those glob-entries onto the worklist. 932112020Sgad */ 933112020Sgadstatic void 934208648Sgordonexpand_globs(struct cflist *work_p, struct cflist *glob_p) 935112020Sgad{ 936161412Sdelphij int gmatch, gres; 937161412Sdelphij size_t i; 938112020Sgad char *mfname; 939208648Sgordon struct conf_entry *dupent, *ent, *globent; 940112020Sgad glob_t pglob; 941112020Sgad struct stat st_fm; 942112020Sgad 943112020Sgad /* 944112020Sgad * The worklist contains all fully-specified (non-GLOB) names. 945112020Sgad * 946112020Sgad * Now expand the list of filename-pattern (GLOB) entries into 947112020Sgad * a second list, which (by definition) will only match files 948112020Sgad * that already exist. Do not add a glob-related entry for any 949112020Sgad * file which already exists in the fully-specified list. 950112020Sgad */ 951208648Sgordon STAILQ_FOREACH(globent, glob_p, cf_nextp) { 952112020Sgad gres = glob(globent->log, GLOB_NOCHECK, NULL, &pglob); 953112020Sgad if (gres != 0) { 954112020Sgad warn("cannot expand pattern (%d): %s", gres, 955112020Sgad globent->log); 956112020Sgad continue; 957112020Sgad } 958112020Sgad 959112020Sgad if (verbose > 2) 960112020Sgad printf("\t+ Expanding pattern %s\n", globent->log); 961112020Sgad for (i = 0; i < pglob.gl_matchc; i++) { 962112020Sgad mfname = pglob.gl_pathv[i]; 963112020Sgad 964112020Sgad /* See if this file already has a specific entry. */ 965112020Sgad gmatch = 0; 966208648Sgordon STAILQ_FOREACH(ent, work_p, cf_nextp) { 967112020Sgad if (strcmp(mfname, ent->log) == 0) { 968112020Sgad gmatch++; 969112020Sgad break; 970112020Sgad } 971112020Sgad } 972112020Sgad if (gmatch) 973112020Sgad continue; 974112020Sgad 975112020Sgad /* Make sure the named matched is a file. */ 976112020Sgad gres = lstat(mfname, &st_fm); 977112020Sgad if (gres != 0) { 978112020Sgad /* Error on a file that glob() matched?!? */ 979112020Sgad warn("Skipping %s - lstat() error", mfname); 980112020Sgad continue; 981112020Sgad } 982112020Sgad if (!S_ISREG(st_fm.st_mode)) { 983112020Sgad /* We only rotate files! */ 984112020Sgad if (verbose > 2) 985112020Sgad printf("\t+ . skipping %s (!file)\n", 986112020Sgad mfname); 987112020Sgad continue; 988112020Sgad } 989112020Sgad 990112020Sgad if (verbose > 2) 991112020Sgad printf("\t+ . add file %s\n", mfname); 992112020Sgad dupent = init_entry(mfname, globent); 993112020Sgad /* This new entry is not a glob! */ 994112020Sgad dupent->flags &= ~CE_GLOB; 995208648Sgordon 996208648Sgordon /* Add to the worklist. */ 997208648Sgordon STAILQ_INSERT_TAIL(work_p, dupent, cf_nextp); 998112020Sgad } 999112020Sgad globfree(&pglob); 1000112020Sgad if (verbose > 2) 1001112020Sgad printf("\t+ Done with pattern %s\n", globent->log); 1002112020Sgad } 1003112020Sgad} 1004112020Sgad 1005112020Sgad/* 1006111773Sgad * Parse a configuration file and update a linked list of all the logs to 1007111773Sgad * process. 1008111773Sgad */ 1009111773Sgadstatic void 1010208648Sgordonparse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p, 1011208649Sgordon struct conf_entry *defconf_p, struct ilist *inclist) 1012111773Sgad{ 101359003Shm char line[BUFSIZ], *parse, *q; 1014107737Ssobomax char *cp, *errline, *group; 1015208648Sgordon struct conf_entry *working; 1016111781Sgad struct passwd *pwd; 101759003Shm struct group *grp; 1018208649Sgordon glob_t pglob; 1019120361Sgad int eol, ptm_opts, res, special; 1020208649Sgordon size_t i; 102113244Sgraichen 1022130165Sgad errline = NULL; 1023111773Sgad while (fgets(line, BUFSIZ, cf)) { 1024107737Ssobomax if ((line[0] == '\n') || (line[0] == '#') || 1025107737Ssobomax (strlen(line) == 0)) 102659003Shm continue; 1027130165Sgad if (errline != NULL) 1028130165Sgad free(errline); 102959003Shm errline = strdup(line); 1030107737Ssobomax for (cp = line + 1; *cp != '\0'; cp++) { 1031107737Ssobomax if (*cp != '#') 1032107737Ssobomax continue; 1033107737Ssobomax if (*(cp - 1) == '\\') { 1034107737Ssobomax strcpy(cp - 1, cp); 1035107737Ssobomax cp--; 1036107737Ssobomax continue; 1037107737Ssobomax } 1038107737Ssobomax *cp = '\0'; 1039107737Ssobomax break; 1040107737Ssobomax } 104160373Sdes 104260373Sdes q = parse = missing_field(sob(line), errline); 104360373Sdes parse = son(line); 104460373Sdes if (!*parse) 104580646Sobrien errx(1, "malformed line (missing fields):\n%s", 104680646Sobrien errline); 104760373Sdes *parse = '\0'; 104860373Sdes 1049130165Sgad /* 1050130165Sgad * Allow people to set debug options via the config file. 1051208648Sgordon * (NOTE: debug options are undocumented, and may disappear 1052130165Sgad * at any time, etc). 1053130165Sgad */ 1054130165Sgad if (strcasecmp(DEBUG_MARKER, q) == 0) { 1055130165Sgad q = parse = missing_field(sob(++parse), errline); 1056130165Sgad parse = son(parse); 1057130165Sgad if (!*parse) 1058130165Sgad warnx("debug line specifies no option:\n%s", 1059130165Sgad errline); 1060130165Sgad else { 1061130165Sgad *parse = '\0'; 1062130165Sgad parse_doption(q); 1063130165Sgad } 1064130165Sgad continue; 1065208649Sgordon } else if (strcasecmp(INCLUDE_MARKER, q) == 0) { 1066208649Sgordon if (verbose) 1067208649Sgordon printf("Found: %s", errline); 1068208649Sgordon q = parse = missing_field(sob(++parse), errline); 1069208649Sgordon parse = son(parse); 1070208649Sgordon if (!*parse) { 1071208649Sgordon warnx("include line missing argument:\n%s", 1072208649Sgordon errline); 1073208649Sgordon continue; 1074208649Sgordon } 1075208649Sgordon 1076208649Sgordon *parse = '\0'; 1077208649Sgordon 1078208649Sgordon if (isglobstr(q)) { 1079208649Sgordon res = glob(q, GLOB_NOCHECK, NULL, &pglob); 1080208649Sgordon if (res != 0) { 1081208649Sgordon warn("cannot expand pattern (%d): %s", 1082208649Sgordon res, q); 1083208649Sgordon continue; 1084208649Sgordon } 1085208649Sgordon 1086208649Sgordon if (verbose > 2) 1087208649Sgordon printf("\t+ Expanding pattern %s\n", q); 1088208649Sgordon 1089208649Sgordon for (i = 0; i < pglob.gl_matchc; i++) 1090208649Sgordon add_to_queue(pglob.gl_pathv[i], 1091208649Sgordon inclist); 1092208649Sgordon globfree(&pglob); 1093208649Sgordon } else 1094208649Sgordon add_to_queue(q, inclist); 1095208649Sgordon continue; 1096130165Sgad } 1097130165Sgad 1098112020Sgad special = 0; 1099111388Sgad working = init_entry(q, NULL); 1100111388Sgad if (strcasecmp(DEFAULT_MARKER, q) == 0) { 1101112020Sgad special = 1; 1102208648Sgordon if (defconf_p != NULL) { 1103111388Sgad warnx("Ignoring duplicate entry for %s!", q); 1104111388Sgad free_entry(working); 1105111388Sgad continue; 1106111388Sgad } 1107208648Sgordon defconf_p = working; 110859003Shm } 110913244Sgraichen 111059003Shm q = parse = missing_field(sob(++parse), errline); 111159003Shm parse = son(parse); 111225518Sbrian if (!*parse) 111380646Sobrien errx(1, "malformed line (missing fields):\n%s", 111480646Sobrien errline); 111559003Shm *parse = '\0'; 111659003Shm if ((group = strchr(q, ':')) != NULL || 111759003Shm (group = strrchr(q, '.')) != NULL) { 111859003Shm *group++ = '\0'; 111959003Shm if (*q) { 1120119102Sgad if (!(isnumberstr(q))) { 1121111781Sgad if ((pwd = getpwnam(q)) == NULL) 112259003Shm errx(1, 112380646Sobrien "error in config file; unknown user:\n%s", 112459003Shm errline); 1125111781Sgad working->uid = pwd->pw_uid; 112659003Shm } else 112759003Shm working->uid = atoi(q); 112859003Shm } else 1129111779Sgad working->uid = (uid_t)-1; 113013244Sgraichen 113159003Shm q = group; 113259003Shm if (*q) { 1133119102Sgad if (!(isnumberstr(q))) { 113459003Shm if ((grp = getgrnam(q)) == NULL) 113559003Shm errx(1, 113680646Sobrien "error in config file; unknown group:\n%s", 113759003Shm errline); 113859003Shm working->gid = grp->gr_gid; 113959003Shm } else 114059003Shm working->gid = atoi(q); 114159003Shm } else 1142111779Sgad working->gid = (gid_t)-1; 114313244Sgraichen 114459003Shm q = parse = missing_field(sob(++parse), errline); 114559003Shm parse = son(parse); 114659003Shm if (!*parse) 114780646Sobrien errx(1, "malformed line (missing fields):\n%s", 114880646Sobrien errline); 114959003Shm *parse = '\0'; 1150111779Sgad } else { 1151111779Sgad working->uid = (uid_t)-1; 1152111779Sgad working->gid = (gid_t)-1; 1153111779Sgad } 115459003Shm 115559003Shm if (!sscanf(q, "%o", &working->permissions)) 115659003Shm errx(1, "error in config file; bad permissions:\n%s", 115759003Shm errline); 115859003Shm 115959003Shm q = parse = missing_field(sob(++parse), errline); 116059003Shm parse = son(parse); 116125518Sbrian if (!*parse) 116280646Sobrien errx(1, "malformed line (missing fields):\n%s", 116380646Sobrien errline); 116459003Shm *parse = '\0'; 1165111400Sgad if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0) 1166111400Sgad errx(1, "error in config file; bad value for count of logs to save:\n%s", 116759003Shm errline); 116813244Sgraichen 116959003Shm q = parse = missing_field(sob(++parse), errline); 117059003Shm parse = son(parse); 117125518Sbrian if (!*parse) 117280646Sobrien errx(1, "malformed line (missing fields):\n%s", 117380646Sobrien errline); 117459003Shm *parse = '\0'; 1175114762Sgad if (isdigitch(*q)) 1176130165Sgad working->trsize = atoi(q); 1177130165Sgad else if (strcmp(q, "*") == 0) 1178130165Sgad working->trsize = -1; 1179114762Sgad else { 1180114762Sgad warnx("Invalid value of '%s' for 'size' in line:\n%s", 1181114762Sgad q, errline); 1182130165Sgad working->trsize = -1; 1183114762Sgad } 118459003Shm 118559003Shm working->flags = 0; 118659003Shm q = parse = missing_field(sob(++parse), errline); 118759003Shm parse = son(parse); 118825518Sbrian eol = !*parse; 118959003Shm *parse = '\0'; 119043071Swollman { 119159003Shm char *ep; 119259003Shm u_long ul; 119313244Sgraichen 119443071Swollman ul = strtoul(q, &ep, 10); 119543071Swollman if (ep == q) 119643071Swollman working->hours = 0; 119743071Swollman else if (*ep == '*') 119843071Swollman working->hours = -1; 119943071Swollman else if (ul > INT_MAX) 120043071Swollman errx(1, "interval is too large:\n%s", errline); 120143071Swollman else 120243071Swollman working->hours = ul; 120343071Swollman 1204120361Sgad if (*ep == '\0' || strcmp(ep, "*") == 0) 1205120361Sgad goto no_trimat; 1206120361Sgad if (*ep != '@' && *ep != '$') 120743071Swollman errx(1, "malformed interval/at:\n%s", errline); 1208120361Sgad 1209120361Sgad working->flags |= CE_TRIMAT; 1210120361Sgad working->trim_at = ptime_init(NULL); 1211120361Sgad ptm_opts = PTM_PARSE_ISO8601; 1212120361Sgad if (*ep == '$') 1213120361Sgad ptm_opts = PTM_PARSE_DWM; 1214120361Sgad ptm_opts |= PTM_PARSE_MATCHDOM; 1215120361Sgad res = ptime_relparse(working->trim_at, ptm_opts, 1216120361Sgad ptimeget_secs(timenow), ep + 1); 1217120361Sgad if (res == -2) 1218120361Sgad errx(1, "nonexistent time for 'at' value:\n%s", 1219120361Sgad errline); 1220120361Sgad else if (res < 0) 1221120361Sgad errx(1, "malformed 'at' value:\n%s", errline); 122243071Swollman } 1223120361Sgadno_trimat: 122443071Swollman 122525518Sbrian if (eol) 122659003Shm q = NULL; 122725518Sbrian else { 122859003Shm q = parse = sob(++parse); /* Optional field */ 122959003Shm parse = son(parse); 123059003Shm if (!*parse) 123159003Shm eol = 1; 123259003Shm *parse = '\0'; 123325518Sbrian } 123425443Sache 1235111768Sgad for (; q && *q && !isspacech(*q); q++) { 1236111768Sgad switch (tolowerch(*q)) { 1237111768Sgad case 'b': 123859003Shm working->flags |= CE_BINARY; 1239111768Sgad break; 1240114137Sgad case 'c': 1241111768Sgad /* 1242114137Sgad * XXX - Ick! Ugly! Remove ASAP! 1243114137Sgad * We want `c' and `C' for "create". But we 1244114137Sgad * will temporarily treat `c' as `g', because 1245114137Sgad * FreeBSD releases <= 4.8 have a typo of 1246114137Sgad * checking ('G' || 'c') for CE_GLOB. 1247111768Sgad */ 1248114137Sgad if (*q == 'c') { 1249114137Sgad warnx("Assuming 'g' for 'c' in flags for line:\n%s", 1250114137Sgad errline); 1251114137Sgad warnx("The 'c' flag will eventually mean 'CREATE'"); 1252114137Sgad working->flags |= CE_GLOB; 1253114137Sgad break; 1254114137Sgad } 1255114137Sgad working->flags |= CE_CREATE; 1256114137Sgad break; 1257130043Sgad case 'd': 1258130043Sgad working->flags |= CE_NODUMP; 1259130043Sgad break; 1260111768Sgad case 'g': 1261106905Ssobomax working->flags |= CE_GLOB; 1262111768Sgad break; 1263111768Sgad case 'j': 1264111768Sgad working->flags |= CE_BZCOMPACT; 1265111768Sgad break; 1266111768Sgad case 'n': 1267111768Sgad working->flags |= CE_NOSIGNAL; 1268111768Sgad break; 1269112003Sgad case 'u': 1270112003Sgad working->flags |= CE_SIGNALGROUP; 1271112003Sgad break; 1272111768Sgad case 'w': 1273160560Ssobomax /* Depreciated flag - keep for compatibility purposes */ 1274111768Sgad break; 1275111768Sgad case 'z': 1276111768Sgad working->flags |= CE_COMPACT; 1277111768Sgad break; 1278111768Sgad case '-': 1279111768Sgad break; 1280111781Sgad case 'f': /* Used by OpenBSD for "CE_FOLLOW" */ 1281111781Sgad case 'm': /* Used by OpenBSD for "CE_MONITOR" */ 1282111781Sgad case 'p': /* Used by NetBSD for "CE_PLAIN0" */ 1283111768Sgad default: 128480646Sobrien errx(1, "illegal flag in config file -- %c", 128580646Sobrien *q); 1286111768Sgad } 128759003Shm } 128859003Shm 128925518Sbrian if (eol) 129059003Shm q = NULL; 129125518Sbrian else { 129259003Shm q = parse = sob(++parse); /* Optional field */ 129359003Shm parse = son(parse); 129459003Shm if (!*parse) 129559003Shm eol = 1; 129659003Shm *parse = '\0'; 129725518Sbrian } 129825443Sache 129925443Sache working->pid_file = NULL; 130025443Sache if (q && *q) { 130125443Sache if (*q == '/') 130225443Sache working->pid_file = strdup(q); 130336817Sache else if (isdigit(*q)) 130436817Sache goto got_sig; 130559003Shm else 130680646Sobrien errx(1, 130780646Sobrien "illegal pid file or signal number in config file:\n%s", 130880646Sobrien errline); 130925443Sache } 131036817Sache if (eol) 131159003Shm q = NULL; 131236817Sache else { 131359003Shm q = parse = sob(++parse); /* Optional field */ 131459003Shm *(parse = son(parse)) = '\0'; 131536817Sache } 131636817Sache 131736817Sache working->sig = SIGHUP; 131836817Sache if (q && *q) { 131936817Sache if (isdigit(*q)) { 132059003Shm got_sig: 132136817Sache working->sig = atoi(q); 132236817Sache } else { 132359003Shm err_sig: 132480646Sobrien errx(1, 132580646Sobrien "illegal signal number in config file:\n%s", 132680646Sobrien errline); 132736817Sache } 132836817Sache if (working->sig < 1 || working->sig >= NSIG) 132936817Sache goto err_sig; 133036817Sache } 1331111768Sgad 1332111768Sgad /* 1333111768Sgad * Finish figuring out what pid-file to use (if any) in 1334111768Sgad * later processing if this logfile needs to be rotated. 1335111768Sgad */ 1336111768Sgad if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) { 1337111768Sgad /* 1338111768Sgad * This config-entry specified 'n' for nosignal, 1339111768Sgad * see if it also specified an explicit pid_file. 1340111768Sgad * This would be a pretty pointless combination. 1341111768Sgad */ 1342111768Sgad if (working->pid_file != NULL) { 1343111768Sgad warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s", 1344111768Sgad working->pid_file, errline); 1345111768Sgad free(working->pid_file); 1346111768Sgad working->pid_file = NULL; 1347111768Sgad } 1348111768Sgad } else if (working->pid_file == NULL) { 1349111768Sgad /* 1350111768Sgad * This entry did not specify the 'n' flag, which 1351111768Sgad * means it should signal syslogd unless it had 1352112003Sgad * specified some other pid-file (and obviously the 1353112003Sgad * syslog pid-file will not be for a process-group). 1354112003Sgad * Also, we should only try to notify syslog if we 1355112003Sgad * are root. 1356111768Sgad */ 1357112003Sgad if (working->flags & CE_SIGNALGROUP) { 1358112003Sgad warnx("Ignoring flag 'U' in line:\n%s", 1359112003Sgad errline); 1360112003Sgad working->flags &= ~CE_SIGNALGROUP; 1361112003Sgad } 1362111768Sgad if (needroot) 1363210407Sbrian working->pid_file = strdup(path_syslogpid); 1364111768Sgad } 1365111768Sgad 1366112020Sgad /* 1367112020Sgad * Add this entry to the appropriate list of entries, unless 1368112020Sgad * it was some kind of special entry (eg: <default>). 1369112020Sgad */ 1370112020Sgad if (special) { 1371112020Sgad ; /* Do not add to any list */ 1372112020Sgad } else if (working->flags & CE_GLOB) { 1373208648Sgordon STAILQ_INSERT_TAIL(glob_p, working, cf_nextp); 1374112020Sgad } else { 1375208648Sgordon STAILQ_INSERT_TAIL(work_p, working, cf_nextp); 1376112020Sgad } 1377130165Sgad } 1378130165Sgad if (errline != NULL) 137959003Shm free(errline); 138013244Sgraichen} 138113244Sgraichen 138259003Shmstatic char * 138359004Shmmissing_field(char *p, char *errline) 138413244Sgraichen{ 138580646Sobrien 138659003Shm if (!p || !*p) 138759003Shm errx(1, "missing field in config file:\n%s", errline); 138859003Shm return (p); 138913244Sgraichen} 139013244Sgraichen 1391208649Sgordon/* 1392210372Ssimon * In our sort we return it in the reverse of what qsort normally 1393210372Ssimon * would do, as we want the newest files first. If we have two 1394210372Ssimon * entries with the same time we don't really care about order. 1395210372Ssimon * 1396210372Ssimon * Support function for qsort() in delete_oldest_timelog(). 1397210372Ssimon */ 1398210372Ssimonstatic int 1399210372Ssimonoldlog_entry_compare(const void *a, const void *b) 1400210372Ssimon{ 1401210372Ssimon const struct oldlog_entry *ola = a, *olb = b; 1402210372Ssimon 1403210372Ssimon if (ola->t > olb->t) 1404210372Ssimon return (-1); 1405210372Ssimon else if (ola->t < olb->t) 1406210372Ssimon return (1); 1407210372Ssimon else 1408210372Ssimon return (0); 1409210372Ssimon} 1410210372Ssimon 1411210372Ssimon/* 1412210372Ssimon * Delete the oldest logfiles, when using time based filenames. 1413210372Ssimon */ 1414210372Ssimonstatic void 1415210372Ssimondelete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir) 1416210372Ssimon{ 1417210372Ssimon char *logfname, *s, *dir, errbuf[80]; 1418210372Ssimon int logcnt, max_logcnt, dirfd, i; 1419210372Ssimon struct oldlog_entry *oldlogs; 1420210372Ssimon size_t logfname_len; 1421210372Ssimon struct dirent *dp; 1422210372Ssimon const char *cdir; 1423210372Ssimon struct tm tm; 1424210372Ssimon DIR *dirp; 1425210372Ssimon 1426210372Ssimon oldlogs = malloc(MAX_OLDLOGS * sizeof(struct oldlog_entry)); 1427210372Ssimon max_logcnt = MAX_OLDLOGS; 1428210372Ssimon logcnt = 0; 1429210372Ssimon 1430210372Ssimon if (archive_dir != NULL && archive_dir[0] != '\0') 1431210372Ssimon cdir = archive_dir; 1432210372Ssimon else 1433210372Ssimon if ((cdir = dirname(ent->log)) == NULL) 1434210372Ssimon err(1, "dirname()"); 1435210372Ssimon if ((dir = strdup(cdir)) == NULL) 1436210372Ssimon err(1, "strdup()"); 1437210372Ssimon 1438210372Ssimon if ((s = basename(ent->log)) == NULL) 1439210372Ssimon err(1, "basename()"); 1440210372Ssimon if ((logfname = strdup(s)) == NULL) 1441210372Ssimon err(1, "strdup()"); 1442210372Ssimon logfname_len = strlen(logfname); 1443210372Ssimon if (strcmp(logfname, "/") == 0) 1444210372Ssimon errx(1, "Invalid log filename - became '/'"); 1445210372Ssimon 1446210372Ssimon if (verbose > 2) 1447210372Ssimon printf("Searching for old logs in %s\n", dir); 1448210372Ssimon 1449210372Ssimon /* First we create a 'list' of all archived logfiles */ 1450210372Ssimon if ((dirp = opendir(dir)) == NULL) 1451210372Ssimon err(1, "Cannot open log directory '%s'", dir); 1452210372Ssimon dirfd = dirfd(dirp); 1453210372Ssimon while ((dp = readdir(dirp)) != NULL) { 1454210372Ssimon if (dp->d_type != DT_REG) 1455210372Ssimon continue; 1456210372Ssimon 1457210372Ssimon /* Ignore everything but files with our logfile prefix */ 1458210372Ssimon if (strncmp(dp->d_name, logfname, logfname_len) != 0) 1459210372Ssimon continue; 1460210372Ssimon /* Ignore the actual non-rotated logfile */ 1461210372Ssimon if (dp->d_namlen == logfname_len) 1462210372Ssimon continue; 1463210372Ssimon /* 1464210372Ssimon * Make sure we created have found a logfile, so the 1465210372Ssimon * postfix is valid, IE format is: '.<time>(.[bg]z)?'. 1466210372Ssimon */ 1467210372Ssimon if (dp->d_name[logfname_len] != '.') { 1468210372Ssimon if (verbose) 1469210372Ssimon printf("Ignoring %s which has unexpected " 1470210372Ssimon "extension '%s'\n", dp->d_name, 1471210372Ssimon &dp->d_name[logfname_len]); 1472210372Ssimon continue; 1473210372Ssimon } 1474210372Ssimon if ((s = strptime(&dp->d_name[logfname_len + 1], 1475210372Ssimon timefnamefmt, &tm)) == NULL) { 1476210372Ssimon /* 1477210372Ssimon * We could special case "old" sequentially 1478210372Ssimon * named logfiles here, but we do not as that 1479210372Ssimon * would require special handling to decide 1480210372Ssimon * which one was the oldest compared to "new" 1481210372Ssimon * time based logfiles. 1482210372Ssimon */ 1483210372Ssimon if (verbose) 1484210372Ssimon printf("Ignoring %s which does not " 1485210372Ssimon "match time format\n", dp->d_name); 1486210372Ssimon continue; 1487210372Ssimon } 1488210372Ssimon if (*s != '\0' && !(strcmp(s, BZCOMPRESS_POSTFIX) == 0 || 1489210372Ssimon strcmp(s, COMPRESS_POSTFIX) == 0)) { 1490210372Ssimon if (verbose) 1491210372Ssimon printf("Ignoring %s which has unexpected " 1492210372Ssimon "extension '%s'\n", dp->d_name, s); 1493210372Ssimon continue; 1494210372Ssimon } 1495210372Ssimon 1496210372Ssimon /* 1497210372Ssimon * We should now have old an old rotated logfile, so 1498210372Ssimon * add it to the 'list'. 1499210372Ssimon */ 1500210372Ssimon if ((oldlogs[logcnt].t = timegm(&tm)) == -1) 1501210372Ssimon err(1, "Could not convert time string to time value"); 1502210372Ssimon if ((oldlogs[logcnt].fname = strdup(dp->d_name)) == NULL) 1503210372Ssimon err(1, "strdup()"); 1504210372Ssimon logcnt++; 1505210372Ssimon 1506210372Ssimon /* 1507210372Ssimon * It is very unlikely we ever run out of space in the 1508210372Ssimon * logfile array from the default size, but lets 1509210372Ssimon * handle it anyway... 1510210372Ssimon */ 1511210372Ssimon if (logcnt >= max_logcnt) { 1512210372Ssimon max_logcnt *= 4; 1513210372Ssimon /* Detect integer overflow */ 1514210372Ssimon if (max_logcnt < logcnt) 1515210372Ssimon errx(1, "Too many old logfiles found"); 1516210372Ssimon oldlogs = realloc(oldlogs, 1517210372Ssimon max_logcnt * sizeof(struct oldlog_entry)); 1518210372Ssimon if (oldlogs == NULL) 1519210372Ssimon err(1, "realloc()"); 1520210372Ssimon } 1521210372Ssimon } 1522210372Ssimon 1523210372Ssimon /* Second, if needed we delete oldest archived logfiles */ 1524210372Ssimon if (logcnt > 0 && logcnt >= ent->numlogs && ent->numlogs > 1) { 1525210372Ssimon oldlogs = realloc(oldlogs, logcnt * 1526210372Ssimon sizeof(struct oldlog_entry)); 1527210372Ssimon if (oldlogs == NULL) 1528210372Ssimon err(1, "realloc()"); 1529210372Ssimon 1530210372Ssimon /* 1531210372Ssimon * We now sort the logs in the order of newest to 1532210372Ssimon * oldest. That way we can simply skip over the 1533210372Ssimon * number of records we want to keep. 1534210372Ssimon */ 1535210372Ssimon qsort(oldlogs, logcnt, sizeof(struct oldlog_entry), 1536210372Ssimon oldlog_entry_compare); 1537210372Ssimon for (i = ent->numlogs - 1; i < logcnt; i++) { 1538210372Ssimon if (noaction) 1539210372Ssimon printf("\trm -f %s/%s\n", dir, 1540210372Ssimon oldlogs[i].fname); 1541210372Ssimon else if (unlinkat(dirfd, oldlogs[i].fname, 0) != 0) { 1542210372Ssimon snprintf(errbuf, sizeof(errbuf), 1543210372Ssimon "Could not delet old logfile '%s'", 1544210372Ssimon oldlogs[i].fname); 1545210372Ssimon perror(errbuf); 1546210372Ssimon } 1547210372Ssimon } 1548210372Ssimon } else if (verbose > 1) 1549210372Ssimon printf("No old logs to delete for logfile %s\n", ent->log); 1550210372Ssimon 1551210372Ssimon /* Third, cleanup */ 1552210372Ssimon closedir(dirp); 1553210372Ssimon for (i = 0; i < logcnt; i++) { 1554210372Ssimon assert(oldlogs[i].fname != NULL); 1555210372Ssimon free(oldlogs[i].fname); 1556210372Ssimon } 1557210372Ssimon free(oldlogs); 1558210372Ssimon free(logfname); 1559210372Ssimon free(dir); 1560210372Ssimon} 1561210372Ssimon 1562210372Ssimon/* 1563208649Sgordon * Only add to the queue if the file hasn't already been added. This is 1564208649Sgordon * done to prevent circular include loops. 1565208649Sgordon */ 1566208649Sgordonstatic void 1567208649Sgordonadd_to_queue(const char *fname, struct ilist *inclist) 1568208649Sgordon{ 1569208649Sgordon struct include_entry *inc; 1570208649Sgordon 1571208649Sgordon STAILQ_FOREACH(inc, inclist, inc_nextp) { 1572208649Sgordon if (strcmp(fname, inc->file) == 0) { 1573208649Sgordon warnx("duplicate include detected: %s", fname); 1574208649Sgordon return; 1575208649Sgordon } 1576208649Sgordon } 1577208649Sgordon 1578208649Sgordon inc = malloc(sizeof(struct include_entry)); 1579208649Sgordon if (inc == NULL) 1580208649Sgordon err(1, "malloc of inc"); 1581208649Sgordon inc->file = strdup(fname); 1582208649Sgordon 1583208649Sgordon if (verbose > 2) 1584208649Sgordon printf("\t+ Adding %s to the processing queue.\n", fname); 1585208649Sgordon 1586208649Sgordon STAILQ_INSERT_TAIL(inclist, inc, inc_nextp); 1587208649Sgordon} 1588208649Sgordon 1589130165Sgadstatic fk_entry 1590130165Sgaddo_rotate(const struct conf_entry *ent) 159113244Sgraichen{ 159271299Sjedgar char dirpart[MAXPATHLEN], namepart[MAXPATHLEN]; 159371299Sjedgar char file1[MAXPATHLEN], file2[MAXPATHLEN]; 159471299Sjedgar char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN]; 159580638Sobrien char jfile1[MAXPATHLEN]; 1596210372Ssimon char datetimestr[30]; 1597159968Sgad int flags, numlogs_c; 1598130165Sgad fk_entry free_or_keep; 1599159968Sgad struct sigwork_entry *swork; 160059003Shm struct stat st; 1601210372Ssimon struct tm tm; 1602210372Ssimon time_t now; 160313244Sgraichen 1604119927Sgad flags = ent->flags; 1605130165Sgad free_or_keep = FREE_ENT; 1606119927Sgad 160759004Shm if (archtodir) { 160859004Shm char *p; 160913244Sgraichen 161059004Shm /* build complete name of archive directory into dirpart */ 161159004Shm if (*archdirname == '/') { /* absolute */ 161271299Sjedgar strlcpy(dirpart, archdirname, sizeof(dirpart)); 161359004Shm } else { /* relative */ 161459004Shm /* get directory part of logfile */ 1615119927Sgad strlcpy(dirpart, ent->log, sizeof(dirpart)); 161659004Shm if ((p = rindex(dirpart, '/')) == NULL) 161759004Shm dirpart[0] = '\0'; 161859004Shm else 161959004Shm *(p + 1) = '\0'; 162071299Sjedgar strlcat(dirpart, archdirname, sizeof(dirpart)); 162159004Shm } 162259004Shm 162359004Shm /* check if archive directory exists, if not, create it */ 162459004Shm if (lstat(dirpart, &st)) 1625114137Sgad createdir(ent, dirpart); 162659004Shm 162759004Shm /* get filename part of logfile */ 1628119927Sgad if ((p = rindex(ent->log, '/')) == NULL) 1629119927Sgad strlcpy(namepart, ent->log, sizeof(namepart)); 163059004Shm else 163171299Sjedgar strlcpy(namepart, p + 1, sizeof(namepart)); 163259004Shm 163359004Shm /* name of oldest log */ 163480646Sobrien (void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart, 1635119927Sgad namepart, ent->numlogs); 163659004Shm } else { 1637210372Ssimon /* 1638210372Ssimon * Tell delete_oldest_timelog() we are not using an 1639210372Ssimon * archive dir. 1640210372Ssimon */ 1641210372Ssimon dirpart[0] = '\0'; 1642210372Ssimon 164359004Shm /* name of oldest log */ 1644119927Sgad (void) snprintf(file1, sizeof(file1), "%s.%d", ent->log, 1645119927Sgad ent->numlogs); 1646210372Ssimon } 1647210372Ssimon 1648210372Ssimon /* Delete old logs */ 1649210372Ssimon if (timefnamefmt != NULL) 1650210372Ssimon delete_oldest_timelog(ent, dirpart); 1651210372Ssimon else { 1652210372Ssimon /* name of oldest log */ 165371299Sjedgar (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, 165471299Sjedgar COMPRESS_POSTFIX); 165580638Sobrien snprintf(jfile1, sizeof(jfile1), "%s%s", file1, 165680638Sobrien BZCOMPRESS_POSTFIX); 165759004Shm 1658210372Ssimon if (noaction) { 1659210372Ssimon printf("\trm -f %s\n", file1); 1660210372Ssimon printf("\trm -f %s\n", zfile1); 1661210372Ssimon printf("\trm -f %s\n", jfile1); 1662210372Ssimon } else { 1663210372Ssimon (void) unlink(file1); 1664210372Ssimon (void) unlink(zfile1); 1665210372Ssimon (void) unlink(jfile1); 1666210372Ssimon } 166759003Shm } 166813244Sgraichen 1669210372Ssimon if (timefnamefmt != NULL) { 1670210372Ssimon /* If time functions fails we can't really do any sensible */ 1671210372Ssimon if (time(&now) == (time_t)-1 || 1672210372Ssimon localtime_r(&now, &tm) == NULL) 1673210372Ssimon bzero(&tm, sizeof(tm)); 1674210372Ssimon 1675210372Ssimon strftime(datetimestr, sizeof(datetimestr), timefnamefmt, &tm); 1676210372Ssimon if (archtodir) 1677210372Ssimon (void) snprintf(file1, sizeof(file1), "%s/%s.%s", 1678210372Ssimon dirpart, namepart, datetimestr); 1679210372Ssimon else 1680210372Ssimon (void) snprintf(file1, sizeof(file1), "%s.%s", 1681210372Ssimon ent->log, datetimestr); 1682210372Ssimon 1683210372Ssimon /* Don't run the code to move down logs */ 1684210372Ssimon numlogs_c = 0; 1685210372Ssimon } else 1686210372Ssimon numlogs_c = ent->numlogs; /* copy for countdown */ 1687210372Ssimon 168859003Shm /* Move down log files */ 1689119927Sgad while (numlogs_c--) { 169059004Shm 169171299Sjedgar (void) strlcpy(file2, file1, sizeof(file2)); 169259004Shm 169359004Shm if (archtodir) 169480646Sobrien (void) snprintf(file1, sizeof(file1), "%s/%s.%d", 1695119927Sgad dirpart, namepart, numlogs_c); 169659004Shm else 1697119927Sgad (void) snprintf(file1, sizeof(file1), "%s.%d", 1698119927Sgad ent->log, numlogs_c); 169959004Shm 170071299Sjedgar (void) strlcpy(zfile1, file1, sizeof(zfile1)); 170171299Sjedgar (void) strlcpy(zfile2, file2, sizeof(zfile2)); 170259003Shm if (lstat(file1, &st)) { 170380646Sobrien (void) strlcat(zfile1, COMPRESS_POSTFIX, 170480646Sobrien sizeof(zfile1)); 170580646Sobrien (void) strlcat(zfile2, COMPRESS_POSTFIX, 170680646Sobrien sizeof(zfile2)); 170780638Sobrien if (lstat(zfile1, &st)) { 170880638Sobrien strlcpy(zfile1, file1, sizeof(zfile1)); 170980638Sobrien strlcpy(zfile2, file2, sizeof(zfile2)); 171080638Sobrien strlcat(zfile1, BZCOMPRESS_POSTFIX, 171180638Sobrien sizeof(zfile1)); 171280638Sobrien strlcat(zfile2, BZCOMPRESS_POSTFIX, 171380638Sobrien sizeof(zfile2)); 171480638Sobrien if (lstat(zfile1, &st)) 171580638Sobrien continue; 171680638Sobrien } 171759003Shm } 1718130165Sgad if (noaction) 1719111967Sgad printf("\tmv %s %s\n", zfile1, zfile2); 1720130165Sgad else { 1721130165Sgad /* XXX - Ought to be checking for failure! */ 1722130165Sgad (void)rename(zfile1, zfile2); 172359003Shm } 1724130165Sgad change_attrs(zfile2, ent); 172559003Shm } 172613244Sgraichen 1727130038Sgad if (ent->numlogs > 0) { 1728129974Sgad if (noaction) { 1729130038Sgad /* 1730130038Sgad * Note that savelog() may succeed with using link() 1731130038Sgad * for the archtodir case, but there is no good way 1732130038Sgad * of knowing if it will when doing "noaction", so 1733130038Sgad * here we claim that it will have to do a copy... 1734130038Sgad */ 1735130038Sgad if (archtodir) 1736130038Sgad printf("\tcp %s %s\n", ent->log, file1); 1737130038Sgad else 1738130038Sgad printf("\tln %s %s\n", ent->log, file1); 1739130165Sgad } else { 1740130038Sgad if (!(flags & CE_BINARY)) { 1741130038Sgad /* Report the trimming to the old log */ 1742130038Sgad log_trim(ent->log, ent); 1743130038Sgad } 1744130038Sgad savelog(ent->log, file1); 174559004Shm } 1746130165Sgad change_attrs(file1, ent); 174718075Sjkh } 174818075Sjkh 1749130038Sgad /* Create the new log file and move it into place */ 1750130038Sgad if (noaction) 1751111781Sgad printf("Start new log...\n"); 1752130038Sgad createlog(ent); 175325443Sache 1754111966Sgad /* 1755130167Sgad * Save all signalling and file-compression to be done after log 1756130167Sgad * files from all entries have been rotated. This way any one 1757130167Sgad * process will not be sent the same signal multiple times when 1758130167Sgad * multiple log files had to be rotated. 1759130167Sgad */ 1760159968Sgad swork = NULL; 1761159968Sgad if (ent->pid_file != NULL) 1762159968Sgad swork = save_sigwork(ent); 1763159968Sgad if (ent->numlogs > 0 && (flags & (CE_COMPACT | CE_BZCOMPACT))) { 1764159968Sgad /* 1765159968Sgad * The zipwork_entry will include a pointer to this 1766159968Sgad * conf_entry, so the conf_entry should not be freed. 1767159968Sgad */ 1768159968Sgad free_or_keep = KEEP_ENT; 1769159968Sgad save_zipwork(ent, swork, ent->fsize, file1); 1770130167Sgad } 1771130167Sgad 1772130165Sgad return (free_or_keep); 177313244Sgraichen} 177413244Sgraichen 1775130167Sgadstatic void 1776130167Sgaddo_sigwork(struct sigwork_entry *swork) 1777130167Sgad{ 1778130204Sgad struct sigwork_entry *nextsig; 1779130204Sgad int kres, secs; 1780130167Sgad 1781130167Sgad if (!(swork->sw_pidok) || swork->sw_pid == 0) 1782130167Sgad return; /* no work to do... */ 1783130167Sgad 1784130167Sgad /* 1785130167Sgad * If nosignal (-s) was specified, then do not signal any process. 1786130167Sgad * Note that a nosignal request triggers a warning message if the 1787130167Sgad * rotated logfile needs to be compressed, *unless* -R was also 1788130167Sgad * specified. We assume that an `-sR' request came from a process 1789130167Sgad * which writes to the logfile, and as such, we assume that process 1790130167Sgad * has already made sure the logfile is not presently in use. This 1791130167Sgad * just sets swork->sw_pidok to a special value, and do_zipwork 1792130167Sgad * will print any necessary warning(s). 1793130167Sgad */ 1794130167Sgad if (nosignal) { 1795130167Sgad if (!rotatereq) 1796130167Sgad swork->sw_pidok = -1; 1797130167Sgad return; 1798130167Sgad } 1799130167Sgad 1800130204Sgad /* 1801130204Sgad * Compute the pause between consecutive signals. Use a longer 1802130204Sgad * sleep time if we will be sending two signals to the same 1803130204Sgad * deamon or process-group. 1804130204Sgad */ 1805130204Sgad secs = 0; 1806130204Sgad nextsig = SLIST_NEXT(swork, sw_nextp); 1807130204Sgad if (nextsig != NULL) { 1808130204Sgad if (swork->sw_pid == nextsig->sw_pid) 1809130204Sgad secs = 10; 1810130204Sgad else 1811130204Sgad secs = 1; 1812130204Sgad } 1813130204Sgad 1814130167Sgad if (noaction) { 1815130204Sgad printf("\tkill -%d %d \t\t# %s\n", swork->sw_signum, 1816130204Sgad (int)swork->sw_pid, swork->sw_fname); 1817130204Sgad if (secs > 0) 1818130204Sgad printf("\tsleep %d\n", secs); 1819130167Sgad return; 1820130167Sgad } 1821130167Sgad 1822130167Sgad kres = kill(swork->sw_pid, swork->sw_signum); 1823130167Sgad if (kres != 0) { 1824130167Sgad /* 1825130167Sgad * Assume that "no such process" (ESRCH) is something 1826130167Sgad * to warn about, but is not an error. Presumably the 1827130167Sgad * process which writes to the rotated log file(s) is 1828130167Sgad * gone, in which case we should have no problem with 1829130167Sgad * compressing the rotated log file(s). 1830130167Sgad */ 1831130167Sgad if (errno != ESRCH) 1832130167Sgad swork->sw_pidok = 0; 1833130167Sgad warn("can't notify %s, pid %d", swork->sw_pidtype, 1834130167Sgad (int)swork->sw_pid); 1835130167Sgad } else { 1836130204Sgad if (verbose) 1837130204Sgad printf("Notified %s pid %d = %s\n", swork->sw_pidtype, 1838130204Sgad (int)swork->sw_pid, swork->sw_fname); 1839130204Sgad if (secs > 0) { 1840130204Sgad if (verbose) 1841130204Sgad printf("Pause %d second(s) between signals\n", 1842130204Sgad secs); 1843130204Sgad sleep(secs); 1844130167Sgad } 1845130167Sgad } 1846130167Sgad} 1847130167Sgad 1848130167Sgadstatic void 1849130167Sgaddo_zipwork(struct zipwork_entry *zwork) 1850130167Sgad{ 1851130167Sgad const char *pgm_name, *pgm_path; 1852154566Sgad int errsav, fcount, zstatus; 1853130167Sgad pid_t pidzip, wpid; 1854130167Sgad char zresult[MAXPATHLEN]; 1855130167Sgad 1856130167Sgad pgm_path = NULL; 1857130167Sgad strlcpy(zresult, zwork->zw_fname, sizeof(zresult)); 1858130167Sgad if (zwork != NULL && zwork->zw_conf != NULL) { 1859130167Sgad if (zwork->zw_conf->flags & CE_COMPACT) { 1860130167Sgad pgm_path = _PATH_GZIP; 1861130167Sgad strlcat(zresult, COMPRESS_POSTFIX, sizeof(zresult)); 1862130167Sgad } else if (zwork->zw_conf->flags & CE_BZCOMPACT) { 1863130167Sgad pgm_path = _PATH_BZIP2; 1864130167Sgad strlcat(zresult, BZCOMPRESS_POSTFIX, sizeof(zresult)); 1865130167Sgad } 1866130167Sgad } 1867130167Sgad if (pgm_path == NULL) { 1868130167Sgad warnx("invalid entry for %s in do_zipwork", zwork->zw_fname); 1869130167Sgad return; 1870130167Sgad } 1871154566Sgad pgm_name = strrchr(pgm_path, '/'); 1872154566Sgad if (pgm_name == NULL) 1873154566Sgad pgm_name = pgm_path; 1874154566Sgad else 1875154566Sgad pgm_name++; 1876130167Sgad 1877130167Sgad if (zwork->zw_swork != NULL && zwork->zw_swork->sw_pidok <= 0) { 1878130167Sgad warnx( 1879130167Sgad "log %s not compressed because daemon(s) not notified", 1880130167Sgad zwork->zw_fname); 1881130167Sgad change_attrs(zwork->zw_fname, zwork->zw_conf); 1882130167Sgad return; 1883130167Sgad } 1884130167Sgad 1885130167Sgad if (noaction) { 1886130167Sgad printf("\t%s %s\n", pgm_name, zwork->zw_fname); 1887130167Sgad change_attrs(zresult, zwork->zw_conf); 1888130167Sgad return; 1889130167Sgad } 1890130167Sgad 1891154566Sgad fcount = 1; 1892130167Sgad pidzip = fork(); 1893154566Sgad while (pidzip < 0) { 1894154566Sgad /* 1895154566Sgad * The fork failed. If the failure was due to a temporary 1896154566Sgad * problem, then wait a short time and try it again. 1897154566Sgad */ 1898154566Sgad errsav = errno; 1899154566Sgad warn("fork() for `%s %s'", pgm_name, zwork->zw_fname); 1900154566Sgad if (errsav != EAGAIN || fcount > 5) 1901154566Sgad errx(1, "Exiting..."); 1902154566Sgad sleep(fcount * 12); 1903154566Sgad fcount++; 1904154566Sgad pidzip = fork(); 1905154566Sgad } 1906154566Sgad if (!pidzip) { 1907130167Sgad /* The child process executes the compression command */ 1908130167Sgad execl(pgm_path, pgm_path, "-f", zwork->zw_fname, (char *)0); 1909154566Sgad err(1, "execl(`%s -f %s')", pgm_path, zwork->zw_fname); 1910130167Sgad } 1911130167Sgad 1912130167Sgad wpid = waitpid(pidzip, &zstatus, 0); 1913130167Sgad if (wpid == -1) { 1914154566Sgad /* XXX - should this be a fatal error? */ 1915130167Sgad warn("%s: waitpid(%d)", pgm_path, pidzip); 1916130167Sgad return; 1917130167Sgad } 1918130167Sgad if (!WIFEXITED(zstatus)) { 1919154566Sgad warnx("`%s -f %s' did not terminate normally", pgm_name, 1920154566Sgad zwork->zw_fname); 1921130167Sgad return; 1922130167Sgad } 1923130167Sgad if (WEXITSTATUS(zstatus)) { 1924154566Sgad warnx("`%s -f %s' terminated with a non-zero status (%d)", 1925154566Sgad pgm_name, zwork->zw_fname, WEXITSTATUS(zstatus)); 1926130167Sgad return; 1927130167Sgad } 1928130167Sgad 1929130167Sgad /* Compression was successful, set file attributes on the result. */ 1930130167Sgad change_attrs(zresult, zwork->zw_conf); 1931130167Sgad} 1932130167Sgad 1933130167Sgad/* 1934130167Sgad * Save information on any process we need to signal. Any single 1935130167Sgad * process may need to be sent different signal-values for different 1936130167Sgad * log files, but usually a single signal-value will cause the process 1937130167Sgad * to close and re-open all of it's log files. 1938130167Sgad */ 1939130167Sgadstatic struct sigwork_entry * 1940130167Sgadsave_sigwork(const struct conf_entry *ent) 1941130167Sgad{ 1942130167Sgad struct sigwork_entry *sprev, *stmp; 1943130167Sgad int ndiff; 1944130167Sgad size_t tmpsiz; 1945130167Sgad 1946130167Sgad sprev = NULL; 1947130167Sgad ndiff = 1; 1948130167Sgad SLIST_FOREACH(stmp, &swhead, sw_nextp) { 1949130167Sgad ndiff = strcmp(ent->pid_file, stmp->sw_fname); 1950130167Sgad if (ndiff > 0) 1951130167Sgad break; 1952130167Sgad if (ndiff == 0) { 1953130167Sgad if (ent->sig == stmp->sw_signum) 1954130167Sgad break; 1955130167Sgad if (ent->sig > stmp->sw_signum) { 1956130167Sgad ndiff = 1; 1957130167Sgad break; 1958130167Sgad } 1959130167Sgad } 1960130167Sgad sprev = stmp; 1961130167Sgad } 1962130167Sgad if (stmp != NULL && ndiff == 0) 1963130167Sgad return (stmp); 1964130167Sgad 1965130167Sgad tmpsiz = sizeof(struct sigwork_entry) + strlen(ent->pid_file) + 1; 1966130167Sgad stmp = malloc(tmpsiz); 1967130167Sgad set_swpid(stmp, ent); 1968130167Sgad stmp->sw_signum = ent->sig; 1969130167Sgad strcpy(stmp->sw_fname, ent->pid_file); 1970130167Sgad if (sprev == NULL) 1971130167Sgad SLIST_INSERT_HEAD(&swhead, stmp, sw_nextp); 1972130167Sgad else 1973130167Sgad SLIST_INSERT_AFTER(sprev, stmp, sw_nextp); 1974130167Sgad return (stmp); 1975130167Sgad} 1976130167Sgad 1977130167Sgad/* 1978130167Sgad * Save information on any file we need to compress. We may see the same 1979130167Sgad * file multiple times, so check the full list to avoid duplicates. The 1980130167Sgad * list itself is sorted smallest-to-largest, because that's the order we 1981130167Sgad * want to compress the files. If the partition is very low on disk space, 1982130167Sgad * then the smallest files are the most likely to compress, and compressing 1983130167Sgad * them first will free up more space for the larger files. 1984130167Sgad */ 1985130167Sgadstatic struct zipwork_entry * 1986130167Sgadsave_zipwork(const struct conf_entry *ent, const struct sigwork_entry *swork, 1987130167Sgad int zsize, const char *zipfname) 1988130167Sgad{ 1989130167Sgad struct zipwork_entry *zprev, *ztmp; 1990130167Sgad int ndiff; 1991130167Sgad size_t tmpsiz; 1992130167Sgad 1993130167Sgad /* Compute the size if the caller did not know it. */ 1994130167Sgad if (zsize < 0) 1995130167Sgad zsize = sizefile(zipfname); 1996130167Sgad 1997130167Sgad zprev = NULL; 1998130167Sgad ndiff = 1; 1999130167Sgad SLIST_FOREACH(ztmp, &zwhead, zw_nextp) { 2000130707Sgad ndiff = strcmp(zipfname, ztmp->zw_fname); 2001130167Sgad if (ndiff == 0) 2002130167Sgad break; 2003130167Sgad if (zsize > ztmp->zw_fsize) 2004130167Sgad zprev = ztmp; 2005130167Sgad } 2006130167Sgad if (ztmp != NULL && ndiff == 0) 2007130167Sgad return (ztmp); 2008130167Sgad 2009130167Sgad tmpsiz = sizeof(struct zipwork_entry) + strlen(zipfname) + 1; 2010130167Sgad ztmp = malloc(tmpsiz); 2011130167Sgad ztmp->zw_conf = ent; 2012130167Sgad ztmp->zw_swork = swork; 2013130167Sgad ztmp->zw_fsize = zsize; 2014130167Sgad strcpy(ztmp->zw_fname, zipfname); 2015130167Sgad if (zprev == NULL) 2016130167Sgad SLIST_INSERT_HEAD(&zwhead, ztmp, zw_nextp); 2017130167Sgad else 2018130167Sgad SLIST_INSERT_AFTER(zprev, ztmp, zw_nextp); 2019130167Sgad return (ztmp); 2020130167Sgad} 2021130167Sgad 2022130167Sgad/* Send a signal to the pid specified by pidfile */ 2023130167Sgadstatic void 2024130167Sgadset_swpid(struct sigwork_entry *swork, const struct conf_entry *ent) 2025130167Sgad{ 2026130167Sgad FILE *f; 2027130167Sgad long minok, maxok, rval; 2028130167Sgad char *endp, *linep, line[BUFSIZ]; 2029130167Sgad 2030130167Sgad minok = MIN_PID; 2031130167Sgad maxok = MAX_PID; 2032130167Sgad swork->sw_pidok = 0; 2033130167Sgad swork->sw_pid = 0; 2034130167Sgad swork->sw_pidtype = "daemon"; 2035130167Sgad if (ent->flags & CE_SIGNALGROUP) { 2036130167Sgad /* 2037130167Sgad * If we are expected to signal a process-group when 2038130167Sgad * rotating this logfile, then the value read in should 2039130167Sgad * be the negative of a valid process ID. 2040130167Sgad */ 2041130167Sgad minok = -MAX_PID; 2042130167Sgad maxok = -MIN_PID; 2043130167Sgad swork->sw_pidtype = "process-group"; 2044130167Sgad } 2045130167Sgad 2046130167Sgad f = fopen(ent->pid_file, "r"); 2047130167Sgad if (f == NULL) { 2048202668Sdelphij if (errno == ENOENT && enforcepid == 0) { 2049200806Sdelphij /* 2050200806Sdelphij * Warn if the PID file doesn't exist, but do 2051200806Sdelphij * not consider it an error. Most likely it 2052200806Sdelphij * means the process has been terminated, 2053200806Sdelphij * so it should be safe to rotate any log 2054200806Sdelphij * files that the process would have been using. 2055200806Sdelphij */ 2056200806Sdelphij swork->sw_pidok = 1; 2057200806Sdelphij warnx("pid file doesn't exist: %s", ent->pid_file); 2058200806Sdelphij } else 2059200806Sdelphij warn("can't open pid file: %s", ent->pid_file); 2060130167Sgad return; 2061130167Sgad } 2062130167Sgad 2063130167Sgad if (fgets(line, BUFSIZ, f) == NULL) { 2064130167Sgad /* 2065130167Sgad * Warn if the PID file is empty, but do not consider 2066130167Sgad * it an error. Most likely it means the process has 2067130167Sgad * has terminated, so it should be safe to rotate any 2068130167Sgad * log files that the process would have been using. 2069130167Sgad */ 2070202668Sdelphij if (feof(f) && enforcepid == 0) { 2071130167Sgad swork->sw_pidok = 1; 2072130167Sgad warnx("pid file is empty: %s", ent->pid_file); 2073130167Sgad } else 2074130167Sgad warn("can't read from pid file: %s", ent->pid_file); 2075130167Sgad (void)fclose(f); 2076130167Sgad return; 2077130167Sgad } 2078130167Sgad (void)fclose(f); 2079130167Sgad 2080130167Sgad errno = 0; 2081130167Sgad linep = line; 2082130167Sgad while (*linep == ' ') 2083130167Sgad linep++; 2084130167Sgad rval = strtol(linep, &endp, 10); 2085130167Sgad if (*endp != '\0' && !isspacech(*endp)) { 2086130167Sgad warnx("pid file does not start with a valid number: %s", 2087130167Sgad ent->pid_file); 2088130167Sgad } else if (rval < minok || rval > maxok) { 2089130167Sgad warnx("bad value '%ld' for process number in %s", 2090130167Sgad rval, ent->pid_file); 2091130167Sgad if (verbose) 2092130167Sgad warnx("\t(expecting value between %ld and %ld)", 2093130167Sgad minok, maxok); 2094130167Sgad } else { 2095130167Sgad swork->sw_pidok = 1; 2096130167Sgad swork->sw_pid = rval; 2097130167Sgad } 2098130167Sgad 2099130167Sgad return; 2100130167Sgad} 2101130167Sgad 210213244Sgraichen/* Log the fact that the logs were turned over */ 210359004Shmstatic int 2104119926Sgadlog_trim(const char *logname, const struct conf_entry *log_ent) 210513244Sgraichen{ 210659003Shm FILE *f; 2107111388Sgad const char *xtra; 210859003Shm 2109119926Sgad if ((f = fopen(logname, "a")) == NULL) 211059003Shm return (-1); 2111111388Sgad xtra = ""; 2112111768Sgad if (log_ent->def_cfg) 2113111388Sgad xtra = " using <default> rule"; 2114114137Sgad if (log_ent->firstcreate) 2115114137Sgad fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n", 2116114137Sgad daytime, hostname, (int) getpid(), xtra); 2117114137Sgad else if (log_ent->r_reason != NULL) 2118111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n", 2119111772Sgad daytime, hostname, (int) getpid(), log_ent->r_reason, xtra); 2120111772Sgad else 2121111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n", 2122111772Sgad daytime, hostname, (int) getpid(), xtra); 212359003Shm if (fclose(f) == EOF) 2124127858Scharnier err(1, "log_trim: fclose"); 212559003Shm return (0); 212613244Sgraichen} 212713244Sgraichen 212813244Sgraichen/* Return size in kilobytes of a file */ 212959004Shmstatic int 2130130165Sgadsizefile(const char *file) 213113244Sgraichen{ 213259003Shm struct stat sb; 213313244Sgraichen 213459003Shm if (stat(file, &sb) < 0) 213559003Shm return (-1); 213659003Shm return (kbytes(dbtob(sb.st_blocks))); 213713244Sgraichen} 213813244Sgraichen 213913244Sgraichen/* Return the age of old log file (file.0) */ 214059004Shmstatic int 214159004Shmage_old_log(char *file) 214213244Sgraichen{ 214359003Shm struct stat sb; 2144114764Sgad char *endp; 2145114764Sgad char tmp[MAXPATHLEN + sizeof(".0") + sizeof(COMPRESS_POSTFIX) + 2146114764Sgad sizeof(BZCOMPRESS_POSTFIX) + 1]; 214713244Sgraichen 214859004Shm if (archtodir) { 214959004Shm char *p; 215059004Shm 215159004Shm /* build name of archive directory into tmp */ 215259004Shm if (*archdirname == '/') { /* absolute */ 215371299Sjedgar strlcpy(tmp, archdirname, sizeof(tmp)); 215459004Shm } else { /* relative */ 215559004Shm /* get directory part of logfile */ 215671299Sjedgar strlcpy(tmp, file, sizeof(tmp)); 215759004Shm if ((p = rindex(tmp, '/')) == NULL) 215859004Shm tmp[0] = '\0'; 215959004Shm else 216059004Shm *(p + 1) = '\0'; 216171299Sjedgar strlcat(tmp, archdirname, sizeof(tmp)); 216259004Shm } 216359004Shm 216471299Sjedgar strlcat(tmp, "/", sizeof(tmp)); 216559004Shm 216659004Shm /* get filename part of logfile */ 216759004Shm if ((p = rindex(file, '/')) == NULL) 216871299Sjedgar strlcat(tmp, file, sizeof(tmp)); 216959004Shm else 217071299Sjedgar strlcat(tmp, p + 1, sizeof(tmp)); 217159004Shm } else { 217271299Sjedgar (void) strlcpy(tmp, file, sizeof(tmp)); 217359004Shm } 217459004Shm 2175114764Sgad strlcat(tmp, ".0", sizeof(tmp)); 2176114764Sgad if (stat(tmp, &sb) < 0) { 2177114764Sgad /* 2178114764Sgad * A plain '.0' file does not exist. Try again, first 2179114764Sgad * with the added suffix of '.gz', then with an added 2180114764Sgad * suffix of '.bz2' instead of '.gz'. 2181114764Sgad */ 2182114764Sgad endp = strchr(tmp, '\0'); 2183114764Sgad strlcat(tmp, COMPRESS_POSTFIX, sizeof(tmp)); 2184114764Sgad if (stat(tmp, &sb) < 0) { 2185114764Sgad *endp = '\0'; /* Remove .gz */ 2186114764Sgad strlcat(tmp, BZCOMPRESS_POSTFIX, sizeof(tmp)); 2187114764Sgad if (stat(tmp, &sb) < 0) 2188114764Sgad return (-1); 2189114764Sgad } 2190114764Sgad } 2191120361Sgad return ((int)(ptimeget_secs(timenow) - sb.st_mtime + 1800) / 3600); 219213244Sgraichen} 219313244Sgraichen 219413244Sgraichen/* Skip Over Blanks */ 2195111820Sgadstatic char * 219659004Shmsob(char *p) 219713244Sgraichen{ 219859003Shm while (p && *p && isspace(*p)) 219959003Shm p++; 220059003Shm return (p); 220113244Sgraichen} 220213244Sgraichen 220313244Sgraichen/* Skip Over Non-Blanks */ 2204111820Sgadstatic char * 220559004Shmson(char *p) 220613244Sgraichen{ 220759003Shm while (p && *p && !isspace(*p)) 220859003Shm p++; 220959003Shm return (p); 221013244Sgraichen} 221143071Swollman 2212119102Sgad/* Check if string is actually a number */ 2213119102Sgadstatic int 2214119102Sgadisnumberstr(const char *string) 2215119102Sgad{ 2216119102Sgad while (*string) { 2217119102Sgad if (!isdigitch(*string++)) 2218119102Sgad return (0); 2219119102Sgad } 2220119102Sgad return (1); 2221119102Sgad} 2222119102Sgad 2223208649Sgordon/* Check if string contains a glob */ 2224208649Sgordonstatic int 2225208649Sgordonisglobstr(const char *string) 2226208649Sgordon{ 2227208649Sgordon char chr; 2228208649Sgordon 2229208649Sgordon while ((chr = *string++)) { 2230208649Sgordon if (chr == '*' || chr == '?' || chr == '[') 2231208649Sgordon return (1); 2232208649Sgordon } 2233208649Sgordon return (0); 2234208649Sgordon} 2235208649Sgordon 2236130038Sgad/* 2237130038Sgad * Save the active log file under a new name. A link to the new name 2238130038Sgad * is the quick-and-easy way to do this. If that fails (which it will 2239130038Sgad * if the destination is on another partition), then make a copy of 2240130038Sgad * the file to the new location. 2241130038Sgad */ 224259004Shmstatic void 2243130038Sgadsavelog(char *from, char *to) 224459004Shm{ 224559004Shm FILE *src, *dst; 2246130038Sgad int c, res; 224759004Shm 2248130038Sgad res = link(from, to); 2249130038Sgad if (res == 0) 2250130038Sgad return; 2251130038Sgad 225259004Shm if ((src = fopen(from, "r")) == NULL) 225359004Shm err(1, "can't fopen %s for reading", from); 225459004Shm if ((dst = fopen(to, "w")) == NULL) 225559004Shm err(1, "can't fopen %s for writing", to); 225659004Shm 225759004Shm while ((c = getc(src)) != EOF) { 225859004Shm if ((putc(c, dst)) == EOF) 225959004Shm err(1, "error writing to %s", to); 226059004Shm } 226159004Shm 226259004Shm if (ferror(src)) 226359004Shm err(1, "error reading from %s", from); 226459004Shm if ((fclose(src)) != 0) 226559004Shm err(1, "can't fclose %s", to); 226659004Shm if ((fclose(dst)) != 0) 226759004Shm err(1, "can't fclose %s", from); 226859004Shm} 226959004Shm 227059004Shm/* create one or more directory components of a path */ 227159004Shmstatic void 2272114137Sgadcreatedir(const struct conf_entry *ent, char *dirpart) 227359004Shm{ 2274111398Sgad int res; 227559004Shm char *s, *d; 227671299Sjedgar char mkdirpath[MAXPATHLEN]; 227759004Shm struct stat st; 227859004Shm 227959004Shm s = dirpart; 228059004Shm d = mkdirpath; 228159004Shm 228259004Shm for (;;) { 228359004Shm *d++ = *s++; 2284111398Sgad if (*s != '/' && *s != '\0') 2285111398Sgad continue; 2286111398Sgad *d = '\0'; 2287111398Sgad res = lstat(mkdirpath, &st); 2288111398Sgad if (res != 0) { 2289111398Sgad if (noaction) { 2290111967Sgad printf("\tmkdir %s\n", mkdirpath); 2291111398Sgad } else { 2292111398Sgad res = mkdir(mkdirpath, 0755); 2293111398Sgad if (res != 0) 2294111398Sgad err(1, "Error on mkdir(\"%s\") for -a", 2295111398Sgad mkdirpath); 2296111398Sgad } 229759004Shm } 229859004Shm if (*s == '\0') 229959004Shm break; 230059004Shm } 2301114137Sgad if (verbose) { 2302114137Sgad if (ent->firstcreate) 2303114137Sgad printf("Created directory '%s' for new %s\n", 2304114137Sgad dirpart, ent->log); 2305114137Sgad else 2306114137Sgad printf("Created directory '%s' for -a\n", dirpart); 2307114137Sgad } 230859004Shm} 230959004Shm 2310114137Sgad/* 2311114137Sgad * Create a new log file, destroying any currently-existing version 2312114137Sgad * of the log file in the process. If the caller wants a backup copy 2313114137Sgad * of the file to exist, they should call 'link(logfile,logbackup)' 2314114137Sgad * before calling this routine. 2315114137Sgad */ 2316114137Sgadvoid 2317114137Sgadcreatelog(const struct conf_entry *ent) 2318114137Sgad{ 2319114137Sgad int fd, failed; 2320114137Sgad struct stat st; 2321114137Sgad char *realfile, *slash, tempfile[MAXPATHLEN]; 2322114137Sgad 2323114137Sgad fd = -1; 2324114137Sgad realfile = ent->log; 2325114137Sgad 2326114137Sgad /* 2327114137Sgad * If this log file is being created for the first time (-C option), 2328114137Sgad * then it may also be true that the parent directory does not exist 2329114137Sgad * yet. Check, and create that directory if it is missing. 2330114137Sgad */ 2331114137Sgad if (ent->firstcreate) { 2332114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 2333114137Sgad slash = strrchr(tempfile, '/'); 2334114137Sgad if (slash != NULL) { 2335114137Sgad *slash = '\0'; 2336131581Ssobomax failed = stat(tempfile, &st); 2337114137Sgad if (failed && errno != ENOENT) 2338131581Ssobomax err(1, "Error on stat(%s)", tempfile); 2339114137Sgad if (failed) 2340114137Sgad createdir(ent, tempfile); 2341114137Sgad else if (!S_ISDIR(st.st_mode)) 2342114137Sgad errx(1, "%s exists but is not a directory", 2343114137Sgad tempfile); 2344114137Sgad } 2345114137Sgad } 2346114137Sgad 2347114137Sgad /* 2348114137Sgad * First create an unused filename, so it can be chown'ed and 2349114137Sgad * chmod'ed before it is moved into the real location. mkstemp 2350114137Sgad * will create the file mode=600 & owned by us. Note that all 2351114137Sgad * temp files will have a suffix of '.z<something>'. 2352114137Sgad */ 2353114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 2354114137Sgad strlcat(tempfile, ".zXXXXXX", sizeof(tempfile)); 2355114137Sgad if (noaction) 2356114137Sgad printf("\tmktemp %s\n", tempfile); 2357114137Sgad else { 2358114137Sgad fd = mkstemp(tempfile); 2359114137Sgad if (fd < 0) 2360114137Sgad err(1, "can't mkstemp logfile %s", tempfile); 2361114137Sgad 2362114137Sgad /* 2363114137Sgad * Add status message to what will become the new log file. 2364114137Sgad */ 2365114137Sgad if (!(ent->flags & CE_BINARY)) { 2366114137Sgad if (log_trim(tempfile, ent)) 2367114137Sgad err(1, "can't add status message to log"); 2368114137Sgad } 2369114137Sgad } 2370114137Sgad 2371114137Sgad /* Change the owner/group, if we are supposed to */ 2372114137Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 2373114137Sgad if (noaction) 2374114137Sgad printf("\tchown %u:%u %s\n", ent->uid, ent->gid, 2375114137Sgad tempfile); 2376114137Sgad else { 2377114137Sgad failed = fchown(fd, ent->uid, ent->gid); 2378114137Sgad if (failed) 2379114137Sgad err(1, "can't fchown temp file %s", tempfile); 2380114137Sgad } 2381114137Sgad } 2382114137Sgad 2383130043Sgad /* Turn on NODUMP if it was requested in the config-file. */ 2384130043Sgad if (ent->flags & CE_NODUMP) { 2385130043Sgad if (noaction) 2386130043Sgad printf("\tchflags nodump %s\n", tempfile); 2387130043Sgad else { 2388130043Sgad failed = fchflags(fd, UF_NODUMP); 2389130043Sgad if (failed) { 2390130043Sgad warn("log_trim: fchflags(NODUMP)"); 2391130043Sgad } 2392130043Sgad } 2393130043Sgad } 2394130043Sgad 2395114137Sgad /* 2396114137Sgad * Note that if the real logfile still exists, and if the call 2397114137Sgad * to rename() fails, then "neither the old file nor the new 2398114137Sgad * file shall be changed or created" (to quote the standard). 2399114137Sgad * If the call succeeds, then the file will be replaced without 2400114137Sgad * any window where some other process might find that the file 2401114137Sgad * did not exist. 2402114137Sgad * XXX - ? It may be that for some error conditions, we could 2403114137Sgad * retry by first removing the realfile and then renaming. 2404114137Sgad */ 2405114137Sgad if (noaction) { 2406114137Sgad printf("\tchmod %o %s\n", ent->permissions, tempfile); 2407114137Sgad printf("\tmv %s %s\n", tempfile, realfile); 2408114137Sgad } else { 2409114137Sgad failed = fchmod(fd, ent->permissions); 2410114137Sgad if (failed) 2411114137Sgad err(1, "can't fchmod temp file '%s'", tempfile); 2412114137Sgad failed = rename(tempfile, realfile); 2413114137Sgad if (failed) 2414114137Sgad err(1, "can't mv %s to %s", tempfile, realfile); 2415114137Sgad } 2416114137Sgad 2417114137Sgad if (fd >= 0) 2418114137Sgad close(fd); 2419114137Sgad} 2420130165Sgad 2421154566Sgad/* 2422154566Sgad * Change the attributes of a given filename to what was specified in 2423154566Sgad * the newsyslog.conf entry. This routine is only called for files 2424154566Sgad * that newsyslog expects that it has created, and thus it is a fatal 2425154566Sgad * error if this routine finds that the file does not exist. 2426154566Sgad */ 2427130165Sgadstatic void 2428130165Sgadchange_attrs(const char *fname, const struct conf_entry *ent) 2429130165Sgad{ 2430130165Sgad int failed; 2431130165Sgad 2432130165Sgad if (noaction) { 2433130165Sgad printf("\tchmod %o %s\n", ent->permissions, fname); 2434130165Sgad 2435130165Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 2436130165Sgad printf("\tchown %u:%u %s\n", 2437130165Sgad ent->uid, ent->gid, fname); 2438130165Sgad 2439130165Sgad if (ent->flags & CE_NODUMP) 2440130165Sgad printf("\tchflags nodump %s\n", fname); 2441130165Sgad return; 2442130165Sgad } 2443130165Sgad 2444130165Sgad failed = chmod(fname, ent->permissions); 2445154566Sgad if (failed) { 2446154566Sgad if (errno != EPERM) 2447154566Sgad err(1, "chmod(%s) in change_attrs", fname); 2448154566Sgad warn("change_attrs couldn't chmod(%s)", fname); 2449154566Sgad } 2450130165Sgad 2451130165Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 2452130165Sgad failed = chown(fname, ent->uid, ent->gid); 2453130165Sgad if (failed) 2454130165Sgad warn("can't chown %s", fname); 2455130165Sgad } 2456130165Sgad 2457130165Sgad if (ent->flags & CE_NODUMP) { 2458130165Sgad failed = chflags(fname, UF_NODUMP); 2459130165Sgad if (failed) 2460130165Sgad warn("can't chflags %s NODUMP", fname); 2461130165Sgad } 2462130165Sgad} 2463