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