2 #include "run-command.h"
5 static inline void close_pair(int fd
[2])
11 static inline void dup_devnull(int to
)
13 int fd
= open("/dev/null", O_RDWR
);
18 static const char **prepare_shell_cmd(const char **argv
)
23 for (argc
= 0; argv
[argc
]; argc
++)
25 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
26 nargv
= xmalloc(sizeof(*nargv
) * (argc
+ 1 + 3));
29 die("BUG: shell command is empty");
31 nargv
[nargc
++] = "sh";
32 nargv
[nargc
++] = "-c";
35 nargv
[nargc
++] = argv
[0];
37 struct strbuf arg0
= STRBUF_INIT
;
38 strbuf_addf(&arg0
, "%s \"$@\"", argv
[0]);
39 nargv
[nargc
++] = strbuf_detach(&arg0
, NULL
);
42 for (argc
= 0; argv
[argc
]; argc
++)
43 nargv
[nargc
++] = argv
[argc
];
50 static int execv_shell_cmd(const char **argv
)
52 const char **nargv
= prepare_shell_cmd(argv
);
53 trace_argv_printf(nargv
, "trace: exec:");
54 execvp(nargv
[0], (char **)nargv
);
60 int start_command(struct child_process
*cmd
)
62 int need_in
, need_out
, need_err
;
63 int fdin
[2], fdout
[2], fderr
[2];
64 int failed_errno
= failed_errno
;
67 * In case of errors we must keep the promise to close FDs
68 * that have been passed in via ->in and ->out.
71 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
82 need_out
= !cmd
->no_stdout
83 && !cmd
->stdout_to_stderr
86 if (pipe(fdout
) < 0) {
97 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
99 if (pipe(fderr
) < 0) {
100 failed_errno
= errno
;
110 error("cannot create pipe for %s: %s",
111 cmd
->argv
[0], strerror(failed_errno
));
112 errno
= failed_errno
;
118 trace_argv_printf(cmd
->argv
, "trace: run_command:");
129 } else if (cmd
->in
) {
143 else if (cmd
->stdout_to_stderr
)
148 } else if (cmd
->out
> 1) {
153 if (cmd
->dir
&& chdir(cmd
->dir
))
154 die_errno("exec '%s': cd to '%s' failed", cmd
->argv
[0],
157 for (; *cmd
->env
; cmd
->env
++) {
158 if (strchr(*cmd
->env
, '='))
159 putenv((char *)*cmd
->env
);
167 execv_git_cmd(cmd
->argv
);
168 } else if (cmd
->use_shell
) {
169 execv_shell_cmd(cmd
->argv
);
171 execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
173 trace_printf("trace: exec '%s' failed: %s\n", cmd
->argv
[0],
178 error("cannot fork() for %s: %s", cmd
->argv
[0],
179 strerror(failed_errno
= errno
));
182 int s0
= -1, s1
= -1, s2
= -1; /* backups of stdin, stdout, stderr */
183 const char **sargv
= cmd
->argv
;
184 char **env
= environ
;
189 } else if (need_in
) {
192 } else if (cmd
->in
) {
197 if (cmd
->no_stderr
) {
200 } else if (need_err
) {
205 if (cmd
->no_stdout
) {
208 } else if (cmd
->stdout_to_stderr
) {
211 } else if (need_out
) {
214 } else if (cmd
->out
> 1) {
220 die("chdir in start_command() not implemented");
222 env
= make_augmented_environ(cmd
->env
);
225 cmd
->argv
= prepare_git_cmd(cmd
->argv
);
226 } else if (cmd
->use_shell
) {
227 cmd
->argv
= prepare_shell_cmd(cmd
->argv
);
230 cmd
->pid
= mingw_spawnvpe(cmd
->argv
[0], cmd
->argv
, env
);
231 failed_errno
= errno
;
232 if (cmd
->pid
< 0 && (!cmd
->silent_exec_failure
|| errno
!= ENOENT
))
233 error("cannot spawn %s: %s", cmd
->argv
[0], strerror(errno
));
242 dup2(s0
, 0), close(s0
);
244 dup2(s1
, 1), close(s1
);
246 dup2(s2
, 2), close(s2
);
261 errno
= failed_errno
;
281 static int wait_or_whine(pid_t pid
, const char *argv0
, int silent_exec_failure
)
283 int status
, code
= -1;
285 int failed_errno
= 0;
287 while ((waiting
= waitpid(pid
, &status
, 0)) < 0 && errno
== EINTR
)
291 failed_errno
= errno
;
292 error("waitpid for %s failed: %s", argv0
, strerror(errno
));
293 } else if (waiting
!= pid
) {
294 error("waitpid is confused (%s)", argv0
);
295 } else if (WIFSIGNALED(status
)) {
296 code
= WTERMSIG(status
);
297 error("%s died of signal %d", argv0
, code
);
299 * This return value is chosen so that code & 0xff
300 * mimics the exit code that a POSIX shell would report for
301 * a program that died from this signal.
304 } else if (WIFEXITED(status
)) {
305 code
= WEXITSTATUS(status
);
307 * Convert special exit code when execvp failed.
311 failed_errno
= ENOENT
;
312 if (!silent_exec_failure
)
313 error("cannot run %s: %s", argv0
,
317 error("waitpid is confused (%s)", argv0
);
319 errno
= failed_errno
;
323 int finish_command(struct child_process
*cmd
)
325 return wait_or_whine(cmd
->pid
, cmd
->argv
[0], cmd
->silent_exec_failure
);
328 int run_command(struct child_process
*cmd
)
330 int code
= start_command(cmd
);
333 return finish_command(cmd
);
336 static void prepare_run_command_v_opt(struct child_process
*cmd
,
340 memset(cmd
, 0, sizeof(*cmd
));
342 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
343 cmd
->git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
344 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
345 cmd
->silent_exec_failure
= opt
& RUN_SILENT_EXEC_FAILURE
? 1 : 0;
346 cmd
->use_shell
= opt
& RUN_USING_SHELL
? 1 : 0;
349 int run_command_v_opt(const char **argv
, int opt
)
351 struct child_process cmd
;
352 prepare_run_command_v_opt(&cmd
, argv
, opt
);
353 return run_command(&cmd
);
356 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
358 struct child_process cmd
;
359 prepare_run_command_v_opt(&cmd
, argv
, opt
);
362 return run_command(&cmd
);
366 static unsigned __stdcall
run_thread(void *data
)
368 struct async
*async
= data
;
369 return async
->proc(async
->fd_for_proc
, async
->data
);
373 int start_async(struct async
*async
)
377 if (pipe(pipe_out
) < 0)
378 return error("cannot create pipe: %s", strerror(errno
));
379 async
->out
= pipe_out
[0];
382 /* Flush stdio before fork() to avoid cloning buffers */
386 if (async
->pid
< 0) {
387 error("fork (async) failed: %s", strerror(errno
));
388 close_pair(pipe_out
);
393 exit(!!async
->proc(pipe_out
[1], async
->data
));
397 async
->fd_for_proc
= pipe_out
[1];
398 async
->tid
= (HANDLE
) _beginthreadex(NULL
, 0, run_thread
, async
, 0, NULL
);
400 error("cannot create thread: %s", strerror(errno
));
401 close_pair(pipe_out
);
408 int finish_async(struct async
*async
)
411 int ret
= wait_or_whine(async
->pid
, "child process", 0);
414 if (WaitForSingleObject(async
->tid
, INFINITE
) != WAIT_OBJECT_0
)
415 ret
= error("waiting for thread failed: %lu", GetLastError());
416 else if (!GetExitCodeThread(async
->tid
, &ret
))
417 ret
= error("cannot get thread exit code: %lu", GetLastError());
418 CloseHandle(async
->tid
);
423 int run_hook(const char *index_file
, const char *name
, ...)
425 struct child_process hook
;
426 const char **argv
= NULL
, *env
[2];
427 char index
[PATH_MAX
];
430 size_t i
= 0, alloc
= 0;
432 if (access(git_path("hooks/%s", name
), X_OK
) < 0)
435 va_start(args
, name
);
436 ALLOC_GROW(argv
, i
+ 1, alloc
);
437 argv
[i
++] = git_path("hooks/%s", name
);
439 ALLOC_GROW(argv
, i
+ 1, alloc
);
440 argv
[i
++] = va_arg(args
, const char *);
444 memset(&hook
, 0, sizeof(hook
));
447 hook
.stdout_to_stderr
= 1;
449 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
455 ret
= run_command(&hook
);