blob: d2c28f393a887423af03bd7f2b3996031693f48e [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 the simulator, which lacks
21 * an ashmem-enabled kernel. See ashmem-dev.c for the real ashmem-based version.
22 */
23
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <errno.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070025#include <fcntl.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#include <limits.h>
Mathieu Chartier1104ae82014-06-03 10:24:21 -070027#include <stdbool.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/stat.h>
32#include <sys/types.h>
33#include <time.h>
34#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
Elliott Hughes2004a862015-02-09 11:41:07 -080036#include <utils/Compat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037
Mark Salyzynedc70932014-05-02 12:23:56 -070038#ifndef __unused
Mark Salyzyn12717162014-04-29 15:49:14 -070039#define __unused __attribute__((__unused__))
Mark Salyzynedc70932014-05-02 12:23:56 -070040#endif
Mark Salyzyn12717162014-04-29 15:49:14 -070041
42int ashmem_create_region(const char *ignored __unused, size_t size)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043{
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080044 char pattern[PATH_MAX];
45 snprintf(pattern, sizeof(pattern), "/tmp/android-ashmem-%d-XXXXXXXXX", getpid());
46 int fd = mkstemp(pattern);
Elliott Hughes39180902015-12-16 08:33:52 -080047 if (fd == -1) return -1;
48
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080049 unlink(pattern);
Elliott Hughes39180902015-12-16 08:33:52 -080050
51 if (TEMP_FAILURE_RETRY(ftruncate(fd, size)) == -1) {
52 close(fd);
53 return -1;
Mathieu Chartier1104ae82014-06-03 10:24:21 -070054 }
Elliott Hughes39180902015-12-16 08:33:52 -080055
56 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057}
58
Mark Salyzyn12717162014-04-29 15:49:14 -070059int ashmem_set_prot_region(int fd __unused, int prot __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060{
Mathieu Chartier1104ae82014-06-03 10:24:21 -070061 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062}
63
Mark Salyzyn12717162014-04-29 15:49:14 -070064int ashmem_pin_region(int fd __unused, size_t offset __unused, size_t len __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065{
Elliott Hughes427c6052016-05-13 14:47:30 -070066 return 0 /*ASHMEM_NOT_PURGED*/;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067}
68
Mark Salyzyn12717162014-04-29 15:49:14 -070069int ashmem_unpin_region(int fd __unused, size_t offset __unused, size_t len __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070{
Elliott Hughes427c6052016-05-13 14:47:30 -070071 return 0 /*ASHMEM_IS_UNPINNED*/;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072}
Bjorn Bringert7be52b12009-06-02 00:41:09 +010073
74int ashmem_get_size_region(int fd)
75{
Mathieu Chartier1104ae82014-06-03 10:24:21 -070076 struct stat buf;
Elliott Hughes2004a862015-02-09 11:41:07 -080077 int result = fstat(fd, &buf);
Mathieu Chartier1104ae82014-06-03 10:24:21 -070078 if (result == -1) {
79 return -1;
80 }
Bjorn Bringert7be52b12009-06-02 00:41:09 +010081
Mark Salyzync2d8aad2016-02-02 08:05:54 -080082 /*
83 * Check if this is an "ashmem" region.
84 * TODO: This is very hacky, and can easily break.
85 * We need some reliable indicator.
86 */
Mathieu Chartier1104ae82014-06-03 10:24:21 -070087 if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) {
88 errno = ENOTTY;
89 return -1;
90 }
Bjorn Bringert7be52b12009-06-02 00:41:09 +010091
Elliott Hughes2004a862015-02-09 11:41:07 -080092 return buf.st_size;
Bjorn Bringert7be52b12009-06-02 00:41:09 +010093}