author | simonis |
Mon, 20 Jan 2014 09:24:25 +0100 | |
changeset 22604 | 9b394795e216 |
parent 14342 | 8435a30053c1 |
child 22631 | ac85b05a53f4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13024
diff
changeset
|
2 |
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
#include <sys/types.h> |
|
27 |
#include <string.h> |
|
12872
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
28 |
#include <sys/resource.h> |
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
29 |
|
2 | 30 |
#include "jni.h" |
31 |
#include "jni_util.h" |
|
32 |
#include "jvm.h" |
|
33 |
#include "jlong.h" |
|
34 |
#include "sun_nio_ch_IOUtil.h" |
|
12872
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
35 |
#include "java_lang_Integer.h" |
2 | 36 |
#include "nio.h" |
37 |
#include "nio_util.h" |
|
38 |
||
39 |
static jfieldID fd_fdID; /* for jint 'fd' in java.io.FileDescriptor */ |
|
40 |
||
41 |
||
42 |
JNIEXPORT void JNICALL |
|
43 |
Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz) |
|
44 |
{ |
|
45 |
clazz = (*env)->FindClass(env, "java/io/FileDescriptor"); |
|
46 |
fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"); |
|
47 |
} |
|
48 |
||
49 |
JNIEXPORT jboolean JNICALL |
|
50 |
Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz, |
|
51 |
jbyteArray randArray) |
|
52 |
{ |
|
53 |
JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", NULL); |
|
54 |
return JNI_FALSE; |
|
55 |
} |
|
56 |
||
57 |
JNIEXPORT jint JNICALL |
|
58 |
Java_sun_nio_ch_IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo) |
|
59 |
{ |
|
60 |
return (*env)->GetIntField(env, fdo, fd_fdID); |
|
61 |
} |
|
62 |
||
63 |
JNIEXPORT void JNICALL |
|
64 |
Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val) |
|
65 |
{ |
|
66 |
(*env)->SetIntField(env, fdo, fd_fdID, val); |
|
67 |
} |
|
68 |
||
69 |
static int |
|
70 |
configureBlocking(int fd, jboolean blocking) |
|
71 |
{ |
|
72 |
int flags = fcntl(fd, F_GETFL); |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
73 |
int newflags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); |
2 | 74 |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
75 |
return (flags == newflags) ? 0 : fcntl(fd, F_SETFL, newflags); |
2 | 76 |
} |
77 |
||
78 |
JNIEXPORT void JNICALL |
|
79 |
Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz, |
|
80 |
jobject fdo, jboolean blocking) |
|
81 |
{ |
|
82 |
if (configureBlocking(fdval(env, fdo), blocking) < 0) |
|
83 |
JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); |
|
84 |
} |
|
85 |
||
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
86 |
JNIEXPORT jlong JNICALL |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
87 |
Java_sun_nio_ch_IOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking) |
2 | 88 |
{ |
89 |
int fd[2]; |
|
90 |
||
91 |
if (pipe(fd) < 0) { |
|
92 |
JNU_ThrowIOExceptionWithLastError(env, "Pipe failed"); |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
93 |
return 0; |
2 | 94 |
} |
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
95 |
if (blocking == JNI_FALSE) { |
2 | 96 |
if ((configureBlocking(fd[0], JNI_FALSE) < 0) |
97 |
|| (configureBlocking(fd[1], JNI_FALSE) < 0)) { |
|
98 |
JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
99 |
close(fd[0]); |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
100 |
close(fd[1]); |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
101 |
return 0; |
2 | 102 |
} |
103 |
} |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
104 |
return ((jlong) fd[0] << 32) | (jlong) fd[1]; |
2 | 105 |
} |
106 |
||
107 |
JNIEXPORT jboolean JNICALL |
|
108 |
Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd) |
|
109 |
{ |
|
110 |
char buf[128]; |
|
111 |
int tn = 0; |
|
112 |
||
113 |
for (;;) { |
|
114 |
int n = read(fd, buf, sizeof(buf)); |
|
115 |
tn += n; |
|
116 |
if ((n < 0) && (errno != EAGAIN)) |
|
117 |
JNU_ThrowIOExceptionWithLastError(env, "Drain"); |
|
118 |
if (n == (int)sizeof(buf)) |
|
119 |
continue; |
|
120 |
return (tn > 0) ? JNI_TRUE : JNI_FALSE; |
|
121 |
} |
|
122 |
} |
|
123 |
||
12872
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
124 |
JNIEXPORT jint JNICALL |
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
125 |
Java_sun_nio_ch_IOUtil_fdLimit(JNIEnv *env, jclass this) |
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
126 |
{ |
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
127 |
struct rlimit rlp; |
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
128 |
if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) { |
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
129 |
JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed"); |
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
130 |
return -1; |
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
131 |
} |
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
132 |
if (rlp.rlim_max < 0 || rlp.rlim_max > java_lang_Integer_MAX_VALUE) { |
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
133 |
return java_lang_Integer_MAX_VALUE; |
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
134 |
} else { |
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
135 |
return (jint)rlp.rlim_max; |
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
136 |
} |
16fa902b1469
7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents:
7668
diff
changeset
|
137 |
} |
2 | 138 |
|
13024
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
12872
diff
changeset
|
139 |
JNIEXPORT jint JNICALL |
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
12872
diff
changeset
|
140 |
Java_sun_nio_ch_IOUtil_iovMax(JNIEnv *env, jclass this) |
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
12872
diff
changeset
|
141 |
{ |
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
12872
diff
changeset
|
142 |
jlong iov_max = sysconf(_SC_IOV_MAX); |
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
12872
diff
changeset
|
143 |
if (iov_max == -1) |
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
12872
diff
changeset
|
144 |
iov_max = 16; |
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
12872
diff
changeset
|
145 |
return (jint)iov_max; |
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
12872
diff
changeset
|
146 |
} |
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
12872
diff
changeset
|
147 |
|
2 | 148 |
/* Declared in nio_util.h for use elsewhere in NIO */ |
149 |
||
150 |
jint |
|
151 |
convertReturnVal(JNIEnv *env, jint n, jboolean reading) |
|
152 |
{ |
|
153 |
if (n > 0) /* Number of bytes written */ |
|
154 |
return n; |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
155 |
else if (n == 0) { |
2 | 156 |
if (reading) { |
157 |
return IOS_EOF; /* EOF is -1 in javaland */ |
|
158 |
} else { |
|
159 |
return 0; |
|
160 |
} |
|
161 |
} |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
162 |
else if (errno == EAGAIN) |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
163 |
return IOS_UNAVAILABLE; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
164 |
else if (errno == EINTR) |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
165 |
return IOS_INTERRUPTED; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
166 |
else { |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
167 |
const char *msg = reading ? "Read failed" : "Write failed"; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
168 |
JNU_ThrowIOExceptionWithLastError(env, msg); |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
169 |
return IOS_THROWN; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
170 |
} |
2 | 171 |
} |
172 |
||
173 |
/* Declared in nio_util.h for use elsewhere in NIO */ |
|
174 |
||
175 |
jlong |
|
176 |
convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading) |
|
177 |
{ |
|
178 |
if (n > 0) /* Number of bytes written */ |
|
179 |
return n; |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
180 |
else if (n == 0) { |
2 | 181 |
if (reading) { |
182 |
return IOS_EOF; /* EOF is -1 in javaland */ |
|
183 |
} else { |
|
184 |
return 0; |
|
185 |
} |
|
186 |
} |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
187 |
else if (errno == EAGAIN) |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
188 |
return IOS_UNAVAILABLE; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
189 |
else if (errno == EINTR) |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
190 |
return IOS_INTERRUPTED; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
191 |
else { |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
192 |
const char *msg = reading ? "Read failed" : "Write failed"; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
193 |
JNU_ThrowIOExceptionWithLastError(env, msg); |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
194 |
return IOS_THROWN; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
195 |
} |
2 | 196 |
} |
197 |
||
198 |
jint |
|
199 |
fdval(JNIEnv *env, jobject fdo) |
|
200 |
{ |
|
201 |
return (*env)->GetIntField(env, fdo, fd_fdID); |
|
202 |
} |