]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_acct.c
52340af598cc9e6384e8ac6b1677421eeaa56417
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * @(#)kern_acct.c 8.1 (Berkeley) 6/14/93
71 * 08-May-95 Mac Gillon (mgillon) at NeXT
73 * New version based on 4.4
77 #include <sys/param.h>
78 #include <sys/proc_internal.h>
79 #include <sys/kauth.h>
80 #include <sys/mount_internal.h>
81 #include <sys/vnode_internal.h>
82 #include <sys/file_internal.h>
83 #include <sys/syslog.h>
84 #include <sys/kernel.h>
85 #include <sys/namei.h>
86 #include <sys/errno.h>
88 #include <sys/resourcevar.h>
89 #include <sys/ioctl.h>
91 #include <sys/sysproto.h>
92 #include <machine/spl.h>
95 * The routines implemented in this file are described in:
96 * Leffler, et al.: The Design and Implementation of the 4.3BSD
97 * UNIX Operating System (Addison Welley, 1989)
100 * Arguably, to simplify accounting operations, this mechanism should
101 * be replaced by one in which an accounting log file (similar to /dev/klog)
102 * is read by a user process, etc. However, that has its own problems.
106 * Internal accounting functions.
107 * The former's operation is described in Leffler, et al., and the latter
108 * was provided by UCB with the 4.4BSD-Lite release
110 comp_t
encode_comp_t(u_long
, u_long
);
111 void acctwatch(void *);
112 void acctwatch_funnel(void *);
115 * Accounting vnode pointer, and suspended accounting vnode pointer. States
118 * acctp suspend_acctp state
119 * ------------- ------------ ------------------------------
120 * NULL NULL Accounting disabled
121 * !NULL NULL Accounting enabled
122 * NULL !NULL Accounting enabled, but suspended
123 * !NULL !NULL <not allowed>
126 struct vnode
*suspend_acctp
;
129 * Values associated with enabling and disabling accounting
131 int acctsuspend
= 2; /* stop accounting when < 2% free space left */
132 int acctresume
= 4; /* resume when free space risen to > 4% */
133 int acctchkfreq
= 15; /* frequency (in seconds) to check space */
136 * Accounting system call. Written based on the specification and
137 * previous implementation done by Mark Tinguely.
140 acct(struct proc
*p
, struct acct_args
*uap
, __unused
int *retval
)
144 struct vfs_context context
;
147 context
.vc_ucred
= kauth_cred_get();
149 /* Make sure that the caller is root. */
150 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
)))
154 * If accounting is to be started to a file, open that file for
155 * writing and make sure it's a 'normal'.
157 if (uap
->path
!= USER_ADDR_NULL
) {
158 NDINIT(&nd
, LOOKUP
, NOFOLLOW
, UIO_USERSPACE
, uap
->path
, &context
);
159 if ((error
= vn_open(&nd
, FWRITE
, 0)))
163 if (nd
.ni_vp
->v_type
!= VREG
) {
164 vn_close(nd
.ni_vp
, FWRITE
, kauth_cred_get(), p
);
170 * If accounting was previously enabled, kill the old space-watcher,
171 * close the file, and (if no new file was specified, leave).
173 if (acctp
!= NULLVP
|| suspend_acctp
!= NULLVP
) {
174 untimeout(acctwatch_funnel
, NULL
);
175 error
= vn_close((acctp
!= NULLVP
? acctp
: suspend_acctp
), FWRITE
,
176 kauth_cred_get(), p
);
178 acctp
= suspend_acctp
= NULLVP
;
180 if (uap
->path
== USER_ADDR_NULL
)
184 * Save the new accounting file vnode, and schedule the new
185 * free space watcher.
193 * Write out process accounting information, on process exit.
194 * Data to be written out is specified in Leffler, et al.
195 * and are enumerated below. (They're also noted in the system
196 * "acct.h" header file.)
204 struct timeval ut
, st
, tmp
;
209 /* If accounting isn't enabled, don't bother */
215 * Get process accounting information.
218 /* (1) The name of the command that ran */
219 bcopy(p
->p_comm
, an_acct
.ac_comm
, sizeof an_acct
.ac_comm
);
221 /* (2) The amount of user and system time that was used */
222 calcru(p
, &ut
, &st
, NULL
);
223 an_acct
.ac_utime
= encode_comp_t(ut
.tv_sec
, ut
.tv_usec
);
224 an_acct
.ac_stime
= encode_comp_t(st
.tv_sec
, st
.tv_usec
);
226 /* (3) The elapsed time the commmand ran (and its starting time) */
227 an_acct
.ac_btime
= p
->p_stats
->p_start
.tv_sec
;
229 timevalsub(&tmp
, &p
->p_stats
->p_start
);
230 an_acct
.ac_etime
= encode_comp_t(tmp
.tv_sec
, tmp
.tv_usec
);
232 /* (4) The average amount of memory used */
233 r
= &p
->p_stats
->p_ru
;
235 timevaladd(&tmp
, &st
);
236 t
= tmp
.tv_sec
* hz
+ tmp
.tv_usec
/ tick
;
238 an_acct
.ac_mem
= (r
->ru_ixrss
+ r
->ru_idrss
+ r
->ru_isrss
) / t
;
242 /* (5) The number of disk I/O operations done */
243 an_acct
.ac_io
= encode_comp_t(r
->ru_inblock
+ r
->ru_oublock
, 0);
245 /* (6) The UID and GID of the process */
246 an_acct
.ac_uid
= p
->p_ucred
->cr_ruid
;
247 an_acct
.ac_gid
= p
->p_ucred
->cr_rgid
;
249 /* (7) The terminal from which the process was started */
250 if ((p
->p_flag
& P_CONTROLT
) && p
->p_pgrp
->pg_session
->s_ttyp
)
251 an_acct
.ac_tty
= p
->p_pgrp
->pg_session
->s_ttyp
->t_dev
;
253 an_acct
.ac_tty
= NODEV
;
255 /* (8) The boolean flags that tell how the process terminated, etc. */
256 an_acct
.ac_flag
= p
->p_acflag
;
259 * Now, just write the accounting information to the file.
261 if ((error
= vnode_getwithref(vp
)) == 0) {
262 error
= vn_rdwr(UIO_WRITE
, vp
, (caddr_t
)&an_acct
, sizeof (an_acct
),
263 (off_t
)0, UIO_SYSSPACE32
, IO_APPEND
|IO_UNIT
, p
->p_ucred
,
271 * Encode_comp_t converts from ticks in seconds and microseconds
272 * to ticks in 1/AHZ seconds. The encoding is described in
273 * Leffler, et al., on page 63.
276 #define MANTSIZE 13 /* 13 bit mantissa. */
277 #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */
278 #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */
289 s
+= us
/ (1000000 / AHZ
); /* Maximize precision. */
291 while (s
> MAXFRACT
) {
292 rnd
= s
& (1 << (EXPSIZE
- 1)); /* Round up? */
293 s
>>= EXPSIZE
; /* Base 8 exponent == 3 bit shift. */
297 /* If we need to round up, do it (and handle overflow correctly). */
298 if (rnd
&& (++s
> MAXFRACT
)) {
303 /* Clean it up and polish it off. */
304 exp
<<= MANTSIZE
; /* Shift the exponent into place */
305 exp
+= s
; /* and add on the mantissa. */
313 thread_funnel_set(kernel_flock
, TRUE
);
315 thread_funnel_set(kernel_flock
, FALSE
);
320 * Periodically check the file system to see if accounting
321 * should be turned on or off. Beware the case where the vnode
322 * has been vgone()'d out from underneath us, e.g. when the file
323 * system containing the accounting file has been forcibly unmounted.
327 acctwatch(__unused
void *a
)
329 struct vfs_context context
;
333 VFSATTR_WANTED(&va
, f_blocks
);
334 VFSATTR_WANTED(&va
, f_bavail
);
335 context
.vc_proc
= current_proc();
336 context
.vc_ucred
= kauth_cred_get();
338 if (suspend_acctp
!= NULLVP
) {
340 * Resuming accounting when accounting is suspended, and the
341 * filesystem containing the suspended accounting file goes
342 * below a low watermark
344 if (suspend_acctp
->v_type
== VBAD
) {
345 (void) vn_close(suspend_acctp
, FWRITE
, NOCRED
, NULL
);
346 suspend_acctp
= NULLVP
;
349 (void)vfs_getattr(suspend_acctp
->v_mount
, &va
, &context
);
350 if (va
.f_bavail
> acctresume
* va
.f_blocks
/ 100) {
351 acctp
= suspend_acctp
;
352 suspend_acctp
= NULLVP
;
353 log(LOG_NOTICE
, "Accounting resumed\n");
355 } else if (acctp
!= NULLVP
) {
357 * Suspending accounting when accounting is currently active,
358 * and the filesystem containing the active accounting file
359 * goes over a high watermark
361 if (acctp
->v_type
== VBAD
) {
362 (void) vn_close(acctp
, FWRITE
, NOCRED
, NULL
);
366 (void)vfs_getattr(acctp
->v_mount
, &va
, &context
);
367 if (va
.f_bavail
<= acctsuspend
* va
.f_blocks
/ 100) {
368 suspend_acctp
= acctp
;
370 log(LOG_NOTICE
, "Accounting suspended\n");
376 timeout(acctwatch_funnel
, NULL
, acctchkfreq
* hz
);