xref: /freebsd/sys/kern/kern_acct.c (revision aa772005)
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$");
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  */
121 static uint32_t	encode_timeval(struct timeval);
122 static uint32_t	encode_long(long);
123 static void	acctwatch(void);
124 static void	acct_thread(void *);
125 static 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  */
132 static int		 acct_configured;
133 static int		 acct_suspended;
134 static struct vnode	*acct_vp;
135 static struct ucred	*acct_cred;
136 static int		 acct_flags;
137 static struct sx	 acct_sx;
138 
139 SX_SYSINIT(acct, &acct_sx, "acct_sx");
140 
141 /*
142  * State of the accounting kthread.
143  */
144 static 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  */
152 static int acctsuspend = 2;	/* stop accounting when < 2% free space left */
153 SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW,
154 	&acctsuspend, 0, "percentage of free disk space below which accounting stops");
155 
156 static int acctresume = 4;	/* resume when free space risen to > 4% */
157 SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW,
158 	&acctresume, 0, "percentage of free disk space above which accounting resumes");
159 
160 static int acctchkfreq = 15;	/* frequency (in seconds) to check space */
161 
162 static int
163 sysctl_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 }
181 SYSCTL_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 
185 SYSCTL_INT(_kern, OID_AUTO, acct_configured, CTLFLAG_RD, &acct_configured, 0,
186 	"Accounting configured or not");
187 
188 SYSCTL_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  */
195 int
196 sys_acct(struct thread *td, struct acct_args *uap)
197 {
198 	struct nameidata nd;
199 	int error, flags, 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 | 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 		NDFREE(&nd, NDF_ONLY_PNBUF);
217 #ifdef MAC
218 		error = mac_system_check_acct(td->td_ucred, nd.ni_vp);
219 		if (error) {
220 			VOP_UNLOCK(nd.ni_vp, 0);
221 			vn_close(nd.ni_vp, flags, td->td_ucred, td);
222 			return (error);
223 		}
224 #endif
225 		VOP_UNLOCK(nd.ni_vp, 0);
226 		if (nd.ni_vp->v_type != VREG) {
227 			vn_close(nd.ni_vp, flags, td->td_ucred, td);
228 			return (EACCES);
229 		}
230 #ifdef MAC
231 	} else {
232 		error = mac_system_check_acct(td->td_ucred, NULL);
233 		if (error)
234 			return (error);
235 #endif
236 	}
237 
238 	/*
239 	 * Disallow concurrent access to the accounting vnode while we swap
240 	 * it out, in order to prevent access after close.
241 	 */
242 	sx_xlock(&acct_sx);
243 
244 	/*
245 	 * Don't log spurious disable/enable messages if we are
246 	 * switching from one accounting file to another due to log
247 	 * rotation.
248 	 */
249 	replacing = (acct_vp != NULL && uap->path != NULL);
250 
251 	/*
252 	 * If accounting was previously enabled, kill the old space-watcher,
253 	 * close the file, and (if no new file was specified, leave).  Reset
254 	 * the suspended state regardless of whether accounting remains
255 	 * enabled.
256 	 */
257 	acct_suspended = 0;
258 	if (acct_vp != NULL)
259 		error = acct_disable(td, !replacing);
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 			(void) vn_close(acct_vp, acct_flags, acct_cred, td);
288 			crfree(acct_cred);
289 			acct_configured = 0;
290 			acct_vp = NULL;
291 			acct_cred = NULL;
292 			acct_flags = 0;
293 			sx_xunlock(&acct_sx);
294 			log(LOG_NOTICE, "Unable to start accounting thread\n");
295 			return (error);
296 		}
297 	}
298 	acct_configured = 1;
299 	sx_xunlock(&acct_sx);
300 	if (!replacing)
301 		log(LOG_NOTICE, "Accounting enabled\n");
302 	return (error);
303 }
304 
305 /*
306  * Disable currently in-progress accounting by closing the vnode, dropping
307  * our reference to the credential, and clearing the vnode's flags.
308  */
309 static int
310 acct_disable(struct thread *td, int logging)
311 {
312 	int error;
313 
314 	sx_assert(&acct_sx, SX_XLOCKED);
315 	error = vn_close(acct_vp, acct_flags, acct_cred, td);
316 	crfree(acct_cred);
317 	acct_configured = 0;
318 	acct_vp = NULL;
319 	acct_cred = NULL;
320 	acct_flags = 0;
321 	if (logging)
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  */
332 int
333 acct_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;
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 	ret = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct, sizeof (acct),
437 	    (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acct_cred, NOCRED,
438 	    NULL, td);
439 	sx_sunlock(&acct_sx);
440 	return (ret);
441 }
442 
443 /* FLOAT_CONVERSION_START (Regression testing; don't remove this line.) */
444 
445 /* Convert timevals and longs into IEEE-754 bit patterns. */
446 
447 /* Mantissa mask (MSB is implied, so subtract 1). */
448 #define MANT_MASK ((1 << (FLT_MANT_DIG - 1)) - 1)
449 
450 /*
451  * We calculate integer values to a precision of approximately
452  * 28 bits.
453  * This is high-enough precision to fill the 24 float bits
454  * and low-enough to avoid overflowing the 32 int bits.
455  */
456 #define CALC_BITS 28
457 
458 /* log_2(1000000). */
459 #define LOG2_1M 20
460 
461 /*
462  * Convert the elements of a timeval into a 32-bit word holding
463  * the bits of a IEEE-754 float.
464  * The float value represents the timeval's value in microsecond units.
465  */
466 static uint32_t
467 encode_timeval(struct timeval tv)
468 {
469 	int log2_s;
470 	int val, exp;	/* Unnormalized value and exponent */
471 	int norm_exp;	/* Normalized exponent */
472 	int shift;
473 
474 	/*
475 	 * First calculate value and exponent to about CALC_BITS precision.
476 	 * Note that the following conditionals have been ordered so that
477 	 * the most common cases appear first.
478 	 */
479 	if (tv.tv_sec == 0) {
480 		if (tv.tv_usec == 0)
481 			return (0);
482 		exp = 0;
483 		val = tv.tv_usec;
484 	} else {
485 		/*
486 		 * Calculate the value to a precision of approximately
487 		 * CALC_BITS.
488 		 */
489 		log2_s = fls(tv.tv_sec) - 1;
490 		if (log2_s + LOG2_1M < CALC_BITS) {
491 			exp = 0;
492 			val = 1000000 * tv.tv_sec + tv.tv_usec;
493 		} else {
494 			exp = log2_s + LOG2_1M - CALC_BITS;
495 			val = (unsigned int)(((uint64_t)1000000 * tv.tv_sec +
496 			    tv.tv_usec) >> exp);
497 		}
498 	}
499 	/* Now normalize and pack the value into an IEEE-754 float. */
500 	norm_exp = fls(val) - 1;
501 	shift = FLT_MANT_DIG - norm_exp - 1;
502 #ifdef ACCT_DEBUG
503 	printf("val=%d exp=%d shift=%d log2(val)=%d\n",
504 	    val, exp, shift, norm_exp);
505 	printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
506 	    ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
507 #endif
508 	return (((FLT_MAX_EXP - 1 + exp + norm_exp) << (FLT_MANT_DIG - 1)) |
509 	    ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
510 }
511 
512 /*
513  * Convert a non-negative long value into the bit pattern of
514  * an IEEE-754 float value.
515  */
516 static uint32_t
517 encode_long(long val)
518 {
519 	int norm_exp;	/* Normalized exponent */
520 	int shift;
521 
522 	if (val == 0)
523 		return (0);
524 	if (val < 0) {
525 		log(LOG_NOTICE,
526 		    "encode_long: negative value %ld in accounting record\n",
527 		    val);
528 		val = LONG_MAX;
529 	}
530 	norm_exp = fls(val) - 1;
531 	shift = FLT_MANT_DIG - norm_exp - 1;
532 #ifdef ACCT_DEBUG
533 	printf("val=%d shift=%d log2(val)=%d\n",
534 	    val, shift, norm_exp);
535 	printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
536 	    ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
537 #endif
538 	return (((FLT_MAX_EXP - 1 + norm_exp) << (FLT_MANT_DIG - 1)) |
539 	    ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
540 }
541 
542 /* FLOAT_CONVERSION_END (Regression testing; don't remove this line.) */
543 
544 /*
545  * Periodically check the filesystem to see if accounting
546  * should be turned on or off.  Beware the case where the vnode
547  * has been vgone()'d out from underneath us, e.g. when the file
548  * system containing the accounting file has been forcibly unmounted.
549  */
550 /* ARGSUSED */
551 static void
552 acctwatch(void)
553 {
554 	struct statfs sb;
555 
556 	sx_assert(&acct_sx, SX_XLOCKED);
557 
558 	/*
559 	 * If accounting was disabled before our kthread was scheduled,
560 	 * then acct_vp might be NULL.  If so, just ask our kthread to
561 	 * exit and return.
562 	 */
563 	if (acct_vp == NULL) {
564 		acct_state |= ACCT_EXITREQ;
565 		return;
566 	}
567 
568 	/*
569 	 * If our vnode is no longer valid, tear it down and signal the
570 	 * accounting thread to die.
571 	 */
572 	if (acct_vp->v_type == VBAD) {
573 		(void) acct_disable(NULL, 1);
574 		acct_state |= ACCT_EXITREQ;
575 		return;
576 	}
577 
578 	/*
579 	 * Stopping here is better than continuing, maybe it will be VBAD
580 	 * next time around.
581 	 */
582 	if (VFS_STATFS(acct_vp->v_mount, &sb) < 0)
583 		return;
584 	if (acct_suspended) {
585 		if (sb.f_bavail > (int64_t)(acctresume * sb.f_blocks /
586 		    100)) {
587 			acct_suspended = 0;
588 			log(LOG_NOTICE, "Accounting resumed\n");
589 		}
590 	} else {
591 		if (sb.f_bavail <= (int64_t)(acctsuspend * sb.f_blocks /
592 		    100)) {
593 			acct_suspended = 1;
594 			log(LOG_NOTICE, "Accounting suspended\n");
595 		}
596 	}
597 }
598 
599 /*
600  * The main loop for the dedicated kernel thread that periodically calls
601  * acctwatch().
602  */
603 static void
604 acct_thread(void *dummy)
605 {
606 	u_char pri;
607 
608 	/* This is a low-priority kernel thread. */
609 	pri = PRI_MAX_KERN;
610 	thread_lock(curthread);
611 	sched_prio(curthread, pri);
612 	thread_unlock(curthread);
613 
614 	/* If another accounting kthread is already running, just die. */
615 	sx_xlock(&acct_sx);
616 	if (acct_state & ACCT_RUNNING) {
617 		sx_xunlock(&acct_sx);
618 		kproc_exit(0);
619 	}
620 	acct_state |= ACCT_RUNNING;
621 
622 	/* Loop until we are asked to exit. */
623 	while (!(acct_state & ACCT_EXITREQ)) {
624 
625 		/* Perform our periodic checks. */
626 		acctwatch();
627 
628 		/*
629 		 * We check this flag again before sleeping since the
630 		 * acctwatch() might have shut down accounting and asked us
631 		 * to exit.
632 		 */
633 		if (!(acct_state & ACCT_EXITREQ)) {
634 			sx_sleep(&acct_state, &acct_sx, 0, "-",
635 			    acctchkfreq * hz);
636 		}
637 	}
638 
639 	/*
640 	 * Acknowledge the exit request and shutdown.  We clear both the
641 	 * exit request and running flags.
642 	 */
643 	acct_state = 0;
644 	sx_xunlock(&acct_sx);
645 	kproc_exit(0);
646 }
647