1 /* Child program invoked by test-spawn-pipe-main.
2 Copyright (C) 2009-2023 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 <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>
26
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
32
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. */
37
38 #define BACKUP_STDERR_FILENO 10
39 #define ASSERT_STREAM myerr
40 #include "macros.h"
41
42 static FILE *myerr;
43
44 /* In this file, we use only system functions, no overrides from gnulib. */
45 #undef atoi
46 #undef close
47 #undef fcntl
48 #undef fdopen
49 #undef fflush
50 #undef fprintf
51 #undef open
52 #undef read
53 #undef strcasestr
54 #undef strstr
55 #undef write
56 #if defined _WIN32 && !defined __CYGWIN__
57 # define fdopen _fdopen
58 #endif
59
60 #include "qemu.h"
61
62 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
63 static void __cdecl
64 gl_msvc_invalid_parameter_handler (const wchar_t *expression,
65 const wchar_t *function,
66 const wchar_t *file,
67 unsigned int line,
68 uintptr_t dummy)
69 {
70 }
71 #endif
72
73 /* Return non-zero if FD is open. */
74 static int
75 is_open (int fd)
76 {
77 #if defined _WIN32 && ! defined __CYGWIN__
78 /* On native Windows, the initial state of unassigned standard file
79 descriptors is that they are open but point to an
80 INVALID_HANDLE_VALUE, and there is no fcntl. */
81 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
82 #else
83 # ifndef F_GETFL
84 # error Please port fcntl to your platform
85 # endif
86 return 0 <= fcntl (fd, F_GETFL);
87 #endif
88 }
89
90 int
91 main (int argc, char *argv[])
92 {
93 /* fd 2 might be closed, but fd BACKUP_STDERR_FILENO is the original
94 stderr. */
95 myerr = fdopen (BACKUP_STDERR_FILENO, "w");
96 if (!myerr)
97 return 2;
98
99 ASSERT (argc == 2);
100
101 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
102 /* Avoid exceptions from within _get_osfhandle. */
103 _set_invalid_parameter_handler (gl_msvc_invalid_parameter_handler);
104 #endif
105
106 /* QEMU 6.1 in user-mode passes an open fd, usually = 3, that references
107 /dev/urandom. We need to ignore this fd. */
108 bool is_qemu = is_running_under_qemu_user ();
109
110 /* Read one byte from fd 0, and write its value plus one to fd 1.
111 fd 2 should be closed iff the argument is 1. Check that no other file
112 descriptors leaked. */
113
114 char buffer[2] = { 's', 't' };
115
116 ASSERT (read (STDIN_FILENO, buffer, 2) == 1);
117
118 buffer[0]++;
119 ASSERT (write (STDOUT_FILENO, buffer, 1) == 1);
120
121 switch (atoi (argv[1]))
122 {
123 case 0:
124 /* Expect fd 2 is open. */
125 ASSERT (is_open (STDERR_FILENO));
126 break;
127 case 1:
128 /* Expect fd 2 is closed.
129 But on HP-UX 11, fd 2 gets automatically re-opened to /dev/null if it
130 was closed. Similarly on Android and on native Windows. Future POSIX
131 will allow this, see <http://austingroupbugs.net/view.php?id=173>. */
132 #if !(defined __hpux || defined __ANDROID__ || (defined _WIN32 && ! defined __CYGWIN__))
133 if (!is_qemu)
134 ASSERT (! is_open (STDERR_FILENO));
135 #endif
136 break;
137 default:
138 ASSERT (0);
139 }
140
141 int fd;
142 for (fd = 3; fd < 7; fd++)
143 if (!(is_qemu && fd == 3))
144 {
145 errno = 0;
146 ASSERT (close (fd) == -1);
147 ASSERT (errno == EBADF);
148 }
149
150 return 0;
151 }