Bitcoin Core 28.99.0
P2P Digital Currency
process.cpp
Go to the documentation of this file.
1// Copyright (c) 2021-2022 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 <ipc/process.h>
6#include <ipc/protocol.h>
7#include <logging.h>
8#include <mp/util.h>
9#include <tinyformat.h>
10#include <util/fs.h>
11#include <util/strencodings.h>
12#include <util/syserror.h>
13
14#include <cstdint>
15#include <cstdlib>
16#include <errno.h>
17#include <exception>
18#include <iostream>
19#include <stdexcept>
20#include <string.h>
21#include <sys/socket.h>
22#include <sys/un.h>
23#include <unistd.h>
24#include <utility>
25#include <vector>
26
28
29namespace ipc {
30namespace {
31class ProcessImpl : public Process
32{
33public:
34 int spawn(const std::string& new_exe_name, const fs::path& argv0_path, int& pid) override
35 {
36 return mp::SpawnProcess(pid, [&](int fd) {
37 fs::path path = argv0_path;
38 path.remove_filename();
39 path /= fs::PathFromString(new_exe_name);
40 return std::vector<std::string>{fs::PathToString(path), "-ipcfd", strprintf("%i", fd)};
41 });
42 }
43 int waitSpawned(int pid) override { return mp::WaitProcess(pid); }
44 bool checkSpawned(int argc, char* argv[], int& fd) override
45 {
46 // If this process was not started with a single -ipcfd argument, it is
47 // not a process spawned by the spawn() call above, so return false and
48 // do not try to serve requests.
49 if (argc != 3 || strcmp(argv[1], "-ipcfd") != 0) {
50 return false;
51 }
52 // If a single -ipcfd argument was provided, return true and get the
53 // file descriptor so Protocol::serve() can be called to handle
54 // requests from the parent process. The -ipcfd argument is not valid
55 // in combination with other arguments because the parent process
56 // should be able to control the child process through the IPC protocol
57 // without passing information out of band.
58 if (!ParseInt32(argv[2], &fd)) {
59 throw std::runtime_error(strprintf("Invalid -ipcfd number '%s'", argv[2]));
60 }
61 return true;
62 }
63 int connect(const fs::path& data_dir,
64 const std::string& dest_exe_name,
65 std::string& address) override;
66 int bind(const fs::path& data_dir, const std::string& exe_name, std::string& address) override;
67};
68
69static bool ParseAddress(std::string& address,
70 const fs::path& data_dir,
71 const std::string& dest_exe_name,
72 struct sockaddr_un& addr,
73 std::string& error)
74{
75 if (address == "unix" || address.starts_with("unix:")) {
76 fs::path path;
77 if (address.size() <= 5) {
78 path = data_dir / fs::PathFromString(strprintf("%s.sock", RemovePrefixView(dest_exe_name, "bitcoin-")));
79 } else {
80 path = data_dir / fs::PathFromString(address.substr(5));
81 }
82 std::string path_str = fs::PathToString(path);
83 address = strprintf("unix:%s", path_str);
84 if (path_str.size() >= sizeof(addr.sun_path)) {
85 error = strprintf("Unix address path %s exceeded maximum socket path length", fs::quoted(fs::PathToString(path)));
86 return false;
87 }
88 memset(&addr, 0, sizeof(addr));
89 addr.sun_family = AF_UNIX;
90 strncpy(addr.sun_path, path_str.c_str(), sizeof(addr.sun_path)-1);
91 return true;
92 }
93
94 error = strprintf("Unrecognized address '%s'", address);
95 return false;
96}
97
98int ProcessImpl::connect(const fs::path& data_dir,
99 const std::string& dest_exe_name,
100 std::string& address)
101{
102 struct sockaddr_un addr;
103 std::string error;
104 if (!ParseAddress(address, data_dir, dest_exe_name, addr, error)) {
105 throw std::invalid_argument(error);
106 }
107
108 int fd;
109 if ((fd = ::socket(addr.sun_family, SOCK_STREAM, 0)) == -1) {
110 throw std::system_error(errno, std::system_category());
111 }
112 if (::connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
113 return fd;
114 }
115 int connect_error = errno;
116 if (::close(fd) != 0) {
117 LogPrintf("Error closing file descriptor %i '%s': %s\n", fd, address, SysErrorString(errno));
118 }
119 throw std::system_error(connect_error, std::system_category());
120}
121
122int ProcessImpl::bind(const fs::path& data_dir, const std::string& exe_name, std::string& address)
123{
124 struct sockaddr_un addr;
125 std::string error;
126 if (!ParseAddress(address, data_dir, exe_name, addr, error)) {
127 throw std::invalid_argument(error);
128 }
129
130 if (addr.sun_family == AF_UNIX) {
131 fs::path path = addr.sun_path;
132 if (path.has_parent_path()) fs::create_directories(path.parent_path());
133 if (fs::symlink_status(path).type() == fs::file_type::socket) {
134 fs::remove(path);
135 }
136 }
137
138 int fd;
139 if ((fd = ::socket(addr.sun_family, SOCK_STREAM, 0)) == -1) {
140 throw std::system_error(errno, std::system_category());
141 }
142
143 if (::bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
144 return fd;
145 }
146 int bind_error = errno;
147 if (::close(fd) != 0) {
148 LogPrintf("Error closing file descriptor %i: %s\n", fd, SysErrorString(errno));
149 }
150 throw std::system_error(bind_error, std::system_category());
151}
152} // namespace
153
154std::unique_ptr<Process> MakeProcess() { return std::make_unique<ProcessImpl>(); }
155} // namespace ipc
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
#define LogPrintf(...)
Definition: logging.h:266
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
Definition: fs.h:190
static auto quoted(const std::string &s)
Definition: fs.h:95
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:151
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:174
Definition: ipc.h:12
std::unique_ptr< Process > MakeProcess()
Constructor for Process interface.
Definition: process.cpp:154
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
Definition: string.h:169
std::string SysErrorString(int err)
Return system error string from errno value.
Definition: syserror.cpp:19
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1165
bool ParseInt32(std::string_view str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.