149587Sbostic /*- 249587Sbostic * Copyright (c) 1982, 1986, 1989 The Regents of the University of California. 349587Sbostic * All rights reserved. 423365Smckusick * 549587Sbostic * %sccs.include.proprietary.c% 649587Sbostic * 7*49916Smckusick * @(#)kern_acct.c 7.19 (Berkeley) 05/28/91 823365Smckusick */ 912788Ssam 1017088Sbloom #include "param.h" 1117088Sbloom #include "systm.h" 1247545Skarels #include "namei.h" 1347545Skarels #include "resourcevar.h" 1438931Skarels #include "proc.h" 1543379Smckusick #include "ioctl.h" 1643379Smckusick #include "termios.h" 1743379Smckusick #include "tty.h" 1837728Smckusick #include "vnode.h" 1937728Smckusick #include "mount.h" 2017088Sbloom #include "kernel.h" 2145055Smckusick #include "file.h" 2217088Sbloom #include "acct.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 /* 3949671Smckusick * Enable or disable process accounting. 4049671Smckusick * 4149671Smckusick * If a non-null filename is given, that file is used to store accounting 4249671Smckusick * records on process exit. If a null filename is given process accounting 4349671Smckusick * is suspended. If accounting is enabled, the system checks the amount 4449671Smckusick * of freespace on the filesystem at timeval intervals. If the amount of 4549671Smckusick * freespace is below acctsuspend percent, accounting is suspended. If 4649671Smckusick * accounting has been suspended, and freespace rises above acctresume, 4749671Smckusick * accounting is resumed. 4812788Ssam */ 4943379Smckusick /* ARGSUSED */ 5043379Smckusick sysacct(p, uap, retval) 5143379Smckusick struct proc *p; 5243379Smckusick struct args { 5343379Smckusick char *fname; 5443379Smckusick } *uap; 5543379Smckusick int *retval; 5612788Ssam { 5737728Smckusick register struct vnode *vp; 5837728Smckusick extern int acctwatch(); 5937728Smckusick struct vnode *oacctp; 6043379Smckusick int error; 6147545Skarels struct nameidata nd; 6212788Ssam 6347545Skarels if (error = suser(p->p_ucred, &p->p_acflag)) 6444404Skarels return (error); 6537553Smckusick if (savacctp) { 6637553Smckusick acctp = savacctp; 6737553Smckusick savacctp = NULL; 6837553Smckusick } 6945055Smckusick if (uap->fname == NULL) { 7037728Smckusick if (vp = acctp) { 7137553Smckusick acctp = NULL; 7237728Smckusick vrele(vp); 7337728Smckusick untimeout(acctwatch, (caddr_t)&chk); 7412788Ssam } 7544404Skarels return (0); 7612788Ssam } 7747545Skarels nd.ni_segflg = UIO_USERSPACE; 7847545Skarels nd.ni_dirp = uap->fname; 7947545Skarels if (error = vn_open(&nd, p, FWRITE, 0644)) 8044404Skarels return (error); 8147545Skarels vp = nd.ni_vp; 82*49916Smckusick VOP_UNLOCK(vp); 8337728Smckusick if (vp->v_type != VREG) { 8437728Smckusick vrele(vp); 8544404Skarels return (EACCES); 8637553Smckusick } 8737728Smckusick oacctp = acctp; 8837728Smckusick acctp = vp; 8937728Smckusick if (oacctp) 9037728Smckusick vrele(oacctp); 9137728Smckusick acctwatch(&chk); 9244404Skarels return (0); 9312788Ssam } 9412788Ssam 9512788Ssam /* 9637728Smckusick * Periodically check the file system to see if accounting 9737728Smckusick * should be turned on or off. 9812788Ssam */ 9937728Smckusick acctwatch(resettime) 10037728Smckusick struct timeval *resettime; 10112788Ssam { 10237728Smckusick struct statfs sb; 10312788Ssam 10412788Ssam if (savacctp) { 10548021Smckusick (void)VFS_STATFS(savacctp->v_mount, &sb, (struct proc *)0); 10637728Smckusick if (sb.f_bavail > acctresume * sb.f_blocks / 100) { 10712788Ssam acctp = savacctp; 10812788Ssam savacctp = NULL; 10937728Smckusick log(LOG_NOTICE, "Accounting resumed\n"); 11037728Smckusick return; 11112788Ssam } 11212788Ssam } 11337728Smckusick if (acctp == NULL) 11412788Ssam return; 11548021Smckusick (void)VFS_STATFS(acctp->v_mount, &sb, (struct proc *)0); 11637728Smckusick if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) { 11712788Ssam savacctp = acctp; 11812788Ssam acctp = NULL; 11937728Smckusick log(LOG_NOTICE, "Accounting suspended\n"); 12037728Smckusick } 12137728Smckusick timeout(acctwatch, (caddr_t)resettime, hzto(resettime)); 12237728Smckusick } 12337728Smckusick 12437728Smckusick /* 12549671Smckusick * This routine calculates an accounting record for a process and, 12649671Smckusick * if accounting is enabled, writes it to the accounting file. 12737728Smckusick */ 12843379Smckusick acct(p) 12943379Smckusick register struct proc *p; 13037728Smckusick { 13137728Smckusick register struct rusage *ru; 13237728Smckusick struct vnode *vp; 13340803Smarc struct timeval t, ut, st; 13440803Smarc int i, s; 13537728Smckusick struct acct acctbuf; 13637728Smckusick register struct acct *ap = &acctbuf; 13737728Smckusick 13837728Smckusick if ((vp = acctp) == NULL) 13943379Smckusick return (0); 14040803Smarc bcopy(p->p_comm, ap->ac_comm, sizeof(ap->ac_comm)); 14147545Skarels ru = &p->p_stats->p_ru; 14240803Smarc s = splclock(); 14340803Smarc ut = p->p_utime; 14440803Smarc st = p->p_stime; 14516715Ssam t = time; 14640803Smarc splx(s); 14740803Smarc ap->ac_utime = compress(ut.tv_sec, ut.tv_usec); 14840803Smarc ap->ac_stime = compress(st.tv_sec, st.tv_usec); 14947545Skarels timevalsub(&t, &p->p_stats->p_start); 15016715Ssam ap->ac_etime = compress(t.tv_sec, t.tv_usec); 15147545Skarels ap->ac_btime = p->p_stats->p_start.tv_sec; 15247545Skarels ap->ac_uid = p->p_cred->p_ruid; 15347545Skarels ap->ac_gid = p->p_cred->p_rgid; 15440803Smarc t = st; 15540803Smarc timevaladd(&t, &ut); 15616719Skarels if (i = t.tv_sec * hz + t.tv_usec / tick) 15747545Skarels ap->ac_mem = (ru->ru_ixrss + ru->ru_idrss + ru->ru_isrss) / i; 15816715Ssam else 15916715Ssam ap->ac_mem = 0; 16026277Skarels ap->ac_io = compress(ru->ru_inblock + ru->ru_oublock, (long)0); 16142204Smarc if (p->p_flag&SCTTY && p->p_session->s_ttyp) 16240803Smarc ap->ac_tty = p->p_session->s_ttyp->t_dev; 16312788Ssam else 16412788Ssam ap->ac_tty = NODEV; 16547545Skarels ap->ac_flag = p->p_acflag; 16648021Smckusick return (vn_rdwr(UIO_WRITE, vp, (caddr_t)ap, sizeof (acctbuf), (off_t)0, 16748021Smckusick UIO_SYSSPACE, IO_UNIT|IO_APPEND, p->p_ucred, (int *)0, 16848021Smckusick (struct proc *)0)); 16912788Ssam } 17012788Ssam 17112788Ssam /* 17212788Ssam * Produce a pseudo-floating point representation 17312788Ssam * with 3 bits base-8 exponent, 13 bits fraction. 17412788Ssam */ 17516715Ssam compress(t, ut) 17612788Ssam register long t; 17716715Ssam long ut; 17812788Ssam { 17912788Ssam register exp = 0, round = 0; 18012788Ssam 18117501Skarels t = t * AHZ; /* compiler will convert only this format to a shift */ 18216715Ssam if (ut) 18317501Skarels t += ut / (1000000 / AHZ); 18412788Ssam while (t >= 8192) { 18512788Ssam exp++; 18612788Ssam round = t&04; 18712788Ssam t >>= 3; 18812788Ssam } 18912788Ssam if (round) { 19012788Ssam t++; 19112788Ssam if (t >= 8192) { 19212788Ssam t >>= 3; 19312788Ssam exp++; 19412788Ssam } 19512788Ssam } 19612788Ssam return ((exp<<13) + t); 19712788Ssam } 198