xref: /original-bsd/usr.sbin/sendmail/src/stats.c (revision 79386b64)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that the above copyright notice and this paragraph are
8  * duplicated in all such forms and that any documentation,
9  * advertising materials, and other materials related to such
10  * distribution and use acknowledge that the software was developed
11  * by the University of California, Berkeley.  The name of the
12  * University may not be used to endorse or promote products derived
13  * from this software without specific prior written permission.
14  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17  */
18 
19 #ifndef lint
20 static char sccsid[] = "@(#)stats.c	5.10 (Berkeley) 06/30/88";
21 #endif /* not lint */
22 
23 # include "sendmail.h"
24 # include "mailstats.h"
25 
26 struct statistics	Stat;
27 
28 #define ONE_K		1000		/* one thousand (twenty-four?) */
29 #define KBYTES(x)	(((x) + (ONE_K - 1)) / ONE_K)
30 /*
31 **  MARKSTATS -- mark statistics
32 */
33 
34 markstats(e, to)
35 	register ENVELOPE *e;
36 	register ADDRESS *to;
37 {
38 	if (to == NULL)
39 	{
40 		if (e->e_from.q_mailer != NULL)
41 		{
42 			Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
43 			Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
44 				KBYTES(CurEnv->e_msgsize);
45 		}
46 	}
47 	else
48 	{
49 		Stat.stat_nt[to->q_mailer->m_mno]++;
50 		Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(CurEnv->e_msgsize);
51 	}
52 }
53 /*
54 **  POSTSTATS -- post statistics in the statistics file
55 **
56 **	Parameters:
57 **		sfile -- the name of the statistics file.
58 **
59 **	Returns:
60 **		none.
61 **
62 **	Side Effects:
63 **		merges the Stat structure with the sfile file.
64 */
65 
66 poststats(sfile)
67 	char *sfile;
68 {
69 	register int fd;
70 	struct statistics stat;
71 	extern off_t lseek();
72 
73 	if (sfile == NULL)
74 		return;
75 
76 	(void) time(&Stat.stat_itime);
77 	Stat.stat_size = sizeof Stat;
78 
79 	fd = open(sfile, 2);
80 	if (fd < 0)
81 	{
82 		errno = 0;
83 		return;
84 	}
85 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
86 	    stat.stat_size == sizeof stat)
87 	{
88 		/* merge current statistics into statfile */
89 		register int i;
90 
91 		for (i = 0; i < MAXMAILERS; i++)
92 		{
93 			stat.stat_nf[i] += Stat.stat_nf[i];
94 			stat.stat_bf[i] += Stat.stat_bf[i];
95 			stat.stat_nt[i] += Stat.stat_nt[i];
96 			stat.stat_bt[i] += Stat.stat_bt[i];
97 		}
98 	}
99 	else
100 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
101 
102 	/* write out results */
103 	(void) lseek(fd, (off_t) 0, 0);
104 	(void) write(fd, (char *) &stat, sizeof stat);
105 	(void) close(fd);
106 }
107