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/vs_limit.h>
32 #include <linux/vs_network.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 /* tasklist_lock is held, is this sufficient? */
51 atomic_dec(&p->vx_info->cvirt.nr_threads);
54 detach_pid(p, PIDTYPE_PID);
55 detach_pid(p, PIDTYPE_TGID);
56 if (thread_group_leader(p)) {
57 detach_pid(p, PIDTYPE_PGID);
58 detach_pid(p, PIDTYPE_SID);
60 __get_cpu_var(process_counts)--;
66 void release_task(struct task_struct * p)
70 struct dentry *proc_dentry;
73 atomic_dec(&p->user->processes);
74 spin_lock(&p->proc_lock);
75 proc_dentry = proc_pid_unhash(p);
76 write_lock_irq(&tasklist_lock);
77 if (unlikely(p->ptrace))
79 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
85 * If we are the last non-leader member of the thread
86 * group, and the leader is zombie, then notify the
87 * group leader's parent process. (if it wants notification.)
90 leader = p->group_leader;
91 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
92 BUG_ON(leader->exit_signal == -1);
93 do_notify_parent(leader, leader->exit_signal);
95 * If we were the last child thread and the leader has
96 * exited already, and the leader's parent ignores SIGCHLD,
97 * then we are the one who should release the leader.
99 * do_notify_parent() will have marked it self-reaping in
102 zap_leader = (leader->exit_signal == -1);
106 write_unlock_irq(&tasklist_lock);
107 spin_unlock(&p->proc_lock);
108 proc_pid_flush(proc_dentry);
111 release_vx_info(p->vx_info, p);
113 release_nx_info(p->nx_info, p);
117 if (unlikely(zap_leader))
121 /* we are using it only for SMP init */
123 void unhash_process(struct task_struct *p)
125 struct dentry *proc_dentry;
127 spin_lock(&p->proc_lock);
128 proc_dentry = proc_pid_unhash(p);
129 write_lock_irq(&tasklist_lock);
131 write_unlock_irq(&tasklist_lock);
132 spin_unlock(&p->proc_lock);
133 proc_pid_flush(proc_dentry);
137 * This checks not only the pgrp, but falls back on the pid if no
138 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
141 int session_of_pgrp(int pgrp)
143 struct task_struct *p;
146 read_lock(&tasklist_lock);
147 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
148 if (p->signal->session > 0) {
149 sid = p->signal->session;
152 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
153 p = find_task_by_pid(pgrp);
155 sid = p->signal->session;
157 read_unlock(&tasklist_lock);
163 * Determine if a process group is "orphaned", according to the POSIX
164 * definition in 2.2.2.52. Orphaned process groups are not to be affected
165 * by terminal-generated stop signals. Newly orphaned process groups are
166 * to receive a SIGHUP and a SIGCONT.
168 * "I ask you, have you ever known what it is to be an orphan?"
170 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
172 struct task_struct *p;
175 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
176 if (p == ignored_task
178 || p->real_parent->pid == 1)
180 if (process_group(p->real_parent) != pgrp
181 && p->real_parent->signal->session == p->signal->session) {
185 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
186 return ret; /* (sighing) "Often!" */
189 int is_orphaned_pgrp(int pgrp)
193 read_lock(&tasklist_lock);
194 retval = will_become_orphaned_pgrp(pgrp, NULL);
195 read_unlock(&tasklist_lock);
200 static inline int has_stopped_jobs(int pgrp)
203 struct task_struct *p;
205 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
206 if (p->state != TASK_STOPPED)
209 /* If p is stopped by a debugger on a signal that won't
210 stop it, then don't count p as stopped. This isn't
211 perfect but it's a good approximation. */
212 if (unlikely (p->ptrace)
213 && p->exit_code != SIGSTOP
214 && p->exit_code != SIGTSTP
215 && p->exit_code != SIGTTOU
216 && p->exit_code != SIGTTIN)
221 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
226 * reparent_to_init - Reparent the calling kernel thread to the init task.
228 * If a kernel thread is launched as a result of a system call, or if
229 * it ever exits, it should generally reparent itself to init so that
230 * it is correctly cleaned up on exit.
232 * The various task state such as scheduling policy and priority may have
233 * been inherited from a user process, so we reset them to sane values here.
235 * NOTE that reparent_to_init() gives the caller full capabilities.
237 static inline void reparent_to_init(void)
239 write_lock_irq(&tasklist_lock);
241 ptrace_unlink(current);
242 /* Reparent to init */
243 REMOVE_LINKS(current);
244 /* FIXME handle vchild_reaper/initpid */
245 current->parent = child_reaper;
246 current->real_parent = child_reaper;
249 /* Set the exit signal to SIGCHLD so we signal init on exit */
250 current->exit_signal = SIGCHLD;
252 if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
253 set_user_nice(current, 0);
257 security_task_reparent_to_init(current);
258 memcpy(current->signal->rlim, init_task.signal->rlim,
259 sizeof(current->signal->rlim));
260 atomic_inc(&(INIT_USER->__count));
261 write_unlock_irq(&tasklist_lock);
262 switch_uid(INIT_USER);
265 void __set_special_pids(pid_t session, pid_t pgrp)
267 struct task_struct *curr = current;
269 if (curr->signal->session != session) {
270 detach_pid(curr, PIDTYPE_SID);
271 curr->signal->session = session;
272 attach_pid(curr, PIDTYPE_SID, session);
274 if (process_group(curr) != pgrp) {
275 detach_pid(curr, PIDTYPE_PGID);
276 curr->signal->pgrp = pgrp;
277 attach_pid(curr, PIDTYPE_PGID, pgrp);
281 void set_special_pids(pid_t session, pid_t pgrp)
283 write_lock_irq(&tasklist_lock);
284 __set_special_pids(session, pgrp);
285 write_unlock_irq(&tasklist_lock);
289 * Let kernel threads use this to say that they
290 * allow a certain signal (since daemonize() will
291 * have disabled all of them by default).
293 int allow_signal(int sig)
295 if (!valid_signal(sig) || sig < 1)
298 spin_lock_irq(¤t->sighand->siglock);
299 sigdelset(¤t->blocked, sig);
301 /* Kernel threads handle their own signals.
302 Let the signal code know it'll be handled, so
303 that they don't get converted to SIGKILL or
304 just silently dropped */
305 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
308 spin_unlock_irq(¤t->sighand->siglock);
312 EXPORT_SYMBOL(allow_signal);
314 int disallow_signal(int sig)
316 if (!valid_signal(sig) || sig < 1)
319 spin_lock_irq(¤t->sighand->siglock);
320 sigaddset(¤t->blocked, sig);
322 spin_unlock_irq(¤t->sighand->siglock);
326 EXPORT_SYMBOL(disallow_signal);
329 * Put all the gunge required to become a kernel thread without
330 * attached user resources in one place where it belongs.
333 void daemonize(const char *name, ...)
336 struct fs_struct *fs;
339 va_start(args, name);
340 vsnprintf(current->comm, sizeof(current->comm), name, args);
344 * If we were started as result of loading a module, close all of the
345 * user space pages. We don't need them, and if we didn't close them
346 * they would be locked into memory.
350 set_special_pids(1, 1);
352 current->signal->tty = NULL;
355 /* Block and flush all signals */
356 sigfillset(&blocked);
357 sigprocmask(SIG_BLOCK, &blocked, NULL);
358 flush_signals(current);
360 /* Become as one with the init task */
362 exit_fs(current); /* current->fs->count--; */
365 atomic_inc(&fs->count);
367 current->files = init_task.files;
368 atomic_inc(¤t->files->count);
373 EXPORT_SYMBOL(daemonize);
375 static inline void close_files(struct files_struct * files)
383 if (i >= files->max_fdset || i >= files->max_fds)
385 set = files->open_fds->fds_bits[j++];
388 struct file * file = xchg(&files->fd[i], NULL);
390 filp_close(file, files);
399 struct files_struct *get_files_struct(struct task_struct *task)
401 struct files_struct *files;
406 atomic_inc(&files->count);
412 void fastcall put_files_struct(struct files_struct *files)
414 if (atomic_dec_and_test(&files->count)) {
417 * Free the fd and fdset arrays if we expanded them.
419 if (files->fd != &files->fd_array[0])
420 free_fd_array(files->fd, files->max_fds);
421 if (files->max_fdset > __FD_SETSIZE) {
422 free_fdset(files->open_fds, files->max_fdset);
423 free_fdset(files->close_on_exec, files->max_fdset);
425 kmem_cache_free(files_cachep, files);
429 EXPORT_SYMBOL(put_files_struct);
431 static inline void __exit_files(struct task_struct *tsk)
433 struct files_struct * files = tsk->files;
439 put_files_struct(files);
443 void exit_files(struct task_struct *tsk)
448 static inline void __put_fs_struct(struct fs_struct *fs)
450 /* No need to hold fs->lock if we are killing it */
451 if (atomic_dec_and_test(&fs->count)) {
458 mntput(fs->altrootmnt);
460 kmem_cache_free(fs_cachep, fs);
464 void put_fs_struct(struct fs_struct *fs)
469 static inline void __exit_fs(struct task_struct *tsk)
471 struct fs_struct * fs = tsk->fs;
481 void exit_fs(struct task_struct *tsk)
486 EXPORT_SYMBOL_GPL(exit_fs);
489 * Turn us into a lazy TLB process if we
492 static void exit_mm(struct task_struct * tsk)
494 struct mm_struct *mm = tsk->mm;
500 * Serialize with any possible pending coredump.
501 * We must hold mmap_sem around checking core_waiters
502 * and clearing tsk->mm. The core-inducing thread
503 * will increment core_waiters for each thread in the
504 * group with ->mm != NULL.
506 down_read(&mm->mmap_sem);
507 if (mm->core_waiters) {
508 up_read(&mm->mmap_sem);
509 down_write(&mm->mmap_sem);
510 if (!--mm->core_waiters)
511 complete(mm->core_startup_done);
512 up_write(&mm->mmap_sem);
514 wait_for_completion(&mm->core_done);
515 down_read(&mm->mmap_sem);
517 atomic_inc(&mm->mm_count);
518 if (mm != tsk->active_mm) BUG();
519 /* more a memory barrier than a real lock */
522 up_read(&mm->mmap_sem);
523 enter_lazy_tlb(mm, current);
528 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
531 * Make sure we're not reparenting to ourselves and that
532 * the parent is not a zombie.
534 BUG_ON(p == reaper || reaper->exit_state >= EXIT_ZOMBIE);
535 p->real_parent = reaper;
538 static inline void reparent_thread(task_t *p, task_t *father, int traced)
540 /* We don't want people slaying init. */
541 if (p->exit_signal != -1)
542 p->exit_signal = SIGCHLD;
544 if (p->pdeath_signal)
545 /* We already hold the tasklist_lock here. */
546 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
548 /* Move the child from its dying parent to the new one. */
549 if (unlikely(traced)) {
550 /* Preserve ptrace links if someone else is tracing this child. */
551 list_del_init(&p->ptrace_list);
552 if (p->parent != p->real_parent)
553 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
555 /* If this child is being traced, then we're the one tracing it
556 * anyway, so let go of it.
559 list_del_init(&p->sibling);
560 p->parent = p->real_parent;
561 list_add_tail(&p->sibling, &p->parent->children);
563 /* If we'd notified the old parent about this child's death,
564 * also notify the new parent.
566 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
567 thread_group_empty(p))
568 do_notify_parent(p, p->exit_signal);
569 else if (p->state == TASK_TRACED) {
571 * If it was at a trace stop, turn it into
572 * a normal stop since it's no longer being
580 * process group orphan check
581 * Case ii: Our child is in a different pgrp
582 * than we are, and it was the only connection
583 * outside, so the child pgrp is now orphaned.
585 if ((process_group(p) != process_group(father)) &&
586 (p->signal->session == father->signal->session)) {
587 int pgrp = process_group(p);
589 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
590 __kill_pg_info(SIGHUP, (void *)1, pgrp);
591 __kill_pg_info(SIGCONT, (void *)1, pgrp);
597 * When we die, we re-parent all our children.
598 * Try to give them to another thread in our thread
599 * group, and if no such member exists, give it to
600 * the global child reaper process (ie "init")
602 static inline void forget_original_parent(struct task_struct * father,
603 struct list_head *to_release)
605 struct task_struct *p, *reaper = father;
606 struct list_head *_p, *_n;
608 /* FIXME handle vchild_reaper/initpid */
610 reaper = next_thread(reaper);
611 if (reaper == father) {
612 reaper = child_reaper;
615 } while (reaper->exit_state);
618 * There are only two places where our children can be:
620 * - in our child list
621 * - in our ptraced child list
623 * Search them and reparent children.
625 list_for_each_safe(_p, _n, &father->children) {
627 p = list_entry(_p,struct task_struct,sibling);
631 /* if father isn't the real parent, then ptrace must be enabled */
632 BUG_ON(father != p->real_parent && !ptrace);
634 if (father == p->real_parent) {
635 /* reparent with a reaper, real father it's us */
636 choose_new_parent(p, reaper, child_reaper);
637 reparent_thread(p, father, 0);
639 /* reparent ptraced task to its real parent */
641 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
642 thread_group_empty(p))
643 do_notify_parent(p, p->exit_signal);
647 * if the ptraced child is a zombie with exit_signal == -1
648 * we must collect it before we exit, or it will remain
649 * zombie forever since we prevented it from self-reap itself
650 * while it was being traced by us, to be able to see it in wait4.
652 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
653 list_add(&p->ptrace_list, to_release);
655 list_for_each_safe(_p, _n, &father->ptrace_children) {
656 p = list_entry(_p,struct task_struct,ptrace_list);
657 choose_new_parent(p, reaper, child_reaper);
658 reparent_thread(p, father, 1);
663 * Send signals to all our closest relatives so that they know
664 * to properly mourn us..
666 static void exit_notify(struct task_struct *tsk)
669 struct task_struct *t;
670 struct list_head ptrace_dead, *_p, *_n;
672 if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
673 && !thread_group_empty(tsk)) {
675 * This occurs when there was a race between our exit
676 * syscall and a group signal choosing us as the one to
677 * wake up. It could be that we are the only thread
678 * alerted to check for pending signals, but another thread
679 * should be woken now to take the signal since we will not.
680 * Now we'll wake all the threads in the group just to make
681 * sure someone gets all the pending signals.
683 read_lock(&tasklist_lock);
684 spin_lock_irq(&tsk->sighand->siglock);
685 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
686 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
687 recalc_sigpending_tsk(t);
688 if (signal_pending(t))
689 signal_wake_up(t, 0);
691 spin_unlock_irq(&tsk->sighand->siglock);
692 read_unlock(&tasklist_lock);
695 write_lock_irq(&tasklist_lock);
698 * This does two things:
700 * A. Make init inherit all the child processes
701 * B. Check to see if any process groups have become orphaned
702 * as a result of our exiting, and if they have any stopped
703 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
706 INIT_LIST_HEAD(&ptrace_dead);
707 forget_original_parent(tsk, &ptrace_dead);
708 BUG_ON(!list_empty(&tsk->children));
709 BUG_ON(!list_empty(&tsk->ptrace_children));
712 * 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)
716 * Case i: Our father is in a different pgrp than we are
717 * and we were the only connection outside, so our pgrp
718 * is about to become orphaned.
721 t = tsk->real_parent;
723 if ((process_group(t) != process_group(tsk)) &&
724 (t->signal->session == tsk->signal->session) &&
725 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
726 has_stopped_jobs(process_group(tsk))) {
727 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
728 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
731 /* Let father know we died
733 * Thread signals are configurable, but you aren't going to use
734 * that to send signals to arbitary processes.
735 * That stops right now.
737 * If the parent exec id doesn't match the exec id we saved
738 * when we started then we know the parent has changed security
741 * If our self_exec id doesn't match our parent_exec_id then
742 * we have changed execution domain as these two values started
743 * the same after a fork.
747 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
748 ( tsk->parent_exec_id != t->self_exec_id ||
749 tsk->self_exec_id != tsk->parent_exec_id)
750 && !capable(CAP_KILL))
751 tsk->exit_signal = SIGCHLD;
754 /* If something other than our normal parent is ptracing us, then
755 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
756 * only has special meaning to our real parent.
758 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
759 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
760 do_notify_parent(tsk, signal);
761 } else if (tsk->ptrace) {
762 do_notify_parent(tsk, SIGCHLD);
766 if (tsk->exit_signal == -1 &&
767 (likely(tsk->ptrace == 0) ||
768 unlikely(tsk->parent->signal->flags & SIGNAL_GROUP_EXIT)))
770 tsk->exit_state = state;
772 write_unlock_irq(&tasklist_lock);
774 list_for_each_safe(_p, _n, &ptrace_dead) {
776 t = list_entry(_p,struct task_struct,ptrace_list);
780 /* If the process is dead, release it - nobody will wait for it */
781 if (state == EXIT_DEAD)
784 /* PF_DEAD causes final put_task_struct after we schedule. */
786 tsk->flags |= PF_DEAD;
789 fastcall NORET_TYPE void do_exit(long code)
791 struct task_struct *tsk = current;
794 profile_task_exit(tsk);
796 if (unlikely(in_interrupt()))
797 panic("Aiee, killing interrupt handler!");
798 if (unlikely(!tsk->pid))
799 panic("Attempted to kill the idle task!");
800 if (unlikely(tsk->pid == 1))
801 panic("Attempted to kill init!");
805 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
806 current->ptrace_message = code;
807 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
810 tsk->flags |= PF_EXITING;
813 * Make sure we don't try to process any timer firings
814 * while we are already exiting.
816 tsk->it_virt_expires = cputime_zero;
817 tsk->it_prof_expires = cputime_zero;
818 tsk->it_sched_expires = 0;
820 if (unlikely(in_atomic()))
821 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
822 current->comm, current->pid,
825 acct_update_integrals(tsk);
826 update_mem_hiwater(tsk);
827 group_dead = atomic_dec_and_test(&tsk->signal->live);
840 if (group_dead && tsk->signal->leader)
841 disassociate_ctty(1);
843 module_put(tsk->thread_info->exec_domain->module);
845 module_put(tsk->binfmt->module);
847 tsk->exit_code = code;
850 mpol_free(tsk->mempolicy);
851 tsk->mempolicy = NULL;
854 BUG_ON(!(current->flags & PF_DEAD));
857 /* Avoid "noreturn function does return". */
861 EXPORT_SYMBOL_GPL(do_exit);
863 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
871 EXPORT_SYMBOL(complete_and_exit);
873 asmlinkage long sys_exit(int error_code)
875 do_exit((error_code&0xff)<<8);
878 task_t fastcall *next_thread(const task_t *p)
880 return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
883 EXPORT_SYMBOL(next_thread);
886 * Take down every thread in the group. This is called by fatal signals
887 * as well as by sys_exit_group (below).
890 do_group_exit(int exit_code)
892 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
894 if (current->signal->flags & SIGNAL_GROUP_EXIT)
895 exit_code = current->signal->group_exit_code;
896 else if (!thread_group_empty(current)) {
897 struct signal_struct *const sig = current->signal;
898 struct sighand_struct *const sighand = current->sighand;
899 read_lock(&tasklist_lock);
900 spin_lock_irq(&sighand->siglock);
901 if (sig->flags & SIGNAL_GROUP_EXIT)
902 /* Another thread got here before we took the lock. */
903 exit_code = sig->group_exit_code;
905 sig->flags = SIGNAL_GROUP_EXIT;
906 sig->group_exit_code = exit_code;
907 zap_other_threads(current);
909 spin_unlock_irq(&sighand->siglock);
910 read_unlock(&tasklist_lock);
918 * this kills every thread in the thread group. Note that any externally
919 * wait4()-ing process will get the correct exit code - even if this
920 * thread is not the thread group leader.
922 asmlinkage void sys_exit_group(int error_code)
924 do_group_exit((error_code & 0xff) << 8);
927 static int eligible_child(pid_t pid, int options, task_t *p)
933 if (process_group(p) != process_group(current))
935 } else if (pid != -1) {
936 if (process_group(p) != -pid)
941 * Do not consider detached threads that are
944 if (p->exit_signal == -1 && !p->ptrace)
947 /* Wait for all children (clone and not) if __WALL is set;
948 * otherwise, wait for clone children *only* if __WCLONE is
949 * set; otherwise, wait for non-clone children *only*. (Note:
950 * A "clone" child here is one that reports to its parent
951 * using a signal other than SIGCHLD.) */
952 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
953 && !(options & __WALL))
956 * Do not consider thread group leaders that are
957 * in a non-empty thread group:
959 if (current->tgid != p->tgid && delay_group_leader(p))
962 if (security_task_wait(p))
968 static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
970 struct siginfo __user *infop,
971 struct rusage __user *rusagep)
973 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
976 retval = put_user(SIGCHLD, &infop->si_signo);
978 retval = put_user(0, &infop->si_errno);
980 retval = put_user((short)why, &infop->si_code);
982 retval = put_user(pid, &infop->si_pid);
984 retval = put_user(uid, &infop->si_uid);
986 retval = put_user(status, &infop->si_status);
993 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
994 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
995 * the lock and this task is uninteresting. If we return nonzero, we have
996 * released the lock and the system call should return.
998 static int wait_task_zombie(task_t *p, int noreap,
999 struct siginfo __user *infop,
1000 int __user *stat_addr, struct rusage __user *ru)
1002 unsigned long state;
1006 if (unlikely(noreap)) {
1009 int exit_code = p->exit_code;
1012 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1014 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1017 read_unlock(&tasklist_lock);
1018 if ((exit_code & 0x7f) == 0) {
1020 status = exit_code >> 8;
1022 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1023 status = exit_code & 0x7f;
1025 return wait_noreap_copyout(p, pid, uid, why,
1030 * Try to move the task's state to DEAD
1031 * only one thread is allowed to do this:
1033 state = xchg(&p->exit_state, EXIT_DEAD);
1034 if (state != EXIT_ZOMBIE) {
1035 BUG_ON(state != EXIT_DEAD);
1038 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1040 * This can only happen in a race with a ptraced thread
1041 * dying on another processor.
1046 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
1048 * The resource counters for the group leader are in its
1049 * own task_struct. Those for dead threads in the group
1050 * are in its signal_struct, as are those for the child
1051 * processes it has previously reaped. All these
1052 * accumulate in the parent's signal_struct c* fields.
1054 * We don't bother to take a lock here to protect these
1055 * p->signal fields, because they are only touched by
1056 * __exit_signal, which runs with tasklist_lock
1057 * write-locked anyway, and so is excluded here. We do
1058 * need to protect the access to p->parent->signal fields,
1059 * as other threads in the parent group can be right
1060 * here reaping other children at the same time.
1062 spin_lock_irq(&p->parent->sighand->siglock);
1063 p->parent->signal->cutime =
1064 cputime_add(p->parent->signal->cutime,
1065 cputime_add(p->utime,
1066 cputime_add(p->signal->utime,
1067 p->signal->cutime)));
1068 p->parent->signal->cstime =
1069 cputime_add(p->parent->signal->cstime,
1070 cputime_add(p->stime,
1071 cputime_add(p->signal->stime,
1072 p->signal->cstime)));
1073 p->parent->signal->cmin_flt +=
1074 p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
1075 p->parent->signal->cmaj_flt +=
1076 p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
1077 p->parent->signal->cnvcsw +=
1078 p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
1079 p->parent->signal->cnivcsw +=
1080 p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
1081 spin_unlock_irq(&p->parent->sighand->siglock);
1085 * Now we are sure this task is interesting, and no other
1086 * thread can reap it because we set its state to EXIT_DEAD.
1088 read_unlock(&tasklist_lock);
1090 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1091 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1092 ? p->signal->group_exit_code : p->exit_code;
1093 if (!retval && stat_addr)
1094 retval = put_user(status, stat_addr);
1095 if (!retval && infop)
1096 retval = put_user(SIGCHLD, &infop->si_signo);
1097 if (!retval && infop)
1098 retval = put_user(0, &infop->si_errno);
1099 if (!retval && infop) {
1102 if ((status & 0x7f) == 0) {
1106 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1109 retval = put_user((short)why, &infop->si_code);
1111 retval = put_user(status, &infop->si_status);
1113 if (!retval && infop)
1114 retval = put_user(p->pid, &infop->si_pid);
1115 if (!retval && infop)
1116 retval = put_user(p->uid, &infop->si_uid);
1118 // TODO: is this safe?
1119 p->exit_state = EXIT_ZOMBIE;
1123 if (p->real_parent != p->parent) {
1124 write_lock_irq(&tasklist_lock);
1125 /* Double-check with lock held. */
1126 if (p->real_parent != p->parent) {
1128 // TODO: is this safe?
1129 p->exit_state = EXIT_ZOMBIE;
1131 * If this is not a detached task, notify the parent.
1132 * If it's still not detached after that, don't release
1135 if (p->exit_signal != -1) {
1136 do_notify_parent(p, p->exit_signal);
1137 if (p->exit_signal != -1)
1141 write_unlock_irq(&tasklist_lock);
1150 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1151 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1152 * the lock and this task is uninteresting. If we return nonzero, we have
1153 * released the lock and the system call should return.
1155 static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
1156 struct siginfo __user *infop,
1157 int __user *stat_addr, struct rusage __user *ru)
1159 int retval, exit_code;
1163 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1164 p->signal && p->signal->group_stop_count > 0)
1166 * A group stop is in progress and this is the group leader.
1167 * We won't report until all threads have stopped.
1172 * Now we are pretty sure this task is interesting.
1173 * Make sure it doesn't get reaped out from under us while we
1174 * give up the lock and then examine it below. We don't want to
1175 * keep holding onto the tasklist_lock while we call getrusage and
1176 * possibly take page faults for user memory.
1179 read_unlock(&tasklist_lock);
1181 if (unlikely(noreap)) {
1184 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1186 exit_code = p->exit_code;
1187 if (unlikely(!exit_code) ||
1188 unlikely(p->state > TASK_STOPPED))
1190 return wait_noreap_copyout(p, pid, uid,
1191 why, (exit_code << 8) | 0x7f,
1195 write_lock_irq(&tasklist_lock);
1198 * This uses xchg to be atomic with the thread resuming and setting
1199 * it. It must also be done with the write lock held to prevent a
1200 * race with the EXIT_ZOMBIE case.
1202 exit_code = xchg(&p->exit_code, 0);
1203 if (unlikely(p->exit_state)) {
1205 * The task resumed and then died. Let the next iteration
1206 * catch it in EXIT_ZOMBIE. Note that exit_code might
1207 * already be zero here if it resumed and did _exit(0).
1208 * The task itself is dead and won't touch exit_code again;
1209 * other processors in this function are locked out.
1211 p->exit_code = exit_code;
1214 if (unlikely(exit_code == 0)) {
1216 * Another thread in this function got to it first, or it
1217 * resumed, or it resumed and then died.
1219 write_unlock_irq(&tasklist_lock);
1223 * We are returning to the wait loop without having successfully
1224 * removed the process and having released the lock. We cannot
1225 * continue, since the "p" task pointer is potentially stale.
1227 * Return -EAGAIN, and do_wait() will restart the loop from the
1228 * beginning. Do _not_ re-acquire the lock.
1233 /* move to end of parent's list to avoid starvation */
1235 add_parent(p, p->parent);
1237 write_unlock_irq(&tasklist_lock);
1239 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1240 if (!retval && stat_addr)
1241 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1242 if (!retval && infop)
1243 retval = put_user(SIGCHLD, &infop->si_signo);
1244 if (!retval && infop)
1245 retval = put_user(0, &infop->si_errno);
1246 if (!retval && infop)
1247 retval = put_user((short)((p->ptrace & PT_PTRACED)
1248 ? CLD_TRAPPED : CLD_STOPPED),
1250 if (!retval && infop)
1251 retval = put_user(exit_code, &infop->si_status);
1252 if (!retval && infop)
1253 retval = put_user(p->pid, &infop->si_pid);
1254 if (!retval && infop)
1255 retval = put_user(p->uid, &infop->si_uid);
1265 * Handle do_wait work for one task in a live, non-stopped state.
1266 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1267 * the lock and this task is uninteresting. If we return nonzero, we have
1268 * released the lock and the system call should return.
1270 static int wait_task_continued(task_t *p, int noreap,
1271 struct siginfo __user *infop,
1272 int __user *stat_addr, struct rusage __user *ru)
1278 if (unlikely(!p->signal))
1281 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1284 spin_lock_irq(&p->sighand->siglock);
1285 /* Re-check with the lock held. */
1286 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1287 spin_unlock_irq(&p->sighand->siglock);
1291 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1292 spin_unlock_irq(&p->sighand->siglock);
1297 read_unlock(&tasklist_lock);
1300 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1302 if (!retval && stat_addr)
1303 retval = put_user(0xffff, stat_addr);
1307 retval = wait_noreap_copyout(p, pid, uid,
1308 CLD_CONTINUED, SIGCONT,
1310 BUG_ON(retval == 0);
1317 static inline int my_ptrace_child(struct task_struct *p)
1319 if (!(p->ptrace & PT_PTRACED))
1321 if (!(p->ptrace & PT_ATTACHED))
1324 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1325 * we are the attacher. If we are the real parent, this is a race
1326 * inside ptrace_attach. It is waiting for the tasklist_lock,
1327 * which we have to switch the parent links, but has already set
1328 * the flags in p->ptrace.
1330 return (p->parent != p->real_parent);
1333 static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1334 int __user *stat_addr, struct rusage __user *ru)
1336 DECLARE_WAITQUEUE(wait, current);
1337 struct task_struct *tsk;
1340 add_wait_queue(¤t->signal->wait_chldexit,&wait);
1343 * We will set this flag if we see any child that might later
1344 * match our criteria, even if we are not able to reap it yet.
1347 current->state = TASK_INTERRUPTIBLE;
1348 read_lock(&tasklist_lock);
1351 struct task_struct *p;
1352 struct list_head *_p;
1355 list_for_each(_p,&tsk->children) {
1356 p = list_entry(_p,struct task_struct,sibling);
1358 ret = eligible_child(pid, options, p);
1364 if (!my_ptrace_child(p))
1369 * It's stopped now, so it might later
1370 * continue, exit, or stop again.
1373 if (!(options & WUNTRACED) &&
1374 !my_ptrace_child(p))
1376 retval = wait_task_stopped(p, ret == 2,
1377 (options & WNOWAIT),
1380 if (retval == -EAGAIN)
1382 if (retval != 0) /* He released the lock. */
1387 if (p->exit_state == EXIT_DEAD)
1389 // case EXIT_ZOMBIE:
1390 if (p->exit_state == EXIT_ZOMBIE) {
1392 * Eligible but we cannot release
1396 goto check_continued;
1397 if (!likely(options & WEXITED))
1399 retval = wait_task_zombie(
1400 p, (options & WNOWAIT),
1401 infop, stat_addr, ru);
1402 /* He released the lock. */
1409 * It's running now, so it might later
1410 * exit, stop, or stop and then continue.
1413 if (!unlikely(options & WCONTINUED))
1415 retval = wait_task_continued(
1416 p, (options & WNOWAIT),
1417 infop, stat_addr, ru);
1418 if (retval != 0) /* He released the lock. */
1424 list_for_each(_p, &tsk->ptrace_children) {
1425 p = list_entry(_p, struct task_struct,
1427 if (!eligible_child(pid, options, p))
1433 if (options & __WNOTHREAD)
1435 tsk = next_thread(tsk);
1436 if (tsk->signal != current->signal)
1438 } while (tsk != current);
1440 read_unlock(&tasklist_lock);
1443 if (options & WNOHANG)
1445 retval = -ERESTARTSYS;
1446 if (signal_pending(current))
1453 current->state = TASK_RUNNING;
1454 remove_wait_queue(¤t->signal->wait_chldexit,&wait);
1460 * For a WNOHANG return, clear out all the fields
1461 * we would set so the user can easily tell the
1465 retval = put_user(0, &infop->si_signo);
1467 retval = put_user(0, &infop->si_errno);
1469 retval = put_user(0, &infop->si_code);
1471 retval = put_user(0, &infop->si_pid);
1473 retval = put_user(0, &infop->si_uid);
1475 retval = put_user(0, &infop->si_status);
1481 asmlinkage long sys_waitid(int which, pid_t pid,
1482 struct siginfo __user *infop, int options,
1483 struct rusage __user *ru)
1487 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1489 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1509 ret = do_wait(pid, options, infop, NULL, ru);
1511 /* avoid REGPARM breakage on x86: */
1512 prevent_tail_call(ret);
1516 asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1517 int options, struct rusage __user *ru)
1521 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1522 __WNOTHREAD|__WCLONE|__WALL))
1524 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1526 /* avoid REGPARM breakage on x86: */
1527 prevent_tail_call(ret);
1531 #ifdef __ARCH_WANT_SYS_WAITPID
1534 * sys_waitpid() remains for compatibility. waitpid() should be
1535 * implemented by calling sys_wait4() from libc.a.
1537 asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1539 return sys_wait4(pid, stat_addr, options, NULL);