Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 12 Jan 2012 10:06:45 +0100
From:      Davide Italiano <davide.italiano@gmail.com>
To:        Eitan Adler <lists@eitanadler.com>
Cc:        jilles@freebsd.org, FreeBSD Hackers <freebsd-hackers@freebsd.org>, Colin Percival <cperciva@freebsd.org>
Subject:   Re: dup3 syscall - atomic set O_CLOEXEC with dup2
Message-ID:  <CACYV=-GfH31V_oU_gLWV1SjOLWYDoqW7V5g8kee%2Bwa2P9im2kA@mail.gmail.com>
In-Reply-To: <CAF6rxg=EjkwFbXQt3i2Nnz6_dcZtdek-2YdqyZnAdVPxVaWR_Q@mail.gmail.com>
References:  <CAF6rxg=EjkwFbXQt3i2Nnz6_dcZtdek-2YdqyZnAdVPxVaWR_Q@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Jan 12, 2012 at 6:01 AM, Eitan Adler <lists@eitanadler.com> 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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CACYV=-GfH31V_oU_gLWV1SjOLWYDoqW7V5g8kee%2Bwa2P9im2kA>