acct.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. #include <linux/mm.h>
  46. #include <linux/slab.h>
  47. #include <linux/acct.h>
  48. #include <linux/capability.h>
  49. #include <linux/file.h>
  50. #include <linux/tty.h>
  51. #include <linux/security.h>
  52. #include <linux/vfs.h>
  53. #include <linux/jiffies.h>
  54. #include <linux/times.h>
  55. #include <linux/syscalls.h>
  56. #include <linux/mount.h>
  57. #include <linux/uaccess.h>
  58. #include <asm/div64.h>
  59. #include <linux/blkdev.h> /* sector_div */
  60. #include <linux/pid_namespace.h>
  61. #include <../fs/mount.h> /* will go away when we refactor */
  62. /*
  63. * These constants control the amount of freespace that suspend and
  64. * resume the process accounting system, and the time delay between
  65. * each check.
  66. * Turned into sysctl-controllable parameters. AV, 12/11/98
  67. */
  68. int acct_parm[3] = {4, 2, 30};
  69. #define RESUME (acct_parm[0]) /* >foo% free space - resume */
  70. #define SUSPEND (acct_parm[1]) /* <foo% free space - suspend */
  71. #define ACCT_TIMEOUT (acct_parm[2]) /* foo second timeout between checks */
  72. /*
  73. * External references and all of the globals.
  74. */
  75. static void do_acct_process(struct bsd_acct_struct *acct);
  76. struct bsd_acct_struct {
  77. atomic_long_t count;
  78. union {
  79. struct {
  80. struct hlist_node s_list;
  81. struct hlist_node m_list;
  82. };
  83. struct rcu_head rcu;
  84. };
  85. struct mutex lock;
  86. int active;
  87. unsigned long needcheck;
  88. struct file *file;
  89. struct pid_namespace *ns;
  90. };
  91. static void acct_free_rcu(struct rcu_head *head)
  92. {
  93. kfree(container_of(head, struct bsd_acct_struct, rcu));
  94. }
  95. static DEFINE_SPINLOCK(acct_lock);
  96. /*
  97. * Check the amount of free space and suspend/resume accordingly.
  98. */
  99. static int check_free_space(struct bsd_acct_struct *acct)
  100. {
  101. struct kstatfs sbuf;
  102. if (time_is_before_jiffies(acct->needcheck))
  103. goto out;
  104. /* May block */
  105. if (vfs_statfs(&acct->file->f_path, &sbuf))
  106. goto out;
  107. if (acct->active) {
  108. u64 suspend = sbuf.f_blocks * SUSPEND;
  109. do_div(suspend, 100);
  110. if (sbuf.f_bavail <= suspend) {
  111. acct->active = 0;
  112. printk(KERN_INFO "Process accounting paused\n");
  113. }
  114. } else {
  115. u64 resume = sbuf.f_blocks * RESUME;
  116. do_div(resume, 100);
  117. if (sbuf.f_bavail >= resume) {
  118. acct->active = 1;
  119. printk(KERN_INFO "Process accounting resumed\n");
  120. }
  121. }
  122. acct->needcheck = jiffies + ACCT_TIMEOUT*HZ;
  123. out:
  124. return acct->active;
  125. }
  126. static void acct_put(struct bsd_acct_struct *p)
  127. {
  128. if (atomic_long_dec_and_test(&p->count))
  129. call_rcu(&p->rcu, acct_free_rcu);
  130. }
  131. static struct bsd_acct_struct *__acct_get(struct bsd_acct_struct *res)
  132. {
  133. if (!atomic_long_inc_not_zero(&res->count)) {
  134. rcu_read_unlock();
  135. cpu_relax();
  136. return NULL;
  137. }
  138. rcu_read_unlock();
  139. mutex_lock(&res->lock);
  140. if (!res->ns) {
  141. mutex_unlock(&res->lock);
  142. acct_put(res);
  143. return NULL;
  144. }
  145. return res;
  146. }
  147. static struct bsd_acct_struct *acct_get(struct pid_namespace *ns)
  148. {
  149. struct bsd_acct_struct *res;
  150. again:
  151. smp_rmb();
  152. rcu_read_lock();
  153. res = ACCESS_ONCE(ns->bacct);
  154. if (!res) {
  155. rcu_read_unlock();
  156. return NULL;
  157. }
  158. res = __acct_get(res);
  159. if (!res)
  160. goto again;
  161. return res;
  162. }
  163. static void acct_kill(struct bsd_acct_struct *acct,
  164. struct bsd_acct_struct *new)
  165. {
  166. if (acct) {
  167. struct file *file = acct->file;
  168. struct pid_namespace *ns = acct->ns;
  169. do_acct_process(acct);
  170. mnt_unpin(file->f_path.mnt);
  171. filp_close(file, NULL);
  172. spin_lock(&acct_lock);
  173. hlist_del(&acct->m_list);
  174. hlist_del(&acct->s_list);
  175. spin_unlock(&acct_lock);
  176. ns->bacct = new;
  177. if (new) {
  178. struct vfsmount *m = new->file->f_path.mnt;
  179. mnt_pin(m);
  180. spin_lock(&acct_lock);
  181. hlist_add_head(&new->s_list, &m->mnt_sb->s_pins);
  182. hlist_add_head(&new->m_list, &real_mount(m)->mnt_pins);
  183. spin_unlock(&acct_lock);
  184. mutex_unlock(&new->lock);
  185. }
  186. acct->ns = NULL;
  187. atomic_long_dec(&acct->count);
  188. mutex_unlock(&acct->lock);
  189. acct_put(acct);
  190. }
  191. }
  192. static int acct_on(struct filename *pathname)
  193. {
  194. struct file *file;
  195. struct vfsmount *mnt;
  196. struct pid_namespace *ns = task_active_pid_ns(current);
  197. struct bsd_acct_struct *acct, *old;
  198. acct = kzalloc(sizeof(struct bsd_acct_struct), GFP_KERNEL);
  199. if (!acct)
  200. return -ENOMEM;
  201. /* Difference from BSD - they don't do O_APPEND */
  202. file = file_open_name(pathname, O_WRONLY|O_APPEND|O_LARGEFILE, 0);
  203. if (IS_ERR(file)) {
  204. kfree(acct);
  205. return PTR_ERR(file);
  206. }
  207. if (!S_ISREG(file_inode(file)->i_mode)) {
  208. kfree(acct);
  209. filp_close(file, NULL);
  210. return -EACCES;
  211. }
  212. if (!file->f_op->write) {
  213. kfree(acct);
  214. filp_close(file, NULL);
  215. return -EIO;
  216. }
  217. atomic_long_set(&acct->count, 1);
  218. acct->file = file;
  219. acct->needcheck = jiffies;
  220. acct->ns = ns;
  221. mutex_init(&acct->lock);
  222. mnt = file->f_path.mnt;
  223. old = acct_get(ns);
  224. mutex_lock_nested(&acct->lock, 1); /* nobody has seen it yet */
  225. if (old) {
  226. acct_kill(old, acct);
  227. } else {
  228. ns->bacct = acct;
  229. spin_lock(&acct_lock);
  230. mnt_pin(mnt);
  231. hlist_add_head(&acct->s_list, &mnt->mnt_sb->s_pins);
  232. hlist_add_head(&acct->m_list, &real_mount(mnt)->mnt_pins);
  233. spin_unlock(&acct_lock);
  234. mutex_unlock(&acct->lock);
  235. }
  236. mntput(mnt); /* it's pinned, now give up active reference */
  237. return 0;
  238. }
  239. static DEFINE_MUTEX(acct_on_mutex);
  240. /**
  241. * sys_acct - enable/disable process accounting
  242. * @name: file name for accounting records or NULL to shutdown accounting
  243. *
  244. * Returns 0 for success or negative errno values for failure.
  245. *
  246. * sys_acct() is the only system call needed to implement process
  247. * accounting. It takes the name of the file where accounting records
  248. * should be written. If the filename is NULL, accounting will be
  249. * shutdown.
  250. */
  251. SYSCALL_DEFINE1(acct, const char __user *, name)
  252. {
  253. int error = 0;
  254. if (!capable(CAP_SYS_PACCT))
  255. return -EPERM;
  256. if (name) {
  257. struct filename *tmp = getname(name);
  258. if (IS_ERR(tmp))
  259. return PTR_ERR(tmp);
  260. mutex_lock(&acct_on_mutex);
  261. error = acct_on(tmp);
  262. mutex_unlock(&acct_on_mutex);
  263. putname(tmp);
  264. } else {
  265. acct_kill(acct_get(task_active_pid_ns(current)), NULL);
  266. }
  267. return error;
  268. }
  269. void acct_auto_close_mnt(struct hlist_head *list)
  270. {
  271. rcu_read_lock();
  272. while (1) {
  273. struct hlist_node *p = ACCESS_ONCE(list->first);
  274. if (!p)
  275. break;
  276. acct_kill(__acct_get(hlist_entry(p,
  277. struct bsd_acct_struct,
  278. m_list)), NULL);
  279. rcu_read_lock();
  280. }
  281. rcu_read_unlock();
  282. }
  283. void acct_auto_close(struct hlist_head *list)
  284. {
  285. rcu_read_lock();
  286. while (1) {
  287. struct hlist_node *p = ACCESS_ONCE(list->first);
  288. if (!p)
  289. break;
  290. acct_kill(__acct_get(hlist_entry(p,
  291. struct bsd_acct_struct,
  292. s_list)), NULL);
  293. rcu_read_lock();
  294. }
  295. rcu_read_unlock();
  296. }
  297. void acct_exit_ns(struct pid_namespace *ns)
  298. {
  299. acct_kill(acct_get(ns), NULL);
  300. }
  301. /*
  302. * encode an unsigned long into a comp_t
  303. *
  304. * This routine has been adopted from the encode_comp_t() function in
  305. * the kern_acct.c file of the FreeBSD operating system. The encoding
  306. * is a 13-bit fraction with a 3-bit (base 8) exponent.
  307. */
  308. #define MANTSIZE 13 /* 13 bit mantissa. */
  309. #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */
  310. #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */
  311. static comp_t encode_comp_t(unsigned long value)
  312. {
  313. int exp, rnd;
  314. exp = rnd = 0;
  315. while (value > MAXFRACT) {
  316. rnd = value & (1 << (EXPSIZE - 1)); /* Round up? */
  317. value >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */
  318. exp++;
  319. }
  320. /*
  321. * If we need to round up, do it (and handle overflow correctly).
  322. */
  323. if (rnd && (++value > MAXFRACT)) {
  324. value >>= EXPSIZE;
  325. exp++;
  326. }
  327. /*
  328. * Clean it up and polish it off.
  329. */
  330. exp <<= MANTSIZE; /* Shift the exponent into place */
  331. exp += value; /* and add on the mantissa. */
  332. return exp;
  333. }
  334. #if ACCT_VERSION==1 || ACCT_VERSION==2
  335. /*
  336. * encode an u64 into a comp2_t (24 bits)
  337. *
  338. * Format: 5 bit base 2 exponent, 20 bits mantissa.
  339. * The leading bit of the mantissa is not stored, but implied for
  340. * non-zero exponents.
  341. * Largest encodable value is 50 bits.
  342. */
  343. #define MANTSIZE2 20 /* 20 bit mantissa. */
  344. #define EXPSIZE2 5 /* 5 bit base 2 exponent. */
  345. #define MAXFRACT2 ((1ul << MANTSIZE2) - 1) /* Maximum fractional value. */
  346. #define MAXEXP2 ((1 <<EXPSIZE2) - 1) /* Maximum exponent. */
  347. static comp2_t encode_comp2_t(u64 value)
  348. {
  349. int exp, rnd;
  350. exp = (value > (MAXFRACT2>>1));
  351. rnd = 0;
  352. while (value > MAXFRACT2) {
  353. rnd = value & 1;
  354. value >>= 1;
  355. exp++;
  356. }
  357. /*
  358. * If we need to round up, do it (and handle overflow correctly).
  359. */
  360. if (rnd && (++value > MAXFRACT2)) {
  361. value >>= 1;
  362. exp++;
  363. }
  364. if (exp > MAXEXP2) {
  365. /* Overflow. Return largest representable number instead. */
  366. return (1ul << (MANTSIZE2+EXPSIZE2-1)) - 1;
  367. } else {
  368. return (value & (MAXFRACT2>>1)) | (exp << (MANTSIZE2-1));
  369. }
  370. }
  371. #endif
  372. #if ACCT_VERSION==3
  373. /*
  374. * encode an u64 into a 32 bit IEEE float
  375. */
  376. static u32 encode_float(u64 value)
  377. {
  378. unsigned exp = 190;
  379. unsigned u;
  380. if (value==0) return 0;
  381. while ((s64)value > 0){
  382. value <<= 1;
  383. exp--;
  384. }
  385. u = (u32)(value >> 40) & 0x7fffffu;
  386. return u | (exp << 23);
  387. }
  388. #endif
  389. /*
  390. * Write an accounting entry for an exiting process
  391. *
  392. * The acct_process() call is the workhorse of the process
  393. * accounting system. The struct acct is built here and then written
  394. * into the accounting file. This function should only be called from
  395. * do_exit() or when switching to a different output file.
  396. */
  397. static void fill_ac(acct_t *ac)
  398. {
  399. struct pacct_struct *pacct = &current->signal->pacct;
  400. u64 elapsed, run_time;
  401. struct tty_struct *tty;
  402. /*
  403. * Fill the accounting struct with the needed info as recorded
  404. * by the different kernel functions.
  405. */
  406. memset(ac, 0, sizeof(acct_t));
  407. ac->ac_version = ACCT_VERSION | ACCT_BYTEORDER;
  408. strlcpy(ac->ac_comm, current->comm, sizeof(ac->ac_comm));
  409. /* calculate run_time in nsec*/
  410. run_time = ktime_get_ns();
  411. run_time -= current->group_leader->start_time;
  412. /* convert nsec -> AHZ */
  413. elapsed = nsec_to_AHZ(run_time);
  414. #if ACCT_VERSION==3
  415. ac->ac_etime = encode_float(elapsed);
  416. #else
  417. ac->ac_etime = encode_comp_t(elapsed < (unsigned long) -1l ?
  418. (unsigned long) elapsed : (unsigned long) -1l);
  419. #endif
  420. #if ACCT_VERSION==1 || ACCT_VERSION==2
  421. {
  422. /* new enlarged etime field */
  423. comp2_t etime = encode_comp2_t(elapsed);
  424. ac->ac_etime_hi = etime >> 16;
  425. ac->ac_etime_lo = (u16) etime;
  426. }
  427. #endif
  428. do_div(elapsed, AHZ);
  429. ac->ac_btime = get_seconds() - elapsed;
  430. #if ACCT_VERSION==2
  431. ac->ac_ahz = AHZ;
  432. #endif
  433. spin_lock_irq(&current->sighand->siglock);
  434. tty = current->signal->tty; /* Safe as we hold the siglock */
  435. ac->ac_tty = tty ? old_encode_dev(tty_devnum(tty)) : 0;
  436. ac->ac_utime = encode_comp_t(jiffies_to_AHZ(cputime_to_jiffies(pacct->ac_utime)));
  437. ac->ac_stime = encode_comp_t(jiffies_to_AHZ(cputime_to_jiffies(pacct->ac_stime)));
  438. ac->ac_flag = pacct->ac_flag;
  439. ac->ac_mem = encode_comp_t(pacct->ac_mem);
  440. ac->ac_minflt = encode_comp_t(pacct->ac_minflt);
  441. ac->ac_majflt = encode_comp_t(pacct->ac_majflt);
  442. ac->ac_exitcode = pacct->ac_exitcode;
  443. spin_unlock_irq(&current->sighand->siglock);
  444. }
  445. /*
  446. * do_acct_process does all actual work. Caller holds the reference to file.
  447. */
  448. static void do_acct_process(struct bsd_acct_struct *acct)
  449. {
  450. acct_t ac;
  451. unsigned long flim;
  452. const struct cred *orig_cred;
  453. struct pid_namespace *ns = acct->ns;
  454. struct file *file = acct->file;
  455. /*
  456. * Accounting records are not subject to resource limits.
  457. */
  458. flim = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  459. current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
  460. /* Perform file operations on behalf of whoever enabled accounting */
  461. orig_cred = override_creds(file->f_cred);
  462. /*
  463. * First check to see if there is enough free_space to continue
  464. * the process accounting system.
  465. */
  466. if (!check_free_space(acct))
  467. goto out;
  468. fill_ac(&ac);
  469. /* we really need to bite the bullet and change layout */
  470. ac.ac_uid = from_kuid_munged(file->f_cred->user_ns, orig_cred->uid);
  471. ac.ac_gid = from_kgid_munged(file->f_cred->user_ns, orig_cred->gid);
  472. #if ACCT_VERSION==1 || ACCT_VERSION==2
  473. /* backward-compatible 16 bit fields */
  474. ac.ac_uid16 = ac.ac_uid;
  475. ac.ac_gid16 = ac.ac_gid;
  476. #endif
  477. #if ACCT_VERSION==3
  478. ac.ac_pid = task_tgid_nr_ns(current, ns);
  479. rcu_read_lock();
  480. ac.ac_ppid = task_tgid_nr_ns(rcu_dereference(current->real_parent), ns);
  481. rcu_read_unlock();
  482. #endif
  483. /*
  484. * Get freeze protection. If the fs is frozen, just skip the write
  485. * as we could deadlock the system otherwise.
  486. */
  487. if (file_start_write_trylock(file)) {
  488. /* it's been opened O_APPEND, so position is irrelevant */
  489. loff_t pos = 0;
  490. __kernel_write(file, (char *)&ac, sizeof(acct_t), &pos);
  491. file_end_write(file);
  492. }
  493. out:
  494. current->signal->rlim[RLIMIT_FSIZE].rlim_cur = flim;
  495. revert_creds(orig_cred);
  496. }
  497. /**
  498. * acct_collect - collect accounting information into pacct_struct
  499. * @exitcode: task exit code
  500. * @group_dead: not 0, if this thread is the last one in the process.
  501. */
  502. void acct_collect(long exitcode, int group_dead)
  503. {
  504. struct pacct_struct *pacct = &current->signal->pacct;
  505. cputime_t utime, stime;
  506. unsigned long vsize = 0;
  507. if (group_dead && current->mm) {
  508. struct vm_area_struct *vma;
  509. down_read(&current->mm->mmap_sem);
  510. vma = current->mm->mmap;
  511. while (vma) {
  512. vsize += vma->vm_end - vma->vm_start;
  513. vma = vma->vm_next;
  514. }
  515. up_read(&current->mm->mmap_sem);
  516. }
  517. spin_lock_irq(&current->sighand->siglock);
  518. if (group_dead)
  519. pacct->ac_mem = vsize / 1024;
  520. if (thread_group_leader(current)) {
  521. pacct->ac_exitcode = exitcode;
  522. if (current->flags & PF_FORKNOEXEC)
  523. pacct->ac_flag |= AFORK;
  524. }
  525. if (current->flags & PF_SUPERPRIV)
  526. pacct->ac_flag |= ASU;
  527. if (current->flags & PF_DUMPCORE)
  528. pacct->ac_flag |= ACORE;
  529. if (current->flags & PF_SIGNALED)
  530. pacct->ac_flag |= AXSIG;
  531. task_cputime(current, &utime, &stime);
  532. pacct->ac_utime += utime;
  533. pacct->ac_stime += stime;
  534. pacct->ac_minflt += current->min_flt;
  535. pacct->ac_majflt += current->maj_flt;
  536. spin_unlock_irq(&current->sighand->siglock);
  537. }
  538. static void slow_acct_process(struct pid_namespace *ns)
  539. {
  540. for ( ; ns; ns = ns->parent) {
  541. struct bsd_acct_struct *acct = acct_get(ns);
  542. if (acct) {
  543. do_acct_process(acct);
  544. mutex_unlock(&acct->lock);
  545. acct_put(acct);
  546. }
  547. }
  548. }
  549. /**
  550. * acct_process
  551. *
  552. * handles process accounting for an exiting task
  553. */
  554. void acct_process(void)
  555. {
  556. struct pid_namespace *ns;
  557. /*
  558. * This loop is safe lockless, since current is still
  559. * alive and holds its namespace, which in turn holds
  560. * its parent.
  561. */
  562. for (ns = task_active_pid_ns(current); ns != NULL; ns = ns->parent) {
  563. if (ns->bacct)
  564. break;
  565. }
  566. if (unlikely(ns))
  567. slow_acct_process(ns);
  568. }