exit.c 38 KB

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