OpenELEC/OpenELEC.tv — pull request #3297 by stefansaraev (2014-06-05 16:33:53+00:00)
Title: xbmc: handle SIGTERM

In some situations, due to deadlocks or crashes, xbmc fails to exit properly in
CApplication::Stop(), so g_powerManager.Reboot() / g_powerManager.Powerdown() never gets
called, that's a big no-no for openelec as our users can't shutdown or reboot.
There are few addons reported to cause this behaviour: trakkt.tv, watchdog, weather.underground.

I've also noticed that sometimes new threads (FileCache ??) get started AFTER
CApplication::Stop() is called, delaying shutdown with 1+ minute (2 curl timeoouts?).
The problem seems to be in CJobManager::CancelJobs() but I am too lame to understand where,
why, and how to fix it.

To me, it seems best to let systemd handle it. systemd sends SIGTERM, then waits
TimeoutStopSec=xx seconds then sends SIGKILL,s so dont call g_application.Stop() from
ApplicationMessenger as it can not be trusted. save the requested exitcode instead
(that's required for CEC to switch off the tv) and do it from a simple SIGTERM handler instead.

CEC thread has enough time (5 seconds in xbmc.service) to switch of the TV after receiving OnQuit.
As a side "effect", now guisettings.xml will ALWAYS be saved, even if shutdown / rebooot
is requested externaly (ssh, 3rdparty script).

tested on current xbmc-master (Helix). my LG tv is broken and pulse8 cec adapter fails to switch it off due to lg tv firmware limitation, but according to xbmc.log everything is fine, it should switch off only if shutdown is requested and do nothing if reboot is requested.

I would like to ask RPi / pulse8 usb adapter users to test on Gotham. if everything with cec on shutdown/reboot is fine, this should be backported to 4.0 branch. for non-cec users this PR is safe.