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