Module Name: src Committed By: pooka Date: Tue Feb 15 15:54:29 UTC 2011
Modified Files: src/sys/kern: kern_descrip.c kern_exec.c subr_exec_fd.c vfs_cwd.c src/sys/rump/librump/rumpkern: rump.c src/sys/sys: filedesc.h Log Message: Support FD_CLOEXEC in rump kernels. To generate a diff of this commit: cvs rdiff -u -r1.210 -r1.211 src/sys/kern/kern_descrip.c cvs rdiff -u -r1.305 -r1.306 src/sys/kern/kern_exec.c cvs rdiff -u -r1.4 -r1.5 src/sys/kern/subr_exec_fd.c cvs rdiff -u -r1.3 -r1.4 src/sys/kern/vfs_cwd.c cvs rdiff -u -r1.230 -r1.231 src/sys/rump/librump/rumpkern/rump.c cvs rdiff -u -r1.57 -r1.58 src/sys/sys/filedesc.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/kern/kern_descrip.c diff -u src/sys/kern/kern_descrip.c:1.210 src/sys/kern/kern_descrip.c:1.211 --- src/sys/kern/kern_descrip.c:1.210 Fri Jan 28 18:44:44 2011 +++ src/sys/kern/kern_descrip.c Tue Feb 15 15:54:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_descrip.c,v 1.210 2011/01/28 18:44:44 pooka Exp $ */ +/* $NetBSD: kern_descrip.c,v 1.211 2011/02/15 15:54:28 pooka Exp $ */ /*- * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. @@ -70,7 +70,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.210 2011/01/28 18:44:44 pooka Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.211 2011/02/15 15:54:28 pooka Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -1691,6 +1691,57 @@ } /* + * Close open files on exec. + */ +void +fd_closeexec(void) +{ + proc_t *p; + filedesc_t *fdp; + fdfile_t *ff; + lwp_t *l; + fdtab_t *dt; + int fd; + + l = curlwp; + p = l->l_proc; + fdp = p->p_fd; + + if (fdp->fd_refcnt > 1) { + fdp = fd_copy(); + fd_free(); + p->p_fd = fdp; + l->l_fd = fdp; + } + if (!fdp->fd_exclose) { + return; + } + fdp->fd_exclose = false; + dt = fdp->fd_dt; + + for (fd = 0; fd <= fdp->fd_lastfile; fd++) { + if ((ff = dt->dt_ff[fd]) == NULL) { + KASSERT(fd >= NDFDFILE); + continue; + } + KASSERT(fd >= NDFDFILE || + ff == (fdfile_t *)fdp->fd_dfdfile[fd]); + if (ff->ff_file == NULL) + continue; + if (ff->ff_exclose) { + /* + * We need a reference to close the file. + * No other threads can see the fdfile_t at + * this point, so don't bother locking. + */ + KASSERT((ff->ff_refcnt & FR_CLOSING) == 0); + ff->ff_refcnt++; + fd_close(fd); + } + } +} + +/* * Sets descriptor owner. If the owner is a process, 'pgid' * is set to positive value, process ID. If the owner is process group, * 'pgid' is set to -pg_id. Index: src/sys/kern/kern_exec.c diff -u src/sys/kern/kern_exec.c:1.305 src/sys/kern/kern_exec.c:1.306 --- src/sys/kern/kern_exec.c:1.305 Tue Jan 18 08:21:03 2011 +++ src/sys/kern/kern_exec.c Tue Feb 15 15:54:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_exec.c,v 1.305 2011/01/18 08:21:03 matt Exp $ */ +/* $NetBSD: kern_exec.c,v 1.306 2011/02/15 15:54:28 pooka Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -59,7 +59,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.305 2011/01/18 08:21:03 matt Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.306 2011/02/15 15:54:28 pooka Exp $"); #include "opt_ktrace.h" #include "opt_modular.h" @@ -951,6 +951,7 @@ goto exec_abort; } + cwdexec(); fd_closeexec(); /* handle close on exec */ execsigs(p); /* reset catched signals */ Index: src/sys/kern/subr_exec_fd.c diff -u src/sys/kern/subr_exec_fd.c:1.4 src/sys/kern/subr_exec_fd.c:1.5 --- src/sys/kern/subr_exec_fd.c:1.4 Fri Nov 19 06:44:43 2010 +++ src/sys/kern/subr_exec_fd.c Tue Feb 15 15:54:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: subr_exec_fd.c,v 1.4 2010/11/19 06:44:43 dholland Exp $ */ +/* $NetBSD: subr_exec_fd.c,v 1.5 2011/02/15 15:54:28 pooka Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -26,12 +26,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ -/* - * File descriptor related subroutines for exec. - */ - #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: subr_exec_fd.c,v 1.4 2010/11/19 06:44:43 dholland Exp $"); +__KERNEL_RCSID(0, "$NetBSD: subr_exec_fd.c,v 1.5 2011/02/15 15:54:28 pooka Exp $"); #include <sys/param.h> #include <sys/file.h> @@ -42,63 +38,6 @@ #include <sys/vnode.h> /* - * Close open files on exec. - */ -void -fd_closeexec(void) -{ - proc_t *p; - filedesc_t *fdp; - fdfile_t *ff; - lwp_t *l; - fdtab_t *dt; - int fd; - - l = curlwp; - p = l->l_proc; - fdp = p->p_fd; - - cwdunshare(p); - - if (p->p_cwdi->cwdi_edir) { - vrele(p->p_cwdi->cwdi_edir); - } - - if (fdp->fd_refcnt > 1) { - fdp = fd_copy(); - fd_free(); - p->p_fd = fdp; - l->l_fd = fdp; - } - if (!fdp->fd_exclose) { - return; - } - fdp->fd_exclose = false; - dt = fdp->fd_dt; - - for (fd = 0; fd <= fdp->fd_lastfile; fd++) { - if ((ff = dt->dt_ff[fd]) == NULL) { - KASSERT(fd >= NDFDFILE); - continue; - } - KASSERT(fd >= NDFDFILE || - ff == (fdfile_t *)fdp->fd_dfdfile[fd]); - if (ff->ff_file == NULL) - continue; - if (ff->ff_exclose) { - /* - * We need a reference to close the file. - * No other threads can see the fdfile_t at - * this point, so don't bother locking. - */ - KASSERT((ff->ff_refcnt & FR_CLOSING) == 0); - ff->ff_refcnt++; - fd_close(fd); - } - } -} - -/* * It is unsafe for set[ug]id processes to be started with file * descriptors 0..2 closed, as these descriptors are given implicit * significance in the Standard C library. fdcheckstd() will create a Index: src/sys/kern/vfs_cwd.c diff -u src/sys/kern/vfs_cwd.c:1.3 src/sys/kern/vfs_cwd.c:1.4 --- src/sys/kern/vfs_cwd.c:1.3 Fri Jan 8 11:35:10 2010 +++ src/sys/kern/vfs_cwd.c Tue Feb 15 15:54:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: vfs_cwd.c,v 1.3 2010/01/08 11:35:10 pooka Exp $ */ +/* $NetBSD: vfs_cwd.c,v 1.4 2011/02/15 15:54:28 pooka Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: vfs_cwd.c,v 1.3 2010/01/08 11:35:10 pooka Exp $"); +__KERNEL_RCSID(0, "$NetBSD: vfs_cwd.c,v 1.4 2011/02/15 15:54:28 pooka Exp $"); #include <sys/param.h> #include <sys/atomic.h> @@ -148,3 +148,14 @@ vrele(cwdi->cwdi_edir); pool_cache_put(cwdi_cache, cwdi); } + +void +cwdexec(struct proc *p) +{ + + cwdunshare(p); + + if (p->p_cwdi->cwdi_edir) { + vrele(p->p_cwdi->cwdi_edir); + } +} Index: src/sys/rump/librump/rumpkern/rump.c diff -u src/sys/rump/librump/rumpkern/rump.c:1.230 src/sys/rump/librump/rumpkern/rump.c:1.231 --- src/sys/rump/librump/rumpkern/rump.c:1.230 Tue Feb 15 10:35:05 2011 +++ src/sys/rump/librump/rumpkern/rump.c Tue Feb 15 15:54:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: rump.c,v 1.230 2011/02/15 10:35:05 pooka Exp $ */ +/* $NetBSD: rump.c,v 1.231 2011/02/15 15:54:28 pooka Exp $ */ /* * Copyright (c) 2007 Antti Kantee. All Rights Reserved. @@ -28,7 +28,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.230 2011/02/15 10:35:05 pooka Exp $"); +__KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.231 2011/02/15 15:54:28 pooka Exp $"); #include <sys/systm.h> #define ELFSIZE ARCH_ELFSIZE @@ -786,10 +786,8 @@ { struct proc *p = curproc; + fd_closeexec(); strlcpy(p->p_comm, comm, sizeof(p->p_comm)); - - /* TODO: apply CLOEXEC */ - /* TODO: other stuff? */ } static void Index: src/sys/sys/filedesc.h diff -u src/sys/sys/filedesc.h:1.57 src/sys/sys/filedesc.h:1.58 --- src/sys/sys/filedesc.h:1.57 Tue Oct 27 02:58:28 2009 +++ src/sys/sys/filedesc.h Tue Feb 15 15:54:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: filedesc.h,v 1.57 2009/10/27 02:58:28 rmind Exp $ */ +/* $NetBSD: filedesc.h,v 1.58 2011/02/15 15:54:28 pooka Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -211,6 +211,7 @@ void cwdshare(proc_t *); void cwdunshare(proc_t *); void cwdfree(struct cwdinfo *); +void cwdexec(struct proc *); #define GETCWD_CHECK_ACCESS 0x0001 int getcwd_common(struct vnode *, struct vnode *, char **, char *, int,