Path: blob/master/test/hotspot/jtreg/serviceability/attach/AttachWithStalePidFile.java
64476 views
/*1* Copyright (c) 2013, 2021, 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* @summary Regression test for attach issue where stale pid files in /tmp lead to connection issues27* @requires os.family != "windows"28* @requires vm.flagless29* @modules java.base/jdk.internal.misc:open30* @modules java.base/java.lang:open31* @modules jdk.attach/sun.tools.attach32* @library /test/lib33* @run driver AttachWithStalePidFile34*/3536import jdk.test.lib.Platform;37import jdk.test.lib.process.ProcessTools;38import com.sun.tools.attach.VirtualMachine;39import sun.tools.attach.HotSpotVirtualMachine;40import java.lang.reflect.Field;41import java.nio.file.*;42import java.nio.file.attribute.*;43import java.io.*;4445public class AttachWithStalePidFile {46public static void main(String... args) throws Exception {4748// Since there might be stale pid-files owned by different49// users on the system we may need to retry the test in case we50// are unable to remove the existing file.51int retries = 5;52while(!runTest() && --retries > 0);5354if(retries == 0) {55throw new RuntimeException("Test failed after 5 retries. " +56"Remove any /tmp/.java_pid* files and retry.");57}58}5960public static boolean runTest() throws Exception {61ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(62"-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");63Process target = pb.start();64Path pidFile = null;6566try {67int pid = getUnixProcessId(target);6869// create the stale .java_pid file. use hard-coded /tmp path as in th VM70pidFile = createJavaPidFile(pid);71if(pidFile == null) {72return false;73}7475// wait for vm.paused file to be created and delete it once we find it.76waitForAndResumeVM(pid);7778waitForTargetReady(target);7980HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());81BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));82String line = null;83while((line = remoteDataReader.readLine()) != null);8485vm.detach();86return true;87}88finally {89target.destroy();90target.waitFor();9192if(pidFile != null && Files.exists(pidFile)) {93Files.delete(pidFile);94}95}96}9798private static void waitForTargetReady(Process target) throws IOException {99BufferedReader br = new BufferedReader(new InputStreamReader(target.getInputStream()));100String line = br.readLine();101// wait for the ready message having been printed or EOF (line == null)102while (line != null && !line.equals(AttachWithStalePidFileTarget.READY_MSG)) {103line = br.readLine();104}105// target VM ready106}107108private static Path createJavaPidFile(int pid) throws Exception {109Path pidFile = Paths.get("/tmp/.java_pid" + pid);110if(Files.exists(pidFile)) {111try {112Files.delete(pidFile);113}114catch(FileSystemException e) {115if(e.getReason().matches("Operation not permitted|Not owner")) {116System.out.println("Unable to remove exisiting stale PID file" + pidFile);117System.out.println("===================================================");118e.printStackTrace(System.out);119return null;120}121throw e;122}123}124return Files.createFile(pidFile,125PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------")));126}127128private static void waitForAndResumeVM(int pid) throws Exception {129Path pauseFile = Paths.get("vm.paused." + pid);130int retries = 60;131while(!Files.exists(pauseFile) && --retries > 0) {132Thread.sleep(1000);133}134if(retries == 0) {135throw new RuntimeException("Timeout waiting for VM to start. " +136"vm.paused file not created within 60 seconds.");137}138Files.delete(pauseFile);139}140141private static int getUnixProcessId(Process unixProcess) throws Exception {142Field pidField = unixProcess.getClass().getDeclaredField("pid");143pidField.setAccessible(true);144return (Integer)pidField.get(unixProcess);145}146}147148149