exit.c 38 KB

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