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/capability.h>
14 #include <linux/completion.h>
15 #include <linux/personality.h>
16 #include <linux/tty.h>
17 #include <linux/namespace.h>
18 #include <linux/key.h>
19 #include <linux/security.h>
20 #include <linux/cpu.h>
21 #include <linux/acct.h>
22 #include <linux/file.h>
23 #include <linux/binfmts.h>
24 #include <linux/ptrace.h>
25 #include <linux/profile.h>
26 #include <linux/mount.h>
27 #include <linux/proc_fs.h>
28 #include <linux/mempolicy.h>
29 #include <linux/cpuset.h>
30 #include <linux/syscalls.h>
31 #include <linux/signal.h>
32 #include <linux/cn_proc.h>
33 #include <linux/mutex.h>
34 #include <linux/futex.h>
35 #include <linux/compat.h>
37 #include <asm/uaccess.h>
38 #include <asm/unistd.h>
39 #include <asm/pgtable.h>
40 #include <asm/mmu_context.h>
42 extern void sem_exit (void);
43 extern struct task_struct
*child_reaper
;
45 int getrusage(struct task_struct
*, int, struct rusage __user
*);
47 static void exit_mm(struct task_struct
* tsk
);
49 static void __unhash_process(struct task_struct
*p
)
52 detach_pid(p
, PIDTYPE_PID
);
53 detach_pid(p
, PIDTYPE_TGID
);
54 if (thread_group_leader(p
)) {
55 detach_pid(p
, PIDTYPE_PGID
);
56 detach_pid(p
, PIDTYPE_SID
);
58 __get_cpu_var(process_counts
)--;
64 void release_task(struct task_struct
* p
)
68 struct dentry
*proc_dentry
;
71 atomic_dec(&p
->user
->processes
);
72 spin_lock(&p
->proc_lock
);
73 proc_dentry
= proc_pid_unhash(p
);
74 write_lock_irq(&tasklist_lock
);
75 if (unlikely(p
->ptrace
))
77 BUG_ON(!list_empty(&p
->ptrace_list
) || !list_empty(&p
->ptrace_children
));
80 * Note that the fastpath in sys_times depends on __exit_signal having
81 * updated the counters before a task is removed from the tasklist of
82 * the process by __unhash_process.
87 * If we are the last non-leader member of the thread
88 * group, and the leader is zombie, then notify the
89 * group leader's parent process. (if it wants notification.)
92 leader
= p
->group_leader
;
93 if (leader
!= p
&& thread_group_empty(leader
) && leader
->exit_state
== EXIT_ZOMBIE
) {
94 BUG_ON(leader
->exit_signal
== -1);
95 do_notify_parent(leader
, leader
->exit_signal
);
97 * If we were the last child thread and the leader has
98 * exited already, and the leader's parent ignores SIGCHLD,
99 * then we are the one who should release the leader.
101 * do_notify_parent() will have marked it self-reaping in
104 zap_leader
= (leader
->exit_signal
== -1);
108 write_unlock_irq(&tasklist_lock
);
109 spin_unlock(&p
->proc_lock
);
110 proc_pid_flush(proc_dentry
);
115 if (unlikely(zap_leader
))
119 /* we are using it only for SMP init */
121 void unhash_process(struct task_struct
*p
)
123 struct dentry
*proc_dentry
;
125 spin_lock(&p
->proc_lock
);
126 proc_dentry
= proc_pid_unhash(p
);
127 write_lock_irq(&tasklist_lock
);
129 write_unlock_irq(&tasklist_lock
);
130 spin_unlock(&p
->proc_lock
);
131 proc_pid_flush(proc_dentry
);
135 * This checks not only the pgrp, but falls back on the pid if no
136 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
139 int session_of_pgrp(int pgrp
)
141 struct task_struct
*p
;
144 read_lock(&tasklist_lock
);
145 do_each_task_pid(pgrp
, PIDTYPE_PGID
, p
) {
146 if (p
->signal
->session
> 0) {
147 sid
= p
->signal
->session
;
150 } while_each_task_pid(pgrp
, PIDTYPE_PGID
, p
);
151 p
= find_task_by_pid(pgrp
);
153 sid
= p
->signal
->session
;
155 read_unlock(&tasklist_lock
);
161 * Determine if a process group is "orphaned", according to the POSIX
162 * definition in 2.2.2.52. Orphaned process groups are not to be affected
163 * by terminal-generated stop signals. Newly orphaned process groups are
164 * to receive a SIGHUP and a SIGCONT.
166 * "I ask you, have you ever known what it is to be an orphan?"
168 static int will_become_orphaned_pgrp(int pgrp
, task_t
*ignored_task
)
170 struct task_struct
*p
;
173 do_each_task_pid(pgrp
, PIDTYPE_PGID
, p
) {
174 if (p
== ignored_task
176 || p
->real_parent
->pid
== 1)
178 if (process_group(p
->real_parent
) != pgrp
179 && p
->real_parent
->signal
->session
== p
->signal
->session
) {
183 } while_each_task_pid(pgrp
, PIDTYPE_PGID
, p
);
184 return ret
; /* (sighing) "Often!" */
187 int is_orphaned_pgrp(int pgrp
)
191 read_lock(&tasklist_lock
);
192 retval
= will_become_orphaned_pgrp(pgrp
, NULL
);
193 read_unlock(&tasklist_lock
);
198 static int has_stopped_jobs(int pgrp
)
201 struct task_struct
*p
;
203 do_each_task_pid(pgrp
, PIDTYPE_PGID
, p
) {
204 if (p
->state
!= TASK_STOPPED
)
207 /* If p is stopped by a debugger on a signal that won't
208 stop it, then don't count p as stopped. This isn't
209 perfect but it's a good approximation. */
210 if (unlikely (p
->ptrace
)
211 && p
->exit_code
!= SIGSTOP
212 && p
->exit_code
!= SIGTSTP
213 && p
->exit_code
!= SIGTTOU
214 && p
->exit_code
!= SIGTTIN
)
219 } while_each_task_pid(pgrp
, PIDTYPE_PGID
, p
);
224 * reparent_to_init - Reparent the calling kernel thread to the init task.
226 * If a kernel thread is launched as a result of a system call, or if
227 * it ever exits, it should generally reparent itself to init so that
228 * it is correctly cleaned up on exit.
230 * The various task state such as scheduling policy and priority may have
231 * been inherited from a user process, so we reset them to sane values here.
233 * NOTE that reparent_to_init() gives the caller full capabilities.
235 static void reparent_to_init(void)
237 write_lock_irq(&tasklist_lock
);
239 ptrace_unlink(current
);
240 /* Reparent to init */
241 REMOVE_LINKS(current
);
242 current
->parent
= child_reaper
;
243 current
->real_parent
= child_reaper
;
246 /* Set the exit signal to SIGCHLD so we signal init on exit */
247 current
->exit_signal
= SIGCHLD
;
249 if ((current
->policy
== SCHED_NORMAL
||
250 current
->policy
== SCHED_BATCH
)
251 && (task_nice(current
) < 0))
252 set_user_nice(current
, 0);
256 security_task_reparent_to_init(current
);
257 memcpy(current
->signal
->rlim
, init_task
.signal
->rlim
,
258 sizeof(current
->signal
->rlim
));
259 atomic_inc(&(INIT_USER
->__count
));
260 write_unlock_irq(&tasklist_lock
);
261 switch_uid(INIT_USER
);
264 void __set_special_pids(pid_t session
, pid_t pgrp
)
266 struct task_struct
*curr
= current
->group_leader
;
268 if (curr
->signal
->session
!= session
) {
269 detach_pid(curr
, PIDTYPE_SID
);
270 curr
->signal
->session
= session
;
271 attach_pid(curr
, PIDTYPE_SID
, session
);
273 if (process_group(curr
) != pgrp
) {
274 detach_pid(curr
, PIDTYPE_PGID
);
275 curr
->signal
->pgrp
= pgrp
;
276 attach_pid(curr
, PIDTYPE_PGID
, pgrp
);
280 void set_special_pids(pid_t session
, pid_t pgrp
)
282 write_lock_irq(&tasklist_lock
);
283 __set_special_pids(session
, pgrp
);
284 write_unlock_irq(&tasklist_lock
);
288 * Let kernel threads use this to say that they
289 * allow a certain signal (since daemonize() will
290 * have disabled all of them by default).
292 int allow_signal(int sig
)
294 if (!valid_signal(sig
) || sig
< 1)
297 spin_lock_irq(¤t
->sighand
->siglock
);
298 sigdelset(¤t
->blocked
, sig
);
300 /* Kernel threads handle their own signals.
301 Let the signal code know it'll be handled, so
302 that they don't get converted to SIGKILL or
303 just silently dropped */
304 current
->sighand
->action
[(sig
)-1].sa
.sa_handler
= (void __user
*)2;
307 spin_unlock_irq(¤t
->sighand
->siglock
);
311 EXPORT_SYMBOL(allow_signal
);
313 int disallow_signal(int sig
)
315 if (!valid_signal(sig
) || sig
< 1)
318 spin_lock_irq(¤t
->sighand
->siglock
);
319 sigaddset(¤t
->blocked
, sig
);
321 spin_unlock_irq(¤t
->sighand
->siglock
);
325 EXPORT_SYMBOL(disallow_signal
);
328 * Put all the gunge required to become a kernel thread without
329 * attached user resources in one place where it belongs.
332 void daemonize(const char *name
, ...)
335 struct fs_struct
*fs
;
338 va_start(args
, name
);
339 vsnprintf(current
->comm
, sizeof(current
->comm
), name
, args
);
343 * If we were started as result of loading a module, close all of the
344 * user space pages. We don't need them, and if we didn't close them
345 * they would be locked into memory.
349 set_special_pids(1, 1);
350 mutex_lock(&tty_mutex
);
351 current
->signal
->tty
= NULL
;
352 mutex_unlock(&tty_mutex
);
354 /* Block and flush all signals */
355 sigfillset(&blocked
);
356 sigprocmask(SIG_BLOCK
, &blocked
, NULL
);
357 flush_signals(current
);
359 /* Become as one with the init task */
361 exit_fs(current
); /* current->fs->count--; */
364 atomic_inc(&fs
->count
);
365 exit_namespace(current
);
366 current
->namespace = init_task
.namespace;
367 get_namespace(current
->namespace);
369 current
->files
= init_task
.files
;
370 atomic_inc(¤t
->files
->count
);
375 EXPORT_SYMBOL(daemonize
);
377 static void close_files(struct files_struct
* files
)
385 * It is safe to dereference the fd table without RCU or
386 * ->file_lock because this is the last reference to the
389 fdt
= files_fdtable(files
);
393 if (i
>= fdt
->max_fdset
|| i
>= fdt
->max_fds
)
395 set
= fdt
->open_fds
->fds_bits
[j
++];
398 struct file
* file
= xchg(&fdt
->fd
[i
], NULL
);
400 filp_close(file
, files
);
408 struct files_struct
*get_files_struct(struct task_struct
*task
)
410 struct files_struct
*files
;
415 atomic_inc(&files
->count
);
421 void fastcall
put_files_struct(struct files_struct
*files
)
425 if (atomic_dec_and_test(&files
->count
)) {
428 * Free the fd and fdset arrays if we expanded them.
429 * If the fdtable was embedded, pass files for freeing
430 * at the end of the RCU grace period. Otherwise,
431 * you can free files immediately.
433 fdt
= files_fdtable(files
);
434 if (fdt
== &files
->fdtab
)
435 fdt
->free_files
= files
;
437 kmem_cache_free(files_cachep
, files
);
442 EXPORT_SYMBOL(put_files_struct
);
444 static inline void __exit_files(struct task_struct
*tsk
)
446 struct files_struct
* files
= tsk
->files
;
452 put_files_struct(files
);
456 void exit_files(struct task_struct
*tsk
)
461 static inline void __put_fs_struct(struct fs_struct
*fs
)
463 /* No need to hold fs->lock if we are killing it */
464 if (atomic_dec_and_test(&fs
->count
)) {
471 mntput(fs
->altrootmnt
);
473 kmem_cache_free(fs_cachep
, fs
);
477 void put_fs_struct(struct fs_struct
*fs
)
482 static inline void __exit_fs(struct task_struct
*tsk
)
484 struct fs_struct
* fs
= tsk
->fs
;
494 void exit_fs(struct task_struct
*tsk
)
499 EXPORT_SYMBOL_GPL(exit_fs
);
502 * Turn us into a lazy TLB process if we
505 static void exit_mm(struct task_struct
* tsk
)
507 struct mm_struct
*mm
= tsk
->mm
;
513 * Serialize with any possible pending coredump.
514 * We must hold mmap_sem around checking core_waiters
515 * and clearing tsk->mm. The core-inducing thread
516 * will increment core_waiters for each thread in the
517 * group with ->mm != NULL.
519 down_read(&mm
->mmap_sem
);
520 if (mm
->core_waiters
) {
521 up_read(&mm
->mmap_sem
);
522 down_write(&mm
->mmap_sem
);
523 if (!--mm
->core_waiters
)
524 complete(mm
->core_startup_done
);
525 up_write(&mm
->mmap_sem
);
527 wait_for_completion(&mm
->core_done
);
528 down_read(&mm
->mmap_sem
);
530 atomic_inc(&mm
->mm_count
);
531 if (mm
!= tsk
->active_mm
) BUG();
532 /* more a memory barrier than a real lock */
535 up_read(&mm
->mmap_sem
);
536 enter_lazy_tlb(mm
, current
);
541 static inline void choose_new_parent(task_t
*p
, task_t
*reaper
, task_t
*child_reaper
)
544 * Make sure we're not reparenting to ourselves and that
545 * the parent is not a zombie.
547 BUG_ON(p
== reaper
|| reaper
->exit_state
>= EXIT_ZOMBIE
);
548 p
->real_parent
= reaper
;
551 static void reparent_thread(task_t
*p
, task_t
*father
, int traced
)
553 /* We don't want people slaying init. */
554 if (p
->exit_signal
!= -1)
555 p
->exit_signal
= SIGCHLD
;
557 if (p
->pdeath_signal
)
558 /* We already hold the tasklist_lock here. */
559 group_send_sig_info(p
->pdeath_signal
, SEND_SIG_NOINFO
, p
);
561 /* Move the child from its dying parent to the new one. */
562 if (unlikely(traced
)) {
563 /* Preserve ptrace links if someone else is tracing this child. */
564 list_del_init(&p
->ptrace_list
);
565 if (p
->parent
!= p
->real_parent
)
566 list_add(&p
->ptrace_list
, &p
->real_parent
->ptrace_children
);
568 /* If this child is being traced, then we're the one tracing it
569 * anyway, so let go of it.
572 list_del_init(&p
->sibling
);
573 p
->parent
= p
->real_parent
;
574 list_add_tail(&p
->sibling
, &p
->parent
->children
);
576 /* If we'd notified the old parent about this child's death,
577 * also notify the new parent.
579 if (p
->exit_state
== EXIT_ZOMBIE
&& p
->exit_signal
!= -1 &&
580 thread_group_empty(p
))
581 do_notify_parent(p
, p
->exit_signal
);
582 else if (p
->state
== TASK_TRACED
) {
584 * If it was at a trace stop, turn it into
585 * a normal stop since it's no longer being
593 * process group orphan check
594 * Case ii: Our child is in a different pgrp
595 * than we are, and it was the only connection
596 * outside, so the child pgrp is now orphaned.
598 if ((process_group(p
) != process_group(father
)) &&
599 (p
->signal
->session
== father
->signal
->session
)) {
600 int pgrp
= process_group(p
);
602 if (will_become_orphaned_pgrp(pgrp
, NULL
) && has_stopped_jobs(pgrp
)) {
603 __kill_pg_info(SIGHUP
, SEND_SIG_PRIV
, pgrp
);
604 __kill_pg_info(SIGCONT
, SEND_SIG_PRIV
, pgrp
);
610 * When we die, we re-parent all our children.
611 * Try to give them to another thread in our thread
612 * group, and if no such member exists, give it to
613 * the global child reaper process (ie "init")
615 static void forget_original_parent(struct task_struct
* father
,
616 struct list_head
*to_release
)
618 struct task_struct
*p
, *reaper
= father
;
619 struct list_head
*_p
, *_n
;
622 reaper
= next_thread(reaper
);
623 if (reaper
== father
) {
624 reaper
= child_reaper
;
627 } while (reaper
->exit_state
);
630 * There are only two places where our children can be:
632 * - in our child list
633 * - in our ptraced child list
635 * Search them and reparent children.
637 list_for_each_safe(_p
, _n
, &father
->children
) {
639 p
= list_entry(_p
,struct task_struct
,sibling
);
643 /* if father isn't the real parent, then ptrace must be enabled */
644 BUG_ON(father
!= p
->real_parent
&& !ptrace
);
646 if (father
== p
->real_parent
) {
647 /* reparent with a reaper, real father it's us */
648 choose_new_parent(p
, reaper
, child_reaper
);
649 reparent_thread(p
, father
, 0);
651 /* reparent ptraced task to its real parent */
653 if (p
->exit_state
== EXIT_ZOMBIE
&& p
->exit_signal
!= -1 &&
654 thread_group_empty(p
))
655 do_notify_parent(p
, p
->exit_signal
);
659 * if the ptraced child is a zombie with exit_signal == -1
660 * we must collect it before we exit, or it will remain
661 * zombie forever since we prevented it from self-reap itself
662 * while it was being traced by us, to be able to see it in wait4.
664 if (unlikely(ptrace
&& p
->exit_state
== EXIT_ZOMBIE
&& p
->exit_signal
== -1))
665 list_add(&p
->ptrace_list
, to_release
);
667 list_for_each_safe(_p
, _n
, &father
->ptrace_children
) {
668 p
= list_entry(_p
,struct task_struct
,ptrace_list
);
669 choose_new_parent(p
, reaper
, child_reaper
);
670 reparent_thread(p
, father
, 1);
675 * Send signals to all our closest relatives so that they know
676 * to properly mourn us..
678 static void exit_notify(struct task_struct
*tsk
)
681 struct task_struct
*t
;
682 struct list_head ptrace_dead
, *_p
, *_n
;
684 if (signal_pending(tsk
) && !(tsk
->signal
->flags
& SIGNAL_GROUP_EXIT
)
685 && !thread_group_empty(tsk
)) {
687 * This occurs when there was a race between our exit
688 * syscall and a group signal choosing us as the one to
689 * wake up. It could be that we are the only thread
690 * alerted to check for pending signals, but another thread
691 * should be woken now to take the signal since we will not.
692 * Now we'll wake all the threads in the group just to make
693 * sure someone gets all the pending signals.
695 read_lock(&tasklist_lock
);
696 spin_lock_irq(&tsk
->sighand
->siglock
);
697 for (t
= next_thread(tsk
); t
!= tsk
; t
= next_thread(t
))
698 if (!signal_pending(t
) && !(t
->flags
& PF_EXITING
)) {
699 recalc_sigpending_tsk(t
);
700 if (signal_pending(t
))
701 signal_wake_up(t
, 0);
703 spin_unlock_irq(&tsk
->sighand
->siglock
);
704 read_unlock(&tasklist_lock
);
707 write_lock_irq(&tasklist_lock
);
710 * This does two things:
712 * A. Make init inherit all the child processes
713 * B. Check to see if any process groups have become orphaned
714 * as a result of our exiting, and if they have any stopped
715 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
718 INIT_LIST_HEAD(&ptrace_dead
);
719 forget_original_parent(tsk
, &ptrace_dead
);
720 BUG_ON(!list_empty(&tsk
->children
));
721 BUG_ON(!list_empty(&tsk
->ptrace_children
));
724 * Check to see if any process groups have become orphaned
725 * as a result of our exiting, and if they have any stopped
726 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
728 * Case i: Our father is in a different pgrp than we are
729 * and we were the only connection outside, so our pgrp
730 * is about to become orphaned.
733 t
= tsk
->real_parent
;
735 if ((process_group(t
) != process_group(tsk
)) &&
736 (t
->signal
->session
== tsk
->signal
->session
) &&
737 will_become_orphaned_pgrp(process_group(tsk
), tsk
) &&
738 has_stopped_jobs(process_group(tsk
))) {
739 __kill_pg_info(SIGHUP
, SEND_SIG_PRIV
, process_group(tsk
));
740 __kill_pg_info(SIGCONT
, SEND_SIG_PRIV
, process_group(tsk
));
743 /* Let father know we died
745 * Thread signals are configurable, but you aren't going to use
746 * that to send signals to arbitary processes.
747 * That stops right now.
749 * If the parent exec id doesn't match the exec id we saved
750 * when we started then we know the parent has changed security
753 * If our self_exec id doesn't match our parent_exec_id then
754 * we have changed execution domain as these two values started
755 * the same after a fork.
759 if (tsk
->exit_signal
!= SIGCHLD
&& tsk
->exit_signal
!= -1 &&
760 ( tsk
->parent_exec_id
!= t
->self_exec_id
||
761 tsk
->self_exec_id
!= tsk
->parent_exec_id
)
762 && !capable(CAP_KILL
))
763 tsk
->exit_signal
= SIGCHLD
;
766 /* If something other than our normal parent is ptracing us, then
767 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
768 * only has special meaning to our real parent.
770 if (tsk
->exit_signal
!= -1 && thread_group_empty(tsk
)) {
771 int signal
= tsk
->parent
== tsk
->real_parent
? tsk
->exit_signal
: SIGCHLD
;
772 do_notify_parent(tsk
, signal
);
773 } else if (tsk
->ptrace
) {
774 do_notify_parent(tsk
, SIGCHLD
);
778 if (tsk
->exit_signal
== -1 &&
779 (likely(tsk
->ptrace
== 0) ||
780 unlikely(tsk
->parent
->signal
->flags
& SIGNAL_GROUP_EXIT
)))
782 tsk
->exit_state
= state
;
784 write_unlock_irq(&tasklist_lock
);
786 list_for_each_safe(_p
, _n
, &ptrace_dead
) {
788 t
= list_entry(_p
,struct task_struct
,ptrace_list
);
792 /* If the process is dead, release it - nobody will wait for it */
793 if (state
== EXIT_DEAD
)
797 fastcall NORET_TYPE
void do_exit(long code
)
799 struct task_struct
*tsk
= current
;
802 profile_task_exit(tsk
);
804 WARN_ON(atomic_read(&tsk
->fs_excl
));
806 if (unlikely(in_interrupt()))
807 panic("Aiee, killing interrupt handler!");
808 if (unlikely(!tsk
->pid
))
809 panic("Attempted to kill the idle task!");
810 if (unlikely(tsk
->pid
== 1))
811 panic("Attempted to kill init!");
813 if (unlikely(current
->ptrace
& PT_TRACE_EXIT
)) {
814 current
->ptrace_message
= code
;
815 ptrace_notify((PTRACE_EVENT_EXIT
<< 8) | SIGTRAP
);
819 * We're taking recursive faults here in do_exit. Safest is to just
820 * leave this task alone and wait for reboot.
822 if (unlikely(tsk
->flags
& PF_EXITING
)) {
824 "Fixing recursive fault but reboot is needed!\n");
827 set_current_state(TASK_UNINTERRUPTIBLE
);
831 tsk
->flags
|= PF_EXITING
;
834 * Make sure we don't try to process any timer firings
835 * while we are already exiting.
837 tsk
->it_virt_expires
= cputime_zero
;
838 tsk
->it_prof_expires
= cputime_zero
;
839 tsk
->it_sched_expires
= 0;
841 if (unlikely(in_atomic()))
842 printk(KERN_INFO
"note: %s[%d] exited with preempt_count %d\n",
843 current
->comm
, current
->pid
,
846 acct_update_integrals(tsk
);
848 update_hiwater_rss(tsk
->mm
);
849 update_hiwater_vm(tsk
->mm
);
851 group_dead
= atomic_dec_and_test(&tsk
->signal
->live
);
853 hrtimer_cancel(&tsk
->signal
->real_timer
);
854 exit_itimers(tsk
->signal
);
857 if (unlikely(tsk
->robust_list
))
858 exit_robust_list(tsk
);
860 if (unlikely(tsk
->compat_robust_list
))
861 compat_exit_robust_list(tsk
);
873 if (group_dead
&& tsk
->signal
->leader
)
874 disassociate_ctty(1);
876 module_put(task_thread_info(tsk
)->exec_domain
->module
);
878 module_put(tsk
->binfmt
->module
);
880 tsk
->exit_code
= code
;
881 proc_exit_connector(tsk
);
884 mpol_free(tsk
->mempolicy
);
885 tsk
->mempolicy
= NULL
;
888 * If DEBUG_MUTEXES is on, make sure we are holding no locks:
890 mutex_debug_check_no_locks_held(tsk
);
895 /* PF_DEAD causes final put_task_struct after we schedule. */
897 BUG_ON(tsk
->flags
& PF_DEAD
);
898 tsk
->flags
|= PF_DEAD
;
902 /* Avoid "noreturn function does return". */
906 EXPORT_SYMBOL_GPL(do_exit
);
908 NORET_TYPE
void complete_and_exit(struct completion
*comp
, long code
)
916 EXPORT_SYMBOL(complete_and_exit
);
918 asmlinkage
long sys_exit(int error_code
)
920 do_exit((error_code
&0xff)<<8);
923 task_t fastcall
*next_thread(const task_t
*p
)
925 return pid_task(p
->pids
[PIDTYPE_TGID
].pid_list
.next
, PIDTYPE_TGID
);
928 EXPORT_SYMBOL(next_thread
);
931 * Take down every thread in the group. This is called by fatal signals
932 * as well as by sys_exit_group (below).
935 do_group_exit(int exit_code
)
937 BUG_ON(exit_code
& 0x80); /* core dumps don't get here */
939 if (current
->signal
->flags
& SIGNAL_GROUP_EXIT
)
940 exit_code
= current
->signal
->group_exit_code
;
941 else if (!thread_group_empty(current
)) {
942 struct signal_struct
*const sig
= current
->signal
;
943 struct sighand_struct
*const sighand
= current
->sighand
;
944 read_lock(&tasklist_lock
);
945 spin_lock_irq(&sighand
->siglock
);
946 if (sig
->flags
& SIGNAL_GROUP_EXIT
)
947 /* Another thread got here before we took the lock. */
948 exit_code
= sig
->group_exit_code
;
950 sig
->group_exit_code
= exit_code
;
951 zap_other_threads(current
);
953 spin_unlock_irq(&sighand
->siglock
);
954 read_unlock(&tasklist_lock
);
962 * this kills every thread in the thread group. Note that any externally
963 * wait4()-ing process will get the correct exit code - even if this
964 * thread is not the thread group leader.
966 asmlinkage
void sys_exit_group(int error_code
)
968 do_group_exit((error_code
& 0xff) << 8);
971 static int eligible_child(pid_t pid
, int options
, task_t
*p
)
977 if (process_group(p
) != process_group(current
))
979 } else if (pid
!= -1) {
980 if (process_group(p
) != -pid
)
985 * Do not consider detached threads that are
988 if (p
->exit_signal
== -1 && !p
->ptrace
)
991 /* Wait for all children (clone and not) if __WALL is set;
992 * otherwise, wait for clone children *only* if __WCLONE is
993 * set; otherwise, wait for non-clone children *only*. (Note:
994 * A "clone" child here is one that reports to its parent
995 * using a signal other than SIGCHLD.) */
996 if (((p
->exit_signal
!= SIGCHLD
) ^ ((options
& __WCLONE
) != 0))
997 && !(options
& __WALL
))
1000 * Do not consider thread group leaders that are
1001 * in a non-empty thread group:
1003 if (current
->tgid
!= p
->tgid
&& delay_group_leader(p
))
1006 if (security_task_wait(p
))
1012 static int wait_noreap_copyout(task_t
*p
, pid_t pid
, uid_t uid
,
1013 int why
, int status
,
1014 struct siginfo __user
*infop
,
1015 struct rusage __user
*rusagep
)
1017 int retval
= rusagep
? getrusage(p
, RUSAGE_BOTH
, rusagep
) : 0;
1020 retval
= put_user(SIGCHLD
, &infop
->si_signo
);
1022 retval
= put_user(0, &infop
->si_errno
);
1024 retval
= put_user((short)why
, &infop
->si_code
);
1026 retval
= put_user(pid
, &infop
->si_pid
);
1028 retval
= put_user(uid
, &infop
->si_uid
);
1030 retval
= put_user(status
, &infop
->si_status
);
1037 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1038 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1039 * the lock and this task is uninteresting. If we return nonzero, we have
1040 * released the lock and the system call should return.
1042 static int wait_task_zombie(task_t
*p
, int noreap
,
1043 struct siginfo __user
*infop
,
1044 int __user
*stat_addr
, struct rusage __user
*ru
)
1046 unsigned long state
;
1050 if (unlikely(noreap
)) {
1053 int exit_code
= p
->exit_code
;
1056 if (unlikely(p
->exit_state
!= EXIT_ZOMBIE
))
1058 if (unlikely(p
->exit_signal
== -1 && p
->ptrace
== 0))
1061 read_unlock(&tasklist_lock
);
1062 if ((exit_code
& 0x7f) == 0) {
1064 status
= exit_code
>> 8;
1066 why
= (exit_code
& 0x80) ? CLD_DUMPED
: CLD_KILLED
;
1067 status
= exit_code
& 0x7f;
1069 return wait_noreap_copyout(p
, pid
, uid
, why
,
1074 * Try to move the task's state to DEAD
1075 * only one thread is allowed to do this:
1077 state
= xchg(&p
->exit_state
, EXIT_DEAD
);
1078 if (state
!= EXIT_ZOMBIE
) {
1079 BUG_ON(state
!= EXIT_DEAD
);
1082 if (unlikely(p
->exit_signal
== -1 && p
->ptrace
== 0)) {
1084 * This can only happen in a race with a ptraced thread
1085 * dying on another processor.
1090 if (likely(p
->real_parent
== p
->parent
) && likely(p
->signal
)) {
1091 struct signal_struct
*psig
;
1092 struct signal_struct
*sig
;
1095 * The resource counters for the group leader are in its
1096 * own task_struct. Those for dead threads in the group
1097 * are in its signal_struct, as are those for the child
1098 * processes it has previously reaped. All these
1099 * accumulate in the parent's signal_struct c* fields.
1101 * We don't bother to take a lock here to protect these
1102 * p->signal fields, because they are only touched by
1103 * __exit_signal, which runs with tasklist_lock
1104 * write-locked anyway, and so is excluded here. We do
1105 * need to protect the access to p->parent->signal fields,
1106 * as other threads in the parent group can be right
1107 * here reaping other children at the same time.
1109 spin_lock_irq(&p
->parent
->sighand
->siglock
);
1110 psig
= p
->parent
->signal
;
1113 cputime_add(psig
->cutime
,
1114 cputime_add(p
->utime
,
1115 cputime_add(sig
->utime
,
1118 cputime_add(psig
->cstime
,
1119 cputime_add(p
->stime
,
1120 cputime_add(sig
->stime
,
1123 p
->min_flt
+ sig
->min_flt
+ sig
->cmin_flt
;
1125 p
->maj_flt
+ sig
->maj_flt
+ sig
->cmaj_flt
;
1127 p
->nvcsw
+ sig
->nvcsw
+ sig
->cnvcsw
;
1129 p
->nivcsw
+ sig
->nivcsw
+ sig
->cnivcsw
;
1130 spin_unlock_irq(&p
->parent
->sighand
->siglock
);
1134 * Now we are sure this task is interesting, and no other
1135 * thread can reap it because we set its state to EXIT_DEAD.
1137 read_unlock(&tasklist_lock
);
1139 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
1140 status
= (p
->signal
->flags
& SIGNAL_GROUP_EXIT
)
1141 ? p
->signal
->group_exit_code
: p
->exit_code
;
1142 if (!retval
&& stat_addr
)
1143 retval
= put_user(status
, stat_addr
);
1144 if (!retval
&& infop
)
1145 retval
= put_user(SIGCHLD
, &infop
->si_signo
);
1146 if (!retval
&& infop
)
1147 retval
= put_user(0, &infop
->si_errno
);
1148 if (!retval
&& infop
) {
1151 if ((status
& 0x7f) == 0) {
1155 why
= (status
& 0x80) ? CLD_DUMPED
: CLD_KILLED
;
1158 retval
= put_user((short)why
, &infop
->si_code
);
1160 retval
= put_user(status
, &infop
->si_status
);
1162 if (!retval
&& infop
)
1163 retval
= put_user(p
->pid
, &infop
->si_pid
);
1164 if (!retval
&& infop
)
1165 retval
= put_user(p
->uid
, &infop
->si_uid
);
1167 // TODO: is this safe?
1168 p
->exit_state
= EXIT_ZOMBIE
;
1172 if (p
->real_parent
!= p
->parent
) {
1173 write_lock_irq(&tasklist_lock
);
1174 /* Double-check with lock held. */
1175 if (p
->real_parent
!= p
->parent
) {
1177 // TODO: is this safe?
1178 p
->exit_state
= EXIT_ZOMBIE
;
1180 * If this is not a detached task, notify the parent.
1181 * If it's still not detached after that, don't release
1184 if (p
->exit_signal
!= -1) {
1185 do_notify_parent(p
, p
->exit_signal
);
1186 if (p
->exit_signal
!= -1)
1190 write_unlock_irq(&tasklist_lock
);
1199 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1200 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1201 * the lock and this task is uninteresting. If we return nonzero, we have
1202 * released the lock and the system call should return.
1204 static int wait_task_stopped(task_t
*p
, int delayed_group_leader
, int noreap
,
1205 struct siginfo __user
*infop
,
1206 int __user
*stat_addr
, struct rusage __user
*ru
)
1208 int retval
, exit_code
;
1212 if (delayed_group_leader
&& !(p
->ptrace
& PT_PTRACED
) &&
1213 p
->signal
&& p
->signal
->group_stop_count
> 0)
1215 * A group stop is in progress and this is the group leader.
1216 * We won't report until all threads have stopped.
1221 * Now we are pretty sure this task is interesting.
1222 * Make sure it doesn't get reaped out from under us while we
1223 * give up the lock and then examine it below. We don't want to
1224 * keep holding onto the tasklist_lock while we call getrusage and
1225 * possibly take page faults for user memory.
1228 read_unlock(&tasklist_lock
);
1230 if (unlikely(noreap
)) {
1233 int why
= (p
->ptrace
& PT_PTRACED
) ? CLD_TRAPPED
: CLD_STOPPED
;
1235 exit_code
= p
->exit_code
;
1236 if (unlikely(!exit_code
) ||
1237 unlikely(p
->state
& TASK_TRACED
))
1239 return wait_noreap_copyout(p
, pid
, uid
,
1240 why
, (exit_code
<< 8) | 0x7f,
1244 write_lock_irq(&tasklist_lock
);
1247 * This uses xchg to be atomic with the thread resuming and setting
1248 * it. It must also be done with the write lock held to prevent a
1249 * race with the EXIT_ZOMBIE case.
1251 exit_code
= xchg(&p
->exit_code
, 0);
1252 if (unlikely(p
->exit_state
)) {
1254 * The task resumed and then died. Let the next iteration
1255 * catch it in EXIT_ZOMBIE. Note that exit_code might
1256 * already be zero here if it resumed and did _exit(0).
1257 * The task itself is dead and won't touch exit_code again;
1258 * other processors in this function are locked out.
1260 p
->exit_code
= exit_code
;
1263 if (unlikely(exit_code
== 0)) {
1265 * Another thread in this function got to it first, or it
1266 * resumed, or it resumed and then died.
1268 write_unlock_irq(&tasklist_lock
);
1272 * We are returning to the wait loop without having successfully
1273 * removed the process and having released the lock. We cannot
1274 * continue, since the "p" task pointer is potentially stale.
1276 * Return -EAGAIN, and do_wait() will restart the loop from the
1277 * beginning. Do _not_ re-acquire the lock.
1282 /* move to end of parent's list to avoid starvation */
1284 add_parent(p
, p
->parent
);
1286 write_unlock_irq(&tasklist_lock
);
1288 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
1289 if (!retval
&& stat_addr
)
1290 retval
= put_user((exit_code
<< 8) | 0x7f, stat_addr
);
1291 if (!retval
&& infop
)
1292 retval
= put_user(SIGCHLD
, &infop
->si_signo
);
1293 if (!retval
&& infop
)
1294 retval
= put_user(0, &infop
->si_errno
);
1295 if (!retval
&& infop
)
1296 retval
= put_user((short)((p
->ptrace
& PT_PTRACED
)
1297 ? CLD_TRAPPED
: CLD_STOPPED
),
1299 if (!retval
&& infop
)
1300 retval
= put_user(exit_code
, &infop
->si_status
);
1301 if (!retval
&& infop
)
1302 retval
= put_user(p
->pid
, &infop
->si_pid
);
1303 if (!retval
&& infop
)
1304 retval
= put_user(p
->uid
, &infop
->si_uid
);
1314 * Handle do_wait work for one task in a live, non-stopped state.
1315 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1316 * the lock and this task is uninteresting. If we return nonzero, we have
1317 * released the lock and the system call should return.
1319 static int wait_task_continued(task_t
*p
, int noreap
,
1320 struct siginfo __user
*infop
,
1321 int __user
*stat_addr
, struct rusage __user
*ru
)
1327 if (unlikely(!p
->signal
))
1330 if (!(p
->signal
->flags
& SIGNAL_STOP_CONTINUED
))
1333 spin_lock_irq(&p
->sighand
->siglock
);
1334 /* Re-check with the lock held. */
1335 if (!(p
->signal
->flags
& SIGNAL_STOP_CONTINUED
)) {
1336 spin_unlock_irq(&p
->sighand
->siglock
);
1340 p
->signal
->flags
&= ~SIGNAL_STOP_CONTINUED
;
1341 spin_unlock_irq(&p
->sighand
->siglock
);
1346 read_unlock(&tasklist_lock
);
1349 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
1351 if (!retval
&& stat_addr
)
1352 retval
= put_user(0xffff, stat_addr
);
1356 retval
= wait_noreap_copyout(p
, pid
, uid
,
1357 CLD_CONTINUED
, SIGCONT
,
1359 BUG_ON(retval
== 0);
1366 static inline int my_ptrace_child(struct task_struct
*p
)
1368 if (!(p
->ptrace
& PT_PTRACED
))
1370 if (!(p
->ptrace
& PT_ATTACHED
))
1373 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1374 * we are the attacher. If we are the real parent, this is a race
1375 * inside ptrace_attach. It is waiting for the tasklist_lock,
1376 * which we have to switch the parent links, but has already set
1377 * the flags in p->ptrace.
1379 return (p
->parent
!= p
->real_parent
);
1382 static long do_wait(pid_t pid
, int options
, struct siginfo __user
*infop
,
1383 int __user
*stat_addr
, struct rusage __user
*ru
)
1385 DECLARE_WAITQUEUE(wait
, current
);
1386 struct task_struct
*tsk
;
1389 add_wait_queue(¤t
->signal
->wait_chldexit
,&wait
);
1392 * We will set this flag if we see any child that might later
1393 * match our criteria, even if we are not able to reap it yet.
1396 current
->state
= TASK_INTERRUPTIBLE
;
1397 read_lock(&tasklist_lock
);
1400 struct task_struct
*p
;
1401 struct list_head
*_p
;
1404 list_for_each(_p
,&tsk
->children
) {
1405 p
= list_entry(_p
,struct task_struct
,sibling
);
1407 ret
= eligible_child(pid
, options
, p
);
1414 * When we hit the race with PTRACE_ATTACH,
1415 * we will not report this child. But the
1416 * race means it has not yet been moved to
1417 * our ptrace_children list, so we need to
1418 * set the flag here to avoid a spurious ECHILD
1419 * when the race happens with the only child.
1422 if (!my_ptrace_child(p
))
1427 * It's stopped now, so it might later
1428 * continue, exit, or stop again.
1431 if (!(options
& WUNTRACED
) &&
1432 !my_ptrace_child(p
))
1434 retval
= wait_task_stopped(p
, ret
== 2,
1435 (options
& WNOWAIT
),
1438 if (retval
== -EAGAIN
)
1440 if (retval
!= 0) /* He released the lock. */
1445 if (p
->exit_state
== EXIT_DEAD
)
1447 // case EXIT_ZOMBIE:
1448 if (p
->exit_state
== EXIT_ZOMBIE
) {
1450 * Eligible but we cannot release
1454 goto check_continued
;
1455 if (!likely(options
& WEXITED
))
1457 retval
= wait_task_zombie(
1458 p
, (options
& WNOWAIT
),
1459 infop
, stat_addr
, ru
);
1460 /* He released the lock. */
1467 * It's running now, so it might later
1468 * exit, stop, or stop and then continue.
1471 if (!unlikely(options
& WCONTINUED
))
1473 retval
= wait_task_continued(
1474 p
, (options
& WNOWAIT
),
1475 infop
, stat_addr
, ru
);
1476 if (retval
!= 0) /* He released the lock. */
1482 list_for_each(_p
, &tsk
->ptrace_children
) {
1483 p
= list_entry(_p
, struct task_struct
,
1485 if (!eligible_child(pid
, options
, p
))
1491 if (options
& __WNOTHREAD
)
1493 tsk
= next_thread(tsk
);
1494 if (tsk
->signal
!= current
->signal
)
1496 } while (tsk
!= current
);
1498 read_unlock(&tasklist_lock
);
1501 if (options
& WNOHANG
)
1503 retval
= -ERESTARTSYS
;
1504 if (signal_pending(current
))
1511 current
->state
= TASK_RUNNING
;
1512 remove_wait_queue(¤t
->signal
->wait_chldexit
,&wait
);
1518 * For a WNOHANG return, clear out all the fields
1519 * we would set so the user can easily tell the
1523 retval
= put_user(0, &infop
->si_signo
);
1525 retval
= put_user(0, &infop
->si_errno
);
1527 retval
= put_user(0, &infop
->si_code
);
1529 retval
= put_user(0, &infop
->si_pid
);
1531 retval
= put_user(0, &infop
->si_uid
);
1533 retval
= put_user(0, &infop
->si_status
);
1539 asmlinkage
long sys_waitid(int which
, pid_t pid
,
1540 struct siginfo __user
*infop
, int options
,
1541 struct rusage __user
*ru
)
1545 if (options
& ~(WNOHANG
|WNOWAIT
|WEXITED
|WSTOPPED
|WCONTINUED
))
1547 if (!(options
& (WEXITED
|WSTOPPED
|WCONTINUED
)))
1567 ret
= do_wait(pid
, options
, infop
, NULL
, ru
);
1569 /* avoid REGPARM breakage on x86: */
1570 prevent_tail_call(ret
);
1574 asmlinkage
long sys_wait4(pid_t pid
, int __user
*stat_addr
,
1575 int options
, struct rusage __user
*ru
)
1579 if (options
& ~(WNOHANG
|WUNTRACED
|WCONTINUED
|
1580 __WNOTHREAD
|__WCLONE
|__WALL
))
1582 ret
= do_wait(pid
, options
| WEXITED
, NULL
, stat_addr
, ru
);
1584 /* avoid REGPARM breakage on x86: */
1585 prevent_tail_call(ret
);
1589 #ifdef __ARCH_WANT_SYS_WAITPID
1592 * sys_waitpid() remains for compatibility. waitpid() should be
1593 * implemented by calling sys_wait4() from libc.a.
1595 asmlinkage
long sys_waitpid(pid_t pid
, int __user
*stat_addr
, int options
)
1597 return sys_wait4(pid
, stat_addr
, options
, NULL
);