android_os_ParcelFileDescriptor.cpp revision ea2117bdc03316a9292e2344c6fd157c85c13167
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_getFileDescriptorFromFd(JNIEnv* env,
56    jobject clazz, jint origfd)
57{
58    int fd = dup(origfd);
59    if (fd < 0) {
60        jniThrowException(env, "java/io/IOException", strerror(errno));
61        return NULL;
62    }
63    jobject fileDescriptorClone = env->NewObject(gFileDescriptorOffsets.mClass,
64        gFileDescriptorOffsets.mConstructor);
65    if (fileDescriptorClone != NULL) {
66        env->SetIntField(fileDescriptorClone, gFileDescriptorOffsets.mDescriptor, fd);
67    }
68    return fileDescriptorClone;
69}
70
71static jobject android_os_ParcelFileDescriptor_getFileDescriptorFromFdNoDup(JNIEnv* env,
72    jobject clazz, jint fd)
73{
74    jobject fileDescriptorClone = env->NewObject(gFileDescriptorOffsets.mClass,
75        gFileDescriptorOffsets.mConstructor);
76    if (fileDescriptorClone != NULL) {
77        env->SetIntField(fileDescriptorClone, gFileDescriptorOffsets.mDescriptor, fd);
78    }
79    return fileDescriptorClone;
80}
81
82static jobject android_os_ParcelFileDescriptor_getFileDescriptorFromSocket(JNIEnv* env,
83    jobject clazz, jobject object)
84{
85    jobject socketImpl = env->GetObjectField(object, gSocketOffsets.mSocketImpl);
86    jobject fileDescriptor = env->GetObjectField(socketImpl, gSocketImplOffsets.mFileDescriptor);
87    jint fd = env->GetIntField(fileDescriptor, gFileDescriptorOffsets.mDescriptor);
88    jobject fileDescriptorClone = env->NewObject(gFileDescriptorOffsets.mClass,
89        gFileDescriptorOffsets.mConstructor);
90    if (fileDescriptorClone != NULL) {
91        // XXXX need to throw an exception if the dup fails!
92        env->SetIntField(fileDescriptorClone, gFileDescriptorOffsets.mDescriptor, dup(fd));
93    }
94    return fileDescriptorClone;
95}
96
97static void android_os_ParcelFileDescriptor_createPipeNative(JNIEnv* env,
98    jobject clazz, jobjectArray outFds)
99{
100    int fds[2];
101    if (pipe(fds) < 0) {
102        int therr = errno;
103        jniThrowException(env, "java/io/IOException", strerror(therr));
104        return;
105    }
106
107    for (int i=0; i<2; i++) {
108        jobject fdObj = env->NewObject(gFileDescriptorOffsets.mClass,
109                gFileDescriptorOffsets.mConstructor);
110        if (fdObj != NULL) {
111            env->SetIntField(fdObj, gFileDescriptorOffsets.mDescriptor, fds[i]);
112        }
113        env->SetObjectArrayElement(outFds, i, fdObj);
114    }
115}
116
117static jint getFd(JNIEnv* env, jobject clazz)
118{
119    jobject descriptor = env->GetObjectField(clazz, gParcelFileDescriptorOffsets.mFileDescriptor);
120    if (descriptor == NULL) return -1;
121    return env->GetIntField(descriptor, gFileDescriptorOffsets.mDescriptor);
122}
123
124static jlong android_os_ParcelFileDescriptor_getStatSize(JNIEnv* env,
125    jobject clazz)
126{
127    jint fd = getFd(env, clazz);
128    if (fd < 0) {
129        jniThrowException(env, "java/lang/IllegalArgumentException", "bad file descriptor");
130        return -1;
131    }
132
133    struct stat st;
134    if (fstat(fd, &st) != 0) {
135        return -1;
136    }
137
138    if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
139        return st.st_size;
140    }
141
142    return -1;
143}
144
145static jlong android_os_ParcelFileDescriptor_seekTo(JNIEnv* env,
146    jobject clazz, jlong pos)
147{
148    jint fd = getFd(env, clazz);
149    if (fd < 0) {
150        jniThrowException(env, "java/lang/IllegalArgumentException", "bad file descriptor");
151        return -1;
152    }
153
154    return lseek(fd, pos, SEEK_SET);
155}
156
157static jlong android_os_ParcelFileDescriptor_getFdNative(JNIEnv* env, jobject clazz)
158{
159    jint fd = getFd(env, clazz);
160    if (fd < 0) {
161        jniThrowException(env, "java/lang/IllegalArgumentException", "bad file descriptor");
162        return -1;
163    }
164
165    return fd;
166}
167
168static const JNINativeMethod gParcelFileDescriptorMethods[] = {
169    {"getFileDescriptorFromFd", "(I)Ljava/io/FileDescriptor;",
170        (void*)android_os_ParcelFileDescriptor_getFileDescriptorFromFd},
171    {"getFileDescriptorFromFdNoDup", "(I)Ljava/io/FileDescriptor;",
172        (void*)android_os_ParcelFileDescriptor_getFileDescriptorFromFdNoDup},
173    {"getFileDescriptorFromSocket", "(Ljava/net/Socket;)Ljava/io/FileDescriptor;",
174        (void*)android_os_ParcelFileDescriptor_getFileDescriptorFromSocket},
175    {"createPipeNative", "([Ljava/io/FileDescriptor;)V",
176        (void*)android_os_ParcelFileDescriptor_createPipeNative},
177    {"getStatSize", "()J",
178        (void*)android_os_ParcelFileDescriptor_getStatSize},
179    {"seekTo", "(J)J",
180        (void*)android_os_ParcelFileDescriptor_seekTo},
181    {"getFdNative", "()I",
182        (void*)android_os_ParcelFileDescriptor_getFdNative}
183};
184
185const char* const kParcelFileDescriptorPathName = "android/os/ParcelFileDescriptor";
186
187int register_android_os_ParcelFileDescriptor(JNIEnv* env)
188{
189    jclass clazz;
190
191    clazz = env->FindClass("java/net/Socket");
192    LOG_FATAL_IF(clazz == NULL, "Unable to find class java.net.Socket");
193    gSocketOffsets.mSocketImpl = env->GetFieldID(clazz, "impl", "Ljava/net/SocketImpl;");
194    LOG_FATAL_IF(gSocketOffsets.mSocketImpl == NULL,
195        "Unable to find impl field in java.net.Socket");
196
197    clazz = env->FindClass("java/net/SocketImpl");
198    LOG_FATAL_IF(clazz == NULL, "Unable to find class java.net.SocketImpl");
199    gSocketImplOffsets.mFileDescriptor = env->GetFieldID(clazz, "fd", "Ljava/io/FileDescriptor;");
200    LOG_FATAL_IF(gSocketImplOffsets.mFileDescriptor == NULL,
201                 "Unable to find fd field in java.net.SocketImpl");
202
203    clazz = env->FindClass("java/io/FileDescriptor");
204    LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
205    gFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
206    gFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "()V");
207    gFileDescriptorOffsets.mDescriptor = env->GetFieldID(clazz, "descriptor", "I");
208    LOG_FATAL_IF(gFileDescriptorOffsets.mDescriptor == NULL,
209                 "Unable to find descriptor field in java.io.FileDescriptor");
210
211    clazz = env->FindClass(kParcelFileDescriptorPathName);
212    LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
213    gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
214    gParcelFileDescriptorOffsets.mFileDescriptor = env->GetFieldID(clazz, "mFileDescriptor", "Ljava/io/FileDescriptor;");
215    LOG_FATAL_IF(gParcelFileDescriptorOffsets.mFileDescriptor == NULL,
216                 "Unable to find mFileDescriptor field in android.os.ParcelFileDescriptor");
217
218    return AndroidRuntime::registerNativeMethods(
219        env, kParcelFileDescriptorPathName,
220        gParcelFileDescriptorMethods, NELEM(gParcelFileDescriptorMethods));
221}
222
223}
224