[PATCH v2 01/23] src: add constants for guest info 'user.' parameters

Daniel P. Berrangé posted 23 patches 22 hours ago
[PATCH v2 01/23] src: add constants for guest info 'user.' parameters
Posted by Daniel P. Berrangé 22 hours ago
Contrary to most APIs returning typed parameters, there are no constants
defined for the guest info data keys. This is was because many of the
keys needs to be dynamically constructed using one or more array index
values.

It is possible to define constants while still supporting dynamic
array indexes by simply defining the prefixes and suffixes as constants.
The consuming code can then combine the constants with array index
value.

With this approach, it is practical to add constants for the guest info
API keys.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 include/libvirt/libvirt-domain.h | 49 ++++++++++++++++++++++++++++++++
 src/libvirt-domain.c             | 14 +++------
 src/qemu/qemu_agent.c            | 11 ++++---
 3 files changed, 60 insertions(+), 14 deletions(-)

diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index 0121620e9c..4f6e369087 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -6455,6 +6455,55 @@ int virDomainSetLaunchSecurityState(virDomainPtr domain,
                                     int nparams,
                                     unsigned int flags);
 
+/**
+ * VIR_DOMAIN_GUEST_INFO_USER_COUNT:
+ *
+ * The number of active users on this domain as an unsigned int.
+ *
+ * Since: 11.2.0
+ */
+#define VIR_DOMAIN_GUEST_INFO_USER_COUNT "user.count"
+
+/**
+ * VIR_DOMAIN_GUEST_INFO_USER_PREFIX:
+ *
+ * The parameter name prefix to access each user entry. Concatenate the
+ * prefix, the entry number formatted as an unsigned integer and one of the
+ * user suffix parameters to form a complete parameter name.
+ *
+ * Since: 11.2.0
+ */
+#define VIR_DOMAIN_GUEST_INFO_USER_PREFIX "user."
+
+/**
+ * VIR_DOMAIN_GUEST_INFO_USER_SUFFIX_NAME:
+ *
+ * Username of the user as a string.
+ *
+ * Since: 11.2.0
+ */
+#define VIR_DOMAIN_GUEST_INFO_USER_SUFFIX_NAME ".name"
+
+/**
+ * VIR_DOMAIN_GUEST_INFO_USER_SUFFIX_DOMAIN:
+ *
+ * Domain of the user as a string (may only be present on certain guest
+ * types).
+ *
+ * Since: 11.2.0
+ */
+#define VIR_DOMAIN_GUEST_INFO_USER_SUFFIX_DOMAIN ".domain"
+
+/**
+ * VIR_DOMAIN_GUEST_INFO_USER_SUFFIX_LOGIN_TIME:
+ *
+ * The login time of a user in milliseconds since the epoch as unsigned long
+ * long.
+ *
+ * Since: 11.2.0
+ */
+#define VIR_DOMAIN_GUEST_INFO_USER_SUFFIX_LOGIN_TIME ".login-time"
+
 /**
  * virDomainGuestInfoTypes:
  *
diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c
index 18451ebdb9..f073c27788 100644
--- a/src/libvirt-domain.c
+++ b/src/libvirt-domain.c
@@ -13202,16 +13202,10 @@ virDomainSetVcpu(virDomainPtr domain,
  * (although not necessarily implemented for each hypervisor):
  *
  * VIR_DOMAIN_GUEST_INFO_USERS:
- *  returns information about users that are currently logged in within the
- *  guest domain. The typed parameter keys are in this format:
- *
- *      "user.count" - the number of active users on this domain as an
- *                     unsigned int
- *      "user.<num>.name" - username of the user as a string
- *      "user.<num>.domain" - domain of the user as a string (may only be
- *                            present on certain guest types)
- *      "user.<num>.login-time" - the login time of a user in milliseconds
- *                                since the epoch as unsigned long long
+ *  Return information about users that are currently logged in within the
+ *  guest domain.
+ *  The VIR_DOMAIN_GUEST_INFO_USER_* constants define the known typed parameter
+ *  keys.
  *
  * VIR_DOMAIN_GUEST_INFO_OS:
  *  Return information about the operating system running within the guest. The
diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c
index fb404a7203..fe2d65ccf3 100644
--- a/src/qemu/qemu_agent.c
+++ b/src/qemu/qemu_agent.c
@@ -2197,7 +2197,7 @@ qemuAgentGetUsers(qemuAgent *agent,
 
     ndata = virJSONValueArraySize(data);
 
-    virTypedParamListAddUInt(list, ndata, "user.count");
+    virTypedParamListAddUInt(list, ndata, VIR_DOMAIN_GUEST_INFO_USER_COUNT);
 
     for (i = 0; i < ndata; i++) {
         virJSONValue *entry = virJSONValueArrayGet(data, i);
@@ -2216,18 +2216,21 @@ qemuAgentGetUsers(qemuAgent *agent,
             return -1;
         }
 
-        virTypedParamListAddString(list, strvalue, "user.%zu.name", i);
+        virTypedParamListAddString(list, strvalue,
+                                   VIR_DOMAIN_GUEST_INFO_USER_PREFIX "%zu" VIR_DOMAIN_GUEST_INFO_USER_SUFFIX_NAME, i);
 
         /* 'domain' is only present for windows guests */
         if ((strvalue = virJSONValueObjectGetString(entry, "domain")))
-            virTypedParamListAddString(list, strvalue, "user.%zu.domain", i);
+            virTypedParamListAddString(list, strvalue,
+                                       VIR_DOMAIN_GUEST_INFO_USER_PREFIX "%zu" VIR_DOMAIN_GUEST_INFO_USER_SUFFIX_DOMAIN, i);
 
         if (virJSONValueObjectGetNumberDouble(entry, "login-time", &logintime) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("'login-time' missing in reply of guest-get-users"));
             return -1;
         }
-        virTypedParamListAddULLong(list, logintime * 1000, "user.%zu.login-time", i);
+        virTypedParamListAddULLong(list, logintime * 1000,
+                                   VIR_DOMAIN_GUEST_INFO_USER_PREFIX "%zu" VIR_DOMAIN_GUEST_INFO_USER_SUFFIX_LOGIN_TIME, i);
     }
 
     return 0;
-- 
2.48.1
Re: [PATCH v2 01/23] src: add constants for guest info 'user.' parameters
Posted by Peter Krempa 20 hours ago
On Tue, Mar 11, 2025 at 14:24:07 +0000, Daniel P. Berrangé wrote:
> Contrary to most APIs returning typed parameters, there are no constants
> defined for the guest info data keys. This is was because many of the
> keys needs to be dynamically constructed using one or more array index
> values.
> 
> It is possible to define constants while still supporting dynamic
> array indexes by simply defining the prefixes and suffixes as constants.
> The consuming code can then combine the constants with array index
> value.
> 
> With this approach, it is practical to add constants for the guest info
> API keys.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  include/libvirt/libvirt-domain.h | 49 ++++++++++++++++++++++++++++++++
>  src/libvirt-domain.c             | 14 +++------
>  src/qemu/qemu_agent.c            | 11 ++++---
>  3 files changed, 60 insertions(+), 14 deletions(-)

Reviewed-by: Peter Krempa <pkrempa@redhat.com>