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