source: installer/resources/i2prouter @ a8746e4

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

updates to i2prouter based upon changes from 3.5.9 → 3.5.12 in tanuki's
src/bin/sh.script.in

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