run-command.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <ctype.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <linux/compiler.h>
  9. #include <linux/string.h>
  10. #include <errno.h>
  11. #include <sys/wait.h>
  12. #include "subcmd-util.h"
  13. #include "run-command.h"
  14. #include "exec-cmd.h"
  15. #define STRERR_BUFSIZE 128
  16. static inline void close_pair(int fd[2])
  17. {
  18. close(fd[0]);
  19. close(fd[1]);
  20. }
  21. static inline void dup_devnull(int to)
  22. {
  23. int fd = open("/dev/null", O_RDWR);
  24. dup2(fd, to);
  25. close(fd);
  26. }
  27. int start_command(struct child_process *cmd)
  28. {
  29. int need_in, need_out, need_err;
  30. int fdin[2], fdout[2], fderr[2];
  31. char sbuf[STRERR_BUFSIZE];
  32. /*
  33. * In case of errors we must keep the promise to close FDs
  34. * that have been passed in via ->in and ->out.
  35. */
  36. need_in = !cmd->no_stdin && cmd->in < 0;
  37. if (need_in) {
  38. if (pipe(fdin) < 0) {
  39. if (cmd->out > 0)
  40. close(cmd->out);
  41. return -ERR_RUN_COMMAND_PIPE;
  42. }
  43. cmd->in = fdin[1];
  44. }
  45. need_out = !cmd->no_stdout
  46. && !cmd->stdout_to_stderr
  47. && cmd->out < 0;
  48. if (need_out) {
  49. if (pipe(fdout) < 0) {
  50. if (need_in)
  51. close_pair(fdin);
  52. else if (cmd->in)
  53. close(cmd->in);
  54. return -ERR_RUN_COMMAND_PIPE;
  55. }
  56. cmd->out = fdout[0];
  57. }
  58. need_err = !cmd->no_stderr && cmd->err < 0;
  59. if (need_err) {
  60. if (pipe(fderr) < 0) {
  61. if (need_in)
  62. close_pair(fdin);
  63. else if (cmd->in)
  64. close(cmd->in);
  65. if (need_out)
  66. close_pair(fdout);
  67. else if (cmd->out)
  68. close(cmd->out);
  69. return -ERR_RUN_COMMAND_PIPE;
  70. }
  71. cmd->err = fderr[0];
  72. }
  73. fflush(NULL);
  74. cmd->pid = fork();
  75. if (!cmd->pid) {
  76. if (cmd->no_stdin)
  77. dup_devnull(0);
  78. else if (need_in) {
  79. dup2(fdin[0], 0);
  80. close_pair(fdin);
  81. } else if (cmd->in) {
  82. dup2(cmd->in, 0);
  83. close(cmd->in);
  84. }
  85. if (cmd->no_stderr)
  86. dup_devnull(2);
  87. else if (need_err) {
  88. dup2(fderr[1], 2);
  89. close_pair(fderr);
  90. }
  91. if (cmd->no_stdout)
  92. dup_devnull(1);
  93. else if (cmd->stdout_to_stderr)
  94. dup2(2, 1);
  95. else if (need_out) {
  96. dup2(fdout[1], 1);
  97. close_pair(fdout);
  98. } else if (cmd->out > 1) {
  99. dup2(cmd->out, 1);
  100. close(cmd->out);
  101. }
  102. if (cmd->dir && chdir(cmd->dir))
  103. die("exec %s: cd to %s failed (%s)", cmd->argv[0],
  104. cmd->dir, str_error_r(errno, sbuf, sizeof(sbuf)));
  105. if (cmd->env) {
  106. for (; *cmd->env; cmd->env++) {
  107. if (strchr(*cmd->env, '='))
  108. putenv((char*)*cmd->env);
  109. else
  110. unsetenv(*cmd->env);
  111. }
  112. }
  113. if (cmd->preexec_cb)
  114. cmd->preexec_cb();
  115. if (cmd->no_exec_cmd)
  116. exit(cmd->no_exec_cmd(cmd));
  117. if (cmd->exec_cmd) {
  118. execv_cmd(cmd->argv);
  119. } else {
  120. execvp(cmd->argv[0], (char *const*) cmd->argv);
  121. }
  122. exit(127);
  123. }
  124. if (cmd->pid < 0) {
  125. int err = errno;
  126. if (need_in)
  127. close_pair(fdin);
  128. else if (cmd->in)
  129. close(cmd->in);
  130. if (need_out)
  131. close_pair(fdout);
  132. else if (cmd->out)
  133. close(cmd->out);
  134. if (need_err)
  135. close_pair(fderr);
  136. return err == ENOENT ?
  137. -ERR_RUN_COMMAND_EXEC :
  138. -ERR_RUN_COMMAND_FORK;
  139. }
  140. if (need_in)
  141. close(fdin[0]);
  142. else if (cmd->in)
  143. close(cmd->in);
  144. if (need_out)
  145. close(fdout[1]);
  146. else if (cmd->out)
  147. close(cmd->out);
  148. if (need_err)
  149. close(fderr[1]);
  150. return 0;
  151. }
  152. static int wait_or_whine(struct child_process *cmd, bool block)
  153. {
  154. bool finished = cmd->finished;
  155. int result = cmd->finish_result;
  156. while (!finished) {
  157. int status, code;
  158. pid_t waiting = waitpid(cmd->pid, &status, block ? 0 : WNOHANG);
  159. if (!block && waiting == 0)
  160. break;
  161. if (waiting < 0 && errno == EINTR)
  162. continue;
  163. finished = true;
  164. if (waiting < 0) {
  165. char sbuf[STRERR_BUFSIZE];
  166. fprintf(stderr, " Error: waitpid failed (%s)",
  167. str_error_r(errno, sbuf, sizeof(sbuf)));
  168. result = -ERR_RUN_COMMAND_WAITPID;
  169. } else if (waiting != cmd->pid) {
  170. result = -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
  171. } else if (WIFSIGNALED(status)) {
  172. result = -ERR_RUN_COMMAND_WAITPID_SIGNAL;
  173. } else if (!WIFEXITED(status)) {
  174. result = -ERR_RUN_COMMAND_WAITPID_NOEXIT;
  175. } else {
  176. code = WEXITSTATUS(status);
  177. switch (code) {
  178. case 127:
  179. result = -ERR_RUN_COMMAND_EXEC;
  180. break;
  181. case 0:
  182. result = 0;
  183. break;
  184. default:
  185. result = -code;
  186. break;
  187. }
  188. }
  189. }
  190. if (finished) {
  191. cmd->finished = 1;
  192. cmd->finish_result = result;
  193. }
  194. return result;
  195. }
  196. /*
  197. * Conservative estimate of number of characaters needed to hold an a decoded
  198. * integer, assume each 3 bits needs a character byte and plus a possible sign
  199. * character.
  200. */
  201. #ifndef is_signed_type
  202. #define is_signed_type(type) (((type)(-1)) < (type)1)
  203. #endif
  204. #define MAX_STRLEN_TYPE(type) (sizeof(type) * 8 / 3 + (is_signed_type(type) ? 1 : 0))
  205. int check_if_command_finished(struct child_process *cmd)
  206. {
  207. #ifdef __linux__
  208. char filename[6 + MAX_STRLEN_TYPE(typeof(cmd->pid)) + 7 + 1];
  209. char status_line[256];
  210. FILE *status_file;
  211. /*
  212. * Check by reading /proc/<pid>/status as calling waitpid causes
  213. * stdout/stderr to be closed and data lost.
  214. */
  215. sprintf(filename, "/proc/%u/status", cmd->pid);
  216. status_file = fopen(filename, "r");
  217. if (status_file == NULL) {
  218. /* Open failed assume finish_command was called. */
  219. return true;
  220. }
  221. while (fgets(status_line, sizeof(status_line), status_file) != NULL) {
  222. char *p;
  223. if (strncmp(status_line, "State:", 6))
  224. continue;
  225. fclose(status_file);
  226. p = status_line + 6;
  227. while (isspace(*p))
  228. p++;
  229. return *p == 'Z' ? 1 : 0;
  230. }
  231. /* Read failed assume finish_command was called. */
  232. fclose(status_file);
  233. return 1;
  234. #else
  235. wait_or_whine(cmd, /*block=*/false);
  236. return cmd->finished;
  237. #endif
  238. }
  239. int finish_command(struct child_process *cmd)
  240. {
  241. return wait_or_whine(cmd, /*block=*/true);
  242. }
  243. int run_command(struct child_process *cmd)
  244. {
  245. int code = start_command(cmd);
  246. if (code)
  247. return code;
  248. return finish_command(cmd);
  249. }
  250. static void prepare_run_command_v_opt(struct child_process *cmd,
  251. const char **argv,
  252. int opt)
  253. {
  254. memset(cmd, 0, sizeof(*cmd));
  255. cmd->argv = argv;
  256. cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
  257. cmd->exec_cmd = opt & RUN_EXEC_CMD ? 1 : 0;
  258. cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
  259. }
  260. int run_command_v_opt(const char **argv, int opt)
  261. {
  262. struct child_process cmd;
  263. prepare_run_command_v_opt(&cmd, argv, opt);
  264. return run_command(&cmd);
  265. }