Merge branch 'jk/warn-author-committer-after-commit'
[git/jrn.git] / run-command.c
bloba90984576411d237c1b83b66427e99d9e37ea7c8
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 #ifndef WIN32
12 static inline void dup_devnull(int to)
14 int fd = open("/dev/null", O_RDWR);
15 dup2(fd, to);
16 close(fd);
18 #endif
20 static const char **prepare_shell_cmd(const char **argv)
22 int argc, nargc = 0;
23 const char **nargv;
25 for (argc = 0; argv[argc]; argc++)
26 ; /* just counting */
27 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
28 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
30 if (argc < 1)
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";
37 if (argc < 2)
38 nargv[nargc++] = argv[0];
39 else {
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];
48 nargv[nargc] = NULL;
50 return nargv;
53 #ifndef WIN32
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);
59 free(nargv);
60 return -1;
62 #endif
64 int start_command(struct child_process *cmd)
66 int need_in, need_out, need_err;
67 int fdin[2], fdout[2], fderr[2];
68 int failed_errno = failed_errno;
71 * In case of errors we must keep the promise to close FDs
72 * that have been passed in via ->in and ->out.
75 need_in = !cmd->no_stdin && cmd->in < 0;
76 if (need_in) {
77 if (pipe(fdin) < 0) {
78 failed_errno = errno;
79 if (cmd->out > 0)
80 close(cmd->out);
81 goto fail_pipe;
83 cmd->in = fdin[1];
86 need_out = !cmd->no_stdout
87 && !cmd->stdout_to_stderr
88 && cmd->out < 0;
89 if (need_out) {
90 if (pipe(fdout) < 0) {
91 failed_errno = errno;
92 if (need_in)
93 close_pair(fdin);
94 else if (cmd->in)
95 close(cmd->in);
96 goto fail_pipe;
98 cmd->out = fdout[0];
101 need_err = !cmd->no_stderr && cmd->err < 0;
102 if (need_err) {
103 if (pipe(fderr) < 0) {
104 failed_errno = errno;
105 if (need_in)
106 close_pair(fdin);
107 else if (cmd->in)
108 close(cmd->in);
109 if (need_out)
110 close_pair(fdout);
111 else if (cmd->out)
112 close(cmd->out);
113 fail_pipe:
114 error("cannot create pipe for %s: %s",
115 cmd->argv[0], strerror(failed_errno));
116 errno = failed_errno;
117 return -1;
119 cmd->err = fderr[0];
122 trace_argv_printf(cmd->argv, "trace: run_command:");
124 #ifndef WIN32
125 fflush(NULL);
126 cmd->pid = fork();
127 if (!cmd->pid) {
128 if (cmd->no_stdin)
129 dup_devnull(0);
130 else if (need_in) {
131 dup2(fdin[0], 0);
132 close_pair(fdin);
133 } else if (cmd->in) {
134 dup2(cmd->in, 0);
135 close(cmd->in);
138 if (cmd->no_stderr)
139 dup_devnull(2);
140 else if (need_err) {
141 dup2(fderr[1], 2);
142 close_pair(fderr);
145 if (cmd->no_stdout)
146 dup_devnull(1);
147 else if (cmd->stdout_to_stderr)
148 dup2(2, 1);
149 else if (need_out) {
150 dup2(fdout[1], 1);
151 close_pair(fdout);
152 } else if (cmd->out > 1) {
153 dup2(cmd->out, 1);
154 close(cmd->out);
157 if (cmd->dir && chdir(cmd->dir))
158 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
159 cmd->dir);
160 if (cmd->env) {
161 for (; *cmd->env; cmd->env++) {
162 if (strchr(*cmd->env, '='))
163 putenv((char *)*cmd->env);
164 else
165 unsetenv(*cmd->env);
168 if (cmd->preexec_cb)
169 cmd->preexec_cb();
170 if (cmd->git_cmd) {
171 execv_git_cmd(cmd->argv);
172 } else if (cmd->use_shell) {
173 execv_shell_cmd(cmd->argv);
174 } else {
175 execvp(cmd->argv[0], (char *const*) cmd->argv);
177 trace_printf("trace: exec '%s' failed: %s\n", cmd->argv[0],
178 strerror(errno));
179 exit(127);
181 if (cmd->pid < 0)
182 error("cannot fork() for %s: %s", cmd->argv[0],
183 strerror(failed_errno = errno));
184 #else
186 int fhin = 0, fhout = 1, fherr = 2;
187 const char **sargv = cmd->argv;
188 char **env = environ;
190 if (cmd->no_stdin)
191 fhin = open("/dev/null", O_RDWR);
192 else if (need_in)
193 fhin = dup(fdin[0]);
194 else if (cmd->in)
195 fhin = dup(cmd->in);
197 if (cmd->no_stderr)
198 fherr = open("/dev/null", O_RDWR);
199 else if (need_err)
200 fherr = dup(fderr[1]);
202 if (cmd->no_stdout)
203 fhout = open("/dev/null", O_RDWR);
204 else if (cmd->stdout_to_stderr)
205 fhout = dup(fherr);
206 else if (need_out)
207 fhout = dup(fdout[1]);
208 else if (cmd->out > 1)
209 fhout = dup(cmd->out);
211 if (cmd->dir)
212 die("chdir in start_command() not implemented");
213 if (cmd->env)
214 env = make_augmented_environ(cmd->env);
216 if (cmd->git_cmd) {
217 cmd->argv = prepare_git_cmd(cmd->argv);
218 } else if (cmd->use_shell) {
219 cmd->argv = prepare_shell_cmd(cmd->argv);
222 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env,
223 fhin, fhout, fherr);
224 failed_errno = errno;
225 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
226 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
228 if (cmd->env)
229 free_environ(env);
230 if (cmd->git_cmd)
231 free(cmd->argv);
233 cmd->argv = sargv;
234 if (fhin != 0)
235 close(fhin);
236 if (fhout != 1)
237 close(fhout);
238 if (fherr != 2)
239 close(fherr);
241 #endif
243 if (cmd->pid < 0) {
244 if (need_in)
245 close_pair(fdin);
246 else if (cmd->in)
247 close(cmd->in);
248 if (need_out)
249 close_pair(fdout);
250 else if (cmd->out)
251 close(cmd->out);
252 if (need_err)
253 close_pair(fderr);
254 errno = failed_errno;
255 return -1;
258 if (need_in)
259 close(fdin[0]);
260 else if (cmd->in)
261 close(cmd->in);
263 if (need_out)
264 close(fdout[1]);
265 else if (cmd->out)
266 close(cmd->out);
268 if (need_err)
269 close(fderr[1]);
271 return 0;
274 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
276 int status, code = -1;
277 pid_t waiting;
278 int failed_errno = 0;
280 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
281 ; /* nothing */
283 if (waiting < 0) {
284 failed_errno = errno;
285 error("waitpid for %s failed: %s", argv0, strerror(errno));
286 } else if (waiting != pid) {
287 error("waitpid is confused (%s)", argv0);
288 } else if (WIFSIGNALED(status)) {
289 code = WTERMSIG(status);
290 error("%s died of signal %d", argv0, code);
292 * This return value is chosen so that code & 0xff
293 * mimics the exit code that a POSIX shell would report for
294 * a program that died from this signal.
296 code -= 128;
297 } else if (WIFEXITED(status)) {
298 code = WEXITSTATUS(status);
300 * Convert special exit code when execvp failed.
302 if (code == 127) {
303 code = -1;
304 failed_errno = ENOENT;
305 if (!silent_exec_failure)
306 error("cannot run %s: %s", argv0,
307 strerror(ENOENT));
309 } else {
310 error("waitpid is confused (%s)", argv0);
312 errno = failed_errno;
313 return code;
316 int finish_command(struct child_process *cmd)
318 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
321 int run_command(struct child_process *cmd)
323 int code = start_command(cmd);
324 if (code)
325 return code;
326 return finish_command(cmd);
329 static void prepare_run_command_v_opt(struct child_process *cmd,
330 const char **argv,
331 int opt)
333 memset(cmd, 0, sizeof(*cmd));
334 cmd->argv = argv;
335 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
336 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
337 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
338 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
339 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
342 int run_command_v_opt(const char **argv, int opt)
344 struct child_process cmd;
345 prepare_run_command_v_opt(&cmd, argv, opt);
346 return run_command(&cmd);
349 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
351 struct child_process cmd;
352 prepare_run_command_v_opt(&cmd, argv, opt);
353 cmd.dir = dir;
354 cmd.env = env;
355 return run_command(&cmd);
358 #ifdef WIN32
359 static unsigned __stdcall run_thread(void *data)
361 struct async *async = data;
362 return async->proc(async->fd_for_proc, async->data);
364 #endif
366 int start_async(struct async *async)
368 int pipe_out[2];
370 if (pipe(pipe_out) < 0)
371 return error("cannot create pipe: %s", strerror(errno));
372 async->out = pipe_out[0];
374 #ifndef WIN32
375 /* Flush stdio before fork() to avoid cloning buffers */
376 fflush(NULL);
378 async->pid = fork();
379 if (async->pid < 0) {
380 error("fork (async) failed: %s", strerror(errno));
381 close_pair(pipe_out);
382 return -1;
384 if (!async->pid) {
385 close(pipe_out[0]);
386 exit(!!async->proc(pipe_out[1], async->data));
388 close(pipe_out[1]);
389 #else
390 async->fd_for_proc = pipe_out[1];
391 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
392 if (!async->tid) {
393 error("cannot create thread: %s", strerror(errno));
394 close_pair(pipe_out);
395 return -1;
397 #endif
398 return 0;
401 int finish_async(struct async *async)
403 #ifndef WIN32
404 int ret = wait_or_whine(async->pid, "child process", 0);
405 #else
406 DWORD ret = 0;
407 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
408 ret = error("waiting for thread failed: %lu", GetLastError());
409 else if (!GetExitCodeThread(async->tid, &ret))
410 ret = error("cannot get thread exit code: %lu", GetLastError());
411 CloseHandle(async->tid);
412 #endif
413 return ret;
416 int run_hook(const char *index_file, const char *name, ...)
418 struct child_process hook;
419 const char **argv = NULL, *env[2];
420 char index[PATH_MAX];
421 va_list args;
422 int ret;
423 size_t i = 0, alloc = 0;
425 if (access(git_path("hooks/%s", name), X_OK) < 0)
426 return 0;
428 va_start(args, name);
429 ALLOC_GROW(argv, i + 1, alloc);
430 argv[i++] = git_path("hooks/%s", name);
431 while (argv[i-1]) {
432 ALLOC_GROW(argv, i + 1, alloc);
433 argv[i++] = va_arg(args, const char *);
435 va_end(args);
437 memset(&hook, 0, sizeof(hook));
438 hook.argv = argv;
439 hook.no_stdin = 1;
440 hook.stdout_to_stderr = 1;
441 if (index_file) {
442 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
443 env[0] = index;
444 env[1] = NULL;
445 hook.env = env;
448 ret = run_command(&hook);
449 free(argv);
450 return ret;