run-command: add "use shell" option
[git/jnareb-git.git] / run-command.c
blobb2bdcfda3df9cc9a50453dae33926da7377a7b57
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
5 static inline void close_pair(int fd[2])
7 close(fd[0]);
8 close(fd[1]);
11 static inline void dup_devnull(int to)
13 int fd = open("/dev/null", O_RDWR);
14 dup2(fd, to);
15 close(fd);
18 static const char **prepare_shell_cmd(const char **argv)
20 int argc, nargc = 0;
21 const char **nargv;
23 for (argc = 0; argv[argc]; argc++)
24 ; /* just counting */
25 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
26 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
28 if (argc < 1)
29 die("BUG: shell command is empty");
31 nargv[nargc++] = "sh";
32 nargv[nargc++] = "-c";
34 if (argc < 2)
35 nargv[nargc++] = argv[0];
36 else {
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];
44 nargv[nargc] = NULL;
46 return nargv;
49 #ifndef WIN32
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);
55 free(nargv);
56 return -1;
58 #endif
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;
72 if (need_in) {
73 if (pipe(fdin) < 0) {
74 failed_errno = errno;
75 if (cmd->out > 0)
76 close(cmd->out);
77 goto fail_pipe;
79 cmd->in = fdin[1];
82 need_out = !cmd->no_stdout
83 && !cmd->stdout_to_stderr
84 && cmd->out < 0;
85 if (need_out) {
86 if (pipe(fdout) < 0) {
87 failed_errno = errno;
88 if (need_in)
89 close_pair(fdin);
90 else if (cmd->in)
91 close(cmd->in);
92 goto fail_pipe;
94 cmd->out = fdout[0];
97 need_err = !cmd->no_stderr && cmd->err < 0;
98 if (need_err) {
99 if (pipe(fderr) < 0) {
100 failed_errno = errno;
101 if (need_in)
102 close_pair(fdin);
103 else if (cmd->in)
104 close(cmd->in);
105 if (need_out)
106 close_pair(fdout);
107 else if (cmd->out)
108 close(cmd->out);
109 fail_pipe:
110 error("cannot create pipe for %s: %s",
111 cmd->argv[0], strerror(failed_errno));
112 errno = failed_errno;
113 return -1;
115 cmd->err = fderr[0];
118 trace_argv_printf(cmd->argv, "trace: run_command:");
120 #ifndef WIN32
121 fflush(NULL);
122 cmd->pid = fork();
123 if (!cmd->pid) {
124 if (cmd->no_stdin)
125 dup_devnull(0);
126 else if (need_in) {
127 dup2(fdin[0], 0);
128 close_pair(fdin);
129 } else if (cmd->in) {
130 dup2(cmd->in, 0);
131 close(cmd->in);
134 if (cmd->no_stderr)
135 dup_devnull(2);
136 else if (need_err) {
137 dup2(fderr[1], 2);
138 close_pair(fderr);
141 if (cmd->no_stdout)
142 dup_devnull(1);
143 else if (cmd->stdout_to_stderr)
144 dup2(2, 1);
145 else if (need_out) {
146 dup2(fdout[1], 1);
147 close_pair(fdout);
148 } else if (cmd->out > 1) {
149 dup2(cmd->out, 1);
150 close(cmd->out);
153 if (cmd->dir && chdir(cmd->dir))
154 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
155 cmd->dir);
156 if (cmd->env) {
157 for (; *cmd->env; cmd->env++) {
158 if (strchr(*cmd->env, '='))
159 putenv((char *)*cmd->env);
160 else
161 unsetenv(*cmd->env);
164 if (cmd->preexec_cb)
165 cmd->preexec_cb();
166 if (cmd->git_cmd) {
167 execv_git_cmd(cmd->argv);
168 } else if (cmd->use_shell) {
169 execv_shell_cmd(cmd->argv);
170 } else {
171 execvp(cmd->argv[0], (char *const*) cmd->argv);
173 trace_printf("trace: exec '%s' failed: %s\n", cmd->argv[0],
174 strerror(errno));
175 exit(127);
177 if (cmd->pid < 0)
178 error("cannot fork() for %s: %s", cmd->argv[0],
179 strerror(failed_errno = errno));
180 #else
182 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
183 const char **sargv = cmd->argv;
184 char **env = environ;
186 if (cmd->no_stdin) {
187 s0 = dup(0);
188 dup_devnull(0);
189 } else if (need_in) {
190 s0 = dup(0);
191 dup2(fdin[0], 0);
192 } else if (cmd->in) {
193 s0 = dup(0);
194 dup2(cmd->in, 0);
197 if (cmd->no_stderr) {
198 s2 = dup(2);
199 dup_devnull(2);
200 } else if (need_err) {
201 s2 = dup(2);
202 dup2(fderr[1], 2);
205 if (cmd->no_stdout) {
206 s1 = dup(1);
207 dup_devnull(1);
208 } else if (cmd->stdout_to_stderr) {
209 s1 = dup(1);
210 dup2(2, 1);
211 } else if (need_out) {
212 s1 = dup(1);
213 dup2(fdout[1], 1);
214 } else if (cmd->out > 1) {
215 s1 = dup(1);
216 dup2(cmd->out, 1);
219 if (cmd->dir)
220 die("chdir in start_command() not implemented");
221 if (cmd->env)
222 env = make_augmented_environ(cmd->env);
224 if (cmd->git_cmd) {
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));
235 if (cmd->env)
236 free_environ(env);
237 if (cmd->git_cmd)
238 free(cmd->argv);
240 cmd->argv = sargv;
241 if (s0 >= 0)
242 dup2(s0, 0), close(s0);
243 if (s1 >= 0)
244 dup2(s1, 1), close(s1);
245 if (s2 >= 0)
246 dup2(s2, 2), close(s2);
248 #endif
250 if (cmd->pid < 0) {
251 if (need_in)
252 close_pair(fdin);
253 else if (cmd->in)
254 close(cmd->in);
255 if (need_out)
256 close_pair(fdout);
257 else if (cmd->out)
258 close(cmd->out);
259 if (need_err)
260 close_pair(fderr);
261 errno = failed_errno;
262 return -1;
265 if (need_in)
266 close(fdin[0]);
267 else if (cmd->in)
268 close(cmd->in);
270 if (need_out)
271 close(fdout[1]);
272 else if (cmd->out)
273 close(cmd->out);
275 if (need_err)
276 close(fderr[1]);
278 return 0;
281 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
283 int status, code = -1;
284 pid_t waiting;
285 int failed_errno = 0;
287 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
288 ; /* nothing */
290 if (waiting < 0) {
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.
303 code -= 128;
304 } else if (WIFEXITED(status)) {
305 code = WEXITSTATUS(status);
307 * Convert special exit code when execvp failed.
309 if (code == 127) {
310 code = -1;
311 failed_errno = ENOENT;
312 if (!silent_exec_failure)
313 error("cannot run %s: %s", argv0,
314 strerror(ENOENT));
316 } else {
317 error("waitpid is confused (%s)", argv0);
319 errno = failed_errno;
320 return code;
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);
331 if (code)
332 return code;
333 return finish_command(cmd);
336 static void prepare_run_command_v_opt(struct child_process *cmd,
337 const char **argv,
338 int opt)
340 memset(cmd, 0, sizeof(*cmd));
341 cmd->argv = argv;
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);
360 cmd.dir = dir;
361 cmd.env = env;
362 return run_command(&cmd);
365 #ifdef WIN32
366 static unsigned __stdcall run_thread(void *data)
368 struct async *async = data;
369 return async->proc(async->fd_for_proc, async->data);
371 #endif
373 int start_async(struct async *async)
375 int pipe_out[2];
377 if (pipe(pipe_out) < 0)
378 return error("cannot create pipe: %s", strerror(errno));
379 async->out = pipe_out[0];
381 #ifndef WIN32
382 /* Flush stdio before fork() to avoid cloning buffers */
383 fflush(NULL);
385 async->pid = fork();
386 if (async->pid < 0) {
387 error("fork (async) failed: %s", strerror(errno));
388 close_pair(pipe_out);
389 return -1;
391 if (!async->pid) {
392 close(pipe_out[0]);
393 exit(!!async->proc(pipe_out[1], async->data));
395 close(pipe_out[1]);
396 #else
397 async->fd_for_proc = pipe_out[1];
398 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
399 if (!async->tid) {
400 error("cannot create thread: %s", strerror(errno));
401 close_pair(pipe_out);
402 return -1;
404 #endif
405 return 0;
408 int finish_async(struct async *async)
410 #ifndef WIN32
411 int ret = wait_or_whine(async->pid, "child process", 0);
412 #else
413 DWORD ret = 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);
419 #endif
420 return ret;
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];
428 va_list args;
429 int ret;
430 size_t i = 0, alloc = 0;
432 if (access(git_path("hooks/%s", name), X_OK) < 0)
433 return 0;
435 va_start(args, name);
436 ALLOC_GROW(argv, i + 1, alloc);
437 argv[i++] = git_path("hooks/%s", name);
438 while (argv[i-1]) {
439 ALLOC_GROW(argv, i + 1, alloc);
440 argv[i++] = va_arg(args, const char *);
442 va_end(args);
444 memset(&hook, 0, sizeof(hook));
445 hook.argv = argv;
446 hook.no_stdin = 1;
447 hook.stdout_to_stderr = 1;
448 if (index_file) {
449 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
450 env[0] = index;
451 env[1] = NULL;
452 hook.env = env;
455 ret = run_command(&hook);
456 free(argv);
457 return ret;