run-command.c 4.3 KB

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