exit.c 40 KB

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