xref: /titanic_51/usr/src/uts/common/syscall/exacctsys.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/acctctl.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/exacct.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/procset.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/task.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/policy.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  * getacct(2), putacct(2), and wracct(2) system calls
45*7c478bd9Sstevel@tonic-gate  *
46*7c478bd9Sstevel@tonic-gate  *   The extended accounting subsystem provides three root-privileged system
47*7c478bd9Sstevel@tonic-gate  *   calls for interacting with the actual resource data associated with each
48*7c478bd9Sstevel@tonic-gate  *   task or process.  getacct() copies a packed exacct record reflecting the
49*7c478bd9Sstevel@tonic-gate  *   resource usage out to the buffer provided by the user.  wracct() writes a
50*7c478bd9Sstevel@tonic-gate  *   record to the appropriate extended accounting file.  putacct() takes the
51*7c478bd9Sstevel@tonic-gate  *   buffer provided by the user, and appends a "tag" record associated with the
52*7c478bd9Sstevel@tonic-gate  *   specified task or project that encapsulates the user data.  All three of
53*7c478bd9Sstevel@tonic-gate  *   these functions exit early if extended accounting is not active for the
54*7c478bd9Sstevel@tonic-gate  *   requested entity type.
55*7c478bd9Sstevel@tonic-gate  *
56*7c478bd9Sstevel@tonic-gate  * Locking
57*7c478bd9Sstevel@tonic-gate  *   Under the terminology introduced in os/task.c, all three of these system
58*7c478bd9Sstevel@tonic-gate  *   calls are task observers, when executing on an existing task.
59*7c478bd9Sstevel@tonic-gate  */
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate  * getacct_callback() is used to copyout the buffer with accounting records
63*7c478bd9Sstevel@tonic-gate  * from the kernel back to the user. It also sets actual to the size of the
64*7c478bd9Sstevel@tonic-gate  * kernel buffer--the required minimum size for a successful outbound copy.
65*7c478bd9Sstevel@tonic-gate  */
66*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
67*7c478bd9Sstevel@tonic-gate static int
68*7c478bd9Sstevel@tonic-gate getacct_callback(ac_info_t *unused, void *ubuf, size_t usize, void *kbuf,
69*7c478bd9Sstevel@tonic-gate     size_t ksize, size_t *actual)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	size_t size = MIN(usize, ksize);
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	if (ubuf != NULL && copyout(kbuf, ubuf, size) != 0)
74*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
75*7c478bd9Sstevel@tonic-gate 	*actual = ksize;
76*7c478bd9Sstevel@tonic-gate 	return (0);
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate static int
80*7c478bd9Sstevel@tonic-gate getacct_task(ac_info_t *ac_task, taskid_t tkid, void *buf, size_t bufsize,
81*7c478bd9Sstevel@tonic-gate     size_t *sizep)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	task_t *tk;
84*7c478bd9Sstevel@tonic-gate 	int error;
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ac_task->ac_lock);
87*7c478bd9Sstevel@tonic-gate 	if (ac_task->ac_state == AC_OFF) {
88*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ac_task->ac_lock);
89*7c478bd9Sstevel@tonic-gate 		return (ENOTACTIVE);
90*7c478bd9Sstevel@tonic-gate 	}
91*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ac_task->ac_lock);
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	if ((tk = task_hold_by_id(tkid)) == NULL)
94*7c478bd9Sstevel@tonic-gate 		return (ESRCH);
95*7c478bd9Sstevel@tonic-gate 	error = exacct_assemble_task_usage(ac_task, tk,
96*7c478bd9Sstevel@tonic-gate 	    getacct_callback, buf, bufsize, sizep, EW_PARTIAL);
97*7c478bd9Sstevel@tonic-gate 	task_rele(tk);
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	return (error);
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate static int
103*7c478bd9Sstevel@tonic-gate getacct_proc(ac_info_t *ac_proc, pid_t pid, void *buf, size_t bufsize,
104*7c478bd9Sstevel@tonic-gate     size_t *sizep)
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate 	proc_t *p;
107*7c478bd9Sstevel@tonic-gate 	proc_usage_t *pu;
108*7c478bd9Sstevel@tonic-gate 	ulong_t mask[AC_MASK_SZ];
109*7c478bd9Sstevel@tonic-gate 	ulong_t *ac_mask = &mask[0];
110*7c478bd9Sstevel@tonic-gate 	int error;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ac_proc->ac_lock);
113*7c478bd9Sstevel@tonic-gate 	if (ac_proc->ac_state == AC_OFF) {
114*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ac_proc->ac_lock);
115*7c478bd9Sstevel@tonic-gate 		return (ENOTACTIVE);
116*7c478bd9Sstevel@tonic-gate 	}
117*7c478bd9Sstevel@tonic-gate 	bt_copy(&ac_proc->ac_mask[0], ac_mask, AC_MASK_SZ);
118*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ac_proc->ac_lock);
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	pu = kmem_zalloc(sizeof (proc_usage_t), KM_SLEEP);
121*7c478bd9Sstevel@tonic-gate 	pu->pu_command = kmem_zalloc(MAXCOMLEN + 1, KM_SLEEP);
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
124*7c478bd9Sstevel@tonic-gate 	if ((p = prfind(pid)) == NULL) {
125*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
126*7c478bd9Sstevel@tonic-gate 		kmem_free(pu->pu_command, MAXCOMLEN + 1);
127*7c478bd9Sstevel@tonic-gate 		kmem_free(pu, sizeof (proc_usage_t));
128*7c478bd9Sstevel@tonic-gate 		return (ESRCH);
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
131*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	exacct_calculate_proc_usage(p, pu, ac_mask, EW_PARTIAL, 0);
134*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	error = exacct_assemble_proc_usage(ac_proc, pu,
137*7c478bd9Sstevel@tonic-gate 	    getacct_callback, buf, bufsize, sizep, EW_PARTIAL);
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	kmem_free(pu->pu_command, MAXCOMLEN + 1);
140*7c478bd9Sstevel@tonic-gate 	kmem_free(pu, sizeof (proc_usage_t));
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	return (error);
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate static ssize_t
146*7c478bd9Sstevel@tonic-gate getacct(idtype_t idtype, id_t id, void *buf, size_t bufsize)
147*7c478bd9Sstevel@tonic-gate {
148*7c478bd9Sstevel@tonic-gate 	size_t size = 0;
149*7c478bd9Sstevel@tonic-gate 	int error;
150*7c478bd9Sstevel@tonic-gate 	struct exacct_globals *acg;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	if (bufsize > EXACCT_MAX_BUFSIZE)
153*7c478bd9Sstevel@tonic-gate 		bufsize = EXACCT_MAX_BUFSIZE;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	acg = zone_getspecific(exacct_zone_key, curproc->p_zone);
156*7c478bd9Sstevel@tonic-gate 	switch (idtype) {
157*7c478bd9Sstevel@tonic-gate 	case P_PID:
158*7c478bd9Sstevel@tonic-gate 		error = getacct_proc(&acg->ac_proc, id, buf, bufsize, &size);
159*7c478bd9Sstevel@tonic-gate 		break;
160*7c478bd9Sstevel@tonic-gate 	case P_TASKID:
161*7c478bd9Sstevel@tonic-gate 		error = getacct_task(&acg->ac_task, id, buf, bufsize, &size);
162*7c478bd9Sstevel@tonic-gate 		break;
163*7c478bd9Sstevel@tonic-gate 	default:
164*7c478bd9Sstevel@tonic-gate 		error = EINVAL;
165*7c478bd9Sstevel@tonic-gate 		break;
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate 	return (error == 0 ? (ssize_t)size : set_errno(error));
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate static int
171*7c478bd9Sstevel@tonic-gate putacct(idtype_t idtype, id_t id, void *buf, size_t bufsize, int flags)
172*7c478bd9Sstevel@tonic-gate {
173*7c478bd9Sstevel@tonic-gate 	int error;
174*7c478bd9Sstevel@tonic-gate 	taskid_t tkid;
175*7c478bd9Sstevel@tonic-gate 	proc_t *p;
176*7c478bd9Sstevel@tonic-gate 	task_t *tk;
177*7c478bd9Sstevel@tonic-gate 	void *kbuf;
178*7c478bd9Sstevel@tonic-gate 	struct exacct_globals *acg;
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	if (bufsize == 0 || bufsize > EXACCT_MAX_BUFSIZE)
181*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	kbuf = kmem_alloc(bufsize, KM_SLEEP);
184*7c478bd9Sstevel@tonic-gate 	if (copyin(buf, kbuf, bufsize) != 0) {
185*7c478bd9Sstevel@tonic-gate 		error = EFAULT;
186*7c478bd9Sstevel@tonic-gate 		goto out;
187*7c478bd9Sstevel@tonic-gate 	}
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	acg = zone_getspecific(exacct_zone_key, curproc->p_zone);
190*7c478bd9Sstevel@tonic-gate 	switch (idtype) {
191*7c478bd9Sstevel@tonic-gate 	case P_PID:
192*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
193*7c478bd9Sstevel@tonic-gate 		if ((p = prfind(id)) == NULL) {
194*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
195*7c478bd9Sstevel@tonic-gate 			error = ESRCH;
196*7c478bd9Sstevel@tonic-gate 		} else {
197*7c478bd9Sstevel@tonic-gate 			zone_t *zone = p->p_zone;
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 			tkid = p->p_task->tk_tkid;
200*7c478bd9Sstevel@tonic-gate 			zone_hold(zone);
201*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 			error = exacct_tag_proc(&acg->ac_proc, id, tkid, kbuf,
204*7c478bd9Sstevel@tonic-gate 			    bufsize, flags, zone->zone_nodename);
205*7c478bd9Sstevel@tonic-gate 			zone_rele(zone);
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 		break;
208*7c478bd9Sstevel@tonic-gate 	case P_TASKID:
209*7c478bd9Sstevel@tonic-gate 		if ((tk = task_hold_by_id(id)) != NULL) {
210*7c478bd9Sstevel@tonic-gate 			error = exacct_tag_task(&acg->ac_task, tk, kbuf,
211*7c478bd9Sstevel@tonic-gate 			    bufsize, flags);
212*7c478bd9Sstevel@tonic-gate 			task_rele(tk);
213*7c478bd9Sstevel@tonic-gate 		} else {
214*7c478bd9Sstevel@tonic-gate 			error = ESRCH;
215*7c478bd9Sstevel@tonic-gate 		}
216*7c478bd9Sstevel@tonic-gate 		break;
217*7c478bd9Sstevel@tonic-gate 	default:
218*7c478bd9Sstevel@tonic-gate 		error = EINVAL;
219*7c478bd9Sstevel@tonic-gate 		break;
220*7c478bd9Sstevel@tonic-gate 	}
221*7c478bd9Sstevel@tonic-gate out:
222*7c478bd9Sstevel@tonic-gate 	kmem_free(kbuf, bufsize);
223*7c478bd9Sstevel@tonic-gate 	return (error == 0 ? error : set_errno(error));
224*7c478bd9Sstevel@tonic-gate }
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate static int
227*7c478bd9Sstevel@tonic-gate wracct_task(ac_info_t *ac_task, taskid_t tkid, int flag, size_t *sizep)
228*7c478bd9Sstevel@tonic-gate {
229*7c478bd9Sstevel@tonic-gate 	task_t *tk;
230*7c478bd9Sstevel@tonic-gate 	int error;
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ac_task->ac_lock);
233*7c478bd9Sstevel@tonic-gate 	if (ac_task->ac_state == AC_OFF || ac_task->ac_vnode == NULL) {
234*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ac_task->ac_lock);
235*7c478bd9Sstevel@tonic-gate 		return (ENOTACTIVE);
236*7c478bd9Sstevel@tonic-gate 	}
237*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ac_task->ac_lock);
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	if ((tk = task_hold_by_id(tkid)) == NULL)
240*7c478bd9Sstevel@tonic-gate 		return (ESRCH);
241*7c478bd9Sstevel@tonic-gate 	error = exacct_assemble_task_usage(ac_task, tk, exacct_commit_callback,
242*7c478bd9Sstevel@tonic-gate 	    NULL, 0, sizep, flag);
243*7c478bd9Sstevel@tonic-gate 	task_rele(tk);
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	return (error);
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate static int
249*7c478bd9Sstevel@tonic-gate wracct_proc(ac_info_t *ac_proc, pid_t pid, int flag, size_t *sizep)
250*7c478bd9Sstevel@tonic-gate {
251*7c478bd9Sstevel@tonic-gate 	proc_t *p;
252*7c478bd9Sstevel@tonic-gate 	proc_usage_t *pu;
253*7c478bd9Sstevel@tonic-gate 	ulong_t mask[AC_MASK_SZ];
254*7c478bd9Sstevel@tonic-gate 	ulong_t *ac_mask = &mask[0];
255*7c478bd9Sstevel@tonic-gate 	int error;
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ac_proc->ac_lock);
258*7c478bd9Sstevel@tonic-gate 	if (ac_proc->ac_state == AC_OFF || ac_proc->ac_vnode == NULL) {
259*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ac_proc->ac_lock);
260*7c478bd9Sstevel@tonic-gate 		return (ENOTACTIVE);
261*7c478bd9Sstevel@tonic-gate 	}
262*7c478bd9Sstevel@tonic-gate 	bt_copy(&ac_proc->ac_mask[0], ac_mask, AC_MASK_SZ);
263*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ac_proc->ac_lock);
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	pu = kmem_zalloc(sizeof (proc_usage_t), KM_SLEEP);
266*7c478bd9Sstevel@tonic-gate 	pu->pu_command = kmem_zalloc(MAXCOMLEN + 1, KM_SLEEP);
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
269*7c478bd9Sstevel@tonic-gate 	if ((p = prfind(pid)) == NULL) {
270*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
271*7c478bd9Sstevel@tonic-gate 		kmem_free(pu->pu_command, MAXCOMLEN + 1);
272*7c478bd9Sstevel@tonic-gate 		kmem_free(pu, sizeof (proc_usage_t));
273*7c478bd9Sstevel@tonic-gate 		return (ESRCH);
274*7c478bd9Sstevel@tonic-gate 	}
275*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
276*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
277*7c478bd9Sstevel@tonic-gate 	exacct_calculate_proc_usage(p, pu, ac_mask, flag, 0);
278*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	error = exacct_assemble_proc_usage(ac_proc, pu,
281*7c478bd9Sstevel@tonic-gate 	    exacct_commit_callback, NULL, 0, sizep, flag);
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	kmem_free(pu->pu_command, MAXCOMLEN + 1);
284*7c478bd9Sstevel@tonic-gate 	kmem_free(pu, sizeof (proc_usage_t));
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 	return (error);
287*7c478bd9Sstevel@tonic-gate }
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate static int
290*7c478bd9Sstevel@tonic-gate wracct(idtype_t idtype, id_t id, int flags)
291*7c478bd9Sstevel@tonic-gate {
292*7c478bd9Sstevel@tonic-gate 	int error;
293*7c478bd9Sstevel@tonic-gate 	size_t size = 0;
294*7c478bd9Sstevel@tonic-gate 	struct exacct_globals *acg;
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 	/*
297*7c478bd9Sstevel@tonic-gate 	 * Validate flags.
298*7c478bd9Sstevel@tonic-gate 	 */
299*7c478bd9Sstevel@tonic-gate 	switch (flags) {
300*7c478bd9Sstevel@tonic-gate 	case EW_PARTIAL:
301*7c478bd9Sstevel@tonic-gate 	case EW_INTERVAL:
302*7c478bd9Sstevel@tonic-gate 		break;
303*7c478bd9Sstevel@tonic-gate 	default:
304*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
305*7c478bd9Sstevel@tonic-gate 	}
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 	acg = zone_getspecific(exacct_zone_key, curproc->p_zone);
308*7c478bd9Sstevel@tonic-gate 	switch (idtype) {
309*7c478bd9Sstevel@tonic-gate 	case P_PID:
310*7c478bd9Sstevel@tonic-gate 		if (flags == EW_INTERVAL)
311*7c478bd9Sstevel@tonic-gate 			return (set_errno(ENOTSUP));
312*7c478bd9Sstevel@tonic-gate 		error = wracct_proc(&acg->ac_proc, id, flags, &size);
313*7c478bd9Sstevel@tonic-gate 		break;
314*7c478bd9Sstevel@tonic-gate 	case P_TASKID:
315*7c478bd9Sstevel@tonic-gate 		error = wracct_task(&acg->ac_task, id, flags, &size);
316*7c478bd9Sstevel@tonic-gate 		break;
317*7c478bd9Sstevel@tonic-gate 	default:
318*7c478bd9Sstevel@tonic-gate 		error = EINVAL;
319*7c478bd9Sstevel@tonic-gate 		break;
320*7c478bd9Sstevel@tonic-gate 	}
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate 	return (error == 0 ? error : set_errno(error));
323*7c478bd9Sstevel@tonic-gate }
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate static long
326*7c478bd9Sstevel@tonic-gate exacct(int code, idtype_t idtype, id_t id, void *buf, size_t bufsize,
327*7c478bd9Sstevel@tonic-gate     int flags)
328*7c478bd9Sstevel@tonic-gate {
329*7c478bd9Sstevel@tonic-gate 	if (secpolicy_acct(CRED()) != 0)
330*7c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	if (exacct_zone_key == ZONE_KEY_UNINITIALIZED)
333*7c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTACTIVE));
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	switch (code) {
336*7c478bd9Sstevel@tonic-gate 	case 0:
337*7c478bd9Sstevel@tonic-gate 		return (getacct(idtype, id, buf, bufsize));
338*7c478bd9Sstevel@tonic-gate 	case 1:
339*7c478bd9Sstevel@tonic-gate 		return (putacct(idtype, id, buf, bufsize, flags));
340*7c478bd9Sstevel@tonic-gate 	case 2:
341*7c478bd9Sstevel@tonic-gate 		return (wracct(idtype, id, flags));
342*7c478bd9Sstevel@tonic-gate 	default:
343*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
344*7c478bd9Sstevel@tonic-gate 	}
345*7c478bd9Sstevel@tonic-gate }
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate #if defined(_LP64)
348*7c478bd9Sstevel@tonic-gate #define	SE_LRVAL	SE_64RVAL
349*7c478bd9Sstevel@tonic-gate #else
350*7c478bd9Sstevel@tonic-gate #define	SE_LRVAL	SE_32RVAL1
351*7c478bd9Sstevel@tonic-gate #endif
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate static struct sysent exacctsys_sysent = {
354*7c478bd9Sstevel@tonic-gate 	6,
355*7c478bd9Sstevel@tonic-gate 	SE_NOUNLOAD | SE_ARGC | SE_LRVAL,
356*7c478bd9Sstevel@tonic-gate 	(int (*)())exacct
357*7c478bd9Sstevel@tonic-gate };
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
360*7c478bd9Sstevel@tonic-gate 	&mod_syscallops,
361*7c478bd9Sstevel@tonic-gate 	"extended accounting facility",
362*7c478bd9Sstevel@tonic-gate 	&exacctsys_sysent
363*7c478bd9Sstevel@tonic-gate };
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate static struct sysent exacctsys_sysent32 = {
368*7c478bd9Sstevel@tonic-gate 	6,
369*7c478bd9Sstevel@tonic-gate 	SE_NOUNLOAD | SE_ARGC | SE_32RVAL1,
370*7c478bd9Sstevel@tonic-gate 	(int (*)())exacct
371*7c478bd9Sstevel@tonic-gate };
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
374*7c478bd9Sstevel@tonic-gate 	&mod_syscallops32,
375*7c478bd9Sstevel@tonic-gate 	"32-bit extended accounting facility",
376*7c478bd9Sstevel@tonic-gate 	&exacctsys_sysent32
377*7c478bd9Sstevel@tonic-gate };
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate #endif
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
382*7c478bd9Sstevel@tonic-gate 	MODREV_1,
383*7c478bd9Sstevel@tonic-gate 	&modlsys,
384*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
385*7c478bd9Sstevel@tonic-gate 	&modlsys32,
386*7c478bd9Sstevel@tonic-gate #endif
387*7c478bd9Sstevel@tonic-gate 	NULL
388*7c478bd9Sstevel@tonic-gate };
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate int
391*7c478bd9Sstevel@tonic-gate _init(void)
392*7c478bd9Sstevel@tonic-gate {
393*7c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
394*7c478bd9Sstevel@tonic-gate }
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate int
397*7c478bd9Sstevel@tonic-gate _fini(void)
398*7c478bd9Sstevel@tonic-gate {
399*7c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
400*7c478bd9Sstevel@tonic-gate }
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate int
403*7c478bd9Sstevel@tonic-gate _info(struct modinfo *mip)
404*7c478bd9Sstevel@tonic-gate {
405*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, mip));
406*7c478bd9Sstevel@tonic-gate }
407