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: releng/10.3/usr.sbin/newsyslog/newsyslog.c 293290 2016-01-07 00:40:51Z bdrewery $"); 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); 283290225Sbaptstatic int parse_signal(const char *str); 28413244Sgraichen 285111768Sgad/* 286129975Sgad * All the following take a parameter of 'int', but expect values in the 287129975Sgad * range of unsigned char. Define wrappers which take values of type 'char', 288129975Sgad * whether signed or unsigned, and ensure they end up in the right range. 289111768Sgad */ 290129975Sgad#define isdigitch(Anychar) isdigit((u_char)(Anychar)) 291129975Sgad#define isprintch(Anychar) isprint((u_char)(Anychar)) 292129975Sgad#define isspacech(Anychar) isspace((u_char)(Anychar)) 293129975Sgad#define tolowerch(Anychar) tolower((u_char)(Anychar)) 294111768Sgad 29559004Shmint 29659004Shmmain(int argc, char **argv) 29713244Sgraichen{ 298208648Sgordon struct cflist *worklist; 299208648Sgordon struct conf_entry *p; 300130167Sgad struct sigwork_entry *stmp; 301130167Sgad struct zipwork_entry *ztmp; 30225443Sache 303130167Sgad SLIST_INIT(&swhead); 304130167Sgad SLIST_INIT(&zwhead); 305130167Sgad 306111781Sgad parse_args(argc, argv); 307111773Sgad argc -= optind; 308111773Sgad argv += optind; 309111773Sgad 31059003Shm if (needroot && getuid() && geteuid()) 31159003Shm errx(1, "must have root privs"); 312208648Sgordon worklist = get_worklist(argv); 31359003Shm 314130165Sgad /* 315130165Sgad * Rotate all the files which need to be rotated. Note that 316130165Sgad * some users have *hundreds* of entries in newsyslog.conf! 317130165Sgad */ 318208648Sgordon while (!STAILQ_EMPTY(worklist)) { 319208648Sgordon p = STAILQ_FIRST(worklist); 320208648Sgordon STAILQ_REMOVE_HEAD(worklist, cf_nextp); 321208648Sgordon if (do_entry(p) == FREE_ENT) 322208648Sgordon free_entry(p); 32359003Shm } 324130165Sgad 325130167Sgad /* 326130167Sgad * Send signals to any processes which need a signal to tell 327130167Sgad * them to close and re-open the log file(s) we have rotated. 328130167Sgad * Note that zipwork_entries include pointers to these 329130167Sgad * sigwork_entry's, so we can not free the entries here. 330130167Sgad */ 331130167Sgad if (!SLIST_EMPTY(&swhead)) { 332130204Sgad if (noaction || verbose) 333130167Sgad printf("Signal all daemon process(es)...\n"); 334130167Sgad SLIST_FOREACH(stmp, &swhead, sw_nextp) 335130167Sgad do_sigwork(stmp); 336130204Sgad if (noaction) 337130204Sgad printf("\tsleep 10\n"); 338130204Sgad else { 339130204Sgad if (verbose) 340130204Sgad printf("Pause 10 seconds to allow daemon(s)" 341130204Sgad " to close log file(s)\n"); 342130204Sgad sleep(10); 343130204Sgad } 344130167Sgad } 345130167Sgad /* 346130167Sgad * Compress all files that we're expected to compress, now 347130167Sgad * that all processes should have closed the files which 348130167Sgad * have been rotated. 349130167Sgad */ 350130167Sgad if (!SLIST_EMPTY(&zwhead)) { 351130204Sgad if (noaction || verbose) 352130167Sgad printf("Compress all rotated log file(s)...\n"); 353130167Sgad while (!SLIST_EMPTY(&zwhead)) { 354130167Sgad ztmp = SLIST_FIRST(&zwhead); 355130167Sgad do_zipwork(ztmp); 356130167Sgad SLIST_REMOVE_HEAD(&zwhead, zw_nextp); 357130167Sgad free(ztmp); 358130167Sgad } 359130167Sgad } 360130167Sgad /* Now free all the sigwork entries. */ 361130167Sgad while (!SLIST_EMPTY(&swhead)) { 362130167Sgad stmp = SLIST_FIRST(&swhead); 363130167Sgad SLIST_REMOVE_HEAD(&swhead, sw_nextp); 364130167Sgad free(stmp); 365130167Sgad } 366130167Sgad 36795999Smaxim while (wait(NULL) > 0 || errno == EINTR) 36895999Smaxim ; 36959003Shm return (0); 37013244Sgraichen} 37113244Sgraichen 372111388Sgadstatic struct conf_entry * 373111388Sgadinit_entry(const char *fname, struct conf_entry *src_entry) 374111388Sgad{ 375111388Sgad struct conf_entry *tempwork; 376111388Sgad 377111388Sgad if (verbose > 4) 378111388Sgad printf("\t--> [creating entry for %s]\n", fname); 379111388Sgad 380111388Sgad tempwork = malloc(sizeof(struct conf_entry)); 381111388Sgad if (tempwork == NULL) 382111388Sgad err(1, "malloc of conf_entry for %s", fname); 383111388Sgad 384136174Sbrooks if (destdir == NULL || fname[0] != '/') 385136127Sbrooks tempwork->log = strdup(fname); 386136127Sbrooks else 387136127Sbrooks asprintf(&tempwork->log, "%s%s", destdir, fname); 388111388Sgad if (tempwork->log == NULL) 389111388Sgad err(1, "strdup for %s", fname); 390111388Sgad 391111388Sgad if (src_entry != NULL) { 392221873Ssobomax tempwork->pid_cmd_file = NULL; 393221873Ssobomax if (src_entry->pid_cmd_file) 394221873Ssobomax tempwork->pid_cmd_file = strdup(src_entry->pid_cmd_file); 395111772Sgad tempwork->r_reason = NULL; 396114137Sgad tempwork->firstcreate = 0; 397111772Sgad tempwork->rotate = 0; 398130165Sgad tempwork->fsize = -1; 399111388Sgad tempwork->uid = src_entry->uid; 400111388Sgad tempwork->gid = src_entry->gid; 401111388Sgad tempwork->numlogs = src_entry->numlogs; 402130165Sgad tempwork->trsize = src_entry->trsize; 403111388Sgad tempwork->hours = src_entry->hours; 404120361Sgad tempwork->trim_at = NULL; 405120361Sgad if (src_entry->trim_at != NULL) 406120361Sgad tempwork->trim_at = ptime_init(src_entry->trim_at); 407111388Sgad tempwork->permissions = src_entry->permissions; 408111388Sgad tempwork->flags = src_entry->flags; 409218127Smm tempwork->compress = src_entry->compress; 410111388Sgad tempwork->sig = src_entry->sig; 411111388Sgad tempwork->def_cfg = src_entry->def_cfg; 412111388Sgad } else { 413111388Sgad /* Initialize as a "do-nothing" entry */ 414221873Ssobomax tempwork->pid_cmd_file = NULL; 415111772Sgad tempwork->r_reason = NULL; 416114137Sgad tempwork->firstcreate = 0; 417111772Sgad tempwork->rotate = 0; 418130165Sgad tempwork->fsize = -1; 419111779Sgad tempwork->uid = (uid_t)-1; 420111779Sgad tempwork->gid = (gid_t)-1; 421111388Sgad tempwork->numlogs = 1; 422130165Sgad tempwork->trsize = -1; 423111388Sgad tempwork->hours = -1; 424120361Sgad tempwork->trim_at = NULL; 425111388Sgad tempwork->permissions = 0; 426111388Sgad tempwork->flags = 0; 427218127Smm tempwork->compress = COMPRESS_NONE; 428111388Sgad tempwork->sig = SIGHUP; 429111388Sgad tempwork->def_cfg = 0; 430111388Sgad } 431111388Sgad 432111388Sgad return (tempwork); 433111388Sgad} 434111388Sgad 43559004Shmstatic void 436111388Sgadfree_entry(struct conf_entry *ent) 437111388Sgad{ 438111388Sgad 439111388Sgad if (ent == NULL) 440111388Sgad return; 441111388Sgad 442111388Sgad if (ent->log != NULL) { 443111388Sgad if (verbose > 4) 444111388Sgad printf("\t--> [freeing entry for %s]\n", ent->log); 445111388Sgad free(ent->log); 446111388Sgad ent->log = NULL; 447111388Sgad } 448111388Sgad 449221873Ssobomax if (ent->pid_cmd_file != NULL) { 450221873Ssobomax free(ent->pid_cmd_file); 451221873Ssobomax ent->pid_cmd_file = NULL; 452111388Sgad } 453111388Sgad 454111772Sgad if (ent->r_reason != NULL) { 455111772Sgad free(ent->r_reason); 456111772Sgad ent->r_reason = NULL; 457111772Sgad } 458111772Sgad 459120361Sgad if (ent->trim_at != NULL) { 460120361Sgad ptime_free(ent->trim_at); 461120361Sgad ent->trim_at = NULL; 462120361Sgad } 463120361Sgad 464111388Sgad free(ent); 465111388Sgad} 466111388Sgad 467111388Sgadstatic void 468208648Sgordonfree_clist(struct cflist *list) 469112020Sgad{ 470208648Sgordon struct conf_entry *ent; 471112020Sgad 472208648Sgordon while (!STAILQ_EMPTY(list)) { 473208648Sgordon ent = STAILQ_FIRST(list); 474208648Sgordon STAILQ_REMOVE_HEAD(list, cf_nextp); 475112020Sgad free_entry(ent); 476112020Sgad } 477208648Sgordon 478208648Sgordon free(list); 479208648Sgordon list = NULL; 480112020Sgad} 481112020Sgad 482130165Sgadstatic fk_entry 48359004Shmdo_entry(struct conf_entry * ent) 48413244Sgraichen{ 485130045Sgad#define REASON_MAX 80 486130165Sgad int modtime; 487130165Sgad fk_entry free_or_keep; 488120361Sgad double diffsecs; 489111772Sgad char temp_reason[REASON_MAX]; 490233257Sglebius int oversized; 49159003Shm 492130165Sgad free_or_keep = FREE_ENT; 493218127Smm if (verbose) 494218127Smm printf("%s <%d%s>: ", ent->log, ent->numlogs, 495218127Smm compress_type[ent->compress].flag); 496130165Sgad ent->fsize = sizefile(ent->log); 497233257Sglebius oversized = ((ent->trsize > 0) && (ent->fsize >= ent->trsize)); 49859003Shm modtime = age_old_log(ent->log); 499111772Sgad ent->rotate = 0; 500114137Sgad ent->firstcreate = 0; 501130165Sgad if (ent->fsize < 0) { 502114137Sgad /* 503114137Sgad * If either the C flag or the -C option was specified, 504114137Sgad * and if we won't be creating the file, then have the 505114137Sgad * verbose message include a hint as to why the file 506114137Sgad * will not be created. 507114137Sgad */ 508114137Sgad temp_reason[0] = '\0'; 509114137Sgad if (createlogs > 1) 510114137Sgad ent->firstcreate = 1; 511114137Sgad else if ((ent->flags & CE_CREATE) && createlogs) 512114137Sgad ent->firstcreate = 1; 513114137Sgad else if (ent->flags & CE_CREATE) 514159998Sgad strlcpy(temp_reason, " (no -C option)", REASON_MAX); 515114137Sgad else if (createlogs) 516159998Sgad strlcpy(temp_reason, " (no C flag)", REASON_MAX); 517114137Sgad 518114137Sgad if (ent->firstcreate) { 519114137Sgad if (verbose) 520114137Sgad printf("does not exist -> will create.\n"); 521114137Sgad createlog(ent); 522114137Sgad } else if (verbose) { 523114137Sgad printf("does not exist, skipped%s.\n", temp_reason); 524114137Sgad } 52559003Shm } else { 526233257Sglebius if (ent->flags & CE_TRIMAT && !force && !rotatereq && 527233257Sglebius !oversized) { 528120361Sgad diffsecs = ptimeget_diff(timenow, ent->trim_at); 529120361Sgad if (diffsecs < 0.0) { 530120361Sgad /* trim_at is some time in the future. */ 531120361Sgad if (verbose) { 532120361Sgad ptime_adjust4dst(ent->trim_at, 533120361Sgad timenow); 53443071Swollman printf("--> will trim at %s", 535120361Sgad ptimeget_ctime(ent->trim_at)); 536120361Sgad } 537130165Sgad return (free_or_keep); 538120361Sgad } else if (diffsecs >= 3600.0) { 539120361Sgad /* 540120361Sgad * trim_at is more than an hour in the past, 541120361Sgad * so find the next valid trim_at time, and 542120361Sgad * tell the user what that will be. 543120361Sgad */ 544120361Sgad if (verbose && dbg_at_times) 545120361Sgad printf("\n\t--> prev trim at %s\t", 546120361Sgad ptimeget_ctime(ent->trim_at)); 547120361Sgad if (verbose) { 548120361Sgad ptimeset_nxtime(ent->trim_at); 549120361Sgad printf("--> will trim at %s", 550120361Sgad ptimeget_ctime(ent->trim_at)); 551120361Sgad } 552130165Sgad return (free_or_keep); 553120361Sgad } else if (verbose && noaction && dbg_at_times) { 554120361Sgad /* 555120361Sgad * If we are just debugging at-times, then 556120361Sgad * a detailed message is helpful. Also 557120361Sgad * skip "doing" any commands, since they 558120361Sgad * would all be turned off by no-action. 559120361Sgad */ 560120361Sgad printf("\n\t--> timematch at %s", 561120361Sgad ptimeget_ctime(ent->trim_at)); 562130165Sgad return (free_or_keep); 56343071Swollman } else if (verbose && ent->hours <= 0) { 56443071Swollman printf("--> time is up\n"); 56543071Swollman } 56643071Swollman } 567130165Sgad if (verbose && (ent->trsize > 0)) 568130165Sgad printf("size (Kb): %d [%d] ", ent->fsize, ent->trsize); 56959003Shm if (verbose && (ent->hours > 0)) 57059003Shm printf(" age (hr): %d [%d] ", modtime, ent->hours); 571111772Sgad 572111772Sgad /* 573111772Sgad * Figure out if this logfile needs to be rotated. 574111772Sgad */ 575111772Sgad temp_reason[0] = '\0'; 576111772Sgad if (rotatereq) { 577111772Sgad ent->rotate = 1; 578111772Sgad snprintf(temp_reason, REASON_MAX, " due to -R from %s", 579111772Sgad requestor); 580111772Sgad } else if (force) { 581111772Sgad ent->rotate = 1; 582111772Sgad snprintf(temp_reason, REASON_MAX, " due to -F request"); 583233257Sglebius } else if (oversized) { 584111772Sgad ent->rotate = 1; 585111772Sgad snprintf(temp_reason, REASON_MAX, " due to size>%dK", 586130165Sgad ent->trsize); 587111772Sgad } else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) { 588111772Sgad ent->rotate = 1; 589111772Sgad } else if ((ent->hours > 0) && ((modtime >= ent->hours) || 590111772Sgad (modtime < 0))) { 591111772Sgad ent->rotate = 1; 592111772Sgad } 593111772Sgad 594111772Sgad /* 595111772Sgad * If the file needs to be rotated, then rotate it. 596111772Sgad */ 597143106Sbrooks if (ent->rotate && !norotate) { 598111772Sgad if (temp_reason[0] != '\0') 599111772Sgad ent->r_reason = strdup(temp_reason); 60059003Shm if (verbose) 60159003Shm printf("--> trimming log....\n"); 602218127Smm if (noaction && !verbose) 603218127Smm printf("%s <%d%s>: trimming\n", ent->log, 604218127Smm ent->numlogs, 605218127Smm compress_type[ent->compress].flag); 606130165Sgad free_or_keep = do_rotate(ent); 60759003Shm } else { 60859003Shm if (verbose) 60959003Shm printf("--> skipping\n"); 61059003Shm } 61159003Shm } 612130165Sgad return (free_or_keep); 613111772Sgad#undef REASON_MAX 61413244Sgraichen} 61513244Sgraichen 61659004Shmstatic void 617111781Sgadparse_args(int argc, char **argv) 61813244Sgraichen{ 619111781Sgad int ch; 62059003Shm char *p; 62113244Sgraichen 622120361Sgad timenow = ptime_init(NULL); 623120361Sgad ptimeset_time(timenow, time(NULL)); 624159998Sgad strlcpy(daytime, ptimeget_ctime(timenow) + 4, DAYTIME_LEN); 62513244Sgraichen 62659003Shm /* Let's get our hostname */ 627111781Sgad (void)gethostname(hostname, sizeof(hostname)); 62813244Sgraichen 62913244Sgraichen /* Truncate domain */ 630111781Sgad if ((p = strchr(hostname, '.')) != NULL) 63113244Sgraichen *p = '\0'; 632111768Sgad 633111768Sgad /* Parse command line options. */ 634216832Sbrian while ((ch = getopt(argc, argv, "a:d:f:nrst:vCD:FNPR:S:")) != -1) 635111781Sgad switch (ch) { 63659004Shm case 'a': 63759004Shm archtodir++; 63859004Shm archdirname = optarg; 63959004Shm break; 640136127Sbrooks case 'd': 641136127Sbrooks destdir = optarg; 642136127Sbrooks break; 643111768Sgad case 'f': 644111768Sgad conf = optarg; 645111768Sgad break; 646111768Sgad case 'n': 647111768Sgad noaction++; 648244996Smarkj /* FALLTHROUGH */ 64959003Shm case 'r': 65059003Shm needroot = 0; 65159003Shm break; 652111768Sgad case 's': 653111768Sgad nosignal = 1; 654111768Sgad break; 655210372Ssimon case 't': 656210372Ssimon if (optarg[0] == '\0' || 657210372Ssimon strcmp(optarg, "DEFAULT") == 0) 658210372Ssimon timefnamefmt = strdup(DEFAULT_TIMEFNAME_FMT); 659210372Ssimon else 660210372Ssimon timefnamefmt = strdup(optarg); 661210372Ssimon break; 66259003Shm case 'v': 66359003Shm verbose++; 66459003Shm break; 665114137Sgad case 'C': 666114137Sgad /* Useful for things like rc.diskless... */ 667114137Sgad createlogs++; 668114137Sgad break; 669119904Sgad case 'D': 670119904Sgad /* 671119904Sgad * Set some debugging option. The specific option 672119904Sgad * depends on the value of optarg. These options 673119904Sgad * may come and go without notice or documentation. 674119904Sgad */ 675119904Sgad if (parse_doption(optarg)) 676119904Sgad break; 677119904Sgad usage(); 678119904Sgad /* NOTREACHED */ 67934584Spst case 'F': 68034584Spst force++; 68134584Spst break; 682143106Sbrooks case 'N': 683143106Sbrooks norotate++; 684143106Sbrooks break; 685202668Sdelphij case 'P': 686202668Sdelphij enforcepid++; 687202668Sdelphij break; 688111772Sgad case 'R': 689111772Sgad rotatereq++; 690111772Sgad requestor = strdup(optarg); 691111772Sgad break; 692210407Sbrian case 'S': 693210407Sbrian path_syslogpid = optarg; 694210407Sbrian break; 695111781Sgad case 'm': /* Used by OpenBSD for "monitor mode" */ 69659003Shm default: 69759003Shm usage(); 698111768Sgad /* NOTREACHED */ 69959003Shm } 700111772Sgad 701143106Sbrooks if (force && norotate) { 702143106Sbrooks warnx("Only one of -F and -N may be specified."); 703143106Sbrooks usage(); 704143106Sbrooks /* NOTREACHED */ 705143106Sbrooks } 706143106Sbrooks 707111772Sgad if (rotatereq) { 708111772Sgad if (optind == argc) { 709111772Sgad warnx("At least one filename must be given when -R is specified."); 710111772Sgad usage(); 711111772Sgad /* NOTREACHED */ 712111772Sgad } 713111772Sgad /* Make sure "requestor" value is safe for a syslog message. */ 714111772Sgad for (p = requestor; *p != '\0'; p++) { 715111772Sgad if (!isprintch(*p) && (*p != '\t')) 716111772Sgad *p = '.'; 717111772Sgad } 718111772Sgad } 719119904Sgad 720119904Sgad if (dbg_timenow) { 721119904Sgad /* 722119904Sgad * Note that the 'daytime' variable is not changed. 723119904Sgad * That is only used in messages that track when a 724119904Sgad * logfile is rotated, and if a file *is* rotated, 725119904Sgad * then it will still rotated at the "real now" time. 726119904Sgad */ 727120361Sgad ptime_free(timenow); 728119904Sgad timenow = dbg_timenow; 729119904Sgad fprintf(stderr, "Debug: Running as if TimeNow is %s", 730120361Sgad ptimeget_ctime(dbg_timenow)); 731119904Sgad } 732119904Sgad 73343071Swollman} 73413244Sgraichen 735120361Sgad/* 736120361Sgad * These debugging options are mainly meant for developer use, such 737120361Sgad * as writing regression-tests. They would not be needed by users 738120361Sgad * during normal operation of newsyslog... 739120361Sgad */ 740119904Sgadstatic int 741119904Sgadparse_doption(const char *doption) 742119904Sgad{ 743119904Sgad const char TN[] = "TN="; 744120361Sgad int res; 745119904Sgad 746119904Sgad if (strncmp(doption, TN, sizeof(TN) - 1) == 0) { 747119904Sgad /* 748120361Sgad * The "TimeNow" debugging option. This might be off 749120361Sgad * by an hour when crossing a timezone change. 750119904Sgad */ 751120361Sgad dbg_timenow = ptime_init(NULL); 752120361Sgad res = ptime_relparse(dbg_timenow, PTM_PARSE_ISO8601, 753120361Sgad time(NULL), doption + sizeof(TN) - 1); 754120361Sgad if (res == -2) { 755120361Sgad warnx("Non-existent time specified on -D %s", doption); 756120361Sgad return (0); /* failure */ 757120361Sgad } else if (res < 0) { 758119904Sgad warnx("Malformed time given on -D %s", doption); 759119904Sgad return (0); /* failure */ 760119904Sgad } 761119904Sgad return (1); /* successfully parsed */ 762119904Sgad 763119904Sgad } 764119904Sgad 765120361Sgad if (strcmp(doption, "ats") == 0) { 766120361Sgad dbg_at_times++; 767120361Sgad return (1); /* successfully parsed */ 768120361Sgad } 769120361Sgad 770159968Sgad /* XXX - This check could probably be dropped. */ 771159968Sgad if ((strcmp(doption, "neworder") == 0) || (strcmp(doption, "oldorder") 772159968Sgad == 0)) { 773159968Sgad warnx("NOTE: newsyslog always uses 'neworder'."); 774130167Sgad return (1); /* successfully parsed */ 775130167Sgad } 776130167Sgad 777130165Sgad warnx("Unknown -D (debug) option: '%s'", doption); 778119904Sgad return (0); /* failure */ 779119904Sgad} 780119904Sgad 78159004Shmstatic void 78259004Shmusage(void) 78313244Sgraichen{ 78480646Sobrien 78580646Sobrien fprintf(stderr, 786219434Sru "usage: newsyslog [-CFNPnrsv] [-a directory] [-d directory] [-f config_file]\n" 787219434Sru " [-S pidfile] [-t timefmt] [[-R tagname] file ...]\n"); 78859003Shm exit(1); 78913244Sgraichen} 79013244Sgraichen 79159004Shm/* 792111773Sgad * Parse a configuration file and return a linked list of all the logs 793111773Sgad * which should be processed. 79413244Sgraichen */ 795208648Sgordonstatic struct cflist * 796111773Sgadget_worklist(char **files) 79713244Sgraichen{ 79859003Shm FILE *f; 799111773Sgad char **given; 800208649Sgordon struct cflist *cmdlist, *filelist, *globlist; 801208648Sgordon struct conf_entry *defconf, *dupent, *ent; 802208649Sgordon struct ilist inclist; 803208649Sgordon struct include_entry *inc; 804112020Sgad int gmatch, fnres; 805111773Sgad 806208648Sgordon defconf = NULL; 807208649Sgordon STAILQ_INIT(&inclist); 808111773Sgad 809208648Sgordon filelist = malloc(sizeof(struct cflist)); 810208648Sgordon if (filelist == NULL) 811208648Sgordon err(1, "malloc of filelist"); 812208648Sgordon STAILQ_INIT(filelist); 813208648Sgordon globlist = malloc(sizeof(struct cflist)); 814208648Sgordon if (globlist == NULL) 815208648Sgordon err(1, "malloc of globlist"); 816208648Sgordon STAILQ_INIT(globlist); 817208648Sgordon 818208649Sgordon inc = malloc(sizeof(struct include_entry)); 819208649Sgordon if (inc == NULL) 820208649Sgordon err(1, "malloc of inc"); 821208649Sgordon inc->file = conf; 822208649Sgordon if (inc->file == NULL) 823208649Sgordon inc->file = _PATH_CONF; 824208649Sgordon STAILQ_INSERT_TAIL(&inclist, inc, inc_nextp); 825111773Sgad 826208649Sgordon STAILQ_FOREACH(inc, &inclist, inc_nextp) { 827208649Sgordon if (strcmp(inc->file, "-") != 0) 828208649Sgordon f = fopen(inc->file, "r"); 829208649Sgordon else { 830208649Sgordon f = stdin; 831208649Sgordon inc->file = "<stdin>"; 832208649Sgordon } 833208649Sgordon if (!f) 834208649Sgordon err(1, "%s", inc->file); 835208649Sgordon 836208649Sgordon if (verbose) 837208649Sgordon printf("Processing %s\n", inc->file); 838208649Sgordon parse_file(f, filelist, globlist, defconf, &inclist); 839208649Sgordon (void) fclose(f); 840111773Sgad } 841111773Sgad 842111773Sgad /* 843111773Sgad * All config-file information has been read in and turned into 844208648Sgordon * a filelist and a globlist. If there were no specific files 845112020Sgad * given on the run command, then the only thing left to do is to 846112020Sgad * call a routine which finds all files matched by the globlist 847208648Sgordon * and adds them to the filelist. Then return the worklist. 848111773Sgad */ 849111773Sgad if (*files == NULL) { 850208648Sgordon expand_globs(filelist, globlist); 851208648Sgordon free_clist(globlist); 852111773Sgad if (defconf != NULL) 853111773Sgad free_entry(defconf); 854208648Sgordon return (filelist); 855111773Sgad /* NOTREACHED */ 856111773Sgad } 857111773Sgad 858111773Sgad /* 859111773Sgad * If newsyslog was given a specific list of files to process, 860111773Sgad * it may be that some of those files were not listed in any 861111773Sgad * config file. Those unlisted files should get the default 862111773Sgad * rotation action. First, create the default-rotation action 863111773Sgad * if none was found in a system config file. 864111773Sgad */ 865111773Sgad if (defconf == NULL) { 866111773Sgad defconf = init_entry(DEFAULT_MARKER, NULL); 867111773Sgad defconf->numlogs = 3; 868130165Sgad defconf->trsize = 50; 869111773Sgad defconf->permissions = S_IRUSR|S_IWUSR; 870111773Sgad } 871111773Sgad 872111773Sgad /* 873111773Sgad * If newsyslog was run with a list of specific filenames, 874111773Sgad * then create a new worklist which has only those files in 875111773Sgad * it, picking up the rotation-rules for those files from 876208648Sgordon * the original filelist. 877111773Sgad * 878111773Sgad * XXX - Note that this will copy multiple rules for a single 879111773Sgad * logfile, if multiple entries are an exact match for 880111773Sgad * that file. That matches the historic behavior, but do 881111773Sgad * we want to continue to allow it? If so, it should 882111773Sgad * probably be handled more intelligently. 883111773Sgad */ 884208648Sgordon cmdlist = malloc(sizeof(struct cflist)); 885208648Sgordon if (cmdlist == NULL) 886208648Sgordon err(1, "malloc of cmdlist"); 887208648Sgordon STAILQ_INIT(cmdlist); 888208648Sgordon 889111773Sgad for (given = files; *given; ++given) { 890111773Sgad /* 891111773Sgad * First try to find exact-matches for this given file. 892111773Sgad */ 893112020Sgad gmatch = 0; 894208648Sgordon STAILQ_FOREACH(ent, filelist, cf_nextp) { 895111773Sgad if (strcmp(ent->log, *given) == 0) { 896111773Sgad gmatch++; 897111773Sgad dupent = init_entry(*given, ent); 898208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 899111773Sgad } 900111773Sgad } 901111773Sgad if (gmatch) { 902111773Sgad if (verbose > 2) 903111773Sgad printf("\t+ Matched entry %s\n", *given); 904111773Sgad continue; 905111773Sgad } 906111773Sgad 907111773Sgad /* 908111773Sgad * There was no exact-match for this given file, so look 909111773Sgad * for a "glob" entry which does match. 910111773Sgad */ 911112020Sgad gmatch = 0; 912112020Sgad if (verbose > 2 && globlist != NULL) 913112020Sgad printf("\t+ Checking globs for %s\n", *given); 914208648Sgordon STAILQ_FOREACH(ent, globlist, cf_nextp) { 915112020Sgad fnres = fnmatch(ent->log, *given, FNM_PATHNAME); 916112020Sgad if (verbose > 2) 917112020Sgad printf("\t+ = %d for pattern %s\n", fnres, 918112020Sgad ent->log); 919112020Sgad if (fnres == 0) { 920111773Sgad gmatch++; 921111773Sgad dupent = init_entry(*given, ent); 922112020Sgad /* This new entry is not a glob! */ 923111773Sgad dupent->flags &= ~CE_GLOB; 924208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 925111773Sgad /* Only allow a match to one glob-entry */ 926111773Sgad break; 927111773Sgad } 928111773Sgad } 929111773Sgad if (gmatch) { 930111773Sgad if (verbose > 2) 931111773Sgad printf("\t+ Matched %s via %s\n", *given, 932111773Sgad ent->log); 933111773Sgad continue; 934111773Sgad } 935111773Sgad 936111773Sgad /* 937111773Sgad * This given file was not found in any config file, so 938111773Sgad * add a worklist item based on the default entry. 939111773Sgad */ 940111773Sgad if (verbose > 2) 941111773Sgad printf("\t+ No entry matched %s (will use %s)\n", 942111773Sgad *given, DEFAULT_MARKER); 943111773Sgad dupent = init_entry(*given, defconf); 944111773Sgad /* Mark that it was *not* found in a config file */ 945111773Sgad dupent->def_cfg = 1; 946208648Sgordon STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 947111773Sgad } 948111773Sgad 949111773Sgad /* 950112020Sgad * Free all the entries in the original work list, the list of 951112020Sgad * glob entries, and the default entry. 952111773Sgad */ 953208648Sgordon free_clist(filelist); 954208648Sgordon free_clist(globlist); 955112020Sgad free_entry(defconf); 956111773Sgad 957112020Sgad /* And finally, return a worklist which matches the given files. */ 958208648Sgordon return (cmdlist); 959111773Sgad} 960111773Sgad 961111773Sgad/* 962112020Sgad * Expand the list of entries with filename patterns, and add all files 963112020Sgad * which match those glob-entries onto the worklist. 964112020Sgad */ 965112020Sgadstatic void 966208648Sgordonexpand_globs(struct cflist *work_p, struct cflist *glob_p) 967112020Sgad{ 968161412Sdelphij int gmatch, gres; 969161412Sdelphij size_t i; 970112020Sgad char *mfname; 971208648Sgordon struct conf_entry *dupent, *ent, *globent; 972112020Sgad glob_t pglob; 973112020Sgad struct stat st_fm; 974112020Sgad 975112020Sgad /* 976112020Sgad * The worklist contains all fully-specified (non-GLOB) names. 977112020Sgad * 978112020Sgad * Now expand the list of filename-pattern (GLOB) entries into 979112020Sgad * a second list, which (by definition) will only match files 980112020Sgad * that already exist. Do not add a glob-related entry for any 981112020Sgad * file which already exists in the fully-specified list. 982112020Sgad */ 983208648Sgordon STAILQ_FOREACH(globent, glob_p, cf_nextp) { 984112020Sgad gres = glob(globent->log, GLOB_NOCHECK, NULL, &pglob); 985112020Sgad if (gres != 0) { 986112020Sgad warn("cannot expand pattern (%d): %s", gres, 987112020Sgad globent->log); 988112020Sgad continue; 989112020Sgad } 990112020Sgad 991112020Sgad if (verbose > 2) 992112020Sgad printf("\t+ Expanding pattern %s\n", globent->log); 993112020Sgad for (i = 0; i < pglob.gl_matchc; i++) { 994112020Sgad mfname = pglob.gl_pathv[i]; 995112020Sgad 996112020Sgad /* See if this file already has a specific entry. */ 997112020Sgad gmatch = 0; 998208648Sgordon STAILQ_FOREACH(ent, work_p, cf_nextp) { 999112020Sgad if (strcmp(mfname, ent->log) == 0) { 1000112020Sgad gmatch++; 1001112020Sgad break; 1002112020Sgad } 1003112020Sgad } 1004112020Sgad if (gmatch) 1005112020Sgad continue; 1006112020Sgad 1007112020Sgad /* Make sure the named matched is a file. */ 1008112020Sgad gres = lstat(mfname, &st_fm); 1009112020Sgad if (gres != 0) { 1010112020Sgad /* Error on a file that glob() matched?!? */ 1011112020Sgad warn("Skipping %s - lstat() error", mfname); 1012112020Sgad continue; 1013112020Sgad } 1014112020Sgad if (!S_ISREG(st_fm.st_mode)) { 1015112020Sgad /* We only rotate files! */ 1016112020Sgad if (verbose > 2) 1017112020Sgad printf("\t+ . skipping %s (!file)\n", 1018112020Sgad mfname); 1019112020Sgad continue; 1020112020Sgad } 1021112020Sgad 1022112020Sgad if (verbose > 2) 1023112020Sgad printf("\t+ . add file %s\n", mfname); 1024112020Sgad dupent = init_entry(mfname, globent); 1025112020Sgad /* This new entry is not a glob! */ 1026112020Sgad dupent->flags &= ~CE_GLOB; 1027208648Sgordon 1028208648Sgordon /* Add to the worklist. */ 1029208648Sgordon STAILQ_INSERT_TAIL(work_p, dupent, cf_nextp); 1030112020Sgad } 1031112020Sgad globfree(&pglob); 1032112020Sgad if (verbose > 2) 1033112020Sgad printf("\t+ Done with pattern %s\n", globent->log); 1034112020Sgad } 1035112020Sgad} 1036112020Sgad 1037112020Sgad/* 1038111773Sgad * Parse a configuration file and update a linked list of all the logs to 1039111773Sgad * process. 1040111773Sgad */ 1041111773Sgadstatic void 1042208648Sgordonparse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p, 1043208649Sgordon struct conf_entry *defconf_p, struct ilist *inclist) 1044111773Sgad{ 104559003Shm char line[BUFSIZ], *parse, *q; 1046107737Ssobomax char *cp, *errline, *group; 1047208648Sgordon struct conf_entry *working; 1048111781Sgad struct passwd *pwd; 104959003Shm struct group *grp; 1050208649Sgordon glob_t pglob; 1051120361Sgad int eol, ptm_opts, res, special; 1052208649Sgordon size_t i; 105313244Sgraichen 1054130165Sgad errline = NULL; 1055111773Sgad while (fgets(line, BUFSIZ, cf)) { 1056107737Ssobomax if ((line[0] == '\n') || (line[0] == '#') || 1057107737Ssobomax (strlen(line) == 0)) 105859003Shm continue; 1059130165Sgad if (errline != NULL) 1060130165Sgad free(errline); 106159003Shm errline = strdup(line); 1062107737Ssobomax for (cp = line + 1; *cp != '\0'; cp++) { 1063107737Ssobomax if (*cp != '#') 1064107737Ssobomax continue; 1065107737Ssobomax if (*(cp - 1) == '\\') { 1066107737Ssobomax strcpy(cp - 1, cp); 1067107737Ssobomax cp--; 1068107737Ssobomax continue; 1069107737Ssobomax } 1070107737Ssobomax *cp = '\0'; 1071107737Ssobomax break; 1072107737Ssobomax } 107360373Sdes 107460373Sdes q = parse = missing_field(sob(line), errline); 107560373Sdes parse = son(line); 107660373Sdes if (!*parse) 107780646Sobrien errx(1, "malformed line (missing fields):\n%s", 107880646Sobrien errline); 107960373Sdes *parse = '\0'; 108060373Sdes 1081130165Sgad /* 1082130165Sgad * Allow people to set debug options via the config file. 1083208648Sgordon * (NOTE: debug options are undocumented, and may disappear 1084130165Sgad * at any time, etc). 1085130165Sgad */ 1086130165Sgad if (strcasecmp(DEBUG_MARKER, q) == 0) { 1087252378Skientzle q = parse = missing_field(sob(parse + 1), errline); 1088130165Sgad parse = son(parse); 1089130165Sgad if (!*parse) 1090130165Sgad warnx("debug line specifies no option:\n%s", 1091130165Sgad errline); 1092130165Sgad else { 1093130165Sgad *parse = '\0'; 1094130165Sgad parse_doption(q); 1095130165Sgad } 1096130165Sgad continue; 1097208649Sgordon } else if (strcasecmp(INCLUDE_MARKER, q) == 0) { 1098208649Sgordon if (verbose) 1099208649Sgordon printf("Found: %s", errline); 1100252378Skientzle q = parse = missing_field(sob(parse + 1), errline); 1101208649Sgordon parse = son(parse); 1102208649Sgordon if (!*parse) { 1103208649Sgordon warnx("include line missing argument:\n%s", 1104208649Sgordon errline); 1105208649Sgordon continue; 1106208649Sgordon } 1107208649Sgordon 1108208649Sgordon *parse = '\0'; 1109208649Sgordon 1110208649Sgordon if (isglobstr(q)) { 1111208649Sgordon res = glob(q, GLOB_NOCHECK, NULL, &pglob); 1112208649Sgordon if (res != 0) { 1113208649Sgordon warn("cannot expand pattern (%d): %s", 1114208649Sgordon res, q); 1115208649Sgordon continue; 1116208649Sgordon } 1117208649Sgordon 1118208649Sgordon if (verbose > 2) 1119208649Sgordon printf("\t+ Expanding pattern %s\n", q); 1120208649Sgordon 1121208649Sgordon for (i = 0; i < pglob.gl_matchc; i++) 1122208649Sgordon add_to_queue(pglob.gl_pathv[i], 1123208649Sgordon inclist); 1124208649Sgordon globfree(&pglob); 1125208649Sgordon } else 1126208649Sgordon add_to_queue(q, inclist); 1127208649Sgordon continue; 1128130165Sgad } 1129130165Sgad 1130112020Sgad special = 0; 1131111388Sgad working = init_entry(q, NULL); 1132111388Sgad if (strcasecmp(DEFAULT_MARKER, q) == 0) { 1133112020Sgad special = 1; 1134208648Sgordon if (defconf_p != NULL) { 1135111388Sgad warnx("Ignoring duplicate entry for %s!", q); 1136111388Sgad free_entry(working); 1137111388Sgad continue; 1138111388Sgad } 1139208648Sgordon defconf_p = working; 114059003Shm } 114113244Sgraichen 1142252378Skientzle q = parse = missing_field(sob(parse + 1), errline); 114359003Shm parse = son(parse); 114425518Sbrian if (!*parse) 114580646Sobrien errx(1, "malformed line (missing fields):\n%s", 114680646Sobrien errline); 114759003Shm *parse = '\0'; 114859003Shm if ((group = strchr(q, ':')) != NULL || 114959003Shm (group = strrchr(q, '.')) != NULL) { 115059003Shm *group++ = '\0'; 115159003Shm if (*q) { 1152119102Sgad if (!(isnumberstr(q))) { 1153111781Sgad if ((pwd = getpwnam(q)) == NULL) 115459003Shm errx(1, 115580646Sobrien "error in config file; unknown user:\n%s", 115659003Shm errline); 1157111781Sgad working->uid = pwd->pw_uid; 115859003Shm } else 115959003Shm working->uid = atoi(q); 116059003Shm } else 1161111779Sgad working->uid = (uid_t)-1; 116213244Sgraichen 116359003Shm q = group; 116459003Shm if (*q) { 1165119102Sgad if (!(isnumberstr(q))) { 116659003Shm if ((grp = getgrnam(q)) == NULL) 116759003Shm errx(1, 116880646Sobrien "error in config file; unknown group:\n%s", 116959003Shm errline); 117059003Shm working->gid = grp->gr_gid; 117159003Shm } else 117259003Shm working->gid = atoi(q); 117359003Shm } else 1174111779Sgad working->gid = (gid_t)-1; 117513244Sgraichen 1176252378Skientzle q = parse = missing_field(sob(parse + 1), errline); 117759003Shm parse = son(parse); 117859003Shm if (!*parse) 117980646Sobrien errx(1, "malformed line (missing fields):\n%s", 118080646Sobrien errline); 118159003Shm *parse = '\0'; 1182111779Sgad } else { 1183111779Sgad working->uid = (uid_t)-1; 1184111779Sgad working->gid = (gid_t)-1; 1185111779Sgad } 118659003Shm 118759003Shm if (!sscanf(q, "%o", &working->permissions)) 118859003Shm errx(1, "error in config file; bad permissions:\n%s", 118959003Shm errline); 119059003Shm 1191252378Skientzle q = parse = missing_field(sob(parse + 1), errline); 119259003Shm parse = son(parse); 119325518Sbrian if (!*parse) 119480646Sobrien errx(1, "malformed line (missing fields):\n%s", 119580646Sobrien errline); 119659003Shm *parse = '\0'; 1197111400Sgad if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0) 1198111400Sgad errx(1, "error in config file; bad value for count of logs to save:\n%s", 119959003Shm errline); 120013244Sgraichen 1201252378Skientzle q = parse = missing_field(sob(parse + 1), errline); 120259003Shm parse = son(parse); 120325518Sbrian if (!*parse) 120480646Sobrien errx(1, "malformed line (missing fields):\n%s", 120580646Sobrien errline); 120659003Shm *parse = '\0'; 1207114762Sgad if (isdigitch(*q)) 1208130165Sgad working->trsize = atoi(q); 1209130165Sgad else if (strcmp(q, "*") == 0) 1210130165Sgad working->trsize = -1; 1211114762Sgad else { 1212114762Sgad warnx("Invalid value of '%s' for 'size' in line:\n%s", 1213114762Sgad q, errline); 1214130165Sgad working->trsize = -1; 1215114762Sgad } 121659003Shm 121759003Shm working->flags = 0; 1218218127Smm working->compress = COMPRESS_NONE; 1219252378Skientzle q = parse = missing_field(sob(parse + 1), errline); 122059003Shm parse = son(parse); 122125518Sbrian eol = !*parse; 122259003Shm *parse = '\0'; 122343071Swollman { 122459003Shm char *ep; 122559003Shm u_long ul; 122613244Sgraichen 122743071Swollman ul = strtoul(q, &ep, 10); 122843071Swollman if (ep == q) 122943071Swollman working->hours = 0; 123043071Swollman else if (*ep == '*') 123143071Swollman working->hours = -1; 123243071Swollman else if (ul > INT_MAX) 123343071Swollman errx(1, "interval is too large:\n%s", errline); 123443071Swollman else 123543071Swollman working->hours = ul; 123643071Swollman 1237120361Sgad if (*ep == '\0' || strcmp(ep, "*") == 0) 1238120361Sgad goto no_trimat; 1239120361Sgad if (*ep != '@' && *ep != '$') 124043071Swollman errx(1, "malformed interval/at:\n%s", errline); 1241120361Sgad 1242120361Sgad working->flags |= CE_TRIMAT; 1243120361Sgad working->trim_at = ptime_init(NULL); 1244120361Sgad ptm_opts = PTM_PARSE_ISO8601; 1245120361Sgad if (*ep == '$') 1246120361Sgad ptm_opts = PTM_PARSE_DWM; 1247120361Sgad ptm_opts |= PTM_PARSE_MATCHDOM; 1248120361Sgad res = ptime_relparse(working->trim_at, ptm_opts, 1249120361Sgad ptimeget_secs(timenow), ep + 1); 1250120361Sgad if (res == -2) 1251120361Sgad errx(1, "nonexistent time for 'at' value:\n%s", 1252120361Sgad errline); 1253120361Sgad else if (res < 0) 1254120361Sgad errx(1, "malformed 'at' value:\n%s", errline); 125543071Swollman } 1256120361Sgadno_trimat: 125743071Swollman 125825518Sbrian if (eol) 125959003Shm q = NULL; 126025518Sbrian else { 1261252378Skientzle q = parse = sob(parse + 1); /* Optional field */ 126259003Shm parse = son(parse); 126359003Shm if (!*parse) 126459003Shm eol = 1; 126559003Shm *parse = '\0'; 126625518Sbrian } 126725443Sache 1268111768Sgad for (; q && *q && !isspacech(*q); q++) { 1269111768Sgad switch (tolowerch(*q)) { 1270111768Sgad case 'b': 127159003Shm working->flags |= CE_BINARY; 1272111768Sgad break; 1273114137Sgad case 'c': 1274111768Sgad /* 1275114137Sgad * XXX - Ick! Ugly! Remove ASAP! 1276114137Sgad * We want `c' and `C' for "create". But we 1277114137Sgad * will temporarily treat `c' as `g', because 1278114137Sgad * FreeBSD releases <= 4.8 have a typo of 1279114137Sgad * checking ('G' || 'c') for CE_GLOB. 1280111768Sgad */ 1281114137Sgad if (*q == 'c') { 1282114137Sgad warnx("Assuming 'g' for 'c' in flags for line:\n%s", 1283114137Sgad errline); 1284114137Sgad warnx("The 'c' flag will eventually mean 'CREATE'"); 1285114137Sgad working->flags |= CE_GLOB; 1286114137Sgad break; 1287114137Sgad } 1288114137Sgad working->flags |= CE_CREATE; 1289114137Sgad break; 1290130043Sgad case 'd': 1291130043Sgad working->flags |= CE_NODUMP; 1292130043Sgad break; 1293111768Sgad case 'g': 1294106905Ssobomax working->flags |= CE_GLOB; 1295111768Sgad break; 1296111768Sgad case 'j': 1297218127Smm working->compress = COMPRESS_BZIP2; 1298111768Sgad break; 1299111768Sgad case 'n': 1300111768Sgad working->flags |= CE_NOSIGNAL; 1301111768Sgad break; 1302221873Ssobomax case 'r': 1303221873Ssobomax working->flags |= CE_PID2CMD; 1304221873Ssobomax break; 1305112003Sgad case 'u': 1306112003Sgad working->flags |= CE_SIGNALGROUP; 1307112003Sgad break; 1308111768Sgad case 'w': 1309160560Ssobomax /* Depreciated flag - keep for compatibility purposes */ 1310111768Sgad break; 1311218127Smm case 'x': 1312218127Smm working->compress = COMPRESS_XZ; 1313218127Smm break; 1314111768Sgad case 'z': 1315218127Smm working->compress = COMPRESS_GZIP; 1316111768Sgad break; 1317111768Sgad case '-': 1318111768Sgad break; 1319111781Sgad case 'f': /* Used by OpenBSD for "CE_FOLLOW" */ 1320111781Sgad case 'm': /* Used by OpenBSD for "CE_MONITOR" */ 1321111781Sgad case 'p': /* Used by NetBSD for "CE_PLAIN0" */ 1322111768Sgad default: 132380646Sobrien errx(1, "illegal flag in config file -- %c", 132480646Sobrien *q); 1325111768Sgad } 132659003Shm } 132759003Shm 132825518Sbrian if (eol) 132959003Shm q = NULL; 133025518Sbrian else { 1331252378Skientzle q = parse = sob(parse + 1); /* Optional field */ 133259003Shm parse = son(parse); 133359003Shm if (!*parse) 133459003Shm eol = 1; 133559003Shm *parse = '\0'; 133625518Sbrian } 133725443Sache 1338221873Ssobomax working->pid_cmd_file = NULL; 133925443Sache if (q && *q) { 134025443Sache if (*q == '/') 1341221873Ssobomax working->pid_cmd_file = strdup(q); 1342290225Sbapt else if (isalnum(*q)) 134336817Sache goto got_sig; 1344290225Sbapt else { 134580646Sobrien errx(1, 1346290225Sbapt "illegal pid file or signal in config file:\n%s", 134780646Sobrien errline); 1348290225Sbapt } 134925443Sache } 135036817Sache if (eol) 135159003Shm q = NULL; 135236817Sache else { 1353252378Skientzle q = parse = sob(parse + 1); /* Optional field */ 135459003Shm *(parse = son(parse)) = '\0'; 135536817Sache } 135636817Sache 135736817Sache working->sig = SIGHUP; 135836817Sache if (q && *q) { 1359290225Sbaptgot_sig: 1360290225Sbapt working->sig = parse_signal(q); 1361290225Sbapt if (working->sig < 1 || working->sig >= sys_nsig) { 136280646Sobrien errx(1, 1363290225Sbapt "illegal signal in config file:\n%s", 136480646Sobrien errline); 136536817Sache } 136636817Sache } 1367111768Sgad 1368111768Sgad /* 1369111768Sgad * Finish figuring out what pid-file to use (if any) in 1370111768Sgad * later processing if this logfile needs to be rotated. 1371111768Sgad */ 1372111768Sgad if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) { 1373111768Sgad /* 1374111768Sgad * This config-entry specified 'n' for nosignal, 1375221873Ssobomax * see if it also specified an explicit pid_cmd_file. 1376111768Sgad * This would be a pretty pointless combination. 1377111768Sgad */ 1378221873Ssobomax if (working->pid_cmd_file != NULL) { 1379111768Sgad warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s", 1380221873Ssobomax working->pid_cmd_file, errline); 1381221873Ssobomax free(working->pid_cmd_file); 1382221873Ssobomax working->pid_cmd_file = NULL; 1383111768Sgad } 1384221873Ssobomax } else if (working->pid_cmd_file == NULL) { 1385111768Sgad /* 1386111768Sgad * This entry did not specify the 'n' flag, which 1387111768Sgad * means it should signal syslogd unless it had 1388112003Sgad * specified some other pid-file (and obviously the 1389112003Sgad * syslog pid-file will not be for a process-group). 1390112003Sgad * Also, we should only try to notify syslog if we 1391112003Sgad * are root. 1392111768Sgad */ 1393112003Sgad if (working->flags & CE_SIGNALGROUP) { 1394112003Sgad warnx("Ignoring flag 'U' in line:\n%s", 1395112003Sgad errline); 1396112003Sgad working->flags &= ~CE_SIGNALGROUP; 1397112003Sgad } 1398111768Sgad if (needroot) 1399221873Ssobomax working->pid_cmd_file = strdup(path_syslogpid); 1400111768Sgad } 1401111768Sgad 1402112020Sgad /* 1403112020Sgad * Add this entry to the appropriate list of entries, unless 1404112020Sgad * it was some kind of special entry (eg: <default>). 1405112020Sgad */ 1406112020Sgad if (special) { 1407112020Sgad ; /* Do not add to any list */ 1408112020Sgad } else if (working->flags & CE_GLOB) { 1409208648Sgordon STAILQ_INSERT_TAIL(glob_p, working, cf_nextp); 1410112020Sgad } else { 1411208648Sgordon STAILQ_INSERT_TAIL(work_p, working, cf_nextp); 1412112020Sgad } 1413130165Sgad } 1414130165Sgad if (errline != NULL) 141559003Shm free(errline); 141613244Sgraichen} 141713244Sgraichen 141859003Shmstatic char * 141959004Shmmissing_field(char *p, char *errline) 142013244Sgraichen{ 142180646Sobrien 142259003Shm if (!p || !*p) 142359003Shm errx(1, "missing field in config file:\n%s", errline); 142459003Shm return (p); 142513244Sgraichen} 142613244Sgraichen 1427208649Sgordon/* 1428210372Ssimon * In our sort we return it in the reverse of what qsort normally 1429210372Ssimon * would do, as we want the newest files first. If we have two 1430210372Ssimon * entries with the same time we don't really care about order. 1431210372Ssimon * 1432210372Ssimon * Support function for qsort() in delete_oldest_timelog(). 1433210372Ssimon */ 1434210372Ssimonstatic int 1435210372Ssimonoldlog_entry_compare(const void *a, const void *b) 1436210372Ssimon{ 1437210372Ssimon const struct oldlog_entry *ola = a, *olb = b; 1438210372Ssimon 1439210372Ssimon if (ola->t > olb->t) 1440210372Ssimon return (-1); 1441210372Ssimon else if (ola->t < olb->t) 1442210372Ssimon return (1); 1443210372Ssimon else 1444210372Ssimon return (0); 1445210372Ssimon} 1446210372Ssimon 1447210372Ssimon/* 1448248776Smarkj * Check whether the file corresponding to dp is an archive of the logfile 1449248776Smarkj * logfname, based on the timefnamefmt format string. Return true and fill out 1450248776Smarkj * tm if this is the case; otherwise return false. 1451248776Smarkj */ 1452248776Smarkjstatic int 1453250545Smarkjvalidate_old_timelog(int fd, const struct dirent *dp, const char *logfname, 1454250545Smarkj struct tm *tm) 1455248776Smarkj{ 1456250545Smarkj struct stat sb; 1457248776Smarkj size_t logfname_len; 1458248776Smarkj char *s; 1459248776Smarkj int c; 1460248776Smarkj 1461248776Smarkj logfname_len = strlen(logfname); 1462248776Smarkj 1463250545Smarkj if (dp->d_type != DT_REG) { 1464250545Smarkj /* 1465250545Smarkj * Some filesystems (e.g. NFS) don't fill out the d_type field 1466250545Smarkj * and leave it set to DT_UNKNOWN; in this case we must obtain 1467250545Smarkj * the file type ourselves. 1468250545Smarkj */ 1469250545Smarkj if (dp->d_type != DT_UNKNOWN || 1470250545Smarkj fstatat(fd, dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) != 0 || 1471250545Smarkj !S_ISREG(sb.st_mode)) 1472250545Smarkj return (0); 1473250545Smarkj } 1474248776Smarkj /* Ignore everything but files with our logfile prefix. */ 1475248776Smarkj if (strncmp(dp->d_name, logfname, logfname_len) != 0) 1476248776Smarkj return (0); 1477248776Smarkj /* Ignore the actual non-rotated logfile. */ 1478248776Smarkj if (dp->d_namlen == logfname_len) 1479248776Smarkj return (0); 1480248776Smarkj 1481248776Smarkj /* 1482248776Smarkj * Make sure we created have found a logfile, so the 1483248776Smarkj * postfix is valid, IE format is: '.<time>(.[bgx]z)?'. 1484248776Smarkj */ 1485248776Smarkj if (dp->d_name[logfname_len] != '.') { 1486248776Smarkj if (verbose) 1487248776Smarkj printf("Ignoring %s which has unexpected " 1488248776Smarkj "extension '%s'\n", dp->d_name, 1489248776Smarkj &dp->d_name[logfname_len]); 1490248776Smarkj return (0); 1491248776Smarkj } 1492261820Smarkj memset(tm, 0, sizeof(*tm)); 1493248776Smarkj if ((s = strptime(&dp->d_name[logfname_len + 1], 1494248776Smarkj timefnamefmt, tm)) == NULL) { 1495248776Smarkj /* 1496248776Smarkj * We could special case "old" sequentially named logfiles here, 1497248776Smarkj * but we do not as that would require special handling to 1498248776Smarkj * decide which one was the oldest compared to "new" time based 1499248776Smarkj * logfiles. 1500248776Smarkj */ 1501248776Smarkj if (verbose) 1502248776Smarkj printf("Ignoring %s which does not " 1503248776Smarkj "match time format\n", dp->d_name); 1504248776Smarkj return (0); 1505248776Smarkj } 1506248776Smarkj 1507248776Smarkj for (c = 0; c < COMPRESS_TYPES; c++) 1508248776Smarkj if (strcmp(s, compress_type[c].suffix) == 0) 1509248776Smarkj /* We're done. */ 1510248776Smarkj return (1); 1511248776Smarkj 1512248776Smarkj if (verbose) 1513248776Smarkj printf("Ignoring %s which has unexpected extension '%s'\n", 1514248776Smarkj dp->d_name, s); 1515248776Smarkj 1516248776Smarkj return (0); 1517248776Smarkj} 1518248776Smarkj 1519248776Smarkj/* 1520210372Ssimon * Delete the oldest logfiles, when using time based filenames. 1521210372Ssimon */ 1522210372Ssimonstatic void 1523210372Ssimondelete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir) 1524210372Ssimon{ 1525210372Ssimon char *logfname, *s, *dir, errbuf[80]; 1526248776Smarkj int dir_fd, i, logcnt, max_logcnt; 1527210372Ssimon struct oldlog_entry *oldlogs; 1528210372Ssimon struct dirent *dp; 1529210372Ssimon const char *cdir; 1530210372Ssimon struct tm tm; 1531210372Ssimon DIR *dirp; 1532210372Ssimon 1533210372Ssimon oldlogs = malloc(MAX_OLDLOGS * sizeof(struct oldlog_entry)); 1534210372Ssimon max_logcnt = MAX_OLDLOGS; 1535210372Ssimon logcnt = 0; 1536210372Ssimon 1537210372Ssimon if (archive_dir != NULL && archive_dir[0] != '\0') 1538210372Ssimon cdir = archive_dir; 1539210372Ssimon else 1540210372Ssimon if ((cdir = dirname(ent->log)) == NULL) 1541210372Ssimon err(1, "dirname()"); 1542210372Ssimon if ((dir = strdup(cdir)) == NULL) 1543210372Ssimon err(1, "strdup()"); 1544210372Ssimon 1545210372Ssimon if ((s = basename(ent->log)) == NULL) 1546210372Ssimon err(1, "basename()"); 1547210372Ssimon if ((logfname = strdup(s)) == NULL) 1548210372Ssimon err(1, "strdup()"); 1549210372Ssimon if (strcmp(logfname, "/") == 0) 1550210372Ssimon errx(1, "Invalid log filename - became '/'"); 1551210372Ssimon 1552210372Ssimon if (verbose > 2) 1553210372Ssimon printf("Searching for old logs in %s\n", dir); 1554210372Ssimon 1555210372Ssimon /* First we create a 'list' of all archived logfiles */ 1556210372Ssimon if ((dirp = opendir(dir)) == NULL) 1557210372Ssimon err(1, "Cannot open log directory '%s'", dir); 1558235647Sgleb dir_fd = dirfd(dirp); 1559210372Ssimon while ((dp = readdir(dirp)) != NULL) { 1560250545Smarkj if (validate_old_timelog(dir_fd, dp, logfname, &tm) == 0) 1561210372Ssimon continue; 1562210372Ssimon 1563210372Ssimon /* 1564210372Ssimon * We should now have old an old rotated logfile, so 1565210372Ssimon * add it to the 'list'. 1566210372Ssimon */ 1567210372Ssimon if ((oldlogs[logcnt].t = timegm(&tm)) == -1) 1568210372Ssimon err(1, "Could not convert time string to time value"); 1569210372Ssimon if ((oldlogs[logcnt].fname = strdup(dp->d_name)) == NULL) 1570210372Ssimon err(1, "strdup()"); 1571210372Ssimon logcnt++; 1572210372Ssimon 1573210372Ssimon /* 1574210372Ssimon * It is very unlikely we ever run out of space in the 1575210372Ssimon * logfile array from the default size, but lets 1576210372Ssimon * handle it anyway... 1577210372Ssimon */ 1578210372Ssimon if (logcnt >= max_logcnt) { 1579210372Ssimon max_logcnt *= 4; 1580210372Ssimon /* Detect integer overflow */ 1581210372Ssimon if (max_logcnt < logcnt) 1582210372Ssimon errx(1, "Too many old logfiles found"); 1583210372Ssimon oldlogs = realloc(oldlogs, 1584210372Ssimon max_logcnt * sizeof(struct oldlog_entry)); 1585210372Ssimon if (oldlogs == NULL) 1586210372Ssimon err(1, "realloc()"); 1587210372Ssimon } 1588210372Ssimon } 1589210372Ssimon 1590210372Ssimon /* Second, if needed we delete oldest archived logfiles */ 1591210372Ssimon if (logcnt > 0 && logcnt >= ent->numlogs && ent->numlogs > 1) { 1592210372Ssimon oldlogs = realloc(oldlogs, logcnt * 1593210372Ssimon sizeof(struct oldlog_entry)); 1594210372Ssimon if (oldlogs == NULL) 1595210372Ssimon err(1, "realloc()"); 1596210372Ssimon 1597210372Ssimon /* 1598210372Ssimon * We now sort the logs in the order of newest to 1599210372Ssimon * oldest. That way we can simply skip over the 1600210372Ssimon * number of records we want to keep. 1601210372Ssimon */ 1602210372Ssimon qsort(oldlogs, logcnt, sizeof(struct oldlog_entry), 1603210372Ssimon oldlog_entry_compare); 1604210372Ssimon for (i = ent->numlogs - 1; i < logcnt; i++) { 1605210372Ssimon if (noaction) 1606210372Ssimon printf("\trm -f %s/%s\n", dir, 1607210372Ssimon oldlogs[i].fname); 1608235647Sgleb else if (unlinkat(dir_fd, oldlogs[i].fname, 0) != 0) { 1609210372Ssimon snprintf(errbuf, sizeof(errbuf), 1610244995Smarkj "Could not delete old logfile '%s'", 1611210372Ssimon oldlogs[i].fname); 1612210372Ssimon perror(errbuf); 1613210372Ssimon } 1614210372Ssimon } 1615210372Ssimon } else if (verbose > 1) 1616210372Ssimon printf("No old logs to delete for logfile %s\n", ent->log); 1617210372Ssimon 1618210372Ssimon /* Third, cleanup */ 1619210372Ssimon closedir(dirp); 1620210372Ssimon for (i = 0; i < logcnt; i++) { 1621210372Ssimon assert(oldlogs[i].fname != NULL); 1622210372Ssimon free(oldlogs[i].fname); 1623210372Ssimon } 1624210372Ssimon free(oldlogs); 1625210372Ssimon free(logfname); 1626210372Ssimon free(dir); 1627210372Ssimon} 1628210372Ssimon 1629210372Ssimon/* 1630228990Suqs * Generate a log filename, when using classic filenames. 1631220926Ssimon */ 1632220926Ssimonstatic void 1633229654Suqsgen_classiclog_fname(char *fname, size_t fname_sz, const char *archive_dir, 1634220926Ssimon const char *namepart, int numlogs_c) 1635220926Ssimon{ 1636220926Ssimon 1637220926Ssimon if (archive_dir[0] != '\0') 1638220926Ssimon (void) snprintf(fname, fname_sz, "%s/%s.%d", archive_dir, 1639220926Ssimon namepart, numlogs_c); 1640220926Ssimon else 1641220926Ssimon (void) snprintf(fname, fname_sz, "%s.%d", namepart, numlogs_c); 1642220926Ssimon} 1643220926Ssimon 1644220926Ssimon/* 1645229654Suqs * Delete a rotated logfile, when using classic filenames. 1646220926Ssimon */ 1647220926Ssimonstatic void 1648229654Suqsdelete_classiclog(const char *archive_dir, const char *namepart, int numlog_c) 1649220926Ssimon{ 1650220926Ssimon char file1[MAXPATHLEN], zfile1[MAXPATHLEN]; 1651220926Ssimon int c; 1652220926Ssimon 1653229654Suqs gen_classiclog_fname(file1, sizeof(file1), archive_dir, namepart, 1654220926Ssimon numlog_c); 1655220926Ssimon 1656220926Ssimon for (c = 0; c < COMPRESS_TYPES; c++) { 1657220926Ssimon (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, 1658220926Ssimon compress_type[c].suffix); 1659220926Ssimon if (noaction) 1660220926Ssimon printf("\trm -f %s\n", zfile1); 1661220926Ssimon else 1662220926Ssimon (void) unlink(zfile1); 1663220926Ssimon } 1664220926Ssimon} 1665220926Ssimon 1666220926Ssimon/* 1667208649Sgordon * Only add to the queue if the file hasn't already been added. This is 1668208649Sgordon * done to prevent circular include loops. 1669208649Sgordon */ 1670208649Sgordonstatic void 1671208649Sgordonadd_to_queue(const char *fname, struct ilist *inclist) 1672208649Sgordon{ 1673208649Sgordon struct include_entry *inc; 1674208649Sgordon 1675208649Sgordon STAILQ_FOREACH(inc, inclist, inc_nextp) { 1676208649Sgordon if (strcmp(fname, inc->file) == 0) { 1677208649Sgordon warnx("duplicate include detected: %s", fname); 1678208649Sgordon return; 1679208649Sgordon } 1680208649Sgordon } 1681208649Sgordon 1682208649Sgordon inc = malloc(sizeof(struct include_entry)); 1683208649Sgordon if (inc == NULL) 1684208649Sgordon err(1, "malloc of inc"); 1685208649Sgordon inc->file = strdup(fname); 1686208649Sgordon 1687208649Sgordon if (verbose > 2) 1688208649Sgordon printf("\t+ Adding %s to the processing queue.\n", fname); 1689208649Sgordon 1690208649Sgordon STAILQ_INSERT_TAIL(inclist, inc, inc_nextp); 1691208649Sgordon} 1692208649Sgordon 1693218127Smm/* 1694218127Smm * Search for logfile and return its compression suffix (if supported) 1695218127Smm * The suffix detection is first-match in the order of compress_types 1696218127Smm * 1697218127Smm * Note: if logfile without suffix exists (uncompressed, COMPRESS_NONE) 1698218127Smm * a zero-length string is returned 1699218127Smm */ 1700218127Smmstatic const char * 1701218127Smmget_logfile_suffix(const char *logfile) 1702218127Smm{ 1703218127Smm struct stat st; 1704218127Smm char zfile[MAXPATHLEN]; 1705218944Ssobomax int c; 1706218127Smm 1707218944Ssobomax for (c = 0; c < COMPRESS_TYPES; c++) { 1708218127Smm (void) strlcpy(zfile, logfile, MAXPATHLEN); 1709218127Smm (void) strlcat(zfile, compress_type[c].suffix, MAXPATHLEN); 1710218127Smm if (lstat(zfile, &st) == 0) 1711218127Smm return (compress_type[c].suffix); 1712218127Smm } 1713218127Smm return (NULL); 1714218127Smm} 1715218127Smm 1716130165Sgadstatic fk_entry 1717130165Sgaddo_rotate(const struct conf_entry *ent) 171813244Sgraichen{ 171971299Sjedgar char dirpart[MAXPATHLEN], namepart[MAXPATHLEN]; 172071299Sjedgar char file1[MAXPATHLEN], file2[MAXPATHLEN]; 172171299Sjedgar char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN]; 1722218127Smm const char *logfile_suffix; 1723210372Ssimon char datetimestr[30]; 1724159968Sgad int flags, numlogs_c; 1725130165Sgad fk_entry free_or_keep; 1726159968Sgad struct sigwork_entry *swork; 172759003Shm struct stat st; 1728210372Ssimon struct tm tm; 1729210372Ssimon time_t now; 173013244Sgraichen 1731119927Sgad flags = ent->flags; 1732130165Sgad free_or_keep = FREE_ENT; 1733119927Sgad 173459004Shm if (archtodir) { 173559004Shm char *p; 173613244Sgraichen 173759004Shm /* build complete name of archive directory into dirpart */ 173859004Shm if (*archdirname == '/') { /* absolute */ 173971299Sjedgar strlcpy(dirpart, archdirname, sizeof(dirpart)); 174059004Shm } else { /* relative */ 174159004Shm /* get directory part of logfile */ 1742119927Sgad strlcpy(dirpart, ent->log, sizeof(dirpart)); 1743229403Sed if ((p = strrchr(dirpart, '/')) == NULL) 174459004Shm dirpart[0] = '\0'; 174559004Shm else 174659004Shm *(p + 1) = '\0'; 174771299Sjedgar strlcat(dirpart, archdirname, sizeof(dirpart)); 174859004Shm } 174959004Shm 175059004Shm /* check if archive directory exists, if not, create it */ 175159004Shm if (lstat(dirpart, &st)) 1752114137Sgad createdir(ent, dirpart); 175359004Shm 175459004Shm /* get filename part of logfile */ 1755229403Sed if ((p = strrchr(ent->log, '/')) == NULL) 1756119927Sgad strlcpy(namepart, ent->log, sizeof(namepart)); 175759004Shm else 175871299Sjedgar strlcpy(namepart, p + 1, sizeof(namepart)); 175959004Shm } else { 1760210372Ssimon /* 1761220926Ssimon * Tell utility functions we are not using an archive 1762220926Ssimon * dir. 1763210372Ssimon */ 1764210372Ssimon dirpart[0] = '\0'; 1765220926Ssimon strlcpy(namepart, ent->log, sizeof(namepart)); 1766210372Ssimon } 1767210372Ssimon 1768210372Ssimon /* Delete old logs */ 1769210372Ssimon if (timefnamefmt != NULL) 1770210372Ssimon delete_oldest_timelog(ent, dirpart); 1771210372Ssimon else { 1772220926Ssimon /* 1773220926Ssimon * Handle cleaning up after legacy newsyslog where we 1774220926Ssimon * kept ent->numlogs + 1 files. This code can go away 1775220926Ssimon * at some point in the future. 1776220926Ssimon */ 1777229654Suqs delete_classiclog(dirpart, namepart, ent->numlogs); 1778220926Ssimon 1779220926Ssimon if (ent->numlogs > 0) 1780229654Suqs delete_classiclog(dirpart, namepart, ent->numlogs - 1); 1781220926Ssimon 178259003Shm } 178313244Sgraichen 1784210372Ssimon if (timefnamefmt != NULL) { 1785210372Ssimon /* If time functions fails we can't really do any sensible */ 1786210372Ssimon if (time(&now) == (time_t)-1 || 1787210372Ssimon localtime_r(&now, &tm) == NULL) 1788210372Ssimon bzero(&tm, sizeof(tm)); 1789210372Ssimon 1790210372Ssimon strftime(datetimestr, sizeof(datetimestr), timefnamefmt, &tm); 1791210372Ssimon if (archtodir) 1792210372Ssimon (void) snprintf(file1, sizeof(file1), "%s/%s.%s", 1793210372Ssimon dirpart, namepart, datetimestr); 1794210372Ssimon else 1795210372Ssimon (void) snprintf(file1, sizeof(file1), "%s.%s", 1796210372Ssimon ent->log, datetimestr); 1797210372Ssimon 1798210372Ssimon /* Don't run the code to move down logs */ 1799220926Ssimon numlogs_c = -1; 1800220926Ssimon } else { 1801229654Suqs gen_classiclog_fname(file1, sizeof(file1), dirpart, namepart, 1802220926Ssimon ent->numlogs - 1); 1803220926Ssimon numlogs_c = ent->numlogs - 2; /* copy for countdown */ 1804220926Ssimon } 1805210372Ssimon 180659003Shm /* Move down log files */ 1807220926Ssimon for (; numlogs_c >= 0; numlogs_c--) { 180871299Sjedgar (void) strlcpy(file2, file1, sizeof(file2)); 180959004Shm 1810229654Suqs gen_classiclog_fname(file1, sizeof(file1), dirpart, namepart, 1811220926Ssimon numlogs_c); 181259004Shm 1813218127Smm logfile_suffix = get_logfile_suffix(file1); 1814218127Smm if (logfile_suffix == NULL) 1815218127Smm continue; 1816218127Smm (void) strlcpy(zfile1, file1, MAXPATHLEN); 1817218127Smm (void) strlcpy(zfile2, file2, MAXPATHLEN); 1818218127Smm (void) strlcat(zfile1, logfile_suffix, MAXPATHLEN); 1819218127Smm (void) strlcat(zfile2, logfile_suffix, MAXPATHLEN); 1820218127Smm 1821130165Sgad if (noaction) 1822111967Sgad printf("\tmv %s %s\n", zfile1, zfile2); 1823130165Sgad else { 1824130165Sgad /* XXX - Ought to be checking for failure! */ 1825130165Sgad (void)rename(zfile1, zfile2); 182659003Shm } 1827130165Sgad change_attrs(zfile2, ent); 182859003Shm } 182913244Sgraichen 1830130038Sgad if (ent->numlogs > 0) { 1831129974Sgad if (noaction) { 1832130038Sgad /* 1833130038Sgad * Note that savelog() may succeed with using link() 1834130038Sgad * for the archtodir case, but there is no good way 1835130038Sgad * of knowing if it will when doing "noaction", so 1836130038Sgad * here we claim that it will have to do a copy... 1837130038Sgad */ 1838130038Sgad if (archtodir) 1839130038Sgad printf("\tcp %s %s\n", ent->log, file1); 1840130038Sgad else 1841130038Sgad printf("\tln %s %s\n", ent->log, file1); 1842244997Smarkj printf("\ttouch %s\t\t" 1843244997Smarkj "# Update mtime for 'when'-interval processing\n", 1844244997Smarkj file1); 1845130165Sgad } else { 1846130038Sgad if (!(flags & CE_BINARY)) { 1847130038Sgad /* Report the trimming to the old log */ 1848130038Sgad log_trim(ent->log, ent); 1849130038Sgad } 1850130038Sgad savelog(ent->log, file1); 1851244997Smarkj /* 1852244997Smarkj * Interval-based rotations are done using the mtime of 1853244997Smarkj * the most recently archived log, so make sure it gets 1854244997Smarkj * updated during a rotation. 1855244997Smarkj */ 1856244997Smarkj utimes(file1, NULL); 185759004Shm } 1858130165Sgad change_attrs(file1, ent); 185918075Sjkh } 186018075Sjkh 1861130038Sgad /* Create the new log file and move it into place */ 1862130038Sgad if (noaction) 1863111781Sgad printf("Start new log...\n"); 1864130038Sgad createlog(ent); 186525443Sache 1866111966Sgad /* 1867130167Sgad * Save all signalling and file-compression to be done after log 1868130167Sgad * files from all entries have been rotated. This way any one 1869130167Sgad * process will not be sent the same signal multiple times when 1870130167Sgad * multiple log files had to be rotated. 1871130167Sgad */ 1872159968Sgad swork = NULL; 1873221873Ssobomax if (ent->pid_cmd_file != NULL) 1874159968Sgad swork = save_sigwork(ent); 1875218127Smm if (ent->numlogs > 0 && ent->compress > COMPRESS_NONE) { 1876159968Sgad /* 1877159968Sgad * The zipwork_entry will include a pointer to this 1878159968Sgad * conf_entry, so the conf_entry should not be freed. 1879159968Sgad */ 1880159968Sgad free_or_keep = KEEP_ENT; 1881159968Sgad save_zipwork(ent, swork, ent->fsize, file1); 1882130167Sgad } 1883130167Sgad 1884130165Sgad return (free_or_keep); 188513244Sgraichen} 188613244Sgraichen 1887130167Sgadstatic void 1888130167Sgaddo_sigwork(struct sigwork_entry *swork) 1889130167Sgad{ 1890130204Sgad struct sigwork_entry *nextsig; 1891130204Sgad int kres, secs; 1892221873Ssobomax char *tmp; 1893130167Sgad 1894245963Smarkj if (swork->sw_runcmd == 0 && (!(swork->sw_pidok) || swork->sw_pid == 0)) 1895130167Sgad return; /* no work to do... */ 1896130167Sgad 1897130167Sgad /* 1898130167Sgad * If nosignal (-s) was specified, then do not signal any process. 1899130167Sgad * Note that a nosignal request triggers a warning message if the 1900130167Sgad * rotated logfile needs to be compressed, *unless* -R was also 1901130167Sgad * specified. We assume that an `-sR' request came from a process 1902130167Sgad * which writes to the logfile, and as such, we assume that process 1903130167Sgad * has already made sure the logfile is not presently in use. This 1904130167Sgad * just sets swork->sw_pidok to a special value, and do_zipwork 1905130167Sgad * will print any necessary warning(s). 1906130167Sgad */ 1907130167Sgad if (nosignal) { 1908130167Sgad if (!rotatereq) 1909130167Sgad swork->sw_pidok = -1; 1910130167Sgad return; 1911130167Sgad } 1912130167Sgad 1913130204Sgad /* 1914130204Sgad * Compute the pause between consecutive signals. Use a longer 1915130204Sgad * sleep time if we will be sending two signals to the same 1916293290Sbdrewery * daemon or process-group. 1917130204Sgad */ 1918130204Sgad secs = 0; 1919130204Sgad nextsig = SLIST_NEXT(swork, sw_nextp); 1920130204Sgad if (nextsig != NULL) { 1921130204Sgad if (swork->sw_pid == nextsig->sw_pid) 1922130204Sgad secs = 10; 1923130204Sgad else 1924130204Sgad secs = 1; 1925130204Sgad } 1926130204Sgad 1927130167Sgad if (noaction) { 1928245963Smarkj if (swork->sw_runcmd) 1929245962Smarkj printf("\tsh -c '%s %d'\n", swork->sw_fname, 1930245962Smarkj swork->sw_signum); 1931245962Smarkj else { 1932245962Smarkj printf("\tkill -%d %d \t\t# %s\n", swork->sw_signum, 1933245962Smarkj (int)swork->sw_pid, swork->sw_fname); 1934245962Smarkj if (secs > 0) 1935245962Smarkj printf("\tsleep %d\n", secs); 1936245962Smarkj } 1937130167Sgad return; 1938130167Sgad } 1939130167Sgad 1940245963Smarkj if (swork->sw_runcmd) { 1941221873Ssobomax asprintf(&tmp, "%s %d", swork->sw_fname, swork->sw_signum); 1942221873Ssobomax if (tmp == NULL) { 1943221873Ssobomax warn("can't allocate memory to run %s", 1944221873Ssobomax swork->sw_fname); 1945221873Ssobomax return; 1946221873Ssobomax } 1947221873Ssobomax if (verbose) 1948221873Ssobomax printf("Run command: %s\n", tmp); 1949221873Ssobomax kres = system(tmp); 1950221873Ssobomax if (kres) { 1951221873Ssobomax warnx("%s: returned non-zero exit code: %d", 1952221873Ssobomax tmp, kres); 1953221873Ssobomax } 1954221873Ssobomax free(tmp); 1955221873Ssobomax return; 1956221873Ssobomax } 1957221873Ssobomax 1958130167Sgad kres = kill(swork->sw_pid, swork->sw_signum); 1959130167Sgad if (kres != 0) { 1960130167Sgad /* 1961130167Sgad * Assume that "no such process" (ESRCH) is something 1962130167Sgad * to warn about, but is not an error. Presumably the 1963130167Sgad * process which writes to the rotated log file(s) is 1964130167Sgad * gone, in which case we should have no problem with 1965130167Sgad * compressing the rotated log file(s). 1966130167Sgad */ 1967130167Sgad if (errno != ESRCH) 1968130167Sgad swork->sw_pidok = 0; 1969273554Smarkj warn("can't notify %s, pid %d = %s", swork->sw_pidtype, 1970273554Smarkj (int)swork->sw_pid, swork->sw_fname); 1971130167Sgad } else { 1972130204Sgad if (verbose) 1973130204Sgad printf("Notified %s pid %d = %s\n", swork->sw_pidtype, 1974130204Sgad (int)swork->sw_pid, swork->sw_fname); 1975130204Sgad if (secs > 0) { 1976130204Sgad if (verbose) 1977130204Sgad printf("Pause %d second(s) between signals\n", 1978130204Sgad secs); 1979130204Sgad sleep(secs); 1980130167Sgad } 1981130167Sgad } 1982130167Sgad} 1983130167Sgad 1984130167Sgadstatic void 1985130167Sgaddo_zipwork(struct zipwork_entry *zwork) 1986130167Sgad{ 1987130167Sgad const char *pgm_name, *pgm_path; 1988154566Sgad int errsav, fcount, zstatus; 1989130167Sgad pid_t pidzip, wpid; 1990130167Sgad char zresult[MAXPATHLEN]; 1991218944Ssobomax int c; 1992130167Sgad 1993228790Seadler assert(zwork != NULL); 1994130167Sgad pgm_path = NULL; 1995130167Sgad strlcpy(zresult, zwork->zw_fname, sizeof(zresult)); 1996228790Seadler if (zwork->zw_conf != NULL && 1997218127Smm zwork->zw_conf->compress > COMPRESS_NONE) 1998218944Ssobomax for (c = 1; c < COMPRESS_TYPES; c++) { 1999218127Smm if (zwork->zw_conf->compress == c) { 2000218127Smm pgm_path = compress_type[c].path; 2001218127Smm (void) strlcat(zresult, 2002218127Smm compress_type[c].suffix, sizeof(zresult)); 2003218127Smm break; 2004218127Smm } 2005130167Sgad } 2006130167Sgad if (pgm_path == NULL) { 2007130167Sgad warnx("invalid entry for %s in do_zipwork", zwork->zw_fname); 2008130167Sgad return; 2009130167Sgad } 2010154566Sgad pgm_name = strrchr(pgm_path, '/'); 2011154566Sgad if (pgm_name == NULL) 2012154566Sgad pgm_name = pgm_path; 2013154566Sgad else 2014154566Sgad pgm_name++; 2015130167Sgad 2016245963Smarkj if (zwork->zw_swork != NULL && zwork->zw_swork->sw_runcmd == 0 && 2017238281Sae zwork->zw_swork->sw_pidok <= 0) { 2018130167Sgad warnx( 2019130167Sgad "log %s not compressed because daemon(s) not notified", 2020130167Sgad zwork->zw_fname); 2021130167Sgad change_attrs(zwork->zw_fname, zwork->zw_conf); 2022130167Sgad return; 2023130167Sgad } 2024130167Sgad 2025130167Sgad if (noaction) { 2026130167Sgad printf("\t%s %s\n", pgm_name, zwork->zw_fname); 2027130167Sgad change_attrs(zresult, zwork->zw_conf); 2028130167Sgad return; 2029130167Sgad } 2030130167Sgad 2031154566Sgad fcount = 1; 2032130167Sgad pidzip = fork(); 2033154566Sgad while (pidzip < 0) { 2034154566Sgad /* 2035154566Sgad * The fork failed. If the failure was due to a temporary 2036154566Sgad * problem, then wait a short time and try it again. 2037154566Sgad */ 2038154566Sgad errsav = errno; 2039154566Sgad warn("fork() for `%s %s'", pgm_name, zwork->zw_fname); 2040154566Sgad if (errsav != EAGAIN || fcount > 5) 2041154566Sgad errx(1, "Exiting..."); 2042154566Sgad sleep(fcount * 12); 2043154566Sgad fcount++; 2044154566Sgad pidzip = fork(); 2045154566Sgad } 2046154566Sgad if (!pidzip) { 2047130167Sgad /* The child process executes the compression command */ 2048130167Sgad execl(pgm_path, pgm_path, "-f", zwork->zw_fname, (char *)0); 2049154566Sgad err(1, "execl(`%s -f %s')", pgm_path, zwork->zw_fname); 2050130167Sgad } 2051130167Sgad 2052130167Sgad wpid = waitpid(pidzip, &zstatus, 0); 2053130167Sgad if (wpid == -1) { 2054154566Sgad /* XXX - should this be a fatal error? */ 2055130167Sgad warn("%s: waitpid(%d)", pgm_path, pidzip); 2056130167Sgad return; 2057130167Sgad } 2058130167Sgad if (!WIFEXITED(zstatus)) { 2059154566Sgad warnx("`%s -f %s' did not terminate normally", pgm_name, 2060154566Sgad zwork->zw_fname); 2061130167Sgad return; 2062130167Sgad } 2063130167Sgad if (WEXITSTATUS(zstatus)) { 2064154566Sgad warnx("`%s -f %s' terminated with a non-zero status (%d)", 2065154566Sgad pgm_name, zwork->zw_fname, WEXITSTATUS(zstatus)); 2066130167Sgad return; 2067130167Sgad } 2068130167Sgad 2069130167Sgad /* Compression was successful, set file attributes on the result. */ 2070130167Sgad change_attrs(zresult, zwork->zw_conf); 2071130167Sgad} 2072130167Sgad 2073130167Sgad/* 2074130167Sgad * Save information on any process we need to signal. Any single 2075130167Sgad * process may need to be sent different signal-values for different 2076130167Sgad * log files, but usually a single signal-value will cause the process 2077130167Sgad * to close and re-open all of it's log files. 2078130167Sgad */ 2079130167Sgadstatic struct sigwork_entry * 2080130167Sgadsave_sigwork(const struct conf_entry *ent) 2081130167Sgad{ 2082130167Sgad struct sigwork_entry *sprev, *stmp; 2083130167Sgad int ndiff; 2084130167Sgad size_t tmpsiz; 2085130167Sgad 2086130167Sgad sprev = NULL; 2087130167Sgad ndiff = 1; 2088130167Sgad SLIST_FOREACH(stmp, &swhead, sw_nextp) { 2089221873Ssobomax ndiff = strcmp(ent->pid_cmd_file, stmp->sw_fname); 2090130167Sgad if (ndiff > 0) 2091130167Sgad break; 2092130167Sgad if (ndiff == 0) { 2093130167Sgad if (ent->sig == stmp->sw_signum) 2094130167Sgad break; 2095130167Sgad if (ent->sig > stmp->sw_signum) { 2096130167Sgad ndiff = 1; 2097130167Sgad break; 2098130167Sgad } 2099130167Sgad } 2100130167Sgad sprev = stmp; 2101130167Sgad } 2102130167Sgad if (stmp != NULL && ndiff == 0) 2103130167Sgad return (stmp); 2104130167Sgad 2105221873Ssobomax tmpsiz = sizeof(struct sigwork_entry) + strlen(ent->pid_cmd_file) + 1; 2106130167Sgad stmp = malloc(tmpsiz); 2107221873Ssobomax 2108245963Smarkj stmp->sw_runcmd = 0; 2109221873Ssobomax /* If this is a command to run we just set the flag and run command */ 2110221873Ssobomax if (ent->flags & CE_PID2CMD) { 2111245961Smarkj stmp->sw_pid = -1; 2112245961Smarkj stmp->sw_pidok = 0; 2113245963Smarkj stmp->sw_runcmd = 1; 2114221873Ssobomax } else { 2115221873Ssobomax set_swpid(stmp, ent); 2116221873Ssobomax } 2117130167Sgad stmp->sw_signum = ent->sig; 2118221873Ssobomax strcpy(stmp->sw_fname, ent->pid_cmd_file); 2119130167Sgad if (sprev == NULL) 2120130167Sgad SLIST_INSERT_HEAD(&swhead, stmp, sw_nextp); 2121130167Sgad else 2122130167Sgad SLIST_INSERT_AFTER(sprev, stmp, sw_nextp); 2123130167Sgad return (stmp); 2124130167Sgad} 2125130167Sgad 2126130167Sgad/* 2127130167Sgad * Save information on any file we need to compress. We may see the same 2128130167Sgad * file multiple times, so check the full list to avoid duplicates. The 2129130167Sgad * list itself is sorted smallest-to-largest, because that's the order we 2130130167Sgad * want to compress the files. If the partition is very low on disk space, 2131130167Sgad * then the smallest files are the most likely to compress, and compressing 2132130167Sgad * them first will free up more space for the larger files. 2133130167Sgad */ 2134130167Sgadstatic struct zipwork_entry * 2135130167Sgadsave_zipwork(const struct conf_entry *ent, const struct sigwork_entry *swork, 2136130167Sgad int zsize, const char *zipfname) 2137130167Sgad{ 2138130167Sgad struct zipwork_entry *zprev, *ztmp; 2139130167Sgad int ndiff; 2140130167Sgad size_t tmpsiz; 2141130167Sgad 2142130167Sgad /* Compute the size if the caller did not know it. */ 2143130167Sgad if (zsize < 0) 2144130167Sgad zsize = sizefile(zipfname); 2145130167Sgad 2146130167Sgad zprev = NULL; 2147130167Sgad ndiff = 1; 2148130167Sgad SLIST_FOREACH(ztmp, &zwhead, zw_nextp) { 2149130707Sgad ndiff = strcmp(zipfname, ztmp->zw_fname); 2150130167Sgad if (ndiff == 0) 2151130167Sgad break; 2152130167Sgad if (zsize > ztmp->zw_fsize) 2153130167Sgad zprev = ztmp; 2154130167Sgad } 2155130167Sgad if (ztmp != NULL && ndiff == 0) 2156130167Sgad return (ztmp); 2157130167Sgad 2158130167Sgad tmpsiz = sizeof(struct zipwork_entry) + strlen(zipfname) + 1; 2159130167Sgad ztmp = malloc(tmpsiz); 2160130167Sgad ztmp->zw_conf = ent; 2161130167Sgad ztmp->zw_swork = swork; 2162130167Sgad ztmp->zw_fsize = zsize; 2163130167Sgad strcpy(ztmp->zw_fname, zipfname); 2164130167Sgad if (zprev == NULL) 2165130167Sgad SLIST_INSERT_HEAD(&zwhead, ztmp, zw_nextp); 2166130167Sgad else 2167130167Sgad SLIST_INSERT_AFTER(zprev, ztmp, zw_nextp); 2168130167Sgad return (ztmp); 2169130167Sgad} 2170130167Sgad 2171130167Sgad/* Send a signal to the pid specified by pidfile */ 2172130167Sgadstatic void 2173130167Sgadset_swpid(struct sigwork_entry *swork, const struct conf_entry *ent) 2174130167Sgad{ 2175130167Sgad FILE *f; 2176130167Sgad long minok, maxok, rval; 2177130167Sgad char *endp, *linep, line[BUFSIZ]; 2178130167Sgad 2179130167Sgad minok = MIN_PID; 2180130167Sgad maxok = MAX_PID; 2181130167Sgad swork->sw_pidok = 0; 2182130167Sgad swork->sw_pid = 0; 2183130167Sgad swork->sw_pidtype = "daemon"; 2184130167Sgad if (ent->flags & CE_SIGNALGROUP) { 2185130167Sgad /* 2186130167Sgad * If we are expected to signal a process-group when 2187130167Sgad * rotating this logfile, then the value read in should 2188130167Sgad * be the negative of a valid process ID. 2189130167Sgad */ 2190130167Sgad minok = -MAX_PID; 2191130167Sgad maxok = -MIN_PID; 2192130167Sgad swork->sw_pidtype = "process-group"; 2193130167Sgad } 2194130167Sgad 2195221873Ssobomax f = fopen(ent->pid_cmd_file, "r"); 2196130167Sgad if (f == NULL) { 2197202668Sdelphij if (errno == ENOENT && enforcepid == 0) { 2198200806Sdelphij /* 2199200806Sdelphij * Warn if the PID file doesn't exist, but do 2200200806Sdelphij * not consider it an error. Most likely it 2201200806Sdelphij * means the process has been terminated, 2202200806Sdelphij * so it should be safe to rotate any log 2203200806Sdelphij * files that the process would have been using. 2204200806Sdelphij */ 2205200806Sdelphij swork->sw_pidok = 1; 2206221873Ssobomax warnx("pid file doesn't exist: %s", ent->pid_cmd_file); 2207200806Sdelphij } else 2208221873Ssobomax warn("can't open pid file: %s", ent->pid_cmd_file); 2209130167Sgad return; 2210130167Sgad } 2211130167Sgad 2212130167Sgad if (fgets(line, BUFSIZ, f) == NULL) { 2213130167Sgad /* 2214130167Sgad * Warn if the PID file is empty, but do not consider 2215130167Sgad * it an error. Most likely it means the process has 2216130167Sgad * has terminated, so it should be safe to rotate any 2217130167Sgad * log files that the process would have been using. 2218130167Sgad */ 2219202668Sdelphij if (feof(f) && enforcepid == 0) { 2220130167Sgad swork->sw_pidok = 1; 2221221873Ssobomax warnx("pid/cmd file is empty: %s", ent->pid_cmd_file); 2222130167Sgad } else 2223221873Ssobomax warn("can't read from pid file: %s", ent->pid_cmd_file); 2224130167Sgad (void)fclose(f); 2225130167Sgad return; 2226130167Sgad } 2227130167Sgad (void)fclose(f); 2228130167Sgad 2229130167Sgad errno = 0; 2230130167Sgad linep = line; 2231130167Sgad while (*linep == ' ') 2232130167Sgad linep++; 2233130167Sgad rval = strtol(linep, &endp, 10); 2234130167Sgad if (*endp != '\0' && !isspacech(*endp)) { 2235130167Sgad warnx("pid file does not start with a valid number: %s", 2236221873Ssobomax ent->pid_cmd_file); 2237130167Sgad } else if (rval < minok || rval > maxok) { 2238130167Sgad warnx("bad value '%ld' for process number in %s", 2239221873Ssobomax rval, ent->pid_cmd_file); 2240130167Sgad if (verbose) 2241130167Sgad warnx("\t(expecting value between %ld and %ld)", 2242130167Sgad minok, maxok); 2243130167Sgad } else { 2244130167Sgad swork->sw_pidok = 1; 2245130167Sgad swork->sw_pid = rval; 2246130167Sgad } 2247130167Sgad 2248130167Sgad return; 2249130167Sgad} 2250130167Sgad 225113244Sgraichen/* Log the fact that the logs were turned over */ 225259004Shmstatic int 2253119926Sgadlog_trim(const char *logname, const struct conf_entry *log_ent) 225413244Sgraichen{ 225559003Shm FILE *f; 2256111388Sgad const char *xtra; 225759003Shm 2258119926Sgad if ((f = fopen(logname, "a")) == NULL) 225959003Shm return (-1); 2260111388Sgad xtra = ""; 2261111768Sgad if (log_ent->def_cfg) 2262111388Sgad xtra = " using <default> rule"; 2263114137Sgad if (log_ent->firstcreate) 2264114137Sgad fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n", 2265114137Sgad daytime, hostname, (int) getpid(), xtra); 2266114137Sgad else if (log_ent->r_reason != NULL) 2267111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n", 2268111772Sgad daytime, hostname, (int) getpid(), log_ent->r_reason, xtra); 2269111772Sgad else 2270111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n", 2271111772Sgad daytime, hostname, (int) getpid(), xtra); 227259003Shm if (fclose(f) == EOF) 2273127858Scharnier err(1, "log_trim: fclose"); 227459003Shm return (0); 227513244Sgraichen} 227613244Sgraichen 227713244Sgraichen/* Return size in kilobytes of a file */ 227859004Shmstatic int 2279130165Sgadsizefile(const char *file) 228013244Sgraichen{ 228159003Shm struct stat sb; 228213244Sgraichen 228359003Shm if (stat(file, &sb) < 0) 228459003Shm return (-1); 2285262075Sbdrewery return (kbytes(sb.st_size)); 228613244Sgraichen} 228713244Sgraichen 2288248776Smarkj/* 2289248776Smarkj * Return the mtime of the most recent archive of the logfile, using timestamp 2290248776Smarkj * based filenames. 2291248776Smarkj */ 2292248776Smarkjstatic time_t 2293248776Smarkjmtime_old_timelog(const char *file) 2294248776Smarkj{ 2295248776Smarkj struct stat sb; 2296248776Smarkj struct tm tm; 2297248776Smarkj int dir_fd; 2298248776Smarkj time_t t; 2299248776Smarkj struct dirent *dp; 2300248776Smarkj DIR *dirp; 2301248776Smarkj char *s, *logfname, *dir; 2302248776Smarkj 2303248776Smarkj t = -1; 2304248776Smarkj 2305248776Smarkj if ((dir = dirname(file)) == NULL) { 2306248776Smarkj warn("dirname() of '%s'", file); 2307248776Smarkj return (t); 2308248776Smarkj } 2309248776Smarkj if ((s = basename(file)) == NULL) { 2310248776Smarkj warn("basename() of '%s'", file); 2311248776Smarkj return (t); 2312248776Smarkj } else if (s[0] == '/') { 2313248776Smarkj warnx("Invalid log filename '%s'", s); 2314248776Smarkj return (t); 2315248776Smarkj } else if ((logfname = strdup(s)) == NULL) 2316248776Smarkj err(1, "strdup()"); 2317248776Smarkj 2318248776Smarkj if ((dirp = opendir(dir)) == NULL) { 2319248776Smarkj warn("Cannot open log directory '%s'", dir); 2320248776Smarkj return (t); 2321248776Smarkj } 2322248776Smarkj dir_fd = dirfd(dirp); 2323248776Smarkj /* Open the archive dir and find the most recent archive of logfname. */ 2324248776Smarkj while ((dp = readdir(dirp)) != NULL) { 2325250545Smarkj if (validate_old_timelog(dir_fd, dp, logfname, &tm) == 0) 2326248776Smarkj continue; 2327248776Smarkj 2328251240Smarkj if (fstatat(dir_fd, dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) == -1) { 2329248776Smarkj warn("Cannot stat '%s'", file); 2330248776Smarkj continue; 2331248776Smarkj } 2332248776Smarkj if (t < sb.st_mtime) 2333248776Smarkj t = sb.st_mtime; 2334248776Smarkj } 2335248776Smarkj closedir(dirp); 2336248776Smarkj 2337248776Smarkj return (t); 2338248776Smarkj} 2339248776Smarkj 2340248776Smarkj/* Return the age in hours of the most recent archive of the logfile. */ 234159004Shmstatic int 2342248776Smarkjage_old_log(const char *file) 234313244Sgraichen{ 234459003Shm struct stat sb; 2345218127Smm const char *logfile_suffix; 2346218127Smm char tmp[MAXPATHLEN + sizeof(".0") + COMPRESS_SUFFIX_MAXLEN + 1]; 2347248776Smarkj time_t mtime; 234813244Sgraichen 234959004Shm if (archtodir) { 235059004Shm char *p; 235159004Shm 235259004Shm /* build name of archive directory into tmp */ 235359004Shm if (*archdirname == '/') { /* absolute */ 235471299Sjedgar strlcpy(tmp, archdirname, sizeof(tmp)); 235559004Shm } else { /* relative */ 235659004Shm /* get directory part of logfile */ 235771299Sjedgar strlcpy(tmp, file, sizeof(tmp)); 2358229403Sed if ((p = strrchr(tmp, '/')) == NULL) 235959004Shm tmp[0] = '\0'; 236059004Shm else 236159004Shm *(p + 1) = '\0'; 236271299Sjedgar strlcat(tmp, archdirname, sizeof(tmp)); 236359004Shm } 236459004Shm 236571299Sjedgar strlcat(tmp, "/", sizeof(tmp)); 236659004Shm 236759004Shm /* get filename part of logfile */ 2368229403Sed if ((p = strrchr(file, '/')) == NULL) 236971299Sjedgar strlcat(tmp, file, sizeof(tmp)); 237059004Shm else 237171299Sjedgar strlcat(tmp, p + 1, sizeof(tmp)); 237259004Shm } else { 237371299Sjedgar (void) strlcpy(tmp, file, sizeof(tmp)); 237459004Shm } 237559004Shm 2376248776Smarkj if (timefnamefmt != NULL) { 2377248776Smarkj mtime = mtime_old_timelog(tmp); 2378248776Smarkj if (mtime == -1) 2379248776Smarkj return (-1); 2380248776Smarkj } else { 2381248776Smarkj strlcat(tmp, ".0", sizeof(tmp)); 2382248776Smarkj logfile_suffix = get_logfile_suffix(tmp); 2383248776Smarkj if (logfile_suffix == NULL) 2384248776Smarkj return (-1); 2385248776Smarkj (void) strlcat(tmp, logfile_suffix, sizeof(tmp)); 2386248776Smarkj if (stat(tmp, &sb) < 0) 2387248776Smarkj return (-1); 2388248776Smarkj mtime = sb.st_mtime; 2389248776Smarkj } 2390248776Smarkj 2391248776Smarkj return ((int)(ptimeget_secs(timenow) - mtime + 1800) / 3600); 239213244Sgraichen} 239313244Sgraichen 239413244Sgraichen/* Skip Over Blanks */ 2395111820Sgadstatic char * 239659004Shmsob(char *p) 239713244Sgraichen{ 239859003Shm while (p && *p && isspace(*p)) 239959003Shm p++; 240059003Shm return (p); 240113244Sgraichen} 240213244Sgraichen 240313244Sgraichen/* Skip Over Non-Blanks */ 2404111820Sgadstatic char * 240559004Shmson(char *p) 240613244Sgraichen{ 240759003Shm while (p && *p && !isspace(*p)) 240859003Shm p++; 240959003Shm return (p); 241013244Sgraichen} 241143071Swollman 2412119102Sgad/* Check if string is actually a number */ 2413119102Sgadstatic int 2414119102Sgadisnumberstr(const char *string) 2415119102Sgad{ 2416119102Sgad while (*string) { 2417119102Sgad if (!isdigitch(*string++)) 2418119102Sgad return (0); 2419119102Sgad } 2420119102Sgad return (1); 2421119102Sgad} 2422119102Sgad 2423208649Sgordon/* Check if string contains a glob */ 2424208649Sgordonstatic int 2425208649Sgordonisglobstr(const char *string) 2426208649Sgordon{ 2427208649Sgordon char chr; 2428208649Sgordon 2429208649Sgordon while ((chr = *string++)) { 2430208649Sgordon if (chr == '*' || chr == '?' || chr == '[') 2431208649Sgordon return (1); 2432208649Sgordon } 2433208649Sgordon return (0); 2434208649Sgordon} 2435208649Sgordon 2436130038Sgad/* 2437130038Sgad * Save the active log file under a new name. A link to the new name 2438130038Sgad * is the quick-and-easy way to do this. If that fails (which it will 2439130038Sgad * if the destination is on another partition), then make a copy of 2440130038Sgad * the file to the new location. 2441130038Sgad */ 244259004Shmstatic void 2443130038Sgadsavelog(char *from, char *to) 244459004Shm{ 244559004Shm FILE *src, *dst; 2446130038Sgad int c, res; 244759004Shm 2448130038Sgad res = link(from, to); 2449130038Sgad if (res == 0) 2450130038Sgad return; 2451130038Sgad 245259004Shm if ((src = fopen(from, "r")) == NULL) 245359004Shm err(1, "can't fopen %s for reading", from); 245459004Shm if ((dst = fopen(to, "w")) == NULL) 245559004Shm err(1, "can't fopen %s for writing", to); 245659004Shm 245759004Shm while ((c = getc(src)) != EOF) { 245859004Shm if ((putc(c, dst)) == EOF) 245959004Shm err(1, "error writing to %s", to); 246059004Shm } 246159004Shm 246259004Shm if (ferror(src)) 246359004Shm err(1, "error reading from %s", from); 246459004Shm if ((fclose(src)) != 0) 246559004Shm err(1, "can't fclose %s", to); 246659004Shm if ((fclose(dst)) != 0) 246759004Shm err(1, "can't fclose %s", from); 246859004Shm} 246959004Shm 247059004Shm/* create one or more directory components of a path */ 247159004Shmstatic void 2472114137Sgadcreatedir(const struct conf_entry *ent, char *dirpart) 247359004Shm{ 2474111398Sgad int res; 247559004Shm char *s, *d; 247671299Sjedgar char mkdirpath[MAXPATHLEN]; 247759004Shm struct stat st; 247859004Shm 247959004Shm s = dirpart; 248059004Shm d = mkdirpath; 248159004Shm 248259004Shm for (;;) { 248359004Shm *d++ = *s++; 2484111398Sgad if (*s != '/' && *s != '\0') 2485111398Sgad continue; 2486111398Sgad *d = '\0'; 2487111398Sgad res = lstat(mkdirpath, &st); 2488111398Sgad if (res != 0) { 2489111398Sgad if (noaction) { 2490111967Sgad printf("\tmkdir %s\n", mkdirpath); 2491111398Sgad } else { 2492111398Sgad res = mkdir(mkdirpath, 0755); 2493111398Sgad if (res != 0) 2494111398Sgad err(1, "Error on mkdir(\"%s\") for -a", 2495111398Sgad mkdirpath); 2496111398Sgad } 249759004Shm } 249859004Shm if (*s == '\0') 249959004Shm break; 250059004Shm } 2501114137Sgad if (verbose) { 2502114137Sgad if (ent->firstcreate) 2503114137Sgad printf("Created directory '%s' for new %s\n", 2504114137Sgad dirpart, ent->log); 2505114137Sgad else 2506114137Sgad printf("Created directory '%s' for -a\n", dirpart); 2507114137Sgad } 250859004Shm} 250959004Shm 2510114137Sgad/* 2511114137Sgad * Create a new log file, destroying any currently-existing version 2512114137Sgad * of the log file in the process. If the caller wants a backup copy 2513114137Sgad * of the file to exist, they should call 'link(logfile,logbackup)' 2514114137Sgad * before calling this routine. 2515114137Sgad */ 2516114137Sgadvoid 2517114137Sgadcreatelog(const struct conf_entry *ent) 2518114137Sgad{ 2519114137Sgad int fd, failed; 2520114137Sgad struct stat st; 2521114137Sgad char *realfile, *slash, tempfile[MAXPATHLEN]; 2522114137Sgad 2523114137Sgad fd = -1; 2524114137Sgad realfile = ent->log; 2525114137Sgad 2526114137Sgad /* 2527114137Sgad * If this log file is being created for the first time (-C option), 2528114137Sgad * then it may also be true that the parent directory does not exist 2529114137Sgad * yet. Check, and create that directory if it is missing. 2530114137Sgad */ 2531114137Sgad if (ent->firstcreate) { 2532114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 2533114137Sgad slash = strrchr(tempfile, '/'); 2534114137Sgad if (slash != NULL) { 2535114137Sgad *slash = '\0'; 2536131581Ssobomax failed = stat(tempfile, &st); 2537114137Sgad if (failed && errno != ENOENT) 2538131581Ssobomax err(1, "Error on stat(%s)", tempfile); 2539114137Sgad if (failed) 2540114137Sgad createdir(ent, tempfile); 2541114137Sgad else if (!S_ISDIR(st.st_mode)) 2542114137Sgad errx(1, "%s exists but is not a directory", 2543114137Sgad tempfile); 2544114137Sgad } 2545114137Sgad } 2546114137Sgad 2547114137Sgad /* 2548114137Sgad * First create an unused filename, so it can be chown'ed and 2549114137Sgad * chmod'ed before it is moved into the real location. mkstemp 2550114137Sgad * will create the file mode=600 & owned by us. Note that all 2551114137Sgad * temp files will have a suffix of '.z<something>'. 2552114137Sgad */ 2553114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 2554114137Sgad strlcat(tempfile, ".zXXXXXX", sizeof(tempfile)); 2555114137Sgad if (noaction) 2556114137Sgad printf("\tmktemp %s\n", tempfile); 2557114137Sgad else { 2558114137Sgad fd = mkstemp(tempfile); 2559114137Sgad if (fd < 0) 2560114137Sgad err(1, "can't mkstemp logfile %s", tempfile); 2561114137Sgad 2562114137Sgad /* 2563114137Sgad * Add status message to what will become the new log file. 2564114137Sgad */ 2565114137Sgad if (!(ent->flags & CE_BINARY)) { 2566114137Sgad if (log_trim(tempfile, ent)) 2567114137Sgad err(1, "can't add status message to log"); 2568114137Sgad } 2569114137Sgad } 2570114137Sgad 2571114137Sgad /* Change the owner/group, if we are supposed to */ 2572114137Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 2573114137Sgad if (noaction) 2574114137Sgad printf("\tchown %u:%u %s\n", ent->uid, ent->gid, 2575114137Sgad tempfile); 2576114137Sgad else { 2577114137Sgad failed = fchown(fd, ent->uid, ent->gid); 2578114137Sgad if (failed) 2579114137Sgad err(1, "can't fchown temp file %s", tempfile); 2580114137Sgad } 2581114137Sgad } 2582114137Sgad 2583130043Sgad /* Turn on NODUMP if it was requested in the config-file. */ 2584130043Sgad if (ent->flags & CE_NODUMP) { 2585130043Sgad if (noaction) 2586130043Sgad printf("\tchflags nodump %s\n", tempfile); 2587130043Sgad else { 2588130043Sgad failed = fchflags(fd, UF_NODUMP); 2589130043Sgad if (failed) { 2590130043Sgad warn("log_trim: fchflags(NODUMP)"); 2591130043Sgad } 2592130043Sgad } 2593130043Sgad } 2594130043Sgad 2595114137Sgad /* 2596114137Sgad * Note that if the real logfile still exists, and if the call 2597114137Sgad * to rename() fails, then "neither the old file nor the new 2598114137Sgad * file shall be changed or created" (to quote the standard). 2599114137Sgad * If the call succeeds, then the file will be replaced without 2600114137Sgad * any window where some other process might find that the file 2601114137Sgad * did not exist. 2602114137Sgad * XXX - ? It may be that for some error conditions, we could 2603114137Sgad * retry by first removing the realfile and then renaming. 2604114137Sgad */ 2605114137Sgad if (noaction) { 2606114137Sgad printf("\tchmod %o %s\n", ent->permissions, tempfile); 2607114137Sgad printf("\tmv %s %s\n", tempfile, realfile); 2608114137Sgad } else { 2609114137Sgad failed = fchmod(fd, ent->permissions); 2610114137Sgad if (failed) 2611114137Sgad err(1, "can't fchmod temp file '%s'", tempfile); 2612114137Sgad failed = rename(tempfile, realfile); 2613114137Sgad if (failed) 2614114137Sgad err(1, "can't mv %s to %s", tempfile, realfile); 2615114137Sgad } 2616114137Sgad 2617114137Sgad if (fd >= 0) 2618114137Sgad close(fd); 2619114137Sgad} 2620130165Sgad 2621154566Sgad/* 2622154566Sgad * Change the attributes of a given filename to what was specified in 2623154566Sgad * the newsyslog.conf entry. This routine is only called for files 2624154566Sgad * that newsyslog expects that it has created, and thus it is a fatal 2625154566Sgad * error if this routine finds that the file does not exist. 2626154566Sgad */ 2627130165Sgadstatic void 2628130165Sgadchange_attrs(const char *fname, const struct conf_entry *ent) 2629130165Sgad{ 2630130165Sgad int failed; 2631130165Sgad 2632130165Sgad if (noaction) { 2633130165Sgad printf("\tchmod %o %s\n", ent->permissions, fname); 2634130165Sgad 2635130165Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 2636130165Sgad printf("\tchown %u:%u %s\n", 2637130165Sgad ent->uid, ent->gid, fname); 2638130165Sgad 2639130165Sgad if (ent->flags & CE_NODUMP) 2640130165Sgad printf("\tchflags nodump %s\n", fname); 2641130165Sgad return; 2642130165Sgad } 2643130165Sgad 2644130165Sgad failed = chmod(fname, ent->permissions); 2645154566Sgad if (failed) { 2646154566Sgad if (errno != EPERM) 2647154566Sgad err(1, "chmod(%s) in change_attrs", fname); 2648154566Sgad warn("change_attrs couldn't chmod(%s)", fname); 2649154566Sgad } 2650130165Sgad 2651130165Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 2652130165Sgad failed = chown(fname, ent->uid, ent->gid); 2653130165Sgad if (failed) 2654130165Sgad warn("can't chown %s", fname); 2655130165Sgad } 2656130165Sgad 2657130165Sgad if (ent->flags & CE_NODUMP) { 2658130165Sgad failed = chflags(fname, UF_NODUMP); 2659130165Sgad if (failed) 2660130165Sgad warn("can't chflags %s NODUMP", fname); 2661130165Sgad } 2662130165Sgad} 2663290225Sbapt 2664290225Sbapt/* 2665290225Sbapt * Parse a signal number or signal name. Returns the signal number parsed or -1 2666290225Sbapt * on failure. 2667290225Sbapt */ 2668290225Sbaptstatic int 2669290225Sbaptparse_signal(const char *str) 2670290225Sbapt{ 2671290225Sbapt int sig, i; 2672290225Sbapt const char *errstr; 2673290225Sbapt 2674290225Sbapt sig = strtonum(str, 1, sys_nsig - 1, &errstr); 2675290225Sbapt 2676290225Sbapt if (errstr == NULL) 2677290225Sbapt return (sig); 2678290225Sbapt if (strncasecmp(str, "SIG", 3) == 0) 2679290225Sbapt str += 3; 2680290225Sbapt 2681290225Sbapt for (i = 1; i < sys_nsig; i++) { 2682290225Sbapt if (strcasecmp(str, sys_signame[i]) == 0) 2683290225Sbapt return (i); 2684290225Sbapt } 2685290225Sbapt 2686290225Sbapt return (-1); 2687290225Sbapt} 2688