author | michaelm |
Tue, 29 Oct 2019 19:23:09 +0000 | |
branch | unixdomainchannels |
changeset 58847 | 692de65ab293 |
parent 58801 | 119ac9128c1b |
child 58981 | 5c79956cc7d7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58295
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2003, 2019, 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 |
package sun.nio.ch; |
|
27 |
||
28 |
import java.lang.reflect.Constructor; |
|
29 |
import java.io.FileDescriptor; |
|
30 |
import java.io.IOException; |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
31 |
import java.io.UncheckedIOException; |
2 | 32 |
import java.net.InetAddress; |
33 |
import java.net.InetSocketAddress; |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
34 |
import java.net.Socket; |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
35 |
import java.net.SocketAddress; |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
36 |
import java.net.SocketOption; |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
37 |
import java.nio.ByteBuffer; |
2 | 38 |
import java.nio.channels.Channel; |
39 |
import java.nio.channels.SocketChannel; |
|
40 |
import java.nio.channels.ServerSocketChannel; |
|
41 |
import java.nio.channels.DatagramChannel; |
|
42 |
import java.nio.channels.spi.SelectorProvider; |
|
43 |
||
58847 | 44 |
class InheritedChannel { |
2 | 45 |
|
46 |
// the "types" of socket returned by soType0 |
|
47 |
private static final int UNKNOWN = -1; |
|
48 |
private static final int SOCK_STREAM = 1; |
|
49 |
private static final int SOCK_DGRAM = 2; |
|
50 |
||
58295
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
51 |
// socket address type |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
52 |
private static final int AF_UNKNOWN = -1; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
53 |
private static final int AF_INET = 1; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
54 |
private static final int AF_INET6 = 2; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
55 |
private static final int AF_UNIX = 3; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
56 |
|
2 | 57 |
// oflag values when opening a file |
58 |
private static final int O_RDONLY = 0; |
|
59 |
private static final int O_WRONLY = 1; |
|
60 |
private static final int O_RDWR = 2; |
|
61 |
||
62 |
/* |
|
63 |
* In order to "detach" the standard streams we dup them to /dev/null. |
|
64 |
* In order to reduce the possibility of an error at close time we |
|
65 |
* open /dev/null early - that way we know we won't run out of file |
|
66 |
* descriptors at close time. This makes the close operation a |
|
67 |
* simple dup2 operation for each of the standard streams. |
|
68 |
*/ |
|
69 |
private static int devnull = -1; |
|
70 |
||
71 |
private static void detachIOStreams() { |
|
72 |
try { |
|
73 |
dup2(devnull, 0); |
|
74 |
dup2(devnull, 1); |
|
75 |
dup2(devnull, 2); |
|
76 |
} catch (IOException ioe) { |
|
77 |
// this shouldn't happen |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
10351
diff
changeset
|
78 |
throw new InternalError(ioe); |
2 | 79 |
} |
80 |
} |
|
81 |
||
82 |
/* |
|
83 |
* Override the implCloseSelectableChannel for each channel type - this |
|
84 |
* allows us to "detach" the standard streams after closing and ensures |
|
85 |
* that the underlying socket really closes. |
|
86 |
*/ |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
87 |
public static class InheritedInetSocketChannelImpl extends InetSocketChannelImpl { |
2 | 88 |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
89 |
InheritedInetSocketChannelImpl(SelectorProvider sp, |
2 | 90 |
FileDescriptor fd, |
91 |
InetSocketAddress remote) |
|
92 |
throws IOException |
|
93 |
{ |
|
94 |
super(sp, fd, remote); |
|
95 |
} |
|
96 |
||
97 |
protected void implCloseSelectableChannel() throws IOException { |
|
98 |
super.implCloseSelectableChannel(); |
|
99 |
detachIOStreams(); |
|
100 |
} |
|
101 |
} |
|
102 |
||
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
103 |
public static class InheritedUnixSocketChannelImpl extends UnixDomainSocketChannelImpl { |
58295
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
104 |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
105 |
InheritedUnixSocketChannelImpl(SelectorProvider sp, FileDescriptor fd) |
58295
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
106 |
throws IOException |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
107 |
{ |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
108 |
super(sp, fd, true); |
58295
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
109 |
} |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
110 |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
111 |
@Override |
58295
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
112 |
protected void implCloseSelectableChannel() throws IOException { |
58847 | 113 |
super.implCloseSelectableChannel(); |
58295
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
114 |
detachIOStreams(); |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
115 |
} |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
116 |
} |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
117 |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
118 |
public static class InheritedInetServerSocketChannelImpl extends |
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
119 |
InetServerSocketChannelImpl { |
2 | 120 |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
121 |
InheritedInetServerSocketChannelImpl(SelectorProvider sp, |
2 | 122 |
FileDescriptor fd) |
123 |
throws IOException |
|
124 |
{ |
|
6525 | 125 |
super(sp, fd, true); |
2 | 126 |
} |
127 |
||
128 |
protected void implCloseSelectableChannel() throws IOException { |
|
129 |
super.implCloseSelectableChannel(); |
|
130 |
detachIOStreams(); |
|
131 |
} |
|
132 |
||
133 |
} |
|
134 |
||
135 |
public static class InheritedDatagramChannelImpl extends |
|
136 |
DatagramChannelImpl { |
|
137 |
||
138 |
InheritedDatagramChannelImpl(SelectorProvider sp, |
|
139 |
FileDescriptor fd) |
|
140 |
throws IOException |
|
141 |
{ |
|
142 |
super(sp, fd); |
|
143 |
} |
|
144 |
||
145 |
protected void implCloseSelectableChannel() throws IOException { |
|
146 |
super.implCloseSelectableChannel(); |
|
147 |
detachIOStreams(); |
|
148 |
} |
|
149 |
} |
|
150 |
||
151 |
/* |
|
152 |
* If there's a SecurityManager then check for the appropriate |
|
153 |
* RuntimePermission. |
|
154 |
*/ |
|
155 |
private static void checkAccess(Channel c) { |
|
156 |
SecurityManager sm = System.getSecurityManager(); |
|
157 |
if (sm != null) { |
|
158 |
sm.checkPermission( |
|
159 |
new RuntimePermission("inheritedChannel") |
|
160 |
); |
|
161 |
} |
|
162 |
} |
|
163 |
||
164 |
||
165 |
/* |
|
166 |
* If standard inherited channel is connected to a socket then return a Channel |
|
167 |
* of the appropriate type based standard input. |
|
168 |
*/ |
|
169 |
private static Channel createChannel() throws IOException { |
|
170 |
||
171 |
// dup the file descriptor - we do this so that for two reasons :- |
|
172 |
// 1. Avoids any timing issues with FileDescriptor.in being closed |
|
173 |
// or redirected while we create the channel. |
|
174 |
// 2. Allows streams based on file descriptor 0 to co-exist with |
|
175 |
// the channel (closing one doesn't impact the other) |
|
176 |
||
177 |
int fdVal = dup(0); |
|
178 |
||
179 |
// Examine the file descriptor - if it's not a socket then we don't |
|
180 |
// create a channel so we release the file descriptor. |
|
181 |
||
182 |
int st; |
|
183 |
st = soType0(fdVal); |
|
184 |
if (st != SOCK_STREAM && st != SOCK_DGRAM) { |
|
185 |
close0(fdVal); |
|
186 |
return null; |
|
187 |
} |
|
188 |
||
189 |
// Next we create a FileDescriptor for the dup'ed file descriptor |
|
190 |
// Have to use reflection and also make assumption on how FD |
|
191 |
// is implemented. |
|
192 |
||
25798
0b2f54e47bc4
8054050: Fix stay raw and unchecked lint warnings in core libs
darcy
parents:
23010
diff
changeset
|
193 |
Class<?> paramTypes[] = { int.class }; |
10351
e2cef0ef9e28
7081796: (ch) rawtype warning in sun.nio.ch.InheritedChannel
alanb
parents:
7668
diff
changeset
|
194 |
Constructor<?> ctr = Reflect.lookupConstructor("java.io.FileDescriptor", |
e2cef0ef9e28
7081796: (ch) rawtype warning in sun.nio.ch.InheritedChannel
alanb
parents:
7668
diff
changeset
|
195 |
paramTypes); |
37521
b6e0f285c998
8145468: update java.lang APIs with new deprecations
smarks
parents:
26207
diff
changeset
|
196 |
Object args[] = { Integer.valueOf(fdVal) }; |
2 | 197 |
FileDescriptor fd = (FileDescriptor)Reflect.invoke(ctr, args); |
198 |
||
199 |
||
200 |
// Now create the channel. If the socket is a streams socket then |
|
201 |
// we see if tthere is a peer (ie: connected). If so, then we |
|
202 |
// create a SocketChannel, otherwise a ServerSocketChannel. |
|
203 |
// If the socket is a datagram socket then create a DatagramChannel |
|
204 |
||
205 |
SelectorProvider provider = SelectorProvider.provider(); |
|
206 |
assert provider instanceof sun.nio.ch.SelectorProviderImpl; |
|
207 |
||
208 |
Channel c; |
|
209 |
if (st == SOCK_STREAM) { |
|
58295
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
210 |
int family = addressFamily(fdVal); |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
211 |
if (family == AF_UNKNOWN) |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
212 |
return null; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
213 |
if (family == AF_UNIX) { |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
214 |
if (isConnected(fdVal)) { |
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
215 |
return new InheritedUnixSocketChannelImpl(provider, fd); |
58295
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
216 |
} else { |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
217 |
// listener. unsupported. |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
218 |
return null; |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
219 |
} |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
220 |
} |
2 | 221 |
InetAddress ia = peerAddress0(fdVal); |
222 |
if (ia == null) { |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
223 |
c = new InheritedInetServerSocketChannelImpl(provider, fd); |
2 | 224 |
} else { |
225 |
int port = peerPort0(fdVal); |
|
226 |
assert port > 0; |
|
227 |
InetSocketAddress isa = new InetSocketAddress(ia, port); |
|
58801
119ac9128c1b
Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents:
58295
diff
changeset
|
228 |
c = new InheritedInetSocketChannelImpl(provider, fd, isa); |
2 | 229 |
} |
230 |
} else { |
|
231 |
c = new InheritedDatagramChannelImpl(provider, fd); |
|
232 |
} |
|
233 |
return c; |
|
234 |
} |
|
235 |
||
236 |
private static boolean haveChannel = false; |
|
237 |
private static Channel channel = null; |
|
238 |
||
239 |
/* |
|
240 |
* Returns a Channel representing the inherited channel if the |
|
241 |
* inherited channel is a stream connected to a network socket. |
|
242 |
*/ |
|
243 |
public static synchronized Channel getChannel() throws IOException { |
|
244 |
if (devnull < 0) { |
|
245 |
devnull = open0("/dev/null", O_RDWR); |
|
246 |
} |
|
247 |
||
248 |
// If we don't have the channel try to create it |
|
249 |
if (!haveChannel) { |
|
250 |
channel = createChannel(); |
|
251 |
haveChannel = true; |
|
252 |
} |
|
253 |
||
254 |
// if there is a channel then do the security check before |
|
255 |
// returning it. |
|
256 |
if (channel != null) { |
|
257 |
checkAccess(channel); |
|
258 |
} |
|
259 |
return channel; |
|
260 |
} |
|
261 |
||
262 |
||
263 |
// -- Native methods -- |
|
264 |
||
26207
a02f6165d5be
8055955: (ch) Remove unnecessary initialization of InetAddress from FileChannel
chegar
parents:
25859
diff
changeset
|
265 |
private static native void initIDs(); |
2 | 266 |
private static native int dup(int fd) throws IOException; |
267 |
private static native void dup2(int fd, int fd2) throws IOException; |
|
268 |
private static native int open0(String path, int oflag) throws IOException; |
|
269 |
private static native void close0(int fd) throws IOException; |
|
270 |
private static native int soType0(int fd); |
|
58295
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
271 |
private static native int addressFamily(int fd); |
2 | 272 |
private static native InetAddress peerAddress0(int fd); |
273 |
private static native int peerPort0(int fd); |
|
274 |
||
58295
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
275 |
// return true if socket is connected to a peer |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
276 |
private static native boolean isConnected(int fd); |
7973073dd048
8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents:
47216
diff
changeset
|
277 |
|
2 | 278 |
static { |
19607
bee007586d06
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents:
14342
diff
changeset
|
279 |
IOUtil.load(); |
26207
a02f6165d5be
8055955: (ch) Remove unnecessary initialization of InetAddress from FileChannel
chegar
parents:
25859
diff
changeset
|
280 |
initIDs(); |
2 | 281 |
} |
282 |
} |