Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/serviceability/attach/AttachWithStalePidFile.java
32284 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 716240026* @key regression27* @summary Regression test for attach issue where stale pid files in /tmp lead to connection issues28* @library /testlibrary29* @build com.oracle.java.testlibrary.* AttachWithStalePidFileTarget30* @run main AttachWithStalePidFile31*/3233import com.oracle.java.testlibrary.*;34import com.sun.tools.attach.VirtualMachine;35import sun.tools.attach.HotSpotVirtualMachine;36import java.lang.reflect.Field;37import java.nio.file.*;38import java.nio.file.attribute.*;39import java.io.*;4041public class AttachWithStalePidFile {42public static void main(String... args) throws Exception {4344// this test is only valid on non-Windows platforms45if(Platform.isWindows()) {46System.out.println("This test is only valid on non-Windows platforms.");47return;48}4950// Since there might be stale pid-files owned by different51// users on the system we may need to retry the test in case we52// are unable to remove the existing file.53int retries = 5;54while(!runTest() && --retries > 0);5556if(retries == 0) {57throw new RuntimeException("Test failed after 5 retries. " +58"Remove any /tmp/.java_pid* files and retry.");59}60}6162public static boolean runTest() throws Exception {63ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(64"-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");65Process target = pb.start();66Path pidFile = null;6768try {69int pid = getUnixProcessId(target);7071// create the stale .java_pid file. use hard-coded /tmp path as in th VM72pidFile = createJavaPidFile(pid);73if(pidFile == null) {74return false;75}7677// wait for vm.paused file to be created and delete it once we find it.78waitForAndResumeVM(pid);7980// unfortunately there's no reliable way to know the VM is ready to receive the81// attach request so we have to do an arbitrary sleep.82Thread.sleep(5000);8384HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());85BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));86String line = null;87while((line = remoteDataReader.readLine()) != null);8889vm.detach();90return true;91}92finally {93target.destroy();94target.waitFor();9596if(pidFile != null && Files.exists(pidFile)) {97Files.delete(pidFile);98}99}100}101102private static Path createJavaPidFile(int pid) throws Exception {103Path pidFile = Paths.get("/tmp/.java_pid" + pid);104if(Files.exists(pidFile)) {105try {106Files.delete(pidFile);107}108catch(FileSystemException e) {109if(e.getReason().equals("Operation not permitted")) {110System.out.println("Unable to remove exisiting stale PID file" + pidFile);111return null;112}113throw e;114}115}116return Files.createFile(pidFile,117PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------")));118}119120private static void waitForAndResumeVM(int pid) throws Exception {121Path pauseFile = Paths.get("vm.paused." + pid);122int retries = 60;123while(!Files.exists(pauseFile) && --retries > 0) {124Thread.sleep(1000);125}126if(retries == 0) {127throw new RuntimeException("Timeout waiting for VM to start. " +128"vm.paused file not created within 60 seconds.");129}130Files.delete(pauseFile);131}132133private static int getUnixProcessId(Process unixProcess) throws Exception {134Field pidField = unixProcess.getClass().getDeclaredField("pid");135pidField.setAccessible(true);136return (Integer)pidField.get(unixProcess);137}138}139140141