xref: /titanic_53/usr/src/uts/common/os/acct.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ae115bc7Smrj  * Common Development and Distribution License (the "License").
6ae115bc7Smrj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/acct.h>
377c478bd9Sstevel@tonic-gate #include <sys/cred.h>
387c478bd9Sstevel@tonic-gate #include <sys/user.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/file.h>
417c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
427c478bd9Sstevel@tonic-gate #include <sys/debug.h>
437c478bd9Sstevel@tonic-gate #include <sys/proc.h>
447c478bd9Sstevel@tonic-gate #include <sys/resource.h>
457c478bd9Sstevel@tonic-gate #include <sys/session.h>
467c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
477c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
487c478bd9Sstevel@tonic-gate #include <sys/policy.h>
497c478bd9Sstevel@tonic-gate #include <sys/list.h>
507c478bd9Sstevel@tonic-gate #include <sys/time.h>
517c478bd9Sstevel@tonic-gate #include <sys/msacct.h>
527c478bd9Sstevel@tonic-gate #include <sys/zone.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * Each zone has its own accounting settings (on or off) and associated
567c478bd9Sstevel@tonic-gate  * file.  The global zone is not special in this aspect; it will only
577c478bd9Sstevel@tonic-gate  * generate records for processes that ran in the global zone.  We could
587c478bd9Sstevel@tonic-gate  * allow the global zone to record all activity on the system, but there
597c478bd9Sstevel@tonic-gate  * would be no way of knowing the zone in which the processes executed.
607c478bd9Sstevel@tonic-gate  * sysacct() is thus virtualized to only act on the caller's zone.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate struct acct_globals {
637c478bd9Sstevel@tonic-gate 	struct acct	acctbuf;
647c478bd9Sstevel@tonic-gate 	kmutex_t	aclock;
657c478bd9Sstevel@tonic-gate 	struct vnode	*acctvp;
667c478bd9Sstevel@tonic-gate 	list_node_t	aclink;
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * We need a list of all accounting settings for all zones, so we can
717c478bd9Sstevel@tonic-gate  * accurately determine if a file is in use for accounting (possibly by
727c478bd9Sstevel@tonic-gate  * another zone).
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate static zone_key_t acct_zone_key;
757c478bd9Sstevel@tonic-gate static list_t acct_list;
767c478bd9Sstevel@tonic-gate kmutex_t acct_list_lock;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static struct sysent acctsysent = {
797c478bd9Sstevel@tonic-gate 	1,
807c478bd9Sstevel@tonic-gate 	SE_NOUNLOAD | SE_ARGC | SE_32RVAL1,
817c478bd9Sstevel@tonic-gate 	sysacct
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
857c478bd9Sstevel@tonic-gate 	&mod_syscallops, "acct(2) syscall", &acctsysent
867c478bd9Sstevel@tonic-gate };
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
897c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
907c478bd9Sstevel@tonic-gate 	&mod_syscallops32, "32-bit acct(2) syscall", &acctsysent
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate #endif
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
957c478bd9Sstevel@tonic-gate 	MODREV_1,
967c478bd9Sstevel@tonic-gate 	&modlsys,
977c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
987c478bd9Sstevel@tonic-gate 	&modlsys32,
997c478bd9Sstevel@tonic-gate #endif
1007c478bd9Sstevel@tonic-gate 	NULL
1017c478bd9Sstevel@tonic-gate };
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1047c478bd9Sstevel@tonic-gate static void *
1057c478bd9Sstevel@tonic-gate acct_init(zoneid_t zoneid)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	struct acct_globals *ag;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	ag = kmem_alloc(sizeof (*ag), KM_SLEEP);
1107c478bd9Sstevel@tonic-gate 	bzero(&ag->acctbuf, sizeof (ag->acctbuf));
1117c478bd9Sstevel@tonic-gate 	mutex_init(&ag->aclock, NULL, MUTEX_DEFAULT, NULL);
1127c478bd9Sstevel@tonic-gate 	ag->acctvp = NULL;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	mutex_enter(&acct_list_lock);
1157c478bd9Sstevel@tonic-gate 	list_insert_tail(&acct_list, ag);
1167c478bd9Sstevel@tonic-gate 	mutex_exit(&acct_list_lock);
1177c478bd9Sstevel@tonic-gate 	return (ag);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /* ARGSUSED */
1217c478bd9Sstevel@tonic-gate static void
1227c478bd9Sstevel@tonic-gate acct_shutdown(zoneid_t zoneid, void *arg)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	struct acct_globals *ag = arg;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	mutex_enter(&ag->aclock);
1277c478bd9Sstevel@tonic-gate 	if (ag->acctvp) {
1287c478bd9Sstevel@tonic-gate 		/*
1297c478bd9Sstevel@tonic-gate 		 * This needs to be done as a shutdown callback, otherwise this
1307c478bd9Sstevel@tonic-gate 		 * held vnode may cause filesystems to be busy, and the zone
1317c478bd9Sstevel@tonic-gate 		 * shutdown operation to fail.
1327c478bd9Sstevel@tonic-gate 		 */
133*da6c28aaSamw 		(void) VOP_CLOSE(ag->acctvp, FWRITE, 1, (offset_t)0, kcred,
134*da6c28aaSamw 		    NULL);
1357c478bd9Sstevel@tonic-gate 		VN_RELE(ag->acctvp);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	ag->acctvp = NULL;
1387c478bd9Sstevel@tonic-gate 	mutex_exit(&ag->aclock);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1427c478bd9Sstevel@tonic-gate static void
1437c478bd9Sstevel@tonic-gate acct_fini(zoneid_t zoneid, void *arg)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	struct acct_globals *ag = arg;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	mutex_enter(&acct_list_lock);
1487c478bd9Sstevel@tonic-gate 	list_remove(&acct_list, ag);
1497c478bd9Sstevel@tonic-gate 	mutex_exit(&acct_list_lock);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	mutex_destroy(&ag->aclock);
1527c478bd9Sstevel@tonic-gate 	kmem_free(ag, sizeof (*ag));
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate int
1567c478bd9Sstevel@tonic-gate _init(void)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	int error;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	mutex_init(&acct_list_lock, NULL, MUTEX_DEFAULT, NULL);
1617c478bd9Sstevel@tonic-gate 	list_create(&acct_list, sizeof (struct acct_globals),
1627c478bd9Sstevel@tonic-gate 	    offsetof(struct acct_globals, aclink));
1637c478bd9Sstevel@tonic-gate 	/*
1647c478bd9Sstevel@tonic-gate 	 * Using an initializer here wastes a bit of memory for zones that
1657c478bd9Sstevel@tonic-gate 	 * don't use accounting, but vastly simplifies the locking.
1667c478bd9Sstevel@tonic-gate 	 */
1677c478bd9Sstevel@tonic-gate 	zone_key_create(&acct_zone_key, acct_init, acct_shutdown, acct_fini);
1687c478bd9Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0) {
1697c478bd9Sstevel@tonic-gate 		(void) zone_key_delete(acct_zone_key);
1707c478bd9Sstevel@tonic-gate 		list_destroy(&acct_list);
1717c478bd9Sstevel@tonic-gate 		mutex_destroy(&acct_list_lock);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	return (error);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate int
1777c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * acct() is a "weak stub" routine called from exit().
1847c478bd9Sstevel@tonic-gate  * Once this module has been loaded, we refuse to allow
1857c478bd9Sstevel@tonic-gate  * it to unload - otherwise accounting would quietly
1867c478bd9Sstevel@tonic-gate  * cease.  See 1211661.  It's possible to make this module
1877c478bd9Sstevel@tonic-gate  * unloadable but it's substantially safer not to bother.
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate int
1907c478bd9Sstevel@tonic-gate _fini(void)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	return (EBUSY);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate  * See if vp is in use by the accounting system on any zone.  This does a deep
1977c478bd9Sstevel@tonic-gate  * comparison of vnodes such that a file and a lofs "shadow" node of it will
1987c478bd9Sstevel@tonic-gate  * appear to be the same.
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  * If 'compare_vfs' is true, the function will do a comparison of vfs_t's
2017c478bd9Sstevel@tonic-gate  * instead (ie, is the vfs_t on which the vnode resides in use by the
2027c478bd9Sstevel@tonic-gate  * accounting system in any zone).
2037c478bd9Sstevel@tonic-gate  *
2047c478bd9Sstevel@tonic-gate  * Returns 1 if found (in use), 0 otherwise.
2057c478bd9Sstevel@tonic-gate  */
2067c478bd9Sstevel@tonic-gate static int
2077c478bd9Sstevel@tonic-gate acct_find(vnode_t *vp, boolean_t compare_vfs)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	struct acct_globals *ag;
2107c478bd9Sstevel@tonic-gate 	vnode_t *realvp;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&acct_list_lock));
2137c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
2147c478bd9Sstevel@tonic-gate 
215*da6c28aaSamw 	if (VOP_REALVP(vp, &realvp, NULL))
2167c478bd9Sstevel@tonic-gate 		realvp = vp;
2177c478bd9Sstevel@tonic-gate 	for (ag = list_head(&acct_list); ag != NULL;
2187c478bd9Sstevel@tonic-gate 	    ag = list_next(&acct_list, ag)) {
2197c478bd9Sstevel@tonic-gate 		vnode_t *racctvp;
2207c478bd9Sstevel@tonic-gate 		boolean_t found = B_FALSE;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		mutex_enter(&ag->aclock);
2237c478bd9Sstevel@tonic-gate 		if (ag->acctvp == NULL) {
2247c478bd9Sstevel@tonic-gate 			mutex_exit(&ag->aclock);
2257c478bd9Sstevel@tonic-gate 			continue;
2267c478bd9Sstevel@tonic-gate 		}
227*da6c28aaSamw 		if (VOP_REALVP(ag->acctvp, &racctvp, NULL))
2287c478bd9Sstevel@tonic-gate 			racctvp = ag->acctvp;
2297c478bd9Sstevel@tonic-gate 		if (compare_vfs) {
2307c478bd9Sstevel@tonic-gate 			if (racctvp->v_vfsp == realvp->v_vfsp)
2317c478bd9Sstevel@tonic-gate 				found = B_TRUE;
2327c478bd9Sstevel@tonic-gate 		} else {
2337c478bd9Sstevel@tonic-gate 			if (VN_CMP(realvp, racctvp))
2347c478bd9Sstevel@tonic-gate 				found = B_TRUE;
2357c478bd9Sstevel@tonic-gate 		}
2367c478bd9Sstevel@tonic-gate 		mutex_exit(&ag->aclock);
2377c478bd9Sstevel@tonic-gate 		if (found)
2387c478bd9Sstevel@tonic-gate 			return (1);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	return (0);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate  * Returns 1 if the vfs that vnode resides on is in use for the accounting
2457c478bd9Sstevel@tonic-gate  * subsystem, 0 otherwise.
2467c478bd9Sstevel@tonic-gate  */
2477c478bd9Sstevel@tonic-gate int
2487c478bd9Sstevel@tonic-gate acct_fs_in_use(vnode_t *vp)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	int found;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (vp == NULL)
2537c478bd9Sstevel@tonic-gate 		return (0);
2547c478bd9Sstevel@tonic-gate 	mutex_enter(&acct_list_lock);
2557c478bd9Sstevel@tonic-gate 	found = acct_find(vp, B_TRUE);
2567c478bd9Sstevel@tonic-gate 	mutex_exit(&acct_list_lock);
2577c478bd9Sstevel@tonic-gate 	return (found);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * Perform process accounting functions.
2627c478bd9Sstevel@tonic-gate  */
2637c478bd9Sstevel@tonic-gate int
2647c478bd9Sstevel@tonic-gate sysacct(char *fname)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate 	struct acct_globals *ag;
2677c478bd9Sstevel@tonic-gate 	struct vnode *vp;
2687c478bd9Sstevel@tonic-gate 	int error = 0;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (secpolicy_acct(CRED()) != 0)
2717c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	ag = zone_getspecific(acct_zone_key, curproc->p_zone);
2747c478bd9Sstevel@tonic-gate 	ASSERT(ag != NULL);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if (fname == NULL) {
2777c478bd9Sstevel@tonic-gate 		/*
2787c478bd9Sstevel@tonic-gate 		 * Close the file and stop accounting.
2797c478bd9Sstevel@tonic-gate 		 */
2807c478bd9Sstevel@tonic-gate 		mutex_enter(&ag->aclock);
2817c478bd9Sstevel@tonic-gate 		vp = ag->acctvp;
2827c478bd9Sstevel@tonic-gate 		ag->acctvp = NULL;
2837c478bd9Sstevel@tonic-gate 		mutex_exit(&ag->aclock);
2847c478bd9Sstevel@tonic-gate 		if (vp) {
285*da6c28aaSamw 			error = VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED(),
286*da6c28aaSamw 			    NULL);
2877c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 		return (error == 0 ? 0 : set_errno(error));
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/*
2937c478bd9Sstevel@tonic-gate 	 * Either (a) open a new file and begin accounting -or- (b)
2947c478bd9Sstevel@tonic-gate 	 * switch accounting from an old to a new file.
2957c478bd9Sstevel@tonic-gate 	 *
2967c478bd9Sstevel@tonic-gate 	 * (Open the file without holding aclock in case it
2977c478bd9Sstevel@tonic-gate 	 * sleeps (holding the lock prevents process exit).)
2987c478bd9Sstevel@tonic-gate 	 */
2997c478bd9Sstevel@tonic-gate 	if ((error = vn_open(fname, UIO_USERSPACE, FWRITE,
3007c478bd9Sstevel@tonic-gate 	    0, &vp, (enum create)0, 0)) != 0) {
3017c478bd9Sstevel@tonic-gate 		/* SVID  compliance */
3027c478bd9Sstevel@tonic-gate 		if (error == EISDIR)
3037c478bd9Sstevel@tonic-gate 			error = EACCES;
3047c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG) {
3087c478bd9Sstevel@tonic-gate 		error = EACCES;
3097c478bd9Sstevel@tonic-gate 	} else {
3107c478bd9Sstevel@tonic-gate 		mutex_enter(&acct_list_lock);
3117c478bd9Sstevel@tonic-gate 		if (acct_find(vp, B_FALSE)) {
3127c478bd9Sstevel@tonic-gate 			error = EBUSY;
3137c478bd9Sstevel@tonic-gate 		} else {
3147c478bd9Sstevel@tonic-gate 			mutex_enter(&ag->aclock);
3157c478bd9Sstevel@tonic-gate 			if (ag->acctvp) {
3167c478bd9Sstevel@tonic-gate 				vnode_t *oldvp;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 				/*
3197c478bd9Sstevel@tonic-gate 				 * close old acctvp, and point acct()
3207c478bd9Sstevel@tonic-gate 				 * at new file by swapping vp and acctvp
3217c478bd9Sstevel@tonic-gate 				 */
3227c478bd9Sstevel@tonic-gate 				oldvp = ag->acctvp;
3237c478bd9Sstevel@tonic-gate 				ag->acctvp = vp;
3247c478bd9Sstevel@tonic-gate 				vp = oldvp;
3257c478bd9Sstevel@tonic-gate 			} else {
3267c478bd9Sstevel@tonic-gate 				/*
3277c478bd9Sstevel@tonic-gate 				 * no existing file, start accounting ..
3287c478bd9Sstevel@tonic-gate 				 */
3297c478bd9Sstevel@tonic-gate 				ag->acctvp = vp;
3307c478bd9Sstevel@tonic-gate 				vp = NULL;
3317c478bd9Sstevel@tonic-gate 			}
3327c478bd9Sstevel@tonic-gate 			mutex_exit(&ag->aclock);
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 		mutex_exit(&acct_list_lock);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (vp) {
338*da6c28aaSamw 		(void) VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED(), NULL);
3397c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 	return (error == 0 ? 0 : set_errno(error));
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * Produce a pseudo-floating point representation
3467c478bd9Sstevel@tonic-gate  * with 3 bits base-8 exponent, 13 bits fraction.
3477c478bd9Sstevel@tonic-gate  */
3487c478bd9Sstevel@tonic-gate static comp_t
3497c478bd9Sstevel@tonic-gate acct_compress(ulong_t t)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	int exp = 0, round = 0;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	while (t >= 8192) {
3547c478bd9Sstevel@tonic-gate 		exp++;
3557c478bd9Sstevel@tonic-gate 		round = t & 04;
3567c478bd9Sstevel@tonic-gate 		t >>= 3;
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 	if (round) {
3597c478bd9Sstevel@tonic-gate 		t++;
3607c478bd9Sstevel@tonic-gate 		if (t >= 8192) {
3617c478bd9Sstevel@tonic-gate 			t >>= 3;
3627c478bd9Sstevel@tonic-gate 			exp++;
3637c478bd9Sstevel@tonic-gate 		}
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate #ifdef _LP64
3667c478bd9Sstevel@tonic-gate 	if (exp > 7) {
3677c478bd9Sstevel@tonic-gate 		/* prevent wraparound */
3687c478bd9Sstevel@tonic-gate 		t = 8191;
3697c478bd9Sstevel@tonic-gate 		exp = 7;
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate #endif
3727c478bd9Sstevel@tonic-gate 	return ((exp << 13) + t);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate  * On exit, write a record on the accounting file.
3777c478bd9Sstevel@tonic-gate  */
3787c478bd9Sstevel@tonic-gate void
3797c478bd9Sstevel@tonic-gate acct(char st)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	struct vnode *vp;
3827c478bd9Sstevel@tonic-gate 	struct cred *cr;
3837c478bd9Sstevel@tonic-gate 	struct proc *p;
384ae115bc7Smrj 	user_t *ua;
3857c478bd9Sstevel@tonic-gate 	struct vattr va;
3867c478bd9Sstevel@tonic-gate 	ssize_t resid = 0;
3877c478bd9Sstevel@tonic-gate 	int error;
3887c478bd9Sstevel@tonic-gate 	struct acct_globals *ag;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	ag = zone_getspecific(acct_zone_key, curproc->p_zone);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	mutex_enter(&ag->aclock);
3937c478bd9Sstevel@tonic-gate 	if ((vp = ag->acctvp) == NULL) {
3947c478bd9Sstevel@tonic-gate 		mutex_exit(&ag->aclock);
3957c478bd9Sstevel@tonic-gate 		return;
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	/*
3997c478bd9Sstevel@tonic-gate 	 * This only gets called from exit after all lwp's have exited so no
4007c478bd9Sstevel@tonic-gate 	 * cred locking is needed.
4017c478bd9Sstevel@tonic-gate 	 */
4027c478bd9Sstevel@tonic-gate 	p = curproc;
403ae115bc7Smrj 	ua = PTOU(p);
404ae115bc7Smrj 	bcopy(ua->u_comm, ag->acctbuf.ac_comm, sizeof (ag->acctbuf.ac_comm));
405ae115bc7Smrj 	ag->acctbuf.ac_btime = ua->u_start.tv_sec;
4064d3d099fSsp92102 	ag->acctbuf.ac_utime = acct_compress(NSEC_TO_TICK(p->p_acct[LMS_USER]));
4077c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_stime = acct_compress(
4084d3d099fSsp92102 	    NSEC_TO_TICK(p->p_acct[LMS_SYSTEM] + p->p_acct[LMS_TRAP]));
409ae115bc7Smrj 	ag->acctbuf.ac_etime = acct_compress(lbolt - ua->u_ticks);
410ae115bc7Smrj 	ag->acctbuf.ac_mem = acct_compress((ulong_t)ua->u_mem);
4117c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_io = acct_compress((ulong_t)p->p_ru.ioch);
4127c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_rw = acct_compress((ulong_t)(p->p_ru.inblock +
4137c478bd9Sstevel@tonic-gate 	    p->p_ru.oublock));
4147c478bd9Sstevel@tonic-gate 	cr = CRED();
4157c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_uid = crgetruid(cr);
4167c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_gid = crgetrgid(cr);
4177c478bd9Sstevel@tonic-gate 	(void) cmpldev(&ag->acctbuf.ac_tty, cttydev(p));
4187c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_stat = st;
419ae115bc7Smrj 	ag->acctbuf.ac_flag = (ua->u_acflag | AEXPND);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	/*
4227c478bd9Sstevel@tonic-gate 	 * Save the size. If the write fails, reset the size to avoid
4237c478bd9Sstevel@tonic-gate 	 * corrupted acct files.
4247c478bd9Sstevel@tonic-gate 	 *
4257c478bd9Sstevel@tonic-gate 	 * Large Files: We deliberately prevent accounting files from
4267c478bd9Sstevel@tonic-gate 	 * exceeding the 2GB limit as none of the accounting commands are
4277c478bd9Sstevel@tonic-gate 	 * currently large file aware.
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	va.va_mask = AT_SIZE;
430*da6c28aaSamw 	if (VOP_GETATTR(vp, &va, 0, kcred, NULL) == 0) {
4317c478bd9Sstevel@tonic-gate 		error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&ag->acctbuf,
4327c478bd9Sstevel@tonic-gate 		    sizeof (ag->acctbuf), 0LL, UIO_SYSSPACE, FAPPEND,
4337c478bd9Sstevel@tonic-gate 		    (rlim64_t)MAXOFF32_T, kcred, &resid);
4347c478bd9Sstevel@tonic-gate 		if (error || resid)
4357c478bd9Sstevel@tonic-gate 			(void) VOP_SETATTR(vp, &va, 0, kcred, NULL);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 	mutex_exit(&ag->aclock);
4387c478bd9Sstevel@tonic-gate }
439