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  */
22 #define LOG_TAG "ashmem"
23 
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <linux/ashmem.h>
27 #include <pthread.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 
34 #include <cutils/ashmem.h>
35 #include <log/log.h>
36 
37 #define ASHMEM_DEVICE "/dev/ashmem"
38 
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 */
__ashmem_open_locked()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 
__ashmem_open()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 */
__ashmem_is_ashmem(int fd,int fatal)87 static int __ashmem_is_ashmem(int fd, int fatal)
88 {
89     dev_t rdev;
90     struct stat st;
91 
92     if (TEMP_FAILURE_RETRY(fstat(fd, &st)) < 0) {
93         return -1;
94     }
95 
96     rdev = 0; /* Too much complexity to sniff __ashmem_rdev */
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 
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 */
131     }
132 
133     errno = ENOTTY;
134     return -1;
135 }
136 
ashmem_valid(int fd)137 int ashmem_valid(int fd)
138 {
139     return __ashmem_is_ashmem(fd, 0) >= 0;
140 }
141 
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  */
ashmem_create_region(const char * name,size_t size)149 int ashmem_create_region(const char *name, size_t size)
150 {
151     int ret, save_errno;
152 
153     int fd = __ashmem_open();
154     if (fd < 0) {
155         return fd;
156     }
157 
158     if (name) {
159         char buf[ASHMEM_NAME_LEN] = {0};
160 
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     }
167 
168     ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size));
169     if (ret < 0) {
170         goto error;
171     }
172 
173     return fd;
174 
175 error:
176     save_errno = errno;
177     close(fd);
178     errno = save_errno;
179     return ret;
180 }
181 
ashmem_set_prot_region(int fd,int prot)182 int ashmem_set_prot_region(int fd, int prot)
183 {
184     int ret = __ashmem_is_ashmem(fd, 1);
185     if (ret < 0) {
186         return ret;
187     }
188 
189     return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot));
190 }
191 
ashmem_pin_region(int fd,size_t offset,size_t len)192 int ashmem_pin_region(int fd, size_t offset, size_t len)
193 {
194     struct ashmem_pin pin = { offset, len };
195 
196     int ret = __ashmem_is_ashmem(fd, 1);
197     if (ret < 0) {
198         return ret;
199     }
200 
201     return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin));
202 }
203 
ashmem_unpin_region(int fd,size_t offset,size_t len)204 int ashmem_unpin_region(int fd, size_t offset, size_t len)
205 {
206     struct ashmem_pin pin = { offset, len };
207 
208     int ret = __ashmem_is_ashmem(fd, 1);
209     if (ret < 0) {
210         return ret;
211     }
212 
213     return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin));
214 }
215 
ashmem_get_size_region(int fd)216 int ashmem_get_size_region(int fd)
217 {
218     int ret = __ashmem_is_ashmem(fd, 1);
219     if (ret < 0) {
220         return ret;
221     }
222 
223     return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL));
224 }
225