1 /*
2 * Copyright (c) 1997, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 #include <sys/socket.h>
26 #include <sys/types.h>
27
28 #include "jni.h"
29 #include "jni_util.h"
30 #include "jvm.h"
31
32 #include <nativehelper/JNIHelp.h>
33 #include <nativehelper/jni_macros.h>
34
35 /*******************************************************************/
36 /* BEGIN JNI ********* BEGIN JNI *********** BEGIN JNI ************/
37 /*******************************************************************/
38
39 /* field id for jint 'fd' in java.io.FileDescriptor */
40 jfieldID IO_fd_fdID;
41
42 /**************************************************************
43 * static methods to store field ID's in initializers
44 */
45
FileDescriptor_initIDs(JNIEnv * env)46 static void FileDescriptor_initIDs(JNIEnv *env) {
47 jclass fdClass = (*env)->FindClass(env, "java/io/FileDescriptor");
48 IO_fd_fdID = (*env)->GetFieldID(env, fdClass, "descriptor", "I");
49 }
50
51 /**************************************************************
52 * File Descriptor
53 */
54
55 JNIEXPORT void JNICALL
FileDescriptor_sync(JNIEnv * env,jobject this)56 FileDescriptor_sync(JNIEnv *env, jobject this) {
57 int fd = (*env)->GetIntField(env, this, IO_fd_fdID);
58 if (JVM_Sync(fd) == -1) {
59 JNU_ThrowByName(env, "java/io/SyncFailedException", "sync failed");
60 }
61 }
62
FileDescriptor_isSocket(jint fd)63 JNIEXPORT jboolean JNICALL FileDescriptor_isSocket(jint fd) {
64 // BEGIN Android-changed: isSocket - do not clear socket error code
65 int domain;
66 socklen_t domain_length = sizeof(domain);
67 return TEMP_FAILURE_RETRY(getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &domain, &domain_length)) == 0;
68 // END Android-changed: isSocket - do not clear socket error code
69 }
70
71 JNIEXPORT jboolean JNICALL
FileDescriptor_getAppend(jint fd)72 FileDescriptor_getAppend(jint fd) {
73 int flags = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFL));
74 return ((flags & O_APPEND) == 0) ? JNI_FALSE : JNI_TRUE;
75 }
76
77
78 static JNINativeMethod gMethods[] = {
79 NATIVE_METHOD(FileDescriptor, sync, "()V"),
80 CRITICAL_NATIVE_METHOD(FileDescriptor, isSocket, "(I)Z"),
81 CRITICAL_NATIVE_METHOD(FileDescriptor, getAppend, "(I)Z"),
82 };
83
register_java_io_FileDescriptor(JNIEnv * env)84 void register_java_io_FileDescriptor(JNIEnv* env) {
85 jniRegisterNativeMethods(env, "java/io/FileDescriptor", gMethods, NELEM(gMethods));
86
87 FileDescriptor_initIDs(env);
88 }
89