usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-spawn-pipe-child.c
blob536a1fe85b3517839ba2a2bd6fbd3e124eb698fe
1 /* Child program invoked by test-spawn-pipe-main.
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
17 #include <config.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #if defined _WIN32 && ! defined __CYGWIN__
28 /* Get declarations of the native Windows API functions. */
29 # define WIN32_LEAN_AND_MEAN
30 # include <windows.h>
31 #endif
33 /* Depending on arguments, this test intentionally closes stderr or
34 starts life with stderr closed. So, we arrange to have fd 10
35 (outside the range of interesting fd's during the test) set up to
36 duplicate the original stderr. */
38 #define BACKUP_STDERR_FILENO 10
39 #define ASSERT_STREAM myerr
40 #undef CONTINUE_AFTER_ASSERT
41 #include "macros.h"
43 static FILE *myerr;
45 /* In this file, we use only system functions, no overrides from gnulib. */
46 #undef abort
47 #undef atoi
48 #undef close
49 #undef fcntl
50 #undef fdopen
51 #undef fflush
52 #undef fprintf
53 #undef open
54 #undef read
55 #undef strcasestr
56 #undef strstr
57 #undef write
58 #if defined _WIN32 && !defined __CYGWIN__
59 # define fdopen _fdopen
60 #endif
62 #include "qemu.h"
64 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
65 static void __cdecl
66 gl_msvc_invalid_parameter_handler (const wchar_t *expression,
67 const wchar_t *function,
68 const wchar_t *file,
69 unsigned int line,
70 uintptr_t dummy)
73 #endif
75 /* Return non-zero if FD is open. */
76 static int
77 is_open (int fd)
79 #if defined _WIN32 && ! defined __CYGWIN__
80 /* On native Windows, the initial state of unassigned standard file
81 descriptors is that they are open but point to an
82 INVALID_HANDLE_VALUE, and there is no fcntl. */
83 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
84 #else
85 # ifndef F_GETFL
86 # error Please port fcntl to your platform
87 # endif
88 return 0 <= fcntl (fd, F_GETFL);
89 #endif
92 int
93 main (int argc, char *argv[])
95 /* fd 2 might be closed, but fd BACKUP_STDERR_FILENO is the original
96 stderr. */
97 myerr = fdopen (BACKUP_STDERR_FILENO, "w");
98 if (!myerr)
99 return 2;
101 ASSERT (argc == 2);
103 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
104 /* Avoid exceptions from within _get_osfhandle. */
105 _set_invalid_parameter_handler (gl_msvc_invalid_parameter_handler);
106 #endif
108 /* QEMU 6.1 in user-mode passes an open fd, usually = 3, that references
109 /dev/urandom. We need to ignore this fd. */
110 bool is_qemu = is_running_under_qemu_user ();
112 /* Read one byte from fd 0, and write its value plus one to fd 1.
113 fd 2 should be closed iff the argument is 1. Check that no other file
114 descriptors leaked. */
116 char buffer[2] = { 's', 't' };
118 ASSERT (read (STDIN_FILENO, buffer, 2) == 1);
120 buffer[0]++;
121 ASSERT (write (STDOUT_FILENO, buffer, 1) == 1);
123 switch (atoi (argv[1]))
125 case 0:
126 /* Expect fd 2 is open. */
127 ASSERT (is_open (STDERR_FILENO));
128 break;
129 case 1:
130 /* Expect fd 2 is closed.
131 But on HP-UX 11, fd 2 gets automatically re-opened to /dev/null if it
132 was closed. Similarly on Android and on native Windows. Future POSIX
133 will allow this, see <http://austingroupbugs.net/view.php?id=173>. */
134 #if !(defined __hpux || defined __ANDROID__ || (defined _WIN32 && ! defined __CYGWIN__))
135 if (!is_qemu)
136 ASSERT (! is_open (STDERR_FILENO));
137 #endif
138 break;
139 default:
140 ASSERT (0);
143 int fd;
144 for (fd = 3; fd < 7; fd++)
145 if (!(is_qemu && fd == 3))
147 errno = 0;
148 ASSERT (close (fd) == -1);
149 ASSERT (errno == EBADF);
152 return test_exit_status;