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>
36 #include <asm/uaccess.h>
37 #include <asm/unistd.h>
38 #include <asm/pgtable.h>
39 #include <asm/mmu_context.h>
41 extern void sem_exit (void);
42 extern struct task_struct
*child_reaper
;
44 int getrusage(struct task_struct
*, int, struct rusage __user
*);
46 static void exit_mm(struct task_struct
* tsk
);
48 static void __unhash_process(struct task_struct
*p
)
51 detach_pid(p
, PIDTYPE_PID
);
52 detach_pid(p
, PIDTYPE_TGID
);
53 if (thread_group_leader(p
)) {
54 detach_pid(p
, PIDTYPE_PGID
);
55 detach_pid(p
, PIDTYPE_SID
);
57 __get_cpu_var(process_counts
)--;
63 void release_task(struct task_struct
* p
)
67 struct dentry
*proc_dentry
;
70 atomic_dec(&p
->user
->processes
);
71 spin_lock(&p
->proc_lock
);
72 proc_dentry
= proc_pid_unhash(p
);
73 write_lock_irq(&tasklist_lock
);
74 if (unlikely(p
->ptrace
))
76 BUG_ON(!list_empty(&p
->ptrace_list
) || !list_empty(&p
->ptrace_children
));
79 * Note that the fastpath in sys_times depends on __exit_signal having
80 * updated the counters before a task is removed from the tasklist of
81 * the process by __unhash_process.
86 * If we are the last non-leader member of the thread
87 * group, and the leader is zombie, then notify the
88 * group leader's parent process. (if it wants notification.)
91 leader
= p
->group_leader
;
92 if (leader
!= p
&& thread_group_empty(leader
) && leader
->exit_state
== EXIT_ZOMBIE
) {
93 BUG_ON(leader
->exit_signal
== -1);
94 do_notify_parent(leader
, leader
->exit_signal
);
96 * If we were the last child thread and the leader has
97 * exited already, and the leader's parent ignores SIGCHLD,
98 * then we are the one who should release the leader.
100 * do_notify_parent() will have marked it self-reaping in
103 zap_leader
= (leader
->exit_signal
== -1);
107 write_unlock_irq(&tasklist_lock
);
108 spin_unlock(&p
->proc_lock
);
109 proc_pid_flush(proc_dentry
);
114 if (unlikely(zap_leader
))
118 /* we are using it only for SMP init */
120 void unhash_process(struct task_struct
*p
)
122 struct dentry
*proc_dentry
;
124 spin_lock(&p
->proc_lock
);
125 proc_dentry
= proc_pid_unhash(p
);
126 write_lock_irq(&tasklist_lock
);
128 write_unlock_irq(&tasklist_lock
);
129 spin_unlock(&p
->proc_lock
);
130 proc_pid_flush(proc_dentry
);
134 * This checks not only the pgrp, but falls back on the pid if no
135 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
138 int session_of_pgrp(int pgrp
)
140 struct task_struct
*p
;
143 read_lock(&tasklist_lock
);
144 do_each_task_pid(pgrp
, PIDTYPE_PGID
, p
) {
145 if (p
->signal
->session
> 0) {
146 sid
= p
->signal
->session
;
149 } while_each_task_pid(pgrp
, PIDTYPE_PGID
, p
);
150 p
= find_task_by_pid(pgrp
);
152 sid
= p
->signal
->session
;
154 read_unlock(&tasklist_lock
);
160 * Determine if a process group is "orphaned", according to the POSIX
161 * definition in 2.2.2.52. Orphaned process groups are not to be affected
162 * by terminal-generated stop signals. Newly orphaned process groups are
163 * to receive a SIGHUP and a SIGCONT.
165 * "I ask you, have you ever known what it is to be an orphan?"
167 static int will_become_orphaned_pgrp(int pgrp
, task_t
*ignored_task
)
169 struct task_struct
*p
;
172 do_each_task_pid(pgrp
, PIDTYPE_PGID
, p
) {
173 if (p
== ignored_task
175 || p
->real_parent
->pid
== 1)
177 if (process_group(p
->real_parent
) != pgrp
178 && p
->real_parent
->signal
->session
== p
->signal
->session
) {
182 } while_each_task_pid(pgrp
, PIDTYPE_PGID
, p
);
183 return ret
; /* (sighing) "Often!" */
186 int is_orphaned_pgrp(int pgrp
)
190 read_lock(&tasklist_lock
);
191 retval
= will_become_orphaned_pgrp(pgrp
, NULL
);
192 read_unlock(&tasklist_lock
);
197 static int has_stopped_jobs(int pgrp
)
200 struct task_struct
*p
;
202 do_each_task_pid(pgrp
, PIDTYPE_PGID
, p
) {
203 if (p
->state
!= TASK_STOPPED
)
206 /* If p is stopped by a debugger on a signal that won't
207 stop it, then don't count p as stopped. This isn't
208 perfect but it's a good approximation. */
209 if (unlikely (p
->ptrace
)
210 && p
->exit_code
!= SIGSTOP
211 && p
->exit_code
!= SIGTSTP
212 && p
->exit_code
!= SIGTTOU
213 && p
->exit_code
!= SIGTTIN
)
218 } while_each_task_pid(pgrp
, PIDTYPE_PGID
, p
);
223 * reparent_to_init - Reparent the calling kernel thread to the init task.
225 * If a kernel thread is launched as a result of a system call, or if
226 * it ever exits, it should generally reparent itself to init so that
227 * it is correctly cleaned up on exit.
229 * The various task state such as scheduling policy and priority may have
230 * been inherited from a user process, so we reset them to sane values here.
232 * NOTE that reparent_to_init() gives the caller full capabilities.
234 static void reparent_to_init(void)
236 write_lock_irq(&tasklist_lock
);
238 ptrace_unlink(current
);
239 /* Reparent to init */
240 REMOVE_LINKS(current
);
241 current
->parent
= child_reaper
;
242 current
->real_parent
= child_reaper
;
245 /* Set the exit signal to SIGCHLD so we signal init on exit */
246 current
->exit_signal
= SIGCHLD
;
248 if ((current
->policy
== SCHED_NORMAL
||
249 current
->policy
== SCHED_BATCH
)
250 && (task_nice(current
) < 0))
251 set_user_nice(current
, 0);
255 security_task_reparent_to_init(current
);
256 memcpy(current
->signal
->rlim
, init_task
.signal
->rlim
,
257 sizeof(current
->signal
->rlim
));
258 atomic_inc(&(INIT_USER
->__count
));
259 write_unlock_irq(&tasklist_lock
);
260 switch_uid(INIT_USER
);
263 void __set_special_pids(pid_t session
, pid_t pgrp
)
265 struct task_struct
*curr
= current
->group_leader
;
267 if (curr
->signal
->session
!= session
) {
268 detach_pid(curr
, PIDTYPE_SID
);
269 curr
->signal
->session
= session
;
270 attach_pid(curr
, PIDTYPE_SID
, session
);
272 if (process_group(curr
) != pgrp
) {
273 detach_pid(curr
, PIDTYPE_PGID
);
274 curr
->signal
->pgrp
= pgrp
;
275 attach_pid(curr
, PIDTYPE_PGID
, pgrp
);
279 void set_special_pids(pid_t session
, pid_t pgrp
)
281 write_lock_irq(&tasklist_lock
);
282 __set_special_pids(session
, pgrp
);
283 write_unlock_irq(&tasklist_lock
);
287 * Let kernel threads use this to say that they
288 * allow a certain signal (since daemonize() will
289 * have disabled all of them by default).
291 int allow_signal(int sig
)
293 if (!valid_signal(sig
) || sig
< 1)
296 spin_lock_irq(¤t
->sighand
->siglock
);
297 sigdelset(¤t
->blocked
, sig
);
299 /* Kernel threads handle their own signals.
300 Let the signal code know it'll be handled, so
301 that they don't get converted to SIGKILL or
302 just silently dropped */
303 current
->sighand
->action
[(sig
)-1].sa
.sa_handler
= (void __user
*)2;
306 spin_unlock_irq(¤t
->sighand
->siglock
);
310 EXPORT_SYMBOL(allow_signal
);
312 int disallow_signal(int sig
)
314 if (!valid_signal(sig
) || sig
< 1)
317 spin_lock_irq(¤t
->sighand
->siglock
);
318 sigaddset(¤t
->blocked
, sig
);
320 spin_unlock_irq(¤t
->sighand
->siglock
);
324 EXPORT_SYMBOL(disallow_signal
);
327 * Put all the gunge required to become a kernel thread without
328 * attached user resources in one place where it belongs.
331 void daemonize(const char *name
, ...)
334 struct fs_struct
*fs
;
337 va_start(args
, name
);
338 vsnprintf(current
->comm
, sizeof(current
->comm
), name
, args
);
342 * If we were started as result of loading a module, close all of the
343 * user space pages. We don't need them, and if we didn't close them
344 * they would be locked into memory.
348 set_special_pids(1, 1);
349 mutex_lock(&tty_mutex
);
350 current
->signal
->tty
= NULL
;
351 mutex_unlock(&tty_mutex
);
353 /* Block and flush all signals */
354 sigfillset(&blocked
);
355 sigprocmask(SIG_BLOCK
, &blocked
, NULL
);
356 flush_signals(current
);
358 /* Become as one with the init task */
360 exit_fs(current
); /* current->fs->count--; */
363 atomic_inc(&fs
->count
);
364 exit_namespace(current
);
365 current
->namespace = init_task
.namespace;
366 get_namespace(current
->namespace);
368 current
->files
= init_task
.files
;
369 atomic_inc(¤t
->files
->count
);
374 EXPORT_SYMBOL(daemonize
);
376 static void close_files(struct files_struct
* files
)
384 * It is safe to dereference the fd table without RCU or
385 * ->file_lock because this is the last reference to the
388 fdt
= files_fdtable(files
);
392 if (i
>= fdt
->max_fdset
|| i
>= fdt
->max_fds
)
394 set
= fdt
->open_fds
->fds_bits
[j
++];
397 struct file
* file
= xchg(&fdt
->fd
[i
], NULL
);
399 filp_close(file
, files
);
407 struct files_struct
*get_files_struct(struct task_struct
*task
)
409 struct files_struct
*files
;
414 atomic_inc(&files
->count
);
420 void fastcall
put_files_struct(struct files_struct
*files
)
424 if (atomic_dec_and_test(&files
->count
)) {
427 * Free the fd and fdset arrays if we expanded them.
428 * If the fdtable was embedded, pass files for freeing
429 * at the end of the RCU grace period. Otherwise,
430 * you can free files immediately.
432 fdt
= files_fdtable(files
);
433 if (fdt
== &files
->fdtab
)
434 fdt
->free_files
= files
;
436 kmem_cache_free(files_cachep
, files
);
441 EXPORT_SYMBOL(put_files_struct
);
443 static inline void __exit_files(struct task_struct
*tsk
)
445 struct files_struct
* files
= tsk
->files
;
451 put_files_struct(files
);
455 void exit_files(struct task_struct
*tsk
)
460 static inline void __put_fs_struct(struct fs_struct
*fs
)
462 /* No need to hold fs->lock if we are killing it */
463 if (atomic_dec_and_test(&fs
->count
)) {
470 mntput(fs
->altrootmnt
);
472 kmem_cache_free(fs_cachep
, fs
);
476 void put_fs_struct(struct fs_struct
*fs
)
481 static inline void __exit_fs(struct task_struct
*tsk
)
483 struct fs_struct
* fs
= tsk
->fs
;
493 void exit_fs(struct task_struct
*tsk
)
498 EXPORT_SYMBOL_GPL(exit_fs
);
501 * Turn us into a lazy TLB process if we
504 static void exit_mm(struct task_struct
* tsk
)
506 struct mm_struct
*mm
= tsk
->mm
;
512 * Serialize with any possible pending coredump.
513 * We must hold mmap_sem around checking core_waiters
514 * and clearing tsk->mm. The core-inducing thread
515 * will increment core_waiters for each thread in the
516 * group with ->mm != NULL.
518 down_read(&mm
->mmap_sem
);
519 if (mm
->core_waiters
) {
520 up_read(&mm
->mmap_sem
);
521 down_write(&mm
->mmap_sem
);
522 if (!--mm
->core_waiters
)
523 complete(mm
->core_startup_done
);
524 up_write(&mm
->mmap_sem
);
526 wait_for_completion(&mm
->core_done
);
527 down_read(&mm
->mmap_sem
);
529 atomic_inc(&mm
->mm_count
);
530 if (mm
!= tsk
->active_mm
) BUG();
531 /* more a memory barrier than a real lock */
534 up_read(&mm
->mmap_sem
);
535 enter_lazy_tlb(mm
, current
);
540 static inline void choose_new_parent(task_t
*p
, task_t
*reaper
, task_t
*child_reaper
)
543 * Make sure we're not reparenting to ourselves and that
544 * the parent is not a zombie.
546 BUG_ON(p
== reaper
|| reaper
->exit_state
>= EXIT_ZOMBIE
);
547 p
->real_parent
= reaper
;
550 static void reparent_thread(task_t
*p
, task_t
*father
, int traced
)
552 /* We don't want people slaying init. */
553 if (p
->exit_signal
!= -1)
554 p
->exit_signal
= SIGCHLD
;
556 if (p
->pdeath_signal
)
557 /* We already hold the tasklist_lock here. */
558 group_send_sig_info(p
->pdeath_signal
, SEND_SIG_NOINFO
, p
);
560 /* Move the child from its dying parent to the new one. */
561 if (unlikely(traced
)) {
562 /* Preserve ptrace links if someone else is tracing this child. */
563 list_del_init(&p
->ptrace_list
);
564 if (p
->parent
!= p
->real_parent
)
565 list_add(&p
->ptrace_list
, &p
->real_parent
->ptrace_children
);
567 /* If this child is being traced, then we're the one tracing it
568 * anyway, so let go of it.
571 list_del_init(&p
->sibling
);
572 p
->parent
= p
->real_parent
;
573 list_add_tail(&p
->sibling
, &p
->parent
->children
);
575 /* If we'd notified the old parent about this child's death,
576 * also notify the new parent.
578 if (p
->exit_state
== EXIT_ZOMBIE
&& p
->exit_signal
!= -1 &&
579 thread_group_empty(p
))
580 do_notify_parent(p
, p
->exit_signal
);
581 else if (p
->state
== TASK_TRACED
) {
583 * If it was at a trace stop, turn it into
584 * a normal stop since it's no longer being
592 * process group orphan check
593 * Case ii: Our child is in a different pgrp
594 * than we are, and it was the only connection
595 * outside, so the child pgrp is now orphaned.
597 if ((process_group(p
) != process_group(father
)) &&
598 (p
->signal
->session
== father
->signal
->session
)) {
599 int pgrp
= process_group(p
);
601 if (will_become_orphaned_pgrp(pgrp
, NULL
) && has_stopped_jobs(pgrp
)) {
602 __kill_pg_info(SIGHUP
, SEND_SIG_PRIV
, pgrp
);
603 __kill_pg_info(SIGCONT
, SEND_SIG_PRIV
, pgrp
);
609 * When we die, we re-parent all our children.
610 * Try to give them to another thread in our thread
611 * group, and if no such member exists, give it to
612 * the global child reaper process (ie "init")
614 static void forget_original_parent(struct task_struct
* father
,
615 struct list_head
*to_release
)
617 struct task_struct
*p
, *reaper
= father
;
618 struct list_head
*_p
, *_n
;
621 reaper
= next_thread(reaper
);
622 if (reaper
== father
) {
623 reaper
= child_reaper
;
626 } while (reaper
->exit_state
);
629 * There are only two places where our children can be:
631 * - in our child list
632 * - in our ptraced child list
634 * Search them and reparent children.
636 list_for_each_safe(_p
, _n
, &father
->children
) {
638 p
= list_entry(_p
,struct task_struct
,sibling
);
642 /* if father isn't the real parent, then ptrace must be enabled */
643 BUG_ON(father
!= p
->real_parent
&& !ptrace
);
645 if (father
== p
->real_parent
) {
646 /* reparent with a reaper, real father it's us */
647 choose_new_parent(p
, reaper
, child_reaper
);
648 reparent_thread(p
, father
, 0);
650 /* reparent ptraced task to its real parent */
652 if (p
->exit_state
== EXIT_ZOMBIE
&& p
->exit_signal
!= -1 &&
653 thread_group_empty(p
))
654 do_notify_parent(p
, p
->exit_signal
);
658 * if the ptraced child is a zombie with exit_signal == -1
659 * we must collect it before we exit, or it will remain
660 * zombie forever since we prevented it from self-reap itself
661 * while it was being traced by us, to be able to see it in wait4.
663 if (unlikely(ptrace
&& p
->exit_state
== EXIT_ZOMBIE
&& p
->exit_signal
== -1))
664 list_add(&p
->ptrace_list
, to_release
);
666 list_for_each_safe(_p
, _n
, &father
->ptrace_children
) {
667 p
= list_entry(_p
,struct task_struct
,ptrace_list
);
668 choose_new_parent(p
, reaper
, child_reaper
);
669 reparent_thread(p
, father
, 1);
674 * Send signals to all our closest relatives so that they know
675 * to properly mourn us..
677 static void exit_notify(struct task_struct
*tsk
)
680 struct task_struct
*t
;
681 struct list_head ptrace_dead
, *_p
, *_n
;
683 if (signal_pending(tsk
) && !(tsk
->signal
->flags
& SIGNAL_GROUP_EXIT
)
684 && !thread_group_empty(tsk
)) {
686 * This occurs when there was a race between our exit
687 * syscall and a group signal choosing us as the one to
688 * wake up. It could be that we are the only thread
689 * alerted to check for pending signals, but another thread
690 * should be woken now to take the signal since we will not.
691 * Now we'll wake all the threads in the group just to make
692 * sure someone gets all the pending signals.
694 read_lock(&tasklist_lock
);
695 spin_lock_irq(&tsk
->sighand
->siglock
);
696 for (t
= next_thread(tsk
); t
!= tsk
; t
= next_thread(t
))
697 if (!signal_pending(t
) && !(t
->flags
& PF_EXITING
)) {
698 recalc_sigpending_tsk(t
);
699 if (signal_pending(t
))
700 signal_wake_up(t
, 0);
702 spin_unlock_irq(&tsk
->sighand
->siglock
);
703 read_unlock(&tasklist_lock
);
706 write_lock_irq(&tasklist_lock
);
709 * This does two things:
711 * A. Make init inherit all the child processes
712 * B. Check to see if any process groups have become orphaned
713 * as a result of our exiting, and if they have any stopped
714 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
717 INIT_LIST_HEAD(&ptrace_dead
);
718 forget_original_parent(tsk
, &ptrace_dead
);
719 BUG_ON(!list_empty(&tsk
->children
));
720 BUG_ON(!list_empty(&tsk
->ptrace_children
));
723 * Check to see if any process groups have become orphaned
724 * as a result of our exiting, and if they have any stopped
725 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
727 * Case i: Our father is in a different pgrp than we are
728 * and we were the only connection outside, so our pgrp
729 * is about to become orphaned.
732 t
= tsk
->real_parent
;
734 if ((process_group(t
) != process_group(tsk
)) &&
735 (t
->signal
->session
== tsk
->signal
->session
) &&
736 will_become_orphaned_pgrp(process_group(tsk
), tsk
) &&
737 has_stopped_jobs(process_group(tsk
))) {
738 __kill_pg_info(SIGHUP
, SEND_SIG_PRIV
, process_group(tsk
));
739 __kill_pg_info(SIGCONT
, SEND_SIG_PRIV
, process_group(tsk
));
742 /* Let father know we died
744 * Thread signals are configurable, but you aren't going to use
745 * that to send signals to arbitary processes.
746 * That stops right now.
748 * If the parent exec id doesn't match the exec id we saved
749 * when we started then we know the parent has changed security
752 * If our self_exec id doesn't match our parent_exec_id then
753 * we have changed execution domain as these two values started
754 * the same after a fork.
758 if (tsk
->exit_signal
!= SIGCHLD
&& tsk
->exit_signal
!= -1 &&
759 ( tsk
->parent_exec_id
!= t
->self_exec_id
||
760 tsk
->self_exec_id
!= tsk
->parent_exec_id
)
761 && !capable(CAP_KILL
))
762 tsk
->exit_signal
= SIGCHLD
;
765 /* If something other than our normal parent is ptracing us, then
766 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
767 * only has special meaning to our real parent.
769 if (tsk
->exit_signal
!= -1 && thread_group_empty(tsk
)) {
770 int signal
= tsk
->parent
== tsk
->real_parent
? tsk
->exit_signal
: SIGCHLD
;
771 do_notify_parent(tsk
, signal
);
772 } else if (tsk
->ptrace
) {
773 do_notify_parent(tsk
, SIGCHLD
);
777 if (tsk
->exit_signal
== -1 &&
778 (likely(tsk
->ptrace
== 0) ||
779 unlikely(tsk
->parent
->signal
->flags
& SIGNAL_GROUP_EXIT
)))
781 tsk
->exit_state
= state
;
783 write_unlock_irq(&tasklist_lock
);
785 list_for_each_safe(_p
, _n
, &ptrace_dead
) {
787 t
= list_entry(_p
,struct task_struct
,ptrace_list
);
791 /* If the process is dead, release it - nobody will wait for it */
792 if (state
== EXIT_DEAD
)
796 fastcall NORET_TYPE
void do_exit(long code
)
798 struct task_struct
*tsk
= current
;
801 profile_task_exit(tsk
);
803 WARN_ON(atomic_read(&tsk
->fs_excl
));
805 if (unlikely(in_interrupt()))
806 panic("Aiee, killing interrupt handler!");
807 if (unlikely(!tsk
->pid
))
808 panic("Attempted to kill the idle task!");
809 if (unlikely(tsk
->pid
== 1))
810 panic("Attempted to kill init!");
812 if (unlikely(current
->ptrace
& PT_TRACE_EXIT
)) {
813 current
->ptrace_message
= code
;
814 ptrace_notify((PTRACE_EVENT_EXIT
<< 8) | SIGTRAP
);
818 * We're taking recursive faults here in do_exit. Safest is to just
819 * leave this task alone and wait for reboot.
821 if (unlikely(tsk
->flags
& PF_EXITING
)) {
823 "Fixing recursive fault but reboot is needed!\n");
826 set_current_state(TASK_UNINTERRUPTIBLE
);
830 tsk
->flags
|= PF_EXITING
;
833 * Make sure we don't try to process any timer firings
834 * while we are already exiting.
836 tsk
->it_virt_expires
= cputime_zero
;
837 tsk
->it_prof_expires
= cputime_zero
;
838 tsk
->it_sched_expires
= 0;
840 if (unlikely(in_atomic()))
841 printk(KERN_INFO
"note: %s[%d] exited with preempt_count %d\n",
842 current
->comm
, current
->pid
,
845 acct_update_integrals(tsk
);
847 update_hiwater_rss(tsk
->mm
);
848 update_hiwater_vm(tsk
->mm
);
850 group_dead
= atomic_dec_and_test(&tsk
->signal
->live
);
852 hrtimer_cancel(&tsk
->signal
->real_timer
);
853 exit_itimers(tsk
->signal
);
856 if (unlikely(tsk
->robust_list
))
857 exit_robust_list(tsk
);
868 if (group_dead
&& tsk
->signal
->leader
)
869 disassociate_ctty(1);
871 module_put(task_thread_info(tsk
)->exec_domain
->module
);
873 module_put(tsk
->binfmt
->module
);
875 tsk
->exit_code
= code
;
876 proc_exit_connector(tsk
);
879 mpol_free(tsk
->mempolicy
);
880 tsk
->mempolicy
= NULL
;
883 * If DEBUG_MUTEXES is on, make sure we are holding no locks:
885 mutex_debug_check_no_locks_held(tsk
);
890 /* PF_DEAD causes final put_task_struct after we schedule. */
892 BUG_ON(tsk
->flags
& PF_DEAD
);
893 tsk
->flags
|= PF_DEAD
;
897 /* Avoid "noreturn function does return". */
901 EXPORT_SYMBOL_GPL(do_exit
);
903 NORET_TYPE
void complete_and_exit(struct completion
*comp
, long code
)
911 EXPORT_SYMBOL(complete_and_exit
);
913 asmlinkage
long sys_exit(int error_code
)
915 do_exit((error_code
&0xff)<<8);
918 task_t fastcall
*next_thread(const task_t
*p
)
920 return pid_task(p
->pids
[PIDTYPE_TGID
].pid_list
.next
, PIDTYPE_TGID
);
923 EXPORT_SYMBOL(next_thread
);
926 * Take down every thread in the group. This is called by fatal signals
927 * as well as by sys_exit_group (below).
930 do_group_exit(int exit_code
)
932 BUG_ON(exit_code
& 0x80); /* core dumps don't get here */
934 if (current
->signal
->flags
& SIGNAL_GROUP_EXIT
)
935 exit_code
= current
->signal
->group_exit_code
;
936 else if (!thread_group_empty(current
)) {
937 struct signal_struct
*const sig
= current
->signal
;
938 struct sighand_struct
*const sighand
= current
->sighand
;
939 read_lock(&tasklist_lock
);
940 spin_lock_irq(&sighand
->siglock
);
941 if (sig
->flags
& SIGNAL_GROUP_EXIT
)
942 /* Another thread got here before we took the lock. */
943 exit_code
= sig
->group_exit_code
;
945 sig
->group_exit_code
= exit_code
;
946 zap_other_threads(current
);
948 spin_unlock_irq(&sighand
->siglock
);
949 read_unlock(&tasklist_lock
);
957 * this kills every thread in the thread group. Note that any externally
958 * wait4()-ing process will get the correct exit code - even if this
959 * thread is not the thread group leader.
961 asmlinkage
void sys_exit_group(int error_code
)
963 do_group_exit((error_code
& 0xff) << 8);
966 static int eligible_child(pid_t pid
, int options
, task_t
*p
)
972 if (process_group(p
) != process_group(current
))
974 } else if (pid
!= -1) {
975 if (process_group(p
) != -pid
)
980 * Do not consider detached threads that are
983 if (p
->exit_signal
== -1 && !p
->ptrace
)
986 /* Wait for all children (clone and not) if __WALL is set;
987 * otherwise, wait for clone children *only* if __WCLONE is
988 * set; otherwise, wait for non-clone children *only*. (Note:
989 * A "clone" child here is one that reports to its parent
990 * using a signal other than SIGCHLD.) */
991 if (((p
->exit_signal
!= SIGCHLD
) ^ ((options
& __WCLONE
) != 0))
992 && !(options
& __WALL
))
995 * Do not consider thread group leaders that are
996 * in a non-empty thread group:
998 if (current
->tgid
!= p
->tgid
&& delay_group_leader(p
))
1001 if (security_task_wait(p
))
1007 static int wait_noreap_copyout(task_t
*p
, pid_t pid
, uid_t uid
,
1008 int why
, int status
,
1009 struct siginfo __user
*infop
,
1010 struct rusage __user
*rusagep
)
1012 int retval
= rusagep
? getrusage(p
, RUSAGE_BOTH
, rusagep
) : 0;
1015 retval
= put_user(SIGCHLD
, &infop
->si_signo
);
1017 retval
= put_user(0, &infop
->si_errno
);
1019 retval
= put_user((short)why
, &infop
->si_code
);
1021 retval
= put_user(pid
, &infop
->si_pid
);
1023 retval
= put_user(uid
, &infop
->si_uid
);
1025 retval
= put_user(status
, &infop
->si_status
);
1032 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1033 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1034 * the lock and this task is uninteresting. If we return nonzero, we have
1035 * released the lock and the system call should return.
1037 static int wait_task_zombie(task_t
*p
, int noreap
,
1038 struct siginfo __user
*infop
,
1039 int __user
*stat_addr
, struct rusage __user
*ru
)
1041 unsigned long state
;
1045 if (unlikely(noreap
)) {
1048 int exit_code
= p
->exit_code
;
1051 if (unlikely(p
->exit_state
!= EXIT_ZOMBIE
))
1053 if (unlikely(p
->exit_signal
== -1 && p
->ptrace
== 0))
1056 read_unlock(&tasklist_lock
);
1057 if ((exit_code
& 0x7f) == 0) {
1059 status
= exit_code
>> 8;
1061 why
= (exit_code
& 0x80) ? CLD_DUMPED
: CLD_KILLED
;
1062 status
= exit_code
& 0x7f;
1064 return wait_noreap_copyout(p
, pid
, uid
, why
,
1069 * Try to move the task's state to DEAD
1070 * only one thread is allowed to do this:
1072 state
= xchg(&p
->exit_state
, EXIT_DEAD
);
1073 if (state
!= EXIT_ZOMBIE
) {
1074 BUG_ON(state
!= EXIT_DEAD
);
1077 if (unlikely(p
->exit_signal
== -1 && p
->ptrace
== 0)) {
1079 * This can only happen in a race with a ptraced thread
1080 * dying on another processor.
1085 if (likely(p
->real_parent
== p
->parent
) && likely(p
->signal
)) {
1086 struct signal_struct
*psig
;
1087 struct signal_struct
*sig
;
1090 * The resource counters for the group leader are in its
1091 * own task_struct. Those for dead threads in the group
1092 * are in its signal_struct, as are those for the child
1093 * processes it has previously reaped. All these
1094 * accumulate in the parent's signal_struct c* fields.
1096 * We don't bother to take a lock here to protect these
1097 * p->signal fields, because they are only touched by
1098 * __exit_signal, which runs with tasklist_lock
1099 * write-locked anyway, and so is excluded here. We do
1100 * need to protect the access to p->parent->signal fields,
1101 * as other threads in the parent group can be right
1102 * here reaping other children at the same time.
1104 spin_lock_irq(&p
->parent
->sighand
->siglock
);
1105 psig
= p
->parent
->signal
;
1108 cputime_add(psig
->cutime
,
1109 cputime_add(p
->utime
,
1110 cputime_add(sig
->utime
,
1113 cputime_add(psig
->cstime
,
1114 cputime_add(p
->stime
,
1115 cputime_add(sig
->stime
,
1118 p
->min_flt
+ sig
->min_flt
+ sig
->cmin_flt
;
1120 p
->maj_flt
+ sig
->maj_flt
+ sig
->cmaj_flt
;
1122 p
->nvcsw
+ sig
->nvcsw
+ sig
->cnvcsw
;
1124 p
->nivcsw
+ sig
->nivcsw
+ sig
->cnivcsw
;
1125 spin_unlock_irq(&p
->parent
->sighand
->siglock
);
1129 * Now we are sure this task is interesting, and no other
1130 * thread can reap it because we set its state to EXIT_DEAD.
1132 read_unlock(&tasklist_lock
);
1134 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
1135 status
= (p
->signal
->flags
& SIGNAL_GROUP_EXIT
)
1136 ? p
->signal
->group_exit_code
: p
->exit_code
;
1137 if (!retval
&& stat_addr
)
1138 retval
= put_user(status
, stat_addr
);
1139 if (!retval
&& infop
)
1140 retval
= put_user(SIGCHLD
, &infop
->si_signo
);
1141 if (!retval
&& infop
)
1142 retval
= put_user(0, &infop
->si_errno
);
1143 if (!retval
&& infop
) {
1146 if ((status
& 0x7f) == 0) {
1150 why
= (status
& 0x80) ? CLD_DUMPED
: CLD_KILLED
;
1153 retval
= put_user((short)why
, &infop
->si_code
);
1155 retval
= put_user(status
, &infop
->si_status
);
1157 if (!retval
&& infop
)
1158 retval
= put_user(p
->pid
, &infop
->si_pid
);
1159 if (!retval
&& infop
)
1160 retval
= put_user(p
->uid
, &infop
->si_uid
);
1162 // TODO: is this safe?
1163 p
->exit_state
= EXIT_ZOMBIE
;
1167 if (p
->real_parent
!= p
->parent
) {
1168 write_lock_irq(&tasklist_lock
);
1169 /* Double-check with lock held. */
1170 if (p
->real_parent
!= p
->parent
) {
1172 // TODO: is this safe?
1173 p
->exit_state
= EXIT_ZOMBIE
;
1175 * If this is not a detached task, notify the parent.
1176 * If it's still not detached after that, don't release
1179 if (p
->exit_signal
!= -1) {
1180 do_notify_parent(p
, p
->exit_signal
);
1181 if (p
->exit_signal
!= -1)
1185 write_unlock_irq(&tasklist_lock
);
1194 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1195 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1196 * the lock and this task is uninteresting. If we return nonzero, we have
1197 * released the lock and the system call should return.
1199 static int wait_task_stopped(task_t
*p
, int delayed_group_leader
, int noreap
,
1200 struct siginfo __user
*infop
,
1201 int __user
*stat_addr
, struct rusage __user
*ru
)
1203 int retval
, exit_code
;
1207 if (delayed_group_leader
&& !(p
->ptrace
& PT_PTRACED
) &&
1208 p
->signal
&& p
->signal
->group_stop_count
> 0)
1210 * A group stop is in progress and this is the group leader.
1211 * We won't report until all threads have stopped.
1216 * Now we are pretty sure this task is interesting.
1217 * Make sure it doesn't get reaped out from under us while we
1218 * give up the lock and then examine it below. We don't want to
1219 * keep holding onto the tasklist_lock while we call getrusage and
1220 * possibly take page faults for user memory.
1223 read_unlock(&tasklist_lock
);
1225 if (unlikely(noreap
)) {
1228 int why
= (p
->ptrace
& PT_PTRACED
) ? CLD_TRAPPED
: CLD_STOPPED
;
1230 exit_code
= p
->exit_code
;
1231 if (unlikely(!exit_code
) ||
1232 unlikely(p
->state
& TASK_TRACED
))
1234 return wait_noreap_copyout(p
, pid
, uid
,
1235 why
, (exit_code
<< 8) | 0x7f,
1239 write_lock_irq(&tasklist_lock
);
1242 * This uses xchg to be atomic with the thread resuming and setting
1243 * it. It must also be done with the write lock held to prevent a
1244 * race with the EXIT_ZOMBIE case.
1246 exit_code
= xchg(&p
->exit_code
, 0);
1247 if (unlikely(p
->exit_state
)) {
1249 * The task resumed and then died. Let the next iteration
1250 * catch it in EXIT_ZOMBIE. Note that exit_code might
1251 * already be zero here if it resumed and did _exit(0).
1252 * The task itself is dead and won't touch exit_code again;
1253 * other processors in this function are locked out.
1255 p
->exit_code
= exit_code
;
1258 if (unlikely(exit_code
== 0)) {
1260 * Another thread in this function got to it first, or it
1261 * resumed, or it resumed and then died.
1263 write_unlock_irq(&tasklist_lock
);
1267 * We are returning to the wait loop without having successfully
1268 * removed the process and having released the lock. We cannot
1269 * continue, since the "p" task pointer is potentially stale.
1271 * Return -EAGAIN, and do_wait() will restart the loop from the
1272 * beginning. Do _not_ re-acquire the lock.
1277 /* move to end of parent's list to avoid starvation */
1279 add_parent(p
, p
->parent
);
1281 write_unlock_irq(&tasklist_lock
);
1283 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
1284 if (!retval
&& stat_addr
)
1285 retval
= put_user((exit_code
<< 8) | 0x7f, stat_addr
);
1286 if (!retval
&& infop
)
1287 retval
= put_user(SIGCHLD
, &infop
->si_signo
);
1288 if (!retval
&& infop
)
1289 retval
= put_user(0, &infop
->si_errno
);
1290 if (!retval
&& infop
)
1291 retval
= put_user((short)((p
->ptrace
& PT_PTRACED
)
1292 ? CLD_TRAPPED
: CLD_STOPPED
),
1294 if (!retval
&& infop
)
1295 retval
= put_user(exit_code
, &infop
->si_status
);
1296 if (!retval
&& infop
)
1297 retval
= put_user(p
->pid
, &infop
->si_pid
);
1298 if (!retval
&& infop
)
1299 retval
= put_user(p
->uid
, &infop
->si_uid
);
1309 * Handle do_wait work for one task in a live, non-stopped state.
1310 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1311 * the lock and this task is uninteresting. If we return nonzero, we have
1312 * released the lock and the system call should return.
1314 static int wait_task_continued(task_t
*p
, int noreap
,
1315 struct siginfo __user
*infop
,
1316 int __user
*stat_addr
, struct rusage __user
*ru
)
1322 if (unlikely(!p
->signal
))
1325 if (!(p
->signal
->flags
& SIGNAL_STOP_CONTINUED
))
1328 spin_lock_irq(&p
->sighand
->siglock
);
1329 /* Re-check with the lock held. */
1330 if (!(p
->signal
->flags
& SIGNAL_STOP_CONTINUED
)) {
1331 spin_unlock_irq(&p
->sighand
->siglock
);
1335 p
->signal
->flags
&= ~SIGNAL_STOP_CONTINUED
;
1336 spin_unlock_irq(&p
->sighand
->siglock
);
1341 read_unlock(&tasklist_lock
);
1344 retval
= ru
? getrusage(p
, RUSAGE_BOTH
, ru
) : 0;
1346 if (!retval
&& stat_addr
)
1347 retval
= put_user(0xffff, stat_addr
);
1351 retval
= wait_noreap_copyout(p
, pid
, uid
,
1352 CLD_CONTINUED
, SIGCONT
,
1354 BUG_ON(retval
== 0);
1361 static inline int my_ptrace_child(struct task_struct
*p
)
1363 if (!(p
->ptrace
& PT_PTRACED
))
1365 if (!(p
->ptrace
& PT_ATTACHED
))
1368 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1369 * we are the attacher. If we are the real parent, this is a race
1370 * inside ptrace_attach. It is waiting for the tasklist_lock,
1371 * which we have to switch the parent links, but has already set
1372 * the flags in p->ptrace.
1374 return (p
->parent
!= p
->real_parent
);
1377 static long do_wait(pid_t pid
, int options
, struct siginfo __user
*infop
,
1378 int __user
*stat_addr
, struct rusage __user
*ru
)
1380 DECLARE_WAITQUEUE(wait
, current
);
1381 struct task_struct
*tsk
;
1384 add_wait_queue(¤t
->signal
->wait_chldexit
,&wait
);
1387 * We will set this flag if we see any child that might later
1388 * match our criteria, even if we are not able to reap it yet.
1391 current
->state
= TASK_INTERRUPTIBLE
;
1392 read_lock(&tasklist_lock
);
1395 struct task_struct
*p
;
1396 struct list_head
*_p
;
1399 list_for_each(_p
,&tsk
->children
) {
1400 p
= list_entry(_p
,struct task_struct
,sibling
);
1402 ret
= eligible_child(pid
, options
, p
);
1409 * When we hit the race with PTRACE_ATTACH,
1410 * we will not report this child. But the
1411 * race means it has not yet been moved to
1412 * our ptrace_children list, so we need to
1413 * set the flag here to avoid a spurious ECHILD
1414 * when the race happens with the only child.
1417 if (!my_ptrace_child(p
))
1422 * It's stopped now, so it might later
1423 * continue, exit, or stop again.
1426 if (!(options
& WUNTRACED
) &&
1427 !my_ptrace_child(p
))
1429 retval
= wait_task_stopped(p
, ret
== 2,
1430 (options
& WNOWAIT
),
1433 if (retval
== -EAGAIN
)
1435 if (retval
!= 0) /* He released the lock. */
1440 if (p
->exit_state
== EXIT_DEAD
)
1442 // case EXIT_ZOMBIE:
1443 if (p
->exit_state
== EXIT_ZOMBIE
) {
1445 * Eligible but we cannot release
1449 goto check_continued
;
1450 if (!likely(options
& WEXITED
))
1452 retval
= wait_task_zombie(
1453 p
, (options
& WNOWAIT
),
1454 infop
, stat_addr
, ru
);
1455 /* He released the lock. */
1462 * It's running now, so it might later
1463 * exit, stop, or stop and then continue.
1466 if (!unlikely(options
& WCONTINUED
))
1468 retval
= wait_task_continued(
1469 p
, (options
& WNOWAIT
),
1470 infop
, stat_addr
, ru
);
1471 if (retval
!= 0) /* He released the lock. */
1477 list_for_each(_p
, &tsk
->ptrace_children
) {
1478 p
= list_entry(_p
, struct task_struct
,
1480 if (!eligible_child(pid
, options
, p
))
1486 if (options
& __WNOTHREAD
)
1488 tsk
= next_thread(tsk
);
1489 if (tsk
->signal
!= current
->signal
)
1491 } while (tsk
!= current
);
1493 read_unlock(&tasklist_lock
);
1496 if (options
& WNOHANG
)
1498 retval
= -ERESTARTSYS
;
1499 if (signal_pending(current
))
1506 current
->state
= TASK_RUNNING
;
1507 remove_wait_queue(¤t
->signal
->wait_chldexit
,&wait
);
1513 * For a WNOHANG return, clear out all the fields
1514 * we would set so the user can easily tell the
1518 retval
= put_user(0, &infop
->si_signo
);
1520 retval
= put_user(0, &infop
->si_errno
);
1522 retval
= put_user(0, &infop
->si_code
);
1524 retval
= put_user(0, &infop
->si_pid
);
1526 retval
= put_user(0, &infop
->si_uid
);
1528 retval
= put_user(0, &infop
->si_status
);
1534 asmlinkage
long sys_waitid(int which
, pid_t pid
,
1535 struct siginfo __user
*infop
, int options
,
1536 struct rusage __user
*ru
)
1540 if (options
& ~(WNOHANG
|WNOWAIT
|WEXITED
|WSTOPPED
|WCONTINUED
))
1542 if (!(options
& (WEXITED
|WSTOPPED
|WCONTINUED
)))
1562 ret
= do_wait(pid
, options
, infop
, NULL
, ru
);
1564 /* avoid REGPARM breakage on x86: */
1565 prevent_tail_call(ret
);
1569 asmlinkage
long sys_wait4(pid_t pid
, int __user
*stat_addr
,
1570 int options
, struct rusage __user
*ru
)
1574 if (options
& ~(WNOHANG
|WUNTRACED
|WCONTINUED
|
1575 __WNOTHREAD
|__WCLONE
|__WALL
))
1577 ret
= do_wait(pid
, options
| WEXITED
, NULL
, stat_addr
, ru
);
1579 /* avoid REGPARM breakage on x86: */
1580 prevent_tail_call(ret
);
1584 #ifdef __ARCH_WANT_SYS_WAITPID
1587 * sys_waitpid() remains for compatibility. waitpid() should be
1588 * implemented by calling sys_wait4() from libc.a.
1590 asmlinkage
long sys_waitpid(pid_t pid
, int __user
*stat_addr
, int options
)
1592 return sys_wait4(pid
, stat_addr
, options
, NULL
);