From: Sascha Wildner Date: Mon, 6 May 2019 16:53:26 +0000 (+0200) Subject: libc: Add dup3() from FreeBSD. X-Git-Tag: v5.7.0~121 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/a0db5952c182198feb29dd55b977177dcf3822e3 libc: Add dup3() from FreeBSD. As proposed in http://austingroupbugs.net/view.php?id=411 It seems to help with various ports. Pointed-out-by: zrj --- diff --git a/include/unistd.h b/include/unistd.h index faf73f1cb1..6c6482bf6a 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -522,6 +522,7 @@ int closefrom(int); const char * crypt_get_format(void); int crypt_set_format(const char *); +int dup3(int, int, int); int eaccess(const char *, int); void endusershell(void); int exect(const char *, char * const *, char * const *); diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index 5d95dfc4d4..08bdf7cf2a 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -12,7 +12,7 @@ SRCS+= _once_stub.c _pthread_stubs.c _rand48.c _spinlock_stub.c \ _thread_init.c alarm.c arc4random.c assert.c basename.c \ clock.c clock_getcpuclockid.c closedir.c confstr.c creat.c \ ctermid.c daemon.c devname.c dirfd.c dirname.c disklabel.c disktab.c \ - dlfcn.c drand48.c elf_utils.c erand48.c err.c errlst.c exec.c \ + dlfcn.c drand48.c dup3.c elf_utils.c erand48.c err.c errlst.c exec.c \ fdevname.c fmtcheck.c fmtmsg.c fnmatch.c fpclassify.c \ frexp.c fstab.c ftok.c fts.c ftw.c getbootfile.c getbsize.c \ getcap.c getcwd.c getdevpath.c getdomainname.c \ @@ -56,7 +56,7 @@ MAN+= alarm.3 arc4random.3 clock.3 \ clock_getcpuclockid.3 confstr.3 creat.3 ctermid.3 daemon.3 \ devname.3 directory.3 dirname.3 \ dladdr.3 dlinfo.3 dlopen.3 \ - dlclose.3 dlerror.3 dlfcn.3 dlsym.3 dlvsym.3 dl_iterate_phdr.3 \ + dlclose.3 dlerror.3 dlfcn.3 dlsym.3 dlvsym.3 dl_iterate_phdr.3 dup3.3 \ endutxent.3 err.3 exec.3 \ fmtcheck.3 fmtmsg.3 fnmatch.3 frexp.3 ftok.3 fts.3 ftw.3 \ getbootfile.3 getbsize.3 getcap.3 getcontext.3 getcwd.3 \ diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map index 554cf80ae4..05cec48eeb 100644 --- a/lib/libc/gen/Symbol.map +++ b/lib/libc/gen/Symbol.map @@ -492,6 +492,10 @@ DF504.0 { waitid; }; +DF506.0 { + dup3; +}; + DFprivate_1.0 { /* pthread stubs */ _pthread_atfork; diff --git a/lib/libc/gen/dup3.3 b/lib/libc/gen/dup3.3 new file mode 100644 index 0000000000..255e94b5c5 --- /dev/null +++ b/lib/libc/gen/dup3.3 @@ -0,0 +1,116 @@ +.\" Copyright (c) 2013 Jilles Tjoelker +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD: head/lib/libc/gen/dup3.3 254488 2013-08-18 13:25:18Z jilles $ +.\" +.Dd August 16, 2013 +.Dt DUP3 3 +.Os +.Sh NAME +.Nm dup3 +.Nd duplicate an existing file descriptor +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In fcntl.h +.In unistd.h +.Ft int +.Fn dup3 "int oldd" "int newd" "int flags" +.Sh DESCRIPTION +The +.Fn dup3 +function +duplicates an existing object descriptor +while allowing the value of the new descriptor to be specified. +.Pp +The close-on-exec flag on the new file descriptor is determined by the +.Dv O_CLOEXEC +bit in +.Fa flags . +.Pp +If +.Fa oldd +\*(Ne +.Fa newd +and +.Fa flags +== 0, +the behavior is identical to +.Li dup2(oldd, newd) . +.Pp +If +.Fa oldd +== +.Fa newd , +then +.Fn dup3 +fails, unlike +.Xr dup2 2 . +.Sh RETURN VALUES +The value -1 is returned if an error occurs. +The external variable +.Va errno +indicates the cause of the error. +.Sh ERRORS +The +.Fn dup3 +function fails if: +.Bl -tag -width Er +.It Bq Er EBADF +The +.Fa oldd +argument is not a valid active descriptor or the +.Fa newd +argument is negative or exceeds the maximum allowable descriptor number +.It Bq Er EINVAL +The +.Fa oldd +argument is equal to the +.Fa newd +argument. +.It Bq Er EINVAL +The +.Fa flags +argument has bits set other than +.Dv O_CLOEXEC . +.El +.Sh SEE ALSO +.Xr accept 2 , +.Xr close 2 , +.Xr dup2 2 , +.Xr fcntl 2 , +.Xr getdtablesize 2 , +.Xr open 2 , +.Xr pipe 2 , +.Xr socket 2 , +.Xr socketpair 2 +.Sh STANDARDS +The +.Fn dup3 +function does not conform to any standard. +.Sh HISTORY +The +.Fn dup3 +function appeared in +.Fx 10.0 . diff --git a/lib/libc/gen/dup3.c b/lib/libc/gen/dup3.c new file mode 100644 index 0000000000..cc09075873 --- /dev/null +++ b/lib/libc/gen/dup3.c @@ -0,0 +1,62 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2012 Jukka A. Ukkonen + * All rights reserved. + * + * This software was developed by Jukka Ukkonen for FreeBSD. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD: head/lib/libc/gen/dup3.c 326193 2017-11-25 17:12:48Z pfg $ + */ + +#include "namespace.h" +#include +#include +#include +#include "un-namespace.h" + +int __dup3(int, int, int); + +int +__dup3(int oldfd, int newfd, int flags) +{ + int how; + + if (oldfd == newfd) { + errno = EINVAL; + return (-1); + } + + if (flags & ~O_CLOEXEC) { + errno = EINVAL; + return (-1); + } + + how = (flags & O_CLOEXEC) ? F_DUP2FD_CLOEXEC : F_DUP2FD; + + return (_fcntl(oldfd, how, newfd)); +} + +__weak_reference(__dup3, dup3); +__weak_reference(__dup3, _dup3); diff --git a/lib/libc/sys/dup.2 b/lib/libc/sys/dup.2 index 81fbe5974c..2b057aca70 100644 --- a/lib/libc/sys/dup.2 +++ b/lib/libc/sys/dup.2 @@ -27,9 +27,8 @@ .\" .\" @(#)dup.2 8.1 (Berkeley) 6/4/93 .\" $FreeBSD: src/lib/libc/sys/dup.2,v 1.9.2.6 2001/12/14 18:34:00 ru Exp $ -.\" $DragonFly: src/lib/libc/sys/dup.2,v 1.2 2003/06/17 04:26:47 dillon Exp $ .\" -.Dd June 4, 1993 +.Dd May 6, 2019 .Dt DUP 2 .Os .Sh NAME @@ -139,7 +138,8 @@ Too many descriptors are active. .Xr open 2 , .Xr pipe 2 , .Xr socket 2 , -.Xr socketpair 2 +.Xr socketpair 2 , +.Xr dup3 3 .Sh STANDARDS The .Fn dup