The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 17 | #include <cutils/ashmem.h> |
| 18 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | /* |
Michael Hoisie | 7c4beee | 2024-01-12 19:18:19 +0000 | [diff] [blame] | 20 | * Implementation of the user-space ashmem API for the simulator, which lacks an |
| 21 | * ashmem-enabled kernel. See ashmem-dev.c for the real ashmem-based version. A |
| 22 | * disk-backed temp file is the best option that is consistently supported |
| 23 | * across all host platforms. |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
Michael Hoisie | 7c4beee | 2024-01-12 19:18:19 +0000 | [diff] [blame] | 26 | #include <android-base/unique_fd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | #include <errno.h> |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 28 | #include <fcntl.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | #include <limits.h> |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <time.h> |
| 36 | #include <unistd.h> |
Elliott Hughes | 2004a86 | 2015-02-09 11:41:07 -0800 | [diff] [blame] | 37 | #include <utils/Compat.h> |
Michael Hoisie | 7c4beee | 2024-01-12 19:18:19 +0000 | [diff] [blame] | 38 | #include <memory> |
| 39 | |
| 40 | using android::base::unique_fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | |
Steven Moreland | c066066 | 2019-09-04 17:48:32 -0700 | [diff] [blame] | 42 | static bool ashmem_validate_stat(int fd, struct stat* buf) { |
| 43 | int result = fstat(fd, buf); |
| 44 | if (result == -1) { |
| 45 | return false; |
| 46 | } |
| 47 | |
Michael Hoisie | 7c4beee | 2024-01-12 19:18:19 +0000 | [diff] [blame] | 48 | // Check if this is an ashmem region. Since there's no such thing on the host, |
| 49 | // we can't actually implement that. Check that it's at least a regular file. |
| 50 | if (!S_ISREG(buf->st_mode)) { |
Steven Moreland | c066066 | 2019-09-04 17:48:32 -0700 | [diff] [blame] | 51 | errno = ENOTTY; |
| 52 | return false; |
| 53 | } |
Michael Hoisie | 7c4beee | 2024-01-12 19:18:19 +0000 | [diff] [blame] | 54 | // In Win32, unlike Unix, the temp file is not unlinked immediately after |
| 55 | // creation. |
| 56 | #if !defined(_WIN32) |
| 57 | if (buf->st_nlink != 0) { |
| 58 | errno = ENOTTY; |
| 59 | return false; |
| 60 | } |
| 61 | #endif |
Steven Moreland | c066066 | 2019-09-04 17:48:32 -0700 | [diff] [blame] | 62 | return true; |
| 63 | } |
| 64 | |
| 65 | int ashmem_valid(int fd) { |
| 66 | struct stat buf; |
| 67 | return ashmem_validate_stat(fd, &buf); |
| 68 | } |
| 69 | |
Elliott Hughes | 38d2567 | 2017-11-30 16:23:51 -0800 | [diff] [blame] | 70 | int ashmem_create_region(const char* /*ignored*/, size_t size) { |
Michael Hoisie | 7c4beee | 2024-01-12 19:18:19 +0000 | [diff] [blame] | 71 | // Files returned by tmpfile are automatically removed. |
| 72 | std::unique_ptr<FILE, decltype(&fclose)> tmp(tmpfile(), &fclose); |
Elliott Hughes | 3918090 | 2015-12-16 08:33:52 -0800 | [diff] [blame] | 73 | |
Michael Hoisie | 7c4beee | 2024-01-12 19:18:19 +0000 | [diff] [blame] | 74 | if (!tmp) { |
| 75 | return -1; |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 76 | } |
Michael Hoisie | 7c4beee | 2024-01-12 19:18:19 +0000 | [diff] [blame] | 77 | int fd = fileno(tmp.get()); |
| 78 | if (fd == -1) { |
| 79 | return -1; |
| 80 | } |
| 81 | unique_fd dupfd = unique_fd(dup(fd)); |
| 82 | if (dupfd == -1) { |
| 83 | return -1; |
| 84 | } |
| 85 | if (TEMP_FAILURE_RETRY(ftruncate(dupfd, size)) == -1) { |
| 86 | return -1; |
| 87 | } |
| 88 | return dupfd.release(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Elliott Hughes | 38d2567 | 2017-11-30 16:23:51 -0800 | [diff] [blame] | 91 | int ashmem_set_prot_region(int /*fd*/, int /*prot*/) { |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 92 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Elliott Hughes | 38d2567 | 2017-11-30 16:23:51 -0800 | [diff] [blame] | 95 | int ashmem_pin_region(int /*fd*/, size_t /*offset*/, size_t /*len*/) { |
Elliott Hughes | 427c605 | 2016-05-13 14:47:30 -0700 | [diff] [blame] | 96 | return 0 /*ASHMEM_NOT_PURGED*/; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Elliott Hughes | 38d2567 | 2017-11-30 16:23:51 -0800 | [diff] [blame] | 99 | int ashmem_unpin_region(int /*fd*/, size_t /*offset*/, size_t /*len*/) { |
Elliott Hughes | 427c605 | 2016-05-13 14:47:30 -0700 | [diff] [blame] | 100 | return 0 /*ASHMEM_IS_UNPINNED*/; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 101 | } |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 102 | |
| 103 | int ashmem_get_size_region(int fd) |
| 104 | { |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 105 | struct stat buf; |
Steven Moreland | c066066 | 2019-09-04 17:48:32 -0700 | [diff] [blame] | 106 | if (!ashmem_validate_stat(fd, &buf)) { |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 107 | return -1; |
| 108 | } |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 109 | |
Elliott Hughes | 2004a86 | 2015-02-09 11:41:07 -0800 | [diff] [blame] | 110 | return buf.st_size; |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 111 | } |