|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <string> |
| 4 | +#include <stdexcept> |
| 5 | +#include <iostream> |
| 6 | +#include <algorithm> |
| 7 | +#include <functional> |
| 8 | +#include <tuple> |
| 9 | +#include <vector> |
| 10 | +#include <list> |
| 11 | +#include <future> |
| 12 | + |
| 13 | +// unix process stuff |
| 14 | +#include <signal.h> |
| 15 | +#include <unistd.h> |
| 16 | +#include <sys/wait.h> |
| 17 | +#include <sys/prctl.h> |
| 18 | +#include <cstring> |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +namespace subprocess { |
| 23 | + |
| 24 | +/* execute a process, inputting stdin and calling the functor with the stdout lines. |
| 25 | + * returns the status code of the executed program |
| 26 | + * */ |
| 27 | + |
| 28 | +int execute(const std::string& commandPath, |
| 29 | + const std::vector<std::string>& commandArgs, |
| 30 | + std::list<std::string>& stringInput /* what pumps into stdin */, |
| 31 | + std::function<void(std::string)> lambda) { |
| 32 | + // based off https://stackoverflow.com/a/6172578 |
| 33 | + pid_t pid = 0; |
| 34 | + int inpipefd[2]; |
| 35 | + int outpipefd[2]; |
| 36 | + |
| 37 | + |
| 38 | + // construct the argument list (unfortunately, the C api wasn't defined with C++ in mind, so we have to abuse const_cast) |
| 39 | + // see: https://stackoverflow.com/a/190208 |
| 40 | + std::vector<char*> cargs; |
| 41 | + // the process name must be first for execv |
| 42 | + cargs.push_back(const_cast<char*>(commandPath.c_str())); |
| 43 | + for (const std::string& arg : commandArgs) { |
| 44 | + cargs.push_back(const_cast<char*>(arg.c_str())); |
| 45 | + } |
| 46 | + // must be terminated with a nullptr for execv |
| 47 | + cargs.push_back(nullptr); |
| 48 | + |
| 49 | + pipe(inpipefd); |
| 50 | + pipe(outpipefd); |
| 51 | + pid = fork(); |
| 52 | + // child |
| 53 | + if (pid == 0) { |
| 54 | + dup2(outpipefd[0], STDIN_FILENO); |
| 55 | + dup2(inpipefd[1], STDOUT_FILENO); |
| 56 | + dup2(inpipefd[1], STDERR_FILENO); |
| 57 | + |
| 58 | + // XXX: test (close the stdin..?) |
| 59 | + close(outpipefd[1]); |
| 60 | + |
| 61 | + //ask kernel to deliver SIGTERM in case the parent dies |
| 62 | + prctl(PR_SET_PDEATHSIG, SIGTERM); |
| 63 | + |
| 64 | + execv(commandPath.c_str(), cargs.data()); |
| 65 | + // Nothing below this line should be executed by child process. If so, |
| 66 | + // it means that the execl function wasn't successfull, so lets exit: |
| 67 | + exit(1); |
| 68 | + } |
| 69 | + |
| 70 | + close(outpipefd[0]); |
| 71 | + close(inpipefd[1]); |
| 72 | + |
| 73 | + // while our string queue is working, |
| 74 | + while (!stringInput.empty()) { |
| 75 | + // write our input to the process's stdin pipe |
| 76 | + std::string newInput = stringInput.front(); |
| 77 | + stringInput.pop_front(); |
| 78 | + write(outpipefd[1], newInput.c_str(), newInput.size()); |
| 79 | + } |
| 80 | + // now we finished chucking in the string, send an EOF |
| 81 | + close(outpipefd[1]); |
| 82 | + |
| 83 | + // iterate over each line output by the child's stdout, and call the functor |
| 84 | + FILE* childStdout = fdopen(inpipefd[0], "r"); |
| 85 | + char* line = nullptr; |
| 86 | + ssize_t nread; |
| 87 | + size_t len; |
| 88 | + while ((nread = getline(&line, &len, childStdout)) != -1) { |
| 89 | + lambda(std::string(line)); |
| 90 | + |
| 91 | + // free up the memory allocated by getline |
| 92 | + free(line); |
| 93 | + line = nullptr; |
| 94 | + } |
| 95 | + if (line != nullptr) free(line); |
| 96 | + |
| 97 | + fclose(childStdout); |
| 98 | + int status; |
| 99 | + waitpid(pid, &status, 0); |
| 100 | + |
| 101 | + return status; |
| 102 | +} |
| 103 | + |
| 104 | +/* convenience fn to return a list of outputted strings */ |
| 105 | +std::vector<std::string> checkOutput(const std::string& commandPath, |
| 106 | + const std::vector<std::string>& commandArgs, |
| 107 | + std::list<std::string>& stringInput /* what pumps into stdin */, |
| 108 | + int& status) { |
| 109 | + std::vector<std::string> retVec; |
| 110 | + status = execute(commandPath, commandArgs, stringInput, [&](std::string s) { retVec.push_back(std::move(s)); }); |
| 111 | + return retVec; |
| 112 | +} |
| 113 | + |
| 114 | +/* spawn the process in the background asynchronously, and return a future of the status code */ |
| 115 | +std::future<int> async(const std::string commandPath, const std::vector<std::string> commandArgs, std::list<std::string> stringInput, std::function<void(std::string)> lambda) { |
| 116 | + // spawn the function async - we must pass the args by value into the async lambda |
| 117 | + // otherwise they may destruct before the execute fn executes! |
| 118 | + // whew, that was an annoying bug to find... |
| 119 | + return std::async(std::launch::async, |
| 120 | + [&](const std::string cp, |
| 121 | + const std::vector<std::string> ca, |
| 122 | + std::list<std::string> si, |
| 123 | + std::function<void(std::string)> l) { return execute(cp, ca, si, l); }, commandPath, commandArgs, stringInput, lambda); |
| 124 | +} |
| 125 | + |
| 126 | +} // end namespace subprocess |
0 commit comments