blob: 4410bd6fbeed3a2844036bfb9ce9941c1bf307e5 [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
Peng Xu5f88ac52017-02-24 01:53:10 -080019#include <android/sharedmem.h>
John Recke4f60cc2017-08-07 11:17:06 -070020#include <android/sharedmem_jni.h>
Peng Xu5f88ac52017-02-24 01:53:10 -080021#include <cutils/ashmem.h>
John Recke4f60cc2017-08-07 11:17:06 -070022#include <log/log.h>
Peng Xu5f88ac52017-02-24 01:53:10 -080023#include <utils/Errors.h>
24
John Recke4f60cc2017-08-07 11:17:06 -070025#include <mutex>
26#include <unistd.h>
27
28static struct {
29 jclass clazz;
30 jmethodID getFd;
31} sSharedMemory;
32
33static void jniInit(JNIEnv* env) {
34 static std::once_flag sJniInitialized;
35 std::call_once(sJniInitialized, [](JNIEnv* env) {
36 jclass clazz = env->FindClass("android/os/SharedMemory");
37 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "Failed to find android.os.SharedMemory");
38 sSharedMemory.clazz = (jclass) env->NewGlobalRef(clazz);
39 LOG_ALWAYS_FATAL_IF(sSharedMemory.clazz == nullptr,
40 "Failed to create global ref of android.os.SharedMemory");
41 sSharedMemory.getFd = env->GetMethodID(sSharedMemory.clazz, "getFd", "()I");
42 LOG_ALWAYS_FATAL_IF(sSharedMemory.getFd == nullptr,
43 "Failed to find method SharedMemory#getFd()");
44 }, env);
45}
46
Peng Xu5f88ac52017-02-24 01:53:10 -080047int ASharedMemory_create(const char *name, size_t size) {
48 if (size == 0) {
49 return android::BAD_VALUE;
50 }
51 return ashmem_create_region(name, size);
52}
53
54size_t ASharedMemory_getSize(int fd) {
55 return ashmem_valid(fd) ? ashmem_get_size_region(fd) : 0;
56}
57
58int ASharedMemory_setProt(int fd, int prot) {
59 return ashmem_set_prot_region(fd, prot);
60}
John Recke4f60cc2017-08-07 11:17:06 -070061
62int ASharedMemory_dupFromJava(JNIEnv* env, jobject javaSharedMemory) {
63 if (env == nullptr || javaSharedMemory == nullptr) {
64 return -1;
65 }
66 jniInit(env);
67 if (!env->IsInstanceOf(javaSharedMemory, sSharedMemory.clazz)) {
68 ALOGW("ASharedMemory_dupFromJava called with object "
69 "that's not an instanceof android.os.SharedMemory");
70 return -1;
71 }
72 int fd = env->CallIntMethod(javaSharedMemory, sSharedMemory.getFd);
73 if (fd != -1) {
Nick Kralevich39649702019-01-14 13:52:43 -080074 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
John Recke4f60cc2017-08-07 11:17:06 -070075 }
76 return fd;
77}