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 148241777Sedstatic const 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" */ 182245963Smarkj 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); 209241777Sedstatic SLIST_HEAD(swlisthead, sigwork_entry) swhead = 210241777Sed SLIST_HEAD_INITIALIZER(swhead); 211241777Sedstatic SLIST_HEAD(zwlisthead, zipwork_entry) zwhead = 212241777Sed SLIST_HEAD_INITIALIZER(zwhead); 213208649SgordonSTAILQ_HEAD(ilist, include_entry); 214130167Sgad 215120361Sgadint dbg_at_times; /* -D Show details of 'trim_at' code */ 216120361Sgad 217241777Sedstatic int archtodir = 0; /* Archive old logfiles to other directory */ 218241777Sedstatic int createlogs; /* Create (non-GLOB) logfiles which do not */ 219114137Sgad /* already exist. 1=='for entries with */ 220114137Sgad /* C flag', 2=='for all entries'. */ 22159003Shmint verbose = 0; /* Print out what's going on */ 222241777Sedstatic int needroot = 1; /* Root privs are necessary */ 22359003Shmint noaction = 0; /* Don't do anything, just show it */ 224241777Sedstatic int norotate = 0; /* Don't rotate */ 225241777Sedstatic int nosignal; /* Do not send any signals */ 226241777Sedstatic int enforcepid = 0; /* If PID file does not exist or empty, do nothing */ 227241777Sedstatic int force = 0; /* Force the trim no matter what */ 228241777Sedstatic int rotatereq = 0; /* -R = Always rotate the file(s) as given */ 229111772Sgad /* on the command (this also requires */ 230111772Sgad /* that a list of files *are* given on */ 231111772Sgad /* the run command). */ 232241777Sedstatic char *requestor; /* The name given on a -R request */ 233241777Sedstatic char *timefnamefmt = NULL;/* Use time based filenames instead of .0 */ 234241777Sedstatic char *archdirname; /* Directory path to old logfiles archive */ 235241777Sedstatic char *destdir = NULL; /* Directory to treat at root for logs */ 236241777Sedstatic const char *conf; /* Configuration file to use */ 23759003Shm 238120361Sgadstruct ptime_data *dbg_timenow; /* A "timenow" value set via -D option */ 239241777Sedstatic struct ptime_data *timenow; /* The time to use for checking at-fields */ 240120361Sgad 241159998Sgad#define DAYTIME_LEN 16 242241777Sedstatic char daytime[DAYTIME_LEN];/* The current time in human readable form, 243241777Sed * used for rotation-tracking messages. */ 244241777Sedstatic char hostname[MAXHOSTNAMELEN]; /* hostname */ 24513244Sgraichen 246241777Sedstatic const char *path_syslogpid = _PATH_SYSLOGPID; 247210407Sbrian 248208648Sgordonstatic struct cflist *get_worklist(char **files); 249208648Sgordonstatic void parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p, 250208649Sgordon struct conf_entry *defconf_p, struct ilist *inclist); 251208649Sgordonstatic void add_to_queue(const char *fname, struct ilist *inclist); 25216240Salexstatic char *sob(char *p); 25316240Salexstatic char *son(char *p); 254119102Sgadstatic int isnumberstr(const char *); 255208649Sgordonstatic int isglobstr(const char *); 25659003Shmstatic char *missing_field(char *p, char *errline); 257130165Sgadstatic void change_attrs(const char *, const struct conf_entry *); 258218127Smmstatic const char *get_logfile_suffix(const char *logfile); 259130165Sgadstatic fk_entry do_entry(struct conf_entry *); 260130165Sgadstatic fk_entry do_rotate(const struct conf_entry *); 261130167Sgadstatic void do_sigwork(struct sigwork_entry *); 262130167Sgadstatic void do_zipwork(struct zipwork_entry *); 263130167Sgadstatic struct sigwork_entry * 264130167Sgad save_sigwork(const struct conf_entry *); 265130167Sgadstatic struct zipwork_entry * 266130167Sgad save_zipwork(const struct conf_entry *, const struct 267130167Sgad sigwork_entry *, int, const char *); 268130167Sgadstatic void set_swpid(struct sigwork_entry *, const struct conf_entry *); 269130165Sgadstatic int sizefile(const char *); 270208648Sgordonstatic void expand_globs(struct cflist *work_p, struct cflist *glob_p); 271208648Sgordonstatic void free_clist(struct cflist *list); 272111388Sgadstatic void free_entry(struct conf_entry *ent); 273111388Sgadstatic struct conf_entry *init_entry(const char *fname, 274111388Sgad struct conf_entry *src_entry); 275111781Sgadstatic void parse_args(int argc, char **argv); 276119904Sgadstatic int parse_doption(const char *doption); 27780640Sobrienstatic void usage(void); 278119926Sgadstatic int log_trim(const char *logname, const struct conf_entry *log_ent); 279248776Smarkjstatic int age_old_log(const char *file); 280130038Sgadstatic void savelog(char *from, char *to); 281114137Sgadstatic void createdir(const struct conf_entry *ent, char *dirpart); 282114137Sgadstatic void createlog(const struct conf_entry *ent); 28313244Sgraichen 284111768Sgad/* 285129975Sgad * All the following take a parameter of 'int', but expect values in the 286129975Sgad * range of unsigned char. Define wrappers which take values of type 'char', 287129975Sgad * whether signed or unsigned, and ensure they end up in the right range. 288111768Sgad */ 289129975Sgad#define isdigitch(Anychar) isdigit((u_char)(Anychar)) 290129975Sgad#define isprintch(Anychar) isprint((u_char)(Anychar)) 291129975Sgad#define isspacech(Anychar) isspace((u_char)(Anychar)) 292129975Sgad#define tolowerch(Anychar) tolower((u_char)(Anychar)) 293111768Sgad 29459004Shmint 29559004Shmmain(int argc, char **argv) 29613244Sgraichen{ 297208648Sgordon struct cflist *worklist; 298208648Sgordon struct conf_entry *p; 299130167Sgad struct sigwork_entry *stmp; 300130167Sgad struct zipwork_entry *ztmp; 30125443Sache 302130167Sgad SLIST_INIT(&swhead); 303130167Sgad SLIST_INIT(&zwhead); 304130167Sgad 305111781Sgad parse_args(argc, argv); 306111773Sgad argc -= optind; 307111773Sgad argv += optind; 308111773Sgad 30959003Shm if (needroot && getuid() && geteuid()) 31059003Shm errx(1, "must have root privs"); 311208648Sgordon worklist = get_worklist(argv); 31259003Shm 313130165Sgad /* 314130165Sgad * Rotate all the files which need to be rotated. Note that 315130165Sgad * some users have *hundreds* of entries in newsyslog.conf! 316130165Sgad */ 317208648Sgordon while (!STAILQ_EMPTY(worklist)) { 318208648Sgordon p = STAILQ_FIRST(worklist); 319208648Sgordon STAILQ_REMOVE_HEAD(worklist, cf_nextp); 320208648Sgordon if (do_entry(p) == FREE_ENT) 321208648Sgordon free_entry(p); 32259003Shm } 323130165Sgad 324130167Sgad /* 325130167Sgad * Send signals to any processes which need a signal to tell 326130167Sgad * them to close and re-open the log file(s) we have rotated. 327130167Sgad * Note that zipwork_entries include pointers to these 328130167Sgad * sigwork_entry's, so we can not free the entries here. 329130167Sgad */ 330130167Sgad if (!SLIST_EMPTY(&swhead)) { 331130204Sgad if (noaction || verbose) 332130167Sgad printf("Signal all daemon process(es)...\n"); 333130167Sgad SLIST_FOREACH(stmp, &swhead, sw_nextp) 334130167Sgad do_sigwork(stmp); 335130204Sgad if (noaction) 336130204Sgad printf("\tsleep 10\n"); 337130204Sgad else { 338130204Sgad if (verbose) 339130204Sgad printf("Pause 10 seconds to allow daemon(s)" 340130204Sgad " to close log file(s)\n"); 341130204Sgad sleep(10); 342130204Sgad } 343130167Sgad } 344130167Sgad /* 345130167Sgad * Compress all files that we're expected to compress, now 346130167Sgad * that all processes should have closed the files which 347130167Sgad * have been rotated. 348130167Sgad */ 349130167Sgad if (!SLIST_EMPTY(&zwhead)) { 350130204Sgad if (noaction || verbose) 351130167Sgad printf("Compress all rotated log file(s)...\n"); 352130167Sgad while (!SLIST_EMPTY(&zwhead)) { 353130167Sgad ztmp = SLIST_FIRST(&zwhead); 354130167Sgad do_zipwork(ztmp); 355130167Sgad SLIST_REMOVE_HEAD(&zwhead, zw_nextp); 356130167Sgad free(ztmp); 357130167Sgad } 358130167Sgad } 359130167Sgad /* Now free all the sigwork entries. */ 360130167Sgad while (!SLIST_EMPTY(&swhead)) { 361130167Sgad stmp = SLIST_FIRST(&swhead); 362130167Sgad SLIST_REMOVE_HEAD(&swhead, sw_nextp); 363130167Sgad free(stmp); 364130167Sgad } 365130167Sgad 36695999Smaxim while (wait(NULL) > 0 || errno == EINTR) 36795999Smaxim ; 36859003Shm return (0); 36913244Sgraichen} 37013244Sgraichen 371111388Sgadstatic struct conf_entry * 372111388Sgadinit_entry(const char *fname, struct conf_entry *src_entry) 373111388Sgad{ 374111388Sgad struct conf_entry *tempwork; 375111388Sgad 376111388Sgad if (verbose > 4) 377111388Sgad printf("\t--> [creating entry for %s]\n", fname); 378111388Sgad 379111388Sgad tempwork = malloc(sizeof(struct conf_entry)); 380111388Sgad if (tempwork == NULL) 381111388Sgad err(1, "malloc of conf_entry for %s", fname); 382111388Sgad 383136174Sbrooks if (destdir == NULL || fname[0] != '/') 384136127Sbrooks tempwork->log = strdup(fname); 385136127Sbrooks else 386136127Sbrooks asprintf(&tempwork->log, "%s%s", destdir, fname); 387111388Sgad if (tempwork->log == NULL) 388111388Sgad err(1, "strdup for %s", fname); 389111388Sgad 390111388Sgad if (src_entry != NULL) { 391221873Ssobomax tempwork->pid_cmd_file = NULL; 392221873Ssobomax if (src_entry->pid_cmd_file) 393221873Ssobomax tempwork->pid_cmd_file = strdup(src_entry->pid_cmd_file); 394111772Sgad tempwork->r_reason = NULL; 395114137Sgad tempwork->firstcreate = 0; 396111772Sgad tempwork->rotate = 0; 397130165Sgad tempwork->fsize = -1; 398111388Sgad tempwork->uid = src_entry->uid; 399111388Sgad tempwork->gid = src_entry->gid; 400111388Sgad tempwork->numlogs = src_entry->numlogs; 401130165Sgad tempwork->trsize = src_entry->trsize; 402111388Sgad tempwork->hours = src_entry->hours; 403120361Sgad tempwork->trim_at = NULL; 404120361Sgad if (src_entry->trim_at != NULL) 405120361Sgad tempwork->trim_at = ptime_init(src_entry->trim_at); 406111388Sgad tempwork->permissions = src_entry->permissions; 407111388Sgad tempwork->flags = src_entry->flags; 408218127Smm tempwork->compress = src_entry->compress; 409111388Sgad tempwork->sig = src_entry->sig; 410111388Sgad tempwork->def_cfg = src_entry->def_cfg; 411111388Sgad } else { 412111388Sgad /* Initialize as a "do-nothing" entry */ 413221873Ssobomax tempwork->pid_cmd_file = NULL; 414111772Sgad tempwork->r_reason = NULL; 415114137Sgad tempwork->firstcreate = 0; 416111772Sgad tempwork->rotate = 0; 417130165Sgad tempwork->fsize = -1; 418111779Sgad tempwork->uid = (uid_t)-1; 419111779Sgad tempwork->gid = (gid_t)-1; 420111388Sgad tempwork->numlogs = 1; 421130165Sgad tempwork->trsize = -1; 422111388Sgad tempwork->hours = -1; 423120361Sgad tempwork->trim_at = NULL; 424111388Sgad tempwork->permissions = 0; 425111388Sgad tempwork->flags = 0; 426218127Smm tempwork->compress = COMPRESS_NONE; 427111388Sgad tempwork->sig = SIGHUP; 428111388Sgad tempwork->def_cfg = 0; 429111388Sgad } 430111388Sgad 431111388Sgad return (tempwork); 432111388Sgad} 433111388Sgad 43459004Shmstatic void 435111388Sgadfree_entry(struct conf_entry *ent) 436111388Sgad{ 437111388Sgad 438111388Sgad if (ent == NULL) 439111388Sgad return; 440111388Sgad 441111388Sgad if (ent->log != NULL) { 442111388Sgad if (verbose > 4) 443111388Sgad printf("\t--> [freeing entry for %s]\n", ent->log); 444111388Sgad free(ent->log); 445111388Sgad ent->log = NULL; 446111388Sgad } 447111388Sgad 448221873Ssobomax if (ent->pid_cmd_file != NULL) { 449221873Ssobomax free(ent->pid_cmd_file); 450221873Ssobomax ent->pid_cmd_file = NULL; 451111388Sgad } 452111388Sgad 453111772Sgad if (ent->r_reason != NULL) { 454111772Sgad free(ent->r_reason); 455111772Sgad ent->r_reason = NULL; 456111772Sgad } 457111772Sgad 458120361Sgad if (ent->trim_at != NULL) { 459120361Sgad ptime_free(ent->trim_at); 460120361Sgad ent->trim_at = NULL; 461120361Sgad } 462120361Sgad 463111388Sgad free(ent); 464111388Sgad} 465111388Sgad 466111388Sgadstatic void 467208648Sgordonfree_clist(struct cflist *list) 468112020Sgad{ 469208648Sgordon struct conf_entry *ent; 470112020Sgad 471208648Sgordon while (!STAILQ_EMPTY(list)) { 472208648Sgordon ent = STAILQ_FIRST(list); 473208648Sgordon STAILQ_REMOVE_HEAD(list, cf_nextp); 474112020Sgad free_entry(ent); 475112020Sgad } 476208648Sgordon 477208648Sgordon free(list); 478208648Sgordon list = NULL; 479112020Sgad} 480112020Sgad 481130165Sgadstatic fk_entry 48259004Shmdo_entry(struct conf_entry * ent) 48313244Sgraichen{ 484130045Sgad#define REASON_MAX 80 485130165Sgad int modtime; 486130165Sgad fk_entry free_or_keep; 487120361Sgad double diffsecs; 488111772Sgad char temp_reason[REASON_MAX]; 489233257Sglebius int oversized; 49059003Shm 491130165Sgad free_or_keep = FREE_ENT; 492218127Smm if (verbose) 493218127Smm printf("%s <%d%s>: ", ent->log, ent->numlogs, 494218127Smm compress_type[ent->compress].flag); 495130165Sgad ent->fsize = sizefile(ent->log); 496233257Sglebius oversized = ((ent->trsize > 0) && (ent->fsize >= ent->trsize)); 49759003Shm modtime = age_old_log(ent->log); 498111772Sgad ent->rotate = 0; 499114137Sgad ent->firstcreate = 0; 500130165Sgad if (ent->fsize < 0) { 501114137Sgad /* 502114137Sgad * If either the C flag or the -C option was specified, 503114137Sgad * and if we won't be creating the file, then have the 504114137Sgad * verbose message include a hint as to why the file 505114137Sgad * will not be created. 506114137Sgad */ 507114137Sgad temp_reason[0] = '\0'; 508114137Sgad if (createlogs > 1) 509114137Sgad ent->firstcreate = 1; 510114137Sgad else if ((ent->flags & CE_CREATE) && createlogs) 511114137Sgad ent->firstcreate = 1; 512114137Sgad else if (ent->flags & CE_CREATE) 513159998Sgad strlcpy(temp_reason, " (no -C option)", REASON_MAX); 514114137Sgad else if (createlogs) 515159998Sgad strlcpy(temp_reason, " (no C flag)", REASON_MAX); 516114137Sgad 517114137Sgad if (ent->firstcreate) { 518114137Sgad if (verbose) 519114137Sgad printf("does not exist -> will create.\n"); 520114137Sgad createlog(ent); 521114137Sgad } else if (verbose) { 522114137Sgad printf("does not exist, skipped%s.\n", temp_reason); 523114137Sgad } 52459003Shm } else { 525233257Sglebius if (ent->flags & CE_TRIMAT && !force && !rotatereq && 526233257Sglebius !oversized) { 527120361Sgad diffsecs = ptimeget_diff(timenow, ent->trim_at); 528120361Sgad if (diffsecs < 0.0) { 529120361Sgad /* trim_at is some time in the future. */ 530120361Sgad if (verbose) { 531120361Sgad ptime_adjust4dst(ent->trim_at, 532120361Sgad timenow); 53343071Swollman printf("--> will trim at %s", 534120361Sgad ptimeget_ctime(ent->trim_at)); 535120361Sgad } 536130165Sgad return (free_or_keep); 537120361Sgad } else if (diffsecs >= 3600.0) { 538120361Sgad /* 539120361Sgad * trim_at is more than an hour in the past, 540120361Sgad * so find the next valid trim_at time, and 541120361Sgad * tell the user what that will be. 542120361Sgad */ 543120361Sgad if (verbose && dbg_at_times) 544120361Sgad printf("\n\t--> prev trim at %s\t", 545120361Sgad ptimeget_ctime(ent->trim_at)); 546120361Sgad if (verbose) { 547120361Sgad ptimeset_nxtime(ent->trim_at); 548120361Sgad printf("--> will trim at %s", 549120361Sgad ptimeget_ctime(ent->trim_at)); 550120361Sgad } 551130165Sgad return (free_or_keep); 552120361Sgad } else if (verbose && noaction && dbg_at_times) { 553120361Sgad /* 554120361Sgad * If we are just debugging at-times, then 555120361Sgad * a detailed message is helpful. Also 556120361Sgad * skip "doing" any commands, since they 557120361Sgad * would all be turned off by no-action. 558120361Sgad */ 559120361Sgad printf("\n\t--> timematch at %s", 560120361Sgad ptimeget_ctime(ent->trim_at)); 561130165Sgad return (free_or_keep); 56243071Swollman } else if (verbose && ent->hours <= 0) { 56343071Swollman printf("--> time is up\n"); 56443071Swollman } 56543071Swollman } 566130165Sgad if (verbose && (ent->trsize > 0)) 567130165Sgad printf("size (Kb): %d [%d] ", ent->fsize, ent->trsize); 56859003Shm if (verbose && (ent->hours > 0)) 56959003Shm printf(" age (hr): %d [%d] ", modtime, ent->hours); 570111772Sgad 571111772Sgad /* 572111772Sgad * Figure out if this logfile needs to be rotated. 573111772Sgad */ 574111772Sgad temp_reason[0] = '\0'; 575111772Sgad if (rotatereq) { 576111772Sgad ent->rotate = 1; 577111772Sgad snprintf(temp_reason, REASON_MAX, " due to -R from %s", 578111772Sgad requestor); 579111772Sgad } else if (force) { 580111772Sgad ent->rotate = 1; 581111772Sgad snprintf(temp_reason, REASON_MAX, " due to -F request"); 582233257Sglebius } else if (oversized) { 583111772Sgad ent->rotate = 1; 584111772Sgad snprintf(temp_reason, REASON_MAX, " due to size>%dK", 585130165Sgad ent->trsize); 586111772Sgad } else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) { 587111772Sgad ent->rotate = 1; 588111772Sgad } else if ((ent->hours > 0) && ((modtime >= ent->hours) || 589111772Sgad (modtime < 0))) { 590111772Sgad ent->rotate = 1; 591111772Sgad } 592111772Sgad 593111772Sgad /* 594111772Sgad * If the file needs to be rotated, then rotate it. 595111772Sgad */ 596143106Sbrooks if (ent->rotate && !norotate) { 597111772Sgad if (temp_reason[0] != '\0') 598111772Sgad ent->r_reason = strdup(temp_reason); 59959003Shm if (verbose) 60059003Shm printf("--> trimming log....\n"); 601218127Smm if (noaction && !verbose) 602218127Smm printf("%s <%d%s>: trimming\n", ent->log, 603218127Smm ent->numlogs, 604218127Smm compress_type[ent->compress].flag); 605130165Sgad free_or_keep = do_rotate(ent); 60659003Shm } else { 60759003Shm if (verbose) 60859003Shm printf("--> skipping\n"); 60959003Shm } 61059003Shm } 611130165Sgad return (free_or_keep); 612111772Sgad#undef REASON_MAX 61313244Sgraichen} 61413244Sgraichen 61559004Shmstatic void 616111781Sgadparse_args(int argc, char **argv) 61713244Sgraichen{ 618111781Sgad int ch; 61959003Shm char *p; 62013244Sgraichen 621120361Sgad timenow = ptime_init(NULL); 622120361Sgad ptimeset_time(timenow, time(NULL)); 623159998Sgad strlcpy(daytime, ptimeget_ctime(timenow) + 4, DAYTIME_LEN); 62413244Sgraichen 62559003Shm /* Let's get our hostname */ 626111781Sgad (void)gethostname(hostname, sizeof(hostname)); 62713244Sgraichen 62813244Sgraichen /* Truncate domain */ 629111781Sgad if ((p = strchr(hostname, '.')) != NULL) 63013244Sgraichen *p = '\0'; 631111768Sgad 632111768Sgad /* Parse command line options. */ 633216832Sbrian while ((ch = getopt(argc, argv, "a:d:f:nrst:vCD:FNPR:S:")) != -1) 634111781Sgad switch (ch) { 63559004Shm case 'a': 63659004Shm archtodir++; 63759004Shm archdirname = optarg; 63859004Shm break; 639136127Sbrooks case 'd': 640136127Sbrooks destdir = optarg; 641136127Sbrooks break; 642111768Sgad case 'f': 643111768Sgad conf = optarg; 644111768Sgad break; 645111768Sgad case 'n': 646111768Sgad noaction++; 647244996Smarkj /* FALLTHROUGH */ 64859003Shm case 'r': 64959003Shm needroot = 0; 65059003Shm break; 651111768Sgad case 's': 652111768Sgad nosignal = 1; 653111768Sgad break; 654210372Ssimon case 't': 655210372Ssimon if (optarg[0] == '\0' || 656210372Ssimon strcmp(optarg, "DEFAULT") == 0) 657210372Ssimon timefnamefmt = strdup(DEFAULT_TIMEFNAME_FMT); 658210372Ssimon else 659210372Ssimon timefnamefmt = strdup(optarg); 660210372Ssimon break; 66159003Shm case 'v': 66259003Shm verbose++; 66359003Shm break; 664114137Sgad case 'C': 665114137Sgad /* Useful for things like rc.diskless... */ 666114137Sgad createlogs++; 667114137Sgad break; 668119904Sgad case 'D': 669119904Sgad /* 670119904Sgad * Set some debugging option. The specific option 671119904Sgad * depends on the value of optarg. These options 672119904Sgad * may come and go without notice or documentation. 673119904Sgad */ 674119904Sgad if (parse_doption(optarg)) 675119904Sgad break; 676119904Sgad usage(); 677119904Sgad /* NOTREACHED */ 67834584Spst case 'F': 67934584Spst force++; 68034584Spst break; 681143106Sbrooks case 'N': 682143106Sbrooks norotate++; 683143106Sbrooks break; 684202668Sdelphij case 'P': 685202668Sdelphij enforcepid++; 686202668Sdelphij break; 687111772Sgad case 'R': 688111772Sgad rotatereq++; 689111772Sgad requestor = strdup(optarg); 690111772Sgad break; 691210407Sbrian case 'S': 692210407Sbrian path_syslogpid = optarg; 693210407Sbrian break; 694111781Sgad case 'm': /* Used by OpenBSD for "monitor mode" */ 69559003Shm default: 69659003Shm usage(); 697111768Sgad /* NOTREACHED */ 69859003Shm } 699111772Sgad 700143106Sbrooks if (force && norotate) { 701143106Sbrooks warnx("Only one of -F and -N may be specified."); 702143106Sbrooks usage(); 703143106Sbrooks /* NOTREACHED */ 704143106Sbrooks } 705143106Sbrooks 706111772Sgad if (rotatereq) { 707111772Sgad if (optind == argc) { 708111772Sgad warnx("At least one filename must be given when -R is specified."); 709111772Sgad usage(); 710111772Sgad /* NOTREACHED */ 711111772Sgad } 712111772Sgad /* Make sure "requestor" value is safe for a syslog message. */ 713111772Sgad for (p = requestor; *p != '\0'; p++) { 714111772Sgad if (!isprintch(*p) && (*p != '\t')) 715111772Sgad *p = '.'; 716111772Sgad } 717111772Sgad } 718119904Sgad 719119904Sgad if (dbg_timenow) { 720119904Sgad /* 721119904Sgad * Note that the 'daytime' variable is not changed. 722119904Sgad * That is only used in messages that track when a 723119904Sgad * logfile is rotated, and if a file *is* rotated, 724119904Sgad * then it will still rotated at the "real now" time. 725119904Sgad */ 726120361Sgad ptime_free(timenow); 727119904Sgad timenow = dbg_timenow; 728119904Sgad fprintf(stderr, "Debug: Running as if TimeNow is %s", 729120361Sgad ptimeget_ctime(dbg_timenow)); 730119904Sgad } 731119904Sgad 73243071Swollman} 73313244Sgraichen 734120361Sgad/* 735120361Sgad * These debugging options are mainly meant for developer use, such 736120361Sgad * as writing regression-tests. They would not be needed by users 737120361Sgad * during normal operation of newsyslog... 738120361Sgad */ 739119904Sgadstatic int 740119904Sgadparse_doption(const char *doption) 741119904Sgad{ 742119904Sgad const char TN[] = "TN="; 743120361Sgad int res; 744119904Sgad 745119904Sgad if (strncmp(doption, TN, sizeof(TN) - 1) == 0) { 746119904Sgad /* 747120361Sgad * The "TimeNow" debugging option. This might be off 748120361Sgad * by an hour when crossing a timezone change. 749119904Sgad */ 750120361Sgad dbg_timenow = ptime_init(NULL); 751120361Sgad res = ptime_relparse(dbg_timenow, PTM_PARSE_ISO8601, 752120361Sgad time(NULL), doption + sizeof(TN) - 1); 753120361Sgad if (res == -2) { 754120361Sgad warnx("Non-existent time specified on -D %s", doption); 755120361Sgad return (0); /* failure */ 756120361Sgad } else if (res < 0) { 757119904Sgad warnx("Malformed time given on -D %s", doption); 758119904Sgad return (0); /* failure */ 759119904Sgad } 760119904Sgad return (1); /* successfully parsed */ 761119904Sgad 762119904Sgad } 763119904Sgad 764120361Sgad if (strcmp(doption, "ats") == 0) { 765120361Sgad dbg_at_times++; 766120361Sgad return (1); /* successfully parsed */ 767120361Sgad } 768120361Sgad 769159968Sgad /* XXX - This check could probably be dropped. */ 770159968Sgad if ((strcmp(doption, "neworder") == 0) || (strcmp(doption, "oldorder") 771159968Sgad == 0)) { 772159968Sgad warnx("NOTE: newsyslog always uses 'neworder'."); 773130167Sgad return (1); /* successfully parsed */ 774130167Sgad } 775130167Sgad 776130165Sgad warnx("Unknown -D (debug) option: '%s'", doption); 777119904Sgad return (0); /* failure */ 778119904Sgad} 779119904Sgad 78059004Shmstatic void 78159004Shmusage(void) 78213244Sgraichen{ 78380646Sobrien 78480646Sobrien fprintf(stderr, 785219434Sru "usage: newsyslog [-CFNPnrsv] [-a directory] [-d directory] [-f config_file]\n" 786219434Sru " [-S pidfile] [-t timefmt] [[-R tagname] file ...]\n"); 78759003Shm exit(1); 78813244Sgraichen} 78913244Sgraichen 79059004Shm/* 791111773Sgad * Parse a configuration file and return a linked list of all the logs 792111773Sgad * which should be processed. 79313244Sgraichen */ 794208648Sgordonstatic struct cflist * 795111773Sgadget_worklist(char **files) 79613244Sgraichen{ 79759003Shm FILE *f; 798111773Sgad char **given; 799208649Sgordon struct cflist *cmdlist, *filelist, *globlist; 800208648Sgordon struct conf_entry *defconf, *dupent, *ent; 801208649Sgordon struct ilist inclist; 802208649Sgordon struct include_entry *inc; 803112020Sgad int gmatch, fnres; 804111773Sgad 805208648Sgordon defconf = NULL; 806208649Sgordon STAILQ_INIT(&inclist); 807111773Sgad 808208648Sgordon filelist = malloc(sizeof(struct cflist)); 809208648Sgordon if (filelist == NULL) 810208648Sgordon err(1, "malloc of filelist"); 811208648Sgordon STAILQ_INIT(filelist); 812208648Sgordon globlist = malloc(sizeof(struct cflist)); 813208648Sgordon if (globlist == NULL) 814208648Sgordon err(1, "malloc of globlist"); 815208648Sgordon STAILQ_INIT(globlist); 816208648Sgordon 817208649Sgordon inc = malloc(sizeof(struct include_entry)); 818208649Sgordon if (inc == NULL) 819208649Sgordon err(1, "malloc of inc"); 820208649Sgordon inc->file = conf; 821208649Sgordon if (inc->file == NULL) 822208649Sgordon inc->file = _PATH_CONF; 823208649Sgordon STAILQ_INSERT_TAIL(&inclist, inc, inc_nextp); 824111773Sgad 825208649Sgordon STAILQ_FOREACH(inc, &inclist, inc_nextp) { 826208649Sgordon if (strcmp(inc->file, "-") != 0) 827208649Sgordon f = fopen(inc->file, "r"); 828208649Sgordon else { 829208649Sgordon f = stdin; 830208649Sgordon inc->file = "<stdin>"; 831208649Sgordon } 832208649Sgordon if (!f) 833208649Sgordon err(1, "%s", inc->file); 834208649Sgordon 835208649Sgordon if (verbose) 836208649Sgordon printf("Processing %s\n", inc->file); 837208649Sgordon parse_file(f, filelist, globlist, defconf, &inclist); 838208649Sgordon (void) fclose(f); 839111773Sgad } 840111773Sgad 841111773Sgad /* 842111773Sgad * All config-file information has been read in and turned into 843208648Sgordon * a filelist and a globlist. If there were no specific files 844112020Sgad * given on the run command, then the only thing left to do is to 845112020Sgad * call a routine which finds all files matched by the globlist 846208648Sgordon * and adds them to the filelist. Then return the worklist. 847111773Sgad */ 848111773Sgad if (*files == NULL) { 849208648Sgordon expand_globs(filelist, globlist); 850208648Sgordon free_clist(globlist); 851111773Sgad if (defconf != NULL) 852111773Sgad free_entry(defconf); 853208648Sgordon return (filelist); 854111773Sgad /* NOTREACHED */ 855111773Sgad } 856111773Sgad 857111773Sgad /* 858111773Sgad * If newsyslog was given a specific list of files to process, 859111773Sgad * it may be that some of those files were not listed in any 860111773Sgad * config file. Those unlisted files should get the default 861111773Sgad * rotation action. First, create the default-rotation action 862111773Sgad * if none was found in a system config file. 863111773Sgad */ 864111773Sgad if (defconf == NULL) { 865111773Sgad defconf = init_entry(DEFAULT_MARKER, NULL); 866111773Sgad defconf->numlogs = 3; 867130165Sgad defconf->trsize = 50; 868111773Sgad defconf->permissions = S_IRUSR|S_IWUSR; 869111773Sgad } 870111773Sgad 871111773Sgad /* 872111773Sgad * If newsyslog was run with a list of specific filenames, 873111773Sgad * then create a new worklist which has only those files in 874111773Sgad * it, picking up the rotation-rules for those files from 875208648Sgordon * the original filelist. 876111773Sgad * 877111773Sgad * XXX - Note that this will copy multiple rules for a single 878111773Sgad * logfile, if multiple entries are an exact match for 879111773Sgad * that file. That matches the historic behavior, but do 880111773Sgad * we want to continue to allow it? If so, it should 881111773Sgad * probably be handled more intelligently. 882111773Sgad */ 883208648Sgordon cmdlist = malloc(sizeof(struct cflist)); 884208648Sgordon if (cmdlist == NULL) 885208648Sgordon err(1, "malloc of cmdlist"); 886208648Sgordon STAILQ_INIT(cmdlist); 887208648Sgordon 888111773Sgad for (given = files; *given; ++given) { 889111773Sgad /* 890111773Sgad * First try to find exact-matches for this given file. 891111773Sgad */ 892112020Sgad gmatch = 0; 893208648Sgordon STAILQ_FOREACH(ent, filelist, cf_nextp) { 894111773Sgad if (strcmp(ent->log, *given) == 0) { 895111773Sgad gmatch++; 896111773Sgad dupent = init_entry(*given, ent); 897208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 898111773Sgad } 899111773Sgad } 900111773Sgad if (gmatch) { 901111773Sgad if (verbose > 2) 902111773Sgad printf("\t+ Matched entry %s\n", *given); 903111773Sgad continue; 904111773Sgad } 905111773Sgad 906111773Sgad /* 907111773Sgad * There was no exact-match for this given file, so look 908111773Sgad * for a "glob" entry which does match. 909111773Sgad */ 910112020Sgad gmatch = 0; 911112020Sgad if (verbose > 2 && globlist != NULL) 912112020Sgad printf("\t+ Checking globs for %s\n", *given); 913208648Sgordon STAILQ_FOREACH(ent, globlist, cf_nextp) { 914112020Sgad fnres = fnmatch(ent->log, *given, FNM_PATHNAME); 915112020Sgad if (verbose > 2) 916112020Sgad printf("\t+ = %d for pattern %s\n", fnres, 917112020Sgad ent->log); 918112020Sgad if (fnres == 0) { 919111773Sgad gmatch++; 920111773Sgad dupent = init_entry(*given, ent); 921112020Sgad /* This new entry is not a glob! */ 922111773Sgad dupent->flags &= ~CE_GLOB; 923208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 924111773Sgad /* Only allow a match to one glob-entry */ 925111773Sgad break; 926111773Sgad } 927111773Sgad } 928111773Sgad if (gmatch) { 929111773Sgad if (verbose > 2) 930111773Sgad printf("\t+ Matched %s via %s\n", *given, 931111773Sgad ent->log); 932111773Sgad continue; 933111773Sgad } 934111773Sgad 935111773Sgad /* 936111773Sgad * This given file was not found in any config file, so 937111773Sgad * add a worklist item based on the default entry. 938111773Sgad */ 939111773Sgad if (verbose > 2) 940111773Sgad printf("\t+ No entry matched %s (will use %s)\n", 941111773Sgad *given, DEFAULT_MARKER); 942111773Sgad dupent = init_entry(*given, defconf); 943111773Sgad /* Mark that it was *not* found in a config file */ 944111773Sgad dupent->def_cfg = 1; 945208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 946111773Sgad } 947111773Sgad 948111773Sgad /* 949112020Sgad * Free all the entries in the original work list, the list of 950112020Sgad * glob entries, and the default entry. 951111773Sgad */ 952208648Sgordon free_clist(filelist); 953208648Sgordon free_clist(globlist); 954112020Sgad free_entry(defconf); 955111773Sgad 956112020Sgad /* And finally, return a worklist which matches the given files. */ 957208648Sgordon return (cmdlist); 958111773Sgad} 959111773Sgad 960111773Sgad/* 961112020Sgad * Expand the list of entries with filename patterns, and add all files 962112020Sgad * which match those glob-entries onto the worklist. 963112020Sgad */ 964112020Sgadstatic void 965208648Sgordonexpand_globs(struct cflist *work_p, struct cflist *glob_p) 966112020Sgad{ 967161412Sdelphij int gmatch, gres; 968161412Sdelphij size_t i; 969112020Sgad char *mfname; 970208648Sgordon struct conf_entry *dupent, *ent, *globent; 971112020Sgad glob_t pglob; 972112020Sgad struct stat st_fm; 973112020Sgad 974112020Sgad /* 975112020Sgad * The worklist contains all fully-specified (non-GLOB) names. 976112020Sgad * 977112020Sgad * Now expand the list of filename-pattern (GLOB) entries into 978112020Sgad * a second list, which (by definition) will only match files 979112020Sgad * that already exist. Do not add a glob-related entry for any 980112020Sgad * file which already exists in the fully-specified list. 981112020Sgad */ 982208648Sgordon STAILQ_FOREACH(globent, glob_p, cf_nextp) { 983112020Sgad gres = glob(globent->log, GLOB_NOCHECK, NULL, &pglob); 984112020Sgad if (gres != 0) { 985112020Sgad warn("cannot expand pattern (%d): %s", gres, 986112020Sgad globent->log); 987112020Sgad continue; 988112020Sgad } 989112020Sgad 990112020Sgad if (verbose > 2) 991112020Sgad printf("\t+ Expanding pattern %s\n", globent->log); 992112020Sgad for (i = 0; i < pglob.gl_matchc; i++) { 993112020Sgad mfname = pglob.gl_pathv[i]; 994112020Sgad 995112020Sgad /* See if this file already has a specific entry. */ 996112020Sgad gmatch = 0; 997208648Sgordon STAILQ_FOREACH(ent, work_p, cf_nextp) { 998112020Sgad if (strcmp(mfname, ent->log) == 0) { 999112020Sgad gmatch++; 1000112020Sgad break; 1001112020Sgad } 1002112020Sgad } 1003112020Sgad if (gmatch) 1004112020Sgad continue; 1005112020Sgad 1006112020Sgad /* Make sure the named matched is a file. */ 1007112020Sgad gres = lstat(mfname, &st_fm); 1008112020Sgad if (gres != 0) { 1009112020Sgad /* Error on a file that glob() matched?!? */ 1010112020Sgad warn("Skipping %s - lstat() error", mfname); 1011112020Sgad continue; 1012112020Sgad } 1013112020Sgad if (!S_ISREG(st_fm.st_mode)) { 1014112020Sgad /* We only rotate files! */ 1015112020Sgad if (verbose > 2) 1016112020Sgad printf("\t+ . skipping %s (!file)\n", 1017112020Sgad mfname); 1018112020Sgad continue; 1019112020Sgad } 1020112020Sgad 1021112020Sgad if (verbose > 2) 1022112020Sgad printf("\t+ . add file %s\n", mfname); 1023112020Sgad dupent = init_entry(mfname, globent); 1024112020Sgad /* This new entry is not a glob! */ 1025112020Sgad dupent->flags &= ~CE_GLOB; 1026208648Sgordon 1027208648Sgordon /* Add to the worklist. */ 1028208648Sgordon STAILQ_INSERT_TAIL(work_p, dupent, cf_nextp); 1029112020Sgad } 1030112020Sgad globfree(&pglob); 1031112020Sgad if (verbose > 2) 1032112020Sgad printf("\t+ Done with pattern %s\n", globent->log); 1033112020Sgad } 1034112020Sgad} 1035112020Sgad 1036112020Sgad/* 1037111773Sgad * Parse a configuration file and update a linked list of all the logs to 1038111773Sgad * process. 1039111773Sgad */ 1040111773Sgadstatic void 1041208648Sgordonparse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p, 1042208649Sgordon struct conf_entry *defconf_p, struct ilist *inclist) 1043111773Sgad{ 104459003Shm char line[BUFSIZ], *parse, *q; 1045107737Ssobomax char *cp, *errline, *group; 1046208648Sgordon struct conf_entry *working; 1047111781Sgad struct passwd *pwd; 104859003Shm struct group *grp; 1049208649Sgordon glob_t pglob; 1050120361Sgad int eol, ptm_opts, res, special; 1051208649Sgordon size_t i; 105213244Sgraichen 1053130165Sgad errline = NULL; 1054111773Sgad while (fgets(line, BUFSIZ, cf)) { 1055107737Ssobomax if ((line[0] == '\n') || (line[0] == '#') || 1056107737Ssobomax (strlen(line) == 0)) 105759003Shm continue; 1058130165Sgad if (errline != NULL) 1059130165Sgad free(errline); 106059003Shm errline = strdup(line); 1061107737Ssobomax for (cp = line + 1; *cp != '\0'; cp++) { 1062107737Ssobomax if (*cp != '#') 1063107737Ssobomax continue; 1064107737Ssobomax if (*(cp - 1) == '\\') { 1065107737Ssobomax strcpy(cp - 1, cp); 1066107737Ssobomax cp--; 1067107737Ssobomax continue; 1068107737Ssobomax } 1069107737Ssobomax *cp = '\0'; 1070107737Ssobomax break; 1071107737Ssobomax } 107260373Sdes 107360373Sdes q = parse = missing_field(sob(line), errline); 107460373Sdes parse = son(line); 107560373Sdes if (!*parse) 107680646Sobrien errx(1, "malformed line (missing fields):\n%s", 107780646Sobrien errline); 107860373Sdes *parse = '\0'; 107960373Sdes 1080130165Sgad /* 1081130165Sgad * Allow people to set debug options via the config file. 1082208648Sgordon * (NOTE: debug options are undocumented, and may disappear 1083130165Sgad * at any time, etc). 1084130165Sgad */ 1085130165Sgad if (strcasecmp(DEBUG_MARKER, q) == 0) { 1086252378Skientzle q = parse = missing_field(sob(parse + 1), errline); 1087130165Sgad parse = son(parse); 1088130165Sgad if (!*parse) 1089130165Sgad warnx("debug line specifies no option:\n%s", 1090130165Sgad errline); 1091130165Sgad else { 1092130165Sgad *parse = '\0'; 1093130165Sgad parse_doption(q); 1094130165Sgad } 1095130165Sgad continue; 1096208649Sgordon } else if (strcasecmp(INCLUDE_MARKER, q) == 0) { 1097208649Sgordon if (verbose) 1098208649Sgordon printf("Found: %s", errline); 1099252378Skientzle q = parse = missing_field(sob(parse + 1), errline); 1100208649Sgordon parse = son(parse); 1101208649Sgordon if (!*parse) { 1102208649Sgordon warnx("include line missing argument:\n%s", 1103208649Sgordon errline); 1104208649Sgordon continue; 1105208649Sgordon } 1106208649Sgordon 1107208649Sgordon *parse = '\0'; 1108208649Sgordon 1109208649Sgordon if (isglobstr(q)) { 1110208649Sgordon res = glob(q, GLOB_NOCHECK, NULL, &pglob); 1111208649Sgordon if (res != 0) { 1112208649Sgordon warn("cannot expand pattern (%d): %s", 1113208649Sgordon res, q); 1114208649Sgordon continue; 1115208649Sgordon } 1116208649Sgordon 1117208649Sgordon if (verbose > 2) 1118208649Sgordon printf("\t+ Expanding pattern %s\n", q); 1119208649Sgordon 1120208649Sgordon for (i = 0; i < pglob.gl_matchc; i++) 1121208649Sgordon add_to_queue(pglob.gl_pathv[i], 1122208649Sgordon inclist); 1123208649Sgordon globfree(&pglob); 1124208649Sgordon } else 1125208649Sgordon add_to_queue(q, inclist); 1126208649Sgordon continue; 1127130165Sgad } 1128130165Sgad 1129112020Sgad special = 0; 1130111388Sgad working = init_entry(q, NULL); 1131111388Sgad if (strcasecmp(DEFAULT_MARKER, q) == 0) { 1132112020Sgad special = 1; 1133208648Sgordon if (defconf_p != NULL) { 1134111388Sgad warnx("Ignoring duplicate entry for %s!", q); 1135111388Sgad free_entry(working); 1136111388Sgad continue; 1137111388Sgad } 1138208648Sgordon defconf_p = working; 113959003Shm } 114013244Sgraichen 1141252378Skientzle q = parse = missing_field(sob(parse + 1), errline); 114259003Shm parse = son(parse); 114325518Sbrian if (!*parse) 114480646Sobrien errx(1, "malformed line (missing fields):\n%s", 114580646Sobrien errline); 114659003Shm *parse = '\0'; 114759003Shm if ((group = strchr(q, ':')) != NULL || 114859003Shm (group = strrchr(q, '.')) != NULL) { 114959003Shm *group++ = '\0'; 115059003Shm if (*q) { 1151119102Sgad if (!(isnumberstr(q))) { 1152111781Sgad if ((pwd = getpwnam(q)) == NULL) 115359003Shm errx(1, 115480646Sobrien "error in config file; unknown user:\n%s", 115559003Shm errline); 1156111781Sgad working->uid = pwd->pw_uid; 115759003Shm } else 115859003Shm working->uid = atoi(q); 115959003Shm } else 1160111779Sgad working->uid = (uid_t)-1; 116113244Sgraichen 116259003Shm q = group; 116359003Shm if (*q) { 1164119102Sgad if (!(isnumberstr(q))) { 116559003Shm if ((grp = getgrnam(q)) == NULL) 116659003Shm errx(1, 116780646Sobrien "error in config file; unknown group:\n%s", 116859003Shm errline); 116959003Shm working->gid = grp->gr_gid; 117059003Shm } else 117159003Shm working->gid = atoi(q); 117259003Shm } else 1173111779Sgad working->gid = (gid_t)-1; 117413244Sgraichen 1175252378Skientzle q = parse = missing_field(sob(parse + 1), errline); 117659003Shm parse = son(parse); 117759003Shm if (!*parse) 117880646Sobrien errx(1, "malformed line (missing fields):\n%s", 117980646Sobrien errline); 118059003Shm *parse = '\0'; 1181111779Sgad } else { 1182111779Sgad working->uid = (uid_t)-1; 1183111779Sgad working->gid = (gid_t)-1; 1184111779Sgad } 118559003Shm 118659003Shm if (!sscanf(q, "%o", &working->permissions)) 118759003Shm errx(1, "error in config file; bad permissions:\n%s", 118859003Shm errline); 118959003Shm 1190252378Skientzle q = parse = missing_field(sob(parse + 1), errline); 119159003Shm parse = son(parse); 119225518Sbrian if (!*parse) 119380646Sobrien errx(1, "malformed line (missing fields):\n%s", 119480646Sobrien errline); 119559003Shm *parse = '\0'; 1196111400Sgad if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0) 1197111400Sgad errx(1, "error in config file; bad value for count of logs to save:\n%s", 119859003Shm errline); 119913244Sgraichen 1200252378Skientzle q = parse = missing_field(sob(parse + 1), errline); 120159003Shm parse = son(parse); 120225518Sbrian if (!*parse) 120380646Sobrien errx(1, "malformed line (missing fields):\n%s", 120480646Sobrien errline); 120559003Shm *parse = '\0'; 1206114762Sgad if (isdigitch(*q)) 1207130165Sgad working->trsize = atoi(q); 1208130165Sgad else if (strcmp(q, "*") == 0) 1209130165Sgad working->trsize = -1; 1210114762Sgad else { 1211114762Sgad warnx("Invalid value of '%s' for 'size' in line:\n%s", 1212114762Sgad q, errline); 1213130165Sgad working->trsize = -1; 1214114762Sgad } 121559003Shm 121659003Shm working->flags = 0; 1217218127Smm working->compress = COMPRESS_NONE; 1218252378Skientzle q = parse = missing_field(sob(parse + 1), errline); 121959003Shm parse = son(parse); 122025518Sbrian eol = !*parse; 122159003Shm *parse = '\0'; 122243071Swollman { 122359003Shm char *ep; 122459003Shm u_long ul; 122513244Sgraichen 122643071Swollman ul = strtoul(q, &ep, 10); 122743071Swollman if (ep == q) 122843071Swollman working->hours = 0; 122943071Swollman else if (*ep == '*') 123043071Swollman working->hours = -1; 123143071Swollman else if (ul > INT_MAX) 123243071Swollman errx(1, "interval is too large:\n%s", errline); 123343071Swollman else 123443071Swollman working->hours = ul; 123543071Swollman 1236120361Sgad if (*ep == '\0' || strcmp(ep, "*") == 0) 1237120361Sgad goto no_trimat; 1238120361Sgad if (*ep != '@' && *ep != '$') 123943071Swollman errx(1, "malformed interval/at:\n%s", errline); 1240120361Sgad 1241120361Sgad working->flags |= CE_TRIMAT; 1242120361Sgad working->trim_at = ptime_init(NULL); 1243120361Sgad ptm_opts = PTM_PARSE_ISO8601; 1244120361Sgad if (*ep == '$') 1245120361Sgad ptm_opts = PTM_PARSE_DWM; 1246120361Sgad ptm_opts |= PTM_PARSE_MATCHDOM; 1247120361Sgad res = ptime_relparse(working->trim_at, ptm_opts, 1248120361Sgad ptimeget_secs(timenow), ep + 1); 1249120361Sgad if (res == -2) 1250120361Sgad errx(1, "nonexistent time for 'at' value:\n%s", 1251120361Sgad errline); 1252120361Sgad else if (res < 0) 1253120361Sgad errx(1, "malformed 'at' value:\n%s", errline); 125443071Swollman } 1255120361Sgadno_trimat: 125643071Swollman 125725518Sbrian if (eol) 125859003Shm q = NULL; 125925518Sbrian else { 1260252378Skientzle q = parse = sob(parse + 1); /* Optional field */ 126159003Shm parse = son(parse); 126259003Shm if (!*parse) 126359003Shm eol = 1; 126459003Shm *parse = '\0'; 126525518Sbrian } 126625443Sache 1267111768Sgad for (; q && *q && !isspacech(*q); q++) { 1268111768Sgad switch (tolowerch(*q)) { 1269111768Sgad case 'b': 127059003Shm working->flags |= CE_BINARY; 1271111768Sgad break; 1272114137Sgad case 'c': 1273111768Sgad /* 1274114137Sgad * XXX - Ick! Ugly! Remove ASAP! 1275114137Sgad * We want `c' and `C' for "create". But we 1276114137Sgad * will temporarily treat `c' as `g', because 1277114137Sgad * FreeBSD releases <= 4.8 have a typo of 1278114137Sgad * checking ('G' || 'c') for CE_GLOB. 1279111768Sgad */ 1280114137Sgad if (*q == 'c') { 1281114137Sgad warnx("Assuming 'g' for 'c' in flags for line:\n%s", 1282114137Sgad errline); 1283114137Sgad warnx("The 'c' flag will eventually mean 'CREATE'"); 1284114137Sgad working->flags |= CE_GLOB; 1285114137Sgad break; 1286114137Sgad } 1287114137Sgad working->flags |= CE_CREATE; 1288114137Sgad break; 1289130043Sgad case 'd': 1290130043Sgad working->flags |= CE_NODUMP; 1291130043Sgad break; 1292111768Sgad case 'g': 1293106905Ssobomax working->flags |= CE_GLOB; 1294111768Sgad break; 1295111768Sgad case 'j': 1296218127Smm working->compress = COMPRESS_BZIP2; 1297111768Sgad break; 1298111768Sgad case 'n': 1299111768Sgad working->flags |= CE_NOSIGNAL; 1300111768Sgad break; 1301221873Ssobomax case 'r': 1302221873Ssobomax working->flags |= CE_PID2CMD; 1303221873Ssobomax break; 1304112003Sgad case 'u': 1305112003Sgad working->flags |= CE_SIGNALGROUP; 1306112003Sgad break; 1307111768Sgad case 'w': 1308160560Ssobomax /* Depreciated flag - keep for compatibility purposes */ 1309111768Sgad break; 1310218127Smm case 'x': 1311218127Smm working->compress = COMPRESS_XZ; 1312218127Smm break; 1313111768Sgad case 'z': 1314218127Smm working->compress = COMPRESS_GZIP; 1315111768Sgad break; 1316111768Sgad case '-': 1317111768Sgad break; 1318111781Sgad case 'f': /* Used by OpenBSD for "CE_FOLLOW" */ 1319111781Sgad case 'm': /* Used by OpenBSD for "CE_MONITOR" */ 1320111781Sgad case 'p': /* Used by NetBSD for "CE_PLAIN0" */ 1321111768Sgad default: 132280646Sobrien errx(1, "illegal flag in config file -- %c", 132380646Sobrien *q); 1324111768Sgad } 132559003Shm } 132659003Shm 132725518Sbrian if (eol) 132859003Shm q = NULL; 132925518Sbrian else { 1330252378Skientzle q = parse = sob(parse + 1); /* Optional field */ 133159003Shm parse = son(parse); 133259003Shm if (!*parse) 133359003Shm eol = 1; 133459003Shm *parse = '\0'; 133525518Sbrian } 133625443Sache 1337221873Ssobomax working->pid_cmd_file = NULL; 133825443Sache if (q && *q) { 133925443Sache if (*q == '/') 1340221873Ssobomax working->pid_cmd_file = strdup(q); 134136817Sache else if (isdigit(*q)) 134236817Sache goto got_sig; 134359003Shm else 134480646Sobrien errx(1, 134580646Sobrien "illegal pid file or signal number in config file:\n%s", 134680646Sobrien errline); 134725443Sache } 134836817Sache if (eol) 134959003Shm q = NULL; 135036817Sache else { 1351252378Skientzle q = parse = sob(parse + 1); /* Optional field */ 135259003Shm *(parse = son(parse)) = '\0'; 135336817Sache } 135436817Sache 135536817Sache working->sig = SIGHUP; 135636817Sache if (q && *q) { 135736817Sache if (isdigit(*q)) { 135859003Shm got_sig: 135936817Sache working->sig = atoi(q); 136036817Sache } else { 136159003Shm err_sig: 136280646Sobrien errx(1, 136380646Sobrien "illegal signal number in config file:\n%s", 136480646Sobrien errline); 136536817Sache } 136636817Sache if (working->sig < 1 || working->sig >= NSIG) 136736817Sache goto err_sig; 136836817Sache } 1369111768Sgad 1370111768Sgad /* 1371111768Sgad * Finish figuring out what pid-file to use (if any) in 1372111768Sgad * later processing if this logfile needs to be rotated. 1373111768Sgad */ 1374111768Sgad if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) { 1375111768Sgad /* 1376111768Sgad * This config-entry specified 'n' for nosignal, 1377221873Ssobomax * see if it also specified an explicit pid_cmd_file. 1378111768Sgad * This would be a pretty pointless combination. 1379111768Sgad */ 1380221873Ssobomax if (working->pid_cmd_file != NULL) { 1381111768Sgad warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s", 1382221873Ssobomax working->pid_cmd_file, errline); 1383221873Ssobomax free(working->pid_cmd_file); 1384221873Ssobomax working->pid_cmd_file = NULL; 1385111768Sgad } 1386221873Ssobomax } else if (working->pid_cmd_file == NULL) { 1387111768Sgad /* 1388111768Sgad * This entry did not specify the 'n' flag, which 1389111768Sgad * means it should signal syslogd unless it had 1390112003Sgad * specified some other pid-file (and obviously the 1391112003Sgad * syslog pid-file will not be for a process-group). 1392112003Sgad * Also, we should only try to notify syslog if we 1393112003Sgad * are root. 1394111768Sgad */ 1395112003Sgad if (working->flags & CE_SIGNALGROUP) { 1396112003Sgad warnx("Ignoring flag 'U' in line:\n%s", 1397112003Sgad errline); 1398112003Sgad working->flags &= ~CE_SIGNALGROUP; 1399112003Sgad } 1400111768Sgad if (needroot) 1401221873Ssobomax working->pid_cmd_file = strdup(path_syslogpid); 1402111768Sgad } 1403111768Sgad 1404112020Sgad /* 1405112020Sgad * Add this entry to the appropriate list of entries, unless 1406112020Sgad * it was some kind of special entry (eg: <default>). 1407112020Sgad */ 1408112020Sgad if (special) { 1409112020Sgad ; /* Do not add to any list */ 1410112020Sgad } else if (working->flags & CE_GLOB) { 1411208648Sgordon STAILQ_INSERT_TAIL(glob_p, working, cf_nextp); 1412112020Sgad } else { 1413208648Sgordon STAILQ_INSERT_TAIL(work_p, working, cf_nextp); 1414112020Sgad } 1415130165Sgad } 1416130165Sgad if (errline != NULL) 141759003Shm free(errline); 141813244Sgraichen} 141913244Sgraichen 142059003Shmstatic char * 142159004Shmmissing_field(char *p, char *errline) 142213244Sgraichen{ 142380646Sobrien 142459003Shm if (!p || !*p) 142559003Shm errx(1, "missing field in config file:\n%s", errline); 142659003Shm return (p); 142713244Sgraichen} 142813244Sgraichen 1429208649Sgordon/* 1430210372Ssimon * In our sort we return it in the reverse of what qsort normally 1431210372Ssimon * would do, as we want the newest files first. If we have two 1432210372Ssimon * entries with the same time we don't really care about order. 1433210372Ssimon * 1434210372Ssimon * Support function for qsort() in delete_oldest_timelog(). 1435210372Ssimon */ 1436210372Ssimonstatic int 1437210372Ssimonoldlog_entry_compare(const void *a, const void *b) 1438210372Ssimon{ 1439210372Ssimon const struct oldlog_entry *ola = a, *olb = b; 1440210372Ssimon 1441210372Ssimon if (ola->t > olb->t) 1442210372Ssimon return (-1); 1443210372Ssimon else if (ola->t < olb->t) 1444210372Ssimon return (1); 1445210372Ssimon else 1446210372Ssimon return (0); 1447210372Ssimon} 1448210372Ssimon 1449210372Ssimon/* 1450248776Smarkj * Check whether the file corresponding to dp is an archive of the logfile 1451248776Smarkj * logfname, based on the timefnamefmt format string. Return true and fill out 1452248776Smarkj * tm if this is the case; otherwise return false. 1453248776Smarkj */ 1454248776Smarkjstatic int 1455250545Smarkjvalidate_old_timelog(int fd, const struct dirent *dp, const char *logfname, 1456250545Smarkj struct tm *tm) 1457248776Smarkj{ 1458250545Smarkj struct stat sb; 1459248776Smarkj size_t logfname_len; 1460248776Smarkj char *s; 1461248776Smarkj int c; 1462248776Smarkj 1463248776Smarkj logfname_len = strlen(logfname); 1464248776Smarkj 1465250545Smarkj if (dp->d_type != DT_REG) { 1466250545Smarkj /* 1467250545Smarkj * Some filesystems (e.g. NFS) don't fill out the d_type field 1468250545Smarkj * and leave it set to DT_UNKNOWN; in this case we must obtain 1469250545Smarkj * the file type ourselves. 1470250545Smarkj */ 1471250545Smarkj if (dp->d_type != DT_UNKNOWN || 1472250545Smarkj fstatat(fd, dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) != 0 || 1473250545Smarkj !S_ISREG(sb.st_mode)) 1474250545Smarkj return (0); 1475250545Smarkj } 1476248776Smarkj /* Ignore everything but files with our logfile prefix. */ 1477248776Smarkj if (strncmp(dp->d_name, logfname, logfname_len) != 0) 1478248776Smarkj return (0); 1479248776Smarkj /* Ignore the actual non-rotated logfile. */ 1480248776Smarkj if (dp->d_namlen == logfname_len) 1481248776Smarkj return (0); 1482248776Smarkj 1483248776Smarkj /* 1484248776Smarkj * Make sure we created have found a logfile, so the 1485248776Smarkj * postfix is valid, IE format is: '.<time>(.[bgx]z)?'. 1486248776Smarkj */ 1487248776Smarkj if (dp->d_name[logfname_len] != '.') { 1488248776Smarkj if (verbose) 1489248776Smarkj printf("Ignoring %s which has unexpected " 1490248776Smarkj "extension '%s'\n", dp->d_name, 1491248776Smarkj &dp->d_name[logfname_len]); 1492248776Smarkj return (0); 1493248776Smarkj } 1494261820Smarkj memset(tm, 0, sizeof(*tm)); 1495248776Smarkj if ((s = strptime(&dp->d_name[logfname_len + 1], 1496248776Smarkj timefnamefmt, tm)) == NULL) { 1497248776Smarkj /* 1498248776Smarkj * We could special case "old" sequentially named logfiles here, 1499248776Smarkj * but we do not as that would require special handling to 1500248776Smarkj * decide which one was the oldest compared to "new" time based 1501248776Smarkj * logfiles. 1502248776Smarkj */ 1503248776Smarkj if (verbose) 1504248776Smarkj printf("Ignoring %s which does not " 1505248776Smarkj "match time format\n", dp->d_name); 1506248776Smarkj return (0); 1507248776Smarkj } 1508248776Smarkj 1509248776Smarkj for (c = 0; c < COMPRESS_TYPES; c++) 1510248776Smarkj if (strcmp(s, compress_type[c].suffix) == 0) 1511248776Smarkj /* We're done. */ 1512248776Smarkj return (1); 1513248776Smarkj 1514248776Smarkj if (verbose) 1515248776Smarkj printf("Ignoring %s which has unexpected extension '%s'\n", 1516248776Smarkj dp->d_name, s); 1517248776Smarkj 1518248776Smarkj return (0); 1519248776Smarkj} 1520248776Smarkj 1521248776Smarkj/* 1522210372Ssimon * Delete the oldest logfiles, when using time based filenames. 1523210372Ssimon */ 1524210372Ssimonstatic void 1525210372Ssimondelete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir) 1526210372Ssimon{ 1527210372Ssimon char *logfname, *s, *dir, errbuf[80]; 1528248776Smarkj int dir_fd, i, logcnt, max_logcnt; 1529210372Ssimon struct oldlog_entry *oldlogs; 1530210372Ssimon struct dirent *dp; 1531210372Ssimon const char *cdir; 1532210372Ssimon struct tm tm; 1533210372Ssimon DIR *dirp; 1534210372Ssimon 1535210372Ssimon oldlogs = malloc(MAX_OLDLOGS * sizeof(struct oldlog_entry)); 1536210372Ssimon max_logcnt = MAX_OLDLOGS; 1537210372Ssimon logcnt = 0; 1538210372Ssimon 1539210372Ssimon if (archive_dir != NULL && archive_dir[0] != '\0') 1540210372Ssimon cdir = archive_dir; 1541210372Ssimon else 1542210372Ssimon if ((cdir = dirname(ent->log)) == NULL) 1543210372Ssimon err(1, "dirname()"); 1544210372Ssimon if ((dir = strdup(cdir)) == NULL) 1545210372Ssimon err(1, "strdup()"); 1546210372Ssimon 1547210372Ssimon if ((s = basename(ent->log)) == NULL) 1548210372Ssimon err(1, "basename()"); 1549210372Ssimon if ((logfname = strdup(s)) == NULL) 1550210372Ssimon err(1, "strdup()"); 1551210372Ssimon if (strcmp(logfname, "/") == 0) 1552210372Ssimon errx(1, "Invalid log filename - became '/'"); 1553210372Ssimon 1554210372Ssimon if (verbose > 2) 1555210372Ssimon printf("Searching for old logs in %s\n", dir); 1556210372Ssimon 1557210372Ssimon /* First we create a 'list' of all archived logfiles */ 1558210372Ssimon if ((dirp = opendir(dir)) == NULL) 1559210372Ssimon err(1, "Cannot open log directory '%s'", dir); 1560235647Sgleb dir_fd = dirfd(dirp); 1561210372Ssimon while ((dp = readdir(dirp)) != NULL) { 1562250545Smarkj if (validate_old_timelog(dir_fd, dp, logfname, &tm) == 0) 1563210372Ssimon continue; 1564210372Ssimon 1565210372Ssimon /* 1566210372Ssimon * We should now have old an old rotated logfile, so 1567210372Ssimon * add it to the 'list'. 1568210372Ssimon */ 1569210372Ssimon if ((oldlogs[logcnt].t = timegm(&tm)) == -1) 1570210372Ssimon err(1, "Could not convert time string to time value"); 1571210372Ssimon if ((oldlogs[logcnt].fname = strdup(dp->d_name)) == NULL) 1572210372Ssimon err(1, "strdup()"); 1573210372Ssimon logcnt++; 1574210372Ssimon 1575210372Ssimon /* 1576210372Ssimon * It is very unlikely we ever run out of space in the 1577210372Ssimon * logfile array from the default size, but lets 1578210372Ssimon * handle it anyway... 1579210372Ssimon */ 1580210372Ssimon if (logcnt >= max_logcnt) { 1581210372Ssimon max_logcnt *= 4; 1582210372Ssimon /* Detect integer overflow */ 1583210372Ssimon if (max_logcnt < logcnt) 1584210372Ssimon errx(1, "Too many old logfiles found"); 1585210372Ssimon oldlogs = realloc(oldlogs, 1586210372Ssimon max_logcnt * sizeof(struct oldlog_entry)); 1587210372Ssimon if (oldlogs == NULL) 1588210372Ssimon err(1, "realloc()"); 1589210372Ssimon } 1590210372Ssimon } 1591210372Ssimon 1592210372Ssimon /* Second, if needed we delete oldest archived logfiles */ 1593210372Ssimon if (logcnt > 0 && logcnt >= ent->numlogs && ent->numlogs > 1) { 1594210372Ssimon oldlogs = realloc(oldlogs, logcnt * 1595210372Ssimon sizeof(struct oldlog_entry)); 1596210372Ssimon if (oldlogs == NULL) 1597210372Ssimon err(1, "realloc()"); 1598210372Ssimon 1599210372Ssimon /* 1600210372Ssimon * We now sort the logs in the order of newest to 1601210372Ssimon * oldest. That way we can simply skip over the 1602210372Ssimon * number of records we want to keep. 1603210372Ssimon */ 1604210372Ssimon qsort(oldlogs, logcnt, sizeof(struct oldlog_entry), 1605210372Ssimon oldlog_entry_compare); 1606210372Ssimon for (i = ent->numlogs - 1; i < logcnt; i++) { 1607210372Ssimon if (noaction) 1608210372Ssimon printf("\trm -f %s/%s\n", dir, 1609210372Ssimon oldlogs[i].fname); 1610235647Sgleb else if (unlinkat(dir_fd, oldlogs[i].fname, 0) != 0) { 1611210372Ssimon snprintf(errbuf, sizeof(errbuf), 1612244995Smarkj "Could not delete old logfile '%s'", 1613210372Ssimon oldlogs[i].fname); 1614210372Ssimon perror(errbuf); 1615210372Ssimon } 1616210372Ssimon } 1617210372Ssimon } else if (verbose > 1) 1618210372Ssimon printf("No old logs to delete for logfile %s\n", ent->log); 1619210372Ssimon 1620210372Ssimon /* Third, cleanup */ 1621210372Ssimon closedir(dirp); 1622210372Ssimon for (i = 0; i < logcnt; i++) { 1623210372Ssimon assert(oldlogs[i].fname != NULL); 1624210372Ssimon free(oldlogs[i].fname); 1625210372Ssimon } 1626210372Ssimon free(oldlogs); 1627210372Ssimon free(logfname); 1628210372Ssimon free(dir); 1629210372Ssimon} 1630210372Ssimon 1631210372Ssimon/* 1632228990Suqs * Generate a log filename, when using classic filenames. 1633220926Ssimon */ 1634220926Ssimonstatic void 1635229654Suqsgen_classiclog_fname(char *fname, size_t fname_sz, const char *archive_dir, 1636220926Ssimon const char *namepart, int numlogs_c) 1637220926Ssimon{ 1638220926Ssimon 1639220926Ssimon if (archive_dir[0] != '\0') 1640220926Ssimon (void) snprintf(fname, fname_sz, "%s/%s.%d", archive_dir, 1641220926Ssimon namepart, numlogs_c); 1642220926Ssimon else 1643220926Ssimon (void) snprintf(fname, fname_sz, "%s.%d", namepart, numlogs_c); 1644220926Ssimon} 1645220926Ssimon 1646220926Ssimon/* 1647229654Suqs * Delete a rotated logfile, when using classic filenames. 1648220926Ssimon */ 1649220926Ssimonstatic void 1650229654Suqsdelete_classiclog(const char *archive_dir, const char *namepart, int numlog_c) 1651220926Ssimon{ 1652220926Ssimon char file1[MAXPATHLEN], zfile1[MAXPATHLEN]; 1653220926Ssimon int c; 1654220926Ssimon 1655229654Suqs gen_classiclog_fname(file1, sizeof(file1), archive_dir, namepart, 1656220926Ssimon numlog_c); 1657220926Ssimon 1658220926Ssimon for (c = 0; c < COMPRESS_TYPES; c++) { 1659220926Ssimon (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, 1660220926Ssimon compress_type[c].suffix); 1661220926Ssimon if (noaction) 1662220926Ssimon printf("\trm -f %s\n", zfile1); 1663220926Ssimon else 1664220926Ssimon (void) unlink(zfile1); 1665220926Ssimon } 1666220926Ssimon} 1667220926Ssimon 1668220926Ssimon/* 1669208649Sgordon * Only add to the queue if the file hasn't already been added. This is 1670208649Sgordon * done to prevent circular include loops. 1671208649Sgordon */ 1672208649Sgordonstatic void 1673208649Sgordonadd_to_queue(const char *fname, struct ilist *inclist) 1674208649Sgordon{ 1675208649Sgordon struct include_entry *inc; 1676208649Sgordon 1677208649Sgordon STAILQ_FOREACH(inc, inclist, inc_nextp) { 1678208649Sgordon if (strcmp(fname, inc->file) == 0) { 1679208649Sgordon warnx("duplicate include detected: %s", fname); 1680208649Sgordon return; 1681208649Sgordon } 1682208649Sgordon } 1683208649Sgordon 1684208649Sgordon inc = malloc(sizeof(struct include_entry)); 1685208649Sgordon if (inc == NULL) 1686208649Sgordon err(1, "malloc of inc"); 1687208649Sgordon inc->file = strdup(fname); 1688208649Sgordon 1689208649Sgordon if (verbose > 2) 1690208649Sgordon printf("\t+ Adding %s to the processing queue.\n", fname); 1691208649Sgordon 1692208649Sgordon STAILQ_INSERT_TAIL(inclist, inc, inc_nextp); 1693208649Sgordon} 1694208649Sgordon 1695218127Smm/* 1696218127Smm * Search for logfile and return its compression suffix (if supported) 1697218127Smm * The suffix detection is first-match in the order of compress_types 1698218127Smm * 1699218127Smm * Note: if logfile without suffix exists (uncompressed, COMPRESS_NONE) 1700218127Smm * a zero-length string is returned 1701218127Smm */ 1702218127Smmstatic const char * 1703218127Smmget_logfile_suffix(const char *logfile) 1704218127Smm{ 1705218127Smm struct stat st; 1706218127Smm char zfile[MAXPATHLEN]; 1707218944Ssobomax int c; 1708218127Smm 1709218944Ssobomax for (c = 0; c < COMPRESS_TYPES; c++) { 1710218127Smm (void) strlcpy(zfile, logfile, MAXPATHLEN); 1711218127Smm (void) strlcat(zfile, compress_type[c].suffix, MAXPATHLEN); 1712218127Smm if (lstat(zfile, &st) == 0) 1713218127Smm return (compress_type[c].suffix); 1714218127Smm } 1715218127Smm return (NULL); 1716218127Smm} 1717218127Smm 1718130165Sgadstatic fk_entry 1719130165Sgaddo_rotate(const struct conf_entry *ent) 172013244Sgraichen{ 172171299Sjedgar char dirpart[MAXPATHLEN], namepart[MAXPATHLEN]; 172271299Sjedgar char file1[MAXPATHLEN], file2[MAXPATHLEN]; 172371299Sjedgar char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN]; 1724218127Smm const char *logfile_suffix; 1725210372Ssimon char datetimestr[30]; 1726159968Sgad int flags, numlogs_c; 1727130165Sgad fk_entry free_or_keep; 1728159968Sgad struct sigwork_entry *swork; 172959003Shm struct stat st; 1730210372Ssimon struct tm tm; 1731210372Ssimon time_t now; 173213244Sgraichen 1733119927Sgad flags = ent->flags; 1734130165Sgad free_or_keep = FREE_ENT; 1735119927Sgad 173659004Shm if (archtodir) { 173759004Shm char *p; 173813244Sgraichen 173959004Shm /* build complete name of archive directory into dirpart */ 174059004Shm if (*archdirname == '/') { /* absolute */ 174171299Sjedgar strlcpy(dirpart, archdirname, sizeof(dirpart)); 174259004Shm } else { /* relative */ 174359004Shm /* get directory part of logfile */ 1744119927Sgad strlcpy(dirpart, ent->log, sizeof(dirpart)); 1745229403Sed if ((p = strrchr(dirpart, '/')) == NULL) 174659004Shm dirpart[0] = '\0'; 174759004Shm else 174859004Shm *(p + 1) = '\0'; 174971299Sjedgar strlcat(dirpart, archdirname, sizeof(dirpart)); 175059004Shm } 175159004Shm 175259004Shm /* check if archive directory exists, if not, create it */ 175359004Shm if (lstat(dirpart, &st)) 1754114137Sgad createdir(ent, dirpart); 175559004Shm 175659004Shm /* get filename part of logfile */ 1757229403Sed if ((p = strrchr(ent->log, '/')) == NULL) 1758119927Sgad strlcpy(namepart, ent->log, sizeof(namepart)); 175959004Shm else 176071299Sjedgar strlcpy(namepart, p + 1, sizeof(namepart)); 176159004Shm } else { 1762210372Ssimon /* 1763220926Ssimon * Tell utility functions we are not using an archive 1764220926Ssimon * dir. 1765210372Ssimon */ 1766210372Ssimon dirpart[0] = '\0'; 1767220926Ssimon strlcpy(namepart, ent->log, sizeof(namepart)); 1768210372Ssimon } 1769210372Ssimon 1770210372Ssimon /* Delete old logs */ 1771210372Ssimon if (timefnamefmt != NULL) 1772210372Ssimon delete_oldest_timelog(ent, dirpart); 1773210372Ssimon else { 1774220926Ssimon /* 1775220926Ssimon * Handle cleaning up after legacy newsyslog where we 1776220926Ssimon * kept ent->numlogs + 1 files. This code can go away 1777220926Ssimon * at some point in the future. 1778220926Ssimon */ 1779229654Suqs delete_classiclog(dirpart, namepart, ent->numlogs); 1780220926Ssimon 1781220926Ssimon if (ent->numlogs > 0) 1782229654Suqs delete_classiclog(dirpart, namepart, ent->numlogs - 1); 1783220926Ssimon 178459003Shm } 178513244Sgraichen 1786210372Ssimon if (timefnamefmt != NULL) { 1787210372Ssimon /* If time functions fails we can't really do any sensible */ 1788210372Ssimon if (time(&now) == (time_t)-1 || 1789210372Ssimon localtime_r(&now, &tm) == NULL) 1790210372Ssimon bzero(&tm, sizeof(tm)); 1791210372Ssimon 1792210372Ssimon strftime(datetimestr, sizeof(datetimestr), timefnamefmt, &tm); 1793210372Ssimon if (archtodir) 1794210372Ssimon (void) snprintf(file1, sizeof(file1), "%s/%s.%s", 1795210372Ssimon dirpart, namepart, datetimestr); 1796210372Ssimon else 1797210372Ssimon (void) snprintf(file1, sizeof(file1), "%s.%s", 1798210372Ssimon ent->log, datetimestr); 1799210372Ssimon 1800210372Ssimon /* Don't run the code to move down logs */ 1801220926Ssimon numlogs_c = -1; 1802220926Ssimon } else { 1803229654Suqs gen_classiclog_fname(file1, sizeof(file1), dirpart, namepart, 1804220926Ssimon ent->numlogs - 1); 1805220926Ssimon numlogs_c = ent->numlogs - 2; /* copy for countdown */ 1806220926Ssimon } 1807210372Ssimon 180859003Shm /* Move down log files */ 1809220926Ssimon for (; numlogs_c >= 0; numlogs_c--) { 181071299Sjedgar (void) strlcpy(file2, file1, sizeof(file2)); 181159004Shm 1812229654Suqs gen_classiclog_fname(file1, sizeof(file1), dirpart, namepart, 1813220926Ssimon numlogs_c); 181459004Shm 1815218127Smm logfile_suffix = get_logfile_suffix(file1); 1816218127Smm if (logfile_suffix == NULL) 1817218127Smm continue; 1818218127Smm (void) strlcpy(zfile1, file1, MAXPATHLEN); 1819218127Smm (void) strlcpy(zfile2, file2, MAXPATHLEN); 1820218127Smm (void) strlcat(zfile1, logfile_suffix, MAXPATHLEN); 1821218127Smm (void) strlcat(zfile2, logfile_suffix, MAXPATHLEN); 1822218127Smm 1823130165Sgad if (noaction) 1824111967Sgad printf("\tmv %s %s\n", zfile1, zfile2); 1825130165Sgad else { 1826130165Sgad /* XXX - Ought to be checking for failure! */ 1827130165Sgad (void)rename(zfile1, zfile2); 182859003Shm } 1829130165Sgad change_attrs(zfile2, ent); 183059003Shm } 183113244Sgraichen 1832130038Sgad if (ent->numlogs > 0) { 1833129974Sgad if (noaction) { 1834130038Sgad /* 1835130038Sgad * Note that savelog() may succeed with using link() 1836130038Sgad * for the archtodir case, but there is no good way 1837130038Sgad * of knowing if it will when doing "noaction", so 1838130038Sgad * here we claim that it will have to do a copy... 1839130038Sgad */ 1840130038Sgad if (archtodir) 1841130038Sgad printf("\tcp %s %s\n", ent->log, file1); 1842130038Sgad else 1843130038Sgad printf("\tln %s %s\n", ent->log, file1); 1844244997Smarkj printf("\ttouch %s\t\t" 1845244997Smarkj "# Update mtime for 'when'-interval processing\n", 1846244997Smarkj file1); 1847130165Sgad } else { 1848130038Sgad if (!(flags & CE_BINARY)) { 1849130038Sgad /* Report the trimming to the old log */ 1850130038Sgad log_trim(ent->log, ent); 1851130038Sgad } 1852130038Sgad savelog(ent->log, file1); 1853244997Smarkj /* 1854244997Smarkj * Interval-based rotations are done using the mtime of 1855244997Smarkj * the most recently archived log, so make sure it gets 1856244997Smarkj * updated during a rotation. 1857244997Smarkj */ 1858244997Smarkj utimes(file1, NULL); 185959004Shm } 1860130165Sgad change_attrs(file1, ent); 186118075Sjkh } 186218075Sjkh 1863130038Sgad /* Create the new log file and move it into place */ 1864130038Sgad if (noaction) 1865111781Sgad printf("Start new log...\n"); 1866130038Sgad createlog(ent); 186725443Sache 1868111966Sgad /* 1869130167Sgad * Save all signalling and file-compression to be done after log 1870130167Sgad * files from all entries have been rotated. This way any one 1871130167Sgad * process will not be sent the same signal multiple times when 1872130167Sgad * multiple log files had to be rotated. 1873130167Sgad */ 1874159968Sgad swork = NULL; 1875221873Ssobomax if (ent->pid_cmd_file != NULL) 1876159968Sgad swork = save_sigwork(ent); 1877218127Smm if (ent->numlogs > 0 && ent->compress > COMPRESS_NONE) { 1878159968Sgad /* 1879159968Sgad * The zipwork_entry will include a pointer to this 1880159968Sgad * conf_entry, so the conf_entry should not be freed. 1881159968Sgad */ 1882159968Sgad free_or_keep = KEEP_ENT; 1883159968Sgad save_zipwork(ent, swork, ent->fsize, file1); 1884130167Sgad } 1885130167Sgad 1886130165Sgad return (free_or_keep); 188713244Sgraichen} 188813244Sgraichen 1889130167Sgadstatic void 1890130167Sgaddo_sigwork(struct sigwork_entry *swork) 1891130167Sgad{ 1892130204Sgad struct sigwork_entry *nextsig; 1893130204Sgad int kres, secs; 1894221873Ssobomax char *tmp; 1895130167Sgad 1896245963Smarkj if (swork->sw_runcmd == 0 && (!(swork->sw_pidok) || swork->sw_pid == 0)) 1897130167Sgad return; /* no work to do... */ 1898130167Sgad 1899130167Sgad /* 1900130167Sgad * If nosignal (-s) was specified, then do not signal any process. 1901130167Sgad * Note that a nosignal request triggers a warning message if the 1902130167Sgad * rotated logfile needs to be compressed, *unless* -R was also 1903130167Sgad * specified. We assume that an `-sR' request came from a process 1904130167Sgad * which writes to the logfile, and as such, we assume that process 1905130167Sgad * has already made sure the logfile is not presently in use. This 1906130167Sgad * just sets swork->sw_pidok to a special value, and do_zipwork 1907130167Sgad * will print any necessary warning(s). 1908130167Sgad */ 1909130167Sgad if (nosignal) { 1910130167Sgad if (!rotatereq) 1911130167Sgad swork->sw_pidok = -1; 1912130167Sgad return; 1913130167Sgad } 1914130167Sgad 1915130204Sgad /* 1916130204Sgad * Compute the pause between consecutive signals. Use a longer 1917130204Sgad * sleep time if we will be sending two signals to the same 1918130204Sgad * deamon or process-group. 1919130204Sgad */ 1920130204Sgad secs = 0; 1921130204Sgad nextsig = SLIST_NEXT(swork, sw_nextp); 1922130204Sgad if (nextsig != NULL) { 1923130204Sgad if (swork->sw_pid == nextsig->sw_pid) 1924130204Sgad secs = 10; 1925130204Sgad else 1926130204Sgad secs = 1; 1927130204Sgad } 1928130204Sgad 1929130167Sgad if (noaction) { 1930245963Smarkj if (swork->sw_runcmd) 1931245962Smarkj printf("\tsh -c '%s %d'\n", swork->sw_fname, 1932245962Smarkj swork->sw_signum); 1933245962Smarkj else { 1934245962Smarkj printf("\tkill -%d %d \t\t# %s\n", swork->sw_signum, 1935245962Smarkj (int)swork->sw_pid, swork->sw_fname); 1936245962Smarkj if (secs > 0) 1937245962Smarkj printf("\tsleep %d\n", secs); 1938245962Smarkj } 1939130167Sgad return; 1940130167Sgad } 1941130167Sgad 1942245963Smarkj if (swork->sw_runcmd) { 1943221873Ssobomax asprintf(&tmp, "%s %d", swork->sw_fname, swork->sw_signum); 1944221873Ssobomax if (tmp == NULL) { 1945221873Ssobomax warn("can't allocate memory to run %s", 1946221873Ssobomax swork->sw_fname); 1947221873Ssobomax return; 1948221873Ssobomax } 1949221873Ssobomax if (verbose) 1950221873Ssobomax printf("Run command: %s\n", tmp); 1951221873Ssobomax kres = system(tmp); 1952221873Ssobomax if (kres) { 1953221873Ssobomax warnx("%s: returned non-zero exit code: %d", 1954221873Ssobomax tmp, kres); 1955221873Ssobomax } 1956221873Ssobomax free(tmp); 1957221873Ssobomax return; 1958221873Ssobomax } 1959221873Ssobomax 1960130167Sgad kres = kill(swork->sw_pid, swork->sw_signum); 1961130167Sgad if (kres != 0) { 1962130167Sgad /* 1963130167Sgad * Assume that "no such process" (ESRCH) is something 1964130167Sgad * to warn about, but is not an error. Presumably the 1965130167Sgad * process which writes to the rotated log file(s) is 1966130167Sgad * gone, in which case we should have no problem with 1967130167Sgad * compressing the rotated log file(s). 1968130167Sgad */ 1969130167Sgad if (errno != ESRCH) 1970130167Sgad swork->sw_pidok = 0; 1971130167Sgad warn("can't notify %s, pid %d", swork->sw_pidtype, 1972130167Sgad (int)swork->sw_pid); 1973130167Sgad } else { 1974130204Sgad if (verbose) 1975130204Sgad printf("Notified %s pid %d = %s\n", swork->sw_pidtype, 1976130204Sgad (int)swork->sw_pid, swork->sw_fname); 1977130204Sgad if (secs > 0) { 1978130204Sgad if (verbose) 1979130204Sgad printf("Pause %d second(s) between signals\n", 1980130204Sgad secs); 1981130204Sgad sleep(secs); 1982130167Sgad } 1983130167Sgad } 1984130167Sgad} 1985130167Sgad 1986130167Sgadstatic void 1987130167Sgaddo_zipwork(struct zipwork_entry *zwork) 1988130167Sgad{ 1989130167Sgad const char *pgm_name, *pgm_path; 1990154566Sgad int errsav, fcount, zstatus; 1991130167Sgad pid_t pidzip, wpid; 1992130167Sgad char zresult[MAXPATHLEN]; 1993218944Ssobomax int c; 1994130167Sgad 1995228790Seadler assert(zwork != NULL); 1996130167Sgad pgm_path = NULL; 1997130167Sgad strlcpy(zresult, zwork->zw_fname, sizeof(zresult)); 1998228790Seadler if (zwork->zw_conf != NULL && 1999218127Smm zwork->zw_conf->compress > COMPRESS_NONE) 2000218944Ssobomax for (c = 1; c < COMPRESS_TYPES; c++) { 2001218127Smm if (zwork->zw_conf->compress == c) { 2002218127Smm pgm_path = compress_type[c].path; 2003218127Smm (void) strlcat(zresult, 2004218127Smm compress_type[c].suffix, sizeof(zresult)); 2005218127Smm break; 2006218127Smm } 2007130167Sgad } 2008130167Sgad if (pgm_path == NULL) { 2009130167Sgad warnx("invalid entry for %s in do_zipwork", zwork->zw_fname); 2010130167Sgad return; 2011130167Sgad } 2012154566Sgad pgm_name = strrchr(pgm_path, '/'); 2013154566Sgad if (pgm_name == NULL) 2014154566Sgad pgm_name = pgm_path; 2015154566Sgad else 2016154566Sgad pgm_name++; 2017130167Sgad 2018245963Smarkj if (zwork->zw_swork != NULL && zwork->zw_swork->sw_runcmd == 0 && 2019238281Sae zwork->zw_swork->sw_pidok <= 0) { 2020130167Sgad warnx( 2021130167Sgad "log %s not compressed because daemon(s) not notified", 2022130167Sgad zwork->zw_fname); 2023130167Sgad change_attrs(zwork->zw_fname, zwork->zw_conf); 2024130167Sgad return; 2025130167Sgad } 2026130167Sgad 2027130167Sgad if (noaction) { 2028130167Sgad printf("\t%s %s\n", pgm_name, zwork->zw_fname); 2029130167Sgad change_attrs(zresult, zwork->zw_conf); 2030130167Sgad return; 2031130167Sgad } 2032130167Sgad 2033154566Sgad fcount = 1; 2034130167Sgad pidzip = fork(); 2035154566Sgad while (pidzip < 0) { 2036154566Sgad /* 2037154566Sgad * The fork failed. If the failure was due to a temporary 2038154566Sgad * problem, then wait a short time and try it again. 2039154566Sgad */ 2040154566Sgad errsav = errno; 2041154566Sgad warn("fork() for `%s %s'", pgm_name, zwork->zw_fname); 2042154566Sgad if (errsav != EAGAIN || fcount > 5) 2043154566Sgad errx(1, "Exiting..."); 2044154566Sgad sleep(fcount * 12); 2045154566Sgad fcount++; 2046154566Sgad pidzip = fork(); 2047154566Sgad } 2048154566Sgad if (!pidzip) { 2049130167Sgad /* The child process executes the compression command */ 2050130167Sgad execl(pgm_path, pgm_path, "-f", zwork->zw_fname, (char *)0); 2051154566Sgad err(1, "execl(`%s -f %s')", pgm_path, zwork->zw_fname); 2052130167Sgad } 2053130167Sgad 2054130167Sgad wpid = waitpid(pidzip, &zstatus, 0); 2055130167Sgad if (wpid == -1) { 2056154566Sgad /* XXX - should this be a fatal error? */ 2057130167Sgad warn("%s: waitpid(%d)", pgm_path, pidzip); 2058130167Sgad return; 2059130167Sgad } 2060130167Sgad if (!WIFEXITED(zstatus)) { 2061154566Sgad warnx("`%s -f %s' did not terminate normally", pgm_name, 2062154566Sgad zwork->zw_fname); 2063130167Sgad return; 2064130167Sgad } 2065130167Sgad if (WEXITSTATUS(zstatus)) { 2066154566Sgad warnx("`%s -f %s' terminated with a non-zero status (%d)", 2067154566Sgad pgm_name, zwork->zw_fname, WEXITSTATUS(zstatus)); 2068130167Sgad return; 2069130167Sgad } 2070130167Sgad 2071130167Sgad /* Compression was successful, set file attributes on the result. */ 2072130167Sgad change_attrs(zresult, zwork->zw_conf); 2073130167Sgad} 2074130167Sgad 2075130167Sgad/* 2076130167Sgad * Save information on any process we need to signal. Any single 2077130167Sgad * process may need to be sent different signal-values for different 2078130167Sgad * log files, but usually a single signal-value will cause the process 2079130167Sgad * to close and re-open all of it's log files. 2080130167Sgad */ 2081130167Sgadstatic struct sigwork_entry * 2082130167Sgadsave_sigwork(const struct conf_entry *ent) 2083130167Sgad{ 2084130167Sgad struct sigwork_entry *sprev, *stmp; 2085130167Sgad int ndiff; 2086130167Sgad size_t tmpsiz; 2087130167Sgad 2088130167Sgad sprev = NULL; 2089130167Sgad ndiff = 1; 2090130167Sgad SLIST_FOREACH(stmp, &swhead, sw_nextp) { 2091221873Ssobomax ndiff = strcmp(ent->pid_cmd_file, stmp->sw_fname); 2092130167Sgad if (ndiff > 0) 2093130167Sgad break; 2094130167Sgad if (ndiff == 0) { 2095130167Sgad if (ent->sig == stmp->sw_signum) 2096130167Sgad break; 2097130167Sgad if (ent->sig > stmp->sw_signum) { 2098130167Sgad ndiff = 1; 2099130167Sgad break; 2100130167Sgad } 2101130167Sgad } 2102130167Sgad sprev = stmp; 2103130167Sgad } 2104130167Sgad if (stmp != NULL && ndiff == 0) 2105130167Sgad return (stmp); 2106130167Sgad 2107221873Ssobomax tmpsiz = sizeof(struct sigwork_entry) + strlen(ent->pid_cmd_file) + 1; 2108130167Sgad stmp = malloc(tmpsiz); 2109221873Ssobomax 2110245963Smarkj stmp->sw_runcmd = 0; 2111221873Ssobomax /* If this is a command to run we just set the flag and run command */ 2112221873Ssobomax if (ent->flags & CE_PID2CMD) { 2113245961Smarkj stmp->sw_pid = -1; 2114245961Smarkj stmp->sw_pidok = 0; 2115245963Smarkj stmp->sw_runcmd = 1; 2116221873Ssobomax } else { 2117221873Ssobomax set_swpid(stmp, ent); 2118221873Ssobomax } 2119130167Sgad stmp->sw_signum = ent->sig; 2120221873Ssobomax strcpy(stmp->sw_fname, ent->pid_cmd_file); 2121130167Sgad if (sprev == NULL) 2122130167Sgad SLIST_INSERT_HEAD(&swhead, stmp, sw_nextp); 2123130167Sgad else 2124130167Sgad SLIST_INSERT_AFTER(sprev, stmp, sw_nextp); 2125130167Sgad return (stmp); 2126130167Sgad} 2127130167Sgad 2128130167Sgad/* 2129130167Sgad * Save information on any file we need to compress. We may see the same 2130130167Sgad * file multiple times, so check the full list to avoid duplicates. The 2131130167Sgad * list itself is sorted smallest-to-largest, because that's the order we 2132130167Sgad * want to compress the files. If the partition is very low on disk space, 2133130167Sgad * then the smallest files are the most likely to compress, and compressing 2134130167Sgad * them first will free up more space for the larger files. 2135130167Sgad */ 2136130167Sgadstatic struct zipwork_entry * 2137130167Sgadsave_zipwork(const struct conf_entry *ent, const struct sigwork_entry *swork, 2138130167Sgad int zsize, const char *zipfname) 2139130167Sgad{ 2140130167Sgad struct zipwork_entry *zprev, *ztmp; 2141130167Sgad int ndiff; 2142130167Sgad size_t tmpsiz; 2143130167Sgad 2144130167Sgad /* Compute the size if the caller did not know it. */ 2145130167Sgad if (zsize < 0) 2146130167Sgad zsize = sizefile(zipfname); 2147130167Sgad 2148130167Sgad zprev = NULL; 2149130167Sgad ndiff = 1; 2150130167Sgad SLIST_FOREACH(ztmp, &zwhead, zw_nextp) { 2151130707Sgad ndiff = strcmp(zipfname, ztmp->zw_fname); 2152130167Sgad if (ndiff == 0) 2153130167Sgad break; 2154130167Sgad if (zsize > ztmp->zw_fsize) 2155130167Sgad zprev = ztmp; 2156130167Sgad } 2157130167Sgad if (ztmp != NULL && ndiff == 0) 2158130167Sgad return (ztmp); 2159130167Sgad 2160130167Sgad tmpsiz = sizeof(struct zipwork_entry) + strlen(zipfname) + 1; 2161130167Sgad ztmp = malloc(tmpsiz); 2162130167Sgad ztmp->zw_conf = ent; 2163130167Sgad ztmp->zw_swork = swork; 2164130167Sgad ztmp->zw_fsize = zsize; 2165130167Sgad strcpy(ztmp->zw_fname, zipfname); 2166130167Sgad if (zprev == NULL) 2167130167Sgad SLIST_INSERT_HEAD(&zwhead, ztmp, zw_nextp); 2168130167Sgad else 2169130167Sgad SLIST_INSERT_AFTER(zprev, ztmp, zw_nextp); 2170130167Sgad return (ztmp); 2171130167Sgad} 2172130167Sgad 2173130167Sgad/* Send a signal to the pid specified by pidfile */ 2174130167Sgadstatic void 2175130167Sgadset_swpid(struct sigwork_entry *swork, const struct conf_entry *ent) 2176130167Sgad{ 2177130167Sgad FILE *f; 2178130167Sgad long minok, maxok, rval; 2179130167Sgad char *endp, *linep, line[BUFSIZ]; 2180130167Sgad 2181130167Sgad minok = MIN_PID; 2182130167Sgad maxok = MAX_PID; 2183130167Sgad swork->sw_pidok = 0; 2184130167Sgad swork->sw_pid = 0; 2185130167Sgad swork->sw_pidtype = "daemon"; 2186130167Sgad if (ent->flags & CE_SIGNALGROUP) { 2187130167Sgad /* 2188130167Sgad * If we are expected to signal a process-group when 2189130167Sgad * rotating this logfile, then the value read in should 2190130167Sgad * be the negative of a valid process ID. 2191130167Sgad */ 2192130167Sgad minok = -MAX_PID; 2193130167Sgad maxok = -MIN_PID; 2194130167Sgad swork->sw_pidtype = "process-group"; 2195130167Sgad } 2196130167Sgad 2197221873Ssobomax f = fopen(ent->pid_cmd_file, "r"); 2198130167Sgad if (f == NULL) { 2199202668Sdelphij if (errno == ENOENT && enforcepid == 0) { 2200200806Sdelphij /* 2201200806Sdelphij * Warn if the PID file doesn't exist, but do 2202200806Sdelphij * not consider it an error. Most likely it 2203200806Sdelphij * means the process has been terminated, 2204200806Sdelphij * so it should be safe to rotate any log 2205200806Sdelphij * files that the process would have been using. 2206200806Sdelphij */ 2207200806Sdelphij swork->sw_pidok = 1; 2208221873Ssobomax warnx("pid file doesn't exist: %s", ent->pid_cmd_file); 2209200806Sdelphij } else 2210221873Ssobomax warn("can't open pid file: %s", ent->pid_cmd_file); 2211130167Sgad return; 2212130167Sgad } 2213130167Sgad 2214130167Sgad if (fgets(line, BUFSIZ, f) == NULL) { 2215130167Sgad /* 2216130167Sgad * Warn if the PID file is empty, but do not consider 2217130167Sgad * it an error. Most likely it means the process has 2218130167Sgad * has terminated, so it should be safe to rotate any 2219130167Sgad * log files that the process would have been using. 2220130167Sgad */ 2221202668Sdelphij if (feof(f) && enforcepid == 0) { 2222130167Sgad swork->sw_pidok = 1; 2223221873Ssobomax warnx("pid/cmd file is empty: %s", ent->pid_cmd_file); 2224130167Sgad } else 2225221873Ssobomax warn("can't read from pid file: %s", ent->pid_cmd_file); 2226130167Sgad (void)fclose(f); 2227130167Sgad return; 2228130167Sgad } 2229130167Sgad (void)fclose(f); 2230130167Sgad 2231130167Sgad errno = 0; 2232130167Sgad linep = line; 2233130167Sgad while (*linep == ' ') 2234130167Sgad linep++; 2235130167Sgad rval = strtol(linep, &endp, 10); 2236130167Sgad if (*endp != '\0' && !isspacech(*endp)) { 2237130167Sgad warnx("pid file does not start with a valid number: %s", 2238221873Ssobomax ent->pid_cmd_file); 2239130167Sgad } else if (rval < minok || rval > maxok) { 2240130167Sgad warnx("bad value '%ld' for process number in %s", 2241221873Ssobomax rval, ent->pid_cmd_file); 2242130167Sgad if (verbose) 2243130167Sgad warnx("\t(expecting value between %ld and %ld)", 2244130167Sgad minok, maxok); 2245130167Sgad } else { 2246130167Sgad swork->sw_pidok = 1; 2247130167Sgad swork->sw_pid = rval; 2248130167Sgad } 2249130167Sgad 2250130167Sgad return; 2251130167Sgad} 2252130167Sgad 225313244Sgraichen/* Log the fact that the logs were turned over */ 225459004Shmstatic int 2255119926Sgadlog_trim(const char *logname, const struct conf_entry *log_ent) 225613244Sgraichen{ 225759003Shm FILE *f; 2258111388Sgad const char *xtra; 225959003Shm 2260119926Sgad if ((f = fopen(logname, "a")) == NULL) 226159003Shm return (-1); 2262111388Sgad xtra = ""; 2263111768Sgad if (log_ent->def_cfg) 2264111388Sgad xtra = " using <default> rule"; 2265114137Sgad if (log_ent->firstcreate) 2266114137Sgad fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n", 2267114137Sgad daytime, hostname, (int) getpid(), xtra); 2268114137Sgad else if (log_ent->r_reason != NULL) 2269111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n", 2270111772Sgad daytime, hostname, (int) getpid(), log_ent->r_reason, xtra); 2271111772Sgad else 2272111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n", 2273111772Sgad daytime, hostname, (int) getpid(), xtra); 227459003Shm if (fclose(f) == EOF) 2275127858Scharnier err(1, "log_trim: fclose"); 227659003Shm return (0); 227713244Sgraichen} 227813244Sgraichen 227913244Sgraichen/* Return size in kilobytes of a file */ 228059004Shmstatic int 2281130165Sgadsizefile(const char *file) 228213244Sgraichen{ 228359003Shm struct stat sb; 228413244Sgraichen 228559003Shm if (stat(file, &sb) < 0) 228659003Shm return (-1); 2287262075Sbdrewery return (kbytes(sb.st_size)); 228813244Sgraichen} 228913244Sgraichen 2290248776Smarkj/* 2291248776Smarkj * Return the mtime of the most recent archive of the logfile, using timestamp 2292248776Smarkj * based filenames. 2293248776Smarkj */ 2294248776Smarkjstatic time_t 2295248776Smarkjmtime_old_timelog(const char *file) 2296248776Smarkj{ 2297248776Smarkj struct stat sb; 2298248776Smarkj struct tm tm; 2299248776Smarkj int dir_fd; 2300248776Smarkj time_t t; 2301248776Smarkj struct dirent *dp; 2302248776Smarkj DIR *dirp; 2303248776Smarkj char *s, *logfname, *dir; 2304248776Smarkj 2305248776Smarkj t = -1; 2306248776Smarkj 2307248776Smarkj if ((dir = dirname(file)) == NULL) { 2308248776Smarkj warn("dirname() of '%s'", file); 2309248776Smarkj return (t); 2310248776Smarkj } 2311248776Smarkj if ((s = basename(file)) == NULL) { 2312248776Smarkj warn("basename() of '%s'", file); 2313248776Smarkj return (t); 2314248776Smarkj } else if (s[0] == '/') { 2315248776Smarkj warnx("Invalid log filename '%s'", s); 2316248776Smarkj return (t); 2317248776Smarkj } else if ((logfname = strdup(s)) == NULL) 2318248776Smarkj err(1, "strdup()"); 2319248776Smarkj 2320248776Smarkj if ((dirp = opendir(dir)) == NULL) { 2321248776Smarkj warn("Cannot open log directory '%s'", dir); 2322248776Smarkj return (t); 2323248776Smarkj } 2324248776Smarkj dir_fd = dirfd(dirp); 2325248776Smarkj /* Open the archive dir and find the most recent archive of logfname. */ 2326248776Smarkj while ((dp = readdir(dirp)) != NULL) { 2327250545Smarkj if (validate_old_timelog(dir_fd, dp, logfname, &tm) == 0) 2328248776Smarkj continue; 2329248776Smarkj 2330251240Smarkj if (fstatat(dir_fd, dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) == -1) { 2331248776Smarkj warn("Cannot stat '%s'", file); 2332248776Smarkj continue; 2333248776Smarkj } 2334248776Smarkj if (t < sb.st_mtime) 2335248776Smarkj t = sb.st_mtime; 2336248776Smarkj } 2337248776Smarkj closedir(dirp); 2338248776Smarkj 2339248776Smarkj return (t); 2340248776Smarkj} 2341248776Smarkj 2342248776Smarkj/* Return the age in hours of the most recent archive of the logfile. */ 234359004Shmstatic int 2344248776Smarkjage_old_log(const char *file) 234513244Sgraichen{ 234659003Shm struct stat sb; 2347218127Smm const char *logfile_suffix; 2348218127Smm char tmp[MAXPATHLEN + sizeof(".0") + COMPRESS_SUFFIX_MAXLEN + 1]; 2349248776Smarkj time_t mtime; 235013244Sgraichen 235159004Shm if (archtodir) { 235259004Shm char *p; 235359004Shm 235459004Shm /* build name of archive directory into tmp */ 235559004Shm if (*archdirname == '/') { /* absolute */ 235671299Sjedgar strlcpy(tmp, archdirname, sizeof(tmp)); 235759004Shm } else { /* relative */ 235859004Shm /* get directory part of logfile */ 235971299Sjedgar strlcpy(tmp, file, sizeof(tmp)); 2360229403Sed if ((p = strrchr(tmp, '/')) == NULL) 236159004Shm tmp[0] = '\0'; 236259004Shm else 236359004Shm *(p + 1) = '\0'; 236471299Sjedgar strlcat(tmp, archdirname, sizeof(tmp)); 236559004Shm } 236659004Shm 236771299Sjedgar strlcat(tmp, "/", sizeof(tmp)); 236859004Shm 236959004Shm /* get filename part of logfile */ 2370229403Sed if ((p = strrchr(file, '/')) == NULL) 237171299Sjedgar strlcat(tmp, file, sizeof(tmp)); 237259004Shm else 237371299Sjedgar strlcat(tmp, p + 1, sizeof(tmp)); 237459004Shm } else { 237571299Sjedgar (void) strlcpy(tmp, file, sizeof(tmp)); 237659004Shm } 237759004Shm 2378248776Smarkj if (timefnamefmt != NULL) { 2379248776Smarkj mtime = mtime_old_timelog(tmp); 2380248776Smarkj if (mtime == -1) 2381248776Smarkj return (-1); 2382248776Smarkj } else { 2383248776Smarkj strlcat(tmp, ".0", sizeof(tmp)); 2384248776Smarkj logfile_suffix = get_logfile_suffix(tmp); 2385248776Smarkj if (logfile_suffix == NULL) 2386248776Smarkj return (-1); 2387248776Smarkj (void) strlcat(tmp, logfile_suffix, sizeof(tmp)); 2388248776Smarkj if (stat(tmp, &sb) < 0) 2389248776Smarkj return (-1); 2390248776Smarkj mtime = sb.st_mtime; 2391248776Smarkj } 2392248776Smarkj 2393248776Smarkj return ((int)(ptimeget_secs(timenow) - mtime + 1800) / 3600); 239413244Sgraichen} 239513244Sgraichen 239613244Sgraichen/* Skip Over Blanks */ 2397111820Sgadstatic char * 239859004Shmsob(char *p) 239913244Sgraichen{ 240059003Shm while (p && *p && isspace(*p)) 240159003Shm p++; 240259003Shm return (p); 240313244Sgraichen} 240413244Sgraichen 240513244Sgraichen/* Skip Over Non-Blanks */ 2406111820Sgadstatic char * 240759004Shmson(char *p) 240813244Sgraichen{ 240959003Shm while (p && *p && !isspace(*p)) 241059003Shm p++; 241159003Shm return (p); 241213244Sgraichen} 241343071Swollman 2414119102Sgad/* Check if string is actually a number */ 2415119102Sgadstatic int 2416119102Sgadisnumberstr(const char *string) 2417119102Sgad{ 2418119102Sgad while (*string) { 2419119102Sgad if (!isdigitch(*string++)) 2420119102Sgad return (0); 2421119102Sgad } 2422119102Sgad return (1); 2423119102Sgad} 2424119102Sgad 2425208649Sgordon/* Check if string contains a glob */ 2426208649Sgordonstatic int 2427208649Sgordonisglobstr(const char *string) 2428208649Sgordon{ 2429208649Sgordon char chr; 2430208649Sgordon 2431208649Sgordon while ((chr = *string++)) { 2432208649Sgordon if (chr == '*' || chr == '?' || chr == '[') 2433208649Sgordon return (1); 2434208649Sgordon } 2435208649Sgordon return (0); 2436208649Sgordon} 2437208649Sgordon 2438130038Sgad/* 2439130038Sgad * Save the active log file under a new name. A link to the new name 2440130038Sgad * is the quick-and-easy way to do this. If that fails (which it will 2441130038Sgad * if the destination is on another partition), then make a copy of 2442130038Sgad * the file to the new location. 2443130038Sgad */ 244459004Shmstatic void 2445130038Sgadsavelog(char *from, char *to) 244659004Shm{ 244759004Shm FILE *src, *dst; 2448130038Sgad int c, res; 244959004Shm 2450130038Sgad res = link(from, to); 2451130038Sgad if (res == 0) 2452130038Sgad return; 2453130038Sgad 245459004Shm if ((src = fopen(from, "r")) == NULL) 245559004Shm err(1, "can't fopen %s for reading", from); 245659004Shm if ((dst = fopen(to, "w")) == NULL) 245759004Shm err(1, "can't fopen %s for writing", to); 245859004Shm 245959004Shm while ((c = getc(src)) != EOF) { 246059004Shm if ((putc(c, dst)) == EOF) 246159004Shm err(1, "error writing to %s", to); 246259004Shm } 246359004Shm 246459004Shm if (ferror(src)) 246559004Shm err(1, "error reading from %s", from); 246659004Shm if ((fclose(src)) != 0) 246759004Shm err(1, "can't fclose %s", to); 246859004Shm if ((fclose(dst)) != 0) 246959004Shm err(1, "can't fclose %s", from); 247059004Shm} 247159004Shm 247259004Shm/* create one or more directory components of a path */ 247359004Shmstatic void 2474114137Sgadcreatedir(const struct conf_entry *ent, char *dirpart) 247559004Shm{ 2476111398Sgad int res; 247759004Shm char *s, *d; 247871299Sjedgar char mkdirpath[MAXPATHLEN]; 247959004Shm struct stat st; 248059004Shm 248159004Shm s = dirpart; 248259004Shm d = mkdirpath; 248359004Shm 248459004Shm for (;;) { 248559004Shm *d++ = *s++; 2486111398Sgad if (*s != '/' && *s != '\0') 2487111398Sgad continue; 2488111398Sgad *d = '\0'; 2489111398Sgad res = lstat(mkdirpath, &st); 2490111398Sgad if (res != 0) { 2491111398Sgad if (noaction) { 2492111967Sgad printf("\tmkdir %s\n", mkdirpath); 2493111398Sgad } else { 2494111398Sgad res = mkdir(mkdirpath, 0755); 2495111398Sgad if (res != 0) 2496111398Sgad err(1, "Error on mkdir(\"%s\") for -a", 2497111398Sgad mkdirpath); 2498111398Sgad } 249959004Shm } 250059004Shm if (*s == '\0') 250159004Shm break; 250259004Shm } 2503114137Sgad if (verbose) { 2504114137Sgad if (ent->firstcreate) 2505114137Sgad printf("Created directory '%s' for new %s\n", 2506114137Sgad dirpart, ent->log); 2507114137Sgad else 2508114137Sgad printf("Created directory '%s' for -a\n", dirpart); 2509114137Sgad } 251059004Shm} 251159004Shm 2512114137Sgad/* 2513114137Sgad * Create a new log file, destroying any currently-existing version 2514114137Sgad * of the log file in the process. If the caller wants a backup copy 2515114137Sgad * of the file to exist, they should call 'link(logfile,logbackup)' 2516114137Sgad * before calling this routine. 2517114137Sgad */ 2518114137Sgadvoid 2519114137Sgadcreatelog(const struct conf_entry *ent) 2520114137Sgad{ 2521114137Sgad int fd, failed; 2522114137Sgad struct stat st; 2523114137Sgad char *realfile, *slash, tempfile[MAXPATHLEN]; 2524114137Sgad 2525114137Sgad fd = -1; 2526114137Sgad realfile = ent->log; 2527114137Sgad 2528114137Sgad /* 2529114137Sgad * If this log file is being created for the first time (-C option), 2530114137Sgad * then it may also be true that the parent directory does not exist 2531114137Sgad * yet. Check, and create that directory if it is missing. 2532114137Sgad */ 2533114137Sgad if (ent->firstcreate) { 2534114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 2535114137Sgad slash = strrchr(tempfile, '/'); 2536114137Sgad if (slash != NULL) { 2537114137Sgad *slash = '\0'; 2538131581Ssobomax failed = stat(tempfile, &st); 2539114137Sgad if (failed && errno != ENOENT) 2540131581Ssobomax err(1, "Error on stat(%s)", tempfile); 2541114137Sgad if (failed) 2542114137Sgad createdir(ent, tempfile); 2543114137Sgad else if (!S_ISDIR(st.st_mode)) 2544114137Sgad errx(1, "%s exists but is not a directory", 2545114137Sgad tempfile); 2546114137Sgad } 2547114137Sgad } 2548114137Sgad 2549114137Sgad /* 2550114137Sgad * First create an unused filename, so it can be chown'ed and 2551114137Sgad * chmod'ed before it is moved into the real location. mkstemp 2552114137Sgad * will create the file mode=600 & owned by us. Note that all 2553114137Sgad * temp files will have a suffix of '.z<something>'. 2554114137Sgad */ 2555114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 2556114137Sgad strlcat(tempfile, ".zXXXXXX", sizeof(tempfile)); 2557114137Sgad if (noaction) 2558114137Sgad printf("\tmktemp %s\n", tempfile); 2559114137Sgad else { 2560114137Sgad fd = mkstemp(tempfile); 2561114137Sgad if (fd < 0) 2562114137Sgad err(1, "can't mkstemp logfile %s", tempfile); 2563114137Sgad 2564114137Sgad /* 2565114137Sgad * Add status message to what will become the new log file. 2566114137Sgad */ 2567114137Sgad if (!(ent->flags & CE_BINARY)) { 2568114137Sgad if (log_trim(tempfile, ent)) 2569114137Sgad err(1, "can't add status message to log"); 2570114137Sgad } 2571114137Sgad } 2572114137Sgad 2573114137Sgad /* Change the owner/group, if we are supposed to */ 2574114137Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 2575114137Sgad if (noaction) 2576114137Sgad printf("\tchown %u:%u %s\n", ent->uid, ent->gid, 2577114137Sgad tempfile); 2578114137Sgad else { 2579114137Sgad failed = fchown(fd, ent->uid, ent->gid); 2580114137Sgad if (failed) 2581114137Sgad err(1, "can't fchown temp file %s", tempfile); 2582114137Sgad } 2583114137Sgad } 2584114137Sgad 2585130043Sgad /* Turn on NODUMP if it was requested in the config-file. */ 2586130043Sgad if (ent->flags & CE_NODUMP) { 2587130043Sgad if (noaction) 2588130043Sgad printf("\tchflags nodump %s\n", tempfile); 2589130043Sgad else { 2590130043Sgad failed = fchflags(fd, UF_NODUMP); 2591130043Sgad if (failed) { 2592130043Sgad warn("log_trim: fchflags(NODUMP)"); 2593130043Sgad } 2594130043Sgad } 2595130043Sgad } 2596130043Sgad 2597114137Sgad /* 2598114137Sgad * Note that if the real logfile still exists, and if the call 2599114137Sgad * to rename() fails, then "neither the old file nor the new 2600114137Sgad * file shall be changed or created" (to quote the standard). 2601114137Sgad * If the call succeeds, then the file will be replaced without 2602114137Sgad * any window where some other process might find that the file 2603114137Sgad * did not exist. 2604114137Sgad * XXX - ? It may be that for some error conditions, we could 2605114137Sgad * retry by first removing the realfile and then renaming. 2606114137Sgad */ 2607114137Sgad if (noaction) { 2608114137Sgad printf("\tchmod %o %s\n", ent->permissions, tempfile); 2609114137Sgad printf("\tmv %s %s\n", tempfile, realfile); 2610114137Sgad } else { 2611114137Sgad failed = fchmod(fd, ent->permissions); 2612114137Sgad if (failed) 2613114137Sgad err(1, "can't fchmod temp file '%s'", tempfile); 2614114137Sgad failed = rename(tempfile, realfile); 2615114137Sgad if (failed) 2616114137Sgad err(1, "can't mv %s to %s", tempfile, realfile); 2617114137Sgad } 2618114137Sgad 2619114137Sgad if (fd >= 0) 2620114137Sgad close(fd); 2621114137Sgad} 2622130165Sgad 2623154566Sgad/* 2624154566Sgad * Change the attributes of a given filename to what was specified in 2625154566Sgad * the newsyslog.conf entry. This routine is only called for files 2626154566Sgad * that newsyslog expects that it has created, and thus it is a fatal 2627154566Sgad * error if this routine finds that the file does not exist. 2628154566Sgad */ 2629130165Sgadstatic void 2630130165Sgadchange_attrs(const char *fname, const struct conf_entry *ent) 2631130165Sgad{ 2632130165Sgad int failed; 2633130165Sgad 2634130165Sgad if (noaction) { 2635130165Sgad printf("\tchmod %o %s\n", ent->permissions, fname); 2636130165Sgad 2637130165Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 2638130165Sgad printf("\tchown %u:%u %s\n", 2639130165Sgad ent->uid, ent->gid, fname); 2640130165Sgad 2641130165Sgad if (ent->flags & CE_NODUMP) 2642130165Sgad printf("\tchflags nodump %s\n", fname); 2643130165Sgad return; 2644130165Sgad } 2645130165Sgad 2646130165Sgad failed = chmod(fname, ent->permissions); 2647154566Sgad if (failed) { 2648154566Sgad if (errno != EPERM) 2649154566Sgad err(1, "chmod(%s) in change_attrs", fname); 2650154566Sgad warn("change_attrs couldn't chmod(%s)", fname); 2651154566Sgad } 2652130165Sgad 2653130165Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 2654130165Sgad failed = chown(fname, ent->uid, ent->gid); 2655130165Sgad if (failed) 2656130165Sgad warn("can't chown %s", fname); 2657130165Sgad } 2658130165Sgad 2659130165Sgad if (ent->flags & CE_NODUMP) { 2660130165Sgad failed = chflags(fname, UF_NODUMP); 2661130165Sgad if (failed) 2662130165Sgad warn("can't chflags %s NODUMP", fname); 2663130165Sgad } 2664130165Sgad} 2665