xref: /original-bsd/usr.sbin/sendmail/src/stats.c (revision 0842ddeb)
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.4 (Berkeley) 04/21/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 markstats(e, to)
27 	register ENVELOPE *e;
28 	register ADDRESS *to;
29 {
30 	if (to == NULL)
31 	{
32 		if (e->e_from.q_mailer != NULL)
33 		{
34 			Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
35 			Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
36 				KBYTES(e->e_msgsize);
37 		}
38 	}
39 	else
40 	{
41 		Stat.stat_nt[to->q_mailer->m_mno]++;
42 		Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(e->e_msgsize);
43 	}
44 	GotStats = TRUE;
45 }
46 /*
47 **  POSTSTATS -- post statistics in the statistics file
48 **
49 **	Parameters:
50 **		sfile -- the name of the statistics file.
51 **
52 **	Returns:
53 **		none.
54 **
55 **	Side Effects:
56 **		merges the Stat structure with the sfile file.
57 */
58 
59 poststats(sfile)
60 	char *sfile;
61 {
62 	register int fd;
63 	struct statistics stat;
64 	extern off_t lseek();
65 
66 	if (sfile == NULL || !GotStats)
67 		return;
68 
69 	(void) time(&Stat.stat_itime);
70 	Stat.stat_size = sizeof Stat;
71 
72 	fd = open(sfile, O_RDWR);
73 	if (fd < 0)
74 	{
75 		errno = 0;
76 		return;
77 	}
78 	(void) lockfile(fd, sfile, NULL, LOCK_EX);
79 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
80 	    stat.stat_size == sizeof stat)
81 	{
82 		/* merge current statistics into statfile */
83 		register int i;
84 
85 		for (i = 0; i < MAXMAILERS; i++)
86 		{
87 			stat.stat_nf[i] += Stat.stat_nf[i];
88 			stat.stat_bf[i] += Stat.stat_bf[i];
89 			stat.stat_nt[i] += Stat.stat_nt[i];
90 			stat.stat_bt[i] += Stat.stat_bt[i];
91 		}
92 	}
93 	else
94 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
95 
96 	/* write out results */
97 	(void) lseek(fd, (off_t) 0, 0);
98 	(void) write(fd, (char *) &stat, sizeof stat);
99 	(void) close(fd);
100 
101 	/* clear the structure to avoid future disappointment */
102 	bzero(&Stat, sizeof stat);
103 	GotStats = FALSE;
104 }
105