exit.c 41 KB

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