annotate lib/spawn-pipe.c @ 40057:b06060465f09

maint: Run 'make update-copyright'
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 01 Jan 2019 00:25:11 +0100
parents 1c8c6cf5d064
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13924
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Creation of subprocesses, communicating via pipes.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19646
diff changeset
2 Copyright (C) 2001-2004, 2006-2019 Free Software Foundation, Inc.
13924
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
13924
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
19471
629f2f5a4e79 spawn-pipe: Silence a clang warning.
Bruno Haible <bruno@clisp.org>
parents: 19190
diff changeset
19 /* Tell clang not to warn about the 'child' variable, below. */
629f2f5a4e79 spawn-pipe: Silence a clang warning.
Bruno Haible <bruno@clisp.org>
parents: 19190
diff changeset
20 #if defined __clang__
629f2f5a4e79 spawn-pipe: Silence a clang warning.
Bruno Haible <bruno@clisp.org>
parents: 19190
diff changeset
21 # pragma clang diagnostic ignored "-Wconditional-uninitialized"
629f2f5a4e79 spawn-pipe: Silence a clang warning.
Bruno Haible <bruno@clisp.org>
parents: 19190
diff changeset
22 #endif
629f2f5a4e79 spawn-pipe: Silence a clang warning.
Bruno Haible <bruno@clisp.org>
parents: 19190
diff changeset
23
13924
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <config.h>
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 /* Specification. */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include "spawn-pipe.h"
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include <errno.h>
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include <fcntl.h>
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #include <stdlib.h>
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #include <signal.h>
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 #include <unistd.h>
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 #include "error.h"
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 #include "fatal-signal.h"
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #include "unistd-safer.h"
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 #include "wait-process.h"
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #include "gettext.h"
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #define _(str) gettext (str)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
43 #if (defined _WIN32 && ! defined __CYGWIN__) || defined __KLIBC__
13924
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44
16242
59c686e5b2df Talk about "native Windows API", not "Woe32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
45 /* Native Windows API. */
13924
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 # include <process.h>
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 # include "w32spawn.h"
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 #else
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 /* Unix API. */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 # include <spawn.h>
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 #endif
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 #ifdef EINTR
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 /* EINTR handling for close().
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 These functions can return -1/EINTR even though we don't have any
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 signal handlers set up, namely when we get interrupted via SIGSTOP. */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62
17185
dd46d4e6beea dup, execute, fatal-signal, etc.: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16242
diff changeset
63 static int
13924
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 nonintr_close (int fd)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 {
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 int retval;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 do
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 retval = close (fd);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 while (retval < 0 && errno == EINTR);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 return retval;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 }
19646
1c8c6cf5d064 execute, spawn-pipe: Avoid warning about redefining 'close'.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
74 #undef close /* avoid warning related to gnulib module unistd */
13924
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 #define close nonintr_close
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
77 #if defined _WIN32 && ! defined __CYGWIN__
17185
dd46d4e6beea dup, execute, fatal-signal, etc.: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16242
diff changeset
78 static int
13924
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 nonintr_open (const char *pathname, int oflag, mode_t mode)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 {
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 int retval;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 do
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 retval = open (pathname, oflag, mode);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 while (retval < 0 && errno == EINTR);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 return retval;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 }
17185
dd46d4e6beea dup, execute, fatal-signal, etc.: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16242
diff changeset
89 # undef open /* avoid warning on VMS */
dd46d4e6beea dup, execute, fatal-signal, etc.: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16242
diff changeset
90 # define open nonintr_open
dd46d4e6beea dup, execute, fatal-signal, etc.: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16242
diff changeset
91 #endif
13924
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 #endif
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 /* Open a pipe connected to a child process.
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 *
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 * write system read
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 * parent -> fd[1] -> STDIN_FILENO -> child if pipe_stdin
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 * parent <- fd[0] <- STDOUT_FILENO <- child if pipe_stdout
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 * read system write
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 *
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 * At least one of pipe_stdin, pipe_stdout must be true.
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 * pipe_stdin and prog_stdin together determine the child's standard input.
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 * pipe_stdout and prog_stdout together determine the child's standard output.
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 * If pipe_stdin is true, prog_stdin is ignored.
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 * If pipe_stdout is true, prog_stdout is ignored.
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 static pid_t
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 create_pipe (const char *progname,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 const char *prog_path, char **prog_argv,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 bool pipe_stdin, bool pipe_stdout,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 const char *prog_stdin, const char *prog_stdout,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 bool null_stderr,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 bool slave_process, bool exit_on_error,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 int fd[2])
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 {
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
118 #if (defined _WIN32 && ! defined __CYGWIN__) || defined __KLIBC__
13924
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119
16242
59c686e5b2df Talk about "native Windows API", not "Woe32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
120 /* Native Windows API.
13924
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 This uses _pipe(), dup2(), and spawnv(). It could also be implemented
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 using the low-level functions CreatePipe(), DuplicateHandle(),
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 CreateProcess() and _open_osfhandle(); see the GNU make and GNU clisp
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 and cvs source code. */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 int ifd[2];
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 int ofd[2];
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 int orig_stdin;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 int orig_stdout;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 int orig_stderr;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 int child;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 int nulloutfd;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 int stdinfd;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 int stdoutfd;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 int saved_errno;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 /* FIXME: Need to free memory allocated by prepare_spawn. */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 prog_argv = prepare_spawn (prog_argv);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 if (pipe_stdout)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 if (pipe2_safer (ifd, O_BINARY | O_CLOEXEC) < 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 error (EXIT_FAILURE, errno, _("cannot create pipe"));
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 if (pipe_stdin)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 if (pipe2_safer (ofd, O_BINARY | O_CLOEXEC) < 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 error (EXIT_FAILURE, errno, _("cannot create pipe"));
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 /* Data flow diagram:
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 *
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 * write system read
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 * read system write
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 *
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 /* Save standard file handles of parent process. */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 if (pipe_stdin || prog_stdin != NULL)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 orig_stdin = dup_safer_noinherit (STDIN_FILENO);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 if (pipe_stdout || prog_stdout != NULL)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 orig_stdout = dup_safer_noinherit (STDOUT_FILENO);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 if (null_stderr)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 orig_stderr = dup_safer_noinherit (STDERR_FILENO);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 child = -1;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 /* Create standard file handles of child process. */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 nulloutfd = -1;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 stdinfd = -1;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 stdoutfd = -1;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 && (!null_stderr
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 && (nulloutfd == STDERR_FILENO
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 && close (nulloutfd) >= 0))))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 && (pipe_stdin
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 || prog_stdin == NULL
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177 && (stdinfd == STDIN_FILENO
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 || (dup2 (stdinfd, STDIN_FILENO) >= 0
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 && close (stdinfd) >= 0))))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 && (pipe_stdout
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 || prog_stdout == NULL
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 && (stdoutfd == STDOUT_FILENO
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 && close (stdoutfd) >= 0)))))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 /* The child process doesn't inherit ifd[0], ifd[1], ofd[0], ofd[1],
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 but it inherits all open()ed or dup2()ed file handles (which is what
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 we want in the case of STD*_FILENO). */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 /* Use spawnvpe and pass the environment explicitly. This is needed if
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 the program has modified the environment using putenv() or [un]setenv().
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 On Windows, programs have two environments, one in the "environment
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 block" of the process and managed through SetEnvironmentVariable(), and
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 one inside the process, in the location retrieved by the 'environ'
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 macro. When using spawnvp() without 'e', the child process inherits a
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 copy of the environment block - ignoring the effects of putenv() and
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196 [un]setenv(). */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 {
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 child = spawnvpe (P_NOWAIT, prog_path, (const char **) prog_argv,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 (const char **) environ);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 if (child < 0 && errno == ENOEXEC)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 {
14774
70d101744577 maint: correct misuse of "a" and "an"
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
202 /* prog is not a native executable. Try to execute it as a
13924
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 shell script. Note that prepare_spawn() has already prepended
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 a hidden element "sh.exe" to prog_argv. */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 --prog_argv;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 child = spawnvpe (P_NOWAIT, prog_argv[0], (const char **) prog_argv,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 (const char **) environ);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 }
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 }
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 if (child == -1)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211 saved_errno = errno;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 if (stdinfd >= 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 close (stdinfd);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 if (stdoutfd >= 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 close (stdoutfd);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 if (nulloutfd >= 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 close (nulloutfd);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 /* Restore standard file handles of parent process. */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 if (null_stderr)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 undup_safer_noinherit (orig_stderr, STDERR_FILENO);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 if (pipe_stdout || prog_stdout != NULL)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223 undup_safer_noinherit (orig_stdout, STDOUT_FILENO);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224 if (pipe_stdin || prog_stdin != NULL)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225 undup_safer_noinherit (orig_stdin, STDIN_FILENO);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 if (pipe_stdin)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 close (ofd[0]);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 if (pipe_stdout)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 close (ifd[1]);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 if (child == -1)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 {
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 if (exit_on_error || !null_stderr)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 error (exit_on_error ? EXIT_FAILURE : 0, saved_errno,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235 _("%s subprocess failed"), progname);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 if (pipe_stdout)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237 close (ifd[0]);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 if (pipe_stdin)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 close (ofd[1]);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240 errno = saved_errno;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 return -1;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242 }
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 if (pipe_stdout)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245 fd[0] = ifd[0];
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246 if (pipe_stdin)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
247 fd[1] = ofd[1];
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
248 return child;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
250 #else
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
251
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
252 /* Unix API. */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
253 int ifd[2];
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
254 int ofd[2];
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 sigset_t blocked_signals;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
256 posix_spawn_file_actions_t actions;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
257 bool actions_allocated;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
258 posix_spawnattr_t attrs;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
259 bool attrs_allocated;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
260 int err;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
261 pid_t child;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
262
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
263 if (pipe_stdout)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
264 if (pipe_safer (ifd) < 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
265 error (EXIT_FAILURE, errno, _("cannot create pipe"));
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
266 if (pipe_stdin)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
267 if (pipe_safer (ofd) < 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
268 error (EXIT_FAILURE, errno, _("cannot create pipe"));
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
269 /* Data flow diagram:
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
270 *
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
271 * write system read
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
272 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
273 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
274 * read system write
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
275 *
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
276 */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
277
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
278 if (slave_process)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
279 {
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
280 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
281 block_fatal_signals ();
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
282 }
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
283 actions_allocated = false;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
284 attrs_allocated = false;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
285 if ((err = posix_spawn_file_actions_init (&actions)) != 0
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
286 || (actions_allocated = true,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
287 (pipe_stdin
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
288 && (err = posix_spawn_file_actions_adddup2 (&actions,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
289 ofd[0], STDIN_FILENO))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
290 != 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
291 || (pipe_stdout
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
292 && (err = posix_spawn_file_actions_adddup2 (&actions,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
293 ifd[1], STDOUT_FILENO))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
294 != 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
295 || (pipe_stdin
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
296 && (err = posix_spawn_file_actions_addclose (&actions, ofd[0]))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
297 != 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
298 || (pipe_stdout
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
299 && (err = posix_spawn_file_actions_addclose (&actions, ifd[1]))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
300 != 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
301 || (pipe_stdin
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
302 && (err = posix_spawn_file_actions_addclose (&actions, ofd[1]))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
303 != 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
304 || (pipe_stdout
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
305 && (err = posix_spawn_file_actions_addclose (&actions, ifd[0]))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
306 != 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
307 || (null_stderr
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
308 && (err = posix_spawn_file_actions_addopen (&actions,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
309 STDERR_FILENO,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
310 "/dev/null", O_RDWR,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
311 0))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
312 != 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
313 || (!pipe_stdin
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
314 && prog_stdin != NULL
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
315 && (err = posix_spawn_file_actions_addopen (&actions,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
316 STDIN_FILENO,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
317 prog_stdin, O_RDONLY,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
318 0))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
319 != 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
320 || (!pipe_stdout
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
321 && prog_stdout != NULL
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
322 && (err = posix_spawn_file_actions_addopen (&actions,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
323 STDOUT_FILENO,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
324 prog_stdout, O_WRONLY,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
325 0))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
326 != 0)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
327 || (slave_process
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
328 && ((err = posix_spawnattr_init (&attrs)) != 0
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
329 || (attrs_allocated = true,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
330 (err = posix_spawnattr_setsigmask (&attrs,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
331 &blocked_signals))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
332 != 0
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
333 || (err = posix_spawnattr_setflags (&attrs,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
334 POSIX_SPAWN_SETSIGMASK))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
335 != 0)))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
336 || (err = posix_spawnp (&child, prog_path, &actions,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
337 attrs_allocated ? &attrs : NULL, prog_argv,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
338 environ))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
339 != 0))
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
340 {
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
341 if (actions_allocated)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
342 posix_spawn_file_actions_destroy (&actions);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
343 if (attrs_allocated)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
344 posix_spawnattr_destroy (&attrs);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
345 if (slave_process)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
346 unblock_fatal_signals ();
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
347 if (exit_on_error || !null_stderr)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
348 error (exit_on_error ? EXIT_FAILURE : 0, err,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
349 _("%s subprocess failed"), progname);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
350 if (pipe_stdout)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
351 {
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
352 close (ifd[0]);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
353 close (ifd[1]);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
354 }
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
355 if (pipe_stdin)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
356 {
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
357 close (ofd[0]);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
358 close (ofd[1]);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
359 }
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
360 errno = err;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
361 return -1;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
362 }
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
363 posix_spawn_file_actions_destroy (&actions);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
364 if (attrs_allocated)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
365 posix_spawnattr_destroy (&attrs);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
366 if (slave_process)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
367 {
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
368 register_slave_subprocess (child);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
369 unblock_fatal_signals ();
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
370 }
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
371 if (pipe_stdin)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
372 close (ofd[0]);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
373 if (pipe_stdout)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
374 close (ifd[1]);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
375
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
376 if (pipe_stdout)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
377 fd[0] = ifd[0];
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
378 if (pipe_stdin)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
379 fd[1] = ofd[1];
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
380 return child;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
381
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
382 #endif
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
383 }
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
384
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
385 /* Open a bidirectional pipe.
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
386 *
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
387 * write system read
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
388 * parent -> fd[1] -> STDIN_FILENO -> child
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
389 * parent <- fd[0] <- STDOUT_FILENO <- child
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
390 * read system write
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
391 *
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
392 */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
393 pid_t
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
394 create_pipe_bidi (const char *progname,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
395 const char *prog_path, char **prog_argv,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
396 bool null_stderr,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
397 bool slave_process, bool exit_on_error,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
398 int fd[2])
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
399 {
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
400 pid_t result = create_pipe (progname, prog_path, prog_argv,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
401 true, true, NULL, NULL,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
402 null_stderr, slave_process, exit_on_error,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
403 fd);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
404 return result;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
405 }
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
406
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
407 /* Open a pipe for input from a child process.
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
408 * The child's stdin comes from a file.
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
409 *
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
410 * read system write
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
411 * parent <- fd[0] <- STDOUT_FILENO <- child
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
412 *
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
413 */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
414 pid_t
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
415 create_pipe_in (const char *progname,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
416 const char *prog_path, char **prog_argv,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
417 const char *prog_stdin, bool null_stderr,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
418 bool slave_process, bool exit_on_error,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
419 int fd[1])
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
420 {
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
421 int iofd[2];
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
422 pid_t result = create_pipe (progname, prog_path, prog_argv,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
423 false, true, prog_stdin, NULL,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
424 null_stderr, slave_process, exit_on_error,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
425 iofd);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
426 if (result != -1)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
427 fd[0] = iofd[0];
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
428 return result;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
429 }
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
430
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
431 /* Open a pipe for output to a child process.
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
432 * The child's stdout goes to a file.
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
433 *
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
434 * write system read
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
435 * parent -> fd[0] -> STDIN_FILENO -> child
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
436 *
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
437 */
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
438 pid_t
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
439 create_pipe_out (const char *progname,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
440 const char *prog_path, char **prog_argv,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
441 const char *prog_stdout, bool null_stderr,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
442 bool slave_process, bool exit_on_error,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
443 int fd[1])
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
444 {
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
445 int iofd[2];
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
446 pid_t result = create_pipe (progname, prog_path, prog_argv,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
447 true, false, NULL, prog_stdout,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
448 null_stderr, slave_process, exit_on_error,
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
449 iofd);
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
450 if (result != -1)
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
451 fd[0] = iofd[1];
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
452 return result;
5be0c314f2f8 Rename module 'pipe' to 'spawn-pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
453 }