From 8fe83de42fd7c14e038cda4da9f54b03e4b97bc6 Mon Sep 17 00:00:00 2001 From: J Reece Wilson Date: Wed, 6 Apr 2022 02:24:38 +0100 Subject: [PATCH] [*] Linux has a pulse... --- Source/Console/ConsoleStd/ConsoleStd.cpp | 4 +-- Source/Debug/ExceptionWatcher.Unix.cpp | 10 ++++++ Source/IO/FS/FS.Unix.cpp | 46 ++++++++++++++++-------- Source/IO/FS/FileStream.Unix.cpp | 36 +++++++++++-------- Source/Loop/LSEvent.Linux.cpp | 4 +-- Source/Processes/Process.Unix.cpp | 15 +++++++- Source/Telemetry/Telemetry.cpp | 7 ++-- 7 files changed, 86 insertions(+), 36 deletions(-) diff --git a/Source/Console/ConsoleStd/ConsoleStd.cpp b/Source/Console/ConsoleStd/ConsoleStd.cpp index d1dd5c8e..a7449688 100644 --- a/Source/Console/ConsoleStd/ConsoleStd.cpp +++ b/Source/Console/ConsoleStd/ConsoleStd.cpp @@ -198,8 +198,8 @@ namespace Aurora::Console::ConsoleStd #endif - SysAssert(gInputStream, "Couldn't allocate input stream handler"); - SysAssert(gOutputStream, "Couldn't allocate output stream handler"); + SysAssert(gInputStream != DEFAULT_HANDLE_VAL, "Couldn't allocate input stream handler"); + SysAssert(gOutputStream != DEFAULT_HANDLE_VAL, "Couldn't allocate output stream handler"); StartLogger(); } diff --git a/Source/Debug/ExceptionWatcher.Unix.cpp b/Source/Debug/ExceptionWatcher.Unix.cpp index 54a85993..fa62ecdd 100644 --- a/Source/Debug/ExceptionWatcher.Unix.cpp +++ b/Source/Debug/ExceptionWatcher.Unix.cpp @@ -5,3 +5,13 @@ Date: 2021-6-12 Author: Reece ***/ +#include +#include "ExceptionWatcher.Unix.hpp" + +namespace Aurora::Debug +{ + void PlatformHandleFatal(bool fatal) + { + + } +} \ No newline at end of file diff --git a/Source/IO/FS/FS.Unix.cpp b/Source/IO/FS/FS.Unix.cpp index 46fd40f7..f9f8eab0 100644 --- a/Source/IO/FS/FS.Unix.cpp +++ b/Source/IO/FS/FS.Unix.cpp @@ -38,19 +38,19 @@ namespace Aurora::IO::FS return IterateDirEntriesSTL(string, false, dirs); } - AUKN_SYM bool WriteFile(const AuString &path, const void *data, AuUInt length) + AUKN_SYM bool WriteFile(const AuString &path, const Memory::MemoryViewRead &blob) { auto file = OpenWriteUnique(path); SysCheckReturn(file, false); - AuUInt written = length; - if (!file->Write(Memory::MemoryViewStreamRead(data, written))) + AuUInt written = blob.length; + if (!file->Write(Memory::MemoryViewStreamRead(blob, written))) { SysPushErrorMem(); return false; } - if (written != length) + if (written != blob.length) { SysPushErrorIO(); return false; @@ -59,32 +59,48 @@ namespace Aurora::IO::FS return false; } - AUKN_SYM bool ReadFile(const AuString &path, AuList &buffer) + AUKN_SYM bool ReadFile(const AuString &path, AuByteBuffer &buffer) { + AuUInt read; + auto file = OpenReadUnique(path); SysCheckReturn(file, false); auto len = file->GetLength(); + + // NOTE: Linux file systems are such a cluster fuck of unimplemented interface + // It's not unusual for these sockets to be unstreamable across NIX-like oses + if (len == 0) + { + if (AuStartsWith(path, "/proc/") || + AuStartsWith(path, "/sys/") || + AuStartsWith(path, "/dev/")) + { + len = 4096; + } + else + { + buffer.clear(); + return true; + } + } + if (!AuTryResize(buffer, len)) { SysPushErrorMem(); return false; } - - AuUInt read; - if (!file->Read(Memory::MemoryViewStreamWrite {buffer, read})) + + if (!file->Read(Memory::MemoryViewStreamWrite {buffer.begin(), buffer.end(), read})) { SysPushErrorIO(); return false; } + + // NOTE: File devices love to lie + // Do not entertain an arbitrarily large page length provided by non-regular fds - if (read != buffer.size()) - { - SysPushErrorIO(); - return false; - } - - return false; + return AuTryResize(buffer, read); } static bool UnixExists(const AuString &path, bool dir) diff --git a/Source/IO/FS/FileStream.Unix.cpp b/Source/IO/FS/FileStream.Unix.cpp index 6e224618..ea3aaab1 100644 --- a/Source/IO/FS/FileStream.Unix.cpp +++ b/Source/IO/FS/FileStream.Unix.cpp @@ -101,7 +101,7 @@ namespace Aurora::IO::FS return true; } - + static bool PosixWrite(int fd, const void *buf, AuUInt32 count, AuUInt32 *pWritten) { auto ret = write(fd, buf, count); @@ -294,16 +294,17 @@ namespace Aurora::IO::FS this->bMadeTemporary = true; } - static IFileStream *OpenNew(const AuString &path, bool read) + static IFileStream *OpenNew(const AuString &path, EFileOpenMode openMode, EFileAdvisoryLockLevel lock) { auto pathex = NormalizePathRet(path); - if (!read) + if (openMode != EFileOpenMode::eRead) { CreateDirectories(pathex, true); } - auto fileHandle = open(pathex.c_str(), read ? O_RDONLY : O_RDWR); + auto fileHandle = open(pathex.c_str(), + openMode == EFileOpenMode::eRead ? O_RDONLY : O_RDWR); if (fileHandle < 0) { @@ -315,34 +316,41 @@ namespace Aurora::IO::FS if (!stream) { ::close(fileHandle); + SysPushErrorMem("{}", path); return nullptr; } if (!stream->Init(fileHandle, pathex)) { delete stream; + SysPushErrorGeneric("{}", path); return nullptr; } return stream; } - AUKN_SYM IFileStream *OpenReadNew(const AuString &path) - { - return OpenNew(path, true); - } - - AUKN_SYM void OpenReadRelease(IFileStream *that) + AUKN_SYM void OpenRelease(IFileStream *that) { AuSafeDelete(that); } - AUKN_SYM IFileStream *OpenWriteNew(const AuString &path) + AUKN_SYM IFileStream *OpenReadNew(const AuString &path, EFileAdvisoryLockLevel level) { - return OpenNew(path, false); + return OpenNew(path, EFileOpenMode::eRead, level); } - - AUKN_SYM void OpenWriteRelease(IFileStream * that) + + AUKN_SYM void OpenReadRelease(IFileStream * that) + { + AuSafeDelete(that); + } + + AUKN_SYM IFileStream *OpenWriteNew(const AuString &path, EFileAdvisoryLockLevel level) + { + return OpenNew(path, EFileOpenMode::eWrite, level); + } + + AUKN_SYM void OpenWriteRelease(IFileStream *that) { AuSafeDelete(that); } diff --git a/Source/Loop/LSEvent.Linux.cpp b/Source/Loop/LSEvent.Linux.cpp index d30efd4a..f9a7d387 100755 --- a/Source/Loop/LSEvent.Linux.cpp +++ b/Source/Loop/LSEvent.Linux.cpp @@ -92,9 +92,9 @@ namespace Aurora::Loop return ELoopSource::eSourceEvent; } - AUKN_SYM AuSPtr NewLSEvent() + AUKN_SYM AuSPtr NewLSEvent(bool triggered, bool atomicRelease, bool permitMultipleTriggers) { - auto event = AuMakeShared(); + auto event = AuMakeShared(triggered, atomicRelease, permitMultipleTriggers); if (!event) { return {}; diff --git a/Source/Processes/Process.Unix.cpp b/Source/Processes/Process.Unix.cpp index 8fdd80cd..26ead570 100644 --- a/Source/Processes/Process.Unix.cpp +++ b/Source/Processes/Process.Unix.cpp @@ -330,12 +330,25 @@ namespace Aurora::Processes void InitUnix() { - signal(SIGCHLD, SigChldHandler); gRWLock = AuThreadPrimitives::RWLockUnique(); + + struct sigaction action = + { + .sa_handler = SigChldHandler, + .sa_flags = SA_ONSTACK + }; + sigaction(SIGCHLD, &action, nullptr); } void DeinitUnix() { + struct sigaction action = + { + .sa_handler = SIG_DFL, + .sa_flags = SA_ONSTACK | SA_NOCLDWAIT + }; + sigaction(SIGCHLD, &action, nullptr); + gRWLock.reset(); } } \ No newline at end of file diff --git a/Source/Telemetry/Telemetry.cpp b/Source/Telemetry/Telemetry.cpp index 9b3347a7..cc47571b 100644 --- a/Source/Telemetry/Telemetry.cpp +++ b/Source/Telemetry/Telemetry.cpp @@ -12,26 +12,29 @@ namespace Aurora::Telemetry { + static bool bHasInit {}; static AuThreadPrimitives::CriticalSectionUnique_t gGroupLock; void BeginBlock() { + if (!bHasInit) return; gGroupLock->Lock(); } void EndBlock() { + if (!bHasInit) return; gGroupLock->Unlock(); } void InsertOSError(const Debug::OSError_t &osError) { - + if (!bHasInit) return; } void InsertMsgError(const Debug::LastError &error) { - + if (!bHasInit) return; } void InsertCError(AuSInt cError)