[*] Other POSIX systems (non-linux) are probably going to need this improved eSpawnChildProcessWorker awareness

This commit is contained in:
Reece Wilson 2023-12-23 03:56:09 +00:00
parent 2fd41d4142
commit 9393c17564
3 changed files with 44 additions and 5 deletions

View File

@ -12,6 +12,12 @@
#if defined(AURORA_IS_POSIX_DERIVED) #if defined(AURORA_IS_POSIX_DERIVED)
#include "AuExit.Unix.hpp" #include "AuExit.Unix.hpp"
namespace Aurora::Processes
{
void PosixProcessShutdown();
}
#endif #endif
#include <Source/Debug/MemoryCrunch.hpp> #include <Source/Debug/MemoryCrunch.hpp>
@ -50,6 +56,13 @@ namespace Aurora::Exit
} }
} }
static void UnsafeProcessShutdownHook()
{
#if defined(AURORA_IS_POSIX_DERIVED)
Processes::PosixProcessShutdown();
#endif
}
void PostLevel(AuThreads::IAuroraThread *thread, ETriggerLevel level) void PostLevel(AuThreads::IAuroraThread *thread, ETriggerLevel level)
{ {
AU_DEBUG_MEMCRUNCH; AU_DEBUG_MEMCRUNCH;
@ -121,6 +134,13 @@ namespace Aurora::Exit
gHasSentTerminate |= isTerminate; gHasSentTerminate |= isTerminate;
} }
if ((level == ETriggerLevel::eSigTerminate && !gHasCanceled) ||
level == ETriggerLevel::eFatalException ||
level == ETriggerLevel::eSafeTermination)
{
UnsafeProcessShutdownHook();
}
// Force exit after calling the subscribers, should the level be eSigTerminate // Force exit after calling the subscribers, should the level be eSigTerminate
if (level == ETriggerLevel::eSigTerminate) if (level == ETriggerLevel::eSigTerminate)
{ {

View File

@ -50,7 +50,7 @@
namespace Aurora::Processes namespace Aurora::Processes
{ {
static AuThreadPrimitives::RWLockUnique_t gRWLock; static AuRWLock gRWLock;
static AuHashMap<pid_t, ProcessImpl *> gPidLookupMap; static AuHashMap<pid_t, ProcessImpl *> gPidLookupMap;
struct ProcessAliveLoopSource : AuLoop::LSEvent struct ProcessAliveLoopSource : AuLoop::LSEvent
@ -715,7 +715,15 @@ namespace Aurora::Processes
return true; return true;
} }
void ProcessImpl::HookMainDeath()
{
if (this->type_ == ESpawnType::eSpawnChildProcessWorker)
{
this->Terminate();
}
}
AUKN_SYM IProcess *SpawnNew(StartupParameters &&params) AUKN_SYM IProcess *SpawnNew(StartupParameters &&params)
{ {
auto ret = _new ProcessImpl(AuMove(params)); auto ret = _new ProcessImpl(AuMove(params));
@ -778,8 +786,6 @@ namespace Aurora::Processes
void InitUnix() void InitUnix()
{ {
gRWLock = AuThreadPrimitives::RWLockUnique();
struct sigaction action = struct sigaction action =
{ {
.sa_handler = SigChldHandler, .sa_handler = SigChldHandler,
@ -790,6 +796,18 @@ namespace Aurora::Processes
::sigaction(SIGCHLD, &action, nullptr); ::sigaction(SIGCHLD, &action, nullptr);
} }
void PosixProcessShutdown()
{
#if !defined(AURORA_IS_LINUX_DERIVED)
AU_LOCK_GUARD(gRWLock->AsReadable());
for (const auto &[pid, pProcess] : gPidLookupMap)
{
pProcess->Terminate();
}
#endif
}
void DeinitUnix() void DeinitUnix()
{ {
struct sigaction action = struct sigaction action =
@ -801,6 +819,6 @@ namespace Aurora::Processes
::sigemptyset(&action.sa_mask); ::sigemptyset(&action.sa_mask);
::sigaction(SIGCHLD, &action, nullptr); ::sigaction(SIGCHLD, &action, nullptr);
gRWLock.reset(); PosixProcessShutdown();
} }
} }

View File

@ -59,6 +59,7 @@ namespace Aurora::Processes
void ByeLol(AuSInt code); void ByeLol(AuSInt code);
void ForkMain(); void ForkMain();
void HookMainDeath();
private: private:
int pipeStdOut_[2] { -1, -1 }; int pipeStdOut_[2] { -1, -1 };