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