annotate lib/execute.c @ 5848:a48fb0e98c8c

*** empty log message ***
author Paul Eggert <eggert@cs.ucla.edu>
date Sat, 14 May 2005 06:03:57 +0000 (2005-05-14)
parents 19f7545b3c2b
children 1b0092424a44
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Creation of autonomous subprocesses.
5580
19f7545b3c2b Declare environ; not all systems declare it.
Bruno Haible <bruno@clisp.org>
parents: 4936
diff changeset
2 Copyright (C) 2001-2004 Free Software Foundation, Inc.
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 any later version.
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
0fb731aa9b43 New module 'execute'.
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. */
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #ifdef HAVE_CONFIG_H
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 # include "config.h"
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #endif
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 /* Specification. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include "execute.h"
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <errno.h>
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <fcntl.h>
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include <stdbool.h>
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include <stdlib.h>
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #include <signal.h>
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 #ifdef HAVE_UNISTD_H
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 # include <unistd.h>
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 #endif
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #include "error.h"
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 #include "exit.h"
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #include "fatal-signal.h"
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 #include "wait-process.h"
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #include "gettext.h"
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 #define _(str) gettext (str)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 #if defined _MSC_VER || defined __MINGW32__
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 /* Native Woe32 API. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 # include <process.h>
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 # include "w32spawn.h"
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 #else
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 /* Unix API. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 # ifdef HAVE_POSIX_SPAWN
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 # include <spawn.h>
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 # else
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 # ifdef HAVE_VFORK_H
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 # include <vfork.h>
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 # endif
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 # endif
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 #endif
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63
5580
19f7545b3c2b Declare environ; not all systems declare it.
Bruno Haible <bruno@clisp.org>
parents: 4936
diff changeset
64 #ifndef HAVE_ENVIRON_DECL
19f7545b3c2b Declare environ; not all systems declare it.
Bruno Haible <bruno@clisp.org>
parents: 4936
diff changeset
65 extern char **environ;
19f7545b3c2b Declare environ; not all systems declare it.
Bruno Haible <bruno@clisp.org>
parents: 4936
diff changeset
66 #endif
19f7545b3c2b Declare environ; not all systems declare it.
Bruno Haible <bruno@clisp.org>
parents: 4936
diff changeset
67
4936
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 #ifndef STDIN_FILENO
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 # define STDIN_FILENO 0
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 #endif
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 #ifndef STDOUT_FILENO
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 # define STDOUT_FILENO 1
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 #endif
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 #ifndef STDERR_FILENO
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 # define STDERR_FILENO 2
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 #endif
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 #ifdef EINTR
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 /* EINTR handling for close(), open().
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 These functions can return -1/EINTR even though we don't have any
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 signal handlers set up, namely when we get interrupted via SIGSTOP. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 static inline int
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 nonintr_close (int fd)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 {
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 int retval;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 do
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 retval = close (fd);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 while (retval < 0 && errno == EINTR);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 return retval;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 }
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 #define close nonintr_close
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 static inline int
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 nonintr_open (const char *pathname, int oflag, mode_t mode)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 {
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 int retval;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 do
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 retval = open (pathname, oflag, mode);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 while (retval < 0 && errno == EINTR);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 return retval;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 }
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 #undef open /* avoid warning on VMS */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 #define open nonintr_open
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 #endif
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 /* Execute a command, optionally redirecting any of the three standard file
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 descriptors to /dev/null. Return its exit code.
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 If it didn't terminate correctly, exit if exit_on_error is true, otherwise
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 return 127.
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 If slave_process is true, the child process will be terminated when its
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 creator receives a catchable fatal signal. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 int
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 execute (const char *progname,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 const char *prog_path, char **prog_argv,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 bool ignore_sigpipe,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 bool null_stdin, bool null_stdout, bool null_stderr,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 bool slave_process, bool exit_on_error)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 {
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 #if defined _MSC_VER || defined __MINGW32__
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 /* Native Woe32 API. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 int orig_stdin;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 int orig_stdout;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 int orig_stderr;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 int exitcode;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 int nullinfd;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 int nulloutfd;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 prog_argv = prepare_spawn (prog_argv);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 /* Save standard file handles of parent process. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 if (null_stdin)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 orig_stdin = dup_noinherit (STDIN_FILENO);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 if (null_stdout)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 orig_stdout = dup_noinherit (STDOUT_FILENO);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 if (null_stderr)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 orig_stderr = dup_noinherit (STDERR_FILENO);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 exitcode = -1;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 /* Create standard file handles of child process. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 nullinfd = -1;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 nulloutfd = -1;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 if ((!null_stdin
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 || ((nullinfd = open ("NUL", O_RDONLY, 0)) >= 0
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 && (nullinfd == STDIN_FILENO
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 || (dup2 (nullinfd, STDIN_FILENO) >= 0
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 && close (nullinfd) >= 0))))
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 && (!(null_stdout || null_stderr)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 && (!null_stdout
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 || nulloutfd == STDOUT_FILENO
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 || dup2 (nulloutfd, STDOUT_FILENO) >= 0)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 && (!null_stderr
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 || nulloutfd == STDERR_FILENO
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 || dup2 (nulloutfd, STDERR_FILENO) >= 0)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 && ((null_stdout && nulloutfd == STDOUT_FILENO)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 || (null_stderr && nulloutfd == STDERR_FILENO)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 || close (nulloutfd) >= 0))))
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 exitcode = spawnvp (P_WAIT, prog_path, prog_argv);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 if (nulloutfd >= 0)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 close (nulloutfd);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 if (nullinfd >= 0)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 close (nullinfd);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 /* Restore standard file handles of parent process. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 if (null_stderr)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 dup2 (orig_stderr, STDERR_FILENO), close (orig_stderr);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177 if (null_stdout)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 dup2 (orig_stdout, STDOUT_FILENO), close (orig_stdout);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 if (null_stdin)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 dup2 (orig_stdin, STDIN_FILENO), close (orig_stdin);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 if (exitcode == -1)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 {
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 if (exit_on_error || !null_stderr)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 error (exit_on_error ? EXIT_FAILURE : 0, errno,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 _("%s subprocess failed"), progname);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 return 127;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 }
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 return exitcode;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 #else
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 /* Unix API. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 /* Note about 127: Some errors during posix_spawnp() cause the function
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196 posix_spawnp() to return an error code; some other errors cause the
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 subprocess to exit with return code 127. It is implementation
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 dependent which error is reported which way. We treat both cases as
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 equivalent. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 #if HAVE_POSIX_SPAWN
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 sigset_t blocked_signals;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 posix_spawn_file_actions_t actions;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 bool actions_allocated;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 posix_spawnattr_t attrs;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 bool attrs_allocated;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 int err;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 pid_t child;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 #else
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 int child;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 #endif
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 #if HAVE_POSIX_SPAWN
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 if (slave_process)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 {
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 block_fatal_signals ();
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 }
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218 actions_allocated = false;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 attrs_allocated = false;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 if ((err = posix_spawn_file_actions_init (&actions)) != 0
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 || (actions_allocated = true,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 (null_stdin
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223 && (err = posix_spawn_file_actions_addopen (&actions,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224 STDIN_FILENO,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225 "/dev/null", O_RDONLY,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226 0))
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 != 0)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 || (null_stdout
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 && (err = posix_spawn_file_actions_addopen (&actions,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 STDOUT_FILENO,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 "/dev/null", O_RDWR,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 0))
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 != 0)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 || (null_stderr
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235 && (err = posix_spawn_file_actions_addopen (&actions,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 STDERR_FILENO,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237 "/dev/null", O_RDWR,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 0))
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 != 0)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240 || (slave_process
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 && ((err = posix_spawnattr_init (&attrs)) != 0
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242 || (attrs_allocated = true,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243 (err = posix_spawnattr_setsigmask (&attrs,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 &blocked_signals))
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245 != 0
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246 || (err = posix_spawnattr_setflags (&attrs,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
247 POSIX_SPAWN_SETSIGMASK))
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
248 != 0)))
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249 || (err = posix_spawnp (&child, prog_path, &actions,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
250 attrs_allocated ? &attrs : NULL, prog_argv,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
251 environ))
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
252 != 0))
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
253 {
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
254 if (actions_allocated)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 posix_spawn_file_actions_destroy (&actions);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
256 if (attrs_allocated)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
257 posix_spawnattr_destroy (&attrs);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
258 if (slave_process)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
259 unblock_fatal_signals ();
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
260 if (exit_on_error || !null_stderr)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
261 error (exit_on_error ? EXIT_FAILURE : 0, err,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
262 _("%s subprocess failed"), progname);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
263 return 127;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
264 }
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
265 posix_spawn_file_actions_destroy (&actions);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
266 if (attrs_allocated)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
267 posix_spawnattr_destroy (&attrs);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
268 #else
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
269 if (slave_process)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
270 block_fatal_signals ();
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
271 /* Use vfork() instead of fork() for efficiency. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
272 if ((child = vfork ()) == 0)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
273 {
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
274 /* Child process code. */
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
275 int nullinfd;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
276 int nulloutfd;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
277
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
278 if ((!null_stdin
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
279 || ((nullinfd = open ("/dev/null", O_RDONLY, 0)) >= 0
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
280 && (nullinfd == STDIN_FILENO
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
281 || (dup2 (nullinfd, STDIN_FILENO) >= 0
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
282 && close (nullinfd) >= 0))))
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
283 && (!(null_stdout || null_stderr)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
284 || ((nulloutfd = open ("/dev/null", O_RDWR, 0)) >= 0
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
285 && (!null_stdout
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
286 || nulloutfd == STDOUT_FILENO
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
287 || dup2 (nulloutfd, STDOUT_FILENO) >= 0)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
288 && (!null_stderr
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
289 || nulloutfd == STDERR_FILENO
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
290 || dup2 (nulloutfd, STDERR_FILENO) >= 0)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
291 && ((null_stdout && nulloutfd == STDOUT_FILENO)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
292 || (null_stderr && nulloutfd == STDERR_FILENO)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
293 || close (nulloutfd) >= 0)))
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
294 && (!slave_process || (unblock_fatal_signals (), true)))
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
295 execvp (prog_path, prog_argv);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
296 _exit (127);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
297 }
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
298 if (child == -1)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
299 {
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
300 if (slave_process)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
301 unblock_fatal_signals ();
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
302 if (exit_on_error || !null_stderr)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
303 error (exit_on_error ? EXIT_FAILURE : 0, errno,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
304 _("%s subprocess failed"), progname);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
305 return 127;
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
306 }
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
307 #endif
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
308 if (slave_process)
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
309 {
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
310 register_slave_subprocess (child);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
311 unblock_fatal_signals ();
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
312 }
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
313
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
314 return wait_subprocess (child, progname, ignore_sigpipe, null_stderr,
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
315 slave_process, exit_on_error);
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
316
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
317 #endif
0fb731aa9b43 New module 'execute'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
318 }