[+] _CLOEXEC on F-tier operating systems predating SMP and processes

This commit is contained in:
Reece Wilson 2022-12-16 03:48:04 +00:00
parent 766be57a46
commit f0fcec0cb7
16 changed files with 58 additions and 26 deletions

View File

@ -338,6 +338,7 @@ namespace Aurora
FIOConfig fio;
DebugConfig debug;
bool bFIODisableBatching {true};
bool bIOLinuxHasProcIpcPerms {false};
};
/**

View File

@ -973,6 +973,12 @@ namespace Aurora::Async
return {};
}
if (!threadState->asyncLoop->Init())
{
SysPushErrorNested();
return {};
}
if (!threadState->syncSema)
{
SysPushErrorMem();

View File

@ -309,7 +309,7 @@ namespace Aurora::IO::FS
int input, output;
if ((input = open(normalizedSrcPath.c_str(), O_RDONLY)) == -1)
if ((input = open(normalizedSrcPath.c_str(), O_RDONLY | O_CLOEXEC)) == -1)
{
return false;
}
@ -344,7 +344,7 @@ namespace Aurora::IO::FS
int input, output;
if ((input = open(normalizedSrcPath.c_str(), O_RDONLY)) == -1)
if ((input = open(normalizedSrcPath.c_str(), O_RDONLY | O_CLOEXEC)) == -1)
{
return false;
}

View File

@ -312,7 +312,7 @@ namespace Aurora::IO::FS
}
auto fileHandle = ::open(pathex.c_str(),
openMode == EFileOpenMode::eRead ? O_RDONLY : (O_RDWR | O_CREAT),
(openMode == EFileOpenMode::eRead ? O_RDONLY : (O_RDWR | O_CREAT)) | O_CLOEXEC,
0664);
if (fileHandle < 0)

View File

@ -44,7 +44,7 @@ namespace Aurora::IO::Loop
void LSEvent::Init(bool init)
{
this->handle = ::eventfd(init ? 1 : 0, EFD_NONBLOCK);
this->handle = ::eventfd(init ? 1 : 0, EFD_NONBLOCK | EFD_CLOEXEC);
}
bool LSEvent::Set()

View File

@ -45,7 +45,7 @@ namespace Aurora::IO::Loop
void LSMutex::Init()
{
handle = ::eventfd(1, EFD_NONBLOCK);
handle = ::eventfd(1, EFD_NONBLOCK | EFD_CLOEXEC );
}
bool LSMutex::Unlock()
@ -93,17 +93,17 @@ namespace Aurora::IO::Loop
AUKN_SYM AuSPtr<ILSMutex> NewLSMutex()
{
auto mutex = AuMakeShared<LSMutex>();
if (!mutex)
auto pMutex = AuMakeShared<LSMutex>();
if (!pMutex)
{
return {};
}
if (!mutex->HasValidHandle())
if (!pMutex->HasValidHandle())
{
return {};
}
return mutex;
return pMutex;
}
}

View File

@ -32,7 +32,7 @@ namespace Aurora::IO::Loop
void LSSemaphore::Init(AuUInt32 initialCount)
{
handle = ::eventfd(initialCount, EFD_SEMAPHORE | EFD_NONBLOCK);
handle = ::eventfd(initialCount, EFD_SEMAPHORE | EFD_NONBLOCK | EFD_CLOEXEC);
}
bool LSSemaphore::IsSignaledNonblocking()
@ -76,17 +76,17 @@ namespace Aurora::IO::Loop
AUKN_SYM AuSPtr<ILSSemaphore> NewLSSemaphore(AuUInt32 initialCount)
{
auto semaphore = AuMakeShared<LSSemaphore>(initialCount);
if (!semaphore)
auto pSemaphore = AuMakeShared<LSSemaphore>(initialCount);
if (!pSemaphore)
{
return {};
}
if (!semaphore->HasValidHandle())
if (!pSemaphore->HasValidHandle())
{
return {};
}
return semaphore;
return pSemaphore;
}
}

View File

@ -122,28 +122,28 @@ namespace Aurora::IO::Loop
return ok;
}
AUKN_SYM AuSPtr<ITimer> NewLSTimer(AuUInt64 absStartTimeMs, AuUInt32 reschedStepMsOrZero, AuUInt32 maxIterationsOrZero, bool bSingleshot)
AUKN_SYM AuSPtr<ITimer> NewLSTimer(AuUInt64 qwAbsStartTimeMs, AuUInt32 dwReschedStepMsOrZero, AuUInt32 dwMaxIterationsOrZero, bool bSingleshot)
{
int fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
int fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC);
if (fd == -1)
{
SysPushErrorIO();
return {};
}
auto object = AuMakeShared<LSTimer>(reschedStepMsOrZero, maxIterationsOrZero, bSingleshot, fd);
if (!object)
auto pTimer = AuMakeShared<LSTimer>(dwReschedStepMsOrZero, dwMaxIterationsOrZero, bSingleshot, fd);
if (!pTimer)
{
SysPushErrorMem();
::close(fd);
return {};
}
if (absStartTimeMs)
if (qwAbsStartTimeMs)
{
object->UpdateTime(absStartTimeMs);
pTimer->UpdateTime(qwAbsStartTimeMs);
}
return object;
return pTimer;
}
}

View File

@ -44,7 +44,7 @@ namespace Aurora::IO::Loop
bool LoopQueue::Init()
{
this->epollFd_ = epoll_create1(0);
this->epollFd_ = epoll_create1(EPOLL_CLOEXEC);
if (this->epollFd_ == -1)
{
return false;

View File

@ -185,6 +185,11 @@ namespace Aurora::IO::Loop
AuWin32CloseHandle(this->hDummy_);
}
bool LoopQueue::Init()
{
return true;
}
bool LoopQueue::IsValid()
{
return this->hEvent_ != INVALID_HANDLE_VALUE && this->handleArrayOr_.size() && this->handleArrayAnd_.size();

View File

@ -17,6 +17,8 @@ namespace Aurora::IO::Loop
LoopQueue();
~LoopQueue();
bool Init();
bool AddHook(const AuFunction<void()> &func);// override;
void PumpHooks();

View File

@ -94,6 +94,7 @@ namespace Aurora::IO::Net
bool Socket::PrepareConnectOperations()
{
(void)this->MakeCloseonexec();
return this->MakeNonblocking();
}
@ -199,6 +200,19 @@ namespace Aurora::IO::Net
return ::fcntl(this->osHandle_, F_SETFL, flags) == 0;
}
bool Socket::MakeCloseonexec()
{
int flags = ::fcntl(this->osHandle_, F_GETFL, 0);
if (flags == -1)
{
return false;
}
flags |= FD_CLOEXEC;
return ::fcntl(this->osHandle_, F_SETFL, flags) == 0;
}
void Socket::Shutdown(bool bNow)
{
if (bNow)

View File

@ -61,5 +61,7 @@ namespace Aurora::IO::Net
virtual void Shutdown(bool bNow) override;
virtual void CloseSocket() override;
bool MakeCloseonexec();
};
}

View File

@ -67,7 +67,7 @@ namespace Aurora::IO::Net
return false;
}
return this->MakeNonblocking();
return this->MakeNonblocking() && this->MakeCloseonexec();
}
bool SocketServerImpl::ImplBind()

View File

@ -253,6 +253,8 @@ namespace Aurora::IO::UNIX
SysPanic("Couldn't bind IPC socket {}", errno);
}
::fcntl(fd, F_SETFL, ::fcntl(fd, F_GETFL, 0) | FD_CLOEXEC);
if (::listen(fd, 128) == -1)
{
::close(fd);
@ -463,7 +465,7 @@ namespace Aurora::IO::UNIX
if ((platform.uKernelMajor > 5) ||
((platform.uKernelMajor == 5) && (platform.uKernelMinor >= 6)))
{
gHasProcSyscall = true;
gHasProcSyscall = gRuntimeConfig.bIOLinuxHasProcIpcPerms;
}
#endif
}

View File

@ -31,10 +31,10 @@ namespace Aurora::RNG
void EntropyInit()
{
gDevURand = ::open("/dev/urandom", O_RDONLY);
gDevURand = ::open("/dev/urandom", O_RDONLY | O_CLOEXEC);
if (gDevURand <= 0)
{
gDevURand = ::open("/dev/random", O_RDONLY);
gDevURand = ::open("/dev/random", O_RDONLY | O_CLOEXEC);
}
if (gDevURand <= 0)