2 #include "run-command.h"
5 static inline void close_pair(int fd
[2])
12 static inline void dup_devnull(int to
)
14 int fd
= open("/dev/null", O_RDWR
);
20 static const char **prepare_shell_cmd(const char **argv
)
25 for (argc
= 0; argv
[argc
]; argc
++)
27 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
28 nargv
= xmalloc(sizeof(*nargv
) * (argc
+ 1 + 3));
31 die("BUG: shell command is empty");
33 if (strcspn(argv
[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv
[0])) {
34 nargv
[nargc
++] = "sh";
35 nargv
[nargc
++] = "-c";
38 nargv
[nargc
++] = argv
[0];
40 struct strbuf arg0
= STRBUF_INIT
;
41 strbuf_addf(&arg0
, "%s \"$@\"", argv
[0]);
42 nargv
[nargc
++] = strbuf_detach(&arg0
, NULL
);
46 for (argc
= 0; argv
[argc
]; argc
++)
47 nargv
[nargc
++] = argv
[argc
];
54 static int execv_shell_cmd(const char **argv
)
56 const char **nargv
= prepare_shell_cmd(argv
);
57 trace_argv_printf(nargv
, "trace: exec:");
58 execvp(nargv
[0], (char **)nargv
);
65 static int child_err
= 2;
66 static int child_notifier
= -1;
68 static void notify_parent(void)
71 unused
= write(child_notifier
, "", 1);
74 static NORETURN
void die_child(const char *err
, va_list params
)
78 int len
= vsnprintf(msg
, sizeof(msg
), err
, params
);
79 if (len
> sizeof(msg
))
82 unused
= write(child_err
, "fatal: ", 7);
83 unused
= write(child_err
, msg
, len
);
84 unused
= write(child_err
, "\n", 1);
88 static inline void set_cloexec(int fd
)
90 int flags
= fcntl(fd
, F_GETFD
);
92 fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
);
96 static int wait_or_whine(pid_t pid
, const char *argv0
, int silent_exec_failure
)
98 int status
, code
= -1;
100 int failed_errno
= 0;
102 while ((waiting
= waitpid(pid
, &status
, 0)) < 0 && errno
== EINTR
)
106 failed_errno
= errno
;
107 error("waitpid for %s failed: %s", argv0
, strerror(errno
));
108 } else if (waiting
!= pid
) {
109 error("waitpid is confused (%s)", argv0
);
110 } else if (WIFSIGNALED(status
)) {
111 code
= WTERMSIG(status
);
112 error("%s died of signal %d", argv0
, code
);
114 * This return value is chosen so that code & 0xff
115 * mimics the exit code that a POSIX shell would report for
116 * a program that died from this signal.
119 } else if (WIFEXITED(status
)) {
120 code
= WEXITSTATUS(status
);
122 * Convert special exit code when execvp failed.
126 failed_errno
= ENOENT
;
127 if (!silent_exec_failure
)
128 error("cannot run %s: %s", argv0
,
132 error("waitpid is confused (%s)", argv0
);
134 errno
= failed_errno
;
138 int start_command(struct child_process
*cmd
)
140 int need_in
, need_out
, need_err
;
141 int fdin
[2], fdout
[2], fderr
[2];
142 int failed_errno
= failed_errno
;
145 * In case of errors we must keep the promise to close FDs
146 * that have been passed in via ->in and ->out.
149 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
151 if (pipe(fdin
) < 0) {
152 failed_errno
= errno
;
160 need_out
= !cmd
->no_stdout
161 && !cmd
->stdout_to_stderr
164 if (pipe(fdout
) < 0) {
165 failed_errno
= errno
;
175 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
177 if (pipe(fderr
) < 0) {
178 failed_errno
= errno
;
188 error("cannot create pipe for %s: %s",
189 cmd
->argv
[0], strerror(failed_errno
));
190 errno
= failed_errno
;
196 trace_argv_printf(cmd
->argv
, "trace: run_command:");
201 if (pipe(notify_pipe
))
202 notify_pipe
[0] = notify_pipe
[1] = -1;
208 * Redirect the channel to write syscall error messages to
209 * before redirecting the process's stderr so that all die()
210 * in subsequent call paths use the parent's stderr.
212 if (cmd
->no_stderr
|| need_err
) {
214 set_cloexec(child_err
);
216 set_die_routine(die_child
);
218 close(notify_pipe
[0]);
219 set_cloexec(notify_pipe
[1]);
220 child_notifier
= notify_pipe
[1];
221 atexit(notify_parent
);
228 } else if (cmd
->in
) {
238 } else if (cmd
->err
> 1) {
245 else if (cmd
->stdout_to_stderr
)
250 } else if (cmd
->out
> 1) {
255 if (cmd
->dir
&& chdir(cmd
->dir
))
256 die_errno("exec '%s': cd to '%s' failed", cmd
->argv
[0],
259 for (; *cmd
->env
; cmd
->env
++) {
260 if (strchr(*cmd
->env
, '='))
261 putenv((char *)*cmd
->env
);
266 if (cmd
->preexec_cb
) {
268 * We cannot predict what the pre-exec callback does.
269 * Forgo parent notification.
271 close(child_notifier
);
277 execv_git_cmd(cmd
->argv
);
278 } else if (cmd
->use_shell
) {
279 execv_shell_cmd(cmd
->argv
);
281 execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
284 * Do not check for cmd->silent_exec_failure; the parent
285 * process will check it when it sees this exit code.
290 die_errno("cannot exec '%s'", cmd
->argv
[0]);
293 error("cannot fork() for %s: %s", cmd
->argv
[0],
294 strerror(failed_errno
= errno
));
297 * Wait for child's execvp. If the execvp succeeds (or if fork()
298 * failed), EOF is seen immediately by the parent. Otherwise, the
299 * child process sends a single byte.
300 * Note that use of this infrastructure is completely advisory,
301 * therefore, we keep error checks minimal.
303 close(notify_pipe
[1]);
304 if (read(notify_pipe
[0], ¬ify_pipe
[1], 1) == 1) {
306 * At this point we know that fork() succeeded, but execvp()
307 * failed. Errors have been reported to our stderr.
309 wait_or_whine(cmd
->pid
, cmd
->argv
[0],
310 cmd
->silent_exec_failure
);
311 failed_errno
= errno
;
314 close(notify_pipe
[0]);
318 int fhin
= 0, fhout
= 1, fherr
= 2;
319 const char **sargv
= cmd
->argv
;
320 char **env
= environ
;
323 fhin
= open("/dev/null", O_RDWR
);
330 fherr
= open("/dev/null", O_RDWR
);
332 fherr
= dup(fderr
[1]);
333 else if (cmd
->err
> 2)
334 fherr
= dup(cmd
->err
);
337 fhout
= open("/dev/null", O_RDWR
);
338 else if (cmd
->stdout_to_stderr
)
341 fhout
= dup(fdout
[1]);
342 else if (cmd
->out
> 1)
343 fhout
= dup(cmd
->out
);
346 env
= make_augmented_environ(cmd
->env
);
349 cmd
->argv
= prepare_git_cmd(cmd
->argv
);
350 } else if (cmd
->use_shell
) {
351 cmd
->argv
= prepare_shell_cmd(cmd
->argv
);
354 cmd
->pid
= mingw_spawnvpe(cmd
->argv
[0], cmd
->argv
, env
, cmd
->dir
,
356 failed_errno
= errno
;
357 if (cmd
->pid
< 0 && (!cmd
->silent_exec_failure
|| errno
!= ENOENT
))
358 error("cannot spawn %s: %s", cmd
->argv
[0], strerror(errno
));
388 errno
= failed_errno
;
410 int finish_command(struct child_process
*cmd
)
412 return wait_or_whine(cmd
->pid
, cmd
->argv
[0], cmd
->silent_exec_failure
);
415 int run_command(struct child_process
*cmd
)
417 int code
= start_command(cmd
);
420 return finish_command(cmd
);
423 static void prepare_run_command_v_opt(struct child_process
*cmd
,
427 memset(cmd
, 0, sizeof(*cmd
));
429 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
430 cmd
->git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
431 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
432 cmd
->silent_exec_failure
= opt
& RUN_SILENT_EXEC_FAILURE
? 1 : 0;
433 cmd
->use_shell
= opt
& RUN_USING_SHELL
? 1 : 0;
436 int run_command_v_opt(const char **argv
, int opt
)
438 struct child_process cmd
;
439 prepare_run_command_v_opt(&cmd
, argv
, opt
);
440 return run_command(&cmd
);
443 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
445 struct child_process cmd
;
446 prepare_run_command_v_opt(&cmd
, argv
, opt
);
449 return run_command(&cmd
);
453 static unsigned __stdcall
run_thread(void *data
)
455 struct async
*async
= data
;
456 return async
->proc(async
->proc_in
, async
->proc_out
, async
->data
);
460 int start_async(struct async
*async
)
462 int need_in
, need_out
;
463 int fdin
[2], fdout
[2];
464 int proc_in
, proc_out
;
466 need_in
= async
->in
< 0;
468 if (pipe(fdin
) < 0) {
471 return error("cannot create pipe: %s", strerror(errno
));
476 need_out
= async
->out
< 0;
478 if (pipe(fdout
) < 0) {
483 return error("cannot create pipe: %s", strerror(errno
));
485 async
->out
= fdout
[0];
498 proc_out
= async
->out
;
503 /* Flush stdio before fork() to avoid cloning buffers */
507 if (async
->pid
< 0) {
508 error("fork (async) failed: %s", strerror(errno
));
516 exit(!!async
->proc(proc_in
, proc_out
, async
->data
));
529 async
->proc_in
= proc_in
;
530 async
->proc_out
= proc_out
;
531 async
->tid
= (HANDLE
) _beginthreadex(NULL
, 0, run_thread
, async
, 0, NULL
);
533 error("cannot create thread: %s", strerror(errno
));
552 int finish_async(struct async
*async
)
555 int ret
= wait_or_whine(async
->pid
, "child process", 0);
558 if (WaitForSingleObject(async
->tid
, INFINITE
) != WAIT_OBJECT_0
)
559 ret
= error("waiting for thread failed: %lu", GetLastError());
560 else if (!GetExitCodeThread(async
->tid
, &ret
))
561 ret
= error("cannot get thread exit code: %lu", GetLastError());
562 CloseHandle(async
->tid
);
567 int run_hook(const char *index_file
, const char *name
, ...)
569 struct child_process hook
;
570 const char **argv
= NULL
, *env
[2];
571 char index
[PATH_MAX
];
574 size_t i
= 0, alloc
= 0;
576 if (access(git_path("hooks/%s", name
), X_OK
) < 0)
579 va_start(args
, name
);
580 ALLOC_GROW(argv
, i
+ 1, alloc
);
581 argv
[i
++] = git_path("hooks/%s", name
);
583 ALLOC_GROW(argv
, i
+ 1, alloc
);
584 argv
[i
++] = va_arg(args
, const char *);
588 memset(&hook
, 0, sizeof(hook
));
591 hook
.stdout_to_stderr
= 1;
593 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
599 ret
= run_command(&hook
);