1*241d6723Sclaudio /* $OpenBSD: kern_acct.c,v 1.49 2024/07/08 13:17:11 claudio Exp $ */
25ac46f4aSniklas /* $NetBSD: kern_acct.c,v 1.42 1996/02/04 02:15:12 christos Exp $ */
3df930be7Sderaadt
4df930be7Sderaadt /*-
5df930be7Sderaadt * Copyright (c) 1994 Christopher G. Demetriou
6df930be7Sderaadt * Copyright (c) 1982, 1986, 1989, 1993
7df930be7Sderaadt * The Regents of the University of California. All rights reserved.
8df930be7Sderaadt * (c) UNIX System Laboratories, Inc.
9df930be7Sderaadt * All or some portions of this file are derived from material licensed
10df930be7Sderaadt * to the University of California by American Telephone and Telegraph
11df930be7Sderaadt * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12df930be7Sderaadt * the permission of UNIX System Laboratories, Inc.
13df930be7Sderaadt *
14df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
15df930be7Sderaadt * modification, are permitted provided that the following conditions
16df930be7Sderaadt * are met:
17df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright
18df930be7Sderaadt * notice, this list of conditions and the following disclaimer.
19df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
20df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the
21df930be7Sderaadt * documentation and/or other materials provided with the distribution.
2229295d1cSmillert * 3. Neither the name of the University nor the names of its contributors
23df930be7Sderaadt * may be used to endorse or promote products derived from this software
24df930be7Sderaadt * without specific prior written permission.
25df930be7Sderaadt *
26df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df930be7Sderaadt * SUCH DAMAGE.
37df930be7Sderaadt *
38df930be7Sderaadt * @(#)kern_acct.c 8.1 (Berkeley) 6/14/93
39df930be7Sderaadt */
40df930be7Sderaadt
41df930be7Sderaadt #include <sys/param.h>
42df930be7Sderaadt #include <sys/systm.h>
43df930be7Sderaadt #include <sys/proc.h>
44df930be7Sderaadt #include <sys/mount.h>
45df930be7Sderaadt #include <sys/vnode.h>
46c0cd3489Sguenther #include <sys/fcntl.h>
47df930be7Sderaadt #include <sys/syslog.h>
48df930be7Sderaadt #include <sys/kernel.h>
49df930be7Sderaadt #include <sys/namei.h>
50df930be7Sderaadt #include <sys/errno.h>
51df930be7Sderaadt #include <sys/acct.h>
52df930be7Sderaadt #include <sys/resourcevar.h>
53df930be7Sderaadt #include <sys/tty.h>
5450635191Sart #include <sys/kthread.h>
55d627fa5cSanton #include <sys/rwlock.h>
56df930be7Sderaadt
57df930be7Sderaadt #include <sys/syscallargs.h>
58df930be7Sderaadt
59df930be7Sderaadt /*
60df930be7Sderaadt * The routines implemented in this file are described in:
61df930be7Sderaadt * Leffler, et al.: The Design and Implementation of the 4.3BSD
62df930be7Sderaadt * UNIX Operating System (Addison Welley, 1989)
63df930be7Sderaadt * on pages 62-63.
64df930be7Sderaadt *
65df930be7Sderaadt * Arguably, to simplify accounting operations, this mechanism should
66df930be7Sderaadt * be replaced by one in which an accounting log file (similar to /dev/klog)
67df930be7Sderaadt * is read by a user process, etc. However, that has its own problems.
68df930be7Sderaadt */
69df930be7Sderaadt
70df930be7Sderaadt /*
71df930be7Sderaadt * Internal accounting functions.
72df930be7Sderaadt */
7350635191Sart comp_t encode_comp_t(u_long, u_long);
7450635191Sart int acct_start(void);
7550635191Sart void acct_thread(void *);
765066d672Stedu void acct_shutdown(void);
77df930be7Sderaadt
78df930be7Sderaadt /*
79df930be7Sderaadt * Accounting vnode pointer, and saved vnode pointer.
80df930be7Sderaadt */
81df930be7Sderaadt struct vnode *acctp;
82df930be7Sderaadt struct vnode *savacctp;
83df930be7Sderaadt
84df930be7Sderaadt /*
85d627fa5cSanton * Lock protecting acctp and savacctp.
86d627fa5cSanton */
87d627fa5cSanton struct rwlock acct_lock = RWLOCK_INITIALIZER("acctlk");
88d627fa5cSanton
89d627fa5cSanton /*
90df930be7Sderaadt * Values associated with enabling and disabling accounting
91df930be7Sderaadt */
92df930be7Sderaadt int acctsuspend = 2; /* stop accounting when < 2% free space left */
93df930be7Sderaadt int acctresume = 4; /* resume when free space risen to > 4% */
944bc97b15Scheloha int acctrate = 15; /* delay (in seconds) between space checks */
95df930be7Sderaadt
9650635191Sart struct proc *acct_proc;
9750635191Sart
98df930be7Sderaadt /*
99df930be7Sderaadt * Accounting system call. Written based on the specification and
100df930be7Sderaadt * previous implementation done by Mark Tinguely.
101df930be7Sderaadt */
102df930be7Sderaadt int
sys_acct(struct proc * p,void * v,register_t * retval)103158fb4f9Sjsg sys_acct(struct proc *p, void *v, register_t *retval)
104df930be7Sderaadt {
105df930be7Sderaadt struct sys_acct_args /* {
10601b77f95Shenning syscallarg(const char *) path;
107df930be7Sderaadt } */ *uap = v;
108df930be7Sderaadt struct nameidata nd;
109df930be7Sderaadt int error;
110df930be7Sderaadt
111df930be7Sderaadt /* Make sure that the caller is root. */
1123e676399Smpi if ((error = suser(p)) != 0)
113df930be7Sderaadt return (error);
114df930be7Sderaadt
115df930be7Sderaadt /*
116df930be7Sderaadt * If accounting is to be started to a file, open that file for
117d8a79e40Sjmc * writing and make sure it's 'normal'.
118df930be7Sderaadt */
119df930be7Sderaadt if (SCARG(uap, path) != NULL) {
120d71aaa68Srob NDINIT(&nd, 0, 0, UIO_USERSPACE, SCARG(uap, path), p);
121bda218b5Sderaadt if ((error = vn_open(&nd, FWRITE|O_APPEND, 0)) != 0)
122df930be7Sderaadt return (error);
12336bb23f1Svisa VOP_UNLOCK(nd.ni_vp);
124df930be7Sderaadt if (nd.ni_vp->v_type != VREG) {
125df930be7Sderaadt vn_close(nd.ni_vp, FWRITE, p->p_ucred, p);
126df930be7Sderaadt return (EACCES);
127df930be7Sderaadt }
128df930be7Sderaadt }
129df930be7Sderaadt
130d627fa5cSanton rw_enter_write(&acct_lock);
131d627fa5cSanton
132df930be7Sderaadt /*
133df930be7Sderaadt * If accounting was previously enabled, kill the old space-watcher,
134df930be7Sderaadt * close the file, and (if no new file was specified, leave).
135df930be7Sderaadt */
13650635191Sart if (acctp != NULL || savacctp != NULL) {
13750635191Sart wakeup(&acct_proc);
138d627fa5cSanton (void)vn_close((acctp != NULL ? acctp : savacctp), FWRITE,
139df930be7Sderaadt p->p_ucred, p);
14050635191Sart acctp = savacctp = NULL;
141df930be7Sderaadt }
142df930be7Sderaadt if (SCARG(uap, path) == NULL)
143d627fa5cSanton goto out;
144df930be7Sderaadt
145df930be7Sderaadt /*
146df930be7Sderaadt * Save the new accounting file vnode, and schedule the new
147df930be7Sderaadt * free space watcher.
148df930be7Sderaadt */
149df930be7Sderaadt acctp = nd.ni_vp;
15050635191Sart if ((error = acct_start()) != 0) {
15150635191Sart acctp = NULL;
15250635191Sart (void)vn_close(nd.ni_vp, FWRITE, p->p_ucred, p);
153df930be7Sderaadt }
154d627fa5cSanton
155d627fa5cSanton out:
156d627fa5cSanton rw_exit_write(&acct_lock);
157d627fa5cSanton return (error);
15850635191Sart }
159df930be7Sderaadt
160df930be7Sderaadt /*
161df930be7Sderaadt * Write out process accounting information, on process exit.
162df930be7Sderaadt * Data to be written out is specified in Leffler, et al.
163df930be7Sderaadt * and are enumerated below. (They're also noted in the system
164df930be7Sderaadt * "acct.h" header file.)
165df930be7Sderaadt */
166df930be7Sderaadt int
acct_process(struct proc * p)16750635191Sart acct_process(struct proc *p)
168df930be7Sderaadt {
169df930be7Sderaadt struct acct acct;
1708f15e6a4Sguenther struct process *pr = p->p_p;
171df930be7Sderaadt struct rusage *r;
172*241d6723Sclaudio struct tusage tu;
173fe57de6bScheloha struct timespec booted, elapsed, realstart, st, tmp, uptime, ut;
174c2475275Stholo int t;
175df930be7Sderaadt struct vnode *vp;
176d627fa5cSanton int error = 0;
177df930be7Sderaadt
178df930be7Sderaadt /* If accounting isn't enabled, don't bother */
179d627fa5cSanton if (acctp == NULL)
180d627fa5cSanton return (0);
181d627fa5cSanton
182d627fa5cSanton rw_enter_read(&acct_lock);
183d627fa5cSanton
184d627fa5cSanton /*
185d627fa5cSanton * Check the vnode again in case accounting got disabled while waiting
186d627fa5cSanton * for the lock.
187d627fa5cSanton */
188df930be7Sderaadt vp = acctp;
18950635191Sart if (vp == NULL)
190d627fa5cSanton goto out;
191df930be7Sderaadt
192df930be7Sderaadt /*
193df930be7Sderaadt * Get process accounting information.
194df930be7Sderaadt */
195df930be7Sderaadt
196df930be7Sderaadt /* (1) The name of the command that ran */
1978fda72b7Sguenther memcpy(acct.ac_comm, pr->ps_comm, sizeof acct.ac_comm);
198df930be7Sderaadt
199df930be7Sderaadt /* (2) The amount of user and system time that was used */
200*241d6723Sclaudio tuagg_get_process(&tu, pr);
201*241d6723Sclaudio calctsru(&tu, &ut, &st, NULL);
20208be1c18Sguenther acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_nsec);
20308be1c18Sguenther acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_nsec);
204df930be7Sderaadt
205a3c911baSschwarze /* (3) The elapsed time the command ran (and its starting time) */
206fe57de6bScheloha nanouptime(&uptime);
207fe57de6bScheloha nanoboottime(&booted);
208fe57de6bScheloha timespecadd(&booted, &pr->ps_start, &realstart);
209fe57de6bScheloha acct.ac_btime = realstart.tv_sec;
210fe57de6bScheloha timespecsub(&uptime, &pr->ps_start, &elapsed);
211fe57de6bScheloha acct.ac_etime = encode_comp_t(elapsed.tv_sec, elapsed.tv_nsec);
212df930be7Sderaadt
213df930be7Sderaadt /* (4) The average amount of memory used */
2148f15e6a4Sguenther r = &p->p_ru;
21508be1c18Sguenther timespecadd(&ut, &st, &tmp);
21608be1c18Sguenther t = tmp.tv_sec * hz + tmp.tv_nsec / (1000 * tick);
217df930be7Sderaadt if (t)
218df930be7Sderaadt acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
219df930be7Sderaadt else
220df930be7Sderaadt acct.ac_mem = 0;
221df930be7Sderaadt
222df930be7Sderaadt /* (5) The number of disk I/O operations done */
223df930be7Sderaadt acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
224df930be7Sderaadt
225df930be7Sderaadt /* (6) The UID and GID of the process */
226d559b8cbSguenther acct.ac_uid = pr->ps_ucred->cr_ruid;
227d559b8cbSguenther acct.ac_gid = pr->ps_ucred->cr_rgid;
228df930be7Sderaadt
229df930be7Sderaadt /* (7) The terminal from which the process was started */
2308f15e6a4Sguenther if ((pr->ps_flags & PS_CONTROLT) &&
2318f15e6a4Sguenther pr->ps_pgrp->pg_session->s_ttyp)
2328f15e6a4Sguenther acct.ac_tty = pr->ps_pgrp->pg_session->s_ttyp->t_dev;
233df930be7Sderaadt else
2345e1064a7Sderaadt acct.ac_tty = -1;
235df930be7Sderaadt
236*241d6723Sclaudio /* (8) The flags that tell how process terminated or misbehaved. */
2370839b846Smikeb acct.ac_flag = pr->ps_acflag;
238df930be7Sderaadt
239f7bca531Sderaadt /* Extensions */
240f7bca531Sderaadt acct.ac_pid = pr->ps_pid;
241f7bca531Sderaadt
242df930be7Sderaadt /*
243df930be7Sderaadt * Now, just write the accounting information to the file.
244df930be7Sderaadt */
245c264bd14Sart error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&acct, sizeof (acct),
2462ce6adc6Sguenther (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT|IO_NOLIMIT,
2472ce6adc6Sguenther p->p_ucred, NULL, p);
248c264bd14Sart
249d627fa5cSanton out:
250d627fa5cSanton rw_exit_read(&acct_lock);
251d627fa5cSanton return (error);
252df930be7Sderaadt }
253df930be7Sderaadt
254df930be7Sderaadt /*
255df930be7Sderaadt * Encode_comp_t converts from ticks in seconds and microseconds
256df930be7Sderaadt * to ticks in 1/AHZ seconds. The encoding is described in
257df930be7Sderaadt * Leffler, et al., on page 63.
258df930be7Sderaadt */
259df930be7Sderaadt
260df930be7Sderaadt #define MANTSIZE 13 /* 13 bit mantissa. */
261df930be7Sderaadt #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */
262df930be7Sderaadt #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */
263df930be7Sderaadt
264df930be7Sderaadt comp_t
encode_comp_t(u_long s,u_long ns)26508be1c18Sguenther encode_comp_t(u_long s, u_long ns)
266df930be7Sderaadt {
267df930be7Sderaadt int exp, rnd;
268df930be7Sderaadt
269df930be7Sderaadt exp = 0;
270df930be7Sderaadt rnd = 0;
271df930be7Sderaadt s *= AHZ;
27208be1c18Sguenther s += ns / (1000000000 / AHZ); /* Maximize precision. */
273df930be7Sderaadt
274df930be7Sderaadt while (s > MAXFRACT) {
275df930be7Sderaadt rnd = s & (1 << (EXPSIZE - 1)); /* Round up? */
276df930be7Sderaadt s >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */
277df930be7Sderaadt exp++;
278df930be7Sderaadt }
279df930be7Sderaadt
280df930be7Sderaadt /* If we need to round up, do it (and handle overflow correctly). */
281df930be7Sderaadt if (rnd && (++s > MAXFRACT)) {
282df930be7Sderaadt s >>= EXPSIZE;
283df930be7Sderaadt exp++;
284df930be7Sderaadt }
285df930be7Sderaadt
286df930be7Sderaadt /* Clean it up and polish it off. */
287df930be7Sderaadt exp <<= MANTSIZE; /* Shift the exponent into place */
288df930be7Sderaadt exp += s; /* and add on the mantissa. */
289df930be7Sderaadt return (exp);
290df930be7Sderaadt }
291df930be7Sderaadt
29250635191Sart int
acct_start(void)29350635191Sart acct_start(void)
29450635191Sart {
29550635191Sart /* Already running. */
29650635191Sart if (acct_proc != NULL)
29750635191Sart return (0);
29850635191Sart
29950635191Sart return (kthread_create(acct_thread, NULL, &acct_proc, "acct"));
30050635191Sart }
30150635191Sart
302df930be7Sderaadt /*
303df930be7Sderaadt * Periodically check the file system to see if accounting
304df930be7Sderaadt * should be turned on or off. Beware the case where the vnode
305df930be7Sderaadt * has been vgone()'d out from underneath us, e.g. when the file
306df930be7Sderaadt * system containing the accounting file has been forcibly unmounted.
307df930be7Sderaadt */
308df930be7Sderaadt void
acct_thread(void * arg)30950635191Sart acct_thread(void *arg)
310df930be7Sderaadt {
311df930be7Sderaadt struct statfs sb;
31266356e39Smarius struct proc *p = curproc;
313df930be7Sderaadt
314d627fa5cSanton rw_enter_write(&acct_lock);
31550635191Sart for (;;) {
31650635191Sart if (savacctp != NULL) {
317df930be7Sderaadt if (savacctp->v_type == VBAD) {
31866356e39Smarius (void) vn_close(savacctp, FWRITE, NOCRED, p);
31950635191Sart savacctp = NULL;
3201b9ab562Stedu break;
321df930be7Sderaadt }
32214bf419fSkrw (void)VFS_STATFS(savacctp->v_mount, &sb, NULL);
323df930be7Sderaadt if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
324df930be7Sderaadt acctp = savacctp;
32550635191Sart savacctp = NULL;
326df930be7Sderaadt log(LOG_NOTICE, "Accounting resumed\n");
327df930be7Sderaadt }
32850635191Sart } else if (acctp != NULL) {
329df930be7Sderaadt if (acctp->v_type == VBAD) {
33066356e39Smarius (void) vn_close(acctp, FWRITE, NOCRED, p);
33150635191Sart acctp = NULL;
3321b9ab562Stedu break;
333df930be7Sderaadt }
33414bf419fSkrw (void)VFS_STATFS(acctp->v_mount, &sb, NULL);
335df930be7Sderaadt if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
336df930be7Sderaadt savacctp = acctp;
33750635191Sart acctp = NULL;
338df930be7Sderaadt log(LOG_NOTICE, "Accounting suspended\n");
339df930be7Sderaadt }
34050635191Sart } else {
3411b9ab562Stedu break;
34250635191Sart }
343d627fa5cSanton rwsleep_nsec(&acct_proc, &acct_lock, PPAUSE, "acct",
344d627fa5cSanton SEC_TO_NSEC(acctrate));
34550635191Sart }
3461b9ab562Stedu acct_proc = NULL;
347d627fa5cSanton rw_exit_write(&acct_lock);
3481b9ab562Stedu kthread_exit(0);
349df930be7Sderaadt }
3505066d672Stedu
3515066d672Stedu void
acct_shutdown(void)3525066d672Stedu acct_shutdown(void)
3535066d672Stedu {
3545066d672Stedu
35566356e39Smarius struct proc *p = curproc;
35666356e39Smarius
357d627fa5cSanton rw_enter_write(&acct_lock);
3585066d672Stedu if (acctp != NULL || savacctp != NULL) {
3595066d672Stedu vn_close((acctp != NULL ? acctp : savacctp), FWRITE,
36066356e39Smarius NOCRED, p);
3615066d672Stedu acctp = savacctp = NULL;
3625066d672Stedu }
363d627fa5cSanton rw_exit_write(&acct_lock);
3645066d672Stedu }
365