xref: /csrg-svn/usr.sbin/sendmail/src/stats.c (revision 25689)
1 /*
2 **  Sendmail
3 **  Copyright (c) 1983  Eric P. Allman
4 **  Berkeley, California
5 **
6 **  Copyright (c) 1983 Regents of the University of California.
7 **  All rights reserved.  The Berkeley software License Agreement
8 **  specifies the terms and conditions for redistribution.
9 */
10 
11 #ifndef lint
12 static char	SccsId[] = "@(#)stats.c	5.7 (Berkeley) 01/05/86";
13 #endif not lint
14 
15 # include "sendmail.h"
16 
17 /*
18 **  Statistics structure.
19 */
20 
21 struct statistics
22 {
23 	time_t	stat_itime;		/* file initialization time */
24 	short	stat_size;		/* size of this structure */
25 	long	stat_nf[MAXMAILERS];	/* # msgs from each mailer */
26 	long	stat_bf[MAXMAILERS];	/* kbytes from each mailer */
27 	long	stat_nt[MAXMAILERS];	/* # msgs to each mailer */
28 	long	stat_bt[MAXMAILERS];	/* kbytes to each mailer */
29 };
30 
31 struct statistics	Stat;
32 
33 #define ONE_K		1000		/* one thousand (twenty-four?) */
34 #define KBYTES(x)	(((x) + (ONE_K - 1)) / ONE_K)
35 /*
36 **  MARKSTATS -- mark statistics
37 */
38 
39 markstats(e, to)
40 	register ENVELOPE *e;
41 	register ADDRESS *to;
42 {
43 	if (to == NULL)
44 	{
45 		if (e->e_from.q_mailer != NULL)
46 		{
47 			Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
48 			Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
49 				KBYTES(CurEnv->e_msgsize);
50 		}
51 	}
52 	else
53 	{
54 		Stat.stat_nt[to->q_mailer->m_mno]++;
55 		Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(CurEnv->e_msgsize);
56 	}
57 }
58 /*
59 **  POSTSTATS -- post statistics in the statistics file
60 **
61 **	Parameters:
62 **		sfile -- the name of the statistics file.
63 **
64 **	Returns:
65 **		none.
66 **
67 **	Side Effects:
68 **		merges the Stat structure with the sfile file.
69 */
70 
71 poststats(sfile)
72 	char *sfile;
73 {
74 	register int fd;
75 	struct statistics stat;
76 	extern off_t lseek();
77 
78 	if (sfile == NULL)
79 		return;
80 
81 	(void) time(&Stat.stat_itime);
82 	Stat.stat_size = sizeof Stat;
83 
84 	fd = open(sfile, 2);
85 	if (fd < 0)
86 	{
87 		errno = 0;
88 		return;
89 	}
90 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
91 	    stat.stat_size == sizeof stat)
92 	{
93 		/* merge current statistics into statfile */
94 		register int i;
95 
96 		for (i = 0; i < MAXMAILERS; i++)
97 		{
98 			stat.stat_nf[i] += Stat.stat_nf[i];
99 			stat.stat_bf[i] += Stat.stat_bf[i];
100 			stat.stat_nt[i] += Stat.stat_nt[i];
101 			stat.stat_bt[i] += Stat.stat_bt[i];
102 		}
103 	}
104 	else
105 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
106 
107 	/* write out results */
108 	(void) lseek(fd, (off_t) 0, 0);
109 	(void) write(fd, (char *) &stat, sizeof stat);
110 	(void) close(fd);
111 }
112