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