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/syscalls.h>
30 #include <asm/uaccess.h>
31 #include <asm/unistd.h>
32 #include <asm/pgtable.h>
33 #include <asm/mmu_context.h>
35 extern void sem_exit (void);
36 extern struct task_struct *child_reaper;
38 int getrusage(struct task_struct *, int, struct rusage __user *);
40 static void __unhash_process(struct task_struct *p)
43 detach_pid(p, PIDTYPE_PID);
44 detach_pid(p, PIDTYPE_TGID);
45 if (thread_group_leader(p)) {
46 detach_pid(p, PIDTYPE_PGID);
47 detach_pid(p, PIDTYPE_SID);
49 __get_cpu_var(process_counts)--;
55 void release_task(struct task_struct * p)
59 struct dentry *proc_dentry;
62 atomic_dec(&p->user->processes);
63 spin_lock(&p->proc_lock);
64 proc_dentry = proc_pid_unhash(p);
65 write_lock_irq(&tasklist_lock);
66 if (unlikely(p->ptrace))
68 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
74 * If we are the last non-leader member of the thread
75 * group, and the leader is zombie, then notify the
76 * group leader's parent process. (if it wants notification.)
79 leader = p->group_leader;
80 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
81 BUG_ON(leader->exit_signal == -1);
82 do_notify_parent(leader, leader->exit_signal);
84 * If we were the last child thread and the leader has
85 * exited already, and the leader's parent ignores SIGCHLD,
86 * then we are the one who should release the leader.
88 * do_notify_parent() will have marked it self-reaping in
91 zap_leader = (leader->exit_signal == -1);
95 write_unlock_irq(&tasklist_lock);
96 spin_unlock(&p->proc_lock);
97 proc_pid_flush(proc_dentry);
102 if (unlikely(zap_leader))
106 /* we are using it only for SMP init */
108 void unhash_process(struct task_struct *p)
110 struct dentry *proc_dentry;
112 spin_lock(&p->proc_lock);
113 proc_dentry = proc_pid_unhash(p);
114 write_lock_irq(&tasklist_lock);
116 write_unlock_irq(&tasklist_lock);
117 spin_unlock(&p->proc_lock);
118 proc_pid_flush(proc_dentry);
122 * This checks not only the pgrp, but falls back on the pid if no
123 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
126 int session_of_pgrp(int pgrp)
128 struct task_struct *p;
131 read_lock(&tasklist_lock);
132 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
133 if (p->signal->session > 0) {
134 sid = p->signal->session;
137 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
138 p = find_task_by_pid(pgrp);
140 sid = p->signal->session;
142 read_unlock(&tasklist_lock);
148 * Determine if a process group is "orphaned", according to the POSIX
149 * definition in 2.2.2.52. Orphaned process groups are not to be affected
150 * by terminal-generated stop signals. Newly orphaned process groups are
151 * to receive a SIGHUP and a SIGCONT.
153 * "I ask you, have you ever known what it is to be an orphan?"
155 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
157 struct task_struct *p;
160 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
161 if (p == ignored_task
162 || p->exit_state >= EXIT_ZOMBIE
163 || p->real_parent->pid == 1)
165 if (process_group(p->real_parent) != pgrp
166 && p->real_parent->signal->session == p->signal->session) {
170 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
171 return ret; /* (sighing) "Often!" */
174 int is_orphaned_pgrp(int pgrp)
178 read_lock(&tasklist_lock);
179 retval = will_become_orphaned_pgrp(pgrp, NULL);
180 read_unlock(&tasklist_lock);
185 static inline int has_stopped_jobs(int pgrp)
188 struct task_struct *p;
190 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
191 if (p->state != TASK_STOPPED)
194 /* If p is stopped by a debugger on a signal that won't
195 stop it, then don't count p as stopped. This isn't
196 perfect but it's a good approximation. */
197 if (unlikely (p->ptrace)
198 && p->exit_code != SIGSTOP
199 && p->exit_code != SIGTSTP
200 && p->exit_code != SIGTTOU
201 && p->exit_code != SIGTTIN)
206 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
211 * reparent_to_init() - Reparent the calling kernel thread to the init task.
213 * If a kernel thread is launched as a result of a system call, or if
214 * it ever exits, it should generally reparent itself to init so that
215 * it is correctly cleaned up on exit.
217 * The various task state such as scheduling policy and priority may have
218 * been inherited from a user process, so we reset them to sane values here.
220 * NOTE that reparent_to_init() gives the caller full capabilities.
222 void reparent_to_init(void)
224 write_lock_irq(&tasklist_lock);
226 ptrace_unlink(current);
227 /* Reparent to init */
228 REMOVE_LINKS(current);
229 current->parent = child_reaper;
230 current->real_parent = child_reaper;
233 /* Set the exit signal to SIGCHLD so we signal init on exit */
234 current->exit_signal = SIGCHLD;
236 if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
237 set_user_nice(current, 0);
241 security_task_reparent_to_init(current);
242 memcpy(current->signal->rlim, init_task.signal->rlim,
243 sizeof(current->signal->rlim));
244 atomic_inc(&(INIT_USER->__count));
245 write_unlock_irq(&tasklist_lock);
246 switch_uid(INIT_USER);
249 void __set_special_pids(pid_t session, pid_t pgrp)
251 struct task_struct *curr = current;
253 if (curr->signal->session != session) {
254 detach_pid(curr, PIDTYPE_SID);
255 curr->signal->session = session;
256 attach_pid(curr, PIDTYPE_SID, session);
258 if (process_group(curr) != pgrp) {
259 detach_pid(curr, PIDTYPE_PGID);
260 curr->signal->pgrp = pgrp;
261 attach_pid(curr, PIDTYPE_PGID, pgrp);
265 void set_special_pids(pid_t session, pid_t pgrp)
267 write_lock_irq(&tasklist_lock);
268 __set_special_pids(session, pgrp);
269 write_unlock_irq(&tasklist_lock);
273 * Let kernel threads use this to say that they
274 * allow a certain signal (since daemonize() will
275 * have disabled all of them by default).
277 int allow_signal(int sig)
279 if (sig < 1 || sig > _NSIG)
282 spin_lock_irq(¤t->sighand->siglock);
283 sigdelset(¤t->blocked, sig);
285 /* Kernel threads handle their own signals.
286 Let the signal code know it'll be handled, so
287 that they don't get converted to SIGKILL or
288 just silently dropped */
289 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
292 spin_unlock_irq(¤t->sighand->siglock);
296 EXPORT_SYMBOL(allow_signal);
298 int disallow_signal(int sig)
300 if (sig < 1 || sig > _NSIG)
303 spin_lock_irq(¤t->sighand->siglock);
304 sigaddset(¤t->blocked, sig);
306 spin_unlock_irq(¤t->sighand->siglock);
310 EXPORT_SYMBOL(disallow_signal);
313 * Put all the gunge required to become a kernel thread without
314 * attached user resources in one place where it belongs.
317 void daemonize(const char *name, ...)
320 struct fs_struct *fs;
323 va_start(args, name);
324 vsnprintf(current->comm, sizeof(current->comm), name, args);
328 * If we were started as result of loading a module, close all of the
329 * user space pages. We don't need them, and if we didn't close them
330 * they would be locked into memory.
334 set_special_pids(1, 1);
335 current->signal->tty = NULL;
337 /* Block and flush all signals */
338 sigfillset(&blocked);
339 sigprocmask(SIG_BLOCK, &blocked, NULL);
340 flush_signals(current);
342 /* Become as one with the init task */
344 exit_fs(current); /* current->fs->count--; */
347 atomic_inc(&fs->count);
349 current->files = init_task.files;
350 atomic_inc(¤t->files->count);
355 EXPORT_SYMBOL(daemonize);
357 static inline void close_files(struct files_struct * files)
365 if (i >= files->max_fdset || i >= files->max_fds)
367 set = files->open_fds->fds_bits[j++];
370 struct file * file = xchg(&files->fd[i], NULL);
372 filp_close(file, files);
380 struct files_struct *get_files_struct(struct task_struct *task)
382 struct files_struct *files;
387 atomic_inc(&files->count);
393 void fastcall put_files_struct(struct files_struct *files)
395 if (atomic_dec_and_test(&files->count)) {
398 * Free the fd and fdset arrays if we expanded them.
400 if (files->fd != &files->fd_array[0])
401 free_fd_array(files->fd, files->max_fds);
402 if (files->max_fdset > __FD_SETSIZE) {
403 free_fdset(files->open_fds, files->max_fdset);
404 free_fdset(files->close_on_exec, files->max_fdset);
406 kmem_cache_free(files_cachep, files);
410 EXPORT_SYMBOL(put_files_struct);
412 static inline void __exit_files(struct task_struct *tsk)
414 struct files_struct * files = tsk->files;
420 put_files_struct(files);
424 void exit_files(struct task_struct *tsk)
429 static inline void __put_fs_struct(struct fs_struct *fs)
431 /* No need to hold fs->lock if we are killing it */
432 if (atomic_dec_and_test(&fs->count)) {
439 mntput(fs->altrootmnt);
441 kmem_cache_free(fs_cachep, fs);
445 void put_fs_struct(struct fs_struct *fs)
450 static inline void __exit_fs(struct task_struct *tsk)
452 struct fs_struct * fs = tsk->fs;
462 void exit_fs(struct task_struct *tsk)
467 EXPORT_SYMBOL_GPL(exit_fs);
470 * Turn us into a lazy TLB process if we
473 static inline void __exit_mm(struct task_struct * tsk)
475 struct mm_struct *mm = tsk->mm;
481 * Serialize with any possible pending coredump.
482 * We must hold mmap_sem around checking core_waiters
483 * and clearing tsk->mm. The core-inducing thread
484 * will increment core_waiters for each thread in the
485 * group with ->mm != NULL.
487 down_read(&mm->mmap_sem);
488 if (mm->core_waiters) {
489 up_read(&mm->mmap_sem);
490 down_write(&mm->mmap_sem);
491 if (!--mm->core_waiters)
492 complete(mm->core_startup_done);
493 up_write(&mm->mmap_sem);
495 wait_for_completion(&mm->core_done);
496 down_read(&mm->mmap_sem);
498 atomic_inc(&mm->mm_count);
499 if (mm != tsk->active_mm) BUG();
500 /* more a memory barrier than a real lock */
503 up_read(&mm->mmap_sem);
504 enter_lazy_tlb(mm, current);
509 void exit_mm(struct task_struct *tsk)
514 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
517 * Make sure we're not reparenting to ourselves and that
518 * the parent is not a zombie.
520 BUG_ON(p == reaper || reaper->state >= EXIT_ZOMBIE || reaper->exit_state >= EXIT_ZOMBIE);
521 p->real_parent = reaper;
522 if (p->parent == p->real_parent)
526 static inline void reparent_thread(task_t *p, task_t *father, int traced)
528 /* We don't want people slaying init. */
529 if (p->exit_signal != -1)
530 p->exit_signal = SIGCHLD;
532 if (p->pdeath_signal)
533 /* We already hold the tasklist_lock here. */
534 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
536 /* Move the child from its dying parent to the new one. */
537 if (unlikely(traced)) {
538 /* Preserve ptrace links if someone else is tracing this child. */
539 list_del_init(&p->ptrace_list);
540 if (p->parent != p->real_parent)
541 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
543 /* If this child is being traced, then we're the one tracing it
544 * anyway, so let go of it.
547 list_del_init(&p->sibling);
548 p->parent = p->real_parent;
549 list_add_tail(&p->sibling, &p->parent->children);
551 /* If we'd notified the old parent about this child's death,
552 * also notify the new parent.
554 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
555 thread_group_empty(p))
556 do_notify_parent(p, p->exit_signal);
557 else if (p->state == TASK_TRACED) {
559 * If it was at a trace stop, turn it into
560 * a normal stop since it's no longer being
563 p->state = TASK_STOPPED;
568 * process group orphan check
569 * Case ii: Our child is in a different pgrp
570 * than we are, and it was the only connection
571 * outside, so the child pgrp is now orphaned.
573 if ((process_group(p) != process_group(father)) &&
574 (p->signal->session == father->signal->session)) {
575 int pgrp = process_group(p);
577 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
578 __kill_pg_info(SIGHUP, (void *)1, pgrp);
579 __kill_pg_info(SIGCONT, (void *)1, pgrp);
585 * When we die, we re-parent all our children.
586 * Try to give them to another thread in our thread
587 * group, and if no such member exists, give it to
588 * the global child reaper process (ie "init")
590 static inline void forget_original_parent(struct task_struct * father,
591 struct list_head *to_release)
593 struct task_struct *p, *reaper = father;
594 struct list_head *_p, *_n;
597 reaper = next_thread(reaper);
598 if (reaper == father) {
599 reaper = child_reaper;
602 } while (reaper->exit_state >= EXIT_ZOMBIE);
605 * There are only two places where our children can be:
607 * - in our child list
608 * - in our ptraced child list
610 * Search them and reparent children.
612 list_for_each_safe(_p, _n, &father->children) {
614 p = list_entry(_p,struct task_struct,sibling);
618 /* if father isn't the real parent, then ptrace must be enabled */
619 BUG_ON(father != p->real_parent && !ptrace);
621 if (father == p->real_parent) {
622 /* reparent with a reaper, real father it's us */
623 choose_new_parent(p, reaper, child_reaper);
624 reparent_thread(p, father, 0);
626 /* reparent ptraced task to its real parent */
628 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
629 thread_group_empty(p))
630 do_notify_parent(p, p->exit_signal);
634 * if the ptraced child is a zombie with exit_signal == -1
635 * we must collect it before we exit, or it will remain
636 * zombie forever since we prevented it from self-reap itself
637 * while it was being traced by us, to be able to see it in wait4.
639 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
640 list_add(&p->ptrace_list, to_release);
642 list_for_each_safe(_p, _n, &father->ptrace_children) {
643 p = list_entry(_p,struct task_struct,ptrace_list);
644 choose_new_parent(p, reaper, child_reaper);
645 reparent_thread(p, father, 1);
650 * Send signals to all our closest relatives so that they know
651 * to properly mourn us..
653 static void exit_notify(struct task_struct *tsk)
656 struct task_struct *t;
657 struct list_head ptrace_dead, *_p, *_n;
659 if (signal_pending(tsk) && !tsk->signal->group_exit
660 && !thread_group_empty(tsk)) {
662 * This occurs when there was a race between our exit
663 * syscall and a group signal choosing us as the one to
664 * wake up. It could be that we are the only thread
665 * alerted to check for pending signals, but another thread
666 * should be woken now to take the signal since we will not.
667 * Now we'll wake all the threads in the group just to make
668 * sure someone gets all the pending signals.
670 read_lock(&tasklist_lock);
671 spin_lock_irq(&tsk->sighand->siglock);
672 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
673 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
674 recalc_sigpending_tsk(t);
675 if (signal_pending(t))
676 signal_wake_up(t, 0);
678 spin_unlock_irq(&tsk->sighand->siglock);
679 read_unlock(&tasklist_lock);
682 write_lock_irq(&tasklist_lock);
685 * This does two things:
687 * A. Make init inherit all the child processes
688 * B. Check to see if any process groups have become orphaned
689 * as a result of our exiting, and if they have any stopped
690 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
693 INIT_LIST_HEAD(&ptrace_dead);
694 forget_original_parent(tsk, &ptrace_dead);
695 BUG_ON(!list_empty(&tsk->children));
696 BUG_ON(!list_empty(&tsk->ptrace_children));
699 * Check to see if any process groups have become orphaned
700 * as a result of our exiting, and if they have any stopped
701 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
703 * Case i: Our father is in a different pgrp than we are
704 * and we were the only connection outside, so our pgrp
705 * is about to become orphaned.
708 t = tsk->real_parent;
710 if ((process_group(t) != process_group(tsk)) &&
711 (t->signal->session == tsk->signal->session) &&
712 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
713 has_stopped_jobs(process_group(tsk))) {
714 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
715 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
718 /* Let father know we died
720 * Thread signals are configurable, but you aren't going to use
721 * that to send signals to arbitary processes.
722 * That stops right now.
724 * If the parent exec id doesn't match the exec id we saved
725 * when we started then we know the parent has changed security
728 * If our self_exec id doesn't match our parent_exec_id then
729 * we have changed execution domain as these two values started
730 * the same after a fork.
734 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
735 ( tsk->parent_exec_id != t->self_exec_id ||
736 tsk->self_exec_id != tsk->parent_exec_id)
737 && !capable(CAP_KILL))
738 tsk->exit_signal = SIGCHLD;
741 /* If something other than our normal parent is ptracing us, then
742 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
743 * only has special meaning to our real parent.
745 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
746 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
747 do_notify_parent(tsk, signal);
748 } else if (tsk->ptrace) {
749 do_notify_parent(tsk, SIGCHLD);
753 if (tsk->exit_signal == -1 && tsk->ptrace == 0)
755 tsk->exit_state = state;
758 * Clear these here so that update_process_times() won't try to deliver
759 * itimer, profile or rlimit signals to this task while it is in late exit.
761 tsk->it_virt_value = 0;
762 tsk->it_prof_value = 0;
764 write_unlock_irq(&tasklist_lock);
766 list_for_each_safe(_p, _n, &ptrace_dead) {
768 t = list_entry(_p,struct task_struct,ptrace_list);
772 /* If the process is dead, release it - nobody will wait for it */
773 if (state == EXIT_DEAD)
776 /* PF_DEAD causes final put_task_struct after we schedule. */
778 tsk->flags |= PF_DEAD;
781 fastcall NORET_TYPE void do_exit(long code)
783 struct task_struct *tsk = current;
786 profile_task_exit(tsk);
788 if (unlikely(in_interrupt()))
789 panic("Aiee, killing interrupt handler!");
790 if (unlikely(!tsk->pid))
791 panic("Attempted to kill the idle task!");
792 if (unlikely(tsk->pid == 1))
793 panic("Attempted to kill init!");
796 tsk->flags |= PF_EXITING;
797 del_timer_sync(&tsk->real_timer);
799 if (unlikely(in_atomic()))
800 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
801 current->comm, current->pid,
804 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
805 current->ptrace_message = code;
806 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
809 group_dead = atomic_dec_and_test(&tsk->signal->live);
821 if (group_dead && tsk->signal->leader)
822 disassociate_ctty(1);
824 module_put(tsk->thread_info->exec_domain->module);
826 module_put(tsk->binfmt->module);
828 tsk->exit_code = code;
831 mpol_free(tsk->mempolicy);
832 tsk->mempolicy = NULL;
835 BUG_ON(!(current->flags & PF_DEAD));
838 /* Avoid "noreturn function does return". */
842 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
850 EXPORT_SYMBOL(complete_and_exit);
852 asmlinkage long sys_exit(int error_code)
854 do_exit((error_code&0xff)<<8);
857 task_t fastcall *next_thread(const task_t *p)
862 if (!spin_is_locked(&p->sighand->siglock) &&
863 !rwlock_is_locked(&tasklist_lock))
866 return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
869 EXPORT_SYMBOL(next_thread);
872 * Take down every thread in the group. This is called by fatal signals
873 * as well as by sys_exit_group (below).
876 do_group_exit(int exit_code)
878 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
880 if (current->signal->group_exit)
881 exit_code = current->signal->group_exit_code;
882 else if (!thread_group_empty(current)) {
883 struct signal_struct *const sig = current->signal;
884 struct sighand_struct *const sighand = current->sighand;
885 read_lock(&tasklist_lock);
886 spin_lock_irq(&sighand->siglock);
888 /* Another thread got here before we took the lock. */
889 exit_code = sig->group_exit_code;
892 sig->group_exit_code = exit_code;
893 zap_other_threads(current);
895 spin_unlock_irq(&sighand->siglock);
896 read_unlock(&tasklist_lock);
904 * this kills every thread in the thread group. Note that any externally
905 * wait4()-ing process will get the correct exit code - even if this
906 * thread is not the thread group leader.
908 asmlinkage void sys_exit_group(int error_code)
910 do_group_exit((error_code & 0xff) << 8);
913 static int eligible_child(pid_t pid, int options, task_t *p)
919 if (process_group(p) != process_group(current))
921 } else if (pid != -1) {
922 if (process_group(p) != -pid)
927 * Do not consider detached threads that are
930 if (p->exit_signal == -1 && !p->ptrace)
933 /* Wait for all children (clone and not) if __WALL is set;
934 * otherwise, wait for clone children *only* if __WCLONE is
935 * set; otherwise, wait for non-clone children *only*. (Note:
936 * A "clone" child here is one that reports to its parent
937 * using a signal other than SIGCHLD.) */
938 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
939 && !(options & __WALL))
942 * Do not consider thread group leaders that are
943 * in a non-empty thread group:
945 if (current->tgid != p->tgid && delay_group_leader(p))
948 if (security_task_wait(p))
954 static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
956 struct siginfo __user *infop,
957 struct rusage __user *rusagep)
959 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
962 retval = put_user(SIGCHLD, &infop->si_signo);
964 retval = put_user(0, &infop->si_errno);
966 retval = put_user((short)why, &infop->si_code);
968 retval = put_user(pid, &infop->si_pid);
970 retval = put_user(uid, &infop->si_uid);
972 retval = put_user(status, &infop->si_status);
979 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
980 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
981 * the lock and this task is uninteresting. If we return nonzero, we have
982 * released the lock and the system call should return.
984 static int wait_task_zombie(task_t *p, int noreap,
985 struct siginfo __user *infop,
986 int __user *stat_addr, struct rusage __user *ru)
992 if (unlikely(noreap)) {
995 int exit_code = p->exit_code;
998 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1000 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1003 read_unlock(&tasklist_lock);
1004 if ((exit_code & 0x7f) == 0) {
1006 status = exit_code >> 8;
1008 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1009 status = exit_code & 0x7f;
1011 return wait_noreap_copyout(p, pid, uid, why,
1016 * Try to move the task's state to DEAD
1017 * only one thread is allowed to do this:
1019 state = xchg(&p->exit_state, EXIT_DEAD);
1020 if (state != EXIT_ZOMBIE) {
1021 BUG_ON(state != EXIT_DEAD);
1024 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1026 * This can only happen in a race with a ptraced thread
1027 * dying on another processor.
1032 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
1034 * The resource counters for the group leader are in its
1035 * own task_struct. Those for dead threads in the group
1036 * are in its signal_struct, as are those for the child
1037 * processes it has previously reaped. All these
1038 * accumulate in the parent's signal_struct c* fields.
1040 * We don't bother to take a lock here to protect these
1041 * p->signal fields, because they are only touched by
1042 * __exit_signal, which runs with tasklist_lock
1043 * write-locked anyway, and so is excluded here. We do
1044 * need to protect the access to p->parent->signal fields,
1045 * as other threads in the parent group can be right
1046 * here reaping other children at the same time.
1048 spin_lock_irq(&p->parent->sighand->siglock);
1049 p->parent->signal->cutime +=
1050 p->utime + p->signal->utime + p->signal->cutime;
1051 p->parent->signal->cstime +=
1052 p->stime + p->signal->stime + p->signal->cstime;
1053 p->parent->signal->cmin_flt +=
1054 p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
1055 p->parent->signal->cmaj_flt +=
1056 p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
1057 p->parent->signal->cnvcsw +=
1058 p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
1059 p->parent->signal->cnivcsw +=
1060 p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
1061 spin_unlock_irq(&p->parent->sighand->siglock);
1065 * Now we are sure this task is interesting, and no other
1066 * thread can reap it because we set its state to EXIT_DEAD.
1068 read_unlock(&tasklist_lock);
1070 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1071 status = p->signal->group_exit
1072 ? p->signal->group_exit_code : p->exit_code;
1073 if (!retval && stat_addr)
1074 retval = put_user(status, stat_addr);
1075 if (!retval && infop)
1076 retval = put_user(SIGCHLD, &infop->si_signo);
1077 if (!retval && infop)
1078 retval = put_user(0, &infop->si_errno);
1079 if (!retval && infop) {
1082 if ((status & 0x7f) == 0) {
1086 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1089 retval = put_user((short)why, &infop->si_code);
1091 retval = put_user(status, &infop->si_status);
1093 if (!retval && infop)
1094 retval = put_user(p->pid, &infop->si_pid);
1095 if (!retval && infop)
1096 retval = put_user(p->uid, &infop->si_uid);
1098 // TODO: is this safe?
1099 p->exit_state = EXIT_ZOMBIE;
1103 if (p->real_parent != p->parent) {
1104 write_lock_irq(&tasklist_lock);
1105 /* Double-check with lock held. */
1106 if (p->real_parent != p->parent) {
1108 // TODO: is this safe?
1109 p->exit_state = EXIT_ZOMBIE;
1111 * If this is not a detached task, notify the parent.
1112 * If it's still not detached after that, don't release
1115 if (p->exit_signal != -1) {
1116 do_notify_parent(p, p->exit_signal);
1117 if (p->exit_signal != -1)
1121 write_unlock_irq(&tasklist_lock);
1130 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1131 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1132 * the lock and this task is uninteresting. If we return nonzero, we have
1133 * released the lock and the system call should return.
1135 static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
1136 struct siginfo __user *infop,
1137 int __user *stat_addr, struct rusage __user *ru)
1139 int retval, exit_code;
1143 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1144 p->signal && p->signal->group_stop_count > 0)
1146 * A group stop is in progress and this is the group leader.
1147 * We won't report until all threads have stopped.
1152 * Now we are pretty sure this task is interesting.
1153 * Make sure it doesn't get reaped out from under us while we
1154 * give up the lock and then examine it below. We don't want to
1155 * keep holding onto the tasklist_lock while we call getrusage and
1156 * possibly take page faults for user memory.
1159 read_unlock(&tasklist_lock);
1161 if (unlikely(noreap)) {
1164 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1166 exit_code = p->exit_code;
1167 if (unlikely(!exit_code) ||
1168 unlikely(p->state > TASK_STOPPED))
1170 return wait_noreap_copyout(p, pid, uid,
1171 why, (exit_code << 8) | 0x7f,
1175 write_lock_irq(&tasklist_lock);
1178 * This uses xchg to be atomic with the thread resuming and setting
1179 * it. It must also be done with the write lock held to prevent a
1180 * race with the EXIT_ZOMBIE case.
1182 exit_code = xchg(&p->exit_code, 0);
1183 if (unlikely(p->exit_state >= EXIT_ZOMBIE)) {
1185 * The task resumed and then died. Let the next iteration
1186 * catch it in EXIT_ZOMBIE. Note that exit_code might
1187 * already be zero here if it resumed and did _exit(0).
1188 * The task itself is dead and won't touch exit_code again;
1189 * other processors in this function are locked out.
1191 p->exit_code = exit_code;
1194 if (unlikely(exit_code == 0)) {
1196 * Another thread in this function got to it first, or it
1197 * resumed, or it resumed and then died.
1199 write_unlock_irq(&tasklist_lock);
1203 * We are returning to the wait loop without having successfully
1204 * removed the process and having released the lock. We cannot
1205 * continue, since the "p" task pointer is potentially stale.
1207 * Return -EAGAIN, and do_wait() will restart the loop from the
1208 * beginning. Do _not_ re-acquire the lock.
1213 /* move to end of parent's list to avoid starvation */
1215 add_parent(p, p->parent);
1217 write_unlock_irq(&tasklist_lock);
1219 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1220 if (!retval && stat_addr)
1221 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1222 if (!retval && infop)
1223 retval = put_user(SIGCHLD, &infop->si_signo);
1224 if (!retval && infop)
1225 retval = put_user(0, &infop->si_errno);
1226 if (!retval && infop)
1227 retval = put_user((short)((p->ptrace & PT_PTRACED)
1228 ? CLD_TRAPPED : CLD_STOPPED),
1230 if (!retval && infop)
1231 retval = put_user(exit_code, &infop->si_status);
1232 if (!retval && infop)
1233 retval = put_user(p->pid, &infop->si_pid);
1234 if (!retval && infop)
1235 retval = put_user(p->uid, &infop->si_uid);
1245 * Handle do_wait work for one task in a live, non-stopped state.
1246 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1247 * the lock and this task is uninteresting. If we return nonzero, we have
1248 * released the lock and the system call should return.
1250 static int wait_task_continued(task_t *p, int noreap,
1251 struct siginfo __user *infop,
1252 int __user *stat_addr, struct rusage __user *ru)
1258 if (unlikely(!p->signal))
1261 if (p->signal->stop_state >= 0)
1264 spin_lock_irq(&p->sighand->siglock);
1265 if (p->signal->stop_state >= 0) { /* Re-check with the lock held. */
1266 spin_unlock_irq(&p->sighand->siglock);
1270 p->signal->stop_state = 0;
1271 spin_unlock_irq(&p->sighand->siglock);
1276 read_unlock(&tasklist_lock);
1279 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1281 if (!retval && stat_addr)
1282 retval = put_user(0xffff, stat_addr);
1286 retval = wait_noreap_copyout(p, pid, uid,
1287 CLD_CONTINUED, SIGCONT,
1289 BUG_ON(retval == 0);
1296 static inline int my_ptrace_child(struct task_struct *p)
1298 if (!(p->ptrace & PT_PTRACED))
1300 if (!(p->ptrace & PT_ATTACHED))
1303 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1304 * we are the attacher. If we are the real parent, this is a race
1305 * inside ptrace_attach. It is waiting for the tasklist_lock,
1306 * which we have to switch the parent links, but has already set
1307 * the flags in p->ptrace.
1309 return (p->parent != p->real_parent);
1312 static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1313 int __user *stat_addr, struct rusage __user *ru)
1315 DECLARE_WAITQUEUE(wait, current);
1316 struct task_struct *tsk;
1319 add_wait_queue(¤t->wait_chldexit,&wait);
1322 * We will set this flag if we see any child that might later
1323 * match our criteria, even if we are not able to reap it yet.
1326 current->state = TASK_INTERRUPTIBLE;
1327 read_lock(&tasklist_lock);
1330 struct task_struct *p;
1331 struct list_head *_p;
1334 list_for_each(_p,&tsk->children) {
1335 p = list_entry(_p,struct task_struct,sibling);
1337 ret = eligible_child(pid, options, p);
1343 if (!my_ptrace_child(p))
1348 * It's stopped now, so it might later
1349 * continue, exit, or stop again.
1352 if (!(options & WUNTRACED) &&
1353 !my_ptrace_child(p))
1355 retval = wait_task_stopped(p, ret == 2,
1356 (options & WNOWAIT),
1359 if (retval == -EAGAIN)
1361 if (retval != 0) /* He released the lock. */
1366 if (p->exit_state == EXIT_DEAD)
1368 // case EXIT_ZOMBIE:
1369 if (p->exit_state == EXIT_ZOMBIE) {
1371 * Eligible but we cannot release
1375 goto check_continued;
1376 if (!likely(options & WEXITED))
1378 retval = wait_task_zombie(
1379 p, (options & WNOWAIT),
1380 infop, stat_addr, ru);
1381 /* He released the lock. */
1388 * It's running now, so it might later
1389 * exit, stop, or stop and then continue.
1392 if (!unlikely(options & WCONTINUED))
1394 retval = wait_task_continued(
1395 p, (options & WNOWAIT),
1396 infop, stat_addr, ru);
1397 if (retval != 0) /* He released the lock. */
1403 list_for_each(_p, &tsk->ptrace_children) {
1404 p = list_entry(_p, struct task_struct,
1406 if (!eligible_child(pid, options, p))
1412 if (options & __WNOTHREAD)
1414 tsk = next_thread(tsk);
1415 if (tsk->signal != current->signal)
1417 } while (tsk != current);
1419 read_unlock(&tasklist_lock);
1422 if (options & WNOHANG)
1424 retval = -ERESTARTSYS;
1425 if (signal_pending(current))
1432 current->state = TASK_RUNNING;
1433 remove_wait_queue(¤t->wait_chldexit,&wait);
1439 * For a WNOHANG return, clear out all the fields
1440 * we would set so the user can easily tell the
1444 retval = put_user(0, &infop->si_signo);
1446 retval = put_user(0, &infop->si_errno);
1448 retval = put_user(0, &infop->si_code);
1450 retval = put_user(0, &infop->si_pid);
1452 retval = put_user(0, &infop->si_uid);
1454 retval = put_user(0, &infop->si_status);
1460 asmlinkage long sys_waitid(int which, pid_t pid,
1461 struct siginfo __user *infop, int options,
1462 struct rusage __user *ru)
1466 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1468 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1488 ret = do_wait(pid, options, infop, NULL, ru);
1490 /* avoid REGPARM breakage on x86: */
1491 prevent_tail_call(ret);
1495 asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1496 int options, struct rusage __user *ru)
1500 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1501 __WNOTHREAD|__WCLONE|__WALL))
1503 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1505 /* avoid REGPARM breakage on x86: */
1506 prevent_tail_call(ret);
1510 #ifdef __ARCH_WANT_SYS_WAITPID
1513 * sys_waitpid() remains for compatibility. waitpid() should be
1514 * implemented by calling sys_wait4() from libc.a.
1516 asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1518 return sys_wait4(pid, stat_addr, options, NULL);