Similar to O_CLOEXEC, F_DUPFD_CLOEXEC allows you to dup a file
descriptor and atomically have FD_CLOEXEC set.  (Also POSIX Issue 7.)

ok?

Index: sys/fcntl.h
===================================================================
RCS file: /home/mdempsky/anoncvs/cvs/src/sys/sys/fcntl.h,v
retrieving revision 1.14
diff -u -p -r1.14 fcntl.h
--- sys/fcntl.h 8 Jul 2011 04:23:24 -0000       1.14
+++ sys/fcntl.h 8 Jul 2011 19:08:37 -0000
@@ -153,6 +156,7 @@
 #define        F_GETLK         7               /* get record locking 
information */
 #define        F_SETLK         8               /* set record locking 
information */
 #define        F_SETLKW        9               /* F_SETLK; wait if blocked */
+#define        F_DUPFD_CLOEXEC 10              /* duplicate with FD_CLOEXEC 
set */
 
 /* file descriptor flags (F_GETFD, F_SETFD) */
 #define        FD_CLOEXEC      1               /* close-on-exec flag */
Index: kern/kern_descrip.c
===================================================================
RCS file: /home/mdempsky/anoncvs/cvs/src/sys/kern/kern_descrip.c,v
retrieving revision 1.86
diff -u -p -r1.86 kern_descrip.c
--- kern/kern_descrip.c 2 Apr 2011 17:04:35 -0000       1.86
+++ kern/kern_descrip.c 8 Jul 2011 19:06:26 -0000
@@ -312,6 +312,7 @@ restart:
        switch (SCARG(uap, cmd)) {
 
        case F_DUPFD:
+       case F_DUPFD_CLOEXEC:
                newmin = (long)SCARG(uap, arg);
                if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
                    (u_int)newmin >= maxfiles) {
@@ -332,6 +333,9 @@ restart:
                        error = finishdup(p, fp, fd, i, retval);
                else
                        FRELE(fp);
+
+               if (!error && SCARG(uap, cmd) == F_DUPFD_CLOEXEC)
+                       fdp->fd_ofileflags[i] |= UF_EXCLOSE;
 
                fdpunlock(fdp);
                return (error);

Reply via email to