forked from ShuriZma/suyu
1
0
Fork 0

Merge pull request #48 from linkmauve/master

Replace insecure temporary file creation with devshm.
This commit is contained in:
bunnei 2014-08-17 20:33:47 -04:00
commit 54d5d4477f
1 changed files with 23 additions and 24 deletions

View File

@ -19,6 +19,7 @@
#include "common/memory_util.h" #include "common/memory_util.h"
#include "common/mem_arena.h" #include "common/mem_arena.h"
#include "common/string_util.h"
#ifndef _WIN32 #ifndef _WIN32
#include <sys/stat.h> #include <sys/stat.h>
@ -98,15 +99,7 @@ int ashmem_unpin_region(int fd, size_t offset, size_t len)
#endif // Android #endif // Android
#if defined(_WIN32) && !defined(_XBOX)
#ifndef _WIN32
// do not make this "static"
#if defined(MAEMO) || defined(MEEGO_EDITION_HARMATTAN)
std::string ram_temp_file = "/home/user/gc_mem.tmp";
#else
std::string ram_temp_file = "/tmp/gc_mem.tmp";
#endif
#elif !defined(_XBOX)
SYSTEM_INFO sysInfo; SYSTEM_INFO sysInfo;
#endif #endif
@ -145,20 +138,26 @@ void MemArena::GrabLowMemSpace(size_t size)
return; return;
} }
#else #else
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // Try to find a non-existing filename for our shared memory.
fd = open(ram_temp_file.c_str(), O_RDWR | O_CREAT, mode); // In most cases the first one will be available, but it's nicer to search
if (fd < 0) // a bit more.
for (int i = 0; i < 10000; i++)
{ {
ERROR_LOG(MEMMAP, "Failed to grab memory space as a file: %s of size: %08x errno: %d", ram_temp_file.c_str(), (int)size, (int)(errno)); std::string file_name = StringFromFormat("/citramem.%d", i);
return; fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd != -1)
{
shm_unlink(file_name.c_str());
break;
}
else if (errno != EEXIST)
{
ERROR_LOG(MEMMAP, "shm_open failed: %s", strerror(errno));
return;
}
} }
// delete immediately, we keep the fd so it still lives if (ftruncate(fd, size) < 0)
unlink(ram_temp_file.c_str()); ERROR_LOG(MEMMAP, "Failed to allocate low memory space");
if (ftruncate(fd, size) != 0)
{
ERROR_LOG(MEMMAP, "Failed to ftruncate %d to size %08x", (int)fd, (int)size);
}
return;
#endif #endif
} }
@ -198,12 +197,12 @@ void *MemArena::CreateView(s64 offset, size_t size, void *base)
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
MAP_NOSYNC | MAP_NOSYNC |
#endif #endif
((base == 0) ? 0 : MAP_FIXED), fd, offset); ((base == nullptr) ? 0 : MAP_FIXED), fd, offset);
if (retval == MAP_FAILED) if (retval == MAP_FAILED)
{ {
NOTICE_LOG(MEMMAP, "mmap on %s (fd: %d) failed", ram_temp_file.c_str(), (int)fd); NOTICE_LOG(MEMMAP, "mmap failed");
return 0; return nullptr;
} }
return retval; return retval;
#endif #endif