blob: 4a07d66bb04db7f4ed40e09edbe95aab1fd39fb7 [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
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 Salyzyne37111d2016-02-02 09:19:39 -080022#define LOG_TAG "ashmem"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023
Mark Salyzync2d8aad2016-02-02 08:05:54 -080024#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <fcntl.h>
Mark Salyzyn1186f3a2016-02-02 08:21:32 -080026#include <pthread.h>
Mark Salyzync2d8aad2016-02-02 08:05:54 -080027#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 Projectdd7bc332009-03-03 19:32:55 -080032
33#include <linux/ashmem.h>
Mark Salyzync2d8aad2016-02-02 08:05:54 -080034
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include <cutils/ashmem.h>
Mark Salyzyne37111d2016-02-02 09:19:39 -080036#include <log/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037
Mark Salyzync2d8aad2016-02-02 08:05:54 -080038#define ASHMEM_DEVICE "/dev/ashmem"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
Mark Salyzyn1186f3a2016-02-02 08:21:32 -080040/* ashmem identity */
41static 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 */
46static pthread_mutex_t __ashmem_lock = PTHREAD_MUTEX_INITIALIZER;
47
48/* logistics of getting file descriptor for ashmem */
49static 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
76static 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 */
88static 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 Salyzyne37111d2016-02-02 09:19:39 -080097 rdev = 0; /* Too much complexity to sniff __ashmem_rdev */
Mark Salyzyn1186f3a2016-02-02 08:21:32 -080098 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 Salyzyne37111d2016-02-02 09:19:39 -0800120 if (rdev) {
Mark Salyzyn53c0ca62016-02-02 09:20:31 -0800121 LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o %d:%d",
Mark Salyzyne37111d2016-02-02 09:19:39 -0800122 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 Salyzyn53c0ca62016-02-02 09:20:31 -0800126 LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o",
Mark Salyzyne37111d2016-02-02 09:19:39 -0800127 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 Salyzyn53c0ca62016-02-02 09:20:31 -0800130 /* NOTREACHED */
Mark Salyzyne37111d2016-02-02 09:19:39 -0800131
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800132 errno = ENOTTY;
133 return -1;
134}
135
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136/*
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 */
143int ashmem_create_region(const char *name, size_t size)
144{
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800145 int ret, save_errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800147 int fd = __ashmem_open();
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800148 if (fd < 0) {
149 return fd;
150 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800152 if (name) {
153 char buf[ASHMEM_NAME_LEN] = {0};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800155 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 Projectdd7bc332009-03-03 19:32:55 -0800161
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800162 ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size));
163 if (ret < 0) {
164 goto error;
165 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800167 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168
169error:
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800170 save_errno = errno;
171 close(fd);
172 errno = save_errno;
173 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174}
175
176int ashmem_set_prot_region(int fd, int prot)
177{
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800178 int ret = __ashmem_is_ashmem(fd);
179 if (ret < 0) {
180 return ret;
181 }
182
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800183 return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184}
185
186int ashmem_pin_region(int fd, size_t offset, size_t len)
187{
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800188 struct ashmem_pin pin = { offset, len };
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800189
190 int ret = __ashmem_is_ashmem(fd);
191 if (ret < 0) {
192 return ret;
193 }
194
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800195 return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196}
197
198int ashmem_unpin_region(int fd, size_t offset, size_t len)
199{
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800200 struct ashmem_pin pin = { offset, len };
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800201
202 int ret = __ashmem_is_ashmem(fd);
203 if (ret < 0) {
204 return ret;
205 }
206
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800207 return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208}
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100209
210int ashmem_get_size_region(int fd)
211{
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800212 int ret = __ashmem_is_ashmem(fd);
213 if (ret < 0) {
214 return ret;
215 }
216
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800217 return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL));
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100218}