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 the simulator, which lacks
19 * an ashmem-enabled kernel. See ashmem-dev.c for the real ashmem-based version.
20 */
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <time.h>
32 #include <unistd.h>
33
34 #include <cutils/ashmem.h>
35 #include <utils/Compat.h>
36
37 #ifndef __unused
38 #define __unused __attribute__((__unused__))
39 #endif
40
ashmem_create_region(const char * ignored __unused,size_t size)41 int ashmem_create_region(const char *ignored __unused, size_t size)
42 {
43 char template[PATH_MAX];
44 snprintf(template, sizeof(template), "/tmp/android-ashmem-%d-XXXXXXXXX", getpid());
45 int fd = mkstemp(template);
46 if (fd == -1) return -1;
47
48 unlink(template);
49
50 if (TEMP_FAILURE_RETRY(ftruncate(fd, size)) == -1) {
51 close(fd);
52 return -1;
53 }
54
55 return fd;
56 }
57
ashmem_set_prot_region(int fd __unused,int prot __unused)58 int ashmem_set_prot_region(int fd __unused, int prot __unused)
59 {
60 return 0;
61 }
62
ashmem_pin_region(int fd __unused,size_t offset __unused,size_t len __unused)63 int ashmem_pin_region(int fd __unused, size_t offset __unused, size_t len __unused)
64 {
65 return ASHMEM_NOT_PURGED;
66 }
67
ashmem_unpin_region(int fd __unused,size_t offset __unused,size_t len __unused)68 int ashmem_unpin_region(int fd __unused, size_t offset __unused, size_t len __unused)
69 {
70 return ASHMEM_IS_UNPINNED;
71 }
72
ashmem_get_size_region(int fd)73 int ashmem_get_size_region(int fd)
74 {
75 struct stat buf;
76 int result = fstat(fd, &buf);
77 if (result == -1) {
78 return -1;
79 }
80
81 /*
82 * Check if this is an "ashmem" region.
83 * TODO: This is very hacky, and can easily break.
84 * We need some reliable indicator.
85 */
86 if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) {
87 errno = ENOTTY;
88 return -1;
89 }
90
91 return buf.st_size;
92 }
93