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/security.h>
18 #include <linux/cpu.h>
19 #include <linux/acct.h>
20 #include <linux/file.h>
21 #include <linux/binfmts.h>
22 #include <linux/ptrace.h>
23 #include <linux/profile.h>
24 #include <linux/mount.h>
25 #include <linux/proc_fs.h>
26 #include <linux/mempolicy.h>
27 #include <linux/ckrm.h>
28 #include <linux/ckrm_tsk.h>
29 #include <linux/vs_limit.h>
30 #include <linux/ckrm_mem.h>
32 #include <asm/uaccess.h>
33 #include <asm/unistd.h>
34 #include <asm/pgtable.h>
35 #include <asm/mmu_context.h>
37 extern void sem_exit (void);
38 extern struct task_struct *child_reaper;
40 int getrusage(struct task_struct *, int, struct rusage __user *);
42 static void __unhash_process(struct task_struct *p)
45 /* tasklist_lock is held, is this sufficient? */
47 atomic_dec(&p->vx_info->cvirt.nr_threads);
50 detach_pid(p, PIDTYPE_PID);
51 detach_pid(p, PIDTYPE_TGID);
52 if (thread_group_leader(p)) {
53 detach_pid(p, PIDTYPE_PGID);
54 detach_pid(p, PIDTYPE_SID);
56 __get_cpu_var(process_counts)--;
62 void release_task(struct task_struct * p)
66 struct dentry *proc_dentry;
69 atomic_dec(&p->user->processes);
70 spin_lock(&p->proc_lock);
71 proc_dentry = proc_pid_unhash(p);
72 write_lock_irq(&tasklist_lock);
73 if (unlikely(p->ptrace))
75 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
81 * If we are the last non-leader member of the thread
82 * group, and the leader is zombie, then notify the
83 * group leader's parent process. (if it wants notification.)
86 leader = p->group_leader;
87 if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
88 BUG_ON(leader->exit_signal == -1);
89 do_notify_parent(leader, leader->exit_signal);
91 * If we were the last child thread and the leader has
92 * exited already, and the leader's parent ignores SIGCHLD,
93 * then we are the one who should release the leader.
95 * do_notify_parent() will have marked it self-reaping in
98 zap_leader = (leader->exit_signal == -1);
102 write_unlock_irq(&tasklist_lock);
103 spin_unlock(&p->proc_lock);
109 if (unlikely(zap_leader))
113 /* we are using it only for SMP init */
115 void unhash_process(struct task_struct *p)
117 struct dentry *proc_dentry;
119 spin_lock(&p->proc_lock);
120 proc_dentry = proc_pid_unhash(p);
121 write_lock_irq(&tasklist_lock);
123 write_unlock_irq(&tasklist_lock);
124 spin_unlock(&p->proc_lock);
125 proc_pid_flush(proc_dentry);
129 * This checks not only the pgrp, but falls back on the pid if no
130 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
133 int session_of_pgrp(int pgrp)
135 struct task_struct *p;
138 read_lock(&tasklist_lock);
139 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
140 if (p->signal->session > 0) {
141 sid = p->signal->session;
144 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
145 p = find_task_by_pid(pgrp);
147 sid = p->signal->session;
149 read_unlock(&tasklist_lock);
155 * Determine if a process group is "orphaned", according to the POSIX
156 * definition in 2.2.2.52. Orphaned process groups are not to be affected
157 * by terminal-generated stop signals. Newly orphaned process groups are
158 * to receive a SIGHUP and a SIGCONT.
160 * "I ask you, have you ever known what it is to be an orphan?"
162 static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
164 struct task_struct *p;
167 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
168 if (p == ignored_task
169 || p->exit_state >= EXIT_ZOMBIE
170 || p->real_parent->pid == 1)
172 if (process_group(p->real_parent) != pgrp
173 && p->real_parent->signal->session == p->signal->session) {
177 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
178 return ret; /* (sighing) "Often!" */
181 int is_orphaned_pgrp(int pgrp)
185 read_lock(&tasklist_lock);
186 retval = will_become_orphaned_pgrp(pgrp, NULL);
187 read_unlock(&tasklist_lock);
192 static inline int has_stopped_jobs(int pgrp)
195 struct task_struct *p;
197 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
198 if (p->state != TASK_STOPPED)
201 /* If p is stopped by a debugger on a signal that won't
202 stop it, then don't count p as stopped. This isn't
203 perfect but it's a good approximation. */
204 if (unlikely (p->ptrace)
205 && p->exit_code != SIGSTOP
206 && p->exit_code != SIGTSTP
207 && p->exit_code != SIGTTOU
208 && p->exit_code != SIGTTIN)
213 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
218 * reparent_to_init() - Reparent the calling kernel thread to the init task.
220 * If a kernel thread is launched as a result of a system call, or if
221 * it ever exits, it should generally reparent itself to init so that
222 * it is correctly cleaned up on exit.
224 * The various task state such as scheduling policy and priority may have
225 * been inherited from a user process, so we reset them to sane values here.
227 * NOTE that reparent_to_init() gives the caller full capabilities.
229 void reparent_to_init(void)
231 write_lock_irq(&tasklist_lock);
233 ptrace_unlink(current);
234 /* Reparent to init */
235 REMOVE_LINKS(current);
236 /* FIXME handle vchild_reaper/initpid */
237 current->parent = child_reaper;
238 current->real_parent = child_reaper;
241 /* Set the exit signal to SIGCHLD so we signal init on exit */
242 current->exit_signal = SIGCHLD;
244 if ((current->policy == SCHED_NORMAL) && (task_nice(current) < 0))
245 set_user_nice(current, 0);
249 security_task_reparent_to_init(current);
250 memcpy(current->rlim, init_task.rlim, sizeof(*(current->rlim)));
251 atomic_inc(&(INIT_USER->__count));
252 switch_uid(INIT_USER);
254 write_unlock_irq(&tasklist_lock);
257 void __set_special_pids(pid_t session, pid_t pgrp)
259 struct task_struct *curr = current;
261 if (curr->signal->session != session) {
262 detach_pid(curr, PIDTYPE_SID);
263 curr->signal->session = session;
264 attach_pid(curr, PIDTYPE_SID, session);
266 if (process_group(curr) != pgrp) {
267 detach_pid(curr, PIDTYPE_PGID);
268 curr->signal->pgrp = pgrp;
269 attach_pid(curr, PIDTYPE_PGID, pgrp);
273 void set_special_pids(pid_t session, pid_t pgrp)
275 write_lock_irq(&tasklist_lock);
276 __set_special_pids(session, pgrp);
277 write_unlock_irq(&tasklist_lock);
281 * Let kernel threads use this to say that they
282 * allow a certain signal (since daemonize() will
283 * have disabled all of them by default).
285 int allow_signal(int sig)
287 if (sig < 1 || sig > _NSIG)
290 spin_lock_irq(¤t->sighand->siglock);
291 sigdelset(¤t->blocked, sig);
293 /* Kernel threads handle their own signals.
294 Let the signal code know it'll be handled, so
295 that they don't get converted to SIGKILL or
296 just silently dropped */
297 current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
300 spin_unlock_irq(¤t->sighand->siglock);
304 EXPORT_SYMBOL(allow_signal);
306 int disallow_signal(int sig)
308 if (sig < 1 || sig > _NSIG)
311 spin_lock_irq(¤t->sighand->siglock);
312 sigaddset(¤t->blocked, sig);
314 spin_unlock_irq(¤t->sighand->siglock);
318 EXPORT_SYMBOL(disallow_signal);
321 * Put all the gunge required to become a kernel thread without
322 * attached user resources in one place where it belongs.
325 void daemonize(const char *name, ...)
328 struct fs_struct *fs;
331 va_start(args, name);
332 vsnprintf(current->comm, sizeof(current->comm), name, args);
336 * If we were started as result of loading a module, close all of the
337 * user space pages. We don't need them, and if we didn't close them
338 * they would be locked into memory.
342 set_special_pids(1, 1);
343 current->signal->tty = NULL;
345 /* Block and flush all signals */
346 sigfillset(&blocked);
347 sigprocmask(SIG_BLOCK, &blocked, NULL);
348 flush_signals(current);
350 /* Become as one with the init task */
352 exit_fs(current); /* current->fs->count--; */
355 atomic_inc(&fs->count);
357 current->files = init_task.files;
358 atomic_inc(¤t->files->count);
363 EXPORT_SYMBOL(daemonize);
365 static inline void close_files(struct files_struct * files)
373 if (i >= files->max_fdset || i >= files->max_fds)
375 set = files->open_fds->fds_bits[j++];
378 struct file * file = xchg(&files->fd[i], NULL);
380 filp_close(file, files);
383 // vx_openfd_dec(fd);
391 struct files_struct *get_files_struct(struct task_struct *task)
393 struct files_struct *files;
398 atomic_inc(&files->count);
404 void fastcall put_files_struct(struct files_struct *files)
406 if (atomic_dec_and_test(&files->count)) {
409 * Free the fd and fdset arrays if we expanded them.
411 if (files->fd != &files->fd_array[0])
412 free_fd_array(files->fd, files->max_fds);
413 if (files->max_fdset > __FD_SETSIZE) {
414 free_fdset(files->open_fds, files->max_fdset);
415 free_fdset(files->close_on_exec, files->max_fdset);
417 kmem_cache_free(files_cachep, files);
421 EXPORT_SYMBOL(put_files_struct);
423 static inline void __exit_files(struct task_struct *tsk)
425 struct files_struct * files = tsk->files;
431 put_files_struct(files);
435 void exit_files(struct task_struct *tsk)
440 static inline void __put_fs_struct(struct fs_struct *fs)
442 /* No need to hold fs->lock if we are killing it */
443 if (atomic_dec_and_test(&fs->count)) {
450 mntput(fs->altrootmnt);
452 kmem_cache_free(fs_cachep, fs);
456 void put_fs_struct(struct fs_struct *fs)
461 static inline void __exit_fs(struct task_struct *tsk)
463 struct fs_struct * fs = tsk->fs;
473 void exit_fs(struct task_struct *tsk)
478 EXPORT_SYMBOL_GPL(exit_fs);
481 * Turn us into a lazy TLB process if we
484 static inline void __exit_mm(struct task_struct * tsk)
486 struct mm_struct *mm = tsk->mm;
492 * Serialize with any possible pending coredump.
493 * We must hold mmap_sem around checking core_waiters
494 * and clearing tsk->mm. The core-inducing thread
495 * will increment core_waiters for each thread in the
496 * group with ->mm != NULL.
498 down_read(&mm->mmap_sem);
499 if (mm->core_waiters) {
500 up_read(&mm->mmap_sem);
501 down_write(&mm->mmap_sem);
502 if (!--mm->core_waiters)
503 complete(mm->core_startup_done);
504 up_write(&mm->mmap_sem);
506 wait_for_completion(&mm->core_done);
507 down_read(&mm->mmap_sem);
509 atomic_inc(&mm->mm_count);
510 if (mm != tsk->active_mm) BUG();
511 /* more a memory barrier than a real lock */
514 up_read(&mm->mmap_sem);
515 #ifdef CONFIG_CKRM_RES_MEM
516 spin_lock(&mm->peertask_lock);
517 list_del_init(&tsk->mm_peers);
518 ckrm_mem_evaluate_mm(mm);
519 spin_unlock(&mm->peertask_lock);
521 enter_lazy_tlb(mm, current);
526 void exit_mm(struct task_struct *tsk)
531 static inline void choose_new_parent(task_t *p, task_t *reaper, task_t *child_reaper)
534 * Make sure we're not reparenting to ourselves and that
535 * the parent is not a zombie.
537 BUG_ON(p == reaper || reaper->state >= EXIT_ZOMBIE || reaper->exit_state >= EXIT_ZOMBIE);
538 p->real_parent = reaper;
539 if (p->parent == p->real_parent)
543 static inline void reparent_thread(task_t *p, task_t *father, int traced)
545 /* We don't want people slaying init. */
546 if (p->exit_signal != -1)
547 p->exit_signal = SIGCHLD;
549 if (p->pdeath_signal)
550 /* We already hold the tasklist_lock here. */
551 group_send_sig_info(p->pdeath_signal, (void *) 0, p);
553 /* Move the child from its dying parent to the new one. */
554 if (unlikely(traced)) {
555 /* Preserve ptrace links if someone else is tracing this child. */
556 list_del_init(&p->ptrace_list);
557 if (p->parent != p->real_parent)
558 list_add(&p->ptrace_list, &p->real_parent->ptrace_children);
560 /* If this child is being traced, then we're the one tracing it
561 * anyway, so let go of it.
564 list_del_init(&p->sibling);
565 p->parent = p->real_parent;
566 list_add_tail(&p->sibling, &p->parent->children);
568 /* If we'd notified the old parent about this child's death,
569 * also notify the new parent.
571 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
572 thread_group_empty(p))
573 do_notify_parent(p, p->exit_signal);
574 else if (p->state == TASK_TRACED) {
576 * If it was at a trace stop, turn it into
577 * a normal stop since it's no longer being
580 p->state = TASK_STOPPED;
585 * process group orphan check
586 * Case ii: Our child is in a different pgrp
587 * than we are, and it was the only connection
588 * outside, so the child pgrp is now orphaned.
590 if ((process_group(p) != process_group(father)) &&
591 (p->signal->session == father->signal->session)) {
592 int pgrp = process_group(p);
594 if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
595 __kill_pg_info(SIGHUP, (void *)1, pgrp);
596 __kill_pg_info(SIGCONT, (void *)1, pgrp);
602 * When we die, we re-parent all our children.
603 * Try to give them to another thread in our thread
604 * group, and if no such member exists, give it to
605 * the global child reaper process (ie "init")
607 static inline void forget_original_parent(struct task_struct * father,
608 struct list_head *to_release)
610 struct task_struct *p, *reaper = father;
611 struct list_head *_p, *_n;
614 reaper = next_thread(reaper);
615 if (reaper == father) {
616 reaper = child_reaper;
619 } while (reaper->exit_state >= EXIT_ZOMBIE);
622 * There are only two places where our children can be:
624 * - in our child list
625 * - in our ptraced child list
627 * Search them and reparent children.
629 list_for_each_safe(_p, _n, &father->children) {
631 p = list_entry(_p,struct task_struct,sibling);
635 /* if father isn't the real parent, then ptrace must be enabled */
636 BUG_ON(father != p->real_parent && !ptrace);
638 if (father == p->real_parent) {
639 /* reparent with a reaper, real father it's us */
640 choose_new_parent(p, reaper, child_reaper);
641 reparent_thread(p, father, 0);
643 /* reparent ptraced task to its real parent */
645 if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
646 thread_group_empty(p))
647 do_notify_parent(p, p->exit_signal);
651 * if the ptraced child is a zombie with exit_signal == -1
652 * we must collect it before we exit, or it will remain
653 * zombie forever since we prevented it from self-reap itself
654 * while it was being traced by us, to be able to see it in wait4.
656 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && p->exit_signal == -1))
657 list_add(&p->ptrace_list, to_release);
659 list_for_each_safe(_p, _n, &father->ptrace_children) {
660 p = list_entry(_p,struct task_struct,ptrace_list);
661 choose_new_parent(p, reaper, child_reaper);
662 reparent_thread(p, father, 1);
667 * Send signals to all our closest relatives so that they know
668 * to properly mourn us..
670 static void exit_notify(struct task_struct *tsk)
673 struct task_struct *t;
674 struct list_head ptrace_dead, *_p, *_n;
678 if (signal_pending(tsk) && !tsk->signal->group_exit
679 && !thread_group_empty(tsk)) {
681 * This occurs when there was a race between our exit
682 * syscall and a group signal choosing us as the one to
683 * wake up. It could be that we are the only thread
684 * alerted to check for pending signals, but another thread
685 * should be woken now to take the signal since we will not.
686 * Now we'll wake all the threads in the group just to make
687 * sure someone gets all the pending signals.
689 read_lock(&tasklist_lock);
690 spin_lock_irq(&tsk->sighand->siglock);
691 for (t = next_thread(tsk); t != tsk; t = next_thread(t))
692 if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
693 recalc_sigpending_tsk(t);
694 if (signal_pending(t))
695 signal_wake_up(t, 0);
697 spin_unlock_irq(&tsk->sighand->siglock);
698 read_unlock(&tasklist_lock);
701 write_lock_irq(&tasklist_lock);
704 * This does two things:
706 * A. Make init inherit all the child processes
707 * B. Check to see if any process groups have become orphaned
708 * as a result of our exiting, and if they have any stopped
709 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
712 INIT_LIST_HEAD(&ptrace_dead);
713 forget_original_parent(tsk, &ptrace_dead);
714 BUG_ON(!list_empty(&tsk->children));
715 BUG_ON(!list_empty(&tsk->ptrace_children));
718 * Check to see if any process groups have become orphaned
719 * as a result of our exiting, and if they have any stopped
720 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
722 * Case i: Our father is in a different pgrp than we are
723 * and we were the only connection outside, so our pgrp
724 * is about to become orphaned.
727 t = tsk->real_parent;
729 if ((process_group(t) != process_group(tsk)) &&
730 (t->signal->session == tsk->signal->session) &&
731 will_become_orphaned_pgrp(process_group(tsk), tsk) &&
732 has_stopped_jobs(process_group(tsk))) {
733 __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));
734 __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));
737 /* Let father know we died
739 * Thread signals are configurable, but you aren't going to use
740 * that to send signals to arbitary processes.
741 * That stops right now.
743 * If the parent exec id doesn't match the exec id we saved
744 * when we started then we know the parent has changed security
747 * If our self_exec id doesn't match our parent_exec_id then
748 * we have changed execution domain as these two values started
749 * the same after a fork.
753 if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&
754 ( tsk->parent_exec_id != t->self_exec_id ||
755 tsk->self_exec_id != tsk->parent_exec_id)
756 && !capable(CAP_KILL))
757 tsk->exit_signal = SIGCHLD;
760 /* If something other than our normal parent is ptracing us, then
761 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
762 * only has special meaning to our real parent.
764 if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {
765 int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;
766 do_notify_parent(tsk, signal);
767 } else if (tsk->ptrace) {
768 do_notify_parent(tsk, SIGCHLD);
772 if (tsk->exit_signal == -1 && tsk->ptrace == 0)
774 tsk->exit_state = state;
777 * Clear these here so that update_process_times() won't try to deliver
778 * itimer, profile or rlimit signals to this task while it is in late exit.
780 tsk->it_virt_value = 0;
781 tsk->it_prof_value = 0;
782 tsk->rlim[RLIMIT_CPU].rlim_cur = RLIM_INFINITY;
784 write_unlock_irq(&tasklist_lock);
786 list_for_each_safe(_p, _n, &ptrace_dead) {
788 t = list_entry(_p,struct task_struct,ptrace_list);
792 /* If the process is dead, release it - nobody will wait for it */
793 if (state == EXIT_DEAD)
796 /* PF_DEAD causes final put_task_struct after we schedule. */
798 tsk->flags |= PF_DEAD;
801 asmlinkage NORET_TYPE void do_exit(long code)
803 struct task_struct *tsk = current;
805 profile_task_exit(tsk);
807 if (unlikely(in_interrupt()))
808 panic("Aiee, killing interrupt handler!");
809 if (unlikely(!tsk->pid))
810 panic("Attempted to kill the idle task!");
811 if (unlikely(tsk->pid == 1))
812 panic("Attempted to kill init!");
815 tsk->flags |= PF_EXITING;
816 del_timer_sync(&tsk->real_timer);
818 if (unlikely(in_atomic()))
819 printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
820 current->comm, current->pid,
823 if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
824 current->ptrace_message = code;
825 ptrace_notify((PTRACE_EVENT_EXIT << 8) | SIGTRAP);
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));
844 if (tsk->signal->leader)
845 disassociate_ctty(1);
847 module_put(tsk->thread_info->exec_domain->module);
849 module_put(tsk->binfmt->module);
851 tsk->exit_code = code;
854 mpol_free(tsk->mempolicy);
855 tsk->mempolicy = NULL;
858 BUG_ON(!(current->flags & PF_DEAD));
861 /* Avoid "noreturn function does return". */
865 NORET_TYPE void complete_and_exit(struct completion *comp, long code)
873 EXPORT_SYMBOL(complete_and_exit);
875 asmlinkage long sys_exit(int error_code)
877 do_exit((error_code&0xff)<<8);
880 task_t fastcall *next_thread(const task_t *p)
885 if (!spin_is_locked(&p->sighand->siglock) &&
886 !rwlock_is_locked(&tasklist_lock))
889 return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
892 EXPORT_SYMBOL(next_thread);
895 * Take down every thread in the group. This is called by fatal signals
896 * as well as by sys_exit_group (below).
899 do_group_exit(int exit_code)
901 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
903 if (current->signal->group_exit)
904 exit_code = current->signal->group_exit_code;
905 else if (!thread_group_empty(current)) {
906 struct signal_struct *const sig = current->signal;
907 struct sighand_struct *const sighand = current->sighand;
908 read_lock(&tasklist_lock);
909 spin_lock_irq(&sighand->siglock);
911 /* Another thread got here before we took the lock. */
912 exit_code = sig->group_exit_code;
915 sig->group_exit_code = exit_code;
916 zap_other_threads(current);
918 spin_unlock_irq(&sighand->siglock);
919 read_unlock(&tasklist_lock);
927 * this kills every thread in the thread group. Note that any externally
928 * wait4()-ing process will get the correct exit code - even if this
929 * thread is not the thread group leader.
931 asmlinkage void sys_exit_group(int error_code)
933 do_group_exit((error_code & 0xff) << 8);
936 static int eligible_child(pid_t pid, int options, task_t *p)
942 if (process_group(p) != process_group(current))
944 } else if (pid != -1) {
945 if (process_group(p) != -pid)
950 * Do not consider detached threads that are
953 if (p->exit_signal == -1 && !p->ptrace)
956 /* Wait for all children (clone and not) if __WALL is set;
957 * otherwise, wait for clone children *only* if __WCLONE is
958 * set; otherwise, wait for non-clone children *only*. (Note:
959 * A "clone" child here is one that reports to its parent
960 * using a signal other than SIGCHLD.) */
961 if (((p->exit_signal != SIGCHLD) ^ ((options & __WCLONE) != 0))
962 && !(options & __WALL))
965 * Do not consider thread group leaders that are
966 * in a non-empty thread group:
968 if (current->tgid != p->tgid && delay_group_leader(p))
971 if (security_task_wait(p))
977 static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
979 struct siginfo __user *infop,
980 struct rusage __user *rusagep)
982 int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
985 retval = put_user(SIGCHLD, &infop->si_signo);
987 retval = put_user(0, &infop->si_errno);
989 retval = put_user((short)why, &infop->si_code);
991 retval = put_user(pid, &infop->si_pid);
993 retval = put_user(uid, &infop->si_uid);
995 retval = put_user(status, &infop->si_status);
1002 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
1003 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1004 * the lock and this task is uninteresting. If we return nonzero, we have
1005 * released the lock and the system call should return.
1007 static int wait_task_zombie(task_t *p, int noreap,
1008 struct siginfo __user *infop,
1009 int __user *stat_addr, struct rusage __user *ru)
1011 unsigned long state;
1015 if (unlikely(noreap)) {
1018 int exit_code = p->exit_code;
1021 if (unlikely(p->exit_state != EXIT_ZOMBIE))
1023 if (unlikely(p->exit_signal == -1 && p->ptrace == 0))
1026 read_unlock(&tasklist_lock);
1027 if ((exit_code & 0x7f) == 0) {
1029 status = exit_code >> 8;
1031 why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
1032 status = exit_code & 0x7f;
1034 return wait_noreap_copyout(p, pid, uid, why,
1039 * Try to move the task's state to DEAD
1040 * only one thread is allowed to do this:
1042 state = xchg(&p->exit_state, EXIT_DEAD);
1043 if (state != EXIT_ZOMBIE) {
1044 BUG_ON(state != EXIT_DEAD);
1047 if (unlikely(p->exit_signal == -1 && p->ptrace == 0)) {
1049 * This can only happen in a race with a ptraced thread
1050 * dying on another processor.
1055 if (likely(p->real_parent == p->parent) && likely(p->signal)) {
1057 * The resource counters for the group leader are in its
1058 * own task_struct. Those for dead threads in the group
1059 * are in its signal_struct, as are those for the child
1060 * processes it has previously reaped. All these
1061 * accumulate in the parent's signal_struct c* fields.
1063 * We don't bother to take a lock here to protect these
1064 * p->signal fields, because they are only touched by
1065 * __exit_signal, which runs with tasklist_lock
1066 * write-locked anyway, and so is excluded here. We do
1067 * need to protect the access to p->parent->signal fields,
1068 * as other threads in the parent group can be right
1069 * here reaping other children at the same time.
1071 spin_lock_irq(&p->parent->sighand->siglock);
1072 p->parent->signal->cutime +=
1073 p->utime + p->signal->utime + p->signal->cutime;
1074 p->parent->signal->cstime +=
1075 p->stime + p->signal->stime + p->signal->cstime;
1076 p->parent->signal->cmin_flt +=
1077 p->min_flt + p->signal->min_flt + p->signal->cmin_flt;
1078 p->parent->signal->cmaj_flt +=
1079 p->maj_flt + p->signal->maj_flt + p->signal->cmaj_flt;
1080 p->parent->signal->cnvcsw +=
1081 p->nvcsw + p->signal->nvcsw + p->signal->cnvcsw;
1082 p->parent->signal->cnivcsw +=
1083 p->nivcsw + p->signal->nivcsw + p->signal->cnivcsw;
1084 spin_unlock_irq(&p->parent->sighand->siglock);
1088 * Now we are sure this task is interesting, and no other
1089 * thread can reap it because we set its state to EXIT_DEAD.
1091 read_unlock(&tasklist_lock);
1093 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1094 status = p->signal->group_exit
1095 ? p->signal->group_exit_code : p->exit_code;
1096 if (!retval && stat_addr)
1097 retval = put_user(status, stat_addr);
1098 if (!retval && infop)
1099 retval = put_user(SIGCHLD, &infop->si_signo);
1100 if (!retval && infop)
1101 retval = put_user(0, &infop->si_errno);
1102 if (!retval && infop) {
1105 if ((status & 0x7f) == 0) {
1109 why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1112 retval = put_user((short)why, &infop->si_code);
1114 retval = put_user(status, &infop->si_status);
1116 if (!retval && infop)
1117 retval = put_user(p->pid, &infop->si_pid);
1118 if (!retval && infop)
1119 retval = put_user(p->uid, &infop->si_uid);
1121 // TODO: is this safe?
1122 p->exit_state = EXIT_ZOMBIE;
1126 if (p->real_parent != p->parent) {
1127 write_lock_irq(&tasklist_lock);
1128 /* Double-check with lock held. */
1129 if (p->real_parent != p->parent) {
1131 // TODO: is this safe?
1132 p->exit_state = EXIT_ZOMBIE;
1134 * If this is not a detached task, notify the parent.
1135 * If it's still not detached after that, don't release
1138 if (p->exit_signal != -1) {
1139 do_notify_parent(p, p->exit_signal);
1140 if (p->exit_signal != -1)
1144 write_unlock_irq(&tasklist_lock);
1153 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1154 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1155 * the lock and this task is uninteresting. If we return nonzero, we have
1156 * released the lock and the system call should return.
1158 static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
1159 struct siginfo __user *infop,
1160 int __user *stat_addr, struct rusage __user *ru)
1162 int retval, exit_code;
1166 if (delayed_group_leader && !(p->ptrace & PT_PTRACED) &&
1167 p->signal && p->signal->group_stop_count > 0)
1169 * A group stop is in progress and this is the group leader.
1170 * We won't report until all threads have stopped.
1175 * Now we are pretty sure this task is interesting.
1176 * Make sure it doesn't get reaped out from under us while we
1177 * give up the lock and then examine it below. We don't want to
1178 * keep holding onto the tasklist_lock while we call getrusage and
1179 * possibly take page faults for user memory.
1182 read_unlock(&tasklist_lock);
1184 if (unlikely(noreap)) {
1187 int why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
1189 exit_code = p->exit_code;
1190 if (unlikely(!exit_code) ||
1191 unlikely(p->state > TASK_STOPPED))
1193 return wait_noreap_copyout(p, pid, uid,
1194 why, (exit_code << 8) | 0x7f,
1198 write_lock_irq(&tasklist_lock);
1201 * This uses xchg to be atomic with the thread resuming and setting
1202 * it. It must also be done with the write lock held to prevent a
1203 * race with the EXIT_ZOMBIE case.
1205 exit_code = xchg(&p->exit_code, 0);
1206 if (unlikely(p->exit_state >= EXIT_ZOMBIE)) {
1208 * The task resumed and then died. Let the next iteration
1209 * catch it in EXIT_ZOMBIE. Note that exit_code might
1210 * already be zero here if it resumed and did _exit(0).
1211 * The task itself is dead and won't touch exit_code again;
1212 * other processors in this function are locked out.
1214 p->exit_code = exit_code;
1217 if (unlikely(exit_code == 0)) {
1219 * Another thread in this function got to it first, or it
1220 * resumed, or it resumed and then died.
1222 write_unlock_irq(&tasklist_lock);
1226 * We are returning to the wait loop without having successfully
1227 * removed the process and having released the lock. We cannot
1228 * continue, since the "p" task pointer is potentially stale.
1230 * Return -EAGAIN, and do_wait() will restart the loop from the
1231 * beginning. Do _not_ re-acquire the lock.
1236 /* move to end of parent's list to avoid starvation */
1238 add_parent(p, p->parent);
1240 write_unlock_irq(&tasklist_lock);
1242 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1243 if (!retval && stat_addr)
1244 retval = put_user((exit_code << 8) | 0x7f, stat_addr);
1245 if (!retval && infop)
1246 retval = put_user(SIGCHLD, &infop->si_signo);
1247 if (!retval && infop)
1248 retval = put_user(0, &infop->si_errno);
1249 if (!retval && infop)
1250 retval = put_user((short)((p->ptrace & PT_PTRACED)
1251 ? CLD_TRAPPED : CLD_STOPPED),
1253 if (!retval && infop)
1254 retval = put_user(exit_code, &infop->si_status);
1255 if (!retval && infop)
1256 retval = put_user(p->pid, &infop->si_pid);
1257 if (!retval && infop)
1258 retval = put_user(p->uid, &infop->si_uid);
1268 * Handle do_wait work for one task in a live, non-stopped state.
1269 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1270 * the lock and this task is uninteresting. If we return nonzero, we have
1271 * released the lock and the system call should return.
1273 static int wait_task_continued(task_t *p, int noreap,
1274 struct siginfo __user *infop,
1275 int __user *stat_addr, struct rusage __user *ru)
1281 if (unlikely(!p->signal))
1284 if (p->signal->stop_state >= 0)
1287 spin_lock_irq(&p->sighand->siglock);
1288 if (p->signal->stop_state >= 0) { /* Re-check with the lock held. */
1289 spin_unlock_irq(&p->sighand->siglock);
1293 p->signal->stop_state = 0;
1294 spin_unlock_irq(&p->sighand->siglock);
1299 read_unlock(&tasklist_lock);
1302 retval = ru ? getrusage(p, RUSAGE_BOTH, ru) : 0;
1304 if (!retval && stat_addr)
1305 retval = put_user(0xffff, stat_addr);
1309 retval = wait_noreap_copyout(p, pid, uid,
1310 CLD_CONTINUED, SIGCONT,
1312 BUG_ON(retval == 0);
1319 static inline int my_ptrace_child(struct task_struct *p)
1321 if (!(p->ptrace & PT_PTRACED))
1324 * This child was PTRACE_ATTACH'd. We should be seeing it only if
1325 * we are the attacher. If we are the real parent, this is a race
1326 * inside ptrace_attach. It is waiting for the tasklist_lock,
1327 * which we have to switch the parent links, but has already set
1328 * the flags in p->ptrace.
1330 return (p->parent != p->real_parent);
1333 static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
1334 int __user *stat_addr, struct rusage __user *ru)
1336 DECLARE_WAITQUEUE(wait, current);
1337 struct task_struct *tsk;
1340 add_wait_queue(¤t->wait_chldexit,&wait);
1343 * We will set this flag if we see any child that might later
1344 * match our criteria, even if we are not able to reap it yet.
1347 current->state = TASK_INTERRUPTIBLE;
1348 read_lock(&tasklist_lock);
1351 struct task_struct *p;
1352 struct list_head *_p;
1355 list_for_each(_p,&tsk->children) {
1356 p = list_entry(_p,struct task_struct,sibling);
1358 ret = eligible_child(pid, options, p);
1364 if (!my_ptrace_child(p))
1369 * It's stopped now, so it might later
1370 * continue, exit, or stop again.
1373 if (!(options & WUNTRACED) &&
1374 !my_ptrace_child(p))
1376 retval = wait_task_stopped(p, ret == 2,
1377 (options & WNOWAIT),
1380 if (retval == -EAGAIN)
1382 if (retval != 0) /* He released the lock. */
1387 if (p->exit_state == EXIT_DEAD)
1389 // case EXIT_ZOMBIE:
1390 if (p->exit_state == EXIT_ZOMBIE) {
1392 * Eligible but we cannot release
1396 goto check_continued;
1397 if (!likely(options & WEXITED))
1399 retval = wait_task_zombie(
1400 p, (options & WNOWAIT),
1401 infop, stat_addr, ru);
1402 /* He released the lock. */
1409 * It's running now, so it might later
1410 * exit, stop, or stop and then continue.
1413 if (!unlikely(options & WCONTINUED))
1416 retval = wait_task_continued(
1417 p, (options & WNOWAIT),
1418 infop, stat_addr, ru);
1419 if (retval != 0) /* He released the lock. */
1425 list_for_each(_p, &tsk->ptrace_children) {
1426 p = list_entry(_p, struct task_struct,
1428 if (!eligible_child(pid, options, p))
1434 if (options & __WNOTHREAD)
1436 tsk = next_thread(tsk);
1437 if (tsk->signal != current->signal)
1439 } while (tsk != current);
1441 read_unlock(&tasklist_lock);
1444 if (options & WNOHANG)
1446 retval = -ERESTARTSYS;
1447 if (signal_pending(current))
1454 current->state = TASK_RUNNING;
1455 remove_wait_queue(¤t->wait_chldexit,&wait);
1461 * For a WNOHANG return, clear out all the fields
1462 * we would set so the user can easily tell the
1466 retval = put_user(0, &infop->si_signo);
1468 retval = put_user(0, &infop->si_errno);
1470 retval = put_user(0, &infop->si_code);
1472 retval = put_user(0, &infop->si_pid);
1474 retval = put_user(0, &infop->si_uid);
1476 retval = put_user(0, &infop->si_status);
1482 asmlinkage long sys_waitid(int which, pid_t pid,
1483 struct siginfo __user *infop, int options,
1484 struct rusage __user *ru)
1488 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
1490 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1510 ret = do_wait(pid, options, infop, NULL, ru);
1512 /* avoid REGPARM breakage on x86: */
1513 prevent_tail_call(ret);
1517 asmlinkage long sys_wait4(pid_t pid, int __user *stat_addr,
1518 int options, struct rusage __user *ru)
1522 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1523 __WNOTHREAD|__WCLONE|__WALL))
1525 ret = do_wait(pid, options | WEXITED, NULL, stat_addr, ru);
1527 /* avoid REGPARM breakage on x86: */
1528 prevent_tail_call(ret);
1532 #ifdef __ARCH_WANT_SYS_WAITPID
1535 * sys_waitpid() remains for compatibility. waitpid() should be
1536 * implemented by calling sys_wait4() from libc.a.
1538 asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options)
1540 return sys_wait4(pid, stat_addr, options, NULL);