Run a command and wait for it. Returns the exit code (or -1 on error).
| 77 | |
| 78 | /// Run a command and wait for it. Returns the exit code (or -1 on error). |
| 79 | int runCommand(const std::vector<std::string> & args) |
| 80 | { |
| 81 | pid_t pid = fork(); |
| 82 | if (pid < 0) |
| 83 | return -1; |
| 84 | |
| 85 | if (pid == 0) |
| 86 | { |
| 87 | auto argv = buildArgv(args); |
| 88 | execvp(argv[0], argv.data()); |
| 89 | _exit(127); |
| 90 | } |
| 91 | |
| 92 | int status = 0; |
| 93 | while (waitpid(pid, &status, 0) < 0) |
| 94 | if (errno != EINTR) |
| 95 | return -1; |
| 96 | return WIFEXITED(status) ? WEXITSTATUS(status) : -1; |
| 97 | } |
| 98 | |
| 99 | /// Run a command, capture its stdout, return {exit_code, output_lines}. |
| 100 | std::pair<int, std::vector<std::string>> captureCommand(const std::vector<std::string> & args) |
no test coverage detected