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