AuroraRuntime/Source/IO/IPC/IPCMemory.Unix.cpp
J Reece Wilson fd0c5b51b2 Further Linux support
[+] Begin work on IO futexes for io release on process/thread exit
[+] Linux ::readdir iteration
[+] AuConsole buffering API
[*] Fix sleep as to not get interrupted by signals
[*] Switch the type of FS lock used under Linux
[*] Linux: Use new IPCHandle encoding scheme
[*] Fix undefined behaviour: unintialized timeout values (AuLoop/Linux)
[*] Fix undefined behaviour: ConsoleTTY clear line was called of a color of a random value on stack
[-] Remainings of std dir iterator
[*] Fix pthread_kill (aka send signal to pthread handle) always kills process. This is what you expect bc signal handler inheritance.
[*] Reformat the build Aurora.json file
[+] Added clang warning ignores to the build file
[*] Fix: UNIX need to use STDOUT_FILENO. Was using CRT handle in place of fd by mistake.
[+] Linux implementation for IO yield (AuIO::IOYield() - UNIX::LinuxOverlappedYield())
[*] Fix: Linux async end of stream processing. res 0 = zero bytes consumed. <= was detecting this as an error of code 0. Should succeed with zero bytes.
[+] Linux LoopQueue missing epilogue hook for the IO processor
[*] Various refactors and minor bug fixes
[*] Linux fix: Handle pipe EOS as zero
[*] Linux fix: thread termination via a user signal of 77. Need a force terminate.
[*] IPC handle: fix improper int to bool cast in the header setup within ToString
[*] Linux fix: HWInfo CPU topology regression
[-] Linux fix: remove SIGABRT handler
[*] Missing override in compression, exit, and consoletty headers.
[+] Unix Syslog logger backend
2022-08-02 05:52:57 +01:00

169 lines
4.3 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: IPCMemory.Unix.cpp
Date: 2022-4-14
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "IPC.hpp"
#include "IPCHandle.hpp"
#include "IPCMemory.Unix.hpp"
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#if defined(AURORA_IS_LINUX_DERIVED)
#include "IPCMutexFutex.Linux.hpp" /* For import */
#endif
namespace Aurora::IO::IPC
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Shared memory
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static AuString GetServerPath(const IPC::IPCToken &handle)
{
AuString path;
path += "/AURORA_";
path += AuToString(AuUInt32(handle.cookie));
path += "_";
path += AuToString(AuUInt32(handle.pid));
return path;
}
IPCSharedMemoryImpl::IPCSharedMemoryImpl(int fd, void *ptr, const IPC::IPCHandle &handle, bool owns) :
fd_(fd), base_(ptr), len_(handle.values[0].token.word), owns_(owns), handle_(handle)
{
}
IPCSharedMemoryImpl::~IPCSharedMemoryImpl()
{
if (this->base_)
{
::munmap(this->base_, this->len_);
}
int fd {-1};
if ((fd = AuExchange(this->fd_, -1)) != -1)
{
::close(fd);
}
}
Memory::MemoryViewWrite IPCSharedMemoryImpl::GetMemory()
{
return AuMemoryViewWrite(this->base_, this->len_);
}
AuUInt IPCSharedMemoryImpl::GetLength()
{
return this->len_;
}
AuString IPCSharedMemoryImpl::ExportToString()
{
return this->handle_.ToString();
}
AUKN_SYM AuSPtr<IPCSharedMemory> NewSharedMemory(AuUInt length)
{
IPC::IPCHandle handle;
IPC::IPCToken token;
token.pid = handle.pid;
token.word = length;
token.NewId();
handle.PushId(EIPCHandleType::eIPCMemory, token);
auto path = GetServerPath(token);
int fd = ::shm_open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd == -1)
{
SysPushErrorIO();
return {};
}
if (::ftruncate(fd, length) == -1)
{
SysPushErrorMem();
::close(fd);
return {};
}
auto map = ::mmap(nullptr, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED)
{
SysPushErrorIO();
::close(fd);
return {};
}
auto object = AuMakeShared<IPCSharedMemoryImpl>(fd, map, handle, true);
if (!object)
{
SysPushErrorMem();
::munmap(map, length);
::close(fd);
return {};
}
return object;
}
AuSPtr<IPCSharedMemory> ImportSharedMemoryEx(const IPCToken &token)
{
auto path = GetServerPath(token);
int fd = ::shm_open(path.c_str(), O_RDWR, S_IRUSR | S_IWUSR);
if (fd == -1)
{
SysPushErrorIO();
return {};
}
auto map = ::mmap(nullptr, token.word, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED)
{
SysPushErrorIO();
::close(fd);
return {};
}
IPC::IPCHandle handle;
handle.PushId(EIPCHandleType::eIPCMemory, token);
auto object = AuMakeShared<IPCSharedMemoryImpl>(fd, map, handle, false);
if (!object)
{
SysPushErrorMem();
::munmap(map, token.word);
::close(fd);
return {};
}
return object;
}
AUKN_SYM AuSPtr<IPCSharedMemory> ImportSharedMemory(const AuString &handleString)
{
IPC::IPCHandle handle;
if (!handle.FromString(handleString))
{
SysPushErrorParseError("Invalid handle: {}", handleString);
return {};
}
auto val = handle.GetToken(IPC::EIPCHandleType::eIPCMemory, 0);
if (!val)
{
SysPushErrorParseError("Invalid handle: {}", handleString);
return {};
}
return ImportSharedMemoryEx(val->token);
}
}