Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1 | From 49046c1685465a5486fe9e1c04b99c585aab6862 Mon Sep 17 00:00:00 2001 |
| 2 | From: Stefan Saraev <stefan@saraev.ca> |
| 3 | Date: Wed, 2 Nov 2016 11:28:34 -0700 |
| 4 | Subject: [PATCH 04/10] handle SIGTERM |
| 5 | |
| 6 | 0. CApplication::Stop cant be trusted. (deadlocks crashes and boo) |
| 7 | |
| 8 | so, when shutdown/reboot is requested: |
| 9 | |
| 10 | 1. save an exit code (for CEC...) |
| 11 | 2. call CPowerManager::{Reboot,PowerDown} |
| 12 | 3. ... then systemd sends TERM and waits xx seconds before sending KILL |
| 13 | 4. CApplication::Stop has xx seconds to save guisettings.xml and boo |
| 14 | 5. CEC thread has xx seconds to switch off after it received OnQuit |
| 15 | 6. addons / pvrmanager / cec / everything else.. are free to deadlock / crash now, we dont care |
| 16 | 7. KILL |
| 17 | |
| 18 | Signed-off-by: Stefan Saraev <stefan@saraev.ca> |
| 19 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 20 | --- |
| 21 | xbmc/Application.cpp | 17 ++++++++++++----- |
| 22 | xbmc/Application.h | 1 + |
| 23 | xbmc/XBApplicationEx.cpp | 1 + |
| 24 | xbmc/XBApplicationEx.h | 1 + |
| 25 | xbmc/platform/posix/main.cpp | 15 +++++++++++++++ |
| 26 | 5 files changed, 30 insertions(+), 5 deletions(-) |
| 27 | |
| 28 | diff --git a/xbmc/Application.cpp b/xbmc/Application.cpp |
| 29 | index 100a2f2..fda892d 100644 |
| 30 | --- a/xbmc/Application.cpp |
| 31 | +++ b/xbmc/Application.cpp |
| 32 | @@ -2426,12 +2426,12 @@ void CApplication::OnApplicationMessage(ThreadMessage* pMsg) |
| 33 | switch (pMsg->dwMessage) |
| 34 | { |
| 35 | case TMSG_POWERDOWN: |
| 36 | - Stop(EXITCODE_POWERDOWN); |
| 37 | + SetExitCode(EXITCODE_POWERDOWN); |
| 38 | g_powerManager.Powerdown(); |
| 39 | break; |
| 40 | |
| 41 | case TMSG_QUIT: |
| 42 | - Stop(EXITCODE_QUIT); |
| 43 | + SetExitCode(EXITCODE_QUIT); |
| 44 | break; |
| 45 | |
| 46 | case TMSG_SHUTDOWN: |
| 47 | @@ -2452,12 +2452,13 @@ void CApplication::OnApplicationMessage(ThreadMessage* pMsg) |
| 48 | |
| 49 | case TMSG_RESTART: |
| 50 | case TMSG_RESET: |
| 51 | - Stop(EXITCODE_REBOOT); |
| 52 | + SetExitCode(EXITCODE_REBOOT); |
| 53 | g_powerManager.Reboot(); |
| 54 | break; |
| 55 | |
| 56 | case TMSG_RESTARTAPP: |
| 57 | #if defined(TARGET_WINDOWS) || defined(TARGET_LINUX) |
| 58 | + SetExitCode(EXITCODE_RESTARTAPP); |
| 59 | Stop(EXITCODE_RESTARTAPP); |
| 60 | #endif |
| 61 | break; |
| 62 | @@ -2881,6 +2882,13 @@ bool CApplication::Cleanup() |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | +void CApplication::SetExitCode(int exitCode) |
| 67 | +{ |
| 68 | + // save it for CEC |
| 69 | + m_ExitCode = exitCode; |
| 70 | + m_ExitCodeSet = true; |
| 71 | +} |
| 72 | + |
| 73 | void CApplication::Stop(int exitCode) |
| 74 | { |
| 75 | try |
| 76 | @@ -2888,7 +2896,7 @@ void CApplication::Stop(int exitCode) |
| 77 | m_frameMoveGuard.unlock(); |
| 78 | |
| 79 | CVariant vExitCode(CVariant::VariantTypeObject); |
| 80 | - vExitCode["exitcode"] = exitCode; |
| 81 | + vExitCode["exitcode"] = m_ExitCode; |
| 82 | CAnnouncementManager::GetInstance().Announce(System, "xbmc", "OnQuit", vExitCode); |
| 83 | |
| 84 | // Abort any active screensaver |
| 85 | @@ -2922,7 +2930,6 @@ void CApplication::Stop(int exitCode) |
| 86 | |
| 87 | m_bStop = true; |
| 88 | m_AppFocused = false; |
| 89 | - m_ExitCode = exitCode; |
| 90 | CLog::Log(LOGNOTICE, "stop all"); |
| 91 | |
| 92 | // cancel any jobs from the jobmanager |
| 93 | diff --git a/xbmc/Application.h b/xbmc/Application.h |
| 94 | index a9d9bf5..e536deb 100644 |
| 95 | --- a/xbmc/Application.h |
| 96 | +++ b/xbmc/Application.h |
| 97 | @@ -159,6 +159,7 @@ public: |
| 98 | void StopPVRManager(); |
| 99 | void ReinitPVRManager(); |
| 100 | bool IsCurrentThread() const; |
| 101 | + void SetExitCode(int exitCode); |
| 102 | void Stop(int exitCode); |
| 103 | void RestartApp(); |
| 104 | void UnloadSkin(bool forReload = false); |
| 105 | diff --git a/xbmc/XBApplicationEx.cpp b/xbmc/XBApplicationEx.cpp |
| 106 | index 035aed2..34102f5 100644 |
| 107 | --- a/xbmc/XBApplicationEx.cpp |
| 108 | +++ b/xbmc/XBApplicationEx.cpp |
| 109 | @@ -46,6 +46,7 @@ CXBApplicationEx::CXBApplicationEx() |
| 110 | m_bStop = false; |
| 111 | m_AppFocused = true; |
| 112 | m_ExitCode = EXITCODE_QUIT; |
| 113 | + m_ExitCodeSet = false; |
| 114 | m_renderGUI = false; |
| 115 | } |
| 116 | |
| 117 | diff --git a/xbmc/XBApplicationEx.h b/xbmc/XBApplicationEx.h |
| 118 | index 9bc14fa..f696b89 100644 |
| 119 | --- a/xbmc/XBApplicationEx.h |
| 120 | +++ b/xbmc/XBApplicationEx.h |
| 121 | @@ -42,6 +42,7 @@ public: |
| 122 | // Variables for timing |
| 123 | bool m_bStop; |
| 124 | int m_ExitCode; |
| 125 | + bool m_ExitCodeSet; |
| 126 | bool m_AppFocused; |
| 127 | bool m_renderGUI; |
| 128 | |
| 129 | diff --git a/xbmc/platform/posix/main.cpp b/xbmc/platform/posix/main.cpp |
| 130 | index a8b64e5..3d80032 100644 |
| 131 | --- a/xbmc/platform/posix/main.cpp |
| 132 | +++ b/xbmc/platform/posix/main.cpp |
| 133 | @@ -41,12 +41,27 @@ |
| 134 | #include "input/linux/LIRC.h" |
| 135 | #endif |
| 136 | #include "platform/XbmcContext.h" |
| 137 | +#include "Application.h" |
| 138 | + |
| 139 | +void xbmc_term_handler(int signum) |
| 140 | +{ |
| 141 | + CLog::Log(LOGINFO, "Received SIGTERM..."); |
| 142 | + if (!g_application.m_ExitCodeSet) |
| 143 | + g_application.SetExitCode(EXITCODE_RESTARTAPP); |
| 144 | + g_application.Stop(EXITCODE_RESTARTAPP); |
| 145 | +} |
| 146 | |
| 147 | #ifdef __cplusplus |
| 148 | extern "C" |
| 149 | #endif |
| 150 | int main(int argc, char* argv[]) |
| 151 | { |
| 152 | + // SIGTERM handler |
| 153 | + struct sigaction action; |
| 154 | + memset(&action, 0, sizeof(struct sigaction)); |
| 155 | + action.sa_handler = xbmc_term_handler; |
| 156 | + sigaction(SIGTERM, &action, NULL); |
| 157 | + |
| 158 | // set up some xbmc specific relationships |
| 159 | XBMC::Context context; |
| 160 | |
| 161 | -- |
| 162 | 2.10.2 |
| 163 | |