xref: /csrg-svn/usr.sbin/sendmail/src/stats.c (revision 69748)
122714Sdist /*
268839Seric  * Copyright (c) 1983, 1995 Eric P. Allman
362532Sbostic  * Copyright (c) 1988, 1993
462532Sbostic  *	The Regents of the University of California.  All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822714Sdist 
922714Sdist #ifndef lint
10*69748Seric static char sccsid[] = "@(#)stats.c	8.5 (Berkeley) 05/28/95";
1133731Sbostic #endif /* not lint */
1222714Sdist 
134286Seric # include "sendmail.h"
1427681Sbloom # include "mailstats.h"
154286Seric 
169383Seric struct statistics	Stat;
1724944Seric 
1863753Seric bool	GotStats = FALSE;	/* set when we have stats to merge */
1963753Seric 
2024944Seric #define ONE_K		1000		/* one thousand (twenty-four?) */
2124944Seric #define KBYTES(x)	(((x) + (ONE_K - 1)) / ONE_K)
229383Seric /*
239383Seric **  MARKSTATS -- mark statistics
249383Seric */
259383Seric 
26*69748Seric void
markstats(e,to)279383Seric markstats(e, to)
289383Seric 	register ENVELOPE *e;
299383Seric 	register ADDRESS *to;
309383Seric {
319383Seric 	if (to == NULL)
329383Seric 	{
3325689Seric 		if (e->e_from.q_mailer != NULL)
3424945Seric 		{
3524945Seric 			Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
3624945Seric 			Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
3755012Seric 				KBYTES(e->e_msgsize);
3824945Seric 		}
399383Seric 	}
409383Seric 	else
419383Seric 	{
429383Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
4355012Seric 		Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(e->e_msgsize);
449383Seric 	}
4563753Seric 	GotStats = TRUE;
469383Seric }
479383Seric /*
484286Seric **  POSTSTATS -- post statistics in the statistics file
494286Seric **
504286Seric **	Parameters:
514286Seric **		sfile -- the name of the statistics file.
524286Seric **
534286Seric **	Returns:
544286Seric **		none.
554286Seric **
564286Seric **	Side Effects:
574286Seric **		merges the Stat structure with the sfile file.
584286Seric */
594286Seric 
60*69748Seric void
poststats(sfile)614286Seric poststats(sfile)
624286Seric 	char *sfile;
634286Seric {
644286Seric 	register int fd;
654286Seric 	struct statistics stat;
6623123Seric 	extern off_t lseek();
674286Seric 
6863753Seric 	if (sfile == NULL || !GotStats)
6923516Seric 		return;
7023516Seric 
714319Seric 	(void) time(&Stat.stat_itime);
724286Seric 	Stat.stat_size = sizeof Stat;
734286Seric 
7464368Seric 	fd = open(sfile, O_RDWR);
754286Seric 	if (fd < 0)
7611937Seric 	{
7711937Seric 		errno = 0;
784286Seric 		return;
7911937Seric 	}
8064368Seric 	(void) lockfile(fd, sfile, NULL, LOCK_EX);
814319Seric 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
824286Seric 	    stat.stat_size == sizeof stat)
834286Seric 	{
844286Seric 		/* merge current statistics into statfile */
854286Seric 		register int i;
864286Seric 
874286Seric 		for (i = 0; i < MAXMAILERS; i++)
884286Seric 		{
894286Seric 			stat.stat_nf[i] += Stat.stat_nf[i];
904286Seric 			stat.stat_bf[i] += Stat.stat_bf[i];
914286Seric 			stat.stat_nt[i] += Stat.stat_nt[i];
924286Seric 			stat.stat_bt[i] += Stat.stat_bt[i];
934286Seric 		}
944286Seric 	}
954286Seric 	else
9616887Seric 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
974286Seric 
984286Seric 	/* write out results */
9923123Seric 	(void) lseek(fd, (off_t) 0, 0);
1004319Seric 	(void) write(fd, (char *) &stat, sizeof stat);
1014319Seric 	(void) close(fd);
10263753Seric 
10363753Seric 	/* clear the structure to avoid future disappointment */
10463753Seric 	bzero(&Stat, sizeof stat);
10563753Seric 	GotStats = FALSE;
1064286Seric }
107