android_os_ParcelFileDescriptor.cpp revision a3804cf77f0edd93f6247a055cdafb856b117eec
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 socket_offsets_t
33{
34    jfieldID mSocketImpl;
35} gSocketOffsets;
36
37static struct socket_impl_offsets_t
38{
39    jfieldID mFileDescriptor;
40} gSocketImplOffsets;
41
42static struct parcel_file_descriptor_offsets_t
43{
44    jclass mClass;
45    jfieldID mFileDescriptor;
46} gParcelFileDescriptorOffsets;
47
48static jobject android_os_ParcelFileDescriptor_getFileDescriptorFromSocket(JNIEnv* env,
49    jobject clazz, jobject object)
50{
51    jobject socketImpl = env->GetObjectField(object, gSocketOffsets.mSocketImpl);
52    jobject fileDescriptor = env->GetObjectField(socketImpl, gSocketImplOffsets.mFileDescriptor);
53    jint fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
54    return jniCreateFileDescriptor(env, dup(fd));
55}
56
57static int android_os_ParcelFileDescriptor_createPipeNative(JNIEnv* env,
58    jobject clazz, jobjectArray outFds)
59{
60    int fds[2];
61    if (pipe(fds) < 0) {
62        return -errno;
63    }
64
65    for (int i=0; i<2; i++) {
66        jobject fdObj = jniCreateFileDescriptor(env, fds[i]);
67        env->SetObjectArrayElement(outFds, i, fdObj);
68    }
69
70    return 0;
71}
72
73static jint getFd(JNIEnv* env, jobject clazz)
74{
75    jobject descriptor = env->GetObjectField(clazz, gParcelFileDescriptorOffsets.mFileDescriptor);
76    if (descriptor == NULL) return -1;
77    return jniGetFDFromFileDescriptor(env, descriptor);
78}
79
80static jlong android_os_ParcelFileDescriptor_getStatSize(JNIEnv* env,
81    jobject clazz)
82{
83    jint fd = getFd(env, clazz);
84    if (fd < 0) {
85        jniThrowException(env, "java/lang/IllegalArgumentException", "bad file descriptor");
86        return -1;
87    }
88
89    struct stat st;
90    if (fstat(fd, &st) != 0) {
91        return -1;
92    }
93
94    if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
95        return st.st_size;
96    }
97
98    return -1;
99}
100
101static jlong android_os_ParcelFileDescriptor_seekTo(JNIEnv* env,
102    jobject clazz, jlong pos)
103{
104    jint fd = getFd(env, clazz);
105    if (fd < 0) {
106        jniThrowException(env, "java/lang/IllegalArgumentException", "bad file descriptor");
107        return -1;
108    }
109
110    return lseek(fd, pos, SEEK_SET);
111}
112
113static jlong android_os_ParcelFileDescriptor_getFdNative(JNIEnv* env, jobject clazz)
114{
115    jint fd = getFd(env, clazz);
116    if (fd < 0) {
117        jniThrowException(env, "java/lang/IllegalArgumentException", "bad file descriptor");
118        return -1;
119    }
120
121    return fd;
122}
123
124static const JNINativeMethod gParcelFileDescriptorMethods[] = {
125    {"getFileDescriptorFromSocket", "(Ljava/net/Socket;)Ljava/io/FileDescriptor;",
126        (void*)android_os_ParcelFileDescriptor_getFileDescriptorFromSocket},
127    {"createPipeNative", "([Ljava/io/FileDescriptor;)I",
128        (void*)android_os_ParcelFileDescriptor_createPipeNative},
129    {"getStatSize", "()J",
130        (void*)android_os_ParcelFileDescriptor_getStatSize},
131    {"seekTo", "(J)J",
132        (void*)android_os_ParcelFileDescriptor_seekTo},
133    {"getFdNative", "()I",
134        (void*)android_os_ParcelFileDescriptor_getFdNative}
135};
136
137const char* const kParcelFileDescriptorPathName = "android/os/ParcelFileDescriptor";
138
139int register_android_os_ParcelFileDescriptor(JNIEnv* env)
140{
141    jclass clazz;
142
143    clazz = env->FindClass("java/net/Socket");
144    LOG_FATAL_IF(clazz == NULL, "Unable to find class java.net.Socket");
145    gSocketOffsets.mSocketImpl = env->GetFieldID(clazz, "impl", "Ljava/net/SocketImpl;");
146    LOG_FATAL_IF(gSocketOffsets.mSocketImpl == NULL,
147        "Unable to find impl field in java.net.Socket");
148
149    clazz = env->FindClass("java/net/SocketImpl");
150    LOG_FATAL_IF(clazz == NULL, "Unable to find class java.net.SocketImpl");
151    gSocketImplOffsets.mFileDescriptor = env->GetFieldID(clazz, "fd", "Ljava/io/FileDescriptor;");
152    LOG_FATAL_IF(gSocketImplOffsets.mFileDescriptor == NULL,
153                 "Unable to find fd field in java.net.SocketImpl");
154
155    clazz = env->FindClass(kParcelFileDescriptorPathName);
156    LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
157    gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
158    gParcelFileDescriptorOffsets.mFileDescriptor = env->GetFieldID(clazz, "mFileDescriptor", "Ljava/io/FileDescriptor;");
159    LOG_FATAL_IF(gParcelFileDescriptorOffsets.mFileDescriptor == NULL,
160                 "Unable to find mFileDescriptor field in android.os.ParcelFileDescriptor");
161
162    return AndroidRuntime::registerNativeMethods(
163        env, kParcelFileDescriptorPathName,
164        gParcelFileDescriptorMethods, NELEM(gParcelFileDescriptorMethods));
165}
166
167}
168