exit.c 40 KB

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