123365Smckusick /* 237728Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 339481Smckusick * All rights reserved. The Berkeley software License Agreement 439481Smckusick * specifies the terms and conditions for redistribution. 523365Smckusick * 6*44404Skarels * @(#)kern_acct.c 7.12 (Berkeley) 06/28/90 723365Smckusick */ 812788Ssam 917088Sbloom #include "param.h" 1017088Sbloom #include "systm.h" 1138931Skarels #include "time.h" 1238931Skarels #include "proc.h" 1343379Smckusick #include "ioctl.h" 1443379Smckusick #include "termios.h" 1543379Smckusick #include "tty.h" 16*44404Skarels #include "user.h" 1737728Smckusick #include "vnode.h" 1837728Smckusick #include "mount.h" 1917088Sbloom #include "kernel.h" 2017088Sbloom #include "acct.h" 2117088Sbloom #include "uio.h" 2237728Smckusick #include "syslog.h" 2312788Ssam 2412788Ssam /* 2537728Smckusick * Values associated with enabling and disabling accounting 2637728Smckusick */ 2737728Smckusick int acctsuspend = 2; /* stop accounting when < 2% free space left */ 2837728Smckusick int acctresume = 4; /* resume when free space risen to > 4% */ 2937728Smckusick struct timeval chk = { 15, 0 };/* frequency to check space for accounting */ 3037728Smckusick 3137728Smckusick /* 3212788Ssam * SHOULD REPLACE THIS WITH A DRIVER THAT CAN BE READ TO SIMPLIFY. 3312788Ssam */ 3437728Smckusick struct vnode *acctp; 3537728Smckusick struct vnode *savacctp; 3612788Ssam 3712788Ssam /* 3812788Ssam * Perform process accounting functions. 3912788Ssam */ 4043379Smckusick /* ARGSUSED */ 4143379Smckusick sysacct(p, uap, retval) 4243379Smckusick struct proc *p; 4343379Smckusick struct args { 4443379Smckusick char *fname; 4543379Smckusick } *uap; 4643379Smckusick int *retval; 4712788Ssam { 4837728Smckusick register struct vnode *vp; 4916695Smckusick register struct nameidata *ndp = &u.u_nd; 5037728Smckusick extern int acctwatch(); 5137728Smckusick struct vnode *oacctp; 5243379Smckusick int error; 5312788Ssam 5443379Smckusick if (error = suser(u.u_cred, &u.u_acflag)) 55*44404Skarels return (error); 5637553Smckusick if (savacctp) { 5737553Smckusick acctp = savacctp; 5837553Smckusick savacctp = NULL; 5937553Smckusick } 6037553Smckusick if (uap->fname==NULL) { 6137728Smckusick if (vp = acctp) { 6237553Smckusick acctp = NULL; 6337728Smckusick vrele(vp); 6437728Smckusick untimeout(acctwatch, (caddr_t)&chk); 6512788Ssam } 66*44404Skarels return (0); 6712788Ssam } 6837553Smckusick ndp->ni_nameiop = LOOKUP | FOLLOW; 6937553Smckusick ndp->ni_segflg = UIO_USERSPACE; 7037553Smckusick ndp->ni_dirp = uap->fname; 7143379Smckusick if (error = namei(ndp)) 72*44404Skarels return (error); 7337728Smckusick vp = ndp->ni_vp; 7437728Smckusick if (vp->v_type != VREG) { 7537728Smckusick vrele(vp); 76*44404Skarels return (EACCES); 7737553Smckusick } 7841400Smckusick if (vp->v_mount->mnt_flag & MNT_RDONLY) { 7937728Smckusick vrele(vp); 80*44404Skarels return (EROFS); 8137553Smckusick } 8237728Smckusick oacctp = acctp; 8337728Smckusick acctp = vp; 8437728Smckusick if (oacctp) 8537728Smckusick vrele(oacctp); 8637728Smckusick acctwatch(&chk); 87*44404Skarels return (0); 8812788Ssam } 8912788Ssam 9012788Ssam /* 9137728Smckusick * Periodically check the file system to see if accounting 9237728Smckusick * should be turned on or off. 9312788Ssam */ 9437728Smckusick acctwatch(resettime) 9537728Smckusick struct timeval *resettime; 9612788Ssam { 9737728Smckusick struct statfs sb; 9812788Ssam 9912788Ssam if (savacctp) { 10037728Smckusick (void)VFS_STATFS(savacctp->v_mount, &sb); 10137728Smckusick if (sb.f_bavail > acctresume * sb.f_blocks / 100) { 10212788Ssam acctp = savacctp; 10312788Ssam savacctp = NULL; 10437728Smckusick log(LOG_NOTICE, "Accounting resumed\n"); 10537728Smckusick return; 10612788Ssam } 10712788Ssam } 10837728Smckusick if (acctp == NULL) 10912788Ssam return; 11037728Smckusick (void)VFS_STATFS(acctp->v_mount, &sb); 11137728Smckusick if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) { 11212788Ssam savacctp = acctp; 11312788Ssam acctp = NULL; 11437728Smckusick log(LOG_NOTICE, "Accounting suspended\n"); 11537728Smckusick } 11637728Smckusick timeout(acctwatch, (caddr_t)resettime, hzto(resettime)); 11737728Smckusick } 11837728Smckusick 11937728Smckusick /* 12037728Smckusick * On exit, write a record on the accounting file. 12137728Smckusick */ 12243379Smckusick acct(p) 12343379Smckusick register struct proc *p; 12437728Smckusick { 12537728Smckusick register struct rusage *ru; 12637728Smckusick struct vnode *vp; 12740803Smarc struct timeval t, ut, st; 12840803Smarc int i, s; 12937728Smckusick struct acct acctbuf; 13037728Smckusick register struct acct *ap = &acctbuf; 13137728Smckusick 13237728Smckusick if ((vp = acctp) == NULL) 13343379Smckusick return (0); 13440803Smarc bcopy(p->p_comm, ap->ac_comm, sizeof(ap->ac_comm)); 13516715Ssam ru = &u.u_ru; 13640803Smarc s = splclock(); 13740803Smarc ut = p->p_utime; 13840803Smarc st = p->p_stime; 13916715Ssam t = time; 14040803Smarc splx(s); 14140803Smarc ap->ac_utime = compress(ut.tv_sec, ut.tv_usec); 14240803Smarc ap->ac_stime = compress(st.tv_sec, st.tv_usec); 14316715Ssam timevalsub(&t, &u.u_start); 14416715Ssam ap->ac_etime = compress(t.tv_sec, t.tv_usec); 14516715Ssam ap->ac_btime = u.u_start.tv_sec; 14643379Smckusick ap->ac_uid = p->p_ruid; 14743379Smckusick ap->ac_gid = p->p_rgid; 14840803Smarc t = st; 14940803Smarc timevaladd(&t, &ut); 15016719Skarels if (i = t.tv_sec * hz + t.tv_usec / tick) 15116715Ssam ap->ac_mem = (ru->ru_ixrss+ru->ru_idrss+ru->ru_isrss) / i; 15216715Ssam else 15316715Ssam ap->ac_mem = 0; 15416715Ssam ap->ac_mem >>= CLSIZELOG2; 15526277Skarels ap->ac_io = compress(ru->ru_inblock + ru->ru_oublock, (long)0); 15642204Smarc if (p->p_flag&SCTTY && p->p_session->s_ttyp) 15740803Smarc ap->ac_tty = p->p_session->s_ttyp->t_dev; 15812788Ssam else 15912788Ssam ap->ac_tty = NODEV; 16012788Ssam ap->ac_flag = u.u_acflag; 16143379Smckusick return (vn_rdwr(UIO_WRITE, vp, (caddr_t)ap, sizeof (acctbuf), 16243379Smckusick (off_t)0, UIO_SYSSPACE, IO_UNIT|IO_APPEND, u.u_cred, (int *)0)); 16312788Ssam } 16412788Ssam 16512788Ssam /* 16612788Ssam * Produce a pseudo-floating point representation 16712788Ssam * with 3 bits base-8 exponent, 13 bits fraction. 16812788Ssam */ 16916715Ssam compress(t, ut) 17012788Ssam register long t; 17116715Ssam long ut; 17212788Ssam { 17312788Ssam register exp = 0, round = 0; 17412788Ssam 17517501Skarels t = t * AHZ; /* compiler will convert only this format to a shift */ 17616715Ssam if (ut) 17717501Skarels t += ut / (1000000 / AHZ); 17812788Ssam while (t >= 8192) { 17912788Ssam exp++; 18012788Ssam round = t&04; 18112788Ssam t >>= 3; 18212788Ssam } 18312788Ssam if (round) { 18412788Ssam t++; 18512788Ssam if (t >= 8192) { 18612788Ssam t >>= 3; 18712788Ssam exp++; 18812788Ssam } 18912788Ssam } 19012788Ssam return ((exp<<13) + t); 19112788Ssam } 192