exit.c 39 KB

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