Merge remote-tracking branch 'local-2.0/stable-2.0'
[bpt/guile.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 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__
34 /* Native Woe32 API. */
35
36 # include <io.h>
37
38 #endif
39
40 int
41 pipe2 (int fd[2], int flags)
42 {
43 #if HAVE_PIPE2
44 # undef pipe2
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.) */
47 {
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)
51 {
52 int result = pipe2 (fd, flags);
53 if (!(result < 0 && errno == ENOSYS))
54 {
55 have_pipe2_really = 1;
56 return result;
57 }
58 have_pipe2_really = -1;
59 }
60 }
61 #endif
62
63 /* Check the supported flags. */
64 if ((flags & ~(O_CLOEXEC | O_NONBLOCK | O_BINARY | O_TEXT)) != 0)
65 {
66 errno = EINVAL;
67 return -1;
68 }
69
70 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
71 /* Native Woe32 API. */
72
73 if (_pipe (fd, 4096, flags & ~O_NONBLOCK) < 0)
74 return -1;
75
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)
81 {
82 if (set_nonblocking_flag (fd[0], true) != 0
83 || set_nonblocking_flag (fd[1], true) != 0)
84 goto fail;
85 }
86 # else
87 verify (O_NONBLOCK == 0);
88 # endif
89
90 return 0;
91
92 #else
93 /* Unix API. */
94
95 if (pipe (fd) < 0)
96 return -1;
97
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]. */
101
102 /* O_NONBLOCK handling.
103 On Unix platforms, O_NONBLOCK is defined by the system. Use fcntl(). */
104 if (flags & O_NONBLOCK)
105 {
106 int fcntl_flags;
107
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)
112 goto fail;
113 }
114
115 if (flags & O_CLOEXEC)
116 {
117 int fcntl_flags;
118
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)
123 goto fail;
124 }
125
126 # if O_BINARY
127 if (flags & O_BINARY)
128 {
129 setmode (fd[1], O_BINARY);
130 setmode (fd[0], O_BINARY);
131 }
132 else if (flags & O_TEXT)
133 {
134 setmode (fd[1], O_TEXT);
135 setmode (fd[0], O_TEXT);
136 }
137 # endif
138
139 return 0;
140
141 #endif
142
143 fail:
144 {
145 int saved_errno = errno;
146 close (fd[0]);
147 close (fd[1]);
148 errno = saved_errno;
149 return -1;
150 }
151 }