1 /*
2  * Copyright (c) 2001, 2005, 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 <poll.h>
31 #include <unistd.h>
32 #include <sys/time.h>
33 
34 #define RESTARTABLE(_cmd, _result) do { \
35   do { \
36     _result = _cmd; \
37   } while((_result == -1) && (errno == EINTR)); \
38 } while(0)
39 
40 #include "JNIHelp.h"
41 
42 #define NATIVE_METHOD(className, functionName, signature) \
43 { #functionName, signature, (void*)(Java_sun_nio_ch_ ## className ## _ ## functionName) }
44 
45 static int
ipoll(struct pollfd fds[],unsigned int nfds,int timeout)46 ipoll(struct pollfd fds[], unsigned int nfds, int timeout)
47 {
48     jlong start, now;
49     int remaining = timeout;
50     struct timeval t;
51     int diff;
52 
53     gettimeofday(&t, NULL);
54     start = t.tv_sec * 1000 + t.tv_usec / 1000;
55 
56     for (;;) {
57         int res = poll(fds, nfds, remaining);
58         if (res < 0 && errno == EINTR) {
59             if (remaining >= 0) {
60                 gettimeofday(&t, NULL);
61                 now = t.tv_sec * 1000 + t.tv_usec / 1000;
62                 diff = now - start;
63                 remaining -= diff;
64                 if (diff < 0 || remaining <= 0) {
65                     return 0;
66                 }
67                 start = now;
68             }
69         } else {
70             return res;
71         }
72     }
73 }
74 
75 JNIEXPORT jint JNICALL
Java_sun_nio_ch_PollArrayWrapper_poll0(JNIEnv * env,jobject this,jlong address,jint numfds,jlong timeout)76 Java_sun_nio_ch_PollArrayWrapper_poll0(JNIEnv *env, jobject this,
77                                        jlong address, jint numfds,
78                                        jlong timeout)
79 {
80     struct pollfd *a;
81     int err = 0;
82 
83     a = (struct pollfd *) jlong_to_ptr(address);
84 
85     if (timeout <= 0) {           /* Indefinite or no wait */
86         RESTARTABLE (poll(a, numfds, timeout), err);
87     } else {                     /* Bounded wait; bounded restarts */
88         err = ipoll(a, numfds, timeout);
89     }
90 
91     if (err < 0) {
92         JNU_ThrowIOExceptionWithLastError(env, "Poll failed");
93     }
94     return (jint)err;
95 }
96 
97 JNIEXPORT void JNICALL
Java_sun_nio_ch_PollArrayWrapper_interrupt(JNIEnv * env,jobject this,jint fd)98 Java_sun_nio_ch_PollArrayWrapper_interrupt(JNIEnv *env, jobject this, jint fd)
99 {
100     int fakebuf[1];
101     fakebuf[0] = 1;
102     if (write(fd, fakebuf, 1) < 0) {
103          JNU_ThrowIOExceptionWithLastError(env,
104                                           "Write to interrupt fd failed");
105     }
106 }
107 
108 
109 static JNINativeMethod gMethods[] = {
110   NATIVE_METHOD(PollArrayWrapper, poll0, "(JIJ)I"),
111   NATIVE_METHOD(PollArrayWrapper, interrupt, "(I)V"),
112 };
113 
register_sun_nio_ch_PollArrayWrapper(JNIEnv * env)114 void register_sun_nio_ch_PollArrayWrapper(JNIEnv* env) {
115   jniRegisterNativeMethods(env, "sun/nio/ch/PollArrayWrapper", gMethods, NELEM(gMethods));
116 }
117