xref: /freebsd/sys/kern/kern_acct.c (revision ab36c06737cfd52b6f1999903ee7f91014ac51de)
1df8bae1dSRodney W. Grimes /*-
2c7d893deSDavid Greenman  * Copyright (c) 1994 Christopher G. Demetriou
3df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
4df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
5df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
6df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
7df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
8df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
9df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
10df8bae1dSRodney W. Grimes  *
11df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
12df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
13df8bae1dSRodney W. Grimes  * are met:
14df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
16df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
18df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
20df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
21df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
22df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
23df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
24df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
25df8bae1dSRodney W. Grimes  *    without specific prior written permission.
26df8bae1dSRodney W. Grimes  *
27df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
38df8bae1dSRodney W. Grimes  *
39c7d893deSDavid Greenman  *	@(#)kern_acct.c	8.1 (Berkeley) 6/14/93
40ab36c067SJustin T. Gibbs  *	$Id: kern_acct.c,v 1.16 1997/09/02 20:05:36 bde Exp $
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
440ad076d5SBruce Evans #include <sys/systm.h>
45d2d3e875SBruce Evans #include <sys/sysproto.h>
46df8bae1dSRodney W. Grimes #include <sys/proc.h>
47df8bae1dSRodney W. Grimes #include <sys/mount.h>
48df8bae1dSRodney W. Grimes #include <sys/vnode.h>
493ac4d1efSBruce Evans #include <sys/fcntl.h>
50df8bae1dSRodney W. Grimes #include <sys/syslog.h>
51df8bae1dSRodney W. Grimes #include <sys/kernel.h>
52996c772fSJohn Dyson #include <sys/sysent.h>
5387b6de2bSPoul-Henning Kamp #include <sys/sysctl.h>
54c7d893deSDavid Greenman #include <sys/namei.h>
55c7d893deSDavid Greenman #include <sys/acct.h>
56c7d893deSDavid Greenman #include <sys/resourcevar.h>
57c7d893deSDavid Greenman #include <sys/tty.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes /*
60c7d893deSDavid Greenman  * The routines implemented in this file are described in:
61c7d893deSDavid Greenman  *      Leffler, et al.: The Design and Implementation of the 4.3BSD
62c7d893deSDavid Greenman  *	    UNIX Operating System (Addison Welley, 1989)
63c7d893deSDavid Greenman  * on pages 62-63.
64c7d893deSDavid Greenman  *
65c7d893deSDavid Greenman  * Arguably, to simplify accounting operations, this mechanism should
66c7d893deSDavid Greenman  * be replaced by one in which an accounting log file (similar to /dev/klog)
67c7d893deSDavid Greenman  * is read by a user process, etc.  However, that has its own problems.
68df8bae1dSRodney W. Grimes  */
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes /*
71c7d893deSDavid Greenman  * Internal accounting functions.
72c7d893deSDavid Greenman  * The former's operation is described in Leffler, et al., and the latter
73c7d893deSDavid Greenman  * was provided by UCB with the 4.4BSD-Lite release
74df8bae1dSRodney W. Grimes  */
7587b6de2bSPoul-Henning Kamp static comp_t	encode_comp_t __P((u_long, u_long));
7687b6de2bSPoul-Henning Kamp static void	acctwatch __P((void *));
77c7d893deSDavid Greenman 
78c7d893deSDavid Greenman /*
79ab36c067SJustin T. Gibbs  * Accounting callout handle used for periodic scheduling of
80ab36c067SJustin T. Gibbs  * acctwatch.
81ab36c067SJustin T. Gibbs  */
82ab36c067SJustin T. Gibbs static struct	callout_handle acctwatch_handle
83ab36c067SJustin T. Gibbs     = CALLOUT_HANDLE_INITIALIZER(&acctwatch_handle);
84ab36c067SJustin T. Gibbs 
85ab36c067SJustin T. Gibbs /*
86c7d893deSDavid Greenman  * Accounting vnode pointer, and saved vnode pointer.
87c7d893deSDavid Greenman  */
8887b6de2bSPoul-Henning Kamp static struct	vnode *acctp;
8987b6de2bSPoul-Henning Kamp static struct	vnode *savacctp;
90df8bae1dSRodney W. Grimes 
91df8bae1dSRodney W. Grimes /*
92df8bae1dSRodney W. Grimes  * Values associated with enabling and disabling accounting
93df8bae1dSRodney W. Grimes  */
9487b6de2bSPoul-Henning Kamp static int acctsuspend = 2;	/* stop accounting when < 2% free space left */
9587b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW,
9687b6de2bSPoul-Henning Kamp 	&acctsuspend, 0, "");
9787b6de2bSPoul-Henning Kamp 
9887b6de2bSPoul-Henning Kamp static int acctresume = 4;	/* resume when free space risen to > 4% */
9987b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW,
10087b6de2bSPoul-Henning Kamp 	&acctresume, 0, "");
10187b6de2bSPoul-Henning Kamp 
10287b6de2bSPoul-Henning Kamp static int acctchkfreq = 15;	/* frequency (in seconds) to check space */
10387b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_chkfreq, CTLFLAG_RW,
10487b6de2bSPoul-Henning Kamp 	&acctchkfreq, 0, "");
105df8bae1dSRodney W. Grimes 
106df8bae1dSRodney W. Grimes /*
107c7d893deSDavid Greenman  * Accounting system call.  Written based on the specification and
108c7d893deSDavid Greenman  * previous implementation done by Mark Tinguely.
109df8bae1dSRodney W. Grimes  */
110c7d893deSDavid Greenman int
111996c772fSJohn Dyson acct(a1, uap, a3)
112996c772fSJohn Dyson 	struct proc *a1;
113996c772fSJohn Dyson 	struct acct_args /* {
114996c772fSJohn Dyson 		syscallarg(char *) path;
115996c772fSJohn Dyson 	} */ *uap;
116996c772fSJohn Dyson 	int *a3;
117c7d893deSDavid Greenman {
118996c772fSJohn Dyson 	struct proc *p = curproc;	/* XXX */
119c7d893deSDavid Greenman 	struct nameidata nd;
120c7d893deSDavid Greenman 	int error;
121c7d893deSDavid Greenman 
122c7d893deSDavid Greenman 	/* Make sure that the caller is root. */
123797f2d22SPoul-Henning Kamp 	error = suser(p->p_ucred, &p->p_acflag);
124797f2d22SPoul-Henning Kamp 	if (error)
125c7d893deSDavid Greenman 		return (error);
126c7d893deSDavid Greenman 
127c7d893deSDavid Greenman 	/*
128c7d893deSDavid Greenman 	 * If accounting is to be started to a file, open that file for
129c7d893deSDavid Greenman 	 * writing and make sure it's a 'normal'.
130c7d893deSDavid Greenman 	 */
131996c772fSJohn Dyson 	if (SCARG(uap, path) != NULL) {
132996c772fSJohn Dyson 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path),
133996c772fSJohn Dyson 		       p);
134797f2d22SPoul-Henning Kamp 		error = vn_open(&nd, FWRITE, 0);
135797f2d22SPoul-Henning Kamp 		if (error)
136c7d893deSDavid Greenman 			return (error);
137996c772fSJohn Dyson 		VOP_UNLOCK(nd.ni_vp, 0, p);
138c7d893deSDavid Greenman 		if (nd.ni_vp->v_type != VREG) {
139c7d893deSDavid Greenman 			vn_close(nd.ni_vp, FWRITE, p->p_ucred, p);
140c7d893deSDavid Greenman 			return (EACCES);
141c7d893deSDavid Greenman 		}
142c7d893deSDavid Greenman 	}
143c7d893deSDavid Greenman 
144c7d893deSDavid Greenman 	/*
145c7d893deSDavid Greenman 	 * If accounting was previously enabled, kill the old space-watcher,
146c7d893deSDavid Greenman 	 * close the file, and (if no new file was specified, leave).
147c7d893deSDavid Greenman 	 */
148c7d893deSDavid Greenman 	if (acctp != NULLVP || savacctp != NULLVP) {
149ab36c067SJustin T. Gibbs 		untimeout(acctwatch, NULL, acctwatch_handle);
150c7d893deSDavid Greenman 		error = vn_close((acctp != NULLVP ? acctp : savacctp), FWRITE,
151c7d893deSDavid Greenman 		    p->p_ucred, p);
152c7d893deSDavid Greenman 		acctp = savacctp = NULLVP;
153c7d893deSDavid Greenman 	}
154996c772fSJohn Dyson 	if (SCARG(uap, path) == NULL)
155c7d893deSDavid Greenman 		return (error);
156c7d893deSDavid Greenman 
157c7d893deSDavid Greenman 	/*
158c7d893deSDavid Greenman 	 * Save the new accounting file vnode, and schedule the new
159c7d893deSDavid Greenman 	 * free space watcher.
160c7d893deSDavid Greenman 	 */
161c7d893deSDavid Greenman 	acctp = nd.ni_vp;
162c7d893deSDavid Greenman 	acctwatch(NULL);
163c7d893deSDavid Greenman 	return (error);
164c7d893deSDavid Greenman }
165c7d893deSDavid Greenman 
166c7d893deSDavid Greenman /*
167c7d893deSDavid Greenman  * Write out process accounting information, on process exit.
168c7d893deSDavid Greenman  * Data to be written out is specified in Leffler, et al.
169c7d893deSDavid Greenman  * and are enumerated below.  (They're also noted in the system
170c7d893deSDavid Greenman  * "acct.h" header file.)
171c7d893deSDavid Greenman  */
172c7d893deSDavid Greenman 
173c7d893deSDavid Greenman int
174c7d893deSDavid Greenman acct_process(p)
175c7d893deSDavid Greenman 	struct proc *p;
176c7d893deSDavid Greenman {
177c7d893deSDavid Greenman 	struct acct acct;
178c7d893deSDavid Greenman 	struct rusage *r;
179c7d893deSDavid Greenman 	struct timeval ut, st, tmp;
180a98ca469SPoul-Henning Kamp 	int t;
181c7d893deSDavid Greenman 	struct vnode *vp;
182c7d893deSDavid Greenman 
183c7d893deSDavid Greenman 	/* If accounting isn't enabled, don't bother */
184c7d893deSDavid Greenman 	vp = acctp;
185c7d893deSDavid Greenman 	if (vp == NULLVP)
186c7d893deSDavid Greenman 		return (0);
187c7d893deSDavid Greenman 
188c7d893deSDavid Greenman 	/*
189c7d893deSDavid Greenman 	 * Get process accounting information.
190c7d893deSDavid Greenman 	 */
191c7d893deSDavid Greenman 
192c7d893deSDavid Greenman 	/* (1) The name of the command that ran */
193c7d893deSDavid Greenman 	bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
194c7d893deSDavid Greenman 
195c7d893deSDavid Greenman 	/* (2) The amount of user and system time that was used */
196c7d893deSDavid Greenman 	calcru(p, &ut, &st, NULL);
197c7d893deSDavid Greenman 	acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
198c7d893deSDavid Greenman 	acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
199c7d893deSDavid Greenman 
200c7d893deSDavid Greenman 	/* (3) The elapsed time the commmand ran (and its starting time) */
201c7d893deSDavid Greenman 	acct.ac_btime = p->p_stats->p_start.tv_sec;
20257585a8eSMike Pritchard 	microtime(&tmp);
203c7d893deSDavid Greenman 	timevalsub(&tmp, &p->p_stats->p_start);
204c7d893deSDavid Greenman 	acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
205c7d893deSDavid Greenman 
206c7d893deSDavid Greenman 	/* (4) The average amount of memory used */
207c7d893deSDavid Greenman 	r = &p->p_stats->p_ru;
208c7d893deSDavid Greenman 	tmp = ut;
209c7d893deSDavid Greenman 	timevaladd(&tmp, &st);
210c7d893deSDavid Greenman 	t = tmp.tv_sec * hz + tmp.tv_usec / tick;
211c7d893deSDavid Greenman 	if (t)
212c7d893deSDavid Greenman 		acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
213c7d893deSDavid Greenman 	else
214c7d893deSDavid Greenman 		acct.ac_mem = 0;
215c7d893deSDavid Greenman 
216c7d893deSDavid Greenman 	/* (5) The number of disk I/O operations done */
217c7d893deSDavid Greenman 	acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
218c7d893deSDavid Greenman 
219c7d893deSDavid Greenman 	/* (6) The UID and GID of the process */
220c7d893deSDavid Greenman 	acct.ac_uid = p->p_cred->p_ruid;
221c7d893deSDavid Greenman 	acct.ac_gid = p->p_cred->p_rgid;
222c7d893deSDavid Greenman 
223c7d893deSDavid Greenman 	/* (7) The terminal from which the process was started */
224c7d893deSDavid Greenman 	if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
225c7d893deSDavid Greenman 		acct.ac_tty = p->p_pgrp->pg_session->s_ttyp->t_dev;
226c7d893deSDavid Greenman 	else
227c7d893deSDavid Greenman 		acct.ac_tty = NODEV;
228c7d893deSDavid Greenman 
229c7d893deSDavid Greenman 	/* (8) The boolean flags that tell how the process terminated, etc. */
230c7d893deSDavid Greenman 	acct.ac_flag = p->p_acflag;
231c7d893deSDavid Greenman 
232c7d893deSDavid Greenman 	/*
233c7d893deSDavid Greenman 	 * Now, just write the accounting information to the file.
234c7d893deSDavid Greenman 	 */
235996c772fSJohn Dyson 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
236c7d893deSDavid Greenman 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t)&acct, sizeof (acct),
237c7d893deSDavid Greenman 	    (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, p->p_ucred,
238c7d893deSDavid Greenman 	    (int *)0, p));
239c7d893deSDavid Greenman }
240c7d893deSDavid Greenman 
241c7d893deSDavid Greenman /*
242c7d893deSDavid Greenman  * Encode_comp_t converts from ticks in seconds and microseconds
243c7d893deSDavid Greenman  * to ticks in 1/AHZ seconds.  The encoding is described in
244c7d893deSDavid Greenman  * Leffler, et al., on page 63.
245c7d893deSDavid Greenman  */
246c7d893deSDavid Greenman 
247c7d893deSDavid Greenman #define	MANTSIZE	13			/* 13 bit mantissa. */
248c7d893deSDavid Greenman #define	EXPSIZE		3			/* Base 8 (3 bit) exponent. */
249c7d893deSDavid Greenman #define	MAXFRACT	((1 << MANTSIZE) - 1)	/* Maximum fractional value. */
250c7d893deSDavid Greenman 
25187b6de2bSPoul-Henning Kamp static comp_t
252c7d893deSDavid Greenman encode_comp_t(s, us)
253c7d893deSDavid Greenman 	u_long s, us;
254c7d893deSDavid Greenman {
255c7d893deSDavid Greenman 	int exp, rnd;
256c7d893deSDavid Greenman 
257c7d893deSDavid Greenman 	exp = 0;
258c7d893deSDavid Greenman 	rnd = 0;
259c7d893deSDavid Greenman 	s *= AHZ;
260c7d893deSDavid Greenman 	s += us / (1000000 / AHZ);	/* Maximize precision. */
261c7d893deSDavid Greenman 
262c7d893deSDavid Greenman 	while (s > MAXFRACT) {
263c7d893deSDavid Greenman 	rnd = s & (1 << (EXPSIZE - 1));	/* Round up? */
264c7d893deSDavid Greenman 		s >>= EXPSIZE;		/* Base 8 exponent == 3 bit shift. */
265c7d893deSDavid Greenman 		exp++;
266c7d893deSDavid Greenman 	}
267c7d893deSDavid Greenman 
268c7d893deSDavid Greenman 	/* If we need to round up, do it (and handle overflow correctly). */
269c7d893deSDavid Greenman 	if (rnd && (++s > MAXFRACT)) {
270c7d893deSDavid Greenman 		s >>= EXPSIZE;
271c7d893deSDavid Greenman 		exp++;
272c7d893deSDavid Greenman 	}
273c7d893deSDavid Greenman 
274c7d893deSDavid Greenman 	/* Clean it up and polish it off. */
275c7d893deSDavid Greenman 	exp <<= MANTSIZE;		/* Shift the exponent into place */
276c7d893deSDavid Greenman 	exp += s;			/* and add on the mantissa. */
277c7d893deSDavid Greenman 	return (exp);
278c7d893deSDavid Greenman }
279c7d893deSDavid Greenman 
280c7d893deSDavid Greenman /*
281c7d893deSDavid Greenman  * Periodically check the file system to see if accounting
282c7d893deSDavid Greenman  * should be turned on or off.  Beware the case where the vnode
283c7d893deSDavid Greenman  * has been vgone()'d out from underneath us, e.g. when the file
284c7d893deSDavid Greenman  * system containing the accounting file has been forcibly unmounted.
285c7d893deSDavid Greenman  */
286df8bae1dSRodney W. Grimes /* ARGSUSED */
28787b6de2bSPoul-Henning Kamp static void
288df8bae1dSRodney W. Grimes acctwatch(a)
289df8bae1dSRodney W. Grimes 	void *a;
290df8bae1dSRodney W. Grimes {
291df8bae1dSRodney W. Grimes 	struct statfs sb;
292df8bae1dSRodney W. Grimes 
293c7d893deSDavid Greenman 	if (savacctp != NULLVP) {
294c7d893deSDavid Greenman 		if (savacctp->v_type == VBAD) {
295c7d893deSDavid Greenman 			(void) vn_close(savacctp, FWRITE, NOCRED, NULL);
296c7d893deSDavid Greenman 			savacctp = NULLVP;
297c7d893deSDavid Greenman 			return;
298c7d893deSDavid Greenman 		}
299df8bae1dSRodney W. Grimes 		(void)VFS_STATFS(savacctp->v_mount, &sb, (struct proc *)0);
300df8bae1dSRodney W. Grimes 		if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
301df8bae1dSRodney W. Grimes 			acctp = savacctp;
302c7d893deSDavid Greenman 			savacctp = NULLVP;
303df8bae1dSRodney W. Grimes 			log(LOG_NOTICE, "Accounting resumed\n");
304df8bae1dSRodney W. Grimes 		}
305996c772fSJohn Dyson 	} else {
306996c772fSJohn Dyson 		if (acctp == NULLVP)
307996c772fSJohn Dyson 			return;
308c7d893deSDavid Greenman 		if (acctp->v_type == VBAD) {
309c7d893deSDavid Greenman 			(void) vn_close(acctp, FWRITE, NOCRED, NULL);
310c7d893deSDavid Greenman 			acctp = NULLVP;
311df8bae1dSRodney W. Grimes 			return;
312c7d893deSDavid Greenman 		}
313df8bae1dSRodney W. Grimes 		(void)VFS_STATFS(acctp->v_mount, &sb, (struct proc *)0);
314df8bae1dSRodney W. Grimes 		if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
315df8bae1dSRodney W. Grimes 			savacctp = acctp;
316c7d893deSDavid Greenman 			acctp = NULLVP;
317df8bae1dSRodney W. Grimes 			log(LOG_NOTICE, "Accounting suspended\n");
318df8bae1dSRodney W. Grimes 		}
319996c772fSJohn Dyson 	}
320ab36c067SJustin T. Gibbs 	acctwatch_handle = timeout(acctwatch, NULL, acctchkfreq * hz);
321df8bae1dSRodney W. Grimes }
322