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