android_os_ParcelFileDescriptor.cpp revision 23fdaf6fb62a9b5154b2508916a21c678462c5d0
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 const JNINativeMethod gParcelFileDescriptorMethods[] = {
130    {"getFileDescriptorFromSocket", "(Ljava/net/Socket;)Ljava/io/FileDescriptor;",
131        (void*)android_os_ParcelFileDescriptor_getFileDescriptorFromSocket},
132    {"createPipeNative", "([Ljava/io/FileDescriptor;)I",
133        (void*)android_os_ParcelFileDescriptor_createPipeNative},
134    {"getStatSize", "()J",
135        (void*)android_os_ParcelFileDescriptor_getStatSize},
136    {"seekTo", "(J)J",
137        (void*)android_os_ParcelFileDescriptor_seekTo}
138};
139
140const char* const kParcelFileDescriptorPathName = "android/os/ParcelFileDescriptor";
141
142int register_android_os_ParcelFileDescriptor(JNIEnv* env)
143{
144    jclass clazz;
145
146    clazz = env->FindClass("java/net/Socket");
147    LOG_FATAL_IF(clazz == NULL, "Unable to find class java.net.Socket");
148    gSocketOffsets.mSocketImpl = env->GetFieldID(clazz, "impl", "Ljava/net/SocketImpl;");
149    LOG_FATAL_IF(gSocketOffsets.mSocketImpl == NULL,
150        "Unable to find impl field in java.net.Socket");
151
152    clazz = env->FindClass("java/net/SocketImpl");
153    LOG_FATAL_IF(clazz == NULL, "Unable to find class java.net.SocketImpl");
154    gSocketImplOffsets.mFileDescriptor = env->GetFieldID(clazz, "fd", "Ljava/io/FileDescriptor;");
155    LOG_FATAL_IF(gSocketImplOffsets.mFileDescriptor == NULL,
156                 "Unable to find fd field in java.net.SocketImpl");
157
158    clazz = env->FindClass("java/io/FileDescriptor");
159    LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
160    gFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
161    gFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "()V");
162    gFileDescriptorOffsets.mDescriptor = env->GetFieldID(clazz, "descriptor", "I");
163    LOG_FATAL_IF(gFileDescriptorOffsets.mDescriptor == NULL,
164                 "Unable to find descriptor field in java.io.FileDescriptor");
165
166    clazz = env->FindClass(kParcelFileDescriptorPathName);
167    LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
168    gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
169    gParcelFileDescriptorOffsets.mFileDescriptor = env->GetFieldID(clazz, "mFileDescriptor", "Ljava/io/FileDescriptor;");
170    LOG_FATAL_IF(gParcelFileDescriptorOffsets.mFileDescriptor == NULL,
171                 "Unable to find mFileDescriptor field in android.os.ParcelFileDescriptor");
172
173    return AndroidRuntime::registerNativeMethods(
174        env, kParcelFileDescriptorPathName,
175        gParcelFileDescriptorMethods, NELEM(gParcelFileDescriptorMethods));
176}
177
178}
179