• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #include <jni.h>
18 
19 #include <fcntl.h>
20 #include <unistd.h>
21 
22 #include <android/sharedmem.h>
23 #include <android/sharedmem_jni.h>
24 #include <cutils/ashmem.h>
25 #include <log/log.h>
26 #include <utils/Errors.h>
27 
28 #include <mutex>
29 
30 static struct {
31     jclass clazz;
32     jmethodID getFd;
33 } sSharedMemory;
34 
jniInit(JNIEnv * env)35 static 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 
ASharedMemory_create(const char * name,size_t size)49 int 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 
ASharedMemory_getSize(int fd)56 size_t ASharedMemory_getSize(int fd) {
57     return ashmem_valid(fd) ? ashmem_get_size_region(fd) : 0;
58 }
59 
ASharedMemory_setProt(int fd,int prot)60 int ASharedMemory_setProt(int fd, int prot) {
61     return ashmem_set_prot_region(fd, prot);
62 }
63 
ASharedMemory_dupFromJava(JNIEnv * env,jobject javaSharedMemory)64 int 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) {
76         fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
77     }
78     return fd;
79 }
80