source: installer/resources/i2prouter @ 7f6c4fa

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

Add "uninstall" as an alternative option for "remove"

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