BSD 4_4_Lite1 release
[unix-history] / usr / src / usr.sbin / sendmail / src / stats.c
index c2b407c..2dc6827 100644 (file)
@@ -33,7 +33,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)stats.c    8.1 (Berkeley) 6/7/93";
+static char sccsid[] = "@(#)stats.c    8.3 (Berkeley) 8/28/93";
 #endif /* not lint */
 
 # include "sendmail.h"
 #endif /* not lint */
 
 # include "sendmail.h"
@@ -41,6 +41,8 @@ static char sccsid[] = "@(#)stats.c   8.1 (Berkeley) 6/7/93";
 
 struct statistics      Stat;
 
 
 struct statistics      Stat;
 
+bool   GotStats = FALSE;       /* set when we have stats to merge */
+
 #define ONE_K          1000            /* one thousand (twenty-four?) */
 #define KBYTES(x)      (((x) + (ONE_K - 1)) / ONE_K)
 \f/*
 #define ONE_K          1000            /* one thousand (twenty-four?) */
 #define KBYTES(x)      (((x) + (ONE_K - 1)) / ONE_K)
 \f/*
@@ -65,6 +67,7 @@ markstats(e, to)
                Stat.stat_nt[to->q_mailer->m_mno]++;
                Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(e->e_msgsize);
        }
                Stat.stat_nt[to->q_mailer->m_mno]++;
                Stat.stat_bt[to->q_mailer->m_mno] += KBYTES(e->e_msgsize);
        }
+       GotStats = TRUE;
 }
 \f/*
 **  POSTSTATS -- post statistics in the statistics file
 }
 \f/*
 **  POSTSTATS -- post statistics in the statistics file
@@ -86,18 +89,19 @@ poststats(sfile)
        struct statistics stat;
        extern off_t lseek();
 
        struct statistics stat;
        extern off_t lseek();
 
-       if (sfile == NULL)
+       if (sfile == NULL || !GotStats)
                return;
 
        (void) time(&Stat.stat_itime);
        Stat.stat_size = sizeof Stat;
 
                return;
 
        (void) time(&Stat.stat_itime);
        Stat.stat_size = sizeof Stat;
 
-       fd = open(sfile, 2);
+       fd = open(sfile, O_RDWR);
        if (fd < 0)
        {
                errno = 0;
                return;
        }
        if (fd < 0)
        {
                errno = 0;
                return;
        }
+       (void) lockfile(fd, sfile, NULL, LOCK_EX);
        if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
            stat.stat_size == sizeof stat)
        {
        if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
            stat.stat_size == sizeof stat)
        {
@@ -119,4 +123,8 @@ poststats(sfile)
        (void) lseek(fd, (off_t) 0, 0);
        (void) write(fd, (char *) &stat, sizeof stat);
        (void) close(fd);
        (void) lseek(fd, (off_t) 0, 0);
        (void) write(fd, (char *) &stat, sizeof stat);
        (void) close(fd);
+
+       /* clear the structure to avoid future disappointment */
+       bzero(&Stat, sizeof stat);
+       GotStats = FALSE;
 }
 }