]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Check in spawn() whether the command we're trying to run is available 2593/head
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 4 Apr 2024 18:29:15 +0000 (20:29 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 5 Apr 2024 06:45:31 +0000 (08:45 +0200)
Currently, if we try to run a command within a sandbox, we fail with
an unclear error if the program is not installed. This is because our
FileNotFoundError exception handler is never triggered as the program
we run via subprocess is almost always "sh" or "bwrap". Let's make sure
we also check for the actual program we're going to run in the sandbox
and show a clear error if it's not available.

mkosi/run.py

index 5b579586e55f691e60fc82374df103d326d6afe4..e7a66dd19d091888044479ab737234e759ec188a 100644 (file)
@@ -244,6 +244,14 @@ def spawn(
             # expect it to pick.
             assert nfd == SD_LISTEN_FDS_START + i
 
+    # First, check if the sandbox works at all before executing the command.
+    if sandbox and (rc := subprocess.run(sandbox + ["true"]).returncode) != 0:
+        log_process_failure(sandbox, cmdline, rc)
+        raise subprocess.CalledProcessError(rc, sandbox + cmdline)
+
+    if subprocess.run(sandbox + ["sh", "-c", f"command -v {cmdline[0]}"], stdout=subprocess.DEVNULL).returncode != 0:
+        die(f"{cmdline[0]} not found.", hint=f"Is {cmdline[0]} installed on the host system?")
+
     if (
         foreground and
         sandbox and