exit.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  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. if (group_dead)
  691. disassociate_ctty(1);
  692. exit_task_namespaces(tsk);
  693. exit_task_work(tsk);
  694. exit_thread();
  695. /*
  696. * Flush inherited counters to the parent - before the parent
  697. * gets woken up by child-exit notifications.
  698. *
  699. * because of cgroup mode, must be called before cgroup_exit()
  700. */
  701. perf_event_exit_task(tsk);
  702. cgroup_exit(tsk);
  703. module_put(task_thread_info(tsk)->exec_domain->module);
  704. /*
  705. * FIXME: do that only when needed, using sched_exit tracepoint
  706. */
  707. flush_ptrace_hw_breakpoint(tsk);
  708. exit_notify(tsk, group_dead);
  709. proc_exit_connector(tsk);
  710. #ifdef CONFIG_NUMA
  711. task_lock(tsk);
  712. mpol_put(tsk->mempolicy);
  713. tsk->mempolicy = NULL;
  714. task_unlock(tsk);
  715. #endif
  716. #ifdef CONFIG_FUTEX
  717. if (unlikely(current->pi_state_cache))
  718. kfree(current->pi_state_cache);
  719. #endif
  720. /*
  721. * Make sure we are holding no locks:
  722. */
  723. debug_check_no_locks_held();
  724. /*
  725. * We can do this unlocked here. The futex code uses this flag
  726. * just to verify whether the pi state cleanup has been done
  727. * or not. In the worst case it loops once more.
  728. */
  729. tsk->flags |= PF_EXITPIDONE;
  730. if (tsk->io_context)
  731. exit_io_context(tsk);
  732. if (tsk->splice_pipe)
  733. free_pipe_info(tsk->splice_pipe);
  734. if (tsk->task_frag.page)
  735. put_page(tsk->task_frag.page);
  736. validate_creds_for_do_exit(tsk);
  737. check_stack_usage();
  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. traced = ptrace_reparented(p);
  907. /*
  908. * Move the task's state to DEAD/TRACE, only one thread can do this.
  909. */
  910. state = traced && thread_group_leader(p) ? EXIT_TRACE : EXIT_DEAD;
  911. if (cmpxchg(&p->exit_state, EXIT_ZOMBIE, state) != EXIT_ZOMBIE)
  912. return 0;
  913. /*
  914. * It can be ptraced but not reparented, check
  915. * thread_group_leader() to filter out sub-threads.
  916. */
  917. if (likely(!traced) && thread_group_leader(p)) {
  918. struct signal_struct *psig;
  919. struct signal_struct *sig;
  920. unsigned long maxrss;
  921. cputime_t tgutime, tgstime;
  922. /*
  923. * The resource counters for the group leader are in its
  924. * own task_struct. Those for dead threads in the group
  925. * are in its signal_struct, as are those for the child
  926. * processes it has previously reaped. All these
  927. * accumulate in the parent's signal_struct c* fields.
  928. *
  929. * We don't bother to take a lock here to protect these
  930. * p->signal fields, because they are only touched by
  931. * __exit_signal, which runs with tasklist_lock
  932. * write-locked anyway, and so is excluded here. We do
  933. * need to protect the access to parent->signal fields,
  934. * as other threads in the parent group can be right
  935. * here reaping other children at the same time.
  936. *
  937. * We use thread_group_cputime_adjusted() to get times for the thread
  938. * group, which consolidates times for all threads in the
  939. * group including the group leader.
  940. */
  941. thread_group_cputime_adjusted(p, &tgutime, &tgstime);
  942. spin_lock_irq(&p->real_parent->sighand->siglock);
  943. psig = p->real_parent->signal;
  944. sig = p->signal;
  945. psig->cutime += tgutime + sig->cutime;
  946. psig->cstime += tgstime + sig->cstime;
  947. psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;
  948. psig->cmin_flt +=
  949. p->min_flt + sig->min_flt + sig->cmin_flt;
  950. psig->cmaj_flt +=
  951. p->maj_flt + sig->maj_flt + sig->cmaj_flt;
  952. psig->cnvcsw +=
  953. p->nvcsw + sig->nvcsw + sig->cnvcsw;
  954. psig->cnivcsw +=
  955. p->nivcsw + sig->nivcsw + sig->cnivcsw;
  956. psig->cinblock +=
  957. task_io_get_inblock(p) +
  958. sig->inblock + sig->cinblock;
  959. psig->coublock +=
  960. task_io_get_oublock(p) +
  961. sig->oublock + sig->coublock;
  962. maxrss = max(sig->maxrss, sig->cmaxrss);
  963. if (psig->cmaxrss < maxrss)
  964. psig->cmaxrss = maxrss;
  965. task_io_accounting_add(&psig->ioac, &p->ioac);
  966. task_io_accounting_add(&psig->ioac, &sig->ioac);
  967. spin_unlock_irq(&p->real_parent->sighand->siglock);
  968. }
  969. /*
  970. * Now we are sure this task is interesting, and no other
  971. * thread can reap it because we its state == DEAD/TRACE.
  972. */
  973. read_unlock(&tasklist_lock);
  974. retval = wo->wo_rusage
  975. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  976. status = (p->signal->flags & SIGNAL_GROUP_EXIT)
  977. ? p->signal->group_exit_code : p->exit_code;
  978. if (!retval && wo->wo_stat)
  979. retval = put_user(status, wo->wo_stat);
  980. infop = wo->wo_info;
  981. if (!retval && infop)
  982. retval = put_user(SIGCHLD, &infop->si_signo);
  983. if (!retval && infop)
  984. retval = put_user(0, &infop->si_errno);
  985. if (!retval && infop) {
  986. int why;
  987. if ((status & 0x7f) == 0) {
  988. why = CLD_EXITED;
  989. status >>= 8;
  990. } else {
  991. why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
  992. status &= 0x7f;
  993. }
  994. retval = put_user((short)why, &infop->si_code);
  995. if (!retval)
  996. retval = put_user(status, &infop->si_status);
  997. }
  998. if (!retval && infop)
  999. retval = put_user(pid, &infop->si_pid);
  1000. if (!retval && infop)
  1001. retval = put_user(uid, &infop->si_uid);
  1002. if (!retval)
  1003. retval = pid;
  1004. if (state == EXIT_TRACE) {
  1005. write_lock_irq(&tasklist_lock);
  1006. /* We dropped tasklist, ptracer could die and untrace */
  1007. ptrace_unlink(p);
  1008. /* If parent wants a zombie, don't release it now */
  1009. state = EXIT_ZOMBIE;
  1010. if (do_notify_parent(p, p->exit_signal))
  1011. state = EXIT_DEAD;
  1012. p->exit_state = state;
  1013. write_unlock_irq(&tasklist_lock);
  1014. }
  1015. if (state == EXIT_DEAD)
  1016. release_task(p);
  1017. return retval;
  1018. }
  1019. static int *task_stopped_code(struct task_struct *p, bool ptrace)
  1020. {
  1021. if (ptrace) {
  1022. if (task_is_stopped_or_traced(p) &&
  1023. !(p->jobctl & JOBCTL_LISTENING))
  1024. return &p->exit_code;
  1025. } else {
  1026. if (p->signal->flags & SIGNAL_STOP_STOPPED)
  1027. return &p->signal->group_exit_code;
  1028. }
  1029. return NULL;
  1030. }
  1031. /**
  1032. * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED
  1033. * @wo: wait options
  1034. * @ptrace: is the wait for ptrace
  1035. * @p: task to wait for
  1036. *
  1037. * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED.
  1038. *
  1039. * CONTEXT:
  1040. * read_lock(&tasklist_lock), which is released if return value is
  1041. * non-zero. Also, grabs and releases @p->sighand->siglock.
  1042. *
  1043. * RETURNS:
  1044. * 0 if wait condition didn't exist and search for other wait conditions
  1045. * should continue. Non-zero return, -errno on failure and @p's pid on
  1046. * success, implies that tasklist_lock is released and wait condition
  1047. * search should terminate.
  1048. */
  1049. static int wait_task_stopped(struct wait_opts *wo,
  1050. int ptrace, struct task_struct *p)
  1051. {
  1052. struct siginfo __user *infop;
  1053. int retval, exit_code, *p_code, why;
  1054. uid_t uid = 0; /* unneeded, required by compiler */
  1055. pid_t pid;
  1056. /*
  1057. * Traditionally we see ptrace'd stopped tasks regardless of options.
  1058. */
  1059. if (!ptrace && !(wo->wo_flags & WUNTRACED))
  1060. return 0;
  1061. if (!task_stopped_code(p, ptrace))
  1062. return 0;
  1063. exit_code = 0;
  1064. spin_lock_irq(&p->sighand->siglock);
  1065. p_code = task_stopped_code(p, ptrace);
  1066. if (unlikely(!p_code))
  1067. goto unlock_sig;
  1068. exit_code = *p_code;
  1069. if (!exit_code)
  1070. goto unlock_sig;
  1071. if (!unlikely(wo->wo_flags & WNOWAIT))
  1072. *p_code = 0;
  1073. uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1074. unlock_sig:
  1075. spin_unlock_irq(&p->sighand->siglock);
  1076. if (!exit_code)
  1077. return 0;
  1078. /*
  1079. * Now we are pretty sure this task is interesting.
  1080. * Make sure it doesn't get reaped out from under us while we
  1081. * give up the lock and then examine it below. We don't want to
  1082. * keep holding onto the tasklist_lock while we call getrusage and
  1083. * possibly take page faults for user memory.
  1084. */
  1085. get_task_struct(p);
  1086. pid = task_pid_vnr(p);
  1087. why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
  1088. read_unlock(&tasklist_lock);
  1089. if (unlikely(wo->wo_flags & WNOWAIT))
  1090. return wait_noreap_copyout(wo, p, pid, uid, why, exit_code);
  1091. retval = wo->wo_rusage
  1092. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  1093. if (!retval && wo->wo_stat)
  1094. retval = put_user((exit_code << 8) | 0x7f, wo->wo_stat);
  1095. infop = wo->wo_info;
  1096. if (!retval && infop)
  1097. retval = put_user(SIGCHLD, &infop->si_signo);
  1098. if (!retval && infop)
  1099. retval = put_user(0, &infop->si_errno);
  1100. if (!retval && infop)
  1101. retval = put_user((short)why, &infop->si_code);
  1102. if (!retval && infop)
  1103. retval = put_user(exit_code, &infop->si_status);
  1104. if (!retval && infop)
  1105. retval = put_user(pid, &infop->si_pid);
  1106. if (!retval && infop)
  1107. retval = put_user(uid, &infop->si_uid);
  1108. if (!retval)
  1109. retval = pid;
  1110. put_task_struct(p);
  1111. BUG_ON(!retval);
  1112. return retval;
  1113. }
  1114. /*
  1115. * Handle do_wait work for one task in a live, non-stopped state.
  1116. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1117. * the lock and this task is uninteresting. If we return nonzero, we have
  1118. * released the lock and the system call should return.
  1119. */
  1120. static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
  1121. {
  1122. int retval;
  1123. pid_t pid;
  1124. uid_t uid;
  1125. if (!unlikely(wo->wo_flags & WCONTINUED))
  1126. return 0;
  1127. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
  1128. return 0;
  1129. spin_lock_irq(&p->sighand->siglock);
  1130. /* Re-check with the lock held. */
  1131. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
  1132. spin_unlock_irq(&p->sighand->siglock);
  1133. return 0;
  1134. }
  1135. if (!unlikely(wo->wo_flags & WNOWAIT))
  1136. p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
  1137. uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1138. spin_unlock_irq(&p->sighand->siglock);
  1139. pid = task_pid_vnr(p);
  1140. get_task_struct(p);
  1141. read_unlock(&tasklist_lock);
  1142. if (!wo->wo_info) {
  1143. retval = wo->wo_rusage
  1144. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  1145. put_task_struct(p);
  1146. if (!retval && wo->wo_stat)
  1147. retval = put_user(0xffff, wo->wo_stat);
  1148. if (!retval)
  1149. retval = pid;
  1150. } else {
  1151. retval = wait_noreap_copyout(wo, p, pid, uid,
  1152. CLD_CONTINUED, SIGCONT);
  1153. BUG_ON(retval == 0);
  1154. }
  1155. return retval;
  1156. }
  1157. /*
  1158. * Consider @p for a wait by @parent.
  1159. *
  1160. * -ECHILD should be in ->notask_error before the first call.
  1161. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1162. * Returns zero if the search for a child should continue;
  1163. * then ->notask_error is 0 if @p is an eligible child,
  1164. * or another error from security_task_wait(), or still -ECHILD.
  1165. */
  1166. static int wait_consider_task(struct wait_opts *wo, int ptrace,
  1167. struct task_struct *p)
  1168. {
  1169. int ret;
  1170. if (unlikely(p->exit_state == EXIT_DEAD))
  1171. return 0;
  1172. ret = eligible_child(wo, p);
  1173. if (!ret)
  1174. return ret;
  1175. ret = security_task_wait(p);
  1176. if (unlikely(ret < 0)) {
  1177. /*
  1178. * If we have not yet seen any eligible child,
  1179. * then let this error code replace -ECHILD.
  1180. * A permission error will give the user a clue
  1181. * to look for security policy problems, rather
  1182. * than for mysterious wait bugs.
  1183. */
  1184. if (wo->notask_error)
  1185. wo->notask_error = ret;
  1186. return 0;
  1187. }
  1188. if (unlikely(p->exit_state == EXIT_TRACE)) {
  1189. /*
  1190. * ptrace == 0 means we are the natural parent. In this case
  1191. * we should clear notask_error, debugger will notify us.
  1192. */
  1193. if (likely(!ptrace))
  1194. wo->notask_error = 0;
  1195. return 0;
  1196. }
  1197. if (likely(!ptrace) && unlikely(p->ptrace)) {
  1198. /*
  1199. * If it is traced by its real parent's group, just pretend
  1200. * the caller is ptrace_do_wait() and reap this child if it
  1201. * is zombie.
  1202. *
  1203. * This also hides group stop state from real parent; otherwise
  1204. * a single stop can be reported twice as group and ptrace stop.
  1205. * If a ptracer wants to distinguish these two events for its
  1206. * own children it should create a separate process which takes
  1207. * the role of real parent.
  1208. */
  1209. if (!ptrace_reparented(p))
  1210. ptrace = 1;
  1211. }
  1212. /* slay zombie? */
  1213. if (p->exit_state == EXIT_ZOMBIE) {
  1214. /* we don't reap group leaders with subthreads */
  1215. if (!delay_group_leader(p)) {
  1216. /*
  1217. * A zombie ptracee is only visible to its ptracer.
  1218. * Notification and reaping will be cascaded to the
  1219. * real parent when the ptracer detaches.
  1220. */
  1221. if (unlikely(ptrace) || likely(!p->ptrace))
  1222. return wait_task_zombie(wo, p);
  1223. }
  1224. /*
  1225. * Allow access to stopped/continued state via zombie by
  1226. * falling through. Clearing of notask_error is complex.
  1227. *
  1228. * When !@ptrace:
  1229. *
  1230. * If WEXITED is set, notask_error should naturally be
  1231. * cleared. If not, subset of WSTOPPED|WCONTINUED is set,
  1232. * so, if there are live subthreads, there are events to
  1233. * wait for. If all subthreads are dead, it's still safe
  1234. * to clear - this function will be called again in finite
  1235. * amount time once all the subthreads are released and
  1236. * will then return without clearing.
  1237. *
  1238. * When @ptrace:
  1239. *
  1240. * Stopped state is per-task and thus can't change once the
  1241. * target task dies. Only continued and exited can happen.
  1242. * Clear notask_error if WCONTINUED | WEXITED.
  1243. */
  1244. if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED)))
  1245. wo->notask_error = 0;
  1246. } else {
  1247. /*
  1248. * @p is alive and it's gonna stop, continue or exit, so
  1249. * there always is something to wait for.
  1250. */
  1251. wo->notask_error = 0;
  1252. }
  1253. /*
  1254. * Wait for stopped. Depending on @ptrace, different stopped state
  1255. * is used and the two don't interact with each other.
  1256. */
  1257. ret = wait_task_stopped(wo, ptrace, p);
  1258. if (ret)
  1259. return ret;
  1260. /*
  1261. * Wait for continued. There's only one continued state and the
  1262. * ptracer can consume it which can confuse the real parent. Don't
  1263. * use WCONTINUED from ptracer. You don't need or want it.
  1264. */
  1265. return wait_task_continued(wo, p);
  1266. }
  1267. /*
  1268. * Do the work of do_wait() for one thread in the group, @tsk.
  1269. *
  1270. * -ECHILD should be in ->notask_error before the first call.
  1271. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1272. * Returns zero if the search for a child should continue; then
  1273. * ->notask_error is 0 if there were any eligible children,
  1274. * or another error from security_task_wait(), or still -ECHILD.
  1275. */
  1276. static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
  1277. {
  1278. struct task_struct *p;
  1279. list_for_each_entry(p, &tsk->children, sibling) {
  1280. int ret = wait_consider_task(wo, 0, p);
  1281. if (ret)
  1282. return ret;
  1283. }
  1284. return 0;
  1285. }
  1286. static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
  1287. {
  1288. struct task_struct *p;
  1289. list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
  1290. int ret = wait_consider_task(wo, 1, p);
  1291. if (ret)
  1292. return ret;
  1293. }
  1294. return 0;
  1295. }
  1296. static int child_wait_callback(wait_queue_t *wait, unsigned mode,
  1297. int sync, void *key)
  1298. {
  1299. struct wait_opts *wo = container_of(wait, struct wait_opts,
  1300. child_wait);
  1301. struct task_struct *p = key;
  1302. if (!eligible_pid(wo, p))
  1303. return 0;
  1304. if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
  1305. return 0;
  1306. return default_wake_function(wait, mode, sync, key);
  1307. }
  1308. void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
  1309. {
  1310. __wake_up_sync_key(&parent->signal->wait_chldexit,
  1311. TASK_INTERRUPTIBLE, 1, p);
  1312. }
  1313. static long do_wait(struct wait_opts *wo)
  1314. {
  1315. struct task_struct *tsk;
  1316. int retval;
  1317. trace_sched_process_wait(wo->wo_pid);
  1318. init_waitqueue_func_entry(&wo->child_wait, child_wait_callback);
  1319. wo->child_wait.private = current;
  1320. add_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
  1321. repeat:
  1322. /*
  1323. * If there is nothing that can match our critiera just get out.
  1324. * We will clear ->notask_error to zero if we see any child that
  1325. * might later match our criteria, even if we are not able to reap
  1326. * it yet.
  1327. */
  1328. wo->notask_error = -ECHILD;
  1329. if ((wo->wo_type < PIDTYPE_MAX) &&
  1330. (!wo->wo_pid || hlist_empty(&wo->wo_pid->tasks[wo->wo_type])))
  1331. goto notask;
  1332. set_current_state(TASK_INTERRUPTIBLE);
  1333. read_lock(&tasklist_lock);
  1334. tsk = current;
  1335. do {
  1336. retval = do_wait_thread(wo, tsk);
  1337. if (retval)
  1338. goto end;
  1339. retval = ptrace_do_wait(wo, tsk);
  1340. if (retval)
  1341. goto end;
  1342. if (wo->wo_flags & __WNOTHREAD)
  1343. break;
  1344. } while_each_thread(current, tsk);
  1345. read_unlock(&tasklist_lock);
  1346. notask:
  1347. retval = wo->notask_error;
  1348. if (!retval && !(wo->wo_flags & WNOHANG)) {
  1349. retval = -ERESTARTSYS;
  1350. if (!signal_pending(current)) {
  1351. schedule();
  1352. goto repeat;
  1353. }
  1354. }
  1355. end:
  1356. __set_current_state(TASK_RUNNING);
  1357. remove_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
  1358. return retval;
  1359. }
  1360. SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
  1361. infop, int, options, struct rusage __user *, ru)
  1362. {
  1363. struct wait_opts wo;
  1364. struct pid *pid = NULL;
  1365. enum pid_type type;
  1366. long ret;
  1367. if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
  1368. return -EINVAL;
  1369. if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
  1370. return -EINVAL;
  1371. switch (which) {
  1372. case P_ALL:
  1373. type = PIDTYPE_MAX;
  1374. break;
  1375. case P_PID:
  1376. type = PIDTYPE_PID;
  1377. if (upid <= 0)
  1378. return -EINVAL;
  1379. break;
  1380. case P_PGID:
  1381. type = PIDTYPE_PGID;
  1382. if (upid <= 0)
  1383. return -EINVAL;
  1384. break;
  1385. default:
  1386. return -EINVAL;
  1387. }
  1388. if (type < PIDTYPE_MAX)
  1389. pid = find_get_pid(upid);
  1390. wo.wo_type = type;
  1391. wo.wo_pid = pid;
  1392. wo.wo_flags = options;
  1393. wo.wo_info = infop;
  1394. wo.wo_stat = NULL;
  1395. wo.wo_rusage = ru;
  1396. ret = do_wait(&wo);
  1397. if (ret > 0) {
  1398. ret = 0;
  1399. } else if (infop) {
  1400. /*
  1401. * For a WNOHANG return, clear out all the fields
  1402. * we would set so the user can easily tell the
  1403. * difference.
  1404. */
  1405. if (!ret)
  1406. ret = put_user(0, &infop->si_signo);
  1407. if (!ret)
  1408. ret = put_user(0, &infop->si_errno);
  1409. if (!ret)
  1410. ret = put_user(0, &infop->si_code);
  1411. if (!ret)
  1412. ret = put_user(0, &infop->si_pid);
  1413. if (!ret)
  1414. ret = put_user(0, &infop->si_uid);
  1415. if (!ret)
  1416. ret = put_user(0, &infop->si_status);
  1417. }
  1418. put_pid(pid);
  1419. return ret;
  1420. }
  1421. SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
  1422. int, options, struct rusage __user *, ru)
  1423. {
  1424. struct wait_opts wo;
  1425. struct pid *pid = NULL;
  1426. enum pid_type type;
  1427. long ret;
  1428. if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
  1429. __WNOTHREAD|__WCLONE|__WALL))
  1430. return -EINVAL;
  1431. if (upid == -1)
  1432. type = PIDTYPE_MAX;
  1433. else if (upid < 0) {
  1434. type = PIDTYPE_PGID;
  1435. pid = find_get_pid(-upid);
  1436. } else if (upid == 0) {
  1437. type = PIDTYPE_PGID;
  1438. pid = get_task_pid(current, PIDTYPE_PGID);
  1439. } else /* upid > 0 */ {
  1440. type = PIDTYPE_PID;
  1441. pid = find_get_pid(upid);
  1442. }
  1443. wo.wo_type = type;
  1444. wo.wo_pid = pid;
  1445. wo.wo_flags = options | WEXITED;
  1446. wo.wo_info = NULL;
  1447. wo.wo_stat = stat_addr;
  1448. wo.wo_rusage = ru;
  1449. ret = do_wait(&wo);
  1450. put_pid(pid);
  1451. return ret;
  1452. }
  1453. #ifdef __ARCH_WANT_SYS_WAITPID
  1454. /*
  1455. * sys_waitpid() remains for compatibility. waitpid() should be
  1456. * implemented by calling sys_wait4() from libc.a.
  1457. */
  1458. SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
  1459. {
  1460. return sys_wait4(pid, stat_addr, options, NULL);
  1461. }
  1462. #endif