exit.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  1. /*
  2. * linux/kernel/exit.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/config.h>
  7. #include <linux/mm.h>
  8. #include <linux/slab.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/smp_lock.h>
  11. #include <linux/module.h>
  12. #include <linux/completion.h>
  13. #include <linux/personality.h>
  14. #include <linux/tty.h>
  15. #include <linux/namespace.h>
  16. #include <linux/key.h>
  17. #include <linux/security.h>
  18. #include <linux/cpu.h>
  19. #include <linux/acct.h>
  20. #include <linux/file.h>
  21. #include <linux/binfmts.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/profile.h>
  24. #include <linux/mount.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/mempolicy.h>
  27. #include <linux/cpuset.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/signal.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/unistd.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/mmu_context.h>
  34. extern void sem_exit (void);
  35. extern struct task_struct *child_reaper;
  36. int getrusage(struct task_struct *, int, struct rusage __user *);
  37. static void exit_mm(struct task_struct * tsk);
  38. static void __unhash_process(struct task_struct *p)
  39. {
  40. nr_threads--;
  41. detach_pid(p, PIDTYPE_PID);
  42. detach_pid(p, PIDTYPE_TGID);
  43. if (thread_group_leader(p)) {
  44. detach_pid(p, PIDTYPE_PGID);
  45. detach_pid(p, PIDTYPE_SID);
  46. if (p->pid)
  47. __get_cpu_var(process_counts)--;
  48. }
  49. REMOVE_LINKS(p);
  50. }
  51. void release_task(struct task_struct * p)
  52. {
  53. int zap_leader;
  54. task_t *leader;
  55. struct dentry *proc_dentry;
  56. repeat:
  57. atomic_dec(&p->user->processes);
  58. spin_lock(&p->proc_lock);
  59. proc_dentry = proc_pid_unhash(p);
  60. write_lock_irq(&tasklist_lock);
  61. if (unlikely(p->ptrace))
  62. __ptrace_unlink(p);
  63. BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
  64. __exit_signal(p);
  65. __exit_sighand(p);
  66. /*
  67. * Note that the fastpath in sys_times depends on __exit_signal having
  68. * updated the counters before a task is removed from the tasklist of
  69. * the process by __unhash_process.
  70. */
  71. __unhash_process(p);
  72. /*
  73. * If we are the last non-leader member of the thread
  74. * group, and the leader is zombie, then notify the
  75. * group leader's parent process. (if it wants notification.)
  76. */
  77. zap_leader = 0;
  78. leader = p->group_leader;
  79. if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
  80. BUG_ON(leader->exit_signal == -1);
  81. do_notify_parent(leader, leader->exit_signal);
  82. /*
  83. * If we were the last child thread and the leader has
  84. * exited already, and the leader's parent ignores SIGCHLD,
  85. * then we are the one who should release the leader.
  86. *
  87. * do_notify_parent() will have marked it self-reaping in
  88. * that case.
  89. */
  90. zap_leader = (leader->exit_signal == -1);
  91. }
  92. sched_exit(p);
  93. write_unlock_irq(&tasklist_lock);
  94. spin_unlock(&p->proc_lock);
  95. proc_pid_flush(proc_dentry);
  96. release_thread(p);
  97. put_task_struct(p);
  98. p = leader;
  99. if (unlikely(zap_leader))
  100. goto repeat;
  101. }
  102. /* we are using it only for SMP init */
  103. void unhash_process(struct task_struct *p)
  104. {
  105. struct dentry *proc_dentry;
  106. spin_lock(&p->proc_lock);
  107. proc_dentry = proc_pid_unhash(p);
  108. write_lock_irq(&tasklist_lock);
  109. __unhash_process(p);
  110. write_unlock_irq(&tasklist_lock);
  111. spin_unlock(&p->proc_lock);
  112. proc_pid_flush(proc_dentry);
  113. }
  114. /*
  115. * This checks not only the pgrp, but falls back on the pid if no
  116. * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
  117. * without this...
  118. */
  119. int session_of_pgrp(int pgrp)
  120. {
  121. struct task_struct *p;
  122. int sid = -1;
  123. read_lock(&tasklist_lock);
  124. do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
  125. if (p->signal->session > 0) {
  126. sid = p->signal->session;
  127. goto out;
  128. }
  129. } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
  130. p = find_task_by_pid(pgrp);
  131. if (p)
  132. sid = p->signal->session;
  133. out:
  134. read_unlock(&tasklist_lock);
  135. return sid;
  136. }
  137. /*
  138. * Determine if a process group is "orphaned", according to the POSIX
  139. * definition in 2.2.2.52. Orphaned process groups are not to be affected
  140. * by terminal-generated stop signals. Newly orphaned process groups are
  141. * to receive a SIGHUP and a SIGCONT.
  142. *
  143. * "I ask you, have you ever known what it is to be an orphan?"
  144. */
  145. static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
  146. {
  147. struct task_struct *p;
  148. int ret = 1;
  149. do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
  150. if (p == ignored_task
  151. || p->exit_state
  152. || p->real_parent->pid == 1)
  153. continue;
  154. if (process_group(p->real_parent) != pgrp
  155. && p->real_parent->signal->session == p->signal->session) {
  156. ret = 0;
  157. break;
  158. }
  159. } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
  160. return ret; /* (sighing) "Often!" */
  161. }
  162. int is_orphaned_pgrp(int pgrp)
  163. {
  164. int retval;
  165. read_lock(&tasklist_lock);
  166. retval = will_become_orphaned_pgrp(pgrp, NULL);
  167. read_unlock(&tasklist_lock);
  168. return retval;
  169. }
  170. static inline int has_stopped_jobs(int pgrp)
  171. {
  172. int retval = 0;
  173. struct task_struct *p;
  174. do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
  175. if (p->state != TASK_STOPPED)
  176. continue;
  177. /* If p is stopped by a debugger on a signal that won't
  178. stop it, then don't count p as stopped. This isn't
  179. perfect but it's a good approximation. */
  180. if (unlikely (p->ptrace)
  181. && p->exit_code != SIGSTOP
  182. && p->exit_code != SIGTSTP
  183. && p->exit_code != SIGTTOU
  184. && p->exit_code != SIGTTIN)
  185. continue;
  186. retval = 1;
  187. break;
  188. } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
  189. return retval;
  190. }
  191. /**
  192. * reparent_to_init - Reparent the calling kernel thread to the init task.
  193. *
  194. * If a kernel thread is launched as a result of a system call, or if
  195. * it ever exits, it should generally reparent itself to init so that
  196. * it is correctly cleaned up on exit.
  197. *
  198. * The various task state such as scheduling policy and priority may have
  199. * been inherited from a user process, so we reset them to sane values here.
  200. *
  201. * NOTE that reparent_to_init() gives the caller full capabilities.
  202. */
  203. static inline void reparent_to_init(void)
  204. {
  205. write_lock_irq(&tasklist_lock);
  206. ptrace_unlink(current);
  207. /* Reparent to init */
  208. REMOVE_LINKS(current);
  209. current->parent = child_reaper;
  210. current->real_parent = child_reaper;
  211. SET_LINKS(current);
  212. /* Set the exit signal to SIGCHLD so we signal init on exit */
  213. current->exit_signal = SIGCHLD;
  214. if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
  215. set_user_nice(current, 0);
  216. /* cpus_allowed? */
  217. /* rt_priority? */
  218. /* signals? */
  219. security_task_reparent_to_init(current);
  220. memcpy(current->signal->rlim, init_task.signal->rlim,
  221. sizeof(current->signal->rlim));
  222. atomic_inc(&(INIT_USER->__count));
  223. write_unlock_irq(&tasklist_lock);
  224. switch_uid(INIT_USER);
  225. }
  226. void __set_special_pids(pid_t session, pid_t pgrp)
  227. {
  228. struct task_struct *curr = current;
  229. if (curr->signal->session != session) {
  230. detach_pid(curr, PIDTYPE_SID);
  231. curr->signal->session = session;
  232. attach_pid(curr, PIDTYPE_SID, session);
  233. }
  234. if (process_group(curr) != pgrp) {
  235. detach_pid(curr, PIDTYPE_PGID);
  236. curr->signal->pgrp = pgrp;
  237. attach_pid(curr, PIDTYPE_PGID, pgrp);
  238. }
  239. }
  240. void set_special_pids(pid_t session, pid_t pgrp)
  241. {
  242. write_lock_irq(&tasklist_lock);
  243. __set_special_pids(session, pgrp);
  244. write_unlock_irq(&tasklist_lock);
  245. }
  246. /*
  247. * Let kernel threads use this to say that they
  248. * allow a certain signal (since daemonize() will
  249. * have disabled all of them by default).
  250. */
  251. int allow_signal(int sig)
  252. {
  253. if (!valid_signal(sig) || sig < 1)
  254. return -EINVAL;
  255. spin_lock_irq(&current->sighand->siglock);
  256. sigdelset(&current->blocked, sig);
  257. if (!current->mm) {
  258. /* Kernel threads handle their own signals.
  259. Let the signal code know it'll be handled, so
  260. that they don't get converted to SIGKILL or
  261. just silently dropped */
  262. current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
  263. }
  264. recalc_sigpending();
  265. spin_unlock_irq(&current->sighand->siglock);
  266. return 0;
  267. }
  268. EXPORT_SYMBOL(allow_signal);
  269. int disallow_signal(int sig)
  270. {
  271. if (!valid_signal(sig) || sig < 1)
  272. return -EINVAL;
  273. spin_lock_irq(&current->sighand->siglock);
  274. sigaddset(&current->blocked, sig);
  275. recalc_sigpending();
  276. spin_unlock_irq(&current->sighand->siglock);
  277. return 0;
  278. }
  279. EXPORT_SYMBOL(disallow_signal);
  280. /*
  281. * Put all the gunge required to become a kernel thread without
  282. * attached user resources in one place where it belongs.
  283. */
  284. void daemonize(const char *name, ...)
  285. {
  286. va_list args;
  287. struct fs_struct *fs;
  288. sigset_t blocked;
  289. va_start(args, name);
  290. vsnprintf(current->comm, sizeof(current->comm), name, args);
  291. va_end(args);
  292. /*
  293. * If we were started as result of loading a module, close all of the
  294. * user space pages. We don't need them, and if we didn't close them
  295. * they would be locked into memory.
  296. */
  297. exit_mm(current);
  298. set_special_pids(1, 1);
  299. down(&tty_sem);
  300. current->signal->tty = NULL;
  301. up(&tty_sem);
  302. /* Block and flush all signals */
  303. sigfillset(&blocked);
  304. sigprocmask(SIG_BLOCK, &blocked, NULL);
  305. flush_signals(current);
  306. /* Become as one with the init task */
  307. exit_fs(current); /* current->fs->count--; */
  308. fs = init_task.fs;
  309. current->fs = fs;
  310. atomic_inc(&fs->count);
  311. exit_files(current);
  312. current->files = init_task.files;
  313. atomic_inc(&current->files->count);
  314. reparent_to_init();
  315. }
  316. EXPORT_SYMBOL(daemonize);
  317. static inline void close_files(struct files_struct * files)
  318. {
  319. int i, j;
  320. struct fdtable *fdt;
  321. j = 0;
  322. fdt = files_fdtable(files);
  323. for (;;) {
  324. unsigned long set;
  325. i = j * __NFDBITS;
  326. if (i >= fdt->max_fdset || i >= fdt->max_fds)
  327. break;
  328. set = fdt->open_fds->fds_bits[j++];
  329. while (set) {
  330. if (set & 1) {
  331. struct file * file = xchg(&fdt->fd[i], NULL);
  332. if (file)
  333. filp_close(file, files);
  334. }
  335. i++;
  336. set >>= 1;
  337. }
  338. }
  339. }
  340. struct files_struct *get_files_struct(struct task_struct *task)
  341. {
  342. struct files_struct *files;
  343. task_lock(task);
  344. files = task->files;
  345. if (files)
  346. atomic_inc(&files->count);
  347. task_unlock(task);
  348. return files;
  349. }
  350. void fastcall put_files_struct(struct files_struct *files)
  351. {
  352. struct fdtable *fdt;
  353. if (atomic_dec_and_test(&files->count)) {
  354. close_files(files);
  355. /*
  356. * Free the fd and fdset arrays if we expanded them.
  357. */
  358. fdt = files_fdtable(files);
  359. if (fdt->fd != &files->fd_array[0])
  360. free_fd_array(fdt->fd, fdt->max_fds);
  361. if (fdt->max_fdset > __FD_SETSIZE) {
  362. free_fdset(fdt->open_fds, fdt->max_fdset);
  363. free_fdset(fdt->close_on_exec, fdt->max_fdset);
  364. }
  365. kmem_cache_free(files_cachep, files);
  366. }
  367. }
  368. EXPORT_SYMBOL(put_files_struct);
  369. static inline void __exit_files(struct task_struct *tsk)
  370. {
  371. struct files_struct * files = tsk->files;
  372. if (files) {
  373. task_lock(tsk);
  374. tsk->files = NULL;
  375. task_unlock(tsk);
  376. put_files_struct(files);
  377. }
  378. }
  379. void exit_files(struct task_struct *tsk)
  380. {
  381. __exit_files(tsk);
  382. }
  383. static inline void __put_fs_struct(struct fs_struct *fs)
  384. {
  385. /* No need to hold fs->lock if we are killing it */
  386. if (atomic_dec_and_test(&fs->count)) {
  387. dput(fs->root);
  388. mntput(fs->rootmnt);
  389. dput(fs->pwd);
  390. mntput(fs->pwdmnt);
  391. if (fs->altroot) {
  392. dput(fs->altroot);
  393. mntput(fs->altrootmnt);
  394. }
  395. kmem_cache_free(fs_cachep, fs);
  396. }
  397. }
  398. void put_fs_struct(struct fs_struct *fs)
  399. {
  400. __put_fs_struct(fs);
  401. }
  402. static inline void __exit_fs(struct task_struct *tsk)
  403. {
  404. struct fs_struct * fs = tsk->fs;
  405. if (fs) {
  406. task_lock(tsk);
  407. tsk->fs = NULL;
  408. task_unlock(tsk);
  409. __put_fs_struct(fs);
  410. }
  411. }
  412. void exit_fs(struct task_struct *tsk)
  413. {
  414. __exit_fs(tsk);
  415. }
  416. EXPORT_SYMBOL_GPL(exit_fs);
  417. /*
  418. * Turn us into a lazy TLB process if we
  419. * aren't already..
  420. */
  421. static void exit_mm(struct task_struct * tsk)
  422. {
  423. struct mm_struct *mm = tsk->mm;
  424. mm_release(tsk, mm);
  425. if (!mm)
  426. return;
  427. /*
  428. * Serialize with any possible pending coredump.
  429. * We must hold mmap_sem around checking core_waiters
  430. * and clearing tsk->mm. The core-inducing thread
  431. * will increment core_waiters for each thread in the
  432. * group with ->mm != NULL.
  433. */
  434. down_read(&mm->mmap_sem);
  435. if (mm->core_waiters) {
  436. up_read(&mm->mmap_sem);
  437. down_write(&mm->mmap_sem);
  438. if (!--mm->core_waiters)
  439. complete(mm->core_startup_done);
  440. up_write(&mm->mmap_sem);
  441. wait_for_completion(&mm->core_done);
  442. down_read(&mm->mmap_sem);
  443. }
  444. atomic_inc(&mm->mm_count);
  445. if (mm != tsk->active_mm) BUG();
  446. /* more a memory barrier than a real lock */
  447. task_lock(tsk);
  448. tsk->mm = NULL;
  449. up_read(&mm->mmap_sem);
  450. enter_lazy_tlb(mm, current);
  451. task_unlock(tsk);
  452. mmput(mm);
  453. }
  454. static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
  455. {
  456. /*
  457. * Make sure we're not reparenting to ourselves and that
  458. * the parent is not a zombie.
  459. */
  460. BUG_ON(p == reaper || reaper->exit_state >= EXIT_ZOMBIE);
  461. p->real_parent = reaper;
  462. }
  463. static inline void reparent_thread(task_t *p, task_t *father, int traced)
  464. {
  465. /* We don't want people slaying init. */
  466. if (p->exit_signal != -1)
  467. p->exit_signal = SIGCHLD;
  468. if (p->pdeath_signal)
  469. /* We already hold the tasklist_lock here. */
  470. group_send_sig_info(p->pdeath_signal, (void *) 0, p);
  471. /* Move the child from its dying parent to the new one. */
  472. if (unlikely(traced)) {
  473. /* Preserve ptrace links if someone else is tracing this child. */
  474. list_del_init(&p->ptrace_list);
  475. if (p->parent != p->real_parent)
  476. list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
  477. } else {
  478. /* If this child is being traced, then we're the one tracing it
  479. * anyway, so let go of it.
  480. */
  481. p->ptrace = 0;
  482. list_del_init(&p->sibling);
  483. p->parent = p->real_parent;
  484. list_add_tail(&p->sibling, &p->parent->children);
  485. /* If we'd notified the old parent about this child's death,
  486. * also notify the new parent.
  487. */
  488. if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
  489. thread_group_empty(p))
  490. do_notify_parent(p, p->exit_signal);
  491. else if (p->state == TASK_TRACED) {
  492. /*
  493. * If it was at a trace stop, turn it into
  494. * a normal stop since it's no longer being
  495. * traced.
  496. */
  497. ptrace_untrace(p);
  498. }
  499. }
  500. /*
  501. * process group orphan check
  502. * Case ii: Our child is in a different pgrp
  503. * than we are, and it was the only connection
  504. * outside, so the child pgrp is now orphaned.
  505. */
  506. if ((process_group(p) != process_group(father)) &&
  507. (p->signal->session == father->signal->session)) {
  508. int pgrp = process_group(p);
  509. if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
  510. __kill_pg_info(SIGHUP, (void *)1, pgrp);
  511. __kill_pg_info(SIGCONT, (void *)1, pgrp);
  512. }
  513. }
  514. }
  515. /*
  516. * When we die, we re-parent all our children.
  517. * Try to give them to another thread in our thread
  518. * group, and if no such member exists, give it to
  519. * the global child reaper process (ie "init")
  520. */
  521. static inline void forget_original_parent(struct task_struct * father,
  522. struct list_head *to_release)
  523. {
  524. struct task_struct *p, *reaper = father;
  525. struct list_head *_p, *_n;
  526. do {
  527. reaper = next_thread(reaper);
  528. if (reaper == father) {
  529. reaper = child_reaper;
  530. break;
  531. }
  532. } while (reaper->exit_state);
  533. /*
  534. * There are only two places where our children can be:
  535. *
  536. * - in our child list
  537. * - in our ptraced child list
  538. *
  539. * Search them and reparent children.
  540. */
  541. list_for_each_safe(_p, _n, &father->children) {
  542. int ptrace;
  543. p = list_entry(_p,struct task_struct,sibling);
  544. ptrace = p->ptrace;
  545. /* if father isn't the real parent, then ptrace must be enabled */
  546. BUG_ON(father != p->real_parent && !ptrace);
  547. if (father == p->real_parent) {
  548. /* reparent with a reaper, real father it's us */
  549. choose_new_parent(p, reaper, child_reaper);
  550. reparent_thread(p, father, 0);
  551. } else {
  552. /* reparent ptraced task to its real parent */
  553. __ptrace_unlink (p);
  554. if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
  555. thread_group_empty(p))
  556. do_notify_parent(p, p->exit_signal);
  557. }
  558. /*
  559. * if the ptraced child is a zombie with exit_signal == -1
  560. * we must collect it before we exit, or it will remain
  561. * zombie forever since we prevented it from self-reap itself
  562. * while it was being traced by us, to be able to see it in wait4.
  563. */
  564. if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
  565. list_add(&p->ptrace_list, to_release);
  566. }
  567. list_for_each_safe(_p, _n, &father->ptrace_children) {
  568. p = list_entry(_p,struct task_struct,ptrace_list);
  569. choose_new_parent(p, reaper, child_reaper);
  570. reparent_thread(p, father, 1);
  571. }
  572. }
  573. /*
  574. * Send signals to all our closest relatives so that they know
  575. * to properly mourn us..
  576. */
  577. static void exit_notify(struct task_struct *tsk)
  578. {
  579. int state;
  580. struct task_struct *t;
  581. struct list_head ptrace_dead, *_p, *_n;
  582. if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
  583. && !thread_group_empty(tsk)) {
  584. /*
  585. * This occurs when there was a race between our exit
  586. * syscall and a group signal choosing us as the one to
  587. * wake up. It could be that we are the only thread
  588. * alerted to check for pending signals, but another thread
  589. * should be woken now to take the signal since we will not.
  590. * Now we'll wake all the threads in the group just to make
  591. * sure someone gets all the pending signals.
  592. */
  593. read_lock(&tasklist_lock);
  594. spin_lock_irq(&tsk->sighand->siglock);
  595. for (t = next_thread(tsk); t != tsk; t = next_thread(t))
  596. if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
  597. recalc_sigpending_tsk(t);
  598. if (signal_pending(t))
  599. signal_wake_up(t, 0);
  600. }
  601. spin_unlock_irq(&tsk->sighand->siglock);
  602. read_unlock(&tasklist_lock);
  603. }
  604. write_lock_irq(&tasklist_lock);
  605. /*
  606. * This does two things:
  607. *
  608. * A. Make init inherit all the child processes
  609. * B. Check to see if any process groups have become orphaned
  610. * as a result of our exiting, and if they have any stopped
  611. * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  612. */
  613. INIT_LIST_HEAD(&ptrace_dead);
  614. forget_original_parent(tsk, &ptrace_dead);
  615. BUG_ON(!list_empty(&tsk->children));
  616. BUG_ON(!list_empty(&tsk->ptrace_children));
  617. /*
  618. * Check to see if any process groups have become orphaned
  619. * as a result of our exiting, and if they have any stopped
  620. * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  621. *
  622. * Case i: Our father is in a different pgrp than we are
  623. * and we were the only connection outside, so our pgrp
  624. * is about to become orphaned.
  625. */
  626. t = tsk->real_parent;
  627. if ((process_group(t) != process_group(tsk)) &&
  628. (t->signal->session == tsk->signal->session) &&
  629. will_become_orphaned_pgrp(process_group(tsk), tsk) &&
  630. has_stopped_jobs(process_group(tsk))) {
  631. __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
  632. __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
  633. }
  634. /* Let father know we died
  635. *
  636. * Thread signals are configurable, but you aren't going to use
  637. * that to send signals to arbitary processes.
  638. * That stops right now.
  639. *
  640. * If the parent exec id doesn't match the exec id we saved
  641. * when we started then we know the parent has changed security
  642. * domain.
  643. *
  644. * If our self_exec id doesn't match our parent_exec_id then
  645. * we have changed execution domain as these two values started
  646. * the same after a fork.
  647. *
  648. */
  649. if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
  650. ( tsk->parent_exec_id != t->self_exec_id ||
  651. tsk->self_exec_id != tsk->parent_exec_id)
  652. && !capable(CAP_KILL))
  653. tsk->exit_signal = SIGCHLD;
  654. /* If something other than our normal parent is ptracing us, then
  655. * send it a SIGCHLD instead of honoring exit_signal. exit_signal
  656. * only has special meaning to our real parent.
  657. */
  658. if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
  659. int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
  660. do_notify_parent(tsk, signal);
  661. } else if (tsk->ptrace) {
  662. do_notify_parent(tsk, SIGCHLD);
  663. }
  664. state = EXIT_ZOMBIE;
  665. if (tsk->exit_signal == -1 &&
  666. (likely(tsk->ptrace == 0) ||
  667. unlikely(tsk->parent->signal->flags & SIGNAL_GROUP_EXIT)))
  668. state = EXIT_DEAD;
  669. tsk->exit_state = state;
  670. write_unlock_irq(&tasklist_lock);
  671. list_for_each_safe(_p, _n, &ptrace_dead) {
  672. list_del_init(_p);
  673. t = list_entry(_p,struct task_struct,ptrace_list);
  674. release_task(t);
  675. }
  676. /* If the process is dead, release it - nobody will wait for it */
  677. if (state == EXIT_DEAD)
  678. release_task(tsk);
  679. /* PF_DEAD causes final put_task_struct after we schedule. */
  680. preempt_disable();
  681. tsk->flags |= PF_DEAD;
  682. }
  683. fastcall NORET_TYPE void do_exit(long code)
  684. {
  685. struct task_struct *tsk = current;
  686. int group_dead;
  687. profile_task_exit(tsk);
  688. WARN_ON(atomic_read(&tsk->fs_excl));
  689. if (unlikely(in_interrupt()))
  690. panic("Aiee, killing interrupt handler!");
  691. if (unlikely(!tsk->pid))
  692. panic("Attempted to kill the idle task!");
  693. if (unlikely(tsk->pid == 1))
  694. panic("Attempted to kill init!");
  695. if (tsk->io_context)
  696. exit_io_context();
  697. if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
  698. current->ptrace_message = code;
  699. ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
  700. }
  701. /*
  702. * We're taking recursive faults here in do_exit. Safest is to just
  703. * leave this task alone and wait for reboot.
  704. */
  705. if (unlikely(tsk->flags & PF_EXITING)) {
  706. printk(KERN_ALERT
  707. "Fixing recursive fault but reboot is needed!\n");
  708. set_current_state(TASK_UNINTERRUPTIBLE);
  709. schedule();
  710. }
  711. tsk->flags |= PF_EXITING;
  712. /*
  713. * Make sure we don't try to process any timer firings
  714. * while we are already exiting.
  715. */
  716. tsk->it_virt_expires = cputime_zero;
  717. tsk->it_prof_expires = cputime_zero;
  718. tsk->it_sched_expires = 0;
  719. if (unlikely(in_atomic()))
  720. printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
  721. current->comm, current->pid,
  722. preempt_count());
  723. acct_update_integrals(tsk);
  724. update_mem_hiwater(tsk);
  725. group_dead = atomic_dec_and_test(&tsk->signal->live);
  726. if (group_dead) {
  727. del_timer_sync(&tsk->signal->real_timer);
  728. acct_process(code);
  729. }
  730. exit_mm(tsk);
  731. exit_sem(tsk);
  732. __exit_files(tsk);
  733. __exit_fs(tsk);
  734. exit_namespace(tsk);
  735. exit_thread();
  736. cpuset_exit(tsk);
  737. exit_keys(tsk);
  738. if (group_dead && tsk->signal->leader)
  739. disassociate_ctty(1);
  740. module_put(tsk->thread_info->exec_domain->module);
  741. if (tsk->binfmt)
  742. module_put(tsk->binfmt->module);
  743. tsk->exit_code = code;
  744. exit_notify(tsk);
  745. #ifdef CONFIG_NUMA
  746. mpol_free(tsk->mempolicy);
  747. tsk->mempolicy = NULL;
  748. #endif
  749. BUG_ON(!(current->flags & PF_DEAD));
  750. schedule();
  751. BUG();
  752. /* Avoid "noreturn function does return". */
  753. for (;;) ;
  754. }
  755. EXPORT_SYMBOL_GPL(do_exit);
  756. NORET_TYPE void complete_and_exit(struct completion *comp, long code)
  757. {
  758. if (comp)
  759. complete(comp);
  760. do_exit(code);
  761. }
  762. EXPORT_SYMBOL(complete_and_exit);
  763. asmlinkage long sys_exit(int error_code)
  764. {
  765. do_exit((error_code&0xff)<<8);
  766. }
  767. task_t fastcall *next_thread(const task_t *p)
  768. {
  769. return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
  770. }
  771. EXPORT_SYMBOL(next_thread);
  772. /*
  773. * Take down every thread in the group. This is called by fatal signals
  774. * as well as by sys_exit_group (below).
  775. */
  776. NORET_TYPE void
  777. do_group_exit(int exit_code)
  778. {
  779. BUG_ON(exit_code & 0x80); /* core dumps don't get here */
  780. if (current->signal->flags & SIGNAL_GROUP_EXIT)
  781. exit_code = current->signal->group_exit_code;
  782. else if (!thread_group_empty(current)) {
  783. struct signal_struct *const sig = current->signal;
  784. struct sighand_struct *const sighand = current->sighand;
  785. read_lock(&tasklist_lock);
  786. spin_lock_irq(&sighand->siglock);
  787. if (sig->flags & SIGNAL_GROUP_EXIT)
  788. /* Another thread got here before we took the lock. */
  789. exit_code = sig->group_exit_code;
  790. else {
  791. sig->flags = SIGNAL_GROUP_EXIT;
  792. sig->group_exit_code = exit_code;
  793. zap_other_threads(current);
  794. }
  795. spin_unlock_irq(&sighand->siglock);
  796. read_unlock(&tasklist_lock);
  797. }
  798. do_exit(exit_code);
  799. /* NOTREACHED */
  800. }
  801. /*
  802. * this kills every thread in the thread group. Note that any externally
  803. * wait4()-ing process will get the correct exit code - even if this
  804. * thread is not the thread group leader.
  805. */
  806. asmlinkage void sys_exit_group(int error_code)
  807. {
  808. do_group_exit((error_code & 0xff) << 8);
  809. }
  810. static int eligible_child(pid_t pid, int options, task_t *p)
  811. {
  812. if (pid > 0) {
  813. if (p->pid != pid)
  814. return 0;
  815. } else if (!pid) {
  816. if (process_group(p) != process_group(current))
  817. return 0;
  818. } else if (pid != -1) {
  819. if (process_group(p) != -pid)
  820. return 0;
  821. }
  822. /*
  823. * Do not consider detached threads that are
  824. * not ptraced:
  825. */
  826. if (p->exit_signal == -1 && !p->ptrace)
  827. return 0;
  828. /* Wait for all children (clone and not) if __WALL is set;
  829. * otherwise, wait for clone children *only* if __WCLONE is
  830. * set; otherwise, wait for non-clone children *only*. (Note:
  831. * A "clone" child here is one that reports to its parent
  832. * using a signal other than SIGCHLD.) */
  833. if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
  834. && !(options & __WALL))
  835. return 0;
  836. /*
  837. * Do not consider thread group leaders that are
  838. * in a non-empty thread group:
  839. */
  840. if (current->tgid != p->tgid && delay_group_leader(p))
  841. return 2;
  842. if (security_task_wait(p))
  843. return 0;
  844. return 1;
  845. }
  846. static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
  847. int why, int status,
  848. struct siginfo __user *infop,
  849. struct rusage __user *rusagep)
  850. {
  851. int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
  852. put_task_struct(p);
  853. if (!retval)
  854. retval = put_user(SIGCHLD, &infop->si_signo);
  855. if (!retval)
  856. retval = put_user(0, &infop->si_errno);
  857. if (!retval)
  858. retval = put_user((short)why, &infop->si_code);
  859. if (!retval)
  860. retval = put_user(pid, &infop->si_pid);
  861. if (!retval)
  862. retval = put_user(uid, &infop->si_uid);
  863. if (!retval)
  864. retval = put_user(status, &infop->si_status);
  865. if (!retval)
  866. retval = pid;
  867. return retval;
  868. }
  869. /*
  870. * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
  871. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  872. * the lock and this task is uninteresting. If we return nonzero, we have
  873. * released the lock and the system call should return.
  874. */
  875. static int wait_task_zombie(task_t *p, int noreap,
  876. struct siginfo __user *infop,
  877. int __user *stat_addr, struct rusage __user *ru)
  878. {
  879. unsigned long state;
  880. int retval;
  881. int status;
  882. if (unlikely(noreap)) {
  883. pid_t pid = p->pid;
  884. uid_t uid = p->uid;
  885. int exit_code = p->exit_code;
  886. int why, status;
  887. if (unlikely(p->exit_state != EXIT_ZOMBIE))
  888. return 0;
  889. if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
  890. return 0;
  891. get_task_struct(p);
  892. read_unlock(&tasklist_lock);
  893. if ((exit_code & 0x7f) == 0) {
  894. why = CLD_EXITED;
  895. status = exit_code >> 8;
  896. } else {
  897. why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
  898. status = exit_code & 0x7f;
  899. }
  900. return wait_noreap_copyout(p, pid, uid, why,
  901. status, infop, ru);
  902. }
  903. /*
  904. * Try to move the task's state to DEAD
  905. * only one thread is allowed to do this:
  906. */
  907. state = xchg(&p->exit_state, EXIT_DEAD);
  908. if (state != EXIT_ZOMBIE) {
  909. BUG_ON(state != EXIT_DEAD);
  910. return 0;
  911. }
  912. if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
  913. /*
  914. * This can only happen in a race with a ptraced thread
  915. * dying on another processor.
  916. */
  917. return 0;
  918. }
  919. if (likely(p->real_parent == p->parent) && likely(p->signal)) {
  920. /*
  921. * The resource counters for the group leader are in its
  922. * own task_struct. Those for dead threads in the group
  923. * are in its signal_struct, as are those for the child
  924. * processes it has previously reaped. All these
  925. * accumulate in the parent's signal_struct c* fields.
  926. *
  927. * We don't bother to take a lock here to protect these
  928. * p->signal fields, because they are only touched by
  929. * __exit_signal, which runs with tasklist_lock
  930. * write-locked anyway, and so is excluded here. We do
  931. * need to protect the access to p->parent->signal fields,
  932. * as other threads in the parent group can be right
  933. * here reaping other children at the same time.
  934. */
  935. spin_lock_irq(&p->parent->sighand->siglock);
  936. p->parent->signal->cutime =
  937. cputime_add(p->parent->signal->cutime,
  938. cputime_add(p->utime,
  939. cputime_add(p->signal->utime,
  940. p->signal->cutime)));
  941. p->parent->signal->cstime =
  942. cputime_add(p->parent->signal->cstime,
  943. cputime_add(p->stime,
  944. cputime_add(p->signal->stime,
  945. p->signal->cstime)));
  946. p->parent->signal->cmin_flt +=
  947. p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
  948. p->parent->signal->cmaj_flt +=
  949. p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
  950. p->parent->signal->cnvcsw +=
  951. p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
  952. p->parent->signal->cnivcsw +=
  953. p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
  954. spin_unlock_irq(&p->parent->sighand->siglock);
  955. }
  956. /*
  957. * Now we are sure this task is interesting, and no other
  958. * thread can reap it because we set its state to EXIT_DEAD.
  959. */
  960. read_unlock(&tasklist_lock);
  961. retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
  962. status = (p->signal->flags & SIGNAL_GROUP_EXIT)
  963. ? p->signal->group_exit_code : p->exit_code;
  964. if (!retval && stat_addr)
  965. retval = put_user(status, stat_addr);
  966. if (!retval && infop)
  967. retval = put_user(SIGCHLD, &infop->si_signo);
  968. if (!retval && infop)
  969. retval = put_user(0, &infop->si_errno);
  970. if (!retval && infop) {
  971. int why;
  972. if ((status & 0x7f) == 0) {
  973. why = CLD_EXITED;
  974. status >>= 8;
  975. } else {
  976. why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
  977. status &= 0x7f;
  978. }
  979. retval = put_user((short)why, &infop->si_code);
  980. if (!retval)
  981. retval = put_user(status, &infop->si_status);
  982. }
  983. if (!retval && infop)
  984. retval = put_user(p->pid, &infop->si_pid);
  985. if (!retval && infop)
  986. retval = put_user(p->uid, &infop->si_uid);
  987. if (retval) {
  988. // TODO: is this safe?
  989. p->exit_state = EXIT_ZOMBIE;
  990. return retval;
  991. }
  992. retval = p->pid;
  993. if (p->real_parent != p->parent) {
  994. write_lock_irq(&tasklist_lock);
  995. /* Double-check with lock held. */
  996. if (p->real_parent != p->parent) {
  997. __ptrace_unlink(p);
  998. // TODO: is this safe?
  999. p->exit_state = EXIT_ZOMBIE;
  1000. /*
  1001. * If this is not a detached task, notify the parent.
  1002. * If it's still not detached after that, don't release
  1003. * it now.
  1004. */
  1005. if (p->exit_signal != -1) {
  1006. do_notify_parent(p, p->exit_signal);
  1007. if (p->exit_signal != -1)
  1008. p = NULL;
  1009. }
  1010. }
  1011. write_unlock_irq(&tasklist_lock);
  1012. }
  1013. if (p != NULL)
  1014. release_task(p);
  1015. BUG_ON(!retval);
  1016. return retval;
  1017. }
  1018. /*
  1019. * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
  1020. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1021. * the lock and this task is uninteresting. If we return nonzero, we have
  1022. * released the lock and the system call should return.
  1023. */
  1024. static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
  1025. struct siginfo __user *infop,
  1026. int __user *stat_addr, struct rusage __user *ru)
  1027. {
  1028. int retval, exit_code;
  1029. if (!p->exit_code)
  1030. return 0;
  1031. if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
  1032. p->signal && p->signal->group_stop_count > 0)
  1033. /*
  1034. * A group stop is in progress and this is the group leader.
  1035. * We won't report until all threads have stopped.
  1036. */
  1037. return 0;
  1038. /*
  1039. * Now we are pretty sure this task is interesting.
  1040. * Make sure it doesn't get reaped out from under us while we
  1041. * give up the lock and then examine it below. We don't want to
  1042. * keep holding onto the tasklist_lock while we call getrusage and
  1043. * possibly take page faults for user memory.
  1044. */
  1045. get_task_struct(p);
  1046. read_unlock(&tasklist_lock);
  1047. if (unlikely(noreap)) {
  1048. pid_t pid = p->pid;
  1049. uid_t uid = p->uid;
  1050. int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
  1051. exit_code = p->exit_code;
  1052. if (unlikely(!exit_code) ||
  1053. unlikely(p->state > TASK_STOPPED))
  1054. goto bail_ref;
  1055. return wait_noreap_copyout(p, pid, uid,
  1056. why, (exit_code << 8) | 0x7f,
  1057. infop, ru);
  1058. }
  1059. write_lock_irq(&tasklist_lock);
  1060. /*
  1061. * This uses xchg to be atomic with the thread resuming and setting
  1062. * it. It must also be done with the write lock held to prevent a
  1063. * race with the EXIT_ZOMBIE case.
  1064. */
  1065. exit_code = xchg(&p->exit_code, 0);
  1066. if (unlikely(p->exit_state)) {
  1067. /*
  1068. * The task resumed and then died. Let the next iteration
  1069. * catch it in EXIT_ZOMBIE. Note that exit_code might
  1070. * already be zero here if it resumed and did _exit(0).
  1071. * The task itself is dead and won't touch exit_code again;
  1072. * other processors in this function are locked out.
  1073. */
  1074. p->exit_code = exit_code;
  1075. exit_code = 0;
  1076. }
  1077. if (unlikely(exit_code == 0)) {
  1078. /*
  1079. * Another thread in this function got to it first, or it
  1080. * resumed, or it resumed and then died.
  1081. */
  1082. write_unlock_irq(&tasklist_lock);
  1083. bail_ref:
  1084. put_task_struct(p);
  1085. /*
  1086. * We are returning to the wait loop without having successfully
  1087. * removed the process and having released the lock. We cannot
  1088. * continue, since the "p" task pointer is potentially stale.
  1089. *
  1090. * Return -EAGAIN, and do_wait() will restart the loop from the
  1091. * beginning. Do _not_ re-acquire the lock.
  1092. */
  1093. return -EAGAIN;
  1094. }
  1095. /* move to end of parent's list to avoid starvation */
  1096. remove_parent(p);
  1097. add_parent(p, p->parent);
  1098. write_unlock_irq(&tasklist_lock);
  1099. retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
  1100. if (!retval && stat_addr)
  1101. retval = put_user((exit_code << 8) | 0x7f, stat_addr);
  1102. if (!retval && infop)
  1103. retval = put_user(SIGCHLD, &infop->si_signo);
  1104. if (!retval && infop)
  1105. retval = put_user(0, &infop->si_errno);
  1106. if (!retval && infop)
  1107. retval = put_user((short)((p->ptrace & PT_PTRACED)
  1108. ? CLD_TRAPPED : CLD_STOPPED),
  1109. &infop->si_code);
  1110. if (!retval && infop)
  1111. retval = put_user(exit_code, &infop->si_status);
  1112. if (!retval && infop)
  1113. retval = put_user(p->pid, &infop->si_pid);
  1114. if (!retval && infop)
  1115. retval = put_user(p->uid, &infop->si_uid);
  1116. if (!retval)
  1117. retval = p->pid;
  1118. put_task_struct(p);
  1119. BUG_ON(!retval);
  1120. return retval;
  1121. }
  1122. /*
  1123. * Handle do_wait work for one task in a live, non-stopped state.
  1124. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1125. * the lock and this task is uninteresting. If we return nonzero, we have
  1126. * released the lock and the system call should return.
  1127. */
  1128. static int wait_task_continued(task_t *p, int noreap,
  1129. struct siginfo __user *infop,
  1130. int __user *stat_addr, struct rusage __user *ru)
  1131. {
  1132. int retval;
  1133. pid_t pid;
  1134. uid_t uid;
  1135. if (unlikely(!p->signal))
  1136. return 0;
  1137. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
  1138. return 0;
  1139. spin_lock_irq(&p->sighand->siglock);
  1140. /* Re-check with the lock held. */
  1141. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
  1142. spin_unlock_irq(&p->sighand->siglock);
  1143. return 0;
  1144. }
  1145. if (!noreap)
  1146. p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
  1147. spin_unlock_irq(&p->sighand->siglock);
  1148. pid = p->pid;
  1149. uid = p->uid;
  1150. get_task_struct(p);
  1151. read_unlock(&tasklist_lock);
  1152. if (!infop) {
  1153. retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
  1154. put_task_struct(p);
  1155. if (!retval && stat_addr)
  1156. retval = put_user(0xffff, stat_addr);
  1157. if (!retval)
  1158. retval = p->pid;
  1159. } else {
  1160. retval = wait_noreap_copyout(p, pid, uid,
  1161. CLD_CONTINUED, SIGCONT,
  1162. infop, ru);
  1163. BUG_ON(retval == 0);
  1164. }
  1165. return retval;
  1166. }
  1167. static inline int my_ptrace_child(struct task_struct *p)
  1168. {
  1169. if (!(p->ptrace & PT_PTRACED))
  1170. return 0;
  1171. if (!(p->ptrace & PT_ATTACHED))
  1172. return 1;
  1173. /*
  1174. * This child was PTRACE_ATTACH'd. We should be seeing it only if
  1175. * we are the attacher. If we are the real parent, this is a race
  1176. * inside ptrace_attach. It is waiting for the tasklist_lock,
  1177. * which we have to switch the parent links, but has already set
  1178. * the flags in p->ptrace.
  1179. */
  1180. return (p->parent != p->real_parent);
  1181. }
  1182. static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
  1183. int __user *stat_addr, struct rusage __user *ru)
  1184. {
  1185. DECLARE_WAITQUEUE(wait, current);
  1186. struct task_struct *tsk;
  1187. int flag, retval;
  1188. add_wait_queue(&current->signal->wait_chldexit,&wait);
  1189. repeat:
  1190. /*
  1191. * We will set this flag if we see any child that might later
  1192. * match our criteria, even if we are not able to reap it yet.
  1193. */
  1194. flag = 0;
  1195. current->state = TASK_INTERRUPTIBLE;
  1196. read_lock(&tasklist_lock);
  1197. tsk = current;
  1198. do {
  1199. struct task_struct *p;
  1200. struct list_head *_p;
  1201. int ret;
  1202. list_for_each(_p,&tsk->children) {
  1203. p = list_entry(_p,struct task_struct,sibling);
  1204. ret = eligible_child(pid, options, p);
  1205. if (!ret)
  1206. continue;
  1207. switch (p->state) {
  1208. case TASK_TRACED:
  1209. if (!my_ptrace_child(p))
  1210. continue;
  1211. /*FALLTHROUGH*/
  1212. case TASK_STOPPED:
  1213. /*
  1214. * It's stopped now, so it might later
  1215. * continue, exit, or stop again.
  1216. */
  1217. flag = 1;
  1218. if (!(options & WUNTRACED) &&
  1219. !my_ptrace_child(p))
  1220. continue;
  1221. retval = wait_task_stopped(p, ret == 2,
  1222. (options & WNOWAIT),
  1223. infop,
  1224. stat_addr, ru);
  1225. if (retval == -EAGAIN)
  1226. goto repeat;
  1227. if (retval != 0) /* He released the lock. */
  1228. goto end;
  1229. break;
  1230. default:
  1231. // case EXIT_DEAD:
  1232. if (p->exit_state == EXIT_DEAD)
  1233. continue;
  1234. // case EXIT_ZOMBIE:
  1235. if (p->exit_state == EXIT_ZOMBIE) {
  1236. /*
  1237. * Eligible but we cannot release
  1238. * it yet:
  1239. */
  1240. if (ret == 2)
  1241. goto check_continued;
  1242. if (!likely(options & WEXITED))
  1243. continue;
  1244. retval = wait_task_zombie(
  1245. p, (options & WNOWAIT),
  1246. infop, stat_addr, ru);
  1247. /* He released the lock. */
  1248. if (retval != 0)
  1249. goto end;
  1250. break;
  1251. }
  1252. check_continued:
  1253. /*
  1254. * It's running now, so it might later
  1255. * exit, stop, or stop and then continue.
  1256. */
  1257. flag = 1;
  1258. if (!unlikely(options & WCONTINUED))
  1259. continue;
  1260. retval = wait_task_continued(
  1261. p, (options & WNOWAIT),
  1262. infop, stat_addr, ru);
  1263. if (retval != 0) /* He released the lock. */
  1264. goto end;
  1265. break;
  1266. }
  1267. }
  1268. if (!flag) {
  1269. list_for_each(_p, &tsk->ptrace_children) {
  1270. p = list_entry(_p, struct task_struct,
  1271. ptrace_list);
  1272. if (!eligible_child(pid, options, p))
  1273. continue;
  1274. flag = 1;
  1275. break;
  1276. }
  1277. }
  1278. if (options & __WNOTHREAD)
  1279. break;
  1280. tsk = next_thread(tsk);
  1281. if (tsk->signal != current->signal)
  1282. BUG();
  1283. } while (tsk != current);
  1284. read_unlock(&tasklist_lock);
  1285. if (flag) {
  1286. retval = 0;
  1287. if (options & WNOHANG)
  1288. goto end;
  1289. retval = -ERESTARTSYS;
  1290. if (signal_pending(current))
  1291. goto end;
  1292. schedule();
  1293. goto repeat;
  1294. }
  1295. retval = -ECHILD;
  1296. end:
  1297. current->state = TASK_RUNNING;
  1298. remove_wait_queue(&current->signal->wait_chldexit,&wait);
  1299. if (infop) {
  1300. if (retval > 0)
  1301. retval = 0;
  1302. else {
  1303. /*
  1304. * For a WNOHANG return, clear out all the fields
  1305. * we would set so the user can easily tell the
  1306. * difference.
  1307. */
  1308. if (!retval)
  1309. retval = put_user(0, &infop->si_signo);
  1310. if (!retval)
  1311. retval = put_user(0, &infop->si_errno);
  1312. if (!retval)
  1313. retval = put_user(0, &infop->si_code);
  1314. if (!retval)
  1315. retval = put_user(0, &infop->si_pid);
  1316. if (!retval)
  1317. retval = put_user(0, &infop->si_uid);
  1318. if (!retval)
  1319. retval = put_user(0, &infop->si_status);
  1320. }
  1321. }
  1322. return retval;
  1323. }
  1324. asmlinkage long sys_waitid(int which, pid_t pid,
  1325. struct siginfo __user *infop, int options,
  1326. struct rusage __user *ru)
  1327. {
  1328. long ret;
  1329. if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
  1330. return -EINVAL;
  1331. if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
  1332. return -EINVAL;
  1333. switch (which) {
  1334. case P_ALL:
  1335. pid = -1;
  1336. break;
  1337. case P_PID:
  1338. if (pid <= 0)
  1339. return -EINVAL;
  1340. break;
  1341. case P_PGID:
  1342. if (pid <= 0)
  1343. return -EINVAL;
  1344. pid = -pid;
  1345. break;
  1346. default:
  1347. return -EINVAL;
  1348. }
  1349. ret = do_wait(pid, options, infop, NULL, ru);
  1350. /* avoid REGPARM breakage on x86: */
  1351. prevent_tail_call(ret);
  1352. return ret;
  1353. }
  1354. asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
  1355. int options, struct rusage __user *ru)
  1356. {
  1357. long ret;
  1358. if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
  1359. __WNOTHREAD|__WCLONE|__WALL))
  1360. return -EINVAL;
  1361. ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
  1362. /* avoid REGPARM breakage on x86: */
  1363. prevent_tail_call(ret);
  1364. return ret;
  1365. }
  1366. #ifdef __ARCH_WANT_SYS_WAITPID
  1367. /*
  1368. * sys_waitpid() remains for compatibility. waitpid() should be
  1369. * implemented by calling sys_wait4() from libc.a.
  1370. */
  1371. asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
  1372. {
  1373. return sys_wait4(pid, stat_addr, options, NULL);
  1374. }
  1375. #endif