exit.c 40 KB

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