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