xaizek / etabench (License: GPLv3) (since 2022-09-08)
Benchmark for algorithms that compute I/O ETA
<root> / src / utils / os.cpp (726b50fa5ada9d4e0b85542efcf17a63e769762a) (2,666B) (mode 100644) [raw]
// etabench
// Copyright (C) 2022 xaizek <xaizek@posteo.net>
//
// This file is part of etabench.
//
// etabench is free software: you can redistribute it and/or modify
// it under the terms of version 3 of the GNU General Public License
// as published by the Free Software Foundation.
//
// etabench is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with etabench.  If not, see <https://www.gnu.org/licenses/>.

#include "os.hpp"

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include <filesystem>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

namespace fs = std::filesystem;

TempDir::TempDir(const std::string &prefix)
{
    path = fs::temp_directory_path() / (prefix + "-XXXXXX");
    if (mkdtemp(path.data()) == nullptr) {
        throw std::runtime_error("Failed to create temporary directory: " +
                                 path);
    }
}

TempDir::~TempDir()
{
    fs::remove_all(path);
}

// Formats command as a string.  Shortens it to a reasonable number of arguments
// if necessary.
static std::string
stringify(const std::vector<std::string> &cmd)
{
    if (cmd.empty()) {
        return std::string();
    }

    std::ostringstream oss;
    oss << cmd[0];
    for (unsigned int i = 1U; i < cmd.size(); ++i) {
        if (i > 5U && cmd.size() - i > 2U) {
            oss << " {" << cmd.size() - i << " more arguments...}";
            break;
        }
        oss << " {" << cmd[i] << '}';
    }
    return oss.str();
}

int
queryProc(std::vector<std::string> &&cmd)
{
    if (cmd.empty()) {
        throw std::runtime_error("Attempt to run an empty command");
    }

    pid_t pid = fork();
    if (pid == static_cast<pid_t>(-1)) {
        throw std::runtime_error("Fork has failed");
    }
    if (pid == static_cast<pid_t>(0)) {
        char *argv[cmd.size() + 1U];
        for (std::size_t i = 0; i < cmd.size(); ++i) {
            argv[i] = &cmd[i][0];
        }
        argv[cmd.size()] = nullptr;

        execvp(argv[0], argv);
        _Exit(127);
    }

    int wstatus;
    if (waitpid(pid, &wstatus, 0) == -1) {
        throw std::runtime_error("Failed to wait for process: " +
                                 stringify(cmd));
    }

    if (!WIFEXITED(wstatus)) {
        throw std::runtime_error("Command hasn't finished: " +
                                 stringify(cmd));
    }

    return WEXITSTATUS(wstatus);
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/etabench

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/etabench

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master