xref: /csrg-svn/usr.sbin/sendmail/src/stats.c (revision 42829)
122714Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333731Sbostic  * Copyright (c) 1988 Regents of the University of California.
433731Sbostic  * All rights reserved.
533731Sbostic  *
6*42829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822714Sdist 
922714Sdist #ifndef lint
10*42829Sbostic static char sccsid[] = "@(#)stats.c	5.11 (Berkeley) 06/01/90";
1133731Sbostic #endif /* not lint */
1222714Sdist 
134286Seric # include "sendmail.h"
1427681Sbloom # include "mailstats.h"
154286Seric 
169383Seric struct statistics	Stat;
1724944Seric 
1824944Seric #define ONE_K		1000		/* one thousand (twenty-four?) */
1924944Seric #define KBYTES(x)	(((x) + (ONE_K - 1)) / ONE_K)
209383Seric /*
219383Seric **  MARKSTATS -- mark statistics
229383Seric */
239383Seric 
249383Seric markstats(e, to)
259383Seric 	register ENVELOPE *e;
269383Seric 	register ADDRESS *to;
279383Seric {
289383Seric 	if (to == NULL)
299383Seric 	{
3025689Seric 		if (e->e_from.q_mailer != NULL)
3124945Seric 		{
3224945Seric 			Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
3324945Seric 			Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
3424945Seric 				KBYTES(CurEnv->e_msgsize);
3524945Seric 		}
369383Seric 	}
379383Seric 	else
389383Seric 	{
399383Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
4024944Seric 		Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(CurEnv->e_msgsize);
419383Seric 	}
429383Seric }
439383Seric /*
444286Seric **  POSTSTATS -- post statistics in the statistics file
454286Seric **
464286Seric **	Parameters:
474286Seric **		sfile -- the name of the statistics file.
484286Seric **
494286Seric **	Returns:
504286Seric **		none.
514286Seric **
524286Seric **	Side Effects:
534286Seric **		merges the Stat structure with the sfile file.
544286Seric */
554286Seric 
564286Seric poststats(sfile)
574286Seric 	char *sfile;
584286Seric {
594286Seric 	register int fd;
604286Seric 	struct statistics stat;
6123123Seric 	extern off_t lseek();
624286Seric 
6323516Seric 	if (sfile == NULL)
6423516Seric 		return;
6523516Seric 
664319Seric 	(void) time(&Stat.stat_itime);
674286Seric 	Stat.stat_size = sizeof Stat;
684286Seric 
694286Seric 	fd = open(sfile, 2);
704286Seric 	if (fd < 0)
7111937Seric 	{
7211937Seric 		errno = 0;
734286Seric 		return;
7411937Seric 	}
754319Seric 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
764286Seric 	    stat.stat_size == sizeof stat)
774286Seric 	{
784286Seric 		/* merge current statistics into statfile */
794286Seric 		register int i;
804286Seric 
814286Seric 		for (i = 0; i < MAXMAILERS; i++)
824286Seric 		{
834286Seric 			stat.stat_nf[i] += Stat.stat_nf[i];
844286Seric 			stat.stat_bf[i] += Stat.stat_bf[i];
854286Seric 			stat.stat_nt[i] += Stat.stat_nt[i];
864286Seric 			stat.stat_bt[i] += Stat.stat_bt[i];
874286Seric 		}
884286Seric 	}
894286Seric 	else
9016887Seric 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
914286Seric 
924286Seric 	/* write out results */
9323123Seric 	(void) lseek(fd, (off_t) 0, 0);
944319Seric 	(void) write(fd, (char *) &stat, sizeof stat);
954319Seric 	(void) close(fd);
964286Seric }
97