acct.c 15 KB

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