Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "run-command.h" |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 3 | #include "exec_cmd.h" |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 4 | #include "sigchain.h" |
Jeff King | 5d40a17 | 2011-09-13 17:58:25 -0400 | [diff] [blame] | 5 | #include "argv-array.h" |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 6 | |
Ben Walton | b3e34dd | 2012-03-30 21:33:21 -0400 | [diff] [blame] | 7 | #ifndef SHELL_PATH |
| 8 | # define SHELL_PATH "/bin/sh" |
| 9 | #endif |
| 10 | |
René Scharfe | 483bbd4 | 2014-08-19 21:10:48 +0200 | [diff] [blame] | 11 | void child_process_init(struct child_process *child) |
| 12 | { |
| 13 | memset(child, 0, sizeof(*child)); |
| 14 | argv_array_init(&child->args); |
| 15 | } |
| 16 | |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 17 | struct child_to_clean { |
| 18 | pid_t pid; |
| 19 | struct child_to_clean *next; |
| 20 | }; |
| 21 | static struct child_to_clean *children_to_clean; |
| 22 | static int installed_child_cleanup_handler; |
| 23 | |
| 24 | static void cleanup_children(int sig) |
| 25 | { |
| 26 | while (children_to_clean) { |
| 27 | struct child_to_clean *p = children_to_clean; |
| 28 | children_to_clean = p->next; |
| 29 | kill(p->pid, sig); |
| 30 | free(p); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | static void cleanup_children_on_signal(int sig) |
| 35 | { |
| 36 | cleanup_children(sig); |
| 37 | sigchain_pop(sig); |
| 38 | raise(sig); |
| 39 | } |
| 40 | |
| 41 | static void cleanup_children_on_exit(void) |
| 42 | { |
| 43 | cleanup_children(SIGTERM); |
| 44 | } |
| 45 | |
| 46 | static void mark_child_for_cleanup(pid_t pid) |
| 47 | { |
| 48 | struct child_to_clean *p = xmalloc(sizeof(*p)); |
| 49 | p->pid = pid; |
| 50 | p->next = children_to_clean; |
| 51 | children_to_clean = p; |
| 52 | |
| 53 | if (!installed_child_cleanup_handler) { |
| 54 | atexit(cleanup_children_on_exit); |
| 55 | sigchain_push_common(cleanup_children_on_signal); |
| 56 | installed_child_cleanup_handler = 1; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static void clear_child_for_cleanup(pid_t pid) |
| 61 | { |
David Gould | bdee397 | 2012-09-11 15:32:47 +0100 | [diff] [blame] | 62 | struct child_to_clean **pp; |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 63 | |
David Gould | bdee397 | 2012-09-11 15:32:47 +0100 | [diff] [blame] | 64 | for (pp = &children_to_clean; *pp; pp = &(*pp)->next) { |
| 65 | struct child_to_clean *clean_me = *pp; |
| 66 | |
| 67 | if (clean_me->pid == pid) { |
| 68 | *pp = clean_me->next; |
| 69 | free(clean_me); |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
Shawn O. Pearce | 9dc09c7 | 2007-03-12 14:37:28 -0400 | [diff] [blame] | 75 | static inline void close_pair(int fd[2]) |
| 76 | { |
| 77 | close(fd[0]); |
| 78 | close(fd[1]); |
| 79 | } |
| 80 | |
Jonathan Nieder | 380395d | 2013-05-02 20:26:08 +0100 | [diff] [blame] | 81 | #ifndef GIT_WINDOWS_NATIVE |
Shawn O. Pearce | e4507ae | 2007-03-12 14:37:55 -0400 | [diff] [blame] | 82 | static inline void dup_devnull(int to) |
| 83 | { |
| 84 | int fd = open("/dev/null", O_RDWR); |
Thomas Rast | a77f106 | 2013-07-12 10:58:36 +0200 | [diff] [blame] | 85 | if (fd < 0) |
| 86 | die_errno(_("open /dev/null failed")); |
| 87 | if (dup2(fd, to) < 0) |
| 88 | die_errno(_("dup2(%d,%d) failed"), fd, to); |
Shawn O. Pearce | e4507ae | 2007-03-12 14:37:55 -0400 | [diff] [blame] | 89 | close(fd); |
| 90 | } |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 91 | #endif |
Shawn O. Pearce | e4507ae | 2007-03-12 14:37:55 -0400 | [diff] [blame] | 92 | |
Jeff King | 38f865c | 2012-03-30 03:52:18 -0400 | [diff] [blame] | 93 | static char *locate_in_PATH(const char *file) |
| 94 | { |
| 95 | const char *p = getenv("PATH"); |
| 96 | struct strbuf buf = STRBUF_INIT; |
| 97 | |
| 98 | if (!p || !*p) |
| 99 | return NULL; |
| 100 | |
| 101 | while (1) { |
| 102 | const char *end = strchrnul(p, ':'); |
| 103 | |
| 104 | strbuf_reset(&buf); |
| 105 | |
| 106 | /* POSIX specifies an empty entry as the current directory. */ |
| 107 | if (end != p) { |
| 108 | strbuf_add(&buf, p, end - p); |
| 109 | strbuf_addch(&buf, '/'); |
| 110 | } |
| 111 | strbuf_addstr(&buf, file); |
| 112 | |
| 113 | if (!access(buf.buf, F_OK)) |
| 114 | return strbuf_detach(&buf, NULL); |
| 115 | |
| 116 | if (!*end) |
| 117 | break; |
| 118 | p = end + 1; |
| 119 | } |
| 120 | |
| 121 | strbuf_release(&buf); |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | static int exists_in_PATH(const char *file) |
| 126 | { |
| 127 | char *r = locate_in_PATH(file); |
| 128 | free(r); |
| 129 | return r != NULL; |
| 130 | } |
| 131 | |
| 132 | int sane_execvp(const char *file, char * const argv[]) |
| 133 | { |
| 134 | if (!execvp(file, argv)) |
| 135 | return 0; /* cannot happen ;-) */ |
| 136 | |
| 137 | /* |
| 138 | * When a command can't be found because one of the directories |
| 139 | * listed in $PATH is unsearchable, execvp reports EACCES, but |
| 140 | * careful usability testing (read: analysis of occasional bug |
| 141 | * reports) reveals that "No such file or directory" is more |
| 142 | * intuitive. |
| 143 | * |
| 144 | * We avoid commands with "/", because execvp will not do $PATH |
| 145 | * lookups in that case. |
| 146 | * |
| 147 | * The reassignment of EACCES to errno looks like a no-op below, |
| 148 | * but we need to protect against exists_in_PATH overwriting errno. |
| 149 | */ |
| 150 | if (errno == EACCES && !strchr(file, '/')) |
| 151 | errno = exists_in_PATH(file) ? EACCES : ENOENT; |
Junio C Hamano | a785508 | 2012-07-31 12:51:30 -0700 | [diff] [blame] | 152 | else if (errno == ENOTDIR && !strchr(file, '/')) |
| 153 | errno = ENOENT; |
Jeff King | 38f865c | 2012-03-30 03:52:18 -0400 | [diff] [blame] | 154 | return -1; |
| 155 | } |
| 156 | |
Jeff King | 8dba1e6 | 2009-12-30 05:53:16 -0500 | [diff] [blame] | 157 | static const char **prepare_shell_cmd(const char **argv) |
| 158 | { |
| 159 | int argc, nargc = 0; |
| 160 | const char **nargv; |
| 161 | |
| 162 | for (argc = 0; argv[argc]; argc++) |
| 163 | ; /* just counting */ |
| 164 | /* +1 for NULL, +3 for "sh -c" plus extra $0 */ |
| 165 | nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3)); |
| 166 | |
| 167 | if (argc < 1) |
| 168 | die("BUG: shell command is empty"); |
| 169 | |
Jeff King | f445644 | 2009-12-30 05:55:36 -0500 | [diff] [blame] | 170 | if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) { |
Jonathan Nieder | 380395d | 2013-05-02 20:26:08 +0100 | [diff] [blame] | 171 | #ifndef GIT_WINDOWS_NATIVE |
Ben Walton | b3e34dd | 2012-03-30 21:33:21 -0400 | [diff] [blame] | 172 | nargv[nargc++] = SHELL_PATH; |
Johannes Sixt | 7762975 | 2012-04-17 09:03:21 +0200 | [diff] [blame] | 173 | #else |
| 174 | nargv[nargc++] = "sh"; |
| 175 | #endif |
Jeff King | f445644 | 2009-12-30 05:55:36 -0500 | [diff] [blame] | 176 | nargv[nargc++] = "-c"; |
Jeff King | 8dba1e6 | 2009-12-30 05:53:16 -0500 | [diff] [blame] | 177 | |
Jeff King | f445644 | 2009-12-30 05:55:36 -0500 | [diff] [blame] | 178 | if (argc < 2) |
| 179 | nargv[nargc++] = argv[0]; |
| 180 | else { |
| 181 | struct strbuf arg0 = STRBUF_INIT; |
| 182 | strbuf_addf(&arg0, "%s \"$@\"", argv[0]); |
| 183 | nargv[nargc++] = strbuf_detach(&arg0, NULL); |
| 184 | } |
Jeff King | 8dba1e6 | 2009-12-30 05:53:16 -0500 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | for (argc = 0; argv[argc]; argc++) |
| 188 | nargv[nargc++] = argv[argc]; |
| 189 | nargv[nargc] = NULL; |
| 190 | |
| 191 | return nargv; |
| 192 | } |
| 193 | |
Jonathan Nieder | 380395d | 2013-05-02 20:26:08 +0100 | [diff] [blame] | 194 | #ifndef GIT_WINDOWS_NATIVE |
Jeff King | 8dba1e6 | 2009-12-30 05:53:16 -0500 | [diff] [blame] | 195 | static int execv_shell_cmd(const char **argv) |
| 196 | { |
| 197 | const char **nargv = prepare_shell_cmd(argv); |
| 198 | trace_argv_printf(nargv, "trace: exec:"); |
Jeff King | 38f865c | 2012-03-30 03:52:18 -0400 | [diff] [blame] | 199 | sane_execvp(nargv[0], (char **)nargv); |
Jeff King | 8dba1e6 | 2009-12-30 05:53:16 -0500 | [diff] [blame] | 200 | free(nargv); |
| 201 | return -1; |
| 202 | } |
| 203 | #endif |
| 204 | |
Jonathan Nieder | 380395d | 2013-05-02 20:26:08 +0100 | [diff] [blame] | 205 | #ifndef GIT_WINDOWS_NATIVE |
Johannes Sixt | a5487dd | 2010-01-10 14:07:52 +0100 | [diff] [blame] | 206 | static int child_err = 2; |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 207 | static int child_notifier = -1; |
| 208 | |
| 209 | static void notify_parent(void) |
| 210 | { |
Jonathan Nieder | a111eb7 | 2011-04-20 05:40:05 -0500 | [diff] [blame] | 211 | /* |
| 212 | * execvp failed. If possible, we'd like to let start_command |
| 213 | * know, so failures like ENOENT can be handled right away; but |
| 214 | * otherwise, finish_command will still report the error. |
| 215 | */ |
| 216 | xwrite(child_notifier, "", 1); |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 217 | } |
Johannes Sixt | a5487dd | 2010-01-10 14:07:52 +0100 | [diff] [blame] | 218 | |
| 219 | static NORETURN void die_child(const char *err, va_list params) |
| 220 | { |
Clemens Buchacher | 3bc4181 | 2011-07-27 23:32:34 +0200 | [diff] [blame] | 221 | vwritef(child_err, "fatal: ", err, params); |
Johannes Sixt | a5487dd | 2010-01-10 14:07:52 +0100 | [diff] [blame] | 222 | exit(128); |
| 223 | } |
Clemens Buchacher | 3bc4181 | 2011-07-27 23:32:34 +0200 | [diff] [blame] | 224 | |
| 225 | static void error_child(const char *err, va_list params) |
| 226 | { |
| 227 | vwritef(child_err, "error: ", err, params); |
| 228 | } |
Johannes Sixt | 200a76b | 2010-03-06 16:40:42 +0100 | [diff] [blame] | 229 | #endif |
Johannes Sixt | a5487dd | 2010-01-10 14:07:52 +0100 | [diff] [blame] | 230 | |
| 231 | static inline void set_cloexec(int fd) |
| 232 | { |
| 233 | int flags = fcntl(fd, F_GETFD); |
| 234 | if (flags >= 0) |
| 235 | fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
| 236 | } |
Johannes Sixt | a5487dd | 2010-01-10 14:07:52 +0100 | [diff] [blame] | 237 | |
Jeff King | 1327452 | 2012-11-30 17:40:50 -0500 | [diff] [blame] | 238 | static int wait_or_whine(pid_t pid, const char *argv0) |
Johannes Sixt | ab0b41d | 2010-01-10 14:08:45 +0100 | [diff] [blame] | 239 | { |
| 240 | int status, code = -1; |
| 241 | pid_t waiting; |
| 242 | int failed_errno = 0; |
| 243 | |
| 244 | while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR) |
| 245 | ; /* nothing */ |
| 246 | |
| 247 | if (waiting < 0) { |
| 248 | failed_errno = errno; |
| 249 | error("waitpid for %s failed: %s", argv0, strerror(errno)); |
| 250 | } else if (waiting != pid) { |
| 251 | error("waitpid is confused (%s)", argv0); |
| 252 | } else if (WIFSIGNALED(status)) { |
| 253 | code = WTERMSIG(status); |
Jeff King | a2767c5 | 2012-11-30 17:41:38 -0500 | [diff] [blame] | 254 | if (code != SIGINT && code != SIGQUIT) |
| 255 | error("%s died of signal %d", argv0, code); |
Johannes Sixt | ab0b41d | 2010-01-10 14:08:45 +0100 | [diff] [blame] | 256 | /* |
| 257 | * This return value is chosen so that code & 0xff |
| 258 | * mimics the exit code that a POSIX shell would report for |
| 259 | * a program that died from this signal. |
| 260 | */ |
Jeff King | 709ca73 | 2013-01-05 09:49:49 -0500 | [diff] [blame] | 261 | code += 128; |
Johannes Sixt | ab0b41d | 2010-01-10 14:08:45 +0100 | [diff] [blame] | 262 | } else if (WIFEXITED(status)) { |
| 263 | code = WEXITSTATUS(status); |
| 264 | /* |
| 265 | * Convert special exit code when execvp failed. |
| 266 | */ |
| 267 | if (code == 127) { |
| 268 | code = -1; |
| 269 | failed_errno = ENOENT; |
Johannes Sixt | ab0b41d | 2010-01-10 14:08:45 +0100 | [diff] [blame] | 270 | } |
| 271 | } else { |
| 272 | error("waitpid is confused (%s)", argv0); |
| 273 | } |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 274 | |
| 275 | clear_child_for_cleanup(pid); |
| 276 | |
Johannes Sixt | ab0b41d | 2010-01-10 14:08:45 +0100 | [diff] [blame] | 277 | errno = failed_errno; |
| 278 | return code; |
| 279 | } |
| 280 | |
Shawn O. Pearce | ebcb5d1 | 2007-03-10 03:28:05 -0500 | [diff] [blame] | 281 | int start_command(struct child_process *cmd) |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 282 | { |
Johannes Sixt | f3b33f1 | 2007-10-19 21:47:58 +0200 | [diff] [blame] | 283 | int need_in, need_out, need_err; |
| 284 | int fdin[2], fdout[2], fderr[2]; |
Jeff King | 25043d8 | 2013-03-21 11:45:00 -0400 | [diff] [blame] | 285 | int failed_errno; |
Stephen Boyd | 939296c | 2013-01-30 18:01:05 -0800 | [diff] [blame] | 286 | char *str; |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 287 | |
Jeff King | c460c0e | 2014-05-15 04:33:26 -0400 | [diff] [blame] | 288 | if (!cmd->argv) |
| 289 | cmd->argv = cmd->args.argv; |
| 290 | |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 291 | /* |
| 292 | * In case of errors we must keep the promise to close FDs |
| 293 | * that have been passed in via ->in and ->out. |
| 294 | */ |
| 295 | |
Shawn O. Pearce | e4507ae | 2007-03-12 14:37:55 -0400 | [diff] [blame] | 296 | need_in = !cmd->no_stdin && cmd->in < 0; |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 297 | if (need_in) { |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 298 | if (pipe(fdin) < 0) { |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 299 | failed_errno = errno; |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 300 | if (cmd->out > 0) |
| 301 | close(cmd->out); |
Stephen Boyd | 939296c | 2013-01-30 18:01:05 -0800 | [diff] [blame] | 302 | str = "standard input"; |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 303 | goto fail_pipe; |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 304 | } |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 305 | cmd->in = fdin[1]; |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 306 | } |
| 307 | |
Shawn O. Pearce | e4507ae | 2007-03-12 14:37:55 -0400 | [diff] [blame] | 308 | need_out = !cmd->no_stdout |
| 309 | && !cmd->stdout_to_stderr |
| 310 | && cmd->out < 0; |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 311 | if (need_out) { |
| 312 | if (pipe(fdout) < 0) { |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 313 | failed_errno = errno; |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 314 | if (need_in) |
| 315 | close_pair(fdin); |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 316 | else if (cmd->in) |
| 317 | close(cmd->in); |
Stephen Boyd | 939296c | 2013-01-30 18:01:05 -0800 | [diff] [blame] | 318 | str = "standard output"; |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 319 | goto fail_pipe; |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 320 | } |
| 321 | cmd->out = fdout[0]; |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 322 | } |
| 323 | |
Shawn O. Pearce | b73a439 | 2007-11-11 02:29:37 -0500 | [diff] [blame] | 324 | need_err = !cmd->no_stderr && cmd->err < 0; |
Johannes Sixt | f3b33f1 | 2007-10-19 21:47:58 +0200 | [diff] [blame] | 325 | if (need_err) { |
| 326 | if (pipe(fderr) < 0) { |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 327 | failed_errno = errno; |
Johannes Sixt | f3b33f1 | 2007-10-19 21:47:58 +0200 | [diff] [blame] | 328 | if (need_in) |
| 329 | close_pair(fdin); |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 330 | else if (cmd->in) |
| 331 | close(cmd->in); |
Johannes Sixt | f3b33f1 | 2007-10-19 21:47:58 +0200 | [diff] [blame] | 332 | if (need_out) |
| 333 | close_pair(fdout); |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 334 | else if (cmd->out) |
| 335 | close(cmd->out); |
Stephen Boyd | 939296c | 2013-01-30 18:01:05 -0800 | [diff] [blame] | 336 | str = "standard error"; |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 337 | fail_pipe: |
Stephen Boyd | 939296c | 2013-01-30 18:01:05 -0800 | [diff] [blame] | 338 | error("cannot create %s pipe for %s: %s", |
| 339 | str, cmd->argv[0], strerror(failed_errno)); |
Jeff King | c460c0e | 2014-05-15 04:33:26 -0400 | [diff] [blame] | 340 | argv_array_clear(&cmd->args); |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 341 | errno = failed_errno; |
| 342 | return -1; |
Johannes Sixt | f3b33f1 | 2007-10-19 21:47:58 +0200 | [diff] [blame] | 343 | } |
| 344 | cmd->err = fderr[0]; |
| 345 | } |
| 346 | |
Johannes Schindelin | 8852f5d | 2008-07-07 14:41:34 +0100 | [diff] [blame] | 347 | trace_argv_printf(cmd->argv, "trace: run_command:"); |
Johannes Sixt | 13af8cb | 2011-02-04 09:41:58 +0100 | [diff] [blame] | 348 | fflush(NULL); |
Johannes Schindelin | 8852f5d | 2008-07-07 14:41:34 +0100 | [diff] [blame] | 349 | |
Jonathan Nieder | 380395d | 2013-05-02 20:26:08 +0100 | [diff] [blame] | 350 | #ifndef GIT_WINDOWS_NATIVE |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 351 | { |
| 352 | int notify_pipe[2]; |
| 353 | if (pipe(notify_pipe)) |
| 354 | notify_pipe[0] = notify_pipe[1] = -1; |
| 355 | |
Shawn O. Pearce | ebcb5d1 | 2007-03-10 03:28:05 -0500 | [diff] [blame] | 356 | cmd->pid = fork(); |
Jeff King | 25043d8 | 2013-03-21 11:45:00 -0400 | [diff] [blame] | 357 | failed_errno = errno; |
Shawn O. Pearce | ebcb5d1 | 2007-03-10 03:28:05 -0500 | [diff] [blame] | 358 | if (!cmd->pid) { |
Johannes Sixt | a5487dd | 2010-01-10 14:07:52 +0100 | [diff] [blame] | 359 | /* |
| 360 | * Redirect the channel to write syscall error messages to |
| 361 | * before redirecting the process's stderr so that all die() |
| 362 | * in subsequent call paths use the parent's stderr. |
| 363 | */ |
| 364 | if (cmd->no_stderr || need_err) { |
| 365 | child_err = dup(2); |
| 366 | set_cloexec(child_err); |
| 367 | } |
| 368 | set_die_routine(die_child); |
Clemens Buchacher | 3bc4181 | 2011-07-27 23:32:34 +0200 | [diff] [blame] | 369 | set_error_routine(error_child); |
Johannes Sixt | a5487dd | 2010-01-10 14:07:52 +0100 | [diff] [blame] | 370 | |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 371 | close(notify_pipe[0]); |
| 372 | set_cloexec(notify_pipe[1]); |
| 373 | child_notifier = notify_pipe[1]; |
| 374 | atexit(notify_parent); |
| 375 | |
Shawn O. Pearce | e4507ae | 2007-03-12 14:37:55 -0400 | [diff] [blame] | 376 | if (cmd->no_stdin) |
| 377 | dup_devnull(0); |
| 378 | else if (need_in) { |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 379 | dup2(fdin[0], 0); |
Shawn O. Pearce | 9dc09c7 | 2007-03-12 14:37:28 -0400 | [diff] [blame] | 380 | close_pair(fdin); |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 381 | } else if (cmd->in) { |
| 382 | dup2(cmd->in, 0); |
| 383 | close(cmd->in); |
Shawn O. Pearce | 95d3c4f | 2006-12-30 21:55:22 -0500 | [diff] [blame] | 384 | } |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 385 | |
Christian Couder | ce2cf27 | 2008-03-05 08:35:16 +0100 | [diff] [blame] | 386 | if (cmd->no_stderr) |
| 387 | dup_devnull(2); |
| 388 | else if (need_err) { |
| 389 | dup2(fderr[1], 2); |
| 390 | close_pair(fderr); |
Shawn O. Pearce | 4f41b61 | 2010-02-05 12:57:37 -0800 | [diff] [blame] | 391 | } else if (cmd->err > 1) { |
| 392 | dup2(cmd->err, 2); |
| 393 | close(cmd->err); |
Christian Couder | ce2cf27 | 2008-03-05 08:35:16 +0100 | [diff] [blame] | 394 | } |
| 395 | |
Shawn O. Pearce | e4507ae | 2007-03-12 14:37:55 -0400 | [diff] [blame] | 396 | if (cmd->no_stdout) |
| 397 | dup_devnull(1); |
| 398 | else if (cmd->stdout_to_stderr) |
Shawn O. Pearce | cd83c74 | 2006-12-30 21:55:19 -0500 | [diff] [blame] | 399 | dup2(2, 1); |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 400 | else if (need_out) { |
| 401 | dup2(fdout[1], 1); |
| 402 | close_pair(fdout); |
| 403 | } else if (cmd->out > 1) { |
| 404 | dup2(cmd->out, 1); |
| 405 | close(cmd->out); |
| 406 | } |
| 407 | |
Alex Riesen | 1568fea | 2007-05-22 23:48:23 +0200 | [diff] [blame] | 408 | if (cmd->dir && chdir(cmd->dir)) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 409 | die_errno("exec '%s': cd to '%s' failed", cmd->argv[0], |
| 410 | cmd->dir); |
Alex Riesen | ee49314 | 2007-05-22 23:48:47 +0200 | [diff] [blame] | 411 | if (cmd->env) { |
Alex Riesen | 3427b37 | 2007-05-23 22:21:39 +0200 | [diff] [blame] | 412 | for (; *cmd->env; cmd->env++) { |
| 413 | if (strchr(*cmd->env, '=')) |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 414 | putenv((char *)*cmd->env); |
Alex Riesen | 3427b37 | 2007-05-23 22:21:39 +0200 | [diff] [blame] | 415 | else |
| 416 | unsetenv(*cmd->env); |
| 417 | } |
Alex Riesen | ee49314 | 2007-05-22 23:48:47 +0200 | [diff] [blame] | 418 | } |
Felipe Contreras | 5a50085 | 2013-10-31 03:25:45 -0600 | [diff] [blame] | 419 | if (cmd->git_cmd) |
Shawn O. Pearce | f100089 | 2007-03-10 03:28:00 -0500 | [diff] [blame] | 420 | execv_git_cmd(cmd->argv); |
Felipe Contreras | 5a50085 | 2013-10-31 03:25:45 -0600 | [diff] [blame] | 421 | else if (cmd->use_shell) |
Jeff King | 8dba1e6 | 2009-12-30 05:53:16 -0500 | [diff] [blame] | 422 | execv_shell_cmd(cmd->argv); |
Felipe Contreras | 5a50085 | 2013-10-31 03:25:45 -0600 | [diff] [blame] | 423 | else |
Jeff King | 38f865c | 2012-03-30 03:52:18 -0400 | [diff] [blame] | 424 | sane_execvp(cmd->argv[0], (char *const*) cmd->argv); |
Clemens Buchacher | fc1b56f | 2011-08-01 19:59:21 +0200 | [diff] [blame] | 425 | if (errno == ENOENT) { |
| 426 | if (!cmd->silent_exec_failure) |
| 427 | error("cannot run %s: %s", cmd->argv[0], |
| 428 | strerror(ENOENT)); |
Johannes Sixt | a5487dd | 2010-01-10 14:07:52 +0100 | [diff] [blame] | 429 | exit(127); |
Clemens Buchacher | fc1b56f | 2011-08-01 19:59:21 +0200 | [diff] [blame] | 430 | } else { |
Johannes Sixt | a5487dd | 2010-01-10 14:07:52 +0100 | [diff] [blame] | 431 | die_errno("cannot exec '%s'", cmd->argv[0]); |
Clemens Buchacher | fc1b56f | 2011-08-01 19:59:21 +0200 | [diff] [blame] | 432 | } |
Josef Weidendorfer | b1bf95b | 2005-07-31 21:17:43 +0200 | [diff] [blame] | 433 | } |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 434 | if (cmd->pid < 0) |
| 435 | error("cannot fork() for %s: %s", cmd->argv[0], |
Jeff King | 25043d8 | 2013-03-21 11:45:00 -0400 | [diff] [blame] | 436 | strerror(errno)); |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 437 | else if (cmd->clean_on_exit) |
| 438 | mark_child_for_cleanup(cmd->pid); |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 439 | |
| 440 | /* |
| 441 | * Wait for child's execvp. If the execvp succeeds (or if fork() |
| 442 | * failed), EOF is seen immediately by the parent. Otherwise, the |
| 443 | * child process sends a single byte. |
| 444 | * Note that use of this infrastructure is completely advisory, |
| 445 | * therefore, we keep error checks minimal. |
| 446 | */ |
| 447 | close(notify_pipe[1]); |
| 448 | if (read(notify_pipe[0], ¬ify_pipe[1], 1) == 1) { |
| 449 | /* |
| 450 | * At this point we know that fork() succeeded, but execvp() |
| 451 | * failed. Errors have been reported to our stderr. |
| 452 | */ |
Jeff King | 1327452 | 2012-11-30 17:40:50 -0500 | [diff] [blame] | 453 | wait_or_whine(cmd->pid, cmd->argv[0]); |
Johannes Sixt | 2b541bf | 2010-01-10 14:11:22 +0100 | [diff] [blame] | 454 | failed_errno = errno; |
| 455 | cmd->pid = -1; |
| 456 | } |
| 457 | close(notify_pipe[0]); |
| 458 | } |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 459 | #else |
Frank Li | 0d30ad7 | 2009-09-16 10:20:17 +0200 | [diff] [blame] | 460 | { |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 461 | int fhin = 0, fhout = 1, fherr = 2; |
Steffen Prohaska | 108ac31 | 2008-07-28 07:50:28 +0200 | [diff] [blame] | 462 | const char **sargv = cmd->argv; |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 463 | |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 464 | if (cmd->no_stdin) |
| 465 | fhin = open("/dev/null", O_RDWR); |
| 466 | else if (need_in) |
| 467 | fhin = dup(fdin[0]); |
| 468 | else if (cmd->in) |
| 469 | fhin = dup(cmd->in); |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 470 | |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 471 | if (cmd->no_stderr) |
| 472 | fherr = open("/dev/null", O_RDWR); |
| 473 | else if (need_err) |
| 474 | fherr = dup(fderr[1]); |
Junio C Hamano | 76d44c8 | 2010-02-05 21:08:53 -0800 | [diff] [blame] | 475 | else if (cmd->err > 2) |
| 476 | fherr = dup(cmd->err); |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 477 | |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 478 | if (cmd->no_stdout) |
| 479 | fhout = open("/dev/null", O_RDWR); |
| 480 | else if (cmd->stdout_to_stderr) |
| 481 | fhout = dup(fherr); |
| 482 | else if (need_out) |
| 483 | fhout = dup(fdout[1]); |
| 484 | else if (cmd->out > 1) |
| 485 | fhout = dup(cmd->out); |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 486 | |
Felipe Contreras | 5a50085 | 2013-10-31 03:25:45 -0600 | [diff] [blame] | 487 | if (cmd->git_cmd) |
Steffen Prohaska | 108ac31 | 2008-07-28 07:50:28 +0200 | [diff] [blame] | 488 | cmd->argv = prepare_git_cmd(cmd->argv); |
Felipe Contreras | 5a50085 | 2013-10-31 03:25:45 -0600 | [diff] [blame] | 489 | else if (cmd->use_shell) |
Jeff King | 8dba1e6 | 2009-12-30 05:53:16 -0500 | [diff] [blame] | 490 | cmd->argv = prepare_shell_cmd(cmd->argv); |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 491 | |
Karsten Blees | 77734da | 2014-07-17 17:38:01 +0200 | [diff] [blame] | 492 | cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env, |
| 493 | cmd->dir, fhin, fhout, fherr); |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 494 | failed_errno = errno; |
Johannes Sixt | c024beb | 2009-07-04 21:26:42 +0200 | [diff] [blame] | 495 | if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT)) |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 496 | error("cannot spawn %s: %s", cmd->argv[0], strerror(errno)); |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 497 | if (cmd->clean_on_exit && cmd->pid >= 0) |
| 498 | mark_child_for_cleanup(cmd->pid); |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 499 | |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 500 | if (cmd->git_cmd) |
Steffen Prohaska | 108ac31 | 2008-07-28 07:50:28 +0200 | [diff] [blame] | 501 | free(cmd->argv); |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 502 | |
Steffen Prohaska | 108ac31 | 2008-07-28 07:50:28 +0200 | [diff] [blame] | 503 | cmd->argv = sargv; |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 504 | if (fhin != 0) |
| 505 | close(fhin); |
| 506 | if (fhout != 1) |
| 507 | close(fhout); |
| 508 | if (fherr != 2) |
| 509 | close(fherr); |
Frank Li | 0d30ad7 | 2009-09-16 10:20:17 +0200 | [diff] [blame] | 510 | } |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 511 | #endif |
| 512 | |
| 513 | if (cmd->pid < 0) { |
| 514 | if (need_in) |
| 515 | close_pair(fdin); |
| 516 | else if (cmd->in) |
| 517 | close(cmd->in); |
| 518 | if (need_out) |
| 519 | close_pair(fdout); |
| 520 | else if (cmd->out) |
| 521 | close(cmd->out); |
| 522 | if (need_err) |
| 523 | close_pair(fderr); |
bert Dvornik | fc012c2 | 2010-05-20 20:57:52 +0200 | [diff] [blame] | 524 | else if (cmd->err) |
| 525 | close(cmd->err); |
Jeff King | c460c0e | 2014-05-15 04:33:26 -0400 | [diff] [blame] | 526 | argv_array_clear(&cmd->args); |
Johannes Sixt | 0ac77ec | 2009-07-04 21:26:40 +0200 | [diff] [blame] | 527 | errno = failed_errno; |
| 528 | return -1; |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 529 | } |
Shawn O. Pearce | 4919bf0 | 2007-03-10 03:28:08 -0500 | [diff] [blame] | 530 | |
| 531 | if (need_in) |
| 532 | close(fdin[0]); |
| 533 | else if (cmd->in) |
| 534 | close(cmd->in); |
| 535 | |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 536 | if (need_out) |
| 537 | close(fdout[1]); |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 538 | else if (cmd->out) |
Shawn O. Pearce | f4bba25 | 2007-03-12 14:37:45 -0400 | [diff] [blame] | 539 | close(cmd->out); |
| 540 | |
Johannes Sixt | f3b33f1 | 2007-10-19 21:47:58 +0200 | [diff] [blame] | 541 | if (need_err) |
| 542 | close(fderr[1]); |
Shawn O. Pearce | 4f41b61 | 2010-02-05 12:57:37 -0800 | [diff] [blame] | 543 | else if (cmd->err) |
| 544 | close(cmd->err); |
Johannes Sixt | f3b33f1 | 2007-10-19 21:47:58 +0200 | [diff] [blame] | 545 | |
Shawn O. Pearce | ebcb5d1 | 2007-03-10 03:28:05 -0500 | [diff] [blame] | 546 | return 0; |
| 547 | } |
| 548 | |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 549 | int finish_command(struct child_process *cmd) |
| 550 | { |
Jeff King | c460c0e | 2014-05-15 04:33:26 -0400 | [diff] [blame] | 551 | int ret = wait_or_whine(cmd->pid, cmd->argv[0]); |
| 552 | argv_array_clear(&cmd->args); |
| 553 | return ret; |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Shawn O. Pearce | ebcb5d1 | 2007-03-10 03:28:05 -0500 | [diff] [blame] | 556 | int run_command(struct child_process *cmd) |
| 557 | { |
| 558 | int code = start_command(cmd); |
| 559 | if (code) |
| 560 | return code; |
| 561 | return finish_command(cmd); |
| 562 | } |
| 563 | |
Shawn O. Pearce | f100089 | 2007-03-10 03:28:00 -0500 | [diff] [blame] | 564 | int run_command_v_opt(const char **argv, int opt) |
| 565 | { |
René Scharfe | 41e9bad | 2014-08-19 21:11:00 +0200 | [diff] [blame] | 566 | return run_command_v_opt_cd_env(argv, opt, NULL, NULL); |
Alex Riesen | 1568fea | 2007-05-22 23:48:23 +0200 | [diff] [blame] | 567 | } |
| 568 | |
Alex Riesen | ee49314 | 2007-05-22 23:48:47 +0200 | [diff] [blame] | 569 | int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env) |
| 570 | { |
René Scharfe | 1f87293 | 2014-08-19 21:11:43 +0200 | [diff] [blame] | 571 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 572 | cmd.argv = argv; |
| 573 | cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0; |
| 574 | cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0; |
| 575 | cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0; |
| 576 | cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0; |
| 577 | cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0; |
| 578 | cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0; |
Alex Riesen | ee49314 | 2007-05-22 23:48:47 +0200 | [diff] [blame] | 579 | cmd.dir = dir; |
| 580 | cmd.env = env; |
| 581 | return run_command(&cmd); |
| 582 | } |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 583 | |
Johannes Sixt | f6b6098 | 2010-03-09 21:00:36 +0100 | [diff] [blame] | 584 | #ifndef NO_PTHREADS |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 585 | static pthread_t main_thread; |
| 586 | static int main_thread_set; |
| 587 | static pthread_key_t async_key; |
Jeff King | 1ece66b | 2013-04-16 15:50:07 -0400 | [diff] [blame] | 588 | static pthread_key_t async_die_counter; |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 589 | |
Johannes Sixt | 200a76b | 2010-03-06 16:40:42 +0100 | [diff] [blame] | 590 | static void *run_thread(void *data) |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 591 | { |
| 592 | struct async *async = data; |
Johannes Sixt | f6b6098 | 2010-03-09 21:00:36 +0100 | [diff] [blame] | 593 | intptr_t ret; |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 594 | |
| 595 | pthread_setspecific(async_key, async); |
Johannes Sixt | f6b6098 | 2010-03-09 21:00:36 +0100 | [diff] [blame] | 596 | ret = async->proc(async->proc_in, async->proc_out, async->data); |
Johannes Sixt | 200a76b | 2010-03-06 16:40:42 +0100 | [diff] [blame] | 597 | return (void *)ret; |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 598 | } |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 599 | |
| 600 | static NORETURN void die_async(const char *err, va_list params) |
| 601 | { |
| 602 | vreportf("fatal: ", err, params); |
| 603 | |
| 604 | if (!pthread_equal(main_thread, pthread_self())) { |
| 605 | struct async *async = pthread_getspecific(async_key); |
| 606 | if (async->proc_in >= 0) |
| 607 | close(async->proc_in); |
| 608 | if (async->proc_out >= 0) |
| 609 | close(async->proc_out); |
| 610 | pthread_exit((void *)128); |
| 611 | } |
| 612 | |
| 613 | exit(128); |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 614 | } |
Jeff King | 1ece66b | 2013-04-16 15:50:07 -0400 | [diff] [blame] | 615 | |
| 616 | static int async_die_is_recursing(void) |
| 617 | { |
| 618 | void *ret = pthread_getspecific(async_die_counter); |
| 619 | pthread_setspecific(async_die_counter, (void *)1); |
| 620 | return ret != NULL; |
| 621 | } |
| 622 | |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 623 | #endif |
| 624 | |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 625 | int start_async(struct async *async) |
| 626 | { |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 627 | int need_in, need_out; |
| 628 | int fdin[2], fdout[2]; |
| 629 | int proc_in, proc_out; |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 630 | |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 631 | need_in = async->in < 0; |
| 632 | if (need_in) { |
| 633 | if (pipe(fdin) < 0) { |
| 634 | if (async->out > 0) |
| 635 | close(async->out); |
| 636 | return error("cannot create pipe: %s", strerror(errno)); |
| 637 | } |
| 638 | async->in = fdin[1]; |
| 639 | } |
| 640 | |
| 641 | need_out = async->out < 0; |
| 642 | if (need_out) { |
| 643 | if (pipe(fdout) < 0) { |
| 644 | if (need_in) |
| 645 | close_pair(fdin); |
| 646 | else if (async->in) |
| 647 | close(async->in); |
| 648 | return error("cannot create pipe: %s", strerror(errno)); |
| 649 | } |
| 650 | async->out = fdout[0]; |
| 651 | } |
| 652 | |
| 653 | if (need_in) |
| 654 | proc_in = fdin[0]; |
| 655 | else if (async->in) |
| 656 | proc_in = async->in; |
| 657 | else |
| 658 | proc_in = -1; |
| 659 | |
| 660 | if (need_out) |
| 661 | proc_out = fdout[1]; |
| 662 | else if (async->out) |
| 663 | proc_out = async->out; |
| 664 | else |
| 665 | proc_out = -1; |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 666 | |
Johannes Sixt | f6b6098 | 2010-03-09 21:00:36 +0100 | [diff] [blame] | 667 | #ifdef NO_PTHREADS |
Anders Melchiorsen | 2c3766f | 2008-08-04 02:30:03 +0200 | [diff] [blame] | 668 | /* Flush stdio before fork() to avoid cloning buffers */ |
| 669 | fflush(NULL); |
| 670 | |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 671 | async->pid = fork(); |
| 672 | if (async->pid < 0) { |
| 673 | error("fork (async) failed: %s", strerror(errno)); |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 674 | goto error; |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 675 | } |
| 676 | if (!async->pid) { |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 677 | if (need_in) |
| 678 | close(fdin[1]); |
| 679 | if (need_out) |
| 680 | close(fdout[0]); |
| 681 | exit(!!async->proc(proc_in, proc_out, async->data)); |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 682 | } |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 683 | |
Jeff King | afe19ff | 2012-01-07 12:42:43 +0100 | [diff] [blame] | 684 | mark_child_for_cleanup(async->pid); |
| 685 | |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 686 | if (need_in) |
| 687 | close(fdin[0]); |
| 688 | else if (async->in) |
| 689 | close(async->in); |
| 690 | |
| 691 | if (need_out) |
| 692 | close(fdout[1]); |
| 693 | else if (async->out) |
| 694 | close(async->out); |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 695 | #else |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 696 | if (!main_thread_set) { |
| 697 | /* |
| 698 | * We assume that the first time that start_async is called |
| 699 | * it is from the main thread. |
| 700 | */ |
| 701 | main_thread_set = 1; |
| 702 | main_thread = pthread_self(); |
| 703 | pthread_key_create(&async_key, NULL); |
Jeff King | 1ece66b | 2013-04-16 15:50:07 -0400 | [diff] [blame] | 704 | pthread_key_create(&async_die_counter, NULL); |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 705 | set_die_routine(die_async); |
Jeff King | 1ece66b | 2013-04-16 15:50:07 -0400 | [diff] [blame] | 706 | set_die_is_recursing_routine(async_die_is_recursing); |
Johannes Sixt | 0ea1c89 | 2010-03-06 16:40:43 +0100 | [diff] [blame] | 707 | } |
| 708 | |
Johannes Sixt | 200a76b | 2010-03-06 16:40:42 +0100 | [diff] [blame] | 709 | if (proc_in >= 0) |
| 710 | set_cloexec(proc_in); |
| 711 | if (proc_out >= 0) |
| 712 | set_cloexec(proc_out); |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 713 | async->proc_in = proc_in; |
| 714 | async->proc_out = proc_out; |
Johannes Sixt | 200a76b | 2010-03-06 16:40:42 +0100 | [diff] [blame] | 715 | { |
| 716 | int err = pthread_create(&async->tid, NULL, run_thread, async); |
| 717 | if (err) { |
| 718 | error("cannot create thread: %s", strerror(err)); |
| 719 | goto error; |
| 720 | } |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 721 | } |
| 722 | #endif |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 723 | return 0; |
Erik Faye-Lund | ae6a560 | 2010-02-05 12:57:38 -0800 | [diff] [blame] | 724 | |
| 725 | error: |
| 726 | if (need_in) |
| 727 | close_pair(fdin); |
| 728 | else if (async->in) |
| 729 | close(async->in); |
| 730 | |
| 731 | if (need_out) |
| 732 | close_pair(fdout); |
| 733 | else if (async->out) |
| 734 | close(async->out); |
| 735 | return -1; |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | int finish_async(struct async *async) |
| 739 | { |
Johannes Sixt | f6b6098 | 2010-03-09 21:00:36 +0100 | [diff] [blame] | 740 | #ifdef NO_PTHREADS |
Jeff King | 0398fc3 | 2013-01-05 09:52:29 -0500 | [diff] [blame] | 741 | return wait_or_whine(async->pid, "child process"); |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 742 | #else |
Johannes Sixt | 200a76b | 2010-03-06 16:40:42 +0100 | [diff] [blame] | 743 | void *ret = (void *)(intptr_t)(-1); |
| 744 | |
| 745 | if (pthread_join(async->tid, &ret)) |
| 746 | error("pthread_join failed"); |
| 747 | return (int)(intptr_t)ret; |
Johannes Sixt | 618ebe9 | 2007-12-08 22:19:14 +0100 | [diff] [blame] | 748 | #endif |
Johannes Sixt | 2d22c20 | 2007-10-19 21:48:00 +0200 | [diff] [blame] | 749 | } |
Stephan Beyer | ae98a00 | 2009-01-16 20:09:59 +0100 | [diff] [blame] | 750 | |
Aaron Schrab | 5a7da2d | 2013-01-13 00:17:02 -0500 | [diff] [blame] | 751 | char *find_hook(const char *name) |
| 752 | { |
| 753 | char *path = git_path("hooks/%s", name); |
| 754 | if (access(path, X_OK) < 0) |
| 755 | path = NULL; |
| 756 | |
| 757 | return path; |
| 758 | } |
| 759 | |
Benoit Pierre | 15048f8 | 2014-03-18 11:00:53 +0100 | [diff] [blame] | 760 | int run_hook_ve(const char *const *env, const char *name, va_list args) |
Stephan Beyer | ae98a00 | 2009-01-16 20:09:59 +0100 | [diff] [blame] | 761 | { |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 762 | struct child_process hook = CHILD_PROCESS_INIT; |
Benoit Pierre | 15048f8 | 2014-03-18 11:00:53 +0100 | [diff] [blame] | 763 | const char *p; |
Stephan Beyer | ae98a00 | 2009-01-16 20:09:59 +0100 | [diff] [blame] | 764 | |
Aaron Schrab | 5a7da2d | 2013-01-13 00:17:02 -0500 | [diff] [blame] | 765 | p = find_hook(name); |
| 766 | if (!p) |
Stephan Beyer | cf94ca8 | 2009-01-16 20:10:01 +0100 | [diff] [blame] | 767 | return 0; |
| 768 | |
René Scharfe | d1d0945 | 2014-07-16 23:57:47 +0200 | [diff] [blame] | 769 | argv_array_push(&hook.args, p); |
| 770 | while ((p = va_arg(args, const char *))) |
| 771 | argv_array_push(&hook.args, p); |
Benoit Pierre | 15048f8 | 2014-03-18 11:00:53 +0100 | [diff] [blame] | 772 | hook.env = env; |
Stephan Beyer | ae98a00 | 2009-01-16 20:09:59 +0100 | [diff] [blame] | 773 | hook.no_stdin = 1; |
| 774 | hook.stdout_to_stderr = 1; |
Stephan Beyer | ae98a00 | 2009-01-16 20:09:59 +0100 | [diff] [blame] | 775 | |
René Scharfe | d1d0945 | 2014-07-16 23:57:47 +0200 | [diff] [blame] | 776 | return run_command(&hook); |
Stephan Beyer | ae98a00 | 2009-01-16 20:09:59 +0100 | [diff] [blame] | 777 | } |
Benoit Pierre | 15048f8 | 2014-03-18 11:00:53 +0100 | [diff] [blame] | 778 | |
| 779 | int run_hook_le(const char *const *env, const char *name, ...) |
| 780 | { |
| 781 | va_list args; |
| 782 | int ret; |
| 783 | |
| 784 | va_start(args, name); |
| 785 | ret = run_hook_ve(env, name, args); |
| 786 | va_end(args); |
| 787 | |
| 788 | return ret; |
| 789 | } |
| 790 | |
| 791 | int run_hook_with_custom_index(const char *index_file, const char *name, ...) |
| 792 | { |
| 793 | const char *hook_env[3] = { NULL }; |
| 794 | char index[PATH_MAX]; |
| 795 | va_list args; |
| 796 | int ret; |
| 797 | |
| 798 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); |
| 799 | hook_env[0] = index; |
| 800 | |
| 801 | va_start(args, name); |
| 802 | ret = run_hook_ve(hook_env, name, args); |
| 803 | va_end(args); |
| 804 | |
| 805 | return ret; |
| 806 | } |