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 * @modules jdk.management.agent/jdk.internal.agent 47 * java.management 48 * jdk.attach 49 * 50 * @build jdk.testlibrary.* 51 * @build TestManager TestApplication CustomLauncherTest 52 * @run main/othervm CustomLauncherTest 53 */ 54 public class CustomLauncherTest { 55 private static final String TEST_CLASSPATH = System.getProperty("test.class.path"); 56 private static final String TEST_JDK = System.getProperty("test.jdk"); 57 private static final String WORK_DIR = System.getProperty("user.dir"); 58 59 private static final String TEST_SRC = System.getProperty("test.src"); 60 private static final String OSNAME = System.getProperty("os.name"); 61 private static final String ARCH; 62 static { 63 // magic with os.arch 64 String osarch = System.getProperty("os.arch"); 65 switch (osarch) { 66 case "i386": 67 case "i486": 68 case "i586": 69 case "i686": 70 case "i786": 71 case "i886": 72 case "i986": { 73 ARCH = "i586"; 74 break; 75 } 76 case "x86_64": 77 case "amd64": { 78 ARCH = "amd64"; 79 break; 80 } 81 case "sparc": 82 ARCH = "sparcv9"; 83 break; 84 default: { 85 ARCH = osarch; 86 } 87 } 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 "--add-exports", "jdk.management.agent/jdk.internal.agent=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, "lib")); 188 return libjvmPath; 189 } 190 191 private static Path findLibjvm(Path libPath) { 192 // libjvm.so -> server/libjvm.so -> client/libjvm.so 193 Path libjvmPath = libPath.resolve("libjvm.so"); 194 if (isFileOk(libjvmPath)) { 195 return libjvmPath; 196 } 197 libjvmPath = libPath.resolve("server/libjvm.so"); 198 if (isFileOk(libjvmPath)) { 199 return libjvmPath; 200 } 201 libjvmPath = libPath.resolve("client/libjvm.so"); 202 if (isFileOk(libPath)) { 203 return libjvmPath; 204 } 205 206 return null; 207 } 208 209 private static boolean isFileOk(Path path) { 210 return Files.isRegularFile(path) && Files.isReadable(path); 211 } 212 213 private static String getPlatform() { 214 String platform = null; 215 switch (OSNAME.toLowerCase()) { 216 case "linux": { 217 platform = "linux"; 218 break; 219 } 220 case "sunos": { 221 platform = "solaris"; 222 break; 223 } 224 default: { 225 platform = null; 226 } 227 } 228 229 return platform; 230 } 231 232 private static String[] getLauncher() throws IOException { 233 String platform = getPlatform(); 234 if (platform == null) { 235 return null; 236 } 237 238 String launcher = TEST_SRC + File.separator + platform + "-" + ARCH + 239 File.separator + "launcher"; 240 241 final FileSystem FS = FileSystems.getDefault(); 242 Path launcherPath = FS.getPath(launcher); 243 244 final boolean hasLauncher = Files.isRegularFile(launcherPath, LinkOption.NOFOLLOW_LINKS)&& 245 Files.isReadable(launcherPath); 246 if (!hasLauncher) { 247 System.out.println("Launcher [" + launcher + "] does not exist. Skipping the test."); 248 return null; 249 } 250 251 // It is impossible to store an executable file in the source control 252 // We need to copy the launcher to the working directory 253 // and set the executable flag 254 Path localLauncherPath = FS.getPath(WORK_DIR, "launcher"); 255 Files.copy(launcherPath, localLauncherPath, 256 StandardCopyOption.REPLACE_EXISTING); 257 if (!Files.isExecutable(localLauncherPath)) { 258 Set<PosixFilePermission> perms = new HashSet<>( 259 Files.getPosixFilePermissions( 260 localLauncherPath, 261 LinkOption.NOFOLLOW_LINKS 262 ) 263 ); 264 perms.add(PosixFilePermission.OWNER_EXECUTE); 265 Files.setPosixFilePermissions(localLauncherPath, perms); 266 } 267 return new String[] {launcher, localLauncherPath.toAbsolutePath().toString()}; 268 } 269 }