ipc, refactor: Add ProcessId type alias and use it

Use ProcessId type instead of int to represent process ids to be
compatible with an upcoming version of libmultiprocess which adds
windows support.
This commit is contained in:
Ryan Ofsky
2025-04-30 08:39:29 -04:00
parent 3449797141
commit 2ee9b69c7a
4 changed files with 27 additions and 5 deletions

View File

@@ -62,7 +62,7 @@ public:
}
std::unique_ptr<interfaces::Init> spawnProcess(const char* new_exe_name) override
{
int pid;
mp::ProcessId pid;
int fd = m_process->spawn(new_exe_name, m_process_argv0, pid);
LogDebug(::BCLog::IPC, "Process %s pid %i launched\n", new_exe_name, pid);
auto init = m_protocol->connect(fd);

View File

@@ -32,7 +32,7 @@ namespace {
class ProcessImpl : public Process
{
public:
int spawn(const std::string& new_exe_name, const fs::path& argv0_path, int& pid) override
int spawn(const std::string& new_exe_name, const fs::path& argv0_path, mp::ProcessId& pid) override
{
return mp::SpawnProcess(pid, [&](int fd) {
fs::path path = argv0_path;
@@ -41,7 +41,7 @@ public:
return std::vector<std::string>{fs::PathToString(path), "-ipcfd", strprintf("%i", fd)};
});
}
int waitSpawned(int pid) override { return mp::WaitProcess(pid); }
int waitSpawned(mp::ProcessId pid) override { return mp::WaitProcess(pid); }
bool checkSpawned(int argc, char* argv[], int& fd) override
{
// If this process was not started with a single -ipcfd argument, it is

View File

@@ -8,6 +8,7 @@
#include <util/fs.h>
#include <memory>
#include <ipc/util.h>
#include <string>
namespace ipc {
@@ -25,10 +26,10 @@ public:
//! Spawn process and return socket file descriptor for communicating with
//! it.
virtual int spawn(const std::string& new_exe_name, const fs::path& argv0_path, int& pid) = 0;
virtual int spawn(const std::string& new_exe_name, const fs::path& argv0_path, mp::ProcessId& pid) = 0;
//! Wait for spawned process to exit and return its exit code.
virtual int waitSpawned(int pid) = 0;
virtual int waitSpawned(mp::ProcessId pid) = 0;
//! Parse command line and determine if current process is a spawned child
//! process. If so, return true and a file descriptor for communicating

21
src/ipc/util.h Normal file
View File

@@ -0,0 +1,21 @@
// 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.
#ifndef BITCOIN_IPC_UTIL_H
#define BITCOIN_IPC_UTIL_H
#include <cstdint>
#include <mp/util.h>
#include <mp/version.h>
namespace mp {
// Definitions that can be deleted when libmultiprocess subtree is updated to
// v14. Having these allows Bitcoin Core changes to be decoupled from
// libmultiprocess changes so they don't have to be reviewed in a single PR.
#if MP_MAJOR_VERSION < 14
using ProcessId = int;
#endif
} // namespace mp
#endif // BITCOIN_IPC_UTIL_H