WIP support for custom shutdown hooks with possible shutdown prevention
#1
hi,

i was wondering, that there isn't any shutdown-hook-possibility integrated already. or did i missed something?
i thought i can not be the only one with this kind of usual use cases, that's why i wanna share the code,
maybe someone else has also usage for it. so far i patched and tested it with the current gotham branch from git with linux.

if someone has any criticism/suggestions about the code, ideas or whatever,
i would be happy to hear about!

what?:
  • easy integration of custom hook scripts, which will be executed right before xbmc exit/shutdown
  • the code is checking a predefined folder for custom hooks and executes all scripts inside this folder (until one of them returns an exit code not 0)
  • prevent xbmc from exit/shutdown (xbmc stays active in it's current state), if one of the custom hook scripts returns an exit code not 0
  • inform the user via xbmc-notifications if the exit/shutdown will be stopped by a custom hook script


why? - some of my use cases
  • this days i built a setup with xbmc and vdr on a single pc for a friend
    and i wanted to be able to prevent xbmc from exit/shutdown and inform the user,
    when a vdr timer is active or in 15min or less. if you are trying to solve that problem outside of xbmc, it's already to late.
    you could try to stop the shutdown with a custom script in your init-system,
    but xbmc would already be closed and you couldn't use it in a usual way from your couch until the blocking condition is done and the pc restarts the next time
    or you restart the xbmc process (which needs manul input or a custom remote key)
  • at my home i have a setup with multiple xbmc-clients, a vdr-server and a data-server. it's all seperated hardware.
    before i shutdown one of the xbmc-clients a script always checks, if the data-server is running
    and if anyone is using it right now (exept the client, which want's to shutdown).
    if this client is the only one, the data-server will be shutdown and all xbmc-clients in the house get a xbmc-notification,
    that the data-server isn't needed anymore and will be shutdown.
    otherwise the xbmc-clients will get a notification, that the server is still in use.
    in the past i solved this just with replacing the xbmc-shutdown function on my remote with a custom script.
    but now i prefer this integrated solution, because it's working without any skin or remote modifications, it's easy expandable and has more possibilities

any future plans? - stuff i was thinking about:
  • replacing curl binary with native c/c++ functions to reduce dependencies
  • gui option to enable/disable shutdown-hooks (old idea: checking only for shutdown-hooks if it's activated in advancedsettings)
  • giving the custom hook scripts the information which kind of 'exit' (exit, shutdown, reboot, ...) is going on as a parameter
    => this adds more possibilities to the custom hook scripts
  • os independence? is there really something else than linux? just kidding ;-)

what's about the os independence exatly?
i have to say that i'm a linux-only guy and i don't have any other os to test this.
that's also a reason, why i wanna make this feature optional via advancedsettings,
to make sure this will not gonna make problems on some other os, if you did'nt activated it.
at least i would say that i would have to add some os-switches for the path of the shutdown-hooks,
but i don't know if that would be enough.
maybe there are also different exit codes for scripting or some c/c++ functions missing.


best regards,
sven


my xbmc branch and patches are rebased against xbmc-gotham.git-867305b.

xbmc branch on github...............................................: https://github.com/svenburkard/xbmc/comm...nHookCheck
some example shutdown-hook-scripts on github: https://github.com/svenburkard/xbmcShutdownHooks

#-----------------------------------------------------------------------------------------------------------------------#
latest patches:
0001-custom-shutdown-hook-support-added-with-a-possible-e.patch
0002-shutdown-hook-folder-is-now-changed-to-an-available-.patch
0003-send-a-xbmc-notification-if-a-shutdown-hook-script-i.patch
0004-.deb-build-related-stuff-added-to-.gitignore-file.patch
0005-trailing-whitespaces-removed.patch
0006-improved-the-way-of-getting-the-home-directory-to-be.patch
0007-replaced-the-sendMSGtoXBMC-curl-hack-with-a-native-x.patch
0008-replaced-cout-with-a-native-xbmc-logging-function.patch
0009-improved-the-way-of-getting-the-home-directory-now-u.patch
0010-separated-the-functions-and-class-stuff-into-.h-.cpp.patch
0011-gui-option-added-for-enabling-disabling-of-shutdownH.patch

latest AllInOne Patch:
fb_shutdownHookCheck-AllInOnePatch-2014.09.13-f0e5f80.patch
#-----------------------------------------------------------------------------------------------------------------------#


EDIT:
latest patches added;
future plans modified;
Reply
#2
Just what i was looking for.

Can you tell me which commits i need to integrate this in my custom OpenELEC build (Gotham), is initial commit sufficient?

/edit:
Maybe provide some example script?
For example logoff user before going in hibernate/shutdown?
Reply
#3
(2014-09-04, 14:56)schumi2004 Wrote: Just what i was looking for.

Can you tell me which commits i need to integrate this in my custom OpenELEC build (Gotham), is initial commit sufficient?

/edit:
Maybe provide some example script?
For example logoff user before going in hibernate/shutdown?

here (link removed: see first post for patches) is a patch with all commits of this branch until now, to make implementing and testing easier.

to apply the patch:
Code:
git am fb_shutdownHookCheck-2014.09.04-66eb2cc.patch

i only tested it with xbmc gotham, generally i think this changes should also work on openelec.
but it could be, that i first have to replace my notification function. because the code is looking for '/usr/bin/curl',
which is maybe not available at your openelec. shouldn't be a big deal, i will replace this curl hack at the weekend.

the code in xbmc is looking for a directory called 'xbmcShutdownHooks/hooks-enabled/' in the home directory of the xbmc user.
my scripts are in '~/xbmcShutdownHooks/hooks-available/' and i just make softlinks to activate them. (like apache modules/sites style)

you can have a look at my hook repository on github, which i'm using in my xbmc users home directory on a debian system.
https://github.com/svenburkard/xbmcShutdownHooks

at the moment there are hook-scripts for:
  • checking for users still logged in (just think about the last time you configured something over ssh and someone else shutdown your xbmc client...)
  • checking for active or upcoming vdr recordings

in the hook-scripts i already wrote some c++ code to send xbmc-notifications,
which i also wanna use in xbmc to replace my curl binary hacks.

so far i didn't wrote any documentation, so here in short to build it:
Code:
cd ~/xbmcShutdownHooks/ && g++ src/sendMSGtoXBMC.cpp -o bin/sendMSGtoXBMC -lcurl


EDIT:
link removed: see first post for patches
Reply
#4
I like the idea, could really be useful.
If you want to get this in mainline, it needs at least using native methods to pop notifications and moving the logic from the header to a cpp.
Reply
#5
(2014-09-04, 21:22)sven88 Wrote: i only tested it with xbmc gotham, generally i think this changes should also work on openelec.
but it could be, that i first have to replace my notification function. because the code is looking for '/usr/bin/curl',
which is maybe not available at your openelec. shouldn't be a big deal, i will replace this curl hack at the weekend.

Thanks for explaining.
Openelec does have curl so that shouldn't be a problem:

openelec:~ # which curl
/usr/bin/curl

Going to compile and test it.


/edit:
Okay, compiling went fine after i changed the patch a bit for part @@ -259,36 +276,51 @@ void CApplicationMessenger:TonguerocessMessage(ThreadMessage *pMsg)
Probably because OE hasn't updated XBMC code to latest Gotham yet. Going to test later today.
Reply
#6
For OpenElec, xbmc-send -a "Notification(Hey,Hey this is a message)"
Reply
#7
OE always has latest code.
Read/follow the forum rules.
For troubleshooting and bug reporting, read this first
Interested in seeing some YouTube videos about Kodi? Go here and subscribe
Reply
#8
(2014-09-05, 20:30)Martijn Wrote: OE always has latest code.

Your right at some point, it doesn't do daily updates to XBMC code. (Can't expect them to do so and don't want them to)
Anyway, or sven88 based his patch on older/newer XBMC code then OE was using or vice versa.
In my case I needed to change that part mentioned to match XBMC version used in latest OE 4.1.5, not such a big deal.
Reply
#9
(2014-09-04, 21:56)wsnipex Wrote: I like the idea, could really be useful.
If you want to get this in mainline, it needs at least using native methods to pop notifications and moving the logic from the header to a cpp.

ok, i will look into this. are there any lists/documentations available for common xbmc functions to get started?
i already had a look at the wiki, but just found built-in functions to use inside of addons.
so far i just did a short grep for 'notification' through the sourcecode, but there are so much matches, that i have to take a deeper look.

(2014-09-05, 09:47)schumi2004 Wrote:
(2014-09-04, 21:22)sven88 Wrote: i only tested it with xbmc gotham, generally i think this changes should also work on openelec.
but it could be, that i first have to replace my notification function. because the code is looking for '/usr/bin/curl',
which is maybe not available at your openelec. shouldn't be a big deal, i will replace this curl hack at the weekend.

Thanks for explaining.
Openelec does have curl so that shouldn't be a problem:

openelec:~ # which curl
/usr/bin/curl

Going to compile and test it.


/edit:
Okay, compiling went fine after i changed the patch a bit for part @@ -259,36 +276,51 @@ void CApplicationMessenger:TonguerocessMessage(ThreadMessage *pMsg)
Probably because OE hasn't updated XBMC code to latest Gotham yet. Going to test later today.


ok, good news that curl is available on openelec, but i wanted to change this curl hack anyway.

but today i remembered something else, which could be a problem.
the way how i got the home directory path.
because in my code i thought to straight about the common linux paths (/home/..) and i think openelec has /storage.

now i'm looking for the xbmc users home env variable, to be more flexible for openelec.
so i think you will have more luck, if you also apply the latest patch: 0006-improved-the-way-of-getting-the-home-directory-to-be.patch
Reply
#10
(2014-09-05, 20:39)sven88 Wrote:
(2014-09-04, 21:56)wsnipex Wrote: I like the idea, could really be useful.
If you want to get this in mainline, it needs at least using native methods to pop notifications and moving the logic from the header to a cpp.

ok, i will look into this. are there any lists/documentations available for common xbmc functions to get started?
i already had a look at the wiki, but just found built-in functions to use inside of addons.
so far i just did a short grep for 'notification' through the sourcecode, but there are so much matches, that i have to take a deeper look.

check xbmc/dialogs/GUIDialogKaiToast.h
Reply
#11
(2014-09-05, 20:34)schumi2004 Wrote:
(2014-09-05, 20:30)Martijn Wrote: OE always has latest code.

Your right at some point, it doesn't do daily updates to XBMC code. (Can't expect them to do so and don't want them to)
Anyway, or sven88 based his patch on older/newer XBMC code then OE was using or vice versa.
In my case I needed to change that part mentioned to match XBMC version used in latest OE 4.1.5, not such a big deal.

to make some things clear, my patches are based on b79e5f1 at the moment.
so, it's a 4 weeks old version of xbmc gotham right now.

(2014-09-05, 20:47)wsnipex Wrote:
(2014-09-05, 20:39)sven88 Wrote:
(2014-09-04, 21:56)wsnipex Wrote: I like the idea, could really be useful.
If you want to get this in mainline, it needs at least using native methods to pop notifications and moving the logic from the header to a cpp.

ok, i will look into this. are there any lists/documentations available for common xbmc functions to get started?
i already had a look at the wiki, but just found built-in functions to use inside of addons.
so far i just did a short grep for 'notification' through the sourcecode, but there are so much matches, that i have to take a deeper look.

check xbmc/dialogs/GUIDialogKaiToast.h

thanks
Reply
#12
(2014-09-05, 21:00)sven88 Wrote:
(2014-09-05, 20:34)schumi2004 Wrote:
(2014-09-05, 20:30)Martijn Wrote: OE always has latest code.

Your right at some point, it doesn't do daily updates to XBMC code. (Can't expect them to do so and don't want them to)
Anyway, or sven88 based his patch on older/newer XBMC code then OE was using or vice versa.
In my case I needed to change that part mentioned to match XBMC version used in latest OE 4.1.5, not such a big deal.

to make some things clear, my patches are based on b79e5f1 at the moment.
so, it's a 4 weeks old version of xbmc gotham right now.
That figures, OE is @0f3db05

Compiling with your newest patch now and testing afterwards.
Reply
#13
I was thinking that wouldn't this be more user friendly if you could select a script from GUI instead of making folders and place scripts in it?

/EDIT:
Ideal would be to have a extra option in power saving settings to or shutdown/hibernate as normal or only run a script and let user decide (in script) to shutdown/hibernate or not.

Or better, have the Shutdown Function list have a extra option "Run script" and an extra selection afterwards to do a shutdown/hibernate as user is used to or not?

And what about multiple users (profiles) with separate scripts?

/EDIT2:
I probably did something wrong but it didn't worked as expected with a simple user logoff python script

Code:
#!/usr/bin/python

import xbmc

xbmc.executebuiltin('PlayerControl(Stop)')
xbmc.executebuiltin('ActivateWindow(home)')
xbmc.executebuiltin('System.LogOff()')

exit(0)

I created the needed dirs in storage folder (openelec:~/xbmcShutdownHooks/hooks-enabled #) and placed script logoff.py in it and shutdown xbmc
When it came back i didn't see the expected user login window. I did see a notification window "xbmcShutdownHooks stopped by sleep.py"
Reply
#14
tbh honest i don't think how you implemented this is in any way user friendly.
you would have to be some "serious" tech savy guy to use this. I would try to aim a little for the lesser tech people as well.
Read/follow the forum rules.
For troubleshooting and bug reporting, read this first
Interested in seeing some YouTube videos about Kodi? Go here and subscribe
Reply
#15
(2014-09-05, 21:24)schumi2004 Wrote: I was thinking that wouldn't this be more user friendly if you could select a script from GUI instead of making folders and place scripts in it?
(2014-09-05, 21:37)Martijn Wrote: tbh honest i don't think how you implemented this is in any way user friendly.
you would have to be some "serious" tech savy guy to use this. I would try to aim a little for the lesser tech people as well.

yes i agree with you guys schumi and martijn,
that this solution isn't easy to use for a default user, without technical background.
but i did not thought this could be an interesting feature for non-tech users, because they couldn't write their own scripts anyway.
so, if there would be a good idea to make this easier, i would like to improve this of course.


(2014-09-05, 21:24)schumi2004 Wrote: /EDIT:
Ideal would be to have a extra option in power saving settings to or shutdown/hibernate as normal or only run a script and let user decide (in script) to shutdown/hibernate or not.

the 'extra-option' suggestion of schumi, could be a good way, right?

maybe this extra option in the gui settings should show a dynamic list of all available hook scripts from the predefined directory,
and make it possible to enable/disable them each per click.

but you still would need to write/clone/cp/... your custom hook scripts at the commandline anyway,
if you wanna use real custom hook scripts, for your own use cases.
so, the full possibilitys could only be used by advanced users anyway,
because the non-tech users could only activate from some predefined common scripts and not write their own scripts.

i also like the idea of a user interaction for some use cases,
to show a window with a question if you really wanna abort the shutdown, if a scripts has not an exit code of 0.
but i wouldn't make this default, this should be configurable in the gui for each of the scripts.
for example i don't want to get asked everytime at a script, which checks for active vdr recordings,
because i never would shutdown xbmc while recording, this would just get anoying.


(2014-09-05, 21:24)schumi2004 Wrote: Or better, have the Shutdown Function list have a extra option "Run script" and an extra selection afterwards to do a shutdown/hibernate as user is used to or not?

i don't see the point in doing that for just a manual single script execute,
this behavior could already be added with a custom remote key or custom button in a skin.
but i wanted to be able to catch every attempt for shutdown/reboot/...,
doesn't matter how you try to do this and automatic execute multiple and expandable scripts before and to be able to abort the shutdown.


(2014-09-05, 21:24)schumi2004 Wrote: And what about multiple users (profiles) with separate scripts?

that is something i didn't thought about yet, i do not have this use case.
but if the hook settings would be done through the gui,
i could imagine that there is somehow a 'xbmc-profile-name-env' variable available in xbmc,
which could be used to get this personalized.


(2014-09-05, 21:24)schumi2004 Wrote: /EDIT2:
I probably did something wrong but it didn't worked as expected with a simple user logoff python script

Code:
#!/usr/bin/python

import xbmc

xbmc.executebuiltin('PlayerControl(Stop)')
xbmc.executebuiltin('ActivateWindow(home)')
xbmc.executebuiltin('System.LogOff()')

exit(0)

I created the needed dirs in storage folder (openelec:~/xbmcShutdownHooks/hooks-enabled #) and placed script logoff.py in it and shutdown xbmc
When it came back i didn't see the expected user login window. I did see a notification window "xbmcShutdownHooks stopped by sleep.py"


i have no experience with python, but if you get the message 'shutdown stopped by sleep.py',
it looks like sleep.py didn't closed with an exit code of 0.

did you mean sleep.py instead of logoff.py with your example code,
or what is inside sleep.py?

i tried to execute your example code and at my system it fails because of the import.
maybe 'import xbmc' works only inside of xbmc itself.
but when executing outside of xbmc, it maybe doesn't finds the libs, at least in my case it looks like that.

did you also executed your file manual for testing?
i would check the exit code of the script and test something without lib dependencies first (like just a script, which exits with an exit code of 0 and another script with an exit code 1)

this is what i tested:

sleep.py:
Code:
#!/usr/bin/python

print "test print #0"

import xbmc

xbmc.executebuiltin('PlayerControl(Stop)')
xbmc.executebuiltin('ActivateWindow(home)')
xbmc.executebuiltin('System.LogOff()')

print "test print #1"

exit(0)

test.pl:
Code:
#!/usr/bin/perl

use strict;
use warnings;

my $code = system('./sleep.py');
print "result:>$code<\n";

Code:
xbmc@xbmcUS:~/xbmcShutdownHooks/hooks-available$ [master] # ./test.pl

test print #0
Traceback (most recent call last):
  File "./sleep.py", line 5, in <module>
    import xbmc
ImportError: No module named xbmc
code:>256<


at my system it isn't able to load the lib, so it's crashing, not doing my second test print
and fails with an exit code of 256 and that is why the shutdown is stopped at my system.
Reply

Logout Mark Read Team Forum Stats Members Help
support for custom shutdown hooks with possible shutdown prevention0