1 /* $OpenBSD: kern_acct.c,v 1.15 2004/06/24 19:35:24 tholo 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/file.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 sys_acct(p, v, retval) 99 struct proc *p; 100 void *v; 101 register_t *retval; 102 { 103 struct sys_acct_args /* { 104 syscallarg(const char *) path; 105 } */ *uap = v; 106 struct nameidata nd; 107 int error; 108 109 /* Make sure that the caller is root. */ 110 if ((error = suser(p, 0)) != 0) 111 return (error); 112 113 /* 114 * If accounting is to be started to a file, open that file for 115 * writing and make sure it's a 'normal'. 116 */ 117 if (SCARG(uap, path) != NULL) { 118 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), 119 p); 120 if ((error = vn_open(&nd, FWRITE|O_APPEND, 0)) != 0) 121 return (error); 122 VOP_UNLOCK(nd.ni_vp, 0, p); 123 if (nd.ni_vp->v_type != VREG) { 124 vn_close(nd.ni_vp, FWRITE, p->p_ucred, p); 125 return (EACCES); 126 } 127 } 128 129 /* 130 * If accounting was previously enabled, kill the old space-watcher, 131 * close the file, and (if no new file was specified, leave). 132 */ 133 if (acctp != NULL || savacctp != NULL) { 134 wakeup(&acct_proc); 135 error = vn_close((acctp != NULL ? acctp : savacctp), FWRITE, 136 p->p_ucred, p); 137 acctp = savacctp = NULL; 138 } 139 if (SCARG(uap, path) == NULL) 140 return (0); 141 142 /* 143 * Save the new accounting file vnode, and schedule the new 144 * free space watcher. 145 */ 146 acctp = nd.ni_vp; 147 if ((error = acct_start()) != 0) { 148 acctp = NULL; 149 (void)vn_close(nd.ni_vp, FWRITE, p->p_ucred, p); 150 return (error); 151 } 152 return (0); 153 } 154 155 /* 156 * Write out process accounting information, on process exit. 157 * Data to be written out is specified in Leffler, et al. 158 * and are enumerated below. (They're also noted in the system 159 * "acct.h" header file.) 160 */ 161 int 162 acct_process(struct proc *p) 163 { 164 struct acct acct; 165 struct rusage *r; 166 struct timeval ut, st, tmp; 167 int t; 168 struct vnode *vp; 169 struct plimit *oplim = NULL; 170 int error; 171 172 /* If accounting isn't enabled, don't bother */ 173 vp = acctp; 174 if (vp == NULL) 175 return (0); 176 177 /* 178 * Raise the file limit so that accounting can't be stopped by the 179 * user. (XXX - we should think about the cpu limit too). 180 */ 181 if (p->p_limit->p_refcnt > 1) { 182 oplim = p->p_limit; 183 p->p_limit = limcopy(p->p_limit); 184 } 185 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 186 187 /* 188 * Get process accounting information. 189 */ 190 191 /* (1) The name of the command that ran */ 192 bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm); 193 194 /* (2) The amount of user and system time that was used */ 195 calcru(p, &ut, &st, NULL); 196 acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec); 197 acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec); 198 199 /* (3) The elapsed time the commmand ran (and its starting time) */ 200 acct.ac_btime = p->p_stats->p_start.tv_sec; 201 getmicrotime(&tmp); 202 timersub(&tmp, &p->p_stats->p_start, &tmp); 203 acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec); 204 205 /* (4) The average amount of memory used */ 206 r = &p->p_stats->p_ru; 207 timeradd(&ut, &st, &tmp); 208 t = tmp.tv_sec * hz + tmp.tv_usec / tick; 209 if (t) 210 acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t; 211 else 212 acct.ac_mem = 0; 213 214 /* (5) The number of disk I/O operations done */ 215 acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0); 216 217 /* (6) The UID and GID of the process */ 218 acct.ac_uid = p->p_cred->p_ruid; 219 acct.ac_gid = p->p_cred->p_rgid; 220 221 /* (7) The terminal from which the process was started */ 222 if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp) 223 acct.ac_tty = p->p_pgrp->pg_session->s_ttyp->t_dev; 224 else 225 acct.ac_tty = NODEV; 226 227 /* (8) The boolean flags that tell how the process terminated, etc. */ 228 acct.ac_flag = p->p_acflag; 229 230 /* 231 * Now, just write the accounting information to the file. 232 */ 233 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE); 234 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&acct, sizeof (acct), 235 (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, p->p_ucred, NULL, p); 236 237 if (oplim) { 238 limfree(p->p_limit); 239 p->p_limit = oplim; 240 } 241 242 return error; 243 } 244 245 /* 246 * Encode_comp_t converts from ticks in seconds and microseconds 247 * to ticks in 1/AHZ seconds. The encoding is described in 248 * Leffler, et al., on page 63. 249 */ 250 251 #define MANTSIZE 13 /* 13 bit mantissa. */ 252 #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */ 253 #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */ 254 255 comp_t 256 encode_comp_t(u_long s, u_long us) 257 { 258 int exp, rnd; 259 260 exp = 0; 261 rnd = 0; 262 s *= AHZ; 263 s += us / (1000000 / AHZ); /* Maximize precision. */ 264 265 while (s > MAXFRACT) { 266 rnd = s & (1 << (EXPSIZE - 1)); /* Round up? */ 267 s >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */ 268 exp++; 269 } 270 271 /* If we need to round up, do it (and handle overflow correctly). */ 272 if (rnd && (++s > MAXFRACT)) { 273 s >>= EXPSIZE; 274 exp++; 275 } 276 277 /* Clean it up and polish it off. */ 278 exp <<= MANTSIZE; /* Shift the exponent into place */ 279 exp += s; /* and add on the mantissa. */ 280 return (exp); 281 } 282 283 int 284 acct_start(void) 285 { 286 /* Already running. */ 287 if (acct_proc != NULL) 288 return (0); 289 290 return (kthread_create(acct_thread, NULL, &acct_proc, "acct")); 291 } 292 293 /* 294 * Periodically check the file system to see if accounting 295 * should be turned on or off. Beware the case where the vnode 296 * has been vgone()'d out from underneath us, e.g. when the file 297 * system containing the accounting file has been forcibly unmounted. 298 */ 299 /* ARGSUSED */ 300 void 301 acct_thread(void *arg) 302 { 303 struct statfs sb; 304 305 for (;;) { 306 if (savacctp != NULL) { 307 if (savacctp->v_type == VBAD) { 308 (void) vn_close(savacctp, FWRITE, NOCRED, NULL); 309 savacctp = NULL; 310 return; 311 } 312 (void)VFS_STATFS(savacctp->v_mount, &sb, (struct proc *)0); 313 if (sb.f_bavail > acctresume * sb.f_blocks / 100) { 314 acctp = savacctp; 315 savacctp = NULL; 316 log(LOG_NOTICE, "Accounting resumed\n"); 317 } 318 } else if (acctp != NULL) { 319 if (acctp->v_type == VBAD) { 320 (void) vn_close(acctp, FWRITE, NOCRED, NULL); 321 acctp = NULL; 322 return; 323 } 324 (void)VFS_STATFS(acctp->v_mount, &sb, (struct proc *)0); 325 if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) { 326 savacctp = acctp; 327 acctp = NULL; 328 log(LOG_NOTICE, "Accounting suspended\n"); 329 } 330 } else { 331 acct_proc = NULL; 332 kthread_exit(0); 333 } 334 tsleep(&acct_proc, PPAUSE, "acct", acctchkfreq *hz); 335 } 336 } 337 338 void 339 acct_shutdown(void) 340 { 341 342 if (acctp != NULL || savacctp != NULL) { 343 vn_close((acctp != NULL ? acctp : savacctp), FWRITE, 344 NOCRED, NULL); 345 acctp = savacctp = NULL; 346 } 347 } 348