xref: /titanic_52/usr/src/uts/common/os/acct.c (revision 78323854f26c30110c6477875241b76265792c09)
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 		    NULL);
135 		VN_RELE(ag->acctvp);
136 	}
137 	ag->acctvp = NULL;
138 	mutex_exit(&ag->aclock);
139 }
140 
141 /*ARGSUSED*/
142 static void
143 acct_fini(zoneid_t zoneid, void *arg)
144 {
145 	struct acct_globals *ag = arg;
146 
147 	mutex_enter(&acct_list_lock);
148 	list_remove(&acct_list, ag);
149 	mutex_exit(&acct_list_lock);
150 
151 	mutex_destroy(&ag->aclock);
152 	kmem_free(ag, sizeof (*ag));
153 }
154 
155 int
156 _init(void)
157 {
158 	int error;
159 
160 	mutex_init(&acct_list_lock, NULL, MUTEX_DEFAULT, NULL);
161 	list_create(&acct_list, sizeof (struct acct_globals),
162 	    offsetof(struct acct_globals, aclink));
163 	/*
164 	 * Using an initializer here wastes a bit of memory for zones that
165 	 * don't use accounting, but vastly simplifies the locking.
166 	 */
167 	zone_key_create(&acct_zone_key, acct_init, acct_shutdown, acct_fini);
168 	if ((error = mod_install(&modlinkage)) != 0) {
169 		(void) zone_key_delete(acct_zone_key);
170 		list_destroy(&acct_list);
171 		mutex_destroy(&acct_list_lock);
172 	}
173 	return (error);
174 }
175 
176 int
177 _info(struct modinfo *modinfop)
178 {
179 	return (mod_info(&modlinkage, modinfop));
180 }
181 
182 /*
183  * acct() is a "weak stub" routine called from exit().
184  * Once this module has been loaded, we refuse to allow
185  * it to unload - otherwise accounting would quietly
186  * cease.  See 1211661.  It's possible to make this module
187  * unloadable but it's substantially safer not to bother.
188  */
189 int
190 _fini(void)
191 {
192 	return (EBUSY);
193 }
194 
195 /*
196  * See if vp is in use by the accounting system on any zone.  This does a deep
197  * comparison of vnodes such that a file and a lofs "shadow" node of it will
198  * appear to be the same.
199  *
200  * If 'compare_vfs' is true, the function will do a comparison of vfs_t's
201  * instead (ie, is the vfs_t on which the vnode resides in use by the
202  * accounting system in any zone).
203  *
204  * Returns 1 if found (in use), 0 otherwise.
205  */
206 static int
207 acct_find(vnode_t *vp, boolean_t compare_vfs)
208 {
209 	struct acct_globals *ag;
210 	vnode_t *realvp;
211 
212 	ASSERT(MUTEX_HELD(&acct_list_lock));
213 	ASSERT(vp != NULL);
214 
215 	if (VOP_REALVP(vp, &realvp, NULL))
216 		realvp = vp;
217 	for (ag = list_head(&acct_list); ag != NULL;
218 	    ag = list_next(&acct_list, ag)) {
219 		vnode_t *racctvp;
220 		boolean_t found = B_FALSE;
221 
222 		mutex_enter(&ag->aclock);
223 		if (ag->acctvp == NULL) {
224 			mutex_exit(&ag->aclock);
225 			continue;
226 		}
227 		if (VOP_REALVP(ag->acctvp, &racctvp, NULL))
228 			racctvp = ag->acctvp;
229 		if (compare_vfs) {
230 			if (racctvp->v_vfsp == realvp->v_vfsp)
231 				found = B_TRUE;
232 		} else {
233 			if (VN_CMP(realvp, racctvp))
234 				found = B_TRUE;
235 		}
236 		mutex_exit(&ag->aclock);
237 		if (found)
238 			return (1);
239 	}
240 	return (0);
241 }
242 
243 /*
244  * Returns 1 if the vfs that vnode resides on is in use for the accounting
245  * subsystem, 0 otherwise.
246  */
247 int
248 acct_fs_in_use(vnode_t *vp)
249 {
250 	int found;
251 
252 	if (vp == NULL)
253 		return (0);
254 	mutex_enter(&acct_list_lock);
255 	found = acct_find(vp, B_TRUE);
256 	mutex_exit(&acct_list_lock);
257 	return (found);
258 }
259 
260 /*
261  * Perform process accounting functions.
262  */
263 int
264 sysacct(char *fname)
265 {
266 	struct acct_globals *ag;
267 	struct vnode *vp;
268 	int error = 0;
269 
270 	if (secpolicy_acct(CRED()) != 0)
271 		return (set_errno(EPERM));
272 
273 	ag = zone_getspecific(acct_zone_key, curproc->p_zone);
274 	ASSERT(ag != NULL);
275 
276 	if (fname == NULL) {
277 		/*
278 		 * Close the file and stop accounting.
279 		 */
280 		mutex_enter(&ag->aclock);
281 		vp = ag->acctvp;
282 		ag->acctvp = NULL;
283 		mutex_exit(&ag->aclock);
284 		if (vp) {
285 			error = VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED(),
286 			    NULL);
287 			VN_RELE(vp);
288 		}
289 		return (error == 0 ? 0 : set_errno(error));
290 	}
291 
292 	/*
293 	 * Either (a) open a new file and begin accounting -or- (b)
294 	 * switch accounting from an old to a new file.
295 	 *
296 	 * (Open the file without holding aclock in case it
297 	 * sleeps (holding the lock prevents process exit).)
298 	 */
299 	if ((error = vn_open(fname, UIO_USERSPACE, FWRITE,
300 	    0, &vp, (enum create)0, 0)) != 0) {
301 		/* SVID  compliance */
302 		if (error == EISDIR)
303 			error = EACCES;
304 		return (set_errno(error));
305 	}
306 
307 	if (vp->v_type != VREG) {
308 		error = EACCES;
309 	} else {
310 		mutex_enter(&acct_list_lock);
311 		if (acct_find(vp, B_FALSE)) {
312 			error = EBUSY;
313 		} else {
314 			mutex_enter(&ag->aclock);
315 			if (ag->acctvp) {
316 				vnode_t *oldvp;
317 
318 				/*
319 				 * close old acctvp, and point acct()
320 				 * at new file by swapping vp and acctvp
321 				 */
322 				oldvp = ag->acctvp;
323 				ag->acctvp = vp;
324 				vp = oldvp;
325 			} else {
326 				/*
327 				 * no existing file, start accounting ..
328 				 */
329 				ag->acctvp = vp;
330 				vp = NULL;
331 			}
332 			mutex_exit(&ag->aclock);
333 		}
334 		mutex_exit(&acct_list_lock);
335 	}
336 
337 	if (vp) {
338 		(void) VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED(), NULL);
339 		VN_RELE(vp);
340 	}
341 	return (error == 0 ? 0 : set_errno(error));
342 }
343 
344 /*
345  * Produce a pseudo-floating point representation
346  * with 3 bits base-8 exponent, 13 bits fraction.
347  */
348 static comp_t
349 acct_compress(ulong_t t)
350 {
351 	int exp = 0, round = 0;
352 
353 	while (t >= 8192) {
354 		exp++;
355 		round = t & 04;
356 		t >>= 3;
357 	}
358 	if (round) {
359 		t++;
360 		if (t >= 8192) {
361 			t >>= 3;
362 			exp++;
363 		}
364 	}
365 #ifdef _LP64
366 	if (exp > 7) {
367 		/* prevent wraparound */
368 		t = 8191;
369 		exp = 7;
370 	}
371 #endif
372 	return ((exp << 13) + t);
373 }
374 
375 /*
376  * On exit, write a record on the accounting file.
377  */
378 void
379 acct(char st)
380 {
381 	struct vnode *vp;
382 	struct cred *cr;
383 	struct proc *p;
384 	user_t *ua;
385 	struct vattr va;
386 	ssize_t resid = 0;
387 	int error;
388 	struct acct_globals *ag;
389 
390 	ag = zone_getspecific(acct_zone_key, curproc->p_zone);
391 
392 	mutex_enter(&ag->aclock);
393 	if ((vp = ag->acctvp) == NULL) {
394 		mutex_exit(&ag->aclock);
395 		return;
396 	}
397 
398 	/*
399 	 * This only gets called from exit after all lwp's have exited so no
400 	 * cred locking is needed.
401 	 */
402 	p = curproc;
403 	ua = PTOU(p);
404 	bcopy(ua->u_comm, ag->acctbuf.ac_comm, sizeof (ag->acctbuf.ac_comm));
405 	ag->acctbuf.ac_btime = ua->u_start.tv_sec;
406 	ag->acctbuf.ac_utime = acct_compress(NSEC_TO_TICK(p->p_acct[LMS_USER]));
407 	ag->acctbuf.ac_stime = acct_compress(
408 	    NSEC_TO_TICK(p->p_acct[LMS_SYSTEM] + p->p_acct[LMS_TRAP]));
409 	ag->acctbuf.ac_etime = acct_compress(lbolt - ua->u_ticks);
410 	ag->acctbuf.ac_mem = acct_compress((ulong_t)ua->u_mem);
411 	ag->acctbuf.ac_io = acct_compress((ulong_t)p->p_ru.ioch);
412 	ag->acctbuf.ac_rw = acct_compress((ulong_t)(p->p_ru.inblock +
413 	    p->p_ru.oublock));
414 	cr = CRED();
415 	ag->acctbuf.ac_uid = crgetruid(cr);
416 	ag->acctbuf.ac_gid = crgetrgid(cr);
417 	(void) cmpldev(&ag->acctbuf.ac_tty, cttydev(p));
418 	ag->acctbuf.ac_stat = st;
419 	ag->acctbuf.ac_flag = (ua->u_acflag | AEXPND);
420 
421 	/*
422 	 * Save the size. If the write fails, reset the size to avoid
423 	 * corrupted acct files.
424 	 *
425 	 * Large Files: We deliberately prevent accounting files from
426 	 * exceeding the 2GB limit as none of the accounting commands are
427 	 * currently large file aware.
428 	 */
429 	va.va_mask = AT_SIZE;
430 	if (VOP_GETATTR(vp, &va, 0, kcred, NULL) == 0) {
431 		error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&ag->acctbuf,
432 		    sizeof (ag->acctbuf), 0LL, UIO_SYSSPACE, FAPPEND,
433 		    (rlim64_t)MAXOFF32_T, kcred, &resid);
434 		if (error || resid)
435 			(void) VOP_SETATTR(vp, &va, 0, kcred, NULL);
436 	}
437 	mutex_exit(&ag->aclock);
438 }
439