summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--kvmd/aioproc.py48
-rw-r--r--kvmd/apps/kvmd/streamer.py18
-rw-r--r--kvmd/plugins/msd/otg/helpers.py14
-rw-r--r--testenv/tests/plugins/auth/test_pam.py5
4 files changed, 58 insertions, 27 deletions
diff --git a/kvmd/aioproc.py b/kvmd/aioproc.py
new file mode 100644
index 00000000..a778141d
--- /dev/null
+++ b/kvmd/aioproc.py
@@ -0,0 +1,48 @@
+# ========================================================================== #
+# #
+# KVMD - The main Pi-KVM daemon. #
+# #
+# Copyright (C) 2018 Maxim Devaev <[email protected]> #
+# #
+# This program 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 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program 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 this program. If not, see <https://www.gnu.org/licenses/>. #
+# #
+# ========================================================================== #
+
+
+import asyncio
+import asyncio.subprocess
+import signal
+
+from typing import Tuple
+from typing import List
+
+
+# =====
+async def run_process(cmd: List[str], err_to_null: bool=False) -> asyncio.subprocess.Process: # pylint: disable=no-member
+ return (await asyncio.create_subprocess_exec(
+ *cmd,
+ stdout=asyncio.subprocess.PIPE,
+ stderr=(asyncio.subprocess.DEVNULL if err_to_null else asyncio.subprocess.STDOUT),
+ preexec_fn=preexec_ignore_sigint,
+ ))
+
+
+async def read_process(cmd: List[str], err_to_null: bool=False) -> Tuple[asyncio.subprocess.Process, str]: # pylint: disable=no-member
+ proc = await run_process(cmd, err_to_null)
+ (stdout, _) = await proc.communicate()
+ return (proc, stdout.decode(errors="ignore").strip())
+
+
+def preexec_ignore_sigint() -> None:
+ signal.signal(signal.SIGINT, signal.SIG_IGN)
diff --git a/kvmd/apps/kvmd/streamer.py b/kvmd/apps/kvmd/streamer.py
index 357a46bb..7f527f69 100644
--- a/kvmd/apps/kvmd/streamer.py
+++ b/kvmd/apps/kvmd/streamer.py
@@ -36,6 +36,7 @@ import aiohttp
from ...logging import get_logger
from ... import aiotools
+from ... import aioproc
from ... import htclient
from ... import gpio
@@ -217,16 +218,10 @@ class Streamer: # pylint: disable=too-many-instance-attributes
waiter_task = None
async def get_info(self) -> Dict:
- proc = await asyncio.create_subprocess_exec(
- *[self.__cmd[0], "--version"],
- stdout=asyncio.subprocess.PIPE,
- stderr=asyncio.subprocess.DEVNULL,
- preexec_fn=(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)),
- )
- (stdout, _) = await proc.communicate()
+ version = (await aioproc.read_process([self.__cmd[0], "--version"], err_to_null=True))[1]
return {
"app": os.path.basename(self.__cmd[0]),
- "version": stdout.decode(errors="ignore").strip(),
+ "version": version,
}
@aiotools.atomic
@@ -329,12 +324,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes
)
for part in self.__cmd
]
- self.__streamer_proc = await asyncio.create_subprocess_exec(
- *cmd,
- stdout=asyncio.subprocess.PIPE,
- stderr=asyncio.subprocess.STDOUT,
- preexec_fn=(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)),
- )
+ self.__streamer_proc = await aioproc.run_process(cmd)
get_logger(0).info("Started streamer pid=%d: %s", self.__streamer_proc.pid, cmd)
async def __kill_streamer_proc(self) -> None:
diff --git a/kvmd/plugins/msd/otg/helpers.py b/kvmd/plugins/msd/otg/helpers.py
index ee759ef0..df004c6d 100644
--- a/kvmd/plugins/msd/otg/helpers.py
+++ b/kvmd/plugins/msd/otg/helpers.py
@@ -20,14 +20,12 @@
# ========================================================================== #
-import signal
-import asyncio
-import asyncio.subprocess
-
from typing import List
from ....logging import get_logger
+from .... import aioproc
+
from .. import MsdError
@@ -62,14 +60,8 @@ async def _run_helper(cmd: List[str]) -> None:
logger = get_logger(0)
logger.info("Executing helper %s ...", cmd)
- proc = await asyncio.create_subprocess_exec(
- *cmd,
- stdout=asyncio.subprocess.PIPE,
- stderr=asyncio.subprocess.STDOUT,
- preexec_fn=(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)),
- )
+ (proc, stdout) = await aioproc.read_process(cmd)
- stdout = (await proc.communicate())[0].decode(errors="ignore").strip()
if stdout:
log = (logger.info if proc.returncode == 0 else logger.error)
for line in stdout.split("\n"):
diff --git a/testenv/tests/plugins/auth/test_pam.py b/testenv/tests/plugins/auth/test_pam.py
index 5e69f305..e3e7fc99 100644
--- a/testenv/tests/plugins/auth/test_pam.py
+++ b/testenv/tests/plugins/auth/test_pam.py
@@ -21,7 +21,6 @@
import asyncio
-import signal
import pwd
from typing import Dict
@@ -30,6 +29,8 @@ from typing import Optional
import pytest
+from kvmd import aioproc
+
from . import get_configured_auth_service
@@ -44,7 +45,7 @@ async def _run_process(cmd: str, input: Optional[str]=None) -> None: # pylint:
proc = await asyncio.create_subprocess_exec(
*cmd.split(" "),
stdin=(asyncio.subprocess.PIPE if input is not None else None),
- preexec_fn=(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)),
+ preexec_fn=aioproc.preexec_ignore_sigint,
)
await proc.communicate(input.encode() if input is not None else None)
assert proc.returncode == 0