newsyslog.c revision 112003
1/* 2 * This file contains changes from the Open Software Foundation. 3 */ 4 5/* 6 * Copyright 1988, 1989 by the Massachusetts Institute of Technology 7 * 8 * Permission to use, copy, modify, and distribute this software and its 9 * documentation for any purpose and without fee is hereby granted, provided 10 * that the above copyright notice appear in all copies and that both that 11 * copyright notice and this permission notice appear in supporting 12 * documentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be 13 * used in advertising or publicity pertaining to distribution of the 14 * software without specific, written prior permission. M.I.T. and the M.I.T. 15 * S.I.P.B. make no representations about the suitability of this software 16 * for any purpose. It is provided "as is" without express or implied 17 * warranty. 18 * 19 */ 20 21/* 22 * newsyslog - roll over selected logs at the appropriate time, keeping the a 23 * specified number of backup files around. 24 */ 25 26#ifndef lint 27static const char rcsid[] = 28"$FreeBSD: head/usr.sbin/newsyslog/newsyslog.c 112003 2003-03-08 20:07:01Z gad $"; 29#endif /* not lint */ 30 31#define OSF 32#ifndef COMPRESS_POSTFIX 33#define COMPRESS_POSTFIX ".gz" 34#endif 35#ifndef BZCOMPRESS_POSTFIX 36#define BZCOMPRESS_POSTFIX ".bz2" 37#endif 38 39#include <sys/param.h> 40#include <sys/stat.h> 41#include <sys/wait.h> 42 43#include <ctype.h> 44#include <err.h> 45#include <errno.h> 46#include <fcntl.h> 47#include <fnmatch.h> 48#include <glob.h> 49#include <grp.h> 50#include <paths.h> 51#include <pwd.h> 52#include <signal.h> 53#include <stdio.h> 54#include <stdlib.h> 55#include <string.h> 56#include <time.h> 57#include <unistd.h> 58 59#include "pathnames.h" 60 61/* 62 * Bit-values for the 'flags' parsed from a config-file entry. 63 */ 64#define CE_COMPACT 0x0001 /* Compact the achived log files with gzip. */ 65#define CE_BZCOMPACT 0x0002 /* Compact the achived log files with bzip2. */ 66#define CE_COMPACTWAIT 0x0004 /* wait until compressing one file finishes */ 67 /* before starting the next step. */ 68#define CE_BINARY 0x0008 /* Logfile is in binary, do not add status */ 69 /* messages to logfile(s) when rotating. */ 70#define CE_NOSIGNAL 0x0010 /* There is no process to signal when */ 71 /* trimming this file. */ 72#define CE_TRIMAT 0x0020 /* trim file at a specific time. */ 73#define CE_GLOB 0x0040 /* name of the log is file name pattern. */ 74#define CE_SIGNALGROUP 0x0080 /* Signal a process-group instead of a single */ 75 /* process when trimming this file. */ 76 77#define MIN_PID 5 /* Don't touch pids lower than this */ 78#define MAX_PID 99999 /* was lower, see /usr/include/sys/proc.h */ 79 80#define kbytes(size) (((size) + 1023) >> 10) 81 82struct conf_entry { 83 char *log; /* Name of the log */ 84 char *pid_file; /* PID file */ 85 char *r_reason; /* The reason this file is being rotated */ 86 int rotate; /* Non-zero if this file should be rotated */ 87 uid_t uid; /* Owner of log */ 88 gid_t gid; /* Group of log */ 89 int numlogs; /* Number of logs to keep */ 90 int size; /* Size cutoff to trigger trimming the log */ 91 int hours; /* Hours between log trimming */ 92 time_t trim_at; /* Specific time to do trimming */ 93 int permissions; /* File permissions on the log */ 94 int flags; /* CE_COMPACT, CE_BZCOMPACT, CE_BINARY */ 95 int sig; /* Signal to send */ 96 int def_cfg; /* Using the <default> rule for this file */ 97 struct conf_entry *next;/* Linked list pointer */ 98}; 99 100#define DEFAULT_MARKER "<default>" 101 102int archtodir = 0; /* Archive old logfiles to other directory */ 103int verbose = 0; /* Print out what's going on */ 104int needroot = 1; /* Root privs are necessary */ 105int noaction = 0; /* Don't do anything, just show it */ 106int nosignal; /* Do not send any signals */ 107int force = 0; /* Force the trim no matter what */ 108int rotatereq = 0; /* -R = Always rotate the file(s) as given */ 109 /* on the command (this also requires */ 110 /* that a list of files *are* given on */ 111 /* the run command). */ 112char *requestor; /* The name given on a -R request */ 113char *archdirname; /* Directory path to old logfiles archive */ 114const char *conf; /* Configuration file to use */ 115time_t timenow; 116 117char hostname[MAXHOSTNAMELEN]; /* hostname */ 118char daytime[16]; /* timenow in human readable form */ 119 120static struct conf_entry *get_worklist(char **files); 121static void parse_file(FILE *cf, const char *cfname, struct conf_entry **work_p, 122 struct conf_entry **defconf_p); 123static char *sob(char *p); 124static char *son(char *p); 125static char *missing_field(char *p, char *errline); 126static void do_entry(struct conf_entry * ent); 127static void free_entry(struct conf_entry *ent); 128static struct conf_entry *init_entry(const char *fname, 129 struct conf_entry *src_entry); 130static void parse_args(int argc, char **argv); 131static void usage(void); 132static void dotrim(const struct conf_entry *ent, char *log, 133 int numdays, int flags); 134static int log_trim(const char *log, const struct conf_entry *log_ent); 135static void compress_log(char *log, int dowait); 136static void bzcompress_log(char *log, int dowait); 137static int sizefile(char *file); 138static int age_old_log(char *file); 139static int send_signal(const struct conf_entry *ent); 140static time_t parse8601(char *s, char *errline); 141static void movefile(char *from, char *to, int perm, uid_t owner_uid, 142 gid_t group_gid); 143static void createdir(char *dirpart); 144static time_t parseDWM(char *s, char *errline); 145 146/* 147 * All the following are defined to work on an 'int', in the 148 * range 0 to 255, plus EOF. Define wrappers which can take 149 * values of type 'char', either signed or unsigned. 150 */ 151#define isprintch(Anychar) isprint(((int) Anychar) & 255) 152#define isspacech(Anychar) isspace(((int) Anychar) & 255) 153#define tolowerch(Anychar) tolower(((int) Anychar) & 255) 154 155int 156main(int argc, char **argv) 157{ 158 struct conf_entry *p, *q; 159 char *savglob; 160 glob_t pglob; 161 int i; 162 163 parse_args(argc, argv); 164 argc -= optind; 165 argv += optind; 166 167 if (needroot && getuid() && geteuid()) 168 errx(1, "must have root privs"); 169 p = q = get_worklist(argv); 170 171 while (p) { 172 if ((p->flags & CE_GLOB) == 0) { 173 do_entry(p); 174 } else { 175 if (verbose > 2) 176 printf("\t+ Processing pattern %s\n", p->log); 177 if (glob(p->log, GLOB_NOCHECK, NULL, &pglob) != 0) { 178 warn("can't expand pattern: %s", p->log); 179 } else { 180 savglob = p->log; 181 for (i = 0; i < pglob.gl_matchc; i++) { 182 p->log = pglob.gl_pathv[i]; 183 do_entry(p); 184 } 185 globfree(&pglob); 186 p->log = savglob; 187 if (verbose > 2) 188 printf("\t+ Done with pattern\n"); 189 } 190 } 191 p = p->next; 192 free_entry(q); 193 q = p; 194 } 195 while (wait(NULL) > 0 || errno == EINTR) 196 ; 197 return (0); 198} 199 200static struct conf_entry * 201init_entry(const char *fname, struct conf_entry *src_entry) 202{ 203 struct conf_entry *tempwork; 204 205 if (verbose > 4) 206 printf("\t--> [creating entry for %s]\n", fname); 207 208 tempwork = malloc(sizeof(struct conf_entry)); 209 if (tempwork == NULL) 210 err(1, "malloc of conf_entry for %s", fname); 211 212 tempwork->log = strdup(fname); 213 if (tempwork->log == NULL) 214 err(1, "strdup for %s", fname); 215 216 if (src_entry != NULL) { 217 tempwork->pid_file = NULL; 218 if (src_entry->pid_file) 219 tempwork->pid_file = strdup(src_entry->pid_file); 220 tempwork->r_reason = NULL; 221 tempwork->rotate = 0; 222 tempwork->uid = src_entry->uid; 223 tempwork->gid = src_entry->gid; 224 tempwork->numlogs = src_entry->numlogs; 225 tempwork->size = src_entry->size; 226 tempwork->hours = src_entry->hours; 227 tempwork->trim_at = src_entry->trim_at; 228 tempwork->permissions = src_entry->permissions; 229 tempwork->flags = src_entry->flags; 230 tempwork->sig = src_entry->sig; 231 tempwork->def_cfg = src_entry->def_cfg; 232 } else { 233 /* Initialize as a "do-nothing" entry */ 234 tempwork->pid_file = NULL; 235 tempwork->r_reason = NULL; 236 tempwork->rotate = 0; 237 tempwork->uid = (uid_t)-1; 238 tempwork->gid = (gid_t)-1; 239 tempwork->numlogs = 1; 240 tempwork->size = -1; 241 tempwork->hours = -1; 242 tempwork->trim_at = (time_t)0; 243 tempwork->permissions = 0; 244 tempwork->flags = 0; 245 tempwork->sig = SIGHUP; 246 tempwork->def_cfg = 0; 247 } 248 tempwork->next = NULL; 249 250 return (tempwork); 251} 252 253static void 254free_entry(struct conf_entry *ent) 255{ 256 257 if (ent == NULL) 258 return; 259 260 if (ent->log != NULL) { 261 if (verbose > 4) 262 printf("\t--> [freeing entry for %s]\n", ent->log); 263 free(ent->log); 264 ent->log = NULL; 265 } 266 267 if (ent->pid_file != NULL) { 268 free(ent->pid_file); 269 ent->pid_file = NULL; 270 } 271 272 if (ent->r_reason != NULL) { 273 free(ent->r_reason); 274 ent->r_reason = NULL; 275 } 276 277 free(ent); 278} 279 280static void 281do_entry(struct conf_entry * ent) 282{ 283#define REASON_MAX 80 284 int size, modtime; 285 char temp_reason[REASON_MAX]; 286 287 if (verbose) { 288 if (ent->flags & CE_COMPACT) 289 printf("%s <%dZ>: ", ent->log, ent->numlogs); 290 else if (ent->flags & CE_BZCOMPACT) 291 printf("%s <%dJ>: ", ent->log, ent->numlogs); 292 else 293 printf("%s <%d>: ", ent->log, ent->numlogs); 294 } 295 size = sizefile(ent->log); 296 modtime = age_old_log(ent->log); 297 ent->rotate = 0; 298 if (size < 0) { 299 if (verbose) 300 printf("does not exist.\n"); 301 } else { 302 if (ent->flags & CE_TRIMAT && !force && !rotatereq) { 303 if (timenow < ent->trim_at 304 || difftime(timenow, ent->trim_at) >= 60 * 60) { 305 if (verbose) 306 printf("--> will trim at %s", 307 ctime(&ent->trim_at)); 308 return; 309 } else if (verbose && ent->hours <= 0) { 310 printf("--> time is up\n"); 311 } 312 } 313 if (verbose && (ent->size > 0)) 314 printf("size (Kb): %d [%d] ", size, ent->size); 315 if (verbose && (ent->hours > 0)) 316 printf(" age (hr): %d [%d] ", modtime, ent->hours); 317 318 /* 319 * Figure out if this logfile needs to be rotated. 320 */ 321 temp_reason[0] = '\0'; 322 if (rotatereq) { 323 ent->rotate = 1; 324 snprintf(temp_reason, REASON_MAX, " due to -R from %s", 325 requestor); 326 } else if (force) { 327 ent->rotate = 1; 328 snprintf(temp_reason, REASON_MAX, " due to -F request"); 329 } else if ((ent->size > 0) && (size >= ent->size)) { 330 ent->rotate = 1; 331 snprintf(temp_reason, REASON_MAX, " due to size>%dK", 332 ent->size); 333 } else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) { 334 ent->rotate = 1; 335 } else if ((ent->hours > 0) && ((modtime >= ent->hours) || 336 (modtime < 0))) { 337 ent->rotate = 1; 338 } 339 340 /* 341 * If the file needs to be rotated, then rotate it. 342 */ 343 if (ent->rotate) { 344 if (temp_reason[0] != '\0') 345 ent->r_reason = strdup(temp_reason); 346 if (verbose) 347 printf("--> trimming log....\n"); 348 if (noaction && !verbose) { 349 if (ent->flags & CE_COMPACT) 350 printf("%s <%dZ>: trimming\n", 351 ent->log, ent->numlogs); 352 else if (ent->flags & CE_BZCOMPACT) 353 printf("%s <%dJ>: trimming\n", 354 ent->log, ent->numlogs); 355 else 356 printf("%s <%d>: trimming\n", 357 ent->log, ent->numlogs); 358 } 359 dotrim(ent, ent->log, ent->numlogs, ent->flags); 360 } else { 361 if (verbose) 362 printf("--> skipping\n"); 363 } 364 } 365#undef REASON_MAX 366} 367 368/* Send a signal to the pid specified by pidfile */ 369static int 370send_signal(const struct conf_entry *ent) 371{ 372 pid_t target_pid; 373 int did_notify; 374 FILE *f; 375 long minok, maxok, rval; 376 const char *target_name; 377 char *endp, *linep, line[BUFSIZ]; 378 379 did_notify = 0; 380 f = fopen(ent->pid_file, "r"); 381 if (f == NULL) { 382 warn("can't open pid file: %s", ent->pid_file); 383 return (did_notify); 384 /* NOTREACHED */ 385 } 386 387 if (fgets(line, BUFSIZ, f) == NULL) { 388 /* 389 * XXX - If the pid file is empty, is that really a 390 * problem? Wouldn't that mean that the process 391 * has shut down? In that case there would be no 392 * problem with compressing the rotated log file. 393 */ 394 if (feof(f)) 395 warnx("pid file is empty: %s", ent->pid_file); 396 else 397 warn("can't read from pid file: %s", ent->pid_file); 398 (void) fclose(f); 399 return (did_notify); 400 /* NOTREACHED */ 401 } 402 (void) fclose(f); 403 404 target_name = "daemon"; 405 minok = MIN_PID; 406 maxok = MAX_PID; 407 if (ent->flags & CE_SIGNALGROUP) { 408 /* 409 * If we are expected to signal a process-group when 410 * rotating this logfile, then the value read in should 411 * be the negative of a valid process ID. 412 */ 413 target_name = "process-group"; 414 minok = -MAX_PID; 415 maxok = -MIN_PID; 416 } 417 418 errno = 0; 419 linep = line; 420 while (*linep == ' ') 421 linep++; 422 rval = strtol(linep, &endp, 10); 423 if (*endp != '\0' && !isspacech(*endp)) { 424 warnx("pid file does not start with a valid number: %s", 425 ent->pid_file); 426 rval = 0; 427 } else if (rval < minok || rval > maxok) { 428 warnx("bad value '%ld' for process number in %s", 429 rval, ent->pid_file); 430 if (verbose) 431 warnx("\t(expecting value between %ld and %ld)", 432 minok, maxok); 433 rval = 0; 434 } 435 if (rval == 0) { 436 return (did_notify); 437 /* NOTREACHED */ 438 } 439 440 target_pid = rval; 441 442 if (noaction) { 443 did_notify = 1; 444 printf("\tkill -%d %d\n", ent->sig, (int) target_pid); 445 } else if (kill(target_pid, ent->sig)) { 446 /* 447 * XXX - Iff the error was "no such process", should that 448 * really be an error for us? Perhaps the process 449 * is already gone, in which case there would be no 450 * problem with compressing the rotated log file. 451 */ 452 warn("can't notify %s, pid %d", target_name, 453 (int) target_pid); 454 } else { 455 did_notify = 1; 456 if (verbose) 457 printf("%s pid %d notified\n", target_name, 458 (int) target_pid); 459 } 460 461 return (did_notify); 462} 463 464static void 465parse_args(int argc, char **argv) 466{ 467 int ch; 468 char *p; 469 470 timenow = time(NULL); 471 (void)strncpy(daytime, ctime(&timenow) + 4, 15); 472 daytime[15] = '\0'; 473 474 /* Let's get our hostname */ 475 (void)gethostname(hostname, sizeof(hostname)); 476 477 /* Truncate domain */ 478 if ((p = strchr(hostname, '.')) != NULL) 479 *p = '\0'; 480 481 /* Parse command line options. */ 482 while ((ch = getopt(argc, argv, "a:f:nrsvFR:")) != -1) 483 switch (ch) { 484 case 'a': 485 archtodir++; 486 archdirname = optarg; 487 break; 488 case 'f': 489 conf = optarg; 490 break; 491 case 'n': 492 noaction++; 493 break; 494 case 'r': 495 needroot = 0; 496 break; 497 case 's': 498 nosignal = 1; 499 break; 500 case 'v': 501 verbose++; 502 break; 503 case 'F': 504 force++; 505 break; 506 case 'R': 507 rotatereq++; 508 requestor = strdup(optarg); 509 break; 510 case 'm': /* Used by OpenBSD for "monitor mode" */ 511 default: 512 usage(); 513 /* NOTREACHED */ 514 } 515 516 if (rotatereq) { 517 if (optind == argc) { 518 warnx("At least one filename must be given when -R is specified."); 519 usage(); 520 /* NOTREACHED */ 521 } 522 /* Make sure "requestor" value is safe for a syslog message. */ 523 for (p = requestor; *p != '\0'; p++) { 524 if (!isprintch(*p) && (*p != '\t')) 525 *p = '.'; 526 } 527 } 528} 529 530static void 531usage(void) 532{ 533 534 fprintf(stderr, 535 "usage: newsyslog [-Fnrsv] [-a directory] [-f config-file]\n" 536 " [ [-R requestor] filename ... ]\n"); 537 exit(1); 538} 539 540/* 541 * Parse a configuration file and return a linked list of all the logs 542 * which should be processed. 543 */ 544static struct conf_entry * 545get_worklist(char **files) 546{ 547 FILE *f; 548 const char *fname; 549 char **given; 550 struct conf_entry *defconf, *dupent, *ent, *firstnew; 551 struct conf_entry *newlist, *worklist; 552 int gmatch; 553 554 defconf = worklist = NULL; 555 556 fname = conf; 557 if (fname == NULL) 558 fname = _PATH_CONF; 559 560 if (strcmp(fname, "-") != 0) 561 f = fopen(fname, "r"); 562 else { 563 f = stdin; 564 fname = "<stdin>"; 565 } 566 if (!f) 567 err(1, "%s", conf); 568 569 parse_file(f, fname, &worklist, &defconf); 570 (void) fclose(f); 571 572 /* 573 * All config-file information has been read in and turned into 574 * a worklist. If there were no specific files given on the run 575 * command, then the work of this routine is done. 576 */ 577 if (*files == NULL) { 578 if (defconf != NULL) 579 free_entry(defconf); 580 return (worklist); 581 /* NOTREACHED */ 582 } 583 584 /* 585 * If newsyslog was given a specific list of files to process, 586 * it may be that some of those files were not listed in any 587 * config file. Those unlisted files should get the default 588 * rotation action. First, create the default-rotation action 589 * if none was found in a system config file. 590 */ 591 if (defconf == NULL) { 592 defconf = init_entry(DEFAULT_MARKER, NULL); 593 defconf->numlogs = 3; 594 defconf->size = 50; 595 defconf->permissions = S_IRUSR|S_IWUSR; 596 } 597 598 /* 599 * If newsyslog was run with a list of specific filenames, 600 * then create a new worklist which has only those files in 601 * it, picking up the rotation-rules for those files from 602 * the original worklist. 603 * 604 * XXX - Note that this will copy multiple rules for a single 605 * logfile, if multiple entries are an exact match for 606 * that file. That matches the historic behavior, but do 607 * we want to continue to allow it? If so, it should 608 * probably be handled more intelligently. 609 */ 610 firstnew = newlist = NULL; 611 for (given = files; *given; ++given) { 612 gmatch = 0; 613 /* 614 * First try to find exact-matches for this given file. 615 */ 616 for (ent = worklist; ent; ent = ent->next) { 617 if ((ent->flags & CE_GLOB) != 0) 618 continue; 619 if (strcmp(ent->log, *given) == 0) { 620 gmatch++; 621 dupent = init_entry(*given, ent); 622 if (!firstnew) 623 firstnew = dupent; 624 else 625 newlist->next = dupent; 626 newlist = dupent; 627 } 628 } 629 if (gmatch) { 630 if (verbose > 2) 631 printf("\t+ Matched entry %s\n", *given); 632 continue; 633 } 634 635 /* 636 * There was no exact-match for this given file, so look 637 * for a "glob" entry which does match. 638 */ 639 for (ent = worklist; ent; ent = ent->next) { 640 if ((ent->flags & CE_GLOB) == 0) 641 continue; 642 if (fnmatch(ent->log, *given, FNM_PATHNAME) == 0) { 643 gmatch++; 644 dupent = init_entry(*given, ent); 645 if (!firstnew) 646 firstnew = dupent; 647 else 648 newlist->next = dupent; 649 newlist = dupent; 650 /* This work entry is *not* a glob! */ 651 dupent->flags &= ~CE_GLOB; 652 /* Only allow a match to one glob-entry */ 653 break; 654 } 655 } 656 if (gmatch) { 657 if (verbose > 2) 658 printf("\t+ Matched %s via %s\n", *given, 659 ent->log); 660 continue; 661 } 662 663 /* 664 * This given file was not found in any config file, so 665 * add a worklist item based on the default entry. 666 */ 667 if (verbose > 2) 668 printf("\t+ No entry matched %s (will use %s)\n", 669 *given, DEFAULT_MARKER); 670 dupent = init_entry(*given, defconf); 671 if (!firstnew) 672 firstnew = dupent; 673 else 674 newlist->next = dupent; 675 /* Mark that it was *not* found in a config file */ 676 dupent->def_cfg = 1; 677 newlist = dupent; 678 } 679 680 /* 681 * Free all the entries in the original work list, and then 682 * return the new work list. 683 */ 684 while (worklist) { 685 ent = worklist->next; 686 free_entry(worklist); 687 worklist = ent; 688 } 689 690 free_entry(defconf); 691 return (newlist); 692} 693 694/* 695 * Parse a configuration file and update a linked list of all the logs to 696 * process. 697 */ 698static void 699parse_file(FILE *cf, const char *cfname, struct conf_entry **work_p, 700 struct conf_entry **defconf_p) 701{ 702 char line[BUFSIZ], *parse, *q; 703 char *cp, *errline, *group; 704 struct conf_entry *working, *worklist; 705 struct passwd *pwd; 706 struct group *grp; 707 int eol; 708 709 /* 710 * XXX - for now, assume that only one config file will be read, 711 * ie, this routine is only called one time. 712 */ 713 worklist = NULL; 714 715 while (fgets(line, BUFSIZ, cf)) { 716 if ((line[0] == '\n') || (line[0] == '#') || 717 (strlen(line) == 0)) 718 continue; 719 errline = strdup(line); 720 for (cp = line + 1; *cp != '\0'; cp++) { 721 if (*cp != '#') 722 continue; 723 if (*(cp - 1) == '\\') { 724 strcpy(cp - 1, cp); 725 cp--; 726 continue; 727 } 728 *cp = '\0'; 729 break; 730 } 731 732 q = parse = missing_field(sob(line), errline); 733 parse = son(line); 734 if (!*parse) 735 errx(1, "malformed line (missing fields):\n%s", 736 errline); 737 *parse = '\0'; 738 739 working = init_entry(q, NULL); 740 if (strcasecmp(DEFAULT_MARKER, q) == 0) { 741 if (defconf_p == NULL) { 742 warnx("Ignoring entry for %s in %s!", q, 743 cfname); 744 free_entry(working); 745 continue; 746 } else if (*defconf_p != NULL) { 747 warnx("Ignoring duplicate entry for %s!", q); 748 free_entry(working); 749 continue; 750 } 751 *defconf_p = working; 752 } else { 753 if (!*work_p) 754 *work_p = working; 755 else 756 worklist->next = working; 757 worklist = working; 758 } 759 760 q = parse = missing_field(sob(++parse), errline); 761 parse = son(parse); 762 if (!*parse) 763 errx(1, "malformed line (missing fields):\n%s", 764 errline); 765 *parse = '\0'; 766 if ((group = strchr(q, ':')) != NULL || 767 (group = strrchr(q, '.')) != NULL) { 768 *group++ = '\0'; 769 if (*q) { 770 if (!(isnumber(*q))) { 771 if ((pwd = getpwnam(q)) == NULL) 772 errx(1, 773 "error in config file; unknown user:\n%s", 774 errline); 775 working->uid = pwd->pw_uid; 776 } else 777 working->uid = atoi(q); 778 } else 779 working->uid = (uid_t)-1; 780 781 q = group; 782 if (*q) { 783 if (!(isnumber(*q))) { 784 if ((grp = getgrnam(q)) == NULL) 785 errx(1, 786 "error in config file; unknown group:\n%s", 787 errline); 788 working->gid = grp->gr_gid; 789 } else 790 working->gid = atoi(q); 791 } else 792 working->gid = (gid_t)-1; 793 794 q = parse = missing_field(sob(++parse), errline); 795 parse = son(parse); 796 if (!*parse) 797 errx(1, "malformed line (missing fields):\n%s", 798 errline); 799 *parse = '\0'; 800 } else { 801 working->uid = (uid_t)-1; 802 working->gid = (gid_t)-1; 803 } 804 805 if (!sscanf(q, "%o", &working->permissions)) 806 errx(1, "error in config file; bad permissions:\n%s", 807 errline); 808 809 q = parse = missing_field(sob(++parse), errline); 810 parse = son(parse); 811 if (!*parse) 812 errx(1, "malformed line (missing fields):\n%s", 813 errline); 814 *parse = '\0'; 815 if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0) 816 errx(1, "error in config file; bad value for count of logs to save:\n%s", 817 errline); 818 819 q = parse = missing_field(sob(++parse), errline); 820 parse = son(parse); 821 if (!*parse) 822 errx(1, "malformed line (missing fields):\n%s", 823 errline); 824 *parse = '\0'; 825 if (isdigit(*q)) 826 working->size = atoi(q); 827 else 828 working->size = -1; 829 830 working->flags = 0; 831 q = parse = missing_field(sob(++parse), errline); 832 parse = son(parse); 833 eol = !*parse; 834 *parse = '\0'; 835 { 836 char *ep; 837 u_long ul; 838 839 ul = strtoul(q, &ep, 10); 840 if (ep == q) 841 working->hours = 0; 842 else if (*ep == '*') 843 working->hours = -1; 844 else if (ul > INT_MAX) 845 errx(1, "interval is too large:\n%s", errline); 846 else 847 working->hours = ul; 848 849 if (*ep != '\0' && *ep != '@' && *ep != '*' && 850 *ep != '$') 851 errx(1, "malformed interval/at:\n%s", errline); 852 if (*ep == '@') { 853 if ((working->trim_at = parse8601(ep + 1, errline)) 854 == (time_t) - 1) 855 errx(1, "malformed at:\n%s", errline); 856 working->flags |= CE_TRIMAT; 857 } else if (*ep == '$') { 858 if ((working->trim_at = parseDWM(ep + 1, errline)) 859 == (time_t) - 1) 860 errx(1, "malformed at:\n%s", errline); 861 working->flags |= CE_TRIMAT; 862 } 863 } 864 865 if (eol) 866 q = NULL; 867 else { 868 q = parse = sob(++parse); /* Optional field */ 869 parse = son(parse); 870 if (!*parse) 871 eol = 1; 872 *parse = '\0'; 873 } 874 875 for (; q && *q && !isspacech(*q); q++) { 876 switch (tolowerch(*q)) { 877 case 'b': 878 working->flags |= CE_BINARY; 879 break; 880 case 'c': /* Used by NetBSD for "CE_CREATE" */ 881 /* 882 * netbsd uses 'c' for "create". We will 883 * temporarily accept it for 'g', because 884 * earlier freebsd versions had a typo 885 * of ('G' || 'c')... 886 */ 887 warnx("Assuming 'g' for 'c' in flags for line:\n%s", 888 errline); 889 /* FALLTHROUGH */ 890 case 'g': 891 working->flags |= CE_GLOB; 892 break; 893 case 'j': 894 working->flags |= CE_BZCOMPACT; 895 break; 896 case 'n': 897 working->flags |= CE_NOSIGNAL; 898 break; 899 case 'u': 900 working->flags |= CE_SIGNALGROUP; 901 break; 902 case 'w': 903 working->flags |= CE_COMPACTWAIT; 904 break; 905 case 'z': 906 working->flags |= CE_COMPACT; 907 break; 908 case '-': 909 break; 910 case 'f': /* Used by OpenBSD for "CE_FOLLOW" */ 911 case 'm': /* Used by OpenBSD for "CE_MONITOR" */ 912 case 'p': /* Used by NetBSD for "CE_PLAIN0" */ 913 default: 914 errx(1, "illegal flag in config file -- %c", 915 *q); 916 } 917 } 918 919 if (eol) 920 q = NULL; 921 else { 922 q = parse = sob(++parse); /* Optional field */ 923 parse = son(parse); 924 if (!*parse) 925 eol = 1; 926 *parse = '\0'; 927 } 928 929 working->pid_file = NULL; 930 if (q && *q) { 931 if (*q == '/') 932 working->pid_file = strdup(q); 933 else if (isdigit(*q)) 934 goto got_sig; 935 else 936 errx(1, 937 "illegal pid file or signal number in config file:\n%s", 938 errline); 939 } 940 if (eol) 941 q = NULL; 942 else { 943 q = parse = sob(++parse); /* Optional field */ 944 *(parse = son(parse)) = '\0'; 945 } 946 947 working->sig = SIGHUP; 948 if (q && *q) { 949 if (isdigit(*q)) { 950 got_sig: 951 working->sig = atoi(q); 952 } else { 953 err_sig: 954 errx(1, 955 "illegal signal number in config file:\n%s", 956 errline); 957 } 958 if (working->sig < 1 || working->sig >= NSIG) 959 goto err_sig; 960 } 961 962 /* 963 * Finish figuring out what pid-file to use (if any) in 964 * later processing if this logfile needs to be rotated. 965 */ 966 if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) { 967 /* 968 * This config-entry specified 'n' for nosignal, 969 * see if it also specified an explicit pid_file. 970 * This would be a pretty pointless combination. 971 */ 972 if (working->pid_file != NULL) { 973 warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s", 974 working->pid_file, errline); 975 free(working->pid_file); 976 working->pid_file = NULL; 977 } 978 } else if (working->pid_file == NULL) { 979 /* 980 * This entry did not specify the 'n' flag, which 981 * means it should signal syslogd unless it had 982 * specified some other pid-file (and obviously the 983 * syslog pid-file will not be for a process-group). 984 * Also, we should only try to notify syslog if we 985 * are root. 986 */ 987 if (working->flags & CE_SIGNALGROUP) { 988 warnx("Ignoring flag 'U' in line:\n%s", 989 errline); 990 working->flags &= ~CE_SIGNALGROUP; 991 } 992 if (needroot) 993 working->pid_file = strdup(_PATH_SYSLOGPID); 994 } 995 996 free(errline); 997 errline = NULL; 998 } 999} 1000 1001static char * 1002missing_field(char *p, char *errline) 1003{ 1004 1005 if (!p || !*p) 1006 errx(1, "missing field in config file:\n%s", errline); 1007 return (p); 1008} 1009 1010static void 1011dotrim(const struct conf_entry *ent, char *log, int numdays, int flags) 1012{ 1013 char dirpart[MAXPATHLEN], namepart[MAXPATHLEN]; 1014 char file1[MAXPATHLEN], file2[MAXPATHLEN]; 1015 char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN]; 1016 char jfile1[MAXPATHLEN]; 1017 char tfile[MAXPATHLEN]; 1018 int notified, need_notification, fd, _numdays; 1019 struct stat st; 1020 1021 if (archtodir) { 1022 char *p; 1023 1024 /* build complete name of archive directory into dirpart */ 1025 if (*archdirname == '/') { /* absolute */ 1026 strlcpy(dirpart, archdirname, sizeof(dirpart)); 1027 } else { /* relative */ 1028 /* get directory part of logfile */ 1029 strlcpy(dirpart, log, sizeof(dirpart)); 1030 if ((p = rindex(dirpart, '/')) == NULL) 1031 dirpart[0] = '\0'; 1032 else 1033 *(p + 1) = '\0'; 1034 strlcat(dirpart, archdirname, sizeof(dirpart)); 1035 } 1036 1037 /* check if archive directory exists, if not, create it */ 1038 if (lstat(dirpart, &st)) 1039 createdir(dirpart); 1040 1041 /* get filename part of logfile */ 1042 if ((p = rindex(log, '/')) == NULL) 1043 strlcpy(namepart, log, sizeof(namepart)); 1044 else 1045 strlcpy(namepart, p + 1, sizeof(namepart)); 1046 1047 /* name of oldest log */ 1048 (void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart, 1049 namepart, numdays); 1050 (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, 1051 COMPRESS_POSTFIX); 1052 snprintf(jfile1, sizeof(jfile1), "%s%s", file1, 1053 BZCOMPRESS_POSTFIX); 1054 } else { 1055 /* name of oldest log */ 1056 (void) snprintf(file1, sizeof(file1), "%s.%d", log, numdays); 1057 (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, 1058 COMPRESS_POSTFIX); 1059 snprintf(jfile1, sizeof(jfile1), "%s%s", file1, 1060 BZCOMPRESS_POSTFIX); 1061 } 1062 1063 if (noaction) { 1064 printf("\trm -f %s\n", file1); 1065 printf("\trm -f %s\n", zfile1); 1066 printf("\trm -f %s\n", jfile1); 1067 } else { 1068 (void) unlink(file1); 1069 (void) unlink(zfile1); 1070 (void) unlink(jfile1); 1071 } 1072 1073 /* Move down log files */ 1074 _numdays = numdays; /* preserve */ 1075 while (numdays--) { 1076 1077 (void) strlcpy(file2, file1, sizeof(file2)); 1078 1079 if (archtodir) 1080 (void) snprintf(file1, sizeof(file1), "%s/%s.%d", 1081 dirpart, namepart, numdays); 1082 else 1083 (void) snprintf(file1, sizeof(file1), "%s.%d", log, 1084 numdays); 1085 1086 (void) strlcpy(zfile1, file1, sizeof(zfile1)); 1087 (void) strlcpy(zfile2, file2, sizeof(zfile2)); 1088 if (lstat(file1, &st)) { 1089 (void) strlcat(zfile1, COMPRESS_POSTFIX, 1090 sizeof(zfile1)); 1091 (void) strlcat(zfile2, COMPRESS_POSTFIX, 1092 sizeof(zfile2)); 1093 if (lstat(zfile1, &st)) { 1094 strlcpy(zfile1, file1, sizeof(zfile1)); 1095 strlcpy(zfile2, file2, sizeof(zfile2)); 1096 strlcat(zfile1, BZCOMPRESS_POSTFIX, 1097 sizeof(zfile1)); 1098 strlcat(zfile2, BZCOMPRESS_POSTFIX, 1099 sizeof(zfile2)); 1100 if (lstat(zfile1, &st)) 1101 continue; 1102 } 1103 } 1104 if (noaction) { 1105 printf("\tmv %s %s\n", zfile1, zfile2); 1106 printf("\tchmod %o %s\n", ent->permissions, zfile2); 1107 if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 1108 printf("\tchown %u:%u %s\n", 1109 ent->uid, ent->gid, zfile2); 1110 } else { 1111 (void) rename(zfile1, zfile2); 1112 if (chmod(zfile2, ent->permissions)) 1113 warn("can't chmod %s", file2); 1114 if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 1115 if (chown(zfile2, ent->uid, ent->gid)) 1116 warn("can't chown %s", zfile2); 1117 } 1118 } 1119 if (!noaction && !(flags & CE_BINARY)) { 1120 /* Report the trimming to the old log */ 1121 (void) log_trim(log, ent); 1122 } 1123 1124 if (!_numdays) { 1125 if (noaction) 1126 printf("\trm %s\n", log); 1127 else 1128 (void) unlink(log); 1129 } else { 1130 if (noaction) 1131 printf("\tmv %s to %s\n", log, file1); 1132 else { 1133 if (archtodir) 1134 movefile(log, file1, ent->permissions, ent->uid, 1135 ent->gid); 1136 else 1137 (void) rename(log, file1); 1138 } 1139 } 1140 1141 /* Now move the new log file into place */ 1142 strlcpy(tfile, log, sizeof(tfile)); 1143 strlcat(tfile, ".XXXXXX", sizeof(tfile)); 1144 if (noaction) { 1145 printf("Start new log...\n"); 1146 printf("\tmktemp %s\n", tfile); 1147 } else { 1148 mkstemp(tfile); 1149 fd = creat(tfile, ent->permissions); 1150 if (fd < 0) 1151 err(1, "can't start new log"); 1152 if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 1153 if (fchown(fd, ent->uid, ent->gid)) 1154 err(1, "can't chown new log file"); 1155 (void) close(fd); 1156 if (!(flags & CE_BINARY)) { 1157 /* Add status message to new log file */ 1158 if (log_trim(tfile, ent)) 1159 err(1, "can't add status message to log"); 1160 } 1161 } 1162 if (noaction) { 1163 printf("\tchmod %o %s\n", ent->permissions, tfile); 1164 printf("\tmv %s %s\n", tfile, log); 1165 } else { 1166 (void) chmod(tfile, ent->permissions); 1167 if (rename(tfile, log) < 0) { 1168 err(1, "can't start new log"); 1169 (void) unlink(tfile); 1170 } 1171 } 1172 1173 /* 1174 * Find out if there is a process to signal. If nosignal (-s) was 1175 * specified, then do not signal any process. Note that nosignal 1176 * will trigger a warning message if the rotated logfile needs to 1177 * be compressed, *unless* -R was specified. This is because there 1178 * presumably still are process(es) writing to the old logfile, but 1179 * we assume that a -sR request comes from a process which writes 1180 * to the logfile, and as such, that process has already made sure 1181 * that the logfile is not presently in use. 1182 */ 1183 need_notification = notified = 0; 1184 if (ent->pid_file != NULL) { 1185 need_notification = 1; 1186 if (!nosignal) 1187 notified = send_signal(ent); /* the normal case! */ 1188 else if (rotatereq) 1189 need_notification = 0; 1190 } 1191 1192 if ((flags & CE_COMPACT) || (flags & CE_BZCOMPACT)) { 1193 if (need_notification && !notified) 1194 warnx( 1195 "log %s.0 not compressed because daemon(s) not notified", 1196 log); 1197 else if (noaction) 1198 if (flags & CE_COMPACT) 1199 printf("\tgzip %s.0\n", log); 1200 else 1201 printf("\tbzip2 %s.0\n", log); 1202 else { 1203 if (notified) { 1204 if (verbose) 1205 printf("small pause to allow daemon(s) to close log\n"); 1206 sleep(10); 1207 } 1208 if (archtodir) { 1209 (void) snprintf(file1, sizeof(file1), "%s/%s", 1210 dirpart, namepart); 1211 if (flags & CE_COMPACT) 1212 compress_log(file1, 1213 flags & CE_COMPACTWAIT); 1214 else if (flags & CE_BZCOMPACT) 1215 bzcompress_log(file1, 1216 flags & CE_COMPACTWAIT); 1217 } else { 1218 if (flags & CE_COMPACT) 1219 compress_log(log, 1220 flags & CE_COMPACTWAIT); 1221 else if (flags & CE_BZCOMPACT) 1222 bzcompress_log(log, 1223 flags & CE_COMPACTWAIT); 1224 } 1225 } 1226 } 1227} 1228 1229/* Log the fact that the logs were turned over */ 1230static int 1231log_trim(const char *log, const struct conf_entry *log_ent) 1232{ 1233 FILE *f; 1234 const char *xtra; 1235 1236 if ((f = fopen(log, "a")) == NULL) 1237 return (-1); 1238 xtra = ""; 1239 if (log_ent->def_cfg) 1240 xtra = " using <default> rule"; 1241 if (log_ent->r_reason != NULL) 1242 fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n", 1243 daytime, hostname, (int) getpid(), log_ent->r_reason, xtra); 1244 else 1245 fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n", 1246 daytime, hostname, (int) getpid(), xtra); 1247 if (fclose(f) == EOF) 1248 err(1, "log_trim: fclose:"); 1249 return (0); 1250} 1251 1252/* Fork of gzip to compress the old log file */ 1253static void 1254compress_log(char *log, int dowait) 1255{ 1256 pid_t pid; 1257 char tmp[MAXPATHLEN]; 1258 1259 while (dowait && (wait(NULL) > 0 || errno == EINTR)) 1260 ; 1261 (void) snprintf(tmp, sizeof(tmp), "%s.0", log); 1262 pid = fork(); 1263 if (pid < 0) 1264 err(1, "gzip fork"); 1265 else if (!pid) { 1266 (void) execl(_PATH_GZIP, _PATH_GZIP, "-f", tmp, (char *)0); 1267 err(1, _PATH_GZIP); 1268 } 1269} 1270 1271/* Fork of bzip2 to compress the old log file */ 1272static void 1273bzcompress_log(char *log, int dowait) 1274{ 1275 pid_t pid; 1276 char tmp[MAXPATHLEN]; 1277 1278 while (dowait && (wait(NULL) > 0 || errno == EINTR)) 1279 ; 1280 snprintf(tmp, sizeof(tmp), "%s.0", log); 1281 pid = fork(); 1282 if (pid < 0) 1283 err(1, "bzip2 fork"); 1284 else if (!pid) { 1285 execl(_PATH_BZIP2, _PATH_BZIP2, "-f", tmp, (char *)0); 1286 err(1, _PATH_BZIP2); 1287 } 1288} 1289 1290/* Return size in kilobytes of a file */ 1291static int 1292sizefile(char *file) 1293{ 1294 struct stat sb; 1295 1296 if (stat(file, &sb) < 0) 1297 return (-1); 1298 return (kbytes(dbtob(sb.st_blocks))); 1299} 1300 1301/* Return the age of old log file (file.0) */ 1302static int 1303age_old_log(char *file) 1304{ 1305 struct stat sb; 1306 char tmp[MAXPATHLEN + sizeof(".0") + sizeof(COMPRESS_POSTFIX) + 1]; 1307 1308 if (archtodir) { 1309 char *p; 1310 1311 /* build name of archive directory into tmp */ 1312 if (*archdirname == '/') { /* absolute */ 1313 strlcpy(tmp, archdirname, sizeof(tmp)); 1314 } else { /* relative */ 1315 /* get directory part of logfile */ 1316 strlcpy(tmp, file, sizeof(tmp)); 1317 if ((p = rindex(tmp, '/')) == NULL) 1318 tmp[0] = '\0'; 1319 else 1320 *(p + 1) = '\0'; 1321 strlcat(tmp, archdirname, sizeof(tmp)); 1322 } 1323 1324 strlcat(tmp, "/", sizeof(tmp)); 1325 1326 /* get filename part of logfile */ 1327 if ((p = rindex(file, '/')) == NULL) 1328 strlcat(tmp, file, sizeof(tmp)); 1329 else 1330 strlcat(tmp, p + 1, sizeof(tmp)); 1331 } else { 1332 (void) strlcpy(tmp, file, sizeof(tmp)); 1333 } 1334 1335 if (stat(strcat(tmp, ".0"), &sb) < 0) 1336 if (stat(strcat(tmp, COMPRESS_POSTFIX), &sb) < 0) 1337 return (-1); 1338 return ((int)(timenow - sb.st_mtime + 1800) / 3600); 1339} 1340 1341/* Skip Over Blanks */ 1342static char * 1343sob(char *p) 1344{ 1345 while (p && *p && isspace(*p)) 1346 p++; 1347 return (p); 1348} 1349 1350/* Skip Over Non-Blanks */ 1351static char * 1352son(char *p) 1353{ 1354 while (p && *p && !isspace(*p)) 1355 p++; 1356 return (p); 1357} 1358 1359/* 1360 * Parse a limited subset of ISO 8601. The specific format is as follows: 1361 * 1362 * [CC[YY[MM[DD]]]][THH[MM[SS]]] (where `T' is the literal letter) 1363 * 1364 * We don't accept a timezone specification; missing fields (including timezone) 1365 * are defaulted to the current date but time zero. 1366 */ 1367static time_t 1368parse8601(char *s, char *errline) 1369{ 1370 char *t; 1371 time_t tsecs; 1372 struct tm tm, *tmp; 1373 u_long ul; 1374 1375 tmp = localtime(&timenow); 1376 tm = *tmp; 1377 1378 tm.tm_hour = tm.tm_min = tm.tm_sec = 0; 1379 1380 ul = strtoul(s, &t, 10); 1381 if (*t != '\0' && *t != 'T') 1382 return (-1); 1383 1384 /* 1385 * Now t points either to the end of the string (if no time was 1386 * provided) or to the letter `T' which separates date and time in 1387 * ISO 8601. The pointer arithmetic is the same for either case. 1388 */ 1389 switch (t - s) { 1390 case 8: 1391 tm.tm_year = ((ul / 1000000) - 19) * 100; 1392 ul = ul % 1000000; 1393 case 6: 1394 tm.tm_year -= tm.tm_year % 100; 1395 tm.tm_year += ul / 10000; 1396 ul = ul % 10000; 1397 case 4: 1398 tm.tm_mon = (ul / 100) - 1; 1399 ul = ul % 100; 1400 case 2: 1401 tm.tm_mday = ul; 1402 case 0: 1403 break; 1404 default: 1405 return (-1); 1406 } 1407 1408 /* sanity check */ 1409 if (tm.tm_year < 70 || tm.tm_mon < 0 || tm.tm_mon > 12 1410 || tm.tm_mday < 1 || tm.tm_mday > 31) 1411 return (-1); 1412 1413 if (*t != '\0') { 1414 s = ++t; 1415 ul = strtoul(s, &t, 10); 1416 if (*t != '\0' && !isspace(*t)) 1417 return (-1); 1418 1419 switch (t - s) { 1420 case 6: 1421 tm.tm_sec = ul % 100; 1422 ul /= 100; 1423 case 4: 1424 tm.tm_min = ul % 100; 1425 ul /= 100; 1426 case 2: 1427 tm.tm_hour = ul; 1428 case 0: 1429 break; 1430 default: 1431 return (-1); 1432 } 1433 1434 /* sanity check */ 1435 if (tm.tm_sec < 0 || tm.tm_sec > 60 || tm.tm_min < 0 1436 || tm.tm_min > 59 || tm.tm_hour < 0 || tm.tm_hour > 23) 1437 return (-1); 1438 } 1439 if ((tsecs = mktime(&tm)) == -1) 1440 errx(1, "nonexistent time:\n%s", errline); 1441 return (tsecs); 1442} 1443 1444/* physically move file */ 1445static void 1446movefile(char *from, char *to, int perm, uid_t owner_uid, gid_t group_gid) 1447{ 1448 FILE *src, *dst; 1449 int c; 1450 1451 if ((src = fopen(from, "r")) == NULL) 1452 err(1, "can't fopen %s for reading", from); 1453 if ((dst = fopen(to, "w")) == NULL) 1454 err(1, "can't fopen %s for writing", to); 1455 if (owner_uid != (uid_t)-1 || group_gid != (gid_t)-1) { 1456 if (fchown(fileno(dst), owner_uid, group_gid)) 1457 err(1, "can't fchown %s", to); 1458 } 1459 if (fchmod(fileno(dst), perm)) 1460 err(1, "can't fchmod %s", to); 1461 1462 while ((c = getc(src)) != EOF) { 1463 if ((putc(c, dst)) == EOF) 1464 err(1, "error writing to %s", to); 1465 } 1466 1467 if (ferror(src)) 1468 err(1, "error reading from %s", from); 1469 if ((fclose(src)) != 0) 1470 err(1, "can't fclose %s", to); 1471 if ((fclose(dst)) != 0) 1472 err(1, "can't fclose %s", from); 1473 if ((unlink(from)) != 0) 1474 err(1, "can't unlink %s", from); 1475} 1476 1477/* create one or more directory components of a path */ 1478static void 1479createdir(char *dirpart) 1480{ 1481 int res; 1482 char *s, *d; 1483 char mkdirpath[MAXPATHLEN]; 1484 struct stat st; 1485 1486 s = dirpart; 1487 d = mkdirpath; 1488 1489 for (;;) { 1490 *d++ = *s++; 1491 if (*s != '/' && *s != '\0') 1492 continue; 1493 *d = '\0'; 1494 res = lstat(mkdirpath, &st); 1495 if (res != 0) { 1496 if (noaction) { 1497 printf("\tmkdir %s\n", mkdirpath); 1498 } else { 1499 res = mkdir(mkdirpath, 0755); 1500 if (res != 0) 1501 err(1, "Error on mkdir(\"%s\") for -a", 1502 mkdirpath); 1503 } 1504 } 1505 if (*s == '\0') 1506 break; 1507 } 1508 if (verbose) 1509 printf("created directory '%s' for -a\n", dirpart); 1510} 1511 1512/*- 1513 * Parse a cyclic time specification, the format is as follows: 1514 * 1515 * [Dhh] or [Wd[Dhh]] or [Mdd[Dhh]] 1516 * 1517 * to rotate a logfile cyclic at 1518 * 1519 * - every day (D) within a specific hour (hh) (hh = 0...23) 1520 * - once a week (W) at a specific day (d) OR (d = 0..6, 0 = Sunday) 1521 * - once a month (M) at a specific day (d) (d = 1..31,l|L) 1522 * 1523 * We don't accept a timezone specification; missing fields 1524 * are defaulted to the current date but time zero. 1525 */ 1526static time_t 1527parseDWM(char *s, char *errline) 1528{ 1529 char *t; 1530 time_t tsecs; 1531 struct tm tm, *tmp; 1532 long l; 1533 int nd; 1534 static int mtab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 1535 int WMseen = 0; 1536 int Dseen = 0; 1537 1538 tmp = localtime(&timenow); 1539 tm = *tmp; 1540 1541 /* set no. of days per month */ 1542 1543 nd = mtab[tm.tm_mon]; 1544 1545 if (tm.tm_mon == 1) { 1546 if (((tm.tm_year + 1900) % 4 == 0) && 1547 ((tm.tm_year + 1900) % 100 != 0) && 1548 ((tm.tm_year + 1900) % 400 == 0)) { 1549 nd++; /* leap year, 29 days in february */ 1550 } 1551 } 1552 tm.tm_hour = tm.tm_min = tm.tm_sec = 0; 1553 1554 for (;;) { 1555 switch (*s) { 1556 case 'D': 1557 if (Dseen) 1558 return (-1); 1559 Dseen++; 1560 s++; 1561 l = strtol(s, &t, 10); 1562 if (l < 0 || l > 23) 1563 return (-1); 1564 tm.tm_hour = l; 1565 break; 1566 1567 case 'W': 1568 if (WMseen) 1569 return (-1); 1570 WMseen++; 1571 s++; 1572 l = strtol(s, &t, 10); 1573 if (l < 0 || l > 6) 1574 return (-1); 1575 if (l != tm.tm_wday) { 1576 int save; 1577 1578 if (l < tm.tm_wday) { 1579 save = 6 - tm.tm_wday; 1580 save += (l + 1); 1581 } else { 1582 save = l - tm.tm_wday; 1583 } 1584 1585 tm.tm_mday += save; 1586 1587 if (tm.tm_mday > nd) { 1588 tm.tm_mon++; 1589 tm.tm_mday = tm.tm_mday - nd; 1590 } 1591 } 1592 break; 1593 1594 case 'M': 1595 if (WMseen) 1596 return (-1); 1597 WMseen++; 1598 s++; 1599 if (tolower(*s) == 'l') { 1600 tm.tm_mday = nd; 1601 s++; 1602 t = s; 1603 } else { 1604 l = strtol(s, &t, 10); 1605 if (l < 1 || l > 31) 1606 return (-1); 1607 1608 if (l > nd) 1609 return (-1); 1610 tm.tm_mday = l; 1611 } 1612 break; 1613 1614 default: 1615 return (-1); 1616 break; 1617 } 1618 1619 if (*t == '\0' || isspace(*t)) 1620 break; 1621 else 1622 s = t; 1623 } 1624 if ((tsecs = mktime(&tm)) == -1) 1625 errx(1, "nonexistent time:\n%s", errline); 1626 return (tsecs); 1627} 1628