kern_acct.c revision 234927
1/*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * Copyright (c) 2005 Robert N. M. Watson
6 * All rights reserved.
7 *
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * Copyright (c) 1994 Christopher G. Demetriou
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 *    notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 *    notice, this list of conditions and the following disclaimer in the
46 *    documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 *    must display the following acknowledgement:
49 *	This product includes software developed by the University of
50 *	California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 *    may be used to endorse or promote products derived from this software
53 *    without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 *	@(#)kern_acct.c	8.1 (Berkeley) 6/14/93
68 */
69
70#include <sys/cdefs.h>
71__FBSDID("$FreeBSD: head/sys/kern/kern_acct.c 234927 2012-05-02 14:25:39Z jhb $");
72
73#include <sys/param.h>
74#include <sys/systm.h>
75#include <sys/acct.h>
76#include <sys/fcntl.h>
77#include <sys/kernel.h>
78#include <sys/kthread.h>
79#include <sys/limits.h>
80#include <sys/lock.h>
81#include <sys/mount.h>
82#include <sys/mutex.h>
83#include <sys/namei.h>
84#include <sys/priv.h>
85#include <sys/proc.h>
86#include <sys/resourcevar.h>
87#include <sys/sched.h>
88#include <sys/sx.h>
89#include <sys/sysctl.h>
90#include <sys/sysent.h>
91#include <sys/syslog.h>
92#include <sys/sysproto.h>
93#include <sys/tty.h>
94#include <sys/vnode.h>
95
96#include <security/mac/mac_framework.h>
97
98/*
99 * The routines implemented in this file are described in:
100 *      Leffler, et al.: The Design and Implementation of the 4.3BSD
101 *	    UNIX Operating System (Addison Welley, 1989)
102 * on pages 62-63.
103 * On May 2007 the historic 3 bits base 8 exponent, 13 bit fraction
104 * compt_t representation described in the above reference was replaced
105 * with that of IEEE-754 floats.
106 *
107 * Arguably, to simplify accounting operations, this mechanism should
108 * be replaced by one in which an accounting log file (similar to /dev/klog)
109 * is read by a user process, etc.  However, that has its own problems.
110 */
111
112/* Floating point definitions from <float.h>. */
113#define FLT_MANT_DIG    24              /* p */
114#define FLT_MAX_EXP     128             /* emax */
115
116/*
117 * Internal accounting functions.
118 * The former's operation is described in Leffler, et al., and the latter
119 * was provided by UCB with the 4.4BSD-Lite release
120 */
121static uint32_t	encode_timeval(struct timeval);
122static uint32_t	encode_long(long);
123static void	acctwatch(void);
124static void	acct_thread(void *);
125static int	acct_disable(struct thread *, int);
126
127/*
128 * Accounting vnode pointer, saved vnode pointer, and flags for each.
129 * acct_sx protects against changes to the active vnode and credentials
130 * while accounting records are being committed to disk.
131 */
132static int		 acct_configured;
133static int		 acct_suspended;
134static struct vnode	*acct_vp;
135static struct ucred	*acct_cred;
136static int		 acct_flags;
137static struct sx	 acct_sx;
138
139SX_SYSINIT(acct, &acct_sx, "acct_sx");
140
141/*
142 * State of the accounting kthread.
143 */
144static int		 acct_state;
145
146#define	ACCT_RUNNING	1	/* Accounting kthread is running. */
147#define	ACCT_EXITREQ	2	/* Accounting kthread should exit. */
148
149/*
150 * Values associated with enabling and disabling accounting
151 */
152static int acctsuspend = 2;	/* stop accounting when < 2% free space left */
153SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW,
154	&acctsuspend, 0, "percentage of free disk space below which accounting stops");
155
156static int acctresume = 4;	/* resume when free space risen to > 4% */
157SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW,
158	&acctresume, 0, "percentage of free disk space above which accounting resumes");
159
160static int acctchkfreq = 15;	/* frequency (in seconds) to check space */
161
162static int
163sysctl_acct_chkfreq(SYSCTL_HANDLER_ARGS)
164{
165	int error, value;
166
167	/* Write out the old value. */
168	error = SYSCTL_OUT(req, &acctchkfreq, sizeof(int));
169	if (error || req->newptr == NULL)
170		return (error);
171
172	/* Read in and verify the new value. */
173	error = SYSCTL_IN(req, &value, sizeof(int));
174	if (error)
175		return (error);
176	if (value <= 0)
177		return (EINVAL);
178	acctchkfreq = value;
179	return (0);
180}
181SYSCTL_PROC(_kern, OID_AUTO, acct_chkfreq, CTLTYPE_INT|CTLFLAG_RW,
182    &acctchkfreq, 0, sysctl_acct_chkfreq, "I",
183    "frequency for checking the free space");
184
185SYSCTL_INT(_kern, OID_AUTO, acct_configured, CTLFLAG_RD, &acct_configured, 0,
186	"Accounting configured or not");
187
188SYSCTL_INT(_kern, OID_AUTO, acct_suspended, CTLFLAG_RD, &acct_suspended, 0,
189	"Accounting suspended or not");
190
191/*
192 * Accounting system call.  Written based on the specification and previous
193 * implementation done by Mark Tinguely.
194 */
195int
196sys_acct(struct thread *td, struct acct_args *uap)
197{
198	struct nameidata nd;
199	int error, flags, vfslocked, replacing;
200
201	error = priv_check(td, PRIV_ACCT);
202	if (error)
203		return (error);
204
205	/*
206	 * If accounting is to be started to a file, open that file for
207	 * appending and make sure it's a 'normal'.
208	 */
209	if (uap->path != NULL) {
210		NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1,
211		    UIO_USERSPACE, uap->path, td);
212		flags = FWRITE | O_APPEND;
213		error = vn_open(&nd, &flags, 0, NULL);
214		if (error)
215			return (error);
216		vfslocked = NDHASGIANT(&nd);
217		NDFREE(&nd, NDF_ONLY_PNBUF);
218#ifdef MAC
219		error = mac_system_check_acct(td->td_ucred, nd.ni_vp);
220		if (error) {
221			VOP_UNLOCK(nd.ni_vp, 0);
222			vn_close(nd.ni_vp, flags, td->td_ucred, td);
223			VFS_UNLOCK_GIANT(vfslocked);
224			return (error);
225		}
226#endif
227		VOP_UNLOCK(nd.ni_vp, 0);
228		if (nd.ni_vp->v_type != VREG) {
229			vn_close(nd.ni_vp, flags, td->td_ucred, td);
230			VFS_UNLOCK_GIANT(vfslocked);
231			return (EACCES);
232		}
233		VFS_UNLOCK_GIANT(vfslocked);
234#ifdef MAC
235	} else {
236		error = mac_system_check_acct(td->td_ucred, NULL);
237		if (error)
238			return (error);
239#endif
240	}
241
242	/*
243	 * Disallow concurrent access to the accounting vnode while we swap
244	 * it out, in order to prevent access after close.
245	 */
246	sx_xlock(&acct_sx);
247
248	/*
249	 * Don't log spurious disable/enable messages if we are
250	 * switching from one accounting file to another due to log
251	 * rotation.
252	 */
253	replacing = (acct_vp != NULL && uap->path != NULL);
254
255	/*
256	 * If accounting was previously enabled, kill the old space-watcher,
257	 * close the file, and (if no new file was specified, leave).  Reset
258	 * the suspended state regardless of whether accounting remains
259	 * enabled.
260	 */
261	acct_suspended = 0;
262	if (acct_vp != NULL) {
263		vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
264		error = acct_disable(td, !replacing);
265		VFS_UNLOCK_GIANT(vfslocked);
266	}
267	if (uap->path == NULL) {
268		if (acct_state & ACCT_RUNNING) {
269			acct_state |= ACCT_EXITREQ;
270			wakeup(&acct_state);
271		}
272		sx_xunlock(&acct_sx);
273		return (error);
274	}
275
276	/*
277	 * Save the new accounting file vnode, and schedule the new
278	 * free space watcher.
279	 */
280	acct_vp = nd.ni_vp;
281	acct_cred = crhold(td->td_ucred);
282	acct_flags = flags;
283	if (acct_state & ACCT_RUNNING)
284		acct_state &= ~ACCT_EXITREQ;
285	else {
286		/*
287		 * Try to start up an accounting kthread.  We may start more
288		 * than one, but if so the extras will commit suicide as
289		 * soon as they start up.
290		 */
291		error = kproc_create(acct_thread, NULL, NULL, 0, 0,
292		    "accounting");
293		if (error) {
294			vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
295			(void) vn_close(acct_vp, acct_flags, acct_cred, td);
296			VFS_UNLOCK_GIANT(vfslocked);
297			crfree(acct_cred);
298			acct_configured = 0;
299			acct_vp = NULL;
300			acct_cred = NULL;
301			acct_flags = 0;
302			sx_xunlock(&acct_sx);
303			log(LOG_NOTICE, "Unable to start accounting thread\n");
304			return (error);
305		}
306	}
307	acct_configured = 1;
308	sx_xunlock(&acct_sx);
309	if (!replacing)
310		log(LOG_NOTICE, "Accounting enabled\n");
311	return (error);
312}
313
314/*
315 * Disable currently in-progress accounting by closing the vnode, dropping
316 * our reference to the credential, and clearing the vnode's flags.
317 */
318static int
319acct_disable(struct thread *td, int logging)
320{
321	int error;
322
323	sx_assert(&acct_sx, SX_XLOCKED);
324	error = vn_close(acct_vp, acct_flags, acct_cred, td);
325	crfree(acct_cred);
326	acct_configured = 0;
327	acct_vp = NULL;
328	acct_cred = NULL;
329	acct_flags = 0;
330	if (logging)
331		log(LOG_NOTICE, "Accounting disabled\n");
332	return (error);
333}
334
335/*
336 * Write out process accounting information, on process exit.
337 * Data to be written out is specified in Leffler, et al.
338 * and are enumerated below.  (They're also noted in the system
339 * "acct.h" header file.)
340 */
341int
342acct_process(struct thread *td)
343{
344	struct acctv2 acct;
345	struct timeval ut, st, tmp;
346	struct plimit *newlim, *oldlim;
347	struct proc *p;
348	struct rusage ru;
349	int t, ret, vfslocked;
350
351	/*
352	 * Lockless check of accounting condition before doing the hard
353	 * work.
354	 */
355	if (acct_vp == NULL || acct_suspended)
356		return (0);
357
358	sx_slock(&acct_sx);
359
360	/*
361	 * If accounting isn't enabled, don't bother.  Have to check again
362	 * once we own the lock in case we raced with disabling of accounting
363	 * by another thread.
364	 */
365	if (acct_vp == NULL || acct_suspended) {
366		sx_sunlock(&acct_sx);
367		return (0);
368	}
369
370	p = td->td_proc;
371
372	/*
373	 * Get process accounting information.
374	 */
375
376	sx_slock(&proctree_lock);
377	PROC_LOCK(p);
378
379	/* (1) The terminal from which the process was started */
380	if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
381		acct.ac_tty = tty_udev(p->p_pgrp->pg_session->s_ttyp);
382	else
383		acct.ac_tty = NODEV;
384	sx_sunlock(&proctree_lock);
385
386	/* (2) The name of the command that ran */
387	bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
388
389	/* (3) The amount of user and system time that was used */
390	rufetchcalc(p, &ru, &ut, &st);
391	acct.ac_utime = encode_timeval(ut);
392	acct.ac_stime = encode_timeval(st);
393
394	/* (4) The elapsed time the command ran (and its starting time) */
395	tmp = boottime;
396	timevaladd(&tmp, &p->p_stats->p_start);
397	acct.ac_btime = tmp.tv_sec;
398	microuptime(&tmp);
399	timevalsub(&tmp, &p->p_stats->p_start);
400	acct.ac_etime = encode_timeval(tmp);
401
402	/* (5) The average amount of memory used */
403	tmp = ut;
404	timevaladd(&tmp, &st);
405	/* Convert tmp (i.e. u + s) into hz units to match ru_i*. */
406	t = tmp.tv_sec * hz + tmp.tv_usec / tick;
407	if (t)
408		acct.ac_mem = encode_long((ru.ru_ixrss + ru.ru_idrss +
409		    + ru.ru_isrss) / t);
410	else
411		acct.ac_mem = 0;
412
413	/* (6) The number of disk I/O operations done */
414	acct.ac_io = encode_long(ru.ru_inblock + ru.ru_oublock);
415
416	/* (7) The UID and GID of the process */
417	acct.ac_uid = p->p_ucred->cr_ruid;
418	acct.ac_gid = p->p_ucred->cr_rgid;
419
420	/* (8) The boolean flags that tell how the process terminated, etc. */
421	acct.ac_flagx = p->p_acflag;
422	PROC_UNLOCK(p);
423
424	/* Setup ancillary structure fields. */
425	acct.ac_flagx |= ANVER;
426	acct.ac_zero = 0;
427	acct.ac_version = 2;
428	acct.ac_len = acct.ac_len2 = sizeof(acct);
429
430	/*
431	 * Eliminate any file size rlimit.
432	 */
433	newlim = lim_alloc();
434	PROC_LOCK(p);
435	oldlim = p->p_limit;
436	lim_copy(newlim, oldlim);
437	newlim->pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
438	p->p_limit = newlim;
439	PROC_UNLOCK(p);
440	lim_free(oldlim);
441
442	/*
443	 * Write the accounting information to the file.
444	 */
445	vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
446	ret = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct, sizeof (acct),
447	    (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acct_cred, NOCRED,
448	    NULL, td);
449	VFS_UNLOCK_GIANT(vfslocked);
450	sx_sunlock(&acct_sx);
451	return (ret);
452}
453
454/* FLOAT_CONVERSION_START (Regression testing; don't remove this line.) */
455
456/* Convert timevals and longs into IEEE-754 bit patterns. */
457
458/* Mantissa mask (MSB is implied, so subtract 1). */
459#define MANT_MASK ((1 << (FLT_MANT_DIG - 1)) - 1)
460
461/*
462 * We calculate integer values to a precision of approximately
463 * 28 bits.
464 * This is high-enough precision to fill the 24 float bits
465 * and low-enough to avoid overflowing the 32 int bits.
466 */
467#define CALC_BITS 28
468
469/* log_2(1000000). */
470#define LOG2_1M 20
471
472/*
473 * Convert the elements of a timeval into a 32-bit word holding
474 * the bits of a IEEE-754 float.
475 * The float value represents the timeval's value in microsecond units.
476 */
477static uint32_t
478encode_timeval(struct timeval tv)
479{
480	int log2_s;
481	int val, exp;	/* Unnormalized value and exponent */
482	int norm_exp;	/* Normalized exponent */
483	int shift;
484
485	/*
486	 * First calculate value and exponent to about CALC_BITS precision.
487	 * Note that the following conditionals have been ordered so that
488	 * the most common cases appear first.
489	 */
490	if (tv.tv_sec == 0) {
491		if (tv.tv_usec == 0)
492			return (0);
493		exp = 0;
494		val = tv.tv_usec;
495	} else {
496		/*
497		 * Calculate the value to a precision of approximately
498		 * CALC_BITS.
499		 */
500		log2_s = fls(tv.tv_sec) - 1;
501		if (log2_s + LOG2_1M < CALC_BITS) {
502			exp = 0;
503			val = 1000000 * tv.tv_sec + tv.tv_usec;
504		} else {
505			exp = log2_s + LOG2_1M - CALC_BITS;
506			val = (unsigned int)(((uint64_t)1000000 * tv.tv_sec +
507			    tv.tv_usec) >> exp);
508		}
509	}
510	/* Now normalize and pack the value into an IEEE-754 float. */
511	norm_exp = fls(val) - 1;
512	shift = FLT_MANT_DIG - norm_exp - 1;
513#ifdef ACCT_DEBUG
514	printf("val=%d exp=%d shift=%d log2(val)=%d\n",
515	    val, exp, shift, norm_exp);
516	printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
517	    ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
518#endif
519	return (((FLT_MAX_EXP - 1 + exp + norm_exp) << (FLT_MANT_DIG - 1)) |
520	    ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
521}
522
523/*
524 * Convert a non-negative long value into the bit pattern of
525 * an IEEE-754 float value.
526 */
527static uint32_t
528encode_long(long val)
529{
530	int norm_exp;	/* Normalized exponent */
531	int shift;
532
533	if (val == 0)
534		return (0);
535	if (val < 0) {
536		log(LOG_NOTICE,
537		    "encode_long: negative value %ld in accounting record\n",
538		    val);
539		val = LONG_MAX;
540	}
541	norm_exp = fls(val) - 1;
542	shift = FLT_MANT_DIG - norm_exp - 1;
543#ifdef ACCT_DEBUG
544	printf("val=%d shift=%d log2(val)=%d\n",
545	    val, shift, norm_exp);
546	printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
547	    ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
548#endif
549	return (((FLT_MAX_EXP - 1 + norm_exp) << (FLT_MANT_DIG - 1)) |
550	    ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
551}
552
553/* FLOAT_CONVERSION_END (Regression testing; don't remove this line.) */
554
555/*
556 * Periodically check the filesystem to see if accounting
557 * should be turned on or off.  Beware the case where the vnode
558 * has been vgone()'d out from underneath us, e.g. when the file
559 * system containing the accounting file has been forcibly unmounted.
560 */
561/* ARGSUSED */
562static void
563acctwatch(void)
564{
565	struct statfs sb;
566	int vfslocked;
567
568	sx_assert(&acct_sx, SX_XLOCKED);
569
570	/*
571	 * If accounting was disabled before our kthread was scheduled,
572	 * then acct_vp might be NULL.  If so, just ask our kthread to
573	 * exit and return.
574	 */
575	if (acct_vp == NULL) {
576		acct_state |= ACCT_EXITREQ;
577		return;
578	}
579
580	/*
581	 * If our vnode is no longer valid, tear it down and signal the
582	 * accounting thread to die.
583	 */
584	vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
585	if (acct_vp->v_type == VBAD) {
586		(void) acct_disable(NULL, 1);
587		VFS_UNLOCK_GIANT(vfslocked);
588		acct_state |= ACCT_EXITREQ;
589		return;
590	}
591
592	/*
593	 * Stopping here is better than continuing, maybe it will be VBAD
594	 * next time around.
595	 */
596	if (VFS_STATFS(acct_vp->v_mount, &sb) < 0) {
597		VFS_UNLOCK_GIANT(vfslocked);
598		return;
599	}
600	VFS_UNLOCK_GIANT(vfslocked);
601	if (acct_suspended) {
602		if (sb.f_bavail > (int64_t)(acctresume * sb.f_blocks /
603		    100)) {
604			acct_suspended = 0;
605			log(LOG_NOTICE, "Accounting resumed\n");
606		}
607	} else {
608		if (sb.f_bavail <= (int64_t)(acctsuspend * sb.f_blocks /
609		    100)) {
610			acct_suspended = 1;
611			log(LOG_NOTICE, "Accounting suspended\n");
612		}
613	}
614}
615
616/*
617 * The main loop for the dedicated kernel thread that periodically calls
618 * acctwatch().
619 */
620static void
621acct_thread(void *dummy)
622{
623	u_char pri;
624
625	/* This is a low-priority kernel thread. */
626	pri = PRI_MAX_KERN;
627	thread_lock(curthread);
628	sched_prio(curthread, pri);
629	thread_unlock(curthread);
630
631	/* If another accounting kthread is already running, just die. */
632	sx_xlock(&acct_sx);
633	if (acct_state & ACCT_RUNNING) {
634		sx_xunlock(&acct_sx);
635		kproc_exit(0);
636	}
637	acct_state |= ACCT_RUNNING;
638
639	/* Loop until we are asked to exit. */
640	while (!(acct_state & ACCT_EXITREQ)) {
641
642		/* Perform our periodic checks. */
643		acctwatch();
644
645		/*
646		 * We check this flag again before sleeping since the
647		 * acctwatch() might have shut down accounting and asked us
648		 * to exit.
649		 */
650		if (!(acct_state & ACCT_EXITREQ)) {
651			sx_sleep(&acct_state, &acct_sx, 0, "-",
652			    acctchkfreq * hz);
653		}
654	}
655
656	/*
657	 * Acknowledge the exit request and shutdown.  We clear both the
658	 * exit request and running flags.
659	 */
660	acct_state = 0;
661	sx_xunlock(&acct_sx);
662	kproc_exit(0);
663}
664