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