2 #include "run-command.h"
4 #include "argv-array.h"
6 static inline void close_pair(int fd
[2])
13 static inline void dup_devnull(int to
)
15 int fd
= open("/dev/null", O_RDWR
);
21 static const char **prepare_shell_cmd(const char **argv
)
26 for (argc
= 0; argv
[argc
]; argc
++)
28 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
29 nargv
= xmalloc(sizeof(*nargv
) * (argc
+ 1 + 3));
32 die("BUG: shell command is empty");
34 if (strcspn(argv
[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv
[0])) {
35 nargv
[nargc
++] = "sh";
36 nargv
[nargc
++] = "-c";
39 nargv
[nargc
++] = argv
[0];
41 struct strbuf arg0
= STRBUF_INIT
;
42 strbuf_addf(&arg0
, "%s \"$@\"", argv
[0]);
43 nargv
[nargc
++] = strbuf_detach(&arg0
, NULL
);
47 for (argc
= 0; argv
[argc
]; argc
++)
48 nargv
[nargc
++] = argv
[argc
];
55 static int execv_shell_cmd(const char **argv
)
57 const char **nargv
= prepare_shell_cmd(argv
);
58 trace_argv_printf(nargv
, "trace: exec:");
59 execvp(nargv
[0], (char **)nargv
);
66 static int child_err
= 2;
67 static int child_notifier
= -1;
69 static void notify_parent(void)
72 * execvp failed. If possible, we'd like to let start_command
73 * know, so failures like ENOENT can be handled right away; but
74 * otherwise, finish_command will still report the error.
76 xwrite(child_notifier
, "", 1);
79 static NORETURN
void die_child(const char *err
, va_list params
)
82 int len
= vsnprintf(msg
, sizeof(msg
), err
, params
);
83 if (len
> sizeof(msg
))
86 write_in_full(child_err
, "fatal: ", 7);
87 write_in_full(child_err
, msg
, len
);
88 write_in_full(child_err
, "\n", 1);
93 static inline void set_cloexec(int fd
)
95 int flags
= fcntl(fd
, F_GETFD
);
97 fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
);
100 static int wait_or_whine(pid_t pid
, const char *argv0
, int silent_exec_failure
)
102 int status
, code
= -1;
104 int failed_errno
= 0;
106 while ((waiting
= waitpid(pid
, &status
, 0)) < 0 && errno
== EINTR
)
110 failed_errno
= errno
;
111 error("waitpid for %s failed: %s", argv0
, strerror(errno
));
112 } else if (waiting
!= pid
) {
113 error("waitpid is confused (%s)", argv0
);
114 } else if (WIFSIGNALED(status
)) {
115 code
= WTERMSIG(status
);
116 error("%s died of signal %d", argv0
, code
);
118 * This return value is chosen so that code & 0xff
119 * mimics the exit code that a POSIX shell would report for
120 * a program that died from this signal.
123 } else if (WIFEXITED(status
)) {
124 code
= WEXITSTATUS(status
);
126 * Convert special exit code when execvp failed.
130 failed_errno
= ENOENT
;
131 if (!silent_exec_failure
)
132 error("cannot run %s: %s", argv0
,
136 error("waitpid is confused (%s)", argv0
);
138 errno
= failed_errno
;
142 int start_command(struct child_process
*cmd
)
144 int need_in
, need_out
, need_err
;
145 int fdin
[2], fdout
[2], fderr
[2];
146 int failed_errno
= failed_errno
;
149 * In case of errors we must keep the promise to close FDs
150 * that have been passed in via ->in and ->out.
153 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
155 if (pipe(fdin
) < 0) {
156 failed_errno
= errno
;
164 need_out
= !cmd
->no_stdout
165 && !cmd
->stdout_to_stderr
168 if (pipe(fdout
) < 0) {
169 failed_errno
= errno
;
179 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
181 if (pipe(fderr
) < 0) {
182 failed_errno
= errno
;
192 error("cannot create pipe for %s: %s",
193 cmd
->argv
[0], strerror(failed_errno
));
194 errno
= failed_errno
;
200 trace_argv_printf(cmd
->argv
, "trace: run_command:");
206 if (pipe(notify_pipe
))
207 notify_pipe
[0] = notify_pipe
[1] = -1;
212 * Redirect the channel to write syscall error messages to
213 * before redirecting the process's stderr so that all die()
214 * in subsequent call paths use the parent's stderr.
216 if (cmd
->no_stderr
|| need_err
) {
218 set_cloexec(child_err
);
220 set_die_routine(die_child
);
222 close(notify_pipe
[0]);
223 set_cloexec(notify_pipe
[1]);
224 child_notifier
= notify_pipe
[1];
225 atexit(notify_parent
);
232 } else if (cmd
->in
) {
242 } else if (cmd
->err
> 1) {
249 else if (cmd
->stdout_to_stderr
)
254 } else if (cmd
->out
> 1) {
259 if (cmd
->dir
&& chdir(cmd
->dir
))
260 die_errno("exec '%s': cd to '%s' failed", cmd
->argv
[0],
263 for (; *cmd
->env
; cmd
->env
++) {
264 if (strchr(*cmd
->env
, '='))
265 putenv((char *)*cmd
->env
);
270 if (cmd
->preexec_cb
) {
272 * We cannot predict what the pre-exec callback does.
273 * Forgo parent notification.
275 close(child_notifier
);
281 execv_git_cmd(cmd
->argv
);
282 } else if (cmd
->use_shell
) {
283 execv_shell_cmd(cmd
->argv
);
285 execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
288 * Do not check for cmd->silent_exec_failure; the parent
289 * process will check it when it sees this exit code.
294 die_errno("cannot exec '%s'", cmd
->argv
[0]);
297 error("cannot fork() for %s: %s", cmd
->argv
[0],
298 strerror(failed_errno
= errno
));
301 * Wait for child's execvp. If the execvp succeeds (or if fork()
302 * failed), EOF is seen immediately by the parent. Otherwise, the
303 * child process sends a single byte.
304 * Note that use of this infrastructure is completely advisory,
305 * therefore, we keep error checks minimal.
307 close(notify_pipe
[1]);
308 if (read(notify_pipe
[0], ¬ify_pipe
[1], 1) == 1) {
310 * At this point we know that fork() succeeded, but execvp()
311 * failed. Errors have been reported to our stderr.
313 wait_or_whine(cmd
->pid
, cmd
->argv
[0],
314 cmd
->silent_exec_failure
);
315 failed_errno
= errno
;
318 close(notify_pipe
[0]);
322 int fhin
= 0, fhout
= 1, fherr
= 2;
323 const char **sargv
= cmd
->argv
;
324 char **env
= environ
;
327 fhin
= open("/dev/null", O_RDWR
);
334 fherr
= open("/dev/null", O_RDWR
);
336 fherr
= dup(fderr
[1]);
337 else if (cmd
->err
> 2)
338 fherr
= dup(cmd
->err
);
341 fhout
= open("/dev/null", O_RDWR
);
342 else if (cmd
->stdout_to_stderr
)
345 fhout
= dup(fdout
[1]);
346 else if (cmd
->out
> 1)
347 fhout
= dup(cmd
->out
);
350 env
= make_augmented_environ(cmd
->env
);
353 cmd
->argv
= prepare_git_cmd(cmd
->argv
);
354 } else if (cmd
->use_shell
) {
355 cmd
->argv
= prepare_shell_cmd(cmd
->argv
);
358 cmd
->pid
= mingw_spawnvpe(cmd
->argv
[0], cmd
->argv
, env
, cmd
->dir
,
360 failed_errno
= errno
;
361 if (cmd
->pid
< 0 && (!cmd
->silent_exec_failure
|| errno
!= ENOENT
))
362 error("cannot spawn %s: %s", cmd
->argv
[0], strerror(errno
));
392 errno
= failed_errno
;
414 int finish_command(struct child_process
*cmd
)
416 return wait_or_whine(cmd
->pid
, cmd
->argv
[0], cmd
->silent_exec_failure
);
419 int run_command(struct child_process
*cmd
)
421 int code
= start_command(cmd
);
424 return finish_command(cmd
);
427 static void prepare_run_command_v_opt(struct child_process
*cmd
,
431 memset(cmd
, 0, sizeof(*cmd
));
433 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
434 cmd
->git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
435 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
436 cmd
->silent_exec_failure
= opt
& RUN_SILENT_EXEC_FAILURE
? 1 : 0;
437 cmd
->use_shell
= opt
& RUN_USING_SHELL
? 1 : 0;
440 int run_command_v_opt(const char **argv
, int opt
)
442 struct child_process cmd
;
443 prepare_run_command_v_opt(&cmd
, argv
, opt
);
444 return run_command(&cmd
);
447 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
449 struct child_process cmd
;
450 prepare_run_command_v_opt(&cmd
, argv
, opt
);
453 return run_command(&cmd
);
457 static pthread_t main_thread
;
458 static int main_thread_set
;
459 static pthread_key_t async_key
;
461 static void *run_thread(void *data
)
463 struct async
*async
= data
;
466 pthread_setspecific(async_key
, async
);
467 ret
= async
->proc(async
->proc_in
, async
->proc_out
, async
->data
);
471 static NORETURN
void die_async(const char *err
, va_list params
)
473 vreportf("fatal: ", err
, params
);
475 if (!pthread_equal(main_thread
, pthread_self())) {
476 struct async
*async
= pthread_getspecific(async_key
);
477 if (async
->proc_in
>= 0)
478 close(async
->proc_in
);
479 if (async
->proc_out
>= 0)
480 close(async
->proc_out
);
481 pthread_exit((void *)128);
488 int start_async(struct async
*async
)
490 int need_in
, need_out
;
491 int fdin
[2], fdout
[2];
492 int proc_in
, proc_out
;
494 need_in
= async
->in
< 0;
496 if (pipe(fdin
) < 0) {
499 return error("cannot create pipe: %s", strerror(errno
));
504 need_out
= async
->out
< 0;
506 if (pipe(fdout
) < 0) {
511 return error("cannot create pipe: %s", strerror(errno
));
513 async
->out
= fdout
[0];
526 proc_out
= async
->out
;
531 /* Flush stdio before fork() to avoid cloning buffers */
535 if (async
->pid
< 0) {
536 error("fork (async) failed: %s", strerror(errno
));
544 exit(!!async
->proc(proc_in
, proc_out
, async
->data
));
557 if (!main_thread_set
) {
559 * We assume that the first time that start_async is called
560 * it is from the main thread.
563 main_thread
= pthread_self();
564 pthread_key_create(&async_key
, NULL
);
565 set_die_routine(die_async
);
569 set_cloexec(proc_in
);
571 set_cloexec(proc_out
);
572 async
->proc_in
= proc_in
;
573 async
->proc_out
= proc_out
;
575 int err
= pthread_create(&async
->tid
, NULL
, run_thread
, async
);
577 error("cannot create thread: %s", strerror(err
));
597 int finish_async(struct async
*async
)
600 return wait_or_whine(async
->pid
, "child process", 0);
602 void *ret
= (void *)(intptr_t)(-1);
604 if (pthread_join(async
->tid
, &ret
))
605 error("pthread_join failed");
606 return (int)(intptr_t)ret
;
610 int run_hook(const char *index_file
, const char *name
, ...)
612 struct child_process hook
;
613 struct argv_array argv
= ARGV_ARRAY_INIT
;
614 const char *p
, *env
[2];
615 char index
[PATH_MAX
];
619 if (access(git_path("hooks/%s", name
), X_OK
) < 0)
622 va_start(args
, name
);
623 argv_array_push(&argv
, git_path("hooks/%s", name
));
624 while ((p
= va_arg(args
, const char *)))
625 argv_array_push(&argv
, p
);
628 memset(&hook
, 0, sizeof(hook
));
629 hook
.argv
= argv
.argv
;
631 hook
.stdout_to_stderr
= 1;
633 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
639 ret
= run_command(&hook
);
640 argv_array_clear(&argv
);