exit.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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/iocontext.h>
  15. #include <linux/key.h>
  16. #include <linux/security.h>
  17. #include <linux/cpu.h>
  18. #include <linux/acct.h>
  19. #include <linux/tsacct_kern.h>
  20. #include <linux/file.h>
  21. #include <linux/fdtable.h>
  22. #include <linux/freezer.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/cgroup.h>
  35. #include <linux/syscalls.h>
  36. #include <linux/signal.h>
  37. #include <linux/posix-timers.h>
  38. #include <linux/cn_proc.h>
  39. #include <linux/mutex.h>
  40. #include <linux/futex.h>
  41. #include <linux/pipe_fs_i.h>
  42. #include <linux/audit.h> /* for audit_free() */
  43. #include <linux/resource.h>
  44. #include <linux/blkdev.h>
  45. #include <linux/task_io_accounting_ops.h>
  46. #include <linux/tracehook.h>
  47. #include <linux/fs_struct.h>
  48. #include <linux/init_task.h>
  49. #include <linux/perf_event.h>
  50. #include <trace/events/sched.h>
  51. #include <linux/hw_breakpoint.h>
  52. #include <linux/oom.h>
  53. #include <linux/writeback.h>
  54. #include <linux/shm.h>
  55. #include <asm/uaccess.h>
  56. #include <asm/unistd.h>
  57. #include <asm/pgtable.h>
  58. #include <asm/mmu_context.h>
  59. static void exit_mm(struct task_struct * tsk);
  60. static void __unhash_process(struct task_struct *p, bool group_dead)
  61. {
  62. nr_threads--;
  63. detach_pid(p, PIDTYPE_PID);
  64. if (group_dead) {
  65. detach_pid(p, PIDTYPE_PGID);
  66. detach_pid(p, PIDTYPE_SID);
  67. list_del_rcu(&p->tasks);
  68. list_del_init(&p->sibling);
  69. __this_cpu_dec(process_counts);
  70. }
  71. list_del_rcu(&p->thread_group);
  72. }
  73. /*
  74. * This function expects the tasklist_lock write-locked.
  75. */
  76. static void __exit_signal(struct task_struct *tsk)
  77. {
  78. struct signal_struct *sig = tsk->signal;
  79. bool group_dead = thread_group_leader(tsk);
  80. struct sighand_struct *sighand;
  81. struct tty_struct *uninitialized_var(tty);
  82. cputime_t utime, stime;
  83. sighand = rcu_dereference_check(tsk->sighand,
  84. lockdep_tasklist_lock_is_held());
  85. spin_lock(&sighand->siglock);
  86. posix_cpu_timers_exit(tsk);
  87. if (group_dead) {
  88. posix_cpu_timers_exit_group(tsk);
  89. tty = sig->tty;
  90. sig->tty = NULL;
  91. } else {
  92. /*
  93. * This can only happen if the caller is de_thread().
  94. * FIXME: this is the temporary hack, we should teach
  95. * posix-cpu-timers to handle this case correctly.
  96. */
  97. if (unlikely(has_group_leader_pid(tsk)))
  98. posix_cpu_timers_exit_group(tsk);
  99. /*
  100. * If there is any task waiting for the group exit
  101. * then notify it:
  102. */
  103. if (sig->notify_count > 0 && !--sig->notify_count)
  104. wake_up_process(sig->group_exit_task);
  105. if (tsk == sig->curr_target)
  106. sig->curr_target = next_thread(tsk);
  107. /*
  108. * Accumulate here the counters for all threads but the
  109. * group leader as they die, so they can be added into
  110. * the process-wide totals when those are taken.
  111. * The group leader stays around as a zombie as long
  112. * as there are other threads. When it gets reaped,
  113. * the exit.c code will add its counts into these totals.
  114. * We won't ever get here for the group leader, since it
  115. * will have been the last reference on the signal_struct.
  116. */
  117. task_cputime(tsk, &utime, &stime);
  118. sig->utime += utime;
  119. sig->stime += stime;
  120. sig->gtime += task_gtime(tsk);
  121. sig->min_flt += tsk->min_flt;
  122. sig->maj_flt += tsk->maj_flt;
  123. sig->nvcsw += tsk->nvcsw;
  124. sig->nivcsw += tsk->nivcsw;
  125. sig->inblock += task_io_get_inblock(tsk);
  126. sig->oublock += task_io_get_oublock(tsk);
  127. task_io_accounting_add(&sig->ioac, &tsk->ioac);
  128. sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
  129. }
  130. sig->nr_threads--;
  131. __unhash_process(tsk, group_dead);
  132. /*
  133. * Do this under ->siglock, we can race with another thread
  134. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  135. */
  136. flush_sigqueue(&tsk->pending);
  137. tsk->sighand = NULL;
  138. spin_unlock(&sighand->siglock);
  139. __cleanup_sighand(sighand);
  140. clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
  141. if (group_dead) {
  142. flush_sigqueue(&sig->shared_pending);
  143. tty_kref_put(tty);
  144. }
  145. }
  146. static void delayed_put_task_struct(struct rcu_head *rhp)
  147. {
  148. struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
  149. perf_event_delayed_put(tsk);
  150. trace_sched_process_free(tsk);
  151. put_task_struct(tsk);
  152. }
  153. void release_task(struct task_struct * p)
  154. {
  155. struct task_struct *leader;
  156. int zap_leader;
  157. repeat:
  158. /* don't need to get the RCU readlock here - the process is dead and
  159. * can't be modifying its own credentials. But shut RCU-lockdep up */
  160. rcu_read_lock();
  161. atomic_dec(&__task_cred(p)->user->processes);
  162. rcu_read_unlock();
  163. proc_flush_task(p);
  164. write_lock_irq(&tasklist_lock);
  165. ptrace_release_task(p);
  166. __exit_signal(p);
  167. /*
  168. * If we are the last non-leader member of the thread
  169. * group, and the leader is zombie, then notify the
  170. * group leader's parent process. (if it wants notification.)
  171. */
  172. zap_leader = 0;
  173. leader = p->group_leader;
  174. if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
  175. /*
  176. * If we were the last child thread and the leader has
  177. * exited already, and the leader's parent ignores SIGCHLD,
  178. * then we are the one who should release the leader.
  179. */
  180. zap_leader = do_notify_parent(leader, leader->exit_signal);
  181. if (zap_leader)
  182. leader->exit_state = EXIT_DEAD;
  183. }
  184. write_unlock_irq(&tasklist_lock);
  185. release_thread(p);
  186. call_rcu(&p->rcu, delayed_put_task_struct);
  187. p = leader;
  188. if (unlikely(zap_leader))
  189. goto repeat;
  190. }
  191. /*
  192. * This checks not only the pgrp, but falls back on the pid if no
  193. * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
  194. * without this...
  195. *
  196. * The caller must hold rcu lock or the tasklist lock.
  197. */
  198. struct pid *session_of_pgrp(struct pid *pgrp)
  199. {
  200. struct task_struct *p;
  201. struct pid *sid = NULL;
  202. p = pid_task(pgrp, PIDTYPE_PGID);
  203. if (p == NULL)
  204. p = pid_task(pgrp, PIDTYPE_PID);
  205. if (p != NULL)
  206. sid = task_session(p);
  207. return sid;
  208. }
  209. /*
  210. * Determine if a process group is "orphaned", according to the POSIX
  211. * definition in 2.2.2.52. Orphaned process groups are not to be affected
  212. * by terminal-generated stop signals. Newly orphaned process groups are
  213. * to receive a SIGHUP and a SIGCONT.
  214. *
  215. * "I ask you, have you ever known what it is to be an orphan?"
  216. */
  217. static int will_become_orphaned_pgrp(struct pid *pgrp, struct task_struct *ignored_task)
  218. {
  219. struct task_struct *p;
  220. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  221. if ((p == ignored_task) ||
  222. (p->exit_state && thread_group_empty(p)) ||
  223. is_global_init(p->real_parent))
  224. continue;
  225. if (task_pgrp(p->real_parent) != pgrp &&
  226. task_session(p->real_parent) == task_session(p))
  227. return 0;
  228. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  229. return 1;
  230. }
  231. int is_current_pgrp_orphaned(void)
  232. {
  233. int retval;
  234. read_lock(&tasklist_lock);
  235. retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
  236. read_unlock(&tasklist_lock);
  237. return retval;
  238. }
  239. static bool has_stopped_jobs(struct pid *pgrp)
  240. {
  241. struct task_struct *p;
  242. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  243. if (p->signal->flags & SIGNAL_STOP_STOPPED)
  244. return true;
  245. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  246. return false;
  247. }
  248. /*
  249. * Check to see if any process groups have become orphaned as
  250. * a result of our exiting, and if they have any stopped jobs,
  251. * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  252. */
  253. static void
  254. kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
  255. {
  256. struct pid *pgrp = task_pgrp(tsk);
  257. struct task_struct *ignored_task = tsk;
  258. if (!parent)
  259. /* exit: our father is in a different pgrp than
  260. * we are and we were the only connection outside.
  261. */
  262. parent = tsk->real_parent;
  263. else
  264. /* reparent: our child is in a different pgrp than
  265. * we are, and it was the only connection outside.
  266. */
  267. ignored_task = NULL;
  268. if (task_pgrp(parent) != pgrp &&
  269. task_session(parent) == task_session(tsk) &&
  270. will_become_orphaned_pgrp(pgrp, ignored_task) &&
  271. has_stopped_jobs(pgrp)) {
  272. __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
  273. __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
  274. }
  275. }
  276. /*
  277. * Let kernel threads use this to say that they allow a certain signal.
  278. * Must not be used if kthread was cloned with CLONE_SIGHAND.
  279. */
  280. int allow_signal(int sig)
  281. {
  282. if (!valid_signal(sig) || sig < 1)
  283. return -EINVAL;
  284. spin_lock_irq(&current->sighand->siglock);
  285. /* This is only needed for daemonize()'ed kthreads */
  286. sigdelset(&current->blocked, sig);
  287. /*
  288. * Kernel threads handle their own signals. Let the signal code
  289. * know it'll be handled, so that they don't get converted to
  290. * SIGKILL or just silently dropped.
  291. */
  292. current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
  293. recalc_sigpending();
  294. spin_unlock_irq(&current->sighand->siglock);
  295. return 0;
  296. }
  297. EXPORT_SYMBOL(allow_signal);
  298. int disallow_signal(int sig)
  299. {
  300. if (!valid_signal(sig) || sig < 1)
  301. return -EINVAL;
  302. spin_lock_irq(&current->sighand->siglock);
  303. current->sighand->action[(sig)-1].sa.sa_handler = SIG_IGN;
  304. recalc_sigpending();
  305. spin_unlock_irq(&current->sighand->siglock);
  306. return 0;
  307. }
  308. EXPORT_SYMBOL(disallow_signal);
  309. #ifdef CONFIG_MM_OWNER
  310. /*
  311. * A task is exiting. If it owned this mm, find a new owner for the mm.
  312. */
  313. void mm_update_next_owner(struct mm_struct *mm)
  314. {
  315. struct task_struct *c, *g, *p = current;
  316. retry:
  317. /*
  318. * If the exiting or execing task is not the owner, it's
  319. * someone else's problem.
  320. */
  321. if (mm->owner != p)
  322. return;
  323. /*
  324. * The current owner is exiting/execing and there are no other
  325. * candidates. Do not leave the mm pointing to a possibly
  326. * freed task structure.
  327. */
  328. if (atomic_read(&mm->mm_users) <= 1) {
  329. mm->owner = NULL;
  330. return;
  331. }
  332. read_lock(&tasklist_lock);
  333. /*
  334. * Search in the children
  335. */
  336. list_for_each_entry(c, &p->children, sibling) {
  337. if (c->mm == mm)
  338. goto assign_new_owner;
  339. }
  340. /*
  341. * Search in the siblings
  342. */
  343. list_for_each_entry(c, &p->real_parent->children, sibling) {
  344. if (c->mm == mm)
  345. goto assign_new_owner;
  346. }
  347. /*
  348. * Search through everything else. We should not get
  349. * here often
  350. */
  351. do_each_thread(g, c) {
  352. if (c->mm == mm)
  353. goto assign_new_owner;
  354. } while_each_thread(g, c);
  355. read_unlock(&tasklist_lock);
  356. /*
  357. * We found no owner yet mm_users > 1: this implies that we are
  358. * most likely racing with swapoff (try_to_unuse()) or /proc or
  359. * ptrace or page migration (get_task_mm()). Mark owner as NULL.
  360. */
  361. mm->owner = NULL;
  362. return;
  363. assign_new_owner:
  364. BUG_ON(c == p);
  365. get_task_struct(c);
  366. /*
  367. * The task_lock protects c->mm from changing.
  368. * We always want mm->owner->mm == mm
  369. */
  370. task_lock(c);
  371. /*
  372. * Delay read_unlock() till we have the task_lock()
  373. * to ensure that c does not slip away underneath us
  374. */
  375. read_unlock(&tasklist_lock);
  376. if (c->mm != mm) {
  377. task_unlock(c);
  378. put_task_struct(c);
  379. goto retry;
  380. }
  381. mm->owner = c;
  382. task_unlock(c);
  383. put_task_struct(c);
  384. }
  385. #endif /* CONFIG_MM_OWNER */
  386. /*
  387. * Turn us into a lazy TLB process if we
  388. * aren't already..
  389. */
  390. static void exit_mm(struct task_struct * tsk)
  391. {
  392. struct mm_struct *mm = tsk->mm;
  393. struct core_state *core_state;
  394. mm_release(tsk, mm);
  395. if (!mm)
  396. return;
  397. sync_mm_rss(mm);
  398. /*
  399. * Serialize with any possible pending coredump.
  400. * We must hold mmap_sem around checking core_state
  401. * and clearing tsk->mm. The core-inducing thread
  402. * will increment ->nr_threads for each thread in the
  403. * group with ->mm != NULL.
  404. */
  405. down_read(&mm->mmap_sem);
  406. core_state = mm->core_state;
  407. if (core_state) {
  408. struct core_thread self;
  409. up_read(&mm->mmap_sem);
  410. self.task = tsk;
  411. self.next = xchg(&core_state->dumper.next, &self);
  412. /*
  413. * Implies mb(), the result of xchg() must be visible
  414. * to core_state->dumper.
  415. */
  416. if (atomic_dec_and_test(&core_state->nr_threads))
  417. complete(&core_state->startup);
  418. for (;;) {
  419. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  420. if (!self.task) /* see coredump_finish() */
  421. break;
  422. freezable_schedule();
  423. }
  424. __set_task_state(tsk, TASK_RUNNING);
  425. down_read(&mm->mmap_sem);
  426. }
  427. atomic_inc(&mm->mm_count);
  428. BUG_ON(mm != tsk->active_mm);
  429. /* more a memory barrier than a real lock */
  430. task_lock(tsk);
  431. tsk->mm = NULL;
  432. up_read(&mm->mmap_sem);
  433. enter_lazy_tlb(mm, current);
  434. task_unlock(tsk);
  435. mm_update_next_owner(mm);
  436. mmput(mm);
  437. }
  438. /*
  439. * When we die, we re-parent all our children, and try to:
  440. * 1. give them to another thread in our thread group, if such a member exists
  441. * 2. give it to the first ancestor process which prctl'd itself as a
  442. * child_subreaper for its children (like a service manager)
  443. * 3. give it to the init process (PID 1) in our pid namespace
  444. */
  445. static struct task_struct *find_new_reaper(struct task_struct *father)
  446. __releases(&tasklist_lock)
  447. __acquires(&tasklist_lock)
  448. {
  449. struct pid_namespace *pid_ns = task_active_pid_ns(father);
  450. struct task_struct *thread;
  451. thread = father;
  452. while_each_thread(father, thread) {
  453. if (thread->flags & PF_EXITING)
  454. continue;
  455. if (unlikely(pid_ns->child_reaper == father))
  456. pid_ns->child_reaper = thread;
  457. return thread;
  458. }
  459. if (unlikely(pid_ns->child_reaper == father)) {
  460. write_unlock_irq(&tasklist_lock);
  461. if (unlikely(pid_ns == &init_pid_ns)) {
  462. panic("Attempted to kill init! exitcode=0x%08x\n",
  463. father->signal->group_exit_code ?:
  464. father->exit_code);
  465. }
  466. zap_pid_ns_processes(pid_ns);
  467. write_lock_irq(&tasklist_lock);
  468. } else if (father->signal->has_child_subreaper) {
  469. struct task_struct *reaper;
  470. /*
  471. * Find the first ancestor marked as child_subreaper.
  472. * Note that the code below checks same_thread_group(reaper,
  473. * pid_ns->child_reaper). This is what we need to DTRT in a
  474. * PID namespace. However we still need the check above, see
  475. * http://marc.info/?l=linux-kernel&m=131385460420380
  476. */
  477. for (reaper = father->real_parent;
  478. reaper != &init_task;
  479. reaper = reaper->real_parent) {
  480. if (same_thread_group(reaper, pid_ns->child_reaper))
  481. break;
  482. if (!reaper->signal->is_child_subreaper)
  483. continue;
  484. thread = reaper;
  485. do {
  486. if (!(thread->flags & PF_EXITING))
  487. return reaper;
  488. } while_each_thread(reaper, thread);
  489. }
  490. }
  491. return pid_ns->child_reaper;
  492. }
  493. /*
  494. * Any that need to be release_task'd are put on the @dead list.
  495. */
  496. static void reparent_leader(struct task_struct *father, struct task_struct *p,
  497. struct list_head *dead)
  498. {
  499. list_move_tail(&p->sibling, &p->real_parent->children);
  500. if (p->exit_state == EXIT_DEAD)
  501. return;
  502. /*
  503. * If this is a threaded reparent there is no need to
  504. * notify anyone anything has happened.
  505. */
  506. if (same_thread_group(p->real_parent, father))
  507. return;
  508. /* We don't want people slaying init. */
  509. p->exit_signal = SIGCHLD;
  510. /* If it has exited notify the new parent about this child's death. */
  511. if (!p->ptrace &&
  512. p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
  513. if (do_notify_parent(p, p->exit_signal)) {
  514. p->exit_state = EXIT_DEAD;
  515. list_move_tail(&p->sibling, dead);
  516. }
  517. }
  518. kill_orphaned_pgrp(p, father);
  519. }
  520. static void forget_original_parent(struct task_struct *father)
  521. {
  522. struct task_struct *p, *n, *reaper;
  523. LIST_HEAD(dead_children);
  524. write_lock_irq(&tasklist_lock);
  525. /*
  526. * Note that exit_ptrace() and find_new_reaper() might
  527. * drop tasklist_lock and reacquire it.
  528. */
  529. exit_ptrace(father);
  530. reaper = find_new_reaper(father);
  531. list_for_each_entry_safe(p, n, &father->children, sibling) {
  532. struct task_struct *t = p;
  533. do {
  534. t->real_parent = reaper;
  535. if (t->parent == father) {
  536. BUG_ON(t->ptrace);
  537. t->parent = t->real_parent;
  538. }
  539. if (t->pdeath_signal)
  540. group_send_sig_info(t->pdeath_signal,
  541. SEND_SIG_NOINFO, t);
  542. } while_each_thread(p, t);
  543. reparent_leader(father, p, &dead_children);
  544. }
  545. write_unlock_irq(&tasklist_lock);
  546. BUG_ON(!list_empty(&father->children));
  547. list_for_each_entry_safe(p, n, &dead_children, sibling) {
  548. list_del_init(&p->sibling);
  549. release_task(p);
  550. }
  551. }
  552. /*
  553. * Send signals to all our closest relatives so that they know
  554. * to properly mourn us..
  555. */
  556. static void exit_notify(struct task_struct *tsk, int group_dead)
  557. {
  558. bool autoreap;
  559. /*
  560. * This does two things:
  561. *
  562. * A. Make init inherit all the child processes
  563. * B. Check to see if any process groups have become orphaned
  564. * as a result of our exiting, and if they have any stopped
  565. * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  566. */
  567. forget_original_parent(tsk);
  568. write_lock_irq(&tasklist_lock);
  569. if (group_dead)
  570. kill_orphaned_pgrp(tsk->group_leader, NULL);
  571. if (unlikely(tsk->ptrace)) {
  572. int sig = thread_group_leader(tsk) &&
  573. thread_group_empty(tsk) &&
  574. !ptrace_reparented(tsk) ?
  575. tsk->exit_signal : SIGCHLD;
  576. autoreap = do_notify_parent(tsk, sig);
  577. } else if (thread_group_leader(tsk)) {
  578. autoreap = thread_group_empty(tsk) &&
  579. do_notify_parent(tsk, tsk->exit_signal);
  580. } else {
  581. autoreap = true;
  582. }
  583. tsk->exit_state = autoreap ? EXIT_DEAD : EXIT_ZOMBIE;
  584. /* mt-exec, de_thread() is waiting for group leader */
  585. if (unlikely(tsk->signal->notify_count < 0))
  586. wake_up_process(tsk->signal->group_exit_task);
  587. write_unlock_irq(&tasklist_lock);
  588. /* If the process is dead, release it - nobody will wait for it */
  589. if (autoreap)
  590. release_task(tsk);
  591. }
  592. #ifdef CONFIG_DEBUG_STACK_USAGE
  593. static void check_stack_usage(void)
  594. {
  595. static DEFINE_SPINLOCK(low_water_lock);
  596. static int lowest_to_date = THREAD_SIZE;
  597. unsigned long free;
  598. free = stack_not_used(current);
  599. if (free >= lowest_to_date)
  600. return;
  601. spin_lock(&low_water_lock);
  602. if (free < lowest_to_date) {
  603. printk(KERN_WARNING "%s (%d) used greatest stack depth: "
  604. "%lu bytes left\n",
  605. current->comm, task_pid_nr(current), free);
  606. lowest_to_date = free;
  607. }
  608. spin_unlock(&low_water_lock);
  609. }
  610. #else
  611. static inline void check_stack_usage(void) {}
  612. #endif
  613. void do_exit(long code)
  614. {
  615. struct task_struct *tsk = current;
  616. int group_dead;
  617. profile_task_exit(tsk);
  618. WARN_ON(blk_needs_flush_plug(tsk));
  619. if (unlikely(in_interrupt()))
  620. panic("Aiee, killing interrupt handler!");
  621. if (unlikely(!tsk->pid))
  622. panic("Attempted to kill the idle task!");
  623. /*
  624. * If do_exit is called because this processes oopsed, it's possible
  625. * that get_fs() was left as KERNEL_DS, so reset it to USER_DS before
  626. * continuing. Amongst other possible reasons, this is to prevent
  627. * mm_release()->clear_child_tid() from writing to a user-controlled
  628. * kernel address.
  629. */
  630. set_fs(USER_DS);
  631. ptrace_event(PTRACE_EVENT_EXIT, code);
  632. validate_creds_for_do_exit(tsk);
  633. /*
  634. * We're taking recursive faults here in do_exit. Safest is to just
  635. * leave this task alone and wait for reboot.
  636. */
  637. if (unlikely(tsk->flags & PF_EXITING)) {
  638. printk(KERN_ALERT
  639. "Fixing recursive fault but reboot is needed!\n");
  640. /*
  641. * We can do this unlocked here. The futex code uses
  642. * this flag just to verify whether the pi state
  643. * cleanup has been done or not. In the worst case it
  644. * loops once more. We pretend that the cleanup was
  645. * done as there is no way to return. Either the
  646. * OWNER_DIED bit is set by now or we push the blocked
  647. * task into the wait for ever nirwana as well.
  648. */
  649. tsk->flags |= PF_EXITPIDONE;
  650. set_current_state(TASK_UNINTERRUPTIBLE);
  651. schedule();
  652. }
  653. exit_signals(tsk); /* sets PF_EXITING */
  654. /*
  655. * tsk->flags are checked in the futex code to protect against
  656. * an exiting task cleaning up the robust pi futexes.
  657. */
  658. smp_mb();
  659. raw_spin_unlock_wait(&tsk->pi_lock);
  660. if (unlikely(in_atomic()))
  661. printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
  662. current->comm, task_pid_nr(current),
  663. preempt_count());
  664. acct_update_integrals(tsk);
  665. /* sync mm's RSS info before statistics gathering */
  666. if (tsk->mm)
  667. sync_mm_rss(tsk->mm);
  668. group_dead = atomic_dec_and_test(&tsk->signal->live);
  669. if (group_dead) {
  670. hrtimer_cancel(&tsk->signal->real_timer);
  671. exit_itimers(tsk->signal);
  672. if (tsk->mm)
  673. setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
  674. }
  675. acct_collect(code, group_dead);
  676. if (group_dead)
  677. tty_audit_exit();
  678. audit_free(tsk);
  679. tsk->exit_code = code;
  680. taskstats_exit(tsk, group_dead);
  681. exit_mm(tsk);
  682. if (group_dead)
  683. acct_process();
  684. trace_sched_process_exit(tsk);
  685. exit_sem(tsk);
  686. exit_shm(tsk);
  687. exit_files(tsk);
  688. exit_fs(tsk);
  689. exit_task_namespaces(tsk);
  690. exit_task_work(tsk);
  691. check_stack_usage();
  692. exit_thread();
  693. /*
  694. * Flush inherited counters to the parent - before the parent
  695. * gets woken up by child-exit notifications.
  696. *
  697. * because of cgroup mode, must be called before cgroup_exit()
  698. */
  699. perf_event_exit_task(tsk);
  700. cgroup_exit(tsk, 1);
  701. if (group_dead)
  702. disassociate_ctty(1);
  703. module_put(task_thread_info(tsk)->exec_domain->module);
  704. proc_exit_connector(tsk);
  705. /*
  706. * FIXME: do that only when needed, using sched_exit tracepoint
  707. */
  708. flush_ptrace_hw_breakpoint(tsk);
  709. exit_notify(tsk, group_dead);
  710. #ifdef CONFIG_NUMA
  711. task_lock(tsk);
  712. mpol_put(tsk->mempolicy);
  713. tsk->mempolicy = NULL;
  714. task_unlock(tsk);
  715. #endif
  716. #ifdef CONFIG_FUTEX
  717. if (unlikely(current->pi_state_cache))
  718. kfree(current->pi_state_cache);
  719. #endif
  720. /*
  721. * Make sure we are holding no locks:
  722. */
  723. debug_check_no_locks_held();
  724. /*
  725. * We can do this unlocked here. The futex code uses this flag
  726. * just to verify whether the pi state cleanup has been done
  727. * or not. In the worst case it loops once more.
  728. */
  729. tsk->flags |= PF_EXITPIDONE;
  730. if (tsk->io_context)
  731. exit_io_context(tsk);
  732. if (tsk->splice_pipe)
  733. free_pipe_info(tsk->splice_pipe);
  734. if (tsk->task_frag.page)
  735. put_page(tsk->task_frag.page);
  736. validate_creds_for_do_exit(tsk);
  737. preempt_disable();
  738. if (tsk->nr_dirtied)
  739. __this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
  740. exit_rcu();
  741. /*
  742. * The setting of TASK_RUNNING by try_to_wake_up() may be delayed
  743. * when the following two conditions become true.
  744. * - There is race condition of mmap_sem (It is acquired by
  745. * exit_mm()), and
  746. * - SMI occurs before setting TASK_RUNINNG.
  747. * (or hypervisor of virtual machine switches to other guest)
  748. * As a result, we may become TASK_RUNNING after becoming TASK_DEAD
  749. *
  750. * To avoid it, we have to wait for releasing tsk->pi_lock which
  751. * is held by try_to_wake_up()
  752. */
  753. smp_mb();
  754. raw_spin_unlock_wait(&tsk->pi_lock);
  755. /* causes final put_task_struct in finish_task_switch(). */
  756. tsk->state = TASK_DEAD;
  757. tsk->flags |= PF_NOFREEZE; /* tell freezer to ignore us */
  758. schedule();
  759. BUG();
  760. /* Avoid "noreturn function does return". */
  761. for (;;)
  762. cpu_relax(); /* For when BUG is null */
  763. }
  764. EXPORT_SYMBOL_GPL(do_exit);
  765. void complete_and_exit(struct completion *comp, long code)
  766. {
  767. if (comp)
  768. complete(comp);
  769. do_exit(code);
  770. }
  771. EXPORT_SYMBOL(complete_and_exit);
  772. SYSCALL_DEFINE1(exit, int, error_code)
  773. {
  774. do_exit((error_code&0xff)<<8);
  775. }
  776. /*
  777. * Take down every thread in the group. This is called by fatal signals
  778. * as well as by sys_exit_group (below).
  779. */
  780. void
  781. do_group_exit(int exit_code)
  782. {
  783. struct signal_struct *sig = current->signal;
  784. BUG_ON(exit_code & 0x80); /* core dumps don't get here */
  785. if (signal_group_exit(sig))
  786. exit_code = sig->group_exit_code;
  787. else if (!thread_group_empty(current)) {
  788. struct sighand_struct *const sighand = current->sighand;
  789. spin_lock_irq(&sighand->siglock);
  790. if (signal_group_exit(sig))
  791. /* Another thread got here before we took the lock. */
  792. exit_code = sig->group_exit_code;
  793. else {
  794. sig->group_exit_code = exit_code;
  795. sig->flags = SIGNAL_GROUP_EXIT;
  796. zap_other_threads(current);
  797. }
  798. spin_unlock_irq(&sighand->siglock);
  799. }
  800. do_exit(exit_code);
  801. /* NOTREACHED */
  802. }
  803. /*
  804. * this kills every thread in the thread group. Note that any externally
  805. * wait4()-ing process will get the correct exit code - even if this
  806. * thread is not the thread group leader.
  807. */
  808. SYSCALL_DEFINE1(exit_group, int, error_code)
  809. {
  810. do_group_exit((error_code & 0xff) << 8);
  811. /* NOTREACHED */
  812. return 0;
  813. }
  814. struct wait_opts {
  815. enum pid_type wo_type;
  816. int wo_flags;
  817. struct pid *wo_pid;
  818. struct siginfo __user *wo_info;
  819. int __user *wo_stat;
  820. struct rusage __user *wo_rusage;
  821. wait_queue_t child_wait;
  822. int notask_error;
  823. };
  824. static inline
  825. struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
  826. {
  827. if (type != PIDTYPE_PID)
  828. task = task->group_leader;
  829. return task->pids[type].pid;
  830. }
  831. static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
  832. {
  833. return wo->wo_type == PIDTYPE_MAX ||
  834. task_pid_type(p, wo->wo_type) == wo->wo_pid;
  835. }
  836. static int eligible_child(struct wait_opts *wo, struct task_struct *p)
  837. {
  838. if (!eligible_pid(wo, p))
  839. return 0;
  840. /* Wait for all children (clone and not) if __WALL is set;
  841. * otherwise, wait for clone children *only* if __WCLONE is
  842. * set; otherwise, wait for non-clone children *only*. (Note:
  843. * A "clone" child here is one that reports to its parent
  844. * using a signal other than SIGCHLD.) */
  845. if (((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
  846. && !(wo->wo_flags & __WALL))
  847. return 0;
  848. return 1;
  849. }
  850. static int wait_noreap_copyout(struct wait_opts *wo, struct task_struct *p,
  851. pid_t pid, uid_t uid, int why, int status)
  852. {
  853. struct siginfo __user *infop;
  854. int retval = wo->wo_rusage
  855. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  856. put_task_struct(p);
  857. infop = wo->wo_info;
  858. if (infop) {
  859. if (!retval)
  860. retval = put_user(SIGCHLD, &infop->si_signo);
  861. if (!retval)
  862. retval = put_user(0, &infop->si_errno);
  863. if (!retval)
  864. retval = put_user((short)why, &infop->si_code);
  865. if (!retval)
  866. retval = put_user(pid, &infop->si_pid);
  867. if (!retval)
  868. retval = put_user(uid, &infop->si_uid);
  869. if (!retval)
  870. retval = put_user(status, &infop->si_status);
  871. }
  872. if (!retval)
  873. retval = pid;
  874. return retval;
  875. }
  876. /*
  877. * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
  878. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  879. * the lock and this task is uninteresting. If we return nonzero, we have
  880. * released the lock and the system call should return.
  881. */
  882. static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
  883. {
  884. unsigned long state;
  885. int retval, status, traced;
  886. pid_t pid = task_pid_vnr(p);
  887. uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));
  888. struct siginfo __user *infop;
  889. if (!likely(wo->wo_flags & WEXITED))
  890. return 0;
  891. if (unlikely(wo->wo_flags & WNOWAIT)) {
  892. int exit_code = p->exit_code;
  893. int why;
  894. get_task_struct(p);
  895. read_unlock(&tasklist_lock);
  896. if ((exit_code & 0x7f) == 0) {
  897. why = CLD_EXITED;
  898. status = exit_code >> 8;
  899. } else {
  900. why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
  901. status = exit_code & 0x7f;
  902. }
  903. return wait_noreap_copyout(wo, p, pid, uid, why, status);
  904. }
  905. /*
  906. * Try to move the task's state to DEAD
  907. * only one thread is allowed to do this:
  908. */
  909. state = xchg(&p->exit_state, EXIT_DEAD);
  910. if (state != EXIT_ZOMBIE) {
  911. BUG_ON(state != EXIT_DEAD);
  912. return 0;
  913. }
  914. traced = ptrace_reparented(p);
  915. /*
  916. * It can be ptraced but not reparented, check
  917. * thread_group_leader() to filter out sub-threads.
  918. */
  919. if (likely(!traced) && thread_group_leader(p)) {
  920. struct signal_struct *psig;
  921. struct signal_struct *sig;
  922. unsigned long maxrss;
  923. cputime_t tgutime, tgstime;
  924. /*
  925. * The resource counters for the group leader are in its
  926. * own task_struct. Those for dead threads in the group
  927. * are in its signal_struct, as are those for the child
  928. * processes it has previously reaped. All these
  929. * accumulate in the parent's signal_struct c* fields.
  930. *
  931. * We don't bother to take a lock here to protect these
  932. * p->signal fields, because they are only touched by
  933. * __exit_signal, which runs with tasklist_lock
  934. * write-locked anyway, and so is excluded here. We do
  935. * need to protect the access to parent->signal fields,
  936. * as other threads in the parent group can be right
  937. * here reaping other children at the same time.
  938. *
  939. * We use thread_group_cputime_adjusted() to get times for the thread
  940. * group, which consolidates times for all threads in the
  941. * group including the group leader.
  942. */
  943. thread_group_cputime_adjusted(p, &tgutime, &tgstime);
  944. spin_lock_irq(&p->real_parent->sighand->siglock);
  945. psig = p->real_parent->signal;
  946. sig = p->signal;
  947. psig->cutime += tgutime + sig->cutime;
  948. psig->cstime += tgstime + sig->cstime;
  949. psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;
  950. psig->cmin_flt +=
  951. p->min_flt + sig->min_flt + sig->cmin_flt;
  952. psig->cmaj_flt +=
  953. p->maj_flt + sig->maj_flt + sig->cmaj_flt;
  954. psig->cnvcsw +=
  955. p->nvcsw + sig->nvcsw + sig->cnvcsw;
  956. psig->cnivcsw +=
  957. p->nivcsw + sig->nivcsw + sig->cnivcsw;
  958. psig->cinblock +=
  959. task_io_get_inblock(p) +
  960. sig->inblock + sig->cinblock;
  961. psig->coublock +=
  962. task_io_get_oublock(p) +
  963. sig->oublock + sig->coublock;
  964. maxrss = max(sig->maxrss, sig->cmaxrss);
  965. if (psig->cmaxrss < maxrss)
  966. psig->cmaxrss = maxrss;
  967. task_io_accounting_add(&psig->ioac, &p->ioac);
  968. task_io_accounting_add(&psig->ioac, &sig->ioac);
  969. spin_unlock_irq(&p->real_parent->sighand->siglock);
  970. }
  971. /*
  972. * Now we are sure this task is interesting, and no other
  973. * thread can reap it because we set its state to EXIT_DEAD.
  974. */
  975. read_unlock(&tasklist_lock);
  976. retval = wo->wo_rusage
  977. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  978. status = (p->signal->flags & SIGNAL_GROUP_EXIT)
  979. ? p->signal->group_exit_code : p->exit_code;
  980. if (!retval && wo->wo_stat)
  981. retval = put_user(status, wo->wo_stat);
  982. infop = wo->wo_info;
  983. if (!retval && infop)
  984. retval = put_user(SIGCHLD, &infop->si_signo);
  985. if (!retval && infop)
  986. retval = put_user(0, &infop->si_errno);
  987. if (!retval && infop) {
  988. int why;
  989. if ((status & 0x7f) == 0) {
  990. why = CLD_EXITED;
  991. status >>= 8;
  992. } else {
  993. why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
  994. status &= 0x7f;
  995. }
  996. retval = put_user((short)why, &infop->si_code);
  997. if (!retval)
  998. retval = put_user(status, &infop->si_status);
  999. }
  1000. if (!retval && infop)
  1001. retval = put_user(pid, &infop->si_pid);
  1002. if (!retval && infop)
  1003. retval = put_user(uid, &infop->si_uid);
  1004. if (!retval)
  1005. retval = pid;
  1006. if (traced) {
  1007. write_lock_irq(&tasklist_lock);
  1008. /* We dropped tasklist, ptracer could die and untrace */
  1009. ptrace_unlink(p);
  1010. /*
  1011. * If this is not a sub-thread, notify the parent.
  1012. * If parent wants a zombie, don't release it now.
  1013. */
  1014. if (thread_group_leader(p) &&
  1015. !do_notify_parent(p, p->exit_signal)) {
  1016. p->exit_state = EXIT_ZOMBIE;
  1017. p = NULL;
  1018. }
  1019. write_unlock_irq(&tasklist_lock);
  1020. }
  1021. if (p != NULL)
  1022. release_task(p);
  1023. return retval;
  1024. }
  1025. static int *task_stopped_code(struct task_struct *p, bool ptrace)
  1026. {
  1027. if (ptrace) {
  1028. if (task_is_stopped_or_traced(p) &&
  1029. !(p->jobctl & JOBCTL_LISTENING))
  1030. return &p->exit_code;
  1031. } else {
  1032. if (p->signal->flags & SIGNAL_STOP_STOPPED)
  1033. return &p->signal->group_exit_code;
  1034. }
  1035. return NULL;
  1036. }
  1037. /**
  1038. * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED
  1039. * @wo: wait options
  1040. * @ptrace: is the wait for ptrace
  1041. * @p: task to wait for
  1042. *
  1043. * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED.
  1044. *
  1045. * CONTEXT:
  1046. * read_lock(&tasklist_lock), which is released if return value is
  1047. * non-zero. Also, grabs and releases @p->sighand->siglock.
  1048. *
  1049. * RETURNS:
  1050. * 0 if wait condition didn't exist and search for other wait conditions
  1051. * should continue. Non-zero return, -errno on failure and @p's pid on
  1052. * success, implies that tasklist_lock is released and wait condition
  1053. * search should terminate.
  1054. */
  1055. static int wait_task_stopped(struct wait_opts *wo,
  1056. int ptrace, struct task_struct *p)
  1057. {
  1058. struct siginfo __user *infop;
  1059. int retval, exit_code, *p_code, why;
  1060. uid_t uid = 0; /* unneeded, required by compiler */
  1061. pid_t pid;
  1062. /*
  1063. * Traditionally we see ptrace'd stopped tasks regardless of options.
  1064. */
  1065. if (!ptrace && !(wo->wo_flags & WUNTRACED))
  1066. return 0;
  1067. if (!task_stopped_code(p, ptrace))
  1068. return 0;
  1069. exit_code = 0;
  1070. spin_lock_irq(&p->sighand->siglock);
  1071. p_code = task_stopped_code(p, ptrace);
  1072. if (unlikely(!p_code))
  1073. goto unlock_sig;
  1074. exit_code = *p_code;
  1075. if (!exit_code)
  1076. goto unlock_sig;
  1077. if (!unlikely(wo->wo_flags & WNOWAIT))
  1078. *p_code = 0;
  1079. uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1080. unlock_sig:
  1081. spin_unlock_irq(&p->sighand->siglock);
  1082. if (!exit_code)
  1083. return 0;
  1084. /*
  1085. * Now we are pretty sure this task is interesting.
  1086. * Make sure it doesn't get reaped out from under us while we
  1087. * give up the lock and then examine it below. We don't want to
  1088. * keep holding onto the tasklist_lock while we call getrusage and
  1089. * possibly take page faults for user memory.
  1090. */
  1091. get_task_struct(p);
  1092. pid = task_pid_vnr(p);
  1093. why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
  1094. read_unlock(&tasklist_lock);
  1095. if (unlikely(wo->wo_flags & WNOWAIT))
  1096. return wait_noreap_copyout(wo, p, pid, uid, why, exit_code);
  1097. retval = wo->wo_rusage
  1098. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  1099. if (!retval && wo->wo_stat)
  1100. retval = put_user((exit_code << 8) | 0x7f, wo->wo_stat);
  1101. infop = wo->wo_info;
  1102. if (!retval && infop)
  1103. retval = put_user(SIGCHLD, &infop->si_signo);
  1104. if (!retval && infop)
  1105. retval = put_user(0, &infop->si_errno);
  1106. if (!retval && infop)
  1107. retval = put_user((short)why, &infop->si_code);
  1108. if (!retval && infop)
  1109. retval = put_user(exit_code, &infop->si_status);
  1110. if (!retval && infop)
  1111. retval = put_user(pid, &infop->si_pid);
  1112. if (!retval && infop)
  1113. retval = put_user(uid, &infop->si_uid);
  1114. if (!retval)
  1115. retval = pid;
  1116. put_task_struct(p);
  1117. BUG_ON(!retval);
  1118. return retval;
  1119. }
  1120. /*
  1121. * Handle do_wait work for one task in a live, non-stopped state.
  1122. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1123. * the lock and this task is uninteresting. If we return nonzero, we have
  1124. * released the lock and the system call should return.
  1125. */
  1126. static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
  1127. {
  1128. int retval;
  1129. pid_t pid;
  1130. uid_t uid;
  1131. if (!unlikely(wo->wo_flags & WCONTINUED))
  1132. return 0;
  1133. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
  1134. return 0;
  1135. spin_lock_irq(&p->sighand->siglock);
  1136. /* Re-check with the lock held. */
  1137. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
  1138. spin_unlock_irq(&p->sighand->siglock);
  1139. return 0;
  1140. }
  1141. if (!unlikely(wo->wo_flags & WNOWAIT))
  1142. p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
  1143. uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1144. spin_unlock_irq(&p->sighand->siglock);
  1145. pid = task_pid_vnr(p);
  1146. get_task_struct(p);
  1147. read_unlock(&tasklist_lock);
  1148. if (!wo->wo_info) {
  1149. retval = wo->wo_rusage
  1150. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  1151. put_task_struct(p);
  1152. if (!retval && wo->wo_stat)
  1153. retval = put_user(0xffff, wo->wo_stat);
  1154. if (!retval)
  1155. retval = pid;
  1156. } else {
  1157. retval = wait_noreap_copyout(wo, p, pid, uid,
  1158. CLD_CONTINUED, SIGCONT);
  1159. BUG_ON(retval == 0);
  1160. }
  1161. return retval;
  1162. }
  1163. /*
  1164. * Consider @p for a wait by @parent.
  1165. *
  1166. * -ECHILD should be in ->notask_error before the first call.
  1167. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1168. * Returns zero if the search for a child should continue;
  1169. * then ->notask_error is 0 if @p is an eligible child,
  1170. * or another error from security_task_wait(), or still -ECHILD.
  1171. */
  1172. static int wait_consider_task(struct wait_opts *wo, int ptrace,
  1173. struct task_struct *p)
  1174. {
  1175. int ret = eligible_child(wo, p);
  1176. if (!ret)
  1177. return ret;
  1178. ret = security_task_wait(p);
  1179. if (unlikely(ret < 0)) {
  1180. /*
  1181. * If we have not yet seen any eligible child,
  1182. * then let this error code replace -ECHILD.
  1183. * A permission error will give the user a clue
  1184. * to look for security policy problems, rather
  1185. * than for mysterious wait bugs.
  1186. */
  1187. if (wo->notask_error)
  1188. wo->notask_error = ret;
  1189. return 0;
  1190. }
  1191. /* dead body doesn't have much to contribute */
  1192. if (unlikely(p->exit_state == EXIT_DEAD)) {
  1193. /*
  1194. * But do not ignore this task until the tracer does
  1195. * wait_task_zombie()->do_notify_parent().
  1196. */
  1197. if (likely(!ptrace) && unlikely(ptrace_reparented(p)))
  1198. wo->notask_error = 0;
  1199. return 0;
  1200. }
  1201. /* slay zombie? */
  1202. if (p->exit_state == EXIT_ZOMBIE) {
  1203. /*
  1204. * A zombie ptracee is only visible to its ptracer.
  1205. * Notification and reaping will be cascaded to the real
  1206. * parent when the ptracer detaches.
  1207. */
  1208. if (likely(!ptrace) && unlikely(p->ptrace)) {
  1209. /* it will become visible, clear notask_error */
  1210. wo->notask_error = 0;
  1211. return 0;
  1212. }
  1213. /* we don't reap group leaders with subthreads */
  1214. if (!delay_group_leader(p))
  1215. return wait_task_zombie(wo, p);
  1216. /*
  1217. * Allow access to stopped/continued state via zombie by
  1218. * falling through. Clearing of notask_error is complex.
  1219. *
  1220. * When !@ptrace:
  1221. *
  1222. * If WEXITED is set, notask_error should naturally be
  1223. * cleared. If not, subset of WSTOPPED|WCONTINUED is set,
  1224. * so, if there are live subthreads, there are events to
  1225. * wait for. If all subthreads are dead, it's still safe
  1226. * to clear - this function will be called again in finite
  1227. * amount time once all the subthreads are released and
  1228. * will then return without clearing.
  1229. *
  1230. * When @ptrace:
  1231. *
  1232. * Stopped state is per-task and thus can't change once the
  1233. * target task dies. Only continued and exited can happen.
  1234. * Clear notask_error if WCONTINUED | WEXITED.
  1235. */
  1236. if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED)))
  1237. wo->notask_error = 0;
  1238. } else {
  1239. /*
  1240. * If @p is ptraced by a task in its real parent's group,
  1241. * hide group stop/continued state when looking at @p as
  1242. * the real parent; otherwise, a single stop can be
  1243. * reported twice as group and ptrace stops.
  1244. *
  1245. * If a ptracer wants to distinguish the two events for its
  1246. * own children, it should create a separate process which
  1247. * takes the role of real parent.
  1248. */
  1249. if (likely(!ptrace) && p->ptrace && !ptrace_reparented(p))
  1250. return 0;
  1251. /*
  1252. * @p is alive and it's gonna stop, continue or exit, so
  1253. * there always is something to wait for.
  1254. */
  1255. wo->notask_error = 0;
  1256. }
  1257. /*
  1258. * Wait for stopped. Depending on @ptrace, different stopped state
  1259. * is used and the two don't interact with each other.
  1260. */
  1261. ret = wait_task_stopped(wo, ptrace, p);
  1262. if (ret)
  1263. return ret;
  1264. /*
  1265. * Wait for continued. There's only one continued state and the
  1266. * ptracer can consume it which can confuse the real parent. Don't
  1267. * use WCONTINUED from ptracer. You don't need or want it.
  1268. */
  1269. return wait_task_continued(wo, p);
  1270. }
  1271. /*
  1272. * Do the work of do_wait() for one thread in the group, @tsk.
  1273. *
  1274. * -ECHILD should be in ->notask_error before the first call.
  1275. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1276. * Returns zero if the search for a child should continue; then
  1277. * ->notask_error is 0 if there were any eligible children,
  1278. * or another error from security_task_wait(), or still -ECHILD.
  1279. */
  1280. static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
  1281. {
  1282. struct task_struct *p;
  1283. list_for_each_entry(p, &tsk->children, sibling) {
  1284. int ret = wait_consider_task(wo, 0, p);
  1285. if (ret)
  1286. return ret;
  1287. }
  1288. return 0;
  1289. }
  1290. static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
  1291. {
  1292. struct task_struct *p;
  1293. list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
  1294. int ret = wait_consider_task(wo, 1, p);
  1295. if (ret)
  1296. return ret;
  1297. }
  1298. return 0;
  1299. }
  1300. static int child_wait_callback(wait_queue_t *wait, unsigned mode,
  1301. int sync, void *key)
  1302. {
  1303. struct wait_opts *wo = container_of(wait, struct wait_opts,
  1304. child_wait);
  1305. struct task_struct *p = key;
  1306. if (!eligible_pid(wo, p))
  1307. return 0;
  1308. if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
  1309. return 0;
  1310. return default_wake_function(wait, mode, sync, key);
  1311. }
  1312. void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
  1313. {
  1314. __wake_up_sync_key(&parent->signal->wait_chldexit,
  1315. TASK_INTERRUPTIBLE, 1, p);
  1316. }
  1317. static long do_wait(struct wait_opts *wo)
  1318. {
  1319. struct task_struct *tsk;
  1320. int retval;
  1321. trace_sched_process_wait(wo->wo_pid);
  1322. init_waitqueue_func_entry(&wo->child_wait, child_wait_callback);
  1323. wo->child_wait.private = current;
  1324. add_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
  1325. repeat:
  1326. /*
  1327. * If there is nothing that can match our critiera just get out.
  1328. * We will clear ->notask_error to zero if we see any child that
  1329. * might later match our criteria, even if we are not able to reap
  1330. * it yet.
  1331. */
  1332. wo->notask_error = -ECHILD;
  1333. if ((wo->wo_type < PIDTYPE_MAX) &&
  1334. (!wo->wo_pid || hlist_empty(&wo->wo_pid->tasks[wo->wo_type])))
  1335. goto notask;
  1336. set_current_state(TASK_INTERRUPTIBLE);
  1337. read_lock(&tasklist_lock);
  1338. tsk = current;
  1339. do {
  1340. retval = do_wait_thread(wo, tsk);
  1341. if (retval)
  1342. goto end;
  1343. retval = ptrace_do_wait(wo, tsk);
  1344. if (retval)
  1345. goto end;
  1346. if (wo->wo_flags & __WNOTHREAD)
  1347. break;
  1348. } while_each_thread(current, tsk);
  1349. read_unlock(&tasklist_lock);
  1350. notask:
  1351. retval = wo->notask_error;
  1352. if (!retval && !(wo->wo_flags & WNOHANG)) {
  1353. retval = -ERESTARTSYS;
  1354. if (!signal_pending(current)) {
  1355. schedule();
  1356. goto repeat;
  1357. }
  1358. }
  1359. end:
  1360. __set_current_state(TASK_RUNNING);
  1361. remove_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
  1362. return retval;
  1363. }
  1364. SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
  1365. infop, int, options, struct rusage __user *, ru)
  1366. {
  1367. struct wait_opts wo;
  1368. struct pid *pid = NULL;
  1369. enum pid_type type;
  1370. long ret;
  1371. if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
  1372. return -EINVAL;
  1373. if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
  1374. return -EINVAL;
  1375. switch (which) {
  1376. case P_ALL:
  1377. type = PIDTYPE_MAX;
  1378. break;
  1379. case P_PID:
  1380. type = PIDTYPE_PID;
  1381. if (upid <= 0)
  1382. return -EINVAL;
  1383. break;
  1384. case P_PGID:
  1385. type = PIDTYPE_PGID;
  1386. if (upid <= 0)
  1387. return -EINVAL;
  1388. break;
  1389. default:
  1390. return -EINVAL;
  1391. }
  1392. if (type < PIDTYPE_MAX)
  1393. pid = find_get_pid(upid);
  1394. wo.wo_type = type;
  1395. wo.wo_pid = pid;
  1396. wo.wo_flags = options;
  1397. wo.wo_info = infop;
  1398. wo.wo_stat = NULL;
  1399. wo.wo_rusage = ru;
  1400. ret = do_wait(&wo);
  1401. if (ret > 0) {
  1402. ret = 0;
  1403. } else if (infop) {
  1404. /*
  1405. * For a WNOHANG return, clear out all the fields
  1406. * we would set so the user can easily tell the
  1407. * difference.
  1408. */
  1409. if (!ret)
  1410. ret = put_user(0, &infop->si_signo);
  1411. if (!ret)
  1412. ret = put_user(0, &infop->si_errno);
  1413. if (!ret)
  1414. ret = put_user(0, &infop->si_code);
  1415. if (!ret)
  1416. ret = put_user(0, &infop->si_pid);
  1417. if (!ret)
  1418. ret = put_user(0, &infop->si_uid);
  1419. if (!ret)
  1420. ret = put_user(0, &infop->si_status);
  1421. }
  1422. put_pid(pid);
  1423. return ret;
  1424. }
  1425. SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
  1426. int, options, struct rusage __user *, ru)
  1427. {
  1428. struct wait_opts wo;
  1429. struct pid *pid = NULL;
  1430. enum pid_type type;
  1431. long ret;
  1432. if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
  1433. __WNOTHREAD|__WCLONE|__WALL))
  1434. return -EINVAL;
  1435. if (upid == -1)
  1436. type = PIDTYPE_MAX;
  1437. else if (upid < 0) {
  1438. type = PIDTYPE_PGID;
  1439. pid = find_get_pid(-upid);
  1440. } else if (upid == 0) {
  1441. type = PIDTYPE_PGID;
  1442. pid = get_task_pid(current, PIDTYPE_PGID);
  1443. } else /* upid > 0 */ {
  1444. type = PIDTYPE_PID;
  1445. pid = find_get_pid(upid);
  1446. }
  1447. wo.wo_type = type;
  1448. wo.wo_pid = pid;
  1449. wo.wo_flags = options | WEXITED;
  1450. wo.wo_info = NULL;
  1451. wo.wo_stat = stat_addr;
  1452. wo.wo_rusage = ru;
  1453. ret = do_wait(&wo);
  1454. put_pid(pid);
  1455. return ret;
  1456. }
  1457. #ifdef __ARCH_WANT_SYS_WAITPID
  1458. /*
  1459. * sys_waitpid() remains for compatibility. waitpid() should be
  1460. * implemented by calling sys_wait4() from libc.a.
  1461. */
  1462. SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
  1463. {
  1464. return sys_wait4(pid, stat_addr, options, NULL);
  1465. }
  1466. #endif