1994-05-24 10:09:53 +00:00
|
|
|
/*-
|
1994-09-26 21:09:15 +00:00
|
|
|
* Copyright (c) 1994 Christopher G. Demetriou
|
1994-05-24 10:09:53 +00:00
|
|
|
* Copyright (c) 1982, 1986, 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1994-09-26 21:09:15 +00:00
|
|
|
* @(#)kern_acct.c 8.1 (Berkeley) 6/14/93
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
1994-09-15 19:47:47 +00:00
|
|
|
#include <sys/systm.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
1995-11-12 06:43:28 +00:00
|
|
|
#include <sys/sysproto.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/vnode.h>
|
1997-03-23 03:37:54 +00:00
|
|
|
#include <sys/fcntl.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/syslog.h>
|
|
|
|
#include <sys/kernel.h>
|
1997-02-10 02:22:35 +00:00
|
|
|
#include <sys/sysent.h>
|
1995-12-14 08:32:45 +00:00
|
|
|
#include <sys/sysctl.h>
|
1994-09-26 21:09:15 +00:00
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/acct.h>
|
|
|
|
#include <sys/resourcevar.h>
|
|
|
|
#include <sys/tty.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1994-09-26 21:09:15 +00:00
|
|
|
/*
|
|
|
|
* The routines implemented in this file are described in:
|
|
|
|
* Leffler, et al.: The Design and Implementation of the 4.3BSD
|
|
|
|
* UNIX Operating System (Addison Welley, 1989)
|
|
|
|
* on pages 62-63.
|
|
|
|
*
|
|
|
|
* Arguably, to simplify accounting operations, this mechanism should
|
|
|
|
* be replaced by one in which an accounting log file (similar to /dev/klog)
|
|
|
|
* is read by a user process, etc. However, that has its own problems.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal accounting functions.
|
|
|
|
* The former's operation is described in Leffler, et al., and the latter
|
|
|
|
* was provided by UCB with the 4.4BSD-Lite release
|
|
|
|
*/
|
2002-03-19 21:25:46 +00:00
|
|
|
static comp_t encode_comp_t(u_long, u_long);
|
|
|
|
static void acctwatch(void *);
|
1994-09-26 21:09:15 +00:00
|
|
|
|
1997-09-21 22:00:25 +00:00
|
|
|
/*
|
2000-11-27 22:52:31 +00:00
|
|
|
* Accounting callout used for periodic scheduling of acctwatch.
|
1997-09-21 22:00:25 +00:00
|
|
|
*/
|
2000-11-27 22:52:31 +00:00
|
|
|
static struct callout acctwatch_callout;
|
1997-09-21 22:00:25 +00:00
|
|
|
|
1994-09-26 21:09:15 +00:00
|
|
|
/*
|
2002-07-21 15:22:56 +00:00
|
|
|
* Accounting vnode pointer, saved vnode pointer, and flags for each.
|
1994-09-26 21:09:15 +00:00
|
|
|
*/
|
1995-12-14 08:32:45 +00:00
|
|
|
static struct vnode *acctp;
|
2002-08-07 19:30:16 +00:00
|
|
|
static struct ucred *acctcred;
|
2002-07-21 15:22:56 +00:00
|
|
|
static int acctflags;
|
1995-12-14 08:32:45 +00:00
|
|
|
static struct vnode *savacctp;
|
2002-08-07 19:30:16 +00:00
|
|
|
static struct ucred *savacctcred;
|
2002-07-21 15:22:56 +00:00
|
|
|
static int savacctflags;
|
1994-09-26 21:09:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Values associated with enabling and disabling accounting
|
|
|
|
*/
|
1995-12-14 08:32:45 +00:00
|
|
|
static int acctsuspend = 2; /* stop accounting when < 2% free space left */
|
|
|
|
SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW,
|
2000-06-26 13:52:31 +00:00
|
|
|
&acctsuspend, 0, "percentage of free disk space below which accounting stops");
|
1995-12-14 08:32:45 +00:00
|
|
|
|
|
|
|
static int acctresume = 4; /* resume when free space risen to > 4% */
|
|
|
|
SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW,
|
2000-06-26 13:52:31 +00:00
|
|
|
&acctresume, 0, "percentage of free disk space above which accounting resumes");
|
1995-12-14 08:32:45 +00:00
|
|
|
|
|
|
|
static int acctchkfreq = 15; /* frequency (in seconds) to check space */
|
|
|
|
SYSCTL_INT(_kern, OID_AUTO, acct_chkfreq, CTLFLAG_RW,
|
2000-06-26 13:52:31 +00:00
|
|
|
&acctchkfreq, 0, "frequency for checking the free space");
|
1994-09-26 21:09:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Accounting system call. Written based on the specification and
|
|
|
|
* previous implementation done by Mark Tinguely.
|
2001-09-01 03:04:31 +00:00
|
|
|
*
|
|
|
|
* MPSAFE
|
1994-09-26 21:09:15 +00:00
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
acct(td, uap)
|
|
|
|
struct thread *td;
|
1997-02-10 02:22:35 +00:00
|
|
|
struct acct_args /* {
|
|
|
|
syscallarg(char *) path;
|
|
|
|
} */ *uap;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
1994-09-26 21:09:15 +00:00
|
|
|
struct nameidata nd;
|
2000-07-04 03:34:11 +00:00
|
|
|
int error, flags;
|
1994-09-26 21:09:15 +00:00
|
|
|
|
|
|
|
/* Make sure that the caller is root. */
|
2002-04-01 21:31:13 +00:00
|
|
|
error = suser(td);
|
1994-10-02 17:35:40 +00:00
|
|
|
if (error)
|
2002-04-09 19:58:38 +00:00
|
|
|
return (error);
|
1994-09-26 21:09:15 +00:00
|
|
|
|
2002-04-09 19:58:38 +00:00
|
|
|
mtx_lock(&Giant);
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
1994-09-26 21:09:15 +00:00
|
|
|
* If accounting is to be started to a file, open that file for
|
2002-07-10 17:31:58 +00:00
|
|
|
* appending and make sure it's a 'normal'.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
1997-02-10 02:22:35 +00:00
|
|
|
if (SCARG(uap, path) != NULL) {
|
|
|
|
NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path),
|
2001-09-12 08:38:13 +00:00
|
|
|
td);
|
2002-07-10 17:31:58 +00:00
|
|
|
flags = FWRITE | O_APPEND;
|
2000-07-04 03:34:11 +00:00
|
|
|
error = vn_open(&nd, &flags, 0);
|
1994-10-02 17:35:40 +00:00
|
|
|
if (error)
|
2001-09-01 03:04:31 +00:00
|
|
|
goto done2;
|
1999-12-15 23:02:35 +00:00
|
|
|
NDFREE(&nd, NDF_ONLY_PNBUF);
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(nd.ni_vp, 0, td);
|
1994-09-26 21:09:15 +00:00
|
|
|
if (nd.ni_vp->v_type != VREG) {
|
2002-07-21 15:22:56 +00:00
|
|
|
vn_close(nd.ni_vp, flags, td->td_ucred, td);
|
2001-09-01 03:04:31 +00:00
|
|
|
error = EACCES;
|
|
|
|
goto done2;
|
1994-09-26 21:09:15 +00:00
|
|
|
}
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1994-09-26 21:09:15 +00:00
|
|
|
/*
|
|
|
|
* If accounting was previously enabled, kill the old space-watcher,
|
|
|
|
* close the file, and (if no new file was specified, leave).
|
|
|
|
*/
|
|
|
|
if (acctp != NULLVP || savacctp != NULLVP) {
|
2000-11-27 22:52:31 +00:00
|
|
|
callout_stop(&acctwatch_callout);
|
2002-07-10 17:31:58 +00:00
|
|
|
error = vn_close((acctp != NULLVP ? acctp : savacctp),
|
2002-07-21 15:22:56 +00:00
|
|
|
(acctp != NULLVP ? acctflags : savacctflags),
|
2002-08-07 19:30:16 +00:00
|
|
|
(acctcred != NOCRED ? acctcred : savacctcred), td);
|
1994-09-26 21:09:15 +00:00
|
|
|
acctp = savacctp = NULLVP;
|
2002-08-07 19:30:16 +00:00
|
|
|
crfree(acctcred != NOCRED ? acctcred : savacctcred);
|
|
|
|
acctcred = savacctcred = NOCRED;
|
1994-09-26 21:09:15 +00:00
|
|
|
}
|
1997-02-10 02:22:35 +00:00
|
|
|
if (SCARG(uap, path) == NULL)
|
2001-09-01 03:04:31 +00:00
|
|
|
goto done2;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
/*
|
1994-09-26 21:09:15 +00:00
|
|
|
* Save the new accounting file vnode, and schedule the new
|
|
|
|
* free space watcher.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
1994-09-26 21:09:15 +00:00
|
|
|
acctp = nd.ni_vp;
|
2002-08-07 19:30:16 +00:00
|
|
|
acctcred = crhold(td->td_ucred);
|
2002-07-21 15:22:56 +00:00
|
|
|
acctflags = flags;
|
2000-11-27 22:52:31 +00:00
|
|
|
callout_init(&acctwatch_callout, 0);
|
1994-09-26 21:09:15 +00:00
|
|
|
acctwatch(NULL);
|
2001-09-01 03:04:31 +00:00
|
|
|
done2:
|
|
|
|
mtx_unlock(&Giant);
|
1994-09-26 21:09:15 +00:00
|
|
|
return (error);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-09-26 21:09:15 +00:00
|
|
|
* Write out process accounting information, on process exit.
|
|
|
|
* Data to be written out is specified in Leffler, et al.
|
|
|
|
* and are enumerated below. (They're also noted in the system
|
|
|
|
* "acct.h" header file.)
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
|
1994-09-26 21:09:15 +00:00
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
acct_process(td)
|
|
|
|
struct thread *td;
|
1994-09-26 21:09:15 +00:00
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
struct proc *p = td->td_proc;
|
1994-09-26 21:09:15 +00:00
|
|
|
struct acct acct;
|
|
|
|
struct rusage *r;
|
|
|
|
struct timeval ut, st, tmp;
|
2001-01-27 00:01:31 +00:00
|
|
|
int t;
|
1994-09-26 21:09:15 +00:00
|
|
|
struct vnode *vp;
|
|
|
|
|
|
|
|
/* If accounting isn't enabled, don't bother */
|
|
|
|
vp = acctp;
|
|
|
|
if (vp == NULLVP)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get process accounting information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* (1) The name of the command that ran */
|
|
|
|
bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
|
|
|
|
|
|
|
|
/* (2) The amount of user and system time that was used */
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
1994-09-26 21:09:15 +00:00
|
|
|
calcru(p, &ut, &st, NULL);
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
1994-09-26 21:09:15 +00:00
|
|
|
acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
|
|
|
|
acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
|
|
|
|
|
|
|
|
/* (3) The elapsed time the commmand ran (and its starting time) */
|
|
|
|
acct.ac_btime = p->p_stats->p_start.tv_sec;
|
1995-07-23 23:02:20 +00:00
|
|
|
microtime(&tmp);
|
1994-09-26 21:09:15 +00:00
|
|
|
timevalsub(&tmp, &p->p_stats->p_start);
|
|
|
|
acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
|
|
|
|
|
|
|
|
/* (4) The average amount of memory used */
|
|
|
|
r = &p->p_stats->p_ru;
|
|
|
|
tmp = ut;
|
|
|
|
timevaladd(&tmp, &st);
|
|
|
|
t = tmp.tv_sec * hz + tmp.tv_usec / tick;
|
|
|
|
if (t)
|
|
|
|
acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
|
|
|
|
else
|
|
|
|
acct.ac_mem = 0;
|
|
|
|
|
|
|
|
/* (5) The number of disk I/O operations done */
|
|
|
|
acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
|
|
|
|
|
|
|
|
/* (6) The UID and GID of the process */
|
o Merge contents of struct pcred into struct ucred. Specifically, add the
real uid, saved uid, real gid, and saved gid to ucred, as well as the
pcred->pc_uidinfo, which was associated with the real uid, only rename
it to cr_ruidinfo so as not to conflict with cr_uidinfo, which
corresponds to the effective uid.
o Remove p_cred from struct proc; add p_ucred to struct proc, replacing
original macro that pointed.
p->p_ucred to p->p_cred->pc_ucred.
o Universally update code so that it makes use of ucred instead of pcred,
p->p_ucred instead of p->p_pcred, cr_ruidinfo instead of p_uidinfo,
cr_{r,sv}{u,g}id instead of p_*, etc.
o Remove pcred0 and its initialization from init_main.c; initialize
cr_ruidinfo there.
o Restruction many credential modification chunks to always crdup while
we figure out locking and optimizations; generally speaking, this
means moving to a structure like this:
newcred = crdup(oldcred);
...
p->p_ucred = newcred;
crfree(oldcred);
It's not race-free, but better than nothing. There are also races
in sys_process.c, all inter-process authorization, fork, exec, and
exit.
o Remove sigio->sio_ruid since sigio->sio_ucred now contains the ruid;
remove comments indicating that the old arrangement was a problem.
o Restructure exec1() a little to use newcred/oldcred arrangement, and
use improved uid management primitives.
o Clean up exit1() so as to do less work in credential cleanup due to
pcred removal.
o Clean up fork1() so as to do less work in credential cleanup and
allocation.
o Clean up ktrcanset() to take into account changes, and move to using
suser_xxx() instead of performing a direct uid==0 comparision.
o Improve commenting in various kern_prot.c credential modification
calls to better document current behavior. In a couple of places,
current behavior is a little questionable and we need to check
POSIX.1 to make sure it's "right". More commenting work still
remains to be done.
o Update credential management calls, such as crfree(), to take into
account new ruidinfo reference.
o Modify or add the following uid and gid helper routines:
change_euid()
change_egid()
change_ruid()
change_rgid()
change_svuid()
change_svgid()
In each case, the call now acts on a credential not a process, and as
such no longer requires more complicated process locking/etc. They
now assume the caller will do any necessary allocation of an
exclusive credential reference. Each is commented to document its
reference requirements.
o CANSIGIO() is simplified to require only credentials, not processes
and pcreds.
o Remove lots of (p_pcred==NULL) checks.
o Add an XXX to authorization code in nfs_lock.c, since it's
questionable, and needs to be considered carefully.
o Simplify posix4 authorization code to require only credentials, not
processes and pcreds. Note that this authorization, as well as
CANSIGIO(), needs to be updated to use the p_cansignal() and
p_cansched() centralized authorization routines, as they currently
do not take into account some desirable restrictions that are handled
by the centralized routines, as well as being inconsistent with other
similar authorization instances.
o Update libkvm to take these changes into account.
Obtained from: TrustedBSD Project
Reviewed by: green, bde, jhb, freebsd-arch, freebsd-audit
2001-05-25 16:59:11 +00:00
|
|
|
acct.ac_uid = p->p_ucred->cr_ruid;
|
|
|
|
acct.ac_gid = p->p_ucred->cr_rgid;
|
1994-09-26 21:09:15 +00:00
|
|
|
|
|
|
|
/* (7) The terminal from which the process was started */
|
2002-02-23 11:12:57 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
SESS_LOCK(p->p_session);
|
1994-09-26 21:09:15 +00:00
|
|
|
if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
|
1999-07-10 06:27:36 +00:00
|
|
|
acct.ac_tty = dev2udev(p->p_pgrp->pg_session->s_ttyp->t_dev);
|
1994-09-26 21:09:15 +00:00
|
|
|
else
|
1999-07-10 06:27:36 +00:00
|
|
|
acct.ac_tty = NOUDEV;
|
2002-02-23 11:12:57 +00:00
|
|
|
SESS_UNLOCK(p->p_session);
|
|
|
|
PROC_UNLOCK(p);
|
1994-09-26 21:09:15 +00:00
|
|
|
|
|
|
|
/* (8) The boolean flags that tell how the process terminated, etc. */
|
|
|
|
acct.ac_flag = p->p_acflag;
|
|
|
|
|
|
|
|
/*
|
1998-06-05 21:44:20 +00:00
|
|
|
* Eliminate any file size rlimit.
|
|
|
|
*/
|
|
|
|
if (p->p_limit->p_refcnt > 1 &&
|
|
|
|
(p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
|
|
|
|
p->p_limit->p_refcnt--;
|
|
|
|
p->p_limit = limcopy(p->p_limit);
|
|
|
|
}
|
|
|
|
p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the accounting information to the file.
|
1994-09-26 21:09:15 +00:00
|
|
|
*/
|
2002-08-07 19:30:16 +00:00
|
|
|
VOP_LEASE(vp, td, acctcred, LEASE_WRITE);
|
2001-01-27 00:01:31 +00:00
|
|
|
return (vn_rdwr(UIO_WRITE, vp, (caddr_t)&acct, sizeof (acct),
|
In order to better support flexible and extensible access control,
make a series of modifications to the credential arguments relating
to file read and write operations to cliarfy which credential is
used for what:
- Change fo_read() and fo_write() to accept "active_cred" instead of
"cred", and change the semantics of consumers of fo_read() and
fo_write() to pass the active credential of the thread requesting
an operation rather than the cached file cred. The cached file
cred is still available in fo_read() and fo_write() consumers
via fp->f_cred. These changes largely in sys_generic.c.
For each implementation of fo_read() and fo_write(), update cred
usage to reflect this change and maintain current semantics:
- badfo_readwrite() unchanged
- kqueue_read/write() unchanged
pipe_read/write() now authorize MAC using active_cred rather
than td->td_ucred
- soo_read/write() unchanged
- vn_read/write() now authorize MAC using active_cred but
VOP_READ/WRITE() with fp->f_cred
Modify vn_rdwr() to accept two credential arguments instead of a
single credential: active_cred and file_cred. Use active_cred
for MAC authorization, and select a credential for use in
VOP_READ/WRITE() based on whether file_cred is NULL or not. If
file_cred is provided, authorize the VOP using that cred,
otherwise the active credential, matching current semantics.
Modify current vn_rdwr() consumers to pass a file_cred if used
in the context of a struct file, and to always pass active_cred.
When vn_rdwr() is used without a file_cred, pass NOCRED.
These changes should maintain current semantics for read/write,
but avoid a redundant passing of fp->f_cred, as well as making
it more clear what the origin of each credential is in file
descriptor read/write operations.
Follow-up commits will make similar changes to other file descriptor
operations, and modify the MAC framework to pass both credentials
to MAC policy modules so they can implement either semantic for
revocation.
Obtained from: TrustedBSD Project
Sponsored by: DARPA, NAI Labs
2002-08-15 20:55:08 +00:00
|
|
|
(off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acctcred, NOCRED,
|
2001-09-12 08:38:13 +00:00
|
|
|
(int *)0, td));
|
1994-09-26 21:09:15 +00:00
|
|
|
}
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
1994-09-26 21:09:15 +00:00
|
|
|
* Encode_comp_t converts from ticks in seconds and microseconds
|
|
|
|
* to ticks in 1/AHZ seconds. The encoding is described in
|
|
|
|
* Leffler, et al., on page 63.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
1994-09-26 21:09:15 +00:00
|
|
|
|
|
|
|
#define MANTSIZE 13 /* 13 bit mantissa. */
|
|
|
|
#define EXPSIZE 3 /* Base 8 (3 bit) exponent. */
|
|
|
|
#define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */
|
|
|
|
|
1995-12-14 08:32:45 +00:00
|
|
|
static comp_t
|
1994-09-26 21:09:15 +00:00
|
|
|
encode_comp_t(s, us)
|
|
|
|
u_long s, us;
|
|
|
|
{
|
|
|
|
int exp, rnd;
|
|
|
|
|
|
|
|
exp = 0;
|
|
|
|
rnd = 0;
|
|
|
|
s *= AHZ;
|
|
|
|
s += us / (1000000 / AHZ); /* Maximize precision. */
|
|
|
|
|
|
|
|
while (s > MAXFRACT) {
|
|
|
|
rnd = s & (1 << (EXPSIZE - 1)); /* Round up? */
|
|
|
|
s >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */
|
|
|
|
exp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we need to round up, do it (and handle overflow correctly). */
|
|
|
|
if (rnd && (++s > MAXFRACT)) {
|
|
|
|
s >>= EXPSIZE;
|
|
|
|
exp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean it up and polish it off. */
|
|
|
|
exp <<= MANTSIZE; /* Shift the exponent into place */
|
|
|
|
exp += s; /* and add on the mantissa. */
|
|
|
|
return (exp);
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
/*
|
2002-05-16 21:28:32 +00:00
|
|
|
* Periodically check the filesystem to see if accounting
|
1994-09-26 21:09:15 +00:00
|
|
|
* should be turned on or off. Beware the case where the vnode
|
|
|
|
* has been vgone()'d out from underneath us, e.g. when the file
|
|
|
|
* system containing the accounting file has been forcibly unmounted.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
1995-12-14 08:32:45 +00:00
|
|
|
static void
|
1994-05-24 10:09:53 +00:00
|
|
|
acctwatch(a)
|
|
|
|
void *a;
|
|
|
|
{
|
|
|
|
struct statfs sb;
|
|
|
|
|
1994-09-26 21:09:15 +00:00
|
|
|
if (savacctp != NULLVP) {
|
|
|
|
if (savacctp->v_type == VBAD) {
|
2002-08-07 19:30:16 +00:00
|
|
|
(void) vn_close(savacctp, savacctflags, savacctcred,
|
|
|
|
NULL);
|
1994-09-26 21:09:15 +00:00
|
|
|
savacctp = NULLVP;
|
2002-08-07 19:30:16 +00:00
|
|
|
savacctcred = NOCRED;
|
1994-09-26 21:09:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
(void)VFS_STATFS(savacctp->v_mount, &sb, (struct thread *)0);
|
1994-05-24 10:09:53 +00:00
|
|
|
if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
|
|
|
|
acctp = savacctp;
|
2002-08-07 19:30:16 +00:00
|
|
|
acctcred = savacctcred;
|
2002-07-21 15:22:56 +00:00
|
|
|
acctflags = savacctflags;
|
1994-09-26 21:09:15 +00:00
|
|
|
savacctp = NULLVP;
|
2002-08-07 19:30:16 +00:00
|
|
|
savacctcred = NOCRED;
|
1994-05-24 10:09:53 +00:00
|
|
|
log(LOG_NOTICE, "Accounting resumed\n");
|
|
|
|
}
|
1997-02-10 02:22:35 +00:00
|
|
|
} else {
|
|
|
|
if (acctp == NULLVP)
|
|
|
|
return;
|
1994-09-26 21:09:15 +00:00
|
|
|
if (acctp->v_type == VBAD) {
|
2002-08-07 19:30:16 +00:00
|
|
|
(void) vn_close(acctp, acctflags, acctcred, NULL);
|
1994-09-26 21:09:15 +00:00
|
|
|
acctp = NULLVP;
|
2002-08-07 19:30:16 +00:00
|
|
|
crfree(acctcred);
|
|
|
|
acctcred = NOCRED;
|
1994-05-24 10:09:53 +00:00
|
|
|
return;
|
1994-09-26 21:09:15 +00:00
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
(void)VFS_STATFS(acctp->v_mount, &sb, (struct thread *)0);
|
1994-05-24 10:09:53 +00:00
|
|
|
if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
|
|
|
|
savacctp = acctp;
|
2002-07-21 15:22:56 +00:00
|
|
|
savacctflags = acctflags;
|
1994-09-26 21:09:15 +00:00
|
|
|
acctp = NULLVP;
|
2002-08-07 19:30:16 +00:00
|
|
|
acctcred = NOCRED;
|
1994-05-24 10:09:53 +00:00
|
|
|
log(LOG_NOTICE, "Accounting suspended\n");
|
|
|
|
}
|
1997-02-10 02:22:35 +00:00
|
|
|
}
|
2000-11-27 22:52:31 +00:00
|
|
|
callout_reset(&acctwatch_callout, acctchkfreq * hz, acctwatch, NULL);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|