1 /*
   2  * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import jdk.test.lib.Utils;
  25 import jdk.test.lib.Platform;
  26 import jdk.test.lib.process.ProcessTools;
  27 
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.util.concurrent.TimeUnit;
  32 import java.util.concurrent.atomic.AtomicReference;
  33 
  34 /**
  35  * @test
  36  * @bug 6434402 8004926
  37  * @author Jaroslav Bachorik
  38  *
  39  * @library /test/lib
  40  * @modules java.management
  41  *          jdk.attach
  42  *          jdk.management.agent/jdk.internal.agent
  43  *
  44  * @requires os.family == "linux"
  45  * @build TestManager TestApplication CustomLauncherTest
  46  * @run main/othervm/native CustomLauncherTest
  47  */
  48 public class CustomLauncherTest {
  49 
  50     public static final String TEST_NATIVE_PATH = System.getProperty("test.nativepath");
  51 
  52     public static void main(String[] args) throws Exception {
  53         if (".".equals(Utils.TEST_CLASS_PATH)) {
  54             System.out.println("Test is designed to be run from jtreg only");
  55             return;
  56         }
  57 
  58         Path libjvm = Platform.jvmLibDir().resolve("libjvm.so");
  59         Process serverPrc = null, clientPrc = null;
  60 
  61         try {
  62             String launcher = getLauncher();
  63 
  64             System.out.println("Starting custom launcher:");
  65             System.out.println("=========================");
  66             System.out.println("  launcher  : " + launcher);
  67             System.out.println("  libjvm    : " + libjvm);
  68             System.out.println("  classpath : " + Utils.TEST_CLASS_PATH);
  69             ProcessBuilder server = new ProcessBuilder(
  70                 launcher,
  71                 libjvm.toString(),
  72                 Utils.TEST_CLASS_PATH,
  73                 "TestApplication"
  74             );
  75 
  76             final AtomicReference<String> port = new AtomicReference<>();
  77 
  78             serverPrc = ProcessTools.startProcess(
  79                 "Launcher",
  80                 server,
  81                 (String line) -> {
  82                     if (line.startsWith("port:")) {
  83                          port.set(line.split("\\:")[1]);
  84                     } else if (line.startsWith("waiting")) {
  85                          return true;
  86                     }
  87                     return false;
  88                 },
  89                 5,
  90                 TimeUnit.SECONDS
  91             );
  92 
  93             System.out.println("Attaching test manager:");
  94             System.out.println("=========================");
  95             System.out.println("  PID           : " + serverPrc.pid());
  96             System.out.println("  shutdown port : " + port.get());
  97 
  98             ProcessBuilder client = ProcessTools.createJavaProcessBuilder(
  99                 "-cp",
 100                 Utils.TEST_CLASS_PATH,
 101                 "--add-exports", "jdk.management.agent/jdk.internal.agent=ALL-UNNAMED",
 102                 "TestManager",
 103                 String.valueOf(serverPrc.pid()),
 104                 port.get(),
 105                 "true"
 106             );
 107 
 108             clientPrc = ProcessTools.startProcess(
 109                 "TestManager",
 110                 client,
 111                 (String line) -> line.startsWith("Starting TestManager for PID"),
 112                 10,
 113                 TimeUnit.SECONDS
 114             );
 115 
 116             int clientExitCode = clientPrc.waitFor();
 117             int serverExitCode = serverPrc.waitFor();
 118 
 119             if (clientExitCode != 0 || serverExitCode != 0) {
 120                 throw new Error("Test failed");
 121             }
 122         } finally {
 123             if (clientPrc != null) {
 124                 clientPrc.destroy();
 125                 clientPrc.waitFor();
 126             }
 127             if (serverPrc != null) {
 128                 serverPrc.destroy();
 129                 serverPrc.waitFor();
 130             }
 131         }
 132     }
 133 
 134     private static String getLauncher() {
 135         Path launcherPath = Paths.get(TEST_NATIVE_PATH, "launcher");
 136         return launcherPath.toAbsolutePath().toString();
 137     }
 138 }