author | mli |
Thu, 28 Nov 2019 13:48:25 +0800 | |
changeset 59300 | 60bdcb9a7b94 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
37892
55da13d60938
8155978: Remove HTTP proxy implementation and tests from RMI
rriggs
parents:
37667
diff
changeset
|
2 |
* Copyright (c) 1996, 2016, 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 |
package sun.rmi.transport.tcp; |
|
26 |
||
27 |
import java.lang.ref.Reference; |
|
28 |
import java.lang.ref.SoftReference; |
|
29 |
import java.lang.ref.WeakReference; |
|
30 |
import java.lang.reflect.InvocationTargetException; |
|
13408
7b1c9c089326
7187876: ClassCastException in TCPTransport.executeAcceptLoop
dmocek
parents:
12040
diff
changeset
|
31 |
import java.lang.reflect.UndeclaredThrowableException; |
2 | 32 |
import java.io.DataInputStream; |
33 |
import java.io.DataOutputStream; |
|
34 |
import java.io.IOException; |
|
35 |
import java.io.InputStream; |
|
36 |
import java.io.OutputStream; |
|
37 |
import java.io.BufferedInputStream; |
|
38 |
import java.io.BufferedOutputStream; |
|
39 |
import java.net.InetAddress; |
|
40 |
import java.net.ServerSocket; |
|
41 |
import java.net.Socket; |
|
42 |
import java.rmi.RemoteException; |
|
43 |
import java.rmi.server.ExportException; |
|
44 |
import java.rmi.server.LogStream; |
|
45 |
import java.rmi.server.RMIFailureHandler; |
|
46 |
import java.rmi.server.RMISocketFactory; |
|
47 |
import java.rmi.server.RemoteCall; |
|
48 |
import java.rmi.server.ServerNotActiveException; |
|
49 |
import java.rmi.server.UID; |
|
50 |
import java.security.AccessControlContext; |
|
51 |
import java.security.AccessController; |
|
28552
608626229264
8055309: RMI needs better transportation considerations
smarks
parents:
25859
diff
changeset
|
52 |
import java.security.Permissions; |
23333
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
19211
diff
changeset
|
53 |
import java.security.PrivilegedAction; |
28552
608626229264
8055309: RMI needs better transportation considerations
smarks
parents:
25859
diff
changeset
|
54 |
import java.security.ProtectionDomain; |
2 | 55 |
import java.util.ArrayList; |
56 |
import java.util.LinkedList; |
|
57 |
import java.util.List; |
|
58 |
import java.util.Map; |
|
59 |
import java.util.WeakHashMap; |
|
60 |
import java.util.logging.Level; |
|
61 |
import java.util.concurrent.ExecutorService; |
|
62 |
import java.util.concurrent.RejectedExecutionException; |
|
63 |
import java.util.concurrent.SynchronousQueue; |
|
64 |
import java.util.concurrent.ThreadFactory; |
|
65 |
import java.util.concurrent.ThreadPoolExecutor; |
|
66 |
import java.util.concurrent.TimeUnit; |
|
67 |
import java.util.concurrent.atomic.AtomicInteger; |
|
68 |
import sun.rmi.runtime.Log; |
|
69 |
import sun.rmi.runtime.NewThreadAction; |
|
70 |
import sun.rmi.transport.Channel; |
|
71 |
import sun.rmi.transport.Connection; |
|
72 |
import sun.rmi.transport.DGCAckHandler; |
|
73 |
import sun.rmi.transport.Endpoint; |
|
74 |
import sun.rmi.transport.StreamRemoteCall; |
|
75 |
import sun.rmi.transport.Target; |
|
76 |
import sun.rmi.transport.Transport; |
|
77 |
import sun.rmi.transport.TransportConstants; |
|
78 |
||
79 |
/** |
|
80 |
* TCPTransport is the socket-based implementation of the RMI Transport |
|
81 |
* abstraction. |
|
82 |
* |
|
83 |
* @author Ann Wollrath |
|
84 |
* @author Peter Jones |
|
85 |
*/ |
|
19211 | 86 |
@SuppressWarnings("deprecation") |
2 | 87 |
public class TCPTransport extends Transport { |
88 |
||
89 |
/* tcp package log */ |
|
90 |
static final Log tcpLog = Log.getLog("sun.rmi.transport.tcp", "tcp", |
|
91 |
LogStream.parseLevel(AccessController.doPrivileged( |
|
23333
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
19211
diff
changeset
|
92 |
(PrivilegedAction<String>) () -> System.getProperty("sun.rmi.transport.tcp.logLevel")))); |
2 | 93 |
|
94 |
/** maximum number of connection handler threads */ |
|
95 |
private static final int maxConnectionThreads = // default no limit |
|
23333
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
19211
diff
changeset
|
96 |
AccessController.doPrivileged((PrivilegedAction<Integer>) () -> |
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
19211
diff
changeset
|
97 |
Integer.getInteger("sun.rmi.transport.tcp.maxConnectionThreads", |
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
19211
diff
changeset
|
98 |
Integer.MAX_VALUE)); |
2 | 99 |
|
100 |
/** keep alive time for idle connection handler threads */ |
|
101 |
private static final long threadKeepAliveTime = // default 1 minute |
|
23333
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
19211
diff
changeset
|
102 |
AccessController.doPrivileged((PrivilegedAction<Long>) () -> |
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
19211
diff
changeset
|
103 |
Long.getLong("sun.rmi.transport.tcp.threadKeepAliveTime", 60000)); |
2 | 104 |
|
105 |
/** thread pool for connection handlers */ |
|
106 |
private static final ExecutorService connectionThreadPool = |
|
107 |
new ThreadPoolExecutor(0, maxConnectionThreads, |
|
108 |
threadKeepAliveTime, TimeUnit.MILLISECONDS, |
|
109 |
new SynchronousQueue<Runnable>(), |
|
110 |
new ThreadFactory() { |
|
111 |
public Thread newThread(Runnable runnable) { |
|
112 |
return AccessController.doPrivileged(new NewThreadAction( |
|
113 |
runnable, "TCP Connection(idle)", true, true)); |
|
114 |
} |
|
115 |
}); |
|
116 |
||
117 |
/** total connections handled */ |
|
118 |
private static final AtomicInteger connectionCount = new AtomicInteger(0); |
|
119 |
||
120 |
/** client host for the current thread's connection */ |
|
121 |
private static final ThreadLocal<ConnectionHandler> |
|
12040
558b0e0d5910
7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents:
5506
diff
changeset
|
122 |
threadConnectionHandler = new ThreadLocal<>(); |
2 | 123 |
|
28552
608626229264
8055309: RMI needs better transportation considerations
smarks
parents:
25859
diff
changeset
|
124 |
/** an AccessControlContext with no permissions */ |
608626229264
8055309: RMI needs better transportation considerations
smarks
parents:
25859
diff
changeset
|
125 |
private static final AccessControlContext NOPERMS_ACC; |
608626229264
8055309: RMI needs better transportation considerations
smarks
parents:
25859
diff
changeset
|
126 |
static { |
608626229264
8055309: RMI needs better transportation considerations
smarks
parents:
25859
diff
changeset
|
127 |
Permissions perms = new Permissions(); |
608626229264
8055309: RMI needs better transportation considerations
smarks
parents:
25859
diff
changeset
|
128 |
ProtectionDomain[] pd = { new ProtectionDomain(null, perms) }; |
608626229264
8055309: RMI needs better transportation considerations
smarks
parents:
25859
diff
changeset
|
129 |
NOPERMS_ACC = new AccessControlContext(pd); |
608626229264
8055309: RMI needs better transportation considerations
smarks
parents:
25859
diff
changeset
|
130 |
} |
608626229264
8055309: RMI needs better transportation considerations
smarks
parents:
25859
diff
changeset
|
131 |
|
2 | 132 |
/** endpoints for this transport */ |
133 |
private final LinkedList<TCPEndpoint> epList; |
|
134 |
/** number of objects exported on this transport */ |
|
135 |
private int exportCount = 0; |
|
136 |
/** server socket for this transport */ |
|
137 |
private ServerSocket server = null; |
|
138 |
/** table mapping endpoints to channels */ |
|
139 |
private final Map<TCPEndpoint,Reference<TCPChannel>> channelTable = |
|
12040
558b0e0d5910
7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents:
5506
diff
changeset
|
140 |
new WeakHashMap<>(); |
2 | 141 |
|
142 |
static final RMISocketFactory defaultSocketFactory = |
|
143 |
RMISocketFactory.getDefaultSocketFactory(); |
|
144 |
||
145 |
/** number of milliseconds in accepted-connection timeout. |
|
146 |
* Warning: this should be greater than 15 seconds (the client-side |
|
147 |
* timeout), and defaults to 2 hours. |
|
148 |
* The maximum representable value is slightly more than 24 days |
|
149 |
* and 20 hours. |
|
150 |
*/ |
|
151 |
private static final int connectionReadTimeout = // default 2 hours |
|
23333
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
19211
diff
changeset
|
152 |
AccessController.doPrivileged((PrivilegedAction<Integer>) () -> |
b0af2c7c8c91
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents:
19211
diff
changeset
|
153 |
Integer.getInteger("sun.rmi.transport.tcp.readTimeout", 2 * 3600 * 1000)); |
2 | 154 |
|
155 |
/** |
|
156 |
* Constructs a TCPTransport. |
|
157 |
*/ |
|
158 |
TCPTransport(LinkedList<TCPEndpoint> epList) { |
|
159 |
// assert ((epList.size() != null) && (epList.size() >= 1)) |
|
160 |
this.epList = epList; |
|
161 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
|
162 |
tcpLog.log(Log.BRIEF, "Version = " + |
|
163 |
TransportConstants.Version + ", ep = " + getEndpoint()); |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
/** |
|
168 |
* Closes all cached connections in every channel subordinated to this |
|
169 |
* transport. Currently, this only closes outgoing connections. |
|
170 |
*/ |
|
171 |
public void shedConnectionCaches() { |
|
172 |
List<TCPChannel> channels; |
|
173 |
synchronized (channelTable) { |
|
174 |
channels = new ArrayList<TCPChannel>(channelTable.values().size()); |
|
175 |
for (Reference<TCPChannel> ref : channelTable.values()) { |
|
176 |
TCPChannel ch = ref.get(); |
|
177 |
if (ch != null) { |
|
178 |
channels.add(ch); |
|
179 |
} |
|
180 |
} |
|
181 |
} |
|
182 |
for (TCPChannel channel : channels) { |
|
183 |
channel.shedCache(); |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* Returns a <I>Channel</I> that generates connections to the |
|
189 |
* endpoint <I>ep</I>. A Channel is an object that creates and |
|
190 |
* manages connections of a particular type to some particular |
|
191 |
* address space. |
|
192 |
* @param ep the endpoint to which connections will be generated. |
|
193 |
* @return the channel or null if the transport cannot |
|
194 |
* generate connections to this endpoint |
|
195 |
*/ |
|
196 |
public TCPChannel getChannel(Endpoint ep) { |
|
197 |
TCPChannel ch = null; |
|
198 |
if (ep instanceof TCPEndpoint) { |
|
199 |
synchronized (channelTable) { |
|
200 |
Reference<TCPChannel> ref = channelTable.get(ep); |
|
201 |
if (ref != null) { |
|
202 |
ch = ref.get(); |
|
203 |
} |
|
204 |
if (ch == null) { |
|
205 |
TCPEndpoint tcpEndpoint = (TCPEndpoint) ep; |
|
206 |
ch = new TCPChannel(this, tcpEndpoint); |
|
207 |
channelTable.put(tcpEndpoint, |
|
208 |
new WeakReference<TCPChannel>(ch)); |
|
209 |
} |
|
210 |
} |
|
211 |
} |
|
212 |
return ch; |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Removes the <I>Channel</I> that generates connections to the |
|
217 |
* endpoint <I>ep</I>. |
|
218 |
*/ |
|
219 |
public void free(Endpoint ep) { |
|
220 |
if (ep instanceof TCPEndpoint) { |
|
221 |
synchronized (channelTable) { |
|
222 |
Reference<TCPChannel> ref = channelTable.remove(ep); |
|
223 |
if (ref != null) { |
|
224 |
TCPChannel channel = ref.get(); |
|
225 |
if (channel != null) { |
|
226 |
channel.shedCache(); |
|
227 |
} |
|
228 |
} |
|
229 |
} |
|
230 |
} |
|
231 |
} |
|
232 |
||
233 |
/** |
|
234 |
* Export the object so that it can accept incoming calls. |
|
235 |
*/ |
|
236 |
public void exportObject(Target target) throws RemoteException { |
|
237 |
/* |
|
238 |
* Ensure that a server socket is listening, and count this |
|
239 |
* export while synchronized to prevent the server socket from |
|
240 |
* being closed due to concurrent unexports. |
|
241 |
*/ |
|
242 |
synchronized (this) { |
|
243 |
listen(); |
|
244 |
exportCount++; |
|
245 |
} |
|
246 |
||
247 |
/* |
|
248 |
* Try to add the Target to the exported object table; keep |
|
249 |
* counting this export (to keep server socket open) only if |
|
250 |
* that succeeds. |
|
251 |
*/ |
|
252 |
boolean ok = false; |
|
253 |
try { |
|
254 |
super.exportObject(target); |
|
255 |
ok = true; |
|
256 |
} finally { |
|
257 |
if (!ok) { |
|
258 |
synchronized (this) { |
|
259 |
decrementExportCount(); |
|
260 |
} |
|
261 |
} |
|
262 |
} |
|
263 |
} |
|
264 |
||
265 |
protected synchronized void targetUnexported() { |
|
266 |
decrementExportCount(); |
|
267 |
} |
|
268 |
||
269 |
/** |
|
270 |
* Decrements the count of exported objects, closing the current |
|
271 |
* server socket if the count reaches zero. |
|
272 |
**/ |
|
273 |
private void decrementExportCount() { |
|
274 |
assert Thread.holdsLock(this); |
|
275 |
exportCount--; |
|
59300
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
276 |
if (tcpLog.isLoggable(Log.VERBOSE)) { |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
277 |
tcpLog.log(Log.VERBOSE, |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
278 |
"server socket: " + server + ", exportCount: " + exportCount); |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
279 |
} |
2 | 280 |
if (exportCount == 0 && getEndpoint().getListenPort() != 0) { |
281 |
ServerSocket ss = server; |
|
282 |
server = null; |
|
283 |
try { |
|
59300
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
284 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
285 |
tcpLog.log(Log.BRIEF, "server socket close: " + ss); |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
286 |
} |
2 | 287 |
ss.close(); |
288 |
} catch (IOException e) { |
|
59300
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
289 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
290 |
tcpLog.log(Log.BRIEF, |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
291 |
"server socket close throws: " + e); |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
292 |
} |
2 | 293 |
} |
294 |
} |
|
295 |
} |
|
296 |
||
297 |
/** |
|
298 |
* Verify that the current access control context has permission to |
|
299 |
* accept the connection being dispatched by the current thread. |
|
300 |
*/ |
|
301 |
protected void checkAcceptPermission(AccessControlContext acc) { |
|
302 |
SecurityManager sm = System.getSecurityManager(); |
|
303 |
if (sm == null) { |
|
304 |
return; |
|
305 |
} |
|
306 |
ConnectionHandler h = threadConnectionHandler.get(); |
|
307 |
if (h == null) { |
|
308 |
throw new Error( |
|
309 |
"checkAcceptPermission not in ConnectionHandler thread"); |
|
310 |
} |
|
311 |
h.checkAcceptPermission(sm, acc); |
|
312 |
} |
|
313 |
||
314 |
private TCPEndpoint getEndpoint() { |
|
315 |
synchronized (epList) { |
|
316 |
return epList.getLast(); |
|
317 |
} |
|
318 |
} |
|
319 |
||
320 |
/** |
|
321 |
* Listen on transport's endpoint. |
|
322 |
*/ |
|
323 |
private void listen() throws RemoteException { |
|
324 |
assert Thread.holdsLock(this); |
|
325 |
TCPEndpoint ep = getEndpoint(); |
|
326 |
int port = ep.getPort(); |
|
327 |
||
328 |
if (server == null) { |
|
329 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
|
330 |
tcpLog.log(Log.BRIEF, |
|
331 |
"(port " + port + ") create server socket"); |
|
332 |
} |
|
333 |
||
334 |
try { |
|
335 |
server = ep.newServerSocket(); |
|
336 |
/* |
|
337 |
* Don't retry ServerSocket if creation fails since |
|
338 |
* "port in use" will cause export to hang if an |
|
339 |
* RMIFailureHandler is not installed. |
|
340 |
*/ |
|
341 |
Thread t = AccessController.doPrivileged( |
|
342 |
new NewThreadAction(new AcceptLoop(server), |
|
343 |
"TCP Accept-" + port, true)); |
|
344 |
t.start(); |
|
345 |
} catch (java.net.BindException e) { |
|
346 |
throw new ExportException("Port already in use: " + port, e); |
|
347 |
} catch (IOException e) { |
|
348 |
throw new ExportException("Listen failed on port: " + port, e); |
|
349 |
} |
|
350 |
||
351 |
} else { |
|
352 |
// otherwise verify security access to existing server socket |
|
353 |
SecurityManager sm = System.getSecurityManager(); |
|
354 |
if (sm != null) { |
|
355 |
sm.checkListen(port); |
|
356 |
} |
|
357 |
} |
|
358 |
} |
|
359 |
||
360 |
/** |
|
361 |
* Worker for accepting connections from a server socket. |
|
362 |
**/ |
|
363 |
private class AcceptLoop implements Runnable { |
|
364 |
||
365 |
private final ServerSocket serverSocket; |
|
366 |
||
367 |
// state for throttling loop on exceptions (local to accept thread) |
|
368 |
private long lastExceptionTime = 0L; |
|
369 |
private int recentExceptionCount; |
|
370 |
||
371 |
AcceptLoop(ServerSocket serverSocket) { |
|
372 |
this.serverSocket = serverSocket; |
|
373 |
} |
|
374 |
||
375 |
public void run() { |
|
376 |
try { |
|
377 |
executeAcceptLoop(); |
|
378 |
} finally { |
|
379 |
try { |
|
59300
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
380 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
381 |
tcpLog.log(Log.BRIEF, |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
382 |
"server socket close: " + serverSocket); |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
383 |
} |
2 | 384 |
/* |
385 |
* Only one accept loop is started per server |
|
386 |
* socket, so after no more connections will be |
|
387 |
* accepted, ensure that the server socket is no |
|
388 |
* longer listening. |
|
389 |
*/ |
|
390 |
serverSocket.close(); |
|
391 |
} catch (IOException e) { |
|
59300
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
392 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
393 |
tcpLog.log(Log.BRIEF, |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
394 |
"server socket close throws: " + e); |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
395 |
} |
2 | 396 |
} |
397 |
} |
|
398 |
} |
|
399 |
||
400 |
/** |
|
401 |
* Accepts connections from the server socket and executes |
|
402 |
* handlers for them in the thread pool. |
|
403 |
**/ |
|
404 |
private void executeAcceptLoop() { |
|
405 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
|
406 |
tcpLog.log(Log.BRIEF, "listening on port " + |
|
407 |
getEndpoint().getPort()); |
|
408 |
} |
|
409 |
||
410 |
while (true) { |
|
411 |
Socket socket = null; |
|
412 |
try { |
|
413 |
socket = serverSocket.accept(); |
|
414 |
||
415 |
/* |
|
416 |
* Find client host name (or "0.0.0.0" if unknown) |
|
417 |
*/ |
|
418 |
InetAddress clientAddr = socket.getInetAddress(); |
|
419 |
String clientHost = (clientAddr != null |
|
420 |
? clientAddr.getHostAddress() |
|
421 |
: "0.0.0.0"); |
|
422 |
||
423 |
/* |
|
424 |
* Execute connection handler in the thread pool, |
|
425 |
* which uses non-system threads. |
|
426 |
*/ |
|
427 |
try { |
|
428 |
connectionThreadPool.execute( |
|
429 |
new ConnectionHandler(socket, clientHost)); |
|
430 |
} catch (RejectedExecutionException e) { |
|
431 |
closeSocket(socket); |
|
432 |
tcpLog.log(Log.BRIEF, |
|
433 |
"rejected connection from " + clientHost); |
|
434 |
} |
|
435 |
||
436 |
} catch (Throwable t) { |
|
437 |
try { |
|
438 |
/* |
|
439 |
* If the server socket has been closed, such |
|
440 |
* as because there are no more exported |
|
441 |
* objects, then we expect accept to throw an |
|
442 |
* exception, so just terminate normally. |
|
443 |
*/ |
|
444 |
if (serverSocket.isClosed()) { |
|
445 |
break; |
|
446 |
} |
|
447 |
||
448 |
try { |
|
449 |
if (tcpLog.isLoggable(Level.WARNING)) { |
|
450 |
tcpLog.log(Level.WARNING, |
|
451 |
"accept loop for " + serverSocket + |
|
452 |
" throws", t); |
|
453 |
} |
|
454 |
} catch (Throwable tt) { |
|
455 |
} |
|
456 |
} finally { |
|
457 |
/* |
|
458 |
* Always close the accepted socket (if any) |
|
459 |
* if an exception occurs, but only after |
|
460 |
* logging an unexpected exception. |
|
461 |
*/ |
|
462 |
if (socket != null) { |
|
463 |
closeSocket(socket); |
|
464 |
} |
|
465 |
} |
|
466 |
||
467 |
/* |
|
468 |
* In case we're running out of file descriptors, |
|
469 |
* release resources held in caches. |
|
470 |
*/ |
|
471 |
if (!(t instanceof SecurityException)) { |
|
472 |
try { |
|
473 |
TCPEndpoint.shedConnectionCaches(); |
|
474 |
} catch (Throwable tt) { |
|
475 |
} |
|
476 |
} |
|
477 |
||
478 |
/* |
|
479 |
* A NoClassDefFoundError can occur if no file |
|
480 |
* descriptors are available, in which case this |
|
481 |
* loop should not terminate. |
|
482 |
*/ |
|
483 |
if (t instanceof Exception || |
|
484 |
t instanceof OutOfMemoryError || |
|
485 |
t instanceof NoClassDefFoundError) |
|
486 |
{ |
|
487 |
if (!continueAfterAcceptFailure(t)) { |
|
488 |
return; |
|
489 |
} |
|
490 |
// continue loop |
|
13408
7b1c9c089326
7187876: ClassCastException in TCPTransport.executeAcceptLoop
dmocek
parents:
12040
diff
changeset
|
491 |
} else if (t instanceof Error) { |
7b1c9c089326
7187876: ClassCastException in TCPTransport.executeAcceptLoop
dmocek
parents:
12040
diff
changeset
|
492 |
throw (Error) t; |
2 | 493 |
} else { |
13408
7b1c9c089326
7187876: ClassCastException in TCPTransport.executeAcceptLoop
dmocek
parents:
12040
diff
changeset
|
494 |
throw new UndeclaredThrowableException(t); |
2 | 495 |
} |
496 |
} |
|
497 |
} |
|
498 |
} |
|
499 |
||
500 |
/** |
|
501 |
* Returns true if the accept loop should continue after the |
|
502 |
* specified exception has been caught, or false if the accept |
|
503 |
* loop should terminate (closing the server socket). If |
|
504 |
* there is an RMIFailureHandler, this method returns the |
|
505 |
* result of passing the specified exception to it; otherwise, |
|
506 |
* this method always returns true, after sleeping to throttle |
|
507 |
* the accept loop if necessary. |
|
508 |
**/ |
|
509 |
private boolean continueAfterAcceptFailure(Throwable t) { |
|
510 |
RMIFailureHandler fh = RMISocketFactory.getFailureHandler(); |
|
511 |
if (fh != null) { |
|
512 |
return fh.failure(t instanceof Exception ? (Exception) t : |
|
513 |
new InvocationTargetException(t)); |
|
514 |
} else { |
|
515 |
throttleLoopOnException(); |
|
516 |
return true; |
|
517 |
} |
|
518 |
} |
|
519 |
||
520 |
/** |
|
521 |
* Throttles the accept loop after an exception has been |
|
522 |
* caught: if a burst of 10 exceptions in 5 seconds occurs, |
|
523 |
* then wait for 10 seconds to curb busy CPU usage. |
|
524 |
**/ |
|
525 |
private void throttleLoopOnException() { |
|
526 |
long now = System.currentTimeMillis(); |
|
527 |
if (lastExceptionTime == 0L || (now - lastExceptionTime) > 5000) { |
|
528 |
// last exception was long ago (or this is the first) |
|
529 |
lastExceptionTime = now; |
|
530 |
recentExceptionCount = 0; |
|
531 |
} else { |
|
532 |
// exception burst window was started recently |
|
533 |
if (++recentExceptionCount >= 10) { |
|
534 |
try { |
|
535 |
Thread.sleep(10000); |
|
536 |
} catch (InterruptedException ignore) { |
|
537 |
} |
|
538 |
} |
|
539 |
} |
|
540 |
} |
|
541 |
} |
|
542 |
||
543 |
/** close socket and eat exception */ |
|
544 |
private static void closeSocket(Socket sock) { |
|
545 |
try { |
|
59300
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
546 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
547 |
tcpLog.log(Log.BRIEF, "socket close: " + sock); |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
548 |
} |
2 | 549 |
sock.close(); |
550 |
} catch (IOException ex) { |
|
551 |
// eat exception |
|
59300
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
552 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
553 |
tcpLog.log(Log.BRIEF, "socket close throws: " + ex); |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
554 |
} |
2 | 555 |
} |
556 |
} |
|
557 |
||
558 |
/** |
|
559 |
* handleMessages decodes transport operations and handles messages |
|
560 |
* appropriately. If an exception occurs during message handling, |
|
561 |
* the socket is closed. |
|
562 |
*/ |
|
563 |
void handleMessages(Connection conn, boolean persistent) { |
|
564 |
int port = getEndpoint().getPort(); |
|
565 |
||
566 |
try { |
|
567 |
DataInputStream in = new DataInputStream(conn.getInputStream()); |
|
568 |
do { |
|
569 |
int op = in.read(); // transport op |
|
570 |
if (op == -1) { |
|
571 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
|
572 |
tcpLog.log(Log.BRIEF, "(port " + |
|
573 |
port + ") connection closed"); |
|
574 |
} |
|
575 |
break; |
|
576 |
} |
|
577 |
||
578 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
|
579 |
tcpLog.log(Log.BRIEF, "(port " + port + |
|
580 |
") op = " + op); |
|
581 |
} |
|
582 |
||
583 |
switch (op) { |
|
584 |
case TransportConstants.Call: |
|
585 |
// service incoming RMI call |
|
586 |
RemoteCall call = new StreamRemoteCall(conn); |
|
587 |
if (serviceCall(call) == false) |
|
588 |
return; |
|
589 |
break; |
|
590 |
||
591 |
case TransportConstants.Ping: |
|
592 |
// send ack for ping |
|
593 |
DataOutputStream out = |
|
594 |
new DataOutputStream(conn.getOutputStream()); |
|
595 |
out.writeByte(TransportConstants.PingAck); |
|
596 |
conn.releaseOutputStream(); |
|
597 |
break; |
|
598 |
||
599 |
case TransportConstants.DGCAck: |
|
600 |
DGCAckHandler.received(UID.read(in)); |
|
601 |
break; |
|
602 |
||
603 |
default: |
|
604 |
throw new IOException("unknown transport op " + op); |
|
605 |
} |
|
606 |
} while (persistent); |
|
607 |
||
608 |
} catch (IOException e) { |
|
609 |
// exception during processing causes connection to close (below) |
|
610 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
|
611 |
tcpLog.log(Log.BRIEF, "(port " + port + |
|
612 |
") exception: ", e); |
|
613 |
} |
|
614 |
} finally { |
|
615 |
try { |
|
616 |
conn.close(); |
|
617 |
} catch (IOException ex) { |
|
618 |
// eat exception |
|
59300
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
619 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
620 |
tcpLog.log(Log.BRIEF, "Connection close throws " + ex); |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
621 |
} |
2 | 622 |
} |
623 |
} |
|
624 |
} |
|
625 |
||
626 |
/** |
|
627 |
* Returns the client host for the current thread's connection. Throws |
|
628 |
* ServerNotActiveException if no connection is active for this thread. |
|
629 |
*/ |
|
630 |
public static String getClientHost() throws ServerNotActiveException { |
|
631 |
ConnectionHandler h = threadConnectionHandler.get(); |
|
632 |
if (h != null) { |
|
633 |
return h.getClientHost(); |
|
634 |
} else { |
|
635 |
throw new ServerNotActiveException("not in a remote call"); |
|
636 |
} |
|
637 |
} |
|
638 |
||
639 |
/** |
|
640 |
* Services messages on accepted connection |
|
641 |
*/ |
|
642 |
private class ConnectionHandler implements Runnable { |
|
643 |
||
644 |
/** int value of "POST" in ASCII (Java's specified data formats |
|
645 |
* make this once-reviled tactic again socially acceptable) */ |
|
646 |
private static final int POST = 0x504f5354; |
|
647 |
||
648 |
/** most recently accept-authorized AccessControlContext */ |
|
649 |
private AccessControlContext okContext; |
|
650 |
/** cache of accept-authorized AccessControlContexts */ |
|
651 |
private Map<AccessControlContext, |
|
652 |
Reference<AccessControlContext>> authCache; |
|
653 |
/** security manager which authorized contexts in authCache */ |
|
654 |
private SecurityManager cacheSecurityManager = null; |
|
655 |
||
656 |
private Socket socket; |
|
657 |
private String remoteHost; |
|
658 |
||
659 |
ConnectionHandler(Socket socket, String remoteHost) { |
|
660 |
this.socket = socket; |
|
661 |
this.remoteHost = remoteHost; |
|
662 |
} |
|
663 |
||
664 |
String getClientHost() { |
|
665 |
return remoteHost; |
|
666 |
} |
|
667 |
||
668 |
/** |
|
669 |
* Verify that the given AccessControlContext has permission to |
|
670 |
* accept this connection. |
|
671 |
*/ |
|
672 |
void checkAcceptPermission(SecurityManager sm, |
|
673 |
AccessControlContext acc) |
|
674 |
{ |
|
675 |
/* |
|
676 |
* Note: no need to synchronize on cache-related fields, since this |
|
677 |
* method only gets called from the ConnectionHandler's thread. |
|
678 |
*/ |
|
679 |
if (sm != cacheSecurityManager) { |
|
680 |
okContext = null; |
|
681 |
authCache = new WeakHashMap<AccessControlContext, |
|
682 |
Reference<AccessControlContext>>(); |
|
683 |
cacheSecurityManager = sm; |
|
684 |
} |
|
685 |
if (acc.equals(okContext) || authCache.containsKey(acc)) { |
|
686 |
return; |
|
687 |
} |
|
688 |
InetAddress addr = socket.getInetAddress(); |
|
689 |
String host = (addr != null) ? addr.getHostAddress() : "*"; |
|
690 |
||
691 |
sm.checkAccept(host, socket.getPort()); |
|
692 |
||
693 |
authCache.put(acc, new SoftReference<AccessControlContext>(acc)); |
|
694 |
okContext = acc; |
|
695 |
} |
|
696 |
||
697 |
public void run() { |
|
28557
ce28716935ce
8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents:
28552
diff
changeset
|
698 |
Thread t = Thread.currentThread(); |
ce28716935ce
8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents:
28552
diff
changeset
|
699 |
String name = t.getName(); |
ce28716935ce
8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents:
28552
diff
changeset
|
700 |
try { |
ce28716935ce
8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents:
28552
diff
changeset
|
701 |
t.setName("RMI TCP Connection(" + |
ce28716935ce
8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents:
28552
diff
changeset
|
702 |
connectionCount.incrementAndGet() + |
ce28716935ce
8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents:
28552
diff
changeset
|
703 |
")-" + remoteHost); |
ce28716935ce
8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents:
28552
diff
changeset
|
704 |
AccessController.doPrivileged((PrivilegedAction<Void>)() -> { |
ce28716935ce
8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents:
28552
diff
changeset
|
705 |
run0(); |
28552
608626229264
8055309: RMI needs better transportation considerations
smarks
parents:
25859
diff
changeset
|
706 |
return null; |
608626229264
8055309: RMI needs better transportation considerations
smarks
parents:
25859
diff
changeset
|
707 |
}, NOPERMS_ACC); |
28557
ce28716935ce
8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents:
28552
diff
changeset
|
708 |
} finally { |
ce28716935ce
8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents:
28552
diff
changeset
|
709 |
t.setName(name); |
ce28716935ce
8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents:
28552
diff
changeset
|
710 |
} |
2 | 711 |
} |
712 |
||
47032
98e444a1b204
8087189: RMI server-side multiplex protocol support should be removed
rriggs
parents:
41883
diff
changeset
|
713 |
@SuppressWarnings("fallthrough") |
2 | 714 |
private void run0() { |
715 |
TCPEndpoint endpoint = getEndpoint(); |
|
716 |
int port = endpoint.getPort(); |
|
717 |
||
718 |
threadConnectionHandler.set(this); |
|
719 |
||
720 |
// set socket to disable Nagle's algorithm (always send |
|
721 |
// immediately) |
|
722 |
// TBD: should this be left up to socket factory instead? |
|
723 |
try { |
|
724 |
socket.setTcpNoDelay(true); |
|
725 |
} catch (Exception e) { |
|
726 |
// if we fail to set this, ignore and proceed anyway |
|
727 |
} |
|
728 |
// set socket to timeout after excessive idle time |
|
729 |
try { |
|
730 |
if (connectionReadTimeout > 0) |
|
731 |
socket.setSoTimeout(connectionReadTimeout); |
|
732 |
} catch (Exception e) { |
|
733 |
// too bad, continue anyway |
|
734 |
} |
|
735 |
||
736 |
try { |
|
737 |
InputStream sockIn = socket.getInputStream(); |
|
738 |
InputStream bufIn = sockIn.markSupported() |
|
739 |
? sockIn |
|
740 |
: new BufferedInputStream(sockIn); |
|
741 |
||
37892
55da13d60938
8155978: Remove HTTP proxy implementation and tests from RMI
rriggs
parents:
37667
diff
changeset
|
742 |
// Read magic |
2 | 743 |
DataInputStream in = new DataInputStream(bufIn); |
744 |
int magic = in.readInt(); |
|
745 |
||
746 |
// read and verify transport header |
|
747 |
short version = in.readShort(); |
|
748 |
if (magic != TransportConstants.Magic || |
|
749 |
version != TransportConstants.Version) { |
|
750 |
// protocol mismatch detected... |
|
751 |
// just close socket: this would recurse if we marshal an |
|
752 |
// exception to the client and the protocol at other end |
|
753 |
// doesn't match. |
|
59300
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
754 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
755 |
tcpLog.log(Log.BRIEF, "magic or version not match: " |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
756 |
+ magic + ", " + version); |
60bdcb9a7b94
8232446: logging enhancement for rmi when socket closed
mli
parents:
47216
diff
changeset
|
757 |
} |
2 | 758 |
closeSocket(socket); |
759 |
return; |
|
760 |
} |
|
761 |
||
762 |
OutputStream sockOut = socket.getOutputStream(); |
|
763 |
BufferedOutputStream bufOut = |
|
764 |
new BufferedOutputStream(sockOut); |
|
765 |
DataOutputStream out = new DataOutputStream(bufOut); |
|
766 |
||
767 |
int remotePort = socket.getPort(); |
|
768 |
||
769 |
if (tcpLog.isLoggable(Log.BRIEF)) { |
|
770 |
tcpLog.log(Log.BRIEF, "accepted socket from [" + |
|
771 |
remoteHost + ":" + remotePort + "]"); |
|
772 |
} |
|
773 |
||
774 |
TCPEndpoint ep; |
|
775 |
TCPChannel ch; |
|
776 |
TCPConnection conn; |
|
777 |
||
778 |
// send ack (or nack) for protocol |
|
779 |
byte protocol = in.readByte(); |
|
780 |
switch (protocol) { |
|
781 |
case TransportConstants.SingleOpProtocol: |
|
782 |
// no ack for protocol |
|
783 |
||
784 |
// create dummy channel for receiving messages |
|
785 |
ep = new TCPEndpoint(remoteHost, socket.getLocalPort(), |
|
786 |
endpoint.getClientSocketFactory(), |
|
787 |
endpoint.getServerSocketFactory()); |
|
788 |
ch = new TCPChannel(TCPTransport.this, ep); |
|
789 |
conn = new TCPConnection(ch, socket, bufIn, bufOut); |
|
790 |
||
791 |
// read input messages |
|
792 |
handleMessages(conn, false); |
|
793 |
break; |
|
794 |
||
795 |
case TransportConstants.StreamProtocol: |
|
796 |
// send ack |
|
797 |
out.writeByte(TransportConstants.ProtocolAck); |
|
798 |
||
799 |
// suggest endpoint (in case client doesn't know host name) |
|
800 |
if (tcpLog.isLoggable(Log.VERBOSE)) { |
|
801 |
tcpLog.log(Log.VERBOSE, "(port " + port + |
|
802 |
") " + "suggesting " + remoteHost + ":" + |
|
803 |
remotePort); |
|
804 |
} |
|
805 |
||
806 |
out.writeUTF(remoteHost); |
|
807 |
out.writeInt(remotePort); |
|
808 |
out.flush(); |
|
809 |
||
810 |
// read and discard (possibly bogus) endpoint |
|
811 |
// REMIND: would be faster to read 2 bytes then skip N+4 |
|
812 |
String clientHost = in.readUTF(); |
|
813 |
int clientPort = in.readInt(); |
|
814 |
if (tcpLog.isLoggable(Log.VERBOSE)) { |
|
815 |
tcpLog.log(Log.VERBOSE, "(port " + port + |
|
816 |
") client using " + clientHost + ":" + clientPort); |
|
817 |
} |
|
818 |
||
819 |
// create dummy channel for receiving messages |
|
820 |
// (why not use clientHost and clientPort?) |
|
821 |
ep = new TCPEndpoint(remoteHost, socket.getLocalPort(), |
|
822 |
endpoint.getClientSocketFactory(), |
|
823 |
endpoint.getServerSocketFactory()); |
|
824 |
ch = new TCPChannel(TCPTransport.this, ep); |
|
825 |
conn = new TCPConnection(ch, socket, bufIn, bufOut); |
|
826 |
||
827 |
// read input messages |
|
828 |
handleMessages(conn, true); |
|
829 |
break; |
|
830 |
||
831 |
case TransportConstants.MultiplexProtocol: |
|
832 |
if (tcpLog.isLoggable(Log.VERBOSE)) { |
|
833 |
tcpLog.log(Log.VERBOSE, "(port " + port + |
|
47032
98e444a1b204
8087189: RMI server-side multiplex protocol support should be removed
rriggs
parents:
41883
diff
changeset
|
834 |
") rejecting multiplex protocol"); |
2 | 835 |
} |
47032
98e444a1b204
8087189: RMI server-side multiplex protocol support should be removed
rriggs
parents:
41883
diff
changeset
|
836 |
// Fall-through to reject use of MultiplexProtocol |
2 | 837 |
default: |
838 |
// protocol not understood, send nack and close socket |
|
839 |
out.writeByte(TransportConstants.ProtocolNack); |
|
840 |
out.flush(); |
|
841 |
break; |
|
842 |
} |
|
843 |
||
844 |
} catch (IOException e) { |
|
845 |
// socket in unknown state: destroy socket |
|
846 |
tcpLog.log(Log.BRIEF, "terminated with exception:", e); |
|
847 |
} finally { |
|
848 |
closeSocket(socket); |
|
849 |
} |
|
850 |
} |
|
851 |
} |
|
852 |
} |