diff options
author | Jacob Keeler <jacob.keeler@livioradio.com> | 2020-06-09 17:06:42 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-09 17:06:42 -0400 |
commit | fdabd521f6daff4d913b0520a323b8d0cde24d0d (patch) | |
tree | efe44a2bcd9e86be1bf34d6616265d0c69b5f0a3 | |
parent | aa28d6d1af0a9156dba9af3f6c03a45ad911a7f7 (diff) | |
parent | e144711cb0db99ccb28957c7511be503e543c213 (diff) | |
download | sdl_core-fdabd521f6daff4d913b0520a323b8d0cde24d0d.tar.gz |
Merge pull request #3417 from smartdevicelink/feature/daemon_script
Add SDL Core Daemon Script
-rw-r--r-- | src/appMain/CMakeLists.txt | 14 | ||||
-rw-r--r-- | src/appMain/core.sh | 8 | ||||
-rw-r--r-- | src/appMain/core_external_proprietary.sh | 9 | ||||
-rw-r--r-- | src/appMain/daemon.sh | 142 | ||||
-rw-r--r-- | src/appMain/main.cc | 3 | ||||
-rw-r--r-- | src/appMain/start.sh | 18 | ||||
-rw-r--r-- | src/appMain/start_external_proprietary.sh | 23 |
7 files changed, 187 insertions, 30 deletions
diff --git a/src/appMain/CMakeLists.txt b/src/appMain/CMakeLists.txt index 86be828c6b..c2b7255ecc 100644 --- a/src/appMain/CMakeLists.txt +++ b/src/appMain/CMakeLists.txt @@ -167,8 +167,8 @@ file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/smartDeviceLink.ini DESTINATION ${CMAKE_CU file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/hmi_capabilities.json DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/sdl_preloaded_pt.json DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/sample_policy_manager.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/start.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/start_external_proprietary.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/core.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/core_external_proprietary.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) if (CMAKE_SYSTEM_NAME STREQUAL "QNX") file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/init_policy.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) endif () @@ -246,15 +246,19 @@ if (CMAKE_SYSTEM_NAME STREQUAL "QNX") endif () if (${EXTENDED_POLICY} STREQUAL "EXTERNAL_PROPRIETARY") - install(FILES start_external_proprietary.sh DESTINATION bin + install(FILES core_external_proprietary.sh DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ - GROUP_EXECUTE WORLD_READ WORLD_EXECUTE RENAME start.sh) + GROUP_EXECUTE WORLD_READ WORLD_EXECUTE RENAME core.sh) else() - install(FILES start.sh DESTINATION bin + install(FILES core.sh DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) endif() +install(FILES start.sh daemon.sh DESTINATION bin + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ + GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) + if(BUILD_TESTS) add_subdirectory(test) endif() diff --git a/src/appMain/core.sh b/src/appMain/core.sh new file mode 100644 index 0000000000..6604368993 --- /dev/null +++ b/src/appMain/core.sh @@ -0,0 +1,8 @@ +#!/bin/bash +DIR=$(dirname $0) + +EXTERNAL_POLICIES=false +if [ "$2" = "true" ]; then + EXTERNAL_POLICIES=true +fi +$DIR/daemon.sh $1 $EXTERNAL_POLICIES diff --git a/src/appMain/core_external_proprietary.sh b/src/appMain/core_external_proprietary.sh new file mode 100644 index 0000000000..dc43bfb42c --- /dev/null +++ b/src/appMain/core_external_proprietary.sh @@ -0,0 +1,9 @@ +#!/bin/bash +DIR=$(dirname $0) + +EXTERNAL_POLICIES=true +# Allow for manual override to disable sample policy server +if [ "$2" = "false" ]; then + EXTERNAL_POLICIES=false +fi +$DIR/daemon.sh $1 $EXTERNAL_POLICIES diff --git a/src/appMain/daemon.sh b/src/appMain/daemon.sh new file mode 100644 index 0000000000..fbde0d750a --- /dev/null +++ b/src/appMain/daemon.sh @@ -0,0 +1,142 @@ +#!/bin/bash +cd $(dirname $0) +COMMAND=$1 +EXTERNAL_POLICIES=$2 +DIR=$(pwd) + +CORE_PID_FILE=${DIR}/core.pid +CORE_APPLICATION_NAME=smartDeviceLinkCore +PM_PID_FILE=${DIR}/policy_manager.pid +PM_APPLICATION_NAME=sample_policy_manager.py + +function core_start() { + 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 -f $CORE_APPLICATION_NAME)" ]; then + echo "Core is already running outside of this script" + echo "All instances of Core can be stopped using the \"kill\" command" + return 2 + else + echo "Starting SmartDeviceLink Core" + LD_LIBRARY_PATH=$DIR ${DIR}/${CORE_APPLICATION_NAME} & + CORE_PID=$! + echo $CORE_PID > $CORE_PID_FILE + return 0 + fi +} + +function core_stop() { + RESULT=1 + if [ -f "$CORE_PID_FILE" ] && [ -n "$(ps -p $(cat $CORE_PID_FILE) -o pid=)" ]; then + echo "Stopping SmartDeviceLink Core" + 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 $RESULT +} + +function pm_install_dependencies() { + pip3 list | grep -F tornado > /dev/null + if [ $? -eq 1 ]; then + echo "Installing tornado python package" + sudo -H pip3 install tornado + fi +} + +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 + elif [ -n "$(pgrep -f $PM_APPLICATION_NAME)" ]; then + echo "Policy Server is already running outside of this script" + echo "All instances of Core can be stopped using the \"kill\" command" + return 2 + else + pm_install_dependencies + echo "Starting Policy Manager" + python3 ${DIR}/${PM_APPLICATION_NAME} --pack_port 8088 --unpack_port 8089 --add_http_header --encryption & + PM_PID=$! + echo $PM_PID > $PM_PID_FILE + return 0 + fi +} + +function pm_stop() { + 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 [ "$COMMAND" == "stop" ]; then + core_stop + if [ "$?" -ne 0 ]; then + echo "Core is not running (or was started outside of this script)" + fi + + if [ "$EXTERNAL_POLICIES" == "true" ]; then + pm_stop + if [ "$?" -ne 0 ]; then + echo "Policy Server is not running (or was started outside of this script)" + fi + fi +elif [ "$COMMAND" == "restart" ]; then + core_stop + if [ "$?" -eq 0 ]; then + core_start + else + echo "Core is not running (or was started outside of this script)" + fi + + if [ "$EXTERNAL_POLICIES" == "true" ]; then + pm_stop + if [ "$?" -eq 0 ]; then + pm_start + else + echo "Policy Server is not running (or was started outside of this script)" + fi + fi +elif [ "$COMMAND" == "start" ]; then + core_start + if [ "$EXTERNAL_POLICIES" == "true" ]; then + pm_start + fi +elif [ "$COMMAND" == "kill" ]; then + core_stop + pkill -9 -f $CORE_APPLICATION_NAME + if [ "$?" -eq 0 ]; then + echo "Killed all lingering instances of SDL Core" + fi + + if [ "$EXTERNAL_POLICIES" == "true" ]; then + pm_stop + pkill -9 -f $PM_APPLICATION_NAME + if [ "$?" -eq 0 ]; then + echo "Killed all lingering instances of the Policy Server" + fi + fi +else + echo "usage: daemon.sh [start/restart/stop/kill] [use_sample_policy_manager?]" +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_, diff --git a/src/appMain/start.sh b/src/appMain/start.sh index ea6dea9d50..df867c75af 100644 --- a/src/appMain/start.sh +++ b/src/appMain/start.sh @@ -1,2 +1,18 @@ #!/bin/bash -LD_LIBRARY_PATH=. ./smartDeviceLinkCore
\ No newline at end of file +trap close INT + +function close() { + $DIR/core.sh stop +} + +DIR=$(dirname $0) +CORE_PID_FILE=${DIR}/core.pid + +$DIR/core.sh start + +# Wait for the application to close +CORE_PID=$(cat $CORE_PID_FILE) +tail --pid=$CORE_PID -f /dev/null + +# Verify that the application closed sucessfully +close > /dev/null diff --git a/src/appMain/start_external_proprietary.sh b/src/appMain/start_external_proprietary.sh deleted file mode 100644 index 35d4fe900c..0000000000 --- a/src/appMain/start_external_proprietary.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash -pip3 list | grep -F tornado > /dev/null - -if [ $? -eq 1 ] - then - echo "Installing tornado python package" - sudo -H pip3 install tornado -fi - -echo "Starting Policy Manager" -python3 sample_policy_manager.py --pack_port 8088 --unpack_port 8089 --add_http_header --encryption & -POLICY_MANAGER=$! - -trap ctrl_c INT - -function ctrl_c() { - echo "Stopping SmartDeviceLinkCore" - kill -INT $POLICY_MANAGER - kill -9 $POLICY_MANAGER -} - -echo "Starting SmartDeviceLinkCore" -LD_LIBRARY_PATH=. ./smartDeviceLinkCore |