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