Home | History | Annotate | Line # | Download | only in kern
kern_acct.c revision 1.47.12.1
      1  1.47.12.1    bouyer /*	$NetBSD: kern_acct.c,v 1.47.12.1 2000/11/20 18:08:57 bouyer 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.44      fvdl  *	@(#)kern_acct.c	8.8 (Berkeley) 5/14/95
     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.47.12.1    bouyer #include <sys/kthread.h>
     53  1.47.12.1    bouyer #include <sys/lock.h>
     54  1.47.12.1    bouyer #include <sys/malloc.h>
     55       1.24       cgd #include <sys/namei.h>
     56       1.24       cgd #include <sys/errno.h>
     57       1.24       cgd #include <sys/acct.h>
     58       1.24       cgd #include <sys/resourcevar.h>
     59       1.24       cgd #include <sys/ioctl.h>
     60       1.24       cgd #include <sys/tty.h>
     61       1.22       cgd 
     62       1.33       cgd #include <sys/syscallargs.h>
     63       1.33       cgd 
     64       1.24       cgd /*
     65       1.24       cgd  * The routines implemented in this file are described in:
     66       1.24       cgd  *      Leffler, et al.: The Design and Implementation of the 4.3BSD
     67       1.24       cgd  *	    UNIX Operating System (Addison Welley, 1989)
     68       1.24       cgd  * on pages 62-63.
     69       1.24       cgd  *
     70       1.24       cgd  * Arguably, to simplify accounting operations, this mechanism should
     71       1.24       cgd  * be replaced by one in which an accounting log file (similar to /dev/klog)
     72       1.24       cgd  * is read by a user process, etc.  However, that has its own problems.
     73       1.24       cgd  */
     74       1.24       cgd 
     75       1.24       cgd /*
     76  1.47.12.1    bouyer  * The global accounting state and related data.  Gain the lock before
     77  1.47.12.1    bouyer  * accessing these variables.
     78  1.47.12.1    bouyer  */
     79  1.47.12.1    bouyer enum {
     80  1.47.12.1    bouyer 	ACCT_STOP,
     81  1.47.12.1    bouyer 	ACCT_ACTIVE,
     82  1.47.12.1    bouyer 	ACCT_SUSPENDED
     83  1.47.12.1    bouyer } acct_state;				/* The current accounting state. */
     84  1.47.12.1    bouyer struct vnode *acct_vp;			/* Accounting vnode pointer. */
     85  1.47.12.1    bouyer struct ucred *acct_ucred;		/* Credential of accounting file
     86  1.47.12.1    bouyer 					   owner (i.e root).  Used when
     87  1.47.12.1    bouyer  					   accounting file i/o.  */
     88  1.47.12.1    bouyer struct proc *acct_dkwatcher;		/* Free disk space checker. */
     89  1.47.12.1    bouyer 
     90  1.47.12.1    bouyer /*
     91  1.47.12.1    bouyer  * Lock to serialize system calls and kernel threads.
     92  1.47.12.1    bouyer  */
     93  1.47.12.1    bouyer struct	lock acct_lock;
     94  1.47.12.1    bouyer #define	ACCT_LOCK()						\
     95  1.47.12.1    bouyer do {								\
     96  1.47.12.1    bouyer 	(void) lockmgr(&acct_lock, LK_EXCLUSIVE, NULL);		\
     97  1.47.12.1    bouyer } while (/* CONSTCOND */0)
     98  1.47.12.1    bouyer #define	ACCT_UNLOCK()						\
     99  1.47.12.1    bouyer do {								\
    100  1.47.12.1    bouyer 	(void) lockmgr(&acct_lock, LK_RELEASE, NULL);		\
    101  1.47.12.1    bouyer } while (/* CONSTCOND */0)
    102  1.47.12.1    bouyer 
    103  1.47.12.1    bouyer /*
    104       1.24       cgd  * Internal accounting functions.
    105       1.24       cgd  * The former's operation is described in Leffler, et al., and the latter
    106       1.24       cgd  * was provided by UCB with the 4.4BSD-Lite release
    107       1.24       cgd  */
    108       1.24       cgd comp_t	encode_comp_t __P((u_long, u_long));
    109       1.24       cgd void	acctwatch __P((void *));
    110  1.47.12.1    bouyer void	acct_stop __P((void));
    111  1.47.12.1    bouyer int	acct_chkfree __P((void));
    112       1.24       cgd 
    113       1.24       cgd /*
    114       1.24       cgd  * Values associated with enabling and disabling accounting
    115       1.24       cgd  */
    116       1.24       cgd int	acctsuspend = 2;	/* stop accounting when < 2% free space left */
    117       1.24       cgd int	acctresume = 4;		/* resume when free space risen to > 4% */
    118       1.24       cgd int	acctchkfreq = 15;	/* frequency (in seconds) to check space */
    119       1.24       cgd 
    120  1.47.12.1    bouyer void
    121  1.47.12.1    bouyer acct_init()
    122  1.47.12.1    bouyer {
    123  1.47.12.1    bouyer 
    124  1.47.12.1    bouyer 	acct_state = ACCT_STOP;
    125  1.47.12.1    bouyer 	acct_vp = NULLVP;
    126  1.47.12.1    bouyer 	acct_ucred = NULL;
    127  1.47.12.1    bouyer 	lockinit(&acct_lock, PWAIT, "acctlk", 0, 0);
    128  1.47.12.1    bouyer }
    129  1.47.12.1    bouyer 
    130  1.47.12.1    bouyer void
    131  1.47.12.1    bouyer acct_stop()
    132  1.47.12.1    bouyer {
    133  1.47.12.1    bouyer 	int error;
    134  1.47.12.1    bouyer 
    135  1.47.12.1    bouyer 	if (acct_vp != NULLVP && acct_vp->v_type != VBAD) {
    136  1.47.12.1    bouyer 		error = vn_close(acct_vp, FWRITE, acct_ucred, NULL);
    137  1.47.12.1    bouyer #ifdef DIAGNOSTIC
    138  1.47.12.1    bouyer 		if (error != 0)
    139  1.47.12.1    bouyer 			printf("acct_stop: failed to close, errno = %d\n",
    140  1.47.12.1    bouyer 			    error);
    141  1.47.12.1    bouyer #endif
    142  1.47.12.1    bouyer 		acct_vp = NULLVP;
    143  1.47.12.1    bouyer 	}
    144  1.47.12.1    bouyer 	if (acct_ucred != NULL) {
    145  1.47.12.1    bouyer 		crfree(acct_ucred);
    146  1.47.12.1    bouyer 		acct_ucred = NULL;
    147  1.47.12.1    bouyer 	}
    148  1.47.12.1    bouyer 	acct_state = ACCT_STOP;
    149  1.47.12.1    bouyer }
    150  1.47.12.1    bouyer 
    151  1.47.12.1    bouyer int
    152  1.47.12.1    bouyer acct_chkfree()
    153  1.47.12.1    bouyer {
    154  1.47.12.1    bouyer 	int error;
    155  1.47.12.1    bouyer 	struct statfs sb;
    156  1.47.12.1    bouyer 
    157  1.47.12.1    bouyer 	error = VFS_STATFS(acct_vp->v_mount, &sb, NULL);
    158  1.47.12.1    bouyer 	if (error != 0)
    159  1.47.12.1    bouyer 		return (error);
    160  1.47.12.1    bouyer 
    161  1.47.12.1    bouyer 	switch (acct_state) {
    162  1.47.12.1    bouyer 	case ACCT_SUSPENDED:
    163  1.47.12.1    bouyer 		if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
    164  1.47.12.1    bouyer 			acct_state = ACCT_ACTIVE;
    165  1.47.12.1    bouyer 			log(LOG_NOTICE, "Accounting resumed\n");
    166  1.47.12.1    bouyer 		}
    167  1.47.12.1    bouyer 		break;
    168  1.47.12.1    bouyer 	case ACCT_ACTIVE:
    169  1.47.12.1    bouyer 		if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
    170  1.47.12.1    bouyer 			acct_state = ACCT_SUSPENDED;
    171  1.47.12.1    bouyer 			log(LOG_NOTICE, "Accounting suspended\n");
    172  1.47.12.1    bouyer 		}
    173  1.47.12.1    bouyer 		break;
    174  1.47.12.1    bouyer 	case ACCT_STOP:
    175  1.47.12.1    bouyer 		break;
    176  1.47.12.1    bouyer 	}
    177  1.47.12.1    bouyer 	return (0);
    178  1.47.12.1    bouyer }
    179  1.47.12.1    bouyer 
    180       1.24       cgd /*
    181       1.24       cgd  * Accounting system call.  Written based on the specification and
    182       1.24       cgd  * previous implementation done by Mark Tinguely.
    183       1.24       cgd  */
    184       1.38       cgd int
    185       1.41   mycroft sys_acct(p, v, retval)
    186       1.24       cgd 	struct proc *p;
    187       1.40   thorpej 	void *v;
    188       1.40   thorpej 	register_t *retval;
    189       1.40   thorpej {
    190       1.41   mycroft 	struct sys_acct_args /* {
    191       1.43   mycroft 		syscallarg(const char *) path;
    192       1.40   thorpej 	} */ *uap = v;
    193       1.24       cgd 	struct nameidata nd;
    194       1.24       cgd 	int error;
    195       1.24       cgd 
    196       1.24       cgd 	/* Make sure that the caller is root. */
    197       1.42  christos 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    198       1.24       cgd 		return (error);
    199       1.24       cgd 
    200       1.24       cgd 	/*
    201       1.24       cgd 	 * If accounting is to be started to a file, open that file for
    202       1.24       cgd 	 * writing and make sure it's a 'normal'.
    203       1.24       cgd 	 */
    204       1.33       cgd 	if (SCARG(uap, path) != NULL) {
    205       1.33       cgd 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path),
    206       1.33       cgd 		    p);
    207       1.42  christos 		if ((error = vn_open(&nd, FWRITE, 0)) != 0)
    208       1.24       cgd 			return (error);
    209       1.44      fvdl 		VOP_UNLOCK(nd.ni_vp, 0);
    210       1.24       cgd 		if (nd.ni_vp->v_type != VREG) {
    211       1.24       cgd 			vn_close(nd.ni_vp, FWRITE, p->p_ucred, p);
    212       1.24       cgd 			return (EACCES);
    213       1.24       cgd 		}
    214       1.24       cgd 	}
    215       1.24       cgd 
    216  1.47.12.1    bouyer 	ACCT_LOCK();
    217  1.47.12.1    bouyer 
    218       1.24       cgd 	/*
    219       1.24       cgd 	 * If accounting was previously enabled, kill the old space-watcher,
    220  1.47.12.1    bouyer 	 * free credential for accounting file i/o,
    221  1.47.12.1    bouyer 	 * ... (and, if no new file was specified, leave).
    222       1.24       cgd 	 */
    223  1.47.12.1    bouyer 	acct_stop();
    224       1.33       cgd 	if (SCARG(uap, path) == NULL)
    225  1.47.12.1    bouyer 		goto out;
    226       1.24       cgd 
    227       1.22       cgd 	/*
    228  1.47.12.1    bouyer 	 * Save the new accounting file vnode and credential,
    229  1.47.12.1    bouyer 	 * and schedule the new free space watcher.
    230       1.22       cgd 	 */
    231  1.47.12.1    bouyer 	acct_state = ACCT_ACTIVE;
    232  1.47.12.1    bouyer 	acct_vp = nd.ni_vp;
    233  1.47.12.1    bouyer 	acct_ucred = p->p_ucred;
    234  1.47.12.1    bouyer 	crhold(acct_ucred);
    235  1.47.12.1    bouyer 
    236  1.47.12.1    bouyer 	error = acct_chkfree();		/* Initial guess. */
    237  1.47.12.1    bouyer 	if (error != 0) {
    238  1.47.12.1    bouyer 		acct_stop();
    239  1.47.12.1    bouyer 		goto out;
    240  1.47.12.1    bouyer 	}
    241  1.47.12.1    bouyer 
    242  1.47.12.1    bouyer 	if (acct_dkwatcher == NULL) {
    243  1.47.12.1    bouyer 		error = kthread_create1(acctwatch, NULL, &acct_dkwatcher,
    244  1.47.12.1    bouyer 		    "acctwatch");
    245  1.47.12.1    bouyer 		if (error != 0)
    246  1.47.12.1    bouyer 			acct_stop();
    247  1.47.12.1    bouyer 	}
    248  1.47.12.1    bouyer 
    249  1.47.12.1    bouyer  out:
    250  1.47.12.1    bouyer 	ACCT_UNLOCK();
    251       1.24       cgd 	return (error);
    252       1.23       cgd }
    253       1.22       cgd 
    254       1.24       cgd /*
    255       1.24       cgd  * Write out process accounting information, on process exit.
    256       1.24       cgd  * Data to be written out is specified in Leffler, et al.
    257       1.24       cgd  * and are enumerated below.  (They're also noted in the system
    258       1.24       cgd  * "acct.h" header file.)
    259       1.24       cgd  */
    260       1.38       cgd int
    261       1.24       cgd acct_process(p)
    262       1.24       cgd 	struct proc *p;
    263       1.23       cgd {
    264       1.24       cgd 	struct acct acct;
    265       1.24       cgd 	struct rusage *r;
    266       1.24       cgd 	struct timeval ut, st, tmp;
    267  1.47.12.1    bouyer 	int s, t, error = 0;
    268  1.47.12.1    bouyer 	struct plimit *oplim = NULL;
    269  1.47.12.1    bouyer 
    270  1.47.12.1    bouyer 	ACCT_LOCK();
    271       1.24       cgd 
    272       1.24       cgd 	/* If accounting isn't enabled, don't bother */
    273  1.47.12.1    bouyer 	if (acct_state != ACCT_ACTIVE)
    274  1.47.12.1    bouyer 		goto out;
    275  1.47.12.1    bouyer 
    276  1.47.12.1    bouyer 	/*
    277  1.47.12.1    bouyer 	 * Raise the file limit so that accounting can't be stopped by
    278  1.47.12.1    bouyer 	 * the user.
    279  1.47.12.1    bouyer 	 *
    280  1.47.12.1    bouyer 	 * XXX We should think about the CPU limit, too.
    281  1.47.12.1    bouyer 	 */
    282  1.47.12.1    bouyer 	if (p->p_limit->p_refcnt > 1) {
    283  1.47.12.1    bouyer 		oplim = p->p_limit;
    284  1.47.12.1    bouyer 		p->p_limit = limcopy(p->p_limit);
    285  1.47.12.1    bouyer 	}
    286  1.47.12.1    bouyer 	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    287       1.24       cgd 
    288       1.24       cgd 	/*
    289       1.24       cgd 	 * Get process accounting information.
    290       1.24       cgd 	 */
    291       1.24       cgd 
    292       1.24       cgd 	/* (1) The name of the command that ran */
    293       1.47     perry 	memcpy(acct.ac_comm, p->p_comm, sizeof(acct.ac_comm));
    294       1.24       cgd 
    295       1.24       cgd 	/* (2) The amount of user and system time that was used */
    296       1.24       cgd 	calcru(p, &ut, &st, NULL);
    297       1.24       cgd 	acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
    298       1.24       cgd 	acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
    299       1.24       cgd 
    300       1.24       cgd 	/* (3) The elapsed time the commmand ran (and its starting time) */
    301       1.24       cgd 	acct.ac_btime = p->p_stats->p_start.tv_sec;
    302       1.24       cgd 	s = splclock();
    303       1.39   mycroft 	timersub(&time, &p->p_stats->p_start, &tmp);
    304       1.24       cgd 	splx(s);
    305       1.24       cgd 	acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
    306       1.24       cgd 
    307       1.24       cgd 	/* (4) The average amount of memory used */
    308       1.24       cgd 	r = &p->p_stats->p_ru;
    309       1.39   mycroft 	timeradd(&ut, &st, &tmp);
    310       1.24       cgd 	t = tmp.tv_sec * hz + tmp.tv_usec / tick;
    311       1.24       cgd 	if (t)
    312       1.24       cgd 		acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
    313       1.24       cgd 	else
    314       1.24       cgd 		acct.ac_mem = 0;
    315       1.24       cgd 
    316       1.29       cgd 	/* (5) The number of disk I/O operations done */
    317       1.24       cgd 	acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
    318       1.24       cgd 
    319       1.24       cgd 	/* (6) The UID and GID of the process */
    320       1.24       cgd 	acct.ac_uid = p->p_cred->p_ruid;
    321       1.24       cgd 	acct.ac_gid = p->p_cred->p_rgid;
    322       1.24       cgd 
    323       1.24       cgd 	/* (7) The terminal from which the process was started */
    324       1.24       cgd 	if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
    325       1.24       cgd 		acct.ac_tty = p->p_pgrp->pg_session->s_ttyp->t_dev;
    326       1.24       cgd 	else
    327       1.24       cgd 		acct.ac_tty = NODEV;
    328       1.24       cgd 
    329       1.24       cgd 	/* (8) The boolean flags that tell how the process terminated, etc. */
    330       1.24       cgd 	acct.ac_flag = p->p_acflag;
    331       1.22       cgd 
    332       1.22       cgd 	/*
    333       1.24       cgd 	 * Now, just write the accounting information to the file.
    334       1.22       cgd 	 */
    335  1.47.12.1    bouyer 	VOP_LEASE(acct_vp, p, p->p_ucred, LEASE_WRITE);
    336  1.47.12.1    bouyer 	error = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct,
    337  1.47.12.1    bouyer 	    sizeof(acct), (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT,
    338  1.47.12.1    bouyer 	    acct_ucred, NULL, p);
    339  1.47.12.1    bouyer 	if (error != 0)
    340  1.47.12.1    bouyer 		log(LOG_ERR, "Accounting: write failed %d\n", error);
    341  1.47.12.1    bouyer 
    342  1.47.12.1    bouyer 	if (oplim) {
    343  1.47.12.1    bouyer 		limfree(p->p_limit);
    344  1.47.12.1    bouyer 		p->p_limit = oplim;
    345  1.47.12.1    bouyer 	}
    346  1.47.12.1    bouyer 
    347  1.47.12.1    bouyer  out:
    348  1.47.12.1    bouyer 	ACCT_UNLOCK();
    349  1.47.12.1    bouyer 	return (error);
    350       1.22       cgd }
    351       1.22       cgd 
    352       1.22       cgd /*
    353       1.24       cgd  * Encode_comp_t converts from ticks in seconds and microseconds
    354       1.24       cgd  * to ticks in 1/AHZ seconds.  The encoding is described in
    355       1.24       cgd  * Leffler, et al., on page 63.
    356       1.22       cgd  */
    357       1.23       cgd 
    358       1.24       cgd #define	MANTSIZE	13			/* 13 bit mantissa. */
    359       1.24       cgd #define	EXPSIZE		3			/* Base 8 (3 bit) exponent. */
    360       1.24       cgd #define	MAXFRACT	((1 << MANTSIZE) - 1)	/* Maximum fractional value. */
    361       1.24       cgd 
    362       1.24       cgd comp_t
    363       1.24       cgd encode_comp_t(s, us)
    364       1.24       cgd 	u_long s, us;
    365       1.24       cgd {
    366       1.24       cgd 	int exp, rnd;
    367       1.24       cgd 
    368       1.24       cgd 	exp = 0;
    369       1.24       cgd 	rnd = 0;
    370       1.24       cgd 	s *= AHZ;
    371       1.24       cgd 	s += us / (1000000 / AHZ);	/* Maximize precision. */
    372       1.24       cgd 
    373       1.24       cgd 	while (s > MAXFRACT) {
    374       1.24       cgd 	rnd = s & (1 << (EXPSIZE - 1));	/* Round up? */
    375       1.24       cgd 		s >>= EXPSIZE;		/* Base 8 exponent == 3 bit shift. */
    376       1.24       cgd 		exp++;
    377       1.24       cgd 	}
    378       1.24       cgd 
    379       1.24       cgd 	/* If we need to round up, do it (and handle overflow correctly). */
    380       1.24       cgd 	if (rnd && (++s > MAXFRACT)) {
    381       1.24       cgd 		s >>= EXPSIZE;
    382       1.24       cgd 		exp++;
    383       1.24       cgd 	}
    384       1.24       cgd 
    385       1.24       cgd 	/* Clean it up and polish it off. */
    386       1.24       cgd 	exp <<= MANTSIZE;		/* Shift the exponent into place */
    387       1.24       cgd 	exp += s;			/* and add on the mantissa. */
    388       1.24       cgd 	return (exp);
    389       1.24       cgd }
    390       1.23       cgd 
    391       1.23       cgd /*
    392       1.24       cgd  * Periodically check the file system to see if accounting
    393       1.27       cgd  * should be turned on or off.  Beware the case where the vnode
    394       1.27       cgd  * has been vgone()'d out from underneath us, e.g. when the file
    395       1.27       cgd  * system containing the accounting file has been forcibly unmounted.
    396       1.23       cgd  */
    397       1.22       cgd void
    398  1.47.12.1    bouyer acctwatch(arg)
    399  1.47.12.1    bouyer 	void *arg;
    400       1.22       cgd {
    401  1.47.12.1    bouyer 	int error;
    402       1.22       cgd 
    403  1.47.12.1    bouyer 	log(LOG_NOTICE, "Accounting started\n");
    404  1.47.12.1    bouyer 	ACCT_LOCK();
    405  1.47.12.1    bouyer 	while (acct_state != ACCT_STOP) {
    406  1.47.12.1    bouyer 		if (acct_vp->v_type == VBAD) {
    407  1.47.12.1    bouyer 			log(LOG_NOTICE, "Accounting terminated\n");
    408  1.47.12.1    bouyer 			acct_stop();
    409  1.47.12.1    bouyer 			continue;
    410       1.22       cgd 		}
    411  1.47.12.1    bouyer 
    412  1.47.12.1    bouyer 		error = acct_chkfree();
    413  1.47.12.1    bouyer #ifdef DIAGNOSTIC
    414  1.47.12.1    bouyer 		if (error != 0)
    415  1.47.12.1    bouyer 			printf("acctwatch: failed to statfs, error = %d\n",
    416  1.47.12.1    bouyer 			    error);
    417  1.47.12.1    bouyer #endif
    418  1.47.12.1    bouyer 
    419  1.47.12.1    bouyer 		ACCT_UNLOCK();
    420  1.47.12.1    bouyer 		error = tsleep(acctwatch, PSWP, "actwat", acctchkfreq * hz);
    421  1.47.12.1    bouyer 		ACCT_LOCK();
    422  1.47.12.1    bouyer #ifdef DIAGNOSTIC
    423  1.47.12.1    bouyer 		if (error != 0 && error != EWOULDBLOCK)
    424  1.47.12.1    bouyer 			printf("acctwatch: sleep error %d\n", error);
    425  1.47.12.1    bouyer #endif
    426  1.47.12.1    bouyer 	}
    427  1.47.12.1    bouyer 	acct_dkwatcher = NULL;
    428  1.47.12.1    bouyer 	ACCT_UNLOCK();
    429  1.47.12.1    bouyer 
    430  1.47.12.1    bouyer 	kthread_exit(0);
    431       1.22       cgd }
    432