LaunchManager.java

/*
** Module   : LaunchManager.java
** Abstract : External application launcher.
**
** Copyright (c) 2010-2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------Description----------------------------------
** 001 SIY 20100629 Created initial version
** 002 SIY 20100804 Changes in imports, some refactorings.
** 003 SIY 20100817 Moved to util package.
** 004 LMR 20101123 Made process launching/waiting routines 
**                  (pseudoTerminalLaunch(), pseudoTerminalWait())
**                  native, using libp2j.so, thus decoupling them
**                  from charva and libTerminal.so.
** 005 GES 20111006 Cleaned up an import, added a helper method and added
**                  docs.
** 006 EVL 20140117 The native library call is performing now in dedicated place and only once.
**                  See the ClientCore class. Removed init native call due to this is signal
**                  related.
** 007 ECF 20150126 Added background mode flag to pseudoTerminalLaunch signature.
** 008 EVL 20160224 Javadoc fixes to make compatible with Oracle Java 8 for Solaris 10.
** 009 GBB 20231207 Adding FWD_VERSION env var before launching a new child process.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
** 
** Additional terms under GNU Affero GPL version 3 section 7:
** 
**   Under Section 7 of the GNU Affero GPL version 3, the following additional
**   terms apply to the works covered under the License.  These additional terms
**   are non-permissive additional terms allowed under Section 7 of the GNU
**   Affero GPL version 3 and may not be removed by you.
** 
**   0. Attribution Requirement.
** 
**     You must preserve all legal notices or author attributions in the covered
**     work or Appropriate Legal Notices displayed by works containing the covered
**     work.  You may not remove from the covered work any author or developer
**     credit already included within the covered work.
** 
**   1. No License To Use Trademarks.
** 
**     This license does not grant any license or rights to use the trademarks
**     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
**     of Golden Code Development Corporation. You are not authorized to use the
**     name Golden Code, FWD, or the names of any author or contributor, for
**     publicity purposes without written authorization.
** 
**   2. No Misrepresentation of Affiliation.
** 
**     You may not represent yourself as Golden Code Development Corporation or FWD.
** 
**     You may not represent yourself for publicity purposes as associated with
**     Golden Code Development Corporation, FWD, or any author or contributor to
**     the covered work, without written authorization.
** 
**   3. No Misrepresentation of Source or Origin.
** 
**     You may not represent the covered work as solely your work.  All modified
**     versions of the covered work must be marked in a reasonable way to make it
**     clear that the modified work is not originating from Golden Code Development
**     Corporation or FWD.  All modified versions must contain the notices of
**     attribution required in this license.
*/

package com.goldencode.p2j.util;

import java.io.*;

/**
 * External program launch manager which properly handles terminal state 
 * during program execution.
 */
public class LaunchManager
{
   /**
    * Creates a child process with all or some of its stdio handles redirected
    * to the pipes and the rest of them left unchanged. Unchanged stdio handles
    * are inherited from the calling process and normally are connected to the
    * process' terminal.
    * <p>
    * The handles redirected to the pipes can be used to communicate between
    * processes.
    * <p>
    * In Progress, there are following use cases for the redirected I/O:
    * <table style="text-align: left; width: 100%;" border="1" cellspacing="2"
    *  cellpadding="2" summary="">
    *   <tbody>
    *     <tr>
    *       <td style="vertical-align: top;">stdio\\use<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;
    *           font-weight: bold;">Interactive Process Launch<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;
    *           font-weight: bold;">INPUT THROUGH<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;
    *           font-weight: bold;">INPUT-OUTPUT THROUGH<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;"><span
    *           style="font-weight: bold;">OUTPUT THROUGH</span><br>
    *       </td>
    *     </tr>
    *     <tr>
    *       <td style="vertical-align: top;">stdin<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;">TTY<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;">TTY<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;">pipe<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;">pipe<br>
    *       </td>
    *     </tr>
    *     <tr>
    *       <td style="vertical-align: top;">stdout<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;">TTY<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;">pipe<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;">pipe<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;">TTY<br>
    *       </td>
    *     </tr>
    *     <tr>
    *       <td style="vertical-align: top;">stderr<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;">TTY<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;">pipe<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;">pipe<br>
    *       </td>
    *       <td style="vertical-align: top; text-align: center;">TTY<br>
    *       </td>
    *     </tr>
    *   </tbody>
    * </table>
    *
    * @param    cmdline
    *           The command line to execute.  The first element must be the 
    *           program name.
    * @param    in
    *           File descriptor to be associated with the pipe that redirects
    *           stdin, or <code>null</code> if no redirection is required.
    * @param    out
    *           File descriptor to be associated with the pipe that redirects
    *           stdout, or <code>null</code> if no redirection is required.
    * @param    err
    *           File descriptor to be associated with the pipe that redirects
    *           stderr, or <code>null</code> if no redirection is required.
    * @param    silent
    *           <code>false</code> for interactive mode (there the terminal
    *           is expected to be shared by the child process and the user is
    *           allowed to directly interact with the child process.
    * @param    background
    *           <code>true</code> if client is running in the background
    *           (disconnected from its controlling terminal); else <code>false</code>.
    * @param    fwdVersion
    *           The FWD version.
    *
    * @return   The ID of the launched child process.
    */
   public static native int pseudoTerminalLaunch(String[]       cmdline,
                                                 FileDescriptor in,
                                                 FileDescriptor out,
                                                 FileDescriptor err,
                                                 boolean        silent,
                                                 boolean        background,
                                                 String         fwdVersion);

   /**
    * Wait for a specific child process (which was previously launched
    * using {@link #pseudoTerminalLaunch}) to exit.
    *
    * @param    pid
    *           ID of the child process to be waited.
    * @param    silent
    *           <code>false</code> for interactive mode (there the terminal
    *           is expected to be shared by the child process and the user is
    *           allowed to directly interact with the child process.
    */
   public static native void pseudoTerminalWait(int pid, boolean silent);
   
   /**
    * Wait for a specific non-interactive child process (which was previously
    * launched using {@link #pseudoTerminalLaunch}) to exit.
    *
    * @param    pid
    *           ID of the child process to be waited.
    */
   public static void pseudoTerminalWait(int pid)
   {
      pseudoTerminalWait(pid, true);
   }
}