run-command.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include "cache.h"
  2. #include "run-command.h"
  3. #include "exec_cmd.h"
  4. static inline void close_pair(int fd[2])
  5. {
  6. close(fd[0]);
  7. close(fd[1]);
  8. }
  9. static inline void dup_devnull(int to)
  10. {
  11. int fd = open("/dev/null", O_RDWR);
  12. dup2(fd, to);
  13. close(fd);
  14. }
  15. int start_command(struct child_process *cmd)
  16. {
  17. int need_in, need_out, need_err;
  18. int fdin[2], fdout[2], fderr[2];
  19. /*
  20. * In case of errors we must keep the promise to close FDs
  21. * that have been passed in via ->in and ->out.
  22. */
  23. need_in = !cmd->no_stdin && cmd->in < 0;
  24. if (need_in) {
  25. if (pipe(fdin) < 0) {
  26. if (cmd->out > 0)
  27. close(cmd->out);
  28. return -ERR_RUN_COMMAND_PIPE;
  29. }
  30. cmd->in = fdin[1];
  31. }
  32. need_out = !cmd->no_stdout
  33. && !cmd->stdout_to_stderr
  34. && cmd->out < 0;
  35. if (need_out) {
  36. if (pipe(fdout) < 0) {
  37. if (need_in)
  38. close_pair(fdin);
  39. else if (cmd->in)
  40. close(cmd->in);
  41. return -ERR_RUN_COMMAND_PIPE;
  42. }
  43. cmd->out = fdout[0];
  44. }
  45. need_err = !cmd->no_stderr && cmd->err < 0;
  46. if (need_err) {
  47. if (pipe(fderr) < 0) {
  48. if (need_in)
  49. close_pair(fdin);
  50. else if (cmd->in)
  51. close(cmd->in);
  52. if (need_out)
  53. close_pair(fdout);
  54. else if (cmd->out)
  55. close(cmd->out);
  56. return -ERR_RUN_COMMAND_PIPE;
  57. }
  58. cmd->err = fderr[0];
  59. }
  60. fflush(NULL);
  61. cmd->pid = fork();
  62. if (!cmd->pid) {
  63. if (cmd->no_stdin)
  64. dup_devnull(0);
  65. else if (need_in) {
  66. dup2(fdin[0], 0);
  67. close_pair(fdin);
  68. } else if (cmd->in) {
  69. dup2(cmd->in, 0);
  70. close(cmd->in);
  71. }
  72. if (cmd->no_stderr)
  73. dup_devnull(2);
  74. else if (need_err) {
  75. dup2(fderr[1], 2);
  76. close_pair(fderr);
  77. }
  78. if (cmd->no_stdout)
  79. dup_devnull(1);
  80. else if (cmd->stdout_to_stderr)
  81. dup2(2, 1);
  82. else if (need_out) {
  83. dup2(fdout[1], 1);
  84. close_pair(fdout);
  85. } else if (cmd->out > 1) {
  86. dup2(cmd->out, 1);
  87. close(cmd->out);
  88. }
  89. if (cmd->dir && chdir(cmd->dir))
  90. die("exec %s: cd to %s failed (%s)", cmd->argv[0],
  91. cmd->dir, strerror(errno));
  92. if (cmd->env) {
  93. for (; *cmd->env; cmd->env++) {
  94. if (strchr(*cmd->env, '='))
  95. putenv((char*)*cmd->env);
  96. else
  97. unsetenv(*cmd->env);
  98. }
  99. }
  100. if (cmd->preexec_cb)
  101. cmd->preexec_cb();
  102. if (cmd->perf_cmd) {
  103. execv_perf_cmd(cmd->argv);
  104. } else {
  105. execvp(cmd->argv[0], (char *const*) cmd->argv);
  106. }
  107. exit(127);
  108. }
  109. if (cmd->pid < 0) {
  110. int err = errno;
  111. if (need_in)
  112. close_pair(fdin);
  113. else if (cmd->in)
  114. close(cmd->in);
  115. if (need_out)
  116. close_pair(fdout);
  117. else if (cmd->out)
  118. close(cmd->out);
  119. if (need_err)
  120. close_pair(fderr);
  121. return err == ENOENT ?
  122. -ERR_RUN_COMMAND_EXEC :
  123. -ERR_RUN_COMMAND_FORK;
  124. }
  125. if (need_in)
  126. close(fdin[0]);
  127. else if (cmd->in)
  128. close(cmd->in);
  129. if (need_out)
  130. close(fdout[1]);
  131. else if (cmd->out)
  132. close(cmd->out);
  133. if (need_err)
  134. close(fderr[1]);
  135. return 0;
  136. }
  137. static int wait_or_whine(pid_t pid)
  138. {
  139. for (;;) {
  140. int status, code;
  141. pid_t waiting = waitpid(pid, &status, 0);
  142. if (waiting < 0) {
  143. if (errno == EINTR)
  144. continue;
  145. error("waitpid failed (%s)", strerror(errno));
  146. return -ERR_RUN_COMMAND_WAITPID;
  147. }
  148. if (waiting != pid)
  149. return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
  150. if (WIFSIGNALED(status))
  151. return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
  152. if (!WIFEXITED(status))
  153. return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
  154. code = WEXITSTATUS(status);
  155. switch (code) {
  156. case 127:
  157. return -ERR_RUN_COMMAND_EXEC;
  158. case 0:
  159. return 0;
  160. default:
  161. return -code;
  162. }
  163. }
  164. }
  165. int finish_command(struct child_process *cmd)
  166. {
  167. return wait_or_whine(cmd->pid);
  168. }
  169. int run_command(struct child_process *cmd)
  170. {
  171. int code = start_command(cmd);
  172. if (code)
  173. return code;
  174. return finish_command(cmd);
  175. }
  176. static void prepare_run_command_v_opt(struct child_process *cmd,
  177. const char **argv,
  178. int opt)
  179. {
  180. memset(cmd, 0, sizeof(*cmd));
  181. cmd->argv = argv;
  182. cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
  183. cmd->perf_cmd = opt & RUN_PERF_CMD ? 1 : 0;
  184. cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
  185. }
  186. int run_command_v_opt(const char **argv, int opt)
  187. {
  188. struct child_process cmd;
  189. prepare_run_command_v_opt(&cmd, argv, opt);
  190. return run_command(&cmd);
  191. }
  192. int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
  193. {
  194. struct child_process cmd;
  195. prepare_run_command_v_opt(&cmd, argv, opt);
  196. cmd.dir = dir;
  197. cmd.env = env;
  198. return run_command(&cmd);
  199. }
  200. int start_async(struct async *async)
  201. {
  202. int pipe_out[2];
  203. if (pipe(pipe_out) < 0)
  204. return error("cannot create pipe: %s", strerror(errno));
  205. async->out = pipe_out[0];
  206. /* Flush stdio before fork() to avoid cloning buffers */
  207. fflush(NULL);
  208. async->pid = fork();
  209. if (async->pid < 0) {
  210. error("fork (async) failed: %s", strerror(errno));
  211. close_pair(pipe_out);
  212. return -1;
  213. }
  214. if (!async->pid) {
  215. close(pipe_out[0]);
  216. exit(!!async->proc(pipe_out[1], async->data));
  217. }
  218. close(pipe_out[1]);
  219. return 0;
  220. }
  221. int finish_async(struct async *async)
  222. {
  223. int ret = 0;
  224. if (wait_or_whine(async->pid))
  225. ret = error("waitpid (async) failed");
  226. return ret;
  227. }
  228. int run_hook(const char *index_file, const char *name, ...)
  229. {
  230. struct child_process hook;
  231. const char **argv = NULL, *env[2];
  232. char index[PATH_MAX];
  233. va_list args;
  234. int ret;
  235. size_t i = 0, alloc = 0;
  236. if (access(perf_path("hooks/%s", name), X_OK) < 0)
  237. return 0;
  238. va_start(args, name);
  239. ALLOC_GROW(argv, i + 1, alloc);
  240. argv[i++] = perf_path("hooks/%s", name);
  241. while (argv[i-1]) {
  242. ALLOC_GROW(argv, i + 1, alloc);
  243. argv[i++] = va_arg(args, const char *);
  244. }
  245. va_end(args);
  246. memset(&hook, 0, sizeof(hook));
  247. hook.argv = argv;
  248. hook.no_stdin = 1;
  249. hook.stdout_to_stderr = 1;
  250. if (index_file) {
  251. snprintf(index, sizeof(index), "PERF_INDEX_FILE=%s", index_file);
  252. env[0] = index;
  253. env[1] = NULL;
  254. hook.env = env;
  255. }
  256. ret = start_command(&hook);
  257. free(argv);
  258. if (ret) {
  259. warning("Could not spawn %s", argv[0]);
  260. return ret;
  261. }
  262. ret = finish_command(&hook);
  263. if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
  264. warning("%s exited due to uncaught signal", argv[0]);
  265. return ret;
  266. }