Path: blob/master/test/hotspot/jtreg/serviceability/attach/AttachWithStalePidFile.java
41149 views
/*1* Copyright (c) 2013, 2020, 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* @modules java.base/jdk.internal.misc:open28* @modules java.base/java.lang:open29* @modules jdk.attach/sun.tools.attach30* @library /test/lib31* @requires os.family != "windows"32* @run main AttachWithStalePidFile33*/3435import jdk.test.lib.Platform;36import jdk.test.lib.process.ProcessTools;37import com.sun.tools.attach.VirtualMachine;38import sun.tools.attach.HotSpotVirtualMachine;39import java.lang.reflect.Field;40import java.nio.file.*;41import java.nio.file.attribute.*;42import java.io.*;4344public class AttachWithStalePidFile {45public static void main(String... args) throws Exception {4647// Since there might be stale pid-files owned by different48// users on the system we may need to retry the test in case we49// are unable to remove the existing file.50int retries = 5;51while(!runTest() && --retries > 0);5253if(retries == 0) {54throw new RuntimeException("Test failed after 5 retries. " +55"Remove any /tmp/.java_pid* files and retry.");56}57}5859public static boolean runTest() throws Exception {60ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(61"-XX:+UnlockDiagnosticVMOptions", "-XX:+PauseAtStartup", "AttachWithStalePidFileTarget");62Process target = pb.start();63Path pidFile = null;6465try {66int pid = getUnixProcessId(target);6768// create the stale .java_pid file. use hard-coded /tmp path as in th VM69pidFile = createJavaPidFile(pid);70if(pidFile == null) {71return false;72}7374// wait for vm.paused file to be created and delete it once we find it.75waitForAndResumeVM(pid);7677waitForTargetReady(target);7879HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());80BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(vm.remoteDataDump()));81String line = null;82while((line = remoteDataReader.readLine()) != null);8384vm.detach();85return true;86}87finally {88target.destroy();89target.waitFor();9091if(pidFile != null && Files.exists(pidFile)) {92Files.delete(pidFile);93}94}95}9697private static void waitForTargetReady(Process target) throws IOException {98BufferedReader br = new BufferedReader(new InputStreamReader(target.getInputStream()));99String line = br.readLine();100// wait for the ready message having been printed or EOF (line == null)101while (line != null && !line.equals(AttachWithStalePidFileTarget.READY_MSG)) {102line = br.readLine();103}104// target VM ready105}106107private static Path createJavaPidFile(int pid) throws Exception {108Path pidFile = Paths.get("/tmp/.java_pid" + pid);109if(Files.exists(pidFile)) {110try {111Files.delete(pidFile);112}113catch(FileSystemException e) {114if(e.getReason().matches("Operation not permitted|Not owner")) {115System.out.println("Unable to remove exisiting stale PID file" + pidFile);116System.out.println("===================================================");117e.printStackTrace(System.out);118return null;119}120throw e;121}122}123return Files.createFile(pidFile,124PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------")));125}126127private static void waitForAndResumeVM(int pid) throws Exception {128Path pauseFile = Paths.get("vm.paused." + pid);129int retries = 60;130while(!Files.exists(pauseFile) && --retries > 0) {131Thread.sleep(1000);132}133if(retries == 0) {134throw new RuntimeException("Timeout waiting for VM to start. " +135"vm.paused file not created within 60 seconds.");136}137Files.delete(pauseFile);138}139140private static int getUnixProcessId(Process unixProcess) throws Exception {141Field pidField = unixProcess.getClass().getDeclaredField("pid");142pidField.setAccessible(true);143return (Integer)pidField.get(unixProcess);144}145}146147148