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 int start_command(struct child_process
*cmd
)
20 int need_in
, need_out
, need_err
;
21 int fdin
[2], fdout
[2], fderr
[2];
25 * In case of errors we must keep the promise to close FDs
26 * that have been passed in via ->in and ->out.
29 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
40 need_out
= !cmd
->no_stdout
41 && !cmd
->stdout_to_stderr
44 if (pipe(fdout
) < 0) {
55 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
57 if (pipe(fderr
) < 0) {
68 error("cannot create pipe for %s: %s",
69 cmd
->argv
[0], strerror(failed_errno
));
76 trace_argv_printf(cmd
->argv
, "trace: run_command:");
101 else if (cmd
->stdout_to_stderr
)
106 } else if (cmd
->out
> 1) {
111 if (cmd
->dir
&& chdir(cmd
->dir
))
112 die("exec %s: cd to %s failed (%s)", cmd
->argv
[0],
113 cmd
->dir
, strerror(errno
));
115 for (; *cmd
->env
; cmd
->env
++) {
116 if (strchr(*cmd
->env
, '='))
117 putenv((char *)*cmd
->env
);
125 execv_git_cmd(cmd
->argv
);
127 execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
129 trace_printf("trace: exec '%s' failed: %s\n", cmd
->argv
[0],
134 error("cannot fork() for %s: %s", cmd
->argv
[0],
135 strerror(failed_errno
= errno
));
137 int s0
= -1, s1
= -1, s2
= -1; /* backups of stdin, stdout, stderr */
138 const char **sargv
= cmd
->argv
;
139 char **env
= environ
;
144 } else if (need_in
) {
147 } else if (cmd
->in
) {
152 if (cmd
->no_stderr
) {
155 } else if (need_err
) {
160 if (cmd
->no_stdout
) {
163 } else if (cmd
->stdout_to_stderr
) {
166 } else if (need_out
) {
169 } else if (cmd
->out
> 1) {
175 die("chdir in start_command() not implemented");
177 env
= copy_environ();
178 for (; *cmd
->env
; cmd
->env
++)
179 env
= env_setenv(env
, *cmd
->env
);
183 cmd
->argv
= prepare_git_cmd(cmd
->argv
);
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
));
198 dup2(s0
, 0), close(s0
);
200 dup2(s1
, 1), close(s1
);
202 dup2(s2
, 2), close(s2
);
216 errno
= failed_errno
;
236 static int wait_or_whine(pid_t pid
, const char *argv0
)
238 int status
, code
= -1;
240 int failed_errno
= 0;
242 while ((waiting
= waitpid(pid
, &status
, 0)) < 0 && errno
== EINTR
)
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 code
= WTERMSIG(status
);
252 error("%s died of signal %d", argv0
, code
);
254 * This return value is chosen so that code & 0xff
255 * mimics the exit code that a POSIX shell would report for
256 * a program that died from this signal.
259 } else if (WIFEXITED(status
)) {
260 code
= WEXITSTATUS(status
);
262 * Convert special exit code when execvp failed.
266 failed_errno
= ENOENT
;
269 error("waitpid is confused (%s)", argv0
);
271 errno
= failed_errno
;
275 int finish_command(struct child_process
*cmd
)
277 return wait_or_whine(cmd
->pid
, cmd
->argv
[0]);
280 int run_command(struct child_process
*cmd
)
282 int code
= start_command(cmd
);
285 return finish_command(cmd
);
288 static void prepare_run_command_v_opt(struct child_process
*cmd
,
292 memset(cmd
, 0, sizeof(*cmd
));
294 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
295 cmd
->git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
296 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
299 int run_command_v_opt(const char **argv
, int opt
)
301 struct child_process cmd
;
302 prepare_run_command_v_opt(&cmd
, argv
, opt
);
303 return run_command(&cmd
);
306 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
308 struct child_process cmd
;
309 prepare_run_command_v_opt(&cmd
, argv
, opt
);
312 return run_command(&cmd
);
316 static __stdcall
unsigned run_thread(void *data
)
318 struct async
*async
= data
;
319 return async
->proc(async
->fd_for_proc
, async
->data
);
323 int start_async(struct async
*async
)
327 if (pipe(pipe_out
) < 0)
328 return error("cannot create pipe: %s", strerror(errno
));
329 async
->out
= pipe_out
[0];
332 /* Flush stdio before fork() to avoid cloning buffers */
336 if (async
->pid
< 0) {
337 error("fork (async) failed: %s", strerror(errno
));
338 close_pair(pipe_out
);
343 exit(!!async
->proc(pipe_out
[1], async
->data
));
347 async
->fd_for_proc
= pipe_out
[1];
348 async
->tid
= (HANDLE
) _beginthreadex(NULL
, 0, run_thread
, async
, 0, NULL
);
350 error("cannot create thread: %s", strerror(errno
));
351 close_pair(pipe_out
);
358 int finish_async(struct async
*async
)
361 int ret
= wait_or_whine(async
->pid
, "child process");
364 if (WaitForSingleObject(async
->tid
, INFINITE
) != WAIT_OBJECT_0
)
365 ret
= error("waiting for thread failed: %lu", GetLastError());
366 else if (!GetExitCodeThread(async
->tid
, &ret
))
367 ret
= error("cannot get thread exit code: %lu", GetLastError());
368 CloseHandle(async
->tid
);
373 int run_hook(const char *index_file
, const char *name
, ...)
375 struct child_process hook
;
376 const char **argv
= NULL
, *env
[2];
377 char index
[PATH_MAX
];
380 size_t i
= 0, alloc
= 0;
382 if (access(git_path("hooks/%s", name
), X_OK
) < 0)
385 va_start(args
, name
);
386 ALLOC_GROW(argv
, i
+ 1, alloc
);
387 argv
[i
++] = git_path("hooks/%s", name
);
389 ALLOC_GROW(argv
, i
+ 1, alloc
);
390 argv
[i
++] = va_arg(args
, const char *);
394 memset(&hook
, 0, sizeof(hook
));
397 hook
.stdout_to_stderr
= 1;
399 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
405 ret
= run_command(&hook
);