AuroraRuntime/Source/Processes/AuProcess.Unix.hpp
2024-01-05 09:25:48 +00:00

106 lines
3.0 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuProcess.Unix.hpp
File: Process.Linux.hpp
Date: 2021-6-12
Author: Reece
***/
#pragma once
#include <Source/IO/Loop/LSEvent.hpp>
#include <Source/IO/FS/FS.hpp>
#if defined(AURORA_IS_LINUX_DERIVED)
#include <Source/IO/UNIX/IOSubmit.Linux.hpp>
#include <Source/IO/FS/Async.Linux.hpp>
using ProcessPipeFileStream = Aurora::IO::FS::LinuxAsyncFileStream;
#else
#include <Source/IO/FS/Async.Unix.hpp>
using ProcessPipeFileStream = Aurora::IO::FS::UnixAsyncFileStream;
#endif
namespace Aurora::Processes
{
void DeinitUnix();
void InitUnix();
struct ProcessImpl :
IProcess,
IO::CompletionGroup::ICompletionGroupWorkItem
{
ProcessImpl(StartupParameters &&params);
~ProcessImpl();
void ShutdownPipes();
bool HasExited() override;
bool TryKill() override;
bool Terminate() override;
AuSPtr<Threading::IWaitable> AsWaitable() override;
AuSPtr<IO::Loop::ILoopSource> AsLoopSource() override;
AuSInt GetExitCode() override;
AuUInt GetProcessId() override;
bool Read(EStandardHandle stream, const AuMemoryViewStreamWrite &destination, bool nonblock) override;
bool Write(const AuMemoryViewStreamRead &source) override;
AuSPtr<IO::IAsyncTransaction> NewAsyncTransaction() override;
AuSPtr<IO::IAsyncTransaction> NewErrorStreamAsyncTransaction() override;
AuSPtr<IO::IIOHandle> GetOutputAndInputHandles() override;
AuSPtr<IO::IIOHandle> GetErrorStreamHandle() override;
AuSPtr<IO::IStreamReader> ToStreamReader(EStandardHandle stream) override;
AuSPtr<IO::IStreamWriter> ToStreamWriter() override;
bool TryAttachProcessExitToCompletionGroup(const AuSPtr<IO::CompletionGroup::ICompletionGroup> &pCompletionGroup) override;
IO::CompletionGroup::ICompletionGroupWorkHandle *ToCompletionGroupHandle() override;
bool HasCompletedForGCWI() override;
void CleanupForGCWI() override;
bool Start() override;
bool Init();
void ByeLol(AuSInt code);
void ForkMain();
void HookMainDeath();
private:
int pipeStdOut_[2] { -1, -1 };
int pipeStdErr_[2] { -1, -1 };
int pipeStdIn_ [2] { -1, -1 };
bool bDontRelOut_ {};
bool bDontRelIn_ {};
bool bDontRelErr_ {};
AuSPtr<IO::Loop::ILSEvent> loopSource_;
AuSPtr<IO::IIOHandle> fsHandle_;
AuSPtr<ProcessPipeFileStream> fsStream_;
AuSPtr<IO::IIOHandle> fsErrorHandle_;
AuSPtr<ProcessPipeFileStream> fsErrorStream_;
AuThreadPrimitives::EventShared_t finished_;
StartupParameters startup_;
ESpawnType type_;
AuList<const char *> cargs_;
AuString debug_;
pid_t pidt_;
bool alive_ {};
AuSInt exitCode_ {};
bool bHasExited {};
AuSPtr<IO::CompletionGroup::ICompletionGroup> pCompletionGroup_;
};
}