exit.c 42 KB

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