newsyslog.c revision 154566
1130167Sgad/*- 2130167Sgad * ------+---------+---------+-------- + --------+---------+---------+---------* 3130167Sgad * This file includes significant modifications done by: 4130167Sgad * Copyright (c) 2003, 2004 - Garance Alistair Drosehn <gad@FreeBSD.org>. 5130167Sgad * All rights reserved. 6130167Sgad * 7130167Sgad * Redistribution and use in source and binary forms, with or without 8130167Sgad * modification, are permitted provided that the following conditions 9130167Sgad * are met: 10130167Sgad * 1. Redistributions of source code must retain the above copyright 11130167Sgad * notice, this list of conditions and the following disclaimer. 12130167Sgad * 2. Redistributions in binary form must reproduce the above copyright 13130167Sgad * notice, this list of conditions and the following disclaimer in the 14130167Sgad * documentation and/or other materials provided with the distribution. 15130167Sgad * 16130167Sgad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17130167Sgad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18130167Sgad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19130167Sgad * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20130167Sgad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21130167Sgad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22130167Sgad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23130167Sgad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24130167Sgad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25130167Sgad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26130167Sgad * SUCH DAMAGE. 27130167Sgad * 28130167Sgad * ------+---------+---------+-------- + --------+---------+---------+---------* 29130167Sgad */ 30130167Sgad 3113244Sgraichen/* 3213244Sgraichen * This file contains changes from the Open Software Foundation. 3313244Sgraichen */ 3413244Sgraichen 3513244Sgraichen/* 3659004Shm * Copyright 1988, 1989 by the Massachusetts Institute of Technology 3759004Shm * 3859004Shm * Permission to use, copy, modify, and distribute this software and its 3959004Shm * documentation for any purpose and without fee is hereby granted, provided 4059004Shm * that the above copyright notice appear in all copies and that both that 4159004Shm * copyright notice and this permission notice appear in supporting 4259004Shm * documentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be 4359004Shm * used in advertising or publicity pertaining to distribution of the 4459004Shm * software without specific, written prior permission. M.I.T. and the M.I.T. 4559004Shm * S.I.P.B. make no representations about the suitability of this software 4659004Shm * for any purpose. It is provided "as is" without express or implied 4759004Shm * warranty. 4859004Shm * 4959004Shm */ 5013244Sgraichen 5113244Sgraichen/* 5259004Shm * newsyslog - roll over selected logs at the appropriate time, keeping the a 5359004Shm * specified number of backup files around. 5413244Sgraichen */ 5513244Sgraichen 56114601Sobrien#include <sys/cdefs.h> 57114601Sobrien__FBSDID("$FreeBSD: head/usr.sbin/newsyslog/newsyslog.c 154566 2006-01-20 05:18:01Z gad $"); 5813244Sgraichen 59130045Sgad#define OSF 6013244Sgraichen#ifndef COMPRESS_POSTFIX 61130045Sgad#define COMPRESS_POSTFIX ".gz" 6213244Sgraichen#endif 6380638Sobrien#ifndef BZCOMPRESS_POSTFIX 6480638Sobrien#define BZCOMPRESS_POSTFIX ".bz2" 6580638Sobrien#endif 6613244Sgraichen 6796001Smaxim#include <sys/param.h> 68130167Sgad#include <sys/queue.h> 6996001Smaxim#include <sys/stat.h> 7096001Smaxim#include <sys/wait.h> 7196001Smaxim 7230160Scharnier#include <ctype.h> 7330160Scharnier#include <err.h> 7495999Smaxim#include <errno.h> 7530160Scharnier#include <fcntl.h> 76111773Sgad#include <fnmatch.h> 77106905Ssobomax#include <glob.h> 7830160Scharnier#include <grp.h> 7943071Swollman#include <paths.h> 8030160Scharnier#include <pwd.h> 8130160Scharnier#include <signal.h> 8213244Sgraichen#include <stdio.h> 8313244Sgraichen#include <stdlib.h> 8413244Sgraichen#include <string.h> 8543071Swollman#include <time.h> 8616240Salex#include <unistd.h> 8713244Sgraichen 8843071Swollman#include "pathnames.h" 89119998Sgad#include "extern.h" 9043071Swollman 91130167Sgad/* Define this symbol to try out the "new order" for work items. */ 92130167Sgad#define TRY_NEWORDER 93130385Sgad#ifndef USE_NEWORDER 94130385Sgad#define USE_NEWORDER 1 /* Initial value for dbg_new_order */ 95130385Sgad#endif 96130167Sgad 97111768Sgad/* 98111768Sgad * Bit-values for the 'flags' parsed from a config-file entry. 99111768Sgad */ 100130045Sgad#define CE_COMPACT 0x0001 /* Compact the achived log files with gzip. */ 101130045Sgad#define CE_BZCOMPACT 0x0002 /* Compact the achived log files with bzip2. */ 102130045Sgad#define CE_COMPACTWAIT 0x0004 /* wait until compressing one file finishes */ 103111768Sgad /* before starting the next step. */ 104130045Sgad#define CE_BINARY 0x0008 /* Logfile is in binary, do not add status */ 105111768Sgad /* messages to logfile(s) when rotating. */ 106130045Sgad#define CE_NOSIGNAL 0x0010 /* There is no process to signal when */ 107111768Sgad /* trimming this file. */ 108130045Sgad#define CE_TRIMAT 0x0020 /* trim file at a specific time. */ 109130045Sgad#define CE_GLOB 0x0040 /* name of the log is file name pattern. */ 110130045Sgad#define CE_SIGNALGROUP 0x0080 /* Signal a process-group instead of a single */ 111112003Sgad /* process when trimming this file. */ 112130045Sgad#define CE_CREATE 0x0100 /* Create the log file if it does not exist. */ 113130043Sgad#define CE_NODUMP 0x0200 /* Set 'nodump' on newly created log file. */ 11443071Swollman 115130045Sgad#define MIN_PID 5 /* Don't touch pids lower than this */ 116130045Sgad#define MAX_PID 99999 /* was lower, see /usr/include/sys/proc.h */ 117111781Sgad 118130045Sgad#define kbytes(size) (((size) + 1023) >> 10) 119111781Sgad 120130165Sgad#define DEFAULT_MARKER "<default>" 121130165Sgad#define DEBUG_MARKER "<debug>" 122130165Sgad 12313244Sgraichenstruct conf_entry { 12459003Shm char *log; /* Name of the log */ 12559003Shm char *pid_file; /* PID file */ 126111772Sgad char *r_reason; /* The reason this file is being rotated */ 127114137Sgad int firstcreate; /* Creating log for the first time (-C). */ 128111772Sgad int rotate; /* Non-zero if this file should be rotated */ 129130165Sgad int fsize; /* size found for the log file */ 130111779Sgad uid_t uid; /* Owner of log */ 131111779Sgad gid_t gid; /* Group of log */ 13259003Shm int numlogs; /* Number of logs to keep */ 133130165Sgad int trsize; /* Size cutoff to trigger trimming the log */ 13459003Shm int hours; /* Hours between log trimming */ 135120361Sgad struct ptime_data *trim_at; /* Specific time to do trimming */ 136139655Sdelphij unsigned int permissions; /* File permissions on the log */ 13780646Sobrien int flags; /* CE_COMPACT, CE_BZCOMPACT, CE_BINARY */ 13859003Shm int sig; /* Signal to send */ 139111388Sgad int def_cfg; /* Using the <default> rule for this file */ 14059003Shm struct conf_entry *next;/* Linked list pointer */ 14113244Sgraichen}; 14213244Sgraichen 143130167Sgadstruct sigwork_entry { 144130167Sgad SLIST_ENTRY(sigwork_entry) sw_nextp; 145130167Sgad int sw_signum; /* the signal to send */ 146130167Sgad int sw_pidok; /* true if pid value is valid */ 147130167Sgad pid_t sw_pid; /* the process id from the PID file */ 148130167Sgad const char *sw_pidtype; /* "daemon" or "process group" */ 149130167Sgad char sw_fname[1]; /* file the PID was read from */ 150130167Sgad}; 151130167Sgad 152130167Sgadstruct zipwork_entry { 153130167Sgad SLIST_ENTRY(zipwork_entry) zw_nextp; 154130167Sgad const struct conf_entry *zw_conf; /* for chown/perm/flag info */ 155130167Sgad const struct sigwork_entry *zw_swork; /* to know success of signal */ 156130167Sgad int zw_fsize; /* size of the file to compress */ 157130167Sgad char zw_fname[1]; /* the file to compress */ 158130167Sgad}; 159130167Sgad 160130165Sgadtypedef enum { 161130165Sgad FREE_ENT, KEEP_ENT 162130165Sgad} fk_entry; 163111388Sgad 164130167SgadSLIST_HEAD(swlisthead, sigwork_entry) swhead = SLIST_HEAD_INITIALIZER(swhead); 165130167SgadSLIST_HEAD(zwlisthead, zipwork_entry) zwhead = SLIST_HEAD_INITIALIZER(zwhead); 166130167Sgad 167120361Sgadint dbg_at_times; /* -D Show details of 'trim_at' code */ 168130385Sgad/* 169130385Sgad * The debug options "neworder" and "oldorder" can be used to change 170130385Sgad * which order work is done in. Note that both options will disappear 171130385Sgad * in the near future, and the "new" order will be the only order. 172130385Sgad */ 173130385Sgadint dbg_new_order = USE_NEWORDER; 174120361Sgad 17559004Shmint archtodir = 0; /* Archive old logfiles to other directory */ 176114137Sgadint createlogs; /* Create (non-GLOB) logfiles which do not */ 177114137Sgad /* already exist. 1=='for entries with */ 178114137Sgad /* C flag', 2=='for all entries'. */ 17959003Shmint verbose = 0; /* Print out what's going on */ 18059003Shmint needroot = 1; /* Root privs are necessary */ 18159003Shmint noaction = 0; /* Don't do anything, just show it */ 182143106Sbrooksint norotate = 0; /* Don't rotate */ 183111768Sgadint nosignal; /* Do not send any signals */ 18459003Shmint force = 0; /* Force the trim no matter what */ 185111772Sgadint rotatereq = 0; /* -R = Always rotate the file(s) as given */ 186111772Sgad /* on the command (this also requires */ 187111772Sgad /* that a list of files *are* given on */ 188111772Sgad /* the run command). */ 189111772Sgadchar *requestor; /* The name given on a -R request */ 19059004Shmchar *archdirname; /* Directory path to old logfiles archive */ 191136127Sbrookschar *destdir = NULL; /* Directory to treat at root for logs */ 192111773Sgadconst char *conf; /* Configuration file to use */ 19359003Shm 194120361Sgadstruct ptime_data *dbg_timenow; /* A "timenow" value set via -D option */ 195120361Sgadstruct ptime_data *timenow; /* The time to use for checking at-fields */ 196120361Sgad 19771299Sjedgarchar hostname[MAXHOSTNAMELEN]; /* hostname */ 198120361Sgadchar daytime[16]; /* The current time in human readable form, 199120361Sgad * used for rotation-tracking messages. */ 20013244Sgraichen 201111773Sgadstatic struct conf_entry *get_worklist(char **files); 202111773Sgadstatic void parse_file(FILE *cf, const char *cfname, struct conf_entry **work_p, 203112020Sgad struct conf_entry **glob_p, struct conf_entry **defconf_p); 20416240Salexstatic char *sob(char *p); 20516240Salexstatic char *son(char *p); 206119102Sgadstatic int isnumberstr(const char *); 20759003Shmstatic char *missing_field(char *p, char *errline); 208130165Sgadstatic void change_attrs(const char *, const struct conf_entry *); 209130165Sgadstatic fk_entry do_entry(struct conf_entry *); 210130165Sgadstatic fk_entry do_rotate(const struct conf_entry *); 211130167Sgad#ifdef TRY_NEWORDER 212130167Sgadstatic void do_sigwork(struct sigwork_entry *); 213130167Sgadstatic void do_zipwork(struct zipwork_entry *); 214130167Sgadstatic struct sigwork_entry * 215130167Sgad save_sigwork(const struct conf_entry *); 216130167Sgadstatic struct zipwork_entry * 217130167Sgad save_zipwork(const struct conf_entry *, const struct 218130167Sgad sigwork_entry *, int, const char *); 219130167Sgadstatic void set_swpid(struct sigwork_entry *, const struct conf_entry *); 220130167Sgad#endif 221130165Sgadstatic int sizefile(const char *); 222112020Sgadstatic void expand_globs(struct conf_entry **work_p, 223112020Sgad struct conf_entry **glob_p); 224112020Sgadstatic void free_clist(struct conf_entry **firstent); 225111388Sgadstatic void free_entry(struct conf_entry *ent); 226111388Sgadstatic struct conf_entry *init_entry(const char *fname, 227111388Sgad struct conf_entry *src_entry); 228111781Sgadstatic void parse_args(int argc, char **argv); 229119904Sgadstatic int parse_doption(const char *doption); 23080640Sobrienstatic void usage(void); 231119926Sgadstatic int log_trim(const char *logname, const struct conf_entry *log_ent); 232119926Sgadstatic void compress_log(char *logname, int dowait); 233119926Sgadstatic void bzcompress_log(char *logname, int dowait); 23416240Salexstatic int age_old_log(char *file); 235112003Sgadstatic int send_signal(const struct conf_entry *ent); 236130038Sgadstatic void savelog(char *from, char *to); 237114137Sgadstatic void createdir(const struct conf_entry *ent, char *dirpart); 238114137Sgadstatic void createlog(const struct conf_entry *ent); 23913244Sgraichen 240111768Sgad/* 241129975Sgad * All the following take a parameter of 'int', but expect values in the 242129975Sgad * range of unsigned char. Define wrappers which take values of type 'char', 243129975Sgad * whether signed or unsigned, and ensure they end up in the right range. 244111768Sgad */ 245129975Sgad#define isdigitch(Anychar) isdigit((u_char)(Anychar)) 246129975Sgad#define isprintch(Anychar) isprint((u_char)(Anychar)) 247129975Sgad#define isspacech(Anychar) isspace((u_char)(Anychar)) 248129975Sgad#define tolowerch(Anychar) tolower((u_char)(Anychar)) 249111768Sgad 25059004Shmint 25159004Shmmain(int argc, char **argv) 25213244Sgraichen{ 253130165Sgad fk_entry free_or_keep; 25459003Shm struct conf_entry *p, *q; 255130167Sgad#ifdef TRY_NEWORDER 256130167Sgad struct sigwork_entry *stmp; 257130167Sgad struct zipwork_entry *ztmp; 258130167Sgad#endif 25925443Sache 260130167Sgad SLIST_INIT(&swhead); 261130167Sgad SLIST_INIT(&zwhead); 262130167Sgad 263111781Sgad parse_args(argc, argv); 264111773Sgad argc -= optind; 265111773Sgad argv += optind; 266111773Sgad 26759003Shm if (needroot && getuid() && geteuid()) 26859003Shm errx(1, "must have root privs"); 269111773Sgad p = q = get_worklist(argv); 27059003Shm 271130165Sgad /* 272130165Sgad * Rotate all the files which need to be rotated. Note that 273130165Sgad * some users have *hundreds* of entries in newsyslog.conf! 274130165Sgad */ 27559003Shm while (p) { 276130165Sgad free_or_keep = do_entry(p); 27759003Shm p = p->next; 278130165Sgad if (free_or_keep == FREE_ENT) 279130165Sgad free_entry(q); 28059003Shm q = p; 28159003Shm } 282130165Sgad 283130167Sgad#ifdef TRY_NEWORDER 284130167Sgad /* 285130167Sgad * Send signals to any processes which need a signal to tell 286130167Sgad * them to close and re-open the log file(s) we have rotated. 287130167Sgad * Note that zipwork_entries include pointers to these 288130167Sgad * sigwork_entry's, so we can not free the entries here. 289130167Sgad */ 290130167Sgad if (!SLIST_EMPTY(&swhead)) { 291130204Sgad if (noaction || verbose) 292130167Sgad printf("Signal all daemon process(es)...\n"); 293130167Sgad SLIST_FOREACH(stmp, &swhead, sw_nextp) 294130167Sgad do_sigwork(stmp); 295130204Sgad if (noaction) 296130204Sgad printf("\tsleep 10\n"); 297130204Sgad else { 298130204Sgad if (verbose) 299130204Sgad printf("Pause 10 seconds to allow daemon(s)" 300130204Sgad " to close log file(s)\n"); 301130204Sgad sleep(10); 302130204Sgad } 303130167Sgad } 304130167Sgad /* 305130167Sgad * Compress all files that we're expected to compress, now 306130167Sgad * that all processes should have closed the files which 307130167Sgad * have been rotated. 308130167Sgad */ 309130167Sgad if (!SLIST_EMPTY(&zwhead)) { 310130204Sgad if (noaction || verbose) 311130167Sgad printf("Compress all rotated log file(s)...\n"); 312130167Sgad while (!SLIST_EMPTY(&zwhead)) { 313130167Sgad ztmp = SLIST_FIRST(&zwhead); 314130167Sgad do_zipwork(ztmp); 315130167Sgad SLIST_REMOVE_HEAD(&zwhead, zw_nextp); 316130167Sgad free(ztmp); 317130167Sgad } 318130167Sgad } 319130167Sgad /* Now free all the sigwork entries. */ 320130167Sgad while (!SLIST_EMPTY(&swhead)) { 321130167Sgad stmp = SLIST_FIRST(&swhead); 322130167Sgad SLIST_REMOVE_HEAD(&swhead, sw_nextp); 323130167Sgad free(stmp); 324130167Sgad } 325130167Sgad#endif /* TRY_NEWORDER */ 326130167Sgad 32795999Smaxim while (wait(NULL) > 0 || errno == EINTR) 32895999Smaxim ; 32959003Shm return (0); 33013244Sgraichen} 33113244Sgraichen 332111388Sgadstatic struct conf_entry * 333111388Sgadinit_entry(const char *fname, struct conf_entry *src_entry) 334111388Sgad{ 335111388Sgad struct conf_entry *tempwork; 336111388Sgad 337111388Sgad if (verbose > 4) 338111388Sgad printf("\t--> [creating entry for %s]\n", fname); 339111388Sgad 340111388Sgad tempwork = malloc(sizeof(struct conf_entry)); 341111388Sgad if (tempwork == NULL) 342111388Sgad err(1, "malloc of conf_entry for %s", fname); 343111388Sgad 344136174Sbrooks if (destdir == NULL || fname[0] != '/') 345136127Sbrooks tempwork->log = strdup(fname); 346136127Sbrooks else 347136127Sbrooks asprintf(&tempwork->log, "%s%s", destdir, fname); 348111388Sgad if (tempwork->log == NULL) 349111388Sgad err(1, "strdup for %s", fname); 350111388Sgad 351111388Sgad if (src_entry != NULL) { 352111388Sgad tempwork->pid_file = NULL; 353111388Sgad if (src_entry->pid_file) 354111388Sgad tempwork->pid_file = strdup(src_entry->pid_file); 355111772Sgad tempwork->r_reason = NULL; 356114137Sgad tempwork->firstcreate = 0; 357111772Sgad tempwork->rotate = 0; 358130165Sgad tempwork->fsize = -1; 359111388Sgad tempwork->uid = src_entry->uid; 360111388Sgad tempwork->gid = src_entry->gid; 361111388Sgad tempwork->numlogs = src_entry->numlogs; 362130165Sgad tempwork->trsize = src_entry->trsize; 363111388Sgad tempwork->hours = src_entry->hours; 364120361Sgad tempwork->trim_at = NULL; 365120361Sgad if (src_entry->trim_at != NULL) 366120361Sgad tempwork->trim_at = ptime_init(src_entry->trim_at); 367111388Sgad tempwork->permissions = src_entry->permissions; 368111388Sgad tempwork->flags = src_entry->flags; 369111388Sgad tempwork->sig = src_entry->sig; 370111388Sgad tempwork->def_cfg = src_entry->def_cfg; 371111388Sgad } else { 372111388Sgad /* Initialize as a "do-nothing" entry */ 373111388Sgad tempwork->pid_file = NULL; 374111772Sgad tempwork->r_reason = NULL; 375114137Sgad tempwork->firstcreate = 0; 376111772Sgad tempwork->rotate = 0; 377130165Sgad tempwork->fsize = -1; 378111779Sgad tempwork->uid = (uid_t)-1; 379111779Sgad tempwork->gid = (gid_t)-1; 380111388Sgad tempwork->numlogs = 1; 381130165Sgad tempwork->trsize = -1; 382111388Sgad tempwork->hours = -1; 383120361Sgad tempwork->trim_at = NULL; 384111388Sgad tempwork->permissions = 0; 385111388Sgad tempwork->flags = 0; 386111388Sgad tempwork->sig = SIGHUP; 387111388Sgad tempwork->def_cfg = 0; 388111388Sgad } 389111388Sgad tempwork->next = NULL; 390111388Sgad 391111388Sgad return (tempwork); 392111388Sgad} 393111388Sgad 39459004Shmstatic void 395111388Sgadfree_entry(struct conf_entry *ent) 396111388Sgad{ 397111388Sgad 398111388Sgad if (ent == NULL) 399111388Sgad return; 400111388Sgad 401111388Sgad if (ent->log != NULL) { 402111388Sgad if (verbose > 4) 403111388Sgad printf("\t--> [freeing entry for %s]\n", ent->log); 404111388Sgad free(ent->log); 405111388Sgad ent->log = NULL; 406111388Sgad } 407111388Sgad 408111388Sgad if (ent->pid_file != NULL) { 409111388Sgad free(ent->pid_file); 410111388Sgad ent->pid_file = NULL; 411111388Sgad } 412111388Sgad 413111772Sgad if (ent->r_reason != NULL) { 414111772Sgad free(ent->r_reason); 415111772Sgad ent->r_reason = NULL; 416111772Sgad } 417111772Sgad 418120361Sgad if (ent->trim_at != NULL) { 419120361Sgad ptime_free(ent->trim_at); 420120361Sgad ent->trim_at = NULL; 421120361Sgad } 422120361Sgad 423111388Sgad free(ent); 424111388Sgad} 425111388Sgad 426111388Sgadstatic void 427112020Sgadfree_clist(struct conf_entry **firstent) 428112020Sgad{ 429112020Sgad struct conf_entry *ent, *nextent; 430112020Sgad 431112020Sgad if (firstent == NULL) 432112020Sgad return; /* There is nothing to do. */ 433112020Sgad 434112020Sgad ent = *firstent; 435112020Sgad firstent = NULL; 436112020Sgad 437112020Sgad while (ent) { 438112020Sgad nextent = ent->next; 439112020Sgad free_entry(ent); 440112020Sgad ent = nextent; 441112020Sgad } 442112020Sgad} 443112020Sgad 444130165Sgadstatic fk_entry 44559004Shmdo_entry(struct conf_entry * ent) 44613244Sgraichen{ 447130045Sgad#define REASON_MAX 80 448130165Sgad int modtime; 449130165Sgad fk_entry free_or_keep; 450120361Sgad double diffsecs; 451111772Sgad char temp_reason[REASON_MAX]; 45259003Shm 453130165Sgad free_or_keep = FREE_ENT; 45459003Shm if (verbose) { 45559003Shm if (ent->flags & CE_COMPACT) 45659003Shm printf("%s <%dZ>: ", ent->log, ent->numlogs); 45780638Sobrien else if (ent->flags & CE_BZCOMPACT) 45880638Sobrien printf("%s <%dJ>: ", ent->log, ent->numlogs); 45959003Shm else 46059003Shm printf("%s <%d>: ", ent->log, ent->numlogs); 46159003Shm } 462130165Sgad ent->fsize = sizefile(ent->log); 46359003Shm modtime = age_old_log(ent->log); 464111772Sgad ent->rotate = 0; 465114137Sgad ent->firstcreate = 0; 466130165Sgad if (ent->fsize < 0) { 467114137Sgad /* 468114137Sgad * If either the C flag or the -C option was specified, 469114137Sgad * and if we won't be creating the file, then have the 470114137Sgad * verbose message include a hint as to why the file 471114137Sgad * will not be created. 472114137Sgad */ 473114137Sgad temp_reason[0] = '\0'; 474114137Sgad if (createlogs > 1) 475114137Sgad ent->firstcreate = 1; 476114137Sgad else if ((ent->flags & CE_CREATE) && createlogs) 477114137Sgad ent->firstcreate = 1; 478114137Sgad else if (ent->flags & CE_CREATE) 479114137Sgad strncpy(temp_reason, " (no -C option)", REASON_MAX); 480114137Sgad else if (createlogs) 481114137Sgad strncpy(temp_reason, " (no C flag)", REASON_MAX); 482114137Sgad 483114137Sgad if (ent->firstcreate) { 484114137Sgad if (verbose) 485114137Sgad printf("does not exist -> will create.\n"); 486114137Sgad createlog(ent); 487114137Sgad } else if (verbose) { 488114137Sgad printf("does not exist, skipped%s.\n", temp_reason); 489114137Sgad } 49059003Shm } else { 491111772Sgad if (ent->flags & CE_TRIMAT && !force && !rotatereq) { 492120361Sgad diffsecs = ptimeget_diff(timenow, ent->trim_at); 493120361Sgad if (diffsecs < 0.0) { 494120361Sgad /* trim_at is some time in the future. */ 495120361Sgad if (verbose) { 496120361Sgad ptime_adjust4dst(ent->trim_at, 497120361Sgad timenow); 49843071Swollman printf("--> will trim at %s", 499120361Sgad ptimeget_ctime(ent->trim_at)); 500120361Sgad } 501130165Sgad return (free_or_keep); 502120361Sgad } else if (diffsecs >= 3600.0) { 503120361Sgad /* 504120361Sgad * trim_at is more than an hour in the past, 505120361Sgad * so find the next valid trim_at time, and 506120361Sgad * tell the user what that will be. 507120361Sgad */ 508120361Sgad if (verbose && dbg_at_times) 509120361Sgad printf("\n\t--> prev trim at %s\t", 510120361Sgad ptimeget_ctime(ent->trim_at)); 511120361Sgad if (verbose) { 512120361Sgad ptimeset_nxtime(ent->trim_at); 513120361Sgad printf("--> will trim at %s", 514120361Sgad ptimeget_ctime(ent->trim_at)); 515120361Sgad } 516130165Sgad return (free_or_keep); 517120361Sgad } else if (verbose && noaction && dbg_at_times) { 518120361Sgad /* 519120361Sgad * If we are just debugging at-times, then 520120361Sgad * a detailed message is helpful. Also 521120361Sgad * skip "doing" any commands, since they 522120361Sgad * would all be turned off by no-action. 523120361Sgad */ 524120361Sgad printf("\n\t--> timematch at %s", 525120361Sgad ptimeget_ctime(ent->trim_at)); 526130165Sgad return (free_or_keep); 52743071Swollman } else if (verbose && ent->hours <= 0) { 52843071Swollman printf("--> time is up\n"); 52943071Swollman } 53043071Swollman } 531130165Sgad if (verbose && (ent->trsize > 0)) 532130165Sgad printf("size (Kb): %d [%d] ", ent->fsize, ent->trsize); 53359003Shm if (verbose && (ent->hours > 0)) 53459003Shm printf(" age (hr): %d [%d] ", modtime, ent->hours); 535111772Sgad 536111772Sgad /* 537111772Sgad * Figure out if this logfile needs to be rotated. 538111772Sgad */ 539111772Sgad temp_reason[0] = '\0'; 540111772Sgad if (rotatereq) { 541111772Sgad ent->rotate = 1; 542111772Sgad snprintf(temp_reason, REASON_MAX, " due to -R from %s", 543111772Sgad requestor); 544111772Sgad } else if (force) { 545111772Sgad ent->rotate = 1; 546111772Sgad snprintf(temp_reason, REASON_MAX, " due to -F request"); 547130165Sgad } else if ((ent->trsize > 0) && (ent->fsize >= ent->trsize)) { 548111772Sgad ent->rotate = 1; 549111772Sgad snprintf(temp_reason, REASON_MAX, " due to size>%dK", 550130165Sgad ent->trsize); 551111772Sgad } else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) { 552111772Sgad ent->rotate = 1; 553111772Sgad } else if ((ent->hours > 0) && ((modtime >= ent->hours) || 554111772Sgad (modtime < 0))) { 555111772Sgad ent->rotate = 1; 556111772Sgad } 557111772Sgad 558111772Sgad /* 559111772Sgad * If the file needs to be rotated, then rotate it. 560111772Sgad */ 561143106Sbrooks if (ent->rotate && !norotate) { 562111772Sgad if (temp_reason[0] != '\0') 563111772Sgad ent->r_reason = strdup(temp_reason); 56459003Shm if (verbose) 56559003Shm printf("--> trimming log....\n"); 56659003Shm if (noaction && !verbose) { 56759003Shm if (ent->flags & CE_COMPACT) 56859003Shm printf("%s <%dZ>: trimming\n", 56959003Shm ent->log, ent->numlogs); 57080638Sobrien else if (ent->flags & CE_BZCOMPACT) 57180638Sobrien printf("%s <%dJ>: trimming\n", 57280638Sobrien ent->log, ent->numlogs); 57359003Shm else 57459003Shm printf("%s <%d>: trimming\n", 57559003Shm ent->log, ent->numlogs); 57659003Shm } 577130165Sgad free_or_keep = do_rotate(ent); 57859003Shm } else { 57959003Shm if (verbose) 58059003Shm printf("--> skipping\n"); 58159003Shm } 58259003Shm } 583130165Sgad return (free_or_keep); 584111772Sgad#undef REASON_MAX 58513244Sgraichen} 58613244Sgraichen 587112003Sgad/* Send a signal to the pid specified by pidfile */ 588112003Sgadstatic int 589112003Sgadsend_signal(const struct conf_entry *ent) 590112003Sgad{ 591112003Sgad pid_t target_pid; 592112003Sgad int did_notify; 593112003Sgad FILE *f; 594112003Sgad long minok, maxok, rval; 595112003Sgad const char *target_name; 596112003Sgad char *endp, *linep, line[BUFSIZ]; 597112003Sgad 598112003Sgad did_notify = 0; 599112003Sgad f = fopen(ent->pid_file, "r"); 600112003Sgad if (f == NULL) { 601112003Sgad warn("can't open pid file: %s", ent->pid_file); 602112003Sgad return (did_notify); 603112003Sgad /* NOTREACHED */ 604112003Sgad } 605112003Sgad 606112003Sgad if (fgets(line, BUFSIZ, f) == NULL) { 607112003Sgad /* 608112003Sgad * XXX - If the pid file is empty, is that really a 609112003Sgad * problem? Wouldn't that mean that the process 610112003Sgad * has shut down? In that case there would be no 611112003Sgad * problem with compressing the rotated log file. 612112003Sgad */ 613112003Sgad if (feof(f)) 614112003Sgad warnx("pid file is empty: %s", ent->pid_file); 615112003Sgad else 616112003Sgad warn("can't read from pid file: %s", ent->pid_file); 617112003Sgad (void) fclose(f); 618112003Sgad return (did_notify); 619112003Sgad /* NOTREACHED */ 620112003Sgad } 621112003Sgad (void) fclose(f); 622112003Sgad 623112003Sgad target_name = "daemon"; 624112003Sgad minok = MIN_PID; 625112003Sgad maxok = MAX_PID; 626112003Sgad if (ent->flags & CE_SIGNALGROUP) { 627112003Sgad /* 628112003Sgad * If we are expected to signal a process-group when 629112003Sgad * rotating this logfile, then the value read in should 630112003Sgad * be the negative of a valid process ID. 631112003Sgad */ 632112003Sgad target_name = "process-group"; 633112003Sgad minok = -MAX_PID; 634112003Sgad maxok = -MIN_PID; 635112003Sgad } 636112003Sgad 637112003Sgad errno = 0; 638112003Sgad linep = line; 639112003Sgad while (*linep == ' ') 640112003Sgad linep++; 641112003Sgad rval = strtol(linep, &endp, 10); 642112003Sgad if (*endp != '\0' && !isspacech(*endp)) { 643112003Sgad warnx("pid file does not start with a valid number: %s", 644112003Sgad ent->pid_file); 645112003Sgad rval = 0; 646112003Sgad } else if (rval < minok || rval > maxok) { 647112003Sgad warnx("bad value '%ld' for process number in %s", 648112003Sgad rval, ent->pid_file); 649112003Sgad if (verbose) 650112003Sgad warnx("\t(expecting value between %ld and %ld)", 651112003Sgad minok, maxok); 652112003Sgad rval = 0; 653112003Sgad } 654112003Sgad if (rval == 0) { 655112003Sgad return (did_notify); 656112003Sgad /* NOTREACHED */ 657112003Sgad } 658112003Sgad 659112003Sgad target_pid = rval; 660112003Sgad 661112003Sgad if (noaction) { 662112003Sgad did_notify = 1; 663112003Sgad printf("\tkill -%d %d\n", ent->sig, (int) target_pid); 664112003Sgad } else if (kill(target_pid, ent->sig)) { 665112003Sgad /* 666112003Sgad * XXX - Iff the error was "no such process", should that 667112003Sgad * really be an error for us? Perhaps the process 668112003Sgad * is already gone, in which case there would be no 669112003Sgad * problem with compressing the rotated log file. 670112003Sgad */ 671112003Sgad warn("can't notify %s, pid %d", target_name, 672112003Sgad (int) target_pid); 673112003Sgad } else { 674112003Sgad did_notify = 1; 675112003Sgad if (verbose) 676112003Sgad printf("%s pid %d notified\n", target_name, 677112003Sgad (int) target_pid); 678112003Sgad } 679112003Sgad 680112003Sgad return (did_notify); 681112003Sgad} 682112003Sgad 68359004Shmstatic void 684111781Sgadparse_args(int argc, char **argv) 68513244Sgraichen{ 686111781Sgad int ch; 68759003Shm char *p; 68813244Sgraichen 689120361Sgad timenow = ptime_init(NULL); 690120361Sgad ptimeset_time(timenow, time(NULL)); 691120361Sgad (void)strncpy(daytime, ptimeget_ctime(timenow) + 4, 15); 69259003Shm daytime[15] = '\0'; 69313244Sgraichen 69459003Shm /* Let's get our hostname */ 695111781Sgad (void)gethostname(hostname, sizeof(hostname)); 69613244Sgraichen 69713244Sgraichen /* Truncate domain */ 698111781Sgad if ((p = strchr(hostname, '.')) != NULL) 69913244Sgraichen *p = '\0'; 700111768Sgad 701111768Sgad /* Parse command line options. */ 702143106Sbrooks while ((ch = getopt(argc, argv, "a:d:f:nrsvCD:FNR:")) != -1) 703111781Sgad switch (ch) { 70459004Shm case 'a': 70559004Shm archtodir++; 70659004Shm archdirname = optarg; 70759004Shm break; 708136127Sbrooks case 'd': 709136127Sbrooks destdir = optarg; 710136127Sbrooks break; 711111768Sgad case 'f': 712111768Sgad conf = optarg; 713111768Sgad break; 714111768Sgad case 'n': 715111768Sgad noaction++; 716111768Sgad break; 71759003Shm case 'r': 71859003Shm needroot = 0; 71959003Shm break; 720111768Sgad case 's': 721111768Sgad nosignal = 1; 722111768Sgad break; 72359003Shm case 'v': 72459003Shm verbose++; 72559003Shm break; 726114137Sgad case 'C': 727114137Sgad /* Useful for things like rc.diskless... */ 728114137Sgad createlogs++; 729114137Sgad break; 730119904Sgad case 'D': 731119904Sgad /* 732119904Sgad * Set some debugging option. The specific option 733119904Sgad * depends on the value of optarg. These options 734119904Sgad * may come and go without notice or documentation. 735119904Sgad */ 736119904Sgad if (parse_doption(optarg)) 737119904Sgad break; 738119904Sgad usage(); 739119904Sgad /* NOTREACHED */ 74034584Spst case 'F': 74134584Spst force++; 74234584Spst break; 743143106Sbrooks case 'N': 744143106Sbrooks norotate++; 745143106Sbrooks break; 746111772Sgad case 'R': 747111772Sgad rotatereq++; 748111772Sgad requestor = strdup(optarg); 749111772Sgad break; 750111781Sgad case 'm': /* Used by OpenBSD for "monitor mode" */ 75159003Shm default: 75259003Shm usage(); 753111768Sgad /* NOTREACHED */ 75459003Shm } 755111772Sgad 756143106Sbrooks if (force && norotate) { 757143106Sbrooks warnx("Only one of -F and -N may be specified."); 758143106Sbrooks usage(); 759143106Sbrooks /* NOTREACHED */ 760143106Sbrooks } 761143106Sbrooks 762111772Sgad if (rotatereq) { 763111772Sgad if (optind == argc) { 764111772Sgad warnx("At least one filename must be given when -R is specified."); 765111772Sgad usage(); 766111772Sgad /* NOTREACHED */ 767111772Sgad } 768111772Sgad /* Make sure "requestor" value is safe for a syslog message. */ 769111772Sgad for (p = requestor; *p != '\0'; p++) { 770111772Sgad if (!isprintch(*p) && (*p != '\t')) 771111772Sgad *p = '.'; 772111772Sgad } 773111772Sgad } 774119904Sgad 775119904Sgad if (dbg_timenow) { 776119904Sgad /* 777119904Sgad * Note that the 'daytime' variable is not changed. 778119904Sgad * That is only used in messages that track when a 779119904Sgad * logfile is rotated, and if a file *is* rotated, 780119904Sgad * then it will still rotated at the "real now" time. 781119904Sgad */ 782120361Sgad ptime_free(timenow); 783119904Sgad timenow = dbg_timenow; 784119904Sgad fprintf(stderr, "Debug: Running as if TimeNow is %s", 785120361Sgad ptimeget_ctime(dbg_timenow)); 786119904Sgad } 787119904Sgad 78843071Swollman} 78913244Sgraichen 790120361Sgad/* 791120361Sgad * These debugging options are mainly meant for developer use, such 792120361Sgad * as writing regression-tests. They would not be needed by users 793120361Sgad * during normal operation of newsyslog... 794120361Sgad */ 795119904Sgadstatic int 796119904Sgadparse_doption(const char *doption) 797119904Sgad{ 798119904Sgad const char TN[] = "TN="; 799120361Sgad int res; 800119904Sgad 801119904Sgad if (strncmp(doption, TN, sizeof(TN) - 1) == 0) { 802119904Sgad /* 803120361Sgad * The "TimeNow" debugging option. This might be off 804120361Sgad * by an hour when crossing a timezone change. 805119904Sgad */ 806120361Sgad dbg_timenow = ptime_init(NULL); 807120361Sgad res = ptime_relparse(dbg_timenow, PTM_PARSE_ISO8601, 808120361Sgad time(NULL), doption + sizeof(TN) - 1); 809120361Sgad if (res == -2) { 810120361Sgad warnx("Non-existent time specified on -D %s", doption); 811120361Sgad return (0); /* failure */ 812120361Sgad } else if (res < 0) { 813119904Sgad warnx("Malformed time given on -D %s", doption); 814119904Sgad return (0); /* failure */ 815119904Sgad } 816119904Sgad return (1); /* successfully parsed */ 817119904Sgad 818119904Sgad } 819119904Sgad 820120361Sgad if (strcmp(doption, "ats") == 0) { 821120361Sgad dbg_at_times++; 822120361Sgad return (1); /* successfully parsed */ 823120361Sgad } 824120361Sgad 825130167Sgad if (strcmp(doption, "neworder") == 0) { 826130167Sgad#ifdef TRY_NEWORDER 827130167Sgad dbg_new_order++; 828130167Sgad#else 829130167Sgad warnx("NOTE: The code for 'neworder' was not compiled in."); 830130167Sgad#endif 831130167Sgad return (1); /* successfully parsed */ 832130167Sgad } 833130205Sgad if (strcmp(doption, "oldorder") == 0) { 834130205Sgad#ifdef TRY_NEWORDER 835130205Sgad dbg_new_order = 0; 836130205Sgad#else 837130205Sgad warnx("NOTE: The code for 'neworder' was not compiled in."); 838130205Sgad#endif 839130205Sgad return (1); /* successfully parsed */ 840130205Sgad } 841130167Sgad 842130165Sgad warnx("Unknown -D (debug) option: '%s'", doption); 843119904Sgad return (0); /* failure */ 844119904Sgad} 845119904Sgad 84659004Shmstatic void 84759004Shmusage(void) 84813244Sgraichen{ 84980646Sobrien 85080646Sobrien fprintf(stderr, 851143106Sbrooks "usage: newsyslog [-CFNnrsv] [-a directory] [-d directory] [-f config-file]\n" 852111772Sgad " [ [-R requestor] filename ... ]\n"); 85359003Shm exit(1); 85413244Sgraichen} 85513244Sgraichen 85659004Shm/* 857111773Sgad * Parse a configuration file and return a linked list of all the logs 858111773Sgad * which should be processed. 85913244Sgraichen */ 86059003Shmstatic struct conf_entry * 861111773Sgadget_worklist(char **files) 86213244Sgraichen{ 86359003Shm FILE *f; 864111773Sgad const char *fname; 865111773Sgad char **given; 866111773Sgad struct conf_entry *defconf, *dupent, *ent, *firstnew; 867112020Sgad struct conf_entry *globlist, *lastnew, *worklist; 868112020Sgad int gmatch, fnres; 869111773Sgad 870112020Sgad defconf = globlist = worklist = NULL; 871111773Sgad 872111773Sgad fname = conf; 873111773Sgad if (fname == NULL) 874111773Sgad fname = _PATH_CONF; 875111773Sgad 876111773Sgad if (strcmp(fname, "-") != 0) 877111773Sgad f = fopen(fname, "r"); 878111773Sgad else { 879111773Sgad f = stdin; 880111773Sgad fname = "<stdin>"; 881111773Sgad } 882111773Sgad if (!f) 883152980Ssobomax err(1, "%s", fname); 884111773Sgad 885112020Sgad parse_file(f, fname, &worklist, &globlist, &defconf); 886111773Sgad (void) fclose(f); 887111773Sgad 888111773Sgad /* 889111773Sgad * All config-file information has been read in and turned into 890112020Sgad * a worklist and a globlist. If there were no specific files 891112020Sgad * given on the run command, then the only thing left to do is to 892112020Sgad * call a routine which finds all files matched by the globlist 893112020Sgad * and adds them to the worklist. Then return the worklist. 894111773Sgad */ 895111773Sgad if (*files == NULL) { 896112020Sgad expand_globs(&worklist, &globlist); 897112020Sgad free_clist(&globlist); 898111773Sgad if (defconf != NULL) 899111773Sgad free_entry(defconf); 900111773Sgad return (worklist); 901111773Sgad /* NOTREACHED */ 902111773Sgad } 903111773Sgad 904111773Sgad /* 905111773Sgad * If newsyslog was given a specific list of files to process, 906111773Sgad * it may be that some of those files were not listed in any 907111773Sgad * config file. Those unlisted files should get the default 908111773Sgad * rotation action. First, create the default-rotation action 909111773Sgad * if none was found in a system config file. 910111773Sgad */ 911111773Sgad if (defconf == NULL) { 912111773Sgad defconf = init_entry(DEFAULT_MARKER, NULL); 913111773Sgad defconf->numlogs = 3; 914130165Sgad defconf->trsize = 50; 915111773Sgad defconf->permissions = S_IRUSR|S_IWUSR; 916111773Sgad } 917111773Sgad 918111773Sgad /* 919111773Sgad * If newsyslog was run with a list of specific filenames, 920111773Sgad * then create a new worklist which has only those files in 921111773Sgad * it, picking up the rotation-rules for those files from 922111773Sgad * the original worklist. 923111773Sgad * 924111773Sgad * XXX - Note that this will copy multiple rules for a single 925111773Sgad * logfile, if multiple entries are an exact match for 926111773Sgad * that file. That matches the historic behavior, but do 927111773Sgad * we want to continue to allow it? If so, it should 928111773Sgad * probably be handled more intelligently. 929111773Sgad */ 930112020Sgad firstnew = lastnew = NULL; 931111773Sgad for (given = files; *given; ++given) { 932111773Sgad /* 933111773Sgad * First try to find exact-matches for this given file. 934111773Sgad */ 935112020Sgad gmatch = 0; 936111773Sgad for (ent = worklist; ent; ent = ent->next) { 937111773Sgad if (strcmp(ent->log, *given) == 0) { 938111773Sgad gmatch++; 939111773Sgad dupent = init_entry(*given, ent); 940111773Sgad if (!firstnew) 941111773Sgad firstnew = dupent; 942111773Sgad else 943112020Sgad lastnew->next = dupent; 944112020Sgad lastnew = dupent; 945111773Sgad } 946111773Sgad } 947111773Sgad if (gmatch) { 948111773Sgad if (verbose > 2) 949111773Sgad printf("\t+ Matched entry %s\n", *given); 950111773Sgad continue; 951111773Sgad } 952111773Sgad 953111773Sgad /* 954111773Sgad * There was no exact-match for this given file, so look 955111773Sgad * for a "glob" entry which does match. 956111773Sgad */ 957112020Sgad gmatch = 0; 958112020Sgad if (verbose > 2 && globlist != NULL) 959112020Sgad printf("\t+ Checking globs for %s\n", *given); 960112020Sgad for (ent = globlist; ent; ent = ent->next) { 961112020Sgad fnres = fnmatch(ent->log, *given, FNM_PATHNAME); 962112020Sgad if (verbose > 2) 963112020Sgad printf("\t+ = %d for pattern %s\n", fnres, 964112020Sgad ent->log); 965112020Sgad if (fnres == 0) { 966111773Sgad gmatch++; 967111773Sgad dupent = init_entry(*given, ent); 968111773Sgad if (!firstnew) 969111773Sgad firstnew = dupent; 970111773Sgad else 971112020Sgad lastnew->next = dupent; 972112020Sgad lastnew = dupent; 973112020Sgad /* This new entry is not a glob! */ 974111773Sgad dupent->flags &= ~CE_GLOB; 975111773Sgad /* Only allow a match to one glob-entry */ 976111773Sgad break; 977111773Sgad } 978111773Sgad } 979111773Sgad if (gmatch) { 980111773Sgad if (verbose > 2) 981111773Sgad printf("\t+ Matched %s via %s\n", *given, 982111773Sgad ent->log); 983111773Sgad continue; 984111773Sgad } 985111773Sgad 986111773Sgad /* 987111773Sgad * This given file was not found in any config file, so 988111773Sgad * add a worklist item based on the default entry. 989111773Sgad */ 990111773Sgad if (verbose > 2) 991111773Sgad printf("\t+ No entry matched %s (will use %s)\n", 992111773Sgad *given, DEFAULT_MARKER); 993111773Sgad dupent = init_entry(*given, defconf); 994111773Sgad if (!firstnew) 995111773Sgad firstnew = dupent; 996111773Sgad else 997112020Sgad lastnew->next = dupent; 998111773Sgad /* Mark that it was *not* found in a config file */ 999111773Sgad dupent->def_cfg = 1; 1000112020Sgad lastnew = dupent; 1001111773Sgad } 1002111773Sgad 1003111773Sgad /* 1004112020Sgad * Free all the entries in the original work list, the list of 1005112020Sgad * glob entries, and the default entry. 1006111773Sgad */ 1007112020Sgad free_clist(&worklist); 1008112020Sgad free_clist(&globlist); 1009112020Sgad free_entry(defconf); 1010111773Sgad 1011112020Sgad /* And finally, return a worklist which matches the given files. */ 1012112013Sgad return (firstnew); 1013111773Sgad} 1014111773Sgad 1015111773Sgad/* 1016112020Sgad * Expand the list of entries with filename patterns, and add all files 1017112020Sgad * which match those glob-entries onto the worklist. 1018112020Sgad */ 1019112020Sgadstatic void 1020112020Sgadexpand_globs(struct conf_entry **work_p, struct conf_entry **glob_p) 1021112020Sgad{ 1022112020Sgad int gmatch, gres, i; 1023112020Sgad char *mfname; 1024112020Sgad struct conf_entry *dupent, *ent, *firstmatch, *globent; 1025112020Sgad struct conf_entry *lastmatch; 1026112020Sgad glob_t pglob; 1027112020Sgad struct stat st_fm; 1028112020Sgad 1029112020Sgad if ((glob_p == NULL) || (*glob_p == NULL)) 1030112020Sgad return; /* There is nothing to do. */ 1031112020Sgad 1032112020Sgad /* 1033112020Sgad * The worklist contains all fully-specified (non-GLOB) names. 1034112020Sgad * 1035112020Sgad * Now expand the list of filename-pattern (GLOB) entries into 1036112020Sgad * a second list, which (by definition) will only match files 1037112020Sgad * that already exist. Do not add a glob-related entry for any 1038112020Sgad * file which already exists in the fully-specified list. 1039112020Sgad */ 1040112020Sgad firstmatch = lastmatch = NULL; 1041112020Sgad for (globent = *glob_p; globent; globent = globent->next) { 1042112020Sgad 1043112020Sgad gres = glob(globent->log, GLOB_NOCHECK, NULL, &pglob); 1044112020Sgad if (gres != 0) { 1045112020Sgad warn("cannot expand pattern (%d): %s", gres, 1046112020Sgad globent->log); 1047112020Sgad continue; 1048112020Sgad } 1049112020Sgad 1050112020Sgad if (verbose > 2) 1051112020Sgad printf("\t+ Expanding pattern %s\n", globent->log); 1052112020Sgad for (i = 0; i < pglob.gl_matchc; i++) { 1053112020Sgad mfname = pglob.gl_pathv[i]; 1054112020Sgad 1055112020Sgad /* See if this file already has a specific entry. */ 1056112020Sgad gmatch = 0; 1057112020Sgad for (ent = *work_p; ent; ent = ent->next) { 1058112020Sgad if (strcmp(mfname, ent->log) == 0) { 1059112020Sgad gmatch++; 1060112020Sgad break; 1061112020Sgad } 1062112020Sgad } 1063112020Sgad if (gmatch) 1064112020Sgad continue; 1065112020Sgad 1066112020Sgad /* Make sure the named matched is a file. */ 1067112020Sgad gres = lstat(mfname, &st_fm); 1068112020Sgad if (gres != 0) { 1069112020Sgad /* Error on a file that glob() matched?!? */ 1070112020Sgad warn("Skipping %s - lstat() error", mfname); 1071112020Sgad continue; 1072112020Sgad } 1073112020Sgad if (!S_ISREG(st_fm.st_mode)) { 1074112020Sgad /* We only rotate files! */ 1075112020Sgad if (verbose > 2) 1076112020Sgad printf("\t+ . skipping %s (!file)\n", 1077112020Sgad mfname); 1078112020Sgad continue; 1079112020Sgad } 1080112020Sgad 1081112020Sgad if (verbose > 2) 1082112020Sgad printf("\t+ . add file %s\n", mfname); 1083112020Sgad dupent = init_entry(mfname, globent); 1084112020Sgad if (!firstmatch) 1085112020Sgad firstmatch = dupent; 1086112020Sgad else 1087112020Sgad lastmatch->next = dupent; 1088112020Sgad lastmatch = dupent; 1089112020Sgad /* This new entry is not a glob! */ 1090112020Sgad dupent->flags &= ~CE_GLOB; 1091112020Sgad } 1092112020Sgad globfree(&pglob); 1093112020Sgad if (verbose > 2) 1094112020Sgad printf("\t+ Done with pattern %s\n", globent->log); 1095112020Sgad } 1096112020Sgad 1097112020Sgad /* Add the list of matched files to the end of the worklist. */ 1098112020Sgad if (!*work_p) 1099112020Sgad *work_p = firstmatch; 1100112020Sgad else { 1101112020Sgad ent = *work_p; 1102112020Sgad while (ent->next) 1103112020Sgad ent = ent->next; 1104112020Sgad ent->next = firstmatch; 1105112020Sgad } 1106112020Sgad 1107112020Sgad} 1108112020Sgad 1109112020Sgad/* 1110111773Sgad * Parse a configuration file and update a linked list of all the logs to 1111111773Sgad * process. 1112111773Sgad */ 1113111773Sgadstatic void 1114111773Sgadparse_file(FILE *cf, const char *cfname, struct conf_entry **work_p, 1115112020Sgad struct conf_entry **glob_p, struct conf_entry **defconf_p) 1116111773Sgad{ 111759003Shm char line[BUFSIZ], *parse, *q; 1118107737Ssobomax char *cp, *errline, *group; 1119112020Sgad struct conf_entry *lastglob, *lastwork, *working; 1120111781Sgad struct passwd *pwd; 112159003Shm struct group *grp; 1122120361Sgad int eol, ptm_opts, res, special; 112313244Sgraichen 1124111773Sgad /* 1125111773Sgad * XXX - for now, assume that only one config file will be read, 1126111773Sgad * ie, this routine is only called one time. 1127111773Sgad */ 1128112020Sgad lastglob = lastwork = NULL; 112980646Sobrien 1130130165Sgad errline = NULL; 1131111773Sgad while (fgets(line, BUFSIZ, cf)) { 1132107737Ssobomax if ((line[0] == '\n') || (line[0] == '#') || 1133107737Ssobomax (strlen(line) == 0)) 113459003Shm continue; 1135130165Sgad if (errline != NULL) 1136130165Sgad free(errline); 113759003Shm errline = strdup(line); 1138107737Ssobomax for (cp = line + 1; *cp != '\0'; cp++) { 1139107737Ssobomax if (*cp != '#') 1140107737Ssobomax continue; 1141107737Ssobomax if (*(cp - 1) == '\\') { 1142107737Ssobomax strcpy(cp - 1, cp); 1143107737Ssobomax cp--; 1144107737Ssobomax continue; 1145107737Ssobomax } 1146107737Ssobomax *cp = '\0'; 1147107737Ssobomax break; 1148107737Ssobomax } 114960373Sdes 115060373Sdes q = parse = missing_field(sob(line), errline); 115160373Sdes parse = son(line); 115260373Sdes if (!*parse) 115380646Sobrien errx(1, "malformed line (missing fields):\n%s", 115480646Sobrien errline); 115560373Sdes *parse = '\0'; 115660373Sdes 1157130165Sgad /* 1158130165Sgad * Allow people to set debug options via the config file. 1159130165Sgad * (NOTE: debug optons are undocumented, and may disappear 1160130165Sgad * at any time, etc). 1161130165Sgad */ 1162130165Sgad if (strcasecmp(DEBUG_MARKER, q) == 0) { 1163130165Sgad q = parse = missing_field(sob(++parse), errline); 1164130165Sgad parse = son(parse); 1165130165Sgad if (!*parse) 1166130165Sgad warnx("debug line specifies no option:\n%s", 1167130165Sgad errline); 1168130165Sgad else { 1169130165Sgad *parse = '\0'; 1170130165Sgad parse_doption(q); 1171130165Sgad } 1172130165Sgad continue; 1173130165Sgad } 1174130165Sgad 1175112020Sgad special = 0; 1176111388Sgad working = init_entry(q, NULL); 1177111388Sgad if (strcasecmp(DEFAULT_MARKER, q) == 0) { 1178112020Sgad special = 1; 1179111773Sgad if (defconf_p == NULL) { 1180111773Sgad warnx("Ignoring entry for %s in %s!", q, 1181111773Sgad cfname); 1182111773Sgad free_entry(working); 1183111773Sgad continue; 1184111773Sgad } else if (*defconf_p != NULL) { 1185111388Sgad warnx("Ignoring duplicate entry for %s!", q); 1186111388Sgad free_entry(working); 1187111388Sgad continue; 1188111388Sgad } 1189111773Sgad *defconf_p = working; 119059003Shm } 119113244Sgraichen 119259003Shm q = parse = missing_field(sob(++parse), errline); 119359003Shm parse = son(parse); 119425518Sbrian if (!*parse) 119580646Sobrien errx(1, "malformed line (missing fields):\n%s", 119680646Sobrien errline); 119759003Shm *parse = '\0'; 119859003Shm if ((group = strchr(q, ':')) != NULL || 119959003Shm (group = strrchr(q, '.')) != NULL) { 120059003Shm *group++ = '\0'; 120159003Shm if (*q) { 1202119102Sgad if (!(isnumberstr(q))) { 1203111781Sgad if ((pwd = getpwnam(q)) == NULL) 120459003Shm errx(1, 120580646Sobrien "error in config file; unknown user:\n%s", 120659003Shm errline); 1207111781Sgad working->uid = pwd->pw_uid; 120859003Shm } else 120959003Shm working->uid = atoi(q); 121059003Shm } else 1211111779Sgad working->uid = (uid_t)-1; 121213244Sgraichen 121359003Shm q = group; 121459003Shm if (*q) { 1215119102Sgad if (!(isnumberstr(q))) { 121659003Shm if ((grp = getgrnam(q)) == NULL) 121759003Shm errx(1, 121880646Sobrien "error in config file; unknown group:\n%s", 121959003Shm errline); 122059003Shm working->gid = grp->gr_gid; 122159003Shm } else 122259003Shm working->gid = atoi(q); 122359003Shm } else 1224111779Sgad working->gid = (gid_t)-1; 122513244Sgraichen 122659003Shm q = parse = missing_field(sob(++parse), errline); 122759003Shm parse = son(parse); 122859003Shm if (!*parse) 122980646Sobrien errx(1, "malformed line (missing fields):\n%s", 123080646Sobrien errline); 123159003Shm *parse = '\0'; 1232111779Sgad } else { 1233111779Sgad working->uid = (uid_t)-1; 1234111779Sgad working->gid = (gid_t)-1; 1235111779Sgad } 123659003Shm 123759003Shm if (!sscanf(q, "%o", &working->permissions)) 123859003Shm errx(1, "error in config file; bad permissions:\n%s", 123959003Shm errline); 124059003Shm 124159003Shm q = parse = missing_field(sob(++parse), errline); 124259003Shm parse = son(parse); 124325518Sbrian if (!*parse) 124480646Sobrien errx(1, "malformed line (missing fields):\n%s", 124580646Sobrien errline); 124659003Shm *parse = '\0'; 1247111400Sgad if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0) 1248111400Sgad errx(1, "error in config file; bad value for count of logs to save:\n%s", 124959003Shm errline); 125013244Sgraichen 125159003Shm q = parse = missing_field(sob(++parse), errline); 125259003Shm parse = son(parse); 125325518Sbrian if (!*parse) 125480646Sobrien errx(1, "malformed line (missing fields):\n%s", 125580646Sobrien errline); 125659003Shm *parse = '\0'; 1257114762Sgad if (isdigitch(*q)) 1258130165Sgad working->trsize = atoi(q); 1259130165Sgad else if (strcmp(q, "*") == 0) 1260130165Sgad working->trsize = -1; 1261114762Sgad else { 1262114762Sgad warnx("Invalid value of '%s' for 'size' in line:\n%s", 1263114762Sgad q, errline); 1264130165Sgad working->trsize = -1; 1265114762Sgad } 126659003Shm 126759003Shm working->flags = 0; 126859003Shm q = parse = missing_field(sob(++parse), errline); 126959003Shm parse = son(parse); 127025518Sbrian eol = !*parse; 127159003Shm *parse = '\0'; 127243071Swollman { 127359003Shm char *ep; 127459003Shm u_long ul; 127513244Sgraichen 127643071Swollman ul = strtoul(q, &ep, 10); 127743071Swollman if (ep == q) 127843071Swollman working->hours = 0; 127943071Swollman else if (*ep == '*') 128043071Swollman working->hours = -1; 128143071Swollman else if (ul > INT_MAX) 128243071Swollman errx(1, "interval is too large:\n%s", errline); 128343071Swollman else 128443071Swollman working->hours = ul; 128543071Swollman 1286120361Sgad if (*ep == '\0' || strcmp(ep, "*") == 0) 1287120361Sgad goto no_trimat; 1288120361Sgad if (*ep != '@' && *ep != '$') 128943071Swollman errx(1, "malformed interval/at:\n%s", errline); 1290120361Sgad 1291120361Sgad working->flags |= CE_TRIMAT; 1292120361Sgad working->trim_at = ptime_init(NULL); 1293120361Sgad ptm_opts = PTM_PARSE_ISO8601; 1294120361Sgad if (*ep == '$') 1295120361Sgad ptm_opts = PTM_PARSE_DWM; 1296120361Sgad ptm_opts |= PTM_PARSE_MATCHDOM; 1297120361Sgad res = ptime_relparse(working->trim_at, ptm_opts, 1298120361Sgad ptimeget_secs(timenow), ep + 1); 1299120361Sgad if (res == -2) 1300120361Sgad errx(1, "nonexistent time for 'at' value:\n%s", 1301120361Sgad errline); 1302120361Sgad else if (res < 0) 1303120361Sgad errx(1, "malformed 'at' value:\n%s", errline); 130443071Swollman } 1305120361Sgadno_trimat: 130643071Swollman 130725518Sbrian if (eol) 130859003Shm q = NULL; 130925518Sbrian else { 131059003Shm q = parse = sob(++parse); /* Optional field */ 131159003Shm parse = son(parse); 131259003Shm if (!*parse) 131359003Shm eol = 1; 131459003Shm *parse = '\0'; 131525518Sbrian } 131625443Sache 1317111768Sgad for (; q && *q && !isspacech(*q); q++) { 1318111768Sgad switch (tolowerch(*q)) { 1319111768Sgad case 'b': 132059003Shm working->flags |= CE_BINARY; 1321111768Sgad break; 1322114137Sgad case 'c': 1323111768Sgad /* 1324114137Sgad * XXX - Ick! Ugly! Remove ASAP! 1325114137Sgad * We want `c' and `C' for "create". But we 1326114137Sgad * will temporarily treat `c' as `g', because 1327114137Sgad * FreeBSD releases <= 4.8 have a typo of 1328114137Sgad * checking ('G' || 'c') for CE_GLOB. 1329111768Sgad */ 1330114137Sgad if (*q == 'c') { 1331114137Sgad warnx("Assuming 'g' for 'c' in flags for line:\n%s", 1332114137Sgad errline); 1333114137Sgad warnx("The 'c' flag will eventually mean 'CREATE'"); 1334114137Sgad working->flags |= CE_GLOB; 1335114137Sgad break; 1336114137Sgad } 1337114137Sgad working->flags |= CE_CREATE; 1338114137Sgad break; 1339130043Sgad case 'd': 1340130043Sgad working->flags |= CE_NODUMP; 1341130043Sgad break; 1342111768Sgad case 'g': 1343106905Ssobomax working->flags |= CE_GLOB; 1344111768Sgad break; 1345111768Sgad case 'j': 1346111768Sgad working->flags |= CE_BZCOMPACT; 1347111768Sgad break; 1348111768Sgad case 'n': 1349111768Sgad working->flags |= CE_NOSIGNAL; 1350111768Sgad break; 1351112003Sgad case 'u': 1352112003Sgad working->flags |= CE_SIGNALGROUP; 1353112003Sgad break; 1354111768Sgad case 'w': 1355107916Ssobomax working->flags |= CE_COMPACTWAIT; 1356111768Sgad break; 1357111768Sgad case 'z': 1358111768Sgad working->flags |= CE_COMPACT; 1359111768Sgad break; 1360111768Sgad case '-': 1361111768Sgad break; 1362111781Sgad case 'f': /* Used by OpenBSD for "CE_FOLLOW" */ 1363111781Sgad case 'm': /* Used by OpenBSD for "CE_MONITOR" */ 1364111781Sgad case 'p': /* Used by NetBSD for "CE_PLAIN0" */ 1365111768Sgad default: 136680646Sobrien errx(1, "illegal flag in config file -- %c", 136780646Sobrien *q); 1368111768Sgad } 136959003Shm } 137059003Shm 137125518Sbrian if (eol) 137259003Shm q = NULL; 137325518Sbrian else { 137459003Shm q = parse = sob(++parse); /* Optional field */ 137559003Shm parse = son(parse); 137659003Shm if (!*parse) 137759003Shm eol = 1; 137859003Shm *parse = '\0'; 137925518Sbrian } 138025443Sache 138125443Sache working->pid_file = NULL; 138225443Sache if (q && *q) { 138325443Sache if (*q == '/') 138425443Sache working->pid_file = strdup(q); 138536817Sache else if (isdigit(*q)) 138636817Sache goto got_sig; 138759003Shm else 138880646Sobrien errx(1, 138980646Sobrien "illegal pid file or signal number in config file:\n%s", 139080646Sobrien errline); 139125443Sache } 139236817Sache if (eol) 139359003Shm q = NULL; 139436817Sache else { 139559003Shm q = parse = sob(++parse); /* Optional field */ 139659003Shm *(parse = son(parse)) = '\0'; 139736817Sache } 139836817Sache 139936817Sache working->sig = SIGHUP; 140036817Sache if (q && *q) { 140136817Sache if (isdigit(*q)) { 140259003Shm got_sig: 140336817Sache working->sig = atoi(q); 140436817Sache } else { 140559003Shm err_sig: 140680646Sobrien errx(1, 140780646Sobrien "illegal signal number in config file:\n%s", 140880646Sobrien errline); 140936817Sache } 141036817Sache if (working->sig < 1 || working->sig >= NSIG) 141136817Sache goto err_sig; 141236817Sache } 1413111768Sgad 1414111768Sgad /* 1415111768Sgad * Finish figuring out what pid-file to use (if any) in 1416111768Sgad * later processing if this logfile needs to be rotated. 1417111768Sgad */ 1418111768Sgad if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) { 1419111768Sgad /* 1420111768Sgad * This config-entry specified 'n' for nosignal, 1421111768Sgad * see if it also specified an explicit pid_file. 1422111768Sgad * This would be a pretty pointless combination. 1423111768Sgad */ 1424111768Sgad if (working->pid_file != NULL) { 1425111768Sgad warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s", 1426111768Sgad working->pid_file, errline); 1427111768Sgad free(working->pid_file); 1428111768Sgad working->pid_file = NULL; 1429111768Sgad } 1430111768Sgad } else if (working->pid_file == NULL) { 1431111768Sgad /* 1432111768Sgad * This entry did not specify the 'n' flag, which 1433111768Sgad * means it should signal syslogd unless it had 1434112003Sgad * specified some other pid-file (and obviously the 1435112003Sgad * syslog pid-file will not be for a process-group). 1436112003Sgad * Also, we should only try to notify syslog if we 1437112003Sgad * are root. 1438111768Sgad */ 1439112003Sgad if (working->flags & CE_SIGNALGROUP) { 1440112003Sgad warnx("Ignoring flag 'U' in line:\n%s", 1441112003Sgad errline); 1442112003Sgad working->flags &= ~CE_SIGNALGROUP; 1443112003Sgad } 1444111768Sgad if (needroot) 1445111768Sgad working->pid_file = strdup(_PATH_SYSLOGPID); 1446111768Sgad } 1447111768Sgad 1448112020Sgad /* 1449112020Sgad * Add this entry to the appropriate list of entries, unless 1450112020Sgad * it was some kind of special entry (eg: <default>). 1451112020Sgad */ 1452112020Sgad if (special) { 1453112020Sgad ; /* Do not add to any list */ 1454112020Sgad } else if (working->flags & CE_GLOB) { 1455112020Sgad if (!*glob_p) 1456112020Sgad *glob_p = working; 1457112020Sgad else 1458112020Sgad lastglob->next = working; 1459112020Sgad lastglob = working; 1460112020Sgad } else { 1461112020Sgad if (!*work_p) 1462112020Sgad *work_p = working; 1463112020Sgad else 1464112020Sgad lastwork->next = working; 1465112020Sgad lastwork = working; 1466112020Sgad } 1467130165Sgad } 1468130165Sgad if (errline != NULL) 146959003Shm free(errline); 147013244Sgraichen} 147113244Sgraichen 147259003Shmstatic char * 147359004Shmmissing_field(char *p, char *errline) 147413244Sgraichen{ 147580646Sobrien 147659003Shm if (!p || !*p) 147759003Shm errx(1, "missing field in config file:\n%s", errline); 147859003Shm return (p); 147913244Sgraichen} 148013244Sgraichen 1481130165Sgadstatic fk_entry 1482130165Sgaddo_rotate(const struct conf_entry *ent) 148313244Sgraichen{ 148471299Sjedgar char dirpart[MAXPATHLEN], namepart[MAXPATHLEN]; 148571299Sjedgar char file1[MAXPATHLEN], file2[MAXPATHLEN]; 148671299Sjedgar char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN]; 148780638Sobrien char jfile1[MAXPATHLEN]; 1488130038Sgad int flags, notified, need_notification, numlogs_c; 1489130165Sgad fk_entry free_or_keep; 149059003Shm struct stat st; 149113244Sgraichen 1492119927Sgad flags = ent->flags; 1493130165Sgad free_or_keep = FREE_ENT; 1494119927Sgad 149559004Shm if (archtodir) { 149659004Shm char *p; 149713244Sgraichen 149859004Shm /* build complete name of archive directory into dirpart */ 149959004Shm if (*archdirname == '/') { /* absolute */ 150071299Sjedgar strlcpy(dirpart, archdirname, sizeof(dirpart)); 150159004Shm } else { /* relative */ 150259004Shm /* get directory part of logfile */ 1503119927Sgad strlcpy(dirpart, ent->log, sizeof(dirpart)); 150459004Shm if ((p = rindex(dirpart, '/')) == NULL) 150559004Shm dirpart[0] = '\0'; 150659004Shm else 150759004Shm *(p + 1) = '\0'; 150871299Sjedgar strlcat(dirpart, archdirname, sizeof(dirpart)); 150959004Shm } 151059004Shm 151159004Shm /* check if archive directory exists, if not, create it */ 151259004Shm if (lstat(dirpart, &st)) 1513114137Sgad createdir(ent, dirpart); 151459004Shm 151559004Shm /* get filename part of logfile */ 1516119927Sgad if ((p = rindex(ent->log, '/')) == NULL) 1517119927Sgad strlcpy(namepart, ent->log, sizeof(namepart)); 151859004Shm else 151971299Sjedgar strlcpy(namepart, p + 1, sizeof(namepart)); 152059004Shm 152159004Shm /* name of oldest log */ 152280646Sobrien (void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart, 1523119927Sgad namepart, ent->numlogs); 152471299Sjedgar (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, 152571299Sjedgar COMPRESS_POSTFIX); 152680638Sobrien snprintf(jfile1, sizeof(jfile1), "%s%s", file1, 152780638Sobrien BZCOMPRESS_POSTFIX); 152859004Shm } else { 152959004Shm /* name of oldest log */ 1530119927Sgad (void) snprintf(file1, sizeof(file1), "%s.%d", ent->log, 1531119927Sgad ent->numlogs); 153271299Sjedgar (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, 153371299Sjedgar COMPRESS_POSTFIX); 153480638Sobrien snprintf(jfile1, sizeof(jfile1), "%s%s", file1, 153580638Sobrien BZCOMPRESS_POSTFIX); 153659004Shm } 153759004Shm 153859003Shm if (noaction) { 1539111967Sgad printf("\trm -f %s\n", file1); 1540111967Sgad printf("\trm -f %s\n", zfile1); 1541111967Sgad printf("\trm -f %s\n", jfile1); 154259003Shm } else { 154359003Shm (void) unlink(file1); 154459003Shm (void) unlink(zfile1); 154580638Sobrien (void) unlink(jfile1); 154659003Shm } 154713244Sgraichen 154859003Shm /* Move down log files */ 1549119927Sgad numlogs_c = ent->numlogs; /* copy for countdown */ 1550119927Sgad while (numlogs_c--) { 155159004Shm 155271299Sjedgar (void) strlcpy(file2, file1, sizeof(file2)); 155359004Shm 155459004Shm if (archtodir) 155580646Sobrien (void) snprintf(file1, sizeof(file1), "%s/%s.%d", 1556119927Sgad dirpart, namepart, numlogs_c); 155759004Shm else 1558119927Sgad (void) snprintf(file1, sizeof(file1), "%s.%d", 1559119927Sgad ent->log, numlogs_c); 156059004Shm 156171299Sjedgar (void) strlcpy(zfile1, file1, sizeof(zfile1)); 156271299Sjedgar (void) strlcpy(zfile2, file2, sizeof(zfile2)); 156359003Shm if (lstat(file1, &st)) { 156480646Sobrien (void) strlcat(zfile1, COMPRESS_POSTFIX, 156580646Sobrien sizeof(zfile1)); 156680646Sobrien (void) strlcat(zfile2, COMPRESS_POSTFIX, 156780646Sobrien sizeof(zfile2)); 156880638Sobrien if (lstat(zfile1, &st)) { 156980638Sobrien strlcpy(zfile1, file1, sizeof(zfile1)); 157080638Sobrien strlcpy(zfile2, file2, sizeof(zfile2)); 157180638Sobrien strlcat(zfile1, BZCOMPRESS_POSTFIX, 157280638Sobrien sizeof(zfile1)); 157380638Sobrien strlcat(zfile2, BZCOMPRESS_POSTFIX, 157480638Sobrien sizeof(zfile2)); 157580638Sobrien if (lstat(zfile1, &st)) 157680638Sobrien continue; 157780638Sobrien } 157859003Shm } 1579130165Sgad if (noaction) 1580111967Sgad printf("\tmv %s %s\n", zfile1, zfile2); 1581130165Sgad else { 1582130165Sgad /* XXX - Ought to be checking for failure! */ 1583130165Sgad (void)rename(zfile1, zfile2); 158459003Shm } 1585130165Sgad change_attrs(zfile2, ent); 158659003Shm } 158713244Sgraichen 1588130038Sgad if (ent->numlogs > 0) { 1589129974Sgad if (noaction) { 1590130038Sgad /* 1591130038Sgad * Note that savelog() may succeed with using link() 1592130038Sgad * for the archtodir case, but there is no good way 1593130038Sgad * of knowing if it will when doing "noaction", so 1594130038Sgad * here we claim that it will have to do a copy... 1595130038Sgad */ 1596130038Sgad if (archtodir) 1597130038Sgad printf("\tcp %s %s\n", ent->log, file1); 1598130038Sgad else 1599130038Sgad printf("\tln %s %s\n", ent->log, file1); 1600130165Sgad } else { 1601130038Sgad if (!(flags & CE_BINARY)) { 1602130038Sgad /* Report the trimming to the old log */ 1603130038Sgad log_trim(ent->log, ent); 1604130038Sgad } 1605130038Sgad savelog(ent->log, file1); 160659004Shm } 1607130165Sgad change_attrs(file1, ent); 160818075Sjkh } 160918075Sjkh 1610130038Sgad /* Create the new log file and move it into place */ 1611130038Sgad if (noaction) 1612111781Sgad printf("Start new log...\n"); 1613130038Sgad createlog(ent); 161425443Sache 1615130167Sgad#ifdef TRY_NEWORDER 1616111966Sgad /* 1617130167Sgad * Save all signalling and file-compression to be done after log 1618130167Sgad * files from all entries have been rotated. This way any one 1619130167Sgad * process will not be sent the same signal multiple times when 1620130167Sgad * multiple log files had to be rotated. 1621130167Sgad */ 1622130167Sgad if (dbg_new_order) { 1623130167Sgad struct sigwork_entry *swork; 1624130167Sgad 1625130167Sgad swork = NULL; 1626130167Sgad if (ent->pid_file != NULL) 1627130167Sgad swork = save_sigwork(ent); 1628130167Sgad if (ent->numlogs > 0 && (flags & (CE_COMPACT | CE_BZCOMPACT))) { 1629130167Sgad /* 1630130167Sgad * The zipwork_entry will include a pointer to this 1631130167Sgad * conf_entry, so the conf_entry should not be freed. 1632130167Sgad */ 1633130167Sgad free_or_keep = KEEP_ENT; 1634130167Sgad save_zipwork(ent, swork, ent->fsize, file1); 1635130167Sgad } 1636130167Sgad return (free_or_keep); 1637130167Sgad } 1638130167Sgad#endif /* TRY_NEWORDER */ 1639130167Sgad 1640130167Sgad /* 1641111966Sgad * Find out if there is a process to signal. If nosignal (-s) was 1642111966Sgad * specified, then do not signal any process. Note that nosignal 1643111966Sgad * will trigger a warning message if the rotated logfile needs to 1644111966Sgad * be compressed, *unless* -R was specified. This is because there 1645111966Sgad * presumably still are process(es) writing to the old logfile, but 1646111966Sgad * we assume that a -sR request comes from a process which writes 1647111966Sgad * to the logfile, and as such, that process has already made sure 1648111966Sgad * that the logfile is not presently in use. 1649111966Sgad */ 165025443Sache need_notification = notified = 0; 1651111779Sgad if (ent->pid_file != NULL) { 165225443Sache need_notification = 1; 1653111966Sgad if (!nosignal) 1654112003Sgad notified = send_signal(ent); /* the normal case! */ 1655111966Sgad else if (rotatereq) 1656111966Sgad need_notification = 0; 165725443Sache } 1658112003Sgad 165980638Sobrien if ((flags & CE_COMPACT) || (flags & CE_BZCOMPACT)) { 166025443Sache if (need_notification && !notified) 166180646Sobrien warnx( 1662112003Sgad "log %s.0 not compressed because daemon(s) not notified", 1663119927Sgad ent->log); 1664130165Sgad else if (noaction) { 1665130165Sgad printf("\tsleep 10\n"); 1666111967Sgad if (flags & CE_COMPACT) 1667119927Sgad printf("\tgzip %s.0\n", ent->log); 1668111967Sgad else 1669119927Sgad printf("\tbzip2 %s.0\n", ent->log); 1670130165Sgad } else { 167125443Sache if (notified) { 167225443Sache if (verbose) 1673112003Sgad printf("small pause to allow daemon(s) to close log\n"); 167431460Sache sleep(10); 167525443Sache } 167659004Shm if (archtodir) { 167780646Sobrien (void) snprintf(file1, sizeof(file1), "%s/%s", 167880646Sobrien dirpart, namepart); 167980638Sobrien if (flags & CE_COMPACT) 1680107916Ssobomax compress_log(file1, 1681107916Ssobomax flags & CE_COMPACTWAIT); 168280638Sobrien else if (flags & CE_BZCOMPACT) 1683107916Ssobomax bzcompress_log(file1, 1684107916Ssobomax flags & CE_COMPACTWAIT); 168559004Shm } else { 168680638Sobrien if (flags & CE_COMPACT) 1687119927Sgad compress_log(ent->log, 1688107916Ssobomax flags & CE_COMPACTWAIT); 168980638Sobrien else if (flags & CE_BZCOMPACT) 1690119927Sgad bzcompress_log(ent->log, 1691107916Ssobomax flags & CE_COMPACTWAIT); 169259004Shm } 169325443Sache } 169459003Shm } 1695130165Sgad return (free_or_keep); 169613244Sgraichen} 169713244Sgraichen 1698130167Sgad#ifdef TRY_NEWORDER 1699130167Sgadstatic void 1700130167Sgaddo_sigwork(struct sigwork_entry *swork) 1701130167Sgad{ 1702130204Sgad struct sigwork_entry *nextsig; 1703130204Sgad int kres, secs; 1704130167Sgad 1705130167Sgad if (!(swork->sw_pidok) || swork->sw_pid == 0) 1706130167Sgad return; /* no work to do... */ 1707130167Sgad 1708130167Sgad /* 1709130167Sgad * If nosignal (-s) was specified, then do not signal any process. 1710130167Sgad * Note that a nosignal request triggers a warning message if the 1711130167Sgad * rotated logfile needs to be compressed, *unless* -R was also 1712130167Sgad * specified. We assume that an `-sR' request came from a process 1713130167Sgad * which writes to the logfile, and as such, we assume that process 1714130167Sgad * has already made sure the logfile is not presently in use. This 1715130167Sgad * just sets swork->sw_pidok to a special value, and do_zipwork 1716130167Sgad * will print any necessary warning(s). 1717130167Sgad */ 1718130167Sgad if (nosignal) { 1719130167Sgad if (!rotatereq) 1720130167Sgad swork->sw_pidok = -1; 1721130167Sgad return; 1722130167Sgad } 1723130167Sgad 1724130204Sgad /* 1725130204Sgad * Compute the pause between consecutive signals. Use a longer 1726130204Sgad * sleep time if we will be sending two signals to the same 1727130204Sgad * deamon or process-group. 1728130204Sgad */ 1729130204Sgad secs = 0; 1730130204Sgad nextsig = SLIST_NEXT(swork, sw_nextp); 1731130204Sgad if (nextsig != NULL) { 1732130204Sgad if (swork->sw_pid == nextsig->sw_pid) 1733130204Sgad secs = 10; 1734130204Sgad else 1735130204Sgad secs = 1; 1736130204Sgad } 1737130204Sgad 1738130167Sgad if (noaction) { 1739130204Sgad printf("\tkill -%d %d \t\t# %s\n", swork->sw_signum, 1740130204Sgad (int)swork->sw_pid, swork->sw_fname); 1741130204Sgad if (secs > 0) 1742130204Sgad printf("\tsleep %d\n", secs); 1743130167Sgad return; 1744130167Sgad } 1745130167Sgad 1746130167Sgad kres = kill(swork->sw_pid, swork->sw_signum); 1747130167Sgad if (kres != 0) { 1748130167Sgad /* 1749130167Sgad * Assume that "no such process" (ESRCH) is something 1750130167Sgad * to warn about, but is not an error. Presumably the 1751130167Sgad * process which writes to the rotated log file(s) is 1752130167Sgad * gone, in which case we should have no problem with 1753130167Sgad * compressing the rotated log file(s). 1754130167Sgad */ 1755130167Sgad if (errno != ESRCH) 1756130167Sgad swork->sw_pidok = 0; 1757130167Sgad warn("can't notify %s, pid %d", swork->sw_pidtype, 1758130167Sgad (int)swork->sw_pid); 1759130167Sgad } else { 1760130204Sgad if (verbose) 1761130204Sgad printf("Notified %s pid %d = %s\n", swork->sw_pidtype, 1762130204Sgad (int)swork->sw_pid, swork->sw_fname); 1763130204Sgad if (secs > 0) { 1764130204Sgad if (verbose) 1765130204Sgad printf("Pause %d second(s) between signals\n", 1766130204Sgad secs); 1767130204Sgad sleep(secs); 1768130167Sgad } 1769130167Sgad } 1770130167Sgad} 1771130167Sgad 1772130167Sgadstatic void 1773130167Sgaddo_zipwork(struct zipwork_entry *zwork) 1774130167Sgad{ 1775130167Sgad const char *pgm_name, *pgm_path; 1776154566Sgad int errsav, fcount, zstatus; 1777130167Sgad pid_t pidzip, wpid; 1778130167Sgad char zresult[MAXPATHLEN]; 1779130167Sgad 1780130167Sgad pgm_path = NULL; 1781130167Sgad strlcpy(zresult, zwork->zw_fname, sizeof(zresult)); 1782130167Sgad if (zwork != NULL && zwork->zw_conf != NULL) { 1783130167Sgad if (zwork->zw_conf->flags & CE_COMPACT) { 1784130167Sgad pgm_path = _PATH_GZIP; 1785130167Sgad strlcat(zresult, COMPRESS_POSTFIX, sizeof(zresult)); 1786130167Sgad } else if (zwork->zw_conf->flags & CE_BZCOMPACT) { 1787130167Sgad pgm_path = _PATH_BZIP2; 1788130167Sgad strlcat(zresult, BZCOMPRESS_POSTFIX, sizeof(zresult)); 1789130167Sgad } 1790130167Sgad } 1791130167Sgad if (pgm_path == NULL) { 1792130167Sgad warnx("invalid entry for %s in do_zipwork", zwork->zw_fname); 1793130167Sgad return; 1794130167Sgad } 1795154566Sgad pgm_name = strrchr(pgm_path, '/'); 1796154566Sgad if (pgm_name == NULL) 1797154566Sgad pgm_name = pgm_path; 1798154566Sgad else 1799154566Sgad pgm_name++; 1800130167Sgad 1801130167Sgad if (zwork->zw_swork != NULL && zwork->zw_swork->sw_pidok <= 0) { 1802130167Sgad warnx( 1803130167Sgad "log %s not compressed because daemon(s) not notified", 1804130167Sgad zwork->zw_fname); 1805130167Sgad change_attrs(zwork->zw_fname, zwork->zw_conf); 1806130167Sgad return; 1807130167Sgad } 1808130167Sgad 1809130167Sgad if (noaction) { 1810130167Sgad printf("\t%s %s\n", pgm_name, zwork->zw_fname); 1811130167Sgad change_attrs(zresult, zwork->zw_conf); 1812130167Sgad return; 1813130167Sgad } 1814130167Sgad 1815154566Sgad fcount = 1; 1816130167Sgad pidzip = fork(); 1817154566Sgad while (pidzip < 0) { 1818154566Sgad /* 1819154566Sgad * The fork failed. If the failure was due to a temporary 1820154566Sgad * problem, then wait a short time and try it again. 1821154566Sgad */ 1822154566Sgad errsav = errno; 1823154566Sgad warn("fork() for `%s %s'", pgm_name, zwork->zw_fname); 1824154566Sgad if (errsav != EAGAIN || fcount > 5) 1825154566Sgad errx(1, "Exiting..."); 1826154566Sgad sleep(fcount * 12); 1827154566Sgad fcount++; 1828154566Sgad pidzip = fork(); 1829154566Sgad } 1830154566Sgad if (!pidzip) { 1831130167Sgad /* The child process executes the compression command */ 1832130167Sgad execl(pgm_path, pgm_path, "-f", zwork->zw_fname, (char *)0); 1833154566Sgad err(1, "execl(`%s -f %s')", pgm_path, zwork->zw_fname); 1834130167Sgad } 1835130167Sgad 1836130167Sgad wpid = waitpid(pidzip, &zstatus, 0); 1837130167Sgad if (wpid == -1) { 1838154566Sgad /* XXX - should this be a fatal error? */ 1839130167Sgad warn("%s: waitpid(%d)", pgm_path, pidzip); 1840130167Sgad return; 1841130167Sgad } 1842130167Sgad if (!WIFEXITED(zstatus)) { 1843154566Sgad warnx("`%s -f %s' did not terminate normally", pgm_name, 1844154566Sgad zwork->zw_fname); 1845130167Sgad return; 1846130167Sgad } 1847130167Sgad if (WEXITSTATUS(zstatus)) { 1848154566Sgad warnx("`%s -f %s' terminated with a non-zero status (%d)", 1849154566Sgad pgm_name, zwork->zw_fname, WEXITSTATUS(zstatus)); 1850130167Sgad return; 1851130167Sgad } 1852130167Sgad 1853130167Sgad /* Compression was successful, set file attributes on the result. */ 1854130167Sgad change_attrs(zresult, zwork->zw_conf); 1855130167Sgad} 1856130167Sgad 1857130167Sgad/* 1858130167Sgad * Save information on any process we need to signal. Any single 1859130167Sgad * process may need to be sent different signal-values for different 1860130167Sgad * log files, but usually a single signal-value will cause the process 1861130167Sgad * to close and re-open all of it's log files. 1862130167Sgad */ 1863130167Sgadstatic struct sigwork_entry * 1864130167Sgadsave_sigwork(const struct conf_entry *ent) 1865130167Sgad{ 1866130167Sgad struct sigwork_entry *sprev, *stmp; 1867130167Sgad int ndiff; 1868130167Sgad size_t tmpsiz; 1869130167Sgad 1870130167Sgad sprev = NULL; 1871130167Sgad ndiff = 1; 1872130167Sgad SLIST_FOREACH(stmp, &swhead, sw_nextp) { 1873130167Sgad ndiff = strcmp(ent->pid_file, stmp->sw_fname); 1874130167Sgad if (ndiff > 0) 1875130167Sgad break; 1876130167Sgad if (ndiff == 0) { 1877130167Sgad if (ent->sig == stmp->sw_signum) 1878130167Sgad break; 1879130167Sgad if (ent->sig > stmp->sw_signum) { 1880130167Sgad ndiff = 1; 1881130167Sgad break; 1882130167Sgad } 1883130167Sgad } 1884130167Sgad sprev = stmp; 1885130167Sgad } 1886130167Sgad if (stmp != NULL && ndiff == 0) 1887130167Sgad return (stmp); 1888130167Sgad 1889130167Sgad tmpsiz = sizeof(struct sigwork_entry) + strlen(ent->pid_file) + 1; 1890130167Sgad stmp = malloc(tmpsiz); 1891130167Sgad set_swpid(stmp, ent); 1892130167Sgad stmp->sw_signum = ent->sig; 1893130167Sgad strcpy(stmp->sw_fname, ent->pid_file); 1894130167Sgad if (sprev == NULL) 1895130167Sgad SLIST_INSERT_HEAD(&swhead, stmp, sw_nextp); 1896130167Sgad else 1897130167Sgad SLIST_INSERT_AFTER(sprev, stmp, sw_nextp); 1898130167Sgad return (stmp); 1899130167Sgad} 1900130167Sgad 1901130167Sgad/* 1902130167Sgad * Save information on any file we need to compress. We may see the same 1903130167Sgad * file multiple times, so check the full list to avoid duplicates. The 1904130167Sgad * list itself is sorted smallest-to-largest, because that's the order we 1905130167Sgad * want to compress the files. If the partition is very low on disk space, 1906130167Sgad * then the smallest files are the most likely to compress, and compressing 1907130167Sgad * them first will free up more space for the larger files. 1908130167Sgad */ 1909130167Sgadstatic struct zipwork_entry * 1910130167Sgadsave_zipwork(const struct conf_entry *ent, const struct sigwork_entry *swork, 1911130167Sgad int zsize, const char *zipfname) 1912130167Sgad{ 1913130167Sgad struct zipwork_entry *zprev, *ztmp; 1914130167Sgad int ndiff; 1915130167Sgad size_t tmpsiz; 1916130167Sgad 1917130167Sgad /* Compute the size if the caller did not know it. */ 1918130167Sgad if (zsize < 0) 1919130167Sgad zsize = sizefile(zipfname); 1920130167Sgad 1921130167Sgad zprev = NULL; 1922130167Sgad ndiff = 1; 1923130167Sgad SLIST_FOREACH(ztmp, &zwhead, zw_nextp) { 1924130707Sgad ndiff = strcmp(zipfname, ztmp->zw_fname); 1925130167Sgad if (ndiff == 0) 1926130167Sgad break; 1927130167Sgad if (zsize > ztmp->zw_fsize) 1928130167Sgad zprev = ztmp; 1929130167Sgad } 1930130167Sgad if (ztmp != NULL && ndiff == 0) 1931130167Sgad return (ztmp); 1932130167Sgad 1933130167Sgad tmpsiz = sizeof(struct zipwork_entry) + strlen(zipfname) + 1; 1934130167Sgad ztmp = malloc(tmpsiz); 1935130167Sgad ztmp->zw_conf = ent; 1936130167Sgad ztmp->zw_swork = swork; 1937130167Sgad ztmp->zw_fsize = zsize; 1938130167Sgad strcpy(ztmp->zw_fname, zipfname); 1939130167Sgad if (zprev == NULL) 1940130167Sgad SLIST_INSERT_HEAD(&zwhead, ztmp, zw_nextp); 1941130167Sgad else 1942130167Sgad SLIST_INSERT_AFTER(zprev, ztmp, zw_nextp); 1943130167Sgad return (ztmp); 1944130167Sgad} 1945130167Sgad 1946130167Sgad/* Send a signal to the pid specified by pidfile */ 1947130167Sgadstatic void 1948130167Sgadset_swpid(struct sigwork_entry *swork, const struct conf_entry *ent) 1949130167Sgad{ 1950130167Sgad FILE *f; 1951130167Sgad long minok, maxok, rval; 1952130167Sgad char *endp, *linep, line[BUFSIZ]; 1953130167Sgad 1954130167Sgad minok = MIN_PID; 1955130167Sgad maxok = MAX_PID; 1956130167Sgad swork->sw_pidok = 0; 1957130167Sgad swork->sw_pid = 0; 1958130167Sgad swork->sw_pidtype = "daemon"; 1959130167Sgad if (ent->flags & CE_SIGNALGROUP) { 1960130167Sgad /* 1961130167Sgad * If we are expected to signal a process-group when 1962130167Sgad * rotating this logfile, then the value read in should 1963130167Sgad * be the negative of a valid process ID. 1964130167Sgad */ 1965130167Sgad minok = -MAX_PID; 1966130167Sgad maxok = -MIN_PID; 1967130167Sgad swork->sw_pidtype = "process-group"; 1968130167Sgad } 1969130167Sgad 1970130167Sgad f = fopen(ent->pid_file, "r"); 1971130167Sgad if (f == NULL) { 1972130167Sgad warn("can't open pid file: %s", ent->pid_file); 1973130167Sgad return; 1974130167Sgad } 1975130167Sgad 1976130167Sgad if (fgets(line, BUFSIZ, f) == NULL) { 1977130167Sgad /* 1978130167Sgad * Warn if the PID file is empty, but do not consider 1979130167Sgad * it an error. Most likely it means the process has 1980130167Sgad * has terminated, so it should be safe to rotate any 1981130167Sgad * log files that the process would have been using. 1982130167Sgad */ 1983130167Sgad if (feof(f)) { 1984130167Sgad swork->sw_pidok = 1; 1985130167Sgad warnx("pid file is empty: %s", ent->pid_file); 1986130167Sgad } else 1987130167Sgad warn("can't read from pid file: %s", ent->pid_file); 1988130167Sgad (void)fclose(f); 1989130167Sgad return; 1990130167Sgad } 1991130167Sgad (void)fclose(f); 1992130167Sgad 1993130167Sgad errno = 0; 1994130167Sgad linep = line; 1995130167Sgad while (*linep == ' ') 1996130167Sgad linep++; 1997130167Sgad rval = strtol(linep, &endp, 10); 1998130167Sgad if (*endp != '\0' && !isspacech(*endp)) { 1999130167Sgad warnx("pid file does not start with a valid number: %s", 2000130167Sgad ent->pid_file); 2001130167Sgad } else if (rval < minok || rval > maxok) { 2002130167Sgad warnx("bad value '%ld' for process number in %s", 2003130167Sgad rval, ent->pid_file); 2004130167Sgad if (verbose) 2005130167Sgad warnx("\t(expecting value between %ld and %ld)", 2006130167Sgad minok, maxok); 2007130167Sgad } else { 2008130167Sgad swork->sw_pidok = 1; 2009130167Sgad swork->sw_pid = rval; 2010130167Sgad } 2011130167Sgad 2012130167Sgad return; 2013130167Sgad} 2014130167Sgad#endif /* TRY_NEWORDER */ 2015130167Sgad 201613244Sgraichen/* Log the fact that the logs were turned over */ 201759004Shmstatic int 2018119926Sgadlog_trim(const char *logname, const struct conf_entry *log_ent) 201913244Sgraichen{ 202059003Shm FILE *f; 2021111388Sgad const char *xtra; 202259003Shm 2023119926Sgad if ((f = fopen(logname, "a")) == NULL) 202459003Shm return (-1); 2025111388Sgad xtra = ""; 2026111768Sgad if (log_ent->def_cfg) 2027111388Sgad xtra = " using <default> rule"; 2028114137Sgad if (log_ent->firstcreate) 2029114137Sgad fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n", 2030114137Sgad daytime, hostname, (int) getpid(), xtra); 2031114137Sgad else if (log_ent->r_reason != NULL) 2032111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n", 2033111772Sgad daytime, hostname, (int) getpid(), log_ent->r_reason, xtra); 2034111772Sgad else 2035111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n", 2036111772Sgad daytime, hostname, (int) getpid(), xtra); 203759003Shm if (fclose(f) == EOF) 2038127858Scharnier err(1, "log_trim: fclose"); 203959003Shm return (0); 204013244Sgraichen} 204113244Sgraichen 2042130043Sgad/* 2043130043Sgad * XXX - Note that both compress_log and bzcompress_log will lose the 2044130043Sgad * NODUMP flag if it was set on somelog.0. Fixing that in newsyslog 2045130043Sgad * (as opposed to fixing gzip/bzip2) will require some restructuring 2046130043Sgad * of the code. That restructuring is planned for a later update... 2047130043Sgad */ 204859004Shm/* Fork of gzip to compress the old log file */ 204959004Shmstatic void 2050119926Sgadcompress_log(char *logname, int dowait) 205113244Sgraichen{ 205259003Shm pid_t pid; 205371299Sjedgar char tmp[MAXPATHLEN]; 205459003Shm 2055107916Ssobomax while (dowait && (wait(NULL) > 0 || errno == EINTR)) 2056107916Ssobomax ; 2057119926Sgad (void) snprintf(tmp, sizeof(tmp), "%s.0", logname); 205825443Sache pid = fork(); 205959003Shm if (pid < 0) 206080638Sobrien err(1, "gzip fork"); 206159003Shm else if (!pid) { 206279452Sbrian (void) execl(_PATH_GZIP, _PATH_GZIP, "-f", tmp, (char *)0); 206343071Swollman err(1, _PATH_GZIP); 206459003Shm } 206513244Sgraichen} 206613244Sgraichen 206780638Sobrien/* Fork of bzip2 to compress the old log file */ 206880638Sobrienstatic void 2069119926Sgadbzcompress_log(char *logname, int dowait) 207080638Sobrien{ 207180638Sobrien pid_t pid; 207280638Sobrien char tmp[MAXPATHLEN]; 207380638Sobrien 2074107916Ssobomax while (dowait && (wait(NULL) > 0 || errno == EINTR)) 2075107916Ssobomax ; 2076119926Sgad snprintf(tmp, sizeof(tmp), "%s.0", logname); 207780638Sobrien pid = fork(); 207880638Sobrien if (pid < 0) 207980638Sobrien err(1, "bzip2 fork"); 208080638Sobrien else if (!pid) { 208186360Sobrien execl(_PATH_BZIP2, _PATH_BZIP2, "-f", tmp, (char *)0); 208280638Sobrien err(1, _PATH_BZIP2); 208380638Sobrien } 208480638Sobrien} 208580638Sobrien 208613244Sgraichen/* Return size in kilobytes of a file */ 208759004Shmstatic int 2088130165Sgadsizefile(const char *file) 208913244Sgraichen{ 209059003Shm struct stat sb; 209113244Sgraichen 209259003Shm if (stat(file, &sb) < 0) 209359003Shm return (-1); 209459003Shm return (kbytes(dbtob(sb.st_blocks))); 209513244Sgraichen} 209613244Sgraichen 209713244Sgraichen/* Return the age of old log file (file.0) */ 209859004Shmstatic int 209959004Shmage_old_log(char *file) 210013244Sgraichen{ 210159003Shm struct stat sb; 2102114764Sgad char *endp; 2103114764Sgad char tmp[MAXPATHLEN + sizeof(".0") + sizeof(COMPRESS_POSTFIX) + 2104114764Sgad sizeof(BZCOMPRESS_POSTFIX) + 1]; 210513244Sgraichen 210659004Shm if (archtodir) { 210759004Shm char *p; 210859004Shm 210959004Shm /* build name of archive directory into tmp */ 211059004Shm if (*archdirname == '/') { /* absolute */ 211171299Sjedgar strlcpy(tmp, archdirname, sizeof(tmp)); 211259004Shm } else { /* relative */ 211359004Shm /* get directory part of logfile */ 211471299Sjedgar strlcpy(tmp, file, sizeof(tmp)); 211559004Shm if ((p = rindex(tmp, '/')) == NULL) 211659004Shm tmp[0] = '\0'; 211759004Shm else 211859004Shm *(p + 1) = '\0'; 211971299Sjedgar strlcat(tmp, archdirname, sizeof(tmp)); 212059004Shm } 212159004Shm 212271299Sjedgar strlcat(tmp, "/", sizeof(tmp)); 212359004Shm 212459004Shm /* get filename part of logfile */ 212559004Shm if ((p = rindex(file, '/')) == NULL) 212671299Sjedgar strlcat(tmp, file, sizeof(tmp)); 212759004Shm else 212871299Sjedgar strlcat(tmp, p + 1, sizeof(tmp)); 212959004Shm } else { 213071299Sjedgar (void) strlcpy(tmp, file, sizeof(tmp)); 213159004Shm } 213259004Shm 2133114764Sgad strlcat(tmp, ".0", sizeof(tmp)); 2134114764Sgad if (stat(tmp, &sb) < 0) { 2135114764Sgad /* 2136114764Sgad * A plain '.0' file does not exist. Try again, first 2137114764Sgad * with the added suffix of '.gz', then with an added 2138114764Sgad * suffix of '.bz2' instead of '.gz'. 2139114764Sgad */ 2140114764Sgad endp = strchr(tmp, '\0'); 2141114764Sgad strlcat(tmp, COMPRESS_POSTFIX, sizeof(tmp)); 2142114764Sgad if (stat(tmp, &sb) < 0) { 2143114764Sgad *endp = '\0'; /* Remove .gz */ 2144114764Sgad strlcat(tmp, BZCOMPRESS_POSTFIX, sizeof(tmp)); 2145114764Sgad if (stat(tmp, &sb) < 0) 2146114764Sgad return (-1); 2147114764Sgad } 2148114764Sgad } 2149120361Sgad return ((int)(ptimeget_secs(timenow) - sb.st_mtime + 1800) / 3600); 215013244Sgraichen} 215113244Sgraichen 215213244Sgraichen/* Skip Over Blanks */ 2153111820Sgadstatic char * 215459004Shmsob(char *p) 215513244Sgraichen{ 215659003Shm while (p && *p && isspace(*p)) 215759003Shm p++; 215859003Shm return (p); 215913244Sgraichen} 216013244Sgraichen 216113244Sgraichen/* Skip Over Non-Blanks */ 2162111820Sgadstatic char * 216359004Shmson(char *p) 216413244Sgraichen{ 216559003Shm while (p && *p && !isspace(*p)) 216659003Shm p++; 216759003Shm return (p); 216813244Sgraichen} 216943071Swollman 2170119102Sgad/* Check if string is actually a number */ 2171119102Sgadstatic int 2172119102Sgadisnumberstr(const char *string) 2173119102Sgad{ 2174119102Sgad while (*string) { 2175119102Sgad if (!isdigitch(*string++)) 2176119102Sgad return (0); 2177119102Sgad } 2178119102Sgad return (1); 2179119102Sgad} 2180119102Sgad 2181130038Sgad/* 2182130038Sgad * Save the active log file under a new name. A link to the new name 2183130038Sgad * is the quick-and-easy way to do this. If that fails (which it will 2184130038Sgad * if the destination is on another partition), then make a copy of 2185130038Sgad * the file to the new location. 2186130038Sgad */ 218759004Shmstatic void 2188130038Sgadsavelog(char *from, char *to) 218959004Shm{ 219059004Shm FILE *src, *dst; 2191130038Sgad int c, res; 219259004Shm 2193130038Sgad res = link(from, to); 2194130038Sgad if (res == 0) 2195130038Sgad return; 2196130038Sgad 219759004Shm if ((src = fopen(from, "r")) == NULL) 219859004Shm err(1, "can't fopen %s for reading", from); 219959004Shm if ((dst = fopen(to, "w")) == NULL) 220059004Shm err(1, "can't fopen %s for writing", to); 220159004Shm 220259004Shm while ((c = getc(src)) != EOF) { 220359004Shm if ((putc(c, dst)) == EOF) 220459004Shm err(1, "error writing to %s", to); 220559004Shm } 220659004Shm 220759004Shm if (ferror(src)) 220859004Shm err(1, "error reading from %s", from); 220959004Shm if ((fclose(src)) != 0) 221059004Shm err(1, "can't fclose %s", to); 221159004Shm if ((fclose(dst)) != 0) 221259004Shm err(1, "can't fclose %s", from); 221359004Shm} 221459004Shm 221559004Shm/* create one or more directory components of a path */ 221659004Shmstatic void 2217114137Sgadcreatedir(const struct conf_entry *ent, char *dirpart) 221859004Shm{ 2219111398Sgad int res; 222059004Shm char *s, *d; 222171299Sjedgar char mkdirpath[MAXPATHLEN]; 222259004Shm struct stat st; 222359004Shm 222459004Shm s = dirpart; 222559004Shm d = mkdirpath; 222659004Shm 222759004Shm for (;;) { 222859004Shm *d++ = *s++; 2229111398Sgad if (*s != '/' && *s != '\0') 2230111398Sgad continue; 2231111398Sgad *d = '\0'; 2232111398Sgad res = lstat(mkdirpath, &st); 2233111398Sgad if (res != 0) { 2234111398Sgad if (noaction) { 2235111967Sgad printf("\tmkdir %s\n", mkdirpath); 2236111398Sgad } else { 2237111398Sgad res = mkdir(mkdirpath, 0755); 2238111398Sgad if (res != 0) 2239111398Sgad err(1, "Error on mkdir(\"%s\") for -a", 2240111398Sgad mkdirpath); 2241111398Sgad } 224259004Shm } 224359004Shm if (*s == '\0') 224459004Shm break; 224559004Shm } 2246114137Sgad if (verbose) { 2247114137Sgad if (ent->firstcreate) 2248114137Sgad printf("Created directory '%s' for new %s\n", 2249114137Sgad dirpart, ent->log); 2250114137Sgad else 2251114137Sgad printf("Created directory '%s' for -a\n", dirpart); 2252114137Sgad } 225359004Shm} 225459004Shm 2255114137Sgad/* 2256114137Sgad * Create a new log file, destroying any currently-existing version 2257114137Sgad * of the log file in the process. If the caller wants a backup copy 2258114137Sgad * of the file to exist, they should call 'link(logfile,logbackup)' 2259114137Sgad * before calling this routine. 2260114137Sgad */ 2261114137Sgadvoid 2262114137Sgadcreatelog(const struct conf_entry *ent) 2263114137Sgad{ 2264114137Sgad int fd, failed; 2265114137Sgad struct stat st; 2266114137Sgad char *realfile, *slash, tempfile[MAXPATHLEN]; 2267114137Sgad 2268114137Sgad fd = -1; 2269114137Sgad realfile = ent->log; 2270114137Sgad 2271114137Sgad /* 2272114137Sgad * If this log file is being created for the first time (-C option), 2273114137Sgad * then it may also be true that the parent directory does not exist 2274114137Sgad * yet. Check, and create that directory if it is missing. 2275114137Sgad */ 2276114137Sgad if (ent->firstcreate) { 2277114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 2278114137Sgad slash = strrchr(tempfile, '/'); 2279114137Sgad if (slash != NULL) { 2280114137Sgad *slash = '\0'; 2281131581Ssobomax failed = stat(tempfile, &st); 2282114137Sgad if (failed && errno != ENOENT) 2283131581Ssobomax err(1, "Error on stat(%s)", tempfile); 2284114137Sgad if (failed) 2285114137Sgad createdir(ent, tempfile); 2286114137Sgad else if (!S_ISDIR(st.st_mode)) 2287114137Sgad errx(1, "%s exists but is not a directory", 2288114137Sgad tempfile); 2289114137Sgad } 2290114137Sgad } 2291114137Sgad 2292114137Sgad /* 2293114137Sgad * First create an unused filename, so it can be chown'ed and 2294114137Sgad * chmod'ed before it is moved into the real location. mkstemp 2295114137Sgad * will create the file mode=600 & owned by us. Note that all 2296114137Sgad * temp files will have a suffix of '.z<something>'. 2297114137Sgad */ 2298114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 2299114137Sgad strlcat(tempfile, ".zXXXXXX", sizeof(tempfile)); 2300114137Sgad if (noaction) 2301114137Sgad printf("\tmktemp %s\n", tempfile); 2302114137Sgad else { 2303114137Sgad fd = mkstemp(tempfile); 2304114137Sgad if (fd < 0) 2305114137Sgad err(1, "can't mkstemp logfile %s", tempfile); 2306114137Sgad 2307114137Sgad /* 2308114137Sgad * Add status message to what will become the new log file. 2309114137Sgad */ 2310114137Sgad if (!(ent->flags & CE_BINARY)) { 2311114137Sgad if (log_trim(tempfile, ent)) 2312114137Sgad err(1, "can't add status message to log"); 2313114137Sgad } 2314114137Sgad } 2315114137Sgad 2316114137Sgad /* Change the owner/group, if we are supposed to */ 2317114137Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 2318114137Sgad if (noaction) 2319114137Sgad printf("\tchown %u:%u %s\n", ent->uid, ent->gid, 2320114137Sgad tempfile); 2321114137Sgad else { 2322114137Sgad failed = fchown(fd, ent->uid, ent->gid); 2323114137Sgad if (failed) 2324114137Sgad err(1, "can't fchown temp file %s", tempfile); 2325114137Sgad } 2326114137Sgad } 2327114137Sgad 2328130043Sgad /* Turn on NODUMP if it was requested in the config-file. */ 2329130043Sgad if (ent->flags & CE_NODUMP) { 2330130043Sgad if (noaction) 2331130043Sgad printf("\tchflags nodump %s\n", tempfile); 2332130043Sgad else { 2333130043Sgad failed = fchflags(fd, UF_NODUMP); 2334130043Sgad if (failed) { 2335130043Sgad warn("log_trim: fchflags(NODUMP)"); 2336130043Sgad } 2337130043Sgad } 2338130043Sgad } 2339130043Sgad 2340114137Sgad /* 2341114137Sgad * Note that if the real logfile still exists, and if the call 2342114137Sgad * to rename() fails, then "neither the old file nor the new 2343114137Sgad * file shall be changed or created" (to quote the standard). 2344114137Sgad * If the call succeeds, then the file will be replaced without 2345114137Sgad * any window where some other process might find that the file 2346114137Sgad * did not exist. 2347114137Sgad * XXX - ? It may be that for some error conditions, we could 2348114137Sgad * retry by first removing the realfile and then renaming. 2349114137Sgad */ 2350114137Sgad if (noaction) { 2351114137Sgad printf("\tchmod %o %s\n", ent->permissions, tempfile); 2352114137Sgad printf("\tmv %s %s\n", tempfile, realfile); 2353114137Sgad } else { 2354114137Sgad failed = fchmod(fd, ent->permissions); 2355114137Sgad if (failed) 2356114137Sgad err(1, "can't fchmod temp file '%s'", tempfile); 2357114137Sgad failed = rename(tempfile, realfile); 2358114137Sgad if (failed) 2359114137Sgad err(1, "can't mv %s to %s", tempfile, realfile); 2360114137Sgad } 2361114137Sgad 2362114137Sgad if (fd >= 0) 2363114137Sgad close(fd); 2364114137Sgad} 2365130165Sgad 2366154566Sgad/* 2367154566Sgad * Change the attributes of a given filename to what was specified in 2368154566Sgad * the newsyslog.conf entry. This routine is only called for files 2369154566Sgad * that newsyslog expects that it has created, and thus it is a fatal 2370154566Sgad * error if this routine finds that the file does not exist. 2371154566Sgad */ 2372130165Sgadstatic void 2373130165Sgadchange_attrs(const char *fname, const struct conf_entry *ent) 2374130165Sgad{ 2375130165Sgad int failed; 2376130165Sgad 2377130165Sgad if (noaction) { 2378130165Sgad printf("\tchmod %o %s\n", ent->permissions, fname); 2379130165Sgad 2380130165Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 2381130165Sgad printf("\tchown %u:%u %s\n", 2382130165Sgad ent->uid, ent->gid, fname); 2383130165Sgad 2384130165Sgad if (ent->flags & CE_NODUMP) 2385130165Sgad printf("\tchflags nodump %s\n", fname); 2386130165Sgad return; 2387130165Sgad } 2388130165Sgad 2389130165Sgad failed = chmod(fname, ent->permissions); 2390154566Sgad if (failed) { 2391154566Sgad if (errno != EPERM) 2392154566Sgad err(1, "chmod(%s) in change_attrs", fname); 2393154566Sgad warn("change_attrs couldn't chmod(%s)", fname); 2394154566Sgad } 2395130165Sgad 2396130165Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 2397130165Sgad failed = chown(fname, ent->uid, ent->gid); 2398130165Sgad if (failed) 2399130165Sgad warn("can't chown %s", fname); 2400130165Sgad } 2401130165Sgad 2402130165Sgad if (ent->flags & CE_NODUMP) { 2403130165Sgad failed = chflags(fname, UF_NODUMP); 2404130165Sgad if (failed) 2405130165Sgad warn("can't chflags %s NODUMP", fname); 2406130165Sgad } 2407130165Sgad} 2408