static bool cmdInterfaceEdit(vshControl *ctl, const vshCmd *cmd) { bool ret = false; virInterfacePtr iface = NULL; virInterfacePtr iface_edited = NULL; unsigned int flags = VIR_INTERFACE_XML_INACTIVE; virshControlPtr priv = ctl->privData; iface = virshCommandOptInterface(ctl, cmd, NULL); if (iface == NULL) goto cleanup; #define EDIT_GET_XML virInterfaceGetXMLDesc(iface, flags) #define EDIT_NOT_CHANGED \ do { \ vshPrintExtra(ctl, _("Interface %s XML configuration not changed.\n"), \ virInterfaceGetName(iface)); \ ret = true; \ goto edit_cleanup; \ } while (0) #define EDIT_DEFINE \ (iface_edited = virInterfaceDefineXML(priv->conn, doc_edited, 0)) #include "virsh-edit.c" vshPrintExtra(ctl, _("Interface %s XML configuration edited.\n"), virInterfaceGetName(iface_edited)); ret = true; cleanup: if (iface) virInterfaceFree(iface); if (iface_edited) virInterfaceFree(iface_edited); return ret; }
static void vshInterfaceListFree(vshInterfaceListPtr list) { int i; if (list && list->nifaces) { for (i = 0; i < list->nifaces; i++) { if (list->ifaces[i]) virInterfaceFree(list->ifaces[i]); } VIR_FREE(list->ifaces); } VIR_FREE(list); }
void vert_cleanup(ErlNifEnv *env, void *obj) { VERT_RESOURCE *vp = obj; if (vp->res == NULL) return; switch (vp->type) { case VERT_RES_CONNECT: (void)virConnectClose(vp->res); break; case VERT_RES_DOMAIN: (void)virDomainFree(vp->res); break; case VERT_RES_INTERFACE: (void)virInterfaceFree(vp->res); break; case VERT_RES_NETWORK: (void)virNetworkFree(vp->res); break; case VERT_RES_NODEDEVICE: (void)virNodeDeviceFree(vp->res); break; #if HAVE_NWFILTER case VERT_RES_NWFILTER: (void)virNWFilterFree(vp->res); break; #endif case VERT_RES_SECRET: (void)virSecretFree(vp->res); break; case VERT_RES_STORAGEPOOL: (void)virStoragePoolFree(vp->res); break; case VERT_RES_STORAGEVOL: (void)virStorageVolFree(vp->res); break; case VERT_RES_STREAM: (void)virStreamFree(vp->res); break; default: break; } vp->res = NULL; }
static int udevConnectListAllInterfaces(virConnectPtr conn, virInterfacePtr **ifaces, unsigned int flags) { struct udev_iface_driver *driverState = conn->interfacePrivateData; struct udev *udev; struct udev_enumerate *enumerate = NULL; struct udev_list_entry *devices; struct udev_list_entry *dev_entry; virInterfacePtr *ifaces_list = NULL; virInterfacePtr iface_obj; int tmp_count; int count = 0; int status = 0; int ret; virCheckFlags(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE, -1); if (virConnectListAllInterfacesEnsureACL(conn) < 0) return -1; /* Grab a udev reference */ udev = udev_ref(driverState->udev); /* List all interfaces in case we support more filter flags in the future */ enumerate = udevGetDevices(udev, VIR_UDEV_IFACE_ALL); if (!enumerate) { virReportError(VIR_ERR_INTERNAL_ERROR, _("failed to get list of %s interfaces on host"), virUdevStatusString(status)); ret = -1; goto cleanup; } /* Do the scan to load up the enumeration */ udev_enumerate_scan_devices(enumerate); /* Get a list we can walk */ devices = udev_enumerate_get_list_entry(enumerate); /* For each item so we can count */ udev_list_entry_foreach(dev_entry, devices) { count++; } /* If we've got nothing, exit out */ if (count == 0) { ret = 0; goto cleanup; } /* If we're asked for the ifaces then alloc up memory */ if (ifaces && VIR_ALLOC_N(ifaces_list, count + 1) < 0) { ret = -1; goto cleanup; } /* Get a list we can walk */ devices = udev_enumerate_get_list_entry(enumerate); /* reset our iterator */ count = 0; /* Walk through each device */ udev_list_entry_foreach(dev_entry, devices) { struct udev_device *dev; const char *path; const char *name; const char *macaddr; virInterfaceDefPtr def; path = udev_list_entry_get_name(dev_entry); dev = udev_device_new_from_syspath(udev, path); name = udev_device_get_sysname(dev); macaddr = udev_device_get_sysattr_value(dev, "address"); status = STREQ(udev_device_get_sysattr_value(dev, "operstate"), "up"); def = udevGetMinimalDefForDevice(dev); if (!virConnectListAllInterfacesCheckACL(conn, def)) { udev_device_unref(dev); virInterfaceDefFree(def); continue; } virInterfaceDefFree(def); /* Filter the results */ if (MATCH(VIR_CONNECT_LIST_INTERFACES_FILTERS_ACTIVE) && !((MATCH(VIR_CONNECT_LIST_INTERFACES_ACTIVE) && status) || (MATCH(VIR_CONNECT_LIST_INTERFACES_INACTIVE) && !status))) { udev_device_unref(dev); continue; } /* If we matched a filter, then add it */ if (ifaces) { iface_obj = virGetInterface(conn, name, macaddr); ifaces_list[count++] = iface_obj; } udev_device_unref(dev); } /* Drop our refcounts */ udev_enumerate_unref(enumerate); udev_unref(udev); /* Trim the array to its final size */ if (ifaces) { ignore_value(VIR_REALLOC_N(ifaces_list, count + 1)); *ifaces = ifaces_list; ifaces_list = NULL; } return count; cleanup: if (enumerate) udev_enumerate_unref(enumerate); udev_unref(udev); if (ifaces) { for (tmp_count = 0; tmp_count < count; tmp_count++) virInterfaceFree(ifaces_list[tmp_count]); } VIR_FREE(ifaces_list); return ret; }