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
34 #define NATIVE_METHOD(className, functionName, signature) \
35 { #functionName, signature, (void*)(className ## _ ## functionName) }
36
37 /*******************************************************************/
38 /* BEGIN JNI ********* BEGIN JNI *********** BEGIN JNI ************/
39 /*******************************************************************/
40
41 /* field id for jint 'fd' in java.io.FileDescriptor */
42 jfieldID IO_fd_fdID;
43
44 /**************************************************************
45 * static methods to store field ID's in initializers
46 */
47
FileDescriptor_initIDs(JNIEnv * env)48 static void FileDescriptor_initIDs(JNIEnv *env) {
49 jclass fdClass = (*env)->FindClass(env, "java/io/FileDescriptor");
50 IO_fd_fdID = (*env)->GetFieldID(env, fdClass, "descriptor", "I");
51 }
52
53 /**************************************************************
54 * File Descriptor
55 */
56
57 JNIEXPORT void JNICALL
FileDescriptor_sync(JNIEnv * env,jobject this)58 FileDescriptor_sync(JNIEnv *env, jobject this) {
59 int fd = (*env)->GetIntField(env, this, IO_fd_fdID);
60 if (JVM_Sync(fd) == -1) {
61 JNU_ThrowByName(env, "java/io/SyncFailedException", "sync failed");
62 }
63 }
64
FileDescriptor_isSocket(JNIEnv * env,jclass ignored,jint fd)65 JNIEXPORT jboolean JNICALL FileDescriptor_isSocket(JNIEnv *env, jclass ignored, jint fd) {
66 int error;
67 socklen_t error_length = sizeof(error);
68 return TEMP_FAILURE_RETRY(getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &error_length)) == 0;
69 }
70
71 static JNINativeMethod gMethods[] = {
72 NATIVE_METHOD(FileDescriptor, sync, "()V"),
73 NATIVE_METHOD(FileDescriptor, isSocket, "(I)Z"),
74 };
75
register_java_io_FileDescriptor(JNIEnv * env)76 void register_java_io_FileDescriptor(JNIEnv* env) {
77 jniRegisterNativeMethods(env, "java/io/FileDescriptor", gMethods, NELEM(gMethods));
78
79 FileDescriptor_initIDs(env);
80 }
81