exit.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  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. j = 0;
  321. for (;;) {
  322. unsigned long set;
  323. i = j * __NFDBITS;
  324. if (i >= files->max_fdset || i >= files->max_fds)
  325. break;
  326. set = files->open_fds->fds_bits[j++];
  327. while (set) {
  328. if (set & 1) {
  329. struct file * file = xchg(&files->fd[i], NULL);
  330. if (file)
  331. filp_close(file, files);
  332. }
  333. i++;
  334. set >>= 1;
  335. }
  336. }
  337. }
  338. struct files_struct *get_files_struct(struct task_struct *task)
  339. {
  340. struct files_struct *files;
  341. task_lock(task);
  342. files = task->files;
  343. if (files)
  344. atomic_inc(&files->count);
  345. task_unlock(task);
  346. return files;
  347. }
  348. void fastcall put_files_struct(struct files_struct *files)
  349. {
  350. if (atomic_dec_and_test(&files->count)) {
  351. close_files(files);
  352. /*
  353. * Free the fd and fdset arrays if we expanded them.
  354. */
  355. if (files->fd != &files->fd_array[0])
  356. free_fd_array(files->fd, files->max_fds);
  357. if (files->max_fdset > __FD_SETSIZE) {
  358. free_fdset(files->open_fds, files->max_fdset);
  359. free_fdset(files->close_on_exec, files->max_fdset);
  360. }
  361. kmem_cache_free(files_cachep, files);
  362. }
  363. }
  364. EXPORT_SYMBOL(put_files_struct);
  365. static inline void __exit_files(struct task_struct *tsk)
  366. {
  367. struct files_struct * files = tsk->files;
  368. if (files) {
  369. task_lock(tsk);
  370. tsk->files = NULL;
  371. task_unlock(tsk);
  372. put_files_struct(files);
  373. }
  374. }
  375. void exit_files(struct task_struct *tsk)
  376. {
  377. __exit_files(tsk);
  378. }
  379. static inline void __put_fs_struct(struct fs_struct *fs)
  380. {
  381. /* No need to hold fs->lock if we are killing it */
  382. if (atomic_dec_and_test(&fs->count)) {
  383. dput(fs->root);
  384. mntput(fs->rootmnt);
  385. dput(fs->pwd);
  386. mntput(fs->pwdmnt);
  387. if (fs->altroot) {
  388. dput(fs->altroot);
  389. mntput(fs->altrootmnt);
  390. }
  391. kmem_cache_free(fs_cachep, fs);
  392. }
  393. }
  394. void put_fs_struct(struct fs_struct *fs)
  395. {
  396. __put_fs_struct(fs);
  397. }
  398. static inline void __exit_fs(struct task_struct *tsk)
  399. {
  400. struct fs_struct * fs = tsk->fs;
  401. if (fs) {
  402. task_lock(tsk);
  403. tsk->fs = NULL;
  404. task_unlock(tsk);
  405. __put_fs_struct(fs);
  406. }
  407. }
  408. void exit_fs(struct task_struct *tsk)
  409. {
  410. __exit_fs(tsk);
  411. }
  412. EXPORT_SYMBOL_GPL(exit_fs);
  413. /*
  414. * Turn us into a lazy TLB process if we
  415. * aren't already..
  416. */
  417. static void exit_mm(struct task_struct * tsk)
  418. {
  419. struct mm_struct *mm = tsk->mm;
  420. mm_release(tsk, mm);
  421. if (!mm)
  422. return;
  423. /*
  424. * Serialize with any possible pending coredump.
  425. * We must hold mmap_sem around checking core_waiters
  426. * and clearing tsk->mm. The core-inducing thread
  427. * will increment core_waiters for each thread in the
  428. * group with ->mm != NULL.
  429. */
  430. down_read(&mm->mmap_sem);
  431. if (mm->core_waiters) {
  432. up_read(&mm->mmap_sem);
  433. down_write(&mm->mmap_sem);
  434. if (!--mm->core_waiters)
  435. complete(mm->core_startup_done);
  436. up_write(&mm->mmap_sem);
  437. wait_for_completion(&mm->core_done);
  438. down_read(&mm->mmap_sem);
  439. }
  440. atomic_inc(&mm->mm_count);
  441. if (mm != tsk->active_mm) BUG();
  442. /* more a memory barrier than a real lock */
  443. task_lock(tsk);
  444. tsk->mm = NULL;
  445. up_read(&mm->mmap_sem);
  446. enter_lazy_tlb(mm, current);
  447. task_unlock(tsk);
  448. mmput(mm);
  449. }
  450. static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
  451. {
  452. /*
  453. * Make sure we're not reparenting to ourselves and that
  454. * the parent is not a zombie.
  455. */
  456. BUG_ON(p == reaper || reaper->exit_state >= EXIT_ZOMBIE);
  457. p->real_parent = reaper;
  458. }
  459. static inline void reparent_thread(task_t *p, task_t *father, int traced)
  460. {
  461. /* We don't want people slaying init. */
  462. if (p->exit_signal != -1)
  463. p->exit_signal = SIGCHLD;
  464. if (p->pdeath_signal)
  465. /* We already hold the tasklist_lock here. */
  466. group_send_sig_info(p->pdeath_signal, (void *) 0, p);
  467. /* Move the child from its dying parent to the new one. */
  468. if (unlikely(traced)) {
  469. /* Preserve ptrace links if someone else is tracing this child. */
  470. list_del_init(&p->ptrace_list);
  471. if (p->parent != p->real_parent)
  472. list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
  473. } else {
  474. /* If this child is being traced, then we're the one tracing it
  475. * anyway, so let go of it.
  476. */
  477. p->ptrace = 0;
  478. list_del_init(&p->sibling);
  479. p->parent = p->real_parent;
  480. list_add_tail(&p->sibling, &p->parent->children);
  481. /* If we'd notified the old parent about this child's death,
  482. * also notify the new parent.
  483. */
  484. if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
  485. thread_group_empty(p))
  486. do_notify_parent(p, p->exit_signal);
  487. else if (p->state == TASK_TRACED) {
  488. /*
  489. * If it was at a trace stop, turn it into
  490. * a normal stop since it's no longer being
  491. * traced.
  492. */
  493. ptrace_untrace(p);
  494. }
  495. }
  496. /*
  497. * process group orphan check
  498. * Case ii: Our child is in a different pgrp
  499. * than we are, and it was the only connection
  500. * outside, so the child pgrp is now orphaned.
  501. */
  502. if ((process_group(p) != process_group(father)) &&
  503. (p->signal->session == father->signal->session)) {
  504. int pgrp = process_group(p);
  505. if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
  506. __kill_pg_info(SIGHUP, (void *)1, pgrp);
  507. __kill_pg_info(SIGCONT, (void *)1, pgrp);
  508. }
  509. }
  510. }
  511. /*
  512. * When we die, we re-parent all our children.
  513. * Try to give them to another thread in our thread
  514. * group, and if no such member exists, give it to
  515. * the global child reaper process (ie "init")
  516. */
  517. static inline void forget_original_parent(struct task_struct * father,
  518. struct list_head *to_release)
  519. {
  520. struct task_struct *p, *reaper = father;
  521. struct list_head *_p, *_n;
  522. do {
  523. reaper = next_thread(reaper);
  524. if (reaper == father) {
  525. reaper = child_reaper;
  526. break;
  527. }
  528. } while (reaper->exit_state);
  529. /*
  530. * There are only two places where our children can be:
  531. *
  532. * - in our child list
  533. * - in our ptraced child list
  534. *
  535. * Search them and reparent children.
  536. */
  537. list_for_each_safe(_p, _n, &father->children) {
  538. int ptrace;
  539. p = list_entry(_p,struct task_struct,sibling);
  540. ptrace = p->ptrace;
  541. /* if father isn't the real parent, then ptrace must be enabled */
  542. BUG_ON(father != p->real_parent && !ptrace);
  543. if (father == p->real_parent) {
  544. /* reparent with a reaper, real father it's us */
  545. choose_new_parent(p, reaper, child_reaper);
  546. reparent_thread(p, father, 0);
  547. } else {
  548. /* reparent ptraced task to its real parent */
  549. __ptrace_unlink (p);
  550. if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
  551. thread_group_empty(p))
  552. do_notify_parent(p, p->exit_signal);
  553. }
  554. /*
  555. * if the ptraced child is a zombie with exit_signal == -1
  556. * we must collect it before we exit, or it will remain
  557. * zombie forever since we prevented it from self-reap itself
  558. * while it was being traced by us, to be able to see it in wait4.
  559. */
  560. if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
  561. list_add(&p->ptrace_list, to_release);
  562. }
  563. list_for_each_safe(_p, _n, &father->ptrace_children) {
  564. p = list_entry(_p,struct task_struct,ptrace_list);
  565. choose_new_parent(p, reaper, child_reaper);
  566. reparent_thread(p, father, 1);
  567. }
  568. }
  569. /*
  570. * Send signals to all our closest relatives so that they know
  571. * to properly mourn us..
  572. */
  573. static void exit_notify(struct task_struct *tsk)
  574. {
  575. int state;
  576. struct task_struct *t;
  577. struct list_head ptrace_dead, *_p, *_n;
  578. if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
  579. && !thread_group_empty(tsk)) {
  580. /*
  581. * This occurs when there was a race between our exit
  582. * syscall and a group signal choosing us as the one to
  583. * wake up. It could be that we are the only thread
  584. * alerted to check for pending signals, but another thread
  585. * should be woken now to take the signal since we will not.
  586. * Now we'll wake all the threads in the group just to make
  587. * sure someone gets all the pending signals.
  588. */
  589. read_lock(&tasklist_lock);
  590. spin_lock_irq(&tsk->sighand->siglock);
  591. for (t = next_thread(tsk); t != tsk; t = next_thread(t))
  592. if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
  593. recalc_sigpending_tsk(t);
  594. if (signal_pending(t))
  595. signal_wake_up(t, 0);
  596. }
  597. spin_unlock_irq(&tsk->sighand->siglock);
  598. read_unlock(&tasklist_lock);
  599. }
  600. write_lock_irq(&tasklist_lock);
  601. /*
  602. * This does two things:
  603. *
  604. * A. Make init inherit all the child processes
  605. * B. Check to see if any process groups have become orphaned
  606. * as a result of our exiting, and if they have any stopped
  607. * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  608. */
  609. INIT_LIST_HEAD(&ptrace_dead);
  610. forget_original_parent(tsk, &ptrace_dead);
  611. BUG_ON(!list_empty(&tsk->children));
  612. BUG_ON(!list_empty(&tsk->ptrace_children));
  613. /*
  614. * Check to see if any process groups have become orphaned
  615. * as a result of our exiting, and if they have any stopped
  616. * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  617. *
  618. * Case i: Our father is in a different pgrp than we are
  619. * and we were the only connection outside, so our pgrp
  620. * is about to become orphaned.
  621. */
  622. t = tsk->real_parent;
  623. if ((process_group(t) != process_group(tsk)) &&
  624. (t->signal->session == tsk->signal->session) &&
  625. will_become_orphaned_pgrp(process_group(tsk), tsk) &&
  626. has_stopped_jobs(process_group(tsk))) {
  627. __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
  628. __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
  629. }
  630. /* Let father know we died
  631. *
  632. * Thread signals are configurable, but you aren't going to use
  633. * that to send signals to arbitary processes.
  634. * That stops right now.
  635. *
  636. * If the parent exec id doesn't match the exec id we saved
  637. * when we started then we know the parent has changed security
  638. * domain.
  639. *
  640. * If our self_exec id doesn't match our parent_exec_id then
  641. * we have changed execution domain as these two values started
  642. * the same after a fork.
  643. *
  644. */
  645. if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
  646. ( tsk->parent_exec_id != t->self_exec_id ||
  647. tsk->self_exec_id != tsk->parent_exec_id)
  648. && !capable(CAP_KILL))
  649. tsk->exit_signal = SIGCHLD;
  650. /* If something other than our normal parent is ptracing us, then
  651. * send it a SIGCHLD instead of honoring exit_signal. exit_signal
  652. * only has special meaning to our real parent.
  653. */
  654. if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
  655. int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
  656. do_notify_parent(tsk, signal);
  657. } else if (tsk->ptrace) {
  658. do_notify_parent(tsk, SIGCHLD);
  659. }
  660. state = EXIT_ZOMBIE;
  661. if (tsk->exit_signal == -1 &&
  662. (likely(tsk->ptrace == 0) ||
  663. unlikely(tsk->parent->signal->flags & SIGNAL_GROUP_EXIT)))
  664. state = EXIT_DEAD;
  665. tsk->exit_state = state;
  666. write_unlock_irq(&tasklist_lock);
  667. list_for_each_safe(_p, _n, &ptrace_dead) {
  668. list_del_init(_p);
  669. t = list_entry(_p,struct task_struct,ptrace_list);
  670. release_task(t);
  671. }
  672. /* If the process is dead, release it - nobody will wait for it */
  673. if (state == EXIT_DEAD)
  674. release_task(tsk);
  675. /* PF_DEAD causes final put_task_struct after we schedule. */
  676. preempt_disable();
  677. tsk->flags |= PF_DEAD;
  678. }
  679. fastcall NORET_TYPE void do_exit(long code)
  680. {
  681. struct task_struct *tsk = current;
  682. int group_dead;
  683. profile_task_exit(tsk);
  684. if (unlikely(in_interrupt()))
  685. panic("Aiee, killing interrupt handler!");
  686. if (unlikely(!tsk->pid))
  687. panic("Attempted to kill the idle task!");
  688. if (unlikely(tsk->pid == 1))
  689. panic("Attempted to kill init!");
  690. if (tsk->io_context)
  691. exit_io_context();
  692. if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
  693. current->ptrace_message = code;
  694. ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
  695. }
  696. /*
  697. * We're taking recursive faults here in do_exit. Safest is to just
  698. * leave this task alone and wait for reboot.
  699. */
  700. if (unlikely(tsk->flags & PF_EXITING)) {
  701. printk(KERN_ALERT
  702. "Fixing recursive fault but reboot is needed!\n");
  703. set_current_state(TASK_UNINTERRUPTIBLE);
  704. schedule();
  705. }
  706. tsk->flags |= PF_EXITING;
  707. /*
  708. * Make sure we don't try to process any timer firings
  709. * while we are already exiting.
  710. */
  711. tsk->it_virt_expires = cputime_zero;
  712. tsk->it_prof_expires = cputime_zero;
  713. tsk->it_sched_expires = 0;
  714. if (unlikely(in_atomic()))
  715. printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
  716. current->comm, current->pid,
  717. preempt_count());
  718. acct_update_integrals(tsk);
  719. update_mem_hiwater(tsk);
  720. group_dead = atomic_dec_and_test(&tsk->signal->live);
  721. if (group_dead)
  722. acct_process(code);
  723. exit_mm(tsk);
  724. exit_sem(tsk);
  725. __exit_files(tsk);
  726. __exit_fs(tsk);
  727. exit_namespace(tsk);
  728. exit_thread();
  729. cpuset_exit(tsk);
  730. exit_keys(tsk);
  731. if (group_dead && tsk->signal->leader)
  732. disassociate_ctty(1);
  733. module_put(tsk->thread_info->exec_domain->module);
  734. if (tsk->binfmt)
  735. module_put(tsk->binfmt->module);
  736. tsk->exit_code = code;
  737. exit_notify(tsk);
  738. #ifdef CONFIG_NUMA
  739. mpol_free(tsk->mempolicy);
  740. tsk->mempolicy = NULL;
  741. #endif
  742. BUG_ON(!(current->flags & PF_DEAD));
  743. schedule();
  744. BUG();
  745. /* Avoid "noreturn function does return". */
  746. for (;;) ;
  747. }
  748. EXPORT_SYMBOL_GPL(do_exit);
  749. NORET_TYPE void complete_and_exit(struct completion *comp, long code)
  750. {
  751. if (comp)
  752. complete(comp);
  753. do_exit(code);
  754. }
  755. EXPORT_SYMBOL(complete_and_exit);
  756. asmlinkage long sys_exit(int error_code)
  757. {
  758. do_exit((error_code&0xff)<<8);
  759. }
  760. task_t fastcall *next_thread(const task_t *p)
  761. {
  762. return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
  763. }
  764. EXPORT_SYMBOL(next_thread);
  765. /*
  766. * Take down every thread in the group. This is called by fatal signals
  767. * as well as by sys_exit_group (below).
  768. */
  769. NORET_TYPE void
  770. do_group_exit(int exit_code)
  771. {
  772. BUG_ON(exit_code & 0x80); /* core dumps don't get here */
  773. if (current->signal->flags & SIGNAL_GROUP_EXIT)
  774. exit_code = current->signal->group_exit_code;
  775. else if (!thread_group_empty(current)) {
  776. struct signal_struct *const sig = current->signal;
  777. struct sighand_struct *const sighand = current->sighand;
  778. read_lock(&tasklist_lock);
  779. spin_lock_irq(&sighand->siglock);
  780. if (sig->flags & SIGNAL_GROUP_EXIT)
  781. /* Another thread got here before we took the lock. */
  782. exit_code = sig->group_exit_code;
  783. else {
  784. sig->flags = SIGNAL_GROUP_EXIT;
  785. sig->group_exit_code = exit_code;
  786. zap_other_threads(current);
  787. }
  788. spin_unlock_irq(&sighand->siglock);
  789. read_unlock(&tasklist_lock);
  790. }
  791. do_exit(exit_code);
  792. /* NOTREACHED */
  793. }
  794. /*
  795. * this kills every thread in the thread group. Note that any externally
  796. * wait4()-ing process will get the correct exit code - even if this
  797. * thread is not the thread group leader.
  798. */
  799. asmlinkage void sys_exit_group(int error_code)
  800. {
  801. do_group_exit((error_code & 0xff) << 8);
  802. }
  803. static int eligible_child(pid_t pid, int options, task_t *p)
  804. {
  805. if (pid > 0) {
  806. if (p->pid != pid)
  807. return 0;
  808. } else if (!pid) {
  809. if (process_group(p) != process_group(current))
  810. return 0;
  811. } else if (pid != -1) {
  812. if (process_group(p) != -pid)
  813. return 0;
  814. }
  815. /*
  816. * Do not consider detached threads that are
  817. * not ptraced:
  818. */
  819. if (p->exit_signal == -1 && !p->ptrace)
  820. return 0;
  821. /* Wait for all children (clone and not) if __WALL is set;
  822. * otherwise, wait for clone children *only* if __WCLONE is
  823. * set; otherwise, wait for non-clone children *only*. (Note:
  824. * A "clone" child here is one that reports to its parent
  825. * using a signal other than SIGCHLD.) */
  826. if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
  827. && !(options & __WALL))
  828. return 0;
  829. /*
  830. * Do not consider thread group leaders that are
  831. * in a non-empty thread group:
  832. */
  833. if (current->tgid != p->tgid && delay_group_leader(p))
  834. return 2;
  835. if (security_task_wait(p))
  836. return 0;
  837. return 1;
  838. }
  839. static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
  840. int why, int status,
  841. struct siginfo __user *infop,
  842. struct rusage __user *rusagep)
  843. {
  844. int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
  845. put_task_struct(p);
  846. if (!retval)
  847. retval = put_user(SIGCHLD, &infop->si_signo);
  848. if (!retval)
  849. retval = put_user(0, &infop->si_errno);
  850. if (!retval)
  851. retval = put_user((short)why, &infop->si_code);
  852. if (!retval)
  853. retval = put_user(pid, &infop->si_pid);
  854. if (!retval)
  855. retval = put_user(uid, &infop->si_uid);
  856. if (!retval)
  857. retval = put_user(status, &infop->si_status);
  858. if (!retval)
  859. retval = pid;
  860. return retval;
  861. }
  862. /*
  863. * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
  864. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  865. * the lock and this task is uninteresting. If we return nonzero, we have
  866. * released the lock and the system call should return.
  867. */
  868. static int wait_task_zombie(task_t *p, int noreap,
  869. struct siginfo __user *infop,
  870. int __user *stat_addr, struct rusage __user *ru)
  871. {
  872. unsigned long state;
  873. int retval;
  874. int status;
  875. if (unlikely(noreap)) {
  876. pid_t pid = p->pid;
  877. uid_t uid = p->uid;
  878. int exit_code = p->exit_code;
  879. int why, status;
  880. if (unlikely(p->exit_state != EXIT_ZOMBIE))
  881. return 0;
  882. if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
  883. return 0;
  884. get_task_struct(p);
  885. read_unlock(&tasklist_lock);
  886. if ((exit_code & 0x7f) == 0) {
  887. why = CLD_EXITED;
  888. status = exit_code >> 8;
  889. } else {
  890. why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
  891. status = exit_code & 0x7f;
  892. }
  893. return wait_noreap_copyout(p, pid, uid, why,
  894. status, infop, ru);
  895. }
  896. /*
  897. * Try to move the task's state to DEAD
  898. * only one thread is allowed to do this:
  899. */
  900. state = xchg(&p->exit_state, EXIT_DEAD);
  901. if (state != EXIT_ZOMBIE) {
  902. BUG_ON(state != EXIT_DEAD);
  903. return 0;
  904. }
  905. if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
  906. /*
  907. * This can only happen in a race with a ptraced thread
  908. * dying on another processor.
  909. */
  910. return 0;
  911. }
  912. if (likely(p->real_parent == p->parent) && likely(p->signal)) {
  913. /*
  914. * The resource counters for the group leader are in its
  915. * own task_struct. Those for dead threads in the group
  916. * are in its signal_struct, as are those for the child
  917. * processes it has previously reaped. All these
  918. * accumulate in the parent's signal_struct c* fields.
  919. *
  920. * We don't bother to take a lock here to protect these
  921. * p->signal fields, because they are only touched by
  922. * __exit_signal, which runs with tasklist_lock
  923. * write-locked anyway, and so is excluded here. We do
  924. * need to protect the access to p->parent->signal fields,
  925. * as other threads in the parent group can be right
  926. * here reaping other children at the same time.
  927. */
  928. spin_lock_irq(&p->parent->sighand->siglock);
  929. p->parent->signal->cutime =
  930. cputime_add(p->parent->signal->cutime,
  931. cputime_add(p->utime,
  932. cputime_add(p->signal->utime,
  933. p->signal->cutime)));
  934. p->parent->signal->cstime =
  935. cputime_add(p->parent->signal->cstime,
  936. cputime_add(p->stime,
  937. cputime_add(p->signal->stime,
  938. p->signal->cstime)));
  939. p->parent->signal->cmin_flt +=
  940. p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
  941. p->parent->signal->cmaj_flt +=
  942. p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
  943. p->parent->signal->cnvcsw +=
  944. p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
  945. p->parent->signal->cnivcsw +=
  946. p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
  947. spin_unlock_irq(&p->parent->sighand->siglock);
  948. }
  949. /*
  950. * Now we are sure this task is interesting, and no other
  951. * thread can reap it because we set its state to EXIT_DEAD.
  952. */
  953. read_unlock(&tasklist_lock);
  954. retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
  955. status = (p->signal->flags & SIGNAL_GROUP_EXIT)
  956. ? p->signal->group_exit_code : p->exit_code;
  957. if (!retval && stat_addr)
  958. retval = put_user(status, stat_addr);
  959. if (!retval && infop)
  960. retval = put_user(SIGCHLD, &infop->si_signo);
  961. if (!retval && infop)
  962. retval = put_user(0, &infop->si_errno);
  963. if (!retval && infop) {
  964. int why;
  965. if ((status & 0x7f) == 0) {
  966. why = CLD_EXITED;
  967. status >>= 8;
  968. } else {
  969. why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
  970. status &= 0x7f;
  971. }
  972. retval = put_user((short)why, &infop->si_code);
  973. if (!retval)
  974. retval = put_user(status, &infop->si_status);
  975. }
  976. if (!retval && infop)
  977. retval = put_user(p->pid, &infop->si_pid);
  978. if (!retval && infop)
  979. retval = put_user(p->uid, &infop->si_uid);
  980. if (retval) {
  981. // TODO: is this safe?
  982. p->exit_state = EXIT_ZOMBIE;
  983. return retval;
  984. }
  985. retval = p->pid;
  986. if (p->real_parent != p->parent) {
  987. write_lock_irq(&tasklist_lock);
  988. /* Double-check with lock held. */
  989. if (p->real_parent != p->parent) {
  990. __ptrace_unlink(p);
  991. // TODO: is this safe?
  992. p->exit_state = EXIT_ZOMBIE;
  993. /*
  994. * If this is not a detached task, notify the parent.
  995. * If it's still not detached after that, don't release
  996. * it now.
  997. */
  998. if (p->exit_signal != -1) {
  999. do_notify_parent(p, p->exit_signal);
  1000. if (p->exit_signal != -1)
  1001. p = NULL;
  1002. }
  1003. }
  1004. write_unlock_irq(&tasklist_lock);
  1005. }
  1006. if (p != NULL)
  1007. release_task(p);
  1008. BUG_ON(!retval);
  1009. return retval;
  1010. }
  1011. /*
  1012. * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
  1013. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1014. * the lock and this task is uninteresting. If we return nonzero, we have
  1015. * released the lock and the system call should return.
  1016. */
  1017. static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
  1018. struct siginfo __user *infop,
  1019. int __user *stat_addr, struct rusage __user *ru)
  1020. {
  1021. int retval, exit_code;
  1022. if (!p->exit_code)
  1023. return 0;
  1024. if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
  1025. p->signal && p->signal->group_stop_count > 0)
  1026. /*
  1027. * A group stop is in progress and this is the group leader.
  1028. * We won't report until all threads have stopped.
  1029. */
  1030. return 0;
  1031. /*
  1032. * Now we are pretty sure this task is interesting.
  1033. * Make sure it doesn't get reaped out from under us while we
  1034. * give up the lock and then examine it below. We don't want to
  1035. * keep holding onto the tasklist_lock while we call getrusage and
  1036. * possibly take page faults for user memory.
  1037. */
  1038. get_task_struct(p);
  1039. read_unlock(&tasklist_lock);
  1040. if (unlikely(noreap)) {
  1041. pid_t pid = p->pid;
  1042. uid_t uid = p->uid;
  1043. int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
  1044. exit_code = p->exit_code;
  1045. if (unlikely(!exit_code) ||
  1046. unlikely(p->state > TASK_STOPPED))
  1047. goto bail_ref;
  1048. return wait_noreap_copyout(p, pid, uid,
  1049. why, (exit_code << 8) | 0x7f,
  1050. infop, ru);
  1051. }
  1052. write_lock_irq(&tasklist_lock);
  1053. /*
  1054. * This uses xchg to be atomic with the thread resuming and setting
  1055. * it. It must also be done with the write lock held to prevent a
  1056. * race with the EXIT_ZOMBIE case.
  1057. */
  1058. exit_code = xchg(&p->exit_code, 0);
  1059. if (unlikely(p->exit_state)) {
  1060. /*
  1061. * The task resumed and then died. Let the next iteration
  1062. * catch it in EXIT_ZOMBIE. Note that exit_code might
  1063. * already be zero here if it resumed and did _exit(0).
  1064. * The task itself is dead and won't touch exit_code again;
  1065. * other processors in this function are locked out.
  1066. */
  1067. p->exit_code = exit_code;
  1068. exit_code = 0;
  1069. }
  1070. if (unlikely(exit_code == 0)) {
  1071. /*
  1072. * Another thread in this function got to it first, or it
  1073. * resumed, or it resumed and then died.
  1074. */
  1075. write_unlock_irq(&tasklist_lock);
  1076. bail_ref:
  1077. put_task_struct(p);
  1078. /*
  1079. * We are returning to the wait loop without having successfully
  1080. * removed the process and having released the lock. We cannot
  1081. * continue, since the "p" task pointer is potentially stale.
  1082. *
  1083. * Return -EAGAIN, and do_wait() will restart the loop from the
  1084. * beginning. Do _not_ re-acquire the lock.
  1085. */
  1086. return -EAGAIN;
  1087. }
  1088. /* move to end of parent's list to avoid starvation */
  1089. remove_parent(p);
  1090. add_parent(p, p->parent);
  1091. write_unlock_irq(&tasklist_lock);
  1092. retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
  1093. if (!retval && stat_addr)
  1094. retval = put_user((exit_code << 8) | 0x7f, stat_addr);
  1095. if (!retval && infop)
  1096. retval = put_user(SIGCHLD, &infop->si_signo);
  1097. if (!retval && infop)
  1098. retval = put_user(0, &infop->si_errno);
  1099. if (!retval && infop)
  1100. retval = put_user((short)((p->ptrace & PT_PTRACED)
  1101. ? CLD_TRAPPED : CLD_STOPPED),
  1102. &infop->si_code);
  1103. if (!retval && infop)
  1104. retval = put_user(exit_code, &infop->si_status);
  1105. if (!retval && infop)
  1106. retval = put_user(p->pid, &infop->si_pid);
  1107. if (!retval && infop)
  1108. retval = put_user(p->uid, &infop->si_uid);
  1109. if (!retval)
  1110. retval = p->pid;
  1111. put_task_struct(p);
  1112. BUG_ON(!retval);
  1113. return retval;
  1114. }
  1115. /*
  1116. * Handle do_wait work for one task in a live, non-stopped state.
  1117. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1118. * the lock and this task is uninteresting. If we return nonzero, we have
  1119. * released the lock and the system call should return.
  1120. */
  1121. static int wait_task_continued(task_t *p, int noreap,
  1122. struct siginfo __user *infop,
  1123. int __user *stat_addr, struct rusage __user *ru)
  1124. {
  1125. int retval;
  1126. pid_t pid;
  1127. uid_t uid;
  1128. if (unlikely(!p->signal))
  1129. return 0;
  1130. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
  1131. return 0;
  1132. spin_lock_irq(&p->sighand->siglock);
  1133. /* Re-check with the lock held. */
  1134. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
  1135. spin_unlock_irq(&p->sighand->siglock);
  1136. return 0;
  1137. }
  1138. if (!noreap)
  1139. p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
  1140. spin_unlock_irq(&p->sighand->siglock);
  1141. pid = p->pid;
  1142. uid = p->uid;
  1143. get_task_struct(p);
  1144. read_unlock(&tasklist_lock);
  1145. if (!infop) {
  1146. retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
  1147. put_task_struct(p);
  1148. if (!retval && stat_addr)
  1149. retval = put_user(0xffff, stat_addr);
  1150. if (!retval)
  1151. retval = p->pid;
  1152. } else {
  1153. retval = wait_noreap_copyout(p, pid, uid,
  1154. CLD_CONTINUED, SIGCONT,
  1155. infop, ru);
  1156. BUG_ON(retval == 0);
  1157. }
  1158. return retval;
  1159. }
  1160. static inline int my_ptrace_child(struct task_struct *p)
  1161. {
  1162. if (!(p->ptrace & PT_PTRACED))
  1163. return 0;
  1164. if (!(p->ptrace & PT_ATTACHED))
  1165. return 1;
  1166. /*
  1167. * This child was PTRACE_ATTACH'd. We should be seeing it only if
  1168. * we are the attacher. If we are the real parent, this is a race
  1169. * inside ptrace_attach. It is waiting for the tasklist_lock,
  1170. * which we have to switch the parent links, but has already set
  1171. * the flags in p->ptrace.
  1172. */
  1173. return (p->parent != p->real_parent);
  1174. }
  1175. static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
  1176. int __user *stat_addr, struct rusage __user *ru)
  1177. {
  1178. DECLARE_WAITQUEUE(wait, current);
  1179. struct task_struct *tsk;
  1180. int flag, retval;
  1181. add_wait_queue(&current->signal->wait_chldexit,&wait);
  1182. repeat:
  1183. /*
  1184. * We will set this flag if we see any child that might later
  1185. * match our criteria, even if we are not able to reap it yet.
  1186. */
  1187. flag = 0;
  1188. current->state = TASK_INTERRUPTIBLE;
  1189. read_lock(&tasklist_lock);
  1190. tsk = current;
  1191. do {
  1192. struct task_struct *p;
  1193. struct list_head *_p;
  1194. int ret;
  1195. list_for_each(_p,&tsk->children) {
  1196. p = list_entry(_p,struct task_struct,sibling);
  1197. ret = eligible_child(pid, options, p);
  1198. if (!ret)
  1199. continue;
  1200. switch (p->state) {
  1201. case TASK_TRACED:
  1202. if (!my_ptrace_child(p))
  1203. continue;
  1204. /*FALLTHROUGH*/
  1205. case TASK_STOPPED:
  1206. /*
  1207. * It's stopped now, so it might later
  1208. * continue, exit, or stop again.
  1209. */
  1210. flag = 1;
  1211. if (!(options & WUNTRACED) &&
  1212. !my_ptrace_child(p))
  1213. continue;
  1214. retval = wait_task_stopped(p, ret == 2,
  1215. (options & WNOWAIT),
  1216. infop,
  1217. stat_addr, ru);
  1218. if (retval == -EAGAIN)
  1219. goto repeat;
  1220. if (retval != 0) /* He released the lock. */
  1221. goto end;
  1222. break;
  1223. default:
  1224. // case EXIT_DEAD:
  1225. if (p->exit_state == EXIT_DEAD)
  1226. continue;
  1227. // case EXIT_ZOMBIE:
  1228. if (p->exit_state == EXIT_ZOMBIE) {
  1229. /*
  1230. * Eligible but we cannot release
  1231. * it yet:
  1232. */
  1233. if (ret == 2)
  1234. goto check_continued;
  1235. if (!likely(options & WEXITED))
  1236. continue;
  1237. retval = wait_task_zombie(
  1238. p, (options & WNOWAIT),
  1239. infop, stat_addr, ru);
  1240. /* He released the lock. */
  1241. if (retval != 0)
  1242. goto end;
  1243. break;
  1244. }
  1245. check_continued:
  1246. /*
  1247. * It's running now, so it might later
  1248. * exit, stop, or stop and then continue.
  1249. */
  1250. flag = 1;
  1251. if (!unlikely(options & WCONTINUED))
  1252. continue;
  1253. retval = wait_task_continued(
  1254. p, (options & WNOWAIT),
  1255. infop, stat_addr, ru);
  1256. if (retval != 0) /* He released the lock. */
  1257. goto end;
  1258. break;
  1259. }
  1260. }
  1261. if (!flag) {
  1262. list_for_each(_p, &tsk->ptrace_children) {
  1263. p = list_entry(_p, struct task_struct,
  1264. ptrace_list);
  1265. if (!eligible_child(pid, options, p))
  1266. continue;
  1267. flag = 1;
  1268. break;
  1269. }
  1270. }
  1271. if (options & __WNOTHREAD)
  1272. break;
  1273. tsk = next_thread(tsk);
  1274. if (tsk->signal != current->signal)
  1275. BUG();
  1276. } while (tsk != current);
  1277. read_unlock(&tasklist_lock);
  1278. if (flag) {
  1279. retval = 0;
  1280. if (options & WNOHANG)
  1281. goto end;
  1282. retval = -ERESTARTSYS;
  1283. if (signal_pending(current))
  1284. goto end;
  1285. schedule();
  1286. goto repeat;
  1287. }
  1288. retval = -ECHILD;
  1289. end:
  1290. current->state = TASK_RUNNING;
  1291. remove_wait_queue(&current->signal->wait_chldexit,&wait);
  1292. if (infop) {
  1293. if (retval > 0)
  1294. retval = 0;
  1295. else {
  1296. /*
  1297. * For a WNOHANG return, clear out all the fields
  1298. * we would set so the user can easily tell the
  1299. * difference.
  1300. */
  1301. if (!retval)
  1302. retval = put_user(0, &infop->si_signo);
  1303. if (!retval)
  1304. retval = put_user(0, &infop->si_errno);
  1305. if (!retval)
  1306. retval = put_user(0, &infop->si_code);
  1307. if (!retval)
  1308. retval = put_user(0, &infop->si_pid);
  1309. if (!retval)
  1310. retval = put_user(0, &infop->si_uid);
  1311. if (!retval)
  1312. retval = put_user(0, &infop->si_status);
  1313. }
  1314. }
  1315. return retval;
  1316. }
  1317. asmlinkage long sys_waitid(int which, pid_t pid,
  1318. struct siginfo __user *infop, int options,
  1319. struct rusage __user *ru)
  1320. {
  1321. long ret;
  1322. if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
  1323. return -EINVAL;
  1324. if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
  1325. return -EINVAL;
  1326. switch (which) {
  1327. case P_ALL:
  1328. pid = -1;
  1329. break;
  1330. case P_PID:
  1331. if (pid <= 0)
  1332. return -EINVAL;
  1333. break;
  1334. case P_PGID:
  1335. if (pid <= 0)
  1336. return -EINVAL;
  1337. pid = -pid;
  1338. break;
  1339. default:
  1340. return -EINVAL;
  1341. }
  1342. ret = do_wait(pid, options, infop, NULL, ru);
  1343. /* avoid REGPARM breakage on x86: */
  1344. prevent_tail_call(ret);
  1345. return ret;
  1346. }
  1347. asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
  1348. int options, struct rusage __user *ru)
  1349. {
  1350. long ret;
  1351. if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
  1352. __WNOTHREAD|__WCLONE|__WALL))
  1353. return -EINVAL;
  1354. ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
  1355. /* avoid REGPARM breakage on x86: */
  1356. prevent_tail_call(ret);
  1357. return ret;
  1358. }
  1359. #ifdef __ARCH_WANT_SYS_WAITPID
  1360. /*
  1361. * sys_waitpid() remains for compatibility. waitpid() should be
  1362. * implemented by calling sys_wait4() from libc.a.
  1363. */
  1364. asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
  1365. {
  1366. return sys_wait4(pid, stat_addr, options, NULL);
  1367. }
  1368. #endif