1 /* Test for nonblocking read and write on pipes.
3 Copyright (C) 2011 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
33 #include "nonblocking.h"
34 #include "wait-process.h"
37 #include "test-nonblocking-pipe.h"
38 #define PROG_ROLE "main"
39 #include "test-nonblocking-writer.h"
42 main (int argc, char *argv[])
44 const char *child_path = argv[1];
45 int test = atoi (argv[2]);
51 ASSERT (pipe (fd) >= 0);
53 /* Map fd[0] to STDIN_FILENO and fd[1] to STDOUT_FILENO, because on Windows,
54 the only three file descriptors that are inherited by child processes are
55 STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO. */
56 if (fd[0] != STDIN_FILENO)
58 ASSERT (dup2 (fd[0], STDIN_FILENO) >= 0);
61 if (fd[1] != STDOUT_FILENO)
63 ASSERT (dup2 (fd[1], STDOUT_FILENO) >= 0);
67 /* Prepare the file descriptors. */
69 ASSERT (set_nonblocking_flag (STDOUT_FILENO, true) >= 0);
71 ASSERT (set_nonblocking_flag (STDIN_FILENO, true) >= 0);
73 /* Spawn the child process. */
75 const char *child_argv[3];
77 child_argv[0] = child_path;
78 child_argv[1] = argv[2];
81 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
82 child = spawnvpe (P_NOWAIT, child_path, child_argv,
83 (const char **) environ);
89 posix_spawnp (&child_pid, child_path, NULL, NULL, (char **) child_argv,
97 /* Close unused file descriptors. */
101 main_writer_loop (test, PIPE_DATA_BLOCK_SIZE, STDOUT_FILENO, false);
105 wait_subprocess (child, child_path, false, false, false, false, NULL);