exit.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. /*
  2. * linux/kernel/exit.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/mm.h>
  7. #include <linux/slab.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/module.h>
  10. #include <linux/capability.h>
  11. #include <linux/completion.h>
  12. #include <linux/personality.h>
  13. #include <linux/tty.h>
  14. #include <linux/mnt_namespace.h>
  15. #include <linux/iocontext.h>
  16. #include <linux/key.h>
  17. #include <linux/security.h>
  18. #include <linux/cpu.h>
  19. #include <linux/acct.h>
  20. #include <linux/tsacct_kern.h>
  21. #include <linux/file.h>
  22. #include <linux/fdtable.h>
  23. #include <linux/binfmts.h>
  24. #include <linux/nsproxy.h>
  25. #include <linux/pid_namespace.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/profile.h>
  28. #include <linux/mount.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/kthread.h>
  31. #include <linux/mempolicy.h>
  32. #include <linux/taskstats_kern.h>
  33. #include <linux/delayacct.h>
  34. #include <linux/freezer.h>
  35. #include <linux/cgroup.h>
  36. #include <linux/syscalls.h>
  37. #include <linux/signal.h>
  38. #include <linux/posix-timers.h>
  39. #include <linux/cn_proc.h>
  40. #include <linux/mutex.h>
  41. #include <linux/futex.h>
  42. #include <linux/compat.h>
  43. #include <linux/pipe_fs_i.h>
  44. #include <linux/audit.h> /* for audit_free() */
  45. #include <linux/resource.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/task_io_accounting_ops.h>
  48. #include <linux/tracehook.h>
  49. #include <asm/uaccess.h>
  50. #include <asm/unistd.h>
  51. #include <asm/pgtable.h>
  52. #include <asm/mmu_context.h>
  53. static void exit_mm(struct task_struct * tsk);
  54. static inline int task_detached(struct task_struct *p)
  55. {
  56. return p->exit_signal == -1;
  57. }
  58. static void __unhash_process(struct task_struct *p)
  59. {
  60. nr_threads--;
  61. detach_pid(p, PIDTYPE_PID);
  62. if (thread_group_leader(p)) {
  63. detach_pid(p, PIDTYPE_PGID);
  64. detach_pid(p, PIDTYPE_SID);
  65. list_del_rcu(&p->tasks);
  66. __get_cpu_var(process_counts)--;
  67. }
  68. list_del_rcu(&p->thread_group);
  69. list_del_init(&p->sibling);
  70. }
  71. /*
  72. * This function expects the tasklist_lock write-locked.
  73. */
  74. static void __exit_signal(struct task_struct *tsk)
  75. {
  76. struct signal_struct *sig = tsk->signal;
  77. struct sighand_struct *sighand;
  78. BUG_ON(!sig);
  79. BUG_ON(!atomic_read(&sig->count));
  80. sighand = rcu_dereference(tsk->sighand);
  81. spin_lock(&sighand->siglock);
  82. posix_cpu_timers_exit(tsk);
  83. if (atomic_dec_and_test(&sig->count))
  84. posix_cpu_timers_exit_group(tsk);
  85. else {
  86. /*
  87. * If there is any task waiting for the group exit
  88. * then notify it:
  89. */
  90. if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count)
  91. wake_up_process(sig->group_exit_task);
  92. if (tsk == sig->curr_target)
  93. sig->curr_target = next_thread(tsk);
  94. /*
  95. * Accumulate here the counters for all threads but the
  96. * group leader as they die, so they can be added into
  97. * the process-wide totals when those are taken.
  98. * The group leader stays around as a zombie as long
  99. * as there are other threads. When it gets reaped,
  100. * the exit.c code will add its counts into these totals.
  101. * We won't ever get here for the group leader, since it
  102. * will have been the last reference on the signal_struct.
  103. */
  104. sig->utime = cputime_add(sig->utime, tsk->utime);
  105. sig->stime = cputime_add(sig->stime, tsk->stime);
  106. sig->gtime = cputime_add(sig->gtime, tsk->gtime);
  107. sig->min_flt += tsk->min_flt;
  108. sig->maj_flt += tsk->maj_flt;
  109. sig->nvcsw += tsk->nvcsw;
  110. sig->nivcsw += tsk->nivcsw;
  111. sig->inblock += task_io_get_inblock(tsk);
  112. sig->oublock += task_io_get_oublock(tsk);
  113. #ifdef CONFIG_TASK_XACCT
  114. sig->rchar += tsk->rchar;
  115. sig->wchar += tsk->wchar;
  116. sig->syscr += tsk->syscr;
  117. sig->syscw += tsk->syscw;
  118. #endif /* CONFIG_TASK_XACCT */
  119. #ifdef CONFIG_TASK_IO_ACCOUNTING
  120. sig->ioac.read_bytes += tsk->ioac.read_bytes;
  121. sig->ioac.write_bytes += tsk->ioac.write_bytes;
  122. sig->ioac.cancelled_write_bytes +=
  123. tsk->ioac.cancelled_write_bytes;
  124. #endif /* CONFIG_TASK_IO_ACCOUNTING */
  125. sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
  126. sig = NULL; /* Marker for below. */
  127. }
  128. __unhash_process(tsk);
  129. /*
  130. * Do this under ->siglock, we can race with another thread
  131. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  132. */
  133. flush_sigqueue(&tsk->pending);
  134. tsk->signal = NULL;
  135. tsk->sighand = NULL;
  136. spin_unlock(&sighand->siglock);
  137. __cleanup_sighand(sighand);
  138. clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
  139. if (sig) {
  140. flush_sigqueue(&sig->shared_pending);
  141. taskstats_tgid_free(sig);
  142. __cleanup_signal(sig);
  143. }
  144. }
  145. static void delayed_put_task_struct(struct rcu_head *rhp)
  146. {
  147. put_task_struct(container_of(rhp, struct task_struct, rcu));
  148. }
  149. void release_task(struct task_struct * p)
  150. {
  151. struct task_struct *leader;
  152. int zap_leader;
  153. repeat:
  154. tracehook_prepare_release_task(p);
  155. atomic_dec(&p->user->processes);
  156. proc_flush_task(p);
  157. write_lock_irq(&tasklist_lock);
  158. tracehook_finish_release_task(p);
  159. __exit_signal(p);
  160. /*
  161. * If we are the last non-leader member of the thread
  162. * group, and the leader is zombie, then notify the
  163. * group leader's parent process. (if it wants notification.)
  164. */
  165. zap_leader = 0;
  166. leader = p->group_leader;
  167. if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
  168. BUG_ON(task_detached(leader));
  169. do_notify_parent(leader, leader->exit_signal);
  170. /*
  171. * If we were the last child thread and the leader has
  172. * exited already, and the leader's parent ignores SIGCHLD,
  173. * then we are the one who should release the leader.
  174. *
  175. * do_notify_parent() will have marked it self-reaping in
  176. * that case.
  177. */
  178. zap_leader = task_detached(leader);
  179. /*
  180. * This maintains the invariant that release_task()
  181. * only runs on a task in EXIT_DEAD, just for sanity.
  182. */
  183. if (zap_leader)
  184. leader->exit_state = EXIT_DEAD;
  185. }
  186. write_unlock_irq(&tasklist_lock);
  187. release_thread(p);
  188. call_rcu(&p->rcu, delayed_put_task_struct);
  189. p = leader;
  190. if (unlikely(zap_leader))
  191. goto repeat;
  192. }
  193. /*
  194. * This checks not only the pgrp, but falls back on the pid if no
  195. * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
  196. * without this...
  197. *
  198. * The caller must hold rcu lock or the tasklist lock.
  199. */
  200. struct pid *session_of_pgrp(struct pid *pgrp)
  201. {
  202. struct task_struct *p;
  203. struct pid *sid = NULL;
  204. p = pid_task(pgrp, PIDTYPE_PGID);
  205. if (p == NULL)
  206. p = pid_task(pgrp, PIDTYPE_PID);
  207. if (p != NULL)
  208. sid = task_session(p);
  209. return sid;
  210. }
  211. /*
  212. * Determine if a process group is "orphaned", according to the POSIX
  213. * definition in 2.2.2.52. Orphaned process groups are not to be affected
  214. * by terminal-generated stop signals. Newly orphaned process groups are
  215. * to receive a SIGHUP and a SIGCONT.
  216. *
  217. * "I ask you, have you ever known what it is to be an orphan?"
  218. */
  219. static int will_become_orphaned_pgrp(struct pid *pgrp, struct task_struct *ignored_task)
  220. {
  221. struct task_struct *p;
  222. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  223. if ((p == ignored_task) ||
  224. (p->exit_state && thread_group_empty(p)) ||
  225. is_global_init(p->real_parent))
  226. continue;
  227. if (task_pgrp(p->real_parent) != pgrp &&
  228. task_session(p->real_parent) == task_session(p))
  229. return 0;
  230. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  231. return 1;
  232. }
  233. int is_current_pgrp_orphaned(void)
  234. {
  235. int retval;
  236. read_lock(&tasklist_lock);
  237. retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
  238. read_unlock(&tasklist_lock);
  239. return retval;
  240. }
  241. static int has_stopped_jobs(struct pid *pgrp)
  242. {
  243. int retval = 0;
  244. struct task_struct *p;
  245. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  246. if (!task_is_stopped(p))
  247. continue;
  248. retval = 1;
  249. break;
  250. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  251. return retval;
  252. }
  253. /*
  254. * Check to see if any process groups have become orphaned as
  255. * a result of our exiting, and if they have any stopped jobs,
  256. * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  257. */
  258. static void
  259. kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
  260. {
  261. struct pid *pgrp = task_pgrp(tsk);
  262. struct task_struct *ignored_task = tsk;
  263. if (!parent)
  264. /* exit: our father is in a different pgrp than
  265. * we are and we were the only connection outside.
  266. */
  267. parent = tsk->real_parent;
  268. else
  269. /* reparent: our child is in a different pgrp than
  270. * we are, and it was the only connection outside.
  271. */
  272. ignored_task = NULL;
  273. if (task_pgrp(parent) != pgrp &&
  274. task_session(parent) == task_session(tsk) &&
  275. will_become_orphaned_pgrp(pgrp, ignored_task) &&
  276. has_stopped_jobs(pgrp)) {
  277. __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
  278. __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
  279. }
  280. }
  281. /**
  282. * reparent_to_kthreadd - Reparent the calling kernel thread to kthreadd
  283. *
  284. * If a kernel thread is launched as a result of a system call, or if
  285. * it ever exits, it should generally reparent itself to kthreadd so it
  286. * isn't in the way of other processes and is correctly cleaned up on exit.
  287. *
  288. * The various task state such as scheduling policy and priority may have
  289. * been inherited from a user process, so we reset them to sane values here.
  290. *
  291. * NOTE that reparent_to_kthreadd() gives the caller full capabilities.
  292. */
  293. static void reparent_to_kthreadd(void)
  294. {
  295. write_lock_irq(&tasklist_lock);
  296. ptrace_unlink(current);
  297. /* Reparent to init */
  298. current->real_parent = current->parent = kthreadd_task;
  299. list_move_tail(&current->sibling, &current->real_parent->children);
  300. /* Set the exit signal to SIGCHLD so we signal init on exit */
  301. current->exit_signal = SIGCHLD;
  302. if (task_nice(current) < 0)
  303. set_user_nice(current, 0);
  304. /* cpus_allowed? */
  305. /* rt_priority? */
  306. /* signals? */
  307. security_task_reparent_to_init(current);
  308. memcpy(current->signal->rlim, init_task.signal->rlim,
  309. sizeof(current->signal->rlim));
  310. atomic_inc(&(INIT_USER->__count));
  311. write_unlock_irq(&tasklist_lock);
  312. switch_uid(INIT_USER);
  313. }
  314. void __set_special_pids(struct pid *pid)
  315. {
  316. struct task_struct *curr = current->group_leader;
  317. pid_t nr = pid_nr(pid);
  318. if (task_session(curr) != pid) {
  319. change_pid(curr, PIDTYPE_SID, pid);
  320. set_task_session(curr, nr);
  321. }
  322. if (task_pgrp(curr) != pid) {
  323. change_pid(curr, PIDTYPE_PGID, pid);
  324. set_task_pgrp(curr, nr);
  325. }
  326. }
  327. static void set_special_pids(struct pid *pid)
  328. {
  329. write_lock_irq(&tasklist_lock);
  330. __set_special_pids(pid);
  331. write_unlock_irq(&tasklist_lock);
  332. }
  333. /*
  334. * Let kernel threads use this to say that they
  335. * allow a certain signal (since daemonize() will
  336. * have disabled all of them by default).
  337. */
  338. int allow_signal(int sig)
  339. {
  340. if (!valid_signal(sig) || sig < 1)
  341. return -EINVAL;
  342. spin_lock_irq(&current->sighand->siglock);
  343. sigdelset(&current->blocked, sig);
  344. if (!current->mm) {
  345. /* Kernel threads handle their own signals.
  346. Let the signal code know it'll be handled, so
  347. that they don't get converted to SIGKILL or
  348. just silently dropped */
  349. current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
  350. }
  351. recalc_sigpending();
  352. spin_unlock_irq(&current->sighand->siglock);
  353. return 0;
  354. }
  355. EXPORT_SYMBOL(allow_signal);
  356. int disallow_signal(int sig)
  357. {
  358. if (!valid_signal(sig) || sig < 1)
  359. return -EINVAL;
  360. spin_lock_irq(&current->sighand->siglock);
  361. current->sighand->action[(sig)-1].sa.sa_handler = SIG_IGN;
  362. recalc_sigpending();
  363. spin_unlock_irq(&current->sighand->siglock);
  364. return 0;
  365. }
  366. EXPORT_SYMBOL(disallow_signal);
  367. /*
  368. * Put all the gunge required to become a kernel thread without
  369. * attached user resources in one place where it belongs.
  370. */
  371. void daemonize(const char *name, ...)
  372. {
  373. va_list args;
  374. struct fs_struct *fs;
  375. sigset_t blocked;
  376. va_start(args, name);
  377. vsnprintf(current->comm, sizeof(current->comm), name, args);
  378. va_end(args);
  379. /*
  380. * If we were started as result of loading a module, close all of the
  381. * user space pages. We don't need them, and if we didn't close them
  382. * they would be locked into memory.
  383. */
  384. exit_mm(current);
  385. /*
  386. * We don't want to have TIF_FREEZE set if the system-wide hibernation
  387. * or suspend transition begins right now.
  388. */
  389. current->flags |= (PF_NOFREEZE | PF_KTHREAD);
  390. if (current->nsproxy != &init_nsproxy) {
  391. get_nsproxy(&init_nsproxy);
  392. switch_task_namespaces(current, &init_nsproxy);
  393. }
  394. set_special_pids(&init_struct_pid);
  395. proc_clear_tty(current);
  396. /* Block and flush all signals */
  397. sigfillset(&blocked);
  398. sigprocmask(SIG_BLOCK, &blocked, NULL);
  399. flush_signals(current);
  400. /* Become as one with the init task */
  401. exit_fs(current); /* current->fs->count--; */
  402. fs = init_task.fs;
  403. current->fs = fs;
  404. atomic_inc(&fs->count);
  405. exit_files(current);
  406. current->files = init_task.files;
  407. atomic_inc(&current->files->count);
  408. reparent_to_kthreadd();
  409. }
  410. EXPORT_SYMBOL(daemonize);
  411. static void close_files(struct files_struct * files)
  412. {
  413. int i, j;
  414. struct fdtable *fdt;
  415. j = 0;
  416. /*
  417. * It is safe to dereference the fd table without RCU or
  418. * ->file_lock because this is the last reference to the
  419. * files structure.
  420. */
  421. fdt = files_fdtable(files);
  422. for (;;) {
  423. unsigned long set;
  424. i = j * __NFDBITS;
  425. if (i >= fdt->max_fds)
  426. break;
  427. set = fdt->open_fds->fds_bits[j++];
  428. while (set) {
  429. if (set & 1) {
  430. struct file * file = xchg(&fdt->fd[i], NULL);
  431. if (file) {
  432. filp_close(file, files);
  433. cond_resched();
  434. }
  435. }
  436. i++;
  437. set >>= 1;
  438. }
  439. }
  440. }
  441. struct files_struct *get_files_struct(struct task_struct *task)
  442. {
  443. struct files_struct *files;
  444. task_lock(task);
  445. files = task->files;
  446. if (files)
  447. atomic_inc(&files->count);
  448. task_unlock(task);
  449. return files;
  450. }
  451. void put_files_struct(struct files_struct *files)
  452. {
  453. struct fdtable *fdt;
  454. if (atomic_dec_and_test(&files->count)) {
  455. close_files(files);
  456. /*
  457. * Free the fd and fdset arrays if we expanded them.
  458. * If the fdtable was embedded, pass files for freeing
  459. * at the end of the RCU grace period. Otherwise,
  460. * you can free files immediately.
  461. */
  462. fdt = files_fdtable(files);
  463. if (fdt != &files->fdtab)
  464. kmem_cache_free(files_cachep, files);
  465. free_fdtable(fdt);
  466. }
  467. }
  468. void reset_files_struct(struct files_struct *files)
  469. {
  470. struct task_struct *tsk = current;
  471. struct files_struct *old;
  472. old = tsk->files;
  473. task_lock(tsk);
  474. tsk->files = files;
  475. task_unlock(tsk);
  476. put_files_struct(old);
  477. }
  478. void exit_files(struct task_struct *tsk)
  479. {
  480. struct files_struct * files = tsk->files;
  481. if (files) {
  482. task_lock(tsk);
  483. tsk->files = NULL;
  484. task_unlock(tsk);
  485. put_files_struct(files);
  486. }
  487. }
  488. void put_fs_struct(struct fs_struct *fs)
  489. {
  490. /* No need to hold fs->lock if we are killing it */
  491. if (atomic_dec_and_test(&fs->count)) {
  492. path_put(&fs->root);
  493. path_put(&fs->pwd);
  494. if (fs->altroot.dentry)
  495. path_put(&fs->altroot);
  496. kmem_cache_free(fs_cachep, fs);
  497. }
  498. }
  499. void exit_fs(struct task_struct *tsk)
  500. {
  501. struct fs_struct * fs = tsk->fs;
  502. if (fs) {
  503. task_lock(tsk);
  504. tsk->fs = NULL;
  505. task_unlock(tsk);
  506. put_fs_struct(fs);
  507. }
  508. }
  509. EXPORT_SYMBOL_GPL(exit_fs);
  510. #ifdef CONFIG_MM_OWNER
  511. /*
  512. * Task p is exiting and it owned mm, lets find a new owner for it
  513. */
  514. static inline int
  515. mm_need_new_owner(struct mm_struct *mm, struct task_struct *p)
  516. {
  517. /*
  518. * If there are other users of the mm and the owner (us) is exiting
  519. * we need to find a new owner to take on the responsibility.
  520. */
  521. if (!mm)
  522. return 0;
  523. if (atomic_read(&mm->mm_users) <= 1)
  524. return 0;
  525. if (mm->owner != p)
  526. return 0;
  527. return 1;
  528. }
  529. void mm_update_next_owner(struct mm_struct *mm)
  530. {
  531. struct task_struct *c, *g, *p = current;
  532. retry:
  533. if (!mm_need_new_owner(mm, p))
  534. return;
  535. read_lock(&tasklist_lock);
  536. /*
  537. * Search in the children
  538. */
  539. list_for_each_entry(c, &p->children, sibling) {
  540. if (c->mm == mm)
  541. goto assign_new_owner;
  542. }
  543. /*
  544. * Search in the siblings
  545. */
  546. list_for_each_entry(c, &p->parent->children, sibling) {
  547. if (c->mm == mm)
  548. goto assign_new_owner;
  549. }
  550. /*
  551. * Search through everything else. We should not get
  552. * here often
  553. */
  554. do_each_thread(g, c) {
  555. if (c->mm == mm)
  556. goto assign_new_owner;
  557. } while_each_thread(g, c);
  558. read_unlock(&tasklist_lock);
  559. return;
  560. assign_new_owner:
  561. BUG_ON(c == p);
  562. get_task_struct(c);
  563. /*
  564. * The task_lock protects c->mm from changing.
  565. * We always want mm->owner->mm == mm
  566. */
  567. task_lock(c);
  568. /*
  569. * Delay read_unlock() till we have the task_lock()
  570. * to ensure that c does not slip away underneath us
  571. */
  572. read_unlock(&tasklist_lock);
  573. if (c->mm != mm) {
  574. task_unlock(c);
  575. put_task_struct(c);
  576. goto retry;
  577. }
  578. cgroup_mm_owner_callbacks(mm->owner, c);
  579. mm->owner = c;
  580. task_unlock(c);
  581. put_task_struct(c);
  582. }
  583. #endif /* CONFIG_MM_OWNER */
  584. /*
  585. * Turn us into a lazy TLB process if we
  586. * aren't already..
  587. */
  588. static void exit_mm(struct task_struct * tsk)
  589. {
  590. struct mm_struct *mm = tsk->mm;
  591. struct core_state *core_state;
  592. mm_release(tsk, mm);
  593. if (!mm)
  594. return;
  595. /*
  596. * Serialize with any possible pending coredump.
  597. * We must hold mmap_sem around checking core_state
  598. * and clearing tsk->mm. The core-inducing thread
  599. * will increment ->nr_threads for each thread in the
  600. * group with ->mm != NULL.
  601. */
  602. down_read(&mm->mmap_sem);
  603. core_state = mm->core_state;
  604. if (core_state) {
  605. struct core_thread self;
  606. up_read(&mm->mmap_sem);
  607. self.task = tsk;
  608. self.next = xchg(&core_state->dumper.next, &self);
  609. /*
  610. * Implies mb(), the result of xchg() must be visible
  611. * to core_state->dumper.
  612. */
  613. if (atomic_dec_and_test(&core_state->nr_threads))
  614. complete(&core_state->startup);
  615. for (;;) {
  616. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  617. if (!self.task) /* see coredump_finish() */
  618. break;
  619. schedule();
  620. }
  621. __set_task_state(tsk, TASK_RUNNING);
  622. down_read(&mm->mmap_sem);
  623. }
  624. atomic_inc(&mm->mm_count);
  625. BUG_ON(mm != tsk->active_mm);
  626. /* more a memory barrier than a real lock */
  627. task_lock(tsk);
  628. tsk->mm = NULL;
  629. up_read(&mm->mmap_sem);
  630. enter_lazy_tlb(mm, current);
  631. /* We don't want this task to be frozen prematurely */
  632. clear_freeze_flag(tsk);
  633. task_unlock(tsk);
  634. mm_update_next_owner(mm);
  635. mmput(mm);
  636. }
  637. /*
  638. * Return nonzero if @parent's children should reap themselves.
  639. *
  640. * Called with write_lock_irq(&tasklist_lock) held.
  641. */
  642. static int ignoring_children(struct task_struct *parent)
  643. {
  644. int ret;
  645. struct sighand_struct *psig = parent->sighand;
  646. unsigned long flags;
  647. spin_lock_irqsave(&psig->siglock, flags);
  648. ret = (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
  649. (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT));
  650. spin_unlock_irqrestore(&psig->siglock, flags);
  651. return ret;
  652. }
  653. /*
  654. * Detach all tasks we were using ptrace on.
  655. * Any that need to be release_task'd are put on the @dead list.
  656. *
  657. * Called with write_lock(&tasklist_lock) held.
  658. */
  659. static void ptrace_exit(struct task_struct *parent, struct list_head *dead)
  660. {
  661. struct task_struct *p, *n;
  662. int ign = -1;
  663. list_for_each_entry_safe(p, n, &parent->ptraced, ptrace_entry) {
  664. __ptrace_unlink(p);
  665. if (p->exit_state != EXIT_ZOMBIE)
  666. continue;
  667. /*
  668. * If it's a zombie, our attachedness prevented normal
  669. * parent notification or self-reaping. Do notification
  670. * now if it would have happened earlier. If it should
  671. * reap itself, add it to the @dead list. We can't call
  672. * release_task() here because we already hold tasklist_lock.
  673. *
  674. * If it's our own child, there is no notification to do.
  675. * But if our normal children self-reap, then this child
  676. * was prevented by ptrace and we must reap it now.
  677. */
  678. if (!task_detached(p) && thread_group_empty(p)) {
  679. if (!same_thread_group(p->real_parent, parent))
  680. do_notify_parent(p, p->exit_signal);
  681. else {
  682. if (ign < 0)
  683. ign = ignoring_children(parent);
  684. if (ign)
  685. p->exit_signal = -1;
  686. }
  687. }
  688. if (task_detached(p)) {
  689. /*
  690. * Mark it as in the process of being reaped.
  691. */
  692. p->exit_state = EXIT_DEAD;
  693. list_add(&p->ptrace_entry, dead);
  694. }
  695. }
  696. }
  697. /*
  698. * Finish up exit-time ptrace cleanup.
  699. *
  700. * Called without locks.
  701. */
  702. static void ptrace_exit_finish(struct task_struct *parent,
  703. struct list_head *dead)
  704. {
  705. struct task_struct *p, *n;
  706. BUG_ON(!list_empty(&parent->ptraced));
  707. list_for_each_entry_safe(p, n, dead, ptrace_entry) {
  708. list_del_init(&p->ptrace_entry);
  709. release_task(p);
  710. }
  711. }
  712. static void reparent_thread(struct task_struct *p, struct task_struct *father)
  713. {
  714. if (p->pdeath_signal)
  715. /* We already hold the tasklist_lock here. */
  716. group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
  717. list_move_tail(&p->sibling, &p->real_parent->children);
  718. /* If this is a threaded reparent there is no need to
  719. * notify anyone anything has happened.
  720. */
  721. if (same_thread_group(p->real_parent, father))
  722. return;
  723. /* We don't want people slaying init. */
  724. if (!task_detached(p))
  725. p->exit_signal = SIGCHLD;
  726. /* If we'd notified the old parent about this child's death,
  727. * also notify the new parent.
  728. */
  729. if (!ptrace_reparented(p) &&
  730. p->exit_state == EXIT_ZOMBIE &&
  731. !task_detached(p) && thread_group_empty(p))
  732. do_notify_parent(p, p->exit_signal);
  733. kill_orphaned_pgrp(p, father);
  734. }
  735. /*
  736. * When we die, we re-parent all our children.
  737. * Try to give them to another thread in our thread
  738. * group, and if no such member exists, give it to
  739. * the child reaper process (ie "init") in our pid
  740. * space.
  741. */
  742. static void forget_original_parent(struct task_struct *father)
  743. {
  744. struct task_struct *p, *n, *reaper = father;
  745. LIST_HEAD(ptrace_dead);
  746. write_lock_irq(&tasklist_lock);
  747. /*
  748. * First clean up ptrace if we were using it.
  749. */
  750. ptrace_exit(father, &ptrace_dead);
  751. do {
  752. reaper = next_thread(reaper);
  753. if (reaper == father) {
  754. reaper = task_child_reaper(father);
  755. break;
  756. }
  757. } while (reaper->flags & PF_EXITING);
  758. list_for_each_entry_safe(p, n, &father->children, sibling) {
  759. p->real_parent = reaper;
  760. if (p->parent == father) {
  761. BUG_ON(p->ptrace);
  762. p->parent = p->real_parent;
  763. }
  764. reparent_thread(p, father);
  765. }
  766. write_unlock_irq(&tasklist_lock);
  767. BUG_ON(!list_empty(&father->children));
  768. ptrace_exit_finish(father, &ptrace_dead);
  769. }
  770. /*
  771. * Send signals to all our closest relatives so that they know
  772. * to properly mourn us..
  773. */
  774. static void exit_notify(struct task_struct *tsk, int group_dead)
  775. {
  776. int signal;
  777. void *cookie;
  778. /*
  779. * This does two things:
  780. *
  781. * A. Make init inherit all the child processes
  782. * B. Check to see if any process groups have become orphaned
  783. * as a result of our exiting, and if they have any stopped
  784. * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  785. */
  786. forget_original_parent(tsk);
  787. exit_task_namespaces(tsk);
  788. write_lock_irq(&tasklist_lock);
  789. if (group_dead)
  790. kill_orphaned_pgrp(tsk->group_leader, NULL);
  791. /* Let father know we died
  792. *
  793. * Thread signals are configurable, but you aren't going to use
  794. * that to send signals to arbitary processes.
  795. * That stops right now.
  796. *
  797. * If the parent exec id doesn't match the exec id we saved
  798. * when we started then we know the parent has changed security
  799. * domain.
  800. *
  801. * If our self_exec id doesn't match our parent_exec_id then
  802. * we have changed execution domain as these two values started
  803. * the same after a fork.
  804. */
  805. if (tsk->exit_signal != SIGCHLD && !task_detached(tsk) &&
  806. (tsk->parent_exec_id != tsk->real_parent->self_exec_id ||
  807. tsk->self_exec_id != tsk->parent_exec_id) &&
  808. !capable(CAP_KILL))
  809. tsk->exit_signal = SIGCHLD;
  810. signal = tracehook_notify_death(tsk, &cookie, group_dead);
  811. if (signal > 0)
  812. signal = do_notify_parent(tsk, signal);
  813. tsk->exit_state = signal < 0 ? EXIT_DEAD : EXIT_ZOMBIE;
  814. /* mt-exec, de_thread() is waiting for us */
  815. if (thread_group_leader(tsk) &&
  816. tsk->signal->notify_count < 0 &&
  817. tsk->signal->group_exit_task)
  818. wake_up_process(tsk->signal->group_exit_task);
  819. write_unlock_irq(&tasklist_lock);
  820. tracehook_report_death(tsk, signal, cookie, group_dead);
  821. /* If the process is dead, release it - nobody will wait for it */
  822. if (signal < 0)
  823. release_task(tsk);
  824. }
  825. #ifdef CONFIG_DEBUG_STACK_USAGE
  826. static void check_stack_usage(void)
  827. {
  828. static DEFINE_SPINLOCK(low_water_lock);
  829. static int lowest_to_date = THREAD_SIZE;
  830. unsigned long *n = end_of_stack(current);
  831. unsigned long free;
  832. while (*n == 0)
  833. n++;
  834. free = (unsigned long)n - (unsigned long)end_of_stack(current);
  835. if (free >= lowest_to_date)
  836. return;
  837. spin_lock(&low_water_lock);
  838. if (free < lowest_to_date) {
  839. printk(KERN_WARNING "%s used greatest stack depth: %lu bytes "
  840. "left\n",
  841. current->comm, free);
  842. lowest_to_date = free;
  843. }
  844. spin_unlock(&low_water_lock);
  845. }
  846. #else
  847. static inline void check_stack_usage(void) {}
  848. #endif
  849. static inline void exit_child_reaper(struct task_struct *tsk)
  850. {
  851. if (likely(tsk->group_leader != task_child_reaper(tsk)))
  852. return;
  853. if (tsk->nsproxy->pid_ns == &init_pid_ns)
  854. panic("Attempted to kill init!");
  855. /*
  856. * @tsk is the last thread in the 'cgroup-init' and is exiting.
  857. * Terminate all remaining processes in the namespace and reap them
  858. * before exiting @tsk.
  859. *
  860. * Note that @tsk (last thread of cgroup-init) may not necessarily
  861. * be the child-reaper (i.e main thread of cgroup-init) of the
  862. * namespace i.e the child_reaper may have already exited.
  863. *
  864. * Even after a child_reaper exits, we let it inherit orphaned children,
  865. * because, pid_ns->child_reaper remains valid as long as there is
  866. * at least one living sub-thread in the cgroup init.
  867. * This living sub-thread of the cgroup-init will be notified when
  868. * a child inherited by the 'child-reaper' exits (do_notify_parent()
  869. * uses __group_send_sig_info()). Further, when reaping child processes,
  870. * do_wait() iterates over children of all living sub threads.
  871. * i.e even though 'child_reaper' thread is listed as the parent of the
  872. * orphaned children, any living sub-thread in the cgroup-init can
  873. * perform the role of the child_reaper.
  874. */
  875. zap_pid_ns_processes(tsk->nsproxy->pid_ns);
  876. }
  877. NORET_TYPE void do_exit(long code)
  878. {
  879. struct task_struct *tsk = current;
  880. int group_dead;
  881. profile_task_exit(tsk);
  882. WARN_ON(atomic_read(&tsk->fs_excl));
  883. if (unlikely(in_interrupt()))
  884. panic("Aiee, killing interrupt handler!");
  885. if (unlikely(!tsk->pid))
  886. panic("Attempted to kill the idle task!");
  887. tracehook_report_exit(&code);
  888. /*
  889. * We're taking recursive faults here in do_exit. Safest is to just
  890. * leave this task alone and wait for reboot.
  891. */
  892. if (unlikely(tsk->flags & PF_EXITING)) {
  893. printk(KERN_ALERT
  894. "Fixing recursive fault but reboot is needed!\n");
  895. /*
  896. * We can do this unlocked here. The futex code uses
  897. * this flag just to verify whether the pi state
  898. * cleanup has been done or not. In the worst case it
  899. * loops once more. We pretend that the cleanup was
  900. * done as there is no way to return. Either the
  901. * OWNER_DIED bit is set by now or we push the blocked
  902. * task into the wait for ever nirwana as well.
  903. */
  904. tsk->flags |= PF_EXITPIDONE;
  905. if (tsk->io_context)
  906. exit_io_context();
  907. set_current_state(TASK_UNINTERRUPTIBLE);
  908. schedule();
  909. }
  910. exit_signals(tsk); /* sets PF_EXITING */
  911. /*
  912. * tsk->flags are checked in the futex code to protect against
  913. * an exiting task cleaning up the robust pi futexes.
  914. */
  915. smp_mb();
  916. spin_unlock_wait(&tsk->pi_lock);
  917. if (unlikely(in_atomic()))
  918. printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
  919. current->comm, task_pid_nr(current),
  920. preempt_count());
  921. acct_update_integrals(tsk);
  922. if (tsk->mm) {
  923. update_hiwater_rss(tsk->mm);
  924. update_hiwater_vm(tsk->mm);
  925. }
  926. group_dead = atomic_dec_and_test(&tsk->signal->live);
  927. if (group_dead) {
  928. exit_child_reaper(tsk);
  929. hrtimer_cancel(&tsk->signal->real_timer);
  930. exit_itimers(tsk->signal);
  931. }
  932. acct_collect(code, group_dead);
  933. #ifdef CONFIG_FUTEX
  934. if (unlikely(tsk->robust_list))
  935. exit_robust_list(tsk);
  936. #ifdef CONFIG_COMPAT
  937. if (unlikely(tsk->compat_robust_list))
  938. compat_exit_robust_list(tsk);
  939. #endif
  940. #endif
  941. if (group_dead)
  942. tty_audit_exit();
  943. if (unlikely(tsk->audit_context))
  944. audit_free(tsk);
  945. tsk->exit_code = code;
  946. taskstats_exit(tsk, group_dead);
  947. exit_mm(tsk);
  948. if (group_dead)
  949. acct_process();
  950. exit_sem(tsk);
  951. exit_files(tsk);
  952. exit_fs(tsk);
  953. check_stack_usage();
  954. exit_thread();
  955. cgroup_exit(tsk, 1);
  956. exit_keys(tsk);
  957. if (group_dead && tsk->signal->leader)
  958. disassociate_ctty(1);
  959. module_put(task_thread_info(tsk)->exec_domain->module);
  960. if (tsk->binfmt)
  961. module_put(tsk->binfmt->module);
  962. proc_exit_connector(tsk);
  963. exit_notify(tsk, group_dead);
  964. #ifdef CONFIG_NUMA
  965. mpol_put(tsk->mempolicy);
  966. tsk->mempolicy = NULL;
  967. #endif
  968. #ifdef CONFIG_FUTEX
  969. /*
  970. * This must happen late, after the PID is not
  971. * hashed anymore:
  972. */
  973. if (unlikely(!list_empty(&tsk->pi_state_list)))
  974. exit_pi_state_list(tsk);
  975. if (unlikely(current->pi_state_cache))
  976. kfree(current->pi_state_cache);
  977. #endif
  978. /*
  979. * Make sure we are holding no locks:
  980. */
  981. debug_check_no_locks_held(tsk);
  982. /*
  983. * We can do this unlocked here. The futex code uses this flag
  984. * just to verify whether the pi state cleanup has been done
  985. * or not. In the worst case it loops once more.
  986. */
  987. tsk->flags |= PF_EXITPIDONE;
  988. if (tsk->io_context)
  989. exit_io_context();
  990. if (tsk->splice_pipe)
  991. __free_pipe_info(tsk->splice_pipe);
  992. preempt_disable();
  993. /* causes final put_task_struct in finish_task_switch(). */
  994. tsk->state = TASK_DEAD;
  995. schedule();
  996. BUG();
  997. /* Avoid "noreturn function does return". */
  998. for (;;)
  999. cpu_relax(); /* For when BUG is null */
  1000. }
  1001. EXPORT_SYMBOL_GPL(do_exit);
  1002. NORET_TYPE void complete_and_exit(struct completion *comp, long code)
  1003. {
  1004. if (comp)
  1005. complete(comp);
  1006. do_exit(code);
  1007. }
  1008. EXPORT_SYMBOL(complete_and_exit);
  1009. asmlinkage long sys_exit(int error_code)
  1010. {
  1011. do_exit((error_code&0xff)<<8);
  1012. }
  1013. /*
  1014. * Take down every thread in the group. This is called by fatal signals
  1015. * as well as by sys_exit_group (below).
  1016. */
  1017. NORET_TYPE void
  1018. do_group_exit(int exit_code)
  1019. {
  1020. struct signal_struct *sig = current->signal;
  1021. BUG_ON(exit_code & 0x80); /* core dumps don't get here */
  1022. if (signal_group_exit(sig))
  1023. exit_code = sig->group_exit_code;
  1024. else if (!thread_group_empty(current)) {
  1025. struct sighand_struct *const sighand = current->sighand;
  1026. spin_lock_irq(&sighand->siglock);
  1027. if (signal_group_exit(sig))
  1028. /* Another thread got here before we took the lock. */
  1029. exit_code = sig->group_exit_code;
  1030. else {
  1031. sig->group_exit_code = exit_code;
  1032. sig->flags = SIGNAL_GROUP_EXIT;
  1033. zap_other_threads(current);
  1034. }
  1035. spin_unlock_irq(&sighand->siglock);
  1036. }
  1037. do_exit(exit_code);
  1038. /* NOTREACHED */
  1039. }
  1040. /*
  1041. * this kills every thread in the thread group. Note that any externally
  1042. * wait4()-ing process will get the correct exit code - even if this
  1043. * thread is not the thread group leader.
  1044. */
  1045. asmlinkage void sys_exit_group(int error_code)
  1046. {
  1047. do_group_exit((error_code & 0xff) << 8);
  1048. }
  1049. static struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
  1050. {
  1051. struct pid *pid = NULL;
  1052. if (type == PIDTYPE_PID)
  1053. pid = task->pids[type].pid;
  1054. else if (type < PIDTYPE_MAX)
  1055. pid = task->group_leader->pids[type].pid;
  1056. return pid;
  1057. }
  1058. static int eligible_child(enum pid_type type, struct pid *pid, int options,
  1059. struct task_struct *p)
  1060. {
  1061. int err;
  1062. if (type < PIDTYPE_MAX) {
  1063. if (task_pid_type(p, type) != pid)
  1064. return 0;
  1065. }
  1066. /* Wait for all children (clone and not) if __WALL is set;
  1067. * otherwise, wait for clone children *only* if __WCLONE is
  1068. * set; otherwise, wait for non-clone children *only*. (Note:
  1069. * A "clone" child here is one that reports to its parent
  1070. * using a signal other than SIGCHLD.) */
  1071. if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
  1072. && !(options & __WALL))
  1073. return 0;
  1074. err = security_task_wait(p);
  1075. if (err)
  1076. return err;
  1077. return 1;
  1078. }
  1079. static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
  1080. int why, int status,
  1081. struct siginfo __user *infop,
  1082. struct rusage __user *rusagep)
  1083. {
  1084. int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
  1085. put_task_struct(p);
  1086. if (!retval)
  1087. retval = put_user(SIGCHLD, &infop->si_signo);
  1088. if (!retval)
  1089. retval = put_user(0, &infop->si_errno);
  1090. if (!retval)
  1091. retval = put_user((short)why, &infop->si_code);
  1092. if (!retval)
  1093. retval = put_user(pid, &infop->si_pid);
  1094. if (!retval)
  1095. retval = put_user(uid, &infop->si_uid);
  1096. if (!retval)
  1097. retval = put_user(status, &infop->si_status);
  1098. if (!retval)
  1099. retval = pid;
  1100. return retval;
  1101. }
  1102. /*
  1103. * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
  1104. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1105. * the lock and this task is uninteresting. If we return nonzero, we have
  1106. * released the lock and the system call should return.
  1107. */
  1108. static int wait_task_zombie(struct task_struct *p, int options,
  1109. struct siginfo __user *infop,
  1110. int __user *stat_addr, struct rusage __user *ru)
  1111. {
  1112. unsigned long state;
  1113. int retval, status, traced;
  1114. pid_t pid = task_pid_vnr(p);
  1115. if (!likely(options & WEXITED))
  1116. return 0;
  1117. if (unlikely(options & WNOWAIT)) {
  1118. uid_t uid = p->uid;
  1119. int exit_code = p->exit_code;
  1120. int why, status;
  1121. get_task_struct(p);
  1122. read_unlock(&tasklist_lock);
  1123. if ((exit_code & 0x7f) == 0) {
  1124. why = CLD_EXITED;
  1125. status = exit_code >> 8;
  1126. } else {
  1127. why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
  1128. status = exit_code & 0x7f;
  1129. }
  1130. return wait_noreap_copyout(p, pid, uid, why,
  1131. status, infop, ru);
  1132. }
  1133. /*
  1134. * Try to move the task's state to DEAD
  1135. * only one thread is allowed to do this:
  1136. */
  1137. state = xchg(&p->exit_state, EXIT_DEAD);
  1138. if (state != EXIT_ZOMBIE) {
  1139. BUG_ON(state != EXIT_DEAD);
  1140. return 0;
  1141. }
  1142. traced = ptrace_reparented(p);
  1143. if (likely(!traced)) {
  1144. struct signal_struct *psig;
  1145. struct signal_struct *sig;
  1146. /*
  1147. * The resource counters for the group leader are in its
  1148. * own task_struct. Those for dead threads in the group
  1149. * are in its signal_struct, as are those for the child
  1150. * processes it has previously reaped. All these
  1151. * accumulate in the parent's signal_struct c* fields.
  1152. *
  1153. * We don't bother to take a lock here to protect these
  1154. * p->signal fields, because they are only touched by
  1155. * __exit_signal, which runs with tasklist_lock
  1156. * write-locked anyway, and so is excluded here. We do
  1157. * need to protect the access to p->parent->signal fields,
  1158. * as other threads in the parent group can be right
  1159. * here reaping other children at the same time.
  1160. */
  1161. spin_lock_irq(&p->parent->sighand->siglock);
  1162. psig = p->parent->signal;
  1163. sig = p->signal;
  1164. psig->cutime =
  1165. cputime_add(psig->cutime,
  1166. cputime_add(p->utime,
  1167. cputime_add(sig->utime,
  1168. sig->cutime)));
  1169. psig->cstime =
  1170. cputime_add(psig->cstime,
  1171. cputime_add(p->stime,
  1172. cputime_add(sig->stime,
  1173. sig->cstime)));
  1174. psig->cgtime =
  1175. cputime_add(psig->cgtime,
  1176. cputime_add(p->gtime,
  1177. cputime_add(sig->gtime,
  1178. sig->cgtime)));
  1179. psig->cmin_flt +=
  1180. p->min_flt + sig->min_flt + sig->cmin_flt;
  1181. psig->cmaj_flt +=
  1182. p->maj_flt + sig->maj_flt + sig->cmaj_flt;
  1183. psig->cnvcsw +=
  1184. p->nvcsw + sig->nvcsw + sig->cnvcsw;
  1185. psig->cnivcsw +=
  1186. p->nivcsw + sig->nivcsw + sig->cnivcsw;
  1187. psig->cinblock +=
  1188. task_io_get_inblock(p) +
  1189. sig->inblock + sig->cinblock;
  1190. psig->coublock +=
  1191. task_io_get_oublock(p) +
  1192. sig->oublock + sig->coublock;
  1193. #ifdef CONFIG_TASK_XACCT
  1194. psig->rchar += p->rchar + sig->rchar;
  1195. psig->wchar += p->wchar + sig->wchar;
  1196. psig->syscr += p->syscr + sig->syscr;
  1197. psig->syscw += p->syscw + sig->syscw;
  1198. #endif /* CONFIG_TASK_XACCT */
  1199. #ifdef CONFIG_TASK_IO_ACCOUNTING
  1200. psig->ioac.read_bytes +=
  1201. p->ioac.read_bytes + sig->ioac.read_bytes;
  1202. psig->ioac.write_bytes +=
  1203. p->ioac.write_bytes + sig->ioac.write_bytes;
  1204. psig->ioac.cancelled_write_bytes +=
  1205. p->ioac.cancelled_write_bytes +
  1206. sig->ioac.cancelled_write_bytes;
  1207. #endif /* CONFIG_TASK_IO_ACCOUNTING */
  1208. spin_unlock_irq(&p->parent->sighand->siglock);
  1209. }
  1210. /*
  1211. * Now we are sure this task is interesting, and no other
  1212. * thread can reap it because we set its state to EXIT_DEAD.
  1213. */
  1214. read_unlock(&tasklist_lock);
  1215. retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
  1216. status = (p->signal->flags & SIGNAL_GROUP_EXIT)
  1217. ? p->signal->group_exit_code : p->exit_code;
  1218. if (!retval && stat_addr)
  1219. retval = put_user(status, stat_addr);
  1220. if (!retval && infop)
  1221. retval = put_user(SIGCHLD, &infop->si_signo);
  1222. if (!retval && infop)
  1223. retval = put_user(0, &infop->si_errno);
  1224. if (!retval && infop) {
  1225. int why;
  1226. if ((status & 0x7f) == 0) {
  1227. why = CLD_EXITED;
  1228. status >>= 8;
  1229. } else {
  1230. why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
  1231. status &= 0x7f;
  1232. }
  1233. retval = put_user((short)why, &infop->si_code);
  1234. if (!retval)
  1235. retval = put_user(status, &infop->si_status);
  1236. }
  1237. if (!retval && infop)
  1238. retval = put_user(pid, &infop->si_pid);
  1239. if (!retval && infop)
  1240. retval = put_user(p->uid, &infop->si_uid);
  1241. if (!retval)
  1242. retval = pid;
  1243. if (traced) {
  1244. write_lock_irq(&tasklist_lock);
  1245. /* We dropped tasklist, ptracer could die and untrace */
  1246. ptrace_unlink(p);
  1247. /*
  1248. * If this is not a detached task, notify the parent.
  1249. * If it's still not detached after that, don't release
  1250. * it now.
  1251. */
  1252. if (!task_detached(p)) {
  1253. do_notify_parent(p, p->exit_signal);
  1254. if (!task_detached(p)) {
  1255. p->exit_state = EXIT_ZOMBIE;
  1256. p = NULL;
  1257. }
  1258. }
  1259. write_unlock_irq(&tasklist_lock);
  1260. }
  1261. if (p != NULL)
  1262. release_task(p);
  1263. return retval;
  1264. }
  1265. /*
  1266. * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
  1267. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1268. * the lock and this task is uninteresting. If we return nonzero, we have
  1269. * released the lock and the system call should return.
  1270. */
  1271. static int wait_task_stopped(int ptrace, struct task_struct *p,
  1272. int options, struct siginfo __user *infop,
  1273. int __user *stat_addr, struct rusage __user *ru)
  1274. {
  1275. int retval, exit_code, why;
  1276. uid_t uid = 0; /* unneeded, required by compiler */
  1277. pid_t pid;
  1278. if (!(options & WUNTRACED))
  1279. return 0;
  1280. exit_code = 0;
  1281. spin_lock_irq(&p->sighand->siglock);
  1282. if (unlikely(!task_is_stopped_or_traced(p)))
  1283. goto unlock_sig;
  1284. if (!ptrace && p->signal->group_stop_count > 0)
  1285. /*
  1286. * A group stop is in progress and this is the group leader.
  1287. * We won't report until all threads have stopped.
  1288. */
  1289. goto unlock_sig;
  1290. exit_code = p->exit_code;
  1291. if (!exit_code)
  1292. goto unlock_sig;
  1293. if (!unlikely(options & WNOWAIT))
  1294. p->exit_code = 0;
  1295. uid = p->uid;
  1296. unlock_sig:
  1297. spin_unlock_irq(&p->sighand->siglock);
  1298. if (!exit_code)
  1299. return 0;
  1300. /*
  1301. * Now we are pretty sure this task is interesting.
  1302. * Make sure it doesn't get reaped out from under us while we
  1303. * give up the lock and then examine it below. We don't want to
  1304. * keep holding onto the tasklist_lock while we call getrusage and
  1305. * possibly take page faults for user memory.
  1306. */
  1307. get_task_struct(p);
  1308. pid = task_pid_vnr(p);
  1309. why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
  1310. read_unlock(&tasklist_lock);
  1311. if (unlikely(options & WNOWAIT))
  1312. return wait_noreap_copyout(p, pid, uid,
  1313. why, exit_code,
  1314. infop, ru);
  1315. retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
  1316. if (!retval && stat_addr)
  1317. retval = put_user((exit_code << 8) | 0x7f, stat_addr);
  1318. if (!retval && infop)
  1319. retval = put_user(SIGCHLD, &infop->si_signo);
  1320. if (!retval && infop)
  1321. retval = put_user(0, &infop->si_errno);
  1322. if (!retval && infop)
  1323. retval = put_user((short)why, &infop->si_code);
  1324. if (!retval && infop)
  1325. retval = put_user(exit_code, &infop->si_status);
  1326. if (!retval && infop)
  1327. retval = put_user(pid, &infop->si_pid);
  1328. if (!retval && infop)
  1329. retval = put_user(uid, &infop->si_uid);
  1330. if (!retval)
  1331. retval = pid;
  1332. put_task_struct(p);
  1333. BUG_ON(!retval);
  1334. return retval;
  1335. }
  1336. /*
  1337. * Handle do_wait work for one task in a live, non-stopped state.
  1338. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1339. * the lock and this task is uninteresting. If we return nonzero, we have
  1340. * released the lock and the system call should return.
  1341. */
  1342. static int wait_task_continued(struct task_struct *p, int options,
  1343. struct siginfo __user *infop,
  1344. int __user *stat_addr, struct rusage __user *ru)
  1345. {
  1346. int retval;
  1347. pid_t pid;
  1348. uid_t uid;
  1349. if (!unlikely(options & WCONTINUED))
  1350. return 0;
  1351. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
  1352. return 0;
  1353. spin_lock_irq(&p->sighand->siglock);
  1354. /* Re-check with the lock held. */
  1355. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
  1356. spin_unlock_irq(&p->sighand->siglock);
  1357. return 0;
  1358. }
  1359. if (!unlikely(options & WNOWAIT))
  1360. p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
  1361. spin_unlock_irq(&p->sighand->siglock);
  1362. pid = task_pid_vnr(p);
  1363. uid = p->uid;
  1364. get_task_struct(p);
  1365. read_unlock(&tasklist_lock);
  1366. if (!infop) {
  1367. retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
  1368. put_task_struct(p);
  1369. if (!retval && stat_addr)
  1370. retval = put_user(0xffff, stat_addr);
  1371. if (!retval)
  1372. retval = pid;
  1373. } else {
  1374. retval = wait_noreap_copyout(p, pid, uid,
  1375. CLD_CONTINUED, SIGCONT,
  1376. infop, ru);
  1377. BUG_ON(retval == 0);
  1378. }
  1379. return retval;
  1380. }
  1381. /*
  1382. * Consider @p for a wait by @parent.
  1383. *
  1384. * -ECHILD should be in *@notask_error before the first call.
  1385. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1386. * Returns zero if the search for a child should continue;
  1387. * then *@notask_error is 0 if @p is an eligible child,
  1388. * or another error from security_task_wait(), or still -ECHILD.
  1389. */
  1390. static int wait_consider_task(struct task_struct *parent, int ptrace,
  1391. struct task_struct *p, int *notask_error,
  1392. enum pid_type type, struct pid *pid, int options,
  1393. struct siginfo __user *infop,
  1394. int __user *stat_addr, struct rusage __user *ru)
  1395. {
  1396. int ret = eligible_child(type, pid, options, p);
  1397. if (!ret)
  1398. return ret;
  1399. if (unlikely(ret < 0)) {
  1400. /*
  1401. * If we have not yet seen any eligible child,
  1402. * then let this error code replace -ECHILD.
  1403. * A permission error will give the user a clue
  1404. * to look for security policy problems, rather
  1405. * than for mysterious wait bugs.
  1406. */
  1407. if (*notask_error)
  1408. *notask_error = ret;
  1409. }
  1410. if (likely(!ptrace) && unlikely(p->ptrace)) {
  1411. /*
  1412. * This child is hidden by ptrace.
  1413. * We aren't allowed to see it now, but eventually we will.
  1414. */
  1415. *notask_error = 0;
  1416. return 0;
  1417. }
  1418. if (p->exit_state == EXIT_DEAD)
  1419. return 0;
  1420. /*
  1421. * We don't reap group leaders with subthreads.
  1422. */
  1423. if (p->exit_state == EXIT_ZOMBIE && !delay_group_leader(p))
  1424. return wait_task_zombie(p, options, infop, stat_addr, ru);
  1425. /*
  1426. * It's stopped or running now, so it might
  1427. * later continue, exit, or stop again.
  1428. */
  1429. *notask_error = 0;
  1430. if (task_is_stopped_or_traced(p))
  1431. return wait_task_stopped(ptrace, p, options,
  1432. infop, stat_addr, ru);
  1433. return wait_task_continued(p, options, infop, stat_addr, ru);
  1434. }
  1435. /*
  1436. * Do the work of do_wait() for one thread in the group, @tsk.
  1437. *
  1438. * -ECHILD should be in *@notask_error before the first call.
  1439. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1440. * Returns zero if the search for a child should continue; then
  1441. * *@notask_error is 0 if there were any eligible children,
  1442. * or another error from security_task_wait(), or still -ECHILD.
  1443. */
  1444. static int do_wait_thread(struct task_struct *tsk, int *notask_error,
  1445. enum pid_type type, struct pid *pid, int options,
  1446. struct siginfo __user *infop, int __user *stat_addr,
  1447. struct rusage __user *ru)
  1448. {
  1449. struct task_struct *p;
  1450. list_for_each_entry(p, &tsk->children, sibling) {
  1451. /*
  1452. * Do not consider detached threads.
  1453. */
  1454. if (!task_detached(p)) {
  1455. int ret = wait_consider_task(tsk, 0, p, notask_error,
  1456. type, pid, options,
  1457. infop, stat_addr, ru);
  1458. if (ret)
  1459. return ret;
  1460. }
  1461. }
  1462. return 0;
  1463. }
  1464. static int ptrace_do_wait(struct task_struct *tsk, int *notask_error,
  1465. enum pid_type type, struct pid *pid, int options,
  1466. struct siginfo __user *infop, int __user *stat_addr,
  1467. struct rusage __user *ru)
  1468. {
  1469. struct task_struct *p;
  1470. /*
  1471. * Traditionally we see ptrace'd stopped tasks regardless of options.
  1472. */
  1473. options |= WUNTRACED;
  1474. list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
  1475. int ret = wait_consider_task(tsk, 1, p, notask_error,
  1476. type, pid, options,
  1477. infop, stat_addr, ru);
  1478. if (ret)
  1479. return ret;
  1480. }
  1481. return 0;
  1482. }
  1483. static long do_wait(enum pid_type type, struct pid *pid, int options,
  1484. struct siginfo __user *infop, int __user *stat_addr,
  1485. struct rusage __user *ru)
  1486. {
  1487. DECLARE_WAITQUEUE(wait, current);
  1488. struct task_struct *tsk;
  1489. int retval;
  1490. add_wait_queue(&current->signal->wait_chldexit,&wait);
  1491. repeat:
  1492. /*
  1493. * If there is nothing that can match our critiera just get out.
  1494. * We will clear @retval to zero if we see any child that might later
  1495. * match our criteria, even if we are not able to reap it yet.
  1496. */
  1497. retval = -ECHILD;
  1498. if ((type < PIDTYPE_MAX) && (!pid || hlist_empty(&pid->tasks[type])))
  1499. goto end;
  1500. current->state = TASK_INTERRUPTIBLE;
  1501. read_lock(&tasklist_lock);
  1502. tsk = current;
  1503. do {
  1504. int tsk_result = do_wait_thread(tsk, &retval,
  1505. type, pid, options,
  1506. infop, stat_addr, ru);
  1507. if (!tsk_result)
  1508. tsk_result = ptrace_do_wait(tsk, &retval,
  1509. type, pid, options,
  1510. infop, stat_addr, ru);
  1511. if (tsk_result) {
  1512. /*
  1513. * tasklist_lock is unlocked and we have a final result.
  1514. */
  1515. retval = tsk_result;
  1516. goto end;
  1517. }
  1518. if (options & __WNOTHREAD)
  1519. break;
  1520. tsk = next_thread(tsk);
  1521. BUG_ON(tsk->signal != current->signal);
  1522. } while (tsk != current);
  1523. read_unlock(&tasklist_lock);
  1524. if (!retval && !(options & WNOHANG)) {
  1525. retval = -ERESTARTSYS;
  1526. if (!signal_pending(current)) {
  1527. schedule();
  1528. goto repeat;
  1529. }
  1530. }
  1531. end:
  1532. current->state = TASK_RUNNING;
  1533. remove_wait_queue(&current->signal->wait_chldexit,&wait);
  1534. if (infop) {
  1535. if (retval > 0)
  1536. retval = 0;
  1537. else {
  1538. /*
  1539. * For a WNOHANG return, clear out all the fields
  1540. * we would set so the user can easily tell the
  1541. * difference.
  1542. */
  1543. if (!retval)
  1544. retval = put_user(0, &infop->si_signo);
  1545. if (!retval)
  1546. retval = put_user(0, &infop->si_errno);
  1547. if (!retval)
  1548. retval = put_user(0, &infop->si_code);
  1549. if (!retval)
  1550. retval = put_user(0, &infop->si_pid);
  1551. if (!retval)
  1552. retval = put_user(0, &infop->si_uid);
  1553. if (!retval)
  1554. retval = put_user(0, &infop->si_status);
  1555. }
  1556. }
  1557. return retval;
  1558. }
  1559. asmlinkage long sys_waitid(int which, pid_t upid,
  1560. struct siginfo __user *infop, int options,
  1561. struct rusage __user *ru)
  1562. {
  1563. struct pid *pid = NULL;
  1564. enum pid_type type;
  1565. long ret;
  1566. if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
  1567. return -EINVAL;
  1568. if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
  1569. return -EINVAL;
  1570. switch (which) {
  1571. case P_ALL:
  1572. type = PIDTYPE_MAX;
  1573. break;
  1574. case P_PID:
  1575. type = PIDTYPE_PID;
  1576. if (upid <= 0)
  1577. return -EINVAL;
  1578. break;
  1579. case P_PGID:
  1580. type = PIDTYPE_PGID;
  1581. if (upid <= 0)
  1582. return -EINVAL;
  1583. break;
  1584. default:
  1585. return -EINVAL;
  1586. }
  1587. if (type < PIDTYPE_MAX)
  1588. pid = find_get_pid(upid);
  1589. ret = do_wait(type, pid, options, infop, NULL, ru);
  1590. put_pid(pid);
  1591. /* avoid REGPARM breakage on x86: */
  1592. asmlinkage_protect(5, ret, which, upid, infop, options, ru);
  1593. return ret;
  1594. }
  1595. asmlinkage long sys_wait4(pid_t upid, int __user *stat_addr,
  1596. int options, struct rusage __user *ru)
  1597. {
  1598. struct pid *pid = NULL;
  1599. enum pid_type type;
  1600. long ret;
  1601. if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
  1602. __WNOTHREAD|__WCLONE|__WALL))
  1603. return -EINVAL;
  1604. if (upid == -1)
  1605. type = PIDTYPE_MAX;
  1606. else if (upid < 0) {
  1607. type = PIDTYPE_PGID;
  1608. pid = find_get_pid(-upid);
  1609. } else if (upid == 0) {
  1610. type = PIDTYPE_PGID;
  1611. pid = get_pid(task_pgrp(current));
  1612. } else /* upid > 0 */ {
  1613. type = PIDTYPE_PID;
  1614. pid = find_get_pid(upid);
  1615. }
  1616. ret = do_wait(type, pid, options | WEXITED, NULL, stat_addr, ru);
  1617. put_pid(pid);
  1618. /* avoid REGPARM breakage on x86: */
  1619. asmlinkage_protect(4, ret, upid, stat_addr, options, ru);
  1620. return ret;
  1621. }
  1622. #ifdef __ARCH_WANT_SYS_WAITPID
  1623. /*
  1624. * sys_waitpid() remains for compatibility. waitpid() should be
  1625. * implemented by calling sys_wait4() from libc.a.
  1626. */
  1627. asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
  1628. {
  1629. return sys_wait4(pid, stat_addr, options, NULL);
  1630. }
  1631. #endif