BSD 4_3_Net_2 release
[unix-history] / usr / src / sys / kern / kern_acct.c
index 38f26a2..c1e07cf 100644 (file)
@@ -1,24 +1,51 @@
 /*
  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
 /*
  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * All rights reserved.
  *
  *
- *     @(#)kern_acct.c 7.12 (Berkeley) 6/28/90
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     from: @(#)kern_acct.c   7.18 (Berkeley) 5/11/91
  */
 
 #include "param.h"
 #include "systm.h"
  */
 
 #include "param.h"
 #include "systm.h"
-#include "time.h"
+#include "namei.h"
+#include "resourcevar.h"
 #include "proc.h"
 #include "ioctl.h"
 #include "termios.h"
 #include "tty.h"
 #include "proc.h"
 #include "ioctl.h"
 #include "termios.h"
 #include "tty.h"
-#include "user.h"
 #include "vnode.h"
 #include "mount.h"
 #include "kernel.h"
 #include "vnode.h"
 #include "mount.h"
 #include "kernel.h"
+#include "file.h"
 #include "acct.h"
 #include "acct.h"
-#include "uio.h"
 #include "syslog.h"
 
 /*
 #include "syslog.h"
 
 /*
 int    acctsuspend = 2;        /* stop accounting when < 2% free space left */
 int    acctresume = 4;         /* resume when free space risen to > 4% */
 struct timeval chk = { 15, 0 };/* frequency to check space for accounting */
 int    acctsuspend = 2;        /* stop accounting when < 2% free space left */
 int    acctresume = 4;         /* resume when free space risen to > 4% */
 struct timeval chk = { 15, 0 };/* frequency to check space for accounting */
+struct  vnode *acctp;          /* file to which to do accounting */
+struct  vnode *savacctp;       /* file to which to do accounting when space */
 
 /*
 
 /*
- * SHOULD REPLACE THIS WITH A DRIVER THAT CAN BE READ TO SIMPLIFY.
- */
-struct vnode *acctp;
-struct vnode *savacctp;
-
-/*
- * Perform process accounting functions.
+ * Enable or disable process accounting.
+ *
+ * If a non-null filename is given, that file is used to store accounting
+ * records on process exit. If a null filename is given process accounting
+ * is suspended. If accounting is enabled, the system checks the amount
+ * of freespace on the filesystem at timeval intervals. If the amount of
+ * freespace is below acctsuspend percent, accounting is suspended. If
+ * accounting has been suspended, and freespace rises above acctresume,
+ * accounting is resumed.
  */
 /* ARGSUSED */
 sysacct(p, uap, retval)
  */
 /* ARGSUSED */
 sysacct(p, uap, retval)
@@ -45,46 +76,11 @@ sysacct(p, uap, retval)
        } *uap;
        int *retval;
 {
        } *uap;
        int *retval;
 {
-       register struct vnode *vp;
-       register struct nameidata *ndp = &u.u_nd;
-       extern int acctwatch();
-       struct vnode *oacctp;
-       int error;
 
 
-       if (error = suser(u.u_cred, &u.u_acflag))
-               return (error);
-       if (savacctp) {
-               acctp = savacctp;
-               savacctp = NULL;
-       }
-       if (uap->fname==NULL) {
-               if (vp = acctp) {
-                       acctp = NULL;
-                       vrele(vp);
-                       untimeout(acctwatch, (caddr_t)&chk);
-               }
-               return (0);
-       }
-       ndp->ni_nameiop = LOOKUP | FOLLOW;
-       ndp->ni_segflg = UIO_USERSPACE;
-       ndp->ni_dirp = uap->fname;
-       if (error = namei(ndp))
-               return (error);
-       vp = ndp->ni_vp;
-       if (vp->v_type != VREG) {
-               vrele(vp);
-               return (EACCES);
-       }
-       if (vp->v_mount->mnt_flag & MNT_RDONLY) {
-               vrele(vp);
-               return (EROFS);
-       }
-       oacctp = acctp;
-       acctp = vp;
-       if (oacctp)
-               vrele(oacctp);
-       acctwatch(&chk);
-       return (0);
+       /*
+        * Body deleted.
+        */
+       return (ENOSYS);
 }
 
 /*
 }
 
 /*
@@ -97,7 +93,7 @@ acctwatch(resettime)
        struct statfs sb;
 
        if (savacctp) {
        struct statfs sb;
 
        if (savacctp) {
-               (void)VFS_STATFS(savacctp->v_mount, &sb);
+               (void)VFS_STATFS(savacctp->v_mount, &sb, (struct proc *)0);
                if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
                        acctp = savacctp;
                        savacctp = NULL;
                if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
                        acctp = savacctp;
                        savacctp = NULL;
@@ -107,7 +103,7 @@ acctwatch(resettime)
        }
        if (acctp == NULL)
                return;
        }
        if (acctp == NULL)
                return;
-       (void)VFS_STATFS(acctp->v_mount, &sb);
+       (void)VFS_STATFS(acctp->v_mount, &sb, (struct proc *)0);
        if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
                savacctp = acctp;
                acctp = NULL;
        if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
                savacctp = acctp;
                acctp = NULL;
@@ -117,75 +113,15 @@ acctwatch(resettime)
 }
 
 /*
 }
 
 /*
- * On exit, write a record on the accounting file.
+ * This routine calculates an accounting record for a process and,
+ * if accounting is enabled, writes it to the accounting file.
  */
 acct(p)
        register struct proc *p;
 {
  */
 acct(p)
        register struct proc *p;
 {
-       register struct rusage *ru;
-       struct vnode *vp;
-       struct timeval t, ut, st;
-       int i, s;
-       struct acct acctbuf;
-       register struct acct *ap = &acctbuf;
-
-       if ((vp = acctp) == NULL)
-               return (0);
-       bcopy(p->p_comm, ap->ac_comm, sizeof(ap->ac_comm));
-       ru = &u.u_ru;
-       s = splclock();
-       ut = p->p_utime;
-       st = p->p_stime;
-       t = time;
-       splx(s);
-       ap->ac_utime = compress(ut.tv_sec, ut.tv_usec);
-       ap->ac_stime = compress(st.tv_sec, st.tv_usec);
-       timevalsub(&t, &u.u_start);
-       ap->ac_etime = compress(t.tv_sec, t.tv_usec);
-       ap->ac_btime = u.u_start.tv_sec;
-       ap->ac_uid = p->p_ruid;
-       ap->ac_gid = p->p_rgid;
-       t = st;
-       timevaladd(&t, &ut);
-       if (i = t.tv_sec * hz + t.tv_usec / tick)
-               ap->ac_mem = (ru->ru_ixrss+ru->ru_idrss+ru->ru_isrss) / i;
-       else
-               ap->ac_mem = 0;
-       ap->ac_mem >>= CLSIZELOG2;
-       ap->ac_io = compress(ru->ru_inblock + ru->ru_oublock, (long)0);
-       if (p->p_flag&SCTTY && p->p_session->s_ttyp)
-               ap->ac_tty = p->p_session->s_ttyp->t_dev;
-       else
-               ap->ac_tty = NODEV;
-       ap->ac_flag = u.u_acflag;
-       return (vn_rdwr(UIO_WRITE, vp, (caddr_t)ap, sizeof (acctbuf),
-               (off_t)0, UIO_SYSSPACE, IO_UNIT|IO_APPEND, u.u_cred, (int *)0));
-}
-
-/*
- * Produce a pseudo-floating point representation
- * with 3 bits base-8 exponent, 13 bits fraction.
- */
-compress(t, ut)
-       register long t;
-       long ut;
-{
-       register exp = 0, round = 0;
 
 
-       t = t * AHZ;  /* compiler will convert only this format to a shift */
-       if (ut)
-               t += ut / (1000000 / AHZ);
-       while (t >= 8192) {
-               exp++;
-               round = t&04;
-               t >>= 3;
-       }
-       if (round) {
-               t++;
-               if (t >= 8192) {
-                       t >>= 3;
-                       exp++;
-               }
-       }
-       return ((exp<<13) + t);
+       /*
+        * Body deleted.
+        */
+       return;
 }
 }