exit.c 43 KB

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