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