2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-08-26 18:35:28 +00:00
|
|
|
#include <array>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <csignal>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <cstdlib>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <map>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2017-11-27 22:22:44 +00:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-03-30 19:59:13 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/error.h"
|
|
|
|
#include "libtransmission/subprocess.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/tr-strbuf.h"
|
|
|
|
#include "libtransmission/utils.h"
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2021-12-28 02:32:22 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
void handle_sigchld(int /*i*/)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2024-01-20 22:56:42 +00:00
|
|
|
for (;;)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2024-01-20 22:56:42 +00:00
|
|
|
// FIXME: only check for our own PIDs
|
|
|
|
auto const res = waitpid(-1, nullptr, WNOHANG);
|
|
|
|
|
|
|
|
if ((res == 0) || (res == -1 && errno != EINTR))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2024-01-20 22:56:42 +00:00
|
|
|
// FIXME: Call old handler, if any
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
void set_system_error(tr_error* error, int code, std::string_view what)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
if (error != nullptr)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2023-11-21 15:02:03 +00:00
|
|
|
error->set(code, fmt::format("{:s} failed: {:s} ({:d})", what, tr_strerror(code), code));
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-07 18:58:16 +00:00
|
|
|
[[nodiscard]] bool tr_spawn_async_in_child(
|
2021-11-24 19:25:23 +00:00
|
|
|
char const* const* cmd,
|
|
|
|
std::map<std::string_view, std::string_view> const& env,
|
2022-09-23 13:39:13 +00:00
|
|
|
std::string_view work_dir)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2021-11-24 19:25:23 +00:00
|
|
|
auto key_sz = std::string{};
|
|
|
|
auto val_sz = std::string{};
|
|
|
|
|
|
|
|
for (auto const& [key_sv, val_sv] : env)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2021-11-24 19:25:23 +00:00
|
|
|
key_sz = key_sv;
|
|
|
|
val_sz = val_sv;
|
|
|
|
|
|
|
|
if (setenv(key_sz.c_str(), val_sz.c_str(), 1) != 0)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-09-23 13:39:13 +00:00
|
|
|
return false;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
if (!std::empty(work_dir) && chdir(tr_pathbuf{ work_dir }) == -1)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-09-23 13:39:13 +00:00
|
|
|
return false;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-24 19:25:23 +00:00
|
|
|
if (execvp(cmd[0], const_cast<char* const*>(cmd)) == -1)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2022-09-23 13:39:13 +00:00
|
|
|
return false;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
[[nodiscard]] bool tr_spawn_async_in_parent(int pipe_fd, tr_error* error)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2024-01-20 22:56:42 +00:00
|
|
|
auto child_errno = int{};
|
|
|
|
auto n_read = ssize_t{};
|
|
|
|
for (auto done = false; !done;)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2024-01-20 22:56:42 +00:00
|
|
|
n_read = read(pipe_fd, &child_errno, sizeof(child_errno));
|
|
|
|
done = n_read != -1 || errno != EINTR;
|
|
|
|
}
|
2017-11-27 22:22:44 +00:00
|
|
|
|
|
|
|
close(pipe_fd);
|
|
|
|
|
2024-01-20 22:56:42 +00:00
|
|
|
if (n_read == 0) // child successfully exec'ed
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2024-01-20 22:56:42 +00:00
|
|
|
return true;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 22:56:42 +00:00
|
|
|
if (n_read > 0) // child errno was set
|
|
|
|
{
|
|
|
|
TR_ASSERT(static_cast<size_t>(n_read) == sizeof(child_errno));
|
2017-11-27 22:22:44 +00:00
|
|
|
set_system_error(error, child_errno, "Child process setup");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:56:42 +00:00
|
|
|
// read failed (what to do?)
|
2017-11-27 22:22:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2023-01-07 18:58:16 +00:00
|
|
|
} // namespace
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2021-11-24 19:25:23 +00:00
|
|
|
bool tr_spawn_async(
|
|
|
|
char const* const* cmd,
|
|
|
|
std::map<std::string_view, std::string_view> const& env,
|
2022-08-05 19:16:25 +00:00
|
|
|
std::string_view work_dir,
|
2023-11-04 16:39:41 +00:00
|
|
|
tr_error* error)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
|
|
|
static bool sigchld_handler_set = false;
|
|
|
|
|
|
|
|
if (!sigchld_handler_set)
|
|
|
|
{
|
2022-01-20 18:27:56 +00:00
|
|
|
/* FIXME: "The effects of signal() in a multithreaded process are unspecified." © man 2 signal */
|
2021-10-17 21:09:58 +00:00
|
|
|
if (signal(SIGCHLD, &handle_sigchld) == SIG_ERR) // NOLINT(performance-no-int-to-ptr)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
|
|
|
set_system_error(error, errno, "Call to signal()");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sigchld_handler_set = true;
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:35:28 +00:00
|
|
|
auto pipe_fds = std::array<int, 2>{};
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-08-26 18:35:28 +00:00
|
|
|
if (pipe(std::data(pipe_fds)) == -1)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
|
|
|
set_system_error(error, errno, "Call to pipe()");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-14 12:40:41 +00:00
|
|
|
if (fcntl(pipe_fds[1], F_SETFD, fcntl(pipe_fds[1], F_GETFD) | FD_CLOEXEC) == -1)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
|
|
|
set_system_error(error, errno, "Call to fcntl()");
|
|
|
|
close(pipe_fds[0]);
|
|
|
|
close(pipe_fds[1]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int const child_pid = fork();
|
|
|
|
|
|
|
|
if (child_pid == -1)
|
|
|
|
{
|
|
|
|
set_system_error(error, errno, "Call to fork()");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child_pid == 0)
|
|
|
|
{
|
|
|
|
close(pipe_fds[0]);
|
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
if (!tr_spawn_async_in_child(cmd, env, work_dir))
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2023-12-23 16:32:04 +00:00
|
|
|
auto const ok = write(pipe_fds[1], &errno, sizeof(errno)) != -1;
|
|
|
|
_exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(pipe_fds[1]);
|
|
|
|
|
|
|
|
return tr_spawn_async_in_parent(pipe_fds[0], error);
|
|
|
|
}
|