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