xref: /freebsd/sys/kern/kern_acct.c (revision 101581b08212a2632a6f88e0bf79a6f4450c8956)
1df8bae1dSRodney W. Grimes /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
1071909edeSRobert Watson  * Copyright (c) 1994 Christopher G. Demetriou
1171909edeSRobert Watson  * Copyright (c) 2005 Robert N. M. Watson
1271909edeSRobert Watson  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
22df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
23df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
24df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
25df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
26df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
27df8bae1dSRodney W. Grimes  *    without specific prior written permission.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
40df8bae1dSRodney W. Grimes  *
41c7d893deSDavid Greenman  *	@(#)kern_acct.c	8.1 (Berkeley) 6/14/93
42df8bae1dSRodney W. Grimes  */
43df8bae1dSRodney W. Grimes 
44677b542eSDavid E. O'Brien #include <sys/cdefs.h>
45677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
46677b542eSDavid E. O'Brien 
47e5e820fdSRobert Watson #include "opt_mac.h"
48e5e820fdSRobert Watson 
49df8bae1dSRodney W. Grimes #include <sys/param.h>
500ad076d5SBruce Evans #include <sys/systm.h>
51b0864d13SJohn Baldwin #include <sys/acct.h>
52b0864d13SJohn Baldwin #include <sys/fcntl.h>
53b0864d13SJohn Baldwin #include <sys/kernel.h>
54505a1493SJohn Baldwin #include <sys/kthread.h>
55fb919e4dSMark Murray #include <sys/lock.h>
56e5e820fdSRobert Watson #include <sys/mac.h>
57df8bae1dSRodney W. Grimes #include <sys/mount.h>
58b0864d13SJohn Baldwin #include <sys/mutex.h>
59c7d893deSDavid Greenman #include <sys/namei.h>
60b0864d13SJohn Baldwin #include <sys/proc.h>
61c7d893deSDavid Greenman #include <sys/resourcevar.h>
62505a1493SJohn Baldwin #include <sys/sched.h>
63b0864d13SJohn Baldwin #include <sys/sx.h>
64b0864d13SJohn Baldwin #include <sys/sysctl.h>
65b0864d13SJohn Baldwin #include <sys/sysent.h>
66b0864d13SJohn Baldwin #include <sys/syslog.h>
67b0864d13SJohn Baldwin #include <sys/sysproto.h>
68c7d893deSDavid Greenman #include <sys/tty.h>
69b0864d13SJohn Baldwin #include <sys/vnode.h>
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes /*
72c7d893deSDavid Greenman  * The routines implemented in this file are described in:
73c7d893deSDavid Greenman  *      Leffler, et al.: The Design and Implementation of the 4.3BSD
74c7d893deSDavid Greenman  *	    UNIX Operating System (Addison Welley, 1989)
75c7d893deSDavid Greenman  * on pages 62-63.
76c7d893deSDavid Greenman  *
77c7d893deSDavid Greenman  * Arguably, to simplify accounting operations, this mechanism should
78c7d893deSDavid Greenman  * be replaced by one in which an accounting log file (similar to /dev/klog)
79c7d893deSDavid Greenman  * is read by a user process, etc.  However, that has its own problems.
80df8bae1dSRodney W. Grimes  */
81df8bae1dSRodney W. Grimes 
82df8bae1dSRodney W. Grimes /*
83c7d893deSDavid Greenman  * Internal accounting functions.
84c7d893deSDavid Greenman  * The former's operation is described in Leffler, et al., and the latter
85c7d893deSDavid Greenman  * was provided by UCB with the 4.4BSD-Lite release
86df8bae1dSRodney W. Grimes  */
874d77a549SAlfred Perlstein static comp_t	encode_comp_t(u_long, u_long);
88505a1493SJohn Baldwin static void	acctwatch(void);
89505a1493SJohn Baldwin static void	acct_thread(void *);
90505a1493SJohn Baldwin static int	acct_disable(struct thread *);
91ab36c067SJustin T. Gibbs 
92ab36c067SJustin T. Gibbs /*
935b606744SJohan Karlsson  * Accounting vnode pointer, saved vnode pointer, and flags for each.
9471909edeSRobert Watson  * acct_sx protects against changes to the active vnode and credentials
9571909edeSRobert Watson  * while accounting records are being committed to disk.
96c7d893deSDavid Greenman  */
97101581b0SRobert Watson static int		 acct_configured;
9871909edeSRobert Watson static int		 acct_suspended;
9971909edeSRobert Watson static struct vnode	*acct_vp;
10071909edeSRobert Watson static struct ucred	*acct_cred;
10171909edeSRobert Watson static int		 acct_flags;
10271909edeSRobert Watson static struct sx	 acct_sx;
103df8bae1dSRodney W. Grimes 
10471909edeSRobert Watson SX_SYSINIT(acct, &acct_sx, "acct_sx");
1054f39d5d5SAndrew R. Reiter 
106df8bae1dSRodney W. Grimes /*
107505a1493SJohn Baldwin  * State of the accounting kthread.
108505a1493SJohn Baldwin  */
109505a1493SJohn Baldwin static int		 acct_state;
110505a1493SJohn Baldwin 
111505a1493SJohn Baldwin #define	ACCT_RUNNING	1	/* Accounting kthread is running. */
112505a1493SJohn Baldwin #define	ACCT_EXITREQ	2	/* Accounting kthread should exit. */
113505a1493SJohn Baldwin 
114505a1493SJohn Baldwin /*
115df8bae1dSRodney W. Grimes  * Values associated with enabling and disabling accounting
116df8bae1dSRodney W. Grimes  */
11787b6de2bSPoul-Henning Kamp static int acctsuspend = 2;	/* stop accounting when < 2% free space left */
11887b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW,
11947fdd692SNeil Blakey-Milner 	&acctsuspend, 0, "percentage of free disk space below which accounting stops");
12087b6de2bSPoul-Henning Kamp 
12187b6de2bSPoul-Henning Kamp static int acctresume = 4;	/* resume when free space risen to > 4% */
12287b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW,
12347fdd692SNeil Blakey-Milner 	&acctresume, 0, "percentage of free disk space above which accounting resumes");
12487b6de2bSPoul-Henning Kamp 
12587b6de2bSPoul-Henning Kamp static int acctchkfreq = 15;	/* frequency (in seconds) to check space */
126222fdf4bSJohn Baldwin 
127222fdf4bSJohn Baldwin static int
128222fdf4bSJohn Baldwin sysctl_acct_chkfreq(SYSCTL_HANDLER_ARGS)
129222fdf4bSJohn Baldwin {
130222fdf4bSJohn Baldwin 	int error, value;
131222fdf4bSJohn Baldwin 
132222fdf4bSJohn Baldwin 	/* Write out the old value. */
133222fdf4bSJohn Baldwin 	error = SYSCTL_OUT(req, &acctchkfreq, sizeof(int));
134222fdf4bSJohn Baldwin 	if (error || req->newptr == NULL)
135222fdf4bSJohn Baldwin 		return (error);
136222fdf4bSJohn Baldwin 
137222fdf4bSJohn Baldwin 	/* Read in and verify the new value. */
138222fdf4bSJohn Baldwin 	error = SYSCTL_IN(req, &value, sizeof(int));
139222fdf4bSJohn Baldwin 	if (error)
140222fdf4bSJohn Baldwin 		return (error);
141222fdf4bSJohn Baldwin 	if (value <= 0)
142222fdf4bSJohn Baldwin 		return (EINVAL);
143222fdf4bSJohn Baldwin 	acctchkfreq = value;
144222fdf4bSJohn Baldwin 	return (0);
145222fdf4bSJohn Baldwin }
146222fdf4bSJohn Baldwin SYSCTL_PROC(_kern, OID_AUTO, acct_chkfreq, CTLTYPE_INT|CTLFLAG_RW,
147222fdf4bSJohn Baldwin     &acctchkfreq, 0, sysctl_acct_chkfreq, "I",
148222fdf4bSJohn Baldwin     "frequency for checking the free space");
149df8bae1dSRodney W. Grimes 
150101581b0SRobert Watson SYSCTL_INT(_kern, OID_AUTO, acct_configured, CTLFLAG_RD, &acct_configured, 0,
151101581b0SRobert Watson 	"Accounting configured or not");
152101581b0SRobert Watson 
15371909edeSRobert Watson SYSCTL_INT(_kern, OID_AUTO, acct_suspended, CTLFLAG_RD, &acct_suspended, 0,
15471909edeSRobert Watson 	"Accounting suspended or not");
15571909edeSRobert Watson 
156df8bae1dSRodney W. Grimes /*
157c7d893deSDavid Greenman  * Accounting system call.  Written based on the specification and
158c7d893deSDavid Greenman  * previous implementation done by Mark Tinguely.
159116734c4SMatthew Dillon  *
160116734c4SMatthew Dillon  * MPSAFE
161df8bae1dSRodney W. Grimes  */
162c7d893deSDavid Greenman int
16371909edeSRobert Watson acct(struct thread *td, struct acct_args *uap)
164c7d893deSDavid Greenman {
165c7d893deSDavid Greenman 	struct nameidata nd;
16611178ee4SJohn Baldwin 	int error, flags, vfslocked;
167c7d893deSDavid Greenman 
168c7d893deSDavid Greenman 	/* Make sure that the caller is root. */
16944731cabSJohn Baldwin 	error = suser(td);
170797f2d22SPoul-Henning Kamp 	if (error)
17116e7bc7bSJohn Baldwin 		return (error);
172c7d893deSDavid Greenman 
173c7d893deSDavid Greenman 	/*
174c7d893deSDavid Greenman 	 * If accounting is to be started to a file, open that file for
17511178ee4SJohn Baldwin 	 * appending and make sure it's a 'normal'.
176c7d893deSDavid Greenman 	 */
177d1e405c5SAlfred Perlstein 	if (uap->path != NULL) {
178d3778141SRobert Watson 		NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1,
179d3778141SRobert Watson 		    UIO_USERSPACE, uap->path, td);
18092da2e76SJohan Karlsson 		flags = FWRITE | O_APPEND;
1817c89f162SPoul-Henning Kamp 		error = vn_open(&nd, &flags, 0, -1);
182797f2d22SPoul-Henning Kamp 		if (error)
18311178ee4SJohn Baldwin 			return (error);
18411178ee4SJohn Baldwin 		vfslocked = NDHASGIANT(&nd);
185762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
186e5e820fdSRobert Watson #ifdef MAC
187e5e820fdSRobert Watson 		error = mac_check_system_acct(td->td_ucred, nd.ni_vp);
188e5e820fdSRobert Watson 		if (error) {
18908132261SRobert Watson 			VOP_UNLOCK(nd.ni_vp, 0, td);
190e5e820fdSRobert Watson 			vn_close(nd.ni_vp, flags, td->td_ucred, td);
19111178ee4SJohn Baldwin 			VFS_UNLOCK_GIANT(vfslocked);
19211178ee4SJohn Baldwin 			return (error);
193e5e820fdSRobert Watson 		}
194e5e820fdSRobert Watson #endif
195b40ce416SJulian Elischer 		VOP_UNLOCK(nd.ni_vp, 0, td);
196c7d893deSDavid Greenman 		if (nd.ni_vp->v_type != VREG) {
1975b606744SJohan Karlsson 			vn_close(nd.ni_vp, flags, td->td_ucred, td);
19811178ee4SJohn Baldwin 			VFS_UNLOCK_GIANT(vfslocked);
19911178ee4SJohn Baldwin 			return (EACCES);
200c7d893deSDavid Greenman 		}
20111178ee4SJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
202e5e820fdSRobert Watson #ifdef MAC
203e5e820fdSRobert Watson 	} else {
204e5e820fdSRobert Watson 		error = mac_check_system_acct(td->td_ucred, NULL);
205e5e820fdSRobert Watson 		if (error)
20611178ee4SJohn Baldwin 			return (error);
207e5e820fdSRobert Watson #endif
208c7d893deSDavid Greenman 	}
209c7d893deSDavid Greenman 
21071909edeSRobert Watson 	/*
21171909edeSRobert Watson 	 * Disallow concurrent access to the accounting vnode while we swap
21271909edeSRobert Watson 	 * it out, in order to prevent access after close.
21371909edeSRobert Watson 	 */
21471909edeSRobert Watson 	sx_xlock(&acct_sx);
21501e3f3aeSBruce Evans 
216c7d893deSDavid Greenman 	/*
217c7d893deSDavid Greenman 	 * If accounting was previously enabled, kill the old space-watcher,
21871909edeSRobert Watson 	 * close the file, and (if no new file was specified, leave).  Reset
21971909edeSRobert Watson 	 * the suspended state regardless of whether accounting remains
22071909edeSRobert Watson 	 * enabled.
221c7d893deSDavid Greenman 	 */
22271909edeSRobert Watson 	acct_suspended = 0;
22311178ee4SJohn Baldwin 	if (acct_vp != NULL) {
22411178ee4SJohn Baldwin 		vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
225505a1493SJohn Baldwin 		error = acct_disable(td);
22611178ee4SJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
22711178ee4SJohn Baldwin 	}
228d1e405c5SAlfred Perlstein 	if (uap->path == NULL) {
229505a1493SJohn Baldwin 		if (acct_state & ACCT_RUNNING) {
230505a1493SJohn Baldwin 			acct_state |= ACCT_EXITREQ;
231505a1493SJohn Baldwin 			wakeup(&acct_state);
232505a1493SJohn Baldwin 		}
23371909edeSRobert Watson 		sx_xunlock(&acct_sx);
23411178ee4SJohn Baldwin 		return (error);
235b4dcc46aSAndrew R. Reiter 	}
236c7d893deSDavid Greenman 
237c7d893deSDavid Greenman 	/*
238c7d893deSDavid Greenman 	 * Save the new accounting file vnode, and schedule the new
239c7d893deSDavid Greenman 	 * free space watcher.
240c7d893deSDavid Greenman 	 */
24171909edeSRobert Watson 	acct_vp = nd.ni_vp;
24271909edeSRobert Watson 	acct_cred = crhold(td->td_ucred);
24371909edeSRobert Watson 	acct_flags = flags;
244505a1493SJohn Baldwin 	if (acct_state & ACCT_RUNNING)
245505a1493SJohn Baldwin 		acct_state &= ~ACCT_EXITREQ;
246505a1493SJohn Baldwin 	else {
247505a1493SJohn Baldwin 		/*
248505a1493SJohn Baldwin 		 * Try to start up an accounting kthread.  We may start more
249505a1493SJohn Baldwin 		 * than one, but if so the extras will commit suicide as
250505a1493SJohn Baldwin 		 * soon as they start up.
251505a1493SJohn Baldwin 		 */
252505a1493SJohn Baldwin 		error = kthread_create(acct_thread, NULL, NULL, 0, 0,
253505a1493SJohn Baldwin 		    "accounting");
254505a1493SJohn Baldwin 		if (error) {
25511178ee4SJohn Baldwin 			vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
256505a1493SJohn Baldwin 			(void) vn_close(acct_vp, acct_flags, acct_cred, td);
25711178ee4SJohn Baldwin 			VFS_UNLOCK_GIANT(vfslocked);
258505a1493SJohn Baldwin 			crfree(acct_cred);
259101581b0SRobert Watson 			acct_configured = 0;
260505a1493SJohn Baldwin 			acct_vp = NULL;
261505a1493SJohn Baldwin 			acct_cred = NULL;
262505a1493SJohn Baldwin 			acct_flags = 0;
263505a1493SJohn Baldwin 			sx_xunlock(&acct_sx);
264505a1493SJohn Baldwin 			log(LOG_NOTICE, "Unable to start accounting thread\n");
26511178ee4SJohn Baldwin 			return (error);
266505a1493SJohn Baldwin 		}
267505a1493SJohn Baldwin 	}
268101581b0SRobert Watson 	acct_configured = 1;
26971909edeSRobert Watson 	sx_xunlock(&acct_sx);
27048719ca7SBosko Milekic 	log(LOG_NOTICE, "Accounting enabled\n");
271c7d893deSDavid Greenman 	return (error);
272c7d893deSDavid Greenman }
273c7d893deSDavid Greenman 
274c7d893deSDavid Greenman /*
275505a1493SJohn Baldwin  * Disable currently in-progress accounting by closing the vnode, dropping
276505a1493SJohn Baldwin  * our reference to the credential, and clearing the vnode's flags.
277505a1493SJohn Baldwin  */
278505a1493SJohn Baldwin static int
279505a1493SJohn Baldwin acct_disable(struct thread *td)
280505a1493SJohn Baldwin {
281505a1493SJohn Baldwin 	int error;
282505a1493SJohn Baldwin 
283505a1493SJohn Baldwin 	sx_assert(&acct_sx, SX_XLOCKED);
284505a1493SJohn Baldwin 	error = vn_close(acct_vp, acct_flags, acct_cred, td);
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 	log(LOG_NOTICE, "Accounting disabled\n");
291505a1493SJohn Baldwin 	return (error);
292505a1493SJohn Baldwin }
293505a1493SJohn Baldwin 
294505a1493SJohn Baldwin /*
295c7d893deSDavid Greenman  * Write out process accounting information, on process exit.
296c7d893deSDavid Greenman  * Data to be written out is specified in Leffler, et al.
297c7d893deSDavid Greenman  * and are enumerated below.  (They're also noted in the system
298c7d893deSDavid Greenman  * "acct.h" header file.)
299c7d893deSDavid Greenman  */
300c7d893deSDavid Greenman int
30171909edeSRobert Watson acct_process(struct thread *td)
302c7d893deSDavid Greenman {
303c7d893deSDavid Greenman 	struct acct acct;
304c7d893deSDavid Greenman 	struct timeval ut, st, tmp;
30591d5354aSJohn Baldwin 	struct plimit *newlim, *oldlim;
30601e3f3aeSBruce Evans 	struct proc *p;
30701e3f3aeSBruce Evans 	struct rusage *r;
30871909edeSRobert Watson 	int t, ret, vfslocked;
3094f39d5d5SAndrew R. Reiter 
3102b05b557SRobert Watson 	/*
3112b05b557SRobert Watson 	 * Lockless check of accounting condition before doing the hard
3122b05b557SRobert Watson 	 * work.
3132b05b557SRobert Watson 	 */
31471909edeSRobert Watson 	if (acct_vp == NULL || acct_suspended)
3152b05b557SRobert Watson 		return (0);
3162b05b557SRobert Watson 
31771909edeSRobert Watson 	sx_slock(&acct_sx);
318c7d893deSDavid Greenman 
3192b05b557SRobert Watson 	/*
3202b05b557SRobert Watson 	 * If accounting isn't enabled, don't bother.  Have to check again
3212b05b557SRobert Watson 	 * once we own the lock in case we raced with disabling of accounting
3222b05b557SRobert Watson 	 * by another thread.
3232b05b557SRobert Watson 	 */
32471909edeSRobert Watson 	if (acct_vp == NULL || acct_suspended) {
32571909edeSRobert Watson 		sx_sunlock(&acct_sx);
326c7d893deSDavid Greenman 		return (0);
3274f39d5d5SAndrew R. Reiter 	}
328c7d893deSDavid Greenman 
32901e3f3aeSBruce Evans 	p = td->td_proc;
33001e3f3aeSBruce Evans 
331c7d893deSDavid Greenman 	/*
332c7d893deSDavid Greenman 	 * Get process accounting information.
333c7d893deSDavid Greenman 	 */
334c7d893deSDavid Greenman 
3357e653dbdSJohn Baldwin 	PROC_LOCK(p);
336c7d893deSDavid Greenman 	/* (1) The name of the command that ran */
337c7d893deSDavid Greenman 	bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
338c7d893deSDavid Greenman 
339c7d893deSDavid Greenman 	/* (2) The amount of user and system time that was used */
34078c85e8dSJohn Baldwin 	calcru(p, &ut, &st);
341c7d893deSDavid Greenman 	acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
342c7d893deSDavid Greenman 	acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
343c7d893deSDavid Greenman 
3445f9ae8e0SGiorgos Keramidas 	/* (3) The elapsed time the command ran (and its starting time) */
34587ccef7bSDag-Erling Smørgrav 	tmp = boottime;
34687ccef7bSDag-Erling Smørgrav 	timevaladd(&tmp, &p->p_stats->p_start);
34787ccef7bSDag-Erling Smørgrav 	acct.ac_btime = tmp.tv_sec;
34887ccef7bSDag-Erling Smørgrav 	microuptime(&tmp);
349c7d893deSDavid Greenman 	timevalsub(&tmp, &p->p_stats->p_start);
350c7d893deSDavid Greenman 	acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
351c7d893deSDavid Greenman 
352c7d893deSDavid Greenman 	/* (4) The average amount of memory used */
353c7d893deSDavid Greenman 	r = &p->p_stats->p_ru;
354c7d893deSDavid Greenman 	tmp = ut;
355c7d893deSDavid Greenman 	timevaladd(&tmp, &st);
356c7d893deSDavid Greenman 	t = tmp.tv_sec * hz + tmp.tv_usec / tick;
357c7d893deSDavid Greenman 	if (t)
358c7d893deSDavid Greenman 		acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
359c7d893deSDavid Greenman 	else
360c7d893deSDavid Greenman 		acct.ac_mem = 0;
361c7d893deSDavid Greenman 
362c7d893deSDavid Greenman 	/* (5) The number of disk I/O operations done */
363c7d893deSDavid Greenman 	acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
364c7d893deSDavid Greenman 
365c7d893deSDavid Greenman 	/* (6) The UID and GID of the process */
366b1fc0ec1SRobert Watson 	acct.ac_uid = p->p_ucred->cr_ruid;
367b1fc0ec1SRobert Watson 	acct.ac_gid = p->p_ucred->cr_rgid;
368c7d893deSDavid Greenman 
369c7d893deSDavid Greenman 	/* (7) The terminal from which the process was started */
370f591779bSSeigo Tanimura 	SESS_LOCK(p->p_session);
371c7d893deSDavid Greenman 	if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
37223d76283SPoul-Henning Kamp 		acct.ac_tty = dev2udev(p->p_pgrp->pg_session->s_ttyp->t_dev);
373c7d893deSDavid Greenman 	else
374f3732fd1SPoul-Henning Kamp 		acct.ac_tty = NODEV;
375f591779bSSeigo Tanimura 	SESS_UNLOCK(p->p_session);
376c7d893deSDavid Greenman 
377c7d893deSDavid Greenman 	/* (8) The boolean flags that tell how the process terminated, etc. */
378c7d893deSDavid Greenman 	acct.ac_flag = p->p_acflag;
3797e653dbdSJohn Baldwin 	PROC_UNLOCK(p);
380c7d893deSDavid Greenman 
381c7d893deSDavid Greenman 	/*
382b5afad71SDavid Greenman 	 * Eliminate any file size rlimit.
383b5afad71SDavid Greenman 	 */
38491d5354aSJohn Baldwin 	newlim = lim_alloc();
38591d5354aSJohn Baldwin 	PROC_LOCK(p);
38691d5354aSJohn Baldwin 	oldlim = p->p_limit;
38791d5354aSJohn Baldwin 	lim_copy(newlim, oldlim);
38891d5354aSJohn Baldwin 	newlim->pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
38991d5354aSJohn Baldwin 	p->p_limit = newlim;
39091d5354aSJohn Baldwin 	PROC_UNLOCK(p);
39191d5354aSJohn Baldwin 	lim_free(oldlim);
392b5afad71SDavid Greenman 
39301e3f3aeSBruce Evans 	/*
39401e3f3aeSBruce Evans 	 * Write the accounting information to the file.
39501e3f3aeSBruce Evans 	 */
39671909edeSRobert Watson 	vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
39771909edeSRobert Watson 	VOP_LEASE(acct_vp, td, acct_cred, LEASE_WRITE);
39871909edeSRobert Watson 	ret = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct, sizeof (acct),
39971909edeSRobert Watson 	    (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acct_cred, NOCRED,
4004f39d5d5SAndrew R. Reiter 	    (int *)0, td);
40171909edeSRobert Watson 	VFS_UNLOCK_GIANT(vfslocked);
40271909edeSRobert Watson 	sx_sunlock(&acct_sx);
4034f39d5d5SAndrew R. Reiter 	return (ret);
404c7d893deSDavid Greenman }
405c7d893deSDavid Greenman 
406c7d893deSDavid Greenman /*
407c7d893deSDavid Greenman  * Encode_comp_t converts from ticks in seconds and microseconds
408c7d893deSDavid Greenman  * to ticks in 1/AHZ seconds.  The encoding is described in
409c7d893deSDavid Greenman  * Leffler, et al., on page 63.
410c7d893deSDavid Greenman  */
411c7d893deSDavid Greenman 
412c7d893deSDavid Greenman #define	MANTSIZE	13			/* 13 bit mantissa. */
413c7d893deSDavid Greenman #define	EXPSIZE		3			/* Base 8 (3 bit) exponent. */
414c7d893deSDavid Greenman #define	MAXFRACT	((1 << MANTSIZE) - 1)	/* Maximum fractional value. */
415c7d893deSDavid Greenman 
41687b6de2bSPoul-Henning Kamp static comp_t
41771909edeSRobert Watson encode_comp_t(u_long s, u_long us)
418c7d893deSDavid Greenman {
419c7d893deSDavid Greenman 	int exp, rnd;
420c7d893deSDavid Greenman 
421c7d893deSDavid Greenman 	exp = 0;
422c7d893deSDavid Greenman 	rnd = 0;
423c7d893deSDavid Greenman 	s *= AHZ;
424c7d893deSDavid Greenman 	s += us / (1000000 / AHZ);	/* Maximize precision. */
425c7d893deSDavid Greenman 
426c7d893deSDavid Greenman 	while (s > MAXFRACT) {
427c7d893deSDavid Greenman 	rnd = s & (1 << (EXPSIZE - 1));	/* Round up? */
428c7d893deSDavid Greenman 		s >>= EXPSIZE;		/* Base 8 exponent == 3 bit shift. */
429c7d893deSDavid Greenman 		exp++;
430c7d893deSDavid Greenman 	}
431c7d893deSDavid Greenman 
432c7d893deSDavid Greenman 	/* If we need to round up, do it (and handle overflow correctly). */
433c7d893deSDavid Greenman 	if (rnd && (++s > MAXFRACT)) {
434c7d893deSDavid Greenman 		s >>= EXPSIZE;
435c7d893deSDavid Greenman 		exp++;
436c7d893deSDavid Greenman 	}
437c7d893deSDavid Greenman 
438c7d893deSDavid Greenman 	/* Clean it up and polish it off. */
439c7d893deSDavid Greenman 	exp <<= MANTSIZE;		/* Shift the exponent into place */
440c7d893deSDavid Greenman 	exp += s;			/* and add on the mantissa. */
441c7d893deSDavid Greenman 	return (exp);
442c7d893deSDavid Greenman }
443c7d893deSDavid Greenman 
444c7d893deSDavid Greenman /*
445c7d893deSDavid Greenman  * Periodically check the filesystem to see if accounting
446c7d893deSDavid Greenman  * should be turned on or off.  Beware the case where the vnode
447c7d893deSDavid Greenman  * has been vgone()'d out from underneath us, e.g. when the file
448c7d893deSDavid Greenman  * system containing the accounting file has been forcibly unmounted.
449c7d893deSDavid Greenman  */
450df8bae1dSRodney W. Grimes /* ARGSUSED */
45187b6de2bSPoul-Henning Kamp static void
452505a1493SJohn Baldwin acctwatch(void)
453df8bae1dSRodney W. Grimes {
454df8bae1dSRodney W. Grimes 	struct statfs sb;
45571909edeSRobert Watson 	int vfslocked;
456df8bae1dSRodney W. Grimes 
457505a1493SJohn Baldwin 	sx_assert(&acct_sx, SX_XLOCKED);
458505a1493SJohn Baldwin 
459505a1493SJohn Baldwin 	/*
460505a1493SJohn Baldwin 	 * If accounting was disabled before our kthread was scheduled,
461505a1493SJohn Baldwin 	 * then acct_vp might be NULL.  If so, just ask our kthread to
462505a1493SJohn Baldwin 	 * exit and return.
463505a1493SJohn Baldwin 	 */
464505a1493SJohn Baldwin 	if (acct_vp == NULL) {
465505a1493SJohn Baldwin 		acct_state |= ACCT_EXITREQ;
466c7d893deSDavid Greenman 		return;
467c7d893deSDavid Greenman 	}
468505a1493SJohn Baldwin 
469505a1493SJohn Baldwin 	/*
470505a1493SJohn Baldwin 	 * If our vnode is no longer valid, tear it down and signal the
471505a1493SJohn Baldwin 	 * accounting thread to die.
472505a1493SJohn Baldwin 	 */
473505a1493SJohn Baldwin 	vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
474505a1493SJohn Baldwin 	if (acct_vp->v_type == VBAD) {
475505a1493SJohn Baldwin 		(void) acct_disable(NULL);
476505a1493SJohn Baldwin 		VFS_UNLOCK_GIANT(vfslocked);
477505a1493SJohn Baldwin 		acct_state |= ACCT_EXITREQ;
478505a1493SJohn Baldwin 		return;
479505a1493SJohn Baldwin 	}
480505a1493SJohn Baldwin 
48171909edeSRobert Watson 	/*
48271909edeSRobert Watson 	 * Stopping here is better than continuing, maybe it will be VBAD
48371909edeSRobert Watson 	 * next time around.
48471909edeSRobert Watson 	 */
48571909edeSRobert Watson 	if (VFS_STATFS(acct_vp->v_mount, &sb, curthread) < 0) {
48671909edeSRobert Watson 		VFS_UNLOCK_GIANT(vfslocked);
48771909edeSRobert Watson 		return;
48871909edeSRobert Watson 	}
48971909edeSRobert Watson 	VFS_UNLOCK_GIANT(vfslocked);
49071909edeSRobert Watson 	if (acct_suspended) {
49171909edeSRobert Watson 		if (sb.f_bavail > (int64_t)(acctresume * sb.f_blocks /
49271909edeSRobert Watson 		    100)) {
49371909edeSRobert Watson 			acct_suspended = 0;
494df8bae1dSRodney W. Grimes 			log(LOG_NOTICE, "Accounting resumed\n");
495df8bae1dSRodney W. Grimes 		}
496996c772fSJohn Dyson 	} else {
49771909edeSRobert Watson 		if (sb.f_bavail <= (int64_t)(acctsuspend * sb.f_blocks /
49871909edeSRobert Watson 		    100)) {
49971909edeSRobert Watson 			acct_suspended = 1;
500df8bae1dSRodney W. Grimes 			log(LOG_NOTICE, "Accounting suspended\n");
501df8bae1dSRodney W. Grimes 		}
502996c772fSJohn Dyson 	}
503505a1493SJohn Baldwin }
504505a1493SJohn Baldwin 
505505a1493SJohn Baldwin /*
506505a1493SJohn Baldwin  * The main loop for the dedicated kernel thread that periodically calls
507505a1493SJohn Baldwin  * acctwatch().
508505a1493SJohn Baldwin  */
509505a1493SJohn Baldwin static void
510505a1493SJohn Baldwin acct_thread(void *dummy)
511505a1493SJohn Baldwin {
512505a1493SJohn Baldwin 	u_char pri;
513505a1493SJohn Baldwin 
514505a1493SJohn Baldwin 	/* This is a low-priority kernel thread. */
515505a1493SJohn Baldwin 	pri = PRI_MAX_KERN;
516505a1493SJohn Baldwin 	mtx_lock_spin(&sched_lock);
517505a1493SJohn Baldwin 	sched_prio(curthread, pri);
518505a1493SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
519505a1493SJohn Baldwin 
520505a1493SJohn Baldwin 	/* If another accounting kthread is already running, just die. */
521505a1493SJohn Baldwin 	sx_xlock(&acct_sx);
522505a1493SJohn Baldwin 	if (acct_state & ACCT_RUNNING) {
52371909edeSRobert Watson 		sx_xunlock(&acct_sx);
524505a1493SJohn Baldwin 		kthread_exit(0);
525505a1493SJohn Baldwin 	}
526505a1493SJohn Baldwin 	acct_state |= ACCT_RUNNING;
527505a1493SJohn Baldwin 
528505a1493SJohn Baldwin 	/* Loop until we are asked to exit. */
529505a1493SJohn Baldwin 	while (!(acct_state & ACCT_EXITREQ)) {
530505a1493SJohn Baldwin 
531505a1493SJohn Baldwin 		/* Perform our periodic checks. */
532505a1493SJohn Baldwin 		acctwatch();
533505a1493SJohn Baldwin 
534505a1493SJohn Baldwin 		/*
535505a1493SJohn Baldwin 		 * We check this flag again before sleeping since the
536505a1493SJohn Baldwin 		 * acctwatch() might have shut down accounting and asked us
537505a1493SJohn Baldwin 		 * to exit.
538505a1493SJohn Baldwin 		 */
539505a1493SJohn Baldwin 		if (!(acct_state & ACCT_EXITREQ)) {
540505a1493SJohn Baldwin 			sx_xunlock(&acct_sx);
541505a1493SJohn Baldwin 			tsleep(&acct_state, pri, "-", acctchkfreq * hz);
542505a1493SJohn Baldwin 			sx_xlock(&acct_sx);
543505a1493SJohn Baldwin 		}
544505a1493SJohn Baldwin 	}
545505a1493SJohn Baldwin 
546505a1493SJohn Baldwin 	/*
547505a1493SJohn Baldwin 	 * Acknowledge the exit request and shutdown.  We clear both the
548505a1493SJohn Baldwin 	 * exit request and running flags.
549505a1493SJohn Baldwin 	 */
550505a1493SJohn Baldwin 	acct_state = 0;
551505a1493SJohn Baldwin 	sx_xunlock(&acct_sx);
552505a1493SJohn Baldwin 	kthread_exit(0);
553df8bae1dSRodney W. Grimes }
554