blob: 92717c043946a52a65cc5aa24f45612975cc813a [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 Salyzyn23ed4c22016-09-28 13:33:27 -070026#include <linux/ashmem.h>
Mark Salyzyn1186f3a2016-02-02 08:21:32 -080027#include <pthread.h>
Mark Salyzync2d8aad2016-02-02 08:05:54 -080028#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 Projectdd7bc332009-03-03 19:32:55 -080033
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034#include <cutils/ashmem.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080035#include <log/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
Mark Salyzync2d8aad2016-02-02 08:05:54 -080037#define ASHMEM_DEVICE "/dev/ashmem"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038
Mark Salyzyn1186f3a2016-02-02 08:21:32 -080039/* ashmem identity */
40static 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 */
45static pthread_mutex_t __ashmem_lock = PTHREAD_MUTEX_INITIALIZER;
46
47/* logistics of getting file descriptor for ashmem */
48static 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
75static 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 Salyzynee431112016-08-23 13:58:37 -070087static int __ashmem_is_ashmem(int fd, int fatal)
Mark Salyzyn1186f3a2016-02-02 08:21:32 -080088{
89 dev_t rdev;
90 struct stat st;
91
92 if (TEMP_FAILURE_RETRY(fstat(fd, &st)) < 0) {
93 return -1;
94 }
95
Mark Salyzyne37111d2016-02-02 09:19:39 -080096 rdev = 0; /* Too much complexity to sniff __ashmem_rdev */
Mark Salyzyn1186f3a2016-02-02 08:21:32 -080097 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 Salyzynee431112016-08-23 13:58:37 -0700119 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 Salyzyne37111d2016-02-02 09:19:39 -0800131 }
132
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800133 errno = ENOTTY;
134 return -1;
135}
136
Mark Salyzynee431112016-08-23 13:58:37 -0700137int ashmem_valid(int fd)
138{
139 return __ashmem_is_ashmem(fd, 0) >= 0;
140}
141
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142/*
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 */
149int ashmem_create_region(const char *name, size_t size)
150{
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800151 int ret, save_errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800153 int fd = __ashmem_open();
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800154 if (fd < 0) {
155 return fd;
156 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800158 if (name) {
159 char buf[ASHMEM_NAME_LEN] = {0};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800161 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 Projectdd7bc332009-03-03 19:32:55 -0800167
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800168 ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size));
169 if (ret < 0) {
170 goto error;
171 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800173 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174
175error:
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800176 save_errno = errno;
177 close(fd);
178 errno = save_errno;
179 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180}
181
182int ashmem_set_prot_region(int fd, int prot)
183{
Mark Salyzynee431112016-08-23 13:58:37 -0700184 int ret = __ashmem_is_ashmem(fd, 1);
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800185 if (ret < 0) {
186 return ret;
187 }
188
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800189 return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190}
191
192int ashmem_pin_region(int fd, size_t offset, size_t len)
193{
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800194 struct ashmem_pin pin = { offset, len };
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800195
Mark Salyzynee431112016-08-23 13:58:37 -0700196 int ret = __ashmem_is_ashmem(fd, 1);
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800197 if (ret < 0) {
198 return ret;
199 }
200
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800201 return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202}
203
204int ashmem_unpin_region(int fd, size_t offset, size_t len)
205{
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800206 struct ashmem_pin pin = { offset, len };
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800207
Mark Salyzynee431112016-08-23 13:58:37 -0700208 int ret = __ashmem_is_ashmem(fd, 1);
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800209 if (ret < 0) {
210 return ret;
211 }
212
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800213 return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214}
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100215
216int ashmem_get_size_region(int fd)
217{
Mark Salyzynee431112016-08-23 13:58:37 -0700218 int ret = __ashmem_is_ashmem(fd, 1);
Mark Salyzyn1186f3a2016-02-02 08:21:32 -0800219 if (ret < 0) {
220 return ret;
221 }
222
Mark Salyzync2d8aad2016-02-02 08:05:54 -0800223 return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL));
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100224}