kern_acct.c revision 69286
1193323Sed/*-
2193323Sed * Copyright (c) 1994 Christopher G. Demetriou
3193323Sed * Copyright (c) 1982, 1986, 1989, 1993
4193323Sed *	The Regents of the University of California.  All rights reserved.
5193323Sed * (c) UNIX System Laboratories, Inc.
6193323Sed * All or some portions of this file are derived from material licensed
7193323Sed * to the University of California by American Telephone and Telegraph
8193323Sed * Co. or Unix System Laboratories, Inc. and are reproduced herein with
9193323Sed * the permission of UNIX System Laboratories, Inc.
10193323Sed *
11193323Sed * Redistribution and use in source and binary forms, with or without
12193323Sed * modification, are permitted provided that the following conditions
13193323Sed * are met:
14193323Sed * 1. Redistributions of source code must retain the above copyright
15193323Sed *    notice, this list of conditions and the following disclaimer.
16193323Sed * 2. Redistributions in binary form must reproduce the above copyright
17193323Sed *    notice, this list of conditions and the following disclaimer in the
18193323Sed *    documentation and/or other materials provided with the distribution.
19193323Sed * 3. All advertising materials mentioning features or use of this software
20193323Sed *    must display the following acknowledgement:
21193323Sed *	This product includes software developed by the University of
22193323Sed *	California, Berkeley and its contributors.
23193323Sed * 4. Neither the name of the University nor the names of its contributors
24193323Sed *    may be used to endorse or promote products derived from this software
25193323Sed *    without specific prior written permission.
26193323Sed *
27198090Srdivacky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30193323Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32198090Srdivacky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37193323Sed * SUCH DAMAGE.
38193323Sed *
39193323Sed *	@(#)kern_acct.c	8.1 (Berkeley) 6/14/93
40193323Sed * $FreeBSD: head/sys/kern/kern_acct.c 69286 2000-11-27 22:52:31Z jake $
41193323Sed */
42193323Sed
43193323Sed#include <sys/param.h>
44193323Sed#include <sys/systm.h>
45193323Sed#include <sys/sysproto.h>
46193323Sed#include <sys/proc.h>
47193323Sed#include <sys/mount.h>
48193323Sed#include <sys/vnode.h>
49202878Srdivacky#include <sys/fcntl.h>
50202878Srdivacky#include <sys/syslog.h>
51202878Srdivacky#include <sys/kernel.h>
52202878Srdivacky#include <sys/sysent.h>
53202878Srdivacky#include <sys/sysctl.h>
54193323Sed#include <sys/namei.h>
55193323Sed#include <sys/acct.h>
56193323Sed#include <sys/resourcevar.h>
57193323Sed#include <sys/tty.h>
58193323Sed
59193323Sed
60198892Srdivacky/*
61193323Sed * The routines implemented in this file are described in:
62193323Sed *      Leffler, et al.: The Design and Implementation of the 4.3BSD
63193323Sed *	    UNIX Operating System (Addison Welley, 1989)
64193323Sed * on pages 62-63.
65193323Sed *
66193323Sed * Arguably, to simplify accounting operations, this mechanism should
67193323Sed * be replaced by one in which an accounting log file (similar to /dev/klog)
68193323Sed * is read by a user process, etc.  However, that has its own problems.
69193323Sed */
70193323Sed
71193323Sed/*
72193323Sed * Internal accounting functions.
73193323Sed * The former's operation is described in Leffler, et al., and the latter
74193323Sed * was provided by UCB with the 4.4BSD-Lite release
75193323Sed */
76193323Sedstatic comp_t	encode_comp_t __P((u_long, u_long));
77193323Sedstatic void	acctwatch __P((void *));
78193323Sed
79193323Sed/*
80193323Sed * Accounting callout used for periodic scheduling of acctwatch.
81193323Sed */
82193323Sedstatic struct	callout acctwatch_callout;
83193323Sed
84193323Sed/*
85193323Sed * Accounting vnode pointer, and saved vnode pointer.
86193323Sed */
87193323Sedstatic struct	vnode *acctp;
88193323Sedstatic struct	vnode *savacctp;
89193323Sed
90193323Sed/*
91193323Sed * Values associated with enabling and disabling accounting
92193323Sed */
93193323Sedstatic int acctsuspend = 2;	/* stop accounting when < 2% free space left */
94193323SedSYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW,
95193323Sed	&acctsuspend, 0, "percentage of free disk space below which accounting stops");
96193323Sed
97193323Sedstatic int acctresume = 4;	/* resume when free space risen to > 4% */
98193323SedSYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW,
99193323Sed	&acctresume, 0, "percentage of free disk space above which accounting resumes");
100193323Sed
101193323Sedstatic int acctchkfreq = 15;	/* frequency (in seconds) to check space */
102193323SedSYSCTL_INT(_kern, OID_AUTO, acct_chkfreq, CTLFLAG_RW,
103193323Sed	&acctchkfreq, 0, "frequency for checking the free space");
104193323Sed
105193323Sed/*
106193323Sed * Accounting system call.  Written based on the specification and
107193323Sed * previous implementation done by Mark Tinguely.
108193323Sed */
109193323Sedint
110193323Sedacct(a1, uap)
111193323Sed	struct proc *a1;
112193323Sed	struct acct_args /* {
113193323Sed		syscallarg(char *) path;
114193323Sed	} */ *uap;
115193323Sed{
116193323Sed	struct proc *p = curproc;	/* XXX */
117193323Sed	struct nameidata nd;
118193323Sed	int error, flags;
119193323Sed
120193323Sed	/* Make sure that the caller is root. */
121193323Sed	error = suser(p);
122193323Sed	if (error)
123193323Sed		return (error);
124193323Sed
125193323Sed	/*
126193323Sed	 * If accounting is to be started to a file, open that file for
127193323Sed	 * writing and make sure it's a 'normal'.
128193323Sed	 */
129193323Sed	if (SCARG(uap, path) != NULL) {
130193323Sed		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path),
131193323Sed		       p);
132193323Sed		flags = FWRITE;
133193323Sed		error = vn_open(&nd, &flags, 0);
134193323Sed		if (error)
135193323Sed			return (error);
136193323Sed		NDFREE(&nd, NDF_ONLY_PNBUF);
137193323Sed		VOP_UNLOCK(nd.ni_vp, 0, p);
138193323Sed		if (nd.ni_vp->v_type != VREG) {
139193323Sed			vn_close(nd.ni_vp, FWRITE, p->p_ucred, p);
140193323Sed			return (EACCES);
141193323Sed		}
142193323Sed	}
143193323Sed
144193323Sed	/*
145193323Sed	 * If accounting was previously enabled, kill the old space-watcher,
146193323Sed	 * close the file, and (if no new file was specified, leave).
147193323Sed	 */
148193323Sed	if (acctp != NULLVP || savacctp != NULLVP) {
149193323Sed		callout_stop(&acctwatch_callout);
150193323Sed		error = vn_close((acctp != NULLVP ? acctp : savacctp), FWRITE,
151193323Sed		    p->p_ucred, p);
152193323Sed		acctp = savacctp = NULLVP;
153193323Sed	}
154193323Sed	if (SCARG(uap, path) == NULL)
155193323Sed		return (error);
156193323Sed
157193323Sed	/*
158193323Sed	 * Save the new accounting file vnode, and schedule the new
159193323Sed	 * free space watcher.
160193323Sed	 */
161193323Sed	acctp = nd.ni_vp;
162193323Sed	callout_init(&acctwatch_callout, 0);
163193323Sed	acctwatch(NULL);
164193323Sed	return (error);
165193323Sed}
166193323Sed
167193323Sed/*
168193323Sed * Write out process accounting information, on process exit.
169193323Sed * Data to be written out is specified in Leffler, et al.
170193323Sed * and are enumerated below.  (They're also noted in the system
171193323Sed * "acct.h" header file.)
172202375Srdivacky */
173193323Sed
174193323Sedint
175193323Sedacct_process(p)
176193323Sed	struct proc *p;
177193323Sed{
178193323Sed	struct acct acct;
179198090Srdivacky	struct rusage *r;
180193323Sed	struct timeval ut, st, tmp;
181193323Sed	int t;
182193323Sed	struct vnode *vp;
183193323Sed
184193323Sed	/* If accounting isn't enabled, don't bother */
185193323Sed	vp = acctp;
186193323Sed	if (vp == NULLVP)
187193323Sed		return (0);
188193323Sed
189193323Sed	/*
190193323Sed	 * Get process accounting information.
191193323Sed	 */
192193323Sed
193193323Sed	/* (1) The name of the command that ran */
194193323Sed	bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
195193323Sed
196193323Sed	/* (2) The amount of user and system time that was used */
197193323Sed	calcru(p, &ut, &st, NULL);
198193323Sed	acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
199193323Sed	acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
200193323Sed
201193323Sed	/* (3) The elapsed time the commmand ran (and its starting time) */
202193323Sed	acct.ac_btime = p->p_stats->p_start.tv_sec;
203193323Sed	microtime(&tmp);
204198090Srdivacky	timevalsub(&tmp, &p->p_stats->p_start);
205193323Sed	acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
206198090Srdivacky
207202375Srdivacky	/* (4) The average amount of memory used */
208193323Sed	r = &p->p_stats->p_ru;
209202375Srdivacky	tmp = ut;
210198090Srdivacky	timevaladd(&tmp, &st);
211193323Sed	t = tmp.tv_sec * hz + tmp.tv_usec / tick;
212193323Sed	if (t)
213198090Srdivacky		acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
214198090Srdivacky	else
215193323Sed		acct.ac_mem = 0;
216193323Sed
217193323Sed	/* (5) The number of disk I/O operations done */
218193323Sed	acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
219193323Sed
220193323Sed	/* (6) The UID and GID of the process */
221193323Sed	acct.ac_uid = p->p_cred->p_ruid;
222193323Sed	acct.ac_gid = p->p_cred->p_rgid;
223193323Sed
224193323Sed	/* (7) The terminal from which the process was started */
225193323Sed	if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
226193323Sed		acct.ac_tty = dev2udev(p->p_pgrp->pg_session->s_ttyp->t_dev);
227193323Sed	else
228193323Sed		acct.ac_tty = NOUDEV;
229193323Sed
230193323Sed	/* (8) The boolean flags that tell how the process terminated, etc. */
231193323Sed	acct.ac_flag = p->p_acflag;
232193323Sed
233193323Sed	/*
234193323Sed	 * Eliminate any file size rlimit.
235193323Sed	 */
236193323Sed	if (p->p_limit->p_refcnt > 1 &&
237193323Sed	    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
238193323Sed		p->p_limit->p_refcnt--;
239193323Sed		p->p_limit = limcopy(p->p_limit);
240193323Sed	}
241193323Sed	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
242193323Sed
243193323Sed	/*
244193323Sed	 * Write the accounting information to the file.
245193323Sed	 */
246202375Srdivacky	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
247193323Sed	return (vn_rdwr(UIO_WRITE, vp, (caddr_t)&acct, sizeof (acct),
248193323Sed	    (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, p->p_ucred,
249193323Sed	    (int *)0, p));
250193323Sed}
251193323Sed
252193323Sed/*
253193323Sed * Encode_comp_t converts from ticks in seconds and microseconds
254193323Sed * to ticks in 1/AHZ seconds.  The encoding is described in
255193323Sed * Leffler, et al., on page 63.
256193323Sed */
257193323Sed
258193323Sed#define	MANTSIZE	13			/* 13 bit mantissa. */
259193323Sed#define	EXPSIZE		3			/* Base 8 (3 bit) exponent. */
260193323Sed#define	MAXFRACT	((1 << MANTSIZE) - 1)	/* Maximum fractional value. */
261193323Sed
262193323Sedstatic comp_t
263193323Sedencode_comp_t(s, us)
264193323Sed	u_long s, us;
265193323Sed{
266193323Sed	int exp, rnd;
267193323Sed
268193323Sed	exp = 0;
269193323Sed	rnd = 0;
270193323Sed	s *= AHZ;
271193323Sed	s += us / (1000000 / AHZ);	/* Maximize precision. */
272193323Sed
273193323Sed	while (s > MAXFRACT) {
274193323Sed	rnd = s & (1 << (EXPSIZE - 1));	/* Round up? */
275193323Sed		s >>= EXPSIZE;		/* Base 8 exponent == 3 bit shift. */
276193323Sed		exp++;
277193323Sed	}
278193323Sed
279193323Sed	/* If we need to round up, do it (and handle overflow correctly). */
280193323Sed	if (rnd && (++s > MAXFRACT)) {
281193323Sed		s >>= EXPSIZE;
282193323Sed		exp++;
283193323Sed	}
284193323Sed
285198090Srdivacky	/* Clean it up and polish it off. */
286193323Sed	exp <<= MANTSIZE;		/* Shift the exponent into place */
287193323Sed	exp += s;			/* and add on the mantissa. */
288193323Sed	return (exp);
289193323Sed}
290193323Sed
291193323Sed/*
292202375Srdivacky * Periodically check the file system to see if accounting
293193323Sed * should be turned on or off.  Beware the case where the vnode
294193323Sed * has been vgone()'d out from underneath us, e.g. when the file
295193323Sed * system containing the accounting file has been forcibly unmounted.
296193323Sed */
297193323Sed/* ARGSUSED */
298193323Sedstatic void
299193323Sedacctwatch(a)
300193323Sed	void *a;
301193323Sed{
302193323Sed	struct statfs sb;
303193323Sed
304193323Sed	if (savacctp != NULLVP) {
305193323Sed		if (savacctp->v_type == VBAD) {
306193323Sed			(void) vn_close(savacctp, FWRITE, NOCRED, NULL);
307193323Sed			savacctp = NULLVP;
308193323Sed			return;
309193323Sed		}
310193323Sed		(void)VFS_STATFS(savacctp->v_mount, &sb, (struct proc *)0);
311193323Sed		if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
312193323Sed			acctp = savacctp;
313193323Sed			savacctp = NULLVP;
314193323Sed			log(LOG_NOTICE, "Accounting resumed\n");
315193323Sed		}
316193323Sed	} else {
317193323Sed		if (acctp == NULLVP)
318193323Sed			return;
319193323Sed		if (acctp->v_type == VBAD) {
320193323Sed			(void) vn_close(acctp, FWRITE, NOCRED, NULL);
321193323Sed			acctp = NULLVP;
322193323Sed			return;
323193323Sed		}
324193323Sed		(void)VFS_STATFS(acctp->v_mount, &sb, (struct proc *)0);
325193323Sed		if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
326193323Sed			savacctp = acctp;
327193323Sed			acctp = NULLVP;
328193323Sed			log(LOG_NOTICE, "Accounting suspended\n");
329193323Sed		}
330193323Sed	}
331193323Sed	callout_reset(&acctwatch_callout, acctchkfreq * hz, acctwatch, NULL);
332193323Sed}
333193323Sed