0N/A/*
3261N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @summary Sockets shouldn't be inherited when creating a child process
0N/A */
0N/Aimport java.nio.ByteBuffer;
0N/Aimport java.nio.channels.*;
0N/Aimport java.net.*;
0N/Aimport java.io.*;
0N/A
0N/Apublic class SocketInheritance {
0N/A
0N/A /*
0N/A * Simple helper class to direct process output to the parent
0N/A * System.out
0N/A */
0N/A static class IOHandler implements Runnable {
0N/A InputStream in;
0N/A
0N/A IOHandler(InputStream in) {
0N/A this.in = in;
0N/A }
0N/A
0N/A static void handle(InputStream in) {
0N/A IOHandler handler = new IOHandler(in);
0N/A Thread thr = new Thread(handler);
0N/A thr.setDaemon(true);
0N/A thr.start();
0N/A }
0N/A
0N/A public void run() {
0N/A try {
0N/A byte b[] = new byte[100];
0N/A for (;;) {
0N/A int n = in.read(b);
0N/A if (n < 0) return;
0N/A System.out.write(b, 0, n);
0N/A }
0N/A } catch (IOException ioe) { }
0N/A }
0N/A
0N/A }
0N/A
0N/A // connect to the given port
0N/A static SocketChannel connect(int port) throws IOException {
0N/A InetAddress lh = InetAddress.getByName("127.0.0.1");
0N/A InetSocketAddress isa = new InetSocketAddress(lh, port);
0N/A return SocketChannel.open(isa);
0N/A }
0N/A
0N/A // simple child process that handshakes with the parent and then
0N/A // waits indefinitely until it is destroyed
0N/A static void child(int port) {
0N/A try {
0N/A connect(port).close();
0N/A } catch (IOException x) {
0N/A x.printStackTrace();
0N/A return;
0N/A }
0N/A
0N/A for (;;) {
0N/A try {
0N/A Thread.sleep(10*1000);
0N/A } catch (InterruptedException x) { }
0N/A }
0N/A }
0N/A
0N/A
0N/A // Creates a loopback connection.
0N/A // Forks process which should not inherit the sockets.
0N/A // Close the sockets, and attempt to re-bind the listener.
0N/A
0N/A static void start() throws Exception {
0N/A
0N/A // setup loopback connection
0N/A ServerSocketChannel ssc = ServerSocketChannel.open();
0N/A ssc.socket().bind( new InetSocketAddress(0) );
0N/A
0N/A int port = ssc.socket().getLocalPort();
0N/A
0N/A SocketChannel sc1 = connect(port);
0N/A SocketChannel sc2 = ssc.accept();
0N/A
0N/A // launch the child
0N/A String cmd = System.getProperty("java.home") + File.separator + "bin" +
2546N/A File.separator + "java";
2546N/A String testClasses = System.getProperty("test.classes");
2546N/A if (testClasses != null)
2546N/A cmd += " -cp " + testClasses;
2546N/A cmd += " SocketInheritance -child " + port;
0N/A
0N/A Process p = Runtime.getRuntime().exec(cmd);
0N/A
0N/A IOHandler.handle(p.getInputStream());
0N/A IOHandler.handle(p.getErrorStream());
0N/A
0N/A // wait for child to connect
0N/A SocketChannel sc3 = ssc.accept();
0N/A
0N/A // close sockets
0N/A sc1.close();
0N/A sc2.close();
0N/A sc3.close();
0N/A ssc.close();
0N/A
0N/A // re-bind the listener - if the sockets were inherited then
0N/A // this will fail
0N/A try {
0N/A ssc = ServerSocketChannel.open();
0N/A ssc.socket().bind(new InetSocketAddress(port));
0N/A ssc.close();
0N/A } finally {
0N/A p.destroy();
0N/A }
0N/A
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A if (!System.getProperty("os.name").startsWith("Windows"))
0N/A return;
0N/A
0N/A if (args.length == 0) {
0N/A start();
0N/A } else {
0N/A if (args[0].equals("-child")) {
0N/A child(Integer.parseInt(args[1]));
0N/A }
0N/A }
0N/A }
0N/A}