2 #include "run-command.h"
5 static inline void close_pair(int fd
[2])
11 int start_command(struct child_process
*cmd
)
13 int need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
18 return -ERR_RUN_COMMAND_PIPE
;
27 return -ERR_RUN_COMMAND_FORK
;
32 int fd
= open("/dev/null", O_RDWR
);
43 if (cmd
->stdout_to_stderr
)
46 execv_git_cmd(cmd
->argv
);
48 execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
50 die("exec %s failed.", cmd
->argv
[0]);
61 int finish_command(struct child_process
*cmd
)
68 pid_t waiting
= waitpid(cmd
->pid
, &status
, 0);
73 error("waitpid failed (%s)", strerror(errno
));
74 return -ERR_RUN_COMMAND_WAITPID
;
76 if (waiting
!= cmd
->pid
)
77 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID
;
78 if (WIFSIGNALED(status
))
79 return -ERR_RUN_COMMAND_WAITPID_SIGNAL
;
81 if (!WIFEXITED(status
))
82 return -ERR_RUN_COMMAND_WAITPID_NOEXIT
;
83 code
= WEXITSTATUS(status
);
90 int run_command(struct child_process
*cmd
)
92 int code
= start_command(cmd
);
95 return finish_command(cmd
);
98 int run_command_v_opt(const char **argv
, int opt
)
100 struct child_process cmd
;
101 memset(&cmd
, 0, sizeof(cmd
));
103 cmd
.no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
104 cmd
.git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
105 cmd
.stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
106 return run_command(&cmd
);