xref: /original-bsd/sys/kern/kern_acct.c (revision 5b560bbe)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)kern_acct.c	7.14 (Berkeley) 08/22/90
7  */
8 
9 #include "param.h"
10 #include "systm.h"
11 #include "time.h"
12 #include "proc.h"
13 #include "ioctl.h"
14 #include "termios.h"
15 #include "tty.h"
16 #include "user.h"
17 #include "vnode.h"
18 #include "mount.h"
19 #include "kernel.h"
20 #include "file.h"
21 #include "acct.h"
22 #include "uio.h"
23 #include "syslog.h"
24 
25 /*
26  * Values associated with enabling and disabling accounting
27  */
28 int	acctsuspend = 2;	/* stop accounting when < 2% free space left */
29 int	acctresume = 4;		/* resume when free space risen to > 4% */
30 struct	timeval chk = { 15, 0 };/* frequency to check space for accounting */
31 
32 /*
33  * SHOULD REPLACE THIS WITH A DRIVER THAT CAN BE READ TO SIMPLIFY.
34  */
35 struct	vnode *acctp;
36 struct	vnode *savacctp;
37 
38 /*
39  * Perform process accounting functions.
40  */
41 /* ARGSUSED */
42 sysacct(p, uap, retval)
43 	struct proc *p;
44 	struct args {
45 		char	*fname;
46 	} *uap;
47 	int *retval;
48 {
49 	register struct vnode *vp;
50 	register struct nameidata *ndp = &u.u_nd;
51 	extern int acctwatch();
52 	struct vnode *oacctp;
53 	int error;
54 
55 	if (error = suser(u.u_cred, &u.u_acflag))
56 		return (error);
57 	if (savacctp) {
58 		acctp = savacctp;
59 		savacctp = NULL;
60 	}
61 	if (uap->fname == NULL) {
62 		if (vp = acctp) {
63 			acctp = NULL;
64 			vrele(vp);
65 			untimeout(acctwatch, (caddr_t)&chk);
66 		}
67 		return (0);
68 	}
69 	ndp->ni_segflg = UIO_USERSPACE;
70 	ndp->ni_dirp = uap->fname;
71 	if (error = vn_open(ndp, FWRITE, 0644))
72 		return (error);
73 	vp = ndp->ni_vp;
74 	if (vp->v_type != VREG) {
75 		vrele(vp);
76 		return (EACCES);
77 	}
78 	oacctp = acctp;
79 	acctp = vp;
80 	if (oacctp)
81 		vrele(oacctp);
82 	acctwatch(&chk);
83 	return (0);
84 }
85 
86 /*
87  * Periodically check the file system to see if accounting
88  * should be turned on or off.
89  */
90 acctwatch(resettime)
91 	struct timeval *resettime;
92 {
93 	struct statfs sb;
94 
95 	if (savacctp) {
96 		(void)VFS_STATFS(savacctp->v_mount, &sb);
97 		if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
98 			acctp = savacctp;
99 			savacctp = NULL;
100 			log(LOG_NOTICE, "Accounting resumed\n");
101 			return;
102 		}
103 	}
104 	if (acctp == NULL)
105 		return;
106 	(void)VFS_STATFS(acctp->v_mount, &sb);
107 	if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
108 		savacctp = acctp;
109 		acctp = NULL;
110 		log(LOG_NOTICE, "Accounting suspended\n");
111 	}
112 	timeout(acctwatch, (caddr_t)resettime, hzto(resettime));
113 }
114 
115 /*
116  * On exit, write a record on the accounting file.
117  */
118 acct(p)
119 	register struct proc *p;
120 {
121 	register struct rusage *ru;
122 	struct vnode *vp;
123 	struct timeval t, ut, st;
124 	int i, s;
125 	struct acct acctbuf;
126 	register struct acct *ap = &acctbuf;
127 
128 	if ((vp = acctp) == NULL)
129 		return (0);
130 	bcopy(p->p_comm, ap->ac_comm, sizeof(ap->ac_comm));
131 	ru = &u.u_ru;
132 	s = splclock();
133 	ut = p->p_utime;
134 	st = p->p_stime;
135 	t = time;
136 	splx(s);
137 	ap->ac_utime = compress(ut.tv_sec, ut.tv_usec);
138 	ap->ac_stime = compress(st.tv_sec, st.tv_usec);
139 	timevalsub(&t, &u.u_start);
140 	ap->ac_etime = compress(t.tv_sec, t.tv_usec);
141 	ap->ac_btime = u.u_start.tv_sec;
142 	ap->ac_uid = p->p_ruid;
143 	ap->ac_gid = p->p_rgid;
144 	t = st;
145 	timevaladd(&t, &ut);
146 	if (i = t.tv_sec * hz + t.tv_usec / tick)
147 		ap->ac_mem = (ru->ru_ixrss+ru->ru_idrss+ru->ru_isrss) / i;
148 	else
149 		ap->ac_mem = 0;
150 	ap->ac_io = compress(ru->ru_inblock + ru->ru_oublock, (long)0);
151 	if (p->p_flag&SCTTY && p->p_session->s_ttyp)
152 		ap->ac_tty = p->p_session->s_ttyp->t_dev;
153 	else
154 		ap->ac_tty = NODEV;
155 	ap->ac_flag = u.u_acflag;
156 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t)ap, sizeof (acctbuf),
157 		(off_t)0, UIO_SYSSPACE, IO_UNIT|IO_APPEND, u.u_cred, (int *)0));
158 }
159 
160 /*
161  * Produce a pseudo-floating point representation
162  * with 3 bits base-8 exponent, 13 bits fraction.
163  */
164 compress(t, ut)
165 	register long t;
166 	long ut;
167 {
168 	register exp = 0, round = 0;
169 
170 	t = t * AHZ;  /* compiler will convert only this format to a shift */
171 	if (ut)
172 		t += ut / (1000000 / AHZ);
173 	while (t >= 8192) {
174 		exp++;
175 		round = t&04;
176 		t >>= 3;
177 	}
178 	if (round) {
179 		t++;
180 		if (t >= 8192) {
181 			t >>= 3;
182 			exp++;
183 		}
184 	}
185 	return ((exp<<13) + t);
186 }
187