dup2.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* Duplicate an open file descriptor to a specified file descriptor.
  2. Copyright (C) 1999, 2004-2007, 2009-2017 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* written by Paul Eggert */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <unistd.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #if HAVE_DUP2
  20. # undef dup2
  21. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  22. /* Get declarations of the native Windows API functions. */
  23. # define WIN32_LEAN_AND_MEAN
  24. # include <windows.h>
  25. # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
  26. # include "msvc-inval.h"
  27. # endif
  28. /* Get _get_osfhandle. */
  29. # if GNULIB_MSVC_NOTHROW
  30. # include "msvc-nothrow.h"
  31. # else
  32. # include <io.h>
  33. # endif
  34. # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
  35. static int
  36. dup2_nothrow (int fd, int desired_fd)
  37. {
  38. int result;
  39. TRY_MSVC_INVAL
  40. {
  41. result = dup2 (fd, desired_fd);
  42. }
  43. CATCH_MSVC_INVAL
  44. {
  45. errno = EBADF;
  46. result = -1;
  47. }
  48. DONE_MSVC_INVAL;
  49. return result;
  50. }
  51. # else
  52. # define dup2_nothrow dup2
  53. # endif
  54. static int
  55. ms_windows_dup2 (int fd, int desired_fd)
  56. {
  57. int result;
  58. /* If fd is closed, mingw hangs on dup2 (fd, fd). If fd is open,
  59. dup2 (fd, fd) returns 0, but all further attempts to use fd in
  60. future dup2 calls will hang. */
  61. if (fd == desired_fd)
  62. {
  63. if ((HANDLE) _get_osfhandle (fd) == INVALID_HANDLE_VALUE)
  64. {
  65. errno = EBADF;
  66. return -1;
  67. }
  68. return fd;
  69. }
  70. /* Wine 1.0.1 return 0 when desired_fd is negative but not -1:
  71. http://bugs.winehq.org/show_bug.cgi?id=21289 */
  72. if (desired_fd < 0)
  73. {
  74. errno = EBADF;
  75. return -1;
  76. }
  77. result = dup2_nothrow (fd, desired_fd);
  78. if (result == 0)
  79. result = desired_fd;
  80. return result;
  81. }
  82. # define dup2 ms_windows_dup2
  83. # elif defined __KLIBC__
  84. # include <InnoTekLIBC/backend.h>
  85. static int
  86. klibc_dup2dirfd (int fd, int desired_fd)
  87. {
  88. int tempfd;
  89. int dupfd;
  90. tempfd = open ("NUL", O_RDONLY);
  91. if (tempfd == -1)
  92. return -1;
  93. if (tempfd == desired_fd)
  94. {
  95. close (tempfd);
  96. char path[_MAX_PATH];
  97. if (__libc_Back_ioFHToPath (fd, path, sizeof (path)))
  98. return -1;
  99. return open(path, O_RDONLY);
  100. }
  101. dupfd = klibc_dup2dirfd (fd, desired_fd);
  102. close (tempfd);
  103. return dupfd;
  104. }
  105. static int
  106. klibc_dup2 (int fd, int desired_fd)
  107. {
  108. int dupfd;
  109. struct stat sbuf;
  110. dupfd = dup2 (fd, desired_fd);
  111. if (dupfd == -1 && errno == ENOTSUP \
  112. && !fstat (fd, &sbuf) && S_ISDIR (sbuf.st_mode))
  113. {
  114. close (desired_fd);
  115. return klibc_dup2dirfd (fd, desired_fd);
  116. }
  117. return dupfd;
  118. }
  119. # define dup2 klibc_dup2
  120. # endif
  121. int
  122. rpl_dup2 (int fd, int desired_fd)
  123. {
  124. int result;
  125. # ifdef F_GETFL
  126. /* On Linux kernels 2.6.26-2.6.29, dup2 (fd, fd) returns -EBADF.
  127. On Cygwin 1.5.x, dup2 (1, 1) returns 0.
  128. On Cygwin 1.7.17, dup2 (1, -1) dumps core.
  129. On Cygwin 1.7.25, dup2 (1, 256) can dump core.
  130. On Haiku, dup2 (fd, fd) mistakenly clears FD_CLOEXEC. */
  131. # if HAVE_SETDTABLESIZE
  132. setdtablesize (desired_fd + 1);
  133. # endif
  134. if (desired_fd < 0)
  135. fd = desired_fd;
  136. if (fd == desired_fd)
  137. return fcntl (fd, F_GETFL) == -1 ? -1 : fd;
  138. # endif
  139. result = dup2 (fd, desired_fd);
  140. /* Correct an errno value on FreeBSD 6.1 and Cygwin 1.5.x. */
  141. if (result == -1 && errno == EMFILE)
  142. errno = EBADF;
  143. # if REPLACE_FCHDIR
  144. if (fd != desired_fd && result != -1)
  145. result = _gl_register_dup (fd, result);
  146. # endif
  147. return result;
  148. }
  149. #else /* !HAVE_DUP2 */
  150. /* On older platforms, dup2 did not exist. */
  151. # ifndef F_DUPFD
  152. static int
  153. dupfd (int fd, int desired_fd)
  154. {
  155. int duplicated_fd = dup (fd);
  156. if (duplicated_fd < 0 || duplicated_fd == desired_fd)
  157. return duplicated_fd;
  158. else
  159. {
  160. int r = dupfd (fd, desired_fd);
  161. int e = errno;
  162. close (duplicated_fd);
  163. errno = e;
  164. return r;
  165. }
  166. }
  167. # endif
  168. int
  169. dup2 (int fd, int desired_fd)
  170. {
  171. int result = fcntl (fd, F_GETFL) < 0 ? -1 : fd;
  172. if (result == -1 || fd == desired_fd)
  173. return result;
  174. close (desired_fd);
  175. # ifdef F_DUPFD
  176. result = fcntl (fd, F_DUPFD, desired_fd);
  177. # if REPLACE_FCHDIR
  178. if (0 <= result)
  179. result = _gl_register_dup (fd, result);
  180. # endif
  181. # else
  182. result = dupfd (fd, desired_fd);
  183. # endif
  184. if (result == -1 && (errno == EMFILE || errno == EINVAL))
  185. errno = EBADF;
  186. return result;
  187. }
  188. #endif /* !HAVE_DUP2 */