source: installer/resources/i2prouter @ f97779b

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

fix stupid typo by adding a missing ! (*facepalm*)

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