1df8bae1dSRodney W. Grimes /*- 2df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause 3df57947fSPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 7fcdc50ebSRobert Watson * Copyright (c) 2005 Robert N. M. Watson 8fcdc50ebSRobert Watson * All rights reserved. 9fcdc50ebSRobert Watson * 10df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 11df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 12df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 13df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 14df8bae1dSRodney W. Grimes * 15fcdc50ebSRobert Watson * Redistribution and use in source and binary forms, with or without 16fcdc50ebSRobert Watson * modification, are permitted provided that the following conditions 17fcdc50ebSRobert Watson * are met: 18fcdc50ebSRobert Watson * 1. Redistributions of source code must retain the above copyright 19fcdc50ebSRobert Watson * notice, this list of conditions and the following disclaimer. 20fcdc50ebSRobert Watson * 2. Redistributions in binary form must reproduce the above copyright 21fcdc50ebSRobert Watson * notice, this list of conditions and the following disclaimer in the 22fcdc50ebSRobert Watson * documentation and/or other materials provided with the distribution. 2369a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors 24fcdc50ebSRobert Watson * may be used to endorse or promote products derived from this software 25fcdc50ebSRobert Watson * without specific prior written permission. 26fcdc50ebSRobert Watson * 27fcdc50ebSRobert Watson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28fcdc50ebSRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29fcdc50ebSRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30fcdc50ebSRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31fcdc50ebSRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32fcdc50ebSRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33fcdc50ebSRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34fcdc50ebSRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35fcdc50ebSRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36fcdc50ebSRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37fcdc50ebSRobert Watson * SUCH DAMAGE. 38fcdc50ebSRobert Watson * 3971909edeSRobert Watson * Copyright (c) 1994 Christopher G. Demetriou 4071909edeSRobert Watson * 41df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 42df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 43df8bae1dSRodney W. Grimes * are met: 44df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 45df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 46df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 47df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 48df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 49df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 50df8bae1dSRodney W. Grimes * must display the following acknowledgement: 51df8bae1dSRodney W. Grimes * This product includes software developed by the University of 52df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 53df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 54df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 55df8bae1dSRodney W. Grimes * without specific prior written permission. 56df8bae1dSRodney W. Grimes * 57df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 58df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 59df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 60df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 61df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 62df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 63df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 64df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 65df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 66df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 67df8bae1dSRodney W. Grimes * SUCH DAMAGE. 68df8bae1dSRodney W. Grimes * 69c7d893deSDavid Greenman * @(#)kern_acct.c 8.1 (Berkeley) 6/14/93 70df8bae1dSRodney W. Grimes */ 71df8bae1dSRodney W. Grimes 72677b542eSDavid E. O'Brien #include <sys/cdefs.h> 73677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 74677b542eSDavid E. O'Brien 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> 8172de1b37SDiomidis Spinellis #include <sys/limits.h> 82fb919e4dSMark Murray #include <sys/lock.h> 832f304845SKonstantin Belousov #include <sys/malloc.h> 84df8bae1dSRodney W. Grimes #include <sys/mount.h> 85b0864d13SJohn Baldwin #include <sys/mutex.h> 86c7d893deSDavid Greenman #include <sys/namei.h> 87acd3428bSRobert Watson #include <sys/priv.h> 88b0864d13SJohn Baldwin #include <sys/proc.h> 89c7d893deSDavid Greenman #include <sys/resourcevar.h> 90505a1493SJohn Baldwin #include <sys/sched.h> 91b0864d13SJohn Baldwin #include <sys/sx.h> 92b0864d13SJohn Baldwin #include <sys/sysctl.h> 93b0864d13SJohn Baldwin #include <sys/sysent.h> 94b0864d13SJohn Baldwin #include <sys/syslog.h> 95b0864d13SJohn Baldwin #include <sys/sysproto.h> 96c7d893deSDavid Greenman #include <sys/tty.h> 97b0864d13SJohn Baldwin #include <sys/vnode.h> 98df8bae1dSRodney W. Grimes 99aed55708SRobert Watson #include <security/mac/mac_framework.h> 100aed55708SRobert Watson 10169921123SKonstantin Belousov _Static_assert(sizeof(struct acctv3) - offsetof(struct acctv3, ac_trailer) == 10269921123SKonstantin Belousov sizeof(struct acctv2) - offsetof(struct acctv2, ac_trailer), "trailer"); 10369921123SKonstantin Belousov _Static_assert(sizeof(struct acctv3) - offsetof(struct acctv3, ac_len2) == 10469921123SKonstantin Belousov sizeof(struct acctv2) - offsetof(struct acctv2, ac_len2), "len2"); 10569921123SKonstantin Belousov 106df8bae1dSRodney W. Grimes /* 107c7d893deSDavid Greenman * The routines implemented in this file are described in: 108c7d893deSDavid Greenman * Leffler, et al.: The Design and Implementation of the 4.3BSD 109c7d893deSDavid Greenman * UNIX Operating System (Addison Welley, 1989) 110c7d893deSDavid Greenman * on pages 62-63. 111fdbe5babSDiomidis Spinellis * On May 2007 the historic 3 bits base 8 exponent, 13 bit fraction 112fdbe5babSDiomidis Spinellis * compt_t representation described in the above reference was replaced 113fdbe5babSDiomidis Spinellis * with that of IEEE-754 floats. 114c7d893deSDavid Greenman * 115c7d893deSDavid Greenman * Arguably, to simplify accounting operations, this mechanism should 116c7d893deSDavid Greenman * be replaced by one in which an accounting log file (similar to /dev/klog) 117c7d893deSDavid Greenman * is read by a user process, etc. However, that has its own problems. 118df8bae1dSRodney W. Grimes */ 119df8bae1dSRodney W. Grimes 120fdbe5babSDiomidis Spinellis /* Floating point definitions from <float.h>. */ 121fdbe5babSDiomidis Spinellis #define FLT_MANT_DIG 24 /* p */ 122fdbe5babSDiomidis Spinellis #define FLT_MAX_EXP 128 /* emax */ 123fdbe5babSDiomidis Spinellis 124df8bae1dSRodney W. Grimes /* 125c7d893deSDavid Greenman * Internal accounting functions. 126c7d893deSDavid Greenman * The former's operation is described in Leffler, et al., and the latter 127c7d893deSDavid Greenman * was provided by UCB with the 4.4BSD-Lite release 128df8bae1dSRodney W. Grimes */ 129fdbe5babSDiomidis Spinellis static uint32_t encode_timeval(struct timeval); 130fdbe5babSDiomidis Spinellis static uint32_t encode_long(long); 131505a1493SJohn Baldwin static void acctwatch(void); 132505a1493SJohn Baldwin static void acct_thread(void *); 133b8cb2346SJohn Baldwin static int acct_disable(struct thread *, int); 134ab36c067SJustin T. Gibbs 135ab36c067SJustin T. Gibbs /* 1365b606744SJohan Karlsson * Accounting vnode pointer, saved vnode pointer, and flags for each. 13771909edeSRobert Watson * acct_sx protects against changes to the active vnode and credentials 13871909edeSRobert Watson * while accounting records are being committed to disk. 139c7d893deSDavid Greenman */ 140101581b0SRobert Watson static int acct_configured; 14171909edeSRobert Watson static int acct_suspended; 14271909edeSRobert Watson static struct vnode *acct_vp; 14371909edeSRobert Watson static struct ucred *acct_cred; 14471909edeSRobert Watson static int acct_flags; 14571909edeSRobert Watson static struct sx acct_sx; 146df8bae1dSRodney W. Grimes 14771909edeSRobert Watson SX_SYSINIT(acct, &acct_sx, "acct_sx"); 1484f39d5d5SAndrew R. Reiter 149df8bae1dSRodney W. Grimes /* 150505a1493SJohn Baldwin * State of the accounting kthread. 151505a1493SJohn Baldwin */ 152505a1493SJohn Baldwin static int acct_state; 153505a1493SJohn Baldwin 154505a1493SJohn Baldwin #define ACCT_RUNNING 1 /* Accounting kthread is running. */ 155505a1493SJohn Baldwin #define ACCT_EXITREQ 2 /* Accounting kthread should exit. */ 156505a1493SJohn Baldwin 157505a1493SJohn Baldwin /* 158df8bae1dSRodney W. Grimes * Values associated with enabling and disabling accounting 159df8bae1dSRodney W. Grimes */ 16087b6de2bSPoul-Henning Kamp static int acctsuspend = 2; /* stop accounting when < 2% free space left */ 16187b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW, 16247fdd692SNeil Blakey-Milner &acctsuspend, 0, "percentage of free disk space below which accounting stops"); 16387b6de2bSPoul-Henning Kamp 16487b6de2bSPoul-Henning Kamp static int acctresume = 4; /* resume when free space risen to > 4% */ 16587b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW, 16647fdd692SNeil Blakey-Milner &acctresume, 0, "percentage of free disk space above which accounting resumes"); 16787b6de2bSPoul-Henning Kamp 16887b6de2bSPoul-Henning Kamp static int acctchkfreq = 15; /* frequency (in seconds) to check space */ 169222fdf4bSJohn Baldwin 170222fdf4bSJohn Baldwin static int 171222fdf4bSJohn Baldwin sysctl_acct_chkfreq(SYSCTL_HANDLER_ARGS) 172222fdf4bSJohn Baldwin { 173222fdf4bSJohn Baldwin int error, value; 174222fdf4bSJohn Baldwin 175222fdf4bSJohn Baldwin /* Write out the old value. */ 176222fdf4bSJohn Baldwin error = SYSCTL_OUT(req, &acctchkfreq, sizeof(int)); 177222fdf4bSJohn Baldwin if (error || req->newptr == NULL) 178222fdf4bSJohn Baldwin return (error); 179222fdf4bSJohn Baldwin 180222fdf4bSJohn Baldwin /* Read in and verify the new value. */ 181222fdf4bSJohn Baldwin error = SYSCTL_IN(req, &value, sizeof(int)); 182222fdf4bSJohn Baldwin if (error) 183222fdf4bSJohn Baldwin return (error); 184222fdf4bSJohn Baldwin if (value <= 0) 185222fdf4bSJohn Baldwin return (EINVAL); 186222fdf4bSJohn Baldwin acctchkfreq = value; 187222fdf4bSJohn Baldwin return (0); 188222fdf4bSJohn Baldwin } 1897029da5cSPawel Biernacki SYSCTL_PROC(_kern, OID_AUTO, acct_chkfreq, 1907029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &acctchkfreq, 0, 1917029da5cSPawel Biernacki sysctl_acct_chkfreq, "I", 192222fdf4bSJohn Baldwin "frequency for checking the free space"); 193df8bae1dSRodney W. Grimes 194101581b0SRobert Watson SYSCTL_INT(_kern, OID_AUTO, acct_configured, CTLFLAG_RD, &acct_configured, 0, 195101581b0SRobert Watson "Accounting configured or not"); 196101581b0SRobert Watson 19771909edeSRobert Watson SYSCTL_INT(_kern, OID_AUTO, acct_suspended, CTLFLAG_RD, &acct_suspended, 0, 19871909edeSRobert Watson "Accounting suspended or not"); 19971909edeSRobert Watson 200df8bae1dSRodney W. Grimes /* 2010c14ff0eSRobert Watson * Accounting system call. Written based on the specification and previous 2020c14ff0eSRobert Watson * implementation done by Mark Tinguely. 203df8bae1dSRodney W. Grimes */ 204c7d893deSDavid Greenman int 2058451d0ddSKip Macy sys_acct(struct thread *td, struct acct_args *uap) 206c7d893deSDavid Greenman { 207c7d893deSDavid Greenman struct nameidata nd; 208*9bb84c23SKonstantin Belousov int error, flags, replacing; 209c7d893deSDavid Greenman 210acd3428bSRobert Watson error = priv_check(td, PRIV_ACCT); 211797f2d22SPoul-Henning Kamp if (error) 21216e7bc7bSJohn Baldwin return (error); 213c7d893deSDavid Greenman 214c7d893deSDavid Greenman /* 215c7d893deSDavid Greenman * If accounting is to be started to a file, open that file for 21611178ee4SJohn Baldwin * appending and make sure it's a 'normal'. 217c7d893deSDavid Greenman */ 218d1e405c5SAlfred Perlstein if (uap->path != NULL) { 2195050aa86SKonstantin Belousov NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, 220d3778141SRobert Watson UIO_USERSPACE, uap->path, td); 22192da2e76SJohan Karlsson flags = FWRITE | O_APPEND; 2229e223287SKonstantin Belousov error = vn_open(&nd, &flags, 0, NULL); 223797f2d22SPoul-Henning Kamp if (error) 22411178ee4SJohn Baldwin return (error); 225762e6b85SEivind Eklund NDFREE(&nd, NDF_ONLY_PNBUF); 226e5e820fdSRobert Watson #ifdef MAC 22730d239bcSRobert Watson error = mac_system_check_acct(td->td_ucred, nd.ni_vp); 228e5e820fdSRobert Watson if (error) { 229b249ce48SMateusz Guzik VOP_UNLOCK(nd.ni_vp); 230e5e820fdSRobert Watson vn_close(nd.ni_vp, flags, td->td_ucred, td); 23111178ee4SJohn Baldwin return (error); 232e5e820fdSRobert Watson } 233e5e820fdSRobert Watson #endif 234b249ce48SMateusz Guzik VOP_UNLOCK(nd.ni_vp); 235c7d893deSDavid Greenman if (nd.ni_vp->v_type != VREG) { 2365b606744SJohan Karlsson vn_close(nd.ni_vp, flags, td->td_ucred, td); 23711178ee4SJohn Baldwin return (EACCES); 238c7d893deSDavid Greenman } 239e5e820fdSRobert Watson #ifdef MAC 240e5e820fdSRobert Watson } else { 24130d239bcSRobert Watson error = mac_system_check_acct(td->td_ucred, NULL); 242e5e820fdSRobert Watson if (error) 24311178ee4SJohn Baldwin return (error); 244e5e820fdSRobert Watson #endif 245c7d893deSDavid Greenman } 246c7d893deSDavid Greenman 24771909edeSRobert Watson /* 24871909edeSRobert Watson * Disallow concurrent access to the accounting vnode while we swap 24971909edeSRobert Watson * it out, in order to prevent access after close. 25071909edeSRobert Watson */ 25171909edeSRobert Watson sx_xlock(&acct_sx); 25201e3f3aeSBruce Evans 253c7d893deSDavid Greenman /* 254b8cb2346SJohn Baldwin * Don't log spurious disable/enable messages if we are 255b8cb2346SJohn Baldwin * switching from one accounting file to another due to log 256b8cb2346SJohn Baldwin * rotation. 257b8cb2346SJohn Baldwin */ 258b8cb2346SJohn Baldwin replacing = (acct_vp != NULL && uap->path != NULL); 259b8cb2346SJohn Baldwin 260b8cb2346SJohn Baldwin /* 261c7d893deSDavid Greenman * If accounting was previously enabled, kill the old space-watcher, 26271909edeSRobert Watson * close the file, and (if no new file was specified, leave). Reset 26371909edeSRobert Watson * the suspended state regardless of whether accounting remains 26471909edeSRobert Watson * enabled. 265c7d893deSDavid Greenman */ 26671909edeSRobert Watson acct_suspended = 0; 2675050aa86SKonstantin Belousov if (acct_vp != NULL) 268b8cb2346SJohn Baldwin error = acct_disable(td, !replacing); 269d1e405c5SAlfred Perlstein if (uap->path == NULL) { 270505a1493SJohn Baldwin if (acct_state & ACCT_RUNNING) { 271505a1493SJohn Baldwin acct_state |= ACCT_EXITREQ; 272505a1493SJohn Baldwin wakeup(&acct_state); 273505a1493SJohn Baldwin } 27471909edeSRobert Watson sx_xunlock(&acct_sx); 27511178ee4SJohn Baldwin return (error); 276b4dcc46aSAndrew R. Reiter } 277c7d893deSDavid Greenman 278c7d893deSDavid Greenman /* 279c7d893deSDavid Greenman * Save the new accounting file vnode, and schedule the new 280c7d893deSDavid Greenman * free space watcher. 281c7d893deSDavid Greenman */ 28271909edeSRobert Watson acct_vp = nd.ni_vp; 28371909edeSRobert Watson acct_cred = crhold(td->td_ucred); 28471909edeSRobert Watson acct_flags = flags; 285505a1493SJohn Baldwin if (acct_state & ACCT_RUNNING) 286505a1493SJohn Baldwin acct_state &= ~ACCT_EXITREQ; 287505a1493SJohn Baldwin else { 288505a1493SJohn Baldwin /* 289505a1493SJohn Baldwin * Try to start up an accounting kthread. We may start more 290505a1493SJohn Baldwin * than one, but if so the extras will commit suicide as 291505a1493SJohn Baldwin * soon as they start up. 292505a1493SJohn Baldwin */ 2933745c395SJulian Elischer error = kproc_create(acct_thread, NULL, NULL, 0, 0, 294505a1493SJohn Baldwin "accounting"); 295505a1493SJohn Baldwin if (error) { 2964a3c4f41SMateusz Guzik (void) acct_disable(td, 0); 297505a1493SJohn Baldwin sx_xunlock(&acct_sx); 298505a1493SJohn Baldwin log(LOG_NOTICE, "Unable to start accounting thread\n"); 29911178ee4SJohn Baldwin return (error); 300505a1493SJohn Baldwin } 301505a1493SJohn Baldwin } 302101581b0SRobert Watson acct_configured = 1; 30371909edeSRobert Watson sx_xunlock(&acct_sx); 304b8cb2346SJohn Baldwin if (!replacing) 30548719ca7SBosko Milekic log(LOG_NOTICE, "Accounting enabled\n"); 306c7d893deSDavid Greenman return (error); 307c7d893deSDavid Greenman } 308c7d893deSDavid Greenman 309c7d893deSDavid Greenman /* 310505a1493SJohn Baldwin * Disable currently in-progress accounting by closing the vnode, dropping 311505a1493SJohn Baldwin * our reference to the credential, and clearing the vnode's flags. 312505a1493SJohn Baldwin */ 313505a1493SJohn Baldwin static int 314b8cb2346SJohn Baldwin acct_disable(struct thread *td, int logging) 315505a1493SJohn Baldwin { 316505a1493SJohn Baldwin int error; 317505a1493SJohn Baldwin 318505a1493SJohn Baldwin sx_assert(&acct_sx, SX_XLOCKED); 319505a1493SJohn Baldwin error = vn_close(acct_vp, acct_flags, acct_cred, td); 320505a1493SJohn Baldwin crfree(acct_cred); 321101581b0SRobert Watson acct_configured = 0; 322505a1493SJohn Baldwin acct_vp = NULL; 323505a1493SJohn Baldwin acct_cred = NULL; 324505a1493SJohn Baldwin acct_flags = 0; 325b8cb2346SJohn Baldwin if (logging) 326505a1493SJohn Baldwin log(LOG_NOTICE, "Accounting disabled\n"); 327505a1493SJohn Baldwin return (error); 328505a1493SJohn Baldwin } 329505a1493SJohn Baldwin 330505a1493SJohn Baldwin /* 331c7d893deSDavid Greenman * Write out process accounting information, on process exit. 332c7d893deSDavid Greenman * Data to be written out is specified in Leffler, et al. 333c7d893deSDavid Greenman * and are enumerated below. (They're also noted in the system 334c7d893deSDavid Greenman * "acct.h" header file.) 335c7d893deSDavid Greenman */ 336c7d893deSDavid Greenman int 33771909edeSRobert Watson acct_process(struct thread *td) 338c7d893deSDavid Greenman { 33969921123SKonstantin Belousov struct acctv3 acct; 340c7d893deSDavid Greenman struct timeval ut, st, tmp; 34101e3f3aeSBruce Evans struct proc *p; 3421c4bcd05SJeff Roberson struct rusage ru; 3435050aa86SKonstantin Belousov int t, ret; 3444f39d5d5SAndrew R. Reiter 3452b05b557SRobert Watson /* 3462b05b557SRobert Watson * Lockless check of accounting condition before doing the hard 3472b05b557SRobert Watson * work. 3482b05b557SRobert Watson */ 34971909edeSRobert Watson if (acct_vp == NULL || acct_suspended) 3502b05b557SRobert Watson return (0); 3512b05b557SRobert Watson 35271909edeSRobert Watson sx_slock(&acct_sx); 353c7d893deSDavid Greenman 3542b05b557SRobert Watson /* 3552b05b557SRobert Watson * If accounting isn't enabled, don't bother. Have to check again 3562b05b557SRobert Watson * once we own the lock in case we raced with disabling of accounting 3572b05b557SRobert Watson * by another thread. 3582b05b557SRobert Watson */ 35971909edeSRobert Watson if (acct_vp == NULL || acct_suspended) { 36071909edeSRobert Watson sx_sunlock(&acct_sx); 361c7d893deSDavid Greenman return (0); 3624f39d5d5SAndrew R. Reiter } 363c7d893deSDavid Greenman 36401e3f3aeSBruce Evans p = td->td_proc; 365*9bb84c23SKonstantin Belousov td->td_pflags2 |= TDP2_ACCT; 36601e3f3aeSBruce Evans 367c7d893deSDavid Greenman /* 368c7d893deSDavid Greenman * Get process accounting information. 369c7d893deSDavid Greenman */ 370c7d893deSDavid Greenman 37140572ab3SEd Schouten sx_slock(&proctree_lock); 3727e653dbdSJohn Baldwin PROC_LOCK(p); 37340572ab3SEd Schouten 37440572ab3SEd Schouten /* (1) The terminal from which the process was started */ 37540572ab3SEd Schouten if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp) 37640572ab3SEd Schouten acct.ac_tty = tty_udev(p->p_pgrp->pg_session->s_ttyp); 37740572ab3SEd Schouten else 37840572ab3SEd Schouten acct.ac_tty = NODEV; 37940572ab3SEd Schouten sx_sunlock(&proctree_lock); 38040572ab3SEd Schouten 38140572ab3SEd Schouten /* (2) The name of the command that ran */ 382c7d893deSDavid Greenman bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm); 383c7d893deSDavid Greenman 38440572ab3SEd Schouten /* (3) The amount of user and system time that was used */ 385a1fe14bcSAttilio Rao rufetchcalc(p, &ru, &ut, &st); 386fdbe5babSDiomidis Spinellis acct.ac_utime = encode_timeval(ut); 387fdbe5babSDiomidis Spinellis acct.ac_stime = encode_timeval(st); 388c7d893deSDavid Greenman 38940572ab3SEd Schouten /* (4) The elapsed time the command ran (and its starting time) */ 390584b675eSKonstantin Belousov getboottime(&tmp); 39187ccef7bSDag-Erling Smørgrav timevaladd(&tmp, &p->p_stats->p_start); 39287ccef7bSDag-Erling Smørgrav acct.ac_btime = tmp.tv_sec; 39387ccef7bSDag-Erling Smørgrav microuptime(&tmp); 394c7d893deSDavid Greenman timevalsub(&tmp, &p->p_stats->p_start); 395fdbe5babSDiomidis Spinellis acct.ac_etime = encode_timeval(tmp); 396c7d893deSDavid Greenman 39740572ab3SEd Schouten /* (5) The average amount of memory used */ 398c7d893deSDavid Greenman tmp = ut; 399c7d893deSDavid Greenman timevaladd(&tmp, &st); 400fdbe5babSDiomidis Spinellis /* Convert tmp (i.e. u + s) into hz units to match ru_i*. */ 401c7d893deSDavid Greenman t = tmp.tv_sec * hz + tmp.tv_usec / tick; 402c7d893deSDavid Greenman if (t) 4031c4bcd05SJeff Roberson acct.ac_mem = encode_long((ru.ru_ixrss + ru.ru_idrss + 4041c4bcd05SJeff Roberson + ru.ru_isrss) / t); 405c7d893deSDavid Greenman else 406c7d893deSDavid Greenman acct.ac_mem = 0; 407c7d893deSDavid Greenman 40840572ab3SEd Schouten /* (6) The number of disk I/O operations done */ 4091c4bcd05SJeff Roberson acct.ac_io = encode_long(ru.ru_inblock + ru.ru_oublock); 410c7d893deSDavid Greenman 41140572ab3SEd Schouten /* (7) The UID and GID of the process */ 412b1fc0ec1SRobert Watson acct.ac_uid = p->p_ucred->cr_ruid; 413b1fc0ec1SRobert Watson acct.ac_gid = p->p_ucred->cr_rgid; 414c7d893deSDavid Greenman 415c7d893deSDavid Greenman /* (8) The boolean flags that tell how the process terminated, etc. */ 416fdbe5babSDiomidis Spinellis acct.ac_flagx = p->p_acflag; 417c7d893deSDavid Greenman 418*9bb84c23SKonstantin Belousov PROC_UNLOCK(p); 419*9bb84c23SKonstantin Belousov 420fdbe5babSDiomidis Spinellis /* Setup ancillary structure fields. */ 421fdbe5babSDiomidis Spinellis acct.ac_flagx |= ANVER; 422fdbe5babSDiomidis Spinellis acct.ac_zero = 0; 42369921123SKonstantin Belousov acct.ac_version = 3; 424fdbe5babSDiomidis Spinellis acct.ac_len = acct.ac_len2 = sizeof(acct); 425fdbe5babSDiomidis Spinellis 426c7d893deSDavid Greenman /* 42701e3f3aeSBruce Evans * Write the accounting information to the file. 42801e3f3aeSBruce Evans */ 42971909edeSRobert Watson ret = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct, sizeof (acct), 43071909edeSRobert Watson (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acct_cred, NOCRED, 43101ed1748SKonstantin Belousov NULL, td); 43271909edeSRobert Watson sx_sunlock(&acct_sx); 433*9bb84c23SKonstantin Belousov td->td_pflags2 &= ~TDP2_ACCT; 4344f39d5d5SAndrew R. Reiter return (ret); 435c7d893deSDavid Greenman } 436c7d893deSDavid Greenman 437fdbe5babSDiomidis Spinellis /* FLOAT_CONVERSION_START (Regression testing; don't remove this line.) */ 438fdbe5babSDiomidis Spinellis 439fdbe5babSDiomidis Spinellis /* Convert timevals and longs into IEEE-754 bit patterns. */ 440fdbe5babSDiomidis Spinellis 441fdbe5babSDiomidis Spinellis /* Mantissa mask (MSB is implied, so subtract 1). */ 442fdbe5babSDiomidis Spinellis #define MANT_MASK ((1 << (FLT_MANT_DIG - 1)) - 1) 443fdbe5babSDiomidis Spinellis 444c7d893deSDavid Greenman /* 445fdbe5babSDiomidis Spinellis * We calculate integer values to a precision of approximately 446fdbe5babSDiomidis Spinellis * 28 bits. 447fdbe5babSDiomidis Spinellis * This is high-enough precision to fill the 24 float bits 448fdbe5babSDiomidis Spinellis * and low-enough to avoid overflowing the 32 int bits. 449c7d893deSDavid Greenman */ 450fdbe5babSDiomidis Spinellis #define CALC_BITS 28 451c7d893deSDavid Greenman 452fdbe5babSDiomidis Spinellis /* log_2(1000000). */ 453fdbe5babSDiomidis Spinellis #define LOG2_1M 20 454c7d893deSDavid Greenman 455fdbe5babSDiomidis Spinellis /* 456fdbe5babSDiomidis Spinellis * Convert the elements of a timeval into a 32-bit word holding 457fdbe5babSDiomidis Spinellis * the bits of a IEEE-754 float. 458fdbe5babSDiomidis Spinellis * The float value represents the timeval's value in microsecond units. 459fdbe5babSDiomidis Spinellis */ 460fdbe5babSDiomidis Spinellis static uint32_t 461fdbe5babSDiomidis Spinellis encode_timeval(struct timeval tv) 462c7d893deSDavid Greenman { 463fdbe5babSDiomidis Spinellis int log2_s; 464d75a7880SEnji Cooper int val, exp; /* Unnormalized value and exponent */ 465d75a7880SEnji Cooper int norm_exp; /* Normalized exponent */ 466fdbe5babSDiomidis Spinellis int shift; 467c7d893deSDavid Greenman 468fdbe5babSDiomidis Spinellis /* 469fdbe5babSDiomidis Spinellis * First calculate value and exponent to about CALC_BITS precision. 470fdbe5babSDiomidis Spinellis * Note that the following conditionals have been ordered so that 471fdbe5babSDiomidis Spinellis * the most common cases appear first. 472fdbe5babSDiomidis Spinellis */ 473fdbe5babSDiomidis Spinellis if (tv.tv_sec == 0) { 474fdbe5babSDiomidis Spinellis if (tv.tv_usec == 0) 475fdbe5babSDiomidis Spinellis return (0); 476d75a7880SEnji Cooper exp = 0; 477fdbe5babSDiomidis Spinellis val = tv.tv_usec; 478fdbe5babSDiomidis Spinellis } else { 479fdbe5babSDiomidis Spinellis /* 480fdbe5babSDiomidis Spinellis * Calculate the value to a precision of approximately 481fdbe5babSDiomidis Spinellis * CALC_BITS. 482fdbe5babSDiomidis Spinellis */ 483fdbe5babSDiomidis Spinellis log2_s = fls(tv.tv_sec) - 1; 484fdbe5babSDiomidis Spinellis if (log2_s + LOG2_1M < CALC_BITS) { 485d75a7880SEnji Cooper exp = 0; 486fdbe5babSDiomidis Spinellis val = 1000000 * tv.tv_sec + tv.tv_usec; 487fdbe5babSDiomidis Spinellis } else { 488d75a7880SEnji Cooper exp = log2_s + LOG2_1M - CALC_BITS; 48960ae52f7SEd Schouten val = (unsigned int)(((uint64_t)1000000 * tv.tv_sec + 490d75a7880SEnji Cooper tv.tv_usec) >> exp); 491fdbe5babSDiomidis Spinellis } 492fdbe5babSDiomidis Spinellis } 493fdbe5babSDiomidis Spinellis /* Now normalize and pack the value into an IEEE-754 float. */ 494d75a7880SEnji Cooper norm_exp = fls(val) - 1; 495d75a7880SEnji Cooper shift = FLT_MANT_DIG - norm_exp - 1; 496fdbe5babSDiomidis Spinellis #ifdef ACCT_DEBUG 497fdbe5babSDiomidis Spinellis printf("val=%d exp=%d shift=%d log2(val)=%d\n", 498d75a7880SEnji Cooper val, exp, shift, norm_exp); 499d75a7880SEnji Cooper printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp, 500fdbe5babSDiomidis Spinellis ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK)); 501fdbe5babSDiomidis Spinellis #endif 502d75a7880SEnji Cooper return (((FLT_MAX_EXP - 1 + exp + norm_exp) << (FLT_MANT_DIG - 1)) | 503fdbe5babSDiomidis Spinellis ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK)); 504c7d893deSDavid Greenman } 505c7d893deSDavid Greenman 506fdbe5babSDiomidis Spinellis /* 507fdbe5babSDiomidis Spinellis * Convert a non-negative long value into the bit pattern of 508fdbe5babSDiomidis Spinellis * an IEEE-754 float value. 509fdbe5babSDiomidis Spinellis */ 510fdbe5babSDiomidis Spinellis static uint32_t 511fdbe5babSDiomidis Spinellis encode_long(long val) 512fdbe5babSDiomidis Spinellis { 513d75a7880SEnji Cooper int norm_exp; /* Normalized exponent */ 514fdbe5babSDiomidis Spinellis int shift; 515fdbe5babSDiomidis Spinellis 516fdbe5babSDiomidis Spinellis if (val == 0) 517fdbe5babSDiomidis Spinellis return (0); 51872de1b37SDiomidis Spinellis if (val < 0) { 51972de1b37SDiomidis Spinellis log(LOG_NOTICE, 520d5b6981eSDiomidis Spinellis "encode_long: negative value %ld in accounting record\n", 52172de1b37SDiomidis Spinellis val); 52272de1b37SDiomidis Spinellis val = LONG_MAX; 52372de1b37SDiomidis Spinellis } 524d75a7880SEnji Cooper norm_exp = fls(val) - 1; 525d75a7880SEnji Cooper shift = FLT_MANT_DIG - norm_exp - 1; 526fdbe5babSDiomidis Spinellis #ifdef ACCT_DEBUG 527fdbe5babSDiomidis Spinellis printf("val=%d shift=%d log2(val)=%d\n", 528d75a7880SEnji Cooper val, shift, norm_exp); 529d75a7880SEnji Cooper printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp, 530fdbe5babSDiomidis Spinellis ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK)); 531fdbe5babSDiomidis Spinellis #endif 532d75a7880SEnji Cooper return (((FLT_MAX_EXP - 1 + norm_exp) << (FLT_MANT_DIG - 1)) | 533fdbe5babSDiomidis Spinellis ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK)); 534c7d893deSDavid Greenman } 535c7d893deSDavid Greenman 536fdbe5babSDiomidis Spinellis /* FLOAT_CONVERSION_END (Regression testing; don't remove this line.) */ 537c7d893deSDavid Greenman 538c7d893deSDavid Greenman /* 539c7d893deSDavid Greenman * Periodically check the filesystem to see if accounting 540c7d893deSDavid Greenman * should be turned on or off. Beware the case where the vnode 541c7d893deSDavid Greenman * has been vgone()'d out from underneath us, e.g. when the file 542c7d893deSDavid Greenman * system containing the accounting file has been forcibly unmounted. 543c7d893deSDavid Greenman */ 544df8bae1dSRodney W. Grimes /* ARGSUSED */ 54587b6de2bSPoul-Henning Kamp static void 546505a1493SJohn Baldwin acctwatch(void) 547df8bae1dSRodney W. Grimes { 5482f304845SKonstantin Belousov struct statfs *sp; 549df8bae1dSRodney W. Grimes 550505a1493SJohn Baldwin sx_assert(&acct_sx, SX_XLOCKED); 551505a1493SJohn Baldwin 552505a1493SJohn Baldwin /* 553505a1493SJohn Baldwin * If accounting was disabled before our kthread was scheduled, 554505a1493SJohn Baldwin * then acct_vp might be NULL. If so, just ask our kthread to 555505a1493SJohn Baldwin * exit and return. 556505a1493SJohn Baldwin */ 557505a1493SJohn Baldwin if (acct_vp == NULL) { 558505a1493SJohn Baldwin acct_state |= ACCT_EXITREQ; 559c7d893deSDavid Greenman return; 560c7d893deSDavid Greenman } 561505a1493SJohn Baldwin 562505a1493SJohn Baldwin /* 563505a1493SJohn Baldwin * If our vnode is no longer valid, tear it down and signal the 564505a1493SJohn Baldwin * accounting thread to die. 565505a1493SJohn Baldwin */ 566505a1493SJohn Baldwin if (acct_vp->v_type == VBAD) { 567b8cb2346SJohn Baldwin (void) acct_disable(NULL, 1); 568505a1493SJohn Baldwin acct_state |= ACCT_EXITREQ; 569505a1493SJohn Baldwin return; 570505a1493SJohn Baldwin } 571505a1493SJohn Baldwin 57271909edeSRobert Watson /* 57371909edeSRobert Watson * Stopping here is better than continuing, maybe it will be VBAD 57471909edeSRobert Watson * next time around. 57571909edeSRobert Watson */ 5762f304845SKonstantin Belousov sp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 5772f304845SKonstantin Belousov if (VFS_STATFS(acct_vp->v_mount, sp) < 0) { 5782f304845SKonstantin Belousov free(sp, M_STATFS); 57971909edeSRobert Watson return; 5802f304845SKonstantin Belousov } 58171909edeSRobert Watson if (acct_suspended) { 5822f304845SKonstantin Belousov if (sp->f_bavail > (int64_t)(acctresume * sp->f_blocks / 58371909edeSRobert Watson 100)) { 58471909edeSRobert Watson acct_suspended = 0; 585df8bae1dSRodney W. Grimes log(LOG_NOTICE, "Accounting resumed\n"); 586df8bae1dSRodney W. Grimes } 587996c772fSJohn Dyson } else { 5882f304845SKonstantin Belousov if (sp->f_bavail <= (int64_t)(acctsuspend * sp->f_blocks / 58971909edeSRobert Watson 100)) { 59071909edeSRobert Watson acct_suspended = 1; 591df8bae1dSRodney W. Grimes log(LOG_NOTICE, "Accounting suspended\n"); 592df8bae1dSRodney W. Grimes } 593996c772fSJohn Dyson } 5942f304845SKonstantin Belousov free(sp, M_STATFS); 595505a1493SJohn Baldwin } 596505a1493SJohn Baldwin 597505a1493SJohn Baldwin /* 598505a1493SJohn Baldwin * The main loop for the dedicated kernel thread that periodically calls 599505a1493SJohn Baldwin * acctwatch(). 600505a1493SJohn Baldwin */ 601505a1493SJohn Baldwin static void 602505a1493SJohn Baldwin acct_thread(void *dummy) 603505a1493SJohn Baldwin { 604505a1493SJohn Baldwin u_char pri; 605505a1493SJohn Baldwin 606505a1493SJohn Baldwin /* This is a low-priority kernel thread. */ 607505a1493SJohn Baldwin pri = PRI_MAX_KERN; 608982d11f8SJeff Roberson thread_lock(curthread); 609505a1493SJohn Baldwin sched_prio(curthread, pri); 610982d11f8SJeff Roberson thread_unlock(curthread); 611505a1493SJohn Baldwin 612505a1493SJohn Baldwin /* If another accounting kthread is already running, just die. */ 613505a1493SJohn Baldwin sx_xlock(&acct_sx); 614505a1493SJohn Baldwin if (acct_state & ACCT_RUNNING) { 61571909edeSRobert Watson sx_xunlock(&acct_sx); 6163745c395SJulian Elischer kproc_exit(0); 617505a1493SJohn Baldwin } 618505a1493SJohn Baldwin acct_state |= ACCT_RUNNING; 619505a1493SJohn Baldwin 620505a1493SJohn Baldwin /* Loop until we are asked to exit. */ 621505a1493SJohn Baldwin while (!(acct_state & ACCT_EXITREQ)) { 622505a1493SJohn Baldwin /* Perform our periodic checks. */ 623505a1493SJohn Baldwin acctwatch(); 624505a1493SJohn Baldwin 625505a1493SJohn Baldwin /* 626505a1493SJohn Baldwin * We check this flag again before sleeping since the 627505a1493SJohn Baldwin * acctwatch() might have shut down accounting and asked us 628505a1493SJohn Baldwin * to exit. 629505a1493SJohn Baldwin */ 630505a1493SJohn Baldwin if (!(acct_state & ACCT_EXITREQ)) { 6316caa5f40SJohn Baldwin sx_sleep(&acct_state, &acct_sx, 0, "-", 6326caa5f40SJohn Baldwin acctchkfreq * hz); 633505a1493SJohn Baldwin } 634505a1493SJohn Baldwin } 635505a1493SJohn Baldwin 636505a1493SJohn Baldwin /* 637505a1493SJohn Baldwin * Acknowledge the exit request and shutdown. We clear both the 638505a1493SJohn Baldwin * exit request and running flags. 639505a1493SJohn Baldwin */ 640505a1493SJohn Baldwin acct_state = 0; 641505a1493SJohn Baldwin sx_xunlock(&acct_sx); 6423745c395SJulian Elischer kproc_exit(0); 643df8bae1dSRodney W. Grimes } 644