4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
9 #include <linux/slab.h>
10 #include <linux/interrupt.h>
11 #include <linux/smp_lock.h>
12 #include <linux/module.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>
31 #include <asm/uaccess.h>
32 #include <asm/unistd.h>
33 #include <asm/pgtable.h>
34 #include <asm/mmu_context.h>
36 extern void sem_exit (void);
37 extern struct task_struct
*child_reaper
;
39 int getrusage(struct task_struct
*, int, struct rusage __user
*);
41 static void __unhash_process(struct task_struct
*p
)
44 detach_pid(p
, PIDTYPE_PID
);
45 detach_pid(p
, PIDTYPE_TGID
);
46 if (thread_group_leader(p
)) {
47 detach_pid(p
, PIDTYPE_PGID
);
48 detach_pid(p
, PIDTYPE_SID
);
50 __get_cpu_var(process_counts
)--;
56 void release_task(struct task_struct
* p
)
60 struct dentry
*proc_dentry
;
63 atomic_dec(&p
->user
->processes
);
64 spin_lock(&p
->proc_lock
);
65 proc_dentry
= proc_pid_unhash(p
);
66 write_lock_irq(&tasklist_lock
);
67 if (unlikely(p
->ptrace
))
69 BUG_ON(!list_empty(&p
->ptrace_list
) || !list_empty(&p
->ptrace_children
));
75 * If we are the last non-leader member of the thread
76 * group, and the leader is zombie, then notify the
77 * group leader's parent process. (if it wants notification.)
80 leader
= p
->group_leader
;
81 if (leader
!= p
&& thread_group_empty(leader
) && leader
->exit_state
== EXIT_ZOMBIE
) {
82 BUG_ON(leader
->exit_signal
== -1);
83 do_notify_parent(leader
, leader
->exit_signal
);
85 * If we were the last child thread and the leader has
86 * exited already, and the leader's parent ignores SIGCHLD,
87 * then we are the one who should release the leader.
89 * do_notify_parent() will have marked it self-reaping in
92 zap_leader
= (leader
->exit_signal
== -1);
96 write_unlock_irq(&tasklist_lock
);
97 spin_unlock(&p
->proc_lock
);
98 proc_pid_flush(proc_dentry
);
103 if (unlikely(zap_leader
))
107 /* we are using it only for SMP init */
109 void unhash_process(struct task_struct
*p
)
111 struct dentry
*proc_dentry
;
113 spin_lock(&p
->proc_lock
);
114 proc_dentry
= proc_pid_unhash(p
);
115 write_lock_irq(&tasklist_lock
);
117 write_unlock_irq(&tasklist_lock
);
118 spin_unlock(&p
->proc_lock
);
119 proc_pid_flush(proc_dentry
);
123 * This checks not only the pgrp, but falls back on the pid if no
124 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
127 int session_of_pgrp(int pgrp
)
129 struct task_struct
*p
;
132 read_lock(&tasklist_lock
);
133 do_each_task_pid(pgrp
, PIDTYPE_PGID
, p
) {
134 if (p
->signal
->session
> 0) {
135 sid
= p
->signal
->session
;
138 } while_each_task_pid(pgrp
, PIDTYPE_PGID
, p
);
139 p
= find_task_by_pid(pgrp
);
141 sid
= p
->signal
->session
;
143 read_unlock(&tasklist_lock
);
149 * Determine if a process group is "orphaned", according to the POSIX
150 * definition in 2.2.2.52. Orphaned process groups are not to be affected
151 * by terminal-generated stop signals. Newly orphaned process groups are
152 * to receive a SIGHUP and a SIGCONT.
154 * "I ask you, have you ever known what it is to be an orphan?"
156 static int will_become_orphaned_pgrp(int pgrp
, task_t
*ignored_task
)
158 struct task_struct
*p
;
161 do_each_task_pid(pgrp
, PIDTYPE_PGID
, p
) {
162 if (p
== ignored_task
164 || p
->real_parent
->pid
== 1)
166 if (process_group(p
->real_parent
) != pgrp
167 && p
->real_parent
->signal
->session
== p
->signal
->session
) {
171 } while_each_task_pid(pgrp
, PIDTYPE_PGID
, p
);
172 return ret
; /* (sighing) "Often!" */
175 int is_orphaned_pgrp(int pgrp
)
179 read_lock(&tasklist_lock
);
180 retval
= will_become_orphaned_pgrp(pgrp
, NULL
);
181 read_unlock(&tasklist_lock
);
186 static inline int has_stopped_jobs(int pgrp
)
189 struct task_struct
*p
;
191 do_each_task_pid(pgrp
, PIDTYPE_PGID
, p
) {
192 if (p
->state
!= TASK_STOPPED
)
195 /* If p is stopped by a debugger on a signal that won't
196 stop it, then don't count p as stopped. This isn't
197 perfect but it's a good approximation. */
198 if (unlikely (p
->ptrace
)
199 && p
->exit_code
!= SIGSTOP
200 && p
->exit_code
!= SIGTSTP
201 && p
->exit_code
!= SIGTTOU
202 && p
->exit_code
!= SIGTTIN
)
207 } while_each_task_pid(pgrp
, PIDTYPE_PGID
, p
);
212 * reparent_to_init() - Reparent the calling kernel thread to the init task.
214 * If a kernel thread is launched as a result of a system call, or if
215 * it ever exits, it should generally reparent itself to init so that
216 * it is correctly cleaned up on exit.
218 * The various task state such as scheduling policy and priority may have
219 * been inherited from a user process, so we reset them to sane values here.
221 * NOTE that reparent_to_init() gives the caller full capabilities.
223 void reparent_to_init(void)
225 write_lock_irq(&tasklist_lock
);
227 ptrace_unlink(current
);
228 /* Reparent to init */
229 REMOVE_LINKS(current
);
230 current
->parent
= child_reaper
;
231 current
->real_parent
= child_reaper
;
234 /* Set the exit signal to SIGCHLD so we signal init on exit */
235 current
->exit_signal
= SIGCHLD
;
237 if ((current
->policy
== SCHED_NORMAL
) && (task_nice(current
) < 0))
238 set_user_nice(current
, 0);
242 security_task_reparent_to_init(current
);
243 memcpy(current
->signal
->rlim
, init_task
.signal
->rlim
,
244 sizeof(current
->signal
->rlim
));
245 atomic_inc(&(INIT_USER
->__count
));
246 write_unlock_irq(&tasklist_lock
);
247 switch_uid(INIT_USER
);
250 void __set_special_pids(pid_t session
, pid_t pgrp
)
252 struct task_struct
*curr
= current
;
254 if (curr
->signal
->session
!= session
) {
255 detach_pid(curr
, PIDTYPE_SID
);
256 curr
->signal
->session
= session
;
257 attach_pid(curr
, PIDTYPE_SID
, session
);
259 if (process_group(curr
) != pgrp
) {
260 detach_pid(curr
, PIDTYPE_PGID
);
261 curr
->signal
->pgrp
= pgrp
;
262 attach_pid(curr
, PIDTYPE_PGID
, pgrp
);
266 void set_special_pids(pid_t session
, pid_t pgrp
)
268 write_lock_irq(&tasklist_lock
);
269 __set_special_pids(session
, pgrp
);
270 write_unlock_irq(&tasklist_lock
);
274 * Let kernel threads use this to say that they
275 * allow a certain signal (since daemonize() will
276 * have disabled all of them by default).
278 int allow_signal(int sig
)
280 if (sig
< 1 || sig
> _NSIG
)
283 spin_lock_irq(¤t
->sighand
->siglock
);
284 sigdelset(¤t
->blocked
, sig
);
286 /* Kernel threads handle their own signals.
287 Let the signal code know it'll be handled, so
288 that they don't get converted to SIGKILL or
289 just silently dropped */
290 current
->sighand
->action
[(sig
)-1].sa
.sa_handler
= (void __user
*)2;
293 spin_unlock_irq(¤t
->sighand
->siglock
);
297 EXPORT_SYMBOL(allow_signal
);
299 int disallow_signal(int sig
)
301 if (sig
< 1 || sig
> _NSIG
)
304 spin_lock_irq(¤t
->sighand
->siglock
);
305 sigaddset(¤t
->blocked
, sig
);
307 spin_unlock_irq(¤t
->sighand
->siglock
);
311 EXPORT_SYMBOL(disallow_signal
);
314 * Put all the gunge required to become a kernel thread without
315 * attached user resources in one place where it belongs.
318 void daemonize(const char *name
, ...)
321 struct fs_struct
*fs
;
324 va_start(args
, name
);
325 vsnprintf(current
->comm
, sizeof(current
->comm
), name
, args
);
329 * If we were started as result of loading a module, close all of the
330 * user space pages. We don't need them, and if we didn't close them
331 * they would be locked into memory.
335 set_special_pids(1, 1);
337 current
->signal
->tty
= NULL
;
340 /* Block and flush all signals */
341 sigfillset(&blocked
);
342 sigprocmask(SIG_BLOCK
, &blocked
, NULL
);
343 flush_signals(current
);
345 /* Become as one with the init task */
347 exit_fs(current
); /* current->fs->count--; */
350 atomic_inc(&fs
->count
);
352 current
->files
= init_task
.files
;
353 atomic_inc(¤t
->files
->count
);
358 EXPORT_SYMBOL(daemonize
);
360 static inline void close_files(struct files_struct
* files
)
368 if (i
>= files
->max_fdset
|| i
>= files
->max_fds
)
370 set
= files
->open_fds
->fds_bits
[j
++];
373 struct file
* file
= xchg(&files
->fd
[i
], NULL
);
375 filp_close(file
, files
);
383 struct files_struct
*get_files_struct(struct task_struct
*task
)
385 struct files_struct
*files
;
390 atomic_inc(&files
->count
);
396 void fastcall
put_files_struct(struct files_struct
*files
)
398 if (atomic_dec_and_test(&files
->count
)) {
401 * Free the fd and fdset arrays if we expanded them.
403 if (files
->fd
!= &files
->fd_array
[0])
404 free_fd_array(files
->fd
, files
->max_fds
);
405 if (files
->max_fdset
> __FD_SETSIZE
) {
406 free_fdset(files
->open_fds
, files
->max_fdset
);
407 free_fdset(files
->close_on_exec
, files
->max_fdset
);
409 kmem_cache_free(files_cachep
, files
);
413 EXPORT_SYMBOL(put_files_struct
);
415 static inline void __exit_files(struct task_struct
*tsk
)
417 struct files_struct
* files
= tsk
->files
;
423 put_files_struct(files
);
427 void exit_files(struct task_struct
*tsk
)
432 static inline void __put_fs_struct(struct fs_struct
*fs
)
434 /* No need to hold fs->lock if we are killing it */
435 if (atomic_dec_and_test(&fs
->count
)) {
442 mntput(fs
->altrootmnt
);
444 kmem_cache_free(fs_cachep
, fs
);
448 void put_fs_struct(struct fs_struct
*fs
)
453 static inline void __exit_fs(struct task_struct
*tsk
)
455 struct fs_struct
* fs
= tsk
->fs
;
465 void exit_fs(struct task_struct
*tsk
)
470 EXPORT_SYMBOL_GPL(exit_fs
);
473 * Turn us into a lazy TLB process if we
476 void exit_mm(struct task_struct
* tsk
)
478 struct mm_struct
*mm
= tsk
->mm
;
484 * Serialize with any possible pending coredump.
485 * We must hold mmap_sem around checking core_waiters
486 * and clearing tsk->mm. The core-inducing thread
487 * will increment core_waiters for each thread in the
488 * group with ->mm != NULL.
490 down_read(&mm
->mmap_sem
);
491 if (mm
->core_waiters
) {
492 up_read(&mm
->mmap_sem
);
493 down_write(&mm
->mmap_sem
);
494 if (!--mm
->core_waiters
)
495 complete(mm
->core_startup_done
);
496 up_write(&mm
->mmap_sem
);
498 wait_for_completion(&mm
->core_done
);
499 down_read(&mm
->mmap_sem
);
501 atomic_inc(&mm
->mm_count
);
502 if (mm
!= tsk
->active_mm
) BUG();
503 /* more a memory barrier than a real lock */
506 up_read(&mm
->mmap_sem
);
507 enter_lazy_tlb(mm
, current
);
512 static inline void choose_new_parent(task_t
*p
, task_t
*reaper
, task_t
*child_reaper
)
515 * Make sure we're not reparenting to ourselves and that
516 * the parent is not a zombie.
518 BUG_ON(p
== reaper
|| reaper
->exit_state
>= EXIT_ZOMBIE
);
519 p
->real_parent
= reaper
;
520 if (p
->parent
== p
->real_parent
)
524 static inline void reparent_thread(task_t
*p
, task_t
*father
, int traced
)
526 /* We don't want people slaying init. */
527 if (p
->exit_signal
!= -1)
528 p
->exit_signal
= SIGCHLD
;
530 if (p
->pdeath_signal
)
531 /* We already hold the tasklist_lock here. */
532 group_send_sig_info(p
->pdeath_signal
, (void *) 0, p
);
534 /* Move the child from its dying parent to the new one. */
535 if (unlikely(traced
)) {
536 /* Preserve ptrace links if someone else is tracing this child. */
537 list_del_init(&p
->ptrace_list
);
538 if (p
->parent
!= p
->real_parent
)
539 list_add(&p
->ptrace_list
, &p
->real_parent
->ptrace_children
);
541 /* If this child is being traced, then we're the one tracing it
542 * anyway, so let go of it.
545 list_del_init(&p
->sibling
);
546 p
->parent
= p
->real_parent
;
547 list_add_tail(&p
->sibling
, &p
->parent
->children
);
549 /* If we'd notified the old parent about this child's death,
550 * also notify the new parent.
552 if (p
->exit_state
== EXIT_ZOMBIE
&& p
->exit_signal
!= -1 &&
553 thread_group_empty(p
))
554 do_notify_parent(p
, p
->exit_signal
);
555 else if (p
->state
== TASK_TRACED
) {
557 * If it was at a trace stop, turn it into
558 * a normal stop since it's no longer being
566 * process group orphan check
567 * Case ii: Our child is in a different pgrp
568 * than we are, and it was the only connection
569 * outside, so the child pgrp is now orphaned.
571 if ((process_group(p
) != process_group(father
)) &&
572 (p
->signal
->session
== father
->signal
->session
)) {
573 int pgrp
= process_group(p
);
575 if (will_become_orphaned_pgrp(pgrp
, NULL
) && has_stopped_jobs(pgrp
)) {
576 __kill_pg_info(SIGHUP
, (void *)1, pgrp
);
577 __kill_pg_info(SIGCONT
, (void *)1, pgrp
);
583 * When we die, we re-parent all our children.
584 * Try to give them to another thread in our thread
585 * group, and if no such member exists, give it to
586 * the global child reaper process (ie "init")
588 static inline void forget_original_parent(struct task_struct
* father
,
589 struct list_head
*to_release
)
591 struct task_struct
*p
, *reaper
= father
;
592 struct list_head
*_p
, *_n
;
595 reaper
= next_thread(reaper
);
596 if (reaper
== father
) {
597 reaper
= child_reaper
;
600 } while (reaper
->exit_state
);
603 * There are only two places where our children can be:
605 * - in our child list
606 * - in our ptraced child list
608 * Search them and reparent children.
610 list_for_each_safe(_p
, _n
, &father
->children
) {
612 p
= list_entry(_p
,struct task_struct
,sibling
);
616 /* if father isn't the real parent, then ptrace must be enabled */
617 BUG_ON(father
!= p
->real_parent
&& !ptrace
);
619 if (father
== p
->real_parent
) {
620 /* reparent with a reaper, real father it's us */
621 choose_new_parent(p
, reaper
, child_reaper
);
622 reparent_thread(p
, father
, 0);
624 /* reparent ptraced task to its real parent */
626 if (p
->exit_state
== EXIT_ZOMBIE
&& p
->exit_signal
!= -1 &&
627 thread_group_empty(p
))
628 do_notify_parent(p
, p
->exit_signal
);
632 * if the ptraced child is a zombie with exit_signal == -1
633 * we must collect it before we exit, or it will remain
634 * zombie forever since we prevented it from self-reap itself
635 * while it was being traced by us, to be able to see it in wait4.
637 if (unlikely(ptrace
&& p
->exit_state
== EXIT_ZOMBIE
&& p
->exit_signal
== -1))
638 list_add(&p
->ptrace_list
, to_release
);
640 list_for_each_safe(_p
, _n
, &father
->ptrace_children
) {
641 p
= list_entry(_p
,struct task_struct
,ptrace_list
);
642 choose_new_parent(p
, reaper
, child_reaper
);
643 reparent_thread(p
, father
, 1);
648 * Send signals to all our closest relatives so that they know
649 * to properly mourn us..
651 static void exit_notify(struct task_struct
*tsk
)
654 struct task_struct
*t
;
655 struct list_head ptrace_dead
, *_p
, *_n
;
657 if (signal_pending(tsk
) && !(tsk
->signal
->flags
& SIGNAL_GROUP_EXIT
)
658 && !thread_group_empty(tsk
)) {
660 * This occurs when there was a race between our exit
661 * syscall and a group signal choosing us as the one to
662 * wake up. It could be that we are the only thread
663 * alerted to check for pending signals, but another thread
664 * should be woken now to take the signal since we will not.
665 * Now we'll wake all the threads in the group just to make
666 * sure someone gets all the pending signals.
668 read_lock(&tasklist_lock
);
669 spin_lock_irq(&tsk
->sighand
->siglock
);
670 for (t
= next_thread(tsk
); t
!= tsk
; t
= next_thread(t
))
671 if (!signal_pending(t
) && !(t
->flags
& PF_EXITING
)) {
672 recalc_sigpending_tsk(t
);
673 if (signal_pending(t
))
674 signal_wake_up(t
, 0);
676 spin_unlock_irq(&tsk
->sighand
->siglock
);
677 read_unlock(&tasklist_lock
);
680 write_lock_irq(&tasklist_lock
);
683 * This does two things:
685 * A. Make init inherit all the child processes
686 * B. Check to see if any process groups have become orphaned
687 * as a result of our exiting, and if they have any stopped
688 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
691 INIT_LIST_HEAD(&ptrace_dead
);
692 forget_original_parent(tsk
, &ptrace_dead
);
693 BUG_ON(!list_empty(&tsk
->children
));
694 BUG_ON(!list_empty(&tsk
->ptrace_children
));
697 * Check to see if any process groups have become orphaned
698 * as a result of our exiting, and if they have any stopped
699 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
701 * Case i: Our father is in a different pgrp than we are
702 * and we were the only connection outside, so our pgrp
703 * is about to become orphaned.
706 t
= tsk
->real_parent
;
708 if ((process_group(t
) != process_group(tsk
)) &&
709 (t
->signal
->session
== tsk
->signal
->session
) &&
710 will_become_orphaned_pgrp(process_group(tsk
), tsk
) &&
711 has_stopped_jobs(process_group(tsk
))) {
712 __kill_pg_info(SIGHUP
, (void *)1, process_group(tsk
));
713 __kill_pg_info(SIGCONT
, (void *)1, process_group(tsk
));
716 /* Let father know we died
718 * Thread signals are configurable, but you aren't going to use
719 * that to send signals to arbitary processes.
720 * That stops right now.
722 * If the parent exec id doesn't match the exec id we saved
723 * when we started then we know the parent has changed security
726 * If our self_exec id doesn't match our parent_exec_id then
727 * we have changed execution domain as these two values started
728 * the same after a fork.
732 if (tsk
->exit_signal
!= SIGCHLD
&& tsk
->exit_signal
!= -1 &&
733 ( tsk
->parent_exec_id
!= t
->self_exec_id
||
734 tsk
->self_exec_id
!= tsk
->parent_exec_id
)
735 && !capable(CAP_KILL
))
736 tsk
->exit_signal
= SIGCHLD
;
739 /* If something other than our normal parent is ptracing us, then
740 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
741 * only has special meaning to our real parent.
743 if (tsk
->exit_signal
!= -1 && thread_group_empty(tsk
)) {
744 int signal
= tsk
->parent
== tsk
->real_parent
? tsk
->exit_signal
: SIGCHLD
;
745 do_notify_parent(tsk
, signal
);
746 } else if (tsk
->ptrace
) {
747 do_notify_parent(tsk
, SIGCHLD
);
751 if (tsk
->exit_signal
== -1 &&
752 (likely(tsk
->ptrace
== 0) ||
753 unlikely(tsk
->parent
->signal
->flags
& SIGNAL_GROUP_EXIT
)))
755 tsk
->exit_state
= state
;
757 write_unlock_irq(&tasklist_lock
);
759 list_for_each_safe(_p
, _n
, &ptrace_dead
) {
761 t
= list_entry(_p
,struct task_struct
,ptrace_list
);
765 /* If the process is dead, release it - nobody will wait for it */
766 if (state
== EXIT_DEAD
)
769 /* PF_DEAD causes final put_task_struct after we schedule. */
771 tsk
->flags
|= PF_DEAD
;
774 fastcall NORET_TYPE
void do_exit(long code
)
776 struct task_struct
*tsk
= current
;
779 profile_task_exit(tsk
);
781 if (unlikely(in_interrupt()))
782 panic("Aiee, killing interrupt handler!");
783 if (unlikely(!tsk
->pid
))
784 panic("Attempted to kill the idle task!");
785 if (unlikely(tsk
->pid
== 1))
786 panic("Attempted to kill init!");
790 if (unlikely(current
->ptrace
& PT_TRACE_EXIT
)) {
791 current
->ptrace_message
= code
;
792 ptrace_notify((PTRACE_EVENT_EXIT
<< 8) | SIGTRAP
);
795 tsk
->flags
|= PF_EXITING
;
798 * Make sure we don't try to process any timer firings
799 * while we are already exiting.
801 tsk
->it_virt_expires
= cputime_zero
;
802 tsk
->it_prof_expires
= cputime_zero
;
803 tsk
->it_sched_expires
= 0;
805 if (unlikely(in_atomic()))
806 printk(KERN_INFO
"note: %s[%d] exited with preempt_count %d\n",
807 current
->comm
, current
->pid
,
810 acct_update_integrals(tsk
);
811 update_mem_hiwater(tsk
);
812 group_dead
= atomic_dec_and_test(&tsk
->signal
->live
);
814 del_timer_sync(&tsk
->signal
->real_timer
);
827 if (group_dead
&& tsk
->signal
->leader
)
828 disassociate_ctty(1);
830 module_put(tsk
->thread_info
->exec_domain
->module
);
832 module_put(tsk
->binfmt
->module
);
834 tsk
->exit_code
= code
;
837 mpol_free(tsk
->mempolicy
);
838 tsk
->mempolicy
= NULL
;
841 BUG_ON(!(current
->flags
& PF_DEAD
));
844 /* Avoid "noreturn function does return". */
848 NORET_TYPE
void complete_and_exit(struct completion
*comp
, long code
)
856 EXPORT_SYMBOL(complete_and_exit
);
858 asmlinkage
long sys_exit(int error_code
)
860 do_exit((error_code
&0xff)<<8);
863 task_t fastcall
*next_thread(const task_t
*p
)
865 return pid_task(p
->pids
[PIDTYPE_TGID
].pid_list
.next
, PIDTYPE_TGID
);
868 EXPORT_SYMBOL(next_thread
);
871 * Take down every thread in the group. This is called by fatal signals
872 * as well as by sys_exit_group (below).
875 do_group_exit(int exit_code
)
877 BUG_ON(exit_code
& 0x80); /* core dumps don't get here */
879 if (current
->signal
->flags
& SIGNAL_GROUP_EXIT
)
880 exit_code
= current
->signal
->group_exit_code
;
881 else if (!thread_group_empty(current
)) {
882 struct signal_struct
*const sig
= current
->signal
;
883 struct sighand_struct
*const sighand
= current
->sighand
;
884 read_lock(&tasklist_lock
);
885 spin_lock_irq(&sighand
->siglock
);
886 if (sig
->flags
& SIGNAL_GROUP_EXIT
)
887 /* Another thread got here before we took the lock. */
888 exit_code
= sig
->group_exit_code
;
890 sig
->flags
= SIGNAL_GROUP_EXIT
;
891 sig
->group_exit_code
= exit_code
;
892 zap_other_threads(current
);
894 spin_unlock_irq(&sighand
->siglock
);
895 read_unlock(&tasklist_lock
);
903 * this kills every thread in the thread group. Note that any externally
904 * wait4()-ing process will get the correct exit code - even if this
905 * thread is not the thread group leader.
907 asmlinkage
void sys_exit_group(int error_code
)
909 do_group_exit((error_code
& 0xff) << 8);
912 static int eligible_child(pid_t pid
, int options
, task_t
*p
)
918 if (process_group(p
) != process_group(current
))
920 } else if (pid
!= -1) {
921 if (process_group(p
) != -pid
)
926 * Do not consider detached threads that are
929 if (p
->exit_signal
== -1 && !p
->ptrace
)
932 /* Wait for all children (clone and not) if __WALL is set;
933 * otherwise, wait for clone children *only* if __WCLONE is
934 * set; otherwise, wait for non-clone children *only*. (Note:
935 * A "clone" child here is one that reports to its parent
936 * using a signal other than SIGCHLD.) */
937 if (((p
->exit_signal
!= SIGCHLD
) ^ ((options
& __WCLONE
) != 0))
938 && !(options
& __WALL
))
941 * Do not consider thread group leaders that are
942 * in a non-empty thread group:
944 if (current
->tgid
!= p
->tgid
&& delay_group_leader(p
))
947 if (security_task_wait(p
))
953 static int wait_noreap_copyout(task_t
*p
, pid_t pid
, uid_t uid
,
955 struct siginfo __user
*infop
,
956 struct rusage __user
*rusagep
)
958 int retval
= rusagep
? getrusage(p
, RUSAGE_BOTH
, rusagep
) : 0;
961 retval
= put_user(SIGCHLD
, &infop
->si_signo
);
963 retval
= put_user(0, &infop
->si_errno
);
965 retval
= put_user((short)why
, &infop
->si_code
);
967 retval
= put_user(pid
, &infop
->si_pid
);
969 retval
= put_user(uid
, &infop
->si_uid
);
971 retval
= put_user(status
, &infop
->si_status
);
978 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
979 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
980 * the lock and this task is uninteresting. If we return nonzero, we have
981 * released the lock and the system call should return.
983 static int wait_task_zombie(task_t
*p
, int noreap
,
984 struct siginfo __user
*infop
,
985 int __user
*stat_addr
, struct rusage __user
*ru
)
991 if (unlikely(noreap
)) {
994 int exit_code
= p
->exit_code
;
997 if (unlikely(p
->exit_state
!= EXIT_ZOMBIE
))
999 if (unlikely(p
->exit_signal
== -1 && p
->ptrace
== 0))
1002 read_unlock(&tasklist_lock
);
1003 if ((exit_code
& 0x7f) == 0) {
1005 status
= exit_code
>> 8;
1007 why
= (exit_code
& 0x80) ? CLD_DUMPED
: CLD_KILLED
;
1008 status
= exit_code
& 0x7f;
1010 return wait_noreap_copyout(p
, pid
, uid
, why
,
1015 * Try to move the task's state to DEAD
1016 * only one thread is allowed to do this:
1018 state
= xchg(&p
->exit_state
, EXIT_DEAD
);
1019 if (state
!= EXIT_ZOMBIE
) {
1020 BUG_ON(state
!= EXIT_DEAD
);
1023 if (unlikely(p
->exit_signal
== -1 && p
->ptrace
== 0)) {
1025 * This can only happen in a race with a ptraced thread
1026 * dying on another processor.
1031 if (likely(p
->real_parent
== p
->parent
) && likely(p
->signal
)) {
1033 * The resource counters for the group leader are in its
1034 * own task_struct. Those for dead threads in the group
1035 * are in its signal_struct, as are those for the child
1036 * processes it has previously reaped. All these
1037 * accumulate in the parent's signal_struct c* fields.
1039 * We don't bother to take a lock here to protect these
1040 * p->signal fields, because they are only touched by
1041 * __exit_signal, which runs with tasklist_lock
1042 * write-locked anyway, and so is excluded here. We do
1043 * need to protect the access to p->parent->signal fields,
1044 * as other threads in the parent group can be right
1045 * here reaping other children at the same time.
1047 spin_lock_irq(&p
->parent
->sighand
->siglock
);
1048 p
->parent
->signal
->cutime
=
1049 cputime_add(p
->parent
->signal
->cutime
,
1050 cputime_add(p
->utime
,
1051 cputime_add(p
->signal
->utime
,
1052 p
->signal
->cutime
)));
1053 p
->parent
->signal
->cstime
=
1054 cputime_add(p
->parent
->signal
->cstime
,
1055 cputime_add(p
->stime
,
1056 cputime_add(p
->signal
->stime
,
1057 p
->signal
->cstime
)));
1058 p
->parent
->signal
->cmin_flt
+=
1059 p
->min_flt
+ p
->signal
->min_flt
+ p
->signal
->cmin_flt
;
1060 p
->parent
->signal
->cmaj_flt
+=
1061 p
->maj_flt
+ p
->signal
->maj_flt
+ p
->signal
->cmaj_flt
;
1062 p
->parent
->signal
->cnvcsw
+=
1063 p
->nvcsw
+ p
->signal
->nvcsw
+ p
->signal
->cnvcsw
;
1064 p
->parent
->signal
->cnivcsw
+=
1065 p
->nivcsw
+ p
->signal
->nivcsw
+ p
->signal
->cnivcsw
;
1066 spin_unlock_irq(&p
->parent
->sighand
->siglock
);
1070 * Now we are sure this task is interesting, and no other
1071 * thread can reap it because we set its state to EXIT_DEAD.
1073 read_unlock(&tasklist_lock
);
1075 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
1076 status
= (p
->signal
->flags
& SIGNAL_GROUP_EXIT
)
1077 ? p
->signal
->group_exit_code
: p
->exit_code
;
1078 if (!retval
&& stat_addr
)
1079 retval
= put_user(status
, stat_addr
);
1080 if (!retval
&& infop
)
1081 retval
= put_user(SIGCHLD
, &infop
->si_signo
);
1082 if (!retval
&& infop
)
1083 retval
= put_user(0, &infop
->si_errno
);
1084 if (!retval
&& infop
) {
1087 if ((status
& 0x7f) == 0) {
1091 why
= (status
& 0x80) ? CLD_DUMPED
: CLD_KILLED
;
1094 retval
= put_user((short)why
, &infop
->si_code
);
1096 retval
= put_user(status
, &infop
->si_status
);
1098 if (!retval
&& infop
)
1099 retval
= put_user(p
->pid
, &infop
->si_pid
);
1100 if (!retval
&& infop
)
1101 retval
= put_user(p
->uid
, &infop
->si_uid
);
1103 // TODO: is this safe?
1104 p
->exit_state
= EXIT_ZOMBIE
;
1108 if (p
->real_parent
!= p
->parent
) {
1109 write_lock_irq(&tasklist_lock
);
1110 /* Double-check with lock held. */
1111 if (p
->real_parent
!= p
->parent
) {
1113 // TODO: is this safe?
1114 p
->exit_state
= EXIT_ZOMBIE
;
1116 * If this is not a detached task, notify the parent.
1117 * If it's still not detached after that, don't release
1120 if (p
->exit_signal
!= -1) {
1121 do_notify_parent(p
, p
->exit_signal
);
1122 if (p
->exit_signal
!= -1)
1126 write_unlock_irq(&tasklist_lock
);
1135 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1136 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1137 * the lock and this task is uninteresting. If we return nonzero, we have
1138 * released the lock and the system call should return.
1140 static int wait_task_stopped(task_t
*p
, int delayed_group_leader
, int noreap
,
1141 struct siginfo __user
*infop
,
1142 int __user
*stat_addr
, struct rusage __user
*ru
)
1144 int retval
, exit_code
;
1148 if (delayed_group_leader
&& !(p
->ptrace
& PT_PTRACED
) &&
1149 p
->signal
&& p
->signal
->group_stop_count
> 0)
1151 * A group stop is in progress and this is the group leader.
1152 * We won't report until all threads have stopped.
1157 * Now we are pretty sure this task is interesting.
1158 * Make sure it doesn't get reaped out from under us while we
1159 * give up the lock and then examine it below. We don't want to
1160 * keep holding onto the tasklist_lock while we call getrusage and
1161 * possibly take page faults for user memory.
1164 read_unlock(&tasklist_lock
);
1166 if (unlikely(noreap
)) {
1169 int why
= (p
->ptrace
& PT_PTRACED
) ? CLD_TRAPPED
: CLD_STOPPED
;
1171 exit_code
= p
->exit_code
;
1172 if (unlikely(!exit_code
) ||
1173 unlikely(p
->state
> TASK_STOPPED
))
1175 return wait_noreap_copyout(p
, pid
, uid
,
1176 why
, (exit_code
<< 8) | 0x7f,
1180 write_lock_irq(&tasklist_lock
);
1183 * This uses xchg to be atomic with the thread resuming and setting
1184 * it. It must also be done with the write lock held to prevent a
1185 * race with the EXIT_ZOMBIE case.
1187 exit_code
= xchg(&p
->exit_code
, 0);
1188 if (unlikely(p
->exit_state
)) {
1190 * The task resumed and then died. Let the next iteration
1191 * catch it in EXIT_ZOMBIE. Note that exit_code might
1192 * already be zero here if it resumed and did _exit(0).
1193 * The task itself is dead and won't touch exit_code again;
1194 * other processors in this function are locked out.
1196 p
->exit_code
= exit_code
;
1199 if (unlikely(exit_code
== 0)) {
1201 * Another thread in this function got to it first, or it
1202 * resumed, or it resumed and then died.
1204 write_unlock_irq(&tasklist_lock
);
1208 * We are returning to the wait loop without having successfully
1209 * removed the process and having released the lock. We cannot
1210 * continue, since the "p" task pointer is potentially stale.
1212 * Return -EAGAIN, and do_wait() will restart the loop from the
1213 * beginning. Do _not_ re-acquire the lock.
1218 /* move to end of parent's list to avoid starvation */
1220 add_parent(p
, p
->parent
);
1222 write_unlock_irq(&tasklist_lock
);
1224 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
1225 if (!retval
&& stat_addr
)
1226 retval
= put_user((exit_code
<< 8) | 0x7f, stat_addr
);
1227 if (!retval
&& infop
)
1228 retval
= put_user(SIGCHLD
, &infop
->si_signo
);
1229 if (!retval
&& infop
)
1230 retval
= put_user(0, &infop
->si_errno
);
1231 if (!retval
&& infop
)
1232 retval
= put_user((short)((p
->ptrace
& PT_PTRACED
)
1233 ? CLD_TRAPPED
: CLD_STOPPED
),
1235 if (!retval
&& infop
)
1236 retval
= put_user(exit_code
, &infop
->si_status
);
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
);
1250 * Handle do_wait work for one task in a live, non-stopped state.
1251 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1252 * the lock and this task is uninteresting. If we return nonzero, we have
1253 * released the lock and the system call should return.
1255 static int wait_task_continued(task_t
*p
, int noreap
,
1256 struct siginfo __user
*infop
,
1257 int __user
*stat_addr
, struct rusage __user
*ru
)
1263 if (unlikely(!p
->signal
))
1266 if (!(p
->signal
->flags
& SIGNAL_STOP_CONTINUED
))
1269 spin_lock_irq(&p
->sighand
->siglock
);
1270 /* Re-check with the lock held. */
1271 if (!(p
->signal
->flags
& SIGNAL_STOP_CONTINUED
)) {
1272 spin_unlock_irq(&p
->sighand
->siglock
);
1276 p
->signal
->flags
&= ~SIGNAL_STOP_CONTINUED
;
1277 spin_unlock_irq(&p
->sighand
->siglock
);
1282 read_unlock(&tasklist_lock
);
1285 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
1287 if (!retval
&& stat_addr
)
1288 retval
= put_user(0xffff, stat_addr
);
1292 retval
= wait_noreap_copyout(p
, pid
, uid
,
1293 CLD_CONTINUED
, SIGCONT
,
1295 BUG_ON(retval
== 0);
1302 static inline int my_ptrace_child(struct task_struct
*p
)
1304 if (!(p
->ptrace
& PT_PTRACED
))
1306 if (!(p
->ptrace
& PT_ATTACHED
))
1309 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1310 * we are the attacher. If we are the real parent, this is a race
1311 * inside ptrace_attach. It is waiting for the tasklist_lock,
1312 * which we have to switch the parent links, but has already set
1313 * the flags in p->ptrace.
1315 return (p
->parent
!= p
->real_parent
);
1318 static long do_wait(pid_t pid
, int options
, struct siginfo __user
*infop
,
1319 int __user
*stat_addr
, struct rusage __user
*ru
)
1321 DECLARE_WAITQUEUE(wait
, current
);
1322 struct task_struct
*tsk
;
1325 add_wait_queue(¤t
->signal
->wait_chldexit
,&wait
);
1328 * We will set this flag if we see any child that might later
1329 * match our criteria, even if we are not able to reap it yet.
1332 current
->state
= TASK_INTERRUPTIBLE
;
1333 read_lock(&tasklist_lock
);
1336 struct task_struct
*p
;
1337 struct list_head
*_p
;
1340 list_for_each(_p
,&tsk
->children
) {
1341 p
= list_entry(_p
,struct task_struct
,sibling
);
1343 ret
= eligible_child(pid
, options
, p
);
1349 if (!my_ptrace_child(p
))
1354 * It's stopped now, so it might later
1355 * continue, exit, or stop again.
1358 if (!(options
& WUNTRACED
) &&
1359 !my_ptrace_child(p
))
1361 retval
= wait_task_stopped(p
, ret
== 2,
1362 (options
& WNOWAIT
),
1365 if (retval
== -EAGAIN
)
1367 if (retval
!= 0) /* He released the lock. */
1372 if (p
->exit_state
== EXIT_DEAD
)
1374 // case EXIT_ZOMBIE:
1375 if (p
->exit_state
== EXIT_ZOMBIE
) {
1377 * Eligible but we cannot release
1381 goto check_continued
;
1382 if (!likely(options
& WEXITED
))
1384 retval
= wait_task_zombie(
1385 p
, (options
& WNOWAIT
),
1386 infop
, stat_addr
, ru
);
1387 /* He released the lock. */
1394 * It's running now, so it might later
1395 * exit, stop, or stop and then continue.
1398 if (!unlikely(options
& WCONTINUED
))
1400 retval
= wait_task_continued(
1401 p
, (options
& WNOWAIT
),
1402 infop
, stat_addr
, ru
);
1403 if (retval
!= 0) /* He released the lock. */
1409 list_for_each(_p
, &tsk
->ptrace_children
) {
1410 p
= list_entry(_p
, struct task_struct
,
1412 if (!eligible_child(pid
, options
, p
))
1418 if (options
& __WNOTHREAD
)
1420 tsk
= next_thread(tsk
);
1421 if (tsk
->signal
!= current
->signal
)
1423 } while (tsk
!= current
);
1425 read_unlock(&tasklist_lock
);
1428 if (options
& WNOHANG
)
1430 retval
= -ERESTARTSYS
;
1431 if (signal_pending(current
))
1438 current
->state
= TASK_RUNNING
;
1439 remove_wait_queue(¤t
->signal
->wait_chldexit
,&wait
);
1445 * For a WNOHANG return, clear out all the fields
1446 * we would set so the user can easily tell the
1450 retval
= put_user(0, &infop
->si_signo
);
1452 retval
= put_user(0, &infop
->si_errno
);
1454 retval
= put_user(0, &infop
->si_code
);
1456 retval
= put_user(0, &infop
->si_pid
);
1458 retval
= put_user(0, &infop
->si_uid
);
1460 retval
= put_user(0, &infop
->si_status
);
1466 asmlinkage
long sys_waitid(int which
, pid_t pid
,
1467 struct siginfo __user
*infop
, int options
,
1468 struct rusage __user
*ru
)
1472 if (options
& ~(WNOHANG
|WNOWAIT
|WEXITED
|WSTOPPED
|WCONTINUED
))
1474 if (!(options
& (WEXITED
|WSTOPPED
|WCONTINUED
)))
1494 ret
= do_wait(pid
, options
, infop
, NULL
, ru
);
1496 /* avoid REGPARM breakage on x86: */
1497 prevent_tail_call(ret
);
1501 asmlinkage
long sys_wait4(pid_t pid
, int __user
*stat_addr
,
1502 int options
, struct rusage __user
*ru
)
1506 if (options
& ~(WNOHANG
|WUNTRACED
|WCONTINUED
|
1507 __WNOTHREAD
|__WCLONE
|__WALL
))
1509 ret
= do_wait(pid
, options
| WEXITED
, NULL
, stat_addr
, ru
);
1511 /* avoid REGPARM breakage on x86: */
1512 prevent_tail_call(ret
);
1516 #ifdef __ARCH_WANT_SYS_WAITPID
1519 * sys_waitpid() remains for compatibility. waitpid() should be
1520 * implemented by calling sys_wait4() from libc.a.
1522 asmlinkage
long sys_waitpid(pid_t pid
, int __user
*stat_addr
, int options
)
1524 return sys_wait4(pid
, stat_addr
, options
, NULL
);