run-command.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "cache.h"
  2. #include "run-command.h"
  3. #include "exec_cmd.h"
  4. static inline void close_pair(int fd[2])
  5. {
  6. close(fd[0]);
  7. close(fd[1]);
  8. }
  9. static inline void dup_devnull(int to)
  10. {
  11. int fd = open("/dev/null", O_RDWR);
  12. dup2(fd, to);
  13. close(fd);
  14. }
  15. int start_command(struct child_process *cmd)
  16. {
  17. int need_in, need_out, need_err;
  18. int fdin[2], fdout[2], fderr[2];
  19. /*
  20. * In case of errors we must keep the promise to close FDs
  21. * that have been passed in via ->in and ->out.
  22. */
  23. need_in = !cmd->no_stdin && cmd->in < 0;
  24. if (need_in) {
  25. if (pipe(fdin) < 0) {
  26. if (cmd->out > 0)
  27. close(cmd->out);
  28. return -ERR_RUN_COMMAND_PIPE;
  29. }
  30. cmd->in = fdin[1];
  31. }
  32. need_out = !cmd->no_stdout
  33. && !cmd->stdout_to_stderr
  34. && cmd->out < 0;
  35. if (need_out) {
  36. if (pipe(fdout) < 0) {
  37. if (need_in)
  38. close_pair(fdin);
  39. else if (cmd->in)
  40. close(cmd->in);
  41. return -ERR_RUN_COMMAND_PIPE;
  42. }
  43. cmd->out = fdout[0];
  44. }
  45. need_err = !cmd->no_stderr && cmd->err < 0;
  46. if (need_err) {
  47. if (pipe(fderr) < 0) {
  48. if (need_in)
  49. close_pair(fdin);
  50. else if (cmd->in)
  51. close(cmd->in);
  52. if (need_out)
  53. close_pair(fdout);
  54. else if (cmd->out)
  55. close(cmd->out);
  56. return -ERR_RUN_COMMAND_PIPE;
  57. }
  58. cmd->err = fderr[0];
  59. }
  60. fflush(NULL);
  61. cmd->pid = fork();
  62. if (!cmd->pid) {
  63. if (cmd->no_stdin)
  64. dup_devnull(0);
  65. else if (need_in) {
  66. dup2(fdin[0], 0);
  67. close_pair(fdin);
  68. } else if (cmd->in) {
  69. dup2(cmd->in, 0);
  70. close(cmd->in);
  71. }
  72. if (cmd->no_stderr)
  73. dup_devnull(2);
  74. else if (need_err) {
  75. dup2(fderr[1], 2);
  76. close_pair(fderr);
  77. }
  78. if (cmd->no_stdout)
  79. dup_devnull(1);
  80. else if (cmd->stdout_to_stderr)
  81. dup2(2, 1);
  82. else if (need_out) {
  83. dup2(fdout[1], 1);
  84. close_pair(fdout);
  85. } else if (cmd->out > 1) {
  86. dup2(cmd->out, 1);
  87. close(cmd->out);
  88. }
  89. if (cmd->dir && chdir(cmd->dir))
  90. die("exec %s: cd to %s failed (%s)", cmd->argv[0],
  91. cmd->dir, strerror(errno));
  92. if (cmd->env) {
  93. for (; *cmd->env; cmd->env++) {
  94. if (strchr(*cmd->env, '='))
  95. putenv((char*)*cmd->env);
  96. else
  97. unsetenv(*cmd->env);
  98. }
  99. }
  100. if (cmd->preexec_cb)
  101. cmd->preexec_cb();
  102. if (cmd->perf_cmd) {
  103. execv_perf_cmd(cmd->argv);
  104. } else {
  105. execvp(cmd->argv[0], (char *const*) cmd->argv);
  106. }
  107. exit(127);
  108. }
  109. if (cmd->pid < 0) {
  110. int err = errno;
  111. if (need_in)
  112. close_pair(fdin);
  113. else if (cmd->in)
  114. close(cmd->in);
  115. if (need_out)
  116. close_pair(fdout);
  117. else if (cmd->out)
  118. close(cmd->out);
  119. if (need_err)
  120. close_pair(fderr);
  121. return err == ENOENT ?
  122. -ERR_RUN_COMMAND_EXEC :
  123. -ERR_RUN_COMMAND_FORK;
  124. }
  125. if (need_in)
  126. close(fdin[0]);
  127. else if (cmd->in)
  128. close(cmd->in);
  129. if (need_out)
  130. close(fdout[1]);
  131. else if (cmd->out)
  132. close(cmd->out);
  133. if (need_err)
  134. close(fderr[1]);
  135. return 0;
  136. }
  137. static int wait_or_whine(pid_t pid)
  138. {
  139. for (;;) {
  140. int status, code;
  141. pid_t waiting = waitpid(pid, &status, 0);
  142. if (waiting < 0) {
  143. if (errno == EINTR)
  144. continue;
  145. error("waitpid failed (%s)", strerror(errno));
  146. return -ERR_RUN_COMMAND_WAITPID;
  147. }
  148. if (waiting != pid)
  149. return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
  150. if (WIFSIGNALED(status))
  151. return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
  152. if (!WIFEXITED(status))
  153. return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
  154. code = WEXITSTATUS(status);
  155. switch (code) {
  156. case 127:
  157. return -ERR_RUN_COMMAND_EXEC;
  158. case 0:
  159. return 0;
  160. default:
  161. return -code;
  162. }
  163. }
  164. }
  165. int finish_command(struct child_process *cmd)
  166. {
  167. return wait_or_whine(cmd->pid);
  168. }
  169. int run_command(struct child_process *cmd)
  170. {
  171. int code = start_command(cmd);
  172. if (code)
  173. return code;
  174. return finish_command(cmd);
  175. }
  176. static void prepare_run_command_v_opt(struct child_process *cmd,
  177. const char **argv,
  178. int opt)
  179. {
  180. memset(cmd, 0, sizeof(*cmd));
  181. cmd->argv = argv;
  182. cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
  183. cmd->perf_cmd = opt & RUN_PERF_CMD ? 1 : 0;
  184. cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
  185. }
  186. int run_command_v_opt(const char **argv, int opt)
  187. {
  188. struct child_process cmd;
  189. prepare_run_command_v_opt(&cmd, argv, opt);
  190. return run_command(&cmd);
  191. }