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