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 /* 25 * @test 26 * @bug 7162400 27 * @key regression 28 * @summary Regression test for attach issue where stale pid files in /tmp lead to connection issues 29 * @modules jdk.attach/sun.tools.attach 30 * @library /testlibrary 31 * @build jdk.test.lib.* AttachWithStalePidFileTarget 32 * @run main AttachWithStalePidFile 33 */ 34 35 import jdk.test.lib.*; 36 import com.sun.tools.attach.VirtualMachine; 37 import sun.tools.attach.HotSpotVirtualMachine; 38 import java.lang.reflect.Field; 39 import java.nio.file.*; 40 import java.nio.file.attribute.*; 41 import java.io.*; 42 43 public class AttachWithStalePidFile { 44 public static void main(String... args) throws Exception { 45 46 // this test is only valid on non-Windows platforms 47 if(Platform.isWindows()) { 48 System.out.println("This test is only valid on non-Windows platforms."); 49 return; 50 } 51 52 // Since there might be stale pid-files owned by different 53 // users on the system we may need to retry the test in case we 54 // are unable to remove the existing file. 55 int retries = 5; 56 while(!runTest() && --retries > 0); 57 58 if(retries == 0) { 59 throw new RuntimeException("Test failed after 5 retries. " + 60 "Remove any /tmp/.java_pid* files and retry."); 61 } 62 } 63 64 public static boolean runTest() throws Exception { 65 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 66 "-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget"); 67 Process target = pb.start(); 68 Path pidFile = null; 69 70 try { 71 int pid = getUnixProcessId(target); 72 73 // create the stale .java_pid file. use hard-coded /tmp path as in th VM 74 pidFile = createJavaPidFile(pid); 75 if(pidFile == null) { 76 return false; 77 } 78 79 // wait for vm.paused file to be created and delete it once we find it. 80 waitForAndResumeVM(pid); 81 82 waitForTargetReady(target); 83 84 HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString()); 85 BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump())); 86 String line = null; 87 while((line = remoteDataReader.readLine()) != null); 88 89 vm.detach(); 90 return true; 91 } 92 finally { 93 target.destroy(); 94 target.waitFor(); 95 96 if(pidFile != null && Files.exists(pidFile)) { 97 Files.delete(pidFile); 98 } 99 } 100 } 101 102 private static void waitForTargetReady(Process target) throws IOException { 103 BufferedReader br = new BufferedReader(new InputStreamReader(target.getInputStream())); 104 String line = br.readLine(); 105 // wait for the ready message having been printed or EOF (line == null) 106 while (line != null && !line.equals(AttachWithStalePidFileTarget.READY_MSG)) { 107 line = br.readLine(); 108 } 109 // target VM ready 110 } 111 112 private static Path createJavaPidFile(int pid) throws Exception { 113 Path pidFile = Paths.get("/tmp/.java_pid" + pid); 114 if(Files.exists(pidFile)) { 115 try { 116 Files.delete(pidFile); 117 } 118 catch(FileSystemException e) { 119 if(e.getReason().matches("Operation not permitted|Not owner")) { 120 System.out.println("Unable to remove exisiting stale PID file" + pidFile); 121 System.out.println("==================================================="); 122 e.printStackTrace(System.out); 123 return null; 124 } 125 throw e; 126 } 127 } 128 return Files.createFile(pidFile, 129 PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------"))); 130 } 131 132 private static void waitForAndResumeVM(int pid) throws Exception { 133 Path pauseFile = Paths.get("vm.paused." + pid); 134 int retries = 60; 135 while(!Files.exists(pauseFile) && --retries > 0) { 136 Thread.sleep(1000); 137 } 138 if(retries == 0) { 139 throw new RuntimeException("Timeout waiting for VM to start. " + 140 "vm.paused file not created within 60 seconds."); 141 } 142 Files.delete(pauseFile); 143 } 144 145 private static int getUnixProcessId(Process unixProcess) throws Exception { 146 Field pidField = unixProcess.getClass().getDeclaredField("pid"); 147 pidField.setAccessible(true); 148 return (Integer)pidField.get(unixProcess); 149 } 150 }