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