This source file includes following definitions.
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 __CYGWIN__
28 # include <process.h>
29 #else
30 # include <spawn.h>
31 #endif
32
33 #include "nonblocking.h"
34 #include "wait-process.h"
35
36 #include "macros.h"
37 #include "test-nonblocking-pipe.h"
38 #define PROG_ROLE "main"
39 #include "test-nonblocking-writer.h"
40
41 int
42 main (int argc, char *argv[])
43 {
44 const char *child_path;
45 int test;
46 int fd[2];
47 pid_t child;
48 int exitcode;
49
50 child_path = argv[1];
51 test = atoi (argv[2]);
52
53
54 ASSERT (pipe (fd) >= 0);
55
56
57
58
59 if (fd[0] != STDIN_FILENO)
60 {
61 ASSERT (dup2 (fd[0], STDIN_FILENO) >= 0);
62 close (fd[0]);
63 }
64 if (fd[1] != STDOUT_FILENO)
65 {
66 ASSERT (dup2 (fd[1], STDOUT_FILENO) >= 0);
67 close (fd[1]);
68 }
69
70
71 if (test & 1)
72 ASSERT (set_nonblocking_flag (STDOUT_FILENO, true) >= 0);
73 if (test & 2)
74 ASSERT (set_nonblocking_flag (STDIN_FILENO, true) >= 0);
75
76
77 {
78 const char *child_argv[3];
79
80 child_argv[0] = child_path;
81 child_argv[1] = argv[2];
82 child_argv[2] = NULL;
83
84 #if defined _WIN32 && ! defined __CYGWIN__
85 child = _spawnvpe (P_NOWAIT, child_path, child_argv,
86 (const char **) environ);
87 ASSERT (child >= 0);
88 #else
89 {
90 pid_t child_pid;
91 int err =
92 posix_spawnp (&child_pid, child_path, NULL, NULL, (char **) child_argv,
93 environ);
94 ASSERT (err == 0);
95 child = child_pid;
96 }
97 #endif
98 }
99
100
101 close (STDIN_FILENO);
102
103 exitcode =
104 main_writer_loop (test, PIPE_DATA_BLOCK_SIZE, STDOUT_FILENO, false);
105
106 {
107 int err =
108 wait_subprocess (child, child_path, false, false, false, false, NULL);
109 ASSERT (err == 0);
110 }
111
112 return exitcode;
113 }