1 /* $NetBSD: kern_acct.c,v 1.87 2009/02/11 00:32:45 enami Exp $ */
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)kern_acct.c 8.8 (Berkeley) 5/14/95
40 * Copyright (c) 1994 Christopher G. Demetriou
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * @(#)kern_acct.c 8.8 (Berkeley) 5/14/95
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: kern_acct.c,v 1.87 2009/02/11 00:32:45 enami Exp $");
76 #include <sys/param.h>
77 #include <sys/systm.h>
79 #include <sys/mount.h>
80 #include <sys/vnode.h>
82 #include <sys/syslog.h>
83 #include <sys/kernel.h>
84 #include <sys/kthread.h>
86 #include <sys/namei.h>
87 #include <sys/errno.h>
89 #include <sys/resourcevar.h>
90 #include <sys/ioctl.h>
92 #include <sys/kauth.h>
94 #include <sys/syscallargs.h>
97 * The routines implemented in this file are described in:
98 * Leffler, et al.: The Design and Implementation of the 4.3BSD
99 * UNIX Operating System (Addison Welley, 1989)
102 * Arguably, to simplify accounting operations, this mechanism should
103 * be replaced by one in which an accounting log file (similar to /dev/klog)
104 * is read by a user process, etc. However, that has its own problems.
108 * Lock to serialize system calls and kernel threads.
113 * The global accounting state and related data. Gain the mutex before
114 * accessing these variables.
120 } acct_state
; /* The current accounting state. */
121 static struct vnode
*acct_vp
; /* Accounting vnode pointer. */
122 static kauth_cred_t acct_cred
; /* Credential of accounting file
123 owner (i.e root). Used when
124 accounting file i/o. */
125 static struct lwp
*acct_dkwatcher
; /* Free disk space checker. */
128 * Values associated with enabling and disabling accounting
130 int acctsuspend
= 2; /* stop accounting when < 2% free space left */
131 int acctresume
= 4; /* resume when free space risen to > 4% */
132 int acctchkfreq
= 15; /* frequency (in seconds) to check space */
135 * Encode_comp_t converts from ticks in seconds and microseconds
136 * to ticks in 1/AHZ seconds. The encoding is described in
137 * Leffler, et al., on page 63.
140 #define MANTSIZE 13 /* 13 bit mantissa. */
141 #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */
142 #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */
145 encode_comp_t(u_long s
, u_long us
)
152 s
+= us
/ (1000000 / AHZ
); /* Maximize precision. */
154 while (s
> MAXFRACT
) {
155 rnd
= s
& (1 << (EXPSIZE
- 1)); /* Round up? */
156 s
>>= EXPSIZE
; /* Base 8 exponent == 3 bit shift. */
160 /* If we need to round up, do it (and handle overflow correctly). */
161 if (rnd
&& (++s
> MAXFRACT
)) {
166 /* Clean it up and polish it off. */
167 exp
<<= MANTSIZE
; /* Shift the exponent into place */
168 exp
+= s
; /* and add on the mantissa. */
179 sb
= kmem_alloc(sizeof(*sb
), KM_SLEEP
);
182 error
= VFS_STATVFS(acct_vp
->v_mount
, sb
);
184 kmem_free(sb
, sizeof(*sb
));
188 bavail
= sb
->f_bfree
- sb
->f_bresvd
;
190 switch (acct_state
) {
192 if (bavail
> acctresume
* sb
->f_blocks
/ 100) {
193 acct_state
= ACCT_ACTIVE
;
194 log(LOG_NOTICE
, "Accounting resumed\n");
198 if (bavail
<= acctsuspend
* sb
->f_blocks
/ 100) {
199 acct_state
= ACCT_SUSPENDED
;
200 log(LOG_NOTICE
, "Accounting suspended\n");
206 kmem_free(sb
, sizeof(*sb
));
215 KASSERT(rw_write_held(&acct_lock
));
217 if (acct_vp
!= NULLVP
&& acct_vp
->v_type
!= VBAD
) {
218 error
= vn_close(acct_vp
, FWRITE
, acct_cred
);
221 printf("acct_stop: failed to close, errno = %d\n",
226 if (acct_cred
!= NULL
) {
227 kauth_cred_free(acct_cred
);
230 acct_state
= ACCT_STOP
;
234 * Periodically check the file system to see if accounting
235 * should be turned on or off. Beware the case where the vnode
236 * has been vgone()'d out from underneath us, e.g. when the file
237 * system containing the accounting file has been forcibly unmounted.
244 log(LOG_NOTICE
, "Accounting started\n");
245 rw_enter(&acct_lock
, RW_WRITER
);
246 while (acct_state
!= ACCT_STOP
) {
247 if (acct_vp
->v_type
== VBAD
) {
248 log(LOG_NOTICE
, "Accounting terminated\n");
253 error
= acct_chkfree();
256 printf("acctwatch: failed to statvfs, error = %d\n",
260 error
= kpause("actwat", false, acctchkfreq
* hz
, NULL
);
261 rw_enter(&acct_lock
, RW_WRITER
);
263 if (error
!= 0 && error
!= EWOULDBLOCK
)
264 printf("acctwatch: sleep error %d\n", error
);
267 acct_dkwatcher
= NULL
;
277 acct_state
= ACCT_STOP
;
284 * Accounting system call. Written based on the specification and
285 * previous implementation done by Mark Tinguely.
288 sys_acct(struct lwp
*l
, const struct sys_acct_args
*uap
, register_t
*retval
)
291 syscallarg(const char *) path;
296 /* Make sure that the caller is root. */
297 if ((error
= kauth_authorize_system(l
->l_cred
, KAUTH_SYSTEM_ACCOUNTING
,
298 0, NULL
, NULL
, NULL
)))
302 * If accounting is to be started to a file, open that file for
303 * writing and make sure it's a 'normal'.
305 if (SCARG(uap
, path
) != NULL
) {
308 NDINIT(&nd
, LOOKUP
, FOLLOW
| TRYEMULROOT
, UIO_USERSPACE
,
310 if ((error
= vn_open(&nd
, FWRITE
|O_APPEND
, 0)) != 0)
312 if (nd
.ni_vp
->v_type
!= VREG
) {
313 VOP_UNLOCK(nd
.ni_vp
, 0);
317 if ((error
= VOP_GETATTR(nd
.ni_vp
, &va
, l
->l_cred
)) != 0) {
318 VOP_UNLOCK(nd
.ni_vp
, 0);
322 if ((pad
= (va
.va_size
% sizeof(struct acct
))) != 0) {
323 u_quad_t size
= va
.va_size
- pad
;
325 printf("Size of accounting file not a multiple of "
326 "%lu - incomplete record truncated\n",
327 (unsigned long)sizeof(struct acct
));
331 error
= VOP_SETATTR(nd
.ni_vp
, &va
, l
->l_cred
);
333 VOP_UNLOCK(nd
.ni_vp
, 0);
337 VOP_UNLOCK(nd
.ni_vp
, 0);
340 rw_enter(&acct_lock
, RW_WRITER
);
343 * If accounting was previously enabled, kill the old space-watcher,
344 * free credential for accounting file i/o,
345 * ... (and, if no new file was specified, leave).
348 if (SCARG(uap
, path
) == NULL
)
352 * Save the new accounting file vnode and credential,
353 * and schedule the new free space watcher.
355 acct_state
= ACCT_ACTIVE
;
357 acct_cred
= l
->l_cred
;
358 kauth_cred_hold(acct_cred
);
360 error
= acct_chkfree(); /* Initial guess. */
366 if (acct_dkwatcher
== NULL
) {
367 error
= kthread_create(PRI_NONE
, KTHREAD_MPSAFE
, NULL
,
368 acctwatch
, NULL
, &acct_dkwatcher
, "acctwatch");
377 vn_close(nd
.ni_vp
, FWRITE
, l
->l_cred
);
382 * Write out process accounting information, on process exit.
383 * Data to be written out is specified in Leffler, et al.
384 * and are enumerated below. (They're also noted in the system
385 * "acct.h" header file.)
388 acct_process(struct lwp
*l
)
391 struct timeval ut
, st
, tmp
;
395 struct proc
*p
= l
->l_proc
;
397 if (acct_state
!= ACCT_ACTIVE
)
400 rw_enter(&acct_lock
, RW_READER
);
402 /* If accounting isn't enabled, don't bother */
403 if (acct_state
!= ACCT_ACTIVE
)
407 * Temporarily raise the file limit so that accounting can't
408 * be stopped by the user.
410 * XXX We should think about the CPU limit, too.
412 lim_privatise(p
, false);
413 orlim
= p
->p_rlimit
[RLIMIT_FSIZE
];
414 /* Set current and max to avoid illegal values */
415 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
= RLIM_INFINITY
;
416 p
->p_rlimit
[RLIMIT_FSIZE
].rlim_max
= RLIM_INFINITY
;
419 * Get process accounting information.
422 /* (1) The name of the command that ran */
423 memcpy(acct
.ac_comm
, p
->p_comm
, sizeof(acct
.ac_comm
));
425 /* (2) The amount of user and system time that was used */
426 mutex_enter(p
->p_lock
);
427 calcru(p
, &ut
, &st
, NULL
, NULL
);
428 mutex_exit(p
->p_lock
);
429 acct
.ac_utime
= encode_comp_t(ut
.tv_sec
, ut
.tv_usec
);
430 acct
.ac_stime
= encode_comp_t(st
.tv_sec
, st
.tv_usec
);
432 /* (3) The elapsed time the commmand ran (and its starting time) */
433 acct
.ac_btime
= p
->p_stats
->p_start
.tv_sec
;
435 timersub(&tmp
, &p
->p_stats
->p_start
, &tmp
);
436 acct
.ac_etime
= encode_comp_t(tmp
.tv_sec
, tmp
.tv_usec
);
438 /* (4) The average amount of memory used */
439 r
= &p
->p_stats
->p_ru
;
440 timeradd(&ut
, &st
, &tmp
);
441 t
= tmp
.tv_sec
* hz
+ tmp
.tv_usec
/ tick
;
443 acct
.ac_mem
= (r
->ru_ixrss
+ r
->ru_idrss
+ r
->ru_isrss
) / t
;
447 /* (5) The number of disk I/O operations done */
448 acct
.ac_io
= encode_comp_t(r
->ru_inblock
+ r
->ru_oublock
, 0);
450 /* (6) The UID and GID of the process */
451 acct
.ac_uid
= kauth_cred_getuid(l
->l_cred
);
452 acct
.ac_gid
= kauth_cred_getgid(l
->l_cred
);
454 /* (7) The terminal from which the process was started */
455 mutex_enter(proc_lock
);
456 if ((p
->p_lflag
& PL_CONTROLT
) && p
->p_pgrp
->pg_session
->s_ttyp
)
457 acct
.ac_tty
= p
->p_pgrp
->pg_session
->s_ttyp
->t_dev
;
460 mutex_exit(proc_lock
);
462 /* (8) The boolean flags that tell how the process terminated, etc. */
463 acct
.ac_flag
= p
->p_acflag
;
466 * Now, just write the accounting information to the file.
468 error
= vn_rdwr(UIO_WRITE
, acct_vp
, (void *)&acct
,
469 sizeof(acct
), (off_t
)0, UIO_SYSSPACE
, IO_APPEND
|IO_UNIT
,
470 acct_cred
, NULL
, NULL
);
472 log(LOG_ERR
, "Accounting: write failed %d\n", error
);
474 /* Restore limit - rather pointless since process is about to exit */
475 p
->p_rlimit
[RLIMIT_FSIZE
] = orlim
;