forked from bitcoin-core/libmultiprocess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
154 lines (136 loc) · 3.97 KB
/
util.cpp
File metadata and controls
154 lines (136 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <mp/config.h>
#include <mp/util.h>
#include <cerrno>
#include <cstdio>
#include <kj/common.h>
#include <kj/string-tree.h>
#include <pthread.h>
#include <sstream>
#include <string>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <system_error>
#include <thread> // NOLINT(misc-include-cleaner) // IWYU pragma: keep
#include <unistd.h>
#include <utility>
#include <vector>
#ifdef __linux__
#include <sys/syscall.h>
#endif
#ifdef HAVE_PTHREAD_GETTHREADID_NP
#include <pthread_np.h>
#endif // HAVE_PTHREAD_GETTHREADID_NP
namespace mp {
namespace {
//! Return highest possible file descriptor.
size_t MaxFd()
{
struct rlimit nofile;
if (getrlimit(RLIMIT_NOFILE, &nofile) == 0) {
return nofile.rlim_cur - 1;
} else {
return 1023;
}
}
} // namespace
std::string ThreadName(const char* exe_name)
{
char thread_name[16] = {0};
#ifdef HAVE_PTHREAD_GETNAME_NP
pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name));
#endif // HAVE_PTHREAD_GETNAME_NP
std::ostringstream buffer;
buffer << (exe_name ? exe_name : "") << "-" << getpid() << "/";
if (thread_name[0] != '\0') {
buffer << thread_name << "-";
}
// Prefer platform specific thread ids over the standard C++11 ones because
// the former are shorter and are the same as what gdb prints "LWP ...".
#ifdef __linux__
buffer << syscall(SYS_gettid);
#elif defined(HAVE_PTHREAD_THREADID_NP)
uint64_t tid = 0;
pthread_threadid_np(NULL, &tid);
buffer << tid;
#elif defined(HAVE_PTHREAD_GETTHREADID_NP)
buffer << pthread_getthreadid_np();
#else
buffer << std::this_thread::get_id();
#endif
return std::move(buffer).str();
}
std::string LogEscape(const kj::StringTree& string, size_t max_size)
{
std::string result;
string.visit([&](const kj::ArrayPtr<const char>& piece) {
if (result.size() > max_size) return;
for (const char c : piece) {
if (c == '\\') {
result.append("\\\\");
} else if (c < 0x20 || c > 0x7e) {
char escape[4];
snprintf(escape, 4, "\\%02x", c);
result.append(escape);
} else {
result.push_back(c);
}
if (result.size() > max_size) {
result += "...";
break;
}
}
});
return result;
}
int SpawnProcess(int& pid, FdToArgsFn&& fd_to_args)
{
int fds[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) != 0) {
throw std::system_error(errno, std::system_category(), "socketpair");
}
pid = fork();
if (pid == -1) {
throw std::system_error(errno, std::system_category(), "fork");
}
// Parent process closes the descriptor for socket 0, child closes the descriptor for socket 1.
if (close(fds[pid ? 0 : 1]) != 0) {
throw std::system_error(errno, std::system_category(), "close");
}
if (!pid) {
// Child process must close all potentially open descriptors, except socket 0.
const int maxFd = MaxFd();
for (int fd = 3; fd < maxFd; ++fd) {
if (fd != fds[0]) {
close(fd);
}
}
ExecProcess(fd_to_args(fds[0]));
}
return fds[1];
}
void ExecProcess(const std::vector<std::string>& args)
{
std::vector<char*> argv;
argv.reserve(args.size());
for (const auto& arg : args) {
argv.push_back(const_cast<char*>(arg.c_str()));
}
argv.push_back(nullptr);
if (execvp(argv[0], argv.data()) != 0) {
perror("execvp failed");
_exit(1);
}
}
int WaitProcess(int pid)
{
int status;
if (::waitpid(pid, &status, 0 /* options */) != pid) {
throw std::system_error(errno, std::system_category(), "waitpid");
}
return status;
}
} // namespace mp