Place __stdcall between return value and function name
[git/dscho.git] / run-command.c
blob62bea191d492d7b429b88dbaa7d51a792c0a8a2d
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
5 static inline void close_pair(int fd[2])
7 close(fd[0]);
8 close(fd[1]);
11 static inline void dup_devnull(int to)
13 int fd = open("/dev/null", O_RDWR);
14 dup2(fd, to);
15 close(fd);
18 int start_command(struct child_process *cmd)
20 int need_in, need_out, need_err;
21 int fdin[2], fdout[2], fderr[2];
24 * In case of errors we must keep the promise to close FDs
25 * that have been passed in via ->in and ->out.
28 need_in = !cmd->no_stdin && cmd->in < 0;
29 if (need_in) {
30 if (pipe(fdin) < 0) {
31 if (cmd->out > 0)
32 close(cmd->out);
33 return -ERR_RUN_COMMAND_PIPE;
35 cmd->in = fdin[1];
38 need_out = !cmd->no_stdout
39 && !cmd->stdout_to_stderr
40 && cmd->out < 0;
41 if (need_out) {
42 if (pipe(fdout) < 0) {
43 if (need_in)
44 close_pair(fdin);
45 else if (cmd->in)
46 close(cmd->in);
47 return -ERR_RUN_COMMAND_PIPE;
49 cmd->out = fdout[0];
52 need_err = !cmd->no_stderr && cmd->err < 0;
53 if (need_err) {
54 if (pipe(fderr) < 0) {
55 if (need_in)
56 close_pair(fdin);
57 else if (cmd->in)
58 close(cmd->in);
59 if (need_out)
60 close_pair(fdout);
61 else if (cmd->out)
62 close(cmd->out);
63 return -ERR_RUN_COMMAND_PIPE;
65 cmd->err = fderr[0];
68 trace_argv_printf(cmd->argv, "trace: run_command:");
70 #ifndef WIN32
71 fflush(NULL);
72 cmd->pid = fork();
73 if (!cmd->pid) {
74 if (cmd->no_stdin)
75 dup_devnull(0);
76 else if (need_in) {
77 dup2(fdin[0], 0);
78 close_pair(fdin);
79 } else if (cmd->in) {
80 dup2(cmd->in, 0);
81 close(cmd->in);
84 if (cmd->no_stderr)
85 dup_devnull(2);
86 else if (need_err) {
87 dup2(fderr[1], 2);
88 close_pair(fderr);
91 if (cmd->no_stdout)
92 dup_devnull(1);
93 else if (cmd->stdout_to_stderr)
94 dup2(2, 1);
95 else if (need_out) {
96 dup2(fdout[1], 1);
97 close_pair(fdout);
98 } else if (cmd->out > 1) {
99 dup2(cmd->out, 1);
100 close(cmd->out);
103 if (cmd->dir && chdir(cmd->dir))
104 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
105 cmd->dir);
106 if (cmd->env) {
107 for (; *cmd->env; cmd->env++) {
108 if (strchr(*cmd->env, '='))
109 putenv((char *)*cmd->env);
110 else
111 unsetenv(*cmd->env);
114 if (cmd->preexec_cb)
115 cmd->preexec_cb();
116 if (cmd->git_cmd) {
117 execv_git_cmd(cmd->argv);
118 } else {
119 execvp(cmd->argv[0], (char *const*) cmd->argv);
121 trace_printf("trace: exec '%s' failed: %s\n", cmd->argv[0],
122 strerror(errno));
123 exit(127);
125 #else
127 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
128 const char **sargv = cmd->argv;
129 char **env = environ;
131 if (cmd->no_stdin) {
132 s0 = dup(0);
133 dup_devnull(0);
134 } else if (need_in) {
135 s0 = dup(0);
136 dup2(fdin[0], 0);
137 } else if (cmd->in) {
138 s0 = dup(0);
139 dup2(cmd->in, 0);
142 if (cmd->no_stderr) {
143 s2 = dup(2);
144 dup_devnull(2);
145 } else if (need_err) {
146 s2 = dup(2);
147 dup2(fderr[1], 2);
150 if (cmd->no_stdout) {
151 s1 = dup(1);
152 dup_devnull(1);
153 } else if (cmd->stdout_to_stderr) {
154 s1 = dup(1);
155 dup2(2, 1);
156 } else if (need_out) {
157 s1 = dup(1);
158 dup2(fdout[1], 1);
159 } else if (cmd->out > 1) {
160 s1 = dup(1);
161 dup2(cmd->out, 1);
164 if (cmd->dir)
165 die("chdir in start_command() not implemented");
166 if (cmd->env) {
167 env = copy_environ();
168 for (; *cmd->env; cmd->env++)
169 env = env_setenv(env, *cmd->env);
172 if (cmd->git_cmd) {
173 cmd->argv = prepare_git_cmd(cmd->argv);
176 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
178 if (cmd->env)
179 free_environ(env);
180 if (cmd->git_cmd)
181 free(cmd->argv);
183 cmd->argv = sargv;
184 if (s0 >= 0)
185 dup2(s0, 0), close(s0);
186 if (s1 >= 0)
187 dup2(s1, 1), close(s1);
188 if (s2 >= 0)
189 dup2(s2, 2), close(s2);
191 #endif
193 if (cmd->pid < 0) {
194 int err = errno;
195 if (need_in)
196 close_pair(fdin);
197 else if (cmd->in)
198 close(cmd->in);
199 if (need_out)
200 close_pair(fdout);
201 else if (cmd->out)
202 close(cmd->out);
203 if (need_err)
204 close_pair(fderr);
205 return err == ENOENT ?
206 -ERR_RUN_COMMAND_EXEC :
207 -ERR_RUN_COMMAND_FORK;
210 if (need_in)
211 close(fdin[0]);
212 else if (cmd->in)
213 close(cmd->in);
215 if (need_out)
216 close(fdout[1]);
217 else if (cmd->out)
218 close(cmd->out);
220 if (need_err)
221 close(fderr[1]);
223 return 0;
226 static int wait_or_whine(pid_t pid)
228 for (;;) {
229 int status, code;
230 pid_t waiting = waitpid(pid, &status, 0);
232 if (waiting < 0) {
233 if (errno == EINTR)
234 continue;
235 error("waitpid failed (%s)", strerror(errno));
236 return -ERR_RUN_COMMAND_WAITPID;
238 if (waiting != pid)
239 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
240 if (WIFSIGNALED(status))
241 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
243 if (!WIFEXITED(status))
244 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
245 code = WEXITSTATUS(status);
246 switch (code) {
247 case 127:
248 return -ERR_RUN_COMMAND_EXEC;
249 case 0:
250 return 0;
251 default:
252 return -code;
257 int finish_command(struct child_process *cmd)
259 return wait_or_whine(cmd->pid);
262 int run_command(struct child_process *cmd)
264 int code = start_command(cmd);
265 if (code)
266 return code;
267 return finish_command(cmd);
270 static void prepare_run_command_v_opt(struct child_process *cmd,
271 const char **argv,
272 int opt)
274 memset(cmd, 0, sizeof(*cmd));
275 cmd->argv = argv;
276 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
277 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
278 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
281 int run_command_v_opt(const char **argv, int opt)
283 struct child_process cmd;
284 prepare_run_command_v_opt(&cmd, argv, opt);
285 return run_command(&cmd);
288 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
290 struct child_process cmd;
291 prepare_run_command_v_opt(&cmd, argv, opt);
292 cmd.dir = dir;
293 cmd.env = env;
294 return run_command(&cmd);
297 #ifdef WIN32
298 static unsigned __stdcall run_thread(void *data)
300 struct async *async = data;
301 return async->proc(async->fd_for_proc, async->data);
303 #endif
305 int start_async(struct async *async)
307 int pipe_out[2];
309 if (pipe(pipe_out) < 0)
310 return error("cannot create pipe: %s", strerror(errno));
311 async->out = pipe_out[0];
313 #ifndef WIN32
314 /* Flush stdio before fork() to avoid cloning buffers */
315 fflush(NULL);
317 async->pid = fork();
318 if (async->pid < 0) {
319 error("fork (async) failed: %s", strerror(errno));
320 close_pair(pipe_out);
321 return -1;
323 if (!async->pid) {
324 close(pipe_out[0]);
325 exit(!!async->proc(pipe_out[1], async->data));
327 close(pipe_out[1]);
328 #else
329 async->fd_for_proc = pipe_out[1];
330 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
331 if (!async->tid) {
332 error("cannot create thread: %s", strerror(errno));
333 close_pair(pipe_out);
334 return -1;
336 #endif
337 return 0;
340 int finish_async(struct async *async)
342 #ifndef WIN32
343 int ret = 0;
345 if (wait_or_whine(async->pid))
346 ret = error("waitpid (async) failed");
347 #else
348 DWORD ret = 0;
349 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
350 ret = error("waiting for thread failed: %lu", GetLastError());
351 else if (!GetExitCodeThread(async->tid, &ret))
352 ret = error("cannot get thread exit code: %lu", GetLastError());
353 CloseHandle(async->tid);
354 #endif
355 return ret;
358 int run_hook(const char *index_file, const char *name, ...)
360 struct child_process hook;
361 const char **argv = NULL, *env[2];
362 char index[PATH_MAX];
363 va_list args;
364 int ret;
365 size_t i = 0, alloc = 0;
367 if (access(git_path("hooks/%s", name), X_OK) < 0)
368 return 0;
370 va_start(args, name);
371 ALLOC_GROW(argv, i + 1, alloc);
372 argv[i++] = git_path("hooks/%s", name);
373 while (argv[i-1]) {
374 ALLOC_GROW(argv, i + 1, alloc);
375 argv[i++] = va_arg(args, const char *);
377 va_end(args);
379 memset(&hook, 0, sizeof(hook));
380 hook.argv = argv;
381 hook.no_stdin = 1;
382 hook.stdout_to_stderr = 1;
383 if (index_file) {
384 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
385 env[0] = index;
386 env[1] = NULL;
387 hook.env = env;
390 ret = start_command(&hook);
391 free(argv);
392 if (ret) {
393 warning("Could not spawn %s", argv[0]);
394 return ret;
396 ret = finish_command(&hook);
397 if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
398 warning("%s exited due to uncaught signal", argv[0]);
400 return ret;