NEWS.stable: update
[gnulib.git] / tests / test-pipe2.c
1 /* Test of pipe2.
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 3, 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
15    along 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 #include <unistd.h>
21
22 #include "signature.h"
23 SIGNATURE_CHECK (pipe2, int, (int[2], int));
24
25 #include <fcntl.h>
26 #include <stdbool.h>
27
28 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
29 /* Get declarations of the Win32 API functions.  */
30 # define WIN32_LEAN_AND_MEAN
31 # include <windows.h>
32 #endif
33
34 #include "binary-io.h"
35 #include "macros.h"
36 #if GNULIB_NONBLOCKING
37 # include "nonblocking.h"
38 #endif
39
40 /* Return true if FD is open.  */
41 static bool
42 is_open (int fd)
43 {
44 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
45   /* On Win32, the initial state of unassigned standard file
46      descriptors is that they are open but point to an
47      INVALID_HANDLE_VALUE, and there is no fcntl.  */
48   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
49 #else
50 # ifndef F_GETFL
51 #  error Please port fcntl to your platform
52 # endif
53   return 0 <= fcntl (fd, F_GETFL);
54 #endif
55 }
56
57 /* Return true if FD is not inherited to child processes.  */
58 static bool
59 is_cloexec (int fd)
60 {
61 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
62   HANDLE h = (HANDLE) _get_osfhandle (fd);
63   DWORD flags;
64   ASSERT (GetHandleInformation (h, &flags));
65   return (flags & HANDLE_FLAG_INHERIT) == 0;
66 #else
67   int flags;
68   ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0);
69   return (flags & FD_CLOEXEC) != 0;
70 #endif
71 }
72
73 #if ! GNULIB_NONBLOCKING
74 static int
75 get_nonblocking_flag (int fd)
76 {
77 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
78   return 0;
79 # else
80 #  ifndef F_GETFL
81 #   error Please port fcntl to your platform
82 #  endif
83   int flags;
84   ASSERT ((flags = fcntl (fd, F_GETFL)) >= 0);
85   return (flags & O_NONBLOCK) != 0;
86 # endif
87 }
88 #endif
89
90 int
91 main ()
92 {
93   int use_nonblocking;
94   int use_cloexec;
95
96   for (use_nonblocking = 0; use_nonblocking <= !!O_NONBLOCK; use_nonblocking++)
97     for (use_cloexec = 0; use_cloexec <= !!O_CLOEXEC; use_cloexec++)
98       {
99         int o_flags;
100         int fd[2];
101
102         o_flags = 0;
103         if (use_nonblocking)
104           o_flags |= O_NONBLOCK;
105         if (use_cloexec)
106           o_flags |= O_CLOEXEC;
107
108         fd[0] = -1;
109         fd[1] = -1;
110         ASSERT (pipe2 (fd, o_flags) >= 0);
111         ASSERT (fd[0] >= 0);
112         ASSERT (fd[1] >= 0);
113         ASSERT (fd[0] != fd[1]);
114         ASSERT (is_open (fd[0]));
115         ASSERT (is_open (fd[1]));
116         if (use_cloexec)
117           {
118             ASSERT (is_cloexec (fd[0]));
119             ASSERT (is_cloexec (fd[1]));
120           }
121         else
122           {
123             ASSERT (!is_cloexec (fd[0]));
124             ASSERT (!is_cloexec (fd[1]));
125           }
126         if (use_nonblocking)
127           {
128             ASSERT (get_nonblocking_flag (fd[0]) == 1);
129             ASSERT (get_nonblocking_flag (fd[1]) == 1);
130           }
131         else
132           {
133             ASSERT (get_nonblocking_flag (fd[0]) == 0);
134             ASSERT (get_nonblocking_flag (fd[1]) == 0);
135           }
136
137         ASSERT (close (fd[0]) == 0);
138         ASSERT (close (fd[1]) == 0);
139       }
140
141   return 0;
142 }