xref: /illumos-gate/usr/src/cmd/sendmail/src/stats.c (revision 058561cbaa119a6f2659bc27ef343e1b47266bb2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1998-2002 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *	All rights reserved.
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1993
67c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
97c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
107c478bd9Sstevel@tonic-gate  * the sendmail distribution.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate #include <sendmail.h>
177c478bd9Sstevel@tonic-gate 
18*058561cbSjbeck SM_RCSID("@(#)$Id: stats.c,v 8.57 2006/08/15 23:24:58 ca Exp $")
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include <sendmail/mailstats.h>
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate static struct statistics	Stat;
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate static bool	GotStats = false;	/* set when we have stats to merge */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /* See http://physics.nist.gov/cuu/Units/binary.html */
277c478bd9Sstevel@tonic-gate #define ONE_K		1000		/* one thousand (twenty-four?) */
287c478bd9Sstevel@tonic-gate #define KBYTES(x)	(((x) + (ONE_K - 1)) / ONE_K)
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate **  MARKSTATS -- mark statistics
317c478bd9Sstevel@tonic-gate **
327c478bd9Sstevel@tonic-gate **	Parameters:
337c478bd9Sstevel@tonic-gate **		e -- the envelope.
347c478bd9Sstevel@tonic-gate **		to -- to address.
357c478bd9Sstevel@tonic-gate **		type -- type of stats this represents.
367c478bd9Sstevel@tonic-gate **
377c478bd9Sstevel@tonic-gate **	Returns:
387c478bd9Sstevel@tonic-gate **		none.
397c478bd9Sstevel@tonic-gate **
407c478bd9Sstevel@tonic-gate **	Side Effects:
417c478bd9Sstevel@tonic-gate **		changes static Stat structure
427c478bd9Sstevel@tonic-gate */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate void
457c478bd9Sstevel@tonic-gate markstats(e, to, type)
467c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
477c478bd9Sstevel@tonic-gate 	register ADDRESS *to;
487c478bd9Sstevel@tonic-gate 	int type;
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	switch (type)
517c478bd9Sstevel@tonic-gate 	{
527c478bd9Sstevel@tonic-gate 	  case STATS_QUARANTINE:
537c478bd9Sstevel@tonic-gate 		if (e->e_from.q_mailer != NULL)
547c478bd9Sstevel@tonic-gate 			Stat.stat_nq[e->e_from.q_mailer->m_mno]++;
557c478bd9Sstevel@tonic-gate 		break;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	  case STATS_REJECT:
587c478bd9Sstevel@tonic-gate 		if (e->e_from.q_mailer != NULL)
597c478bd9Sstevel@tonic-gate 		{
607c478bd9Sstevel@tonic-gate 			if (bitset(EF_DISCARD, e->e_flags))
617c478bd9Sstevel@tonic-gate 				Stat.stat_nd[e->e_from.q_mailer->m_mno]++;
627c478bd9Sstevel@tonic-gate 			else
637c478bd9Sstevel@tonic-gate 				Stat.stat_nr[e->e_from.q_mailer->m_mno]++;
647c478bd9Sstevel@tonic-gate 		}
657c478bd9Sstevel@tonic-gate 		Stat.stat_cr++;
667c478bd9Sstevel@tonic-gate 		break;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	  case STATS_CONNECT:
697c478bd9Sstevel@tonic-gate 		if (to == NULL)
707c478bd9Sstevel@tonic-gate 			Stat.stat_cf++;
717c478bd9Sstevel@tonic-gate 		else
727c478bd9Sstevel@tonic-gate 			Stat.stat_ct++;
737c478bd9Sstevel@tonic-gate 		break;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	  case STATS_NORMAL:
767c478bd9Sstevel@tonic-gate 		if (to == NULL)
777c478bd9Sstevel@tonic-gate 		{
787c478bd9Sstevel@tonic-gate 			if (e->e_from.q_mailer != NULL)
797c478bd9Sstevel@tonic-gate 			{
807c478bd9Sstevel@tonic-gate 				Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
817c478bd9Sstevel@tonic-gate 				Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
827c478bd9Sstevel@tonic-gate 					KBYTES(e->e_msgsize);
837c478bd9Sstevel@tonic-gate 			}
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 		else
867c478bd9Sstevel@tonic-gate 		{
877c478bd9Sstevel@tonic-gate 			Stat.stat_nt[to->q_mailer->m_mno]++;
887c478bd9Sstevel@tonic-gate 			Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(e->e_msgsize);
897c478bd9Sstevel@tonic-gate 		}
907c478bd9Sstevel@tonic-gate 		break;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	  default:
937c478bd9Sstevel@tonic-gate 		/* Silently ignore bogus call */
947c478bd9Sstevel@tonic-gate 		return;
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	GotStats = true;
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate **  CLEARSTATS -- clear statistics structure
1027c478bd9Sstevel@tonic-gate **
1037c478bd9Sstevel@tonic-gate **	Parameters:
1047c478bd9Sstevel@tonic-gate **		none.
1057c478bd9Sstevel@tonic-gate **
1067c478bd9Sstevel@tonic-gate **	Returns:
1077c478bd9Sstevel@tonic-gate **		none.
1087c478bd9Sstevel@tonic-gate **
1097c478bd9Sstevel@tonic-gate **	Side Effects:
1107c478bd9Sstevel@tonic-gate **		clears the Stat structure.
1117c478bd9Sstevel@tonic-gate */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate void
1147c478bd9Sstevel@tonic-gate clearstats()
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	/* clear the structure to avoid future disappointment */
117*058561cbSjbeck 	memset(&Stat, '\0', sizeof(Stat));
1187c478bd9Sstevel@tonic-gate 	GotStats = false;
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate **  POSTSTATS -- post statistics in the statistics file
1227c478bd9Sstevel@tonic-gate **
1237c478bd9Sstevel@tonic-gate **	Parameters:
1247c478bd9Sstevel@tonic-gate **		sfile -- the name of the statistics file.
1257c478bd9Sstevel@tonic-gate **
1267c478bd9Sstevel@tonic-gate **	Returns:
1277c478bd9Sstevel@tonic-gate **		none.
1287c478bd9Sstevel@tonic-gate **
1297c478bd9Sstevel@tonic-gate **	Side Effects:
1307c478bd9Sstevel@tonic-gate **		merges the Stat structure with the sfile file.
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate void
1347c478bd9Sstevel@tonic-gate poststats(sfile)
1357c478bd9Sstevel@tonic-gate 	char *sfile;
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	int fd;
1387c478bd9Sstevel@tonic-gate 	static bool entered = false;
1397c478bd9Sstevel@tonic-gate 	long sff = SFF_REGONLY|SFF_OPENASROOT;
1407c478bd9Sstevel@tonic-gate 	struct statistics stats;
1417c478bd9Sstevel@tonic-gate 	extern off_t lseek();
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (sfile == NULL || *sfile == '\0' || !GotStats || entered)
1447c478bd9Sstevel@tonic-gate 		return;
1457c478bd9Sstevel@tonic-gate 	entered = true;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	(void) time(&Stat.stat_itime);
148*058561cbSjbeck 	Stat.stat_size = sizeof(Stat);
1497c478bd9Sstevel@tonic-gate 	Stat.stat_magic = STAT_MAGIC;
1507c478bd9Sstevel@tonic-gate 	Stat.stat_version = STAT_VERSION;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (!bitnset(DBS_WRITESTATSTOSYMLINK, DontBlameSendmail))
1537c478bd9Sstevel@tonic-gate 		sff |= SFF_NOSLINK;
1547c478bd9Sstevel@tonic-gate 	if (!bitnset(DBS_WRITESTATSTOHARDLINK, DontBlameSendmail))
1557c478bd9Sstevel@tonic-gate 		sff |= SFF_NOHLINK;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	fd = safeopen(sfile, O_RDWR, 0600, sff);
1587c478bd9Sstevel@tonic-gate 	if (fd < 0)
1597c478bd9Sstevel@tonic-gate 	{
1607c478bd9Sstevel@tonic-gate 		if (LogLevel > 12)
1617c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_INFO, NOQID, "poststats: %s: %s",
1627c478bd9Sstevel@tonic-gate 				  sfile, sm_errstring(errno));
1637c478bd9Sstevel@tonic-gate 		errno = 0;
1647c478bd9Sstevel@tonic-gate 		entered = false;
1657c478bd9Sstevel@tonic-gate 		return;
1667c478bd9Sstevel@tonic-gate 	}
167*058561cbSjbeck 	if (read(fd, (char *) &stats, sizeof(stats)) == sizeof(stats) &&
168*058561cbSjbeck 	    stats.stat_size == sizeof(stats) &&
1697c478bd9Sstevel@tonic-gate 	    stats.stat_magic == Stat.stat_magic &&
1707c478bd9Sstevel@tonic-gate 	    stats.stat_version == Stat.stat_version)
1717c478bd9Sstevel@tonic-gate 	{
1727c478bd9Sstevel@tonic-gate 		/* merge current statistics into statfile */
1737c478bd9Sstevel@tonic-gate 		register int i;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		for (i = 0; i < MAXMAILERS; i++)
1767c478bd9Sstevel@tonic-gate 		{
1777c478bd9Sstevel@tonic-gate 			stats.stat_nf[i] += Stat.stat_nf[i];
1787c478bd9Sstevel@tonic-gate 			stats.stat_bf[i] += Stat.stat_bf[i];
1797c478bd9Sstevel@tonic-gate 			stats.stat_nt[i] += Stat.stat_nt[i];
1807c478bd9Sstevel@tonic-gate 			stats.stat_bt[i] += Stat.stat_bt[i];
1817c478bd9Sstevel@tonic-gate 			stats.stat_nr[i] += Stat.stat_nr[i];
1827c478bd9Sstevel@tonic-gate 			stats.stat_nd[i] += Stat.stat_nd[i];
1837c478bd9Sstevel@tonic-gate 			stats.stat_nq[i] += Stat.stat_nq[i];
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 		stats.stat_cr += Stat.stat_cr;
1867c478bd9Sstevel@tonic-gate 		stats.stat_ct += Stat.stat_ct;
1877c478bd9Sstevel@tonic-gate 		stats.stat_cf += Stat.stat_cf;
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 	else
190*058561cbSjbeck 		memmove((char *) &stats, (char *) &Stat, sizeof(stats));
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/* write out results */
1937c478bd9Sstevel@tonic-gate 	(void) lseek(fd, (off_t) 0, 0);
194*058561cbSjbeck 	(void) write(fd, (char *) &stats, sizeof(stats));
1957c478bd9Sstevel@tonic-gate 	(void) close(fd);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* clear the structure to avoid future disappointment */
1987c478bd9Sstevel@tonic-gate 	clearstats();
1997c478bd9Sstevel@tonic-gate 	entered = false;
2007c478bd9Sstevel@tonic-gate }
201