xref: /original-bsd/sys/kern/kern_acct.c (revision 333da485)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * %sccs.include.proprietary.c%
11  *
12  *	@(#)kern_acct.c	8.3 (Berkeley) 01/21/94
13  */
14 
15 #include <sys/param.h>
16 #include <sys/systm.h>
17 #include <sys/namei.h>
18 #include <sys/resourcevar.h>
19 #include <sys/proc.h>
20 #include <sys/ioctl.h>
21 #include <sys/termios.h>
22 #include <sys/tty.h>
23 #include <sys/vnode.h>
24 #include <sys/mount.h>
25 #include <sys/kernel.h>
26 #include <sys/file.h>
27 #include <sys/acct.h>
28 #include <sys/syslog.h>
29 
30 /*
31  * Values associated with enabling and disabling accounting
32  */
33 int	acctsuspend = 2;	/* stop accounting when < 2% free space left */
34 int	acctresume = 4;		/* resume when free space risen to > 4% */
35 int	acctchkfreq = 15;	/* frequency (in seconds) to check space */
36 
37 /*
38  * SHOULD REPLACE THIS WITH A DRIVER THAT CAN BE READ TO SIMPLIFY.
39  */
40 struct	vnode *acctp;
41 struct	vnode *savacctp;
42 
43 /*
44  * Enable or disable process accounting.
45  *
46  * If a non-null filename is given, that file is used to store accounting
47  * records on process exit. If a null filename is given process accounting
48  * is suspended. If accounting is enabled, the system checks the amount
49  * of freespace on the filesystem at timeval intervals. If the amount of
50  * freespace is below acctsuspend percent, accounting is suspended. If
51  * accounting has been suspended, and freespace rises above acctresume,
52  * accounting is resumed.
53  */
54 struct acct_args {
55 	char	*fname;
56 };
57 acct(p, uap, retval)
58 	struct proc *p;
59 	struct acct_args *uap;
60 	int *retval;
61 {
62 	register struct vnode *vp;
63 	extern void acctwatch __P((void *));
64 	struct vnode *oacctp;
65 	int error;
66 	struct nameidata nd;
67 
68 	if (error = suser(p->p_ucred, &p->p_acflag))
69 		return (error);
70 	if (savacctp) {
71 		acctp = savacctp;
72 		savacctp = NULL;
73 	}
74 	if (uap->fname == NULL) {
75 		if (vp = acctp) {
76 			acctp = NULL;
77 			error = vn_close(vp, FWRITE, p->p_ucred, p);
78 			untimeout(acctwatch, NULL);
79 		}
80 		return (error);
81 	}
82 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->fname, p);
83 	if (error = vn_open(&nd, FWRITE, 0644))
84 		return (error);
85 	vp = nd.ni_vp;
86 	VOP_UNLOCK(vp);
87 	if (vp->v_type != VREG) {
88 		(void) vn_close(vp, FWRITE, p->p_ucred, p);
89 		return (EACCES);
90 	}
91 	oacctp = acctp;
92 	acctp = vp;
93 	if (oacctp)
94 		error = vn_close(oacctp, FWRITE, p->p_ucred, p);
95 	acctwatch(NULL);
96 	return (error);
97 }
98 
99 /*
100  * Periodically check the file system to see if accounting
101  * should be turned on or off.
102  */
103 /* ARGSUSED */
104 void
105 acctwatch(a)
106 	void *a;
107 {
108 	struct statfs sb;
109 
110 	if (savacctp) {
111 		(void)VFS_STATFS(savacctp->v_mount, &sb, (struct proc *)0);
112 		if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
113 			acctp = savacctp;
114 			savacctp = NULL;
115 			log(LOG_NOTICE, "Accounting resumed\n");
116 		}
117 	} else {
118 		if (acctp == NULL)
119 			return;
120 		(void)VFS_STATFS(acctp->v_mount, &sb, (struct proc *)0);
121 		if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
122 			savacctp = acctp;
123 			acctp = NULL;
124 			log(LOG_NOTICE, "Accounting suspended\n");
125 		}
126 	}
127 	timeout(acctwatch, NULL, acctchkfreq * hz);
128 }
129 
130 /*
131  * This routine calculates an accounting record for a process and,
132  * if accounting is enabled, writes it to the accounting file.
133  */
134 acct_process(p)
135 	register struct proc *p;
136 {
137 	register struct rusage *ru;
138 	struct vnode *vp;
139 	struct timeval t, ut, st;
140 	int i, s;
141 	struct acct acctbuf;
142 	register struct acct *ap = &acctbuf;
143 
144 	if ((vp = acctp) == NULL)
145 		return (0);
146 	bcopy(p->p_comm, ap->ac_comm, sizeof(ap->ac_comm));
147 	ru = &p->p_stats->p_ru;
148 	calcru(p, &ut, &st, NULL);
149 	s = splclock();
150 	t = time;
151 	splx(s);
152 	ap->ac_utime = compress(ut.tv_sec, ut.tv_usec);
153 	ap->ac_stime = compress(st.tv_sec, st.tv_usec);
154 	timevalsub(&t, &p->p_stats->p_start);
155 	ap->ac_etime = compress(t.tv_sec, t.tv_usec);
156 	ap->ac_btime = p->p_stats->p_start.tv_sec;
157 	ap->ac_uid = p->p_cred->p_ruid;
158 	ap->ac_gid = p->p_cred->p_rgid;
159 	t = st;
160 	timevaladd(&t, &ut);
161 	if (i = t.tv_sec * hz + t.tv_usec / tick)
162 		ap->ac_mem = (ru->ru_ixrss + ru->ru_idrss + ru->ru_isrss) / i;
163 	else
164 		ap->ac_mem = 0;
165 	ap->ac_io = compress(ru->ru_inblock + ru->ru_oublock, (long)0);
166 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyp)
167 		ap->ac_tty = p->p_session->s_ttyp->t_dev;
168 	else
169 		ap->ac_tty = NODEV;
170 	ap->ac_flag = p->p_acflag;
171 	LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
172 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t)ap, sizeof (acctbuf), (off_t)0,
173 		UIO_SYSSPACE, IO_UNIT|IO_APPEND, p->p_ucred, (int *)0,
174 		(struct proc *)0));
175 }
176 
177 /*
178  * Produce a pseudo-floating point representation
179  * with 3 bits base-8 exponent, 13 bits fraction.
180  */
181 compress(t, ut)
182 	register long t;
183 	long ut;
184 {
185 	register exp = 0, round = 0;
186 
187 	t = t * AHZ;  /* compiler will convert only this format to a shift */
188 	if (ut)
189 		t += ut / (1000000 / AHZ);
190 	while (t >= 8192) {
191 		exp++;
192 		round = t&04;
193 		t >>= 3;
194 	}
195 	if (round) {
196 		t++;
197 		if (t >= 8192) {
198 			t >>= 3;
199 			exp++;
200 		}
201 	}
202 	return ((exp<<13) + t);
203 }
204