[PATCH 1/5] lib: Introduce 'virDomainQemuMonitorCommandWithFiles'

Peter Krempa pkrempa at redhat.com
Thu Feb 3 14:51:55 UTC 2022


This API has the same semantics as 'virDomainQemuMonitorCommand' but
accepts file descriptors which are then forwarded to qemu.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 include/libvirt/libvirt-qemu.h      |  6 +++
 src/driver-hypervisor.h             |  8 ++++
 src/libvirt-qemu.c                  | 71 +++++++++++++++++++++++++++++
 src/libvirt_qemu.syms               |  5 ++
 src/qemu_protocol-structs           |  9 ++++
 src/remote/qemu_protocol.x          | 20 +++++++-
 src/remote/remote_daemon_dispatch.c | 42 +++++++++++++++++
 src/remote/remote_driver.c          | 40 ++++++++++++++++
 8 files changed, 200 insertions(+), 1 deletion(-)

diff --git a/include/libvirt/libvirt-qemu.h b/include/libvirt/libvirt-qemu.h
index 0cc2872821..eed691ec91 100644
--- a/include/libvirt/libvirt-qemu.h
+++ b/include/libvirt/libvirt-qemu.h
@@ -37,6 +37,12 @@ typedef enum {

 int virDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd,
                                 char **result, unsigned int flags);
+int virDomainQemuMonitorCommandWithFiles(virDomainPtr domain,
+                                         const char *cmd,
+                                         unsigned int nfiles,
+                                         int *files,
+                                         char **result,
+                                         unsigned int flags);

 virDomainPtr virDomainQemuAttach(virConnectPtr domain,
                                  unsigned int pid_value,
diff --git a/src/driver-hypervisor.h b/src/driver-hypervisor.h
index c83fb648a2..b3e55cf4ac 100644
--- a/src/driver-hypervisor.h
+++ b/src/driver-hypervisor.h
@@ -874,6 +874,13 @@ typedef int
                                   const char *cmd,
                                   char **result,
                                   unsigned int flags);
+typedef int
+(*virDrvDomainQemuMonitorCommandWithFiles)(virDomainPtr domain,
+                                           const char *cmd,
+                                           unsigned int nfiles,
+                                           int *files,
+                                           char **result,
+                                           unsigned int flags);

 typedef char *
 (*virDrvDomainQemuAgentCommand)(virDomainPtr domain,
@@ -1597,6 +1604,7 @@ struct _virHypervisorDriver {
     virDrvDomainRevertToSnapshot domainRevertToSnapshot;
     virDrvDomainSnapshotDelete domainSnapshotDelete;
     virDrvDomainQemuMonitorCommand domainQemuMonitorCommand;
+    virDrvDomainQemuMonitorCommandWithFiles domainQemuMonitorCommandWithFiles;
     virDrvDomainQemuAttach domainQemuAttach;
     virDrvDomainQemuAgentCommand domainQemuAgentCommand;
     virDrvConnectDomainQemuMonitorEventRegister connectDomainQemuMonitorEventRegister;
diff --git a/src/libvirt-qemu.c b/src/libvirt-qemu.c
index 1afb5fe529..1dbe0cba54 100644
--- a/src/libvirt-qemu.c
+++ b/src/libvirt-qemu.c
@@ -96,6 +96,77 @@ virDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd,
 }


+/**
+ * virDomainQemuMonitorCommandWithFiles:
+ * @domain: a domain object
+ * @cmd: the qemu monitor command string
+ * @nfiles: number of filedescriptors passed in @files
+ * @files: filedescriptors to be passed to qemu with the command
+ * @result: a string returned by @cmd
+ * @flags: bitwise-or of supported virDomainQemuMonitorCommandFlags
+ *
+ * This API is QEMU specific, so it will only work with hypervisor
+ * connections to the QEMU driver with local connections using the unix socket.
+ *
+ * Send an arbitrary monitor command @cmd with file descriptors @files to
+ * @domain through the qemu monitor. There are several requirements to safely
+ * and successfully use this API:
+ *
+ *   - A @cmd that queries state without making any modifications is safe
+ *   - A @cmd that alters state that is also tracked by libvirt is unsafe,
+ *     and may cause libvirtd to crash
+ *   - A @cmd that alters state not tracked by the current version of
+ *     libvirt is possible as a means to test new qemu features before
+ *     they have support in libvirt, but no guarantees are made to safety
+ *
+ * If VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP is set, the command is
+ * considered to be a human monitor command and libvirt will automatically
+ * convert it into QMP if needed.  In that case the @result will also
+ * be converted back from QMP.
+ *
+ * If successful, @result will be filled with the string output of the
+ * @cmd, and the caller must free this string.
+ *
+ * Returns 0 in case of success, -1 in case of failure
+ */
+int
+virDomainQemuMonitorCommandWithFiles(virDomainPtr domain,
+                                     const char *cmd,
+                                     unsigned int nfiles,
+                                     int *files,
+                                     char **result,
+                                     unsigned int flags)
+{
+    virConnectPtr conn;
+
+    VIR_DOMAIN_DEBUG(domain, "cmd=%s, nfiles=%u, files=%p, result=%p, flags=0x%x",
+                     cmd, nfiles, files, result, flags);
+
+    virResetLastError();
+
+    virCheckDomainReturn(domain, -1);
+    conn = domain->conn;
+
+    virCheckNonNullArgGoto(result, error);
+    virCheckReadOnlyGoto(conn->flags, error);
+
+    if (conn->driver->domainQemuMonitorCommandWithFiles) {
+        int ret;
+        ret = conn->driver->domainQemuMonitorCommandWithFiles(domain, cmd,
+                                                              nfiles, files,
+                                                              result, flags);
+        if (ret < 0)
+            goto error;
+        return ret;
+    }
+
+    virReportUnsupportedError();
+
+ error:
+    virDispatchError(conn);
+    return -1;
+}
+
 /**
  * virDomainQemuAttach:
  * @conn: pointer to a hypervisor connection
diff --git a/src/libvirt_qemu.syms b/src/libvirt_qemu.syms
index 3a297e3a2b..019e545101 100644
--- a/src/libvirt_qemu.syms
+++ b/src/libvirt_qemu.syms
@@ -30,3 +30,8 @@ LIBVIRT_QEMU_1.2.3 {
         virConnectDomainQemuMonitorEventDeregister;
         virConnectDomainQemuMonitorEventRegister;
 } LIBVIRT_QEMU_0.10.0;
+
+LIBVIRT_QEMU_8.1.0 {
+    global:
+        virDomainQemuMonitorCommandWithFiles;
+} LIBVIRT_QEMU_1.2.3;
diff --git a/src/qemu_protocol-structs b/src/qemu_protocol-structs
index 8501543cd9..ea0854385f 100644
--- a/src/qemu_protocol-structs
+++ b/src/qemu_protocol-structs
@@ -47,6 +47,14 @@ struct qemu_domain_monitor_event_msg {
         u_int                      micros;
         remote_string              details;
 };
+struct qemu_domain_monitor_command_with_files_args {
+        remote_nonnull_domain      dom;
+        remote_nonnull_string      cmd;
+        u_int                      flags;
+};
+struct qemu_domain_monitor_command_with_files_ret {
+        remote_nonnull_string      result;
+};
 enum qemu_procedure {
         QEMU_PROC_DOMAIN_MONITOR_COMMAND = 1,
         QEMU_PROC_DOMAIN_ATTACH = 2,
@@ -54,4 +62,5 @@ enum qemu_procedure {
         QEMU_PROC_CONNECT_DOMAIN_MONITOR_EVENT_REGISTER = 4,
         QEMU_PROC_CONNECT_DOMAIN_MONITOR_EVENT_DEREGISTER = 5,
         QEMU_PROC_DOMAIN_MONITOR_EVENT = 6,
+        QEMU_PROC_DOMAIN_MONITOR_COMMAND_WITH_FILES       = 7,
 };
diff --git a/src/remote/qemu_protocol.x b/src/remote/qemu_protocol.x
index 8ff5dc8568..c7f3abfcbf 100644
--- a/src/remote/qemu_protocol.x
+++ b/src/remote/qemu_protocol.x
@@ -79,6 +79,17 @@ struct qemu_domain_monitor_event_msg {
     remote_string details;
 };

+struct qemu_domain_monitor_command_with_files_args {
+    remote_nonnull_domain dom;
+    remote_nonnull_string cmd;
+    unsigned int flags;
+};
+
+struct qemu_domain_monitor_command_with_files_ret {
+    remote_nonnull_string result;
+};
+
+
 /* Define the program number, protocol version and procedure numbers here. */
 const QEMU_PROGRAM = 0x20008087;
 const QEMU_PROTOCOL_VERSION = 1;
@@ -151,5 +162,12 @@ enum qemu_procedure {
      * @generate: both
      * @acl: none
      */
-    QEMU_PROC_DOMAIN_MONITOR_EVENT = 6
+    QEMU_PROC_DOMAIN_MONITOR_EVENT = 6,
+
+    /**
+     * @generate: none
+     * @priority: low
+     * @acl: domain:write
+     */
+    QEMU_PROC_DOMAIN_MONITOR_COMMAND_WITH_FILES = 7
 };
diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c
index 689001889e..5f967abed0 100644
--- a/src/remote/remote_daemon_dispatch.c
+++ b/src/remote/remote_daemon_dispatch.c
@@ -4689,6 +4689,48 @@ qemuDispatchDomainMonitorCommand(virNetServer *server G_GNUC_UNUSED,
 }


+static int
+qemuDispatchDomainMonitorCommandWithFiles(virNetServer *server G_GNUC_UNUSED,
+                                          virNetServerClient *client,
+                                          virNetMessage *msg,
+                                          struct virNetMessageError *rerr,
+                                          qemu_domain_monitor_command_with_files_args *args,
+                                          qemu_domain_monitor_command_with_files_ret *ret)
+{
+    virDomainPtr dom = NULL;
+    int *files = NULL;
+    unsigned int nfiles = 0;
+    int rv = -1;
+    virConnectPtr conn = remoteGetHypervisorConn(client);
+    size_t i;
+
+    if (!conn)
+        goto cleanup;
+
+    if (!(dom = get_nonnull_domain(conn, args->dom)))
+        goto cleanup;
+
+    files = g_new0(int, msg->nfds);
+    for (i = 0; i < msg->nfds; i++) {
+        if ((files[i] = virNetMessageDupFD(msg, i)) < 0)
+            goto cleanup;
+        nfiles++;
+    }
+
+    if (virDomainQemuMonitorCommandWithFiles(dom, args->cmd, nfiles, files,
+                                             &ret->result, args->flags) < 0)
+        goto cleanup;
+
+    rv = 0;
+
+ cleanup:
+    if (rv < 0)
+        virNetMessageSaveError(rerr);
+    virObjectUnref(dom);
+    return rv;
+}
+
+
 static int
 remoteDispatchDomainMigrateBegin3(virNetServer *server G_GNUC_UNUSED,
                                   virNetServerClient *client,
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 5b7ccfaebd..ee3f4e4e63 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -5938,6 +5938,45 @@ remoteDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd,
 }


+static int
+remoteDomainQemuMonitorCommandWithFiles(virDomainPtr domain,
+                                        const char *cmd,
+                                        unsigned int nfiles,
+                                        int *files,
+                                        char **result,
+                                        unsigned int flags)
+{
+    int rv = -1;
+    qemu_domain_monitor_command_with_files_args args;
+    qemu_domain_monitor_command_with_files_ret ret;
+    struct private_data *priv = domain->conn->privateData;
+
+    remoteDriverLock(priv);
+
+    make_nonnull_domain(&args.dom, domain);
+    args.cmd = (char *)cmd;
+    args.flags = flags;
+
+    memset(&ret, 0, sizeof(ret));
+    if (callFull(domain->conn, priv, REMOTE_CALL_QEMU,
+                 files, nfiles, NULL, NULL,
+                 QEMU_PROC_DOMAIN_MONITOR_COMMAND_WITH_FILES,
+                 (xdrproc_t) xdr_qemu_domain_monitor_command_with_files_args, (char *) &args,
+                 (xdrproc_t) xdr_qemu_domain_monitor_command_with_files_ret, (char *) &ret) == -1)
+        goto done;
+
+    *result = g_strdup(ret.result);
+
+    rv = 0;
+
+    xdr_free((xdrproc_t) xdr_qemu_domain_monitor_command_ret, (char *) &ret);
+
+ done:
+    remoteDriverUnlock(priv);
+    return rv;
+}
+
+
 static char *
 remoteDomainMigrateBegin3(virDomainPtr domain,
                           const char *xmlin,
@@ -8513,6 +8552,7 @@ static virHypervisorDriver hypervisor_driver = {
     .domainSnapshotHasMetadata = remoteDomainSnapshotHasMetadata, /* 0.9.13 */
     .domainSnapshotDelete = remoteDomainSnapshotDelete, /* 0.8.0 */
     .domainQemuMonitorCommand = remoteDomainQemuMonitorCommand, /* 0.8.3 */
+    .domainQemuMonitorCommandWithFiles = remoteDomainQemuMonitorCommandWithFiles, /* 8.1.0 */
     .domainQemuAttach = remoteDomainQemuAttach, /* 0.9.4 */
     .domainQemuAgentCommand = remoteDomainQemuAgentCommand, /* 0.10.0 */
     .connectDomainQemuMonitorEventRegister = remoteConnectDomainQemuMonitorEventRegister, /* 1.2.3 */
-- 
2.34.1




More information about the libvir-list mailing list