test/jdk/java/lang/ProcessBuilder/DestroyTest.java
author herrick
Mon, 14 Oct 2019 14:36:45 -0400
branchJDK-8200758-branch
changeset 58584 910b14f4fe3a
parent 53301 50355c3d35c0
permissions -rw-r--r--
8232042: [macos] Installation fails if application name contains spaces Submitted-by: almatvee Reviewed-by: aherrick, asemenyuk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
     1
/*
53301
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
     2
 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
     4
 *
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
     8
 *
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    13
 * accompanied this code).
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    14
 *
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    18
 *
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    21
 * questions.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    22
 */
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    23
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    24
/*
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    25
 * @test
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    26
 * @bug 4244896
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    27
 * @summary Test for the various platform specific implementations of
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    28
 *          destroyForcibly.
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    29
 */
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    30
53301
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    31
import java.io.BufferedReader;
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    32
import java.io.BufferedWriter;
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    33
import java.io.File;
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    34
import java.io.FileWriter;
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    35
import java.io.IOException;
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    36
import java.io.InputStreamReader;
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    37
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    38
abstract class ProcessTest implements Runnable {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    39
    ProcessBuilder bldr;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    40
    Process p;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    41
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    42
    public void run() {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    43
        try {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    44
            String line;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    45
            BufferedReader is =
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    46
                new BufferedReader(new InputStreamReader(p.getInputStream()));
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    47
            while ((line = is.readLine()) != null)
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    48
                System.err.println("ProcessTrap: " + line);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    49
        } catch(IOException e) {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    50
            if (!e.getMessage().matches("[Ss]tream [Cc]losed")) {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    51
                throw new RuntimeException(e);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    52
            }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    53
        }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    54
    }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    55
53301
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    56
    public void runTest() throws Exception {
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    57
        // The destroy() method is not tested because
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    58
        // the process streams are closed by the destroy() call.
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    59
        // After a destroy() call, the process terminates with a
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    60
        // SIGPIPE even if it was trapping SIGTERM.
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    61
        // So skip the trap test and go straight to destroyForcibly().
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    62
        p.destroyForcibly();
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    63
        p.waitFor();
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    64
        if (p.isAlive())
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    65
            throw new RuntimeException("Problem terminating the process.");
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    66
    }
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    67
}
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    68
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    69
class UnixTest extends ProcessTest {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    70
    public UnixTest(File script) throws IOException {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    71
        script.deleteOnExit();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    72
        createScript(script);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    73
        bldr = new ProcessBuilder(script.getCanonicalPath());
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    74
        bldr.redirectErrorStream(true);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    75
        bldr.directory(new File("."));
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    76
        p = bldr.start();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    77
    }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    78
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    79
    void createScript(File processTrapScript) throws IOException {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    80
        processTrapScript.deleteOnExit();
53301
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    81
        try (FileWriter fstream = new FileWriter(processTrapScript);
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    82
             BufferedWriter out = new BufferedWriter(fstream)) {
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    83
            out.write("#!/bin/bash\n" +
53301
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
    84
                "echo \\\"ProcessTrap.sh started\\\"\n" +
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    85
                "while :\n" +
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    86
                "do\n" +
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    87
                "    sleep 1;\n" +
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    88
                "done\n");
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    89
        }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    90
        processTrapScript.setExecutable(true, true);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    91
    }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    92
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    93
}
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    94
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    95
class WindowsTest extends ProcessTest {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    96
    public WindowsTest() throws IOException {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    97
        bldr = new ProcessBuilder("ftp");
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    98
        bldr.redirectErrorStream(true);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
    99
        bldr.directory(new File("."));
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   100
        p = bldr.start();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   101
    }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   102
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   103
}
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   104
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   105
public class DestroyTest {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   106
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   107
    public static ProcessTest getTest() throws Exception {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   108
        String osName = System.getProperty("os.name");
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   109
        if (osName.startsWith("Windows")) {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   110
            return new WindowsTest();
49544
d958597c7908 8200706: Better cleanup for open/test/jdk/java/lang/ProcessBuilder/DestroyTest.java
rriggs
parents: 47216
diff changeset
   111
        } else {
d958597c7908 8200706: Better cleanup for open/test/jdk/java/lang/ProcessBuilder/DestroyTest.java
rriggs
parents: 47216
diff changeset
   112
            File userDir = new File(System.getProperty("user.dir", "."));
d958597c7908 8200706: Better cleanup for open/test/jdk/java/lang/ProcessBuilder/DestroyTest.java
rriggs
parents: 47216
diff changeset
   113
            File tempFile = File.createTempFile("ProcessTrap-", ".sh", userDir);
53301
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
   114
            if (osName.startsWith("Linux")
50355c3d35c0 8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "Process terminated prematurely"
rriggs
parents: 49544
diff changeset
   115
                    || osName.startsWith("Mac OS")
49544
d958597c7908 8200706: Better cleanup for open/test/jdk/java/lang/ProcessBuilder/DestroyTest.java
rriggs
parents: 47216
diff changeset
   116
                    || osName.equals("SunOS")
d958597c7908 8200706: Better cleanup for open/test/jdk/java/lang/ProcessBuilder/DestroyTest.java
rriggs
parents: 47216
diff changeset
   117
                    || osName.equals("AIX")) {
d958597c7908 8200706: Better cleanup for open/test/jdk/java/lang/ProcessBuilder/DestroyTest.java
rriggs
parents: 47216
diff changeset
   118
                return new UnixTest(tempFile);
d958597c7908 8200706: Better cleanup for open/test/jdk/java/lang/ProcessBuilder/DestroyTest.java
rriggs
parents: 47216
diff changeset
   119
            }
13149
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   120
        }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   121
        return null;
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   122
    }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   123
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   124
    public static void main(String args[]) throws Exception {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   125
        ProcessTest test = getTest();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   126
        if (test == null) {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   127
            throw new RuntimeException("Unrecognised OS");
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   128
        } else {
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   129
            new Thread(test).start();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   130
            Thread.sleep(1000);
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   131
            test.runTest();
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   132
        }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   133
    }
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   134
}
27d52f97a5cc 4244896: (process) Provide System.getPid(), System.killProcess(String pid)
robm
parents:
diff changeset
   135