mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-05 04:25:19 +00:00
virsh: cmdEvent: Rewrite questionable event registration
The code registering the event handlers in 'cmdEvent' had too many blocks of code conditional on whether just one event is being listened to or all events. The code can be greatly simplified by uniting the code paths and having only one branch when filling the list of events we want to listen for. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
ea3e64fefd
commit
c6bb274693
@ -274,6 +274,7 @@ typedef struct virshDomainEventCallback virshDomainEventCallback;
|
|||||||
|
|
||||||
struct virshDomEventData {
|
struct virshDomEventData {
|
||||||
vshControl *ctl;
|
vshControl *ctl;
|
||||||
|
int event;
|
||||||
bool loop;
|
bool loop;
|
||||||
int *count;
|
int *count;
|
||||||
bool timestamp;
|
bool timestamp;
|
||||||
@ -885,10 +886,10 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd)
|
|||||||
g_autoptr(virshDomain) dom = NULL;
|
g_autoptr(virshDomain) dom = NULL;
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
int timeout = 0;
|
int timeout = 0;
|
||||||
virshDomEventData *data = NULL;
|
g_autofree virshDomEventData *data = NULL;
|
||||||
|
size_t ndata = 0;
|
||||||
size_t i;
|
size_t i;
|
||||||
const char *eventName = NULL;
|
const char *eventName = NULL;
|
||||||
int event = -1;
|
|
||||||
bool all = vshCommandOptBool(cmd, "all");
|
bool all = vshCommandOptBool(cmd, "all");
|
||||||
bool loop = vshCommandOptBool(cmd, "loop");
|
bool loop = vshCommandOptBool(cmd, "loop");
|
||||||
bool timestamp = vshCommandOptBool(cmd, "timestamp");
|
bool timestamp = vshCommandOptBool(cmd, "timestamp");
|
||||||
@ -900,59 +901,59 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd)
|
|||||||
VSH_EXCLUSIVE_OPTIONS("list", "event");
|
VSH_EXCLUSIVE_OPTIONS("list", "event");
|
||||||
|
|
||||||
if (vshCommandOptBool(cmd, "list")) {
|
if (vshCommandOptBool(cmd, "list")) {
|
||||||
for (event = 0; event < VIR_DOMAIN_EVENT_ID_LAST; event++)
|
for (i = 0; i < G_N_ELEMENTS(virshDomainEventCallbacks); i++)
|
||||||
vshPrint(ctl, "%s\n", virshDomainEventCallbacks[event].name);
|
vshPrint(ctl, "%s\n", virshDomainEventCallbacks[i].name);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (vshCommandOptStringReq(ctl, cmd, "event", &eventName) < 0)
|
if (vshCommandOptStringReq(ctl, cmd, "event", &eventName) < 0)
|
||||||
return false;
|
return false;
|
||||||
if (eventName) {
|
|
||||||
for (event = 0; event < VIR_DOMAIN_EVENT_ID_LAST; event++)
|
if (!eventName && !all) {
|
||||||
if (STREQ(eventName, virshDomainEventCallbacks[event].name))
|
|
||||||
break;
|
|
||||||
if (event == VIR_DOMAIN_EVENT_ID_LAST) {
|
|
||||||
vshError(ctl, _("unknown event type %s"), eventName);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if (!all) {
|
|
||||||
vshError(ctl, "%s",
|
vshError(ctl, "%s",
|
||||||
_("one of --list, --all, or --event <type> is required"));
|
_("one of --list, --all, or --event <type> is required"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (all) {
|
if (eventName)
|
||||||
data = g_new0(virshDomEventData, VIR_DOMAIN_EVENT_ID_LAST);
|
data = g_new0(virshDomEventData, 1);
|
||||||
for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++) {
|
else
|
||||||
|
data = g_new0(virshDomEventData, G_N_ELEMENTS(virshDomainEventCallbacks));
|
||||||
|
|
||||||
|
for (i = 0; i < G_N_ELEMENTS(virshDomainEventCallbacks); i++) {
|
||||||
|
if (eventName &&
|
||||||
|
STRNEQ(eventName, virshDomainEventCallbacks[i].name))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
data[i].event = i;
|
||||||
data[i].ctl = ctl;
|
data[i].ctl = ctl;
|
||||||
data[i].loop = loop;
|
data[i].loop = loop;
|
||||||
data[i].count = &count;
|
data[i].count = &count;
|
||||||
data[i].timestamp = timestamp;
|
data[i].timestamp = timestamp;
|
||||||
data[i].cb = &virshDomainEventCallbacks[i];
|
data[i].cb = &virshDomainEventCallbacks[i];
|
||||||
data[i].id = -1;
|
data[i].id = -1;
|
||||||
|
ndata++;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
data = g_new0(virshDomEventData, 1);
|
if (ndata == 0) {
|
||||||
data[0].ctl = ctl;
|
vshError(ctl, _("unknown event type %s"), eventName);
|
||||||
data[0].loop = vshCommandOptBool(cmd, "loop");
|
return false;
|
||||||
data[0].count = &count;
|
|
||||||
data[0].timestamp = timestamp;
|
|
||||||
data[0].cb = &virshDomainEventCallbacks[event];
|
|
||||||
data[0].id = -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0)
|
if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if (vshCommandOptBool(cmd, "domain"))
|
if (vshCommandOptBool(cmd, "domain")) {
|
||||||
if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
|
if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
if (vshEventStart(ctl, timeout) < 0)
|
if (vshEventStart(ctl, timeout) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
for (i = 0; i < (all ? VIR_DOMAIN_EVENT_ID_LAST : 1); i++) {
|
for (i = 0; i < ndata; i++) {
|
||||||
if ((data[i].id = virConnectDomainEventRegisterAny(priv->conn, dom,
|
if ((data[i].id = virConnectDomainEventRegisterAny(priv->conn, dom,
|
||||||
all ? i : event,
|
data[i].event,
|
||||||
data[i].cb->cb,
|
data[i].cb->cb,
|
||||||
&data[i],
|
&data[i],
|
||||||
NULL)) < 0) {
|
NULL)) < 0) {
|
||||||
@ -985,12 +986,11 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd)
|
|||||||
cleanup:
|
cleanup:
|
||||||
vshEventCleanup(ctl);
|
vshEventCleanup(ctl);
|
||||||
if (data) {
|
if (data) {
|
||||||
for (i = 0; i < (all ? VIR_DOMAIN_EVENT_ID_LAST : 1); i++) {
|
for (i = 0; i < ndata; i++) {
|
||||||
if (data[i].id >= 0 &&
|
if (data[i].id >= 0 &&
|
||||||
virConnectDomainEventDeregisterAny(priv->conn, data[i].id) < 0)
|
virConnectDomainEventDeregisterAny(priv->conn, data[i].id) < 0)
|
||||||
ret = false;
|
ret = false;
|
||||||
}
|
}
|
||||||
VIR_FREE(data);
|
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user