]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - kernel/exit.c
802722814925d6391bb021d99106314c7c6b085e
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>
30 #include <linux/signal.h>
31 #include <linux/cn_proc.h>
32 #include <linux/mutex.h>
34 #include <asm/uaccess.h>
35 #include <asm/unistd.h>
36 #include <asm/pgtable.h>
37 #include <asm/mmu_context.h>
39 extern void sem_exit (void);
40 extern struct task_struct
*child_reaper
;
42 int getrusage(struct task_struct
*, int, struct rusage __user
*);
44 static void exit_mm(struct task_struct
* tsk
);
46 static void __unhash_process(struct task_struct
*p
)
49 detach_pid(p
, PIDTYPE_PID
);
50 detach_pid(p
, PIDTYPE_TGID
);
51 if (thread_group_leader(p
)) {
52 detach_pid(p
, PIDTYPE_PGID
);
53 detach_pid(p
, PIDTYPE_SID
);
55 __get_cpu_var(process_counts
)--;
61 void release_task(struct task_struct
* p
)
65 struct dentry
*proc_dentry
;
68 atomic_dec(&p
->user
->processes
);
69 spin_lock(&p
->proc_lock
);
70 proc_dentry
= proc_pid_unhash(p
);
71 write_lock_irq(&tasklist_lock
);
72 if (unlikely(p
->ptrace
))
74 BUG_ON(!list_empty(&p
->ptrace_list
) || !list_empty(&p
->ptrace_children
));
77 * Note that the fastpath in sys_times depends on __exit_signal having
78 * updated the counters before a task is removed from the tasklist of
79 * the process by __unhash_process.
84 * If we are the last non-leader member of the thread
85 * group, and the leader is zombie, then notify the
86 * group leader's parent process. (if it wants notification.)
89 leader
= p
->group_leader
;
90 if (leader
!= p
&& thread_group_empty(leader
) && leader
->exit_state
== EXIT_ZOMBIE
) {
91 BUG_ON(leader
->exit_signal
== -1);
92 do_notify_parent(leader
, leader
->exit_signal
);
94 * If we were the last child thread and the leader has
95 * exited already, and the leader's parent ignores SIGCHLD,
96 * then we are the one who should release the leader.
98 * do_notify_parent() will have marked it self-reaping in
101 zap_leader
= (leader
->exit_signal
== -1);
105 write_unlock_irq(&tasklist_lock
);
106 spin_unlock(&p
->proc_lock
);
107 proc_pid_flush(proc_dentry
);
112 if (unlikely(zap_leader
))
116 /* we are using it only for SMP init */
118 void unhash_process(struct task_struct
*p
)
120 struct dentry
*proc_dentry
;
122 spin_lock(&p
->proc_lock
);
123 proc_dentry
= proc_pid_unhash(p
);
124 write_lock_irq(&tasklist_lock
);
126 write_unlock_irq(&tasklist_lock
);
127 spin_unlock(&p
->proc_lock
);
128 proc_pid_flush(proc_dentry
);
132 * This checks not only the pgrp, but falls back on the pid if no
133 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
136 int session_of_pgrp(int pgrp
)
138 struct task_struct
*p
;
141 read_lock(&tasklist_lock
);
142 do_each_task_pid(pgrp
, PIDTYPE_PGID
, p
) {
143 if (p
->signal
->session
> 0) {
144 sid
= p
->signal
->session
;
147 } while_each_task_pid(pgrp
, PIDTYPE_PGID
, p
);
148 p
= find_task_by_pid(pgrp
);
150 sid
= p
->signal
->session
;
152 read_unlock(&tasklist_lock
);
158 * Determine if a process group is "orphaned", according to the POSIX
159 * definition in 2.2.2.52. Orphaned process groups are not to be affected
160 * by terminal-generated stop signals. Newly orphaned process groups are
161 * to receive a SIGHUP and a SIGCONT.
163 * "I ask you, have you ever known what it is to be an orphan?"
165 static int will_become_orphaned_pgrp(int pgrp
, task_t
*ignored_task
)
167 struct task_struct
*p
;
170 do_each_task_pid(pgrp
, PIDTYPE_PGID
, p
) {
171 if (p
== ignored_task
173 || p
->real_parent
->pid
== 1)
175 if (process_group(p
->real_parent
) != pgrp
176 && p
->real_parent
->signal
->session
== p
->signal
->session
) {
180 } while_each_task_pid(pgrp
, PIDTYPE_PGID
, p
);
181 return ret
; /* (sighing) "Often!" */
184 int is_orphaned_pgrp(int pgrp
)
188 read_lock(&tasklist_lock
);
189 retval
= will_become_orphaned_pgrp(pgrp
, NULL
);
190 read_unlock(&tasklist_lock
);
195 static inline int has_stopped_jobs(int pgrp
)
198 struct task_struct
*p
;
200 do_each_task_pid(pgrp
, PIDTYPE_PGID
, p
) {
201 if (p
->state
!= TASK_STOPPED
)
204 /* If p is stopped by a debugger on a signal that won't
205 stop it, then don't count p as stopped. This isn't
206 perfect but it's a good approximation. */
207 if (unlikely (p
->ptrace
)
208 && p
->exit_code
!= SIGSTOP
209 && p
->exit_code
!= SIGTSTP
210 && p
->exit_code
!= SIGTTOU
211 && p
->exit_code
!= SIGTTIN
)
216 } while_each_task_pid(pgrp
, PIDTYPE_PGID
, p
);
221 * reparent_to_init - Reparent the calling kernel thread to the init task.
223 * If a kernel thread is launched as a result of a system call, or if
224 * it ever exits, it should generally reparent itself to init so that
225 * it is correctly cleaned up on exit.
227 * The various task state such as scheduling policy and priority may have
228 * been inherited from a user process, so we reset them to sane values here.
230 * NOTE that reparent_to_init() gives the caller full capabilities.
232 static inline void reparent_to_init(void)
234 write_lock_irq(&tasklist_lock
);
236 ptrace_unlink(current
);
237 /* Reparent to init */
238 REMOVE_LINKS(current
);
239 current
->parent
= child_reaper
;
240 current
->real_parent
= child_reaper
;
243 /* Set the exit signal to SIGCHLD so we signal init on exit */
244 current
->exit_signal
= SIGCHLD
;
246 if ((current
->policy
== SCHED_NORMAL
) && (task_nice(current
) < 0))
247 set_user_nice(current
, 0);
251 security_task_reparent_to_init(current
);
252 memcpy(current
->signal
->rlim
, init_task
.signal
->rlim
,
253 sizeof(current
->signal
->rlim
));
254 atomic_inc(&(INIT_USER
->__count
));
255 write_unlock_irq(&tasklist_lock
);
256 switch_uid(INIT_USER
);
259 void __set_special_pids(pid_t session
, pid_t pgrp
)
261 struct task_struct
*curr
= current
->group_leader
;
263 if (curr
->signal
->session
!= session
) {
264 detach_pid(curr
, PIDTYPE_SID
);
265 curr
->signal
->session
= session
;
266 attach_pid(curr
, PIDTYPE_SID
, session
);
268 if (process_group(curr
) != pgrp
) {
269 detach_pid(curr
, PIDTYPE_PGID
);
270 curr
->signal
->pgrp
= pgrp
;
271 attach_pid(curr
, PIDTYPE_PGID
, pgrp
);
275 void set_special_pids(pid_t session
, pid_t pgrp
)
277 write_lock_irq(&tasklist_lock
);
278 __set_special_pids(session
, pgrp
);
279 write_unlock_irq(&tasklist_lock
);
283 * Let kernel threads use this to say that they
284 * allow a certain signal (since daemonize() will
285 * have disabled all of them by default).
287 int allow_signal(int sig
)
289 if (!valid_signal(sig
) || sig
< 1)
292 spin_lock_irq(¤t
->sighand
->siglock
);
293 sigdelset(¤t
->blocked
, sig
);
295 /* Kernel threads handle their own signals.
296 Let the signal code know it'll be handled, so
297 that they don't get converted to SIGKILL or
298 just silently dropped */
299 current
->sighand
->action
[(sig
)-1].sa
.sa_handler
= (void __user
*)2;
302 spin_unlock_irq(¤t
->sighand
->siglock
);
306 EXPORT_SYMBOL(allow_signal
);
308 int disallow_signal(int sig
)
310 if (!valid_signal(sig
) || sig
< 1)
313 spin_lock_irq(¤t
->sighand
->siglock
);
314 sigaddset(¤t
->blocked
, sig
);
316 spin_unlock_irq(¤t
->sighand
->siglock
);
320 EXPORT_SYMBOL(disallow_signal
);
323 * Put all the gunge required to become a kernel thread without
324 * attached user resources in one place where it belongs.
327 void daemonize(const char *name
, ...)
330 struct fs_struct
*fs
;
333 va_start(args
, name
);
334 vsnprintf(current
->comm
, sizeof(current
->comm
), name
, args
);
338 * If we were started as result of loading a module, close all of the
339 * user space pages. We don't need them, and if we didn't close them
340 * they would be locked into memory.
344 set_special_pids(1, 1);
346 current
->signal
->tty
= NULL
;
349 /* Block and flush all signals */
350 sigfillset(&blocked
);
351 sigprocmask(SIG_BLOCK
, &blocked
, NULL
);
352 flush_signals(current
);
354 /* Become as one with the init task */
356 exit_fs(current
); /* current->fs->count--; */
359 atomic_inc(&fs
->count
);
361 current
->files
= init_task
.files
;
362 atomic_inc(¤t
->files
->count
);
367 EXPORT_SYMBOL(daemonize
);
369 static inline void close_files(struct files_struct
* files
)
377 * It is safe to dereference the fd table without RCU or
378 * ->file_lock because this is the last reference to the
381 fdt
= files_fdtable(files
);
385 if (i
>= fdt
->max_fdset
|| i
>= fdt
->max_fds
)
387 set
= fdt
->open_fds
->fds_bits
[j
++];
390 struct file
* file
= xchg(&fdt
->fd
[i
], NULL
);
392 filp_close(file
, files
);
400 struct files_struct
*get_files_struct(struct task_struct
*task
)
402 struct files_struct
*files
;
407 atomic_inc(&files
->count
);
413 void fastcall
put_files_struct(struct files_struct
*files
)
417 if (atomic_dec_and_test(&files
->count
)) {
420 * Free the fd and fdset arrays if we expanded them.
421 * If the fdtable was embedded, pass files for freeing
422 * at the end of the RCU grace period. Otherwise,
423 * you can free files immediately.
425 fdt
= files_fdtable(files
);
426 if (fdt
== &files
->fdtab
)
427 fdt
->free_files
= files
;
429 kmem_cache_free(files_cachep
, files
);
434 EXPORT_SYMBOL(put_files_struct
);
436 static inline void __exit_files(struct task_struct
*tsk
)
438 struct files_struct
* files
= tsk
->files
;
444 put_files_struct(files
);
448 void exit_files(struct task_struct
*tsk
)
453 static inline void __put_fs_struct(struct fs_struct
*fs
)
455 /* No need to hold fs->lock if we are killing it */
456 if (atomic_dec_and_test(&fs
->count
)) {
463 mntput(fs
->altrootmnt
);
465 kmem_cache_free(fs_cachep
, fs
);
469 void put_fs_struct(struct fs_struct
*fs
)
474 static inline void __exit_fs(struct task_struct
*tsk
)
476 struct fs_struct
* fs
= tsk
->fs
;
486 void exit_fs(struct task_struct
*tsk
)
491 EXPORT_SYMBOL_GPL(exit_fs
);
494 * Turn us into a lazy TLB process if we
497 static void exit_mm(struct task_struct
* tsk
)
499 struct mm_struct
*mm
= tsk
->mm
;
505 * Serialize with any possible pending coredump.
506 * We must hold mmap_sem around checking core_waiters
507 * and clearing tsk->mm. The core-inducing thread
508 * will increment core_waiters for each thread in the
509 * group with ->mm != NULL.
511 down_read(&mm
->mmap_sem
);
512 if (mm
->core_waiters
) {
513 up_read(&mm
->mmap_sem
);
514 down_write(&mm
->mmap_sem
);
515 if (!--mm
->core_waiters
)
516 complete(mm
->core_startup_done
);
517 up_write(&mm
->mmap_sem
);
519 wait_for_completion(&mm
->core_done
);
520 down_read(&mm
->mmap_sem
);
522 atomic_inc(&mm
->mm_count
);
523 if (mm
!= tsk
->active_mm
) BUG();
524 /* more a memory barrier than a real lock */
527 up_read(&mm
->mmap_sem
);
528 enter_lazy_tlb(mm
, current
);
533 static inline void choose_new_parent(task_t
*p
, task_t
*reaper
, task_t
*child_reaper
)
536 * Make sure we're not reparenting to ourselves and that
537 * the parent is not a zombie.
539 BUG_ON(p
== reaper
|| reaper
->exit_state
>= EXIT_ZOMBIE
);
540 p
->real_parent
= reaper
;
543 static inline void reparent_thread(task_t
*p
, task_t
*father
, int traced
)
545 /* We don't want people slaying init. */
546 if (p
->exit_signal
!= -1)
547 p
->exit_signal
= SIGCHLD
;
549 if (p
->pdeath_signal
)
550 /* We already hold the tasklist_lock here. */
551 group_send_sig_info(p
->pdeath_signal
, SEND_SIG_NOINFO
, p
);
553 /* Move the child from its dying parent to the new one. */
554 if (unlikely(traced
)) {
555 /* Preserve ptrace links if someone else is tracing this child. */
556 list_del_init(&p
->ptrace_list
);
557 if (p
->parent
!= p
->real_parent
)
558 list_add(&p
->ptrace_list
, &p
->real_parent
->ptrace_children
);
560 /* If this child is being traced, then we're the one tracing it
561 * anyway, so let go of it.
564 list_del_init(&p
->sibling
);
565 p
->parent
= p
->real_parent
;
566 list_add_tail(&p
->sibling
, &p
->parent
->children
);
568 /* If we'd notified the old parent about this child's death,
569 * also notify the new parent.
571 if (p
->exit_state
== EXIT_ZOMBIE
&& p
->exit_signal
!= -1 &&
572 thread_group_empty(p
))
573 do_notify_parent(p
, p
->exit_signal
);
574 else if (p
->state
== TASK_TRACED
) {
576 * If it was at a trace stop, turn it into
577 * a normal stop since it's no longer being
585 * process group orphan check
586 * Case ii: Our child is in a different pgrp
587 * than we are, and it was the only connection
588 * outside, so the child pgrp is now orphaned.
590 if ((process_group(p
) != process_group(father
)) &&
591 (p
->signal
->session
== father
->signal
->session
)) {
592 int pgrp
= process_group(p
);
594 if (will_become_orphaned_pgrp(pgrp
, NULL
) && has_stopped_jobs(pgrp
)) {
595 __kill_pg_info(SIGHUP
, SEND_SIG_PRIV
, pgrp
);
596 __kill_pg_info(SIGCONT
, SEND_SIG_PRIV
, pgrp
);
602 * When we die, we re-parent all our children.
603 * Try to give them to another thread in our thread
604 * group, and if no such member exists, give it to
605 * the global child reaper process (ie "init")
607 static inline void forget_original_parent(struct task_struct
* father
,
608 struct list_head
*to_release
)
610 struct task_struct
*p
, *reaper
= father
;
611 struct list_head
*_p
, *_n
;
614 reaper
= next_thread(reaper
);
615 if (reaper
== father
) {
616 reaper
= child_reaper
;
619 } while (reaper
->exit_state
);
622 * There are only two places where our children can be:
624 * - in our child list
625 * - in our ptraced child list
627 * Search them and reparent children.
629 list_for_each_safe(_p
, _n
, &father
->children
) {
631 p
= list_entry(_p
,struct task_struct
,sibling
);
635 /* if father isn't the real parent, then ptrace must be enabled */
636 BUG_ON(father
!= p
->real_parent
&& !ptrace
);
638 if (father
== p
->real_parent
) {
639 /* reparent with a reaper, real father it's us */
640 choose_new_parent(p
, reaper
, child_reaper
);
641 reparent_thread(p
, father
, 0);
643 /* reparent ptraced task to its real parent */
645 if (p
->exit_state
== EXIT_ZOMBIE
&& p
->exit_signal
!= -1 &&
646 thread_group_empty(p
))
647 do_notify_parent(p
, p
->exit_signal
);
651 * if the ptraced child is a zombie with exit_signal == -1
652 * we must collect it before we exit, or it will remain
653 * zombie forever since we prevented it from self-reap itself
654 * while it was being traced by us, to be able to see it in wait4.
656 if (unlikely(ptrace
&& p
->exit_state
== EXIT_ZOMBIE
&& p
->exit_signal
== -1))
657 list_add(&p
->ptrace_list
, to_release
);
659 list_for_each_safe(_p
, _n
, &father
->ptrace_children
) {
660 p
= list_entry(_p
,struct task_struct
,ptrace_list
);
661 choose_new_parent(p
, reaper
, child_reaper
);
662 reparent_thread(p
, father
, 1);
667 * Send signals to all our closest relatives so that they know
668 * to properly mourn us..
670 static void exit_notify(struct task_struct
*tsk
)
673 struct task_struct
*t
;
674 struct list_head ptrace_dead
, *_p
, *_n
;
676 if (signal_pending(tsk
) && !(tsk
->signal
->flags
& SIGNAL_GROUP_EXIT
)
677 && !thread_group_empty(tsk
)) {
679 * This occurs when there was a race between our exit
680 * syscall and a group signal choosing us as the one to
681 * wake up. It could be that we are the only thread
682 * alerted to check for pending signals, but another thread
683 * should be woken now to take the signal since we will not.
684 * Now we'll wake all the threads in the group just to make
685 * sure someone gets all the pending signals.
687 read_lock(&tasklist_lock
);
688 spin_lock_irq(&tsk
->sighand
->siglock
);
689 for (t
= next_thread(tsk
); t
!= tsk
; t
= next_thread(t
))
690 if (!signal_pending(t
) && !(t
->flags
& PF_EXITING
)) {
691 recalc_sigpending_tsk(t
);
692 if (signal_pending(t
))
693 signal_wake_up(t
, 0);
695 spin_unlock_irq(&tsk
->sighand
->siglock
);
696 read_unlock(&tasklist_lock
);
699 write_lock_irq(&tasklist_lock
);
702 * This does two things:
704 * A. Make init inherit all the child processes
705 * B. Check to see if any process groups have become orphaned
706 * as a result of our exiting, and if they have any stopped
707 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
710 INIT_LIST_HEAD(&ptrace_dead
);
711 forget_original_parent(tsk
, &ptrace_dead
);
712 BUG_ON(!list_empty(&tsk
->children
));
713 BUG_ON(!list_empty(&tsk
->ptrace_children
));
716 * Check to see if any process groups have become orphaned
717 * as a result of our exiting, and if they have any stopped
718 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
720 * Case i: Our father is in a different pgrp than we are
721 * and we were the only connection outside, so our pgrp
722 * is about to become orphaned.
725 t
= tsk
->real_parent
;
727 if ((process_group(t
) != process_group(tsk
)) &&
728 (t
->signal
->session
== tsk
->signal
->session
) &&
729 will_become_orphaned_pgrp(process_group(tsk
), tsk
) &&
730 has_stopped_jobs(process_group(tsk
))) {
731 __kill_pg_info(SIGHUP
, SEND_SIG_PRIV
, process_group(tsk
));
732 __kill_pg_info(SIGCONT
, SEND_SIG_PRIV
, process_group(tsk
));
735 /* Let father know we died
737 * Thread signals are configurable, but you aren't going to use
738 * that to send signals to arbitary processes.
739 * That stops right now.
741 * If the parent exec id doesn't match the exec id we saved
742 * when we started then we know the parent has changed security
745 * If our self_exec id doesn't match our parent_exec_id then
746 * we have changed execution domain as these two values started
747 * the same after a fork.
751 if (tsk
->exit_signal
!= SIGCHLD
&& tsk
->exit_signal
!= -1 &&
752 ( tsk
->parent_exec_id
!= t
->self_exec_id
||
753 tsk
->self_exec_id
!= tsk
->parent_exec_id
)
754 && !capable(CAP_KILL
))
755 tsk
->exit_signal
= SIGCHLD
;
758 /* If something other than our normal parent is ptracing us, then
759 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
760 * only has special meaning to our real parent.
762 if (tsk
->exit_signal
!= -1 && thread_group_empty(tsk
)) {
763 int signal
= tsk
->parent
== tsk
->real_parent
? tsk
->exit_signal
: SIGCHLD
;
764 do_notify_parent(tsk
, signal
);
765 } else if (tsk
->ptrace
) {
766 do_notify_parent(tsk
, SIGCHLD
);
770 if (tsk
->exit_signal
== -1 &&
771 (likely(tsk
->ptrace
== 0) ||
772 unlikely(tsk
->parent
->signal
->flags
& SIGNAL_GROUP_EXIT
)))
774 tsk
->exit_state
= state
;
776 write_unlock_irq(&tasklist_lock
);
778 list_for_each_safe(_p
, _n
, &ptrace_dead
) {
780 t
= list_entry(_p
,struct task_struct
,ptrace_list
);
784 /* If the process is dead, release it - nobody will wait for it */
785 if (state
== EXIT_DEAD
)
789 fastcall NORET_TYPE
void do_exit(long code
)
791 struct task_struct
*tsk
= current
;
794 profile_task_exit(tsk
);
796 WARN_ON(atomic_read(&tsk
->fs_excl
));
798 if (unlikely(in_interrupt()))
799 panic("Aiee, killing interrupt handler!");
800 if (unlikely(!tsk
->pid
))
801 panic("Attempted to kill the idle task!");
802 if (unlikely(tsk
->pid
== 1))
803 panic("Attempted to kill init!");
807 if (unlikely(current
->ptrace
& PT_TRACE_EXIT
)) {
808 current
->ptrace_message
= code
;
809 ptrace_notify((PTRACE_EVENT_EXIT
<< 8) | SIGTRAP
);
813 * We're taking recursive faults here in do_exit. Safest is to just
814 * leave this task alone and wait for reboot.
816 if (unlikely(tsk
->flags
& PF_EXITING
)) {
818 "Fixing recursive fault but reboot is needed!\n");
819 set_current_state(TASK_UNINTERRUPTIBLE
);
823 tsk
->flags
|= PF_EXITING
;
826 * Make sure we don't try to process any timer firings
827 * while we are already exiting.
829 tsk
->it_virt_expires
= cputime_zero
;
830 tsk
->it_prof_expires
= cputime_zero
;
831 tsk
->it_sched_expires
= 0;
833 if (unlikely(in_atomic()))
834 printk(KERN_INFO
"note: %s[%d] exited with preempt_count %d\n",
835 current
->comm
, current
->pid
,
838 acct_update_integrals(tsk
);
840 update_hiwater_rss(tsk
->mm
);
841 update_hiwater_vm(tsk
->mm
);
843 group_dead
= atomic_dec_and_test(&tsk
->signal
->live
);
845 hrtimer_cancel(&tsk
->signal
->real_timer
);
846 exit_itimers(tsk
->signal
);
859 if (group_dead
&& tsk
->signal
->leader
)
860 disassociate_ctty(1);
862 module_put(task_thread_info(tsk
)->exec_domain
->module
);
864 module_put(tsk
->binfmt
->module
);
866 tsk
->exit_code
= code
;
867 proc_exit_connector(tsk
);
870 mpol_free(tsk
->mempolicy
);
871 tsk
->mempolicy
= NULL
;
874 * If DEBUG_MUTEXES is on, make sure we are holding no locks:
876 mutex_debug_check_no_locks_held(tsk
);
878 /* PF_DEAD causes final put_task_struct after we schedule. */
880 BUG_ON(tsk
->flags
& PF_DEAD
);
881 tsk
->flags
|= PF_DEAD
;
885 /* Avoid "noreturn function does return". */
889 EXPORT_SYMBOL_GPL(do_exit
);
891 NORET_TYPE
void complete_and_exit(struct completion
*comp
, long code
)
899 EXPORT_SYMBOL(complete_and_exit
);
901 asmlinkage
long sys_exit(int error_code
)
903 do_exit((error_code
&0xff)<<8);
906 task_t fastcall
*next_thread(const task_t
*p
)
908 return pid_task(p
->pids
[PIDTYPE_TGID
].pid_list
.next
, PIDTYPE_TGID
);
911 EXPORT_SYMBOL(next_thread
);
914 * Take down every thread in the group. This is called by fatal signals
915 * as well as by sys_exit_group (below).
918 do_group_exit(int exit_code
)
920 BUG_ON(exit_code
& 0x80); /* core dumps don't get here */
922 if (current
->signal
->flags
& SIGNAL_GROUP_EXIT
)
923 exit_code
= current
->signal
->group_exit_code
;
924 else if (!thread_group_empty(current
)) {
925 struct signal_struct
*const sig
= current
->signal
;
926 struct sighand_struct
*const sighand
= current
->sighand
;
927 read_lock(&tasklist_lock
);
928 spin_lock_irq(&sighand
->siglock
);
929 if (sig
->flags
& SIGNAL_GROUP_EXIT
)
930 /* Another thread got here before we took the lock. */
931 exit_code
= sig
->group_exit_code
;
933 sig
->group_exit_code
= exit_code
;
934 zap_other_threads(current
);
936 spin_unlock_irq(&sighand
->siglock
);
937 read_unlock(&tasklist_lock
);
945 * this kills every thread in the thread group. Note that any externally
946 * wait4()-ing process will get the correct exit code - even if this
947 * thread is not the thread group leader.
949 asmlinkage
void sys_exit_group(int error_code
)
951 do_group_exit((error_code
& 0xff) << 8);
954 static int eligible_child(pid_t pid
, int options
, task_t
*p
)
960 if (process_group(p
) != process_group(current
))
962 } else if (pid
!= -1) {
963 if (process_group(p
) != -pid
)
968 * Do not consider detached threads that are
971 if (p
->exit_signal
== -1 && !p
->ptrace
)
974 /* Wait for all children (clone and not) if __WALL is set;
975 * otherwise, wait for clone children *only* if __WCLONE is
976 * set; otherwise, wait for non-clone children *only*. (Note:
977 * A "clone" child here is one that reports to its parent
978 * using a signal other than SIGCHLD.) */
979 if (((p
->exit_signal
!= SIGCHLD
) ^ ((options
& __WCLONE
) != 0))
980 && !(options
& __WALL
))
983 * Do not consider thread group leaders that are
984 * in a non-empty thread group:
986 if (current
->tgid
!= p
->tgid
&& delay_group_leader(p
))
989 if (security_task_wait(p
))
995 static int wait_noreap_copyout(task_t
*p
, pid_t pid
, uid_t uid
,
997 struct siginfo __user
*infop
,
998 struct rusage __user
*rusagep
)
1000 int retval
= rusagep
? getrusage(p
, RUSAGE_BOTH
, rusagep
) : 0;
1003 retval
= put_user(SIGCHLD
, &infop
->si_signo
);
1005 retval
= put_user(0, &infop
->si_errno
);
1007 retval
= put_user((short)why
, &infop
->si_code
);
1009 retval
= put_user(pid
, &infop
->si_pid
);
1011 retval
= put_user(uid
, &infop
->si_uid
);
1013 retval
= put_user(status
, &infop
->si_status
);
1020 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1021 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1022 * the lock and this task is uninteresting. If we return nonzero, we have
1023 * released the lock and the system call should return.
1025 static int wait_task_zombie(task_t
*p
, int noreap
,
1026 struct siginfo __user
*infop
,
1027 int __user
*stat_addr
, struct rusage __user
*ru
)
1029 unsigned long state
;
1033 if (unlikely(noreap
)) {
1036 int exit_code
= p
->exit_code
;
1039 if (unlikely(p
->exit_state
!= EXIT_ZOMBIE
))
1041 if (unlikely(p
->exit_signal
== -1 && p
->ptrace
== 0))
1044 read_unlock(&tasklist_lock
);
1045 if ((exit_code
& 0x7f) == 0) {
1047 status
= exit_code
>> 8;
1049 why
= (exit_code
& 0x80) ? CLD_DUMPED
: CLD_KILLED
;
1050 status
= exit_code
& 0x7f;
1052 return wait_noreap_copyout(p
, pid
, uid
, why
,
1057 * Try to move the task's state to DEAD
1058 * only one thread is allowed to do this:
1060 state
= xchg(&p
->exit_state
, EXIT_DEAD
);
1061 if (state
!= EXIT_ZOMBIE
) {
1062 BUG_ON(state
!= EXIT_DEAD
);
1065 if (unlikely(p
->exit_signal
== -1 && p
->ptrace
== 0)) {
1067 * This can only happen in a race with a ptraced thread
1068 * dying on another processor.
1073 if (likely(p
->real_parent
== p
->parent
) && likely(p
->signal
)) {
1074 struct signal_struct
*psig
;
1075 struct signal_struct
*sig
;
1078 * The resource counters for the group leader are in its
1079 * own task_struct. Those for dead threads in the group
1080 * are in its signal_struct, as are those for the child
1081 * processes it has previously reaped. All these
1082 * accumulate in the parent's signal_struct c* fields.
1084 * We don't bother to take a lock here to protect these
1085 * p->signal fields, because they are only touched by
1086 * __exit_signal, which runs with tasklist_lock
1087 * write-locked anyway, and so is excluded here. We do
1088 * need to protect the access to p->parent->signal fields,
1089 * as other threads in the parent group can be right
1090 * here reaping other children at the same time.
1092 spin_lock_irq(&p
->parent
->sighand
->siglock
);
1093 psig
= p
->parent
->signal
;
1096 cputime_add(psig
->cutime
,
1097 cputime_add(p
->utime
,
1098 cputime_add(sig
->utime
,
1101 cputime_add(psig
->cstime
,
1102 cputime_add(p
->stime
,
1103 cputime_add(sig
->stime
,
1106 p
->min_flt
+ sig
->min_flt
+ sig
->cmin_flt
;
1108 p
->maj_flt
+ sig
->maj_flt
+ sig
->cmaj_flt
;
1110 p
->nvcsw
+ sig
->nvcsw
+ sig
->cnvcsw
;
1112 p
->nivcsw
+ sig
->nivcsw
+ sig
->cnivcsw
;
1113 spin_unlock_irq(&p
->parent
->sighand
->siglock
);
1117 * Now we are sure this task is interesting, and no other
1118 * thread can reap it because we set its state to EXIT_DEAD.
1120 read_unlock(&tasklist_lock
);
1122 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
1123 status
= (p
->signal
->flags
& SIGNAL_GROUP_EXIT
)
1124 ? p
->signal
->group_exit_code
: p
->exit_code
;
1125 if (!retval
&& stat_addr
)
1126 retval
= put_user(status
, stat_addr
);
1127 if (!retval
&& infop
)
1128 retval
= put_user(SIGCHLD
, &infop
->si_signo
);
1129 if (!retval
&& infop
)
1130 retval
= put_user(0, &infop
->si_errno
);
1131 if (!retval
&& infop
) {
1134 if ((status
& 0x7f) == 0) {
1138 why
= (status
& 0x80) ? CLD_DUMPED
: CLD_KILLED
;
1141 retval
= put_user((short)why
, &infop
->si_code
);
1143 retval
= put_user(status
, &infop
->si_status
);
1145 if (!retval
&& infop
)
1146 retval
= put_user(p
->pid
, &infop
->si_pid
);
1147 if (!retval
&& infop
)
1148 retval
= put_user(p
->uid
, &infop
->si_uid
);
1150 // TODO: is this safe?
1151 p
->exit_state
= EXIT_ZOMBIE
;
1155 if (p
->real_parent
!= p
->parent
) {
1156 write_lock_irq(&tasklist_lock
);
1157 /* Double-check with lock held. */
1158 if (p
->real_parent
!= p
->parent
) {
1160 // TODO: is this safe?
1161 p
->exit_state
= EXIT_ZOMBIE
;
1163 * If this is not a detached task, notify the parent.
1164 * If it's still not detached after that, don't release
1167 if (p
->exit_signal
!= -1) {
1168 do_notify_parent(p
, p
->exit_signal
);
1169 if (p
->exit_signal
!= -1)
1173 write_unlock_irq(&tasklist_lock
);
1182 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1183 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1184 * the lock and this task is uninteresting. If we return nonzero, we have
1185 * released the lock and the system call should return.
1187 static int wait_task_stopped(task_t
*p
, int delayed_group_leader
, int noreap
,
1188 struct siginfo __user
*infop
,
1189 int __user
*stat_addr
, struct rusage __user
*ru
)
1191 int retval
, exit_code
;
1195 if (delayed_group_leader
&& !(p
->ptrace
& PT_PTRACED
) &&
1196 p
->signal
&& p
->signal
->group_stop_count
> 0)
1198 * A group stop is in progress and this is the group leader.
1199 * We won't report until all threads have stopped.
1204 * Now we are pretty sure this task is interesting.
1205 * Make sure it doesn't get reaped out from under us while we
1206 * give up the lock and then examine it below. We don't want to
1207 * keep holding onto the tasklist_lock while we call getrusage and
1208 * possibly take page faults for user memory.
1211 read_unlock(&tasklist_lock
);
1213 if (unlikely(noreap
)) {
1216 int why
= (p
->ptrace
& PT_PTRACED
) ? CLD_TRAPPED
: CLD_STOPPED
;
1218 exit_code
= p
->exit_code
;
1219 if (unlikely(!exit_code
) ||
1220 unlikely(p
->state
& TASK_TRACED
))
1222 return wait_noreap_copyout(p
, pid
, uid
,
1223 why
, (exit_code
<< 8) | 0x7f,
1227 write_lock_irq(&tasklist_lock
);
1230 * This uses xchg to be atomic with the thread resuming and setting
1231 * it. It must also be done with the write lock held to prevent a
1232 * race with the EXIT_ZOMBIE case.
1234 exit_code
= xchg(&p
->exit_code
, 0);
1235 if (unlikely(p
->exit_state
)) {
1237 * The task resumed and then died. Let the next iteration
1238 * catch it in EXIT_ZOMBIE. Note that exit_code might
1239 * already be zero here if it resumed and did _exit(0).
1240 * The task itself is dead and won't touch exit_code again;
1241 * other processors in this function are locked out.
1243 p
->exit_code
= exit_code
;
1246 if (unlikely(exit_code
== 0)) {
1248 * Another thread in this function got to it first, or it
1249 * resumed, or it resumed and then died.
1251 write_unlock_irq(&tasklist_lock
);
1255 * We are returning to the wait loop without having successfully
1256 * removed the process and having released the lock. We cannot
1257 * continue, since the "p" task pointer is potentially stale.
1259 * Return -EAGAIN, and do_wait() will restart the loop from the
1260 * beginning. Do _not_ re-acquire the lock.
1265 /* move to end of parent's list to avoid starvation */
1267 add_parent(p
, p
->parent
);
1269 write_unlock_irq(&tasklist_lock
);
1271 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
1272 if (!retval
&& stat_addr
)
1273 retval
= put_user((exit_code
<< 8) | 0x7f, stat_addr
);
1274 if (!retval
&& infop
)
1275 retval
= put_user(SIGCHLD
, &infop
->si_signo
);
1276 if (!retval
&& infop
)
1277 retval
= put_user(0, &infop
->si_errno
);
1278 if (!retval
&& infop
)
1279 retval
= put_user((short)((p
->ptrace
& PT_PTRACED
)
1280 ? CLD_TRAPPED
: CLD_STOPPED
),
1282 if (!retval
&& infop
)
1283 retval
= put_user(exit_code
, &infop
->si_status
);
1284 if (!retval
&& infop
)
1285 retval
= put_user(p
->pid
, &infop
->si_pid
);
1286 if (!retval
&& infop
)
1287 retval
= put_user(p
->uid
, &infop
->si_uid
);
1297 * Handle do_wait work for one task in a live, non-stopped state.
1298 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1299 * the lock and this task is uninteresting. If we return nonzero, we have
1300 * released the lock and the system call should return.
1302 static int wait_task_continued(task_t
*p
, int noreap
,
1303 struct siginfo __user
*infop
,
1304 int __user
*stat_addr
, struct rusage __user
*ru
)
1310 if (unlikely(!p
->signal
))
1313 if (!(p
->signal
->flags
& SIGNAL_STOP_CONTINUED
))
1316 spin_lock_irq(&p
->sighand
->siglock
);
1317 /* Re-check with the lock held. */
1318 if (!(p
->signal
->flags
& SIGNAL_STOP_CONTINUED
)) {
1319 spin_unlock_irq(&p
->sighand
->siglock
);
1323 p
->signal
->flags
&= ~SIGNAL_STOP_CONTINUED
;
1324 spin_unlock_irq(&p
->sighand
->siglock
);
1329 read_unlock(&tasklist_lock
);
1332 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
1334 if (!retval
&& stat_addr
)
1335 retval
= put_user(0xffff, stat_addr
);
1339 retval
= wait_noreap_copyout(p
, pid
, uid
,
1340 CLD_CONTINUED
, SIGCONT
,
1342 BUG_ON(retval
== 0);
1349 static inline int my_ptrace_child(struct task_struct
*p
)
1351 if (!(p
->ptrace
& PT_PTRACED
))
1353 if (!(p
->ptrace
& PT_ATTACHED
))
1356 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1357 * we are the attacher. If we are the real parent, this is a race
1358 * inside ptrace_attach. It is waiting for the tasklist_lock,
1359 * which we have to switch the parent links, but has already set
1360 * the flags in p->ptrace.
1362 return (p
->parent
!= p
->real_parent
);
1365 static long do_wait(pid_t pid
, int options
, struct siginfo __user
*infop
,
1366 int __user
*stat_addr
, struct rusage __user
*ru
)
1368 DECLARE_WAITQUEUE(wait
, current
);
1369 struct task_struct
*tsk
;
1372 add_wait_queue(¤t
->signal
->wait_chldexit
,&wait
);
1375 * We will set this flag if we see any child that might later
1376 * match our criteria, even if we are not able to reap it yet.
1379 current
->state
= TASK_INTERRUPTIBLE
;
1380 read_lock(&tasklist_lock
);
1383 struct task_struct
*p
;
1384 struct list_head
*_p
;
1387 list_for_each(_p
,&tsk
->children
) {
1388 p
= list_entry(_p
,struct task_struct
,sibling
);
1390 ret
= eligible_child(pid
, options
, p
);
1397 * When we hit the race with PTRACE_ATTACH,
1398 * we will not report this child. But the
1399 * race means it has not yet been moved to
1400 * our ptrace_children list, so we need to
1401 * set the flag here to avoid a spurious ECHILD
1402 * when the race happens with the only child.
1405 if (!my_ptrace_child(p
))
1410 * It's stopped now, so it might later
1411 * continue, exit, or stop again.
1414 if (!(options
& WUNTRACED
) &&
1415 !my_ptrace_child(p
))
1417 retval
= wait_task_stopped(p
, ret
== 2,
1418 (options
& WNOWAIT
),
1421 if (retval
== -EAGAIN
)
1423 if (retval
!= 0) /* He released the lock. */
1428 if (p
->exit_state
== EXIT_DEAD
)
1430 // case EXIT_ZOMBIE:
1431 if (p
->exit_state
== EXIT_ZOMBIE
) {
1433 * Eligible but we cannot release
1437 goto check_continued
;
1438 if (!likely(options
& WEXITED
))
1440 retval
= wait_task_zombie(
1441 p
, (options
& WNOWAIT
),
1442 infop
, stat_addr
, ru
);
1443 /* He released the lock. */
1450 * It's running now, so it might later
1451 * exit, stop, or stop and then continue.
1454 if (!unlikely(options
& WCONTINUED
))
1456 retval
= wait_task_continued(
1457 p
, (options
& WNOWAIT
),
1458 infop
, stat_addr
, ru
);
1459 if (retval
!= 0) /* He released the lock. */
1465 list_for_each(_p
, &tsk
->ptrace_children
) {
1466 p
= list_entry(_p
, struct task_struct
,
1468 if (!eligible_child(pid
, options
, p
))
1474 if (options
& __WNOTHREAD
)
1476 tsk
= next_thread(tsk
);
1477 if (tsk
->signal
!= current
->signal
)
1479 } while (tsk
!= current
);
1481 read_unlock(&tasklist_lock
);
1484 if (options
& WNOHANG
)
1486 retval
= -ERESTARTSYS
;
1487 if (signal_pending(current
))
1494 current
->state
= TASK_RUNNING
;
1495 remove_wait_queue(¤t
->signal
->wait_chldexit
,&wait
);
1501 * For a WNOHANG return, clear out all the fields
1502 * we would set so the user can easily tell the
1506 retval
= put_user(0, &infop
->si_signo
);
1508 retval
= put_user(0, &infop
->si_errno
);
1510 retval
= put_user(0, &infop
->si_code
);
1512 retval
= put_user(0, &infop
->si_pid
);
1514 retval
= put_user(0, &infop
->si_uid
);
1516 retval
= put_user(0, &infop
->si_status
);
1522 asmlinkage
long sys_waitid(int which
, pid_t pid
,
1523 struct siginfo __user
*infop
, int options
,
1524 struct rusage __user
*ru
)
1528 if (options
& ~(WNOHANG
|WNOWAIT
|WEXITED
|WSTOPPED
|WCONTINUED
))
1530 if (!(options
& (WEXITED
|WSTOPPED
|WCONTINUED
)))
1550 ret
= do_wait(pid
, options
, infop
, NULL
, ru
);
1552 /* avoid REGPARM breakage on x86: */
1553 prevent_tail_call(ret
);
1557 asmlinkage
long sys_wait4(pid_t pid
, int __user
*stat_addr
,
1558 int options
, struct rusage __user
*ru
)
1562 if (options
& ~(WNOHANG
|WUNTRACED
|WCONTINUED
|
1563 __WNOTHREAD
|__WCLONE
|__WALL
))
1565 ret
= do_wait(pid
, options
| WEXITED
, NULL
, stat_addr
, ru
);
1567 /* avoid REGPARM breakage on x86: */
1568 prevent_tail_call(ret
);
1572 #ifdef __ARCH_WANT_SYS_WAITPID
1575 * sys_waitpid() remains for compatibility. waitpid() should be
1576 * implemented by calling sys_wait4() from libc.a.
1578 asmlinkage
long sys_waitpid(pid_t pid
, int __user
*stat_addr
, int options
)
1580 return sys_wait4(pid
, stat_addr
, options
, NULL
);