exit.c 43 KB

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