run-command.c 4.9 KB

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