virsh: domain: refactor cmdSchedinfo()

Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Kristina Hanicova 2021-09-24 01:25:07 +02:00 committed by Michal Privoznik
parent 55178b5c9e
commit e31502544c

View File

@ -5150,14 +5150,13 @@ cmdSchedInfoUpdate(vshControl *ctl, const vshCmd *cmd,
static bool static bool
cmdSchedinfo(vshControl *ctl, const vshCmd *cmd) cmdSchedinfo(vshControl *ctl, const vshCmd *cmd)
{ {
char *schedulertype; g_autofree char *schedulertype = NULL;
g_autoptr(virshDomain) dom = NULL; g_autoptr(virshDomain) dom = NULL;
virTypedParameterPtr params = NULL; virTypedParameterPtr params = NULL;
virTypedParameterPtr updates = NULL; virTypedParameterPtr updates = NULL;
int nparams = 0; int nparams = 0;
int nupdates = 0; int nupdates = 0;
size_t i; size_t i;
int ret;
bool ret_val = false; bool ret_val = false;
unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT; unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
bool current = vshCommandOptBool(cmd, "current"); bool current = vshCommandOptBool(cmd, "current");
@ -5176,32 +5175,31 @@ cmdSchedinfo(vshControl *ctl, const vshCmd *cmd)
return false; return false;
/* Print SchedulerType */ /* Print SchedulerType */
schedulertype = virDomainGetSchedulerType(dom, &nparams); if (!(schedulertype = virDomainGetSchedulerType(dom, &nparams))) {
if (schedulertype != NULL) {
vshPrint(ctl, "%-15s: %s\n", _("Scheduler"), schedulertype);
VIR_FREE(schedulertype);
} else {
vshPrint(ctl, "%-15s: %s\n", _("Scheduler"), _("Unknown")); vshPrint(ctl, "%-15s: %s\n", _("Scheduler"), _("Unknown"));
goto cleanup; goto cleanup;
} }
if (nparams) { vshPrint(ctl, "%-15s: %s\n", _("Scheduler"), schedulertype);
params = g_new0(virTypedParameter, nparams);
if (!nparams)
goto cleanup;
params = g_new0(virTypedParameter, nparams);
memset(params, 0, sizeof(*params) * nparams); memset(params, 0, sizeof(*params) * nparams);
if (flags || current) { if (flags || current) {
/* We cannot query both live and config at once, so settle /* We cannot query both live and config at once, so settle
on current in that case. If we are setting, then the on current in that case. If we are setting, then the
two values should match when we re-query; otherwise, we two values should match when we re-query; otherwise, we
report the error later. */ report the error later. */
ret = virDomainGetSchedulerParametersFlags(dom, params, &nparams, if (virDomainGetSchedulerParametersFlags(dom, params, &nparams,
((live && config) ? 0 ((live && config) ? 0 : flags)) == -1)
: flags));
} else {
ret = virDomainGetSchedulerParameters(dom, params, &nparams);
}
if (ret == -1)
goto cleanup; goto cleanup;
} else {
if (virDomainGetSchedulerParameters(dom, params, &nparams) == -1)
goto cleanup;
}
/* See if any params are being set */ /* See if any params are being set */
if ((nupdates = cmdSchedInfoUpdate(ctl, cmd, params, nparams, if ((nupdates = cmdSchedInfoUpdate(ctl, cmd, params, nparams,
@ -5210,24 +5208,21 @@ cmdSchedinfo(vshControl *ctl, const vshCmd *cmd)
/* Update parameters & refresh data */ /* Update parameters & refresh data */
if (nupdates > 0) { if (nupdates > 0) {
if (flags || current) if (flags || current) {
ret = virDomainSetSchedulerParametersFlags(dom, updates, if (virDomainSetSchedulerParametersFlags(dom, updates,
nupdates, flags); nupdates, flags) == -1)
else
ret = virDomainSetSchedulerParameters(dom, updates, nupdates);
if (ret == -1)
goto cleanup; goto cleanup;
if (flags || current) if (virDomainGetSchedulerParametersFlags(dom, params, &nparams,
ret = virDomainGetSchedulerParametersFlags(dom, params, ((live && config) ? 0 : flags)) == -1)
&nparams,
((live && config) ? 0
: flags));
else
ret = virDomainGetSchedulerParameters(dom, params, &nparams);
if (ret == -1)
goto cleanup; goto cleanup;
} else {
if (virDomainSetSchedulerParameters(dom, updates, nupdates) == -1)
goto cleanup;
if (virDomainGetSchedulerParameters(dom, params, &nparams) == -1)
goto cleanup;
}
} else { } else {
/* When not doing --set, --live and --config do not mix. */ /* When not doing --set, --live and --config do not mix. */
if (live && config) { if (live && config) {
@ -5239,10 +5234,8 @@ cmdSchedinfo(vshControl *ctl, const vshCmd *cmd)
ret_val = true; ret_val = true;
for (i = 0; i < nparams; i++) { for (i = 0; i < nparams; i++) {
char *str = vshGetTypedParamValue(ctl, &params[i]); g_autofree char *str = vshGetTypedParamValue(ctl, &params[i]);
vshPrint(ctl, "%-15s: %s\n", params[i].field, str); vshPrint(ctl, "%-15s: %s\n", params[i].field, str);
VIR_FREE(str);
}
} }
cleanup: cleanup: