Files
slocker-lite/src/process.cpp
T
ceamac a301dfb44f Initial commit: OCI image mounting via containers-storage
slocker-lite validates an OCI Image Layout tar, imports its layers into
containers-storage's layer store in order, and mounts the assembled image
using fuse-overlayfs, printing the resulting merged path.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-10 08:12:50 +00:00

79 lines
2.3 KiB
C++

// Copyright (C) 2026 Viorel Munteanu
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include "process.h"
#include <sys/wait.h>
#include <unistd.h>
#include <cstring>
#include <fmt/ranges.h>
#include <spdlog/spdlog.h>
ProcessResult run_process(const std::vector<std::string>& argv) {
spdlog::debug("running external command: {}", fmt::join(argv, " "));
int stdout_pipe[2];
if (pipe(stdout_pipe) != 0) {
return {-1, ""};
}
pid_t pid = fork();
if (pid < 0) {
close(stdout_pipe[0]);
close(stdout_pipe[1]);
return {-1, ""};
}
if (pid == 0) {
close(stdout_pipe[0]);
dup2(stdout_pipe[1], STDOUT_FILENO);
close(stdout_pipe[1]);
std::vector<char*> c_argv;
c_argv.reserve(argv.size() + 1);
for (const auto& arg : argv) {
c_argv.push_back(const_cast<char*>(arg.c_str()));
}
c_argv.push_back(nullptr);
execvp(c_argv[0], c_argv.data());
const char* msg = "run_process: execvp failed\n";
write(STDERR_FILENO, msg, std::strlen(msg));
_exit(127);
}
close(stdout_pipe[1]);
std::string output;
char buffer[4096];
ssize_t n;
while ((n = read(stdout_pipe[0], buffer, sizeof(buffer))) > 0) {
output.append(buffer, static_cast<size_t>(n));
}
close(stdout_pipe[0]);
int status = 0;
waitpid(pid, &status, 0);
int exit_code = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
if (exit_code != 0) {
spdlog::warn("external command failed (exit code {}): {}", exit_code, fmt::join(argv, " "));
}
return {exit_code, output};
}