blob: f65dc8b021fc33bd3c633b615e86a243d1e259a5 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 Hughes8e9aeb92017-11-10 10:22:07 -080017#include <cutils/ashmem.h>
18
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019/*
20 * Implementation of the user-space ashmem API for devices, which have our
21 * ashmem-enabled kernel. See ashmem-sim.c for the "fake" tmp-based version,
22 * used by the simulator.
23 */
Mark Salyzyne37111d2016-02-02 09:19:39 -080024#define LOG_TAG "ashmem"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025
Tri Vo2891ba02019-01-28 17:56:43 -080026#ifndef __ANDROID_VNDK__
27#include <dlfcn.h>
28#endif
Mark Salyzync2d8aad2016-02-02 08:05:54 -080029#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <fcntl.h>
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070031#include <linux/ashmem.h>
Mark Salyzyn1186f3a2016-02-02 08:21:32 -080032#include <pthread.h>
Mark Salyzync2d8aad2016-02-02 08:05:54 -080033#include <string.h>
34#include <sys/ioctl.h>
35#include <sys/stat.h>
Elliott Hughesd77b5372017-05-17 11:36:51 -070036#include <sys/sysmacros.h>
Mark Salyzync2d8aad2016-02-02 08:05:54 -080037#include <sys/types.h>
38#include <unistd.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080039#include <log/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
Mark Salyzync2d8aad2016-02-02 08:05:54 -080041#define ASHMEM_DEVICE "/dev/ashmem"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
Mark Salyzyn1186f3a2016-02-02 08:21:32 -080043/* ashmem identity */
44static dev_t __ashmem_rdev;
45/*
46 * If we trigger a signal handler in the middle of locked activity and the
47 * signal handler calls ashmem, we could get into a deadlock state.
48 */
49static pthread_mutex_t __ashmem_lock = PTHREAD_MUTEX_INITIALIZER;
50
Tri Vo2891ba02019-01-28 17:56:43 -080051/*
52 * We use ashmemd to enforce that apps don't open /dev/ashmem directly. Vendor
53 * code can't access system aidl services per Treble requirements. So we limit
54 * ashmemd access to the system variant of libcutils.
55 */
56#ifndef __ANDROID_VNDK__
57using openFdType = int (*)();
58
59openFdType initOpenAshmemFd() {
60 openFdType openFd = nullptr;
61 void* handle = dlopen("libashmemd_client.so", RTLD_NOW);
62 if (!handle) {
63 ALOGE("Failed to dlopen() libashmemd_client.so: %s", dlerror());
64 return openFd;
65 }
66
67 openFd = reinterpret_cast<openFdType>(dlsym(handle, "openAshmemdFd"));
68 if (!openFd) {
69 ALOGE("Failed to dlsym() openAshmemdFd() function: %s", dlerror());
70 }
71 return openFd;
72}
73#endif
74
Mark Salyzyn1186f3a2016-02-02 08:21:32 -080075/* logistics of getting file descriptor for ashmem */
76static int __ashmem_open_locked()
77{
78 int ret;
79 struct stat st;
80
Tri Vo2891ba02019-01-28 17:56:43 -080081 int fd = -1;
82#ifndef __ANDROID_VNDK__
83 static auto openFd = initOpenAshmemFd();
84 if (openFd) {
85 fd = openFd();
86 }
87#endif
88 if (fd < 0) {
89 fd = TEMP_FAILURE_RETRY(open(ASHMEM_DEVICE, O_RDWR | O_CLOEXEC));
90 }
Mark Salyzyn1186f3a2016-02-02 08:21:32 -080091 if (fd < 0) {
92 return fd;
93 }
94
95 ret = TEMP_FAILURE_RETRY(fstat(fd, &st));
96 if (ret < 0) {
97 int save_errno = errno;
98 close(fd);
99 errno = save_errno;
100 return ret;
101 }
102 if (!S_ISCHR(st.st_mode) || !st.st_rdev) {
103 close(fd);
104 errno = ENOTTY;
105 return -1;
106 }
107
108 __ashmem_rdev = st.st_rdev;
109 return fd;
110}
111
112static int __ashmem_open()
113{
114 int fd;
115
116 pthread_mutex_lock(&__ashmem_lock);
117 fd = __ashmem_open_locked();
118 pthread_mutex_unlock(&__ashmem_lock);
119
120 return fd;
121}
122
123/* Make sure file descriptor references ashmem, negative number means false */
Mark Salyzynee431112016-08-23 13:58:37 -0700124static int __ashmem_is_ashmem(int fd, int fatal)
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800125{
126 dev_t rdev;
127 struct stat st;
128
Joel Fernandes56cd6512018-07-17 13:00:17 -0700129 if (fstat(fd, &st) < 0) {
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800130 return -1;
131 }
132
Mark Salyzyne37111d2016-02-02 09:19:39 -0800133 rdev = 0; /* Too much complexity to sniff __ashmem_rdev */
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800134 if (S_ISCHR(st.st_mode) && st.st_rdev) {
135 pthread_mutex_lock(&__ashmem_lock);
136 rdev = __ashmem_rdev;
137 if (rdev) {
138 pthread_mutex_unlock(&__ashmem_lock);
139 } else {
140 int fd = __ashmem_open_locked();
141 if (fd < 0) {
142 pthread_mutex_unlock(&__ashmem_lock);
143 return -1;
144 }
145 rdev = __ashmem_rdev;
146 pthread_mutex_unlock(&__ashmem_lock);
147
148 close(fd);
149 }
150
151 if (st.st_rdev == rdev) {
152 return 0;
153 }
154 }
155
Mark Salyzynee431112016-08-23 13:58:37 -0700156 if (fatal) {
157 if (rdev) {
158 LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o %d:%d",
159 fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev),
160 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP,
161 major(rdev), minor(rdev));
162 } else {
163 LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o",
164 fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev),
165 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP);
166 }
167 /* NOTREACHED */
Mark Salyzyne37111d2016-02-02 09:19:39 -0800168 }
169
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800170 errno = ENOTTY;
171 return -1;
172}
173
Joel Fernandes56cd6512018-07-17 13:00:17 -0700174static int __ashmem_check_failure(int fd, int result)
175{
176 if (result == -1 && errno == ENOTTY) __ashmem_is_ashmem(fd, 1);
177 return result;
178}
179
Mark Salyzynee431112016-08-23 13:58:37 -0700180int ashmem_valid(int fd)
181{
182 return __ashmem_is_ashmem(fd, 0) >= 0;
183}
184
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185/*
186 * ashmem_create_region - creates a new ashmem region and returns the file
187 * descriptor, or <0 on error
188 *
189 * `name' is an optional label to give the region (visible in /proc/pid/maps)
190 * `size' is the size of the region, in page-aligned bytes
191 */
192int ashmem_create_region(const char *name, size_t size)
193{
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800194 int ret, save_errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800196 int fd = __ashmem_open();
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800197 if (fd < 0) {
198 return fd;
199 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800201 if (name) {
202 char buf[ASHMEM_NAME_LEN] = {0};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800204 strlcpy(buf, name, sizeof(buf));
205 ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_NAME, buf));
206 if (ret < 0) {
207 goto error;
208 }
209 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800211 ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size));
212 if (ret < 0) {
213 goto error;
214 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800216 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217
218error:
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800219 save_errno = errno;
220 close(fd);
221 errno = save_errno;
222 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223}
224
225int ashmem_set_prot_region(int fd, int prot)
226{
Joel Fernandes56cd6512018-07-17 13:00:17 -0700227 return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228}
229
230int ashmem_pin_region(int fd, size_t offset, size_t len)
231{
Elliott Hughes8e9aeb92017-11-10 10:22:07 -0800232 // TODO: should LP64 reject too-large offset/len?
233 ashmem_pin pin = { static_cast<uint32_t>(offset), static_cast<uint32_t>(len) };
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800234
Joel Fernandes56cd6512018-07-17 13:00:17 -0700235 return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236}
237
238int ashmem_unpin_region(int fd, size_t offset, size_t len)
239{
Elliott Hughes8e9aeb92017-11-10 10:22:07 -0800240 // TODO: should LP64 reject too-large offset/len?
241 ashmem_pin pin = { static_cast<uint32_t>(offset), static_cast<uint32_t>(len) };
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800242
Joel Fernandes56cd6512018-07-17 13:00:17 -0700243 return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244}
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100245
246int ashmem_get_size_region(int fd)
247{
Joel Fernandes56cd6512018-07-17 13:00:17 -0700248 return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL)));
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100249}