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