Home | History | Annotate | Line # | Download | only in kern
kern_acct.c revision 1.98
      1  1.98  dholland /*	$NetBSD: kern_acct.c,v 1.98 2021/06/29 22:40:53 dholland Exp $	*/
      2  1.30       cgd 
      3  1.23       cgd /*-
      4  1.23       cgd  * Copyright (c) 1982, 1986, 1989, 1993
      5  1.23       cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.22       cgd  * (c) UNIX System Laboratories, Inc.
      7  1.22       cgd  * All or some portions of this file are derived from material licensed
      8  1.22       cgd  * to the University of California by American Telephone and Telegraph
      9  1.22       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  1.22       cgd  * the permission of UNIX System Laboratories, Inc.
     11  1.22       cgd  *
     12  1.22       cgd  * Redistribution and use in source and binary forms, with or without
     13  1.22       cgd  * modification, are permitted provided that the following conditions
     14  1.22       cgd  * are met:
     15  1.22       cgd  * 1. Redistributions of source code must retain the above copyright
     16  1.22       cgd  *    notice, this list of conditions and the following disclaimer.
     17  1.22       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.22       cgd  *    notice, this list of conditions and the following disclaimer in the
     19  1.22       cgd  *    documentation and/or other materials provided with the distribution.
     20  1.55       agc  * 3. Neither the name of the University nor the names of its contributors
     21  1.55       agc  *    may be used to endorse or promote products derived from this software
     22  1.55       agc  *    without specific prior written permission.
     23  1.55       agc  *
     24  1.55       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.55       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.55       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.55       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.55       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.55       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.55       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.55       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.55       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.55       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.55       agc  * SUCH DAMAGE.
     35  1.55       agc  *
     36  1.55       agc  *	@(#)kern_acct.c	8.8 (Berkeley) 5/14/95
     37  1.55       agc  */
     38  1.55       agc 
     39  1.55       agc /*-
     40  1.55       agc  * Copyright (c) 1994 Christopher G. Demetriou
     41  1.55       agc  *
     42  1.55       agc  * Redistribution and use in source and binary forms, with or without
     43  1.55       agc  * modification, are permitted provided that the following conditions
     44  1.55       agc  * are met:
     45  1.55       agc  * 1. Redistributions of source code must retain the above copyright
     46  1.55       agc  *    notice, this list of conditions and the following disclaimer.
     47  1.55       agc  * 2. Redistributions in binary form must reproduce the above copyright
     48  1.55       agc  *    notice, this list of conditions and the following disclaimer in the
     49  1.55       agc  *    documentation and/or other materials provided with the distribution.
     50  1.22       cgd  * 3. All advertising materials mentioning features or use of this software
     51  1.22       cgd  *    must display the following acknowledgement:
     52  1.22       cgd  *	This product includes software developed by the University of
     53  1.22       cgd  *	California, Berkeley and its contributors.
     54  1.22       cgd  * 4. Neither the name of the University nor the names of its contributors
     55  1.22       cgd  *    may be used to endorse or promote products derived from this software
     56  1.22       cgd  *    without specific prior written permission.
     57  1.22       cgd  *
     58  1.22       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  1.22       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  1.22       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  1.22       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  1.22       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  1.22       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  1.22       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  1.22       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  1.22       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  1.22       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  1.22       cgd  * SUCH DAMAGE.
     69  1.22       cgd  *
     70  1.44      fvdl  *	@(#)kern_acct.c	8.8 (Berkeley) 5/14/95
     71  1.22       cgd  */
     72  1.50     lukem 
     73  1.50     lukem #include <sys/cdefs.h>
     74  1.98  dholland __KERNEL_RCSID(0, "$NetBSD: kern_acct.c,v 1.98 2021/06/29 22:40:53 dholland Exp $");
     75  1.22       cgd 
     76  1.22       cgd #include <sys/param.h>
     77  1.33       cgd #include <sys/systm.h>
     78  1.22       cgd #include <sys/proc.h>
     79  1.23       cgd #include <sys/mount.h>
     80  1.22       cgd #include <sys/vnode.h>
     81  1.22       cgd #include <sys/file.h>
     82  1.22       cgd #include <sys/syslog.h>
     83  1.23       cgd #include <sys/kernel.h>
     84  1.48     enami #include <sys/kthread.h>
     85  1.75        ad #include <sys/kmem.h>
     86  1.24       cgd #include <sys/namei.h>
     87  1.24       cgd #include <sys/errno.h>
     88  1.24       cgd #include <sys/acct.h>
     89  1.24       cgd #include <sys/resourcevar.h>
     90  1.24       cgd #include <sys/ioctl.h>
     91  1.24       cgd #include <sys/tty.h>
     92  1.63      elad #include <sys/kauth.h>
     93  1.22       cgd 
     94  1.33       cgd #include <sys/syscallargs.h>
     95  1.33       cgd 
     96  1.24       cgd /*
     97  1.24       cgd  * The routines implemented in this file are described in:
     98  1.24       cgd  *      Leffler, et al.: The Design and Implementation of the 4.3BSD
     99  1.24       cgd  *	    UNIX Operating System (Addison Welley, 1989)
    100  1.24       cgd  * on pages 62-63.
    101  1.24       cgd  *
    102  1.24       cgd  * Arguably, to simplify accounting operations, this mechanism should
    103  1.24       cgd  * be replaced by one in which an accounting log file (similar to /dev/klog)
    104  1.24       cgd  * is read by a user process, etc.  However, that has its own problems.
    105  1.24       cgd  */
    106  1.24       cgd 
    107  1.24       cgd /*
    108  1.81        ad  * Lock to serialize system calls and kernel threads.
    109  1.48     enami  */
    110  1.81        ad krwlock_t	acct_lock;
    111  1.48     enami 
    112  1.48     enami /*
    113  1.70        ad  * The global accounting state and related data.  Gain the mutex before
    114  1.61   thorpej  * accessing these variables.
    115  1.24       cgd  */
    116  1.61   thorpej static enum {
    117  1.61   thorpej 	ACCT_STOP,
    118  1.61   thorpej 	ACCT_ACTIVE,
    119  1.61   thorpej 	ACCT_SUSPENDED
    120  1.61   thorpej } acct_state;				/* The current accounting state. */
    121  1.61   thorpej static struct vnode *acct_vp;		/* Accounting vnode pointer. */
    122  1.63      elad static kauth_cred_t acct_cred;		/* Credential of accounting file
    123  1.61   thorpej 					   owner (i.e root).  Used when
    124  1.61   thorpej  					   accounting file i/o.  */
    125  1.75        ad static struct lwp *acct_dkwatcher;	/* Free disk space checker. */
    126  1.24       cgd 
    127  1.24       cgd /*
    128  1.24       cgd  * Values associated with enabling and disabling accounting
    129  1.24       cgd  */
    130  1.24       cgd int	acctsuspend = 2;	/* stop accounting when < 2% free space left */
    131  1.24       cgd int	acctresume = 4;		/* resume when free space risen to > 4% */
    132  1.24       cgd int	acctchkfreq = 15;	/* frequency (in seconds) to check space */
    133  1.24       cgd 
    134  1.61   thorpej /*
    135  1.61   thorpej  * Encode_comp_t converts from ticks in seconds and microseconds
    136  1.61   thorpej  * to ticks in 1/AHZ seconds.  The encoding is described in
    137  1.61   thorpej  * Leffler, et al., on page 63.
    138  1.61   thorpej  */
    139  1.48     enami 
    140  1.61   thorpej #define	MANTSIZE	13			/* 13 bit mantissa. */
    141  1.61   thorpej #define	EXPSIZE		3			/* Base 8 (3 bit) exponent. */
    142  1.61   thorpej #define	MAXFRACT	((1 << MANTSIZE) - 1)	/* Maximum fractional value. */
    143  1.48     enami 
    144  1.61   thorpej static comp_t
    145  1.61   thorpej encode_comp_t(u_long s, u_long us)
    146  1.48     enami {
    147  1.61   thorpej 	int exp, rnd;
    148  1.61   thorpej 
    149  1.61   thorpej 	exp = 0;
    150  1.61   thorpej 	rnd = 0;
    151  1.61   thorpej 	s *= AHZ;
    152  1.61   thorpej 	s += us / (1000000 / AHZ);	/* Maximize precision. */
    153  1.48     enami 
    154  1.61   thorpej 	while (s > MAXFRACT) {
    155  1.96  dholland 		rnd = s & (1 << (EXPSIZE - 1));	/* Round up? */
    156  1.61   thorpej 		s >>= EXPSIZE;		/* Base 8 exponent == 3 bit shift. */
    157  1.61   thorpej 		exp++;
    158  1.48     enami 	}
    159  1.61   thorpej 
    160  1.61   thorpej 	/* If we need to round up, do it (and handle overflow correctly). */
    161  1.61   thorpej 	if (rnd && (++s > MAXFRACT)) {
    162  1.61   thorpej 		s >>= EXPSIZE;
    163  1.61   thorpej 		exp++;
    164  1.48     enami 	}
    165  1.61   thorpej 
    166  1.61   thorpej 	/* Clean it up and polish it off. */
    167  1.61   thorpej 	exp <<= MANTSIZE;		/* Shift the exponent into place */
    168  1.61   thorpej 	exp += s;			/* and add on the mantissa. */
    169  1.61   thorpej 	return (exp);
    170  1.48     enami }
    171  1.48     enami 
    172  1.61   thorpej static int
    173  1.61   thorpej acct_chkfree(void)
    174  1.48     enami {
    175  1.48     enami 	int error;
    176  1.65  christos 	struct statvfs *sb;
    177  1.91  dholland 	fsblkcnt_t bavail;
    178  1.48     enami 
    179  1.75        ad 	sb = kmem_alloc(sizeof(*sb), KM_SLEEP);
    180  1.78     pooka 	error = VFS_STATVFS(acct_vp->v_mount, sb);
    181  1.75        ad 	if (error != 0) {
    182  1.75        ad 		kmem_free(sb, sizeof(*sb));
    183  1.48     enami 		return (error);
    184  1.75        ad 	}
    185  1.48     enami 
    186  1.91  dholland 	if (sb->f_bfree < sb->f_bresvd) {
    187  1.91  dholland 		bavail = 0;
    188  1.91  dholland 	} else {
    189  1.91  dholland 		bavail = sb->f_bfree - sb->f_bresvd;
    190  1.91  dholland 	}
    191  1.57  christos 
    192  1.48     enami 	switch (acct_state) {
    193  1.48     enami 	case ACCT_SUSPENDED:
    194  1.65  christos 		if (bavail > acctresume * sb->f_blocks / 100) {
    195  1.48     enami 			acct_state = ACCT_ACTIVE;
    196  1.48     enami 			log(LOG_NOTICE, "Accounting resumed\n");
    197  1.48     enami 		}
    198  1.48     enami 		break;
    199  1.48     enami 	case ACCT_ACTIVE:
    200  1.65  christos 		if (bavail <= acctsuspend * sb->f_blocks / 100) {
    201  1.48     enami 			acct_state = ACCT_SUSPENDED;
    202  1.48     enami 			log(LOG_NOTICE, "Accounting suspended\n");
    203  1.48     enami 		}
    204  1.48     enami 		break;
    205  1.48     enami 	case ACCT_STOP:
    206  1.48     enami 		break;
    207  1.48     enami 	}
    208  1.75        ad 	kmem_free(sb, sizeof(*sb));
    209  1.48     enami 	return (0);
    210  1.48     enami }
    211  1.48     enami 
    212  1.61   thorpej static void
    213  1.61   thorpej acct_stop(void)
    214  1.61   thorpej {
    215  1.61   thorpej 	int error;
    216  1.61   thorpej 
    217  1.81        ad 	KASSERT(rw_write_held(&acct_lock));
    218  1.81        ad 
    219  1.61   thorpej 	if (acct_vp != NULLVP && acct_vp->v_type != VBAD) {
    220  1.84        ad 		error = vn_close(acct_vp, FWRITE, acct_cred);
    221  1.61   thorpej #ifdef DIAGNOSTIC
    222  1.61   thorpej 		if (error != 0)
    223  1.61   thorpej 			printf("acct_stop: failed to close, errno = %d\n",
    224  1.61   thorpej 			    error);
    225  1.94       mrg #else
    226  1.94       mrg 		__USE(error);
    227  1.61   thorpej #endif
    228  1.61   thorpej 		acct_vp = NULLVP;
    229  1.61   thorpej 	}
    230  1.63      elad 	if (acct_cred != NULL) {
    231  1.63      elad 		kauth_cred_free(acct_cred);
    232  1.63      elad 		acct_cred = NULL;
    233  1.61   thorpej 	}
    234  1.61   thorpej 	acct_state = ACCT_STOP;
    235  1.61   thorpej }
    236  1.61   thorpej 
    237  1.61   thorpej /*
    238  1.61   thorpej  * Periodically check the file system to see if accounting
    239  1.61   thorpej  * should be turned on or off.  Beware the case where the vnode
    240  1.61   thorpej  * has been vgone()'d out from underneath us, e.g. when the file
    241  1.61   thorpej  * system containing the accounting file has been forcibly unmounted.
    242  1.61   thorpej  */
    243  1.61   thorpej static void
    244  1.69      yamt acctwatch(void *arg)
    245  1.61   thorpej {
    246  1.61   thorpej 	int error;
    247  1.61   thorpej 
    248  1.61   thorpej 	log(LOG_NOTICE, "Accounting started\n");
    249  1.81        ad 	rw_enter(&acct_lock, RW_WRITER);
    250  1.61   thorpej 	while (acct_state != ACCT_STOP) {
    251  1.61   thorpej 		if (acct_vp->v_type == VBAD) {
    252  1.61   thorpej 			log(LOG_NOTICE, "Accounting terminated\n");
    253  1.61   thorpej 			acct_stop();
    254  1.61   thorpej 			continue;
    255  1.61   thorpej 		}
    256  1.61   thorpej 
    257  1.61   thorpej 		error = acct_chkfree();
    258  1.61   thorpej #ifdef DIAGNOSTIC
    259  1.61   thorpej 		if (error != 0)
    260  1.61   thorpej 			printf("acctwatch: failed to statvfs, error = %d\n",
    261  1.61   thorpej 			    error);
    262  1.94       mrg #else
    263  1.94       mrg 		__USE(error);
    264  1.61   thorpej #endif
    265  1.81        ad 		rw_exit(&acct_lock);
    266  1.81        ad 		error = kpause("actwat", false, acctchkfreq * hz, NULL);
    267  1.81        ad 		rw_enter(&acct_lock, RW_WRITER);
    268  1.61   thorpej #ifdef DIAGNOSTIC
    269  1.61   thorpej 		if (error != 0 && error != EWOULDBLOCK)
    270  1.61   thorpej 			printf("acctwatch: sleep error %d\n", error);
    271  1.61   thorpej #endif
    272  1.61   thorpej 	}
    273  1.61   thorpej 	acct_dkwatcher = NULL;
    274  1.81        ad 	rw_exit(&acct_lock);
    275  1.61   thorpej 
    276  1.61   thorpej 	kthread_exit(0);
    277  1.61   thorpej }
    278  1.61   thorpej 
    279  1.61   thorpej void
    280  1.61   thorpej acct_init(void)
    281  1.61   thorpej {
    282  1.61   thorpej 
    283  1.61   thorpej 	acct_state = ACCT_STOP;
    284  1.61   thorpej 	acct_vp = NULLVP;
    285  1.63      elad 	acct_cred = NULL;
    286  1.81        ad 	rw_init(&acct_lock);
    287  1.61   thorpej }
    288  1.61   thorpej 
    289  1.24       cgd /*
    290  1.24       cgd  * Accounting system call.  Written based on the specification and
    291  1.24       cgd  * previous implementation done by Mark Tinguely.
    292  1.24       cgd  */
    293  1.38       cgd int
    294  1.80       dsl sys_acct(struct lwp *l, const struct sys_acct_args *uap, register_t *retval)
    295  1.40   thorpej {
    296  1.80       dsl 	/* {
    297  1.43   mycroft 		syscallarg(const char *) path;
    298  1.80       dsl 	} */
    299  1.90  dholland 	struct pathbuf *pb;
    300  1.98  dholland 	struct vnode *vp;
    301  1.24       cgd 	int error;
    302  1.24       cgd 
    303  1.24       cgd 	/* Make sure that the caller is root. */
    304  1.67      elad 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_ACCOUNTING,
    305  1.67      elad 	    0, NULL, NULL, NULL)))
    306  1.24       cgd 		return (error);
    307  1.24       cgd 
    308  1.24       cgd 	/*
    309  1.24       cgd 	 * If accounting is to be started to a file, open that file for
    310  1.24       cgd 	 * writing and make sure it's a 'normal'.
    311  1.24       cgd 	 */
    312  1.33       cgd 	if (SCARG(uap, path) != NULL) {
    313  1.59  christos 		struct vattr va;
    314  1.59  christos 		size_t pad;
    315  1.90  dholland 
    316  1.90  dholland 		error = pathbuf_copyin(SCARG(uap, path), &pb);
    317  1.90  dholland 		if (error) {
    318  1.90  dholland 			return error;
    319  1.90  dholland 		}
    320  1.98  dholland 		error = vn_open(NULL, pb, TRYEMULROOT, FWRITE|O_APPEND, 0,
    321  1.98  dholland 		    &vp, NULL, NULL);
    322  1.98  dholland 		if (error != 0) {
    323  1.90  dholland 			pathbuf_destroy(pb);
    324  1.90  dholland 			return error;
    325  1.90  dholland 		}
    326  1.98  dholland 		if (vp->v_type != VREG) {
    327  1.98  dholland 			VOP_UNLOCK(vp);
    328  1.59  christos 			error = EACCES;
    329  1.59  christos 			goto bad;
    330  1.59  christos 		}
    331  1.98  dholland 		if ((error = VOP_GETATTR(vp, &va, l->l_cred)) != 0) {
    332  1.98  dholland 			VOP_UNLOCK(vp);
    333  1.59  christos 			goto bad;
    334  1.60      yamt 		}
    335  1.59  christos 
    336  1.59  christos 		if ((pad = (va.va_size % sizeof(struct acct))) != 0) {
    337  1.60      yamt 			u_quad_t size = va.va_size - pad;
    338  1.59  christos #ifdef DIAGNOSTIC
    339  1.59  christos 			printf("Size of accounting file not a multiple of "
    340  1.59  christos 			    "%lu - incomplete record truncated\n",
    341  1.59  christos 			    (unsigned long)sizeof(struct acct));
    342  1.59  christos #endif
    343  1.88     pooka 			vattr_null(&va);
    344  1.60      yamt 			va.va_size = size;
    345  1.98  dholland 			error = VOP_SETATTR(vp, &va, l->l_cred);
    346  1.60      yamt 			if (error != 0) {
    347  1.98  dholland 				VOP_UNLOCK(vp);
    348  1.59  christos 				goto bad;
    349  1.60      yamt 			}
    350  1.24       cgd 		}
    351  1.98  dholland 		VOP_UNLOCK(vp);
    352  1.24       cgd 	}
    353  1.24       cgd 
    354  1.81        ad 	rw_enter(&acct_lock, RW_WRITER);
    355  1.48     enami 
    356  1.24       cgd 	/*
    357  1.24       cgd 	 * If accounting was previously enabled, kill the old space-watcher,
    358  1.48     enami 	 * free credential for accounting file i/o,
    359  1.48     enami 	 * ... (and, if no new file was specified, leave).
    360  1.24       cgd 	 */
    361  1.48     enami 	acct_stop();
    362  1.33       cgd 	if (SCARG(uap, path) == NULL)
    363  1.48     enami 		goto out;
    364  1.24       cgd 
    365  1.22       cgd 	/*
    366  1.48     enami 	 * Save the new accounting file vnode and credential,
    367  1.48     enami 	 * and schedule the new free space watcher.
    368  1.22       cgd 	 */
    369  1.48     enami 	acct_state = ACCT_ACTIVE;
    370  1.98  dholland 	acct_vp = vp;
    371  1.66        ad 	acct_cred = l->l_cred;
    372  1.63      elad 	kauth_cred_hold(acct_cred);
    373  1.48     enami 
    374  1.90  dholland 	pathbuf_destroy(pb);
    375  1.90  dholland 
    376  1.48     enami 	error = acct_chkfree();		/* Initial guess. */
    377  1.48     enami 	if (error != 0) {
    378  1.48     enami 		acct_stop();
    379  1.48     enami 		goto out;
    380  1.48     enami 	}
    381  1.48     enami 
    382  1.48     enami 	if (acct_dkwatcher == NULL) {
    383  1.82        ad 		error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    384  1.82        ad 		    acctwatch, NULL, &acct_dkwatcher, "acctwatch");
    385  1.48     enami 		if (error != 0)
    386  1.48     enami 			acct_stop();
    387  1.48     enami 	}
    388  1.48     enami 
    389  1.48     enami  out:
    390  1.81        ad 	rw_exit(&acct_lock);
    391  1.24       cgd 	return (error);
    392  1.59  christos  bad:
    393  1.98  dholland 	vn_close(vp, FWRITE, l->l_cred);
    394  1.90  dholland 	pathbuf_destroy(pb);
    395  1.59  christos 	return error;
    396  1.23       cgd }
    397  1.22       cgd 
    398  1.24       cgd /*
    399  1.24       cgd  * Write out process accounting information, on process exit.
    400  1.24       cgd  * Data to be written out is specified in Leffler, et al.
    401  1.24       cgd  * and are enumerated below.  (They're also noted in the system
    402  1.24       cgd  * "acct.h" header file.)
    403  1.24       cgd  */
    404  1.38       cgd int
    405  1.62  christos acct_process(struct lwp *l)
    406  1.23       cgd {
    407  1.24       cgd 	struct acct acct;
    408  1.64    kardel 	struct timeval ut, st, tmp;
    409  1.24       cgd 	struct rusage *r;
    410  1.64    kardel 	int t, error = 0;
    411  1.77       dsl 	struct rlimit orlim;
    412  1.62  christos 	struct proc *p = l->l_proc;
    413  1.48     enami 
    414  1.77       dsl 	if (acct_state != ACCT_ACTIVE)
    415  1.77       dsl 		return 0;
    416  1.77       dsl 
    417  1.93  christos 	memset(&acct, 0, sizeof(acct));	/* to zerofill padded data */
    418  1.93  christos 
    419  1.81        ad 	rw_enter(&acct_lock, RW_READER);
    420  1.24       cgd 
    421  1.24       cgd 	/* If accounting isn't enabled, don't bother */
    422  1.48     enami 	if (acct_state != ACCT_ACTIVE)
    423  1.48     enami 		goto out;
    424  1.24       cgd 
    425  1.24       cgd 	/*
    426  1.77       dsl 	 * Temporarily raise the file limit so that accounting can't
    427  1.77       dsl 	 * be stopped by the user.
    428  1.49   thorpej 	 *
    429  1.49   thorpej 	 * XXX We should think about the CPU limit, too.
    430  1.49   thorpej 	 */
    431  1.92     rmind 	lim_privatise(p);
    432  1.77       dsl 	orlim = p->p_rlimit[RLIMIT_FSIZE];
    433  1.77       dsl 	/* Set current and max to avoid illegal values */
    434  1.49   thorpej 	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    435  1.77       dsl 	p->p_rlimit[RLIMIT_FSIZE].rlim_max = RLIM_INFINITY;
    436  1.49   thorpej 
    437  1.49   thorpej 	/*
    438  1.24       cgd 	 * Get process accounting information.
    439  1.24       cgd 	 */
    440  1.24       cgd 
    441  1.24       cgd 	/* (1) The name of the command that ran */
    442  1.93  christos 	strncpy(acct.ac_comm, p->p_comm, sizeof(acct.ac_comm));
    443  1.24       cgd 
    444  1.24       cgd 	/* (2) The amount of user and system time that was used */
    445  1.86        ad 	mutex_enter(p->p_lock);
    446  1.70        ad 	calcru(p, &ut, &st, NULL, NULL);
    447  1.86        ad 	mutex_exit(p->p_lock);
    448  1.24       cgd 	acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
    449  1.24       cgd 	acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
    450  1.24       cgd 
    451  1.24       cgd 	/* (3) The elapsed time the commmand ran (and its starting time) */
    452  1.24       cgd 	acct.ac_btime = p->p_stats->p_start.tv_sec;
    453  1.64    kardel 	getmicrotime(&tmp);
    454  1.64    kardel 	timersub(&tmp, &p->p_stats->p_start, &tmp);
    455  1.24       cgd 	acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
    456  1.24       cgd 
    457  1.24       cgd 	/* (4) The average amount of memory used */
    458  1.24       cgd 	r = &p->p_stats->p_ru;
    459  1.39   mycroft 	timeradd(&ut, &st, &tmp);
    460  1.24       cgd 	t = tmp.tv_sec * hz + tmp.tv_usec / tick;
    461  1.24       cgd 	if (t)
    462  1.24       cgd 		acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
    463  1.24       cgd 	else
    464  1.24       cgd 		acct.ac_mem = 0;
    465  1.24       cgd 
    466  1.29       cgd 	/* (5) The number of disk I/O operations done */
    467  1.24       cgd 	acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
    468  1.24       cgd 
    469  1.24       cgd 	/* (6) The UID and GID of the process */
    470  1.66        ad 	acct.ac_uid = kauth_cred_getuid(l->l_cred);
    471  1.66        ad 	acct.ac_gid = kauth_cred_getgid(l->l_cred);
    472  1.24       cgd 
    473  1.24       cgd 	/* (7) The terminal from which the process was started */
    474  1.97        ad 	mutex_enter(&proc_lock);
    475  1.70        ad 	if ((p->p_lflag & PL_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
    476  1.24       cgd 		acct.ac_tty = p->p_pgrp->pg_session->s_ttyp->t_dev;
    477  1.24       cgd 	else
    478  1.24       cgd 		acct.ac_tty = NODEV;
    479  1.97        ad 	mutex_exit(&proc_lock);
    480  1.24       cgd 
    481  1.24       cgd 	/* (8) The boolean flags that tell how the process terminated, etc. */
    482  1.24       cgd 	acct.ac_flag = p->p_acflag;
    483  1.22       cgd 
    484  1.22       cgd 	/*
    485  1.24       cgd 	 * Now, just write the accounting information to the file.
    486  1.22       cgd 	 */
    487  1.72  christos 	error = vn_rdwr(UIO_WRITE, acct_vp, (void *)&acct,
    488  1.48     enami 	    sizeof(acct), (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT,
    489  1.63      elad 	    acct_cred, NULL, NULL);
    490  1.48     enami 	if (error != 0)
    491  1.48     enami 		log(LOG_ERR, "Accounting: write failed %d\n", error);
    492  1.49   thorpej 
    493  1.77       dsl 	/* Restore limit - rather pointless since process is about to exit */
    494  1.77       dsl 	p->p_rlimit[RLIMIT_FSIZE] = orlim;
    495  1.48     enami 
    496  1.48     enami  out:
    497  1.81        ad 	rw_exit(&acct_lock);
    498  1.48     enami 	return (error);
    499  1.22       cgd }
    500