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*45055Smckusick * @(#)kern_acct.c 7.14 (Berkeley) 08/22/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" 1644404Skarels #include "user.h" 1737728Smckusick #include "vnode.h" 1837728Smckusick #include "mount.h" 1917088Sbloom #include "kernel.h" 20*45055Smckusick #include "file.h" 2117088Sbloom #include "acct.h" 2217088Sbloom #include "uio.h" 2337728Smckusick #include "syslog.h" 2412788Ssam 2512788Ssam /* 2637728Smckusick * Values associated with enabling and disabling accounting 2737728Smckusick */ 2837728Smckusick int acctsuspend = 2; /* stop accounting when < 2% free space left */ 2937728Smckusick int acctresume = 4; /* resume when free space risen to > 4% */ 3037728Smckusick struct timeval chk = { 15, 0 };/* frequency to check space for accounting */ 3137728Smckusick 3237728Smckusick /* 3312788Ssam * SHOULD REPLACE THIS WITH A DRIVER THAT CAN BE READ TO SIMPLIFY. 3412788Ssam */ 3537728Smckusick struct vnode *acctp; 3637728Smckusick struct vnode *savacctp; 3712788Ssam 3812788Ssam /* 3912788Ssam * Perform process accounting functions. 4012788Ssam */ 4143379Smckusick /* ARGSUSED */ 4243379Smckusick sysacct(p, uap, retval) 4343379Smckusick struct proc *p; 4443379Smckusick struct args { 4543379Smckusick char *fname; 4643379Smckusick } *uap; 4743379Smckusick int *retval; 4812788Ssam { 4937728Smckusick register struct vnode *vp; 5016695Smckusick register struct nameidata *ndp = &u.u_nd; 5137728Smckusick extern int acctwatch(); 5237728Smckusick struct vnode *oacctp; 5343379Smckusick int error; 5412788Ssam 5543379Smckusick if (error = suser(u.u_cred, &u.u_acflag)) 5644404Skarels return (error); 5737553Smckusick if (savacctp) { 5837553Smckusick acctp = savacctp; 5937553Smckusick savacctp = NULL; 6037553Smckusick } 61*45055Smckusick if (uap->fname == NULL) { 6237728Smckusick if (vp = acctp) { 6337553Smckusick acctp = NULL; 6437728Smckusick vrele(vp); 6537728Smckusick untimeout(acctwatch, (caddr_t)&chk); 6612788Ssam } 6744404Skarels return (0); 6812788Ssam } 6937553Smckusick ndp->ni_segflg = UIO_USERSPACE; 7037553Smckusick ndp->ni_dirp = uap->fname; 71*45055Smckusick if (error = vn_open(ndp, FWRITE, 0644)) 7244404Skarels return (error); 7337728Smckusick vp = ndp->ni_vp; 7437728Smckusick if (vp->v_type != VREG) { 7537728Smckusick vrele(vp); 7644404Skarels return (EACCES); 7737553Smckusick } 7837728Smckusick oacctp = acctp; 7937728Smckusick acctp = vp; 8037728Smckusick if (oacctp) 8137728Smckusick vrele(oacctp); 8237728Smckusick acctwatch(&chk); 8344404Skarels return (0); 8412788Ssam } 8512788Ssam 8612788Ssam /* 8737728Smckusick * Periodically check the file system to see if accounting 8837728Smckusick * should be turned on or off. 8912788Ssam */ 9037728Smckusick acctwatch(resettime) 9137728Smckusick struct timeval *resettime; 9212788Ssam { 9337728Smckusick struct statfs sb; 9412788Ssam 9512788Ssam if (savacctp) { 9637728Smckusick (void)VFS_STATFS(savacctp->v_mount, &sb); 9737728Smckusick if (sb.f_bavail > acctresume * sb.f_blocks / 100) { 9812788Ssam acctp = savacctp; 9912788Ssam savacctp = NULL; 10037728Smckusick log(LOG_NOTICE, "Accounting resumed\n"); 10137728Smckusick return; 10212788Ssam } 10312788Ssam } 10437728Smckusick if (acctp == NULL) 10512788Ssam return; 10637728Smckusick (void)VFS_STATFS(acctp->v_mount, &sb); 10737728Smckusick if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) { 10812788Ssam savacctp = acctp; 10912788Ssam acctp = NULL; 11037728Smckusick log(LOG_NOTICE, "Accounting suspended\n"); 11137728Smckusick } 11237728Smckusick timeout(acctwatch, (caddr_t)resettime, hzto(resettime)); 11337728Smckusick } 11437728Smckusick 11537728Smckusick /* 11637728Smckusick * On exit, write a record on the accounting file. 11737728Smckusick */ 11843379Smckusick acct(p) 11943379Smckusick register struct proc *p; 12037728Smckusick { 12137728Smckusick register struct rusage *ru; 12237728Smckusick struct vnode *vp; 12340803Smarc struct timeval t, ut, st; 12440803Smarc int i, s; 12537728Smckusick struct acct acctbuf; 12637728Smckusick register struct acct *ap = &acctbuf; 12737728Smckusick 12837728Smckusick if ((vp = acctp) == NULL) 12943379Smckusick return (0); 13040803Smarc bcopy(p->p_comm, ap->ac_comm, sizeof(ap->ac_comm)); 13116715Ssam ru = &u.u_ru; 13240803Smarc s = splclock(); 13340803Smarc ut = p->p_utime; 13440803Smarc st = p->p_stime; 13516715Ssam t = time; 13640803Smarc splx(s); 13740803Smarc ap->ac_utime = compress(ut.tv_sec, ut.tv_usec); 13840803Smarc ap->ac_stime = compress(st.tv_sec, st.tv_usec); 13916715Ssam timevalsub(&t, &u.u_start); 14016715Ssam ap->ac_etime = compress(t.tv_sec, t.tv_usec); 14116715Ssam ap->ac_btime = u.u_start.tv_sec; 14243379Smckusick ap->ac_uid = p->p_ruid; 14343379Smckusick ap->ac_gid = p->p_rgid; 14440803Smarc t = st; 14540803Smarc timevaladd(&t, &ut); 14616719Skarels if (i = t.tv_sec * hz + t.tv_usec / tick) 14716715Ssam ap->ac_mem = (ru->ru_ixrss+ru->ru_idrss+ru->ru_isrss) / i; 14816715Ssam else 14916715Ssam ap->ac_mem = 0; 15026277Skarels ap->ac_io = compress(ru->ru_inblock + ru->ru_oublock, (long)0); 15142204Smarc if (p->p_flag&SCTTY && p->p_session->s_ttyp) 15240803Smarc ap->ac_tty = p->p_session->s_ttyp->t_dev; 15312788Ssam else 15412788Ssam ap->ac_tty = NODEV; 15512788Ssam ap->ac_flag = u.u_acflag; 15643379Smckusick return (vn_rdwr(UIO_WRITE, vp, (caddr_t)ap, sizeof (acctbuf), 15743379Smckusick (off_t)0, UIO_SYSSPACE, IO_UNIT|IO_APPEND, u.u_cred, (int *)0)); 15812788Ssam } 15912788Ssam 16012788Ssam /* 16112788Ssam * Produce a pseudo-floating point representation 16212788Ssam * with 3 bits base-8 exponent, 13 bits fraction. 16312788Ssam */ 16416715Ssam compress(t, ut) 16512788Ssam register long t; 16616715Ssam long ut; 16712788Ssam { 16812788Ssam register exp = 0, round = 0; 16912788Ssam 17017501Skarels t = t * AHZ; /* compiler will convert only this format to a shift */ 17116715Ssam if (ut) 17217501Skarels t += ut / (1000000 / AHZ); 17312788Ssam while (t >= 8192) { 17412788Ssam exp++; 17512788Ssam round = t&04; 17612788Ssam t >>= 3; 17712788Ssam } 17812788Ssam if (round) { 17912788Ssam t++; 18012788Ssam if (t >= 8192) { 18112788Ssam t >>= 3; 18212788Ssam exp++; 18312788Ssam } 18412788Ssam } 18512788Ssam return ((exp<<13) + t); 18612788Ssam } 187