test/hotspot/jtreg/serviceability/attach/AttachWithStalePidFile.java
author sspitsyn
Tue, 26 Jun 2018 13:50:59 -0700
changeset 50802 fa380b3b2b7d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8205721: Problem list serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitorStatArrayCorrectnessTest.java Summary: Add the test to the ProblemList.txt Reviewed-by: dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18931
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
     1
/*
36209
8db2a78cbc29 8151156: [TESTBUG] Integrate trivial Hotspot test changes from Jake before Jigsaw M3
ctornqvi
parents: 30604
diff changeset
     2
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
18931
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
     4
 *
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
     8
 *
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    13
 * accompanied this code).
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    14
 *
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    18
 *
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    21
 * questions.
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    22
 */
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    23
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    24
/*
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    25
 * @test
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    26
 * @bug 7162400
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    27
 * @key regression
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    28
 * @summary Regression test for attach issue where stale pid files in /tmp lead to connection issues
42307
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
    29
 * @modules java.base/jdk.internal.misc:open
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
    30
 * @modules java.base/java.lang:open
36209
8db2a78cbc29 8151156: [TESTBUG] Integrate trivial Hotspot test changes from Jake before Jigsaw M3
ctornqvi
parents: 30604
diff changeset
    31
 * @modules jdk.attach/sun.tools.attach
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38152
diff changeset
    32
 * @library /test/lib
18931
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    33
 * @run main AttachWithStalePidFile
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    34
 */
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    35
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38152
diff changeset
    36
import jdk.test.lib.Platform;
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38152
diff changeset
    37
import jdk.test.lib.process.ProcessTools;
18931
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    38
import com.sun.tools.attach.VirtualMachine;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    39
import sun.tools.attach.HotSpotVirtualMachine;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    40
import java.lang.reflect.Field;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    41
import java.nio.file.*;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    42
import java.nio.file.attribute.*;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    43
import java.io.*;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    44
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    45
public class AttachWithStalePidFile {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    46
  public static void main(String... args) throws Exception {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    47
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    48
    // this test is only valid on non-Windows platforms
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    49
    if(Platform.isWindows()) {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    50
      System.out.println("This test is only valid on non-Windows platforms.");
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    51
      return;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    52
    }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    53
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    54
    // Since there might be stale pid-files owned by different
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    55
    // users on the system we may need to retry the test in case we
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    56
    // are unable to remove the existing file.
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    57
    int retries = 5;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    58
    while(!runTest() && --retries > 0);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    59
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    60
    if(retries == 0) {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    61
      throw new RuntimeException("Test failed after 5 retries. " +
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    62
        "Remove any /tmp/.java_pid* files and retry.");
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    63
    }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    64
  }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    65
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    66
  public static boolean runTest() throws Exception {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    67
    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    68
      "-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    69
    Process target = pb.start();
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    70
    Path pidFile = null;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    71
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    72
    try {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    73
      int pid = getUnixProcessId(target);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    74
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    75
      // create the stale .java_pid file. use hard-coded /tmp path as in th VM
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    76
      pidFile = createJavaPidFile(pid);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    77
      if(pidFile == null) {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    78
        return false;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    79
      }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    80
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    81
      // wait for vm.paused file to be created and delete it once we find it.
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    82
      waitForAndResumeVM(pid);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    83
30111
fbba8c368ce0 8024055: serviceability/attach/AttachWithStalePidFile.java createJavaPidFile() fails
jbachorik
parents: 28819
diff changeset
    84
      waitForTargetReady(target);
18931
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    85
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    86
      HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    87
      BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    88
      String line = null;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    89
      while((line = remoteDataReader.readLine()) != null);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    90
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    91
      vm.detach();
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    92
      return true;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    93
    }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    94
    finally {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    95
      target.destroy();
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    96
      target.waitFor();
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    97
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    98
      if(pidFile != null && Files.exists(pidFile)) {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
    99
        Files.delete(pidFile);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   100
      }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   101
    }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   102
  }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   103
30111
fbba8c368ce0 8024055: serviceability/attach/AttachWithStalePidFile.java createJavaPidFile() fails
jbachorik
parents: 28819
diff changeset
   104
  private static void waitForTargetReady(Process target) throws IOException {
fbba8c368ce0 8024055: serviceability/attach/AttachWithStalePidFile.java createJavaPidFile() fails
jbachorik
parents: 28819
diff changeset
   105
    BufferedReader br = new BufferedReader(new InputStreamReader(target.getInputStream()));
fbba8c368ce0 8024055: serviceability/attach/AttachWithStalePidFile.java createJavaPidFile() fails
jbachorik
parents: 28819
diff changeset
   106
    String line = br.readLine();
fbba8c368ce0 8024055: serviceability/attach/AttachWithStalePidFile.java createJavaPidFile() fails
jbachorik
parents: 28819
diff changeset
   107
    // wait for the ready message having been printed or EOF (line == null)
fbba8c368ce0 8024055: serviceability/attach/AttachWithStalePidFile.java createJavaPidFile() fails
jbachorik
parents: 28819
diff changeset
   108
    while (line != null && !line.equals(AttachWithStalePidFileTarget.READY_MSG)) {
fbba8c368ce0 8024055: serviceability/attach/AttachWithStalePidFile.java createJavaPidFile() fails
jbachorik
parents: 28819
diff changeset
   109
        line = br.readLine();
fbba8c368ce0 8024055: serviceability/attach/AttachWithStalePidFile.java createJavaPidFile() fails
jbachorik
parents: 28819
diff changeset
   110
    }
fbba8c368ce0 8024055: serviceability/attach/AttachWithStalePidFile.java createJavaPidFile() fails
jbachorik
parents: 28819
diff changeset
   111
    // target VM ready
fbba8c368ce0 8024055: serviceability/attach/AttachWithStalePidFile.java createJavaPidFile() fails
jbachorik
parents: 28819
diff changeset
   112
  }
fbba8c368ce0 8024055: serviceability/attach/AttachWithStalePidFile.java createJavaPidFile() fails
jbachorik
parents: 28819
diff changeset
   113
18931
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   114
  private static Path createJavaPidFile(int pid) throws Exception {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   115
    Path pidFile = Paths.get("/tmp/.java_pid" + pid);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   116
    if(Files.exists(pidFile)) {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   117
      try {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   118
        Files.delete(pidFile);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   119
      }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   120
      catch(FileSystemException e) {
30111
fbba8c368ce0 8024055: serviceability/attach/AttachWithStalePidFile.java createJavaPidFile() fails
jbachorik
parents: 28819
diff changeset
   121
        if(e.getReason().matches("Operation not permitted|Not owner")) {
18931
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   122
          System.out.println("Unable to remove exisiting stale PID file" + pidFile);
30111
fbba8c368ce0 8024055: serviceability/attach/AttachWithStalePidFile.java createJavaPidFile() fails
jbachorik
parents: 28819
diff changeset
   123
          System.out.println("===================================================");
fbba8c368ce0 8024055: serviceability/attach/AttachWithStalePidFile.java createJavaPidFile() fails
jbachorik
parents: 28819
diff changeset
   124
          e.printStackTrace(System.out);
18931
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   125
          return null;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   126
        }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   127
        throw e;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   128
      }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   129
    }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   130
    return Files.createFile(pidFile,
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   131
      PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------")));
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   132
  }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   133
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   134
  private static void waitForAndResumeVM(int pid) throws Exception {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   135
    Path pauseFile = Paths.get("vm.paused." + pid);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   136
    int retries = 60;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   137
    while(!Files.exists(pauseFile) && --retries > 0) {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   138
      Thread.sleep(1000);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   139
    }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   140
    if(retries == 0) {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   141
      throw new RuntimeException("Timeout waiting for VM to start. " +
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   142
        "vm.paused file not created within 60 seconds.");
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   143
    }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   144
    Files.delete(pauseFile);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   145
  }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   146
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   147
  private static int getUnixProcessId(Process unixProcess) throws Exception {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   148
    Field pidField = unixProcess.getClass().getDeclaredField("pid");
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   149
    pidField.setAccessible(true);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   150
    return (Integer)pidField.get(unixProcess);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   151
  }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents:
diff changeset
   152
}