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