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 * execvp failed. If possible, we'd like to let start_command
72 * know, so failures like ENOENT can be handled right away; but
73 * otherwise, finish_command will still report the error.
75 xwrite(child_notifier
, "", 1);
78 static NORETURN
void die_child(const char *err
, va_list params
)
81 int len
= vsnprintf(msg
, sizeof(msg
), err
, params
);
82 if (len
> sizeof(msg
))
85 write_in_full(child_err
, "fatal: ", 7);
86 write_in_full(child_err
, msg
, len
);
87 write_in_full(child_err
, "\n", 1);
92 static inline void set_cloexec(int fd
)
94 int flags
= fcntl(fd
, F_GETFD
);
96 fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
);
99 static int wait_or_whine(pid_t pid
, const char *argv0
, int silent_exec_failure
)
101 int status
, code
= -1;
103 int failed_errno
= 0;
105 while ((waiting
= waitpid(pid
, &status
, 0)) < 0 && errno
== EINTR
)
109 failed_errno
= errno
;
110 error("waitpid for %s failed: %s", argv0
, strerror(errno
));
111 } else if (waiting
!= pid
) {
112 error("waitpid is confused (%s)", argv0
);
113 } else if (WIFSIGNALED(status
)) {
114 code
= WTERMSIG(status
);
115 error("%s died of signal %d", argv0
, code
);
117 * This return value is chosen so that code & 0xff
118 * mimics the exit code that a POSIX shell would report for
119 * a program that died from this signal.
122 } else if (WIFEXITED(status
)) {
123 code
= WEXITSTATUS(status
);
125 * Convert special exit code when execvp failed.
129 failed_errno
= ENOENT
;
130 if (!silent_exec_failure
)
131 error("cannot run %s: %s", argv0
,
135 error("waitpid is confused (%s)", argv0
);
137 errno
= failed_errno
;
141 int start_command(struct child_process
*cmd
)
143 int need_in
, need_out
, need_err
;
144 int fdin
[2], fdout
[2], fderr
[2];
145 int failed_errno
= failed_errno
;
148 * In case of errors we must keep the promise to close FDs
149 * that have been passed in via ->in and ->out.
152 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
154 if (pipe(fdin
) < 0) {
155 failed_errno
= errno
;
163 need_out
= !cmd
->no_stdout
164 && !cmd
->stdout_to_stderr
167 if (pipe(fdout
) < 0) {
168 failed_errno
= errno
;
178 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
180 if (pipe(fderr
) < 0) {
181 failed_errno
= errno
;
191 error("cannot create pipe for %s: %s",
192 cmd
->argv
[0], strerror(failed_errno
));
193 errno
= failed_errno
;
199 trace_argv_printf(cmd
->argv
, "trace: run_command:");
205 if (pipe(notify_pipe
))
206 notify_pipe
[0] = notify_pipe
[1] = -1;
211 * Redirect the channel to write syscall error messages to
212 * before redirecting the process's stderr so that all die()
213 * in subsequent call paths use the parent's stderr.
215 if (cmd
->no_stderr
|| need_err
) {
217 set_cloexec(child_err
);
219 set_die_routine(die_child
);
221 close(notify_pipe
[0]);
222 set_cloexec(notify_pipe
[1]);
223 child_notifier
= notify_pipe
[1];
224 atexit(notify_parent
);
231 } else if (cmd
->in
) {
241 } else if (cmd
->err
> 1) {
248 else if (cmd
->stdout_to_stderr
)
253 } else if (cmd
->out
> 1) {
258 if (cmd
->dir
&& chdir(cmd
->dir
))
259 die_errno("exec '%s': cd to '%s' failed", cmd
->argv
[0],
262 for (; *cmd
->env
; cmd
->env
++) {
263 if (strchr(*cmd
->env
, '='))
264 putenv((char *)*cmd
->env
);
269 if (cmd
->preexec_cb
) {
271 * We cannot predict what the pre-exec callback does.
272 * Forgo parent notification.
274 close(child_notifier
);
280 execv_git_cmd(cmd
->argv
);
281 } else if (cmd
->use_shell
) {
282 execv_shell_cmd(cmd
->argv
);
284 execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
287 * Do not check for cmd->silent_exec_failure; the parent
288 * process will check it when it sees this exit code.
293 die_errno("cannot exec '%s'", cmd
->argv
[0]);
296 error("cannot fork() for %s: %s", cmd
->argv
[0],
297 strerror(failed_errno
= errno
));
300 * Wait for child's execvp. If the execvp succeeds (or if fork()
301 * failed), EOF is seen immediately by the parent. Otherwise, the
302 * child process sends a single byte.
303 * Note that use of this infrastructure is completely advisory,
304 * therefore, we keep error checks minimal.
306 close(notify_pipe
[1]);
307 if (read(notify_pipe
[0], ¬ify_pipe
[1], 1) == 1) {
309 * At this point we know that fork() succeeded, but execvp()
310 * failed. Errors have been reported to our stderr.
312 wait_or_whine(cmd
->pid
, cmd
->argv
[0],
313 cmd
->silent_exec_failure
);
314 failed_errno
= errno
;
317 close(notify_pipe
[0]);
321 int fhin
= 0, fhout
= 1, fherr
= 2;
322 const char **sargv
= cmd
->argv
;
323 char **env
= environ
;
326 fhin
= open("/dev/null", O_RDWR
);
333 fherr
= open("/dev/null", O_RDWR
);
335 fherr
= dup(fderr
[1]);
336 else if (cmd
->err
> 2)
337 fherr
= dup(cmd
->err
);
340 fhout
= open("/dev/null", O_RDWR
);
341 else if (cmd
->stdout_to_stderr
)
344 fhout
= dup(fdout
[1]);
345 else if (cmd
->out
> 1)
346 fhout
= dup(cmd
->out
);
349 env
= make_augmented_environ(cmd
->env
);
352 cmd
->argv
= prepare_git_cmd(cmd
->argv
);
353 } else if (cmd
->use_shell
) {
354 cmd
->argv
= prepare_shell_cmd(cmd
->argv
);
357 cmd
->pid
= mingw_spawnvpe(cmd
->argv
[0], cmd
->argv
, env
, cmd
->dir
,
359 failed_errno
= errno
;
360 if (cmd
->pid
< 0 && (!cmd
->silent_exec_failure
|| errno
!= ENOENT
))
361 error("cannot spawn %s: %s", cmd
->argv
[0], strerror(errno
));
391 errno
= failed_errno
;
413 int finish_command(struct child_process
*cmd
)
415 return wait_or_whine(cmd
->pid
, cmd
->argv
[0], cmd
->silent_exec_failure
);
418 int run_command(struct child_process
*cmd
)
420 int code
= start_command(cmd
);
423 return finish_command(cmd
);
426 static void prepare_run_command_v_opt(struct child_process
*cmd
,
430 memset(cmd
, 0, sizeof(*cmd
));
432 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
433 cmd
->git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
434 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
435 cmd
->silent_exec_failure
= opt
& RUN_SILENT_EXEC_FAILURE
? 1 : 0;
436 cmd
->use_shell
= opt
& RUN_USING_SHELL
? 1 : 0;
439 int run_command_v_opt(const char **argv
, int opt
)
441 struct child_process cmd
;
442 prepare_run_command_v_opt(&cmd
, argv
, opt
);
443 return run_command(&cmd
);
446 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
448 struct child_process cmd
;
449 prepare_run_command_v_opt(&cmd
, argv
, opt
);
452 return run_command(&cmd
);
456 static pthread_t main_thread
;
457 static int main_thread_set
;
458 static pthread_key_t async_key
;
460 static void *run_thread(void *data
)
462 struct async
*async
= data
;
465 pthread_setspecific(async_key
, async
);
466 ret
= async
->proc(async
->proc_in
, async
->proc_out
, async
->data
);
470 static NORETURN
void die_async(const char *err
, va_list params
)
472 vreportf("fatal: ", err
, params
);
474 if (!pthread_equal(main_thread
, pthread_self())) {
475 struct async
*async
= pthread_getspecific(async_key
);
476 if (async
->proc_in
>= 0)
477 close(async
->proc_in
);
478 if (async
->proc_out
>= 0)
479 close(async
->proc_out
);
480 pthread_exit((void *)128);
487 int start_async(struct async
*async
)
489 int need_in
, need_out
;
490 int fdin
[2], fdout
[2];
491 int proc_in
, proc_out
;
493 need_in
= async
->in
< 0;
495 if (pipe(fdin
) < 0) {
498 return error("cannot create pipe: %s", strerror(errno
));
503 need_out
= async
->out
< 0;
505 if (pipe(fdout
) < 0) {
510 return error("cannot create pipe: %s", strerror(errno
));
512 async
->out
= fdout
[0];
525 proc_out
= async
->out
;
530 /* Flush stdio before fork() to avoid cloning buffers */
534 if (async
->pid
< 0) {
535 error("fork (async) failed: %s", strerror(errno
));
543 exit(!!async
->proc(proc_in
, proc_out
, async
->data
));
556 if (!main_thread_set
) {
558 * We assume that the first time that start_async is called
559 * it is from the main thread.
562 main_thread
= pthread_self();
563 pthread_key_create(&async_key
, NULL
);
564 set_die_routine(die_async
);
568 set_cloexec(proc_in
);
570 set_cloexec(proc_out
);
571 async
->proc_in
= proc_in
;
572 async
->proc_out
= proc_out
;
574 int err
= pthread_create(&async
->tid
, NULL
, run_thread
, async
);
576 error("cannot create thread: %s", strerror(err
));
596 int finish_async(struct async
*async
)
599 return wait_or_whine(async
->pid
, "child process", 0);
601 void *ret
= (void *)(intptr_t)(-1);
603 if (pthread_join(async
->tid
, &ret
))
604 error("pthread_join failed");
605 return (int)(intptr_t)ret
;
609 int run_hook(const char *index_file
, const char *name
, ...)
611 struct child_process hook
;
612 const char **argv
= NULL
, *env
[2];
613 char index
[PATH_MAX
];
616 size_t i
= 0, alloc
= 0;
618 if (access(git_path("hooks/%s", name
), X_OK
) < 0)
621 va_start(args
, name
);
622 ALLOC_GROW(argv
, i
+ 1, alloc
);
623 argv
[i
++] = git_path("hooks/%s", name
);
625 ALLOC_GROW(argv
, i
+ 1, alloc
);
626 argv
[i
++] = va_arg(args
, const char *);
630 memset(&hook
, 0, sizeof(hook
));
633 hook
.stdout_to_stderr
= 1;
635 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
641 ret
= run_command(&hook
);