android_os_ParcelFileDescriptor.cpp revision c9119f5034d36f548bbddd8f60291e24ab4e270b
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_NDEBUG 0
18
19#include "JNIHelp.h"
20
21#include <fcntl.h>
22#include <sys/stat.h>
23#include <stdio.h>
24
25#include <utils/Log.h>
26
27#include <android_runtime/AndroidRuntime.h>
28
29namespace android
30{
31
32static struct file_descriptor_offsets_t
33{
34    jclass mClass;
35    jmethodID mConstructor;
36    jfieldID mDescriptor;
37} gFileDescriptorOffsets;
38
39static struct socket_offsets_t
40{
41    jfieldID mSocketImpl;
42} gSocketOffsets;
43
44static struct socket_impl_offsets_t
45{
46    jfieldID mFileDescriptor;
47} gSocketImplOffsets;
48
49static struct parcel_file_descriptor_offsets_t
50{
51    jclass mClass;
52    jfieldID mFileDescriptor;
53} gParcelFileDescriptorOffsets;
54
55static jobject android_os_ParcelFileDescriptor_getFileDescriptorFromSocket(JNIEnv* env,
56    jobject clazz, jobject object)
57{
58    jobject socketImpl = env->GetObjectField(object, gSocketOffsets.mSocketImpl);
59    jobject fileDescriptor = env->GetObjectField(socketImpl, gSocketImplOffsets.mFileDescriptor);
60    jint fd = env->GetIntField(fileDescriptor, gFileDescriptorOffsets.mDescriptor);
61    jobject fileDescriptorClone = env->NewObject(gFileDescriptorOffsets.mClass,
62        gFileDescriptorOffsets.mConstructor);
63    if (fileDescriptorClone != NULL) {
64        env->SetIntField(fileDescriptorClone, gFileDescriptorOffsets.mDescriptor, dup(fd));
65    }
66    return fileDescriptorClone;
67}
68
69static int android_os_ParcelFileDescriptor_createPipeNative(JNIEnv* env,
70    jobject clazz, jobjectArray outFds)
71{
72    int fds[2];
73    if (pipe(fds) < 0) {
74        return -errno;
75    }
76
77    for (int i=0; i<2; i++) {
78        jobject fdObj = env->NewObject(gFileDescriptorOffsets.mClass,
79                gFileDescriptorOffsets.mConstructor);
80        if (fdObj != NULL) {
81            env->SetIntField(fdObj, gFileDescriptorOffsets.mDescriptor, fds[i]);
82        }
83        env->SetObjectArrayElement(outFds, i, fdObj);
84    }
85
86    return 0;
87}
88
89static jint getFd(JNIEnv* env, jobject clazz)
90{
91    jobject descriptor = env->GetObjectField(clazz, gParcelFileDescriptorOffsets.mFileDescriptor);
92    if (descriptor == NULL) return -1;
93    return env->GetIntField(descriptor, gFileDescriptorOffsets.mDescriptor);
94}
95
96static jlong android_os_ParcelFileDescriptor_getStatSize(JNIEnv* env,
97    jobject clazz)
98{
99    jint fd = getFd(env, clazz);
100    if (fd < 0) {
101        jniThrowException(env, "java/lang/IllegalArgumentException", "bad file descriptor");
102        return -1;
103    }
104
105    struct stat st;
106    if (fstat(fd, &st) != 0) {
107        return -1;
108    }
109
110    if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
111        return st.st_size;
112    }
113
114    return -1;
115}
116
117static jlong android_os_ParcelFileDescriptor_seekTo(JNIEnv* env,
118    jobject clazz, jlong pos)
119{
120    jint fd = getFd(env, clazz);
121    if (fd < 0) {
122        jniThrowException(env, "java/lang/IllegalArgumentException", "bad file descriptor");
123        return -1;
124    }
125
126    return lseek(fd, pos, SEEK_SET);
127}
128
129static jlong android_os_ParcelFileDescriptor_getFdNative(JNIEnv* env, jobject clazz)
130{
131    jint fd = getFd(env, clazz);
132    if (fd < 0) {
133        jniThrowException(env, "java/lang/IllegalArgumentException", "bad file descriptor");
134        return -1;
135    }
136
137    return fd;
138}
139
140static const JNINativeMethod gParcelFileDescriptorMethods[] = {
141    {"getFileDescriptorFromSocket", "(Ljava/net/Socket;)Ljava/io/FileDescriptor;",
142        (void*)android_os_ParcelFileDescriptor_getFileDescriptorFromSocket},
143    {"createPipeNative", "([Ljava/io/FileDescriptor;)I",
144        (void*)android_os_ParcelFileDescriptor_createPipeNative},
145    {"getStatSize", "()J",
146        (void*)android_os_ParcelFileDescriptor_getStatSize},
147    {"seekTo", "(J)J",
148        (void*)android_os_ParcelFileDescriptor_seekTo},
149    {"getFdNative", "()I",
150        (void*)android_os_ParcelFileDescriptor_getFdNative}
151};
152
153const char* const kParcelFileDescriptorPathName = "android/os/ParcelFileDescriptor";
154
155int register_android_os_ParcelFileDescriptor(JNIEnv* env)
156{
157    jclass clazz;
158
159    clazz = env->FindClass("java/net/Socket");
160    LOG_FATAL_IF(clazz == NULL, "Unable to find class java.net.Socket");
161    gSocketOffsets.mSocketImpl = env->GetFieldID(clazz, "impl", "Ljava/net/SocketImpl;");
162    LOG_FATAL_IF(gSocketOffsets.mSocketImpl == NULL,
163        "Unable to find impl field in java.net.Socket");
164
165    clazz = env->FindClass("java/net/SocketImpl");
166    LOG_FATAL_IF(clazz == NULL, "Unable to find class java.net.SocketImpl");
167    gSocketImplOffsets.mFileDescriptor = env->GetFieldID(clazz, "fd", "Ljava/io/FileDescriptor;");
168    LOG_FATAL_IF(gSocketImplOffsets.mFileDescriptor == NULL,
169                 "Unable to find fd field in java.net.SocketImpl");
170
171    clazz = env->FindClass("java/io/FileDescriptor");
172    LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
173    gFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
174    gFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "()V");
175    gFileDescriptorOffsets.mDescriptor = env->GetFieldID(clazz, "descriptor", "I");
176    LOG_FATAL_IF(gFileDescriptorOffsets.mDescriptor == NULL,
177                 "Unable to find descriptor field in java.io.FileDescriptor");
178
179    clazz = env->FindClass(kParcelFileDescriptorPathName);
180    LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
181    gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
182    gParcelFileDescriptorOffsets.mFileDescriptor = env->GetFieldID(clazz, "mFileDescriptor", "Ljava/io/FileDescriptor;");
183    LOG_FATAL_IF(gParcelFileDescriptorOffsets.mFileDescriptor == NULL,
184                 "Unable to find mFileDescriptor field in android.os.ParcelFileDescriptor");
185
186    return AndroidRuntime::registerNativeMethods(
187        env, kParcelFileDescriptorPathName,
188        gParcelFileDescriptorMethods, NELEM(gParcelFileDescriptorMethods));
189}
190
191}
192