run-command.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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->exec_cmd) {
  114. execv_cmd(cmd->argv);
  115. } else {
  116. execvp(cmd->argv[0], (char *const*) cmd->argv);
  117. }
  118. exit(127);
  119. }
  120. if (cmd->pid < 0) {
  121. int err = errno;
  122. if (need_in)
  123. close_pair(fdin);
  124. else if (cmd->in)
  125. close(cmd->in);
  126. if (need_out)
  127. close_pair(fdout);
  128. else if (cmd->out)
  129. close(cmd->out);
  130. if (need_err)
  131. close_pair(fderr);
  132. return err == ENOENT ?
  133. -ERR_RUN_COMMAND_EXEC :
  134. -ERR_RUN_COMMAND_FORK;
  135. }
  136. if (need_in)
  137. close(fdin[0]);
  138. else if (cmd->in)
  139. close(cmd->in);
  140. if (need_out)
  141. close(fdout[1]);
  142. else if (cmd->out)
  143. close(cmd->out);
  144. if (need_err)
  145. close(fderr[1]);
  146. return 0;
  147. }
  148. static int wait_or_whine(pid_t pid)
  149. {
  150. char sbuf[STRERR_BUFSIZE];
  151. for (;;) {
  152. int status, code;
  153. pid_t waiting = waitpid(pid, &status, 0);
  154. if (waiting < 0) {
  155. if (errno == EINTR)
  156. continue;
  157. fprintf(stderr, " Error: waitpid failed (%s)",
  158. str_error_r(errno, sbuf, sizeof(sbuf)));
  159. return -ERR_RUN_COMMAND_WAITPID;
  160. }
  161. if (waiting != pid)
  162. return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
  163. if (WIFSIGNALED(status))
  164. return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
  165. if (!WIFEXITED(status))
  166. return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
  167. code = WEXITSTATUS(status);
  168. switch (code) {
  169. case 127:
  170. return -ERR_RUN_COMMAND_EXEC;
  171. case 0:
  172. return 0;
  173. default:
  174. return -code;
  175. }
  176. }
  177. }
  178. int finish_command(struct child_process *cmd)
  179. {
  180. return wait_or_whine(cmd->pid);
  181. }
  182. int run_command(struct child_process *cmd)
  183. {
  184. int code = start_command(cmd);
  185. if (code)
  186. return code;
  187. return finish_command(cmd);
  188. }
  189. static void prepare_run_command_v_opt(struct child_process *cmd,
  190. const char **argv,
  191. int opt)
  192. {
  193. memset(cmd, 0, sizeof(*cmd));
  194. cmd->argv = argv;
  195. cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
  196. cmd->exec_cmd = opt & RUN_EXEC_CMD ? 1 : 0;
  197. cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
  198. }
  199. int run_command_v_opt(const char **argv, int opt)
  200. {
  201. struct child_process cmd;
  202. prepare_run_command_v_opt(&cmd, argv, opt);
  203. return run_command(&cmd);
  204. }