LibCore: Support launching a singleton process with an IPC connection

This largely adapts the code from SingletonProcess.cpp to work a bit
closer with Core::Process. Ideally, we'll move the daemonizing feature
into Core::Process::disown() eventually.
This commit is contained in:
Timothy Flynn 2024-04-23 18:53:00 -04:00 committed by Andrew Kaster
commit c723fc9611
Notes: sideshowbarker 2024-07-17 09:47:09 +09:00
2 changed files with 148 additions and 0 deletions

View file

@ -44,6 +44,8 @@ struct ProcessSpawnOptions {
Vector<FileActionType> const& file_actions {};
};
class IPCProcess;
class Process {
AK_MAKE_NONCOPYABLE(Process);
@ -98,6 +100,8 @@ public:
ErrorOr<bool> wait_for_termination();
private:
friend IPCProcess;
Process(pid_t pid)
: m_pid(pid)
, m_should_disown(true)
@ -125,6 +129,15 @@ public:
return ProcessAndIPCClient<ClientType> { move(process), move(client) };
}
template<typename ClientType, typename... ClientArguments>
static ErrorOr<ProcessAndIPCClient<ClientType>> spawn_singleton(ProcessSpawnOptions const& options, ClientArguments&&... client_arguments)
{
auto [process, socket] = TRY(spawn_singleton_and_connect_to_process(options));
auto client = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ClientType { move(socket), forward<ClientArguments>(client_arguments)... }));
return ProcessAndIPCClient<ClientType> { move(process), move(client) };
}
pid_t pid() const { return m_process.pid(); }
private:
@ -133,6 +146,7 @@ private:
NonnullOwnPtr<Core::LocalSocket> m_ipc_socket;
};
static ErrorOr<ProcessAndIPCSocket> spawn_and_connect_to_process(ProcessSpawnOptions const& options);
static ErrorOr<ProcessAndIPCSocket> spawn_singleton_and_connect_to_process(ProcessSpawnOptions const& options);
Process m_process;
};