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