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 #include <sendmail.h>
157c478bd9Sstevel@tonic-gate
16*058561cbSjbeck SM_RCSID("@(#)$Id: stats.c,v 8.57 2006/08/15 23:24:58 ca Exp $")
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #include <sendmail/mailstats.h>
197c478bd9Sstevel@tonic-gate
207c478bd9Sstevel@tonic-gate static struct statistics Stat;
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate static bool GotStats = false; /* set when we have stats to merge */
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate /* See http://physics.nist.gov/cuu/Units/binary.html */
257c478bd9Sstevel@tonic-gate #define ONE_K 1000 /* one thousand (twenty-four?) */
267c478bd9Sstevel@tonic-gate #define KBYTES(x) (((x) + (ONE_K - 1)) / ONE_K)
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate ** MARKSTATS -- mark statistics
297c478bd9Sstevel@tonic-gate **
307c478bd9Sstevel@tonic-gate ** Parameters:
317c478bd9Sstevel@tonic-gate ** e -- the envelope.
327c478bd9Sstevel@tonic-gate ** to -- to address.
337c478bd9Sstevel@tonic-gate ** type -- type of stats this represents.
347c478bd9Sstevel@tonic-gate **
357c478bd9Sstevel@tonic-gate ** Returns:
367c478bd9Sstevel@tonic-gate ** none.
377c478bd9Sstevel@tonic-gate **
387c478bd9Sstevel@tonic-gate ** Side Effects:
397c478bd9Sstevel@tonic-gate ** changes static Stat structure
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate void
markstats(e,to,type)437c478bd9Sstevel@tonic-gate markstats(e, to, type)
447c478bd9Sstevel@tonic-gate register ENVELOPE *e;
457c478bd9Sstevel@tonic-gate register ADDRESS *to;
467c478bd9Sstevel@tonic-gate int type;
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate switch (type)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate case STATS_QUARANTINE:
517c478bd9Sstevel@tonic-gate if (e->e_from.q_mailer != NULL)
527c478bd9Sstevel@tonic-gate Stat.stat_nq[e->e_from.q_mailer->m_mno]++;
537c478bd9Sstevel@tonic-gate break;
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate case STATS_REJECT:
567c478bd9Sstevel@tonic-gate if (e->e_from.q_mailer != NULL)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate if (bitset(EF_DISCARD, e->e_flags))
597c478bd9Sstevel@tonic-gate Stat.stat_nd[e->e_from.q_mailer->m_mno]++;
607c478bd9Sstevel@tonic-gate else
617c478bd9Sstevel@tonic-gate Stat.stat_nr[e->e_from.q_mailer->m_mno]++;
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate Stat.stat_cr++;
647c478bd9Sstevel@tonic-gate break;
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate case STATS_CONNECT:
677c478bd9Sstevel@tonic-gate if (to == NULL)
687c478bd9Sstevel@tonic-gate Stat.stat_cf++;
697c478bd9Sstevel@tonic-gate else
707c478bd9Sstevel@tonic-gate Stat.stat_ct++;
717c478bd9Sstevel@tonic-gate break;
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate case STATS_NORMAL:
747c478bd9Sstevel@tonic-gate if (to == NULL)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate if (e->e_from.q_mailer != NULL)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
797c478bd9Sstevel@tonic-gate Stat.stat_bf[e->e_from.q_mailer->m_mno] +=
807c478bd9Sstevel@tonic-gate KBYTES(e->e_msgsize);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate else
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate Stat.stat_nt[to->q_mailer->m_mno]++;
867c478bd9Sstevel@tonic-gate Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(e->e_msgsize);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate break;
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate default:
917c478bd9Sstevel@tonic-gate /* Silently ignore bogus call */
927c478bd9Sstevel@tonic-gate return;
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate GotStats = true;
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate ** CLEARSTATS -- clear statistics structure
1007c478bd9Sstevel@tonic-gate **
1017c478bd9Sstevel@tonic-gate ** Parameters:
1027c478bd9Sstevel@tonic-gate ** none.
1037c478bd9Sstevel@tonic-gate **
1047c478bd9Sstevel@tonic-gate ** Returns:
1057c478bd9Sstevel@tonic-gate ** none.
1067c478bd9Sstevel@tonic-gate **
1077c478bd9Sstevel@tonic-gate ** Side Effects:
1087c478bd9Sstevel@tonic-gate ** clears the Stat structure.
1097c478bd9Sstevel@tonic-gate */
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate void
clearstats()1127c478bd9Sstevel@tonic-gate clearstats()
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate /* clear the structure to avoid future disappointment */
115*058561cbSjbeck memset(&Stat, '\0', sizeof(Stat));
1167c478bd9Sstevel@tonic-gate GotStats = false;
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate ** POSTSTATS -- post statistics in the statistics file
1207c478bd9Sstevel@tonic-gate **
1217c478bd9Sstevel@tonic-gate ** Parameters:
1227c478bd9Sstevel@tonic-gate ** sfile -- the name of the statistics file.
1237c478bd9Sstevel@tonic-gate **
1247c478bd9Sstevel@tonic-gate ** Returns:
1257c478bd9Sstevel@tonic-gate ** none.
1267c478bd9Sstevel@tonic-gate **
1277c478bd9Sstevel@tonic-gate ** Side Effects:
1287c478bd9Sstevel@tonic-gate ** merges the Stat structure with the sfile file.
1297c478bd9Sstevel@tonic-gate */
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate void
poststats(sfile)1327c478bd9Sstevel@tonic-gate poststats(sfile)
1337c478bd9Sstevel@tonic-gate char *sfile;
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate int fd;
1367c478bd9Sstevel@tonic-gate static bool entered = false;
1377c478bd9Sstevel@tonic-gate long sff = SFF_REGONLY|SFF_OPENASROOT;
1387c478bd9Sstevel@tonic-gate struct statistics stats;
1397c478bd9Sstevel@tonic-gate extern off_t lseek();
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate if (sfile == NULL || *sfile == '\0' || !GotStats || entered)
1427c478bd9Sstevel@tonic-gate return;
1437c478bd9Sstevel@tonic-gate entered = true;
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate (void) time(&Stat.stat_itime);
146*058561cbSjbeck Stat.stat_size = sizeof(Stat);
1477c478bd9Sstevel@tonic-gate Stat.stat_magic = STAT_MAGIC;
1487c478bd9Sstevel@tonic-gate Stat.stat_version = STAT_VERSION;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate if (!bitnset(DBS_WRITESTATSTOSYMLINK, DontBlameSendmail))
1517c478bd9Sstevel@tonic-gate sff |= SFF_NOSLINK;
1527c478bd9Sstevel@tonic-gate if (!bitnset(DBS_WRITESTATSTOHARDLINK, DontBlameSendmail))
1537c478bd9Sstevel@tonic-gate sff |= SFF_NOHLINK;
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate fd = safeopen(sfile, O_RDWR, 0600, sff);
1567c478bd9Sstevel@tonic-gate if (fd < 0)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate if (LogLevel > 12)
1597c478bd9Sstevel@tonic-gate sm_syslog(LOG_INFO, NOQID, "poststats: %s: %s",
1607c478bd9Sstevel@tonic-gate sfile, sm_errstring(errno));
1617c478bd9Sstevel@tonic-gate errno = 0;
1627c478bd9Sstevel@tonic-gate entered = false;
1637c478bd9Sstevel@tonic-gate return;
1647c478bd9Sstevel@tonic-gate }
165*058561cbSjbeck if (read(fd, (char *) &stats, sizeof(stats)) == sizeof(stats) &&
166*058561cbSjbeck stats.stat_size == sizeof(stats) &&
1677c478bd9Sstevel@tonic-gate stats.stat_magic == Stat.stat_magic &&
1687c478bd9Sstevel@tonic-gate stats.stat_version == Stat.stat_version)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate /* merge current statistics into statfile */
1717c478bd9Sstevel@tonic-gate register int i;
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate for (i = 0; i < MAXMAILERS; i++)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate stats.stat_nf[i] += Stat.stat_nf[i];
1767c478bd9Sstevel@tonic-gate stats.stat_bf[i] += Stat.stat_bf[i];
1777c478bd9Sstevel@tonic-gate stats.stat_nt[i] += Stat.stat_nt[i];
1787c478bd9Sstevel@tonic-gate stats.stat_bt[i] += Stat.stat_bt[i];
1797c478bd9Sstevel@tonic-gate stats.stat_nr[i] += Stat.stat_nr[i];
1807c478bd9Sstevel@tonic-gate stats.stat_nd[i] += Stat.stat_nd[i];
1817c478bd9Sstevel@tonic-gate stats.stat_nq[i] += Stat.stat_nq[i];
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate stats.stat_cr += Stat.stat_cr;
1847c478bd9Sstevel@tonic-gate stats.stat_ct += Stat.stat_ct;
1857c478bd9Sstevel@tonic-gate stats.stat_cf += Stat.stat_cf;
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate else
188*058561cbSjbeck memmove((char *) &stats, (char *) &Stat, sizeof(stats));
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate /* write out results */
1917c478bd9Sstevel@tonic-gate (void) lseek(fd, (off_t) 0, 0);
192*058561cbSjbeck (void) write(fd, (char *) &stats, sizeof(stats));
1937c478bd9Sstevel@tonic-gate (void) close(fd);
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate /* clear the structure to avoid future disappointment */
1967c478bd9Sstevel@tonic-gate clearstats();
1977c478bd9Sstevel@tonic-gate entered = false;
1987c478bd9Sstevel@tonic-gate }
199