exit.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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 <asm/uaccess.h>
  32. #include <asm/unistd.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/mmu_context.h>
  35. extern void sem_exit (void);
  36. extern struct task_struct *child_reaper;
  37. int getrusage(struct task_struct *, int, struct rusage __user *);
  38. static void exit_mm(struct task_struct * tsk);
  39. static void __unhash_process(struct task_struct *p)
  40. {
  41. nr_threads--;
  42. detach_pid(p, PIDTYPE_PID);
  43. detach_pid(p, PIDTYPE_TGID);
  44. if (thread_group_leader(p)) {
  45. detach_pid(p, PIDTYPE_PGID);
  46. detach_pid(p, PIDTYPE_SID);
  47. if (p->pid)
  48. __get_cpu_var(process_counts)--;
  49. }
  50. REMOVE_LINKS(p);
  51. }
  52. void release_task(struct task_struct * p)
  53. {
  54. int zap_leader;
  55. task_t *leader;
  56. struct dentry *proc_dentry;
  57. repeat:
  58. atomic_dec(&p->user->processes);
  59. spin_lock(&p->proc_lock);
  60. proc_dentry = proc_pid_unhash(p);
  61. write_lock_irq(&tasklist_lock);
  62. if (unlikely(p->ptrace))
  63. __ptrace_unlink(p);
  64. BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
  65. __exit_signal(p);
  66. __exit_sighand(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;
  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. del_timer_sync(&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. /* PF_DEAD causes final put_task_struct after we schedule. */
  759. preempt_disable();
  760. BUG_ON(tsk->flags & PF_DEAD);
  761. tsk->flags |= PF_DEAD;
  762. schedule();
  763. BUG();
  764. /* Avoid "noreturn function does return". */
  765. for (;;) ;
  766. }
  767. EXPORT_SYMBOL_GPL(do_exit);
  768. NORET_TYPE void complete_and_exit(struct completion *comp, long code)
  769. {
  770. if (comp)
  771. complete(comp);
  772. do_exit(code);
  773. }
  774. EXPORT_SYMBOL(complete_and_exit);
  775. asmlinkage long sys_exit(int error_code)
  776. {
  777. do_exit((error_code&0xff)<<8);
  778. }
  779. task_t fastcall *next_thread(const task_t *p)
  780. {
  781. return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
  782. }
  783. EXPORT_SYMBOL(next_thread);
  784. /*
  785. * Take down every thread in the group. This is called by fatal signals
  786. * as well as by sys_exit_group (below).
  787. */
  788. NORET_TYPE void
  789. do_group_exit(int exit_code)
  790. {
  791. BUG_ON(exit_code & 0x80); /* core dumps don't get here */
  792. if (current->signal->flags & SIGNAL_GROUP_EXIT)
  793. exit_code = current->signal->group_exit_code;
  794. else if (!thread_group_empty(current)) {
  795. struct signal_struct *const sig = current->signal;
  796. struct sighand_struct *const sighand = current->sighand;
  797. read_lock(&tasklist_lock);
  798. spin_lock_irq(&sighand->siglock);
  799. if (sig->flags & SIGNAL_GROUP_EXIT)
  800. /* Another thread got here before we took the lock. */
  801. exit_code = sig->group_exit_code;
  802. else {
  803. sig->flags = SIGNAL_GROUP_EXIT;
  804. sig->group_exit_code = exit_code;
  805. zap_other_threads(current);
  806. }
  807. spin_unlock_irq(&sighand->siglock);
  808. read_unlock(&tasklist_lock);
  809. }
  810. do_exit(exit_code);
  811. /* NOTREACHED */
  812. }
  813. /*
  814. * this kills every thread in the thread group. Note that any externally
  815. * wait4()-ing process will get the correct exit code - even if this
  816. * thread is not the thread group leader.
  817. */
  818. asmlinkage void sys_exit_group(int error_code)
  819. {
  820. do_group_exit((error_code & 0xff) << 8);
  821. }
  822. static int eligible_child(pid_t pid, int options, task_t *p)
  823. {
  824. if (pid > 0) {
  825. if (p->pid != pid)
  826. return 0;
  827. } else if (!pid) {
  828. if (process_group(p) != process_group(current))
  829. return 0;
  830. } else if (pid != -1) {
  831. if (process_group(p) != -pid)
  832. return 0;
  833. }
  834. /*
  835. * Do not consider detached threads that are
  836. * not ptraced:
  837. */
  838. if (p->exit_signal == -1 && !p->ptrace)
  839. return 0;
  840. /* Wait for all children (clone and not) if __WALL is set;
  841. * otherwise, wait for clone children *only* if __WCLONE is
  842. * set; otherwise, wait for non-clone children *only*. (Note:
  843. * A "clone" child here is one that reports to its parent
  844. * using a signal other than SIGCHLD.) */
  845. if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
  846. && !(options & __WALL))
  847. return 0;
  848. /*
  849. * Do not consider thread group leaders that are
  850. * in a non-empty thread group:
  851. */
  852. if (current->tgid != p->tgid && delay_group_leader(p))
  853. return 2;
  854. if (security_task_wait(p))
  855. return 0;
  856. return 1;
  857. }
  858. static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
  859. int why, int status,
  860. struct siginfo __user *infop,
  861. struct rusage __user *rusagep)
  862. {
  863. int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
  864. put_task_struct(p);
  865. if (!retval)
  866. retval = put_user(SIGCHLD, &infop->si_signo);
  867. if (!retval)
  868. retval = put_user(0, &infop->si_errno);
  869. if (!retval)
  870. retval = put_user((short)why, &infop->si_code);
  871. if (!retval)
  872. retval = put_user(pid, &infop->si_pid);
  873. if (!retval)
  874. retval = put_user(uid, &infop->si_uid);
  875. if (!retval)
  876. retval = put_user(status, &infop->si_status);
  877. if (!retval)
  878. retval = pid;
  879. return retval;
  880. }
  881. /*
  882. * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
  883. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  884. * the lock and this task is uninteresting. If we return nonzero, we have
  885. * released the lock and the system call should return.
  886. */
  887. static int wait_task_zombie(task_t *p, int noreap,
  888. struct siginfo __user *infop,
  889. int __user *stat_addr, struct rusage __user *ru)
  890. {
  891. unsigned long state;
  892. int retval;
  893. int status;
  894. if (unlikely(noreap)) {
  895. pid_t pid = p->pid;
  896. uid_t uid = p->uid;
  897. int exit_code = p->exit_code;
  898. int why, status;
  899. if (unlikely(p->exit_state != EXIT_ZOMBIE))
  900. return 0;
  901. if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
  902. return 0;
  903. get_task_struct(p);
  904. read_unlock(&tasklist_lock);
  905. if ((exit_code & 0x7f) == 0) {
  906. why = CLD_EXITED;
  907. status = exit_code >> 8;
  908. } else {
  909. why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
  910. status = exit_code & 0x7f;
  911. }
  912. return wait_noreap_copyout(p, pid, uid, why,
  913. status, infop, ru);
  914. }
  915. /*
  916. * Try to move the task's state to DEAD
  917. * only one thread is allowed to do this:
  918. */
  919. state = xchg(&p->exit_state, EXIT_DEAD);
  920. if (state != EXIT_ZOMBIE) {
  921. BUG_ON(state != EXIT_DEAD);
  922. return 0;
  923. }
  924. if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
  925. /*
  926. * This can only happen in a race with a ptraced thread
  927. * dying on another processor.
  928. */
  929. return 0;
  930. }
  931. if (likely(p->real_parent == p->parent) && likely(p->signal)) {
  932. /*
  933. * The resource counters for the group leader are in its
  934. * own task_struct. Those for dead threads in the group
  935. * are in its signal_struct, as are those for the child
  936. * processes it has previously reaped. All these
  937. * accumulate in the parent's signal_struct c* fields.
  938. *
  939. * We don't bother to take a lock here to protect these
  940. * p->signal fields, because they are only touched by
  941. * __exit_signal, which runs with tasklist_lock
  942. * write-locked anyway, and so is excluded here. We do
  943. * need to protect the access to p->parent->signal fields,
  944. * as other threads in the parent group can be right
  945. * here reaping other children at the same time.
  946. */
  947. spin_lock_irq(&p->parent->sighand->siglock);
  948. p->parent->signal->cutime =
  949. cputime_add(p->parent->signal->cutime,
  950. cputime_add(p->utime,
  951. cputime_add(p->signal->utime,
  952. p->signal->cutime)));
  953. p->parent->signal->cstime =
  954. cputime_add(p->parent->signal->cstime,
  955. cputime_add(p->stime,
  956. cputime_add(p->signal->stime,
  957. p->signal->cstime)));
  958. p->parent->signal->cmin_flt +=
  959. p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
  960. p->parent->signal->cmaj_flt +=
  961. p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
  962. p->parent->signal->cnvcsw +=
  963. p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
  964. p->parent->signal->cnivcsw +=
  965. p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
  966. spin_unlock_irq(&p->parent->sighand->siglock);
  967. }
  968. /*
  969. * Now we are sure this task is interesting, and no other
  970. * thread can reap it because we set its state to EXIT_DEAD.
  971. */
  972. read_unlock(&tasklist_lock);
  973. retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
  974. status = (p->signal->flags & SIGNAL_GROUP_EXIT)
  975. ? p->signal->group_exit_code : p->exit_code;
  976. if (!retval && stat_addr)
  977. retval = put_user(status, stat_addr);
  978. if (!retval && infop)
  979. retval = put_user(SIGCHLD, &infop->si_signo);
  980. if (!retval && infop)
  981. retval = put_user(0, &infop->si_errno);
  982. if (!retval && infop) {
  983. int why;
  984. if ((status & 0x7f) == 0) {
  985. why = CLD_EXITED;
  986. status >>= 8;
  987. } else {
  988. why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
  989. status &= 0x7f;
  990. }
  991. retval = put_user((short)why, &infop->si_code);
  992. if (!retval)
  993. retval = put_user(status, &infop->si_status);
  994. }
  995. if (!retval && infop)
  996. retval = put_user(p->pid, &infop->si_pid);
  997. if (!retval && infop)
  998. retval = put_user(p->uid, &infop->si_uid);
  999. if (retval) {
  1000. // TODO: is this safe?
  1001. p->exit_state = EXIT_ZOMBIE;
  1002. return retval;
  1003. }
  1004. retval = p->pid;
  1005. if (p->real_parent != p->parent) {
  1006. write_lock_irq(&tasklist_lock);
  1007. /* Double-check with lock held. */
  1008. if (p->real_parent != p->parent) {
  1009. __ptrace_unlink(p);
  1010. // TODO: is this safe?
  1011. p->exit_state = EXIT_ZOMBIE;
  1012. /*
  1013. * If this is not a detached task, notify the parent.
  1014. * If it's still not detached after that, don't release
  1015. * it now.
  1016. */
  1017. if (p->exit_signal != -1) {
  1018. do_notify_parent(p, p->exit_signal);
  1019. if (p->exit_signal != -1)
  1020. p = NULL;
  1021. }
  1022. }
  1023. write_unlock_irq(&tasklist_lock);
  1024. }
  1025. if (p != NULL)
  1026. release_task(p);
  1027. BUG_ON(!retval);
  1028. return retval;
  1029. }
  1030. /*
  1031. * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
  1032. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1033. * the lock and this task is uninteresting. If we return nonzero, we have
  1034. * released the lock and the system call should return.
  1035. */
  1036. static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
  1037. struct siginfo __user *infop,
  1038. int __user *stat_addr, struct rusage __user *ru)
  1039. {
  1040. int retval, exit_code;
  1041. if (!p->exit_code)
  1042. return 0;
  1043. if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
  1044. p->signal && p->signal->group_stop_count > 0)
  1045. /*
  1046. * A group stop is in progress and this is the group leader.
  1047. * We won't report until all threads have stopped.
  1048. */
  1049. return 0;
  1050. /*
  1051. * Now we are pretty sure this task is interesting.
  1052. * Make sure it doesn't get reaped out from under us while we
  1053. * give up the lock and then examine it below. We don't want to
  1054. * keep holding onto the tasklist_lock while we call getrusage and
  1055. * possibly take page faults for user memory.
  1056. */
  1057. get_task_struct(p);
  1058. read_unlock(&tasklist_lock);
  1059. if (unlikely(noreap)) {
  1060. pid_t pid = p->pid;
  1061. uid_t uid = p->uid;
  1062. int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
  1063. exit_code = p->exit_code;
  1064. if (unlikely(!exit_code) ||
  1065. unlikely(p->state & TASK_TRACED))
  1066. goto bail_ref;
  1067. return wait_noreap_copyout(p, pid, uid,
  1068. why, (exit_code << 8) | 0x7f,
  1069. infop, ru);
  1070. }
  1071. write_lock_irq(&tasklist_lock);
  1072. /*
  1073. * This uses xchg to be atomic with the thread resuming and setting
  1074. * it. It must also be done with the write lock held to prevent a
  1075. * race with the EXIT_ZOMBIE case.
  1076. */
  1077. exit_code = xchg(&p->exit_code, 0);
  1078. if (unlikely(p->exit_state)) {
  1079. /*
  1080. * The task resumed and then died. Let the next iteration
  1081. * catch it in EXIT_ZOMBIE. Note that exit_code might
  1082. * already be zero here if it resumed and did _exit(0).
  1083. * The task itself is dead and won't touch exit_code again;
  1084. * other processors in this function are locked out.
  1085. */
  1086. p->exit_code = exit_code;
  1087. exit_code = 0;
  1088. }
  1089. if (unlikely(exit_code == 0)) {
  1090. /*
  1091. * Another thread in this function got to it first, or it
  1092. * resumed, or it resumed and then died.
  1093. */
  1094. write_unlock_irq(&tasklist_lock);
  1095. bail_ref:
  1096. put_task_struct(p);
  1097. /*
  1098. * We are returning to the wait loop without having successfully
  1099. * removed the process and having released the lock. We cannot
  1100. * continue, since the "p" task pointer is potentially stale.
  1101. *
  1102. * Return -EAGAIN, and do_wait() will restart the loop from the
  1103. * beginning. Do _not_ re-acquire the lock.
  1104. */
  1105. return -EAGAIN;
  1106. }
  1107. /* move to end of parent's list to avoid starvation */
  1108. remove_parent(p);
  1109. add_parent(p, p->parent);
  1110. write_unlock_irq(&tasklist_lock);
  1111. retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
  1112. if (!retval && stat_addr)
  1113. retval = put_user((exit_code << 8) | 0x7f, stat_addr);
  1114. if (!retval && infop)
  1115. retval = put_user(SIGCHLD, &infop->si_signo);
  1116. if (!retval && infop)
  1117. retval = put_user(0, &infop->si_errno);
  1118. if (!retval && infop)
  1119. retval = put_user((short)((p->ptrace & PT_PTRACED)
  1120. ? CLD_TRAPPED : CLD_STOPPED),
  1121. &infop->si_code);
  1122. if (!retval && infop)
  1123. retval = put_user(exit_code, &infop->si_status);
  1124. if (!retval && infop)
  1125. retval = put_user(p->pid, &infop->si_pid);
  1126. if (!retval && infop)
  1127. retval = put_user(p->uid, &infop->si_uid);
  1128. if (!retval)
  1129. retval = p->pid;
  1130. put_task_struct(p);
  1131. BUG_ON(!retval);
  1132. return retval;
  1133. }
  1134. /*
  1135. * Handle do_wait work for one task in a live, non-stopped state.
  1136. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1137. * the lock and this task is uninteresting. If we return nonzero, we have
  1138. * released the lock and the system call should return.
  1139. */
  1140. static int wait_task_continued(task_t *p, int noreap,
  1141. struct siginfo __user *infop,
  1142. int __user *stat_addr, struct rusage __user *ru)
  1143. {
  1144. int retval;
  1145. pid_t pid;
  1146. uid_t uid;
  1147. if (unlikely(!p->signal))
  1148. return 0;
  1149. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
  1150. return 0;
  1151. spin_lock_irq(&p->sighand->siglock);
  1152. /* Re-check with the lock held. */
  1153. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
  1154. spin_unlock_irq(&p->sighand->siglock);
  1155. return 0;
  1156. }
  1157. if (!noreap)
  1158. p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
  1159. spin_unlock_irq(&p->sighand->siglock);
  1160. pid = p->pid;
  1161. uid = p->uid;
  1162. get_task_struct(p);
  1163. read_unlock(&tasklist_lock);
  1164. if (!infop) {
  1165. retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
  1166. put_task_struct(p);
  1167. if (!retval && stat_addr)
  1168. retval = put_user(0xffff, stat_addr);
  1169. if (!retval)
  1170. retval = p->pid;
  1171. } else {
  1172. retval = wait_noreap_copyout(p, pid, uid,
  1173. CLD_CONTINUED, SIGCONT,
  1174. infop, ru);
  1175. BUG_ON(retval == 0);
  1176. }
  1177. return retval;
  1178. }
  1179. static inline int my_ptrace_child(struct task_struct *p)
  1180. {
  1181. if (!(p->ptrace & PT_PTRACED))
  1182. return 0;
  1183. if (!(p->ptrace & PT_ATTACHED))
  1184. return 1;
  1185. /*
  1186. * This child was PTRACE_ATTACH'd. We should be seeing it only if
  1187. * we are the attacher. If we are the real parent, this is a race
  1188. * inside ptrace_attach. It is waiting for the tasklist_lock,
  1189. * which we have to switch the parent links, but has already set
  1190. * the flags in p->ptrace.
  1191. */
  1192. return (p->parent != p->real_parent);
  1193. }
  1194. static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
  1195. int __user *stat_addr, struct rusage __user *ru)
  1196. {
  1197. DECLARE_WAITQUEUE(wait, current);
  1198. struct task_struct *tsk;
  1199. int flag, retval;
  1200. add_wait_queue(&current->signal->wait_chldexit,&wait);
  1201. repeat:
  1202. /*
  1203. * We will set this flag if we see any child that might later
  1204. * match our criteria, even if we are not able to reap it yet.
  1205. */
  1206. flag = 0;
  1207. current->state = TASK_INTERRUPTIBLE;
  1208. read_lock(&tasklist_lock);
  1209. tsk = current;
  1210. do {
  1211. struct task_struct *p;
  1212. struct list_head *_p;
  1213. int ret;
  1214. list_for_each(_p,&tsk->children) {
  1215. p = list_entry(_p,struct task_struct,sibling);
  1216. ret = eligible_child(pid, options, p);
  1217. if (!ret)
  1218. continue;
  1219. switch (p->state) {
  1220. case TASK_TRACED:
  1221. /*
  1222. * When we hit the race with PTRACE_ATTACH,
  1223. * we will not report this child. But the
  1224. * race means it has not yet been moved to
  1225. * our ptrace_children list, so we need to
  1226. * set the flag here to avoid a spurious ECHILD
  1227. * when the race happens with the only child.
  1228. */
  1229. flag = 1;
  1230. if (!my_ptrace_child(p))
  1231. continue;
  1232. /*FALLTHROUGH*/
  1233. case TASK_STOPPED:
  1234. /*
  1235. * It's stopped now, so it might later
  1236. * continue, exit, or stop again.
  1237. */
  1238. flag = 1;
  1239. if (!(options & WUNTRACED) &&
  1240. !my_ptrace_child(p))
  1241. continue;
  1242. retval = wait_task_stopped(p, ret == 2,
  1243. (options & WNOWAIT),
  1244. infop,
  1245. stat_addr, ru);
  1246. if (retval == -EAGAIN)
  1247. goto repeat;
  1248. if (retval != 0) /* He released the lock. */
  1249. goto end;
  1250. break;
  1251. default:
  1252. // case EXIT_DEAD:
  1253. if (p->exit_state == EXIT_DEAD)
  1254. continue;
  1255. // case EXIT_ZOMBIE:
  1256. if (p->exit_state == EXIT_ZOMBIE) {
  1257. /*
  1258. * Eligible but we cannot release
  1259. * it yet:
  1260. */
  1261. if (ret == 2)
  1262. goto check_continued;
  1263. if (!likely(options & WEXITED))
  1264. continue;
  1265. retval = wait_task_zombie(
  1266. p, (options & WNOWAIT),
  1267. infop, stat_addr, ru);
  1268. /* He released the lock. */
  1269. if (retval != 0)
  1270. goto end;
  1271. break;
  1272. }
  1273. check_continued:
  1274. /*
  1275. * It's running now, so it might later
  1276. * exit, stop, or stop and then continue.
  1277. */
  1278. flag = 1;
  1279. if (!unlikely(options & WCONTINUED))
  1280. continue;
  1281. retval = wait_task_continued(
  1282. p, (options & WNOWAIT),
  1283. infop, stat_addr, ru);
  1284. if (retval != 0) /* He released the lock. */
  1285. goto end;
  1286. break;
  1287. }
  1288. }
  1289. if (!flag) {
  1290. list_for_each(_p, &tsk->ptrace_children) {
  1291. p = list_entry(_p, struct task_struct,
  1292. ptrace_list);
  1293. if (!eligible_child(pid, options, p))
  1294. continue;
  1295. flag = 1;
  1296. break;
  1297. }
  1298. }
  1299. if (options & __WNOTHREAD)
  1300. break;
  1301. tsk = next_thread(tsk);
  1302. if (tsk->signal != current->signal)
  1303. BUG();
  1304. } while (tsk != current);
  1305. read_unlock(&tasklist_lock);
  1306. if (flag) {
  1307. retval = 0;
  1308. if (options & WNOHANG)
  1309. goto end;
  1310. retval = -ERESTARTSYS;
  1311. if (signal_pending(current))
  1312. goto end;
  1313. schedule();
  1314. goto repeat;
  1315. }
  1316. retval = -ECHILD;
  1317. end:
  1318. current->state = TASK_RUNNING;
  1319. remove_wait_queue(&current->signal->wait_chldexit,&wait);
  1320. if (infop) {
  1321. if (retval > 0)
  1322. retval = 0;
  1323. else {
  1324. /*
  1325. * For a WNOHANG return, clear out all the fields
  1326. * we would set so the user can easily tell the
  1327. * difference.
  1328. */
  1329. if (!retval)
  1330. retval = put_user(0, &infop->si_signo);
  1331. if (!retval)
  1332. retval = put_user(0, &infop->si_errno);
  1333. if (!retval)
  1334. retval = put_user(0, &infop->si_code);
  1335. if (!retval)
  1336. retval = put_user(0, &infop->si_pid);
  1337. if (!retval)
  1338. retval = put_user(0, &infop->si_uid);
  1339. if (!retval)
  1340. retval = put_user(0, &infop->si_status);
  1341. }
  1342. }
  1343. return retval;
  1344. }
  1345. asmlinkage long sys_waitid(int which, pid_t pid,
  1346. struct siginfo __user *infop, int options,
  1347. struct rusage __user *ru)
  1348. {
  1349. long ret;
  1350. if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
  1351. return -EINVAL;
  1352. if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
  1353. return -EINVAL;
  1354. switch (which) {
  1355. case P_ALL:
  1356. pid = -1;
  1357. break;
  1358. case P_PID:
  1359. if (pid <= 0)
  1360. return -EINVAL;
  1361. break;
  1362. case P_PGID:
  1363. if (pid <= 0)
  1364. return -EINVAL;
  1365. pid = -pid;
  1366. break;
  1367. default:
  1368. return -EINVAL;
  1369. }
  1370. ret = do_wait(pid, options, infop, NULL, ru);
  1371. /* avoid REGPARM breakage on x86: */
  1372. prevent_tail_call(ret);
  1373. return ret;
  1374. }
  1375. asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
  1376. int options, struct rusage __user *ru)
  1377. {
  1378. long ret;
  1379. if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
  1380. __WNOTHREAD|__WCLONE|__WALL))
  1381. return -EINVAL;
  1382. ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
  1383. /* avoid REGPARM breakage on x86: */
  1384. prevent_tail_call(ret);
  1385. return ret;
  1386. }
  1387. #ifdef __ARCH_WANT_SYS_WAITPID
  1388. /*
  1389. * sys_waitpid() remains for compatibility. waitpid() should be
  1390. * implemented by calling sys_wait4() from libc.a.
  1391. */
  1392. asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
  1393. {
  1394. return sys_wait4(pid, stat_addr, options, NULL);
  1395. }
  1396. #endif