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