13#include <kj/string-tree.h>
18#include <sys/resource.h>
19#include <sys/socket.h>
21#include <system_error>
28#include <sys/syscall.h>
31#ifdef HAVE_PTHREAD_GETTHREADID_NP
32#include <pthread_np.h>
35namespace fs = std::filesystem;
40std::vector<char*>
MakeArgv(
const std::vector<std::string>&
args)
42 std::vector<char*>
argv;
45 argv.push_back(
const_cast<char*
>(
arg.c_str()));
47 argv.push_back(
nullptr);
56 return nofile.rlim_cur - 1;
66 char thread_name[16] = {0};
67#ifdef HAVE_PTHREAD_GETNAME_NP
71 std::ostringstream buffer;
74 if (thread_name[0] !=
'\0') {
75 buffer << thread_name <<
"-";
82#elif defined(HAVE_PTHREAD_THREADID_NP)
86#elif defined(HAVE_PTHREAD_GETTHREADID_NP)
89 buffer << std::this_thread::get_id();
92 return std::move(buffer).str();
95std::string
LogEscape(
const kj::StringTree&
string,
size_t max_size)
98 string.visit([&](
const kj::ArrayPtr<const char>&
piece) {
99 if (result.size() > max_size)
return;
100 for (
const char c :
piece) {
102 result.append(
"\\\\");
110 if (result.size() > max_size) {
123 throw std::system_error(
errno, std::system_category(),
"socketpair");
137 throw std::system_error(
errno, std::system_category(),
"fork");
142 if (close(
fds[
pid ? 0 : 1]) != 0) {
145 throw std::system_error(
errno, std::system_category(),
"close");
147 static constexpr char msg[] =
"SpawnProcess(child): close(fds[1]) failed\n";
157 for (
int fd = 3; fd <
maxFd; ++fd) {
180 std::cerr <<
"Missing executable: " << fs::weakly_canonical(
args.front()) <<
'\n';
190 throw std::system_error(
errno, std::system_category(),
"waitpid");
Functions to serialize / deserialize common bitcoin types.
std::function< std::vector< std::string >(int fd)> FdToArgsFn
Callback type used by SpawnProcess below.
int WaitProcess(int pid)
Wait for a process to exit and return its exit code.
std::string ThreadName(const char *exe_name)
Format current thread name as "{exe_name}-{$pid}/{thread_name}-{$tid}".
int SpawnProcess(int &pid, FdToArgsFn &&fd_to_args)
Spawn a new process that communicates with the current process over a socket pair.
void ExecProcess(const std::vector< std::string > &args)
Call execvp with vector args.
std::string LogEscape(const kj::StringTree &string, size_t max_size)
Escape binary string for use in log so it doesn't trigger unicode decode errors in python unit tests.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.