1 /*
   2  * Copyright (c) 2013, 2015, 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 java.io.File;
  25 import java.io.IOException;
  26 import java.nio.file.FileSystem;
  27 import java.nio.file.FileSystems;
  28 import java.nio.file.Files;
  29 import java.nio.file.LinkOption;
  30 import java.nio.file.Path;
  31 import java.nio.file.StandardCopyOption;
  32 import java.nio.file.attribute.PosixFilePermission;
  33 import java.util.HashSet;
  34 import java.util.Set;
  35 import java.util.concurrent.TimeUnit;
  36 import java.util.concurrent.atomic.AtomicReference;
  37 
  38 import jdk.testlibrary.ProcessTools;
  39 
  40 /**
  41  * @test
  42  * @bug 6434402 8004926
  43  * @library /lib/testlibrary
  44  * @modules java.management/sun.management
  45  *          java.management
  46  * @build jdk.testlibrary.*
  47  * @build TestManager TestApplication CustomLauncherTest
  48  * @run main/othervm CustomLauncherTest
  49  * @author Jaroslav Bachorik
  50  */
  51 public class CustomLauncherTest {
  52     private static final  String TEST_CLASSPATH = System.getProperty("test.class.path");
  53     private static final  String TEST_JDK = System.getProperty("test.jdk");
  54     private static final  String WORK_DIR = System.getProperty("user.dir");
  55 
  56     private static final  String TEST_SRC = System.getProperty("test.src");
  57     private static final  String OSNAME = System.getProperty("os.name");
  58     private static final  String ARCH;
  59     private static final  String LIBARCH;
  60 
  61     static {
  62         // magic with os.arch
  63         String osarch = System.getProperty("os.arch");
  64         switch (osarch) {
  65             case "i386":
  66             case "i486":
  67             case "i586":
  68             case "i686":
  69             case "i786":
  70             case "i886":
  71             case "i986": {
  72                 ARCH = "i586";
  73                 break;
  74             }
  75             case "x86_64":
  76             case "amd64": {
  77                 ARCH = "amd64";
  78                 break;
  79             }
  80             case "sparc":
  81                 ARCH = "sparcv9";
  82                 break;
  83             default: {
  84                 ARCH = osarch;
  85             }
  86         }
  87         LIBARCH = ARCH.equals("i586") ? "i386" : ARCH;
  88     }
  89 
  90     public static void main(String[] args) throws Exception {
  91         if (TEST_CLASSPATH == null || TEST_CLASSPATH.isEmpty()) {
  92             System.out.println("Test is designed to be run from jtreg only");
  93             return;
  94         }
  95 
  96         if (getPlatform() == null) {
  97             System.out.println("Test not designed to run on this operating " +
  98                                 "system (" + OSNAME + "), skipping...");
  99             return;
 100         }
 101 
 102         final FileSystem FS = FileSystems.getDefault();
 103 
 104         Path libjvmPath = findLibjvm(FS);
 105         if (libjvmPath == null) {
 106             throw new Error("Unable to locate 'libjvm.so' in " + TEST_JDK);
 107         }
 108 
 109         Process serverPrc = null, clientPrc = null;
 110 
 111         try {
 112             String[] launcher = getLauncher();
 113 
 114             System.out.println("Starting custom launcher:");
 115             System.out.println("=========================");
 116             System.out.println("  launcher  : " + launcher[0]);
 117             System.out.println("  libjvm    : " + libjvmPath.toString());
 118             System.out.println("  classpath : " + TEST_CLASSPATH);
 119             ProcessBuilder server = new ProcessBuilder(
 120                 launcher[1],
 121                 libjvmPath.toString(),
 122                 TEST_CLASSPATH,
 123                 "TestApplication"
 124             );
 125 
 126             final AtomicReference<String> port = new AtomicReference<>();
 127 
 128             serverPrc = ProcessTools.startProcess(
 129                 "Launcher",
 130                 server,
 131                 (String line) -> {
 132                     if (line.startsWith("port:")) {
 133                          port.set(line.split("\\:")[1]);
 134                     } else if (line.startsWith("waiting")) {
 135                          return true;
 136                     }
 137                     return false;
 138                 },
 139                 5,
 140                 TimeUnit.SECONDS
 141             );
 142 
 143             System.out.println("Attaching test manager:");
 144             System.out.println("=========================");
 145             System.out.println("  PID           : " + serverPrc.getPid());
 146             System.out.println("  shutdown port : " + port.get());
 147 
 148             ProcessBuilder client = ProcessTools.createJavaProcessBuilder(
 149                 "-cp",
 150                 TEST_CLASSPATH,
 151                 "TestManager",
 152                 String.valueOf(serverPrc.getPid()),
 153                 port.get(),
 154                 "true"
 155             );
 156 
 157             clientPrc = ProcessTools.startProcess(
 158                 "TestManager",
 159                 client,
 160                 (String line) -> line.startsWith("Starting TestManager for PID"),
 161                 10,
 162                 TimeUnit.SECONDS
 163             );
 164 
 165             int clientExitCode = clientPrc.waitFor();
 166             int serverExitCode = serverPrc.waitFor();
 167 
 168             if (clientExitCode != 0 || serverExitCode != 0) {
 169                 throw new Error("Test failed");
 170             }
 171         } finally {
 172             if (clientPrc != null) {
 173                 clientPrc.destroy();
 174                 clientPrc.waitFor();
 175             }
 176             if (serverPrc != null) {
 177                 serverPrc.destroy();
 178                 serverPrc.waitFor();
 179             }
 180         }
 181     }
 182 
 183     private static Path findLibjvm(FileSystem FS) {
 184         Path libjvmPath = findLibjvm(FS.getPath(TEST_JDK, "jre", "lib", LIBARCH));
 185         if (libjvmPath == null) {
 186             libjvmPath = findLibjvm(FS.getPath(TEST_JDK, "lib", LIBARCH));
 187         }
 188         return libjvmPath;
 189     }
 190 
 191     private static Path findLibjvm(Path libPath) {
 192         // ARCH/libjvm.so -> ARCH/server/libjvm.so -> ARCH/client/libjvm.so
 193         Path libjvmPath = libPath.resolve("libjvm.so");
 194         if (isFileOk(libjvmPath)) {
 195             return libjvmPath;
 196         }
 197         libjvmPath = libPath.resolve("server/libjvm.so");
 198         if (isFileOk(libjvmPath)) {
 199             return libjvmPath;
 200         }
 201         libjvmPath = libPath.resolve("client/libjvm.so");
 202         if (isFileOk(libPath)) {
 203             return libjvmPath;
 204         }
 205 
 206         return null;
 207     }
 208 
 209     private static boolean isFileOk(Path path) {
 210         return Files.isRegularFile(path) && Files.isReadable(path);
 211     }
 212 
 213     private static String getPlatform() {
 214         String platform = null;
 215         switch (OSNAME.toLowerCase()) {
 216             case "linux": {
 217                 platform = "linux";
 218                 break;
 219             }
 220             case "sunos": {
 221                 platform = "solaris";
 222                 break;
 223             }
 224             default: {
 225                 platform = null;
 226             }
 227         }
 228 
 229         return platform;
 230     }
 231 
 232     private static String[] getLauncher() throws IOException {
 233         String platform = getPlatform();
 234         if (platform == null) {
 235             return null;
 236         }
 237 
 238         String launcher = TEST_SRC + File.separator + platform + "-" + ARCH +
 239                           File.separator + "launcher";
 240 
 241         final FileSystem FS = FileSystems.getDefault();
 242         Path launcherPath = FS.getPath(launcher);
 243 
 244         final boolean hasLauncher = Files.isRegularFile(launcherPath, LinkOption.NOFOLLOW_LINKS)&&
 245                                     Files.isReadable(launcherPath);
 246         if (!hasLauncher) {
 247             System.out.println("Launcher [" + launcher + "] does not exist. Skipping the test.");
 248             return null;
 249         }
 250 
 251         // It is impossible to store an executable file in the source control
 252         // We need to copy the launcher to the working directory
 253         // and set the executable flag
 254         Path localLauncherPath = FS.getPath(WORK_DIR, "launcher");
 255         Files.copy(launcherPath, localLauncherPath,
 256                    StandardCopyOption.REPLACE_EXISTING,
 257                    StandardCopyOption.COPY_ATTRIBUTES);
 258         if (!Files.isExecutable(localLauncherPath)) {
 259             Set<PosixFilePermission> perms = new HashSet<>(
 260                 Files.getPosixFilePermissions(
 261                     localLauncherPath,
 262                     LinkOption.NOFOLLOW_LINKS
 263                 )
 264             );
 265             perms.add(PosixFilePermission.OWNER_EXECUTE);
 266             Files.setPosixFilePermissions(localLauncherPath, perms);
 267         }
 268         return new String[] {launcher, localLauncherPath.toAbsolutePath().toString()};
 269     }
 270 }