1 /*
2  * Copyright (c) 2008, 2011, 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_EPoll.h"
33 
34 #include <dlfcn.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/epoll.h>
38 
39 JNIEXPORT jint JNICALL
Java_sun_nio_ch_EPoll_eventSize(JNIEnv * env,jclass this)40 Java_sun_nio_ch_EPoll_eventSize(JNIEnv* env, jclass this)
41 {
42     return sizeof(struct epoll_event);
43 }
44 
45 JNIEXPORT jint JNICALL
Java_sun_nio_ch_EPoll_eventsOffset(JNIEnv * env,jclass this)46 Java_sun_nio_ch_EPoll_eventsOffset(JNIEnv* env, jclass this)
47 {
48     return offsetof(struct epoll_event, events);
49 }
50 
51 JNIEXPORT jint JNICALL
Java_sun_nio_ch_EPoll_dataOffset(JNIEnv * env,jclass this)52 Java_sun_nio_ch_EPoll_dataOffset(JNIEnv* env, jclass this)
53 {
54     return offsetof(struct epoll_event, data);
55 }
56 
57 JNIEXPORT jint JNICALL
Java_sun_nio_ch_EPoll_epollCreate(JNIEnv * env,jclass c)58 Java_sun_nio_ch_EPoll_epollCreate(JNIEnv *env, jclass c) {
59     /*
60      * epoll_create expects a size as a hint to the kernel about how to
61      * dimension internal structures. We can't predict the size in advance.
62      */
63     // Android-changed: Avoid FD leaks through epoll_create. http://b/120983106
64     // int epfd = epoll_create(256);
65     int epfd = epoll_create1(EPOLL_CLOEXEC);
66     if (epfd < 0) {
67        JNU_ThrowIOExceptionWithLastError(env, "epoll_create failed");
68     }
69     return epfd;
70 }
71 
72 JNIEXPORT jint JNICALL
Java_sun_nio_ch_EPoll_epollCtl(JNIEnv * env,jclass c,jint epfd,jint opcode,jint fd,jint events)73 Java_sun_nio_ch_EPoll_epollCtl(JNIEnv *env, jclass c, jint epfd,
74                                    jint opcode, jint fd, jint events)
75 {
76     struct epoll_event event;
77     int res;
78 
79     event.events = events;
80     event.data.fd = fd;
81 
82     RESTARTABLE(epoll_ctl(epfd, (int)opcode, (int)fd, &event), res);
83 
84     return (res == 0) ? 0 : errno;
85 }
86 
87 JNIEXPORT jint JNICALL
Java_sun_nio_ch_EPoll_epollWait(JNIEnv * env,jclass c,jint epfd,jlong address,jint numfds)88 Java_sun_nio_ch_EPoll_epollWait(JNIEnv *env, jclass c,
89                                     jint epfd, jlong address, jint numfds)
90 {
91     struct epoll_event *events = jlong_to_ptr(address);
92     int res;
93 
94     RESTARTABLE(epoll_wait(epfd, events, numfds, -1), res);
95     if (res < 0) {
96         JNU_ThrowIOExceptionWithLastError(env, "epoll_wait failed");
97     }
98     return res;
99 }
100 
101 JNIEXPORT void JNICALL
Java_sun_nio_ch_EPoll_close0(JNIEnv * env,jclass c,jint epfd)102 Java_sun_nio_ch_EPoll_close0(JNIEnv *env, jclass c, jint epfd) {
103     int res;
104     RESTARTABLE(close(epfd), res);
105 }
106