Path: blob/master/test/jdk/java/nio/channels/SocketChannel/SocketInheritance.java
41154 views
/*1* Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Sockets shouldn't be inherited when creating a child process26*/27import java.nio.ByteBuffer;28import java.nio.channels.*;29import java.net.*;30import java.io.*;3132public class SocketInheritance {3334/*35* Simple helper class to direct process output to the parent36* System.out37*/38static class IOHandler implements Runnable {39InputStream in;4041IOHandler(InputStream in) {42this.in = in;43}4445static void handle(InputStream in) {46IOHandler handler = new IOHandler(in);47Thread thr = new Thread(handler);48thr.setDaemon(true);49thr.start();50}5152public void run() {53try {54byte b[] = new byte[100];55for (;;) {56int n = in.read(b);57if (n < 0) return;58System.out.write(b, 0, n);59}60} catch (IOException ioe) { }61}6263}6465// connect to the given port66static SocketChannel connect(int port) throws IOException {67InetAddress lh = InetAddress.getLoopbackAddress();68InetSocketAddress isa = new InetSocketAddress(lh, port);69return SocketChannel.open(isa);70}7172// simple child process that handshakes with the parent and then73// waits indefinitely until it is destroyed74static void child(int port) {75try {76connect(port).close();77} catch (IOException x) {78x.printStackTrace();79return;80}8182for (;;) {83try {84Thread.sleep(10*1000);85} catch (InterruptedException x) { }86}87}888990// Creates a loopback connection.91// Forks process which should not inherit the sockets.92// Close the sockets, and attempt to re-bind the listener.9394static void start() throws Exception {9596// setup loopback connection97ServerSocketChannel ssc = ServerSocketChannel.open();98ssc.socket().bind( new InetSocketAddress(0) );99100int port = ssc.socket().getLocalPort();101102SocketChannel sc1 = connect(port);103SocketChannel sc2 = ssc.accept();104105// launch the child106String cmd = System.getProperty("java.home") + File.separator + "bin" +107File.separator + "java";108String testClasses = System.getProperty("test.classes");109if (testClasses != null)110cmd += " -cp " + testClasses;111cmd += " SocketInheritance -child " + port;112113Process p = Runtime.getRuntime().exec(cmd);114115IOHandler.handle(p.getInputStream());116IOHandler.handle(p.getErrorStream());117118// wait for child to connect119SocketChannel sc3 = ssc.accept();120121// close sockets122sc1.close();123sc2.close();124sc3.close();125ssc.close();126127// re-bind the listener - if the sockets were inherited then128// this will fail129try {130ssc = ServerSocketChannel.open();131ssc.socket().bind(new InetSocketAddress(port));132ssc.close();133} finally {134p.destroy();135}136137}138139public static void main(String[] args) throws Exception {140if (!System.getProperty("os.name").startsWith("Windows"))141return;142143if (args.length == 0) {144start();145} else {146if (args[0].equals("-child")) {147child(Integer.parseInt(args[1]));148}149}150}151}152153154