1 /*
2  * Copyright (c) 2012, 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 <strings.h>
27 #include <sys/types.h>
28 #include <sys/event.h>
29 #include <sys/time.h>
30 
31 #include "jni.h"
32 #include "jni_util.h"
33 #include "jlong.h"
34 #include "nio.h"
35 #include "nio_util.h"
36 
37 #include "sun_nio_ch_KQueue.h"
38 
39 JNIEXPORT jint JNICALL
Java_sun_nio_ch_KQueue_keventSize(JNIEnv * env,jclass clazz)40 Java_sun_nio_ch_KQueue_keventSize(JNIEnv* env, jclass clazz)
41 {
42     return sizeof(struct kevent);
43 }
44 
45 JNIEXPORT jint JNICALL
Java_sun_nio_ch_KQueue_identOffset(JNIEnv * env,jclass clazz)46 Java_sun_nio_ch_KQueue_identOffset(JNIEnv* env, jclass clazz)
47 {
48     return offsetof(struct kevent, ident);
49 }
50 
51 JNIEXPORT jint JNICALL
Java_sun_nio_ch_KQueue_filterOffset(JNIEnv * env,jclass clazz)52 Java_sun_nio_ch_KQueue_filterOffset(JNIEnv* env, jclass clazz)
53 {
54     return offsetof(struct kevent, filter);
55 }
56 
57 JNIEXPORT jint JNICALL
Java_sun_nio_ch_KQueue_flagsOffset(JNIEnv * env,jclass clazz)58 Java_sun_nio_ch_KQueue_flagsOffset(JNIEnv* env, jclass clazz)
59 {
60     return offsetof(struct kevent, flags);
61 }
62 
63 JNIEXPORT jint JNICALL
Java_sun_nio_ch_KQueue_create(JNIEnv * env,jclass clazz)64 Java_sun_nio_ch_KQueue_create(JNIEnv *env, jclass clazz) {
65     int kqfd = kqueue();
66     if (kqfd < 0) {
67         JNU_ThrowIOExceptionWithLastError(env, "kqueue failed");
68         return IOS_THROWN;
69     }
70     return kqfd;
71 }
72 
73 JNIEXPORT jint JNICALL
Java_sun_nio_ch_KQueue_register(JNIEnv * env,jclass clazz,jint kqfd,jint fd,jint filter,jint flags)74 Java_sun_nio_ch_KQueue_register(JNIEnv *env, jclass clazz, jint kqfd,
75                                 jint fd, jint filter, jint flags)
76 
77 {
78     struct kevent changes[1];
79     int res;
80 
81     EV_SET(&changes[0], fd, filter, flags, 0, 0, 0);
82     RESTARTABLE(kevent(kqfd, &changes[0], 1, NULL, 0, NULL), res);
83     return (res == -1) ? errno : 0;
84 }
85 
86 JNIEXPORT jint JNICALL
Java_sun_nio_ch_KQueue_poll(JNIEnv * env,jclass clazz,jint kqfd,jlong address,jint nevents,jlong timeout)87 Java_sun_nio_ch_KQueue_poll(JNIEnv *env, jclass clazz, jint kqfd, jlong address,
88                             jint nevents, jlong timeout)
89 {
90     struct kevent *events = jlong_to_ptr(address);
91     int res;
92     struct timespec ts;
93     struct timespec *tsp;
94 
95     if (timeout >= 0) {
96         ts.tv_sec = timeout / 1000;
97         ts.tv_nsec = (timeout % 1000) * 1000000;
98         tsp = &ts;
99     } else {
100         tsp = NULL;
101     }
102 
103     res = kevent(kqfd, NULL, 0, events, nevents, tsp);
104     if (res < 0) {
105         if (errno == EINTR) {
106             return IOS_INTERRUPTED;
107         } else {
108             JNU_ThrowIOExceptionWithLastError(env, "kqueue failed");
109             return IOS_THROWN;
110         }
111     }
112     return res;
113 }
114