pipe2: decouple from binary-io a bit
[gnulib.git] / lib / pipe2.c
1 /* Create a pipe, with specific opening flags.
2    Copyright (C) 2009-2013 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, see <http://www.gnu.org/licenses/>.  */
16
17 #include <config.h>
18
19 /* Specification.  */
20 #include <unistd.h>
21
22 #include <errno.h>
23 #include <fcntl.h>
24
25 #if GNULIB_BINARY_IO
26 # include "binary-io.h"
27 #endif
28
29 #include "verify.h"
30
31 #if GNULIB_defined_O_NONBLOCK
32 # include "nonblocking.h"
33 #endif
34
35 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
36 /* Native Windows API.  */
37
38 # include <io.h>
39
40 #endif
41
42 int
43 pipe2 (int fd[2], int flags)
44 {
45   /* Mingw _pipe() corrupts fd on failure; also, if we succeed at
46      creating the pipe but later fail at changing fcntl, we want
47      to leave fd unchanged: http://austingroupbugs.net/view.php?id=467  */
48   int tmp[2];
49   tmp[0] = fd[0];
50   tmp[1] = fd[1];
51
52 #if HAVE_PIPE2
53 # undef pipe2
54   /* Try the system call first, if it exists.  (We may be running with a glibc
55      that has the function but with an older kernel that lacks it.)  */
56   {
57     /* Cache the information whether the system call really exists.  */
58     static int have_pipe2_really; /* 0 = unknown, 1 = yes, -1 = no */
59     if (have_pipe2_really >= 0)
60       {
61         int result = pipe2 (fd, flags);
62         if (!(result < 0 && errno == ENOSYS))
63           {
64             have_pipe2_really = 1;
65             return result;
66           }
67         have_pipe2_really = -1;
68       }
69   }
70 #endif
71
72   /* Check the supported flags.  */
73   if ((flags & ~(O_CLOEXEC | O_NONBLOCK | O_BINARY | O_TEXT)) != 0)
74     {
75       errno = EINVAL;
76       return -1;
77     }
78
79 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
80 /* Native Windows API.  */
81
82   if (_pipe (fd, 4096, flags & ~O_NONBLOCK) < 0)
83     {
84       fd[0] = tmp[0];
85       fd[1] = tmp[1];
86       return -1;
87     }
88
89   /* O_NONBLOCK handling.
90      On native Windows platforms, O_NONBLOCK is defined by gnulib.  Use the
91      functions defined by the gnulib module 'nonblocking'.  */
92 # if GNULIB_defined_O_NONBLOCK
93   if (flags & O_NONBLOCK)
94     {
95       if (set_nonblocking_flag (fd[0], true) != 0
96           || set_nonblocking_flag (fd[1], true) != 0)
97         goto fail;
98     }
99 # else
100   {
101     verify (O_NONBLOCK == 0);
102   }
103 # endif
104
105   return 0;
106
107 #else
108 /* Unix API.  */
109
110   if (pipe (fd) < 0)
111     return -1;
112
113   /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/pipe.html>
114      says that initially, the O_NONBLOCK and FD_CLOEXEC flags are cleared on
115      both fd[0] and fd[1].  */
116
117   /* O_NONBLOCK handling.
118      On Unix platforms, O_NONBLOCK is defined by the system.  Use fcntl().  */
119   if (flags & O_NONBLOCK)
120     {
121       int fcntl_flags;
122
123       if ((fcntl_flags = fcntl (fd[1], F_GETFL, 0)) < 0
124           || fcntl (fd[1], F_SETFL, fcntl_flags | O_NONBLOCK) == -1
125           || (fcntl_flags = fcntl (fd[0], F_GETFL, 0)) < 0
126           || fcntl (fd[0], F_SETFL, fcntl_flags | O_NONBLOCK) == -1)
127         goto fail;
128     }
129
130   if (flags & O_CLOEXEC)
131     {
132       int fcntl_flags;
133
134       if ((fcntl_flags = fcntl (fd[1], F_GETFD, 0)) < 0
135           || fcntl (fd[1], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1
136           || (fcntl_flags = fcntl (fd[0], F_GETFD, 0)) < 0
137           || fcntl (fd[0], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
138         goto fail;
139     }
140
141 # if O_BINARY
142   if (flags & O_BINARY)
143     {
144       setmode (fd[1], O_BINARY);
145       setmode (fd[0], O_BINARY);
146     }
147   else if (flags & O_TEXT)
148     {
149       setmode (fd[1], O_TEXT);
150       setmode (fd[0], O_TEXT);
151     }
152 # endif
153
154   return 0;
155
156 #endif
157
158 #if GNULIB_defined_O_NONBLOCK || \
159   !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
160  fail:
161   {
162     int saved_errno = errno;
163     close (fd[0]);
164     close (fd[1]);
165     fd[0] = tmp[0];
166     fd[1] = tmp[1];
167     errno = saved_errno;
168     return -1;
169   }
170 #endif
171 }