]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Set stdin=/dev/null for all commands invoked by run()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 20 Apr 2023 08:12:01 +0000 (10:12 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 20 Apr 2023 11:56:30 +0000 (13:56 +0200)
In ed93e8c49fc2afb9255280d1dec9b7d81d07ff94 I added this for two
commands that were invoked directly from summary. But the same issue
occurs for other commands that we invoke. Instead of handling them
one-by-one, let's set stdin for all commands. We don't want anything that
we invoke to ever read input from the console, so this is a suitable
default for us.

mkosi/__init__.py
mkosi/run.py

index 458b5b775b4048484bb075620b16a887df944300..7c040bd969bc97d85b0582b6fec45de6fd678d01 100644 (file)
@@ -1083,7 +1083,6 @@ def load_credentials(args: argparse.Namespace) -> dict[str, str]:
         tz = run(
             ["timedatectl", "show", "-p", "Timezone", "--value"],
             text=True,
-            stdin=subprocess.DEVNULL,
             stdout=subprocess.PIPE,
         ).stdout.strip()
         creds["firstboot.timezone"] = tz
@@ -1098,7 +1097,6 @@ def load_credentials(args: argparse.Namespace) -> dict[str, str]:
         key = run(
             ["ssh-add", "-L"],
             text=True,
-            stdin=subprocess.DEVNULL,
             stdout=subprocess.PIPE,
             env=os.environ,
         ).stdout.strip()
index c7b069e86430cf62cd7e5cc5f22aafee0e00308e..743318f0c46b3b8cd93e35a8ef7194592273e224 100644 (file)
@@ -190,6 +190,7 @@ def fork_and_wait(target: Callable[[], T]) -> T:
 def run(
     cmdline: Sequence[PathString],
     check: bool = True,
+    stdin: _FILE = None,
     stdout: _FILE = None,
     stderr: _FILE = None,
     env: Mapping[str, PathString] = {},
@@ -219,8 +220,19 @@ def run(
     if "run" in ARG_DEBUG:
         env["SYSTEMD_LOG_LEVEL"] = "debug"
 
+    if "input" in kwargs:
+        assert stdin is None  # stdin and input can be specified together
+    elif stdin is None:
+        stdin = subprocess.DEVNULL
+
     try:
-        return subprocess.run(cmdline, check=check, stdout=stdout, stderr=stderr, env=env, **kwargs,
+        return subprocess.run(cmdline,
+                              check=check,
+                              stdin=stdin,
+                              stdout=stdout,
+                              stderr=stderr,
+                              env=env,
+                              **kwargs,
                               preexec_fn=foreground)
     except FileNotFoundError as e:
         die(f"{cmdline[0]} not found in PATH.", e)