1*22b6bc1fSmsaitoh /* $NetBSD: kern_acct.c,v 1.99 2021/12/05 04:35:38 msaitoh Exp $ */
2cf92afd6Scgd
347d57570Scgd /*-
447d57570Scgd * Copyright (c) 1982, 1986, 1989, 1993
547d57570Scgd * The Regents of the University of California. All rights reserved.
691cf0fbaScgd * (c) UNIX System Laboratories, Inc.
791cf0fbaScgd * All or some portions of this file are derived from material licensed
891cf0fbaScgd * to the University of California by American Telephone and Telegraph
991cf0fbaScgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
1091cf0fbaScgd * the permission of UNIX System Laboratories, Inc.
1191cf0fbaScgd *
1291cf0fbaScgd * Redistribution and use in source and binary forms, with or without
1391cf0fbaScgd * modification, are permitted provided that the following conditions
1491cf0fbaScgd * are met:
1591cf0fbaScgd * 1. Redistributions of source code must retain the above copyright
1691cf0fbaScgd * notice, this list of conditions and the following disclaimer.
1791cf0fbaScgd * 2. Redistributions in binary form must reproduce the above copyright
1891cf0fbaScgd * notice, this list of conditions and the following disclaimer in the
1991cf0fbaScgd * documentation and/or other materials provided with the distribution.
20aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
21aad01611Sagc * may be used to endorse or promote products derived from this software
22aad01611Sagc * without specific prior written permission.
23aad01611Sagc *
24aad01611Sagc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25aad01611Sagc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26aad01611Sagc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27aad01611Sagc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28aad01611Sagc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29aad01611Sagc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30aad01611Sagc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31aad01611Sagc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32aad01611Sagc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33aad01611Sagc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34aad01611Sagc * SUCH DAMAGE.
35aad01611Sagc *
36aad01611Sagc * @(#)kern_acct.c 8.8 (Berkeley) 5/14/95
37aad01611Sagc */
38aad01611Sagc
39aad01611Sagc /*-
40aad01611Sagc * Copyright (c) 1994 Christopher G. Demetriou
41aad01611Sagc *
42aad01611Sagc * Redistribution and use in source and binary forms, with or without
43aad01611Sagc * modification, are permitted provided that the following conditions
44aad01611Sagc * are met:
45aad01611Sagc * 1. Redistributions of source code must retain the above copyright
46aad01611Sagc * notice, this list of conditions and the following disclaimer.
47aad01611Sagc * 2. Redistributions in binary form must reproduce the above copyright
48aad01611Sagc * notice, this list of conditions and the following disclaimer in the
49aad01611Sagc * documentation and/or other materials provided with the distribution.
5091cf0fbaScgd * 3. All advertising materials mentioning features or use of this software
5191cf0fbaScgd * must display the following acknowledgement:
5291cf0fbaScgd * This product includes software developed by the University of
5391cf0fbaScgd * California, Berkeley and its contributors.
5491cf0fbaScgd * 4. Neither the name of the University nor the names of its contributors
5591cf0fbaScgd * may be used to endorse or promote products derived from this software
5691cf0fbaScgd * without specific prior written permission.
5791cf0fbaScgd *
5891cf0fbaScgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5991cf0fbaScgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6091cf0fbaScgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6191cf0fbaScgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6291cf0fbaScgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6391cf0fbaScgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6491cf0fbaScgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6591cf0fbaScgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6691cf0fbaScgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6791cf0fbaScgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6891cf0fbaScgd * SUCH DAMAGE.
6991cf0fbaScgd *
70e5bc90f4Sfvdl * @(#)kern_acct.c 8.8 (Berkeley) 5/14/95
7191cf0fbaScgd */
7291cf0fbaScgd
73adc783d5Slukem #include <sys/cdefs.h>
74*22b6bc1fSmsaitoh __KERNEL_RCSID(0, "$NetBSD: kern_acct.c,v 1.99 2021/12/05 04:35:38 msaitoh Exp $");
75adc783d5Slukem
7691cf0fbaScgd #include <sys/param.h>
776b861304Scgd #include <sys/systm.h>
7891cf0fbaScgd #include <sys/proc.h>
7991cf0fbaScgd #include <sys/mount.h>
8047d57570Scgd #include <sys/vnode.h>
8191cf0fbaScgd #include <sys/file.h>
8291cf0fbaScgd #include <sys/syslog.h>
8347d57570Scgd #include <sys/kernel.h>
8401a5f6c9Senami #include <sys/kthread.h>
8588ab7da9Sad #include <sys/kmem.h>
86b3fda1e8Scgd #include <sys/namei.h>
87b3fda1e8Scgd #include <sys/errno.h>
88b3fda1e8Scgd #include <sys/acct.h>
89b3fda1e8Scgd #include <sys/resourcevar.h>
90b3fda1e8Scgd #include <sys/ioctl.h>
91b3fda1e8Scgd #include <sys/tty.h>
92215bd95bSelad #include <sys/kauth.h>
9391cf0fbaScgd
946b861304Scgd #include <sys/syscallargs.h>
956b861304Scgd
9691cf0fbaScgd /*
97b3fda1e8Scgd * The routines implemented in this file are described in:
98b3fda1e8Scgd * Leffler, et al.: The Design and Implementation of the 4.3BSD
99b3fda1e8Scgd * UNIX Operating System (Addison Welley, 1989)
100b3fda1e8Scgd * on pages 62-63.
101b3fda1e8Scgd *
102b3fda1e8Scgd * Arguably, to simplify accounting operations, this mechanism should
103b3fda1e8Scgd * be replaced by one in which an accounting log file (similar to /dev/klog)
104b3fda1e8Scgd * is read by a user process, etc. However, that has its own problems.
10591cf0fbaScgd */
10691cf0fbaScgd
10791cf0fbaScgd /*
10821946bbaSad * Lock to serialize system calls and kernel threads.
10901a5f6c9Senami */
11021946bbaSad krwlock_t acct_lock;
11101a5f6c9Senami
11201a5f6c9Senami /*
113b07ec3fcSad * The global accounting state and related data. Gain the mutex before
11473c9fbb0Sthorpej * accessing these variables.
11591cf0fbaScgd */
11673c9fbb0Sthorpej static enum {
11773c9fbb0Sthorpej ACCT_STOP,
11873c9fbb0Sthorpej ACCT_ACTIVE,
11973c9fbb0Sthorpej ACCT_SUSPENDED
12073c9fbb0Sthorpej } acct_state; /* The current accounting state. */
12173c9fbb0Sthorpej static struct vnode *acct_vp; /* Accounting vnode pointer. */
122215bd95bSelad static kauth_cred_t acct_cred; /* Credential of accounting file
12373c9fbb0Sthorpej owner (i.e root). Used when
12473c9fbb0Sthorpej accounting file i/o. */
12588ab7da9Sad static struct lwp *acct_dkwatcher; /* Free disk space checker. */
12647d57570Scgd
12747d57570Scgd /*
12847d57570Scgd * Values associated with enabling and disabling accounting
12947d57570Scgd */
13047d57570Scgd int acctsuspend = 2; /* stop accounting when < 2% free space left */
13147d57570Scgd int acctresume = 4; /* resume when free space risen to > 4% */
13247d57570Scgd int acctchkfreq = 15; /* frequency (in seconds) to check space */
13347d57570Scgd
13473c9fbb0Sthorpej /*
13573c9fbb0Sthorpej * Encode_comp_t converts from ticks in seconds and microseconds
13673c9fbb0Sthorpej * to ticks in 1/AHZ seconds. The encoding is described in
13773c9fbb0Sthorpej * Leffler, et al., on page 63.
13873c9fbb0Sthorpej */
13973c9fbb0Sthorpej
14073c9fbb0Sthorpej #define MANTSIZE 13 /* 13 bit mantissa. */
14173c9fbb0Sthorpej #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */
14273c9fbb0Sthorpej #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */
14373c9fbb0Sthorpej
14473c9fbb0Sthorpej static comp_t
encode_comp_t(u_long s,u_long us)14573c9fbb0Sthorpej encode_comp_t(u_long s, u_long us)
14601a5f6c9Senami {
14773c9fbb0Sthorpej int exp, rnd;
14801a5f6c9Senami
14973c9fbb0Sthorpej exp = 0;
15073c9fbb0Sthorpej rnd = 0;
15173c9fbb0Sthorpej s *= AHZ;
15273c9fbb0Sthorpej s += us / (1000000 / AHZ); /* Maximize precision. */
15373c9fbb0Sthorpej
15473c9fbb0Sthorpej while (s > MAXFRACT) {
15573c9fbb0Sthorpej rnd = s & (1 << (EXPSIZE - 1)); /* Round up? */
15673c9fbb0Sthorpej s >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */
15773c9fbb0Sthorpej exp++;
15801a5f6c9Senami }
15901a5f6c9Senami
16073c9fbb0Sthorpej /* If we need to round up, do it (and handle overflow correctly). */
16173c9fbb0Sthorpej if (rnd && (++s > MAXFRACT)) {
16273c9fbb0Sthorpej s >>= EXPSIZE;
16373c9fbb0Sthorpej exp++;
16401a5f6c9Senami }
16501a5f6c9Senami
16673c9fbb0Sthorpej /* Clean it up and polish it off. */
16773c9fbb0Sthorpej exp <<= MANTSIZE; /* Shift the exponent into place */
16873c9fbb0Sthorpej exp += s; /* and add on the mantissa. */
16973c9fbb0Sthorpej return (exp);
17073c9fbb0Sthorpej }
17173c9fbb0Sthorpej
17273c9fbb0Sthorpej static int
acct_chkfree(void)17373c9fbb0Sthorpej acct_chkfree(void)
17401a5f6c9Senami {
17501a5f6c9Senami int error;
176c0160ad4Schristos struct statvfs *sb;
177ab5c3733Sdholland fsblkcnt_t bavail;
17801a5f6c9Senami
17988ab7da9Sad sb = kmem_alloc(sizeof(*sb), KM_SLEEP);
18061e8303eSpooka error = VFS_STATVFS(acct_vp->v_mount, sb);
18188ab7da9Sad if (error != 0) {
18288ab7da9Sad kmem_free(sb, sizeof(*sb));
18301a5f6c9Senami return (error);
18488ab7da9Sad }
18501a5f6c9Senami
186ab5c3733Sdholland if (sb->f_bfree < sb->f_bresvd) {
187ab5c3733Sdholland bavail = 0;
188ab5c3733Sdholland } else {
189c0160ad4Schristos bavail = sb->f_bfree - sb->f_bresvd;
190ab5c3733Sdholland }
1916bd1d6d4Schristos
19201a5f6c9Senami switch (acct_state) {
19301a5f6c9Senami case ACCT_SUSPENDED:
194c0160ad4Schristos if (bavail > acctresume * sb->f_blocks / 100) {
19501a5f6c9Senami acct_state = ACCT_ACTIVE;
19601a5f6c9Senami log(LOG_NOTICE, "Accounting resumed\n");
19701a5f6c9Senami }
19801a5f6c9Senami break;
19901a5f6c9Senami case ACCT_ACTIVE:
200c0160ad4Schristos if (bavail <= acctsuspend * sb->f_blocks / 100) {
20101a5f6c9Senami acct_state = ACCT_SUSPENDED;
20201a5f6c9Senami log(LOG_NOTICE, "Accounting suspended\n");
20301a5f6c9Senami }
20401a5f6c9Senami break;
20501a5f6c9Senami case ACCT_STOP:
20601a5f6c9Senami break;
20701a5f6c9Senami }
20888ab7da9Sad kmem_free(sb, sizeof(*sb));
20901a5f6c9Senami return (0);
21001a5f6c9Senami }
21101a5f6c9Senami
21273c9fbb0Sthorpej static void
acct_stop(void)21373c9fbb0Sthorpej acct_stop(void)
21473c9fbb0Sthorpej {
21573c9fbb0Sthorpej int error;
21673c9fbb0Sthorpej
21721946bbaSad KASSERT(rw_write_held(&acct_lock));
21821946bbaSad
21973c9fbb0Sthorpej if (acct_vp != NULLVP && acct_vp->v_type != VBAD) {
220a9ca7a37Sad error = vn_close(acct_vp, FWRITE, acct_cred);
22173c9fbb0Sthorpej #ifdef DIAGNOSTIC
22273c9fbb0Sthorpej if (error != 0)
22373c9fbb0Sthorpej printf("acct_stop: failed to close, errno = %d\n",
22473c9fbb0Sthorpej error);
225923e17fdSmrg #else
226923e17fdSmrg __USE(error);
22773c9fbb0Sthorpej #endif
22873c9fbb0Sthorpej acct_vp = NULLVP;
22973c9fbb0Sthorpej }
230215bd95bSelad if (acct_cred != NULL) {
231215bd95bSelad kauth_cred_free(acct_cred);
232215bd95bSelad acct_cred = NULL;
23373c9fbb0Sthorpej }
23473c9fbb0Sthorpej acct_state = ACCT_STOP;
23573c9fbb0Sthorpej }
23673c9fbb0Sthorpej
23773c9fbb0Sthorpej /*
23873c9fbb0Sthorpej * Periodically check the file system to see if accounting
23973c9fbb0Sthorpej * should be turned on or off. Beware the case where the vnode
24073c9fbb0Sthorpej * has been vgone()'d out from underneath us, e.g. when the file
24173c9fbb0Sthorpej * system containing the accounting file has been forcibly unmounted.
24273c9fbb0Sthorpej */
24373c9fbb0Sthorpej static void
acctwatch(void * arg)2441a7bc55dSyamt acctwatch(void *arg)
24573c9fbb0Sthorpej {
24673c9fbb0Sthorpej int error;
24773c9fbb0Sthorpej
24873c9fbb0Sthorpej log(LOG_NOTICE, "Accounting started\n");
24921946bbaSad rw_enter(&acct_lock, RW_WRITER);
25073c9fbb0Sthorpej while (acct_state != ACCT_STOP) {
25173c9fbb0Sthorpej if (acct_vp->v_type == VBAD) {
25273c9fbb0Sthorpej log(LOG_NOTICE, "Accounting terminated\n");
25373c9fbb0Sthorpej acct_stop();
25473c9fbb0Sthorpej continue;
25573c9fbb0Sthorpej }
25673c9fbb0Sthorpej
25773c9fbb0Sthorpej error = acct_chkfree();
25873c9fbb0Sthorpej #ifdef DIAGNOSTIC
25973c9fbb0Sthorpej if (error != 0)
26073c9fbb0Sthorpej printf("acctwatch: failed to statvfs, error = %d\n",
26173c9fbb0Sthorpej error);
262923e17fdSmrg #else
263923e17fdSmrg __USE(error);
26473c9fbb0Sthorpej #endif
26521946bbaSad rw_exit(&acct_lock);
26621946bbaSad error = kpause("actwat", false, acctchkfreq * hz, NULL);
26721946bbaSad rw_enter(&acct_lock, RW_WRITER);
26873c9fbb0Sthorpej #ifdef DIAGNOSTIC
26973c9fbb0Sthorpej if (error != 0 && error != EWOULDBLOCK)
27073c9fbb0Sthorpej printf("acctwatch: sleep error %d\n", error);
27173c9fbb0Sthorpej #endif
27273c9fbb0Sthorpej }
27373c9fbb0Sthorpej acct_dkwatcher = NULL;
27421946bbaSad rw_exit(&acct_lock);
27573c9fbb0Sthorpej
27673c9fbb0Sthorpej kthread_exit(0);
27773c9fbb0Sthorpej }
27873c9fbb0Sthorpej
27973c9fbb0Sthorpej void
acct_init(void)28073c9fbb0Sthorpej acct_init(void)
28173c9fbb0Sthorpej {
28273c9fbb0Sthorpej
28373c9fbb0Sthorpej acct_state = ACCT_STOP;
28473c9fbb0Sthorpej acct_vp = NULLVP;
285215bd95bSelad acct_cred = NULL;
28621946bbaSad rw_init(&acct_lock);
28773c9fbb0Sthorpej }
28873c9fbb0Sthorpej
28947d57570Scgd /*
290b3fda1e8Scgd * Accounting system call. Written based on the specification and
291b3fda1e8Scgd * previous implementation done by Mark Tinguely.
29247d57570Scgd */
293d218233bScgd int
sys_acct(struct lwp * l,const struct sys_acct_args * uap,register_t * retval)2947e2790cfSdsl sys_acct(struct lwp *l, const struct sys_acct_args *uap, register_t *retval)
295b3fda1e8Scgd {
2967e2790cfSdsl /* {
29704daee4fSmycroft syscallarg(const char *) path;
2987e2790cfSdsl } */
2998f6ed30dSdholland struct pathbuf *pb;
300723d09ceSdholland struct vnode *vp;
301b3fda1e8Scgd int error;
30247d57570Scgd
303b3fda1e8Scgd /* Make sure that the caller is root. */
3045f7169ccSelad if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_ACCOUNTING,
3055f7169ccSelad 0, NULL, NULL, NULL)))
306b3fda1e8Scgd return (error);
307b3fda1e8Scgd
308b3fda1e8Scgd /*
309b3fda1e8Scgd * If accounting is to be started to a file, open that file for
310b3fda1e8Scgd * writing and make sure it's a 'normal'.
311b3fda1e8Scgd */
3126b861304Scgd if (SCARG(uap, path) != NULL) {
3136665736bSchristos struct vattr va;
3146665736bSchristos size_t pad;
3158f6ed30dSdholland
3168f6ed30dSdholland error = pathbuf_copyin(SCARG(uap, path), &pb);
3178f6ed30dSdholland if (error) {
3188f6ed30dSdholland return error;
3198f6ed30dSdholland }
320723d09ceSdholland error = vn_open(NULL, pb, TRYEMULROOT, FWRITE|O_APPEND, 0,
321723d09ceSdholland &vp, NULL, NULL);
322723d09ceSdholland if (error != 0) {
3238f6ed30dSdholland pathbuf_destroy(pb);
3248f6ed30dSdholland return error;
3258f6ed30dSdholland }
326723d09ceSdholland if (vp->v_type != VREG) {
327723d09ceSdholland VOP_UNLOCK(vp);
3286665736bSchristos error = EACCES;
3296665736bSchristos goto bad;
3306665736bSchristos }
331723d09ceSdholland if ((error = VOP_GETATTR(vp, &va, l->l_cred)) != 0) {
332723d09ceSdholland VOP_UNLOCK(vp);
3336665736bSchristos goto bad;
33481973871Syamt }
3356665736bSchristos
3366665736bSchristos if ((pad = (va.va_size % sizeof(struct acct))) != 0) {
33781973871Syamt u_quad_t size = va.va_size - pad;
3386665736bSchristos #ifdef DIAGNOSTIC
3396665736bSchristos printf("Size of accounting file not a multiple of "
3406665736bSchristos "%lu - incomplete record truncated\n",
3416665736bSchristos (unsigned long)sizeof(struct acct));
3426665736bSchristos #endif
343c3183f32Spooka vattr_null(&va);
34481973871Syamt va.va_size = size;
345723d09ceSdholland error = VOP_SETATTR(vp, &va, l->l_cred);
34681973871Syamt if (error != 0) {
347723d09ceSdholland VOP_UNLOCK(vp);
3486665736bSchristos goto bad;
349b3fda1e8Scgd }
350b3fda1e8Scgd }
351723d09ceSdholland VOP_UNLOCK(vp);
35281973871Syamt }
353b3fda1e8Scgd
35421946bbaSad rw_enter(&acct_lock, RW_WRITER);
355b3fda1e8Scgd
356b3fda1e8Scgd /*
35701a5f6c9Senami * If accounting was previously enabled, kill the old space-watcher,
35801a5f6c9Senami * free credential for accounting file i/o,
35901a5f6c9Senami * ... (and, if no new file was specified, leave).
360b3fda1e8Scgd */
36101a5f6c9Senami acct_stop();
36201a5f6c9Senami if (SCARG(uap, path) == NULL)
36301a5f6c9Senami goto out;
36401a5f6c9Senami
36501a5f6c9Senami /*
36601a5f6c9Senami * Save the new accounting file vnode and credential,
36701a5f6c9Senami * and schedule the new free space watcher.
36801a5f6c9Senami */
36901a5f6c9Senami acct_state = ACCT_ACTIVE;
370723d09ceSdholland acct_vp = vp;
371f474dcebSad acct_cred = l->l_cred;
372215bd95bSelad kauth_cred_hold(acct_cred);
37301a5f6c9Senami
3748f6ed30dSdholland pathbuf_destroy(pb);
3758f6ed30dSdholland
37601a5f6c9Senami error = acct_chkfree(); /* Initial guess. */
37701a5f6c9Senami if (error != 0) {
37801a5f6c9Senami acct_stop();
37901a5f6c9Senami goto out;
38001a5f6c9Senami }
38101a5f6c9Senami
38201a5f6c9Senami if (acct_dkwatcher == NULL) {
383fef86815Sad error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
384fef86815Sad acctwatch, NULL, &acct_dkwatcher, "acctwatch");
38501a5f6c9Senami if (error != 0)
38601a5f6c9Senami acct_stop();
38701a5f6c9Senami }
38801a5f6c9Senami
38901a5f6c9Senami out:
39021946bbaSad rw_exit(&acct_lock);
391b3fda1e8Scgd return (error);
3926665736bSchristos bad:
393723d09ceSdholland vn_close(vp, FWRITE, l->l_cred);
3948f6ed30dSdholland pathbuf_destroy(pb);
3956665736bSchristos return error;
396b3fda1e8Scgd }
397b3fda1e8Scgd
398b3fda1e8Scgd /*
399b3fda1e8Scgd * Write out process accounting information, on process exit.
400b3fda1e8Scgd * Data to be written out is specified in Leffler, et al.
401b3fda1e8Scgd * and are enumerated below. (They're also noted in the system
402b3fda1e8Scgd * "acct.h" header file.)
403b3fda1e8Scgd */
404d218233bScgd int
acct_process(struct lwp * l)40595e1ffb1Schristos acct_process(struct lwp *l)
406b3fda1e8Scgd {
407b3fda1e8Scgd struct acct acct;
408b3fda1e8Scgd struct timeval ut, st, tmp;
409de4337abSkardel struct rusage *r;
410de4337abSkardel int t, error = 0;
4112e20a70dSdsl struct rlimit orlim;
41295e1ffb1Schristos struct proc *p = l->l_proc;
41301a5f6c9Senami
4142e20a70dSdsl if (acct_state != ACCT_ACTIVE)
4152e20a70dSdsl return 0;
4162e20a70dSdsl
41734987887Schristos memset(&acct, 0, sizeof(acct)); /* to zerofill padded data */
41834987887Schristos
41921946bbaSad rw_enter(&acct_lock, RW_READER);
420b3fda1e8Scgd
421b3fda1e8Scgd /* If accounting isn't enabled, don't bother */
42201a5f6c9Senami if (acct_state != ACCT_ACTIVE)
42301a5f6c9Senami goto out;
424b3fda1e8Scgd
425b3fda1e8Scgd /*
4262e20a70dSdsl * Temporarily raise the file limit so that accounting can't
4272e20a70dSdsl * be stopped by the user.
428f10e7dc6Sthorpej *
429f10e7dc6Sthorpej * XXX We should think about the CPU limit, too.
430f10e7dc6Sthorpej */
431a38faca9Srmind lim_privatise(p);
4322e20a70dSdsl orlim = p->p_rlimit[RLIMIT_FSIZE];
4332e20a70dSdsl /* Set current and max to avoid illegal values */
434f10e7dc6Sthorpej p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
4352e20a70dSdsl p->p_rlimit[RLIMIT_FSIZE].rlim_max = RLIM_INFINITY;
436f10e7dc6Sthorpej
437f10e7dc6Sthorpej /*
438b3fda1e8Scgd * Get process accounting information.
439b3fda1e8Scgd */
440b3fda1e8Scgd
441b3fda1e8Scgd /* (1) The name of the command that ran */
44234987887Schristos strncpy(acct.ac_comm, p->p_comm, sizeof(acct.ac_comm));
443b3fda1e8Scgd
444b3fda1e8Scgd /* (2) The amount of user and system time that was used */
445284c2b9aSad mutex_enter(p->p_lock);
446b07ec3fcSad calcru(p, &ut, &st, NULL, NULL);
447284c2b9aSad mutex_exit(p->p_lock);
448b3fda1e8Scgd acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
449b3fda1e8Scgd acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
450b3fda1e8Scgd
451*22b6bc1fSmsaitoh /* (3) The elapsed time the command ran (and its starting time) */
452b3fda1e8Scgd acct.ac_btime = p->p_stats->p_start.tv_sec;
453de4337abSkardel getmicrotime(&tmp);
454de4337abSkardel timersub(&tmp, &p->p_stats->p_start, &tmp);
455b3fda1e8Scgd acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
456b3fda1e8Scgd
457b3fda1e8Scgd /* (4) The average amount of memory used */
458b3fda1e8Scgd r = &p->p_stats->p_ru;
459f75ba16bSmycroft timeradd(&ut, &st, &tmp);
460b3fda1e8Scgd t = tmp.tv_sec * hz + tmp.tv_usec / tick;
461b3fda1e8Scgd if (t)
462b3fda1e8Scgd acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
463b3fda1e8Scgd else
464b3fda1e8Scgd acct.ac_mem = 0;
465b3fda1e8Scgd
466975727e1Scgd /* (5) The number of disk I/O operations done */
467b3fda1e8Scgd acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
468b3fda1e8Scgd
469b3fda1e8Scgd /* (6) The UID and GID of the process */
470f474dcebSad acct.ac_uid = kauth_cred_getuid(l->l_cred);
471f474dcebSad acct.ac_gid = kauth_cred_getgid(l->l_cred);
472b3fda1e8Scgd
473b3fda1e8Scgd /* (7) The terminal from which the process was started */
4740eaaa024Sad mutex_enter(&proc_lock);
475b07ec3fcSad if ((p->p_lflag & PL_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
476b3fda1e8Scgd acct.ac_tty = p->p_pgrp->pg_session->s_ttyp->t_dev;
477b3fda1e8Scgd else
478b3fda1e8Scgd acct.ac_tty = NODEV;
4790eaaa024Sad mutex_exit(&proc_lock);
480b3fda1e8Scgd
481b3fda1e8Scgd /* (8) The boolean flags that tell how the process terminated, etc. */
482b3fda1e8Scgd acct.ac_flag = p->p_acflag;
483b3fda1e8Scgd
484b3fda1e8Scgd /*
485b3fda1e8Scgd * Now, just write the accounting information to the file.
486b3fda1e8Scgd */
48753524e44Schristos error = vn_rdwr(UIO_WRITE, acct_vp, (void *)&acct,
48801a5f6c9Senami sizeof(acct), (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT,
489215bd95bSelad acct_cred, NULL, NULL);
49001a5f6c9Senami if (error != 0)
49101a5f6c9Senami log(LOG_ERR, "Accounting: write failed %d\n", error);
49201a5f6c9Senami
4932e20a70dSdsl /* Restore limit - rather pointless since process is about to exit */
4942e20a70dSdsl p->p_rlimit[RLIMIT_FSIZE] = orlim;
495f10e7dc6Sthorpej
49601a5f6c9Senami out:
49721946bbaSad rw_exit(&acct_lock);
49801a5f6c9Senami return (error);
499b3fda1e8Scgd }
500