exit.c 43 KB

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