acct.c 17 KB

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