1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/systm.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/acct.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/cred.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/user.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/errno.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/file.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 43*7c478bd9Sstevel@tonic-gate #include <sys/debug.h> 44*7c478bd9Sstevel@tonic-gate #include <sys/proc.h> 45*7c478bd9Sstevel@tonic-gate #include <sys/resource.h> 46*7c478bd9Sstevel@tonic-gate #include <sys/session.h> 47*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 48*7c478bd9Sstevel@tonic-gate #include <sys/syscall.h> 49*7c478bd9Sstevel@tonic-gate #include <sys/policy.h> 50*7c478bd9Sstevel@tonic-gate #include <sys/list.h> 51*7c478bd9Sstevel@tonic-gate #include <sys/time.h> 52*7c478bd9Sstevel@tonic-gate #include <sys/msacct.h> 53*7c478bd9Sstevel@tonic-gate #include <sys/zone.h> 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate /* 56*7c478bd9Sstevel@tonic-gate * Each zone has its own accounting settings (on or off) and associated 57*7c478bd9Sstevel@tonic-gate * file. The global zone is not special in this aspect; it will only 58*7c478bd9Sstevel@tonic-gate * generate records for processes that ran in the global zone. We could 59*7c478bd9Sstevel@tonic-gate * allow the global zone to record all activity on the system, but there 60*7c478bd9Sstevel@tonic-gate * would be no way of knowing the zone in which the processes executed. 61*7c478bd9Sstevel@tonic-gate * sysacct() is thus virtualized to only act on the caller's zone. 62*7c478bd9Sstevel@tonic-gate */ 63*7c478bd9Sstevel@tonic-gate struct acct_globals { 64*7c478bd9Sstevel@tonic-gate struct acct acctbuf; 65*7c478bd9Sstevel@tonic-gate kmutex_t aclock; 66*7c478bd9Sstevel@tonic-gate struct vnode *acctvp; 67*7c478bd9Sstevel@tonic-gate list_node_t aclink; 68*7c478bd9Sstevel@tonic-gate }; 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate /* 71*7c478bd9Sstevel@tonic-gate * We need a list of all accounting settings for all zones, so we can 72*7c478bd9Sstevel@tonic-gate * accurately determine if a file is in use for accounting (possibly by 73*7c478bd9Sstevel@tonic-gate * another zone). 74*7c478bd9Sstevel@tonic-gate */ 75*7c478bd9Sstevel@tonic-gate static zone_key_t acct_zone_key; 76*7c478bd9Sstevel@tonic-gate static list_t acct_list; 77*7c478bd9Sstevel@tonic-gate kmutex_t acct_list_lock; 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate static struct sysent acctsysent = { 80*7c478bd9Sstevel@tonic-gate 1, 81*7c478bd9Sstevel@tonic-gate SE_NOUNLOAD | SE_ARGC | SE_32RVAL1, 82*7c478bd9Sstevel@tonic-gate sysacct 83*7c478bd9Sstevel@tonic-gate }; 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate static struct modlsys modlsys = { 86*7c478bd9Sstevel@tonic-gate &mod_syscallops, "acct(2) syscall", &acctsysent 87*7c478bd9Sstevel@tonic-gate }; 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 90*7c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = { 91*7c478bd9Sstevel@tonic-gate &mod_syscallops32, "32-bit acct(2) syscall", &acctsysent 92*7c478bd9Sstevel@tonic-gate }; 93*7c478bd9Sstevel@tonic-gate #endif 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 96*7c478bd9Sstevel@tonic-gate MODREV_1, 97*7c478bd9Sstevel@tonic-gate &modlsys, 98*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 99*7c478bd9Sstevel@tonic-gate &modlsys32, 100*7c478bd9Sstevel@tonic-gate #endif 101*7c478bd9Sstevel@tonic-gate NULL 102*7c478bd9Sstevel@tonic-gate }; 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 105*7c478bd9Sstevel@tonic-gate static void * 106*7c478bd9Sstevel@tonic-gate acct_init(zoneid_t zoneid) 107*7c478bd9Sstevel@tonic-gate { 108*7c478bd9Sstevel@tonic-gate struct acct_globals *ag; 109*7c478bd9Sstevel@tonic-gate 110*7c478bd9Sstevel@tonic-gate ag = kmem_alloc(sizeof (*ag), KM_SLEEP); 111*7c478bd9Sstevel@tonic-gate bzero(&ag->acctbuf, sizeof (ag->acctbuf)); 112*7c478bd9Sstevel@tonic-gate mutex_init(&ag->aclock, NULL, MUTEX_DEFAULT, NULL); 113*7c478bd9Sstevel@tonic-gate ag->acctvp = NULL; 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate mutex_enter(&acct_list_lock); 116*7c478bd9Sstevel@tonic-gate list_insert_tail(&acct_list, ag); 117*7c478bd9Sstevel@tonic-gate mutex_exit(&acct_list_lock); 118*7c478bd9Sstevel@tonic-gate return (ag); 119*7c478bd9Sstevel@tonic-gate } 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 122*7c478bd9Sstevel@tonic-gate static void 123*7c478bd9Sstevel@tonic-gate acct_shutdown(zoneid_t zoneid, void *arg) 124*7c478bd9Sstevel@tonic-gate { 125*7c478bd9Sstevel@tonic-gate struct acct_globals *ag = arg; 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate mutex_enter(&ag->aclock); 128*7c478bd9Sstevel@tonic-gate if (ag->acctvp) { 129*7c478bd9Sstevel@tonic-gate /* 130*7c478bd9Sstevel@tonic-gate * This needs to be done as a shutdown callback, otherwise this 131*7c478bd9Sstevel@tonic-gate * held vnode may cause filesystems to be busy, and the zone 132*7c478bd9Sstevel@tonic-gate * shutdown operation to fail. 133*7c478bd9Sstevel@tonic-gate */ 134*7c478bd9Sstevel@tonic-gate (void) VOP_CLOSE(ag->acctvp, FWRITE, 1, (offset_t)0, kcred); 135*7c478bd9Sstevel@tonic-gate VN_RELE(ag->acctvp); 136*7c478bd9Sstevel@tonic-gate } 137*7c478bd9Sstevel@tonic-gate ag->acctvp = NULL; 138*7c478bd9Sstevel@tonic-gate mutex_exit(&ag->aclock); 139*7c478bd9Sstevel@tonic-gate } 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 142*7c478bd9Sstevel@tonic-gate static void 143*7c478bd9Sstevel@tonic-gate acct_fini(zoneid_t zoneid, void *arg) 144*7c478bd9Sstevel@tonic-gate { 145*7c478bd9Sstevel@tonic-gate struct acct_globals *ag = arg; 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate mutex_enter(&acct_list_lock); 148*7c478bd9Sstevel@tonic-gate list_remove(&acct_list, ag); 149*7c478bd9Sstevel@tonic-gate mutex_exit(&acct_list_lock); 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate mutex_destroy(&ag->aclock); 152*7c478bd9Sstevel@tonic-gate kmem_free(ag, sizeof (*ag)); 153*7c478bd9Sstevel@tonic-gate } 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate int 156*7c478bd9Sstevel@tonic-gate _init(void) 157*7c478bd9Sstevel@tonic-gate { 158*7c478bd9Sstevel@tonic-gate int error; 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate mutex_init(&acct_list_lock, NULL, MUTEX_DEFAULT, NULL); 161*7c478bd9Sstevel@tonic-gate list_create(&acct_list, sizeof (struct acct_globals), 162*7c478bd9Sstevel@tonic-gate offsetof(struct acct_globals, aclink)); 163*7c478bd9Sstevel@tonic-gate /* 164*7c478bd9Sstevel@tonic-gate * Using an initializer here wastes a bit of memory for zones that 165*7c478bd9Sstevel@tonic-gate * don't use accounting, but vastly simplifies the locking. 166*7c478bd9Sstevel@tonic-gate */ 167*7c478bd9Sstevel@tonic-gate zone_key_create(&acct_zone_key, acct_init, acct_shutdown, acct_fini); 168*7c478bd9Sstevel@tonic-gate if ((error = mod_install(&modlinkage)) != 0) { 169*7c478bd9Sstevel@tonic-gate (void) zone_key_delete(acct_zone_key); 170*7c478bd9Sstevel@tonic-gate list_destroy(&acct_list); 171*7c478bd9Sstevel@tonic-gate mutex_destroy(&acct_list_lock); 172*7c478bd9Sstevel@tonic-gate } 173*7c478bd9Sstevel@tonic-gate return (error); 174*7c478bd9Sstevel@tonic-gate } 175*7c478bd9Sstevel@tonic-gate 176*7c478bd9Sstevel@tonic-gate int 177*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 178*7c478bd9Sstevel@tonic-gate { 179*7c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 180*7c478bd9Sstevel@tonic-gate } 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate /* 183*7c478bd9Sstevel@tonic-gate * acct() is a "weak stub" routine called from exit(). 184*7c478bd9Sstevel@tonic-gate * Once this module has been loaded, we refuse to allow 185*7c478bd9Sstevel@tonic-gate * it to unload - otherwise accounting would quietly 186*7c478bd9Sstevel@tonic-gate * cease. See 1211661. It's possible to make this module 187*7c478bd9Sstevel@tonic-gate * unloadable but it's substantially safer not to bother. 188*7c478bd9Sstevel@tonic-gate */ 189*7c478bd9Sstevel@tonic-gate int 190*7c478bd9Sstevel@tonic-gate _fini(void) 191*7c478bd9Sstevel@tonic-gate { 192*7c478bd9Sstevel@tonic-gate return (EBUSY); 193*7c478bd9Sstevel@tonic-gate } 194*7c478bd9Sstevel@tonic-gate 195*7c478bd9Sstevel@tonic-gate /* 196*7c478bd9Sstevel@tonic-gate * See if vp is in use by the accounting system on any zone. This does a deep 197*7c478bd9Sstevel@tonic-gate * comparison of vnodes such that a file and a lofs "shadow" node of it will 198*7c478bd9Sstevel@tonic-gate * appear to be the same. 199*7c478bd9Sstevel@tonic-gate * 200*7c478bd9Sstevel@tonic-gate * If 'compare_vfs' is true, the function will do a comparison of vfs_t's 201*7c478bd9Sstevel@tonic-gate * instead (ie, is the vfs_t on which the vnode resides in use by the 202*7c478bd9Sstevel@tonic-gate * accounting system in any zone). 203*7c478bd9Sstevel@tonic-gate * 204*7c478bd9Sstevel@tonic-gate * Returns 1 if found (in use), 0 otherwise. 205*7c478bd9Sstevel@tonic-gate */ 206*7c478bd9Sstevel@tonic-gate static int 207*7c478bd9Sstevel@tonic-gate acct_find(vnode_t *vp, boolean_t compare_vfs) 208*7c478bd9Sstevel@tonic-gate { 209*7c478bd9Sstevel@tonic-gate struct acct_globals *ag; 210*7c478bd9Sstevel@tonic-gate vnode_t *realvp; 211*7c478bd9Sstevel@tonic-gate 212*7c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&acct_list_lock)); 213*7c478bd9Sstevel@tonic-gate ASSERT(vp != NULL); 214*7c478bd9Sstevel@tonic-gate 215*7c478bd9Sstevel@tonic-gate if (VOP_REALVP(vp, &realvp)) 216*7c478bd9Sstevel@tonic-gate realvp = vp; 217*7c478bd9Sstevel@tonic-gate for (ag = list_head(&acct_list); ag != NULL; 218*7c478bd9Sstevel@tonic-gate ag = list_next(&acct_list, ag)) { 219*7c478bd9Sstevel@tonic-gate vnode_t *racctvp; 220*7c478bd9Sstevel@tonic-gate boolean_t found = B_FALSE; 221*7c478bd9Sstevel@tonic-gate 222*7c478bd9Sstevel@tonic-gate mutex_enter(&ag->aclock); 223*7c478bd9Sstevel@tonic-gate if (ag->acctvp == NULL) { 224*7c478bd9Sstevel@tonic-gate mutex_exit(&ag->aclock); 225*7c478bd9Sstevel@tonic-gate continue; 226*7c478bd9Sstevel@tonic-gate } 227*7c478bd9Sstevel@tonic-gate if (VOP_REALVP(ag->acctvp, &racctvp)) 228*7c478bd9Sstevel@tonic-gate racctvp = ag->acctvp; 229*7c478bd9Sstevel@tonic-gate if (compare_vfs) { 230*7c478bd9Sstevel@tonic-gate if (racctvp->v_vfsp == realvp->v_vfsp) 231*7c478bd9Sstevel@tonic-gate found = B_TRUE; 232*7c478bd9Sstevel@tonic-gate } else { 233*7c478bd9Sstevel@tonic-gate if (VN_CMP(realvp, racctvp)) 234*7c478bd9Sstevel@tonic-gate found = B_TRUE; 235*7c478bd9Sstevel@tonic-gate } 236*7c478bd9Sstevel@tonic-gate mutex_exit(&ag->aclock); 237*7c478bd9Sstevel@tonic-gate if (found) 238*7c478bd9Sstevel@tonic-gate return (1); 239*7c478bd9Sstevel@tonic-gate } 240*7c478bd9Sstevel@tonic-gate return (0); 241*7c478bd9Sstevel@tonic-gate } 242*7c478bd9Sstevel@tonic-gate 243*7c478bd9Sstevel@tonic-gate /* 244*7c478bd9Sstevel@tonic-gate * Returns 1 if the vfs that vnode resides on is in use for the accounting 245*7c478bd9Sstevel@tonic-gate * subsystem, 0 otherwise. 246*7c478bd9Sstevel@tonic-gate */ 247*7c478bd9Sstevel@tonic-gate int 248*7c478bd9Sstevel@tonic-gate acct_fs_in_use(vnode_t *vp) 249*7c478bd9Sstevel@tonic-gate { 250*7c478bd9Sstevel@tonic-gate int found; 251*7c478bd9Sstevel@tonic-gate 252*7c478bd9Sstevel@tonic-gate if (vp == NULL) 253*7c478bd9Sstevel@tonic-gate return (0); 254*7c478bd9Sstevel@tonic-gate mutex_enter(&acct_list_lock); 255*7c478bd9Sstevel@tonic-gate found = acct_find(vp, B_TRUE); 256*7c478bd9Sstevel@tonic-gate mutex_exit(&acct_list_lock); 257*7c478bd9Sstevel@tonic-gate return (found); 258*7c478bd9Sstevel@tonic-gate } 259*7c478bd9Sstevel@tonic-gate 260*7c478bd9Sstevel@tonic-gate /* 261*7c478bd9Sstevel@tonic-gate * Perform process accounting functions. 262*7c478bd9Sstevel@tonic-gate */ 263*7c478bd9Sstevel@tonic-gate int 264*7c478bd9Sstevel@tonic-gate sysacct(char *fname) 265*7c478bd9Sstevel@tonic-gate { 266*7c478bd9Sstevel@tonic-gate struct acct_globals *ag; 267*7c478bd9Sstevel@tonic-gate struct vnode *vp; 268*7c478bd9Sstevel@tonic-gate int error = 0; 269*7c478bd9Sstevel@tonic-gate 270*7c478bd9Sstevel@tonic-gate if (secpolicy_acct(CRED()) != 0) 271*7c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 272*7c478bd9Sstevel@tonic-gate 273*7c478bd9Sstevel@tonic-gate ag = zone_getspecific(acct_zone_key, curproc->p_zone); 274*7c478bd9Sstevel@tonic-gate ASSERT(ag != NULL); 275*7c478bd9Sstevel@tonic-gate 276*7c478bd9Sstevel@tonic-gate if (fname == NULL) { 277*7c478bd9Sstevel@tonic-gate /* 278*7c478bd9Sstevel@tonic-gate * Close the file and stop accounting. 279*7c478bd9Sstevel@tonic-gate */ 280*7c478bd9Sstevel@tonic-gate mutex_enter(&ag->aclock); 281*7c478bd9Sstevel@tonic-gate vp = ag->acctvp; 282*7c478bd9Sstevel@tonic-gate ag->acctvp = NULL; 283*7c478bd9Sstevel@tonic-gate mutex_exit(&ag->aclock); 284*7c478bd9Sstevel@tonic-gate if (vp) { 285*7c478bd9Sstevel@tonic-gate error = VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED()); 286*7c478bd9Sstevel@tonic-gate VN_RELE(vp); 287*7c478bd9Sstevel@tonic-gate } 288*7c478bd9Sstevel@tonic-gate return (error == 0 ? 0 : set_errno(error)); 289*7c478bd9Sstevel@tonic-gate } 290*7c478bd9Sstevel@tonic-gate 291*7c478bd9Sstevel@tonic-gate /* 292*7c478bd9Sstevel@tonic-gate * Either (a) open a new file and begin accounting -or- (b) 293*7c478bd9Sstevel@tonic-gate * switch accounting from an old to a new file. 294*7c478bd9Sstevel@tonic-gate * 295*7c478bd9Sstevel@tonic-gate * (Open the file without holding aclock in case it 296*7c478bd9Sstevel@tonic-gate * sleeps (holding the lock prevents process exit).) 297*7c478bd9Sstevel@tonic-gate */ 298*7c478bd9Sstevel@tonic-gate if ((error = vn_open(fname, UIO_USERSPACE, FWRITE, 299*7c478bd9Sstevel@tonic-gate 0, &vp, (enum create)0, 0)) != 0) { 300*7c478bd9Sstevel@tonic-gate /* SVID compliance */ 301*7c478bd9Sstevel@tonic-gate if (error == EISDIR) 302*7c478bd9Sstevel@tonic-gate error = EACCES; 303*7c478bd9Sstevel@tonic-gate return (set_errno(error)); 304*7c478bd9Sstevel@tonic-gate } 305*7c478bd9Sstevel@tonic-gate 306*7c478bd9Sstevel@tonic-gate if (vp->v_type != VREG) { 307*7c478bd9Sstevel@tonic-gate error = EACCES; 308*7c478bd9Sstevel@tonic-gate } else { 309*7c478bd9Sstevel@tonic-gate mutex_enter(&acct_list_lock); 310*7c478bd9Sstevel@tonic-gate if (acct_find(vp, B_FALSE)) { 311*7c478bd9Sstevel@tonic-gate error = EBUSY; 312*7c478bd9Sstevel@tonic-gate } else { 313*7c478bd9Sstevel@tonic-gate mutex_enter(&ag->aclock); 314*7c478bd9Sstevel@tonic-gate if (ag->acctvp) { 315*7c478bd9Sstevel@tonic-gate vnode_t *oldvp; 316*7c478bd9Sstevel@tonic-gate 317*7c478bd9Sstevel@tonic-gate /* 318*7c478bd9Sstevel@tonic-gate * close old acctvp, and point acct() 319*7c478bd9Sstevel@tonic-gate * at new file by swapping vp and acctvp 320*7c478bd9Sstevel@tonic-gate */ 321*7c478bd9Sstevel@tonic-gate oldvp = ag->acctvp; 322*7c478bd9Sstevel@tonic-gate ag->acctvp = vp; 323*7c478bd9Sstevel@tonic-gate vp = oldvp; 324*7c478bd9Sstevel@tonic-gate } else { 325*7c478bd9Sstevel@tonic-gate /* 326*7c478bd9Sstevel@tonic-gate * no existing file, start accounting .. 327*7c478bd9Sstevel@tonic-gate */ 328*7c478bd9Sstevel@tonic-gate ag->acctvp = vp; 329*7c478bd9Sstevel@tonic-gate vp = NULL; 330*7c478bd9Sstevel@tonic-gate } 331*7c478bd9Sstevel@tonic-gate mutex_exit(&ag->aclock); 332*7c478bd9Sstevel@tonic-gate } 333*7c478bd9Sstevel@tonic-gate mutex_exit(&acct_list_lock); 334*7c478bd9Sstevel@tonic-gate } 335*7c478bd9Sstevel@tonic-gate 336*7c478bd9Sstevel@tonic-gate if (vp) { 337*7c478bd9Sstevel@tonic-gate (void) VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED()); 338*7c478bd9Sstevel@tonic-gate VN_RELE(vp); 339*7c478bd9Sstevel@tonic-gate } 340*7c478bd9Sstevel@tonic-gate return (error == 0 ? 0 : set_errno(error)); 341*7c478bd9Sstevel@tonic-gate } 342*7c478bd9Sstevel@tonic-gate 343*7c478bd9Sstevel@tonic-gate /* 344*7c478bd9Sstevel@tonic-gate * Produce a pseudo-floating point representation 345*7c478bd9Sstevel@tonic-gate * with 3 bits base-8 exponent, 13 bits fraction. 346*7c478bd9Sstevel@tonic-gate */ 347*7c478bd9Sstevel@tonic-gate static comp_t 348*7c478bd9Sstevel@tonic-gate acct_compress(ulong_t t) 349*7c478bd9Sstevel@tonic-gate { 350*7c478bd9Sstevel@tonic-gate int exp = 0, round = 0; 351*7c478bd9Sstevel@tonic-gate 352*7c478bd9Sstevel@tonic-gate while (t >= 8192) { 353*7c478bd9Sstevel@tonic-gate exp++; 354*7c478bd9Sstevel@tonic-gate round = t & 04; 355*7c478bd9Sstevel@tonic-gate t >>= 3; 356*7c478bd9Sstevel@tonic-gate } 357*7c478bd9Sstevel@tonic-gate if (round) { 358*7c478bd9Sstevel@tonic-gate t++; 359*7c478bd9Sstevel@tonic-gate if (t >= 8192) { 360*7c478bd9Sstevel@tonic-gate t >>= 3; 361*7c478bd9Sstevel@tonic-gate exp++; 362*7c478bd9Sstevel@tonic-gate } 363*7c478bd9Sstevel@tonic-gate } 364*7c478bd9Sstevel@tonic-gate #ifdef _LP64 365*7c478bd9Sstevel@tonic-gate if (exp > 7) { 366*7c478bd9Sstevel@tonic-gate /* prevent wraparound */ 367*7c478bd9Sstevel@tonic-gate t = 8191; 368*7c478bd9Sstevel@tonic-gate exp = 7; 369*7c478bd9Sstevel@tonic-gate } 370*7c478bd9Sstevel@tonic-gate #endif 371*7c478bd9Sstevel@tonic-gate return ((exp << 13) + t); 372*7c478bd9Sstevel@tonic-gate } 373*7c478bd9Sstevel@tonic-gate 374*7c478bd9Sstevel@tonic-gate /* 375*7c478bd9Sstevel@tonic-gate * On exit, write a record on the accounting file. 376*7c478bd9Sstevel@tonic-gate */ 377*7c478bd9Sstevel@tonic-gate void 378*7c478bd9Sstevel@tonic-gate acct(char st) 379*7c478bd9Sstevel@tonic-gate { 380*7c478bd9Sstevel@tonic-gate struct vnode *vp; 381*7c478bd9Sstevel@tonic-gate struct cred *cr; 382*7c478bd9Sstevel@tonic-gate struct proc *p; 383*7c478bd9Sstevel@tonic-gate struct vattr va; 384*7c478bd9Sstevel@tonic-gate ssize_t resid = 0; 385*7c478bd9Sstevel@tonic-gate int error; 386*7c478bd9Sstevel@tonic-gate struct acct_globals *ag; 387*7c478bd9Sstevel@tonic-gate 388*7c478bd9Sstevel@tonic-gate ag = zone_getspecific(acct_zone_key, curproc->p_zone); 389*7c478bd9Sstevel@tonic-gate 390*7c478bd9Sstevel@tonic-gate mutex_enter(&ag->aclock); 391*7c478bd9Sstevel@tonic-gate if ((vp = ag->acctvp) == NULL) { 392*7c478bd9Sstevel@tonic-gate mutex_exit(&ag->aclock); 393*7c478bd9Sstevel@tonic-gate return; 394*7c478bd9Sstevel@tonic-gate } 395*7c478bd9Sstevel@tonic-gate 396*7c478bd9Sstevel@tonic-gate /* 397*7c478bd9Sstevel@tonic-gate * This only gets called from exit after all lwp's have exited so no 398*7c478bd9Sstevel@tonic-gate * cred locking is needed. 399*7c478bd9Sstevel@tonic-gate */ 400*7c478bd9Sstevel@tonic-gate p = curproc; 401*7c478bd9Sstevel@tonic-gate bcopy(u.u_comm, ag->acctbuf.ac_comm, sizeof (ag->acctbuf.ac_comm)); 402*7c478bd9Sstevel@tonic-gate ag->acctbuf.ac_btime = u.u_start.tv_sec; 403*7c478bd9Sstevel@tonic-gate ag->acctbuf.ac_utime = acct_compress( 404*7c478bd9Sstevel@tonic-gate (comp_t)NSEC_TO_TICK(p->p_acct[LMS_USER])); 405*7c478bd9Sstevel@tonic-gate ag->acctbuf.ac_stime = acct_compress( 406*7c478bd9Sstevel@tonic-gate (comp_t)NSEC_TO_TICK(p->p_acct[LMS_SYSTEM] + p->p_acct[LMS_TRAP])); 407*7c478bd9Sstevel@tonic-gate ag->acctbuf.ac_etime = acct_compress(lbolt - u.u_ticks); 408*7c478bd9Sstevel@tonic-gate ag->acctbuf.ac_mem = acct_compress((ulong_t)u.u_mem); 409*7c478bd9Sstevel@tonic-gate ag->acctbuf.ac_io = acct_compress((ulong_t)p->p_ru.ioch); 410*7c478bd9Sstevel@tonic-gate ag->acctbuf.ac_rw = acct_compress((ulong_t)(p->p_ru.inblock + 411*7c478bd9Sstevel@tonic-gate p->p_ru.oublock)); 412*7c478bd9Sstevel@tonic-gate cr = CRED(); 413*7c478bd9Sstevel@tonic-gate ag->acctbuf.ac_uid = crgetruid(cr); 414*7c478bd9Sstevel@tonic-gate ag->acctbuf.ac_gid = crgetrgid(cr); 415*7c478bd9Sstevel@tonic-gate (void) cmpldev(&ag->acctbuf.ac_tty, cttydev(p)); 416*7c478bd9Sstevel@tonic-gate ag->acctbuf.ac_stat = st; 417*7c478bd9Sstevel@tonic-gate ag->acctbuf.ac_flag = (u.u_acflag | AEXPND); 418*7c478bd9Sstevel@tonic-gate 419*7c478bd9Sstevel@tonic-gate /* 420*7c478bd9Sstevel@tonic-gate * Save the size. If the write fails, reset the size to avoid 421*7c478bd9Sstevel@tonic-gate * corrupted acct files. 422*7c478bd9Sstevel@tonic-gate * 423*7c478bd9Sstevel@tonic-gate * Large Files: We deliberately prevent accounting files from 424*7c478bd9Sstevel@tonic-gate * exceeding the 2GB limit as none of the accounting commands are 425*7c478bd9Sstevel@tonic-gate * currently large file aware. 426*7c478bd9Sstevel@tonic-gate */ 427*7c478bd9Sstevel@tonic-gate va.va_mask = AT_SIZE; 428*7c478bd9Sstevel@tonic-gate if (VOP_GETATTR(vp, &va, 0, kcred) == 0) { 429*7c478bd9Sstevel@tonic-gate error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&ag->acctbuf, 430*7c478bd9Sstevel@tonic-gate sizeof (ag->acctbuf), 0LL, UIO_SYSSPACE, FAPPEND, 431*7c478bd9Sstevel@tonic-gate (rlim64_t)MAXOFF32_T, kcred, &resid); 432*7c478bd9Sstevel@tonic-gate if (error || resid) 433*7c478bd9Sstevel@tonic-gate (void) VOP_SETATTR(vp, &va, 0, kcred, NULL); 434*7c478bd9Sstevel@tonic-gate } 435*7c478bd9Sstevel@tonic-gate mutex_exit(&ag->aclock); 436*7c478bd9Sstevel@tonic-gate } 437