1 /*
2  * Copyright (c) 2006, 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  * A test for
26  * 6469606: (process) Process.destroy() can kill wrong process (Unix)
27  * that is not suitable for use as a regression test, because it must
28  * create 32768 processes, and depends on perl.
29  */
30 import java.io.*;
31 
32 public class FeelingLucky {
33 
main(String[] args)34     public static void main(String[] args) throws Throwable {
35         final Runtime rt = Runtime.getRuntime();
36         final String[] pidPrinter = {"/bin/sh", "-c", "echo $$"};
37         final Process minedProcess = rt.exec(pidPrinter);
38         int minedPid = 0;
39         final InputStream is = minedProcess.getInputStream();
40         int c;
41         while ((c = is.read()) >= '0' && c <= '9')
42             minedPid = 10 * minedPid + (c - '0');
43         System.out.printf("minedPid=%d%n", minedPid);
44         minedProcess.waitFor();
45 
46         final String[] magnum = {
47             "perl", "-e",
48             "my $punk = getppid();" +
49             "open TTY, '> /dev/tty';" +
50             "print TTY \"punk=$punk\\n\";" +
51             "for (my $i = 0; $i < 32768; $i++) {" +
52             "  my $pid = fork();" +
53             "  if ($pid == 0) {" +
54             "    if ($$ == " + minedPid + ") {" +
55             "      print TTY \"MATCH $$ $punk\\n\";" +
56             "      $SIG{TERM} = sub {" +
57             "        print TTY \"Got TERM $$ $punk\\n\";" +
58             "        kill 9, $punk;" +
59             "        exit; };" +
60             "      $| = 1; print 'Go ahead.  Make my day.';" +
61             "      sleep 100;" +
62             "    }" +
63             "    exit;" +
64             "  } else { " +
65             "    waitpid($pid,0);" +
66             "    break if $pid == " + minedPid + ";" +
67             "  }" +
68             "}"
69         };
70 
71         Process p = rt.exec(magnum);
72         if (p.getInputStream().read() != -1) {
73             System.out.println("got a char");
74             minedProcess.destroy();
75             Thread.sleep(1000);
76         }
77     }
78 }
79