exit.c 38 KB

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