exit.c 43 KB

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