Home | History | Annotate | Line # | Download | only in kern
kern_acct.c revision 1.39
      1  1.39  mycroft /*	$NetBSD: kern_acct.c,v 1.39 1995/03/21 13:33:46 mycroft Exp $	*/
      2  1.30      cgd 
      3  1.23      cgd /*-
      4  1.24      cgd  * Copyright (c) 1994 Christopher G. Demetriou
      5  1.23      cgd  * Copyright (c) 1982, 1986, 1989, 1993
      6  1.23      cgd  *	The Regents of the University of California.  All rights reserved.
      7  1.22      cgd  * (c) UNIX System Laboratories, Inc.
      8  1.22      cgd  * All or some portions of this file are derived from material licensed
      9  1.22      cgd  * to the University of California by American Telephone and Telegraph
     10  1.22      cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     11  1.22      cgd  * the permission of UNIX System Laboratories, Inc.
     12  1.22      cgd  *
     13  1.22      cgd  * Redistribution and use in source and binary forms, with or without
     14  1.22      cgd  * modification, are permitted provided that the following conditions
     15  1.22      cgd  * are met:
     16  1.22      cgd  * 1. Redistributions of source code must retain the above copyright
     17  1.22      cgd  *    notice, this list of conditions and the following disclaimer.
     18  1.22      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.22      cgd  *    notice, this list of conditions and the following disclaimer in the
     20  1.22      cgd  *    documentation and/or other materials provided with the distribution.
     21  1.22      cgd  * 3. All advertising materials mentioning features or use of this software
     22  1.22      cgd  *    must display the following acknowledgement:
     23  1.22      cgd  *	This product includes software developed by the University of
     24  1.22      cgd  *	California, Berkeley and its contributors.
     25  1.22      cgd  * 4. Neither the name of the University nor the names of its contributors
     26  1.22      cgd  *    may be used to endorse or promote products derived from this software
     27  1.22      cgd  *    without specific prior written permission.
     28  1.22      cgd  *
     29  1.22      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  1.22      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  1.22      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  1.22      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  1.22      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  1.22      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  1.22      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  1.22      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  1.22      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  1.22      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  1.22      cgd  * SUCH DAMAGE.
     40  1.22      cgd  *
     41  1.30      cgd  *	@(#)kern_acct.c	8.1 (Berkeley) 6/14/93
     42  1.22      cgd  */
     43  1.22      cgd 
     44  1.22      cgd #include <sys/param.h>
     45  1.33      cgd #include <sys/systm.h>
     46  1.22      cgd #include <sys/proc.h>
     47  1.23      cgd #include <sys/mount.h>
     48  1.22      cgd #include <sys/vnode.h>
     49  1.22      cgd #include <sys/file.h>
     50  1.22      cgd #include <sys/syslog.h>
     51  1.23      cgd #include <sys/kernel.h>
     52  1.24      cgd #include <sys/namei.h>
     53  1.24      cgd #include <sys/errno.h>
     54  1.24      cgd #include <sys/acct.h>
     55  1.24      cgd #include <sys/resourcevar.h>
     56  1.24      cgd #include <sys/ioctl.h>
     57  1.24      cgd #include <sys/tty.h>
     58  1.22      cgd 
     59  1.33      cgd #include <sys/syscallargs.h>
     60  1.33      cgd 
     61  1.24      cgd /*
     62  1.24      cgd  * The routines implemented in this file are described in:
     63  1.24      cgd  *      Leffler, et al.: The Design and Implementation of the 4.3BSD
     64  1.24      cgd  *	    UNIX Operating System (Addison Welley, 1989)
     65  1.24      cgd  * on pages 62-63.
     66  1.24      cgd  *
     67  1.24      cgd  * Arguably, to simplify accounting operations, this mechanism should
     68  1.24      cgd  * be replaced by one in which an accounting log file (similar to /dev/klog)
     69  1.24      cgd  * is read by a user process, etc.  However, that has its own problems.
     70  1.24      cgd  */
     71  1.24      cgd 
     72  1.24      cgd /*
     73  1.24      cgd  * Internal accounting functions.
     74  1.24      cgd  * The former's operation is described in Leffler, et al., and the latter
     75  1.24      cgd  * was provided by UCB with the 4.4BSD-Lite release
     76  1.24      cgd  */
     77  1.24      cgd comp_t	encode_comp_t __P((u_long, u_long));
     78  1.24      cgd void	acctwatch __P((void *));
     79  1.24      cgd 
     80  1.24      cgd /*
     81  1.24      cgd  * Accounting vnode pointer, and saved vnode pointer.
     82  1.24      cgd  */
     83  1.24      cgd struct	vnode *acctp;
     84  1.24      cgd struct	vnode *savacctp;
     85  1.24      cgd 
     86  1.24      cgd /*
     87  1.24      cgd  * Values associated with enabling and disabling accounting
     88  1.24      cgd  */
     89  1.24      cgd int	acctsuspend = 2;	/* stop accounting when < 2% free space left */
     90  1.24      cgd int	acctresume = 4;		/* resume when free space risen to > 4% */
     91  1.24      cgd int	acctchkfreq = 15;	/* frequency (in seconds) to check space */
     92  1.24      cgd 
     93  1.24      cgd /*
     94  1.24      cgd  * Accounting system call.  Written based on the specification and
     95  1.24      cgd  * previous implementation done by Mark Tinguely.
     96  1.24      cgd  */
     97  1.38      cgd int
     98  1.24      cgd acct(p, uap, retval)
     99  1.24      cgd 	struct proc *p;
    100  1.33      cgd 	struct acct_args /* {
    101  1.33      cgd 		syscallarg(char *) path;
    102  1.33      cgd 	} */ *uap;
    103  1.33      cgd 	register_t *retval;
    104  1.22      cgd {
    105  1.24      cgd 	struct nameidata nd;
    106  1.24      cgd 	int error;
    107  1.24      cgd 
    108  1.24      cgd 	/* Make sure that the caller is root. */
    109  1.24      cgd 	if (error = suser(p->p_ucred, &p->p_acflag))
    110  1.24      cgd 		return (error);
    111  1.24      cgd 
    112  1.24      cgd 	/*
    113  1.24      cgd 	 * If accounting is to be started to a file, open that file for
    114  1.24      cgd 	 * writing and make sure it's a 'normal'.
    115  1.24      cgd 	 */
    116  1.33      cgd 	if (SCARG(uap, path) != NULL) {
    117  1.33      cgd 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path),
    118  1.33      cgd 		    p);
    119  1.37  mycroft 		if (error = vn_open(&nd, FWRITE, 0))
    120  1.24      cgd 			return (error);
    121  1.24      cgd 		VOP_UNLOCK(nd.ni_vp);
    122  1.24      cgd 		if (nd.ni_vp->v_type != VREG) {
    123  1.24      cgd 			vn_close(nd.ni_vp, FWRITE, p->p_ucred, p);
    124  1.24      cgd 			return (EACCES);
    125  1.24      cgd 		}
    126  1.24      cgd 	}
    127  1.24      cgd 
    128  1.24      cgd 	/*
    129  1.24      cgd 	 * If accounting was previously enabled, kill the old space-watcher,
    130  1.24      cgd 	 * close the file, and (if no new file was specified, leave).
    131  1.24      cgd 	 */
    132  1.32  mycroft 	if (acctp != NULLVP || savacctp != NULLVP) {
    133  1.24      cgd 		untimeout(acctwatch, NULL);
    134  1.32  mycroft 		error = vn_close((acctp != NULLVP ? acctp : savacctp), FWRITE,
    135  1.24      cgd 		    p->p_ucred, p);
    136  1.32  mycroft 		acctp = savacctp = NULLVP;
    137  1.24      cgd 	}
    138  1.33      cgd 	if (SCARG(uap, path) == NULL)
    139  1.25      cgd 		return (error);
    140  1.24      cgd 
    141  1.22      cgd 	/*
    142  1.24      cgd 	 * Save the new accounting file vnode, and schedule the new
    143  1.24      cgd 	 * free space watcher.
    144  1.22      cgd 	 */
    145  1.24      cgd 	acctp = nd.ni_vp;
    146  1.24      cgd 	acctwatch(NULL);
    147  1.24      cgd 	return (error);
    148  1.23      cgd }
    149  1.22      cgd 
    150  1.24      cgd /*
    151  1.24      cgd  * Write out process accounting information, on process exit.
    152  1.24      cgd  * Data to be written out is specified in Leffler, et al.
    153  1.24      cgd  * and are enumerated below.  (They're also noted in the system
    154  1.24      cgd  * "acct.h" header file.)
    155  1.24      cgd  */
    156  1.38      cgd int
    157  1.24      cgd acct_process(p)
    158  1.24      cgd 	struct proc *p;
    159  1.23      cgd {
    160  1.24      cgd 	struct acct acct;
    161  1.24      cgd 	struct rusage *r;
    162  1.24      cgd 	struct timeval ut, st, tmp;
    163  1.24      cgd 	int s, t;
    164  1.26  mycroft 	struct vnode *vp;
    165  1.24      cgd 
    166  1.24      cgd 	/* If accounting isn't enabled, don't bother */
    167  1.26  mycroft 	vp = acctp;
    168  1.32  mycroft 	if (vp == NULLVP)
    169  1.24      cgd 		return (0);
    170  1.24      cgd 
    171  1.24      cgd 	/*
    172  1.24      cgd 	 * Get process accounting information.
    173  1.24      cgd 	 */
    174  1.24      cgd 
    175  1.24      cgd 	/* (1) The name of the command that ran */
    176  1.24      cgd 	bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
    177  1.24      cgd 
    178  1.24      cgd 	/* (2) The amount of user and system time that was used */
    179  1.24      cgd 	calcru(p, &ut, &st, NULL);
    180  1.24      cgd 	acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
    181  1.24      cgd 	acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
    182  1.24      cgd 
    183  1.24      cgd 	/* (3) The elapsed time the commmand ran (and its starting time) */
    184  1.24      cgd 	acct.ac_btime = p->p_stats->p_start.tv_sec;
    185  1.24      cgd 	s = splclock();
    186  1.39  mycroft 	timersub(&time, &p->p_stats->p_start, &tmp);
    187  1.24      cgd 	splx(s);
    188  1.24      cgd 	acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
    189  1.24      cgd 
    190  1.24      cgd 	/* (4) The average amount of memory used */
    191  1.24      cgd 	r = &p->p_stats->p_ru;
    192  1.39  mycroft 	timeradd(&ut, &st, &tmp);
    193  1.24      cgd 	t = tmp.tv_sec * hz + tmp.tv_usec / tick;
    194  1.24      cgd 	if (t)
    195  1.24      cgd 		acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
    196  1.24      cgd 	else
    197  1.24      cgd 		acct.ac_mem = 0;
    198  1.24      cgd 
    199  1.29      cgd 	/* (5) The number of disk I/O operations done */
    200  1.24      cgd 	acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
    201  1.24      cgd 
    202  1.24      cgd 	/* (6) The UID and GID of the process */
    203  1.24      cgd 	acct.ac_uid = p->p_cred->p_ruid;
    204  1.24      cgd 	acct.ac_gid = p->p_cred->p_rgid;
    205  1.24      cgd 
    206  1.24      cgd 	/* (7) The terminal from which the process was started */
    207  1.24      cgd 	if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
    208  1.24      cgd 		acct.ac_tty = p->p_pgrp->pg_session->s_ttyp->t_dev;
    209  1.24      cgd 	else
    210  1.24      cgd 		acct.ac_tty = NODEV;
    211  1.24      cgd 
    212  1.24      cgd 	/* (8) The boolean flags that tell how the process terminated, etc. */
    213  1.24      cgd 	acct.ac_flag = p->p_acflag;
    214  1.22      cgd 
    215  1.22      cgd 	/*
    216  1.24      cgd 	 * Now, just write the accounting information to the file.
    217  1.22      cgd 	 */
    218  1.36  mycroft 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
    219  1.26  mycroft 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t)&acct, sizeof (acct),
    220  1.24      cgd 	    (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, p->p_ucred,
    221  1.24      cgd 	    (int *)0, p));
    222  1.22      cgd }
    223  1.22      cgd 
    224  1.22      cgd /*
    225  1.24      cgd  * Encode_comp_t converts from ticks in seconds and microseconds
    226  1.24      cgd  * to ticks in 1/AHZ seconds.  The encoding is described in
    227  1.24      cgd  * Leffler, et al., on page 63.
    228  1.22      cgd  */
    229  1.23      cgd 
    230  1.24      cgd #define	MANTSIZE	13			/* 13 bit mantissa. */
    231  1.24      cgd #define	EXPSIZE		3			/* Base 8 (3 bit) exponent. */
    232  1.24      cgd #define	MAXFRACT	((1 << MANTSIZE) - 1)	/* Maximum fractional value. */
    233  1.24      cgd 
    234  1.24      cgd comp_t
    235  1.24      cgd encode_comp_t(s, us)
    236  1.24      cgd 	u_long s, us;
    237  1.24      cgd {
    238  1.24      cgd 	int exp, rnd;
    239  1.24      cgd 
    240  1.24      cgd 	exp = 0;
    241  1.24      cgd 	rnd = 0;
    242  1.24      cgd 	s *= AHZ;
    243  1.24      cgd 	s += us / (1000000 / AHZ);	/* Maximize precision. */
    244  1.24      cgd 
    245  1.24      cgd 	while (s > MAXFRACT) {
    246  1.24      cgd 	rnd = s & (1 << (EXPSIZE - 1));	/* Round up? */
    247  1.24      cgd 		s >>= EXPSIZE;		/* Base 8 exponent == 3 bit shift. */
    248  1.24      cgd 		exp++;
    249  1.24      cgd 	}
    250  1.24      cgd 
    251  1.24      cgd 	/* If we need to round up, do it (and handle overflow correctly). */
    252  1.24      cgd 	if (rnd && (++s > MAXFRACT)) {
    253  1.24      cgd 		s >>= EXPSIZE;
    254  1.24      cgd 		exp++;
    255  1.24      cgd 	}
    256  1.24      cgd 
    257  1.24      cgd 	/* Clean it up and polish it off. */
    258  1.24      cgd 	exp <<= MANTSIZE;		/* Shift the exponent into place */
    259  1.24      cgd 	exp += s;			/* and add on the mantissa. */
    260  1.24      cgd 	return (exp);
    261  1.24      cgd }
    262  1.23      cgd 
    263  1.23      cgd /*
    264  1.24      cgd  * Periodically check the file system to see if accounting
    265  1.27      cgd  * should be turned on or off.  Beware the case where the vnode
    266  1.27      cgd  * has been vgone()'d out from underneath us, e.g. when the file
    267  1.27      cgd  * system containing the accounting file has been forcibly unmounted.
    268  1.23      cgd  */
    269  1.23      cgd /* ARGSUSED */
    270  1.22      cgd void
    271  1.23      cgd acctwatch(a)
    272  1.23      cgd 	void *a;
    273  1.22      cgd {
    274  1.22      cgd 	struct statfs sb;
    275  1.22      cgd 
    276  1.32  mycroft 	if (savacctp != NULLVP) {
    277  1.31      cgd 		if (savacctp->v_type == VBAD) {
    278  1.31      cgd 			(void) vn_close(savacctp, FWRITE, NOCRED, NULL);
    279  1.32  mycroft 			savacctp = NULLVP;
    280  1.31      cgd 			return;
    281  1.31      cgd 		}
    282  1.22      cgd 		(void)VFS_STATFS(savacctp->v_mount, &sb, (struct proc *)0);
    283  1.22      cgd 		if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
    284  1.22      cgd 			acctp = savacctp;
    285  1.32  mycroft 			savacctp = NULLVP;
    286  1.22      cgd 			log(LOG_NOTICE, "Accounting resumed\n");
    287  1.22      cgd 		}
    288  1.32  mycroft 	} else if (acctp != NULLVP) {
    289  1.31      cgd 		if (acctp->v_type == VBAD) {
    290  1.31      cgd 			(void) vn_close(acctp, FWRITE, NOCRED, NULL);
    291  1.32  mycroft 			acctp = NULLVP;
    292  1.31      cgd 			return;
    293  1.31      cgd 		}
    294  1.22      cgd 		(void)VFS_STATFS(acctp->v_mount, &sb, (struct proc *)0);
    295  1.22      cgd 		if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
    296  1.22      cgd 			savacctp = acctp;
    297  1.32  mycroft 			acctp = NULLVP;
    298  1.22      cgd 			log(LOG_NOTICE, "Accounting suspended\n");
    299  1.22      cgd 		}
    300  1.32  mycroft 	} else
    301  1.32  mycroft 		return;
    302  1.23      cgd 	timeout(acctwatch, NULL, acctchkfreq * hz);
    303  1.22      cgd }
    304