xref: /csrg-svn/usr.sbin/sendmail/src/stats.c (revision 24945)
122714Sdist /*
222714Sdist **  Sendmail
322714Sdist **  Copyright (c) 1983  Eric P. Allman
422714Sdist **  Berkeley, California
522714Sdist **
622714Sdist **  Copyright (c) 1983 Regents of the University of California.
722714Sdist **  All rights reserved.  The Berkeley software License Agreement
822714Sdist **  specifies the terms and conditions for redistribution.
922714Sdist */
1022714Sdist 
1122714Sdist #ifndef lint
12*24945Seric static char	SccsId[] = "@(#)stats.c	5.6 (Berkeley) 09/19/85";
1322714Sdist #endif not lint
1422714Sdist 
154286Seric # include "sendmail.h"
164286Seric 
174286Seric /*
189383Seric **  Statistics structure.
199383Seric */
209383Seric 
219383Seric struct statistics
229383Seric {
239383Seric 	time_t	stat_itime;		/* file initialization time */
249383Seric 	short	stat_size;		/* size of this structure */
259383Seric 	long	stat_nf[MAXMAILERS];	/* # msgs from each mailer */
269383Seric 	long	stat_bf[MAXMAILERS];	/* kbytes from each mailer */
279383Seric 	long	stat_nt[MAXMAILERS];	/* # msgs to each mailer */
289383Seric 	long	stat_bt[MAXMAILERS];	/* kbytes to each mailer */
299383Seric };
309383Seric 
319383Seric struct statistics	Stat;
3224944Seric 
3324944Seric #define ONE_K		1000		/* one thousand (twenty-four?) */
3424944Seric #define KBYTES(x)	(((x) + (ONE_K - 1)) / ONE_K)
359383Seric /*
369383Seric **  MARKSTATS -- mark statistics
379383Seric */
389383Seric 
399383Seric markstats(e, to)
409383Seric 	register ENVELOPE *e;
419383Seric 	register ADDRESS *to;
429383Seric {
439383Seric 	if (to == NULL)
449383Seric 	{
4524944Seric 		Stat.stat_bf[e->e_from.q_mailer->m_mno] += KBYTES(CurEnv->e_msgsize);
46*24945Seric 
47*24945Seric 		/*
48*24945Seric 		**  If is possible to get mail from an unparseable address,
49*24945Seric 		**  in this case, the q_mailer field is null, so that the
50*24945Seric 		**  indirection below causes a dereference of a NULL pointer.
51*24945Seric 		*/
52*24945Seric 
53*24945Seric 		if (e->e_from.q_mailer != NULL )
54*24945Seric 		{
55*24945Seric 			Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
56*24945Seric 			Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
57*24945Seric 				KBYTES(CurEnv->e_msgsize);
58*24945Seric 		}
599383Seric 	}
609383Seric 	else
619383Seric 	{
629383Seric 		Stat.stat_nt[to->q_mailer->m_mno]++;
6324944Seric 		Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(CurEnv->e_msgsize);
649383Seric 	}
659383Seric }
669383Seric /*
674286Seric **  POSTSTATS -- post statistics in the statistics file
684286Seric **
694286Seric **	Parameters:
704286Seric **		sfile -- the name of the statistics file.
714286Seric **
724286Seric **	Returns:
734286Seric **		none.
744286Seric **
754286Seric **	Side Effects:
764286Seric **		merges the Stat structure with the sfile file.
774286Seric */
784286Seric 
794286Seric poststats(sfile)
804286Seric 	char *sfile;
814286Seric {
824286Seric 	register int fd;
834286Seric 	struct statistics stat;
8423123Seric 	extern off_t lseek();
854286Seric 
8623516Seric 	if (sfile == NULL)
8723516Seric 		return;
8823516Seric 
894319Seric 	(void) time(&Stat.stat_itime);
904286Seric 	Stat.stat_size = sizeof Stat;
914286Seric 
924286Seric 	fd = open(sfile, 2);
934286Seric 	if (fd < 0)
9411937Seric 	{
9511937Seric 		errno = 0;
964286Seric 		return;
9711937Seric 	}
984319Seric 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
994286Seric 	    stat.stat_size == sizeof stat)
1004286Seric 	{
1014286Seric 		/* merge current statistics into statfile */
1024286Seric 		register int i;
1034286Seric 
1044286Seric 		for (i = 0; i < MAXMAILERS; i++)
1054286Seric 		{
1064286Seric 			stat.stat_nf[i] += Stat.stat_nf[i];
1074286Seric 			stat.stat_bf[i] += Stat.stat_bf[i];
1084286Seric 			stat.stat_nt[i] += Stat.stat_nt[i];
1094286Seric 			stat.stat_bt[i] += Stat.stat_bt[i];
1104286Seric 		}
1114286Seric 	}
1124286Seric 	else
11316887Seric 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
1144286Seric 
1154286Seric 	/* write out results */
11623123Seric 	(void) lseek(fd, (off_t) 0, 0);
1174319Seric 	(void) write(fd, (char *) &stat, sizeof stat);
1184319Seric 	(void) close(fd);
1194286Seric }
120