run-command.c 4.2 KB

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