1 /*
2  * Copyright (c) 2019, 2020, 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 8225193
27  * @requires os.family != "windows"
28  * @library /test/lib
29  * @run main RemovingUnixDomainSocketTest
30  */
31 
32 import java.io.File;
33 import java.io.IOException;
34 import java.nio.file.Path;
35 import java.util.concurrent.TimeUnit;
36 
37 import jdk.test.lib.Utils;
38 import jdk.test.lib.apps.LingeredApp;
39 import jdk.test.lib.JDKToolLauncher;
40 import jdk.test.lib.process.OutputAnalyzer;
41 import jdk.test.lib.process.ProcessTools;
42 
43 public class RemovingUnixDomainSocketTest {
44 
45     // timeout (in seconds)
46     private static final long timeout = Utils.adjustTimeout(60);
47 
runJCmd(long pid)48     private static void runJCmd(long pid) throws InterruptedException, IOException {
49         JDKToolLauncher jcmd = JDKToolLauncher.createUsingTestJDK("jcmd");
50         jcmd.addVMArgs(Utils.getFilteredTestJavaOpts("-showversion"));
51         jcmd.addToolArg(Long.toString(pid));
52         jcmd.addToolArg("VM.version");
53 
54         Process jcmdProc = ProcessTools.startProcess("jcmd", new ProcessBuilder(jcmd.getCommand()));
55 
56         OutputAnalyzer out = new OutputAnalyzer(jcmdProc);
57 
58         if (!jcmdProc.waitFor(timeout, TimeUnit.SECONDS)) {
59             log("jcmd is still running after " + timeout + " seconds, terminating...");
60             jcmdProc.destroy();
61             jcmdProc.waitFor();
62         }
63 
64         log("jcmd stdout: [" + out.getStdout() + "];\n" +
65             "jcmd  stderr: [" + out.getStderr() + "]\n" +
66             "jcmd  exitValue = " + out.getExitValue());
67 
68         out.shouldHaveExitValue(0);
69         out.stderrShouldBeEmptyIgnoreVMWarnings();
70     }
71 
main(String... args)72     public static void main(String... args) throws Exception {
73         LingeredApp app = null;
74         try {
75             app = LingeredApp.startApp();
76 
77             // Access to Attach Listener
78             runJCmd(app.getPid());
79 
80             // Remove unix domain socket file
81             File sockFile = Path.of(System.getProperty("java.io.tmpdir"),
82                                    ".java_pid" + app.getPid())
83                                .toFile();
84             log("Remove " + sockFile.toString());
85             sockFile.delete();
86 
87             // Access to Attach Listener again
88             runJCmd(app.getPid());
89         } finally {
90             LingeredApp.stopApp(app);
91         }
92     }
93 
log(Object s)94     static void log(Object s) {
95         System.out.println(String.valueOf(s));
96     }
97 }
98