1#!/bin/bash
2#
3# This work is dual-licensed under the Apache License 2.0
4# and European Union Public License. See LICENSE file for details.
5#
6# Parts of this file Copyright (c) 2017 Evolveum and contributors
7#
8
9SCRIPT_PATH=$(cd $(dirname "$0") && pwd -P)/$(basename "$2")
10USE_NOHUP="true"
11# resolve links - $0 may be a softlink
12PRG="$0"
13
14while [ -h "$PRG" ]; do
15ls=`ls -ld "$PRG"`
16  link=`expr "$ls" : '.*-> \(.*\)$'`
17  if expr "$link" : '/.*' > /dev/null; then
18    PRG="$link"
19  else
20    PRG=`dirname "$PRG"`/"$link"
21  fi
22done
23
24# Get standard environment variables
25PRGDIR=`dirname "$PRG"`
26
27cd "$PRGDIR/.." >/dev/null
28
29cd "$SCRIPT_PATH/.."
30
31if [ ! -d var ] ; then
32    mkdir var
33fi
34
35if [ ! -d var/log ] ; then
36    mkdir var/log
37fi
38
39if [ -z "$MIDPOINT_HOME" ] ; then
40    MIDPOINT_HOME=$(cd "$SCRIPT_PATH../var"; pwd)
41fi
42JAVA_OPTS="$JAVA_OPTS
43-Xms2048M
44-Xmx4096M
45-Dpython.cachedir=$MIDPOINT_HOME/tmp
46-Djavax.net.ssl.trustStore=$MIDPOINT_HOME/keystore.jceks
47-Djavax.net.ssl.trustStoreType=jceks
48-Dmidpoint.home=$MIDPOINT_HOME"
49
50# apply setenv.sh if it exists. This can be used for -Dmidpoint.nodeId etc.
51# the script can either append or overwrite JAVA_OPTS
52if [ -r "$SCRIPT_PATH/setenv.sh" ]; then
53    echo "Applying setenv.sh from $SCRIPT_PATH directory."
54    . "$SCRIPT_PATH/setenv.sh"
55fi
56
57if [ -z "$BOOT_OUT" ] ; then
58  BOOT_OUT="$SCRIPT_PATH"../var/log/midpoint.out
59fi
60
61if [ -z "$PID_FILE" ] ; then
62 PID_FILE="$SCRIPT_PATH"../var/log/midpoint.pid
63fi
64
65[ -z "$MIDPOINT_HOME" ] && MIDPOINT_HOME=`cd "$SCRIPT_PATH../var" >/dev/null; pwd`
66
67cd "$SCRIPT_PATH../lib"
68
69if [ ! -f midpoint.war ] ; then
70echo "ERROR: midpoint.war is not in /lib directory"
71exit 1
72fi
73
74# Bugzilla 37848: When no TTY is available, don't output to console
75have_tty=0
76if [ "`tty`" != "not a tty" ]; then
77    have_tty=1
78fi
79
80if [ -z "$LOGGING_MANAGER" ]; then
81  LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
82fi
83
84# Set UMASK unless it has been overridden
85if [ -z "$UMASK" ]; then
86    UMASK="0027"
87fi
88umask $UMASK
89
90if [ -z "$JAVA_HOME" ] ; then
91  _RUNJAVA=java
92else
93  _RUNJAVA="$JAVA_HOME"/bin/java
94fi
95
96if [ "$USE_NOHUP" = "true" ]; then
97    _NOHUP=nohup
98fi
99
100
101# ----- Execute The Requested Command -----------------------------------------
102
103#if [ $have_tty -eq 1 ]; then
104#  if [ ! -z "$PID_FILE" ]; then
105#    echo "Using PID_FILE:    $PID_FILE"
106#  fi
107#fi
108
109if [ "$1" = "start" ] ; then
110 if [ ! -z "$PID_FILE" ]; then
111    if [ -f "$PID_FILE" ]; then
112      if [ -s "$PID_FILE" ]; then
113        echo "Existing PID file found during start."
114        if [ -r "$PID_FILE" ]; then
115          PID=`cat "$PID_FILE"`
116          ps -p $PID >/dev/null 2>&1
117          if [ $? -eq 0 ] ; then
118            echo "Midpoint appears to still be running with PID $PID. Start aborted."
119            echo "If the following process is not a midpoint process, remove the PID file and try again:"
120            ps -f -p $PID
121            exit 1
122          else
123            echo "Removing/clearing stale PID file."
124            rm -f "$PID_FILE" >/dev/null 2>&1
125            if [ $? != 0 ]; then
126              if [ -w "$PID_FILE" ]; then
127                cat /dev/null > "$PID_FILE"
128              else
129                echo "Unable to remove or clear stale PID file. Start aborted."
130                exit 1
131              fi
132            fi
133          fi
134        else
135          echo "Unable to read PID file. Start aborted."
136          exit 1
137        fi
138      else
139        rm -f "$PID_FILE" >/dev/null 2>&1
140if [ $? != 0 ]; then
141          if [ ! -w "$PID_FILE" ]; then
142            echo "Unable to remove or write to empty PID file. Start aborted."
143            exit 1
144          fi
145        fi
146      fi
147    fi
148  fi
149
150  shift
151  touch "$BOOT_OUT"
152
153echo "Starting midPoint..."
154echo "MIDPOINT_HOME=$MIDPOINT_HOME"
155
156cd
157eval $_NOHUP "\"$_RUNJAVA\"" -jar $LOGGING_MANAGER $JAVA_OPTS \
158    $SCRIPT_PATH../lib/midpoint.war \
159      >> "$BOOT_OUT" 2>&1 "&"
160
161
162if [ ! -z "$PID_FILE" ]; then
163    echo $! > "$PID_FILE"
164fi
165
166
167elif [ "$1" = "stop" ] ; then
168
169  shift
170
171  SLEEP=10
172  if [ ! -z "$1" ]; then
173    echo $1 | grep "[^0-9]" >/dev/null 2>&1
174    if [ $? -gt 0 ]; then
175      SLEEP=$1
176      shift
177    fi
178  fi
179
180  FORCE=0
181  if [ "$1" = "-force" ]; then
182    shift
183    FORCE=1
184  fi
185
186  if [ ! -z "$PID_FILE" ]; then
187    if [ -f "$PID_FILE" ]; then
188      if [ -s "$PID_FILE" ]; then
189        kill -0 `cat "$PID_FILE"` >/dev/null 2>&1
190        if [ $? -gt 0 ]; then
191          echo "PID file found but no matching process was found. Stop aborted."
192          exit 1
193        fi
194      else
195        echo "PID file is empty and has been ignored."
196      fi
197    else
198      echo "\$PID_FILE was set but the specified file does not exist. Is Tomcat running? Stop aborted."
199      exit 1
200    fi
201  fi
202
203    if [ ! -z "$CATALINA_PID" ]; then
204      echo "The stop command failed. Attempting to signal the process to stop through OS signal."
205      kill -15 `cat "$CATALINA_PID"` >/dev/null 2>&1
206    fi
207
208    if [ ! -z "$PID_FILE" ]; then
209    echo "Stopping midPoint"
210      kill -TERM `cat "$PID_FILE"` >/dev/null 2>&1
211    fi
212  # stop failed. Shutdown port disabled? Try a normal kill.
213
214
215  if [ ! -z "$PID_FILE" ]; then
216    if [ -f "$PID_FILE" ]; then
217      while [ $SLEEP -ge 0 ]; do
218
219        kill -0 `cat "$PID_FILE"` >/dev/null 2>&1
220        if [ $? -gt 0 ]; then
221          rm -f "$PID_FILE" >/dev/null 2>&1
222          if [ $? != 0 ]; then
223            if [ -w "$PID_FILE" ]; then
224              cat /dev/null > "$PID_FILE"
225              # If MidPoint has stopped don't try and force a stop with an empty PID file
226              FORCE=0
227            else
228              echo "The PID file could not be removed or cleared."
229            fi
230          fi
231          echo "Midpoint stopped."
232          break
233        fi
234        if [ $SLEEP -gt 0 ]; then
235          sleep 1
236        fi
237
238  if [ $SLEEP -eq 0 ]; then
239          echo "Midpoint did not stop in time."
240          if [ $FORCE -eq 0 ]; then
241            echo "PID file was not removed."
242          fi
243          echo "To aid diagnostics a thread dump has been written to standard out."
244          kill -3 `cat "$PID_FILE"`
245        fi
246        SLEEP=`expr $SLEEP - 1 `
247      done
248    fi
249  fi
250
251  KILL_SLEEP_INTERVAL=5
252  if [ $FORCE -eq 1 ]; then
253    if [ -z "$PID_FILE" ]; then
254      echo "Kill failed: \$PID_FILE not set"
255    else
256      if [ -f "$PID_FILE" ]; then
257        PID=`cat "$PID_FILE"`
258        echo "Killing midPoint with the PID: $PID"
259        kill -9 $PID
260        while [ $KILL_SLEEP_INTERVAL -ge 0 ]; do
261            kill -0 `cat "$PID_FILE"` >/dev/null 2>&1
262            if [ $? -gt 0 ]; then
263                rm -f "$PID_FILE" >/dev/null 2>&1
264                if [ $? != 0 ]; then
265                    if [ -w "$PID_FILE" ]; then
266                        cat /dev/null > "$PID_FILE"
267                    else
268                        echo "The PID file could not be removed."
269                    fi
270                fi
271                echo "The MidPoint process has been killed."
272                break
273            fi
274            if [ $KILL_SLEEP_INTERVAL -gt 0 ]; then
275                sleep 1
276            fi
277            KILL_SLEEP_INTERVAL=`expr $KILL_SLEEP_INTERVAL - 1 `
278        done
279        if [ $KILL_SLEEP_INTERVAL -lt 0 ]; then
280            echo "MidPoint has not been killed completely yet. The process might be waiting on some system call or might be UNINTERRUPTIBLE."
281        fi
282      fi
283    fi
284  fi
285fi
286
287