exit.c 40 KB

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