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 if (launcher == null) return; // launcher not available for the tested platform; skip 115 116 System.out.println("Starting custom launcher:"); 117 System.out.println("========================="); 118 System.out.println(" launcher : " + launcher[0]); 119 System.out.println(" libjvm : " + libjvmPath.toString()); 120 System.out.println(" classpath : " + TEST_CLASSPATH); 121 ProcessBuilder server = new ProcessBuilder( 122 launcher[1], 123 libjvmPath.toString(), 124 TEST_CLASSPATH, 125 "TestApplication" 126 ); 127 128 final AtomicReference<String> port = new AtomicReference<>(); 129 130 serverPrc = ProcessTools.startProcess( 131 "Launcher", 132 server, 133 (String line) -> { 134 if (line.startsWith("port:")) { 135 port.set(line.split("\\:")[1]); 136 } else if (line.startsWith("waiting")) { 137 return true; 138 } 139 return false; 140 }, 141 5, 142 TimeUnit.SECONDS 143 ); 144 145 System.out.println("Attaching test manager:"); 146 System.out.println("========================="); 147 System.out.println(" PID : " + serverPrc.getPid()); 148 System.out.println(" shutdown port : " + port.get()); 149 150 ProcessBuilder client = ProcessTools.createJavaProcessBuilder( 151 "-cp", 152 TEST_CLASSPATH, 153 "-XaddExports:java.management/sun.management=ALL-UNNAMED", 154 "TestManager", 155 String.valueOf(serverPrc.getPid()), 156 port.get(), 157 "true" 158 ); 159 160 clientPrc = ProcessTools.startProcess( 161 "TestManager", 162 client, 163 (String line) -> line.startsWith("Starting TestManager for PID"), 164 10, 165 TimeUnit.SECONDS 166 ); 167 168 int clientExitCode = clientPrc.waitFor(); 169 int serverExitCode = serverPrc.waitFor(); 170 171 if (clientExitCode != 0 || serverExitCode != 0) { 172 throw new Error("Test failed"); 173 } 174 } finally { 175 if (clientPrc != null) { 176 clientPrc.destroy(); 177 clientPrc.waitFor(); 178 } 179 if (serverPrc != null) { 180 serverPrc.destroy(); 181 serverPrc.waitFor(); 182 } 183 } 184 } 185 186 private static Path findLibjvm(FileSystem FS) { 187 Path libjvmPath = findLibjvm(FS.getPath(TEST_JDK, "jre", "lib", LIBARCH)); 188 if (libjvmPath == null) { 189 libjvmPath = findLibjvm(FS.getPath(TEST_JDK, "lib", LIBARCH)); 190 } 191 return libjvmPath; 192 } 193 194 private static Path findLibjvm(Path libPath) { 195 // ARCH/libjvm.so -> ARCH/server/libjvm.so -> ARCH/client/libjvm.so 196 Path libjvmPath = libPath.resolve("libjvm.so"); 197 if (isFileOk(libjvmPath)) { 198 return libjvmPath; 199 } 200 libjvmPath = libPath.resolve("server/libjvm.so"); 201 if (isFileOk(libjvmPath)) { 202 return libjvmPath; 203 } 204 libjvmPath = libPath.resolve("client/libjvm.so"); 205 if (isFileOk(libPath)) { 206 return libjvmPath; 207 } 208 209 return null; 210 } 211 212 private static boolean isFileOk(Path path) { 213 return Files.isRegularFile(path) && Files.isReadable(path); 214 } 215 216 private static String getPlatform() { 217 String platform = null; 218 switch (OSNAME.toLowerCase()) { 219 case "linux": { 220 platform = "linux"; 221 break; 222 } 223 case "sunos": { 224 platform = "solaris"; 225 break; 226 } 227 default: { 228 platform = null; 229 } 230 } 231 232 return platform; 233 } 234 235 private static String[] getLauncher() throws IOException { 236 String platform = getPlatform(); 237 if (platform == null) { 238 return null; 239 } 240 241 String launcher = TEST_SRC + File.separator + platform + "-" + ARCH + 242 File.separator + "launcher"; 243 244 final FileSystem FS = FileSystems.getDefault(); 245 Path launcherPath = FS.getPath(launcher); 246 247 final boolean hasLauncher = Files.isRegularFile(launcherPath, LinkOption.NOFOLLOW_LINKS)&& 248 Files.isReadable(launcherPath); 249 if (!hasLauncher) { 250 System.out.println("Launcher [" + launcher + "] does not exist. Skipping the test."); 251 return null; 252 } 253 254 // It is impossible to store an executable file in the source control 255 // We need to copy the launcher to the working directory 256 // and set the executable flag 257 Path localLauncherPath = FS.getPath(WORK_DIR, "launcher"); 258 Files.copy(launcherPath, localLauncherPath, 259 StandardCopyOption.REPLACE_EXISTING); 260 if (!Files.isExecutable(localLauncherPath)) { 261 Set<PosixFilePermission> perms = new HashSet<>( 262 Files.getPosixFilePermissions( 263 localLauncherPath, 264 LinkOption.NOFOLLOW_LINKS 265 ) 266 ); 267 perms.add(PosixFilePermission.OWNER_EXECUTE); 268 Files.setPosixFilePermissions(localLauncherPath, perms); 269 } 270 return new String[] {launcher, localLauncherPath.toAbsolutePath().toString()}; 271 } 272 }