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