LCOV - code coverage report
Current view: top level - kern - kern_acct.c (source / functions) Hit Total Coverage
Test: 6.4 Lines: 0 116 0.0 %
Date: 2018-10-19 03:25:38 Functions: 0 6 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*      $OpenBSD: kern_acct.c,v 1.36 2018/04/28 03:13:04 visa Exp $     */
       2             : /*      $NetBSD: kern_acct.c,v 1.42 1996/02/04 02:15:12 christos Exp $  */
       3             : 
       4             : /*-
       5             :  * Copyright (c) 1994 Christopher G. Demetriou
       6             :  * Copyright (c) 1982, 1986, 1989, 1993
       7             :  *      The Regents of the University of California.  All rights reserved.
       8             :  * (c) UNIX System Laboratories, Inc.
       9             :  * All or some portions of this file are derived from material licensed
      10             :  * to the University of California by American Telephone and Telegraph
      11             :  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
      12             :  * the permission of UNIX System Laboratories, Inc.
      13             :  *
      14             :  * Redistribution and use in source and binary forms, with or without
      15             :  * modification, are permitted provided that the following conditions
      16             :  * are met:
      17             :  * 1. Redistributions of source code must retain the above copyright
      18             :  *    notice, this list of conditions and the following disclaimer.
      19             :  * 2. Redistributions in binary form must reproduce the above copyright
      20             :  *    notice, this list of conditions and the following disclaimer in the
      21             :  *    documentation and/or other materials provided with the distribution.
      22             :  * 3. Neither the name of the University nor the names of its contributors
      23             :  *    may be used to endorse or promote products derived from this software
      24             :  *    without specific prior written permission.
      25             :  *
      26             :  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
      27             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      28             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      29             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
      30             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      31             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      32             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      33             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      34             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      35             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      36             :  * SUCH DAMAGE.
      37             :  *
      38             :  *      @(#)kern_acct.c 8.1 (Berkeley) 6/14/93
      39             :  */
      40             : 
      41             : #include <sys/param.h>
      42             : #include <sys/systm.h>
      43             : #include <sys/proc.h>
      44             : #include <sys/mount.h>
      45             : #include <sys/vnode.h>
      46             : #include <sys/fcntl.h>
      47             : #include <sys/syslog.h>
      48             : #include <sys/kernel.h>
      49             : #include <sys/namei.h>
      50             : #include <sys/errno.h>
      51             : #include <sys/acct.h>
      52             : #include <sys/resourcevar.h>
      53             : #include <sys/ioctl.h>
      54             : #include <sys/tty.h>
      55             : #include <sys/kthread.h>
      56             : 
      57             : #include <sys/syscallargs.h>
      58             : 
      59             : /*
      60             :  * The routines implemented in this file are described in:
      61             :  *      Leffler, et al.: The Design and Implementation of the 4.3BSD
      62             :  *          UNIX Operating System (Addison Welley, 1989)
      63             :  * on pages 62-63.
      64             :  *
      65             :  * Arguably, to simplify accounting operations, this mechanism should
      66             :  * be replaced by one in which an accounting log file (similar to /dev/klog)
      67             :  * is read by a user process, etc.  However, that has its own problems.
      68             :  */
      69             : 
      70             : /*
      71             :  * Internal accounting functions.
      72             :  */
      73             : comp_t  encode_comp_t(u_long, u_long);
      74             : int     acct_start(void);
      75             : void    acct_thread(void *);
      76             : void    acct_shutdown(void);
      77             : 
      78             : /*
      79             :  * Accounting vnode pointer, and saved vnode pointer.
      80             :  */
      81             : struct  vnode *acctp;
      82             : struct  vnode *savacctp;
      83             : 
      84             : /*
      85             :  * Values associated with enabling and disabling accounting
      86             :  */
      87             : int     acctsuspend = 2;        /* stop accounting when < 2% free space left */
      88             : int     acctresume = 4;         /* resume when free space risen to > 4% */
      89             : int     acctchkfreq = 15;       /* frequency (in seconds) to check space */
      90             : 
      91             : struct proc *acct_proc;
      92             : 
      93             : /*
      94             :  * Accounting system call.  Written based on the specification and
      95             :  * previous implementation done by Mark Tinguely.
      96             :  */
      97             : int
      98           0 : sys_acct(struct proc *p, void *v, register_t *retval)
      99             : {
     100             :         struct sys_acct_args /* {
     101             :                 syscallarg(const char *) path;
     102           0 :         } */ *uap = v;
     103           0 :         struct nameidata nd;
     104             :         int error;
     105             : 
     106             :         /* Make sure that the caller is root. */
     107           0 :         if ((error = suser(p)) != 0)
     108           0 :                 return (error);
     109             : 
     110             :         /*
     111             :          * If accounting is to be started to a file, open that file for
     112             :          * writing and make sure it's 'normal'.
     113             :          */
     114           0 :         if (SCARG(uap, path) != NULL) {
     115           0 :                 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path),
     116             :                     p);
     117           0 :                 if ((error = vn_open(&nd, FWRITE|O_APPEND, 0)) != 0)
     118           0 :                         return (error);
     119           0 :                 VOP_UNLOCK(nd.ni_vp);
     120           0 :                 if (nd.ni_vp->v_type != VREG) {
     121           0 :                         vn_close(nd.ni_vp, FWRITE, p->p_ucred, p);
     122           0 :                         return (EACCES);
     123             :                 }
     124             :         }
     125             : 
     126             :         /*
     127             :          * If accounting was previously enabled, kill the old space-watcher,
     128             :          * close the file, and (if no new file was specified, leave).
     129             :          */
     130           0 :         if (acctp != NULL || savacctp != NULL) {
     131           0 :                 wakeup(&acct_proc);
     132           0 :                 error = vn_close((acctp != NULL ? acctp : savacctp), FWRITE,
     133           0 :                     p->p_ucred, p);
     134           0 :                 acctp = savacctp = NULL;
     135           0 :         }
     136           0 :         if (SCARG(uap, path) == NULL)
     137           0 :                 return (0);
     138             : 
     139             :         /*
     140             :          * Save the new accounting file vnode, and schedule the new
     141             :          * free space watcher.
     142             :          */
     143           0 :         acctp = nd.ni_vp;
     144           0 :         if ((error = acct_start()) != 0) {
     145           0 :                 acctp = NULL;
     146           0 :                 (void)vn_close(nd.ni_vp, FWRITE, p->p_ucred, p);
     147           0 :                 return (error);
     148             :         }
     149           0 :         return (0);
     150           0 : }
     151             : 
     152             : /*
     153             :  * Write out process accounting information, on process exit.
     154             :  * Data to be written out is specified in Leffler, et al.
     155             :  * and are enumerated below.  (They're also noted in the system
     156             :  * "acct.h" header file.)
     157             :  */
     158             : int
     159           0 : acct_process(struct proc *p)
     160             : {
     161           0 :         struct acct acct;
     162           0 :         struct process *pr = p->p_p;
     163             :         struct rusage *r;
     164           0 :         struct timespec ut, st, tmp;
     165             :         int t;
     166             :         struct vnode *vp;
     167             :         int error;
     168             : 
     169             :         /* If accounting isn't enabled, don't bother */
     170           0 :         vp = acctp;
     171           0 :         if (vp == NULL)
     172           0 :                 return (0);
     173             : 
     174             :         /*
     175             :          * Get process accounting information.
     176             :          */
     177             : 
     178             :         /* (1) The name of the command that ran */
     179           0 :         memcpy(acct.ac_comm, pr->ps_comm, sizeof acct.ac_comm);
     180             : 
     181             :         /* (2) The amount of user and system time that was used */
     182           0 :         calctsru(&pr->ps_tu, &ut, &st, NULL);
     183           0 :         acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_nsec);
     184           0 :         acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_nsec);
     185             : 
     186             :         /* (3) The elapsed time the command ran (and its starting time) */
     187           0 :         acct.ac_btime = pr->ps_start.tv_sec;
     188           0 :         getnanotime(&tmp);
     189           0 :         timespecsub(&tmp, &pr->ps_start, &tmp);
     190           0 :         acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_nsec);
     191             : 
     192             :         /* (4) The average amount of memory used */
     193           0 :         r = &p->p_ru;
     194           0 :         timespecadd(&ut, &st, &tmp);
     195           0 :         t = tmp.tv_sec * hz + tmp.tv_nsec / (1000 * tick);
     196           0 :         if (t)
     197           0 :                 acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
     198             :         else
     199           0 :                 acct.ac_mem = 0;
     200             : 
     201             :         /* (5) The number of disk I/O operations done */
     202           0 :         acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
     203             : 
     204             :         /* (6) The UID and GID of the process */
     205           0 :         acct.ac_uid = pr->ps_ucred->cr_ruid;
     206           0 :         acct.ac_gid = pr->ps_ucred->cr_rgid;
     207             : 
     208             :         /* (7) The terminal from which the process was started */
     209           0 :         if ((pr->ps_flags & PS_CONTROLT) &&
     210           0 :             pr->ps_pgrp->pg_session->s_ttyp)
     211           0 :                 acct.ac_tty = pr->ps_pgrp->pg_session->s_ttyp->t_dev;
     212             :         else
     213           0 :                 acct.ac_tty = NODEV;
     214             : 
     215             :         /* (8) The boolean flags that tell how the process terminated, etc. */
     216           0 :         acct.ac_flag = pr->ps_acflag;
     217             : 
     218             :         /*
     219             :          * Now, just write the accounting information to the file.
     220             :          */
     221           0 :         error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&acct, sizeof (acct),
     222             :             (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT|IO_NOLIMIT,
     223           0 :             p->p_ucred, NULL, p);
     224             : 
     225           0 :         return error;
     226           0 : }
     227             : 
     228             : /*
     229             :  * Encode_comp_t converts from ticks in seconds and microseconds
     230             :  * to ticks in 1/AHZ seconds.  The encoding is described in
     231             :  * Leffler, et al., on page 63.
     232             :  */
     233             : 
     234             : #define MANTSIZE        13                      /* 13 bit mantissa. */
     235             : #define EXPSIZE         3                       /* Base 8 (3 bit) exponent. */
     236             : #define MAXFRACT        ((1 << MANTSIZE) - 1)     /* Maximum fractional value. */
     237             : 
     238             : comp_t
     239           0 : encode_comp_t(u_long s, u_long ns)
     240             : {
     241             :         int exp, rnd;
     242             : 
     243             :         exp = 0;
     244             :         rnd = 0;
     245           0 :         s *= AHZ;
     246           0 :         s += ns / (1000000000 / AHZ);   /* Maximize precision. */
     247             : 
     248           0 :         while (s > MAXFRACT) {
     249           0 :         rnd = s & (1 << (EXPSIZE - 1));       /* Round up? */
     250           0 :                 s >>= EXPSIZE;            /* Base 8 exponent == 3 bit shift. */
     251           0 :                 exp++;
     252             :         }
     253             : 
     254             :         /* If we need to round up, do it (and handle overflow correctly). */
     255           0 :         if (rnd && (++s > MAXFRACT)) {
     256           0 :                 s >>= EXPSIZE;
     257           0 :                 exp++;
     258           0 :         }
     259             : 
     260             :         /* Clean it up and polish it off. */
     261           0 :         exp <<= MANTSIZE;         /* Shift the exponent into place */
     262           0 :         exp += s;                       /* and add on the mantissa. */
     263           0 :         return (exp);
     264             : }
     265             : 
     266             : int
     267           0 : acct_start(void)
     268             : {
     269             :         /* Already running. */
     270           0 :         if (acct_proc != NULL)
     271           0 :                 return (0);
     272             : 
     273           0 :         return (kthread_create(acct_thread, NULL, &acct_proc, "acct"));
     274           0 : }
     275             : 
     276             : /*
     277             :  * Periodically check the file system to see if accounting
     278             :  * should be turned on or off.  Beware the case where the vnode
     279             :  * has been vgone()'d out from underneath us, e.g. when the file
     280             :  * system containing the accounting file has been forcibly unmounted.
     281             :  */
     282             : void
     283           0 : acct_thread(void *arg)
     284             : {
     285           0 :         struct statfs sb;
     286           0 :         struct proc *p = curproc;
     287             : 
     288           0 :         for (;;) {
     289           0 :                 if (savacctp != NULL) {
     290           0 :                         if (savacctp->v_type == VBAD) {
     291           0 :                                 (void) vn_close(savacctp, FWRITE, NOCRED, p);
     292           0 :                                 savacctp = NULL;
     293           0 :                                 break;
     294             :                         }
     295           0 :                         (void)VFS_STATFS(savacctp->v_mount, &sb, NULL);
     296           0 :                         if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
     297           0 :                                 acctp = savacctp;
     298           0 :                                 savacctp = NULL;
     299           0 :                                 log(LOG_NOTICE, "Accounting resumed\n");
     300           0 :                         }
     301           0 :                 } else if (acctp != NULL) {
     302           0 :                         if (acctp->v_type == VBAD) {
     303           0 :                                 (void) vn_close(acctp, FWRITE, NOCRED, p);
     304           0 :                                 acctp = NULL;
     305           0 :                                 break;
     306             :                         }
     307           0 :                         (void)VFS_STATFS(acctp->v_mount, &sb, NULL);
     308           0 :                         if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
     309           0 :                                 savacctp = acctp;
     310           0 :                                 acctp = NULL;
     311           0 :                                 log(LOG_NOTICE, "Accounting suspended\n");
     312           0 :                         }
     313             :                 } else {
     314             :                         break;
     315             :                 }
     316           0 :                 tsleep(&acct_proc, PPAUSE, "acct", acctchkfreq *hz);
     317             :         }
     318           0 :         acct_proc = NULL;
     319           0 :         kthread_exit(0);
     320             : }
     321             : 
     322             : void
     323           0 : acct_shutdown(void)
     324             : {
     325             : 
     326           0 :         struct proc *p = curproc;
     327             : 
     328           0 :         if (acctp != NULL || savacctp != NULL) {
     329           0 :                 vn_close((acctp != NULL ? acctp : savacctp), FWRITE,
     330             :                     NOCRED, p);
     331           0 :                 acctp = savacctp = NULL;
     332           0 :         }
     333           0 : }

Generated by: LCOV version 1.13