I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
i2prouter 57.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • # Copyright (c) 1999, 2011 Tanuki Software, Ltd.
    # http://www.tanukisoftware.com
    # All rights reserved.
    
    #
    # Make sure that PIDFILE points to the correct location,
    # if you have changed the default location set in the
    # wrapper configuration file.
    #
    
    zzz's avatar
    zzz committed
    # If this script fails to successfully invoke i2psvc on your platform,
    
    zzz's avatar
    zzz committed
    # try the runplain.sh script instead.
    #
    
    # This software is the proprietary information of Tanuki Software.
    # You shall use it only in accordance with the terms of the
    # license agreement you entered into with Tanuki Software.
    # http://wrapper.tanukisoftware.com/doc/english/licenseOverview.html
    #
    # Java Service Wrapper sh script.  Suitable for starting and stopping
    #  wrapped Java applications on UNIX platforms.
    #
    
    #-----------------------------------------------------------------------------
    # These settings can be modified to fit the needs of your application
    
    # Optimized for use with version 3.5.12 of the Wrapper.
    
    zzz's avatar
    zzz committed
    # Paths
    # Note that (percent)INSTALL_PATH and (percent)SYSTEM_java_io_tmpdir
    # should have been replaced by the izpack installer.
    # If you did not run the installer, replace them with the appropriate path.
    I2P="%INSTALL_PATH"
    I2PTEMP="%SYSTEM_java_io_tmpdir"
    
    # PORTABLE installation:
    # Use the following instead.
    #I2PTEMP="%INSTALL_PATH"
    
    zzz's avatar
    zzz committed
    
    
    # Application
    APP_NAME="i2p"
    APP_LONG_NAME="I2P Service"
    
    
    # If specified, the Wrapper will be run as the specified user.
    # IMPORTANT - Make sure that the user has the required privileges to write
    #  the PID file and wrapper.log files.  Failure to be able to write the log
    #  file will cause the Wrapper to exit without any way to write out an error
    #  message.
    # NOTE - This will set the user which is used to run the Wrapper as well as
    #  the JVM and is not useful in situations where a privileged resource or
    #  port needs to be allocated prior to the user being changed.
    #RUN_AS_USER=
    
    
    zzz's avatar
    zzz committed
    WRAPPER_CMD="$I2P/i2psvc"
    WRAPPER_CONF="$I2P/wrapper.config"
    
    # Priority at which to run the wrapper.  See "man nice" for valid priorities.
    #  nice is only used if a priority is specified.
    PRIORITY=
    
    # Location of the pid file.
    PIDDIR="$I2PTEMP"
    
    
    # If you'd like to run I2P as root (not recommended), uncomment the
    # following line
    #ALLOW_ROOT=true
    
    
    # FIXED_COMMAND tells the script to use a hard coded action rather than
    # expecting the first parameter of the command line to be the command.
    # By default the command will will be expected to be the first parameter.
    #FIXED_COMMAND=console
    
    # PASS_THROUGH tells the script to pass all arguments through to the JVM
    #  as is.  If FIXED_COMMAND is specified then all arguments will be passed.
    #  If not set then all arguments starting with the second will be passed.
    #PASS_THROUGH=true
    
    # If uncommented, causes the Wrapper to be shutdown using an anchor file.
    #  When launched with the 'start' command, it will also ignore all INT and
    #  TERM signals.
    #IGNORE_SIGNALS=true
    
    # Wrapper will start the JVM asynchronously. Your application may have some
    #  initialization tasks and it may be desirable to wait a few seconds
    #  before returning.  For example, to delay the invocation of following
    #  startup scripts.  Setting WAIT_AFTER_STARTUP to a positive number will
    #  cause the start command to delay for the indicated period of time
    #  (in seconds).
    #
    WAIT_AFTER_STARTUP=0
    
    # If set, wait for the wrapper to report that the daemon has started
    WAIT_FOR_STARTED_STATUS=true
    WAIT_FOR_STARTED_TIMEOUT=120
    
    # If set, the status, start_msg and stop_msg commands will print out detailed
    #   state information on the Wrapper and Java processes.
    #DETAIL_STATUS=true
    
    # If set, the 'pause' and 'resume' commands will be enabled.  These make it
    #  possible to pause the JVM or Java application without completely stopping
    #  the Wrapper.  See the wrapper.pausable and wrapper.pausable.stop_jvm
    #  properties for more information.
    #PAUSABLE=true
    
    # By default we show a detailed usage block.  Uncomment to show brief usage.
    #BRIEF_USAGE=true
    
    # flag for using upstart when installing (rather than init.d rc.d)
    USE_UPSTART=
    
    
    # Source the environment variables for the locale if $LANG isn't set
    # If you want to set custom locale variables for I2P,
    # you may comment out this block and set them yourself here.
    
    if [ ! -n $LANG ]; then
            for ENV_FILE in /etc/environment /etc/default/locale; do
                    [ -r "$ENV_FILE" ] || continue
                    [ -s "$ENV_FILE" ] || continue
    
                    for var in LANG LANGUAGE LC_ALL LC_CTYPE; do
                            value=`egrep "^${var}=" "$ENV_FILE" | tail -n1 | cut -d= -f2`
                            [ -n "$value" ] && eval export $var=$value
                    done
            done
    fi
    
    
    # When installing on On Mac OSX platforms, the following domain will be used to
    #  prefix the plist file name.
    PLIST_DOMAIN=org.tanukisoftware.wrapper
    
    # The following two lines are used by the chkconfig command. Change as is
    #  appropriate for your application.  They should remain commented.
    # chkconfig: 2345 20 80
    # description: I2P Service
    
    # Initialization block for the install_initd and remove_initd scripts used by
    #  SUSE linux distributions.
    ### BEGIN INIT INFO
    # Provides: i2p
    # Required-Start: $local_fs $network $syslog
    # Should-Start:
    # Required-Stop:
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: I2P Service
    # Description: I2P is a load-balanced unspoofable packet switching network
    ### END INIT INFO
    
    # Do not modify anything beyond this point
    #-----------------------------------------------------------------------------
    
    if [ ! -e $WRAPPER_CONF ]; then
    
           echo "Starting I2P Failed: Unable to find $WRAPPER_CONF"
           exit 1
    fi
    
    
    kytv's avatar
    kytv committed
    # Workaround for Gentoo
    
    JAVABINARY=$(awk -F'=' '/^ *wrapper\.java\.command/{print $2}' $WRAPPER_CONF)
    
    kytv's avatar
    kytv committed
    
    if [ -e /etc/gentoo-release ]; then
        if [ $JAVABINARY = java ]; then
            if [ -x /etc/java-config-2/current-system-vm/bin/java ]; then
                JAVABINARY="/etc/java-config-2/current-system-vm/bin/java"
            else
    
                echo "Please set wrapper.java.command in $WRAPPER_CONF"
    
    kytv's avatar
    kytv committed
                exit 1
            fi
        fi
    fi
    
    
    WRAPPER_URL="http://www.i2p2.de/manualwrapper"
    
    unsupported() {
        echo "The most likely reason is that a supported version of the java"
        echo "wrapper is not available in the I2P installation package for your"
        echo "platform. It may be possible to manually download and install"
        echo "a compatible wrapper for your system."
        echo "See ${WRAPPER_URL} for hints."
    
        echo "In the meantime, you may start I2P by running the script"
        echo "${I2P}/runplain.sh"
        echo
    }
    
    
    failed() {
            echo "**Failed to load the wrapper**"
            case `uname -s` in
                 FreeBSD)
                    echo
    
                    echo "The wrapper requires libiconv to be on your system."
    
                    echo "It can be installed with pkg_add -r libiconv"
                    echo
                    exit 1
                    ;;
                 *)
                    echo
                    unsupported
                    exit 1
                    ;;
            esac
    }
    
    
    if [ ! -r ${WRAPPER_CMD} ]; then
        echo "Unable to locate i2psvc in ${I2P}!"
        echo
        unsupported
        echo
        exit 1
    fi
    
    
    if [ -n "$FIXED_COMMAND" ]
    then
        COMMAND="$FIXED_COMMAND"
    else
        COMMAND="$1"
    fi
    
    # Required for HP-UX Startup
    if [ `uname -s` = "HP-UX" -o `uname -s` = "HP-UX64" ] ; then
            PATH=$PATH:/usr/bin
    fi
    
    # Get the fully qualified path to the script
    case $0 in
        /*)
            SCRIPT="$0"
            ;;
        *)
            PWD=`pwd`
            SCRIPT="$PWD/$0"
            ;;
    esac
    
    # Resolve the true real path without any sym links.
    CHANGED=true
    while [ "X$CHANGED" != "X" ]
    do
        # Change spaces to ":" so the tokens can be parsed.
        SAFESCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
        # Get the real path to this script, resolving any symbolic links
        TOKENS=`echo $SAFESCRIPT | sed -e 's;/; ;g'`
        REALPATH=
        for C in $TOKENS; do
            # Change any ":" in the token back to a space.
            C=`echo $C | sed -e 's;:; ;g'`
            REALPATH="$REALPATH/$C"
            # If REALPATH is a sym link, resolve it.  Loop for nested links.
            while [ -h "$REALPATH" ] ; do
                LS="`ls -ld "$REALPATH"`"
                LINK="`expr "$LS" : '.*-> \(.*\)$'`"
                if expr "$LINK" : '/.*' > /dev/null; then
                    # LINK is absolute.
                    REALPATH="$LINK"
                else
                    # LINK is relative.
                    REALPATH="`dirname "$REALPATH"`""/$LINK"
                fi
            done
        done
    
        if [ "$REALPATH" = "$SCRIPT" ]
        then
            CHANGED=""
        else
            SCRIPT="$REALPATH"
        fi
    done
    
    # Get the location of the script.
    REALDIR=`dirname "$REALPATH"`
    # Normalize the path
    
    REALDIR=`cd "${REALDIR}"; pwd`
    
    
    # If the PIDDIR is relative, set its value relative to the full REALPATH to avoid problems if
    #  the working directory is later changed.
    FIRST_CHAR=`echo $PIDDIR | cut -c1,1`
    if [ "$FIRST_CHAR" != "/" ]
    then
        PIDDIR=$REALDIR/$PIDDIR
    fi
    # Same test for WRAPPER_CMD
    FIRST_CHAR=`echo $WRAPPER_CMD | cut -c1,1`
    if [ "$FIRST_CHAR" != "/" ]
    then
        WRAPPER_CMD=$REALDIR/$WRAPPER_CMD
    fi
    # Same test for WRAPPER_CONF
    FIRST_CHAR=`echo $WRAPPER_CONF | cut -c1,1`
    if [ "$FIRST_CHAR" != "/" ]
    then
        WRAPPER_CONF=$REALDIR/$WRAPPER_CONF
    fi
    
    
    # Process ID
    ANCHORFILE="$PIDDIR/$APP_NAME.anchor"
    
    COMMANDFILE="$PIDDIR/$APP_NAME.command"
    STATUSFILE="$PIDDIR/$APP_NAME.status"
    JAVASTATUSFILE="$PIDDIR/$APP_NAME.java.status"
    
    PIDFILE="$PIDDIR/$APP_NAME.pid"
    
    LOCKDIR="/var/lock/subsys"
    LOCKFILE="$LOCKDIR/$APP_NAME"
    
    pid=""
    
    # Resolve the location of the 'ps' command
    
    PSEXE="/usr/ucb/ps"
        if [ ! -x "$PSEXE" ]
        then
            PSEXE="/usr/bin/ps"
            if [ ! -x "$PSEXE" ]
            then
                PSEXE="/bin/ps"
                if [ ! -x "$PSEXE" ]
                then
                    eval echo `gettext 'Unable to locate "ps".'`
                    eval echo `gettext 'Please report this message along with the location of the command on your system.'`
                    exit 1
                fi
            fi
        fi
    
    
    TREXE="/usr/bin/tr"
    if [ ! -x "$TREXE" ]
    then
        TREXE="/bin/tr"
        if [ ! -x "$TREXE" ]
        then
            eval echo `gettext 'Unable to locate "tr".'`
            eval echo `gettext 'Please report this message along with the location of the command on your system.'`
            exit 1
        fi
    fi
    
    
    
    DIST_OS=`uname -s | $TREXE "[A-Z]" "[a-z]" | $TREXE -d ' '`
    
    case "$DIST_OS" in
        'sunos')
            DIST_OS="solaris"
            ;;
        'hp-ux' | 'hp-ux64')
            # HP-UX needs the XPG4 version of ps (for -o args)
            DIST_OS="hpux"
            UNIX95=""
            export UNIX95
            ;;
        'darwin')
            DIST_OS="macosx"
            ;;
        'unix_sv')
            DIST_OS="unixware"
            ;;
        'os/390')
            DIST_OS="zos"
            ;;
    esac
    
    # Resolve the architecture
    if [ "$DIST_OS" = "macosx" ]
    
        OS_VER=`sw_vers | grep 'ProductVersion:' | grep -o '[0-9]*\.[0-9]*\.[0-9]*'`
        DIST_ARCH="universal"
    
    kytv's avatar
    kytv committed
        if [ $(sysctl -n hw.cpu64bit_capable) -eq 1 ]; then
            DIST_BITS="64"
        else
            DIST_BITS="32"
        fi
    
        APP_PLIST_BASE=${PLIST_DOMAIN}.${APP_NAME}
        APP_PLIST=${APP_PLIST_BASE}.plist
    else
        DIST_ARCH=
    
        DIST_ARCH=`uname -p 2>/dev/null | $TREXE "[A-Z]" "[a-z]" | $TREXE -d ' '`
    
        if [ "X$DIST_ARCH" = "X" ]
        then
            DIST_ARCH="unknown"
        fi
        if [ "$DIST_ARCH" = "unknown" ]
        then
    
            DIST_ARCH=`uname -m 2>/dev/null | $TREXE "[A-Z]" "[a-z]" | $TREXE -d ' '`
    
        fi
        case "$DIST_ARCH" in
            'athlon' | 'i386' | 'i486' | 'i586' | 'i686')
                DIST_ARCH="x86"
                if [ "${DIST_OS}" = "solaris" ] ; then
                    DIST_BITS=`isainfo -b`
                else
                    DIST_BITS="32"
                fi
                ;;
            'amd64' | 'x86_64')
                DIST_ARCH="x86"
                DIST_BITS="64"
                ;;
            'ia32')
                DIST_ARCH="ia"
                DIST_BITS="32"
                ;;
            'ia64' | 'ia64n' | 'ia64w')
                DIST_ARCH="ia"
                DIST_BITS="64"
                ;;
            'ip27')
                DIST_ARCH="mips"
                DIST_BITS="32"
                ;;
            'power' | 'powerpc' | 'power_pc' | 'ppc64')
                if [ "${DIST_ARCH}" = "ppc64" ] ; then
                    DIST_BITS="64"
                else
                    DIST_BITS="32"
                fi
                DIST_ARCH="ppc"
                if [ "${DIST_OS}" = "aix" ] ; then
                    if [ `getconf KERNEL_BITMODE` -eq 64 ]; then
                        DIST_BITS="64"
                    else
                        DIST_BITS="32"
                    fi
                fi
                ;;
            'pa_risc' | 'pa-risc')
                DIST_ARCH="parisc"
                if [ `getconf KERNEL_BITS` -eq 64 ]; then
                    DIST_BITS="64"
                else
                    DIST_BITS="32"
                fi
                ;;
            'sun4u' | 'sparcv9' | 'sparc')
                DIST_ARCH="sparc"
                DIST_BITS=`isainfo -b`
                ;;
            '9000/800' | '9000/785')
                DIST_ARCH="parisc"
                if [ `getconf KERNEL_BITS` -eq 64 ]; then
                    DIST_BITS="64"
                else
                    DIST_BITS="32"
                fi
                ;;
            '2064' | '2066' | '2084' | '2086' | '2094' | '2096' | '2097' | '2098' | '2817')
                DIST_ARCH="390"
                DIST_BITS="64"
                ;;
        esac
    fi
    
    # OSX always places Java in the same location so we can reliably set JAVA_HOME
    if [ "$DIST_OS" = "macosx" ]
    then
        if [ -z "$JAVA_HOME" ]; then
            JAVA_HOME="/Library/Java/Home"; export JAVA_HOME
    
    # Test Echo
    ECHOTEST=`echo -n "x"`
    if [ "$ECHOTEST" = "x" ]
    then
        ECHOOPT="-n "
    else
        ECHOOPT=""
    fi
    
    
    gettext() {
        "$WRAPPER_CMD" --translate "$1" "$WRAPPER_CONF" 2>/dev/null
        if [ $? != 0 ] ; then
            echo "$1"
        fi
    }
    
    outputFile() {
        if [ -f "$1" ]
        then
            eval echo `gettext '  $1  Found but not executable.'`;
        else
            echo "  $1"
        fi
    }
    
    # Decide on the wrapper binary to use.
    # If the bits of the OS could be detected, we will try to look for the
    #  binary with the correct bits value.  If it doesn't exist, fall back
    #  and look for the 32-bit binary.  If that doesn't exist either then
    #  look for the default.
    WRAPPER_TEST_CMD=""
    if [ -f "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BITS" ]
    then
        WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BITS"
        if [ ! -x "$WRAPPER_TEST_CMD" ]
        then
            chmod +x "$WRAPPER_TEST_CMD" 2>/dev/null
        fi
        if [ -x "$WRAPPER_TEST_CMD" ]
        then
            WRAPPER_CMD="$WRAPPER_TEST_CMD"
        else
            outputFile "$WRAPPER_TEST_CMD"
            WRAPPER_TEST_CMD=""
        fi
    fi
    if [ -f "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32" -a -z "$WRAPPER_TEST_CMD" ]
    then
        WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
        if [ ! -x "$WRAPPER_TEST_CMD" ]
        then
            chmod +x "$WRAPPER_TEST_CMD" 2>/dev/null
        fi
        if [ -x "$WRAPPER_TEST_CMD" ]
        then
            WRAPPER_CMD="$WRAPPER_TEST_CMD"
        else
            outputFile "$WRAPPER_TEST_CMD"
            WRAPPER_TEST_CMD=""
        fi
    fi
    if [ -f "$WRAPPER_CMD" -a -z "$WRAPPER_TEST_CMD" ]
    then
        WRAPPER_TEST_CMD="$WRAPPER_CMD"
        if [ ! -x "$WRAPPER_TEST_CMD" ]
        then
            chmod +x "$WRAPPER_TEST_CMD" 2>/dev/null
        fi
        if [ -x "$WRAPPER_TEST_CMD" ]
        then
            WRAPPER_CMD="$WRAPPER_TEST_CMD"
        else
            outputFile "$WRAPPER_TEST_CMD"
            WRAPPER_TEST_CMD=""
        fi
    fi
    if [ -z "$WRAPPER_TEST_CMD" ]
    then
        eval echo `gettext 'Unable to locate any of the following binaries:'`
        outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BITS"
        if [ ! "$DIST_BITS" = "32" ]
        then
            outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
        fi
        outputFile "$WRAPPER_CMD"
    
        exit 1
    fi
    
    
    
    # Build the nice clause
    if [ "X$PRIORITY" = "X" ]
    then
        CMDNICE=""
    else
        CMDNICE="nice -$PRIORITY"
    fi
    
    
    # Build the anchor file clause.
    if [ "X$IGNORE_SIGNALS" = "X" ]
    then
       ANCHORPROP=
       IGNOREPROP=
    else
       ANCHORPROP=wrapper.anchorfile=\"$ANCHORFILE\"
       IGNOREPROP=wrapper.ignore_signals=TRUE
    fi
    
    # Build the status file clause.
    if [ "X$DETAIL_STATUS$WAIT_FOR_STARTED_STATUS" = "X" ]
    
       STATUSPROP=
    else
       STATUSPROP="wrapper.statusfile=\"$STATUSFILE\" wrapper.java.statusfile=\"$JAVASTATUSFILE\""
    fi
    
    # Build the command file clause.
    if [ -n "$PAUSABLE" ]
    then
       COMMANDPROP="wrapper.commandfile=\"$COMMANDFILE\" wrapper.pausable=TRUE"
    else
       COMMANDPROP=
    fi
    
    if [ ! -n "$WAIT_FOR_STARTED_STATUS" ]
    then
        WAIT_FOR_STARTED_STATUS=true
    fi
    
    if [ $WAIT_FOR_STARTED_STATUS = true ] ; then
        DETAIL_STATUS=true
    fi
    
    
    # Build the lock file clause.  Only create a lock file if the lock directory exists on this platform.
    LOCKPROP=
    if [ -d $LOCKDIR ]
    then
        if [ -w $LOCKDIR ]
    
            LOCKPROP=wrapper.lockfile=\"$LOCKFILE\"
    
    prepAdditionalParams() {
        ADDITIONAL_PARA=""
        if [ -n "$PASS_THROUGH" ] ; then
            ADDITIONAL_PARA="--"
        fi
        while [ -n "$1" ] ; do
            ADDITIONAL_PARA="$ADDITIONAL_PARA \"$1\""
            shift
        done
    }
    
    checkUser() {
        # $1 touchLock flag
        # $2.. [command] args
    
        # Check the configured user.  If necessary rerun this script as the desired user.
        if [ "X$RUN_AS_USER" != "X" ]
        then
            # Resolve the location of the 'id' command
            IDEXE="/usr/xpg4/bin/id"
            if [ ! -x "$IDEXE" ]
            then
                IDEXE="/usr/bin/id"
                if [ ! -x "$IDEXE" ]
                then
                    eval echo `gettext 'Unable to locate "id".'`
                    eval echo `gettext 'Please report this message along with the location of the command on your system.'`
                    exit 1
                fi
            fi
    
            if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ]
            then
                # Already running as the configured user.  Avoid password prompts by not calling su.
                RUN_AS_USER=""
            fi
        fi
        if [ "X$RUN_AS_USER" != "X" ]
        then
            # If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be
            # able to create the lock file.  The Wrapper will be able to update this file once it
            # is created but will not be able to delete it on shutdown.  If $1 is set then
            # the lock file should be created for the current command
            if [ "X$LOCKPROP" != "X" ]
            then
                if [ "X$1" != "X" ]
                then
                    # Resolve the primary group
                    RUN_AS_GROUP=`groups $RUN_AS_USER | awk '{print $3}' | tail -1`
                    if [ "X$RUN_AS_GROUP" = "X" ]
                    then
                        RUN_AS_GROUP=$RUN_AS_USER
                    fi
                    touch $LOCKFILE
                    chown $RUN_AS_USER:$RUN_AS_GROUP $LOCKFILE
                fi
            fi
    
            # Still want to change users, recurse.  This means that the user will only be
            #  prompted for a password once. Variables shifted by 1
            shift
    
            # Wrap the parameters so they can be passed.
            ADDITIONAL_PARA=""
            while [ -n "$1" ] ; do
                ADDITIONAL_PARA="$ADDITIONAL_PARA \"$1\""
                shift
            done
    
            # Use "runuser" if this exists.  runuser should be used on RedHat in preference to su.
            #
            if test -f "/sbin/runuser"
            then
                /sbin/runuser - $RUN_AS_USER -c "\"$REALPATH\" $ADDITIONAL_PARA"
            else
                su - $RUN_AS_USER -c "\"$REALPATH\" $ADDITIONAL_PARA"
            fi
            RUN_AS_USER_EXITCODE=$?
            # Now that we are the original user again, we may need to clean up the lock file.
            if [ "X$LOCKPROP" != "X" ]
            then
                getpid
                if [ "X$pid" = "X" ]
                then
                    # Wrapper is not running so make sure the lock file is deleted.
                    if [ -f "$LOCKFILE" ]
                    then
                        rm "$LOCKFILE"
                    fi
                fi
            fi
    
            exit $RUN_AS_USER_EXITCODE
        fi
    }
    
    
    getpid() {
    
        pid=""
        if [ -f "$PIDFILE" ]
    
                if [ "X$pid" != "X" ]
                then
    
                    # It is possible that 'a' process with the pid exists but that it is not the
                    #  correct process.  This can happen in a number of cases, but the most
                    #  common is during system startup after an unclean shutdown.
                    # The ps statement below looks for the specific wrapper command running as
                    #  the pid.  If it is not found then the pid file is considered to be stale.
                    case "$DIST_OS" in
                        'freebsd')
                            pidtest=`$PSEXE -p $pid -o args | tail -1`
                            if [ "X$pidtest" = "XCOMMAND" ]
                            then
                                pidtest=""
                            fi
                            ;;
                        'macosx')
                            pidtest=`$PSEXE -ww -p $pid -o command | grep -F "$WRAPPER_CMD" | tail -1`
                            ;;
                        'solaris')
                            if [ -f "/usr/bin/pargs" ]
                            then
                                pidtest=`pargs $pid | fgrep "$WRAPPER_CMD" | tail -1`
                            else
                                case "$PSEXE" in
                                '/usr/ucb/ps')
                                    pidtest=`$PSEXE -auxww  $pid | fgrep "$WRAPPER_CMD" | tail -1`
                                    ;;
                                '/usr/bin/ps')
                                    TRUNCATED_CMD=`$PSEXE -o comm -p $pid | tail -1`
                                    COUNT=`echo $TRUNCATED_CMD | wc -m`
                                    COUNT=`echo ${COUNT}`
                                    COUNT=`expr $COUNT - 1`
                                    TRUNCATED_CMD=`echo $WRAPPER_CMD | cut -c1-$COUNT`
                                    pidtest=`$PSEXE -o comm -p $pid | fgrep "$TRUNCATED_CMD" | tail -1`
                                    ;;
                                '/bin/ps')
                                    TRUNCATED_CMD=`$PSEXE -o comm -p $pid | tail -1`
                                    COUNT=`echo $TRUNCATED_CMD | wc -m`
                                    COUNT=`echo ${COUNT}`
                                    COUNT=`expr $COUNT - 1`
                                    TRUNCATED_CMD=`echo $WRAPPER_CMD | cut -c1-$COUNT`
                                    pidtest=`$PSEXE -o comm -p $pid | fgrep "$TRUNCATED_CMD" | tail -1`
                                    ;;
                                *)
                                    echo "Unsupported ps command $PSEXE"
                                    exit 1
                                    ;;
                                esac
                            fi
                            ;;
                        'hpux')
                            pidtest=`$PSEXE -p $pid -x -o args | grep -F "$WRAPPER_CMD" | tail -1`
                            ;;
                        *)
                            pidtest=`$PSEXE -p $pid -o args | grep -F "$WRAPPER_CMD" | tail -1`
                            ;;
                    esac
    
                    if [ "X$pidtest" = "X" ]
    
                    then
                        # This is a stale pid file.
    
                        rm -f "$PIDFILE"
                        eval echo `gettext 'Removed stale pid file: $PIDFILE'`
                        pid=""
    
                eval echo `gettext 'Cannot read $PIDFILE.'`
    
    getstatus() {
        STATUS=
        if [ -f "$STATUSFILE" ]
        then
            if [ -r "$STATUSFILE" ]
            then
                STATUS=`cat "$STATUSFILE"`
            fi
        fi
        if [ "X$STATUS" = "X" ]
        then
            STATUS="Unknown"
        fi
    
        JAVASTATUS=
        if [ -f "$JAVASTATUSFILE" ]
        then
            if [ -r "$JAVASTATUSFILE" ]
            then
                JAVASTATUS=`cat "$JAVASTATUSFILE"`
            fi
        fi
        if [ "X$JAVASTATUS" = "X" ]
        then
            JAVASTATUS="Unknown"
        fi
    }
    
    
    testpid() {
    
        case "$DIST_OS" in
         'solaris')
            case "$PSEXE" in
            '/usr/ucb/ps')
                pid=`$PSEXE  $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
                ;;
            '/usr/bin/ps')
                pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
                ;;
            '/bin/ps')
                pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
                ;;
            *)
                echo "Unsupported ps command $PSEXE"
                exit 1
                ;;
            esac
            ;;
        *)
            pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1` 2>/dev/null
            ;;
        esac
    
        if [ "X$pid" = "X" ]
        then
            # Process is gone so remove the pid file.
    
            rm -f "$PIDFILE"
            pid=""
        fi
    }
    
    launchdtrap() {
        stopit
        exit
    }
    
    waitforwrapperstop() {
        getpid
        while [ "X$pid" != "X" ] ; do
            sleep 1
            getpid
        done
    }
    
    launchdinternal() {
        getpid
        trap launchdtrap TERM
        if [ "X$pid" = "X" ]
        then
            prepAdditionalParams "$@"
    
            # The string passed to eval must handles spaces in paths correctly.
            COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.pidfile=\"$PIDFILE\" wrapper.name=\"$APP_NAME\" wrapper.displayname=\"$APP_LONG_NAME\" wrapper.daemonize=TRUE $ANCHORPROP $IGNOREPROP $STATUSPROP $COMMANDPROP $LOCKPROP $ADDITIONAL_PARA"
            eval $COMMAND_LINE
        else
            eval echo `gettext '$APP_LONG_NAME is already running.'`
            exit 1
        fi
        # launchd expects that this script stay up and running so we need to do our own monitoring of the Wrapper process.
        if [ $WAIT_FOR_STARTED_STATUS = true ]
        then
            waitforwrapperstop
    
        eval echo `gettext 'Running $APP_LONG_NAME...'`
    
        getpid
        if [ "X$pid" = "X" ]
        then
    
    
            prepAdditionalParams "$@"
    
            # The string passed to eval must handles spaces in paths correctly.
    
    kytv's avatar
    kytv committed
            COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.java.command=\"$JAVABINARY\" wrapper.pidfile=\"$PIDFILE\" wrapper.name=\"$APP_NAME\" wrapper.displayname=\"$APP_LONG_NAME\" $ANCHORPROP $STATUSPROP $COMMANDPROP $LOCKPROP $ADDITIONAL_PARA"
    
            eval $COMMAND_LINE
            if [ "$?" -ne "0" ]; then
                    failed
            fi
        else
            eval echo `gettext '$APP_LONG_NAME is already running.'`
            exit 1
        fi
    }
    
    waitforjavastartup() {
        getstatus
        eval echo $ECHOOPT `gettext 'Waiting for $APP_LONG_NAME...'`
    
        # Wait until the timeout or we have something besides Unknown.
        counter=15
        while [ "$JAVASTATUS" = "Unknown" -a $counter -gt 0 -a -n "$JAVASTATUS" ] ; do
            echo $ECHOOPT"."
            sleep 1
            getstatus
            counter=`expr $counter - 1`
        done
    
        if [ -n "$WAIT_FOR_STARTED_TIMEOUT" ] ; then
            counter=$WAIT_FOR_STARTED_TIMEOUT
        else
            counter=120
        fi
        while [ "$JAVASTATUS" != "STARTED" -a "$JAVASTATUS" != "Unknown" -a $counter -gt 0 -a -n "$JAVASTATUS" ] ; do
            echo $ECHOOPT"."
            sleep 1
            getstatus
            counter=`expr $counter - 1`
        done
        if [ "X$ECHOOPT" != "X" ] ; then
            echo ""
        fi
    }
    
    startwait() {
        if [ $WAIT_FOR_STARTED_STATUS = true ]
        then
            waitforjavastartup
        fi
        # Sleep for a few seconds to allow for intialization if required
        #  then test to make sure we're still running.
        #
        i=0
        while [ $i -lt $WAIT_AFTER_STARTUP ]
        do
            sleep 1
            echo $ECHOOPT"."
            i=`expr $i + 1`
        done
        if [ $WAIT_AFTER_STARTUP -gt 0 -o $WAIT_FOR_STARTED_STATUS = true ]
        then
            getpid
            if [ "X$pid" = "X" ]
    
                eval echo `gettext ' WARNING: $APP_LONG_NAME may have failed to start.'`
                exit 1
    
                eval echo `gettext ' running: PID:$pid'`
    
            echo ""
        fi
    }
    
    macosxstart() {
        # The daemon has been installed.
        eval echo `gettext 'Starting $APP_LONG_NAME.  Detected Mac OSX and installed launchd daemon.'`
        if [ `id | sed 's/^uid=//;s/(.*$//'` != "0" ] ; then
            eval echo `gettext 'Must be root to perform this action.'`
            exit 1
        fi
    
        getpid
        if [ "X$pid" != "X" ] ; then
            eval echo `gettext '$APP_LONG_NAME is already running.'`
    
    
        # If the daemon was just installed, it may not be loaded.
        LOADED_PLIST=`launchctl list | grep ${APP_PLIST_BASE}`
        if [ "X${LOADED_PLIST}" = "X" ] ; then
            launchctl load /Library/LaunchDaemons/${APP_PLIST}
        fi
        # If launchd is set to run the daemon already at Load, we don't need to call start
        getpid
    
    kytv's avatar
    kytv committed
        if [ "X$pid" = "X" ] ; then
    
            launchctl start ${APP_PLIST_BASE}
        fi
    
        startwait
    }
    
    upstartstart() {
        # The daemon has been installed.
        eval echo `gettext 'Starting $APP_LONG_NAME.  Detected Linux and installed upstart.'`
        if [ `id | sed 's/^uid=//;s/(.*$//'` != "0" ] ; then
            eval echo `gettext 'Must be root to perform this action.'`
            exit 1
        fi
    
        getpid
        if [ "X$pid" != "X" ] ; then
            eval echo `gettext '$APP_LONG_NAME is already running.'`
            exit 1
        fi
    
        /sbin/start ${APP_NAME}
    
        startwait
    
        eval echo `gettext 'Starting $APP_LONG_NAME...'`
    
        getpid
        if [ "X$pid" = "X" ]
        then