From owner-freebsd-hackers@FreeBSD.ORG Fri Jan 13 03:08:31 2012 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 45BBA106566C; Fri, 13 Jan 2012 03:08:31 +0000 (UTC) (envelope-from lists@eitanadler.com) Received: from mail-lpp01m010-f54.google.com (mail-lpp01m010-f54.google.com [209.85.215.54]) by mx1.freebsd.org (Postfix) with ESMTP id 62DC68FC13; Fri, 13 Jan 2012 03:08:30 +0000 (UTC) Received: by lahd3 with SMTP id d3so48012lah.13 for ; Thu, 12 Jan 2012 19:08:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eitanadler.com; s=0xdeadbeef; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=JfNgc/L/G6kDfjN9JmLyuJTfnlNZ0GNwNmsYfFow9A4=; b=bvjObFl3nkYixT7QAMdAWffxyT3X7dzKqMqQ6Ac0xJneqRiYkkjogdCXvWF0kMk8wq rmWQZPMAgsaBylLR19ry0SajSMohjrosL7bvU801oWexslNSJLoomZ8SnP25VZcbPDVx WOFTEbYPs7oZfvbuf/UpSn+kOU8wRob9AcUSg= Received: by 10.112.83.197 with SMTP id s5mr132891lby.9.1326424109130; Thu, 12 Jan 2012 19:08:29 -0800 (PST) MIME-Version: 1.0 Received: by 10.112.17.163 with HTTP; Thu, 12 Jan 2012 19:07:58 -0800 (PST) In-Reply-To: References: <20120112100840.GV31224@deviant.kiev.zoral.com.ua> From: Eitan Adler Date: Thu, 12 Jan 2012 22:07:58 -0500 Message-ID: To: Kostik Belousov Content-Type: text/plain; charset=UTF-8 Cc: jilles@freebsd.org, FreeBSD Hackers , Colin Percival Subject: Re: dup3 syscall - atomic set O_CLOEXEC with dup2 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 13 Jan 2012 03:08:31 -0000 Okay - here is version 2 (compile and run tested) Index: sys/kern/kern_descrip.c =================================================================== --- sys/kern/kern_descrip.c (revision 229830) +++ sys/kern/kern_descrip.c (working copy) @@ -110,6 +110,7 @@ /* Flags for do_dup() */ #define DUP_FIXED 0x1 /* Force fixed allocation */ #define DUP_FCNTL 0x2 /* fcntl()-style errors */ +#define DUP_CLOEXEC 0x4 /* Enable O_CLOEXEC on the new fs */ static int do_dup(struct thread *td, int flags, int old, int new, register_t *retval); @@ -307,7 +308,36 @@ return (0); } +struct dup3_args { + u_int from; + u_int to; + int flags; +}; + /* + * Duplicate a file descriptor and allow for O_CLOEXEC + */ + +int +sys_dup3(struct thread * td, struct dup3_args * uap) { + int dupflags; + + if (uap->from == uap->to) + return (EINVAL); + + if (uap->flags & ~O_CLOEXEC) + return (EINVAL); + + dupflags = DUP_FIXED; + if (uap->flags & O_CLOEXEC) + dupflags |= DUP_CLOEXEC; + + return (do_dup(td, dupflags, (int)uap->from, (int)uap->to, + td->td_retval)); + return (0); +} + +/* * Duplicate a file descriptor to a particular value. * * Note: keep in mind that a potential race condition exists when closing @@ -912,6 +942,9 @@ fdp->fd_lastfile = new; *retval = new; + if (flags & DUP_CLOEXEC) + fdp->fd_ofileflags[new] |= UF_EXCLOSE; + /* * If we dup'd over a valid file, we now own the reference to it * and must dispose of it using closef() semantics (as if a Index: sys/kern/syscalls.master =================================================================== --- sys/kern/syscalls.master (revision 229830) +++ sys/kern/syscalls.master (working copy) @@ -951,5 +951,6 @@ off_t offset, off_t len); } 531 AUE_NULL STD { int posix_fadvise(int fd, off_t offset, \ off_t len, int advice); } +532 AUE_NULL STD { int dup3(u_int from, u_int to, int flags); } ; Please copy any additions and changes to the following compatability tables: ; sys/compat/freebsd32/syscalls.master Index: sys/compat/freebsd32/syscalls.master =================================================================== --- sys/compat/freebsd32/syscalls.master (revision 229830) +++ sys/compat/freebsd32/syscalls.master (working copy) @@ -997,3 +997,4 @@ uint32_t offset1, uint32_t offset2,\ uint32_t len1, uint32_t len2, \ int advice); } +532 AUE_NULL STD { int dup3(u_int from, u_int to, int flags); } Index: lib/libc/sys/Symbol.map =================================================================== --- lib/libc/sys/Symbol.map (revision 229830) +++ lib/libc/sys/Symbol.map (working copy) @@ -383,6 +383,7 @@ FBSD_1.3 { posix_fadvise; + dup3; }; FBSDprivate_1.0 { -- Eitan Adler