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