exit.c 40 KB

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