Annotation of src/usr.bin/newsyslog/newsyslog.c, Revision 1.22
1.22 ! millert 1: /* $OpenBSD: newsyslog.c,v 1.21 1999/10/13 17:24:23 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.22 ! millert 64: static char rcsid[] = "$OpenBSD: newsyslog.c,v 1.21 1999/10/13 17:24:23 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:
102: #define kbytes(size) (((size) + 1023) >> 10)
103:
1.10 downsj 104: #define CE_COMPACT 0x01 /* Compact the achived log files */
105: #define CE_BINARY 0x02 /* Logfile is in binary, don't add */
106: /* status messages */
107: #define CE_MONITOR 0x04 /* Monitory for changes */
1.1 deraadt 108: #define NONE -1
109:
110: struct conf_entry {
111: char *log; /* Name of the log */
112: int uid; /* Owner of log */
113: int gid; /* Group of log */
114: int numlogs; /* Number of logs to keep */
115: int size; /* Size cutoff to trigger trimming the log */
116: int hours; /* Hours between log trimming */
117: int permissions; /* File permissions on the log */
118: int flags; /* Flags (CE_COMPACT & CE_BINARY) */
1.10 downsj 119: char *whom; /* Whom to notify if logfile changes */
1.14 millert 120: char *pidfile; /* Path to file containg pid to HUP */
1.1 deraadt 121: struct conf_entry *next; /* Linked list pointer */
122: };
123:
1.11 downsj 124: extern const char *__progname;
125:
1.1 deraadt 126: int verbose = 0; /* Print out what's going on */
127: int needroot = 1; /* Root privs are necessary */
128: int noaction = 0; /* Don't do anything, just show it */
1.10 downsj 129: int monitor = 0; /* Don't do monitoring by default */
1.1 deraadt 130: char *conf = CONF; /* Configuration file to use */
131: time_t timenow;
1.16 millert 132: #define MIN_PID 4
1.7 deraadt 133: char hostname[MAXHOSTNAMELEN]; /* hostname */
1.1 deraadt 134: char *daytime; /* timenow in human readable form */
135:
136:
1.9 downsj 137: void do_entry __P((struct conf_entry *));
138: void PRS __P((int, char **));
139: void usage __P((void));
140: struct conf_entry *parse_file __P((void));
141: char *missing_field __P((char *, char *));
1.14 millert 142: void dotrim __P((char *, int, int, int, int, int, int));
1.9 downsj 143: int log_trim __P((char *));
144: void compress_log __P((char *));
145: int sizefile __P((char *));
1.21 millert 146: int age_old_log __P((char *, int *));
1.9 downsj 147: char *sob __P((char *));
148: char *son __P((char *));
1.10 downsj 149: int isnumberstr __P((char *));
150: void domonitor __P((char *, char *));
151: FILE *openmail __P((void));
152: void closemail __P((FILE *));
1.16 millert 153: void child_killer __P((int));
1.1 deraadt 154:
1.9 downsj 155: int main(argc, argv)
1.1 deraadt 156: int argc;
157: char **argv;
158: {
159: struct conf_entry *p, *q;
1.16 millert 160: int status;
1.1 deraadt 161:
162: PRS(argc,argv);
1.11 downsj 163: if (needroot && getuid() && geteuid())
164: errx(1, "You must be root.");
1.1 deraadt 165: p = q = parse_file();
1.16 millert 166: signal(SIGCHLD, child_killer);
1.1 deraadt 167: while (p) {
168: do_entry(p);
169: p=p->next;
1.11 downsj 170: free(q);
1.1 deraadt 171: q=p;
172: }
1.16 millert 173:
174: /* Wait for children to finish, then exit */
175: while (waitpid(-1, &status, 0) != -1)
176: ;
1.1 deraadt 177: exit(0);
178: }
179:
1.9 downsj 180: void do_entry(ent)
1.1 deraadt 181: struct conf_entry *ent;
182:
183: {
1.14 millert 184: int size, modtime, pid;
185: char line[BUFSIZ];
186: FILE *f;
187:
188: /* First find the pid to HUP */
189: pid = -1;
190: if ((f = fopen(ent->pidfile,"r")) != NULL) {
191: if (fgets(line,BUFSIZ,f))
192: pid = atoi(line);
193: (void)fclose(f);
194: }
1.1 deraadt 195:
1.19 kstailey 196: if (verbose)
197: printf("%s <%d%s>: ", ent->log, ent->numlogs,
198: (ent->flags & CE_COMPACT) ? "Z" : "");
1.1 deraadt 199: size = sizefile(ent->log);
1.21 millert 200: if (age_old_log(ent->log, &modtime) == -1)
1.22 ! millert 201: modtime = -1;
1.1 deraadt 202: if (size < 0) {
203: if (verbose)
204: printf("does not exist.\n");
205: } else {
206: if (verbose && (ent->size > 0))
207: printf("size (Kb): %d [%d] ", size, ent->size);
208: if (verbose && (ent->hours > 0))
209: printf(" age (hr): %d [%d] ", modtime, ent->hours);
1.10 downsj 210: if (monitor && ent->flags & CE_MONITOR)
211: domonitor(ent->log, ent->whom);
212: if (!monitor && ((ent->size > 0) && (size >= ent->size)) ||
1.1 deraadt 213: ((ent->hours > 0) && ((modtime >= ent->hours)
214: || (modtime < 0)))) {
215: if (verbose)
216: printf("--> trimming log....\n");
1.19 kstailey 217: if (noaction && !verbose)
218: printf("%s <%d%s>: ", ent->log, ent->numlogs,
219: (ent->flags & CE_COMPACT) ? "Z" : "");
1.1 deraadt 220: dotrim(ent->log, ent->numlogs, ent->flags,
1.14 millert 221: ent->permissions, ent->uid, ent->gid, pid);
1.1 deraadt 222: } else {
223: if (verbose)
224: printf("--> skipping\n");
225: }
226: }
227: }
228:
1.9 downsj 229: void PRS(argc, argv)
1.1 deraadt 230: int argc;
231: char **argv;
232: {
233: int c;
234: char *p;
235:
1.12 kstailey 236: timenow = time(NULL);
1.1 deraadt 237: daytime = ctime(&timenow) + 4;
1.2 deraadt 238: daytime[15] = '\0';
1.1 deraadt 239:
240: /* Let's get our hostname */
241: (void) gethostname(hostname, sizeof(hostname));
242:
243: /* Truncate domain */
1.9 downsj 244: p = strchr(hostname, '.');
245: if (p)
1.1 deraadt 246: *p = '\0';
247:
248: optind = 1; /* Start options parsing */
1.10 downsj 249: while ((c = getopt(argc,argv,"nrvmf:t:")) != -1) {
1.1 deraadt 250: switch (c) {
251: case 'n':
252: noaction++; /* This implies needroot as off */
253: /* fall through */
254: case 'r':
255: needroot = 0;
256: break;
257: case 'v':
258: verbose++;
259: break;
260: case 'f':
261: conf = optarg;
262: break;
1.10 downsj 263: case 'm':
264: monitor++;
265: break;
1.1 deraadt 266: default:
267: usage();
268: }
269: }
1.9 downsj 270: }
1.1 deraadt 271:
1.9 downsj 272: void usage()
1.1 deraadt 273: {
1.14 millert 274: fprintf(stderr, "usage: %s [-nrvm] [-f config-file]\n", __progname);
275: exit(1);
1.1 deraadt 276: }
277:
278: /* Parse a configuration file and return a linked list of all the logs
279: * to process
280: */
281: struct conf_entry *parse_file()
282: {
283: FILE *f;
284: char line[BUFSIZ], *parse, *q;
285: char *errline, *group;
286: struct conf_entry *first = NULL;
287: struct conf_entry *working;
288: struct passwd *pass;
289: struct group *grp;
290:
291: if (strcmp(conf,"-"))
292: f = fopen(conf,"r");
293: else
294: f = stdin;
1.11 downsj 295: if (!f)
296: err(1, conf);
297:
1.1 deraadt 298: while (fgets(line,BUFSIZ,f)) {
299: if ((line[0]== '\n') || (line[0] == '#'))
300: continue;
301: errline = strdup(line);
1.11 downsj 302: if (errline == NULL)
303: err(1, "strdup");
1.1 deraadt 304: if (!first) {
305: working = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11 downsj 306: if (working == NULL)
307: err(1, "malloc");
1.1 deraadt 308: first = working;
309: } else {
310: working->next = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11 downsj 311: if (working->next == NULL)
312: err(1, "malloc");
1.1 deraadt 313: working = working->next;
314: }
315:
316: q = parse = missing_field(sob(line),errline);
317: *(parse = son(line)) = '\0';
318: working->log = strdup(q);
1.11 downsj 319: if (working->log == NULL)
320: err(1, "strdup");
1.1 deraadt 321:
322: q = parse = missing_field(sob(++parse),errline);
323: *(parse = son(parse)) = '\0';
324: if ((group = strchr(q, '.')) != NULL) {
325: *group++ = '\0';
326: if (*q) {
1.10 downsj 327: if (!(isnumberstr(q))) {
1.11 downsj 328: if ((pass = getpwnam(q)) == NULL)
329: errx(1, "Error in config file; unknown user: %s", q);
1.1 deraadt 330: working->uid = pass->pw_uid;
331: } else
332: working->uid = atoi(q);
333: } else
334: working->uid = NONE;
335:
336: q = group;
337: if (*q) {
1.10 downsj 338: if (!(isnumberstr(q))) {
1.11 downsj 339: if ((grp = getgrnam(q)) == NULL)
340: errx(1, "Error in config file; unknown group: %s", q);
1.1 deraadt 341: working->gid = grp->gr_gid;
342: } else
343: working->gid = atoi(q);
344: } else
345: working->gid = NONE;
346:
347: q = parse = missing_field(sob(++parse),errline);
348: *(parse = son(parse)) = '\0';
1.11 downsj 349: } else
1.1 deraadt 350: working->uid = working->gid = NONE;
351:
1.11 downsj 352: if (!sscanf(q,"%o",&working->permissions))
353: errx(1, "Error in config file; bad permissions: %s", q);
1.1 deraadt 354:
355: q = parse = missing_field(sob(++parse),errline);
356: *(parse = son(parse)) = '\0';
1.18 millert 357: if (!sscanf(q,"%d",&working->numlogs) || working->numlogs < 0)
1.11 downsj 358: errx(1, "Error in config file; bad number: %s", q);
1.1 deraadt 359:
360: q = parse = missing_field(sob(++parse),errline);
361: *(parse = son(parse)) = '\0';
362: if (isdigit(*q))
363: working->size = atoi(q);
364: else
365: working->size = -1;
366:
367: q = parse = missing_field(sob(++parse),errline);
368: *(parse = son(parse)) = '\0';
369: if (isdigit(*q))
370: working->hours = atoi(q);
371: else
372: working->hours = -1;
373:
374: q = parse = sob(++parse); /* Optional field */
375: *(parse = son(parse)) = '\0';
376: working->flags = 0;
377: while (q && *q && !isspace(*q)) {
378: if ((*q == 'Z') || (*q == 'z'))
379: working->flags |= CE_COMPACT;
380: else if ((*q == 'B') || (*q == 'b'))
381: working->flags |= CE_BINARY;
1.10 downsj 382: else if ((*q == 'M') || (*q == 'm'))
383: working->flags |= CE_MONITOR;
1.11 downsj 384: else
385: errx(1, "Illegal flag in config file: %c", *q);
1.1 deraadt 386: q++;
387: }
1.10 downsj 388:
389: working->whom = NULL;
390: if (working->flags & CE_MONITOR) { /* Optional field */
391: q = parse = sob(++parse);
392: *(parse = son(parse)) = '\0';
393:
394: working->whom = strdup(q);
1.11 downsj 395: if (working->log == NULL)
396: err(1, "strdup");
1.10 downsj 397: }
1.14 millert 398:
399: working->pidfile = PIDFILE;
400: q = parse = sob(++parse); /* Optional field */
401: *(parse = son(parse)) = '\0';
402: if (q && *q != '\0') {
403: working->pidfile = strdup(q);
404: if (working->pidfile == NULL)
405: err(1, "strdup");
406: }
1.1 deraadt 407:
408: free(errline);
409: }
410: if (working)
1.12 kstailey 411: working->next = NULL;
1.1 deraadt 412: (void) fclose(f);
413: return(first);
414: }
415:
1.9 downsj 416: char *missing_field(p, errline)
1.1 deraadt 417: char *p,*errline;
418: {
419: if (!p || !*p) {
1.14 millert 420: warnx("Missing field in config file line:");
1.11 downsj 421: fputs(errline, stderr);
1.1 deraadt 422: exit(1);
423: }
424: return(p);
425: }
426:
1.14 millert 427: void dotrim(log, numdays, flags, perm, owner_uid, group_gid, daemon_pid)
1.1 deraadt 428: char *log;
429: int numdays;
430: int flags;
431: int perm;
432: int owner_uid;
433: int group_gid;
1.14 millert 434: int daemon_pid;
1.1 deraadt 435: {
1.7 deraadt 436: char file1[MAXPATHLEN], file2[MAXPATHLEN];
437: char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
1.1 deraadt 438: int fd;
439: struct stat st;
1.6 tholo 440: int days = numdays;
1.1 deraadt 441:
442: /* Remove oldest log */
443: (void) sprintf(file1,"%s.%d",log,numdays);
444: (void) strcpy(zfile1, file1);
445: (void) strcat(zfile1, COMPRESS_POSTFIX);
446:
447: if (noaction) {
448: printf("rm -f %s\n", file1);
449: printf("rm -f %s\n", zfile1);
450: } else {
451: (void) unlink(file1);
452: (void) unlink(zfile1);
453: }
454:
455: /* Move down log files */
456: while (numdays--) {
457: (void) strcpy(file2,file1);
458: (void) sprintf(file1,"%s.%d",log,numdays);
459: (void) strcpy(zfile1, file1);
460: (void) strcpy(zfile2, file2);
461: if (lstat(file1, &st)) {
462: (void) strcat(zfile1, COMPRESS_POSTFIX);
463: (void) strcat(zfile2, COMPRESS_POSTFIX);
464: if (lstat(zfile1, &st)) continue;
465: }
466: if (noaction) {
467: printf("mv %s %s\n",zfile1,zfile2);
468: printf("chmod %o %s\n", perm, zfile2);
469: printf("chown %d.%d %s\n",
470: owner_uid, group_gid, zfile2);
471: } else {
472: (void) rename(zfile1, zfile2);
473: (void) chmod(zfile2, perm);
474: (void) chown(zfile2, owner_uid, group_gid);
475: }
476: }
477: if (!noaction && !(flags & CE_BINARY))
478: (void) log_trim(log); /* Report the trimming to the old log */
479:
1.6 tholo 480: if (days == 0) {
1.5 deraadt 481: if (noaction)
482: printf("rm %s\n",log);
483: else
484: (void) unlink(log);
485: } else {
486: if (noaction)
487: printf("mv %s to %s\n",log,file1);
488: else
489: (void) rename(log,file1);
490: }
491:
1.22 ! millert 492: if (noaction) {
1.1 deraadt 493: printf("Start new log...");
1.22 ! millert 494: } else {
! 495: fd = open(log, O_WRONLY|O_TRUNC|O_CREAT, perm);
1.11 downsj 496: if (fd < 0)
1.13 mickey 497: err(1, "can't start \'%s\' log", log);
1.11 downsj 498: if (fchown(fd, owner_uid, group_gid))
1.13 mickey 499: err(1, "can't chown \'%s\' log file", log);
1.22 ! millert 500: if (fchmod(fd, perm))
! 501: err(1, "can't chmod \'%s\' log file", log, perm);
1.1 deraadt 502: (void) close(fd);
503: if (!(flags & CE_BINARY))
1.11 downsj 504: if (log_trim(log)) /* Add status message */
1.13 mickey 505: err(1, "can't add status message to log \'%s\'", log);
1.1 deraadt 506: }
507: if (noaction)
1.14 millert 508: printf("kill -HUP %d\n",daemon_pid);
1.17 deraadt 509: else if (daemon_pid < MIN_PID)
1.14 millert 510: warnx("preposterous process number: %d", daemon_pid);
511: else if (kill(daemon_pid,SIGHUP))
1.17 deraadt 512: warnx("warning - could not HUP daemon");
513: if (flags & CE_COMPACT) {
514: if (noaction)
515: printf("Compress %s.0\n",log);
516: else
517: compress_log(log);
518: }
1.1 deraadt 519: }
520:
521: /* Log the fact that the logs were turned over */
1.9 downsj 522: int log_trim(log)
1.17 deraadt 523: char *log;
1.1 deraadt 524: {
525: FILE *f;
526: if ((f = fopen(log,"a")) == NULL)
527: return(-1);
528: fprintf(f,"%s %s newsyslog[%d]: logfile turned over\n",
529: daytime, hostname, getpid());
530: if (fclose(f) == EOF) {
1.11 downsj 531: err(1, "log_trim: fclose");
1.1 deraadt 532: }
533: return(0);
534: }
535:
1.16 millert 536: /* Fork off compress or gzip to compress the old log file */
1.9 downsj 537: void compress_log(log)
1.1 deraadt 538: char *log;
539: {
540: int pid;
1.7 deraadt 541: char tmp[MAXPATHLEN];
1.1 deraadt 542:
543: pid = fork();
544: (void) sprintf(tmp,"%s.0",log);
545: if (pid < 0) {
1.11 downsj 546: err(1, "fork");
1.1 deraadt 547: } else if (!pid) {
548: (void) execl(COMPRESS,"compress","-f",tmp,0);
1.16 millert 549: warn(COMPRESS);
550: _exit(1);
1.1 deraadt 551: }
552: }
553:
554: /* Return size in kilobytes of a file */
555: int sizefile(file)
556: char *file;
557: {
558: struct stat sb;
559:
560: if (stat(file,&sb) < 0)
561: return(-1);
562: return(kbytes(dbtob(sb.st_blocks)));
563: }
564:
565: /* Return the age of old log file (file.0) */
1.21 millert 566: int age_old_log(file, mtime)
1.1 deraadt 567: char *file;
1.21 millert 568: int *mtime;
1.1 deraadt 569: {
570: struct stat sb;
1.7 deraadt 571: char tmp[MAXPATHLEN];
1.1 deraadt 572:
573: (void) strcpy(tmp,file);
574: if (stat(strcat(tmp,".0"),&sb) < 0)
575: if (stat(strcat(tmp,COMPRESS_POSTFIX), &sb) < 0)
576: return(-1);
1.21 millert 577: *mtime = (int) (timenow - sb.st_mtime + 1800) / 3600;
578: return(0);
1.1 deraadt 579: }
580:
581: /* Skip Over Blanks */
582: char *sob(p)
583: register char *p;
584: {
585: while (p && *p && isspace(*p))
586: p++;
587: return(p);
588: }
589:
590: /* Skip Over Non-Blanks */
591: char *son(p)
592: register char *p;
593: {
594: while (p && *p && !isspace(*p))
595: p++;
596: return(p);
597: }
598:
599:
600: /* Check if string is actually a number */
601:
1.10 downsj 602: int isnumberstr(string)
603: char *string;
1.1 deraadt 604: {
605: while (*string != '\0') {
1.9 downsj 606: if (!isdigit(*string++))
607: return(0);
1.1 deraadt 608: }
609: return(1);
1.10 downsj 610: }
611:
612: void domonitor(log, whom)
613: char *log, *whom;
614: {
615: struct stat sb, tsb;
616: char *fname, *flog, *p, *rb = NULL;
617: FILE *fp;
618: off_t osize;
619: int rd;
620:
621: if (stat(log, &sb) < 0)
622: return;
623:
624: flog = strdup(log);
1.11 downsj 625: if (flog == NULL)
626: err(1, "strdup");
627:
1.10 downsj 628: for (p = flog; *p != '\0'; p++) {
629: if (*p == '/')
630: *p = '_';
631: }
1.20 fgsch 632: fname = (char *) malloc(sizeof(STATS_DIR) + strlen(flog) + 16);
1.11 downsj 633: if (fname == NULL)
634: err(1, "malloc");
635:
1.10 downsj 636: sprintf(fname, "%s/newsyslog.%s.size", STATS_DIR, flog);
637:
638: /* ..if it doesn't exist, simply record the current size. */
639: if ((sb.st_size == 0) || stat(fname, &tsb) < 0)
640: goto update;
641:
642: fp = fopen(fname, "r");
643: if (fp == NULL) {
1.11 downsj 644: warn(fname);
1.10 downsj 645: goto cleanup;
646: }
647: #ifdef QUAD_OFF_T
648: if (fscanf(fp, "%qd\n", &osize) != 1) {
649: #else
650: if (fscanf(fp, "%ld\n", &osize) != 1) {
651: #endif /* QUAD_OFF_T */
652: fclose(fp);
653: goto update;
654: }
655:
656: fclose(fp);
657:
658: /* If the file is smaller, mark the entire thing as changed. */
659: if (sb.st_size < osize)
660: osize = 0;
661:
662: /* Now see if current size is larger. */
663: if (sb.st_size > osize) {
664: rb = (char *) malloc(sb.st_size - osize);
1.11 downsj 665: if (rb == NULL)
666: err(1, "malloc");
1.10 downsj 667:
668: /* Open logfile, seek. */
669: fp = fopen(log, "r");
670: if (fp == NULL) {
1.11 downsj 671: warn(log);
1.10 downsj 672: goto cleanup;
673: }
674: fseek(fp, osize, SEEK_SET);
675: rd = fread(rb, 1, sb.st_size - osize, fp);
676: if (rd < 1) {
1.11 downsj 677: warn("fread");
1.10 downsj 678: fclose(fp);
679: goto cleanup;
680: }
681:
682: /* Send message. */
683: fclose(fp);
684:
685: fp = openmail();
686: if (fp == NULL) {
1.11 downsj 687: warn("openmail");
1.10 downsj 688: goto cleanup;
689: }
690: fprintf(fp, "To: %s\nSubject: LOGFILE NOTIFICATION: %s\n\n\n",
691: whom, log);
692: fwrite(rb, 1, rd, fp);
693: fputs("\n\n", fp);
694:
695: closemail(fp);
696: }
697: update:
698: /* Reopen for writing and update file. */
699: fp = fopen(fname, "w");
700: if (fp == NULL) {
1.11 downsj 701: warn(fname);
1.10 downsj 702: goto cleanup;
703: }
704: #ifdef QUAD_OFF_T
705: fprintf(fp, "%qd\n", sb.st_size);
706: #else
707: fprintf(fp, "%ld\n", sb.st_size);
708: #endif /* QUAD_OFF_T */
709: fclose(fp);
710:
711: cleanup:
712: free(flog);
713: free(fname);
714: if (rb != NULL)
715: free(rb);
716: }
717:
718: FILE *openmail()
719: {
720: char *cmdbuf;
721: FILE *ret;
722:
1.20 fgsch 723: cmdbuf = (char *) malloc(sizeof(SENDMAIL) + 3);
1.10 downsj 724: if (cmdbuf == NULL)
725: return(NULL);
726:
727: sprintf(cmdbuf, "%s -t", SENDMAIL);
728: ret = popen(cmdbuf, "w");
729:
730: free(cmdbuf);
731: return(ret);
732: }
733:
734: void closemail(pfp)
735: FILE *pfp;
736: {
737: pclose(pfp);
1.16 millert 738: }
739:
740: void child_killer(signum)
741: int signum;
742: {
743: int status;
744:
745: while (waitpid(-1, &status, WNOHANG) > 0)
746: ;
1.1 deraadt 747: }