usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-spawn-pipe-main.c
blob612d47bed54bd3af479e88e44e7cf6a3d77cd813
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)
7 any later version.
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/>. */
17 #include <config.h>
19 #include "spawn-pipe.h"
20 #include "wait-process.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.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
34 #include "macros.h"
36 static FILE *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. */
42 static void
43 test_pipe (const char *prog, bool stderr_closed)
45 int fd[2];
46 const char *argv[3];
47 pid_t pid;
48 char buffer[2] = { 'a', 't' };
50 /* Set up child. */
51 argv[0] = prog;
52 argv[1] = (stderr_closed ? "1" : "0");
53 argv[2] = NULL;
54 pid = create_pipe_bidi (prog, prog, argv, NULL, false, true, true, fd);
55 ASSERT (0 <= pid);
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);
66 /* Wait for child. */
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');
75 int
76 main (int argc, char *argv[])
78 int test;
79 int fd;
81 if (argc != 3)
83 fprintf (stderr, "%s: need 2 arguments\n", argv[0]);
84 return 2;
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)
89 return 2;
91 /* Selectively close various standard fds, to verify the child process is
92 not impacted by this. */
93 test = atoi (argv[2]);
94 switch (test)
96 case 0:
97 break;
98 case 1:
99 close (0);
100 break;
101 case 2:
102 close (1);
103 break;
104 case 3:
105 close (0);
106 close (1);
107 break;
108 case 4:
109 close (2);
110 break;
111 case 5:
112 close (0);
113 close (2);
114 break;
115 case 6:
116 close (1);
117 close (2);
118 break;
119 case 7:
120 close (0);
121 close (1);
122 close (2);
123 break;
124 default:
125 ASSERT (false);
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++)
132 close (fd);
134 test_pipe (argv[1], test >= 4);
136 return test_exit_status;