提交 1f0cac35 编写于 作者: P Peter Krempa

virsh-domain-monitor: Refactor cmdDomIfGetLink

The domif-getlink command did not terminate successfully when the
interface state was found. As the code used old and too complex approach
to do the job, this patch refactors it and fixes the bug.
上级 6bd94a1b
...@@ -663,25 +663,23 @@ cmdDomIfGetLink(vshControl *ctl, const vshCmd *cmd) ...@@ -663,25 +663,23 @@ cmdDomIfGetLink(vshControl *ctl, const vshCmd *cmd)
{ {
virDomainPtr dom; virDomainPtr dom;
const char *iface = NULL; const char *iface = NULL;
int flags = 0;
char *state = NULL; char *state = NULL;
char *value = NULL; char *xpath = NULL;
virMacAddr macaddr; virMacAddr macaddr;
const char *element; char macstr[VIR_MAC_STRING_BUFLEN] = "";
const char *attr; char *desc = NULL;
bool ret = false;
int i;
char *desc;
xmlDocPtr xml = NULL; xmlDocPtr xml = NULL;
xmlXPathContextPtr ctxt = NULL; xmlXPathContextPtr ctxt = NULL;
xmlNodePtr cur = NULL; xmlNodePtr *interfaces = NULL;
xmlXPathObjectPtr obj = NULL; int ninterfaces;
unsigned int flags = 0;
bool ret = false;
if (!(dom = vshCommandOptDomain(ctl, cmd, NULL))) if (vshCommandOptStringReq(ctl, cmd, "interface", &iface) < 0)
return false; return false;
if (vshCommandOptStringReq(ctl, cmd, "interface", &iface) < 0) if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
goto cleanup; return false;
if (vshCommandOptBool(cmd, "config")) if (vshCommandOptBool(cmd, "config"))
flags = VIR_DOMAIN_XML_INACTIVE; flags = VIR_DOMAIN_XML_INACTIVE;
...@@ -691,72 +689,50 @@ cmdDomIfGetLink(vshControl *ctl, const vshCmd *cmd) ...@@ -691,72 +689,50 @@ cmdDomIfGetLink(vshControl *ctl, const vshCmd *cmd)
goto cleanup; goto cleanup;
} }
xml = virXMLParseStringCtxt(desc, _("(domain_definition)"), &ctxt); if (!(xml = virXMLParseStringCtxt(desc, _("(domain_definition)"), &ctxt))) {
VIR_FREE(desc);
if (!xml) {
vshError(ctl, _("Failed to parse domain description xml")); vshError(ctl, _("Failed to parse domain description xml"));
goto cleanup; goto cleanup;
} }
obj = xmlXPathEval(BAD_CAST "/domain/devices/interface", ctxt); /* normalize the mac addr */
if (obj == NULL || obj->type != XPATH_NODESET || if (virMacAddrParse(iface, &macaddr) == 0)
obj->nodesetval == NULL || obj->nodesetval->nodeNr == 0) { virMacAddrFormat(&macaddr, macstr);
vshError(ctl, _("Failed to extract interface information or no interfaces found"));
if (virAsprintf(&xpath, "/domain/devices/interface[(mac/@address = '%s') or "
" (target/@dev = '%s')]",
macstr, iface) < 0) {
virReportOOMError();
goto cleanup; goto cleanup;
} }
if (virMacAddrParse(iface, &macaddr) == 0) { if ((ninterfaces = virXPathNodeSet(xpath, ctxt, &interfaces)) < 0) {
element = "mac"; vshError(ctl, _("Failed to extract interface information"));
attr = "address"; goto cleanup;
} else {
element = "target";
attr = "dev";
} }
/* find interface with matching mac addr */ if (ninterfaces != 1) {
for (i = 0; i < obj->nodesetval->nodeNr; i++) { if (macstr[0])
cur = obj->nodesetval->nodeTab[i]->children; vshError(ctl, _("Interface (mac: %s) not found."), macstr);
else
while (cur) { vshError(ctl, _("Interface (dev: %s) not found."), iface);
if (cur->type == XML_ELEMENT_NODE &&
xmlStrEqual(cur->name, BAD_CAST element)) {
value = virXMLPropString(cur, attr);
if (STRCASEEQ(value, iface)) { goto cleanup;
VIR_FREE(value);
goto hit;
}
VIR_FREE(value);
}
cur = cur->next;
}
} }
vshError(ctl, _("Interface (%s: %s) not found."), element, iface); ctxt->node = interfaces[0];
goto cleanup;
hit:
cur = obj->nodesetval->nodeTab[i]->children;
while (cur) {
if (cur->type == XML_ELEMENT_NODE &&
xmlStrEqual(cur->name, BAD_CAST "link")) {
state = virXMLPropString(cur, "state");
vshPrint(ctl, "%s %s", iface, state);
VIR_FREE(state);
goto cleanup; if ((state = virXPathString("string(./link/@state)", ctxt)))
} vshPrint(ctl, "%s %s", iface, state);
cur = cur->next; else
} vshPrint(ctl, "%s default", iface);
/* attribute not found */
vshPrint(ctl, "%s default", iface);
ret = true; ret = true;
cleanup: cleanup:
xmlXPathFreeObject(obj); VIR_FREE(desc);
VIR_FREE(state);
VIR_FREE(interfaces);
VIR_FREE(xpath);
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
xmlFreeDoc(xml); xmlFreeDoc(xml);
virDomainFree(dom); virDomainFree(dom);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册