acct.c 17 KB

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