1 /*
2 * Copyright (c) 1983, 1995 Eric P. Allman
3 * Copyright (c) 1988, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * %sccs.include.redist.c%
7 */
8
9 #ifndef lint
10 static char sccsid[] = "@(#)stats.c 8.5 (Berkeley) 05/28/95";
11 #endif /* not lint */
12
13 # include "sendmail.h"
14 # include "mailstats.h"
15
16 struct statistics Stat;
17
18 bool GotStats = FALSE; /* set when we have stats to merge */
19
20 #define ONE_K 1000 /* one thousand (twenty-four?) */
21 #define KBYTES(x) (((x) + (ONE_K - 1)) / ONE_K)
22 /*
23 ** MARKSTATS -- mark statistics
24 */
25
26 void
markstats(e,to)27 markstats(e, to)
28 register ENVELOPE *e;
29 register ADDRESS *to;
30 {
31 if (to == NULL)
32 {
33 if (e->e_from.q_mailer != NULL)
34 {
35 Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
36 Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
37 KBYTES(e->e_msgsize);
38 }
39 }
40 else
41 {
42 Stat.stat_nt[to->q_mailer->m_mno]++;
43 Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(e->e_msgsize);
44 }
45 GotStats = TRUE;
46 }
47 /*
48 ** POSTSTATS -- post statistics in the statistics file
49 **
50 ** Parameters:
51 ** sfile -- the name of the statistics file.
52 **
53 ** Returns:
54 ** none.
55 **
56 ** Side Effects:
57 ** merges the Stat structure with the sfile file.
58 */
59
60 void
poststats(sfile)61 poststats(sfile)
62 char *sfile;
63 {
64 register int fd;
65 struct statistics stat;
66 extern off_t lseek();
67
68 if (sfile == NULL || !GotStats)
69 return;
70
71 (void) time(&Stat.stat_itime);
72 Stat.stat_size = sizeof Stat;
73
74 fd = open(sfile, O_RDWR);
75 if (fd < 0)
76 {
77 errno = 0;
78 return;
79 }
80 (void) lockfile(fd, sfile, NULL, LOCK_EX);
81 if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
82 stat.stat_size == sizeof stat)
83 {
84 /* merge current statistics into statfile */
85 register int i;
86
87 for (i = 0; i < MAXMAILERS; i++)
88 {
89 stat.stat_nf[i] += Stat.stat_nf[i];
90 stat.stat_bf[i] += Stat.stat_bf[i];
91 stat.stat_nt[i] += Stat.stat_nt[i];
92 stat.stat_bt[i] += Stat.stat_bt[i];
93 }
94 }
95 else
96 bcopy((char *) &Stat, (char *) &stat, sizeof stat);
97
98 /* write out results */
99 (void) lseek(fd, (off_t) 0, 0);
100 (void) write(fd, (char *) &stat, sizeof stat);
101 (void) close(fd);
102
103 /* clear the structure to avoid future disappointment */
104 bzero(&Stat, sizeof stat);
105 GotStats = FALSE;
106 }
107