19
{
20
int need_in, need_out, need_err;
21
int fdin[2], fdout[2], fderr[2];
22
+ int failed_errno;
23
24
/*
25
* In case of errors we must keep the promise to close FDs
29
need_in = !cmd->no_stdin && cmd->in < 0;
30
if (need_in) {
31
if (pipe(fdin) < 0) {
32
+ failed_errno = errno;
33
if (cmd->out > 0)
34
close(cmd->out);
33
- return -ERR_RUN_COMMAND_PIPE;
35
+ goto fail_pipe;
36
}
37
cmd->in = fdin[1];
38
}
42
&& cmd->out < 0;
43
if (need_out) {
44
if (pipe(fdout) < 0) {
45
+ failed_errno = errno;
46
if (need_in)
47
close_pair(fdin);
48
else if (cmd->in)
49
close(cmd->in);
47
- return -ERR_RUN_COMMAND_PIPE;
50
+ goto fail_pipe;
51
}
52
cmd->out = fdout[0];
53
}
55
need_err = !cmd->no_stderr && cmd->err < 0;
56
if (need_err) {
57
if (pipe(fderr) < 0) {
58
+ failed_errno = errno;
59
if (need_in)
60
close_pair(fdin);
61
else if (cmd->in)
64
close_pair(fdout);
65
else if (cmd->out)
66
close(cmd->out);
63
- return -ERR_RUN_COMMAND_PIPE;
67
+fail_pipe:
68
+ error("cannot create pipe for %s: %s",
69
+ cmd->argv[0], strerror(failed_errno));
70
+ errno = failed_errno;
71
+ return -1;
72
}
73
cmd->err = fderr[0];
74
}
130
strerror(errno));
131
exit(127);
132
}
133
+ if (cmd->pid < 0)
134
+ error("cannot fork() for %s: %s", cmd->argv[0],
135
+ strerror(failed_errno = errno));
136
#else
137
int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
138
const char **sargv = cmd->argv;
184
}
185
186
cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
187
+ failed_errno = errno;
188
+ if (cmd->pid < 0 && errno != ENOENT)
189
+ error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
190
191
if (cmd->env)
192
free_environ(env);
203
#endif
204
205
if (cmd->pid < 0) {
192
- int err = errno;
206
if (need_in)
207
close_pair(fdin);
208
else if (cmd->in)
213
close(cmd->out);
214
if (need_err)
215
close_pair(fderr);
203
- return err == ENOENT ?
204
- -ERR_RUN_COMMAND_EXEC :
205
- -ERR_RUN_COMMAND_FORK;
216
+ errno = failed_errno;
217
+ return -1;
218
}
219
220
if (need_in)
233
return 0;
234
}
235
224
-static int wait_or_whine(pid_t pid)
236
+static int wait_or_whine(pid_t pid, const char *argv0)
237
{
226
- for (;;) {
227
- int status, code;
228
- pid_t waiting = waitpid(pid, &status, 0);
229
-
230
- if (waiting < 0) {
231
- if (errno == EINTR)
232
- continue;
233
- error("waitpid failed (%s)", strerror(errno));
234
- return -ERR_RUN_COMMAND_WAITPID;
235
- }
236
- if (waiting != pid)
237
- return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
238
- if (WIFSIGNALED(status))
239
- return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
240
-
241
- if (!WIFEXITED(status))
242
- return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
238
+ int status, code = -1;
239
+ pid_t waiting;
240
+ int failed_errno = 0;
241
+
242
+ while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
243
+ ; /* nothing */
244
+
245
+ if (waiting < 0) {
246
+ failed_errno = errno;
247
+ error("waitpid for %s failed: %s", argv0, strerror(errno));
248
+ } else if (waiting != pid) {
249
+ error("waitpid is confused (%s)", argv0);
250
+ } else if (WIFSIGNALED(status)) {
251
+ error("%s died of signal", argv0);
252
+ } else if (WIFEXITED(status)) {
253
code = WEXITSTATUS(status);
244
- return code == 127 ? -ERR_RUN_COMMAND_EXEC : code;
254
+ /*
255
+ * Convert special exit code when execvp failed.
256
+ */
257
+ if (code == 127) {
258
+ code = -1;
259
+ failed_errno = ENOENT;
260
+ }
261
+ } else {
262
+ error("waitpid is confused (%s)", argv0);
263
}
264
+ errno = failed_errno;
265
+ return code;
266
}
267
268
int finish_command(struct child_process *cmd)
269
{
250
- return wait_or_whine(cmd->pid);
270
+ return wait_or_whine(cmd->pid, cmd->argv[0]);
271
}
272
273
int run_command(struct child_process *cmd)
351
int finish_async(struct async *async)
352
{
353
#ifndef __MINGW32__
334
- int ret = 0;
335
-
336
- if (wait_or_whine(async->pid))
337
- ret = error("waitpid (async) failed");
354
+ int ret = wait_or_whine(async->pid, "child process");
355
#else
356
DWORD ret = 0;
357
if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
395
hook.env = env;
396
}
397
381
- ret = start_command(&hook);
398
+ ret = run_command(&hook);
399
free(argv);
383
- if (ret) {
384
- warning("Could not spawn %s", argv[0]);
385
- return ret;
386
- }
387
- ret = finish_command(&hook);
388
- if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
389
- warning("%s exited due to uncaught signal", argv[0]);
390
-
400
return ret;
401
}