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 <dlfcn.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/epoll.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_EPoll.h"
39
40JNIEXPORT jint JNICALL
41Java_sun_nio_ch_EPoll_eventSize(JNIEnv* env, jclass clazz)
42{
43 return sizeof(struct epoll_event);
44}
45
46JNIEXPORT jint JNICALL
47Java_sun_nio_ch_EPoll_eventsOffset(JNIEnv* env, jclass clazz)
48{
49 return offsetof(struct epoll_event, events);
50}
51
52JNIEXPORT jint JNICALL
53Java_sun_nio_ch_EPoll_dataOffset(JNIEnv* env, jclass clazz)
54{
55 return offsetof(struct epoll_event, data);
56}
57
58JNIEXPORT jint JNICALL
59Java_sun_nio_ch_EPoll_create(JNIEnv *env, jclass clazz) {
60 /* size hint not used in modern kernels */
61 int epfd = epoll_create(256);
62 if (epfd < 0) {
63 JNU_ThrowIOExceptionWithLastError(env, "epoll_create failed");
64 }
65 return epfd;
66}
67
68JNIEXPORT jint JNICALL
69Java_sun_nio_ch_EPoll_ctl(JNIEnv *env, jclass clazz, jint epfd,
70 jint opcode, jint fd, jint events)
71{
72 struct epoll_event event;
73 int res;
74
75 event.events = events;
76 event.data.fd = fd;
77
78 res = epoll_ctl(epfd, (int)opcode, (int)fd, &event);
79 return (res == 0) ? 0 : errno;
80}
81
82JNIEXPORT jint JNICALL
83Java_sun_nio_ch_EPoll_wait(JNIEnv *env, jclass clazz, jint epfd,
84 jlong address, jint numfds, jint timeout)
85{
86 struct epoll_event *events = jlong_to_ptr(address);
87 int res = epoll_wait(epfd, events, numfds, timeout);
88 if (res < 0) {
89 if (errno == EINTR) {
90 return IOS_INTERRUPTED;
91 } else {
92 JNU_ThrowIOExceptionWithLastError(env, "epoll_wait failed");
93 return IOS_THROWN;
94 }
95 }
96 return res;
97}
98