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$"); 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. */ 128221873Ssobomax#define CE_PID2CMD 0x0400 /* Replace PID file with a shell command.*/ 12943071Swollman 130130045Sgad#define MIN_PID 5 /* Don't touch pids lower than this */ 131130045Sgad#define MAX_PID 99999 /* was lower, see /usr/include/sys/proc.h */ 132111781Sgad 133130045Sgad#define kbytes(size) (((size) + 1023) >> 10) 134111781Sgad 135130165Sgad#define DEFAULT_MARKER "<default>" 136130165Sgad#define DEBUG_MARKER "<debug>" 137208649Sgordon#define INCLUDE_MARKER "<include>" 138210372Ssimon#define DEFAULT_TIMEFNAME_FMT "%Y%m%dT%H%M%S" 139130165Sgad 140210372Ssimon#define MAX_OLDLOGS 65536 /* Default maximum number of old logfiles */ 141210372Ssimon 142218127Smmstruct compress_types { 143218127Smm const char *flag; /* Flag in configuration file */ 144218127Smm const char *suffix; /* Compression suffix */ 145218127Smm const char *path; /* Path to compression program */ 146218127Smm}; 147218127Smm 148218127Smmconst struct compress_types compress_type[COMPRESS_TYPES] = { 149218127Smm { "", "", "" }, /* no compression */ 150218127Smm { "Z", COMPRESS_SUFFIX_GZ, _PATH_GZIP }, /* gzip compression */ 151218127Smm { "J", COMPRESS_SUFFIX_BZ2, _PATH_BZIP2 }, /* bzip2 compression */ 152218127Smm { "X", COMPRESS_SUFFIX_XZ, _PATH_XZ } /* xz compression */ 153218127Smm}; 154218127Smm 15513244Sgraichenstruct conf_entry { 156208648Sgordon STAILQ_ENTRY(conf_entry) cf_nextp; 15759003Shm char *log; /* Name of the log */ 158221873Ssobomax char *pid_cmd_file; /* PID or command file */ 159111772Sgad char *r_reason; /* The reason this file is being rotated */ 160114137Sgad int firstcreate; /* Creating log for the first time (-C). */ 161111772Sgad int rotate; /* Non-zero if this file should be rotated */ 162130165Sgad int fsize; /* size found for the log file */ 163111779Sgad uid_t uid; /* Owner of log */ 164111779Sgad gid_t gid; /* Group of log */ 16559003Shm int numlogs; /* Number of logs to keep */ 166130165Sgad int trsize; /* Size cutoff to trigger trimming the log */ 16759003Shm int hours; /* Hours between log trimming */ 168120361Sgad struct ptime_data *trim_at; /* Specific time to do trimming */ 169139655Sdelphij unsigned int permissions; /* File permissions on the log */ 170218127Smm int flags; /* CE_BINARY */ 171218127Smm int compress; /* Compression */ 17259003Shm int sig; /* Signal to send */ 173111388Sgad int def_cfg; /* Using the <default> rule for this file */ 17413244Sgraichen}; 17513244Sgraichen 176130167Sgadstruct sigwork_entry { 177130167Sgad SLIST_ENTRY(sigwork_entry) sw_nextp; 178130167Sgad int sw_signum; /* the signal to send */ 179130167Sgad int sw_pidok; /* true if pid value is valid */ 180130167Sgad pid_t sw_pid; /* the process id from the PID file */ 181130167Sgad const char *sw_pidtype; /* "daemon" or "process group" */ 182246918Smarkj int sw_runcmd; /* run command or send PID to signal */ 183221873Ssobomax char sw_fname[1]; /* file the PID was read from or shell cmd */ 184130167Sgad}; 185130167Sgad 186130167Sgadstruct zipwork_entry { 187130167Sgad SLIST_ENTRY(zipwork_entry) zw_nextp; 188130167Sgad const struct conf_entry *zw_conf; /* for chown/perm/flag info */ 189130167Sgad const struct sigwork_entry *zw_swork; /* to know success of signal */ 190130167Sgad int zw_fsize; /* size of the file to compress */ 191130167Sgad char zw_fname[1]; /* the file to compress */ 192130167Sgad}; 193130167Sgad 194208649Sgordonstruct include_entry { 195208649Sgordon STAILQ_ENTRY(include_entry) inc_nextp; 196208649Sgordon const char *file; /* Name of file to process */ 197208649Sgordon}; 198208649Sgordon 199210372Ssimonstruct oldlog_entry { 200210372Ssimon char *fname; /* Filename of the log file */ 201215625Ssimon time_t t; /* Parsed timestamp of the logfile */ 202210372Ssimon}; 203210372Ssimon 204130165Sgadtypedef enum { 205130165Sgad FREE_ENT, KEEP_ENT 206130165Sgad} fk_entry; 207111388Sgad 208208648SgordonSTAILQ_HEAD(cflist, conf_entry); 209130167SgadSLIST_HEAD(swlisthead, sigwork_entry) swhead = SLIST_HEAD_INITIALIZER(swhead); 210130167SgadSLIST_HEAD(zwlisthead, zipwork_entry) zwhead = SLIST_HEAD_INITIALIZER(zwhead); 211208649SgordonSTAILQ_HEAD(ilist, include_entry); 212130167Sgad 213120361Sgadint dbg_at_times; /* -D Show details of 'trim_at' code */ 214120361Sgad 21559004Shmint archtodir = 0; /* Archive old logfiles to other directory */ 216114137Sgadint createlogs; /* Create (non-GLOB) logfiles which do not */ 217114137Sgad /* already exist. 1=='for entries with */ 218114137Sgad /* C flag', 2=='for all entries'. */ 21959003Shmint verbose = 0; /* Print out what's going on */ 22059003Shmint needroot = 1; /* Root privs are necessary */ 22159003Shmint noaction = 0; /* Don't do anything, just show it */ 222143106Sbrooksint norotate = 0; /* Don't rotate */ 223111768Sgadint nosignal; /* Do not send any signals */ 224202668Sdelphijint enforcepid = 0; /* If PID file does not exist or empty, do nothing */ 22559003Shmint force = 0; /* Force the trim no matter what */ 226111772Sgadint rotatereq = 0; /* -R = Always rotate the file(s) as given */ 227111772Sgad /* on the command (this also requires */ 228111772Sgad /* that a list of files *are* given on */ 229111772Sgad /* the run command). */ 230111772Sgadchar *requestor; /* The name given on a -R request */ 231210372Ssimonchar *timefnamefmt = NULL; /* Use time based filenames instead of .0 etc */ 23259004Shmchar *archdirname; /* Directory path to old logfiles archive */ 233136127Sbrookschar *destdir = NULL; /* Directory to treat at root for logs */ 234111773Sgadconst char *conf; /* Configuration file to use */ 23559003Shm 236120361Sgadstruct ptime_data *dbg_timenow; /* A "timenow" value set via -D option */ 237120361Sgadstruct ptime_data *timenow; /* The time to use for checking at-fields */ 238120361Sgad 239159998Sgad#define DAYTIME_LEN 16 240159998Sgadchar daytime[DAYTIME_LEN]; /* The current time in human readable form, 241159998Sgad * used for rotation-tracking messages. */ 24271299Sjedgarchar hostname[MAXHOSTNAMELEN]; /* hostname */ 24313244Sgraichen 244210407Sbrianconst char *path_syslogpid = _PATH_SYSLOGPID; 245210407Sbrian 246208648Sgordonstatic struct cflist *get_worklist(char **files); 247208648Sgordonstatic void parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p, 248208649Sgordon struct conf_entry *defconf_p, struct ilist *inclist); 249208649Sgordonstatic void add_to_queue(const char *fname, struct ilist *inclist); 25016240Salexstatic char *sob(char *p); 25116240Salexstatic char *son(char *p); 252119102Sgadstatic int isnumberstr(const char *); 253208649Sgordonstatic int isglobstr(const char *); 25459003Shmstatic char *missing_field(char *p, char *errline); 255130165Sgadstatic void change_attrs(const char *, const struct conf_entry *); 256218127Smmstatic const char *get_logfile_suffix(const char *logfile); 257130165Sgadstatic fk_entry do_entry(struct conf_entry *); 258130165Sgadstatic fk_entry do_rotate(const struct conf_entry *); 259130167Sgadstatic void do_sigwork(struct sigwork_entry *); 260130167Sgadstatic void do_zipwork(struct zipwork_entry *); 261130167Sgadstatic struct sigwork_entry * 262130167Sgad save_sigwork(const struct conf_entry *); 263130167Sgadstatic struct zipwork_entry * 264130167Sgad save_zipwork(const struct conf_entry *, const struct 265130167Sgad sigwork_entry *, int, const char *); 266130167Sgadstatic void set_swpid(struct sigwork_entry *, const struct conf_entry *); 267130165Sgadstatic int sizefile(const char *); 268208648Sgordonstatic void expand_globs(struct cflist *work_p, struct cflist *glob_p); 269208648Sgordonstatic void free_clist(struct cflist *list); 270111388Sgadstatic void free_entry(struct conf_entry *ent); 271111388Sgadstatic struct conf_entry *init_entry(const char *fname, 272111388Sgad struct conf_entry *src_entry); 273111781Sgadstatic void parse_args(int argc, char **argv); 274119904Sgadstatic int parse_doption(const char *doption); 27580640Sobrienstatic void usage(void); 276119926Sgadstatic int log_trim(const char *logname, const struct conf_entry *log_ent); 277250478Smarkjstatic int age_old_log(const char *file); 278130038Sgadstatic void savelog(char *from, char *to); 279114137Sgadstatic void createdir(const struct conf_entry *ent, char *dirpart); 280114137Sgadstatic void createlog(const struct conf_entry *ent); 28113244Sgraichen 282111768Sgad/* 283129975Sgad * All the following take a parameter of 'int', but expect values in the 284129975Sgad * range of unsigned char. Define wrappers which take values of type 'char', 285129975Sgad * whether signed or unsigned, and ensure they end up in the right range. 286111768Sgad */ 287129975Sgad#define isdigitch(Anychar) isdigit((u_char)(Anychar)) 288129975Sgad#define isprintch(Anychar) isprint((u_char)(Anychar)) 289129975Sgad#define isspacech(Anychar) isspace((u_char)(Anychar)) 290129975Sgad#define tolowerch(Anychar) tolower((u_char)(Anychar)) 291111768Sgad 29259004Shmint 29359004Shmmain(int argc, char **argv) 29413244Sgraichen{ 295208648Sgordon struct cflist *worklist; 296208648Sgordon struct conf_entry *p; 297130167Sgad struct sigwork_entry *stmp; 298130167Sgad struct zipwork_entry *ztmp; 29925443Sache 300130167Sgad SLIST_INIT(&swhead); 301130167Sgad SLIST_INIT(&zwhead); 302130167Sgad 303111781Sgad parse_args(argc, argv); 304111773Sgad argc -= optind; 305111773Sgad argv += optind; 306111773Sgad 30759003Shm if (needroot && getuid() && geteuid()) 30859003Shm errx(1, "must have root privs"); 309208648Sgordon worklist = get_worklist(argv); 31059003Shm 311130165Sgad /* 312130165Sgad * Rotate all the files which need to be rotated. Note that 313130165Sgad * some users have *hundreds* of entries in newsyslog.conf! 314130165Sgad */ 315208648Sgordon while (!STAILQ_EMPTY(worklist)) { 316208648Sgordon p = STAILQ_FIRST(worklist); 317208648Sgordon STAILQ_REMOVE_HEAD(worklist, cf_nextp); 318208648Sgordon if (do_entry(p) == FREE_ENT) 319208648Sgordon free_entry(p); 32059003Shm } 321130165Sgad 322130167Sgad /* 323130167Sgad * Send signals to any processes which need a signal to tell 324130167Sgad * them to close and re-open the log file(s) we have rotated. 325130167Sgad * Note that zipwork_entries include pointers to these 326130167Sgad * sigwork_entry's, so we can not free the entries here. 327130167Sgad */ 328130167Sgad if (!SLIST_EMPTY(&swhead)) { 329130204Sgad if (noaction || verbose) 330130167Sgad printf("Signal all daemon process(es)...\n"); 331130167Sgad SLIST_FOREACH(stmp, &swhead, sw_nextp) 332130167Sgad do_sigwork(stmp); 333130204Sgad if (noaction) 334130204Sgad printf("\tsleep 10\n"); 335130204Sgad else { 336130204Sgad if (verbose) 337130204Sgad printf("Pause 10 seconds to allow daemon(s)" 338130204Sgad " to close log file(s)\n"); 339130204Sgad sleep(10); 340130204Sgad } 341130167Sgad } 342130167Sgad /* 343130167Sgad * Compress all files that we're expected to compress, now 344130167Sgad * that all processes should have closed the files which 345130167Sgad * have been rotated. 346130167Sgad */ 347130167Sgad if (!SLIST_EMPTY(&zwhead)) { 348130204Sgad if (noaction || verbose) 349130167Sgad printf("Compress all rotated log file(s)...\n"); 350130167Sgad while (!SLIST_EMPTY(&zwhead)) { 351130167Sgad ztmp = SLIST_FIRST(&zwhead); 352130167Sgad do_zipwork(ztmp); 353130167Sgad SLIST_REMOVE_HEAD(&zwhead, zw_nextp); 354130167Sgad free(ztmp); 355130167Sgad } 356130167Sgad } 357130167Sgad /* Now free all the sigwork entries. */ 358130167Sgad while (!SLIST_EMPTY(&swhead)) { 359130167Sgad stmp = SLIST_FIRST(&swhead); 360130167Sgad SLIST_REMOVE_HEAD(&swhead, sw_nextp); 361130167Sgad free(stmp); 362130167Sgad } 363130167Sgad 36495999Smaxim while (wait(NULL) > 0 || errno == EINTR) 36595999Smaxim ; 36659003Shm return (0); 36713244Sgraichen} 36813244Sgraichen 369111388Sgadstatic struct conf_entry * 370111388Sgadinit_entry(const char *fname, struct conf_entry *src_entry) 371111388Sgad{ 372111388Sgad struct conf_entry *tempwork; 373111388Sgad 374111388Sgad if (verbose > 4) 375111388Sgad printf("\t--> [creating entry for %s]\n", fname); 376111388Sgad 377111388Sgad tempwork = malloc(sizeof(struct conf_entry)); 378111388Sgad if (tempwork == NULL) 379111388Sgad err(1, "malloc of conf_entry for %s", fname); 380111388Sgad 381136174Sbrooks if (destdir == NULL || fname[0] != '/') 382136127Sbrooks tempwork->log = strdup(fname); 383136127Sbrooks else 384136127Sbrooks asprintf(&tempwork->log, "%s%s", destdir, fname); 385111388Sgad if (tempwork->log == NULL) 386111388Sgad err(1, "strdup for %s", fname); 387111388Sgad 388111388Sgad if (src_entry != NULL) { 389221873Ssobomax tempwork->pid_cmd_file = NULL; 390221873Ssobomax if (src_entry->pid_cmd_file) 391221873Ssobomax tempwork->pid_cmd_file = strdup(src_entry->pid_cmd_file); 392111772Sgad tempwork->r_reason = NULL; 393114137Sgad tempwork->firstcreate = 0; 394111772Sgad tempwork->rotate = 0; 395130165Sgad tempwork->fsize = -1; 396111388Sgad tempwork->uid = src_entry->uid; 397111388Sgad tempwork->gid = src_entry->gid; 398111388Sgad tempwork->numlogs = src_entry->numlogs; 399130165Sgad tempwork->trsize = src_entry->trsize; 400111388Sgad tempwork->hours = src_entry->hours; 401120361Sgad tempwork->trim_at = NULL; 402120361Sgad if (src_entry->trim_at != NULL) 403120361Sgad tempwork->trim_at = ptime_init(src_entry->trim_at); 404111388Sgad tempwork->permissions = src_entry->permissions; 405111388Sgad tempwork->flags = src_entry->flags; 406218127Smm tempwork->compress = src_entry->compress; 407111388Sgad tempwork->sig = src_entry->sig; 408111388Sgad tempwork->def_cfg = src_entry->def_cfg; 409111388Sgad } else { 410111388Sgad /* Initialize as a "do-nothing" entry */ 411221873Ssobomax tempwork->pid_cmd_file = NULL; 412111772Sgad tempwork->r_reason = NULL; 413114137Sgad tempwork->firstcreate = 0; 414111772Sgad tempwork->rotate = 0; 415130165Sgad tempwork->fsize = -1; 416111779Sgad tempwork->uid = (uid_t)-1; 417111779Sgad tempwork->gid = (gid_t)-1; 418111388Sgad tempwork->numlogs = 1; 419130165Sgad tempwork->trsize = -1; 420111388Sgad tempwork->hours = -1; 421120361Sgad tempwork->trim_at = NULL; 422111388Sgad tempwork->permissions = 0; 423111388Sgad tempwork->flags = 0; 424218127Smm tempwork->compress = COMPRESS_NONE; 425111388Sgad tempwork->sig = SIGHUP; 426111388Sgad tempwork->def_cfg = 0; 427111388Sgad } 428111388Sgad 429111388Sgad return (tempwork); 430111388Sgad} 431111388Sgad 43259004Shmstatic void 433111388Sgadfree_entry(struct conf_entry *ent) 434111388Sgad{ 435111388Sgad 436111388Sgad if (ent == NULL) 437111388Sgad return; 438111388Sgad 439111388Sgad if (ent->log != NULL) { 440111388Sgad if (verbose > 4) 441111388Sgad printf("\t--> [freeing entry for %s]\n", ent->log); 442111388Sgad free(ent->log); 443111388Sgad ent->log = NULL; 444111388Sgad } 445111388Sgad 446221873Ssobomax if (ent->pid_cmd_file != NULL) { 447221873Ssobomax free(ent->pid_cmd_file); 448221873Ssobomax ent->pid_cmd_file = NULL; 449111388Sgad } 450111388Sgad 451111772Sgad if (ent->r_reason != NULL) { 452111772Sgad free(ent->r_reason); 453111772Sgad ent->r_reason = NULL; 454111772Sgad } 455111772Sgad 456120361Sgad if (ent->trim_at != NULL) { 457120361Sgad ptime_free(ent->trim_at); 458120361Sgad ent->trim_at = NULL; 459120361Sgad } 460120361Sgad 461111388Sgad free(ent); 462111388Sgad} 463111388Sgad 464111388Sgadstatic void 465208648Sgordonfree_clist(struct cflist *list) 466112020Sgad{ 467208648Sgordon struct conf_entry *ent; 468112020Sgad 469208648Sgordon while (!STAILQ_EMPTY(list)) { 470208648Sgordon ent = STAILQ_FIRST(list); 471208648Sgordon STAILQ_REMOVE_HEAD(list, cf_nextp); 472112020Sgad free_entry(ent); 473112020Sgad } 474208648Sgordon 475208648Sgordon free(list); 476208648Sgordon list = NULL; 477112020Sgad} 478112020Sgad 479130165Sgadstatic fk_entry 48059004Shmdo_entry(struct conf_entry * ent) 48113244Sgraichen{ 482130045Sgad#define REASON_MAX 80 483130165Sgad int modtime; 484130165Sgad fk_entry free_or_keep; 485120361Sgad double diffsecs; 486111772Sgad char temp_reason[REASON_MAX]; 487234674Sglebius int oversized; 48859003Shm 489130165Sgad free_or_keep = FREE_ENT; 490218127Smm if (verbose) 491218127Smm printf("%s <%d%s>: ", ent->log, ent->numlogs, 492218127Smm compress_type[ent->compress].flag); 493130165Sgad ent->fsize = sizefile(ent->log); 494234674Sglebius oversized = ((ent->trsize > 0) && (ent->fsize >= ent->trsize)); 49559003Shm modtime = age_old_log(ent->log); 496111772Sgad ent->rotate = 0; 497114137Sgad ent->firstcreate = 0; 498130165Sgad if (ent->fsize < 0) { 499114137Sgad /* 500114137Sgad * If either the C flag or the -C option was specified, 501114137Sgad * and if we won't be creating the file, then have the 502114137Sgad * verbose message include a hint as to why the file 503114137Sgad * will not be created. 504114137Sgad */ 505114137Sgad temp_reason[0] = '\0'; 506114137Sgad if (createlogs > 1) 507114137Sgad ent->firstcreate = 1; 508114137Sgad else if ((ent->flags & CE_CREATE) && createlogs) 509114137Sgad ent->firstcreate = 1; 510114137Sgad else if (ent->flags & CE_CREATE) 511159998Sgad strlcpy(temp_reason, " (no -C option)", REASON_MAX); 512114137Sgad else if (createlogs) 513159998Sgad strlcpy(temp_reason, " (no C flag)", REASON_MAX); 514114137Sgad 515114137Sgad if (ent->firstcreate) { 516114137Sgad if (verbose) 517114137Sgad printf("does not exist -> will create.\n"); 518114137Sgad createlog(ent); 519114137Sgad } else if (verbose) { 520114137Sgad printf("does not exist, skipped%s.\n", temp_reason); 521114137Sgad } 52259003Shm } else { 523234674Sglebius if (ent->flags & CE_TRIMAT && !force && !rotatereq && 524234674Sglebius !oversized) { 525120361Sgad diffsecs = ptimeget_diff(timenow, ent->trim_at); 526120361Sgad if (diffsecs < 0.0) { 527120361Sgad /* trim_at is some time in the future. */ 528120361Sgad if (verbose) { 529120361Sgad ptime_adjust4dst(ent->trim_at, 530120361Sgad timenow); 53143071Swollman printf("--> will trim at %s", 532120361Sgad ptimeget_ctime(ent->trim_at)); 533120361Sgad } 534130165Sgad return (free_or_keep); 535120361Sgad } else if (diffsecs >= 3600.0) { 536120361Sgad /* 537120361Sgad * trim_at is more than an hour in the past, 538120361Sgad * so find the next valid trim_at time, and 539120361Sgad * tell the user what that will be. 540120361Sgad */ 541120361Sgad if (verbose && dbg_at_times) 542120361Sgad printf("\n\t--> prev trim at %s\t", 543120361Sgad ptimeget_ctime(ent->trim_at)); 544120361Sgad if (verbose) { 545120361Sgad ptimeset_nxtime(ent->trim_at); 546120361Sgad printf("--> will trim at %s", 547120361Sgad ptimeget_ctime(ent->trim_at)); 548120361Sgad } 549130165Sgad return (free_or_keep); 550120361Sgad } else if (verbose && noaction && dbg_at_times) { 551120361Sgad /* 552120361Sgad * If we are just debugging at-times, then 553120361Sgad * a detailed message is helpful. Also 554120361Sgad * skip "doing" any commands, since they 555120361Sgad * would all be turned off by no-action. 556120361Sgad */ 557120361Sgad printf("\n\t--> timematch at %s", 558120361Sgad ptimeget_ctime(ent->trim_at)); 559130165Sgad return (free_or_keep); 56043071Swollman } else if (verbose && ent->hours <= 0) { 56143071Swollman printf("--> time is up\n"); 56243071Swollman } 56343071Swollman } 564130165Sgad if (verbose && (ent->trsize > 0)) 565130165Sgad printf("size (Kb): %d [%d] ", ent->fsize, ent->trsize); 56659003Shm if (verbose && (ent->hours > 0)) 56759003Shm printf(" age (hr): %d [%d] ", modtime, ent->hours); 568111772Sgad 569111772Sgad /* 570111772Sgad * Figure out if this logfile needs to be rotated. 571111772Sgad */ 572111772Sgad temp_reason[0] = '\0'; 573111772Sgad if (rotatereq) { 574111772Sgad ent->rotate = 1; 575111772Sgad snprintf(temp_reason, REASON_MAX, " due to -R from %s", 576111772Sgad requestor); 577111772Sgad } else if (force) { 578111772Sgad ent->rotate = 1; 579111772Sgad snprintf(temp_reason, REASON_MAX, " due to -F request"); 580234674Sglebius } else if (oversized) { 581111772Sgad ent->rotate = 1; 582111772Sgad snprintf(temp_reason, REASON_MAX, " due to size>%dK", 583130165Sgad ent->trsize); 584111772Sgad } else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) { 585111772Sgad ent->rotate = 1; 586111772Sgad } else if ((ent->hours > 0) && ((modtime >= ent->hours) || 587111772Sgad (modtime < 0))) { 588111772Sgad ent->rotate = 1; 589111772Sgad } 590111772Sgad 591111772Sgad /* 592111772Sgad * If the file needs to be rotated, then rotate it. 593111772Sgad */ 594143106Sbrooks if (ent->rotate && !norotate) { 595111772Sgad if (temp_reason[0] != '\0') 596111772Sgad ent->r_reason = strdup(temp_reason); 59759003Shm if (verbose) 59859003Shm printf("--> trimming log....\n"); 599218127Smm if (noaction && !verbose) 600218127Smm printf("%s <%d%s>: trimming\n", ent->log, 601218127Smm ent->numlogs, 602218127Smm compress_type[ent->compress].flag); 603130165Sgad free_or_keep = do_rotate(ent); 60459003Shm } else { 60559003Shm if (verbose) 60659003Shm printf("--> skipping\n"); 60759003Shm } 60859003Shm } 609130165Sgad return (free_or_keep); 610111772Sgad#undef REASON_MAX 61113244Sgraichen} 61213244Sgraichen 61359004Shmstatic void 614111781Sgadparse_args(int argc, char **argv) 61513244Sgraichen{ 616111781Sgad int ch; 61759003Shm char *p; 61813244Sgraichen 619120361Sgad timenow = ptime_init(NULL); 620120361Sgad ptimeset_time(timenow, time(NULL)); 621159998Sgad strlcpy(daytime, ptimeget_ctime(timenow) + 4, DAYTIME_LEN); 62213244Sgraichen 62359003Shm /* Let's get our hostname */ 624111781Sgad (void)gethostname(hostname, sizeof(hostname)); 62513244Sgraichen 62613244Sgraichen /* Truncate domain */ 627111781Sgad if ((p = strchr(hostname, '.')) != NULL) 62813244Sgraichen *p = '\0'; 629111768Sgad 630111768Sgad /* Parse command line options. */ 631216832Sbrian while ((ch = getopt(argc, argv, "a:d:f:nrst:vCD:FNPR:S:")) != -1) 632111781Sgad switch (ch) { 63359004Shm case 'a': 63459004Shm archtodir++; 63559004Shm archdirname = optarg; 63659004Shm break; 637136127Sbrooks case 'd': 638136127Sbrooks destdir = optarg; 639136127Sbrooks break; 640111768Sgad case 'f': 641111768Sgad conf = optarg; 642111768Sgad break; 643111768Sgad case 'n': 644111768Sgad noaction++; 645245360Smarkj /* FALLTHROUGH */ 64659003Shm case 'r': 64759003Shm needroot = 0; 64859003Shm break; 649111768Sgad case 's': 650111768Sgad nosignal = 1; 651111768Sgad break; 652210372Ssimon case 't': 653210372Ssimon if (optarg[0] == '\0' || 654210372Ssimon strcmp(optarg, "DEFAULT") == 0) 655210372Ssimon timefnamefmt = strdup(DEFAULT_TIMEFNAME_FMT); 656210372Ssimon else 657210372Ssimon timefnamefmt = strdup(optarg); 658210372Ssimon break; 65959003Shm case 'v': 66059003Shm verbose++; 66159003Shm break; 662114137Sgad case 'C': 663114137Sgad /* Useful for things like rc.diskless... */ 664114137Sgad createlogs++; 665114137Sgad break; 666119904Sgad case 'D': 667119904Sgad /* 668119904Sgad * Set some debugging option. The specific option 669119904Sgad * depends on the value of optarg. These options 670119904Sgad * may come and go without notice or documentation. 671119904Sgad */ 672119904Sgad if (parse_doption(optarg)) 673119904Sgad break; 674119904Sgad usage(); 675119904Sgad /* NOTREACHED */ 67634584Spst case 'F': 67734584Spst force++; 67834584Spst break; 679143106Sbrooks case 'N': 680143106Sbrooks norotate++; 681143106Sbrooks break; 682202668Sdelphij case 'P': 683202668Sdelphij enforcepid++; 684202668Sdelphij break; 685111772Sgad case 'R': 686111772Sgad rotatereq++; 687111772Sgad requestor = strdup(optarg); 688111772Sgad break; 689210407Sbrian case 'S': 690210407Sbrian path_syslogpid = optarg; 691210407Sbrian break; 692111781Sgad case 'm': /* Used by OpenBSD for "monitor mode" */ 69359003Shm default: 69459003Shm usage(); 695111768Sgad /* NOTREACHED */ 69659003Shm } 697111772Sgad 698143106Sbrooks if (force && norotate) { 699143106Sbrooks warnx("Only one of -F and -N may be specified."); 700143106Sbrooks usage(); 701143106Sbrooks /* NOTREACHED */ 702143106Sbrooks } 703143106Sbrooks 704111772Sgad if (rotatereq) { 705111772Sgad if (optind == argc) { 706111772Sgad warnx("At least one filename must be given when -R is specified."); 707111772Sgad usage(); 708111772Sgad /* NOTREACHED */ 709111772Sgad } 710111772Sgad /* Make sure "requestor" value is safe for a syslog message. */ 711111772Sgad for (p = requestor; *p != '\0'; p++) { 712111772Sgad if (!isprintch(*p) && (*p != '\t')) 713111772Sgad *p = '.'; 714111772Sgad } 715111772Sgad } 716119904Sgad 717119904Sgad if (dbg_timenow) { 718119904Sgad /* 719119904Sgad * Note that the 'daytime' variable is not changed. 720119904Sgad * That is only used in messages that track when a 721119904Sgad * logfile is rotated, and if a file *is* rotated, 722119904Sgad * then it will still rotated at the "real now" time. 723119904Sgad */ 724120361Sgad ptime_free(timenow); 725119904Sgad timenow = dbg_timenow; 726119904Sgad fprintf(stderr, "Debug: Running as if TimeNow is %s", 727120361Sgad ptimeget_ctime(dbg_timenow)); 728119904Sgad } 729119904Sgad 73043071Swollman} 73113244Sgraichen 732120361Sgad/* 733120361Sgad * These debugging options are mainly meant for developer use, such 734120361Sgad * as writing regression-tests. They would not be needed by users 735120361Sgad * during normal operation of newsyslog... 736120361Sgad */ 737119904Sgadstatic int 738119904Sgadparse_doption(const char *doption) 739119904Sgad{ 740119904Sgad const char TN[] = "TN="; 741120361Sgad int res; 742119904Sgad 743119904Sgad if (strncmp(doption, TN, sizeof(TN) - 1) == 0) { 744119904Sgad /* 745120361Sgad * The "TimeNow" debugging option. This might be off 746120361Sgad * by an hour when crossing a timezone change. 747119904Sgad */ 748120361Sgad dbg_timenow = ptime_init(NULL); 749120361Sgad res = ptime_relparse(dbg_timenow, PTM_PARSE_ISO8601, 750120361Sgad time(NULL), doption + sizeof(TN) - 1); 751120361Sgad if (res == -2) { 752120361Sgad warnx("Non-existent time specified on -D %s", doption); 753120361Sgad return (0); /* failure */ 754120361Sgad } else if (res < 0) { 755119904Sgad warnx("Malformed time given on -D %s", doption); 756119904Sgad return (0); /* failure */ 757119904Sgad } 758119904Sgad return (1); /* successfully parsed */ 759119904Sgad 760119904Sgad } 761119904Sgad 762120361Sgad if (strcmp(doption, "ats") == 0) { 763120361Sgad dbg_at_times++; 764120361Sgad return (1); /* successfully parsed */ 765120361Sgad } 766120361Sgad 767159968Sgad /* XXX - This check could probably be dropped. */ 768159968Sgad if ((strcmp(doption, "neworder") == 0) || (strcmp(doption, "oldorder") 769159968Sgad == 0)) { 770159968Sgad warnx("NOTE: newsyslog always uses 'neworder'."); 771130167Sgad return (1); /* successfully parsed */ 772130167Sgad } 773130167Sgad 774130165Sgad warnx("Unknown -D (debug) option: '%s'", doption); 775119904Sgad return (0); /* failure */ 776119904Sgad} 777119904Sgad 77859004Shmstatic void 77959004Shmusage(void) 78013244Sgraichen{ 78180646Sobrien 78280646Sobrien fprintf(stderr, 783219434Sru "usage: newsyslog [-CFNPnrsv] [-a directory] [-d directory] [-f config_file]\n" 784219434Sru " [-S pidfile] [-t timefmt] [[-R tagname] file ...]\n"); 78559003Shm exit(1); 78613244Sgraichen} 78713244Sgraichen 78859004Shm/* 789111773Sgad * Parse a configuration file and return a linked list of all the logs 790111773Sgad * which should be processed. 79113244Sgraichen */ 792208648Sgordonstatic struct cflist * 793111773Sgadget_worklist(char **files) 79413244Sgraichen{ 79559003Shm FILE *f; 796111773Sgad char **given; 797208649Sgordon struct cflist *cmdlist, *filelist, *globlist; 798208648Sgordon struct conf_entry *defconf, *dupent, *ent; 799208649Sgordon struct ilist inclist; 800208649Sgordon struct include_entry *inc; 801112020Sgad int gmatch, fnres; 802111773Sgad 803208648Sgordon defconf = NULL; 804208649Sgordon STAILQ_INIT(&inclist); 805111773Sgad 806208648Sgordon filelist = malloc(sizeof(struct cflist)); 807208648Sgordon if (filelist == NULL) 808208648Sgordon err(1, "malloc of filelist"); 809208648Sgordon STAILQ_INIT(filelist); 810208648Sgordon globlist = malloc(sizeof(struct cflist)); 811208648Sgordon if (globlist == NULL) 812208648Sgordon err(1, "malloc of globlist"); 813208648Sgordon STAILQ_INIT(globlist); 814208648Sgordon 815208649Sgordon inc = malloc(sizeof(struct include_entry)); 816208649Sgordon if (inc == NULL) 817208649Sgordon err(1, "malloc of inc"); 818208649Sgordon inc->file = conf; 819208649Sgordon if (inc->file == NULL) 820208649Sgordon inc->file = _PATH_CONF; 821208649Sgordon STAILQ_INSERT_TAIL(&inclist, inc, inc_nextp); 822111773Sgad 823208649Sgordon STAILQ_FOREACH(inc, &inclist, inc_nextp) { 824208649Sgordon if (strcmp(inc->file, "-") != 0) 825208649Sgordon f = fopen(inc->file, "r"); 826208649Sgordon else { 827208649Sgordon f = stdin; 828208649Sgordon inc->file = "<stdin>"; 829208649Sgordon } 830208649Sgordon if (!f) 831208649Sgordon err(1, "%s", inc->file); 832208649Sgordon 833208649Sgordon if (verbose) 834208649Sgordon printf("Processing %s\n", inc->file); 835208649Sgordon parse_file(f, filelist, globlist, defconf, &inclist); 836208649Sgordon (void) fclose(f); 837111773Sgad } 838111773Sgad 839111773Sgad /* 840111773Sgad * All config-file information has been read in and turned into 841208648Sgordon * a filelist and a globlist. If there were no specific files 842112020Sgad * given on the run command, then the only thing left to do is to 843112020Sgad * call a routine which finds all files matched by the globlist 844208648Sgordon * and adds them to the filelist. Then return the worklist. 845111773Sgad */ 846111773Sgad if (*files == NULL) { 847208648Sgordon expand_globs(filelist, globlist); 848208648Sgordon free_clist(globlist); 849111773Sgad if (defconf != NULL) 850111773Sgad free_entry(defconf); 851208648Sgordon return (filelist); 852111773Sgad /* NOTREACHED */ 853111773Sgad } 854111773Sgad 855111773Sgad /* 856111773Sgad * If newsyslog was given a specific list of files to process, 857111773Sgad * it may be that some of those files were not listed in any 858111773Sgad * config file. Those unlisted files should get the default 859111773Sgad * rotation action. First, create the default-rotation action 860111773Sgad * if none was found in a system config file. 861111773Sgad */ 862111773Sgad if (defconf == NULL) { 863111773Sgad defconf = init_entry(DEFAULT_MARKER, NULL); 864111773Sgad defconf->numlogs = 3; 865130165Sgad defconf->trsize = 50; 866111773Sgad defconf->permissions = S_IRUSR|S_IWUSR; 867111773Sgad } 868111773Sgad 869111773Sgad /* 870111773Sgad * If newsyslog was run with a list of specific filenames, 871111773Sgad * then create a new worklist which has only those files in 872111773Sgad * it, picking up the rotation-rules for those files from 873208648Sgordon * the original filelist. 874111773Sgad * 875111773Sgad * XXX - Note that this will copy multiple rules for a single 876111773Sgad * logfile, if multiple entries are an exact match for 877111773Sgad * that file. That matches the historic behavior, but do 878111773Sgad * we want to continue to allow it? If so, it should 879111773Sgad * probably be handled more intelligently. 880111773Sgad */ 881208648Sgordon cmdlist = malloc(sizeof(struct cflist)); 882208648Sgordon if (cmdlist == NULL) 883208648Sgordon err(1, "malloc of cmdlist"); 884208648Sgordon STAILQ_INIT(cmdlist); 885208648Sgordon 886111773Sgad for (given = files; *given; ++given) { 887111773Sgad /* 888111773Sgad * First try to find exact-matches for this given file. 889111773Sgad */ 890112020Sgad gmatch = 0; 891208648Sgordon STAILQ_FOREACH(ent, filelist, cf_nextp) { 892111773Sgad if (strcmp(ent->log, *given) == 0) { 893111773Sgad gmatch++; 894111773Sgad dupent = init_entry(*given, ent); 895208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 896111773Sgad } 897111773Sgad } 898111773Sgad if (gmatch) { 899111773Sgad if (verbose > 2) 900111773Sgad printf("\t+ Matched entry %s\n", *given); 901111773Sgad continue; 902111773Sgad } 903111773Sgad 904111773Sgad /* 905111773Sgad * There was no exact-match for this given file, so look 906111773Sgad * for a "glob" entry which does match. 907111773Sgad */ 908112020Sgad gmatch = 0; 909112020Sgad if (verbose > 2 && globlist != NULL) 910112020Sgad printf("\t+ Checking globs for %s\n", *given); 911208648Sgordon STAILQ_FOREACH(ent, globlist, cf_nextp) { 912112020Sgad fnres = fnmatch(ent->log, *given, FNM_PATHNAME); 913112020Sgad if (verbose > 2) 914112020Sgad printf("\t+ = %d for pattern %s\n", fnres, 915112020Sgad ent->log); 916112020Sgad if (fnres == 0) { 917111773Sgad gmatch++; 918111773Sgad dupent = init_entry(*given, ent); 919112020Sgad /* This new entry is not a glob! */ 920111773Sgad dupent->flags &= ~CE_GLOB; 921208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 922111773Sgad /* Only allow a match to one glob-entry */ 923111773Sgad break; 924111773Sgad } 925111773Sgad } 926111773Sgad if (gmatch) { 927111773Sgad if (verbose > 2) 928111773Sgad printf("\t+ Matched %s via %s\n", *given, 929111773Sgad ent->log); 930111773Sgad continue; 931111773Sgad } 932111773Sgad 933111773Sgad /* 934111773Sgad * This given file was not found in any config file, so 935111773Sgad * add a worklist item based on the default entry. 936111773Sgad */ 937111773Sgad if (verbose > 2) 938111773Sgad printf("\t+ No entry matched %s (will use %s)\n", 939111773Sgad *given, DEFAULT_MARKER); 940111773Sgad dupent = init_entry(*given, defconf); 941111773Sgad /* Mark that it was *not* found in a config file */ 942111773Sgad dupent->def_cfg = 1; 943208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 944111773Sgad } 945111773Sgad 946111773Sgad /* 947112020Sgad * Free all the entries in the original work list, the list of 948112020Sgad * glob entries, and the default entry. 949111773Sgad */ 950208648Sgordon free_clist(filelist); 951208648Sgordon free_clist(globlist); 952112020Sgad free_entry(defconf); 953111773Sgad 954112020Sgad /* And finally, return a worklist which matches the given files. */ 955208648Sgordon return (cmdlist); 956111773Sgad} 957111773Sgad 958111773Sgad/* 959112020Sgad * Expand the list of entries with filename patterns, and add all files 960112020Sgad * which match those glob-entries onto the worklist. 961112020Sgad */ 962112020Sgadstatic void 963208648Sgordonexpand_globs(struct cflist *work_p, struct cflist *glob_p) 964112020Sgad{ 965161412Sdelphij int gmatch, gres; 966161412Sdelphij size_t i; 967112020Sgad char *mfname; 968208648Sgordon struct conf_entry *dupent, *ent, *globent; 969112020Sgad glob_t pglob; 970112020Sgad struct stat st_fm; 971112020Sgad 972112020Sgad /* 973112020Sgad * The worklist contains all fully-specified (non-GLOB) names. 974112020Sgad * 975112020Sgad * Now expand the list of filename-pattern (GLOB) entries into 976112020Sgad * a second list, which (by definition) will only match files 977112020Sgad * that already exist. Do not add a glob-related entry for any 978112020Sgad * file which already exists in the fully-specified list. 979112020Sgad */ 980208648Sgordon STAILQ_FOREACH(globent, glob_p, cf_nextp) { 981112020Sgad gres = glob(globent->log, GLOB_NOCHECK, NULL, &pglob); 982112020Sgad if (gres != 0) { 983112020Sgad warn("cannot expand pattern (%d): %s", gres, 984112020Sgad globent->log); 985112020Sgad continue; 986112020Sgad } 987112020Sgad 988112020Sgad if (verbose > 2) 989112020Sgad printf("\t+ Expanding pattern %s\n", globent->log); 990112020Sgad for (i = 0; i < pglob.gl_matchc; i++) { 991112020Sgad mfname = pglob.gl_pathv[i]; 992112020Sgad 993112020Sgad /* See if this file already has a specific entry. */ 994112020Sgad gmatch = 0; 995208648Sgordon STAILQ_FOREACH(ent, work_p, cf_nextp) { 996112020Sgad if (strcmp(mfname, ent->log) == 0) { 997112020Sgad gmatch++; 998112020Sgad break; 999112020Sgad } 1000112020Sgad } 1001112020Sgad if (gmatch) 1002112020Sgad continue; 1003112020Sgad 1004112020Sgad /* Make sure the named matched is a file. */ 1005112020Sgad gres = lstat(mfname, &st_fm); 1006112020Sgad if (gres != 0) { 1007112020Sgad /* Error on a file that glob() matched?!? */ 1008112020Sgad warn("Skipping %s - lstat() error", mfname); 1009112020Sgad continue; 1010112020Sgad } 1011112020Sgad if (!S_ISREG(st_fm.st_mode)) { 1012112020Sgad /* We only rotate files! */ 1013112020Sgad if (verbose > 2) 1014112020Sgad printf("\t+ . skipping %s (!file)\n", 1015112020Sgad mfname); 1016112020Sgad continue; 1017112020Sgad } 1018112020Sgad 1019112020Sgad if (verbose > 2) 1020112020Sgad printf("\t+ . add file %s\n", mfname); 1021112020Sgad dupent = init_entry(mfname, globent); 1022112020Sgad /* This new entry is not a glob! */ 1023112020Sgad dupent->flags &= ~CE_GLOB; 1024208648Sgordon 1025208648Sgordon /* Add to the worklist. */ 1026208648Sgordon STAILQ_INSERT_TAIL(work_p, dupent, cf_nextp); 1027112020Sgad } 1028112020Sgad globfree(&pglob); 1029112020Sgad if (verbose > 2) 1030112020Sgad printf("\t+ Done with pattern %s\n", globent->log); 1031112020Sgad } 1032112020Sgad} 1033112020Sgad 1034112020Sgad/* 1035111773Sgad * Parse a configuration file and update a linked list of all the logs to 1036111773Sgad * process. 1037111773Sgad */ 1038111773Sgadstatic void 1039208648Sgordonparse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p, 1040208649Sgordon struct conf_entry *defconf_p, struct ilist *inclist) 1041111773Sgad{ 104259003Shm char line[BUFSIZ], *parse, *q; 1043107737Ssobomax char *cp, *errline, *group; 1044208648Sgordon struct conf_entry *working; 1045111781Sgad struct passwd *pwd; 104659003Shm struct group *grp; 1047208649Sgordon glob_t pglob; 1048120361Sgad int eol, ptm_opts, res, special; 1049208649Sgordon size_t i; 105013244Sgraichen 1051130165Sgad errline = NULL; 1052111773Sgad while (fgets(line, BUFSIZ, cf)) { 1053107737Ssobomax if ((line[0] == '\n') || (line[0] == '#') || 1054107737Ssobomax (strlen(line) == 0)) 105559003Shm continue; 1056130165Sgad if (errline != NULL) 1057130165Sgad free(errline); 105859003Shm errline = strdup(line); 1059107737Ssobomax for (cp = line + 1; *cp != '\0'; cp++) { 1060107737Ssobomax if (*cp != '#') 1061107737Ssobomax continue; 1062107737Ssobomax if (*(cp - 1) == '\\') { 1063107737Ssobomax strcpy(cp - 1, cp); 1064107737Ssobomax cp--; 1065107737Ssobomax continue; 1066107737Ssobomax } 1067107737Ssobomax *cp = '\0'; 1068107737Ssobomax break; 1069107737Ssobomax } 107060373Sdes 107160373Sdes q = parse = missing_field(sob(line), errline); 107260373Sdes parse = son(line); 107360373Sdes if (!*parse) 107480646Sobrien errx(1, "malformed line (missing fields):\n%s", 107580646Sobrien errline); 107660373Sdes *parse = '\0'; 107760373Sdes 1078130165Sgad /* 1079130165Sgad * Allow people to set debug options via the config file. 1080208648Sgordon * (NOTE: debug options are undocumented, and may disappear 1081130165Sgad * at any time, etc). 1082130165Sgad */ 1083130165Sgad if (strcasecmp(DEBUG_MARKER, q) == 0) { 1084130165Sgad q = parse = missing_field(sob(++parse), errline); 1085130165Sgad parse = son(parse); 1086130165Sgad if (!*parse) 1087130165Sgad warnx("debug line specifies no option:\n%s", 1088130165Sgad errline); 1089130165Sgad else { 1090130165Sgad *parse = '\0'; 1091130165Sgad parse_doption(q); 1092130165Sgad } 1093130165Sgad continue; 1094208649Sgordon } else if (strcasecmp(INCLUDE_MARKER, q) == 0) { 1095208649Sgordon if (verbose) 1096208649Sgordon printf("Found: %s", errline); 1097208649Sgordon q = parse = missing_field(sob(++parse), errline); 1098208649Sgordon parse = son(parse); 1099208649Sgordon if (!*parse) { 1100208649Sgordon warnx("include line missing argument:\n%s", 1101208649Sgordon errline); 1102208649Sgordon continue; 1103208649Sgordon } 1104208649Sgordon 1105208649Sgordon *parse = '\0'; 1106208649Sgordon 1107208649Sgordon if (isglobstr(q)) { 1108208649Sgordon res = glob(q, GLOB_NOCHECK, NULL, &pglob); 1109208649Sgordon if (res != 0) { 1110208649Sgordon warn("cannot expand pattern (%d): %s", 1111208649Sgordon res, q); 1112208649Sgordon continue; 1113208649Sgordon } 1114208649Sgordon 1115208649Sgordon if (verbose > 2) 1116208649Sgordon printf("\t+ Expanding pattern %s\n", q); 1117208649Sgordon 1118208649Sgordon for (i = 0; i < pglob.gl_matchc; i++) 1119208649Sgordon add_to_queue(pglob.gl_pathv[i], 1120208649Sgordon inclist); 1121208649Sgordon globfree(&pglob); 1122208649Sgordon } else 1123208649Sgordon add_to_queue(q, inclist); 1124208649Sgordon continue; 1125130165Sgad } 1126130165Sgad 1127112020Sgad special = 0; 1128111388Sgad working = init_entry(q, NULL); 1129111388Sgad if (strcasecmp(DEFAULT_MARKER, q) == 0) { 1130112020Sgad special = 1; 1131208648Sgordon if (defconf_p != NULL) { 1132111388Sgad warnx("Ignoring duplicate entry for %s!", q); 1133111388Sgad free_entry(working); 1134111388Sgad continue; 1135111388Sgad } 1136208648Sgordon defconf_p = working; 113759003Shm } 113813244Sgraichen 113959003Shm q = parse = missing_field(sob(++parse), errline); 114059003Shm parse = son(parse); 114125518Sbrian if (!*parse) 114280646Sobrien errx(1, "malformed line (missing fields):\n%s", 114380646Sobrien errline); 114459003Shm *parse = '\0'; 114559003Shm if ((group = strchr(q, ':')) != NULL || 114659003Shm (group = strrchr(q, '.')) != NULL) { 114759003Shm *group++ = '\0'; 114859003Shm if (*q) { 1149119102Sgad if (!(isnumberstr(q))) { 1150111781Sgad if ((pwd = getpwnam(q)) == NULL) 115159003Shm errx(1, 115280646Sobrien "error in config file; unknown user:\n%s", 115359003Shm errline); 1154111781Sgad working->uid = pwd->pw_uid; 115559003Shm } else 115659003Shm working->uid = atoi(q); 115759003Shm } else 1158111779Sgad working->uid = (uid_t)-1; 115913244Sgraichen 116059003Shm q = group; 116159003Shm if (*q) { 1162119102Sgad if (!(isnumberstr(q))) { 116359003Shm if ((grp = getgrnam(q)) == NULL) 116459003Shm errx(1, 116580646Sobrien "error in config file; unknown group:\n%s", 116659003Shm errline); 116759003Shm working->gid = grp->gr_gid; 116859003Shm } else 116959003Shm working->gid = atoi(q); 117059003Shm } else 1171111779Sgad working->gid = (gid_t)-1; 117213244Sgraichen 117359003Shm q = parse = missing_field(sob(++parse), errline); 117459003Shm parse = son(parse); 117559003Shm if (!*parse) 117680646Sobrien errx(1, "malformed line (missing fields):\n%s", 117780646Sobrien errline); 117859003Shm *parse = '\0'; 1179111779Sgad } else { 1180111779Sgad working->uid = (uid_t)-1; 1181111779Sgad working->gid = (gid_t)-1; 1182111779Sgad } 118359003Shm 118459003Shm if (!sscanf(q, "%o", &working->permissions)) 118559003Shm errx(1, "error in config file; bad permissions:\n%s", 118659003Shm errline); 118759003Shm 118859003Shm q = parse = missing_field(sob(++parse), errline); 118959003Shm parse = son(parse); 119025518Sbrian if (!*parse) 119180646Sobrien errx(1, "malformed line (missing fields):\n%s", 119280646Sobrien errline); 119359003Shm *parse = '\0'; 1194111400Sgad if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0) 1195111400Sgad errx(1, "error in config file; bad value for count of logs to save:\n%s", 119659003Shm errline); 119713244Sgraichen 119859003Shm q = parse = missing_field(sob(++parse), errline); 119959003Shm parse = son(parse); 120025518Sbrian if (!*parse) 120180646Sobrien errx(1, "malformed line (missing fields):\n%s", 120280646Sobrien errline); 120359003Shm *parse = '\0'; 1204114762Sgad if (isdigitch(*q)) 1205130165Sgad working->trsize = atoi(q); 1206130165Sgad else if (strcmp(q, "*") == 0) 1207130165Sgad working->trsize = -1; 1208114762Sgad else { 1209114762Sgad warnx("Invalid value of '%s' for 'size' in line:\n%s", 1210114762Sgad q, errline); 1211130165Sgad working->trsize = -1; 1212114762Sgad } 121359003Shm 121459003Shm working->flags = 0; 1215218127Smm working->compress = COMPRESS_NONE; 121659003Shm q = parse = missing_field(sob(++parse), errline); 121759003Shm parse = son(parse); 121825518Sbrian eol = !*parse; 121959003Shm *parse = '\0'; 122043071Swollman { 122159003Shm char *ep; 122259003Shm u_long ul; 122313244Sgraichen 122443071Swollman ul = strtoul(q, &ep, 10); 122543071Swollman if (ep == q) 122643071Swollman working->hours = 0; 122743071Swollman else if (*ep == '*') 122843071Swollman working->hours = -1; 122943071Swollman else if (ul > INT_MAX) 123043071Swollman errx(1, "interval is too large:\n%s", errline); 123143071Swollman else 123243071Swollman working->hours = ul; 123343071Swollman 1234120361Sgad if (*ep == '\0' || strcmp(ep, "*") == 0) 1235120361Sgad goto no_trimat; 1236120361Sgad if (*ep != '@' && *ep != '$') 123743071Swollman errx(1, "malformed interval/at:\n%s", errline); 1238120361Sgad 1239120361Sgad working->flags |= CE_TRIMAT; 1240120361Sgad working->trim_at = ptime_init(NULL); 1241120361Sgad ptm_opts = PTM_PARSE_ISO8601; 1242120361Sgad if (*ep == '$') 1243120361Sgad ptm_opts = PTM_PARSE_DWM; 1244120361Sgad ptm_opts |= PTM_PARSE_MATCHDOM; 1245120361Sgad res = ptime_relparse(working->trim_at, ptm_opts, 1246120361Sgad ptimeget_secs(timenow), ep + 1); 1247120361Sgad if (res == -2) 1248120361Sgad errx(1, "nonexistent time for 'at' value:\n%s", 1249120361Sgad errline); 1250120361Sgad else if (res < 0) 1251120361Sgad errx(1, "malformed 'at' value:\n%s", errline); 125243071Swollman } 1253120361Sgadno_trimat: 125443071Swollman 125525518Sbrian if (eol) 125659003Shm q = NULL; 125725518Sbrian else { 125859003Shm q = parse = sob(++parse); /* Optional field */ 125959003Shm parse = son(parse); 126059003Shm if (!*parse) 126159003Shm eol = 1; 126259003Shm *parse = '\0'; 126325518Sbrian } 126425443Sache 1265111768Sgad for (; q && *q && !isspacech(*q); q++) { 1266111768Sgad switch (tolowerch(*q)) { 1267111768Sgad case 'b': 126859003Shm working->flags |= CE_BINARY; 1269111768Sgad break; 1270114137Sgad case 'c': 1271111768Sgad /* 1272114137Sgad * XXX - Ick! Ugly! Remove ASAP! 1273114137Sgad * We want `c' and `C' for "create". But we 1274114137Sgad * will temporarily treat `c' as `g', because 1275114137Sgad * FreeBSD releases <= 4.8 have a typo of 1276114137Sgad * checking ('G' || 'c') for CE_GLOB. 1277111768Sgad */ 1278114137Sgad if (*q == 'c') { 1279114137Sgad warnx("Assuming 'g' for 'c' in flags for line:\n%s", 1280114137Sgad errline); 1281114137Sgad warnx("The 'c' flag will eventually mean 'CREATE'"); 1282114137Sgad working->flags |= CE_GLOB; 1283114137Sgad break; 1284114137Sgad } 1285114137Sgad working->flags |= CE_CREATE; 1286114137Sgad break; 1287130043Sgad case 'd': 1288130043Sgad working->flags |= CE_NODUMP; 1289130043Sgad break; 1290111768Sgad case 'g': 1291106905Ssobomax working->flags |= CE_GLOB; 1292111768Sgad break; 1293111768Sgad case 'j': 1294218127Smm working->compress = COMPRESS_BZIP2; 1295111768Sgad break; 1296111768Sgad case 'n': 1297111768Sgad working->flags |= CE_NOSIGNAL; 1298111768Sgad break; 1299221873Ssobomax case 'r': 1300221873Ssobomax working->flags |= CE_PID2CMD; 1301221873Ssobomax break; 1302112003Sgad case 'u': 1303112003Sgad working->flags |= CE_SIGNALGROUP; 1304112003Sgad break; 1305111768Sgad case 'w': 1306160560Ssobomax /* Depreciated flag - keep for compatibility purposes */ 1307111768Sgad break; 1308218127Smm case 'x': 1309218127Smm working->compress = COMPRESS_XZ; 1310218127Smm break; 1311111768Sgad case 'z': 1312218127Smm working->compress = COMPRESS_GZIP; 1313111768Sgad break; 1314111768Sgad case '-': 1315111768Sgad break; 1316111781Sgad case 'f': /* Used by OpenBSD for "CE_FOLLOW" */ 1317111781Sgad case 'm': /* Used by OpenBSD for "CE_MONITOR" */ 1318111781Sgad case 'p': /* Used by NetBSD for "CE_PLAIN0" */ 1319111768Sgad default: 132080646Sobrien errx(1, "illegal flag in config file -- %c", 132180646Sobrien *q); 1322111768Sgad } 132359003Shm } 132459003Shm 132525518Sbrian if (eol) 132659003Shm q = NULL; 132725518Sbrian else { 132859003Shm q = parse = sob(++parse); /* Optional field */ 132959003Shm parse = son(parse); 133059003Shm if (!*parse) 133159003Shm eol = 1; 133259003Shm *parse = '\0'; 133325518Sbrian } 133425443Sache 1335221873Ssobomax working->pid_cmd_file = NULL; 133625443Sache if (q && *q) { 133725443Sache if (*q == '/') 1338221873Ssobomax working->pid_cmd_file = strdup(q); 133936817Sache else if (isdigit(*q)) 134036817Sache goto got_sig; 134159003Shm else 134280646Sobrien errx(1, 134380646Sobrien "illegal pid file or signal number in config file:\n%s", 134480646Sobrien errline); 134525443Sache } 134636817Sache if (eol) 134759003Shm q = NULL; 134836817Sache else { 134959003Shm q = parse = sob(++parse); /* Optional field */ 135059003Shm *(parse = son(parse)) = '\0'; 135136817Sache } 135236817Sache 135336817Sache working->sig = SIGHUP; 135436817Sache if (q && *q) { 135536817Sache if (isdigit(*q)) { 135659003Shm got_sig: 135736817Sache working->sig = atoi(q); 135836817Sache } else { 135959003Shm err_sig: 136080646Sobrien errx(1, 136180646Sobrien "illegal signal number in config file:\n%s", 136280646Sobrien errline); 136336817Sache } 136436817Sache if (working->sig < 1 || working->sig >= NSIG) 136536817Sache goto err_sig; 136636817Sache } 1367111768Sgad 1368111768Sgad /* 1369111768Sgad * Finish figuring out what pid-file to use (if any) in 1370111768Sgad * later processing if this logfile needs to be rotated. 1371111768Sgad */ 1372111768Sgad if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) { 1373111768Sgad /* 1374111768Sgad * This config-entry specified 'n' for nosignal, 1375221873Ssobomax * see if it also specified an explicit pid_cmd_file. 1376111768Sgad * This would be a pretty pointless combination. 1377111768Sgad */ 1378221873Ssobomax if (working->pid_cmd_file != NULL) { 1379111768Sgad warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s", 1380221873Ssobomax working->pid_cmd_file, errline); 1381221873Ssobomax free(working->pid_cmd_file); 1382221873Ssobomax working->pid_cmd_file = NULL; 1383111768Sgad } 1384221873Ssobomax } else if (working->pid_cmd_file == NULL) { 1385111768Sgad /* 1386111768Sgad * This entry did not specify the 'n' flag, which 1387111768Sgad * means it should signal syslogd unless it had 1388112003Sgad * specified some other pid-file (and obviously the 1389112003Sgad * syslog pid-file will not be for a process-group). 1390112003Sgad * Also, we should only try to notify syslog if we 1391112003Sgad * are root. 1392111768Sgad */ 1393112003Sgad if (working->flags & CE_SIGNALGROUP) { 1394112003Sgad warnx("Ignoring flag 'U' in line:\n%s", 1395112003Sgad errline); 1396112003Sgad working->flags &= ~CE_SIGNALGROUP; 1397112003Sgad } 1398111768Sgad if (needroot) 1399221873Ssobomax working->pid_cmd_file = strdup(path_syslogpid); 1400111768Sgad } 1401111768Sgad 1402112020Sgad /* 1403112020Sgad * Add this entry to the appropriate list of entries, unless 1404112020Sgad * it was some kind of special entry (eg: <default>). 1405112020Sgad */ 1406112020Sgad if (special) { 1407112020Sgad ; /* Do not add to any list */ 1408112020Sgad } else if (working->flags & CE_GLOB) { 1409208648Sgordon STAILQ_INSERT_TAIL(glob_p, working, cf_nextp); 1410112020Sgad } else { 1411208648Sgordon STAILQ_INSERT_TAIL(work_p, working, cf_nextp); 1412112020Sgad } 1413130165Sgad } 1414130165Sgad if (errline != NULL) 141559003Shm free(errline); 141613244Sgraichen} 141713244Sgraichen 141859003Shmstatic char * 141959004Shmmissing_field(char *p, char *errline) 142013244Sgraichen{ 142180646Sobrien 142259003Shm if (!p || !*p) 142359003Shm errx(1, "missing field in config file:\n%s", errline); 142459003Shm return (p); 142513244Sgraichen} 142613244Sgraichen 1427208649Sgordon/* 1428210372Ssimon * In our sort we return it in the reverse of what qsort normally 1429210372Ssimon * would do, as we want the newest files first. If we have two 1430210372Ssimon * entries with the same time we don't really care about order. 1431210372Ssimon * 1432210372Ssimon * Support function for qsort() in delete_oldest_timelog(). 1433210372Ssimon */ 1434210372Ssimonstatic int 1435210372Ssimonoldlog_entry_compare(const void *a, const void *b) 1436210372Ssimon{ 1437210372Ssimon const struct oldlog_entry *ola = a, *olb = b; 1438210372Ssimon 1439210372Ssimon if (ola->t > olb->t) 1440210372Ssimon return (-1); 1441210372Ssimon else if (ola->t < olb->t) 1442210372Ssimon return (1); 1443210372Ssimon else 1444210372Ssimon return (0); 1445210372Ssimon} 1446210372Ssimon 1447210372Ssimon/* 1448250478Smarkj * Check whether the file corresponding to dp is an archive of the logfile 1449250478Smarkj * logfname, based on the timefnamefmt format string. Return true and fill out 1450250478Smarkj * tm if this is the case; otherwise return false. 1451250478Smarkj */ 1452250478Smarkjstatic int 1453251532Smarkjvalidate_old_timelog(int fd, const struct dirent *dp, const char *logfname, 1454251532Smarkj struct tm *tm) 1455250478Smarkj{ 1456251532Smarkj struct stat sb; 1457250478Smarkj size_t logfname_len; 1458250478Smarkj char *s; 1459250478Smarkj int c; 1460250478Smarkj 1461250478Smarkj logfname_len = strlen(logfname); 1462250478Smarkj 1463251532Smarkj if (dp->d_type != DT_REG) { 1464251532Smarkj /* 1465251532Smarkj * Some filesystems (e.g. NFS) don't fill out the d_type field 1466251532Smarkj * and leave it set to DT_UNKNOWN; in this case we must obtain 1467251532Smarkj * the file type ourselves. 1468251532Smarkj */ 1469251532Smarkj if (dp->d_type != DT_UNKNOWN || 1470251532Smarkj fstatat(fd, dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) != 0 || 1471251532Smarkj !S_ISREG(sb.st_mode)) 1472251532Smarkj return (0); 1473251532Smarkj } 1474250478Smarkj /* Ignore everything but files with our logfile prefix. */ 1475250478Smarkj if (strncmp(dp->d_name, logfname, logfname_len) != 0) 1476250478Smarkj return (0); 1477250478Smarkj /* Ignore the actual non-rotated logfile. */ 1478250478Smarkj if (dp->d_namlen == logfname_len) 1479250478Smarkj return (0); 1480250478Smarkj 1481250478Smarkj /* 1482250478Smarkj * Make sure we created have found a logfile, so the 1483250478Smarkj * postfix is valid, IE format is: '.<time>(.[bgx]z)?'. 1484250478Smarkj */ 1485250478Smarkj if (dp->d_name[logfname_len] != '.') { 1486250478Smarkj if (verbose) 1487250478Smarkj printf("Ignoring %s which has unexpected " 1488250478Smarkj "extension '%s'\n", dp->d_name, 1489250478Smarkj &dp->d_name[logfname_len]); 1490250478Smarkj return (0); 1491250478Smarkj } 1492261821Smarkj memset(tm, 0, sizeof(*tm)); 1493250478Smarkj if ((s = strptime(&dp->d_name[logfname_len + 1], 1494250478Smarkj timefnamefmt, tm)) == NULL) { 1495250478Smarkj /* 1496250478Smarkj * We could special case "old" sequentially named logfiles here, 1497250478Smarkj * but we do not as that would require special handling to 1498250478Smarkj * decide which one was the oldest compared to "new" time based 1499250478Smarkj * logfiles. 1500250478Smarkj */ 1501250478Smarkj if (verbose) 1502250478Smarkj printf("Ignoring %s which does not " 1503250478Smarkj "match time format\n", dp->d_name); 1504250478Smarkj return (0); 1505250478Smarkj } 1506250478Smarkj 1507250478Smarkj for (c = 0; c < COMPRESS_TYPES; c++) 1508250478Smarkj if (strcmp(s, compress_type[c].suffix) == 0) 1509250478Smarkj /* We're done. */ 1510250478Smarkj return (1); 1511250478Smarkj 1512250478Smarkj if (verbose) 1513250478Smarkj printf("Ignoring %s which has unexpected extension '%s'\n", 1514250478Smarkj dp->d_name, s); 1515250478Smarkj 1516250478Smarkj return (0); 1517250478Smarkj} 1518250478Smarkj 1519250478Smarkj/* 1520210372Ssimon * Delete the oldest logfiles, when using time based filenames. 1521210372Ssimon */ 1522210372Ssimonstatic void 1523210372Ssimondelete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir) 1524210372Ssimon{ 1525210372Ssimon char *logfname, *s, *dir, errbuf[80]; 1526251532Smarkj int dir_fd, i, logcnt, max_logcnt; 1527210372Ssimon struct oldlog_entry *oldlogs; 1528210372Ssimon struct dirent *dp; 1529210372Ssimon const char *cdir; 1530210372Ssimon struct tm tm; 1531210372Ssimon DIR *dirp; 1532210372Ssimon 1533210372Ssimon oldlogs = malloc(MAX_OLDLOGS * sizeof(struct oldlog_entry)); 1534210372Ssimon max_logcnt = MAX_OLDLOGS; 1535210372Ssimon logcnt = 0; 1536210372Ssimon 1537210372Ssimon if (archive_dir != NULL && archive_dir[0] != '\0') 1538210372Ssimon cdir = archive_dir; 1539210372Ssimon else 1540210372Ssimon if ((cdir = dirname(ent->log)) == NULL) 1541210372Ssimon err(1, "dirname()"); 1542210372Ssimon if ((dir = strdup(cdir)) == NULL) 1543210372Ssimon err(1, "strdup()"); 1544210372Ssimon 1545210372Ssimon if ((s = basename(ent->log)) == NULL) 1546210372Ssimon err(1, "basename()"); 1547210372Ssimon if ((logfname = strdup(s)) == NULL) 1548210372Ssimon err(1, "strdup()"); 1549210372Ssimon if (strcmp(logfname, "/") == 0) 1550210372Ssimon errx(1, "Invalid log filename - became '/'"); 1551210372Ssimon 1552210372Ssimon if (verbose > 2) 1553210372Ssimon printf("Searching for old logs in %s\n", dir); 1554210372Ssimon 1555210372Ssimon /* First we create a 'list' of all archived logfiles */ 1556210372Ssimon if ((dirp = opendir(dir)) == NULL) 1557210372Ssimon err(1, "Cannot open log directory '%s'", dir); 1558251532Smarkj dir_fd = dirfd(dirp); 1559210372Ssimon while ((dp = readdir(dirp)) != NULL) { 1560251532Smarkj if (validate_old_timelog(dir_fd, dp, logfname, &tm) == 0) 1561210372Ssimon continue; 1562210372Ssimon 1563210372Ssimon /* 1564210372Ssimon * We should now have old an old rotated logfile, so 1565210372Ssimon * add it to the 'list'. 1566210372Ssimon */ 1567210372Ssimon if ((oldlogs[logcnt].t = timegm(&tm)) == -1) 1568210372Ssimon err(1, "Could not convert time string to time value"); 1569210372Ssimon if ((oldlogs[logcnt].fname = strdup(dp->d_name)) == NULL) 1570210372Ssimon err(1, "strdup()"); 1571210372Ssimon logcnt++; 1572210372Ssimon 1573210372Ssimon /* 1574210372Ssimon * It is very unlikely we ever run out of space in the 1575210372Ssimon * logfile array from the default size, but lets 1576210372Ssimon * handle it anyway... 1577210372Ssimon */ 1578210372Ssimon if (logcnt >= max_logcnt) { 1579210372Ssimon max_logcnt *= 4; 1580210372Ssimon /* Detect integer overflow */ 1581210372Ssimon if (max_logcnt < logcnt) 1582210372Ssimon errx(1, "Too many old logfiles found"); 1583210372Ssimon oldlogs = realloc(oldlogs, 1584210372Ssimon max_logcnt * sizeof(struct oldlog_entry)); 1585210372Ssimon if (oldlogs == NULL) 1586210372Ssimon err(1, "realloc()"); 1587210372Ssimon } 1588210372Ssimon } 1589210372Ssimon 1590210372Ssimon /* Second, if needed we delete oldest archived logfiles */ 1591210372Ssimon if (logcnt > 0 && logcnt >= ent->numlogs && ent->numlogs > 1) { 1592210372Ssimon oldlogs = realloc(oldlogs, logcnt * 1593210372Ssimon sizeof(struct oldlog_entry)); 1594210372Ssimon if (oldlogs == NULL) 1595210372Ssimon err(1, "realloc()"); 1596210372Ssimon 1597210372Ssimon /* 1598210372Ssimon * We now sort the logs in the order of newest to 1599210372Ssimon * oldest. That way we can simply skip over the 1600210372Ssimon * number of records we want to keep. 1601210372Ssimon */ 1602210372Ssimon qsort(oldlogs, logcnt, sizeof(struct oldlog_entry), 1603210372Ssimon oldlog_entry_compare); 1604210372Ssimon for (i = ent->numlogs - 1; i < logcnt; i++) { 1605210372Ssimon if (noaction) 1606210372Ssimon printf("\trm -f %s/%s\n", dir, 1607210372Ssimon oldlogs[i].fname); 1608251532Smarkj else if (unlinkat(dir_fd, oldlogs[i].fname, 0) != 0) { 1609210372Ssimon snprintf(errbuf, sizeof(errbuf), 1610245360Smarkj "Could not delete old logfile '%s'", 1611210372Ssimon oldlogs[i].fname); 1612210372Ssimon perror(errbuf); 1613210372Ssimon } 1614210372Ssimon } 1615210372Ssimon } else if (verbose > 1) 1616210372Ssimon printf("No old logs to delete for logfile %s\n", ent->log); 1617210372Ssimon 1618210372Ssimon /* Third, cleanup */ 1619210372Ssimon closedir(dirp); 1620210372Ssimon for (i = 0; i < logcnt; i++) { 1621210372Ssimon assert(oldlogs[i].fname != NULL); 1622210372Ssimon free(oldlogs[i].fname); 1623210372Ssimon } 1624210372Ssimon free(oldlogs); 1625210372Ssimon free(logfname); 1626210372Ssimon free(dir); 1627210372Ssimon} 1628210372Ssimon 1629210372Ssimon/* 1630220926Ssimon * Generate a log filename, when using clasic filenames. 1631220926Ssimon */ 1632220926Ssimonstatic void 1633220926Ssimongen_clasiclog_fname(char *fname, size_t fname_sz, const char *archive_dir, 1634220926Ssimon const char *namepart, int numlogs_c) 1635220926Ssimon{ 1636220926Ssimon 1637220926Ssimon if (archive_dir[0] != '\0') 1638220926Ssimon (void) snprintf(fname, fname_sz, "%s/%s.%d", archive_dir, 1639220926Ssimon namepart, numlogs_c); 1640220926Ssimon else 1641220926Ssimon (void) snprintf(fname, fname_sz, "%s.%d", namepart, numlogs_c); 1642220926Ssimon} 1643220926Ssimon 1644220926Ssimon/* 1645220926Ssimon * Delete a rotated logfiles, when using clasic filenames. 1646220926Ssimon */ 1647220926Ssimonstatic void 1648220926Ssimondelete_clasiclog(const char *archive_dir, const char *namepart, int numlog_c) 1649220926Ssimon{ 1650220926Ssimon char file1[MAXPATHLEN], zfile1[MAXPATHLEN]; 1651220926Ssimon int c; 1652220926Ssimon 1653220926Ssimon gen_clasiclog_fname(file1, sizeof(file1), archive_dir, namepart, 1654220926Ssimon numlog_c); 1655220926Ssimon 1656220926Ssimon for (c = 0; c < COMPRESS_TYPES; c++) { 1657220926Ssimon (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, 1658220926Ssimon compress_type[c].suffix); 1659220926Ssimon if (noaction) 1660220926Ssimon printf("\trm -f %s\n", zfile1); 1661220926Ssimon else 1662220926Ssimon (void) unlink(zfile1); 1663220926Ssimon } 1664220926Ssimon} 1665220926Ssimon 1666220926Ssimon/* 1667208649Sgordon * Only add to the queue if the file hasn't already been added. This is 1668208649Sgordon * done to prevent circular include loops. 1669208649Sgordon */ 1670208649Sgordonstatic void 1671208649Sgordonadd_to_queue(const char *fname, struct ilist *inclist) 1672208649Sgordon{ 1673208649Sgordon struct include_entry *inc; 1674208649Sgordon 1675208649Sgordon STAILQ_FOREACH(inc, inclist, inc_nextp) { 1676208649Sgordon if (strcmp(fname, inc->file) == 0) { 1677208649Sgordon warnx("duplicate include detected: %s", fname); 1678208649Sgordon return; 1679208649Sgordon } 1680208649Sgordon } 1681208649Sgordon 1682208649Sgordon inc = malloc(sizeof(struct include_entry)); 1683208649Sgordon if (inc == NULL) 1684208649Sgordon err(1, "malloc of inc"); 1685208649Sgordon inc->file = strdup(fname); 1686208649Sgordon 1687208649Sgordon if (verbose > 2) 1688208649Sgordon printf("\t+ Adding %s to the processing queue.\n", fname); 1689208649Sgordon 1690208649Sgordon STAILQ_INSERT_TAIL(inclist, inc, inc_nextp); 1691208649Sgordon} 1692208649Sgordon 1693218127Smm/* 1694218127Smm * Search for logfile and return its compression suffix (if supported) 1695218127Smm * The suffix detection is first-match in the order of compress_types 1696218127Smm * 1697218127Smm * Note: if logfile without suffix exists (uncompressed, COMPRESS_NONE) 1698218127Smm * a zero-length string is returned 1699218127Smm */ 1700218127Smmstatic const char * 1701218127Smmget_logfile_suffix(const char *logfile) 1702218127Smm{ 1703218127Smm struct stat st; 1704218127Smm char zfile[MAXPATHLEN]; 1705218944Ssobomax int c; 1706218127Smm 1707218944Ssobomax for (c = 0; c < COMPRESS_TYPES; c++) { 1708218127Smm (void) strlcpy(zfile, logfile, MAXPATHLEN); 1709218127Smm (void) strlcat(zfile, compress_type[c].suffix, MAXPATHLEN); 1710218127Smm if (lstat(zfile, &st) == 0) 1711218127Smm return (compress_type[c].suffix); 1712218127Smm } 1713218127Smm return (NULL); 1714218127Smm} 1715218127Smm 1716130165Sgadstatic fk_entry 1717130165Sgaddo_rotate(const struct conf_entry *ent) 171813244Sgraichen{ 171971299Sjedgar char dirpart[MAXPATHLEN], namepart[MAXPATHLEN]; 172071299Sjedgar char file1[MAXPATHLEN], file2[MAXPATHLEN]; 172171299Sjedgar char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN]; 1722218127Smm const char *logfile_suffix; 1723210372Ssimon char datetimestr[30]; 1724159968Sgad int flags, numlogs_c; 1725130165Sgad fk_entry free_or_keep; 1726159968Sgad struct sigwork_entry *swork; 172759003Shm struct stat st; 1728210372Ssimon struct tm tm; 1729210372Ssimon time_t now; 173013244Sgraichen 1731119927Sgad flags = ent->flags; 1732130165Sgad free_or_keep = FREE_ENT; 1733119927Sgad 173459004Shm if (archtodir) { 173559004Shm char *p; 173613244Sgraichen 173759004Shm /* build complete name of archive directory into dirpart */ 173859004Shm if (*archdirname == '/') { /* absolute */ 173971299Sjedgar strlcpy(dirpart, archdirname, sizeof(dirpart)); 174059004Shm } else { /* relative */ 174159004Shm /* get directory part of logfile */ 1742119927Sgad strlcpy(dirpart, ent->log, sizeof(dirpart)); 174359004Shm if ((p = rindex(dirpart, '/')) == NULL) 174459004Shm dirpart[0] = '\0'; 174559004Shm else 174659004Shm *(p + 1) = '\0'; 174771299Sjedgar strlcat(dirpart, archdirname, sizeof(dirpart)); 174859004Shm } 174959004Shm 175059004Shm /* check if archive directory exists, if not, create it */ 175159004Shm if (lstat(dirpart, &st)) 1752114137Sgad createdir(ent, dirpart); 175359004Shm 175459004Shm /* get filename part of logfile */ 1755119927Sgad if ((p = rindex(ent->log, '/')) == NULL) 1756119927Sgad strlcpy(namepart, ent->log, sizeof(namepart)); 175759004Shm else 175871299Sjedgar strlcpy(namepart, p + 1, sizeof(namepart)); 175959004Shm } else { 1760210372Ssimon /* 1761220926Ssimon * Tell utility functions we are not using an archive 1762220926Ssimon * dir. 1763210372Ssimon */ 1764210372Ssimon dirpart[0] = '\0'; 1765220926Ssimon strlcpy(namepart, ent->log, sizeof(namepart)); 1766210372Ssimon } 1767210372Ssimon 1768210372Ssimon /* Delete old logs */ 1769210372Ssimon if (timefnamefmt != NULL) 1770210372Ssimon delete_oldest_timelog(ent, dirpart); 1771210372Ssimon else { 1772220926Ssimon /* 1773220926Ssimon * Handle cleaning up after legacy newsyslog where we 1774220926Ssimon * kept ent->numlogs + 1 files. This code can go away 1775220926Ssimon * at some point in the future. 1776220926Ssimon */ 1777220926Ssimon delete_clasiclog(dirpart, namepart, ent->numlogs); 1778220926Ssimon 1779220926Ssimon if (ent->numlogs > 0) 1780220926Ssimon delete_clasiclog(dirpart, namepart, ent->numlogs - 1); 1781220926Ssimon 178259003Shm } 178313244Sgraichen 1784210372Ssimon if (timefnamefmt != NULL) { 1785210372Ssimon /* If time functions fails we can't really do any sensible */ 1786210372Ssimon if (time(&now) == (time_t)-1 || 1787210372Ssimon localtime_r(&now, &tm) == NULL) 1788210372Ssimon bzero(&tm, sizeof(tm)); 1789210372Ssimon 1790210372Ssimon strftime(datetimestr, sizeof(datetimestr), timefnamefmt, &tm); 1791210372Ssimon if (archtodir) 1792210372Ssimon (void) snprintf(file1, sizeof(file1), "%s/%s.%s", 1793210372Ssimon dirpart, namepart, datetimestr); 1794210372Ssimon else 1795210372Ssimon (void) snprintf(file1, sizeof(file1), "%s.%s", 1796210372Ssimon ent->log, datetimestr); 1797210372Ssimon 1798210372Ssimon /* Don't run the code to move down logs */ 1799220926Ssimon numlogs_c = -1; 1800220926Ssimon } else { 1801220926Ssimon gen_clasiclog_fname(file1, sizeof(file1), dirpart, namepart, 1802220926Ssimon ent->numlogs - 1); 1803220926Ssimon numlogs_c = ent->numlogs - 2; /* copy for countdown */ 1804220926Ssimon } 1805210372Ssimon 180659003Shm /* Move down log files */ 1807220926Ssimon for (; numlogs_c >= 0; numlogs_c--) { 180871299Sjedgar (void) strlcpy(file2, file1, sizeof(file2)); 180959004Shm 1810220926Ssimon gen_clasiclog_fname(file1, sizeof(file1), dirpart, namepart, 1811220926Ssimon numlogs_c); 181259004Shm 1813218127Smm logfile_suffix = get_logfile_suffix(file1); 1814218127Smm if (logfile_suffix == NULL) 1815218127Smm continue; 1816218127Smm (void) strlcpy(zfile1, file1, MAXPATHLEN); 1817218127Smm (void) strlcpy(zfile2, file2, MAXPATHLEN); 1818218127Smm (void) strlcat(zfile1, logfile_suffix, MAXPATHLEN); 1819218127Smm (void) strlcat(zfile2, logfile_suffix, MAXPATHLEN); 1820218127Smm 1821130165Sgad if (noaction) 1822111967Sgad printf("\tmv %s %s\n", zfile1, zfile2); 1823130165Sgad else { 1824130165Sgad /* XXX - Ought to be checking for failure! */ 1825130165Sgad (void)rename(zfile1, zfile2); 182659003Shm } 1827130165Sgad change_attrs(zfile2, ent); 182859003Shm } 182913244Sgraichen 1830130038Sgad if (ent->numlogs > 0) { 1831129974Sgad if (noaction) { 1832130038Sgad /* 1833130038Sgad * Note that savelog() may succeed with using link() 1834130038Sgad * for the archtodir case, but there is no good way 1835130038Sgad * of knowing if it will when doing "noaction", so 1836130038Sgad * here we claim that it will have to do a copy... 1837130038Sgad */ 1838130038Sgad if (archtodir) 1839130038Sgad printf("\tcp %s %s\n", ent->log, file1); 1840130038Sgad else 1841130038Sgad printf("\tln %s %s\n", ent->log, file1); 1842245360Smarkj printf("\ttouch %s\t\t" 1843245360Smarkj "# Update mtime for 'when'-interval processing\n", 1844245360Smarkj file1); 1845130165Sgad } else { 1846130038Sgad if (!(flags & CE_BINARY)) { 1847130038Sgad /* Report the trimming to the old log */ 1848130038Sgad log_trim(ent->log, ent); 1849130038Sgad } 1850130038Sgad savelog(ent->log, file1); 1851245360Smarkj /* 1852245360Smarkj * Interval-based rotations are done using the mtime of 1853245360Smarkj * the most recently archived log, so make sure it gets 1854245360Smarkj * updated during a rotation. 1855245360Smarkj */ 1856245360Smarkj utimes(file1, NULL); 185759004Shm } 1858130165Sgad change_attrs(file1, ent); 185918075Sjkh } 186018075Sjkh 1861130038Sgad /* Create the new log file and move it into place */ 1862130038Sgad if (noaction) 1863111781Sgad printf("Start new log...\n"); 1864130038Sgad createlog(ent); 186525443Sache 1866111966Sgad /* 1867130167Sgad * Save all signalling and file-compression to be done after log 1868130167Sgad * files from all entries have been rotated. This way any one 1869130167Sgad * process will not be sent the same signal multiple times when 1870130167Sgad * multiple log files had to be rotated. 1871130167Sgad */ 1872159968Sgad swork = NULL; 1873221873Ssobomax if (ent->pid_cmd_file != NULL) 1874159968Sgad swork = save_sigwork(ent); 1875218127Smm if (ent->numlogs > 0 && ent->compress > COMPRESS_NONE) { 1876159968Sgad /* 1877159968Sgad * The zipwork_entry will include a pointer to this 1878159968Sgad * conf_entry, so the conf_entry should not be freed. 1879159968Sgad */ 1880159968Sgad free_or_keep = KEEP_ENT; 1881159968Sgad save_zipwork(ent, swork, ent->fsize, file1); 1882130167Sgad } 1883130167Sgad 1884130165Sgad return (free_or_keep); 188513244Sgraichen} 188613244Sgraichen 1887130167Sgadstatic void 1888130167Sgaddo_sigwork(struct sigwork_entry *swork) 1889130167Sgad{ 1890130204Sgad struct sigwork_entry *nextsig; 1891130204Sgad int kres, secs; 1892221873Ssobomax char *tmp; 1893130167Sgad 1894246918Smarkj if (swork->sw_runcmd == 0 && (!(swork->sw_pidok) || swork->sw_pid == 0)) 1895130167Sgad return; /* no work to do... */ 1896130167Sgad 1897130167Sgad /* 1898130167Sgad * If nosignal (-s) was specified, then do not signal any process. 1899130167Sgad * Note that a nosignal request triggers a warning message if the 1900130167Sgad * rotated logfile needs to be compressed, *unless* -R was also 1901130167Sgad * specified. We assume that an `-sR' request came from a process 1902130167Sgad * which writes to the logfile, and as such, we assume that process 1903130167Sgad * has already made sure the logfile is not presently in use. This 1904130167Sgad * just sets swork->sw_pidok to a special value, and do_zipwork 1905130167Sgad * will print any necessary warning(s). 1906130167Sgad */ 1907130167Sgad if (nosignal) { 1908130167Sgad if (!rotatereq) 1909130167Sgad swork->sw_pidok = -1; 1910130167Sgad return; 1911130167Sgad } 1912130167Sgad 1913130204Sgad /* 1914130204Sgad * Compute the pause between consecutive signals. Use a longer 1915130204Sgad * sleep time if we will be sending two signals to the same 1916130204Sgad * deamon or process-group. 1917130204Sgad */ 1918130204Sgad secs = 0; 1919130204Sgad nextsig = SLIST_NEXT(swork, sw_nextp); 1920130204Sgad if (nextsig != NULL) { 1921130204Sgad if (swork->sw_pid == nextsig->sw_pid) 1922130204Sgad secs = 10; 1923130204Sgad else 1924130204Sgad secs = 1; 1925130204Sgad } 1926130204Sgad 1927130167Sgad if (noaction) { 1928246918Smarkj if (swork->sw_runcmd) 1929246918Smarkj printf("\tsh -c '%s %d'\n", swork->sw_fname, 1930246918Smarkj swork->sw_signum); 1931246918Smarkj else { 1932246918Smarkj printf("\tkill -%d %d \t\t# %s\n", swork->sw_signum, 1933246918Smarkj (int)swork->sw_pid, swork->sw_fname); 1934246918Smarkj if (secs > 0) 1935246918Smarkj printf("\tsleep %d\n", secs); 1936246918Smarkj } 1937130167Sgad return; 1938130167Sgad } 1939130167Sgad 1940246918Smarkj if (swork->sw_runcmd) { 1941221873Ssobomax asprintf(&tmp, "%s %d", swork->sw_fname, swork->sw_signum); 1942221873Ssobomax if (tmp == NULL) { 1943221873Ssobomax warn("can't allocate memory to run %s", 1944221873Ssobomax swork->sw_fname); 1945221873Ssobomax return; 1946221873Ssobomax } 1947221873Ssobomax if (verbose) 1948221873Ssobomax printf("Run command: %s\n", tmp); 1949221873Ssobomax kres = system(tmp); 1950221873Ssobomax if (kres) { 1951221873Ssobomax warnx("%s: returned non-zero exit code: %d", 1952221873Ssobomax tmp, kres); 1953221873Ssobomax } 1954221873Ssobomax free(tmp); 1955221873Ssobomax return; 1956221873Ssobomax } 1957221873Ssobomax 1958130167Sgad kres = kill(swork->sw_pid, swork->sw_signum); 1959130167Sgad if (kres != 0) { 1960130167Sgad /* 1961130167Sgad * Assume that "no such process" (ESRCH) is something 1962130167Sgad * to warn about, but is not an error. Presumably the 1963130167Sgad * process which writes to the rotated log file(s) is 1964130167Sgad * gone, in which case we should have no problem with 1965130167Sgad * compressing the rotated log file(s). 1966130167Sgad */ 1967130167Sgad if (errno != ESRCH) 1968130167Sgad swork->sw_pidok = 0; 1969130167Sgad warn("can't notify %s, pid %d", swork->sw_pidtype, 1970130167Sgad (int)swork->sw_pid); 1971130167Sgad } else { 1972130204Sgad if (verbose) 1973130204Sgad printf("Notified %s pid %d = %s\n", swork->sw_pidtype, 1974130204Sgad (int)swork->sw_pid, swork->sw_fname); 1975130204Sgad if (secs > 0) { 1976130204Sgad if (verbose) 1977130204Sgad printf("Pause %d second(s) between signals\n", 1978130204Sgad secs); 1979130204Sgad sleep(secs); 1980130167Sgad } 1981130167Sgad } 1982130167Sgad} 1983130167Sgad 1984130167Sgadstatic void 1985130167Sgaddo_zipwork(struct zipwork_entry *zwork) 1986130167Sgad{ 1987130167Sgad const char *pgm_name, *pgm_path; 1988154566Sgad int errsav, fcount, zstatus; 1989130167Sgad pid_t pidzip, wpid; 1990130167Sgad char zresult[MAXPATHLEN]; 1991218944Ssobomax int c; 1992130167Sgad 1993229899Seadler assert(zwork != NULL); 1994130167Sgad pgm_path = NULL; 1995130167Sgad strlcpy(zresult, zwork->zw_fname, sizeof(zresult)); 1996229899Seadler if (zwork->zw_conf != NULL && 1997218127Smm zwork->zw_conf->compress > COMPRESS_NONE) 1998218944Ssobomax for (c = 1; c < COMPRESS_TYPES; c++) { 1999218127Smm if (zwork->zw_conf->compress == c) { 2000218127Smm pgm_path = compress_type[c].path; 2001218127Smm (void) strlcat(zresult, 2002218127Smm compress_type[c].suffix, sizeof(zresult)); 2003218127Smm break; 2004218127Smm } 2005130167Sgad } 2006130167Sgad if (pgm_path == NULL) { 2007130167Sgad warnx("invalid entry for %s in do_zipwork", zwork->zw_fname); 2008130167Sgad return; 2009130167Sgad } 2010154566Sgad pgm_name = strrchr(pgm_path, '/'); 2011154566Sgad if (pgm_name == NULL) 2012154566Sgad pgm_name = pgm_path; 2013154566Sgad else 2014154566Sgad pgm_name++; 2015130167Sgad 2016246918Smarkj if (zwork->zw_swork != NULL && zwork->zw_swork->sw_runcmd == 0 && 2017238520Sae zwork->zw_swork->sw_pidok <= 0) { 2018130167Sgad warnx( 2019130167Sgad "log %s not compressed because daemon(s) not notified", 2020130167Sgad zwork->zw_fname); 2021130167Sgad change_attrs(zwork->zw_fname, zwork->zw_conf); 2022130167Sgad return; 2023130167Sgad } 2024130167Sgad 2025130167Sgad if (noaction) { 2026130167Sgad printf("\t%s %s\n", pgm_name, zwork->zw_fname); 2027130167Sgad change_attrs(zresult, zwork->zw_conf); 2028130167Sgad return; 2029130167Sgad } 2030130167Sgad 2031154566Sgad fcount = 1; 2032130167Sgad pidzip = fork(); 2033154566Sgad while (pidzip < 0) { 2034154566Sgad /* 2035154566Sgad * The fork failed. If the failure was due to a temporary 2036154566Sgad * problem, then wait a short time and try it again. 2037154566Sgad */ 2038154566Sgad errsav = errno; 2039154566Sgad warn("fork() for `%s %s'", pgm_name, zwork->zw_fname); 2040154566Sgad if (errsav != EAGAIN || fcount > 5) 2041154566Sgad errx(1, "Exiting..."); 2042154566Sgad sleep(fcount * 12); 2043154566Sgad fcount++; 2044154566Sgad pidzip = fork(); 2045154566Sgad } 2046154566Sgad if (!pidzip) { 2047130167Sgad /* The child process executes the compression command */ 2048130167Sgad execl(pgm_path, pgm_path, "-f", zwork->zw_fname, (char *)0); 2049154566Sgad err(1, "execl(`%s -f %s')", pgm_path, zwork->zw_fname); 2050130167Sgad } 2051130167Sgad 2052130167Sgad wpid = waitpid(pidzip, &zstatus, 0); 2053130167Sgad if (wpid == -1) { 2054154566Sgad /* XXX - should this be a fatal error? */ 2055130167Sgad warn("%s: waitpid(%d)", pgm_path, pidzip); 2056130167Sgad return; 2057130167Sgad } 2058130167Sgad if (!WIFEXITED(zstatus)) { 2059154566Sgad warnx("`%s -f %s' did not terminate normally", pgm_name, 2060154566Sgad zwork->zw_fname); 2061130167Sgad return; 2062130167Sgad } 2063130167Sgad if (WEXITSTATUS(zstatus)) { 2064154566Sgad warnx("`%s -f %s' terminated with a non-zero status (%d)", 2065154566Sgad pgm_name, zwork->zw_fname, WEXITSTATUS(zstatus)); 2066130167Sgad return; 2067130167Sgad } 2068130167Sgad 2069130167Sgad /* Compression was successful, set file attributes on the result. */ 2070130167Sgad change_attrs(zresult, zwork->zw_conf); 2071130167Sgad} 2072130167Sgad 2073130167Sgad/* 2074130167Sgad * Save information on any process we need to signal. Any single 2075130167Sgad * process may need to be sent different signal-values for different 2076130167Sgad * log files, but usually a single signal-value will cause the process 2077130167Sgad * to close and re-open all of it's log files. 2078130167Sgad */ 2079130167Sgadstatic struct sigwork_entry * 2080130167Sgadsave_sigwork(const struct conf_entry *ent) 2081130167Sgad{ 2082130167Sgad struct sigwork_entry *sprev, *stmp; 2083130167Sgad int ndiff; 2084130167Sgad size_t tmpsiz; 2085130167Sgad 2086130167Sgad sprev = NULL; 2087130167Sgad ndiff = 1; 2088130167Sgad SLIST_FOREACH(stmp, &swhead, sw_nextp) { 2089221873Ssobomax ndiff = strcmp(ent->pid_cmd_file, stmp->sw_fname); 2090130167Sgad if (ndiff > 0) 2091130167Sgad break; 2092130167Sgad if (ndiff == 0) { 2093130167Sgad if (ent->sig == stmp->sw_signum) 2094130167Sgad break; 2095130167Sgad if (ent->sig > stmp->sw_signum) { 2096130167Sgad ndiff = 1; 2097130167Sgad break; 2098130167Sgad } 2099130167Sgad } 2100130167Sgad sprev = stmp; 2101130167Sgad } 2102130167Sgad if (stmp != NULL && ndiff == 0) 2103130167Sgad return (stmp); 2104130167Sgad 2105221873Ssobomax tmpsiz = sizeof(struct sigwork_entry) + strlen(ent->pid_cmd_file) + 1; 2106130167Sgad stmp = malloc(tmpsiz); 2107221873Ssobomax 2108246918Smarkj stmp->sw_runcmd = 0; 2109221873Ssobomax /* If this is a command to run we just set the flag and run command */ 2110221873Ssobomax if (ent->flags & CE_PID2CMD) { 2111246918Smarkj stmp->sw_pid = -1; 2112246918Smarkj stmp->sw_pidok = 0; 2113246918Smarkj stmp->sw_runcmd = 1; 2114221873Ssobomax } else { 2115221873Ssobomax set_swpid(stmp, ent); 2116221873Ssobomax } 2117130167Sgad stmp->sw_signum = ent->sig; 2118221873Ssobomax strcpy(stmp->sw_fname, ent->pid_cmd_file); 2119130167Sgad if (sprev == NULL) 2120130167Sgad SLIST_INSERT_HEAD(&swhead, stmp, sw_nextp); 2121130167Sgad else 2122130167Sgad SLIST_INSERT_AFTER(sprev, stmp, sw_nextp); 2123130167Sgad return (stmp); 2124130167Sgad} 2125130167Sgad 2126130167Sgad/* 2127130167Sgad * Save information on any file we need to compress. We may see the same 2128130167Sgad * file multiple times, so check the full list to avoid duplicates. The 2129130167Sgad * list itself is sorted smallest-to-largest, because that's the order we 2130130167Sgad * want to compress the files. If the partition is very low on disk space, 2131130167Sgad * then the smallest files are the most likely to compress, and compressing 2132130167Sgad * them first will free up more space for the larger files. 2133130167Sgad */ 2134130167Sgadstatic struct zipwork_entry * 2135130167Sgadsave_zipwork(const struct conf_entry *ent, const struct sigwork_entry *swork, 2136130167Sgad int zsize, const char *zipfname) 2137130167Sgad{ 2138130167Sgad struct zipwork_entry *zprev, *ztmp; 2139130167Sgad int ndiff; 2140130167Sgad size_t tmpsiz; 2141130167Sgad 2142130167Sgad /* Compute the size if the caller did not know it. */ 2143130167Sgad if (zsize < 0) 2144130167Sgad zsize = sizefile(zipfname); 2145130167Sgad 2146130167Sgad zprev = NULL; 2147130167Sgad ndiff = 1; 2148130167Sgad SLIST_FOREACH(ztmp, &zwhead, zw_nextp) { 2149130707Sgad ndiff = strcmp(zipfname, ztmp->zw_fname); 2150130167Sgad if (ndiff == 0) 2151130167Sgad break; 2152130167Sgad if (zsize > ztmp->zw_fsize) 2153130167Sgad zprev = ztmp; 2154130167Sgad } 2155130167Sgad if (ztmp != NULL && ndiff == 0) 2156130167Sgad return (ztmp); 2157130167Sgad 2158130167Sgad tmpsiz = sizeof(struct zipwork_entry) + strlen(zipfname) + 1; 2159130167Sgad ztmp = malloc(tmpsiz); 2160130167Sgad ztmp->zw_conf = ent; 2161130167Sgad ztmp->zw_swork = swork; 2162130167Sgad ztmp->zw_fsize = zsize; 2163130167Sgad strcpy(ztmp->zw_fname, zipfname); 2164130167Sgad if (zprev == NULL) 2165130167Sgad SLIST_INSERT_HEAD(&zwhead, ztmp, zw_nextp); 2166130167Sgad else 2167130167Sgad SLIST_INSERT_AFTER(zprev, ztmp, zw_nextp); 2168130167Sgad return (ztmp); 2169130167Sgad} 2170130167Sgad 2171130167Sgad/* Send a signal to the pid specified by pidfile */ 2172130167Sgadstatic void 2173130167Sgadset_swpid(struct sigwork_entry *swork, const struct conf_entry *ent) 2174130167Sgad{ 2175130167Sgad FILE *f; 2176130167Sgad long minok, maxok, rval; 2177130167Sgad char *endp, *linep, line[BUFSIZ]; 2178130167Sgad 2179130167Sgad minok = MIN_PID; 2180130167Sgad maxok = MAX_PID; 2181130167Sgad swork->sw_pidok = 0; 2182130167Sgad swork->sw_pid = 0; 2183130167Sgad swork->sw_pidtype = "daemon"; 2184130167Sgad if (ent->flags & CE_SIGNALGROUP) { 2185130167Sgad /* 2186130167Sgad * If we are expected to signal a process-group when 2187130167Sgad * rotating this logfile, then the value read in should 2188130167Sgad * be the negative of a valid process ID. 2189130167Sgad */ 2190130167Sgad minok = -MAX_PID; 2191130167Sgad maxok = -MIN_PID; 2192130167Sgad swork->sw_pidtype = "process-group"; 2193130167Sgad } 2194130167Sgad 2195221873Ssobomax f = fopen(ent->pid_cmd_file, "r"); 2196130167Sgad if (f == NULL) { 2197202668Sdelphij if (errno == ENOENT && enforcepid == 0) { 2198200806Sdelphij /* 2199200806Sdelphij * Warn if the PID file doesn't exist, but do 2200200806Sdelphij * not consider it an error. Most likely it 2201200806Sdelphij * means the process has been terminated, 2202200806Sdelphij * so it should be safe to rotate any log 2203200806Sdelphij * files that the process would have been using. 2204200806Sdelphij */ 2205200806Sdelphij swork->sw_pidok = 1; 2206221873Ssobomax warnx("pid file doesn't exist: %s", ent->pid_cmd_file); 2207200806Sdelphij } else 2208221873Ssobomax warn("can't open pid file: %s", ent->pid_cmd_file); 2209130167Sgad return; 2210130167Sgad } 2211130167Sgad 2212130167Sgad if (fgets(line, BUFSIZ, f) == NULL) { 2213130167Sgad /* 2214130167Sgad * Warn if the PID file is empty, but do not consider 2215130167Sgad * it an error. Most likely it means the process has 2216130167Sgad * has terminated, so it should be safe to rotate any 2217130167Sgad * log files that the process would have been using. 2218130167Sgad */ 2219202668Sdelphij if (feof(f) && enforcepid == 0) { 2220130167Sgad swork->sw_pidok = 1; 2221221873Ssobomax warnx("pid/cmd file is empty: %s", ent->pid_cmd_file); 2222130167Sgad } else 2223221873Ssobomax warn("can't read from pid file: %s", ent->pid_cmd_file); 2224130167Sgad (void)fclose(f); 2225130167Sgad return; 2226130167Sgad } 2227130167Sgad (void)fclose(f); 2228130167Sgad 2229130167Sgad errno = 0; 2230130167Sgad linep = line; 2231130167Sgad while (*linep == ' ') 2232130167Sgad linep++; 2233130167Sgad rval = strtol(linep, &endp, 10); 2234130167Sgad if (*endp != '\0' && !isspacech(*endp)) { 2235130167Sgad warnx("pid file does not start with a valid number: %s", 2236221873Ssobomax ent->pid_cmd_file); 2237130167Sgad } else if (rval < minok || rval > maxok) { 2238130167Sgad warnx("bad value '%ld' for process number in %s", 2239221873Ssobomax rval, ent->pid_cmd_file); 2240130167Sgad if (verbose) 2241130167Sgad warnx("\t(expecting value between %ld and %ld)", 2242130167Sgad minok, maxok); 2243130167Sgad } else { 2244130167Sgad swork->sw_pidok = 1; 2245130167Sgad swork->sw_pid = rval; 2246130167Sgad } 2247130167Sgad 2248130167Sgad return; 2249130167Sgad} 2250130167Sgad 225113244Sgraichen/* Log the fact that the logs were turned over */ 225259004Shmstatic int 2253119926Sgadlog_trim(const char *logname, const struct conf_entry *log_ent) 225413244Sgraichen{ 225559003Shm FILE *f; 2256111388Sgad const char *xtra; 225759003Shm 2258119926Sgad if ((f = fopen(logname, "a")) == NULL) 225959003Shm return (-1); 2260111388Sgad xtra = ""; 2261111768Sgad if (log_ent->def_cfg) 2262111388Sgad xtra = " using <default> rule"; 2263114137Sgad if (log_ent->firstcreate) 2264114137Sgad fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n", 2265114137Sgad daytime, hostname, (int) getpid(), xtra); 2266114137Sgad else if (log_ent->r_reason != NULL) 2267111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n", 2268111772Sgad daytime, hostname, (int) getpid(), log_ent->r_reason, xtra); 2269111772Sgad else 2270111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n", 2271111772Sgad daytime, hostname, (int) getpid(), xtra); 227259003Shm if (fclose(f) == EOF) 2273127858Scharnier err(1, "log_trim: fclose"); 227459003Shm return (0); 227513244Sgraichen} 227613244Sgraichen 227713244Sgraichen/* Return size in kilobytes of a file */ 227859004Shmstatic int 2279130165Sgadsizefile(const char *file) 228013244Sgraichen{ 228159003Shm struct stat sb; 228213244Sgraichen 228359003Shm if (stat(file, &sb) < 0) 228459003Shm return (-1); 2285262076Sbdrewery return (kbytes(sb.st_size)); 228613244Sgraichen} 228713244Sgraichen 2288250478Smarkj/* 2289250478Smarkj * Return the mtime of the most recent archive of the logfile, using timestamp 2290250478Smarkj * based filenames. 2291250478Smarkj */ 2292250478Smarkjstatic time_t 2293250478Smarkjmtime_old_timelog(const char *file) 2294250478Smarkj{ 2295250478Smarkj struct stat sb; 2296250478Smarkj struct tm tm; 2297250478Smarkj int dir_fd; 2298250478Smarkj time_t t; 2299250478Smarkj struct dirent *dp; 2300250478Smarkj DIR *dirp; 2301250478Smarkj char *s, *logfname, *dir; 2302250478Smarkj 2303250478Smarkj t = -1; 2304250478Smarkj 2305250478Smarkj if ((dir = dirname(file)) == NULL) { 2306250478Smarkj warn("dirname() of '%s'", file); 2307250478Smarkj return (t); 2308250478Smarkj } 2309250478Smarkj if ((s = basename(file)) == NULL) { 2310250478Smarkj warn("basename() of '%s'", file); 2311250478Smarkj return (t); 2312250478Smarkj } else if (s[0] == '/') { 2313250478Smarkj warnx("Invalid log filename '%s'", s); 2314250478Smarkj return (t); 2315250478Smarkj } else if ((logfname = strdup(s)) == NULL) 2316250478Smarkj err(1, "strdup()"); 2317250478Smarkj 2318250478Smarkj if ((dirp = opendir(dir)) == NULL) { 2319250478Smarkj warn("Cannot open log directory '%s'", dir); 2320250478Smarkj return (t); 2321250478Smarkj } 2322250478Smarkj dir_fd = dirfd(dirp); 2323250478Smarkj /* Open the archive dir and find the most recent archive of logfname. */ 2324250478Smarkj while ((dp = readdir(dirp)) != NULL) { 2325251532Smarkj if (validate_old_timelog(dir_fd, dp, logfname, &tm) == 0) 2326250478Smarkj continue; 2327250478Smarkj 2328251532Smarkj if (fstatat(dir_fd, dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) == -1) { 2329250478Smarkj warn("Cannot stat '%s'", file); 2330250478Smarkj continue; 2331250478Smarkj } 2332250478Smarkj if (t < sb.st_mtime) 2333250478Smarkj t = sb.st_mtime; 2334250478Smarkj } 2335250478Smarkj closedir(dirp); 2336250478Smarkj 2337250478Smarkj return (t); 2338250478Smarkj} 2339250478Smarkj 2340250478Smarkj/* Return the age in hours of the most recent archive of the logfile. */ 234159004Shmstatic int 2342250478Smarkjage_old_log(const char *file) 234313244Sgraichen{ 234459003Shm struct stat sb; 2345218127Smm const char *logfile_suffix; 2346218127Smm char tmp[MAXPATHLEN + sizeof(".0") + COMPRESS_SUFFIX_MAXLEN + 1]; 2347250478Smarkj time_t mtime; 234813244Sgraichen 234959004Shm if (archtodir) { 235059004Shm char *p; 235159004Shm 235259004Shm /* build name of archive directory into tmp */ 235359004Shm if (*archdirname == '/') { /* absolute */ 235471299Sjedgar strlcpy(tmp, archdirname, sizeof(tmp)); 235559004Shm } else { /* relative */ 235659004Shm /* get directory part of logfile */ 235771299Sjedgar strlcpy(tmp, file, sizeof(tmp)); 235859004Shm if ((p = rindex(tmp, '/')) == NULL) 235959004Shm tmp[0] = '\0'; 236059004Shm else 236159004Shm *(p + 1) = '\0'; 236271299Sjedgar strlcat(tmp, archdirname, sizeof(tmp)); 236359004Shm } 236459004Shm 236571299Sjedgar strlcat(tmp, "/", sizeof(tmp)); 236659004Shm 236759004Shm /* get filename part of logfile */ 236859004Shm if ((p = rindex(file, '/')) == NULL) 236971299Sjedgar strlcat(tmp, file, sizeof(tmp)); 237059004Shm else 237171299Sjedgar strlcat(tmp, p + 1, sizeof(tmp)); 237259004Shm } else { 237371299Sjedgar (void) strlcpy(tmp, file, sizeof(tmp)); 237459004Shm } 237559004Shm 2376250478Smarkj if (timefnamefmt != NULL) { 2377250478Smarkj mtime = mtime_old_timelog(tmp); 2378250478Smarkj if (mtime == -1) 2379250478Smarkj return (-1); 2380250478Smarkj } else { 2381250478Smarkj strlcat(tmp, ".0", sizeof(tmp)); 2382250478Smarkj logfile_suffix = get_logfile_suffix(tmp); 2383250478Smarkj if (logfile_suffix == NULL) 2384250478Smarkj return (-1); 2385250478Smarkj (void) strlcat(tmp, logfile_suffix, sizeof(tmp)); 2386250478Smarkj if (stat(tmp, &sb) < 0) 2387250478Smarkj return (-1); 2388250478Smarkj mtime = sb.st_mtime; 2389250478Smarkj } 2390250478Smarkj 2391250478Smarkj return ((int)(ptimeget_secs(timenow) - mtime + 1800) / 3600); 239213244Sgraichen} 239313244Sgraichen 239413244Sgraichen/* Skip Over Blanks */ 2395111820Sgadstatic char * 239659004Shmsob(char *p) 239713244Sgraichen{ 239859003Shm while (p && *p && isspace(*p)) 239959003Shm p++; 240059003Shm return (p); 240113244Sgraichen} 240213244Sgraichen 240313244Sgraichen/* Skip Over Non-Blanks */ 2404111820Sgadstatic char * 240559004Shmson(char *p) 240613244Sgraichen{ 240759003Shm while (p && *p && !isspace(*p)) 240859003Shm p++; 240959003Shm return (p); 241013244Sgraichen} 241143071Swollman 2412119102Sgad/* Check if string is actually a number */ 2413119102Sgadstatic int 2414119102Sgadisnumberstr(const char *string) 2415119102Sgad{ 2416119102Sgad while (*string) { 2417119102Sgad if (!isdigitch(*string++)) 2418119102Sgad return (0); 2419119102Sgad } 2420119102Sgad return (1); 2421119102Sgad} 2422119102Sgad 2423208649Sgordon/* Check if string contains a glob */ 2424208649Sgordonstatic int 2425208649Sgordonisglobstr(const char *string) 2426208649Sgordon{ 2427208649Sgordon char chr; 2428208649Sgordon 2429208649Sgordon while ((chr = *string++)) { 2430208649Sgordon if (chr == '*' || chr == '?' || chr == '[') 2431208649Sgordon return (1); 2432208649Sgordon } 2433208649Sgordon return (0); 2434208649Sgordon} 2435208649Sgordon 2436130038Sgad/* 2437130038Sgad * Save the active log file under a new name. A link to the new name 2438130038Sgad * is the quick-and-easy way to do this. If that fails (which it will 2439130038Sgad * if the destination is on another partition), then make a copy of 2440130038Sgad * the file to the new location. 2441130038Sgad */ 244259004Shmstatic void 2443130038Sgadsavelog(char *from, char *to) 244459004Shm{ 244559004Shm FILE *src, *dst; 2446130038Sgad int c, res; 244759004Shm 2448130038Sgad res = link(from, to); 2449130038Sgad if (res == 0) 2450130038Sgad return; 2451130038Sgad 245259004Shm if ((src = fopen(from, "r")) == NULL) 245359004Shm err(1, "can't fopen %s for reading", from); 245459004Shm if ((dst = fopen(to, "w")) == NULL) 245559004Shm err(1, "can't fopen %s for writing", to); 245659004Shm 245759004Shm while ((c = getc(src)) != EOF) { 245859004Shm if ((putc(c, dst)) == EOF) 245959004Shm err(1, "error writing to %s", to); 246059004Shm } 246159004Shm 246259004Shm if (ferror(src)) 246359004Shm err(1, "error reading from %s", from); 246459004Shm if ((fclose(src)) != 0) 246559004Shm err(1, "can't fclose %s", to); 246659004Shm if ((fclose(dst)) != 0) 246759004Shm err(1, "can't fclose %s", from); 246859004Shm} 246959004Shm 247059004Shm/* create one or more directory components of a path */ 247159004Shmstatic void 2472114137Sgadcreatedir(const struct conf_entry *ent, char *dirpart) 247359004Shm{ 2474111398Sgad int res; 247559004Shm char *s, *d; 247671299Sjedgar char mkdirpath[MAXPATHLEN]; 247759004Shm struct stat st; 247859004Shm 247959004Shm s = dirpart; 248059004Shm d = mkdirpath; 248159004Shm 248259004Shm for (;;) { 248359004Shm *d++ = *s++; 2484111398Sgad if (*s != '/' && *s != '\0') 2485111398Sgad continue; 2486111398Sgad *d = '\0'; 2487111398Sgad res = lstat(mkdirpath, &st); 2488111398Sgad if (res != 0) { 2489111398Sgad if (noaction) { 2490111967Sgad printf("\tmkdir %s\n", mkdirpath); 2491111398Sgad } else { 2492111398Sgad res = mkdir(mkdirpath, 0755); 2493111398Sgad if (res != 0) 2494111398Sgad err(1, "Error on mkdir(\"%s\") for -a", 2495111398Sgad mkdirpath); 2496111398Sgad } 249759004Shm } 249859004Shm if (*s == '\0') 249959004Shm break; 250059004Shm } 2501114137Sgad if (verbose) { 2502114137Sgad if (ent->firstcreate) 2503114137Sgad printf("Created directory '%s' for new %s\n", 2504114137Sgad dirpart, ent->log); 2505114137Sgad else 2506114137Sgad printf("Created directory '%s' for -a\n", dirpart); 2507114137Sgad } 250859004Shm} 250959004Shm 2510114137Sgad/* 2511114137Sgad * Create a new log file, destroying any currently-existing version 2512114137Sgad * of the log file in the process. If the caller wants a backup copy 2513114137Sgad * of the file to exist, they should call 'link(logfile,logbackup)' 2514114137Sgad * before calling this routine. 2515114137Sgad */ 2516114137Sgadvoid 2517114137Sgadcreatelog(const struct conf_entry *ent) 2518114137Sgad{ 2519114137Sgad int fd, failed; 2520114137Sgad struct stat st; 2521114137Sgad char *realfile, *slash, tempfile[MAXPATHLEN]; 2522114137Sgad 2523114137Sgad fd = -1; 2524114137Sgad realfile = ent->log; 2525114137Sgad 2526114137Sgad /* 2527114137Sgad * If this log file is being created for the first time (-C option), 2528114137Sgad * then it may also be true that the parent directory does not exist 2529114137Sgad * yet. Check, and create that directory if it is missing. 2530114137Sgad */ 2531114137Sgad if (ent->firstcreate) { 2532114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 2533114137Sgad slash = strrchr(tempfile, '/'); 2534114137Sgad if (slash != NULL) { 2535114137Sgad *slash = '\0'; 2536131581Ssobomax failed = stat(tempfile, &st); 2537114137Sgad if (failed && errno != ENOENT) 2538131581Ssobomax err(1, "Error on stat(%s)", tempfile); 2539114137Sgad if (failed) 2540114137Sgad createdir(ent, tempfile); 2541114137Sgad else if (!S_ISDIR(st.st_mode)) 2542114137Sgad errx(1, "%s exists but is not a directory", 2543114137Sgad tempfile); 2544114137Sgad } 2545114137Sgad } 2546114137Sgad 2547114137Sgad /* 2548114137Sgad * First create an unused filename, so it can be chown'ed and 2549114137Sgad * chmod'ed before it is moved into the real location. mkstemp 2550114137Sgad * will create the file mode=600 & owned by us. Note that all 2551114137Sgad * temp files will have a suffix of '.z<something>'. 2552114137Sgad */ 2553114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 2554114137Sgad strlcat(tempfile, ".zXXXXXX", sizeof(tempfile)); 2555114137Sgad if (noaction) 2556114137Sgad printf("\tmktemp %s\n", tempfile); 2557114137Sgad else { 2558114137Sgad fd = mkstemp(tempfile); 2559114137Sgad if (fd < 0) 2560114137Sgad err(1, "can't mkstemp logfile %s", tempfile); 2561114137Sgad 2562114137Sgad /* 2563114137Sgad * Add status message to what will become the new log file. 2564114137Sgad */ 2565114137Sgad if (!(ent->flags & CE_BINARY)) { 2566114137Sgad if (log_trim(tempfile, ent)) 2567114137Sgad err(1, "can't add status message to log"); 2568114137Sgad } 2569114137Sgad } 2570114137Sgad 2571114137Sgad /* Change the owner/group, if we are supposed to */ 2572114137Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 2573114137Sgad if (noaction) 2574114137Sgad printf("\tchown %u:%u %s\n", ent->uid, ent->gid, 2575114137Sgad tempfile); 2576114137Sgad else { 2577114137Sgad failed = fchown(fd, ent->uid, ent->gid); 2578114137Sgad if (failed) 2579114137Sgad err(1, "can't fchown temp file %s", tempfile); 2580114137Sgad } 2581114137Sgad } 2582114137Sgad 2583130043Sgad /* Turn on NODUMP if it was requested in the config-file. */ 2584130043Sgad if (ent->flags & CE_NODUMP) { 2585130043Sgad if (noaction) 2586130043Sgad printf("\tchflags nodump %s\n", tempfile); 2587130043Sgad else { 2588130043Sgad failed = fchflags(fd, UF_NODUMP); 2589130043Sgad if (failed) { 2590130043Sgad warn("log_trim: fchflags(NODUMP)"); 2591130043Sgad } 2592130043Sgad } 2593130043Sgad } 2594130043Sgad 2595114137Sgad /* 2596114137Sgad * Note that if the real logfile still exists, and if the call 2597114137Sgad * to rename() fails, then "neither the old file nor the new 2598114137Sgad * file shall be changed or created" (to quote the standard). 2599114137Sgad * If the call succeeds, then the file will be replaced without 2600114137Sgad * any window where some other process might find that the file 2601114137Sgad * did not exist. 2602114137Sgad * XXX - ? It may be that for some error conditions, we could 2603114137Sgad * retry by first removing the realfile and then renaming. 2604114137Sgad */ 2605114137Sgad if (noaction) { 2606114137Sgad printf("\tchmod %o %s\n", ent->permissions, tempfile); 2607114137Sgad printf("\tmv %s %s\n", tempfile, realfile); 2608114137Sgad } else { 2609114137Sgad failed = fchmod(fd, ent->permissions); 2610114137Sgad if (failed) 2611114137Sgad err(1, "can't fchmod temp file '%s'", tempfile); 2612114137Sgad failed = rename(tempfile, realfile); 2613114137Sgad if (failed) 2614114137Sgad err(1, "can't mv %s to %s", tempfile, realfile); 2615114137Sgad } 2616114137Sgad 2617114137Sgad if (fd >= 0) 2618114137Sgad close(fd); 2619114137Sgad} 2620130165Sgad 2621154566Sgad/* 2622154566Sgad * Change the attributes of a given filename to what was specified in 2623154566Sgad * the newsyslog.conf entry. This routine is only called for files 2624154566Sgad * that newsyslog expects that it has created, and thus it is a fatal 2625154566Sgad * error if this routine finds that the file does not exist. 2626154566Sgad */ 2627130165Sgadstatic void 2628130165Sgadchange_attrs(const char *fname, const struct conf_entry *ent) 2629130165Sgad{ 2630130165Sgad int failed; 2631130165Sgad 2632130165Sgad if (noaction) { 2633130165Sgad printf("\tchmod %o %s\n", ent->permissions, fname); 2634130165Sgad 2635130165Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 2636130165Sgad printf("\tchown %u:%u %s\n", 2637130165Sgad ent->uid, ent->gid, fname); 2638130165Sgad 2639130165Sgad if (ent->flags & CE_NODUMP) 2640130165Sgad printf("\tchflags nodump %s\n", fname); 2641130165Sgad return; 2642130165Sgad } 2643130165Sgad 2644130165Sgad failed = chmod(fname, ent->permissions); 2645154566Sgad if (failed) { 2646154566Sgad if (errno != EPERM) 2647154566Sgad err(1, "chmod(%s) in change_attrs", fname); 2648154566Sgad warn("change_attrs couldn't chmod(%s)", fname); 2649154566Sgad } 2650130165Sgad 2651130165Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 2652130165Sgad failed = chown(fname, ent->uid, ent->gid); 2653130165Sgad if (failed) 2654130165Sgad warn("can't chown %s", fname); 2655130165Sgad } 2656130165Sgad 2657130165Sgad if (ent->flags & CE_NODUMP) { 2658130165Sgad failed = chflags(fname, UF_NODUMP); 2659130165Sgad if (failed) 2660130165Sgad warn("can't chflags %s NODUMP", fname); 2661130165Sgad } 2662130165Sgad} 2663