Home | History | Annotate | Line # | Download | only in kern
kern_acct.c revision 1.54.2.2
      1  1.54.2.2     skrll /*	$NetBSD: kern_acct.c,v 1.54.2.2 2004/08/03 10:52:43 skrll 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.54.2.2     skrll  * 3. Neither the name of the University nor the names of its contributors
     21  1.54.2.2     skrll  *    may be used to endorse or promote products derived from this software
     22  1.54.2.2     skrll  *    without specific prior written permission.
     23  1.54.2.2     skrll  *
     24  1.54.2.2     skrll  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.54.2.2     skrll  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.54.2.2     skrll  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.54.2.2     skrll  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.54.2.2     skrll  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.54.2.2     skrll  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.54.2.2     skrll  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.54.2.2     skrll  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.54.2.2     skrll  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.54.2.2     skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.54.2.2     skrll  * SUCH DAMAGE.
     35  1.54.2.2     skrll  *
     36  1.54.2.2     skrll  *	@(#)kern_acct.c	8.8 (Berkeley) 5/14/95
     37  1.54.2.2     skrll  */
     38  1.54.2.2     skrll 
     39  1.54.2.2     skrll /*-
     40  1.54.2.2     skrll  * Copyright (c) 1994 Christopher G. Demetriou
     41  1.54.2.2     skrll  *
     42  1.54.2.2     skrll  * Redistribution and use in source and binary forms, with or without
     43  1.54.2.2     skrll  * modification, are permitted provided that the following conditions
     44  1.54.2.2     skrll  * are met:
     45  1.54.2.2     skrll  * 1. Redistributions of source code must retain the above copyright
     46  1.54.2.2     skrll  *    notice, this list of conditions and the following disclaimer.
     47  1.54.2.2     skrll  * 2. Redistributions in binary form must reproduce the above copyright
     48  1.54.2.2     skrll  *    notice, this list of conditions and the following disclaimer in the
     49  1.54.2.2     skrll  *    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.54.2.2     skrll __KERNEL_RCSID(0, "$NetBSD: kern_acct.c,v 1.54.2.2 2004/08/03 10:52:43 skrll 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.48     enami #include <sys/lock.h>
     86      1.48     enami #include <sys/malloc.h>
     87      1.24       cgd #include <sys/namei.h>
     88      1.24       cgd #include <sys/errno.h>
     89      1.24       cgd #include <sys/acct.h>
     90      1.24       cgd #include <sys/resourcevar.h>
     91      1.24       cgd #include <sys/ioctl.h>
     92      1.24       cgd #include <sys/tty.h>
     93      1.22       cgd 
     94      1.51   thorpej #include <sys/sa.h>
     95      1.33       cgd #include <sys/syscallargs.h>
     96      1.33       cgd 
     97      1.24       cgd /*
     98      1.24       cgd  * The routines implemented in this file are described in:
     99      1.24       cgd  *      Leffler, et al.: The Design and Implementation of the 4.3BSD
    100      1.24       cgd  *	    UNIX Operating System (Addison Welley, 1989)
    101      1.24       cgd  * on pages 62-63.
    102      1.24       cgd  *
    103      1.24       cgd  * Arguably, to simplify accounting operations, this mechanism should
    104      1.24       cgd  * be replaced by one in which an accounting log file (similar to /dev/klog)
    105      1.24       cgd  * is read by a user process, etc.  However, that has its own problems.
    106      1.24       cgd  */
    107      1.24       cgd 
    108      1.24       cgd /*
    109      1.48     enami  * The global accounting state and related data.  Gain the lock before
    110      1.48     enami  * accessing these variables.
    111      1.48     enami  */
    112      1.48     enami enum {
    113      1.48     enami 	ACCT_STOP,
    114      1.48     enami 	ACCT_ACTIVE,
    115      1.48     enami 	ACCT_SUSPENDED
    116      1.48     enami } acct_state;				/* The current accounting state. */
    117      1.48     enami struct vnode *acct_vp;			/* Accounting vnode pointer. */
    118      1.48     enami struct ucred *acct_ucred;		/* Credential of accounting file
    119      1.48     enami 					   owner (i.e root).  Used when
    120      1.48     enami  					   accounting file i/o.  */
    121      1.48     enami struct proc *acct_dkwatcher;		/* Free disk space checker. */
    122      1.48     enami 
    123      1.48     enami /*
    124      1.48     enami  * Lock to serialize system calls and kernel threads.
    125      1.48     enami  */
    126      1.48     enami struct	lock acct_lock;
    127      1.48     enami #define	ACCT_LOCK()						\
    128      1.48     enami do {								\
    129      1.48     enami 	(void) lockmgr(&acct_lock, LK_EXCLUSIVE, NULL);		\
    130      1.48     enami } while (/* CONSTCOND */0)
    131      1.48     enami #define	ACCT_UNLOCK()						\
    132      1.48     enami do {								\
    133      1.48     enami 	(void) lockmgr(&acct_lock, LK_RELEASE, NULL);		\
    134      1.48     enami } while (/* CONSTCOND */0)
    135      1.48     enami 
    136      1.48     enami /*
    137      1.24       cgd  * Internal accounting functions.
    138      1.24       cgd  * The former's operation is described in Leffler, et al., and the latter
    139      1.24       cgd  * was provided by UCB with the 4.4BSD-Lite release
    140      1.24       cgd  */
    141  1.54.2.2     skrll comp_t	encode_comp_t(u_long, u_long);
    142  1.54.2.2     skrll void	acctwatch(void *);
    143  1.54.2.2     skrll void	acct_stop(void);
    144  1.54.2.2     skrll int	acct_chkfree(void);
    145      1.24       cgd 
    146      1.24       cgd /*
    147      1.24       cgd  * Values associated with enabling and disabling accounting
    148      1.24       cgd  */
    149      1.24       cgd int	acctsuspend = 2;	/* stop accounting when < 2% free space left */
    150      1.24       cgd int	acctresume = 4;		/* resume when free space risen to > 4% */
    151      1.24       cgd int	acctchkfreq = 15;	/* frequency (in seconds) to check space */
    152      1.24       cgd 
    153      1.48     enami void
    154      1.48     enami acct_init()
    155      1.48     enami {
    156      1.48     enami 
    157      1.48     enami 	acct_state = ACCT_STOP;
    158      1.48     enami 	acct_vp = NULLVP;
    159      1.48     enami 	acct_ucred = NULL;
    160      1.48     enami 	lockinit(&acct_lock, PWAIT, "acctlk", 0, 0);
    161      1.48     enami }
    162      1.48     enami 
    163      1.48     enami void
    164      1.48     enami acct_stop()
    165      1.48     enami {
    166      1.48     enami 	int error;
    167      1.48     enami 
    168      1.48     enami 	if (acct_vp != NULLVP && acct_vp->v_type != VBAD) {
    169      1.48     enami 		error = vn_close(acct_vp, FWRITE, acct_ucred, NULL);
    170      1.48     enami #ifdef DIAGNOSTIC
    171      1.48     enami 		if (error != 0)
    172      1.48     enami 			printf("acct_stop: failed to close, errno = %d\n",
    173      1.48     enami 			    error);
    174      1.48     enami #endif
    175      1.48     enami 		acct_vp = NULLVP;
    176      1.48     enami 	}
    177      1.48     enami 	if (acct_ucred != NULL) {
    178      1.48     enami 		crfree(acct_ucred);
    179      1.48     enami 		acct_ucred = NULL;
    180      1.48     enami 	}
    181      1.48     enami 	acct_state = ACCT_STOP;
    182      1.48     enami }
    183      1.48     enami 
    184      1.48     enami int
    185      1.48     enami acct_chkfree()
    186      1.48     enami {
    187      1.48     enami 	int error;
    188  1.54.2.2     skrll 	struct statvfs sb;
    189  1.54.2.2     skrll 	int64_t bavail;
    190      1.48     enami 
    191  1.54.2.2     skrll 	error = VFS_STATVFS(acct_vp->v_mount, &sb, NULL);
    192      1.48     enami 	if (error != 0)
    193      1.48     enami 		return (error);
    194      1.48     enami 
    195  1.54.2.2     skrll 	bavail = sb.f_bfree - sb.f_bresvd;
    196  1.54.2.2     skrll 
    197      1.48     enami 	switch (acct_state) {
    198      1.48     enami 	case ACCT_SUSPENDED:
    199  1.54.2.2     skrll 		if (bavail > acctresume * sb.f_blocks / 100) {
    200      1.48     enami 			acct_state = ACCT_ACTIVE;
    201      1.48     enami 			log(LOG_NOTICE, "Accounting resumed\n");
    202      1.48     enami 		}
    203      1.48     enami 		break;
    204      1.48     enami 	case ACCT_ACTIVE:
    205  1.54.2.2     skrll 		if (bavail <= acctsuspend * sb.f_blocks / 100) {
    206      1.48     enami 			acct_state = ACCT_SUSPENDED;
    207      1.48     enami 			log(LOG_NOTICE, "Accounting suspended\n");
    208      1.48     enami 		}
    209      1.48     enami 		break;
    210      1.48     enami 	case ACCT_STOP:
    211      1.48     enami 		break;
    212      1.48     enami 	}
    213      1.48     enami 	return (0);
    214      1.48     enami }
    215      1.48     enami 
    216      1.24       cgd /*
    217      1.24       cgd  * Accounting system call.  Written based on the specification and
    218      1.24       cgd  * previous implementation done by Mark Tinguely.
    219      1.24       cgd  */
    220      1.38       cgd int
    221      1.51   thorpej sys_acct(l, v, retval)
    222      1.51   thorpej 	struct lwp *l;
    223      1.40   thorpej 	void *v;
    224      1.40   thorpej 	register_t *retval;
    225      1.40   thorpej {
    226      1.41   mycroft 	struct sys_acct_args /* {
    227      1.43   mycroft 		syscallarg(const char *) path;
    228      1.40   thorpej 	} */ *uap = v;
    229      1.24       cgd 	struct nameidata nd;
    230      1.24       cgd 	int error;
    231      1.51   thorpej 	struct proc *p = l->l_proc;
    232      1.24       cgd 
    233      1.24       cgd 	/* Make sure that the caller is root. */
    234      1.42  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    235      1.24       cgd 		return (error);
    236      1.24       cgd 
    237      1.24       cgd 	/*
    238      1.24       cgd 	 * If accounting is to be started to a file, open that file for
    239      1.24       cgd 	 * writing and make sure it's a 'normal'.
    240      1.24       cgd 	 */
    241      1.33       cgd 	if (SCARG(uap, path) != NULL) {
    242      1.33       cgd 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path),
    243  1.54.2.1   darrenr 		    l);
    244      1.52  christos 		if ((error = vn_open(&nd, FWRITE|O_APPEND, 0)) != 0)
    245      1.24       cgd 			return (error);
    246      1.44      fvdl 		VOP_UNLOCK(nd.ni_vp, 0);
    247      1.24       cgd 		if (nd.ni_vp->v_type != VREG) {
    248  1.54.2.1   darrenr 			vn_close(nd.ni_vp, FWRITE, p->p_ucred, l);
    249      1.24       cgd 			return (EACCES);
    250      1.24       cgd 		}
    251      1.24       cgd 	}
    252      1.24       cgd 
    253      1.48     enami 	ACCT_LOCK();
    254      1.48     enami 
    255      1.24       cgd 	/*
    256      1.24       cgd 	 * If accounting was previously enabled, kill the old space-watcher,
    257      1.48     enami 	 * free credential for accounting file i/o,
    258      1.48     enami 	 * ... (and, if no new file was specified, leave).
    259      1.24       cgd 	 */
    260      1.48     enami 	acct_stop();
    261      1.33       cgd 	if (SCARG(uap, path) == NULL)
    262      1.48     enami 		goto out;
    263      1.24       cgd 
    264      1.22       cgd 	/*
    265      1.48     enami 	 * Save the new accounting file vnode and credential,
    266      1.48     enami 	 * and schedule the new free space watcher.
    267      1.22       cgd 	 */
    268      1.48     enami 	acct_state = ACCT_ACTIVE;
    269      1.48     enami 	acct_vp = nd.ni_vp;
    270      1.48     enami 	acct_ucred = p->p_ucred;
    271      1.48     enami 	crhold(acct_ucred);
    272      1.48     enami 
    273      1.48     enami 	error = acct_chkfree();		/* Initial guess. */
    274      1.48     enami 	if (error != 0) {
    275      1.48     enami 		acct_stop();
    276      1.48     enami 		goto out;
    277      1.48     enami 	}
    278      1.48     enami 
    279      1.48     enami 	if (acct_dkwatcher == NULL) {
    280      1.48     enami 		error = kthread_create1(acctwatch, NULL, &acct_dkwatcher,
    281      1.48     enami 		    "acctwatch");
    282      1.48     enami 		if (error != 0)
    283      1.48     enami 			acct_stop();
    284      1.48     enami 	}
    285      1.48     enami 
    286      1.48     enami  out:
    287      1.48     enami 	ACCT_UNLOCK();
    288      1.24       cgd 	return (error);
    289      1.23       cgd }
    290      1.22       cgd 
    291      1.24       cgd /*
    292      1.24       cgd  * Write out process accounting information, on process exit.
    293      1.24       cgd  * Data to be written out is specified in Leffler, et al.
    294      1.24       cgd  * and are enumerated below.  (They're also noted in the system
    295      1.24       cgd  * "acct.h" header file.)
    296      1.24       cgd  */
    297      1.38       cgd int
    298  1.54.2.1   darrenr acct_process(l)
    299  1.54.2.1   darrenr 	struct lwp *l;
    300      1.23       cgd {
    301      1.24       cgd 	struct acct acct;
    302      1.24       cgd 	struct rusage *r;
    303      1.24       cgd 	struct timeval ut, st, tmp;
    304      1.48     enami 	int s, t, error = 0;
    305      1.49   thorpej 	struct plimit *oplim = NULL;
    306  1.54.2.1   darrenr 	struct proc *p = l->l_proc;
    307      1.48     enami 
    308      1.48     enami 	ACCT_LOCK();
    309      1.24       cgd 
    310      1.24       cgd 	/* If accounting isn't enabled, don't bother */
    311      1.48     enami 	if (acct_state != ACCT_ACTIVE)
    312      1.48     enami 		goto out;
    313      1.24       cgd 
    314      1.24       cgd 	/*
    315      1.49   thorpej 	 * Raise the file limit so that accounting can't be stopped by
    316      1.49   thorpej 	 * the user.
    317      1.49   thorpej 	 *
    318      1.49   thorpej 	 * XXX We should think about the CPU limit, too.
    319      1.49   thorpej 	 */
    320      1.49   thorpej 	if (p->p_limit->p_refcnt > 1) {
    321      1.49   thorpej 		oplim = p->p_limit;
    322      1.49   thorpej 		p->p_limit = limcopy(p->p_limit);
    323      1.49   thorpej 	}
    324      1.49   thorpej 	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    325      1.49   thorpej 
    326      1.49   thorpej 	/*
    327      1.24       cgd 	 * Get process accounting information.
    328      1.24       cgd 	 */
    329      1.24       cgd 
    330      1.24       cgd 	/* (1) The name of the command that ran */
    331      1.47     perry 	memcpy(acct.ac_comm, p->p_comm, sizeof(acct.ac_comm));
    332      1.24       cgd 
    333      1.24       cgd 	/* (2) The amount of user and system time that was used */
    334      1.24       cgd 	calcru(p, &ut, &st, NULL);
    335      1.24       cgd 	acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
    336      1.24       cgd 	acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
    337      1.24       cgd 
    338      1.24       cgd 	/* (3) The elapsed time the commmand ran (and its starting time) */
    339      1.24       cgd 	acct.ac_btime = p->p_stats->p_start.tv_sec;
    340      1.24       cgd 	s = splclock();
    341      1.39   mycroft 	timersub(&time, &p->p_stats->p_start, &tmp);
    342      1.24       cgd 	splx(s);
    343      1.24       cgd 	acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
    344      1.24       cgd 
    345      1.24       cgd 	/* (4) The average amount of memory used */
    346      1.24       cgd 	r = &p->p_stats->p_ru;
    347      1.39   mycroft 	timeradd(&ut, &st, &tmp);
    348      1.24       cgd 	t = tmp.tv_sec * hz + tmp.tv_usec / tick;
    349      1.24       cgd 	if (t)
    350      1.24       cgd 		acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
    351      1.24       cgd 	else
    352      1.24       cgd 		acct.ac_mem = 0;
    353      1.24       cgd 
    354      1.29       cgd 	/* (5) The number of disk I/O operations done */
    355      1.24       cgd 	acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
    356      1.24       cgd 
    357      1.24       cgd 	/* (6) The UID and GID of the process */
    358      1.24       cgd 	acct.ac_uid = p->p_cred->p_ruid;
    359      1.24       cgd 	acct.ac_gid = p->p_cred->p_rgid;
    360      1.24       cgd 
    361      1.24       cgd 	/* (7) The terminal from which the process was started */
    362      1.24       cgd 	if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
    363      1.24       cgd 		acct.ac_tty = p->p_pgrp->pg_session->s_ttyp->t_dev;
    364      1.24       cgd 	else
    365      1.24       cgd 		acct.ac_tty = NODEV;
    366      1.24       cgd 
    367      1.24       cgd 	/* (8) The boolean flags that tell how the process terminated, etc. */
    368      1.24       cgd 	acct.ac_flag = p->p_acflag;
    369      1.22       cgd 
    370      1.22       cgd 	/*
    371      1.24       cgd 	 * Now, just write the accounting information to the file.
    372      1.22       cgd 	 */
    373  1.54.2.1   darrenr 	VOP_LEASE(acct_vp, l, p->p_ucred, LEASE_WRITE);
    374      1.48     enami 	error = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct,
    375      1.48     enami 	    sizeof(acct), (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT,
    376  1.54.2.1   darrenr 	    acct_ucred, NULL, l);
    377      1.48     enami 	if (error != 0)
    378      1.48     enami 		log(LOG_ERR, "Accounting: write failed %d\n", error);
    379      1.49   thorpej 
    380      1.49   thorpej 	if (oplim) {
    381      1.49   thorpej 		limfree(p->p_limit);
    382      1.49   thorpej 		p->p_limit = oplim;
    383      1.49   thorpej 	}
    384      1.48     enami 
    385      1.48     enami  out:
    386      1.48     enami 	ACCT_UNLOCK();
    387      1.48     enami 	return (error);
    388      1.22       cgd }
    389      1.22       cgd 
    390      1.22       cgd /*
    391      1.24       cgd  * Encode_comp_t converts from ticks in seconds and microseconds
    392      1.24       cgd  * to ticks in 1/AHZ seconds.  The encoding is described in
    393      1.24       cgd  * Leffler, et al., on page 63.
    394      1.22       cgd  */
    395      1.23       cgd 
    396      1.24       cgd #define	MANTSIZE	13			/* 13 bit mantissa. */
    397      1.24       cgd #define	EXPSIZE		3			/* Base 8 (3 bit) exponent. */
    398      1.24       cgd #define	MAXFRACT	((1 << MANTSIZE) - 1)	/* Maximum fractional value. */
    399      1.24       cgd 
    400      1.24       cgd comp_t
    401      1.24       cgd encode_comp_t(s, us)
    402      1.24       cgd 	u_long s, us;
    403      1.24       cgd {
    404      1.24       cgd 	int exp, rnd;
    405      1.24       cgd 
    406      1.24       cgd 	exp = 0;
    407      1.24       cgd 	rnd = 0;
    408      1.24       cgd 	s *= AHZ;
    409      1.24       cgd 	s += us / (1000000 / AHZ);	/* Maximize precision. */
    410      1.24       cgd 
    411      1.24       cgd 	while (s > MAXFRACT) {
    412      1.24       cgd 	rnd = s & (1 << (EXPSIZE - 1));	/* Round up? */
    413      1.24       cgd 		s >>= EXPSIZE;		/* Base 8 exponent == 3 bit shift. */
    414      1.24       cgd 		exp++;
    415      1.24       cgd 	}
    416      1.24       cgd 
    417      1.24       cgd 	/* If we need to round up, do it (and handle overflow correctly). */
    418      1.24       cgd 	if (rnd && (++s > MAXFRACT)) {
    419      1.24       cgd 		s >>= EXPSIZE;
    420      1.24       cgd 		exp++;
    421      1.24       cgd 	}
    422      1.24       cgd 
    423      1.24       cgd 	/* Clean it up and polish it off. */
    424      1.24       cgd 	exp <<= MANTSIZE;		/* Shift the exponent into place */
    425      1.24       cgd 	exp += s;			/* and add on the mantissa. */
    426      1.24       cgd 	return (exp);
    427      1.24       cgd }
    428      1.23       cgd 
    429      1.23       cgd /*
    430      1.24       cgd  * Periodically check the file system to see if accounting
    431      1.27       cgd  * should be turned on or off.  Beware the case where the vnode
    432      1.27       cgd  * has been vgone()'d out from underneath us, e.g. when the file
    433      1.27       cgd  * system containing the accounting file has been forcibly unmounted.
    434      1.23       cgd  */
    435      1.22       cgd void
    436      1.48     enami acctwatch(arg)
    437      1.48     enami 	void *arg;
    438      1.22       cgd {
    439      1.48     enami 	int error;
    440      1.22       cgd 
    441      1.48     enami 	log(LOG_NOTICE, "Accounting started\n");
    442      1.48     enami 	ACCT_LOCK();
    443      1.48     enami 	while (acct_state != ACCT_STOP) {
    444      1.48     enami 		if (acct_vp->v_type == VBAD) {
    445      1.48     enami 			log(LOG_NOTICE, "Accounting terminated\n");
    446      1.48     enami 			acct_stop();
    447      1.48     enami 			continue;
    448      1.31       cgd 		}
    449      1.48     enami 
    450      1.48     enami 		error = acct_chkfree();
    451      1.48     enami #ifdef DIAGNOSTIC
    452      1.48     enami 		if (error != 0)
    453  1.54.2.2     skrll 			printf("acctwatch: failed to statvfs, error = %d\n",
    454      1.48     enami 			    error);
    455      1.48     enami #endif
    456      1.48     enami 
    457      1.48     enami 		ACCT_UNLOCK();
    458      1.48     enami 		error = tsleep(acctwatch, PSWP, "actwat", acctchkfreq * hz);
    459      1.48     enami 		ACCT_LOCK();
    460      1.48     enami #ifdef DIAGNOSTIC
    461      1.48     enami 		if (error != 0 && error != EWOULDBLOCK)
    462      1.48     enami 			printf("acctwatch: sleep error %d\n", error);
    463      1.48     enami #endif
    464      1.48     enami 	}
    465      1.48     enami 	acct_dkwatcher = NULL;
    466      1.48     enami 	ACCT_UNLOCK();
    467      1.48     enami 
    468      1.48     enami 	kthread_exit(0);
    469      1.22       cgd }
    470