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> 56df8bae1dSRodney W. Grimes #include <sys/mount.h> 57b0864d13SJohn Baldwin #include <sys/mutex.h> 58c7d893deSDavid Greenman #include <sys/namei.h> 59acd3428bSRobert Watson #include <sys/priv.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 71aed55708SRobert Watson #include <security/mac/mac_framework.h> 72aed55708SRobert Watson 73df8bae1dSRodney W. Grimes /* 74c7d893deSDavid Greenman * The routines implemented in this file are described in: 75c7d893deSDavid Greenman * Leffler, et al.: The Design and Implementation of the 4.3BSD 76c7d893deSDavid Greenman * UNIX Operating System (Addison Welley, 1989) 77c7d893deSDavid Greenman * on pages 62-63. 78c7d893deSDavid Greenman * 79c7d893deSDavid Greenman * Arguably, to simplify accounting operations, this mechanism should 80c7d893deSDavid Greenman * be replaced by one in which an accounting log file (similar to /dev/klog) 81c7d893deSDavid Greenman * is read by a user process, etc. However, that has its own problems. 82df8bae1dSRodney W. Grimes */ 83df8bae1dSRodney W. Grimes 84df8bae1dSRodney W. Grimes /* 85c7d893deSDavid Greenman * Internal accounting functions. 86c7d893deSDavid Greenman * The former's operation is described in Leffler, et al., and the latter 87c7d893deSDavid Greenman * was provided by UCB with the 4.4BSD-Lite release 88df8bae1dSRodney W. Grimes */ 894d77a549SAlfred Perlstein static comp_t encode_comp_t(u_long, u_long); 90505a1493SJohn Baldwin static void acctwatch(void); 91505a1493SJohn Baldwin static void acct_thread(void *); 92505a1493SJohn Baldwin static int acct_disable(struct thread *); 93ab36c067SJustin T. Gibbs 94ab36c067SJustin T. Gibbs /* 955b606744SJohan Karlsson * Accounting vnode pointer, saved vnode pointer, and flags for each. 9671909edeSRobert Watson * acct_sx protects against changes to the active vnode and credentials 9771909edeSRobert Watson * while accounting records are being committed to disk. 98c7d893deSDavid Greenman */ 99101581b0SRobert Watson static int acct_configured; 10071909edeSRobert Watson static int acct_suspended; 10171909edeSRobert Watson static struct vnode *acct_vp; 10271909edeSRobert Watson static struct ucred *acct_cred; 10371909edeSRobert Watson static int acct_flags; 10471909edeSRobert Watson static struct sx acct_sx; 105df8bae1dSRodney W. Grimes 10671909edeSRobert Watson SX_SYSINIT(acct, &acct_sx, "acct_sx"); 1074f39d5d5SAndrew R. Reiter 108df8bae1dSRodney W. Grimes /* 109505a1493SJohn Baldwin * State of the accounting kthread. 110505a1493SJohn Baldwin */ 111505a1493SJohn Baldwin static int acct_state; 112505a1493SJohn Baldwin 113505a1493SJohn Baldwin #define ACCT_RUNNING 1 /* Accounting kthread is running. */ 114505a1493SJohn Baldwin #define ACCT_EXITREQ 2 /* Accounting kthread should exit. */ 115505a1493SJohn Baldwin 116505a1493SJohn Baldwin /* 117df8bae1dSRodney W. Grimes * Values associated with enabling and disabling accounting 118df8bae1dSRodney W. Grimes */ 11987b6de2bSPoul-Henning Kamp static int acctsuspend = 2; /* stop accounting when < 2% free space left */ 12087b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW, 12147fdd692SNeil Blakey-Milner &acctsuspend, 0, "percentage of free disk space below which accounting stops"); 12287b6de2bSPoul-Henning Kamp 12387b6de2bSPoul-Henning Kamp static int acctresume = 4; /* resume when free space risen to > 4% */ 12487b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW, 12547fdd692SNeil Blakey-Milner &acctresume, 0, "percentage of free disk space above which accounting resumes"); 12687b6de2bSPoul-Henning Kamp 12787b6de2bSPoul-Henning Kamp static int acctchkfreq = 15; /* frequency (in seconds) to check space */ 128222fdf4bSJohn Baldwin 129222fdf4bSJohn Baldwin static int 130222fdf4bSJohn Baldwin sysctl_acct_chkfreq(SYSCTL_HANDLER_ARGS) 131222fdf4bSJohn Baldwin { 132222fdf4bSJohn Baldwin int error, value; 133222fdf4bSJohn Baldwin 134222fdf4bSJohn Baldwin /* Write out the old value. */ 135222fdf4bSJohn Baldwin error = SYSCTL_OUT(req, &acctchkfreq, sizeof(int)); 136222fdf4bSJohn Baldwin if (error || req->newptr == NULL) 137222fdf4bSJohn Baldwin return (error); 138222fdf4bSJohn Baldwin 139222fdf4bSJohn Baldwin /* Read in and verify the new value. */ 140222fdf4bSJohn Baldwin error = SYSCTL_IN(req, &value, sizeof(int)); 141222fdf4bSJohn Baldwin if (error) 142222fdf4bSJohn Baldwin return (error); 143222fdf4bSJohn Baldwin if (value <= 0) 144222fdf4bSJohn Baldwin return (EINVAL); 145222fdf4bSJohn Baldwin acctchkfreq = value; 146222fdf4bSJohn Baldwin return (0); 147222fdf4bSJohn Baldwin } 148222fdf4bSJohn Baldwin SYSCTL_PROC(_kern, OID_AUTO, acct_chkfreq, CTLTYPE_INT|CTLFLAG_RW, 149222fdf4bSJohn Baldwin &acctchkfreq, 0, sysctl_acct_chkfreq, "I", 150222fdf4bSJohn Baldwin "frequency for checking the free space"); 151df8bae1dSRodney W. Grimes 152101581b0SRobert Watson SYSCTL_INT(_kern, OID_AUTO, acct_configured, CTLFLAG_RD, &acct_configured, 0, 153101581b0SRobert Watson "Accounting configured or not"); 154101581b0SRobert Watson 15571909edeSRobert Watson SYSCTL_INT(_kern, OID_AUTO, acct_suspended, CTLFLAG_RD, &acct_suspended, 0, 15671909edeSRobert Watson "Accounting suspended or not"); 15771909edeSRobert Watson 158df8bae1dSRodney W. Grimes /* 159c7d893deSDavid Greenman * Accounting system call. Written based on the specification and 160c7d893deSDavid Greenman * previous implementation done by Mark Tinguely. 161116734c4SMatthew Dillon * 162116734c4SMatthew Dillon * MPSAFE 163df8bae1dSRodney W. Grimes */ 164c7d893deSDavid Greenman int 16571909edeSRobert Watson acct(struct thread *td, struct acct_args *uap) 166c7d893deSDavid Greenman { 167c7d893deSDavid Greenman struct nameidata nd; 16811178ee4SJohn Baldwin int error, flags, vfslocked; 169c7d893deSDavid Greenman 170acd3428bSRobert Watson error = priv_check(td, PRIV_ACCT); 171797f2d22SPoul-Henning Kamp if (error) 17216e7bc7bSJohn Baldwin return (error); 173c7d893deSDavid Greenman 174c7d893deSDavid Greenman /* 175c7d893deSDavid Greenman * If accounting is to be started to a file, open that file for 17611178ee4SJohn Baldwin * appending and make sure it's a 'normal'. 177c7d893deSDavid Greenman */ 178d1e405c5SAlfred Perlstein if (uap->path != NULL) { 179d3778141SRobert Watson NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, 180d3778141SRobert Watson UIO_USERSPACE, uap->path, td); 18192da2e76SJohan Karlsson flags = FWRITE | O_APPEND; 1827c89f162SPoul-Henning Kamp error = vn_open(&nd, &flags, 0, -1); 183797f2d22SPoul-Henning Kamp if (error) 18411178ee4SJohn Baldwin return (error); 18511178ee4SJohn Baldwin vfslocked = NDHASGIANT(&nd); 186762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 187e5e820fdSRobert Watson #ifdef MAC 188e5e820fdSRobert Watson error = mac_check_system_acct(td->td_ucred, nd.ni_vp); 189e5e820fdSRobert Watson if (error) { 19008132261SRobert Watson VOP_UNLOCK(nd.ni_vp, 0, td); 191e5e820fdSRobert Watson vn_close(nd.ni_vp, flags, td->td_ucred, td); 19211178ee4SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 19311178ee4SJohn Baldwin return (error); 194e5e820fdSRobert Watson } 195e5e820fdSRobert Watson #endif 196b40ce416SJulian Elischer VOP_UNLOCK(nd.ni_vp, 0, td); 197c7d893deSDavid Greenman if (nd.ni_vp->v_type != VREG) { 1985b606744SJohan Karlsson vn_close(nd.ni_vp, flags, td->td_ucred, td); 19911178ee4SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 20011178ee4SJohn Baldwin return (EACCES); 201c7d893deSDavid Greenman } 20211178ee4SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 203e5e820fdSRobert Watson #ifdef MAC 204e5e820fdSRobert Watson } else { 205e5e820fdSRobert Watson error = mac_check_system_acct(td->td_ucred, NULL); 206e5e820fdSRobert Watson if (error) 20711178ee4SJohn Baldwin return (error); 208e5e820fdSRobert Watson #endif 209c7d893deSDavid Greenman } 210c7d893deSDavid Greenman 21171909edeSRobert Watson /* 21271909edeSRobert Watson * Disallow concurrent access to the accounting vnode while we swap 21371909edeSRobert Watson * it out, in order to prevent access after close. 21471909edeSRobert Watson */ 21571909edeSRobert Watson sx_xlock(&acct_sx); 21601e3f3aeSBruce Evans 217c7d893deSDavid Greenman /* 218c7d893deSDavid Greenman * If accounting was previously enabled, kill the old space-watcher, 21971909edeSRobert Watson * close the file, and (if no new file was specified, leave). Reset 22071909edeSRobert Watson * the suspended state regardless of whether accounting remains 22171909edeSRobert Watson * enabled. 222c7d893deSDavid Greenman */ 22371909edeSRobert Watson acct_suspended = 0; 22411178ee4SJohn Baldwin if (acct_vp != NULL) { 22511178ee4SJohn Baldwin vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount); 226505a1493SJohn Baldwin error = acct_disable(td); 22711178ee4SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 22811178ee4SJohn Baldwin } 229d1e405c5SAlfred Perlstein if (uap->path == NULL) { 230505a1493SJohn Baldwin if (acct_state & ACCT_RUNNING) { 231505a1493SJohn Baldwin acct_state |= ACCT_EXITREQ; 232505a1493SJohn Baldwin wakeup(&acct_state); 233505a1493SJohn Baldwin } 23471909edeSRobert Watson sx_xunlock(&acct_sx); 23511178ee4SJohn Baldwin return (error); 236b4dcc46aSAndrew R. Reiter } 237c7d893deSDavid Greenman 238c7d893deSDavid Greenman /* 239c7d893deSDavid Greenman * Save the new accounting file vnode, and schedule the new 240c7d893deSDavid Greenman * free space watcher. 241c7d893deSDavid Greenman */ 24271909edeSRobert Watson acct_vp = nd.ni_vp; 24371909edeSRobert Watson acct_cred = crhold(td->td_ucred); 24471909edeSRobert Watson acct_flags = flags; 245505a1493SJohn Baldwin if (acct_state & ACCT_RUNNING) 246505a1493SJohn Baldwin acct_state &= ~ACCT_EXITREQ; 247505a1493SJohn Baldwin else { 248505a1493SJohn Baldwin /* 249505a1493SJohn Baldwin * Try to start up an accounting kthread. We may start more 250505a1493SJohn Baldwin * than one, but if so the extras will commit suicide as 251505a1493SJohn Baldwin * soon as they start up. 252505a1493SJohn Baldwin */ 253505a1493SJohn Baldwin error = kthread_create(acct_thread, NULL, NULL, 0, 0, 254505a1493SJohn Baldwin "accounting"); 255505a1493SJohn Baldwin if (error) { 25611178ee4SJohn Baldwin vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount); 257505a1493SJohn Baldwin (void) vn_close(acct_vp, acct_flags, acct_cred, td); 25811178ee4SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 259505a1493SJohn Baldwin crfree(acct_cred); 260101581b0SRobert Watson acct_configured = 0; 261505a1493SJohn Baldwin acct_vp = NULL; 262505a1493SJohn Baldwin acct_cred = NULL; 263505a1493SJohn Baldwin acct_flags = 0; 264505a1493SJohn Baldwin sx_xunlock(&acct_sx); 265505a1493SJohn Baldwin log(LOG_NOTICE, "Unable to start accounting thread\n"); 26611178ee4SJohn Baldwin return (error); 267505a1493SJohn Baldwin } 268505a1493SJohn Baldwin } 269101581b0SRobert Watson acct_configured = 1; 27071909edeSRobert Watson sx_xunlock(&acct_sx); 27148719ca7SBosko Milekic log(LOG_NOTICE, "Accounting enabled\n"); 272c7d893deSDavid Greenman return (error); 273c7d893deSDavid Greenman } 274c7d893deSDavid Greenman 275c7d893deSDavid Greenman /* 276505a1493SJohn Baldwin * Disable currently in-progress accounting by closing the vnode, dropping 277505a1493SJohn Baldwin * our reference to the credential, and clearing the vnode's flags. 278505a1493SJohn Baldwin */ 279505a1493SJohn Baldwin static int 280505a1493SJohn Baldwin acct_disable(struct thread *td) 281505a1493SJohn Baldwin { 282505a1493SJohn Baldwin int error; 283505a1493SJohn Baldwin 284505a1493SJohn Baldwin sx_assert(&acct_sx, SX_XLOCKED); 285505a1493SJohn Baldwin error = vn_close(acct_vp, acct_flags, acct_cred, td); 286505a1493SJohn Baldwin crfree(acct_cred); 287101581b0SRobert Watson acct_configured = 0; 288505a1493SJohn Baldwin acct_vp = NULL; 289505a1493SJohn Baldwin acct_cred = NULL; 290505a1493SJohn Baldwin acct_flags = 0; 291505a1493SJohn Baldwin log(LOG_NOTICE, "Accounting disabled\n"); 292505a1493SJohn Baldwin return (error); 293505a1493SJohn Baldwin } 294505a1493SJohn Baldwin 295505a1493SJohn Baldwin /* 296c7d893deSDavid Greenman * Write out process accounting information, on process exit. 297c7d893deSDavid Greenman * Data to be written out is specified in Leffler, et al. 298c7d893deSDavid Greenman * and are enumerated below. (They're also noted in the system 299c7d893deSDavid Greenman * "acct.h" header file.) 300c7d893deSDavid Greenman */ 301c7d893deSDavid Greenman int 30271909edeSRobert Watson acct_process(struct thread *td) 303c7d893deSDavid Greenman { 304c7d893deSDavid Greenman struct acct acct; 305c7d893deSDavid Greenman struct timeval ut, st, tmp; 30691d5354aSJohn Baldwin struct plimit *newlim, *oldlim; 30701e3f3aeSBruce Evans struct proc *p; 30801e3f3aeSBruce Evans struct rusage *r; 30971909edeSRobert Watson int t, ret, vfslocked; 3104f39d5d5SAndrew R. Reiter 3112b05b557SRobert Watson /* 3122b05b557SRobert Watson * Lockless check of accounting condition before doing the hard 3132b05b557SRobert Watson * work. 3142b05b557SRobert Watson */ 31571909edeSRobert Watson if (acct_vp == NULL || acct_suspended) 3162b05b557SRobert Watson return (0); 3172b05b557SRobert Watson 31871909edeSRobert Watson sx_slock(&acct_sx); 319c7d893deSDavid Greenman 3202b05b557SRobert Watson /* 3212b05b557SRobert Watson * If accounting isn't enabled, don't bother. Have to check again 3222b05b557SRobert Watson * once we own the lock in case we raced with disabling of accounting 3232b05b557SRobert Watson * by another thread. 3242b05b557SRobert Watson */ 32571909edeSRobert Watson if (acct_vp == NULL || acct_suspended) { 32671909edeSRobert Watson sx_sunlock(&acct_sx); 327c7d893deSDavid Greenman return (0); 3284f39d5d5SAndrew R. Reiter } 329c7d893deSDavid Greenman 33001e3f3aeSBruce Evans p = td->td_proc; 33101e3f3aeSBruce Evans 332c7d893deSDavid Greenman /* 333c7d893deSDavid Greenman * Get process accounting information. 334c7d893deSDavid Greenman */ 335c7d893deSDavid Greenman 3367e653dbdSJohn Baldwin PROC_LOCK(p); 337c7d893deSDavid Greenman /* (1) The name of the command that ran */ 338c7d893deSDavid Greenman bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm); 339c7d893deSDavid Greenman 340c7d893deSDavid Greenman /* (2) The amount of user and system time that was used */ 34178c85e8dSJohn Baldwin calcru(p, &ut, &st); 342c7d893deSDavid Greenman acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec); 343c7d893deSDavid Greenman acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec); 344c7d893deSDavid Greenman 3455f9ae8e0SGiorgos Keramidas /* (3) The elapsed time the command ran (and its starting time) */ 34687ccef7bSDag-Erling Smørgrav tmp = boottime; 34787ccef7bSDag-Erling Smørgrav timevaladd(&tmp, &p->p_stats->p_start); 34887ccef7bSDag-Erling Smørgrav acct.ac_btime = tmp.tv_sec; 34987ccef7bSDag-Erling Smørgrav microuptime(&tmp); 350c7d893deSDavid Greenman timevalsub(&tmp, &p->p_stats->p_start); 351c7d893deSDavid Greenman acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec); 352c7d893deSDavid Greenman 353c7d893deSDavid Greenman /* (4) The average amount of memory used */ 354c7d893deSDavid Greenman r = &p->p_stats->p_ru; 355c7d893deSDavid Greenman tmp = ut; 356c7d893deSDavid Greenman timevaladd(&tmp, &st); 357c7d893deSDavid Greenman t = tmp.tv_sec * hz + tmp.tv_usec / tick; 358c7d893deSDavid Greenman if (t) 359c7d893deSDavid Greenman acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t; 360c7d893deSDavid Greenman else 361c7d893deSDavid Greenman acct.ac_mem = 0; 362c7d893deSDavid Greenman 363c7d893deSDavid Greenman /* (5) The number of disk I/O operations done */ 364c7d893deSDavid Greenman acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0); 365c7d893deSDavid Greenman 366c7d893deSDavid Greenman /* (6) The UID and GID of the process */ 367b1fc0ec1SRobert Watson acct.ac_uid = p->p_ucred->cr_ruid; 368b1fc0ec1SRobert Watson acct.ac_gid = p->p_ucred->cr_rgid; 369c7d893deSDavid Greenman 370c7d893deSDavid Greenman /* (7) The terminal from which the process was started */ 371f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 372c7d893deSDavid Greenman if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp) 37323d76283SPoul-Henning Kamp acct.ac_tty = dev2udev(p->p_pgrp->pg_session->s_ttyp->t_dev); 374c7d893deSDavid Greenman else 375f3732fd1SPoul-Henning Kamp acct.ac_tty = NODEV; 376f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 377c7d893deSDavid Greenman 378c7d893deSDavid Greenman /* (8) The boolean flags that tell how the process terminated, etc. */ 379c7d893deSDavid Greenman acct.ac_flag = p->p_acflag; 3807e653dbdSJohn Baldwin PROC_UNLOCK(p); 381c7d893deSDavid Greenman 382c7d893deSDavid Greenman /* 383b5afad71SDavid Greenman * Eliminate any file size rlimit. 384b5afad71SDavid Greenman */ 38591d5354aSJohn Baldwin newlim = lim_alloc(); 38691d5354aSJohn Baldwin PROC_LOCK(p); 38791d5354aSJohn Baldwin oldlim = p->p_limit; 38891d5354aSJohn Baldwin lim_copy(newlim, oldlim); 38991d5354aSJohn Baldwin newlim->pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 39091d5354aSJohn Baldwin p->p_limit = newlim; 39191d5354aSJohn Baldwin PROC_UNLOCK(p); 39291d5354aSJohn Baldwin lim_free(oldlim); 393b5afad71SDavid Greenman 39401e3f3aeSBruce Evans /* 39501e3f3aeSBruce Evans * Write the accounting information to the file. 39601e3f3aeSBruce Evans */ 39771909edeSRobert Watson vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount); 39871909edeSRobert Watson VOP_LEASE(acct_vp, td, acct_cred, LEASE_WRITE); 39971909edeSRobert Watson ret = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct, sizeof (acct), 40071909edeSRobert Watson (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acct_cred, NOCRED, 4014f39d5d5SAndrew R. Reiter (int *)0, td); 40271909edeSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 40371909edeSRobert Watson sx_sunlock(&acct_sx); 4044f39d5d5SAndrew R. Reiter return (ret); 405c7d893deSDavid Greenman } 406c7d893deSDavid Greenman 407c7d893deSDavid Greenman /* 408c7d893deSDavid Greenman * Encode_comp_t converts from ticks in seconds and microseconds 409c7d893deSDavid Greenman * to ticks in 1/AHZ seconds. The encoding is described in 410c7d893deSDavid Greenman * Leffler, et al., on page 63. 411c7d893deSDavid Greenman */ 412c7d893deSDavid Greenman 413c7d893deSDavid Greenman #define MANTSIZE 13 /* 13 bit mantissa. */ 414c7d893deSDavid Greenman #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */ 415c7d893deSDavid Greenman #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */ 416c7d893deSDavid Greenman 41787b6de2bSPoul-Henning Kamp static comp_t 41871909edeSRobert Watson encode_comp_t(u_long s, u_long us) 419c7d893deSDavid Greenman { 420c7d893deSDavid Greenman int exp, rnd; 421c7d893deSDavid Greenman 422c7d893deSDavid Greenman exp = 0; 423c7d893deSDavid Greenman rnd = 0; 424c7d893deSDavid Greenman s *= AHZ; 425c7d893deSDavid Greenman s += us / (1000000 / AHZ); /* Maximize precision. */ 426c7d893deSDavid Greenman 427c7d893deSDavid Greenman while (s > MAXFRACT) { 428c7d893deSDavid Greenman rnd = s & (1 << (EXPSIZE - 1)); /* Round up? */ 429c7d893deSDavid Greenman s >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */ 430c7d893deSDavid Greenman exp++; 431c7d893deSDavid Greenman } 432c7d893deSDavid Greenman 433c7d893deSDavid Greenman /* If we need to round up, do it (and handle overflow correctly). */ 434c7d893deSDavid Greenman if (rnd && (++s > MAXFRACT)) { 435c7d893deSDavid Greenman s >>= EXPSIZE; 436c7d893deSDavid Greenman exp++; 437c7d893deSDavid Greenman } 438c7d893deSDavid Greenman 439c7d893deSDavid Greenman /* Clean it up and polish it off. */ 440c7d893deSDavid Greenman exp <<= MANTSIZE; /* Shift the exponent into place */ 441c7d893deSDavid Greenman exp += s; /* and add on the mantissa. */ 442c7d893deSDavid Greenman return (exp); 443c7d893deSDavid Greenman } 444c7d893deSDavid Greenman 445c7d893deSDavid Greenman /* 446c7d893deSDavid Greenman * Periodically check the filesystem to see if accounting 447c7d893deSDavid Greenman * should be turned on or off. Beware the case where the vnode 448c7d893deSDavid Greenman * has been vgone()'d out from underneath us, e.g. when the file 449c7d893deSDavid Greenman * system containing the accounting file has been forcibly unmounted. 450c7d893deSDavid Greenman */ 451df8bae1dSRodney W. Grimes /* ARGSUSED */ 45287b6de2bSPoul-Henning Kamp static void 453505a1493SJohn Baldwin acctwatch(void) 454df8bae1dSRodney W. Grimes { 455df8bae1dSRodney W. Grimes struct statfs sb; 45671909edeSRobert Watson int vfslocked; 457df8bae1dSRodney W. Grimes 458505a1493SJohn Baldwin sx_assert(&acct_sx, SX_XLOCKED); 459505a1493SJohn Baldwin 460505a1493SJohn Baldwin /* 461505a1493SJohn Baldwin * If accounting was disabled before our kthread was scheduled, 462505a1493SJohn Baldwin * then acct_vp might be NULL. If so, just ask our kthread to 463505a1493SJohn Baldwin * exit and return. 464505a1493SJohn Baldwin */ 465505a1493SJohn Baldwin if (acct_vp == NULL) { 466505a1493SJohn Baldwin acct_state |= ACCT_EXITREQ; 467c7d893deSDavid Greenman return; 468c7d893deSDavid Greenman } 469505a1493SJohn Baldwin 470505a1493SJohn Baldwin /* 471505a1493SJohn Baldwin * If our vnode is no longer valid, tear it down and signal the 472505a1493SJohn Baldwin * accounting thread to die. 473505a1493SJohn Baldwin */ 474505a1493SJohn Baldwin vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount); 475505a1493SJohn Baldwin if (acct_vp->v_type == VBAD) { 476505a1493SJohn Baldwin (void) acct_disable(NULL); 477505a1493SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 478505a1493SJohn Baldwin acct_state |= ACCT_EXITREQ; 479505a1493SJohn Baldwin return; 480505a1493SJohn Baldwin } 481505a1493SJohn Baldwin 48271909edeSRobert Watson /* 48371909edeSRobert Watson * Stopping here is better than continuing, maybe it will be VBAD 48471909edeSRobert Watson * next time around. 48571909edeSRobert Watson */ 48671909edeSRobert Watson if (VFS_STATFS(acct_vp->v_mount, &sb, curthread) < 0) { 48771909edeSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 48871909edeSRobert Watson return; 48971909edeSRobert Watson } 49071909edeSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 49171909edeSRobert Watson if (acct_suspended) { 49271909edeSRobert Watson if (sb.f_bavail > (int64_t)(acctresume * sb.f_blocks / 49371909edeSRobert Watson 100)) { 49471909edeSRobert Watson acct_suspended = 0; 495df8bae1dSRodney W. Grimes log(LOG_NOTICE, "Accounting resumed\n"); 496df8bae1dSRodney W. Grimes } 497996c772fSJohn Dyson } else { 49871909edeSRobert Watson if (sb.f_bavail <= (int64_t)(acctsuspend * sb.f_blocks / 49971909edeSRobert Watson 100)) { 50071909edeSRobert Watson acct_suspended = 1; 501df8bae1dSRodney W. Grimes log(LOG_NOTICE, "Accounting suspended\n"); 502df8bae1dSRodney W. Grimes } 503996c772fSJohn Dyson } 504505a1493SJohn Baldwin } 505505a1493SJohn Baldwin 506505a1493SJohn Baldwin /* 507505a1493SJohn Baldwin * The main loop for the dedicated kernel thread that periodically calls 508505a1493SJohn Baldwin * acctwatch(). 509505a1493SJohn Baldwin */ 510505a1493SJohn Baldwin static void 511505a1493SJohn Baldwin acct_thread(void *dummy) 512505a1493SJohn Baldwin { 513505a1493SJohn Baldwin u_char pri; 514505a1493SJohn Baldwin 515505a1493SJohn Baldwin /* This is a low-priority kernel thread. */ 516505a1493SJohn Baldwin pri = PRI_MAX_KERN; 517505a1493SJohn Baldwin mtx_lock_spin(&sched_lock); 518505a1493SJohn Baldwin sched_prio(curthread, pri); 519505a1493SJohn Baldwin mtx_unlock_spin(&sched_lock); 520505a1493SJohn Baldwin 521505a1493SJohn Baldwin /* If another accounting kthread is already running, just die. */ 522505a1493SJohn Baldwin sx_xlock(&acct_sx); 523505a1493SJohn Baldwin if (acct_state & ACCT_RUNNING) { 52471909edeSRobert Watson sx_xunlock(&acct_sx); 525505a1493SJohn Baldwin kthread_exit(0); 526505a1493SJohn Baldwin } 527505a1493SJohn Baldwin acct_state |= ACCT_RUNNING; 528505a1493SJohn Baldwin 529505a1493SJohn Baldwin /* Loop until we are asked to exit. */ 530505a1493SJohn Baldwin while (!(acct_state & ACCT_EXITREQ)) { 531505a1493SJohn Baldwin 532505a1493SJohn Baldwin /* Perform our periodic checks. */ 533505a1493SJohn Baldwin acctwatch(); 534505a1493SJohn Baldwin 535505a1493SJohn Baldwin /* 536505a1493SJohn Baldwin * We check this flag again before sleeping since the 537505a1493SJohn Baldwin * acctwatch() might have shut down accounting and asked us 538505a1493SJohn Baldwin * to exit. 539505a1493SJohn Baldwin */ 540505a1493SJohn Baldwin if (!(acct_state & ACCT_EXITREQ)) { 541505a1493SJohn Baldwin sx_xunlock(&acct_sx); 542505a1493SJohn Baldwin tsleep(&acct_state, pri, "-", acctchkfreq * hz); 543505a1493SJohn Baldwin sx_xlock(&acct_sx); 544505a1493SJohn Baldwin } 545505a1493SJohn Baldwin } 546505a1493SJohn Baldwin 547505a1493SJohn Baldwin /* 548505a1493SJohn Baldwin * Acknowledge the exit request and shutdown. We clear both the 549505a1493SJohn Baldwin * exit request and running flags. 550505a1493SJohn Baldwin */ 551505a1493SJohn Baldwin acct_state = 0; 552505a1493SJohn Baldwin sx_xunlock(&acct_sx); 553505a1493SJohn Baldwin kthread_exit(0); 554df8bae1dSRodney W. Grimes } 555