1#! /bin/sh
2
3#
4# Copyright (c) 1999, 2021 Tanuki Software, Ltd.
5# http://www.tanukisoftware.com
6# All rights reserved.
7#
8# This software is the proprietary information of Tanuki Software.
9# You shall use it only in accordance with the terms of the
10# license agreement you entered into with Tanuki Software.
11# http://wrapper.tanukisoftware.com/doc/english/licenseOverview.html
12#
13# Java Service Wrapper sh script.  Suitable for starting and stopping
14#  wrapped Java applications on UNIX platforms.
15# Optimized for use with version 3.5.45 of the Wrapper.
16#
17
18#-----------------------------------------------------------------------------
19# These settings can be modified to fit the needs of your application
20
21# IMPORTANT - Please always stop and uninstall an application before making
22#             any changes to this file.  Failure to do so could remove the
23#             script's ability to control the application.
24
25# NOTE - After loading the variables below, the script will attempt to locate a
26#  file with the same basename as this script and having a '.shconf' extension.
27#  If such file exists, it will be executed giving the user a chance to
28#  override the default settings. Having the customized configuration in a
29#  separate '.shconf' file makes it easier to upgrade the Wrapper, as the
30#  present script file can then be replaced with minimal changes (although at
31#  least the 'INIT INFO' below needs to be updated).
32
33# Initialization block for the install_initd and remove_initd scripts used by
34#  SUSE linux, CentOS and RHEL distributions.  Also used by update-rc.d.
35# Note: From CentOS 6, make sure the BEGIN INIT INFO section is before any line
36#       of code otherwise the service won't be displayed in the Service
37#       Configuration GUI.
38### BEGIN INIT INFO
39# Provides: @app.name@
40# Required-Start: $remote_fs $syslog
41# Should-Start: $network $time
42# Should-Stop: $network $time
43# Required-Stop: $remote_fs $syslog
44# Default-Start: 2 3 4 5
45# Default-Stop: 0 1 6
46# Short-Description: @app.long.name@
47# Description: @app.description@
48### END INIT INFO
49
50# Application name and long name: If these variables are not set (or left to
51#  the default tokens), APP_NAME will default to the name of the script, then
52#  APP_LONG_NAME will default to the value of APP_NAME.
53APP_NAME="@app.name@"
54APP_LONG_NAME="@app.long.name@"
55
56# If uncommented (and set to false), APP_NAME and APP_LONG_NAME will no longer
57#  be passed to the wrapper. See documentation for details.
58#APP_NAME_PASS_TO_WRAPPER=false
59
60# Wrapper
61WRAPPER_CMD="./wrapper"
62WRAPPER_CONF="../conf/wrapper.conf"
63
64# Priority at which to run the wrapper.  See "man nice" for valid priorities.
65#  nice is only used if a priority is specified.
66PRIORITY=
67
68# Location of the pid file.
69PIDDIR="."
70
71# PIDFILE_CHECK_PID tells the script to double check whether the pid in the pid
72#  file actually exists and belongs to this application.  When not set, only
73#  check the pid, but not what it is.  This is only needed when multiple
74#  applications need to share the same pid file.
75PIDFILE_CHECK_PID=true
76
77# FIXED_COMMAND tells the script to use a hard coded action rather than
78#  expecting the first parameter of the command line to be the command.
79#  By default the command will be expected to be the first parameter.
80#FIXED_COMMAND=console
81
82# PASS_THROUGH controls how the script arguments should be passed to the
83#  Wrapper. Possible values are:
84#  - commented or 'false': the arguments will be ignored (not passed).
85#  - 'app_args' or 'true': the arguments will be passed through the Wrapper as
86#                          arguments for the Java Application.
87#  - 'both': both Wrapper properties and Application arguments can be passed to
88#            the Wrapper. The Wrapper properties come in first position. The
89#            user can optionally add a '--' separator followed by application
90#            arguments.
91# NOTE - If FIXED_COMMAND is set to true the above applies to all arguments,
92#        otherwise it applies to arguments starting with the second.
93# NOTE - Passing arguments is only valid with the following commands:
94#          - 'console'
95#          - 'start', 'restart', 'condrestart' (if not installed as a daemon)
96#PASS_THROUGH=app_args
97
98# If uncommented, causes the Wrapper to be shutdown using an anchor file.
99#  When launched with the 'start' command, it will also ignore all INT and
100#  TERM signals.
101#IGNORE_SIGNALS=true
102
103# Wrapper will start the JVM asynchronously. Your application may have some
104#  initialization tasks and it may be desirable to wait a few seconds
105#  before returning.  For example, to delay the invocation of following
106#  startup scripts.  Setting WAIT_AFTER_STARTUP to a positive number will
107#  cause the start command to delay for the indicated period of time
108#  (in seconds).
109WAIT_AFTER_STARTUP=0
110
111# If set, wait for the wrapper to report that the daemon has started
112WAIT_FOR_STARTED_STATUS=true
113WAIT_FOR_STARTED_TIMEOUT=120
114
115# If set, the status, start_msg and stop_msg commands will print out detailed
116#   state information on the Wrapper and Java processes.
117#DETAIL_STATUS=true
118
119# If set, the 'pause' and 'resume' commands will be enabled.  These make it
120#  possible to pause the JVM or Java application without completely stopping
121#  the Wrapper.  See the wrapper.pausable and wrapper.pausable.stop_jvm
122#  properties for more information.
123#PAUSABLE=true
124
125# Set the mode used to 'pause' or 'resume' the Wrapper. Possible values are
126#  'signals' which uses SIGUSR1 and SIGUSR2, and 'file' which uses the command
127#  file to communicate these actions.  The default value is 'signals'.
128#  Be aware that depending on the mode, the properties wrapper.signal.mode.usr1,
129#  wrapper.signal.mode.usr2, or wrapper.commandfile of the configuration file may
130#  be overriden.
131#PAUSABLE_MODE=signals
132
133# If specified, the Wrapper will be run as the specified user.
134# IMPORTANT - Make sure that the user has the required privileges to write
135#  the PID file and wrapper.log files.  Failure to be able to write the log
136#  file will cause the Wrapper to exit without any way to write out an error
137#  message.
138# NOTE - This will set the user which is used to run the Wrapper as well as
139#  the JVM and is not useful in situations where a privileged resource or
140#  port needs to be allocated prior to the user being changed.
141#RUN_AS_USER=
142
143# Set the full path to the 'su' command (substitute user).
144# NOTE - In case 'su' is not in the PATH, you can set the absolute path here,
145#  for example:
146#  SU_BIN=/bin/su
147# NOTE - For Red Hat, the script will use '/sbin/runuser' if it is present and
148#  ignore the value of SU_BIN.
149SU_BIN=su
150
151# Set option(s) for 'su' or 'runuser'.
152# In case the user set in RUN_AS_USER has no bash set, the 'su' command will fail.
153# The workaround for GNU/Linux system is to specify which bash to use with
154#  the '-s' option.
155#SU_OPTS="-s /bin/bash"
156
157# By default we show a detailed usage block.  Uncomment to show brief usage.
158#BRIEF_USAGE=true
159
160# Set which service management tool to use.
161# Possible values are:
162#   for linux...: auto, systemd, upstart, initd
163#   for aix.....: auto, src, initd
164# When set to 'auto', this script file will try to detect in the order of the
165# list for each platform which service management tool to use to install the Wrapper.
166SERVICE_MANAGEMENT_TOOL=auto
167
168# Specify how the Wrapper daemon and its child processes should be killed
169#  when using systemd.
170#  The default is 'control-group' which tells systemd to kill all child
171#  processes (including detached ones) in the control group of the daemon
172#  when it stops.
173#  Alternatively, 'process' can be specified to prevent systemd from
174#  killing the child processes leaving this responsibility to the Wrapper.
175#  In this case child processes marked as 'detached' will not be killed on shutdown.
176# NOTE - the daemon must be reinstalled for any changes on this property to take effect.
177SYSTEMD_KILLMODE=control-group
178
179# When installing on Mac OSX platforms, the following domain will be used to
180#  prefix the plist file name.
181PLIST_DOMAIN=org.tanukisoftware.wrapper
182
183# When installing on Mac OSX platforms, this parameter controls whether the daemon
184#  is to be kept continuously running or to let demand and conditions control the
185#  invocation.
186MACOSX_KEEP_RUNNING="false"
187
188# The following two lines are used by the chkconfig command. Change as is
189#  appropriate for your application.  They should remain commented.
190# chkconfig: 2345 20 80
191# description: @app.long.name@
192
193# Set run level to use when installing the application to start and stop on
194#  system startup and shutdown.  It is important that the application always
195#  be uninstalled before making any changes to the run levels.
196# It is also possible to specify different run levels based on the individual
197#  platform.  When doing so this script will look for defined run levels in
198#  the following order:
199#   1) "RUN_LEVEL_S_$DIST_OS" or "RUN_LEVEL_K_$DIST_OS", where "$DIST_OS" is
200#      the value of DIST_OS.  "RUN_LEVEL_S_solaris=20" for example.
201#   2) RUN_LEVEL_S or RUN_LEVEL_K, to specify specify start or stop run levels.
202#   3) RUN_LEVEL, to specify a general run level.
203RUN_LEVEL=20
204
205# List of files to source prior to executing any commands. Use ';' as delimiter.
206# For example:
207#  FILES_TO_SOURCE="/home/user/.bashrc;anotherfile;../file3"
208FILES_TO_SOURCE=
209
210# Do not modify anything beyond this point
211#-----------------------------------------------------------------------------
212
213checkIsRoot() {
214    if [ `id | sed 's/^uid=//;s/(.*$//'` != "0" ] ; then
215        IS_ROOT=false
216    else
217        IS_ROOT=true
218    fi
219}
220
221gettext() {
222    "$WRAPPER_CMD" --translate "$1" "$WRAPPER_CONF" 2>/dev/null
223    if [ $? != 0 ] ; then
224        echo "$1"
225    fi
226}
227
228##
229# Resolves the location of a system command.
230#
231# $1: the name of the variable to set (without the $)
232# $2: the name of the command
233# $3: an ordered and semicolon-separated list of paths where the command should
234#     be searched. The list should contain an empty value for the command to be
235#     searched using the PATH environment variable.
236# $4: 1 to be strict (the script will stop with an error), 0 otherwise.
237resolveLocation() {
238    eval "CMD_TEMP=\$$1"
239    if [ "X$CMD_TEMP" = "X" ] ; then
240        found=0
241
242        OIFS=$IFS
243        IFS=';'
244        for CMD_PATH in $3
245        do
246            if [ -z "$CMD_PATH" ] ; then
247                # empty path
248                CMD_TEMP="$2"
249                ret=`command -v $CMD_TEMP 2>/dev/null`
250                if [ $? -eq 0 ] ; then
251                    found=1
252                    break
253                fi
254            else
255                CMD_TEMP="${CMD_PATH}/$2"
256                if [ -x "$CMD_TEMP" ] ; then
257                    found=1
258                    break
259                fi
260            fi
261        done
262        IFS=$OIFS
263
264        if [ $found -eq 1 ] ; then
265            eval "$1=$CMD_TEMP"
266        elif [ $4 -eq 1 ] ; then
267            eval echo `gettext 'Unable to locate "$2".'`
268            eval echo `gettext 'Please report this message along with the location of the command on your system.'`
269            exit 1
270        else
271            # return the error
272            return 1
273        fi
274    fi
275    return 0
276}
277
278resolveIdLocation() {
279    # On Solaris, the version in /usr/xpg4/bin should be used in priority.
280    resolveLocation ID_BIN id "/usr/xpg4/bin/id;;/usr/bin/id" 1
281}
282
283resolveCurrentUser() {
284    if [ "X$CURRENT_USER" = "X" ] ; then
285        resolveIdLocation
286        CURRENT_USER=`$ID_BIN -u -n 2>/dev/null`
287    fi
288}
289
290# check if we are running under Cygwin terminal.
291# Note: on some OS's (for example Solaris, MacOS), -o is not a valid parameter
292# and it shows an error message. We redirect stderr to null so the error message
293# doesn't show up.
294CYGWIN=`uname -o 2>/dev/null`
295if [ "$CYGWIN" = "Cygwin" ]
296then
297  eval echo `gettext 'This script is not compatible with Cygwin.  Please use the Wrapper batch files to control the Wrapper.'`
298  exit 1
299fi
300
301# check if there is a parameter "sysd"
302SYSD=
303if [ $# -gt 1 ] ; then
304    if [ $2 = "sysd" ] ; then
305        SYSD=1
306    fi
307fi
308
309# Required for HP-UX Startup
310if [ `uname -s` = "HP-UX" -o `uname -s` = "HP-UX64" ] ; then
311    PATH=$PATH:/usr/bin
312fi
313
314# Get the fully qualified path to the script
315case $0 in
316    /*)
317        SCRIPT="$0"
318        ;;
319    *)
320        PWD=`pwd`
321        if [ $? -ne 0 ] ; then
322            # On some systems pwd may fail if one of the parent folder has insufficient permissions.
323            #  Is there a way to access the current location which would allow to print the permissions
324            #  for each folder like we do below?
325            exit 1
326        fi
327        SCRIPT="$PWD/$0"
328        ;;
329esac
330
331# Resolve the true real path without any sym links.
332CHANGED=true
333while [ "X$CHANGED" != "X" ]
334do
335    # Change spaces to ":" so the tokens can be parsed.
336    SAFESCRIPT=`echo "$SCRIPT" | sed -e 's; ;:;g'`
337    # Get the real path to this script, resolving any symbolic links
338    TOKENS=`echo $SAFESCRIPT | sed -e 's;/; ;g'`
339    REALPATH=
340    for C in $TOKENS; do
341        # Change any ":" in the token back to a space.
342        C=`echo $C | sed -e 's;:; ;g'`
343        REALPATH="$REALPATH/$C"
344        # If REALPATH is a sym link, resolve it.  Loop for nested links.
345        while [ -h "$REALPATH" ] ; do
346            LS="`ls -ld "$REALPATH"`"
347            LINK="`expr "$LS" : '.*-> \(.*\)$'`"
348            if expr "$LINK" : '/.*' > /dev/null; then
349                # LINK is absolute.
350                REALPATH="$LINK"
351            else
352                # LINK is relative.
353                REALPATH="`dirname "$REALPATH"`""/$LINK"
354            fi
355        done
356    done
357
358    if [ "$REALPATH" = "$SCRIPT" ]
359    then
360        CHANGED=""
361    else
362        SCRIPT="$REALPATH"
363    fi
364done
365
366# Try to source a file with the same filename as the script and with the '.shconf' extension.
367case $REALPATH in
368    *.sh)
369        SHCONF_FILE=`echo $REALPATH | rev | cut -d "." -f2- | rev`
370        ;;
371    *)
372        SHCONF_FILE="$REALPATH"
373        ;;
374esac
375
376SHCONF_FILE="${SHCONF_FILE}.shconf"
377
378if [ -f "$SHCONF_FILE" ] ; then
379    if [ ! -x "$SHCONF_FILE" ] ; then
380        # We should stop here because the configuration expected in the shconf file will not be loaded.
381        eval echo `gettext 'Found $SHCONF_FILE but could not execute it. Please make sure that the file has execute permissions.'`
382        exit 1
383    fi
384    . "$SHCONF_FILE"
385fi
386
387if [ -n "$FIXED_COMMAND" ] ; then
388    COMMAND="$FIXED_COMMAND"
389    FIRST_ARG=$1
390else
391    COMMAND="$1"
392    FIRST_ARG=$2
393fi
394
395if [ "X${PASS_THROUGH}" = "Xtrue" -o "X${PASS_THROUGH}" = "Xapp_args" ] ; then
396    PASS_THROUGH=app_args
397elif [ "X${PASS_THROUGH}" != "Xboth" ] ; then
398    PASS_THROUGH=false
399fi
400
401# Get the location of the script.
402REALDIR=`dirname "$REALPATH"`
403
404if [ "X$RUN_AS_USER" != "X" ] ; then
405    CHECK_FOLDER_PERMISSIONS=0
406    if [ "$COMMAND" = "console" -o "$COMMAND" = "start" -o "$COMMAND" = "restart" -o "$COMMAND" = "condrestart" -o "$COMMAND" = "install" -o "$COMMAND" = "installstart" ] ; then
407        checkIsRoot
408        if [ "$IS_ROOT" = "true" ] ; then
409            if [ -f "/sbin/runuser" -a $COMMAND != "console" ] ; then
410                result=`/sbin/runuser - $RUN_AS_USER -c "cd \"${REALDIR}\"" $SU_OPTS 2>/dev/null`
411            else
412                result=`$SU_BIN - $RUN_AS_USER -c "cd \"${REALDIR}\"" $SU_OPTS 2>/dev/null`
413            fi
414            CHECK_FOLDER_PERMISSIONS=$?
415        fi
416    fi
417else
418    # Technically REALDIR should not be required for all commands, but in fact the script will fail below if the Wrapper binaries can't be accessed.
419    #  It is thus better to show a message and print folder permissions now.
420    result=`cd "${REALDIR}" 2>/dev/null`
421    CHECK_FOLDER_PERMISSIONS=$?
422fi
423if [ $CHECK_FOLDER_PERMISSIONS -ne 0 ] ; then
424    if [ "X$RUN_AS_USER" != "X" ] ; then
425        CHECK_USER=$RUN_AS_USER
426    else
427        resolveCurrentUser
428        CHECK_USER=$CURRENT_USER
429    fi
430    # Unfortunately the following message will not be translated because currently the Wrapper fails to execute correctly when it can't access to the absolute path.
431    eval echo `gettext 'Failed to access the script using an absolute path. Insufficient permissions may prevent the user \"$CHECK_USER\" from traversing one of the folders. Please check the following permissions:'`
432
433    OIFS=$IFS
434    IFS='/'
435    for DIR in $REALDIR
436    do
437        INT_PATH="${INT_PATH}/$DIR"
438        if [ "$DIR" != "." -a "$DIR" != ".." ] ; then
439            ALL_PATHS="${ALL_PATHS} ${INT_PATH}"
440            result=`cd "${INT_PATH}" 2>/dev/null`
441            if [ $? -ne 0 ] ; then
442                # no access to this folder and so to the rest of the path.
443                break
444            fi
445        fi
446    done
447    IFS=$OIFS
448    ls -dal $ALL_PATHS
449    exit 1
450fi
451
452# Normalize the path
453REALDIR=`cd "${REALDIR}"; pwd`
454
455# If the PIDDIR is relative, set its value relative to the full REALPATH to avoid problems if
456#  the working directory is later changed.
457FIRST_CHAR=`echo $PIDDIR | cut -c1,1`
458if [ "$FIRST_CHAR" != "/" ]
459then
460    PIDDIR=$REALDIR/$PIDDIR
461fi
462# Same test for WRAPPER_CMD
463FIRST_CHAR=`echo $WRAPPER_CMD | cut -c1,1`
464if [ "$FIRST_CHAR" != "/" ]
465then
466    WRAPPER_CMD=$REALDIR/$WRAPPER_CMD
467fi
468# Same test for WRAPPER_CONF
469FIRST_CHAR=`echo $WRAPPER_CONF | cut -c1,1`
470if [ "$FIRST_CHAR" != "/" ]
471then
472    WRAPPER_CONF=$REALDIR/$WRAPPER_CONF
473fi
474
475# Give default values to $APP_NAME and $APP_LONG_NAME
476DELIMITER="@"
477if [ "X$APP_NAME" = "X" -o "$APP_NAME" = "${DELIMITER}app.name${DELIMITER}" ] ; then
478    APP_NAME=`basename -- "$0"`
479fi
480if [ "X$APP_LONG_NAME" = "X" -o "$APP_LONG_NAME" = "${DELIMITER}app.long.name${DELIMITER}" ] ; then
481    APP_LONG_NAME=$APP_NAME
482fi
483
484# default location for the service file
485SYSTEMD_SERVICE_FILE="/etc/systemd/system/$APP_NAME.service"
486
487# Installation status
488SERVICE_NOT_INSTALLED=0
489SERVICE_INSTALLED_DEFAULT=1
490SERVICE_INSTALLED_SYSTEMD=2
491SERVICE_INSTALLED_UPSTART=4
492SERVICE_INSTALLED_SRC=8
493SERVICE_INSTALLED_SRC_PARTIAL=16 #incomplete installation with SRC (lssrc or lsitab failed to return a record)
494
495# Process ID
496ANCHORFILE="$PIDDIR/$APP_NAME.anchor"
497COMMANDFILE="$PIDDIR/$APP_NAME.command"
498STATUSFILE="$PIDDIR/$APP_NAME.status"
499JAVASTATUSFILE="$PIDDIR/$APP_NAME.java.status"
500PIDFILE="$PIDDIR/$APP_NAME.pid"
501LOCKDIR="/var/lock/subsys"
502LOCKFILE="$LOCKDIR/$APP_NAME"
503pid=""
504
505# Resolve the location of the 'ps' & 'tr' command
506resolveLocation PS_BIN ps "/usr/ucb;/usr/bin;/bin" 1
507resolveLocation TR_BIN tr "/usr/bin;/bin" 1
508
509# Resolve the os
510DIST_OS=`uname -s | $TR_BIN "[A-Z]" "[a-z]" | $TR_BIN -d ' '`
511case "$DIST_OS" in
512    'sunos')
513        DIST_OS="solaris"
514        ;;
515    'hp-ux' | 'hp-ux64')
516        # HP-UX needs the XPG4 version of ps (for -o args)
517        DIST_OS="hpux"
518        UNIX95=""
519        export UNIX95
520        ;;
521    'darwin')
522        DIST_OS="macosx"
523        ;;
524    'unix_sv')
525        DIST_OS="unixware"
526        ;;
527    'os/390')
528        DIST_OS="zos"
529        ;;
530    'linux')
531        DIST_OS="linux"
532        ;;
533esac
534
535# Compare Versions $1<$2=0, $1==$2=1, $1>$2=2
536compareVersions () {
537    if [ "$1" = "$2" ]
538    then
539        return 1
540    else
541        local i=1
542        while true
543        do
544            local v1=`echo "$1" | cut -d '.' -f $i`
545            local v2=`echo "$2" | cut -d '.' -f $i`
546            if [ "X$v1" = "X" ]
547            then
548                if [ "X$v2" = "X" ]
549                then
550                    return 1
551                fi
552                v1="0"
553            elif [ "X$v2" = "X" ]
554            then
555                v2="0"
556            fi
557            if [ $v1 -lt $v2 ]
558            then
559                return 0
560            elif [ $v1 -gt $v2 ]
561            then
562                return 2
563            fi
564            i=`expr $i + 1`
565        done
566    fi
567}
568
569# Resolve the architecture
570if [ "$DIST_OS" = "macosx" ]
571then
572    OS_VER=`sw_vers | grep 'ProductVersion:' | grep -o '[0-9]*\.[0-9]*\.[0-9]*\|[0-9]*\.[0-9]*'`
573    DIST_ARCH="universal"
574    compareVersions "$OS_VER" "10.5.0"
575    if [ $? -lt 1 ]
576    then
577        DIST_BITS="32"
578        KEY_KEEP_ALIVE="OnDemand"
579    else
580        # Note: "OnDemand" has been deprecated and replaced from Mac OS X 10.5 by "KeepAlive"
581        KEY_KEEP_ALIVE="KeepAlive"
582
583        if [ "X`/usr/sbin/sysctl -n hw.cpu64bit_capable`" = "X1" ]
584        then
585            DIST_BITS="64"
586        else
587            DIST_BITS="32"
588        fi
589    fi
590    APP_PLIST_BASE=${PLIST_DOMAIN}.${APP_NAME}
591    APP_PLIST=${APP_PLIST_BASE}.plist
592elif [ "$DIST_OS" = "zos" ] ; then
593    # Z/Os is not supported in the Delta Pack, therefore we only provide a binary
594    # file named "wrapper". However this script will still check for a file named
595    # "wrapper-zos-390-32" and "wrapper-zos-390-64" in case the user edited the name.
596    DIST_ARCH="390"
597    DIST_BITS="64"
598else
599    if [ "$DIST_OS" = "linux" ]
600    then
601        DIST_ARCH=
602    else
603        DIST_ARCH=`uname -p 2>/dev/null | $TR_BIN "[A-Z]" "[a-z]" | $TR_BIN -d ' '`
604    fi
605    if [ "X$DIST_ARCH" = "X" ]
606    then
607        DIST_ARCH="unknown"
608    fi
609    if [ "$DIST_ARCH" = "unknown" ]
610    then
611        DIST_ARCH=`uname -m 2>/dev/null | $TR_BIN "[A-Z]" "[a-z]" | $TR_BIN -d ' '`
612    fi
613    case "$DIST_ARCH" in
614        'athlon' | 'i386' | 'i486' | 'i586' | 'i686')
615            DIST_ARCH="x86"
616            if [ "${DIST_OS}" = "solaris" ] ; then
617                DIST_BITS=`isainfo -b`
618            else
619                DIST_BITS="32"
620            fi
621            ;;
622        'amd64' | 'x86_64')
623            DIST_ARCH="x86"
624            DIST_BITS="64"
625            ;;
626        'ia32')
627            DIST_ARCH="ia"
628            DIST_BITS="32"
629            ;;
630        'ia64' | 'ia64n' | 'ia64w')
631            DIST_ARCH="ia"
632            DIST_BITS="64"
633            ;;
634        'ip27')
635            DIST_ARCH="mips"
636            DIST_BITS="32"
637            ;;
638        'ppc64le')
639            DIST_ARCH="ppcle"
640            DIST_BITS="64"
641            ;;
642        'power' | 'powerpc' | 'power_pc' | 'ppc64')
643            if [ "${DIST_ARCH}" = "ppc64" ] ; then
644                DIST_BITS="64"
645            else
646                DIST_BITS="32"
647            fi
648            DIST_ARCH="ppcbe"
649
650            if [ "${DIST_OS}" = "aix" ] ; then
651                DIST_ARCH="ppc"
652                if [ `getconf KERNEL_BITMODE` -eq 64 ]; then
653                    DIST_BITS="64"
654                else
655                    DIST_BITS="32"
656                fi
657            fi
658            ;;
659        'pa_risc' | 'pa-risc')
660            DIST_ARCH="parisc"
661            if [ `getconf KERNEL_BITS` -eq 64 ]; then
662                DIST_BITS="64"
663            else
664                DIST_BITS="32"
665            fi
666            ;;
667        'sun4u' | 'sparcv9' | 'sparc')
668            DIST_ARCH="sparc"
669            DIST_BITS=`isainfo -b`
670            ;;
671        '9000/800' | '9000/785')
672            DIST_ARCH="parisc"
673            if [ `getconf KERNEL_BITS` -eq 64 ]; then
674                DIST_BITS="64"
675            else
676                DIST_BITS="32"
677            fi
678            ;;
679        s390* )
680            DIST_ARCH="390"
681            if [ `getconf LONG_BIT` -eq 64 ] ; then
682                DIST_BITS="64"
683            else
684                DIST_BITS="32"
685            fi
686            ;;
687        aarch64* | arm64*)
688            # 'aarch64_be', 'aarch64', 'arm64', etc.
689            # => only armhf is 64-bit
690            DIST_ARCH="armhf"
691            DIST_BITS="64"
692            ;;
693        armv*)
694            # 'armv8b', 'armv8l', 'armv7l', 'armv5tel', etc.
695            # => armv8 and above should be 64-bit, but it is more reliable to check the bits with getconf.
696            if [ `getconf LONG_BIT` -eq 64 ]; then
697                DIST_ARCH="armhf"
698                DIST_BITS="64"
699            else
700                # Note: The following command returns nothing on SUSE for Raspberry Pi 3 (aarch64).
701                #       An alternative command would be 'dpkg --print-architecture', but dpkg may not exist.
702                if [ -z "`readelf -A /proc/self/exe | grep Tag_ABI_VFP_args`" ] ; then
703                    DIST_ARCH="armel"
704                else
705                    DIST_ARCH="armhf"
706                fi
707                DIST_BITS="32"
708            fi
709            ;;
710    esac
711fi
712
713# OSX always places Java in the same location so we can reliably set JAVA_HOME
714if [ "$DIST_OS" = "macosx" ]
715then
716    if [ -z "$JAVA_HOME" ]; then
717        if [ -x /usr/libexec/java_home ]; then
718            JAVA_HOME=`/usr/libexec/java_home`; export JAVA_HOME
719        else
720            JAVA_HOME="/Library/Java/Home"; export JAVA_HOME
721        fi
722    fi
723fi
724
725# Test Echo
726ECHOTEST=`echo -n "x"`
727if [ "$ECHOTEST" = "x" ]
728then
729    ECHOOPT="-n "
730    ECHOOPTC=""
731else
732    ECHOOPT=""
733    ECHOOPTC="\c"
734fi
735
736outputFile() {
737    if [ -f "$1" ]
738    then
739        eval echo `gettext '  $1  Found but not executable.'`;
740    else
741        echo "  $1"
742    fi
743}
744
745# Check if the first parameter is an existing executable file.
746detectWrapperBinary() {
747    if [ -f "$1" ] ; then
748        WRAPPER_TEST_CMD="$1"
749        if [ ! -x "$WRAPPER_TEST_CMD" ] ; then
750            chmod +x "$WRAPPER_TEST_CMD" 2>/dev/null
751        fi
752        if [ -x "$WRAPPER_TEST_CMD" ] ; then
753            WRAPPER_CMD="$WRAPPER_TEST_CMD"
754        else
755            outputFile "$WRAPPER_TEST_CMD"
756            WRAPPER_TEST_CMD=""
757        fi
758    fi
759}
760
761# Decide on the wrapper binary to use.
762# If the bits of the OS could be detected, we will try to look for the
763#  binary with the correct bits value.  If it doesn't exist, fall back
764#  and look for the 32-bit binary.  If that doesn't exist either then
765#  look for the default.
766WRAPPER_TEST_CMD=""
767
768BIN_BITS=$DIST_BITS
769if [ ! "$BIN_BITS" = "32" ] ; then
770    # On a 64-bit platform, both Wrapper 32-Bit and 64-Bit can be used.
771    #  Send a request to the Wrapper to know if the license has the 64-bit feature.
772    WRAPPER_CMD_ORIG=$WRAPPER_CMD
773    detectWrapperBinary "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$BIN_BITS"
774    if [ ! -z "$WRAPPER_TEST_CMD" ] ; then
775        if [ "$COMMAND" = "console" -o "$COMMAND" = "start" -o "$COMMAND" = "restart" -o "$COMMAND" = "condrestart" -o "$COMMAND" = "installstart" ] ; then
776            "$WRAPPER_CMD" --request_delta_binary_bits "$WRAPPER_CONF" 2>/dev/null
777            if [ $? = 32 ] ; then
778                # License is 32-Bit. Reset and search for 32-Bit Wrapper binaries.
779                WRAPPER_TEST_CMD=""
780                WRAPPER_CMD=$WRAPPER_CMD_ORIG
781                BIN_BITS=32
782            fi
783        fi
784    fi
785fi
786
787if [ -z "$WRAPPER_TEST_CMD" ] ; then
788    detectWrapperBinary "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
789fi
790if [ -z "$WRAPPER_TEST_CMD" ] ; then
791    detectWrapperBinary "$WRAPPER_CMD"
792fi
793
794
795if [ -z "$WRAPPER_TEST_CMD" ] ; then
796    eval echo `gettext 'Unable to locate any of the following binaries:'`
797    outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$BIN_BITS"
798    if [ ! "$BIN_BITS" = "32" ] ; then
799        outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
800    fi
801    outputFile "$WRAPPER_CMD"
802
803    exit 1
804fi
805
806
807# Build the nice clause
808if [ "X$PRIORITY" = "X" ]
809then
810    CMDNICE=""
811else
812    CMDNICE="nice -$PRIORITY"
813fi
814
815# Build the anchor file clause.
816if [ "X$IGNORE_SIGNALS" = "X" ]
817then
818   ANCHORPROP=
819   IGNOREPROP=
820else
821   ANCHORPROP=wrapper.anchorfile=\"$ANCHORFILE\"
822   IGNOREPROP=wrapper.ignore_signals=TRUE
823fi
824
825# Build the status file clause.
826if [ "X$DETAIL_STATUS$WAIT_FOR_STARTED_STATUS" = "X" ]
827then
828   STATUSPROP=
829else
830   STATUSPROP="wrapper.statusfile=\"$STATUSFILE\" wrapper.java.statusfile=\"$JAVASTATUSFILE\""
831fi
832
833# Build the command file clause.
834if [ -n "$PAUSABLE" ]
835then
836    if [ "$PAUSABLE_MODE" = "file" ] ; then
837        COMMANDPROP="wrapper.commandfile=\"$COMMANDFILE\" wrapper.pausable=TRUE"
838    else
839        COMMANDPROP="wrapper.signal.mode.usr1=PAUSE wrapper.signal.mode.usr2=RESUME wrapper.pausable=TRUE"
840    fi
841else
842   COMMANDPROP=
843fi
844
845if [ ! -n "$WAIT_FOR_STARTED_STATUS" ]
846then
847    WAIT_FOR_STARTED_STATUS=true
848fi
849
850if [ $WAIT_FOR_STARTED_STATUS = true ] ; then
851    DETAIL_STATUS=true
852fi
853
854
855# Build the lock file clause.  Only create a lock file if the lock directory exists on this platform.
856LOCKPROP=
857if [ -d $LOCKDIR ]
858then
859    if [ -w $LOCKDIR ]
860    then
861        LOCKPROP=wrapper.lockfile=\"$LOCKFILE\"
862    fi
863fi
864
865# Build app name clause
866if [ ! -n "$APP_NAME_PASS_TO_WRAPPER" ]
867then
868    APP_NAME_PASS_TO_WRAPPER=true
869fi
870if [ $APP_NAME_PASS_TO_WRAPPER = false ]
871then
872   APPNAMEPROP=
873else
874   APPNAMEPROP="wrapper.name=\"$APP_NAME\" wrapper.displayname=\"$APP_LONG_NAME\""
875fi
876
877# Decide on run levels.
878RUN_LEVEL_S_DIST_OS_TMP=`eval "echo \$\{RUN_LEVEL_S_${DIST_OS}\}"`
879RUN_LEVEL_S_DIST_OS=`eval "echo ${RUN_LEVEL_S_DIST_OS_TMP}"`
880if [ "X${RUN_LEVEL_S_DIST_OS}" != "X" ] ; then
881    APP_RUN_LEVEL_S=${RUN_LEVEL_S_DIST_OS}
882elif [ "X$RUN_LEVEL_S" != "X" ] ; then
883    APP_RUN_LEVEL_S=$RUN_LEVEL_S
884else
885    APP_RUN_LEVEL_S=$RUN_LEVEL
886fi
887APP_RUN_LEVEL_S_CHECK=`echo "$APP_RUN_LEVEL_S" | sed "s/[(0-9)*]/0/g"`
888if [ "X${APP_RUN_LEVEL_S_CHECK}" != "X00" ] ; then
889    eval echo `gettext 'Run level \"${APP_RUN_LEVEL_S}\" must be numeric and have a length of two \(00-99\).'`
890    exit 1;
891fi
892RUN_LEVEL_K_DIST_OS_TMP=`eval "echo \$\{RUN_LEVEL_K_${DIST_OS}\}"`
893RUN_LEVEL_K_DIST_OS=`eval "echo ${RUN_LEVEL_K_DIST_OS_TMP}"`
894if [ "X${RUN_LEVEL_K_DIST_OS}" != "X" ] ; then
895    APP_RUN_LEVEL_K=${RUN_LEVEL_K_DIST_OS}
896elif [ "X$RUN_LEVEL_K" != "X" ] ; then
897    APP_RUN_LEVEL_K=$RUN_LEVEL_K
898else
899    APP_RUN_LEVEL_K=$RUN_LEVEL
900fi
901APP_RUN_LEVEL_K_CHECK=`echo "$APP_RUN_LEVEL_K" | sed "s/[(0-9)*]/0/g"`
902if [ "X${APP_RUN_LEVEL_K_CHECK}" != "X00" ] ; then
903    eval echo `gettext 'Run level \"${APP_RUN_LEVEL_K}\" must be numeric and have a length of two \(00-99\).'`
904    exit 1;
905fi
906
907prepAdditionalParams() {
908    ADDITIONAL_PARA=""
909    if [ "X${PASS_THROUGH}" = "Xapp_args" -o "X${PASS_THROUGH}" = "Xboth" ] ; then
910        if [ "X${PASS_THROUGH}" = "Xapp_args" ] ; then
911            ADDITIONAL_PARA="--"
912            ARGS_ARE_APP_PARAMS=true
913        fi
914        while [ -n "$1" ] ; do
915            if [ ! -n "$ARGS_ARE_APP_PARAMS" ] ; then
916                if [ "$1" = "--" ] ; then
917                    ARGS_ARE_APP_PARAMS=true
918                else
919                    # Check that the arg matches the pattern of a property
920                    case "$1" in
921                        wrapper.*=*)
922                            # Correct, nothing to do
923                            ;;
924                        *=*)
925                            # The property name is not starting with 'wrapper.' so invalid.
926                            COMMAND_PROP=${1%%=*}
927                            eval echo `gettext 'WARNING: Encountered an unknown configuration property \"${COMMAND_PROP}\". When PASS_THROUGH is set to \"both\", any argument before \"--\" should target a valid Wrapper configuration property.'`
928                            ;;
929                        *)
930                            # Not a valid assignment.
931                            eval echo `gettext 'WARNING: Encountered an invalid configuration property assignment \"$1\". When PASS_THROUGH is set to \"both\", any argument before \"--\" should be in the format \"\<property_name\>=\<value\>\".'`
932                            ;;
933                    esac
934                fi
935            fi
936            ADDITIONAL_PARA="$ADDITIONAL_PARA \"$1\""
937            shift
938        done
939    fi
940}
941
942resolveSysLocale() {
943    # First try to get the system encoding from /etc/default/locale.
944    # Note: For some reason, the system encoding stored in /etc/default/locale and the value returned by localectl may differ.
945    #       When using 'localectl set-local', /etc/default/locale is always updated accordingly, but when manually editing /etc/default/locale,
946    #       the output of the command sometimes doesn't get updated. When the values differ, SU uses the same locale as /etc/default/locale,
947    #       so that's why we start trying to get the locale using this method.
948    if [ -f "/etc/default/locale" ] ; then
949        PASS_SYS_LANG=`grep 'LANG=' /etc/default/locale`
950        case "$PASS_SYS_LANG" in
951            LANG=*)
952                PASS_SYS_LANG="$PASS_SYS_LANG "
953                ;;
954            *)
955                PASS_SYS_LANG=""
956                ;;
957        esac
958    fi
959    if [ "X$PASS_SYS_LANG" = "X" ] ; then
960        # Try to get the system encoding using localectl.
961        LOCALECTL_BIN="localectl"
962        result=`command -v $LOCALECTL_BIN 2>/dev/null`
963        if [ $? -ne 0 ] ; then
964            LOCALECTL_BIN="/usr/bin/localectl"
965            if [ ! -x "$LOCALECTL_BIN" ] ; then
966                # Keep the warning for debugging, but don't actually show it because it may be normal for some OS to not have the command.
967                # echo " WARNING: Could not locate localectl used to get the system locale. The encoding may not be correct when running as $RUN_AS_USER."
968                LOCALECTL_BIN=""
969            fi
970        fi
971        if [ "X$LOCALECTL_BIN" != "X" ] ; then
972            # The first line that localectl outputs should look like this: '    System Locale: n/a'
973            PASS_SYS_LANG=`$LOCALECTL_BIN | grep "System Locale" | awk '{print $3}' 2>/dev/null`
974            case "$PASS_SYS_LANG" in
975                *n/a*)
976                    # No system locale set. Skip.
977                    PASS_SYS_LANG=""
978                    ;;
979                LANG=*)
980                    PASS_SYS_LANG="$PASS_SYS_LANG "
981                    ;;
982                *)
983                    # echo " WARNING: Failed to parse the output of localectl. The encoding may not be correct when running as $RUN_AS_USER.'"
984                    PASS_SYS_LANG=""
985                    ;;
986            esac
987        fi
988    fi
989}
990
991resolveSuLocale() {
992    if [ "X$PASS_SU_LANG" = "X" -a "X$RUN_AS_USER" != "X" ] ; then
993        PASS_SU_LANG=`$SU_BIN - $RUN_AS_USER -c "locale" $SU_OPTS | grep "LANG=" 2>/dev/null`
994        if [ "X$PASS_SU_LANG" != "X" ] ; then
995            PASS_SU_LANG="$PASS_SU_LANG "
996        fi
997    fi
998}
999
1000checkUser() {
1001    # $1 touchLock flag
1002    # $2.. [command] args
1003
1004    # Check the configured user.  If necessary rerun this script as the desired user.
1005    if [ "X$RUN_AS_USER" != "X" ]
1006    then
1007        resolveCurrentUser
1008        if [ "$CURRENT_USER" = "$RUN_AS_USER" ]
1009        then
1010            # Already running as the configured user.  Avoid password prompts by not calling su.
1011            RUN_AS_USER=""
1012        fi
1013    fi
1014    if [ "X$RUN_AS_USER" != "X" ]
1015    then
1016        if [ "`$ID_BIN -u -n "$RUN_AS_USER" 2>/dev/null`" != "$RUN_AS_USER" ]
1017        then
1018            eval echo `gettext 'User $RUN_AS_USER does not exist.'`
1019            exit 1
1020        fi
1021
1022        # If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be
1023        # able to create the lock file.  The Wrapper will be able to update this file once it
1024        # is created but will not be able to delete it on shutdown.  If $1 is set then
1025        # the lock file should be created for the current command
1026        if [ "X$LOCKPROP" != "X" ]
1027        then
1028            if [ "X$1" != "X" ]
1029            then
1030                # Resolve the primary group
1031                RUN_AS_GROUP=`groups $RUN_AS_USER | awk '{print $3}' | tail -1`
1032                if [ "X$RUN_AS_GROUP" = "X" ]
1033                then
1034                    RUN_AS_GROUP=$RUN_AS_USER
1035                fi
1036                touch $LOCKFILE
1037                chown $RUN_AS_USER:$RUN_AS_GROUP $LOCKFILE
1038            fi
1039        fi
1040
1041        # Still want to change users, recurse.  This means that the user will only be
1042        #  prompted for a password once. Variables shifted by 1
1043        shift
1044
1045        # Wrap the parameters so they can be passed.
1046        ADDITIONAL_PARA=""
1047        while [ -n "$1" ] ; do
1048            if [ "$1" = 'installstart' ] ; then
1049                # At this point the service is already installed. When we will fork the process we only need to start the service.
1050                ADDITIONAL_PARA="$ADDITIONAL_PARA \"start\""
1051            else
1052                ADDITIONAL_PARA="$ADDITIONAL_PARA \"$1\""
1053            fi
1054            shift
1055        done
1056
1057        # runuser doesn't transmit signals (such as ctrl+c) to the Wrapper. This causes problem when running as a console.
1058        # When the Wrapper is launched as a service on RedHat, runuser should be used in preference to su.
1059        resolveSysLocale
1060        if [ -f "/sbin/runuser" -a $COMMAND != "console" ] ; then
1061            # Unlike su, runuser doesn't use the default system locale. Force passing it for consistency.
1062            if [ "X$PASS_SYS_LANG" = "X" ] ; then
1063                # Fallback to the same locale as when using SU (which should be the system encoding).
1064                resolveSuLocale
1065                PASS_SYS_LANG=$PASS_SU_LANG
1066            fi
1067            /sbin/runuser - $RUN_AS_USER -c "$PASS_SYS_LANG\"$REALPATH\" $ADDITIONAL_PARA" $SU_OPTS
1068        else
1069            # Normally SU should use the default system locale, but make sure it is the case.
1070            resolveSuLocale
1071            if [ "X$PASS_SU_LANG" != "X" -a "$PASS_SU_LANG" != "$PASS_SYS_LANG" ] ; then
1072                PASS_SU_LANG=$PASS_SYS_LANG
1073            else
1074                PASS_SU_LANG=""
1075            fi
1076            $SU_BIN - $RUN_AS_USER -c "$PASS_SU_LANG\"$REALPATH\" $ADDITIONAL_PARA" $SU_OPTS
1077        fi
1078        RUN_AS_USER_EXITCODE=$?
1079
1080        # we check if the previous command has failed
1081        if [ $RUN_AS_USER_EXITCODE -ne 0 ]
1082        then
1083             if [ $RUN_AS_USER_EXITCODE -eq 1 ]
1084             then
1085                 checkForkCommand
1086             else
1087                 eval echo `gettext 'Error executing the requested command with user \"$RUN_AS_USER\" \(error code $RUN_AS_USER_EXITCODE\).'`
1088                 echo ""
1089            fi
1090        fi
1091
1092        # Now that we are the original user again, we may need to clean up the lock file.
1093        if [ "X$LOCKPROP" != "X" ]
1094        then
1095            getpid
1096            if [ "X$pid" = "X" ]
1097            then
1098                # Wrapper is not running so make sure the lock file is deleted.
1099                if [ -f "$LOCKFILE" ]
1100                then
1101                    rm "$LOCKFILE"
1102                fi
1103            fi
1104        fi
1105
1106        exit $RUN_AS_USER_EXITCODE
1107    fi
1108}
1109
1110# Try to fork by executing a simple command.
1111# With this function, we want to make sure we are able to fork.
1112checkForkCommand() {
1113
1114    if [ -f "/sbin/runuser" -a $COMMAND != "console" ] ; then
1115        /sbin/runuser - $RUN_AS_USER -c "ls \"$REALPATH\"" $SU_OPTS > /dev/null 2>&1 &
1116    else
1117        $SU_BIN - $RUN_AS_USER -c "ls \"$REALPATH\"" $SU_OPTS > /dev/null 2>&1 &
1118    fi
1119    CHECK_EXITCODE=$?
1120
1121    if [ $CHECK_EXITCODE -ne 0 ]
1122    then
1123        # clearly a problem with forking
1124        eval echo `gettext 'Error: unable to create fork process.'`
1125        eval echo `gettext 'Advice:'`
1126        eval echo `gettext 'One possible cause of failure is when the user \(\"$RUN_AS_USER\"\) has no shell.'`
1127        eval echo `gettext 'In this case, two solutions are available by editing the script file:'`
1128        eval echo `gettext '1. Use \"SU_OPTS\" to set the shell for the user.'`
1129        eval echo `gettext '2. Use a OS service management tool \(only available on some platforms\).'`
1130        echo ""
1131    fi
1132}
1133
1134getpid() {
1135    pid=""
1136    if [ -f "$PIDFILE" ]
1137    then
1138        if [ -r "$PIDFILE" ]
1139        then
1140            pid=`cat "$PIDFILE"`
1141            if [ "X$pid" != "X" ]
1142            then
1143                if [ "X$PIDFILE_CHECK_PID" != "X" ]
1144                then
1145                    # It is possible that 'a' process with the pid exists but that it is not the
1146                    #  correct process.  This can happen in a number of cases, but the most
1147                    #  common is during system startup after an unclean shutdown.
1148                    # The ps statement below looks for the specific wrapper command running as
1149                    #  the pid.  If it is not found then the pid file is considered to be stale.
1150                    case "$DIST_OS" in
1151                        'freebsd'|'dragonfly')
1152                            pidtest=`$PS_BIN -p $pid -o args | tail -1`
1153                            if [ "X$pidtest" = "XCOMMAND" ]
1154                            then
1155                                pidtest=""
1156                            fi
1157                            ;;
1158                        'macosx')
1159                            pidtest=`$PS_BIN -ww -p $pid -o command | grep -F "$WRAPPER_CMD" | tail -1`
1160                            ;;
1161                        'solaris')
1162                            if [ -f "/usr/bin/pargs" ]
1163                            then
1164                                pidtest=`pargs $pid | fgrep "$WRAPPER_CMD" | tail -1`
1165                            else
1166                                case "$PS_BIN" in
1167                                    '/usr/ucb/ps')
1168                                        pidtest=`$PS_BIN -auxww $pid | fgrep "$WRAPPER_CMD" | tail -1`
1169                                        ;;
1170                                    '/usr/bin/ps')
1171                                        TRUNCATED_CMD=`$PS_BIN -o comm -p $pid | tail -1`
1172                                        COUNT=`echo $TRUNCATED_CMD | wc -m`
1173                                        COUNT=`echo ${COUNT}`
1174                                        COUNT=`expr $COUNT - 1`
1175                                        TRUNCATED_CMD=`echo $WRAPPER_CMD | cut -c1-$COUNT`
1176                                        pidtest=`$PS_BIN -o comm -p $pid | fgrep "$TRUNCATED_CMD" | tail -1`
1177                                        ;;
1178                                    '/bin/ps')
1179                                        TRUNCATED_CMD=`$PS_BIN -o comm -p $pid | tail -1`
1180                                        COUNT=`echo $TRUNCATED_CMD | wc -m`
1181                                        COUNT=`echo ${COUNT}`
1182                                        COUNT=`expr $COUNT - 1`
1183                                        TRUNCATED_CMD=`echo $WRAPPER_CMD | cut -c1-$COUNT`
1184                                        pidtest=`$PS_BIN -o comm -p $pid | fgrep "$TRUNCATED_CMD" | tail -1`
1185                                        ;;
1186                                    *)
1187                                        echo "Unsupported ps command $PS_BIN"
1188                                        exit 1
1189                                        ;;
1190                                esac
1191                            fi
1192                            ;;
1193                        'hpux')
1194                            pidtest=`$PS_BIN -p $pid -x -o args | grep -F "$WRAPPER_CMD" | tail -1`
1195                            ;;
1196                        'zos')
1197                            TRUNCATED_CMD=`$PS_BIN -p $pid -o args | tail -1`
1198                            COUNT=`echo $TRUNCATED_CMD | wc -m`
1199                            COUNT=`echo ${COUNT}`
1200                            COUNT=`expr $COUNT - 1`
1201                            TRUNCATED_CMD=`echo $WRAPPER_CMD | cut -c1-$COUNT`
1202                            pidtest=`$PS_BIN -p $pid -o args | grep -F "$TRUNCATED_CMD" | tail -1`
1203                            ;;
1204                        'linux')
1205                            pidtest=`$PS_BIN -ww -p $pid -o args | grep -F "$WRAPPER_CMD" | tail -1`
1206                            ;;
1207                        *)
1208                            pidtest=`$PS_BIN -p $pid -o args | grep -F "$WRAPPER_CMD" | tail -1`
1209                            ;;
1210                    esac
1211                else
1212                    # Check to see whether the pid exists as a running process, but in this mode, don't check what that pid is.
1213                    case "$DIST_OS" in
1214                        'solaris')
1215                            case "$PS_BIN" in
1216                                '/usr/ucb/ps')
1217                                    pidtest=`$PS_BIN $pid | grep "$pid" | awk '{print $1}' | tail -1`
1218                                    ;;
1219                                '/usr/bin/ps')
1220                                    pidtest=`$PS_BIN -p $pid -o pid | grep "$pid" | tail -1`
1221                                    ;;
1222                                '/bin/ps')
1223                                    pidtest=`$PS_BIN -p $pid -o pid | grep "$pid" | tail -1`
1224                                    ;;
1225                                *)
1226                                    echo "Unsupported ps command $PS_BIN"
1227                                    exit 1
1228                                    ;;
1229                            esac
1230                            ;;
1231                        *)
1232                            pidtest=`$PS_BIN -p $pid -o pid | grep "$pid" | tail -1`
1233                            ;;
1234                    esac
1235                fi
1236
1237                if [ "X$pidtest" = "X" ]
1238                then
1239                    # This is a stale pid file.
1240                    rm -f "$PIDFILE"
1241                    eval echo `gettext 'Removed stale pid file: $PIDFILE'`
1242                    pid=""
1243                fi
1244            fi
1245        else
1246            eval echo `gettext 'Cannot read $PIDFILE.'`
1247            exit 1
1248        fi
1249    fi
1250}
1251
1252getstatus() {
1253    STATUS=
1254    if [ -f "$STATUSFILE" ]
1255    then
1256        if [ -r "$STATUSFILE" ]
1257        then
1258            STATUS=`cat "$STATUSFILE"`
1259        fi
1260    fi
1261    if [ "X$STATUS" = "X" ]
1262    then
1263        STATUS="Unknown"
1264    fi
1265
1266    JAVASTATUS=
1267    if [ -f "$JAVASTATUSFILE" ]
1268    then
1269        if [ -r "$JAVASTATUSFILE" ]
1270        then
1271            JAVASTATUS=`cat "$JAVASTATUSFILE"`
1272        fi
1273    fi
1274    if [ "X$JAVASTATUS" = "X" ]
1275    then
1276        JAVASTATUS="Unknown"
1277    fi
1278}
1279
1280testpid() {
1281    case "$DIST_OS" in
1282     'solaris')
1283        case "$PS_BIN" in
1284        '/usr/ucb/ps')
1285            pid=`$PS_BIN  $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
1286            ;;
1287        '/usr/bin/ps')
1288            pid=`$PS_BIN -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
1289            ;;
1290        '/bin/ps')
1291            pid=`$PS_BIN -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
1292            ;;
1293        *)
1294            echo "Unsupported ps command $PS_BIN"
1295            exit 1
1296            ;;
1297        esac
1298        ;;
1299    *)
1300        pid=`$PS_BIN -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1` 2>/dev/null
1301        ;;
1302    esac
1303    if [ "X$pid" = "X" ]
1304    then
1305        pid=""
1306    fi
1307}
1308
1309launchdtrap() {
1310    stopit
1311    exit
1312}
1313
1314waitForWrapperStop() {
1315    getpid
1316    while [ "X$pid" != "X" ] ; do
1317        sleep 1
1318        getpid
1319    done
1320}
1321
1322launchinternal() {
1323    getpid
1324    trap launchdtrap TERM
1325    if [ "X$pid" = "X" ]
1326    then
1327        # The string passed to eval must handles spaces in paths correctly.
1328        COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.pidfile=\"$PIDFILE\" wrapper.daemonize=TRUE $APPNAMEPROP $ANCHORPROP $IGNOREPROP $STATUSPROP $COMMANDPROP $LOCKPROP wrapper.script.version=3.5.45 $ADDITIONAL_PARA"
1329        eval "$COMMAND_LINE"
1330    else
1331        eval echo `gettext '$APP_LONG_NAME is already running.'`
1332        exit 1
1333    fi
1334    # launchd expects that this script stay up and running so we need to do our own monitoring of the Wrapper process.
1335    if [ $WAIT_FOR_STARTED_STATUS = true ]
1336    then
1337        waitForWrapperStop
1338    fi
1339}
1340
1341console() {
1342    eval echo `gettext 'Running $APP_LONG_NAME...'`
1343    getpid
1344    if [ "X$pid" = "X" ]
1345    then
1346        trap '' 3
1347
1348        prepAdditionalParams "$@"
1349
1350        # The string passed to eval must handles spaces in paths correctly.
1351        COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.pidfile=\"$PIDFILE\" $APPNAMEPROP $ANCHORPROP $STATUSPROP $COMMANDPROP $LOCKPROP wrapper.script.version=3.5.45 $ADDITIONAL_PARA"
1352        eval "$COMMAND_LINE"
1353        COMMAND_EXIT_CODE=$?
1354        if [ $COMMAND_EXIT_CODE -ne 0 ] ; then
1355            exit $COMMAND_EXIT_CODE
1356        fi
1357    else
1358        eval echo `gettext '$APP_LONG_NAME is already running.'`
1359        exit 1
1360    fi
1361}
1362
1363waitforjavastartup() {
1364    getstatus
1365    eval echo $ECHOOPT `gettext 'Waiting for $APP_LONG_NAME...$ECHOOPTC'`
1366
1367    # Wait until the timeout or we have something besides Unknown.
1368    counter=15
1369    while [ "$JAVASTATUS" = "Unknown" -a $counter -gt 0 -a -n "$JAVASTATUS" ] ; do
1370        echo $ECHOOPT".$ECHOOPTC"
1371        sleep 1
1372        getstatus
1373        counter=`expr $counter - 1`
1374    done
1375
1376    if [ -n "$WAIT_FOR_STARTED_TIMEOUT" ] ; then
1377        counter=$WAIT_FOR_STARTED_TIMEOUT
1378    else
1379        counter=120
1380    fi
1381    while [ "$JAVASTATUS" != "STARTED" -a "$JAVASTATUS" != "Unknown" -a $counter -gt 0 -a -n "$JAVASTATUS" ] ; do
1382        echo $ECHOOPT".$ECHOOPTC"
1383        sleep 1
1384        getstatus
1385        counter=`expr $counter - 1`
1386    done
1387    echo ""
1388}
1389
1390##
1391# Request the path to the log file to the Wrapper and print it
1392#
1393# $1 prefix
1394printlogfilepath() {
1395    LOG_FILE=`"$WRAPPER_CMD" --request_log_file "$WRAPPER_CONF"`
1396    if [ $? = 0 -a "X$LOG_FILE" != "X" ] ; then
1397        LOG_FILE_MSG=`gettext 'See \"${LOG_FILE}\" for details.'`   # translate
1398        LOG_FILE_MSG=`eval echo "${LOG_FILE_MSG}"`                  # expand ${LOG_FILE}
1399        echo "$1${LOG_FILE_MSG}"                                    # print with indentation
1400    fi
1401}
1402
1403startwait() {
1404    if [ $WAIT_FOR_STARTED_STATUS = true ]
1405    then
1406        waitforjavastartup
1407    fi
1408    # Sleep for a few seconds to allow for intialization if required
1409    #  then test to make sure we're still running.
1410    #
1411    i=0
1412    while [ $i -lt $WAIT_AFTER_STARTUP ]
1413    do
1414        sleep 1
1415        echo $ECHOOPT".$ECHOOPTC"
1416        i=`expr $i + 1`
1417    done
1418    if [ $WAIT_AFTER_STARTUP -gt 0 -o $WAIT_FOR_STARTED_STATUS = true ]
1419    then
1420        getpid
1421        if [ "X$pid" = "X" ]
1422        then
1423            eval echo `gettext 'WARNING: $APP_LONG_NAME may have failed to start.'`
1424            printlogfilepath "         "
1425            exit 1
1426        else
1427            eval echo `gettext ' running: PID:$pid'`
1428        fi
1429    else
1430        echo ""
1431    fi
1432}
1433
1434mustBeRootOrExit() {
1435    checkIsRoot
1436    if [ "$IS_ROOT" != "true" ] ; then
1437        eval echo `gettext 'Must be root to perform this action.'`
1438        exit 1
1439    fi
1440}
1441
1442mustBeStoppedOrExit() {
1443    getpid
1444    if [ "X$pid" != "X" ] ; then
1445        eval echo `gettext '$APP_LONG_NAME is already running.'`
1446        exit 1
1447    fi
1448}
1449
1450# Detect if the Wrapper is running
1451# Returns 0 if the process is running, otherwise returns 1.
1452checkRunning() {
1453    getpid
1454    if [ "X$pid" = "X" ] ; then
1455        eval echo `gettext '$APP_LONG_NAME is not running.'`
1456        if [ "X$1" = "X1" ] ; then
1457            exit 1
1458        fi
1459        return 1
1460    fi
1461    return 0
1462}
1463
1464
1465macosxStart() {
1466    mustBeRootOrExit
1467    mustBeStoppedOrExit
1468
1469    eval echo `gettext 'Starting $APP_LONG_NAME with launchd...'`
1470
1471    # If the daemon was just installed, it may not be loaded.
1472    LOADED_PLIST=`launchctl list | grep ${APP_PLIST_BASE}`
1473    if [ "X${LOADED_PLIST}" = "X" ] ; then
1474        launchctl load /Library/LaunchDaemons/${APP_PLIST}
1475    fi
1476    # If launchd is set to run the daemon already at Load, we don't need to call start
1477    getpid
1478    if [ "X$pid" = "X" ] ; then
1479        launchctl start ${APP_PLIST_BASE}
1480    fi
1481
1482    startwait
1483}
1484
1485macosxStop() {
1486    mustBeRootOrExit
1487    # The daemon must be running.
1488    checkRunning "1"
1489
1490    eval echo `gettext 'Stopping $APP_LONG_NAME...'`
1491
1492    if [ "$MACOSX_KEEP_RUNNING" = "true" ] ; then
1493        echo ""
1494        eval echo `gettext 'Daemon is set to be kept continuously running and it will be automatically restarted.'`
1495        eval echo `gettext 'To stop the daemon you need to uninstall it.'`
1496        eval echo `gettext 'If you want to use the \"stop\" argument, you need to find MACOSX_KEEP_RUNNING'`
1497        eval echo `gettext 'at the beginning of the script file and set it to \"false\".'`
1498        echo ""
1499    fi
1500    launchctl stop ${APP_PLIST_BASE}
1501}
1502
1503macosxRestart() {
1504    mustBeRootOrExit
1505    checkRunning $1
1506
1507    eval echo `gettext 'Restarting $APP_LONG_NAME...'`
1508
1509    if [ "$MACOSX_KEEP_RUNNING" = "true" ] ; then
1510        # by stopping it, launchd will automatically restart it
1511        launchctl stop ${APP_PLIST_BASE}
1512    else
1513        launchctl unload "/Library/LaunchDaemons/${APP_PLIST}"
1514        sleep 1
1515        launchctl load   "/Library/LaunchDaemons/${APP_PLIST}"
1516    fi
1517
1518    startwait
1519}
1520
1521# Detect if upstart is installed
1522# Returns 0 if upstart is found, otherwise returns 1.
1523upstartDetection() {
1524    if [ ! -d "/etc/init" ] ; then
1525        return 1
1526    fi
1527
1528    INITCTL_BIN="initctl"
1529    result=`command -v $INITCTL_BIN 2>/dev/null`
1530    if [ $? -ne 0 ] ; then
1531        INITCTL_BIN="/sbin/initctl"
1532        if [ ! -x "$INITCTL_BIN" ] ; then
1533            return 1
1534        fi
1535    fi
1536
1537    result=`$INITCTL_BIN version 2>/dev/null`
1538    if [ $? -eq 0 ] ; then
1539        # if the word "upstart" is in the result, then we assume upstart is installed.
1540        result=`echo $result | grep upstart`
1541        if [ $? -eq 0 ] ; then
1542            return 0
1543        fi
1544    fi
1545
1546    # The command /sbin/initctl may fail if the user doesn't have enough privilege
1547    # but that doesn't mean upstart is not present.
1548    # In this case we can assume upstart exists if the service was previously
1549    # installed by the root user.
1550    if  [ -f "/etc/init/${APP_NAME}.conf" ] ; then
1551        return 0
1552    fi
1553
1554    return 1
1555}
1556
1557upstartInstall() {
1558    # Always verify that upstart exists.
1559    upstartDetection
1560    if [ $? -ne 0 ] ; then
1561        eval echo `gettext 'Unable to install the $APP_LONG_NAME daemon because upstart does not exist.'`
1562        return 1
1563    fi
1564
1565    eval echo `gettext ' Installing the $APP_LONG_NAME daemon with upstart...'`
1566    if [ -f "${REALDIR}/${APP_NAME}.install" ] ; then
1567        eval echo `gettext ' Custom upstart conf file ${APP_NAME}.install found.'`
1568        cp "${REALDIR}/${APP_NAME}.install" "/etc/init/${APP_NAME}.conf"
1569    else
1570        eval echo `gettext ' Creating default upstart conf file...'`
1571        echo "# ${APP_NAME} - ${APP_LONG_NAME}"                           > "/etc/init/${APP_NAME}.conf"
1572        echo "description \"${APP_LONG_NAME}\""                          >> "/etc/init/${APP_NAME}.conf"
1573        echo "author \"Tanuki Software Ltd. <info@tanukisoftware.com>\"" >> "/etc/init/${APP_NAME}.conf"
1574        echo "start on runlevel [2345]"                                  >> "/etc/init/${APP_NAME}.conf"
1575        echo "stop on runlevel [!2345]"                                  >> "/etc/init/${APP_NAME}.conf"
1576        echo "env LANG=${LANG}"                                          >> "/etc/init/${APP_NAME}.conf"
1577        echo "exec \"${REALPATH}\" upstartinternal"                      >> "/etc/init/${APP_NAME}.conf"
1578    fi
1579}
1580
1581upstartStart() {
1582    mustBeRootOrExit
1583    mustBeStoppedOrExit
1584
1585    eval echo `gettext 'Starting $APP_LONG_NAME with upstart...'`
1586
1587    /sbin/start ${APP_NAME}
1588
1589    startwait
1590}
1591
1592upstartStop() {
1593    mustBeRootOrExit
1594    # The daemon must be running.
1595    checkRunning "1"
1596
1597    eval echo `gettext 'Stopping $APP_LONG_NAME...'`
1598
1599    /sbin/stop ${APP_NAME}
1600}
1601
1602upstartRestart() {
1603    mustBeRootOrExit
1604    checkRunning $1
1605
1606    if [ "X$pid" = "X" ] ; then
1607        # Don't use /sbin/restart because it requires the daemon to be running.
1608        #  We want to match with other systems behaviour which is to restart the
1609        #  daemon even if it was not running.
1610        upstartStart
1611    else
1612        # The daemon was running
1613        eval echo `gettext 'Restarting $APP_LONG_NAME...'`
1614
1615        /sbin/restart ${APP_NAME}
1616
1617        startwait
1618    fi
1619}
1620
1621upstartRemove() {
1622    stopit "0"
1623    eval echo `gettext ' Removing the $APP_LONG_NAME daemon from upstart...'`
1624    rm "/etc/init/${APP_NAME}.conf"
1625}
1626
1627# Method to check if systemd is running.
1628# Returns 0 if systemd is found, otherwise returns 1.
1629systemdDetection() {
1630    if [ ! -d "/etc/systemd" ] ; then
1631        return 1
1632    fi
1633
1634    resolveLocation PIDOF_BIN pidof ";/bin;/usr/sbin" 1
1635    result=`$PIDOF_BIN systemd`
1636    return $?
1637}
1638
1639systemdInstall() {
1640    # Always verify that systemd exists.
1641    systemdDetection
1642    if [ $? -ne 0 ] ; then
1643      eval echo `gettext 'Unable to install the $APP_LONG_NAME daemon because systemd does not exist.'`
1644      return 1
1645    fi
1646
1647    eval echo `gettext ' Installing the $APP_LONG_NAME daemon with systemd...'`
1648    if [ -f "${REALDIR}/${APP_NAME}.service" ] ; then
1649        eval echo `gettext ' Custom service file ${APP_NAME}.service found.'`
1650        cp "${REALDIR}/${APP_NAME}.service" "${SYSTEMD_SERVICE_FILE}"
1651    else
1652        eval echo `gettext ' Creating default service file...'`
1653        echo "[Unit]"                            > "${SYSTEMD_SERVICE_FILE}"
1654        echo "Description=${APP_LONG_NAME}"     >> "${SYSTEMD_SERVICE_FILE}"
1655        echo "After=syslog.target"              >> "${SYSTEMD_SERVICE_FILE}"
1656        echo ""                                 >> "${SYSTEMD_SERVICE_FILE}"
1657        echo "[Service]"                        >> "${SYSTEMD_SERVICE_FILE}"
1658        echo "Type=forking"                     >> "${SYSTEMD_SERVICE_FILE}"
1659        case "${REALPATH}" in
1660            *\ *)
1661                # REALPATH contains spaces
1662                LINE_HEAD='ExecStart=/usr/bin/env "'
1663                LINE_TAIL='" start sysd'
1664                echo "${LINE_HEAD}${REALPATH}${LINE_TAIL}" >> "${SYSTEMD_SERVICE_FILE}"
1665                LINE_HEAD='ExecStop=/usr/bin/env "'
1666                LINE_TAIL='" stop sysd'
1667                echo "${LINE_HEAD}${REALPATH}${LINE_TAIL}" >> "${SYSTEMD_SERVICE_FILE}"
1668                ;;
1669            *)
1670                echo "ExecStart=${REALPATH} start sysd" >> "${SYSTEMD_SERVICE_FILE}"
1671                echo "ExecStop=${REALPATH} stop sysd"   >> "${SYSTEMD_SERVICE_FILE}"
1672                ;;
1673        esac
1674        if [ "X${RUN_AS_USER}" != "X" ] ; then
1675          echo "User=${RUN_AS_USER}"            >> "${SYSTEMD_SERVICE_FILE}"
1676        fi
1677        if [ "X${SYSTEMD_KILLMODE}" != "X" ] ; then
1678          echo "KillMode=${SYSTEMD_KILLMODE}"   >> "${SYSTEMD_SERVICE_FILE}"
1679        fi
1680        if [ $SYSTEMD_KILLMODE != "process" -a $SYSTEMD_KILLMODE != "none" ] ; then
1681          # Set an environment variable to show a warning if a detached process is launched by the WrapperManager.
1682          echo "Environment=SYSTEMD_KILLMODE_WARNING=true" >> "${SYSTEMD_SERVICE_FILE}"
1683        fi
1684        echo ""                                 >> "${SYSTEMD_SERVICE_FILE}"
1685        echo "[Install]"                        >> "${SYSTEMD_SERVICE_FILE}"
1686        echo "WantedBy=multi-user.target"       >> "${SYSTEMD_SERVICE_FILE}"
1687    fi
1688    systemctl daemon-reload
1689    systemctl enable "${APP_NAME}"
1690}
1691
1692systemdStart() {
1693    # Don't do mustBeRootOrExit because systemd will ask for the password when not root
1694    mustBeStoppedOrExit
1695
1696    result=`systemctl -p KillMode show $APP_NAME`
1697    if [ $result != "KillMode=$SYSTEMD_KILLMODE" ] ; then
1698        eval echo `gettext 'SYSTEMD_KILLMODE is set to \"${SYSTEMD_KILLMODE}\" on the top of the script, but the daemon is running with $result.'`
1699        eval echo `gettext 'The daemon must be reinstalled for the value of SYSTEMD_KILLMODE to take effect.'`
1700        exit 1
1701    fi
1702
1703    eval echo `gettext 'Starting $APP_LONG_NAME with systemd...'`
1704
1705    systemctl start $APP_NAME
1706    if [ $? -ne 0 ] ; then
1707        eval echo `gettext 'Failed to start $APP_LONG_NAME.'`
1708        printlogfilepath
1709        exit 1
1710    fi
1711
1712    startwait
1713}
1714
1715systemdStop() {
1716    # Don't do mustBeRootOrExit because systemd will ask for the password when not root
1717    # The daemon must be running.
1718    checkRunning "1"
1719
1720    eval echo `gettext 'Stopping $APP_LONG_NAME...'`
1721
1722    systemctl stop $APP_NAME
1723    if [ $? -ne 0 ] ; then
1724        eval echo `gettext 'Failed to stop $APP_LONG_NAME.'`
1725        exit 1
1726    fi
1727}
1728
1729systemdRestart() {
1730    # Don't do mustBeRootOrExit because systemd will ask for the password when not root
1731    checkRunning $1
1732
1733    eval echo `gettext 'Restarting $APP_LONG_NAME...'`
1734
1735    systemctl restart $APP_NAME
1736    if [ $? -ne 0 ] ; then
1737        eval echo `gettext 'Failed to restart service $APP_NAME'`
1738        printlogfilepath
1739        exit 1
1740    fi
1741
1742    startwait
1743}
1744
1745systemdRemove() {
1746    stopit "0"
1747    eval echo `gettext ' Removing the $APP_LONG_NAME daemon from systemd...'`
1748    systemctl disable $APP_NAME
1749    rm "/etc/systemd/system/${APP_NAME}.service"
1750    systemctl daemon-reload
1751}
1752
1753# Method to check if SRC is running.
1754# Returns 0 if SRC is found, otherwise returns 1.
1755srcDetection() {
1756    # $PS_BIN has already been resolved at startup
1757    result=`$PS_BIN -A | grep srcmstr`
1758    return $?
1759}
1760
1761srcInstall() {
1762    # Always verify that SRC exists.
1763    srcDetection
1764    if [ $? -ne 0 ] ; then
1765      eval echo `gettext 'Unable to install the $APP_LONG_NAME daemon because SRC does not exist.'`
1766      return 1
1767    fi
1768
1769    if [ "X$RUN_AS_USER" = "X" ] ; then
1770        USERID="0"
1771    else
1772        resolveIdLocation
1773        USERID=`$ID_BIN -u "$RUN_AS_USER"`
1774        if [ $? -ne 0 ] ; then
1775            eval echo `gettext 'Failed to get user id for $RUN_AS_USER'`
1776            exit 1
1777        fi
1778    fi
1779
1780    validateAppNameLength
1781    /usr/bin/mkssys -s "$APP_NAME" -p "$REALPATH" -a "launchdinternal" -u "$USERID" -f 9 -n 15 -S
1782    /usr/sbin/mkitab "$APP_NAME":2:once:"/usr/bin/startsrc -s \"${APP_NAME}\" >/dev/console 2>&1"
1783}
1784
1785srcStart() {
1786    mustBeRootOrExit
1787    mustBeStoppedOrExit
1788
1789    eval echo `gettext 'Starting $APP_LONG_NAME with SRC...'`
1790
1791    startsrc -s "${APP_NAME}"
1792    if [ $? -ne 0 ] ; then
1793        eval echo `gettext 'Failed to start $APP_LONG_NAME.'`
1794        printlogfilepath
1795        exit 1
1796    fi
1797
1798    startwait
1799}
1800
1801srcStop() {
1802    mustBeRootOrExit
1803    # The daemon must be running.
1804    checkRunning "1"
1805
1806    eval echo `gettext 'Stopping $APP_LONG_NAME...'`
1807
1808    stopsrc -s "${APP_NAME}"
1809    if [ $? -ne 0 ] ; then
1810        eval echo `gettext 'Failed to stop $APP_LONG_NAME.'`
1811        exit 1
1812    fi
1813}
1814
1815srcRestart() {
1816    mustBeRootOrExit
1817    checkRunning $1
1818
1819    if [ "X$pid" != "X" ] ; then
1820        # The daemon was running. Stop it first.
1821        eval echo `gettext 'Restarting $APP_LONG_NAME...'`
1822        srcStop
1823        waitForWrapperStop
1824    fi
1825    srcStart
1826}
1827
1828start() {
1829    mustBeStoppedOrExit
1830
1831    eval echo `gettext 'Starting $APP_LONG_NAME...'`
1832
1833    prepAdditionalParams "$@"
1834
1835    # The string passed to eval must handles spaces in paths correctly.
1836    COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.pidfile=\"$PIDFILE\" wrapper.daemonize=TRUE $APPNAMEPROP $ANCHORPROP $IGNOREPROP $STATUSPROP $COMMANDPROP $LOCKPROP wrapper.script.version=3.5.45 $ADDITIONAL_PARA"
1837    eval "$COMMAND_LINE"
1838
1839    startwait
1840}
1841
1842stopit() {
1843    # $1 exit if down flag
1844    checkRunning $1
1845    if [ $? -eq 0 ] ; then
1846        # The daemon should be running.
1847        eval echo `gettext 'Stopping $APP_LONG_NAME...'`
1848
1849        if [ "X$IGNORE_SIGNALS" = "X" ]
1850        then
1851            # Running so try to stop it.
1852            kill $pid
1853            if [ $? -ne 0 ]
1854            then
1855                # An explanation for the failure should have been given
1856                eval echo `gettext 'Unable to stop $APP_LONG_NAME.'`
1857                exit 1
1858            fi
1859        else
1860            rm -f "$ANCHORFILE"
1861            if [ -f "$ANCHORFILE" ]
1862            then
1863                # An explanation for the failure should have been given
1864                eval echo `gettext 'Unable to stop $APP_LONG_NAME.'`
1865                exit 1
1866            fi
1867        fi
1868
1869        # We can not predict how long it will take for the wrapper to
1870        #  actually stop as it depends on settings in wrapper.conf.
1871        #  Loop until it does.
1872        savepid=$pid
1873        CNT=0
1874        TOTCNT=0
1875        while [ "X$pid" != "X" ]
1876        do
1877            # Show a waiting message every 5 seconds.
1878            if [ "$CNT" -lt "5" ]
1879            then
1880                CNT=`expr $CNT + 1`
1881            else
1882                eval echo `gettext 'Waiting for $APP_LONG_NAME to exit...'`
1883                CNT=0
1884            fi
1885            TOTCNT=`expr $TOTCNT + 1`
1886
1887            sleep 1
1888
1889            # Check if the process is still running.
1890            testpid
1891
1892            if [ "X$pid" = "X" ]
1893                then
1894                # The process is gone, but it is possible that another instance restarted while we waited...
1895                SAVE_PIDFILE_CHECK_PID=$PIDFILE_CHECK_PID
1896                PIDFILE_CHECK_PID=""
1897                getpid
1898                PIDFILE_CHECK_PID=$SAVE_PIDFILE_CHECK_PID
1899
1900                if [ "X$pid" != "X" ]
1901                then
1902                    # Another process is running.
1903                    if [ "$pid" = "$savepid" ]
1904                    then
1905                        # This should never happen, unless the PID was recycled?
1906                        eval echo `gettext 'Failed to stop $APP_LONG_NAME.'`
1907                        exit 1
1908                    else
1909                        eval echo `gettext 'The content of $PIDFILE has changed.'`
1910                        eval echo `gettext 'Another instance of the Wrapper might have started in the meantime.'`
1911
1912                        # Exit now. Any further actions might compromise the running instance.
1913                        exit 1
1914                    fi
1915                fi
1916            fi
1917        done
1918
1919        eval echo `gettext 'Stopped $APP_LONG_NAME.'`
1920    fi
1921}
1922
1923pause() {
1924    getpid
1925    if [ "X$pid" = "X" ] ; then
1926        # The application is not running, print the status
1927        status
1928    else
1929        if [ "$PAUSABLE_MODE" = "file" ] ; then
1930            echo "PAUSE" >> $COMMANDFILE
1931            if [ $? != 0 ] ; then
1932                eval echo `gettext 'Failed to write in the command file to pause $APP_LONG_NAME.'`
1933                exit 1
1934            fi
1935        else
1936            # send a SIGUSR1 (use constants because numbers change depending on the platform)
1937            kill -USR1 $pid 2>/dev/null
1938            if [ $? != 0 ] ; then
1939                kill -SIGUSR1 $pid
1940                if [ $? != 0 ] ; then
1941                    eval echo `gettext 'Failed to send a signal to pause $APP_LONG_NAME.'`
1942                    exit 1
1943                fi
1944            fi
1945        fi
1946        eval echo `gettext 'Pausing $APP_LONG_NAME.'`
1947    fi
1948}
1949
1950resume() {
1951    getpid
1952    if [ "X$pid" = "X" ] ; then
1953        # The application is not running, print the status
1954        status
1955    else
1956        if [ "$PAUSABLE_MODE" = "file" ] ; then
1957            echo "RESUME" >> $COMMANDFILE
1958            if [ $? != 0 ] ; then
1959                eval echo `gettext 'Failed to write in the command file to resume $APP_LONG_NAME.'`
1960                exit 1
1961            fi
1962        else
1963            # send a SIGUSR2 (use constants because numbers change depending on the platform)
1964            kill -USR2 $pid 2>/dev/null
1965            if [ $? != 0 ] ; then
1966                kill -SIGUSR2 $pid
1967                if [ $? != 0 ] ; then
1968                    eval echo `gettext 'Failed to send a signal to resume $APP_LONG_NAME.'`
1969                    exit 1
1970                fi
1971            fi
1972        fi
1973        eval echo `gettext 'Resuming $APP_LONG_NAME.'`
1974    fi
1975}
1976
1977checkInstalled() {
1978    # Install status
1979    installedStatus=$SERVICE_NOT_INSTALLED
1980    installedWith=""
1981
1982    if [ "$DIST_OS" = "solaris" ] ; then
1983        if [ -f "/etc/init.d/$APP_NAME" -o -L "/etc/init.d/$APP_NAME" ] ; then
1984            installedStatus=$SERVICE_INSTALLED_DEFAULT
1985        fi
1986    elif [ "$DIST_OS" = "linux" ] ; then
1987        if [ "X$1" != "Xstrict" -o \( -z "$USE_SYSTEMD" -a -z "$USE_UPSTART" \) ] ; then
1988            if [ -f "/etc/init.d/$APP_NAME" -o -L "/etc/init.d/$APP_NAME" ] ; then
1989                installedStatus=$SERVICE_INSTALLED_DEFAULT
1990                installedWith="init.d"
1991            fi
1992        fi
1993        if [ "X$1" != "Xstrict" -o -n "$USE_SYSTEMD" ] ; then
1994            if [ -f "${SYSTEMD_SERVICE_FILE}" ] ; then
1995                installedStatus=`expr $installedStatus + $SERVICE_INSTALLED_SYSTEMD`
1996                installedWith="${installedWith}${installedWith:+, }systemd"
1997            fi
1998        fi
1999        if [ "X$1" != "Xstrict" -o -n "$USE_UPSTART" ] ; then
2000            if [ -f "/etc/init/${APP_NAME}.conf" ] ; then
2001                installedStatus=`expr $installedStatus + $SERVICE_INSTALLED_UPSTART`
2002                installedWith="${installedWith}${installedWith:+, }upstart"
2003            fi
2004        fi
2005    elif [ "$DIST_OS" = "hpux" ] ; then
2006        if [ -f "/sbin/init.d/$APP_NAME" -o -L "/sbin/init.d/$APP_NAME" ] ; then
2007            installedStatus=$SERVICE_INSTALLED_DEFAULT
2008        fi
2009    elif [ "$DIST_OS" = "aix" ] ; then
2010        if [ "X$1" != "Xstrict" -o -z "$USE_SRC" ] ; then
2011            if [ -f "/etc/rc.d/init.d/$APP_NAME" -o -L "/etc/rc.d/init.d/$APP_NAME" ] ; then
2012                installedStatus=$SERVICE_INSTALLED_DEFAULT
2013                installedWith="init.d"
2014            fi
2015        fi
2016        if [ "X$1" != "Xstrict" -o -n "$USE_SRC" ] ; then
2017            validateAppNameLength
2018            if [ "$IS_ROOT" = "true" ] ; then
2019                # Check both lssrc & lsitab to make sure the installation is complete. lsitab requires root privileges.
2020                # We will go through this case before installing or removing a service, so we can redo a clean installation
2021                # or a clean removal if installedStatus=5
2022                if [ -n "`/usr/sbin/lsitab $APP_NAME`" -a -n "`/usr/bin/lssrc -S -s $APP_NAME`" ] ; then
2023                    installedStatus=`expr $installedStatus + $SERVICE_INSTALLED_SRC`
2024                    installedWith="${installedWith}${installedWith:+, }SRC"
2025                elif [ -n "`/usr/sbin/lsitab $APP_NAME`" -o -n "`/usr/bin/lssrc -S -s $APP_NAME`" ] ; then
2026                    installedStatus=`expr $installedStatus + $SERVICE_INSTALLED_SRC_PARTIAL`
2027                    installedWith="${installedWith}${installedWith:+, }SRC"
2028                fi
2029            else
2030                # Using lssrc is enough to test normal installations and doesn't require root privileges
2031                if [ -n "`/usr/bin/lssrc -S -s $APP_NAME`" ] ; then
2032                    installedStatus=`expr $installedStatus + $SERVICE_INSTALLED_SRC`
2033                    installedWith="${installedWith}${installedWith:+, }SRC"
2034                fi
2035            fi
2036        fi
2037    elif [ "$DIST_OS" = "freebsd" ] ; then
2038        if [ -f "/etc/rc.d/$APP_NAME" -o -L "/etc/rc.d/$APP_NAME" ] ; then
2039            installedStatus=$SERVICE_INSTALLED_DEFAULT
2040        fi
2041    elif [ "$DIST_OS" = "macosx" ] ; then
2042        if [ -f "/Library/LaunchDaemons/${APP_PLIST}" -o -L "/Library/LaunchDaemons/${APP_PLIST}" ] ; then
2043            installedStatus=$SERVICE_INSTALLED_DEFAULT
2044        fi
2045    elif [ "$DIST_OS" = "zos" ] ; then
2046        if [ -f /etc/rc.bak ] ; then
2047            installedStatus=$SERVICE_INSTALLED_DEFAULT
2048        fi
2049    fi
2050}
2051
2052status() {
2053    checkInstalled
2054    getpid
2055    if [ "X$pid" = "X" ]
2056    then
2057        if [ $installedStatus -eq $SERVICE_NOT_INSTALLED ] ; then
2058            eval echo `gettext '$APP_LONG_NAME \(not installed\) is not running.'`
2059        elif [ "X$installedWith" = "X" ] ; then
2060            eval echo `gettext '$APP_LONG_NAME \(installed\) is not running.'`
2061        else
2062            eval echo `gettext '$APP_LONG_NAME \(installed with $installedWith\) is not running.'`
2063        fi
2064        exit 1
2065    else
2066        if [ "X$DETAIL_STATUS" = "X" ]
2067        then
2068            if [ $installedStatus -eq $SERVICE_NOT_INSTALLED ] ; then
2069                eval echo `gettext '$APP_LONG_NAME \(not installed\) is running: PID:$pid'`
2070            elif [ "X$installedWith" = "X" ] ; then
2071                eval echo `gettext '$APP_LONG_NAME \(installed\) is running: PID:$pid'`
2072            else
2073                eval echo `gettext '$APP_LONG_NAME \(installed with $installedWith\) is running: PID:$pid'`
2074            fi
2075        else
2076            getstatus
2077            if [ $installedStatus -eq $SERVICE_NOT_INSTALLED ] ; then
2078                eval echo `gettext '$APP_LONG_NAME \(not installed\) is running: PID:$pid, Wrapper:$STATUS, Java:$JAVASTATUS'`
2079            elif [ "X$installedWith" = "X" ] ; then
2080                eval echo `gettext '$APP_LONG_NAME \(installed\) is running: PID:$pid, Wrapper:$STATUS, Java:$JAVASTATUS'`
2081            else
2082                eval echo `gettext '$APP_LONG_NAME \(installed with $installedWith\) is running: PID:$pid, Wrapper:$STATUS, Java:$JAVASTATUS'`
2083            fi
2084        fi
2085        exit 0
2086    fi
2087}
2088
2089# Make sure APP_NAME is less than 14 characters, otherwise in AIX, the commands
2090# "lsitab" or "lssrc" will fail
2091validateAppNameLength() {
2092    if [ ${#APP_NAME} -gt 14 ] ; then
2093        eval echo `gettext ' APP_NAME must be less than 14 characters long. Length of ${APP_NAME} is ${#APP_NAME}.'`
2094        exit 1
2095    fi
2096}
2097
2098resolveInitdCommand() {
2099    # NOTE: update-rc.d & chkconfig are the recommended interfaces for managing
2100    #       init scripts. insserv is a low level tool used by these interfaces.
2101    #       chkconfig was available on old versions Ubuntu, but update-rc.d is
2102    #       preferred. chkconfig is used on RHEL based Linux.
2103
2104    # update-rc.d is used on distros such as Debian/Ubuntu
2105    resolveLocation INITD_COMMAND "update-rc.d" ";/usr/sbin" 0
2106    if [ $? -eq 0 ] ; then
2107        USE_UPDATE_RC=1
2108    else
2109        # chkconfig is used on distros such as RHEL or Amazon Linux
2110        resolveLocation INITD_COMMAND chkconfig ";/sbin" 0
2111        if [ $? -eq 0 ] ; then
2112            USE_CHKCONFIG=1
2113        else
2114            # if neither chkconfig nor update-rc.d are present, try insserv
2115            resolveLocation INITD_COMMAND insserv ";/sbin" 0
2116            if [ $? -eq 0 ] ; then
2117                USE_INSSERV=1
2118            else
2119                INITD_COMMAND=""
2120            fi
2121        fi
2122    fi
2123}
2124
2125installdaemon() {
2126    mustBeRootOrExit
2127
2128    checkInstalled
2129    APP_NAME_LOWER=`echo "$APP_NAME" | $TR_BIN "[A-Z]" "[a-z]"`
2130    if [ "$DIST_OS" = "solaris" ] ; then
2131        eval echo `gettext 'Detected Solaris:'`
2132        if [ $installedStatus -ne $SERVICE_NOT_INSTALLED ] ; then
2133            eval echo `gettext ' The $APP_LONG_NAME daemon is already installed.'`
2134        else
2135            eval echo `gettext ' Installing the $APP_LONG_NAME daemon...'`
2136            ln -s "$REALPATH" "/etc/init.d/$APP_NAME"
2137            for i in `ls "/etc/rc3.d/K"??"$APP_NAME_LOWER" "/etc/rc3.d/S"??"$APP_NAME_LOWER" 2>/dev/null` ; do
2138                eval echo `gettext ' Removing unexpected file before proceeding: $i'`
2139                rm -f $i
2140            done
2141            ln -s "/etc/init.d/$APP_NAME" "/etc/rc3.d/K${APP_RUN_LEVEL_K}$APP_NAME_LOWER"
2142            ln -s "/etc/init.d/$APP_NAME" "/etc/rc3.d/S${APP_RUN_LEVEL_S}$APP_NAME_LOWER"
2143        fi
2144    elif [ "$DIST_OS" = "linux" ] ; then
2145        eval echo `gettext 'Detected Linux:'`
2146        if [ $installedStatus -ne $SERVICE_NOT_INSTALLED ] ; then
2147            eval echo `gettext ' The $APP_LONG_NAME daemon is already installed with $installedWith.'`
2148        elif [ -n "$USE_SYSTEMD" ] ; then
2149            systemdInstall
2150        elif [ -n "$USE_UPSTART" ] ; then
2151            upstartInstall
2152        else
2153            resolveInitdCommand
2154            if [ -n "$USE_CHKCONFIG" ] ; then
2155                eval echo `gettext ' Installing the $APP_LONG_NAME daemon with init.d \($INITD_COMMAND\)...'`
2156                ln -s "$REALPATH" "/etc/init.d/$APP_NAME"
2157                $INITD_COMMAND --add "$APP_NAME"
2158                $INITD_COMMAND "$APP_NAME" on
2159            elif [ "$USE_INSSERV" ] ; then
2160                eval echo `gettext ' Installing the $APP_LONG_NAME daemon with init.d \($INITD_COMMAND\)...'`
2161                ln -s "$REALPATH" "/etc/init.d/$APP_NAME"
2162                $INITD_COMMAND "/etc/init.d/$APP_NAME"
2163            elif [ "$USE_UPDATE_RC" ] ; then
2164                eval echo `gettext ' Installing the $APP_LONG_NAME daemon with init.d \($INITD_COMMAND\)...'`
2165                ln -s "$REALPATH" "/etc/init.d/$APP_NAME"
2166                $INITD_COMMAND "$APP_NAME" defaults
2167            else
2168                eval echo `gettext ' Installing the $APP_LONG_NAME daemon with init.d...'`
2169                ln -s "$REALPATH" /etc/init.d/$APP_NAME
2170                for i in `ls "/etc/rc3.d/K"??"$APP_NAME_LOWER" "/etc/rc5.d/K"??"$APP_NAME_LOWER" "/etc/rc3.d/S"??"$APP_NAME_LOWER" "/etc/rc5.d/S"??"$APP_NAME_LOWER" 2>/dev/null` ; do
2171                    eval echo `gettext ' Removing unexpected file before proceeding: $i'`
2172                    rm -f $i
2173                done
2174                ln -s "/etc/init.d/$APP_NAME" "/etc/rc3.d/K${APP_RUN_LEVEL_K}$APP_NAME_LOWER"
2175                ln -s "/etc/init.d/$APP_NAME" "/etc/rc3.d/S${APP_RUN_LEVEL_S}$APP_NAME_LOWER"
2176                ln -s "/etc/init.d/$APP_NAME" "/etc/rc5.d/S${APP_RUN_LEVEL_S}$APP_NAME_LOWER"
2177                ln -s "/etc/init.d/$APP_NAME" "/etc/rc5.d/K${APP_RUN_LEVEL_K}$APP_NAME_LOWER"
2178            fi
2179        fi
2180    elif [ "$DIST_OS" = "hpux" ] ; then
2181        eval echo `gettext 'Detected HP-UX:'`
2182        if [ $installedStatus -ne $SERVICE_NOT_INSTALLED ] ; then
2183            eval echo `gettext ' The $APP_LONG_NAME daemon is already installed.'`
2184        else
2185            eval echo `gettext ' Installing the $APP_LONG_NAME daemon...'`
2186            ln -s "$REALPATH" "/sbin/init.d/$APP_NAME"
2187            for i in `ls "/sbin/rc3.d/K"??"$APP_NAME_LOWER" "/sbin/rc3.d/S"??"$APP_NAME_LOWER" 2>/dev/null` ; do
2188                eval echo `gettext ' Removing unexpected file before proceeding: $i'`
2189                rm -f $i
2190            done
2191            ln -s "/sbin/init.d/$APP_NAME" "/sbin/rc3.d/K${APP_RUN_LEVEL_K}$APP_NAME_LOWER"
2192            ln -s "/sbin/init.d/$APP_NAME" "/sbin/rc3.d/S${APP_RUN_LEVEL_S}$APP_NAME_LOWER"
2193        fi
2194    elif [ "$DIST_OS" = "aix" ] ; then
2195        eval echo `gettext 'Detected AIX:'`
2196        if [ $installedStatus -ne $SERVICE_NOT_INSTALLED -a $installedStatus -ne $SERVICE_INSTALLED_SRC_PARTIAL ] ; then
2197            eval echo `gettext ' The $APP_LONG_NAME daemon is already installed with $installedWith.'`
2198        else
2199            eval echo `gettext ' Installing the $APP_LONG_NAME daemon...'`
2200            if [ -n "`/usr/sbin/lsitab install_assist`" ] ; then
2201                eval echo `gettext ' The task /usr/sbin/install_assist was found in the inittab, this might cause problems for all subsequent tasks to launch as this process is known to block the init task. Please make sure this task is not needed anymore and remove/deactivate it.'`
2202            fi
2203
2204            for i in `ls "/etc/rc.d/rc2.d/K"??"$APP_NAME_LOWER" "/etc/rc.d/rc2.d/S"??"$APP_NAME_LOWER" 2>/dev/null` ; do
2205                eval echo `gettext ' Removing unexpected file before proceeding: $i'`
2206                rm -f $i
2207            done
2208
2209            if [ -n "$USE_SRC" ] ; then
2210                srcInstall
2211            else
2212                # install using initd
2213                ln -s "$REALPATH" "/etc/rc.d/init.d/$APP_NAME"
2214                ln -s "/etc/rc.d/init.d/$APP_NAME" "/etc/rc.d/rc2.d/K${APP_RUN_LEVEL_K}$APP_NAME_LOWER"
2215                ln -s "/etc/rc.d/init.d/$APP_NAME" "/etc/rc.d/rc2.d/S${APP_RUN_LEVEL_S}$APP_NAME_LOWER"
2216            fi
2217        fi
2218    elif [ "$DIST_OS" = "freebsd" -o "$DIST_OS" = "dragonfly" ] ; then
2219        eval echo `gettext 'Detected FreeBSD/DragonFly:'`
2220        if [ $installedStatus -ne $SERVICE_NOT_INSTALLED ] ; then
2221            eval echo `gettext ' The $APP_LONG_NAME daemon is already installed.'`
2222        else
2223            eval echo `gettext ' Installing the $APP_LONG_NAME daemon...'`
2224            sed -i.bak "/${APP_NAME}_enable=\"YES\"/d" /etc/rc.conf
2225            if [ -f "${REALDIR}/${APP_NAME}.install" ] ; then
2226                ln -s "${REALDIR}/${APP_NAME}.install" "/etc/rc.d/$APP_NAME"
2227            else
2228                echo '#!/bin/sh'                    > "/etc/rc.d/$APP_NAME"
2229                echo "#"                           >> "/etc/rc.d/$APP_NAME"
2230                echo "# PROVIDE: $APP_NAME"        >> "/etc/rc.d/$APP_NAME"
2231                echo "# REQUIRE: NETWORKING"       >> "/etc/rc.d/$APP_NAME"
2232                echo "# KEYWORD: shutdown"         >> "/etc/rc.d/$APP_NAME"
2233                echo ". /etc/rc.subr"              >> "/etc/rc.d/$APP_NAME"
2234                echo "name=\"$APP_NAME\""          >> "/etc/rc.d/$APP_NAME"
2235                echo "rcvar=\`set_rcvar\`"         >> "/etc/rc.d/$APP_NAME"
2236                echo "command=\"${REALPATH}\""     >> "/etc/rc.d/$APP_NAME"
2237                echo 'start_cmd="${name}_start"'   >> "/etc/rc.d/$APP_NAME"
2238                echo 'load_rc_config $name'        >> "/etc/rc.d/$APP_NAME"
2239                echo 'status_cmd="${name}_status"' >> "/etc/rc.d/$APP_NAME"
2240                echo 'stop_cmd="${name}_stop"'     >> "/etc/rc.d/$APP_NAME"
2241                echo "${APP_NAME}_status() {"      >> "/etc/rc.d/$APP_NAME"
2242                echo '${command} status'           >> "/etc/rc.d/$APP_NAME"
2243                echo '}'                           >> "/etc/rc.d/$APP_NAME"
2244                echo "${APP_NAME}_stop() {"        >> "/etc/rc.d/$APP_NAME"
2245                echo '${command} stop'             >> "/etc/rc.d/$APP_NAME"
2246                echo '}'                           >> "/etc/rc.d/$APP_NAME"
2247                echo "${APP_NAME}_start() {"       >> "/etc/rc.d/$APP_NAME"
2248                echo '${command} start'            >> "/etc/rc.d/$APP_NAME"
2249                echo '}'                           >> "/etc/rc.d/$APP_NAME"
2250                echo 'run_rc_command "$1"'         >> "/etc/rc.d/$APP_NAME"
2251            fi
2252            echo "${APP_NAME}_enable=\"YES\"" >> /etc/rc.conf
2253            chmod 555 "/etc/rc.d/$APP_NAME"
2254        fi
2255    elif [ "$DIST_OS" = "macosx" ] ; then
2256        eval echo `gettext 'Detected Mac OSX:'`
2257        if [ $installedStatus -ne $SERVICE_NOT_INSTALLED ] ; then
2258            eval echo `gettext ' The $APP_LONG_NAME daemon is already installed.'`
2259        else
2260            eval echo `gettext ' Installing the $APP_LONG_NAME daemon...'`
2261            if [ -f "${REALDIR}/${APP_PLIST}" ] ; then
2262                ln -s "${REALDIR}/${APP_PLIST}" "/Library/LaunchDaemons/${APP_PLIST}"
2263            else
2264                echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"                       > "/Library/LaunchDaemons/${APP_PLIST}"
2265                echo "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\"" >> "/Library/LaunchDaemons/${APP_PLIST}"
2266                echo "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"             >> "/Library/LaunchDaemons/${APP_PLIST}"
2267                echo "<plist version=\"1.0\">"                                         >> "/Library/LaunchDaemons/${APP_PLIST}"
2268                echo "    <dict>"                                                      >> "/Library/LaunchDaemons/${APP_PLIST}"
2269                echo "        <key>Label</key>"                                        >> "/Library/LaunchDaemons/${APP_PLIST}"
2270                echo "        <string>${APP_PLIST_BASE}</string>"                      >> "/Library/LaunchDaemons/${APP_PLIST}"
2271                echo "        <key>ProgramArguments</key>"                             >> "/Library/LaunchDaemons/${APP_PLIST}"
2272                echo "        <array>"                                                 >> "/Library/LaunchDaemons/${APP_PLIST}"
2273                echo "            <string>${REALPATH}</string>"                        >> "/Library/LaunchDaemons/${APP_PLIST}"
2274                echo "            <string>launchdinternal</string>"                    >> "/Library/LaunchDaemons/${APP_PLIST}"
2275                echo "        </array>"                                                >> "/Library/LaunchDaemons/${APP_PLIST}"
2276                echo "        <key>${KEY_KEEP_ALIVE}</key>"                            >> "/Library/LaunchDaemons/${APP_PLIST}"
2277                echo "        <${MACOSX_KEEP_RUNNING}/>"                               >> "/Library/LaunchDaemons/${APP_PLIST}"
2278                echo "        <key>RunAtLoad</key>"                                    >> "/Library/LaunchDaemons/${APP_PLIST}"
2279                echo "        <true/>"                                                 >> "/Library/LaunchDaemons/${APP_PLIST}"
2280                if [ "X$RUN_AS_USER" != "X" ] ; then
2281                    echo "        <key>UserName</key>"                                 >> "/Library/LaunchDaemons/${APP_PLIST}"
2282                    echo "        <string>${RUN_AS_USER}</string>"                     >> "/Library/LaunchDaemons/${APP_PLIST}"
2283                fi
2284                echo "    </dict>"                                                     >> "/Library/LaunchDaemons/${APP_PLIST}"
2285                echo "</plist>"                                                        >> "/Library/LaunchDaemons/${APP_PLIST}"
2286            fi
2287            chmod 555 "/Library/LaunchDaemons/${APP_PLIST}"
2288        fi
2289    elif [ "$DIST_OS" = "zos" ] ; then
2290        eval echo `gettext 'Detected z/OS:'`
2291        if [ $installedStatus -ne $SERVICE_NOT_INSTALLED ] ; then
2292            eval echo `gettext ' The $APP_LONG_NAME daemon is already installed.'`
2293        else
2294            eval echo `gettext ' Installing the $APP_LONG_NAME daemon...'`
2295            cp /etc/rc /etc/rc.bak
2296            sed  "s:echo /etc/rc script executed, \`date\`::g" /etc/rc.bak > /etc/rc
2297            echo "_BPX_JOBNAME='${APP_NAME}' \"${REALDIR}/${APP_NAME}\" start" >>/etc/rc
2298            echo '/etc/rc script executed, `date`' >>/etc/rc
2299        fi
2300    else
2301        eval echo `gettext 'Install not currently supported for $DIST_OS.'`
2302        exit 1
2303    fi
2304
2305    # Exit with 1 if the daemon was already installed when calling installdaemon().
2306    #  The test below requires $installedStatus to be unchanged since the call to
2307    #  checkInstalled() at the beginning of the function. If the script was launched
2308    #  with 'installstart' we want to start normally, so don't exit here.
2309    if [ $installedStatus -ne $SERVICE_NOT_INSTALLED -a "$COMMAND" != 'installstart' ] ; then
2310        exit 1
2311    fi
2312
2313    # Check again the installation status. The script should exit with 1 if the service
2314    #  could not be installed (even if it was launched with 'installstart').
2315    checkInstalled
2316    if [ $installedStatus -eq $SERVICE_NOT_INSTALLED ] ; then
2317        eval echo `gettext 'Service not installed.'`
2318        exit 1
2319    fi
2320}
2321
2322startdaemon() {
2323    if [ "$DIST_OS" = "macosx" -a -f "/Library/LaunchDaemons/${APP_PLIST}" ] ; then
2324        macosxStart
2325    elif [ "$DIST_OS" = "linux" -a -n "$USE_UPSTART" ] && [ -f "/etc/init/${APP_NAME}.conf" ] ; then
2326        upstartStart
2327    elif [ "$DIST_OS" = "linux" -a -n "$USE_SYSTEMD" -a -z "$SYSD" ] && [ -f "${SYSTEMD_SERVICE_FILE}" ] ; then
2328        systemdStart
2329    elif [ "$DIST_OS" = "aix" -a -n "$USE_SRC" ] && [ -n "`/usr/bin/lssrc -S -s $APP_NAME`" ] ; then
2330        srcStart
2331    else
2332        if [ -n "$SYSD" ] ; then
2333            shift
2334        fi
2335
2336        checkUser touchlock "$@"
2337        if [ ! -n "$FIXED_COMMAND" ] ; then
2338            shift
2339        fi
2340
2341        if [ "$SERVICE_MANAGEMENT_TOOL" != "auto" -a "$SERVICE_MANAGEMENT_TOOL" != "initd" ] ; then
2342            eval echo `gettext 'Starting without using the configured service management tool \"$SERVICE_MANAGEMENT_TOOL\" because the service $APP_NAME is not installed.'`
2343        fi
2344
2345        start "$@"
2346    fi
2347}
2348
2349restartdaemon() {
2350    if [ "$DIST_OS" = "macosx" -a -f "/Library/LaunchDaemons/${APP_PLIST}" ] ; then
2351        macosxRestart $1
2352    elif [ "$DIST_OS" = "linux" -a -n "$USE_UPSTART" ] && [ -f "/etc/init/${APP_NAME}.conf" ] ; then
2353        upstartRestart $1
2354    elif [ "$DIST_OS" = "linux" -a -n "$USE_SYSTEMD" -a -z "$SYSD" ] && [ -f "${SYSTEMD_SERVICE_FILE}" ] ; then
2355        systemdRestart $1
2356    elif [ "$DIST_OS" = "aix" -a -n "$USE_SRC" ] && [ -n "`/usr/bin/lssrc -S -s $APP_NAME`" ] ; then
2357        srcRestart $1
2358    else
2359        checkUser touchlock "$COMMAND"
2360
2361        checkRunning $1
2362        if [ "X$pid" != "X" ] ; then
2363            # The daemon was running. Stop it first.
2364            eval echo `gettext 'Restarting $APP_LONG_NAME...'`
2365            stopit $1
2366        fi
2367        shift
2368        if [ ! -n "$FIXED_COMMAND" ] ; then
2369            shift
2370        fi
2371        start "$@"
2372    fi
2373}
2374
2375isBitSet() {
2376    if [ `expr \( $1 / $2 \) % 2` -eq 1 ]; then
2377        return 1
2378    else
2379        return 0
2380    fi
2381}
2382
2383removedaemon() {
2384    mustBeRootOrExit
2385
2386    checkInstalled
2387    APP_NAME_LOWER=`echo "$APP_NAME" | $TR_BIN "[A-Z]" "[a-z]"`
2388    if [ "$DIST_OS" = "solaris" ] ; then
2389        eval echo `gettext 'Detected Solaris:'`
2390        isBitSet $installedStatus $SERVICE_INSTALLED_DEFAULT
2391        if [ $? -eq 1 ] ; then
2392            stopit "0"
2393            eval echo `gettext ' Removing the $APP_LONG_NAME daemon...'`
2394            for i in `ls "/etc/rc3.d/K"??"$APP_NAME_LOWER" "/etc/rc3.d/S"??"$APP_NAME_LOWER" "/etc/init.d/$APP_NAME" 2>/dev/null` ; do
2395                rm -f $i
2396            done
2397        else
2398            eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
2399            exit 1
2400        fi
2401    elif [ "$DIST_OS" = "linux" ] ; then
2402        eval echo `gettext 'Detected Linux:'`
2403        isBitSet $installedStatus $SERVICE_INSTALLED_SYSTEMD
2404        if [ $? -eq 1 ] ; then
2405            systemdRemove
2406        fi
2407        isBitSet $installedStatus $SERVICE_INSTALLED_UPSTART
2408        if [ $? -eq 1 ] ; then
2409            upstartRemove
2410        fi
2411        isBitSet $installedStatus $SERVICE_INSTALLED_DEFAULT
2412        if [ $? -eq 1 ] ; then
2413            stopit "0"
2414            eval echo `gettext ' Removing the $APP_LONG_NAME daemon from init.d...'`
2415            resolveInitdCommand
2416            if [ -n "$USE_CHKCONFIG" ] ; then
2417                $INITD_COMMAND "$APP_NAME" off
2418                $INITD_COMMAND --del "$APP_NAME"
2419                rm -f "/etc/init.d/$APP_NAME"
2420            elif [ "$USE_INSSERV" ] ; then
2421                $INITD_COMMAND -r "/etc/init.d/$APP_NAME"
2422                rm -f "/etc/init.d/$APP_NAME"
2423            elif [ "$USE_UPDATE_RC" ] ; then
2424                $INITD_COMMAND -f "$APP_NAME" remove
2425                rm -f "/etc/init.d/$APP_NAME"
2426            else
2427                for i in `ls "/etc/rc3.d/K"??"$APP_NAME_LOWER" "/etc/rc5.d/K"??"$APP_NAME_LOWER" "/etc/rc3.d/S"??"$APP_NAME_LOWER" "/etc/rc5.d/S"??"$APP_NAME_LOWER" "/etc/init.d/$APP_NAME" 2>/dev/null` ; do
2428                    rm -f $i
2429                done
2430            fi
2431        fi
2432        if [ $installedStatus -eq $SERVICE_NOT_INSTALLED ] ; then
2433            eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
2434            exit 1
2435        fi
2436    elif [ "$DIST_OS" = "hpux" ] ; then
2437        eval echo `gettext 'Detected HP-UX:'`
2438        isBitSet $installedStatus $SERVICE_INSTALLED_DEFAULT
2439        if [ $? -eq 1 ] ; then
2440            stopit "0"
2441            eval echo `gettext ' Removing the $APP_LONG_NAME daemon...'`
2442            for i in `ls "/sbin/rc3.d/K"??"$APP_NAME_LOWER" "/sbin/rc3.d/S"??"$APP_NAME_LOWER" "/sbin/init.d/$APP_NAME" 2>/dev/null` ; do
2443                rm -f $i
2444            done
2445        else
2446            eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
2447            exit 1
2448        fi
2449    elif [ "$DIST_OS" = "aix" ] ; then
2450        eval echo `gettext 'Detected AIX:'`
2451        if [ $installedStatus -ne $SERVICE_NOT_INSTALLED ] ; then
2452            stopit "0"
2453            eval echo `gettext ' Removing the $APP_LONG_NAME daemon...'`
2454            isBitSet $installedStatus $SERVICE_INSTALLED_DEFAULT
2455            if [ $? -eq 1 ] ; then
2456                for i in `ls "/etc/rc.d/rc2.d/K"??"$APP_NAME_LOWER" "/etc/rc.d/rc2.d/S"??"$APP_NAME_LOWER" "/etc/rc.d/init.d/$APP_NAME" 2>/dev/null` ; do
2457                    rm -f $i
2458                done
2459            fi
2460            isBitSet $installedStatus $SERVICE_INSTALLED_SRC
2461            isSrcSet=$?
2462            isBitSet $installedStatus $SERVICE_INSTALLED_SRC_PARTIAL
2463            isSrcPartialSet=$?
2464            if [ $isSrcSet -eq 1 -o $isSrcPartialSet -eq 1 ] ; then
2465                validateAppNameLength
2466                /usr/sbin/rmitab $APP_NAME
2467                /usr/bin/rmssys -s $APP_NAME
2468            fi
2469        else
2470            eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
2471            exit 1
2472        fi
2473    elif [ "$DIST_OS" = "freebsd" -o "$DIST_OS" = "dragonfly" ] ; then
2474        eval echo `gettext 'Detected FreeBSD/DragonFly:'`
2475        if [ -f "/etc/rc.d/$APP_NAME" -o -L "/etc/rc.d/$APP_NAME" ] ; then
2476            stopit "0"
2477            eval echo `gettext ' Removing the $APP_LONG_NAME daemon...'`
2478            for i in "/etc/rc.d/$APP_NAME"
2479            do
2480                rm -f $i
2481            done
2482            sed -i.bak "/${APP_NAME}_enable=\"YES\"/d" /etc/rc.conf
2483        else
2484            eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
2485            exit 1
2486        fi
2487    elif [ "$DIST_OS" = "macosx" ] ; then
2488        eval echo `gettext 'Detected Mac OSX:'`
2489        if [ -f "/Library/LaunchDaemons/${APP_PLIST}" -o -L "/Library/LaunchDaemons/${APP_PLIST}" ] ; then
2490            stopit "0"
2491            eval echo `gettext ' Removing the $APP_LONG_NAME daemon...'`
2492            # Make sure the plist is installed
2493            LOADED_PLIST=`launchctl list | grep ${APP_PLIST_BASE}`
2494            if [ "X${LOADED_PLIST}" != "X" ] ; then
2495                launchctl unload "/Library/LaunchDaemons/${APP_PLIST}"
2496            fi
2497            rm -f "/Library/LaunchDaemons/${APP_PLIST}"
2498        else
2499            eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
2500            exit 1
2501        fi
2502    elif [ "$DIST_OS" = "zos" ] ; then
2503        eval echo `gettext 'Detected z/OS:'`
2504        if [ -f /etc/rc.bak ] ; then
2505            stopit "0"
2506            eval echo `gettext ' Removing the $APP_LONG_NAME daemon...'`
2507            cp /etc/rc /etc/rc.bak
2508            sed  "s/_BPX_JOBNAME=\'APP_NAME\'.*//g" /etc/rc.bak > /etc/rc
2509            rm /etc/rc.bak
2510        else
2511            eval echo `gettext ' The $APP_LONG_NAME daemon is not currently installed.'`
2512            exit 1
2513        fi
2514    else
2515        eval echo `gettext 'Remove not currently supported for $DIST_OS.'`
2516        exit 1
2517    fi
2518}
2519
2520dump() {
2521    eval echo `gettext 'Dumping $APP_LONG_NAME...'`
2522    getpid
2523    if [ "X$pid" = "X" ]
2524    then
2525        eval echo `gettext '$APP_LONG_NAME was not running.'`
2526    else
2527        kill -3 $pid
2528
2529        if [ $? -ne 0 ]
2530        then
2531            eval echo `gettext 'Failed to dump $APP_LONG_NAME.'`
2532            exit 1
2533        else
2534            eval echo `gettext 'Dumped $APP_LONG_NAME.'`
2535        fi
2536    fi
2537}
2538
2539# Used by HP-UX init scripts.
2540startmsg() {
2541    getpid
2542    if [ "X$pid" = "X" ]
2543    then
2544        eval echo `gettext 'Starting $APP_LONG_NAME...  Wrapper:Stopped'`
2545    else
2546        if [ "X$DETAIL_STATUS" = "X" ]
2547        then
2548            eval echo `gettext 'Starting $APP_LONG_NAME...  Wrapper:Running'`
2549        else
2550            getstatus
2551            eval echo `gettext 'Starting $APP_LONG_NAME...  Wrapper:$STATUS, Java:$JAVASTATUS'`
2552        fi
2553    fi
2554}
2555
2556# Used by HP-UX init scripts.
2557stopmsg() {
2558    getpid
2559    if [ "X$pid" = "X" ]
2560    then
2561        eval echo `gettext 'Stopping $APP_LONG_NAME...  Wrapper:Stopped'`
2562    else
2563        if [ "X$DETAIL_STATUS" = "X" ]
2564        then
2565            eval echo `gettext 'Stopping $APP_LONG_NAME...  Wrapper:Running'`
2566        else
2567            getstatus
2568            eval echo `gettext 'Stopping $APP_LONG_NAME...  Wrapper:$STATUS, Java:$JAVASTATUS'`
2569        fi
2570    fi
2571}
2572
2573# 'source' files
2574sourceFiles() {
2575    if [ -n "$FILES_TO_SOURCE" ] ; then
2576        OIFS=$IFS
2577        IFS=';'
2578        files=$FILES_TO_SOURCE
2579        for file in $files
2580        do
2581            . $file
2582        done
2583
2584        IFS=$OIFS
2585    fi
2586}
2587
2588showUsage() {
2589    # $1 bad command
2590
2591    if [ -n "$1" ]
2592    then
2593        eval echo `gettext 'Unexpected command: $1'`
2594        echo "";
2595    fi
2596
2597    if [ "X${PASS_THROUGH}" = "Xapp_args" ] ; then
2598        ARGS=" {JavaAppArgs}"
2599    elif [ "X${PASS_THROUGH}" = "Xboth" ] ; then
2600        ARGS=" {WrapperProperties} [-- {JavaAppArgs}]"
2601    else
2602        ARGS=""
2603    fi
2604
2605    eval MSG=`gettext 'Usage: '`
2606    if [ -n "$FIXED_COMMAND" ] ; then
2607        echo "${MSG} $0${ARGS}"
2608    else
2609        checkInstalled "strict"
2610        if [ $installedStatus -eq $SERVICE_NOT_INSTALLED ] ; then
2611            # not installed, allow arguments to be passed through
2612            ARGS_START=$ARGS
2613        else
2614            ARGS_START=""
2615        fi
2616        if [ -n "$PAUSABLE" ] ; then
2617            echo "${MSG} $0 [ console${ARGS} | start${ARGS_START} | stop | restart${ARGS_START} | condrestart${ARGS_START} | pause | resume | status | install | installstart | remove | dump ]"
2618        else
2619            echo "${MSG} $0 [ console${ARGS} | start${ARGS_START} | stop | restart${ARGS_START} | condrestart${ARGS_START} | status | install | installstart | remove | dump ]"
2620        fi
2621    fi
2622
2623    if [ ! -n "$BRIEF_USAGE" ]
2624    then
2625        echo "";
2626        if [ ! -n "$FIXED_COMMAND" ] ; then
2627            echo "`gettext 'Commands:'`"
2628            echo "`gettext '  console      Launch in the current console.'`"
2629            echo "`gettext '  start        Start in the background as a daemon process.'`"
2630            echo "`gettext '  stop         Stop if running as a daemon or in another console.'`"
2631            echo "`gettext '  restart      Stop if running and then start.'`"
2632            echo "`gettext '  condrestart  Restart only if already running.'`"
2633            if [ -n "$PAUSABLE" ] ; then
2634                echo "`gettext '  pause        Pause if running.'`"
2635                echo "`gettext '  resume       Resume if paused.'`"
2636            fi
2637            echo "`gettext '  status       Query the current status.'`"
2638            echo "`gettext '  install      Install to start automatically when system boots.'`"
2639            echo "`gettext '  installstart Install and start running as a daemon process.'`"
2640            echo "`gettext '  remove       Uninstall.'`"
2641            echo "`gettext '  dump         Request a Java thread dump if running.'`"
2642            echo "";
2643        fi
2644        if [ "X${PASS_THROUGH}" = "Xapp_args" -o "X${PASS_THROUGH}" = "Xboth" ] ; then
2645            if [ "X${PASS_THROUGH}" = "Xboth" ] ; then
2646                echo "WrapperProperties:"
2647                echo "`gettext '  Optional configuration properties which will be passed to the Wrapper.'`"
2648                echo "";
2649            fi
2650            echo "JavaAppArgs:"
2651            echo "`gettext '  Optional arguments which will be passed to the Java application.'`"
2652            echo "";
2653        fi
2654    fi
2655
2656    exit 1
2657}
2658
2659# Check if arguments are correct. This needs to be done after knowing the
2660#  command, the application name and OS (to check the installation status).
2661# NOTE: When the validity of the arguments depends on the daemon installation,
2662#       make sure to call setServiceManagementTool() before this function.
2663#
2664# $1 0: no parameter allowed
2665#   -1: parameter allowed if not installed
2666#    1: parameter allowed
2667checkArguments() {
2668    # If this is a systemd call (from the systemd service file), we don't want
2669    #  to check arguments ('sysd' itself should not be blocking and the user
2670    #  may also have edited the file and added additional args in it).
2671    if [ -n "$SYSD" ] ; then
2672        return
2673    fi
2674
2675    if [ -n "$FIRST_ARG" ] ; then
2676        if [ $1 = -1 ] ; then
2677            checkInstalled "strict"
2678            if [ $installedStatus -eq $SERVICE_NOT_INSTALLED ] ; then
2679                PASS_THROUGH_ALLOWED=true
2680            fi
2681        elif [ $1 = 1 ] ; then
2682            PASS_THROUGH_ALLOWED=true
2683        fi
2684
2685        if [ "X${PASS_THROUGH_ALLOWED}" != "Xtrue" ] ; then
2686            eval echo `gettext 'Additional arguments are not allowed with the ${COMMAND} command.'`
2687
2688            if [ -n "$FIXED_COMMAND" ] ; then
2689                # The command can't be used with PASS_THROUGH, so disable it to show appropriate usage.
2690                PASS_THROUGH=false
2691            fi
2692            showUsage
2693            exit 2 # LSB - invalid or excess argument(s)
2694        elif [ "X${PASS_THROUGH}" = "Xfalse" ] ; then
2695            eval echo `gettext 'Additional arguments are not allowed when PASS_THROUGH is set to false.'`
2696            showUsage
2697            exit 2 # LSB - invalid or excess argument(s)
2698        fi
2699    fi
2700}
2701
2702# Resolve the service management tool for linux and aix.
2703setServiceManagementTool() {
2704    if [ ! -n "$SERVICE_MANAGEMENT_TOOL" ] ; then
2705        # Set the default value to auto.
2706        SERVICE_MANAGEMENT_TOOL=auto
2707    fi
2708
2709    if [ "$DIST_OS" = "linux" ] ; then
2710        setServiceManagementToolLinux
2711    elif [ "$DIST_OS" = "aix" ] ; then
2712        setServiceManagementToolAix
2713    else
2714        if [ "$SERVICE_MANAGEMENT_TOOL" != "auto" ] ; then
2715            eval echo `gettext 'The script does not support the service management tool \"$SERVICE_MANAGEMENT_TOOL\" on this platform.'`
2716            SERVICE_MANAGEMENT_TOOL=auto
2717        fi
2718        return
2719    fi
2720
2721    if [ $? = 1 ] ; then
2722        eval echo `gettext 'Service management tool value is invalid: $SERVICE_MANAGEMENT_TOOL.'`
2723        exit 1
2724    fi
2725}
2726
2727setServiceManagementToolLinux() {
2728    case "$SERVICE_MANAGEMENT_TOOL" in
2729        'auto')
2730            systemdDetection
2731            if [ $? -eq 0 ] ; then
2732                USE_SYSTEMD=1
2733                return
2734            fi
2735
2736            upstartDetection
2737            if [ $? -eq 0 ] ; then
2738                USE_UPSTART=1
2739                return
2740            fi
2741            ;;
2742        'systemd')
2743            USE_SYSTEMD=1
2744            ;;
2745        'upstart')
2746            USE_UPSTART=1
2747            ;;
2748        'initd')
2749            ;;
2750        *)
2751            return 1
2752    esac
2753}
2754
2755setServiceManagementToolAix() {
2756    case "$SERVICE_MANAGEMENT_TOOL" in
2757        'auto')
2758            srcDetection
2759            if [ $? -eq 0 ] ; then
2760                USE_SRC=1
2761            fi
2762            ;;
2763        'src')
2764            USE_SRC=1
2765            ;;
2766        'initd')
2767            ;;
2768        *)
2769            return 1
2770    esac
2771}
2772
2773docommand() {
2774
2775    case "$COMMAND" in
2776        'console')
2777            checkArguments 1
2778            checkUser touchlock "$@"
2779            if [ ! -n "$FIXED_COMMAND" ] ; then
2780                shift
2781            fi
2782            console "$@"
2783            ;;
2784
2785        'start')
2786            setServiceManagementTool
2787            checkArguments -1
2788            startdaemon "$@"
2789            ;;
2790
2791        'stop')
2792            checkArguments 0
2793            setServiceManagementTool
2794            if [ "$DIST_OS" = "macosx" -a -f "/Library/LaunchDaemons/${APP_PLIST}" ] ; then
2795                macosxStop
2796            elif [ "$DIST_OS" = "linux" -a -n "$USE_UPSTART" ] && [ -f "/etc/init/${APP_NAME}.conf" ] ; then
2797                upstartStop
2798            elif [ "$DIST_OS" = "linux" -a -n "$USE_SYSTEMD" -a -z "$SYSD" ] && [ -f "${SYSTEMD_SERVICE_FILE}" ] ; then
2799                systemdStop
2800            elif [ "$DIST_OS" = "aix" -a -n "$USE_SRC" ] && [ -n "`/usr/bin/lssrc -S -s $APP_NAME`" ] ; then
2801                srcStop
2802            else
2803                checkUser "" "$COMMAND"
2804                stopit "0"
2805            fi
2806            ;;
2807
2808        'restart')
2809            setServiceManagementTool
2810            checkArguments -1
2811            restartdaemon "0" "$@"
2812            ;;
2813
2814        'condrestart')
2815            setServiceManagementTool
2816            checkArguments -1
2817            restartdaemon "1" "$@"
2818            ;;
2819
2820        'pause')
2821            checkArguments 0
2822            setServiceManagementTool
2823            if [ -n "$PAUSABLE" ]
2824            then
2825                pause
2826            else
2827                showUsage "$COMMAND"
2828            fi
2829            ;;
2830
2831        'resume')
2832            checkArguments 0
2833            setServiceManagementTool
2834            if [ -n "$PAUSABLE" ]
2835            then
2836                resume
2837            else
2838                showUsage "$COMMAND"
2839            fi
2840            ;;
2841
2842        'status')
2843            checkArguments 0
2844            setServiceManagementTool
2845            status
2846            ;;
2847
2848        'install')
2849            checkArguments 0
2850            setServiceManagementTool
2851            installdaemon "$@"
2852            ;;
2853
2854        'installstart')
2855            checkArguments 0
2856            setServiceManagementTool
2857            installdaemon "$@"
2858            startdaemon "$@"
2859            ;;
2860
2861        'remove')
2862            checkArguments 0
2863            setServiceManagementTool
2864            removedaemon
2865            ;;
2866
2867        'dump')
2868            checkArguments 0
2869            checkUser "" "$COMMAND"
2870            dump
2871            ;;
2872
2873        'start_msg')
2874            # Internal command called by launchd on HP-UX.
2875            checkUser "" "$COMMAND"
2876            startmsg
2877            ;;
2878
2879        'stop_msg')
2880            # Internal command called by launchd on HP-UX.
2881            checkUser "" "$COMMAND"
2882            stopmsg
2883            ;;
2884
2885        'launchdinternal' | 'upstartinternal')
2886            if [ ! "$DIST_OS" = "macosx" -o ! -f "/Library/LaunchDaemons/${APP_PLIST}" ] ; then
2887                checkUser touchlock "$@"
2888            fi
2889            # Internal command called by launchd on Max OSX.
2890            # We do not want to call checkUser here as it is handled in the launchd plist file.  Doing it here would confuse launchd.
2891            if [ ! -n "$FIXED_COMMAND" ] ; then
2892                shift
2893            fi
2894            launchinternal "$@"
2895            ;;
2896
2897        *)
2898            showUsage "$COMMAND"
2899            ;;
2900    esac
2901}
2902
2903sourceFiles
2904docommand "$@"
2905
2906exit 0
2907