Tue, 21 Jan 2025 18:25:59 +0100
add utility to spawn processes and retrieve their output
src/Makefile | file | annotate | diff | comparison | revisions | |
src/process.cpp | file | annotate | diff | comparison | revisions | |
src/process.h | file | annotate | diff | comparison | revisions |
--- a/src/Makefile Tue Jan 21 18:09:14 2025 +0100 +++ b/src/Makefile Tue Jan 21 18:25:59 2025 +0100 @@ -23,7 +23,7 @@ include ../config.mk -SRC=main.cpp +SRC=main.cpp process.cpp OBJ=$(SRC:%.cpp=../build/%.o) OUTPUT=../build/fallusmeter @@ -44,3 +44,7 @@ @echo "Compiling $<" $(CXX) -o $@ $(CXXFLAGS) -c $< +../build/process.o: process.cpp process.h + @echo "Compiling $<" + $(CXX) -o $@ $(CXXFLAGS) -c $< +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/process.cpp Tue Jan 21 18:25:59 2025 +0100 @@ -0,0 +1,104 @@ +/* Copyright 2025 Mike Becker. All rights reserved. +* + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "process.h" + +#include <cstdio> +#include <cstdlib> +#include <fcntl.h> +#include <unistd.h> +#include <sys/wait.h> + +using namespace fm; + +process::process(std::string path): m_path(std::move(path)) { +} + +bool process::exec(std::vector<std::string> args) { + // fd-pair for the pipe + int pipefd[2]; + + if (pipe(pipefd)) { + perror("pipe"); + return false; + } + + pid_t pid = fork(); + if (pid == 0) { + // connect the pipe and close the end we don't use + if (dup2(pipefd[1], STDOUT_FILENO) == -1) { + perror("pipe"); + exit(EXIT_FAILURE); + } + close(pipefd[0]); + + // create the execv argument list + char *argv[args.size() + 2] = {}; + auto slash = m_path.find_last_of('/'); + if (slash == std::string::npos) { + argv[0] = m_path.data(); + } else { + argv[0] = m_path.data() + slash + 1; + } + unsigned i = 0; + for (auto&& arg : args) { + argv[++i] = arg.data(); + } + argv[args.size() + 1] = nullptr; + + // execute the child program + if (execv(m_path.c_str(), argv)) { + perror("execl"); + exit(EXIT_FAILURE); + } + return true; // unreachable, but the compiler doesn't know that + } else if (pid > 0) { + // close the end of the pipe we don't use + close(pipefd[1]); + + // read all the output + char buf[64]; + ssize_t r; + while ((r = read(pipefd[0], buf, sizeof(buf))) > 0) { + m_output.append(buf, r); + } + if (r < 0) { + perror("read"); + } + close(pipefd[0]); + + // wait for the process to completely finish + int status = -1; + waitpid(pid, &status, 0); + + return status == 0; + } else { + perror("fork"); + return false; + } +} + +const std::string &process::output() const { + return m_output; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/process.h Tue Jan 21 18:25:59 2025 +0100 @@ -0,0 +1,46 @@ +/* Copyright 2025 Mike Becker. All rights reserved. +* + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef PROCESS_H +#define PROCESS_H + +#include <string> +#include <vector> + +namespace fm { + +class process { + std::string m_path; + std::string m_output; +public: + explicit process(std::string path); + ~process() = default; + + [[nodiscard]] bool exec(std::vector<std::string> args); + [[nodiscard]] const std::string &output() const; +}; + +} + +#endif //PROCESS_H