1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-20 06:50:22 +03:00

qemuAgentGetUsers: Convert to virTypedParamList

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2025-02-26 14:42:03 +01:00
parent 0f5bb2c20d
commit dd59d0f77d
4 changed files with 33 additions and 57 deletions

View File

@ -2172,9 +2172,7 @@ qemuAgentSetUserPassword(qemuAgent *agent,
*/
int
qemuAgentGetUsers(qemuAgent *agent,
virTypedParameterPtr *params,
int *nparams,
int *maxparams,
virTypedParamList *list,
bool report_unsupported)
{
g_autoptr(virJSONValue) cmd = NULL;
@ -2199,13 +2197,10 @@ qemuAgentGetUsers(qemuAgent *agent,
ndata = virJSONValueArraySize(data);
if (virTypedParamsAddUInt(params, nparams, maxparams,
"user.count", ndata) < 0)
return -1;
virTypedParamListAddUInt(list, ndata, "user.count");
for (i = 0; i < ndata; i++) {
virJSONValue *entry = virJSONValueArrayGet(data, i);
char param_name[VIR_TYPED_PARAM_FIELD_LENGTH];
const char *strvalue;
double logintime;
@ -2221,30 +2216,18 @@ qemuAgentGetUsers(qemuAgent *agent,
return -1;
}
g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH, "user.%zu.name", i);
if (virTypedParamsAddString(params, nparams, maxparams,
param_name, strvalue) < 0)
return -1;
virTypedParamListAddString(list, strvalue, "user.%zu.name", i);
/* 'domain' is only present for windows guests */
if ((strvalue = virJSONValueObjectGetString(entry, "domain"))) {
g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
"user.%zu.domain", i);
if (virTypedParamsAddString(params, nparams, maxparams,
param_name, strvalue) < 0)
return -1;
}
if ((strvalue = virJSONValueObjectGetString(entry, "domain")))
virTypedParamListAddString(list, strvalue, "user.%zu.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;
}
g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
"user.%zu.login-time", i);
if (virTypedParamsAddULLong(params, nparams, maxparams,
param_name, logintime * 1000) < 0)
return -1;
virTypedParamListAddULLong(list, logintime * 1000, "user.%zu.login-time", i);
}
return 0;

View File

@ -157,9 +157,7 @@ int qemuAgentSetUserPassword(qemuAgent *mon,
bool crypted);
int qemuAgentGetUsers(qemuAgent *mon,
virTypedParameterPtr *params,
int *nparams,
int *maxparams,
virTypedParamList *list,
bool report_unsupported);
int qemuAgentGetOSInfo(qemuAgent *mon,

View File

@ -19400,7 +19400,7 @@ qemuDomainGetGuestInfo(virDomainPtr dom,
* 'unsupported' errors and gather as much information as we can. In all
* other cases, abort on error. */
if (supportedTypes & VIR_DOMAIN_GUEST_INFO_USERS &&
qemuAgentGetUsers(agent, params, nparams, &maxparams, report_unsupported) == -1)
qemuAgentGetUsers(agent, list, report_unsupported) == -1)
goto exitagent;
if (supportedTypes & VIR_DOMAIN_GUEST_INFO_OS &&

View File

@ -1095,69 +1095,64 @@ testQemuAgentUsers(const void *data)
{
virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
virTypedParameterPtr params = NULL;
int nparams = 0;
int maxparams = 0;
int ret = -1;
g_autoptr(virTypedParamList) list = virTypedParamListNew();
virTypedParameterPtr params;
size_t nparams = 0;
unsigned int count;
if (!test)
return -1;
if (qemuMonitorTestAddAgentSyncResponse(test) < 0)
goto cleanup;
return -1;
if (qemuMonitorTestAddItem(test, "guest-get-users",
testQemuAgentUsersResponse) < 0)
goto cleanup;
return -1;
/* get users */
if (qemuAgentGetUsers(qemuMonitorTestGetAgent(test),
&params, &nparams, &maxparams, true) < 0)
goto cleanup;
if (qemuAgentGetUsers(qemuMonitorTestGetAgent(test), list, true) < 0)
return -1;
if (virTypedParamListFetch(list, &params, &nparams) < 0)
return -1;
if (virTypedParamsGetUInt(params, nparams, "user.count", &count) < 0)
goto cleanup;
return -1;
if (count != 2) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"Expected '2' users, got '%u'", count);
goto cleanup;
return -1;
}
if (checkUserInfo(params, nparams, 0, "test", NULL, 1561739203584) < 0 ||
checkUserInfo(params, nparams, 1, "test2", NULL, 1561739229190) < 0)
goto cleanup;
return -1;
g_clear_pointer(&list, virTypedParamListFree);
list = virTypedParamListNew();
if (qemuMonitorTestAddItem(test, "guest-get-users",
testQemuAgentUsersResponse2) < 0)
goto cleanup;
return -1;
virTypedParamsFree(params, nparams);
params = NULL;
nparams = 0;
maxparams = 0;
if (qemuAgentGetUsers(qemuMonitorTestGetAgent(test), list, true) < 0)
return -1;
/* get users with domain */
if (qemuAgentGetUsers(qemuMonitorTestGetAgent(test),
&params, &nparams, &maxparams, true) < 0)
goto cleanup;
if (virTypedParamListFetch(list, &params, &nparams) < 0)
return -1;
if (virTypedParamsGetUInt(params, nparams, "user.count", &count) < 0)
goto cleanup;
return -1;
if (count != 1) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"Expected '1' user, got '%u'", count);
goto cleanup;
return -1;
}
if (checkUserInfo(params, nparams, 0, "test", "DOMAIN", 1561739203584) < 0)
goto cleanup;
return -1;
ret = 0;
cleanup:
virTypedParamsFree(params, nparams);
return ret;
return 0;
}
static const char testQemuAgentOSInfoResponse[] =