exit.c 47 KB

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