src/process.cpp

Tue, 21 Jan 2025 20:24:45 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 21 Jan 2025 20:24:45 +0100
changeset 3
c87bde92805f
parent 2
7384ebae6b7c
child 4
82680ce258d6
permissions
-rw-r--r--

add repository scanner

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

mercurial