xref: /onnv-gate/usr/src/uts/common/os/acct.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <sys/sysmacros.h>
35*0Sstevel@tonic-gate #include <sys/param.h>
36*0Sstevel@tonic-gate #include <sys/systm.h>
37*0Sstevel@tonic-gate #include <sys/acct.h>
38*0Sstevel@tonic-gate #include <sys/cred.h>
39*0Sstevel@tonic-gate #include <sys/user.h>
40*0Sstevel@tonic-gate #include <sys/errno.h>
41*0Sstevel@tonic-gate #include <sys/file.h>
42*0Sstevel@tonic-gate #include <sys/vnode.h>
43*0Sstevel@tonic-gate #include <sys/debug.h>
44*0Sstevel@tonic-gate #include <sys/proc.h>
45*0Sstevel@tonic-gate #include <sys/resource.h>
46*0Sstevel@tonic-gate #include <sys/session.h>
47*0Sstevel@tonic-gate #include <sys/modctl.h>
48*0Sstevel@tonic-gate #include <sys/syscall.h>
49*0Sstevel@tonic-gate #include <sys/policy.h>
50*0Sstevel@tonic-gate #include <sys/list.h>
51*0Sstevel@tonic-gate #include <sys/time.h>
52*0Sstevel@tonic-gate #include <sys/msacct.h>
53*0Sstevel@tonic-gate #include <sys/zone.h>
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate /*
56*0Sstevel@tonic-gate  * Each zone has its own accounting settings (on or off) and associated
57*0Sstevel@tonic-gate  * file.  The global zone is not special in this aspect; it will only
58*0Sstevel@tonic-gate  * generate records for processes that ran in the global zone.  We could
59*0Sstevel@tonic-gate  * allow the global zone to record all activity on the system, but there
60*0Sstevel@tonic-gate  * would be no way of knowing the zone in which the processes executed.
61*0Sstevel@tonic-gate  * sysacct() is thus virtualized to only act on the caller's zone.
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate struct acct_globals {
64*0Sstevel@tonic-gate 	struct acct	acctbuf;
65*0Sstevel@tonic-gate 	kmutex_t	aclock;
66*0Sstevel@tonic-gate 	struct vnode	*acctvp;
67*0Sstevel@tonic-gate 	list_node_t	aclink;
68*0Sstevel@tonic-gate };
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate /*
71*0Sstevel@tonic-gate  * We need a list of all accounting settings for all zones, so we can
72*0Sstevel@tonic-gate  * accurately determine if a file is in use for accounting (possibly by
73*0Sstevel@tonic-gate  * another zone).
74*0Sstevel@tonic-gate  */
75*0Sstevel@tonic-gate static zone_key_t acct_zone_key;
76*0Sstevel@tonic-gate static list_t acct_list;
77*0Sstevel@tonic-gate kmutex_t acct_list_lock;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate static struct sysent acctsysent = {
80*0Sstevel@tonic-gate 	1,
81*0Sstevel@tonic-gate 	SE_NOUNLOAD | SE_ARGC | SE_32RVAL1,
82*0Sstevel@tonic-gate 	sysacct
83*0Sstevel@tonic-gate };
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate static struct modlsys modlsys = {
86*0Sstevel@tonic-gate 	&mod_syscallops, "acct(2) syscall", &acctsysent
87*0Sstevel@tonic-gate };
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
90*0Sstevel@tonic-gate static struct modlsys modlsys32 = {
91*0Sstevel@tonic-gate 	&mod_syscallops32, "32-bit acct(2) syscall", &acctsysent
92*0Sstevel@tonic-gate };
93*0Sstevel@tonic-gate #endif
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
96*0Sstevel@tonic-gate 	MODREV_1,
97*0Sstevel@tonic-gate 	&modlsys,
98*0Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
99*0Sstevel@tonic-gate 	&modlsys32,
100*0Sstevel@tonic-gate #endif
101*0Sstevel@tonic-gate 	NULL
102*0Sstevel@tonic-gate };
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate /*ARGSUSED*/
105*0Sstevel@tonic-gate static void *
106*0Sstevel@tonic-gate acct_init(zoneid_t zoneid)
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate 	struct acct_globals *ag;
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	ag = kmem_alloc(sizeof (*ag), KM_SLEEP);
111*0Sstevel@tonic-gate 	bzero(&ag->acctbuf, sizeof (ag->acctbuf));
112*0Sstevel@tonic-gate 	mutex_init(&ag->aclock, NULL, MUTEX_DEFAULT, NULL);
113*0Sstevel@tonic-gate 	ag->acctvp = NULL;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	mutex_enter(&acct_list_lock);
116*0Sstevel@tonic-gate 	list_insert_tail(&acct_list, ag);
117*0Sstevel@tonic-gate 	mutex_exit(&acct_list_lock);
118*0Sstevel@tonic-gate 	return (ag);
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate /* ARGSUSED */
122*0Sstevel@tonic-gate static void
123*0Sstevel@tonic-gate acct_shutdown(zoneid_t zoneid, void *arg)
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate 	struct acct_globals *ag = arg;
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	mutex_enter(&ag->aclock);
128*0Sstevel@tonic-gate 	if (ag->acctvp) {
129*0Sstevel@tonic-gate 		/*
130*0Sstevel@tonic-gate 		 * This needs to be done as a shutdown callback, otherwise this
131*0Sstevel@tonic-gate 		 * held vnode may cause filesystems to be busy, and the zone
132*0Sstevel@tonic-gate 		 * shutdown operation to fail.
133*0Sstevel@tonic-gate 		 */
134*0Sstevel@tonic-gate 		(void) VOP_CLOSE(ag->acctvp, FWRITE, 1, (offset_t)0, kcred);
135*0Sstevel@tonic-gate 		VN_RELE(ag->acctvp);
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 	ag->acctvp = NULL;
138*0Sstevel@tonic-gate 	mutex_exit(&ag->aclock);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /*ARGSUSED*/
142*0Sstevel@tonic-gate static void
143*0Sstevel@tonic-gate acct_fini(zoneid_t zoneid, void *arg)
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	struct acct_globals *ag = arg;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	mutex_enter(&acct_list_lock);
148*0Sstevel@tonic-gate 	list_remove(&acct_list, ag);
149*0Sstevel@tonic-gate 	mutex_exit(&acct_list_lock);
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	mutex_destroy(&ag->aclock);
152*0Sstevel@tonic-gate 	kmem_free(ag, sizeof (*ag));
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate int
156*0Sstevel@tonic-gate _init(void)
157*0Sstevel@tonic-gate {
158*0Sstevel@tonic-gate 	int error;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	mutex_init(&acct_list_lock, NULL, MUTEX_DEFAULT, NULL);
161*0Sstevel@tonic-gate 	list_create(&acct_list, sizeof (struct acct_globals),
162*0Sstevel@tonic-gate 	    offsetof(struct acct_globals, aclink));
163*0Sstevel@tonic-gate 	/*
164*0Sstevel@tonic-gate 	 * Using an initializer here wastes a bit of memory for zones that
165*0Sstevel@tonic-gate 	 * don't use accounting, but vastly simplifies the locking.
166*0Sstevel@tonic-gate 	 */
167*0Sstevel@tonic-gate 	zone_key_create(&acct_zone_key, acct_init, acct_shutdown, acct_fini);
168*0Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0) {
169*0Sstevel@tonic-gate 		(void) zone_key_delete(acct_zone_key);
170*0Sstevel@tonic-gate 		list_destroy(&acct_list);
171*0Sstevel@tonic-gate 		mutex_destroy(&acct_list_lock);
172*0Sstevel@tonic-gate 	}
173*0Sstevel@tonic-gate 	return (error);
174*0Sstevel@tonic-gate }
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate int
177*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
178*0Sstevel@tonic-gate {
179*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate /*
183*0Sstevel@tonic-gate  * acct() is a "weak stub" routine called from exit().
184*0Sstevel@tonic-gate  * Once this module has been loaded, we refuse to allow
185*0Sstevel@tonic-gate  * it to unload - otherwise accounting would quietly
186*0Sstevel@tonic-gate  * cease.  See 1211661.  It's possible to make this module
187*0Sstevel@tonic-gate  * unloadable but it's substantially safer not to bother.
188*0Sstevel@tonic-gate  */
189*0Sstevel@tonic-gate int
190*0Sstevel@tonic-gate _fini(void)
191*0Sstevel@tonic-gate {
192*0Sstevel@tonic-gate 	return (EBUSY);
193*0Sstevel@tonic-gate }
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate /*
196*0Sstevel@tonic-gate  * See if vp is in use by the accounting system on any zone.  This does a deep
197*0Sstevel@tonic-gate  * comparison of vnodes such that a file and a lofs "shadow" node of it will
198*0Sstevel@tonic-gate  * appear to be the same.
199*0Sstevel@tonic-gate  *
200*0Sstevel@tonic-gate  * If 'compare_vfs' is true, the function will do a comparison of vfs_t's
201*0Sstevel@tonic-gate  * instead (ie, is the vfs_t on which the vnode resides in use by the
202*0Sstevel@tonic-gate  * accounting system in any zone).
203*0Sstevel@tonic-gate  *
204*0Sstevel@tonic-gate  * Returns 1 if found (in use), 0 otherwise.
205*0Sstevel@tonic-gate  */
206*0Sstevel@tonic-gate static int
207*0Sstevel@tonic-gate acct_find(vnode_t *vp, boolean_t compare_vfs)
208*0Sstevel@tonic-gate {
209*0Sstevel@tonic-gate 	struct acct_globals *ag;
210*0Sstevel@tonic-gate 	vnode_t *realvp;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&acct_list_lock));
213*0Sstevel@tonic-gate 	ASSERT(vp != NULL);
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	if (VOP_REALVP(vp, &realvp))
216*0Sstevel@tonic-gate 		realvp = vp;
217*0Sstevel@tonic-gate 	for (ag = list_head(&acct_list); ag != NULL;
218*0Sstevel@tonic-gate 	    ag = list_next(&acct_list, ag)) {
219*0Sstevel@tonic-gate 		vnode_t *racctvp;
220*0Sstevel@tonic-gate 		boolean_t found = B_FALSE;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 		mutex_enter(&ag->aclock);
223*0Sstevel@tonic-gate 		if (ag->acctvp == NULL) {
224*0Sstevel@tonic-gate 			mutex_exit(&ag->aclock);
225*0Sstevel@tonic-gate 			continue;
226*0Sstevel@tonic-gate 		}
227*0Sstevel@tonic-gate 		if (VOP_REALVP(ag->acctvp, &racctvp))
228*0Sstevel@tonic-gate 			racctvp = ag->acctvp;
229*0Sstevel@tonic-gate 		if (compare_vfs) {
230*0Sstevel@tonic-gate 			if (racctvp->v_vfsp == realvp->v_vfsp)
231*0Sstevel@tonic-gate 				found = B_TRUE;
232*0Sstevel@tonic-gate 		} else {
233*0Sstevel@tonic-gate 			if (VN_CMP(realvp, racctvp))
234*0Sstevel@tonic-gate 				found = B_TRUE;
235*0Sstevel@tonic-gate 		}
236*0Sstevel@tonic-gate 		mutex_exit(&ag->aclock);
237*0Sstevel@tonic-gate 		if (found)
238*0Sstevel@tonic-gate 			return (1);
239*0Sstevel@tonic-gate 	}
240*0Sstevel@tonic-gate 	return (0);
241*0Sstevel@tonic-gate }
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate /*
244*0Sstevel@tonic-gate  * Returns 1 if the vfs that vnode resides on is in use for the accounting
245*0Sstevel@tonic-gate  * subsystem, 0 otherwise.
246*0Sstevel@tonic-gate  */
247*0Sstevel@tonic-gate int
248*0Sstevel@tonic-gate acct_fs_in_use(vnode_t *vp)
249*0Sstevel@tonic-gate {
250*0Sstevel@tonic-gate 	int found;
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	if (vp == NULL)
253*0Sstevel@tonic-gate 		return (0);
254*0Sstevel@tonic-gate 	mutex_enter(&acct_list_lock);
255*0Sstevel@tonic-gate 	found = acct_find(vp, B_TRUE);
256*0Sstevel@tonic-gate 	mutex_exit(&acct_list_lock);
257*0Sstevel@tonic-gate 	return (found);
258*0Sstevel@tonic-gate }
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate /*
261*0Sstevel@tonic-gate  * Perform process accounting functions.
262*0Sstevel@tonic-gate  */
263*0Sstevel@tonic-gate int
264*0Sstevel@tonic-gate sysacct(char *fname)
265*0Sstevel@tonic-gate {
266*0Sstevel@tonic-gate 	struct acct_globals *ag;
267*0Sstevel@tonic-gate 	struct vnode *vp;
268*0Sstevel@tonic-gate 	int error = 0;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	if (secpolicy_acct(CRED()) != 0)
271*0Sstevel@tonic-gate 		return (set_errno(EPERM));
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	ag = zone_getspecific(acct_zone_key, curproc->p_zone);
274*0Sstevel@tonic-gate 	ASSERT(ag != NULL);
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	if (fname == NULL) {
277*0Sstevel@tonic-gate 		/*
278*0Sstevel@tonic-gate 		 * Close the file and stop accounting.
279*0Sstevel@tonic-gate 		 */
280*0Sstevel@tonic-gate 		mutex_enter(&ag->aclock);
281*0Sstevel@tonic-gate 		vp = ag->acctvp;
282*0Sstevel@tonic-gate 		ag->acctvp = NULL;
283*0Sstevel@tonic-gate 		mutex_exit(&ag->aclock);
284*0Sstevel@tonic-gate 		if (vp) {
285*0Sstevel@tonic-gate 			error = VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED());
286*0Sstevel@tonic-gate 			VN_RELE(vp);
287*0Sstevel@tonic-gate 		}
288*0Sstevel@tonic-gate 		return (error == 0 ? 0 : set_errno(error));
289*0Sstevel@tonic-gate 	}
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	/*
292*0Sstevel@tonic-gate 	 * Either (a) open a new file and begin accounting -or- (b)
293*0Sstevel@tonic-gate 	 * switch accounting from an old to a new file.
294*0Sstevel@tonic-gate 	 *
295*0Sstevel@tonic-gate 	 * (Open the file without holding aclock in case it
296*0Sstevel@tonic-gate 	 * sleeps (holding the lock prevents process exit).)
297*0Sstevel@tonic-gate 	 */
298*0Sstevel@tonic-gate 	if ((error = vn_open(fname, UIO_USERSPACE, FWRITE,
299*0Sstevel@tonic-gate 	    0, &vp, (enum create)0, 0)) != 0) {
300*0Sstevel@tonic-gate 		/* SVID  compliance */
301*0Sstevel@tonic-gate 		if (error == EISDIR)
302*0Sstevel@tonic-gate 			error = EACCES;
303*0Sstevel@tonic-gate 		return (set_errno(error));
304*0Sstevel@tonic-gate 	}
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	if (vp->v_type != VREG) {
307*0Sstevel@tonic-gate 		error = EACCES;
308*0Sstevel@tonic-gate 	} else {
309*0Sstevel@tonic-gate 		mutex_enter(&acct_list_lock);
310*0Sstevel@tonic-gate 		if (acct_find(vp, B_FALSE)) {
311*0Sstevel@tonic-gate 			error = EBUSY;
312*0Sstevel@tonic-gate 		} else {
313*0Sstevel@tonic-gate 			mutex_enter(&ag->aclock);
314*0Sstevel@tonic-gate 			if (ag->acctvp) {
315*0Sstevel@tonic-gate 				vnode_t *oldvp;
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 				/*
318*0Sstevel@tonic-gate 				 * close old acctvp, and point acct()
319*0Sstevel@tonic-gate 				 * at new file by swapping vp and acctvp
320*0Sstevel@tonic-gate 				 */
321*0Sstevel@tonic-gate 				oldvp = ag->acctvp;
322*0Sstevel@tonic-gate 				ag->acctvp = vp;
323*0Sstevel@tonic-gate 				vp = oldvp;
324*0Sstevel@tonic-gate 			} else {
325*0Sstevel@tonic-gate 				/*
326*0Sstevel@tonic-gate 				 * no existing file, start accounting ..
327*0Sstevel@tonic-gate 				 */
328*0Sstevel@tonic-gate 				ag->acctvp = vp;
329*0Sstevel@tonic-gate 				vp = NULL;
330*0Sstevel@tonic-gate 			}
331*0Sstevel@tonic-gate 			mutex_exit(&ag->aclock);
332*0Sstevel@tonic-gate 		}
333*0Sstevel@tonic-gate 		mutex_exit(&acct_list_lock);
334*0Sstevel@tonic-gate 	}
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	if (vp) {
337*0Sstevel@tonic-gate 		(void) VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED());
338*0Sstevel@tonic-gate 		VN_RELE(vp);
339*0Sstevel@tonic-gate 	}
340*0Sstevel@tonic-gate 	return (error == 0 ? 0 : set_errno(error));
341*0Sstevel@tonic-gate }
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate /*
344*0Sstevel@tonic-gate  * Produce a pseudo-floating point representation
345*0Sstevel@tonic-gate  * with 3 bits base-8 exponent, 13 bits fraction.
346*0Sstevel@tonic-gate  */
347*0Sstevel@tonic-gate static comp_t
348*0Sstevel@tonic-gate acct_compress(ulong_t t)
349*0Sstevel@tonic-gate {
350*0Sstevel@tonic-gate 	int exp = 0, round = 0;
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	while (t >= 8192) {
353*0Sstevel@tonic-gate 		exp++;
354*0Sstevel@tonic-gate 		round = t & 04;
355*0Sstevel@tonic-gate 		t >>= 3;
356*0Sstevel@tonic-gate 	}
357*0Sstevel@tonic-gate 	if (round) {
358*0Sstevel@tonic-gate 		t++;
359*0Sstevel@tonic-gate 		if (t >= 8192) {
360*0Sstevel@tonic-gate 			t >>= 3;
361*0Sstevel@tonic-gate 			exp++;
362*0Sstevel@tonic-gate 		}
363*0Sstevel@tonic-gate 	}
364*0Sstevel@tonic-gate #ifdef _LP64
365*0Sstevel@tonic-gate 	if (exp > 7) {
366*0Sstevel@tonic-gate 		/* prevent wraparound */
367*0Sstevel@tonic-gate 		t = 8191;
368*0Sstevel@tonic-gate 		exp = 7;
369*0Sstevel@tonic-gate 	}
370*0Sstevel@tonic-gate #endif
371*0Sstevel@tonic-gate 	return ((exp << 13) + t);
372*0Sstevel@tonic-gate }
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate /*
375*0Sstevel@tonic-gate  * On exit, write a record on the accounting file.
376*0Sstevel@tonic-gate  */
377*0Sstevel@tonic-gate void
378*0Sstevel@tonic-gate acct(char st)
379*0Sstevel@tonic-gate {
380*0Sstevel@tonic-gate 	struct vnode *vp;
381*0Sstevel@tonic-gate 	struct cred *cr;
382*0Sstevel@tonic-gate 	struct proc *p;
383*0Sstevel@tonic-gate 	struct vattr va;
384*0Sstevel@tonic-gate 	ssize_t resid = 0;
385*0Sstevel@tonic-gate 	int error;
386*0Sstevel@tonic-gate 	struct acct_globals *ag;
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 	ag = zone_getspecific(acct_zone_key, curproc->p_zone);
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 	mutex_enter(&ag->aclock);
391*0Sstevel@tonic-gate 	if ((vp = ag->acctvp) == NULL) {
392*0Sstevel@tonic-gate 		mutex_exit(&ag->aclock);
393*0Sstevel@tonic-gate 		return;
394*0Sstevel@tonic-gate 	}
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	/*
397*0Sstevel@tonic-gate 	 * This only gets called from exit after all lwp's have exited so no
398*0Sstevel@tonic-gate 	 * cred locking is needed.
399*0Sstevel@tonic-gate 	 */
400*0Sstevel@tonic-gate 	p = curproc;
401*0Sstevel@tonic-gate 	bcopy(u.u_comm, ag->acctbuf.ac_comm, sizeof (ag->acctbuf.ac_comm));
402*0Sstevel@tonic-gate 	ag->acctbuf.ac_btime = u.u_start.tv_sec;
403*0Sstevel@tonic-gate 	ag->acctbuf.ac_utime = acct_compress(
404*0Sstevel@tonic-gate 	    (comp_t)NSEC_TO_TICK(p->p_acct[LMS_USER]));
405*0Sstevel@tonic-gate 	ag->acctbuf.ac_stime = acct_compress(
406*0Sstevel@tonic-gate 	    (comp_t)NSEC_TO_TICK(p->p_acct[LMS_SYSTEM] + p->p_acct[LMS_TRAP]));
407*0Sstevel@tonic-gate 	ag->acctbuf.ac_etime = acct_compress(lbolt - u.u_ticks);
408*0Sstevel@tonic-gate 	ag->acctbuf.ac_mem = acct_compress((ulong_t)u.u_mem);
409*0Sstevel@tonic-gate 	ag->acctbuf.ac_io = acct_compress((ulong_t)p->p_ru.ioch);
410*0Sstevel@tonic-gate 	ag->acctbuf.ac_rw = acct_compress((ulong_t)(p->p_ru.inblock +
411*0Sstevel@tonic-gate 	    p->p_ru.oublock));
412*0Sstevel@tonic-gate 	cr = CRED();
413*0Sstevel@tonic-gate 	ag->acctbuf.ac_uid = crgetruid(cr);
414*0Sstevel@tonic-gate 	ag->acctbuf.ac_gid = crgetrgid(cr);
415*0Sstevel@tonic-gate 	(void) cmpldev(&ag->acctbuf.ac_tty, cttydev(p));
416*0Sstevel@tonic-gate 	ag->acctbuf.ac_stat = st;
417*0Sstevel@tonic-gate 	ag->acctbuf.ac_flag = (u.u_acflag | AEXPND);
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 	/*
420*0Sstevel@tonic-gate 	 * Save the size. If the write fails, reset the size to avoid
421*0Sstevel@tonic-gate 	 * corrupted acct files.
422*0Sstevel@tonic-gate 	 *
423*0Sstevel@tonic-gate 	 * Large Files: We deliberately prevent accounting files from
424*0Sstevel@tonic-gate 	 * exceeding the 2GB limit as none of the accounting commands are
425*0Sstevel@tonic-gate 	 * currently large file aware.
426*0Sstevel@tonic-gate 	 */
427*0Sstevel@tonic-gate 	va.va_mask = AT_SIZE;
428*0Sstevel@tonic-gate 	if (VOP_GETATTR(vp, &va, 0, kcred) == 0) {
429*0Sstevel@tonic-gate 		error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&ag->acctbuf,
430*0Sstevel@tonic-gate 		    sizeof (ag->acctbuf), 0LL, UIO_SYSSPACE, FAPPEND,
431*0Sstevel@tonic-gate 		    (rlim64_t)MAXOFF32_T, kcred, &resid);
432*0Sstevel@tonic-gate 		if (error || resid)
433*0Sstevel@tonic-gate 			(void) VOP_SETATTR(vp, &va, 0, kcred, NULL);
434*0Sstevel@tonic-gate 	}
435*0Sstevel@tonic-gate 	mutex_exit(&ag->aclock);
436*0Sstevel@tonic-gate }
437