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 #include <cutils/ashmem.h>
18
19 /*
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
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <stdbool.h>
28 #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>
35
36 #include <utils/Compat.h>
37
ashmem_create_region(const char *,size_t size)38 int ashmem_create_region(const char* /*ignored*/, size_t size) {
39 char pattern[PATH_MAX];
40 snprintf(pattern, sizeof(pattern), "/tmp/android-ashmem-%d-XXXXXXXXX", getpid());
41 int fd = mkstemp(pattern);
42 if (fd == -1) return -1;
43
44 unlink(pattern);
45
46 if (TEMP_FAILURE_RETRY(ftruncate(fd, size)) == -1) {
47 close(fd);
48 return -1;
49 }
50
51 return fd;
52 }
53
ashmem_set_prot_region(int,int)54 int ashmem_set_prot_region(int /*fd*/, int /*prot*/) {
55 return 0;
56 }
57
ashmem_pin_region(int,size_t,size_t)58 int ashmem_pin_region(int /*fd*/, size_t /*offset*/, size_t /*len*/) {
59 return 0 /*ASHMEM_NOT_PURGED*/;
60 }
61
ashmem_unpin_region(int,size_t,size_t)62 int ashmem_unpin_region(int /*fd*/, size_t /*offset*/, size_t /*len*/) {
63 return 0 /*ASHMEM_IS_UNPINNED*/;
64 }
65
ashmem_get_size_region(int fd)66 int ashmem_get_size_region(int fd)
67 {
68 struct stat buf;
69 int result = fstat(fd, &buf);
70 if (result == -1) {
71 return -1;
72 }
73
74 /*
75 * Check if this is an "ashmem" region.
76 * TODO: This is very hacky, and can easily break.
77 * We need some reliable indicator.
78 */
79 if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) {
80 errno = ENOTTY;
81 return -1;
82 }
83
84 return buf.st_size;
85 }
86