annotate lib/pipe.c @ 6259:96c32553b4c6

Use a consistent style for including <config.h>. * __fpending.c, acl.c, argmatch.c, argp-help.c, argp-parse.c, argp-pvh.c, backupfile.c, basename.c, c-stack.c, calloc.c, check-version.c, cloexec.c, closeout.c, copy-file.c, creat-safer.c, cycle-check.c, dirfd.c, dirname.c, dup-safer.c, dup2.c, euidaccess.c, exclude.c, exitfail.c, fatal-signal.c, fd-safer.c, file-type.c, fileblocks.c, filemode.c, filenamecat.c, findprog.c, fnmatch.c, fopen-safer.c, free.c, fsusage.c, ftruncate.c, full-write.c, fwriteerror.c, getaddrinfo.c, getcwd.c, getdelim.c, getline.c, getlogin_r.c, getndelim2.c, getnline.c, getopt1.c, getpass.c, group-member.c, hard-locale.c, hash-pjw.c, hash.c, human.c, idcache.c, inet_ntop.c, isdir.c, long-options.c, malloc.c, memcasecmp.c, memcmp.c, memcoll.c, memcpy.c, memmove.c, mkdir-p.c, modechange.c, mountlist.c, open-safer.c, physmem.c, pipe-safer.c, pipe.c, poll.c, posixver.c, progname.c, progreloc.c, putenv.c, quote.c, quotearg.c, readline.c, readlink.c, realloc.c, regex.c, rename.c, rmdir.c, rpmatch.c, safe-read.c, same.c, save-cwd.c, savedir.c, sig2str.c, strcspn.c, strerror.c, stripslash.c, strncasecmp.c, strndup.c, strnlen.c, strnlen1.c, strsep.c, strstr.c, strtod.c, strtoimax.c, strtol.c, strverscmp.c, tempname.c, time_r.c, userspec.c, utimecmp.c, version-etc-fsf.c, version-etc.c, wait-process.c, xalloc-die.c, xgetcwd.c, xmalloc.c, xmemcoll.c, xnanosleep.c, xreadlink.c, xsetenv.c, xstrndup.c, xstrtoimax.c, xstrtol.c, xstrtoumax.c, yesno.c: Standardize inclusion of config.h. * __fpending.h, dirfd.h, getdate.h, human.h, inttostr.h: Removed inclusion of config.h from header files. * inttostr.c: Adjusted in-tree users. * timespec.h: Remove superfluous warning to include config.h. * atexit.c, chdir-long.c chown.c, fchown-stub.c, getgroups.c, gettimeofday.c, lchown.c, lstat.c, mkdir.c, mkstemp.c, nanosleep.c, openat.c, raise.c, readtokens0.c, readutmp.c, unlinkdir.c: Guard inclusion of config.h with HAVE_CONFIG_H.
author Paul Eggert <eggert@cs.ucla.edu>
date Mon, 19 Sep 2005 17:28:14 +0000
parents a48fb0e98c8c
children 1b0092424a44
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,
5848
a48fb0e98c8c *** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents: 5580
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
4940
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #ifdef HAVE_CONFIG_H
6259
96c32553b4c6 Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
21 # include <config.h>
4940
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 /* Specification. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include "pipe.h"
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <errno.h>
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <fcntl.h>
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include <stdlib.h>
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include <signal.h>
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
5580
19f7545b3c2b Declare environ; not all systems declare it.
Bruno Haible <bruno@clisp.org>
parents: 4940
diff changeset
63 #ifndef HAVE_ENVIRON_DECL
19f7545b3c2b Declare environ; not all systems declare it.
Bruno Haible <bruno@clisp.org>
parents: 4940
diff changeset
64 extern char **environ;
19f7545b3c2b Declare environ; not all systems declare it.
Bruno Haible <bruno@clisp.org>
parents: 4940
diff changeset
65 #endif
19f7545b3c2b Declare environ; not all systems declare it.
Bruno Haible <bruno@clisp.org>
parents: 4940
diff changeset
66
4940
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 #ifndef STDIN_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 # define STDIN_FILENO 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 #ifndef STDOUT_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 # define STDOUT_FILENO 1
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 #ifndef STDERR_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 # define STDERR_FILENO 2
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 #ifdef EINTR
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 /* EINTR handling for close().
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 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
82 signal handlers set up, namely when we get interrupted via SIGSTOP. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 static inline int
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 nonintr_close (int fd)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 int retval;
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 do
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 retval = close (fd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 while (retval < 0 && errno == EINTR);
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 return retval;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 #define close nonintr_close
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 static inline int
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 nonintr_open (const char *pathname, int oflag, mode_t mode)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 int retval;
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 do
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 retval = open (pathname, oflag, mode);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 while (retval < 0 && errno == EINTR);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 return retval;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 #undef open /* avoid warning on VMS */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 #define open nonintr_open
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 /* Open a pipe connected to a child process.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 * parent -> fd[1] -> STDIN_FILENO -> child if pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 * parent <- fd[0] <- STDOUT_FILENO <- child if pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 * At least one of pipe_stdin, pipe_stdout must be true.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 * pipe_stdin and prog_stdin together determine the child's standard input.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 * pipe_stdout and prog_stdout together determine the child's standard output.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 * If pipe_stdin is true, prog_stdin is ignored.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 * If pipe_stdout is true, prog_stdout is ignored.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 static pid_t
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 create_pipe (const char *progname,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 const char *prog_path, char **prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 bool pipe_stdin, bool pipe_stdout,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 const char *prog_stdin, const char *prog_stdout,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 bool null_stderr,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 bool slave_process, bool exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 int fd[2])
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 #if defined _MSC_VER || defined __MINGW32__
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 /* Native Woe32 API.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 This uses _pipe(), dup2(), and spawnv(). It could also be implemented
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 using the low-level functions CreatePipe(), DuplicateHandle(),
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 CreateProcess() and _open_osfhandle(); see the GNU make and GNU clisp
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 and cvs source code. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 int ifd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 int ofd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 int orig_stdin;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 int orig_stdout;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 int orig_stderr;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 int child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 int nulloutfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 int stdinfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 int stdoutfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 prog_argv = prepare_spawn (prog_argv);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 if (_pipe (ifd, 4096, O_BINARY | O_NOINHERIT) < 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 error (EXIT_FAILURE, errno, _("cannot create pipe"));
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 if (_pipe (ofd, 4096, O_BINARY | O_NOINHERIT) < 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 error (EXIT_FAILURE, errno, _("cannot create pipe"));
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 /* Data flow diagram:
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 /* Save standard file handles of parent process. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 if (pipe_stdin || prog_stdin != NULL)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 orig_stdin = dup_noinherit (STDIN_FILENO);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 if (pipe_stdout || prog_stdout != NULL)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 orig_stdout = dup_noinherit (STDOUT_FILENO);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 if (null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 orig_stderr = dup_noinherit (STDERR_FILENO);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177 child = -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 /* Create standard file handles of child process. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 nulloutfd = -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 stdinfd = -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 stdoutfd = -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 && (!null_stderr
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 && (nulloutfd == STDERR_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 && close (nulloutfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 && (pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 || prog_stdin == NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 && (stdinfd == STDIN_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 || (dup2 (stdinfd, STDIN_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 && close (stdinfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196 && (pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 || prog_stdout == NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 && (stdoutfd == STDOUT_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 && close (stdoutfd) >= 0)))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 /* 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
203 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
204 we want in the case of STD*_FILENO) and also orig_stdin,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 orig_stdout, orig_stderr (which is not explicitly wanted but
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 harmless). */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 child = spawnvp (P_NOWAIT, prog_path, prog_argv);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 if (stdinfd >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 close (stdinfd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 if (stdoutfd >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211 close (stdoutfd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 if (nulloutfd >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 close (nulloutfd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 /* Restore standard file handles of parent process. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 if (null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 dup2 (orig_stderr, STDERR_FILENO), close (orig_stderr);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218 if (pipe_stdout || prog_stdout != NULL)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 dup2 (orig_stdout, STDOUT_FILENO), close (orig_stdout);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 if (pipe_stdin || prog_stdin != NULL)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 dup2 (orig_stdin, STDIN_FILENO), close (orig_stdin);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224 close (ofd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226 close (ifd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 if (child == -1)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 if (exit_on_error || !null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 error (exit_on_error ? EXIT_FAILURE : 0, errno,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 _("%s subprocess failed"), progname);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 close (ifd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235 close (ofd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 return -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240 fd[0] = ifd[0];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242 fd[1] = ofd[1];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243 return child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245 #else
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
247 /* Unix API. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
248 int ifd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249 int ofd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
250 # if HAVE_POSIX_SPAWN
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
251 sigset_t blocked_signals;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
252 posix_spawn_file_actions_t actions;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
253 bool actions_allocated;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
254 posix_spawnattr_t attrs;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 bool attrs_allocated;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
256 int err;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
257 pid_t child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
258 # else
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
259 int child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
260 # endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
261
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
262 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
263 if (pipe (ifd) < 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
264 error (EXIT_FAILURE, errno, _("cannot create pipe"));
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
265 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
266 if (pipe (ofd) < 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
267 error (EXIT_FAILURE, errno, _("cannot create pipe"));
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
268 /* Data flow diagram:
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
269 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
270 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
271 * parent -> ofd[1] -> ofd[0] -> child if pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
272 * parent <- ifd[0] <- ifd[1] <- child if pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
273 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
274 *
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
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
277 # if HAVE_POSIX_SPAWN
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
278 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
279 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
280 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
281 block_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
282 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
283 actions_allocated = false;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
284 attrs_allocated = false;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
285 if ((err = posix_spawn_file_actions_init (&actions)) != 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
286 || (actions_allocated = true,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
287 (pipe_stdin
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 ofd[0], STDIN_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_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
292 && (err = posix_spawn_file_actions_adddup2 (&actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
293 ifd[1], STDOUT_FILENO))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
294 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
295 || (pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
296 && (err = posix_spawn_file_actions_addclose (&actions, ofd[0]))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
297 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
298 || (pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
299 && (err = posix_spawn_file_actions_addclose (&actions, ifd[1]))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
300 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
301 || (pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
302 && (err = posix_spawn_file_actions_addclose (&actions, ofd[1]))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
303 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
304 || (pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
305 && (err = posix_spawn_file_actions_addclose (&actions, ifd[0]))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
306 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
307 || (null_stderr
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
308 && (err = posix_spawn_file_actions_addopen (&actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
309 STDERR_FILENO,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
310 "/dev/null", O_RDWR,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
311 0))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
312 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
313 || (!pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
314 && prog_stdin != NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
315 && (err = posix_spawn_file_actions_addopen (&actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
316 STDIN_FILENO,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
317 prog_stdin, O_RDONLY,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
318 0))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
319 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
320 || (!pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
321 && prog_stdout != NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
322 && (err = posix_spawn_file_actions_addopen (&actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
323 STDOUT_FILENO,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
324 prog_stdout, O_WRONLY,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
325 0))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
326 != 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
327 || (slave_process
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
328 && ((err = posix_spawnattr_init (&attrs)) != 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
329 || (attrs_allocated = true,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
330 (err = posix_spawnattr_setsigmask (&attrs,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
331 &blocked_signals))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
332 != 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
333 || (err = posix_spawnattr_setflags (&attrs,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
334 POSIX_SPAWN_SETSIGMASK))
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 || (err = posix_spawnp (&child, prog_path, &actions,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
337 attrs_allocated ? &attrs : NULL, prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
338 environ))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
339 != 0))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
340 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
341 if (actions_allocated)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
342 posix_spawn_file_actions_destroy (&actions);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
343 if (attrs_allocated)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
344 posix_spawnattr_destroy (&attrs);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
345 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
346 unblock_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
347 if (exit_on_error || !null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
348 error (exit_on_error ? EXIT_FAILURE : 0, err,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
349 _("%s subprocess failed"), progname);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
350 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
351 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
352 close (ifd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
353 close (ifd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
354 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
355 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
356 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
357 close (ofd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
358 close (ofd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
359 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
360 return -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
361 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
362 posix_spawn_file_actions_destroy (&actions);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
363 if (attrs_allocated)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
364 posix_spawnattr_destroy (&attrs);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
365 # else
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
366 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
367 block_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
368 /* Use vfork() instead of fork() for efficiency. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
369 if ((child = vfork ()) == 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
370 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
371 /* Child process code. */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
372 int nulloutfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
373 int stdinfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
374 int stdoutfd;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
375
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
376 if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
377 && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
378 && (!pipe_stdin || close (ofd[0]) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
379 && (!pipe_stdout || close (ifd[1]) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
380 && (!pipe_stdin || close (ofd[1]) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
381 && (!pipe_stdout || close (ifd[0]) >= 0)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
382 && (!null_stderr
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
383 || ((nulloutfd = open ("/dev/null", O_RDWR, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
384 && (nulloutfd == STDERR_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
385 || (dup2 (nulloutfd, STDERR_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
386 && close (nulloutfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
387 && (pipe_stdin
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
388 || prog_stdin == NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
389 || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
390 && (stdinfd == STDIN_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
391 || (dup2 (stdinfd, STDIN_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
392 && close (stdinfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
393 && (pipe_stdout
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
394 || prog_stdout == NULL
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
395 || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
396 && (stdoutfd == STDOUT_FILENO
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
397 || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
398 && close (stdoutfd) >= 0))))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
399 && (!slave_process || (unblock_fatal_signals (), true)))
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
400 execvp (prog_path, prog_argv);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
401 _exit (127);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
402 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
403 if (child == -1)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
404 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
405 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
406 unblock_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
407 if (exit_on_error || !null_stderr)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
408 error (exit_on_error ? EXIT_FAILURE : 0, errno,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
409 _("%s subprocess failed"), progname);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
410 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
411 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
412 close (ifd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
413 close (ifd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
414 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
415 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
416 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
417 close (ofd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
418 close (ofd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
419 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
420 return -1;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
421 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
422 # endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
423 if (slave_process)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
424 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
425 register_slave_subprocess (child);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
426 unblock_fatal_signals ();
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
427 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
428 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
429 close (ofd[0]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
430 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
431 close (ifd[1]);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
432
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
433 if (pipe_stdout)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
434 fd[0] = ifd[0];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
435 if (pipe_stdin)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
436 fd[1] = ofd[1];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
437 return child;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
438
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
439 #endif
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
440 }
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
441
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
442 /* Open a bidirectional pipe.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
443 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
444 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
445 * parent -> fd[1] -> STDIN_FILENO -> child
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
446 * parent <- fd[0] <- STDOUT_FILENO <- child
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
447 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
448 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
449 */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
450 pid_t
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
451 create_pipe_bidi (const char *progname,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
452 const char *prog_path, char **prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
453 bool null_stderr,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
454 bool slave_process, bool exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
455 int fd[2])
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
456 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
457 pid_t result = create_pipe (progname, prog_path, prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
458 true, true, NULL, NULL,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
459 null_stderr, slave_process, exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
460 fd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
461 return result;
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
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
464 /* Open a pipe for input from a child process.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
465 * The child's stdin comes from a file.
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 * read system write
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
468 * parent <- fd[0] <- STDOUT_FILENO <- child
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
469 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
470 */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
471 pid_t
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
472 create_pipe_in (const char *progname,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
473 const char *prog_path, char **prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
474 const char *prog_stdin, bool null_stderr,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
475 bool slave_process, bool exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
476 int fd[1])
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
477 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
478 int iofd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
479 pid_t result = create_pipe (progname, prog_path, prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
480 false, true, prog_stdin, NULL,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
481 null_stderr, slave_process, exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
482 iofd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
483 if (result != -1)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
484 fd[0] = iofd[0];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
485 return result;
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
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
488 /* Open a pipe for output to a child process.
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
489 * The child's stdout goes to a file.
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 * write system read
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
492 * parent -> fd[0] -> STDIN_FILENO -> child
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
493 *
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
494 */
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
495 pid_t
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
496 create_pipe_out (const char *progname,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
497 const char *prog_path, char **prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
498 const char *prog_stdout, bool null_stderr,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
499 bool slave_process, bool exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
500 int fd[1])
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
501 {
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
502 int iofd[2];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
503 pid_t result = create_pipe (progname, prog_path, prog_argv,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
504 true, false, NULL, prog_stdout,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
505 null_stderr, slave_process, exit_on_error,
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
506 iofd);
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
507 if (result != -1)
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
508 fd[0] = iofd[1];
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
509 return result;
f3ef51adfb09 New module 'pipe'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
510 }