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 /*
237c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/acctctl.h>
287c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
297c478bd9Sstevel@tonic-gate #include <sys/cred.h>
307c478bd9Sstevel@tonic-gate #include <sys/errno.h>
317c478bd9Sstevel@tonic-gate #include <sys/exacct.h>
327c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
337c478bd9Sstevel@tonic-gate #include <sys/procset.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/task.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/user.h>
397c478bd9Sstevel@tonic-gate #include <sys/policy.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * getacct(2), putacct(2), and wracct(2) system calls
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  *   The extended accounting subsystem provides three root-privileged system
457c478bd9Sstevel@tonic-gate  *   calls for interacting with the actual resource data associated with each
467c478bd9Sstevel@tonic-gate  *   task or process.  getacct() copies a packed exacct record reflecting the
477c478bd9Sstevel@tonic-gate  *   resource usage out to the buffer provided by the user.  wracct() writes a
487c478bd9Sstevel@tonic-gate  *   record to the appropriate extended accounting file.  putacct() takes the
497c478bd9Sstevel@tonic-gate  *   buffer provided by the user, and appends a "tag" record associated with the
507c478bd9Sstevel@tonic-gate  *   specified task or project that encapsulates the user data.  All three of
517c478bd9Sstevel@tonic-gate  *   these functions exit early if extended accounting is not active for the
527c478bd9Sstevel@tonic-gate  *   requested entity type.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * Locking
557c478bd9Sstevel@tonic-gate  *   Under the terminology introduced in os/task.c, all three of these system
567c478bd9Sstevel@tonic-gate  *   calls are task observers, when executing on an existing task.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * getacct_callback() is used to copyout the buffer with accounting records
617c478bd9Sstevel@tonic-gate  * from the kernel back to the user. It also sets actual to the size of the
627c478bd9Sstevel@tonic-gate  * kernel buffer--the required minimum size for a successful outbound copy.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate /* ARGSUSED */
657c478bd9Sstevel@tonic-gate static int
getacct_callback(ac_info_t * unused,void * ubuf,size_t usize,void * kbuf,size_t ksize,size_t * actual)667c478bd9Sstevel@tonic-gate getacct_callback(ac_info_t *unused, void *ubuf, size_t usize, void *kbuf,
677c478bd9Sstevel@tonic-gate     size_t ksize, size_t *actual)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	size_t size = MIN(usize, ksize);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	if (ubuf != NULL && copyout(kbuf, ubuf, size) != 0)
727c478bd9Sstevel@tonic-gate 		return (EFAULT);
737c478bd9Sstevel@tonic-gate 	*actual = ksize;
747c478bd9Sstevel@tonic-gate 	return (0);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static int
getacct_task(ac_info_t * ac_task,taskid_t tkid,void * buf,size_t bufsize,size_t * sizep)787c478bd9Sstevel@tonic-gate getacct_task(ac_info_t *ac_task, taskid_t tkid, void *buf, size_t bufsize,
797c478bd9Sstevel@tonic-gate     size_t *sizep)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	task_t *tk;
827c478bd9Sstevel@tonic-gate 	int error;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	mutex_enter(&ac_task->ac_lock);
857c478bd9Sstevel@tonic-gate 	if (ac_task->ac_state == AC_OFF) {
867c478bd9Sstevel@tonic-gate 		mutex_exit(&ac_task->ac_lock);
877c478bd9Sstevel@tonic-gate 		return (ENOTACTIVE);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 	mutex_exit(&ac_task->ac_lock);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if ((tk = task_hold_by_id(tkid)) == NULL)
927c478bd9Sstevel@tonic-gate 		return (ESRCH);
937c478bd9Sstevel@tonic-gate 	error = exacct_assemble_task_usage(ac_task, tk,
947c478bd9Sstevel@tonic-gate 	    getacct_callback, buf, bufsize, sizep, EW_PARTIAL);
957c478bd9Sstevel@tonic-gate 	task_rele(tk);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	return (error);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static int
getacct_proc(ac_info_t * ac_proc,pid_t pid,void * buf,size_t bufsize,size_t * sizep)1017c478bd9Sstevel@tonic-gate getacct_proc(ac_info_t *ac_proc, pid_t pid, void *buf, size_t bufsize,
1027c478bd9Sstevel@tonic-gate     size_t *sizep)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	proc_t *p;
1057c478bd9Sstevel@tonic-gate 	proc_usage_t *pu;
1067c478bd9Sstevel@tonic-gate 	ulong_t mask[AC_MASK_SZ];
1077c478bd9Sstevel@tonic-gate 	ulong_t *ac_mask = &mask[0];
1087c478bd9Sstevel@tonic-gate 	int error;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	mutex_enter(&ac_proc->ac_lock);
1117c478bd9Sstevel@tonic-gate 	if (ac_proc->ac_state == AC_OFF) {
1127c478bd9Sstevel@tonic-gate 		mutex_exit(&ac_proc->ac_lock);
1137c478bd9Sstevel@tonic-gate 		return (ENOTACTIVE);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	bt_copy(&ac_proc->ac_mask[0], ac_mask, AC_MASK_SZ);
1167c478bd9Sstevel@tonic-gate 	mutex_exit(&ac_proc->ac_lock);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	pu = kmem_zalloc(sizeof (proc_usage_t), KM_SLEEP);
1197c478bd9Sstevel@tonic-gate 	pu->pu_command = kmem_zalloc(MAXCOMLEN + 1, KM_SLEEP);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
1227c478bd9Sstevel@tonic-gate 	if ((p = prfind(pid)) == NULL) {
1237c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
1247c478bd9Sstevel@tonic-gate 		kmem_free(pu->pu_command, MAXCOMLEN + 1);
1257c478bd9Sstevel@tonic-gate 		kmem_free(pu, sizeof (proc_usage_t));
1267c478bd9Sstevel@tonic-gate 		return (ESRCH);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1297c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	exacct_calculate_proc_usage(p, pu, ac_mask, EW_PARTIAL, 0);
1327c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	error = exacct_assemble_proc_usage(ac_proc, pu,
1357c478bd9Sstevel@tonic-gate 	    getacct_callback, buf, bufsize, sizep, EW_PARTIAL);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	kmem_free(pu->pu_command, MAXCOMLEN + 1);
1387c478bd9Sstevel@tonic-gate 	kmem_free(pu, sizeof (proc_usage_t));
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	return (error);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static ssize_t
getacct(idtype_t idtype,id_t id,void * buf,size_t bufsize)1447c478bd9Sstevel@tonic-gate getacct(idtype_t idtype, id_t id, void *buf, size_t bufsize)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	size_t size = 0;
1477c478bd9Sstevel@tonic-gate 	int error;
1487c478bd9Sstevel@tonic-gate 	struct exacct_globals *acg;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (bufsize > EXACCT_MAX_BUFSIZE)
1517c478bd9Sstevel@tonic-gate 		bufsize = EXACCT_MAX_BUFSIZE;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	acg = zone_getspecific(exacct_zone_key, curproc->p_zone);
1547c478bd9Sstevel@tonic-gate 	switch (idtype) {
1557c478bd9Sstevel@tonic-gate 	case P_PID:
1567c478bd9Sstevel@tonic-gate 		error = getacct_proc(&acg->ac_proc, id, buf, bufsize, &size);
1577c478bd9Sstevel@tonic-gate 		break;
1587c478bd9Sstevel@tonic-gate 	case P_TASKID:
1597c478bd9Sstevel@tonic-gate 		error = getacct_task(&acg->ac_task, id, buf, bufsize, &size);
1607c478bd9Sstevel@tonic-gate 		break;
1617c478bd9Sstevel@tonic-gate 	default:
1627c478bd9Sstevel@tonic-gate 		error = EINVAL;
1637c478bd9Sstevel@tonic-gate 		break;
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	return (error == 0 ? (ssize_t)size : set_errno(error));
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate static int
putacct(idtype_t idtype,id_t id,void * buf,size_t bufsize,int flags)1697c478bd9Sstevel@tonic-gate putacct(idtype_t idtype, id_t id, void *buf, size_t bufsize, int flags)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	int error;
1727c478bd9Sstevel@tonic-gate 	taskid_t tkid;
1737c478bd9Sstevel@tonic-gate 	proc_t *p;
1747c478bd9Sstevel@tonic-gate 	task_t *tk;
1757c478bd9Sstevel@tonic-gate 	void *kbuf;
1767c478bd9Sstevel@tonic-gate 	struct exacct_globals *acg;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (bufsize == 0 || bufsize > EXACCT_MAX_BUFSIZE)
1797c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	kbuf = kmem_alloc(bufsize, KM_SLEEP);
1827c478bd9Sstevel@tonic-gate 	if (copyin(buf, kbuf, bufsize) != 0) {
1837c478bd9Sstevel@tonic-gate 		error = EFAULT;
1847c478bd9Sstevel@tonic-gate 		goto out;
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	acg = zone_getspecific(exacct_zone_key, curproc->p_zone);
1887c478bd9Sstevel@tonic-gate 	switch (idtype) {
1897c478bd9Sstevel@tonic-gate 	case P_PID:
1907c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
1917c478bd9Sstevel@tonic-gate 		if ((p = prfind(id)) == NULL) {
1927c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
1937c478bd9Sstevel@tonic-gate 			error = ESRCH;
1947c478bd9Sstevel@tonic-gate 		} else {
1957c478bd9Sstevel@tonic-gate 			zone_t *zone = p->p_zone;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 			tkid = p->p_task->tk_tkid;
1987c478bd9Sstevel@tonic-gate 			zone_hold(zone);
1997c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 			error = exacct_tag_proc(&acg->ac_proc, id, tkid, kbuf,
2027c478bd9Sstevel@tonic-gate 			    bufsize, flags, zone->zone_nodename);
2037c478bd9Sstevel@tonic-gate 			zone_rele(zone);
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 		break;
2067c478bd9Sstevel@tonic-gate 	case P_TASKID:
2077c478bd9Sstevel@tonic-gate 		if ((tk = task_hold_by_id(id)) != NULL) {
2087c478bd9Sstevel@tonic-gate 			error = exacct_tag_task(&acg->ac_task, tk, kbuf,
2097c478bd9Sstevel@tonic-gate 			    bufsize, flags);
2107c478bd9Sstevel@tonic-gate 			task_rele(tk);
2117c478bd9Sstevel@tonic-gate 		} else {
2127c478bd9Sstevel@tonic-gate 			error = ESRCH;
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 		break;
2157c478bd9Sstevel@tonic-gate 	default:
2167c478bd9Sstevel@tonic-gate 		error = EINVAL;
2177c478bd9Sstevel@tonic-gate 		break;
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate out:
2207c478bd9Sstevel@tonic-gate 	kmem_free(kbuf, bufsize);
2217c478bd9Sstevel@tonic-gate 	return (error == 0 ? error : set_errno(error));
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate static int
wracct_task(ac_info_t * ac_task,taskid_t tkid,int flag,size_t * sizep)2257c478bd9Sstevel@tonic-gate wracct_task(ac_info_t *ac_task, taskid_t tkid, int flag, size_t *sizep)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	task_t *tk;
2287c478bd9Sstevel@tonic-gate 	int error;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	mutex_enter(&ac_task->ac_lock);
2317c478bd9Sstevel@tonic-gate 	if (ac_task->ac_state == AC_OFF || ac_task->ac_vnode == NULL) {
2327c478bd9Sstevel@tonic-gate 		mutex_exit(&ac_task->ac_lock);
2337c478bd9Sstevel@tonic-gate 		return (ENOTACTIVE);
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 	mutex_exit(&ac_task->ac_lock);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if ((tk = task_hold_by_id(tkid)) == NULL)
2387c478bd9Sstevel@tonic-gate 		return (ESRCH);
2397c478bd9Sstevel@tonic-gate 	error = exacct_assemble_task_usage(ac_task, tk, exacct_commit_callback,
2407c478bd9Sstevel@tonic-gate 	    NULL, 0, sizep, flag);
2417c478bd9Sstevel@tonic-gate 	task_rele(tk);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	return (error);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate static int
wracct_proc(ac_info_t * ac_proc,pid_t pid,int flag,size_t * sizep)2477c478bd9Sstevel@tonic-gate wracct_proc(ac_info_t *ac_proc, pid_t pid, int flag, size_t *sizep)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	proc_t *p;
2507c478bd9Sstevel@tonic-gate 	proc_usage_t *pu;
2517c478bd9Sstevel@tonic-gate 	ulong_t mask[AC_MASK_SZ];
2527c478bd9Sstevel@tonic-gate 	ulong_t *ac_mask = &mask[0];
2537c478bd9Sstevel@tonic-gate 	int error;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	mutex_enter(&ac_proc->ac_lock);
2567c478bd9Sstevel@tonic-gate 	if (ac_proc->ac_state == AC_OFF || ac_proc->ac_vnode == NULL) {
2577c478bd9Sstevel@tonic-gate 		mutex_exit(&ac_proc->ac_lock);
2587c478bd9Sstevel@tonic-gate 		return (ENOTACTIVE);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 	bt_copy(&ac_proc->ac_mask[0], ac_mask, AC_MASK_SZ);
2617c478bd9Sstevel@tonic-gate 	mutex_exit(&ac_proc->ac_lock);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	pu = kmem_zalloc(sizeof (proc_usage_t), KM_SLEEP);
2647c478bd9Sstevel@tonic-gate 	pu->pu_command = kmem_zalloc(MAXCOMLEN + 1, KM_SLEEP);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
2677c478bd9Sstevel@tonic-gate 	if ((p = prfind(pid)) == NULL) {
2687c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
2697c478bd9Sstevel@tonic-gate 		kmem_free(pu->pu_command, MAXCOMLEN + 1);
2707c478bd9Sstevel@tonic-gate 		kmem_free(pu, sizeof (proc_usage_t));
2717c478bd9Sstevel@tonic-gate 		return (ESRCH);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2747c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
2757c478bd9Sstevel@tonic-gate 	exacct_calculate_proc_usage(p, pu, ac_mask, flag, 0);
2767c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	error = exacct_assemble_proc_usage(ac_proc, pu,
2797c478bd9Sstevel@tonic-gate 	    exacct_commit_callback, NULL, 0, sizep, flag);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	kmem_free(pu->pu_command, MAXCOMLEN + 1);
2827c478bd9Sstevel@tonic-gate 	kmem_free(pu, sizeof (proc_usage_t));
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	return (error);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate static int
wracct(idtype_t idtype,id_t id,int flags)2887c478bd9Sstevel@tonic-gate wracct(idtype_t idtype, id_t id, int flags)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	int error;
2917c478bd9Sstevel@tonic-gate 	size_t size = 0;
2927c478bd9Sstevel@tonic-gate 	struct exacct_globals *acg;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	/*
2957c478bd9Sstevel@tonic-gate 	 * Validate flags.
2967c478bd9Sstevel@tonic-gate 	 */
2977c478bd9Sstevel@tonic-gate 	switch (flags) {
2987c478bd9Sstevel@tonic-gate 	case EW_PARTIAL:
2997c478bd9Sstevel@tonic-gate 	case EW_INTERVAL:
3007c478bd9Sstevel@tonic-gate 		break;
3017c478bd9Sstevel@tonic-gate 	default:
3027c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	acg = zone_getspecific(exacct_zone_key, curproc->p_zone);
3067c478bd9Sstevel@tonic-gate 	switch (idtype) {
3077c478bd9Sstevel@tonic-gate 	case P_PID:
3087c478bd9Sstevel@tonic-gate 		if (flags == EW_INTERVAL)
3097c478bd9Sstevel@tonic-gate 			return (set_errno(ENOTSUP));
3107c478bd9Sstevel@tonic-gate 		error = wracct_proc(&acg->ac_proc, id, flags, &size);
3117c478bd9Sstevel@tonic-gate 		break;
3127c478bd9Sstevel@tonic-gate 	case P_TASKID:
3137c478bd9Sstevel@tonic-gate 		error = wracct_task(&acg->ac_task, id, flags, &size);
3147c478bd9Sstevel@tonic-gate 		break;
3157c478bd9Sstevel@tonic-gate 	default:
3167c478bd9Sstevel@tonic-gate 		error = EINVAL;
3177c478bd9Sstevel@tonic-gate 		break;
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	return (error == 0 ? error : set_errno(error));
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate static long
exacct(int code,idtype_t idtype,id_t id,void * buf,size_t bufsize,int flags)3247c478bd9Sstevel@tonic-gate exacct(int code, idtype_t idtype, id_t id, void *buf, size_t bufsize,
3257c478bd9Sstevel@tonic-gate     int flags)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	if (secpolicy_acct(CRED()) != 0)
3287c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (exacct_zone_key == ZONE_KEY_UNINITIALIZED)
3317c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTACTIVE));
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	switch (code) {
3347c478bd9Sstevel@tonic-gate 	case 0:
3357c478bd9Sstevel@tonic-gate 		return (getacct(idtype, id, buf, bufsize));
3367c478bd9Sstevel@tonic-gate 	case 1:
3377c478bd9Sstevel@tonic-gate 		return (putacct(idtype, id, buf, bufsize, flags));
3387c478bd9Sstevel@tonic-gate 	case 2:
3397c478bd9Sstevel@tonic-gate 		return (wracct(idtype, id, flags));
3407c478bd9Sstevel@tonic-gate 	default:
3417c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate #if defined(_LP64)
3467c478bd9Sstevel@tonic-gate #define	SE_LRVAL	SE_64RVAL
3477c478bd9Sstevel@tonic-gate #else
3487c478bd9Sstevel@tonic-gate #define	SE_LRVAL	SE_32RVAL1
3497c478bd9Sstevel@tonic-gate #endif
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate static struct sysent exacctsys_sysent = {
3527c478bd9Sstevel@tonic-gate 	6,
3537c478bd9Sstevel@tonic-gate 	SE_NOUNLOAD | SE_ARGC | SE_LRVAL,
354*fe9b1195SToomas Soome 	(int (*)())(uintptr_t)exacct
3557c478bd9Sstevel@tonic-gate };
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
3587c478bd9Sstevel@tonic-gate 	&mod_syscallops,
3597c478bd9Sstevel@tonic-gate 	"extended accounting facility",
3607c478bd9Sstevel@tonic-gate 	&exacctsys_sysent
3617c478bd9Sstevel@tonic-gate };
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate static struct sysent exacctsys_sysent32 = {
3667c478bd9Sstevel@tonic-gate 	6,
3677c478bd9Sstevel@tonic-gate 	SE_NOUNLOAD | SE_ARGC | SE_32RVAL1,
368*fe9b1195SToomas Soome 	(int (*)())(uintptr_t)exacct
3697c478bd9Sstevel@tonic-gate };
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
3727c478bd9Sstevel@tonic-gate 	&mod_syscallops32,
3737c478bd9Sstevel@tonic-gate 	"32-bit extended accounting facility",
3747c478bd9Sstevel@tonic-gate 	&exacctsys_sysent32
3757c478bd9Sstevel@tonic-gate };
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate #endif
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
3807c478bd9Sstevel@tonic-gate 	MODREV_1,
3817c478bd9Sstevel@tonic-gate 	&modlsys,
3827c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
3837c478bd9Sstevel@tonic-gate 	&modlsys32,
3847c478bd9Sstevel@tonic-gate #endif
3857c478bd9Sstevel@tonic-gate 	NULL
3867c478bd9Sstevel@tonic-gate };
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate int
_init(void)3897c478bd9Sstevel@tonic-gate _init(void)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate int
_fini(void)3957c478bd9Sstevel@tonic-gate _fini(void)
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate int
_info(struct modinfo * mip)4017c478bd9Sstevel@tonic-gate _info(struct modinfo *mip)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, mip));
4047c478bd9Sstevel@tonic-gate }
405