source: installer/resources/i2prouter @ 85d7cfb

Last change on this file since 85d7cfb was 85d7cfb, checked in by kytv <kytv@…>, 9 years ago

Remove broken if statement

Tanuki's script has a broken check for
OSX versions older than Leopard that sets
the CPU bitness to 32 for < 10.5. Since the
doesn't even work on Tiger (or older) (and
our binary is quad-FAT), this check is not
neeeded.

  • Property mode set to 100644
File size: 57.0 KB
Line 
1#!/bin/sh
2
3#
4# Copyright (c) 1999, 2011 Tanuki Software, Ltd.
5# http://www.tanukisoftware.com
6# All rights reserved.
7#
8# Make sure that PIDFILE points to the correct location,
9# if you have changed the default location set in the
10# wrapper configuration file.
11#
12# If this script fails to successfully invoke i2psvc on your platform,
13# try the runplain.sh script instead.
14#
15# This software is the proprietary information of Tanuki Software.
16# You shall use it only in accordance with the terms of the
17# license agreement you entered into with Tanuki Software.
18# http://wrapper.tanukisoftware.com/doc/english/licenseOverview.html
19#
20# Java Service Wrapper sh script.  Suitable for starting and stopping
21#  wrapped Java applications on UNIX platforms.
22#
23#-----------------------------------------------------------------------------
24# These settings can be modified to fit the needs of your application
25# Optimized for use with version 3.5.12 of the Wrapper.
26
27# Paths
28# Note that (percent)INSTALL_PATH and (percent)SYSTEM_java_io_tmpdir
29# should have been replaced by the izpack installer.
30# If you did not run the installer, replace them with the appropriate path.
31I2P="%INSTALL_PATH"
32I2PTEMP="%SYSTEM_java_io_tmpdir"
33# PORTABLE installation:
34# Use the following instead.
35#I2PTEMP="%INSTALL_PATH"
36
37# Application
38APP_NAME="i2p"
39APP_LONG_NAME="I2P Service"
40
41# If specified, the Wrapper will be run as the specified user.
42# IMPORTANT - Make sure that the user has the required privileges to write
43#  the PID file and wrapper.log files.  Failure to be able to write the log
44#  file will cause the Wrapper to exit without any way to write out an error
45#  message.
46# NOTE - This will set the user which is used to run the Wrapper as well as
47#  the JVM and is not useful in situations where a privileged resource or
48#  port needs to be allocated prior to the user being changed.
49#RUN_AS_USER=
50
51# Wrapper
52WRAPPER_CMD="$I2P/i2psvc"
53WRAPPER_CONF="$I2P/wrapper.config"
54
55# Priority at which to run the wrapper.  See "man nice" for valid priorities.
56#  nice is only used if a priority is specified.
57PRIORITY=
58
59# Location of the pid file.
60PIDDIR="$I2PTEMP"
61
62# If you'd like to run I2P as root (not recommended), uncomment the
63# following line
64#ALLOW_ROOT=true
65
66# FIXED_COMMAND tells the script to use a hard coded action rather than
67# expecting the first parameter of the command line to be the command.
68# By default the command will will be expected to be the first parameter.
69#FIXED_COMMAND=console
70
71# PASS_THROUGH tells the script to pass all arguments through to the JVM
72#  as is.  If FIXED_COMMAND is specified then all arguments will be passed.
73#  If not set then all arguments starting with the second will be passed.
74#PASS_THROUGH=true
75
76# If uncommented, causes the Wrapper to be shutdown using an anchor file.
77#  When launched with the 'start' command, it will also ignore all INT and
78#  TERM signals.
79#IGNORE_SIGNALS=true
80
81# Wrapper will start the JVM asynchronously. Your application may have some
82#  initialization tasks and it may be desirable to wait a few seconds
83#  before returning.  For example, to delay the invocation of following
84#  startup scripts.  Setting WAIT_AFTER_STARTUP to a positive number will
85#  cause the start command to delay for the indicated period of time
86#  (in seconds).
87#
88WAIT_AFTER_STARTUP=0
89
90# If set, wait for the wrapper to report that the daemon has started
91WAIT_FOR_STARTED_STATUS=true
92WAIT_FOR_STARTED_TIMEOUT=120
93
94# If set, the status, start_msg and stop_msg commands will print out detailed
95#   state information on the Wrapper and Java processes.
96#DETAIL_STATUS=true
97
98# If set, the 'pause' and 'resume' commands will be enabled.  These make it
99#  possible to pause the JVM or Java application without completely stopping
100#  the Wrapper.  See the wrapper.pausable and wrapper.pausable.stop_jvm
101#  properties for more information.
102#PAUSABLE=true
103
104# By default we show a detailed usage block.  Uncomment to show brief usage.
105#BRIEF_USAGE=true
106
107# flag for using upstart when installing (rather than init.d rc.d)
108USE_UPSTART=
109
110# When installing on On Mac OSX platforms, the following domain will be used to
111#  prefix the plist file name.
112PLIST_DOMAIN=org.tanukisoftware.wrapper
113
114# The following two lines are used by the chkconfig command. Change as is
115#  appropriate for your application.  They should remain commented.
116# chkconfig: 2345 20 80
117# description: I2P Service
118
119# Initialization block for the install_initd and remove_initd scripts used by
120#  SUSE linux distributions.
121### BEGIN INIT INFO
122# Provides: i2p
123# Required-Start: $local_fs $network $syslog
124# Should-Start:
125# Required-Stop:
126# Default-Start: 2 3 4 5
127# Default-Stop: 0 1 6
128# Short-Description: I2P Service
129# Description: I2P is a load-balanced unspoofable packet switching network
130### END INIT INFO
131
132# Do not modify anything beyond this point
133#-----------------------------------------------------------------------------
134if [ ! -e $WRAPPER_CONF ]; then
135       echo "Starting I2P Failed: Unable to find $WRAPPER_CONF"
136       exit 1
137fi
138
139# Workaround for Gentoo
140JAVABINARY=`grep ^wrapper\.java\.command $WRAPPER_CONF | cut -f2 -d=`
141
142if [ -e /etc/gentoo-release ]; then
143    if [ $JAVABINARY = java ]; then
144        if [ -x /etc/java-config-2/current-system-vm/bin/java ]; then
145            JAVABINARY="/etc/java-config-2/current-system-vm/bin/java"
146        else
147            echo "Please set wrapper.java.command in $WRAPPER_CONF"
148            exit 1
149        fi
150    fi
151fi
152
153WRAPPER_URL="http://www.i2p2.de/manualwrapper"
154unsupported() {
155    echo "The most likely reason is that a supported version of the java"
156    echo "wrapper is not available in the I2P installation package for your"
157    echo "platform. It may be possible to manually download and install"
158    echo "a compatible wrapper for your system."
159    echo "See ${WRAPPER_URL} for hints."
160    echo
161    echo "In the meantime, you may start I2P by running the script"
162    echo "${I2P}/runplain.sh"
163    echo
164}
165
166failed() {
167        echo "**Failed to load the wrapper**"
168        case `uname -s` in
169             FreeBSD)
170                echo
171                echo "The wrapper requires libiconv to be on your system."
172                echo "It can be installed with pkg_add -r libiconv"
173                echo
174                exit 1
175                ;;
176             *)
177                echo
178                unsupported
179                exit 1
180                ;;
181        esac
182}
183
184if [ ! -r ${WRAPPER_CMD} ]; then
185    echo "Unable to locate i2psvc in ${I2P}!"
186    echo
187    unsupported
188    echo
189    exit 1
190fi
191
192if [ -n "$FIXED_COMMAND" ]
193then
194    COMMAND="$FIXED_COMMAND"
195else
196    COMMAND="$1"
197fi
198
199
200# Required for HP-UX Startup
201if [ `uname -s` = "HP-UX" -o `uname -s` = "HP-UX64" ] ; then
202        PATH=$PATH:/usr/bin
203fi
204
205# Get the fully qualified path to the script
206case $0 in
207    /*)
208        SCRIPT="$0"
209        ;;
210    *)
211        PWD=`pwd`
212        SCRIPT="$PWD/$0"
213        ;;
214esac
215
216# Resolve the true real path without any sym links.
217CHANGED=true
218while [ "X$CHANGED" != "X" ]
219do
220    # Change spaces to ":" so the tokens can be parsed.
221    SAFESCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
222    # Get the real path to this script, resolving any symbolic links
223    TOKENS=`echo $SAFESCRIPT | sed -e 's;/; ;g'`
224    REALPATH=
225    for C in $TOKENS; do
226        # Change any ":" in the token back to a space.
227        C=`echo $C | sed -e 's;:; ;g'`
228        REALPATH="$REALPATH/$C"
229        # If REALPATH is a sym link, resolve it.  Loop for nested links.
230        while [ -h "$REALPATH" ] ; do
231            LS="`ls -ld "$REALPATH"`"
232            LINK="`expr "$LS" : '.*-> \(.*\)$'`"
233            if expr "$LINK" : '/.*' > /dev/null; then
234                # LINK is absolute.
235                REALPATH="$LINK"
236            else
237                # LINK is relative.
238                REALPATH="`dirname "$REALPATH"`""/$LINK"
239            fi
240        done
241    done
242
243    if [ "$REALPATH" = "$SCRIPT" ]
244    then
245        CHANGED=""
246    else
247        SCRIPT="$REALPATH"
248    fi
249done
250
251# Get the location of the script.
252REALDIR=`dirname "$REALPATH"`
253# Normalize the path
254REALDIR=`cd "${REALDIR}"; pwd`
255
256# If the PIDDIR is relative, set its value relative to the full REALPATH to avoid problems if
257#  the working directory is later changed.
258FIRST_CHAR=`echo $PIDDIR | cut -c1,1`
259if [ "$FIRST_CHAR" != "/" ]
260then
261    PIDDIR=$REALDIR/$PIDDIR
262fi
263# Same test for WRAPPER_CMD
264FIRST_CHAR=`echo $WRAPPER_CMD | cut -c1,1`
265if [ "$FIRST_CHAR" != "/" ]
266then
267    WRAPPER_CMD=$REALDIR/$WRAPPER_CMD
268fi
269# Same test for WRAPPER_CONF
270FIRST_CHAR=`echo $WRAPPER_CONF | cut -c1,1`
271if [ "$FIRST_CHAR" != "/" ]
272then
273    WRAPPER_CONF=$REALDIR/$WRAPPER_CONF
274fi
275
276# Process ID
277ANCHORFILE="$PIDDIR/$APP_NAME.anchor"
278COMMANDFILE="$PIDDIR/$APP_NAME.command"
279STATUSFILE="$PIDDIR/$APP_NAME.status"
280JAVASTATUSFILE="$PIDDIR/$APP_NAME.java.status"
281PIDFILE="$PIDDIR/$APP_NAME.pid"
282LOCKDIR="/var/lock/subsys"
283LOCKFILE="$LOCKDIR/$APP_NAME"
284pid=""
285
286# Resolve the location of the 'ps' command
287PSEXE="/usr/ucb/ps"
288    if [ ! -x "$PSEXE" ]
289    then
290        PSEXE="/usr/bin/ps"
291        if [ ! -x "$PSEXE" ]
292        then
293            PSEXE="/bin/ps"
294            if [ ! -x "$PSEXE" ]
295            then
296                eval echo `gettext 'Unable to locate "ps".'`
297                eval echo `gettext 'Please report this message along with the location of the command on your system.'`
298                exit 1
299            fi
300        fi
301    fi
302
303TREXE="/usr/bin/tr"
304if [ ! -x "$TREXE" ]
305then
306    TREXE="/bin/tr"
307    if [ ! -x "$TREXE" ]
308    then
309        eval echo `gettext 'Unable to locate "tr".'`
310        eval echo `gettext 'Please report this message along with the location of the command on your system.'`
311        exit 1
312    fi
313fi
314
315
316# Resolve the os
317DIST_OS=`uname -s | $TREXE "[A-Z]" "[a-z]" | $TREXE -d ' '`
318case "$DIST_OS" in
319    'sunos')
320        DIST_OS="solaris"
321        ;;
322    'hp-ux' | 'hp-ux64')
323        # HP-UX needs the XPG4 version of ps (for -o args)
324        DIST_OS="hpux"
325        UNIX95=""
326        export UNIX95
327        ;;
328    'darwin')
329        DIST_OS="macosx"
330        ;;
331    'unix_sv')
332        DIST_OS="unixware"
333        ;;
334    'os/390')
335        DIST_OS="zos"
336        ;;
337esac
338
339# Resolve the architecture
340if [ "$DIST_OS" = "macosx" ]
341then
342    OS_VER=`sw_vers | grep 'ProductVersion:' | grep -o '[0-9]*\.[0-9]*\.[0-9]*'`
343    DIST_ARCH="universal"
344    DIST_BITS="64"
345    APP_PLIST_BASE=${PLIST_DOMAIN}.${APP_NAME}
346    APP_PLIST=${APP_PLIST_BASE}.plist
347else
348    DIST_ARCH=
349    DIST_ARCH=`uname -p 2>/dev/null | $TREXE "[A-Z]" "[a-z]" | $TREXE -d ' '`
350    if [ "X$DIST_ARCH" = "X" ]
351    then
352        DIST_ARCH="unknown"
353    fi
354    if [ "$DIST_ARCH" = "unknown" ]
355    then
356        DIST_ARCH=`uname -m 2>/dev/null | $TREXE "[A-Z]" "[a-z]" | $TREXE -d ' '`
357    fi
358    case "$DIST_ARCH" in
359        'athlon' | 'i386' | 'i486' | 'i586' | 'i686')
360            DIST_ARCH="x86"
361            if [ "${DIST_OS}" = "solaris" ] ; then
362                DIST_BITS=`isainfo -b`
363            else
364                DIST_BITS="32"
365            fi
366            ;;
367        'amd64' | 'x86_64')
368            DIST_ARCH="x86"
369            DIST_BITS="64"
370            ;;
371        'ia32')
372            DIST_ARCH="ia"
373            DIST_BITS="32"
374            ;;
375        'ia64' | 'ia64n' | 'ia64w')
376            DIST_ARCH="ia"
377            DIST_BITS="64"
378            ;;
379        'ip27')
380            DIST_ARCH="mips"
381            DIST_BITS="32"
382            ;;
383        'power' | 'powerpc' | 'power_pc' | 'ppc64')
384            if [ "${DIST_ARCH}" = "ppc64" ] ; then
385                DIST_BITS="64"
386            else
387                DIST_BITS="32"
388            fi
389            DIST_ARCH="ppc"
390            if [ "${DIST_OS}" = "aix" ] ; then
391                if [ `getconf KERNEL_BITMODE` -eq 64 ]; then
392                    DIST_BITS="64"
393                else
394                    DIST_BITS="32"
395                fi
396            fi
397            ;;
398        'pa_risc' | 'pa-risc')
399            DIST_ARCH="parisc"
400            if [ `getconf KERNEL_BITS` -eq 64 ]; then
401                DIST_BITS="64"
402            else
403                DIST_BITS="32"
404            fi
405            ;;
406        'sun4u' | 'sparcv9' | 'sparc')
407            DIST_ARCH="sparc"
408            DIST_BITS=`isainfo -b`
409            ;;
410        '9000/800' | '9000/785')
411            DIST_ARCH="parisc"
412            if [ `getconf KERNEL_BITS` -eq 64 ]; then
413                DIST_BITS="64"
414            else
415                DIST_BITS="32"
416            fi
417            ;;
418        '2064' | '2066' | '2084' | '2086' | '2094' | '2096' | '2097' | '2098' | '2817')
419            DIST_ARCH="390"
420            DIST_BITS="64"
421            ;;
422    esac
423fi
424
425# OSX always places Java in the same location so we can reliably set JAVA_HOME
426if [ "$DIST_OS" = "macosx" ]
427then
428    if [ -z "$JAVA_HOME" ]; then
429        JAVA_HOME="/Library/Java/Home"; export JAVA_HOME
430    fi
431fi
432
433# Test Echo
434ECHOTEST=`echo -n "x"`
435if [ "$ECHOTEST" = "x" ]
436then
437    ECHOOPT="-n "
438else
439    ECHOOPT=""
440fi
441
442
443gettext() {
444    "$WRAPPER_CMD" --translate "$1" "$WRAPPER_CONF" 2>/dev/null
445    if [ $? != 0 ] ; then
446        echo "$1"
447    fi
448}
449
450outputFile() {
451    if [ -f "$1" ]
452    then
453        eval echo `gettext '  $1  Found but not executable.'`;
454    else
455        echo "  $1"
456    fi
457}
458
459# Decide on the wrapper binary to use.
460# If the bits of the OS could be detected, we will try to look for the
461#  binary with the correct bits value.  If it doesn't exist, fall back
462#  and look for the 32-bit binary.  If that doesn't exist either then
463#  look for the default.
464WRAPPER_TEST_CMD=""
465if [ -f "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BITS" ]
466then
467    WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BITS"
468    if [ ! -x "$WRAPPER_TEST_CMD" ]
469    then
470        chmod +x "$WRAPPER_TEST_CMD" 2>/dev/null
471    fi
472    if [ -x "$WRAPPER_TEST_CMD" ]
473    then
474        WRAPPER_CMD="$WRAPPER_TEST_CMD"
475    else
476        outputFile "$WRAPPER_TEST_CMD"
477        WRAPPER_TEST_CMD=""
478    fi
479fi
480if [ -f "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32" -a -z "$WRAPPER_TEST_CMD" ]
481then
482    WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
483    if [ ! -x "$WRAPPER_TEST_CMD" ]
484    then
485        chmod +x "$WRAPPER_TEST_CMD" 2>/dev/null
486    fi
487    if [ -x "$WRAPPER_TEST_CMD" ]
488    then
489        WRAPPER_CMD="$WRAPPER_TEST_CMD"
490    else
491        outputFile "$WRAPPER_TEST_CMD"
492        WRAPPER_TEST_CMD=""
493    fi
494fi
495if [ -f "$WRAPPER_CMD" -a -z "$WRAPPER_TEST_CMD" ]
496then
497    WRAPPER_TEST_CMD="$WRAPPER_CMD"
498    if [ ! -x "$WRAPPER_TEST_CMD" ]
499    then
500        chmod +x "$WRAPPER_TEST_CMD" 2>/dev/null
501    fi
502    if [ -x "$WRAPPER_TEST_CMD" ]
503    then
504        WRAPPER_CMD="$WRAPPER_TEST_CMD"
505    else
506        outputFile "$WRAPPER_TEST_CMD"
507        WRAPPER_TEST_CMD=""
508    fi
509fi
510if [ -z "$WRAPPER_TEST_CMD" ]
511then
512    eval echo `gettext 'Unable to locate any of the following binaries:'`
513    outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BITS"
514    if [ ! "$DIST_BITS" = "32" ]
515    then
516        outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
517    fi
518    outputFile "$WRAPPER_CMD"
519
520    exit 1
521fi
522
523
524# Build the nice clause
525if [ "X$PRIORITY" = "X" ]
526then
527    CMDNICE=""
528else
529    CMDNICE="nice -$PRIORITY"
530fi
531
532# Build the anchor file clause.
533if [ "X$IGNORE_SIGNALS" = "X" ]
534then
535   ANCHORPROP=
536   IGNOREPROP=
537else
538   ANCHORPROP=wrapper.anchorfile=\"$ANCHORFILE\"
539   IGNOREPROP=wrapper.ignore_signals=TRUE
540fi
541
542# Build the status file clause.
543if [ "X$DETAIL_STATUS$WAIT_FOR_STARTED_STATUS" = "X" ]
544then
545   STATUSPROP=
546else
547   STATUSPROP="wrapper.statusfile=\"$STATUSFILE\" wrapper.java.statusfile=\"$JAVASTATUSFILE\""
548fi
549
550# Build the command file clause.
551if [ -n "$PAUSABLE" ]
552then
553   COMMANDPROP="wrapper.commandfile=\"$COMMANDFILE\" wrapper.pausable=TRUE"
554else
555   COMMANDPROP=
556fi
557
558if [ ! -n "$WAIT_FOR_STARTED_STATUS" ]
559then
560    WAIT_FOR_STARTED_STATUS=true
561fi
562
563if [ $WAIT_FOR_STARTED_STATUS = true ] ; then
564    DETAIL_STATUS=true
565fi
566
567
568# Build the lock file clause.  Only create a lock file if the lock directory exists on this platform.
569LOCKPROP=
570if [ -d $LOCKDIR ]
571then
572    if [ -w $LOCKDIR ]
573    then
574        LOCKPROP=wrapper.lockfile=\"$LOCKFILE\"
575    fi
576fi
577
578prepAdditionalParams() {
579    ADDITIONAL_PARA=""
580    if [ -n "$PASS_THROUGH" ] ; then
581        ADDITIONAL_PARA="--"
582    fi
583    while [ -n "$1" ] ; do
584        ADDITIONAL_PARA="$ADDITIONAL_PARA \"$1\""
585        shift
586    done
587}
588
589checkUser() {
590    # $1 touchLock flag
591    # $2.. [command] args
592
593    # Check the configured user.  If necessary rerun this script as the desired user.
594    if [ "X$RUN_AS_USER" != "X" ]
595    then
596        # Resolve the location of the 'id' command
597        IDEXE="/usr/xpg4/bin/id"
598        if [ ! -x "$IDEXE" ]
599        then
600            IDEXE="/usr/bin/id"
601            if [ ! -x "$IDEXE" ]
602            then
603                eval echo `gettext 'Unable to locate "id".'`
604                eval echo `gettext 'Please report this message along with the location of the command on your system.'`
605                exit 1
606            fi
607        fi
608
609        if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ]
610        then
611            # Already running as the configured user.  Avoid password prompts by not calling su.
612            RUN_AS_USER=""
613        fi
614    fi
615    if [ "X$RUN_AS_USER" != "X" ]
616    then
617        # If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be
618        # able to create the lock file.  The Wrapper will be able to update this file once it
619        # is created but will not be able to delete it on shutdown.  If $1 is set then
620        # the lock file should be created for the current command
621        if [ "X$LOCKPROP" != "X" ]
622        then
623            if [ "X$1" != "X" ]
624            then
625                # Resolve the primary group
626                RUN_AS_GROUP=`groups $RUN_AS_USER | awk '{print $3}' | tail -1`
627                if [ "X$RUN_AS_GROUP" = "X" ]
628                then
629                    RUN_AS_GROUP=$RUN_AS_USER
630                fi
631                touch $LOCKFILE
632                chown $RUN_AS_USER:$RUN_AS_GROUP $LOCKFILE
633            fi
634        fi
635
636        # Still want to change users, recurse.  This means that the user will only be
637        #  prompted for a password once. Variables shifted by 1
638        shift
639
640        # Wrap the parameters so they can be passed.
641        ADDITIONAL_PARA=""
642        while [ -n "$1" ] ; do
643            ADDITIONAL_PARA="$ADDITIONAL_PARA \"$1\""
644            shift
645        done
646
647        # Use "runuser" if this exists.  runuser should be used on RedHat in preference to su.
648        #
649        if test -f "/sbin/runuser"
650        then
651            /sbin/runuser - $RUN_AS_USER -c "\"$REALPATH\" $ADDITIONAL_PARA"
652        else
653            su - $RUN_AS_USER -c "\"$REALPATH\" $ADDITIONAL_PARA"
654        fi
655        RUN_AS_USER_EXITCODE=$?
656        # Now that we are the original user again, we may need to clean up the lock file.
657        if [ "X$LOCKPROP" != "X" ]
658        then
659            getpid
660            if [ "X$pid" = "X" ]
661            then
662                # Wrapper is not running so make sure the lock file is deleted.
663                if [ -f "$LOCKFILE" ]
664                then
665                    rm "$LOCKFILE"
666                fi
667            fi
668        fi
669
670        exit $RUN_AS_USER_EXITCODE
671    fi
672}
673
674getpid() {
675    pid=""
676    if [ -f "$PIDFILE" ]
677    then
678        if [ -r "$PIDFILE" ]
679        then
680            pid=`cat "$PIDFILE"`
681            if [ "X$pid" != "X" ]
682            then
683                # It is possible that 'a' process with the pid exists but that it is not the
684                #  correct process.  This can happen in a number of cases, but the most
685                #  common is during system startup after an unclean shutdown.
686                # The ps statement below looks for the specific wrapper command running as
687                #  the pid.  If it is not found then the pid file is considered to be stale.
688                case "$DIST_OS" in
689                    'freebsd')
690                        pidtest=`$PSEXE -p $pid -o args | tail -1`
691                        if [ "X$pidtest" = "XCOMMAND" ]
692                        then
693                            pidtest=""
694                        fi
695                        ;;
696                    'macosx')
697                        pidtest=`$PSEXE -ww -p $pid -o command | grep -F "$WRAPPER_CMD" | tail -1`
698                        ;;
699                    'solaris')
700                        if [ -f "/usr/bin/pargs" ]
701                        then
702                            pidtest=`pargs $pid | fgrep "$WRAPPER_CMD" | tail -1`
703                        else
704                            case "$PSEXE" in
705                            '/usr/ucb/ps')
706                                pidtest=`$PSEXE -auxww  $pid | fgrep "$WRAPPER_CMD" | tail -1`
707                                ;;
708                            '/usr/bin/ps')
709                                TRUNCATED_CMD=`$PSEXE -o comm -p $pid | tail -1`
710                                COUNT=`echo $TRUNCATED_CMD | wc -m`
711                                COUNT=`echo ${COUNT}`
712                                COUNT=`expr $COUNT - 1`
713                                TRUNCATED_CMD=`echo $WRAPPER_CMD | cut -c1-$COUNT`
714                                pidtest=`$PSEXE -o comm -p $pid | fgrep "$TRUNCATED_CMD" | tail -1`
715                                ;;
716                            '/bin/ps')
717                                TRUNCATED_CMD=`$PSEXE -o comm -p $pid | tail -1`
718                                COUNT=`echo $TRUNCATED_CMD | wc -m`
719                                COUNT=`echo ${COUNT}`
720                                COUNT=`expr $COUNT - 1`
721                                TRUNCATED_CMD=`echo $WRAPPER_CMD | cut -c1-$COUNT`
722                                pidtest=`$PSEXE -o comm -p $pid | fgrep "$TRUNCATED_CMD" | tail -1`
723                                ;;
724                            *)
725                                echo "Unsupported ps command $PSEXE"
726                                exit 1
727                                ;;
728                            esac
729                        fi
730                        ;;
731                    'hpux')
732                        pidtest=`$PSEXE -p $pid -x -o args | grep -F "$WRAPPER_CMD" | tail -1`
733                        ;;
734                    *)
735                        pidtest=`$PSEXE -p $pid -o args | grep -F "$WRAPPER_CMD" | tail -1`
736                        ;;
737                esac
738
739                if [ "X$pidtest" = "X" ]
740                then
741                    # This is a stale pid file.
742                    rm -f "$PIDFILE"
743                    eval echo `gettext 'Removed stale pid file: $PIDFILE'`
744                    pid=""
745                fi
746            fi
747        else
748            eval echo `gettext 'Cannot read $PIDFILE.'`
749            exit 1
750        fi
751    fi
752}
753
754getstatus() {
755    STATUS=
756    if [ -f "$STATUSFILE" ]
757    then
758        if [ -r "$STATUSFILE" ]
759        then
760            STATUS=`cat "$STATUSFILE"`
761        fi
762    fi
763    if [ "X$STATUS" = "X" ]
764    then
765        STATUS="Unknown"
766    fi
767
768    JAVASTATUS=
769    if [ -f "$JAVASTATUSFILE" ]
770    then
771        if [ -r "$JAVASTATUSFILE" ]
772        then
773            JAVASTATUS=`cat "$JAVASTATUSFILE"`
774        fi
775    fi
776    if [ "X$JAVASTATUS" = "X" ]
777    then
778        JAVASTATUS="Unknown"
779    fi
780}
781
782testpid() {
783    case "$DIST_OS" in
784     'solaris')
785        case "$PSEXE" in
786        '/usr/ucb/ps')
787            pid=`$PSEXE  $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
788            ;;
789        '/usr/bin/ps')
790            pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
791            ;;
792        '/bin/ps')
793            pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
794            ;;
795        *)
796            echo "Unsupported ps command $PSEXE"
797            exit 1
798            ;;
799        esac
800        ;;
801    *)
802        pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1` 2>/dev/null
803        ;;
804    esac
805    if [ "X$pid" = "X" ]
806    then
807        # Process is gone so remove the pid file.
808        rm -f "$PIDFILE"
809        pid=""
810    fi
811}
812
813launchdtrap() {
814    stopit
815    exit
816}
817
818waitforwrapperstop() {
819    getpid
820    while [ "X$pid" != "X" ] ; do
821        sleep 1
822        getpid
823    done
824}
825
826launchdinternal() {
827    getpid
828    trap launchdtrap TERM
829    if [ "X$pid" = "X" ]
830    then
831        prepAdditionalParams "$@"
832
833        # The string passed to eval must handles spaces in paths correctly.
834        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"
835        eval $COMMAND_LINE
836    else
837        eval echo `gettext '$APP_LONG_NAME is already running.'`
838        exit 1
839    fi
840    # launchd expects that this script stay up and running so we need to do our own monitoring of the Wrapper process.
841    if [ $WAIT_FOR_STARTED_STATUS = true ]
842    then
843        waitforwrapperstop
844    fi
845}
846
847console() {
848    eval echo `gettext 'Running $APP_LONG_NAME...'`
849    getpid
850    if [ "X$pid" = "X" ]
851    then
852        trap '' 3 2
853
854        prepAdditionalParams "$@"
855
856        # The string passed to eval must handles spaces in paths correctly.
857        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"
858        eval $COMMAND_LINE
859        if [ "$?" -ne "0" ]; then
860                failed
861        fi
862    else
863        eval echo `gettext '$APP_LONG_NAME is already running.'`
864        exit 1
865    fi
866}
867
868waitforjavastartup() {
869    getstatus
870    eval echo $ECHOOPT `gettext 'Waiting for $APP_LONG_NAME...'`
871
872    # Wait until the timeout or we have something besides Unknown.
873    counter=15
874    while [ "$JAVASTATUS" = "Unknown" -a $counter -gt 0 -a -n "$JAVASTATUS" ] ; do
875        echo $ECHOOPT"."
876        sleep 1
877        getstatus
878        counter=`expr $counter - 1`
879    done
880
881    if [ -n "$WAIT_FOR_STARTED_TIMEOUT" ] ; then
882        counter=$WAIT_FOR_STARTED_TIMEOUT
883    else
884        counter=120
885    fi
886    while [ "$JAVASTATUS" != "STARTED" -a "$JAVASTATUS" != "Unknown" -a $counter -gt 0 -a -n "$JAVASTATUS" ] ; do
887        echo $ECHOOPT"."
888        sleep 1
889        getstatus
890        counter=`expr $counter - 1`
891    done
892    if [ "X$ECHOOPT" != "X" ] ; then
893        echo ""
894    fi
895}
896
897startwait() {
898    if [ $WAIT_FOR_STARTED_STATUS = true ]
899    then
900        waitforjavastartup
901    fi
902    # Sleep for a few seconds to allow for intialization if required
903    #  then test to make sure we're still running.
904    #
905    i=0
906    while [ $i -lt $WAIT_AFTER_STARTUP ]
907    do
908        sleep 1
909        echo $ECHOOPT"."
910        i=`expr $i + 1`
911    done
912    if [ $WAIT_AFTER_STARTUP -gt 0 -o $WAIT_FOR_STARTED_STATUS = true ]
913    then
914        getpid
915        if [ "X$pid" = "X" ]
916        then
917            eval echo `gettext ' WARNING: $APP_LONG_NAME may have failed to start.'`
918            exit 1
919        else
920            eval echo `gettext ' running: PID:$pid'`
921        fi
922    else
923        echo ""
924    fi
925}
926
927macosxstart() {
928    # The daemon has been installed.
929    eval echo `gettext 'Starting $APP_LONG_NAME.  Detected Mac OSX and installed launchd daemon.'`
930    if [ `id | sed 's/^uid=//;s/(.*$//'` != "0" ] ; then
931        eval echo `gettext 'Must be root to perform this action.'`
932        exit 1
933    fi
934
935    getpid
936    if [ "X$pid" != "X" ] ; then
937        eval echo `gettext '$APP_LONG_NAME is already running.'`
938        exit 1
939    fi
940
941    # If the daemon was just installed, it may not be loaded.
942    LOADED_PLIST=`launchctl list | grep ${APP_PLIST_BASE}`
943    if [ "X${LOADED_PLIST}" = "X" ] ; then
944        launchctl load /Library/LaunchDaemons/${APP_PLIST}
945    fi
946    # If launchd is set to run the daemon already at Load, we don't need to call start
947    getpid
948    if [ "X$pid" = "X" ] ; then
949        launchctl start ${APP_PLIST_BASE}
950    fi
951
952    startwait
953}
954
955upstartstart() {
956    # The daemon has been installed.
957    eval echo `gettext 'Starting $APP_LONG_NAME.  Detected Linux and installed upstart.'`
958    if [ `id | sed 's/^uid=//;s/(.*$//'` != "0" ] ; then
959        eval echo `gettext 'Must be root to perform this action.'`
960        exit 1
961    fi
962
963    getpid
964    if [ "X$pid" != "X" ] ; then
965        eval echo `gettext '$APP_LONG_NAME is already running.'`
966        exit 1
967    fi
968
969    /sbin/start ${APP_NAME}
970
971    startwait
972}
973
974start() {
975    eval echo `gettext 'Starting $APP_LONG_NAME...'`
976    getpid
977    if [ "X$pid" = "X" ]
978    then
979        prepAdditionalParams "$@"
980
981        # The string passed to eval must handles spaces in paths correctly.
982        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\" wrapper.daemonize=TRUE $ANCHORPROP $IGNOREPROP $STATUSPROP $COMMANDPROP $LOCKPROP $ADDITIONAL_PARA"
983        eval $COMMAND_LINE
984        if [ "$?" -ne "0" ]; then
985                failed
986        fi
987    else
988        eval echo `gettext '$APP_LONG_NAME is already running.'`
989        exit 1
990    fi
991
992    startwait
993}
994
995stopit() {
996    # $1 exit if down flag
997
998    eval echo `gettext 'Stopping $APP_LONG_NAME...'`
999    getpid
1000    if [ "X$pid" = "X" ]
1001    then
1002        eval echo `gettext '$APP_LONG_NAME was not running.'`
1003        if [ "X$1" = "X1" ]
1004        then
1005            exit 1
1006        fi
1007    else
1008        if [ "X$IGNORE_SIGNALS" = "X" ]
1009        then
1010            # Running so try to stop it.
1011            kill $pid
1012            if [ $? -ne 0 ]
1013            then
1014                # An explanation for the failure should have been given
1015                eval echo `gettext 'Unable to stop $APP_LONG_NAME.'`
1016                exit 1
1017            fi
1018        else
1019            rm -f "$ANCHORFILE"
1020            if [ -f "$ANCHORFILE" ]
1021            then
1022                # An explanation for the failure should have been given
1023                eval echo `gettext 'Unable to stop $APP_LONG_NAME.'`
1024                exit 1
1025            fi
1026        fi
1027
1028        # We can not predict how long it will take for the wrapper to
1029        #  actually stop as it depends on settings in wrapper.conf.
1030        #  Loop until it does.
1031        savepid=$pid
1032        CNT=0
1033        TOTCNT=0
1034        while [ "X$pid" != "X" ]
1035        do
1036            # Show a waiting message every 5 seconds.
1037            if [ "$CNT" -lt "5" ]
1038            then
1039                CNT=`expr $CNT + 1`
1040            else
1041                eval echo `gettext 'Waiting for $APP_LONG_NAME to exit...'`
1042                CNT=0
1043            fi
1044            TOTCNT=`expr $TOTCNT + 1`
1045
1046            sleep 1
1047
1048            testpid
1049        done
1050
1051        pid=$savepid
1052        testpid
1053        if [ "X$pid" != "X" ]
1054        then
1055            eval echo `gettext 'Failed to stop $APP_LONG_NAME.'`
1056            exit 1
1057        else
1058            eval echo `gettext 'Stopped $APP_LONG_NAME.'`
1059        fi
1060    fi
1061}
1062
1063pause() {
1064    eval echo `gettext 'Pausing $APP_LONG_NAME.'`
1065}
1066
1067resume() {
1068    eval echo `gettext 'Resuming $APP_LONG_NAME.'`
1069}
1070
1071status() {
1072    getpid
1073    if [ "X$pid" = "X" ]
1074    then
1075        eval echo `gettext '$APP_LONG_NAME is not running.'`
1076        exit 1
1077    else
1078        if [ "X$DETAIL_STATUS" = "X" ]
1079        then
1080            eval echo `gettext '$APP_LONG_NAME is running: PID:$pid'`
1081        else
1082            getstatus
1083            eval echo `gettext '$APP_LONG_NAME is running: PID:$pid, Wrapper:$STATUS, Java:$JAVASTATUS'`
1084        fi
1085        exit 0
1086    fi
1087}
1088
1089installUpstart() {
1090    eval echo `gettext ' Installing the $APP_LONG_NAME daemon using upstart..'`
1091    if [ -f "${APP_NAME}.conf" ] ; then
1092        eval echo `gettext ' a custom upstart conf file ${APP_NAME}.conf found'`
1093        cp "${REALDIR}/${APP_NAME}.install" "/etc/init/${APP_NAME}.conf"
1094    else
1095        eval echo `gettext ' creating default upstart conf file..'`
1096        echo "# ${APP_NAME} - ${APP_LONG_NAME}" > "/etc/init/${APP_NAME}.conf"
1097        echo "description \"${APP_LONG_NAME}\"" >> "/etc/init/${APP_NAME}.conf"
1098        echo "author \"Tanuki Software Ltd. <info@tanukisoftware.com>\"" >> "/etc/init/${APP_NAME}.conf"
1099        echo "start on runlevel [2345]" >> "/etc/init/${APP_NAME}.conf"
1100        echo "stop on runlevel [!2345]" >> "/etc/init/${APP_NAME}.conf"
1101        echo "env LANG=${LANG}" >> "/etc/init/${APP_NAME}.conf"
1102       echo "exec \"${REALPATH}\" launchdinternal" >> "/etc/init/${APP_NAME}.conf"
1103    fi
1104}
1105
1106installdaemon() {
1107    if [ `id | sed 's/^uid=//;s/(.*$//'` != "0" ] ; then
1108        eval echo `gettext 'Must be root to perform this action.'`
1109        exit 1
1110    else
1111        APP_NAME_LOWER=`echo "$APP_NAME" | $TREXE "[A-Z]" "[a-z]"`
1112        if [ "$DIST_OS" = "solaris" ] ; then
1113            eval echo `gettext 'Detected Solaris:'`
1114            if [ -f /etc/init.d/$APP_NAME ] ; then
1115                eval echo `gettext ' The $APP_LONG_NAME daemon is already installed.'`
1116                exit 1
1117            else
1118                eval echo `gettext ' Installing the $APP_LONG_NAME daemon..'`
1119                ln -s "$REALPATH" "/etc/init.d/$APP_NAME"
1120                ln -s "/etc/init.d/$APP_NAME" "/etc/rc3.d/K20$APP_NAME_LOWER"
1121                ln -s "/etc/init.d/$APP_NAME" "/etc/rc3.d/S20$APP_NAME_LOWER"
1122            fi
1123        elif [ "$DIST_OS" = "linux" ] ; then
1124            if [ -f /etc/redhat-release -o -f /etc/redhat_version -o -f /etc/fedora-release ]  ; then
1125                eval echo `gettext 'Detected RHEL or Fedora:'`
1126                if [ -f "/etc/init.d/$APP_NAME" -o -f "/etc/init/${APP_NAME}.conf" ] ; then
1127                    eval echo `gettext ' The $APP_LONG_NAME daemon is already installed.'`
1128                    exit 1
1129                else
1130                    if [ -n "$USE_UPSTART" -a -d "/etc/init" ] ; then
1131                        installUpstart
1132                    else
1133                        eval echo `gettext ' Installing the $APP_LONG_NAME daemon..'`
1134                        ln -s "$REALPATH" "/etc/init.d/$APP_NAME"
1135                        /sbin/chkconfig --add "$APP_NAME"
1136                        /sbin/chkconfig "$APP_NAME" on
1137                    fi
1138                fi
1139            elif [ -f /etc/arch-release ]; then
1140                eval echo `gettext 'Detected Arch Linux:'`
1141                eval echo `gettext 'Installing as a daemon is not yet supported in Arch Linux.'`
1142                exit 1
1143            elif [ -f /etc/SuSE-release ] ; then
1144                eval echo `gettext 'Detected SuSE or SLES:'`
1145                if [ -f "/etc/init.d/$APP_NAME" ] ; then
1146                    eval echo `gettext ' The $APP_LONG_NAME daemon is already installed.'`
1147                    exit 1
1148                else
1149                    eval echo `gettext ' Installing the $APP_LONG_NAME daemon..'`
1150                    ln -s "$REALPATH" "/etc/init.d/$APP_NAME"
1151                    sed -i "s/Default-Start: 2 3 4 5/Default-Start: 5/" $0
1152                    insserv "/etc/init.d/$APP_NAME"
1153                fi
1154            elif [ -f /etc/lsb-release -o -f /etc/debian_version ] ; then
1155                eval echo `gettext 'Detected Debian-based distribution:'`
1156                if [ -f "/etc/init.d/$APP_NAME" -o -f "/etc/init/${APP_NAME}.conf" ] ; then
1157                    eval echo `gettext ' The $APP_LONG_NAME daemon is already installed.'`
1158                    exit 1
1159                else
1160                    if [ -n "$USE_UPSTART" -a -d "/etc/init" ] ; then
1161                        installUpstart
1162                    else
1163                        eval echo `gettext ' Installing the $APP_LONG_NAME daemon using init.d..'`
1164                        ln -s "$REALPATH" "/etc/init.d/$APP_NAME"
1165                        update-rc.d "$APP_NAME" defaults
1166                    fi
1167                fi
1168            else
1169                eval echo `gettext 'Detected Linux:'`
1170                if [ -f "/etc/init.d/$APP_NAME" ] ; then
1171                    eval echo `gettext ' The $APP_LONG_NAME daemon is already installed.'`
1172                    exit 1
1173                else
1174                    eval echo `gettext ' Installing the $APP_LONG_NAME daemon..'`
1175                    ln -s "$REALPATH" /etc/init.d/$APP_NAME
1176                    ln -s "/etc/init.d/$APP_NAME" "/etc/rc3.d/K20$APP_NAME_LOWER"
1177                    ln -s "/etc/init.d/$APP_NAME" "/etc/rc3.d/S20$APP_NAME_LOWER"
1178                    ln -s "/etc/init.d/$APP_NAME" "/etc/rc5.d/S20$APP_NAME_LOWER"
1179                    ln -s "/etc/init.d/$APP_NAME" "/etc/rc5.d/K20$APP_NAME_LOWER"
1180                fi
1181            fi
1182        elif [ "$DIST_OS" = "hpux" ] ; then
1183            eval echo `gettext 'Detected HP-UX:'`
1184            if [ -f "/sbin/init.d/$APP_NAME" ] ; then
1185                eval echo `gettext ' The $APP_LONG_NAME daemon is already installed.'`
1186                exit 1
1187            else
1188                eval echo `gettext ' Installing the $APP_LONG_NAME daemon..'`
1189                ln -s "$REALPATH" "/sbin/init.d/$APP_NAME"
1190                ln -s "/sbin/init.d/$APP_NAME" "/sbin/rc3.d/K20$APP_NAME_LOWER"
1191                ln -s "/sbin/init.d/$APP_NAME" "/sbin/rc3.d/S20$APP_NAME_LOWER"
1192            fi
1193        elif [ "$DIST_OS" = "aix" ] ; then
1194            eval echo `gettext 'Detected AIX:'`
1195            if [ -f "/etc/rc.d/init.d/$APP_NAME" ] ; then
1196                eval echo `gettext ' The $APP_LONG_NAME daemon is already installed as rc.d script.'`
1197                exit 1
1198            elif [ -n "`/usr/sbin/lsitab $APP_NAME`" -a -n "`/usr/bin/lssrc -S -s $APP_NAME`" ] ; then
1199                eval echo `gettext ' The $APP_LONG_NAME daemon is already installed as SRC service.'`
1200                exit 1
1201            else
1202                eval echo `gettext ' Installing the $APP_LONG_NAME daemon..'`
1203                if [ -n "`/usr/sbin/lsitab install_assist`" ] ; then
1204                    eval echo `gettext ' The task /usr/sbin/install_assist was found in the inittab, this might cause problems for all subsequent tasks to launch at this process is known to block the init task. Please make sure this task is not needed anymore and remove/deactivate it.'`
1205                fi
1206                /usr/bin/mkssys -s "$APP_NAME" -p "$REALPATH" -a "launchdinternal" -u 0 -f 9 -n 15 -S
1207                /usr/sbin/mkitab "$APP_NAME":2:once:"/usr/bin/startsrc -s \"${APP_NAME}\" >/dev/console 2>&1"
1208
1209            fi
1210        elif [ "$DIST_OS" = "freebsd" ] ; then
1211            eval echo `gettext 'Detected FreeBSD:'`
1212            if [ -f "/etc/rc.d/$APP_NAME" ] ; then
1213                eval echo `gettext ' The $APP_LONG_NAME daemon is already installed.'`
1214                exit 1
1215            else
1216                eval echo `gettext ' Installing the $APP_LONG_NAME daemon..'`
1217                sed -i .bak "/${APP_NAME}_enable=\"YES\"/d" /etc/rc.conf
1218                if [ -f "${REALDIR}/${APP_NAME}.install" ] ; then
1219                    ln -s "${REALDIR}/${APP_NAME}.install" "/etc/rc.d/$APP_NAME"
1220                else
1221                    echo '#!/bin/sh' > "/etc/rc.d/$APP_NAME"
1222                    echo "#" >> "/etc/rc.d/$APP_NAME"
1223                    echo "# PROVIDE: $APP_NAME" >> "/etc/rc.d/$APP_NAME"
1224                    echo "# REQUIRE: NETWORKING" >> "/etc/rc.d/$APP_NAME"
1225                    echo "# KEYWORD: shutdown" >> "/etc/rc.d/$APP_NAME"
1226                    echo ". /etc/rc.subr" >> "/etc/rc.d/$APP_NAME"
1227                    echo "name=\"$APP_NAME\"" >> "/etc/rc.d/$APP_NAME"
1228                    echo "rcvar=\`set_rcvar\`" >> "/etc/rc.d/$APP_NAME"
1229                    echo "command=\"${REALDIR}/${APP_NAME}router\"" >> "/etc/rc.d/$APP_NAME"
1230                    echo 'start_cmd="${name}_start"' >> "/etc/rc.d/$APP_NAME"
1231                    echo 'load_rc_config $name' >> "/etc/rc.d/$APP_NAME"
1232                    echo 'status_cmd="${name}_status"' >> "/etc/rc.d/$APP_NAME"
1233                    echo 'stop_cmd="${name}_stop"' >> "/etc/rc.d/$APP_NAME"
1234                    echo "${APP_NAME}_status() {" >> "/etc/rc.d/$APP_NAME"
1235                    echo '${command} status' >> "/etc/rc.d/$APP_NAME"
1236                    echo '}' >> "/etc/rc.d/$APP_NAME"
1237                    echo "${APP_NAME}_stop() {" >> "/etc/rc.d/$APP_NAME"
1238                    echo '${command} stop' >> "/etc/rc.d/$APP_NAME"
1239                    echo '}' >> "/etc/rc.d/$APP_NAME"
1240                    echo "${APP_NAME}_start() {" >> "/etc/rc.d/$APP_NAME"
1241                    echo '${command} start' >> "/etc/rc.d/$APP_NAME"
1242                    echo '}' >> "/etc/rc.d/$APP_NAME"
1243                    echo 'run_rc_command "$1"' >> "/etc/rc.d/$APP_NAME"
1244                fi
1245                echo "${APP_NAME}_enable=\"YES\"" >> /etc/rc.conf
1246                chmod 555 "/etc/rc.d/$APP_NAME"
1247            fi
1248        elif [ "$DIST_OS" = "macosx" ] ; then
1249            eval echo `gettext 'Detected Mac OSX:'`
1250            if [ -f "/Library/LaunchDaemons/${APP_PLIST}" ] ; then
1251                eval echo `gettext ' The $APP_LONG_NAME daemon is already installed.'`
1252                exit 1
1253            else
1254                eval echo `gettext ' Installing the $APP_LONG_NAME daemon..'`
1255                if [ -f "${REALDIR}/${APP_PLIST}" ] ; then
1256                    ln -s "${REALDIR}/${APP_PLIST}" "/Library/LaunchDaemons/${APP_PLIST}"
1257                else
1258                    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > "/Library/LaunchDaemons/${APP_PLIST}"
1259                    echo "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\"" >> "/Library/LaunchDaemons/${APP_PLIST}"
1260                    echo "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" >> "/Library/LaunchDaemons/${APP_PLIST}"
1261                    echo "<plist version=\"1.0\">" >> "/Library/LaunchDaemons/${APP_PLIST}"
1262                    echo "    <dict>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1263                    echo "        <key>Label</key>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1264                    echo "        <string>${APP_PLIST_BASE}</string>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1265                    echo "        <key>ProgramArguments</key>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1266                    echo "        <array>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1267                    echo "            <string>${REALDIR}/${APP_NAME}router</string>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1268                    echo "            <string>launchdinternal</string>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1269                    echo "        </array>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1270                    echo "        <key>OnDemand</key>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1271                    echo "        <true/>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1272                    echo "        <key>RunAtLoad</key>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1273                    echo "        <true/>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1274                    if [ "X$RUN_AS_USER" != "X" ] ; then
1275                        echo "        <key>UserName</key>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1276                        echo "        <string>${RUN_AS_USER}</string>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1277                    fi
1278                    echo "    </dict>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1279                    echo "</plist>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1280                fi
1281                chmod 555 "/Library/LaunchDaemons/${APP_PLIST}"
1282            fi
1283        elif [ "$DIST_OS" = "zos" ] ; then
1284            eval echo `gettext 'Detected z/OS:'`
1285            if [ -f /etc/rc.bak ] ; then
1286                eval echo `gettext ' The $APP_LONG_NAME daemon is already installed.'`
1287                exit 1
1288            else
1289                eval echo `gettext ' Installing the $APP_LONG_NAME daemon..'`
1290                cp /etc/rc /etc/rc.bak
1291                sed  "s:echo /etc/rc script executed, \`date\`::g" /etc/rc.bak > /etc/rc
1292                echo "_BPX_JOBNAME='${APP_NAME}' \"${REALDIR}/${APP_NAME}router\" start" >>/etc/rc
1293                echo '/etc/rc script executed, `date`' >>/etc/rc
1294            fi
1295        else
1296            eval echo `gettext 'Install not currently supported for $DIST_OS'`
1297            exit 1
1298        fi
1299    fi
1300}
1301
1302removedaemon() {
1303    if [ `id | sed 's/^uid=//;s/(.*$//'` != "0" ] ; then
1304        eval echo `gettext 'Must be root to perform this action.'`
1305        exit 1
1306    else
1307        stopit "0"
1308        APP_NAME_LOWER=`echo "$APP_NAME" | $TREXE "[A-Z]" "[a-z]"`
1309        if [ "$DIST_OS" = "solaris" ] ; then
1310            eval echo `gettext 'Detected Solaris:'`
1311            if [ -f "/etc/init.d/$APP_NAME" ] ; then
1312                eval echo `gettext ' Removing $APP_LONG_NAME daemon...'`
1313                for i in "/etc/rc3.d/S20$APP_NAME_LOWER" "/etc/rc3.d/K20$APP_NAME_LOWER" "/etc/init.d/$APP_NAME"
1314                do
1315                    rm -f $i
1316                done
1317            else
1318                eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
1319                exit 1
1320            fi
1321        elif [ "$DIST_OS" = "linux" ] ; then
1322            if [ -f /etc/redhat-release -o -f /etc/redhat_version -o -f /etc/fedora-release ] ; then
1323                eval echo `gettext 'Detected RHEL or Fedora:'`
1324                if [ -f "/etc/init.d/$APP_NAME" ] ; then
1325                    eval echo `gettext ' Removing $APP_LONG_NAME daemon...'`
1326                    /sbin/chkconfig "$APP_NAME" off
1327                    /sbin/chkconfig --del "$APP_NAME"
1328                    rm -f "/etc/init.d/$APP_NAME"
1329                elif [ -f "/etc/init/${APP_NAME}.conf" ] ; then
1330                    eval echo `gettext ' Removing $APP_LONG_NAME daemon from upstart...'`
1331                    rm "/etc/init/${APP_NAME}.conf"
1332                else
1333                    eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
1334                    exit 1
1335                fi
1336            elif [ -f /etc/SuSE-release ] ; then
1337                eval echo `gettext 'Detected SuSE or SLES:'`
1338                if [ -f "/etc/init.d/$APP_NAME" ] ; then
1339                    eval echo `gettext ' Removing $APP_LONG_NAME daemon...'`
1340                    insserv -r "/etc/init.d/$APP_NAME"
1341                    rm -f "/etc/init.d/$APP_NAME"
1342                else
1343                    eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
1344                    exit 1
1345                fi
1346            elif [ -f /etc/lsb-release -o -f /etc/debian_version ] ; then
1347                eval echo `gettext 'Detected Debian-based distribution:'`
1348                if [ -f "/etc/init.d/$APP_NAME" ] ; then
1349                    eval echo `gettext ' Removing $APP_LONG_NAME daemon from init.d...'`
1350                    update-rc.d -f "$APP_NAME" remove
1351                    rm -f "/etc/init.d/$APP_NAME"
1352                elif [ -f "/etc/init/${APP_NAME}.conf" ] ; then
1353                    eval echo `gettext ' Removing $APP_LONG_NAME daemon from upstart...'`
1354                    rm "/etc/init/${APP_NAME}.conf"
1355                else
1356                    eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
1357                    exit 1
1358                fi
1359            else
1360                eval echo `gettext 'Detected Linux:'`
1361                if [ -f "/etc/init.d/$APP_NAME" ] ; then
1362                    eval echo `gettext ' Removing $APP_LONG_NAME daemon...'`
1363                    for i in "/etc/rc3.d/K20$APP_NAME_LOWER" "/etc/rc5.d/K20$APP_NAME_LOWER" "/etc/rc3.d/S20$APP_NAME_LOWER" "/etc/init.d/$APP_NAME" "/etc/rc5.d/S20$APP_NAME_LOWER"
1364                    do
1365                        rm -f $i
1366                    done
1367                else
1368                    eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
1369                    exit 1
1370                fi
1371            fi
1372        elif [ "$DIST_OS" = "hpux" ] ; then
1373            eval echo `gettext 'Detected HP-UX:'`
1374            if [ -f "/sbin/init.d/$APP_NAME" ] ; then
1375                eval echo `gettext ' Removing $APP_LONG_NAME daemon...'`
1376                for i in "/sbin/rc3.d/K20$APP_NAME_LOWER" "/sbin/rc3.d/S20$APP_NAME_LOWER" "/sbin/init.d/$APP_NAME"
1377                do
1378                    rm -f $i
1379                done
1380            else
1381                eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
1382                exit 1
1383            fi
1384        elif [ "$DIST_OS" = "aix" ] ; then
1385            eval echo `gettext 'Detected AIX:'`
1386            if [ -f "/etc/rc.d/init.d/$APP_NAME" -o -n "`/usr/sbin/lsitab $APP_NAME`" -o -n "`/usr/bin/lssrc -S -s $APP_NAME`" ] ; then
1387                eval echo `gettext ' Removing $APP_LONG_NAME daemon...'`
1388                if [ -f "/etc/rc.d/init.d/$APP_NAME" ] ; then
1389                    for i in "/etc/rc.d/rc2.d/S20$APP_NAME_LOWER" "/etc/rc.d/rc2.d/K20$APP_NAME_LOWER" "/etc/rc.d/init.d/$APP_NAME"
1390                    do
1391                        rm -f $i
1392                    done
1393                fi
1394                if [ -n "`/usr/sbin/lsitab $APP_NAME`" -o -n "`/usr/bin/lssrc -S -s $APP_NAME`" ] ; then
1395                    /usr/sbin/rmitab $APP_NAME
1396                    /usr/bin/rmssys -s $APP_NAME
1397                fi
1398            else
1399                eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
1400                exit 1
1401            fi
1402        elif [ "$DIST_OS" = "freebsd" ] ; then
1403            eval echo `gettext 'Detected FreeBSD:'`
1404            if [ -f "/etc/rc.d/$APP_NAME" ] ; then
1405                eval echo `gettext ' Removing $APP_LONG_NAME daemon...'`
1406                for i in "/etc/rc.d/$APP_NAME"
1407                do
1408                    rm -f $i
1409                done
1410                sed -i .bak "/${APP_NAME}_enable=\"YES\"/d" /etc/rc.conf
1411            else
1412                eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
1413                exit 1
1414            fi
1415        elif [ "$DIST_OS" = "macosx" ] ; then
1416            eval echo `gettext 'Detected Mac OSX:'`
1417            if [ -f "/Library/LaunchDaemons/${APP_PLIST}" ] ; then
1418                eval echo `gettext ' Removing $APP_LONG_NAME daemon...'`
1419                # Make sure the plist is installed
1420                LOADED_PLIST=`launchctl list | grep ${APP_PLIST_BASE}`
1421                if [ "X${LOADED_PLIST}" != "X" ] ; then
1422                    launchctl unload "/Library/LaunchDaemons/${APP_PLIST}"
1423                fi
1424                rm -f "/Library/LaunchDaemons/${APP_PLIST}"
1425            else
1426                eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
1427                exit 1
1428            fi
1429        elif [ "$DIST_OS" = "zos" ] ; then
1430            eval echo `gettext 'Detected z/OS:'`
1431            if [ -f /etc/rc.bak ] ; then
1432                eval echo `gettext ' Removing $APP_LONG_NAME daemon...'`
1433                cp /etc/rc /etc/rc.bak
1434                sed  "s/_BPX_JOBNAME=\'APP_NAME\'.*//g" /etc/rc.bak > /etc/rc
1435                rm /etc/rc.bak
1436            else
1437                eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
1438                exit 1
1439            fi
1440        else
1441            eval echo `gettext 'Remove not currently supported for $DIST_OS'`
1442            exit 1
1443        fi
1444    fi
1445}
1446
1447dump() {
1448    eval echo `gettext 'Dumping $APP_LONG_NAME...'`
1449    getpid
1450    if [ "X$pid" = "X" ]
1451    then
1452        eval echo `gettext '$APP_LONG_NAME was not running.'`
1453    else
1454        kill -3 $pid
1455
1456        if [ $? -ne 0 ]
1457        then
1458            eval echo `gettext 'Failed to dump $APP_LONG_NAME.'`
1459            exit 1
1460        else
1461            eval echo `gettext 'Dumped $APP_LONG_NAME.'`
1462        fi
1463    fi
1464}
1465
1466# Used by HP-UX init scripts.
1467startmsg() {
1468    getpid
1469    if [ "X$pid" = "X" ]
1470    then
1471        eval echo `gettext 'Starting $APP_LONG_NAME...  Wrapper:Stopped'`
1472    else
1473        if [ "X$DETAIL_STATUS" = "X" ]
1474        then
1475            eval echo `gettext 'Starting $APP_LONG_NAME...  Wrapper:Running'`
1476        else
1477            getstatus
1478            eval echo `gettext 'Starting $APP_LONG_NAME...  Wrapper:$STATUS, Java:$JAVASTATUS'`
1479        fi
1480    fi
1481}
1482
1483# Used by HP-UX init scripts.
1484stopmsg() {
1485    getpid
1486    if [ "X$pid" = "X" ]
1487    then
1488        eval echo `gettext 'Stopping $APP_LONG_NAME...  Wrapper:Stopped'`
1489    else
1490        if [ "X$DETAIL_STATUS" = "X" ]
1491        then
1492            eval echo `gettext 'Stopping $APP_LONG_NAME...  Wrapper:Running'`
1493        else
1494            getstatus
1495            eval echo `gettext 'Stopping $APP_LONG_NAME...  Wrapper:$STATUS, Java:$JAVASTATUS'`
1496        fi
1497    fi
1498}
1499
1500showUsage() {
1501    # $1 bad command
1502
1503    if [ -n "$1" ]
1504    then
1505        eval echo `gettext 'Unexpected command: $1'`
1506        echo "";
1507    fi
1508
1509    eval MSG=`gettext 'Usage: '`
1510    if [ -n "$FIXED_COMMAND" ] ; then
1511        if [ -n "$PASS_THROUGH" ] ; then
1512            echo "${MSG} $0 {JavaAppArgs}"
1513        else
1514            echo "${MSG} $0"
1515        fi
1516    else
1517        if [ -n "$PAUSABLE" ] ; then
1518            if [ -n "$PASS_THROUGH" ] ; then
1519                echo "${MSG} $0 [ console {JavaAppArgs} | start {JavaAppArgs} | stop | restart {JavaAppArgs} | condrestart {JavaAppArgs} | pause | resume | status | install | remove | dump ]"
1520            else
1521                echo "${MSG} $0 [ console | start | stop | restart | condrestart | pause | resume | status | install | remove | dump ]"
1522            fi
1523        else
1524            if [ -n "$PASS_THROUGH" ] ; then
1525                echo "${MSG} $0 [ console {JavaAppArgs} | start {JavaAppArgs} | stop | restart {JavaAppArgs} | condrestart {JavaAppArgs} | status | install | remove | dump ]"
1526            else
1527                echo "${MSG} $0 [ console | start | stop | restart | condrestart | status | install | remove | dump ]"
1528            fi
1529        fi
1530    fi
1531
1532    if [ ! -n "$BRIEF_USAGE" ]
1533    then
1534        echo "";
1535        if [ ! -n "$FIXED_COMMAND" ] ; then
1536            echo "`gettext 'Commands:'`"
1537            echo "`gettext '  console      Launch in the current console.'`"
1538            echo "`gettext '  start        Start in the background as a daemon process.'`"
1539            echo "`gettext '  stop         Stop if running as a daemon or in another console.'`"
1540            echo "`gettext '  restart      Stop if running and then start.'`"
1541            echo "`gettext '  condrestart  Restart only if already running.'`"
1542            if [ -n "$PAUSABLE" ] ; then
1543                echo "`gettext '  pause        Pause if running.'`"
1544                echo "`gettext '  resume       Resume if paused.'`"
1545            fi
1546            echo "`gettext '  status       Query the current status.'`"
1547            echo "`gettext '  install      Install to start automatically when system boots.'`"
1548            echo "`gettext '  remove       Uninstall.'`"
1549            echo "`gettext '  dump         Request a Java thread dump if running.'`"
1550            echo "";
1551        fi
1552        if [ -n "$PASS_THROUGH" ] ; then
1553            echo "`gettext 'JavaAppArgs: Zero or more arguments which will be passed to the Java application.'`"
1554            echo "";
1555        fi
1556    fi
1557
1558    exit 1
1559}
1560
1561showsetusermesg()  {
1562    echo "Please edit $0 and set the variable RUN_AS_USER."
1563}
1564
1565checkifstartingasroot() {
1566    if [ ! `grep ^RUN_AS_USER $0` ] && [ ! `grep ^ALLOW_ROOT $0` ] && [ `id -ur` = '0' ]; then
1567        echo "Running I2P as the root user is *not* recommended."
1568        showsetusermesg
1569        echo
1570        echo "If you'd like to run as root anyway you can edit"
1571        echo "$0 and set ALLOW_ROOT=true instead."
1572        exit 1
1573    fi
1574    }
1575docommand() {
1576    case "$COMMAND" in
1577        'console')
1578            checkifstartingasroot
1579            checkUser touchlock "$@"
1580            if [ ! -n "$FIXED_COMMAND" ] ; then
1581                shift
1582            fi
1583            console "$@"
1584            ;;
1585
1586        'start')
1587            checkifstartingasroot
1588            if [ "$DIST_OS" = "macosx" -a -f "/Library/LaunchDaemons/${APP_PLIST}" ] ; then
1589                macosxstart
1590            elif [ "$DIST_OS" = "linux" -a -f "/etc/init/${APP_NAME}.conf" ] ; then
1591                checkUser touchlock "$@"
1592                upstartstart
1593            else
1594                checkUser touchlock "$@"
1595                if [ ! -n "$FIXED_COMMAND" ] ; then
1596                    shift
1597                fi
1598                start "$@"
1599            fi
1600            ;;
1601
1602        'stop')
1603            checkUser "" "$COMMAND"
1604            stopit "0"
1605            ;;
1606
1607        'restart')
1608            checkUser touchlock "$COMMAND"
1609            if [ ! -n "$FIXED_COMMAND" ] ; then
1610                shift
1611            fi
1612            stopit "0"
1613            start "$@"
1614            ;;
1615
1616        'condrestart')
1617            checkUser touchlock "$COMMAND"
1618            if [ ! -n "$FIXED_COMMAND" ] ; then
1619                shift
1620            fi
1621            stopit "1"
1622            start "$@"
1623            ;;
1624
1625        'pause')
1626            if [ -n "$PAUSABLE" ]
1627            then
1628                pause
1629            else
1630                showUsage "$COMMAND"
1631            fi
1632            ;;
1633
1634        'resume')
1635            if [ -n "$PAUSABLE" ]
1636            then
1637                resume
1638            else
1639                showUsage "$COMMAND"
1640            fi
1641            ;;
1642
1643        'status')
1644            checkUser "" "$COMMAND"
1645            status
1646            ;;
1647
1648        'install')
1649            installdaemon
1650            if [ ! `grep ^RUN_AS_USER $0` ]; then
1651                 showsetusermesg
1652            fi
1653
1654            ;;
1655
1656        'remove' | 'uninstall')
1657            removedaemon
1658            ;;
1659
1660        'dump')
1661            checkUser "" "$COMMAND"
1662            dump
1663            ;;
1664
1665        'start_msg')
1666            # Internal command called by launchd on HP-UX.
1667            checkUser "" "$COMMAND"
1668            startmsg
1669            ;;
1670
1671        'stop_msg')
1672            # Internal command called by launchd on HP-UX.
1673            checkUser "" "$COMMAND"
1674            stopmsg
1675            ;;
1676
1677        'launchdinternal')
1678            # Internal command called by launchd on Max OSX.
1679            # We do not want to call checkUser here as it is handled in the launchd plist file.  Doing it here would confuse launchd.
1680            if [ ! -n "$FIXED_COMMAND" ] ; then
1681                shift
1682            fi
1683            launchdinternal "$@"
1684            ;;
1685
1686        *)
1687            showUsage "$COMMAND"
1688            ;;
1689    esac
1690}
1691
1692docommand "$@"
1693
1694exit 0
Note: See TracBrowser for help on using the repository browser.