exit.c 39 KB

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