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;
43 argv.reserve(
args.size() + 1);
44 for (
const auto& arg :
args) {
45 argv.push_back(
const_cast<char*
>(arg.c_str()));
47 argv.push_back(
nullptr);
55 if (getrlimit(RLIMIT_NOFILE, &nofile) == 0) {
56 return nofile.rlim_cur - 1;
66 char thread_name[16] = {0};
67#ifdef HAVE_PTHREAD_GETNAME_NP
68 pthread_getname_np(pthread_self(), thread_name,
sizeof(thread_name));
71 std::ostringstream buffer;
72 buffer << (exe_name ? exe_name :
"") <<
"-" << getpid() <<
"/";
74 if (thread_name[0] !=
'\0') {
75 buffer << thread_name <<
"-";
81 buffer << syscall(SYS_gettid);
82#elif defined(HAVE_PTHREAD_THREADID_NP)
84 pthread_threadid_np(NULL, &tid);
86#elif defined(HAVE_PTHREAD_GETTHREADID_NP)
87 buffer << 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(
"\\\\");
103 }
else if (c < 0x20 || c > 0x7e) {
105 snprintf(escape, 4,
"\\%02x", c);
106 result.append(escape);
110 if (result.size() > max_size) {
122 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) != 0) {
123 throw std::system_error(errno, std::system_category(),
"socketpair");
132 const std::vector<std::string>
args{fd_to_args(fds[0])};
133 const std::vector<char*> argv{MakeArgv(
args)};
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";
148 const ssize_t writeResult = ::write(STDERR_FILENO,
msg,
sizeof(
msg) - 1);
156 const int maxFd = MaxFd();
157 for (
int fd = 3; fd < maxFd; ++fd) {
163 execvp(argv[0], argv.data());
168 perror(
"execvp failed");
176 const std::vector<char*> argv{MakeArgv(
args)};
177 if (execvp(argv[0], argv.data()) != 0) {
178 perror(
"execvp failed");
179 if (errno == ENOENT && !
args.empty()) {
180 std::cerr <<
"Missing executable: " << fs::weakly_canonical(
args.front()) <<
'\n';
189 if (::waitpid(pid, &status, 0) != pid) {
190 throw std::system_error(errno, std::system_category(),
"waitpid");
Functions to serialize / deserialize common bitcoin types.
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.
std::function< std::vector< std::string >(int fd)> FdToArgsFn
Callback type used by SpawnProcess below.
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.
for(const CTxIn &txin :tx.vin)