src/java.base/unix/classes/sun/nio/ch/InheritedChannel.java
author michaelm
Fri, 08 Nov 2019 09:46:55 +0000
branchunixdomainchannels
changeset 58981 5c79956cc7d7
parent 58847 692de65ab293
child 58988 b88e23c84b23
permissions -rw-r--r--
Minor updates to spec, and fix test issues
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.nio.channels.Channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.nio.channels.SocketChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.nio.channels.ServerSocketChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.nio.channels.DatagramChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.nio.channels.spi.SelectorProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
58847
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
    44
class InheritedChannel {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // the "types" of socket returned by soType0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final int UNKNOWN            = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final int SOCK_STREAM        = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int SOCK_DGRAM         = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // oflag values when opening a file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static final int O_RDONLY           = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final int O_WRONLY           = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final int O_RDWR             = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * In order to "detach" the standard streams we dup them to /dev/null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * In order to reduce the possibility of an error at close time we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * open /dev/null early - that way we know we won't run out of file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * descriptors at close time. This makes the close operation a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * simple dup2 operation for each of the standard streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static int devnull = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static void detachIOStreams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            dup2(devnull, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            dup2(devnull, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            dup2(devnull, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Override the implCloseSelectableChannel for each channel type - this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * allows us to "detach" the standard streams after closing and ensures
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * that the underlying socket really closes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                   FileDescriptor fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                   InetSocketAddress remote)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            super(sp, fd, remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        protected void implCloseSelectableChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            super.implCloseSelectableChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            detachIOStreams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
692de65ab293 partial cleanup
michaelm
parents: 58801
diff changeset
   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
58981
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   118
    public static class InheritedUnixServerSocketChannelImpl extends UnixDomainServerSocketChannelImpl {
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   119
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   120
        InheritedUnixServerSocketChannelImpl(SelectorProvider sp, FileDescriptor fd)
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   121
            throws IOException
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   122
        {
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   123
            super(sp, fd, true);
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   124
        }
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   125
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   126
        @Override
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   127
        protected void implCloseSelectableChannel() throws IOException {
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   128
            super.implCloseSelectableChannel();
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   129
            detachIOStreams();
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   130
        }
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   131
    }
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   132
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 58295
diff changeset
   133
    public static class InheritedInetServerSocketChannelImpl extends
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 58295
diff changeset
   134
        InetServerSocketChannelImpl {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 58295
diff changeset
   136
        InheritedInetServerSocketChannelImpl(SelectorProvider sp,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                         FileDescriptor fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        {
6525
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents: 5506
diff changeset
   140
            super(sp, fd, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        protected void implCloseSelectableChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            super.implCloseSelectableChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            detachIOStreams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public static class InheritedDatagramChannelImpl extends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        DatagramChannelImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        InheritedDatagramChannelImpl(SelectorProvider sp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                     FileDescriptor fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            super(sp, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        protected void implCloseSelectableChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            super.implCloseSelectableChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            detachIOStreams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * If there's a SecurityManager then check for the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * RuntimePermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private static void checkAccess(Channel c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            sm.checkPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                new RuntimePermission("inheritedChannel")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * If standard inherited channel is connected to a socket then return a Channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * of the appropriate type based standard input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private static Channel createChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // dup the file descriptor - we do this so that for two reasons :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // 1. Avoids any timing issues with FileDescriptor.in being closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        //    or redirected while we create the channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // 2. Allows streams based on file descriptor 0 to co-exist with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        //    the channel (closing one doesn't impact the other)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        int fdVal = dup(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // Examine the file descriptor - if it's not a socket then we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // create a channel so we release the file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        int st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        st = soType0(fdVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (st != SOCK_STREAM && st != SOCK_DGRAM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            close0(fdVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // Next we create a FileDescriptor for the dup'ed file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // Have to use reflection and also make assumption on how FD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // is implemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
25798
0b2f54e47bc4 8054050: Fix stay raw and unchecked lint warnings in core libs
darcy
parents: 23010
diff changeset
   208
        Class<?> paramTypes[] = { int.class };
10351
e2cef0ef9e28 7081796: (ch) rawtype warning in sun.nio.ch.InheritedChannel
alanb
parents: 7668
diff changeset
   209
        Constructor<?> ctr = Reflect.lookupConstructor("java.io.FileDescriptor",
e2cef0ef9e28 7081796: (ch) rawtype warning in sun.nio.ch.InheritedChannel
alanb
parents: 7668
diff changeset
   210
                                                       paramTypes);
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 26207
diff changeset
   211
        Object args[] = { Integer.valueOf(fdVal) };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        FileDescriptor fd = (FileDescriptor)Reflect.invoke(ctr, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // Now create the channel. If the socket is a streams socket then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        // we see if tthere is a peer (ie: connected). If so, then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // create a SocketChannel, otherwise a ServerSocketChannel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // If the socket is a datagram socket then create a DatagramChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        SelectorProvider provider = SelectorProvider.provider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        assert provider instanceof sun.nio.ch.SelectorProviderImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        Channel c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (st == SOCK_STREAM) {
58295
7973073dd048 8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents: 47216
diff changeset
   225
            int family = addressFamily(fdVal);
7973073dd048 8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents: 47216
diff changeset
   226
            if (family == AF_UNKNOWN)
7973073dd048 8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents: 47216
diff changeset
   227
                return null;
7973073dd048 8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents: 47216
diff changeset
   228
            if (family == AF_UNIX) {
7973073dd048 8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents: 47216
diff changeset
   229
                if (isConnected(fdVal)) {
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 58295
diff changeset
   230
                    return new InheritedUnixSocketChannelImpl(provider, fd);
58295
7973073dd048 8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents: 47216
diff changeset
   231
                } else {
58981
5c79956cc7d7 Minor updates to spec, and fix test issues
michaelm
parents: 58847
diff changeset
   232
                    return new InheritedUnixServerSocketChannelImpl(provider, fd);
58295
7973073dd048 8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents: 47216
diff changeset
   233
                }
7973073dd048 8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents: 47216
diff changeset
   234
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            InetAddress ia = peerAddress0(fdVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (ia == null) {
58801
119ac9128c1b Initial implementation of unix domain channels. See j.n.c.{Server}SocketChannel apidoc
michaelm
parents: 58295
diff changeset
   237
               c = new InheritedInetServerSocketChannelImpl(provider, fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
               int port = peerPort0(fdVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
               assert port > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
               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
   242
               c = new InheritedInetSocketChannelImpl(provider, fd, isa);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            c = new InheritedDatagramChannelImpl(provider, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private static boolean haveChannel = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private static Channel channel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Returns a Channel representing the inherited channel if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * inherited channel is a stream connected to a network socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public static synchronized Channel getChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (devnull < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            devnull = open0("/dev/null", O_RDWR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // If we don't have the channel try to create it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (!haveChannel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            channel = createChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            haveChannel = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // if there is a channel then do the security check before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // returning it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (channel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            checkAccess(channel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    // -- Native methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
26207
a02f6165d5be 8055955: (ch) Remove unnecessary initialization of InetAddress from FileChannel
chegar
parents: 25859
diff changeset
   279
    private static native void initIDs();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private static native int dup(int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private static native void dup2(int fd, int fd2) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    private static native int open0(String path, int oflag) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private static native void close0(int fd) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    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
   285
    private static native int addressFamily(int fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private static native InetAddress peerAddress0(int fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private static native int peerPort0(int fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
58295
7973073dd048 8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents: 47216
diff changeset
   289
    // 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
   290
    private static native boolean isConnected(int fd);
7973073dd048 8231187: SelectorProvider.inheritedChannel() returns TCP socket channel for Unix domain socket
michaelm
parents: 47216
diff changeset
   291
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    static {
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 14342
diff changeset
   293
        IOUtil.load();
26207
a02f6165d5be 8055955: (ch) Remove unnecessary initialization of InetAddress from FileChannel
chegar
parents: 25859
diff changeset
   294
        initIDs();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
}