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 <pthread.h>
27#include <string.h>
28#include <sys/ioctl.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31#include <unistd.h>
32
33#include <linux/ashmem.h>
34
35#include <cutils/ashmem.h>
36#include <log/log.h>
37
38#define ASHMEM_DEVICE "/dev/ashmem"
39
40/* 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
97    rdev = 0; /* Too much complexity to sniff __ashmem_rdev */
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
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    errno = ENOTTY;
133    return -1;
134}
135
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 */
143int ashmem_create_region(const char *name, size_t size)
144{
145    int ret, save_errno;
146
147    int fd = __ashmem_open();
148    if (fd < 0) {
149        return fd;
150    }
151
152    if (name) {
153        char buf[ASHMEM_NAME_LEN] = {0};
154
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    }
161
162    ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size));
163    if (ret < 0) {
164        goto error;
165    }
166
167    return fd;
168
169error:
170    save_errno = errno;
171    close(fd);
172    errno = save_errno;
173    return ret;
174}
175
176int ashmem_set_prot_region(int fd, int prot)
177{
178    int ret = __ashmem_is_ashmem(fd);
179    if (ret < 0) {
180        return ret;
181    }
182
183    return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot));
184}
185
186int ashmem_pin_region(int fd, size_t offset, size_t len)
187{
188    struct ashmem_pin pin = { offset, len };
189
190    int ret = __ashmem_is_ashmem(fd);
191    if (ret < 0) {
192        return ret;
193    }
194
195    return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin));
196}
197
198int ashmem_unpin_region(int fd, size_t offset, size_t len)
199{
200    struct ashmem_pin pin = { offset, len };
201
202    int ret = __ashmem_is_ashmem(fd);
203    if (ret < 0) {
204        return ret;
205    }
206
207    return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin));
208}
209
210int ashmem_get_size_region(int fd)
211{
212    int ret = __ashmem_is_ashmem(fd);
213    if (ret < 0) {
214        return ret;
215    }
216
217    return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL));
218}
219