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 | |
| 17 | /* |
| 18 | * Implementation of the user-space ashmem API for devices, which have our |
| 19 | * ashmem-enabled kernel. See ashmem-sim.c for the "fake" tmp-based version, |
| 20 | * used by the simulator. |
| 21 | */ |
Mark Salyzyn | e37111d | 2016-02-02 09:19:39 -0800 | [diff] [blame] | 22 | #define LOG_TAG "ashmem" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 24 | #include <errno.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | #include <fcntl.h> |
Mark Salyzyn | 23ed4c2 | 2016-09-28 13:33:27 -0700 | [diff] [blame] | 26 | #include <linux/ashmem.h> |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 27 | #include <pthread.h> |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <sys/ioctl.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | #include <cutils/ashmem.h> |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 35 | #include <log/log.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 37 | #define ASHMEM_DEVICE "/dev/ashmem" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 39 | /* ashmem identity */ |
| 40 | static dev_t __ashmem_rdev; |
| 41 | /* |
| 42 | * If we trigger a signal handler in the middle of locked activity and the |
| 43 | * signal handler calls ashmem, we could get into a deadlock state. |
| 44 | */ |
| 45 | static pthread_mutex_t __ashmem_lock = PTHREAD_MUTEX_INITIALIZER; |
| 46 | |
| 47 | /* logistics of getting file descriptor for ashmem */ |
| 48 | static int __ashmem_open_locked() |
| 49 | { |
| 50 | int ret; |
| 51 | struct stat st; |
| 52 | |
| 53 | int fd = TEMP_FAILURE_RETRY(open(ASHMEM_DEVICE, O_RDWR)); |
| 54 | if (fd < 0) { |
| 55 | return fd; |
| 56 | } |
| 57 | |
| 58 | ret = TEMP_FAILURE_RETRY(fstat(fd, &st)); |
| 59 | if (ret < 0) { |
| 60 | int save_errno = errno; |
| 61 | close(fd); |
| 62 | errno = save_errno; |
| 63 | return ret; |
| 64 | } |
| 65 | if (!S_ISCHR(st.st_mode) || !st.st_rdev) { |
| 66 | close(fd); |
| 67 | errno = ENOTTY; |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | __ashmem_rdev = st.st_rdev; |
| 72 | return fd; |
| 73 | } |
| 74 | |
| 75 | static int __ashmem_open() |
| 76 | { |
| 77 | int fd; |
| 78 | |
| 79 | pthread_mutex_lock(&__ashmem_lock); |
| 80 | fd = __ashmem_open_locked(); |
| 81 | pthread_mutex_unlock(&__ashmem_lock); |
| 82 | |
| 83 | return fd; |
| 84 | } |
| 85 | |
| 86 | /* Make sure file descriptor references ashmem, negative number means false */ |
Mark Salyzyn | ee43111 | 2016-08-23 13:58:37 -0700 | [diff] [blame] | 87 | static int __ashmem_is_ashmem(int fd, int fatal) |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 88 | { |
| 89 | dev_t rdev; |
| 90 | struct stat st; |
| 91 | |
| 92 | if (TEMP_FAILURE_RETRY(fstat(fd, &st)) < 0) { |
| 93 | return -1; |
| 94 | } |
| 95 | |
Mark Salyzyn | e37111d | 2016-02-02 09:19:39 -0800 | [diff] [blame] | 96 | rdev = 0; /* Too much complexity to sniff __ashmem_rdev */ |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 97 | if (S_ISCHR(st.st_mode) && st.st_rdev) { |
| 98 | pthread_mutex_lock(&__ashmem_lock); |
| 99 | rdev = __ashmem_rdev; |
| 100 | if (rdev) { |
| 101 | pthread_mutex_unlock(&__ashmem_lock); |
| 102 | } else { |
| 103 | int fd = __ashmem_open_locked(); |
| 104 | if (fd < 0) { |
| 105 | pthread_mutex_unlock(&__ashmem_lock); |
| 106 | return -1; |
| 107 | } |
| 108 | rdev = __ashmem_rdev; |
| 109 | pthread_mutex_unlock(&__ashmem_lock); |
| 110 | |
| 111 | close(fd); |
| 112 | } |
| 113 | |
| 114 | if (st.st_rdev == rdev) { |
| 115 | return 0; |
| 116 | } |
| 117 | } |
| 118 | |
Mark Salyzyn | ee43111 | 2016-08-23 13:58:37 -0700 | [diff] [blame] | 119 | if (fatal) { |
| 120 | if (rdev) { |
| 121 | LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o %d:%d", |
| 122 | fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev), |
| 123 | S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP, |
| 124 | major(rdev), minor(rdev)); |
| 125 | } else { |
| 126 | LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o", |
| 127 | fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev), |
| 128 | S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP); |
| 129 | } |
| 130 | /* NOTREACHED */ |
Mark Salyzyn | e37111d | 2016-02-02 09:19:39 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 133 | errno = ENOTTY; |
| 134 | return -1; |
| 135 | } |
| 136 | |
Mark Salyzyn | ee43111 | 2016-08-23 13:58:37 -0700 | [diff] [blame] | 137 | int ashmem_valid(int fd) |
| 138 | { |
| 139 | return __ashmem_is_ashmem(fd, 0) >= 0; |
| 140 | } |
| 141 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 142 | /* |
| 143 | * ashmem_create_region - creates a new ashmem region and returns the file |
| 144 | * descriptor, or <0 on error |
| 145 | * |
| 146 | * `name' is an optional label to give the region (visible in /proc/pid/maps) |
| 147 | * `size' is the size of the region, in page-aligned bytes |
| 148 | */ |
| 149 | int ashmem_create_region(const char *name, size_t size) |
| 150 | { |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 151 | int ret, save_errno; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 | |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 153 | int fd = __ashmem_open(); |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 154 | if (fd < 0) { |
| 155 | return fd; |
| 156 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 157 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 158 | if (name) { |
| 159 | char buf[ASHMEM_NAME_LEN] = {0}; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 161 | strlcpy(buf, name, sizeof(buf)); |
| 162 | ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_NAME, buf)); |
| 163 | if (ret < 0) { |
| 164 | goto error; |
| 165 | } |
| 166 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 167 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 168 | ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size)); |
| 169 | if (ret < 0) { |
| 170 | goto error; |
| 171 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 173 | return fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 174 | |
| 175 | error: |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 176 | save_errno = errno; |
| 177 | close(fd); |
| 178 | errno = save_errno; |
| 179 | return ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | int ashmem_set_prot_region(int fd, int prot) |
| 183 | { |
Mark Salyzyn | ee43111 | 2016-08-23 13:58:37 -0700 | [diff] [blame] | 184 | int ret = __ashmem_is_ashmem(fd, 1); |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 185 | if (ret < 0) { |
| 186 | return ret; |
| 187 | } |
| 188 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 189 | return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | int ashmem_pin_region(int fd, size_t offset, size_t len) |
| 193 | { |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 194 | struct ashmem_pin pin = { offset, len }; |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 195 | |
Mark Salyzyn | ee43111 | 2016-08-23 13:58:37 -0700 | [diff] [blame] | 196 | int ret = __ashmem_is_ashmem(fd, 1); |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 197 | if (ret < 0) { |
| 198 | return ret; |
| 199 | } |
| 200 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 201 | return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | int ashmem_unpin_region(int fd, size_t offset, size_t len) |
| 205 | { |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 206 | struct ashmem_pin pin = { offset, len }; |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 207 | |
Mark Salyzyn | ee43111 | 2016-08-23 13:58:37 -0700 | [diff] [blame] | 208 | int ret = __ashmem_is_ashmem(fd, 1); |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 209 | if (ret < 0) { |
| 210 | return ret; |
| 211 | } |
| 212 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 213 | return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 214 | } |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 215 | |
| 216 | int ashmem_get_size_region(int fd) |
| 217 | { |
Mark Salyzyn | ee43111 | 2016-08-23 13:58:37 -0700 | [diff] [blame] | 218 | int ret = __ashmem_is_ashmem(fd, 1); |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 219 | if (ret < 0) { |
| 220 | return ret; |
| 221 | } |
| 222 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 223 | return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL)); |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 224 | } |