blob: 338b280a8ebeaa219f4080693d95b487a4d0e9cb [file] [log] [blame]
Peng Xu5f88ac52017-02-24 01:53:10 -08001/*
2 * Copyright (C) 2017 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
John Recke4f60cc2017-08-07 11:17:06 -070017#include <jni.h>
18
Tom Cherry30f8f822020-04-13 11:06:23 -070019#include <fcntl.h>
20#include <unistd.h>
21
Peng Xu5f88ac52017-02-24 01:53:10 -080022#include <android/sharedmem.h>
John Recke4f60cc2017-08-07 11:17:06 -070023#include <android/sharedmem_jni.h>
Peng Xu5f88ac52017-02-24 01:53:10 -080024#include <cutils/ashmem.h>
John Recke4f60cc2017-08-07 11:17:06 -070025#include <log/log.h>
Peng Xu5f88ac52017-02-24 01:53:10 -080026#include <utils/Errors.h>
27
John Recke4f60cc2017-08-07 11:17:06 -070028#include <mutex>
John Recke4f60cc2017-08-07 11:17:06 -070029
30static struct {
31 jclass clazz;
32 jmethodID getFd;
33} sSharedMemory;
34
35static void jniInit(JNIEnv* env) {
36 static std::once_flag sJniInitialized;
37 std::call_once(sJniInitialized, [](JNIEnv* env) {
38 jclass clazz = env->FindClass("android/os/SharedMemory");
39 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "Failed to find android.os.SharedMemory");
40 sSharedMemory.clazz = (jclass) env->NewGlobalRef(clazz);
41 LOG_ALWAYS_FATAL_IF(sSharedMemory.clazz == nullptr,
42 "Failed to create global ref of android.os.SharedMemory");
43 sSharedMemory.getFd = env->GetMethodID(sSharedMemory.clazz, "getFd", "()I");
44 LOG_ALWAYS_FATAL_IF(sSharedMemory.getFd == nullptr,
45 "Failed to find method SharedMemory#getFd()");
46 }, env);
47}
48
Peng Xu5f88ac52017-02-24 01:53:10 -080049int ASharedMemory_create(const char *name, size_t size) {
50 if (size == 0) {
51 return android::BAD_VALUE;
52 }
53 return ashmem_create_region(name, size);
54}
55
56size_t ASharedMemory_getSize(int fd) {
57 return ashmem_valid(fd) ? ashmem_get_size_region(fd) : 0;
58}
59
60int ASharedMemory_setProt(int fd, int prot) {
61 return ashmem_set_prot_region(fd, prot);
62}
John Recke4f60cc2017-08-07 11:17:06 -070063
64int ASharedMemory_dupFromJava(JNIEnv* env, jobject javaSharedMemory) {
65 if (env == nullptr || javaSharedMemory == nullptr) {
66 return -1;
67 }
68 jniInit(env);
69 if (!env->IsInstanceOf(javaSharedMemory, sSharedMemory.clazz)) {
70 ALOGW("ASharedMemory_dupFromJava called with object "
71 "that's not an instanceof android.os.SharedMemory");
72 return -1;
73 }
74 int fd = env->CallIntMethod(javaSharedMemory, sSharedMemory.getFd);
75 if (fd != -1) {
Nick Kralevich39649702019-01-14 13:52:43 -080076 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
John Recke4f60cc2017-08-07 11:17:06 -070077 }
78 return fd;
79}