exit.c 41 KB

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