1 /* Child program invoked by test-spawn-pipe-main.
2 Copyright (C) 2009-2025 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)
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/>. */
27 #if defined _WIN32 && ! defined __CYGWIN__
28 /* Get declarations of the native Windows API functions. */
29 # define WIN32_LEAN_AND_MEAN
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
45 /* In this file, we use only system functions, no overrides from gnulib. */
58 #if defined _WIN32 && !defined __CYGWIN__
59 # define fdopen _fdopen
64 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
66 gl_msvc_invalid_parameter_handler (const wchar_t *expression
,
67 const wchar_t *function
,
75 /* Return non-zero if FD is open. */
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
;
86 # error Please port fcntl to your platform
88 return 0 <= fcntl (fd
, F_GETFL
);
93 main (int argc
, char *argv
[])
95 /* fd 2 might be closed, but fd BACKUP_STDERR_FILENO is the original
97 myerr
= fdopen (BACKUP_STDERR_FILENO
, "w");
103 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
104 /* Avoid exceptions from within _get_osfhandle. */
105 _set_invalid_parameter_handler (gl_msvc_invalid_parameter_handler
);
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);
121 ASSERT (write (STDOUT_FILENO
, buffer
, 1) == 1);
123 switch (atoi (argv
[1]))
126 /* Expect fd 2 is open. */
127 ASSERT (is_open (STDERR_FILENO
));
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 <https://austingroupbugs.net/view.php?id=173>. */
134 #if !(defined __hpux || defined __ANDROID__ || (defined _WIN32 && ! defined __CYGWIN__))
136 ASSERT (! is_open (STDERR_FILENO
));
144 for (fd
= 3; fd
< 7; fd
++)
145 if (!(is_qemu
&& fd
== 3))
148 ASSERT (close (fd
) == -1);
149 ASSERT (errno
== EBADF
);
152 return test_exit_status
;