1 /* Create a pipe, with specific opening flags.
2 Copyright (C) 2009-2025 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file 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
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
25 #include "binary-io.h"
27 #if GNULIB_defined_O_NONBLOCK
28 # include "nonblocking.h"
31 #if defined _WIN32 && ! defined __CYGWIN__
32 /* Native Windows API. */
39 pipe2 (int fd
[2], int flags
)
41 /* Mingw _pipe() corrupts fd on failure; also, if we succeed at
42 creating the pipe but later fail at changing fcntl, we want
43 to leave fd unchanged: https://austingroupbugs.net/view.php?id=467 */
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.) */
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)
57 int result
= pipe2 (fd
, flags
);
58 if (!(result
< 0 && errno
== ENOSYS
))
60 have_pipe2_really
= 1;
63 have_pipe2_really
= -1;
68 /* Check the supported flags. */
69 if ((flags
& ~(O_CLOEXEC
| O_NONBLOCK
| O_BINARY
| O_TEXT
)) != 0)
75 #if defined _WIN32 && ! defined __CYGWIN__
76 /* Native Windows API. */
78 if (_pipe (fd
, 4096, flags
& ~O_NONBLOCK
) < 0)
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
)
91 if (set_nonblocking_flag (fd
[0], true) != 0
92 || set_nonblocking_flag (fd
[1], true) != 0)
97 static_assert (O_NONBLOCK
== 0);
109 /* POSIX <https://pubs.opengroup.org/onlinepubs/9699919799/functions/pipe.html>
110 says that initially, the O_NONBLOCK and FD_CLOEXEC flags are cleared on
111 both fd[0] and fd[1]. */
113 /* O_NONBLOCK handling.
114 On Unix platforms, O_NONBLOCK is defined by the system. Use fcntl(). */
115 if (flags
& O_NONBLOCK
)
119 if ((fcntl_flags
= fcntl (fd
[1], F_GETFL
, 0)) < 0
120 || fcntl (fd
[1], F_SETFL
, fcntl_flags
| O_NONBLOCK
) == -1
121 || (fcntl_flags
= fcntl (fd
[0], F_GETFL
, 0)) < 0
122 || fcntl (fd
[0], F_SETFL
, fcntl_flags
| O_NONBLOCK
) == -1)
126 if (flags
& O_CLOEXEC
)
130 if ((fcntl_flags
= fcntl (fd
[1], F_GETFD
, 0)) < 0
131 || fcntl (fd
[1], F_SETFD
, fcntl_flags
| FD_CLOEXEC
) == -1
132 || (fcntl_flags
= fcntl (fd
[0], F_GETFD
, 0)) < 0
133 || fcntl (fd
[0], F_SETFD
, fcntl_flags
| FD_CLOEXEC
) == -1)
138 if (flags
& O_BINARY
)
140 set_binary_mode (fd
[1], O_BINARY
);
141 set_binary_mode (fd
[0], O_BINARY
);
143 else if (flags
& O_TEXT
)
145 set_binary_mode (fd
[1], O_TEXT
);
146 set_binary_mode (fd
[0], O_TEXT
);
154 #if GNULIB_defined_O_NONBLOCK || !(defined _WIN32 && ! defined __CYGWIN__)
157 int saved_errno
= errno
;