NEWS.stable: log cherry-pick [7c886ca]->[802ee67] Fix link errors in tests: openat...
[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 (_pipe (fd, 4096, flags & ~O_NONBLOCK) < 0)
70     return -1;
71
72   if (flags & O_NONBLOCK)
73     {
74       if (set_nonblocking_flag (fd[0], true) != 0
75           || set_nonblocking_flag (fd[1], true) != 0)
76         goto fail;
77     }
78
79   return 0;
80
81 #else
82 /* Unix API.  */
83
84   if (pipe (fd) < 0)
85     return -1;
86
87   /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/pipe.html>
88      says that initially, the O_NONBLOCK and FD_CLOEXEC flags are cleared on
89      both fd[0] and fd[1].  */
90
91   if (flags & O_NONBLOCK)
92     {
93       int fcntl_flags;
94
95       if ((fcntl_flags = fcntl (fd[1], F_GETFL, 0)) < 0
96           || fcntl (fd[1], F_SETFL, fcntl_flags | O_NONBLOCK) == -1
97           || (fcntl_flags = fcntl (fd[0], F_GETFL, 0)) < 0
98           || fcntl (fd[0], F_SETFL, fcntl_flags | O_NONBLOCK) == -1)
99         goto fail;
100     }
101
102   if (flags & O_CLOEXEC)
103     {
104       int fcntl_flags;
105
106       if ((fcntl_flags = fcntl (fd[1], F_GETFD, 0)) < 0
107           || fcntl (fd[1], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1
108           || (fcntl_flags = fcntl (fd[0], F_GETFD, 0)) < 0
109           || fcntl (fd[0], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
110         goto fail;
111     }
112
113 # if O_BINARY
114   if (flags & O_BINARY)
115     {
116       setmode (fd[1], O_BINARY);
117       setmode (fd[0], O_BINARY);
118     }
119   else if (flags & O_TEXT)
120     {
121       setmode (fd[1], O_TEXT);
122       setmode (fd[0], O_TEXT);
123     }
124 # endif
125
126   return 0;
127
128 #endif
129
130  fail:
131   {
132     int saved_errno = errno;
133     close (fd[0]);
134     close (fd[1]);
135     errno = saved_errno;
136     return -1;
137   }
138 }