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