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