1 /* Test of create_pipe_bidi/wait_subprocess.
2 Copyright (C) 2009 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. */
21 #include "wait-process.h"
30 /* Depending on arguments, this test intentionally closes stderr or
31 starts life with stderr closed. So, we arrange to have fd 10
32 (outside the range of interesting fd's during the test) set up to
33 duplicate the original stderr. */
35 #define BACKUP_STDERR_FILENO 10
38 #define ASSERT(expr) \
43 fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
50 /* Code executed by the child process. argv[1] = "child". */
52 child_main (int argc, char *argv[])
54 char buffer[2] = { 's', 't' };
60 /* Read one byte from fd 0, and write its value plus one to fd 1.
61 fd 2 should be closed iff the argument is 1. Check that no other file
62 descriptors leaked. */
64 ASSERT (read (STDIN_FILENO, buffer, 2) == 1);
67 ASSERT (write (STDOUT_FILENO, buffer, 1) == 1);
70 ret = dup2 (STDERR_FILENO, STDERR_FILENO);
71 switch (atoi (argv[2]))
74 /* Expect fd 2 is open. */
75 ASSERT (ret == STDERR_FILENO);
78 /* Expect fd 2 is closed. */
80 ASSERT (errno == EBADF);
86 for (fd = 3; fd < 7; fd++)
89 ASSERT (close (fd) == -1);
90 ASSERT (errno == EBADF);
96 /* Create a bi-directional pipe to a test child, and validate that the
97 child program returns the expected output. The child is the same
98 program as the parent ARGV0, but with different arguments.
99 STDERR_CLOSED is true if we have already closed fd 2. */
101 test_pipe (const char *argv0, bool stderr_closed)
106 char buffer[2] = { 'a', 't' };
109 argv[0] = (char *) argv0;
110 argv[1] = (char *) "child";
111 argv[2] = (char *) (stderr_closed ? "1" : "0");
113 pid = create_pipe_bidi (argv0, argv0, argv, false, true, true, fd);
115 ASSERT (STDERR_FILENO < fd[0]);
116 ASSERT (STDERR_FILENO < fd[1]);
118 /* Push child's input. */
119 ASSERT (write (fd[1], buffer, 1) == 1);
120 ASSERT (close (fd[1]) == 0);
122 /* Get child's output. */
123 ASSERT (read (fd[0], buffer, 2) == 1);
125 /* Wait for child. */
126 ASSERT (wait_subprocess (pid, argv0, true, false, true, true, NULL) == 0);
127 ASSERT (close (fd[0]) == 0);
129 /* Check the result. */
130 ASSERT (buffer[0] == 'b');
131 ASSERT (buffer[1] == 't');
134 /* Code executed by the parent process. */
136 parent_main (int argc, char *argv[])
143 /* Selectively close various standard fds, to verify the child process is
144 not impacted by this. */
145 test = atoi (argv[1]);
180 /* Plug any file descriptor leaks inherited from outside world before
181 starting, so that child has a clean slate (at least for the fds that we
182 might be manipulating). */
183 for (fd = 3; fd < 7; fd++)
186 test_pipe (argv[0], test >= 4);
192 main (int argc, char *argv[])
196 fprintf (stderr, "%s: need arguments\n", argv[0]);
199 if (strcmp (argv[1], "child") == 0)
201 /* fd 2 might be closed, but fd BACKUP_STDERR_FILENO is the original
203 myerr = fdopen (BACKUP_STDERR_FILENO, "w");
206 return child_main (argc, argv);
208 /* We might close fd 2 later, so save it in fd 10. */
209 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
210 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
212 return parent_main (argc, argv);