newsyslog.c revision 218127
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 218127 2011-01-31 10:57:54Z mm $"); 5813244Sgraichen 59130045Sgad#define OSF 6013244Sgraichen 6196001Smaxim#include <sys/param.h> 62130167Sgad#include <sys/queue.h> 6396001Smaxim#include <sys/stat.h> 6496001Smaxim#include <sys/wait.h> 6596001Smaxim 66210372Ssimon#include <assert.h> 6730160Scharnier#include <ctype.h> 6830160Scharnier#include <err.h> 6995999Smaxim#include <errno.h> 70210372Ssimon#include <dirent.h> 7130160Scharnier#include <fcntl.h> 72111773Sgad#include <fnmatch.h> 73106905Ssobomax#include <glob.h> 7430160Scharnier#include <grp.h> 7543071Swollman#include <paths.h> 7630160Scharnier#include <pwd.h> 7730160Scharnier#include <signal.h> 7813244Sgraichen#include <stdio.h> 79210372Ssimon#include <libgen.h> 8013244Sgraichen#include <stdlib.h> 8113244Sgraichen#include <string.h> 8243071Swollman#include <time.h> 8316240Salex#include <unistd.h> 8413244Sgraichen 8543071Swollman#include "pathnames.h" 86119998Sgad#include "extern.h" 8743071Swollman 88111768Sgad/* 89218127Smm * Compression suffixes 90218127Smm */ 91218127Smm#ifndef COMPRESS_SUFFIX_GZ 92218127Smm#define COMPRESS_SUFFIX_GZ ".gz" 93218127Smm#endif 94218127Smm 95218127Smm#ifndef COMPRESS_SUFFIX_BZ2 96218127Smm#define COMPRESS_SUFFIX_BZ2 ".bz2" 97218127Smm#endif 98218127Smm 99218127Smm#ifndef COMPRESS_SUFFIX_XZ 100218127Smm#define COMPRESS_SUFFIX_XZ ".xz" 101218127Smm#endif 102218127Smm 103218127Smm#define COMPRESS_SUFFIX_MAXLEN MAX(MAX(sizeof(COMPRESS_SUFFIX_GZ),sizeof(COMPRESS_SUFFIX_BZ2)),sizeof(COMPRESS_SUFFIX_XZ)) 104218127Smm 105218127Smm/* 106218127Smm * Compression types 107218127Smm */ 108218127Smm#define COMPRESS_TYPES 4 /* Number of supported compression types */ 109218127Smm 110218127Smm#define COMPRESS_NONE 0 111218127Smm#define COMPRESS_GZIP 1 112218127Smm#define COMPRESS_BZIP2 2 113218127Smm#define COMPRESS_XZ 3 114218127Smm 115218127Smm/* 116111768Sgad * Bit-values for the 'flags' parsed from a config-file entry. 117111768Sgad */ 118130045Sgad#define CE_BINARY 0x0008 /* Logfile is in binary, do not add status */ 119111768Sgad /* messages to logfile(s) when rotating. */ 120130045Sgad#define CE_NOSIGNAL 0x0010 /* There is no process to signal when */ 121111768Sgad /* trimming this file. */ 122130045Sgad#define CE_TRIMAT 0x0020 /* trim file at a specific time. */ 123130045Sgad#define CE_GLOB 0x0040 /* name of the log is file name pattern. */ 124130045Sgad#define CE_SIGNALGROUP 0x0080 /* Signal a process-group instead of a single */ 125112003Sgad /* process when trimming this file. */ 126130045Sgad#define CE_CREATE 0x0100 /* Create the log file if it does not exist. */ 127130043Sgad#define CE_NODUMP 0x0200 /* Set 'nodump' on newly created log file. */ 12843071Swollman 129130045Sgad#define MIN_PID 5 /* Don't touch pids lower than this */ 130130045Sgad#define MAX_PID 99999 /* was lower, see /usr/include/sys/proc.h */ 131111781Sgad 132130045Sgad#define kbytes(size) (((size) + 1023) >> 10) 133111781Sgad 134130165Sgad#define DEFAULT_MARKER "<default>" 135130165Sgad#define DEBUG_MARKER "<debug>" 136208649Sgordon#define INCLUDE_MARKER "<include>" 137210372Ssimon#define DEFAULT_TIMEFNAME_FMT "%Y%m%dT%H%M%S" 138130165Sgad 139210372Ssimon#define MAX_OLDLOGS 65536 /* Default maximum number of old logfiles */ 140210372Ssimon 141218127Smmstruct compress_types { 142218127Smm const char *flag; /* Flag in configuration file */ 143218127Smm const char *suffix; /* Compression suffix */ 144218127Smm const char *path; /* Path to compression program */ 145218127Smm}; 146218127Smm 147218127Smmconst struct compress_types compress_type[COMPRESS_TYPES] = { 148218127Smm { "", "", "" }, /* no compression */ 149218127Smm { "Z", COMPRESS_SUFFIX_GZ, _PATH_GZIP }, /* gzip compression */ 150218127Smm { "J", COMPRESS_SUFFIX_BZ2, _PATH_BZIP2 }, /* bzip2 compression */ 151218127Smm { "X", COMPRESS_SUFFIX_XZ, _PATH_XZ } /* xz compression */ 152218127Smm}; 153218127Smm 15413244Sgraichenstruct conf_entry { 155208648Sgordon STAILQ_ENTRY(conf_entry) cf_nextp; 15659003Shm char *log; /* Name of the log */ 15759003Shm char *pid_file; /* PID file */ 158111772Sgad char *r_reason; /* The reason this file is being rotated */ 159114137Sgad int firstcreate; /* Creating log for the first time (-C). */ 160111772Sgad int rotate; /* Non-zero if this file should be rotated */ 161130165Sgad int fsize; /* size found for the log file */ 162111779Sgad uid_t uid; /* Owner of log */ 163111779Sgad gid_t gid; /* Group of log */ 16459003Shm int numlogs; /* Number of logs to keep */ 165130165Sgad int trsize; /* Size cutoff to trigger trimming the log */ 16659003Shm int hours; /* Hours between log trimming */ 167120361Sgad struct ptime_data *trim_at; /* Specific time to do trimming */ 168139655Sdelphij unsigned int permissions; /* File permissions on the log */ 169218127Smm int flags; /* CE_BINARY */ 170218127Smm int compress; /* Compression */ 17159003Shm int sig; /* Signal to send */ 172111388Sgad int def_cfg; /* Using the <default> rule for this file */ 17313244Sgraichen}; 17413244Sgraichen 175130167Sgadstruct sigwork_entry { 176130167Sgad SLIST_ENTRY(sigwork_entry) sw_nextp; 177130167Sgad int sw_signum; /* the signal to send */ 178130167Sgad int sw_pidok; /* true if pid value is valid */ 179130167Sgad pid_t sw_pid; /* the process id from the PID file */ 180130167Sgad const char *sw_pidtype; /* "daemon" or "process group" */ 181130167Sgad char sw_fname[1]; /* file the PID was read from */ 182130167Sgad}; 183130167Sgad 184130167Sgadstruct zipwork_entry { 185130167Sgad SLIST_ENTRY(zipwork_entry) zw_nextp; 186130167Sgad const struct conf_entry *zw_conf; /* for chown/perm/flag info */ 187130167Sgad const struct sigwork_entry *zw_swork; /* to know success of signal */ 188130167Sgad int zw_fsize; /* size of the file to compress */ 189130167Sgad char zw_fname[1]; /* the file to compress */ 190130167Sgad}; 191130167Sgad 192208649Sgordonstruct include_entry { 193208649Sgordon STAILQ_ENTRY(include_entry) inc_nextp; 194208649Sgordon const char *file; /* Name of file to process */ 195208649Sgordon}; 196208649Sgordon 197210372Ssimonstruct oldlog_entry { 198210372Ssimon char *fname; /* Filename of the log file */ 199215625Ssimon time_t t; /* Parsed timestamp of the logfile */ 200210372Ssimon}; 201210372Ssimon 202130165Sgadtypedef enum { 203130165Sgad FREE_ENT, KEEP_ENT 204130165Sgad} fk_entry; 205111388Sgad 206208648SgordonSTAILQ_HEAD(cflist, conf_entry); 207130167SgadSLIST_HEAD(swlisthead, sigwork_entry) swhead = SLIST_HEAD_INITIALIZER(swhead); 208130167SgadSLIST_HEAD(zwlisthead, zipwork_entry) zwhead = SLIST_HEAD_INITIALIZER(zwhead); 209208649SgordonSTAILQ_HEAD(ilist, include_entry); 210130167Sgad 211120361Sgadint dbg_at_times; /* -D Show details of 'trim_at' code */ 212120361Sgad 21359004Shmint archtodir = 0; /* Archive old logfiles to other directory */ 214114137Sgadint createlogs; /* Create (non-GLOB) logfiles which do not */ 215114137Sgad /* already exist. 1=='for entries with */ 216114137Sgad /* C flag', 2=='for all entries'. */ 21759003Shmint verbose = 0; /* Print out what's going on */ 21859003Shmint needroot = 1; /* Root privs are necessary */ 21959003Shmint noaction = 0; /* Don't do anything, just show it */ 220143106Sbrooksint norotate = 0; /* Don't rotate */ 221111768Sgadint nosignal; /* Do not send any signals */ 222202668Sdelphijint enforcepid = 0; /* If PID file does not exist or empty, do nothing */ 22359003Shmint force = 0; /* Force the trim no matter what */ 224111772Sgadint rotatereq = 0; /* -R = Always rotate the file(s) as given */ 225111772Sgad /* on the command (this also requires */ 226111772Sgad /* that a list of files *are* given on */ 227111772Sgad /* the run command). */ 228111772Sgadchar *requestor; /* The name given on a -R request */ 229210372Ssimonchar *timefnamefmt = NULL; /* Use time based filenames instead of .0 etc */ 23059004Shmchar *archdirname; /* Directory path to old logfiles archive */ 231136127Sbrookschar *destdir = NULL; /* Directory to treat at root for logs */ 232111773Sgadconst char *conf; /* Configuration file to use */ 23359003Shm 234120361Sgadstruct ptime_data *dbg_timenow; /* A "timenow" value set via -D option */ 235120361Sgadstruct ptime_data *timenow; /* The time to use for checking at-fields */ 236120361Sgad 237159998Sgad#define DAYTIME_LEN 16 238159998Sgadchar daytime[DAYTIME_LEN]; /* The current time in human readable form, 239159998Sgad * used for rotation-tracking messages. */ 24071299Sjedgarchar hostname[MAXHOSTNAMELEN]; /* hostname */ 24113244Sgraichen 242210407Sbrianconst char *path_syslogpid = _PATH_SYSLOGPID; 243210407Sbrian 244208648Sgordonstatic struct cflist *get_worklist(char **files); 245208648Sgordonstatic void parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p, 246208649Sgordon struct conf_entry *defconf_p, struct ilist *inclist); 247208649Sgordonstatic void add_to_queue(const char *fname, struct ilist *inclist); 24816240Salexstatic char *sob(char *p); 24916240Salexstatic char *son(char *p); 250119102Sgadstatic int isnumberstr(const char *); 251208649Sgordonstatic int isglobstr(const char *); 25259003Shmstatic char *missing_field(char *p, char *errline); 253130165Sgadstatic void change_attrs(const char *, const struct conf_entry *); 254218127Smmstatic const char *get_logfile_suffix(const char *logfile); 255130165Sgadstatic fk_entry do_entry(struct conf_entry *); 256130165Sgadstatic fk_entry do_rotate(const struct conf_entry *); 257130167Sgadstatic void do_sigwork(struct sigwork_entry *); 258130167Sgadstatic void do_zipwork(struct zipwork_entry *); 259130167Sgadstatic struct sigwork_entry * 260130167Sgad save_sigwork(const struct conf_entry *); 261130167Sgadstatic struct zipwork_entry * 262130167Sgad save_zipwork(const struct conf_entry *, const struct 263130167Sgad sigwork_entry *, int, const char *); 264130167Sgadstatic void set_swpid(struct sigwork_entry *, const struct conf_entry *); 265130165Sgadstatic int sizefile(const char *); 266208648Sgordonstatic void expand_globs(struct cflist *work_p, struct cflist *glob_p); 267208648Sgordonstatic void free_clist(struct cflist *list); 268111388Sgadstatic void free_entry(struct conf_entry *ent); 269111388Sgadstatic struct conf_entry *init_entry(const char *fname, 270111388Sgad struct conf_entry *src_entry); 271111781Sgadstatic void parse_args(int argc, char **argv); 272119904Sgadstatic int parse_doption(const char *doption); 27380640Sobrienstatic void usage(void); 274119926Sgadstatic int log_trim(const char *logname, const struct conf_entry *log_ent); 27516240Salexstatic int age_old_log(char *file); 276130038Sgadstatic void savelog(char *from, char *to); 277114137Sgadstatic void createdir(const struct conf_entry *ent, char *dirpart); 278114137Sgadstatic void createlog(const struct conf_entry *ent); 27913244Sgraichen 280111768Sgad/* 281129975Sgad * All the following take a parameter of 'int', but expect values in the 282129975Sgad * range of unsigned char. Define wrappers which take values of type 'char', 283129975Sgad * whether signed or unsigned, and ensure they end up in the right range. 284111768Sgad */ 285129975Sgad#define isdigitch(Anychar) isdigit((u_char)(Anychar)) 286129975Sgad#define isprintch(Anychar) isprint((u_char)(Anychar)) 287129975Sgad#define isspacech(Anychar) isspace((u_char)(Anychar)) 288129975Sgad#define tolowerch(Anychar) tolower((u_char)(Anychar)) 289111768Sgad 29059004Shmint 29159004Shmmain(int argc, char **argv) 29213244Sgraichen{ 293208648Sgordon struct cflist *worklist; 294208648Sgordon struct conf_entry *p; 295130167Sgad struct sigwork_entry *stmp; 296130167Sgad struct zipwork_entry *ztmp; 29725443Sache 298130167Sgad SLIST_INIT(&swhead); 299130167Sgad SLIST_INIT(&zwhead); 300130167Sgad 301111781Sgad parse_args(argc, argv); 302111773Sgad argc -= optind; 303111773Sgad argv += optind; 304111773Sgad 30559003Shm if (needroot && getuid() && geteuid()) 30659003Shm errx(1, "must have root privs"); 307208648Sgordon worklist = get_worklist(argv); 30859003Shm 309130165Sgad /* 310130165Sgad * Rotate all the files which need to be rotated. Note that 311130165Sgad * some users have *hundreds* of entries in newsyslog.conf! 312130165Sgad */ 313208648Sgordon while (!STAILQ_EMPTY(worklist)) { 314208648Sgordon p = STAILQ_FIRST(worklist); 315208648Sgordon STAILQ_REMOVE_HEAD(worklist, cf_nextp); 316208648Sgordon if (do_entry(p) == FREE_ENT) 317208648Sgordon free_entry(p); 31859003Shm } 319130165Sgad 320130167Sgad /* 321130167Sgad * Send signals to any processes which need a signal to tell 322130167Sgad * them to close and re-open the log file(s) we have rotated. 323130167Sgad * Note that zipwork_entries include pointers to these 324130167Sgad * sigwork_entry's, so we can not free the entries here. 325130167Sgad */ 326130167Sgad if (!SLIST_EMPTY(&swhead)) { 327130204Sgad if (noaction || verbose) 328130167Sgad printf("Signal all daemon process(es)...\n"); 329130167Sgad SLIST_FOREACH(stmp, &swhead, sw_nextp) 330130167Sgad do_sigwork(stmp); 331130204Sgad if (noaction) 332130204Sgad printf("\tsleep 10\n"); 333130204Sgad else { 334130204Sgad if (verbose) 335130204Sgad printf("Pause 10 seconds to allow daemon(s)" 336130204Sgad " to close log file(s)\n"); 337130204Sgad sleep(10); 338130204Sgad } 339130167Sgad } 340130167Sgad /* 341130167Sgad * Compress all files that we're expected to compress, now 342130167Sgad * that all processes should have closed the files which 343130167Sgad * have been rotated. 344130167Sgad */ 345130167Sgad if (!SLIST_EMPTY(&zwhead)) { 346130204Sgad if (noaction || verbose) 347130167Sgad printf("Compress all rotated log file(s)...\n"); 348130167Sgad while (!SLIST_EMPTY(&zwhead)) { 349130167Sgad ztmp = SLIST_FIRST(&zwhead); 350130167Sgad do_zipwork(ztmp); 351130167Sgad SLIST_REMOVE_HEAD(&zwhead, zw_nextp); 352130167Sgad free(ztmp); 353130167Sgad } 354130167Sgad } 355130167Sgad /* Now free all the sigwork entries. */ 356130167Sgad while (!SLIST_EMPTY(&swhead)) { 357130167Sgad stmp = SLIST_FIRST(&swhead); 358130167Sgad SLIST_REMOVE_HEAD(&swhead, sw_nextp); 359130167Sgad free(stmp); 360130167Sgad } 361130167Sgad 36295999Smaxim while (wait(NULL) > 0 || errno == EINTR) 36395999Smaxim ; 36459003Shm return (0); 36513244Sgraichen} 36613244Sgraichen 367111388Sgadstatic struct conf_entry * 368111388Sgadinit_entry(const char *fname, struct conf_entry *src_entry) 369111388Sgad{ 370111388Sgad struct conf_entry *tempwork; 371111388Sgad 372111388Sgad if (verbose > 4) 373111388Sgad printf("\t--> [creating entry for %s]\n", fname); 374111388Sgad 375111388Sgad tempwork = malloc(sizeof(struct conf_entry)); 376111388Sgad if (tempwork == NULL) 377111388Sgad err(1, "malloc of conf_entry for %s", fname); 378111388Sgad 379136174Sbrooks if (destdir == NULL || fname[0] != '/') 380136127Sbrooks tempwork->log = strdup(fname); 381136127Sbrooks else 382136127Sbrooks asprintf(&tempwork->log, "%s%s", destdir, fname); 383111388Sgad if (tempwork->log == NULL) 384111388Sgad err(1, "strdup for %s", fname); 385111388Sgad 386111388Sgad if (src_entry != NULL) { 387111388Sgad tempwork->pid_file = NULL; 388111388Sgad if (src_entry->pid_file) 389111388Sgad tempwork->pid_file = strdup(src_entry->pid_file); 390111772Sgad tempwork->r_reason = NULL; 391114137Sgad tempwork->firstcreate = 0; 392111772Sgad tempwork->rotate = 0; 393130165Sgad tempwork->fsize = -1; 394111388Sgad tempwork->uid = src_entry->uid; 395111388Sgad tempwork->gid = src_entry->gid; 396111388Sgad tempwork->numlogs = src_entry->numlogs; 397130165Sgad tempwork->trsize = src_entry->trsize; 398111388Sgad tempwork->hours = src_entry->hours; 399120361Sgad tempwork->trim_at = NULL; 400120361Sgad if (src_entry->trim_at != NULL) 401120361Sgad tempwork->trim_at = ptime_init(src_entry->trim_at); 402111388Sgad tempwork->permissions = src_entry->permissions; 403111388Sgad tempwork->flags = src_entry->flags; 404218127Smm tempwork->compress = src_entry->compress; 405111388Sgad tempwork->sig = src_entry->sig; 406111388Sgad tempwork->def_cfg = src_entry->def_cfg; 407111388Sgad } else { 408111388Sgad /* Initialize as a "do-nothing" entry */ 409111388Sgad tempwork->pid_file = NULL; 410111772Sgad tempwork->r_reason = NULL; 411114137Sgad tempwork->firstcreate = 0; 412111772Sgad tempwork->rotate = 0; 413130165Sgad tempwork->fsize = -1; 414111779Sgad tempwork->uid = (uid_t)-1; 415111779Sgad tempwork->gid = (gid_t)-1; 416111388Sgad tempwork->numlogs = 1; 417130165Sgad tempwork->trsize = -1; 418111388Sgad tempwork->hours = -1; 419120361Sgad tempwork->trim_at = NULL; 420111388Sgad tempwork->permissions = 0; 421111388Sgad tempwork->flags = 0; 422218127Smm tempwork->compress = COMPRESS_NONE; 423111388Sgad tempwork->sig = SIGHUP; 424111388Sgad tempwork->def_cfg = 0; 425111388Sgad } 426111388Sgad 427111388Sgad return (tempwork); 428111388Sgad} 429111388Sgad 43059004Shmstatic void 431111388Sgadfree_entry(struct conf_entry *ent) 432111388Sgad{ 433111388Sgad 434111388Sgad if (ent == NULL) 435111388Sgad return; 436111388Sgad 437111388Sgad if (ent->log != NULL) { 438111388Sgad if (verbose > 4) 439111388Sgad printf("\t--> [freeing entry for %s]\n", ent->log); 440111388Sgad free(ent->log); 441111388Sgad ent->log = NULL; 442111388Sgad } 443111388Sgad 444111388Sgad if (ent->pid_file != NULL) { 445111388Sgad free(ent->pid_file); 446111388Sgad ent->pid_file = NULL; 447111388Sgad } 448111388Sgad 449111772Sgad if (ent->r_reason != NULL) { 450111772Sgad free(ent->r_reason); 451111772Sgad ent->r_reason = NULL; 452111772Sgad } 453111772Sgad 454120361Sgad if (ent->trim_at != NULL) { 455120361Sgad ptime_free(ent->trim_at); 456120361Sgad ent->trim_at = NULL; 457120361Sgad } 458120361Sgad 459111388Sgad free(ent); 460111388Sgad} 461111388Sgad 462111388Sgadstatic void 463208648Sgordonfree_clist(struct cflist *list) 464112020Sgad{ 465208648Sgordon struct conf_entry *ent; 466112020Sgad 467208648Sgordon while (!STAILQ_EMPTY(list)) { 468208648Sgordon ent = STAILQ_FIRST(list); 469208648Sgordon STAILQ_REMOVE_HEAD(list, cf_nextp); 470112020Sgad free_entry(ent); 471112020Sgad } 472208648Sgordon 473208648Sgordon free(list); 474208648Sgordon list = NULL; 475112020Sgad} 476112020Sgad 477130165Sgadstatic fk_entry 47859004Shmdo_entry(struct conf_entry * ent) 47913244Sgraichen{ 480130045Sgad#define REASON_MAX 80 481130165Sgad int modtime; 482130165Sgad fk_entry free_or_keep; 483120361Sgad double diffsecs; 484111772Sgad char temp_reason[REASON_MAX]; 48559003Shm 486130165Sgad free_or_keep = FREE_ENT; 487218127Smm if (verbose) 488218127Smm printf("%s <%d%s>: ", ent->log, ent->numlogs, 489218127Smm compress_type[ent->compress].flag); 490130165Sgad ent->fsize = sizefile(ent->log); 49159003Shm modtime = age_old_log(ent->log); 492111772Sgad ent->rotate = 0; 493114137Sgad ent->firstcreate = 0; 494130165Sgad if (ent->fsize < 0) { 495114137Sgad /* 496114137Sgad * If either the C flag or the -C option was specified, 497114137Sgad * and if we won't be creating the file, then have the 498114137Sgad * verbose message include a hint as to why the file 499114137Sgad * will not be created. 500114137Sgad */ 501114137Sgad temp_reason[0] = '\0'; 502114137Sgad if (createlogs > 1) 503114137Sgad ent->firstcreate = 1; 504114137Sgad else if ((ent->flags & CE_CREATE) && createlogs) 505114137Sgad ent->firstcreate = 1; 506114137Sgad else if (ent->flags & CE_CREATE) 507159998Sgad strlcpy(temp_reason, " (no -C option)", REASON_MAX); 508114137Sgad else if (createlogs) 509159998Sgad strlcpy(temp_reason, " (no C flag)", REASON_MAX); 510114137Sgad 511114137Sgad if (ent->firstcreate) { 512114137Sgad if (verbose) 513114137Sgad printf("does not exist -> will create.\n"); 514114137Sgad createlog(ent); 515114137Sgad } else if (verbose) { 516114137Sgad printf("does not exist, skipped%s.\n", temp_reason); 517114137Sgad } 51859003Shm } else { 519111772Sgad if (ent->flags & CE_TRIMAT && !force && !rotatereq) { 520120361Sgad diffsecs = ptimeget_diff(timenow, ent->trim_at); 521120361Sgad if (diffsecs < 0.0) { 522120361Sgad /* trim_at is some time in the future. */ 523120361Sgad if (verbose) { 524120361Sgad ptime_adjust4dst(ent->trim_at, 525120361Sgad timenow); 52643071Swollman printf("--> will trim at %s", 527120361Sgad ptimeget_ctime(ent->trim_at)); 528120361Sgad } 529130165Sgad return (free_or_keep); 530120361Sgad } else if (diffsecs >= 3600.0) { 531120361Sgad /* 532120361Sgad * trim_at is more than an hour in the past, 533120361Sgad * so find the next valid trim_at time, and 534120361Sgad * tell the user what that will be. 535120361Sgad */ 536120361Sgad if (verbose && dbg_at_times) 537120361Sgad printf("\n\t--> prev trim at %s\t", 538120361Sgad ptimeget_ctime(ent->trim_at)); 539120361Sgad if (verbose) { 540120361Sgad ptimeset_nxtime(ent->trim_at); 541120361Sgad printf("--> will trim at %s", 542120361Sgad ptimeget_ctime(ent->trim_at)); 543120361Sgad } 544130165Sgad return (free_or_keep); 545120361Sgad } else if (verbose && noaction && dbg_at_times) { 546120361Sgad /* 547120361Sgad * If we are just debugging at-times, then 548120361Sgad * a detailed message is helpful. Also 549120361Sgad * skip "doing" any commands, since they 550120361Sgad * would all be turned off by no-action. 551120361Sgad */ 552120361Sgad printf("\n\t--> timematch at %s", 553120361Sgad ptimeget_ctime(ent->trim_at)); 554130165Sgad return (free_or_keep); 55543071Swollman } else if (verbose && ent->hours <= 0) { 55643071Swollman printf("--> time is up\n"); 55743071Swollman } 55843071Swollman } 559130165Sgad if (verbose && (ent->trsize > 0)) 560130165Sgad printf("size (Kb): %d [%d] ", ent->fsize, ent->trsize); 56159003Shm if (verbose && (ent->hours > 0)) 56259003Shm printf(" age (hr): %d [%d] ", modtime, ent->hours); 563111772Sgad 564111772Sgad /* 565111772Sgad * Figure out if this logfile needs to be rotated. 566111772Sgad */ 567111772Sgad temp_reason[0] = '\0'; 568111772Sgad if (rotatereq) { 569111772Sgad ent->rotate = 1; 570111772Sgad snprintf(temp_reason, REASON_MAX, " due to -R from %s", 571111772Sgad requestor); 572111772Sgad } else if (force) { 573111772Sgad ent->rotate = 1; 574111772Sgad snprintf(temp_reason, REASON_MAX, " due to -F request"); 575130165Sgad } else if ((ent->trsize > 0) && (ent->fsize >= ent->trsize)) { 576111772Sgad ent->rotate = 1; 577111772Sgad snprintf(temp_reason, REASON_MAX, " due to size>%dK", 578130165Sgad ent->trsize); 579111772Sgad } else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) { 580111772Sgad ent->rotate = 1; 581111772Sgad } else if ((ent->hours > 0) && ((modtime >= ent->hours) || 582111772Sgad (modtime < 0))) { 583111772Sgad ent->rotate = 1; 584111772Sgad } 585111772Sgad 586111772Sgad /* 587111772Sgad * If the file needs to be rotated, then rotate it. 588111772Sgad */ 589143106Sbrooks if (ent->rotate && !norotate) { 590111772Sgad if (temp_reason[0] != '\0') 591111772Sgad ent->r_reason = strdup(temp_reason); 59259003Shm if (verbose) 59359003Shm printf("--> trimming log....\n"); 594218127Smm if (noaction && !verbose) 595218127Smm printf("%s <%d%s>: trimming\n", ent->log, 596218127Smm ent->numlogs, 597218127Smm compress_type[ent->compress].flag); 598130165Sgad free_or_keep = do_rotate(ent); 59959003Shm } else { 60059003Shm if (verbose) 60159003Shm printf("--> skipping\n"); 60259003Shm } 60359003Shm } 604130165Sgad return (free_or_keep); 605111772Sgad#undef REASON_MAX 60613244Sgraichen} 60713244Sgraichen 60859004Shmstatic void 609111781Sgadparse_args(int argc, char **argv) 61013244Sgraichen{ 611111781Sgad int ch; 61259003Shm char *p; 61313244Sgraichen 614120361Sgad timenow = ptime_init(NULL); 615120361Sgad ptimeset_time(timenow, time(NULL)); 616159998Sgad strlcpy(daytime, ptimeget_ctime(timenow) + 4, DAYTIME_LEN); 61713244Sgraichen 61859003Shm /* Let's get our hostname */ 619111781Sgad (void)gethostname(hostname, sizeof(hostname)); 62013244Sgraichen 62113244Sgraichen /* Truncate domain */ 622111781Sgad if ((p = strchr(hostname, '.')) != NULL) 62313244Sgraichen *p = '\0'; 624111768Sgad 625111768Sgad /* Parse command line options. */ 626216832Sbrian while ((ch = getopt(argc, argv, "a:d:f:nrst:vCD:FNPR:S:")) != -1) 627111781Sgad switch (ch) { 62859004Shm case 'a': 62959004Shm archtodir++; 63059004Shm archdirname = optarg; 63159004Shm break; 632136127Sbrooks case 'd': 633136127Sbrooks destdir = optarg; 634136127Sbrooks break; 635111768Sgad case 'f': 636111768Sgad conf = optarg; 637111768Sgad break; 638111768Sgad case 'n': 639111768Sgad noaction++; 640111768Sgad break; 64159003Shm case 'r': 64259003Shm needroot = 0; 64359003Shm break; 644111768Sgad case 's': 645111768Sgad nosignal = 1; 646111768Sgad break; 647210372Ssimon case 't': 648210372Ssimon if (optarg[0] == '\0' || 649210372Ssimon strcmp(optarg, "DEFAULT") == 0) 650210372Ssimon timefnamefmt = strdup(DEFAULT_TIMEFNAME_FMT); 651210372Ssimon else 652210372Ssimon timefnamefmt = strdup(optarg); 653210372Ssimon break; 65459003Shm case 'v': 65559003Shm verbose++; 65659003Shm break; 657114137Sgad case 'C': 658114137Sgad /* Useful for things like rc.diskless... */ 659114137Sgad createlogs++; 660114137Sgad break; 661119904Sgad case 'D': 662119904Sgad /* 663119904Sgad * Set some debugging option. The specific option 664119904Sgad * depends on the value of optarg. These options 665119904Sgad * may come and go without notice or documentation. 666119904Sgad */ 667119904Sgad if (parse_doption(optarg)) 668119904Sgad break; 669119904Sgad usage(); 670119904Sgad /* NOTREACHED */ 67134584Spst case 'F': 67234584Spst force++; 67334584Spst break; 674143106Sbrooks case 'N': 675143106Sbrooks norotate++; 676143106Sbrooks break; 677202668Sdelphij case 'P': 678202668Sdelphij enforcepid++; 679202668Sdelphij break; 680111772Sgad case 'R': 681111772Sgad rotatereq++; 682111772Sgad requestor = strdup(optarg); 683111772Sgad break; 684210407Sbrian case 'S': 685210407Sbrian path_syslogpid = optarg; 686210407Sbrian break; 687111781Sgad case 'm': /* Used by OpenBSD for "monitor mode" */ 68859003Shm default: 68959003Shm usage(); 690111768Sgad /* NOTREACHED */ 69159003Shm } 692111772Sgad 693143106Sbrooks if (force && norotate) { 694143106Sbrooks warnx("Only one of -F and -N may be specified."); 695143106Sbrooks usage(); 696143106Sbrooks /* NOTREACHED */ 697143106Sbrooks } 698143106Sbrooks 699111772Sgad if (rotatereq) { 700111772Sgad if (optind == argc) { 701111772Sgad warnx("At least one filename must be given when -R is specified."); 702111772Sgad usage(); 703111772Sgad /* NOTREACHED */ 704111772Sgad } 705111772Sgad /* Make sure "requestor" value is safe for a syslog message. */ 706111772Sgad for (p = requestor; *p != '\0'; p++) { 707111772Sgad if (!isprintch(*p) && (*p != '\t')) 708111772Sgad *p = '.'; 709111772Sgad } 710111772Sgad } 711119904Sgad 712119904Sgad if (dbg_timenow) { 713119904Sgad /* 714119904Sgad * Note that the 'daytime' variable is not changed. 715119904Sgad * That is only used in messages that track when a 716119904Sgad * logfile is rotated, and if a file *is* rotated, 717119904Sgad * then it will still rotated at the "real now" time. 718119904Sgad */ 719120361Sgad ptime_free(timenow); 720119904Sgad timenow = dbg_timenow; 721119904Sgad fprintf(stderr, "Debug: Running as if TimeNow is %s", 722120361Sgad ptimeget_ctime(dbg_timenow)); 723119904Sgad } 724119904Sgad 72543071Swollman} 72613244Sgraichen 727120361Sgad/* 728120361Sgad * These debugging options are mainly meant for developer use, such 729120361Sgad * as writing regression-tests. They would not be needed by users 730120361Sgad * during normal operation of newsyslog... 731120361Sgad */ 732119904Sgadstatic int 733119904Sgadparse_doption(const char *doption) 734119904Sgad{ 735119904Sgad const char TN[] = "TN="; 736120361Sgad int res; 737119904Sgad 738119904Sgad if (strncmp(doption, TN, sizeof(TN) - 1) == 0) { 739119904Sgad /* 740120361Sgad * The "TimeNow" debugging option. This might be off 741120361Sgad * by an hour when crossing a timezone change. 742119904Sgad */ 743120361Sgad dbg_timenow = ptime_init(NULL); 744120361Sgad res = ptime_relparse(dbg_timenow, PTM_PARSE_ISO8601, 745120361Sgad time(NULL), doption + sizeof(TN) - 1); 746120361Sgad if (res == -2) { 747120361Sgad warnx("Non-existent time specified on -D %s", doption); 748120361Sgad return (0); /* failure */ 749120361Sgad } else if (res < 0) { 750119904Sgad warnx("Malformed time given on -D %s", doption); 751119904Sgad return (0); /* failure */ 752119904Sgad } 753119904Sgad return (1); /* successfully parsed */ 754119904Sgad 755119904Sgad } 756119904Sgad 757120361Sgad if (strcmp(doption, "ats") == 0) { 758120361Sgad dbg_at_times++; 759120361Sgad return (1); /* successfully parsed */ 760120361Sgad } 761120361Sgad 762159968Sgad /* XXX - This check could probably be dropped. */ 763159968Sgad if ((strcmp(doption, "neworder") == 0) || (strcmp(doption, "oldorder") 764159968Sgad == 0)) { 765159968Sgad warnx("NOTE: newsyslog always uses 'neworder'."); 766130167Sgad return (1); /* successfully parsed */ 767130167Sgad } 768130167Sgad 769130165Sgad warnx("Unknown -D (debug) option: '%s'", doption); 770119904Sgad return (0); /* failure */ 771119904Sgad} 772119904Sgad 77359004Shmstatic void 77459004Shmusage(void) 77513244Sgraichen{ 77680646Sobrien 77780646Sobrien fprintf(stderr, 778143106Sbrooks "usage: newsyslog [-CFNnrsv] [-a directory] [-d directory] [-f config-file]\n" 779210407Sbrian " [-S pidfile] [-t timefmt ] [ [-R requestor] filename ... ]\n"); 78059003Shm exit(1); 78113244Sgraichen} 78213244Sgraichen 78359004Shm/* 784111773Sgad * Parse a configuration file and return a linked list of all the logs 785111773Sgad * which should be processed. 78613244Sgraichen */ 787208648Sgordonstatic struct cflist * 788111773Sgadget_worklist(char **files) 78913244Sgraichen{ 79059003Shm FILE *f; 791111773Sgad char **given; 792208649Sgordon struct cflist *cmdlist, *filelist, *globlist; 793208648Sgordon struct conf_entry *defconf, *dupent, *ent; 794208649Sgordon struct ilist inclist; 795208649Sgordon struct include_entry *inc; 796112020Sgad int gmatch, fnres; 797111773Sgad 798208648Sgordon defconf = NULL; 799208649Sgordon STAILQ_INIT(&inclist); 800111773Sgad 801208648Sgordon filelist = malloc(sizeof(struct cflist)); 802208648Sgordon if (filelist == NULL) 803208648Sgordon err(1, "malloc of filelist"); 804208648Sgordon STAILQ_INIT(filelist); 805208648Sgordon globlist = malloc(sizeof(struct cflist)); 806208648Sgordon if (globlist == NULL) 807208648Sgordon err(1, "malloc of globlist"); 808208648Sgordon STAILQ_INIT(globlist); 809208648Sgordon 810208649Sgordon inc = malloc(sizeof(struct include_entry)); 811208649Sgordon if (inc == NULL) 812208649Sgordon err(1, "malloc of inc"); 813208649Sgordon inc->file = conf; 814208649Sgordon if (inc->file == NULL) 815208649Sgordon inc->file = _PATH_CONF; 816208649Sgordon STAILQ_INSERT_TAIL(&inclist, inc, inc_nextp); 817111773Sgad 818208649Sgordon STAILQ_FOREACH(inc, &inclist, inc_nextp) { 819208649Sgordon if (strcmp(inc->file, "-") != 0) 820208649Sgordon f = fopen(inc->file, "r"); 821208649Sgordon else { 822208649Sgordon f = stdin; 823208649Sgordon inc->file = "<stdin>"; 824208649Sgordon } 825208649Sgordon if (!f) 826208649Sgordon err(1, "%s", inc->file); 827208649Sgordon 828208649Sgordon if (verbose) 829208649Sgordon printf("Processing %s\n", inc->file); 830208649Sgordon parse_file(f, filelist, globlist, defconf, &inclist); 831208649Sgordon (void) fclose(f); 832111773Sgad } 833111773Sgad 834111773Sgad /* 835111773Sgad * All config-file information has been read in and turned into 836208648Sgordon * a filelist and a globlist. If there were no specific files 837112020Sgad * given on the run command, then the only thing left to do is to 838112020Sgad * call a routine which finds all files matched by the globlist 839208648Sgordon * and adds them to the filelist. Then return the worklist. 840111773Sgad */ 841111773Sgad if (*files == NULL) { 842208648Sgordon expand_globs(filelist, globlist); 843208648Sgordon free_clist(globlist); 844111773Sgad if (defconf != NULL) 845111773Sgad free_entry(defconf); 846208648Sgordon return (filelist); 847111773Sgad /* NOTREACHED */ 848111773Sgad } 849111773Sgad 850111773Sgad /* 851111773Sgad * If newsyslog was given a specific list of files to process, 852111773Sgad * it may be that some of those files were not listed in any 853111773Sgad * config file. Those unlisted files should get the default 854111773Sgad * rotation action. First, create the default-rotation action 855111773Sgad * if none was found in a system config file. 856111773Sgad */ 857111773Sgad if (defconf == NULL) { 858111773Sgad defconf = init_entry(DEFAULT_MARKER, NULL); 859111773Sgad defconf->numlogs = 3; 860130165Sgad defconf->trsize = 50; 861111773Sgad defconf->permissions = S_IRUSR|S_IWUSR; 862111773Sgad } 863111773Sgad 864111773Sgad /* 865111773Sgad * If newsyslog was run with a list of specific filenames, 866111773Sgad * then create a new worklist which has only those files in 867111773Sgad * it, picking up the rotation-rules for those files from 868208648Sgordon * the original filelist. 869111773Sgad * 870111773Sgad * XXX - Note that this will copy multiple rules for a single 871111773Sgad * logfile, if multiple entries are an exact match for 872111773Sgad * that file. That matches the historic behavior, but do 873111773Sgad * we want to continue to allow it? If so, it should 874111773Sgad * probably be handled more intelligently. 875111773Sgad */ 876208648Sgordon cmdlist = malloc(sizeof(struct cflist)); 877208648Sgordon if (cmdlist == NULL) 878208648Sgordon err(1, "malloc of cmdlist"); 879208648Sgordon STAILQ_INIT(cmdlist); 880208648Sgordon 881111773Sgad for (given = files; *given; ++given) { 882111773Sgad /* 883111773Sgad * First try to find exact-matches for this given file. 884111773Sgad */ 885112020Sgad gmatch = 0; 886208648Sgordon STAILQ_FOREACH(ent, filelist, cf_nextp) { 887111773Sgad if (strcmp(ent->log, *given) == 0) { 888111773Sgad gmatch++; 889111773Sgad dupent = init_entry(*given, ent); 890208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 891111773Sgad } 892111773Sgad } 893111773Sgad if (gmatch) { 894111773Sgad if (verbose > 2) 895111773Sgad printf("\t+ Matched entry %s\n", *given); 896111773Sgad continue; 897111773Sgad } 898111773Sgad 899111773Sgad /* 900111773Sgad * There was no exact-match for this given file, so look 901111773Sgad * for a "glob" entry which does match. 902111773Sgad */ 903112020Sgad gmatch = 0; 904112020Sgad if (verbose > 2 && globlist != NULL) 905112020Sgad printf("\t+ Checking globs for %s\n", *given); 906208648Sgordon STAILQ_FOREACH(ent, globlist, cf_nextp) { 907112020Sgad fnres = fnmatch(ent->log, *given, FNM_PATHNAME); 908112020Sgad if (verbose > 2) 909112020Sgad printf("\t+ = %d for pattern %s\n", fnres, 910112020Sgad ent->log); 911112020Sgad if (fnres == 0) { 912111773Sgad gmatch++; 913111773Sgad dupent = init_entry(*given, ent); 914112020Sgad /* This new entry is not a glob! */ 915111773Sgad dupent->flags &= ~CE_GLOB; 916208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 917111773Sgad /* Only allow a match to one glob-entry */ 918111773Sgad break; 919111773Sgad } 920111773Sgad } 921111773Sgad if (gmatch) { 922111773Sgad if (verbose > 2) 923111773Sgad printf("\t+ Matched %s via %s\n", *given, 924111773Sgad ent->log); 925111773Sgad continue; 926111773Sgad } 927111773Sgad 928111773Sgad /* 929111773Sgad * This given file was not found in any config file, so 930111773Sgad * add a worklist item based on the default entry. 931111773Sgad */ 932111773Sgad if (verbose > 2) 933111773Sgad printf("\t+ No entry matched %s (will use %s)\n", 934111773Sgad *given, DEFAULT_MARKER); 935111773Sgad dupent = init_entry(*given, defconf); 936111773Sgad /* Mark that it was *not* found in a config file */ 937111773Sgad dupent->def_cfg = 1; 938208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 939111773Sgad } 940111773Sgad 941111773Sgad /* 942112020Sgad * Free all the entries in the original work list, the list of 943112020Sgad * glob entries, and the default entry. 944111773Sgad */ 945208648Sgordon free_clist(filelist); 946208648Sgordon free_clist(globlist); 947112020Sgad free_entry(defconf); 948111773Sgad 949112020Sgad /* And finally, return a worklist which matches the given files. */ 950208648Sgordon return (cmdlist); 951111773Sgad} 952111773Sgad 953111773Sgad/* 954112020Sgad * Expand the list of entries with filename patterns, and add all files 955112020Sgad * which match those glob-entries onto the worklist. 956112020Sgad */ 957112020Sgadstatic void 958208648Sgordonexpand_globs(struct cflist *work_p, struct cflist *glob_p) 959112020Sgad{ 960161412Sdelphij int gmatch, gres; 961161412Sdelphij size_t i; 962112020Sgad char *mfname; 963208648Sgordon struct conf_entry *dupent, *ent, *globent; 964112020Sgad glob_t pglob; 965112020Sgad struct stat st_fm; 966112020Sgad 967112020Sgad /* 968112020Sgad * The worklist contains all fully-specified (non-GLOB) names. 969112020Sgad * 970112020Sgad * Now expand the list of filename-pattern (GLOB) entries into 971112020Sgad * a second list, which (by definition) will only match files 972112020Sgad * that already exist. Do not add a glob-related entry for any 973112020Sgad * file which already exists in the fully-specified list. 974112020Sgad */ 975208648Sgordon STAILQ_FOREACH(globent, glob_p, cf_nextp) { 976112020Sgad gres = glob(globent->log, GLOB_NOCHECK, NULL, &pglob); 977112020Sgad if (gres != 0) { 978112020Sgad warn("cannot expand pattern (%d): %s", gres, 979112020Sgad globent->log); 980112020Sgad continue; 981112020Sgad } 982112020Sgad 983112020Sgad if (verbose > 2) 984112020Sgad printf("\t+ Expanding pattern %s\n", globent->log); 985112020Sgad for (i = 0; i < pglob.gl_matchc; i++) { 986112020Sgad mfname = pglob.gl_pathv[i]; 987112020Sgad 988112020Sgad /* See if this file already has a specific entry. */ 989112020Sgad gmatch = 0; 990208648Sgordon STAILQ_FOREACH(ent, work_p, cf_nextp) { 991112020Sgad if (strcmp(mfname, ent->log) == 0) { 992112020Sgad gmatch++; 993112020Sgad break; 994112020Sgad } 995112020Sgad } 996112020Sgad if (gmatch) 997112020Sgad continue; 998112020Sgad 999112020Sgad /* Make sure the named matched is a file. */ 1000112020Sgad gres = lstat(mfname, &st_fm); 1001112020Sgad if (gres != 0) { 1002112020Sgad /* Error on a file that glob() matched?!? */ 1003112020Sgad warn("Skipping %s - lstat() error", mfname); 1004112020Sgad continue; 1005112020Sgad } 1006112020Sgad if (!S_ISREG(st_fm.st_mode)) { 1007112020Sgad /* We only rotate files! */ 1008112020Sgad if (verbose > 2) 1009112020Sgad printf("\t+ . skipping %s (!file)\n", 1010112020Sgad mfname); 1011112020Sgad continue; 1012112020Sgad } 1013112020Sgad 1014112020Sgad if (verbose > 2) 1015112020Sgad printf("\t+ . add file %s\n", mfname); 1016112020Sgad dupent = init_entry(mfname, globent); 1017112020Sgad /* This new entry is not a glob! */ 1018112020Sgad dupent->flags &= ~CE_GLOB; 1019208648Sgordon 1020208648Sgordon /* Add to the worklist. */ 1021208648Sgordon STAILQ_INSERT_TAIL(work_p, dupent, cf_nextp); 1022112020Sgad } 1023112020Sgad globfree(&pglob); 1024112020Sgad if (verbose > 2) 1025112020Sgad printf("\t+ Done with pattern %s\n", globent->log); 1026112020Sgad } 1027112020Sgad} 1028112020Sgad 1029112020Sgad/* 1030111773Sgad * Parse a configuration file and update a linked list of all the logs to 1031111773Sgad * process. 1032111773Sgad */ 1033111773Sgadstatic void 1034208648Sgordonparse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p, 1035208649Sgordon struct conf_entry *defconf_p, struct ilist *inclist) 1036111773Sgad{ 103759003Shm char line[BUFSIZ], *parse, *q; 1038107737Ssobomax char *cp, *errline, *group; 1039208648Sgordon struct conf_entry *working; 1040111781Sgad struct passwd *pwd; 104159003Shm struct group *grp; 1042208649Sgordon glob_t pglob; 1043120361Sgad int eol, ptm_opts, res, special; 1044208649Sgordon size_t i; 104513244Sgraichen 1046130165Sgad errline = NULL; 1047111773Sgad while (fgets(line, BUFSIZ, cf)) { 1048107737Ssobomax if ((line[0] == '\n') || (line[0] == '#') || 1049107737Ssobomax (strlen(line) == 0)) 105059003Shm continue; 1051130165Sgad if (errline != NULL) 1052130165Sgad free(errline); 105359003Shm errline = strdup(line); 1054107737Ssobomax for (cp = line + 1; *cp != '\0'; cp++) { 1055107737Ssobomax if (*cp != '#') 1056107737Ssobomax continue; 1057107737Ssobomax if (*(cp - 1) == '\\') { 1058107737Ssobomax strcpy(cp - 1, cp); 1059107737Ssobomax cp--; 1060107737Ssobomax continue; 1061107737Ssobomax } 1062107737Ssobomax *cp = '\0'; 1063107737Ssobomax break; 1064107737Ssobomax } 106560373Sdes 106660373Sdes q = parse = missing_field(sob(line), errline); 106760373Sdes parse = son(line); 106860373Sdes if (!*parse) 106980646Sobrien errx(1, "malformed line (missing fields):\n%s", 107080646Sobrien errline); 107160373Sdes *parse = '\0'; 107260373Sdes 1073130165Sgad /* 1074130165Sgad * Allow people to set debug options via the config file. 1075208648Sgordon * (NOTE: debug options are undocumented, and may disappear 1076130165Sgad * at any time, etc). 1077130165Sgad */ 1078130165Sgad if (strcasecmp(DEBUG_MARKER, q) == 0) { 1079130165Sgad q = parse = missing_field(sob(++parse), errline); 1080130165Sgad parse = son(parse); 1081130165Sgad if (!*parse) 1082130165Sgad warnx("debug line specifies no option:\n%s", 1083130165Sgad errline); 1084130165Sgad else { 1085130165Sgad *parse = '\0'; 1086130165Sgad parse_doption(q); 1087130165Sgad } 1088130165Sgad continue; 1089208649Sgordon } else if (strcasecmp(INCLUDE_MARKER, q) == 0) { 1090208649Sgordon if (verbose) 1091208649Sgordon printf("Found: %s", errline); 1092208649Sgordon q = parse = missing_field(sob(++parse), errline); 1093208649Sgordon parse = son(parse); 1094208649Sgordon if (!*parse) { 1095208649Sgordon warnx("include line missing argument:\n%s", 1096208649Sgordon errline); 1097208649Sgordon continue; 1098208649Sgordon } 1099208649Sgordon 1100208649Sgordon *parse = '\0'; 1101208649Sgordon 1102208649Sgordon if (isglobstr(q)) { 1103208649Sgordon res = glob(q, GLOB_NOCHECK, NULL, &pglob); 1104208649Sgordon if (res != 0) { 1105208649Sgordon warn("cannot expand pattern (%d): %s", 1106208649Sgordon res, q); 1107208649Sgordon continue; 1108208649Sgordon } 1109208649Sgordon 1110208649Sgordon if (verbose > 2) 1111208649Sgordon printf("\t+ Expanding pattern %s\n", q); 1112208649Sgordon 1113208649Sgordon for (i = 0; i < pglob.gl_matchc; i++) 1114208649Sgordon add_to_queue(pglob.gl_pathv[i], 1115208649Sgordon inclist); 1116208649Sgordon globfree(&pglob); 1117208649Sgordon } else 1118208649Sgordon add_to_queue(q, inclist); 1119208649Sgordon continue; 1120130165Sgad } 1121130165Sgad 1122112020Sgad special = 0; 1123111388Sgad working = init_entry(q, NULL); 1124111388Sgad if (strcasecmp(DEFAULT_MARKER, q) == 0) { 1125112020Sgad special = 1; 1126208648Sgordon if (defconf_p != NULL) { 1127111388Sgad warnx("Ignoring duplicate entry for %s!", q); 1128111388Sgad free_entry(working); 1129111388Sgad continue; 1130111388Sgad } 1131208648Sgordon defconf_p = working; 113259003Shm } 113313244Sgraichen 113459003Shm q = parse = missing_field(sob(++parse), errline); 113559003Shm parse = son(parse); 113625518Sbrian if (!*parse) 113780646Sobrien errx(1, "malformed line (missing fields):\n%s", 113880646Sobrien errline); 113959003Shm *parse = '\0'; 114059003Shm if ((group = strchr(q, ':')) != NULL || 114159003Shm (group = strrchr(q, '.')) != NULL) { 114259003Shm *group++ = '\0'; 114359003Shm if (*q) { 1144119102Sgad if (!(isnumberstr(q))) { 1145111781Sgad if ((pwd = getpwnam(q)) == NULL) 114659003Shm errx(1, 114780646Sobrien "error in config file; unknown user:\n%s", 114859003Shm errline); 1149111781Sgad working->uid = pwd->pw_uid; 115059003Shm } else 115159003Shm working->uid = atoi(q); 115259003Shm } else 1153111779Sgad working->uid = (uid_t)-1; 115413244Sgraichen 115559003Shm q = group; 115659003Shm if (*q) { 1157119102Sgad if (!(isnumberstr(q))) { 115859003Shm if ((grp = getgrnam(q)) == NULL) 115959003Shm errx(1, 116080646Sobrien "error in config file; unknown group:\n%s", 116159003Shm errline); 116259003Shm working->gid = grp->gr_gid; 116359003Shm } else 116459003Shm working->gid = atoi(q); 116559003Shm } else 1166111779Sgad working->gid = (gid_t)-1; 116713244Sgraichen 116859003Shm q = parse = missing_field(sob(++parse), errline); 116959003Shm parse = son(parse); 117059003Shm if (!*parse) 117180646Sobrien errx(1, "malformed line (missing fields):\n%s", 117280646Sobrien errline); 117359003Shm *parse = '\0'; 1174111779Sgad } else { 1175111779Sgad working->uid = (uid_t)-1; 1176111779Sgad working->gid = (gid_t)-1; 1177111779Sgad } 117859003Shm 117959003Shm if (!sscanf(q, "%o", &working->permissions)) 118059003Shm errx(1, "error in config file; bad permissions:\n%s", 118159003Shm errline); 118259003Shm 118359003Shm q = parse = missing_field(sob(++parse), errline); 118459003Shm parse = son(parse); 118525518Sbrian if (!*parse) 118680646Sobrien errx(1, "malformed line (missing fields):\n%s", 118780646Sobrien errline); 118859003Shm *parse = '\0'; 1189111400Sgad if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0) 1190111400Sgad errx(1, "error in config file; bad value for count of logs to save:\n%s", 119159003Shm errline); 119213244Sgraichen 119359003Shm q = parse = missing_field(sob(++parse), errline); 119459003Shm parse = son(parse); 119525518Sbrian if (!*parse) 119680646Sobrien errx(1, "malformed line (missing fields):\n%s", 119780646Sobrien errline); 119859003Shm *parse = '\0'; 1199114762Sgad if (isdigitch(*q)) 1200130165Sgad working->trsize = atoi(q); 1201130165Sgad else if (strcmp(q, "*") == 0) 1202130165Sgad working->trsize = -1; 1203114762Sgad else { 1204114762Sgad warnx("Invalid value of '%s' for 'size' in line:\n%s", 1205114762Sgad q, errline); 1206130165Sgad working->trsize = -1; 1207114762Sgad } 120859003Shm 120959003Shm working->flags = 0; 1210218127Smm working->compress = COMPRESS_NONE; 121159003Shm q = parse = missing_field(sob(++parse), errline); 121259003Shm parse = son(parse); 121325518Sbrian eol = !*parse; 121459003Shm *parse = '\0'; 121543071Swollman { 121659003Shm char *ep; 121759003Shm u_long ul; 121813244Sgraichen 121943071Swollman ul = strtoul(q, &ep, 10); 122043071Swollman if (ep == q) 122143071Swollman working->hours = 0; 122243071Swollman else if (*ep == '*') 122343071Swollman working->hours = -1; 122443071Swollman else if (ul > INT_MAX) 122543071Swollman errx(1, "interval is too large:\n%s", errline); 122643071Swollman else 122743071Swollman working->hours = ul; 122843071Swollman 1229120361Sgad if (*ep == '\0' || strcmp(ep, "*") == 0) 1230120361Sgad goto no_trimat; 1231120361Sgad if (*ep != '@' && *ep != '$') 123243071Swollman errx(1, "malformed interval/at:\n%s", errline); 1233120361Sgad 1234120361Sgad working->flags |= CE_TRIMAT; 1235120361Sgad working->trim_at = ptime_init(NULL); 1236120361Sgad ptm_opts = PTM_PARSE_ISO8601; 1237120361Sgad if (*ep == '$') 1238120361Sgad ptm_opts = PTM_PARSE_DWM; 1239120361Sgad ptm_opts |= PTM_PARSE_MATCHDOM; 1240120361Sgad res = ptime_relparse(working->trim_at, ptm_opts, 1241120361Sgad ptimeget_secs(timenow), ep + 1); 1242120361Sgad if (res == -2) 1243120361Sgad errx(1, "nonexistent time for 'at' value:\n%s", 1244120361Sgad errline); 1245120361Sgad else if (res < 0) 1246120361Sgad errx(1, "malformed 'at' value:\n%s", errline); 124743071Swollman } 1248120361Sgadno_trimat: 124943071Swollman 125025518Sbrian if (eol) 125159003Shm q = NULL; 125225518Sbrian else { 125359003Shm q = parse = sob(++parse); /* Optional field */ 125459003Shm parse = son(parse); 125559003Shm if (!*parse) 125659003Shm eol = 1; 125759003Shm *parse = '\0'; 125825518Sbrian } 125925443Sache 1260111768Sgad for (; q && *q && !isspacech(*q); q++) { 1261111768Sgad switch (tolowerch(*q)) { 1262111768Sgad case 'b': 126359003Shm working->flags |= CE_BINARY; 1264111768Sgad break; 1265114137Sgad case 'c': 1266111768Sgad /* 1267114137Sgad * XXX - Ick! Ugly! Remove ASAP! 1268114137Sgad * We want `c' and `C' for "create". But we 1269114137Sgad * will temporarily treat `c' as `g', because 1270114137Sgad * FreeBSD releases <= 4.8 have a typo of 1271114137Sgad * checking ('G' || 'c') for CE_GLOB. 1272111768Sgad */ 1273114137Sgad if (*q == 'c') { 1274114137Sgad warnx("Assuming 'g' for 'c' in flags for line:\n%s", 1275114137Sgad errline); 1276114137Sgad warnx("The 'c' flag will eventually mean 'CREATE'"); 1277114137Sgad working->flags |= CE_GLOB; 1278114137Sgad break; 1279114137Sgad } 1280114137Sgad working->flags |= CE_CREATE; 1281114137Sgad break; 1282130043Sgad case 'd': 1283130043Sgad working->flags |= CE_NODUMP; 1284130043Sgad break; 1285111768Sgad case 'g': 1286106905Ssobomax working->flags |= CE_GLOB; 1287111768Sgad break; 1288111768Sgad case 'j': 1289218127Smm working->compress = COMPRESS_BZIP2; 1290111768Sgad break; 1291111768Sgad case 'n': 1292111768Sgad working->flags |= CE_NOSIGNAL; 1293111768Sgad break; 1294112003Sgad case 'u': 1295112003Sgad working->flags |= CE_SIGNALGROUP; 1296112003Sgad break; 1297111768Sgad case 'w': 1298160560Ssobomax /* Depreciated flag - keep for compatibility purposes */ 1299111768Sgad break; 1300218127Smm case 'x': 1301218127Smm working->compress = COMPRESS_XZ; 1302218127Smm break; 1303111768Sgad case 'z': 1304218127Smm working->compress = COMPRESS_GZIP; 1305111768Sgad break; 1306111768Sgad case '-': 1307111768Sgad break; 1308111781Sgad case 'f': /* Used by OpenBSD for "CE_FOLLOW" */ 1309111781Sgad case 'm': /* Used by OpenBSD for "CE_MONITOR" */ 1310111781Sgad case 'p': /* Used by NetBSD for "CE_PLAIN0" */ 1311111768Sgad default: 131280646Sobrien errx(1, "illegal flag in config file -- %c", 131380646Sobrien *q); 1314111768Sgad } 131559003Shm } 131659003Shm 131725518Sbrian if (eol) 131859003Shm q = NULL; 131925518Sbrian else { 132059003Shm q = parse = sob(++parse); /* Optional field */ 132159003Shm parse = son(parse); 132259003Shm if (!*parse) 132359003Shm eol = 1; 132459003Shm *parse = '\0'; 132525518Sbrian } 132625443Sache 132725443Sache working->pid_file = NULL; 132825443Sache if (q && *q) { 132925443Sache if (*q == '/') 133025443Sache working->pid_file = strdup(q); 133136817Sache else if (isdigit(*q)) 133236817Sache goto got_sig; 133359003Shm else 133480646Sobrien errx(1, 133580646Sobrien "illegal pid file or signal number in config file:\n%s", 133680646Sobrien errline); 133725443Sache } 133836817Sache if (eol) 133959003Shm q = NULL; 134036817Sache else { 134159003Shm q = parse = sob(++parse); /* Optional field */ 134259003Shm *(parse = son(parse)) = '\0'; 134336817Sache } 134436817Sache 134536817Sache working->sig = SIGHUP; 134636817Sache if (q && *q) { 134736817Sache if (isdigit(*q)) { 134859003Shm got_sig: 134936817Sache working->sig = atoi(q); 135036817Sache } else { 135159003Shm err_sig: 135280646Sobrien errx(1, 135380646Sobrien "illegal signal number in config file:\n%s", 135480646Sobrien errline); 135536817Sache } 135636817Sache if (working->sig < 1 || working->sig >= NSIG) 135736817Sache goto err_sig; 135836817Sache } 1359111768Sgad 1360111768Sgad /* 1361111768Sgad * Finish figuring out what pid-file to use (if any) in 1362111768Sgad * later processing if this logfile needs to be rotated. 1363111768Sgad */ 1364111768Sgad if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) { 1365111768Sgad /* 1366111768Sgad * This config-entry specified 'n' for nosignal, 1367111768Sgad * see if it also specified an explicit pid_file. 1368111768Sgad * This would be a pretty pointless combination. 1369111768Sgad */ 1370111768Sgad if (working->pid_file != NULL) { 1371111768Sgad warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s", 1372111768Sgad working->pid_file, errline); 1373111768Sgad free(working->pid_file); 1374111768Sgad working->pid_file = NULL; 1375111768Sgad } 1376111768Sgad } else if (working->pid_file == NULL) { 1377111768Sgad /* 1378111768Sgad * This entry did not specify the 'n' flag, which 1379111768Sgad * means it should signal syslogd unless it had 1380112003Sgad * specified some other pid-file (and obviously the 1381112003Sgad * syslog pid-file will not be for a process-group). 1382112003Sgad * Also, we should only try to notify syslog if we 1383112003Sgad * are root. 1384111768Sgad */ 1385112003Sgad if (working->flags & CE_SIGNALGROUP) { 1386112003Sgad warnx("Ignoring flag 'U' in line:\n%s", 1387112003Sgad errline); 1388112003Sgad working->flags &= ~CE_SIGNALGROUP; 1389112003Sgad } 1390111768Sgad if (needroot) 1391210407Sbrian working->pid_file = strdup(path_syslogpid); 1392111768Sgad } 1393111768Sgad 1394112020Sgad /* 1395112020Sgad * Add this entry to the appropriate list of entries, unless 1396112020Sgad * it was some kind of special entry (eg: <default>). 1397112020Sgad */ 1398112020Sgad if (special) { 1399112020Sgad ; /* Do not add to any list */ 1400112020Sgad } else if (working->flags & CE_GLOB) { 1401208648Sgordon STAILQ_INSERT_TAIL(glob_p, working, cf_nextp); 1402112020Sgad } else { 1403208648Sgordon STAILQ_INSERT_TAIL(work_p, working, cf_nextp); 1404112020Sgad } 1405130165Sgad } 1406130165Sgad if (errline != NULL) 140759003Shm free(errline); 140813244Sgraichen} 140913244Sgraichen 141059003Shmstatic char * 141159004Shmmissing_field(char *p, char *errline) 141213244Sgraichen{ 141380646Sobrien 141459003Shm if (!p || !*p) 141559003Shm errx(1, "missing field in config file:\n%s", errline); 141659003Shm return (p); 141713244Sgraichen} 141813244Sgraichen 1419208649Sgordon/* 1420210372Ssimon * In our sort we return it in the reverse of what qsort normally 1421210372Ssimon * would do, as we want the newest files first. If we have two 1422210372Ssimon * entries with the same time we don't really care about order. 1423210372Ssimon * 1424210372Ssimon * Support function for qsort() in delete_oldest_timelog(). 1425210372Ssimon */ 1426210372Ssimonstatic int 1427210372Ssimonoldlog_entry_compare(const void *a, const void *b) 1428210372Ssimon{ 1429210372Ssimon const struct oldlog_entry *ola = a, *olb = b; 1430210372Ssimon 1431210372Ssimon if (ola->t > olb->t) 1432210372Ssimon return (-1); 1433210372Ssimon else if (ola->t < olb->t) 1434210372Ssimon return (1); 1435210372Ssimon else 1436210372Ssimon return (0); 1437210372Ssimon} 1438210372Ssimon 1439210372Ssimon/* 1440210372Ssimon * Delete the oldest logfiles, when using time based filenames. 1441210372Ssimon */ 1442210372Ssimonstatic void 1443210372Ssimondelete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir) 1444210372Ssimon{ 1445210372Ssimon char *logfname, *s, *dir, errbuf[80]; 1446218127Smm int dirfd, i, logcnt, max_logcnt, valid; 1447210372Ssimon struct oldlog_entry *oldlogs; 1448210372Ssimon size_t logfname_len; 1449210372Ssimon struct dirent *dp; 1450210372Ssimon const char *cdir; 1451210372Ssimon struct tm tm; 1452210372Ssimon DIR *dirp; 1453210372Ssimon 1454210372Ssimon oldlogs = malloc(MAX_OLDLOGS * sizeof(struct oldlog_entry)); 1455210372Ssimon max_logcnt = MAX_OLDLOGS; 1456210372Ssimon logcnt = 0; 1457210372Ssimon 1458210372Ssimon if (archive_dir != NULL && archive_dir[0] != '\0') 1459210372Ssimon cdir = archive_dir; 1460210372Ssimon else 1461210372Ssimon if ((cdir = dirname(ent->log)) == NULL) 1462210372Ssimon err(1, "dirname()"); 1463210372Ssimon if ((dir = strdup(cdir)) == NULL) 1464210372Ssimon err(1, "strdup()"); 1465210372Ssimon 1466210372Ssimon if ((s = basename(ent->log)) == NULL) 1467210372Ssimon err(1, "basename()"); 1468210372Ssimon if ((logfname = strdup(s)) == NULL) 1469210372Ssimon err(1, "strdup()"); 1470210372Ssimon logfname_len = strlen(logfname); 1471210372Ssimon if (strcmp(logfname, "/") == 0) 1472210372Ssimon errx(1, "Invalid log filename - became '/'"); 1473210372Ssimon 1474210372Ssimon if (verbose > 2) 1475210372Ssimon printf("Searching for old logs in %s\n", dir); 1476210372Ssimon 1477210372Ssimon /* First we create a 'list' of all archived logfiles */ 1478210372Ssimon if ((dirp = opendir(dir)) == NULL) 1479210372Ssimon err(1, "Cannot open log directory '%s'", dir); 1480210372Ssimon dirfd = dirfd(dirp); 1481210372Ssimon while ((dp = readdir(dirp)) != NULL) { 1482210372Ssimon if (dp->d_type != DT_REG) 1483210372Ssimon continue; 1484210372Ssimon 1485210372Ssimon /* Ignore everything but files with our logfile prefix */ 1486210372Ssimon if (strncmp(dp->d_name, logfname, logfname_len) != 0) 1487210372Ssimon continue; 1488210372Ssimon /* Ignore the actual non-rotated logfile */ 1489210372Ssimon if (dp->d_namlen == logfname_len) 1490210372Ssimon continue; 1491210372Ssimon /* 1492210372Ssimon * Make sure we created have found a logfile, so the 1493210372Ssimon * postfix is valid, IE format is: '.<time>(.[bg]z)?'. 1494210372Ssimon */ 1495210372Ssimon if (dp->d_name[logfname_len] != '.') { 1496210372Ssimon if (verbose) 1497210372Ssimon printf("Ignoring %s which has unexpected " 1498210372Ssimon "extension '%s'\n", dp->d_name, 1499210372Ssimon &dp->d_name[logfname_len]); 1500210372Ssimon continue; 1501210372Ssimon } 1502210372Ssimon if ((s = strptime(&dp->d_name[logfname_len + 1], 1503210372Ssimon timefnamefmt, &tm)) == NULL) { 1504210372Ssimon /* 1505210372Ssimon * We could special case "old" sequentially 1506210372Ssimon * named logfiles here, but we do not as that 1507210372Ssimon * would require special handling to decide 1508210372Ssimon * which one was the oldest compared to "new" 1509210372Ssimon * time based logfiles. 1510210372Ssimon */ 1511210372Ssimon if (verbose) 1512210372Ssimon printf("Ignoring %s which does not " 1513210372Ssimon "match time format\n", dp->d_name); 1514210372Ssimon continue; 1515210372Ssimon } 1516218127Smm 1517218127Smm for (int c = 0; c < COMPRESS_TYPES; c++) 1518218127Smm if (strcmp(s, compress_type[c].suffix) == 0) 1519218127Smm valid = 1; 1520218127Smm if (valid != 1) { 1521218127Smm if (verbose) 1522210372Ssimon printf("Ignoring %s which has unexpected " 1523210372Ssimon "extension '%s'\n", dp->d_name, s); 1524210372Ssimon continue; 1525210372Ssimon } 1526210372Ssimon 1527210372Ssimon /* 1528210372Ssimon * We should now have old an old rotated logfile, so 1529210372Ssimon * add it to the 'list'. 1530210372Ssimon */ 1531210372Ssimon if ((oldlogs[logcnt].t = timegm(&tm)) == -1) 1532210372Ssimon err(1, "Could not convert time string to time value"); 1533210372Ssimon if ((oldlogs[logcnt].fname = strdup(dp->d_name)) == NULL) 1534210372Ssimon err(1, "strdup()"); 1535210372Ssimon logcnt++; 1536210372Ssimon 1537210372Ssimon /* 1538210372Ssimon * It is very unlikely we ever run out of space in the 1539210372Ssimon * logfile array from the default size, but lets 1540210372Ssimon * handle it anyway... 1541210372Ssimon */ 1542210372Ssimon if (logcnt >= max_logcnt) { 1543210372Ssimon max_logcnt *= 4; 1544210372Ssimon /* Detect integer overflow */ 1545210372Ssimon if (max_logcnt < logcnt) 1546210372Ssimon errx(1, "Too many old logfiles found"); 1547210372Ssimon oldlogs = realloc(oldlogs, 1548210372Ssimon max_logcnt * sizeof(struct oldlog_entry)); 1549210372Ssimon if (oldlogs == NULL) 1550210372Ssimon err(1, "realloc()"); 1551210372Ssimon } 1552210372Ssimon } 1553210372Ssimon 1554210372Ssimon /* Second, if needed we delete oldest archived logfiles */ 1555210372Ssimon if (logcnt > 0 && logcnt >= ent->numlogs && ent->numlogs > 1) { 1556210372Ssimon oldlogs = realloc(oldlogs, logcnt * 1557210372Ssimon sizeof(struct oldlog_entry)); 1558210372Ssimon if (oldlogs == NULL) 1559210372Ssimon err(1, "realloc()"); 1560210372Ssimon 1561210372Ssimon /* 1562210372Ssimon * We now sort the logs in the order of newest to 1563210372Ssimon * oldest. That way we can simply skip over the 1564210372Ssimon * number of records we want to keep. 1565210372Ssimon */ 1566210372Ssimon qsort(oldlogs, logcnt, sizeof(struct oldlog_entry), 1567210372Ssimon oldlog_entry_compare); 1568210372Ssimon for (i = ent->numlogs - 1; i < logcnt; i++) { 1569210372Ssimon if (noaction) 1570210372Ssimon printf("\trm -f %s/%s\n", dir, 1571210372Ssimon oldlogs[i].fname); 1572210372Ssimon else if (unlinkat(dirfd, oldlogs[i].fname, 0) != 0) { 1573210372Ssimon snprintf(errbuf, sizeof(errbuf), 1574210372Ssimon "Could not delet old logfile '%s'", 1575210372Ssimon oldlogs[i].fname); 1576210372Ssimon perror(errbuf); 1577210372Ssimon } 1578210372Ssimon } 1579210372Ssimon } else if (verbose > 1) 1580210372Ssimon printf("No old logs to delete for logfile %s\n", ent->log); 1581210372Ssimon 1582210372Ssimon /* Third, cleanup */ 1583210372Ssimon closedir(dirp); 1584210372Ssimon for (i = 0; i < logcnt; i++) { 1585210372Ssimon assert(oldlogs[i].fname != NULL); 1586210372Ssimon free(oldlogs[i].fname); 1587210372Ssimon } 1588210372Ssimon free(oldlogs); 1589210372Ssimon free(logfname); 1590210372Ssimon free(dir); 1591210372Ssimon} 1592210372Ssimon 1593210372Ssimon/* 1594208649Sgordon * Only add to the queue if the file hasn't already been added. This is 1595208649Sgordon * done to prevent circular include loops. 1596208649Sgordon */ 1597208649Sgordonstatic void 1598208649Sgordonadd_to_queue(const char *fname, struct ilist *inclist) 1599208649Sgordon{ 1600208649Sgordon struct include_entry *inc; 1601208649Sgordon 1602208649Sgordon STAILQ_FOREACH(inc, inclist, inc_nextp) { 1603208649Sgordon if (strcmp(fname, inc->file) == 0) { 1604208649Sgordon warnx("duplicate include detected: %s", fname); 1605208649Sgordon return; 1606208649Sgordon } 1607208649Sgordon } 1608208649Sgordon 1609208649Sgordon inc = malloc(sizeof(struct include_entry)); 1610208649Sgordon if (inc == NULL) 1611208649Sgordon err(1, "malloc of inc"); 1612208649Sgordon inc->file = strdup(fname); 1613208649Sgordon 1614208649Sgordon if (verbose > 2) 1615208649Sgordon printf("\t+ Adding %s to the processing queue.\n", fname); 1616208649Sgordon 1617208649Sgordon STAILQ_INSERT_TAIL(inclist, inc, inc_nextp); 1618208649Sgordon} 1619208649Sgordon 1620218127Smm/* 1621218127Smm * Search for logfile and return its compression suffix (if supported) 1622218127Smm * The suffix detection is first-match in the order of compress_types 1623218127Smm * 1624218127Smm * Note: if logfile without suffix exists (uncompressed, COMPRESS_NONE) 1625218127Smm * a zero-length string is returned 1626218127Smm */ 1627218127Smmstatic const char * 1628218127Smmget_logfile_suffix(const char *logfile) 1629218127Smm{ 1630218127Smm struct stat st; 1631218127Smm char zfile[MAXPATHLEN]; 1632218127Smm 1633218127Smm for (int c = 0; c < COMPRESS_TYPES; c++) { 1634218127Smm (void) strlcpy(zfile, logfile, MAXPATHLEN); 1635218127Smm (void) strlcat(zfile, compress_type[c].suffix, MAXPATHLEN); 1636218127Smm if (lstat(zfile, &st) == 0) 1637218127Smm return (compress_type[c].suffix); 1638218127Smm } 1639218127Smm return (NULL); 1640218127Smm} 1641218127Smm 1642130165Sgadstatic fk_entry 1643130165Sgaddo_rotate(const struct conf_entry *ent) 164413244Sgraichen{ 164571299Sjedgar char dirpart[MAXPATHLEN], namepart[MAXPATHLEN]; 164671299Sjedgar char file1[MAXPATHLEN], file2[MAXPATHLEN]; 164771299Sjedgar char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN]; 1648218127Smm const char *logfile_suffix; 1649210372Ssimon char datetimestr[30]; 1650159968Sgad int flags, numlogs_c; 1651130165Sgad fk_entry free_or_keep; 1652159968Sgad struct sigwork_entry *swork; 165359003Shm struct stat st; 1654210372Ssimon struct tm tm; 1655210372Ssimon time_t now; 165613244Sgraichen 1657119927Sgad flags = ent->flags; 1658130165Sgad free_or_keep = FREE_ENT; 1659119927Sgad 166059004Shm if (archtodir) { 166159004Shm char *p; 166213244Sgraichen 166359004Shm /* build complete name of archive directory into dirpart */ 166459004Shm if (*archdirname == '/') { /* absolute */ 166571299Sjedgar strlcpy(dirpart, archdirname, sizeof(dirpart)); 166659004Shm } else { /* relative */ 166759004Shm /* get directory part of logfile */ 1668119927Sgad strlcpy(dirpart, ent->log, sizeof(dirpart)); 166959004Shm if ((p = rindex(dirpart, '/')) == NULL) 167059004Shm dirpart[0] = '\0'; 167159004Shm else 167259004Shm *(p + 1) = '\0'; 167371299Sjedgar strlcat(dirpart, archdirname, sizeof(dirpart)); 167459004Shm } 167559004Shm 167659004Shm /* check if archive directory exists, if not, create it */ 167759004Shm if (lstat(dirpart, &st)) 1678114137Sgad createdir(ent, dirpart); 167959004Shm 168059004Shm /* get filename part of logfile */ 1681119927Sgad if ((p = rindex(ent->log, '/')) == NULL) 1682119927Sgad strlcpy(namepart, ent->log, sizeof(namepart)); 168359004Shm else 168471299Sjedgar strlcpy(namepart, p + 1, sizeof(namepart)); 168559004Shm 168659004Shm /* name of oldest log */ 168780646Sobrien (void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart, 1688119927Sgad namepart, ent->numlogs); 168959004Shm } else { 1690210372Ssimon /* 1691210372Ssimon * Tell delete_oldest_timelog() we are not using an 1692210372Ssimon * archive dir. 1693210372Ssimon */ 1694210372Ssimon dirpart[0] = '\0'; 1695210372Ssimon 169659004Shm /* name of oldest log */ 1697119927Sgad (void) snprintf(file1, sizeof(file1), "%s.%d", ent->log, 1698119927Sgad ent->numlogs); 1699210372Ssimon } 1700210372Ssimon 1701210372Ssimon /* Delete old logs */ 1702210372Ssimon if (timefnamefmt != NULL) 1703210372Ssimon delete_oldest_timelog(ent, dirpart); 1704210372Ssimon else { 1705210372Ssimon /* name of oldest log */ 1706218127Smm for (int c = 0; c < COMPRESS_TYPES; c++) { 1707218127Smm (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, 1708218127Smm compress_type[c].suffix); 1709218127Smm if (noaction) 1710218127Smm printf("\trm -f %s\n", zfile1); 1711218127Smm else 1712218127Smm (void) unlink(zfile1); 1713210372Ssimon } 171459003Shm } 171513244Sgraichen 1716210372Ssimon if (timefnamefmt != NULL) { 1717210372Ssimon /* If time functions fails we can't really do any sensible */ 1718210372Ssimon if (time(&now) == (time_t)-1 || 1719210372Ssimon localtime_r(&now, &tm) == NULL) 1720210372Ssimon bzero(&tm, sizeof(tm)); 1721210372Ssimon 1722210372Ssimon strftime(datetimestr, sizeof(datetimestr), timefnamefmt, &tm); 1723210372Ssimon if (archtodir) 1724210372Ssimon (void) snprintf(file1, sizeof(file1), "%s/%s.%s", 1725210372Ssimon dirpart, namepart, datetimestr); 1726210372Ssimon else 1727210372Ssimon (void) snprintf(file1, sizeof(file1), "%s.%s", 1728210372Ssimon ent->log, datetimestr); 1729210372Ssimon 1730210372Ssimon /* Don't run the code to move down logs */ 1731210372Ssimon numlogs_c = 0; 1732210372Ssimon } else 1733210372Ssimon numlogs_c = ent->numlogs; /* copy for countdown */ 1734210372Ssimon 173559003Shm /* Move down log files */ 1736119927Sgad while (numlogs_c--) { 173759004Shm 173871299Sjedgar (void) strlcpy(file2, file1, sizeof(file2)); 173959004Shm 174059004Shm if (archtodir) 174180646Sobrien (void) snprintf(file1, sizeof(file1), "%s/%s.%d", 1742119927Sgad dirpart, namepart, numlogs_c); 174359004Shm else 1744119927Sgad (void) snprintf(file1, sizeof(file1), "%s.%d", 1745119927Sgad ent->log, numlogs_c); 174659004Shm 1747218127Smm logfile_suffix = get_logfile_suffix(file1); 1748218127Smm if (logfile_suffix == NULL) 1749218127Smm continue; 1750218127Smm (void) strlcpy(zfile1, file1, MAXPATHLEN); 1751218127Smm (void) strlcpy(zfile2, file2, MAXPATHLEN); 1752218127Smm (void) strlcat(zfile1, logfile_suffix, MAXPATHLEN); 1753218127Smm (void) strlcat(zfile2, logfile_suffix, MAXPATHLEN); 1754218127Smm 1755130165Sgad if (noaction) 1756111967Sgad printf("\tmv %s %s\n", zfile1, zfile2); 1757130165Sgad else { 1758130165Sgad /* XXX - Ought to be checking for failure! */ 1759130165Sgad (void)rename(zfile1, zfile2); 176059003Shm } 1761130165Sgad change_attrs(zfile2, ent); 176259003Shm } 176313244Sgraichen 1764130038Sgad if (ent->numlogs > 0) { 1765129974Sgad if (noaction) { 1766130038Sgad /* 1767130038Sgad * Note that savelog() may succeed with using link() 1768130038Sgad * for the archtodir case, but there is no good way 1769130038Sgad * of knowing if it will when doing "noaction", so 1770130038Sgad * here we claim that it will have to do a copy... 1771130038Sgad */ 1772130038Sgad if (archtodir) 1773130038Sgad printf("\tcp %s %s\n", ent->log, file1); 1774130038Sgad else 1775130038Sgad printf("\tln %s %s\n", ent->log, file1); 1776130165Sgad } else { 1777130038Sgad if (!(flags & CE_BINARY)) { 1778130038Sgad /* Report the trimming to the old log */ 1779130038Sgad log_trim(ent->log, ent); 1780130038Sgad } 1781130038Sgad savelog(ent->log, file1); 178259004Shm } 1783130165Sgad change_attrs(file1, ent); 178418075Sjkh } 178518075Sjkh 1786130038Sgad /* Create the new log file and move it into place */ 1787130038Sgad if (noaction) 1788111781Sgad printf("Start new log...\n"); 1789130038Sgad createlog(ent); 179025443Sache 1791111966Sgad /* 1792130167Sgad * Save all signalling and file-compression to be done after log 1793130167Sgad * files from all entries have been rotated. This way any one 1794130167Sgad * process will not be sent the same signal multiple times when 1795130167Sgad * multiple log files had to be rotated. 1796130167Sgad */ 1797159968Sgad swork = NULL; 1798159968Sgad if (ent->pid_file != NULL) 1799159968Sgad swork = save_sigwork(ent); 1800218127Smm if (ent->numlogs > 0 && ent->compress > COMPRESS_NONE) { 1801159968Sgad /* 1802159968Sgad * The zipwork_entry will include a pointer to this 1803159968Sgad * conf_entry, so the conf_entry should not be freed. 1804159968Sgad */ 1805159968Sgad free_or_keep = KEEP_ENT; 1806159968Sgad save_zipwork(ent, swork, ent->fsize, file1); 1807130167Sgad } 1808130167Sgad 1809130165Sgad return (free_or_keep); 181013244Sgraichen} 181113244Sgraichen 1812130167Sgadstatic void 1813130167Sgaddo_sigwork(struct sigwork_entry *swork) 1814130167Sgad{ 1815130204Sgad struct sigwork_entry *nextsig; 1816130204Sgad int kres, secs; 1817130167Sgad 1818130167Sgad if (!(swork->sw_pidok) || swork->sw_pid == 0) 1819130167Sgad return; /* no work to do... */ 1820130167Sgad 1821130167Sgad /* 1822130167Sgad * If nosignal (-s) was specified, then do not signal any process. 1823130167Sgad * Note that a nosignal request triggers a warning message if the 1824130167Sgad * rotated logfile needs to be compressed, *unless* -R was also 1825130167Sgad * specified. We assume that an `-sR' request came from a process 1826130167Sgad * which writes to the logfile, and as such, we assume that process 1827130167Sgad * has already made sure the logfile is not presently in use. This 1828130167Sgad * just sets swork->sw_pidok to a special value, and do_zipwork 1829130167Sgad * will print any necessary warning(s). 1830130167Sgad */ 1831130167Sgad if (nosignal) { 1832130167Sgad if (!rotatereq) 1833130167Sgad swork->sw_pidok = -1; 1834130167Sgad return; 1835130167Sgad } 1836130167Sgad 1837130204Sgad /* 1838130204Sgad * Compute the pause between consecutive signals. Use a longer 1839130204Sgad * sleep time if we will be sending two signals to the same 1840130204Sgad * deamon or process-group. 1841130204Sgad */ 1842130204Sgad secs = 0; 1843130204Sgad nextsig = SLIST_NEXT(swork, sw_nextp); 1844130204Sgad if (nextsig != NULL) { 1845130204Sgad if (swork->sw_pid == nextsig->sw_pid) 1846130204Sgad secs = 10; 1847130204Sgad else 1848130204Sgad secs = 1; 1849130204Sgad } 1850130204Sgad 1851130167Sgad if (noaction) { 1852130204Sgad printf("\tkill -%d %d \t\t# %s\n", swork->sw_signum, 1853130204Sgad (int)swork->sw_pid, swork->sw_fname); 1854130204Sgad if (secs > 0) 1855130204Sgad printf("\tsleep %d\n", secs); 1856130167Sgad return; 1857130167Sgad } 1858130167Sgad 1859130167Sgad kres = kill(swork->sw_pid, swork->sw_signum); 1860130167Sgad if (kres != 0) { 1861130167Sgad /* 1862130167Sgad * Assume that "no such process" (ESRCH) is something 1863130167Sgad * to warn about, but is not an error. Presumably the 1864130167Sgad * process which writes to the rotated log file(s) is 1865130167Sgad * gone, in which case we should have no problem with 1866130167Sgad * compressing the rotated log file(s). 1867130167Sgad */ 1868130167Sgad if (errno != ESRCH) 1869130167Sgad swork->sw_pidok = 0; 1870130167Sgad warn("can't notify %s, pid %d", swork->sw_pidtype, 1871130167Sgad (int)swork->sw_pid); 1872130167Sgad } else { 1873130204Sgad if (verbose) 1874130204Sgad printf("Notified %s pid %d = %s\n", swork->sw_pidtype, 1875130204Sgad (int)swork->sw_pid, swork->sw_fname); 1876130204Sgad if (secs > 0) { 1877130204Sgad if (verbose) 1878130204Sgad printf("Pause %d second(s) between signals\n", 1879130204Sgad secs); 1880130204Sgad sleep(secs); 1881130167Sgad } 1882130167Sgad } 1883130167Sgad} 1884130167Sgad 1885130167Sgadstatic void 1886130167Sgaddo_zipwork(struct zipwork_entry *zwork) 1887130167Sgad{ 1888130167Sgad const char *pgm_name, *pgm_path; 1889154566Sgad int errsav, fcount, zstatus; 1890130167Sgad pid_t pidzip, wpid; 1891130167Sgad char zresult[MAXPATHLEN]; 1892130167Sgad 1893130167Sgad pgm_path = NULL; 1894130167Sgad strlcpy(zresult, zwork->zw_fname, sizeof(zresult)); 1895218127Smm if (zwork != NULL && zwork->zw_conf != NULL && 1896218127Smm zwork->zw_conf->compress > COMPRESS_NONE) 1897218127Smm for (int c = 1; c < COMPRESS_TYPES; c++) { 1898218127Smm if (zwork->zw_conf->compress == c) { 1899218127Smm pgm_path = compress_type[c].path; 1900218127Smm (void) strlcat(zresult, 1901218127Smm compress_type[c].suffix, sizeof(zresult)); 1902218127Smm break; 1903218127Smm } 1904130167Sgad } 1905130167Sgad if (pgm_path == NULL) { 1906130167Sgad warnx("invalid entry for %s in do_zipwork", zwork->zw_fname); 1907130167Sgad return; 1908130167Sgad } 1909154566Sgad pgm_name = strrchr(pgm_path, '/'); 1910154566Sgad if (pgm_name == NULL) 1911154566Sgad pgm_name = pgm_path; 1912154566Sgad else 1913154566Sgad pgm_name++; 1914130167Sgad 1915130167Sgad if (zwork->zw_swork != NULL && zwork->zw_swork->sw_pidok <= 0) { 1916130167Sgad warnx( 1917130167Sgad "log %s not compressed because daemon(s) not notified", 1918130167Sgad zwork->zw_fname); 1919130167Sgad change_attrs(zwork->zw_fname, zwork->zw_conf); 1920130167Sgad return; 1921130167Sgad } 1922130167Sgad 1923130167Sgad if (noaction) { 1924130167Sgad printf("\t%s %s\n", pgm_name, zwork->zw_fname); 1925130167Sgad change_attrs(zresult, zwork->zw_conf); 1926130167Sgad return; 1927130167Sgad } 1928130167Sgad 1929154566Sgad fcount = 1; 1930130167Sgad pidzip = fork(); 1931154566Sgad while (pidzip < 0) { 1932154566Sgad /* 1933154566Sgad * The fork failed. If the failure was due to a temporary 1934154566Sgad * problem, then wait a short time and try it again. 1935154566Sgad */ 1936154566Sgad errsav = errno; 1937154566Sgad warn("fork() for `%s %s'", pgm_name, zwork->zw_fname); 1938154566Sgad if (errsav != EAGAIN || fcount > 5) 1939154566Sgad errx(1, "Exiting..."); 1940154566Sgad sleep(fcount * 12); 1941154566Sgad fcount++; 1942154566Sgad pidzip = fork(); 1943154566Sgad } 1944154566Sgad if (!pidzip) { 1945130167Sgad /* The child process executes the compression command */ 1946130167Sgad execl(pgm_path, pgm_path, "-f", zwork->zw_fname, (char *)0); 1947154566Sgad err(1, "execl(`%s -f %s')", pgm_path, zwork->zw_fname); 1948130167Sgad } 1949130167Sgad 1950130167Sgad wpid = waitpid(pidzip, &zstatus, 0); 1951130167Sgad if (wpid == -1) { 1952154566Sgad /* XXX - should this be a fatal error? */ 1953130167Sgad warn("%s: waitpid(%d)", pgm_path, pidzip); 1954130167Sgad return; 1955130167Sgad } 1956130167Sgad if (!WIFEXITED(zstatus)) { 1957154566Sgad warnx("`%s -f %s' did not terminate normally", pgm_name, 1958154566Sgad zwork->zw_fname); 1959130167Sgad return; 1960130167Sgad } 1961130167Sgad if (WEXITSTATUS(zstatus)) { 1962154566Sgad warnx("`%s -f %s' terminated with a non-zero status (%d)", 1963154566Sgad pgm_name, zwork->zw_fname, WEXITSTATUS(zstatus)); 1964130167Sgad return; 1965130167Sgad } 1966130167Sgad 1967130167Sgad /* Compression was successful, set file attributes on the result. */ 1968130167Sgad change_attrs(zresult, zwork->zw_conf); 1969130167Sgad} 1970130167Sgad 1971130167Sgad/* 1972130167Sgad * Save information on any process we need to signal. Any single 1973130167Sgad * process may need to be sent different signal-values for different 1974130167Sgad * log files, but usually a single signal-value will cause the process 1975130167Sgad * to close and re-open all of it's log files. 1976130167Sgad */ 1977130167Sgadstatic struct sigwork_entry * 1978130167Sgadsave_sigwork(const struct conf_entry *ent) 1979130167Sgad{ 1980130167Sgad struct sigwork_entry *sprev, *stmp; 1981130167Sgad int ndiff; 1982130167Sgad size_t tmpsiz; 1983130167Sgad 1984130167Sgad sprev = NULL; 1985130167Sgad ndiff = 1; 1986130167Sgad SLIST_FOREACH(stmp, &swhead, sw_nextp) { 1987130167Sgad ndiff = strcmp(ent->pid_file, stmp->sw_fname); 1988130167Sgad if (ndiff > 0) 1989130167Sgad break; 1990130167Sgad if (ndiff == 0) { 1991130167Sgad if (ent->sig == stmp->sw_signum) 1992130167Sgad break; 1993130167Sgad if (ent->sig > stmp->sw_signum) { 1994130167Sgad ndiff = 1; 1995130167Sgad break; 1996130167Sgad } 1997130167Sgad } 1998130167Sgad sprev = stmp; 1999130167Sgad } 2000130167Sgad if (stmp != NULL && ndiff == 0) 2001130167Sgad return (stmp); 2002130167Sgad 2003130167Sgad tmpsiz = sizeof(struct sigwork_entry) + strlen(ent->pid_file) + 1; 2004130167Sgad stmp = malloc(tmpsiz); 2005130167Sgad set_swpid(stmp, ent); 2006130167Sgad stmp->sw_signum = ent->sig; 2007130167Sgad strcpy(stmp->sw_fname, ent->pid_file); 2008130167Sgad if (sprev == NULL) 2009130167Sgad SLIST_INSERT_HEAD(&swhead, stmp, sw_nextp); 2010130167Sgad else 2011130167Sgad SLIST_INSERT_AFTER(sprev, stmp, sw_nextp); 2012130167Sgad return (stmp); 2013130167Sgad} 2014130167Sgad 2015130167Sgad/* 2016130167Sgad * Save information on any file we need to compress. We may see the same 2017130167Sgad * file multiple times, so check the full list to avoid duplicates. The 2018130167Sgad * list itself is sorted smallest-to-largest, because that's the order we 2019130167Sgad * want to compress the files. If the partition is very low on disk space, 2020130167Sgad * then the smallest files are the most likely to compress, and compressing 2021130167Sgad * them first will free up more space for the larger files. 2022130167Sgad */ 2023130167Sgadstatic struct zipwork_entry * 2024130167Sgadsave_zipwork(const struct conf_entry *ent, const struct sigwork_entry *swork, 2025130167Sgad int zsize, const char *zipfname) 2026130167Sgad{ 2027130167Sgad struct zipwork_entry *zprev, *ztmp; 2028130167Sgad int ndiff; 2029130167Sgad size_t tmpsiz; 2030130167Sgad 2031130167Sgad /* Compute the size if the caller did not know it. */ 2032130167Sgad if (zsize < 0) 2033130167Sgad zsize = sizefile(zipfname); 2034130167Sgad 2035130167Sgad zprev = NULL; 2036130167Sgad ndiff = 1; 2037130167Sgad SLIST_FOREACH(ztmp, &zwhead, zw_nextp) { 2038130707Sgad ndiff = strcmp(zipfname, ztmp->zw_fname); 2039130167Sgad if (ndiff == 0) 2040130167Sgad break; 2041130167Sgad if (zsize > ztmp->zw_fsize) 2042130167Sgad zprev = ztmp; 2043130167Sgad } 2044130167Sgad if (ztmp != NULL && ndiff == 0) 2045130167Sgad return (ztmp); 2046130167Sgad 2047130167Sgad tmpsiz = sizeof(struct zipwork_entry) + strlen(zipfname) + 1; 2048130167Sgad ztmp = malloc(tmpsiz); 2049130167Sgad ztmp->zw_conf = ent; 2050130167Sgad ztmp->zw_swork = swork; 2051130167Sgad ztmp->zw_fsize = zsize; 2052130167Sgad strcpy(ztmp->zw_fname, zipfname); 2053130167Sgad if (zprev == NULL) 2054130167Sgad SLIST_INSERT_HEAD(&zwhead, ztmp, zw_nextp); 2055130167Sgad else 2056130167Sgad SLIST_INSERT_AFTER(zprev, ztmp, zw_nextp); 2057130167Sgad return (ztmp); 2058130167Sgad} 2059130167Sgad 2060130167Sgad/* Send a signal to the pid specified by pidfile */ 2061130167Sgadstatic void 2062130167Sgadset_swpid(struct sigwork_entry *swork, const struct conf_entry *ent) 2063130167Sgad{ 2064130167Sgad FILE *f; 2065130167Sgad long minok, maxok, rval; 2066130167Sgad char *endp, *linep, line[BUFSIZ]; 2067130167Sgad 2068130167Sgad minok = MIN_PID; 2069130167Sgad maxok = MAX_PID; 2070130167Sgad swork->sw_pidok = 0; 2071130167Sgad swork->sw_pid = 0; 2072130167Sgad swork->sw_pidtype = "daemon"; 2073130167Sgad if (ent->flags & CE_SIGNALGROUP) { 2074130167Sgad /* 2075130167Sgad * If we are expected to signal a process-group when 2076130167Sgad * rotating this logfile, then the value read in should 2077130167Sgad * be the negative of a valid process ID. 2078130167Sgad */ 2079130167Sgad minok = -MAX_PID; 2080130167Sgad maxok = -MIN_PID; 2081130167Sgad swork->sw_pidtype = "process-group"; 2082130167Sgad } 2083130167Sgad 2084130167Sgad f = fopen(ent->pid_file, "r"); 2085130167Sgad if (f == NULL) { 2086202668Sdelphij if (errno == ENOENT && enforcepid == 0) { 2087200806Sdelphij /* 2088200806Sdelphij * Warn if the PID file doesn't exist, but do 2089200806Sdelphij * not consider it an error. Most likely it 2090200806Sdelphij * means the process has been terminated, 2091200806Sdelphij * so it should be safe to rotate any log 2092200806Sdelphij * files that the process would have been using. 2093200806Sdelphij */ 2094200806Sdelphij swork->sw_pidok = 1; 2095200806Sdelphij warnx("pid file doesn't exist: %s", ent->pid_file); 2096200806Sdelphij } else 2097200806Sdelphij warn("can't open pid file: %s", ent->pid_file); 2098130167Sgad return; 2099130167Sgad } 2100130167Sgad 2101130167Sgad if (fgets(line, BUFSIZ, f) == NULL) { 2102130167Sgad /* 2103130167Sgad * Warn if the PID file is empty, but do not consider 2104130167Sgad * it an error. Most likely it means the process has 2105130167Sgad * has terminated, so it should be safe to rotate any 2106130167Sgad * log files that the process would have been using. 2107130167Sgad */ 2108202668Sdelphij if (feof(f) && enforcepid == 0) { 2109130167Sgad swork->sw_pidok = 1; 2110130167Sgad warnx("pid file is empty: %s", ent->pid_file); 2111130167Sgad } else 2112130167Sgad warn("can't read from pid file: %s", ent->pid_file); 2113130167Sgad (void)fclose(f); 2114130167Sgad return; 2115130167Sgad } 2116130167Sgad (void)fclose(f); 2117130167Sgad 2118130167Sgad errno = 0; 2119130167Sgad linep = line; 2120130167Sgad while (*linep == ' ') 2121130167Sgad linep++; 2122130167Sgad rval = strtol(linep, &endp, 10); 2123130167Sgad if (*endp != '\0' && !isspacech(*endp)) { 2124130167Sgad warnx("pid file does not start with a valid number: %s", 2125130167Sgad ent->pid_file); 2126130167Sgad } else if (rval < minok || rval > maxok) { 2127130167Sgad warnx("bad value '%ld' for process number in %s", 2128130167Sgad rval, ent->pid_file); 2129130167Sgad if (verbose) 2130130167Sgad warnx("\t(expecting value between %ld and %ld)", 2131130167Sgad minok, maxok); 2132130167Sgad } else { 2133130167Sgad swork->sw_pidok = 1; 2134130167Sgad swork->sw_pid = rval; 2135130167Sgad } 2136130167Sgad 2137130167Sgad return; 2138130167Sgad} 2139130167Sgad 214013244Sgraichen/* Log the fact that the logs were turned over */ 214159004Shmstatic int 2142119926Sgadlog_trim(const char *logname, const struct conf_entry *log_ent) 214313244Sgraichen{ 214459003Shm FILE *f; 2145111388Sgad const char *xtra; 214659003Shm 2147119926Sgad if ((f = fopen(logname, "a")) == NULL) 214859003Shm return (-1); 2149111388Sgad xtra = ""; 2150111768Sgad if (log_ent->def_cfg) 2151111388Sgad xtra = " using <default> rule"; 2152114137Sgad if (log_ent->firstcreate) 2153114137Sgad fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n", 2154114137Sgad daytime, hostname, (int) getpid(), xtra); 2155114137Sgad else if (log_ent->r_reason != NULL) 2156111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n", 2157111772Sgad daytime, hostname, (int) getpid(), log_ent->r_reason, xtra); 2158111772Sgad else 2159111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n", 2160111772Sgad daytime, hostname, (int) getpid(), xtra); 216159003Shm if (fclose(f) == EOF) 2162127858Scharnier err(1, "log_trim: fclose"); 216359003Shm return (0); 216413244Sgraichen} 216513244Sgraichen 216613244Sgraichen/* Return size in kilobytes of a file */ 216759004Shmstatic int 2168130165Sgadsizefile(const char *file) 216913244Sgraichen{ 217059003Shm struct stat sb; 217113244Sgraichen 217259003Shm if (stat(file, &sb) < 0) 217359003Shm return (-1); 217459003Shm return (kbytes(dbtob(sb.st_blocks))); 217513244Sgraichen} 217613244Sgraichen 217713244Sgraichen/* Return the age of old log file (file.0) */ 217859004Shmstatic int 217959004Shmage_old_log(char *file) 218013244Sgraichen{ 218159003Shm struct stat sb; 2182218127Smm const char *logfile_suffix; 2183218127Smm char tmp[MAXPATHLEN + sizeof(".0") + COMPRESS_SUFFIX_MAXLEN + 1]; 218413244Sgraichen 218559004Shm if (archtodir) { 218659004Shm char *p; 218759004Shm 218859004Shm /* build name of archive directory into tmp */ 218959004Shm if (*archdirname == '/') { /* absolute */ 219071299Sjedgar strlcpy(tmp, archdirname, sizeof(tmp)); 219159004Shm } else { /* relative */ 219259004Shm /* get directory part of logfile */ 219371299Sjedgar strlcpy(tmp, file, sizeof(tmp)); 219459004Shm if ((p = rindex(tmp, '/')) == NULL) 219559004Shm tmp[0] = '\0'; 219659004Shm else 219759004Shm *(p + 1) = '\0'; 219871299Sjedgar strlcat(tmp, archdirname, sizeof(tmp)); 219959004Shm } 220059004Shm 220171299Sjedgar strlcat(tmp, "/", sizeof(tmp)); 220259004Shm 220359004Shm /* get filename part of logfile */ 220459004Shm if ((p = rindex(file, '/')) == NULL) 220571299Sjedgar strlcat(tmp, file, sizeof(tmp)); 220659004Shm else 220771299Sjedgar strlcat(tmp, p + 1, sizeof(tmp)); 220859004Shm } else { 220971299Sjedgar (void) strlcpy(tmp, file, sizeof(tmp)); 221059004Shm } 221159004Shm 2212114764Sgad strlcat(tmp, ".0", sizeof(tmp)); 2213218127Smm logfile_suffix = get_logfile_suffix(tmp); 2214218127Smm if (logfile_suffix == NULL) 2215218127Smm return (-1); 2216218127Smm (void) strlcat(tmp, logfile_suffix, sizeof(tmp)); 2217218127Smm if (stat(tmp, &sb) < 0) 2218218127Smm return (-1); 2219120361Sgad return ((int)(ptimeget_secs(timenow) - sb.st_mtime + 1800) / 3600); 222013244Sgraichen} 222113244Sgraichen 222213244Sgraichen/* Skip Over Blanks */ 2223111820Sgadstatic char * 222459004Shmsob(char *p) 222513244Sgraichen{ 222659003Shm while (p && *p && isspace(*p)) 222759003Shm p++; 222859003Shm return (p); 222913244Sgraichen} 223013244Sgraichen 223113244Sgraichen/* Skip Over Non-Blanks */ 2232111820Sgadstatic char * 223359004Shmson(char *p) 223413244Sgraichen{ 223559003Shm while (p && *p && !isspace(*p)) 223659003Shm p++; 223759003Shm return (p); 223813244Sgraichen} 223943071Swollman 2240119102Sgad/* Check if string is actually a number */ 2241119102Sgadstatic int 2242119102Sgadisnumberstr(const char *string) 2243119102Sgad{ 2244119102Sgad while (*string) { 2245119102Sgad if (!isdigitch(*string++)) 2246119102Sgad return (0); 2247119102Sgad } 2248119102Sgad return (1); 2249119102Sgad} 2250119102Sgad 2251208649Sgordon/* Check if string contains a glob */ 2252208649Sgordonstatic int 2253208649Sgordonisglobstr(const char *string) 2254208649Sgordon{ 2255208649Sgordon char chr; 2256208649Sgordon 2257208649Sgordon while ((chr = *string++)) { 2258208649Sgordon if (chr == '*' || chr == '?' || chr == '[') 2259208649Sgordon return (1); 2260208649Sgordon } 2261208649Sgordon return (0); 2262208649Sgordon} 2263208649Sgordon 2264130038Sgad/* 2265130038Sgad * Save the active log file under a new name. A link to the new name 2266130038Sgad * is the quick-and-easy way to do this. If that fails (which it will 2267130038Sgad * if the destination is on another partition), then make a copy of 2268130038Sgad * the file to the new location. 2269130038Sgad */ 227059004Shmstatic void 2271130038Sgadsavelog(char *from, char *to) 227259004Shm{ 227359004Shm FILE *src, *dst; 2274130038Sgad int c, res; 227559004Shm 2276130038Sgad res = link(from, to); 2277130038Sgad if (res == 0) 2278130038Sgad return; 2279130038Sgad 228059004Shm if ((src = fopen(from, "r")) == NULL) 228159004Shm err(1, "can't fopen %s for reading", from); 228259004Shm if ((dst = fopen(to, "w")) == NULL) 228359004Shm err(1, "can't fopen %s for writing", to); 228459004Shm 228559004Shm while ((c = getc(src)) != EOF) { 228659004Shm if ((putc(c, dst)) == EOF) 228759004Shm err(1, "error writing to %s", to); 228859004Shm } 228959004Shm 229059004Shm if (ferror(src)) 229159004Shm err(1, "error reading from %s", from); 229259004Shm if ((fclose(src)) != 0) 229359004Shm err(1, "can't fclose %s", to); 229459004Shm if ((fclose(dst)) != 0) 229559004Shm err(1, "can't fclose %s", from); 229659004Shm} 229759004Shm 229859004Shm/* create one or more directory components of a path */ 229959004Shmstatic void 2300114137Sgadcreatedir(const struct conf_entry *ent, char *dirpart) 230159004Shm{ 2302111398Sgad int res; 230359004Shm char *s, *d; 230471299Sjedgar char mkdirpath[MAXPATHLEN]; 230559004Shm struct stat st; 230659004Shm 230759004Shm s = dirpart; 230859004Shm d = mkdirpath; 230959004Shm 231059004Shm for (;;) { 231159004Shm *d++ = *s++; 2312111398Sgad if (*s != '/' && *s != '\0') 2313111398Sgad continue; 2314111398Sgad *d = '\0'; 2315111398Sgad res = lstat(mkdirpath, &st); 2316111398Sgad if (res != 0) { 2317111398Sgad if (noaction) { 2318111967Sgad printf("\tmkdir %s\n", mkdirpath); 2319111398Sgad } else { 2320111398Sgad res = mkdir(mkdirpath, 0755); 2321111398Sgad if (res != 0) 2322111398Sgad err(1, "Error on mkdir(\"%s\") for -a", 2323111398Sgad mkdirpath); 2324111398Sgad } 232559004Shm } 232659004Shm if (*s == '\0') 232759004Shm break; 232859004Shm } 2329114137Sgad if (verbose) { 2330114137Sgad if (ent->firstcreate) 2331114137Sgad printf("Created directory '%s' for new %s\n", 2332114137Sgad dirpart, ent->log); 2333114137Sgad else 2334114137Sgad printf("Created directory '%s' for -a\n", dirpart); 2335114137Sgad } 233659004Shm} 233759004Shm 2338114137Sgad/* 2339114137Sgad * Create a new log file, destroying any currently-existing version 2340114137Sgad * of the log file in the process. If the caller wants a backup copy 2341114137Sgad * of the file to exist, they should call 'link(logfile,logbackup)' 2342114137Sgad * before calling this routine. 2343114137Sgad */ 2344114137Sgadvoid 2345114137Sgadcreatelog(const struct conf_entry *ent) 2346114137Sgad{ 2347114137Sgad int fd, failed; 2348114137Sgad struct stat st; 2349114137Sgad char *realfile, *slash, tempfile[MAXPATHLEN]; 2350114137Sgad 2351114137Sgad fd = -1; 2352114137Sgad realfile = ent->log; 2353114137Sgad 2354114137Sgad /* 2355114137Sgad * If this log file is being created for the first time (-C option), 2356114137Sgad * then it may also be true that the parent directory does not exist 2357114137Sgad * yet. Check, and create that directory if it is missing. 2358114137Sgad */ 2359114137Sgad if (ent->firstcreate) { 2360114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 2361114137Sgad slash = strrchr(tempfile, '/'); 2362114137Sgad if (slash != NULL) { 2363114137Sgad *slash = '\0'; 2364131581Ssobomax failed = stat(tempfile, &st); 2365114137Sgad if (failed && errno != ENOENT) 2366131581Ssobomax err(1, "Error on stat(%s)", tempfile); 2367114137Sgad if (failed) 2368114137Sgad createdir(ent, tempfile); 2369114137Sgad else if (!S_ISDIR(st.st_mode)) 2370114137Sgad errx(1, "%s exists but is not a directory", 2371114137Sgad tempfile); 2372114137Sgad } 2373114137Sgad } 2374114137Sgad 2375114137Sgad /* 2376114137Sgad * First create an unused filename, so it can be chown'ed and 2377114137Sgad * chmod'ed before it is moved into the real location. mkstemp 2378114137Sgad * will create the file mode=600 & owned by us. Note that all 2379114137Sgad * temp files will have a suffix of '.z<something>'. 2380114137Sgad */ 2381114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 2382114137Sgad strlcat(tempfile, ".zXXXXXX", sizeof(tempfile)); 2383114137Sgad if (noaction) 2384114137Sgad printf("\tmktemp %s\n", tempfile); 2385114137Sgad else { 2386114137Sgad fd = mkstemp(tempfile); 2387114137Sgad if (fd < 0) 2388114137Sgad err(1, "can't mkstemp logfile %s", tempfile); 2389114137Sgad 2390114137Sgad /* 2391114137Sgad * Add status message to what will become the new log file. 2392114137Sgad */ 2393114137Sgad if (!(ent->flags & CE_BINARY)) { 2394114137Sgad if (log_trim(tempfile, ent)) 2395114137Sgad err(1, "can't add status message to log"); 2396114137Sgad } 2397114137Sgad } 2398114137Sgad 2399114137Sgad /* Change the owner/group, if we are supposed to */ 2400114137Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 2401114137Sgad if (noaction) 2402114137Sgad printf("\tchown %u:%u %s\n", ent->uid, ent->gid, 2403114137Sgad tempfile); 2404114137Sgad else { 2405114137Sgad failed = fchown(fd, ent->uid, ent->gid); 2406114137Sgad if (failed) 2407114137Sgad err(1, "can't fchown temp file %s", tempfile); 2408114137Sgad } 2409114137Sgad } 2410114137Sgad 2411130043Sgad /* Turn on NODUMP if it was requested in the config-file. */ 2412130043Sgad if (ent->flags & CE_NODUMP) { 2413130043Sgad if (noaction) 2414130043Sgad printf("\tchflags nodump %s\n", tempfile); 2415130043Sgad else { 2416130043Sgad failed = fchflags(fd, UF_NODUMP); 2417130043Sgad if (failed) { 2418130043Sgad warn("log_trim: fchflags(NODUMP)"); 2419130043Sgad } 2420130043Sgad } 2421130043Sgad } 2422130043Sgad 2423114137Sgad /* 2424114137Sgad * Note that if the real logfile still exists, and if the call 2425114137Sgad * to rename() fails, then "neither the old file nor the new 2426114137Sgad * file shall be changed or created" (to quote the standard). 2427114137Sgad * If the call succeeds, then the file will be replaced without 2428114137Sgad * any window where some other process might find that the file 2429114137Sgad * did not exist. 2430114137Sgad * XXX - ? It may be that for some error conditions, we could 2431114137Sgad * retry by first removing the realfile and then renaming. 2432114137Sgad */ 2433114137Sgad if (noaction) { 2434114137Sgad printf("\tchmod %o %s\n", ent->permissions, tempfile); 2435114137Sgad printf("\tmv %s %s\n", tempfile, realfile); 2436114137Sgad } else { 2437114137Sgad failed = fchmod(fd, ent->permissions); 2438114137Sgad if (failed) 2439114137Sgad err(1, "can't fchmod temp file '%s'", tempfile); 2440114137Sgad failed = rename(tempfile, realfile); 2441114137Sgad if (failed) 2442114137Sgad err(1, "can't mv %s to %s", tempfile, realfile); 2443114137Sgad } 2444114137Sgad 2445114137Sgad if (fd >= 0) 2446114137Sgad close(fd); 2447114137Sgad} 2448130165Sgad 2449154566Sgad/* 2450154566Sgad * Change the attributes of a given filename to what was specified in 2451154566Sgad * the newsyslog.conf entry. This routine is only called for files 2452154566Sgad * that newsyslog expects that it has created, and thus it is a fatal 2453154566Sgad * error if this routine finds that the file does not exist. 2454154566Sgad */ 2455130165Sgadstatic void 2456130165Sgadchange_attrs(const char *fname, const struct conf_entry *ent) 2457130165Sgad{ 2458130165Sgad int failed; 2459130165Sgad 2460130165Sgad if (noaction) { 2461130165Sgad printf("\tchmod %o %s\n", ent->permissions, fname); 2462130165Sgad 2463130165Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 2464130165Sgad printf("\tchown %u:%u %s\n", 2465130165Sgad ent->uid, ent->gid, fname); 2466130165Sgad 2467130165Sgad if (ent->flags & CE_NODUMP) 2468130165Sgad printf("\tchflags nodump %s\n", fname); 2469130165Sgad return; 2470130165Sgad } 2471130165Sgad 2472130165Sgad failed = chmod(fname, ent->permissions); 2473154566Sgad if (failed) { 2474154566Sgad if (errno != EPERM) 2475154566Sgad err(1, "chmod(%s) in change_attrs", fname); 2476154566Sgad warn("change_attrs couldn't chmod(%s)", fname); 2477154566Sgad } 2478130165Sgad 2479130165Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 2480130165Sgad failed = chown(fname, ent->uid, ent->gid); 2481130165Sgad if (failed) 2482130165Sgad warn("can't chown %s", fname); 2483130165Sgad } 2484130165Sgad 2485130165Sgad if (ent->flags & CE_NODUMP) { 2486130165Sgad failed = chflags(fname, UF_NODUMP); 2487130165Sgad if (failed) 2488130165Sgad warn("can't chflags %s NODUMP", fname); 2489130165Sgad } 2490130165Sgad} 2491