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/ckrm_events.h>
29 #include <linux/ckrm_tsk.h>
30 #include <linux/ckrm_mem_inline.h>
31 #include <linux/syscalls.h>
32 #include <linux/vs_limit.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 __unhash_process(struct task_struct *p)
47 /* tasklist_lock is held, is this sufficient? */
49 atomic_dec(&p->vx_info->cvirt.nr_threads);
52 detach_pid(p, PIDTYPE_PID);
53 detach_pid(p, PIDTYPE_TGID);
54 if (thread_group_leader(p)) {
55 detach_pid(p, PIDTYPE_PGID);
56 detach_pid(p, PIDTYPE_SID);
58 __get_cpu_var(process_counts)--;
64 void release_task(struct task_struct * p)
68 struct dentry *proc_dentry;
71 atomic_dec(&p->user->processes);
72 spin_lock(&p->proc_lock);
73 proc_dentry = proc_pid_unhash(p);
74 write_lock_irq(&tasklist_lock);
75 if (unlikely(p->ptrace))
77 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
83 * If we are the last non-leader member of the thread
84 * group, and the leader is zombie, then notify the
85 * group leader's parent process. (if it wants notification.)
88 leader = p->group_leader;
89 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
90 BUG_ON(leader->exit_signal == -1);
91 do_notify_parent(leader, leader->exit_signal);
93 * If we were the last child thread and the leader has
94 * exited already, and the leader's parent ignores SIGCHLD,
95 * then we are the one who should release the leader.
97 * do_notify_parent() will have marked it self-reaping in
100 zap_leader = (leader->exit_signal == -1);
104 write_unlock_irq(&tasklist_lock);
105 spin_unlock(&p->proc_lock);
106 proc_pid_flush(proc_dentry);
111 if (unlikely(zap_leader))
115 /* we are using it only for SMP init */
117 void unhash_process(struct task_struct *p)
119 struct dentry *proc_dentry;
121 spin_lock(&p->proc_lock);
122 proc_dentry = proc_pid_unhash(p);
123 write_lock_irq(&tasklist_lock);
125 write_unlock_irq(&tasklist_lock);
126 spin_unlock(&p->proc_lock);
127 proc_pid_flush(proc_dentry);
131 * This checks not only the pgrp, but falls back on the pid if no
132 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
135 int session_of_pgrp(int pgrp)
137 struct task_struct *p;
140 read_lock(&tasklist_lock);
141 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
142 if (p->signal->session > 0) {
143 sid = p->signal->session;
146 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
147 p = find_task_by_pid(pgrp);
149 sid = p->signal->session;
151 read_unlock(&tasklist_lock);
157 * Determine if a process group is "orphaned", according to the POSIX
158 * definition in 2.2.2.52. Orphaned process groups are not to be affected
159 * by terminal-generated stop signals. Newly orphaned process groups are
160 * to receive a SIGHUP and a SIGCONT.
162 * "I ask you, have you ever known what it is to be an orphan?"
164 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
166 struct task_struct *p;
169 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
170 if (p == ignored_task
171 || p->exit_state >= EXIT_ZOMBIE
172 || p->real_parent->pid == 1)
174 if (process_group(p->real_parent) != pgrp
175 && p->real_parent->signal->session == p->signal->session) {
179 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
180 return ret; /* (sighing) "Often!" */
183 int is_orphaned_pgrp(int pgrp)
187 read_lock(&tasklist_lock);
188 retval = will_become_orphaned_pgrp(pgrp, NULL);
189 read_unlock(&tasklist_lock);
194 static inline int has_stopped_jobs(int pgrp)
197 struct task_struct *p;
199 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
200 if (p->state != TASK_STOPPED)
203 /* If p is stopped by a debugger on a signal that won't
204 stop it, then don't count p as stopped. This isn't
205 perfect but it's a good approximation. */
206 if (unlikely (p->ptrace)
207 && p->exit_code != SIGSTOP
208 && p->exit_code != SIGTSTP
209 && p->exit_code != SIGTTOU
210 && p->exit_code != SIGTTIN)
215 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
220 * reparent_to_init() - Reparent the calling kernel thread to the init task.
222 * If a kernel thread is launched as a result of a system call, or if
223 * it ever exits, it should generally reparent itself to init so that
224 * it is correctly cleaned up on exit.
226 * The various task state such as scheduling policy and priority may have
227 * been inherited from a user process, so we reset them to sane values here.
229 * NOTE that reparent_to_init() gives the caller full capabilities.
231 void reparent_to_init(void)
233 write_lock_irq(&tasklist_lock);
235 ptrace_unlink(current);
236 /* Reparent to init */
237 REMOVE_LINKS(current);
238 /* FIXME handle vchild_reaper/initpid */
239 current->parent = child_reaper;
240 current->real_parent = child_reaper;
243 /* Set the exit signal to SIGCHLD so we signal init on exit */
244 current->exit_signal = SIGCHLD;
246 if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
247 set_user_nice(current, 0);
251 security_task_reparent_to_init(current);
252 memcpy(current->signal->rlim, init_task.signal->rlim,
253 sizeof(current->signal->rlim));
254 atomic_inc(&(INIT_USER->__count));
255 write_unlock_irq(&tasklist_lock);
256 switch_uid(INIT_USER);
259 void __set_special_pids(pid_t session, pid_t pgrp)
261 struct task_struct *curr = current;
263 if (curr->signal->session != session) {
264 detach_pid(curr, PIDTYPE_SID);
265 curr->signal->session = session;
266 attach_pid(curr, PIDTYPE_SID, session);
268 if (process_group(curr) != pgrp) {
269 detach_pid(curr, PIDTYPE_PGID);
270 curr->signal->pgrp = pgrp;
271 attach_pid(curr, PIDTYPE_PGID, pgrp);
275 void set_special_pids(pid_t session, pid_t pgrp)
277 write_lock_irq(&tasklist_lock);
278 __set_special_pids(session, pgrp);
279 write_unlock_irq(&tasklist_lock);
283 * Let kernel threads use this to say that they
284 * allow a certain signal (since daemonize() will
285 * have disabled all of them by default).
287 int allow_signal(int sig)
289 if (sig < 1 || sig > _NSIG)
292 spin_lock_irq(¤t->sighand->siglock);
293 sigdelset(¤t->blocked, sig);
295 /* Kernel threads handle their own signals.
296 Let the signal code know it'll be handled, so
297 that they don't get converted to SIGKILL or
298 just silently dropped */
299 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
302 spin_unlock_irq(¤t->sighand->siglock);
306 EXPORT_SYMBOL(allow_signal);
308 int disallow_signal(int sig)
310 if (sig < 1 || sig > _NSIG)
313 spin_lock_irq(¤t->sighand->siglock);
314 sigaddset(¤t->blocked, sig);
316 spin_unlock_irq(¤t->sighand->siglock);
320 EXPORT_SYMBOL(disallow_signal);
323 * Put all the gunge required to become a kernel thread without
324 * attached user resources in one place where it belongs.
327 void daemonize(const char *name, ...)
330 struct fs_struct *fs;
333 va_start(args, name);
334 vsnprintf(current->comm, sizeof(current->comm), name, args);
338 * If we were started as result of loading a module, close all of the
339 * user space pages. We don't need them, and if we didn't close them
340 * they would be locked into memory.
344 set_special_pids(1, 1);
346 current->signal->tty = NULL;
349 /* Block and flush all signals */
350 sigfillset(&blocked);
351 sigprocmask(SIG_BLOCK, &blocked, NULL);
352 flush_signals(current);
354 /* Become as one with the init task */
356 exit_fs(current); /* current->fs->count--; */
359 atomic_inc(&fs->count);
361 current->files = init_task.files;
362 atomic_inc(¤t->files->count);
367 EXPORT_SYMBOL(daemonize);
369 static inline void close_files(struct files_struct * files)
377 if (i >= files->max_fdset || i >= files->max_fds)
379 set = files->open_fds->fds_bits[j++];
382 struct file * file = xchg(&files->fd[i], NULL);
384 filp_close(file, files);
393 struct files_struct *get_files_struct(struct task_struct *task)
395 struct files_struct *files;
400 atomic_inc(&files->count);
406 void fastcall put_files_struct(struct files_struct *files)
408 if (atomic_dec_and_test(&files->count)) {
411 * Free the fd and fdset arrays if we expanded them.
413 if (files->fd != &files->fd_array[0])
414 free_fd_array(files->fd, files->max_fds);
415 if (files->max_fdset > __FD_SETSIZE) {
416 free_fdset(files->open_fds, files->max_fdset);
417 free_fdset(files->close_on_exec, files->max_fdset);
419 kmem_cache_free(files_cachep, files);
423 EXPORT_SYMBOL(put_files_struct);
425 static inline void __exit_files(struct task_struct *tsk)
427 struct files_struct * files = tsk->files;
433 put_files_struct(files);
437 void exit_files(struct task_struct *tsk)
442 static inline void __put_fs_struct(struct fs_struct *fs)
444 /* No need to hold fs->lock if we are killing it */
445 if (atomic_dec_and_test(&fs->count)) {
452 mntput(fs->altrootmnt);
454 kmem_cache_free(fs_cachep, fs);
458 void put_fs_struct(struct fs_struct *fs)
463 static inline void __exit_fs(struct task_struct *tsk)
465 struct fs_struct * fs = tsk->fs;
475 void exit_fs(struct task_struct *tsk)
480 EXPORT_SYMBOL_GPL(exit_fs);
483 * Turn us into a lazy TLB process if we
486 static inline void __exit_mm(struct task_struct * tsk)
488 struct mm_struct *mm = tsk->mm;
494 * Serialize with any possible pending coredump.
495 * We must hold mmap_sem around checking core_waiters
496 * and clearing tsk->mm. The core-inducing thread
497 * will increment core_waiters for each thread in the
498 * group with ->mm != NULL.
500 down_read(&mm->mmap_sem);
501 if (mm->core_waiters) {
502 up_read(&mm->mmap_sem);
503 down_write(&mm->mmap_sem);
504 if (!--mm->core_waiters)
505 complete(mm->core_startup_done);
506 up_write(&mm->mmap_sem);
508 wait_for_completion(&mm->core_done);
509 down_read(&mm->mmap_sem);
511 atomic_inc(&mm->mm_count);
512 if (mm != tsk->active_mm) BUG();
513 /* more a memory barrier than a real lock */
516 up_read(&mm->mmap_sem);
517 ckrm_task_clear_mm(tsk, mm);
518 enter_lazy_tlb(mm, current);
523 void exit_mm(struct task_struct *tsk)
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->state >= EXIT_ZOMBIE || reaper->exit_state >= EXIT_ZOMBIE);
535 p->real_parent = reaper;
536 if (p->parent == p->real_parent)
540 static inline void reparent_thread(task_t *p, task_t *father, int traced)
542 /* We don't want people slaying init. */
543 if (p->exit_signal != -1)
544 p->exit_signal = SIGCHLD;
546 if (p->pdeath_signal)
547 /* We already hold the tasklist_lock here. */
548 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
550 /* Move the child from its dying parent to the new one. */
551 if (unlikely(traced)) {
552 /* Preserve ptrace links if someone else is tracing this child. */
553 list_del_init(&p->ptrace_list);
554 if (p->parent != p->real_parent)
555 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
557 /* If this child is being traced, then we're the one tracing it
558 * anyway, so let go of it.
561 list_del_init(&p->sibling);
562 p->parent = p->real_parent;
563 list_add_tail(&p->sibling, &p->parent->children);
565 /* If we'd notified the old parent about this child's death,
566 * also notify the new parent.
568 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
569 thread_group_empty(p))
570 do_notify_parent(p, p->exit_signal);
571 else if (p->state == TASK_TRACED) {
573 * If it was at a trace stop, turn it into
574 * a normal stop since it's no longer being
582 * process group orphan check
583 * Case ii: Our child is in a different pgrp
584 * than we are, and it was the only connection
585 * outside, so the child pgrp is now orphaned.
587 if ((process_group(p) != process_group(father)) &&
588 (p->signal->session == father->signal->session)) {
589 int pgrp = process_group(p);
591 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
592 __kill_pg_info(SIGHUP, (void *)1, pgrp);
593 __kill_pg_info(SIGCONT, (void *)1, pgrp);
599 * When we die, we re-parent all our children.
600 * Try to give them to another thread in our thread
601 * group, and if no such member exists, give it to
602 * the global child reaper process (ie "init")
604 static inline void forget_original_parent(struct task_struct * father,
605 struct list_head *to_release)
607 struct task_struct *p, *reaper = father;
608 struct list_head *_p, *_n;
610 /* FIXME handle vchild_reaper/initpid */
612 reaper = next_thread(reaper);
613 if (reaper == father) {
614 reaper = child_reaper;
617 } while (reaper->exit_state >= EXIT_ZOMBIE);
620 * There are only two places where our children can be:
622 * - in our child list
623 * - in our ptraced child list
625 * Search them and reparent children.
627 list_for_each_safe(_p, _n, &father->children) {
629 p = list_entry(_p,struct task_struct,sibling);
633 /* if father isn't the real parent, then ptrace must be enabled */
634 BUG_ON(father != p->real_parent && !ptrace);
636 if (father == p->real_parent) {
637 /* reparent with a reaper, real father it's us */
638 choose_new_parent(p, reaper, child_reaper);
639 reparent_thread(p, father, 0);
641 /* reparent ptraced task to its real parent */
643 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
644 thread_group_empty(p))
645 do_notify_parent(p, p->exit_signal);
649 * if the ptraced child is a zombie with exit_signal == -1
650 * we must collect it before we exit, or it will remain
651 * zombie forever since we prevented it from self-reap itself
652 * while it was being traced by us, to be able to see it in wait4.
654 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
655 list_add(&p->ptrace_list, to_release);
657 list_for_each_safe(_p, _n, &father->ptrace_children) {
658 p = list_entry(_p,struct task_struct,ptrace_list);
659 choose_new_parent(p, reaper, child_reaper);
660 reparent_thread(p, father, 1);
665 * Send signals to all our closest relatives so that they know
666 * to properly mourn us..
668 static void exit_notify(struct task_struct *tsk)
671 struct task_struct *t;
672 struct list_head ptrace_dead, *_p, *_n;
676 if (signal_pending(tsk) && !tsk->signal->group_exit
677 && !thread_group_empty(tsk)) {
679 * This occurs when there was a race between our exit
680 * syscall and a group signal choosing us as the one to
681 * wake up. It could be that we are the only thread
682 * alerted to check for pending signals, but another thread
683 * should be woken now to take the signal since we will not.
684 * Now we'll wake all the threads in the group just to make
685 * sure someone gets all the pending signals.
687 read_lock(&tasklist_lock);
688 spin_lock_irq(&tsk->sighand->siglock);
689 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
690 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
691 recalc_sigpending_tsk(t);
692 if (signal_pending(t))
693 signal_wake_up(t, 0);
695 spin_unlock_irq(&tsk->sighand->siglock);
696 read_unlock(&tasklist_lock);
699 write_lock_irq(&tasklist_lock);
702 * This does two things:
704 * A. Make init inherit all the child processes
705 * B. Check to see if any process groups have become orphaned
706 * as a result of our exiting, and if they have any stopped
707 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
710 INIT_LIST_HEAD(&ptrace_dead);
711 forget_original_parent(tsk, &ptrace_dead);
712 BUG_ON(!list_empty(&tsk->children));
713 BUG_ON(!list_empty(&tsk->ptrace_children));
716 * Check to see if any process groups have become orphaned
717 * as a result of our exiting, and if they have any stopped
718 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
720 * Case i: Our father is in a different pgrp than we are
721 * and we were the only connection outside, so our pgrp
722 * is about to become orphaned.
725 t = tsk->real_parent;
727 if ((process_group(t) != process_group(tsk)) &&
728 (t->signal->session == tsk->signal->session) &&
729 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
730 has_stopped_jobs(process_group(tsk))) {
731 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
732 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
735 /* Let father know we died
737 * Thread signals are configurable, but you aren't going to use
738 * that to send signals to arbitary processes.
739 * That stops right now.
741 * If the parent exec id doesn't match the exec id we saved
742 * when we started then we know the parent has changed security
745 * If our self_exec id doesn't match our parent_exec_id then
746 * we have changed execution domain as these two values started
747 * the same after a fork.
751 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
752 ( tsk->parent_exec_id != t->self_exec_id ||
753 tsk->self_exec_id != tsk->parent_exec_id)
754 && !capable(CAP_KILL))
755 tsk->exit_signal = SIGCHLD;
758 /* If something other than our normal parent is ptracing us, then
759 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
760 * only has special meaning to our real parent.
762 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
763 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
764 do_notify_parent(tsk, signal);
765 } else if (tsk->ptrace) {
766 do_notify_parent(tsk, SIGCHLD);
770 if (tsk->exit_signal == -1 && tsk->ptrace == 0)
772 tsk->exit_state = state;
775 * Clear these here so that update_process_times() won't try to deliver
776 * itimer, profile or rlimit signals to this task while it is in late exit.
778 tsk->it_virt_value = 0;
779 tsk->it_prof_value = 0;
781 write_unlock_irq(&tasklist_lock);
783 list_for_each_safe(_p, _n, &ptrace_dead) {
785 t = list_entry(_p,struct task_struct,ptrace_list);
789 /* If the process is dead, release it - nobody will wait for it */
790 if (state == EXIT_DEAD)
793 /* PF_DEAD causes final put_task_struct after we schedule. */
795 tsk->flags |= PF_DEAD;
798 fastcall NORET_TYPE void do_exit(long code)
800 struct task_struct *tsk = current;
803 profile_task_exit(tsk);
805 if (unlikely(in_interrupt()))
806 panic("Aiee, killing interrupt handler!");
807 if (unlikely(!tsk->pid))
808 panic("Attempted to kill the idle task!");
809 if (unlikely(tsk->pid == 1))
810 panic("Attempted to kill init!");
813 tsk->flags |= PF_EXITING;
814 del_timer_sync(&tsk->real_timer);
816 if (unlikely(in_atomic()))
817 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
818 current->comm, current->pid,
821 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
822 current->ptrace_message = code;
823 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
826 group_dead = atomic_dec_and_test(&tsk->signal->live);
829 if (current->tux_info) {
830 #ifdef CONFIG_TUX_DEBUG
831 printk("Possibly unexpected TUX-thread exit(%ld) at %p?\n",
832 code, __builtin_return_address(0));
845 if (group_dead && tsk->signal->leader)
846 disassociate_ctty(1);
848 module_put(tsk->thread_info->exec_domain->module);
850 module_put(tsk->binfmt->module);
852 tsk->exit_code = code;
855 mpol_free(tsk->mempolicy);
856 tsk->mempolicy = NULL;
859 BUG_ON(!(current->flags & PF_DEAD));
862 /* Avoid "noreturn function does return". */
866 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
874 EXPORT_SYMBOL(complete_and_exit);
876 asmlinkage long sys_exit(int error_code)
878 do_exit((error_code&0xff)<<8);
881 task_t fastcall *next_thread(const task_t *p)
886 if (!spin_is_locked(&p->sighand->siglock) &&
887 !rwlock_is_locked(&tasklist_lock))
890 return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
893 EXPORT_SYMBOL(next_thread);
896 * Take down every thread in the group. This is called by fatal signals
897 * as well as by sys_exit_group (below).
900 do_group_exit(int exit_code)
902 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
904 if (current->signal->group_exit)
905 exit_code = current->signal->group_exit_code;
906 else if (!thread_group_empty(current)) {
907 struct signal_struct *const sig = current->signal;
908 struct sighand_struct *const sighand = current->sighand;
909 read_lock(&tasklist_lock);
910 spin_lock_irq(&sighand->siglock);
912 /* Another thread got here before we took the lock. */
913 exit_code = sig->group_exit_code;
916 sig->group_exit_code = exit_code;
917 zap_other_threads(current);
919 spin_unlock_irq(&sighand->siglock);
920 read_unlock(&tasklist_lock);
928 * this kills every thread in the thread group. Note that any externally
929 * wait4()-ing process will get the correct exit code - even if this
930 * thread is not the thread group leader.
932 asmlinkage void sys_exit_group(int error_code)
934 do_group_exit((error_code & 0xff) << 8);
937 static int eligible_child(pid_t pid, int options, task_t *p)
943 if (process_group(p) != process_group(current))
945 } else if (pid != -1) {
946 if (process_group(p) != -pid)
951 * Do not consider detached threads that are
954 if (p->exit_signal == -1 && !p->ptrace)
957 /* Wait for all children (clone and not) if __WALL is set;
958 * otherwise, wait for clone children *only* if __WCLONE is
959 * set; otherwise, wait for non-clone children *only*. (Note:
960 * A "clone" child here is one that reports to its parent
961 * using a signal other than SIGCHLD.) */
962 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
963 && !(options & __WALL))
966 * Do not consider thread group leaders that are
967 * in a non-empty thread group:
969 if (current->tgid != p->tgid && delay_group_leader(p))
972 if (security_task_wait(p))
978 static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
980 struct siginfo __user *infop,
981 struct rusage __user *rusagep)
983 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
986 retval = put_user(SIGCHLD, &infop->si_signo);
988 retval = put_user(0, &infop->si_errno);
990 retval = put_user((short)why, &infop->si_code);
992 retval = put_user(pid, &infop->si_pid);
994 retval = put_user(uid, &infop->si_uid);
996 retval = put_user(status, &infop->si_status);
1003 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1004 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1005 * the lock and this task is uninteresting. If we return nonzero, we have
1006 * released the lock and the system call should return.
1008 static int wait_task_zombie(task_t *p, int noreap,
1009 struct siginfo __user *infop,
1010 int __user *stat_addr, struct rusage __user *ru)
1012 unsigned long state;
1016 if (unlikely(noreap)) {
1019 int exit_code = p->exit_code;
1022 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1024 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1027 read_unlock(&tasklist_lock);
1028 if ((exit_code & 0x7f) == 0) {
1030 status = exit_code >> 8;
1032 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1033 status = exit_code & 0x7f;
1035 return wait_noreap_copyout(p, pid, uid, why,
1040 * Try to move the task's state to DEAD
1041 * only one thread is allowed to do this:
1043 state = xchg(&p->exit_state, EXIT_DEAD);
1044 if (state != EXIT_ZOMBIE) {
1045 BUG_ON(state != EXIT_DEAD);
1048 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1050 * This can only happen in a race with a ptraced thread
1051 * dying on another processor.
1056 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
1058 * The resource counters for the group leader are in its
1059 * own task_struct. Those for dead threads in the group
1060 * are in its signal_struct, as are those for the child
1061 * processes it has previously reaped. All these
1062 * accumulate in the parent's signal_struct c* fields.
1064 * We don't bother to take a lock here to protect these
1065 * p->signal fields, because they are only touched by
1066 * __exit_signal, which runs with tasklist_lock
1067 * write-locked anyway, and so is excluded here. We do
1068 * need to protect the access to p->parent->signal fields,
1069 * as other threads in the parent group can be right
1070 * here reaping other children at the same time.
1072 spin_lock_irq(&p->parent->sighand->siglock);
1073 p->parent->signal->cutime +=
1074 p->utime + p->signal->utime + p->signal->cutime;
1075 p->parent->signal->cstime +=
1076 p->stime + p->signal->stime + p->signal->cstime;
1077 p->parent->signal->cmin_flt +=
1078 p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
1079 p->parent->signal->cmaj_flt +=
1080 p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
1081 p->parent->signal->cnvcsw +=
1082 p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
1083 p->parent->signal->cnivcsw +=
1084 p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
1085 spin_unlock_irq(&p->parent->sighand->siglock);
1089 * Now we are sure this task is interesting, and no other
1090 * thread can reap it because we set its state to EXIT_DEAD.
1092 read_unlock(&tasklist_lock);
1094 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1095 status = p->signal->group_exit
1096 ? p->signal->group_exit_code : p->exit_code;
1097 if (!retval && stat_addr)
1098 retval = put_user(status, stat_addr);
1099 if (!retval && infop)
1100 retval = put_user(SIGCHLD, &infop->si_signo);
1101 if (!retval && infop)
1102 retval = put_user(0, &infop->si_errno);
1103 if (!retval && infop) {
1106 if ((status & 0x7f) == 0) {
1110 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1113 retval = put_user((short)why, &infop->si_code);
1115 retval = put_user(status, &infop->si_status);
1117 if (!retval && infop)
1118 retval = put_user(p->pid, &infop->si_pid);
1119 if (!retval && infop)
1120 retval = put_user(p->uid, &infop->si_uid);
1122 // TODO: is this safe?
1123 p->exit_state = EXIT_ZOMBIE;
1127 if (p->real_parent != p->parent) {
1128 write_lock_irq(&tasklist_lock);
1129 /* Double-check with lock held. */
1130 if (p->real_parent != p->parent) {
1132 // TODO: is this safe?
1133 p->exit_state = EXIT_ZOMBIE;
1135 * If this is not a detached task, notify the parent.
1136 * If it's still not detached after that, don't release
1139 if (p->exit_signal != -1) {
1140 do_notify_parent(p, p->exit_signal);
1141 if (p->exit_signal != -1)
1145 write_unlock_irq(&tasklist_lock);
1154 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1155 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1156 * the lock and this task is uninteresting. If we return nonzero, we have
1157 * released the lock and the system call should return.
1159 static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
1160 struct siginfo __user *infop,
1161 int __user *stat_addr, struct rusage __user *ru)
1163 int retval, exit_code;
1167 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1168 p->signal && p->signal->group_stop_count > 0)
1170 * A group stop is in progress and this is the group leader.
1171 * We won't report until all threads have stopped.
1176 * Now we are pretty sure this task is interesting.
1177 * Make sure it doesn't get reaped out from under us while we
1178 * give up the lock and then examine it below. We don't want to
1179 * keep holding onto the tasklist_lock while we call getrusage and
1180 * possibly take page faults for user memory.
1183 read_unlock(&tasklist_lock);
1185 if (unlikely(noreap)) {
1188 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1190 exit_code = p->exit_code;
1191 if (unlikely(!exit_code) ||
1192 unlikely(p->state > TASK_STOPPED))
1194 return wait_noreap_copyout(p, pid, uid,
1195 why, (exit_code << 8) | 0x7f,
1199 write_lock_irq(&tasklist_lock);
1202 * This uses xchg to be atomic with the thread resuming and setting
1203 * it. It must also be done with the write lock held to prevent a
1204 * race with the EXIT_ZOMBIE case.
1206 exit_code = xchg(&p->exit_code, 0);
1207 if (unlikely(p->exit_state >= EXIT_ZOMBIE)) {
1209 * The task resumed and then died. Let the next iteration
1210 * catch it in EXIT_ZOMBIE. Note that exit_code might
1211 * already be zero here if it resumed and did _exit(0).
1212 * The task itself is dead and won't touch exit_code again;
1213 * other processors in this function are locked out.
1215 p->exit_code = exit_code;
1218 if (unlikely(exit_code == 0)) {
1220 * Another thread in this function got to it first, or it
1221 * resumed, or it resumed and then died.
1223 write_unlock_irq(&tasklist_lock);
1227 * We are returning to the wait loop without having successfully
1228 * removed the process and having released the lock. We cannot
1229 * continue, since the "p" task pointer is potentially stale.
1231 * Return -EAGAIN, and do_wait() will restart the loop from the
1232 * beginning. Do _not_ re-acquire the lock.
1237 /* move to end of parent's list to avoid starvation */
1239 add_parent(p, p->parent);
1241 write_unlock_irq(&tasklist_lock);
1243 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1244 if (!retval && stat_addr)
1245 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1246 if (!retval && infop)
1247 retval = put_user(SIGCHLD, &infop->si_signo);
1248 if (!retval && infop)
1249 retval = put_user(0, &infop->si_errno);
1250 if (!retval && infop)
1251 retval = put_user((short)((p->ptrace & PT_PTRACED)
1252 ? CLD_TRAPPED : CLD_STOPPED),
1254 if (!retval && infop)
1255 retval = put_user(exit_code, &infop->si_status);
1256 if (!retval && infop)
1257 retval = put_user(p->pid, &infop->si_pid);
1258 if (!retval && infop)
1259 retval = put_user(p->uid, &infop->si_uid);
1269 * Handle do_wait work for one task in a live, non-stopped state.
1270 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1271 * the lock and this task is uninteresting. If we return nonzero, we have
1272 * released the lock and the system call should return.
1274 static int wait_task_continued(task_t *p, int noreap,
1275 struct siginfo __user *infop,
1276 int __user *stat_addr, struct rusage __user *ru)
1282 if (unlikely(!p->signal))
1285 if (p->signal->stop_state >= 0)
1288 spin_lock_irq(&p->sighand->siglock);
1289 if (p->signal->stop_state >= 0) { /* Re-check with the lock held. */
1290 spin_unlock_irq(&p->sighand->siglock);
1294 p->signal->stop_state = 0;
1295 spin_unlock_irq(&p->sighand->siglock);
1300 read_unlock(&tasklist_lock);
1303 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1305 if (!retval && stat_addr)
1306 retval = put_user(0xffff, stat_addr);
1310 retval = wait_noreap_copyout(p, pid, uid,
1311 CLD_CONTINUED, SIGCONT,
1313 BUG_ON(retval == 0);
1320 static inline int my_ptrace_child(struct task_struct *p)
1322 if (!(p->ptrace & PT_PTRACED))
1324 if (!(p->ptrace & PT_ATTACHED))
1327 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1328 * we are the attacher. If we are the real parent, this is a race
1329 * inside ptrace_attach. It is waiting for the tasklist_lock,
1330 * which we have to switch the parent links, but has already set
1331 * the flags in p->ptrace.
1333 return (p->parent != p->real_parent);
1336 static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1337 int __user *stat_addr, struct rusage __user *ru)
1339 DECLARE_WAITQUEUE(wait, current);
1340 struct task_struct *tsk;
1343 add_wait_queue(¤t->wait_chldexit,&wait);
1346 * We will set this flag if we see any child that might later
1347 * match our criteria, even if we are not able to reap it yet.
1350 current->state = TASK_INTERRUPTIBLE;
1351 read_lock(&tasklist_lock);
1354 struct task_struct *p;
1355 struct list_head *_p;
1358 list_for_each(_p,&tsk->children) {
1359 p = list_entry(_p,struct task_struct,sibling);
1361 ret = eligible_child(pid, options, p);
1367 if (!my_ptrace_child(p))
1372 * It's stopped now, so it might later
1373 * continue, exit, or stop again.
1376 if (!(options & WUNTRACED) &&
1377 !my_ptrace_child(p))
1379 retval = wait_task_stopped(p, ret == 2,
1380 (options & WNOWAIT),
1383 if (retval == -EAGAIN)
1385 if (retval != 0) /* He released the lock. */
1390 if (p->exit_state == EXIT_DEAD)
1392 // case EXIT_ZOMBIE:
1393 if (p->exit_state == EXIT_ZOMBIE) {
1395 * Eligible but we cannot release
1399 goto check_continued;
1400 if (!likely(options & WEXITED))
1402 retval = wait_task_zombie(
1403 p, (options & WNOWAIT),
1404 infop, stat_addr, ru);
1405 /* He released the lock. */
1412 * It's running now, so it might later
1413 * exit, stop, or stop and then continue.
1416 if (!unlikely(options & WCONTINUED))
1419 retval = wait_task_continued(
1420 p, (options & WNOWAIT),
1421 infop, stat_addr, ru);
1422 if (retval != 0) /* He released the lock. */
1428 list_for_each(_p, &tsk->ptrace_children) {
1429 p = list_entry(_p, struct task_struct,
1431 if (!eligible_child(pid, options, p))
1437 if (options & __WNOTHREAD)
1439 tsk = next_thread(tsk);
1440 if (tsk->signal != current->signal)
1442 } while (tsk != current);
1444 read_unlock(&tasklist_lock);
1447 if (options & WNOHANG)
1449 retval = -ERESTARTSYS;
1450 if (signal_pending(current))
1457 current->state = TASK_RUNNING;
1458 remove_wait_queue(¤t->wait_chldexit,&wait);
1464 * For a WNOHANG return, clear out all the fields
1465 * we would set so the user can easily tell the
1469 retval = put_user(0, &infop->si_signo);
1471 retval = put_user(0, &infop->si_errno);
1473 retval = put_user(0, &infop->si_code);
1475 retval = put_user(0, &infop->si_pid);
1477 retval = put_user(0, &infop->si_uid);
1479 retval = put_user(0, &infop->si_status);
1485 asmlinkage long sys_waitid(int which, pid_t pid,
1486 struct siginfo __user *infop, int options,
1487 struct rusage __user *ru)
1491 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1493 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1513 ret = do_wait(pid, options, infop, NULL, ru);
1515 /* avoid REGPARM breakage on x86: */
1516 prevent_tail_call(ret);
1520 asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1521 int options, struct rusage __user *ru)
1525 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1526 __WNOTHREAD|__WCLONE|__WALL))
1528 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1530 /* avoid REGPARM breakage on x86: */
1531 prevent_tail_call(ret);
1535 #ifdef __ARCH_WANT_SYS_WAITPID
1538 * sys_waitpid() remains for compatibility. waitpid() should be
1539 * implemented by calling sys_wait4() from libc.a.
1541 asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1543 return sys_wait4(pid, stat_addr, options, NULL);