Avoid running into nonexistent system calls repeatedly.
[pspp] / lib / pipe2.c
1 /* Create a pipe, with specific opening flags.
2    Copyright (C) 2009 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 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
27 /* Native Woe32 API.  */
28
29 # include <io.h>
30
31 #else
32 /* Unix API.  */
33
34 # ifndef O_CLOEXEC
35 #  define O_CLOEXEC 0
36 # endif
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 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
64 /* Native Woe32 API.  */
65
66   /* Check the supported flags.  */
67   if ((flags & ~(O_CLOEXEC | O_BINARY | O_TEXT)) != 0)
68     {
69       errno = EINVAL;
70       return -1;
71     }
72
73   return _pipe (fd, 4096, flags);
74
75 #else
76 /* Unix API.  */
77
78   /* Check the supported flags.  */
79   if ((flags & ~(O_CLOEXEC | O_NONBLOCK | O_TEXT | O_BINARY)) != 0)
80     {
81       errno = EINVAL;
82       return -1;
83     }
84
85   if (pipe (fd) < 0)
86     return -1;
87
88   /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/pipe.html>
89      says that initially, the O_NONBLOCK and FD_CLOEXEC flags are cleared on
90      both fd[0] amd fd[1].  */
91
92   if (flags & O_NONBLOCK)
93     {
94       int fcntl_flags;
95
96       if ((fcntl_flags = fcntl (fd[1], F_GETFL, 0)) < 0
97           || fcntl (fd[1], F_SETFL, fcntl_flags | O_NONBLOCK) == -1
98           || (fcntl_flags = fcntl (fd[0], F_GETFL, 0)) < 0
99           || fcntl (fd[0], F_SETFL, fcntl_flags | O_NONBLOCK) == -1)
100         goto fail;
101     }
102
103   if (flags & O_CLOEXEC)
104     {
105       int fcntl_flags;
106
107       if ((fcntl_flags = fcntl (fd[1], F_GETFD, 0)) < 0
108           || fcntl (fd[1], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1
109           || (fcntl_flags = fcntl (fd[0], F_GETFD, 0)) < 0
110           || fcntl (fd[0], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
111         goto fail;
112     }
113
114 # if O_BINARY
115   if (flags & O_BINARY)
116     {
117       setmode (fd[1], O_BINARY);
118       setmode (fd[0], O_BINARY);
119     }
120   else if (flags & O_TEXT)
121     {
122       setmode (fd[1], O_TEXT);
123       setmode (fd[0], O_TEXT);
124     }
125 # endif
126
127   return 0;
128
129  fail:
130   {
131     int saved_errno = errno;
132     close (fd[0]);
133     close (fd[1]);
134     errno = saved_errno;
135     return -1;
136   }
137
138 #endif
139 }