The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | #define LOG_TAG "MemoryFile" |
| 18 | #include <utils/Log.h> |
| 19 | |
| 20 | #include <cutils/ashmem.h> |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 21 | #include "core_jni_helpers.h" |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 22 | #include <nativehelper/JNIHelp.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | #include <sys/mman.h> |
| 25 | |
| 26 | |
| 27 | namespace android { |
| 28 | |
John Reck | dea6a02 | 2017-05-25 17:09:33 -0700 | [diff] [blame] | 29 | static jboolean android_os_MemoryFile_pin(JNIEnv* env, jobject clazz, jobject fileDescriptor, |
| 30 | jboolean pin) { |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 31 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | int result = (pin ? ashmem_pin_region(fd, 0, 0) : ashmem_unpin_region(fd, 0, 0)); |
| 33 | if (result < 0) { |
| 34 | jniThrowException(env, "java/io/IOException", NULL); |
| 35 | } |
John Reck | dea6a02 | 2017-05-25 17:09:33 -0700 | [diff] [blame] | 36 | return result == ASHMEM_WAS_PURGED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Marco Nelissen | 7bcbd51 | 2009-06-23 10:34:55 -0700 | [diff] [blame] | 39 | static jint android_os_MemoryFile_get_size(JNIEnv* env, jobject clazz, |
Bjorn Bringert | 963cd006 | 2009-05-29 14:05:12 +0100 | [diff] [blame] | 40 | jobject fileDescriptor) { |
| 41 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 42 | // Use ASHMEM_GET_SIZE to find out if the fd refers to an ashmem region. |
| 43 | // ASHMEM_GET_SIZE should succeed for all ashmem regions, and the kernel |
| 44 | // should return ENOTTY for all other valid file descriptors |
| 45 | int result = ashmem_get_size_region(fd); |
| 46 | if (result < 0) { |
| 47 | if (errno == ENOTTY) { |
| 48 | // ENOTTY means that the ioctl does not apply to this object, |
| 49 | // i.e., it is not an ashmem region. |
Marco Nelissen | ec10090 | 2009-06-17 08:56:59 -0700 | [diff] [blame] | 50 | return (jint) -1; |
Bjorn Bringert | 963cd006 | 2009-05-29 14:05:12 +0100 | [diff] [blame] | 51 | } |
| 52 | // Some other error, throw exception |
| 53 | jniThrowIOException(env, errno); |
Marco Nelissen | ec10090 | 2009-06-17 08:56:59 -0700 | [diff] [blame] | 54 | return (jint) -1; |
Bjorn Bringert | 963cd006 | 2009-05-29 14:05:12 +0100 | [diff] [blame] | 55 | } |
Marco Nelissen | ec10090 | 2009-06-17 08:56:59 -0700 | [diff] [blame] | 56 | return (jint) result; |
Bjorn Bringert | 963cd006 | 2009-05-29 14:05:12 +0100 | [diff] [blame] | 57 | } |
| 58 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | static const JNINativeMethod methods[] = { |
John Reck | dea6a02 | 2017-05-25 17:09:33 -0700 | [diff] [blame] | 60 | {"native_pin", "(Ljava/io/FileDescriptor;Z)Z", (void*)android_os_MemoryFile_pin}, |
Marco Nelissen | 7bcbd51 | 2009-06-23 10:34:55 -0700 | [diff] [blame] | 61 | {"native_get_size", "(Ljava/io/FileDescriptor;)I", |
| 62 | (void*)android_os_MemoryFile_get_size} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
John Reck | dea6a02 | 2017-05-25 17:09:33 -0700 | [diff] [blame] | 65 | int register_android_os_MemoryFile(JNIEnv* env) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 66 | return RegisterMethodsOrDie(env, "android/os/MemoryFile", methods, NELEM(methods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | } |