annotate lib/pipe.c @ 7292:17785d5bede0

Fix docstrings
author Sergey Poznyakoff <gray@gnu.org.ua>
date Sun, 10 Sep 2006 11:52:44 +0000
parents 1b0092424a44
children 1c4ed7637c24
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4940
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Creation of subprocesses, communicating via pipes.
6751
1b0092424a44 Include <unistd.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
2 Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
4940
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 any later version.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
5848
a48fb0e98c8c *** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents: 5580
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
4940
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #ifdef HAVE_CONFIG_H
6259
96c32553b4c6 Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
21 # include <config.h>
4940
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 /* Specification. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include "pipe.h"
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <errno.h>
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <fcntl.h>
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include <stdlib.h>
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include <signal.h>
6751
1b0092424a44 Include <unistd.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
31 #include <unistd.h>
4940
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 #include "error.h"
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #include "exit.h"
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 #include "fatal-signal.h"
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 #include "wait-process.h"
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #include "gettext.h"
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #define _(str) gettext (str)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #if defined _MSC_VER || defined __MINGW32__
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 /* Native Woe32 API. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 # include <process.h>
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 # include "w32spawn.h"
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 #else
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 /* Unix API. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 # ifdef HAVE_POSIX_SPAWN
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 # include <spawn.h>
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 # else
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 # ifdef HAVE_VFORK_H
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 # include <vfork.h>
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 # endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 # endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59
5580
19f7545b3c2b Declare environ; not all systems declare it.
Bruno Haible <bruno@clisp.org>
parents: 4940
diff changeset
60 #ifndef HAVE_ENVIRON_DECL
19f7545b3c2b Declare environ; not all systems declare it.
Bruno Haible <bruno@clisp.org>
parents: 4940
diff changeset
61 extern char **environ;
19f7545b3c2b Declare environ; not all systems declare it.
Bruno Haible <bruno@clisp.org>
parents: 4940
diff changeset
62 #endif
19f7545b3c2b Declare environ; not all systems declare it.
Bruno Haible <bruno@clisp.org>
parents: 4940
diff changeset
63
4940
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 #ifndef STDIN_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 # define STDIN_FILENO 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 #ifndef STDOUT_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 # define STDOUT_FILENO 1
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 #ifndef STDERR_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 # define STDERR_FILENO 2
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 #ifdef EINTR
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 /* EINTR handling for close().
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 These functions can return -1/EINTR even though we don't have any
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 signal handlers set up, namely when we get interrupted via SIGSTOP. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 static inline int
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 nonintr_close (int fd)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 int retval;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 do
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 retval = close (fd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 while (retval < 0 && errno == EINTR);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 return retval;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 #define close nonintr_close
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 static inline int
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 nonintr_open (const char *pathname, int oflag, mode_t mode)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 int retval;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 do
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 retval = open (pathname, oflag, mode);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 while (retval < 0 && errno == EINTR);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 return retval;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 #undef open /* avoid warning on VMS */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 #define open nonintr_open
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 /* Open a pipe connected to a child process.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 * parent -> fd[1] -> STDIN_FILENO -> child if pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 * parent <- fd[0] <- STDOUT_FILENO <- child if pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 * At least one of pipe_stdin, pipe_stdout must be true.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 * pipe_stdin and prog_stdin together determine the child's standard input.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 * pipe_stdout and prog_stdout together determine the child's standard output.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 * If pipe_stdin is true, prog_stdin is ignored.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 * If pipe_stdout is true, prog_stdout is ignored.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 static pid_t
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 create_pipe (const char *progname,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 const char *prog_path, char **prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 bool pipe_stdin, bool pipe_stdout,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 const char *prog_stdin, const char *prog_stdout,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 bool null_stderr,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 bool slave_process, bool exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 int fd[2])
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 #if defined _MSC_VER || defined __MINGW32__
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 /* Native Woe32 API.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 This uses _pipe(), dup2(), and spawnv(). It could also be implemented
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 using the low-level functions CreatePipe(), DuplicateHandle(),
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 CreateProcess() and _open_osfhandle(); see the GNU make and GNU clisp
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 and cvs source code. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 int ifd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 int ofd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 int orig_stdin;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 int orig_stdout;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 int orig_stderr;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 int child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 int nulloutfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 int stdinfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 int stdoutfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 prog_argv = prepare_spawn (prog_argv);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 if (_pipe (ifd, 4096, O_BINARY | O_NOINHERIT) < 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 error (EXIT_FAILURE, errno, _("cannot create pipe"));
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 if (_pipe (ofd, 4096, O_BINARY | O_NOINHERIT) < 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 error (EXIT_FAILURE, errno, _("cannot create pipe"));
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 /* Data flow diagram:
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 /* Save standard file handles of parent process. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 if (pipe_stdin || prog_stdin != NULL)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 orig_stdin = dup_noinherit (STDIN_FILENO);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 if (pipe_stdout || prog_stdout != NULL)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 orig_stdout = dup_noinherit (STDOUT_FILENO);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 if (null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 orig_stderr = dup_noinherit (STDERR_FILENO);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 child = -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 /* Create standard file handles of child process. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177 nulloutfd = -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 stdinfd = -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 stdoutfd = -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 && (!null_stderr
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 && (nulloutfd == STDERR_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 && close (nulloutfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 && (pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 || prog_stdin == NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 && (stdinfd == STDIN_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 || (dup2 (stdinfd, STDIN_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 && close (stdinfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 && (pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 || prog_stdout == NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196 && (stdoutfd == STDOUT_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 && close (stdoutfd) >= 0)))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 /* The child process doesn't inherit ifd[0], ifd[1], ofd[0], ofd[1],
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 but it inherits all open()ed or dup2()ed file handles (which is what
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 we want in the case of STD*_FILENO) and also orig_stdin,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 orig_stdout, orig_stderr (which is not explicitly wanted but
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 harmless). */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 child = spawnvp (P_NOWAIT, prog_path, prog_argv);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 if (stdinfd >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 close (stdinfd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 if (stdoutfd >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 close (stdoutfd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 if (nulloutfd >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 close (nulloutfd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 /* Restore standard file handles of parent process. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 if (null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 dup2 (orig_stderr, STDERR_FILENO), close (orig_stderr);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 if (pipe_stdout || prog_stdout != NULL)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 dup2 (orig_stdout, STDOUT_FILENO), close (orig_stdout);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 if (pipe_stdin || prog_stdin != NULL)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218 dup2 (orig_stdin, STDIN_FILENO), close (orig_stdin);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 close (ofd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223 close (ifd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224 if (child == -1)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226 if (exit_on_error || !null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 error (exit_on_error ? EXIT_FAILURE : 0, errno,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 _("%s subprocess failed"), progname);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 close (ifd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 close (ofd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 return -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237 fd[0] = ifd[0];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 fd[1] = ofd[1];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240 return child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242 #else
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 /* Unix API. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245 int ifd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246 int ofd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
247 # if HAVE_POSIX_SPAWN
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
248 sigset_t blocked_signals;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249 posix_spawn_file_actions_t actions;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
250 bool actions_allocated;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
251 posix_spawnattr_t attrs;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
252 bool attrs_allocated;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
253 int err;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
254 pid_t child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 # else
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
256 int child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
257 # endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
258
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
259 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
260 if (pipe (ifd) < 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
261 error (EXIT_FAILURE, errno, _("cannot create pipe"));
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
262 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
263 if (pipe (ofd) < 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
264 error (EXIT_FAILURE, errno, _("cannot create pipe"));
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
265 /* Data flow diagram:
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
266 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
267 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
268 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
269 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
270 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
271 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
272 */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
273
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
274 # if HAVE_POSIX_SPAWN
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
275 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
276 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
277 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
278 block_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
279 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
280 actions_allocated = false;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
281 attrs_allocated = false;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
282 if ((err = posix_spawn_file_actions_init (&actions)) != 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
283 || (actions_allocated = true,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
284 (pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
285 && (err = posix_spawn_file_actions_adddup2 (&actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
286 ofd[0], STDIN_FILENO))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
287 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
288 || (pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
289 && (err = posix_spawn_file_actions_adddup2 (&actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
290 ifd[1], STDOUT_FILENO))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
291 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
292 || (pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
293 && (err = posix_spawn_file_actions_addclose (&actions, ofd[0]))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
294 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
295 || (pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
296 && (err = posix_spawn_file_actions_addclose (&actions, ifd[1]))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
297 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
298 || (pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
299 && (err = posix_spawn_file_actions_addclose (&actions, ofd[1]))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
300 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
301 || (pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
302 && (err = posix_spawn_file_actions_addclose (&actions, ifd[0]))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
303 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
304 || (null_stderr
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
305 && (err = posix_spawn_file_actions_addopen (&actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
306 STDERR_FILENO,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
307 "/dev/null", O_RDWR,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
308 0))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
309 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
310 || (!pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
311 && prog_stdin != NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
312 && (err = posix_spawn_file_actions_addopen (&actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
313 STDIN_FILENO,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
314 prog_stdin, O_RDONLY,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
315 0))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
316 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
317 || (!pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
318 && prog_stdout != NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
319 && (err = posix_spawn_file_actions_addopen (&actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
320 STDOUT_FILENO,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
321 prog_stdout, O_WRONLY,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
322 0))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
323 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
324 || (slave_process
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
325 && ((err = posix_spawnattr_init (&attrs)) != 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
326 || (attrs_allocated = true,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
327 (err = posix_spawnattr_setsigmask (&attrs,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
328 &blocked_signals))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
329 != 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
330 || (err = posix_spawnattr_setflags (&attrs,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
331 POSIX_SPAWN_SETSIGMASK))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
332 != 0)))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
333 || (err = posix_spawnp (&child, prog_path, &actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
334 attrs_allocated ? &attrs : NULL, prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
335 environ))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
336 != 0))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
337 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
338 if (actions_allocated)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
339 posix_spawn_file_actions_destroy (&actions);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
340 if (attrs_allocated)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
341 posix_spawnattr_destroy (&attrs);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
342 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
343 unblock_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
344 if (exit_on_error || !null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
345 error (exit_on_error ? EXIT_FAILURE : 0, err,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
346 _("%s subprocess failed"), progname);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
347 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
348 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
349 close (ifd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
350 close (ifd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
351 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
352 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
353 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
354 close (ofd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
355 close (ofd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
356 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
357 return -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
358 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
359 posix_spawn_file_actions_destroy (&actions);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
360 if (attrs_allocated)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
361 posix_spawnattr_destroy (&attrs);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
362 # else
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
363 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
364 block_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
365 /* Use vfork() instead of fork() for efficiency. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
366 if ((child = vfork ()) == 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
367 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
368 /* Child process code. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
369 int nulloutfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
370 int stdinfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
371 int stdoutfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
372
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
373 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
374 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
375 && (!pipe_stdin || close (ofd[0]) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
376 && (!pipe_stdout || close (ifd[1]) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
377 && (!pipe_stdin || close (ofd[1]) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
378 && (!pipe_stdout || close (ifd[0]) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
379 && (!null_stderr
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
380 || ((nulloutfd = open ("/dev/null", O_RDWR, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
381 && (nulloutfd == STDERR_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
382 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
383 && close (nulloutfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
384 && (pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
385 || prog_stdin == NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
386 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
387 && (stdinfd == STDIN_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
388 || (dup2 (stdinfd, STDIN_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
389 && close (stdinfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
390 && (pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
391 || prog_stdout == NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
392 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
393 && (stdoutfd == STDOUT_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
394 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
395 && close (stdoutfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
396 && (!slave_process || (unblock_fatal_signals (), true)))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
397 execvp (prog_path, prog_argv);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
398 _exit (127);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
399 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
400 if (child == -1)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
401 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
402 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
403 unblock_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
404 if (exit_on_error || !null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
405 error (exit_on_error ? EXIT_FAILURE : 0, errno,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
406 _("%s subprocess failed"), progname);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
407 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
408 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
409 close (ifd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
410 close (ifd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
411 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
412 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
413 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
414 close (ofd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
415 close (ofd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
416 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
417 return -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
418 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
419 # endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
420 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
421 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
422 register_slave_subprocess (child);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
423 unblock_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
424 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
425 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
426 close (ofd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
427 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
428 close (ifd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
429
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
430 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
431 fd[0] = ifd[0];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
432 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
433 fd[1] = ofd[1];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
434 return child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
435
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
436 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
437 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
438
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
439 /* Open a bidirectional pipe.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
440 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
441 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
442 * parent -> fd[1] -> STDIN_FILENO -> child
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
443 * parent <- fd[0] <- STDOUT_FILENO <- child
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
444 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
445 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
446 */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
447 pid_t
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
448 create_pipe_bidi (const char *progname,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
449 const char *prog_path, char **prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
450 bool null_stderr,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
451 bool slave_process, bool exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
452 int fd[2])
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
453 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
454 pid_t result = create_pipe (progname, prog_path, prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
455 true, true, NULL, NULL,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
456 null_stderr, slave_process, exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
457 fd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
458 return result;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
459 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
460
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
461 /* Open a pipe for input from a child process.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
462 * The child's stdin comes from a file.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
463 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
464 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
465 * parent <- fd[0] <- STDOUT_FILENO <- child
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
466 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
467 */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
468 pid_t
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
469 create_pipe_in (const char *progname,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
470 const char *prog_path, char **prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
471 const char *prog_stdin, bool null_stderr,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
472 bool slave_process, bool exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
473 int fd[1])
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
474 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
475 int iofd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
476 pid_t result = create_pipe (progname, prog_path, prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
477 false, true, prog_stdin, NULL,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
478 null_stderr, slave_process, exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
479 iofd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
480 if (result != -1)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
481 fd[0] = iofd[0];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
482 return result;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
483 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
484
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
485 /* Open a pipe for output to a child process.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
486 * The child's stdout goes to a file.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
487 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
488 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
489 * parent -> fd[0] -> STDIN_FILENO -> child
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
490 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
491 */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
492 pid_t
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
493 create_pipe_out (const char *progname,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
494 const char *prog_path, char **prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
495 const char *prog_stdout, bool null_stderr,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
496 bool slave_process, bool exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
497 int fd[1])
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
498 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
499 int iofd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
500 pid_t result = create_pipe (progname, prog_path, prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
501 true, false, NULL, prog_stdout,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
502 null_stderr, slave_process, exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
503 iofd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
504 if (result != -1)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
505 fd[0] = iofd[1];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
506 return result;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
507 }