1 /* Test of create_pipe_bidi/wait_subprocess.
2 Copyright (C) 2009-2011 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 #include "spawn-pipe.h"
21 #include "wait-process.h"
29 /* Depending on arguments, this test intentionally closes stderr or
30 starts life with stderr closed. So, we arrange to have fd 10
31 (outside the range of interesting fd's during the test) set up to
32 duplicate the original stderr. */
34 #define BACKUP_STDERR_FILENO 10
35 #define ASSERT_STREAM myerr
40 /* Create a bi-directional pipe to a test child, and validate that the
41 child program returns the expected output.
42 PROG is the program to run in the child process.
43 STDERR_CLOSED is true if we have already closed fd 2. */
45 test_pipe (const char *prog, bool stderr_closed)
50 char buffer[2] = { 'a', 't' };
53 argv[0] = (char *) prog;
54 argv[1] = (char *) (stderr_closed ? "1" : "0");
56 pid = create_pipe_bidi (prog, prog, argv, false, true, true, fd);
58 ASSERT (STDERR_FILENO < fd[0]);
59 ASSERT (STDERR_FILENO < fd[1]);
61 /* Push child's input. */
62 ASSERT (write (fd[1], buffer, 1) == 1);
63 ASSERT (close (fd[1]) == 0);
65 /* Get child's output. */
66 ASSERT (read (fd[0], buffer, 2) == 1);
69 ASSERT (wait_subprocess (pid, prog, true, false, true, true, NULL) == 0);
70 ASSERT (close (fd[0]) == 0);
72 /* Check the result. */
73 ASSERT (buffer[0] == 'b');
74 ASSERT (buffer[1] == 't');
78 main (int argc, char *argv[])
85 fprintf (stderr, "%s: need 2 arguments\n", argv[0]);
88 /* We might close fd 2 later, so save it in fd 10. */
89 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
90 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
93 /* Selectively close various standard fds, to verify the child process is
94 not impacted by this. */
95 test = atoi (argv[2]);
130 /* Plug any file descriptor leaks inherited from outside world before
131 starting, so that child has a clean slate (at least for the fds that we
132 might be manipulating). */
133 for (fd = 3; fd < 7; fd++)
136 test_pipe (argv[1], test >= 4);