diff options
author | Michael Hoisie <hoisie@google.com> | 2024-03-18 23:53:30 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2024-03-18 23:53:30 +0000 |
commit | e0cd651de7d699c9a5216503de4597f885c1099b (patch) | |
tree | 8581a93ef362a79ac403414bf0c16527f30157a2 | |
parent | f7d69c6e54bcd7260a3fb56b3317bb45f8bed604 (diff) | |
parent | 03adc99ce32e035b8b7c73c252b9c59ef22f010a (diff) | |
download | core-android12-hostruntime-dev.tar.gz |
Merge "Add support for ashmem-host for host Windows" into android12-hostruntime-devandroid12-hostruntime-dev
-rw-r--r-- | libcutils/Android.bp | 12 | ||||
-rw-r--r-- | libcutils/OWNERS | 1 | ||||
-rw-r--r-- | libcutils/ashmem-host.cpp | 55 |
3 files changed, 42 insertions, 26 deletions
diff --git a/libcutils/Android.bp b/libcutils/Android.bp index 68b21c6a0..94f80bb9f 100644 --- a/libcutils/Android.bp +++ b/libcutils/Android.bp @@ -170,20 +170,20 @@ cc_library { linux_bionic: { enabled: true, }, + host: { + srcs: [ + "ashmem-host.cpp", + "trace-host.cpp", + ], + }, not_windows: { srcs: libcutils_nonwindows_sources + [ - "ashmem-host.cpp", "fs_config.cpp", - "trace-host.cpp", ], }, windows: { host_ldlibs: ["-lws2_32"], - srcs: [ - "trace-host.cpp", - ], - enabled: true, cflags: [ "-D_GNU_SOURCE", diff --git a/libcutils/OWNERS b/libcutils/OWNERS index 7529cb920..e1cbe4aaf 100644 --- a/libcutils/OWNERS +++ b/libcutils/OWNERS @@ -1 +1,2 @@ +# Bug component: 128577 include platform/system/core:/janitors/OWNERS diff --git a/libcutils/ashmem-host.cpp b/libcutils/ashmem-host.cpp index 2ba1eb0c3..9003b76c9 100644 --- a/libcutils/ashmem-host.cpp +++ b/libcutils/ashmem-host.cpp @@ -17,10 +17,13 @@ #include <cutils/ashmem.h> /* - * Implementation of the user-space ashmem API for the simulator, which lacks - * an ashmem-enabled kernel. See ashmem-dev.c for the real ashmem-based version. + * Implementation of the user-space ashmem API for the simulator, which lacks an + * ashmem-enabled kernel. See ashmem-dev.c for the real ashmem-based version. A + * disk-backed temp file is the best option that is consistently supported + * across all host platforms. */ +#include <android-base/unique_fd.h> #include <errno.h> #include <fcntl.h> #include <limits.h> @@ -31,8 +34,10 @@ #include <sys/types.h> #include <time.h> #include <unistd.h> - #include <utils/Compat.h> +#include <memory> + +using android::base::unique_fd; static bool ashmem_validate_stat(int fd, struct stat* buf) { int result = fstat(fd, buf); @@ -40,15 +45,20 @@ static bool ashmem_validate_stat(int fd, struct stat* buf) { return false; } - /* - * Check if this is an "ashmem" region. - * TODO: This is very hacky, and can easily break. - * We need some reliable indicator. - */ - if (!(buf->st_nlink == 0 && S_ISREG(buf->st_mode))) { + // Check if this is an ashmem region. Since there's no such thing on the host, + // we can't actually implement that. Check that it's at least a regular file. + if (!S_ISREG(buf->st_mode)) { errno = ENOTTY; return false; } + // In Win32, unlike Unix, the temp file is not unlinked immediately after + // creation. +#if !defined(_WIN32) + if (buf->st_nlink != 0) { + errno = ENOTTY; + return false; + } +#endif return true; } @@ -58,19 +68,24 @@ int ashmem_valid(int fd) { } int ashmem_create_region(const char* /*ignored*/, size_t size) { - char pattern[PATH_MAX]; - snprintf(pattern, sizeof(pattern), "/tmp/android-ashmem-%d-XXXXXXXXX", getpid()); - int fd = mkstemp(pattern); - if (fd == -1) return -1; - - unlink(pattern); + // Files returned by tmpfile are automatically removed. + std::unique_ptr<FILE, decltype(&fclose)> tmp(tmpfile(), &fclose); - if (TEMP_FAILURE_RETRY(ftruncate(fd, size)) == -1) { - close(fd); - return -1; + if (!tmp) { + return -1; } - - return fd; + int fd = fileno(tmp.get()); + if (fd == -1) { + return -1; + } + unique_fd dupfd = unique_fd(dup(fd)); + if (dupfd == -1) { + return -1; + } + if (TEMP_FAILURE_RETRY(ftruncate(dupfd, size)) == -1) { + return -1; + } + return dupfd.release(); } int ashmem_set_prot_region(int /*fd*/, int /*prot*/) { |