Deleted Added
sdiff udiff text old ( 167389 ) new ( 169857 )
full compact
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

--- 54 unchanged lines hidden (view full) ---

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 $");
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>

--- 16 unchanged lines hidden (view full) ---

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 *
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
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 */
115static comp_t encode_comp_t(u_long, u_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.

--- 196 unchanged lines hidden (view full) ---

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{
328 struct acct 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

--- 21 unchanged lines hidden (view full) ---

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);
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);
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);
375 acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
376
377 /* (4) The average amount of memory used */
378 r = &p->p_stats->p_ru;
379 tmp = ut;
380 timevaladd(&tmp, &st);
381 t = tmp.tv_sec * hz + tmp.tv_usec / tick;
382 if (t)
383 acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
384 else
385 acct.ac_mem = 0;
386
387 /* (5) The number of disk I/O operations done */
388 acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
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. */
403 acct.ac_flag = p->p_acflag;
404 PROC_UNLOCK(p);
405
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;

--- 9 unchanged lines hidden (view full) ---

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
431/*
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.
435 */
436
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. */
440
441static comp_t
442encode_comp_t(u_long s, u_long us)
443{
444 int exp, rnd;
445
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++;
455 }
456
457 /* If we need to round up, do it (and handle overflow correctly). */
458 if (rnd && (++s > MAXFRACT)) {
459 s >>= EXPSIZE;
460 exp++;
461 }
462
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);
467}
468
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

--- 101 unchanged lines hidden ---