exit.c 45 KB

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