acct.c 16 KB

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