xref: /titanic_52/usr/src/uts/common/os/acct.c (revision 4d3d099f13169ab8c528b49d89bd3d787104cf98)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*4d3d099fSsp92102  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/acct.h>
387c478bd9Sstevel@tonic-gate #include <sys/cred.h>
397c478bd9Sstevel@tonic-gate #include <sys/user.h>
407c478bd9Sstevel@tonic-gate #include <sys/errno.h>
417c478bd9Sstevel@tonic-gate #include <sys/file.h>
427c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
437c478bd9Sstevel@tonic-gate #include <sys/debug.h>
447c478bd9Sstevel@tonic-gate #include <sys/proc.h>
457c478bd9Sstevel@tonic-gate #include <sys/resource.h>
467c478bd9Sstevel@tonic-gate #include <sys/session.h>
477c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
487c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
497c478bd9Sstevel@tonic-gate #include <sys/policy.h>
507c478bd9Sstevel@tonic-gate #include <sys/list.h>
517c478bd9Sstevel@tonic-gate #include <sys/time.h>
527c478bd9Sstevel@tonic-gate #include <sys/msacct.h>
537c478bd9Sstevel@tonic-gate #include <sys/zone.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Each zone has its own accounting settings (on or off) and associated
577c478bd9Sstevel@tonic-gate  * file.  The global zone is not special in this aspect; it will only
587c478bd9Sstevel@tonic-gate  * generate records for processes that ran in the global zone.  We could
597c478bd9Sstevel@tonic-gate  * allow the global zone to record all activity on the system, but there
607c478bd9Sstevel@tonic-gate  * would be no way of knowing the zone in which the processes executed.
617c478bd9Sstevel@tonic-gate  * sysacct() is thus virtualized to only act on the caller's zone.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate struct acct_globals {
647c478bd9Sstevel@tonic-gate 	struct acct	acctbuf;
657c478bd9Sstevel@tonic-gate 	kmutex_t	aclock;
667c478bd9Sstevel@tonic-gate 	struct vnode	*acctvp;
677c478bd9Sstevel@tonic-gate 	list_node_t	aclink;
687c478bd9Sstevel@tonic-gate };
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * We need a list of all accounting settings for all zones, so we can
727c478bd9Sstevel@tonic-gate  * accurately determine if a file is in use for accounting (possibly by
737c478bd9Sstevel@tonic-gate  * another zone).
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate static zone_key_t acct_zone_key;
767c478bd9Sstevel@tonic-gate static list_t acct_list;
777c478bd9Sstevel@tonic-gate kmutex_t acct_list_lock;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static struct sysent acctsysent = {
807c478bd9Sstevel@tonic-gate 	1,
817c478bd9Sstevel@tonic-gate 	SE_NOUNLOAD | SE_ARGC | SE_32RVAL1,
827c478bd9Sstevel@tonic-gate 	sysacct
837c478bd9Sstevel@tonic-gate };
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
867c478bd9Sstevel@tonic-gate 	&mod_syscallops, "acct(2) syscall", &acctsysent
877c478bd9Sstevel@tonic-gate };
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
907c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
917c478bd9Sstevel@tonic-gate 	&mod_syscallops32, "32-bit acct(2) syscall", &acctsysent
927c478bd9Sstevel@tonic-gate };
937c478bd9Sstevel@tonic-gate #endif
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
967c478bd9Sstevel@tonic-gate 	MODREV_1,
977c478bd9Sstevel@tonic-gate 	&modlsys,
987c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
997c478bd9Sstevel@tonic-gate 	&modlsys32,
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate 	NULL
1027c478bd9Sstevel@tonic-gate };
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1057c478bd9Sstevel@tonic-gate static void *
1067c478bd9Sstevel@tonic-gate acct_init(zoneid_t zoneid)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	struct acct_globals *ag;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	ag = kmem_alloc(sizeof (*ag), KM_SLEEP);
1117c478bd9Sstevel@tonic-gate 	bzero(&ag->acctbuf, sizeof (ag->acctbuf));
1127c478bd9Sstevel@tonic-gate 	mutex_init(&ag->aclock, NULL, MUTEX_DEFAULT, NULL);
1137c478bd9Sstevel@tonic-gate 	ag->acctvp = NULL;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	mutex_enter(&acct_list_lock);
1167c478bd9Sstevel@tonic-gate 	list_insert_tail(&acct_list, ag);
1177c478bd9Sstevel@tonic-gate 	mutex_exit(&acct_list_lock);
1187c478bd9Sstevel@tonic-gate 	return (ag);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /* ARGSUSED */
1227c478bd9Sstevel@tonic-gate static void
1237c478bd9Sstevel@tonic-gate acct_shutdown(zoneid_t zoneid, void *arg)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	struct acct_globals *ag = arg;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	mutex_enter(&ag->aclock);
1287c478bd9Sstevel@tonic-gate 	if (ag->acctvp) {
1297c478bd9Sstevel@tonic-gate 		/*
1307c478bd9Sstevel@tonic-gate 		 * This needs to be done as a shutdown callback, otherwise this
1317c478bd9Sstevel@tonic-gate 		 * held vnode may cause filesystems to be busy, and the zone
1327c478bd9Sstevel@tonic-gate 		 * shutdown operation to fail.
1337c478bd9Sstevel@tonic-gate 		 */
1347c478bd9Sstevel@tonic-gate 		(void) VOP_CLOSE(ag->acctvp, FWRITE, 1, (offset_t)0, kcred);
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 
2157c478bd9Sstevel@tonic-gate 	if (VOP_REALVP(vp, &realvp))
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 		}
2277c478bd9Sstevel@tonic-gate 		if (VOP_REALVP(ag->acctvp, &racctvp))
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) {
2857c478bd9Sstevel@tonic-gate 			error = VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED());
2867c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 		return (error == 0 ? 0 : set_errno(error));
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/*
2927c478bd9Sstevel@tonic-gate 	 * Either (a) open a new file and begin accounting -or- (b)
2937c478bd9Sstevel@tonic-gate 	 * switch accounting from an old to a new file.
2947c478bd9Sstevel@tonic-gate 	 *
2957c478bd9Sstevel@tonic-gate 	 * (Open the file without holding aclock in case it
2967c478bd9Sstevel@tonic-gate 	 * sleeps (holding the lock prevents process exit).)
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate 	if ((error = vn_open(fname, UIO_USERSPACE, FWRITE,
2997c478bd9Sstevel@tonic-gate 	    0, &vp, (enum create)0, 0)) != 0) {
3007c478bd9Sstevel@tonic-gate 		/* SVID  compliance */
3017c478bd9Sstevel@tonic-gate 		if (error == EISDIR)
3027c478bd9Sstevel@tonic-gate 			error = EACCES;
3037c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG) {
3077c478bd9Sstevel@tonic-gate 		error = EACCES;
3087c478bd9Sstevel@tonic-gate 	} else {
3097c478bd9Sstevel@tonic-gate 		mutex_enter(&acct_list_lock);
3107c478bd9Sstevel@tonic-gate 		if (acct_find(vp, B_FALSE)) {
3117c478bd9Sstevel@tonic-gate 			error = EBUSY;
3127c478bd9Sstevel@tonic-gate 		} else {
3137c478bd9Sstevel@tonic-gate 			mutex_enter(&ag->aclock);
3147c478bd9Sstevel@tonic-gate 			if (ag->acctvp) {
3157c478bd9Sstevel@tonic-gate 				vnode_t *oldvp;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 				/*
3187c478bd9Sstevel@tonic-gate 				 * close old acctvp, and point acct()
3197c478bd9Sstevel@tonic-gate 				 * at new file by swapping vp and acctvp
3207c478bd9Sstevel@tonic-gate 				 */
3217c478bd9Sstevel@tonic-gate 				oldvp = ag->acctvp;
3227c478bd9Sstevel@tonic-gate 				ag->acctvp = vp;
3237c478bd9Sstevel@tonic-gate 				vp = oldvp;
3247c478bd9Sstevel@tonic-gate 			} else {
3257c478bd9Sstevel@tonic-gate 				/*
3267c478bd9Sstevel@tonic-gate 				 * no existing file, start accounting ..
3277c478bd9Sstevel@tonic-gate 				 */
3287c478bd9Sstevel@tonic-gate 				ag->acctvp = vp;
3297c478bd9Sstevel@tonic-gate 				vp = NULL;
3307c478bd9Sstevel@tonic-gate 			}
3317c478bd9Sstevel@tonic-gate 			mutex_exit(&ag->aclock);
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 		mutex_exit(&acct_list_lock);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if (vp) {
3377c478bd9Sstevel@tonic-gate 		(void) VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED());
3387c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 	return (error == 0 ? 0 : set_errno(error));
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate /*
3447c478bd9Sstevel@tonic-gate  * Produce a pseudo-floating point representation
3457c478bd9Sstevel@tonic-gate  * with 3 bits base-8 exponent, 13 bits fraction.
3467c478bd9Sstevel@tonic-gate  */
3477c478bd9Sstevel@tonic-gate static comp_t
3487c478bd9Sstevel@tonic-gate acct_compress(ulong_t t)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	int exp = 0, round = 0;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	while (t >= 8192) {
3537c478bd9Sstevel@tonic-gate 		exp++;
3547c478bd9Sstevel@tonic-gate 		round = t & 04;
3557c478bd9Sstevel@tonic-gate 		t >>= 3;
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	if (round) {
3587c478bd9Sstevel@tonic-gate 		t++;
3597c478bd9Sstevel@tonic-gate 		if (t >= 8192) {
3607c478bd9Sstevel@tonic-gate 			t >>= 3;
3617c478bd9Sstevel@tonic-gate 			exp++;
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate #ifdef _LP64
3657c478bd9Sstevel@tonic-gate 	if (exp > 7) {
3667c478bd9Sstevel@tonic-gate 		/* prevent wraparound */
3677c478bd9Sstevel@tonic-gate 		t = 8191;
3687c478bd9Sstevel@tonic-gate 		exp = 7;
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate #endif
3717c478bd9Sstevel@tonic-gate 	return ((exp << 13) + t);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate  * On exit, write a record on the accounting file.
3767c478bd9Sstevel@tonic-gate  */
3777c478bd9Sstevel@tonic-gate void
3787c478bd9Sstevel@tonic-gate acct(char st)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	struct vnode *vp;
3817c478bd9Sstevel@tonic-gate 	struct cred *cr;
3827c478bd9Sstevel@tonic-gate 	struct proc *p;
3837c478bd9Sstevel@tonic-gate 	struct vattr va;
3847c478bd9Sstevel@tonic-gate 	ssize_t resid = 0;
3857c478bd9Sstevel@tonic-gate 	int error;
3867c478bd9Sstevel@tonic-gate 	struct acct_globals *ag;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	ag = zone_getspecific(acct_zone_key, curproc->p_zone);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	mutex_enter(&ag->aclock);
3917c478bd9Sstevel@tonic-gate 	if ((vp = ag->acctvp) == NULL) {
3927c478bd9Sstevel@tonic-gate 		mutex_exit(&ag->aclock);
3937c478bd9Sstevel@tonic-gate 		return;
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * This only gets called from exit after all lwp's have exited so no
3987c478bd9Sstevel@tonic-gate 	 * cred locking is needed.
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	p = curproc;
4017c478bd9Sstevel@tonic-gate 	bcopy(u.u_comm, ag->acctbuf.ac_comm, sizeof (ag->acctbuf.ac_comm));
4027c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_btime = u.u_start.tv_sec;
403*4d3d099fSsp92102 	ag->acctbuf.ac_utime = acct_compress(NSEC_TO_TICK(p->p_acct[LMS_USER]));
4047c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_stime = acct_compress(
405*4d3d099fSsp92102 	    NSEC_TO_TICK(p->p_acct[LMS_SYSTEM] + p->p_acct[LMS_TRAP]));
4067c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_etime = acct_compress(lbolt - u.u_ticks);
4077c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_mem = acct_compress((ulong_t)u.u_mem);
4087c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_io = acct_compress((ulong_t)p->p_ru.ioch);
4097c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_rw = acct_compress((ulong_t)(p->p_ru.inblock +
4107c478bd9Sstevel@tonic-gate 	    p->p_ru.oublock));
4117c478bd9Sstevel@tonic-gate 	cr = CRED();
4127c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_uid = crgetruid(cr);
4137c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_gid = crgetrgid(cr);
4147c478bd9Sstevel@tonic-gate 	(void) cmpldev(&ag->acctbuf.ac_tty, cttydev(p));
4157c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_stat = st;
4167c478bd9Sstevel@tonic-gate 	ag->acctbuf.ac_flag = (u.u_acflag | AEXPND);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * Save the size. If the write fails, reset the size to avoid
4207c478bd9Sstevel@tonic-gate 	 * corrupted acct files.
4217c478bd9Sstevel@tonic-gate 	 *
4227c478bd9Sstevel@tonic-gate 	 * Large Files: We deliberately prevent accounting files from
4237c478bd9Sstevel@tonic-gate 	 * exceeding the 2GB limit as none of the accounting commands are
4247c478bd9Sstevel@tonic-gate 	 * currently large file aware.
4257c478bd9Sstevel@tonic-gate 	 */
4267c478bd9Sstevel@tonic-gate 	va.va_mask = AT_SIZE;
4277c478bd9Sstevel@tonic-gate 	if (VOP_GETATTR(vp, &va, 0, kcred) == 0) {
4287c478bd9Sstevel@tonic-gate 		error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&ag->acctbuf,
4297c478bd9Sstevel@tonic-gate 		    sizeof (ag->acctbuf), 0LL, UIO_SYSSPACE, FAPPEND,
4307c478bd9Sstevel@tonic-gate 		    (rlim64_t)MAXOFF32_T, kcred, &resid);
4317c478bd9Sstevel@tonic-gate 		if (error || resid)
4327c478bd9Sstevel@tonic-gate 			(void) VOP_SETATTR(vp, &va, 0, kcred, NULL);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 	mutex_exit(&ag->aclock);
4357c478bd9Sstevel@tonic-gate }
436