exit.c 42 KB

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