From 2a343037d733e4d63c8cc6cd4b642260bc2d25b9 Mon Sep 17 00:00:00 2001 From: marios8543 Date: Tue, 14 Nov 2023 22:50:16 +0200 Subject: Infrastructure for custom backend support --- backend/decky_loader/plugin/binary_plugin.py | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 backend/decky_loader/plugin/binary_plugin.py (limited to 'backend/decky_loader/plugin/binary_plugin.py') diff --git a/backend/decky_loader/plugin/binary_plugin.py b/backend/decky_loader/plugin/binary_plugin.py new file mode 100644 index 00000000..d1b8cea9 --- /dev/null +++ b/backend/decky_loader/plugin/binary_plugin.py @@ -0,0 +1,54 @@ +from asyncio import StreamReader, create_task, sleep, create_subprocess_exec +from asyncio.subprocess import Process +from subprocess import PIPE + +from .sandboxed_plugin import SandboxedPlugin +from ..localplatform.localsocket import LocalSocket +from ..customtypes import UserType + +from typing import Dict, List + +class BinaryPlugin(SandboxedPlugin): + def __init__(self, + socket: LocalSocket, + name: str, + flags: List[str], + file: str, + plugin_directory: str, + plugin_path: str, + version: str | None, + author: str, + env: Dict[str, str]) -> None: + super().__init__(socket, name, flags, file, plugin_directory, plugin_path, version, author, env) + self.process: Process + + def start(self): + create_task(self._start()) + + async def stop(self): + self.process.terminate() + while not self.process.returncode: + await sleep(0) + + async def _start(self): + self.env["DECKY_SOCKET"] = self.socket.socket_addr + user_type = UserType.ROOT.value if "root" in self.flags else UserType.HOST_USER.value + self.process = await create_subprocess_exec(self.file, + env=self.env, + user=user_type, + group=user_type, + stdout=PIPE, + stderr=PIPE) + assert self.process.stderr and self.process.stdout + create_task(self._stream_watcher(self.process.stdout, False)) + create_task(self._stream_watcher(self.process.stderr, True)) + + async def _stream_watcher(self, stream: StreamReader, is_err: bool): + async for line in stream: + line = line.decode("utf-8") + if not line.strip(): + continue + if is_err: + self.log.error(line) + else: + self.log.info(line) \ No newline at end of file -- cgit v1.2.3