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