exit.c 40 KB

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