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 * @author Jaroslav Bachorik 44 * 45 * @library /lib/testlibrary 46 * 47 * @build jdk.testlibrary.* 48 * @build TestManager TestApplication CustomLauncherTest 49 * @run main/othervm CustomLauncherTest 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 static { 60 // magic with os.arch 61 String osarch = System.getProperty("os.arch"); 62 switch (osarch) { 63 case "i386": 64 case "i486": 65 case "i586": 66 case "i686": 67 case "i786": 68 case "i886": 69 case "i986": { 70 ARCH = "i586"; 71 break; 72 } 73 case "x86_64": 74 case "amd64": { 75 ARCH = "amd64"; 76 break; 77 } 78 case "sparc": 79 ARCH = "sparcv9"; 80 break; 81 default: { 82 ARCH = osarch; 83 } 84 } 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 if (launcher == null) return; // launcher not available for the tested platform; skip 112 113 System.out.println("Starting custom launcher:"); 114 System.out.println("========================="); 115 System.out.println(" launcher : " + launcher[0]); 116 System.out.println(" libjvm : " + libjvmPath.toString()); 117 System.out.println(" classpath : " + TEST_CLASSPATH); 118 ProcessBuilder server = new ProcessBuilder( 119 launcher[1], 120 libjvmPath.toString(), 121 TEST_CLASSPATH, 122 "TestApplication" 123 ); 124 125 final AtomicReference<String> port = new AtomicReference<>(); 126 127 serverPrc = ProcessTools.startProcess( 128 "Launcher", 129 server, 130 (String line) -> { 131 if (line.startsWith("port:")) { 132 port.set(line.split("\\:")[1]); 133 } else if (line.startsWith("waiting")) { 134 return true; 135 } 136 return false; 137 }, 138 5, 139 TimeUnit.SECONDS 140 ); 141 142 System.out.println("Attaching test manager:"); 143 System.out.println("========================="); 144 System.out.println(" PID : " + serverPrc.getPid()); 145 System.out.println(" shutdown port : " + port.get()); 146 147 ProcessBuilder client = ProcessTools.createJavaProcessBuilder( 148 "-cp", 149 TEST_CLASSPATH, 150 "--add-exports", "jdk.management.agent/jdk.internal.agent=ALL-UNNAMED", 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, "lib")); 185 return libjvmPath; 186 } 187 188 private static Path findLibjvm(Path libPath) { 189 // libjvm.so -> server/libjvm.so -> client/libjvm.so 190 Path libjvmPath = libPath.resolve("libjvm.so"); 191 if (isFileOk(libjvmPath)) { 192 return libjvmPath; 193 } 194 libjvmPath = libPath.resolve("server/libjvm.so"); 195 if (isFileOk(libjvmPath)) { 196 return libjvmPath; 197 } 198 libjvmPath = libPath.resolve("client/libjvm.so"); 199 if (isFileOk(libPath)) { 200 return libjvmPath; 201 } 202 203 return null; 204 } 205 206 private static boolean isFileOk(Path path) { 207 return Files.isRegularFile(path) && Files.isReadable(path); 208 } 209 210 private static String getPlatform() { 211 String platform = null; 212 switch (OSNAME.toLowerCase()) { 213 case "linux": { 214 platform = "linux"; 215 break; 216 } 217 case "sunos": { 218 platform = "solaris"; 219 break; 220 } 221 default: { 222 platform = null; 223 } 224 } 225 226 return platform; 227 } 228 229 private static String[] getLauncher() throws IOException { 230 String platform = getPlatform(); 231 if (platform == null) { 232 return null; 233 } 234 235 String launcher = TEST_SRC + File.separator + platform + "-" + ARCH + 236 File.separator + "launcher"; 237 238 final FileSystem FS = FileSystems.getDefault(); 239 Path launcherPath = FS.getPath(launcher); 240 241 final boolean hasLauncher = Files.isRegularFile(launcherPath, LinkOption.NOFOLLOW_LINKS)&& 242 Files.isReadable(launcherPath); 243 if (!hasLauncher) { 244 System.out.println("Launcher [" + launcher + "] does not exist. Skipping the test."); 245 return null; 246 } 247 248 // It is impossible to store an executable file in the source control 249 // We need to copy the launcher to the working directory 250 // and set the executable flag 251 Path localLauncherPath = FS.getPath(WORK_DIR, "launcher"); 252 Files.copy(launcherPath, localLauncherPath, 253 StandardCopyOption.REPLACE_EXISTING); 254 if (!Files.isExecutable(localLauncherPath)) { 255 Set<PosixFilePermission> perms = new HashSet<>( 256 Files.getPosixFilePermissions( 257 localLauncherPath, 258 LinkOption.NOFOLLOW_LINKS 259 ) 260 ); 261 perms.add(PosixFilePermission.OWNER_EXECUTE); 262 Files.setPosixFilePermissions(localLauncherPath, perms); 263 } 264 return new String[] {launcher, localLauncherPath.toAbsolutePath().toString()}; 265 } 266 }