pipe2: fix O_NONBLOCK support on mingw
[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 "nonblocking.h"
28
29 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
30 /* Native Woe32 API.  */
31
32 # include <io.h>
33
34 #endif
35
36 int
37 pipe2 (int fd[2], int flags)
38 {
39 #if HAVE_PIPE2
40 # undef pipe2
41   /* Try the system call first, if it exists.  (We may be running with a glibc
42      that has the function but with an older kernel that lacks it.)  */
43   {
44     /* Cache the information whether the system call really exists.  */
45     static int have_pipe2_really; /* 0 = unknown, 1 = yes, -1 = no */
46     if (have_pipe2_really >= 0)
47       {
48         int result = pipe2 (fd, flags);
49         if (!(result < 0 && errno == ENOSYS))
50           {
51             have_pipe2_really = 1;
52             return result;
53           }
54         have_pipe2_really = -1;
55       }
56   }
57 #endif
58
59   /* Check the supported flags.  */
60   if ((flags & ~(O_CLOEXEC | O_NONBLOCK | O_BINARY | O_TEXT)) != 0)
61     {
62       errno = EINVAL;
63       return -1;
64     }
65
66 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
67 /* Native Woe32 API.  */
68
69   if (flags & O_NONBLOCK)
70     {
71       int result = _pipe (fd, 4096, flags & ~O_NONBLOCK);
72       if (result != 0)
73         return result;
74       if (set_nonblocking_flag (fd[0], true) != 0
75           || set_nonblocking_flag (fd[1], true) != 0)
76         {
77           int saved_errno = errno;
78           close (fd[0]);
79           close (fd[1]);
80           result = -1;
81           errno = saved_errno;
82         }
83       return result;
84     }
85   return _pipe (fd, 4096, flags);
86
87 #else
88 /* Unix API.  */
89
90   if (pipe (fd) < 0)
91     return -1;
92
93   /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/pipe.html>
94      says that initially, the O_NONBLOCK and FD_CLOEXEC flags are cleared on
95      both fd[0] and fd[1].  */
96
97   if (flags & O_NONBLOCK)
98     {
99       int fcntl_flags;
100
101       if ((fcntl_flags = fcntl (fd[1], F_GETFL, 0)) < 0
102           || fcntl (fd[1], F_SETFL, fcntl_flags | O_NONBLOCK) == -1
103           || (fcntl_flags = fcntl (fd[0], F_GETFL, 0)) < 0
104           || fcntl (fd[0], F_SETFL, fcntl_flags | O_NONBLOCK) == -1)
105         goto fail;
106     }
107
108   if (flags & O_CLOEXEC)
109     {
110       int fcntl_flags;
111
112       if ((fcntl_flags = fcntl (fd[1], F_GETFD, 0)) < 0
113           || fcntl (fd[1], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1
114           || (fcntl_flags = fcntl (fd[0], F_GETFD, 0)) < 0
115           || fcntl (fd[0], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
116         goto fail;
117     }
118
119 # if O_BINARY
120   if (flags & O_BINARY)
121     {
122       setmode (fd[1], O_BINARY);
123       setmode (fd[0], O_BINARY);
124     }
125   else if (flags & O_TEXT)
126     {
127       setmode (fd[1], O_TEXT);
128       setmode (fd[0], O_TEXT);
129     }
130 # endif
131
132   return 0;
133
134  fail:
135   {
136     int saved_errno = errno;
137     close (fd[0]);
138     close (fd[1]);
139     errno = saved_errno;
140     return -1;
141   }
142
143 #endif
144 }