source: installer/resources/i2prouter @ 5d43282

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

Remove note about the misc/compat6x port; it is no longer required

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