run-command.c 4.3 KB

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