Bitcoin Core 22.99.0
P2P Digital Currency
process.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 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 <fs.h>
6#include <ipc/process.h>
7#include <ipc/protocol.h>
8#include <mp/util.h>
9#include <tinyformat.h>
10#include <util/strencodings.h>
11
12#include <cstdint>
13#include <exception>
14#include <iostream>
15#include <stdexcept>
16#include <stdlib.h>
17#include <string.h>
18#include <system_error>
19#include <unistd.h>
20#include <utility>
21#include <vector>
22
23namespace ipc {
24namespace {
25class ProcessImpl : public Process
26{
27public:
28 int spawn(const std::string& new_exe_name, const fs::path& argv0_path, int& pid) override
29 {
30 return mp::SpawnProcess(pid, [&](int fd) {
31 fs::path path = argv0_path;
32 path.remove_filename();
33 path /= fs::PathFromString(new_exe_name);
34 return std::vector<std::string>{fs::PathToString(path), "-ipcfd", strprintf("%i", fd)};
35 });
36 }
37 int waitSpawned(int pid) override { return mp::WaitProcess(pid); }
38 bool checkSpawned(int argc, char* argv[], int& fd) override
39 {
40 // If this process was not started with a single -ipcfd argument, it is
41 // not a process spawned by the spawn() call above, so return false and
42 // do not try to serve requests.
43 if (argc != 3 || strcmp(argv[1], "-ipcfd") != 0) {
44 return false;
45 }
46 // If a single -ipcfd argument was provided, return true and get the
47 // file descriptor so Protocol::serve() can be called to handle
48 // requests from the parent process. The -ipcfd argument is not valid
49 // in combination with other arguments because the parent process
50 // should be able to control the child process through the IPC protocol
51 // without passing information out of band.
52 if (!ParseInt32(argv[2], &fd)) {
53 throw std::runtime_error(strprintf("Invalid -ipcfd number '%s'", argv[2]));
54 }
55 return true;
56 }
57};
58} // namespace
59
60std::unique_ptr<Process> MakeProcess() { return std::make_unique<ProcessImpl>(); }
61} // namespace ipc
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:34
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:120
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:133
Definition: ipc.h:12
std::unique_ptr< Process > MakeProcess()
Constructor for Process interface.
Definition: process.cpp:60
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164