2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright 2010-2022 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
|
|
|
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <csignal>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string_view>
|
2017-11-27 22:22:44 +00:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-03-30 19:59:13 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2017-11-27 22:22:44 +00:00
|
|
|
#include "transmission.h"
|
2022-08-05 19:16:25 +00:00
|
|
|
|
2017-11-27 22:22:44 +00:00
|
|
|
#include "error.h"
|
|
|
|
#include "subprocess.h"
|
|
|
|
#include "tr-assert.h"
|
|
|
|
#include "tr-macros.h"
|
2022-08-05 19:16:25 +00:00
|
|
|
#include "tr-strbuf.h"
|
2017-11-27 22:22:44 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2021-12-28 02:32:22 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void handle_sigchld(int /*i*/)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2021-10-29 22:43:25 +00:00
|
|
|
int rc = 0;
|
2017-11-27 22:22:44 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* FIXME: Only check for our own PIDs */
|
2021-09-15 00:18:09 +00:00
|
|
|
rc = waitpid(-1, nullptr, WNOHANG);
|
2021-08-15 09:41:48 +00:00
|
|
|
} while (rc > 0 || (rc == -1 && errno == EINTR));
|
2017-11-27 22:22:44 +00:00
|
|
|
|
|
|
|
/* FIXME: Call old handler, if any */
|
|
|
|
}
|
|
|
|
|
2021-12-28 02:32:22 +00:00
|
|
|
static void set_system_error(tr_error** error, int code, std::string_view what)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (error == nullptr)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-30 19:59:13 +00:00
|
|
|
tr_error_set(error, code, fmt::format(FMT_STRING("{:s} failed: {:s} ({:d})"), what, tr_strerror(code), code));
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-24 19:25:23 +00:00
|
|
|
static bool tr_spawn_async_in_child(
|
|
|
|
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,
|
2021-11-24 19:25:23 +00:00
|
|
|
int pipe_fd)
|
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
|
|
|
{
|
2021-11-24 19:25:23 +00:00
|
|
|
goto FAIL;
|
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
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
goto FAIL;
|
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
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
goto FAIL;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
FAIL:
|
2022-02-07 04:28:36 +00:00
|
|
|
(void)write(pipe_fd, &errno, sizeof(errno));
|
2017-11-27 22:22:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool tr_spawn_async_in_parent(int pipe_fd, tr_error** error)
|
|
|
|
{
|
2021-10-29 22:43:25 +00:00
|
|
|
int child_errno = 0;
|
|
|
|
ssize_t count = 0;
|
2017-11-27 22:22:44 +00:00
|
|
|
|
2022-01-23 22:47:41 +00:00
|
|
|
static_assert(sizeof(child_errno) == sizeof(errno));
|
2017-11-27 22:22:44 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
count = read(pipe_fd, &child_errno, sizeof(child_errno));
|
2021-08-15 09:41:48 +00:00
|
|
|
} while (count == -1 && errno == EINTR);
|
2017-11-27 22:22:44 +00:00
|
|
|
|
|
|
|
close(pipe_fd);
|
|
|
|
|
|
|
|
if (count == -1)
|
|
|
|
{
|
|
|
|
/* Read failed (what to do?) */
|
|
|
|
}
|
|
|
|
else if (count == 0)
|
|
|
|
{
|
|
|
|
/* Child successfully exec-ed */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TR_ASSERT((size_t)count == sizeof(child_errno));
|
|
|
|
|
|
|
|
set_system_error(error, child_errno, "Child process setup");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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,
|
2021-11-24 19:25:23 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pipe_fds[2];
|
|
|
|
|
|
|
|
if (pipe(pipe_fds) == -1)
|
|
|
|
{
|
|
|
|
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]);
|
|
|
|
|
|
|
|
if (!tr_spawn_async_in_child(cmd, env, work_dir, pipe_fds[1]))
|
|
|
|
{
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(pipe_fds[1]);
|
|
|
|
|
|
|
|
return tr_spawn_async_in_parent(pipe_fds[0], error);
|
|
|
|
}
|