Code

run_command: report system call errors instead of returning error codes
[git.git] / run-command.c
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
5 static inline void close_pair(int fd[2])
6 {
7         close(fd[0]);
8         close(fd[1]);
9 }
11 static inline void dup_devnull(int to)
12 {
13         int fd = open("/dev/null", O_RDWR);
14         dup2(fd, to);
15         close(fd);
16 }
18 int start_command(struct child_process *cmd)
19 {
20         int need_in, need_out, need_err;
21         int fdin[2], fdout[2], fderr[2];
22         int failed_errno;
24         /*
25          * In case of errors we must keep the promise to close FDs
26          * that have been passed in via ->in and ->out.
27          */
29         need_in = !cmd->no_stdin && cmd->in < 0;
30         if (need_in) {
31                 if (pipe(fdin) < 0) {
32                         failed_errno = errno;
33                         if (cmd->out > 0)
34                                 close(cmd->out);
35                         goto fail_pipe;
36                 }
37                 cmd->in = fdin[1];
38         }
40         need_out = !cmd->no_stdout
41                 && !cmd->stdout_to_stderr
42                 && cmd->out < 0;
43         if (need_out) {
44                 if (pipe(fdout) < 0) {
45                         failed_errno = errno;
46                         if (need_in)
47                                 close_pair(fdin);
48                         else if (cmd->in)
49                                 close(cmd->in);
50                         goto fail_pipe;
51                 }
52                 cmd->out = fdout[0];
53         }
55         need_err = !cmd->no_stderr && cmd->err < 0;
56         if (need_err) {
57                 if (pipe(fderr) < 0) {
58                         failed_errno = errno;
59                         if (need_in)
60                                 close_pair(fdin);
61                         else if (cmd->in)
62                                 close(cmd->in);
63                         if (need_out)
64                                 close_pair(fdout);
65                         else if (cmd->out)
66                                 close(cmd->out);
67 fail_pipe:
68                         error("cannot create pipe for %s: %s",
69                                 cmd->argv[0], strerror(failed_errno));
70                         errno = failed_errno;
71                         return -1;
72                 }
73                 cmd->err = fderr[0];
74         }
76         trace_argv_printf(cmd->argv, "trace: run_command:");
78 #ifndef __MINGW32__
79         fflush(NULL);
80         cmd->pid = fork();
81         if (!cmd->pid) {
82                 if (cmd->no_stdin)
83                         dup_devnull(0);
84                 else if (need_in) {
85                         dup2(fdin[0], 0);
86                         close_pair(fdin);
87                 } else if (cmd->in) {
88                         dup2(cmd->in, 0);
89                         close(cmd->in);
90                 }
92                 if (cmd->no_stderr)
93                         dup_devnull(2);
94                 else if (need_err) {
95                         dup2(fderr[1], 2);
96                         close_pair(fderr);
97                 }
99                 if (cmd->no_stdout)
100                         dup_devnull(1);
101                 else if (cmd->stdout_to_stderr)
102                         dup2(2, 1);
103                 else if (need_out) {
104                         dup2(fdout[1], 1);
105                         close_pair(fdout);
106                 } else if (cmd->out > 1) {
107                         dup2(cmd->out, 1);
108                         close(cmd->out);
109                 }
111                 if (cmd->dir && chdir(cmd->dir))
112                         die("exec %s: cd to %s failed (%s)", cmd->argv[0],
113                             cmd->dir, strerror(errno));
114                 if (cmd->env) {
115                         for (; *cmd->env; cmd->env++) {
116                                 if (strchr(*cmd->env, '='))
117                                         putenv((char *)*cmd->env);
118                                 else
119                                         unsetenv(*cmd->env);
120                         }
121                 }
122                 if (cmd->preexec_cb)
123                         cmd->preexec_cb();
124                 if (cmd->git_cmd) {
125                         execv_git_cmd(cmd->argv);
126                 } else {
127                         execvp(cmd->argv[0], (char *const*) cmd->argv);
128                 }
129                 trace_printf("trace: exec '%s' failed: %s\n", cmd->argv[0],
130                                 strerror(errno));
131                 exit(127);
132         }
133         if (cmd->pid < 0)
134                 error("cannot fork() for %s: %s", cmd->argv[0],
135                         strerror(failed_errno = errno));
136 #else
137         int s0 = -1, s1 = -1, s2 = -1;  /* backups of stdin, stdout, stderr */
138         const char **sargv = cmd->argv;
139         char **env = environ;
141         if (cmd->no_stdin) {
142                 s0 = dup(0);
143                 dup_devnull(0);
144         } else if (need_in) {
145                 s0 = dup(0);
146                 dup2(fdin[0], 0);
147         } else if (cmd->in) {
148                 s0 = dup(0);
149                 dup2(cmd->in, 0);
150         }
152         if (cmd->no_stderr) {
153                 s2 = dup(2);
154                 dup_devnull(2);
155         } else if (need_err) {
156                 s2 = dup(2);
157                 dup2(fderr[1], 2);
158         }
160         if (cmd->no_stdout) {
161                 s1 = dup(1);
162                 dup_devnull(1);
163         } else if (cmd->stdout_to_stderr) {
164                 s1 = dup(1);
165                 dup2(2, 1);
166         } else if (need_out) {
167                 s1 = dup(1);
168                 dup2(fdout[1], 1);
169         } else if (cmd->out > 1) {
170                 s1 = dup(1);
171                 dup2(cmd->out, 1);
172         }
174         if (cmd->dir)
175                 die("chdir in start_command() not implemented");
176         if (cmd->env) {
177                 env = copy_environ();
178                 for (; *cmd->env; cmd->env++)
179                         env = env_setenv(env, *cmd->env);
180         }
182         if (cmd->git_cmd) {
183                 cmd->argv = prepare_git_cmd(cmd->argv);
184         }
186         cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
187         failed_errno = errno;
188         if (cmd->pid < 0 && errno != ENOENT)
189                 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
191         if (cmd->env)
192                 free_environ(env);
193         if (cmd->git_cmd)
194                 free(cmd->argv);
196         cmd->argv = sargv;
197         if (s0 >= 0)
198                 dup2(s0, 0), close(s0);
199         if (s1 >= 0)
200                 dup2(s1, 1), close(s1);
201         if (s2 >= 0)
202                 dup2(s2, 2), close(s2);
203 #endif
205         if (cmd->pid < 0) {
206                 if (need_in)
207                         close_pair(fdin);
208                 else if (cmd->in)
209                         close(cmd->in);
210                 if (need_out)
211                         close_pair(fdout);
212                 else if (cmd->out)
213                         close(cmd->out);
214                 if (need_err)
215                         close_pair(fderr);
216                 errno = failed_errno;
217                 return -1;
218         }
220         if (need_in)
221                 close(fdin[0]);
222         else if (cmd->in)
223                 close(cmd->in);
225         if (need_out)
226                 close(fdout[1]);
227         else if (cmd->out)
228                 close(cmd->out);
230         if (need_err)
231                 close(fderr[1]);
233         return 0;
236 static int wait_or_whine(pid_t pid, const char *argv0)
238         int status, code = -1;
239         pid_t waiting;
240         int failed_errno = 0;
242         while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
243                 ;       /* nothing */
245         if (waiting < 0) {
246                 failed_errno = errno;
247                 error("waitpid for %s failed: %s", argv0, strerror(errno));
248         } else if (waiting != pid) {
249                 error("waitpid is confused (%s)", argv0);
250         } else if (WIFSIGNALED(status)) {
251                 error("%s died of signal", argv0);
252         } else if (WIFEXITED(status)) {
253                 code = WEXITSTATUS(status);
254                 /*
255                  * Convert special exit code when execvp failed.
256                  */
257                 if (code == 127) {
258                         code = -1;
259                         failed_errno = ENOENT;
260                 }
261         } else {
262                 error("waitpid is confused (%s)", argv0);
263         }
264         errno = failed_errno;
265         return code;
268 int finish_command(struct child_process *cmd)
270         return wait_or_whine(cmd->pid, cmd->argv[0]);
273 int run_command(struct child_process *cmd)
275         int code = start_command(cmd);
276         if (code)
277                 return code;
278         return finish_command(cmd);
281 static void prepare_run_command_v_opt(struct child_process *cmd,
282                                       const char **argv,
283                                       int opt)
285         memset(cmd, 0, sizeof(*cmd));
286         cmd->argv = argv;
287         cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
288         cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
289         cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
292 int run_command_v_opt(const char **argv, int opt)
294         struct child_process cmd;
295         prepare_run_command_v_opt(&cmd, argv, opt);
296         return run_command(&cmd);
299 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
301         struct child_process cmd;
302         prepare_run_command_v_opt(&cmd, argv, opt);
303         cmd.dir = dir;
304         cmd.env = env;
305         return run_command(&cmd);
308 #ifdef __MINGW32__
309 static __stdcall unsigned run_thread(void *data)
311         struct async *async = data;
312         return async->proc(async->fd_for_proc, async->data);
314 #endif
316 int start_async(struct async *async)
318         int pipe_out[2];
320         if (pipe(pipe_out) < 0)
321                 return error("cannot create pipe: %s", strerror(errno));
322         async->out = pipe_out[0];
324 #ifndef __MINGW32__
325         /* Flush stdio before fork() to avoid cloning buffers */
326         fflush(NULL);
328         async->pid = fork();
329         if (async->pid < 0) {
330                 error("fork (async) failed: %s", strerror(errno));
331                 close_pair(pipe_out);
332                 return -1;
333         }
334         if (!async->pid) {
335                 close(pipe_out[0]);
336                 exit(!!async->proc(pipe_out[1], async->data));
337         }
338         close(pipe_out[1]);
339 #else
340         async->fd_for_proc = pipe_out[1];
341         async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
342         if (!async->tid) {
343                 error("cannot create thread: %s", strerror(errno));
344                 close_pair(pipe_out);
345                 return -1;
346         }
347 #endif
348         return 0;
351 int finish_async(struct async *async)
353 #ifndef __MINGW32__
354         int ret = wait_or_whine(async->pid, "child process");
355 #else
356         DWORD ret = 0;
357         if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
358                 ret = error("waiting for thread failed: %lu", GetLastError());
359         else if (!GetExitCodeThread(async->tid, &ret))
360                 ret = error("cannot get thread exit code: %lu", GetLastError());
361         CloseHandle(async->tid);
362 #endif
363         return ret;
366 int run_hook(const char *index_file, const char *name, ...)
368         struct child_process hook;
369         const char **argv = NULL, *env[2];
370         char index[PATH_MAX];
371         va_list args;
372         int ret;
373         size_t i = 0, alloc = 0;
375         if (access(git_path("hooks/%s", name), X_OK) < 0)
376                 return 0;
378         va_start(args, name);
379         ALLOC_GROW(argv, i + 1, alloc);
380         argv[i++] = git_path("hooks/%s", name);
381         while (argv[i-1]) {
382                 ALLOC_GROW(argv, i + 1, alloc);
383                 argv[i++] = va_arg(args, const char *);
384         }
385         va_end(args);
387         memset(&hook, 0, sizeof(hook));
388         hook.argv = argv;
389         hook.no_stdin = 1;
390         hook.stdout_to_stderr = 1;
391         if (index_file) {
392                 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
393                 env[0] = index;
394                 env[1] = NULL;
395                 hook.env = env;
396         }
398         ret = run_command(&hook);
399         free(argv);
400         return ret;