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