xref: /freebsd/sys/kern/kern_acct.c (revision ee28ad11b7635715ed7d2f5815ace34c433992d1)
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  *	@(#)kern_acct.c	8.1 (Berkeley) 6/14/93
70  */
71 
72 #include <sys/cdefs.h>
73 __FBSDID("$FreeBSD$");
74 
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/acct.h>
78 #include <sys/fcntl.h>
79 #include <sys/kernel.h>
80 #include <sys/kthread.h>
81 #include <sys/limits.h>
82 #include <sys/lock.h>
83 #include <sys/malloc.h>
84 #include <sys/mount.h>
85 #include <sys/mutex.h>
86 #include <sys/namei.h>
87 #include <sys/priv.h>
88 #include <sys/proc.h>
89 #include <sys/resourcevar.h>
90 #include <sys/sched.h>
91 #include <sys/sx.h>
92 #include <sys/sysctl.h>
93 #include <sys/sysent.h>
94 #include <sys/syslog.h>
95 #include <sys/sysproto.h>
96 #include <sys/tty.h>
97 #include <sys/vnode.h>
98 
99 #include <security/mac/mac_framework.h>
100 
101 _Static_assert(sizeof(struct acctv3) - offsetof(struct acctv3, ac_trailer) ==
102     sizeof(struct acctv2) - offsetof(struct acctv2, ac_trailer), "trailer");
103 _Static_assert(sizeof(struct acctv3) - offsetof(struct acctv3, ac_len2) ==
104     sizeof(struct acctv2) - offsetof(struct acctv2, ac_len2), "len2");
105 
106 /*
107  * The routines implemented in this file are described in:
108  *      Leffler, et al.: The Design and Implementation of the 4.3BSD
109  *	    UNIX Operating System (Addison Welley, 1989)
110  * on pages 62-63.
111  * On May 2007 the historic 3 bits base 8 exponent, 13 bit fraction
112  * compt_t representation described in the above reference was replaced
113  * with that of IEEE-754 floats.
114  *
115  * Arguably, to simplify accounting operations, this mechanism should
116  * be replaced by one in which an accounting log file (similar to /dev/klog)
117  * is read by a user process, etc.  However, that has its own problems.
118  */
119 
120 /* Floating point definitions from <float.h>. */
121 #define FLT_MANT_DIG    24              /* p */
122 #define FLT_MAX_EXP     128             /* emax */
123 
124 /*
125  * Internal accounting functions.
126  * The former's operation is described in Leffler, et al., and the latter
127  * was provided by UCB with the 4.4BSD-Lite release
128  */
129 static uint32_t	encode_timeval(struct timeval);
130 static uint32_t	encode_long(long);
131 static void	acctwatch(void);
132 static void	acct_thread(void *);
133 static int	acct_disable(struct thread *, int);
134 
135 /*
136  * Accounting vnode pointer, saved vnode pointer, and flags for each.
137  * acct_sx protects against changes to the active vnode and credentials
138  * while accounting records are being committed to disk.
139  */
140 static int		 acct_configured;
141 static int		 acct_suspended;
142 static struct vnode	*acct_vp;
143 static struct ucred	*acct_cred;
144 static int		 acct_flags;
145 static struct sx	 acct_sx;
146 
147 SX_SYSINIT(acct, &acct_sx, "acct_sx");
148 
149 /*
150  * State of the accounting kthread.
151  */
152 static int		 acct_state;
153 
154 #define	ACCT_RUNNING	1	/* Accounting kthread is running. */
155 #define	ACCT_EXITREQ	2	/* Accounting kthread should exit. */
156 
157 /*
158  * Values associated with enabling and disabling accounting
159  */
160 static int acctsuspend = 2;	/* stop accounting when < 2% free space left */
161 SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW,
162 	&acctsuspend, 0, "percentage of free disk space below which accounting stops");
163 
164 static int acctresume = 4;	/* resume when free space risen to > 4% */
165 SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW,
166 	&acctresume, 0, "percentage of free disk space above which accounting resumes");
167 
168 static int acctchkfreq = 15;	/* frequency (in seconds) to check space */
169 
170 static int
171 sysctl_acct_chkfreq(SYSCTL_HANDLER_ARGS)
172 {
173 	int error, value;
174 
175 	/* Write out the old value. */
176 	error = SYSCTL_OUT(req, &acctchkfreq, sizeof(int));
177 	if (error || req->newptr == NULL)
178 		return (error);
179 
180 	/* Read in and verify the new value. */
181 	error = SYSCTL_IN(req, &value, sizeof(int));
182 	if (error)
183 		return (error);
184 	if (value <= 0)
185 		return (EINVAL);
186 	acctchkfreq = value;
187 	return (0);
188 }
189 SYSCTL_PROC(_kern, OID_AUTO, acct_chkfreq,
190     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, &acctchkfreq, 0,
191     sysctl_acct_chkfreq, "I",
192     "frequency for checking the free space");
193 
194 SYSCTL_INT(_kern, OID_AUTO, acct_configured, CTLFLAG_RD, &acct_configured, 0,
195 	"Accounting configured or not");
196 
197 SYSCTL_INT(_kern, OID_AUTO, acct_suspended, CTLFLAG_RD, &acct_suspended, 0,
198 	"Accounting suspended or not");
199 
200 /*
201  * Accounting system call.  Written based on the specification and previous
202  * implementation done by Mark Tinguely.
203  */
204 int
205 sys_acct(struct thread *td, struct acct_args *uap)
206 {
207 	struct nameidata nd;
208 	int error, flags, replacing;
209 
210 	error = priv_check(td, PRIV_ACCT);
211 	if (error)
212 		return (error);
213 
214 	/*
215 	 * If accounting is to be started to a file, open that file for
216 	 * appending and make sure it's a 'normal'.
217 	 */
218 	if (uap->path != NULL) {
219 		NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, UIO_USERSPACE,
220 		    uap->path);
221 		flags = FWRITE | O_APPEND;
222 		error = vn_open(&nd, &flags, 0, NULL);
223 		if (error)
224 			return (error);
225 		NDFREE_PNBUF(&nd);
226 #ifdef MAC
227 		error = mac_system_check_acct(td->td_ucred, nd.ni_vp);
228 		if (error) {
229 			VOP_UNLOCK(nd.ni_vp);
230 			vn_close(nd.ni_vp, flags, td->td_ucred, td);
231 			return (error);
232 		}
233 #endif
234 		VOP_UNLOCK(nd.ni_vp);
235 		if (nd.ni_vp->v_type != VREG) {
236 			vn_close(nd.ni_vp, flags, td->td_ucred, td);
237 			return (EACCES);
238 		}
239 #ifdef MAC
240 	} else {
241 		error = mac_system_check_acct(td->td_ucred, NULL);
242 		if (error)
243 			return (error);
244 #endif
245 	}
246 
247 	/*
248 	 * Disallow concurrent access to the accounting vnode while we swap
249 	 * it out, in order to prevent access after close.
250 	 */
251 	sx_xlock(&acct_sx);
252 
253 	/*
254 	 * Don't log spurious disable/enable messages if we are
255 	 * switching from one accounting file to another due to log
256 	 * rotation.
257 	 */
258 	replacing = (acct_vp != NULL && uap->path != NULL);
259 
260 	/*
261 	 * If accounting was previously enabled, kill the old space-watcher,
262 	 * close the file, and (if no new file was specified, leave).  Reset
263 	 * the suspended state regardless of whether accounting remains
264 	 * enabled.
265 	 */
266 	acct_suspended = 0;
267 	if (acct_vp != NULL)
268 		error = acct_disable(td, !replacing);
269 	if (uap->path == NULL) {
270 		if (acct_state & ACCT_RUNNING) {
271 			acct_state |= ACCT_EXITREQ;
272 			wakeup(&acct_state);
273 		}
274 		sx_xunlock(&acct_sx);
275 		return (error);
276 	}
277 
278 	/*
279 	 * Save the new accounting file vnode, and schedule the new
280 	 * free space watcher.
281 	 */
282 	acct_vp = nd.ni_vp;
283 	acct_cred = crhold(td->td_ucred);
284 	acct_flags = flags;
285 	if (acct_state & ACCT_RUNNING)
286 		acct_state &= ~ACCT_EXITREQ;
287 	else {
288 		/*
289 		 * Try to start up an accounting kthread.  We may start more
290 		 * than one, but if so the extras will commit suicide as
291 		 * soon as they start up.
292 		 */
293 		error = kproc_create(acct_thread, NULL, NULL, 0, 0,
294 		    "accounting");
295 		if (error) {
296 			(void) acct_disable(td, 0);
297 			sx_xunlock(&acct_sx);
298 			log(LOG_NOTICE, "Unable to start accounting thread\n");
299 			return (error);
300 		}
301 	}
302 	acct_configured = 1;
303 	sx_xunlock(&acct_sx);
304 	if (!replacing)
305 		log(LOG_NOTICE, "Accounting enabled\n");
306 	return (error);
307 }
308 
309 /*
310  * Disable currently in-progress accounting by closing the vnode, dropping
311  * our reference to the credential, and clearing the vnode's flags.
312  */
313 static int
314 acct_disable(struct thread *td, int logging)
315 {
316 	int error;
317 
318 	sx_assert(&acct_sx, SX_XLOCKED);
319 	error = vn_close(acct_vp, acct_flags, acct_cred, td);
320 	crfree(acct_cred);
321 	acct_configured = 0;
322 	acct_vp = NULL;
323 	acct_cred = NULL;
324 	acct_flags = 0;
325 	if (logging)
326 		log(LOG_NOTICE, "Accounting disabled\n");
327 	return (error);
328 }
329 
330 /*
331  * Write out process accounting information, on process exit.
332  * Data to be written out is specified in Leffler, et al.
333  * and are enumerated below.  (They're also noted in the system
334  * "acct.h" header file.)
335  */
336 int
337 acct_process(struct thread *td)
338 {
339 	struct acctv3 acct;
340 	struct timeval ut, st, tmp;
341 	struct proc *p;
342 	struct rusage ru;
343 	int t, ret;
344 
345 	/*
346 	 * Lockless check of accounting condition before doing the hard
347 	 * work.
348 	 */
349 	if (acct_vp == NULL || acct_suspended)
350 		return (0);
351 
352 	memset(&acct, 0, sizeof(acct));
353 
354 	sx_slock(&acct_sx);
355 
356 	/*
357 	 * If accounting isn't enabled, don't bother.  Have to check again
358 	 * once we own the lock in case we raced with disabling of accounting
359 	 * by another thread.
360 	 */
361 	if (acct_vp == NULL || acct_suspended) {
362 		sx_sunlock(&acct_sx);
363 		return (0);
364 	}
365 
366 	p = td->td_proc;
367 	td->td_pflags2 |= TDP2_ACCT;
368 
369 	/*
370 	 * Get process accounting information.
371 	 */
372 
373 	sx_slock(&proctree_lock);
374 	PROC_LOCK(p);
375 
376 	/* (1) The terminal from which the process was started */
377 	if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
378 		acct.ac_tty = tty_udev(p->p_pgrp->pg_session->s_ttyp);
379 	else
380 		acct.ac_tty = NODEV;
381 	sx_sunlock(&proctree_lock);
382 
383 	/* (2) The name of the command that ran */
384 	bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
385 
386 	/* (3) The amount of user and system time that was used */
387 	rufetchcalc(p, &ru, &ut, &st);
388 	acct.ac_utime = encode_timeval(ut);
389 	acct.ac_stime = encode_timeval(st);
390 
391 	/* (4) The elapsed time the command ran (and its starting time) */
392 	getboottime(&tmp);
393 	timevaladd(&tmp, &p->p_stats->p_start);
394 	acct.ac_btime = tmp.tv_sec;
395 	microuptime(&tmp);
396 	timevalsub(&tmp, &p->p_stats->p_start);
397 	acct.ac_etime = encode_timeval(tmp);
398 
399 	/* (5) The average amount of memory used */
400 	tmp = ut;
401 	timevaladd(&tmp, &st);
402 	/* Convert tmp (i.e. u + s) into hz units to match ru_i*. */
403 	t = tmp.tv_sec * hz + tmp.tv_usec / tick;
404 	if (t)
405 		acct.ac_mem = encode_long((ru.ru_ixrss + ru.ru_idrss +
406 		    + ru.ru_isrss) / t);
407 	else
408 		acct.ac_mem = 0;
409 
410 	/* (6) The number of disk I/O operations done */
411 	acct.ac_io = encode_long(ru.ru_inblock + ru.ru_oublock);
412 
413 	/* (7) The UID and GID of the process */
414 	acct.ac_uid = p->p_ucred->cr_ruid;
415 	acct.ac_gid = p->p_ucred->cr_rgid;
416 
417 	/* (8) The boolean flags that tell how the process terminated, etc. */
418 	acct.ac_flagx = p->p_acflag;
419 
420 	PROC_UNLOCK(p);
421 
422 	/* Setup ancillary structure fields. */
423 	acct.ac_flagx |= ANVER;
424 	acct.ac_zero = 0;
425 	acct.ac_version = 3;
426 	acct.ac_len = acct.ac_len2 = sizeof(acct);
427 
428 	/*
429 	 * Write the accounting information to the file.
430 	 */
431 	ret = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct, sizeof (acct),
432 	    (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acct_cred, NOCRED,
433 	    NULL, td);
434 	sx_sunlock(&acct_sx);
435 	td->td_pflags2 &= ~TDP2_ACCT;
436 	return (ret);
437 }
438 
439 /* FLOAT_CONVERSION_START (Regression testing; don't remove this line.) */
440 
441 /* Convert timevals and longs into IEEE-754 bit patterns. */
442 
443 /* Mantissa mask (MSB is implied, so subtract 1). */
444 #define MANT_MASK ((1 << (FLT_MANT_DIG - 1)) - 1)
445 
446 /*
447  * We calculate integer values to a precision of approximately
448  * 28 bits.
449  * This is high-enough precision to fill the 24 float bits
450  * and low-enough to avoid overflowing the 32 int bits.
451  */
452 #define CALC_BITS 28
453 
454 /* log_2(1000000). */
455 #define LOG2_1M 20
456 
457 /*
458  * Convert the elements of a timeval into a 32-bit word holding
459  * the bits of a IEEE-754 float.
460  * The float value represents the timeval's value in microsecond units.
461  */
462 static uint32_t
463 encode_timeval(struct timeval tv)
464 {
465 	int log2_s;
466 	int val, exp;	/* Unnormalized value and exponent */
467 	int norm_exp;	/* Normalized exponent */
468 	int shift;
469 
470 	/*
471 	 * First calculate value and exponent to about CALC_BITS precision.
472 	 * Note that the following conditionals have been ordered so that
473 	 * the most common cases appear first.
474 	 */
475 	if (tv.tv_sec == 0) {
476 		if (tv.tv_usec == 0)
477 			return (0);
478 		exp = 0;
479 		val = tv.tv_usec;
480 	} else {
481 		/*
482 		 * Calculate the value to a precision of approximately
483 		 * CALC_BITS.
484 		 */
485 		log2_s = fls(tv.tv_sec) - 1;
486 		if (log2_s + LOG2_1M < CALC_BITS) {
487 			exp = 0;
488 			val = 1000000 * tv.tv_sec + tv.tv_usec;
489 		} else {
490 			exp = log2_s + LOG2_1M - CALC_BITS;
491 			val = (unsigned int)(((uint64_t)1000000 * tv.tv_sec +
492 			    tv.tv_usec) >> exp);
493 		}
494 	}
495 	/* Now normalize and pack the value into an IEEE-754 float. */
496 	norm_exp = fls(val) - 1;
497 	shift = FLT_MANT_DIG - norm_exp - 1;
498 #ifdef ACCT_DEBUG
499 	printf("val=%d exp=%d shift=%d log2(val)=%d\n",
500 	    val, exp, shift, norm_exp);
501 	printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
502 	    ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
503 #endif
504 	return (((FLT_MAX_EXP - 1 + exp + norm_exp) << (FLT_MANT_DIG - 1)) |
505 	    ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
506 }
507 
508 /*
509  * Convert a non-negative long value into the bit pattern of
510  * an IEEE-754 float value.
511  */
512 static uint32_t
513 encode_long(long val)
514 {
515 	int norm_exp;	/* Normalized exponent */
516 	int shift;
517 
518 	if (val == 0)
519 		return (0);
520 	if (val < 0) {
521 		log(LOG_NOTICE,
522 		    "encode_long: negative value %ld in accounting record\n",
523 		    val);
524 		val = LONG_MAX;
525 	}
526 	norm_exp = fls(val) - 1;
527 	shift = FLT_MANT_DIG - norm_exp - 1;
528 #ifdef ACCT_DEBUG
529 	printf("val=%d shift=%d log2(val)=%d\n",
530 	    val, shift, norm_exp);
531 	printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
532 	    ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
533 #endif
534 	return (((FLT_MAX_EXP - 1 + norm_exp) << (FLT_MANT_DIG - 1)) |
535 	    ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
536 }
537 
538 /* FLOAT_CONVERSION_END (Regression testing; don't remove this line.) */
539 
540 /*
541  * Periodically check the filesystem to see if accounting
542  * should be turned on or off.  Beware the case where the vnode
543  * has been vgone()'d out from underneath us, e.g. when the file
544  * system containing the accounting file has been forcibly unmounted.
545  */
546 /* ARGSUSED */
547 static void
548 acctwatch(void)
549 {
550 	struct statfs *sp;
551 
552 	sx_assert(&acct_sx, SX_XLOCKED);
553 
554 	/*
555 	 * If accounting was disabled before our kthread was scheduled,
556 	 * then acct_vp might be NULL.  If so, just ask our kthread to
557 	 * exit and return.
558 	 */
559 	if (acct_vp == NULL) {
560 		acct_state |= ACCT_EXITREQ;
561 		return;
562 	}
563 
564 	/*
565 	 * If our vnode is no longer valid, tear it down and signal the
566 	 * accounting thread to die.
567 	 */
568 	if (acct_vp->v_type == VBAD) {
569 		(void) acct_disable(NULL, 1);
570 		acct_state |= ACCT_EXITREQ;
571 		return;
572 	}
573 
574 	/*
575 	 * Stopping here is better than continuing, maybe it will be VBAD
576 	 * next time around.
577 	 */
578 	sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
579 	if (VFS_STATFS(acct_vp->v_mount, sp) < 0) {
580 		free(sp, M_STATFS);
581 		return;
582 	}
583 	if (acct_suspended) {
584 		if (sp->f_bavail > (int64_t)(acctresume * sp->f_blocks /
585 		    100)) {
586 			acct_suspended = 0;
587 			log(LOG_NOTICE, "Accounting resumed\n");
588 		}
589 	} else {
590 		if (sp->f_bavail <= (int64_t)(acctsuspend * sp->f_blocks /
591 		    100)) {
592 			acct_suspended = 1;
593 			log(LOG_NOTICE, "Accounting suspended\n");
594 		}
595 	}
596 	free(sp, M_STATFS);
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 		/* Perform our periodic checks. */
625 		acctwatch();
626 
627 		/*
628 		 * We check this flag again before sleeping since the
629 		 * acctwatch() might have shut down accounting and asked us
630 		 * to exit.
631 		 */
632 		if (!(acct_state & ACCT_EXITREQ)) {
633 			sx_sleep(&acct_state, &acct_sx, 0, "-",
634 			    acctchkfreq * hz);
635 		}
636 	}
637 
638 	/*
639 	 * Acknowledge the exit request and shutdown.  We clear both the
640 	 * exit request and running flags.
641 	 */
642 	acct_state = 0;
643 	sx_xunlock(&acct_sx);
644 	kproc_exit(0);
645 }
646