summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjacobkeeler <jacob.keeler@livioradio.com>2020-06-02 12:48:16 -0400
committerjacobkeeler <jacob.keeler@livioradio.com>2020-06-02 12:48:16 -0400
commit3a58005a5fc4a802d7fa46f746f1fb0f5637bf8c (patch)
tree33adcd97953b2de6594b3ce994099017116aef24
parentbd153b1cb73a6c7c3158e9f4f4744fdc2441fb0e (diff)
downloadsdl_core-3a58005a5fc4a802d7fa46f746f1fb0f5637bf8c.tar.gz
Add features to make the daemon script more reliable
Create dedicated PID file directory Check running processes before starting or stopping Core Add `kill` command to destroy all running instances of Core Change main SDL thread to be identifiable by name
-rw-r--r--src/appMain/core.sh66
-rw-r--r--src/appMain/core_external_proprietary.sh117
-rw-r--r--src/appMain/main.cc3
3 files changed, 154 insertions, 32 deletions
diff --git a/src/appMain/core.sh b/src/appMain/core.sh
index 4d3fbfce1e..1ea1747f1f 100644
--- a/src/appMain/core.sh
+++ b/src/appMain/core.sh
@@ -1,28 +1,74 @@
#!/bin/bash
cd $(dirname $0)
DIR=$(pwd)
-PID_FILE=$DIR/core.pid
+PID_DIR=~/.sdl
+
+if [ ! -d "$PID_DIR" ]; then
+ mkdir $PID_DIR
+fi
+
+CORE_PID_FILE=${PID_DIR}/core.pid
+CORE_PROCESS_NAME=SDLCore
function core_start() {
- echo "Starting SmartDeviceLinkCore"
- LD_LIBRARY_PATH=$DIR ${DIR}/smartDeviceLinkCore &
- PID=$!
- echo $PID > $PID_FILE
+ if [ -f "$CORE_PID_FILE" ] && [ -n "$(ps -p $(cat $CORE_PID_FILE) -o pid=)" ]; then
+ echo "Core is already running"
+ return 1
+ elif [ -n "$(pgrep $CORE_PROCESS_NAME)" ]; then
+ echo "Core is already running outside of this script"
+ echo "This lingering instance can be stopped using the \"kill\" command"
+ return 2
+ else
+ echo "Starting SmartDeviceLinkCore"
+ LD_LIBRARY_PATH=$DIR ${DIR}/smartDeviceLinkCore &
+ CORE_PID=$!
+ echo $CORE_PID > $CORE_PID_FILE
+ return 0
+ fi
}
function core_stop() {
- echo "Stopping SmartDeviceLinkCore"
- kill $(cat $PID_FILE)
- rm $PID_FILE
+ RESULT=1
+ if [ -f "$CORE_PID_FILE" ] && [ -n "$(ps -p $(cat $CORE_PID_FILE) -o pid=)" ]; then
+ echo "Stopping SmartDeviceLinkCore"
+ CORE_PID=$(cat $CORE_PID_FILE)
+ kill $CORE_PID
+
+ # If Core doesn't close normally within 10 seconds, force-close it
+ timeout 10s tail --pid=$CORE_PID -f /dev/null
+ if [ -n "$(ps -p $CORE_PID -o pid=)" ]; then
+ echo "Core did not shut down properly, force-killing"
+ kill -9 $CORE_PID
+ fi
+ RESULT=0
+ fi
+
+ if [ -f "$CORE_PID_FILE" ]; then
+ rm $CORE_PID_FILE
+ fi
+ return $RET
}
if [ x$1 == xstop ]; then
core_stop
+ if [ ! "$?" -eq 0 ]; then
+ echo "Core is not running (or was started outside of this script)"
+ fi
elif [ x$1 == xrestart ]; then
core_stop
- core_start
+ if [ "$?" -eq 0 ]; then
+ core_start
+ else
+ echo "Core is not running (or was started outside of this script)"
+ fi
elif [ x$1 == xstart ]; then
core_start
+elif [ x$1 == xkill ]; then
+ core_stop
+ if [ -n "$(pgrep $CORE_PROCESS_NAME)" ]; then
+ echo "Killing all lingering instances of SDL Core"
+ killall -9 $CORE_PROCESS_NAME
+ fi
else
- echo "usage: core.sh [start/restart/stop]"
+ echo "usage: core.sh [start/restart/stop/kill]"
fi
diff --git a/src/appMain/core_external_proprietary.sh b/src/appMain/core_external_proprietary.sh
index 7857ea4258..dbc2452869 100644
--- a/src/appMain/core_external_proprietary.sh
+++ b/src/appMain/core_external_proprietary.sh
@@ -1,52 +1,127 @@
#!/bin/bash
cd $(dirname $0)
DIR=$(pwd)
-CORE_PID_FILE=/var/run/core.pid
-PM_PID_FILE=/var/run/policy_manager.pid
+PID_DIR=~/.sdl
+
+if [ ! -d "$PID_DIR" ]; then
+ mkdir $PID_DIR
+fi
+
+CORE_PID_FILE=${PID_DIR}/core.pid
+CORE_PROCESS_NAME=SDLCore
+PM_PID_FILE=${PID_DIR}/policy_manager.pid
function core_start() {
- echo "Starting SmartDeviceLinkCore"
- LD_LIBRARY_PATH=$DIR ${DIR}/smartDeviceLinkCore &
- CORE_PID=$!
- echo $CORE_PID > $CORE_PID_FILE
+ if [ -f "$CORE_PID_FILE" ] && [ -n "$(ps -p $(cat $CORE_PID_FILE) -o pid=)" ]; then
+ echo "Core is already running"
+ return 1
+ elif [ -n "$(pgrep $CORE_PROCESS_NAME)" ]; then
+ echo "Core is already running outside of this script"
+ echo "This lingering instance can be stopped using the \"kill\" command"
+ return 2
+ else
+ echo "Starting SmartDeviceLinkCore"
+ LD_LIBRARY_PATH=$DIR ${DIR}/smartDeviceLinkCore &
+ CORE_PID=$!
+ echo $CORE_PID > $CORE_PID_FILE
+ return 0
+ fi
}
function core_stop() {
- echo "Stopping SmartDeviceLinkCore"
- kill $(cat $CORE_PID_FILE)
- rm $CORE_PID_FILE
+ RESULT=1
+ if [ -f "$CORE_PID_FILE" ] && [ -n "$(ps -p $(cat $CORE_PID_FILE) -o pid=)" ]; then
+ echo "Stopping SmartDeviceLinkCore"
+ CORE_PID=$(cat $CORE_PID_FILE)
+ kill $CORE_PID
+
+ # If Core doesn't close normally within 10 seconds, force-close it
+ timeout 10s tail --pid=$CORE_PID -f /dev/null
+ if [ -n "$(ps -p $CORE_PID -o pid=)" ]; then
+ echo "Core did not shut down properly, force-killing"
+ kill -9 $CORE_PID
+ fi
+ RESULT=0
+ fi
+
+ if [ -f "$CORE_PID_FILE" ]; then
+ rm $CORE_PID_FILE
+ fi
+ return $RET
}
-function pm_start() {
+function pm_install_dependencies() {
pip3 list | grep -F tornado > /dev/null
if [ $? -eq 1 ]; then
echo "Installing tornado python package"
sudo pip3 install tornado
fi
- echo "Starting Policy Manager"
- python3 ${DIR}/sample_policy_manager.py --pack_port 8088 --unpack_port 8089 --add_http_header --encryption &
- PM_PID=$!
- echo $PM_PID > $PM_PID_FILE
+}
+
+function pm_start() {
+ if [ -f "$PM_PID_FILE" ] && [ -n "$(ps -p $(cat $PM_PID_FILE) -o pid=)" ]; then
+ echo "Policy Server is already running"
+ return 1
+ else
+ pm_install_dependencies
+ echo "Starting Policy Manager"
+ python3 ${DIR}/sample_policy_manager.py --pack_port 8088 --unpack_port 8089 --add_http_header --encryption &
+ PM_PID=$!
+ echo $PM_PID > $PM_PID_FILE
+ return 0
+ fi
}
function pm_stop() {
- echo "Stopping Policy Manager"
- kill -INT $(cat $PM_PID_FILE)
- kill -9 $(cat $PM_PID_FILE)
- rm $PM_PID_FILE
+ RESULT=1
+ if [ -f "$PM_PID_FILE" ] && [ -n "$(ps -p $(cat $PM_PID_FILE) -o pid=)" ]; then
+ echo "Stopping Policy Manager"
+ kill -INT $(cat $PM_PID_FILE)
+ kill -9 $(cat $PM_PID_FILE)
+ RESULT=0
+ fi
+
+ # Clear PID regardless of whether the process was running or not
+ if [ -f "$PM_PID_FILE" ]; then
+ rm $PM_PID_FILE
+ fi
+ return $RESULT
}
if [ x$1 == xstop ]; then
core_stop
+ if [ ! "$?" -eq 0 ]; then
+ echo "Core is not running (or was started outside of this script)"
+ fi
+
pm_stop
+ if [ ! "$?" -eq 0 ]; then
+ echo "Policy Server is not running (or was started outside of this script)"
+ fi
elif [ x$1 == xrestart ]; then
core_stop
+ if [ "$?" -eq 0 ]; then
+ core_start
+ else
+ echo "Core is not running (or was started outside of this script)"
+ fi
+
pm_stop
- core_start
- pm_start
+ if [ "$?" -eq 0 ]; then
+ pm_start
+ else
+ echo "Policy Server is not running (or was started outside of this script)"
+ fi
elif [ x$1 == xstart ]; then
core_start
pm_start
+elif [ x$1 == xkill ]; then
+ core_stop
+ pm_stop
+ if [ -n "$(pgrep $CORE_PROCESS_NAME)" ]; then
+ echo "Killing all lingering instances of SDL Core"
+ killall -9 $CORE_PROCESS_NAME
+ fi
else
- echo "usage: core.sh [start/restart/stop]"
+ echo "usage: core.sh [start/restart/stop/kill]"
fi
diff --git a/src/appMain/main.cc b/src/appMain/main.cc
index cb59f63b1d..f2f7af5f2e 100644
--- a/src/appMain/main.cc
+++ b/src/appMain/main.cc
@@ -135,7 +135,8 @@ int32_t main(int32_t argc, char** argv) {
// Logger initialization
INIT_LOGGER("log4cxx.properties", profile_instance.logs_enabled());
- threads::Thread::SetNameForId(threads::Thread::CurrentId(), "MainThread");
+ threads::Thread::SetNameForId(threads::Thread::CurrentId(),
+ "SDLCore");
if (!utils::appenders_loader.Loaded()) {
LOG4CXX_ERROR(logger_,