source: installer/resources/i2prouter @ 57afc4c

Last change on this file since 57afc4c was 57afc4c, checked in by kytv <kytv@…>, 10 years ago

Advise to set RUN_AS_USER when installing as a daemon

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