maint: update copyright
[gnulib.git] / tests / test-nonblocking-pipe-main.c
1 /* Test for nonblocking read and write on pipes.
2
3    Copyright (C) 2011-2014 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>.  */
17
18 #include <config.h>
19
20 #include <errno.h>
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/time.h>
26
27 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
28 # include <process.h>
29 #else
30 # include <spawn.h>
31 #endif
32
33 #include "nonblocking.h"
34 #include "wait-process.h"
35 #include "progname.h"
36
37 #include "macros.h"
38 #include "test-nonblocking-pipe.h"
39 #define PROG_ROLE "main"
40 #include "test-nonblocking-writer.h"
41
42 int
43 main (int argc, char *argv[])
44 {
45   const char *child_path;
46   int test;
47   int fd[2];
48   int child;
49   int exitcode;
50
51   set_program_name (argv[0]);
52
53   child_path = argv[1];
54   test = atoi (argv[2]);
55
56   /* Create a pipe.  */
57   ASSERT (pipe (fd) >= 0);
58
59   /* Map fd[0] to STDIN_FILENO and fd[1] to STDOUT_FILENO, because on Windows,
60      the only three file descriptors that are inherited by child processes are
61      STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.  */
62   if (fd[0] != STDIN_FILENO)
63     {
64       ASSERT (dup2 (fd[0], STDIN_FILENO) >= 0);
65       close (fd[0]);
66     }
67   if (fd[1] != STDOUT_FILENO)
68     {
69       ASSERT (dup2 (fd[1], STDOUT_FILENO) >= 0);
70       close (fd[1]);
71     }
72
73   /* Prepare the file descriptors.  */
74   if (test & 1)
75     ASSERT (set_nonblocking_flag (STDOUT_FILENO, true) >= 0);
76   if (test & 2)
77     ASSERT (set_nonblocking_flag (STDIN_FILENO, true) >= 0);
78
79   /* Spawn the child process.  */
80   {
81     const char *child_argv[3];
82
83     child_argv[0] = child_path;
84     child_argv[1] = argv[2];
85     child_argv[2] = NULL;
86
87 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
88     child = spawnvpe (P_NOWAIT, child_path, child_argv,
89                       (const char **) environ);
90     ASSERT (child >= 0);
91 #else
92     {
93       pid_t child_pid;
94       int err =
95         posix_spawnp (&child_pid, child_path, NULL, NULL, (char **) child_argv,
96                       environ);
97       ASSERT (err == 0);
98       child = child_pid;
99     }
100 #endif
101   }
102
103   /* Close unused file descriptors.  */
104   close (STDIN_FILENO);
105
106   exitcode =
107     main_writer_loop (test, PIPE_DATA_BLOCK_SIZE, STDOUT_FILENO, false);
108
109   {
110     int err =
111       wait_subprocess (child, child_path, false, false, false, false, NULL);
112     ASSERT (err == 0);
113   }
114
115   return exitcode;
116 }