fcntl.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* Provide file descriptor control.
  2. Copyright (C) 2009-2013 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 Eric Blake <ebb9@byu.net>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <fcntl.h>
  17. #include <errno.h>
  18. #include <limits.h>
  19. #include <stdarg.h>
  20. #include <unistd.h>
  21. #if !HAVE_FCNTL
  22. # define rpl_fcntl fcntl
  23. #endif
  24. #undef fcntl
  25. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  26. /* Get declarations of the native Windows API functions. */
  27. # define WIN32_LEAN_AND_MEAN
  28. # include <windows.h>
  29. /* Get _get_osfhandle. */
  30. # include "msvc-nothrow.h"
  31. /* Upper bound on getdtablesize(). See lib/getdtablesize.c. */
  32. # define OPEN_MAX_MAX 0x10000
  33. /* Duplicate OLDFD into the first available slot of at least NEWFD,
  34. which must be positive, with FLAGS determining whether the duplicate
  35. will be inheritable. */
  36. static int
  37. dupfd (int oldfd, int newfd, int flags)
  38. {
  39. /* Mingw has no way to create an arbitrary fd. Iterate until all
  40. file descriptors less than newfd are filled up. */
  41. HANDLE curr_process = GetCurrentProcess ();
  42. HANDLE old_handle = (HANDLE) _get_osfhandle (oldfd);
  43. unsigned char fds_to_close[OPEN_MAX_MAX / CHAR_BIT];
  44. unsigned int fds_to_close_bound = 0;
  45. int result;
  46. BOOL inherit = flags & O_CLOEXEC ? FALSE : TRUE;
  47. int mode;
  48. if (newfd < 0 || getdtablesize () <= newfd)
  49. {
  50. errno = EINVAL;
  51. return -1;
  52. }
  53. if (old_handle == INVALID_HANDLE_VALUE
  54. || (mode = setmode (oldfd, O_BINARY)) == -1)
  55. {
  56. /* oldfd is not open, or is an unassigned standard file
  57. descriptor. */
  58. errno = EBADF;
  59. return -1;
  60. }
  61. setmode (oldfd, mode);
  62. flags |= mode;
  63. for (;;)
  64. {
  65. HANDLE new_handle;
  66. int duplicated_fd;
  67. unsigned int index;
  68. if (!DuplicateHandle (curr_process, /* SourceProcessHandle */
  69. old_handle, /* SourceHandle */
  70. curr_process, /* TargetProcessHandle */
  71. (PHANDLE) &new_handle, /* TargetHandle */
  72. (DWORD) 0, /* DesiredAccess */
  73. inherit, /* InheritHandle */
  74. DUPLICATE_SAME_ACCESS)) /* Options */
  75. {
  76. /* TODO: Translate GetLastError () into errno. */
  77. errno = EMFILE;
  78. result = -1;
  79. break;
  80. }
  81. duplicated_fd = _open_osfhandle ((intptr_t) new_handle, flags);
  82. if (duplicated_fd < 0)
  83. {
  84. CloseHandle (new_handle);
  85. errno = EMFILE;
  86. result = -1;
  87. break;
  88. }
  89. if (newfd <= duplicated_fd)
  90. {
  91. result = duplicated_fd;
  92. break;
  93. }
  94. /* Set the bit duplicated_fd in fds_to_close[]. */
  95. index = (unsigned int) duplicated_fd / CHAR_BIT;
  96. if (fds_to_close_bound <= index)
  97. {
  98. if (sizeof fds_to_close <= index)
  99. /* Need to increase OPEN_MAX_MAX. */
  100. abort ();
  101. memset (fds_to_close + fds_to_close_bound, '\0',
  102. index + 1 - fds_to_close_bound);
  103. fds_to_close_bound = index + 1;
  104. }
  105. fds_to_close[index] |= 1 << ((unsigned int) duplicated_fd % CHAR_BIT);
  106. }
  107. /* Close the previous fds that turned out to be too small. */
  108. {
  109. int saved_errno = errno;
  110. unsigned int duplicated_fd;
  111. for (duplicated_fd = 0;
  112. duplicated_fd < fds_to_close_bound * CHAR_BIT;
  113. duplicated_fd++)
  114. if ((fds_to_close[duplicated_fd / CHAR_BIT]
  115. >> (duplicated_fd % CHAR_BIT))
  116. & 1)
  117. close (duplicated_fd);
  118. errno = saved_errno;
  119. }
  120. # if REPLACE_FCHDIR
  121. if (0 <= result)
  122. result = _gl_register_dup (oldfd, result);
  123. # endif
  124. return result;
  125. }
  126. #endif /* W32 */
  127. /* Perform the specified ACTION on the file descriptor FD, possibly
  128. using the argument ARG further described below. This replacement
  129. handles the following actions, and forwards all others on to the
  130. native fcntl. An unrecognized ACTION returns -1 with errno set to
  131. EINVAL.
  132. F_DUPFD - duplicate FD, with int ARG being the minimum target fd.
  133. If successful, return the duplicate, which will be inheritable;
  134. otherwise return -1 and set errno.
  135. F_DUPFD_CLOEXEC - duplicate FD, with int ARG being the minimum
  136. target fd. If successful, return the duplicate, which will not be
  137. inheritable; otherwise return -1 and set errno.
  138. F_GETFD - ARG need not be present. If successful, return a
  139. non-negative value containing the descriptor flags of FD (only
  140. FD_CLOEXEC is portable, but other flags may be present); otherwise
  141. return -1 and set errno. */
  142. int
  143. rpl_fcntl (int fd, int action, /* arg */...)
  144. {
  145. va_list arg;
  146. int result = -1;
  147. va_start (arg, action);
  148. switch (action)
  149. {
  150. #if !HAVE_FCNTL
  151. case F_DUPFD:
  152. {
  153. int target = va_arg (arg, int);
  154. result = dupfd (fd, target, 0);
  155. break;
  156. }
  157. #elif FCNTL_DUPFD_BUGGY || REPLACE_FCHDIR
  158. case F_DUPFD:
  159. {
  160. int target = va_arg (arg, int);
  161. /* Detect invalid target; needed for cygwin 1.5.x. */
  162. if (target < 0 || getdtablesize () <= target)
  163. errno = EINVAL;
  164. else
  165. {
  166. /* Haiku alpha 2 loses fd flags on original. */
  167. int flags = fcntl (fd, F_GETFD);
  168. if (flags < 0)
  169. {
  170. result = -1;
  171. break;
  172. }
  173. result = fcntl (fd, action, target);
  174. if (0 <= result && fcntl (fd, F_SETFD, flags) == -1)
  175. {
  176. int saved_errno = errno;
  177. close (result);
  178. result = -1;
  179. errno = saved_errno;
  180. }
  181. # if REPLACE_FCHDIR
  182. if (0 <= result)
  183. result = _gl_register_dup (fd, result);
  184. # endif
  185. }
  186. break;
  187. } /* F_DUPFD */
  188. #endif /* FCNTL_DUPFD_BUGGY || REPLACE_FCHDIR */
  189. case F_DUPFD_CLOEXEC:
  190. {
  191. int target = va_arg (arg, int);
  192. #if !HAVE_FCNTL
  193. result = dupfd (fd, target, O_CLOEXEC);
  194. break;
  195. #else /* HAVE_FCNTL */
  196. /* Try the system call first, if the headers claim it exists
  197. (that is, if GNULIB_defined_F_DUPFD_CLOEXEC is 0), since we
  198. may be running with a glibc that has the macro but with an
  199. older kernel that does not support it. Cache the
  200. information on whether the system call really works, but
  201. avoid caching failure if the corresponding F_DUPFD fails
  202. for any reason. 0 = unknown, 1 = yes, -1 = no. */
  203. static int have_dupfd_cloexec = GNULIB_defined_F_DUPFD_CLOEXEC ? -1 : 0;
  204. if (0 <= have_dupfd_cloexec)
  205. {
  206. result = fcntl (fd, action, target);
  207. if (0 <= result || errno != EINVAL)
  208. {
  209. have_dupfd_cloexec = 1;
  210. # if REPLACE_FCHDIR
  211. if (0 <= result)
  212. result = _gl_register_dup (fd, result);
  213. # endif
  214. }
  215. else
  216. {
  217. result = rpl_fcntl (fd, F_DUPFD, target);
  218. if (result < 0)
  219. break;
  220. have_dupfd_cloexec = -1;
  221. }
  222. }
  223. else
  224. result = rpl_fcntl (fd, F_DUPFD, target);
  225. if (0 <= result && have_dupfd_cloexec == -1)
  226. {
  227. int flags = fcntl (result, F_GETFD);
  228. if (flags < 0 || fcntl (result, F_SETFD, flags | FD_CLOEXEC) == -1)
  229. {
  230. int saved_errno = errno;
  231. close (result);
  232. errno = saved_errno;
  233. result = -1;
  234. }
  235. }
  236. break;
  237. #endif /* HAVE_FCNTL */
  238. } /* F_DUPFD_CLOEXEC */
  239. #if !HAVE_FCNTL
  240. case F_GETFD:
  241. {
  242. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  243. HANDLE handle = (HANDLE) _get_osfhandle (fd);
  244. DWORD flags;
  245. if (handle == INVALID_HANDLE_VALUE
  246. || GetHandleInformation (handle, &flags) == 0)
  247. errno = EBADF;
  248. else
  249. result = (flags & HANDLE_FLAG_INHERIT) ? 0 : FD_CLOEXEC;
  250. # else /* !W32 */
  251. /* Use dup2 to reject invalid file descriptors. No way to
  252. access this information, so punt. */
  253. if (0 <= dup2 (fd, fd))
  254. result = 0;
  255. # endif /* !W32 */
  256. break;
  257. } /* F_GETFD */
  258. #endif /* !HAVE_FCNTL */
  259. /* Implementing F_SETFD on mingw is not trivial - there is no
  260. API for changing the O_NOINHERIT bit on an fd, and merely
  261. changing the HANDLE_FLAG_INHERIT bit on the underlying handle
  262. can lead to odd state. It may be possible by duplicating the
  263. handle, using _open_osfhandle with the right flags, then
  264. using dup2 to move the duplicate onto the original, but that
  265. is not supported for now. */
  266. default:
  267. {
  268. #if HAVE_FCNTL
  269. void *p = va_arg (arg, void *);
  270. result = fcntl (fd, action, p);
  271. #else
  272. errno = EINVAL;
  273. #endif
  274. break;
  275. }
  276. }
  277. va_end (arg);
  278. return result;
  279. }