]> git.ipfire.org Git - thirdparty/git.git/blame - run-command.c
fast-import: introduce "feature notes" command
[thirdparty/git.git] / run-command.c
CommitLineData
b1bf95bb
JW
1#include "cache.h"
2#include "run-command.h"
77cb17e9 3#include "exec_cmd.h"
b1bf95bb 4
9dc09c76
SP
5static inline void close_pair(int fd[2])
6{
7 close(fd[0]);
8 close(fd[1]);
9}
10
75301f90 11#ifndef WIN32
e4507ae8
SP
12static inline void dup_devnull(int to)
13{
14 int fd = open("/dev/null", O_RDWR);
15 dup2(fd, to);
16 close(fd);
17}
75301f90 18#endif
e4507ae8 19
8dba1e63
JK
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
f445644f
JK
33 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
34 nargv[nargc++] = "sh";
35 nargv[nargc++] = "-c";
8dba1e63 36
f445644f
JK
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 }
8dba1e63
JK
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
a5487ddf
JS
64#ifndef WIN32
65static int child_err = 2;
2b541bf8
JS
66static int child_notifier = -1;
67
68static void notify_parent(void)
69{
90ff12a8
MW
70 ssize_t unused;
71 unused = write(child_notifier, "", 1);
2b541bf8 72}
a5487ddf
JS
73
74static NORETURN void die_child(const char *err, va_list params)
75{
76 char msg[4096];
90ff12a8 77 ssize_t unused;
a5487ddf
JS
78 int len = vsnprintf(msg, sizeof(msg), err, params);
79 if (len > sizeof(msg))
80 len = sizeof(msg);
81
90ff12a8
MW
82 unused = write(child_err, "fatal: ", 7);
83 unused = write(child_err, msg, len);
84 unused = write(child_err, "\n", 1);
a5487ddf
JS
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
ab0b41da
JS
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
ebcb5d16 138int start_command(struct child_process *cmd)
b1bf95bb 139{
f3b33f1d
JS
140 int need_in, need_out, need_err;
141 int fdin[2], fdout[2], fderr[2];
5a7a3671 142 int failed_errno = failed_errno;
4919bf03 143
c20181e3
JS
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
e4507ae8 149 need_in = !cmd->no_stdin && cmd->in < 0;
4919bf03 150 if (need_in) {
c20181e3 151 if (pipe(fdin) < 0) {
0ac77ec3 152 failed_errno = errno;
c20181e3
JS
153 if (cmd->out > 0)
154 close(cmd->out);
0ac77ec3 155 goto fail_pipe;
c20181e3 156 }
4919bf03 157 cmd->in = fdin[1];
4919bf03
SP
158 }
159
e4507ae8
SP
160 need_out = !cmd->no_stdout
161 && !cmd->stdout_to_stderr
162 && cmd->out < 0;
f4bba25b
SP
163 if (need_out) {
164 if (pipe(fdout) < 0) {
0ac77ec3 165 failed_errno = errno;
f4bba25b
SP
166 if (need_in)
167 close_pair(fdin);
c20181e3
JS
168 else if (cmd->in)
169 close(cmd->in);
0ac77ec3 170 goto fail_pipe;
f4bba25b
SP
171 }
172 cmd->out = fdout[0];
f4bba25b
SP
173 }
174
b73a4397 175 need_err = !cmd->no_stderr && cmd->err < 0;
f3b33f1d
JS
176 if (need_err) {
177 if (pipe(fderr) < 0) {
0ac77ec3 178 failed_errno = errno;
f3b33f1d
JS
179 if (need_in)
180 close_pair(fdin);
c20181e3
JS
181 else if (cmd->in)
182 close(cmd->in);
f3b33f1d
JS
183 if (need_out)
184 close_pair(fdout);
c20181e3
JS
185 else if (cmd->out)
186 close(cmd->out);
0ac77ec3
JS
187fail_pipe:
188 error("cannot create pipe for %s: %s",
189 cmd->argv[0], strerror(failed_errno));
190 errno = failed_errno;
191 return -1;
f3b33f1d
JS
192 }
193 cmd->err = fderr[0];
194 }
195
8852f5d7
JS
196 trace_argv_printf(cmd->argv, "trace: run_command:");
197
71064e3f 198#ifndef WIN32
2b541bf8
JS
199{
200 int notify_pipe[2];
201 if (pipe(notify_pipe))
202 notify_pipe[0] = notify_pipe[1] = -1;
203
7d0b18a4 204 fflush(NULL);
ebcb5d16 205 cmd->pid = fork();
ebcb5d16 206 if (!cmd->pid) {
a5487ddf
JS
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
2b541bf8
JS
218 close(notify_pipe[0]);
219 set_cloexec(notify_pipe[1]);
220 child_notifier = notify_pipe[1];
221 atexit(notify_parent);
222
e4507ae8
SP
223 if (cmd->no_stdin)
224 dup_devnull(0);
225 else if (need_in) {
4919bf03 226 dup2(fdin[0], 0);
9dc09c76 227 close_pair(fdin);
4919bf03
SP
228 } else if (cmd->in) {
229 dup2(cmd->in, 0);
230 close(cmd->in);
95d3c4f5 231 }
4919bf03 232
ce2cf27a
CC
233 if (cmd->no_stderr)
234 dup_devnull(2);
235 else if (need_err) {
236 dup2(fderr[1], 2);
237 close_pair(fderr);
4f41b611
SP
238 } else if (cmd->err > 1) {
239 dup2(cmd->err, 2);
240 close(cmd->err);
ce2cf27a
CC
241 }
242
e4507ae8
SP
243 if (cmd->no_stdout)
244 dup_devnull(1);
245 else if (cmd->stdout_to_stderr)
cd83c74c 246 dup2(2, 1);
f4bba25b
SP
247 else if (need_out) {
248 dup2(fdout[1], 1);
249 close_pair(fdout);
250 } else if (cmd->out > 1) {
251 dup2(cmd->out, 1);
252 close(cmd->out);
253 }
254
1568fea0 255 if (cmd->dir && chdir(cmd->dir))
d824cbba
TR
256 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
257 cmd->dir);
ee493148 258 if (cmd->env) {
3427b375
AR
259 for (; *cmd->env; cmd->env++) {
260 if (strchr(*cmd->env, '='))
4b25d091 261 putenv((char *)*cmd->env);
3427b375
AR
262 else
263 unsetenv(*cmd->env);
264 }
ee493148 265 }
2b541bf8
JS
266 if (cmd->preexec_cb) {
267 /*
268 * We cannot predict what the pre-exec callback does.
269 * Forgo parent notification.
270 */
271 close(child_notifier);
272 child_notifier = -1;
273
ccf08bc3 274 cmd->preexec_cb();
2b541bf8 275 }
f1000898
SP
276 if (cmd->git_cmd) {
277 execv_git_cmd(cmd->argv);
8dba1e63
JK
278 } else if (cmd->use_shell) {
279 execv_shell_cmd(cmd->argv);
77cb17e9 280 } else {
f1000898 281 execvp(cmd->argv[0], (char *const*) cmd->argv);
128aed68 282 }
a5487ddf
JS
283 /*
284 * Do not check for cmd->silent_exec_failure; the parent
285 * process will check it when it sees this exit code.
286 */
287 if (errno == ENOENT)
288 exit(127);
289 else
290 die_errno("cannot exec '%s'", cmd->argv[0]);
b1bf95bb 291 }
0ac77ec3
JS
292 if (cmd->pid < 0)
293 error("cannot fork() for %s: %s", cmd->argv[0],
294 strerror(failed_errno = errno));
2b541bf8
JS
295
296 /*
297 * Wait for child's execvp. If the execvp succeeds (or if fork()
298 * failed), EOF is seen immediately by the parent. Otherwise, the
299 * child process sends a single byte.
300 * Note that use of this infrastructure is completely advisory,
301 * therefore, we keep error checks minimal.
302 */
303 close(notify_pipe[1]);
304 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
305 /*
306 * At this point we know that fork() succeeded, but execvp()
307 * failed. Errors have been reported to our stderr.
308 */
309 wait_or_whine(cmd->pid, cmd->argv[0],
310 cmd->silent_exec_failure);
311 failed_errno = errno;
312 cmd->pid = -1;
313 }
314 close(notify_pipe[0]);
315}
ba26f296 316#else
0d30ad71 317{
75301f90 318 int fhin = 0, fhout = 1, fherr = 2;
108ac313 319 const char **sargv = cmd->argv;
ba26f296 320 char **env = environ;
ba26f296 321
75301f90
JS
322 if (cmd->no_stdin)
323 fhin = open("/dev/null", O_RDWR);
324 else if (need_in)
325 fhin = dup(fdin[0]);
326 else if (cmd->in)
327 fhin = dup(cmd->in);
328
329 if (cmd->no_stderr)
330 fherr = open("/dev/null", O_RDWR);
331 else if (need_err)
332 fherr = dup(fderr[1]);
a886ba28
JH
333 else if (cmd->err > 2)
334 fherr = dup(cmd->err);
75301f90
JS
335
336 if (cmd->no_stdout)
337 fhout = open("/dev/null", O_RDWR);
338 else if (cmd->stdout_to_stderr)
339 fhout = dup(fherr);
340 else if (need_out)
341 fhout = dup(fdout[1]);
342 else if (cmd->out > 1)
343 fhout = dup(cmd->out);
ba26f296
JS
344
345 if (cmd->dir)
346 die("chdir in start_command() not implemented");
2affea41
JS
347 if (cmd->env)
348 env = make_augmented_environ(cmd->env);
ba26f296
JS
349
350 if (cmd->git_cmd) {
108ac313 351 cmd->argv = prepare_git_cmd(cmd->argv);
8dba1e63
JK
352 } else if (cmd->use_shell) {
353 cmd->argv = prepare_shell_cmd(cmd->argv);
ba26f296
JS
354 }
355
75301f90
JS
356 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env,
357 fhin, fhout, fherr);
0ac77ec3 358 failed_errno = errno;
c024beb5 359 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
0ac77ec3 360 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
ba26f296
JS
361
362 if (cmd->env)
363 free_environ(env);
364 if (cmd->git_cmd)
108ac313 365 free(cmd->argv);
ba26f296 366
108ac313 367 cmd->argv = sargv;
75301f90
JS
368 if (fhin != 0)
369 close(fhin);
370 if (fhout != 1)
371 close(fhout);
372 if (fherr != 2)
373 close(fherr);
0d30ad71 374}
ba26f296
JS
375#endif
376
377 if (cmd->pid < 0) {
378 if (need_in)
379 close_pair(fdin);
380 else if (cmd->in)
381 close(cmd->in);
382 if (need_out)
383 close_pair(fdout);
384 else if (cmd->out)
385 close(cmd->out);
386 if (need_err)
387 close_pair(fderr);
0ac77ec3
JS
388 errno = failed_errno;
389 return -1;
ba26f296 390 }
4919bf03
SP
391
392 if (need_in)
393 close(fdin[0]);
394 else if (cmd->in)
395 close(cmd->in);
396
f4bba25b
SP
397 if (need_out)
398 close(fdout[1]);
c20181e3 399 else if (cmd->out)
f4bba25b
SP
400 close(cmd->out);
401
f3b33f1d
JS
402 if (need_err)
403 close(fderr[1]);
4f41b611
SP
404 else if (cmd->err)
405 close(cmd->err);
f3b33f1d 406
ebcb5d16
SP
407 return 0;
408}
409
2d22c208
JS
410int finish_command(struct child_process *cmd)
411{
c024beb5 412 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
2d22c208
JS
413}
414
ebcb5d16
SP
415int run_command(struct child_process *cmd)
416{
417 int code = start_command(cmd);
418 if (code)
419 return code;
420 return finish_command(cmd);
421}
422
1568fea0 423static void prepare_run_command_v_opt(struct child_process *cmd,
ee493148
AR
424 const char **argv,
425 int opt)
1568fea0
AR
426{
427 memset(cmd, 0, sizeof(*cmd));
428 cmd->argv = argv;
429 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
430 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
431 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
c024beb5 432 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
8dba1e63 433 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
1568fea0
AR
434}
435
f1000898
SP
436int run_command_v_opt(const char **argv, int opt)
437{
438 struct child_process cmd;
1568fea0
AR
439 prepare_run_command_v_opt(&cmd, argv, opt);
440 return run_command(&cmd);
441}
442
ee493148
AR
443int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
444{
445 struct child_process cmd;
446 prepare_run_command_v_opt(&cmd, argv, opt);
447 cmd.dir = dir;
448 cmd.env = env;
449 return run_command(&cmd);
450}
2d22c208 451
71064e3f 452#ifdef WIN32
d7fa500f 453static unsigned __stdcall run_thread(void *data)
618ebe9f
JS
454{
455 struct async *async = data;
ae6a5609 456 return async->proc(async->proc_in, async->proc_out, async->data);
618ebe9f
JS
457}
458#endif
459
2d22c208
JS
460int start_async(struct async *async)
461{
ae6a5609
EFL
462 int need_in, need_out;
463 int fdin[2], fdout[2];
464 int proc_in, proc_out;
2d22c208 465
ae6a5609
EFL
466 need_in = async->in < 0;
467 if (need_in) {
468 if (pipe(fdin) < 0) {
469 if (async->out > 0)
470 close(async->out);
471 return error("cannot create pipe: %s", strerror(errno));
472 }
473 async->in = fdin[1];
474 }
475
476 need_out = async->out < 0;
477 if (need_out) {
478 if (pipe(fdout) < 0) {
479 if (need_in)
480 close_pair(fdin);
481 else if (async->in)
482 close(async->in);
483 return error("cannot create pipe: %s", strerror(errno));
484 }
485 async->out = fdout[0];
486 }
487
488 if (need_in)
489 proc_in = fdin[0];
490 else if (async->in)
491 proc_in = async->in;
492 else
493 proc_in = -1;
494
495 if (need_out)
496 proc_out = fdout[1];
497 else if (async->out)
498 proc_out = async->out;
499 else
500 proc_out = -1;
2d22c208 501
71064e3f 502#ifndef WIN32
2c3766f0
AM
503 /* Flush stdio before fork() to avoid cloning buffers */
504 fflush(NULL);
505
2d22c208
JS
506 async->pid = fork();
507 if (async->pid < 0) {
508 error("fork (async) failed: %s", strerror(errno));
ae6a5609 509 goto error;
2d22c208
JS
510 }
511 if (!async->pid) {
ae6a5609
EFL
512 if (need_in)
513 close(fdin[1]);
514 if (need_out)
515 close(fdout[0]);
516 exit(!!async->proc(proc_in, proc_out, async->data));
2d22c208 517 }
ae6a5609
EFL
518
519 if (need_in)
520 close(fdin[0]);
521 else if (async->in)
522 close(async->in);
523
524 if (need_out)
525 close(fdout[1]);
526 else if (async->out)
527 close(async->out);
618ebe9f 528#else
ae6a5609
EFL
529 async->proc_in = proc_in;
530 async->proc_out = proc_out;
618ebe9f
JS
531 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
532 if (!async->tid) {
533 error("cannot create thread: %s", strerror(errno));
ae6a5609 534 goto error;
618ebe9f
JS
535 }
536#endif
2d22c208 537 return 0;
ae6a5609
EFL
538
539error:
540 if (need_in)
541 close_pair(fdin);
542 else if (async->in)
543 close(async->in);
544
545 if (need_out)
546 close_pair(fdout);
547 else if (async->out)
548 close(async->out);
549 return -1;
2d22c208
JS
550}
551
552int finish_async(struct async *async)
553{
71064e3f 554#ifndef WIN32
c024beb5 555 int ret = wait_or_whine(async->pid, "child process", 0);
618ebe9f
JS
556#else
557 DWORD ret = 0;
558 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
559 ret = error("waiting for thread failed: %lu", GetLastError());
560 else if (!GetExitCodeThread(async->tid, &ret))
561 ret = error("cannot get thread exit code: %lu", GetLastError());
562 CloseHandle(async->tid);
563#endif
2d22c208
JS
564 return ret;
565}
ae98a008
SB
566
567int run_hook(const char *index_file, const char *name, ...)
568{
569 struct child_process hook;
14e6298f 570 const char **argv = NULL, *env[2];
ae98a008
SB
571 char index[PATH_MAX];
572 va_list args;
573 int ret;
14e6298f 574 size_t i = 0, alloc = 0;
ae98a008 575
cf94ca8e
SB
576 if (access(git_path("hooks/%s", name), X_OK) < 0)
577 return 0;
578
ae98a008 579 va_start(args, name);
14e6298f
SB
580 ALLOC_GROW(argv, i + 1, alloc);
581 argv[i++] = git_path("hooks/%s", name);
582 while (argv[i-1]) {
583 ALLOC_GROW(argv, i + 1, alloc);
584 argv[i++] = va_arg(args, const char *);
585 }
ae98a008
SB
586 va_end(args);
587
ae98a008
SB
588 memset(&hook, 0, sizeof(hook));
589 hook.argv = argv;
590 hook.no_stdin = 1;
591 hook.stdout_to_stderr = 1;
592 if (index_file) {
593 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
594 env[0] = index;
595 env[1] = NULL;
596 hook.env = env;
597 }
598
0ac77ec3 599 ret = run_command(&hook);
14e6298f 600 free(argv);
ae98a008
SB
601 return ret;
602}