exit.c 40 KB

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