87 lines
1.9 KiB
C++
87 lines
1.9 KiB
C++
|
/***
|
||
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
||
|
|
||
|
File: LSSignalCatcher.Linux.cpp
|
||
|
Date: 2022-8-27
|
||
|
Author: Reece
|
||
|
***/
|
||
|
#include <Source/RuntimeInternal.hpp>
|
||
|
#include "LSSignalCatcher.Linux.hpp"
|
||
|
#include "LSFromFdNonblocking.hpp"
|
||
|
|
||
|
namespace Aurora::IO::Loop
|
||
|
{
|
||
|
LSSignalCatcher::LSSignalCatcher(const AuList<int> &signals)
|
||
|
{
|
||
|
Init(signals);
|
||
|
}
|
||
|
|
||
|
LSSignalCatcher::~LSSignalCatcher()
|
||
|
{
|
||
|
if ((this->handle != 0) &&
|
||
|
(this->handle != -1))
|
||
|
{
|
||
|
::close(AuExchange(this->handle, -1));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void LSSignalCatcher::Init(const AuList<int> &signals)
|
||
|
{
|
||
|
sigset_t mask;
|
||
|
|
||
|
::sigemptyset(&mask);
|
||
|
for (const auto i : signals)
|
||
|
{
|
||
|
::sigaddset(&mask, i);
|
||
|
}
|
||
|
::sigprocmask(SIG_BLOCK, &mask, NULL);
|
||
|
|
||
|
this->handle = ::signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
|
||
|
}
|
||
|
|
||
|
void *LSSignalCatcher::GetLastSignalInfo()
|
||
|
{
|
||
|
return &this->siginfo_;
|
||
|
}
|
||
|
|
||
|
bool LSSignalCatcher::IsSignaledNonblocking()
|
||
|
{
|
||
|
return ::read(this->handle, &this->siginfo_, sizeof(this->siginfo_)) == 128;
|
||
|
}
|
||
|
|
||
|
bool LSSignalCatcher::OnTrigger(AuUInt handle)
|
||
|
{
|
||
|
return IsSignaledNonblocking();
|
||
|
}
|
||
|
|
||
|
bool LSSignalCatcher::IsSignaled()
|
||
|
{
|
||
|
return IsSignaledFromNonblockingImpl(this, this, &LSSignalCatcher::IsSignaledNonblocking);
|
||
|
}
|
||
|
|
||
|
bool LSSignalCatcher::WaitOn(AuUInt32 timeout)
|
||
|
{
|
||
|
return LSHandle::WaitOn(timeout);
|
||
|
}
|
||
|
|
||
|
ELoopSource LSSignalCatcher::GetType()
|
||
|
{
|
||
|
return ELoopSource::eSourceInternalReserved2;
|
||
|
}
|
||
|
|
||
|
AUKN_SYM AuSPtr<ILSSignalCatcher> NewLSSignalCatcher(const AuList<int> &signals)
|
||
|
{
|
||
|
auto pLS = AuMakeShared<LSSignalCatcher>(signals);
|
||
|
if (!pLS)
|
||
|
{
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
if (!pLS->HasValidHandle())
|
||
|
{
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
return pLS;
|
||
|
}
|
||
|
}
|