Bitcoin Core 29.99.0
P2P Digital Currency
util.cpp
Go to the documentation of this file.
1// Copyright (c) The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <mp/config.h>
6#include <mp/util.h>
7
8#include <cerrno>
9#include <cstdio>
10#include <kj/common.h>
11#include <kj/string-tree.h>
12#include <pthread.h>
13#include <sstream>
14#include <string>
15#include <sys/resource.h>
16#include <sys/socket.h>
17#include <sys/wait.h>
18#include <system_error>
19#include <thread> // NOLINT(misc-include-cleaner) // IWYU pragma: keep
20#include <unistd.h>
21#include <utility>
22#include <vector>
23
24#ifdef __linux__
25#include <sys/syscall.h>
26#endif
27
28#ifdef HAVE_PTHREAD_GETTHREADID_NP
29#include <pthread_np.h>
30#endif // HAVE_PTHREAD_GETTHREADID_NP
31
32namespace mp {
33namespace {
34
36size_t MaxFd()
37{
38 struct rlimit nofile;
39 if (getrlimit(RLIMIT_NOFILE, &nofile) == 0) {
40 return nofile.rlim_cur - 1;
41 } else {
42 return 1023;
43 }
44}
45
46} // namespace
47
48std::string ThreadName(const char* exe_name)
49{
50 char thread_name[16] = {0};
51#ifdef HAVE_PTHREAD_GETNAME_NP
52 pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name));
53#endif // HAVE_PTHREAD_GETNAME_NP
54
55 std::ostringstream buffer;
56 buffer << (exe_name ? exe_name : "") << "-" << getpid() << "/";
57
58 if (thread_name[0] != '\0') {
59 buffer << thread_name << "-";
60 }
61
62 // Prefer platform specific thread ids over the standard C++11 ones because
63 // the former are shorter and are the same as what gdb prints "LWP ...".
64#ifdef __linux__
65 buffer << syscall(SYS_gettid);
66#elif defined(HAVE_PTHREAD_THREADID_NP)
67 uint64_t tid = 0;
68 pthread_threadid_np(NULL, &tid);
69 buffer << tid;
70#elif defined(HAVE_PTHREAD_GETTHREADID_NP)
71 buffer << pthread_getthreadid_np();
72#else
73 buffer << std::this_thread::get_id();
74#endif
75
76 return std::move(buffer).str();
77}
78
79std::string LogEscape(const kj::StringTree& string)
80{
81 const int MAX_SIZE = 1000;
82 std::string result;
83 string.visit([&](const kj::ArrayPtr<const char>& piece) {
84 if (result.size() > MAX_SIZE) return;
85 for (const char c : piece) {
86 if (c == '\\') {
87 result.append("\\\\");
88 } else if (c < 0x20 || c > 0x7e) {
89 char escape[4];
90 snprintf(escape, 4, "\\%02x", c);
91 result.append(escape);
92 } else {
93 result.push_back(c);
94 }
95 if (result.size() > MAX_SIZE) {
96 result += "...";
97 break;
98 }
99 }
100 });
101 return result;
102}
103
104int SpawnProcess(int& pid, FdToArgsFn&& fd_to_args)
105{
106 int fds[2];
107 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) != 0) {
108 throw std::system_error(errno, std::system_category(), "socketpair");
109 }
110
111 pid = fork();
112 if (pid == -1) {
113 throw std::system_error(errno, std::system_category(), "fork");
114 }
115 // Parent process closes the descriptor for socket 0, child closes the descriptor for socket 1.
116 if (close(fds[pid ? 0 : 1]) != 0) {
117 throw std::system_error(errno, std::system_category(), "close");
118 }
119 if (!pid) {
120 // Child process must close all potentially open descriptors, except socket 0.
121 const int maxFd = MaxFd();
122 for (int fd = 3; fd < maxFd; ++fd) {
123 if (fd != fds[0]) {
124 close(fd);
125 }
126 }
127 ExecProcess(fd_to_args(fds[0]));
128 }
129 return fds[1];
130}
131
132void ExecProcess(const std::vector<std::string>& args)
133{
134 std::vector<char*> argv;
135 argv.reserve(args.size());
136 for (const auto& arg : args) {
137 argv.push_back(const_cast<char*>(arg.c_str()));
138 }
139 argv.push_back(nullptr);
140 if (execvp(argv[0], argv.data()) != 0) {
141 perror("execvp failed");
142 _exit(1);
143 }
144}
145
146int WaitProcess(int pid)
147{
148 int status;
149 if (::waitpid(pid, &status, 0 /* options */) != pid) {
150 throw std::system_error(errno, std::system_category(), "waitpid");
151 }
152 return status;
153}
154
155} // namespace mp
ArgsManager & args
Definition: bitcoind.cpp:277
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
int WaitProcess(int pid)
Wait for a process to exit and return its exit code.
Definition: util.cpp:146
std::string ThreadName(const char *exe_name)
Format current thread name as "{exe_name}-{$pid}/{thread_name}-{$tid}".
Definition: util.cpp:48
int SpawnProcess(int &pid, FdToArgsFn &&fd_to_args)
Spawn a new process that communicates with the current process over a socket pair.
Definition: util.cpp:104
std::string LogEscape(const kj::StringTree &string)
Escape binary string for use in log so it doesn't trigger unicode decode errors in python unit tests.
Definition: util.cpp:79
std::function< std::vector< std::string >(int fd)> FdToArgsFn
Callback type used by SpawnProcess below.
Definition: util.h:209
void ExecProcess(const std::vector< std::string > &args)
Call execvp with vector args.
Definition: util.cpp:132
static constexpr uint64_t MAX_SIZE
The maximum size of a serialized object in bytes or number of elements (for eg vectors) when the size...
Definition: serialize.h:32
for(const CTxIn &txin :tx.vin)
Definition: validation.cpp:409