Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys - Add O_CLOEXEC to open(2)



details:   https://anonhg.NetBSD.org/src/rev/dee11110528d
branches:  trunk
changeset: 764017:dee11110528d
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Apr 10 15:45:33 2011 +0000

description:
- Add O_CLOEXEC to open(2)
- Add fd_set_exclose() to encapsulate uses of FIO{,N}CLEX, O_CLOEXEC, F{G,S}ETFD
- Add a pipe1() function to allow passing flags to the fd's that pipe(2)
  opens to ease implementation of linux pipe2(2)
- Factor out fp handling code from open(2) and fhopen(2)

diffstat:

 sys/kern/kern_descrip.c  |   14 +++++-
 sys/kern/sys_descrip.c   |   16 ++----
 sys/kern/sys_generic.c   |   14 +----
 sys/kern/sys_pipe.c      |   16 ++++--
 sys/kern/uipc_syscalls.c |   16 ++++--
 sys/kern/vfs_syscalls.c  |  106 +++++++++++++++++++++-------------------------
 sys/sys/fcntl.h          |    5 +-
 sys/sys/filedesc.h       |    4 +-
 8 files changed, 97 insertions(+), 94 deletions(-)

diffs (truncated from 458 to 300 lines):

diff -r bb0793b4722f -r dee11110528d sys/kern/kern_descrip.c
--- a/sys/kern/kern_descrip.c   Sun Apr 10 15:26:37 2011 +0000
+++ b/sys/kern/kern_descrip.c   Sun Apr 10 15:45:33 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_descrip.c,v 1.211 2011/02/15 15:54:28 pooka Exp $ */
+/*     $NetBSD: kern_descrip.c,v 1.212 2011/04/10 15:45:33 christos 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.211 2011/02/15 15:54:28 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.212 2011/04/10 15:45:33 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1776,6 +1776,16 @@
        return error;
 }
 
+void
+fd_set_exclose(struct lwp *l, int fd, bool exclose)
+{
+       filedesc_t *fdp = l->l_fd;
+       fdfile_t *ff = fdp->fd_dt->dt_ff[fd];
+       ff->ff_exclose = exclose;
+       if (exclose)
+               fdp->fd_exclose = true;
+}
+
 /*
  * Return descriptor owner information. If the value is positive,
  * it's process ID. If it's negative, it's process group ID and
diff -r bb0793b4722f -r dee11110528d sys/kern/sys_descrip.c
--- a/sys/kern/sys_descrip.c    Sun Apr 10 15:26:37 2011 +0000
+++ b/sys/kern/sys_descrip.c    Sun Apr 10 15:45:33 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sys_descrip.c,v 1.19 2010/12/18 01:18:48 rmind Exp $   */
+/*     $NetBSD: sys_descrip.c,v 1.20 2011/04/10 15:45:33 christos Exp $        */
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.19 2010/12/18 01:18:48 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.20 2011/04/10 15:45:33 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -315,7 +315,6 @@
        int fd, i, tmp, error, cmd, newmin;
        filedesc_t *fdp;
        file_t *fp;
-       fdfile_t *ff;
        struct flock fl;
 
        fd = SCARG(uap, fd);
@@ -358,7 +357,6 @@
 
        if ((fp = fd_getfile(fd)) == NULL)
                return (EBADF);
-       ff = fdp->fd_dt->dt_ff[fd];
 
        if ((cmd & F_FSCTL)) {
                error = fcntl_forfs(fd, fp, cmd, SCARG(uap, arg));
@@ -380,16 +378,12 @@
                break;
 
        case F_GETFD:
-               *retval = ff->ff_exclose;
+               *retval = fdp->fd_dt->dt_ff[fd]->ff_exclose;
                break;
 
        case F_SETFD:
-               if ((long)SCARG(uap, arg) & FD_CLOEXEC) {
-                       ff->ff_exclose = true;
-                       fdp->fd_exclose = true;
-               } else {
-                       ff->ff_exclose = false;
-               }
+               fd_set_exclose(l, fd,
+                   ((long)SCARG(uap, arg) & FD_CLOEXEC) != 0);
                break;
 
        case F_GETFL:
diff -r bb0793b4722f -r dee11110528d sys/kern/sys_generic.c
--- a/sys/kern/sys_generic.c    Sun Apr 10 15:26:37 2011 +0000
+++ b/sys/kern/sys_generic.c    Sun Apr 10 15:45:33 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sys_generic.c,v 1.125 2011/01/18 19:52:23 matt Exp $   */
+/*     $NetBSD: sys_generic.c,v 1.126 2011/04/10 15:45:33 christos Exp $       */
 
 /*-
  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.125 2011/01/18 19:52:23 matt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.126 2011/04/10 15:45:33 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -522,20 +522,17 @@
        } */
        struct file     *fp;
        proc_t          *p;
-       struct filedesc *fdp;
        u_long          com;
        int             error;
        size_t          size, alloc_size;
        void            *data, *memp;
 #define        STK_PARAMS      128
        u_long          stkbuf[STK_PARAMS/sizeof(u_long)];
-       fdfile_t        *ff;
 
        memp = NULL;
        alloc_size = 0;
        error = 0;
        p = l->l_proc;
-       fdp = p->p_fd;
 
        if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
                return (EBADF);
@@ -546,15 +543,10 @@
                goto out;
        }
 
-       ff = fdp->fd_dt->dt_ff[SCARG(uap, fd)];
        switch (com = SCARG(uap, com)) {
        case FIONCLEX:
-               ff->ff_exclose = false;
-               goto out;
-
        case FIOCLEX:
-               ff->ff_exclose = true;
-               fdp->fd_exclose = true;
+               fd_set_exclose(l, SCARG(uap, fd), com == FIOCLEX);
                goto out;
        }
 
diff -r bb0793b4722f -r dee11110528d sys/kern/sys_pipe.c
--- a/sys/kern/sys_pipe.c       Sun Apr 10 15:26:37 2011 +0000
+++ b/sys/kern/sys_pipe.c       Sun Apr 10 15:45:33 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sys_pipe.c,v 1.129 2011/01/17 07:13:32 uebayasi Exp $  */
+/*     $NetBSD: sys_pipe.c,v 1.130 2011/04/10 15:45:33 christos Exp $  */
 
 /*-
  * Copyright (c) 2003, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.129 2011/01/17 07:13:32 uebayasi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.130 2011/04/10 15:45:33 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -244,7 +244,7 @@
  * The pipe system call for the DTYPE_PIPE type of pipes
  */
 int
-sys_pipe(struct lwp *l, const void *v, register_t *retval)
+pipe1(struct lwp *l, register_t *retval, int flags)
 {
        struct pipe *rpipe, *wpipe;
        file_t *rf, *wf;
@@ -266,7 +266,7 @@
        if (error)
                goto free2;
        retval[0] = fd;
-       rf->f_flag = FREAD;
+       rf->f_flag = FREAD | flags;
        rf->f_type = DTYPE_PIPE;
        rf->f_data = (void *)rpipe;
        rf->f_ops = &pipeops;
@@ -275,7 +275,7 @@
        if (error)
                goto free3;
        retval[1] = fd;
-       wf->f_flag = FWRITE;
+       wf->f_flag = FWRITE | flags;
        wf->f_type = DTYPE_PIPE;
        wf->f_data = (void *)wpipe;
        wf->f_ops = &pipeops;
@@ -295,6 +295,12 @@
        return (error);
 }
 
+int
+sys_pipe(struct lwp *l, const void *v, register_t *retval)
+{
+       return pipe1(l, retval, 0);
+}
+
 /*
  * Allocate kva for pipe circular buffer, the space is pageable
  * This routine will 'realloc' the size of a pipe safely, if it fails
diff -r bb0793b4722f -r dee11110528d sys/kern/uipc_syscalls.c
--- a/sys/kern/uipc_syscalls.c  Sun Apr 10 15:26:37 2011 +0000
+++ b/sys/kern/uipc_syscalls.c  Sun Apr 10 15:45:33 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: uipc_syscalls.c,v 1.141 2010/04/23 15:19:19 rmind Exp $        */
+/*     $NetBSD: uipc_syscalls.c,v 1.142 2011/04/10 15:45:33 christos Exp $     */
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.141 2010/04/23 15:19:19 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.142 2011/04/10 15:45:33 christos Exp $");
 
 #include "opt_pipe.h"
 
@@ -960,7 +960,7 @@
 #ifdef PIPE_SOCKETPAIR
 /* ARGSUSED */
 int
-sys_pipe(struct lwp *l, const void *v, register_t *retval)
+pipe1(struct lwp *l, register_t *retval, int flags)
 {
        file_t          *rf, *wf;
        struct socket   *rso, *wso;
@@ -978,13 +978,13 @@
        if ((error = fd_allocfile(&rf, &fd)) != 0)
                goto free2;
        retval[0] = fd;
-       rf->f_flag = FREAD;
+       rf->f_flag = FREAD | flags;
        rf->f_type = DTYPE_SOCKET;
        rf->f_ops = &socketops;
        rf->f_data = rso;
        if ((error = fd_allocfile(&wf, &fd)) != 0)
                goto free3;
-       wf->f_flag = FWRITE;
+       wf->f_flag = FWRITE | flags;
        wf->f_type = DTYPE_SOCKET;
        wf->f_ops = &socketops;
        wf->f_data = wso;
@@ -1007,6 +1007,12 @@
        (void)soclose(rso);
        return (error);
 }
+
+int
+sys_pipe(struct lwp *l, const void *v, register_t *retval)
+{
+       return pipe1(l, retval, 0);
+}
 #endif /* PIPE_SOCKETPAIR */
 
 /*
diff -r bb0793b4722f -r dee11110528d sys/kern/vfs_syscalls.c
--- a/sys/kern/vfs_syscalls.c   Sun Apr 10 15:26:37 2011 +0000
+++ b/sys/kern/vfs_syscalls.c   Sun Apr 10 15:45:33 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: vfs_syscalls.c,v 1.421 2011/04/02 04:57:35 rmind Exp $ */
+/*     $NetBSD: vfs_syscalls.c,v 1.422 2011/04/10 15:45:33 christos Exp $      */
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.421 2011/04/02 04:57:35 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.422 2011/04/10 15:45:33 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_fileassoc.h"
@@ -140,6 +140,45 @@
 const int nmountcompatnames = __arraycount(mountcompatnames);
 
 static int
+open_setfp(struct lwp *l, file_t *fp, struct vnode *vp, int indx, int flags)
+{
+       int error;
+
+       fp->f_flag = flags & FMASK;
+       fp->f_type = DTYPE_VNODE;
+       fp->f_ops = &vnops;
+       fp->f_data = vp;
+
+       if (flags & (O_EXLOCK | O_SHLOCK)) {
+               struct flock lf;
+               int type;
+
+               lf.l_whence = SEEK_SET;
+               lf.l_start = 0;
+               lf.l_len = 0;
+               if (flags & O_EXLOCK)
+                       lf.l_type = F_WRLCK;
+               else



Home | Main Index | Thread Index | Old Index