1 /* 2 * Copyright (c) 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 "sun_nio_ch_KQueue.h" 33 34 #include <strings.h> 35 #include <sys/types.h> 36 #include <sys/event.h> 37 #include <sys/time.h> 38 39 JNIEXPORT jint JNICALL 40 Java_sun_nio_ch_KQueue_keventSize(JNIEnv* env, jclass this) 41 { 42 return sizeof(struct kevent); 43 } 44 45 JNIEXPORT jint JNICALL 46 Java_sun_nio_ch_KQueue_identOffset(JNIEnv* env, jclass this) 47 { 48 return offsetof(struct kevent, ident); 49 } 50 51 JNIEXPORT jint JNICALL 52 Java_sun_nio_ch_KQueue_filterOffset(JNIEnv* env, jclass this) 53 { 54 return offsetof(struct kevent, filter); 55 } 56 57 JNIEXPORT jint JNICALL 58 Java_sun_nio_ch_KQueue_flagsOffset(JNIEnv* env, jclass this) 59 { 60 return offsetof(struct kevent, flags); 61 } 62 63 JNIEXPORT jint JNICALL 64 Java_sun_nio_ch_KQueue_kqueue(JNIEnv *env, jclass c) { 65 int kqfd = kqueue(); 66 if (kqfd < 0) { 67 JNU_ThrowIOExceptionWithLastError(env, "kqueue failed"); 68 } 69 return kqfd; 70 } 71 72 JNIEXPORT jint JNICALL 73 Java_sun_nio_ch_KQueue_keventRegister(JNIEnv *env, jclass c, jint kqfd, 74 jint fd, jint filter, jint flags) 75 76 { 77 struct kevent changes[1]; 78 struct timespec timeout = {0, 0}; 79 int res; 80 81 EV_SET(&changes[0], fd, filter, flags, 0, 0, 0); 82 RESTARTABLE(kevent(kqfd, &changes[0], 1, NULL, 0, &timeout), res); 83 return (res == -1) ? errno : 0; 84 } 85 86 JNIEXPORT jint JNICALL 87 Java_sun_nio_ch_KQueue_keventPoll(JNIEnv *env, jclass c, 88 jint kqfd, jlong address, jint nevents) 89 { 90 struct kevent *events = jlong_to_ptr(address); 91 int res; 92 93 RESTARTABLE(kevent(kqfd, NULL, 0, events, nevents, NULL), res); 94 if (res < 0) { 95 JNU_ThrowIOExceptionWithLastError(env, "kqueue failed"); 96 } 97 return res; 98 }