acda74
From 54806234834ab5f2a1ada02afc5ad5ef6a789dc9 Mon Sep 17 00:00:00 2001
acda74
Message-Id: <54806234834ab5f2a1ada02afc5ad5ef6a789dc9@dist-git>
acda74
From: Laine Stump <laine@redhat.com>
acda74
Date: Wed, 1 Mar 2023 11:34:24 -0500
acda74
Subject: [PATCH] util: add an API to retrieve the resolved path to a
acda74
 virCommand's binary
acda74
acda74
The binary to be exec'ed by virExec() is stored in
acda74
virCommand::args[0], and is resolved to a full absolute path (stored
acda74
in a local of virExec() just prior to execve().
acda74
acda74
Since we will have another use for the full absolute path, lets make
acda74
an API to resolve/retrieve the absolute path, and cache it in
acda74
virCommand::binaryPath so we only have to do the resolution once.
acda74
acda74
Signed-off-by: Laine Stump <laine@redhat.com>
acda74
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
acda74
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
acda74
(cherry picked from commit a53c1d6f842ba0f516bbacff8250ba0d7a10074a)
acda74
acda74
https://bugzilla.redhat.com/2172267
acda74
Signed-off-by: Laine Stump <laine@redhat.com>
acda74
---
acda74
 src/libvirt_private.syms |  1 +
acda74
 src/util/vircommand.c    | 51 +++++++++++++++++++++++++++++++---------
acda74
 src/util/vircommand.h    |  1 +
acda74
 3 files changed, 42 insertions(+), 11 deletions(-)
acda74
acda74
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
acda74
index 576ec8f95f..e20421e7cd 100644
acda74
--- a/src/libvirt_private.syms
acda74
+++ b/src/libvirt_private.syms
acda74
@@ -2076,6 +2076,7 @@ virCommandDryRunTokenNew;
acda74
 virCommandExec;
acda74
 virCommandFree;
acda74
 virCommandGetArgList;
acda74
+virCommandGetBinaryPath;
acda74
 virCommandGetGID;
acda74
 virCommandGetUID;
acda74
 virCommandHandshakeNotify;
acda74
diff --git a/src/util/vircommand.c b/src/util/vircommand.c
acda74
index 0917bc9cfb..a31509e977 100644
acda74
--- a/src/util/vircommand.c
acda74
+++ b/src/util/vircommand.c
acda74
@@ -88,6 +88,7 @@ struct _virCommandSendBuffer {
acda74
 struct _virCommand {
acda74
     int has_error; /* 0 on success, -1 on error  */
acda74
 
acda74
+    char *binaryPath; /* only valid if args[0] isn't absolute path */
acda74
     char **args;
acda74
     size_t nargs;
acda74
     size_t maxargs;
acda74
@@ -630,6 +631,7 @@ virCommandMassClose(virCommand *cmd,
acda74
 
acda74
 # endif /* ! __FreeBSD__ */
acda74
 
acda74
+
acda74
 /*
acda74
  * virExec:
acda74
  * @cmd virCommand * containing all information about the program to
acda74
@@ -646,22 +648,13 @@ virExec(virCommand *cmd)
acda74
     int childin = cmd->infd;
acda74
     int childout = -1;
acda74
     int childerr = -1;
acda74
-    g_autofree char *binarystr = NULL;
acda74
     const char *binary = NULL;
acda74
     int ret;
acda74
     g_autofree gid_t *groups = NULL;
acda74
     int ngroups;
acda74
 
acda74
-    if (!g_path_is_absolute(cmd->args[0])) {
acda74
-        if (!(binary = binarystr = virFindFileInPath(cmd->args[0]))) {
acda74
-            virReportSystemError(ENOENT,
acda74
-                                 _("Cannot find '%s' in path"),
acda74
-                                 cmd->args[0]);
acda74
-            return -1;
acda74
-        }
acda74
-    } else {
acda74
-        binary = cmd->args[0];
acda74
-    }
acda74
+    if (!(binary = virCommandGetBinaryPath(cmd)))
acda74
+        return -1;
acda74
 
acda74
     if (childin < 0) {
acda74
         if (getDevNull(&null) < 0)
acda74
@@ -2164,6 +2157,40 @@ virCommandGetArgList(virCommand *cmd,
acda74
 }
acda74
 
acda74
 
acda74
+/*
acda74
+ * virCommandGetBinaryPath:
acda74
+ * @cmd: virCommand* containing all information about the program
acda74
+ *
acda74
+ * If args[0] is an absolute path, return that. If not, then resolve
acda74
+ * args[0] to a full absolute path, cache that in binaryPath, and
acda74
+ * return a pointer to this resolved string. binaryPath is only set by
acda74
+ * calling this function, so even other virCommand functions should
acda74
+ * access binaryPath via this function.
acda74
+ *
acda74
+ * returns const char* with the full path of the binary to be
acda74
+ * executed, or NULL on failure.
acda74
+ */
acda74
+const char *
acda74
+virCommandGetBinaryPath(virCommand *cmd)
acda74
+{
acda74
+
acda74
+    if (cmd->binaryPath)
acda74
+        return cmd->binaryPath;
acda74
+
acda74
+    if (g_path_is_absolute(cmd->args[0]))
acda74
+        return cmd->args[0];
acda74
+
acda74
+    if (!(cmd->binaryPath = virFindFileInPath(cmd->args[0]))) {
acda74
+        virReportSystemError(ENOENT,
acda74
+                             _("Cannot find '%s' in path"),
acda74
+                             cmd->args[0]);
acda74
+        return NULL;
acda74
+    }
acda74
+
acda74
+    return cmd->binaryPath;
acda74
+}
acda74
+
acda74
+
acda74
 #ifndef WIN32
acda74
 /*
acda74
  * Manage input and output to the child process.
acda74
@@ -3015,6 +3042,8 @@ virCommandFree(virCommand *cmd)
acda74
     VIR_FORCE_CLOSE(cmd->outfd);
acda74
     VIR_FORCE_CLOSE(cmd->errfd);
acda74
 
acda74
+    g_free(cmd->binaryPath);
acda74
+
acda74
     for (i = 0; i < cmd->nargs; i++)
acda74
         g_free(cmd->args[i]);
acda74
     g_free(cmd->args);
acda74
diff --git a/src/util/vircommand.h b/src/util/vircommand.h
acda74
index e0002103b6..d51449ac90 100644
acda74
--- a/src/util/vircommand.h
acda74
+++ b/src/util/vircommand.h
acda74
@@ -170,6 +170,7 @@ int virCommandToStringBuf(virCommand *cmd,
acda74
                           bool linebreaks,
acda74
                           bool stripCommandPath);
acda74
 
acda74
+const char *virCommandGetBinaryPath(virCommand *cmd);
acda74
 int virCommandGetArgList(virCommand *cmd, char ***args);
acda74
 
acda74
 int virCommandExec(virCommand *cmd, gid_t *groups, int ngroups) G_GNUC_WARN_UNUSED_RESULT;
acda74
-- 
acda74
2.40.0
acda74