fchdir: use more consistent macro convention
[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 # include <string.h>
33
34 /* Get declarations of the Win32 API functions.  */
35 # define WIN32_LEAN_AND_MEAN
36 # include <windows.h>
37
38 /* Upper bound on getdtablesize().  See lib/getdtablesize.c.  */
39 # define OPEN_MAX_MAX 0x10000
40
41 #else
42 /* Unix API.  */
43
44 # ifndef O_CLOEXEC
45 #  define O_CLOEXEC 0
46 # endif
47
48 #endif
49
50 int
51 dup3 (int oldfd, int newfd, int flags)
52 {
53 #if HAVE_DUP3
54 # undef dup3
55   /* Try the system call first, if it exists.  (We may be running with a glibc
56      that has the function but with an older kernel that lacks it.)  */
57   {
58     /* Cache the information whether the system call really exists.  */
59     static int have_dup3_really; /* 0 = unknown, 1 = yes, -1 = no */
60     if (have_dup3_really >= 0)
61       {
62         int result = dup3 (oldfd, newfd, flags);
63         if (!(result < 0 && errno == ENOSYS))
64           {
65             have_dup3_really = 1;
66 #if REPLACE_FCHDIR
67             if (0 <= result)
68               result = _gl_register_dup (oldfd, newfd);
69 #endif
70             return result;
71           }
72         have_dup3_really = -1;
73       }
74   }
75 #endif
76
77   if (oldfd < 0 || newfd < 0 || newfd >= getdtablesize ())
78     {
79       errno = EBADF;
80       return -1;
81     }
82
83   if (newfd == oldfd)
84     {
85       errno = EINVAL;
86       return -1;
87     }
88
89   /* Check the supported flags.
90      Note that O_NONBLOCK is not supported, because setting it on newfd
91      would implicitly also set it on oldfd.  */
92   if ((flags & ~(O_CLOEXEC | O_BINARY | O_TEXT)) != 0)
93     {
94       errno = EINVAL;
95       return -1;
96     }
97
98 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
99 /* Native Woe32 API.  */
100
101   if (flags & O_CLOEXEC)
102     {
103       /* Neither dup() nor dup2() can create a file descriptor with
104          O_CLOEXEC = O_NOINHERIT set.  We need to use the low-level function
105          _open_osfhandle for this.  Iterate until all file descriptors less
106          than newfd are filled up.  */
107       HANDLE curr_process = GetCurrentProcess ();
108       HANDLE old_handle = (HANDLE) _get_osfhandle (oldfd);
109       unsigned char fds_to_close[OPEN_MAX_MAX / CHAR_BIT];
110       unsigned int fds_to_close_bound = 0;
111       int result;
112
113       if (old_handle == INVALID_HANDLE_VALUE)
114         {
115           /* oldfd is not open, or is an unassigned standard file
116              descriptor.  */
117           errno = EBADF;
118           return -1;
119         }
120
121       close (newfd);
122
123       for (;;)
124         {
125           HANDLE new_handle;
126           int duplicated_fd;
127           unsigned int index;
128
129           if (!DuplicateHandle (curr_process,         /* SourceProcessHandle */
130                                 old_handle,           /* SourceHandle */
131                                 curr_process,         /* TargetProcessHandle */
132                                 (PHANDLE) &new_handle, /* TargetHandle */
133                                 (DWORD) 0,            /* DesiredAccess */
134                                 FALSE,                /* InheritHandle */
135                                 DUPLICATE_SAME_ACCESS)) /* Options */
136             {
137               errno = EBADF; /* arbitrary */
138               result = -1;
139               break;
140             }
141           duplicated_fd = _open_osfhandle ((long) new_handle, flags);
142           if (duplicated_fd < 0)
143             {
144               CloseHandle (new_handle);
145               result = -1;
146               break;
147             }
148           if (duplicated_fd > newfd)
149             /* Shouldn't happen, since newfd is still closed.  */
150             abort ();
151           if (duplicated_fd == newfd)
152             {
153               result = newfd;
154               break;
155             }
156
157           /* Set the bit duplicated_fd in fds_to_close[].  */
158           index = (unsigned int) duplicated_fd / CHAR_BIT;
159           if (index >= fds_to_close_bound)
160             {
161               if (index >= sizeof (fds_to_close))
162                 /* Need to increase OPEN_MAX_MAX.  */
163                 abort ();
164               memset (fds_to_close + fds_to_close_bound, '\0',
165                       index + 1 - fds_to_close_bound);
166               fds_to_close_bound = index + 1;
167             }
168           fds_to_close[index] |= 1 << ((unsigned int) duplicated_fd % CHAR_BIT);
169         }
170
171       /* Close the previous fds that turned out to be too small.  */
172       {
173         int saved_errno = errno;
174         unsigned int duplicated_fd;
175
176         for (duplicated_fd = 0;
177              duplicated_fd < fds_to_close_bound * CHAR_BIT;
178              duplicated_fd++)
179           if ((fds_to_close[duplicated_fd / CHAR_BIT]
180                >> (duplicated_fd % CHAR_BIT))
181               & 1)
182             close (duplicated_fd);
183
184         errno = saved_errno;
185       }
186
187 #if REPLACE_FCHDIR
188       if (result == newfd)
189         result = _gl_register_dup (oldfd, newfd);
190 #endif
191       return result;
192     }
193
194   if (dup2 (oldfd, newfd) < 0)
195     return -1;
196
197 #else
198 /* Unix API.  */
199
200   if (dup2 (oldfd, newfd) < 0)
201     return -1;
202
203   /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/dup.html>
204      says that initially, the FD_CLOEXEC flag is cleared on newfd.  */
205
206   if (flags & O_CLOEXEC)
207     {
208       int fcntl_flags;
209
210       if ((fcntl_flags = fcntl (newfd, F_GETFD, 0)) < 0
211           || fcntl (newfd, F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
212         {
213           int saved_errno = errno;
214           close (newfd);
215           errno = saved_errno;
216           return -1;
217         }
218     }
219
220 #endif
221
222 #if O_BINARY
223   if (flags & O_BINARY)
224     setmode (newfd, O_BINARY);
225   else if (flags & O_TEXT)
226     setmode (newfd, O_TEXT);
227 #endif
228
229 #if REPLACE_FCHDIR
230   newfd = _gl_register_dup (oldfd, newfd);
231 #endif
232   return newfd;
233 }