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. 5fcdc50ebSRobert Watson * Copyright (c) 2005 Robert N. M. Watson 6fcdc50ebSRobert Watson * All rights reserved. 7fcdc50ebSRobert Watson * 8df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 9df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 10df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 12df8bae1dSRodney W. Grimes * 13fcdc50ebSRobert Watson * Redistribution and use in source and binary forms, with or without 14fcdc50ebSRobert Watson * modification, are permitted provided that the following conditions 15fcdc50ebSRobert Watson * are met: 16fcdc50ebSRobert Watson * 1. Redistributions of source code must retain the above copyright 17fcdc50ebSRobert Watson * notice, this list of conditions and the following disclaimer. 18fcdc50ebSRobert Watson * 2. Redistributions in binary form must reproduce the above copyright 19fcdc50ebSRobert Watson * notice, this list of conditions and the following disclaimer in the 20fcdc50ebSRobert Watson * documentation and/or other materials provided with the distribution. 21fcdc50ebSRobert Watson * 4. Neither the name of the University nor the names of its contributors 22fcdc50ebSRobert Watson * may be used to endorse or promote products derived from this software 23fcdc50ebSRobert Watson * without specific prior written permission. 24fcdc50ebSRobert Watson * 25fcdc50ebSRobert Watson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26fcdc50ebSRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27fcdc50ebSRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28fcdc50ebSRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29fcdc50ebSRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30fcdc50ebSRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31fcdc50ebSRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32fcdc50ebSRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33fcdc50ebSRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34fcdc50ebSRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35fcdc50ebSRobert Watson * SUCH DAMAGE. 36fcdc50ebSRobert Watson * 3771909edeSRobert Watson * Copyright (c) 1994 Christopher G. Demetriou 3871909edeSRobert Watson * 39df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 40df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 41df8bae1dSRodney W. Grimes * are met: 42df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 43df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 44df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 45df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 46df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 47df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 48df8bae1dSRodney W. Grimes * must display the following acknowledgement: 49df8bae1dSRodney W. Grimes * This product includes software developed by the University of 50df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 51df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 52df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 53df8bae1dSRodney W. Grimes * without specific prior written permission. 54df8bae1dSRodney W. Grimes * 55df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 56df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 59df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65df8bae1dSRodney W. Grimes * SUCH DAMAGE. 66df8bae1dSRodney W. Grimes * 67c7d893deSDavid Greenman * @(#)kern_acct.c 8.1 (Berkeley) 6/14/93 68df8bae1dSRodney W. Grimes */ 69df8bae1dSRodney W. Grimes 70677b542eSDavid E. O'Brien #include <sys/cdefs.h> 71677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 72677b542eSDavid E. O'Brien 73e5e820fdSRobert Watson #include "opt_mac.h" 74e5e820fdSRobert Watson 75df8bae1dSRodney W. Grimes #include <sys/param.h> 760ad076d5SBruce Evans #include <sys/systm.h> 77b0864d13SJohn Baldwin #include <sys/acct.h> 78b0864d13SJohn Baldwin #include <sys/fcntl.h> 79b0864d13SJohn Baldwin #include <sys/kernel.h> 80505a1493SJohn Baldwin #include <sys/kthread.h> 81fb919e4dSMark Murray #include <sys/lock.h> 82df8bae1dSRodney W. Grimes #include <sys/mount.h> 83b0864d13SJohn Baldwin #include <sys/mutex.h> 84c7d893deSDavid Greenman #include <sys/namei.h> 85acd3428bSRobert Watson #include <sys/priv.h> 86b0864d13SJohn Baldwin #include <sys/proc.h> 87c7d893deSDavid Greenman #include <sys/resourcevar.h> 88505a1493SJohn Baldwin #include <sys/sched.h> 89b0864d13SJohn Baldwin #include <sys/sx.h> 90b0864d13SJohn Baldwin #include <sys/sysctl.h> 91b0864d13SJohn Baldwin #include <sys/sysent.h> 92b0864d13SJohn Baldwin #include <sys/syslog.h> 93b0864d13SJohn Baldwin #include <sys/sysproto.h> 94c7d893deSDavid Greenman #include <sys/tty.h> 95b0864d13SJohn Baldwin #include <sys/vnode.h> 96df8bae1dSRodney W. Grimes 97aed55708SRobert Watson #include <security/mac/mac_framework.h> 98aed55708SRobert Watson 99df8bae1dSRodney W. Grimes /* 100c7d893deSDavid Greenman * The routines implemented in this file are described in: 101c7d893deSDavid Greenman * Leffler, et al.: The Design and Implementation of the 4.3BSD 102c7d893deSDavid Greenman * UNIX Operating System (Addison Welley, 1989) 103c7d893deSDavid Greenman * on pages 62-63. 104c7d893deSDavid Greenman * 105c7d893deSDavid Greenman * Arguably, to simplify accounting operations, this mechanism should 106c7d893deSDavid Greenman * be replaced by one in which an accounting log file (similar to /dev/klog) 107c7d893deSDavid Greenman * is read by a user process, etc. However, that has its own problems. 108df8bae1dSRodney W. Grimes */ 109df8bae1dSRodney W. Grimes 110df8bae1dSRodney W. Grimes /* 111c7d893deSDavid Greenman * Internal accounting functions. 112c7d893deSDavid Greenman * The former's operation is described in Leffler, et al., and the latter 113c7d893deSDavid Greenman * was provided by UCB with the 4.4BSD-Lite release 114df8bae1dSRodney W. Grimes */ 1154d77a549SAlfred Perlstein static comp_t encode_comp_t(u_long, u_long); 116505a1493SJohn Baldwin static void acctwatch(void); 117505a1493SJohn Baldwin static void acct_thread(void *); 118505a1493SJohn Baldwin static int acct_disable(struct thread *); 119ab36c067SJustin T. Gibbs 120ab36c067SJustin T. Gibbs /* 1215b606744SJohan Karlsson * Accounting vnode pointer, saved vnode pointer, and flags for each. 12271909edeSRobert Watson * acct_sx protects against changes to the active vnode and credentials 12371909edeSRobert Watson * while accounting records are being committed to disk. 124c7d893deSDavid Greenman */ 125101581b0SRobert Watson static int acct_configured; 12671909edeSRobert Watson static int acct_suspended; 12771909edeSRobert Watson static struct vnode *acct_vp; 12871909edeSRobert Watson static struct ucred *acct_cred; 12971909edeSRobert Watson static int acct_flags; 13071909edeSRobert Watson static struct sx acct_sx; 131df8bae1dSRodney W. Grimes 13271909edeSRobert Watson SX_SYSINIT(acct, &acct_sx, "acct_sx"); 1334f39d5d5SAndrew R. Reiter 134df8bae1dSRodney W. Grimes /* 135505a1493SJohn Baldwin * State of the accounting kthread. 136505a1493SJohn Baldwin */ 137505a1493SJohn Baldwin static int acct_state; 138505a1493SJohn Baldwin 139505a1493SJohn Baldwin #define ACCT_RUNNING 1 /* Accounting kthread is running. */ 140505a1493SJohn Baldwin #define ACCT_EXITREQ 2 /* Accounting kthread should exit. */ 141505a1493SJohn Baldwin 142505a1493SJohn Baldwin /* 143df8bae1dSRodney W. Grimes * Values associated with enabling and disabling accounting 144df8bae1dSRodney W. Grimes */ 14587b6de2bSPoul-Henning Kamp static int acctsuspend = 2; /* stop accounting when < 2% free space left */ 14687b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW, 14747fdd692SNeil Blakey-Milner &acctsuspend, 0, "percentage of free disk space below which accounting stops"); 14887b6de2bSPoul-Henning Kamp 14987b6de2bSPoul-Henning Kamp static int acctresume = 4; /* resume when free space risen to > 4% */ 15087b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW, 15147fdd692SNeil Blakey-Milner &acctresume, 0, "percentage of free disk space above which accounting resumes"); 15287b6de2bSPoul-Henning Kamp 15387b6de2bSPoul-Henning Kamp static int acctchkfreq = 15; /* frequency (in seconds) to check space */ 154222fdf4bSJohn Baldwin 155222fdf4bSJohn Baldwin static int 156222fdf4bSJohn Baldwin sysctl_acct_chkfreq(SYSCTL_HANDLER_ARGS) 157222fdf4bSJohn Baldwin { 158222fdf4bSJohn Baldwin int error, value; 159222fdf4bSJohn Baldwin 160222fdf4bSJohn Baldwin /* Write out the old value. */ 161222fdf4bSJohn Baldwin error = SYSCTL_OUT(req, &acctchkfreq, sizeof(int)); 162222fdf4bSJohn Baldwin if (error || req->newptr == NULL) 163222fdf4bSJohn Baldwin return (error); 164222fdf4bSJohn Baldwin 165222fdf4bSJohn Baldwin /* Read in and verify the new value. */ 166222fdf4bSJohn Baldwin error = SYSCTL_IN(req, &value, sizeof(int)); 167222fdf4bSJohn Baldwin if (error) 168222fdf4bSJohn Baldwin return (error); 169222fdf4bSJohn Baldwin if (value <= 0) 170222fdf4bSJohn Baldwin return (EINVAL); 171222fdf4bSJohn Baldwin acctchkfreq = value; 172222fdf4bSJohn Baldwin return (0); 173222fdf4bSJohn Baldwin } 174222fdf4bSJohn Baldwin SYSCTL_PROC(_kern, OID_AUTO, acct_chkfreq, CTLTYPE_INT|CTLFLAG_RW, 175222fdf4bSJohn Baldwin &acctchkfreq, 0, sysctl_acct_chkfreq, "I", 176222fdf4bSJohn Baldwin "frequency for checking the free space"); 177df8bae1dSRodney W. Grimes 178101581b0SRobert Watson SYSCTL_INT(_kern, OID_AUTO, acct_configured, CTLFLAG_RD, &acct_configured, 0, 179101581b0SRobert Watson "Accounting configured or not"); 180101581b0SRobert Watson 18171909edeSRobert Watson SYSCTL_INT(_kern, OID_AUTO, acct_suspended, CTLFLAG_RD, &acct_suspended, 0, 18271909edeSRobert Watson "Accounting suspended or not"); 18371909edeSRobert Watson 184df8bae1dSRodney W. Grimes /* 185c7d893deSDavid Greenman * Accounting system call. Written based on the specification and 186c7d893deSDavid Greenman * previous implementation done by Mark Tinguely. 187116734c4SMatthew Dillon * 188116734c4SMatthew Dillon * MPSAFE 189df8bae1dSRodney W. Grimes */ 190c7d893deSDavid Greenman int 19171909edeSRobert Watson acct(struct thread *td, struct acct_args *uap) 192c7d893deSDavid Greenman { 193c7d893deSDavid Greenman struct nameidata nd; 19411178ee4SJohn Baldwin int error, flags, vfslocked; 195c7d893deSDavid Greenman 196acd3428bSRobert Watson error = priv_check(td, PRIV_ACCT); 197797f2d22SPoul-Henning Kamp if (error) 19816e7bc7bSJohn Baldwin return (error); 199c7d893deSDavid Greenman 200c7d893deSDavid Greenman /* 201c7d893deSDavid Greenman * If accounting is to be started to a file, open that file for 20211178ee4SJohn Baldwin * appending and make sure it's a 'normal'. 203c7d893deSDavid Greenman */ 204d1e405c5SAlfred Perlstein if (uap->path != NULL) { 205d3778141SRobert Watson NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1, 206d3778141SRobert Watson UIO_USERSPACE, uap->path, td); 20792da2e76SJohan Karlsson flags = FWRITE | O_APPEND; 2087c89f162SPoul-Henning Kamp error = vn_open(&nd, &flags, 0, -1); 209797f2d22SPoul-Henning Kamp if (error) 21011178ee4SJohn Baldwin return (error); 21111178ee4SJohn Baldwin vfslocked = NDHASGIANT(&nd); 212762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 213e5e820fdSRobert Watson #ifdef MAC 214e5e820fdSRobert Watson error = mac_check_system_acct(td->td_ucred, nd.ni_vp); 215e5e820fdSRobert Watson if (error) { 21608132261SRobert Watson VOP_UNLOCK(nd.ni_vp, 0, td); 217e5e820fdSRobert Watson vn_close(nd.ni_vp, flags, td->td_ucred, td); 21811178ee4SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 21911178ee4SJohn Baldwin return (error); 220e5e820fdSRobert Watson } 221e5e820fdSRobert Watson #endif 222b40ce416SJulian Elischer VOP_UNLOCK(nd.ni_vp, 0, td); 223c7d893deSDavid Greenman if (nd.ni_vp->v_type != VREG) { 2245b606744SJohan Karlsson vn_close(nd.ni_vp, flags, td->td_ucred, td); 22511178ee4SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 22611178ee4SJohn Baldwin return (EACCES); 227c7d893deSDavid Greenman } 22811178ee4SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 229e5e820fdSRobert Watson #ifdef MAC 230e5e820fdSRobert Watson } else { 231e5e820fdSRobert Watson error = mac_check_system_acct(td->td_ucred, NULL); 232e5e820fdSRobert Watson if (error) 23311178ee4SJohn Baldwin return (error); 234e5e820fdSRobert Watson #endif 235c7d893deSDavid Greenman } 236c7d893deSDavid Greenman 23771909edeSRobert Watson /* 23871909edeSRobert Watson * Disallow concurrent access to the accounting vnode while we swap 23971909edeSRobert Watson * it out, in order to prevent access after close. 24071909edeSRobert Watson */ 24171909edeSRobert Watson sx_xlock(&acct_sx); 24201e3f3aeSBruce Evans 243c7d893deSDavid Greenman /* 244c7d893deSDavid Greenman * If accounting was previously enabled, kill the old space-watcher, 24571909edeSRobert Watson * close the file, and (if no new file was specified, leave). Reset 24671909edeSRobert Watson * the suspended state regardless of whether accounting remains 24771909edeSRobert Watson * enabled. 248c7d893deSDavid Greenman */ 24971909edeSRobert Watson acct_suspended = 0; 25011178ee4SJohn Baldwin if (acct_vp != NULL) { 25111178ee4SJohn Baldwin vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount); 252505a1493SJohn Baldwin error = acct_disable(td); 25311178ee4SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 25411178ee4SJohn Baldwin } 255d1e405c5SAlfred Perlstein if (uap->path == NULL) { 256505a1493SJohn Baldwin if (acct_state & ACCT_RUNNING) { 257505a1493SJohn Baldwin acct_state |= ACCT_EXITREQ; 258505a1493SJohn Baldwin wakeup(&acct_state); 259505a1493SJohn Baldwin } 26071909edeSRobert Watson sx_xunlock(&acct_sx); 26111178ee4SJohn Baldwin return (error); 262b4dcc46aSAndrew R. Reiter } 263c7d893deSDavid Greenman 264c7d893deSDavid Greenman /* 265c7d893deSDavid Greenman * Save the new accounting file vnode, and schedule the new 266c7d893deSDavid Greenman * free space watcher. 267c7d893deSDavid Greenman */ 26871909edeSRobert Watson acct_vp = nd.ni_vp; 26971909edeSRobert Watson acct_cred = crhold(td->td_ucred); 27071909edeSRobert Watson acct_flags = flags; 271505a1493SJohn Baldwin if (acct_state & ACCT_RUNNING) 272505a1493SJohn Baldwin acct_state &= ~ACCT_EXITREQ; 273505a1493SJohn Baldwin else { 274505a1493SJohn Baldwin /* 275505a1493SJohn Baldwin * Try to start up an accounting kthread. We may start more 276505a1493SJohn Baldwin * than one, but if so the extras will commit suicide as 277505a1493SJohn Baldwin * soon as they start up. 278505a1493SJohn Baldwin */ 279505a1493SJohn Baldwin error = kthread_create(acct_thread, NULL, NULL, 0, 0, 280505a1493SJohn Baldwin "accounting"); 281505a1493SJohn Baldwin if (error) { 28211178ee4SJohn Baldwin vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount); 283505a1493SJohn Baldwin (void) vn_close(acct_vp, acct_flags, acct_cred, td); 28411178ee4SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 285505a1493SJohn Baldwin crfree(acct_cred); 286101581b0SRobert Watson acct_configured = 0; 287505a1493SJohn Baldwin acct_vp = NULL; 288505a1493SJohn Baldwin acct_cred = NULL; 289505a1493SJohn Baldwin acct_flags = 0; 290505a1493SJohn Baldwin sx_xunlock(&acct_sx); 291505a1493SJohn Baldwin log(LOG_NOTICE, "Unable to start accounting thread\n"); 29211178ee4SJohn Baldwin return (error); 293505a1493SJohn Baldwin } 294505a1493SJohn Baldwin } 295101581b0SRobert Watson acct_configured = 1; 29671909edeSRobert Watson sx_xunlock(&acct_sx); 29748719ca7SBosko Milekic log(LOG_NOTICE, "Accounting enabled\n"); 298c7d893deSDavid Greenman return (error); 299c7d893deSDavid Greenman } 300c7d893deSDavid Greenman 301c7d893deSDavid Greenman /* 302505a1493SJohn Baldwin * Disable currently in-progress accounting by closing the vnode, dropping 303505a1493SJohn Baldwin * our reference to the credential, and clearing the vnode's flags. 304505a1493SJohn Baldwin */ 305505a1493SJohn Baldwin static int 306505a1493SJohn Baldwin acct_disable(struct thread *td) 307505a1493SJohn Baldwin { 308505a1493SJohn Baldwin int error; 309505a1493SJohn Baldwin 310505a1493SJohn Baldwin sx_assert(&acct_sx, SX_XLOCKED); 311505a1493SJohn Baldwin error = vn_close(acct_vp, acct_flags, acct_cred, td); 312505a1493SJohn Baldwin crfree(acct_cred); 313101581b0SRobert Watson acct_configured = 0; 314505a1493SJohn Baldwin acct_vp = NULL; 315505a1493SJohn Baldwin acct_cred = NULL; 316505a1493SJohn Baldwin acct_flags = 0; 317505a1493SJohn Baldwin log(LOG_NOTICE, "Accounting disabled\n"); 318505a1493SJohn Baldwin return (error); 319505a1493SJohn Baldwin } 320505a1493SJohn Baldwin 321505a1493SJohn Baldwin /* 322c7d893deSDavid Greenman * Write out process accounting information, on process exit. 323c7d893deSDavid Greenman * Data to be written out is specified in Leffler, et al. 324c7d893deSDavid Greenman * and are enumerated below. (They're also noted in the system 325c7d893deSDavid Greenman * "acct.h" header file.) 326c7d893deSDavid Greenman */ 327c7d893deSDavid Greenman int 32871909edeSRobert Watson acct_process(struct thread *td) 329c7d893deSDavid Greenman { 330c7d893deSDavid Greenman struct acct acct; 331c7d893deSDavid Greenman struct timeval ut, st, tmp; 33291d5354aSJohn Baldwin struct plimit *newlim, *oldlim; 33301e3f3aeSBruce Evans struct proc *p; 33401e3f3aeSBruce Evans struct rusage *r; 33571909edeSRobert Watson int t, ret, vfslocked; 3364f39d5d5SAndrew R. Reiter 3372b05b557SRobert Watson /* 3382b05b557SRobert Watson * Lockless check of accounting condition before doing the hard 3392b05b557SRobert Watson * work. 3402b05b557SRobert Watson */ 34171909edeSRobert Watson if (acct_vp == NULL || acct_suspended) 3422b05b557SRobert Watson return (0); 3432b05b557SRobert Watson 34471909edeSRobert Watson sx_slock(&acct_sx); 345c7d893deSDavid Greenman 3462b05b557SRobert Watson /* 3472b05b557SRobert Watson * If accounting isn't enabled, don't bother. Have to check again 3482b05b557SRobert Watson * once we own the lock in case we raced with disabling of accounting 3492b05b557SRobert Watson * by another thread. 3502b05b557SRobert Watson */ 35171909edeSRobert Watson if (acct_vp == NULL || acct_suspended) { 35271909edeSRobert Watson sx_sunlock(&acct_sx); 353c7d893deSDavid Greenman return (0); 3544f39d5d5SAndrew R. Reiter } 355c7d893deSDavid Greenman 35601e3f3aeSBruce Evans p = td->td_proc; 35701e3f3aeSBruce Evans 358c7d893deSDavid Greenman /* 359c7d893deSDavid Greenman * Get process accounting information. 360c7d893deSDavid Greenman */ 361c7d893deSDavid Greenman 3627e653dbdSJohn Baldwin PROC_LOCK(p); 363c7d893deSDavid Greenman /* (1) The name of the command that ran */ 364c7d893deSDavid Greenman bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm); 365c7d893deSDavid Greenman 366c7d893deSDavid Greenman /* (2) The amount of user and system time that was used */ 36778c85e8dSJohn Baldwin calcru(p, &ut, &st); 368c7d893deSDavid Greenman acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec); 369c7d893deSDavid Greenman acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec); 370c7d893deSDavid Greenman 3715f9ae8e0SGiorgos Keramidas /* (3) The elapsed time the command ran (and its starting time) */ 37287ccef7bSDag-Erling Smørgrav tmp = boottime; 37387ccef7bSDag-Erling Smørgrav timevaladd(&tmp, &p->p_stats->p_start); 37487ccef7bSDag-Erling Smørgrav acct.ac_btime = tmp.tv_sec; 37587ccef7bSDag-Erling Smørgrav microuptime(&tmp); 376c7d893deSDavid Greenman timevalsub(&tmp, &p->p_stats->p_start); 377c7d893deSDavid Greenman acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec); 378c7d893deSDavid Greenman 379c7d893deSDavid Greenman /* (4) The average amount of memory used */ 380c7d893deSDavid Greenman r = &p->p_stats->p_ru; 381c7d893deSDavid Greenman tmp = ut; 382c7d893deSDavid Greenman timevaladd(&tmp, &st); 383c7d893deSDavid Greenman t = tmp.tv_sec * hz + tmp.tv_usec / tick; 384c7d893deSDavid Greenman if (t) 385c7d893deSDavid Greenman acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t; 386c7d893deSDavid Greenman else 387c7d893deSDavid Greenman acct.ac_mem = 0; 388c7d893deSDavid Greenman 389c7d893deSDavid Greenman /* (5) The number of disk I/O operations done */ 390c7d893deSDavid Greenman acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0); 391c7d893deSDavid Greenman 392c7d893deSDavid Greenman /* (6) The UID and GID of the process */ 393b1fc0ec1SRobert Watson acct.ac_uid = p->p_ucred->cr_ruid; 394b1fc0ec1SRobert Watson acct.ac_gid = p->p_ucred->cr_rgid; 395c7d893deSDavid Greenman 396c7d893deSDavid Greenman /* (7) The terminal from which the process was started */ 397f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 398c7d893deSDavid Greenman if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp) 39923d76283SPoul-Henning Kamp acct.ac_tty = dev2udev(p->p_pgrp->pg_session->s_ttyp->t_dev); 400c7d893deSDavid Greenman else 401f3732fd1SPoul-Henning Kamp acct.ac_tty = NODEV; 402f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 403c7d893deSDavid Greenman 404c7d893deSDavid Greenman /* (8) The boolean flags that tell how the process terminated, etc. */ 405c7d893deSDavid Greenman acct.ac_flag = p->p_acflag; 4067e653dbdSJohn Baldwin PROC_UNLOCK(p); 407c7d893deSDavid Greenman 408c7d893deSDavid Greenman /* 409b5afad71SDavid Greenman * Eliminate any file size rlimit. 410b5afad71SDavid Greenman */ 41191d5354aSJohn Baldwin newlim = lim_alloc(); 41291d5354aSJohn Baldwin PROC_LOCK(p); 41391d5354aSJohn Baldwin oldlim = p->p_limit; 41491d5354aSJohn Baldwin lim_copy(newlim, oldlim); 41591d5354aSJohn Baldwin newlim->pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 41691d5354aSJohn Baldwin p->p_limit = newlim; 41791d5354aSJohn Baldwin PROC_UNLOCK(p); 41891d5354aSJohn Baldwin lim_free(oldlim); 419b5afad71SDavid Greenman 42001e3f3aeSBruce Evans /* 42101e3f3aeSBruce Evans * Write the accounting information to the file. 42201e3f3aeSBruce Evans */ 42371909edeSRobert Watson vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount); 42471909edeSRobert Watson VOP_LEASE(acct_vp, td, acct_cred, LEASE_WRITE); 42571909edeSRobert Watson ret = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct, sizeof (acct), 42671909edeSRobert Watson (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acct_cred, NOCRED, 4274f39d5d5SAndrew R. Reiter (int *)0, td); 42871909edeSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 42971909edeSRobert Watson sx_sunlock(&acct_sx); 4304f39d5d5SAndrew R. Reiter return (ret); 431c7d893deSDavid Greenman } 432c7d893deSDavid Greenman 433c7d893deSDavid Greenman /* 434c7d893deSDavid Greenman * Encode_comp_t converts from ticks in seconds and microseconds 435c7d893deSDavid Greenman * to ticks in 1/AHZ seconds. The encoding is described in 436c7d893deSDavid Greenman * Leffler, et al., on page 63. 437c7d893deSDavid Greenman */ 438c7d893deSDavid Greenman 439c7d893deSDavid Greenman #define MANTSIZE 13 /* 13 bit mantissa. */ 440c7d893deSDavid Greenman #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */ 441c7d893deSDavid Greenman #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */ 442c7d893deSDavid Greenman 44387b6de2bSPoul-Henning Kamp static comp_t 44471909edeSRobert Watson encode_comp_t(u_long s, u_long us) 445c7d893deSDavid Greenman { 446c7d893deSDavid Greenman int exp, rnd; 447c7d893deSDavid Greenman 448c7d893deSDavid Greenman exp = 0; 449c7d893deSDavid Greenman rnd = 0; 450c7d893deSDavid Greenman s *= AHZ; 451c7d893deSDavid Greenman s += us / (1000000 / AHZ); /* Maximize precision. */ 452c7d893deSDavid Greenman 453c7d893deSDavid Greenman while (s > MAXFRACT) { 454c7d893deSDavid Greenman rnd = s & (1 << (EXPSIZE - 1)); /* Round up? */ 455c7d893deSDavid Greenman s >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */ 456c7d893deSDavid Greenman exp++; 457c7d893deSDavid Greenman } 458c7d893deSDavid Greenman 459c7d893deSDavid Greenman /* If we need to round up, do it (and handle overflow correctly). */ 460c7d893deSDavid Greenman if (rnd && (++s > MAXFRACT)) { 461c7d893deSDavid Greenman s >>= EXPSIZE; 462c7d893deSDavid Greenman exp++; 463c7d893deSDavid Greenman } 464c7d893deSDavid Greenman 465c7d893deSDavid Greenman /* Clean it up and polish it off. */ 466c7d893deSDavid Greenman exp <<= MANTSIZE; /* Shift the exponent into place */ 467c7d893deSDavid Greenman exp += s; /* and add on the mantissa. */ 468c7d893deSDavid Greenman return (exp); 469c7d893deSDavid Greenman } 470c7d893deSDavid Greenman 471c7d893deSDavid Greenman /* 472c7d893deSDavid Greenman * Periodically check the filesystem to see if accounting 473c7d893deSDavid Greenman * should be turned on or off. Beware the case where the vnode 474c7d893deSDavid Greenman * has been vgone()'d out from underneath us, e.g. when the file 475c7d893deSDavid Greenman * system containing the accounting file has been forcibly unmounted. 476c7d893deSDavid Greenman */ 477df8bae1dSRodney W. Grimes /* ARGSUSED */ 47887b6de2bSPoul-Henning Kamp static void 479505a1493SJohn Baldwin acctwatch(void) 480df8bae1dSRodney W. Grimes { 481df8bae1dSRodney W. Grimes struct statfs sb; 48271909edeSRobert Watson int vfslocked; 483df8bae1dSRodney W. Grimes 484505a1493SJohn Baldwin sx_assert(&acct_sx, SX_XLOCKED); 485505a1493SJohn Baldwin 486505a1493SJohn Baldwin /* 487505a1493SJohn Baldwin * If accounting was disabled before our kthread was scheduled, 488505a1493SJohn Baldwin * then acct_vp might be NULL. If so, just ask our kthread to 489505a1493SJohn Baldwin * exit and return. 490505a1493SJohn Baldwin */ 491505a1493SJohn Baldwin if (acct_vp == NULL) { 492505a1493SJohn Baldwin acct_state |= ACCT_EXITREQ; 493c7d893deSDavid Greenman return; 494c7d893deSDavid Greenman } 495505a1493SJohn Baldwin 496505a1493SJohn Baldwin /* 497505a1493SJohn Baldwin * If our vnode is no longer valid, tear it down and signal the 498505a1493SJohn Baldwin * accounting thread to die. 499505a1493SJohn Baldwin */ 500505a1493SJohn Baldwin vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount); 501505a1493SJohn Baldwin if (acct_vp->v_type == VBAD) { 502505a1493SJohn Baldwin (void) acct_disable(NULL); 503505a1493SJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 504505a1493SJohn Baldwin acct_state |= ACCT_EXITREQ; 505505a1493SJohn Baldwin return; 506505a1493SJohn Baldwin } 507505a1493SJohn Baldwin 50871909edeSRobert Watson /* 50971909edeSRobert Watson * Stopping here is better than continuing, maybe it will be VBAD 51071909edeSRobert Watson * next time around. 51171909edeSRobert Watson */ 51271909edeSRobert Watson if (VFS_STATFS(acct_vp->v_mount, &sb, curthread) < 0) { 51371909edeSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 51471909edeSRobert Watson return; 51571909edeSRobert Watson } 51671909edeSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 51771909edeSRobert Watson if (acct_suspended) { 51871909edeSRobert Watson if (sb.f_bavail > (int64_t)(acctresume * sb.f_blocks / 51971909edeSRobert Watson 100)) { 52071909edeSRobert Watson acct_suspended = 0; 521df8bae1dSRodney W. Grimes log(LOG_NOTICE, "Accounting resumed\n"); 522df8bae1dSRodney W. Grimes } 523996c772fSJohn Dyson } else { 52471909edeSRobert Watson if (sb.f_bavail <= (int64_t)(acctsuspend * sb.f_blocks / 52571909edeSRobert Watson 100)) { 52671909edeSRobert Watson acct_suspended = 1; 527df8bae1dSRodney W. Grimes log(LOG_NOTICE, "Accounting suspended\n"); 528df8bae1dSRodney W. Grimes } 529996c772fSJohn Dyson } 530505a1493SJohn Baldwin } 531505a1493SJohn Baldwin 532505a1493SJohn Baldwin /* 533505a1493SJohn Baldwin * The main loop for the dedicated kernel thread that periodically calls 534505a1493SJohn Baldwin * acctwatch(). 535505a1493SJohn Baldwin */ 536505a1493SJohn Baldwin static void 537505a1493SJohn Baldwin acct_thread(void *dummy) 538505a1493SJohn Baldwin { 539505a1493SJohn Baldwin u_char pri; 540505a1493SJohn Baldwin 541505a1493SJohn Baldwin /* This is a low-priority kernel thread. */ 542505a1493SJohn Baldwin pri = PRI_MAX_KERN; 543505a1493SJohn Baldwin mtx_lock_spin(&sched_lock); 544505a1493SJohn Baldwin sched_prio(curthread, pri); 545505a1493SJohn Baldwin mtx_unlock_spin(&sched_lock); 546505a1493SJohn Baldwin 547505a1493SJohn Baldwin /* If another accounting kthread is already running, just die. */ 548505a1493SJohn Baldwin sx_xlock(&acct_sx); 549505a1493SJohn Baldwin if (acct_state & ACCT_RUNNING) { 55071909edeSRobert Watson sx_xunlock(&acct_sx); 551505a1493SJohn Baldwin kthread_exit(0); 552505a1493SJohn Baldwin } 553505a1493SJohn Baldwin acct_state |= ACCT_RUNNING; 554505a1493SJohn Baldwin 555505a1493SJohn Baldwin /* Loop until we are asked to exit. */ 556505a1493SJohn Baldwin while (!(acct_state & ACCT_EXITREQ)) { 557505a1493SJohn Baldwin 558505a1493SJohn Baldwin /* Perform our periodic checks. */ 559505a1493SJohn Baldwin acctwatch(); 560505a1493SJohn Baldwin 561505a1493SJohn Baldwin /* 562505a1493SJohn Baldwin * We check this flag again before sleeping since the 563505a1493SJohn Baldwin * acctwatch() might have shut down accounting and asked us 564505a1493SJohn Baldwin * to exit. 565505a1493SJohn Baldwin */ 566505a1493SJohn Baldwin if (!(acct_state & ACCT_EXITREQ)) { 567505a1493SJohn Baldwin sx_xunlock(&acct_sx); 568505a1493SJohn Baldwin tsleep(&acct_state, pri, "-", acctchkfreq * hz); 569505a1493SJohn Baldwin sx_xlock(&acct_sx); 570505a1493SJohn Baldwin } 571505a1493SJohn Baldwin } 572505a1493SJohn Baldwin 573505a1493SJohn Baldwin /* 574505a1493SJohn Baldwin * Acknowledge the exit request and shutdown. We clear both the 575505a1493SJohn Baldwin * exit request and running flags. 576505a1493SJohn Baldwin */ 577505a1493SJohn Baldwin acct_state = 0; 578505a1493SJohn Baldwin sx_xunlock(&acct_sx); 579505a1493SJohn Baldwin kthread_exit(0); 580df8bae1dSRodney W. Grimes } 581