1 /* 2 * Copyright (c) 2008, 2018, 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 26 #include <stdlib.h> 27 #include <dlfcn.h> 28 #include <sys/types.h> 29 #include <port.h> 30 31 #include "jni.h" 32 #include "jni_util.h" 33 #include "jvm.h" 34 #include "jlong.h" 35 #include "nio.h" 36 #include "nio_util.h" 37 38 #include "sun_nio_ch_SolarisEventPort.h" 39 40 JNIEXPORT jint JNICALL 41 Java_sun_nio_ch_SolarisEventPort_port_1create 42 (JNIEnv* env, jclass clazz) 43 { 44 int port = port_create(); 45 if (port == -1) { 46 JNU_ThrowIOExceptionWithLastError(env, "port_create"); 47 } 48 return (jint)port; 49 } 50 51 JNIEXPORT void JNICALL 52 Java_sun_nio_ch_SolarisEventPort_port_1close 53 (JNIEnv* env, jclass clazz, jint port) 54 { 55 int res = close(port); 56 if (res < 0 && res != EINTR) { 57 JNU_ThrowIOExceptionWithLastError(env, "close failed"); 58 } 59 } 60 61 JNIEXPORT jboolean JNICALL 62 Java_sun_nio_ch_SolarisEventPort_port_1associate 63 (JNIEnv* env, jclass clazz, jint port, jint source, jlong objectAddress, jint events) 64 { 65 uintptr_t object = (uintptr_t)jlong_to_ptr(objectAddress); 66 if (port_associate((int)port, (int)source, object, (int)events, NULL) == 0) { 67 return JNI_TRUE; 68 } else { 69 if (errno != EBADFD) 70 JNU_ThrowIOExceptionWithLastError(env, "port_associate"); 71 return JNI_FALSE; 72 } 73 } 74 75 JNIEXPORT jboolean JNICALL 76 Java_sun_nio_ch_SolarisEventPort_port_1dissociate 77 (JNIEnv* env, jclass clazz, jint port, jint source, jlong objectAddress) 78 { 79 uintptr_t object = (uintptr_t)jlong_to_ptr(objectAddress); 80 81 if (port_dissociate((int)port, (int)source, object) == 0) { 82 return JNI_TRUE; 83 } else { 84 if (errno != ENOENT) 85 JNU_ThrowIOExceptionWithLastError(env, "port_dissociate"); 86 return JNI_FALSE; 87 } 88 } 89 90 JNIEXPORT void JNICALL 91 Java_sun_nio_ch_SolarisEventPort_port_1send(JNIEnv* env, jclass clazz, 92 jint port, jint events) 93 { 94 if (port_send((int)port, (int)events, NULL) == -1) { 95 JNU_ThrowIOExceptionWithLastError(env, "port_send"); 96 } 97 } 98 99 JNIEXPORT jint JNICALL 100 Java_sun_nio_ch_SolarisEventPort_port_1get(JNIEnv* env, jclass clazz, 101 jint port, jlong eventAddress) 102 { 103 int res; 104 port_event_t* ev = (port_event_t*)jlong_to_ptr(eventAddress); 105 106 res = port_get((int)port, ev, NULL); 107 if (res == -1) { 108 if (errno == EINTR) { 109 return IOS_INTERRUPTED; 110 } else { 111 JNU_ThrowIOExceptionWithLastError(env, "port_get failed"); 112 return IOS_THROWN; 113 } 114 } 115 return res; 116 } 117 118 JNIEXPORT jint JNICALL 119 Java_sun_nio_ch_SolarisEventPort_port_1getn(JNIEnv* env, jclass clazz, 120 jint port, jlong arrayAddress, jint max, jlong timeout) 121 { 122 int res; 123 uint_t n = 1; 124 port_event_t* list = (port_event_t*)jlong_to_ptr(arrayAddress); 125 timespec_t ts; 126 timespec_t* tsp; 127 128 if (timeout >= 0L) { 129 ts.tv_sec = timeout / 1000; 130 ts.tv_nsec = 1000000 * (timeout % 1000); 131 tsp = &ts; 132 } else { 133 tsp = NULL; 134 } 135 136 res = port_getn((int)port, list, (uint_t)max, &n, tsp); 137 if (res == -1 && errno != ETIME) { 138 if (errno == EINTR) { 139 return IOS_INTERRUPTED; 140 } else { 141 JNU_ThrowIOExceptionWithLastError(env, "port_getn failed"); 142 return IOS_THROWN; 143 } 144 } 145 146 return (jint)n; 147 }