From owner-freebsd-hackers@FreeBSD.ORG Thu Jan 12 09:35:17 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 BBB0F1065672; Thu, 12 Jan 2012 09:35:17 +0000 (UTC) (envelope-from davide.italiano@gmail.com) Received: from mail-vx0-f182.google.com (mail-vx0-f182.google.com [209.85.220.182]) by mx1.freebsd.org (Postfix) with ESMTP id 5D0AF8FC1A; Thu, 12 Jan 2012 09:35:17 +0000 (UTC) Received: by vcbfk1 with SMTP id fk1so1987936vcb.13 for ; Thu, 12 Jan 2012 01:35:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=Bh8XzL0vRfa5CHUT+FvcOGekYpYfDC70p0IQiQYtgMQ=; b=WowQS6LsuelmUGwcEn0oznj1aY2mNt4+77MtWuVpQ56tUq7jfH2dHcf9zRibdxT7fZ edWnrd90z2HdmcI/7OaXNT1ZmiHvsxwW0fgpNb8b18zMIstCk/R+IsmUg7vACTCF/Bxh HrM+k7fziSmUlirzCGHn1SBZbUQDFyT15Z628= MIME-Version: 1.0 Received: by 10.220.148.138 with SMTP id p10mr1571103vcv.28.1326359205204; Thu, 12 Jan 2012 01:06:45 -0800 (PST) Received: by 10.52.181.199 with HTTP; Thu, 12 Jan 2012 01:06:45 -0800 (PST) In-Reply-To: References: Date: Thu, 12 Jan 2012 10:06:45 +0100 Message-ID: From: Davide Italiano To: Eitan Adler Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: Thu, 12 Jan 2012 09:35:17 -0000 On Thu, Jan 12, 2012 at 6:01 AM, Eitan Adler wrote: > This is an implementation of dup3 for FreeBSD: > man page here (with a FreeBSD patch coming soon): > https://www.kernel.org/doc/man-pages/online/pages/man2/dup.2.html > > Is this implementation correct? If so any objection to adding this as > a supported syscall? > > > Index: sys/kern/kern_descrip.c > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > --- sys/kern/kern_descrip.c =A0 =A0 (revision 229830) > +++ sys/kern/kern_descrip.c =A0 =A0 (working copy) > @@ -110,6 +110,7 @@ > =A0/* Flags for do_dup() */ > =A0#define DUP_FIXED =A0 =A0 =A00x1 =A0 =A0 /* Force fixed allocation */ > =A0#define DUP_FCNTL =A0 =A0 =A00x2 =A0 =A0 /* fcntl()-style errors */ > +#define DUP_CLOEXEC =A0 =A00x4 =A0 =A0 /* Enable O_CLOEXEC on the new fs= */ > > =A0static int do_dup(struct thread *td, int flags, int old, int new, > =A0 =A0 register_t *retval); > @@ -307,7 +308,39 @@ > =A0 =A0 =A0 =A0return (0); > =A0} > > +#ifndef _SYS_SYSPROTO_H_ > +struct dup3_args { > + =A0 =A0 =A0 u_int =A0 from; > + =A0 =A0 =A0 u_int =A0 to; > + =A0 =A0 =A0 int =A0 =A0 flags; > +}; > +#endif > + > =A0/* > + * Duplicate a file descriptor and allow for O_CLOEXEC > + */ > + > +/* ARGSUSED */ > +int > +sys_dup3(struct thread * const td, struct dup3_args * const uap) { > + > + =A0 =A0 =A0 KASSERT(td !=3D NULL, ("%s: td =3D=3D NULL", __func__)); > + =A0 =A0 =A0 KASSERT(uap !=3D NULL, ("%s: uap =3D=3D NULL", __func__)); > + > + =A0 =A0 =A0 if (uap->from =3D=3D uap->to) > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 return EINVAL; > + > + =A0 =A0 =A0 if (uap->flags & ~O_CLOEXEC) > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 return EINVAL; > + > + =A0 =A0 =A0 const int dupflags =3D (uap->flags =3D=3D O_CLOEXEC) ? > DUP_FIXED|DUP_CLOEXEC : DUP_FIXED; > + > + =A0 =A0 =A0 return (do_dup(td, dupflags, (int)uap->from, (int)uap->to, > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 td->td_retval)); > + =A0 =A0 =A0 return (0); > +} > + > +/* > =A0* Duplicate a file descriptor to a particular value. > =A0* > =A0* Note: keep in mind that a potential race condition exists when closi= ng > @@ -912,6 +945,9 @@ > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0fdp->fd_lastfile =3D new; > =A0 =A0 =A0 =A0*retval =3D new; > > + =A0 =A0 =A0 if (flags & DUP_CLOEXEC) > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 fdp->fd_ofileflags[new] |=3D UF_EXCLOSE; > + > =A0 =A0 =A0 =A0/* > =A0 =A0 =A0 =A0 * If we dup'd over a valid file, we now own the reference= to it > =A0 =A0 =A0 =A0 * and must dispose of it using closef() semantics (as if = a > Index: sys/kern/syscalls.master > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > --- sys/kern/syscalls.master =A0 =A0(revision 229830) > +++ sys/kern/syscalls.master =A0 =A0(working copy) > @@ -951,5 +951,6 @@ > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0of= f_t offset, off_t len); } > =A0531 =A0 =A0AUE_NULL =A0 =A0 =A0 =A0STD =A0 =A0 { int posix_fadvise(int= fd, off_t offset, \ > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0of= f_t len, int advice); } > +532 =A0 =A0AUE_NULL =A0 =A0 =A0 =A0STD =A0 =A0 { int dup3(u_int from, u_= int to, int flags); } > =A0; Please copy any additions and changes to the following compatability= tables: > =A0; sys/compat/freebsd32/syscalls.master > Index: sys/compat/freebsd32/syscalls.master > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > --- sys/compat/freebsd32/syscalls.master =A0 =A0 =A0 =A0(revision 229830) > +++ sys/compat/freebsd32/syscalls.master =A0 =A0 =A0 =A0(working copy) > @@ -997,3 +997,4 @@ > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ui= nt32_t offset1, uint32_t offset2,\ > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ui= nt32_t len1, uint32_t len2, \ > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0in= t advice); } > +532 =A0 =A0AUE_NULL =A0 =A0 =A0 =A0STD =A0 =A0 { int dup3(u_int from, u_= int to, int flags); } > > > -- > Eitan Adler > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org= " >From what I can see it seems that dup3() is Linux specific and not POSIX, so maybe there are some issues in adding it, but I may be wrong. Davide