2 * This file contains changes from the Open Software Foundation.
6 * Copyright 1988, 1989 by the Massachusetts Institute of Technology
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
22 * newsyslog - roll over selected logs at the appropriate time, keeping the a
23 * specified number of backup files around.
27 static const char rcsid[] =
32 #ifndef COMPRESS_POSTFIX
33 #define COMPRESS_POSTFIX ".gz"
35 #ifndef BZCOMPRESS_POSTFIX
36 #define BZCOMPRESS_POSTFIX ".bz2"
51 #include <sys/types.h>
53 #include <sys/param.h>
56 #include "pathnames.h"
58 #define kbytes(size) (((size) + 1023) >> 10)
61 /* Calculates (db * DEV_BSIZE) */
62 #define dbtob(db) ((unsigned)(db) << UBSHIFT)
65 #define CE_COMPACT 1 /* Compact the achived log files */
66 #define CE_BINARY 2 /* Logfile is in binary, don't add */
67 #define CE_BZCOMPACT 8 /* Compact the achived log files with bzip2 */
69 #define CE_TRIMAT 4 /* trim at a specific time */
74 char *log; /* Name of the log */
75 char *pid_file; /* PID file */
76 int uid; /* Owner of log */
77 int gid; /* Group of log */
78 int numlogs; /* Number of logs to keep */
79 int size; /* Size cutoff to trigger trimming the log */
80 int hours; /* Hours between log trimming */
81 time_t trim_at; /* Specific time to do trimming */
82 int permissions; /* File permissions on the log */
83 int flags; /* CE_COMPACT, CE_BZCOMPACT, CE_BINARY */
84 int sig; /* Signal to send */
85 struct conf_entry *next;/* Linked list pointer */
88 int archtodir = 0; /* Archive old logfiles to other directory */
89 int verbose = 0; /* Print out what's going on */
90 int needroot = 1; /* Root privs are necessary */
91 int noaction = 0; /* Don't do anything, just show it */
92 int force = 0; /* Force the trim no matter what */
93 char *archdirname; /* Directory path to old logfiles archive */
94 const char *conf = _PATH_CONF; /* Configuration file to use */
98 #define MAX_PID 99999 /* was lower, see /usr/include/sys/proc.h */
99 char hostname[MAXHOSTNAMELEN]; /* hostname */
100 char *daytime; /* timenow in human readable form */
102 static struct conf_entry *parse_file(char **files);
103 static char *sob(char *p);
104 static char *son(char *p);
105 static char *missing_field(char *p, char *errline);
106 static void do_entry(struct conf_entry * ent);
107 static void PRS(int argc, char **argv);
108 static void usage(void);
109 static void dotrim(char *log, const char *pid_file, int numdays, int falgs,
110 int perm, int owner_uid, int group_gid, int sig);
111 static int log_trim(char *log);
112 static void compress_log(char *log);
113 static void bzcompress_log(char *log);
114 static int sizefile(char *file);
115 static int age_old_log(char *file);
116 static pid_t get_pid(const char *pid_file);
117 static time_t parse8601(char *s);
118 static void movefile(char *from, char *to, int perm, int owner_uid,
120 static void createdir(char *dirpart);
121 static time_t parseDWM(char *s);
124 main(int argc, char **argv)
126 struct conf_entry *p, *q;
129 if (needroot && getuid() && geteuid())
130 errx(1, "must have root privs");
131 p = q = parse_file(argv + optind);
143 do_entry(struct conf_entry * ent)
146 const char *pid_file;
149 if (ent->flags & CE_COMPACT)
150 printf("%s <%dZ>: ", ent->log, ent->numlogs);
151 else if (ent->flags & CE_BZCOMPACT)
152 printf("%s <%dJ>: ", ent->log, ent->numlogs);
154 printf("%s <%d>: ", ent->log, ent->numlogs);
156 size = sizefile(ent->log);
157 modtime = age_old_log(ent->log);
160 printf("does not exist.\n");
162 if (ent->flags & CE_TRIMAT) {
163 if (timenow < ent->trim_at
164 || difftime(timenow, ent->trim_at) >= 60 * 60) {
166 printf("--> will trim at %s",
167 ctime(&ent->trim_at));
169 } else if (verbose && ent->hours <= 0) {
170 printf("--> time is up\n");
173 if (verbose && (ent->size > 0))
174 printf("size (Kb): %d [%d] ", size, ent->size);
175 if (verbose && (ent->hours > 0))
176 printf(" age (hr): %d [%d] ", modtime, ent->hours);
177 if (force || ((ent->size > 0) && (size >= ent->size)) ||
178 (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) ||
179 ((ent->hours > 0) && ((modtime >= ent->hours)
180 || (modtime < 0)))) {
182 printf("--> trimming log....\n");
183 if (noaction && !verbose) {
184 if (ent->flags & CE_COMPACT)
185 printf("%s <%dZ>: trimming\n",
186 ent->log, ent->numlogs);
187 else if (ent->flags & CE_BZCOMPACT)
188 printf("%s <%dJ>: trimming\n",
189 ent->log, ent->numlogs);
191 printf("%s <%d>: trimming\n",
192 ent->log, ent->numlogs);
195 pid_file = ent->pid_file;
197 /* Only try to notify syslog if we are root */
199 pid_file = _PATH_SYSLOGPID;
203 dotrim(ent->log, pid_file, ent->numlogs,
204 ent->flags, ent->permissions, ent->uid, ent->gid,
208 printf("--> skipping\n");
214 PRS(int argc, char **argv)
219 timenow = time((time_t *) 0);
220 daytime = ctime(&timenow) + 4;
223 /* Let's get our hostname */
224 (void) gethostname(hostname, sizeof(hostname));
226 /* Truncate domain */
227 if ((p = strchr(hostname, '.'))) {
230 while ((c = getopt(argc, argv, "nrvFf:a:t:")) != -1)
237 archdirname = optarg;
261 "usage: newsyslog [-Fnrv] [-f config-file] [-a directory]\n");
266 * Parse a configuration file and return a linked list of all the logs to
269 static struct conf_entry *
270 parse_file(char **files)
273 char line[BUFSIZ], *parse, *q;
274 char *errline, *group;
276 struct conf_entry *first, *working;
281 first = working = NULL;
283 if (strcmp(conf, "-"))
284 f = fopen(conf, "r");
289 while (fgets(line, BUFSIZ, f)) {
290 if ((line[0] == '\n') || (line[0] == '#'))
292 errline = strdup(line);
294 q = parse = missing_field(sob(line), errline);
297 errx(1, "malformed line (missing fields):\n%s",
302 for (p = files; *p; ++p)
303 if (strcmp(*p, q) == 0)
310 if ((working = malloc(sizeof(struct conf_entry))) ==
315 if ((working->next = malloc(sizeof(struct conf_entry)))
318 working = working->next;
320 if ((working->log = strdup(q)) == NULL)
323 q = parse = missing_field(sob(++parse), errline);
326 errx(1, "malformed line (missing fields):\n%s",
329 if ((group = strchr(q, ':')) != NULL ||
330 (group = strrchr(q, '.')) != NULL) {
333 if (!(isnumber(*q))) {
334 if ((pass = getpwnam(q)) == NULL)
336 "error in config file; unknown user:\n%s",
338 working->uid = pass->pw_uid;
340 working->uid = atoi(q);
346 if (!(isnumber(*q))) {
347 if ((grp = getgrnam(q)) == NULL)
349 "error in config file; unknown group:\n%s",
351 working->gid = grp->gr_gid;
353 working->gid = atoi(q);
357 q = parse = missing_field(sob(++parse), errline);
360 errx(1, "malformed line (missing fields):\n%s",
364 working->uid = working->gid = NONE;
366 if (!sscanf(q, "%o", &working->permissions))
367 errx(1, "error in config file; bad permissions:\n%s",
370 q = parse = missing_field(sob(++parse), errline);
373 errx(1, "malformed line (missing fields):\n%s",
376 if (!sscanf(q, "%d", &working->numlogs))
377 errx(1, "error in config file; bad number:\n%s",
380 q = parse = missing_field(sob(++parse), errline);
383 errx(1, "malformed line (missing fields):\n%s",
387 working->size = atoi(q);
392 q = parse = missing_field(sob(++parse), errline);
400 ul = strtoul(q, &ep, 10);
405 else if (ul > INT_MAX)
406 errx(1, "interval is too large:\n%s", errline);
410 if (*ep != '\0' && *ep != '@' && *ep != '*' &&
412 errx(1, "malformed interval/at:\n%s", errline);
414 if ((working->trim_at = parse8601(ep + 1))
416 errx(1, "malformed at:\n%s", errline);
417 working->flags |= CE_TRIMAT;
418 } else if (*ep == '$') {
419 if ((working->trim_at = parseDWM(ep + 1))
421 errx(1, "malformed at:\n%s", errline);
422 working->flags |= CE_TRIMAT;
429 q = parse = sob(++parse); /* Optional field */
436 while (q && *q && !isspace(*q)) {
437 if ((*q == 'Z') || (*q == 'z'))
438 working->flags |= CE_COMPACT;
439 else if ((*q == 'J') || (*q == 'j'))
440 working->flags |= CE_BZCOMPACT;
441 else if ((*q == 'B') || (*q == 'b'))
442 working->flags |= CE_BINARY;
444 errx(1, "illegal flag in config file -- %c",
452 q = parse = sob(++parse); /* Optional field */
459 working->pid_file = NULL;
462 working->pid_file = strdup(q);
463 else if (isdigit(*q))
467 "illegal pid file or signal number in config file:\n%s",
473 q = parse = sob(++parse); /* Optional field */
474 *(parse = son(parse)) = '\0';
477 working->sig = SIGHUP;
481 working->sig = atoi(q);
485 "illegal signal number in config file:\n%s",
488 if (working->sig < 1 || working->sig >= NSIG)
494 working->next = (struct conf_entry *) NULL;
500 missing_field(char *p, char *errline)
504 errx(1, "missing field in config file:\n%s", errline);
509 dotrim(char *log, const char *pid_file, int numdays, int flags, int perm,
510 int owner_uid, int group_gid, int sig)
512 char dirpart[MAXPATHLEN], namepart[MAXPATHLEN];
513 char file1[MAXPATHLEN], file2[MAXPATHLEN];
514 char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
515 char jfile1[MAXPATHLEN];
516 int notified, need_notification, fd, _numdays;
522 * AIX 3.1 has a broken fchown- if the owner_uid is -1, it will
523 * actually change it to be owned by uid -1, instead of leaving it
524 * as is, as it is supposed to.
527 owner_uid = geteuid();
533 /* build complete name of archive directory into dirpart */
534 if (*archdirname == '/') { /* absolute */
535 strlcpy(dirpart, archdirname, sizeof(dirpart));
536 } else { /* relative */
537 /* get directory part of logfile */
538 strlcpy(dirpart, log, sizeof(dirpart));
539 if ((p = rindex(dirpart, '/')) == NULL)
543 strlcat(dirpart, archdirname, sizeof(dirpart));
546 /* check if archive directory exists, if not, create it */
547 if (lstat(dirpart, &st))
550 /* get filename part of logfile */
551 if ((p = rindex(log, '/')) == NULL)
552 strlcpy(namepart, log, sizeof(namepart));
554 strlcpy(namepart, p + 1, sizeof(namepart));
556 /* name of oldest log */
557 (void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart,
559 (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
561 snprintf(jfile1, sizeof(jfile1), "%s%s", file1,
564 /* name of oldest log */
565 (void) snprintf(file1, sizeof(file1), "%s.%d", log, numdays);
566 (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
568 snprintf(jfile1, sizeof(jfile1), "%s%s", file1,
573 printf("rm -f %s\n", file1);
574 printf("rm -f %s\n", zfile1);
575 printf("rm -f %s\n", jfile1);
577 (void) unlink(file1);
578 (void) unlink(zfile1);
579 (void) unlink(jfile1);
582 /* Move down log files */
583 _numdays = numdays; /* preserve */
586 (void) strlcpy(file2, file1, sizeof(file2));
589 (void) snprintf(file1, sizeof(file1), "%s/%s.%d",
590 dirpart, namepart, numdays);
592 (void) snprintf(file1, sizeof(file1), "%s.%d", log,
595 (void) strlcpy(zfile1, file1, sizeof(zfile1));
596 (void) strlcpy(zfile2, file2, sizeof(zfile2));
597 if (lstat(file1, &st)) {
598 (void) strlcat(zfile1, COMPRESS_POSTFIX,
600 (void) strlcat(zfile2, COMPRESS_POSTFIX,
602 if (lstat(zfile1, &st)) {
603 strlcpy(zfile1, file1, sizeof(zfile1));
604 strlcpy(zfile2, file2, sizeof(zfile2));
605 strlcat(zfile1, BZCOMPRESS_POSTFIX,
607 strlcat(zfile2, BZCOMPRESS_POSTFIX,
609 if (lstat(zfile1, &st))
614 printf("mv %s %s\n", zfile1, zfile2);
615 printf("chmod %o %s\n", perm, zfile2);
616 printf("chown %d:%d %s\n",
617 owner_uid, group_gid, zfile2);
619 (void) rename(zfile1, zfile2);
620 (void) chmod(zfile2, perm);
621 (void) chown(zfile2, owner_uid, group_gid);
624 if (!noaction && !(flags & CE_BINARY))
625 (void) log_trim(log); /* Report the trimming to the old log */
629 printf("rm %s\n", log);
634 printf("mv %s to %s\n", log, file1);
637 movefile(log, file1, perm, owner_uid,
640 (void) rename(log, file1);
645 printf("Start new log...");
647 fd = creat(log, perm);
649 err(1, "can't start new log");
650 if (fchown(fd, owner_uid, group_gid))
651 err(1, "can't chmod new log file");
653 if (!(flags & CE_BINARY))
654 if (log_trim(log)) /* Add status message */
655 err(1, "can't add status message to log");
658 printf("chmod %o %s...\n", perm, log);
660 (void) chmod(log, perm);
663 need_notification = notified = 0;
664 if (pid_file != NULL) {
665 need_notification = 1;
666 pid = get_pid(pid_file);
671 printf("kill -%d %d\n", sig, (int) pid);
672 } else if (kill(pid, sig))
673 warn("can't notify daemon, pid %d", (int) pid);
677 printf("daemon pid %d notified\n", (int) pid);
680 if ((flags & CE_COMPACT) || (flags & CE_BZCOMPACT)) {
681 if (need_notification && !notified)
683 "log %s not compressed because daemon not notified",
686 printf("Compress %s.0\n", log);
690 printf("small pause to allow daemon to close log\n");
694 (void) snprintf(file1, sizeof(file1), "%s/%s",
696 if (flags & CE_COMPACT)
698 else if (flags & CE_BZCOMPACT)
699 bzcompress_log(file1);
701 if (flags & CE_COMPACT)
703 else if (flags & CE_BZCOMPACT)
710 /* Log the fact that the logs were turned over */
716 if ((f = fopen(log, "a")) == NULL)
718 fprintf(f, "%s %s newsyslog[%d]: logfile turned over\n",
719 daytime, hostname, (int) getpid());
720 if (fclose(f) == EOF)
721 err(1, "log_trim: fclose:");
725 /* Fork of gzip to compress the old log file */
727 compress_log(char *log)
730 char tmp[MAXPATHLEN];
732 (void) snprintf(tmp, sizeof(tmp), "%s.0", log);
737 (void) execl(_PATH_GZIP, _PATH_GZIP, "-f", tmp, (char *)0);
742 /* Fork of bzip2 to compress the old log file */
744 bzcompress_log(char *log)
747 char tmp[MAXPATHLEN];
749 snprintf(tmp, sizeof(tmp), "%s.0", log);
752 err(1, "bzip2 fork");
754 execl(_PATH_BZIP2, _PATH_BZIP2, "-f", tmp, 0);
759 /* Return size in kilobytes of a file */
765 if (stat(file, &sb) < 0)
767 return (kbytes(dbtob(sb.st_blocks)));
770 /* Return the age of old log file (file.0) */
772 age_old_log(char *file)
775 char tmp[MAXPATHLEN + sizeof(".0") + sizeof(COMPRESS_POSTFIX) + 1];
780 /* build name of archive directory into tmp */
781 if (*archdirname == '/') { /* absolute */
782 strlcpy(tmp, archdirname, sizeof(tmp));
783 } else { /* relative */
784 /* get directory part of logfile */
785 strlcpy(tmp, file, sizeof(tmp));
786 if ((p = rindex(tmp, '/')) == NULL)
790 strlcat(tmp, archdirname, sizeof(tmp));
793 strlcat(tmp, "/", sizeof(tmp));
795 /* get filename part of logfile */
796 if ((p = rindex(file, '/')) == NULL)
797 strlcat(tmp, file, sizeof(tmp));
799 strlcat(tmp, p + 1, sizeof(tmp));
801 (void) strlcpy(tmp, file, sizeof(tmp));
804 if (stat(strcat(tmp, ".0"), &sb) < 0)
805 if (stat(strcat(tmp, COMPRESS_POSTFIX), &sb) < 0)
807 return ((int) (timenow - sb.st_mtime + 1800) / 3600);
811 get_pid(const char *pid_file)
817 if ((f = fopen(pid_file, "r")) == NULL)
818 warn("can't open %s pid file to restart a daemon",
821 if (fgets(line, BUFSIZ, f)) {
823 if (pid < MIN_PID || pid > MAX_PID) {
824 warnx("preposterous process number: %d",
829 warn("can't read %s pid file to restart a daemon",
836 /* Skip Over Blanks */
840 while (p && *p && isspace(*p))
845 /* Skip Over Non-Blanks */
849 while (p && *p && !isspace(*p))
855 * Parse a limited subset of ISO 8601. The specific format is as follows:
857 * [CC[YY[MM[DD]]]][THH[MM[SS]]] (where `T' is the literal letter)
859 * We don't accept a timezone specification; missing fields (including timezone)
860 * are defaulted to the current date but time zero.
869 tmp = localtime(&timenow);
872 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
874 ul = strtoul(s, &t, 10);
875 if (*t != '\0' && *t != 'T')
879 * Now t points either to the end of the string (if no time was
880 * provided) or to the letter `T' which separates date and time in
881 * ISO 8601. The pointer arithmetic is the same for either case.
885 tm.tm_year = ((ul / 1000000) - 19) * 100;
888 tm.tm_year -= tm.tm_year % 100;
889 tm.tm_year += ul / 10000;
892 tm.tm_mon = (ul / 100) - 1;
903 if (tm.tm_year < 70 || tm.tm_mon < 0 || tm.tm_mon > 12
904 || tm.tm_mday < 1 || tm.tm_mday > 31)
909 ul = strtoul(s, &t, 10);
910 if (*t != '\0' && !isspace(*t))
915 tm.tm_sec = ul % 100;
918 tm.tm_min = ul % 100;
929 if (tm.tm_sec < 0 || tm.tm_sec > 60 || tm.tm_min < 0
930 || tm.tm_min > 59 || tm.tm_hour < 0 || tm.tm_hour > 23)
936 /* physically move file */
938 movefile(char *from, char *to, int perm, int owner_uid, int group_gid)
943 if ((src = fopen(from, "r")) == NULL)
944 err(1, "can't fopen %s for reading", from);
945 if ((dst = fopen(to, "w")) == NULL)
946 err(1, "can't fopen %s for writing", to);
947 if (fchown(fileno(dst), owner_uid, group_gid))
948 err(1, "can't fchown %s", to);
949 if (fchmod(fileno(dst), perm))
950 err(1, "can't fchmod %s", to);
952 while ((c = getc(src)) != EOF) {
953 if ((putc(c, dst)) == EOF)
954 err(1, "error writing to %s", to);
958 err(1, "error reading from %s", from);
959 if ((fclose(src)) != 0)
960 err(1, "can't fclose %s", to);
961 if ((fclose(dst)) != 0)
962 err(1, "can't fclose %s", from);
963 if ((unlink(from)) != 0)
964 err(1, "can't unlink %s", from);
967 /* create one or more directory components of a path */
969 createdir(char *dirpart)
972 char mkdirpath[MAXPATHLEN];
980 if (*s == '/' || *s == '\0') {
982 if (lstat(mkdirpath, &st))
983 mkdir(mkdirpath, 0755);
991 * Parse a cyclic time specification, the format is as follows:
993 * [Dhh] or [Wd[Dhh]] or [Mdd[Dhh]]
995 * to rotate a logfile cyclic at
997 * - every day (D) within a specific hour (hh) (hh = 0...23)
998 * - once a week (W) at a specific day (d) OR (d = 0..6, 0 = Sunday)
999 * - once a month (M) at a specific day (d) (d = 1..31,l|L)
1001 * We don't accept a timezone specification; missing fields
1002 * are defaulted to the current date but time zero.
1011 static int mtab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1015 tmp = localtime(&timenow);
1018 /* set no. of days per month */
1020 nd = mtab[tm.tm_mon];
1022 if (tm.tm_mon == 1) {
1023 if (((tm.tm_year + 1900) % 4 == 0) &&
1024 ((tm.tm_year + 1900) % 100 != 0) &&
1025 ((tm.tm_year + 1900) % 400 == 0)) {
1026 nd++; /* leap year, 29 days in february */
1029 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
1038 ul = strtoul(s, &t, 10);
1049 ul = strtoul(s, &t, 10);
1052 if (ul != (unsigned)tm.tm_wday) {
1055 if (ul < (unsigned)tm.tm_wday) {
1056 save = 6 - tm.tm_wday;
1059 save = ul - tm.tm_wday;
1064 if (tm.tm_mday > nd) {
1066 tm.tm_mday = tm.tm_mday - nd;
1076 if (tolower(*s) == 'l') {
1081 ul = strtoul(s, &t, 10);
1082 if (ul < 1 || ul > 31)
1085 if (ul > (unsigned)nd)
1096 if (*t == '\0' || isspace(*t))