diff options
| author | marios <marios8543@gmail.com> | 2022-04-13 02:14:44 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-13 02:14:44 +0300 |
| commit | e3d7b50bd91fa092a9d40be3ce016c330f9311ca (patch) | |
| tree | 8c8dc24fa16c04757631da96f797c8d190c1fde8 /plugin_loader/plugin.py | |
| parent | 0359fd966a5a33cd646202d0349ad5bdf01a6d1a (diff) | |
| download | decky-loader-e3d7b50bd91fa092a9d40be3ce016c330f9311ca.tar.gz decky-loader-e3d7b50bd91fa092a9d40be3ce016c330f9311ca.zip | |
Root plugins (#35)
* root plugins
plugins can now specify if they want their methods to be ran as root. this is done via the multiprocess module. method calls are delegated to a separate process that is then down-privileged by default to user 1000, so the loader can safely be ran as root
except it isn't really safe because the plugin is imported as root anyway
* working implementation
- follows the new plugin format with the plugin.json file
- plugins are loaded in their own isolated process along with their own event loop and unix socket server for calling methods
- private methods are now prepended with _ instead of __
* converted format to f-strings
Diffstat (limited to 'plugin_loader/plugin.py')
| -rw-r--r-- | plugin_loader/plugin.py | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/plugin_loader/plugin.py b/plugin_loader/plugin.py new file mode 100644 index 00000000..cd737c72 --- /dev/null +++ b/plugin_loader/plugin.py @@ -0,0 +1,88 @@ +from importlib.util import spec_from_file_location, module_from_spec +from asyncio import get_event_loop, start_unix_server, open_unix_connection, sleep, Lock +from os import path, setuid +from json import loads, dumps, load +from concurrent.futures import ProcessPoolExecutor +from time import time + +class PluginWrapper: + def __init__(self, file, plugin_directory, plugin_path) -> None: + self.file = file + self.plugin_directory = plugin_directory + self.reader = None + self.writer = None + self.socket_addr = f"/tmp/plugin_socket_{time()}" + self.method_call_lock = Lock() + + json = load(open(path.join(plugin_path, plugin_directory, "plugin.json"), "r")) + + self.name = json["name"] + self.author = json["author"] + self.main_view_html = json["main_view_html"] + self.tile_view_html = json["tile_view_html"] if "tile_view_html" in json else "" + self.flags = json["flags"] + + def _init(self): + setuid(0 if "root" in self.flags else 1000) + spec = spec_from_file_location("_", self.file) + module = module_from_spec(spec) + spec.loader.exec_module(module) + self.Plugin = module.Plugin + + if hasattr(self.Plugin, "_main"): + get_event_loop().create_task(self.Plugin._main(self.Plugin)) + get_event_loop().create_task(self._setup_socket()) + get_event_loop().run_forever() + + async def _setup_socket(self): + self.socket = await start_unix_server(self._listen_for_method_call, path=self.socket_addr) + + async def _listen_for_method_call(self, reader, writer): + while True: + data = loads((await reader.readline()).decode("utf-8")) + if "stop" in data: + return get_event_loop().stop() + d = {"res": None, "success": True} + try: + d["res"] = await getattr(self.Plugin, data["method"])(self.Plugin, **data["args"]) + except Exception as e: + d["res"] = str(e) + d["success"] = False + finally: + writer.write((dumps(d)+"\n").encode("utf-8")) + await writer.drain() + + async def _open_socket_if_not_exists(self): + if not self.reader: + while True: + try: + self.reader, self.writer = await open_unix_connection(self.socket_addr) + break + except: + await sleep(0) + + def start(self, loop): + executor = ProcessPoolExecutor() + loop.run_in_executor( + executor, + self._init + ) + return self + + def stop(self, loop): + async def _(self): + await self._open_socket_if_not_exists() + self.writer.write((dumps({"stop": True})+"\n").encode("utf-8")) + await self.writer.drain() + loop.create_task(_(self)) + + async def execute_method(self, method_name, kwargs): + async with self.method_call_lock: + await self._open_socket_if_not_exists() + self.writer.write( + (dumps({"method": method_name, "args": kwargs})+"\n").encode("utf-8")) + await self.writer.drain() + res = loads((await self.reader.readline()).decode("utf-8")) + if not res["success"]: + raise Exception(res["res"]) + return res["res"] |
