0N/A/*
3945N/A * Copyright (c) 2003, 2006, 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 *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A */
0N/A
0N/A/* @test
1879N/A * @bug 4531526
1879N/A * @summary Test that more than one debuggee cannot bind to same port
1879N/A * at the same time.
1879N/A *
1890N/A * @build VMConnection ExclusiveBind HelloWorld
1879N/A * @run main ExclusiveBind
1879N/A */
1879N/Aimport java.io.InputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.File;
0N/Aimport java.net.ServerSocket;
0N/Aimport com.sun.jdi.Bootstrap;
0N/Aimport com.sun.jdi.VirtualMachine;
3863N/Aimport com.sun.jdi.connect.Connector;
0N/Aimport com.sun.jdi.connect.AttachingConnector;
0N/Aimport java.util.Map;
0N/Aimport java.util.List;
0N/Aimport java.util.Iterator;
0N/A
0N/Apublic class ExclusiveBind {
0N/A
0N/A /*
0N/A * 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;
113N/A
113N/A IOHandler(InputStream in) {
0N/A this.in = in;
4006N/A }
4006N/A
4006N/A static void handle(InputStream in) {
4006N/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 for (int i=0; i<n; i++) {
0N/A System.out.print((char)b[i]);
0N/A }
0N/A }
0N/A } catch (IOException ioe) { }
0N/A }
0N/A
0N/A }
634N/A
634N/A /*
0N/A * Find a connector by name
0N/A */
0N/A private static Connector findConnector(String name) {
0N/A List connectors = Bootstrap.virtualMachineManager().allConnectors();
0N/A Iterator iter = connectors.iterator();
0N/A while (iter.hasNext()) {
0N/A Connector connector = (Connector)iter.next();
0N/A if (connector.name().equals(name)) {
0N/A return connector;
634N/A }
634N/A }
634N/A return null;
0N/A }
0N/A
0N/A /*
0N/A * Launch (in server mode) a debuggee with the given address and
0N/A * suspend mode.
0N/A */
0N/A private static Process launch(String address, boolean suspend, String class_name) throws IOException {
0N/A String exe = System.getProperty("java.home") + File.separator + "bin" +
0N/A File.separator;
0N/A String arch = System.getProperty("os.arch");
0N/A String osname = System.getProperty("os.name");
0N/A if (osname.equals("SunOS") && arch.equals("sparcv9")) {
0N/A exe += "sparcv9/java";
0N/A } else if (osname.equals("SunOS") && arch.equals("amd64")) {
0N/A exe += "amd64/java";
0N/A } else {
0N/A exe += "java";
0N/A }
0N/A String cmd = exe + " " + VMConnection.getDebuggeeVMOptions() +
0N/A " -agentlib:jdwp=transport=dt_socket,server=y,suspend=";
0N/A if (suspend) {
0N/A cmd += "y";
0N/A } else {
0N/A cmd += "n";
0N/A }
0N/A cmd += ",address=" + address + " " + class_name;
0N/A
0N/A System.out.println("Starting: " + cmd);
0N/A
0N/A Process p = Runtime.getRuntime().exec(cmd);
0N/A IOHandler.handle(p.getInputStream());
0N/A IOHandler.handle(p.getErrorStream());
0N/A
0N/A return p;
0N/A }
4006N/A
3945N/A /*
0N/A * - pick a TCP port
0N/A * - Launch a debuggee in server=y,suspend=y,address=${port}
0N/A * - Launch a second debuggee in server=y,suspend=n with the same port
0N/A * - Second debuggee should fail with an error (address already in use)
4006N/A * - For clean-up we attach to the first debuggee and resume it.
4006N/A */
4006N/A public static void main(String args[]) throws Exception {
4006N/A // find a free port
4006N/A ServerSocket ss = new ServerSocket(0);
4006N/A int port = ss.getLocalPort();
4006N/A ss.close();
4006N/A
4006N/A String address = String.valueOf(port);
4006N/A
0N/A // launch the first debuggee
0N/A Process process1 = launch(address, true, "HelloWorld");
3945N/A
0N/A // give first debuggee time to suspend
0N/A Thread.currentThread().sleep(5000);
0N/A
0N/A // launch a second debuggee with the same address
0N/A Process process2 = launch(address, false, "HelloWorld");
0N/A
0N/A // get exit status from second debuggee
0N/A int exitCode = process2.waitFor();
0N/A
0N/A // clean-up - attach to first debuggee and resume it
0N/A AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
0N/A Map conn_args = conn.defaultArguments();
0N/A Connector.IntegerArgument port_arg =
0N/A (Connector.IntegerArgument)conn_args.get("port");
0N/A port_arg.setValue(port);
0N/A VirtualMachine vm = conn.attach(conn_args);
0N/A vm.resume();
0N/A
0N/A // if the second debuggee ran to completion then we've got a problem
0N/A if (exitCode == 0) {
0N/A throw new RuntimeException("Test failed - second debuggee didn't fail to bind");
0N/A } else {
0N/A System.out.println("Test passed - second debuggee correctly failed to bind");
0N/A }
0N/A }
0N/A}
0N/A