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