source: installer/resources/i2prouter @ f6ca6a5

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