Tolerate declared but missing dup3 syscall.
[gnulib.git] / lib / dup3.c
1 /* Copy a file descriptor, applying specific flags.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License along
15    with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 #include <config.h>
19
20 /* Specification.  */
21 #include <unistd.h>
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26
27 #include "binary-io.h"
28
29 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
30 /* Native Woe32 API.  */
31
32 /* Get declarations of the Win32 API functions.  */
33 # define WIN32_LEAN_AND_MEAN
34 # include <windows.h>
35
36 /* Upper bound on getdtablesize().  See lib/getdtablesize.c.  */
37 # define OPEN_MAX_MAX 0x10000
38
39 #else
40 /* Unix API.  */
41
42 # ifndef O_CLOEXEC
43 #  define O_CLOEXEC 0
44 # endif
45
46 #endif
47
48 int
49 dup3 (int oldfd, int newfd, int flags)
50 {
51 #if HAVE_DUP3
52 # undef dup3
53   /* Try the system call first, if it exists.  (We may be running with a glibc
54      that has the function but with an older kernel that lacks it.)  */
55   {
56     int result = dup3 (oldfd, newfd, flags);
57     if (!(result < 0 && errno == ENOSYS))
58       return result;
59   }
60 #endif
61
62   if (oldfd < 0 || newfd < 0 || newfd >= getdtablesize ())
63     {
64       errno = EBADF;
65       return -1;
66     }
67
68   if (newfd == oldfd)
69     {
70       errno = EINVAL;
71       return -1;
72     }
73
74   /* Check the supported flags.
75      Note that O_NONBLOCK is not supported, because setting it on newfd
76      would implicitly also set it on oldfd.  */
77   if ((flags & ~(O_CLOEXEC | O_BINARY | O_TEXT)) != 0)
78     {
79       errno = EINVAL;
80       return -1;
81     }
82
83 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
84 /* Native Woe32 API.  */
85
86   if (flags & O_CLOEXEC)
87     {
88       /* Neither dup() nor dup2() can create a file descriptor with
89          O_CLOEXEC = O_NOINHERIT set.  We need to use the low-level function
90          _open_osfhandle for this.  Iterate until all file descriptors less
91          than newfd are filled up.  */
92       HANDLE curr_process = GetCurrentProcess ();
93       HANDLE old_handle = (HANDLE) _get_osfhandle (oldfd);
94       unsigned char fds_to_close[OPEN_MAX_MAX / CHAR_BIT];
95       unsigned int fds_to_close_bound = 0;
96       int result;
97
98       if (old_handle == INVALID_HANDLE_VALUE)
99         {
100           /* oldfd is not open, or is an unassigned standard file
101              descriptor.  */
102           errno = EBADF;
103           return -1;
104         }
105
106       close (newfd);
107
108       for (;;)
109         {
110           HANDLE new_handle;
111           int duplicated_fd;
112           unsigned int index;
113
114           if (!DuplicateHandle (curr_process,         /* SourceProcessHandle */
115                                 old_handle,           /* SourceHandle */
116                                 curr_process,         /* TargetProcessHandle */
117                                 (PHANDLE) &new_handle, /* TargetHandle */
118                                 (DWORD) 0,            /* DesiredAccess */
119                                 FALSE,                /* InheritHandle */
120                                 DUPLICATE_SAME_ACCESS)) /* Options */
121             {
122               errno = EBADF; /* arbitrary */
123               result = -1;
124               break;
125             }
126           duplicated_fd = _open_osfhandle ((long) new_handle, flags);
127           if (duplicated_fd < 0)
128             {
129               CloseHandle (new_handle);
130               result = -1;
131               break;
132             }
133           if (duplicated_fd > newfd)
134             /* Shouldn't happen, since newfd is still closed.  */
135             abort ();
136           if (duplicated_fd == newfd)
137             {
138               result = newfd;
139               break;
140             }
141
142           /* Set the bit duplicated_fd in fds_to_close[].  */
143           index = (unsigned int) duplicated_fd / CHAR_BIT;
144           if (index >= fds_to_close_bound)
145             {
146               if (index >= sizeof (fds_to_close))
147                 /* Need to increase OPEN_MAX_MAX.  */
148                 abort ();
149               memset (fds_to_close + fds_to_close_bound, '\0',
150                       index + 1 - fds_to_close_bound);
151               fds_to_close_bound = index + 1;
152             }
153           fds_to_close[index] |= 1 << ((unsigned int) duplicated_fd % CHAR_BIT);
154         }
155
156       /* Close the previous fds that turned out to be too small.  */
157       {
158         int saved_errno = errno;
159         unsigned int duplicated_fd;
160
161         for (duplicated_fd = 0;
162              duplicated_fd < fds_to_close_bound * CHAR_BIT;
163              duplicated_fd++)
164           if ((fds_to_close[duplicated_fd / CHAR_BIT]
165                >> (duplicated_fd % CHAR_BIT))
166               & 1)
167             close (duplicated_fd);
168
169         errno = saved_errno;
170       }
171
172       return result;
173     }
174
175   if (dup2 (oldfd, newfd) < 0)
176     return -1;
177
178 #else
179 /* Unix API.  */
180
181   if (dup2 (oldfd, newfd) < 0)
182     return -1;
183
184   /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/dup.html>
185      says that initially, the FD_CLOEXEC flag is cleared on newfd.  */
186
187   if (flags & O_CLOEXEC)
188     {
189       int fcntl_flags;
190
191       if ((fcntl_flags = fcntl (newfd, F_GETFD, 0)) < 0
192           || fcntl (newfd, F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
193         {
194           int saved_errno = errno;
195           close (newfd);
196           errno = saved_errno;
197           return -1;
198         }
199     }
200
201 #endif
202
203 #if O_BINARY
204   if (flags & O_BINARY)
205     setmode (newfd, O_BINARY);
206   else if (flags & O_TEXT)
207     setmode (newfd, O_TEXT);
208 #endif
209
210   return newfd;
211 }