exit.c 41 KB

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