exit.c 43 KB

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