diff mbox series

[XEN,v2,1/1] tools/libxl: search PATH for QEMU if `QEMU_XEN_PATH` is not absolute

Message ID 20250330160304.20899-1-hehongbo@mail.com (mailing list archive)
State New
Headers show
Series [XEN,v2,1/1] tools/libxl: search PATH for QEMU if `QEMU_XEN_PATH` is not absolute | expand

Commit Message

Hongbo March 30, 2025, 4:03 p.m. UTC
`QEMU_XEN_PATH` will be configured as `qemu-system-i386` with no clue where, if
`--with-system-qemu` is set without giving a path (as matched in the case `yes`
but not `*`). However, the existence of the executable is checked by `access()`,
that will not look for anywhere in $PATH but the current directory. And since it
is possible for `qemu-system-i386` (or any other configured values) to be
executed from PATH later, we'd better find that in PATH and return the full path
for the caller to check against.

Signed-off-by: Hongbo <hehongbo@mail.com>
---
v2:
 - Identify absolute/relative paths with their first char (being `/` or not).
 - Put the case inside a block `{}` to address `clang` warnings about the new
   variable.
 - Avoid unnecessary string duplications.
 - Parity of using `{}` block on both sides of `if` statements.
 - Use `libxl__abs_path()` to get absolute paths.

Updated the patch as requested.

Also, I just realized that there is a `libxl__abs_path()` (occurred just above
my patched hunk), and I should utilize that instead of doing the `%s/%s`
`snprintf` thing myself.

Let me know if further changes are needed.

Best regards,
Hongbo
---
CC: Anthony PERARD <anthony.perard@vates.tech>
CC: Juergen Gross <jgross@suse.com>
---
 tools/libs/light/libxl_dm.c | 38 +++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)

--
2.39.5 (Apple Git-154)
diff mbox series

Patch

diff --git a/tools/libs/light/libxl_dm.c b/tools/libs/light/libxl_dm.c
index b193a5dc37..8f0bbd5d64 100644
--- a/tools/libs/light/libxl_dm.c
+++ b/tools/libs/light/libxl_dm.c
@@ -331,9 +331,43 @@  const char *libxl__domain_device_model(libxl__gc *gc,
         case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL:
             dm = libxl__abs_path(gc, "qemu-dm", libxl__private_bindir_path());
             break;
-        case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN:
-            dm = qemu_xen_path(gc);
+        case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN: {
+            const char *configured_dm = qemu_xen_path(gc);
+            if (configured_dm[0] == '/')
+            {
+                dm = configured_dm;
+            }
+            else
+            {
+                const char *path_env = getenv("PATH");
+                if (!path_env)
+                {
+                    dm = configured_dm;
+                }
+                else
+                {
+                    char *path_dup = libxl__strdup(gc, path_env);
+                    char *saveptr;
+
+                    char *path = strtok_r(path_dup, ":", &saveptr);
+                    dm = NULL;
+                    while (path)
+                    {
+                        char *candidate = libxl__abs_path(gc, configured_dm, path);
+                        if (access(candidate, X_OK) == 0)
+                        {
+                            dm = candidate;
+                            break;
+                        }
+                        path = strtok_r(NULL, ":", &saveptr);
+                    }
+
+                    if (!dm)
+                        dm = configured_dm;
+                }
+            }
             break;
+        }
         default:
             LOG(ERROR, "invalid device model version %d",
                 info->device_model_version);