exit.c 40 KB

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