Annotation of src/usr.bin/newsyslog/newsyslog.c, Revision 1.27
1.27 ! millert 1: /* $OpenBSD: newsyslog.c,v 1.26 1999/11/07 05:16:28 millert Exp $ */
1.10 downsj 2:
3: /*
4: * Copyright (c) 1997, Jason Downs. All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: * 3. All advertising materials mentioning features or use of this software
15: * must display the following acknowledgement:
16: * This product includes software developed by Jason Downs for the
17: * OpenBSD system.
18: * 4. Neither the name(s) of the author(s) nor the name OpenBSD
19: * may be used to endorse or promote products derived from this software
20: * without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
23: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25: * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
26: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32: * SUCH DAMAGE.
33: */
1.3 deraadt 34:
1.1 deraadt 35: /*
36: * This file contains changes from the Open Software Foundation.
37: */
38:
39: /*
40:
41: Copyright 1988, 1989 by the Massachusetts Institute of Technology
42:
43: Permission to use, copy, modify, and distribute this software
44: and its documentation for any purpose and without fee is
45: hereby granted, provided that the above copyright notice
46: appear in all copies and that both that copyright notice and
47: this permission notice appear in supporting documentation,
48: and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
49: used in advertising or publicity pertaining to distribution
50: of the software without specific, written prior permission.
51: M.I.T. and the M.I.T. S.I.P.B. make no representations about
52: the suitability of this software for any purpose. It is
53: provided "as is" without express or implied warranty.
54:
55: */
56:
57: /*
58: * newsyslog - roll over selected logs at the appropriate time,
59: * keeping the a specified number of backup files around.
60: *
61: */
62:
63: #ifndef lint
1.27 ! millert 64: static char rcsid[] = "$OpenBSD: newsyslog.c,v 1.26 1999/11/07 05:16:28 millert Exp $";
1.1 deraadt 65: #endif /* not lint */
66:
67: #ifndef CONF
68: #define CONF "/etc/athena/newsyslog.conf" /* Configuration file */
69: #endif
70: #ifndef PIDFILE
71: #define PIDFILE "/etc/syslog.pid"
72: #endif
73: #ifndef COMPRESS
74: #define COMPRESS "/usr/ucb/compress" /* File compression program */
75: #endif
76: #ifndef COMPRESS_POSTFIX
77: #define COMPRESS_POSTFIX ".Z"
78: #endif
1.10 downsj 79: #ifndef STATS_DIR
80: #define STATS_DIR "/etc"
81: #endif
82: #ifndef SENDMAIL
83: #define SENDMAIL "/usr/lib/sendmail"
84: #endif
1.1 deraadt 85:
86: #include <stdio.h>
1.9 downsj 87: #include <sys/types.h>
88: #include <sys/time.h>
89: #include <sys/stat.h>
90: #include <sys/param.h>
91: #include <sys/wait.h>
1.1 deraadt 92: #include <stdlib.h>
93: #include <string.h>
94: #include <ctype.h>
95: #include <signal.h>
1.9 downsj 96: #include <fcntl.h>
1.1 deraadt 97: #include <pwd.h>
98: #include <grp.h>
1.9 downsj 99: #include <unistd.h>
1.11 downsj 100: #include <err.h>
1.1 deraadt 101:
1.26 millert 102: #define CE_ROTATED 0x01 /* Log file has been rotated */
103: #define CE_COMPACT 0x02 /* Compact the achived log files */
104: #define CE_BINARY 0x04 /* Logfile is in binary, don't add */
1.10 downsj 105: /* status messages */
1.26 millert 106: #define CE_MONITOR 0x08 /* Monitory for changes */
1.1 deraadt 107: #define NONE -1
108:
109: struct conf_entry {
110: char *log; /* Name of the log */
1.25 millert 111: uid_t uid; /* Owner of log */
112: gid_t gid; /* Group of log */
1.1 deraadt 113: int numlogs; /* Number of logs to keep */
114: int size; /* Size cutoff to trigger trimming the log */
115: int hours; /* Hours between log trimming */
116: int permissions; /* File permissions on the log */
117: int flags; /* Flags (CE_COMPACT & CE_BINARY) */
1.10 downsj 118: char *whom; /* Whom to notify if logfile changes */
1.14 millert 119: char *pidfile; /* Path to file containg pid to HUP */
1.1 deraadt 120: struct conf_entry *next; /* Linked list pointer */
121: };
122:
123: int verbose = 0; /* Print out what's going on */
124: int needroot = 1; /* Root privs are necessary */
125: int noaction = 0; /* Don't do anything, just show it */
1.10 downsj 126: int monitor = 0; /* Don't do monitoring by default */
1.1 deraadt 127: char *conf = CONF; /* Configuration file to use */
128: time_t timenow;
1.16 millert 129: #define MIN_PID 4
1.7 deraadt 130: char hostname[MAXHOSTNAMELEN]; /* hostname */
1.1 deraadt 131: char *daytime; /* timenow in human readable form */
132:
133:
1.9 downsj 134: void do_entry __P((struct conf_entry *));
135: void PRS __P((int, char **));
136: void usage __P((void));
137: struct conf_entry *parse_file __P((void));
138: char *missing_field __P((char *, char *));
1.26 millert 139: void dotrim __P((char *, int, int, int, uid_t, gid_t));
1.9 downsj 140: int log_trim __P((char *));
141: void compress_log __P((char *));
142: int sizefile __P((char *));
1.24 millert 143: int age_old_log __P((char *));
1.9 downsj 144: char *sob __P((char *));
145: char *son __P((char *));
1.10 downsj 146: int isnumberstr __P((char *));
147: void domonitor __P((char *, char *));
148: FILE *openmail __P((void));
149: void closemail __P((FILE *));
1.16 millert 150: void child_killer __P((int));
1.26 millert 151: void send_hup __P((char *));
1.1 deraadt 152:
1.25 millert 153: int
154: main(argc, argv)
1.1 deraadt 155: int argc;
156: char **argv;
157: {
158: struct conf_entry *p, *q;
1.16 millert 159: int status;
1.1 deraadt 160:
1.25 millert 161: PRS(argc, argv);
1.11 downsj 162: if (needroot && getuid() && geteuid())
163: errx(1, "You must be root.");
1.1 deraadt 164: p = q = parse_file();
1.16 millert 165: signal(SIGCHLD, child_killer);
1.26 millert 166:
167: /* Step 1, rotate all log files */
168: while (q) {
169: do_entry(q);
170: q = q->next;
171: }
172:
173: /* Step 2, send a HUP to relevant processes */
174: /* XXX - should avoid HUP'ing the same process multiple times */
175: q = p;
176: while (q) {
177: if (q->flags & CE_ROTATED)
178: send_hup(q->pidfile);
179: q = q->next;
180: }
181:
182: /* Step 3, compress the log.0 file if configured to do so and free */
1.1 deraadt 183: while (p) {
1.26 millert 184: if ((p->flags & CE_COMPACT) && (p->flags & CE_ROTATED))
185: compress_log(p->log);
186: q = p;
1.25 millert 187: p = p->next;
1.11 downsj 188: free(q);
1.1 deraadt 189: }
1.16 millert 190:
191: /* Wait for children to finish, then exit */
192: while (waitpid(-1, &status, 0) != -1)
193: ;
1.1 deraadt 194: exit(0);
195: }
196:
1.25 millert 197: void
198: do_entry(ent)
1.1 deraadt 199: struct conf_entry *ent;
200:
201: {
1.25 millert 202: int modtime, size;
1.14 millert 203:
1.19 kstailey 204: if (verbose)
205: printf("%s <%d%s>: ", ent->log, ent->numlogs,
206: (ent->flags & CE_COMPACT) ? "Z" : "");
1.1 deraadt 207: size = sizefile(ent->log);
1.24 millert 208: modtime = age_old_log(ent->log);
1.1 deraadt 209: if (size < 0) {
210: if (verbose)
211: printf("does not exist.\n");
212: } else {
213: if (verbose && (ent->size > 0))
214: printf("size (Kb): %d [%d] ", size, ent->size);
215: if (verbose && (ent->hours > 0))
216: printf(" age (hr): %d [%d] ", modtime, ent->hours);
1.10 downsj 217: if (monitor && ent->flags & CE_MONITOR)
218: domonitor(ent->log, ent->whom);
1.25 millert 219: if (!monitor && (((ent->size > 0) && (size >= ent->size)) ||
1.1 deraadt 220: ((ent->hours > 0) && ((modtime >= ent->hours)
1.25 millert 221: || (modtime < 0))))) {
1.1 deraadt 222: if (verbose)
223: printf("--> trimming log....\n");
1.19 kstailey 224: if (noaction && !verbose)
225: printf("%s <%d%s>: ", ent->log, ent->numlogs,
226: (ent->flags & CE_COMPACT) ? "Z" : "");
1.1 deraadt 227: dotrim(ent->log, ent->numlogs, ent->flags,
1.26 millert 228: ent->permissions, ent->uid, ent->gid);
229: ent->flags |= CE_ROTATED;
1.25 millert 230: } else if (verbose)
231: printf("--> skipping\n");
1.1 deraadt 232: }
233: }
234:
1.26 millert 235: /* Send a HUP to the pid specified by pidfile */
236: void
237: send_hup(pidfile)
238: char *pidfile;
239: {
240: FILE *f;
241: char line[BUFSIZ];
242: pid_t pid = 0;
243:
244: if ((f = fopen(pidfile, "r")) == NULL) {
245: warn("can't open %s", pidfile);
246: return;
247: }
248:
249: if (fgets(line, sizeof(line), f))
250: pid = atoi(line);
251: (void)fclose(f);
252:
253: if (noaction)
254: (void)printf("kill -HUP %d\n", pid);
255: else if (pid == 0)
256: warnx("empty pid file: %s", pidfile);
257: else if (pid < MIN_PID)
258: warnx("preposterous process number: %d", pid);
259: else if (kill(pid, SIGHUP))
260: warnx("warning - could not HUP daemon");
261: }
262:
1.25 millert 263: void
264: PRS(argc, argv)
1.1 deraadt 265: int argc;
266: char **argv;
267: {
268: int c;
269: char *p;
270:
1.12 kstailey 271: timenow = time(NULL);
1.1 deraadt 272: daytime = ctime(&timenow) + 4;
1.2 deraadt 273: daytime[15] = '\0';
1.1 deraadt 274:
275: /* Let's get our hostname */
1.25 millert 276: (void)gethostname(hostname, sizeof(hostname));
1.1 deraadt 277:
278: /* Truncate domain */
1.9 downsj 279: p = strchr(hostname, '.');
280: if (p)
1.1 deraadt 281: *p = '\0';
282:
283: optind = 1; /* Start options parsing */
1.25 millert 284: while ((c = getopt(argc, argv, "nrvmf:t:")) != -1) {
1.1 deraadt 285: switch (c) {
286: case 'n':
287: noaction++; /* This implies needroot as off */
288: /* fall through */
289: case 'r':
290: needroot = 0;
291: break;
292: case 'v':
293: verbose++;
294: break;
295: case 'f':
296: conf = optarg;
297: break;
1.10 downsj 298: case 'm':
299: monitor++;
300: break;
1.1 deraadt 301: default:
302: usage();
303: }
304: }
1.9 downsj 305: }
1.1 deraadt 306:
1.25 millert 307: void
308: usage()
1.1 deraadt 309: {
1.25 millert 310: extern const char *__progname;
311:
312: (void)fprintf(stderr, "usage: %s [-nrvm] [-f config-file]\n",
313: __progname);
1.14 millert 314: exit(1);
1.1 deraadt 315: }
316:
317: /* Parse a configuration file and return a linked list of all the logs
318: * to process
319: */
1.25 millert 320: struct conf_entry *
321: parse_file()
1.1 deraadt 322: {
323: FILE *f;
324: char line[BUFSIZ], *parse, *q;
1.25 millert 325: char *errline, *group, *tmp;
1.1 deraadt 326: struct conf_entry *first = NULL;
327: struct conf_entry *working;
328: struct passwd *pass;
329: struct group *grp;
330:
1.25 millert 331: if (strcmp(conf, "-") == 0)
1.1 deraadt 332: f = stdin;
1.25 millert 333: else {
334: if ((f = fopen(conf, "r")) == NULL)
335: err(1, "can't open %s", conf);
336: }
1.11 downsj 337:
1.25 millert 338: while (fgets(line, sizeof(line), f)) {
339: if ((line[0] == '\n') || (line[0] == '#'))
1.1 deraadt 340: continue;
341: errline = strdup(line);
1.11 downsj 342: if (errline == NULL)
343: err(1, "strdup");
1.1 deraadt 344: if (!first) {
345: working = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11 downsj 346: if (working == NULL)
347: err(1, "malloc");
1.1 deraadt 348: first = working;
349: } else {
350: working->next = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11 downsj 351: if (working->next == NULL)
352: err(1, "malloc");
1.1 deraadt 353: working = working->next;
354: }
355:
1.25 millert 356: q = parse = missing_field(sob(line), errline);
1.1 deraadt 357: *(parse = son(line)) = '\0';
358: working->log = strdup(q);
1.11 downsj 359: if (working->log == NULL)
360: err(1, "strdup");
1.1 deraadt 361:
1.25 millert 362: q = parse = missing_field(sob(++parse), errline);
1.1 deraadt 363: *(parse = son(parse)) = '\0';
1.25 millert 364: if ((group = strchr(q, '.')) != NULL) {
365: *group++ = '\0';
366: if (*q) {
367: if (!(isnumberstr(q))) {
368: if ((pass = getpwnam(q)) == NULL)
369: errx(1, "Error in config file; unknown user: %s", q);
370: working->uid = pass->pw_uid;
371: } else
372: working->uid = atoi(q);
373: } else
374: working->uid = NONE;
375:
376: q = group;
377: if (*q) {
378: if (!(isnumberstr(q))) {
379: if ((grp = getgrnam(q)) == NULL)
380: errx(1, "Error in config file; unknown group: %s", q);
381: working->gid = grp->gr_gid;
382: } else
383: working->gid = atoi(q);
384: } else
385: working->gid = NONE;
386:
387: q = parse = missing_field(sob(++parse), errline);
388: *(parse = son(parse)) = '\0';
389: } else
390: working->uid = working->gid = NONE;
1.1 deraadt 391:
1.25 millert 392: if (!sscanf(q, "%o", &working->permissions))
1.11 downsj 393: errx(1, "Error in config file; bad permissions: %s", q);
1.1 deraadt 394:
1.25 millert 395: q = parse = missing_field(sob(++parse), errline);
1.1 deraadt 396: *(parse = son(parse)) = '\0';
1.25 millert 397: if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0)
1.11 downsj 398: errx(1, "Error in config file; bad number: %s", q);
1.1 deraadt 399:
1.25 millert 400: q = parse = missing_field(sob(++parse), errline);
1.1 deraadt 401: *(parse = son(parse)) = '\0';
402: if (isdigit(*q))
403: working->size = atoi(q);
404: else
405: working->size = -1;
406:
1.25 millert 407: q = parse = missing_field(sob(++parse), errline);
1.1 deraadt 408: *(parse = son(parse)) = '\0';
409: if (isdigit(*q))
410: working->hours = atoi(q);
411: else
412: working->hours = -1;
413:
414: q = parse = sob(++parse); /* Optional field */
415: *(parse = son(parse)) = '\0';
416: working->flags = 0;
417: while (q && *q && !isspace(*q)) {
418: if ((*q == 'Z') || (*q == 'z'))
419: working->flags |= CE_COMPACT;
420: else if ((*q == 'B') || (*q == 'b'))
421: working->flags |= CE_BINARY;
1.10 downsj 422: else if ((*q == 'M') || (*q == 'm'))
423: working->flags |= CE_MONITOR;
1.11 downsj 424: else
425: errx(1, "Illegal flag in config file: %c", *q);
1.1 deraadt 426: q++;
427: }
1.10 downsj 428:
429: working->whom = NULL;
430: if (working->flags & CE_MONITOR) { /* Optional field */
431: q = parse = sob(++parse);
432: *(parse = son(parse)) = '\0';
433:
434: working->whom = strdup(q);
1.11 downsj 435: if (working->log == NULL)
436: err(1, "strdup");
1.10 downsj 437: }
1.14 millert 438:
439: working->pidfile = PIDFILE;
440: q = parse = sob(++parse); /* Optional field */
441: *(parse = son(parse)) = '\0';
442: if (q && *q != '\0') {
1.25 millert 443: if (strlen(q) >= MAXPATHLEN)
444: errx(1, "%s: pathname too long", q);
1.14 millert 445: working->pidfile = strdup(q);
446: if (working->pidfile == NULL)
447: err(1, "strdup");
448: }
1.25 millert 449:
450: /* Make sure we can't oflow MAXPATHLEN */
451: if (asprintf(&tmp, "%s.%d%s", working->log, working->numlogs,
452: COMPRESS_POSTFIX) >= MAXPATHLEN)
453: errx(1, "%s: pathname too long", working->log);
1.1 deraadt 454:
1.25 millert 455: free(tmp);
1.1 deraadt 456: free(errline);
457: }
458: if (working)
1.12 kstailey 459: working->next = NULL;
1.25 millert 460: (void)fclose(f);
1.1 deraadt 461: return(first);
462: }
463:
1.25 millert 464: char *
465: missing_field(p, errline)
466: char *p;
467: char *errline;
1.1 deraadt 468: {
469: if (!p || !*p) {
1.14 millert 470: warnx("Missing field in config file line:");
1.11 downsj 471: fputs(errline, stderr);
1.1 deraadt 472: exit(1);
473: }
474: return(p);
475: }
476:
1.25 millert 477: void
1.26 millert 478: dotrim(log, numdays, flags, perm, owner_uid, group_gid)
1.1 deraadt 479: char *log;
480: int numdays;
481: int flags;
482: int perm;
1.25 millert 483: uid_t owner_uid;
484: gid_t group_gid;
1.1 deraadt 485: {
1.7 deraadt 486: char file1[MAXPATHLEN], file2[MAXPATHLEN];
487: char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
1.1 deraadt 488: int fd;
489: struct stat st;
1.6 tholo 490: int days = numdays;
1.1 deraadt 491:
1.25 millert 492: /* Remove oldest log (may not exist) */
493: (void)sprintf(file1, "%s.%d", log, numdays);
494: (void)strcpy(zfile1, file1);
495: (void)strcat(zfile1, COMPRESS_POSTFIX);
1.1 deraadt 496: if (noaction) {
1.26 millert 497: printf("rm -f %s %s\n", file1, zfile1);
1.1 deraadt 498: } else {
1.25 millert 499: (void)unlink(file1);
500: (void)unlink(zfile1);
1.1 deraadt 501: }
502:
503: /* Move down log files */
504: while (numdays--) {
1.25 millert 505: (void)strcpy(file2, file1);
506: (void)sprintf(file1, "%s.%d", log, numdays);
507: (void)strcpy(zfile1, file1);
508: (void)strcpy(zfile2, file2);
1.1 deraadt 509: if (lstat(file1, &st)) {
1.25 millert 510: (void)strcat(zfile1, COMPRESS_POSTFIX);
511: (void)strcat(zfile2, COMPRESS_POSTFIX);
512: if (lstat(zfile1, &st))
513: continue;
1.1 deraadt 514: }
515: if (noaction) {
1.25 millert 516: printf("mv %s %s\n", zfile1, zfile2);
1.1 deraadt 517: printf("chmod %o %s\n", perm, zfile2);
1.25 millert 518: printf("chown %d:%d %s\n",
1.1 deraadt 519: owner_uid, group_gid, zfile2);
520: } else {
1.25 millert 521: if (rename(zfile1, zfile2))
522: warn("can't mv %s to %s", zfile1, zfile2);
523: if (chmod(zfile2, perm))
524: warn("can't chmod %s", zfile2);
525: if (chown(zfile2, owner_uid, group_gid))
526: warn("can't chown %s", zfile2);
1.1 deraadt 527: }
528: }
529: if (!noaction && !(flags & CE_BINARY))
1.25 millert 530: (void)log_trim(log); /* Report the trimming to the old log */
1.1 deraadt 531:
1.26 millert 532: (void)snprintf(file2, sizeof(file2), "%s.XXXXXXXXXX", log);
533: if (noaction) {
534: printf("Create new log file...\n");
535: } else {
536: if ((fd = mkstemp(file2)) < 0)
537: err(1, "can't start '%s' log", file2);
538: if (fchown(fd, owner_uid, group_gid))
539: err(1, "can't chown '%s' log file", file2);
540: if (fchmod(fd, perm))
541: err(1, "can't chmod '%s' log file", file2);
542: (void)close(fd);
543: /* Add status message */
544: if (!(flags & CE_BINARY) && log_trim(file2))
545: err(1, "can't add status message to log '%s'", file2);
546: }
547:
1.6 tholo 548: if (days == 0) {
1.5 deraadt 549: if (noaction)
1.25 millert 550: printf("rm %s\n", log);
551: else if (unlink(log))
552: warn("can't rm %s", log);
1.5 deraadt 553: } else {
554: if (noaction)
1.25 millert 555: printf("mv %s to %s\n", log, file1);
556: else if (rename(log, file1))
557: warn("can't to mv %s to %s", log, file1);
1.5 deraadt 558: }
559:
1.26 millert 560: /* Now move the new log file into place */
561: if (noaction)
562: printf("mv %s to %s\n", file2, log);
563: else if (rename(file2, log))
564: warn("can't to mv %s to %s", file2, log);
1.1 deraadt 565: }
566:
567: /* Log the fact that the logs were turned over */
1.25 millert 568: int
569: log_trim(log)
1.17 deraadt 570: char *log;
1.1 deraadt 571: {
572: FILE *f;
1.25 millert 573:
574: if ((f = fopen(log, "a")) == NULL)
1.1 deraadt 575: return(-1);
1.25 millert 576: (void)fprintf(f, "%s %s newsyslog[%d]: logfile turned over\n",
577: daytime, hostname, getpid());
1.24 millert 578: if (fclose(f) == EOF)
1.11 downsj 579: err(1, "log_trim: fclose");
1.1 deraadt 580: return(0);
581: }
582:
1.16 millert 583: /* Fork off compress or gzip to compress the old log file */
1.25 millert 584: void
585: compress_log(log)
1.1 deraadt 586: char *log;
587: {
1.25 millert 588: pid_t pid;
1.27 ! millert 589: char *base;
1.7 deraadt 590: char tmp[MAXPATHLEN];
1.1 deraadt 591:
1.27 ! millert 592: if ((base = strrchr(COMPRESS, '/')) == NULL)
! 593: base = COMPRESS;
! 594: else
! 595: base++;
! 596: if (noaction) {
! 597: printf("%s %s.0\n", base, log);
! 598: return;
! 599: }
1.1 deraadt 600: pid = fork();
1.25 millert 601: (void)sprintf(tmp, "%s.0", log);
1.1 deraadt 602: if (pid < 0) {
1.11 downsj 603: err(1, "fork");
1.1 deraadt 604: } else if (!pid) {
1.27 ! millert 605: (void)execl(COMPRESS, base, "-f", tmp, 0);
1.16 millert 606: warn(COMPRESS);
607: _exit(1);
1.1 deraadt 608: }
609: }
610:
611: /* Return size in kilobytes of a file */
1.25 millert 612: int
613: sizefile(file)
1.1 deraadt 614: char *file;
615: {
616: struct stat sb;
617:
1.25 millert 618: if (stat(file, &sb) < 0)
1.1 deraadt 619: return(-1);
1.25 millert 620: return(sb.st_blocks / (1024.0 / DEV_BSIZE));
1.1 deraadt 621: }
622:
1.25 millert 623: /* Return the age (in hours) of old log file (file.0), or -1 if none */
624: int
625: age_old_log(file)
1.1 deraadt 626: char *file;
627: {
628: struct stat sb;
1.7 deraadt 629: char tmp[MAXPATHLEN];
1.1 deraadt 630:
1.25 millert 631: (void)strcpy(tmp, file);
632: if (stat(strcat(tmp, ".0"), &sb) < 0)
633: if (stat(strcat(tmp, COMPRESS_POSTFIX), &sb) < 0)
1.1 deraadt 634: return(-1);
1.24 millert 635: return( (int) (timenow - sb.st_mtime + 1800) / 3600);
1.1 deraadt 636: }
637:
638: /* Skip Over Blanks */
1.25 millert 639: char *
640: sob(p)
1.1 deraadt 641: register char *p;
642: {
643: while (p && *p && isspace(*p))
644: p++;
645: return(p);
646: }
647:
648: /* Skip Over Non-Blanks */
1.25 millert 649: char *
650: son(p)
1.1 deraadt 651: register char *p;
652: {
653: while (p && *p && !isspace(*p))
654: p++;
655: return(p);
656: }
657:
658: /* Check if string is actually a number */
1.25 millert 659: int
660: isnumberstr(string)
1.10 downsj 661: char *string;
1.1 deraadt 662: {
1.25 millert 663: while (*string) {
1.9 downsj 664: if (!isdigit(*string++))
665: return(0);
1.1 deraadt 666: }
667: return(1);
1.10 downsj 668: }
669:
1.25 millert 670: void
671: domonitor(log, whom)
1.10 downsj 672: char *log, *whom;
673: {
674: struct stat sb, tsb;
675: char *fname, *flog, *p, *rb = NULL;
676: FILE *fp;
677: off_t osize;
678: int rd;
679:
680: if (stat(log, &sb) < 0)
681: return;
682:
683: flog = strdup(log);
1.11 downsj 684: if (flog == NULL)
685: err(1, "strdup");
686:
1.10 downsj 687: for (p = flog; *p != '\0'; p++) {
688: if (*p == '/')
689: *p = '_';
690: }
1.20 fgsch 691: fname = (char *) malloc(sizeof(STATS_DIR) + strlen(flog) + 16);
1.11 downsj 692: if (fname == NULL)
693: err(1, "malloc");
694:
1.10 downsj 695: sprintf(fname, "%s/newsyslog.%s.size", STATS_DIR, flog);
696:
697: /* ..if it doesn't exist, simply record the current size. */
698: if ((sb.st_size == 0) || stat(fname, &tsb) < 0)
699: goto update;
700:
701: fp = fopen(fname, "r");
702: if (fp == NULL) {
1.11 downsj 703: warn(fname);
1.10 downsj 704: goto cleanup;
705: }
706: #ifdef QUAD_OFF_T
707: if (fscanf(fp, "%qd\n", &osize) != 1) {
708: #else
709: if (fscanf(fp, "%ld\n", &osize) != 1) {
710: #endif /* QUAD_OFF_T */
711: fclose(fp);
712: goto update;
713: }
714:
715: fclose(fp);
716:
717: /* If the file is smaller, mark the entire thing as changed. */
718: if (sb.st_size < osize)
719: osize = 0;
720:
721: /* Now see if current size is larger. */
722: if (sb.st_size > osize) {
723: rb = (char *) malloc(sb.st_size - osize);
1.11 downsj 724: if (rb == NULL)
725: err(1, "malloc");
1.10 downsj 726:
727: /* Open logfile, seek. */
728: fp = fopen(log, "r");
729: if (fp == NULL) {
1.11 downsj 730: warn(log);
1.10 downsj 731: goto cleanup;
732: }
733: fseek(fp, osize, SEEK_SET);
734: rd = fread(rb, 1, sb.st_size - osize, fp);
735: if (rd < 1) {
1.11 downsj 736: warn("fread");
1.10 downsj 737: fclose(fp);
738: goto cleanup;
739: }
740:
741: /* Send message. */
742: fclose(fp);
743:
744: fp = openmail();
745: if (fp == NULL) {
1.11 downsj 746: warn("openmail");
1.10 downsj 747: goto cleanup;
748: }
749: fprintf(fp, "To: %s\nSubject: LOGFILE NOTIFICATION: %s\n\n\n",
750: whom, log);
751: fwrite(rb, 1, rd, fp);
752: fputs("\n\n", fp);
753:
754: closemail(fp);
755: }
756: update:
757: /* Reopen for writing and update file. */
758: fp = fopen(fname, "w");
759: if (fp == NULL) {
1.11 downsj 760: warn(fname);
1.10 downsj 761: goto cleanup;
762: }
763: #ifdef QUAD_OFF_T
764: fprintf(fp, "%qd\n", sb.st_size);
765: #else
766: fprintf(fp, "%ld\n", sb.st_size);
767: #endif /* QUAD_OFF_T */
768: fclose(fp);
769:
770: cleanup:
771: free(flog);
772: free(fname);
773: if (rb != NULL)
774: free(rb);
775: }
776:
1.25 millert 777: FILE *
778: openmail()
1.10 downsj 779: {
780: char *cmdbuf;
781: FILE *ret;
782:
1.20 fgsch 783: cmdbuf = (char *) malloc(sizeof(SENDMAIL) + 3);
1.10 downsj 784: if (cmdbuf == NULL)
785: return(NULL);
786:
787: sprintf(cmdbuf, "%s -t", SENDMAIL);
788: ret = popen(cmdbuf, "w");
789:
790: free(cmdbuf);
791: return(ret);
792: }
793:
1.25 millert 794: void
795: closemail(pfp)
1.10 downsj 796: FILE *pfp;
797: {
798: pclose(pfp);
1.16 millert 799: }
800:
1.25 millert 801: void
802: child_killer(signum)
1.16 millert 803: int signum;
804: {
805: int status;
806:
807: while (waitpid(-1, &status, WNOHANG) > 0)
808: ;
1.1 deraadt 809: }