xref: /freebsd/contrib/sendmail/src/stats.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
1 /*
2  * Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.
3  *	All rights reserved.
4  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
5  * Copyright (c) 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * By using this file, you agree to the terms and conditions set
9  * forth in the LICENSE file which can be found at the top level of
10  * the sendmail distribution.
11  *
12  */
13 
14 #include <sendmail.h>
15 
16 SM_RCSID("@(#)$Id: stats.c,v 8.52 2001/11/21 13:39:14 gshapiro Exp $")
17 
18 #include <sendmail/mailstats.h>
19 
20 static struct statistics	Stat;
21 
22 static bool	GotStats = false;	/* set when we have stats to merge */
23 
24 /* See http://physics.nist.gov/cuu/Units/binary.html */
25 #define ONE_K		1000		/* one thousand (twenty-four?) */
26 #define KBYTES(x)	(((x) + (ONE_K - 1)) / ONE_K)
27 /*
28 **  MARKSTATS -- mark statistics
29 **
30 **	Parameters:
31 **		e -- the envelope.
32 **		to -- to address.
33 **		type -- type of stats this represents.
34 **
35 **	Returns:
36 **		none.
37 **
38 **	Side Effects:
39 **		changes static Stat structure
40 */
41 
42 void
43 markstats(e, to, type)
44 	register ENVELOPE *e;
45 	register ADDRESS *to;
46 	int type;
47 {
48 	switch (type)
49 	{
50 #if _FFR_QUARANTINE
51 	  case STATS_QUARANTINE:
52 		if (e->e_from.q_mailer != NULL)
53 			Stat.stat_nq[e->e_from.q_mailer->m_mno]++;
54 		break;
55 #endif /* _FFR_QUARANTINE */
56 
57 	  case STATS_REJECT:
58 		if (e->e_from.q_mailer != NULL)
59 		{
60 			if (bitset(EF_DISCARD, e->e_flags))
61 				Stat.stat_nd[e->e_from.q_mailer->m_mno]++;
62 			else
63 				Stat.stat_nr[e->e_from.q_mailer->m_mno]++;
64 		}
65 		Stat.stat_cr++;
66 		break;
67 
68 	  case STATS_NORMAL:
69 		if (to == NULL)
70 		{
71 			Stat.stat_cf++;
72 			if (e->e_from.q_mailer != NULL)
73 			{
74 				Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
75 				Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
76 					KBYTES(e->e_msgsize);
77 			}
78 		}
79 		else
80 		{
81 			Stat.stat_ct++;
82 			Stat.stat_nt[to->q_mailer->m_mno]++;
83 			Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(e->e_msgsize);
84 		}
85 		break;
86 
87 	  default:
88 		/* Silently ignore bogus call */
89 		return;
90 	}
91 
92 
93 	GotStats = true;
94 }
95 /*
96 **  CLEARSTATS -- clear statistics structure
97 **
98 **	Parameters:
99 **		none.
100 **
101 **	Returns:
102 **		none.
103 **
104 **	Side Effects:
105 **		clears the Stat structure.
106 */
107 
108 void
109 clearstats()
110 {
111 	/* clear the structure to avoid future disappointment */
112 	memset(&Stat, '\0', sizeof Stat);
113 	GotStats = false;
114 }
115 /*
116 **  POSTSTATS -- post statistics in the statistics file
117 **
118 **	Parameters:
119 **		sfile -- the name of the statistics file.
120 **
121 **	Returns:
122 **		none.
123 **
124 **	Side Effects:
125 **		merges the Stat structure with the sfile file.
126 */
127 
128 void
129 poststats(sfile)
130 	char *sfile;
131 {
132 	int fd;
133 	static bool entered = false;
134 	long sff = SFF_REGONLY|SFF_OPENASROOT;
135 	struct statistics stats;
136 	extern off_t lseek();
137 
138 	if (sfile == NULL || *sfile == '\0' || !GotStats || entered)
139 		return;
140 	entered = true;
141 
142 	(void) time(&Stat.stat_itime);
143 	Stat.stat_size = sizeof Stat;
144 	Stat.stat_magic = STAT_MAGIC;
145 	Stat.stat_version = STAT_VERSION;
146 
147 	if (!bitnset(DBS_WRITESTATSTOSYMLINK, DontBlameSendmail))
148 		sff |= SFF_NOSLINK;
149 	if (!bitnset(DBS_WRITESTATSTOHARDLINK, DontBlameSendmail))
150 		sff |= SFF_NOHLINK;
151 
152 	fd = safeopen(sfile, O_RDWR, 0644, sff);
153 	if (fd < 0)
154 	{
155 		if (LogLevel > 12)
156 			sm_syslog(LOG_INFO, NOQID, "poststats: %s: %s",
157 				  sfile, sm_errstring(errno));
158 		errno = 0;
159 		entered = false;
160 		return;
161 	}
162 	if (read(fd, (char *) &stats, sizeof stats) == sizeof stats &&
163 	    stats.stat_size == sizeof stats &&
164 	    stats.stat_magic == Stat.stat_magic &&
165 	    stats.stat_version == Stat.stat_version)
166 	{
167 		/* merge current statistics into statfile */
168 		register int i;
169 
170 		for (i = 0; i < MAXMAILERS; i++)
171 		{
172 			stats.stat_nf[i] += Stat.stat_nf[i];
173 			stats.stat_bf[i] += Stat.stat_bf[i];
174 			stats.stat_nt[i] += Stat.stat_nt[i];
175 			stats.stat_bt[i] += Stat.stat_bt[i];
176 			stats.stat_nr[i] += Stat.stat_nr[i];
177 			stats.stat_nd[i] += Stat.stat_nd[i];
178 #if _FFR_QUARANTINE
179 			stats.stat_nq[i] += Stat.stat_nq[i];
180 #endif /* _FFR_QUARANTINE */
181 		}
182 		stats.stat_cr += Stat.stat_cr;
183 		stats.stat_ct += Stat.stat_ct;
184 		stats.stat_cf += Stat.stat_cf;
185 	}
186 	else
187 		memmove((char *) &stats, (char *) &Stat, sizeof stats);
188 
189 	/* write out results */
190 	(void) lseek(fd, (off_t) 0, 0);
191 	(void) write(fd, (char *) &stats, sizeof stats);
192 	(void) close(fd);
193 
194 	/* clear the structure to avoid future disappointment */
195 	clearstats();
196 	entered = false;
197 }
198