1 /* Child program invoked by test-spawn-pipe-main.
2    Copyright (C) 2009-2020 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 <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 
25 #if defined _WIN32 && ! defined __CYGWIN__
26 /* Get declarations of the native Windows API functions.  */
27 # define WIN32_LEAN_AND_MEAN
28 # include <windows.h>
29 #endif
30 
31 /* Depending on arguments, this test intentionally closes stderr or
32    starts life with stderr closed.  So, we arrange to have fd 10
33    (outside the range of interesting fd's during the test) set up to
34    duplicate the original stderr.  */
35 
36 #define BACKUP_STDERR_FILENO 10
37 #define ASSERT_STREAM myerr
38 #include "macros.h"
39 
40 static FILE *myerr;
41 
42 /* In this file, we use only system functions, no overrides from gnulib.  */
43 #undef atoi
44 #undef close
45 #undef fcntl
46 #undef fdopen
47 #undef fflush
48 #undef fprintf
49 #undef read
50 #undef write
51 
52 /* Return non-zero if FD is open.  */
53 static int
is_open(int fd)54 is_open (int fd)
55 {
56 #if defined _WIN32 && ! defined __CYGWIN__
57   /* On native Windows, the initial state of unassigned standard file
58      descriptors is that they are open but point to an
59      INVALID_HANDLE_VALUE, and there is no fcntl.  */
60   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
61 #else
62 # ifndef F_GETFL
63 #  error Please port fcntl to your platform
64 # endif
65   return 0 <= fcntl (fd, F_GETFL);
66 #endif
67 }
68 
69 int
main(int argc,char * argv[])70 main (int argc, char *argv[])
71 {
72   char buffer[2] = { 's', 't' };
73   int fd;
74 
75   /* fd 2 might be closed, but fd BACKUP_STDERR_FILENO is the original
76      stderr.  */
77   myerr = fdopen (BACKUP_STDERR_FILENO, "w");
78   if (!myerr)
79     return 2;
80 
81   ASSERT (argc == 2);
82 
83   /* Read one byte from fd 0, and write its value plus one to fd 1.
84      fd 2 should be closed iff the argument is 1.  Check that no other file
85      descriptors leaked.  */
86 
87   ASSERT (read (STDIN_FILENO, buffer, 2) == 1);
88 
89   buffer[0]++;
90   ASSERT (write (STDOUT_FILENO, buffer, 1) == 1);
91 
92   switch (atoi (argv[1]))
93     {
94     case 0:
95       /* Expect fd 2 is open.  */
96       ASSERT (is_open (STDERR_FILENO));
97       break;
98     case 1:
99       /* Expect fd 2 is closed.
100          But on HP-UX 11, fd 2 gets automatically re-opened to /dev/null if it
101          was closed.  Similarly on native Windows.  Future POSIX will allow
102          this, see <http://austingroupbugs.net/view.php?id=173>.  */
103 #if !(defined __hpux || (defined _WIN32 && ! defined __CYGWIN__))
104       ASSERT (! is_open (STDERR_FILENO));
105 #endif
106       break;
107     default:
108       ASSERT (0);
109     }
110 
111   for (fd = 3; fd < 7; fd++)
112     {
113       errno = 0;
114       ASSERT (close (fd) == -1);
115       ASSERT (errno == EBADF);
116     }
117 
118   return 0;
119 }
120