| 1 |
/* Test of pipe. |
| 2 |
Copyright (C) 2009-2025 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, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
#include <config.h> |
| 18 |
|
| 19 |
#include <unistd.h> |
| 20 |
|
| 21 |
#include "signature.h" |
| 22 |
SIGNATURE_CHECK (pipe, int, (int[2])); |
| 23 |
|
| 24 |
#include <fcntl.h> |
| 25 |
|
| 26 |
#if defined _WIN32 && ! defined __CYGWIN__ |
| 27 |
/* Get declarations of the native Windows API functions. */ |
| 28 |
# define WIN32_LEAN_AND_MEAN |
| 29 |
# include <windows.h> |
| 30 |
/* Get _get_osfhandle. */ |
| 31 |
# if GNULIB_MSVC_NOTHROW |
| 32 |
# include "msvc-nothrow.h" |
| 33 |
# else |
| 34 |
# include <io.h> |
| 35 |
# endif |
| 36 |
#endif |
| 37 |
|
| 38 |
#include "binary-io.h" |
| 39 |
#include "macros.h" |
| 40 |
|
| 41 |
/* Return true if FD is open. */ |
| 42 |
static bool |
| 43 |
is_open (int fd) |
| 44 |
{ |
| 45 |
#if defined _WIN32 && ! defined __CYGWIN__ |
| 46 |
/* On native Windows, the initial state of unassigned standard file |
| 47 |
descriptors is that they are open but point to an |
| 48 |
INVALID_HANDLE_VALUE, and there is no fcntl. */ |
| 49 |
return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE; |
| 50 |
#else |
| 51 |
# ifndef F_GETFL |
| 52 |
# error Please port fcntl to your platform |
| 53 |
# endif |
| 54 |
return 0 <= fcntl (fd, F_GETFL); |
| 55 |
#endif |
| 56 |
} |
| 57 |
|
| 58 |
/* Return true if FD is not inherited to child processes. */ |
| 59 |
static bool |
| 60 |
is_cloexec (int fd) |
| 61 |
{ |
| 62 |
#if defined _WIN32 && ! defined __CYGWIN__ |
| 63 |
HANDLE h = (HANDLE) _get_osfhandle (fd); |
| 64 |
DWORD flags; |
| 65 |
ASSERT (GetHandleInformation (h, &flags)); |
| 66 |
return (flags & HANDLE_FLAG_INHERIT) == 0; |
| 67 |
#else |
| 68 |
int flags; |
| 69 |
ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0); |
| 70 |
return (flags & FD_CLOEXEC) != 0; |
| 71 |
#endif |
| 72 |
} |
| 73 |
|
| 74 |
/* Return true if FD is in non-blocking mode. */ |
| 75 |
static bool |
| 76 |
is_nonblocking (int fd) |
| 77 |
{ |
| 78 |
#if defined _WIN32 && ! defined __CYGWIN__ |
| 79 |
/* We don't use the non-blocking mode for sockets here. */ |
| 80 |
return 0; |
| 81 |
#else |
| 82 |
int flags; |
| 83 |
ASSERT ((flags = fcntl (fd, F_GETFL)) >= 0); |
| 84 |
return (flags & O_NONBLOCK) != 0; |
| 85 |
#endif |
| 86 |
} |
| 87 |
|
| 88 |
int |
| 89 |
main () |
| 90 |
{ |
| 91 |
int fd[2]; |
| 92 |
|
| 93 |
fd[0] = -1; |
| 94 |
fd[1] = -1; |
| 95 |
ASSERT (pipe (fd) >= 0); |
| 96 |
ASSERT (fd[0] >= 0); |
| 97 |
ASSERT (fd[1] >= 0); |
| 98 |
ASSERT (fd[0] != fd[1]); |
| 99 |
ASSERT (is_open (fd[0])); |
| 100 |
ASSERT (is_open (fd[1])); |
| 101 |
ASSERT (!is_cloexec (fd[0])); |
| 102 |
ASSERT (!is_cloexec (fd[1])); |
| 103 |
ASSERT (!is_nonblocking (fd[0])); |
| 104 |
ASSERT (!is_nonblocking (fd[1])); |
| 105 |
|
| 106 |
return test_exit_status; |
| 107 |
} |