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