Home | History | Annotate | Download | only in newsyslog

Lines Matching refs:log

52  * newsyslog(8) - a program to roll over log files provided that specified
53 * critera are met, optionally preserving a number of historical log files.
93 #define CE_CREATE 0x08 /* Create log file if none exists */
95 #define CE_SYSLPROTOCOL 0x20 /* log in syslog-protocol format,
97 #define CE_NOEMPTY 0x40 /* Do not rotate empty log file */
100 uid_t uid; /* Owner of log */
101 gid_t gid; /* Group of log */
104 size_t maxsize; /* Maximum log size */
105 int maxage; /* Hours between log trimming */
110 char logfile[MAXPATHLEN]; /* Path to log file */
160 struct conf_entry log;
214 for (lineno = 0; !parse_cfgline(&log, fd, &lineno);) {
216 * If specific log files were specified, touch only
221 if (strcmp(log.logfile, argv[i]) == 0)
226 log_examine(&log, force);
240 parse_cfgline(struct conf_entry *log, FILE *fd, size_t *_lineno)
273 (void)memset(log, 0, sizeof(*log));
277 (void)strlcpy(log->logfile, *ap++, sizeof(log->logfile));
278 if (log->logfile[0] != '/')
297 log->uid = (uid_t)-1;
299 log->uid = pw->pw_uid;
300 log->gid = gr->gr_gid;
305 log->uid = (uid_t)-1;
306 log->gid = getegid();
314 log->mode = (mode_t)i;
317 if (sscanf(*ap++, "%d", &log->numhist) != 1) {
318 warnx("config line %d: bad log count", lineno);
324 log->maxsize = (size_t)-1;
326 log->maxsize = (int)strtol(*ap, &q, 0);
328 warnx("config line %d: bad log size", lineno);
335 log->maxage = -1;
336 log->trimat = (time_t)-1;
341 log->maxage = (int)strtol(q, &q, 10);
348 log->trimat = parse_iso8601(q + 1);
349 if (log->trimat == (time_t)-1) {
354 if ((log->trimat = parse_dwm(q + 1)) == (time_t)-1) {
358 } else if (log->maxage == -1) {
359 warnx("config line %d: bad log age", lineno);
365 log->flags = (nosignal ? CE_NOSIGNAL : 0);
371 log->flags |= CE_BINARY;
374 log->flags |= CE_CREATE;
377 log->flags |= CE_NOEMPTY;
380 log->flags |= CE_NOSIGNAL;
383 log->flags |= CE_PLAIN0;
401 (void)strlcpy(log->pidfile, *ap++, sizeof(log->pidfile));
403 log->pidfile[0] = '\0';
407 if ((log->signum = getsig(*ap++)) < 0) {
412 log->signum = SIGHUP;
422 * Examine a log file. If the trim conditions are met, call log_trim() to
423 * trim the log file.
426 log_examine(struct conf_entry *log, int force)
438 PRHDRINFO(("\n%s <%d%s>: ", log->logfile, log->numhist,
446 if (stat(log->logfile, &sb) < 0) {
447 if (errno == ENOENT && (log->flags & CE_CREATE) != 0) {
450 log_create(log);
455 if (stat(log->logfile, &sb))
456 err(EXIT_FAILURE, "%s", log->logfile);
458 PRHDRINFO(("does not exist --> skip log\n"));
461 err(EXIT_FAILURE, "%s", log->logfile);
465 PRHDRINFO(("not a regular file --> skip log\n"));
469 /* Skip rotation of empty log file when flag
472 if (sb.st_size == 0 && (log->flags & CE_NOEMPTY) != 0) {
473 PRHDRINFO(("empty file --> skip log\n"));
477 /* Size of the log file in kB. */
481 * Get the age (expressed in hours) of the current log file with
482 * respect to the newest historical log file.
486 (void)strlcpy(tmp, log->logfile, sizeof(tmp));
497 * trim the log.
500 * need at least one historical log file to determine the `age' of
501 * the active log file. WRT `trimat', we will trim up to one hour
506 if (log->maxage >= 0 && (age >= log->maxage || age < 0)) {
508 reason = "log age > interval";
509 } else if (size >= log->maxsize) {
511 reason = "log size > size";
512 } else if (log->trimat != (time_t)-1 && now >= log->trimat &&
514 difftime(now, log->trimat) < 60 * 60) {
523 PRHDRINFO(("--> trim log (%s)\n", reason));
524 log_trim(log);
526 PRHDRINFO(("--> skip log (trim conditions not met)\n"));
530 * Trim the specified log file.
533 log_trim(struct conf_entry *log)
540 if (log->numhist != 0) {
541 /* Remove oldest historical log. */
544 log->logfile, log->numhist - 1);
554 * If a historical log file isn't compressed, and 'z' has been
560 for (i = 0; i < log->numhist; i++) {
561 snprintf(file1, sizeof(file1), "%s.%d", log->logfile, i);
568 log_compress(log, file1);
572 /* Move down log files. */
573 for (i = log->numhist - 1; i > 0; i--) {
575 snprintf(file1, sizeof(file1), "%s.%d%s", log->logfile,
577 snprintf(file2, sizeof(file2), "%s.%d%s", log->logfile,
588 PRINFO(("chmod %o %s\n", log->mode, file2));
590 if (chmod(file2, log->mode))
592 PRINFO(("chown %d:%d %s\n", log->uid, log->gid,
595 if (chown(file2, log->uid, log->gid))
599 log_get_format(log);
600 log_trimmed(log);
602 /* Create the historical log file if we're maintaining history. */
603 if (log->numhist == 0) {
604 PRINFO(("rm -f %s\n", log->logfile));
606 if (unlink(log->logfile))
607 err(EXIT_FAILURE, "%s", log->logfile);
609 (void)snprintf(file1, sizeof(file1), "%s.0", log->logfile);
610 PRINFO(("mv %s %s\n", log->logfile, file1));
612 if (rename(log->logfile, file1))
613 err(EXIT_FAILURE, "%s", log->logfile);
616 PRINFO(("(create new log)\n"));
617 log_create(log);
618 log_trimmed(log);
620 /* Set the correct permissions on the log. */
621 PRINFO(("chmod %o %s\n", log->mode, log->logfile));
623 if (chmod(log->logfile, log->mode))
624 log->logfile);
627 if ((log->flags & CE_NOSIGNAL) == 0) {
628 if (log->pidfile[0] != '\0')
629 pid = readpidfile(log->pidfile);
635 sys_signame[log->signum], (u_long)pid));
637 if (kill(pid, log->signum))
642 /* If the newest historical log is to be compressed, do it here. */
643 if (ziptype && !(log->flags & CE_PLAIN0) && log->numhist != 0) {
644 snprintf(file1, sizeof(file1), "%s.0", log->logfile);
645 if ((log->flags & CE_NOSIGNAL) == 0) {
649 log_compress(log, file1);
654 log_get_format(struct conf_entry *log)
660 if ((log->flags & CE_BINARY) != 0)
662 PRINFO(("(read line format of %s)\n", log->logfile));
666 if ((fd = fopen(log->logfile, "r")) == NULL)
673 log->flags |= CE_SYSLPROTOCOL;
678 * Write an entry to the log file recording the fact that it was trimmed.
681 log_trimmed(struct conf_entry *log)
686 const char trim_message[] = "log file turned over";
688 if ((log->flags & CE_BINARY) != 0)
690 PRINFO(("(append rotation notice to %s)\n", log->logfile));
694 if ((fd = fopen(log->logfile, "at")) == NULL)
695 err(EXIT_FAILURE, "%s", log->logfile);
697 if ((log->flags & CE_SYSLPROTOCOL) == 0) {
749 * Create a new log file.
752 log_create(struct conf_entry *log)
759 if ((fd = creat(log->logfile, log->mode)) < 0)
760 err(EXIT_FAILURE, "%s", log->logfile);
761 if (fchown(fd, log->uid, log->gid) < 0)
762 err(EXIT_FAILURE, "%s", log->logfile);
767 * Fork off gzip(1) to compress a log file. This routine takes an
769 * used to compress historical log files other than the newest.
772 log_compress(struct conf_entry *log, const char *fn)
797 PRINFO(("chown %d:%d %s\n", log->uid, log->gid, tmp));
799 if (chown(tmp, log->uid, log->gid))
941 * to rotate a log file cyclic at