Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/kernel/acct.c | |
3 | * | |
4 | * BSD Process Accounting for Linux | |
5 | * | |
6 | * Author: Marco van Wieringen <mvw@planets.elm.net> | |
7 | * | |
8 | * Some code based on ideas and code from: | |
9 | * Thomas K. Dyas <tdyas@eden.rutgers.edu> | |
10 | * | |
11 | * This file implements BSD-style process accounting. Whenever any | |
12 | * process exits, an accounting record of type "struct acct" is | |
13 | * written to the file specified with the acct() system call. It is | |
14 | * up to user-level programs to do useful things with the accounting | |
15 | * log. The kernel just provides the raw accounting information. | |
16 | * | |
17 | * (C) Copyright 1995 - 1997 Marco van Wieringen - ELM Consultancy B.V. | |
18 | * | |
19 | * Plugged two leaks. 1) It didn't return acct_file into the free_filps if | |
20 | * the file happened to be read-only. 2) If the accounting was suspended | |
21 | * due to the lack of space it happily allowed to reopen it and completely | |
22 | * lost the old acct_file. 3/10/98, Al Viro. | |
23 | * | |
24 | * Now we silently close acct_file on attempt to reopen. Cleaned sys_acct(). | |
25 | * XTerms and EMACS are manifestations of pure evil. 21/10/98, AV. | |
26 | * | |
27 | * Fixed a nasty interaction with with sys_umount(). If the accointing | |
28 | * was suspeneded we failed to stop it on umount(). Messy. | |
29 | * Another one: remount to readonly didn't stop accounting. | |
30 | * Question: what should we do if we have CAP_SYS_ADMIN but not | |
31 | * CAP_SYS_PACCT? Current code does the following: umount returns -EBUSY | |
32 | * unless we are messing with the root. In that case we are getting a | |
33 | * real mess with do_remount_sb(). 9/11/98, AV. | |
34 | * | |
35 | * Fixed a bunch of races (and pair of leaks). Probably not the best way, | |
36 | * but this one obviously doesn't introduce deadlocks. Later. BTW, found | |
37 | * one race (and leak) in BSD implementation. | |
38 | * OK, that's better. ANOTHER race and leak in BSD variant. There always | |
39 | * is one more bug... 10/11/98, AV. | |
40 | * | |
41 | * Oh, fsck... Oopsable SMP race in do_process_acct() - we must hold | |
42 | * ->mmap_sem to walk the vma list of current->mm. Nasty, since it leaks | |
43 | * a struct file opened for write. Fixed. 2/6/2000, AV. | |
44 | */ | |
45 | ||
1da177e4 LT |
46 | #include <linux/mm.h> |
47 | #include <linux/slab.h> | |
48 | #include <linux/acct.h> | |
c59ede7b | 49 | #include <linux/capability.h> |
1da177e4 LT |
50 | #include <linux/file.h> |
51 | #include <linux/tty.h> | |
52 | #include <linux/security.h> | |
53 | #include <linux/vfs.h> | |
54 | #include <linux/jiffies.h> | |
55 | #include <linux/times.h> | |
56 | #include <linux/syscalls.h> | |
7b7b1ace | 57 | #include <linux/mount.h> |
7153e402 | 58 | #include <linux/uaccess.h> |
1da177e4 LT |
59 | #include <asm/div64.h> |
60 | #include <linux/blkdev.h> /* sector_div */ | |
5f7b703f | 61 | #include <linux/pid_namespace.h> |
215752fc | 62 | #include <../fs/mount.h> /* will go away when we refactor */ |
1da177e4 LT |
63 | |
64 | /* | |
65 | * These constants control the amount of freespace that suspend and | |
66 | * resume the process accounting system, and the time delay between | |
67 | * each check. | |
68 | * Turned into sysctl-controllable parameters. AV, 12/11/98 | |
69 | */ | |
70 | ||
71 | int acct_parm[3] = {4, 2, 30}; | |
72 | #define RESUME (acct_parm[0]) /* >foo% free space - resume */ | |
73 | #define SUSPEND (acct_parm[1]) /* <foo% free space - suspend */ | |
74 | #define ACCT_TIMEOUT (acct_parm[2]) /* foo second timeout between checks */ | |
75 | ||
76 | /* | |
77 | * External references and all of the globals. | |
78 | */ | |
b8f00e6b | 79 | static void do_acct_process(struct bsd_acct_struct *acct); |
1da177e4 | 80 | |
081e4c8a | 81 | struct bsd_acct_struct { |
2798d4ce AV |
82 | atomic_long_t count; |
83 | union { | |
84 | struct { | |
85 | struct hlist_node s_list; | |
86 | struct hlist_node m_list; | |
87 | }; | |
88 | struct rcu_head rcu; | |
89 | }; | |
b8f00e6b | 90 | struct mutex lock; |
32dc7308 AV |
91 | int active; |
92 | unsigned long needcheck; | |
1da177e4 | 93 | struct file *file; |
5f7b703f | 94 | struct pid_namespace *ns; |
1da177e4 LT |
95 | }; |
96 | ||
2798d4ce AV |
97 | static void acct_free_rcu(struct rcu_head *head) |
98 | { | |
99 | kfree(container_of(head, struct bsd_acct_struct, rcu)); | |
100 | } | |
101 | ||
a75d9797 PE |
102 | static DEFINE_SPINLOCK(acct_lock); |
103 | ||
1da177e4 LT |
104 | /* |
105 | * Check the amount of free space and suspend/resume accordingly. | |
106 | */ | |
54a4d58a | 107 | static int check_free_space(struct bsd_acct_struct *acct) |
1da177e4 LT |
108 | { |
109 | struct kstatfs sbuf; | |
1da177e4 | 110 | |
54a4d58a | 111 | if (time_is_before_jiffies(acct->needcheck)) |
1da177e4 | 112 | goto out; |
1da177e4 LT |
113 | |
114 | /* May block */ | |
54a4d58a | 115 | if (vfs_statfs(&acct->file->f_path, &sbuf)) |
1da177e4 | 116 | goto out; |
1da177e4 | 117 | |
6248b1b3 | 118 | if (acct->active) { |
54a4d58a AV |
119 | u64 suspend = sbuf.f_blocks * SUSPEND; |
120 | do_div(suspend, 100); | |
121 | if (sbuf.f_bavail <= suspend) { | |
6248b1b3 | 122 | acct->active = 0; |
1da177e4 LT |
123 | printk(KERN_INFO "Process accounting paused\n"); |
124 | } | |
125 | } else { | |
54a4d58a AV |
126 | u64 resume = sbuf.f_blocks * RESUME; |
127 | do_div(resume, 100); | |
128 | if (sbuf.f_bavail >= resume) { | |
6248b1b3 | 129 | acct->active = 1; |
1da177e4 LT |
130 | printk(KERN_INFO "Process accounting resumed\n"); |
131 | } | |
132 | } | |
133 | ||
32dc7308 | 134 | acct->needcheck = jiffies + ACCT_TIMEOUT*HZ; |
1da177e4 | 135 | out: |
54a4d58a | 136 | return acct->active; |
1da177e4 LT |
137 | } |
138 | ||
b8f00e6b | 139 | static void acct_put(struct bsd_acct_struct *p) |
1da177e4 | 140 | { |
2798d4ce AV |
141 | if (atomic_long_dec_and_test(&p->count)) |
142 | call_rcu(&p->rcu, acct_free_rcu); | |
b8f00e6b AV |
143 | } |
144 | ||
215752fc AV |
145 | static struct bsd_acct_struct *__acct_get(struct bsd_acct_struct *res) |
146 | { | |
2798d4ce AV |
147 | if (!atomic_long_inc_not_zero(&res->count)) { |
148 | rcu_read_unlock(); | |
149 | cpu_relax(); | |
150 | return NULL; | |
151 | } | |
152 | rcu_read_unlock(); | |
215752fc AV |
153 | mutex_lock(&res->lock); |
154 | if (!res->ns) { | |
155 | mutex_unlock(&res->lock); | |
2798d4ce | 156 | acct_put(res); |
215752fc AV |
157 | return NULL; |
158 | } | |
159 | return res; | |
160 | } | |
161 | ||
162 | static struct bsd_acct_struct *acct_get(struct pid_namespace *ns) | |
b8f00e6b AV |
163 | { |
164 | struct bsd_acct_struct *res; | |
b8f00e6b | 165 | again: |
2798d4ce AV |
166 | smp_rmb(); |
167 | rcu_read_lock(); | |
168 | res = ACCESS_ONCE(ns->bacct); | |
169 | if (!res) { | |
170 | rcu_read_unlock(); | |
215752fc | 171 | return NULL; |
1da177e4 | 172 | } |
2798d4ce | 173 | res = __acct_get(res); |
215752fc AV |
174 | if (!res) |
175 | goto again; | |
b8f00e6b AV |
176 | return res; |
177 | } | |
178 | ||
179 | static void acct_kill(struct bsd_acct_struct *acct, | |
180 | struct bsd_acct_struct *new) | |
181 | { | |
182 | if (acct) { | |
183 | struct file *file = acct->file; | |
184 | struct pid_namespace *ns = acct->ns; | |
2798d4ce AV |
185 | do_acct_process(acct); |
186 | mnt_unpin(file->f_path.mnt); | |
187 | filp_close(file, NULL); | |
b8f00e6b | 188 | spin_lock(&acct_lock); |
215752fc AV |
189 | hlist_del(&acct->m_list); |
190 | hlist_del(&acct->s_list); | |
a75d9797 | 191 | spin_unlock(&acct_lock); |
b8f00e6b AV |
192 | ns->bacct = new; |
193 | if (new) { | |
215752fc AV |
194 | struct vfsmount *m = new->file->f_path.mnt; |
195 | mnt_pin(m); | |
2798d4ce | 196 | spin_lock(&acct_lock); |
215752fc AV |
197 | hlist_add_head(&new->s_list, &m->mnt_sb->s_pins); |
198 | hlist_add_head(&new->m_list, &real_mount(m)->mnt_pins); | |
2798d4ce AV |
199 | spin_unlock(&acct_lock); |
200 | mutex_unlock(&new->lock); | |
b8f00e6b AV |
201 | } |
202 | acct->ns = NULL; | |
2798d4ce | 203 | atomic_long_dec(&acct->count); |
b8f00e6b | 204 | mutex_unlock(&acct->lock); |
2798d4ce | 205 | acct_put(acct); |
1da177e4 LT |
206 | } |
207 | } | |
208 | ||
669abf4e | 209 | static int acct_on(struct filename *pathname) |
7b7b1ace AV |
210 | { |
211 | struct file *file; | |
df279ca8 | 212 | struct vfsmount *mnt; |
b8f00e6b AV |
213 | struct pid_namespace *ns = task_active_pid_ns(current); |
214 | struct bsd_acct_struct *acct, *old; | |
215 | ||
216 | acct = kzalloc(sizeof(struct bsd_acct_struct), GFP_KERNEL); | |
217 | if (!acct) | |
218 | return -ENOMEM; | |
7b7b1ace AV |
219 | |
220 | /* Difference from BSD - they don't do O_APPEND */ | |
669abf4e | 221 | file = file_open_name(pathname, O_WRONLY|O_APPEND|O_LARGEFILE, 0); |
b8f00e6b AV |
222 | if (IS_ERR(file)) { |
223 | kfree(acct); | |
7b7b1ace | 224 | return PTR_ERR(file); |
b8f00e6b | 225 | } |
7b7b1ace | 226 | |
496ad9aa | 227 | if (!S_ISREG(file_inode(file)->i_mode)) { |
b8f00e6b | 228 | kfree(acct); |
7b7b1ace AV |
229 | filp_close(file, NULL); |
230 | return -EACCES; | |
231 | } | |
232 | ||
233 | if (!file->f_op->write) { | |
b8f00e6b | 234 | kfree(acct); |
7b7b1ace AV |
235 | filp_close(file, NULL); |
236 | return -EIO; | |
237 | } | |
238 | ||
2798d4ce | 239 | atomic_long_set(&acct->count, 1); |
b8f00e6b AV |
240 | acct->file = file; |
241 | acct->needcheck = jiffies; | |
242 | acct->ns = ns; | |
243 | mutex_init(&acct->lock); | |
244 | mnt = file->f_path.mnt; | |
0b6b030f | 245 | |
215752fc | 246 | old = acct_get(ns); |
2798d4ce | 247 | mutex_lock_nested(&acct->lock, 1); /* nobody has seen it yet */ |
b8f00e6b AV |
248 | if (old) { |
249 | acct_kill(old, acct); | |
250 | } else { | |
0b6b030f | 251 | ns->bacct = acct; |
2798d4ce | 252 | spin_lock(&acct_lock); |
b8f00e6b | 253 | mnt_pin(mnt); |
215752fc AV |
254 | hlist_add_head(&acct->s_list, &mnt->mnt_sb->s_pins); |
255 | hlist_add_head(&acct->m_list, &real_mount(mnt)->mnt_pins); | |
b8f00e6b | 256 | spin_unlock(&acct_lock); |
2798d4ce | 257 | mutex_unlock(&acct->lock); |
0b6b030f | 258 | } |
df279ca8 | 259 | mntput(mnt); /* it's pinned, now give up active reference */ |
7b7b1ace AV |
260 | return 0; |
261 | } | |
262 | ||
9df7fa16 AV |
263 | static DEFINE_MUTEX(acct_on_mutex); |
264 | ||
417ef531 RD |
265 | /** |
266 | * sys_acct - enable/disable process accounting | |
267 | * @name: file name for accounting records or NULL to shutdown accounting | |
268 | * | |
269 | * Returns 0 for success or negative errno values for failure. | |
270 | * | |
271 | * sys_acct() is the only system call needed to implement process | |
272 | * accounting. It takes the name of the file where accounting records | |
273 | * should be written. If the filename is NULL, accounting will be | |
274 | * shutdown. | |
1da177e4 | 275 | */ |
b290ebe2 | 276 | SYSCALL_DEFINE1(acct, const char __user *, name) |
1da177e4 | 277 | { |
05b90496 | 278 | int error = 0; |
1da177e4 LT |
279 | |
280 | if (!capable(CAP_SYS_PACCT)) | |
281 | return -EPERM; | |
282 | ||
283 | if (name) { | |
91a27b2a | 284 | struct filename *tmp = getname(name); |
7b7b1ace | 285 | if (IS_ERR(tmp)) |
46c0a8ca | 286 | return PTR_ERR(tmp); |
9df7fa16 | 287 | mutex_lock(&acct_on_mutex); |
669abf4e | 288 | error = acct_on(tmp); |
9df7fa16 | 289 | mutex_unlock(&acct_on_mutex); |
1da177e4 | 290 | putname(tmp); |
7b7b1ace | 291 | } else { |
215752fc | 292 | acct_kill(acct_get(task_active_pid_ns(current)), NULL); |
1da177e4 | 293 | } |
05b90496 | 294 | |
7b7b1ace AV |
295 | return error; |
296 | } | |
1da177e4 | 297 | |
215752fc | 298 | void acct_auto_close_mnt(struct hlist_head *list) |
7b7b1ace | 299 | { |
2798d4ce | 300 | rcu_read_lock(); |
215752fc | 301 | while (1) { |
2798d4ce AV |
302 | struct hlist_node *p = ACCESS_ONCE(list->first); |
303 | if (!p) | |
215752fc | 304 | break; |
2798d4ce | 305 | acct_kill(__acct_get(hlist_entry(p, |
215752fc AV |
306 | struct bsd_acct_struct, |
307 | m_list)), NULL); | |
2798d4ce | 308 | rcu_read_lock(); |
215752fc | 309 | } |
2798d4ce | 310 | rcu_read_unlock(); |
1da177e4 LT |
311 | } |
312 | ||
215752fc | 313 | void acct_auto_close(struct hlist_head *list) |
1da177e4 | 314 | { |
2798d4ce | 315 | rcu_read_lock(); |
215752fc | 316 | while (1) { |
2798d4ce AV |
317 | struct hlist_node *p = ACCESS_ONCE(list->first); |
318 | if (!p) | |
215752fc | 319 | break; |
2798d4ce | 320 | acct_kill(__acct_get(hlist_entry(p, |
215752fc AV |
321 | struct bsd_acct_struct, |
322 | s_list)), NULL); | |
2798d4ce | 323 | rcu_read_lock(); |
215752fc | 324 | } |
2798d4ce | 325 | rcu_read_unlock(); |
0b6b030f PE |
326 | } |
327 | ||
328 | void acct_exit_ns(struct pid_namespace *ns) | |
329 | { | |
215752fc | 330 | acct_kill(acct_get(ns), NULL); |
1da177e4 LT |
331 | } |
332 | ||
333 | /* | |
334 | * encode an unsigned long into a comp_t | |
335 | * | |
336 | * This routine has been adopted from the encode_comp_t() function in | |
337 | * the kern_acct.c file of the FreeBSD operating system. The encoding | |
338 | * is a 13-bit fraction with a 3-bit (base 8) exponent. | |
339 | */ | |
340 | ||
341 | #define MANTSIZE 13 /* 13 bit mantissa. */ | |
342 | #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */ | |
343 | #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */ | |
344 | ||
345 | static comp_t encode_comp_t(unsigned long value) | |
346 | { | |
347 | int exp, rnd; | |
348 | ||
349 | exp = rnd = 0; | |
350 | while (value > MAXFRACT) { | |
351 | rnd = value & (1 << (EXPSIZE - 1)); /* Round up? */ | |
352 | value >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */ | |
353 | exp++; | |
354 | } | |
355 | ||
356 | /* | |
6ae965cd DW |
357 | * If we need to round up, do it (and handle overflow correctly). |
358 | */ | |
1da177e4 LT |
359 | if (rnd && (++value > MAXFRACT)) { |
360 | value >>= EXPSIZE; | |
361 | exp++; | |
362 | } | |
363 | ||
364 | /* | |
6ae965cd DW |
365 | * Clean it up and polish it off. |
366 | */ | |
1da177e4 LT |
367 | exp <<= MANTSIZE; /* Shift the exponent into place */ |
368 | exp += value; /* and add on the mantissa. */ | |
369 | return exp; | |
370 | } | |
371 | ||
372 | #if ACCT_VERSION==1 || ACCT_VERSION==2 | |
373 | /* | |
374 | * encode an u64 into a comp2_t (24 bits) | |
375 | * | |
376 | * Format: 5 bit base 2 exponent, 20 bits mantissa. | |
377 | * The leading bit of the mantissa is not stored, but implied for | |
378 | * non-zero exponents. | |
379 | * Largest encodable value is 50 bits. | |
380 | */ | |
381 | ||
382 | #define MANTSIZE2 20 /* 20 bit mantissa. */ | |
383 | #define EXPSIZE2 5 /* 5 bit base 2 exponent. */ | |
384 | #define MAXFRACT2 ((1ul << MANTSIZE2) - 1) /* Maximum fractional value. */ | |
385 | #define MAXEXP2 ((1 <<EXPSIZE2) - 1) /* Maximum exponent. */ | |
386 | ||
387 | static comp2_t encode_comp2_t(u64 value) | |
388 | { | |
6ae965cd DW |
389 | int exp, rnd; |
390 | ||
391 | exp = (value > (MAXFRACT2>>1)); | |
392 | rnd = 0; | |
393 | while (value > MAXFRACT2) { | |
394 | rnd = value & 1; | |
395 | value >>= 1; | |
396 | exp++; | |
397 | } | |
398 | ||
399 | /* | |
400 | * If we need to round up, do it (and handle overflow correctly). | |
401 | */ | |
402 | if (rnd && (++value > MAXFRACT2)) { | |
403 | value >>= 1; | |
404 | exp++; | |
405 | } | |
406 | ||
407 | if (exp > MAXEXP2) { | |
408 | /* Overflow. Return largest representable number instead. */ | |
409 | return (1ul << (MANTSIZE2+EXPSIZE2-1)) - 1; | |
410 | } else { | |
411 | return (value & (MAXFRACT2>>1)) | (exp << (MANTSIZE2-1)); | |
412 | } | |
1da177e4 LT |
413 | } |
414 | #endif | |
415 | ||
416 | #if ACCT_VERSION==3 | |
417 | /* | |
418 | * encode an u64 into a 32 bit IEEE float | |
419 | */ | |
420 | static u32 encode_float(u64 value) | |
421 | { | |
422 | unsigned exp = 190; | |
423 | unsigned u; | |
424 | ||
425 | if (value==0) return 0; | |
426 | while ((s64)value > 0){ | |
427 | value <<= 1; | |
428 | exp--; | |
429 | } | |
430 | u = (u32)(value >> 40) & 0x7fffffu; | |
431 | return u | (exp << 23); | |
432 | } | |
433 | #endif | |
434 | ||
435 | /* | |
436 | * Write an accounting entry for an exiting process | |
437 | * | |
438 | * The acct_process() call is the workhorse of the process | |
439 | * accounting system. The struct acct is built here and then written | |
440 | * into the accounting file. This function should only be called from | |
bcbe4a07 | 441 | * do_exit() or when switching to a different output file. |
1da177e4 LT |
442 | */ |
443 | ||
cdd37e23 | 444 | static void fill_ac(acct_t *ac) |
1da177e4 | 445 | { |
0e464814 | 446 | struct pacct_struct *pacct = ¤t->signal->pacct; |
ccbf62d8 | 447 | u64 elapsed, run_time; |
24ec839c | 448 | struct tty_struct *tty; |
1da177e4 LT |
449 | |
450 | /* | |
451 | * Fill the accounting struct with the needed info as recorded | |
452 | * by the different kernel functions. | |
453 | */ | |
cdd37e23 | 454 | memset(ac, 0, sizeof(acct_t)); |
1da177e4 | 455 | |
cdd37e23 AV |
456 | ac->ac_version = ACCT_VERSION | ACCT_BYTEORDER; |
457 | strlcpy(ac->ac_comm, current->comm, sizeof(ac->ac_comm)); | |
1da177e4 LT |
458 | |
459 | /* calculate run_time in nsec*/ | |
ccbf62d8 TG |
460 | run_time = ktime_get_ns(); |
461 | run_time -= current->group_leader->start_time; | |
1da177e4 LT |
462 | /* convert nsec -> AHZ */ |
463 | elapsed = nsec_to_AHZ(run_time); | |
464 | #if ACCT_VERSION==3 | |
cdd37e23 | 465 | ac->ac_etime = encode_float(elapsed); |
1da177e4 | 466 | #else |
cdd37e23 | 467 | ac->ac_etime = encode_comp_t(elapsed < (unsigned long) -1l ? |
1da177e4 LT |
468 | (unsigned long) elapsed : (unsigned long) -1l); |
469 | #endif | |
470 | #if ACCT_VERSION==1 || ACCT_VERSION==2 | |
471 | { | |
472 | /* new enlarged etime field */ | |
473 | comp2_t etime = encode_comp2_t(elapsed); | |
cdd37e23 AV |
474 | ac->ac_etime_hi = etime >> 16; |
475 | ac->ac_etime_lo = (u16) etime; | |
1da177e4 LT |
476 | } |
477 | #endif | |
478 | do_div(elapsed, AHZ); | |
cdd37e23 AV |
479 | ac->ac_btime = get_seconds() - elapsed; |
480 | #if ACCT_VERSION==2 | |
481 | ac->ac_ahz = AHZ; | |
482 | #endif | |
483 | ||
484 | spin_lock_irq(¤t->sighand->siglock); | |
485 | tty = current->signal->tty; /* Safe as we hold the siglock */ | |
486 | ac->ac_tty = tty ? old_encode_dev(tty_devnum(tty)) : 0; | |
487 | ac->ac_utime = encode_comp_t(jiffies_to_AHZ(cputime_to_jiffies(pacct->ac_utime))); | |
488 | ac->ac_stime = encode_comp_t(jiffies_to_AHZ(cputime_to_jiffies(pacct->ac_stime))); | |
489 | ac->ac_flag = pacct->ac_flag; | |
490 | ac->ac_mem = encode_comp_t(pacct->ac_mem); | |
491 | ac->ac_minflt = encode_comp_t(pacct->ac_minflt); | |
492 | ac->ac_majflt = encode_comp_t(pacct->ac_majflt); | |
493 | ac->ac_exitcode = pacct->ac_exitcode; | |
494 | spin_unlock_irq(¤t->sighand->siglock); | |
495 | } | |
496 | /* | |
497 | * do_acct_process does all actual work. Caller holds the reference to file. | |
498 | */ | |
b8f00e6b | 499 | static void do_acct_process(struct bsd_acct_struct *acct) |
cdd37e23 AV |
500 | { |
501 | acct_t ac; | |
502 | unsigned long flim; | |
503 | const struct cred *orig_cred; | |
b8f00e6b AV |
504 | struct pid_namespace *ns = acct->ns; |
505 | struct file *file = acct->file; | |
cdd37e23 AV |
506 | |
507 | /* | |
508 | * Accounting records are not subject to resource limits. | |
509 | */ | |
510 | flim = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; | |
511 | current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; | |
512 | /* Perform file operations on behalf of whoever enabled accounting */ | |
513 | orig_cred = override_creds(file->f_cred); | |
514 | ||
515 | /* | |
516 | * First check to see if there is enough free_space to continue | |
517 | * the process accounting system. | |
518 | */ | |
54a4d58a | 519 | if (!check_free_space(acct)) |
cdd37e23 AV |
520 | goto out; |
521 | ||
522 | fill_ac(&ac); | |
1da177e4 | 523 | /* we really need to bite the bullet and change layout */ |
f8f3d4de EB |
524 | ac.ac_uid = from_kuid_munged(file->f_cred->user_ns, orig_cred->uid); |
525 | ac.ac_gid = from_kgid_munged(file->f_cred->user_ns, orig_cred->gid); | |
1da177e4 LT |
526 | #if ACCT_VERSION==1 || ACCT_VERSION==2 |
527 | /* backward-compatible 16 bit fields */ | |
76aac0e9 DH |
528 | ac.ac_uid16 = ac.ac_uid; |
529 | ac.ac_gid16 = ac.ac_gid; | |
1da177e4 LT |
530 | #endif |
531 | #if ACCT_VERSION==3 | |
5f7b703f | 532 | ac.ac_pid = task_tgid_nr_ns(current, ns); |
a846a195 | 533 | rcu_read_lock(); |
5f7b703f | 534 | ac.ac_ppid = task_tgid_nr_ns(rcu_dereference(current->real_parent), ns); |
a846a195 | 535 | rcu_read_unlock(); |
1da177e4 | 536 | #endif |
5ae98f15 JK |
537 | /* |
538 | * Get freeze protection. If the fs is frozen, just skip the write | |
539 | * as we could deadlock the system otherwise. | |
540 | */ | |
ed44724b AV |
541 | if (file_start_write_trylock(file)) { |
542 | /* it's been opened O_APPEND, so position is irrelevant */ | |
543 | loff_t pos = 0; | |
544 | __kernel_write(file, (char *)&ac, sizeof(acct_t), &pos); | |
545 | file_end_write(file); | |
546 | } | |
d8e180dc | 547 | out: |
ed44724b | 548 | current->signal->rlim[RLIMIT_FSIZE].rlim_cur = flim; |
d8e180dc | 549 | revert_creds(orig_cred); |
1da177e4 LT |
550 | } |
551 | ||
0e464814 KK |
552 | /** |
553 | * acct_collect - collect accounting information into pacct_struct | |
f6ec29a4 KK |
554 | * @exitcode: task exit code |
555 | * @group_dead: not 0, if this thread is the last one in the process. | |
0e464814 | 556 | */ |
f6ec29a4 | 557 | void acct_collect(long exitcode, int group_dead) |
0e464814 KK |
558 | { |
559 | struct pacct_struct *pacct = ¤t->signal->pacct; | |
6fac4829 | 560 | cputime_t utime, stime; |
0e464814 KK |
561 | unsigned long vsize = 0; |
562 | ||
f6ec29a4 | 563 | if (group_dead && current->mm) { |
0e464814 KK |
564 | struct vm_area_struct *vma; |
565 | down_read(¤t->mm->mmap_sem); | |
566 | vma = current->mm->mmap; | |
567 | while (vma) { | |
568 | vsize += vma->vm_end - vma->vm_start; | |
569 | vma = vma->vm_next; | |
570 | } | |
571 | up_read(¤t->mm->mmap_sem); | |
572 | } | |
573 | ||
77787bfb | 574 | spin_lock_irq(¤t->sighand->siglock); |
f6ec29a4 KK |
575 | if (group_dead) |
576 | pacct->ac_mem = vsize / 1024; | |
577 | if (thread_group_leader(current)) { | |
578 | pacct->ac_exitcode = exitcode; | |
579 | if (current->flags & PF_FORKNOEXEC) | |
580 | pacct->ac_flag |= AFORK; | |
581 | } | |
582 | if (current->flags & PF_SUPERPRIV) | |
583 | pacct->ac_flag |= ASU; | |
584 | if (current->flags & PF_DUMPCORE) | |
585 | pacct->ac_flag |= ACORE; | |
586 | if (current->flags & PF_SIGNALED) | |
587 | pacct->ac_flag |= AXSIG; | |
6fac4829 FW |
588 | task_cputime(current, &utime, &stime); |
589 | pacct->ac_utime += utime; | |
590 | pacct->ac_stime += stime; | |
77787bfb KK |
591 | pacct->ac_minflt += current->min_flt; |
592 | pacct->ac_majflt += current->maj_flt; | |
593 | spin_unlock_irq(¤t->sighand->siglock); | |
0e464814 KK |
594 | } |
595 | ||
e25ff11f | 596 | static void slow_acct_process(struct pid_namespace *ns) |
1da177e4 | 597 | { |
e25ff11f | 598 | for ( ; ns; ns = ns->parent) { |
215752fc | 599 | struct bsd_acct_struct *acct = acct_get(ns); |
b8f00e6b AV |
600 | if (acct) { |
601 | do_acct_process(acct); | |
602 | mutex_unlock(&acct->lock); | |
603 | acct_put(acct); | |
e25ff11f | 604 | } |
e25ff11f | 605 | } |
1da177e4 | 606 | } |
7d1e1350 PE |
607 | |
608 | /** | |
e25ff11f | 609 | * acct_process |
7d1e1350 PE |
610 | * |
611 | * handles process accounting for an exiting task | |
612 | */ | |
613 | void acct_process(void) | |
614 | { | |
615 | struct pid_namespace *ns; | |
616 | ||
0c18d7a5 PE |
617 | /* |
618 | * This loop is safe lockless, since current is still | |
619 | * alive and holds its namespace, which in turn holds | |
620 | * its parent. | |
621 | */ | |
e25ff11f | 622 | for (ns = task_active_pid_ns(current); ns != NULL; ns = ns->parent) { |
b8f00e6b | 623 | if (ns->bacct) |
e25ff11f AV |
624 | break; |
625 | } | |
626 | if (unlikely(ns)) | |
627 | slow_acct_process(ns); | |
7d1e1350 | 628 | } |