This source file includes following definitions.
- sys_acct
- acct_process
- encode_comp_t
- acct_start
- acct_thread
- acct_shutdown
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/mount.h>
45 #include <sys/vnode.h>
46 #include <sys/file.h>
47 #include <sys/syslog.h>
48 #include <sys/kernel.h>
49 #include <sys/namei.h>
50 #include <sys/errno.h>
51 #include <sys/acct.h>
52 #include <sys/resourcevar.h>
53 #include <sys/ioctl.h>
54 #include <sys/tty.h>
55 #include <sys/kthread.h>
56
57 #include <sys/syscallargs.h>
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 comp_t encode_comp_t(u_long, u_long);
74 int acct_start(void);
75 void acct_thread(void *);
76 void acct_shutdown(void);
77
78
79
80
81 struct vnode *acctp;
82 struct vnode *savacctp;
83
84
85
86
87 int acctsuspend = 2;
88 int acctresume = 4;
89 int acctchkfreq = 15;
90
91 struct proc *acct_proc;
92
93
94
95
96
97 int
98 sys_acct(struct proc *p, void *v, register_t *retval)
99 {
100 struct sys_acct_args
101
102 *uap = v;
103 struct nameidata nd;
104 int error;
105
106
107 if ((error = suser(p, 0)) != 0)
108 return (error);
109
110
111
112
113
114 if (SCARG(uap, path) != NULL) {
115 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path),
116 p);
117 if ((error = vn_open(&nd, FWRITE|O_APPEND, 0)) != 0)
118 return (error);
119 VOP_UNLOCK(nd.ni_vp, 0, p);
120 if (nd.ni_vp->v_type != VREG) {
121 vn_close(nd.ni_vp, FWRITE, p->p_ucred, p);
122 return (EACCES);
123 }
124 }
125
126
127
128
129
130 if (acctp != NULL || savacctp != NULL) {
131 wakeup(&acct_proc);
132 error = vn_close((acctp != NULL ? acctp : savacctp), FWRITE,
133 p->p_ucred, p);
134 acctp = savacctp = NULL;
135 }
136 if (SCARG(uap, path) == NULL)
137 return (0);
138
139
140
141
142
143 acctp = nd.ni_vp;
144 if ((error = acct_start()) != 0) {
145 acctp = NULL;
146 (void)vn_close(nd.ni_vp, FWRITE, p->p_ucred, p);
147 return (error);
148 }
149 return (0);
150 }
151
152
153
154
155
156
157
158 int
159 acct_process(struct proc *p)
160 {
161 struct acct acct;
162 struct rusage *r;
163 struct timeval ut, st, tmp;
164 int t;
165 struct vnode *vp;
166 struct plimit *oplim = NULL;
167 int error;
168
169
170 vp = acctp;
171 if (vp == NULL)
172 return (0);
173
174
175
176
177
178 if (p->p_p->ps_limit->p_refcnt > 1) {
179 oplim = p->p_p->ps_limit;
180 p->p_p->ps_limit = limcopy(p->p_p->ps_limit);
181 }
182 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
183
184
185
186
187
188
189 bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
190
191
192 calcru(p, &ut, &st, NULL);
193 acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
194 acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
195
196
197 acct.ac_btime = p->p_stats->p_start.tv_sec;
198 getmicrotime(&tmp);
199 timersub(&tmp, &p->p_stats->p_start, &tmp);
200 acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
201
202
203 r = &p->p_stats->p_ru;
204 timeradd(&ut, &st, &tmp);
205 t = tmp.tv_sec * hz + tmp.tv_usec / tick;
206 if (t)
207 acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
208 else
209 acct.ac_mem = 0;
210
211
212 acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
213
214
215 acct.ac_uid = p->p_cred->p_ruid;
216 acct.ac_gid = p->p_cred->p_rgid;
217
218
219 if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
220 acct.ac_tty = p->p_pgrp->pg_session->s_ttyp->t_dev;
221 else
222 acct.ac_tty = NODEV;
223
224
225 acct.ac_flag = p->p_acflag;
226
227
228
229
230 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&acct, sizeof (acct),
231 (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, p->p_ucred, NULL, p);
232
233 if (oplim) {
234 limfree(p->p_p->ps_limit);
235 p->p_p->ps_limit = oplim;
236 }
237
238 return error;
239 }
240
241
242
243
244
245
246
247 #define MANTSIZE 13
248 #define EXPSIZE 3
249 #define MAXFRACT ((1 << MANTSIZE) - 1)
250
251 comp_t
252 encode_comp_t(u_long s, u_long us)
253 {
254 int exp, rnd;
255
256 exp = 0;
257 rnd = 0;
258 s *= AHZ;
259 s += us / (1000000 / AHZ);
260
261 while (s > MAXFRACT) {
262 rnd = s & (1 << (EXPSIZE - 1));
263 s >>= EXPSIZE;
264 exp++;
265 }
266
267
268 if (rnd && (++s > MAXFRACT)) {
269 s >>= EXPSIZE;
270 exp++;
271 }
272
273
274 exp <<= MANTSIZE;
275 exp += s;
276 return (exp);
277 }
278
279 int
280 acct_start(void)
281 {
282
283 if (acct_proc != NULL)
284 return (0);
285
286 return (kthread_create(acct_thread, NULL, &acct_proc, "acct"));
287 }
288
289
290
291
292
293
294
295
296 void
297 acct_thread(void *arg)
298 {
299 struct statfs sb;
300 struct proc *p = curproc;
301
302 for (;;) {
303 if (savacctp != NULL) {
304 if (savacctp->v_type == VBAD) {
305 (void) vn_close(savacctp, FWRITE, NOCRED, p);
306 savacctp = NULL;
307 break;
308 }
309 (void)VFS_STATFS(savacctp->v_mount, &sb, (struct proc *)0);
310 if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
311 acctp = savacctp;
312 savacctp = NULL;
313 log(LOG_NOTICE, "Accounting resumed\n");
314 }
315 } else if (acctp != NULL) {
316 if (acctp->v_type == VBAD) {
317 (void) vn_close(acctp, FWRITE, NOCRED, p);
318 acctp = NULL;
319 break;
320 }
321 (void)VFS_STATFS(acctp->v_mount, &sb, (struct proc *)0);
322 if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
323 savacctp = acctp;
324 acctp = NULL;
325 log(LOG_NOTICE, "Accounting suspended\n");
326 }
327 } else {
328 break;
329 }
330 tsleep(&acct_proc, PPAUSE, "acct", acctchkfreq *hz);
331 }
332 acct_proc = NULL;
333 kthread_exit(0);
334 }
335
336 void
337 acct_shutdown(void)
338 {
339
340 struct proc *p = curproc;
341
342 if (acctp != NULL || savacctp != NULL) {
343 vn_close((acctp != NULL ? acctp : savacctp), FWRITE,
344 NOCRED, p);
345 acctp = savacctp = NULL;
346 }
347 }