4 * BSD Process Accounting for Linux
6 * Author: Marco van Wieringen <mvw@planets.elm.net>
8 * Some code based on ideas and code from:
9 * Thomas K. Dyas <tdyas@eden.rutgers.edu>
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.
17 * (C) Copyright 1995 - 1997 Marco van Wieringen - ELM Consultancy B.V.
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.
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.
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.
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.
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.
47 #include <linux/slab.h>
48 #include <linux/acct.h>
49 #include <linux/capability.h>
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>
57 #include <linux/mount.h>
58 #include <linux/uaccess.h>
59 #include <asm/div64.h>
60 #include <linux/blkdev.h> /* sector_div */
61 #include <linux/pid_namespace.h>
64 * These constants control the amount of freespace that suspend and
65 * resume the process accounting system, and the time delay between
67 * Turned into sysctl-controllable parameters. AV, 12/11/98
70 int acct_parm[3] = {4, 2, 30};
71 #define RESUME (acct_parm[0]) /* >foo% free space - resume */
72 #define SUSPEND (acct_parm[1]) /* <foo% free space - suspend */
73 #define ACCT_TIMEOUT (acct_parm[2]) /* foo second timeout between checks */
76 * External references and all of the globals.
78 static void do_acct_process(struct bsd_acct_struct *acct);
80 struct bsd_acct_struct {
84 unsigned long needcheck;
86 struct pid_namespace *ns;
87 struct list_head list;
90 static DEFINE_SPINLOCK(acct_lock);
91 static LIST_HEAD(acct_list);
94 * Check the amount of free space and suspend/resume accordingly.
96 static int check_free_space(struct bsd_acct_struct *acct)
100 if (time_is_before_jiffies(acct->needcheck))
104 if (vfs_statfs(&acct->file->f_path, &sbuf))
108 u64 suspend = sbuf.f_blocks * SUSPEND;
109 do_div(suspend, 100);
110 if (sbuf.f_bavail <= suspend) {
112 printk(KERN_INFO "Process accounting paused\n");
115 u64 resume = sbuf.f_blocks * RESUME;
117 if (sbuf.f_bavail >= resume) {
119 printk(KERN_INFO "Process accounting resumed\n");
123 acct->needcheck = jiffies + ACCT_TIMEOUT*HZ;
128 static void acct_put(struct bsd_acct_struct *p)
130 spin_lock(&acct_lock);
133 spin_unlock(&acct_lock);
136 static struct bsd_acct_struct *acct_get(struct bsd_acct_struct **p)
138 struct bsd_acct_struct *res;
139 spin_lock(&acct_lock);
144 spin_unlock(&acct_lock);
146 mutex_lock(&res->lock);
148 mutex_unlock(&res->lock);
149 spin_lock(&acct_lock);
158 static void acct_kill(struct bsd_acct_struct *acct,
159 struct bsd_acct_struct *new)
162 struct file *file = acct->file;
163 struct pid_namespace *ns = acct->ns;
164 spin_lock(&acct_lock);
165 list_del(&acct->list);
166 mnt_unpin(file->f_path.mnt);
167 spin_unlock(&acct_lock);
168 do_acct_process(acct);
169 filp_close(file, NULL);
170 spin_lock(&acct_lock);
173 mnt_pin(new->file->f_path.mnt);
174 list_add(&new->list, &acct_list);
177 mutex_unlock(&acct->lock);
178 if (!(acct->count -= 2))
180 spin_unlock(&acct_lock);
184 static int acct_on(struct filename *pathname)
187 struct vfsmount *mnt;
188 struct pid_namespace *ns = task_active_pid_ns(current);
189 struct bsd_acct_struct *acct, *old;
191 acct = kzalloc(sizeof(struct bsd_acct_struct), GFP_KERNEL);
195 /* Difference from BSD - they don't do O_APPEND */
196 file = file_open_name(pathname, O_WRONLY|O_APPEND|O_LARGEFILE, 0);
199 return PTR_ERR(file);
202 if (!S_ISREG(file_inode(file)->i_mode)) {
204 filp_close(file, NULL);
208 if (!file->f_op->write) {
210 filp_close(file, NULL);
216 acct->needcheck = jiffies;
218 mutex_init(&acct->lock);
219 mnt = file->f_path.mnt;
221 old = acct_get(&ns->bacct);
223 acct_kill(old, acct);
225 spin_lock(&acct_lock);
228 list_add(&acct->list, &acct_list);
229 spin_unlock(&acct_lock);
231 mntput(mnt); /* it's pinned, now give up active reference */
235 static DEFINE_MUTEX(acct_on_mutex);
238 * sys_acct - enable/disable process accounting
239 * @name: file name for accounting records or NULL to shutdown accounting
241 * Returns 0 for success or negative errno values for failure.
243 * sys_acct() is the only system call needed to implement process
244 * accounting. It takes the name of the file where accounting records
245 * should be written. If the filename is NULL, accounting will be
248 SYSCALL_DEFINE1(acct, const char __user *, name)
252 if (!capable(CAP_SYS_PACCT))
256 struct filename *tmp = getname(name);
259 mutex_lock(&acct_on_mutex);
260 error = acct_on(tmp);
261 mutex_unlock(&acct_on_mutex);
264 acct_kill(acct_get(&task_active_pid_ns(current)->bacct), NULL);
271 * acct_auto_close - turn off a filesystem's accounting if it is on
272 * @m: vfsmount being shut down
274 * If the accounting is turned on for a file in the subtree pointed to
275 * to by m, turn accounting off. Done when m is about to die.
277 void acct_auto_close_mnt(struct vfsmount *m)
279 struct bsd_acct_struct *acct;
281 spin_lock(&acct_lock);
283 list_for_each_entry(acct, &acct_list, list)
284 if (acct->file->f_path.mnt == m) {
286 spin_unlock(&acct_lock);
287 mutex_lock(&acct->lock);
289 mutex_unlock(&acct->lock);
290 spin_lock(&acct_lock);
295 acct_kill(acct, NULL);
296 spin_lock(&acct_lock);
299 spin_unlock(&acct_lock);
303 * acct_auto_close - turn off a filesystem's accounting if it is on
304 * @sb: super block for the filesystem
306 * If the accounting is turned on for a file in the filesystem pointed
307 * to by sb, turn accounting off.
309 void acct_auto_close(struct super_block *sb)
311 struct bsd_acct_struct *acct;
313 spin_lock(&acct_lock);
315 list_for_each_entry(acct, &acct_list, list)
316 if (acct->file->f_path.dentry->d_sb == sb) {
318 spin_unlock(&acct_lock);
319 mutex_lock(&acct->lock);
321 mutex_unlock(&acct->lock);
322 spin_lock(&acct_lock);
327 acct_kill(acct, NULL);
328 spin_lock(&acct_lock);
331 spin_unlock(&acct_lock);
334 void acct_exit_ns(struct pid_namespace *ns)
336 acct_kill(acct_get(&ns->bacct), NULL);
340 * encode an unsigned long into a comp_t
342 * This routine has been adopted from the encode_comp_t() function in
343 * the kern_acct.c file of the FreeBSD operating system. The encoding
344 * is a 13-bit fraction with a 3-bit (base 8) exponent.
347 #define MANTSIZE 13 /* 13 bit mantissa. */
348 #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */
349 #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */
351 static comp_t encode_comp_t(unsigned long value)
356 while (value > MAXFRACT) {
357 rnd = value & (1 << (EXPSIZE - 1)); /* Round up? */
358 value >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */
363 * If we need to round up, do it (and handle overflow correctly).
365 if (rnd && (++value > MAXFRACT)) {
371 * Clean it up and polish it off.
373 exp <<= MANTSIZE; /* Shift the exponent into place */
374 exp += value; /* and add on the mantissa. */
378 #if ACCT_VERSION==1 || ACCT_VERSION==2
380 * encode an u64 into a comp2_t (24 bits)
382 * Format: 5 bit base 2 exponent, 20 bits mantissa.
383 * The leading bit of the mantissa is not stored, but implied for
384 * non-zero exponents.
385 * Largest encodable value is 50 bits.
388 #define MANTSIZE2 20 /* 20 bit mantissa. */
389 #define EXPSIZE2 5 /* 5 bit base 2 exponent. */
390 #define MAXFRACT2 ((1ul << MANTSIZE2) - 1) /* Maximum fractional value. */
391 #define MAXEXP2 ((1 <<EXPSIZE2) - 1) /* Maximum exponent. */
393 static comp2_t encode_comp2_t(u64 value)
397 exp = (value > (MAXFRACT2>>1));
399 while (value > MAXFRACT2) {
406 * If we need to round up, do it (and handle overflow correctly).
408 if (rnd && (++value > MAXFRACT2)) {
414 /* Overflow. Return largest representable number instead. */
415 return (1ul << (MANTSIZE2+EXPSIZE2-1)) - 1;
417 return (value & (MAXFRACT2>>1)) | (exp << (MANTSIZE2-1));
424 * encode an u64 into a 32 bit IEEE float
426 static u32 encode_float(u64 value)
431 if (value==0) return 0;
432 while ((s64)value > 0){
436 u = (u32)(value >> 40) & 0x7fffffu;
437 return u | (exp << 23);
442 * Write an accounting entry for an exiting process
444 * The acct_process() call is the workhorse of the process
445 * accounting system. The struct acct is built here and then written
446 * into the accounting file. This function should only be called from
447 * do_exit() or when switching to a different output file.
450 static void fill_ac(acct_t *ac)
452 struct pacct_struct *pacct = ¤t->signal->pacct;
453 u64 elapsed, run_time;
454 struct tty_struct *tty;
457 * Fill the accounting struct with the needed info as recorded
458 * by the different kernel functions.
460 memset(ac, 0, sizeof(acct_t));
462 ac->ac_version = ACCT_VERSION | ACCT_BYTEORDER;
463 strlcpy(ac->ac_comm, current->comm, sizeof(ac->ac_comm));
465 /* calculate run_time in nsec*/
466 run_time = ktime_get_ns();
467 run_time -= current->group_leader->start_time;
468 /* convert nsec -> AHZ */
469 elapsed = nsec_to_AHZ(run_time);
471 ac->ac_etime = encode_float(elapsed);
473 ac->ac_etime = encode_comp_t(elapsed < (unsigned long) -1l ?
474 (unsigned long) elapsed : (unsigned long) -1l);
476 #if ACCT_VERSION==1 || ACCT_VERSION==2
478 /* new enlarged etime field */
479 comp2_t etime = encode_comp2_t(elapsed);
480 ac->ac_etime_hi = etime >> 16;
481 ac->ac_etime_lo = (u16) etime;
484 do_div(elapsed, AHZ);
485 ac->ac_btime = get_seconds() - elapsed;
490 spin_lock_irq(¤t->sighand->siglock);
491 tty = current->signal->tty; /* Safe as we hold the siglock */
492 ac->ac_tty = tty ? old_encode_dev(tty_devnum(tty)) : 0;
493 ac->ac_utime = encode_comp_t(jiffies_to_AHZ(cputime_to_jiffies(pacct->ac_utime)));
494 ac->ac_stime = encode_comp_t(jiffies_to_AHZ(cputime_to_jiffies(pacct->ac_stime)));
495 ac->ac_flag = pacct->ac_flag;
496 ac->ac_mem = encode_comp_t(pacct->ac_mem);
497 ac->ac_minflt = encode_comp_t(pacct->ac_minflt);
498 ac->ac_majflt = encode_comp_t(pacct->ac_majflt);
499 ac->ac_exitcode = pacct->ac_exitcode;
500 spin_unlock_irq(¤t->sighand->siglock);
503 * do_acct_process does all actual work. Caller holds the reference to file.
505 static void do_acct_process(struct bsd_acct_struct *acct)
509 const struct cred *orig_cred;
510 struct pid_namespace *ns = acct->ns;
511 struct file *file = acct->file;
514 * Accounting records are not subject to resource limits.
516 flim = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
517 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
518 /* Perform file operations on behalf of whoever enabled accounting */
519 orig_cred = override_creds(file->f_cred);
522 * First check to see if there is enough free_space to continue
523 * the process accounting system.
525 if (!check_free_space(acct))
529 /* we really need to bite the bullet and change layout */
530 ac.ac_uid = from_kuid_munged(file->f_cred->user_ns, orig_cred->uid);
531 ac.ac_gid = from_kgid_munged(file->f_cred->user_ns, orig_cred->gid);
532 #if ACCT_VERSION==1 || ACCT_VERSION==2
533 /* backward-compatible 16 bit fields */
534 ac.ac_uid16 = ac.ac_uid;
535 ac.ac_gid16 = ac.ac_gid;
538 ac.ac_pid = task_tgid_nr_ns(current, ns);
540 ac.ac_ppid = task_tgid_nr_ns(rcu_dereference(current->real_parent), ns);
544 * Get freeze protection. If the fs is frozen, just skip the write
545 * as we could deadlock the system otherwise.
547 if (file_start_write_trylock(file)) {
548 /* it's been opened O_APPEND, so position is irrelevant */
550 __kernel_write(file, (char *)&ac, sizeof(acct_t), &pos);
551 file_end_write(file);
554 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = flim;
555 revert_creds(orig_cred);
559 * acct_collect - collect accounting information into pacct_struct
560 * @exitcode: task exit code
561 * @group_dead: not 0, if this thread is the last one in the process.
563 void acct_collect(long exitcode, int group_dead)
565 struct pacct_struct *pacct = ¤t->signal->pacct;
566 cputime_t utime, stime;
567 unsigned long vsize = 0;
569 if (group_dead && current->mm) {
570 struct vm_area_struct *vma;
571 down_read(¤t->mm->mmap_sem);
572 vma = current->mm->mmap;
574 vsize += vma->vm_end - vma->vm_start;
577 up_read(¤t->mm->mmap_sem);
580 spin_lock_irq(¤t->sighand->siglock);
582 pacct->ac_mem = vsize / 1024;
583 if (thread_group_leader(current)) {
584 pacct->ac_exitcode = exitcode;
585 if (current->flags & PF_FORKNOEXEC)
586 pacct->ac_flag |= AFORK;
588 if (current->flags & PF_SUPERPRIV)
589 pacct->ac_flag |= ASU;
590 if (current->flags & PF_DUMPCORE)
591 pacct->ac_flag |= ACORE;
592 if (current->flags & PF_SIGNALED)
593 pacct->ac_flag |= AXSIG;
594 task_cputime(current, &utime, &stime);
595 pacct->ac_utime += utime;
596 pacct->ac_stime += stime;
597 pacct->ac_minflt += current->min_flt;
598 pacct->ac_majflt += current->maj_flt;
599 spin_unlock_irq(¤t->sighand->siglock);
602 static void slow_acct_process(struct pid_namespace *ns)
604 for ( ; ns; ns = ns->parent) {
605 struct bsd_acct_struct *acct = acct_get(&ns->bacct);
607 do_acct_process(acct);
608 mutex_unlock(&acct->lock);
617 * handles process accounting for an exiting task
619 void acct_process(void)
621 struct pid_namespace *ns;
624 * This loop is safe lockless, since current is still
625 * alive and holds its namespace, which in turn holds
628 for (ns = task_active_pid_ns(current); ns != NULL; ns = ns->parent) {
633 slow_acct_process(ns);