kern_acct.c revision 1.66.4.8 1 1.66.4.8 ad /* $NetBSD: kern_acct.c,v 1.66.4.8 2007/01/28 01:34:18 ad Exp $ */
2 1.30 cgd
3 1.23 cgd /*-
4 1.23 cgd * Copyright (c) 1982, 1986, 1989, 1993
5 1.23 cgd * The Regents of the University of California. All rights reserved.
6 1.22 cgd * (c) UNIX System Laboratories, Inc.
7 1.22 cgd * All or some portions of this file are derived from material licensed
8 1.22 cgd * to the University of California by American Telephone and Telegraph
9 1.22 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.22 cgd * the permission of UNIX System Laboratories, Inc.
11 1.22 cgd *
12 1.22 cgd * Redistribution and use in source and binary forms, with or without
13 1.22 cgd * modification, are permitted provided that the following conditions
14 1.22 cgd * are met:
15 1.22 cgd * 1. Redistributions of source code must retain the above copyright
16 1.22 cgd * notice, this list of conditions and the following disclaimer.
17 1.22 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.22 cgd * notice, this list of conditions and the following disclaimer in the
19 1.22 cgd * documentation and/or other materials provided with the distribution.
20 1.55 agc * 3. Neither the name of the University nor the names of its contributors
21 1.55 agc * may be used to endorse or promote products derived from this software
22 1.55 agc * without specific prior written permission.
23 1.55 agc *
24 1.55 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.55 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.55 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.55 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.55 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.55 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.55 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.55 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.55 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.55 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.55 agc * SUCH DAMAGE.
35 1.55 agc *
36 1.55 agc * @(#)kern_acct.c 8.8 (Berkeley) 5/14/95
37 1.55 agc */
38 1.55 agc
39 1.55 agc /*-
40 1.55 agc * Copyright (c) 1994 Christopher G. Demetriou
41 1.55 agc *
42 1.55 agc * Redistribution and use in source and binary forms, with or without
43 1.55 agc * modification, are permitted provided that the following conditions
44 1.55 agc * are met:
45 1.55 agc * 1. Redistributions of source code must retain the above copyright
46 1.55 agc * notice, this list of conditions and the following disclaimer.
47 1.55 agc * 2. Redistributions in binary form must reproduce the above copyright
48 1.55 agc * notice, this list of conditions and the following disclaimer in the
49 1.55 agc * documentation and/or other materials provided with the distribution.
50 1.22 cgd * 3. All advertising materials mentioning features or use of this software
51 1.22 cgd * must display the following acknowledgement:
52 1.22 cgd * This product includes software developed by the University of
53 1.22 cgd * California, Berkeley and its contributors.
54 1.22 cgd * 4. Neither the name of the University nor the names of its contributors
55 1.22 cgd * may be used to endorse or promote products derived from this software
56 1.22 cgd * without specific prior written permission.
57 1.22 cgd *
58 1.22 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 1.22 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 1.22 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 1.22 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 1.22 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 1.22 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 1.22 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 1.22 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 1.22 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 1.22 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 1.22 cgd * SUCH DAMAGE.
69 1.22 cgd *
70 1.44 fvdl * @(#)kern_acct.c 8.8 (Berkeley) 5/14/95
71 1.22 cgd */
72 1.50 lukem
73 1.50 lukem #include <sys/cdefs.h>
74 1.66.4.8 ad __KERNEL_RCSID(0, "$NetBSD: kern_acct.c,v 1.66.4.8 2007/01/28 01:34:18 ad Exp $");
75 1.22 cgd
76 1.22 cgd #include <sys/param.h>
77 1.33 cgd #include <sys/systm.h>
78 1.22 cgd #include <sys/proc.h>
79 1.23 cgd #include <sys/mount.h>
80 1.22 cgd #include <sys/vnode.h>
81 1.22 cgd #include <sys/file.h>
82 1.22 cgd #include <sys/syslog.h>
83 1.23 cgd #include <sys/kernel.h>
84 1.48 enami #include <sys/kthread.h>
85 1.48 enami #include <sys/malloc.h>
86 1.24 cgd #include <sys/namei.h>
87 1.24 cgd #include <sys/errno.h>
88 1.24 cgd #include <sys/acct.h>
89 1.24 cgd #include <sys/resourcevar.h>
90 1.24 cgd #include <sys/ioctl.h>
91 1.24 cgd #include <sys/tty.h>
92 1.63 elad #include <sys/kauth.h>
93 1.22 cgd
94 1.51 thorpej #include <sys/sa.h>
95 1.33 cgd #include <sys/syscallargs.h>
96 1.33 cgd
97 1.24 cgd /*
98 1.24 cgd * The routines implemented in this file are described in:
99 1.24 cgd * Leffler, et al.: The Design and Implementation of the 4.3BSD
100 1.24 cgd * UNIX Operating System (Addison Welley, 1989)
101 1.24 cgd * on pages 62-63.
102 1.24 cgd *
103 1.24 cgd * Arguably, to simplify accounting operations, this mechanism should
104 1.24 cgd * be replaced by one in which an accounting log file (similar to /dev/klog)
105 1.24 cgd * is read by a user process, etc. However, that has its own problems.
106 1.24 cgd */
107 1.24 cgd
108 1.24 cgd /*
109 1.66.4.1 ad * Mutex to serialize system calls and kernel threads.
110 1.48 enami */
111 1.66.4.6 ad kmutex_t acct_mutex;
112 1.48 enami
113 1.48 enami /*
114 1.66.4.1 ad * The global accounting state and related data. Gain the mutex before
115 1.61 thorpej * accessing these variables.
116 1.24 cgd */
117 1.61 thorpej static enum {
118 1.61 thorpej ACCT_STOP,
119 1.61 thorpej ACCT_ACTIVE,
120 1.61 thorpej ACCT_SUSPENDED
121 1.61 thorpej } acct_state; /* The current accounting state. */
122 1.61 thorpej static struct vnode *acct_vp; /* Accounting vnode pointer. */
123 1.63 elad static kauth_cred_t acct_cred; /* Credential of accounting file
124 1.61 thorpej owner (i.e root). Used when
125 1.61 thorpej accounting file i/o. */
126 1.61 thorpej static struct proc *acct_dkwatcher; /* Free disk space checker. */
127 1.24 cgd
128 1.24 cgd /*
129 1.24 cgd * Values associated with enabling and disabling accounting
130 1.24 cgd */
131 1.24 cgd int acctsuspend = 2; /* stop accounting when < 2% free space left */
132 1.24 cgd int acctresume = 4; /* resume when free space risen to > 4% */
133 1.24 cgd int acctchkfreq = 15; /* frequency (in seconds) to check space */
134 1.24 cgd
135 1.61 thorpej /*
136 1.61 thorpej * Encode_comp_t converts from ticks in seconds and microseconds
137 1.61 thorpej * to ticks in 1/AHZ seconds. The encoding is described in
138 1.61 thorpej * Leffler, et al., on page 63.
139 1.61 thorpej */
140 1.48 enami
141 1.61 thorpej #define MANTSIZE 13 /* 13 bit mantissa. */
142 1.61 thorpej #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */
143 1.61 thorpej #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */
144 1.48 enami
145 1.61 thorpej static comp_t
146 1.61 thorpej encode_comp_t(u_long s, u_long us)
147 1.48 enami {
148 1.61 thorpej int exp, rnd;
149 1.61 thorpej
150 1.61 thorpej exp = 0;
151 1.61 thorpej rnd = 0;
152 1.61 thorpej s *= AHZ;
153 1.61 thorpej s += us / (1000000 / AHZ); /* Maximize precision. */
154 1.48 enami
155 1.61 thorpej while (s > MAXFRACT) {
156 1.61 thorpej rnd = s & (1 << (EXPSIZE - 1)); /* Round up? */
157 1.61 thorpej s >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */
158 1.61 thorpej exp++;
159 1.48 enami }
160 1.61 thorpej
161 1.61 thorpej /* If we need to round up, do it (and handle overflow correctly). */
162 1.61 thorpej if (rnd && (++s > MAXFRACT)) {
163 1.61 thorpej s >>= EXPSIZE;
164 1.61 thorpej exp++;
165 1.48 enami }
166 1.61 thorpej
167 1.61 thorpej /* Clean it up and polish it off. */
168 1.61 thorpej exp <<= MANTSIZE; /* Shift the exponent into place */
169 1.61 thorpej exp += s; /* and add on the mantissa. */
170 1.61 thorpej return (exp);
171 1.48 enami }
172 1.48 enami
173 1.61 thorpej static int
174 1.61 thorpej acct_chkfree(void)
175 1.48 enami {
176 1.48 enami int error;
177 1.65 christos struct statvfs *sb;
178 1.57 christos int64_t bavail;
179 1.48 enami
180 1.65 christos sb = malloc(sizeof(*sb), M_TEMP, M_WAITOK);
181 1.65 christos error = VFS_STATVFS(acct_vp->v_mount, sb, NULL);
182 1.48 enami if (error != 0)
183 1.48 enami return (error);
184 1.48 enami
185 1.65 christos bavail = sb->f_bfree - sb->f_bresvd;
186 1.57 christos
187 1.48 enami switch (acct_state) {
188 1.48 enami case ACCT_SUSPENDED:
189 1.65 christos if (bavail > acctresume * sb->f_blocks / 100) {
190 1.48 enami acct_state = ACCT_ACTIVE;
191 1.48 enami log(LOG_NOTICE, "Accounting resumed\n");
192 1.48 enami }
193 1.48 enami break;
194 1.48 enami case ACCT_ACTIVE:
195 1.65 christos if (bavail <= acctsuspend * sb->f_blocks / 100) {
196 1.48 enami acct_state = ACCT_SUSPENDED;
197 1.48 enami log(LOG_NOTICE, "Accounting suspended\n");
198 1.48 enami }
199 1.48 enami break;
200 1.48 enami case ACCT_STOP:
201 1.48 enami break;
202 1.48 enami }
203 1.65 christos free(sb, M_TEMP);
204 1.48 enami return (0);
205 1.48 enami }
206 1.48 enami
207 1.61 thorpej static void
208 1.61 thorpej acct_stop(void)
209 1.61 thorpej {
210 1.61 thorpej int error;
211 1.61 thorpej
212 1.61 thorpej if (acct_vp != NULLVP && acct_vp->v_type != VBAD) {
213 1.63 elad error = vn_close(acct_vp, FWRITE, acct_cred, NULL);
214 1.61 thorpej #ifdef DIAGNOSTIC
215 1.61 thorpej if (error != 0)
216 1.61 thorpej printf("acct_stop: failed to close, errno = %d\n",
217 1.61 thorpej error);
218 1.61 thorpej #endif
219 1.61 thorpej acct_vp = NULLVP;
220 1.61 thorpej }
221 1.63 elad if (acct_cred != NULL) {
222 1.63 elad kauth_cred_free(acct_cred);
223 1.63 elad acct_cred = NULL;
224 1.61 thorpej }
225 1.61 thorpej acct_state = ACCT_STOP;
226 1.61 thorpej }
227 1.61 thorpej
228 1.61 thorpej /*
229 1.61 thorpej * Periodically check the file system to see if accounting
230 1.61 thorpej * should be turned on or off. Beware the case where the vnode
231 1.61 thorpej * has been vgone()'d out from underneath us, e.g. when the file
232 1.61 thorpej * system containing the accounting file has been forcibly unmounted.
233 1.61 thorpej */
234 1.61 thorpej static void
235 1.61 thorpej acctwatch(void *arg)
236 1.61 thorpej {
237 1.61 thorpej int error;
238 1.61 thorpej
239 1.61 thorpej log(LOG_NOTICE, "Accounting started\n");
240 1.66.4.1 ad mutex_enter(&acct_mutex);
241 1.61 thorpej while (acct_state != ACCT_STOP) {
242 1.61 thorpej if (acct_vp->v_type == VBAD) {
243 1.61 thorpej log(LOG_NOTICE, "Accounting terminated\n");
244 1.61 thorpej acct_stop();
245 1.61 thorpej continue;
246 1.61 thorpej }
247 1.61 thorpej
248 1.61 thorpej error = acct_chkfree();
249 1.61 thorpej #ifdef DIAGNOSTIC
250 1.61 thorpej if (error != 0)
251 1.61 thorpej printf("acctwatch: failed to statvfs, error = %d\n",
252 1.61 thorpej error);
253 1.61 thorpej #endif
254 1.66.4.8 ad error = kpause("actwat", FALSE, acctchkfreq * hz, &acct_mutex);
255 1.61 thorpej #ifdef DIAGNOSTIC
256 1.61 thorpej if (error != 0 && error != EWOULDBLOCK)
257 1.61 thorpej printf("acctwatch: sleep error %d\n", error);
258 1.61 thorpej #endif
259 1.61 thorpej }
260 1.61 thorpej acct_dkwatcher = NULL;
261 1.66.4.1 ad mutex_exit(&acct_mutex);
262 1.61 thorpej
263 1.61 thorpej kthread_exit(0);
264 1.61 thorpej }
265 1.61 thorpej
266 1.61 thorpej void
267 1.61 thorpej acct_init(void)
268 1.61 thorpej {
269 1.61 thorpej
270 1.61 thorpej acct_state = ACCT_STOP;
271 1.61 thorpej acct_vp = NULLVP;
272 1.63 elad acct_cred = NULL;
273 1.66.4.1 ad mutex_init(&acct_mutex, MUTEX_DEFAULT, IPL_NONE);
274 1.61 thorpej }
275 1.61 thorpej
276 1.24 cgd /*
277 1.24 cgd * Accounting system call. Written based on the specification and
278 1.24 cgd * previous implementation done by Mark Tinguely.
279 1.24 cgd */
280 1.38 cgd int
281 1.61 thorpej sys_acct(struct lwp *l, void *v, register_t *retval)
282 1.40 thorpej {
283 1.41 mycroft struct sys_acct_args /* {
284 1.43 mycroft syscallarg(const char *) path;
285 1.40 thorpej } */ *uap = v;
286 1.24 cgd struct nameidata nd;
287 1.24 cgd int error;
288 1.24 cgd
289 1.24 cgd /* Make sure that the caller is root. */
290 1.66.4.5 ad if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_ACCOUNTING,
291 1.66.4.5 ad 0, NULL, NULL, NULL)))
292 1.24 cgd return (error);
293 1.24 cgd
294 1.24 cgd /*
295 1.24 cgd * If accounting is to be started to a file, open that file for
296 1.24 cgd * writing and make sure it's a 'normal'.
297 1.24 cgd */
298 1.33 cgd if (SCARG(uap, path) != NULL) {
299 1.59 christos struct vattr va;
300 1.59 christos size_t pad;
301 1.33 cgd NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path),
302 1.62 christos l);
303 1.52 christos if ((error = vn_open(&nd, FWRITE|O_APPEND, 0)) != 0)
304 1.24 cgd return (error);
305 1.24 cgd if (nd.ni_vp->v_type != VREG) {
306 1.60 yamt VOP_UNLOCK(nd.ni_vp, 0);
307 1.59 christos error = EACCES;
308 1.59 christos goto bad;
309 1.59 christos }
310 1.66 ad if ((error = VOP_GETATTR(nd.ni_vp, &va, l->l_cred, l)) != 0) {
311 1.60 yamt VOP_UNLOCK(nd.ni_vp, 0);
312 1.59 christos goto bad;
313 1.60 yamt }
314 1.59 christos
315 1.59 christos if ((pad = (va.va_size % sizeof(struct acct))) != 0) {
316 1.60 yamt u_quad_t size = va.va_size - pad;
317 1.59 christos #ifdef DIAGNOSTIC
318 1.59 christos printf("Size of accounting file not a multiple of "
319 1.59 christos "%lu - incomplete record truncated\n",
320 1.59 christos (unsigned long)sizeof(struct acct));
321 1.59 christos #endif
322 1.60 yamt VATTR_NULL(&va);
323 1.60 yamt va.va_size = size;
324 1.66 ad error = VOP_SETATTR(nd.ni_vp, &va, l->l_cred, l);
325 1.60 yamt if (error != 0) {
326 1.60 yamt VOP_UNLOCK(nd.ni_vp, 0);
327 1.59 christos goto bad;
328 1.60 yamt }
329 1.24 cgd }
330 1.60 yamt VOP_UNLOCK(nd.ni_vp, 0);
331 1.24 cgd }
332 1.24 cgd
333 1.66.4.1 ad mutex_enter(&acct_mutex);
334 1.48 enami
335 1.24 cgd /*
336 1.24 cgd * If accounting was previously enabled, kill the old space-watcher,
337 1.48 enami * free credential for accounting file i/o,
338 1.48 enami * ... (and, if no new file was specified, leave).
339 1.24 cgd */
340 1.48 enami acct_stop();
341 1.33 cgd if (SCARG(uap, path) == NULL)
342 1.48 enami goto out;
343 1.24 cgd
344 1.22 cgd /*
345 1.48 enami * Save the new accounting file vnode and credential,
346 1.48 enami * and schedule the new free space watcher.
347 1.22 cgd */
348 1.48 enami acct_state = ACCT_ACTIVE;
349 1.48 enami acct_vp = nd.ni_vp;
350 1.66 ad acct_cred = l->l_cred;
351 1.63 elad kauth_cred_hold(acct_cred);
352 1.48 enami
353 1.48 enami error = acct_chkfree(); /* Initial guess. */
354 1.48 enami if (error != 0) {
355 1.48 enami acct_stop();
356 1.48 enami goto out;
357 1.48 enami }
358 1.48 enami
359 1.48 enami if (acct_dkwatcher == NULL) {
360 1.48 enami error = kthread_create1(acctwatch, NULL, &acct_dkwatcher,
361 1.48 enami "acctwatch");
362 1.48 enami if (error != 0)
363 1.48 enami acct_stop();
364 1.48 enami }
365 1.48 enami
366 1.48 enami out:
367 1.66.4.1 ad mutex_exit(&acct_mutex);
368 1.24 cgd return (error);
369 1.59 christos bad:
370 1.66 ad vn_close(nd.ni_vp, FWRITE, l->l_cred, l);
371 1.59 christos return error;
372 1.23 cgd }
373 1.22 cgd
374 1.24 cgd /*
375 1.24 cgd * Write out process accounting information, on process exit.
376 1.24 cgd * Data to be written out is specified in Leffler, et al.
377 1.24 cgd * and are enumerated below. (They're also noted in the system
378 1.24 cgd * "acct.h" header file.)
379 1.24 cgd */
380 1.38 cgd int
381 1.62 christos acct_process(struct lwp *l)
382 1.23 cgd {
383 1.24 cgd struct acct acct;
384 1.64 kardel struct timeval ut, st, tmp;
385 1.24 cgd struct rusage *r;
386 1.64 kardel int t, error = 0;
387 1.49 thorpej struct plimit *oplim = NULL;
388 1.62 christos struct proc *p = l->l_proc;
389 1.48 enami
390 1.66.4.1 ad mutex_enter(&acct_mutex);
391 1.24 cgd
392 1.24 cgd /* If accounting isn't enabled, don't bother */
393 1.48 enami if (acct_state != ACCT_ACTIVE)
394 1.48 enami goto out;
395 1.24 cgd
396 1.24 cgd /*
397 1.49 thorpej * Raise the file limit so that accounting can't be stopped by
398 1.49 thorpej * the user.
399 1.49 thorpej *
400 1.49 thorpej * XXX We should think about the CPU limit, too.
401 1.49 thorpej */
402 1.49 thorpej if (p->p_limit->p_refcnt > 1) {
403 1.49 thorpej oplim = p->p_limit;
404 1.49 thorpej p->p_limit = limcopy(p->p_limit);
405 1.49 thorpej }
406 1.49 thorpej p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
407 1.49 thorpej
408 1.49 thorpej /*
409 1.24 cgd * Get process accounting information.
410 1.24 cgd */
411 1.24 cgd
412 1.24 cgd /* (1) The name of the command that ran */
413 1.47 perry memcpy(acct.ac_comm, p->p_comm, sizeof(acct.ac_comm));
414 1.24 cgd
415 1.24 cgd /* (2) The amount of user and system time that was used */
416 1.66.4.3 ad mutex_enter(&p->p_smutex);
417 1.66.4.2 ad calcru(p, &ut, &st, NULL, NULL);
418 1.66.4.3 ad mutex_exit(&p->p_smutex);
419 1.24 cgd acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
420 1.24 cgd acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
421 1.24 cgd
422 1.24 cgd /* (3) The elapsed time the commmand ran (and its starting time) */
423 1.24 cgd acct.ac_btime = p->p_stats->p_start.tv_sec;
424 1.64 kardel getmicrotime(&tmp);
425 1.64 kardel timersub(&tmp, &p->p_stats->p_start, &tmp);
426 1.24 cgd acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
427 1.24 cgd
428 1.24 cgd /* (4) The average amount of memory used */
429 1.24 cgd r = &p->p_stats->p_ru;
430 1.39 mycroft timeradd(&ut, &st, &tmp);
431 1.24 cgd t = tmp.tv_sec * hz + tmp.tv_usec / tick;
432 1.24 cgd if (t)
433 1.24 cgd acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
434 1.24 cgd else
435 1.24 cgd acct.ac_mem = 0;
436 1.24 cgd
437 1.29 cgd /* (5) The number of disk I/O operations done */
438 1.24 cgd acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
439 1.24 cgd
440 1.24 cgd /* (6) The UID and GID of the process */
441 1.66 ad acct.ac_uid = kauth_cred_getuid(l->l_cred);
442 1.66 ad acct.ac_gid = kauth_cred_getgid(l->l_cred);
443 1.24 cgd
444 1.24 cgd /* (7) The terminal from which the process was started */
445 1.66.4.1 ad rw_enter(&proclist_lock, RW_READER);
446 1.66.4.4 ad if ((p->p_lflag & PL_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
447 1.24 cgd acct.ac_tty = p->p_pgrp->pg_session->s_ttyp->t_dev;
448 1.24 cgd else
449 1.24 cgd acct.ac_tty = NODEV;
450 1.66.4.1 ad rw_exit(&proclist_lock);
451 1.24 cgd
452 1.24 cgd /* (8) The boolean flags that tell how the process terminated, etc. */
453 1.24 cgd acct.ac_flag = p->p_acflag;
454 1.22 cgd
455 1.22 cgd /*
456 1.24 cgd * Now, just write the accounting information to the file.
457 1.22 cgd */
458 1.66 ad VOP_LEASE(acct_vp, l, l->l_cred, LEASE_WRITE);
459 1.48 enami error = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct,
460 1.48 enami sizeof(acct), (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT,
461 1.63 elad acct_cred, NULL, NULL);
462 1.48 enami if (error != 0)
463 1.48 enami log(LOG_ERR, "Accounting: write failed %d\n", error);
464 1.49 thorpej
465 1.49 thorpej if (oplim) {
466 1.49 thorpej limfree(p->p_limit);
467 1.49 thorpej p->p_limit = oplim;
468 1.49 thorpej }
469 1.48 enami
470 1.48 enami out:
471 1.66.4.1 ad mutex_exit(&acct_mutex);
472 1.48 enami return (error);
473 1.22 cgd }
474