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 an
21 * ashmem-enabled kernel. See ashmem-dev.c for the real ashmem-based version. A
22 * disk-backed temp file is the best option that is consistently supported
23 * across all host platforms.
24 */
25
26 #include <android-base/unique_fd.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <time.h>
36 #include <unistd.h>
37 #include <utils/Compat.h>
38 #include <memory>
39
40 using android::base::unique_fd;
41
ashmem_validate_stat(int fd,struct stat * buf)42 static bool ashmem_validate_stat(int fd, struct stat* buf) {
43 int result = fstat(fd, buf);
44 if (result == -1) {
45 return false;
46 }
47
48 // Check if this is an ashmem region. Since there's no such thing on the host,
49 // we can't actually implement that. Check that it's at least a regular file.
50 if (!S_ISREG(buf->st_mode)) {
51 errno = ENOTTY;
52 return false;
53 }
54 // In Win32, unlike Unix, the temp file is not unlinked immediately after
55 // creation.
56 #if !defined(_WIN32)
57 if (buf->st_nlink != 0) {
58 errno = ENOTTY;
59 return false;
60 }
61 #endif
62 return true;
63 }
64
ashmem_valid(int fd)65 int ashmem_valid(int fd) {
66 struct stat buf;
67 return ashmem_validate_stat(fd, &buf);
68 }
69
ashmem_create_region(const char *,size_t size)70 int ashmem_create_region(const char* /*ignored*/, size_t size) {
71 // Files returned by tmpfile are automatically removed.
72 std::unique_ptr<FILE, decltype(&fclose)> tmp(tmpfile(), &fclose);
73
74 if (!tmp) {
75 return -1;
76 }
77 int fd = fileno(tmp.get());
78 if (fd == -1) {
79 return -1;
80 }
81 unique_fd dupfd = unique_fd(dup(fd));
82 if (dupfd == -1) {
83 return -1;
84 }
85 if (TEMP_FAILURE_RETRY(ftruncate(dupfd, size)) == -1) {
86 return -1;
87 }
88 return dupfd.release();
89 }
90
ashmem_set_prot_region(int,int)91 int ashmem_set_prot_region(int /*fd*/, int /*prot*/) {
92 return 0;
93 }
94
ashmem_pin_region(int,size_t,size_t)95 int ashmem_pin_region(int /*fd*/, size_t /*offset*/, size_t /*len*/) {
96 return 0 /*ASHMEM_NOT_PURGED*/;
97 }
98
ashmem_unpin_region(int,size_t,size_t)99 int ashmem_unpin_region(int /*fd*/, size_t /*offset*/, size_t /*len*/) {
100 return 0 /*ASHMEM_IS_UNPINNED*/;
101 }
102
ashmem_get_size_region(int fd)103 int ashmem_get_size_region(int fd)
104 {
105 struct stat buf;
106 if (!ashmem_validate_stat(fd, &buf)) {
107 return -1;
108 }
109
110 return buf.st_size;
111 }
112