Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/ProcessBuilder/Zombies.java
41149 views
1
/*
2
* Copyright (c) 2006, 2017, 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
* @run main/othervm Zombies
27
* @bug 6474073 6180151
28
* @summary Make sure zombies don't get created on Unix
29
* @author Martin Buchholz
30
*/
31
32
import java.io.*;
33
34
public class Zombies {
35
36
static final String os = System.getProperty("os.name");
37
38
static final String TrueCommand = os.contains("OS X")?
39
"/usr/bin/true" : "/bin/true";
40
41
public static void main(String[] args) throws Throwable {
42
if (! new File("/usr/bin/perl").canExecute() ||
43
! new File("/bin/ps").canExecute())
44
return;
45
System.out.println("Looks like a Unix system.");
46
long mypid = ProcessHandle.current().pid();
47
System.out.printf("mypid: %d%n", mypid);
48
49
final Runtime rt = Runtime.getRuntime();
50
51
try {
52
rt.exec("no-such-file");
53
throw new Error("expected IOException not thrown");
54
} catch (IOException expected) {/* OK */}
55
56
try {
57
rt.exec(".");
58
throw new Error("expected IOException not thrown");
59
} catch (IOException expected) {/* OK */}
60
61
try {
62
rt.exec(TrueCommand, null, new File("no-such-dir"));
63
throw new Error("expected IOException not thrown");
64
} catch (IOException expected) {/* OK */}
65
66
Process p = rt.exec(TrueCommand);
67
ProcessHandle pp = p.toHandle().parent().orElse(null);
68
System.out.printf("%s pid: %d, parent: %s%n", TrueCommand, p.pid(), pp);
69
p.waitFor();
70
71
// Count all the zombies that are children of this Java process
72
final String[] zombieCounter = {
73
"/usr/bin/perl", "-e",
74
"$a=`/bin/ps -eo ppid,pid,s,command`;" +
75
"print @b=$a=~/^ *@{[getppid]} +[0-9]+ +Z.*$/mog;" +
76
"exit @b"
77
};
78
79
ProcessBuilder pb = new ProcessBuilder(zombieCounter);
80
pb.inheritIO();
81
int zombies = pb.start().waitFor();
82
if (zombies != 0) {
83
throw new Error(zombies + " zombies!");
84
}
85
}
86
}
87
88