1 /* Test of create_pipe_bidi/wait_subprocess.
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)
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/>. */
19 #include "spawn-pipe.h"
20 #include "wait-process.h"
27 /* Depending on arguments, this test intentionally closes stderr or
28 starts life with stderr closed. So, we arrange to have fd 10
29 (outside the range of interesting fd's during the test) set up to
30 duplicate the original stderr. */
32 #define BACKUP_STDERR_FILENO 10
33 #define ASSERT_STREAM myerr
38 /* Create a bi-directional pipe to a test child, and validate that the
39 child program returns the expected output.
40 PROG is the program to run in the child process.
41 STDERR_CLOSED is true if we have already closed fd 2. */
43 test_pipe (const char *prog
, bool stderr_closed
)
48 char buffer
[2] = { 'a', 't' };
52 argv
[1] = (stderr_closed
? "1" : "0");
54 pid
= create_pipe_bidi (prog
, prog
, argv
, NULL
, false, true, true, fd
);
56 ASSERT (STDERR_FILENO
< fd
[0]);
57 ASSERT (STDERR_FILENO
< fd
[1]);
59 /* Push child's input. */
60 ASSERT (write (fd
[1], buffer
, 1) == 1);
61 ASSERT (close (fd
[1]) == 0);
63 /* Get child's output. */
64 ASSERT (read (fd
[0], buffer
, 2) == 1);
67 ASSERT (wait_subprocess (pid
, prog
, true, false, true, true, NULL
) == 0);
68 ASSERT (close (fd
[0]) == 0);
70 /* Check the result. */
71 ASSERT (buffer
[0] == 'b');
72 ASSERT (buffer
[1] == 't');
76 main (int argc
, char *argv
[])
83 fprintf (stderr
, "%s: need 2 arguments\n", argv
[0]);
86 /* We might close fd 2 later, so save it in fd 10. */
87 if (dup2 (STDERR_FILENO
, BACKUP_STDERR_FILENO
) != BACKUP_STDERR_FILENO
88 || (myerr
= fdopen (BACKUP_STDERR_FILENO
, "w")) == NULL
)
91 /* Selectively close various standard fds, to verify the child process is
92 not impacted by this. */
93 test
= atoi (argv
[2]);
128 /* Plug any file descriptor leaks inherited from outside world before
129 starting, so that child has a clean slate (at least for the fds that we
130 might be manipulating). */
131 for (fd
= 3; fd
< 7; fd
++)
134 test_pipe (argv
[1], test
>= 4);
136 return test_exit_status
;