1 /*- 2 * ------+---------+---------+-------- + --------+---------+---------+---------* 3 * This file includes significant modifications done by: 4 * Copyright (c) 2003, 2004 - Garance Alistair Drosehn <gad@FreeBSD.org>. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 * 28 * ------+---------+---------+-------- + --------+---------+---------+---------* 29 */ 30 31 /* 32 * This file contains changes from the Open Software Foundation. 33 */ 34 35 /* 36 * Copyright 1988, 1989 by the Massachusetts Institute of Technology 37 * 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 47 * warranty. 48 * 49 * $FreeBSD: src/usr.sbin/newsyslog/newsyslog.c,v 1.117 2011/01/31 10:57:54 mm Exp $ 50 */ 51 52 /* 53 * newsyslog - roll over selected logs at the appropriate time, keeping the a 54 * specified number of backup files around. 55 */ 56 57 #define OSF 58 59 #include <sys/param.h> 60 #include <sys/queue.h> 61 #include <sys/stat.h> 62 #include <sys/user.h> 63 #include <sys/wait.h> 64 65 #include <assert.h> 66 #include <ctype.h> 67 #include <err.h> 68 #include <errno.h> 69 #include <dirent.h> 70 #include <fcntl.h> 71 #include <fnmatch.h> 72 #include <glob.h> 73 #include <grp.h> 74 #include <paths.h> 75 #include <pwd.h> 76 #include <signal.h> 77 #include <stdio.h> 78 #include <libgen.h> 79 #include <stdlib.h> 80 #include <string.h> 81 #include <time.h> 82 #include <unistd.h> 83 84 #include "pathnames.h" 85 #include "extern.h" 86 87 /* 88 * Compression suffixes 89 */ 90 #ifndef COMPRESS_SUFFIX_GZ 91 #define COMPRESS_SUFFIX_GZ ".gz" 92 #endif 93 94 #ifndef COMPRESS_SUFFIX_BZ2 95 #define COMPRESS_SUFFIX_BZ2 ".bz2" 96 #endif 97 98 #ifndef COMPRESS_SUFFIX_XZ 99 #define COMPRESS_SUFFIX_XZ ".xz" 100 #endif 101 102 #define COMPRESS_SUFFIX_MAXLEN MAX(MAX(sizeof(COMPRESS_SUFFIX_GZ),sizeof(COMPRESS_SUFFIX_BZ2)),sizeof(COMPRESS_SUFFIX_XZ)) 103 104 /* 105 * Compression types 106 */ 107 #define COMPRESS_TYPES 4 /* Number of supported compression types */ 108 109 #define COMPRESS_NONE 0 110 #define COMPRESS_GZIP 1 111 #define COMPRESS_BZIP2 2 112 #define COMPRESS_XZ 3 113 114 /* 115 * Bit-values for the 'flags' parsed from a config-file entry. 116 */ 117 #define CE_BINARY 0x0008 /* Logfile is in binary, do not add status */ 118 /* messages to logfile(s) when rotating. */ 119 #define CE_NOSIGNAL 0x0010 /* There is no process to signal when */ 120 /* trimming this file. */ 121 #define CE_TRIMAT 0x0020 /* trim file at a specific time. */ 122 #define CE_GLOB 0x0040 /* name of the log is file name pattern. */ 123 #define CE_SIGNALGROUP 0x0080 /* Signal a process-group instead of a single */ 124 /* process when trimming this file. */ 125 #define CE_CREATE 0x0100 /* Create the log file if it does not exist. */ 126 #define CE_NODUMP 0x0200 /* Set 'nodump' on newly created log file. */ 127 128 #define MIN_PID 5 /* Don't touch pids lower than this */ 129 #define MAX_PID PID_MAX /* was lower, see /usr/include/sys/proc.h */ 130 131 #define kbytes(size) (((size) + 1023) >> 10) 132 133 #define DEFAULT_MARKER "<default>" 134 #define DEBUG_MARKER "<debug>" 135 #define INCLUDE_MARKER "<include>" 136 #define DEFAULT_TIMEFNAME_FMT "%Y%m%dT%H%M%S" 137 138 #define MAX_OLDLOGS 65536 /* Default maximum number of old logfiles */ 139 140 struct compress_types { 141 const char *flag; /* Flag in configuration file */ 142 const char *suffix; /* Compression suffix */ 143 const char *path; /* Path to compression program */ 144 }; 145 146 const struct compress_types compress_type[COMPRESS_TYPES] = { 147 { "", "", "" }, /* no compression */ 148 { "Z", COMPRESS_SUFFIX_GZ, _PATH_GZIP }, /* gzip compression */ 149 { "J", COMPRESS_SUFFIX_BZ2, _PATH_BZIP2 }, /* bzip2 compression */ 150 { "X", COMPRESS_SUFFIX_XZ, _PATH_XZ } /* xz compression */ 151 }; 152 153 struct conf_entry { 154 STAILQ_ENTRY(conf_entry) cf_nextp; 155 char *log; /* Name of the log */ 156 char *pid_file; /* PID file */ 157 char *r_reason; /* The reason this file is being rotated */ 158 int firstcreate; /* Creating log for the first time (-C). */ 159 int rotate; /* Non-zero if this file should be rotated */ 160 int fsize; /* size found for the log file */ 161 uid_t uid; /* Owner of log */ 162 gid_t gid; /* Group of log */ 163 int numlogs; /* Number of logs to keep */ 164 int trsize; /* Size cutoff to trigger trimming the log */ 165 int hours; /* Hours between log trimming */ 166 struct ptime_data *trim_at; /* Specific time to do trimming */ 167 unsigned int permissions; /* File permissions on the log */ 168 int flags; /* CE_BINARY */ 169 int compress; /* Compression */ 170 int sig; /* Signal to send */ 171 int def_cfg; /* Using the <default> rule for this file */ 172 }; 173 174 struct sigwork_entry { 175 SLIST_ENTRY(sigwork_entry) sw_nextp; 176 int sw_signum; /* the signal to send */ 177 int sw_pidok; /* true if pid value is valid */ 178 pid_t sw_pid; /* the process id from the PID file */ 179 const char *sw_pidtype; /* "daemon" or "process group" */ 180 char sw_fname[1]; /* file the PID was read from */ 181 }; 182 183 struct zipwork_entry { 184 SLIST_ENTRY(zipwork_entry) zw_nextp; 185 const struct conf_entry *zw_conf; /* for chown/perm/flag info */ 186 const struct sigwork_entry *zw_swork; /* to know success of signal */ 187 int zw_fsize; /* size of the file to compress */ 188 char zw_fname[1]; /* the file to compress */ 189 }; 190 191 struct include_entry { 192 STAILQ_ENTRY(include_entry) inc_nextp; 193 const char *file; /* Name of file to process */ 194 }; 195 196 struct oldlog_entry { 197 char *fname; /* Filename of the log file */ 198 time_t t; /* Parsed timestamp of the logfile */ 199 }; 200 201 typedef enum { 202 FREE_ENT, KEEP_ENT 203 } fk_entry; 204 205 STAILQ_HEAD(cflist, conf_entry); 206 SLIST_HEAD(swlisthead, sigwork_entry) swhead = SLIST_HEAD_INITIALIZER(swhead); 207 SLIST_HEAD(zwlisthead, zipwork_entry) zwhead = SLIST_HEAD_INITIALIZER(zwhead); 208 STAILQ_HEAD(ilist, include_entry); 209 210 int dbg_at_times; /* -D Show details of 'trim_at' code */ 211 212 int archtodir = 0; /* Archive old logfiles to other directory */ 213 int createlogs; /* Create (non-GLOB) logfiles which do not */ 214 /* already exist. 1=='for entries with */ 215 /* C flag', 2=='for all entries'. */ 216 int verbose = 0; /* Print out what's going on */ 217 int needroot = 1; /* Root privs are necessary */ 218 int noaction = 0; /* Don't do anything, just show it */ 219 int norotate = 0; /* Don't rotate */ 220 int nosignal; /* Do not send any signals */ 221 int enforcepid = 0; /* If PID file does not exist or empty, do nothing */ 222 int force = 0; /* Force the trim no matter what */ 223 int rotatereq = 0; /* -R = Always rotate the file(s) as given */ 224 /* on the command (this also requires */ 225 /* that a list of files *are* given on */ 226 /* the run command). */ 227 char *requestor; /* The name given on a -R request */ 228 char *timefnamefmt = NULL; /* Use time based filenames instead of .0 etc */ 229 char *archdirname; /* Directory path to old logfiles archive */ 230 char *destdir = NULL; /* Directory to treat at root for logs */ 231 const char *conf; /* Configuration file to use */ 232 233 struct ptime_data *dbg_timenow; /* A "timenow" value set via -D option */ 234 struct ptime_data *timenow; /* The time to use for checking at-fields */ 235 236 #define DAYTIME_LEN 16 237 char daytime[DAYTIME_LEN]; /* The current time in human readable form, 238 * used for rotation-tracking messages. */ 239 char hostname[MAXHOSTNAMELEN]; /* hostname */ 240 241 const char *path_syslogpid = _PATH_SYSLOGPID; 242 243 static struct cflist *get_worklist(char **files); 244 static void parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p, 245 struct conf_entry *defconf_p, struct ilist *inclist); 246 static void add_to_queue(const char *fname, struct ilist *inclist); 247 static char *sob(char *p); 248 static char *son(char *p); 249 static int isnumberstr(const char *); 250 static int isglobstr(const char *); 251 static char *missing_field(char *p, char *errline); 252 static void change_attrs(const char *, const struct conf_entry *); 253 static const char *get_logfile_suffix(const char *logfile); 254 static fk_entry do_entry(struct conf_entry *); 255 static fk_entry do_rotate(const struct conf_entry *); 256 static void do_sigwork(struct sigwork_entry *); 257 static void do_zipwork(struct zipwork_entry *); 258 static struct sigwork_entry * 259 save_sigwork(const struct conf_entry *); 260 static struct zipwork_entry * 261 save_zipwork(const struct conf_entry *, const struct 262 sigwork_entry *, int, const char *); 263 static void set_swpid(struct sigwork_entry *, const struct conf_entry *); 264 static int sizefile(const char *); 265 static void expand_globs(struct cflist *work_p, struct cflist *glob_p); 266 static void free_clist(struct cflist *list); 267 static void free_entry(struct conf_entry *ent); 268 static struct conf_entry *init_entry(const char *fname, 269 struct conf_entry *src_entry); 270 static void parse_args(int argc, char **argv); 271 static int parse_doption(const char *doption); 272 static void usage(void); 273 static int log_trim(const char *logname, const struct conf_entry *log_ent); 274 static int age_old_log(char *file); 275 static void savelog(char *from, char *to); 276 static void createdir(const struct conf_entry *ent, char *dirpart); 277 static void createlog(const struct conf_entry *ent); 278 279 /* 280 * All the following take a parameter of 'int', but expect values in the 281 * range of unsigned char. Define wrappers which take values of type 'char', 282 * whether signed or unsigned, and ensure they end up in the right range. 283 */ 284 #define isdigitch(Anychar) isdigit((u_char)(Anychar)) 285 #define isprintch(Anychar) isprint((u_char)(Anychar)) 286 #define isspacech(Anychar) isspace((u_char)(Anychar)) 287 #define tolowerch(Anychar) tolower((u_char)(Anychar)) 288 289 int 290 main(int argc, char **argv) 291 { 292 struct cflist *worklist; 293 struct conf_entry *p; 294 struct sigwork_entry *stmp; 295 struct zipwork_entry *ztmp; 296 297 SLIST_INIT(&swhead); 298 SLIST_INIT(&zwhead); 299 300 parse_args(argc, argv); 301 argc -= optind; 302 argv += optind; 303 304 if (needroot && getuid() && geteuid()) 305 errx(1, "must have root privs"); 306 worklist = get_worklist(argv); 307 308 /* 309 * Rotate all the files which need to be rotated. Note that 310 * some users have *hundreds* of entries in newsyslog.conf! 311 */ 312 while (!STAILQ_EMPTY(worklist)) { 313 p = STAILQ_FIRST(worklist); 314 STAILQ_REMOVE_HEAD(worklist, cf_nextp); 315 if (do_entry(p) == FREE_ENT) 316 free_entry(p); 317 } 318 319 /* 320 * Send signals to any processes which need a signal to tell 321 * them to close and re-open the log file(s) we have rotated. 322 * Note that zipwork_entries include pointers to these 323 * sigwork_entry's, so we can not free the entries here. 324 */ 325 if (!SLIST_EMPTY(&swhead)) { 326 if (noaction || verbose) 327 printf("Signal all daemon process(es)...\n"); 328 SLIST_FOREACH(stmp, &swhead, sw_nextp) 329 do_sigwork(stmp); 330 if (noaction) 331 printf("\tsleep 10\n"); 332 else { 333 if (verbose) 334 printf("Pause 10 seconds to allow daemon(s)" 335 " to close log file(s)\n"); 336 sleep(10); 337 } 338 } 339 /* 340 * Compress all files that we're expected to compress, now 341 * that all processes should have closed the files which 342 * have been rotated. 343 */ 344 if (!SLIST_EMPTY(&zwhead)) { 345 if (noaction || verbose) 346 printf("Compress all rotated log file(s)...\n"); 347 while (!SLIST_EMPTY(&zwhead)) { 348 ztmp = SLIST_FIRST(&zwhead); 349 do_zipwork(ztmp); 350 SLIST_REMOVE_HEAD(&zwhead, zw_nextp); 351 free(ztmp); 352 } 353 } 354 /* Now free all the sigwork entries. */ 355 while (!SLIST_EMPTY(&swhead)) { 356 stmp = SLIST_FIRST(&swhead); 357 SLIST_REMOVE_HEAD(&swhead, sw_nextp); 358 free(stmp); 359 } 360 361 while (wait(NULL) > 0 || errno == EINTR) 362 ; 363 return (0); 364 } 365 366 static struct conf_entry * 367 init_entry(const char *fname, struct conf_entry *src_entry) 368 { 369 struct conf_entry *tempwork; 370 371 if (verbose > 4) 372 printf("\t--> [creating entry for %s]\n", fname); 373 374 tempwork = malloc(sizeof(struct conf_entry)); 375 if (tempwork == NULL) 376 err(1, "malloc of conf_entry for %s", fname); 377 378 if (destdir == NULL || fname[0] != '/') 379 tempwork->log = strdup(fname); 380 else 381 asprintf(&tempwork->log, "%s%s", destdir, fname); 382 if (tempwork->log == NULL) 383 err(1, "strdup for %s", fname); 384 385 if (src_entry != NULL) { 386 tempwork->pid_file = NULL; 387 if (src_entry->pid_file) 388 tempwork->pid_file = strdup(src_entry->pid_file); 389 tempwork->r_reason = NULL; 390 tempwork->firstcreate = 0; 391 tempwork->rotate = 0; 392 tempwork->fsize = -1; 393 tempwork->uid = src_entry->uid; 394 tempwork->gid = src_entry->gid; 395 tempwork->numlogs = src_entry->numlogs; 396 tempwork->trsize = src_entry->trsize; 397 tempwork->hours = src_entry->hours; 398 tempwork->trim_at = NULL; 399 if (src_entry->trim_at != NULL) 400 tempwork->trim_at = ptime_init(src_entry->trim_at); 401 tempwork->permissions = src_entry->permissions; 402 tempwork->flags = src_entry->flags; 403 tempwork->compress = src_entry->compress; 404 tempwork->sig = src_entry->sig; 405 tempwork->def_cfg = src_entry->def_cfg; 406 } else { 407 /* Initialize as a "do-nothing" entry */ 408 tempwork->pid_file = NULL; 409 tempwork->r_reason = NULL; 410 tempwork->firstcreate = 0; 411 tempwork->rotate = 0; 412 tempwork->fsize = -1; 413 tempwork->uid = (uid_t)-1; 414 tempwork->gid = (gid_t)-1; 415 tempwork->numlogs = 1; 416 tempwork->trsize = -1; 417 tempwork->hours = -1; 418 tempwork->trim_at = NULL; 419 tempwork->permissions = 0; 420 tempwork->flags = 0; 421 tempwork->compress = COMPRESS_NONE; 422 tempwork->sig = SIGHUP; 423 tempwork->def_cfg = 0; 424 } 425 426 return (tempwork); 427 } 428 429 static void 430 free_entry(struct conf_entry *ent) 431 { 432 433 if (ent == NULL) 434 return; 435 436 if (ent->log != NULL) { 437 if (verbose > 4) 438 printf("\t--> [freeing entry for %s]\n", ent->log); 439 free(ent->log); 440 ent->log = NULL; 441 } 442 443 if (ent->pid_file != NULL) { 444 free(ent->pid_file); 445 ent->pid_file = NULL; 446 } 447 448 if (ent->r_reason != NULL) { 449 free(ent->r_reason); 450 ent->r_reason = NULL; 451 } 452 453 if (ent->trim_at != NULL) { 454 ptime_free(ent->trim_at); 455 ent->trim_at = NULL; 456 } 457 458 free(ent); 459 } 460 461 static void 462 free_clist(struct cflist *list) 463 { 464 struct conf_entry *ent; 465 466 while (!STAILQ_EMPTY(list)) { 467 ent = STAILQ_FIRST(list); 468 STAILQ_REMOVE_HEAD(list, cf_nextp); 469 free_entry(ent); 470 } 471 472 free(list); 473 list = NULL; 474 } 475 476 static fk_entry 477 do_entry(struct conf_entry * ent) 478 { 479 #define REASON_MAX 80 480 int modtime; 481 fk_entry free_or_keep; 482 double diffsecs; 483 char temp_reason[REASON_MAX]; 484 485 free_or_keep = FREE_ENT; 486 if (verbose) 487 printf("%s <%d%s>: ", ent->log, ent->numlogs, 488 compress_type[ent->compress].flag); 489 ent->fsize = sizefile(ent->log); 490 modtime = age_old_log(ent->log); 491 ent->rotate = 0; 492 ent->firstcreate = 0; 493 if (ent->fsize < 0) { 494 /* 495 * If either the C flag or the -C option was specified, 496 * and if we won't be creating the file, then have the 497 * verbose message include a hint as to why the file 498 * will not be created. 499 */ 500 temp_reason[0] = '\0'; 501 if (createlogs > 1) 502 ent->firstcreate = 1; 503 else if ((ent->flags & CE_CREATE) && createlogs) 504 ent->firstcreate = 1; 505 else if (ent->flags & CE_CREATE) 506 strlcpy(temp_reason, " (no -C option)", REASON_MAX); 507 else if (createlogs) 508 strlcpy(temp_reason, " (no C flag)", REASON_MAX); 509 510 if (ent->firstcreate) { 511 if (verbose) 512 printf("does not exist -> will create.\n"); 513 createlog(ent); 514 } else if (verbose) { 515 printf("does not exist, skipped%s.\n", temp_reason); 516 } 517 } else { 518 if (ent->flags & CE_TRIMAT && !force && !rotatereq) { 519 diffsecs = ptimeget_diff(timenow, ent->trim_at); 520 if (diffsecs < 0.0) { 521 /* trim_at is some time in the future. */ 522 if (verbose) { 523 ptime_adjust4dst(ent->trim_at, 524 timenow); 525 printf("--> will trim at %s", 526 ptimeget_ctime(ent->trim_at)); 527 } 528 return (free_or_keep); 529 } else if (diffsecs >= 3600.0) { 530 /* 531 * trim_at is more than an hour in the past, 532 * so find the next valid trim_at time, and 533 * tell the user what that will be. 534 */ 535 if (verbose && dbg_at_times) 536 printf("\n\t--> prev trim at %s\t", 537 ptimeget_ctime(ent->trim_at)); 538 if (verbose) { 539 ptimeset_nxtime(ent->trim_at); 540 printf("--> will trim at %s", 541 ptimeget_ctime(ent->trim_at)); 542 } 543 return (free_or_keep); 544 } else if (verbose && noaction && dbg_at_times) { 545 /* 546 * If we are just debugging at-times, then 547 * a detailed message is helpful. Also 548 * skip "doing" any commands, since they 549 * would all be turned off by no-action. 550 */ 551 printf("\n\t--> timematch at %s", 552 ptimeget_ctime(ent->trim_at)); 553 return (free_or_keep); 554 } else if (verbose && ent->hours <= 0) { 555 printf("--> time is up\n"); 556 } 557 } 558 if (verbose && (ent->trsize > 0)) 559 printf("size (Kb): %d [%d] ", ent->fsize, ent->trsize); 560 if (verbose && (ent->hours > 0)) 561 printf(" age (hr): %d [%d] ", modtime, ent->hours); 562 563 /* 564 * Figure out if this logfile needs to be rotated. 565 */ 566 temp_reason[0] = '\0'; 567 if (rotatereq) { 568 ent->rotate = 1; 569 snprintf(temp_reason, REASON_MAX, " due to -R from %s", 570 requestor); 571 } else if (force) { 572 ent->rotate = 1; 573 snprintf(temp_reason, REASON_MAX, " due to -F request"); 574 } else if ((ent->trsize > 0) && (ent->fsize >= ent->trsize)) { 575 ent->rotate = 1; 576 snprintf(temp_reason, REASON_MAX, " due to size>%dK", 577 ent->trsize); 578 } else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) { 579 ent->rotate = 1; 580 } else if ((ent->hours > 0) && ((modtime >= ent->hours) || 581 (modtime < 0))) { 582 ent->rotate = 1; 583 } 584 585 /* 586 * If the file needs to be rotated, then rotate it. 587 */ 588 if (ent->rotate && !norotate) { 589 if (temp_reason[0] != '\0') 590 ent->r_reason = strdup(temp_reason); 591 if (verbose) 592 printf("--> trimming log....\n"); 593 if (noaction && !verbose) 594 printf("%s <%d%s>: trimming\n", ent->log, 595 ent->numlogs, 596 compress_type[ent->compress].flag); 597 free_or_keep = do_rotate(ent); 598 } else { 599 if (verbose) 600 printf("--> skipping\n"); 601 } 602 } 603 return (free_or_keep); 604 #undef REASON_MAX 605 } 606 607 static void 608 parse_args(int argc, char **argv) 609 { 610 int ch; 611 char *p; 612 613 timenow = ptime_init(NULL); 614 ptimeset_time(timenow, time(NULL)); 615 strlcpy(daytime, ptimeget_ctime(timenow) + 4, DAYTIME_LEN); 616 617 /* Let's get our hostname */ 618 gethostname(hostname, sizeof(hostname)); 619 620 /* Truncate domain */ 621 if ((p = strchr(hostname, '.')) != NULL) 622 *p = '\0'; 623 624 /* Parse command line options. */ 625 while ((ch = getopt(argc, argv, "a:d:f:nrst:vCD:FNPR:S:")) != -1) 626 switch (ch) { 627 case 'a': 628 archtodir++; 629 archdirname = optarg; 630 break; 631 case 'd': 632 destdir = optarg; 633 break; 634 case 'f': 635 conf = optarg; 636 break; 637 case 'n': 638 noaction++; 639 break; 640 case 'r': 641 needroot = 0; 642 break; 643 case 's': 644 nosignal = 1; 645 break; 646 case 't': 647 if (optarg[0] == '\0' || 648 strcmp(optarg, "DEFAULT") == 0) 649 timefnamefmt = strdup(DEFAULT_TIMEFNAME_FMT); 650 else 651 timefnamefmt = strdup(optarg); 652 break; 653 case 'v': 654 verbose++; 655 break; 656 case 'C': 657 /* Useful for things like rc.diskless... */ 658 createlogs++; 659 break; 660 case 'D': 661 /* 662 * Set some debugging option. The specific option 663 * depends on the value of optarg. These options 664 * may come and go without notice or documentation. 665 */ 666 if (parse_doption(optarg)) 667 break; 668 usage(); 669 /* NOTREACHED */ 670 case 'F': 671 force++; 672 break; 673 case 'N': 674 norotate++; 675 break; 676 case 'P': 677 enforcepid++; 678 break; 679 case 'R': 680 rotatereq++; 681 requestor = strdup(optarg); 682 break; 683 case 'S': 684 path_syslogpid = optarg; 685 break; 686 case 'm': /* Used by OpenBSD for "monitor mode" */ 687 default: 688 usage(); 689 /* NOTREACHED */ 690 } 691 692 if (force && norotate) { 693 warnx("Only one of -F and -N may be specified."); 694 usage(); 695 /* NOTREACHED */ 696 } 697 698 if (rotatereq) { 699 if (optind == argc) { 700 warnx("At least one filename must be given when -R is specified."); 701 usage(); 702 /* NOTREACHED */ 703 } 704 /* Make sure "requestor" value is safe for a syslog message. */ 705 for (p = requestor; *p != '\0'; p++) { 706 if (!isprintch(*p) && (*p != '\t')) 707 *p = '.'; 708 } 709 } 710 711 if (dbg_timenow) { 712 /* 713 * Note that the 'daytime' variable is not changed. 714 * That is only used in messages that track when a 715 * logfile is rotated, and if a file *is* rotated, 716 * then it will still rotated at the "real now" time. 717 */ 718 ptime_free(timenow); 719 timenow = dbg_timenow; 720 fprintf(stderr, "Debug: Running as if TimeNow is %s", 721 ptimeget_ctime(dbg_timenow)); 722 } 723 724 } 725 726 /* 727 * These debugging options are mainly meant for developer use, such 728 * as writing regression-tests. They would not be needed by users 729 * during normal operation of newsyslog... 730 */ 731 static int 732 parse_doption(const char *doption) 733 { 734 const char TN[] = "TN="; 735 int res; 736 737 if (strncmp(doption, TN, sizeof(TN) - 1) == 0) { 738 /* 739 * The "TimeNow" debugging option. This might be off 740 * by an hour when crossing a timezone change. 741 */ 742 dbg_timenow = ptime_init(NULL); 743 res = ptime_relparse(dbg_timenow, PTM_PARSE_ISO8601, 744 time(NULL), doption + sizeof(TN) - 1); 745 if (res == -2) { 746 warnx("Non-existent time specified on -D %s", doption); 747 return (0); /* failure */ 748 } else if (res < 0) { 749 warnx("Malformed time given on -D %s", doption); 750 return (0); /* failure */ 751 } 752 return (1); /* successfully parsed */ 753 754 } 755 756 if (strcmp(doption, "ats") == 0) { 757 dbg_at_times++; 758 return (1); /* successfully parsed */ 759 } 760 761 /* XXX - This check could probably be dropped. */ 762 if ((strcmp(doption, "neworder") == 0) || (strcmp(doption, "oldorder") 763 == 0)) { 764 warnx("NOTE: newsyslog always uses 'neworder'."); 765 return (1); /* successfully parsed */ 766 } 767 768 warnx("Unknown -D (debug) option: '%s'", doption); 769 return (0); /* failure */ 770 } 771 772 static void 773 usage(void) 774 { 775 776 fprintf(stderr, 777 "usage: newsyslog [-CFNnrsv] [-a directory] [-d directory] [-f config-file]\n" 778 " [-S pidfile] [-t timefmt ] [ [-R requestor] filename ... ]\n"); 779 exit(1); 780 } 781 782 /* 783 * Parse a configuration file and return a linked list of all the logs 784 * which should be processed. 785 */ 786 static struct cflist * 787 get_worklist(char **files) 788 { 789 FILE *f; 790 char **given; 791 struct cflist *cmdlist, *filelist, *globlist; 792 struct conf_entry *defconf, *dupent, *ent; 793 struct ilist inclist; 794 struct include_entry *inc; 795 int gmatch, fnres; 796 797 defconf = NULL; 798 STAILQ_INIT(&inclist); 799 800 filelist = malloc(sizeof(struct cflist)); 801 if (filelist == NULL) 802 err(1, "malloc of filelist"); 803 STAILQ_INIT(filelist); 804 globlist = malloc(sizeof(struct cflist)); 805 if (globlist == NULL) 806 err(1, "malloc of globlist"); 807 STAILQ_INIT(globlist); 808 809 inc = malloc(sizeof(struct include_entry)); 810 if (inc == NULL) 811 err(1, "malloc of inc"); 812 inc->file = conf; 813 if (inc->file == NULL) 814 inc->file = _PATH_CONF; 815 STAILQ_INSERT_TAIL(&inclist, inc, inc_nextp); 816 817 STAILQ_FOREACH(inc, &inclist, inc_nextp) { 818 if (strcmp(inc->file, "-") != 0) 819 f = fopen(inc->file, "r"); 820 else { 821 f = stdin; 822 inc->file = "<stdin>"; 823 } 824 if (!f) 825 err(1, "%s", inc->file); 826 827 if (verbose) 828 printf("Processing %s\n", inc->file); 829 parse_file(f, filelist, globlist, defconf, &inclist); 830 fclose(f); 831 } 832 833 /* 834 * All config-file information has been read in and turned into 835 * a filelist and a globlist. If there were no specific files 836 * given on the run command, then the only thing left to do is to 837 * call a routine which finds all files matched by the globlist 838 * and adds them to the filelist. Then return the worklist. 839 */ 840 if (*files == NULL) { 841 expand_globs(filelist, globlist); 842 free_clist(globlist); 843 if (defconf != NULL) 844 free_entry(defconf); 845 return (filelist); 846 /* NOTREACHED */ 847 } 848 849 /* 850 * If newsyslog was given a specific list of files to process, 851 * it may be that some of those files were not listed in any 852 * config file. Those unlisted files should get the default 853 * rotation action. First, create the default-rotation action 854 * if none was found in a system config file. 855 */ 856 if (defconf == NULL) { 857 defconf = init_entry(DEFAULT_MARKER, NULL); 858 defconf->numlogs = 3; 859 defconf->trsize = 50; 860 defconf->permissions = S_IRUSR|S_IWUSR; 861 } 862 863 /* 864 * If newsyslog was run with a list of specific filenames, 865 * then create a new worklist which has only those files in 866 * it, picking up the rotation-rules for those files from 867 * the original filelist. 868 * 869 * XXX - Note that this will copy multiple rules for a single 870 * logfile, if multiple entries are an exact match for 871 * that file. That matches the historic behavior, but do 872 * we want to continue to allow it? If so, it should 873 * probably be handled more intelligently. 874 */ 875 cmdlist = malloc(sizeof(struct cflist)); 876 if (cmdlist == NULL) 877 err(1, "malloc of cmdlist"); 878 STAILQ_INIT(cmdlist); 879 880 for (given = files; *given; ++given) { 881 /* 882 * First try to find exact-matches for this given file. 883 */ 884 gmatch = 0; 885 STAILQ_FOREACH(ent, filelist, cf_nextp) { 886 if (strcmp(ent->log, *given) == 0) { 887 gmatch++; 888 dupent = init_entry(*given, ent); 889 STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 890 } 891 } 892 if (gmatch) { 893 if (verbose > 2) 894 printf("\t+ Matched entry %s\n", *given); 895 continue; 896 } 897 898 /* 899 * There was no exact-match for this given file, so look 900 * for a "glob" entry which does match. 901 */ 902 gmatch = 0; 903 if (verbose > 2 && globlist != NULL) 904 printf("\t+ Checking globs for %s\n", *given); 905 STAILQ_FOREACH(ent, globlist, cf_nextp) { 906 fnres = fnmatch(ent->log, *given, FNM_PATHNAME); 907 if (verbose > 2) 908 printf("\t+ = %d for pattern %s\n", fnres, 909 ent->log); 910 if (fnres == 0) { 911 gmatch++; 912 dupent = init_entry(*given, ent); 913 /* This new entry is not a glob! */ 914 dupent->flags &= ~CE_GLOB; 915 STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 916 /* Only allow a match to one glob-entry */ 917 break; 918 } 919 } 920 if (gmatch) { 921 if (verbose > 2) 922 printf("\t+ Matched %s via %s\n", *given, 923 ent->log); 924 continue; 925 } 926 927 /* 928 * This given file was not found in any config file, so 929 * add a worklist item based on the default entry. 930 */ 931 if (verbose > 2) 932 printf("\t+ No entry matched %s (will use %s)\n", 933 *given, DEFAULT_MARKER); 934 dupent = init_entry(*given, defconf); 935 /* Mark that it was *not* found in a config file */ 936 dupent->def_cfg = 1; 937 STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp); 938 } 939 940 /* 941 * Free all the entries in the original work list, the list of 942 * glob entries, and the default entry. 943 */ 944 free_clist(filelist); 945 free_clist(globlist); 946 free_entry(defconf); 947 948 /* And finally, return a worklist which matches the given files. */ 949 return (cmdlist); 950 } 951 952 /* 953 * Expand the list of entries with filename patterns, and add all files 954 * which match those glob-entries onto the worklist. 955 */ 956 static void 957 expand_globs(struct cflist *work_p, struct cflist *glob_p) 958 { 959 int gmatch, gres; 960 size_t i; 961 char *mfname; 962 struct conf_entry *dupent, *ent, *globent; 963 glob_t pglob; 964 struct stat st_fm; 965 966 /* 967 * The worklist contains all fully-specified (non-GLOB) names. 968 * 969 * Now expand the list of filename-pattern (GLOB) entries into 970 * a second list, which (by definition) will only match files 971 * that already exist. Do not add a glob-related entry for any 972 * file which already exists in the fully-specified list. 973 */ 974 STAILQ_FOREACH(globent, glob_p, cf_nextp) { 975 gres = glob(globent->log, GLOB_NOCHECK, NULL, &pglob); 976 if (gres != 0) { 977 warn("cannot expand pattern (%d): %s", gres, 978 globent->log); 979 continue; 980 } 981 982 if (verbose > 2) 983 printf("\t+ Expanding pattern %s\n", globent->log); 984 for (i = 0; i < pglob.gl_matchc; i++) { 985 mfname = pglob.gl_pathv[i]; 986 987 /* See if this file already has a specific entry. */ 988 gmatch = 0; 989 STAILQ_FOREACH(ent, work_p, cf_nextp) { 990 if (strcmp(mfname, ent->log) == 0) { 991 gmatch++; 992 break; 993 } 994 } 995 if (gmatch) 996 continue; 997 998 /* Make sure the named matched is a file. */ 999 gres = lstat(mfname, &st_fm); 1000 if (gres != 0) { 1001 /* Error on a file that glob() matched?!? */ 1002 warn("Skipping %s - lstat() error", mfname); 1003 continue; 1004 } 1005 if (!S_ISREG(st_fm.st_mode)) { 1006 /* We only rotate files! */ 1007 if (verbose > 2) 1008 printf("\t+ . skipping %s (!file)\n", 1009 mfname); 1010 continue; 1011 } 1012 1013 if (verbose > 2) 1014 printf("\t+ . add file %s\n", mfname); 1015 dupent = init_entry(mfname, globent); 1016 /* This new entry is not a glob! */ 1017 dupent->flags &= ~CE_GLOB; 1018 1019 /* Add to the worklist. */ 1020 STAILQ_INSERT_TAIL(work_p, dupent, cf_nextp); 1021 } 1022 globfree(&pglob); 1023 if (verbose > 2) 1024 printf("\t+ Done with pattern %s\n", globent->log); 1025 } 1026 } 1027 1028 /* 1029 * Parse a configuration file and update a linked list of all the logs to 1030 * process. 1031 */ 1032 static void 1033 parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p, 1034 struct conf_entry *defconf_p, struct ilist *inclist) 1035 { 1036 char line[BUFSIZ], *parse, *q; 1037 char *cp, *errline, *group; 1038 struct conf_entry *working; 1039 struct passwd *pwd; 1040 struct group *grp; 1041 glob_t pglob; 1042 int eol, ptm_opts, res, special; 1043 size_t i; 1044 1045 errline = NULL; 1046 while (fgets(line, BUFSIZ, cf)) { 1047 if ((line[0] == '\n') || (line[0] == '#') || 1048 (strlen(line) == 0)) 1049 continue; 1050 if (errline != NULL) 1051 free(errline); 1052 errline = strdup(line); 1053 for (cp = line + 1; *cp != '\0'; cp++) { 1054 if (*cp != '#') 1055 continue; 1056 if (*(cp - 1) == '\\') { 1057 strcpy(cp - 1, cp); 1058 cp--; 1059 continue; 1060 } 1061 *cp = '\0'; 1062 break; 1063 } 1064 1065 q = parse = missing_field(sob(line), errline); 1066 parse = son(line); 1067 if (!*parse) 1068 errx(1, "malformed line (missing fields):\n%s", 1069 errline); 1070 *parse = '\0'; 1071 1072 /* 1073 * Allow people to set debug options via the config file. 1074 * (NOTE: debug options are undocumented, and may disappear 1075 * at any time, etc). 1076 */ 1077 if (strcasecmp(DEBUG_MARKER, q) == 0) { 1078 q = parse = missing_field(sob(++parse), errline); 1079 parse = son(parse); 1080 if (!*parse) 1081 warnx("debug line specifies no option:\n%s", 1082 errline); 1083 else { 1084 *parse = '\0'; 1085 parse_doption(q); 1086 } 1087 continue; 1088 } else if (strcasecmp(INCLUDE_MARKER, q) == 0) { 1089 if (verbose) 1090 printf("Found: %s", errline); 1091 q = parse = missing_field(sob(++parse), errline); 1092 parse = son(parse); 1093 if (!*parse) { 1094 warnx("include line missing argument:\n%s", 1095 errline); 1096 continue; 1097 } 1098 1099 *parse = '\0'; 1100 1101 if (isglobstr(q)) { 1102 res = glob(q, GLOB_NOCHECK, NULL, &pglob); 1103 if (res != 0) { 1104 warn("cannot expand pattern (%d): %s", 1105 res, q); 1106 continue; 1107 } 1108 1109 if (verbose > 2) 1110 printf("\t+ Expanding pattern %s\n", q); 1111 1112 for (i = 0; i < pglob.gl_matchc; i++) 1113 add_to_queue(pglob.gl_pathv[i], 1114 inclist); 1115 globfree(&pglob); 1116 } else 1117 add_to_queue(q, inclist); 1118 continue; 1119 } 1120 1121 special = 0; 1122 working = init_entry(q, NULL); 1123 if (strcasecmp(DEFAULT_MARKER, q) == 0) { 1124 special = 1; 1125 if (defconf_p != NULL) { 1126 warnx("Ignoring duplicate entry for %s!", q); 1127 free_entry(working); 1128 continue; 1129 } 1130 defconf_p = working; 1131 } 1132 1133 q = parse = missing_field(sob(++parse), errline); 1134 parse = son(parse); 1135 if (!*parse) 1136 errx(1, "malformed line (missing fields):\n%s", 1137 errline); 1138 *parse = '\0'; 1139 if ((group = strchr(q, ':')) != NULL || 1140 (group = strrchr(q, '.')) != NULL) { 1141 *group++ = '\0'; 1142 if (*q) { 1143 if (!(isnumberstr(q))) { 1144 if ((pwd = getpwnam(q)) == NULL) 1145 errx(1, 1146 "error in config file; unknown user:\n%s", 1147 errline); 1148 working->uid = pwd->pw_uid; 1149 } else 1150 working->uid = atoi(q); 1151 } else 1152 working->uid = (uid_t)-1; 1153 1154 q = group; 1155 if (*q) { 1156 if (!(isnumberstr(q))) { 1157 if ((grp = getgrnam(q)) == NULL) 1158 errx(1, 1159 "error in config file; unknown group:\n%s", 1160 errline); 1161 working->gid = grp->gr_gid; 1162 } else 1163 working->gid = atoi(q); 1164 } else 1165 working->gid = (gid_t)-1; 1166 1167 q = parse = missing_field(sob(++parse), errline); 1168 parse = son(parse); 1169 if (!*parse) 1170 errx(1, "malformed line (missing fields):\n%s", 1171 errline); 1172 *parse = '\0'; 1173 } else { 1174 working->uid = (uid_t)-1; 1175 working->gid = (gid_t)-1; 1176 } 1177 1178 if (!sscanf(q, "%o", &working->permissions)) 1179 errx(1, "error in config file; bad permissions:\n%s", 1180 errline); 1181 1182 q = parse = missing_field(sob(++parse), errline); 1183 parse = son(parse); 1184 if (!*parse) 1185 errx(1, "malformed line (missing fields):\n%s", 1186 errline); 1187 *parse = '\0'; 1188 if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0) 1189 errx(1, "error in config file; bad value for count of logs to save:\n%s", 1190 errline); 1191 1192 q = parse = missing_field(sob(++parse), errline); 1193 parse = son(parse); 1194 if (!*parse) 1195 errx(1, "malformed line (missing fields):\n%s", 1196 errline); 1197 *parse = '\0'; 1198 if (isdigitch(*q)) 1199 working->trsize = atoi(q); 1200 else if (strcmp(q, "*") == 0) 1201 working->trsize = -1; 1202 else { 1203 warnx("Invalid value of '%s' for 'size' in line:\n%s", 1204 q, errline); 1205 working->trsize = -1; 1206 } 1207 1208 working->flags = 0; 1209 working->compress = COMPRESS_NONE; 1210 q = parse = missing_field(sob(++parse), errline); 1211 parse = son(parse); 1212 eol = !*parse; 1213 *parse = '\0'; 1214 { 1215 char *ep; 1216 u_long ul; 1217 1218 ul = strtoul(q, &ep, 10); 1219 if (ep == q) 1220 working->hours = 0; 1221 else if (*ep == '*') 1222 working->hours = -1; 1223 else if (ul > INT_MAX) 1224 errx(1, "interval is too large:\n%s", errline); 1225 else 1226 working->hours = ul; 1227 1228 if (*ep == '\0' || strcmp(ep, "*") == 0) 1229 goto no_trimat; 1230 if (*ep != '@' && *ep != '$') 1231 errx(1, "malformed interval/at:\n%s", errline); 1232 1233 working->flags |= CE_TRIMAT; 1234 working->trim_at = ptime_init(NULL); 1235 ptm_opts = PTM_PARSE_ISO8601; 1236 if (*ep == '$') 1237 ptm_opts = PTM_PARSE_DWM; 1238 ptm_opts |= PTM_PARSE_MATCHDOM; 1239 res = ptime_relparse(working->trim_at, ptm_opts, 1240 ptimeget_secs(timenow), ep + 1); 1241 if (res == -2) 1242 errx(1, "nonexistent time for 'at' value:\n%s", 1243 errline); 1244 else if (res < 0) 1245 errx(1, "malformed 'at' value:\n%s", errline); 1246 } 1247 no_trimat: 1248 1249 if (eol) 1250 q = NULL; 1251 else { 1252 q = parse = sob(++parse); /* Optional field */ 1253 parse = son(parse); 1254 if (!*parse) 1255 eol = 1; 1256 *parse = '\0'; 1257 } 1258 1259 for (; q && *q && !isspacech(*q); q++) { 1260 switch (tolowerch(*q)) { 1261 case 'b': 1262 working->flags |= CE_BINARY; 1263 break; 1264 case 'c': 1265 /* 1266 * XXX - Ick! Ugly! Remove ASAP! 1267 * We want `c' and `C' for "create". But we 1268 * will temporarily treat `c' as `g', because 1269 * FreeBSD releases <= 4.8 have a typo of 1270 * checking ('G' || 'c') for CE_GLOB. 1271 */ 1272 if (*q == 'c') { 1273 warnx("Assuming 'g' for 'c' in flags for line:\n%s", 1274 errline); 1275 warnx("The 'c' flag will eventually mean 'CREATE'"); 1276 working->flags |= CE_GLOB; 1277 break; 1278 } 1279 working->flags |= CE_CREATE; 1280 break; 1281 case 'd': 1282 working->flags |= CE_NODUMP; 1283 break; 1284 case 'g': 1285 working->flags |= CE_GLOB; 1286 break; 1287 case 'j': 1288 working->compress = COMPRESS_BZIP2; 1289 break; 1290 case 'n': 1291 working->flags |= CE_NOSIGNAL; 1292 break; 1293 case 'u': 1294 working->flags |= CE_SIGNALGROUP; 1295 break; 1296 case 'w': 1297 /* Depreciated flag - keep for compatibility purposes */ 1298 break; 1299 case 'x': 1300 working->compress = COMPRESS_XZ; 1301 break; 1302 case 'z': 1303 working->compress = COMPRESS_GZIP; 1304 break; 1305 case '-': 1306 break; 1307 case 'f': /* Used by OpenBSD for "CE_FOLLOW" */ 1308 case 'm': /* Used by OpenBSD for "CE_MONITOR" */ 1309 case 'p': /* Used by NetBSD for "CE_PLAIN0" */ 1310 default: 1311 errx(1, "illegal flag in config file -- %c", 1312 *q); 1313 } 1314 } 1315 1316 if (eol) 1317 q = NULL; 1318 else { 1319 q = parse = sob(++parse); /* Optional field */ 1320 parse = son(parse); 1321 if (!*parse) 1322 eol = 1; 1323 *parse = '\0'; 1324 } 1325 1326 working->pid_file = NULL; 1327 if (q && *q) { 1328 if (*q == '/') 1329 working->pid_file = strdup(q); 1330 else if (isdigit(*q)) 1331 goto got_sig; 1332 else 1333 errx(1, 1334 "illegal pid file or signal number in config file:\n%s", 1335 errline); 1336 } 1337 if (eol) 1338 q = NULL; 1339 else { 1340 q = parse = sob(++parse); /* Optional field */ 1341 *(parse = son(parse)) = '\0'; 1342 } 1343 1344 working->sig = SIGHUP; 1345 if (q && *q) { 1346 if (isdigit(*q)) { 1347 got_sig: 1348 working->sig = atoi(q); 1349 } else { 1350 err_sig: 1351 errx(1, 1352 "illegal signal number in config file:\n%s", 1353 errline); 1354 } 1355 if (working->sig < 1 || working->sig >= NSIG) 1356 goto err_sig; 1357 } 1358 1359 /* 1360 * Finish figuring out what pid-file to use (if any) in 1361 * later processing if this logfile needs to be rotated. 1362 */ 1363 if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) { 1364 /* 1365 * This config-entry specified 'n' for nosignal, 1366 * see if it also specified an explicit pid_file. 1367 * This would be a pretty pointless combination. 1368 */ 1369 if (working->pid_file != NULL) { 1370 warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s", 1371 working->pid_file, errline); 1372 free(working->pid_file); 1373 working->pid_file = NULL; 1374 } 1375 } else if (working->pid_file == NULL) { 1376 /* 1377 * This entry did not specify the 'n' flag, which 1378 * means it should signal syslogd unless it had 1379 * specified some other pid-file (and obviously the 1380 * syslog pid-file will not be for a process-group). 1381 * Also, we should only try to notify syslog if we 1382 * are root. 1383 */ 1384 if (working->flags & CE_SIGNALGROUP) { 1385 warnx("Ignoring flag 'U' in line:\n%s", 1386 errline); 1387 working->flags &= ~CE_SIGNALGROUP; 1388 } 1389 if (needroot) 1390 working->pid_file = strdup(path_syslogpid); 1391 } 1392 1393 /* 1394 * Add this entry to the appropriate list of entries, unless 1395 * it was some kind of special entry (eg: <default>). 1396 */ 1397 if (special) { 1398 ; /* Do not add to any list */ 1399 } else if (working->flags & CE_GLOB) { 1400 STAILQ_INSERT_TAIL(glob_p, working, cf_nextp); 1401 } else { 1402 STAILQ_INSERT_TAIL(work_p, working, cf_nextp); 1403 } 1404 } 1405 if (errline != NULL) 1406 free(errline); 1407 } 1408 1409 static char * 1410 missing_field(char *p, char *errline) 1411 { 1412 1413 if (!p || !*p) 1414 errx(1, "missing field in config file:\n%s", errline); 1415 return (p); 1416 } 1417 1418 /* 1419 * In our sort we return it in the reverse of what qsort normally 1420 * would do, as we want the newest files first. If we have two 1421 * entries with the same time we don't really care about order. 1422 * 1423 * Support function for qsort() in delete_oldest_timelog(). 1424 */ 1425 static int 1426 oldlog_entry_compare(const void *a, const void *b) 1427 { 1428 const struct oldlog_entry *ola = a, *olb = b; 1429 1430 if (ola->t > olb->t) 1431 return (-1); 1432 else if (ola->t < olb->t) 1433 return (1); 1434 else 1435 return (0); 1436 } 1437 1438 /* 1439 * Delete the oldest logfiles, when using time based filenames. 1440 */ 1441 static void 1442 delete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir) 1443 { 1444 char *logfname, *s, *dir, errbuf[80]; 1445 int dirfd, i, logcnt, max_logcnt, valid; 1446 struct oldlog_entry *oldlogs; 1447 size_t logfname_len; 1448 struct dirent *dp; 1449 const char *cdir; 1450 struct tm tm; 1451 DIR *dirp; 1452 1453 oldlogs = malloc(MAX_OLDLOGS * sizeof(struct oldlog_entry)); 1454 max_logcnt = MAX_OLDLOGS; 1455 logcnt = 0; 1456 1457 if (archive_dir != NULL && archive_dir[0] != '\0') 1458 cdir = archive_dir; 1459 else 1460 if ((cdir = dirname(ent->log)) == NULL) 1461 err(1, "dirname()"); 1462 if ((dir = strdup(cdir)) == NULL) 1463 err(1, "strdup()"); 1464 1465 if ((s = basename(ent->log)) == NULL) 1466 err(1, "basename()"); 1467 if ((logfname = strdup(s)) == NULL) 1468 err(1, "strdup()"); 1469 logfname_len = strlen(logfname); 1470 if (strcmp(logfname, "/") == 0) 1471 errx(1, "Invalid log filename - became '/'"); 1472 1473 if (verbose > 2) 1474 printf("Searching for old logs in %s\n", dir); 1475 1476 /* First we create a 'list' of all archived logfiles */ 1477 if ((dirp = opendir(dir)) == NULL) 1478 err(1, "Cannot open log directory '%s'", dir); 1479 dirfd = dirfd(dirp); 1480 while ((dp = readdir(dirp)) != NULL) { 1481 if (dp->d_type != DT_REG) 1482 continue; 1483 1484 /* Ignore everything but files with our logfile prefix */ 1485 if (strncmp(dp->d_name, logfname, logfname_len) != 0) 1486 continue; 1487 /* Ignore the actual non-rotated logfile */ 1488 if (dp->d_namlen == logfname_len) 1489 continue; 1490 /* 1491 * Make sure we created have found a logfile, so the 1492 * postfix is valid, IE format is: '.<time>(.[bg]z)?'. 1493 */ 1494 if (dp->d_name[logfname_len] != '.') { 1495 if (verbose) 1496 printf("Ignoring %s which has unexpected " 1497 "extension '%s'\n", dp->d_name, 1498 &dp->d_name[logfname_len]); 1499 continue; 1500 } 1501 if ((s = strptime(&dp->d_name[logfname_len + 1], 1502 timefnamefmt, &tm)) == NULL) { 1503 /* 1504 * We could special case "old" sequentially 1505 * named logfiles here, but we do not as that 1506 * would require special handling to decide 1507 * which one was the oldest compared to "new" 1508 * time based logfiles. 1509 */ 1510 if (verbose) 1511 printf("Ignoring %s which does not " 1512 "match time format\n", dp->d_name); 1513 continue; 1514 } 1515 1516 for (int c = 0; c < COMPRESS_TYPES; c++) 1517 if (strcmp(s, compress_type[c].suffix) == 0) 1518 valid = 1; 1519 if (valid != 1) { 1520 if (verbose) 1521 printf("Ignoring %s which has unexpected " 1522 "extension '%s'\n", dp->d_name, s); 1523 continue; 1524 } 1525 1526 /* 1527 * We should now have old an old rotated logfile, so 1528 * add it to the 'list'. 1529 */ 1530 if ((oldlogs[logcnt].t = timegm(&tm)) == -1) 1531 err(1, "Could not convert time string to time value"); 1532 if ((oldlogs[logcnt].fname = strdup(dp->d_name)) == NULL) 1533 err(1, "strdup()"); 1534 logcnt++; 1535 1536 /* 1537 * It is very unlikely we ever run out of space in the 1538 * logfile array from the default size, but lets 1539 * handle it anyway... 1540 */ 1541 if (logcnt >= max_logcnt) { 1542 max_logcnt *= 4; 1543 /* Detect integer overflow */ 1544 if (max_logcnt < logcnt) 1545 errx(1, "Too many old logfiles found"); 1546 oldlogs = realloc(oldlogs, 1547 max_logcnt * sizeof(struct oldlog_entry)); 1548 if (oldlogs == NULL) 1549 err(1, "realloc()"); 1550 } 1551 } 1552 1553 /* Second, if needed we delete oldest archived logfiles */ 1554 if (logcnt > 0 && logcnt >= ent->numlogs && ent->numlogs > 1) { 1555 oldlogs = realloc(oldlogs, logcnt * 1556 sizeof(struct oldlog_entry)); 1557 if (oldlogs == NULL) 1558 err(1, "realloc()"); 1559 1560 /* 1561 * We now sort the logs in the order of newest to 1562 * oldest. That way we can simply skip over the 1563 * number of records we want to keep. 1564 */ 1565 qsort(oldlogs, logcnt, sizeof(struct oldlog_entry), 1566 oldlog_entry_compare); 1567 for (i = ent->numlogs - 1; i < logcnt; i++) { 1568 if (noaction) 1569 printf("\trm -f %s/%s\n", dir, 1570 oldlogs[i].fname); 1571 else if (unlinkat(dirfd, oldlogs[i].fname, 0) != 0) { 1572 snprintf(errbuf, sizeof(errbuf), 1573 "Could not delete old logfile '%s'", 1574 oldlogs[i].fname); 1575 perror(errbuf); 1576 } 1577 } 1578 } else if (verbose > 1) 1579 printf("No old logs to delete for logfile %s\n", ent->log); 1580 1581 /* Third, cleanup */ 1582 closedir(dirp); 1583 for (i = 0; i < logcnt; i++) { 1584 assert(oldlogs[i].fname != NULL); 1585 free(oldlogs[i].fname); 1586 } 1587 free(oldlogs); 1588 free(logfname); 1589 free(dir); 1590 } 1591 1592 /* 1593 * Only add to the queue if the file hasn't already been added. This is 1594 * done to prevent circular include loops. 1595 */ 1596 static void 1597 add_to_queue(const char *fname, struct ilist *inclist) 1598 { 1599 struct include_entry *inc; 1600 1601 STAILQ_FOREACH(inc, inclist, inc_nextp) { 1602 if (strcmp(fname, inc->file) == 0) { 1603 warnx("duplicate include detected: %s", fname); 1604 return; 1605 } 1606 } 1607 1608 inc = malloc(sizeof(struct include_entry)); 1609 if (inc == NULL) 1610 err(1, "malloc of inc"); 1611 inc->file = strdup(fname); 1612 1613 if (verbose > 2) 1614 printf("\t+ Adding %s to the processing queue.\n", fname); 1615 1616 STAILQ_INSERT_TAIL(inclist, inc, inc_nextp); 1617 } 1618 1619 /* 1620 * Search for logfile and return its compression suffix (if supported) 1621 * The suffix detection is first-match in the order of compress_types 1622 * 1623 * Note: if logfile without suffix exists (uncompressed, COMPRESS_NONE) 1624 * a zero-length string is returned 1625 */ 1626 static const char * 1627 get_logfile_suffix(const char *logfile) 1628 { 1629 struct stat st; 1630 char zfile[MAXPATHLEN]; 1631 1632 for (int c = 0; c < COMPRESS_TYPES; c++) { 1633 strlcpy(zfile, logfile, MAXPATHLEN); 1634 strlcat(zfile, compress_type[c].suffix, MAXPATHLEN); 1635 if (lstat(zfile, &st) == 0) 1636 return (compress_type[c].suffix); 1637 } 1638 return (NULL); 1639 } 1640 1641 static fk_entry 1642 do_rotate(const struct conf_entry *ent) 1643 { 1644 char dirpart[MAXPATHLEN], namepart[MAXPATHLEN]; 1645 char file1[MAXPATHLEN], file2[MAXPATHLEN]; 1646 char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN]; 1647 const char *logfile_suffix; 1648 char datetimestr[30]; 1649 int flags, numlogs_c; 1650 fk_entry free_or_keep; 1651 struct sigwork_entry *swork; 1652 struct stat st; 1653 struct tm tm; 1654 time_t now; 1655 1656 flags = ent->flags; 1657 free_or_keep = FREE_ENT; 1658 1659 if (archtodir) { 1660 char *p; 1661 1662 /* build complete name of archive directory into dirpart */ 1663 if (*archdirname == '/') { /* absolute */ 1664 strlcpy(dirpart, archdirname, sizeof(dirpart)); 1665 } else { /* relative */ 1666 /* get directory part of logfile */ 1667 strlcpy(dirpart, ent->log, sizeof(dirpart)); 1668 if ((p = strrchr(dirpart, '/')) == NULL) 1669 dirpart[0] = '\0'; 1670 else 1671 *(p + 1) = '\0'; 1672 strlcat(dirpart, archdirname, sizeof(dirpart)); 1673 } 1674 1675 /* check if archive directory exists, if not, create it */ 1676 if (lstat(dirpart, &st)) 1677 createdir(ent, dirpart); 1678 1679 /* get filename part of logfile */ 1680 if ((p = strrchr(ent->log, '/')) == NULL) 1681 strlcpy(namepart, ent->log, sizeof(namepart)); 1682 else 1683 strlcpy(namepart, p + 1, sizeof(namepart)); 1684 1685 /* name of oldest log */ 1686 snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart, 1687 namepart, ent->numlogs); 1688 } else { 1689 /* 1690 * Tell delete_oldest_timelog() we are not using an 1691 * archive dir. 1692 */ 1693 dirpart[0] = '\0'; 1694 1695 /* name of oldest log */ 1696 snprintf(file1, sizeof(file1), "%s.%d", ent->log, 1697 ent->numlogs); 1698 } 1699 1700 /* Delete old logs */ 1701 if (timefnamefmt != NULL) 1702 delete_oldest_timelog(ent, dirpart); 1703 else { 1704 /* name of oldest log */ 1705 for (int c = 0; c < COMPRESS_TYPES; c++) { 1706 snprintf(zfile1, sizeof(zfile1), "%s%s", file1, 1707 compress_type[c].suffix); 1708 if (noaction) 1709 printf("\trm -f %s\n", zfile1); 1710 else 1711 unlink(zfile1); 1712 } 1713 } 1714 1715 if (timefnamefmt != NULL) { 1716 /* If time functions fails we can't really do any sensible */ 1717 if (time(&now) == (time_t)-1 || 1718 localtime_r(&now, &tm) == NULL) 1719 bzero(&tm, sizeof(tm)); 1720 1721 strftime(datetimestr, sizeof(datetimestr), timefnamefmt, &tm); 1722 if (archtodir) { 1723 snprintf(file1, sizeof(file1), "%s/%s.%s", 1724 dirpart, namepart, datetimestr); 1725 } else { 1726 snprintf(file1, sizeof(file1), "%s.%s", 1727 ent->log, datetimestr); 1728 } 1729 1730 /* Don't run the code to move down logs */ 1731 numlogs_c = 0; 1732 } else 1733 numlogs_c = ent->numlogs; /* copy for countdown */ 1734 1735 /* Move down log files */ 1736 while (numlogs_c--) { 1737 1738 strlcpy(file2, file1, sizeof(file2)); 1739 1740 if (archtodir) 1741 snprintf(file1, sizeof(file1), "%s/%s.%d", 1742 dirpart, namepart, numlogs_c); 1743 else 1744 snprintf(file1, sizeof(file1), "%s.%d", 1745 ent->log, numlogs_c); 1746 1747 logfile_suffix = get_logfile_suffix(file1); 1748 if (logfile_suffix == NULL) 1749 continue; 1750 strlcpy(zfile1, file1, MAXPATHLEN); 1751 strlcpy(zfile2, file2, MAXPATHLEN); 1752 strlcat(zfile1, logfile_suffix, MAXPATHLEN); 1753 strlcat(zfile2, logfile_suffix, MAXPATHLEN); 1754 1755 if (noaction) 1756 printf("\tmv %s %s\n", zfile1, zfile2); 1757 else { 1758 /* XXX - Ought to be checking for failure! */ 1759 rename(zfile1, zfile2); 1760 } 1761 change_attrs(zfile2, ent); 1762 } 1763 1764 if (ent->numlogs > 0) { 1765 if (noaction) { 1766 /* 1767 * Note that savelog() may succeed with using link() 1768 * for the archtodir case, but there is no good way 1769 * of knowing if it will when doing "noaction", so 1770 * here we claim that it will have to do a copy... 1771 */ 1772 if (archtodir) 1773 printf("\tcp %s %s\n", ent->log, file1); 1774 else 1775 printf("\tln %s %s\n", ent->log, file1); 1776 } else { 1777 if (!(flags & CE_BINARY)) { 1778 /* Report the trimming to the old log */ 1779 log_trim(ent->log, ent); 1780 } 1781 savelog(ent->log, file1); 1782 } 1783 change_attrs(file1, ent); 1784 } 1785 1786 /* Create the new log file and move it into place */ 1787 if (noaction) 1788 printf("Start new log...\n"); 1789 createlog(ent); 1790 1791 /* 1792 * Save all signalling and file-compression to be done after log 1793 * files from all entries have been rotated. This way any one 1794 * process will not be sent the same signal multiple times when 1795 * multiple log files had to be rotated. 1796 */ 1797 swork = NULL; 1798 if (ent->pid_file != NULL) 1799 swork = save_sigwork(ent); 1800 if (ent->numlogs > 0 && ent->compress > COMPRESS_NONE) { 1801 /* 1802 * The zipwork_entry will include a pointer to this 1803 * conf_entry, so the conf_entry should not be freed. 1804 */ 1805 free_or_keep = KEEP_ENT; 1806 save_zipwork(ent, swork, ent->fsize, file1); 1807 } 1808 1809 return (free_or_keep); 1810 } 1811 1812 static void 1813 do_sigwork(struct sigwork_entry *swork) 1814 { 1815 struct sigwork_entry *nextsig; 1816 int kres, secs; 1817 1818 if (!(swork->sw_pidok) || swork->sw_pid == 0) 1819 return; /* no work to do... */ 1820 1821 /* 1822 * If nosignal (-s) was specified, then do not signal any process. 1823 * Note that a nosignal request triggers a warning message if the 1824 * rotated logfile needs to be compressed, *unless* -R was also 1825 * specified. We assume that an `-sR' request came from a process 1826 * which writes to the logfile, and as such, we assume that process 1827 * has already made sure the logfile is not presently in use. This 1828 * just sets swork->sw_pidok to a special value, and do_zipwork 1829 * will print any necessary warning(s). 1830 */ 1831 if (nosignal) { 1832 if (!rotatereq) 1833 swork->sw_pidok = -1; 1834 return; 1835 } 1836 1837 /* 1838 * Compute the pause between consecutive signals. Use a longer 1839 * sleep time if we will be sending two signals to the same 1840 * deamon or process-group. 1841 */ 1842 secs = 0; 1843 nextsig = SLIST_NEXT(swork, sw_nextp); 1844 if (nextsig != NULL) { 1845 if (swork->sw_pid == nextsig->sw_pid) 1846 secs = 10; 1847 else 1848 secs = 1; 1849 } 1850 1851 if (noaction) { 1852 printf("\tkill -%d %d \t\t# %s\n", swork->sw_signum, 1853 (int)swork->sw_pid, swork->sw_fname); 1854 if (secs > 0) 1855 printf("\tsleep %d\n", secs); 1856 return; 1857 } 1858 1859 kres = kill(swork->sw_pid, swork->sw_signum); 1860 if (kres != 0) { 1861 /* 1862 * Assume that "no such process" (ESRCH) is something 1863 * to warn about, but is not an error. Presumably the 1864 * process which writes to the rotated log file(s) is 1865 * gone, in which case we should have no problem with 1866 * compressing the rotated log file(s). 1867 */ 1868 if (errno != ESRCH) 1869 swork->sw_pidok = 0; 1870 warn("can't notify %s, pid %d", swork->sw_pidtype, 1871 (int)swork->sw_pid); 1872 } else { 1873 if (verbose) 1874 printf("Notified %s pid %d = %s\n", swork->sw_pidtype, 1875 (int)swork->sw_pid, swork->sw_fname); 1876 if (secs > 0) { 1877 if (verbose) 1878 printf("Pause %d second(s) between signals\n", 1879 secs); 1880 sleep(secs); 1881 } 1882 } 1883 } 1884 1885 static void 1886 do_zipwork(struct zipwork_entry *zwork) 1887 { 1888 const char *pgm_name, *pgm_path; 1889 int errsav, fcount, zstatus; 1890 pid_t pidzip, wpid; 1891 char zresult[MAXPATHLEN]; 1892 1893 assert(zwork != NULL); 1894 pgm_path = NULL; 1895 strlcpy(zresult, zwork->zw_fname, sizeof(zresult)); 1896 if (zwork->zw_conf != NULL && 1897 zwork->zw_conf->compress > COMPRESS_NONE) 1898 for (int c = 1; c < COMPRESS_TYPES; c++) { 1899 if (zwork->zw_conf->compress == c) { 1900 pgm_path = compress_type[c].path; 1901 strlcat(zresult, 1902 compress_type[c].suffix, sizeof(zresult)); 1903 break; 1904 } 1905 } 1906 if (pgm_path == NULL) { 1907 warnx("invalid entry for %s in do_zipwork", zwork->zw_fname); 1908 return; 1909 } 1910 pgm_name = strrchr(pgm_path, '/'); 1911 if (pgm_name == NULL) 1912 pgm_name = pgm_path; 1913 else 1914 pgm_name++; 1915 1916 if (zwork->zw_swork != NULL && zwork->zw_swork->sw_pidok <= 0) { 1917 warnx( 1918 "log %s not compressed because daemon(s) not notified", 1919 zwork->zw_fname); 1920 change_attrs(zwork->zw_fname, zwork->zw_conf); 1921 return; 1922 } 1923 1924 if (noaction) { 1925 printf("\t%s %s\n", pgm_name, zwork->zw_fname); 1926 change_attrs(zresult, zwork->zw_conf); 1927 return; 1928 } 1929 1930 fcount = 1; 1931 pidzip = fork(); 1932 while (pidzip < 0) { 1933 /* 1934 * The fork failed. If the failure was due to a temporary 1935 * problem, then wait a short time and try it again. 1936 */ 1937 errsav = errno; 1938 warn("fork() for `%s %s'", pgm_name, zwork->zw_fname); 1939 if (errsav != EAGAIN || fcount > 5) 1940 errx(1, "Exiting..."); 1941 sleep(fcount * 12); 1942 fcount++; 1943 pidzip = fork(); 1944 } 1945 if (!pidzip) { 1946 /* The child process executes the compression command */ 1947 execl(pgm_path, pgm_path, "-f", zwork->zw_fname, NULL); 1948 err(1, "execl(`%s -f %s')", pgm_path, zwork->zw_fname); 1949 } 1950 1951 wpid = waitpid(pidzip, &zstatus, 0); 1952 if (wpid == -1) { 1953 /* XXX - should this be a fatal error? */ 1954 warn("%s: waitpid(%d)", pgm_path, pidzip); 1955 return; 1956 } 1957 if (!WIFEXITED(zstatus)) { 1958 warnx("`%s -f %s' did not terminate normally", pgm_name, 1959 zwork->zw_fname); 1960 return; 1961 } 1962 if (WEXITSTATUS(zstatus)) { 1963 warnx("`%s -f %s' terminated with a non-zero status (%d)", 1964 pgm_name, zwork->zw_fname, WEXITSTATUS(zstatus)); 1965 return; 1966 } 1967 1968 /* Compression was successful, set file attributes on the result. */ 1969 change_attrs(zresult, zwork->zw_conf); 1970 } 1971 1972 /* 1973 * Save information on any process we need to signal. Any single 1974 * process may need to be sent different signal-values for different 1975 * log files, but usually a single signal-value will cause the process 1976 * to close and re-open all of it's log files. 1977 */ 1978 static struct sigwork_entry * 1979 save_sigwork(const struct conf_entry *ent) 1980 { 1981 struct sigwork_entry *sprev, *stmp; 1982 int ndiff; 1983 size_t tmpsiz; 1984 1985 sprev = NULL; 1986 ndiff = 1; 1987 SLIST_FOREACH(stmp, &swhead, sw_nextp) { 1988 ndiff = strcmp(ent->pid_file, stmp->sw_fname); 1989 if (ndiff > 0) 1990 break; 1991 if (ndiff == 0) { 1992 if (ent->sig == stmp->sw_signum) 1993 break; 1994 if (ent->sig > stmp->sw_signum) { 1995 ndiff = 1; 1996 break; 1997 } 1998 } 1999 sprev = stmp; 2000 } 2001 if (stmp != NULL && ndiff == 0) 2002 return (stmp); 2003 2004 tmpsiz = sizeof(struct sigwork_entry) + strlen(ent->pid_file) + 1; 2005 stmp = malloc(tmpsiz); 2006 set_swpid(stmp, ent); 2007 stmp->sw_signum = ent->sig; 2008 strcpy(stmp->sw_fname, ent->pid_file); 2009 if (sprev == NULL) 2010 SLIST_INSERT_HEAD(&swhead, stmp, sw_nextp); 2011 else 2012 SLIST_INSERT_AFTER(sprev, stmp, sw_nextp); 2013 return (stmp); 2014 } 2015 2016 /* 2017 * Save information on any file we need to compress. We may see the same 2018 * file multiple times, so check the full list to avoid duplicates. The 2019 * list itself is sorted smallest-to-largest, because that's the order we 2020 * want to compress the files. If the partition is very low on disk space, 2021 * then the smallest files are the most likely to compress, and compressing 2022 * them first will free up more space for the larger files. 2023 */ 2024 static struct zipwork_entry * 2025 save_zipwork(const struct conf_entry *ent, const struct sigwork_entry *swork, 2026 int zsize, const char *zipfname) 2027 { 2028 struct zipwork_entry *zprev, *ztmp; 2029 int ndiff; 2030 size_t tmpsiz; 2031 2032 /* Compute the size if the caller did not know it. */ 2033 if (zsize < 0) 2034 zsize = sizefile(zipfname); 2035 2036 zprev = NULL; 2037 ndiff = 1; 2038 SLIST_FOREACH(ztmp, &zwhead, zw_nextp) { 2039 ndiff = strcmp(zipfname, ztmp->zw_fname); 2040 if (ndiff == 0) 2041 break; 2042 if (zsize > ztmp->zw_fsize) 2043 zprev = ztmp; 2044 } 2045 if (ztmp != NULL && ndiff == 0) 2046 return (ztmp); 2047 2048 tmpsiz = sizeof(struct zipwork_entry) + strlen(zipfname) + 1; 2049 ztmp = malloc(tmpsiz); 2050 ztmp->zw_conf = ent; 2051 ztmp->zw_swork = swork; 2052 ztmp->zw_fsize = zsize; 2053 strcpy(ztmp->zw_fname, zipfname); 2054 if (zprev == NULL) 2055 SLIST_INSERT_HEAD(&zwhead, ztmp, zw_nextp); 2056 else 2057 SLIST_INSERT_AFTER(zprev, ztmp, zw_nextp); 2058 return (ztmp); 2059 } 2060 2061 /* Send a signal to the pid specified by pidfile */ 2062 static void 2063 set_swpid(struct sigwork_entry *swork, const struct conf_entry *ent) 2064 { 2065 FILE *f; 2066 long minok, maxok, rval; 2067 char *endp, *linep, line[BUFSIZ]; 2068 2069 minok = MIN_PID; 2070 maxok = MAX_PID; 2071 swork->sw_pidok = 0; 2072 swork->sw_pid = 0; 2073 swork->sw_pidtype = "daemon"; 2074 if (ent->flags & CE_SIGNALGROUP) { 2075 /* 2076 * If we are expected to signal a process-group when 2077 * rotating this logfile, then the value read in should 2078 * be the negative of a valid process ID. 2079 */ 2080 minok = -MAX_PID; 2081 maxok = -MIN_PID; 2082 swork->sw_pidtype = "process-group"; 2083 } 2084 2085 f = fopen(ent->pid_file, "r"); 2086 if (f == NULL) { 2087 if (errno == ENOENT && enforcepid == 0) { 2088 /* 2089 * Warn if the PID file doesn't exist, but do 2090 * not consider it an error. Most likely it 2091 * means the process has been terminated, 2092 * so it should be safe to rotate any log 2093 * files that the process would have been using. 2094 */ 2095 swork->sw_pidok = 1; 2096 warnx("pid file doesn't exist: %s", ent->pid_file); 2097 } else 2098 warn("can't open pid file: %s", ent->pid_file); 2099 return; 2100 } 2101 2102 if (fgets(line, BUFSIZ, f) == NULL) { 2103 /* 2104 * Warn if the PID file is empty, but do not consider 2105 * it an error. Most likely it means the process has 2106 * has terminated, so it should be safe to rotate any 2107 * log files that the process would have been using. 2108 */ 2109 if (feof(f) && enforcepid == 0) { 2110 swork->sw_pidok = 1; 2111 warnx("pid file is empty: %s", ent->pid_file); 2112 } else 2113 warn("can't read from pid file: %s", ent->pid_file); 2114 fclose(f); 2115 return; 2116 } 2117 fclose(f); 2118 2119 errno = 0; 2120 linep = line; 2121 while (*linep == ' ') 2122 linep++; 2123 rval = strtol(linep, &endp, 10); 2124 if (*endp != '\0' && !isspacech(*endp)) { 2125 warnx("pid file does not start with a valid number: %s", 2126 ent->pid_file); 2127 } else if (rval < minok || rval > maxok) { 2128 warnx("bad value '%ld' for process number in %s", 2129 rval, ent->pid_file); 2130 if (verbose) 2131 warnx("\t(expecting value between %ld and %ld)", 2132 minok, maxok); 2133 } else { 2134 swork->sw_pidok = 1; 2135 swork->sw_pid = rval; 2136 } 2137 2138 return; 2139 } 2140 2141 /* Log the fact that the logs were turned over */ 2142 static int 2143 log_trim(const char *logname, const struct conf_entry *log_ent) 2144 { 2145 FILE *f; 2146 const char *xtra; 2147 2148 if ((f = fopen(logname, "a")) == NULL) 2149 return (-1); 2150 xtra = ""; 2151 if (log_ent->def_cfg) 2152 xtra = " using <default> rule"; 2153 if (log_ent->firstcreate) 2154 fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n", 2155 daytime, hostname, (int) getpid(), xtra); 2156 else if (log_ent->r_reason != NULL) 2157 fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n", 2158 daytime, hostname, (int) getpid(), log_ent->r_reason, xtra); 2159 else 2160 fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n", 2161 daytime, hostname, (int) getpid(), xtra); 2162 if (fclose(f) == EOF) 2163 err(1, "log_trim: fclose"); 2164 return (0); 2165 } 2166 2167 /* Return size in kilobytes of a file */ 2168 static int 2169 sizefile(const char *file) 2170 { 2171 struct stat sb; 2172 2173 if (stat(file, &sb) < 0) 2174 return (-1); 2175 return (kbytes(dbtob(sb.st_blocks))); 2176 } 2177 2178 /* Return the age of old log file (file.0) */ 2179 static int 2180 age_old_log(char *file) 2181 { 2182 struct stat sb; 2183 const char *logfile_suffix; 2184 char tmp[MAXPATHLEN + sizeof(".0") + COMPRESS_SUFFIX_MAXLEN + 1]; 2185 2186 if (archtodir) { 2187 char *p; 2188 2189 /* build name of archive directory into tmp */ 2190 if (*archdirname == '/') { /* absolute */ 2191 strlcpy(tmp, archdirname, sizeof(tmp)); 2192 } else { /* relative */ 2193 /* get directory part of logfile */ 2194 strlcpy(tmp, file, sizeof(tmp)); 2195 if ((p = strrchr(tmp, '/')) == NULL) 2196 tmp[0] = '\0'; 2197 else 2198 *(p + 1) = '\0'; 2199 strlcat(tmp, archdirname, sizeof(tmp)); 2200 } 2201 2202 strlcat(tmp, "/", sizeof(tmp)); 2203 2204 /* get filename part of logfile */ 2205 if ((p = strrchr(file, '/')) == NULL) 2206 strlcat(tmp, file, sizeof(tmp)); 2207 else 2208 strlcat(tmp, p + 1, sizeof(tmp)); 2209 } else { 2210 strlcpy(tmp, file, sizeof(tmp)); 2211 } 2212 2213 strlcat(tmp, ".0", sizeof(tmp)); 2214 logfile_suffix = get_logfile_suffix(tmp); 2215 if (logfile_suffix == NULL) 2216 return (-1); 2217 strlcat(tmp, logfile_suffix, sizeof(tmp)); 2218 if (stat(tmp, &sb) < 0) 2219 return (-1); 2220 return ((int)(ptimeget_secs(timenow) - sb.st_mtime + 1800) / 3600); 2221 } 2222 2223 /* Skip Over Blanks */ 2224 static char * 2225 sob(char *p) 2226 { 2227 while (p && *p && isspace(*p)) 2228 p++; 2229 return (p); 2230 } 2231 2232 /* Skip Over Non-Blanks */ 2233 static char * 2234 son(char *p) 2235 { 2236 while (p && *p && !isspace(*p)) 2237 p++; 2238 return (p); 2239 } 2240 2241 /* Check if string is actually a number */ 2242 static int 2243 isnumberstr(const char *string) 2244 { 2245 while (*string) { 2246 if (!isdigitch(*string++)) 2247 return (0); 2248 } 2249 return (1); 2250 } 2251 2252 /* Check if string contains a glob */ 2253 static int 2254 isglobstr(const char *string) 2255 { 2256 char chr; 2257 2258 while ((chr = *string++)) { 2259 if (chr == '*' || chr == '?' || chr == '[') 2260 return (1); 2261 } 2262 return (0); 2263 } 2264 2265 /* 2266 * Save the active log file under a new name. A link to the new name 2267 * is the quick-and-easy way to do this. If that fails (which it will 2268 * if the destination is on another partition), then make a copy of 2269 * the file to the new location. 2270 */ 2271 static void 2272 savelog(char *from, char *to) 2273 { 2274 FILE *src, *dst; 2275 int c, res; 2276 2277 res = link(from, to); 2278 if (res == 0) 2279 return; 2280 2281 if ((src = fopen(from, "r")) == NULL) 2282 err(1, "can't fopen %s for reading", from); 2283 if ((dst = fopen(to, "w")) == NULL) 2284 err(1, "can't fopen %s for writing", to); 2285 2286 while ((c = getc(src)) != EOF) { 2287 if ((putc(c, dst)) == EOF) 2288 err(1, "error writing to %s", to); 2289 } 2290 2291 if (ferror(src)) 2292 err(1, "error reading from %s", from); 2293 if ((fclose(src)) != 0) 2294 err(1, "can't fclose %s", to); 2295 if ((fclose(dst)) != 0) 2296 err(1, "can't fclose %s", from); 2297 } 2298 2299 /* create one or more directory components of a path */ 2300 static void 2301 createdir(const struct conf_entry *ent, char *dirpart) 2302 { 2303 int res; 2304 char *s, *d; 2305 char mkdirpath[MAXPATHLEN]; 2306 struct stat st; 2307 2308 s = dirpart; 2309 d = mkdirpath; 2310 2311 for (;;) { 2312 *d++ = *s++; 2313 if (*s != '/' && *s != '\0') 2314 continue; 2315 *d = '\0'; 2316 res = lstat(mkdirpath, &st); 2317 if (res != 0) { 2318 if (noaction) { 2319 printf("\tmkdir %s\n", mkdirpath); 2320 } else { 2321 res = mkdir(mkdirpath, 0755); 2322 if (res != 0) 2323 err(1, "Error on mkdir(\"%s\") for -a", 2324 mkdirpath); 2325 } 2326 } 2327 if (*s == '\0') 2328 break; 2329 } 2330 if (verbose) { 2331 if (ent->firstcreate) 2332 printf("Created directory '%s' for new %s\n", 2333 dirpart, ent->log); 2334 else 2335 printf("Created directory '%s' for -a\n", dirpart); 2336 } 2337 } 2338 2339 /* 2340 * Create a new log file, destroying any currently-existing version 2341 * of the log file in the process. If the caller wants a backup copy 2342 * of the file to exist, they should call 'link(logfile,logbackup)' 2343 * before calling this routine. 2344 */ 2345 void 2346 createlog(const struct conf_entry *ent) 2347 { 2348 int fd, failed; 2349 struct stat st; 2350 char *realfile, *slash, tempfile[MAXPATHLEN]; 2351 2352 fd = -1; 2353 realfile = ent->log; 2354 2355 /* 2356 * If this log file is being created for the first time (-C option), 2357 * then it may also be true that the parent directory does not exist 2358 * yet. Check, and create that directory if it is missing. 2359 */ 2360 if (ent->firstcreate) { 2361 strlcpy(tempfile, realfile, sizeof(tempfile)); 2362 slash = strrchr(tempfile, '/'); 2363 if (slash != NULL) { 2364 *slash = '\0'; 2365 failed = stat(tempfile, &st); 2366 if (failed && errno != ENOENT) 2367 err(1, "Error on stat(%s)", tempfile); 2368 if (failed) 2369 createdir(ent, tempfile); 2370 else if (!S_ISDIR(st.st_mode)) 2371 errx(1, "%s exists but is not a directory", 2372 tempfile); 2373 } 2374 } 2375 2376 /* 2377 * First create an unused filename, so it can be chown'ed and 2378 * chmod'ed before it is moved into the real location. mkstemp 2379 * will create the file mode=600 & owned by us. Note that all 2380 * temp files will have a suffix of '.z<something>'. 2381 */ 2382 strlcpy(tempfile, realfile, sizeof(tempfile)); 2383 strlcat(tempfile, ".zXXXXXX", sizeof(tempfile)); 2384 if (noaction) 2385 printf("\tmktemp %s\n", tempfile); 2386 else { 2387 fd = mkstemp(tempfile); 2388 if (fd < 0) 2389 err(1, "can't mkstemp logfile %s", tempfile); 2390 2391 /* 2392 * Add status message to what will become the new log file. 2393 */ 2394 if (!(ent->flags & CE_BINARY)) { 2395 if (log_trim(tempfile, ent)) 2396 err(1, "can't add status message to log"); 2397 } 2398 } 2399 2400 /* Change the owner/group, if we are supposed to */ 2401 if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 2402 if (noaction) 2403 printf("\tchown %u:%u %s\n", ent->uid, ent->gid, 2404 tempfile); 2405 else { 2406 failed = fchown(fd, ent->uid, ent->gid); 2407 if (failed) 2408 err(1, "can't fchown temp file %s", tempfile); 2409 } 2410 } 2411 2412 /* Turn on NODUMP if it was requested in the config-file. */ 2413 if (ent->flags & CE_NODUMP) { 2414 if (noaction) 2415 printf("\tchflags nodump %s\n", tempfile); 2416 else { 2417 failed = fchflags(fd, UF_NODUMP); 2418 if (failed) { 2419 warn("log_trim: fchflags(NODUMP)"); 2420 } 2421 } 2422 } 2423 2424 /* 2425 * Note that if the real logfile still exists, and if the call 2426 * to rename() fails, then "neither the old file nor the new 2427 * file shall be changed or created" (to quote the standard). 2428 * If the call succeeds, then the file will be replaced without 2429 * any window where some other process might find that the file 2430 * did not exist. 2431 * XXX - ? It may be that for some error conditions, we could 2432 * retry by first removing the realfile and then renaming. 2433 */ 2434 if (noaction) { 2435 printf("\tchmod %o %s\n", ent->permissions, tempfile); 2436 printf("\tmv %s %s\n", tempfile, realfile); 2437 } else { 2438 failed = fchmod(fd, ent->permissions); 2439 if (failed) 2440 err(1, "can't fchmod temp file '%s'", tempfile); 2441 failed = rename(tempfile, realfile); 2442 if (failed) 2443 err(1, "can't mv %s to %s", tempfile, realfile); 2444 } 2445 2446 if (fd >= 0) 2447 close(fd); 2448 } 2449 2450 /* 2451 * Change the attributes of a given filename to what was specified in 2452 * the newsyslog.conf entry. This routine is only called for files 2453 * that newsyslog expects that it has created, and thus it is a fatal 2454 * error if this routine finds that the file does not exist. 2455 */ 2456 static void 2457 change_attrs(const char *fname, const struct conf_entry *ent) 2458 { 2459 int failed; 2460 2461 if (noaction) { 2462 printf("\tchmod %o %s\n", ent->permissions, fname); 2463 2464 if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 2465 printf("\tchown %u:%u %s\n", 2466 ent->uid, ent->gid, fname); 2467 2468 if (ent->flags & CE_NODUMP) 2469 printf("\tchflags nodump %s\n", fname); 2470 return; 2471 } 2472 2473 failed = chmod(fname, ent->permissions); 2474 if (failed) { 2475 if (errno != EPERM) 2476 err(1, "chmod(%s) in change_attrs", fname); 2477 warn("change_attrs couldn't chmod(%s)", fname); 2478 } 2479 2480 if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 2481 failed = chown(fname, ent->uid, ent->gid); 2482 if (failed) 2483 warn("can't chown %s", fname); 2484 } 2485 2486 if (ent->flags & CE_NODUMP) { 2487 failed = chflags(fname, UF_NODUMP); 2488 if (failed) 2489 warn("can't chflags %s NODUMP", fname); 2490 } 2491 } 2492