kern_acct.c revision 194296
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 194296 2009-06-16 15:13:45Z kib $");
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 *);
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
196acct(struct thread *td, struct acct_args *uap)
197{
198	struct nameidata nd;
199	int error, flags, vfslocked;
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	 * If accounting was previously enabled, kill the old space-watcher,
250	 * close the file, and (if no new file was specified, leave).  Reset
251	 * the suspended state regardless of whether accounting remains
252	 * enabled.
253	 */
254	acct_suspended = 0;
255	if (acct_vp != NULL) {
256		vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
257		error = acct_disable(td);
258		VFS_UNLOCK_GIANT(vfslocked);
259	}
260	if (uap->path == NULL) {
261		if (acct_state & ACCT_RUNNING) {
262			acct_state |= ACCT_EXITREQ;
263			wakeup(&acct_state);
264		}
265		sx_xunlock(&acct_sx);
266		return (error);
267	}
268
269	/*
270	 * Save the new accounting file vnode, and schedule the new
271	 * free space watcher.
272	 */
273	acct_vp = nd.ni_vp;
274	acct_cred = crhold(td->td_ucred);
275	acct_flags = flags;
276	if (acct_state & ACCT_RUNNING)
277		acct_state &= ~ACCT_EXITREQ;
278	else {
279		/*
280		 * Try to start up an accounting kthread.  We may start more
281		 * than one, but if so the extras will commit suicide as
282		 * soon as they start up.
283		 */
284		error = kproc_create(acct_thread, NULL, NULL, 0, 0,
285		    "accounting");
286		if (error) {
287			vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
288			(void) vn_close(acct_vp, acct_flags, acct_cred, td);
289			VFS_UNLOCK_GIANT(vfslocked);
290			crfree(acct_cred);
291			acct_configured = 0;
292			acct_vp = NULL;
293			acct_cred = NULL;
294			acct_flags = 0;
295			sx_xunlock(&acct_sx);
296			log(LOG_NOTICE, "Unable to start accounting thread\n");
297			return (error);
298		}
299	}
300	acct_configured = 1;
301	sx_xunlock(&acct_sx);
302	log(LOG_NOTICE, "Accounting enabled\n");
303	return (error);
304}
305
306/*
307 * Disable currently in-progress accounting by closing the vnode, dropping
308 * our reference to the credential, and clearing the vnode's flags.
309 */
310static int
311acct_disable(struct thread *td)
312{
313	int error;
314
315	sx_assert(&acct_sx, SX_XLOCKED);
316	error = vn_close(acct_vp, acct_flags, acct_cred, td);
317	crfree(acct_cred);
318	acct_configured = 0;
319	acct_vp = NULL;
320	acct_cred = NULL;
321	acct_flags = 0;
322	log(LOG_NOTICE, "Accounting disabled\n");
323	return (error);
324}
325
326/*
327 * Write out process accounting information, on process exit.
328 * Data to be written out is specified in Leffler, et al.
329 * and are enumerated below.  (They're also noted in the system
330 * "acct.h" header file.)
331 */
332int
333acct_process(struct thread *td)
334{
335	struct acctv2 acct;
336	struct timeval ut, st, tmp;
337	struct plimit *newlim, *oldlim;
338	struct proc *p;
339	struct rusage ru;
340	int t, ret, vfslocked;
341
342	/*
343	 * Lockless check of accounting condition before doing the hard
344	 * work.
345	 */
346	if (acct_vp == NULL || acct_suspended)
347		return (0);
348
349	sx_slock(&acct_sx);
350
351	/*
352	 * If accounting isn't enabled, don't bother.  Have to check again
353	 * once we own the lock in case we raced with disabling of accounting
354	 * by another thread.
355	 */
356	if (acct_vp == NULL || acct_suspended) {
357		sx_sunlock(&acct_sx);
358		return (0);
359	}
360
361	p = td->td_proc;
362
363	/*
364	 * Get process accounting information.
365	 */
366
367	sx_slock(&proctree_lock);
368	PROC_LOCK(p);
369
370	/* (1) The terminal from which the process was started */
371	if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
372		acct.ac_tty = tty_udev(p->p_pgrp->pg_session->s_ttyp);
373	else
374		acct.ac_tty = NODEV;
375	sx_sunlock(&proctree_lock);
376
377	/* (2) The name of the command that ran */
378	bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
379
380	/* (3) The amount of user and system time that was used */
381	rufetchcalc(p, &ru, &ut, &st);
382	acct.ac_utime = encode_timeval(ut);
383	acct.ac_stime = encode_timeval(st);
384
385	/* (4) The elapsed time the command ran (and its starting time) */
386	tmp = boottime;
387	timevaladd(&tmp, &p->p_stats->p_start);
388	acct.ac_btime = tmp.tv_sec;
389	microuptime(&tmp);
390	timevalsub(&tmp, &p->p_stats->p_start);
391	acct.ac_etime = encode_timeval(tmp);
392
393	/* (5) The average amount of memory used */
394	tmp = ut;
395	timevaladd(&tmp, &st);
396	/* Convert tmp (i.e. u + s) into hz units to match ru_i*. */
397	t = tmp.tv_sec * hz + tmp.tv_usec / tick;
398	if (t)
399		acct.ac_mem = encode_long((ru.ru_ixrss + ru.ru_idrss +
400		    + ru.ru_isrss) / t);
401	else
402		acct.ac_mem = 0;
403
404	/* (6) The number of disk I/O operations done */
405	acct.ac_io = encode_long(ru.ru_inblock + ru.ru_oublock);
406
407	/* (7) The UID and GID of the process */
408	acct.ac_uid = p->p_ucred->cr_ruid;
409	acct.ac_gid = p->p_ucred->cr_rgid;
410
411	/* (8) The boolean flags that tell how the process terminated, etc. */
412	acct.ac_flagx = p->p_acflag;
413	PROC_UNLOCK(p);
414
415	/* Setup ancillary structure fields. */
416	acct.ac_flagx |= ANVER;
417	acct.ac_zero = 0;
418	acct.ac_version = 2;
419	acct.ac_len = acct.ac_len2 = sizeof(acct);
420
421	/*
422	 * Eliminate any file size rlimit.
423	 */
424	newlim = lim_alloc();
425	PROC_LOCK(p);
426	oldlim = p->p_limit;
427	lim_copy(newlim, oldlim);
428	newlim->pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
429	p->p_limit = newlim;
430	PROC_UNLOCK(p);
431	lim_free(oldlim);
432
433	/*
434	 * Write the accounting information to the file.
435	 */
436	vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
437	ret = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct, sizeof (acct),
438	    (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acct_cred, NOCRED,
439	    NULL, td);
440	VFS_UNLOCK_GIANT(vfslocked);
441	sx_sunlock(&acct_sx);
442	return (ret);
443}
444
445/* FLOAT_CONVERSION_START (Regression testing; don't remove this line.) */
446
447/* Convert timevals and longs into IEEE-754 bit patterns. */
448
449/* Mantissa mask (MSB is implied, so subtract 1). */
450#define MANT_MASK ((1 << (FLT_MANT_DIG - 1)) - 1)
451
452/*
453 * We calculate integer values to a precision of approximately
454 * 28 bits.
455 * This is high-enough precision to fill the 24 float bits
456 * and low-enough to avoid overflowing the 32 int bits.
457 */
458#define CALC_BITS 28
459
460/* log_2(1000000). */
461#define LOG2_1M 20
462
463/*
464 * Convert the elements of a timeval into a 32-bit word holding
465 * the bits of a IEEE-754 float.
466 * The float value represents the timeval's value in microsecond units.
467 */
468static uint32_t
469encode_timeval(struct timeval tv)
470{
471	int log2_s;
472	int val, exp;	/* Unnormalized value and exponent */
473	int norm_exp;	/* Normalized exponent */
474	int shift;
475
476	/*
477	 * First calculate value and exponent to about CALC_BITS precision.
478	 * Note that the following conditionals have been ordered so that
479	 * the most common cases appear first.
480	 */
481	if (tv.tv_sec == 0) {
482		if (tv.tv_usec == 0)
483			return (0);
484		exp = 0;
485		val = tv.tv_usec;
486	} else {
487		/*
488		 * Calculate the value to a precision of approximately
489		 * CALC_BITS.
490		 */
491		log2_s = fls(tv.tv_sec) - 1;
492		if (log2_s + LOG2_1M < CALC_BITS) {
493			exp = 0;
494			val = 1000000 * tv.tv_sec + tv.tv_usec;
495		} else {
496			exp = log2_s + LOG2_1M - CALC_BITS;
497			val = (unsigned int)(((u_int64_t)1000000 * tv.tv_sec +
498			    tv.tv_usec) >> exp);
499		}
500	}
501	/* Now normalize and pack the value into an IEEE-754 float. */
502	norm_exp = fls(val) - 1;
503	shift = FLT_MANT_DIG - norm_exp - 1;
504#ifdef ACCT_DEBUG
505	printf("val=%d exp=%d shift=%d log2(val)=%d\n",
506	    val, exp, shift, norm_exp);
507	printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
508	    ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
509#endif
510	return (((FLT_MAX_EXP - 1 + exp + norm_exp) << (FLT_MANT_DIG - 1)) |
511	    ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
512}
513
514/*
515 * Convert a non-negative long value into the bit pattern of
516 * an IEEE-754 float value.
517 */
518static uint32_t
519encode_long(long val)
520{
521	int norm_exp;	/* Normalized exponent */
522	int shift;
523
524	if (val == 0)
525		return (0);
526	if (val < 0) {
527		log(LOG_NOTICE,
528		    "encode_long: negative value %ld in accounting record\n",
529		    val);
530		val = LONG_MAX;
531	}
532	norm_exp = fls(val) - 1;
533	shift = FLT_MANT_DIG - norm_exp - 1;
534#ifdef ACCT_DEBUG
535	printf("val=%d shift=%d log2(val)=%d\n",
536	    val, shift, norm_exp);
537	printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
538	    ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
539#endif
540	return (((FLT_MAX_EXP - 1 + norm_exp) << (FLT_MANT_DIG - 1)) |
541	    ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
542}
543
544/* FLOAT_CONVERSION_END (Regression testing; don't remove this line.) */
545
546/*
547 * Periodically check the filesystem to see if accounting
548 * should be turned on or off.  Beware the case where the vnode
549 * has been vgone()'d out from underneath us, e.g. when the file
550 * system containing the accounting file has been forcibly unmounted.
551 */
552/* ARGSUSED */
553static void
554acctwatch(void)
555{
556	struct statfs sb;
557	int vfslocked;
558
559	sx_assert(&acct_sx, SX_XLOCKED);
560
561	/*
562	 * If accounting was disabled before our kthread was scheduled,
563	 * then acct_vp might be NULL.  If so, just ask our kthread to
564	 * exit and return.
565	 */
566	if (acct_vp == NULL) {
567		acct_state |= ACCT_EXITREQ;
568		return;
569	}
570
571	/*
572	 * If our vnode is no longer valid, tear it down and signal the
573	 * accounting thread to die.
574	 */
575	vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
576	if (acct_vp->v_type == VBAD) {
577		(void) acct_disable(NULL);
578		VFS_UNLOCK_GIANT(vfslocked);
579		acct_state |= ACCT_EXITREQ;
580		return;
581	}
582
583	/*
584	 * Stopping here is better than continuing, maybe it will be VBAD
585	 * next time around.
586	 */
587	if (VFS_STATFS(acct_vp->v_mount, &sb) < 0) {
588		VFS_UNLOCK_GIANT(vfslocked);
589		return;
590	}
591	VFS_UNLOCK_GIANT(vfslocked);
592	if (acct_suspended) {
593		if (sb.f_bavail > (int64_t)(acctresume * sb.f_blocks /
594		    100)) {
595			acct_suspended = 0;
596			log(LOG_NOTICE, "Accounting resumed\n");
597		}
598	} else {
599		if (sb.f_bavail <= (int64_t)(acctsuspend * sb.f_blocks /
600		    100)) {
601			acct_suspended = 1;
602			log(LOG_NOTICE, "Accounting suspended\n");
603		}
604	}
605}
606
607/*
608 * The main loop for the dedicated kernel thread that periodically calls
609 * acctwatch().
610 */
611static void
612acct_thread(void *dummy)
613{
614	u_char pri;
615
616	/* This is a low-priority kernel thread. */
617	pri = PRI_MAX_KERN;
618	thread_lock(curthread);
619	sched_prio(curthread, pri);
620	thread_unlock(curthread);
621
622	/* If another accounting kthread is already running, just die. */
623	sx_xlock(&acct_sx);
624	if (acct_state & ACCT_RUNNING) {
625		sx_xunlock(&acct_sx);
626		kproc_exit(0);
627	}
628	acct_state |= ACCT_RUNNING;
629
630	/* Loop until we are asked to exit. */
631	while (!(acct_state & ACCT_EXITREQ)) {
632
633		/* Perform our periodic checks. */
634		acctwatch();
635
636		/*
637		 * We check this flag again before sleeping since the
638		 * acctwatch() might have shut down accounting and asked us
639		 * to exit.
640		 */
641		if (!(acct_state & ACCT_EXITREQ)) {
642			sx_sleep(&acct_state, &acct_sx, 0, "-",
643			    acctchkfreq * hz);
644		}
645	}
646
647	/*
648	 * Acknowledge the exit request and shutdown.  We clear both the
649	 * exit request and running flags.
650	 */
651	acct_state = 0;
652	sx_xunlock(&acct_sx);
653	kproc_exit(0);
654}
655