1#include "cache.h"
2#include "run-command.h"
3#include "exec_cmd.h"
4
5static inline void close_pair(int fd[2])
6{
7 close(fd[0]);
8 close(fd[1]);
9}
10
11#ifndef WIN32
12static inline void dup_devnull(int to)
13{
14 int fd = open("/dev/null", O_RDWR);
15 dup2(fd, to);
16 close(fd);
17}
18#endif
19
20static const char **prepare_shell_cmd(const char **argv)
21{
22 int argc, nargc = 0;
23 const char **nargv;
24
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));
29
30 if (argc < 1)
31 die("BUG: shell command is empty");
32
33 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
34 nargv[nargc++] = "sh";
35 nargv[nargc++] = "-c";
36
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);
43 }
44 }
45
46 for (argc = 0; argv[argc]; argc++)
47 nargv[nargc++] = argv[argc];
48 nargv[nargc] = NULL;
49
50 return nargv;
51}
52
53#ifndef WIN32
54static int execv_shell_cmd(const char **argv)
55{
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;
61}
62#endif
63
64#ifndef WIN32
65static int child_err = 2;
66static int child_notifier = -1;
67
68static void notify_parent(void)
69{
70 ssize_t unused;
71 unused = write(child_notifier, "", 1);
72}
73
74static NORETURN void die_child(const char *err, va_list params)
75{
76 char msg[4096];
77 ssize_t unused;
78 int len = vsnprintf(msg, sizeof(msg), err, params);
79 if (len > sizeof(msg))
80 len = sizeof(msg);
81
82 unused = write(child_err, "fatal: ", 7);
83 unused = write(child_err, msg, len);
84 unused = write(child_err, "\n", 1);
85 exit(128);
86}
87
88static inline void set_cloexec(int fd)
89{
90 int flags = fcntl(fd, F_GETFD);
91 if (flags >= 0)
92 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
93}
94#endif
95
96static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
97{
98 int status, code = -1;
99 pid_t waiting;
100 int failed_errno = 0;
101
102 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
103 ; /* nothing */
104
105 if (waiting < 0) {
106 failed_errno = errno;
107 error("waitpid for %s failed: %s", argv0, strerror(errno));
108 } else if (waiting != pid) {
109 error("waitpid is confused (%s)", argv0);
110 } else if (WIFSIGNALED(status)) {
111 code = WTERMSIG(status);
112 error("%s died of signal %d", argv0, code);
113 /*
114 * This return value is chosen so that code & 0xff
115 * mimics the exit code that a POSIX shell would report for
116 * a program that died from this signal.
117 */
118 code -= 128;
119 } else if (WIFEXITED(status)) {
120 code = WEXITSTATUS(status);
121 /*
122 * Convert special exit code when execvp failed.
123 */
124 if (code == 127) {
125 code = -1;
126 failed_errno = ENOENT;
127 if (!silent_exec_failure)
128 error("cannot run %s: %s", argv0,
129 strerror(ENOENT));
130 }
131 } else {
132 error("waitpid is confused (%s)", argv0);
133 }
134 errno = failed_errno;
135 return code;
136}
137
138int start_command(struct child_process *cmd)
139{
140 int need_in, need_out, need_err;
141 int fdin[2], fdout[2], fderr[2];
142 int failed_errno = failed_errno;
143
144 /*
145 * In case of errors we must keep the promise to close FDs
146 * that have been passed in via ->in and ->out.
147 */
148
149 need_in = !cmd->no_stdin && cmd->in < 0;
150 if (need_in) {
151 if (pipe(fdin) < 0) {
152 failed_errno = errno;
153 if (cmd->out > 0)
154 close(cmd->out);
155 goto fail_pipe;
156 }
157 cmd->in = fdin[1];
158 }
159
160 need_out = !cmd->no_stdout
161 && !cmd->stdout_to_stderr
162 && cmd->out < 0;
163 if (need_out) {
164 if (pipe(fdout) < 0) {
165 failed_errno = errno;
166 if (need_in)
167 close_pair(fdin);
168 else if (cmd->in)
169 close(cmd->in);
170 goto fail_pipe;
171 }
172 cmd->out = fdout[0];
173 }
174
175 need_err = !cmd->no_stderr && cmd->err < 0;
176 if (need_err) {
177 if (pipe(fderr) < 0) {
178 failed_errno = errno;
179 if (need_in)
180 close_pair(fdin);
181 else if (cmd->in)
182 close(cmd->in);
183 if (need_out)
184 close_pair(fdout);
185 else if (cmd->out)
186 close(cmd->out);
187fail_pipe:
188 error("cannot create pipe for %s: %s",
189 cmd->argv[0], strerror(failed_errno));
190 errno = failed_errno;
191 return -1;
192 }
193 cmd->err = fderr[0];
194 }
195
196 trace_argv_printf(cmd->argv, "trace: run_command:");
197
198#ifndef WIN32
199{
200 int notify_pipe[2];
201 if (pipe(notify_pipe))
202 notify_pipe[0] = notify_pipe[1] = -1;
203
204 fflush(NULL);
205 cmd->pid = fork();
206 if (!cmd->pid) {
207 /*
208 * Redirect the channel to write syscall error messages to
209 * before redirecting the process's stderr so that all die()
210 * in subsequent call paths use the parent's stderr.
211 */
212 if (cmd->no_stderr || need_err) {
213 child_err = dup(2);
214 set_cloexec(child_err);
215 }
216 set_die_routine(die_child);
217
218 close(notify_pipe[0]);
219 set_cloexec(notify_pipe[1]);
220 child_notifier = notify_pipe[1];
221 atexit(notify_parent);
222
223 if (cmd->no_stdin)
224 dup_devnull(0);
225 else if (need_in) {
226 dup2(fdin[0], 0);
227 close_pair(fdin);
228 } else if (cmd->in) {
229 dup2(cmd->in, 0);
230 close(cmd->in);
231 }
232
233 if (cmd->no_stderr)
234 dup_devnull(2);
235 else if (need_err) {
236 dup2(fderr[1], 2);
237 close_pair(fderr);
238 }
239
240 if (cmd->no_stdout)
241 dup_devnull(1);
242 else if (cmd->stdout_to_stderr)
243 dup2(2, 1);
244 else if (need_out) {
245 dup2(fdout[1], 1);
246 close_pair(fdout);
247 } else if (cmd->out > 1) {
248 dup2(cmd->out, 1);
249 close(cmd->out);
250 }
251
252 if (cmd->dir && chdir(cmd->dir))
253 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
254 cmd->dir);
255 if (cmd->env) {
256 for (; *cmd->env; cmd->env++) {
257 if (strchr(*cmd->env, '='))
258 putenv((char *)*cmd->env);
259 else
260 unsetenv(*cmd->env);
261 }
262 }
263 if (cmd->preexec_cb) {
264 /*
265 * We cannot predict what the pre-exec callback does.
266 * Forgo parent notification.
267 */
268 close(child_notifier);
269 child_notifier = -1;
270
271 cmd->preexec_cb();
272 }
273 if (cmd->git_cmd) {
274 execv_git_cmd(cmd->argv);
275 } else if (cmd->use_shell) {
276 execv_shell_cmd(cmd->argv);
277 } else {
278 execvp(cmd->argv[0], (char *const*) cmd->argv);
279 }
280 /*
281 * Do not check for cmd->silent_exec_failure; the parent
282 * process will check it when it sees this exit code.
283 */
284 if (errno == ENOENT)
285 exit(127);
286 else
287 die_errno("cannot exec '%s'", cmd->argv[0]);
288 }
289 if (cmd->pid < 0)
290 error("cannot fork() for %s: %s", cmd->argv[0],
291 strerror(failed_errno = errno));
292
293 /*
294 * Wait for child's execvp. If the execvp succeeds (or if fork()
295 * failed), EOF is seen immediately by the parent. Otherwise, the
296 * child process sends a single byte.
297 * Note that use of this infrastructure is completely advisory,
298 * therefore, we keep error checks minimal.
299 */
300 close(notify_pipe[1]);
301 if (read(notify_pipe[0], ¬ify_pipe[1], 1) == 1) {
302 /*
303 * At this point we know that fork() succeeded, but execvp()
304 * failed. Errors have been reported to our stderr.
305 */
306 wait_or_whine(cmd->pid, cmd->argv[0],
307 cmd->silent_exec_failure);
308 failed_errno = errno;
309 cmd->pid = -1;
310 }
311 close(notify_pipe[0]);
312}
313#else
314{
315 int fhin = 0, fhout = 1, fherr = 2;
316 const char **sargv = cmd->argv;
317 char **env = environ;
318
319 if (cmd->no_stdin)
320 fhin = open("/dev/null", O_RDWR);
321 else if (need_in)
322 fhin = dup(fdin[0]);
323 else if (cmd->in)
324 fhin = dup(cmd->in);
325
326 if (cmd->no_stderr)
327 fherr = open("/dev/null", O_RDWR);
328 else if (need_err)
329 fherr = dup(fderr[1]);
330
331 if (cmd->no_stdout)
332 fhout = open("/dev/null", O_RDWR);
333 else if (cmd->stdout_to_stderr)
334 fhout = dup(fherr);
335 else if (need_out)
336 fhout = dup(fdout[1]);
337 else if (cmd->out > 1)
338 fhout = dup(cmd->out);
339
340 if (cmd->dir)
341 die("chdir in start_command() not implemented");
342 if (cmd->env)
343 env = make_augmented_environ(cmd->env);
344
345 if (cmd->git_cmd) {
346 cmd->argv = prepare_git_cmd(cmd->argv);
347 } else if (cmd->use_shell) {
348 cmd->argv = prepare_shell_cmd(cmd->argv);
349 }
350
351 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env,
352 fhin, fhout, fherr);
353 failed_errno = errno;
354 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
355 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
356
357 if (cmd->env)
358 free_environ(env);
359 if (cmd->git_cmd)
360 free(cmd->argv);
361
362 cmd->argv = sargv;
363 if (fhin != 0)
364 close(fhin);
365 if (fhout != 1)
366 close(fhout);
367 if (fherr != 2)
368 close(fherr);
369}
370#endif
371
372 if (cmd->pid < 0) {
373 if (need_in)
374 close_pair(fdin);
375 else if (cmd->in)
376 close(cmd->in);
377 if (need_out)
378 close_pair(fdout);
379 else if (cmd->out)
380 close(cmd->out);
381 if (need_err)
382 close_pair(fderr);
383 errno = failed_errno;
384 return -1;
385 }
386
387 if (need_in)
388 close(fdin[0]);
389 else if (cmd->in)
390 close(cmd->in);
391
392 if (need_out)
393 close(fdout[1]);
394 else if (cmd->out)
395 close(cmd->out);
396
397 if (need_err)
398 close(fderr[1]);
399
400 return 0;
401}
402
403int finish_command(struct child_process *cmd)
404{
405 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
406}
407
408int run_command(struct child_process *cmd)
409{
410 int code = start_command(cmd);
411 if (code)
412 return code;
413 return finish_command(cmd);
414}
415
416static void prepare_run_command_v_opt(struct child_process *cmd,
417 const char **argv,
418 int opt)
419{
420 memset(cmd, 0, sizeof(*cmd));
421 cmd->argv = argv;
422 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
423 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
424 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
425 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
426 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
427}
428
429int run_command_v_opt(const char **argv, int opt)
430{
431 struct child_process cmd;
432 prepare_run_command_v_opt(&cmd, argv, opt);
433 return run_command(&cmd);
434}
435
436int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
437{
438 struct child_process cmd;
439 prepare_run_command_v_opt(&cmd, argv, opt);
440 cmd.dir = dir;
441 cmd.env = env;
442 return run_command(&cmd);
443}
444
445#ifdef WIN32
446static unsigned __stdcall run_thread(void *data)
447{
448 struct async *async = data;
449 return async->proc(async->fd_for_proc, async->data);
450}
451#endif
452
453int start_async(struct async *async)
454{
455 int pipe_out[2];
456
457 if (pipe(pipe_out) < 0)
458 return error("cannot create pipe: %s", strerror(errno));
459 async->out = pipe_out[0];
460
461#ifndef WIN32
462 /* Flush stdio before fork() to avoid cloning buffers */
463 fflush(NULL);
464
465 async->pid = fork();
466 if (async->pid < 0) {
467 error("fork (async) failed: %s", strerror(errno));
468 close_pair(pipe_out);
469 return -1;
470 }
471 if (!async->pid) {
472 close(pipe_out[0]);
473 exit(!!async->proc(pipe_out[1], async->data));
474 }
475 close(pipe_out[1]);
476#else
477 async->fd_for_proc = pipe_out[1];
478 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
479 if (!async->tid) {
480 error("cannot create thread: %s", strerror(errno));
481 close_pair(pipe_out);
482 return -1;
483 }
484#endif
485 return 0;
486}
487
488int finish_async(struct async *async)
489{
490#ifndef WIN32
491 int ret = wait_or_whine(async->pid, "child process", 0);
492#else
493 DWORD ret = 0;
494 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
495 ret = error("waiting for thread failed: %lu", GetLastError());
496 else if (!GetExitCodeThread(async->tid, &ret))
497 ret = error("cannot get thread exit code: %lu", GetLastError());
498 CloseHandle(async->tid);
499#endif
500 return ret;
501}
502
503int run_hook(const char *index_file, const char *name, ...)
504{
505 struct child_process hook;
506 const char **argv = NULL, *env[2];
507 char index[PATH_MAX];
508 va_list args;
509 int ret;
510 size_t i = 0, alloc = 0;
511
512 if (access(git_path("hooks/%s", name), X_OK) < 0)
513 return 0;
514
515 va_start(args, name);
516 ALLOC_GROW(argv, i + 1, alloc);
517 argv[i++] = git_path("hooks/%s", name);
518 while (argv[i-1]) {
519 ALLOC_GROW(argv, i + 1, alloc);
520 argv[i++] = va_arg(args, const char *);
521 }
522 va_end(args);
523
524 memset(&hook, 0, sizeof(hook));
525 hook.argv = argv;
526 hook.no_stdin = 1;
527 hook.stdout_to_stderr = 1;
528 if (index_file) {
529 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
530 env[0] = index;
531 env[1] = NULL;
532 hook.env = env;
533 }
534
535 ret = run_command(&hook);
536 free(argv);
537 return ret;
538}