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) 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: releng/9.2/sys/kern/kern_acct.c 252886 2013-07-06 12:56:30Z mjg $");
   72 
   73 #include <sys/param.h>
   74 #include <sys/systm.h>
   75 #include <sys/acct.h>
   76 #include <sys/fcntl.h>
   77 #include <sys/kernel.h>
   78 #include <sys/kthread.h>
   79 #include <sys/limits.h>
   80 #include <sys/lock.h>
   81 #include <sys/mount.h>
   82 #include <sys/mutex.h>
   83 #include <sys/namei.h>
   84 #include <sys/priv.h>
   85 #include <sys/proc.h>
   86 #include <sys/resourcevar.h>
   87 #include <sys/sched.h>
   88 #include <sys/sx.h>
   89 #include <sys/sysctl.h>
   90 #include <sys/sysent.h>
   91 #include <sys/syslog.h>
   92 #include <sys/sysproto.h>
   93 #include <sys/tty.h>
   94 #include <sys/vnode.h>
   95 
   96 #include <security/mac/mac_framework.h>
   97 
   98 /*
   99  * The routines implemented in this file are described in:
  100  *      Leffler, et al.: The Design and Implementation of the 4.3BSD
  101  *          UNIX Operating System (Addison Welley, 1989)
  102  * on pages 62-63.
  103  * On May 2007 the historic 3 bits base 8 exponent, 13 bit fraction
  104  * compt_t representation described in the above reference was replaced
  105  * with that of IEEE-754 floats.
  106  *
  107  * Arguably, to simplify accounting operations, this mechanism should
  108  * be replaced by one in which an accounting log file (similar to /dev/klog)
  109  * is read by a user process, etc.  However, that has its own problems.
  110  */
  111 
  112 /* Floating point definitions from <float.h>. */
  113 #define FLT_MANT_DIG    24              /* p */
  114 #define FLT_MAX_EXP     128             /* emax */
  115 
  116 /*
  117  * Internal accounting functions.
  118  * The former's operation is described in Leffler, et al., and the latter
  119  * was provided by UCB with the 4.4BSD-Lite release
  120  */
  121 static uint32_t encode_timeval(struct timeval);
  122 static uint32_t encode_long(long);
  123 static void     acctwatch(void);
  124 static void     acct_thread(void *);
  125 static int      acct_disable(struct thread *, int);
  126 
  127 /*
  128  * Accounting vnode pointer, saved vnode pointer, and flags for each.
  129  * acct_sx protects against changes to the active vnode and credentials
  130  * while accounting records are being committed to disk.
  131  */
  132 static int               acct_configured;
  133 static int               acct_suspended;
  134 static struct vnode     *acct_vp;
  135 static struct ucred     *acct_cred;
  136 static struct plimit    *acct_limit;
  137 static int               acct_flags;
  138 static struct sx         acct_sx;
  139 
  140 SX_SYSINIT(acct, &acct_sx, "acct_sx");
  141 
  142 /*
  143  * State of the accounting kthread.
  144  */
  145 static 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  */
  153 static int acctsuspend = 2;     /* stop accounting when < 2% free space left */
  154 SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW,
  155         &acctsuspend, 0, "percentage of free disk space below which accounting stops");
  156 
  157 static int acctresume = 4;      /* resume when free space risen to > 4% */
  158 SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW,
  159         &acctresume, 0, "percentage of free disk space above which accounting resumes");
  160 
  161 static int acctchkfreq = 15;    /* frequency (in seconds) to check space */
  162 
  163 static int
  164 sysctl_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 }
  182 SYSCTL_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 
  186 SYSCTL_INT(_kern, OID_AUTO, acct_configured, CTLFLAG_RD, &acct_configured, 0,
  187         "Accounting configured or not");
  188 
  189 SYSCTL_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  */
  196 int
  197 sys_acct(struct thread *td, struct acct_args *uap)
  198 {
  199         struct nameidata nd;
  200         int error, flags, i, vfslocked, replacing;
  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, NULL);
  215                 if (error)
  216                         return (error);
  217                 vfslocked = NDHASGIANT(&nd);
  218                 NDFREE(&nd, NDF_ONLY_PNBUF);
  219 #ifdef MAC
  220                 error = mac_system_check_acct(td->td_ucred, nd.ni_vp);
  221                 if (error) {
  222                         VOP_UNLOCK(nd.ni_vp, 0);
  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);
  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_system_check_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          * Don't log spurious disable/enable messages if we are
  251          * switching from one accounting file to another due to log
  252          * rotation.
  253          */
  254         replacing = (acct_vp != NULL && uap->path != NULL);
  255 
  256         /*
  257          * If accounting was previously enabled, kill the old space-watcher,
  258          * close the file, and (if no new file was specified, leave).  Reset
  259          * the suspended state regardless of whether accounting remains
  260          * enabled.
  261          */
  262         acct_suspended = 0;
  263         if (acct_vp != NULL) {
  264                 vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
  265                 error = acct_disable(td, !replacing);
  266                 VFS_UNLOCK_GIANT(vfslocked);
  267         }
  268         if (uap->path == NULL) {
  269                 if (acct_state & ACCT_RUNNING) {
  270                         acct_state |= ACCT_EXITREQ;
  271                         wakeup(&acct_state);
  272                 }
  273                 sx_xunlock(&acct_sx);
  274                 return (error);
  275         }
  276 
  277         /*
  278          * Create our own plimit object without limits. It will be assigned
  279          * to exiting processes.
  280          */
  281         acct_limit = lim_alloc();
  282         for (i = 0; i < RLIM_NLIMITS; i++)
  283                 acct_limit->pl_rlimit[i].rlim_cur =
  284                     acct_limit->pl_rlimit[i].rlim_max = RLIM_INFINITY;
  285 
  286         /*
  287          * Save the new accounting file vnode, and schedule the new
  288          * free space watcher.
  289          */
  290         acct_vp = nd.ni_vp;
  291         acct_cred = crhold(td->td_ucred);
  292         acct_flags = flags;
  293         if (acct_state & ACCT_RUNNING)
  294                 acct_state &= ~ACCT_EXITREQ;
  295         else {
  296                 /*
  297                  * Try to start up an accounting kthread.  We may start more
  298                  * than one, but if so the extras will commit suicide as
  299                  * soon as they start up.
  300                  */
  301                 error = kproc_create(acct_thread, NULL, NULL, 0, 0,
  302                     "accounting");
  303                 if (error) {
  304                         vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
  305                         (void) acct_disable(td, 0);
  306                         VFS_UNLOCK_GIANT(vfslocked);
  307                         sx_xunlock(&acct_sx);
  308                         log(LOG_NOTICE, "Unable to start accounting thread\n");
  309                         return (error);
  310                 }
  311         }
  312         acct_configured = 1;
  313         sx_xunlock(&acct_sx);
  314         if (!replacing)
  315                 log(LOG_NOTICE, "Accounting enabled\n");
  316         return (error);
  317 }
  318 
  319 /*
  320  * Disable currently in-progress accounting by closing the vnode, dropping
  321  * our reference to the credential, and clearing the vnode's flags.
  322  */
  323 static int
  324 acct_disable(struct thread *td, int logging)
  325 {
  326         int error;
  327 
  328         sx_assert(&acct_sx, SX_XLOCKED);
  329         error = vn_close(acct_vp, acct_flags, acct_cred, td);
  330         crfree(acct_cred);
  331         lim_free(acct_limit);
  332         acct_configured = 0;
  333         acct_vp = NULL;
  334         acct_cred = NULL;
  335         acct_flags = 0;
  336         if (logging)
  337                 log(LOG_NOTICE, "Accounting disabled\n");
  338         return (error);
  339 }
  340 
  341 /*
  342  * Write out process accounting information, on process exit.
  343  * Data to be written out is specified in Leffler, et al.
  344  * and are enumerated below.  (They're also noted in the system
  345  * "acct.h" header file.)
  346  */
  347 int
  348 acct_process(struct thread *td)
  349 {
  350         struct acctv2 acct;
  351         struct timeval ut, st, tmp;
  352         struct plimit *oldlim;
  353         struct proc *p;
  354         struct rusage ru;
  355         int t, ret, vfslocked;
  356 
  357         /*
  358          * Lockless check of accounting condition before doing the hard
  359          * work.
  360          */
  361         if (acct_vp == NULL || acct_suspended)
  362                 return (0);
  363 
  364         sx_slock(&acct_sx);
  365 
  366         /*
  367          * If accounting isn't enabled, don't bother.  Have to check again
  368          * once we own the lock in case we raced with disabling of accounting
  369          * by another thread.
  370          */
  371         if (acct_vp == NULL || acct_suspended) {
  372                 sx_sunlock(&acct_sx);
  373                 return (0);
  374         }
  375 
  376         p = td->td_proc;
  377 
  378         /*
  379          * Get process accounting information.
  380          */
  381 
  382         sx_slock(&proctree_lock);
  383         PROC_LOCK(p);
  384 
  385         /* (1) The terminal from which the process was started */
  386         if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
  387                 acct.ac_tty = tty_udev(p->p_pgrp->pg_session->s_ttyp);
  388         else
  389                 acct.ac_tty = NODEV;
  390         sx_sunlock(&proctree_lock);
  391 
  392         /* (2) The name of the command that ran */
  393         bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
  394 
  395         /* (3) The amount of user and system time that was used */
  396         rufetchcalc(p, &ru, &ut, &st);
  397         acct.ac_utime = encode_timeval(ut);
  398         acct.ac_stime = encode_timeval(st);
  399 
  400         /* (4) The elapsed time the command ran (and its starting time) */
  401         tmp = boottime;
  402         timevaladd(&tmp, &p->p_stats->p_start);
  403         acct.ac_btime = tmp.tv_sec;
  404         microuptime(&tmp);
  405         timevalsub(&tmp, &p->p_stats->p_start);
  406         acct.ac_etime = encode_timeval(tmp);
  407 
  408         /* (5) The average amount of memory used */
  409         tmp = ut;
  410         timevaladd(&tmp, &st);
  411         /* Convert tmp (i.e. u + s) into hz units to match ru_i*. */
  412         t = tmp.tv_sec * hz + tmp.tv_usec / tick;
  413         if (t)
  414                 acct.ac_mem = encode_long((ru.ru_ixrss + ru.ru_idrss +
  415                     + ru.ru_isrss) / t);
  416         else
  417                 acct.ac_mem = 0;
  418 
  419         /* (6) The number of disk I/O operations done */
  420         acct.ac_io = encode_long(ru.ru_inblock + ru.ru_oublock);
  421 
  422         /* (7) The UID and GID of the process */
  423         acct.ac_uid = p->p_ucred->cr_ruid;
  424         acct.ac_gid = p->p_ucred->cr_rgid;
  425 
  426         /* (8) The boolean flags that tell how the process terminated, etc. */
  427         acct.ac_flagx = p->p_acflag;
  428 
  429         /* Setup ancillary structure fields. */
  430         acct.ac_flagx |= ANVER;
  431         acct.ac_zero = 0;
  432         acct.ac_version = 2;
  433         acct.ac_len = acct.ac_len2 = sizeof(acct);
  434 
  435         /*
  436          * Eliminate rlimits (file size limit in particular).
  437          */
  438         oldlim = p->p_limit;
  439         p->p_limit = lim_hold(acct_limit);
  440         PROC_UNLOCK(p);
  441         lim_free(oldlim);
  442 
  443         /*
  444          * Write the accounting information to the file.
  445          */
  446         vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
  447         ret = vn_rdwr(UIO_WRITE, acct_vp, (caddr_t)&acct, sizeof (acct),
  448             (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, acct_cred, NOCRED,
  449             NULL, td);
  450         VFS_UNLOCK_GIANT(vfslocked);
  451         sx_sunlock(&acct_sx);
  452         return (ret);
  453 }
  454 
  455 /* FLOAT_CONVERSION_START (Regression testing; don't remove this line.) */
  456 
  457 /* Convert timevals and longs into IEEE-754 bit patterns. */
  458 
  459 /* Mantissa mask (MSB is implied, so subtract 1). */
  460 #define MANT_MASK ((1 << (FLT_MANT_DIG - 1)) - 1)
  461 
  462 /*
  463  * We calculate integer values to a precision of approximately
  464  * 28 bits.
  465  * This is high-enough precision to fill the 24 float bits
  466  * and low-enough to avoid overflowing the 32 int bits.
  467  */
  468 #define CALC_BITS 28
  469 
  470 /* log_2(1000000). */
  471 #define LOG2_1M 20
  472 
  473 /*
  474  * Convert the elements of a timeval into a 32-bit word holding
  475  * the bits of a IEEE-754 float.
  476  * The float value represents the timeval's value in microsecond units.
  477  */
  478 static uint32_t
  479 encode_timeval(struct timeval tv)
  480 {
  481         int log2_s;
  482         int val, exp;   /* Unnormalized value and exponent */
  483         int norm_exp;   /* Normalized exponent */
  484         int shift;
  485 
  486         /*
  487          * First calculate value and exponent to about CALC_BITS precision.
  488          * Note that the following conditionals have been ordered so that
  489          * the most common cases appear first.
  490          */
  491         if (tv.tv_sec == 0) {
  492                 if (tv.tv_usec == 0)
  493                         return (0);
  494                 exp = 0;
  495                 val = tv.tv_usec;
  496         } else {
  497                 /*
  498                  * Calculate the value to a precision of approximately
  499                  * CALC_BITS.
  500                  */
  501                 log2_s = fls(tv.tv_sec) - 1;
  502                 if (log2_s + LOG2_1M < CALC_BITS) {
  503                         exp = 0;
  504                         val = 1000000 * tv.tv_sec + tv.tv_usec;
  505                 } else {
  506                         exp = log2_s + LOG2_1M - CALC_BITS;
  507                         val = (unsigned int)(((uint64_t)1000000 * tv.tv_sec +
  508                             tv.tv_usec) >> exp);
  509                 }
  510         }
  511         /* Now normalize and pack the value into an IEEE-754 float. */
  512         norm_exp = fls(val) - 1;
  513         shift = FLT_MANT_DIG - norm_exp - 1;
  514 #ifdef ACCT_DEBUG
  515         printf("val=%d exp=%d shift=%d log2(val)=%d\n",
  516             val, exp, shift, norm_exp);
  517         printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
  518             ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
  519 #endif
  520         return (((FLT_MAX_EXP - 1 + exp + norm_exp) << (FLT_MANT_DIG - 1)) |
  521             ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
  522 }
  523 
  524 /*
  525  * Convert a non-negative long value into the bit pattern of
  526  * an IEEE-754 float value.
  527  */
  528 static uint32_t
  529 encode_long(long val)
  530 {
  531         int norm_exp;   /* Normalized exponent */
  532         int shift;
  533 
  534         if (val == 0)
  535                 return (0);
  536         if (val < 0) {
  537                 log(LOG_NOTICE,
  538                     "encode_long: negative value %ld in accounting record\n",
  539                     val);
  540                 val = LONG_MAX;
  541         }
  542         norm_exp = fls(val) - 1;
  543         shift = FLT_MANT_DIG - norm_exp - 1;
  544 #ifdef ACCT_DEBUG
  545         printf("val=%d shift=%d log2(val)=%d\n",
  546             val, shift, norm_exp);
  547         printf("exp=%x mant=%x\n", FLT_MAX_EXP - 1 + exp + norm_exp,
  548             ((shift > 0 ? (val << shift) : (val >> -shift)) & MANT_MASK));
  549 #endif
  550         return (((FLT_MAX_EXP - 1 + norm_exp) << (FLT_MANT_DIG - 1)) |
  551             ((shift > 0 ? val << shift : val >> -shift) & MANT_MASK));
  552 }
  553 
  554 /* FLOAT_CONVERSION_END (Regression testing; don't remove this line.) */
  555 
  556 /*
  557  * Periodically check the filesystem to see if accounting
  558  * should be turned on or off.  Beware the case where the vnode
  559  * has been vgone()'d out from underneath us, e.g. when the file
  560  * system containing the accounting file has been forcibly unmounted.
  561  */
  562 /* ARGSUSED */
  563 static void
  564 acctwatch(void)
  565 {
  566         struct statfs sb;
  567         int vfslocked;
  568 
  569         sx_assert(&acct_sx, SX_XLOCKED);
  570 
  571         /*
  572          * If accounting was disabled before our kthread was scheduled,
  573          * then acct_vp might be NULL.  If so, just ask our kthread to
  574          * exit and return.
  575          */
  576         if (acct_vp == NULL) {
  577                 acct_state |= ACCT_EXITREQ;
  578                 return;
  579         }
  580 
  581         /*
  582          * If our vnode is no longer valid, tear it down and signal the
  583          * accounting thread to die.
  584          */
  585         vfslocked = VFS_LOCK_GIANT(acct_vp->v_mount);
  586         if (acct_vp->v_type == VBAD) {
  587                 (void) acct_disable(NULL, 1);
  588                 VFS_UNLOCK_GIANT(vfslocked);
  589                 acct_state |= ACCT_EXITREQ;
  590                 return;
  591         }
  592 
  593         /*
  594          * Stopping here is better than continuing, maybe it will be VBAD
  595          * next time around.
  596          */
  597         if (VFS_STATFS(acct_vp->v_mount, &sb) < 0) {
  598                 VFS_UNLOCK_GIANT(vfslocked);
  599                 return;
  600         }
  601         VFS_UNLOCK_GIANT(vfslocked);
  602         if (acct_suspended) {
  603                 if (sb.f_bavail > (int64_t)(acctresume * sb.f_blocks /
  604                     100)) {
  605                         acct_suspended = 0;
  606                         log(LOG_NOTICE, "Accounting resumed\n");
  607                 }
  608         } else {
  609                 if (sb.f_bavail <= (int64_t)(acctsuspend * sb.f_blocks /
  610                     100)) {
  611                         acct_suspended = 1;
  612                         log(LOG_NOTICE, "Accounting suspended\n");
  613                 }
  614         }
  615 }
  616 
  617 /*
  618  * The main loop for the dedicated kernel thread that periodically calls
  619  * acctwatch().
  620  */
  621 static void
  622 acct_thread(void *dummy)
  623 {
  624         u_char pri;
  625 
  626         /* This is a low-priority kernel thread. */
  627         pri = PRI_MAX_KERN;
  628         thread_lock(curthread);
  629         sched_prio(curthread, pri);
  630         thread_unlock(curthread);
  631 
  632         /* If another accounting kthread is already running, just die. */
  633         sx_xlock(&acct_sx);
  634         if (acct_state & ACCT_RUNNING) {
  635                 sx_xunlock(&acct_sx);
  636                 kproc_exit(0);
  637         }
  638         acct_state |= ACCT_RUNNING;
  639 
  640         /* Loop until we are asked to exit. */
  641         while (!(acct_state & ACCT_EXITREQ)) {
  642 
  643                 /* Perform our periodic checks. */
  644                 acctwatch();
  645 
  646                 /*
  647                  * We check this flag again before sleeping since the
  648                  * acctwatch() might have shut down accounting and asked us
  649                  * to exit.
  650                  */
  651                 if (!(acct_state & ACCT_EXITREQ)) {
  652                         sx_sleep(&acct_state, &acct_sx, 0, "-",
  653                             acctchkfreq * hz);
  654                 }
  655         }
  656 
  657         /*
  658          * Acknowledge the exit request and shutdown.  We clear both the
  659          * exit request and running flags.
  660          */
  661         acct_state = 0;
  662         sx_xunlock(&acct_sx);
  663         kproc_exit(0);
  664 }

Cache object: 45a842b70681c3355d128b6ecf2de8d9


[ 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.