exit.c 43 KB

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