From 89e6d75a409445cc6c3240ea31191317ecf06f8e Mon Sep 17 00:00:00 2001 From: Matthias Reichl Date: Tue, 5 Sep 2017 09:17:30 +0200 Subject: [PATCH] kodi: refactor service addon wrapper Simplify the kodi addon wrapper patch, pass call hook context and addon path on to wrapper instead of just enable/disable. The logic of what should happen with service addons on enable/disable/install/uninstall is moved out of kodi into the wrapper script. Also add restart and reload contexts so service addons can call the wrapper from python code on settings change instead of having to directly interact with systemctl. Signed-off-by: Matthias Reichl --- packages/mediacenter/kodi/package.mk | 4 +- ...se-a-wrapper-to-setup-service-addons.patch | 89 ++++++++++++++++++ ...-a-wrapper-to-setup-systemd-services.patch | 94 ------------------- .../kodi/scripts/service-addon-wrapper | 66 +++++++++++++ .../kodi/scripts/systemd-addon-wrapper | 35 ------- 5 files changed, 157 insertions(+), 131 deletions(-) create mode 100644 packages/mediacenter/kodi/patches/kodi-100.09-use-a-wrapper-to-setup-service-addons.patch delete mode 100644 packages/mediacenter/kodi/patches/kodi-100.09-use-a-wrapper-to-setup-systemd-services.patch create mode 100755 packages/mediacenter/kodi/scripts/service-addon-wrapper delete mode 100755 packages/mediacenter/kodi/scripts/systemd-addon-wrapper diff --git a/packages/mediacenter/kodi/package.mk b/packages/mediacenter/kodi/package.mk index efa80c96ca..08692726a8 100644 --- a/packages/mediacenter/kodi/package.mk +++ b/packages/mediacenter/kodi/package.mk @@ -271,8 +271,8 @@ post_makeinstall_target() { cp $PKG_DIR/scripts/kodi-config $INSTALL/usr/lib/kodi cp $PKG_DIR/scripts/kodi.sh $INSTALL/usr/lib/kodi - mkdir -p $INSTALL/usr/lib/libreelec - cp $PKG_DIR/scripts/systemd-addon-wrapper $INSTALL/usr/lib/libreelec + mkdir -p $INSTALL/usr/sbin + cp $PKG_DIR/scripts/service-addon-wrapper $INSTALL/usr/sbin mkdir -p $INSTALL/usr/bin cp $PKG_DIR/scripts/cputemp $INSTALL/usr/bin diff --git a/packages/mediacenter/kodi/patches/kodi-100.09-use-a-wrapper-to-setup-service-addons.patch b/packages/mediacenter/kodi/patches/kodi-100.09-use-a-wrapper-to-setup-service-addons.patch new file mode 100644 index 0000000000..84d2f12a01 --- /dev/null +++ b/packages/mediacenter/kodi/patches/kodi-100.09-use-a-wrapper-to-setup-service-addons.patch @@ -0,0 +1,89 @@ +diff --git a/xbmc/addons/Addon.cpp b/xbmc/addons/Addon.cpp +index 45f42ad2e3..ce10080469 100644 +--- a/xbmc/addons/Addon.cpp ++++ b/xbmc/addons/Addon.cpp +@@ -397,13 +397,48 @@ AddonVersion CAddon::GetDependencyVersion(const std::string &dependencyID) const + return AddonVersion("0.0.0"); + } + ++void LEAddonHook(const AddonPtr& addon, const LE_ADDON_CONTEXT context) { ++ ++ if (addon->Type() == ADDON_SERVICE) { ++ std::string contextStr; ++ char cmd[255]; ++ ++ switch (context) { ++ case LE_ADDON_ENABLED: ++ contextStr = "enable"; ++ break; ++ case LE_ADDON_DISABLED: ++ contextStr = "disable"; ++ break; ++ case LE_ADDON_POST_INSTALL: ++ contextStr = "post-install"; ++ break; ++ case LE_ADDON_PRE_UNINSTALL: ++ contextStr = "pre-uninstall"; ++ break; ++ default: ++ contextStr = StringUtils::Format("unknown(%d)", context); ++ break; ++ } ++ ++ snprintf(cmd, sizeof(cmd), "/usr/sbin/service-addon-wrapper %s %s %s", ++ contextStr.c_str(), addon->ID().c_str(), addon->Path().c_str()); ++ ++ system(cmd); ++ } ++} ++ + void OnEnabled(const AddonPtr& addon) + { ++ LEAddonHook(addon, LE_ADDON_ENABLED); ++ + addon->OnEnabled(); + } + + void OnDisabled(const AddonPtr& addon) + { ++ LEAddonHook(addon, LE_ADDON_DISABLED); ++ + addon->OnDisabled(); + } + +@@ -438,11 +476,15 @@ void OnPostInstall(const AddonPtr& addon, bool update, bool modal) + } + // OE + ++ LEAddonHook(addon, LE_ADDON_POST_INSTALL); ++ + addon->OnPostInstall(update, modal); + } + + void OnPreUnInstall(const AddonPtr& addon) + { ++ LEAddonHook(addon, LE_ADDON_PRE_UNINSTALL); ++ + addon->OnPreUnInstall(); + } + +diff --git a/xbmc/addons/Addon.h b/xbmc/addons/Addon.h +index 95f3f23437..23ad39116a 100644 +--- a/xbmc/addons/Addon.h ++++ b/xbmc/addons/Addon.h +@@ -43,6 +43,15 @@ void OnPostInstall(const AddonPtr& addon, bool update, bool modal); + void OnPreUnInstall(const AddonPtr& addon); + void OnPostUnInstall(const AddonPtr& addon); + ++typedef enum { ++ LE_ADDON_ENABLED, ++ LE_ADDON_DISABLED, ++ LE_ADDON_POST_INSTALL, ++ LE_ADDON_PRE_UNINSTALL, ++} LE_ADDON_CONTEXT; ++ ++void LEAddonHook(const AddonPtr& addon, const LE_ADDON_CONTEXT context); ++ + class CAddon : public IAddon + { + public: diff --git a/packages/mediacenter/kodi/patches/kodi-100.09-use-a-wrapper-to-setup-systemd-services.patch b/packages/mediacenter/kodi/patches/kodi-100.09-use-a-wrapper-to-setup-systemd-services.patch deleted file mode 100644 index 8b83bb8bca..0000000000 --- a/packages/mediacenter/kodi/patches/kodi-100.09-use-a-wrapper-to-setup-systemd-services.patch +++ /dev/null @@ -1,94 +0,0 @@ -From 4abf9ef52d9f5cf625e196935683eb35f150e7e9 Mon Sep 17 00:00:00 2001 -From: MilhouseVH -Date: Tue, 11 Jul 2017 17:46:48 +0100 -Subject: [PATCH] use a wrapper to setup systemd services - ---- - xbmc/addons/Addon.cpp | 38 ++++++++++++++++++++++++++++++++++++++ - xbmc/addons/Addon.h | 2 ++ - 2 files changed, 40 insertions(+) - -diff --git a/xbmc/addons/Addon.cpp b/xbmc/addons/Addon.cpp -index 45f42ad..5870328 100644 ---- a/xbmc/addons/Addon.cpp -+++ b/xbmc/addons/Addon.cpp -@@ -397,13 +397,34 @@ AddonVersion CAddon::GetDependencyVersion(const std::string &dependencyID) const - return AddonVersion("0.0.0"); - } - -+void CallOEWrapper(const std::string& ID, bool disable) -+{ -+ char cmd[255]; -+ snprintf(cmd, sizeof(cmd), "/usr/lib/libreelec/systemd-addon-wrapper %s %d", ID.c_str(), disable); -+ system(cmd); -+} -+ - void OnEnabled(const AddonPtr& addon) - { -+ AddonPtr localAddon; -+ -+ // OE: systemctl enable & start on addon enable -+ if (CServiceBroker::GetAddonMgr().GetAddon(addon->ID(), localAddon, ADDON_SERVICE)) -+ CallOEWrapper(addon->ID(), false); -+ // OE -+ - addon->OnEnabled(); - } - - void OnDisabled(const AddonPtr& addon) - { -+ AddonPtr localAddon; -+ -+ // OE: systemctl stop & disable on addon disable -+ if (CServiceBroker::GetAddonMgr().GetAddon(addon->ID(), localAddon, ADDON_SERVICE, false)) -+ CallOEWrapper(addon->ID(), true); -+ // OE -+ - addon->OnDisabled(); - } - -@@ -438,11 +459,28 @@ void OnPostInstall(const AddonPtr& addon, bool update, bool modal) - } - // OE - -+ AddonPtr localAddon; -+ -+ // OE: systemctl stop & disable / enable & start on addon upgrade -+ if (CServiceBroker::GetAddonMgr().GetAddon(addon->ID(), localAddon, ADDON_SERVICE)) -+ { -+ CallOEWrapper(addon->ID(), true); -+ CallOEWrapper(addon->ID(), false); -+ } -+ // OE -+ - addon->OnPostInstall(update, modal); - } - - void OnPreUnInstall(const AddonPtr& addon) - { -+ AddonPtr localAddon; -+ -+ // OE: systemctl stop & disable on addon uninstall -+ if (CServiceBroker::GetAddonMgr().GetAddon(addon->ID(), localAddon, ADDON_SERVICE)) -+ CallOEWrapper(addon->ID(), true); -+ // OE -+ - addon->OnPreUnInstall(); - } - -diff --git a/xbmc/addons/Addon.h b/xbmc/addons/Addon.h -index 95f3f23..5f2223c 100644 ---- a/xbmc/addons/Addon.h -+++ b/xbmc/addons/Addon.h -@@ -77,6 +77,8 @@ public: - const InfoMap& ExtraInfo() const override { return m_addonInfo.ExtraInfo(); } - const ADDONDEPS& GetDeps() const override { return m_addonInfo.GetDeps(); } - -+ void CallOEWrapper(const std::string& ID, bool disable); -+ - std::string FanArt() const override - { - auto it = m_addonInfo.Art().find("fanart"); --- -2.7.4 - diff --git a/packages/mediacenter/kodi/scripts/service-addon-wrapper b/packages/mediacenter/kodi/scripts/service-addon-wrapper new file mode 100755 index 0000000000..ced608c6ba --- /dev/null +++ b/packages/mediacenter/kodi/scripts/service-addon-wrapper @@ -0,0 +1,66 @@ +#!/bin/sh +################################################################################ +# This file is part of LibreELEC - https://www.libreelec.tv +# Copyright (C) 2017-present Team LibreELEC +# +# LibreELEC is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# LibreELEC is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with LibreELEC. If not, see . +################################################################################ + +if [ $# -ne 3 ] ; then + echo "$0 usage: context addon-id addon-path" + exit 1 +fi + +CONTEXT="$1" +ADDON_ID="$2" +ADDON_PATH="$3" + +if [ ! -d /storage/.config/system.d ] ; then + mkdir -p /storage/.config/system.d +fi + +SERVICE_FILE="${ADDON_PATH}/system.d/${ADDON_ID}.service" + +if [ -f "${SERVICE_FILE}" ] ; then + case "${CONTEXT}" in + enable) + systemctl enable "${SERVICE_FILE}" + chmod +x "${ADDON_PATH}/bin"/* + systemctl start "${ADDON_ID}.service" + ;; + disable | pre-uninstall) + systemctl stop "${ADDON_ID}.service" + systemctl disable "${ADDON_ID}.service" + ;; + post-install) + # post-install is triggered on update as well, + # make sure to stop and re-install service + systemctl stop "${ADDON_ID}.service" + systemctl disable "${ADDON_ID}.service" + systemctl enable "${SERVICE_FILE}" + chmod +x "${ADDON_PATH}/bin"/* + systemctl start "${ADDON_ID}.service" + ;; + restart) + systemctl restart "${ADDON_ID}.service" + ;; + reload) + systemctl reload "${ADDON_ID}.service" + ;; + *) + echo "$0: unknown service context $CONTEXT" + exit 1 + ;; + esac +fi diff --git a/packages/mediacenter/kodi/scripts/systemd-addon-wrapper b/packages/mediacenter/kodi/scripts/systemd-addon-wrapper deleted file mode 100755 index 32630c8ca0..0000000000 --- a/packages/mediacenter/kodi/scripts/systemd-addon-wrapper +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/sh -################################################################################ -# This file is part of OpenELEC - http://www.openelec.tv -# Copyright (C) 2009-2014 Stephan Raue (stephan@openelec.tv) -# -# OpenELEC is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 2 of the License, or -# (at your option) any later version. -# -# OpenELEC is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with OpenELEC. If not, see . -################################################################################ - -if [ ! -d /storage/.config/system.d ] ; then - mkdir -p /storage/.config/system.d -fi - -if [ -f "/storage/.kodi/addons/$1/system.d/$1.service" ] ; then - if [ "_$2" = "_1" ] ; then - # disable = true: cleanup - systemctl stop "$1.service" - systemctl disable "$1.service" - else - # disable = false: setup - systemctl enable "/storage/.kodi/addons/$1/system.d/$1.service" - chmod +x "/storage/.kodi/addons/$1/bin"/* - systemctl start "$1.service" - fi -fi