1df8bae1dSRodney W. Grimes /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 1071909edeSRobert Watson * Copyright (c) 1994 Christopher G. Demetriou 1171909edeSRobert Watson * Copyright (c) 2005 Robert N. M. Watson 1271909edeSRobert Watson * 13df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 14df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 15df8bae1dSRodney W. Grimes * are met: 16df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 17df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 18df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 19df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 20df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 21df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 22df8bae1dSRodney W. Grimes * must display the following acknowledgement: 23df8bae1dSRodney W. Grimes * This product includes software developed by the University of 24df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 25df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 26df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 27df8bae1dSRodney W. Grimes * without specific prior written permission. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39df8bae1dSRodney W. Grimes * SUCH DAMAGE. 40df8bae1dSRodney W. Grimes * 41c7d893deSDavid Greenman * @(#)kern_acct.c 8.1 (Berkeley) 6/14/93 42df8bae1dSRodney W. Grimes */ 43df8bae1dSRodney W. Grimes 44677b542eSDavid E. O'Brien #include <sys/cdefs.h> 45677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 46677b542eSDavid E. O'Brien 47e5e820fdSRobert Watson #include "opt_mac.h" 48e5e820fdSRobert Watson 49df8bae1dSRodney W. Grimes #include <sys/param.h> 500ad076d5SBruce Evans #include <sys/systm.h> 51b0864d13SJohn Baldwin #include <sys/acct.h> 52b0864d13SJohn Baldwin #include <sys/fcntl.h> 53b0864d13SJohn Baldwin #include <sys/kernel.h> 54505a1493SJohn Baldwin #include <sys/kthread.h> 55fb919e4dSMark Murray #include <sys/lock.h> 56e5e820fdSRobert Watson #include <sys/mac.h> 57df8bae1dSRodney W. Grimes #include <sys/mount.h> 58b0864d13SJohn Baldwin #include <sys/mutex.h> 59c7d893deSDavid Greenman #include <sys/namei.h> 60b0864d13SJohn Baldwin #include <sys/proc.h> 61c7d893deSDavid Greenman #include <sys/resourcevar.h> 62505a1493SJohn Baldwin #include <sys/sched.h> 63b0864d13SJohn Baldwin #include <sys/sx.h> 64b0864d13SJohn Baldwin #include <sys/sysctl.h> 65b0864d13SJohn Baldwin #include <sys/sysent.h> 66b0864d13SJohn Baldwin #include <sys/syslog.h> 67b0864d13SJohn Baldwin #include <sys/sysproto.h> 68c7d893deSDavid Greenman #include <sys/tty.h> 69b0864d13SJohn Baldwin #include <sys/vnode.h> 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes /* 72c7d893deSDavid Greenman * The routines implemented in this file are described in: 73c7d893deSDavid Greenman * Leffler, et al.: The Design and Implementation of the 4.3BSD 74c7d893deSDavid Greenman * UNIX Operating System (Addison Welley, 1989) 75c7d893deSDavid Greenman * on pages 62-63. 76c7d893deSDavid Greenman * 77c7d893deSDavid Greenman * Arguably, to simplify accounting operations, this mechanism should 78c7d893deSDavid Greenman * be replaced by one in which an accounting log file (similar to /dev/klog) 79c7d893deSDavid Greenman * is read by a user process, etc. However, that has its own problems. 80df8bae1dSRodney W. Grimes */ 81df8bae1dSRodney W. Grimes 82df8bae1dSRodney W. Grimes /* 83c7d893deSDavid Greenman * Internal accounting functions. 84c7d893deSDavid Greenman * The former's operation is described in Leffler, et al., and the latter 85c7d893deSDavid Greenman * was provided by UCB with the 4.4BSD-Lite release 86df8bae1dSRodney W. Grimes */ 874d77a549SAlfred Perlstein static comp_t encode_comp_t(u_long, u_long); 88505a1493SJohn Baldwin static void acctwatch(void); 89505a1493SJohn Baldwin static void acct_thread(void *); 90505a1493SJohn Baldwin static int acct_disable(struct thread *); 91ab36c067SJustin T. Gibbs 92ab36c067SJustin T. Gibbs /* 935b606744SJohan Karlsson * Accounting vnode pointer, saved vnode pointer, and flags for each. 9471909edeSRobert Watson * acct_sx protects against changes to the active vnode and credentials 9571909edeSRobert Watson * while accounting records are being committed to disk. 96c7d893deSDavid Greenman */ 9771909edeSRobert Watson static int acct_suspended; 9871909edeSRobert Watson static struct vnode *acct_vp; 9971909edeSRobert Watson static struct ucred *acct_cred; 10071909edeSRobert Watson static int acct_flags; 10171909edeSRobert Watson static struct sx acct_sx; 102df8bae1dSRodney W. Grimes 10371909edeSRobert Watson SX_SYSINIT(acct, &acct_sx, "acct_sx"); 1044f39d5d5SAndrew R. Reiter 105df8bae1dSRodney W. Grimes /* 106505a1493SJohn Baldwin * State of the accounting kthread. 107505a1493SJohn Baldwin */ 108505a1493SJohn Baldwin static int acct_state; 109505a1493SJohn Baldwin 110505a1493SJohn Baldwin #define ACCT_RUNNING 1 /* Accounting kthread is running. */ 111505a1493SJohn Baldwin #define ACCT_EXITREQ 2 /* Accounting kthread should exit. */ 112505a1493SJohn Baldwin 113505a1493SJohn Baldwin /* 114df8bae1dSRodney W. Grimes * Values associated with enabling and disabling accounting 115df8bae1dSRodney W. Grimes */ 11687b6de2bSPoul-Henning Kamp static int acctsuspend = 2; /* stop accounting when < 2% free space left */ 11787b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW, 11847fdd692SNeil Blakey-Milner &acctsuspend, 0, "percentage of free disk space below which accounting stops"); 11987b6de2bSPoul-Henning Kamp 12087b6de2bSPoul-Henning Kamp static int acctresume = 4; /* resume when free space risen to > 4% */ 12187b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW, 12247fdd692SNeil Blakey-Milner &acctresume, 0, "percentage of free disk space above which accounting resumes"); 12387b6de2bSPoul-Henning Kamp 12487b6de2bSPoul-Henning Kamp static int acctchkfreq = 15; /* frequency (in seconds) to check space */ 125222fdf4bSJohn Baldwin 126222fdf4bSJohn Baldwin static int 127222fdf4bSJohn Baldwin sysctl_acct_chkfreq(SYSCTL_HANDLER_ARGS) 128222fdf4bSJohn Baldwin { 129222fdf4bSJohn Baldwin int error, value; 130222fdf4bSJohn Baldwin 131222fdf4bSJohn Baldwin /* Write out the old value. */ 132222fdf4bSJohn Baldwin error = SYSCTL_OUT(req, &acctchkfreq, sizeof(int)); 133222fdf4bSJohn Baldwin if (error || req->newptr == NULL) 134222fdf4bSJohn Baldwin return (error); 135222fdf4bSJohn Baldwin 136222fdf4bSJohn Baldwin /* Read in and verify the new value. */ 137222fdf4bSJohn Baldwin error = SYSCTL_IN(req, &value, sizeof(int)); 138222fdf4bSJohn Baldwin if (error) 139222fdf4bSJohn Baldwin return (error); 140222fdf4bSJohn Baldwin if (value <= 0) 141222fdf4bSJohn Baldwin return (EINVAL); 142222fdf4bSJohn Baldwin acctchkfreq = value; 143222fdf4bSJohn Baldwin return (0); 144222fdf4bSJohn Baldwin } 145222fdf4bSJohn Baldwin SYSCTL_PROC(_kern, OID_AUTO, acct_chkfreq, CTLTYPE_INT|CTLFLAG_RW, 146222fdf4bSJohn Baldwin &acctchkfreq, 0, sysctl_acct_chkfreq, "I", 147222fdf4bSJohn Baldwin "frequency for checking the free space"); 148df8bae1dSRodney W. Grimes 14971909edeSRobert Watson SYSCTL_INT(_kern, OID_AUTO, acct_suspended, CTLFLAG_RD, &acct_suspended, 0, 15071909edeSRobert Watson "Accounting suspended or not"); 15171909edeSRobert Watson 152df8bae1dSRodney W. Grimes /* 153c7d893deSDavid Greenman * Accounting system call. Written based on the specification and 154c7d893deSDavid Greenman * previous implementation done by Mark Tinguely. 155116734c4SMatthew Dillon * 156116734c4SMatthew Dillon * MPSAFE 157df8bae1dSRodney W. Grimes */ 158c7d893deSDavid Greenman int 15971909edeSRobert Watson acct(struct thread *td, struct acct_args *uap) 160c7d893deSDavid Greenman { 161c7d893deSDavid Greenman struct nameidata nd; 162e6796b67SKirk McKusick int error, flags; 163c7d893deSDavid Greenman 164c7d893deSDavid Greenman /* Make sure that the caller is root. */ 16544731cabSJohn Baldwin error = suser(td); 166797f2d22SPoul-Henning Kamp if (error) 16716e7bc7bSJohn Baldwin return (error); 168c7d893deSDavid Greenman 169c7d893deSDavid Greenman /* 170c7d893deSDavid Greenman * If accounting is to be started to a file, open that file for 17171909edeSRobert Watson * appending and make sure it's a 'normal'. While we could 17271909edeSRobert Watson * conditionally acquire Giant here, we're actually interacting with 17371909edeSRobert Watson * vnodes from possibly two file systems, making the logic a bit 17471909edeSRobert Watson * complicated. For now, use Giant unconditionally. 175c7d893deSDavid Greenman */ 17671909edeSRobert Watson mtx_lock(&Giant); 177d1e405c5SAlfred Perlstein if (uap->path != NULL) { 178f97182acSAlfred Perlstein NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td); 17992da2e76SJohan Karlsson flags = FWRITE | O_APPEND; 1807c89f162SPoul-Henning Kamp error = vn_open(&nd, &flags, 0, -1); 181797f2d22SPoul-Henning Kamp if (error) 18271909edeSRobert Watson goto done; 183762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 184e5e820fdSRobert Watson #ifdef MAC 185e5e820fdSRobert Watson error = mac_check_system_acct(td->td_ucred, nd.ni_vp); 186e5e820fdSRobert Watson if (error) { 18708132261SRobert Watson VOP_UNLOCK(nd.ni_vp, 0, td); 188e5e820fdSRobert Watson vn_close(nd.ni_vp, flags, td->td_ucred, td); 18971909edeSRobert Watson goto done; 190e5e820fdSRobert Watson } 191e5e820fdSRobert Watson #endif 192b40ce416SJulian Elischer VOP_UNLOCK(nd.ni_vp, 0, td); 193c7d893deSDavid Greenman if (nd.ni_vp->v_type != VREG) { 1945b606744SJohan Karlsson vn_close(nd.ni_vp, flags, td->td_ucred, td); 195116734c4SMatthew Dillon error = EACCES; 19671909edeSRobert Watson goto done; 197c7d893deSDavid Greenman } 198e5e820fdSRobert Watson #ifdef MAC 199e5e820fdSRobert Watson } else { 200e5e820fdSRobert Watson error = mac_check_system_acct(td->td_ucred, NULL); 201e5e820fdSRobert Watson if (error) 20271909edeSRobert Watson goto done; 203e5e820fdSRobert Watson #endif 204c7d893deSDavid Greenman } 205c7d893deSDavid Greenman 20671909edeSRobert Watson /* 20771909edeSRobert Watson * Disallow concurrent access to the accounting vnode while we swap 20871909edeSRobert Watson * it out, in order to prevent access after close. 20971909edeSRobert Watson */ 21071909edeSRobert Watson sx_xlock(&acct_sx); 21101e3f3aeSBruce Evans 212c7d893deSDavid Greenman /* 213c7d893deSDavid Greenman * If accounting was previously enabled, kill the old space-watcher, 21471909edeSRobert Watson * close the file, and (if no new file was specified, leave). Reset 21571909edeSRobert Watson * the suspended state regardless of whether accounting remains 21671909edeSRobert Watson * enabled. 217c7d893deSDavid Greenman */ 21871909edeSRobert Watson acct_suspended = 0; 219505a1493SJohn Baldwin if (acct_vp != NULL) 220505a1493SJohn Baldwin error = acct_disable(td); 221d1e405c5SAlfred Perlstein if (uap->path == NULL) { 222505a1493SJohn Baldwin if (acct_state & ACCT_RUNNING) { 223505a1493SJohn Baldwin acct_state |= ACCT_EXITREQ; 224505a1493SJohn Baldwin wakeup(&acct_state); 225505a1493SJohn Baldwin } 22671909edeSRobert Watson sx_xunlock(&acct_sx); 22771909edeSRobert Watson goto done; 228b4dcc46aSAndrew R. Reiter } 229c7d893deSDavid Greenman 230c7d893deSDavid Greenman /* 231c7d893deSDavid Greenman * Save the new accounting file vnode, and schedule the new 232c7d893deSDavid Greenman * free space watcher. 233c7d893deSDavid Greenman */ 23471909edeSRobert Watson acct_vp = nd.ni_vp; 23571909edeSRobert Watson acct_cred = crhold(td->td_ucred); 23671909edeSRobert Watson acct_flags = flags; 237505a1493SJohn Baldwin if (acct_state & ACCT_RUNNING) 238505a1493SJohn Baldwin acct_state &= ~ACCT_EXITREQ; 239505a1493SJohn Baldwin else { 240505a1493SJohn Baldwin /* 241505a1493SJohn Baldwin * Try to start up an accounting kthread. We may start more 242505a1493SJohn Baldwin * than one, but if so the extras will commit suicide as 243505a1493SJohn Baldwin * soon as they start up. 244505a1493SJohn Baldwin */ 245505a1493SJohn Baldwin error = kthread_create(acct_thread, NULL, NULL, 0, 0, 246505a1493SJohn Baldwin "accounting"); 247505a1493SJohn Baldwin if (error) { 248505a1493SJohn Baldwin (void) vn_close(acct_vp, acct_flags, acct_cred, td); 249505a1493SJohn Baldwin crfree(acct_cred); 250505a1493SJohn Baldwin acct_vp = NULL; 251505a1493SJohn Baldwin acct_cred = NULL; 252505a1493SJohn Baldwin acct_flags = 0; 253505a1493SJohn Baldwin sx_xunlock(&acct_sx); 254505a1493SJohn Baldwin log(LOG_NOTICE, "Unable to start accounting thread\n"); 255505a1493SJohn Baldwin goto done; 256505a1493SJohn Baldwin } 257505a1493SJohn Baldwin } 25871909edeSRobert Watson sx_xunlock(&acct_sx); 25948719ca7SBosko Milekic log(LOG_NOTICE, "Accounting enabled\n"); 26071909edeSRobert Watson done: 261116734c4SMatthew Dillon mtx_unlock(&Giant); 262c7d893deSDavid Greenman return (error); 263c7d893deSDavid Greenman } 264c7d893deSDavid Greenman 265c7d893deSDavid Greenman /* 266505a1493SJohn Baldwin * Disable currently in-progress accounting by closing the vnode, dropping 267505a1493SJohn Baldwin * our reference to the credential, and clearing the vnode's flags. 268505a1493SJohn Baldwin */ 269505a1493SJohn Baldwin static int 270505a1493SJohn Baldwin acct_disable(struct thread *td) 271505a1493SJohn Baldwin { 272505a1493SJohn Baldwin int error; 273505a1493SJohn Baldwin 274505a1493SJohn Baldwin sx_assert(&acct_sx, SX_XLOCKED); 275505a1493SJohn Baldwin error = vn_close(acct_vp, acct_flags, acct_cred, td); 276505a1493SJohn Baldwin crfree(acct_cred); 277505a1493SJohn Baldwin acct_vp = NULL; 278505a1493SJohn Baldwin acct_cred = NULL; 279505a1493SJohn Baldwin acct_flags = 0; 280505a1493SJohn Baldwin log(LOG_NOTICE, "Accounting disabled\n"); 281505a1493SJohn Baldwin return (error); 282505a1493SJohn Baldwin } 283505a1493SJohn Baldwin 284505a1493SJohn Baldwin /* 285c7d893deSDavid Greenman * Write out process accounting information, on process exit. 286c7d893deSDavid Greenman * Data to be written out is specified in Leffler, et al. 287c7d893deSDavid Greenman * and are enumerated below. (They're also noted in the system 288c7d893deSDavid Greenman * "acct.h" header file.) 289c7d893deSDavid Greenman */ 290c7d893deSDavid Greenman int 29171909edeSRobert Watson acct_process(struct thread *td) 292c7d893deSDavid Greenman { 293c7d893deSDavid Greenman struct acct acct; 294c7d893deSDavid Greenman struct timeval ut, st, tmp; 29591d5354aSJohn Baldwin struct plimit *newlim, *oldlim; 29601e3f3aeSBruce Evans struct proc *p; 29701e3f3aeSBruce Evans struct rusage *r; 29871909edeSRobert Watson int t, ret, vfslocked; 2994f39d5d5SAndrew R. Reiter 3002b05b557SRobert Watson /* 3012b05b557SRobert Watson * Lockless check of accounting condition before doing the hard 3022b05b557SRobert Watson * work. 3032b05b557SRobert Watson */ 30471909edeSRobert Watson if (acct_vp == NULL || acct_suspended) 3052b05b557SRobert Watson return (0); 3062b05b557SRobert Watson 30771909edeSRobert Watson sx_slock(&acct_sx); 308c7d893deSDavid Greenman 3092b05b557SRobert Watson /* 3102b05b557SRobert Watson * If accounting isn't enabled, don't bother. Have to check again 3112b05b557SRobert Watson * once we own the lock in case we raced with disabling of accounting 3122b05b557SRobert Watson * by another thread. 3132b05b557SRobert Watson */ 31471909edeSRobert Watson if (acct_vp == NULL || acct_suspended) { 31571909edeSRobert Watson sx_sunlock(&acct_sx); 316c7d893deSDavid Greenman return (0); 3174f39d5d5SAndrew R. Reiter } 318c7d893deSDavid Greenman 31901e3f3aeSBruce Evans p = td->td_proc; 32001e3f3aeSBruce Evans 321c7d893deSDavid Greenman /* 322c7d893deSDavid Greenman * Get process accounting information. 323c7d893deSDavid Greenman */ 324c7d893deSDavid Greenman 3257e653dbdSJohn Baldwin PROC_LOCK(p); 326c7d893deSDavid Greenman /* (1) The name of the command that ran */ 327c7d893deSDavid Greenman bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm); 328c7d893deSDavid Greenman 329c7d893deSDavid Greenman /* (2) The amount of user and system time that was used */ 33078c85e8dSJohn Baldwin calcru(p, &ut, &st); 331c7d893deSDavid Greenman acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec); 332c7d893deSDavid Greenman acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec); 333c7d893deSDavid Greenman 3345f9ae8e0SGiorgos Keramidas /* (3) The elapsed time the command ran (and its starting time) */ 33587ccef7bSDag-Erling Smørgrav tmp = boottime; 33687ccef7bSDag-Erling Smørgrav timevaladd(&tmp, &p->p_stats->p_start); 33787ccef7bSDag-Erling Smørgrav acct.ac_btime = tmp.tv_sec; 33887ccef7bSDag-Erling Smørgrav microuptime(&tmp); 339c7d893deSDavid Greenman timevalsub(&tmp, &p->p_stats->p_start); 340c7d893deSDavid Greenman acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec); 341c7d893deSDavid Greenman 342c7d893deSDavid Greenman /* (4) The average amount of memory used */ 343c7d893deSDavid Greenman r = &p->p_stats->p_ru; 344c7d893deSDavid Greenman tmp = ut; 345c7d893deSDavid Greenman timevaladd(&tmp, &st); 346c7d893deSDavid Greenman t = tmp.tv_sec * hz + tmp.tv_usec / tick; 347c7d893deSDavid Greenman if (t) 348c7d893deSDavid Greenman acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t; 349c7d893deSDavid Greenman else 350c7d893deSDavid Greenman acct.ac_mem = 0; 351c7d893deSDavid Greenman 352c7d893deSDavid Greenman /* (5) The number of disk I/O operations done */ 353c7d893deSDavid Greenman acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0); 354c7d893deSDavid Greenman 355c7d893deSDavid Greenman /* (6) The UID and GID of the process */ 356b1fc0ec1SRobert Watson acct.ac_uid = p->p_ucred->cr_ruid; 357b1fc0ec1SRobert Watson acct.ac_gid = p->p_ucred->cr_rgid; 358c7d893deSDavid Greenman 359c7d893deSDavid Greenman /* (7) The terminal from which the process was started */ 360f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 361c7d893deSDavid Greenman if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp) 36223d76283SPoul-Henning Kamp acct.ac_tty = dev2udev(p->p_pgrp->pg_session->s_ttyp->t_dev); 363c7d893deSDavid Greenman else 364f3732fd1SPoul-Henning Kamp acct.ac_tty = NODEV; 365f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 366c7d893deSDavid Greenman 367c7d893deSDavid Greenman /* (8) The boolean flags that tell how the process terminated, etc. */ 368c7d893deSDavid Greenman acct.ac_flag = p->p_acflag; 3697e653dbdSJohn Baldwin PROC_UNLOCK(p); 370c7d893deSDavid Greenman 371c7d893deSDavid Greenman /* 372b5afad71SDavid Greenman * Eliminate any file size rlimit. 373b5afad71SDavid Greenman */ 37491d5354aSJohn Baldwin newlim = lim_alloc(); 37591d5354aSJohn Baldwin PROC_LOCK(p); 37691d5354aSJohn Baldwin oldlim = p->p_limit; 37791d5354aSJohn Baldwin lim_copy(newlim, oldlim); 37891d5354aSJohn Baldwin newlim->pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 37991d5354aSJohn Baldwin p->p_limit = newlim; 38091d5354aSJohn Baldwin PROC_UNLOCK(p); 38191d5354aSJohn Baldwin lim_free(oldlim); 382b5afad71SDavid Greenman 38301e3f3aeSBruce Evans /* 38401e3f3aeSBruce Evans * Write the accounting information to the file. 38501e3f3aeSBruce Evans */ 38671909edeSRobert Watson vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount); 38771909edeSRobert Watson VOP_LEASE(acct_vp, td, acct_cred, LEASE_WRITE); 38871909edeSRobert Watson ret = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct, sizeof (acct), 38971909edeSRobert Watson (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acct_cred, NOCRED, 3904f39d5d5SAndrew R. Reiter (int *)0, td); 39171909edeSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 39271909edeSRobert Watson sx_sunlock(&acct_sx); 3934f39d5d5SAndrew R. Reiter return (ret); 394c7d893deSDavid Greenman } 395c7d893deSDavid Greenman 396c7d893deSDavid Greenman /* 397c7d893deSDavid Greenman * Encode_comp_t converts from ticks in seconds and microseconds 398c7d893deSDavid Greenman * to ticks in 1/AHZ seconds. The encoding is described in 399c7d893deSDavid Greenman * Leffler, et al., on page 63. 400c7d893deSDavid Greenman */ 401c7d893deSDavid Greenman 402c7d893deSDavid Greenman #define MANTSIZE 13 /* 13 bit mantissa. */ 403c7d893deSDavid Greenman #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */ 404c7d893deSDavid Greenman #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */ 405c7d893deSDavid Greenman 40687b6de2bSPoul-Henning Kamp static comp_t 40771909edeSRobert Watson encode_comp_t(u_long s, u_long us) 408c7d893deSDavid Greenman { 409c7d893deSDavid Greenman int exp, rnd; 410c7d893deSDavid Greenman 411c7d893deSDavid Greenman exp = 0; 412c7d893deSDavid Greenman rnd = 0; 413c7d893deSDavid Greenman s *= AHZ; 414c7d893deSDavid Greenman s += us / (1000000 / AHZ); /* Maximize precision. */ 415c7d893deSDavid Greenman 416c7d893deSDavid Greenman while (s > MAXFRACT) { 417c7d893deSDavid Greenman rnd = s & (1 << (EXPSIZE - 1)); /* Round up? */ 418c7d893deSDavid Greenman s >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */ 419c7d893deSDavid Greenman exp++; 420c7d893deSDavid Greenman } 421c7d893deSDavid Greenman 422c7d893deSDavid Greenman /* If we need to round up, do it (and handle overflow correctly). */ 423c7d893deSDavid Greenman if (rnd && (++s > MAXFRACT)) { 424c7d893deSDavid Greenman s >>= EXPSIZE; 425c7d893deSDavid Greenman exp++; 426c7d893deSDavid Greenman } 427c7d893deSDavid Greenman 428c7d893deSDavid Greenman /* Clean it up and polish it off. */ 429c7d893deSDavid Greenman exp <<= MANTSIZE; /* Shift the exponent into place */ 430c7d893deSDavid Greenman exp += s; /* and add on the mantissa. */ 431c7d893deSDavid Greenman return (exp); 432c7d893deSDavid Greenman } 433c7d893deSDavid Greenman 434c7d893deSDavid Greenman /* 435c7d893deSDavid Greenman * Periodically check the filesystem to see if accounting 436c7d893deSDavid Greenman * should be turned on or off. Beware the case where the vnode 437c7d893deSDavid Greenman * has been vgone()'d out from underneath us, e.g. when the file 438c7d893deSDavid Greenman * system containing the accounting file has been forcibly unmounted. 439c7d893deSDavid Greenman */ 440df8bae1dSRodney W. Grimes /* ARGSUSED */ 44187b6de2bSPoul-Henning Kamp static void 442505a1493SJohn Baldwin acctwatch(void) 443df8bae1dSRodney W. Grimes { 444df8bae1dSRodney W. Grimes struct statfs sb; 44571909edeSRobert Watson int vfslocked; 446df8bae1dSRodney W. Grimes 447505a1493SJohn Baldwin sx_assert(&acct_sx, SX_XLOCKED); 448505a1493SJohn Baldwin 449505a1493SJohn Baldwin /* 450505a1493SJohn Baldwin * If accounting was disabled before our kthread was scheduled, 451505a1493SJohn Baldwin * then acct_vp might be NULL. If so, just ask our kthread to 452505a1493SJohn Baldwin * exit and return. 453505a1493SJohn Baldwin */ 454505a1493SJohn Baldwin if (acct_vp == NULL) { 455505a1493SJohn Baldwin acct_state |= ACCT_EXITREQ; 456c7d893deSDavid Greenman return; 457c7d893deSDavid Greenman } 458505a1493SJohn Baldwin 459505a1493SJohn Baldwin /* 460505a1493SJohn Baldwin * If our vnode is no longer valid, tear it down and signal the 461505a1493SJohn Baldwin * accounting thread to die. 462505a1493SJohn Baldwin */ 463505a1493SJohn Baldwin vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount); 464505a1493SJohn Baldwin if (acct_vp->v_type == VBAD) { 465505a1493SJohn Baldwin (void) acct_disable(NULL); 466505a1493SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 467505a1493SJohn Baldwin acct_state |= ACCT_EXITREQ; 468505a1493SJohn Baldwin return; 469505a1493SJohn Baldwin } 470505a1493SJohn Baldwin 47171909edeSRobert Watson /* 47271909edeSRobert Watson * Stopping here is better than continuing, maybe it will be VBAD 47371909edeSRobert Watson * next time around. 47471909edeSRobert Watson */ 47571909edeSRobert Watson if (VFS_STATFS(acct_vp->v_mount, &sb, curthread) < 0) { 47671909edeSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 47771909edeSRobert Watson return; 47871909edeSRobert Watson } 47971909edeSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 48071909edeSRobert Watson if (acct_suspended) { 48171909edeSRobert Watson if (sb.f_bavail > (int64_t)(acctresume * sb.f_blocks / 48271909edeSRobert Watson 100)) { 48371909edeSRobert Watson acct_suspended = 0; 484df8bae1dSRodney W. Grimes log(LOG_NOTICE, "Accounting resumed\n"); 485df8bae1dSRodney W. Grimes } 486996c772fSJohn Dyson } else { 48771909edeSRobert Watson if (sb.f_bavail <= (int64_t)(acctsuspend * sb.f_blocks / 48871909edeSRobert Watson 100)) { 48971909edeSRobert Watson acct_suspended = 1; 490df8bae1dSRodney W. Grimes log(LOG_NOTICE, "Accounting suspended\n"); 491df8bae1dSRodney W. Grimes } 492996c772fSJohn Dyson } 493505a1493SJohn Baldwin } 494505a1493SJohn Baldwin 495505a1493SJohn Baldwin /* 496505a1493SJohn Baldwin * The main loop for the dedicated kernel thread that periodically calls 497505a1493SJohn Baldwin * acctwatch(). 498505a1493SJohn Baldwin */ 499505a1493SJohn Baldwin static void 500505a1493SJohn Baldwin acct_thread(void *dummy) 501505a1493SJohn Baldwin { 502505a1493SJohn Baldwin u_char pri; 503505a1493SJohn Baldwin 504505a1493SJohn Baldwin /* This is a low-priority kernel thread. */ 505505a1493SJohn Baldwin pri = PRI_MAX_KERN; 506505a1493SJohn Baldwin mtx_lock_spin(&sched_lock); 507505a1493SJohn Baldwin sched_prio(curthread, pri); 508505a1493SJohn Baldwin mtx_unlock_spin(&sched_lock); 509505a1493SJohn Baldwin 510505a1493SJohn Baldwin /* If another accounting kthread is already running, just die. */ 511505a1493SJohn Baldwin sx_xlock(&acct_sx); 512505a1493SJohn Baldwin if (acct_state & ACCT_RUNNING) { 51371909edeSRobert Watson sx_xunlock(&acct_sx); 514505a1493SJohn Baldwin kthread_exit(0); 515505a1493SJohn Baldwin } 516505a1493SJohn Baldwin acct_state |= ACCT_RUNNING; 517505a1493SJohn Baldwin 518505a1493SJohn Baldwin /* Loop until we are asked to exit. */ 519505a1493SJohn Baldwin while (!(acct_state & ACCT_EXITREQ)) { 520505a1493SJohn Baldwin 521505a1493SJohn Baldwin /* Perform our periodic checks. */ 522505a1493SJohn Baldwin acctwatch(); 523505a1493SJohn Baldwin 524505a1493SJohn Baldwin /* 525505a1493SJohn Baldwin * We check this flag again before sleeping since the 526505a1493SJohn Baldwin * acctwatch() might have shut down accounting and asked us 527505a1493SJohn Baldwin * to exit. 528505a1493SJohn Baldwin */ 529505a1493SJohn Baldwin if (!(acct_state & ACCT_EXITREQ)) { 530505a1493SJohn Baldwin sx_xunlock(&acct_sx); 531505a1493SJohn Baldwin tsleep(&acct_state, pri, "-", acctchkfreq * hz); 532505a1493SJohn Baldwin sx_xlock(&acct_sx); 533505a1493SJohn Baldwin } 534505a1493SJohn Baldwin } 535505a1493SJohn Baldwin 536505a1493SJohn Baldwin /* 537505a1493SJohn Baldwin * Acknowledge the exit request and shutdown. We clear both the 538505a1493SJohn Baldwin * exit request and running flags. 539505a1493SJohn Baldwin */ 540505a1493SJohn Baldwin acct_state = 0; 541505a1493SJohn Baldwin sx_xunlock(&acct_sx); 542505a1493SJohn Baldwin kthread_exit(0); 543df8bae1dSRodney W. Grimes } 544