Merge branch 'bt/elisp'
[bpt/guile.git] / lib / pipe2.c
CommitLineData
3d458a81 1/* Create a pipe, with specific opening flags.
f0007cad 2 Copyright (C) 2009-2012 Free Software Foundation, Inc.
3d458a81
AW
3
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)
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 Lesser General Public License for more details.
13
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. */
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__
f0007cad 34/* Native Windows API. */
3d458a81
AW
35
36# include <io.h>
37
38#endif
39
40int
41pipe2 (int fd[2], int flags)
42{
231c0e0e
LC
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];
47 tmp[0] = fd[0];
48 tmp[1] = fd[1];
49
3d458a81
AW
50#if HAVE_PIPE2
51# undef pipe2
52 /* Try the system call first, if it exists. (We may be running with a glibc
53 that has the function but with an older kernel that lacks it.) */
54 {
55 /* Cache the information whether the system call really exists. */
56 static int have_pipe2_really; /* 0 = unknown, 1 = yes, -1 = no */
57 if (have_pipe2_really >= 0)
58 {
59 int result = pipe2 (fd, flags);
60 if (!(result < 0 && errno == ENOSYS))
61 {
62 have_pipe2_really = 1;
63 return result;
64 }
65 have_pipe2_really = -1;
66 }
67 }
68#endif
69
70 /* Check the supported flags. */
71 if ((flags & ~(O_CLOEXEC | O_NONBLOCK | O_BINARY | O_TEXT)) != 0)
72 {
73 errno = EINVAL;
74 return -1;
75 }
76
77#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
f0007cad 78/* Native Windows API. */
3d458a81
AW
79
80 if (_pipe (fd, 4096, flags & ~O_NONBLOCK) < 0)
231c0e0e
LC
81 {
82 fd[0] = tmp[0];
83 fd[1] = tmp[1];
84 return -1;
85 }
3d458a81
AW
86
87 /* O_NONBLOCK handling.
88 On native Windows platforms, O_NONBLOCK is defined by gnulib. Use the
89 functions defined by the gnulib module 'nonblocking'. */
90# if GNULIB_defined_O_NONBLOCK
91 if (flags & O_NONBLOCK)
92 {
93 if (set_nonblocking_flag (fd[0], true) != 0
94 || set_nonblocking_flag (fd[1], true) != 0)
95 goto fail;
96 }
97# else
35428fb6
LC
98 {
99 verify (O_NONBLOCK == 0);
100 }
3d458a81
AW
101# endif
102
103 return 0;
104
105#else
106/* Unix API. */
107
108 if (pipe (fd) < 0)
109 return -1;
110
111 /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/pipe.html>
112 says that initially, the O_NONBLOCK and FD_CLOEXEC flags are cleared on
113 both fd[0] and fd[1]. */
114
115 /* O_NONBLOCK handling.
116 On Unix platforms, O_NONBLOCK is defined by the system. Use fcntl(). */
117 if (flags & O_NONBLOCK)
118 {
119 int fcntl_flags;
120
121 if ((fcntl_flags = fcntl (fd[1], F_GETFL, 0)) < 0
122 || fcntl (fd[1], F_SETFL, fcntl_flags | O_NONBLOCK) == -1
123 || (fcntl_flags = fcntl (fd[0], F_GETFL, 0)) < 0
124 || fcntl (fd[0], F_SETFL, fcntl_flags | O_NONBLOCK) == -1)
125 goto fail;
126 }
127
128 if (flags & O_CLOEXEC)
129 {
130 int fcntl_flags;
131
132 if ((fcntl_flags = fcntl (fd[1], F_GETFD, 0)) < 0
133 || fcntl (fd[1], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1
134 || (fcntl_flags = fcntl (fd[0], F_GETFD, 0)) < 0
135 || fcntl (fd[0], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
136 goto fail;
137 }
138
139# if O_BINARY
140 if (flags & O_BINARY)
141 {
142 setmode (fd[1], O_BINARY);
143 setmode (fd[0], O_BINARY);
144 }
145 else if (flags & O_TEXT)
146 {
147 setmode (fd[1], O_TEXT);
148 setmode (fd[0], O_TEXT);
149 }
150# endif
151
152 return 0;
153
154#endif
155
35428fb6
LC
156#if GNULIB_defined_O_NONBLOCK || \
157 !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
3d458a81
AW
158 fail:
159 {
160 int saved_errno = errno;
161 close (fd[0]);
162 close (fd[1]);
231c0e0e
LC
163 fd[0] = tmp[0];
164 fd[1] = tmp[1];
3d458a81
AW
165 errno = saved_errno;
166 return -1;
167 }
35428fb6 168#endif
3d458a81 169}