exit.c 40 KB

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