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