2 * ------+---------+---------+-------- + --------+---------+---------+---------*
3 * This file includes significant modifications done by:
4 * Copyright (c) 2003, 2004 - Garance Alistair Drosehn <gad@FreeBSD.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * ------+---------+---------+-------- + --------+---------+---------+---------*
32 * This file contains changes from the Open Software Foundation.
36 * Copyright 1988, 1989 by the Massachusetts Institute of Technology
38 * Permission to use, copy, modify, and distribute this software and its
39 * documentation for any purpose and without fee is hereby granted, provided
40 * that the above copyright notice appear in all copies and that both that
41 * copyright notice and this permission notice appear in supporting
42 * documentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
43 * used in advertising or publicity pertaining to distribution of the
44 * software without specific, written prior permission. M.I.T. and the M.I.T.
45 * S.I.P.B. make no representations about the suitability of this software
46 * for any purpose. It is provided "as is" without express or implied
52 * newsyslog - roll over selected logs at the appropriate time, keeping the a
53 * specified number of backup files around.
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
61 #include <sys/param.h>
62 #include <sys/queue.h>
85 #include "pathnames.h"
89 * Compression suffixes
91 #ifndef COMPRESS_SUFFIX_GZ
92 #define COMPRESS_SUFFIX_GZ ".gz"
95 #ifndef COMPRESS_SUFFIX_BZ2
96 #define COMPRESS_SUFFIX_BZ2 ".bz2"
99 #ifndef COMPRESS_SUFFIX_XZ
100 #define COMPRESS_SUFFIX_XZ ".xz"
103 #define COMPRESS_SUFFIX_MAXLEN MAX(MAX(sizeof(COMPRESS_SUFFIX_GZ),sizeof(COMPRESS_SUFFIX_BZ2)),sizeof(COMPRESS_SUFFIX_XZ))
108 #define COMPRESS_TYPES 4 /* Number of supported compression types */
110 #define COMPRESS_NONE 0
111 #define COMPRESS_GZIP 1
112 #define COMPRESS_BZIP2 2
113 #define COMPRESS_XZ 3
116 * Bit-values for the 'flags' parsed from a config-file entry.
118 #define CE_BINARY 0x0008 /* Logfile is in binary, do not add status */
119 /* messages to logfile(s) when rotating. */
120 #define CE_NOSIGNAL 0x0010 /* There is no process to signal when */
121 /* trimming this file. */
122 #define CE_TRIMAT 0x0020 /* trim file at a specific time. */
123 #define CE_GLOB 0x0040 /* name of the log is file name pattern. */
124 #define CE_SIGNALGROUP 0x0080 /* Signal a process-group instead of a single */
125 /* process when trimming this file. */
126 #define CE_CREATE 0x0100 /* Create the log file if it does not exist. */
127 #define CE_NODUMP 0x0200 /* Set 'nodump' on newly created log file. */
128 #define CE_PID2CMD 0x0400 /* Replace PID file with a shell command.*/
130 #define MIN_PID 5 /* Don't touch pids lower than this */
131 #define MAX_PID 99999 /* was lower, see /usr/include/sys/proc.h */
133 #define kbytes(size) (((size) + 1023) >> 10)
135 #define DEFAULT_MARKER "<default>"
136 #define DEBUG_MARKER "<debug>"
137 #define INCLUDE_MARKER "<include>"
138 #define DEFAULT_TIMEFNAME_FMT "%Y%m%dT%H%M%S"
140 #define MAX_OLDLOGS 65536 /* Default maximum number of old logfiles */
142 struct compress_types {
143 const char *flag; /* Flag in configuration file */
144 const char *suffix; /* Compression suffix */
145 const char *path; /* Path to compression program */
148 const struct compress_types compress_type[COMPRESS_TYPES] = {
149 { "", "", "" }, /* no compression */
150 { "Z", COMPRESS_SUFFIX_GZ, _PATH_GZIP }, /* gzip compression */
151 { "J", COMPRESS_SUFFIX_BZ2, _PATH_BZIP2 }, /* bzip2 compression */
152 { "X", COMPRESS_SUFFIX_XZ, _PATH_XZ } /* xz compression */
156 STAILQ_ENTRY(conf_entry) cf_nextp;
157 char *log; /* Name of the log */
158 char *pid_cmd_file; /* PID or command file */
159 char *r_reason; /* The reason this file is being rotated */
160 int firstcreate; /* Creating log for the first time (-C). */
161 int rotate; /* Non-zero if this file should be rotated */
162 int fsize; /* size found for the log file */
163 uid_t uid; /* Owner of log */
164 gid_t gid; /* Group of log */
165 int numlogs; /* Number of logs to keep */
166 int trsize; /* Size cutoff to trigger trimming the log */
167 int hours; /* Hours between log trimming */
168 struct ptime_data *trim_at; /* Specific time to do trimming */
169 unsigned int permissions; /* File permissions on the log */
170 int flags; /* CE_BINARY */
171 int compress; /* Compression */
172 int sig; /* Signal to send */
173 int def_cfg; /* Using the <default> rule for this file */
176 struct sigwork_entry {
177 SLIST_ENTRY(sigwork_entry) sw_nextp;
178 int sw_signum; /* the signal to send */
179 int sw_pidok; /* true if pid value is valid */
180 pid_t sw_pid; /* the process id from the PID file */
181 const char *sw_pidtype; /* "daemon" or "process group" */
182 int run_cmd; /* run command or send PID to signal */
183 char sw_fname[1]; /* file the PID was read from or shell cmd */
186 struct zipwork_entry {
187 SLIST_ENTRY(zipwork_entry) zw_nextp;
188 const struct conf_entry *zw_conf; /* for chown/perm/flag info */
189 const struct sigwork_entry *zw_swork; /* to know success of signal */
190 int zw_fsize; /* size of the file to compress */
191 char zw_fname[1]; /* the file to compress */
194 struct include_entry {
195 STAILQ_ENTRY(include_entry) inc_nextp;
196 const char *file; /* Name of file to process */
199 struct oldlog_entry {
200 char *fname; /* Filename of the log file */
201 time_t t; /* Parsed timestamp of the logfile */
208 STAILQ_HEAD(cflist, conf_entry);
209 SLIST_HEAD(swlisthead, sigwork_entry) swhead = SLIST_HEAD_INITIALIZER(swhead);
210 SLIST_HEAD(zwlisthead, zipwork_entry) zwhead = SLIST_HEAD_INITIALIZER(zwhead);
211 STAILQ_HEAD(ilist, include_entry);
213 int dbg_at_times; /* -D Show details of 'trim_at' code */
215 int archtodir = 0; /* Archive old logfiles to other directory */
216 int createlogs; /* Create (non-GLOB) logfiles which do not */
217 /* already exist. 1=='for entries with */
218 /* C flag', 2=='for all entries'. */
219 int verbose = 0; /* Print out what's going on */
220 int needroot = 1; /* Root privs are necessary */
221 int noaction = 0; /* Don't do anything, just show it */
222 int norotate = 0; /* Don't rotate */
223 int nosignal; /* Do not send any signals */
224 int enforcepid = 0; /* If PID file does not exist or empty, do nothing */
225 int force = 0; /* Force the trim no matter what */
226 int rotatereq = 0; /* -R = Always rotate the file(s) as given */
227 /* on the command (this also requires */
228 /* that a list of files *are* given on */
229 /* the run command). */
230 char *requestor; /* The name given on a -R request */
231 char *timefnamefmt = NULL; /* Use time based filenames instead of .0 etc */
232 char *archdirname; /* Directory path to old logfiles archive */
233 char *destdir = NULL; /* Directory to treat at root for logs */
234 const char *conf; /* Configuration file to use */
236 struct ptime_data *dbg_timenow; /* A "timenow" value set via -D option */
237 struct ptime_data *timenow; /* The time to use for checking at-fields */
239 #define DAYTIME_LEN 16
240 char daytime[DAYTIME_LEN]; /* The current time in human readable form,
241 * used for rotation-tracking messages. */
242 char hostname[MAXHOSTNAMELEN]; /* hostname */
244 const char *path_syslogpid = _PATH_SYSLOGPID;
246 static struct cflist *get_worklist(char **files);
247 static void parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p,
248 struct conf_entry *defconf_p, struct ilist *inclist);
249 static void add_to_queue(const char *fname, struct ilist *inclist);
250 static char *sob(char *p);
251 static char *son(char *p);
252 static int isnumberstr(const char *);
253 static int isglobstr(const char *);
254 static char *missing_field(char *p, char *errline);
255 static void change_attrs(const char *, const struct conf_entry *);
256 static const char *get_logfile_suffix(const char *logfile);
257 static fk_entry do_entry(struct conf_entry *);
258 static fk_entry do_rotate(const struct conf_entry *);
259 static void do_sigwork(struct sigwork_entry *);
260 static void do_zipwork(struct zipwork_entry *);
261 static struct sigwork_entry *
262 save_sigwork(const struct conf_entry *);
263 static struct zipwork_entry *
264 save_zipwork(const struct conf_entry *, const struct
265 sigwork_entry *, int, const char *);
266 static void set_swpid(struct sigwork_entry *, const struct conf_entry *);
267 static int sizefile(const char *);
268 static void expand_globs(struct cflist *work_p, struct cflist *glob_p);
269 static void free_clist(struct cflist *list);
270 static void free_entry(struct conf_entry *ent);
271 static struct conf_entry *init_entry(const char *fname,
272 struct conf_entry *src_entry);
273 static void parse_args(int argc, char **argv);
274 static int parse_doption(const char *doption);
275 static void usage(void);
276 static int log_trim(const char *logname, const struct conf_entry *log_ent);
277 static int age_old_log(char *file);
278 static void savelog(char *from, char *to);
279 static void createdir(const struct conf_entry *ent, char *dirpart);
280 static void createlog(const struct conf_entry *ent);
283 * All the following take a parameter of 'int', but expect values in the
284 * range of unsigned char. Define wrappers which take values of type 'char',
285 * whether signed or unsigned, and ensure they end up in the right range.
287 #define isdigitch(Anychar) isdigit((u_char)(Anychar))
288 #define isprintch(Anychar) isprint((u_char)(Anychar))
289 #define isspacech(Anychar) isspace((u_char)(Anychar))
290 #define tolowerch(Anychar) tolower((u_char)(Anychar))
293 main(int argc, char **argv)
295 struct cflist *worklist;
296 struct conf_entry *p;
297 struct sigwork_entry *stmp;
298 struct zipwork_entry *ztmp;
303 parse_args(argc, argv);
307 if (needroot && getuid() && geteuid())
308 errx(1, "must have root privs");
309 worklist = get_worklist(argv);
312 * Rotate all the files which need to be rotated. Note that
313 * some users have *hundreds* of entries in newsyslog.conf!
315 while (!STAILQ_EMPTY(worklist)) {
316 p = STAILQ_FIRST(worklist);
317 STAILQ_REMOVE_HEAD(worklist, cf_nextp);
318 if (do_entry(p) == FREE_ENT)
323 * Send signals to any processes which need a signal to tell
324 * them to close and re-open the log file(s) we have rotated.
325 * Note that zipwork_entries include pointers to these
326 * sigwork_entry's, so we can not free the entries here.
328 if (!SLIST_EMPTY(&swhead)) {
329 if (noaction || verbose)
330 printf("Signal all daemon process(es)...\n");
331 SLIST_FOREACH(stmp, &swhead, sw_nextp)
334 printf("\tsleep 10\n");
337 printf("Pause 10 seconds to allow daemon(s)"
338 " to close log file(s)\n");
343 * Compress all files that we're expected to compress, now
344 * that all processes should have closed the files which
347 if (!SLIST_EMPTY(&zwhead)) {
348 if (noaction || verbose)
349 printf("Compress all rotated log file(s)...\n");
350 while (!SLIST_EMPTY(&zwhead)) {
351 ztmp = SLIST_FIRST(&zwhead);
353 SLIST_REMOVE_HEAD(&zwhead, zw_nextp);
357 /* Now free all the sigwork entries. */
358 while (!SLIST_EMPTY(&swhead)) {
359 stmp = SLIST_FIRST(&swhead);
360 SLIST_REMOVE_HEAD(&swhead, sw_nextp);
364 while (wait(NULL) > 0 || errno == EINTR)
369 static struct conf_entry *
370 init_entry(const char *fname, struct conf_entry *src_entry)
372 struct conf_entry *tempwork;
375 printf("\t--> [creating entry for %s]\n", fname);
377 tempwork = malloc(sizeof(struct conf_entry));
378 if (tempwork == NULL)
379 err(1, "malloc of conf_entry for %s", fname);
381 if (destdir == NULL || fname[0] != '/')
382 tempwork->log = strdup(fname);
384 asprintf(&tempwork->log, "%s%s", destdir, fname);
385 if (tempwork->log == NULL)
386 err(1, "strdup for %s", fname);
388 if (src_entry != NULL) {
389 tempwork->pid_cmd_file = NULL;
390 if (src_entry->pid_cmd_file)
391 tempwork->pid_cmd_file = strdup(src_entry->pid_cmd_file);
392 tempwork->r_reason = NULL;
393 tempwork->firstcreate = 0;
394 tempwork->rotate = 0;
395 tempwork->fsize = -1;
396 tempwork->uid = src_entry->uid;
397 tempwork->gid = src_entry->gid;
398 tempwork->numlogs = src_entry->numlogs;
399 tempwork->trsize = src_entry->trsize;
400 tempwork->hours = src_entry->hours;
401 tempwork->trim_at = NULL;
402 if (src_entry->trim_at != NULL)
403 tempwork->trim_at = ptime_init(src_entry->trim_at);
404 tempwork->permissions = src_entry->permissions;
405 tempwork->flags = src_entry->flags;
406 tempwork->compress = src_entry->compress;
407 tempwork->sig = src_entry->sig;
408 tempwork->def_cfg = src_entry->def_cfg;
410 /* Initialize as a "do-nothing" entry */
411 tempwork->pid_cmd_file = NULL;
412 tempwork->r_reason = NULL;
413 tempwork->firstcreate = 0;
414 tempwork->rotate = 0;
415 tempwork->fsize = -1;
416 tempwork->uid = (uid_t)-1;
417 tempwork->gid = (gid_t)-1;
418 tempwork->numlogs = 1;
419 tempwork->trsize = -1;
420 tempwork->hours = -1;
421 tempwork->trim_at = NULL;
422 tempwork->permissions = 0;
424 tempwork->compress = COMPRESS_NONE;
425 tempwork->sig = SIGHUP;
426 tempwork->def_cfg = 0;
433 free_entry(struct conf_entry *ent)
439 if (ent->log != NULL) {
441 printf("\t--> [freeing entry for %s]\n", ent->log);
446 if (ent->pid_cmd_file != NULL) {
447 free(ent->pid_cmd_file);
448 ent->pid_cmd_file = NULL;
451 if (ent->r_reason != NULL) {
453 ent->r_reason = NULL;
456 if (ent->trim_at != NULL) {
457 ptime_free(ent->trim_at);
465 free_clist(struct cflist *list)
467 struct conf_entry *ent;
469 while (!STAILQ_EMPTY(list)) {
470 ent = STAILQ_FIRST(list);
471 STAILQ_REMOVE_HEAD(list, cf_nextp);
480 do_entry(struct conf_entry * ent)
482 #define REASON_MAX 80
484 fk_entry free_or_keep;
486 char temp_reason[REASON_MAX];
488 free_or_keep = FREE_ENT;
490 printf("%s <%d%s>: ", ent->log, ent->numlogs,
491 compress_type[ent->compress].flag);
492 ent->fsize = sizefile(ent->log);
493 modtime = age_old_log(ent->log);
495 ent->firstcreate = 0;
496 if (ent->fsize < 0) {
498 * If either the C flag or the -C option was specified,
499 * and if we won't be creating the file, then have the
500 * verbose message include a hint as to why the file
501 * will not be created.
503 temp_reason[0] = '\0';
505 ent->firstcreate = 1;
506 else if ((ent->flags & CE_CREATE) && createlogs)
507 ent->firstcreate = 1;
508 else if (ent->flags & CE_CREATE)
509 strlcpy(temp_reason, " (no -C option)", REASON_MAX);
511 strlcpy(temp_reason, " (no C flag)", REASON_MAX);
513 if (ent->firstcreate) {
515 printf("does not exist -> will create.\n");
517 } else if (verbose) {
518 printf("does not exist, skipped%s.\n", temp_reason);
521 if (ent->flags & CE_TRIMAT && !force && !rotatereq) {
522 diffsecs = ptimeget_diff(timenow, ent->trim_at);
523 if (diffsecs < 0.0) {
524 /* trim_at is some time in the future. */
526 ptime_adjust4dst(ent->trim_at,
528 printf("--> will trim at %s",
529 ptimeget_ctime(ent->trim_at));
531 return (free_or_keep);
532 } else if (diffsecs >= 3600.0) {
534 * trim_at is more than an hour in the past,
535 * so find the next valid trim_at time, and
536 * tell the user what that will be.
538 if (verbose && dbg_at_times)
539 printf("\n\t--> prev trim at %s\t",
540 ptimeget_ctime(ent->trim_at));
542 ptimeset_nxtime(ent->trim_at);
543 printf("--> will trim at %s",
544 ptimeget_ctime(ent->trim_at));
546 return (free_or_keep);
547 } else if (verbose && noaction && dbg_at_times) {
549 * If we are just debugging at-times, then
550 * a detailed message is helpful. Also
551 * skip "doing" any commands, since they
552 * would all be turned off by no-action.
554 printf("\n\t--> timematch at %s",
555 ptimeget_ctime(ent->trim_at));
556 return (free_or_keep);
557 } else if (verbose && ent->hours <= 0) {
558 printf("--> time is up\n");
561 if (verbose && (ent->trsize > 0))
562 printf("size (Kb): %d [%d] ", ent->fsize, ent->trsize);
563 if (verbose && (ent->hours > 0))
564 printf(" age (hr): %d [%d] ", modtime, ent->hours);
567 * Figure out if this logfile needs to be rotated.
569 temp_reason[0] = '\0';
572 snprintf(temp_reason, REASON_MAX, " due to -R from %s",
576 snprintf(temp_reason, REASON_MAX, " due to -F request");
577 } else if ((ent->trsize > 0) && (ent->fsize >= ent->trsize)) {
579 snprintf(temp_reason, REASON_MAX, " due to size>%dK",
581 } else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) {
583 } else if ((ent->hours > 0) && ((modtime >= ent->hours) ||
589 * If the file needs to be rotated, then rotate it.
591 if (ent->rotate && !norotate) {
592 if (temp_reason[0] != '\0')
593 ent->r_reason = strdup(temp_reason);
595 printf("--> trimming log....\n");
596 if (noaction && !verbose)
597 printf("%s <%d%s>: trimming\n", ent->log,
599 compress_type[ent->compress].flag);
600 free_or_keep = do_rotate(ent);
603 printf("--> skipping\n");
606 return (free_or_keep);
611 parse_args(int argc, char **argv)
616 timenow = ptime_init(NULL);
617 ptimeset_time(timenow, time(NULL));
618 strlcpy(daytime, ptimeget_ctime(timenow) + 4, DAYTIME_LEN);
620 /* Let's get our hostname */
621 (void)gethostname(hostname, sizeof(hostname));
623 /* Truncate domain */
624 if ((p = strchr(hostname, '.')) != NULL)
627 /* Parse command line options. */
628 while ((ch = getopt(argc, argv, "a:d:f:nrst:vCD:FNPR:S:")) != -1)
632 archdirname = optarg;
650 if (optarg[0] == '\0' ||
651 strcmp(optarg, "DEFAULT") == 0)
652 timefnamefmt = strdup(DEFAULT_TIMEFNAME_FMT);
654 timefnamefmt = strdup(optarg);
660 /* Useful for things like rc.diskless... */
665 * Set some debugging option. The specific option
666 * depends on the value of optarg. These options
667 * may come and go without notice or documentation.
669 if (parse_doption(optarg))
684 requestor = strdup(optarg);
687 path_syslogpid = optarg;
689 case 'm': /* Used by OpenBSD for "monitor mode" */
695 if (force && norotate) {
696 warnx("Only one of -F and -N may be specified.");
702 if (optind == argc) {
703 warnx("At least one filename must be given when -R is specified.");
707 /* Make sure "requestor" value is safe for a syslog message. */
708 for (p = requestor; *p != '\0'; p++) {
709 if (!isprintch(*p) && (*p != '\t'))
716 * Note that the 'daytime' variable is not changed.
717 * That is only used in messages that track when a
718 * logfile is rotated, and if a file *is* rotated,
719 * then it will still rotated at the "real now" time.
722 timenow = dbg_timenow;
723 fprintf(stderr, "Debug: Running as if TimeNow is %s",
724 ptimeget_ctime(dbg_timenow));
730 * These debugging options are mainly meant for developer use, such
731 * as writing regression-tests. They would not be needed by users
732 * during normal operation of newsyslog...
735 parse_doption(const char *doption)
737 const char TN[] = "TN=";
740 if (strncmp(doption, TN, sizeof(TN) - 1) == 0) {
742 * The "TimeNow" debugging option. This might be off
743 * by an hour when crossing a timezone change.
745 dbg_timenow = ptime_init(NULL);
746 res = ptime_relparse(dbg_timenow, PTM_PARSE_ISO8601,
747 time(NULL), doption + sizeof(TN) - 1);
749 warnx("Non-existent time specified on -D %s", doption);
750 return (0); /* failure */
751 } else if (res < 0) {
752 warnx("Malformed time given on -D %s", doption);
753 return (0); /* failure */
755 return (1); /* successfully parsed */
759 if (strcmp(doption, "ats") == 0) {
761 return (1); /* successfully parsed */
764 /* XXX - This check could probably be dropped. */
765 if ((strcmp(doption, "neworder") == 0) || (strcmp(doption, "oldorder")
767 warnx("NOTE: newsyslog always uses 'neworder'.");
768 return (1); /* successfully parsed */
771 warnx("Unknown -D (debug) option: '%s'", doption);
772 return (0); /* failure */
780 "usage: newsyslog [-CFNPnrsv] [-a directory] [-d directory] [-f config_file]\n"
781 " [-S pidfile] [-t timefmt] [[-R tagname] file ...]\n");
786 * Parse a configuration file and return a linked list of all the logs
787 * which should be processed.
789 static struct cflist *
790 get_worklist(char **files)
794 struct cflist *cmdlist, *filelist, *globlist;
795 struct conf_entry *defconf, *dupent, *ent;
796 struct ilist inclist;
797 struct include_entry *inc;
801 STAILQ_INIT(&inclist);
803 filelist = malloc(sizeof(struct cflist));
804 if (filelist == NULL)
805 err(1, "malloc of filelist");
806 STAILQ_INIT(filelist);
807 globlist = malloc(sizeof(struct cflist));
808 if (globlist == NULL)
809 err(1, "malloc of globlist");
810 STAILQ_INIT(globlist);
812 inc = malloc(sizeof(struct include_entry));
814 err(1, "malloc of inc");
816 if (inc->file == NULL)
817 inc->file = _PATH_CONF;
818 STAILQ_INSERT_TAIL(&inclist, inc, inc_nextp);
820 STAILQ_FOREACH(inc, &inclist, inc_nextp) {
821 if (strcmp(inc->file, "-") != 0)
822 f = fopen(inc->file, "r");
825 inc->file = "<stdin>";
828 err(1, "%s", inc->file);
831 printf("Processing %s\n", inc->file);
832 parse_file(f, filelist, globlist, defconf, &inclist);
837 * All config-file information has been read in and turned into
838 * a filelist and a globlist. If there were no specific files
839 * given on the run command, then the only thing left to do is to
840 * call a routine which finds all files matched by the globlist
841 * and adds them to the filelist. Then return the worklist.
843 if (*files == NULL) {
844 expand_globs(filelist, globlist);
845 free_clist(globlist);
853 * If newsyslog was given a specific list of files to process,
854 * it may be that some of those files were not listed in any
855 * config file. Those unlisted files should get the default
856 * rotation action. First, create the default-rotation action
857 * if none was found in a system config file.
859 if (defconf == NULL) {
860 defconf = init_entry(DEFAULT_MARKER, NULL);
861 defconf->numlogs = 3;
862 defconf->trsize = 50;
863 defconf->permissions = S_IRUSR|S_IWUSR;
867 * If newsyslog was run with a list of specific filenames,
868 * then create a new worklist which has only those files in
869 * it, picking up the rotation-rules for those files from
870 * the original filelist.
872 * XXX - Note that this will copy multiple rules for a single
873 * logfile, if multiple entries are an exact match for
874 * that file. That matches the historic behavior, but do
875 * we want to continue to allow it? If so, it should
876 * probably be handled more intelligently.
878 cmdlist = malloc(sizeof(struct cflist));
880 err(1, "malloc of cmdlist");
881 STAILQ_INIT(cmdlist);
883 for (given = files; *given; ++given) {
885 * First try to find exact-matches for this given file.
888 STAILQ_FOREACH(ent, filelist, cf_nextp) {
889 if (strcmp(ent->log, *given) == 0) {
891 dupent = init_entry(*given, ent);
892 STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp);
897 printf("\t+ Matched entry %s\n", *given);
902 * There was no exact-match for this given file, so look
903 * for a "glob" entry which does match.
906 if (verbose > 2 && globlist != NULL)
907 printf("\t+ Checking globs for %s\n", *given);
908 STAILQ_FOREACH(ent, globlist, cf_nextp) {
909 fnres = fnmatch(ent->log, *given, FNM_PATHNAME);
911 printf("\t+ = %d for pattern %s\n", fnres,
915 dupent = init_entry(*given, ent);
916 /* This new entry is not a glob! */
917 dupent->flags &= ~CE_GLOB;
918 STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp);
919 /* Only allow a match to one glob-entry */
925 printf("\t+ Matched %s via %s\n", *given,
931 * This given file was not found in any config file, so
932 * add a worklist item based on the default entry.
935 printf("\t+ No entry matched %s (will use %s)\n",
936 *given, DEFAULT_MARKER);
937 dupent = init_entry(*given, defconf);
938 /* Mark that it was *not* found in a config file */
940 STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp);
944 * Free all the entries in the original work list, the list of
945 * glob entries, and the default entry.
947 free_clist(filelist);
948 free_clist(globlist);
951 /* And finally, return a worklist which matches the given files. */
956 * Expand the list of entries with filename patterns, and add all files
957 * which match those glob-entries onto the worklist.
960 expand_globs(struct cflist *work_p, struct cflist *glob_p)
965 struct conf_entry *dupent, *ent, *globent;
970 * The worklist contains all fully-specified (non-GLOB) names.
972 * Now expand the list of filename-pattern (GLOB) entries into
973 * a second list, which (by definition) will only match files
974 * that already exist. Do not add a glob-related entry for any
975 * file which already exists in the fully-specified list.
977 STAILQ_FOREACH(globent, glob_p, cf_nextp) {
978 gres = glob(globent->log, GLOB_NOCHECK, NULL, &pglob);
980 warn("cannot expand pattern (%d): %s", gres,
986 printf("\t+ Expanding pattern %s\n", globent->log);
987 for (i = 0; i < pglob.gl_matchc; i++) {
988 mfname = pglob.gl_pathv[i];
990 /* See if this file already has a specific entry. */
992 STAILQ_FOREACH(ent, work_p, cf_nextp) {
993 if (strcmp(mfname, ent->log) == 0) {
1001 /* Make sure the named matched is a file. */
1002 gres = lstat(mfname, &st_fm);
1004 /* Error on a file that glob() matched?!? */
1005 warn("Skipping %s - lstat() error", mfname);
1008 if (!S_ISREG(st_fm.st_mode)) {
1009 /* We only rotate files! */
1011 printf("\t+ . skipping %s (!file)\n",
1017 printf("\t+ . add file %s\n", mfname);
1018 dupent = init_entry(mfname, globent);
1019 /* This new entry is not a glob! */
1020 dupent->flags &= ~CE_GLOB;
1022 /* Add to the worklist. */
1023 STAILQ_INSERT_TAIL(work_p, dupent, cf_nextp);
1027 printf("\t+ Done with pattern %s\n", globent->log);
1032 * Parse a configuration file and update a linked list of all the logs to
1036 parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p,
1037 struct conf_entry *defconf_p, struct ilist *inclist)
1039 char line[BUFSIZ], *parse, *q;
1040 char *cp, *errline, *group;
1041 struct conf_entry *working;
1045 int eol, ptm_opts, res, special;
1049 while (fgets(line, BUFSIZ, cf)) {
1050 if ((line[0] == '\n') || (line[0] == '#') ||
1051 (strlen(line) == 0))
1053 if (errline != NULL)
1055 errline = strdup(line);
1056 for (cp = line + 1; *cp != '\0'; cp++) {
1059 if (*(cp - 1) == '\\') {
1068 q = parse = missing_field(sob(line), errline);
1071 errx(1, "malformed line (missing fields):\n%s",
1076 * Allow people to set debug options via the config file.
1077 * (NOTE: debug options are undocumented, and may disappear
1078 * at any time, etc).
1080 if (strcasecmp(DEBUG_MARKER, q) == 0) {
1081 q = parse = missing_field(sob(++parse), errline);
1084 warnx("debug line specifies no option:\n%s",
1091 } else if (strcasecmp(INCLUDE_MARKER, q) == 0) {
1093 printf("Found: %s", errline);
1094 q = parse = missing_field(sob(++parse), errline);
1097 warnx("include line missing argument:\n%s",
1105 res = glob(q, GLOB_NOCHECK, NULL, &pglob);
1107 warn("cannot expand pattern (%d): %s",
1113 printf("\t+ Expanding pattern %s\n", q);
1115 for (i = 0; i < pglob.gl_matchc; i++)
1116 add_to_queue(pglob.gl_pathv[i],
1120 add_to_queue(q, inclist);
1125 working = init_entry(q, NULL);
1126 if (strcasecmp(DEFAULT_MARKER, q) == 0) {
1128 if (defconf_p != NULL) {
1129 warnx("Ignoring duplicate entry for %s!", q);
1130 free_entry(working);
1133 defconf_p = working;
1136 q = parse = missing_field(sob(++parse), errline);
1139 errx(1, "malformed line (missing fields):\n%s",
1142 if ((group = strchr(q, ':')) != NULL ||
1143 (group = strrchr(q, '.')) != NULL) {
1146 if (!(isnumberstr(q))) {
1147 if ((pwd = getpwnam(q)) == NULL)
1149 "error in config file; unknown user:\n%s",
1151 working->uid = pwd->pw_uid;
1153 working->uid = atoi(q);
1155 working->uid = (uid_t)-1;
1159 if (!(isnumberstr(q))) {
1160 if ((grp = getgrnam(q)) == NULL)
1162 "error in config file; unknown group:\n%s",
1164 working->gid = grp->gr_gid;
1166 working->gid = atoi(q);
1168 working->gid = (gid_t)-1;
1170 q = parse = missing_field(sob(++parse), errline);
1173 errx(1, "malformed line (missing fields):\n%s",
1177 working->uid = (uid_t)-1;
1178 working->gid = (gid_t)-1;
1181 if (!sscanf(q, "%o", &working->permissions))
1182 errx(1, "error in config file; bad permissions:\n%s",
1185 q = parse = missing_field(sob(++parse), errline);
1188 errx(1, "malformed line (missing fields):\n%s",
1191 if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0)
1192 errx(1, "error in config file; bad value for count of logs to save:\n%s",
1195 q = parse = missing_field(sob(++parse), errline);
1198 errx(1, "malformed line (missing fields):\n%s",
1202 working->trsize = atoi(q);
1203 else if (strcmp(q, "*") == 0)
1204 working->trsize = -1;
1206 warnx("Invalid value of '%s' for 'size' in line:\n%s",
1208 working->trsize = -1;
1212 working->compress = COMPRESS_NONE;
1213 q = parse = missing_field(sob(++parse), errline);
1221 ul = strtoul(q, &ep, 10);
1224 else if (*ep == '*')
1225 working->hours = -1;
1226 else if (ul > INT_MAX)
1227 errx(1, "interval is too large:\n%s", errline);
1229 working->hours = ul;
1231 if (*ep == '\0' || strcmp(ep, "*") == 0)
1233 if (*ep != '@' && *ep != '$')
1234 errx(1, "malformed interval/at:\n%s", errline);
1236 working->flags |= CE_TRIMAT;
1237 working->trim_at = ptime_init(NULL);
1238 ptm_opts = PTM_PARSE_ISO8601;
1240 ptm_opts = PTM_PARSE_DWM;
1241 ptm_opts |= PTM_PARSE_MATCHDOM;
1242 res = ptime_relparse(working->trim_at, ptm_opts,
1243 ptimeget_secs(timenow), ep + 1);
1245 errx(1, "nonexistent time for 'at' value:\n%s",
1248 errx(1, "malformed 'at' value:\n%s", errline);
1255 q = parse = sob(++parse); /* Optional field */
1262 for (; q && *q && !isspacech(*q); q++) {
1263 switch (tolowerch(*q)) {
1265 working->flags |= CE_BINARY;
1269 * XXX - Ick! Ugly! Remove ASAP!
1270 * We want `c' and `C' for "create". But we
1271 * will temporarily treat `c' as `g', because
1272 * FreeBSD releases <= 4.8 have a typo of
1273 * checking ('G' || 'c') for CE_GLOB.
1276 warnx("Assuming 'g' for 'c' in flags for line:\n%s",
1278 warnx("The 'c' flag will eventually mean 'CREATE'");
1279 working->flags |= CE_GLOB;
1282 working->flags |= CE_CREATE;
1285 working->flags |= CE_NODUMP;
1288 working->flags |= CE_GLOB;
1291 working->compress = COMPRESS_BZIP2;
1294 working->flags |= CE_NOSIGNAL;
1297 working->flags |= CE_PID2CMD;
1300 working->flags |= CE_SIGNALGROUP;
1303 /* Depreciated flag - keep for compatibility purposes */
1306 working->compress = COMPRESS_XZ;
1309 working->compress = COMPRESS_GZIP;
1313 case 'f': /* Used by OpenBSD for "CE_FOLLOW" */
1314 case 'm': /* Used by OpenBSD for "CE_MONITOR" */
1315 case 'p': /* Used by NetBSD for "CE_PLAIN0" */
1317 errx(1, "illegal flag in config file -- %c",
1325 q = parse = sob(++parse); /* Optional field */
1332 working->pid_cmd_file = NULL;
1335 working->pid_cmd_file = strdup(q);
1336 else if (isdigit(*q))
1340 "illegal pid file or signal number in config file:\n%s",
1346 q = parse = sob(++parse); /* Optional field */
1347 *(parse = son(parse)) = '\0';
1350 working->sig = SIGHUP;
1354 working->sig = atoi(q);
1358 "illegal signal number in config file:\n%s",
1361 if (working->sig < 1 || working->sig >= NSIG)
1366 * Finish figuring out what pid-file to use (if any) in
1367 * later processing if this logfile needs to be rotated.
1369 if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) {
1371 * This config-entry specified 'n' for nosignal,
1372 * see if it also specified an explicit pid_cmd_file.
1373 * This would be a pretty pointless combination.
1375 if (working->pid_cmd_file != NULL) {
1376 warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s",
1377 working->pid_cmd_file, errline);
1378 free(working->pid_cmd_file);
1379 working->pid_cmd_file = NULL;
1381 } else if (working->pid_cmd_file == NULL) {
1383 * This entry did not specify the 'n' flag, which
1384 * means it should signal syslogd unless it had
1385 * specified some other pid-file (and obviously the
1386 * syslog pid-file will not be for a process-group).
1387 * Also, we should only try to notify syslog if we
1390 if (working->flags & CE_SIGNALGROUP) {
1391 warnx("Ignoring flag 'U' in line:\n%s",
1393 working->flags &= ~CE_SIGNALGROUP;
1396 working->pid_cmd_file = strdup(path_syslogpid);
1400 * Add this entry to the appropriate list of entries, unless
1401 * it was some kind of special entry (eg: <default>).
1404 ; /* Do not add to any list */
1405 } else if (working->flags & CE_GLOB) {
1406 STAILQ_INSERT_TAIL(glob_p, working, cf_nextp);
1408 STAILQ_INSERT_TAIL(work_p, working, cf_nextp);
1411 if (errline != NULL)
1416 missing_field(char *p, char *errline)
1420 errx(1, "missing field in config file:\n%s", errline);
1425 * In our sort we return it in the reverse of what qsort normally
1426 * would do, as we want the newest files first. If we have two
1427 * entries with the same time we don't really care about order.
1429 * Support function for qsort() in delete_oldest_timelog().
1432 oldlog_entry_compare(const void *a, const void *b)
1434 const struct oldlog_entry *ola = a, *olb = b;
1436 if (ola->t > olb->t)
1438 else if (ola->t < olb->t)
1445 * Delete the oldest logfiles, when using time based filenames.
1448 delete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir)
1450 char *logfname, *s, *dir, errbuf[80];
1451 int dirfd, i, logcnt, max_logcnt, valid;
1452 struct oldlog_entry *oldlogs;
1453 size_t logfname_len;
1460 oldlogs = malloc(MAX_OLDLOGS * sizeof(struct oldlog_entry));
1461 max_logcnt = MAX_OLDLOGS;
1464 if (archive_dir != NULL && archive_dir[0] != '\0')
1467 if ((cdir = dirname(ent->log)) == NULL)
1468 err(1, "dirname()");
1469 if ((dir = strdup(cdir)) == NULL)
1472 if ((s = basename(ent->log)) == NULL)
1473 err(1, "basename()");
1474 if ((logfname = strdup(s)) == NULL)
1476 logfname_len = strlen(logfname);
1477 if (strcmp(logfname, "/") == 0)
1478 errx(1, "Invalid log filename - became '/'");
1481 printf("Searching for old logs in %s\n", dir);
1483 /* First we create a 'list' of all archived logfiles */
1484 if ((dirp = opendir(dir)) == NULL)
1485 err(1, "Cannot open log directory '%s'", dir);
1486 dirfd = dirfd(dirp);
1487 while ((dp = readdir(dirp)) != NULL) {
1488 if (dp->d_type != DT_REG)
1491 /* Ignore everything but files with our logfile prefix */
1492 if (strncmp(dp->d_name, logfname, logfname_len) != 0)
1494 /* Ignore the actual non-rotated logfile */
1495 if (dp->d_namlen == logfname_len)
1498 * Make sure we created have found a logfile, so the
1499 * postfix is valid, IE format is: '.<time>(.[bg]z)?'.
1501 if (dp->d_name[logfname_len] != '.') {
1503 printf("Ignoring %s which has unexpected "
1504 "extension '%s'\n", dp->d_name,
1505 &dp->d_name[logfname_len]);
1508 if ((s = strptime(&dp->d_name[logfname_len + 1],
1509 timefnamefmt, &tm)) == NULL) {
1511 * We could special case "old" sequentially
1512 * named logfiles here, but we do not as that
1513 * would require special handling to decide
1514 * which one was the oldest compared to "new"
1515 * time based logfiles.
1518 printf("Ignoring %s which does not "
1519 "match time format\n", dp->d_name);
1523 for (c = 0; c < COMPRESS_TYPES; c++)
1524 if (strcmp(s, compress_type[c].suffix) == 0)
1528 printf("Ignoring %s which has unexpected "
1529 "extension '%s'\n", dp->d_name, s);
1534 * We should now have old an old rotated logfile, so
1535 * add it to the 'list'.
1537 if ((oldlogs[logcnt].t = timegm(&tm)) == -1)
1538 err(1, "Could not convert time string to time value");
1539 if ((oldlogs[logcnt].fname = strdup(dp->d_name)) == NULL)
1544 * It is very unlikely we ever run out of space in the
1545 * logfile array from the default size, but lets
1546 * handle it anyway...
1548 if (logcnt >= max_logcnt) {
1550 /* Detect integer overflow */
1551 if (max_logcnt < logcnt)
1552 errx(1, "Too many old logfiles found");
1553 oldlogs = realloc(oldlogs,
1554 max_logcnt * sizeof(struct oldlog_entry));
1555 if (oldlogs == NULL)
1556 err(1, "realloc()");
1560 /* Second, if needed we delete oldest archived logfiles */
1561 if (logcnt > 0 && logcnt >= ent->numlogs && ent->numlogs > 1) {
1562 oldlogs = realloc(oldlogs, logcnt *
1563 sizeof(struct oldlog_entry));
1564 if (oldlogs == NULL)
1565 err(1, "realloc()");
1568 * We now sort the logs in the order of newest to
1569 * oldest. That way we can simply skip over the
1570 * number of records we want to keep.
1572 qsort(oldlogs, logcnt, sizeof(struct oldlog_entry),
1573 oldlog_entry_compare);
1574 for (i = ent->numlogs - 1; i < logcnt; i++) {
1576 printf("\trm -f %s/%s\n", dir,
1578 else if (unlinkat(dirfd, oldlogs[i].fname, 0) != 0) {
1579 snprintf(errbuf, sizeof(errbuf),
1580 "Could not delet old logfile '%s'",
1585 } else if (verbose > 1)
1586 printf("No old logs to delete for logfile %s\n", ent->log);
1588 /* Third, cleanup */
1590 for (i = 0; i < logcnt; i++) {
1591 assert(oldlogs[i].fname != NULL);
1592 free(oldlogs[i].fname);
1600 * Generate a log filename, when using clasic filenames.
1603 gen_clasiclog_fname(char *fname, size_t fname_sz, const char *archive_dir,
1604 const char *namepart, int numlogs_c)
1607 if (archive_dir[0] != '\0')
1608 (void) snprintf(fname, fname_sz, "%s/%s.%d", archive_dir,
1609 namepart, numlogs_c);
1611 (void) snprintf(fname, fname_sz, "%s.%d", namepart, numlogs_c);
1615 * Delete a rotated logfiles, when using clasic filenames.
1618 delete_clasiclog(const char *archive_dir, const char *namepart, int numlog_c)
1620 char file1[MAXPATHLEN], zfile1[MAXPATHLEN];
1623 gen_clasiclog_fname(file1, sizeof(file1), archive_dir, namepart,
1626 for (c = 0; c < COMPRESS_TYPES; c++) {
1627 (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
1628 compress_type[c].suffix);
1630 printf("\trm -f %s\n", zfile1);
1632 (void) unlink(zfile1);
1637 * Only add to the queue if the file hasn't already been added. This is
1638 * done to prevent circular include loops.
1641 add_to_queue(const char *fname, struct ilist *inclist)
1643 struct include_entry *inc;
1645 STAILQ_FOREACH(inc, inclist, inc_nextp) {
1646 if (strcmp(fname, inc->file) == 0) {
1647 warnx("duplicate include detected: %s", fname);
1652 inc = malloc(sizeof(struct include_entry));
1654 err(1, "malloc of inc");
1655 inc->file = strdup(fname);
1658 printf("\t+ Adding %s to the processing queue.\n", fname);
1660 STAILQ_INSERT_TAIL(inclist, inc, inc_nextp);
1664 * Search for logfile and return its compression suffix (if supported)
1665 * The suffix detection is first-match in the order of compress_types
1667 * Note: if logfile without suffix exists (uncompressed, COMPRESS_NONE)
1668 * a zero-length string is returned
1671 get_logfile_suffix(const char *logfile)
1674 char zfile[MAXPATHLEN];
1677 for (c = 0; c < COMPRESS_TYPES; c++) {
1678 (void) strlcpy(zfile, logfile, MAXPATHLEN);
1679 (void) strlcat(zfile, compress_type[c].suffix, MAXPATHLEN);
1680 if (lstat(zfile, &st) == 0)
1681 return (compress_type[c].suffix);
1687 do_rotate(const struct conf_entry *ent)
1689 char dirpart[MAXPATHLEN], namepart[MAXPATHLEN];
1690 char file1[MAXPATHLEN], file2[MAXPATHLEN];
1691 char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
1692 const char *logfile_suffix;
1693 char datetimestr[30];
1694 int flags, numlogs_c;
1695 fk_entry free_or_keep;
1696 struct sigwork_entry *swork;
1702 free_or_keep = FREE_ENT;
1707 /* build complete name of archive directory into dirpart */
1708 if (*archdirname == '/') { /* absolute */
1709 strlcpy(dirpart, archdirname, sizeof(dirpart));
1710 } else { /* relative */
1711 /* get directory part of logfile */
1712 strlcpy(dirpart, ent->log, sizeof(dirpart));
1713 if ((p = rindex(dirpart, '/')) == NULL)
1717 strlcat(dirpart, archdirname, sizeof(dirpart));
1720 /* check if archive directory exists, if not, create it */
1721 if (lstat(dirpart, &st))
1722 createdir(ent, dirpart);
1724 /* get filename part of logfile */
1725 if ((p = rindex(ent->log, '/')) == NULL)
1726 strlcpy(namepart, ent->log, sizeof(namepart));
1728 strlcpy(namepart, p + 1, sizeof(namepart));
1731 * Tell utility functions we are not using an archive
1735 strlcpy(namepart, ent->log, sizeof(namepart));
1738 /* Delete old logs */
1739 if (timefnamefmt != NULL)
1740 delete_oldest_timelog(ent, dirpart);
1743 * Handle cleaning up after legacy newsyslog where we
1744 * kept ent->numlogs + 1 files. This code can go away
1745 * at some point in the future.
1747 delete_clasiclog(dirpart, namepart, ent->numlogs);
1749 if (ent->numlogs > 0)
1750 delete_clasiclog(dirpart, namepart, ent->numlogs - 1);
1754 if (timefnamefmt != NULL) {
1755 /* If time functions fails we can't really do any sensible */
1756 if (time(&now) == (time_t)-1 ||
1757 localtime_r(&now, &tm) == NULL)
1758 bzero(&tm, sizeof(tm));
1760 strftime(datetimestr, sizeof(datetimestr), timefnamefmt, &tm);
1762 (void) snprintf(file1, sizeof(file1), "%s/%s.%s",
1763 dirpart, namepart, datetimestr);
1765 (void) snprintf(file1, sizeof(file1), "%s.%s",
1766 ent->log, datetimestr);
1768 /* Don't run the code to move down logs */
1771 gen_clasiclog_fname(file1, sizeof(file1), dirpart, namepart,
1773 numlogs_c = ent->numlogs - 2; /* copy for countdown */
1776 /* Move down log files */
1777 for (; numlogs_c >= 0; numlogs_c--) {
1778 (void) strlcpy(file2, file1, sizeof(file2));
1780 gen_clasiclog_fname(file1, sizeof(file1), dirpart, namepart,
1783 logfile_suffix = get_logfile_suffix(file1);
1784 if (logfile_suffix == NULL)
1786 (void) strlcpy(zfile1, file1, MAXPATHLEN);
1787 (void) strlcpy(zfile2, file2, MAXPATHLEN);
1788 (void) strlcat(zfile1, logfile_suffix, MAXPATHLEN);
1789 (void) strlcat(zfile2, logfile_suffix, MAXPATHLEN);
1792 printf("\tmv %s %s\n", zfile1, zfile2);
1794 /* XXX - Ought to be checking for failure! */
1795 (void)rename(zfile1, zfile2);
1797 change_attrs(zfile2, ent);
1800 if (ent->numlogs > 0) {
1803 * Note that savelog() may succeed with using link()
1804 * for the archtodir case, but there is no good way
1805 * of knowing if it will when doing "noaction", so
1806 * here we claim that it will have to do a copy...
1809 printf("\tcp %s %s\n", ent->log, file1);
1811 printf("\tln %s %s\n", ent->log, file1);
1813 if (!(flags & CE_BINARY)) {
1814 /* Report the trimming to the old log */
1815 log_trim(ent->log, ent);
1817 savelog(ent->log, file1);
1819 change_attrs(file1, ent);
1822 /* Create the new log file and move it into place */
1824 printf("Start new log...\n");
1828 * Save all signalling and file-compression to be done after log
1829 * files from all entries have been rotated. This way any one
1830 * process will not be sent the same signal multiple times when
1831 * multiple log files had to be rotated.
1834 if (ent->pid_cmd_file != NULL)
1835 swork = save_sigwork(ent);
1836 if (ent->numlogs > 0 && ent->compress > COMPRESS_NONE) {
1838 * The zipwork_entry will include a pointer to this
1839 * conf_entry, so the conf_entry should not be freed.
1841 free_or_keep = KEEP_ENT;
1842 save_zipwork(ent, swork, ent->fsize, file1);
1845 return (free_or_keep);
1849 do_sigwork(struct sigwork_entry *swork)
1851 struct sigwork_entry *nextsig;
1855 if (!(swork->sw_pidok) || swork->sw_pid == 0)
1856 return; /* no work to do... */
1859 * If nosignal (-s) was specified, then do not signal any process.
1860 * Note that a nosignal request triggers a warning message if the
1861 * rotated logfile needs to be compressed, *unless* -R was also
1862 * specified. We assume that an `-sR' request came from a process
1863 * which writes to the logfile, and as such, we assume that process
1864 * has already made sure the logfile is not presently in use. This
1865 * just sets swork->sw_pidok to a special value, and do_zipwork
1866 * will print any necessary warning(s).
1870 swork->sw_pidok = -1;
1875 * Compute the pause between consecutive signals. Use a longer
1876 * sleep time if we will be sending two signals to the same
1877 * deamon or process-group.
1880 nextsig = SLIST_NEXT(swork, sw_nextp);
1881 if (nextsig != NULL) {
1882 if (swork->sw_pid == nextsig->sw_pid)
1889 printf("\tkill -%d %d \t\t# %s\n", swork->sw_signum,
1890 (int)swork->sw_pid, swork->sw_fname);
1892 printf("\tsleep %d\n", secs);
1896 if (swork->run_cmd) {
1897 asprintf(&tmp, "%s %d", swork->sw_fname, swork->sw_signum);
1899 warn("can't allocate memory to run %s",
1904 printf("Run command: %s\n", tmp);
1907 warnx("%s: returned non-zero exit code: %d",
1914 kres = kill(swork->sw_pid, swork->sw_signum);
1917 * Assume that "no such process" (ESRCH) is something
1918 * to warn about, but is not an error. Presumably the
1919 * process which writes to the rotated log file(s) is
1920 * gone, in which case we should have no problem with
1921 * compressing the rotated log file(s).
1924 swork->sw_pidok = 0;
1925 warn("can't notify %s, pid %d", swork->sw_pidtype,
1926 (int)swork->sw_pid);
1929 printf("Notified %s pid %d = %s\n", swork->sw_pidtype,
1930 (int)swork->sw_pid, swork->sw_fname);
1933 printf("Pause %d second(s) between signals\n",
1941 do_zipwork(struct zipwork_entry *zwork)
1943 const char *pgm_name, *pgm_path;
1944 int errsav, fcount, zstatus;
1946 char zresult[MAXPATHLEN];
1950 strlcpy(zresult, zwork->zw_fname, sizeof(zresult));
1951 if (zwork != NULL && zwork->zw_conf != NULL &&
1952 zwork->zw_conf->compress > COMPRESS_NONE)
1953 for (c = 1; c < COMPRESS_TYPES; c++) {
1954 if (zwork->zw_conf->compress == c) {
1955 pgm_path = compress_type[c].path;
1956 (void) strlcat(zresult,
1957 compress_type[c].suffix, sizeof(zresult));
1961 if (pgm_path == NULL) {
1962 warnx("invalid entry for %s in do_zipwork", zwork->zw_fname);
1965 pgm_name = strrchr(pgm_path, '/');
1966 if (pgm_name == NULL)
1967 pgm_name = pgm_path;
1971 if (zwork->zw_swork != NULL && zwork->zw_swork->sw_pidok <= 0) {
1973 "log %s not compressed because daemon(s) not notified",
1975 change_attrs(zwork->zw_fname, zwork->zw_conf);
1980 printf("\t%s %s\n", pgm_name, zwork->zw_fname);
1981 change_attrs(zresult, zwork->zw_conf);
1987 while (pidzip < 0) {
1989 * The fork failed. If the failure was due to a temporary
1990 * problem, then wait a short time and try it again.
1993 warn("fork() for `%s %s'", pgm_name, zwork->zw_fname);
1994 if (errsav != EAGAIN || fcount > 5)
1995 errx(1, "Exiting...");
2001 /* The child process executes the compression command */
2002 execl(pgm_path, pgm_path, "-f", zwork->zw_fname, (char *)0);
2003 err(1, "execl(`%s -f %s')", pgm_path, zwork->zw_fname);
2006 wpid = waitpid(pidzip, &zstatus, 0);
2008 /* XXX - should this be a fatal error? */
2009 warn("%s: waitpid(%d)", pgm_path, pidzip);
2012 if (!WIFEXITED(zstatus)) {
2013 warnx("`%s -f %s' did not terminate normally", pgm_name,
2017 if (WEXITSTATUS(zstatus)) {
2018 warnx("`%s -f %s' terminated with a non-zero status (%d)",
2019 pgm_name, zwork->zw_fname, WEXITSTATUS(zstatus));
2023 /* Compression was successful, set file attributes on the result. */
2024 change_attrs(zresult, zwork->zw_conf);
2028 * Save information on any process we need to signal. Any single
2029 * process may need to be sent different signal-values for different
2030 * log files, but usually a single signal-value will cause the process
2031 * to close and re-open all of it's log files.
2033 static struct sigwork_entry *
2034 save_sigwork(const struct conf_entry *ent)
2036 struct sigwork_entry *sprev, *stmp;
2042 SLIST_FOREACH(stmp, &swhead, sw_nextp) {
2043 ndiff = strcmp(ent->pid_cmd_file, stmp->sw_fname);
2047 if (ent->sig == stmp->sw_signum)
2049 if (ent->sig > stmp->sw_signum) {
2056 if (stmp != NULL && ndiff == 0)
2059 tmpsiz = sizeof(struct sigwork_entry) + strlen(ent->pid_cmd_file) + 1;
2060 stmp = malloc(tmpsiz);
2063 /* If this is a command to run we just set the flag and run command */
2064 if (ent->flags & CE_PID2CMD) {
2067 set_swpid(stmp, ent);
2069 stmp->sw_signum = ent->sig;
2070 strcpy(stmp->sw_fname, ent->pid_cmd_file);
2072 SLIST_INSERT_HEAD(&swhead, stmp, sw_nextp);
2074 SLIST_INSERT_AFTER(sprev, stmp, sw_nextp);
2079 * Save information on any file we need to compress. We may see the same
2080 * file multiple times, so check the full list to avoid duplicates. The
2081 * list itself is sorted smallest-to-largest, because that's the order we
2082 * want to compress the files. If the partition is very low on disk space,
2083 * then the smallest files are the most likely to compress, and compressing
2084 * them first will free up more space for the larger files.
2086 static struct zipwork_entry *
2087 save_zipwork(const struct conf_entry *ent, const struct sigwork_entry *swork,
2088 int zsize, const char *zipfname)
2090 struct zipwork_entry *zprev, *ztmp;
2094 /* Compute the size if the caller did not know it. */
2096 zsize = sizefile(zipfname);
2100 SLIST_FOREACH(ztmp, &zwhead, zw_nextp) {
2101 ndiff = strcmp(zipfname, ztmp->zw_fname);
2104 if (zsize > ztmp->zw_fsize)
2107 if (ztmp != NULL && ndiff == 0)
2110 tmpsiz = sizeof(struct zipwork_entry) + strlen(zipfname) + 1;
2111 ztmp = malloc(tmpsiz);
2112 ztmp->zw_conf = ent;
2113 ztmp->zw_swork = swork;
2114 ztmp->zw_fsize = zsize;
2115 strcpy(ztmp->zw_fname, zipfname);
2117 SLIST_INSERT_HEAD(&zwhead, ztmp, zw_nextp);
2119 SLIST_INSERT_AFTER(zprev, ztmp, zw_nextp);
2123 /* Send a signal to the pid specified by pidfile */
2125 set_swpid(struct sigwork_entry *swork, const struct conf_entry *ent)
2128 long minok, maxok, rval;
2129 char *endp, *linep, line[BUFSIZ];
2133 swork->sw_pidok = 0;
2135 swork->sw_pidtype = "daemon";
2136 if (ent->flags & CE_SIGNALGROUP) {
2138 * If we are expected to signal a process-group when
2139 * rotating this logfile, then the value read in should
2140 * be the negative of a valid process ID.
2144 swork->sw_pidtype = "process-group";
2147 f = fopen(ent->pid_cmd_file, "r");
2149 if (errno == ENOENT && enforcepid == 0) {
2151 * Warn if the PID file doesn't exist, but do
2152 * not consider it an error. Most likely it
2153 * means the process has been terminated,
2154 * so it should be safe to rotate any log
2155 * files that the process would have been using.
2157 swork->sw_pidok = 1;
2158 warnx("pid file doesn't exist: %s", ent->pid_cmd_file);
2160 warn("can't open pid file: %s", ent->pid_cmd_file);
2164 if (fgets(line, BUFSIZ, f) == NULL) {
2166 * Warn if the PID file is empty, but do not consider
2167 * it an error. Most likely it means the process has
2168 * has terminated, so it should be safe to rotate any
2169 * log files that the process would have been using.
2171 if (feof(f) && enforcepid == 0) {
2172 swork->sw_pidok = 1;
2173 warnx("pid/cmd file is empty: %s", ent->pid_cmd_file);
2175 warn("can't read from pid file: %s", ent->pid_cmd_file);
2183 while (*linep == ' ')
2185 rval = strtol(linep, &endp, 10);
2186 if (*endp != '\0' && !isspacech(*endp)) {
2187 warnx("pid file does not start with a valid number: %s",
2189 } else if (rval < minok || rval > maxok) {
2190 warnx("bad value '%ld' for process number in %s",
2191 rval, ent->pid_cmd_file);
2193 warnx("\t(expecting value between %ld and %ld)",
2196 swork->sw_pidok = 1;
2197 swork->sw_pid = rval;
2203 /* Log the fact that the logs were turned over */
2205 log_trim(const char *logname, const struct conf_entry *log_ent)
2210 if ((f = fopen(logname, "a")) == NULL)
2213 if (log_ent->def_cfg)
2214 xtra = " using <default> rule";
2215 if (log_ent->firstcreate)
2216 fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n",
2217 daytime, hostname, (int) getpid(), xtra);
2218 else if (log_ent->r_reason != NULL)
2219 fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n",
2220 daytime, hostname, (int) getpid(), log_ent->r_reason, xtra);
2222 fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n",
2223 daytime, hostname, (int) getpid(), xtra);
2224 if (fclose(f) == EOF)
2225 err(1, "log_trim: fclose");
2229 /* Return size in kilobytes of a file */
2231 sizefile(const char *file)
2235 if (stat(file, &sb) < 0)
2237 return (kbytes(dbtob(sb.st_blocks)));
2240 /* Return the age of old log file (file.0) */
2242 age_old_log(char *file)
2245 const char *logfile_suffix;
2246 char tmp[MAXPATHLEN + sizeof(".0") + COMPRESS_SUFFIX_MAXLEN + 1];
2251 /* build name of archive directory into tmp */
2252 if (*archdirname == '/') { /* absolute */
2253 strlcpy(tmp, archdirname, sizeof(tmp));
2254 } else { /* relative */
2255 /* get directory part of logfile */
2256 strlcpy(tmp, file, sizeof(tmp));
2257 if ((p = rindex(tmp, '/')) == NULL)
2261 strlcat(tmp, archdirname, sizeof(tmp));
2264 strlcat(tmp, "/", sizeof(tmp));
2266 /* get filename part of logfile */
2267 if ((p = rindex(file, '/')) == NULL)
2268 strlcat(tmp, file, sizeof(tmp));
2270 strlcat(tmp, p + 1, sizeof(tmp));
2272 (void) strlcpy(tmp, file, sizeof(tmp));
2275 strlcat(tmp, ".0", sizeof(tmp));
2276 logfile_suffix = get_logfile_suffix(tmp);
2277 if (logfile_suffix == NULL)
2279 (void) strlcat(tmp, logfile_suffix, sizeof(tmp));
2280 if (stat(tmp, &sb) < 0)
2282 return ((int)(ptimeget_secs(timenow) - sb.st_mtime + 1800) / 3600);
2285 /* Skip Over Blanks */
2289 while (p && *p && isspace(*p))
2294 /* Skip Over Non-Blanks */
2298 while (p && *p && !isspace(*p))
2303 /* Check if string is actually a number */
2305 isnumberstr(const char *string)
2308 if (!isdigitch(*string++))
2314 /* Check if string contains a glob */
2316 isglobstr(const char *string)
2320 while ((chr = *string++)) {
2321 if (chr == '*' || chr == '?' || chr == '[')
2328 * Save the active log file under a new name. A link to the new name
2329 * is the quick-and-easy way to do this. If that fails (which it will
2330 * if the destination is on another partition), then make a copy of
2331 * the file to the new location.
2334 savelog(char *from, char *to)
2339 res = link(from, to);
2343 if ((src = fopen(from, "r")) == NULL)
2344 err(1, "can't fopen %s for reading", from);
2345 if ((dst = fopen(to, "w")) == NULL)
2346 err(1, "can't fopen %s for writing", to);
2348 while ((c = getc(src)) != EOF) {
2349 if ((putc(c, dst)) == EOF)
2350 err(1, "error writing to %s", to);
2354 err(1, "error reading from %s", from);
2355 if ((fclose(src)) != 0)
2356 err(1, "can't fclose %s", to);
2357 if ((fclose(dst)) != 0)
2358 err(1, "can't fclose %s", from);
2361 /* create one or more directory components of a path */
2363 createdir(const struct conf_entry *ent, char *dirpart)
2367 char mkdirpath[MAXPATHLEN];
2375 if (*s != '/' && *s != '\0')
2378 res = lstat(mkdirpath, &st);
2381 printf("\tmkdir %s\n", mkdirpath);
2383 res = mkdir(mkdirpath, 0755);
2385 err(1, "Error on mkdir(\"%s\") for -a",
2393 if (ent->firstcreate)
2394 printf("Created directory '%s' for new %s\n",
2397 printf("Created directory '%s' for -a\n", dirpart);
2402 * Create a new log file, destroying any currently-existing version
2403 * of the log file in the process. If the caller wants a backup copy
2404 * of the file to exist, they should call 'link(logfile,logbackup)'
2405 * before calling this routine.
2408 createlog(const struct conf_entry *ent)
2412 char *realfile, *slash, tempfile[MAXPATHLEN];
2415 realfile = ent->log;
2418 * If this log file is being created for the first time (-C option),
2419 * then it may also be true that the parent directory does not exist
2420 * yet. Check, and create that directory if it is missing.
2422 if (ent->firstcreate) {
2423 strlcpy(tempfile, realfile, sizeof(tempfile));
2424 slash = strrchr(tempfile, '/');
2425 if (slash != NULL) {
2427 failed = stat(tempfile, &st);
2428 if (failed && errno != ENOENT)
2429 err(1, "Error on stat(%s)", tempfile);
2431 createdir(ent, tempfile);
2432 else if (!S_ISDIR(st.st_mode))
2433 errx(1, "%s exists but is not a directory",
2439 * First create an unused filename, so it can be chown'ed and
2440 * chmod'ed before it is moved into the real location. mkstemp
2441 * will create the file mode=600 & owned by us. Note that all
2442 * temp files will have a suffix of '.z<something>'.
2444 strlcpy(tempfile, realfile, sizeof(tempfile));
2445 strlcat(tempfile, ".zXXXXXX", sizeof(tempfile));
2447 printf("\tmktemp %s\n", tempfile);
2449 fd = mkstemp(tempfile);
2451 err(1, "can't mkstemp logfile %s", tempfile);
2454 * Add status message to what will become the new log file.
2456 if (!(ent->flags & CE_BINARY)) {
2457 if (log_trim(tempfile, ent))
2458 err(1, "can't add status message to log");
2462 /* Change the owner/group, if we are supposed to */
2463 if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) {
2465 printf("\tchown %u:%u %s\n", ent->uid, ent->gid,
2468 failed = fchown(fd, ent->uid, ent->gid);
2470 err(1, "can't fchown temp file %s", tempfile);
2474 /* Turn on NODUMP if it was requested in the config-file. */
2475 if (ent->flags & CE_NODUMP) {
2477 printf("\tchflags nodump %s\n", tempfile);
2479 failed = fchflags(fd, UF_NODUMP);
2481 warn("log_trim: fchflags(NODUMP)");
2487 * Note that if the real logfile still exists, and if the call
2488 * to rename() fails, then "neither the old file nor the new
2489 * file shall be changed or created" (to quote the standard).
2490 * If the call succeeds, then the file will be replaced without
2491 * any window where some other process might find that the file
2493 * XXX - ? It may be that for some error conditions, we could
2494 * retry by first removing the realfile and then renaming.
2497 printf("\tchmod %o %s\n", ent->permissions, tempfile);
2498 printf("\tmv %s %s\n", tempfile, realfile);
2500 failed = fchmod(fd, ent->permissions);
2502 err(1, "can't fchmod temp file '%s'", tempfile);
2503 failed = rename(tempfile, realfile);
2505 err(1, "can't mv %s to %s", tempfile, realfile);
2513 * Change the attributes of a given filename to what was specified in
2514 * the newsyslog.conf entry. This routine is only called for files
2515 * that newsyslog expects that it has created, and thus it is a fatal
2516 * error if this routine finds that the file does not exist.
2519 change_attrs(const char *fname, const struct conf_entry *ent)
2524 printf("\tchmod %o %s\n", ent->permissions, fname);
2526 if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
2527 printf("\tchown %u:%u %s\n",
2528 ent->uid, ent->gid, fname);
2530 if (ent->flags & CE_NODUMP)
2531 printf("\tchflags nodump %s\n", fname);
2535 failed = chmod(fname, ent->permissions);
2538 err(1, "chmod(%s) in change_attrs", fname);
2539 warn("change_attrs couldn't chmod(%s)", fname);
2542 if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) {
2543 failed = chown(fname, ent->uid, ent->gid);
2545 warn("can't chown %s", fname);
2548 if (ent->flags & CE_NODUMP) {
2549 failed = chflags(fname, UF_NODUMP);
2551 warn("can't chflags %s NODUMP", fname);