diff options
author | Eric Blake <ebb9@byu.net> | 2009-12-07 06:53:59 -0700 |
---|---|---|
committer | Eric Blake <ebb9@byu.net> | 2009-12-07 06:55:09 -0700 |
commit | 73da5fb7e129f1fa540e040582cda710b8c2cce4 (patch) | |
tree | ffc2a62ecc40ef03a476c0923c4839bf656cb42a /lib/cloexec.c | |
parent | b1ab442e6db76de2a7315531f6f0ec59c5934368 (diff) | |
download | gnulib-73da5fb7e129f1fa540e040582cda710b8c2cce4.tar.gz |
cloexec: preserve text vs. binary across dup_cloexec
On mingw, dup_cloexec mistakenly converted a text fd into a
binary fd. Cygwin copied the source mode. Most other platforms
don't distinguish between modes.
* lib/cloexec.c (dup_cloexec) [W32]: Query and use translation
mode.
* modules/dup2-tests (Depends-on): Add binary-io.
* modules/cloexec-tests (Depends-on): Likewise.
* tests/test-dup2.c (setmode, is_mode): New helpers.
(main): Add tests that translation mode is preserved.
* tests/test-cloexec.c (setmode, is_mode, main): Likewise.
Reported by Bruno Haible.
Signed-off-by: Eric Blake <ebb9@byu.net>
Diffstat (limited to 'lib/cloexec.c')
-rw-r--r-- | lib/cloexec.c | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/lib/cloexec.c b/lib/cloexec.c index 5479477e51..57e0be59e2 100644 --- a/lib/cloexec.c +++ b/lib/cloexec.c @@ -64,6 +64,8 @@ set_cloexec_flag (int desc, bool value) #else + /* Use dup2 to reject invalid file descriptors; the cloexec flag + will be unaffected. */ if (desc < 0) { errno = EBADF; @@ -90,8 +92,10 @@ dup_cloexec (int fd) HANDLE curr_process = GetCurrentProcess (); HANDLE old_handle = (HANDLE) _get_osfhandle (fd); HANDLE new_handle; + int mode; - if (old_handle == INVALID_HANDLE_VALUE) + if (old_handle == INVALID_HANDLE_VALUE + || (mode = setmode (fd, O_BINARY)) == -1) { /* fd is closed, or is open to no handle at all. We cannot duplicate fd in this case, because _open_osfhandle @@ -99,6 +103,7 @@ dup_cloexec (int fd) errno = EBADF; return -1; } + setmode (fd, mode); if (!DuplicateHandle (curr_process, /* SourceProcessHandle */ old_handle, /* SourceHandle */ @@ -113,7 +118,7 @@ dup_cloexec (int fd) return -1; } - nfd = _open_osfhandle ((long) new_handle, O_BINARY | O_NOINHERIT); + nfd = _open_osfhandle ((long) new_handle, mode | O_NOINHERIT); if (nfd < 0) { CloseHandle (new_handle); |