Deleted Added
full compact
kern_acct.c (167389) kern_acct.c (169857)
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 * Copyright (c) 2005 Robert N. M. Watson
6 * All rights reserved.
7 *
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * Copyright (c) 1994 Christopher G. Demetriou
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)kern_acct.c 8.1 (Berkeley) 6/14/93
68 */
69
70#include <sys/cdefs.h>
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 * Copyright (c) 2005 Robert N. M. Watson
6 * All rights reserved.
7 *
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * Copyright (c) 1994 Christopher G. Demetriou
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)kern_acct.c 8.1 (Berkeley) 6/14/93
68 */
69
70#include <sys/cdefs.h>
71__FBSDID("$FreeBSD: head/sys/kern/kern_acct.c 167389 2007-03-09 23:29:31Z jhb $");
71__FBSDID("$FreeBSD: head/sys/kern/kern_acct.c 169857 2007-05-22 06:51:38Z dds $");
72
73#include "opt_mac.h"
74
75#include <sys/param.h>
76#include <sys/systm.h>
77#include <sys/acct.h>
78#include <sys/fcntl.h>
79#include <sys/kernel.h>
80#include <sys/kthread.h>
81#include <sys/lock.h>
82#include <sys/mount.h>
83#include <sys/mutex.h>
84#include <sys/namei.h>
85#include <sys/priv.h>
86#include <sys/proc.h>
87#include <sys/resourcevar.h>
88#include <sys/sched.h>
89#include <sys/sx.h>
90#include <sys/sysctl.h>
91#include <sys/sysent.h>
92#include <sys/syslog.h>
93#include <sys/sysproto.h>
94#include <sys/tty.h>
95#include <sys/vnode.h>
96
97#include <security/mac/mac_framework.h>
98
99/*
100 * The routines implemented in this file are described in:
101 * Leffler, et al.: The Design and Implementation of the 4.3BSD
102 * UNIX Operating System (Addison Welley, 1989)
103 * on pages 62-63.
72
73#include "opt_mac.h"
74
75#include <sys/param.h>
76#include <sys/systm.h>
77#include <sys/acct.h>
78#include <sys/fcntl.h>
79#include <sys/kernel.h>
80#include <sys/kthread.h>
81#include <sys/lock.h>
82#include <sys/mount.h>
83#include <sys/mutex.h>
84#include <sys/namei.h>
85#include <sys/priv.h>
86#include <sys/proc.h>
87#include <sys/resourcevar.h>
88#include <sys/sched.h>
89#include <sys/sx.h>
90#include <sys/sysctl.h>
91#include <sys/sysent.h>
92#include <sys/syslog.h>
93#include <sys/sysproto.h>
94#include <sys/tty.h>
95#include <sys/vnode.h>
96
97#include <security/mac/mac_framework.h>
98
99/*
100 * The routines implemented in this file are described in:
101 * Leffler, et al.: The Design and Implementation of the 4.3BSD
102 * UNIX Operating System (Addison Welley, 1989)
103 * on pages 62-63.
104 * On May 2007 the historic 3 bits base 8 exponent, 13 bit fraction
105 * compt_t representation described in the above reference was replaced
106 * with that of IEEE-754 floats.
104 *
105 * Arguably, to simplify accounting operations, this mechanism should
106 * be replaced by one in which an accounting log file (similar to /dev/klog)
107 * is read by a user process, etc. However, that has its own problems.
108 */
109
107 *
108 * Arguably, to simplify accounting operations, this mechanism should
109 * be replaced by one in which an accounting log file (similar to /dev/klog)
110 * is read by a user process, etc. However, that has its own problems.
111 */
112
113/* Floating point definitions from <float.h>. */
114#define FLT_MANT_DIG 24 /* p */
115#define FLT_MAX_EXP 128 /* emax */
116
110/*
111 * Internal accounting functions.
112 * The former's operation is described in Leffler, et al., and the latter
113 * was provided by UCB with the 4.4BSD-Lite release
114 */
117/*
118 * Internal accounting functions.
119 * The former's operation is described in Leffler, et al., and the latter
120 * was provided by UCB with the 4.4BSD-Lite release
121 */
115static comp_t encode_comp_t(u_long, u_long);
122static uint32_t encode_timeval(struct timeval);
123static uint32_t encode_long(long);
116static void acctwatch(void);
117static void acct_thread(void *);
118static int acct_disable(struct thread *);
119
120/*
121 * Accounting vnode pointer, saved vnode pointer, and flags for each.
122 * acct_sx protects against changes to the active vnode and credentials
123 * while accounting records are being committed to disk.
124 */
125static int acct_configured;
126static int acct_suspended;
127static struct vnode *acct_vp;
128static struct ucred *acct_cred;
129static int acct_flags;
130static struct sx acct_sx;
131
132SX_SYSINIT(acct, &acct_sx, "acct_sx");
133
134/*
135 * State of the accounting kthread.
136 */
137static int acct_state;
138
139#define ACCT_RUNNING 1 /* Accounting kthread is running. */
140#define ACCT_EXITREQ 2 /* Accounting kthread should exit. */
141
142/*
143 * Values associated with enabling and disabling accounting
144 */
145static int acctsuspend = 2; /* stop accounting when < 2% free space left */
146SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW,
147 &acctsuspend, 0, "percentage of free disk space below which accounting stops");
148
149static int acctresume = 4; /* resume when free space risen to > 4% */
150SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW,
151 &acctresume, 0, "percentage of free disk space above which accounting resumes");
152
153static int acctchkfreq = 15; /* frequency (in seconds) to check space */
154
155static int
156sysctl_acct_chkfreq(SYSCTL_HANDLER_ARGS)
157{
158 int error, value;
159
160 /* Write out the old value. */
161 error = SYSCTL_OUT(req, &acctchkfreq, sizeof(int));
162 if (error || req->newptr == NULL)
163 return (error);
164
165 /* Read in and verify the new value. */
166 error = SYSCTL_IN(req, &value, sizeof(int));
167 if (error)
168 return (error);
169 if (value <= 0)
170 return (EINVAL);
171 acctchkfreq = value;
172 return (0);
173}
174SYSCTL_PROC(_kern, OID_AUTO, acct_chkfreq, CTLTYPE_INT|CTLFLAG_RW,
175 &acctchkfreq, 0, sysctl_acct_chkfreq, "I",
176 "frequency for checking the free space");
177
178SYSCTL_INT(_kern, OID_AUTO, acct_configured, CTLFLAG_RD, &acct_configured, 0,
179 "Accounting configured or not");
180
181SYSCTL_INT(_kern, OID_AUTO, acct_suspended, CTLFLAG_RD, &acct_suspended, 0,
182 "Accounting suspended or not");
183
184/*
185 * Accounting system call. Written based on the specification and previous
186 * implementation done by Mark Tinguely.
187 */
188int
189acct(struct thread *td, struct acct_args *uap)
190{
191 struct nameidata nd;
192 int error, flags, vfslocked;
193
194 error = priv_check(td, PRIV_ACCT);
195 if (error)
196 return (error);
197
198 /*
199 * If accounting is to be started to a file, open that file for
200 * appending and make sure it's a 'normal'.
201 */
202 if (uap->path != NULL) {
203 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1,
204 UIO_USERSPACE, uap->path, td);
205 flags = FWRITE | O_APPEND;
206 error = vn_open(&nd, &flags, 0, -1);
207 if (error)
208 return (error);
209 vfslocked = NDHASGIANT(&nd);
210 NDFREE(&nd, NDF_ONLY_PNBUF);
211#ifdef MAC
212 error = mac_check_system_acct(td->td_ucred, nd.ni_vp);
213 if (error) {
214 VOP_UNLOCK(nd.ni_vp, 0, td);
215 vn_close(nd.ni_vp, flags, td->td_ucred, td);
216 VFS_UNLOCK_GIANT(vfslocked);
217 return (error);
218 }
219#endif
220 VOP_UNLOCK(nd.ni_vp, 0, td);
221 if (nd.ni_vp->v_type != VREG) {
222 vn_close(nd.ni_vp, flags, td->td_ucred, td);
223 VFS_UNLOCK_GIANT(vfslocked);
224 return (EACCES);
225 }
226 VFS_UNLOCK_GIANT(vfslocked);
227#ifdef MAC
228 } else {
229 error = mac_check_system_acct(td->td_ucred, NULL);
230 if (error)
231 return (error);
232#endif
233 }
234
235 /*
236 * Disallow concurrent access to the accounting vnode while we swap
237 * it out, in order to prevent access after close.
238 */
239 sx_xlock(&acct_sx);
240
241 /*
242 * If accounting was previously enabled, kill the old space-watcher,
243 * close the file, and (if no new file was specified, leave). Reset
244 * the suspended state regardless of whether accounting remains
245 * enabled.
246 */
247 acct_suspended = 0;
248 if (acct_vp != NULL) {
249 vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
250 error = acct_disable(td);
251 VFS_UNLOCK_GIANT(vfslocked);
252 }
253 if (uap->path == NULL) {
254 if (acct_state & ACCT_RUNNING) {
255 acct_state |= ACCT_EXITREQ;
256 wakeup(&acct_state);
257 }
258 sx_xunlock(&acct_sx);
259 return (error);
260 }
261
262 /*
263 * Save the new accounting file vnode, and schedule the new
264 * free space watcher.
265 */
266 acct_vp = nd.ni_vp;
267 acct_cred = crhold(td->td_ucred);
268 acct_flags = flags;
269 if (acct_state & ACCT_RUNNING)
270 acct_state &= ~ACCT_EXITREQ;
271 else {
272 /*
273 * Try to start up an accounting kthread. We may start more
274 * than one, but if so the extras will commit suicide as
275 * soon as they start up.
276 */
277 error = kthread_create(acct_thread, NULL, NULL, 0, 0,
278 "accounting");
279 if (error) {
280 vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
281 (void) vn_close(acct_vp, acct_flags, acct_cred, td);
282 VFS_UNLOCK_GIANT(vfslocked);
283 crfree(acct_cred);
284 acct_configured = 0;
285 acct_vp = NULL;
286 acct_cred = NULL;
287 acct_flags = 0;
288 sx_xunlock(&acct_sx);
289 log(LOG_NOTICE, "Unable to start accounting thread\n");
290 return (error);
291 }
292 }
293 acct_configured = 1;
294 sx_xunlock(&acct_sx);
295 log(LOG_NOTICE, "Accounting enabled\n");
296 return (error);
297}
298
299/*
300 * Disable currently in-progress accounting by closing the vnode, dropping
301 * our reference to the credential, and clearing the vnode's flags.
302 */
303static int
304acct_disable(struct thread *td)
305{
306 int error;
307
308 sx_assert(&acct_sx, SX_XLOCKED);
309 error = vn_close(acct_vp, acct_flags, acct_cred, td);
310 crfree(acct_cred);
311 acct_configured = 0;
312 acct_vp = NULL;
313 acct_cred = NULL;
314 acct_flags = 0;
315 log(LOG_NOTICE, "Accounting disabled\n");
316 return (error);
317}
318
319/*
320 * Write out process accounting information, on process exit.
321 * Data to be written out is specified in Leffler, et al.
322 * and are enumerated below. (They're also noted in the system
323 * "acct.h" header file.)
324 */
325int
326acct_process(struct thread *td)
327{
124static void acctwatch(void);
125static void acct_thread(void *);
126static int acct_disable(struct thread *);
127
128/*
129 * Accounting vnode pointer, saved vnode pointer, and flags for each.
130 * acct_sx protects against changes to the active vnode and credentials
131 * while accounting records are being committed to disk.
132 */
133static int acct_configured;
134static int acct_suspended;
135static struct vnode *acct_vp;
136static struct ucred *acct_cred;
137static int acct_flags;
138static struct sx acct_sx;
139
140SX_SYSINIT(acct, &acct_sx, "acct_sx");
141
142/*
143 * State of the accounting kthread.
144 */
145static int acct_state;
146
147#define ACCT_RUNNING 1 /* Accounting kthread is running. */
148#define ACCT_EXITREQ 2 /* Accounting kthread should exit. */
149
150/*
151 * Values associated with enabling and disabling accounting
152 */
153static int acctsuspend = 2; /* stop accounting when < 2% free space left */
154SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW,
155 &acctsuspend, 0, "percentage of free disk space below which accounting stops");
156
157static int acctresume = 4; /* resume when free space risen to > 4% */
158SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW,
159 &acctresume, 0, "percentage of free disk space above which accounting resumes");
160
161static int acctchkfreq = 15; /* frequency (in seconds) to check space */
162
163static int
164sysctl_acct_chkfreq(SYSCTL_HANDLER_ARGS)
165{
166 int error, value;
167
168 /* Write out the old value. */
169 error = SYSCTL_OUT(req, &acctchkfreq, sizeof(int));
170 if (error || req->newptr == NULL)
171 return (error);
172
173 /* Read in and verify the new value. */
174 error = SYSCTL_IN(req, &value, sizeof(int));
175 if (error)
176 return (error);
177 if (value <= 0)
178 return (EINVAL);
179 acctchkfreq = value;
180 return (0);
181}
182SYSCTL_PROC(_kern, OID_AUTO, acct_chkfreq, CTLTYPE_INT|CTLFLAG_RW,
183 &acctchkfreq, 0, sysctl_acct_chkfreq, "I",
184 "frequency for checking the free space");
185
186SYSCTL_INT(_kern, OID_AUTO, acct_configured, CTLFLAG_RD, &acct_configured, 0,
187 "Accounting configured or not");
188
189SYSCTL_INT(_kern, OID_AUTO, acct_suspended, CTLFLAG_RD, &acct_suspended, 0,
190 "Accounting suspended or not");
191
192/*
193 * Accounting system call. Written based on the specification and previous
194 * implementation done by Mark Tinguely.
195 */
196int
197acct(struct thread *td, struct acct_args *uap)
198{
199 struct nameidata nd;
200 int error, flags, vfslocked;
201
202 error = priv_check(td, PRIV_ACCT);
203 if (error)
204 return (error);
205
206 /*
207 * If accounting is to be started to a file, open that file for
208 * appending and make sure it's a 'normal'.
209 */
210 if (uap->path != NULL) {
211 NDINIT(&nd, LOOKUP, NOFOLLOW | MPSAFE | AUDITVNODE1,
212 UIO_USERSPACE, uap->path, td);
213 flags = FWRITE | O_APPEND;
214 error = vn_open(&nd, &flags, 0, -1);
215 if (error)
216 return (error);
217 vfslocked = NDHASGIANT(&nd);
218 NDFREE(&nd, NDF_ONLY_PNBUF);
219#ifdef MAC
220 error = mac_check_system_acct(td->td_ucred, nd.ni_vp);
221 if (error) {
222 VOP_UNLOCK(nd.ni_vp, 0, td);
223 vn_close(nd.ni_vp, flags, td->td_ucred, td);
224 VFS_UNLOCK_GIANT(vfslocked);
225 return (error);
226 }
227#endif
228 VOP_UNLOCK(nd.ni_vp, 0, td);
229 if (nd.ni_vp->v_type != VREG) {
230 vn_close(nd.ni_vp, flags, td->td_ucred, td);
231 VFS_UNLOCK_GIANT(vfslocked);
232 return (EACCES);
233 }
234 VFS_UNLOCK_GIANT(vfslocked);
235#ifdef MAC
236 } else {
237 error = mac_check_system_acct(td->td_ucred, NULL);
238 if (error)
239 return (error);
240#endif
241 }
242
243 /*
244 * Disallow concurrent access to the accounting vnode while we swap
245 * it out, in order to prevent access after close.
246 */
247 sx_xlock(&acct_sx);
248
249 /*
250 * If accounting was previously enabled, kill the old space-watcher,
251 * close the file, and (if no new file was specified, leave). Reset
252 * the suspended state regardless of whether accounting remains
253 * enabled.
254 */
255 acct_suspended = 0;
256 if (acct_vp != NULL) {
257 vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
258 error = acct_disable(td);
259 VFS_UNLOCK_GIANT(vfslocked);
260 }
261 if (uap->path == NULL) {
262 if (acct_state & ACCT_RUNNING) {
263 acct_state |= ACCT_EXITREQ;
264 wakeup(&acct_state);
265 }
266 sx_xunlock(&acct_sx);
267 return (error);
268 }
269
270 /*
271 * Save the new accounting file vnode, and schedule the new
272 * free space watcher.
273 */
274 acct_vp = nd.ni_vp;
275 acct_cred = crhold(td->td_ucred);
276 acct_flags = flags;
277 if (acct_state & ACCT_RUNNING)
278 acct_state &= ~ACCT_EXITREQ;
279 else {
280 /*
281 * Try to start up an accounting kthread. We may start more
282 * than one, but if so the extras will commit suicide as
283 * soon as they start up.
284 */
285 error = kthread_create(acct_thread, NULL, NULL, 0, 0,
286 "accounting");
287 if (error) {
288 vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
289 (void) vn_close(acct_vp, acct_flags, acct_cred, td);
290 VFS_UNLOCK_GIANT(vfslocked);
291 crfree(acct_cred);
292 acct_configured = 0;
293 acct_vp = NULL;
294 acct_cred = NULL;
295 acct_flags = 0;
296 sx_xunlock(&acct_sx);
297 log(LOG_NOTICE, "Unable to start accounting thread\n");
298 return (error);
299 }
300 }
301 acct_configured = 1;
302 sx_xunlock(&acct_sx);
303 log(LOG_NOTICE, "Accounting enabled\n");
304 return (error);
305}
306
307/*
308 * Disable currently in-progress accounting by closing the vnode, dropping
309 * our reference to the credential, and clearing the vnode's flags.
310 */
311static int
312acct_disable(struct thread *td)
313{
314 int error;
315
316 sx_assert(&acct_sx, SX_XLOCKED);
317 error = vn_close(acct_vp, acct_flags, acct_cred, td);
318 crfree(acct_cred);
319 acct_configured = 0;
320 acct_vp = NULL;
321 acct_cred = NULL;
322 acct_flags = 0;
323 log(LOG_NOTICE, "Accounting disabled\n");
324 return (error);
325}
326
327/*
328 * Write out process accounting information, on process exit.
329 * Data to be written out is specified in Leffler, et al.
330 * and are enumerated below. (They're also noted in the system
331 * "acct.h" header file.)
332 */
333int
334acct_process(struct thread *td)
335{
328 struct acct acct;
336 struct acctv2 acct;
329 struct timeval ut, st, tmp;
330 struct plimit *newlim, *oldlim;
331 struct proc *p;
332 struct rusage *r;
333 int t, ret, vfslocked;
334
335 /*
336 * Lockless check of accounting condition before doing the hard
337 * work.
338 */
339 if (acct_vp == NULL || acct_suspended)
340 return (0);
341
342 sx_slock(&acct_sx);
343
344 /*
345 * If accounting isn't enabled, don't bother. Have to check again
346 * once we own the lock in case we raced with disabling of accounting
347 * by another thread.
348 */
349 if (acct_vp == NULL || acct_suspended) {
350 sx_sunlock(&acct_sx);
351 return (0);
352 }
353
354 p = td->td_proc;
355
356 /*
357 * Get process accounting information.
358 */
359
360 PROC_LOCK(p);
361 /* (1) The name of the command that ran */
362 bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
363
364 /* (2) The amount of user and system time that was used */
365 calcru(p, &ut, &st);
337 struct timeval ut, st, tmp;
338 struct plimit *newlim, *oldlim;
339 struct proc *p;
340 struct rusage *r;
341 int t, ret, vfslocked;
342
343 /*
344 * Lockless check of accounting condition before doing the hard
345 * work.
346 */
347 if (acct_vp == NULL || acct_suspended)
348 return (0);
349
350 sx_slock(&acct_sx);
351
352 /*
353 * If accounting isn't enabled, don't bother. Have to check again
354 * once we own the lock in case we raced with disabling of accounting
355 * by another thread.
356 */
357 if (acct_vp == NULL || acct_suspended) {
358 sx_sunlock(&acct_sx);
359 return (0);
360 }
361
362 p = td->td_proc;
363
364 /*
365 * Get process accounting information.
366 */
367
368 PROC_LOCK(p);
369 /* (1) The name of the command that ran */
370 bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
371
372 /* (2) The amount of user and system time that was used */
373 calcru(p, &ut, &st);
366 acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
367 acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
374 acct.ac_utime = encode_timeval(ut);
375 acct.ac_stime = encode_timeval(st);
368
369 /* (3) The elapsed time the command ran (and its starting time) */
370 tmp = boottime;
371 timevaladd(&tmp, &p->p_stats->p_start);
372 acct.ac_btime = tmp.tv_sec;
373 microuptime(&tmp);
374 timevalsub(&tmp, &p->p_stats->p_start);
376
377 /* (3) The elapsed time the command ran (and its starting time) */
378 tmp = boottime;
379 timevaladd(&tmp, &p->p_stats->p_start);
380 acct.ac_btime = tmp.tv_sec;
381 microuptime(&tmp);
382 timevalsub(&tmp, &p->p_stats->p_start);
375 acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
383 acct.ac_etime = encode_timeval(tmp);
376
377 /* (4) The average amount of memory used */
378 r = &p->p_stats->p_ru;
379 tmp = ut;
380 timevaladd(&tmp, &st);
384
385 /* (4) The average amount of memory used */
386 r = &p->p_stats->p_ru;
387 tmp = ut;
388 timevaladd(&tmp, &st);
389 /* Convert tmp (i.e. u + s) into hz units to match ru_i*. */
381 t = tmp.tv_sec * hz + tmp.tv_usec / tick;
382 if (t)
390 t = tmp.tv_sec * hz + tmp.tv_usec / tick;
391 if (t)
383 acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
392 acct.ac_mem = encode_long((r->ru_ixrss + r->ru_idrss +
393 + r->ru_isrss) / t);
384 else
385 acct.ac_mem = 0;
386
387 /* (5) The number of disk I/O operations done */
394 else
395 acct.ac_mem = 0;
396
397 /* (5) The number of disk I/O operations done */
388 acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
398 acct.ac_io = encode_long(r->ru_inblock + r->ru_oublock);
389
390 /* (6) The UID and GID of the process */
391 acct.ac_uid = p->p_ucred->cr_ruid;
392 acct.ac_gid = p->p_ucred->cr_rgid;
393
394 /* (7) The terminal from which the process was started */
395 SESS_LOCK(p->p_session);
396 if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
397 acct.ac_tty = dev2udev(p->p_pgrp->pg_session->s_ttyp->t_dev);
398 else
399 acct.ac_tty = NODEV;
400 SESS_UNLOCK(p->p_session);
401
402 /* (8) The boolean flags that tell how the process terminated, etc. */
399
400 /* (6) The UID and GID of the process */
401 acct.ac_uid = p->p_ucred->cr_ruid;
402 acct.ac_gid = p->p_ucred->cr_rgid;
403
404 /* (7) The terminal from which the process was started */
405 SESS_LOCK(p->p_session);
406 if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
407 acct.ac_tty = dev2udev(p->p_pgrp->pg_session->s_ttyp->t_dev);
408 else
409 acct.ac_tty = NODEV;
410 SESS_UNLOCK(p->p_session);
411
412 /* (8) The boolean flags that tell how the process terminated, etc. */
403 acct.ac_flag = p->p_acflag;
413 acct.ac_flagx = p->p_acflag;
404 PROC_UNLOCK(p);
405
414 PROC_UNLOCK(p);
415
416 /* Setup ancillary structure fields. */
417 acct.ac_flagx |= ANVER;
418 acct.ac_zero = 0;
419 acct.ac_version = 2;
420 acct.ac_len = acct.ac_len2 = sizeof(acct);
421
406 /*
407 * Eliminate any file size rlimit.
408 */
409 newlim = lim_alloc();
410 PROC_LOCK(p);
411 oldlim = p->p_limit;
412 lim_copy(newlim, oldlim);
413 newlim->pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
414 p->p_limit = newlim;
415 PROC_UNLOCK(p);
416 lim_free(oldlim);
417
418 /*
419 * Write the accounting information to the file.
420 */
421 vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
422 VOP_LEASE(acct_vp, td, acct_cred, LEASE_WRITE);
423 ret = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct, sizeof (acct),
424 (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acct_cred, NOCRED,
425 (int *)0, td);
426 VFS_UNLOCK_GIANT(vfslocked);
427 sx_sunlock(&acct_sx);
428 return (ret);
429}
430
422 /*
423 * Eliminate any file size rlimit.
424 */
425 newlim = lim_alloc();
426 PROC_LOCK(p);
427 oldlim = p->p_limit;
428 lim_copy(newlim, oldlim);
429 newlim->pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
430 p->p_limit = newlim;
431 PROC_UNLOCK(p);
432 lim_free(oldlim);
433
434 /*
435 * Write the accounting information to the file.
436 */
437 vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
438 VOP_LEASE(acct_vp, td, acct_cred, LEASE_WRITE);
439 ret = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct, sizeof (acct),
440 (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acct_cred, NOCRED,
441 (int *)0, td);
442 VFS_UNLOCK_GIANT(vfslocked);
443 sx_sunlock(&acct_sx);
444 return (ret);
445}
446
447/* FLOAT_CONVERSION_START (Regression testing; don't remove this line.) */
448
449/* Convert timevals and longs into IEEE-754 bit patterns. */
450
451/* Mantissa mask (MSB is implied, so subtract 1). */
452#define MANT_MASK ((1 << (FLT_MANT_DIG - 1)) - 1)
453
431/*
454/*
432 * Encode_comp_t converts from ticks in seconds and microseconds
433 * to ticks in 1/AHZ seconds. The encoding is described in
434 * Leffler, et al., on page 63.
455 * We calculate integer values to a precision of approximately
456 * 28 bits.
457 * This is high-enough precision to fill the 24 float bits
458 * and low-enough to avoid overflowing the 32 int bits.
435 */
459 */
460#define CALC_BITS 28
436
461
437#define MANTSIZE 13 /* 13 bit mantissa. */
438#define EXPSIZE 3 /* Base 8 (3 bit) exponent. */
439#define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */
462/* log_2(1000000). */
463#define LOG2_1M 20
440
464
441static comp_t
442encode_comp_t(u_long s, u_long us)
465/*
466 * Convert the elements of a timeval into a 32-bit word holding
467 * the bits of a IEEE-754 float.
468 * The float value represents the timeval's value in microsecond units.
469 */
470static uint32_t
471encode_timeval(struct timeval tv)
443{
472{
444 int exp, rnd;
473 int log2_s;
474 int val, exp; /* Unnormalized value and exponent */
475 int norm_exp; /* Normalized exponent */
476 int shift;
445
477
446 exp = 0;
447 rnd = 0;
448 s *= AHZ;
449 s += us / (1000000 / AHZ); /* Maximize precision. */
450
451 while (s > MAXFRACT) {
452 rnd = s & (1 << (EXPSIZE - 1)); /* Round up? */
453 s >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */
454 exp++;
478 /*
479 * First calculate value and exponent to about CALC_BITS precision.
480 * Note that the following conditionals have been ordered so that
481 * the most common cases appear first.
482 */
483 if (tv.tv_sec == 0) {
484 if (tv.tv_usec == 0)
485 return (0);
486 exp = 0;
487 val = tv.tv_usec;
488 } else {
489 /*
490 * Calculate the value to a precision of approximately
491 * CALC_BITS.
492 */
493 log2_s = fls(tv.tv_sec) - 1;
494 if (log2_s + LOG2_1M < CALC_BITS) {
495 exp = 0;
496 val = 1000000 * tv.tv_sec + tv.tv_usec;
497 } else {
498 exp = log2_s + LOG2_1M - CALC_BITS;
499 val = (unsigned int)(((u_int64_t)1000000 * tv.tv_sec +
500 tv.tv_usec) >> exp);
501 }
455 }
502 }
503 /* Now normalize and pack the value into an IEEE-754 float. */
504 norm_exp = fls(val) - 1;
505 shift = FLT_MANT_DIG - norm_exp - 1;
506#ifdef ACCT_DEBUG
507 printf("val=%d exp=%d shift=%d log2(val)=%d\n",
508 val, exp, shift, norm_exp);
509 printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
510 ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
511#endif
512 return (((FLT_MAX_EXP - 1 + exp + norm_exp) << (FLT_MANT_DIG - 1)) |
513 ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
514}
456
515
457 /* If we need to round up, do it (and handle overflow correctly). */
458 if (rnd && (++s > MAXFRACT)) {
459 s >>= EXPSIZE;
460 exp++;
461 }
516/*
517 * Convert a non-negative long value into the bit pattern of
518 * an IEEE-754 float value.
519 */
520static uint32_t
521encode_long(long val)
522{
523 int norm_exp; /* Normalized exponent */
524 int shift;
462
525
463 /* Clean it up and polish it off. */
464 exp <<= MANTSIZE; /* Shift the exponent into place */
465 exp += s; /* and add on the mantissa. */
466 return (exp);
526 KASSERT(val >= 0, ("encode_long: -ve value %ld", val));
527 if (val == 0)
528 return (0);
529 norm_exp = fls(val) - 1;
530 shift = FLT_MANT_DIG - norm_exp - 1;
531#ifdef ACCT_DEBUG
532 printf("val=%d shift=%d log2(val)=%d\n",
533 val, shift, norm_exp);
534 printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
535 ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
536#endif
537 return (((FLT_MAX_EXP - 1 + norm_exp) << (FLT_MANT_DIG - 1)) |
538 ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
467}
468
539}
540
541/* FLOAT_CONVERSION_END (Regression testing; don't remove this line.) */
542
469/*
470 * Periodically check the filesystem to see if accounting
471 * should be turned on or off. Beware the case where the vnode
472 * has been vgone()'d out from underneath us, e.g. when the file
473 * system containing the accounting file has been forcibly unmounted.
474 */
475/* ARGSUSED */
476static void
477acctwatch(void)
478{
479 struct statfs sb;
480 int vfslocked;
481
482 sx_assert(&acct_sx, SX_XLOCKED);
483
484 /*
485 * If accounting was disabled before our kthread was scheduled,
486 * then acct_vp might be NULL. If so, just ask our kthread to
487 * exit and return.
488 */
489 if (acct_vp == NULL) {
490 acct_state |= ACCT_EXITREQ;
491 return;
492 }
493
494 /*
495 * If our vnode is no longer valid, tear it down and signal the
496 * accounting thread to die.
497 */
498 vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
499 if (acct_vp->v_type == VBAD) {
500 (void) acct_disable(NULL);
501 VFS_UNLOCK_GIANT(vfslocked);
502 acct_state |= ACCT_EXITREQ;
503 return;
504 }
505
506 /*
507 * Stopping here is better than continuing, maybe it will be VBAD
508 * next time around.
509 */
510 if (VFS_STATFS(acct_vp->v_mount, &sb, curthread) < 0) {
511 VFS_UNLOCK_GIANT(vfslocked);
512 return;
513 }
514 VFS_UNLOCK_GIANT(vfslocked);
515 if (acct_suspended) {
516 if (sb.f_bavail > (int64_t)(acctresume * sb.f_blocks /
517 100)) {
518 acct_suspended = 0;
519 log(LOG_NOTICE, "Accounting resumed\n");
520 }
521 } else {
522 if (sb.f_bavail <= (int64_t)(acctsuspend * sb.f_blocks /
523 100)) {
524 acct_suspended = 1;
525 log(LOG_NOTICE, "Accounting suspended\n");
526 }
527 }
528}
529
530/*
531 * The main loop for the dedicated kernel thread that periodically calls
532 * acctwatch().
533 */
534static void
535acct_thread(void *dummy)
536{
537 u_char pri;
538
539 /* This is a low-priority kernel thread. */
540 pri = PRI_MAX_KERN;
541 mtx_lock_spin(&sched_lock);
542 sched_prio(curthread, pri);
543 mtx_unlock_spin(&sched_lock);
544
545 /* If another accounting kthread is already running, just die. */
546 sx_xlock(&acct_sx);
547 if (acct_state & ACCT_RUNNING) {
548 sx_xunlock(&acct_sx);
549 kthread_exit(0);
550 }
551 acct_state |= ACCT_RUNNING;
552
553 /* Loop until we are asked to exit. */
554 while (!(acct_state & ACCT_EXITREQ)) {
555
556 /* Perform our periodic checks. */
557 acctwatch();
558
559 /*
560 * We check this flag again before sleeping since the
561 * acctwatch() might have shut down accounting and asked us
562 * to exit.
563 */
564 if (!(acct_state & ACCT_EXITREQ)) {
565 sx_sleep(&acct_state, &acct_sx, 0, "-",
566 acctchkfreq * hz);
567 }
568 }
569
570 /*
571 * Acknowledge the exit request and shutdown. We clear both the
572 * exit request and running flags.
573 */
574 acct_state = 0;
575 sx_xunlock(&acct_sx);
576 kthread_exit(0);
577}
543/*
544 * Periodically check the filesystem to see if accounting
545 * should be turned on or off. Beware the case where the vnode
546 * has been vgone()'d out from underneath us, e.g. when the file
547 * system containing the accounting file has been forcibly unmounted.
548 */
549/* ARGSUSED */
550static void
551acctwatch(void)
552{
553 struct statfs sb;
554 int vfslocked;
555
556 sx_assert(&acct_sx, SX_XLOCKED);
557
558 /*
559 * If accounting was disabled before our kthread was scheduled,
560 * then acct_vp might be NULL. If so, just ask our kthread to
561 * exit and return.
562 */
563 if (acct_vp == NULL) {
564 acct_state |= ACCT_EXITREQ;
565 return;
566 }
567
568 /*
569 * If our vnode is no longer valid, tear it down and signal the
570 * accounting thread to die.
571 */
572 vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
573 if (acct_vp->v_type == VBAD) {
574 (void) acct_disable(NULL);
575 VFS_UNLOCK_GIANT(vfslocked);
576 acct_state |= ACCT_EXITREQ;
577 return;
578 }
579
580 /*
581 * Stopping here is better than continuing, maybe it will be VBAD
582 * next time around.
583 */
584 if (VFS_STATFS(acct_vp->v_mount, &sb, curthread) < 0) {
585 VFS_UNLOCK_GIANT(vfslocked);
586 return;
587 }
588 VFS_UNLOCK_GIANT(vfslocked);
589 if (acct_suspended) {
590 if (sb.f_bavail > (int64_t)(acctresume * sb.f_blocks /
591 100)) {
592 acct_suspended = 0;
593 log(LOG_NOTICE, "Accounting resumed\n");
594 }
595 } else {
596 if (sb.f_bavail <= (int64_t)(acctsuspend * sb.f_blocks /
597 100)) {
598 acct_suspended = 1;
599 log(LOG_NOTICE, "Accounting suspended\n");
600 }
601 }
602}
603
604/*
605 * The main loop for the dedicated kernel thread that periodically calls
606 * acctwatch().
607 */
608static void
609acct_thread(void *dummy)
610{
611 u_char pri;
612
613 /* This is a low-priority kernel thread. */
614 pri = PRI_MAX_KERN;
615 mtx_lock_spin(&sched_lock);
616 sched_prio(curthread, pri);
617 mtx_unlock_spin(&sched_lock);
618
619 /* If another accounting kthread is already running, just die. */
620 sx_xlock(&acct_sx);
621 if (acct_state & ACCT_RUNNING) {
622 sx_xunlock(&acct_sx);
623 kthread_exit(0);
624 }
625 acct_state |= ACCT_RUNNING;
626
627 /* Loop until we are asked to exit. */
628 while (!(acct_state & ACCT_EXITREQ)) {
629
630 /* Perform our periodic checks. */
631 acctwatch();
632
633 /*
634 * We check this flag again before sleeping since the
635 * acctwatch() might have shut down accounting and asked us
636 * to exit.
637 */
638 if (!(acct_state & ACCT_EXITREQ)) {
639 sx_sleep(&acct_state, &acct_sx, 0, "-",
640 acctchkfreq * hz);
641 }
642 }
643
644 /*
645 * Acknowledge the exit request and shutdown. We clear both the
646 * exit request and running flags.
647 */
648 acct_state = 0;
649 sx_xunlock(&acct_sx);
650 kthread_exit(0);
651}