annotate lib/pipe.c @ 4945:07fb9f5d51e6

update from gettext 0.14.1
author Karl Berry <karl@freefriends.org>
date Fri, 06 Feb 2004 14:40:09 +0000
parents f3ef51adfb09
children 19f7545b3c2b
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.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2001-2004 Free Software Foundation, Inc.
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,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 # include "config.h"
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>
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #ifdef HAVE_UNISTD_H
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 # include <unistd.h>
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 #include "error.h"
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #include "exit.h"
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 #include "fatal-signal.h"
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #include "wait-process.h"
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 #include "gettext.h"
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 #define _(str) gettext (str)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 #if defined _MSC_VER || defined __MINGW32__
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 /* Native Woe32 API. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 # include <process.h>
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 # include "w32spawn.h"
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 #else
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 /* Unix API. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 # ifdef HAVE_POSIX_SPAWN
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 # include <spawn.h>
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 # else
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 # ifdef HAVE_VFORK_H
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 # include <vfork.h>
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 # endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 #ifndef STDIN_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 # define STDIN_FILENO 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 #ifndef STDOUT_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 # define STDOUT_FILENO 1
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 #ifndef STDERR_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 # define STDERR_FILENO 2
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72
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 #ifdef EINTR
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 /* EINTR handling for close().
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 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
78 signal handlers set up, namely when we get interrupted via SIGSTOP. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 static inline int
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 nonintr_close (int fd)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 int retval;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 do
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 retval = close (fd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 while (retval < 0 && errno == EINTR);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 return retval;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 #define close nonintr_close
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 static inline int
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 nonintr_open (const char *pathname, int oflag, mode_t mode)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 int retval;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 do
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 retval = open (pathname, oflag, mode);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 while (retval < 0 && errno == EINTR);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 return retval;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 #undef open /* avoid warning on VMS */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 #define open nonintr_open
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108
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 /* Open a pipe connected to a child process.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 * parent -> fd[1] -> STDIN_FILENO -> child if pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 * parent <- fd[0] <- STDOUT_FILENO <- child if pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 * At least one of pipe_stdin, pipe_stdout must be true.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 * pipe_stdin and prog_stdin together determine the child's standard input.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 * pipe_stdout and prog_stdout together determine the child's standard output.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 * If pipe_stdin is true, prog_stdin is ignored.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 * If pipe_stdout is true, prog_stdout is ignored.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 static pid_t
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 create_pipe (const char *progname,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 const char *prog_path, char **prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 bool pipe_stdin, bool pipe_stdout,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 const char *prog_stdin, const char *prog_stdout,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 bool null_stderr,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 bool slave_process, bool exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 int fd[2])
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 #if defined _MSC_VER || defined __MINGW32__
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 /* Native Woe32 API.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 This uses _pipe(), dup2(), and spawnv(). It could also be implemented
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 using the low-level functions CreatePipe(), DuplicateHandle(),
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 CreateProcess() and _open_osfhandle(); see the GNU make and GNU clisp
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 and cvs source code. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 int ifd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 int ofd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 int orig_stdin;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 int orig_stdout;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 int orig_stderr;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 int child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 int nulloutfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 int stdinfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 int stdoutfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 prog_argv = prepare_spawn (prog_argv);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 if (_pipe (ifd, 4096, O_BINARY | O_NOINHERIT) < 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 error (EXIT_FAILURE, errno, _("cannot create pipe"));
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 if (_pipe (ofd, 4096, O_BINARY | O_NOINHERIT) < 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 error (EXIT_FAILURE, errno, _("cannot create pipe"));
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 /* Data flow diagram:
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 *
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 /* Save standard file handles of parent process. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 if (pipe_stdin || prog_stdin != NULL)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 orig_stdin = dup_noinherit (STDIN_FILENO);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 if (pipe_stdout || prog_stdout != NULL)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 orig_stdout = dup_noinherit (STDOUT_FILENO);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 if (null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 orig_stderr = dup_noinherit (STDERR_FILENO);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 child = -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 /* Create standard file handles of child process. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 nulloutfd = -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177 stdinfd = -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 stdoutfd = -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 && (!null_stderr
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 && (nulloutfd == STDERR_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 && close (nulloutfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 && (pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 || prog_stdin == NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 && (stdinfd == STDIN_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 || (dup2 (stdinfd, STDIN_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 && close (stdinfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 && (pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 || prog_stdout == NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 && (stdoutfd == STDOUT_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 && close (stdoutfd) >= 0)))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 /* 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
199 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
200 we want in the case of STD*_FILENO) and also orig_stdin,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 orig_stdout, orig_stderr (which is not explicitly wanted but
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 harmless). */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 child = spawnvp (P_NOWAIT, prog_path, prog_argv);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 if (stdinfd >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 close (stdinfd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 if (stdoutfd >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 close (stdoutfd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 if (nulloutfd >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 close (nulloutfd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211 /* Restore standard file handles of parent process. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 if (null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 dup2 (orig_stderr, STDERR_FILENO), close (orig_stderr);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 if (pipe_stdout || prog_stdout != NULL)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 dup2 (orig_stdout, STDOUT_FILENO), close (orig_stdout);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 if (pipe_stdin || prog_stdin != NULL)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 dup2 (orig_stdin, STDIN_FILENO), close (orig_stdin);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 close (ofd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 close (ifd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223 if (child == -1)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225 if (exit_on_error || !null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226 error (exit_on_error ? EXIT_FAILURE : 0, errno,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 _("%s subprocess failed"), progname);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 close (ifd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 close (ofd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 return -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 }
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 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 fd[0] = ifd[0];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 fd[1] = ofd[1];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 return child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 #else
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243 /* Unix API. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 int ifd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245 int ofd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246 # if HAVE_POSIX_SPAWN
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
247 sigset_t blocked_signals;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
248 posix_spawn_file_actions_t actions;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249 bool actions_allocated;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
250 posix_spawnattr_t attrs;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
251 bool attrs_allocated;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
252 int err;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
253 pid_t child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
254 # else
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 int child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
256 # endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
257
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
258 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
259 if (pipe (ifd) < 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
260 error (EXIT_FAILURE, errno, _("cannot create pipe"));
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
261 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
262 if (pipe (ofd) < 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
263 error (EXIT_FAILURE, errno, _("cannot create pipe"));
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
264 /* Data flow diagram:
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
265 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
266 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
267 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
268 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
269 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
270 *
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 # if HAVE_POSIX_SPAWN
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
274 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
275 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
276 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
277 block_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
278 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
279 actions_allocated = false;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
280 attrs_allocated = false;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
281 if ((err = posix_spawn_file_actions_init (&actions)) != 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
282 || (actions_allocated = true,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
283 (pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
284 && (err = posix_spawn_file_actions_adddup2 (&actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
285 ofd[0], STDIN_FILENO))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
286 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
287 || (pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
288 && (err = posix_spawn_file_actions_adddup2 (&actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
289 ifd[1], STDOUT_FILENO))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
290 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
291 || (pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
292 && (err = posix_spawn_file_actions_addclose (&actions, ofd[0]))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
293 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
294 || (pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
295 && (err = posix_spawn_file_actions_addclose (&actions, ifd[1]))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
296 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
297 || (pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
298 && (err = posix_spawn_file_actions_addclose (&actions, ofd[1]))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
299 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
300 || (pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
301 && (err = posix_spawn_file_actions_addclose (&actions, ifd[0]))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
302 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
303 || (null_stderr
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
304 && (err = posix_spawn_file_actions_addopen (&actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
305 STDERR_FILENO,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
306 "/dev/null", O_RDWR,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
307 0))
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 || (!pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
310 && prog_stdin != NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
311 && (err = posix_spawn_file_actions_addopen (&actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
312 STDIN_FILENO,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
313 prog_stdin, O_RDONLY,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
314 0))
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 || (!pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
317 && prog_stdout != NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
318 && (err = posix_spawn_file_actions_addopen (&actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
319 STDOUT_FILENO,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
320 prog_stdout, O_WRONLY,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
321 0))
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 || (slave_process
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
324 && ((err = posix_spawnattr_init (&attrs)) != 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
325 || (attrs_allocated = true,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
326 (err = posix_spawnattr_setsigmask (&attrs,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
327 &blocked_signals))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
328 != 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
329 || (err = posix_spawnattr_setflags (&attrs,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
330 POSIX_SPAWN_SETSIGMASK))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
331 != 0)))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
332 || (err = posix_spawnp (&child, prog_path, &actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
333 attrs_allocated ? &attrs : NULL, prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
334 environ))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
335 != 0))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
336 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
337 if (actions_allocated)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
338 posix_spawn_file_actions_destroy (&actions);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
339 if (attrs_allocated)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
340 posix_spawnattr_destroy (&attrs);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
341 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
342 unblock_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
343 if (exit_on_error || !null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
344 error (exit_on_error ? EXIT_FAILURE : 0, err,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
345 _("%s subprocess failed"), progname);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
346 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
347 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
348 close (ifd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
349 close (ifd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
350 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
351 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
352 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
353 close (ofd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
354 close (ofd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
355 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
356 return -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
357 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
358 posix_spawn_file_actions_destroy (&actions);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
359 if (attrs_allocated)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
360 posix_spawnattr_destroy (&attrs);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
361 # else
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
362 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
363 block_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
364 /* Use vfork() instead of fork() for efficiency. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
365 if ((child = vfork ()) == 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
366 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
367 /* Child process code. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
368 int nulloutfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
369 int stdinfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
370 int stdoutfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
371
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
372 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
373 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
374 && (!pipe_stdin || close (ofd[0]) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
375 && (!pipe_stdout || close (ifd[1]) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
376 && (!pipe_stdin || close (ofd[1]) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
377 && (!pipe_stdout || close (ifd[0]) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
378 && (!null_stderr
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
379 || ((nulloutfd = open ("/dev/null", O_RDWR, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
380 && (nulloutfd == STDERR_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
381 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
382 && close (nulloutfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
383 && (pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
384 || prog_stdin == NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
385 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
386 && (stdinfd == STDIN_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
387 || (dup2 (stdinfd, STDIN_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
388 && close (stdinfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
389 && (pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
390 || prog_stdout == NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
391 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
392 && (stdoutfd == STDOUT_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
393 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
394 && close (stdoutfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
395 && (!slave_process || (unblock_fatal_signals (), true)))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
396 execvp (prog_path, prog_argv);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
397 _exit (127);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
398 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
399 if (child == -1)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
400 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
401 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
402 unblock_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
403 if (exit_on_error || !null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
404 error (exit_on_error ? EXIT_FAILURE : 0, errno,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
405 _("%s subprocess failed"), progname);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
406 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
407 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
408 close (ifd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
409 close (ifd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
410 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
411 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
412 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
413 close (ofd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
414 close (ofd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
415 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
416 return -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
417 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
418 # endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
419 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
420 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
421 register_slave_subprocess (child);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
422 unblock_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
423 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
424 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
425 close (ofd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
426 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
427 close (ifd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
428
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
429 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
430 fd[0] = ifd[0];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
431 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
432 fd[1] = ofd[1];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
433 return child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
434
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
435 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
436 }
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 /* Open a bidirectional pipe.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
439 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
440 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
441 * parent -> fd[1] -> STDIN_FILENO -> child
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
442 * parent <- fd[0] <- STDOUT_FILENO <- child
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
443 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
444 *
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 pid_t
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
447 create_pipe_bidi (const char *progname,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
448 const char *prog_path, char **prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
449 bool null_stderr,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
450 bool slave_process, bool exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
451 int fd[2])
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
452 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
453 pid_t result = create_pipe (progname, prog_path, prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
454 true, true, NULL, NULL,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
455 null_stderr, slave_process, exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
456 fd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
457 return result;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
458 }
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 /* Open a pipe for input from a child process.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
461 * The child's stdin comes from a file.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
462 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
463 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
464 * parent <- fd[0] <- STDOUT_FILENO <- child
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
465 *
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 pid_t
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
468 create_pipe_in (const char *progname,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
469 const char *prog_path, char **prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
470 const char *prog_stdin, bool null_stderr,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
471 bool slave_process, bool exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
472 int fd[1])
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
473 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
474 int iofd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
475 pid_t result = create_pipe (progname, prog_path, prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
476 false, true, prog_stdin, NULL,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
477 null_stderr, slave_process, exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
478 iofd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
479 if (result != -1)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
480 fd[0] = iofd[0];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
481 return result;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
482 }
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 /* Open a pipe for output to a child process.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
485 * The child's stdout goes to a file.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
486 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
487 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
488 * parent -> fd[0] -> STDIN_FILENO -> child
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
489 *
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 pid_t
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
492 create_pipe_out (const char *progname,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
493 const char *prog_path, char **prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
494 const char *prog_stdout, bool null_stderr,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
495 bool slave_process, bool exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
496 int fd[1])
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
497 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
498 int iofd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
499 pid_t result = create_pipe (progname, prog_path, prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
500 true, false, NULL, prog_stdout,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
501 null_stderr, slave_process, exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
502 iofd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
503 if (result != -1)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
504 fd[0] = iofd[1];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
505 return result;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
506 }