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>
29 #include <linux/vs_limit.h>
31 #include <asm/uaccess.h>
32 #include <asm/unistd.h>
33 #include <asm/pgtable.h>
34 #include <asm/mmu_context.h>
36 extern void sem_exit (void);
37 extern struct task_struct *child_reaper;
39 int getrusage(struct task_struct *, int, struct rusage __user *);
41 static void __unhash_process(struct task_struct *p)
44 /* tasklist_lock is held, is this sufficient? */
46 atomic_dec(&p->vx_info->cvirt.nr_threads);
49 detach_pid(p, PIDTYPE_PID);
50 detach_pid(p, PIDTYPE_TGID);
51 if (thread_group_leader(p)) {
52 detach_pid(p, PIDTYPE_PGID);
53 detach_pid(p, PIDTYPE_SID);
55 __get_cpu_var(process_counts)--;
61 void release_task(struct task_struct * p)
65 struct dentry *proc_dentry;
68 atomic_dec(&p->user->processes);
69 spin_lock(&p->proc_lock);
70 proc_dentry = proc_pid_unhash(p);
71 write_lock_irq(&tasklist_lock);
72 if (unlikely(p->ptrace))
74 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
80 * If we are the last non-leader member of the thread
81 * group, and the leader is zombie, then notify the
82 * group leader's parent process. (if it wants notification.)
85 leader = p->group_leader;
86 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
87 BUG_ON(leader->exit_signal == -1);
88 do_notify_parent(leader, leader->exit_signal);
90 * If we were the last child thread and the leader has
91 * exited already, and the leader's parent ignores SIGCHLD,
92 * then we are the one who should release the leader.
94 * do_notify_parent() will have marked it self-reaping in
97 zap_leader = (leader->exit_signal == -1);
101 write_unlock_irq(&tasklist_lock);
102 spin_unlock(&p->proc_lock);
103 proc_pid_flush(proc_dentry);
108 if (unlikely(zap_leader))
112 /* we are using it only for SMP init */
114 void unhash_process(struct task_struct *p)
116 struct dentry *proc_dentry;
118 spin_lock(&p->proc_lock);
119 proc_dentry = proc_pid_unhash(p);
120 write_lock_irq(&tasklist_lock);
122 write_unlock_irq(&tasklist_lock);
123 spin_unlock(&p->proc_lock);
124 proc_pid_flush(proc_dentry);
128 * This checks not only the pgrp, but falls back on the pid if no
129 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
132 int session_of_pgrp(int pgrp)
134 struct task_struct *p;
137 read_lock(&tasklist_lock);
138 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
139 if (p->signal->session > 0) {
140 sid = p->signal->session;
143 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
144 p = find_task_by_pid(pgrp);
146 sid = p->signal->session;
148 read_unlock(&tasklist_lock);
154 * Determine if a process group is "orphaned", according to the POSIX
155 * definition in 2.2.2.52. Orphaned process groups are not to be affected
156 * by terminal-generated stop signals. Newly orphaned process groups are
157 * to receive a SIGHUP and a SIGCONT.
159 * "I ask you, have you ever known what it is to be an orphan?"
161 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
163 struct task_struct *p;
166 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
167 if (p == ignored_task
168 || p->exit_state >= EXIT_ZOMBIE
169 || p->real_parent->pid == 1)
171 if (process_group(p->real_parent) != pgrp
172 && p->real_parent->signal->session == p->signal->session) {
176 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
177 return ret; /* (sighing) "Often!" */
180 int is_orphaned_pgrp(int pgrp)
184 read_lock(&tasklist_lock);
185 retval = will_become_orphaned_pgrp(pgrp, NULL);
186 read_unlock(&tasklist_lock);
191 static inline int has_stopped_jobs(int pgrp)
194 struct task_struct *p;
196 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
197 if (p->state != TASK_STOPPED)
200 /* If p is stopped by a debugger on a signal that won't
201 stop it, then don't count p as stopped. This isn't
202 perfect but it's a good approximation. */
203 if (unlikely (p->ptrace)
204 && p->exit_code != SIGSTOP
205 && p->exit_code != SIGTSTP
206 && p->exit_code != SIGTTOU
207 && p->exit_code != SIGTTIN)
212 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
217 * reparent_to_init() - Reparent the calling kernel thread to the init task.
219 * If a kernel thread is launched as a result of a system call, or if
220 * it ever exits, it should generally reparent itself to init so that
221 * it is correctly cleaned up on exit.
223 * The various task state such as scheduling policy and priority may have
224 * been inherited from a user process, so we reset them to sane values here.
226 * NOTE that reparent_to_init() gives the caller full capabilities.
228 void reparent_to_init(void)
230 write_lock_irq(&tasklist_lock);
232 ptrace_unlink(current);
233 /* Reparent to init */
234 REMOVE_LINKS(current);
235 /* FIXME handle vchild_reaper/initpid */
236 current->parent = child_reaper;
237 current->real_parent = child_reaper;
240 /* Set the exit signal to SIGCHLD so we signal init on exit */
241 current->exit_signal = SIGCHLD;
243 if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
244 set_user_nice(current, 0);
248 security_task_reparent_to_init(current);
249 memcpy(current->signal->rlim, init_task.signal->rlim,
250 sizeof(current->signal->rlim));
251 atomic_inc(&(INIT_USER->__count));
252 write_unlock_irq(&tasklist_lock);
253 switch_uid(INIT_USER);
256 void __set_special_pids(pid_t session, pid_t pgrp)
258 struct task_struct *curr = current;
260 if (curr->signal->session != session) {
261 detach_pid(curr, PIDTYPE_SID);
262 curr->signal->session = session;
263 attach_pid(curr, PIDTYPE_SID, session);
265 if (process_group(curr) != pgrp) {
266 detach_pid(curr, PIDTYPE_PGID);
267 curr->signal->pgrp = pgrp;
268 attach_pid(curr, PIDTYPE_PGID, pgrp);
272 void set_special_pids(pid_t session, pid_t pgrp)
274 write_lock_irq(&tasklist_lock);
275 __set_special_pids(session, pgrp);
276 write_unlock_irq(&tasklist_lock);
280 * Let kernel threads use this to say that they
281 * allow a certain signal (since daemonize() will
282 * have disabled all of them by default).
284 int allow_signal(int sig)
286 if (sig < 1 || sig > _NSIG)
289 spin_lock_irq(¤t->sighand->siglock);
290 sigdelset(¤t->blocked, sig);
292 /* Kernel threads handle their own signals.
293 Let the signal code know it'll be handled, so
294 that they don't get converted to SIGKILL or
295 just silently dropped */
296 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
299 spin_unlock_irq(¤t->sighand->siglock);
303 EXPORT_SYMBOL(allow_signal);
305 int disallow_signal(int sig)
307 if (sig < 1 || sig > _NSIG)
310 spin_lock_irq(¤t->sighand->siglock);
311 sigaddset(¤t->blocked, sig);
313 spin_unlock_irq(¤t->sighand->siglock);
317 EXPORT_SYMBOL(disallow_signal);
320 * Put all the gunge required to become a kernel thread without
321 * attached user resources in one place where it belongs.
324 void daemonize(const char *name, ...)
327 struct fs_struct *fs;
330 va_start(args, name);
331 vsnprintf(current->comm, sizeof(current->comm), name, args);
335 * If we were started as result of loading a module, close all of the
336 * user space pages. We don't need them, and if we didn't close them
337 * they would be locked into memory.
341 set_special_pids(1, 1);
343 current->signal->tty = NULL;
346 /* Block and flush all signals */
347 sigfillset(&blocked);
348 sigprocmask(SIG_BLOCK, &blocked, NULL);
349 flush_signals(current);
351 /* Become as one with the init task */
353 exit_fs(current); /* current->fs->count--; */
356 atomic_inc(&fs->count);
358 current->files = init_task.files;
359 atomic_inc(¤t->files->count);
364 EXPORT_SYMBOL(daemonize);
366 static inline void close_files(struct files_struct * files)
374 if (i >= files->max_fdset || i >= files->max_fds)
376 set = files->open_fds->fds_bits[j++];
379 struct file * file = xchg(&files->fd[i], NULL);
381 filp_close(file, files);
390 struct files_struct *get_files_struct(struct task_struct *task)
392 struct files_struct *files;
397 atomic_inc(&files->count);
403 void fastcall put_files_struct(struct files_struct *files)
405 if (atomic_dec_and_test(&files->count)) {
408 * Free the fd and fdset arrays if we expanded them.
410 if (files->fd != &files->fd_array[0])
411 free_fd_array(files->fd, files->max_fds);
412 if (files->max_fdset > __FD_SETSIZE) {
413 free_fdset(files->open_fds, files->max_fdset);
414 free_fdset(files->close_on_exec, files->max_fdset);
416 kmem_cache_free(files_cachep, files);
420 EXPORT_SYMBOL(put_files_struct);
422 static inline void __exit_files(struct task_struct *tsk)
424 struct files_struct * files = tsk->files;
430 put_files_struct(files);
434 void exit_files(struct task_struct *tsk)
439 static inline void __put_fs_struct(struct fs_struct *fs)
441 /* No need to hold fs->lock if we are killing it */
442 if (atomic_dec_and_test(&fs->count)) {
449 mntput(fs->altrootmnt);
451 kmem_cache_free(fs_cachep, fs);
455 void put_fs_struct(struct fs_struct *fs)
460 static inline void __exit_fs(struct task_struct *tsk)
462 struct fs_struct * fs = tsk->fs;
472 void exit_fs(struct task_struct *tsk)
477 EXPORT_SYMBOL_GPL(exit_fs);
480 * Turn us into a lazy TLB process if we
483 static inline void __exit_mm(struct task_struct * tsk)
485 struct mm_struct *mm = tsk->mm;
491 * Serialize with any possible pending coredump.
492 * We must hold mmap_sem around checking core_waiters
493 * and clearing tsk->mm. The core-inducing thread
494 * will increment core_waiters for each thread in the
495 * group with ->mm != NULL.
497 down_read(&mm->mmap_sem);
498 if (mm->core_waiters) {
499 up_read(&mm->mmap_sem);
500 down_write(&mm->mmap_sem);
501 if (!--mm->core_waiters)
502 complete(mm->core_startup_done);
503 up_write(&mm->mmap_sem);
505 wait_for_completion(&mm->core_done);
506 down_read(&mm->mmap_sem);
508 atomic_inc(&mm->mm_count);
509 if (mm != tsk->active_mm) BUG();
510 /* more a memory barrier than a real lock */
513 up_read(&mm->mmap_sem);
514 enter_lazy_tlb(mm, current);
519 void exit_mm(struct task_struct *tsk)
524 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
527 * Make sure we're not reparenting to ourselves and that
528 * the parent is not a zombie.
530 BUG_ON(p == reaper || reaper->state >= EXIT_ZOMBIE || reaper->exit_state >= EXIT_ZOMBIE);
531 p->real_parent = reaper;
532 if (p->parent == p->real_parent)
536 static inline void reparent_thread(task_t *p, task_t *father, int traced)
538 /* We don't want people slaying init. */
539 if (p->exit_signal != -1)
540 p->exit_signal = SIGCHLD;
542 if (p->pdeath_signal)
543 /* We already hold the tasklist_lock here. */
544 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
546 /* Move the child from its dying parent to the new one. */
547 if (unlikely(traced)) {
548 /* Preserve ptrace links if someone else is tracing this child. */
549 list_del_init(&p->ptrace_list);
550 if (p->parent != p->real_parent)
551 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
553 /* If this child is being traced, then we're the one tracing it
554 * anyway, so let go of it.
557 list_del_init(&p->sibling);
558 p->parent = p->real_parent;
559 list_add_tail(&p->sibling, &p->parent->children);
561 /* If we'd notified the old parent about this child's death,
562 * also notify the new parent.
564 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
565 thread_group_empty(p))
566 do_notify_parent(p, p->exit_signal);
567 else if (p->state == TASK_TRACED) {
569 * If it was at a trace stop, turn it into
570 * a normal stop since it's no longer being
578 * process group orphan check
579 * Case ii: Our child is in a different pgrp
580 * than we are, and it was the only connection
581 * outside, so the child pgrp is now orphaned.
583 if ((process_group(p) != process_group(father)) &&
584 (p->signal->session == father->signal->session)) {
585 int pgrp = process_group(p);
587 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
588 __kill_pg_info(SIGHUP, (void *)1, pgrp);
589 __kill_pg_info(SIGCONT, (void *)1, pgrp);
595 * When we die, we re-parent all our children.
596 * Try to give them to another thread in our thread
597 * group, and if no such member exists, give it to
598 * the global child reaper process (ie "init")
600 static inline void forget_original_parent(struct task_struct * father,
601 struct list_head *to_release)
603 struct task_struct *p, *reaper = father;
604 struct list_head *_p, *_n;
606 /* FIXME handle vchild_reaper/initpid */
608 reaper = next_thread(reaper);
609 if (reaper == father) {
610 reaper = child_reaper;
613 } while (reaper->exit_state >= EXIT_ZOMBIE);
616 * There are only two places where our children can be:
618 * - in our child list
619 * - in our ptraced child list
621 * Search them and reparent children.
623 list_for_each_safe(_p, _n, &father->children) {
625 p = list_entry(_p,struct task_struct,sibling);
629 /* if father isn't the real parent, then ptrace must be enabled */
630 BUG_ON(father != p->real_parent && !ptrace);
632 if (father == p->real_parent) {
633 /* reparent with a reaper, real father it's us */
634 choose_new_parent(p, reaper, child_reaper);
635 reparent_thread(p, father, 0);
637 /* reparent ptraced task to its real parent */
639 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
640 thread_group_empty(p))
641 do_notify_parent(p, p->exit_signal);
645 * if the ptraced child is a zombie with exit_signal == -1
646 * we must collect it before we exit, or it will remain
647 * zombie forever since we prevented it from self-reap itself
648 * while it was being traced by us, to be able to see it in wait4.
650 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
651 list_add(&p->ptrace_list, to_release);
653 list_for_each_safe(_p, _n, &father->ptrace_children) {
654 p = list_entry(_p,struct task_struct,ptrace_list);
655 choose_new_parent(p, reaper, child_reaper);
656 reparent_thread(p, father, 1);
661 * Send signals to all our closest relatives so that they know
662 * to properly mourn us..
664 static void exit_notify(struct task_struct *tsk)
667 struct task_struct *t;
668 struct list_head ptrace_dead, *_p, *_n;
670 if (signal_pending(tsk) && !tsk->signal->group_exit
671 && !thread_group_empty(tsk)) {
673 * This occurs when there was a race between our exit
674 * syscall and a group signal choosing us as the one to
675 * wake up. It could be that we are the only thread
676 * alerted to check for pending signals, but another thread
677 * should be woken now to take the signal since we will not.
678 * Now we'll wake all the threads in the group just to make
679 * sure someone gets all the pending signals.
681 read_lock(&tasklist_lock);
682 spin_lock_irq(&tsk->sighand->siglock);
683 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
684 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
685 recalc_sigpending_tsk(t);
686 if (signal_pending(t))
687 signal_wake_up(t, 0);
689 spin_unlock_irq(&tsk->sighand->siglock);
690 read_unlock(&tasklist_lock);
693 write_lock_irq(&tasklist_lock);
696 * This does two things:
698 * A. Make init inherit all the child processes
699 * B. 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)
704 INIT_LIST_HEAD(&ptrace_dead);
705 forget_original_parent(tsk, &ptrace_dead);
706 BUG_ON(!list_empty(&tsk->children));
707 BUG_ON(!list_empty(&tsk->ptrace_children));
710 * Check to see if any process groups have become orphaned
711 * as a result of our exiting, and if they have any stopped
712 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
714 * Case i: Our father is in a different pgrp than we are
715 * and we were the only connection outside, so our pgrp
716 * is about to become orphaned.
719 t = tsk->real_parent;
721 if ((process_group(t) != process_group(tsk)) &&
722 (t->signal->session == tsk->signal->session) &&
723 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
724 has_stopped_jobs(process_group(tsk))) {
725 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
726 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
729 /* Let father know we died
731 * Thread signals are configurable, but you aren't going to use
732 * that to send signals to arbitary processes.
733 * That stops right now.
735 * If the parent exec id doesn't match the exec id we saved
736 * when we started then we know the parent has changed security
739 * If our self_exec id doesn't match our parent_exec_id then
740 * we have changed execution domain as these two values started
741 * the same after a fork.
745 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
746 ( tsk->parent_exec_id != t->self_exec_id ||
747 tsk->self_exec_id != tsk->parent_exec_id)
748 && !capable(CAP_KILL))
749 tsk->exit_signal = SIGCHLD;
752 /* If something other than our normal parent is ptracing us, then
753 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
754 * only has special meaning to our real parent.
756 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
757 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
758 do_notify_parent(tsk, signal);
759 } else if (tsk->ptrace) {
760 do_notify_parent(tsk, SIGCHLD);
764 if (tsk->exit_signal == -1 && tsk->ptrace == 0)
766 tsk->exit_state = state;
769 * Clear these here so that update_process_times() won't try to deliver
770 * itimer, profile or rlimit signals to this task while it is in late exit.
772 tsk->it_virt_value = 0;
773 tsk->it_prof_value = 0;
775 write_unlock_irq(&tasklist_lock);
777 list_for_each_safe(_p, _n, &ptrace_dead) {
779 t = list_entry(_p,struct task_struct,ptrace_list);
783 /* If the process is dead, release it - nobody will wait for it */
784 if (state == EXIT_DEAD)
787 /* PF_DEAD causes final put_task_struct after we schedule. */
789 tsk->flags |= PF_DEAD;
792 fastcall NORET_TYPE void do_exit(long code)
794 struct task_struct *tsk = current;
797 profile_task_exit(tsk);
799 if (unlikely(in_interrupt()))
800 panic("Aiee, killing interrupt handler!");
801 if (unlikely(!tsk->pid))
802 panic("Attempted to kill the idle task!");
803 if (unlikely(tsk->pid == 1))
804 panic("Attempted to kill init!");
807 tsk->flags |= PF_EXITING;
808 del_timer_sync(&tsk->real_timer);
810 if (unlikely(in_atomic()))
811 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
812 current->comm, current->pid,
815 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
816 current->ptrace_message = code;
817 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
820 group_dead = atomic_dec_and_test(&tsk->signal->live);
823 if (current->tux_info) {
824 #ifdef CONFIG_TUX_DEBUG
825 printk("Possibly unexpected TUX-thread exit(%ld) at %p?\n",
826 code, __builtin_return_address(0));
839 if (group_dead && tsk->signal->leader)
840 disassociate_ctty(1);
842 module_put(tsk->thread_info->exec_domain->module);
844 module_put(tsk->binfmt->module);
846 tsk->exit_code = code;
849 mpol_free(tsk->mempolicy);
850 tsk->mempolicy = NULL;
853 BUG_ON(!(current->flags & PF_DEAD));
856 /* Avoid "noreturn function does return". */
860 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
868 EXPORT_SYMBOL(complete_and_exit);
870 asmlinkage long sys_exit(int error_code)
872 do_exit((error_code&0xff)<<8);
875 task_t fastcall *next_thread(const task_t *p)
880 if (!spin_is_locked(&p->sighand->siglock) &&
881 !rwlock_is_locked(&tasklist_lock))
884 return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
887 EXPORT_SYMBOL(next_thread);
890 * Take down every thread in the group. This is called by fatal signals
891 * as well as by sys_exit_group (below).
894 do_group_exit(int exit_code)
896 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
898 if (current->signal->group_exit)
899 exit_code = current->signal->group_exit_code;
900 else if (!thread_group_empty(current)) {
901 struct signal_struct *const sig = current->signal;
902 struct sighand_struct *const sighand = current->sighand;
903 read_lock(&tasklist_lock);
904 spin_lock_irq(&sighand->siglock);
906 /* Another thread got here before we took the lock. */
907 exit_code = sig->group_exit_code;
910 sig->group_exit_code = exit_code;
911 zap_other_threads(current);
913 spin_unlock_irq(&sighand->siglock);
914 read_unlock(&tasklist_lock);
922 * this kills every thread in the thread group. Note that any externally
923 * wait4()-ing process will get the correct exit code - even if this
924 * thread is not the thread group leader.
926 asmlinkage void sys_exit_group(int error_code)
928 do_group_exit((error_code & 0xff) << 8);
931 static int eligible_child(pid_t pid, int options, task_t *p)
937 if (process_group(p) != process_group(current))
939 } else if (pid != -1) {
940 if (process_group(p) != -pid)
945 * Do not consider detached threads that are
948 if (p->exit_signal == -1 && !p->ptrace)
951 /* Wait for all children (clone and not) if __WALL is set;
952 * otherwise, wait for clone children *only* if __WCLONE is
953 * set; otherwise, wait for non-clone children *only*. (Note:
954 * A "clone" child here is one that reports to its parent
955 * using a signal other than SIGCHLD.) */
956 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
957 && !(options & __WALL))
960 * Do not consider thread group leaders that are
961 * in a non-empty thread group:
963 if (current->tgid != p->tgid && delay_group_leader(p))
966 if (security_task_wait(p))
972 static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
974 struct siginfo __user *infop,
975 struct rusage __user *rusagep)
977 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
980 retval = put_user(SIGCHLD, &infop->si_signo);
982 retval = put_user(0, &infop->si_errno);
984 retval = put_user((short)why, &infop->si_code);
986 retval = put_user(pid, &infop->si_pid);
988 retval = put_user(uid, &infop->si_uid);
990 retval = put_user(status, &infop->si_status);
997 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
998 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
999 * the lock and this task is uninteresting. If we return nonzero, we have
1000 * released the lock and the system call should return.
1002 static int wait_task_zombie(task_t *p, int noreap,
1003 struct siginfo __user *infop,
1004 int __user *stat_addr, struct rusage __user *ru)
1006 unsigned long state;
1010 if (unlikely(noreap)) {
1013 int exit_code = p->exit_code;
1016 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1018 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1021 read_unlock(&tasklist_lock);
1022 if ((exit_code & 0x7f) == 0) {
1024 status = exit_code >> 8;
1026 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1027 status = exit_code & 0x7f;
1029 return wait_noreap_copyout(p, pid, uid, why,
1034 * Try to move the task's state to DEAD
1035 * only one thread is allowed to do this:
1037 state = xchg(&p->exit_state, EXIT_DEAD);
1038 if (state != EXIT_ZOMBIE) {
1039 BUG_ON(state != EXIT_DEAD);
1042 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1044 * This can only happen in a race with a ptraced thread
1045 * dying on another processor.
1050 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
1052 * The resource counters for the group leader are in its
1053 * own task_struct. Those for dead threads in the group
1054 * are in its signal_struct, as are those for the child
1055 * processes it has previously reaped. All these
1056 * accumulate in the parent's signal_struct c* fields.
1058 * We don't bother to take a lock here to protect these
1059 * p->signal fields, because they are only touched by
1060 * __exit_signal, which runs with tasklist_lock
1061 * write-locked anyway, and so is excluded here. We do
1062 * need to protect the access to p->parent->signal fields,
1063 * as other threads in the parent group can be right
1064 * here reaping other children at the same time.
1066 spin_lock_irq(&p->parent->sighand->siglock);
1067 p->parent->signal->cutime +=
1068 p->utime + p->signal->utime + p->signal->cutime;
1069 p->parent->signal->cstime +=
1070 p->stime + p->signal->stime + p->signal->cstime;
1071 p->parent->signal->cmin_flt +=
1072 p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
1073 p->parent->signal->cmaj_flt +=
1074 p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
1075 p->parent->signal->cnvcsw +=
1076 p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
1077 p->parent->signal->cnivcsw +=
1078 p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
1079 spin_unlock_irq(&p->parent->sighand->siglock);
1083 * Now we are sure this task is interesting, and no other
1084 * thread can reap it because we set its state to EXIT_DEAD.
1086 read_unlock(&tasklist_lock);
1088 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1089 status = p->signal->group_exit
1090 ? p->signal->group_exit_code : p->exit_code;
1091 if (!retval && stat_addr)
1092 retval = put_user(status, stat_addr);
1093 if (!retval && infop)
1094 retval = put_user(SIGCHLD, &infop->si_signo);
1095 if (!retval && infop)
1096 retval = put_user(0, &infop->si_errno);
1097 if (!retval && infop) {
1100 if ((status & 0x7f) == 0) {
1104 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1107 retval = put_user((short)why, &infop->si_code);
1109 retval = put_user(status, &infop->si_status);
1111 if (!retval && infop)
1112 retval = put_user(p->pid, &infop->si_pid);
1113 if (!retval && infop)
1114 retval = put_user(p->uid, &infop->si_uid);
1116 // TODO: is this safe?
1117 p->exit_state = EXIT_ZOMBIE;
1121 if (p->real_parent != p->parent) {
1122 write_lock_irq(&tasklist_lock);
1123 /* Double-check with lock held. */
1124 if (p->real_parent != p->parent) {
1126 // TODO: is this safe?
1127 p->exit_state = EXIT_ZOMBIE;
1129 * If this is not a detached task, notify the parent.
1130 * If it's still not detached after that, don't release
1133 if (p->exit_signal != -1) {
1134 do_notify_parent(p, p->exit_signal);
1135 if (p->exit_signal != -1)
1139 write_unlock_irq(&tasklist_lock);
1148 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1149 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1150 * the lock and this task is uninteresting. If we return nonzero, we have
1151 * released the lock and the system call should return.
1153 static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
1154 struct siginfo __user *infop,
1155 int __user *stat_addr, struct rusage __user *ru)
1157 int retval, exit_code;
1161 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1162 p->signal && p->signal->group_stop_count > 0)
1164 * A group stop is in progress and this is the group leader.
1165 * We won't report until all threads have stopped.
1170 * Now we are pretty sure this task is interesting.
1171 * Make sure it doesn't get reaped out from under us while we
1172 * give up the lock and then examine it below. We don't want to
1173 * keep holding onto the tasklist_lock while we call getrusage and
1174 * possibly take page faults for user memory.
1177 read_unlock(&tasklist_lock);
1179 if (unlikely(noreap)) {
1182 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1184 exit_code = p->exit_code;
1185 if (unlikely(!exit_code) ||
1186 unlikely(p->state > TASK_STOPPED))
1188 return wait_noreap_copyout(p, pid, uid,
1189 why, (exit_code << 8) | 0x7f,
1193 write_lock_irq(&tasklist_lock);
1196 * This uses xchg to be atomic with the thread resuming and setting
1197 * it. It must also be done with the write lock held to prevent a
1198 * race with the EXIT_ZOMBIE case.
1200 exit_code = xchg(&p->exit_code, 0);
1201 if (unlikely(p->exit_state >= EXIT_ZOMBIE)) {
1203 * The task resumed and then died. Let the next iteration
1204 * catch it in EXIT_ZOMBIE. Note that exit_code might
1205 * already be zero here if it resumed and did _exit(0).
1206 * The task itself is dead and won't touch exit_code again;
1207 * other processors in this function are locked out.
1209 p->exit_code = exit_code;
1212 if (unlikely(exit_code == 0)) {
1214 * Another thread in this function got to it first, or it
1215 * resumed, or it resumed and then died.
1217 write_unlock_irq(&tasklist_lock);
1221 * We are returning to the wait loop without having successfully
1222 * removed the process and having released the lock. We cannot
1223 * continue, since the "p" task pointer is potentially stale.
1225 * Return -EAGAIN, and do_wait() will restart the loop from the
1226 * beginning. Do _not_ re-acquire the lock.
1231 /* move to end of parent's list to avoid starvation */
1233 add_parent(p, p->parent);
1235 write_unlock_irq(&tasklist_lock);
1237 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1238 if (!retval && stat_addr)
1239 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1240 if (!retval && infop)
1241 retval = put_user(SIGCHLD, &infop->si_signo);
1242 if (!retval && infop)
1243 retval = put_user(0, &infop->si_errno);
1244 if (!retval && infop)
1245 retval = put_user((short)((p->ptrace & PT_PTRACED)
1246 ? CLD_TRAPPED : CLD_STOPPED),
1248 if (!retval && infop)
1249 retval = put_user(exit_code, &infop->si_status);
1250 if (!retval && infop)
1251 retval = put_user(p->pid, &infop->si_pid);
1252 if (!retval && infop)
1253 retval = put_user(p->uid, &infop->si_uid);
1263 * Handle do_wait work for one task in a live, non-stopped state.
1264 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1265 * the lock and this task is uninteresting. If we return nonzero, we have
1266 * released the lock and the system call should return.
1268 static int wait_task_continued(task_t *p, int noreap,
1269 struct siginfo __user *infop,
1270 int __user *stat_addr, struct rusage __user *ru)
1276 if (unlikely(!p->signal))
1279 if (p->signal->stop_state >= 0)
1282 spin_lock_irq(&p->sighand->siglock);
1283 if (p->signal->stop_state >= 0) { /* Re-check with the lock held. */
1284 spin_unlock_irq(&p->sighand->siglock);
1288 p->signal->stop_state = 0;
1289 spin_unlock_irq(&p->sighand->siglock);
1294 read_unlock(&tasklist_lock);
1297 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1299 if (!retval && stat_addr)
1300 retval = put_user(0xffff, stat_addr);
1304 retval = wait_noreap_copyout(p, pid, uid,
1305 CLD_CONTINUED, SIGCONT,
1307 BUG_ON(retval == 0);
1314 static inline int my_ptrace_child(struct task_struct *p)
1316 if (!(p->ptrace & PT_PTRACED))
1318 if (!(p->ptrace & PT_ATTACHED))
1321 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1322 * we are the attacher. If we are the real parent, this is a race
1323 * inside ptrace_attach. It is waiting for the tasklist_lock,
1324 * which we have to switch the parent links, but has already set
1325 * the flags in p->ptrace.
1327 return (p->parent != p->real_parent);
1330 static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1331 int __user *stat_addr, struct rusage __user *ru)
1333 DECLARE_WAITQUEUE(wait, current);
1334 struct task_struct *tsk;
1337 add_wait_queue(¤t->wait_chldexit,&wait);
1340 * We will set this flag if we see any child that might later
1341 * match our criteria, even if we are not able to reap it yet.
1344 current->state = TASK_INTERRUPTIBLE;
1345 read_lock(&tasklist_lock);
1348 struct task_struct *p;
1349 struct list_head *_p;
1352 list_for_each(_p,&tsk->children) {
1353 p = list_entry(_p,struct task_struct,sibling);
1355 ret = eligible_child(pid, options, p);
1361 if (!my_ptrace_child(p))
1366 * It's stopped now, so it might later
1367 * continue, exit, or stop again.
1370 if (!(options & WUNTRACED) &&
1371 !my_ptrace_child(p))
1373 retval = wait_task_stopped(p, ret == 2,
1374 (options & WNOWAIT),
1377 if (retval == -EAGAIN)
1379 if (retval != 0) /* He released the lock. */
1384 if (p->exit_state == EXIT_DEAD)
1386 // case EXIT_ZOMBIE:
1387 if (p->exit_state == EXIT_ZOMBIE) {
1389 * Eligible but we cannot release
1393 goto check_continued;
1394 if (!likely(options & WEXITED))
1396 retval = wait_task_zombie(
1397 p, (options & WNOWAIT),
1398 infop, stat_addr, ru);
1399 /* He released the lock. */
1406 * It's running now, so it might later
1407 * exit, stop, or stop and then continue.
1410 if (!unlikely(options & WCONTINUED))
1413 retval = wait_task_continued(
1414 p, (options & WNOWAIT),
1415 infop, stat_addr, ru);
1416 if (retval != 0) /* He released the lock. */
1422 list_for_each(_p, &tsk->ptrace_children) {
1423 p = list_entry(_p, struct task_struct,
1425 if (!eligible_child(pid, options, p))
1431 if (options & __WNOTHREAD)
1433 tsk = next_thread(tsk);
1434 if (tsk->signal != current->signal)
1436 } while (tsk != current);
1438 read_unlock(&tasklist_lock);
1441 if (options & WNOHANG)
1443 retval = -ERESTARTSYS;
1444 if (signal_pending(current))
1451 current->state = TASK_RUNNING;
1452 remove_wait_queue(¤t->wait_chldexit,&wait);
1458 * For a WNOHANG return, clear out all the fields
1459 * we would set so the user can easily tell the
1463 retval = put_user(0, &infop->si_signo);
1465 retval = put_user(0, &infop->si_errno);
1467 retval = put_user(0, &infop->si_code);
1469 retval = put_user(0, &infop->si_pid);
1471 retval = put_user(0, &infop->si_uid);
1473 retval = put_user(0, &infop->si_status);
1479 asmlinkage long sys_waitid(int which, pid_t pid,
1480 struct siginfo __user *infop, int options,
1481 struct rusage __user *ru)
1485 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1487 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1507 ret = do_wait(pid, options, infop, NULL, ru);
1509 /* avoid REGPARM breakage on x86: */
1510 prevent_tail_call(ret);
1514 asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1515 int options, struct rusage __user *ru)
1519 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1520 __WNOTHREAD|__WCLONE|__WALL))
1522 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1524 /* avoid REGPARM breakage on x86: */
1525 prevent_tail_call(ret);
1529 #ifdef __ARCH_WANT_SYS_WAITPID
1532 * sys_waitpid() remains for compatibility. waitpid() should be
1533 * implemented by calling sys_wait4() from libc.a.
1535 asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1537 return sys_wait4(pid, stat_addr, options, NULL);