From owner-freebsd-standards@FreeBSD.ORG Wed Jul 18 08:50:11 2012 Return-Path: Delivered-To: freebsd-standards@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C7589106566B for ; Wed, 18 Jul 2012 08:50:11 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id A01AF8FC0C for ; Wed, 18 Jul 2012 08:50:11 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.5/8.14.5) with ESMTP id q6I8oBFm098816 for ; Wed, 18 Jul 2012 08:50:11 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.5/8.14.5/Submit) id q6I8oBWu098815; Wed, 18 Jul 2012 08:50:11 GMT (envelope-from gnats) Resent-Date: Wed, 18 Jul 2012 08:50:11 GMT Resent-Message-Id: <201207180850.q6I8oBWu098815@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-standards@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Jukka Ukkonen Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 025911065687 for ; Wed, 18 Jul 2012 08:40:26 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22]) by mx1.freebsd.org (Postfix) with ESMTP id D84E38FC0A for ; Wed, 18 Jul 2012 08:40:25 +0000 (UTC) Received: from red.freebsd.org (localhost [127.0.0.1]) by red.freebsd.org (8.14.4/8.14.4) with ESMTP id q6I8ePf3056274 for ; Wed, 18 Jul 2012 08:40:25 GMT (envelope-from nobody@red.freebsd.org) Received: (from nobody@localhost) by red.freebsd.org (8.14.4/8.14.4/Submit) id q6I8ePQh056273; Wed, 18 Jul 2012 08:40:25 GMT (envelope-from nobody) Message-Id: <201207180840.q6I8ePQh056273@red.freebsd.org> Date: Wed, 18 Jul 2012 08:40:25 GMT From: Jukka Ukkonen To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Cc: Subject: standards/169962: fcntl() to support F_DUPFD_CLOEXEC specified in POSIX.1-2008 X-BeenThere: freebsd-standards@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Standards compliance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Jul 2012 08:50:11 -0000 >Number: 169962 >Category: standards >Synopsis: fcntl() to support F_DUPFD_CLOEXEC specified in POSIX.1-2008 >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-standards >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Wed Jul 18 08:50:11 UTC 2012 >Closed-Date: >Last-Modified: >Originator: Jukka Ukkonen >Release: FreeBSD 9.1-PRERELEASE >Organization: ----- >Environment: FreeBSD sleipnir 9.1-PRERELEASE FreeBSD 9.1-PRERELEASE #0: Wed Jul 18 10:49:35 EEST 2012 root@sleipnir:/usr/obj/usr/src/sys/Sleipnir amd64 >Description: The attached patch adds the flag F_DUPFD_CLOEXEC to fcntl() to allow duplication of file descriptors such that FD_CLOEXEC will be automatically and atomically set for the new fd. While in the process of adding F_DUPFD_CLOEXEC which is required by POSIX.1-2008 I also added F_DUP2FD_CLOEXEC for symmetry and analogy with F_DUPFD<->F_DUP2FD. >How-To-Repeat: See full description above. >Fix: Find a patch attached. Patch attached with submission follows: --- sys/sys/fcntl.h.orig 2012-07-18 10:37:07.000000000 +0300 +++ sys/sys/fcntl.h 2012-07-18 10:42:27.000000000 +0300 @@ -225,6 +225,8 @@ #define F_SETLK_REMOTE 14 /* debugging support for remote locks */ #define F_READAHEAD 15 /* read ahead */ #define F_RDAHEAD 16 /* Darwin compatible read ahead */ +#define F_DUPFD_CLOEXEC 17 /* POSIX.1-2008 */ +#define F_DUP2FD_CLOEXEC 18 /* Symmetry DUPFD<->DUP2FD */ /* file descriptor flags (F_GETFD, F_SETFD) */ #define FD_CLOEXEC 1 /* close-on-exec flag */ --- sys/kern/kern_descrip.c.orig 2012-07-18 10:33:57.000000000 +0300 +++ sys/kern/kern_descrip.c 2012-07-18 10:34:45.000000000 +0300 @@ -113,6 +113,7 @@ /* Flags for do_dup() */ #define DUP_FIXED 0x1 /* Force fixed allocation */ #define DUP_FCNTL 0x2 /* fcntl()-style errors */ +#define DUP_CLOEXEC 0x4 /* Atomically set FD_CLOEXEC. */ static int do_dup(struct thread *td, int flags, int old, int new, register_t *retval); @@ -490,6 +491,18 @@ error = do_dup(td, DUP_FIXED, fd, tmp, td->td_retval); break; + case F_DUPFD_CLOEXEC: + tmp = arg; + error = do_dup(td, (DUP_FCNTL|DUP_CLOEXEC), + fd, tmp, td->td_retval); + break; + + case F_DUP2FD_CLOEXEC: + tmp = arg; + error = do_dup(td, (DUP_FIXED|DUP_CLOEXEC), + fd, tmp, td->td_retval); + break; + case F_GETFD: FILEDESC_SLOCK(fdp); if ((fp = fdtofp(fd, fdp)) == NULL) { @@ -826,6 +839,19 @@ } if (flags & DUP_FIXED && old == new) { *retval = new; + + /* + * NOTICE! - We err on the safe side. + * The principle is least surprise, least confusion. + * If we dup the fd to itself explicitly requesting CLOEXEC, + * the flag shall be added. + * If there is no explicit request to set CLOEXEC but + * the fd already had it set, we shall not touch it. + */ + + if (flags & DUP_CLOEXEC) + fdp->fd_ofileflags[new] |= UF_EXCLOSE; + FILEDESC_XUNLOCK(fdp); return (0); } @@ -911,7 +937,12 @@ * Duplicate the source descriptor */ fdp->fd_ofiles[new] = fp; - fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE; + + if (flags & DUP_CLOEXEC) + fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] | UF_EXCLOSE; + else + fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE; + if (new > fdp->fd_lastfile) fdp->fd_lastfile = new; *retval = new; >Release-Note: >Audit-Trail: >Unformatted: