exit.c 40 KB

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