summaryrefslogtreecommitdiff
path: root/libartbase/base/memfd.cc
diff options
context:
space:
mode:
Diffstat (limited to 'libartbase/base/memfd.cc')
-rw-r--r--libartbase/base/memfd.cc24
1 files changed, 24 insertions, 0 deletions
diff --git a/libartbase/base/memfd.cc b/libartbase/base/memfd.cc
index 780be328af..c8ec18da9b 100644
--- a/libartbase/base/memfd.cc
+++ b/libartbase/base/memfd.cc
@@ -19,6 +19,7 @@
#include <errno.h>
#include <stdio.h>
#if !defined(_WIN32)
+#include <fcntl.h>
#include <sys/syscall.h>
#include <sys/utsname.h>
#include <unistd.h>
@@ -67,4 +68,27 @@ int memfd_create(const char* name ATTRIBUTE_UNUSED, unsigned int flags ATTRIBUTE
#endif // __NR_memfd_create
+// This is a wrapper that will attempt to simulate memfd_create if normal running fails.
+int memfd_create_compat(const char* name, unsigned int flags) {
+ int res = memfd_create(name, flags);
+ if (res >= 0) {
+ return res;
+ }
+#if !defined(_WIN32)
+ // Try to create an anonymous file with tmpfile that we can use instead.
+ if (flags == 0) {
+ FILE* file = tmpfile();
+ if (file != nullptr) {
+ // We want the normal 'dup' semantics since memfd_create without any flags isn't CLOEXEC.
+ // Unfortunately on some android targets we will compiler error if we use dup directly and so
+ // need to use fcntl.
+ int nfd = fcntl(fileno(file), F_DUPFD);
+ fclose(file);
+ return nfd;
+ }
+ }
+#endif
+ return res;
+}
+
} // namespace art