1 /* Create a pipe, with specific opening flags.
2 Copyright (C) 2009-2011 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser 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. */
26 #include "binary-io.h"
29 #if GNULIB_defined_O_NONBLOCK
30 # include "nonblocking.h"
33 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
34 /* Native Woe32 API. */
41 pipe2 (int fd
[2], int flags
)
45 /* Try the system call first, if it exists. (We may be running with a glibc
46 that has the function but with an older kernel that lacks it.) */
48 /* Cache the information whether the system call really exists. */
49 static int have_pipe2_really
; /* 0 = unknown, 1 = yes, -1 = no */
50 if (have_pipe2_really
>= 0)
52 int result
= pipe2 (fd
, flags
);
53 if (!(result
< 0 && errno
== ENOSYS
))
55 have_pipe2_really
= 1;
58 have_pipe2_really
= -1;
63 /* Check the supported flags. */
64 if ((flags
& ~(O_CLOEXEC
| O_NONBLOCK
| O_BINARY
| O_TEXT
)) != 0)
70 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
71 /* Native Woe32 API. */
73 if (_pipe (fd
, 4096, flags
& ~O_NONBLOCK
) < 0)
76 /* O_NONBLOCK handling.
77 On native Windows platforms, O_NONBLOCK is defined by gnulib. Use the
78 functions defined by the gnulib module 'nonblocking'. */
79 # if GNULIB_defined_O_NONBLOCK
80 if (flags
& O_NONBLOCK
)
82 if (set_nonblocking_flag (fd
[0], true) != 0
83 || set_nonblocking_flag (fd
[1], true) != 0)
87 verify (O_NONBLOCK
== 0);
98 /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/pipe.html>
99 says that initially, the O_NONBLOCK and FD_CLOEXEC flags are cleared on
100 both fd[0] and fd[1]. */
102 /* O_NONBLOCK handling.
103 On Unix platforms, O_NONBLOCK is defined by the system. Use fcntl(). */
104 if (flags
& O_NONBLOCK
)
108 if ((fcntl_flags
= fcntl (fd
[1], F_GETFL
, 0)) < 0
109 || fcntl (fd
[1], F_SETFL
, fcntl_flags
| O_NONBLOCK
) == -1
110 || (fcntl_flags
= fcntl (fd
[0], F_GETFL
, 0)) < 0
111 || fcntl (fd
[0], F_SETFL
, fcntl_flags
| O_NONBLOCK
) == -1)
115 if (flags
& O_CLOEXEC
)
119 if ((fcntl_flags
= fcntl (fd
[1], F_GETFD
, 0)) < 0
120 || fcntl (fd
[1], F_SETFD
, fcntl_flags
| FD_CLOEXEC
) == -1
121 || (fcntl_flags
= fcntl (fd
[0], F_GETFD
, 0)) < 0
122 || fcntl (fd
[0], F_SETFD
, fcntl_flags
| FD_CLOEXEC
) == -1)
127 if (flags
& O_BINARY
)
129 setmode (fd
[1], O_BINARY
);
130 setmode (fd
[0], O_BINARY
);
132 else if (flags
& O_TEXT
)
134 setmode (fd
[1], O_TEXT
);
135 setmode (fd
[0], O_TEXT
);
145 int saved_errno
= errno
;