rpcsx/rpcsx-os/iodev/shm.cpp

67 lines
1.8 KiB
C++
Raw Normal View History

2023-07-29 21:46:28 +02:00
#include "io-device.hpp"
#include "orbis/KernelAllocator.hpp"
#include "orbis/error/ErrorCode.hpp"
#include "orbis/file.hpp"
#include "orbis/thread/Thread.hpp"
2023-07-29 21:46:28 +02:00
#include "orbis/utils/Logs.hpp"
#include <fcntl.h>
#include <sys/mman.h>
struct ShmDevice : IoDevice {
orbis::ErrorCode open(orbis::Ref<orbis::File> *file, const char *path,
std::uint32_t flags, std::uint32_t mode,
orbis::Thread *thread) override;
2023-11-11 23:27:51 +01:00
orbis::ErrorCode unlink(const char *path, bool recursive,
orbis::Thread *thread) override;
2023-07-29 21:46:28 +02:00
};
2023-11-11 23:27:51 +01:00
static std::string realShmPath(const char *path) {
2023-07-29 21:46:28 +02:00
std::string name = "/rpcsx-";
2023-11-10 19:30:43 +01:00
if (path[0] == '/') {
2023-07-29 21:46:28 +02:00
name += path + 1;
} else {
name += path;
}
2023-11-11 23:27:51 +01:00
for (auto pos = name.find('/', 1); pos != std::string::npos;
pos = name.find('/', pos + 1)) {
2023-11-10 19:30:43 +01:00
name[pos] = '$';
}
2023-11-11 23:27:51 +01:00
return name;
}
orbis::ErrorCode ShmDevice::open(orbis::Ref<orbis::File> *file,
const char *path, std::uint32_t flags,
std::uint32_t mode, orbis::Thread *thread) {
ORBIS_LOG_WARNING("shm_open", path, flags, mode);
auto name = realShmPath(path);
2023-07-29 21:46:28 +02:00
auto realFlags = O_RDWR; // TODO
if (flags & 0x200) {
realFlags |= O_CREAT;
2023-07-29 21:46:28 +02:00
}
int fd = shm_open(name.c_str(), realFlags, S_IRUSR | S_IWUSR);
if (fd < 0) {
2023-11-11 18:31:12 +01:00
return convertErrno();
2023-07-29 21:46:28 +02:00
}
auto hostFile = createHostFile(fd, this, true);
2023-07-29 21:46:28 +02:00
*file = hostFile;
return {};
}
2023-11-11 23:27:51 +01:00
orbis::ErrorCode ShmDevice::unlink(const char *path, bool recursive,
orbis::Thread *thread) {
ORBIS_LOG_WARNING("shm_unlink", path);
auto name = realShmPath(path);
if (shm_unlink(name.c_str())) {
return convertErrno();
}
return{};
}
2023-07-29 21:46:28 +02:00
IoDevice *createShmDevice() { return orbis::knew<ShmDevice>(); }