1 /*
   2  * Copyright (c) 2003, 2018, 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 Launcher to launch a java process with its standard input, output,
  26  * and error streams connected to a socket.
  27  */
  28 import java.io.IOException;
  29 import java.net.InetAddress;
  30 import java.net.InetSocketAddress;
  31 import java.nio.channels.DatagramChannel;
  32 import java.nio.channels.ServerSocketChannel;
  33 import java.nio.channels.SocketChannel;
  34 
  35 public class Launcher {
  36 
  37     static {
  38         System.loadLibrary("InheritedChannel");
  39     }
  40 
  41     private static native void launch0(String cmdarray[], int fd) throws IOException;
  42 
  43     private static void launch(String className, String options[], String args[], int fd) throws IOException {
  44         // java [-options] class [args...]
  45         int optsLen = (options == null) ? 0 : options.length;
  46         int argsLen = (args == null) ? 0 : args.length;
  47         int len = 1 + optsLen + 1 + argsLen;
  48         String cmdarray[] = new String[len];
  49         int pos = 0;
  50         cmdarray[pos++] = Util.javaCommand();
  51         if (options != null) {
  52             for (String opt: options) {
  53                 cmdarray[pos++] = opt;
  54             }
  55         }
  56         cmdarray[pos++] = className;
  57         if (args != null) {
  58             for (String arg: args) {
  59                 cmdarray[pos++] = arg;
  60             }
  61         }
  62         launch0(cmdarray, fd);
  63     }
  64 
  65 
  66     /**
  67      * Launch 'java' with specified class using a UnixDomainSocket pair linking calling
  68      * process to the child VM. UnixDomainSocket is a simplified interface to PF_UNIX sockets
  69      * which supports byte a time reads and writes.
  70      */
  71     public static UnixDomainSocket launchWithUnixDomainSocket(String className) throws IOException {
  72         UnixDomainSocket[] socks = UnixDomainSocket.socketpair();
  73         launch(className, null, null, socks[0].fd());
  74         socks[0].close();
  75         return socks[1];
  76     }
  77 
  78     /*
  79      * Launch 'java' with specified class with the specified arguments (may be null).
  80      * The launched process will inherit a connected TCP socket. The remote endpoint
  81      * will be the SocketChannel returned by this method.
  82      */
  83     public static SocketChannel launchWithSocketChannel(String className, String options[], String args[]) throws IOException {
  84         ServerSocketChannel ssc = ServerSocketChannel.open();
  85         ssc.socket().bind(new InetSocketAddress(0));
  86         InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(),
  87                                                       ssc.socket().getLocalPort());
  88         SocketChannel sc1 = SocketChannel.open(isa);
  89         SocketChannel sc2 = ssc.accept();
  90         launch(className, options, args, Util.getFD(sc2));
  91         sc2.close();
  92         ssc.close();
  93         return sc1;
  94     }
  95 
  96     public static SocketChannel launchWithSocketChannel(String className, String args[]) throws IOException {
  97         return launchWithSocketChannel(className, null, args);
  98     }
  99 
 100     public static SocketChannel launchWithSocketChannel(String className) throws IOException {
 101         return launchWithSocketChannel(className, null);
 102     }
 103 
 104     /*
 105      * Launch 'java' with specified class with the specified arguments (may be null).
 106      * The launched process will inherited a TCP listener socket.
 107      * Once launched this method tries to connect to service. If a connection
 108      * can be established a SocketChannel, connected to the service, is returned.
 109      */
 110     public static SocketChannel launchWithServerSocketChannel(String className, String options[], String args[])
 111         throws IOException
 112     {
 113         ServerSocketChannel ssc = ServerSocketChannel.open();
 114         ssc.socket().bind(new InetSocketAddress(0));
 115         int port = ssc.socket().getLocalPort();
 116         launch(className, options, args, Util.getFD(ssc));
 117         ssc.close();
 118         InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
 119         return SocketChannel.open(isa);
 120     }
 121 
 122     public static SocketChannel launchWithServerSocketChannel(String className, String args[]) throws IOException {
 123         return launchWithServerSocketChannel(className, null, args);
 124     }
 125 
 126     public static SocketChannel launchWithServerSocketChannel(String className) throws IOException {
 127         return launchWithServerSocketChannel(className, null);
 128     }
 129 
 130     /*
 131      * Launch 'java' with specified class with the specified arguments (may be null).
 132      * The launch process will inherited a bound UDP socket.
 133      * Once launched this method creates a DatagramChannel and "connects
 134      * it to the service. The created DatagramChannel is then returned.
 135      * As it is connected any packets sent from the socket will be
 136      * sent to the service.
 137      */
 138     public static DatagramChannel launchWithDatagramChannel(String className, String options[], String args[])
 139         throws IOException
 140     {
 141         DatagramChannel dc = DatagramChannel.open();
 142         dc.socket().bind(new InetSocketAddress(0));
 143 
 144         int port = dc.socket().getLocalPort();
 145         launch(className, options, args, Util.getFD(dc));
 146         dc.close();
 147 
 148         dc = DatagramChannel.open();
 149         InetAddress address = InetAddress.getLocalHost();
 150         if (address.isLoopbackAddress()) {
 151             address = InetAddress.getLoopbackAddress();
 152         }
 153         InetSocketAddress isa = new InetSocketAddress(address, port);
 154 
 155         dc.connect(isa);
 156         return dc;
 157     }
 158 
 159     public static DatagramChannel launchWithDatagramChannel(String className, String args[]) throws IOException {
 160         return launchWithDatagramChannel(className, null, args);
 161     }
 162 
 163     public static DatagramChannel launchWithDatagramChannel(String className) throws IOException {
 164         return launchWithDatagramChannel(className, null);
 165     }
 166 }