acct.c 14 KB

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