exit.c 39 KB

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