xref: /titanic_44/usr/src/uts/common/syscall/acctctl.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
5*da6c28aaSamw  * Common Development and Distribution License (the "License").
6*da6c28aaSamw  * 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*da6c28aaSamw  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/proc.h>
297c478bd9Sstevel@tonic-gate #include <sys/systm.h>
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
357c478bd9Sstevel@tonic-gate #include <sys/user.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
387c478bd9Sstevel@tonic-gate #include <sys/file.h>
397c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
407c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
417c478bd9Sstevel@tonic-gate #include <sys/acctctl.h>
427c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
437c478bd9Sstevel@tonic-gate #include <sys/exacct.h>
447c478bd9Sstevel@tonic-gate #include <sys/policy.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * acctctl(2)
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  *   acctctl() provides the administrative interface to the extended accounting
507c478bd9Sstevel@tonic-gate  *   subsystem.  The process and task accounting facilities are configurable:
517c478bd9Sstevel@tonic-gate  *   resources can be individually specified for recording in the appropriate
527c478bd9Sstevel@tonic-gate  *   accounting file.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  *   The current implementation of acctctl() requires that the process and task
557c478bd9Sstevel@tonic-gate  *   and flow files be distinct across all zones.
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * Locking
587c478bd9Sstevel@tonic-gate  *   Each accounting species has an ac_info_t which contains a mutex,
597c478bd9Sstevel@tonic-gate  *   used to protect the ac_info_t's contents, and to serialize access to the
607c478bd9Sstevel@tonic-gate  *   appropriate file.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static list_t exacct_globals_list;
647c478bd9Sstevel@tonic-gate static kmutex_t exacct_globals_list_lock;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static int
677c478bd9Sstevel@tonic-gate ac_state_set(ac_info_t *info, void *buf, size_t bufsz)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	int state;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	if (buf == NULL || (bufsz != sizeof (int)))
727c478bd9Sstevel@tonic-gate 		return (EINVAL);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if (copyin(buf, &state, bufsz) != 0)
757c478bd9Sstevel@tonic-gate 		return (EFAULT);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if (state != AC_ON && state != AC_OFF)
787c478bd9Sstevel@tonic-gate 		return (EINVAL);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	mutex_enter(&info->ac_lock);
817c478bd9Sstevel@tonic-gate 	info->ac_state = state;
827c478bd9Sstevel@tonic-gate 	mutex_exit(&info->ac_lock);
837c478bd9Sstevel@tonic-gate 	return (0);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static int
877c478bd9Sstevel@tonic-gate ac_state_get(ac_info_t *info, void *buf, size_t bufsz)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	if (buf == NULL || (bufsz != sizeof (int)))
907c478bd9Sstevel@tonic-gate 		return (EINVAL);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	mutex_enter(&info->ac_lock);
937c478bd9Sstevel@tonic-gate 	if (copyout(&info->ac_state, buf, bufsz) != 0) {
947c478bd9Sstevel@tonic-gate 		mutex_exit(&info->ac_lock);
957c478bd9Sstevel@tonic-gate 		return (EFAULT);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	mutex_exit(&info->ac_lock);
987c478bd9Sstevel@tonic-gate 	return (0);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static boolean_t
1027c478bd9Sstevel@tonic-gate ac_file_in_use(vnode_t *vp)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	boolean_t in_use = B_FALSE;
1057c478bd9Sstevel@tonic-gate 	struct exacct_globals *acg;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (vp == NULL)
1087c478bd9Sstevel@tonic-gate 		return (B_FALSE);
1097c478bd9Sstevel@tonic-gate 	mutex_enter(&exacct_globals_list_lock);
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * Start off by grabbing all locks.
1127c478bd9Sstevel@tonic-gate 	 */
1137c478bd9Sstevel@tonic-gate 	for (acg = list_head(&exacct_globals_list); acg != NULL;
1147c478bd9Sstevel@tonic-gate 	    acg = list_next(&exacct_globals_list, acg)) {
1157c478bd9Sstevel@tonic-gate 		mutex_enter(&acg->ac_proc.ac_lock);
1167c478bd9Sstevel@tonic-gate 		mutex_enter(&acg->ac_task.ac_lock);
1177c478bd9Sstevel@tonic-gate 		mutex_enter(&acg->ac_flow.ac_lock);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	for (acg = list_head(&exacct_globals_list); !in_use && acg != NULL;
1217c478bd9Sstevel@tonic-gate 	    acg = list_next(&exacct_globals_list, acg)) {
1227c478bd9Sstevel@tonic-gate 		/*
1237c478bd9Sstevel@tonic-gate 		 * We need to verify that we aren't already using this file for
1247c478bd9Sstevel@tonic-gate 		 * accounting in any zone.
1257c478bd9Sstevel@tonic-gate 		 */
1267c478bd9Sstevel@tonic-gate 		if (vn_compare(acg->ac_proc.ac_vnode, vp) ||
1277c478bd9Sstevel@tonic-gate 		    vn_compare(acg->ac_task.ac_vnode, vp) ||
1287c478bd9Sstevel@tonic-gate 		    vn_compare(acg->ac_flow.ac_vnode, vp))
1297c478bd9Sstevel@tonic-gate 			in_use = B_TRUE;
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * Drop all locks.
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 	for (acg = list_head(&exacct_globals_list); acg != NULL;
1367c478bd9Sstevel@tonic-gate 	    acg = list_next(&exacct_globals_list, acg)) {
1377c478bd9Sstevel@tonic-gate 		mutex_exit(&acg->ac_proc.ac_lock);
1387c478bd9Sstevel@tonic-gate 		mutex_exit(&acg->ac_task.ac_lock);
1397c478bd9Sstevel@tonic-gate 		mutex_exit(&acg->ac_flow.ac_lock);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	mutex_exit(&exacct_globals_list_lock);
1427c478bd9Sstevel@tonic-gate 	return (in_use);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate static int
1467c478bd9Sstevel@tonic-gate ac_file_set(ac_info_t *info, void *ubuf, size_t bufsz)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	int error = 0;
1497c478bd9Sstevel@tonic-gate 	void *kbuf;
1507c478bd9Sstevel@tonic-gate 	void *namebuf;
1517c478bd9Sstevel@tonic-gate 	int namelen;
1527c478bd9Sstevel@tonic-gate 	vnode_t *vp;
1537c478bd9Sstevel@tonic-gate 	void *hdr;
1547c478bd9Sstevel@tonic-gate 	size_t hdrsize;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (ubuf == NULL) {
1577c478bd9Sstevel@tonic-gate 		mutex_enter(&info->ac_lock);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		/*
1607c478bd9Sstevel@tonic-gate 		 * Closing accounting file
1617c478bd9Sstevel@tonic-gate 		 */
1627c478bd9Sstevel@tonic-gate 		if (info->ac_vnode != NULL) {
163*da6c28aaSamw 			error = VOP_CLOSE(info->ac_vnode, FWRITE, 1, 0,
164*da6c28aaSamw 			    CRED(), NULL);
1657c478bd9Sstevel@tonic-gate 			if (error) {
1667c478bd9Sstevel@tonic-gate 				mutex_exit(&info->ac_lock);
1677c478bd9Sstevel@tonic-gate 				return (error);
1687c478bd9Sstevel@tonic-gate 			}
1697c478bd9Sstevel@tonic-gate 			VN_RELE(info->ac_vnode);
1707c478bd9Sstevel@tonic-gate 			info->ac_vnode = NULL;
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 		if (info->ac_file != NULL) {
1737c478bd9Sstevel@tonic-gate 			kmem_free(info->ac_file, strlen(info->ac_file) + 1);
1747c478bd9Sstevel@tonic-gate 			info->ac_file = NULL;
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 		mutex_exit(&info->ac_lock);
1787c478bd9Sstevel@tonic-gate 		return (error);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (bufsz < 2 || bufsz > MAXPATHLEN)
1827c478bd9Sstevel@tonic-gate 		return (EINVAL);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/*
1857c478bd9Sstevel@tonic-gate 	 * We have to copy in the whole buffer since we can't tell the length
1867c478bd9Sstevel@tonic-gate 	 * of the string in user's address space.
1877c478bd9Sstevel@tonic-gate 	 */
1887c478bd9Sstevel@tonic-gate 	kbuf = kmem_zalloc(bufsz, KM_SLEEP);
1897c478bd9Sstevel@tonic-gate 	if ((error = copyinstr((char *)ubuf, (char *)kbuf, bufsz, NULL)) != 0) {
1907c478bd9Sstevel@tonic-gate 		kmem_free(kbuf, bufsz);
1917c478bd9Sstevel@tonic-gate 		return (error);
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 	if (*((char *)kbuf) != '/') {
1947c478bd9Sstevel@tonic-gate 		kmem_free(kbuf, bufsz);
1957c478bd9Sstevel@tonic-gate 		return (EINVAL);
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/*
1997c478bd9Sstevel@tonic-gate 	 * Now, allocate the space where we are going to save the
2007c478bd9Sstevel@tonic-gate 	 * name of the accounting file and kmem_free kbuf. We have to do this
2017c478bd9Sstevel@tonic-gate 	 * now because it is not good to sleep in kmem_alloc() while
2027c478bd9Sstevel@tonic-gate 	 * holding ac_info's lock.
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 	namelen = strlen(kbuf) + 1;
2057c478bd9Sstevel@tonic-gate 	namebuf = kmem_alloc(namelen, KM_SLEEP);
2067c478bd9Sstevel@tonic-gate 	(void) strcpy(namebuf, kbuf);
2077c478bd9Sstevel@tonic-gate 	kmem_free(kbuf, bufsz);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * Check if this file already exists.
2117c478bd9Sstevel@tonic-gate 	 */
2127c478bd9Sstevel@tonic-gate 	error = lookupname(namebuf, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * Check if the file is already in use.
2167c478bd9Sstevel@tonic-gate 	 */
2177c478bd9Sstevel@tonic-gate 	if (!error) {
2187c478bd9Sstevel@tonic-gate 		if (ac_file_in_use(vp)) {
2197c478bd9Sstevel@tonic-gate 			/*
2207c478bd9Sstevel@tonic-gate 			 * If we're already using it then return EBUSY
2217c478bd9Sstevel@tonic-gate 			 */
2227c478bd9Sstevel@tonic-gate 			kmem_free(namebuf, namelen);
2237c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
2247c478bd9Sstevel@tonic-gate 			return (EBUSY);
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * Now, grab info's ac_lock and try to set up everything.
2317c478bd9Sstevel@tonic-gate 	 */
2327c478bd9Sstevel@tonic-gate 	mutex_enter(&info->ac_lock);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if ((error = vn_open(namebuf, UIO_SYSSPACE,
2357c478bd9Sstevel@tonic-gate 	    FCREAT | FWRITE | FTRUNC, 0600, &vp, CRCREAT, 0)) != 0) {
2367c478bd9Sstevel@tonic-gate 		mutex_exit(&info->ac_lock);
2377c478bd9Sstevel@tonic-gate 		kmem_free(namebuf, namelen);
2387c478bd9Sstevel@tonic-gate 		return (error);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG) {
2427c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
2437c478bd9Sstevel@tonic-gate 		mutex_exit(&info->ac_lock);
2447c478bd9Sstevel@tonic-gate 		kmem_free(namebuf, namelen);
2457c478bd9Sstevel@tonic-gate 		return (EACCES);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (info->ac_vnode != NULL) {
2497c478bd9Sstevel@tonic-gate 		/*
2507c478bd9Sstevel@tonic-gate 		 * Switch from an old file to a new file by swapping
2517c478bd9Sstevel@tonic-gate 		 * their vnode pointers.
2527c478bd9Sstevel@tonic-gate 		 */
2537c478bd9Sstevel@tonic-gate 		vnode_t *oldvp;
2547c478bd9Sstevel@tonic-gate 		oldvp = info->ac_vnode;
2557c478bd9Sstevel@tonic-gate 		info->ac_vnode = vp;
2567c478bd9Sstevel@tonic-gate 		vp = oldvp;
2577c478bd9Sstevel@tonic-gate 	} else {
2587c478bd9Sstevel@tonic-gate 		/*
2597c478bd9Sstevel@tonic-gate 		 * Start writing accounting records to a new file.
2607c478bd9Sstevel@tonic-gate 		 */
2617c478bd9Sstevel@tonic-gate 		info->ac_vnode = vp;
2627c478bd9Sstevel@tonic-gate 		vp = NULL;
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 	if (vp) {
2657c478bd9Sstevel@tonic-gate 		/*
2667c478bd9Sstevel@tonic-gate 		 * We still need to close the old file.
2677c478bd9Sstevel@tonic-gate 		 */
268*da6c28aaSamw 		if ((error = VOP_CLOSE(vp, FWRITE, 1, 0, CRED(), NULL)) != 0) {
2697c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
2707c478bd9Sstevel@tonic-gate 			mutex_exit(&info->ac_lock);
2717c478bd9Sstevel@tonic-gate 			kmem_free(namebuf, namelen);
2727c478bd9Sstevel@tonic-gate 			return (error);
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
2757c478bd9Sstevel@tonic-gate 		if (info->ac_file != NULL) {
2767c478bd9Sstevel@tonic-gate 			kmem_free(info->ac_file,
2777c478bd9Sstevel@tonic-gate 			    strlen(info->ac_file) + 1);
2787c478bd9Sstevel@tonic-gate 			info->ac_file = NULL;
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 	/*
2827c478bd9Sstevel@tonic-gate 	 * Finally, point ac_file to the filename string and release the lock.
2837c478bd9Sstevel@tonic-gate 	 */
2847c478bd9Sstevel@tonic-gate 	info->ac_file = namebuf;
2857c478bd9Sstevel@tonic-gate 	mutex_exit(&info->ac_lock);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/*
2887c478bd9Sstevel@tonic-gate 	 * Create and write an exacct header to the file.
2897c478bd9Sstevel@tonic-gate 	 */
2907c478bd9Sstevel@tonic-gate 	hdr = exacct_create_header(&hdrsize);
2917c478bd9Sstevel@tonic-gate 	error = exacct_write_header(info, hdr, hdrsize);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	return (error);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate static int
2977c478bd9Sstevel@tonic-gate ac_file_get(ac_info_t *info, void *buf, size_t bufsz)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	int error = 0;
3007c478bd9Sstevel@tonic-gate 	vnode_t *vnode;
3017c478bd9Sstevel@tonic-gate 	char *file;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	mutex_enter(&info->ac_lock);
3047c478bd9Sstevel@tonic-gate 	file = info->ac_file;
3057c478bd9Sstevel@tonic-gate 	vnode = info->ac_vnode;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	if (file == NULL || vnode == NULL) {
3087c478bd9Sstevel@tonic-gate 		mutex_exit(&info->ac_lock);
3097c478bd9Sstevel@tonic-gate 		return (ENOTACTIVE);
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	if (strlen(file) >= bufsz)
3137c478bd9Sstevel@tonic-gate 		error = ENOMEM;
3147c478bd9Sstevel@tonic-gate 	else
3157c478bd9Sstevel@tonic-gate 		error = copyoutstr(file, buf, MAXPATHLEN, NULL);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	mutex_exit(&info->ac_lock);
3187c478bd9Sstevel@tonic-gate 	return (error);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate static int
3227c478bd9Sstevel@tonic-gate ac_res_set(ac_info_t *info, void *buf, size_t bufsz, int maxres)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	ac_res_t *res;
3257c478bd9Sstevel@tonic-gate 	ac_res_t *tmp;
3267c478bd9Sstevel@tonic-gate 	ulong_t *maskp;
3277c478bd9Sstevel@tonic-gate 	int id;
3287c478bd9Sstevel@tonic-gate 	uint_t counter = 0;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * Validate that a non-zero buffer, sized within limits and to an
3327c478bd9Sstevel@tonic-gate 	 * integral number of ac_res_t's has been specified.
3337c478bd9Sstevel@tonic-gate 	 */
3347c478bd9Sstevel@tonic-gate 	if (bufsz == 0 ||
3357c478bd9Sstevel@tonic-gate 	    bufsz > sizeof (ac_res_t) * (AC_MAX_RES + 1) ||
3367c478bd9Sstevel@tonic-gate 	    (bufsz / sizeof (ac_res_t)) * sizeof (ac_res_t) != bufsz)
3377c478bd9Sstevel@tonic-gate 		return (EINVAL);
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	tmp = res = kmem_alloc(bufsz, KM_SLEEP);
3407c478bd9Sstevel@tonic-gate 	if (copyin(buf, res, bufsz) != 0) {
3417c478bd9Sstevel@tonic-gate 		kmem_free(res, bufsz);
3427c478bd9Sstevel@tonic-gate 		return (EFAULT);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	maskp = (ulong_t *)&info->ac_mask;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	mutex_enter(&info->ac_lock);
3487c478bd9Sstevel@tonic-gate 	while ((id = tmp->ar_id) != AC_NONE && counter < maxres + 1) {
3497c478bd9Sstevel@tonic-gate 		if (id > maxres || id < 0) {
3507c478bd9Sstevel@tonic-gate 			mutex_exit(&info->ac_lock);
3517c478bd9Sstevel@tonic-gate 			kmem_free(res, bufsz);
3527c478bd9Sstevel@tonic-gate 			return (EINVAL);
3537c478bd9Sstevel@tonic-gate 		}
3547c478bd9Sstevel@tonic-gate 		if (tmp->ar_state == AC_ON) {
3557c478bd9Sstevel@tonic-gate 			BT_SET(maskp, id);
3567c478bd9Sstevel@tonic-gate 		} else if (tmp->ar_state == AC_OFF) {
3577c478bd9Sstevel@tonic-gate 			BT_CLEAR(maskp, id);
3587c478bd9Sstevel@tonic-gate 		} else {
3597c478bd9Sstevel@tonic-gate 			mutex_exit(&info->ac_lock);
3607c478bd9Sstevel@tonic-gate 			kmem_free(res, bufsz);
3617c478bd9Sstevel@tonic-gate 			return (EINVAL);
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 		tmp++;
3647c478bd9Sstevel@tonic-gate 		counter++;
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 	mutex_exit(&info->ac_lock);
3677c478bd9Sstevel@tonic-gate 	kmem_free(res, bufsz);
3687c478bd9Sstevel@tonic-gate 	return (0);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate static int
3727c478bd9Sstevel@tonic-gate ac_res_get(ac_info_t *info, void *buf, size_t bufsz, int maxres)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	int error = 0;
3757c478bd9Sstevel@tonic-gate 	ac_res_t *res;
3767c478bd9Sstevel@tonic-gate 	ac_res_t *tmp;
3777c478bd9Sstevel@tonic-gate 	size_t ressz = sizeof (ac_res_t) * (maxres + 1);
3787c478bd9Sstevel@tonic-gate 	ulong_t *maskp;
3797c478bd9Sstevel@tonic-gate 	int id;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	if (bufsz < ressz)
3827c478bd9Sstevel@tonic-gate 		return (EINVAL);
3837c478bd9Sstevel@tonic-gate 	tmp = res = kmem_alloc(ressz, KM_SLEEP);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	mutex_enter(&info->ac_lock);
3867c478bd9Sstevel@tonic-gate 	maskp = (ulong_t *)&info->ac_mask;
3877c478bd9Sstevel@tonic-gate 	for (id = 1; id <= maxres; id++) {
3887c478bd9Sstevel@tonic-gate 		tmp->ar_id = id;
3897c478bd9Sstevel@tonic-gate 		tmp->ar_state = BT_TEST(maskp, id);
3907c478bd9Sstevel@tonic-gate 		tmp++;
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 	tmp->ar_id = AC_NONE;
3937c478bd9Sstevel@tonic-gate 	tmp->ar_state = AC_OFF;
3947c478bd9Sstevel@tonic-gate 	mutex_exit(&info->ac_lock);
3957c478bd9Sstevel@tonic-gate 	error = copyout(res, buf, ressz);
3967c478bd9Sstevel@tonic-gate 	kmem_free(res, ressz);
3977c478bd9Sstevel@tonic-gate 	return (error);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate  * acctctl()
4027c478bd9Sstevel@tonic-gate  *
4037c478bd9Sstevel@tonic-gate  * Overview
4047c478bd9Sstevel@tonic-gate  *   acctctl() is the entry point for the acctctl(2) system call.
4057c478bd9Sstevel@tonic-gate  *
4067c478bd9Sstevel@tonic-gate  * Return values
4077c478bd9Sstevel@tonic-gate  *   On successful completion, return 0; otherwise -1 is returned and errno is
4087c478bd9Sstevel@tonic-gate  *   set appropriately.
4097c478bd9Sstevel@tonic-gate  *
4107c478bd9Sstevel@tonic-gate  * Caller's context
4117c478bd9Sstevel@tonic-gate  *   Called from the system call path.
4127c478bd9Sstevel@tonic-gate  */
4137c478bd9Sstevel@tonic-gate int
4147c478bd9Sstevel@tonic-gate acctctl(int cmd, void *buf, size_t bufsz)
4157c478bd9Sstevel@tonic-gate {
4167c478bd9Sstevel@tonic-gate 	int error = 0;
4177c478bd9Sstevel@tonic-gate 	int mode = AC_MODE(cmd);
4187c478bd9Sstevel@tonic-gate 	int option = AC_OPTION(cmd);
4197c478bd9Sstevel@tonic-gate 	int maxres;
4207c478bd9Sstevel@tonic-gate 	ac_info_t *info;
4217c478bd9Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
4227c478bd9Sstevel@tonic-gate 	struct exacct_globals *acg;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	acg = zone_getspecific(exacct_zone_key, zone);
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * exacct_zone_key and associated per-zone state were initialized when
4277c478bd9Sstevel@tonic-gate 	 * the module was loaded.
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	ASSERT(exacct_zone_key != ZONE_KEY_UNINITIALIZED);
4307c478bd9Sstevel@tonic-gate 	ASSERT(acg != NULL);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	switch (mode) {	/* sanity check */
4337c478bd9Sstevel@tonic-gate 	case AC_TASK:
4347c478bd9Sstevel@tonic-gate 		info = &acg->ac_task;
4357c478bd9Sstevel@tonic-gate 		maxres = AC_TASK_MAX_RES;
4367c478bd9Sstevel@tonic-gate 		break;
4377c478bd9Sstevel@tonic-gate 	case AC_PROC:
4387c478bd9Sstevel@tonic-gate 		info = &acg->ac_proc;
4397c478bd9Sstevel@tonic-gate 		maxres = AC_PROC_MAX_RES;
4407c478bd9Sstevel@tonic-gate 		break;
4417c478bd9Sstevel@tonic-gate 	case AC_FLOW:
4427c478bd9Sstevel@tonic-gate 		/*
4437c478bd9Sstevel@tonic-gate 		 * Flow accounting isn't currently configurable in non-global
4447c478bd9Sstevel@tonic-gate 		 * zones, but we have this field on a per-zone basis for future
4457c478bd9Sstevel@tonic-gate 		 * expansion as well as the ability to return default "unset"
4467c478bd9Sstevel@tonic-gate 		 * values for the various AC_*_GET queries.  AC_*_SET commands
4477c478bd9Sstevel@tonic-gate 		 * fail with EPERM for AC_FLOW in non-global zones.
4487c478bd9Sstevel@tonic-gate 		 */
4497c478bd9Sstevel@tonic-gate 		info = &acg->ac_flow;
4507c478bd9Sstevel@tonic-gate 		maxres = AC_FLOW_MAX_RES;
4517c478bd9Sstevel@tonic-gate 		break;
4527c478bd9Sstevel@tonic-gate 	default:
4537c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	switch (option) {
4577c478bd9Sstevel@tonic-gate 	case AC_STATE_SET:
4587c478bd9Sstevel@tonic-gate 		if ((error = secpolicy_acct(CRED())) != 0)
4597c478bd9Sstevel@tonic-gate 			break;
4607c478bd9Sstevel@tonic-gate 		if (mode == AC_FLOW && getzoneid() != GLOBAL_ZONEID) {
4617c478bd9Sstevel@tonic-gate 			error = EPERM;
4627c478bd9Sstevel@tonic-gate 			break;
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 		error = ac_state_set(info, buf, bufsz);
4657c478bd9Sstevel@tonic-gate 		break;
4667c478bd9Sstevel@tonic-gate 	case AC_STATE_GET:
4677c478bd9Sstevel@tonic-gate 		error = ac_state_get(info, buf, bufsz);
4687c478bd9Sstevel@tonic-gate 		break;
4697c478bd9Sstevel@tonic-gate 	case AC_FILE_SET:
4707c478bd9Sstevel@tonic-gate 		if ((error = secpolicy_acct(CRED())) != 0)
4717c478bd9Sstevel@tonic-gate 			break;
4727c478bd9Sstevel@tonic-gate 		if (mode == AC_FLOW && getzoneid() != GLOBAL_ZONEID) {
4737c478bd9Sstevel@tonic-gate 			error = EPERM;
4747c478bd9Sstevel@tonic-gate 			break;
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 		error = ac_file_set(info, buf, bufsz);
4777c478bd9Sstevel@tonic-gate 		break;
4787c478bd9Sstevel@tonic-gate 	case AC_FILE_GET:
4797c478bd9Sstevel@tonic-gate 		error = ac_file_get(info, buf, bufsz);
4807c478bd9Sstevel@tonic-gate 		break;
4817c478bd9Sstevel@tonic-gate 	case AC_RES_SET:
4827c478bd9Sstevel@tonic-gate 		if ((error = secpolicy_acct(CRED())) != 0)
4837c478bd9Sstevel@tonic-gate 			break;
4847c478bd9Sstevel@tonic-gate 		if (mode == AC_FLOW && getzoneid() != GLOBAL_ZONEID) {
4857c478bd9Sstevel@tonic-gate 			error = EPERM;
4867c478bd9Sstevel@tonic-gate 			break;
4877c478bd9Sstevel@tonic-gate 		}
4887c478bd9Sstevel@tonic-gate 		error = ac_res_set(info, buf, bufsz, maxres);
4897c478bd9Sstevel@tonic-gate 		break;
4907c478bd9Sstevel@tonic-gate 	case AC_RES_GET:
4917c478bd9Sstevel@tonic-gate 		error = ac_res_get(info, buf, bufsz, maxres);
4927c478bd9Sstevel@tonic-gate 		break;
4937c478bd9Sstevel@tonic-gate 	default:
4947c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 	if (error)
4977c478bd9Sstevel@tonic-gate 		return (set_errno(error));
4987c478bd9Sstevel@tonic-gate 	return (0);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate static struct sysent ac_sysent = {
5027c478bd9Sstevel@tonic-gate 	3,
5037c478bd9Sstevel@tonic-gate 	SE_NOUNLOAD | SE_ARGC | SE_32RVAL1,
5047c478bd9Sstevel@tonic-gate 	acctctl
5057c478bd9Sstevel@tonic-gate };
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
5087c478bd9Sstevel@tonic-gate 	&mod_syscallops,
5097c478bd9Sstevel@tonic-gate 	"acctctl system call",
5107c478bd9Sstevel@tonic-gate 	&ac_sysent
5117c478bd9Sstevel@tonic-gate };
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
5147c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
5157c478bd9Sstevel@tonic-gate 	&mod_syscallops32,
5167c478bd9Sstevel@tonic-gate 	"32-bit acctctl system call",
5177c478bd9Sstevel@tonic-gate 	&ac_sysent
5187c478bd9Sstevel@tonic-gate };
5197c478bd9Sstevel@tonic-gate #endif
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
5227c478bd9Sstevel@tonic-gate 	MODREV_1,
5237c478bd9Sstevel@tonic-gate 	&modlsys,
5247c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
5257c478bd9Sstevel@tonic-gate 	&modlsys32,
5267c478bd9Sstevel@tonic-gate #endif
5277c478bd9Sstevel@tonic-gate 	NULL
5287c478bd9Sstevel@tonic-gate };
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate /* ARGSUSED */
5317c478bd9Sstevel@tonic-gate static void *
5327c478bd9Sstevel@tonic-gate exacct_zone_init(zoneid_t zoneid)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	struct exacct_globals *acg;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	acg = kmem_zalloc(sizeof (*acg), KM_SLEEP);
5377c478bd9Sstevel@tonic-gate 	mutex_enter(&exacct_globals_list_lock);
5387c478bd9Sstevel@tonic-gate 	list_insert_tail(&exacct_globals_list, acg);
5397c478bd9Sstevel@tonic-gate 	mutex_exit(&exacct_globals_list_lock);
5407c478bd9Sstevel@tonic-gate 	return (acg);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate static void
5447c478bd9Sstevel@tonic-gate exacct_free_info(ac_info_t *info)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate 	mutex_enter(&info->ac_lock);
5477c478bd9Sstevel@tonic-gate 	if (info->ac_vnode) {
548*da6c28aaSamw 		(void) VOP_CLOSE(info->ac_vnode, FWRITE, 1, 0, kcred, NULL);
5497c478bd9Sstevel@tonic-gate 		VN_RELE(info->ac_vnode);
5507c478bd9Sstevel@tonic-gate 		kmem_free(info->ac_file, strlen(info->ac_file) + 1);
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 	info->ac_state = AC_OFF;
5537c478bd9Sstevel@tonic-gate 	info->ac_vnode = NULL;
5547c478bd9Sstevel@tonic-gate 	info->ac_file = NULL;
5557c478bd9Sstevel@tonic-gate 	mutex_exit(&info->ac_lock);
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate /* ARGSUSED */
5597c478bd9Sstevel@tonic-gate static void
5607c478bd9Sstevel@tonic-gate exacct_zone_shutdown(zoneid_t zoneid, void *data)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	struct exacct_globals *acg = data;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/*
5657c478bd9Sstevel@tonic-gate 	 * The accounting files need to be closed during shutdown rather than
5667c478bd9Sstevel@tonic-gate 	 * destroy, since otherwise the filesystem they reside on may fail to
5677c478bd9Sstevel@tonic-gate 	 * unmount, thus causing the entire zone halt/reboot to fail.
5687c478bd9Sstevel@tonic-gate 	 */
5697c478bd9Sstevel@tonic-gate 	exacct_free_info(&acg->ac_proc);
5707c478bd9Sstevel@tonic-gate 	exacct_free_info(&acg->ac_task);
5717c478bd9Sstevel@tonic-gate 	exacct_free_info(&acg->ac_flow);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate /* ARGSUSED */
5757c478bd9Sstevel@tonic-gate static void
5767c478bd9Sstevel@tonic-gate exacct_zone_fini(zoneid_t zoneid, void *data)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate 	struct exacct_globals *acg = data;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	mutex_enter(&exacct_globals_list_lock);
5817c478bd9Sstevel@tonic-gate 	list_remove(&exacct_globals_list, acg);
5827c478bd9Sstevel@tonic-gate 	mutex_exit(&exacct_globals_list_lock);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	mutex_destroy(&acg->ac_proc.ac_lock);
5857c478bd9Sstevel@tonic-gate 	mutex_destroy(&acg->ac_task.ac_lock);
5867c478bd9Sstevel@tonic-gate 	mutex_destroy(&acg->ac_flow.ac_lock);
5877c478bd9Sstevel@tonic-gate 	kmem_free(acg, sizeof (*acg));
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate int
5917c478bd9Sstevel@tonic-gate _init()
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate 	int error;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	mutex_init(&exacct_globals_list_lock, NULL, MUTEX_DEFAULT, NULL);
5967c478bd9Sstevel@tonic-gate 	list_create(&exacct_globals_list, sizeof (struct exacct_globals),
5977c478bd9Sstevel@tonic-gate 	    offsetof(struct exacct_globals, ac_link));
5987c478bd9Sstevel@tonic-gate 	zone_key_create(&exacct_zone_key, exacct_zone_init,
5997c478bd9Sstevel@tonic-gate 	    exacct_zone_shutdown, exacct_zone_fini);
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0) {
6027c478bd9Sstevel@tonic-gate 		(void) zone_key_delete(exacct_zone_key);
6037c478bd9Sstevel@tonic-gate 		exacct_zone_key = ZONE_KEY_UNINITIALIZED;
6047c478bd9Sstevel@tonic-gate 		mutex_destroy(&exacct_globals_list_lock);
6057c478bd9Sstevel@tonic-gate 		list_destroy(&exacct_globals_list);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 	return (error);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate int
6117c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
6127c478bd9Sstevel@tonic-gate {
6137c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate int
6177c478bd9Sstevel@tonic-gate _fini()
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate 	return (EBUSY);
6207c478bd9Sstevel@tonic-gate }
621