fcntl.c 9.0 KB

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