From John Dyson - fix for bug in kern_physio where buffers would be
[unix-history] / sys / kern / kern_acct.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * from: @(#)kern_acct.c 7.18 (Berkeley) 5/11/91
4c45483e 34 * $Id: kern_acct.c,v 1.8 1993/11/12 21:19:58 nate Exp $
15637ed4
RG
35 */
36
37#include "param.h"
38#include "systm.h"
39#include "namei.h"
40#include "resourcevar.h"
41#include "proc.h"
42#include "ioctl.h"
43#include "termios.h"
44#include "tty.h"
45#include "vnode.h"
46#include "mount.h"
47#include "kernel.h"
48#include "file.h"
49#include "acct.h"
50#include "syslog.h"
51
d0409160
NW
52#include "vm/vm.h"
53#include "vm/vm_param.h"
54
15637ed4
RG
55/*
56 * Values associated with enabling and disabling accounting
57 */
58int acctsuspend = 2; /* stop accounting when < 2% free space left */
59int acctresume = 4; /* resume when free space risen to > 4% */
f4e776c5 60struct timeval chk; /* frequency to check space for accounting */
d0409160
NW
61struct vnode *acctp = NULL; /* file to which to do accounting */
62struct vnode *savacctp = NULL; /* file to which to do accounting when space */
15637ed4 63
4c45483e
GW
64static void acctwatch(caddr_t, int);
65
15637ed4
RG
66/*
67 * Enable or disable process accounting.
68 *
69 * If a non-null filename is given, that file is used to store accounting
70 * records on process exit. If a null filename is given process accounting
71 * is suspended. If accounting is enabled, the system checks the amount
72 * of freespace on the filesystem at timeval intervals. If the amount of
73 * freespace is below acctsuspend percent, accounting is suspended. If
74 * accounting has been suspended, and freespace rises above acctresume,
75 * accounting is resumed.
76 */
3c7eb27c 77
d0409160 78/* Mark Tinguely (tinguely@plains.NoDak.edu) 8/10/93 */
3c7eb27c 79
e8f51f85
DG
80struct sysacct_args {
81 char *fname;
82};
83
15637ed4 84/* ARGSUSED */
4c45483e 85int
15637ed4
RG
86sysacct(p, uap, retval)
87 struct proc *p;
e8f51f85 88 struct sysacct_args *uap;
15637ed4
RG
89 int *retval;
90{
91
d0409160
NW
92 register struct nameidata *ndp;
93 struct nameidata nd;
94 struct vattr attr;
4c45483e 95 int rv;
d0409160
NW
96
97 if (p->p_ucred->cr_uid != 0)
98 return(EPERM); /* must be root */
99
15637ed4 100 /*
d0409160 101 * Step 1. turn off accounting (if on). exit if fname is nil
15637ed4 102 */
d0409160
NW
103
104 rv = 0; /* just in case nothing is open */
105 if (acctp != NULL) {
106 rv = vn_close(acctp, FWRITE, p->p_ucred, p);
107 untimeout(acctwatch, (caddr_t) &chk); /* turn off disk check */
108 acctp = NULL;
109 }
110 else if (savacctp != NULL ) {
111 rv = vn_close(savacctp, FWRITE, p->p_ucred, p);
112 untimeout(acctwatch, (caddr_t) &chk); /* turn off disk check */
113 savacctp = NULL;
114 }
115
116 if (uap->fname == NULL) /* accounting stopping complete */
117 return(rv);
118
119 /*
120 * Step 2. open accounting filename for writing.
121 */
122
123 nd.ni_segflg = UIO_USERSPACE;
124 nd.ni_dirp = uap->fname;
125
126 /* is it there? */
127 if (rv = vn_open(&nd, p, FWRITE, 0))
128 return (rv);
129
130 /* Step 2. Check the attributes on accounting file */
131 rv = VOP_GETATTR(nd.ni_vp, &attr, p->p_ucred, p);
132 if (rv)
133 goto acct_fail;
134
135 /* is filesystem writable, do I have permission to write and is
136 * a regular file?
137 */
138 if (nd.ni_vp->v_mount->mnt_flag & MNT_RDONLY) {
139 rv = EROFS; /* to be consistant with man page */
140 goto acct_fail;
141 }
142
143 if ((VOP_ACCESS(nd.ni_vp, VWRITE, p->p_ucred, p)) ||
144 (attr.va_type != VREG)) {
145 rv = EACCES; /* permission denied error */
146 goto acct_fail;
147 }
148
149 /* Step 3. Save the accounting file vnode, schedule freespace watch. */
150
151 acctp = nd.ni_vp;
152 savacctp = NULL;
d0409160 153 VOP_UNLOCK(acctp);
4c45483e 154 acctwatch((caddr_t)&chk, 0); /* look for full system */
d0409160
NW
155 return(0); /* end successfully */
156
157acct_fail:
158
159 vn_close(nd.ni_vp, FWRITE, p->p_ucred, p);
160 return(rv);
15637ed4
RG
161}
162
163/*
164 * Periodically check the file system to see if accounting
165 * should be turned on or off.
166 */
4c45483e
GW
167static void
168acctwatch(arg1, arg2)
169 caddr_t arg1;
170 int arg2;
15637ed4 171{
4c45483e 172 struct timeval *resettime = (struct timeval *)arg1;
15637ed4 173 struct statfs sb;
f4e776c5 174 int s;
15637ed4
RG
175
176 if (savacctp) {
177 (void)VFS_STATFS(savacctp->v_mount, &sb, (struct proc *)0);
178 if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
179 acctp = savacctp;
180 savacctp = NULL;
181 log(LOG_NOTICE, "Accounting resumed\n");
182 return;
183 }
184 }
185 if (acctp == NULL)
186 return;
187 (void)VFS_STATFS(acctp->v_mount, &sb, (struct proc *)0);
188 if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
189 savacctp = acctp;
190 acctp = NULL;
191 log(LOG_NOTICE, "Accounting suspended\n");
192 }
0f34a638 193 s = splhigh(); *resettime = time; splx(s);
f4e776c5 194 resettime->tv_sec += 15;
15637ed4
RG
195 timeout(acctwatch, (caddr_t)resettime, hzto(resettime));
196}
197
198/*
199 * This routine calculates an accounting record for a process and,
200 * if accounting is enabled, writes it to the accounting file.
201 */
d0409160
NW
202
203/* Mark Tinguely (tinguely@plains.NoDak.edu) 8/10/93 */
204
4c45483e 205void
15637ed4
RG
206acct(p)
207 register struct proc *p;
208{
209
d0409160
NW
210 struct acct acct;
211 struct rusage *r;
212 int rv;
213 long i;
214 u_int cnt;
215 char *c;
216 comp_t int2comp();
217
218
219 if (acctp == NULL) /* accounting not turned on */
220 return;
221
222 /* Step 1. Get command name (remove path if necessary) */
223
224 strncpy(acct.ac_comm, p->p_comm, sizeof(acct.ac_comm));
225
226 /* Step 2. Get rest of information */
227
228 acct.ac_utime = int2comp((unsigned) p->p_utime.tv_sec * 1000000 + p->p_utime.tv_usec);
229 acct.ac_stime = int2comp((unsigned) p->p_stime.tv_sec * 1000000 + p->p_stime.tv_usec);
230 acct.ac_btime = p->p_stats->p_start.tv_sec;
231 /* elapse time = current - start */
232 i = (time.tv_sec - p->p_stats->p_start.tv_sec) * 1000000 +
233 (time.tv_usec - p->p_stats->p_start.tv_usec);
234 acct.ac_etime = int2comp((unsigned) i);
235
236 acct.ac_uid = p->p_cred->p_ruid;
237 acct.ac_gid = p->p_cred->p_rgid;
238
239 r = &p->p_stats->p_ru;
240 if (i = (p->p_utime.tv_sec + p->p_stime.tv_sec) * hz +
241 (p->p_utime.tv_usec + p->p_stime.tv_usec) / tick)
242 acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / i;
243 else
244 acct.ac_mem = 0;
245 acct.ac_io = int2comp((unsigned) (r->ru_inblock + r->ru_oublock) * 1000000);
246
247 if ((p->p_flag & SCTTY) && p->p_pgrp->pg_session->s_ttyp)
248 acct.ac_tty = p->p_pgrp->pg_session->s_ttyp->t_dev;
249 else
250 acct.ac_tty = NODEV;
251 acct.ac_flag = p->p_acflag;
252
253 /* Step 3. Write record to file */
254
255
256 rv = vn_rdwr(UIO_WRITE, acctp, (caddr_t) &acct, sizeof (acct),
257 (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, p->p_ucred, (int *) NULL,
258 p);
259}
260
261/* int2comp converts from ticks in a microsecond to ticks in 1/AHZ second
262 *
263 * comp_t is a psuedo-floating point number with 13 bits of
264 * mantissa and 3 bits of base 8 exponent and has resolution
265 * of 1/AHZ seconds.
266 *
267 * notice I already converted the incoming values into microseconds
268 * I need to convert back into AHZ ticks.
269 */
270
271/* Mark Tinguely (tinguely@plains.NoDak.edu) 8/10/93 */
272
273
274#define RES 13
275#define EXP 3
276#define MAXFRACT 1<<RES
277
278comp_t
279int2comp(mantissa)
280unsigned int mantissa;
281{
282 comp_t exp=0;
283
284 mantissa = mantissa * AHZ / 1000000; /* convert back to AHZ ticks */
285 while (mantissa > MAXFRACT) {
286 mantissa >>= EXP; /* base 8 exponent */
287 exp++;
288 }
289 exp <<= RES; /* move the exponent */
290 exp += mantissa; /* add on the manissa */
291 return (exp);
15637ed4 292}