From owner-svn-src-all@FreeBSD.ORG Thu Aug 18 22:51:31 2011 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id A2DC21065670; Thu, 18 Aug 2011 22:51:31 +0000 (UTC) (envelope-from jonathan@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 37CD58FC14; Thu, 18 Aug 2011 22:51:31 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id p7IMpVvs076708; Thu, 18 Aug 2011 22:51:31 GMT (envelope-from jonathan@svn.freebsd.org) Received: (from jonathan@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id p7IMpUir076690; Thu, 18 Aug 2011 22:51:30 GMT (envelope-from jonathan@svn.freebsd.org) Message-Id: <201108182251.p7IMpUir076690@svn.freebsd.org> From: Jonathan Anderson Date: Thu, 18 Aug 2011 22:51:30 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r224987 - in head: lib/libc/sys sys/compat/linux sys/conf sys/kern sys/sys X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 18 Aug 2011 22:51:31 -0000 Author: jonathan Date: Thu Aug 18 22:51:30 2011 New Revision: 224987 URL: http://svn.freebsd.org/changeset/base/224987 Log: Add experimental support for process descriptors A "process descriptor" file descriptor is used to manage processes without using the PID namespace. This is required for Capsicum's Capability Mode, where the PID namespace is unavailable. New system calls pdfork(2) and pdkill(2) offer the functional equivalents of fork(2) and kill(2). pdgetpid(2) allows querying the PID of the remote process for debugging purposes. The currently-unimplemented pdwait(2) will, in the future, allow querying rusage/exit status. In the interim, poll(2) may be used to check (and wait for) process termination. When a process is referenced by a process descriptor, it does not issue SIGCHLD to the parent, making it suitable for use in libraries---a common scenario when using library compartmentalisation from within large applications (such as web browsers). Some observers may note a similarity to Mach task ports; process descriptors provide a subset of this behaviour, but in a UNIX style. This feature is enabled by "options PROCDESC", but as with several other Capsicum kernel features, is not enabled by default in GENERIC 9.0. Reviewed by: jhb, kib Approved by: re (kib), mentor (rwatson) Sponsored by: Google Inc Added: head/lib/libc/sys/pdfork.2 (contents, props changed) head/sys/kern/sys_procdesc.c (contents, props changed) head/sys/sys/procdesc.h (contents, props changed) Modified: head/lib/libc/sys/Makefile.inc head/lib/libc/sys/Symbol.map head/lib/libc/sys/cap_new.2 head/sys/compat/linux/linux_fork.c head/sys/conf/NOTES head/sys/conf/files head/sys/conf/options head/sys/kern/capabilities.conf head/sys/kern/init_main.c head/sys/kern/kern_descrip.c head/sys/kern/kern_exit.c head/sys/kern/kern_fork.c head/sys/kern/kern_kthread.c head/sys/kern/kern_sig.c head/sys/kern/syscalls.master head/sys/sys/capability.h head/sys/sys/file.h head/sys/sys/proc.h head/sys/sys/unistd.h head/sys/sys/user.h Modified: head/lib/libc/sys/Makefile.inc ============================================================================== --- head/lib/libc/sys/Makefile.inc Thu Aug 18 22:20:45 2011 (r224986) +++ head/lib/libc/sys/Makefile.inc Thu Aug 18 22:51:30 2011 (r224987) @@ -96,7 +96,7 @@ MAN+= abort2.2 accept.2 access.2 acct.2 mq_setattr.2 \ msgctl.2 msgget.2 msgrcv.2 msgsnd.2 \ msync.2 munmap.2 nanosleep.2 nfssvc.2 ntp_adjtime.2 open.2 \ - pathconf.2 pipe.2 poll.2 posix_fallocate.2 posix_openpt.2 profil.2 \ + pathconf.2 pdfork.2 pipe.2 poll.2 posix_fallocate.2 posix_openpt.2 profil.2 \ pselect.2 ptrace.2 quotactl.2 \ read.2 readlink.2 reboot.2 recv.2 rename.2 revoke.2 rfork.2 rmdir.2 \ rtprio.2 @@ -178,6 +178,9 @@ MLINKS+=ntp_adjtime.2 ntp_gettime.2 MLINKS+=open.2 openat.2 MLINKS+=pathconf.2 fpathconf.2 MLINKS+=pathconf.2 lpathconf.2 +MLINKS+=pdfork.2 pdgetpid.2\ + pdfork.2 pdkill.2 \ + pdfork.2 pdwait4.2 MLINKS+=read.2 pread.2 read.2 preadv.2 read.2 readv.2 MLINKS+=readlink.2 readlinkat.2 MLINKS+=recv.2 recvfrom.2 recv.2 recvmsg.2 Modified: head/lib/libc/sys/Symbol.map ============================================================================== --- head/lib/libc/sys/Symbol.map Thu Aug 18 22:20:45 2011 (r224986) +++ head/lib/libc/sys/Symbol.map Thu Aug 18 22:51:30 2011 (r224987) @@ -366,6 +366,9 @@ FBSD_1.2 { cap_new; cap_getrights; getloginclass; + pdfork; + pdgetpid; + pdkill; posix_fallocate; rctl_get_racct; rctl_get_rules; Modified: head/lib/libc/sys/cap_new.2 ============================================================================== --- head/lib/libc/sys/cap_new.2 Thu Aug 18 22:20:45 2011 (r224986) +++ head/lib/libc/sys/cap_new.2 Thu Aug 18 22:51:30 2011 (r224987) @@ -260,7 +260,7 @@ Permit .Xr pdkill 2 . .It Dv CAP_PDWAIT Permit -.Xr pdwait 2 . +.Xr pdwait4 2 . .It Dv CAP_PEELOFF Permit .Xr sctp_peeloff 2 . @@ -429,7 +429,7 @@ argument is not a capability. .Xr openat 2 , .Xr pdgetpid 2 , .Xr pdkill 2 , -.Xr pdwait 2 , +.Xr pdwait4 2 , .Xr pipe 2 , .Xr poll 2 , .Xr pread 2 , Added: head/lib/libc/sys/pdfork.2 ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/lib/libc/sys/pdfork.2 Thu Aug 18 22:51:30 2011 (r224987) @@ -0,0 +1,182 @@ +.\" +.\" Copyright (c) 2009-2010 Robert N. M. Watson +.\" All rights reserved. +.\" +.\" This software was developed at the University of Cambridge Computer +.\" Laboratory with support from a grant from Google, Inc. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd August 16, 2011 +.Dt PDFORK 2 +.Os +.Sh NAME +.Nm pdfork , +.Nm pdgetpid , +.Nm pdkill , +.Nm pdwait4 +.Nd System calls to manage process descriptors +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In sys/procdesc.h +.Ft int +.Fn pdfork "int *fdp" "int flags" +.Ft int +.Fn pdgetpid "int fd" "pid_t *pidp" +.Ft int +.Fn pdkill "int fd" "int signum" +.Ft int +.Fn pdwait4 "int fd" "int *status" "int options" "struct rusage *rusage" +.Sh DESCRIPTION +Process descriptors are special file descriptors that represent processes, +and are created using +.Fn pdfork , +a variant of +.Xr fork 2 , +which, if successful, returns a process descriptor in the integer pointed to +by +.Fa pidp . +Processes created via +.Fn pdfork +will not cause +.Dv SIGCHLD +on termination. +.Fn pdfork +can accept the flags: +.Bl -tag -width ".Dv PD_DAEMON" +.It Dv PD_DAEMON +Instead of the default terminate-on-close behaviour, allow the process to +live until it is explicitly killed with +.Xr kill 2 . +.Pp +This option is not permitted in Capsicum capability mode (see +.Xr cap_enter 2 ) . +.El +.Pp +.Fn pdgetpid +queries the process ID (PID) if the process descriptor +.Fa fd . +.Pp +.Fn pdkill +is functionally identical to +.Xr kill 2 , +except that it accepts a process descriptor, +.Fa fd , +rather than a PID. +.Pp +.Fn pdwait4 +behaves identially to +.Xr wait4 2 , +but operates with respect to a process descriptor argument rather than a PID. +.Pp +The following system calls also have effects specific to process descriptors: +.Pp +.Xr fstat 2 +queries status of a process descriptor; currently only the +.Fa st_mode , +.Fa st_birthtime , +.Fa st_atime , +.Fa st_ctime +and +.Fa st_mtime +fields are defined. If the owner read, write, and execute bits are set then the +process represented by the process descriptor is still alive. +.Pp +.Xr poll 2 +and +.Xr select 2 +allow waiting for process state transitions; currently only +.Dv POLLHUP +is defined, and will be raised when the process dies. +.Pp +.Xr close 2 +will close the process descriptor unless +.Dv PD_DAEMON +is set; if the process is still alive and this is +the last reference to the process descriptor, the process will be terminated +with the signal +.Dv SIGKILL . +.Sh RETURN VALUES +.Fn pdfork +returns a PID, 0 or -1, as +.Xr fork 2 +does. +.Pp +.Fn pdgetpid +and +.Fn pdkill +return 0 on success and -1 on failure. +.Pp +.Fn pdwait4 +returns a PID on success and -1 on failure. +.Sh ERRORS +These functions may return the same error numbers as their PID-based equivalents +(e.g. +.Fn pdfork +may return the same error numbers as +.Xr fork 2 ) , +with the following additions: +.Bl -tag -width Er +.It Bq Er EINVAL +The signal number given to +.Fn pdkill +is invalid. +.It Bq Er ENOTCAPABLE +The process descriptor being operated on has insufficient rights (e.g. +.Dv CAP_PDKILL +for +.Fn pdkill ) . +.El +.Sh SEE ALSO +.Xr close 2 , +.Xr fork 2 , +.Xr fstat 2 , +.Xr kill 2 , +.Xr poll 2 , +.Xr wait4 2 +.Sh HISTORY +The +.Fn pdfork , +.Fn pdgetpid , +.Fn pdkill +and +.Fn pdwait4 +system calls first appeared in +.Fx 9.0 . +.Pp +Support for process descriptors mode was developed as part of the +.Tn TrustedBSD +Project. +.Sh AUTHORS +.An -nosplit +These functions and the capability facility were created by +.An "Robert N. M. Watson" Aq rwatson@FreeBSD.org +and +.An "Jonathan Anderson" Aq jonathan@FreeBSD.org +at the University of Cambridge Computer Laboratory with support from a grant +from Google, Inc. +.Sh BUGS +.Fn pdwait4 +has not yet been implemented. Modified: head/sys/compat/linux/linux_fork.c ============================================================================== --- head/sys/compat/linux/linux_fork.c Thu Aug 18 22:20:45 2011 (r224986) +++ head/sys/compat/linux/linux_fork.c Thu Aug 18 22:51:30 2011 (r224987) @@ -64,7 +64,8 @@ linux_fork(struct thread *td, struct lin printf(ARGS(fork, "")); #endif - if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2)) != 0) + if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2, NULL, 0)) + != 0) return (error); td->td_retval[0] = p2->p_pid; @@ -100,7 +101,8 @@ linux_vfork(struct thread *td, struct li #endif /* Exclude RFPPWAIT */ - if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2)) != 0) + if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2, + NULL, 0)) != 0) return (error); td->td_retval[0] = p2->p_pid; @@ -190,7 +192,7 @@ linux_clone(struct thread *td, struct li if (args->parent_tidptr == NULL) return (EINVAL); - error = fork1(td, ff, 0, &p2); + error = fork1(td, ff, 0, &p2, NULL, 0); if (error) return (error); Modified: head/sys/conf/NOTES ============================================================================== --- head/sys/conf/NOTES Thu Aug 18 22:20:45 2011 (r224986) +++ head/sys/conf/NOTES Thu Aug 18 22:51:30 2011 (r224987) @@ -1159,6 +1159,9 @@ options MAC_TEST options CAPABILITIES # fine-grained rights on file descriptors options CAPABILITY_MODE # sandboxes with no global namespace access +# Support for process descriptors +options PROCDESC + ##################################################################### # CLOCK OPTIONS Modified: head/sys/conf/files ============================================================================== --- head/sys/conf/files Thu Aug 18 22:20:45 2011 (r224986) +++ head/sys/conf/files Thu Aug 18 22:51:30 2011 (r224987) @@ -2412,6 +2412,7 @@ kern/subr_witness.c optional witness kern/sys_capability.c standard kern/sys_generic.c standard kern/sys_pipe.c standard +kern/sys_procdesc.c standard kern/sys_process.c standard kern/sys_socket.c standard kern/syscalls.c standard Modified: head/sys/conf/options ============================================================================== --- head/sys/conf/options Thu Aug 18 22:20:45 2011 (r224986) +++ head/sys/conf/options Thu Aug 18 22:51:30 2011 (r224987) @@ -149,6 +149,7 @@ PPC_DEBUG opt_ppc.h PPC_PROBE_CHIPSET opt_ppc.h PPS_SYNC opt_ntp.h PREEMPTION opt_sched.h +PROCDESC opt_procdesc.h QUOTA SCHED_4BSD opt_sched.h SCHED_STATS opt_sched.h Modified: head/sys/kern/capabilities.conf ============================================================================== --- head/sys/kern/capabilities.conf Thu Aug 18 22:20:45 2011 (r224986) +++ head/sys/kern/capabilities.conf Thu Aug 18 22:51:30 2011 (r224987) @@ -475,7 +475,7 @@ openbsd_poll pdfork pdgetpid pdkill -pdwait4 +#pdwait4 # not yet implemented ## ## Allow pipe(2). Modified: head/sys/kern/init_main.c ============================================================================== --- head/sys/kern/init_main.c Thu Aug 18 22:20:45 2011 (r224986) +++ head/sys/kern/init_main.c Thu Aug 18 22:51:30 2011 (r224987) @@ -790,7 +790,8 @@ create_init(const void *udata __unused) struct ucred *newcred, *oldcred; int error; - error = fork1(&thread0, RFFDG | RFPROC | RFSTOPPED, 0, &initproc); + error = fork1(&thread0, RFFDG | RFPROC | RFSTOPPED, 0, &initproc, + NULL, 0); if (error) panic("cannot fork init: %d\n", error); KASSERT(initproc->p_pid == 1, ("create_init: initproc->p_pid != 1")); Modified: head/sys/kern/kern_descrip.c ============================================================================== --- head/sys/kern/kern_descrip.c Thu Aug 18 22:20:45 2011 (r224986) +++ head/sys/kern/kern_descrip.c Thu Aug 18 22:51:30 2011 (r224987) @@ -41,6 +41,7 @@ __FBSDID("$FreeBSD$"); #include "opt_compat.h" #include "opt_ddb.h" #include "opt_ktrace.h" +#include "opt_procdesc.h" #include #include @@ -65,6 +66,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -120,6 +122,8 @@ static int fill_vnode_info(struct vnode static int fill_socket_info(struct socket *so, struct kinfo_file *kif); static int fill_pts_info(struct tty *tp, struct kinfo_file *kif); static int fill_pipe_info(struct pipe *pi, struct kinfo_file *kif); +static int fill_procdesc_info(struct procdesc *pdp, + struct kinfo_file *kif); /* * A process is initially started out with NDFILE descriptors stored within @@ -3056,6 +3060,12 @@ sysctl_kern_proc_ofiledesc(SYSCTL_HANDLE tp = fp->f_data; break; +#ifdef PROCDESC + case DTYPE_PROCDESC: + kif->kf_type = KF_TYPE_PROCDESC; + break; +#endif + default: kif->kf_type = KF_TYPE_UNKNOWN; break; @@ -3218,6 +3228,9 @@ export_fd_for_sysctl(void *data, int typ case KF_TYPE_PTS: error = fill_pts_info((struct tty *)data, kif); break; + case KF_TYPE_PROCDESC: + error = fill_procdesc_info((struct procdesc *)data, kif); + break; default: error = 0; } @@ -3391,6 +3404,13 @@ sysctl_kern_proc_filedesc(SYSCTL_HANDLER data = fp->f_data; break; +#ifdef PROCDESC + case DTYPE_PROCDESC: + type = KF_TYPE_PROCDESC; + data = fp->f_data; + break; +#endif + default: type = KF_TYPE_UNKNOWN; break; @@ -3586,6 +3606,16 @@ fill_pipe_info(struct pipe *pi, struct k return (0); } +static int +fill_procdesc_info(struct procdesc *pdp, struct kinfo_file *kif) +{ + + if (pdp == NULL) + return (1); + kif->kf_un.kf_proc.kf_pid = pdp->pd_pid; + return (0); +} + static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD, sysctl_kern_proc_filedesc, "Process filedesc entries"); Modified: head/sys/kern/kern_exit.c ============================================================================== --- head/sys/kern/kern_exit.c Thu Aug 18 22:20:45 2011 (r224986) +++ head/sys/kern/kern_exit.c Thu Aug 18 22:51:30 2011 (r224987) @@ -40,16 +40,19 @@ __FBSDID("$FreeBSD$"); #include "opt_compat.h" #include "opt_kdtrace.h" #include "opt_ktrace.h" +#include "opt_procdesc.h" #include #include #include +#include #include #include #include #include #include #include +#include #include #include #include @@ -461,39 +464,54 @@ exit1(struct thread *td, int rv) knlist_clear(&p->p_klist, 1); /* - * Notify parent that we're gone. If parent has the PS_NOCLDWAIT - * flag set, or if the handler is set to SIG_IGN, notify process - * 1 instead (and hope it will handle this situation). + * If this is a process with a descriptor, we may not need to deliver + * a signal to the parent. proctree_lock is held over + * procdesc_exit() to serialize concurrent calls to close() and + * exit(). */ - PROC_LOCK(p->p_pptr); - mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); - if (p->p_pptr->p_sigacts->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) { - struct proc *pp; - - mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); - pp = p->p_pptr; - PROC_UNLOCK(pp); - proc_reparent(p, initproc); - p->p_sigparent = SIGCHLD; - PROC_LOCK(p->p_pptr); - +#ifdef PROCDESC + if (p->p_procdesc == NULL || procdesc_exit(p)) { +#endif /* - * Notify parent, so in case he was wait(2)ing or - * executing waitpid(2) with our pid, he will - * continue. + * Notify parent that we're gone. If parent has the + * PS_NOCLDWAIT flag set, or if the handler is set to SIG_IGN, + * notify process 1 instead (and hope it will handle this + * situation). */ - wakeup(pp); + PROC_LOCK(p->p_pptr); + mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); + if (p->p_pptr->p_sigacts->ps_flag & + (PS_NOCLDWAIT | PS_CLDSIGIGN)) { + struct proc *pp; + + mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); + pp = p->p_pptr; + PROC_UNLOCK(pp); + proc_reparent(p, initproc); + p->p_sigparent = SIGCHLD; + PROC_LOCK(p->p_pptr); + + /* + * Notify parent, so in case he was wait(2)ing or + * executing waitpid(2) with our pid, he will + * continue. + */ + wakeup(pp); + } else + mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); + + if (p->p_pptr == initproc) + psignal(p->p_pptr, SIGCHLD); + else if (p->p_sigparent != 0) { + if (p->p_sigparent == SIGCHLD) + childproc_exited(p); + else /* LINUX thread */ + psignal(p->p_pptr, p->p_sigparent); + } +#ifdef PROCDESC } else - mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); - - if (p->p_pptr == initproc) - psignal(p->p_pptr, SIGCHLD); - else if (p->p_sigparent != 0) { - if (p->p_sigparent == SIGCHLD) - childproc_exited(p); - else /* LINUX thread */ - psignal(p->p_pptr, p->p_sigparent); - } + PROC_LOCK(p->p_pptr); +#endif sx_xunlock(&proctree_lock); /* @@ -660,7 +678,7 @@ wait4(struct thread *td, struct wait_arg * rusage. Asserts and will release both the proctree_lock and the process * lock as part of its work. */ -static void +void proc_reap(struct thread *td, struct proc *p, int *status, int options, struct rusage *rusage) { @@ -722,6 +740,10 @@ proc_reap(struct thread *td, struct proc sx_xunlock(&allproc_lock); LIST_REMOVE(p, p_sibling); leavepgrp(p); +#ifdef PROCDESC + if (p->p_procdesc != NULL) + procdesc_reap(p); +#endif sx_xunlock(&proctree_lock); /* Modified: head/sys/kern/kern_fork.c ============================================================================== --- head/sys/kern/kern_fork.c Thu Aug 18 22:20:45 2011 (r224986) +++ head/sys/kern/kern_fork.c Thu Aug 18 22:51:30 2011 (r224987) @@ -40,11 +40,13 @@ __FBSDID("$FreeBSD$"); #include "opt_kdtrace.h" #include "opt_ktrace.h" #include "opt_kstack_pages.h" +#include "opt_procdesc.h" #include #include #include #include +#include #include #include #include @@ -55,6 +57,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -104,7 +107,7 @@ fork(struct thread *td, struct fork_args int error; struct proc *p2; - error = fork1(td, RFFDG | RFPROC, 0, &p2); + error = fork1(td, RFFDG | RFPROC, 0, &p2, NULL, 0); if (error == 0) { td->td_retval[0] = p2->p_pid; td->td_retval[1] = 0; @@ -112,6 +115,34 @@ fork(struct thread *td, struct fork_args return (error); } +/* ARGUSED */ +int +pdfork(td, uap) + struct thread *td; + struct pdfork_args *uap; +{ +#ifdef PROCDESC + int error, fd; + struct proc *p2; + + /* + * It is necessary to return fd by reference because 0 is a valid file + * descriptor number, and the child needs to be able to distinguish + * itself from the parent using the return value. + */ + error = fork1(td, RFFDG | RFPROC | RFPROCDESC, 0, &p2, + &fd, uap->flags); + if (error == 0) { + td->td_retval[0] = p2->p_pid; + td->td_retval[1] = 0; + error = copyout(&fd, uap->fdp, sizeof(fd)); + } + return (error); +#else + return (ENOSYS); +#endif +} + /* ARGSUSED */ int vfork(struct thread *td, struct vfork_args *uap) @@ -124,7 +155,7 @@ vfork(struct thread *td, struct vfork_ar #else flags = RFFDG | RFPROC | RFPPWAIT | RFMEM; #endif - error = fork1(td, flags, 0, &p2); + error = fork1(td, flags, 0, &p2, NULL, 0); if (error == 0) { td->td_retval[0] = p2->p_pid; td->td_retval[1] = 0; @@ -143,7 +174,7 @@ rfork(struct thread *td, struct rfork_ar return (EINVAL); AUDIT_ARG_FFLAGS(uap->flags); - error = fork1(td, uap->flags, 0, &p2); + error = fork1(td, uap->flags, 0, &p2, NULL, 0); if (error == 0) { td->td_retval[0] = p2 ? p2->p_pid : 0; td->td_retval[1] = 0; @@ -337,7 +368,7 @@ fail: static void do_fork(struct thread *td, int flags, struct proc *p2, struct thread *td2, - struct vmspace *vm2) + struct vmspace *vm2, int pdflags) { struct proc *p1, *pptr; int p2_held, trypid; @@ -625,6 +656,16 @@ do_fork(struct thread *td, int flags, st p2->p_vmspace->vm_ssize); } +#ifdef PROCDESC + /* + * Associate the process descriptor with the process before anything + * can happen that might cause that process to need the descriptor. + * However, don't do this until after fork(2) can no longer fail. + */ + if (flags & RFPROCDESC) + procdesc_new(p2, pdflags); +#endif + /* * Both processes are set up, now check if any loadable modules want * to adjust anything. @@ -710,7 +751,8 @@ do_fork(struct thread *td, int flags, st } int -fork1(struct thread *td, int flags, int pages, struct proc **procp) +fork1(struct thread *td, int flags, int pages, struct proc **procp, + int *procdescp, int pdflags) { struct proc *p1; struct proc *newproc; @@ -721,6 +763,9 @@ fork1(struct thread *td, int flags, int int error; static int curfail; static struct timeval lastfail; +#ifdef PROCDESC + struct file *fp_procdesc = NULL; +#endif /* Check for the undefined or unimplemented flags. */ if ((flags & ~(RFFLAGS | RFTSIGFLAGS(RFTSIGMASK))) != 0) @@ -738,6 +783,18 @@ fork1(struct thread *td, int flags, int if ((flags & RFTSIGZMB) != 0 && (u_int)RFTSIGNUM(flags) > _SIG_MAXSIG) return (EINVAL); +#ifdef PROCDESC + if ((flags & RFPROCDESC) != 0) { + /* Can't not create a process yet get a process descriptor. */ + if ((flags & RFPROC) == 0) + return (EINVAL); + + /* Must provide a place to put a procdesc if creating one. */ + if (procdescp == NULL) + return (EINVAL); + } +#endif + p1 = td->td_proc; /* @@ -757,6 +814,25 @@ fork1(struct thread *td, int flags, int return (EAGAIN); #endif +#ifdef PROCDESC + /* + * If required, create a process descriptor in the parent first; we + * will abandon it if something goes wrong. We don't finit() until + * later. + */ + if (flags & RFPROCDESC) { + error = falloc(td, &fp_procdesc, procdescp, 0); + if (error != 0) { +#ifdef RACCT + PROC_LOCK(p1); + racct_sub(p1, RACCT_NPROC, 1); + PROC_UNLOCK(p1); +#endif + return (error); + } + } +#endif + mem_charged = 0; vm2 = NULL; if (pages == 0) @@ -868,12 +944,16 @@ fork1(struct thread *td, int flags, int PROC_UNLOCK(p1); } if (ok) { - do_fork(td, flags, newproc, td2, vm2); + do_fork(td, flags, newproc, td2, vm2, pdflags); /* * Return child proc pointer to parent. */ *procp = newproc; +#ifdef PROCDESC + if (flags & RFPROCDESC) + procdesc_finit(newproc->p_procdesc, fp_procdesc); +#endif return (0); } @@ -892,6 +972,10 @@ fail1: if (vm2 != NULL) vmspace_free(vm2); uma_zfree(proc_zone, newproc); +#ifdef PROCDESC + if (((flags & RFPROCDESC) != 0) && (fp_procdesc != NULL)) + fdrop(fp_procdesc, td); +#endif pause("fork", hz / 2); #ifdef RACCT PROC_LOCK(p1); Modified: head/sys/kern/kern_kthread.c ============================================================================== --- head/sys/kern/kern_kthread.c Thu Aug 18 22:20:45 2011 (r224986) +++ head/sys/kern/kern_kthread.c Thu Aug 18 22:51:30 2011 (r224987) @@ -88,7 +88,7 @@ kproc_create(void (*func)(void *), void panic("kproc_create called too soon"); error = fork1(&thread0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags, - pages, &p2); + pages, &p2, NULL, 0); if (error) return error; Modified: head/sys/kern/kern_sig.c ============================================================================== --- head/sys/kern/kern_sig.c Thu Aug 18 22:20:45 2011 (r224986) +++ head/sys/kern/kern_sig.c Thu Aug 18 22:51:30 2011 (r224987) @@ -41,12 +41,14 @@ __FBSDID("$FreeBSD$"); #include "opt_kdtrace.h" #include "opt_ktrace.h" #include "opt_core.h" +#include "opt_procdesc.h" #include #include #include #include #include +#include #include #include #include @@ -59,6 +61,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -1698,6 +1701,34 @@ kill(struct thread *td, struct kill_args /* NOTREACHED */ } +int +pdkill(td, uap) + struct thread *td; + struct pdkill_args *uap; +{ +#ifdef PROCDESC + struct proc *p; + int error; + + AUDIT_ARG_SIGNUM(uap->signum); + AUDIT_ARG_FD(uap->fd); + if ((u_int)uap->signum > _SIG_MAXSIG) + return (EINVAL); + + error = procdesc_find(td, uap->fd, CAP_PDKILL, &p); + if (error) + return (error); + AUDIT_ARG_PROCESS(p); + error = p_cansignal(td, p, uap->signum); + if (error == 0 && uap->signum) + psignal(p, uap->signum); + PROC_UNLOCK(p); + return (error); +#else + return (ENOSYS); +#endif +} + #if defined(COMPAT_43) #ifndef _SYS_SYSPROTO_H_ struct okillpg_args { Added: head/sys/kern/sys_procdesc.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/kern/sys_procdesc.c Thu Aug 18 22:51:30 2011 (r224987) @@ -0,0 +1,524 @@ +/*- + * Copyright (c) 2009 Robert N. M. Watson + * All rights reserved. + * + * This software was developed at the University of Cambridge Computer + * Laboratory with support from a grant from Google, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/*- + * FreeBSD process descriptor facility. + * + * Some processes are represented by a file descriptor, which will be used in + * preference to signaling and pids for the purposes of process management, + * and is, in effect, a form of capability. When a process descriptor is + * used with a process, it ceases to be visible to certain traditional UNIX + * process facilities, such as waitpid(2). + * + * Some semantics: + * + * - At most one process descriptor will exist for any process, although + * references to that descriptor may be held from many processes (or even + * be in flight between processes over a local domain socket). + * - Last close on the process descriptor will terminate the process using + * SIGKILL and reparent it to init so that there's a process to reap it + * when it's done exiting. + * - If the process exits before the descriptor is closed, it will not + * generate SIGCHLD on termination, or be picked up by waitpid(). + * - The pdkill(2) system call may be used to deliver a signal to the process + * using its process descriptor. + * - The pdwait4(2) system call may be used to block (or not) on a process + * descriptor to collect termination information. + * + * Open questions: + * + * - How to handle ptrace(2)? + * - Will we want to add a pidtoprocdesc(2) system call to allow process + * descriptors to be created for processes without pfork(2)? + */ + +#include +__FBSDID("$FreeBSD$"); + +#include "opt_procdesc.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#ifdef PROCDESC + +FEATURE(process_descriptors, "Process Descriptors"); + +static uma_zone_t procdesc_zone; + +static fo_rdwr_t procdesc_read; +static fo_rdwr_t procdesc_write; +static fo_truncate_t procdesc_truncate; +static fo_ioctl_t procdesc_ioctl; +static fo_poll_t procdesc_poll; +static fo_kqfilter_t procdesc_kqfilter; +static fo_stat_t procdesc_stat; +static fo_close_t procdesc_close; +static fo_chmod_t procdesc_chmod; +static fo_chown_t procdesc_chown; + +static struct fileops procdesc_ops = { + .fo_read = procdesc_read, + .fo_write = procdesc_write, + .fo_truncate = procdesc_truncate, + .fo_ioctl = procdesc_ioctl, + .fo_poll = procdesc_poll, + .fo_kqfilter = procdesc_kqfilter, + .fo_stat = procdesc_stat, + .fo_close = procdesc_close, + .fo_chmod = procdesc_chmod, + .fo_chown = procdesc_chown, + .fo_flags = DFLAG_PASSABLE, +}; + +/* + * Initialize with VFS so that process descriptors are available along with + * other file descriptor types. As long as it runs before init(8) starts, + * there shouldn't be a problem. + */ +static void +procdesc_init(void *dummy __unused) +{ + + procdesc_zone = uma_zcreate("procdesc", sizeof(struct procdesc), + NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); + if (procdesc_zone == NULL) + panic("procdesc_init: procdesc_zone not initialized"); +} +SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_ANY, procdesc_init, NULL); + +/* + * Return a locked process given a process descriptor, or ESRCH if it has + * died. + */ +int +procdesc_find(struct thread *td, int fd, cap_rights_t rights, + struct proc **p) +{ + struct procdesc *pd; + struct file *fp; + int error; + + error = fget(td, fd, rights, &fp); + if (error) + return (error); + if (fp->f_type != DTYPE_PROCDESC) { + error = EBADF; + goto out; + } + pd = fp->f_data; + sx_slock(&proctree_lock); + if (pd->pd_proc != NULL) { + *p = pd->pd_proc; + PROC_LOCK(*p); + } else + error = ESRCH; + sx_sunlock(&proctree_lock); +out: + fdrop(fp, td); + return (error); +} + +/* + * Function to be used by procstat(1) sysctls when returning procdesc + * information. + */ +pid_t +procdesc_pid(struct file *fp_procdesc) +{ + struct procdesc *pd; + + KASSERT(fp_procdesc->f_type == DTYPE_PROCDESC, + ("procdesc_pid: !procdesc")); + + pd = fp_procdesc->f_data; + return (pd->pd_pid); +} + +/* + * Retrieve the PID associated with a process descriptor. + */ +int +kern_pdgetpid(struct thread *td, int fd, cap_rights_t rights, pid_t *pidp) +{ + struct file *fp; *** DIFF OUTPUT TRUNCATED AT 1000 LINES ***