2022-04-15 10:01:43 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-12-14 05:03:37 +00:00
|
|
|
File: AuIPCMemory.Unix.cpp
|
2022-04-15 10:01:43 +00:00
|
|
|
Date: 2022-4-14
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "IPC.hpp"
|
2022-12-14 05:03:37 +00:00
|
|
|
#include "AuIPCHandle.hpp"
|
|
|
|
#include "AuIPCMemory.Unix.hpp"
|
2022-04-15 10:01:43 +00:00
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h> /* For mode constants */
|
|
|
|
#include <fcntl.h> /* For O_* constants */
|
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
#if defined(AURORA_IS_LINUX_DERIVED)
|
2022-12-16 00:41:01 +00:00
|
|
|
#include "AuIPCMutexFutex.Linux.hpp" /* For import */
|
2022-08-02 04:52:17 +00:00
|
|
|
#endif
|
|
|
|
|
2022-06-11 23:52:46 +00:00
|
|
|
namespace Aurora::IO::IPC
|
2022-04-15 10:01:43 +00:00
|
|
|
{
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Shared memory
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2022-08-10 11:12:36 +00:00
|
|
|
AuString GetServerPath(const IPC::IPCToken &handle)
|
2022-04-15 10:01:43 +00:00
|
|
|
{
|
|
|
|
AuString path;
|
|
|
|
path += "/AURORA_";
|
|
|
|
path += AuToString(AuUInt32(handle.cookie));
|
|
|
|
path += "_";
|
|
|
|
path += AuToString(AuUInt32(handle.pid));
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2022-12-14 05:03:37 +00:00
|
|
|
IPCSharedMemoryImpl::IPCSharedMemoryImpl(int fd,
|
|
|
|
void *pBase,
|
|
|
|
const IPC::IPCHandle &handle,
|
|
|
|
bool bOwns) :
|
|
|
|
fd_(fd),
|
2022-12-16 00:41:01 +00:00
|
|
|
pBase_(pBase),
|
2022-12-14 05:03:37 +00:00
|
|
|
uLen_(handle.values[0].token.word),
|
|
|
|
bOwns_(bOwns),
|
|
|
|
handle_(handle)
|
2022-04-15 10:01:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
IPCSharedMemoryImpl::~IPCSharedMemoryImpl()
|
|
|
|
{
|
2022-12-14 05:03:37 +00:00
|
|
|
if (this->pBase_)
|
2022-04-15 10:01:43 +00:00
|
|
|
{
|
2022-12-14 05:03:37 +00:00
|
|
|
::munmap(this->pBase_, this->uLen_);
|
2022-04-15 10:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int fd {-1};
|
|
|
|
if ((fd = AuExchange(this->fd_, -1)) != -1)
|
|
|
|
{
|
|
|
|
::close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Memory::MemoryViewWrite IPCSharedMemoryImpl::GetMemory()
|
|
|
|
{
|
2022-12-14 05:03:37 +00:00
|
|
|
return AuMemoryViewWrite(this->pBase_, this->uLen_);
|
2022-04-15 10:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt IPCSharedMemoryImpl::GetLength()
|
|
|
|
{
|
2022-12-14 05:03:37 +00:00
|
|
|
return this->uLen_;
|
2022-04-15 10:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuString IPCSharedMemoryImpl::ExportToString()
|
|
|
|
{
|
|
|
|
return this->handle_.ToString();
|
|
|
|
}
|
|
|
|
|
2022-12-14 05:03:37 +00:00
|
|
|
AUKN_SYM AuSPtr<IPCSharedMemory> NewSharedMemory(AuUInt uLength)
|
2022-04-15 10:01:43 +00:00
|
|
|
{
|
|
|
|
IPC::IPCHandle handle;
|
2022-08-02 04:52:17 +00:00
|
|
|
IPC::IPCToken token;
|
|
|
|
token.pid = handle.pid;
|
2022-12-14 05:03:37 +00:00
|
|
|
token.word = uLength;
|
2022-08-02 04:52:17 +00:00
|
|
|
token.NewId();
|
2022-04-15 10:01:43 +00:00
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
handle.PushId(EIPCHandleType::eIPCMemory, token);
|
|
|
|
|
|
|
|
auto path = GetServerPath(token);
|
2022-04-15 10:01:43 +00:00
|
|
|
int fd = ::shm_open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-14 05:03:37 +00:00
|
|
|
if (::ftruncate(fd, uLength) == -1)
|
2022-04-15 10:01:43 +00:00
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
::close(fd);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-14 05:03:37 +00:00
|
|
|
auto map = ::mmap(nullptr, uLength, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
2022-04-15 10:01:43 +00:00
|
|
|
if (map == MAP_FAILED)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
::close(fd);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-14 05:03:37 +00:00
|
|
|
auto pObject = AuMakeShared<IPCSharedMemoryImpl>(fd, map, handle, true);
|
|
|
|
if (!pObject)
|
2022-04-15 10:01:43 +00:00
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
2022-12-14 05:03:37 +00:00
|
|
|
::munmap(map, uLength);
|
2022-04-15 10:01:43 +00:00
|
|
|
::close(fd);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-14 05:03:37 +00:00
|
|
|
return pObject;
|
2022-04-15 10:01:43 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
AuSPtr<IPCSharedMemory> ImportSharedMemoryEx(const IPCToken &token)
|
2022-04-15 10:01:43 +00:00
|
|
|
{
|
2022-08-02 04:52:17 +00:00
|
|
|
auto path = GetServerPath(token);
|
2022-04-15 10:01:43 +00:00
|
|
|
int fd = ::shm_open(path.c_str(), O_RDWR, S_IRUSR | S_IWUSR);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
auto map = ::mmap(nullptr, token.word, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
2022-04-15 10:01:43 +00:00
|
|
|
if (map == MAP_FAILED)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
::close(fd);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
IPC::IPCHandle handle;
|
|
|
|
handle.PushId(EIPCHandleType::eIPCMemory, token);
|
|
|
|
|
2022-12-14 05:03:37 +00:00
|
|
|
auto pObject = AuMakeShared<IPCSharedMemoryImpl>(fd, map, handle, false);
|
|
|
|
if (!pObject)
|
2022-04-15 10:01:43 +00:00
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
2022-08-02 04:52:17 +00:00
|
|
|
::munmap(map, token.word);
|
2022-04-15 10:01:43 +00:00
|
|
|
::close(fd);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-14 05:03:37 +00:00
|
|
|
return pObject;
|
2022-04-15 10:01:43 +00:00
|
|
|
}
|
2022-08-02 04:52:17 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2022-04-15 10:01:43 +00:00
|
|
|
}
|