diff --git a/include/asm-generic/siginfo.h b/include/asm-generic/siginfo.h index 942d30b..1da9c20 100644 --- a/include/asm-generic/siginfo.h +++ b/include/asm-generic/siginfo.h @@ -218,7 +218,8 @@ typedef struct siginfo { #define CLD_TRAPPED (__SI_CHLD|4) /* traced child has trapped */ #define CLD_STOPPED (__SI_CHLD|5) /* child has stopped */ #define CLD_CONTINUED (__SI_CHLD|6) /* stopped child has continued */ -#define NSIGCHLD 6 +#define CLD_DETACHED (__SI_CHLD|7) /* child has detached */ +#define NSIGCHLD 7 /* * SIGPOLL si_codes diff --git a/include/linux/prctl.h b/include/linux/prctl.h index a3baeb2..fbd2451 100644 --- a/include/linux/prctl.h +++ b/include/linux/prctl.h @@ -102,4 +102,6 @@ #define PR_MCE_KILL_GET 34 +#define PR_DETACH 35 + #endif /* _LINUX_PRCTL_H */ diff --git a/include/linux/sched.h b/include/linux/sched.h index e74882f..2e2acba 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1260,6 +1260,8 @@ struct task_struct { /* task state */ int exit_state; int exit_code, exit_signal; + int detach_code; + int detaching; int pdeath_signal; /* The signal sent when the parent dies */ /* ??? */ unsigned int personality; diff --git a/kernel/exit.c b/kernel/exit.c index f9a45eb..276b39f 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -791,7 +791,14 @@ static void forget_original_parent(struct task_struct *father) reaper = find_new_reaper(father); list_for_each_entry_safe(p, n, &father->children, sibling) { - struct task_struct *t = p; + struct task_struct *t; + if (p->detaching) { + list_move_tail(&p->sibling, + &p->real_parent->children); + p->detaching = 0; + continue; + } + t = p; do { t->real_parent = reaper; if (t->parent == father) { @@ -1507,6 +1514,50 @@ static int wait_task_continued(struct wait_opts *wo, struct task_struct *p) return retval; } +static int wait_task_detached(struct wait_opts *wo, struct task_struct *p) +{ + int dt, retval = 0; + pid_t pid; + uid_t uid; + + if (!likely(wo->wo_flags & WEXITED)) + return 0; + + if (unlikely(wo->wo_flags & WNOWAIT)) { + get_task_struct(p); + read_unlock(&tasklist_lock); + pid = task_pid_vnr(p); + uid = __task_cred(p)->uid; + return wait_noreap_copyout(wo, p, pid, uid, CLD_DETACHED, + p->detach_code >> 8); + } + + dt = xchg(&p->detaching, 0); + if (dt != 1) + return 0; + get_task_struct(p); + read_unlock(&tasklist_lock); + + /* hand it over to init */ + write_lock_irq(&tasklist_lock); + list_move_tail(&p->sibling, &p->real_parent->children); + write_unlock_irq(&tasklist_lock); + + if (wo->wo_stat) + retval = put_user(p->detach_code, wo->wo_stat); + + if (!retval) { + pid = task_pid_vnr(p); + uid = __task_cred(p)->uid; + retval = wait_noreap_copyout(wo, p, pid, uid, CLD_DETACHED, + p->detach_code >> 8); + } else { + put_task_struct(p); + } + + return retval; +} + /* * Consider @p for a wait by @parent. * @@ -1549,6 +1600,9 @@ static int wait_consider_task(struct wait_opts *wo, int ptrace, if (p->exit_state == EXIT_DEAD) return 0; + if (p->detaching) + return wait_task_detached(wo, p); + /* * We don't reap group leaders with subthreads. */ diff --git a/kernel/fork.c b/kernel/fork.c index 25e4291..dd28aff 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1233,6 +1233,7 @@ static struct task_struct *copy_process(unsigned long clone_flags, p->exit_signal = (clone_flags & CLONE_THREAD) ? -1 : (clone_flags & CSIGNAL); p->pdeath_signal = 0; p->exit_state = 0; + p->detaching = 0; /* * Ok, make it visible to the rest of the system. diff --git a/kernel/sys.c b/kernel/sys.c index 18da702..e4dadd6 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -1736,6 +1737,40 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, else error = PR_MCE_KILL_DEFAULT; break; + case PR_DETACH: { + struct task_struct *p; + struct pid_namespace *pid_ns = task_active_pid_ns(me); + int notif = DEATH_REAP; + error = -EPERM; + /* not detaching from init */ + if (me->real_parent == pid_ns->child_reaper) + break; + if (arg2 & ~0x7f) + break; + write_lock_irq(&tasklist_lock); + me->detach_code = arg2 << 8; + notif = do_signal_parent(me, me->exit_signal, + CLD_DETACHED, arg2); + if (notif != DEATH_REAP) + me->detaching = 1; + else + list_move_tail(&me->sibling, + &me->real_parent->children); + if (!ptrace_reparented(me)) + me->parent = pid_ns->child_reaper; + me->real_parent = pid_ns->child_reaper; + /* reparent threads */ + p = me; + while_each_thread(me, p) { + if (!ptrace_reparented(p)) + p->parent = pid_ns->child_reaper; + p->real_parent = pid_ns->child_reaper; + } + me->exit_signal = SIGCHLD; + write_unlock_irq(&tasklist_lock); + error = 0; + break; + } default: error = -EINVAL; break;