The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_acct.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1994 Christopher G. Demetriou
    3  * Copyright (c) 1982, 1986, 1989, 1993
    4  *      The Regents of the University of California.  All rights reserved.
    5  * (c) UNIX System Laboratories, Inc.
    6  * All or some portions of this file are derived from material licensed
    7  * to the University of California by American Telephone and Telegraph
    8  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
    9  * the permission of UNIX System Laboratories, Inc.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. All advertising materials mentioning features or use of this software
   20  *    must display the following acknowledgement:
   21  *      This product includes software developed by the University of
   22  *      California, Berkeley and its contributors.
   23  * 4. Neither the name of the University nor the names of its contributors
   24  *    may be used to endorse or promote products derived from this software
   25  *    without specific prior written permission.
   26  *
   27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   37  * SUCH DAMAGE.
   38  *
   39  *      @(#)kern_acct.c 8.1 (Berkeley) 6/14/93
   40  */
   41 
   42 #include <sys/cdefs.h>
   43 __FBSDID("$FreeBSD: releng/5.2/sys/kern/kern_acct.c 118094 2003-07-27 17:04:56Z phk $");
   44 
   45 #include "opt_mac.h"
   46 
   47 #include <sys/param.h>
   48 #include <sys/systm.h>
   49 #include <sys/lock.h>
   50 #include <sys/mutex.h>
   51 #include <sys/sysproto.h>
   52 #include <sys/proc.h>
   53 #include <sys/mac.h>
   54 #include <sys/mount.h>
   55 #include <sys/vnode.h>
   56 #include <sys/fcntl.h>
   57 #include <sys/syslog.h>
   58 #include <sys/kernel.h>
   59 #include <sys/sysent.h>
   60 #include <sys/sysctl.h>
   61 #include <sys/namei.h>
   62 #include <sys/acct.h>
   63 #include <sys/resourcevar.h>
   64 #include <sys/tty.h>
   65 
   66 /*
   67  * The routines implemented in this file are described in:
   68  *      Leffler, et al.: The Design and Implementation of the 4.3BSD
   69  *          UNIX Operating System (Addison Welley, 1989)
   70  * on pages 62-63.
   71  *
   72  * Arguably, to simplify accounting operations, this mechanism should
   73  * be replaced by one in which an accounting log file (similar to /dev/klog)
   74  * is read by a user process, etc.  However, that has its own problems.
   75  */
   76 
   77 /*
   78  * Internal accounting functions.
   79  * The former's operation is described in Leffler, et al., and the latter
   80  * was provided by UCB with the 4.4BSD-Lite release
   81  */
   82 static comp_t   encode_comp_t(u_long, u_long);
   83 static void     acctwatch(void *);
   84 
   85 /*
   86  * Accounting callout used for periodic scheduling of acctwatch.
   87  */
   88 static struct   callout acctwatch_callout;
   89 
   90 /*
   91  * Accounting vnode pointer, saved vnode pointer, and flags for each.
   92  */
   93 static struct   vnode *acctp;
   94 static struct   ucred *acctcred;
   95 static int      acctflags;
   96 static struct   vnode *savacctp;
   97 static struct   ucred *savacctcred;
   98 static int      savacctflags;
   99 
  100 static struct mtx       acct_mtx;
  101 MTX_SYSINIT(acct, &acct_mtx, "accounting", MTX_DEF);
  102 
  103 /*
  104  * Values associated with enabling and disabling accounting
  105  */
  106 static int acctsuspend = 2;     /* stop accounting when < 2% free space left */
  107 SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW,
  108         &acctsuspend, 0, "percentage of free disk space below which accounting stops");
  109 
  110 static int acctresume = 4;      /* resume when free space risen to > 4% */
  111 SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW,
  112         &acctresume, 0, "percentage of free disk space above which accounting resumes");
  113 
  114 static int acctchkfreq = 15;    /* frequency (in seconds) to check space */
  115 SYSCTL_INT(_kern, OID_AUTO, acct_chkfreq, CTLFLAG_RW,
  116         &acctchkfreq, 0, "frequency for checking the free space");
  117 
  118 /*
  119  * Accounting system call.  Written based on the specification and
  120  * previous implementation done by Mark Tinguely.
  121  *
  122  * MPSAFE
  123  */
  124 int
  125 acct(td, uap)
  126         struct thread *td;
  127         struct acct_args /* {
  128                 char *path;
  129         } */ *uap;
  130 {
  131         struct nameidata nd;
  132         int error, flags;
  133 
  134         /* Make sure that the caller is root. */
  135         error = suser(td);
  136         if (error)
  137                 return (error);
  138 
  139         mtx_lock(&Giant);
  140         /*
  141          * If accounting is to be started to a file, open that file for
  142          * appending and make sure it's a 'normal'.
  143          */
  144         if (uap->path != NULL) {
  145                 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
  146                 flags = FWRITE | O_APPEND;
  147                 error = vn_open(&nd, &flags, 0, -1);
  148                 if (error)
  149                         goto done2;
  150                 NDFREE(&nd, NDF_ONLY_PNBUF);
  151 #ifdef MAC
  152                 error = mac_check_system_acct(td->td_ucred, nd.ni_vp);
  153                 if (error) {
  154                         vn_close(nd.ni_vp, flags, td->td_ucred, td);
  155                         goto done2;
  156                 }
  157 #endif
  158                 VOP_UNLOCK(nd.ni_vp, 0, td);
  159                 if (nd.ni_vp->v_type != VREG) {
  160                         vn_close(nd.ni_vp, flags, td->td_ucred, td);
  161                         error = EACCES;
  162                         goto done2;
  163                 }
  164 #ifdef MAC
  165         } else {
  166                 error = mac_check_system_acct(td->td_ucred, NULL);
  167                 if (error)
  168                         goto done2;
  169 #endif
  170         }
  171 
  172         /*
  173          * If accounting was previously enabled, kill the old space-watcher,
  174          * close the file, and (if no new file was specified, leave).
  175          */
  176 
  177         /*
  178          * XXX arr: Should not hold lock over vnode operation.
  179          */
  180 
  181         mtx_lock(&acct_mtx);
  182         if (acctp != NULLVP || savacctp != NULLVP) {
  183                 callout_stop(&acctwatch_callout);
  184                 error = vn_close((acctp != NULLVP ? acctp : savacctp),
  185                     (acctp != NULLVP ? acctflags : savacctflags),
  186                     (acctcred != NOCRED ? acctcred : savacctcred), td);
  187                 acctp = savacctp = NULLVP;
  188                 crfree(acctcred != NOCRED ? acctcred : savacctcred);
  189                 acctcred = savacctcred = NOCRED;
  190                 log(LOG_NOTICE, "Accounting disabled\n");
  191         }
  192         if (uap->path == NULL) {
  193                 mtx_unlock(&acct_mtx);
  194                 goto done2;
  195         }
  196 
  197         /*
  198          * Save the new accounting file vnode, and schedule the new
  199          * free space watcher.
  200          */
  201         acctp = nd.ni_vp;
  202         acctcred = crhold(td->td_ucred);
  203         acctflags = flags;
  204         callout_init(&acctwatch_callout, 0);
  205         mtx_unlock(&acct_mtx);
  206         log(LOG_NOTICE, "Accounting enabled\n");
  207         acctwatch(NULL);
  208 done2:
  209         mtx_unlock(&Giant);
  210         return (error);
  211 }
  212 
  213 /*
  214  * Write out process accounting information, on process exit.
  215  * Data to be written out is specified in Leffler, et al.
  216  * and are enumerated below.  (They're also noted in the system
  217  * "acct.h" header file.)
  218  */
  219 
  220 int
  221 acct_process(td)
  222         struct thread *td;
  223 {
  224         struct proc *p = td->td_proc;
  225         struct acct acct;
  226         struct rusage *r;
  227         struct timeval ut, st, tmp;
  228         int t, ret;
  229         struct vnode *vp;
  230         struct ucred *uc;
  231 
  232         mtx_lock(&acct_mtx);
  233 
  234         /* If accounting isn't enabled, don't bother */
  235         vp = acctp;
  236         if (vp == NULLVP) {
  237                 mtx_unlock(&acct_mtx);
  238                 return (0);
  239         }
  240 
  241         /*
  242          * Get process accounting information.
  243          */
  244 
  245         PROC_LOCK(p);
  246         /* (1) The name of the command that ran */
  247         bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
  248 
  249         /* (2) The amount of user and system time that was used */
  250         mtx_lock_spin(&sched_lock);
  251         calcru(p, &ut, &st, NULL);
  252         mtx_unlock_spin(&sched_lock);
  253         acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
  254         acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
  255 
  256         /* (3) The elapsed time the command ran (and its starting time) */
  257         tmp = boottime;
  258         timevaladd(&tmp, &p->p_stats->p_start);
  259         acct.ac_btime = tmp.tv_sec;
  260         microuptime(&tmp);
  261         timevalsub(&tmp, &p->p_stats->p_start);
  262         acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
  263 
  264         /* (4) The average amount of memory used */
  265         r = &p->p_stats->p_ru;
  266         tmp = ut;
  267         timevaladd(&tmp, &st);
  268         t = tmp.tv_sec * hz + tmp.tv_usec / tick;
  269         if (t)
  270                 acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
  271         else
  272                 acct.ac_mem = 0;
  273 
  274         /* (5) The number of disk I/O operations done */
  275         acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
  276 
  277         /* (6) The UID and GID of the process */
  278         acct.ac_uid = p->p_ucred->cr_ruid;
  279         acct.ac_gid = p->p_ucred->cr_rgid;
  280 
  281         /* (7) The terminal from which the process was started */
  282         SESS_LOCK(p->p_session);
  283         if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
  284                 acct.ac_tty = dev2udev(p->p_pgrp->pg_session->s_ttyp->t_dev);
  285         else
  286                 acct.ac_tty = NOUDEV;
  287         SESS_UNLOCK(p->p_session);
  288 
  289         /* (8) The boolean flags that tell how the process terminated, etc. */
  290         acct.ac_flag = p->p_acflag;
  291         PROC_UNLOCK(p);
  292 
  293         /*
  294          * Write the accounting information to the file.
  295          */
  296         uc = crhold(acctcred);
  297         vref(vp);
  298         mtx_unlock(&acct_mtx);
  299 
  300         /*
  301          * Eliminate any file size rlimit.
  302          */
  303         if (p->p_limit->p_refcnt > 1) {
  304                 p->p_limit->p_refcnt--;
  305                 p->p_limit = limcopy(p->p_limit);
  306         }
  307         p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
  308 
  309         VOP_LEASE(vp, td, uc, LEASE_WRITE);
  310         ret = vn_rdwr(UIO_WRITE, vp, (caddr_t)&acct, sizeof (acct),
  311             (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, uc, NOCRED,
  312             (int *)0, td);
  313         vrele(vp);
  314         crfree(uc);
  315         return (ret);
  316 }
  317 
  318 /*
  319  * Encode_comp_t converts from ticks in seconds and microseconds
  320  * to ticks in 1/AHZ seconds.  The encoding is described in
  321  * Leffler, et al., on page 63.
  322  */
  323 
  324 #define MANTSIZE        13                      /* 13 bit mantissa. */
  325 #define EXPSIZE         3                       /* Base 8 (3 bit) exponent. */
  326 #define MAXFRACT        ((1 << MANTSIZE) - 1)   /* Maximum fractional value. */
  327 
  328 static comp_t
  329 encode_comp_t(s, us)
  330         u_long s, us;
  331 {
  332         int exp, rnd;
  333 
  334         exp = 0;
  335         rnd = 0;
  336         s *= AHZ;
  337         s += us / (1000000 / AHZ);      /* Maximize precision. */
  338 
  339         while (s > MAXFRACT) {
  340         rnd = s & (1 << (EXPSIZE - 1)); /* Round up? */
  341                 s >>= EXPSIZE;          /* Base 8 exponent == 3 bit shift. */
  342                 exp++;
  343         }
  344 
  345         /* If we need to round up, do it (and handle overflow correctly). */
  346         if (rnd && (++s > MAXFRACT)) {
  347                 s >>= EXPSIZE;
  348                 exp++;
  349         }
  350 
  351         /* Clean it up and polish it off. */
  352         exp <<= MANTSIZE;               /* Shift the exponent into place */
  353         exp += s;                       /* and add on the mantissa. */
  354         return (exp);
  355 }
  356 
  357 /*
  358  * Periodically check the filesystem to see if accounting
  359  * should be turned on or off.  Beware the case where the vnode
  360  * has been vgone()'d out from underneath us, e.g. when the file
  361  * system containing the accounting file has been forcibly unmounted.
  362  */
  363 /* ARGSUSED */
  364 static void
  365 acctwatch(a)
  366         void *a;
  367 {
  368         struct statfs sb;
  369 
  370         mtx_lock(&acct_mtx);
  371 
  372         /*
  373          * XXX arr: Need to fix the issue of holding acct_mtx over
  374          * the below vnode operations.
  375          */
  376 
  377         if (savacctp != NULLVP) {
  378                 if (savacctp->v_type == VBAD) {
  379                         (void) vn_close(savacctp, savacctflags, savacctcred,
  380                             NULL);
  381                         savacctp = NULLVP;
  382                         savacctcred = NOCRED;
  383                         mtx_unlock(&acct_mtx);
  384                         return;
  385                 }
  386                 (void)VFS_STATFS(savacctp->v_mount, &sb, (struct thread *)0);
  387                 if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
  388                         acctp = savacctp;
  389                         acctcred = savacctcred;
  390                         acctflags = savacctflags;
  391                         savacctp = NULLVP;
  392                         savacctcred = NOCRED;
  393                         log(LOG_NOTICE, "Accounting resumed\n");
  394                 }
  395         } else {
  396                 if (acctp == NULLVP) {
  397                         mtx_unlock(&acct_mtx);
  398                         return;
  399                 }
  400                 if (acctp->v_type == VBAD) {
  401                         (void) vn_close(acctp, acctflags, acctcred, NULL);
  402                         acctp = NULLVP;
  403                         crfree(acctcred);
  404                         acctcred = NOCRED;
  405                         mtx_unlock(&acct_mtx);
  406                         return;
  407                 }
  408                 (void)VFS_STATFS(acctp->v_mount, &sb, (struct thread *)0);
  409                 if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
  410                         savacctp = acctp;
  411                         savacctflags = acctflags;
  412                         savacctcred = acctcred;
  413                         acctp = NULLVP;
  414                         acctcred = NOCRED;
  415                         log(LOG_NOTICE, "Accounting suspended\n");
  416                 }
  417         }
  418         callout_reset(&acctwatch_callout, acctchkfreq * hz, acctwatch, NULL);
  419         mtx_unlock(&acct_mtx);
  420 }

Cache object: e3259a13df305009e3458df61a9c34c2


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.