exit.c 43 KB

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