src/process.cpp

Fri, 31 Jan 2025 22:11:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 31 Jan 2025 22:11:04 +0100
changeset 5
60c2588b4455
parent 4
82680ce258d6
child 18
fd7a7785c963
permissions
-rw-r--r--

finish MVP

/* 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;

void process::setbin(std::string path) {
    m_path = std::move(path);
}

void process::chdir(std::string path) {
    m_dir = std::move(path);
}

int process::exec(std::vector<std::string> args, bool capture) {
    m_output.clear();

    // fd-pair for the pipe
    int pipefd[2];

    if (pipe(pipefd)) {
        perror("pipe");
        return -1;
    }

    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 (::chdir(m_dir.c_str())) {
            perror("chdir");
            exit(EXIT_FAILURE);
        }
        if (execv(m_path.c_str(), argv)) {
            perror("execl");
            exit(EXIT_FAILURE);
        }
        return 0; // 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
        if (capture) {
            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;
    } else {
        perror("fork");
        return -1;
    }
}

const std::string &process::output() const {
    return m_output;
}

mercurial