blob: 9003b76c9acfb0e5833a1699431b02f541e1e8f4 [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/*
Michael Hoisie7c4beee2024-01-12 19:18:19 +000020 * 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.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024 */
25
Michael Hoisie7c4beee2024-01-12 19:18:19 +000026#include <android-base/unique_fd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include <errno.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070028#include <fcntl.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029#include <limits.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070030#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>
Elliott Hughes2004a862015-02-09 11:41:07 -080037#include <utils/Compat.h>
Michael Hoisie7c4beee2024-01-12 19:18:19 +000038#include <memory>
39
40using android::base::unique_fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
Steven Morelandc0660662019-09-04 17:48:32 -070042static bool ashmem_validate_stat(int fd, struct stat* buf) {
43 int result = fstat(fd, buf);
44 if (result == -1) {
45 return false;
46 }
47
Michael Hoisie7c4beee2024-01-12 19:18:19 +000048 // 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)) {
Steven Morelandc0660662019-09-04 17:48:32 -070051 errno = ENOTTY;
52 return false;
53 }
Michael Hoisie7c4beee2024-01-12 19:18:19 +000054 // 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
Steven Morelandc0660662019-09-04 17:48:32 -070062 return true;
63}
64
65int ashmem_valid(int fd) {
66 struct stat buf;
67 return ashmem_validate_stat(fd, &buf);
68}
69
Elliott Hughes38d25672017-11-30 16:23:51 -080070int ashmem_create_region(const char* /*ignored*/, size_t size) {
Michael Hoisie7c4beee2024-01-12 19:18:19 +000071 // Files returned by tmpfile are automatically removed.
72 std::unique_ptr<FILE, decltype(&fclose)> tmp(tmpfile(), &fclose);
Elliott Hughes39180902015-12-16 08:33:52 -080073
Michael Hoisie7c4beee2024-01-12 19:18:19 +000074 if (!tmp) {
75 return -1;
Mathieu Chartier1104ae82014-06-03 10:24:21 -070076 }
Michael Hoisie7c4beee2024-01-12 19:18:19 +000077 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();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089}
90
Elliott Hughes38d25672017-11-30 16:23:51 -080091int ashmem_set_prot_region(int /*fd*/, int /*prot*/) {
Mathieu Chartier1104ae82014-06-03 10:24:21 -070092 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093}
94
Elliott Hughes38d25672017-11-30 16:23:51 -080095int ashmem_pin_region(int /*fd*/, size_t /*offset*/, size_t /*len*/) {
Elliott Hughes427c6052016-05-13 14:47:30 -070096 return 0 /*ASHMEM_NOT_PURGED*/;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097}
98
Elliott Hughes38d25672017-11-30 16:23:51 -080099int ashmem_unpin_region(int /*fd*/, size_t /*offset*/, size_t /*len*/) {
Elliott Hughes427c6052016-05-13 14:47:30 -0700100 return 0 /*ASHMEM_IS_UNPINNED*/;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101}
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100102
103int ashmem_get_size_region(int fd)
104{
Mathieu Chartier1104ae82014-06-03 10:24:21 -0700105 struct stat buf;
Steven Morelandc0660662019-09-04 17:48:32 -0700106 if (!ashmem_validate_stat(fd, &buf)) {
Mathieu Chartier1104ae82014-06-03 10:24:21 -0700107 return -1;
108 }
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100109
Elliott Hughes2004a862015-02-09 11:41:07 -0800110 return buf.st_size;
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100111}