xref: /freebsd/sys/kern/kern_acct.c (revision 1c4bcd050a4f7163d68f608db860aef5fad81d6c)
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.
104fdbe5babSDiomidis Spinellis  * On May 2007 the historic 3 bits base 8 exponent, 13 bit fraction
105fdbe5babSDiomidis Spinellis  * compt_t representation described in the above reference was replaced
106fdbe5babSDiomidis Spinellis  * with that of IEEE-754 floats.
107c7d893deSDavid Greenman  *
108c7d893deSDavid Greenman  * Arguably, to simplify accounting operations, this mechanism should
109c7d893deSDavid Greenman  * be replaced by one in which an accounting log file (similar to /dev/klog)
110c7d893deSDavid Greenman  * is read by a user process, etc.  However, that has its own problems.
111df8bae1dSRodney W. Grimes  */
112df8bae1dSRodney W. Grimes 
113fdbe5babSDiomidis Spinellis /* Floating point definitions from <float.h>. */
114fdbe5babSDiomidis Spinellis #define FLT_MANT_DIG    24              /* p */
115fdbe5babSDiomidis Spinellis #define FLT_MAX_EXP     128             /* emax */
116fdbe5babSDiomidis Spinellis 
117df8bae1dSRodney W. Grimes /*
118c7d893deSDavid Greenman  * Internal accounting functions.
119c7d893deSDavid Greenman  * The former's operation is described in Leffler, et al., and the latter
120c7d893deSDavid Greenman  * was provided by UCB with the 4.4BSD-Lite release
121df8bae1dSRodney W. Grimes  */
122fdbe5babSDiomidis Spinellis static uint32_t	encode_timeval(struct timeval);
123fdbe5babSDiomidis Spinellis static uint32_t	encode_long(long);
124505a1493SJohn Baldwin static void	acctwatch(void);
125505a1493SJohn Baldwin static void	acct_thread(void *);
126505a1493SJohn Baldwin static int	acct_disable(struct thread *);
127ab36c067SJustin T. Gibbs 
128ab36c067SJustin T. Gibbs /*
1295b606744SJohan Karlsson  * Accounting vnode pointer, saved vnode pointer, and flags for each.
13071909edeSRobert Watson  * acct_sx protects against changes to the active vnode and credentials
13171909edeSRobert Watson  * while accounting records are being committed to disk.
132c7d893deSDavid Greenman  */
133101581b0SRobert Watson static int		 acct_configured;
13471909edeSRobert Watson static int		 acct_suspended;
13571909edeSRobert Watson static struct vnode	*acct_vp;
13671909edeSRobert Watson static struct ucred	*acct_cred;
13771909edeSRobert Watson static int		 acct_flags;
13871909edeSRobert Watson static struct sx	 acct_sx;
139df8bae1dSRodney W. Grimes 
14071909edeSRobert Watson SX_SYSINIT(acct, &acct_sx, "acct_sx");
1414f39d5d5SAndrew R. Reiter 
142df8bae1dSRodney W. Grimes /*
143505a1493SJohn Baldwin  * State of the accounting kthread.
144505a1493SJohn Baldwin  */
145505a1493SJohn Baldwin static int		 acct_state;
146505a1493SJohn Baldwin 
147505a1493SJohn Baldwin #define	ACCT_RUNNING	1	/* Accounting kthread is running. */
148505a1493SJohn Baldwin #define	ACCT_EXITREQ	2	/* Accounting kthread should exit. */
149505a1493SJohn Baldwin 
150505a1493SJohn Baldwin /*
151df8bae1dSRodney W. Grimes  * Values associated with enabling and disabling accounting
152df8bae1dSRodney W. Grimes  */
15387b6de2bSPoul-Henning Kamp static int acctsuspend = 2;	/* stop accounting when < 2% free space left */
15487b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW,
15547fdd692SNeil Blakey-Milner 	&acctsuspend, 0, "percentage of free disk space below which accounting stops");
15687b6de2bSPoul-Henning Kamp 
15787b6de2bSPoul-Henning Kamp static int acctresume = 4;	/* resume when free space risen to > 4% */
15887b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW,
15947fdd692SNeil Blakey-Milner 	&acctresume, 0, "percentage of free disk space above which accounting resumes");
16087b6de2bSPoul-Henning Kamp 
16187b6de2bSPoul-Henning Kamp static int acctchkfreq = 15;	/* frequency (in seconds) to check space */
162222fdf4bSJohn Baldwin 
163222fdf4bSJohn Baldwin static int
164222fdf4bSJohn Baldwin sysctl_acct_chkfreq(SYSCTL_HANDLER_ARGS)
165222fdf4bSJohn Baldwin {
166222fdf4bSJohn Baldwin 	int error, value;
167222fdf4bSJohn Baldwin 
168222fdf4bSJohn Baldwin 	/* Write out the old value. */
169222fdf4bSJohn Baldwin 	error = SYSCTL_OUT(req, &acctchkfreq, sizeof(int));
170222fdf4bSJohn Baldwin 	if (error || req->newptr == NULL)
171222fdf4bSJohn Baldwin 		return (error);
172222fdf4bSJohn Baldwin 
173222fdf4bSJohn Baldwin 	/* Read in and verify the new value. */
174222fdf4bSJohn Baldwin 	error = SYSCTL_IN(req, &value, sizeof(int));
175222fdf4bSJohn Baldwin 	if (error)
176222fdf4bSJohn Baldwin 		return (error);
177222fdf4bSJohn Baldwin 	if (value <= 0)
178222fdf4bSJohn Baldwin 		return (EINVAL);
179222fdf4bSJohn Baldwin 	acctchkfreq = value;
180222fdf4bSJohn Baldwin 	return (0);
181222fdf4bSJohn Baldwin }
182222fdf4bSJohn Baldwin SYSCTL_PROC(_kern, OID_AUTO, acct_chkfreq, CTLTYPE_INT|CTLFLAG_RW,
183222fdf4bSJohn Baldwin     &acctchkfreq, 0, sysctl_acct_chkfreq, "I",
184222fdf4bSJohn Baldwin     "frequency for checking the free space");
185df8bae1dSRodney W. Grimes 
186101581b0SRobert Watson SYSCTL_INT(_kern, OID_AUTO, acct_configured, CTLFLAG_RD, &acct_configured, 0,
187101581b0SRobert Watson 	"Accounting configured or not");
188101581b0SRobert Watson 
18971909edeSRobert Watson SYSCTL_INT(_kern, OID_AUTO, acct_suspended, CTLFLAG_RD, &acct_suspended, 0,
19071909edeSRobert Watson 	"Accounting suspended or not");
19171909edeSRobert Watson 
192df8bae1dSRodney W. Grimes /*
1930c14ff0eSRobert Watson  * Accounting system call.  Written based on the specification and previous
1940c14ff0eSRobert Watson  * implementation done by Mark Tinguely.
195df8bae1dSRodney W. Grimes  */
196c7d893deSDavid Greenman int
19771909edeSRobert Watson acct(struct thread *td, struct acct_args *uap)
198c7d893deSDavid Greenman {
199c7d893deSDavid Greenman 	struct nameidata nd;
20011178ee4SJohn Baldwin 	int error, flags, vfslocked;
201c7d893deSDavid Greenman 
202acd3428bSRobert Watson 	error = priv_check(td, PRIV_ACCT);
203797f2d22SPoul-Henning Kamp 	if (error)
20416e7bc7bSJohn Baldwin 		return (error);
205c7d893deSDavid Greenman 
206c7d893deSDavid Greenman 	/*
207c7d893deSDavid Greenman 	 * If accounting is to be started to a file, open that file for
20811178ee4SJohn Baldwin 	 * appending and make sure it's a 'normal'.
209c7d893deSDavid Greenman 	 */
210d1e405c5SAlfred Perlstein 	if (uap->path != NULL) {
211d3778141SRobert Watson 		NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1,
212d3778141SRobert Watson 		    UIO_USERSPACE, uap->path, td);
21392da2e76SJohan Karlsson 		flags = FWRITE | O_APPEND;
2149e223287SKonstantin Belousov 		error = vn_open(&nd, &flags, 0, NULL);
215797f2d22SPoul-Henning Kamp 		if (error)
21611178ee4SJohn Baldwin 			return (error);
21711178ee4SJohn Baldwin 		vfslocked = NDHASGIANT(&nd);
218762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
219e5e820fdSRobert Watson #ifdef MAC
220e5e820fdSRobert Watson 		error = mac_check_system_acct(td->td_ucred, nd.ni_vp);
221e5e820fdSRobert Watson 		if (error) {
22208132261SRobert Watson 			VOP_UNLOCK(nd.ni_vp, 0, td);
223e5e820fdSRobert Watson 			vn_close(nd.ni_vp, flags, td->td_ucred, td);
22411178ee4SJohn Baldwin 			VFS_UNLOCK_GIANT(vfslocked);
22511178ee4SJohn Baldwin 			return (error);
226e5e820fdSRobert Watson 		}
227e5e820fdSRobert Watson #endif
228b40ce416SJulian Elischer 		VOP_UNLOCK(nd.ni_vp, 0, td);
229c7d893deSDavid Greenman 		if (nd.ni_vp->v_type != VREG) {
2305b606744SJohan Karlsson 			vn_close(nd.ni_vp, flags, td->td_ucred, td);
23111178ee4SJohn Baldwin 			VFS_UNLOCK_GIANT(vfslocked);
23211178ee4SJohn Baldwin 			return (EACCES);
233c7d893deSDavid Greenman 		}
23411178ee4SJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
235e5e820fdSRobert Watson #ifdef MAC
236e5e820fdSRobert Watson 	} else {
237e5e820fdSRobert Watson 		error = mac_check_system_acct(td->td_ucred, NULL);
238e5e820fdSRobert Watson 		if (error)
23911178ee4SJohn Baldwin 			return (error);
240e5e820fdSRobert Watson #endif
241c7d893deSDavid Greenman 	}
242c7d893deSDavid Greenman 
24371909edeSRobert Watson 	/*
24471909edeSRobert Watson 	 * Disallow concurrent access to the accounting vnode while we swap
24571909edeSRobert Watson 	 * it out, in order to prevent access after close.
24671909edeSRobert Watson 	 */
24771909edeSRobert Watson 	sx_xlock(&acct_sx);
24801e3f3aeSBruce Evans 
249c7d893deSDavid Greenman 	/*
250c7d893deSDavid Greenman 	 * If accounting was previously enabled, kill the old space-watcher,
25171909edeSRobert Watson 	 * close the file, and (if no new file was specified, leave).  Reset
25271909edeSRobert Watson 	 * the suspended state regardless of whether accounting remains
25371909edeSRobert Watson 	 * enabled.
254c7d893deSDavid Greenman 	 */
25571909edeSRobert Watson 	acct_suspended = 0;
25611178ee4SJohn Baldwin 	if (acct_vp != NULL) {
25711178ee4SJohn Baldwin 		vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
258505a1493SJohn Baldwin 		error = acct_disable(td);
25911178ee4SJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
26011178ee4SJohn Baldwin 	}
261d1e405c5SAlfred Perlstein 	if (uap->path == NULL) {
262505a1493SJohn Baldwin 		if (acct_state & ACCT_RUNNING) {
263505a1493SJohn Baldwin 			acct_state |= ACCT_EXITREQ;
264505a1493SJohn Baldwin 			wakeup(&acct_state);
265505a1493SJohn Baldwin 		}
26671909edeSRobert Watson 		sx_xunlock(&acct_sx);
26711178ee4SJohn Baldwin 		return (error);
268b4dcc46aSAndrew R. Reiter 	}
269c7d893deSDavid Greenman 
270c7d893deSDavid Greenman 	/*
271c7d893deSDavid Greenman 	 * Save the new accounting file vnode, and schedule the new
272c7d893deSDavid Greenman 	 * free space watcher.
273c7d893deSDavid Greenman 	 */
27471909edeSRobert Watson 	acct_vp = nd.ni_vp;
27571909edeSRobert Watson 	acct_cred = crhold(td->td_ucred);
27671909edeSRobert Watson 	acct_flags = flags;
277505a1493SJohn Baldwin 	if (acct_state & ACCT_RUNNING)
278505a1493SJohn Baldwin 		acct_state &= ~ACCT_EXITREQ;
279505a1493SJohn Baldwin 	else {
280505a1493SJohn Baldwin 		/*
281505a1493SJohn Baldwin 		 * Try to start up an accounting kthread.  We may start more
282505a1493SJohn Baldwin 		 * than one, but if so the extras will commit suicide as
283505a1493SJohn Baldwin 		 * soon as they start up.
284505a1493SJohn Baldwin 		 */
285505a1493SJohn Baldwin 		error = kthread_create(acct_thread, NULL, NULL, 0, 0,
286505a1493SJohn Baldwin 		    "accounting");
287505a1493SJohn Baldwin 		if (error) {
28811178ee4SJohn Baldwin 			vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
289505a1493SJohn Baldwin 			(void) vn_close(acct_vp, acct_flags, acct_cred, td);
29011178ee4SJohn Baldwin 			VFS_UNLOCK_GIANT(vfslocked);
291505a1493SJohn Baldwin 			crfree(acct_cred);
292101581b0SRobert Watson 			acct_configured = 0;
293505a1493SJohn Baldwin 			acct_vp = NULL;
294505a1493SJohn Baldwin 			acct_cred = NULL;
295505a1493SJohn Baldwin 			acct_flags = 0;
296505a1493SJohn Baldwin 			sx_xunlock(&acct_sx);
297505a1493SJohn Baldwin 			log(LOG_NOTICE, "Unable to start accounting thread\n");
29811178ee4SJohn Baldwin 			return (error);
299505a1493SJohn Baldwin 		}
300505a1493SJohn Baldwin 	}
301101581b0SRobert Watson 	acct_configured = 1;
30271909edeSRobert Watson 	sx_xunlock(&acct_sx);
30348719ca7SBosko Milekic 	log(LOG_NOTICE, "Accounting enabled\n");
304c7d893deSDavid Greenman 	return (error);
305c7d893deSDavid Greenman }
306c7d893deSDavid Greenman 
307c7d893deSDavid Greenman /*
308505a1493SJohn Baldwin  * Disable currently in-progress accounting by closing the vnode, dropping
309505a1493SJohn Baldwin  * our reference to the credential, and clearing the vnode's flags.
310505a1493SJohn Baldwin  */
311505a1493SJohn Baldwin static int
312505a1493SJohn Baldwin acct_disable(struct thread *td)
313505a1493SJohn Baldwin {
314505a1493SJohn Baldwin 	int error;
315505a1493SJohn Baldwin 
316505a1493SJohn Baldwin 	sx_assert(&acct_sx, SX_XLOCKED);
317505a1493SJohn Baldwin 	error = vn_close(acct_vp, acct_flags, acct_cred, td);
318505a1493SJohn Baldwin 	crfree(acct_cred);
319101581b0SRobert Watson 	acct_configured = 0;
320505a1493SJohn Baldwin 	acct_vp = NULL;
321505a1493SJohn Baldwin 	acct_cred = NULL;
322505a1493SJohn Baldwin 	acct_flags = 0;
323505a1493SJohn Baldwin 	log(LOG_NOTICE, "Accounting disabled\n");
324505a1493SJohn Baldwin 	return (error);
325505a1493SJohn Baldwin }
326505a1493SJohn Baldwin 
327505a1493SJohn Baldwin /*
328c7d893deSDavid Greenman  * Write out process accounting information, on process exit.
329c7d893deSDavid Greenman  * Data to be written out is specified in Leffler, et al.
330c7d893deSDavid Greenman  * and are enumerated below.  (They're also noted in the system
331c7d893deSDavid Greenman  * "acct.h" header file.)
332c7d893deSDavid Greenman  */
333c7d893deSDavid Greenman int
33471909edeSRobert Watson acct_process(struct thread *td)
335c7d893deSDavid Greenman {
336fdbe5babSDiomidis Spinellis 	struct acctv2 acct;
337c7d893deSDavid Greenman 	struct timeval ut, st, tmp;
33891d5354aSJohn Baldwin 	struct plimit *newlim, *oldlim;
33901e3f3aeSBruce Evans 	struct proc *p;
3401c4bcd05SJeff Roberson 	struct rusage ru;
34171909edeSRobert Watson 	int t, ret, vfslocked;
3424f39d5d5SAndrew R. Reiter 
3432b05b557SRobert Watson 	/*
3442b05b557SRobert Watson 	 * Lockless check of accounting condition before doing the hard
3452b05b557SRobert Watson 	 * work.
3462b05b557SRobert Watson 	 */
34771909edeSRobert Watson 	if (acct_vp == NULL || acct_suspended)
3482b05b557SRobert Watson 		return (0);
3492b05b557SRobert Watson 
35071909edeSRobert Watson 	sx_slock(&acct_sx);
351c7d893deSDavid Greenman 
3522b05b557SRobert Watson 	/*
3532b05b557SRobert Watson 	 * If accounting isn't enabled, don't bother.  Have to check again
3542b05b557SRobert Watson 	 * once we own the lock in case we raced with disabling of accounting
3552b05b557SRobert Watson 	 * by another thread.
3562b05b557SRobert Watson 	 */
35771909edeSRobert Watson 	if (acct_vp == NULL || acct_suspended) {
35871909edeSRobert Watson 		sx_sunlock(&acct_sx);
359c7d893deSDavid Greenman 		return (0);
3604f39d5d5SAndrew R. Reiter 	}
361c7d893deSDavid Greenman 
36201e3f3aeSBruce Evans 	p = td->td_proc;
36301e3f3aeSBruce Evans 
364c7d893deSDavid Greenman 	/*
365c7d893deSDavid Greenman 	 * Get process accounting information.
366c7d893deSDavid Greenman 	 */
367c7d893deSDavid Greenman 
3687e653dbdSJohn Baldwin 	PROC_LOCK(p);
369c7d893deSDavid Greenman 	/* (1) The name of the command that ran */
370c7d893deSDavid Greenman 	bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
371c7d893deSDavid Greenman 
372c7d893deSDavid Greenman 	/* (2) The amount of user and system time that was used */
3731c4bcd05SJeff Roberson 	rufetch(p, &ru);
37478c85e8dSJohn Baldwin 	calcru(p, &ut, &st);
375fdbe5babSDiomidis Spinellis 	acct.ac_utime = encode_timeval(ut);
376fdbe5babSDiomidis Spinellis 	acct.ac_stime = encode_timeval(st);
377c7d893deSDavid Greenman 
3785f9ae8e0SGiorgos Keramidas 	/* (3) The elapsed time the command ran (and its starting time) */
37987ccef7bSDag-Erling Smørgrav 	tmp = boottime;
38087ccef7bSDag-Erling Smørgrav 	timevaladd(&tmp, &p->p_stats->p_start);
38187ccef7bSDag-Erling Smørgrav 	acct.ac_btime = tmp.tv_sec;
38287ccef7bSDag-Erling Smørgrav 	microuptime(&tmp);
383c7d893deSDavid Greenman 	timevalsub(&tmp, &p->p_stats->p_start);
384fdbe5babSDiomidis Spinellis 	acct.ac_etime = encode_timeval(tmp);
385c7d893deSDavid Greenman 
386c7d893deSDavid Greenman 	/* (4) The average amount of memory used */
387c7d893deSDavid Greenman 	tmp = ut;
388c7d893deSDavid Greenman 	timevaladd(&tmp, &st);
389fdbe5babSDiomidis Spinellis 	/* Convert tmp (i.e. u + s) into hz units to match ru_i*. */
390c7d893deSDavid Greenman 	t = tmp.tv_sec * hz + tmp.tv_usec / tick;
391c7d893deSDavid Greenman 	if (t)
3921c4bcd05SJeff Roberson 		acct.ac_mem = encode_long((ru.ru_ixrss + ru.ru_idrss +
3931c4bcd05SJeff Roberson 		    + ru.ru_isrss) / t);
394c7d893deSDavid Greenman 	else
395c7d893deSDavid Greenman 		acct.ac_mem = 0;
396c7d893deSDavid Greenman 
397c7d893deSDavid Greenman 	/* (5) The number of disk I/O operations done */
3981c4bcd05SJeff Roberson 	acct.ac_io = encode_long(ru.ru_inblock + ru.ru_oublock);
399c7d893deSDavid Greenman 
400c7d893deSDavid Greenman 	/* (6) The UID and GID of the process */
401b1fc0ec1SRobert Watson 	acct.ac_uid = p->p_ucred->cr_ruid;
402b1fc0ec1SRobert Watson 	acct.ac_gid = p->p_ucred->cr_rgid;
403c7d893deSDavid Greenman 
404c7d893deSDavid Greenman 	/* (7) The terminal from which the process was started */
405f591779bSSeigo Tanimura 	SESS_LOCK(p->p_session);
406c7d893deSDavid Greenman 	if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
40723d76283SPoul-Henning Kamp 		acct.ac_tty = dev2udev(p->p_pgrp->pg_session->s_ttyp->t_dev);
408c7d893deSDavid Greenman 	else
409f3732fd1SPoul-Henning Kamp 		acct.ac_tty = NODEV;
410f591779bSSeigo Tanimura 	SESS_UNLOCK(p->p_session);
411c7d893deSDavid Greenman 
412c7d893deSDavid Greenman 	/* (8) The boolean flags that tell how the process terminated, etc. */
413fdbe5babSDiomidis Spinellis 	acct.ac_flagx = p->p_acflag;
4147e653dbdSJohn Baldwin 	PROC_UNLOCK(p);
415c7d893deSDavid Greenman 
416fdbe5babSDiomidis Spinellis 	/* Setup ancillary structure fields. */
417fdbe5babSDiomidis Spinellis 	acct.ac_flagx |= ANVER;
418fdbe5babSDiomidis Spinellis 	acct.ac_zero = 0;
419fdbe5babSDiomidis Spinellis 	acct.ac_version = 2;
420fdbe5babSDiomidis Spinellis 	acct.ac_len = acct.ac_len2 = sizeof(acct);
421fdbe5babSDiomidis Spinellis 
422c7d893deSDavid Greenman 	/*
423b5afad71SDavid Greenman 	 * Eliminate any file size rlimit.
424b5afad71SDavid Greenman 	 */
42591d5354aSJohn Baldwin 	newlim = lim_alloc();
42691d5354aSJohn Baldwin 	PROC_LOCK(p);
42791d5354aSJohn Baldwin 	oldlim = p->p_limit;
42891d5354aSJohn Baldwin 	lim_copy(newlim, oldlim);
42991d5354aSJohn Baldwin 	newlim->pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
43091d5354aSJohn Baldwin 	p->p_limit = newlim;
43191d5354aSJohn Baldwin 	PROC_UNLOCK(p);
43291d5354aSJohn Baldwin 	lim_free(oldlim);
433b5afad71SDavid Greenman 
43401e3f3aeSBruce Evans 	/*
43501e3f3aeSBruce Evans 	 * Write the accounting information to the file.
43601e3f3aeSBruce Evans 	 */
43771909edeSRobert Watson 	vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
43871909edeSRobert Watson 	VOP_LEASE(acct_vp, td, acct_cred, LEASE_WRITE);
43971909edeSRobert Watson 	ret = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct, sizeof (acct),
44071909edeSRobert Watson 	    (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acct_cred, NOCRED,
4414f39d5d5SAndrew R. Reiter 	    (int *)0, td);
44271909edeSRobert Watson 	VFS_UNLOCK_GIANT(vfslocked);
44371909edeSRobert Watson 	sx_sunlock(&acct_sx);
4444f39d5d5SAndrew R. Reiter 	return (ret);
445c7d893deSDavid Greenman }
446c7d893deSDavid Greenman 
447fdbe5babSDiomidis Spinellis /* FLOAT_CONVERSION_START (Regression testing; don't remove this line.) */
448fdbe5babSDiomidis Spinellis 
449fdbe5babSDiomidis Spinellis /* Convert timevals and longs into IEEE-754 bit patterns. */
450fdbe5babSDiomidis Spinellis 
451fdbe5babSDiomidis Spinellis /* Mantissa mask (MSB is implied, so subtract 1). */
452fdbe5babSDiomidis Spinellis #define MANT_MASK ((1 << (FLT_MANT_DIG - 1)) - 1)
453fdbe5babSDiomidis Spinellis 
454c7d893deSDavid Greenman /*
455fdbe5babSDiomidis Spinellis  * We calculate integer values to a precision of approximately
456fdbe5babSDiomidis Spinellis  * 28 bits.
457fdbe5babSDiomidis Spinellis  * This is high-enough precision to fill the 24 float bits
458fdbe5babSDiomidis Spinellis  * and low-enough to avoid overflowing the 32 int bits.
459c7d893deSDavid Greenman  */
460fdbe5babSDiomidis Spinellis #define CALC_BITS 28
461c7d893deSDavid Greenman 
462fdbe5babSDiomidis Spinellis /* log_2(1000000). */
463fdbe5babSDiomidis Spinellis #define LOG2_1M 20
464c7d893deSDavid Greenman 
465fdbe5babSDiomidis Spinellis /*
466fdbe5babSDiomidis Spinellis  * Convert the elements of a timeval into a 32-bit word holding
467fdbe5babSDiomidis Spinellis  * the bits of a IEEE-754 float.
468fdbe5babSDiomidis Spinellis  * The float value represents the timeval's value in microsecond units.
469fdbe5babSDiomidis Spinellis  */
470fdbe5babSDiomidis Spinellis static uint32_t
471fdbe5babSDiomidis Spinellis encode_timeval(struct timeval tv)
472c7d893deSDavid Greenman {
473fdbe5babSDiomidis Spinellis 	int log2_s;
474fdbe5babSDiomidis Spinellis 	int val, exp;	/* Unnormalized value and exponent */
475fdbe5babSDiomidis Spinellis 	int norm_exp;	/* Normalized exponent */
476fdbe5babSDiomidis Spinellis 	int shift;
477c7d893deSDavid Greenman 
478fdbe5babSDiomidis Spinellis 	/*
479fdbe5babSDiomidis Spinellis 	 * First calculate value and exponent to about CALC_BITS precision.
480fdbe5babSDiomidis Spinellis 	 * Note that the following conditionals have been ordered so that
481fdbe5babSDiomidis Spinellis 	 * the most common cases appear first.
482fdbe5babSDiomidis Spinellis 	 */
483fdbe5babSDiomidis Spinellis 	if (tv.tv_sec == 0) {
484fdbe5babSDiomidis Spinellis 		if (tv.tv_usec == 0)
485fdbe5babSDiomidis Spinellis 			return (0);
486c7d893deSDavid Greenman 		exp = 0;
487fdbe5babSDiomidis Spinellis 		val = tv.tv_usec;
488fdbe5babSDiomidis Spinellis 	} else {
489fdbe5babSDiomidis Spinellis 		/*
490fdbe5babSDiomidis Spinellis 		 * Calculate the value to a precision of approximately
491fdbe5babSDiomidis Spinellis 		 * CALC_BITS.
492fdbe5babSDiomidis Spinellis 		 */
493fdbe5babSDiomidis Spinellis 		log2_s = fls(tv.tv_sec) - 1;
494fdbe5babSDiomidis Spinellis 		if (log2_s + LOG2_1M < CALC_BITS) {
495fdbe5babSDiomidis Spinellis 			exp = 0;
496fdbe5babSDiomidis Spinellis 			val = 1000000 * tv.tv_sec + tv.tv_usec;
497fdbe5babSDiomidis Spinellis 		} else {
498fdbe5babSDiomidis Spinellis 			exp = log2_s + LOG2_1M - CALC_BITS;
499fdbe5babSDiomidis Spinellis 			val = (unsigned int)(((u_int64_t)1000000 * tv.tv_sec +
500fdbe5babSDiomidis Spinellis 			    tv.tv_usec) >> exp);
501fdbe5babSDiomidis Spinellis 		}
502fdbe5babSDiomidis Spinellis 	}
503fdbe5babSDiomidis Spinellis 	/* Now normalize and pack the value into an IEEE-754 float. */
504fdbe5babSDiomidis Spinellis 	norm_exp = fls(val) - 1;
505fdbe5babSDiomidis Spinellis 	shift = FLT_MANT_DIG - norm_exp - 1;
506fdbe5babSDiomidis Spinellis #ifdef ACCT_DEBUG
507fdbe5babSDiomidis Spinellis 	printf("val=%d exp=%d shift=%d log2(val)=%d\n",
508fdbe5babSDiomidis Spinellis 	    val, exp, shift, norm_exp);
509fdbe5babSDiomidis Spinellis 	printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
510fdbe5babSDiomidis Spinellis 	    ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
511fdbe5babSDiomidis Spinellis #endif
512fdbe5babSDiomidis Spinellis 	return (((FLT_MAX_EXP - 1 + exp + norm_exp) << (FLT_MANT_DIG - 1)) |
513fdbe5babSDiomidis Spinellis 	    ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
514c7d893deSDavid Greenman }
515c7d893deSDavid Greenman 
516fdbe5babSDiomidis Spinellis /*
517fdbe5babSDiomidis Spinellis  * Convert a non-negative long value into the bit pattern of
518fdbe5babSDiomidis Spinellis  * an IEEE-754 float value.
519fdbe5babSDiomidis Spinellis  */
520fdbe5babSDiomidis Spinellis static uint32_t
521fdbe5babSDiomidis Spinellis encode_long(long val)
522fdbe5babSDiomidis Spinellis {
523fdbe5babSDiomidis Spinellis 	int norm_exp;	/* Normalized exponent */
524fdbe5babSDiomidis Spinellis 	int shift;
525fdbe5babSDiomidis Spinellis 
526fdbe5babSDiomidis Spinellis 	KASSERT(val >= 0,  ("encode_long: -ve value %ld", val));
527fdbe5babSDiomidis Spinellis 	if (val == 0)
528fdbe5babSDiomidis Spinellis 		return (0);
529fdbe5babSDiomidis Spinellis 	norm_exp = fls(val) - 1;
530fdbe5babSDiomidis Spinellis 	shift = FLT_MANT_DIG - norm_exp - 1;
531fdbe5babSDiomidis Spinellis #ifdef ACCT_DEBUG
532fdbe5babSDiomidis Spinellis 	printf("val=%d shift=%d log2(val)=%d\n",
533fdbe5babSDiomidis Spinellis 	    val, shift, norm_exp);
534fdbe5babSDiomidis Spinellis 	printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
535fdbe5babSDiomidis Spinellis 	    ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
536fdbe5babSDiomidis Spinellis #endif
537fdbe5babSDiomidis Spinellis 	return (((FLT_MAX_EXP - 1 + norm_exp) << (FLT_MANT_DIG - 1)) |
538fdbe5babSDiomidis Spinellis 	    ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
539c7d893deSDavid Greenman }
540c7d893deSDavid Greenman 
541fdbe5babSDiomidis Spinellis /* FLOAT_CONVERSION_END (Regression testing; don't remove this line.) */
542c7d893deSDavid Greenman 
543c7d893deSDavid Greenman /*
544c7d893deSDavid Greenman  * Periodically check the filesystem to see if accounting
545c7d893deSDavid Greenman  * should be turned on or off.  Beware the case where the vnode
546c7d893deSDavid Greenman  * has been vgone()'d out from underneath us, e.g. when the file
547c7d893deSDavid Greenman  * system containing the accounting file has been forcibly unmounted.
548c7d893deSDavid Greenman  */
549df8bae1dSRodney W. Grimes /* ARGSUSED */
55087b6de2bSPoul-Henning Kamp static void
551505a1493SJohn Baldwin acctwatch(void)
552df8bae1dSRodney W. Grimes {
553df8bae1dSRodney W. Grimes 	struct statfs sb;
55471909edeSRobert Watson 	int vfslocked;
555df8bae1dSRodney W. Grimes 
556505a1493SJohn Baldwin 	sx_assert(&acct_sx, SX_XLOCKED);
557505a1493SJohn Baldwin 
558505a1493SJohn Baldwin 	/*
559505a1493SJohn Baldwin 	 * If accounting was disabled before our kthread was scheduled,
560505a1493SJohn Baldwin 	 * then acct_vp might be NULL.  If so, just ask our kthread to
561505a1493SJohn Baldwin 	 * exit and return.
562505a1493SJohn Baldwin 	 */
563505a1493SJohn Baldwin 	if (acct_vp == NULL) {
564505a1493SJohn Baldwin 		acct_state |= ACCT_EXITREQ;
565c7d893deSDavid Greenman 		return;
566c7d893deSDavid Greenman 	}
567505a1493SJohn Baldwin 
568505a1493SJohn Baldwin 	/*
569505a1493SJohn Baldwin 	 * If our vnode is no longer valid, tear it down and signal the
570505a1493SJohn Baldwin 	 * accounting thread to die.
571505a1493SJohn Baldwin 	 */
572505a1493SJohn Baldwin 	vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
573505a1493SJohn Baldwin 	if (acct_vp->v_type == VBAD) {
574505a1493SJohn Baldwin 		(void) acct_disable(NULL);
575505a1493SJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
576505a1493SJohn Baldwin 		acct_state |= ACCT_EXITREQ;
577505a1493SJohn Baldwin 		return;
578505a1493SJohn Baldwin 	}
579505a1493SJohn Baldwin 
58071909edeSRobert Watson 	/*
58171909edeSRobert Watson 	 * Stopping here is better than continuing, maybe it will be VBAD
58271909edeSRobert Watson 	 * next time around.
58371909edeSRobert Watson 	 */
58471909edeSRobert Watson 	if (VFS_STATFS(acct_vp->v_mount, &sb, curthread) < 0) {
58571909edeSRobert Watson 		VFS_UNLOCK_GIANT(vfslocked);
58671909edeSRobert Watson 		return;
58771909edeSRobert Watson 	}
58871909edeSRobert Watson 	VFS_UNLOCK_GIANT(vfslocked);
58971909edeSRobert Watson 	if (acct_suspended) {
59071909edeSRobert Watson 		if (sb.f_bavail > (int64_t)(acctresume * sb.f_blocks /
59171909edeSRobert Watson 		    100)) {
59271909edeSRobert Watson 			acct_suspended = 0;
593df8bae1dSRodney W. Grimes 			log(LOG_NOTICE, "Accounting resumed\n");
594df8bae1dSRodney W. Grimes 		}
595996c772fSJohn Dyson 	} else {
59671909edeSRobert Watson 		if (sb.f_bavail <= (int64_t)(acctsuspend * sb.f_blocks /
59771909edeSRobert Watson 		    100)) {
59871909edeSRobert Watson 			acct_suspended = 1;
599df8bae1dSRodney W. Grimes 			log(LOG_NOTICE, "Accounting suspended\n");
600df8bae1dSRodney W. Grimes 		}
601996c772fSJohn Dyson 	}
602505a1493SJohn Baldwin }
603505a1493SJohn Baldwin 
604505a1493SJohn Baldwin /*
605505a1493SJohn Baldwin  * The main loop for the dedicated kernel thread that periodically calls
606505a1493SJohn Baldwin  * acctwatch().
607505a1493SJohn Baldwin  */
608505a1493SJohn Baldwin static void
609505a1493SJohn Baldwin acct_thread(void *dummy)
610505a1493SJohn Baldwin {
611505a1493SJohn Baldwin 	u_char pri;
612505a1493SJohn Baldwin 
613505a1493SJohn Baldwin 	/* This is a low-priority kernel thread. */
614505a1493SJohn Baldwin 	pri = PRI_MAX_KERN;
615505a1493SJohn Baldwin 	mtx_lock_spin(&sched_lock);
616505a1493SJohn Baldwin 	sched_prio(curthread, pri);
617505a1493SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
618505a1493SJohn Baldwin 
619505a1493SJohn Baldwin 	/* If another accounting kthread is already running, just die. */
620505a1493SJohn Baldwin 	sx_xlock(&acct_sx);
621505a1493SJohn Baldwin 	if (acct_state & ACCT_RUNNING) {
62271909edeSRobert Watson 		sx_xunlock(&acct_sx);
623505a1493SJohn Baldwin 		kthread_exit(0);
624505a1493SJohn Baldwin 	}
625505a1493SJohn Baldwin 	acct_state |= ACCT_RUNNING;
626505a1493SJohn Baldwin 
627505a1493SJohn Baldwin 	/* Loop until we are asked to exit. */
628505a1493SJohn Baldwin 	while (!(acct_state & ACCT_EXITREQ)) {
629505a1493SJohn Baldwin 
630505a1493SJohn Baldwin 		/* Perform our periodic checks. */
631505a1493SJohn Baldwin 		acctwatch();
632505a1493SJohn Baldwin 
633505a1493SJohn Baldwin 		/*
634505a1493SJohn Baldwin 		 * We check this flag again before sleeping since the
635505a1493SJohn Baldwin 		 * acctwatch() might have shut down accounting and asked us
636505a1493SJohn Baldwin 		 * to exit.
637505a1493SJohn Baldwin 		 */
638505a1493SJohn Baldwin 		if (!(acct_state & ACCT_EXITREQ)) {
6396caa5f40SJohn Baldwin 			sx_sleep(&acct_state, &acct_sx, 0, "-",
6406caa5f40SJohn Baldwin 			    acctchkfreq * hz);
641505a1493SJohn Baldwin 		}
642505a1493SJohn Baldwin 	}
643505a1493SJohn Baldwin 
644505a1493SJohn Baldwin 	/*
645505a1493SJohn Baldwin 	 * Acknowledge the exit request and shutdown.  We clear both the
646505a1493SJohn Baldwin 	 * exit request and running flags.
647505a1493SJohn Baldwin 	 */
648505a1493SJohn Baldwin 	acct_state = 0;
649505a1493SJohn Baldwin 	sx_xunlock(&acct_sx);
650505a1493SJohn Baldwin 	kthread_exit(0);
651df8bae1dSRodney W. Grimes }
652