From 892cafff665b44d238e3b664f61ca38dd965cba6 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Wed, 24 Oct 2012 21:16:06 -0400 Subject: greatly improve freopen behavior 1. don't open /dev/null just as a basis to copy flags; use shared __fmodeflags function to get the right file flags for the mode. 2. handle the case (probably invalid, but whatever) case where the original stream's file descriptor was closed; previously, the logic re-closed it. 3. accept the "e" mode flag for close-on-exec; update dup3 to fallback to using dup2 so we can simply call __dup3 instead of putting fallback logic in freopen itself. --- src/unistd/dup3.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src/unistd') diff --git a/src/unistd/dup3.c b/src/unistd/dup3.c index 18f6fcce..1f7134b3 100644 --- a/src/unistd/dup3.c +++ b/src/unistd/dup3.c @@ -1,10 +1,21 @@ #define _GNU_SOURCE #include #include +#include #include "syscall.h" +#include "libc.h" -int dup3(int old, int new, int flags) { +int __dup3(int old, int new, int flags) +{ int r; - while ((r=__syscall(SYS_dup3, old, new, flags))==-EBUSY); + if (old==new) return __syscall_ret(-EINVAL); + if (flags & O_CLOEXEC) { + while ((r=__syscall(SYS_dup3, old, new, flags))==-EBUSY); + if (r!=-ENOSYS) return __syscall_ret(r); + } + while ((r=__syscall(SYS_dup2, old, new))==-EBUSY); + if (flags & O_CLOEXEC) __syscall(SYS_fcntl, new, F_SETFD, FD_CLOEXEC); return __syscall_ret(r); } + +weak_alias(__dup3, dup3); -- cgit v1.2.1