2023-08-31 17:35:12 +02:00
|
|
|
/* Bi-directional popen.
|
2019-06-02 20:48:04 +02:00
|
|
|
*
|
2023-08-31 11:17:07 +02:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <signal.h>
|
2019-06-02 20:48:04 +02:00
|
|
|
#include <sys/stat.h>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <sys/types.h>
|
2019-06-02 20:48:04 +02:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-06-23 10:50:35 +02:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <cstring>
|
2019-06-23 10:50:35 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <fmt/core.h>
|
2019-06-02 20:48:04 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
|
|
|
#include <villas/popen.hpp>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <villas/utils.hpp>
|
2019-06-02 20:48:04 +02:00
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace utils {
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Popen::Popen(const std::string &cmd, const arg_list &args, const env_map &env,
|
|
|
|
const std::string &wd, bool sh)
|
|
|
|
: command(cmd), working_dir(wd), arguments(args), environment(env),
|
|
|
|
shell(sh) {
|
|
|
|
open();
|
2019-06-02 20:48:04 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Popen::~Popen() { close(); }
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void Popen::kill(int signal) { ::kill(pid, signal); }
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void Popen::open() {
|
|
|
|
std::vector<char *> argv, envp;
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
const int READ = 0;
|
|
|
|
const int WRITE = 1;
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
int ret;
|
|
|
|
int inpipe[2];
|
|
|
|
int outpipe[2];
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = pipe(inpipe);
|
|
|
|
if (ret)
|
|
|
|
goto error_out;
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = pipe(outpipe);
|
|
|
|
if (ret)
|
|
|
|
goto clean_inpipe_out;
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
fd_in = outpipe[READ];
|
|
|
|
fd_out = inpipe[WRITE];
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
pid = fork();
|
|
|
|
if (pid == -1)
|
|
|
|
goto clean_outpipe_out;
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
if (pid == 0) {
|
|
|
|
// Prepare arguments
|
|
|
|
if (shell) {
|
|
|
|
argv.push_back((char *)"sh");
|
|
|
|
argv.push_back((char *)"-c");
|
|
|
|
argv.push_back((char *)command.c_str());
|
|
|
|
} else {
|
|
|
|
for (auto arg : arguments) {
|
|
|
|
auto s = strdup(arg.c_str());
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
argv.push_back(s);
|
|
|
|
}
|
|
|
|
}
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Prepare environment
|
|
|
|
for (char **p = environ; *p; p++)
|
|
|
|
envp.push_back(*p);
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
for (auto env : environment) {
|
|
|
|
auto e = fmt::format("{}={}", env.first, env.second);
|
|
|
|
auto s = strdup(e.c_str());
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
envp.push_back(s);
|
|
|
|
}
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
argv.push_back(nullptr);
|
|
|
|
envp.push_back(nullptr);
|
2019-06-23 10:50:35 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Redirect IO
|
|
|
|
::close(outpipe[READ]);
|
|
|
|
::close(inpipe[WRITE]);
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
if (inpipe[READ] != STDIN_FILENO) {
|
|
|
|
dup2(inpipe[READ], STDIN_FILENO);
|
|
|
|
::close(inpipe[READ]);
|
|
|
|
}
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
if (outpipe[WRITE] != STDOUT_FILENO) {
|
|
|
|
dup2(outpipe[WRITE], STDOUT_FILENO);
|
|
|
|
::close(outpipe[WRITE]);
|
|
|
|
}
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Change working directory
|
|
|
|
if (!working_dir.empty()) {
|
|
|
|
int ret;
|
2019-06-23 16:12:53 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
ret = chdir(working_dir.c_str());
|
|
|
|
if (ret)
|
|
|
|
exit(127);
|
|
|
|
}
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
execvpe(shell ? _PATH_BSHELL : command.c_str(), (char *const *)argv.data(),
|
|
|
|
(char *const *)envp.data());
|
|
|
|
exit(127);
|
|
|
|
}
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
::close(outpipe[WRITE]);
|
|
|
|
::close(inpipe[READ]);
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
return;
|
2019-06-02 20:48:04 +02:00
|
|
|
|
|
|
|
clean_outpipe_out:
|
2023-09-07 13:19:19 +02:00
|
|
|
::close(outpipe[READ]);
|
|
|
|
::close(outpipe[WRITE]);
|
2019-06-02 20:48:04 +02:00
|
|
|
|
|
|
|
clean_inpipe_out:
|
2023-09-07 13:19:19 +02:00
|
|
|
::close(inpipe[READ]);
|
|
|
|
::close(inpipe[WRITE]);
|
2019-06-02 20:48:04 +02:00
|
|
|
|
|
|
|
error_out:
|
2023-09-07 13:19:19 +02:00
|
|
|
throw SystemError("Failed to start subprocess");
|
2019-06-02 20:48:04 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
int Popen::close() {
|
|
|
|
int pstat;
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
if (pid != -1) {
|
|
|
|
do {
|
|
|
|
pid = waitpid(pid, &pstat, 0);
|
|
|
|
} while (pid == -1 && errno == EINTR);
|
|
|
|
}
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
return pid == -1 ? -1 : pstat;
|
2021-11-24 08:54:55 -05:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
PopenStream::PopenStream(const std::string &cmd, const arg_list &args,
|
|
|
|
const env_map &env, const std::string &wd, bool sh)
|
|
|
|
: Popen(cmd, args, env, wd, sh) {
|
|
|
|
open();
|
2021-11-24 08:54:55 -05:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
PopenStream::~PopenStream() { close(); }
|
2021-11-24 08:54:55 -05:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void PopenStream::open() {
|
|
|
|
Popen::open();
|
2021-11-24 08:54:55 -05:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
input.buffer = std::make_unique<stdio_buf>(fd_in, std::ios_base::in);
|
|
|
|
output.buffer = std::make_unique<stdio_buf>(fd_out, std::ios_base::out);
|
2021-11-24 08:54:55 -05:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
input.stream = std::make_unique<std::istream>(input.buffer.get());
|
|
|
|
output.stream = std::make_unique<std::ostream>(output.buffer.get());
|
2021-11-24 08:54:55 -05:00
|
|
|
}
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
int PopenStream::close() {
|
|
|
|
int ret = Popen::close();
|
2021-11-24 08:54:55 -05:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
input.stream.reset();
|
|
|
|
output.stream.reset();
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
input.buffer.reset();
|
|
|
|
output.buffer.reset();
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
return ret;
|
2019-06-02 20:48:04 +02:00
|
|
|
}
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
} // namespace utils
|
|
|
|
} // namespace villas
|