Zombies.java revision 11039:403e8685286d
1/*
2 * Copyright (c) 2006, 2012, 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 6474073
27 * @summary Make sure zombies don't get created on Unix
28 * @author Martin Buchholz
29 */
30
31import java.io.*;
32
33public class Zombies {
34
35    static final String os = System.getProperty("os.name");
36
37    static final String TrueCommand = os.contains("OS X")?
38        "/usr/bin/true" : "/bin/true";
39
40    public static void main(String[] args) throws Throwable {
41        if (! new File("/usr/bin/perl").canExecute() ||
42            ! new File("/bin/ps").canExecute())
43            return;
44        System.out.println("Looks like a Unix system.");
45        final Runtime rt = Runtime.getRuntime();
46
47        try {
48            rt.exec("no-such-file");
49            throw new Error("expected IOException not thrown");
50        } catch (IOException expected) {/* OK */}
51
52        try {
53            rt.exec(".");
54            throw new Error("expected IOException not thrown");
55        } catch (IOException expected) {/* OK */}
56
57        try {
58            rt.exec(TrueCommand, null, new File("no-such-dir"));
59            throw new Error("expected IOException not thrown");
60        } catch (IOException expected) {/* OK */}
61
62        rt.exec(TrueCommand).waitFor();
63
64        // Count all the zombies that are children of this Java process
65        final String[] zombieCounter = {
66            "/usr/bin/perl", "-e",
67            "exit @{[`/bin/ps -eo ppid,s` =~ /^ *@{[getppid]} +Z$/mog]}"
68        };
69
70        int zombies = rt.exec(zombieCounter).waitFor();
71        if (zombies != 0) throw new Error(zombies + " zombies!");
72    }
73}
74