hotspot/test/serviceability/attach/AttachWithStalePidFile.java
changeset 18931 5de3638104b4
child 24835 be963975e2f7
equal deleted inserted replaced
18928:ac0163dd0a50 18931:5de3638104b4
       
     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 /*
       
    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  * @compile AttachWithStalePidFileTarget.java
       
    31  * @run main AttachWithStalePidFile
       
    32  */
       
    33 
       
    34 import com.oracle.java.testlibrary.*;
       
    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       // unfortunately there's no reliable way to know the VM is ready to receive the
       
    82       // attach request so we have to do an arbitrary sleep.
       
    83       Thread.sleep(5000);
       
    84 
       
    85       HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
       
    86       BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
       
    87       String line = null;
       
    88       while((line = remoteDataReader.readLine()) != null);
       
    89 
       
    90       vm.detach();
       
    91       return true;
       
    92     }
       
    93     finally {
       
    94       target.destroy();
       
    95       target.waitFor();
       
    96 
       
    97       if(pidFile != null && Files.exists(pidFile)) {
       
    98         Files.delete(pidFile);
       
    99       }
       
   100     }
       
   101   }
       
   102 
       
   103   private static Path createJavaPidFile(int pid) throws Exception {
       
   104     Path pidFile = Paths.get("/tmp/.java_pid" + pid);
       
   105     if(Files.exists(pidFile)) {
       
   106       try {
       
   107         Files.delete(pidFile);
       
   108       }
       
   109       catch(FileSystemException e) {
       
   110         if(e.getReason().equals("Operation not permitted")) {
       
   111           System.out.println("Unable to remove exisiting stale PID file" + pidFile);
       
   112           return null;
       
   113         }
       
   114         throw e;
       
   115       }
       
   116     }
       
   117     return Files.createFile(pidFile,
       
   118       PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------")));
       
   119   }
       
   120 
       
   121   private static void waitForAndResumeVM(int pid) throws Exception {
       
   122     Path pauseFile = Paths.get("vm.paused." + pid);
       
   123     int retries = 60;
       
   124     while(!Files.exists(pauseFile) && --retries > 0) {
       
   125       Thread.sleep(1000);
       
   126     }
       
   127     if(retries == 0) {
       
   128       throw new RuntimeException("Timeout waiting for VM to start. " +
       
   129         "vm.paused file not created within 60 seconds.");
       
   130     }
       
   131     Files.delete(pauseFile);
       
   132   }
       
   133 
       
   134   private static int getUnixProcessId(Process unixProcess) throws Exception {
       
   135     Field pidField = unixProcess.getClass().getDeclaredField("pid");
       
   136     pidField.setAccessible(true);
       
   137     return (Integer)pidField.get(unixProcess);
       
   138   }
       
   139 }