acct.c 15 KB

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