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