int c_oneVmInfo(int vmid, char* ret_info, int length) { xmlrpc_value *resultP; int return_code; char *return_string; int retval = -1; resultP = xmlrpc_client_call(&one_client.env, one_client.url, "one.vmget_info", "(si)", one_client.session, vmid); xmlrpc_decompose_value(&one_client.env, resultP, "(bs)", &return_code, &return_string); if( return_code ) { if (virStrncpy(ret_info, return_string, length-1, length) != NULL) /* Only set the return value to 0 if we succeeded */ retval = 0; } xmlrpc_DECREF(resultP); VIR_FREE(return_string); return retval; }
static int getFlags(int fd, const char *ifname, struct ifreq *ifr) { memset(ifr, 0, sizeof(*ifr)); if (virStrncpy(ifr->ifr_name, ifname, strlen(ifname), sizeof(ifr->ifr_name)) == NULL) return ENODEV; if (ioctl(fd, SIOCGIFFLAGS, ifr) < 0) return errno; return 0; }
char* getContainerNameFromPath(const char* path) { int pathlen; int i = 0; char* ret = NULL; if (path == NULL) return NULL; pathlen = strlen(path); if (path[pathlen-1] == '/') { pathlen--; } i = pathlen - 1; while(i > 0 && path[i] != '/') { i--; } if (VIR_ALLOC_N(ret, pathlen-i) < 0) { return NULL; } return virStrncpy(ret, path+i+1, pathlen-i-1, pathlen-i); }
/** * virStrcpy * * A safe version of strcpy. The last parameter is the number of bytes * available in the destination string, *not* the number of bytes you want * to copy. If the destination is not large enough to hold all n of the * src string bytes plus a \0, NULL is returned and no data is copied. * If the destination is large enough to hold the source plus \0, then the * string is copied and a pointer to the destination string is returned. */ char * virStrcpy(char *dest, const char *src, size_t destbytes) { return virStrncpy(dest, src, strlen(src), destbytes); }
static int xenParseXLUSB(virConfPtr conf, virDomainDefPtr def) { virConfValuePtr list = virConfGetValue(conf, "usbdev"); virDomainHostdevDefPtr hostdev = NULL; if (list && list->type == VIR_CONF_LIST) { list = list->list; while (list) { char bus[3]; char device[3]; char *key; int busNum; int devNum; bus[0] = device[0] = '\0'; if ((list->type != VIR_CONF_STRING) || (list->str == NULL)) goto skipusb; /* usbdev=['hostbus=1,hostaddr=3'] */ key = list->str; while (key) { char *data; char *nextkey = strchr(key, ','); if (!(data = strchr(key, '='))) goto skipusb; data++; if (STRPREFIX(key, "hostbus=")) { int len = nextkey ? (nextkey - data) : sizeof(bus) - 1; if (virStrncpy(bus, data, len, sizeof(bus)) == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("bus %s too big for destination"), data); goto skipusb; } } else if (STRPREFIX(key, "hostaddr=")) { int len = nextkey ? (nextkey - data) : sizeof(device) - 1; if (virStrncpy(device, data, len, sizeof(device)) == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("device %s too big for destination"), data); goto skipusb; } } while (nextkey && (nextkey[0] == ',' || nextkey[0] == ' ' || nextkey[0] == '\t')) nextkey++; key = nextkey; } if (virStrToLong_i(bus, NULL, 16, &busNum) < 0) goto skipusb; if (virStrToLong_i(device, NULL, 16, &devNum) < 0) goto skipusb; if (!(hostdev = virDomainHostdevDefAlloc(NULL))) return -1; hostdev->managed = false; hostdev->source.subsys.type = VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB; hostdev->source.subsys.u.usb.bus = busNum; hostdev->source.subsys.u.usb.device = devNum; if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev) < 0) { virDomainHostdevDefFree(hostdev); return -1; } skipusb: list = list->next; } } return 0; }
static int hypervNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info) { int result = -1; hypervPrivate *priv = conn->privateData; virBuffer query = VIR_BUFFER_INITIALIZER; Win32_ComputerSystem *computerSystem = NULL; Win32_Processor *processorList = NULL; Win32_Processor *processor = NULL; char *tmp; memset(info, 0, sizeof(*info)); virBufferAddLit(&query, WIN32_COMPUTERSYSTEM_WQL_SELECT); /* Get Win32_ComputerSystem */ if (hypervGetWin32ComputerSystemList(priv, &query, &computerSystem) < 0) { goto cleanup; } if (computerSystem == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not lookup %s"), "Win32_ComputerSystem"); goto cleanup; } /* Get Win32_Processor list */ virBufferAsprintf(&query, "associators of " "{Win32_ComputerSystem.Name=\"%s\"} " "where AssocClass = Win32_ComputerSystemProcessor " "ResultClass = Win32_Processor", computerSystem->data->Name); if (hypervGetWin32ProcessorList(priv, &query, &processorList) < 0) { goto cleanup; } if (processorList == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not lookup %s"), "Win32_Processor"); goto cleanup; } /* Strip the string to fit more relevant information in 32 chars */ tmp = processorList->data->Name; while (*tmp != '\0') { if (STRPREFIX(tmp, " ")) { memmove(tmp, tmp + 1, strlen(tmp + 1) + 1); continue; } else if (STRPREFIX(tmp, "(R)") || STRPREFIX(tmp, "(C)")) { memmove(tmp, tmp + 3, strlen(tmp + 3) + 1); continue; } else if (STRPREFIX(tmp, "(TM)")) { memmove(tmp, tmp + 4, strlen(tmp + 4) + 1); continue; } ++tmp; } /* Fill struct */ if (virStrncpy(info->model, processorList->data->Name, sizeof(info->model) - 1, sizeof(info->model)) == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("CPU model %s too long for destination"), processorList->data->Name); goto cleanup; } info->memory = computerSystem->data->TotalPhysicalMemory / 1024; /* byte to kilobyte */ info->mhz = processorList->data->MaxClockSpeed; info->nodes = 1; info->sockets = 0; for (processor = processorList; processor != NULL; processor = processor->next) { ++info->sockets; } info->cores = processorList->data->NumberOfCores; info->threads = info->cores / processorList->data->NumberOfLogicalProcessors; info->cpus = info->sockets * info->cores; result = 0; cleanup: hypervFreeObject(priv, (hypervObject *)computerSystem); hypervFreeObject(priv, (hypervObject *)processorList); return result; }
int openvzReadNetworkConf(virDomainDefPtr def, int veid) { int ret; virDomainNetDefPtr net = NULL; char *temp = NULL; char *token, *saveptr = NULL; /*parse routing network configuration* * Sample from config: * IP_ADDRESS="1.1.1.1 1.1.1.2" * IPs split by space */ ret = openvzReadVPSConfigParam(veid, "IP_ADDRESS", &temp); if (ret < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not read 'IP_ADDRESS' from config for container %d"), veid); goto error; } else if (ret > 0) { token = strtok_r(temp, " ", &saveptr); while (token != NULL) { if (VIR_ALLOC(net) < 0) goto error; net->type = VIR_DOMAIN_NET_TYPE_ETHERNET; if (virDomainNetAppendIpAddress(net, token, AF_UNSPEC, 0) < 0) goto error; if (VIR_APPEND_ELEMENT_COPY(def->nets, def->nnets, net) < 0) goto error; token = strtok_r(NULL, " ", &saveptr); } } /*parse bridge devices*/ /*Sample from config: * NETIF="ifname=eth10,mac=00:18:51:C1:05:EE,host_ifname=veth105.10,host_mac=00:18:51:8F:D9:F3" *devices split by ';' */ ret = openvzReadVPSConfigParam(veid, "NETIF", &temp); if (ret < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not read 'NETIF' from config for container %d"), veid); goto error; } else if (ret > 0) { token = strtok_r(temp, ";", &saveptr); while (token != NULL) { /*add new device to list*/ if (VIR_ALLOC(net) < 0) goto error; net->type = VIR_DOMAIN_NET_TYPE_BRIDGE; char *p = token; char cpy_temp[32]; int len; /*parse string*/ do { char *next = strchrnul(p, ','); if (STRPREFIX(p, "ifname=")) { /* skip in libvirt */ } else if (STRPREFIX(p, "host_ifname=")) { p += 12; len = next - p; if (len > 16) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Too long network device name")); goto error; } if (VIR_ALLOC_N(net->ifname, len+1) < 0) goto error; if (virStrncpy(net->ifname, p, len, len+1) == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Network ifname %s too long for destination"), p); goto error; } } else if (STRPREFIX(p, "bridge=")) { p += 7; len = next - p; if (len > 16) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Too long bridge device name")); goto error; } if (VIR_ALLOC_N(net->data.bridge.brname, len+1) < 0) goto error; if (virStrncpy(net->data.bridge.brname, p, len, len+1) == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Bridge name %s too long for destination"), p); goto error; } } else if (STRPREFIX(p, "mac=")) { p += 4; len = next - p; if (len != 17) { /* should be 17 */ virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Wrong length MAC address")); goto error; } if (virStrncpy(cpy_temp, p, len, sizeof(cpy_temp)) == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("MAC address %s too long for destination"), p); goto error; } if (virMacAddrParse(cpy_temp, &net->mac) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Wrong MAC address")); goto error; } } p = ++next; } while (p < token + strlen(token)); if (VIR_APPEND_ELEMENT_COPY(def->nets, def->nnets, net) < 0) goto error; token = strtok_r(NULL, ";", &saveptr); } } VIR_FREE(temp); return 0; error: VIR_FREE(temp); virDomainNetDefFree(net); return -1; }
static int xenParseXMDisk(virConfPtr conf, virDomainDefPtr def) { virDomainDiskDefPtr disk = NULL; int hvm = def->os.type == VIR_DOMAIN_OSTYPE_HVM; virConfValuePtr list = virConfGetValue(conf, "disk"); if (list && list->type == VIR_CONF_LIST) { list = list->list; while (list) { char *head; char *offset; char *tmp; const char *src; if ((list->type != VIR_CONF_STRING) || (list->str == NULL)) goto skipdisk; head = list->str; if (!(disk = virDomainDiskDefNew(NULL))) return -1; /* * Disks have 3 components, SOURCE,DEST-DEVICE,MODE * eg, phy:/dev/HostVG/XenGuest1,xvda,w * The SOURCE is usually prefixed with a driver type, * and optionally driver sub-type * The DEST-DEVICE is optionally post-fixed with disk type */ /* Extract the source file path*/ if (!(offset = strchr(head, ','))) goto skipdisk; if (offset == head) { /* No source file given, eg CDROM with no media */ ignore_value(virDomainDiskSetSource(disk, NULL)); } else { if (VIR_STRNDUP(tmp, head, offset - head) < 0) goto cleanup; if (virDomainDiskSetSource(disk, tmp) < 0) { VIR_FREE(tmp); goto cleanup; } VIR_FREE(tmp); } head = offset + 1; /* Remove legacy ioemu: junk */ if (STRPREFIX(head, "ioemu:")) head = head + 6; /* Extract the dest device name */ if (!(offset = strchr(head, ','))) goto skipdisk; if (VIR_ALLOC_N(disk->dst, (offset - head) + 1) < 0) goto cleanup; if (virStrncpy(disk->dst, head, offset - head, (offset - head) + 1) == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Dest file %s too big for destination"), head); goto cleanup; } head = offset + 1; /* Extract source driver type */ src = virDomainDiskGetSource(disk); if (src) { size_t len; /* The main type phy:, file:, tap: ... */ if ((tmp = strchr(src, ':')) != NULL) { len = tmp - src; if (VIR_STRNDUP(tmp, src, len) < 0) goto cleanup; if (virDomainDiskSetDriver(disk, tmp) < 0) { VIR_FREE(tmp); goto cleanup; } VIR_FREE(tmp); /* Strip the prefix we found off the source file name */ if (virDomainDiskSetSource(disk, src + len + 1) < 0) goto cleanup; src = virDomainDiskGetSource(disk); } /* And the sub-type for tap:XXX: type */ if (STREQ_NULLABLE(virDomainDiskGetDriver(disk), "tap")) { char *driverType; if (!(tmp = strchr(src, ':'))) goto skipdisk; len = tmp - src; if (VIR_STRNDUP(driverType, src, len) < 0) goto cleanup; if (STREQ(driverType, "aio")) virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_RAW); else virDomainDiskSetFormat(disk, virStorageFileFormatTypeFromString(driverType)); VIR_FREE(driverType); if (virDomainDiskGetFormat(disk) <= 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Unknown driver type %s"), src); goto cleanup; } /* Strip the prefix we found off the source file name */ if (virDomainDiskSetSource(disk, src + len + 1) < 0) goto cleanup; src = virDomainDiskGetSource(disk); } } /* No source, or driver name, so fix to phy: */ if (!virDomainDiskGetDriver(disk) && virDomainDiskSetDriver(disk, "phy") < 0) goto cleanup; /* phy: type indicates a block device */ virDomainDiskSetType(disk, STREQ(virDomainDiskGetDriver(disk), "phy") ? VIR_STORAGE_TYPE_BLOCK : VIR_STORAGE_TYPE_FILE); /* Check for a :cdrom/:disk postfix */ disk->device = VIR_DOMAIN_DISK_DEVICE_DISK; if ((tmp = strchr(disk->dst, ':')) != NULL) { if (STREQ(tmp, ":cdrom")) disk->device = VIR_DOMAIN_DISK_DEVICE_CDROM; tmp[0] = '\0'; } if (STRPREFIX(disk->dst, "xvd") || !hvm) { disk->bus = VIR_DOMAIN_DISK_BUS_XEN; } else if (STRPREFIX(disk->dst, "sd")) { disk->bus = VIR_DOMAIN_DISK_BUS_SCSI; } else { disk->bus = VIR_DOMAIN_DISK_BUS_IDE; } if (STREQ(head, "r") || STREQ(head, "ro")) disk->src->readonly = true; else if ((STREQ(head, "w!")) || (STREQ(head, "!"))) disk->src->shared = true; /* Maintain list in sorted order according to target device name */ if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0) goto cleanup; skipdisk: list = list->next; virDomainDiskDefFree(disk); disk = NULL; } } return 0; cleanup: virDomainDiskDefFree(disk); return -1; }