/** * virGetNWFilter: * @conn: the hypervisor connection * @name: pointer to the network filter pool name * @uuid: pointer to the uuid * * Lookup if the network filter is already registered for that connection, * if yes return a new pointer to it, if no allocate a new structure, * and register it in the table. In any case a corresponding call to * virObjectUnref() is needed to not leak data. * * Returns a pointer to the network, or NULL in case of failure */ virNWFilterPtr virGetNWFilter(virConnectPtr conn, const char *name, const unsigned char *uuid) { virNWFilterPtr ret = NULL; if (virDataTypesInitialize() < 0) return NULL; if (!VIR_IS_CONNECT(conn)) { virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection")); return NULL; } virCheckNonNullArgReturn(name, NULL); virCheckNonNullArgReturn(uuid, NULL); if (!(ret = virObjectNew(virNWFilterClass))) return NULL; if (VIR_STRDUP(ret->name, name) < 0) goto error; memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); ret->conn = virObjectRef(conn); return ret; error: virObjectUnref(ret); return NULL; }
/** * virGetNodeDevice: * @conn: the hypervisor connection * @name: device name (unique on node) * * Lookup if the device is already registered for that connection, * if yes return a new pointer to it, if no allocate a new structure, * and register it in the table. In any case a corresponding call to * virObjectUnref() is needed to not leak data. * * Returns a pointer to the node device, or NULL in case of failure */ virNodeDevicePtr virGetNodeDevice(virConnectPtr conn, const char *name) { virNodeDevicePtr ret = NULL; if (virDataTypesInitialize() < 0) return NULL; if (!VIR_IS_CONNECT(conn)) { virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection")); return NULL; } virCheckNonNullArgReturn(name, NULL); if (!(ret = virObjectNew(virNodeDeviceClass))) return NULL; if (VIR_STRDUP(ret->name, name) < 0) goto error; ret->conn = virObjectRef(conn); return ret; error: virObjectUnref(ret); return NULL; }
/** * virGetSecret: * @conn: the hypervisor connection * @uuid: secret UUID * * Lookup if the secret is already registered for that connection, if so return * a pointer to it, otherwise allocate a new structure, and register it in the * table. In any case a corresponding call to virObjectUnref() is needed to not * leak data. * * Returns a pointer to the secret, or NULL in case of failure */ virSecretPtr virGetSecret(virConnectPtr conn, const unsigned char *uuid, int usageType, const char *usageID) { virSecretPtr ret = NULL; if (virDataTypesInitialize() < 0) return NULL; if (!VIR_IS_CONNECT(conn)) { virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection")); return NULL; } virCheckNonNullArgReturn(uuid, NULL); virCheckNonNullArgReturn(usageID, NULL); if (!(ret = virObjectNew(virSecretClass))) return NULL; memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); ret->usageType = usageType; if (VIR_STRDUP(ret->usageID, usageID) < 0) goto error; ret->conn = virObjectRef(conn); return ret; error: virObjectUnref(ret); return NULL; }
/** * virGetInterface: * @conn: the hypervisor connection * @name: pointer to the interface name * @mac: pointer to the mac * * Lookup if the interface is already registered for that connection, * if yes return a new pointer to it (possibly updating the MAC * address), if no allocate a new structure, and register it in the * table. In any case a corresponding call to virObjectUnref() is * needed to not leak data. * * Returns a pointer to the interface, or NULL in case of failure */ virInterfacePtr virGetInterface(virConnectPtr conn, const char *name, const char *mac) { virInterfacePtr ret = NULL; if (virDataTypesInitialize() < 0) return NULL; if (!VIR_IS_CONNECT(conn)) { virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection")); return NULL; } virCheckNonNullArgReturn(name, NULL); /* a NULL mac from caller is okay. Treat it as blank */ if (mac == NULL) mac = ""; if (!(ret = virObjectNew(virInterfaceClass))) return NULL; if (VIR_STRDUP(ret->name, name) < 0 || VIR_STRDUP(ret->mac, mac) < 0) goto error; ret->conn = virObjectRef(conn); return ret; error: virObjectUnref(ret); return NULL; }
/** * virGetStorageVol: * @conn: the hypervisor connection * @pool: pool owning the volume * @name: pointer to the storage vol name * @key: pointer to unique key of the volume * * Lookup if the storage vol is already registered for that connection, * if yes return a new pointer to it, if no allocate a new structure, * and register it in the table. In any case a corresponding call to * virUnrefStorageVol() is needed to not leak data. * * Returns a pointer to the storage vol, or NULL in case of failure */ virStorageVolPtr virGetStorageVol(virConnectPtr conn, const char *pool, const char *name, const char *key) { virStorageVolPtr ret = NULL; if (!VIR_IS_CONNECT(conn)) { virLibConnError(VIR_ERR_INVALID_ARG, _("no connection")); return NULL; } if (name == NULL) { virLibConnError(VIR_ERR_INVALID_ARG, _("missing name")); return NULL; } if (key == NULL) { virLibConnError(VIR_ERR_INVALID_ARG, _("missing key")); return NULL; } virMutexLock(&conn->lock); if (VIR_ALLOC(ret) < 0) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->pool = strdup(pool); if (ret->pool == NULL) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->name = strdup(name); if (ret->name == NULL) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } if (virStrcpyStatic(ret->key, key) == NULL) { virMutexUnlock(&conn->lock); virLibConnError(VIR_ERR_INTERNAL_ERROR, _("Volume key %s too large for destination"), key); goto error; } ret->magic = VIR_STORAGE_VOL_MAGIC; ret->conn = conn; conn->refs++; ret->refs++; virMutexUnlock(&conn->lock); return(ret); error: if (ret != NULL) { VIR_FREE(ret->name); VIR_FREE(ret->pool); VIR_FREE(ret); } return(NULL); }
/** * virGetStorageVol: * @conn: the hypervisor connection * @pool: pool owning the volume * @name: pointer to the storage vol name * @key: pointer to unique key of the volume * * Lookup if the storage vol is already registered for that connection, * if yes return a new pointer to it, if no allocate a new structure, * and register it in the table. In any case a corresponding call to * virUnrefStorageVol() is needed to not leak data. * * Returns a pointer to the storage vol, or NULL in case of failure */ virStorageVolPtr virGetStorageVol(virConnectPtr conn, const char *pool, const char *name, const char *key) { virStorageVolPtr ret = NULL; if (!VIR_IS_CONNECT(conn)) { virLibConnError(VIR_ERR_INVALID_CONN, _("no connection")); return NULL; } virCheckNonNullArgReturn(name, NULL); virCheckNonNullArgReturn(key, NULL); virMutexLock(&conn->lock); if (VIR_ALLOC(ret) < 0) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->pool = strdup(pool); if (ret->pool == NULL) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->name = strdup(name); if (ret->name == NULL) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->key = strdup(key); if (ret->key == NULL) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->magic = VIR_STORAGE_VOL_MAGIC; ret->conn = conn; conn->refs++; ret->refs++; virMutexUnlock(&conn->lock); return ret; error: if (ret != NULL) { VIR_FREE(ret->key); VIR_FREE(ret->name); VIR_FREE(ret->pool); VIR_FREE(ret); } return NULL; }
/** * virGetInterface: * @conn: the hypervisor connection * @name: pointer to the interface name * @mac: pointer to the mac * * Lookup if the interface is already registered for that connection, * if yes return a new pointer to it (possibly updating the MAC * address), if no allocate a new structure, and register it in the * table. In any case a corresponding call to virUnrefInterface() is * needed to not leak data. * * Returns a pointer to the interface, or NULL in case of failure */ virInterfacePtr virGetInterface(virConnectPtr conn, const char *name, const char *mac) { virInterfacePtr ret = NULL; if (!VIR_IS_CONNECT(conn)) { virLibConnError(VIR_ERR_INVALID_ARG, _("no connection")); return NULL; } if (name == NULL) { virLibConnError(VIR_ERR_INVALID_ARG, _("missing name")); return NULL; } /* a NULL mac from caller is okay. Treat it as blank */ if (mac == NULL) mac = ""; virMutexLock(&conn->lock); if (VIR_ALLOC(ret) < 0) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->name = strdup(name); if (ret->name == NULL) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->mac = strdup(mac); if (ret->mac == NULL) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->magic = VIR_INTERFACE_MAGIC; ret->conn = conn; conn->refs++; ret->refs++; virMutexUnlock(&conn->lock); return(ret); error: if (ret != NULL) { VIR_FREE(ret->name); VIR_FREE(ret->mac); VIR_FREE(ret); } return(NULL); }
/** * virGetSecret: * @conn: the hypervisor connection * @uuid: secret UUID * * Lookup if the secret is already registered for that connection, if so return * a pointer to it, otherwise allocate a new structure, and register it in the * table. In any case a corresponding call to virUnrefSecret() is needed to not * leak data. * * Returns a pointer to the secret, or NULL in case of failure */ virSecretPtr virGetSecret(virConnectPtr conn, const unsigned char *uuid, int usageType, const char *usageID) { virSecretPtr ret = NULL; char uuidstr[VIR_UUID_STRING_BUFLEN]; if (!VIR_IS_CONNECT(conn)) { virLibConnError(VIR_ERR_INVALID_ARG, _("no connection")); return NULL; } if (uuid == NULL) { virLibConnError(VIR_ERR_INVALID_ARG, _("missing uuid")); return NULL; } if (usageID == NULL) { virLibConnError(VIR_ERR_INVALID_ARG, _("missing usageID")); return NULL; } virMutexLock(&conn->lock); virUUIDFormat(uuid, uuidstr); if (VIR_ALLOC(ret) < 0) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->magic = VIR_SECRET_MAGIC; ret->conn = conn; memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); ret->usageType = usageType; if (!(ret->usageID = strdup(usageID))) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } conn->refs++; ret->refs++; virMutexUnlock(&conn->lock); return ret; error: if (ret != NULL) { VIR_FREE(ret->usageID); VIR_FREE(ret); } return NULL; }
/** * virGetStoragePool: * @conn: the hypervisor connection * @name: pointer to the storage pool name * @uuid: pointer to the uuid * * Lookup if the storage pool is already registered for that connection, * if yes return a new pointer to it, if no allocate a new structure, * and register it in the table. In any case a corresponding call to * virUnrefStoragePool() is needed to not leak data. * * Returns a pointer to the network, or NULL in case of failure */ virStoragePoolPtr virGetStoragePool(virConnectPtr conn, const char *name, const unsigned char *uuid) { virStoragePoolPtr ret = NULL; char uuidstr[VIR_UUID_STRING_BUFLEN]; if (!VIR_IS_CONNECT(conn)) { virLibConnError(VIR_ERR_INVALID_ARG, _("no connection")); return NULL; } if (name == NULL) { virLibConnError(VIR_ERR_INVALID_ARG, _("missing name")); return NULL; } if (uuid == NULL) { virLibConnError(VIR_ERR_INVALID_ARG, _("missing uuid")); return NULL; } virMutexLock(&conn->lock); virUUIDFormat(uuid, uuidstr); if (VIR_ALLOC(ret) < 0) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->name = strdup(name); if (ret->name == NULL) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->magic = VIR_STORAGE_POOL_MAGIC; ret->conn = conn; memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); conn->refs++; ret->refs++; virMutexUnlock(&conn->lock); return(ret); error: if (ret != NULL) { VIR_FREE(ret->name); VIR_FREE(ret); } return(NULL); }
/** * virGetDomain: * @conn: the hypervisor connection * @name: pointer to the domain name * @uuid: pointer to the uuid * * Lookup if the domain is already registered for that connection, * if yes return a new pointer to it, if no allocate a new structure, * and register it in the table. In any case a corresponding call to * virUnrefDomain() is needed to not leak data. * * Returns a pointer to the domain, or NULL in case of failure */ virDomainPtr virGetDomain(virConnectPtr conn, const char *name, const unsigned char *uuid) { virDomainPtr ret = NULL; char uuidstr[VIR_UUID_STRING_BUFLEN]; if (!VIR_IS_CONNECT(conn)) { virLibConnError(VIR_ERR_INVALID_CONN, _("no connection")); return NULL; } virCheckNonNullArgReturn(name, NULL); virCheckNonNullArgReturn(uuid, NULL); virMutexLock(&conn->lock); virUUIDFormat(uuid, uuidstr); if (VIR_ALLOC(ret) < 0) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->name = strdup(name); if (ret->name == NULL) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->magic = VIR_DOMAIN_MAGIC; ret->conn = conn; ret->id = -1; memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); conn->refs++; ret->refs++; virMutexUnlock(&conn->lock); return ret; error: if (ret != NULL) { VIR_FREE(ret->name); VIR_FREE(ret); } return NULL; }
/** * virDomainQemuAttach: * @conn: pointer to a hypervisor connection * @pid_value: the UNIX process ID of the external QEMU process * @flags: optional flags, currently unused * * This API is QEMU specific, so it will only work with hypervisor * connections to the QEMU driver. * * This API will attach to an externally launched QEMU process * identified by @pid. There are several requirements to successfully * attach to an external QEMU process: * * - It must have been started with a monitor socket using the UNIX * domain socket protocol. * - No device hotplug/unplug, or other configuration changes can * have been made via the monitor since it started. * - The '-name' and '-uuid' arguments should have been set (not * mandatory, but strongly recommended) * * To date, the only platforms we know of where pid_t is larger than * unsigned int (64-bit Windows) also lack UNIX sockets, so the choice * of @pid_value as an unsigned int should not present any difficulties. * * If successful, then the guest will appear in the list of running * domains for this connection, and other APIs should operate * normally (provided the above requirements were honored). * * Returns a new domain object on success, NULL otherwise */ virDomainPtr virDomainQemuAttach(virConnectPtr conn, unsigned int pid_value, unsigned int flags) { pid_t pid = pid_value; VIR_DEBUG("conn=%p, pid=%u, flags=%x", conn, pid_value, flags); virResetLastError(); if (!VIR_IS_CONNECT(conn)) { virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__); virDispatchError(NULL); return NULL; } virCheckPositiveArgGoto(pid_value, error); if (pid != pid_value) { virReportInvalidArg(pid_value, _("pid_value in %s is too large"), __FUNCTION__); goto error; } if (conn->flags & VIR_CONNECT_RO) { virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__); goto error; } if (conn->driver->qemuDomainAttach) { virDomainPtr ret; ret = conn->driver->qemuDomainAttach(conn, pid_value, flags); if (!ret) goto error; return ret; } virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__); error: virDispatchError(conn); return NULL; }
/** * virUnrefConnect: * @conn: the hypervisor connection to unreference * * Unreference the connection. If the use count drops to zero, the structure is * actually freed. * * Returns the reference count or -1 in case of failure. */ int virUnrefConnect(virConnectPtr conn) { int refs; if ((!VIR_IS_CONNECT(conn))) { virLibConnError(VIR_ERR_INVALID_CONN, _("no connection")); return -1; } virMutexLock(&conn->lock); VIR_DEBUG("unref connection %p %d", conn, conn->refs); conn->refs--; refs = conn->refs; if (refs == 0) { virReleaseConnect(conn); /* Already unlocked mutex */ return 0; } virMutexUnlock(&conn->lock); return refs; }
/** * virGetNodeDevice: * @conn: the hypervisor connection * @name: device name (unique on node) * * Lookup if the device is already registered for that connection, * if yes return a new pointer to it, if no allocate a new structure, * and register it in the table. In any case a corresponding call to * virUnrefNodeDevice() is needed to not leak data. * * Returns a pointer to the node device, or NULL in case of failure */ virNodeDevicePtr virGetNodeDevice(virConnectPtr conn, const char *name) { virNodeDevicePtr ret = NULL; if (!VIR_IS_CONNECT(conn)) { virLibConnError(VIR_ERR_INVALID_ARG, _("no connection")); return NULL; } if (name == NULL) { virLibConnError(VIR_ERR_INVALID_ARG, _("missing name")); return NULL; } virMutexLock(&conn->lock); if (VIR_ALLOC(ret) < 0) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } ret->magic = VIR_NODE_DEVICE_MAGIC; ret->conn = conn; ret->name = strdup(name); if (ret->name == NULL) { virMutexUnlock(&conn->lock); virReportOOMError(); goto error; } conn->refs++; ret->refs++; virMutexUnlock(&conn->lock); return(ret); error: if (ret != NULL) { VIR_FREE(ret->name); VIR_FREE(ret); } return(NULL); }
/** * virGetStorageVol: * @conn: the hypervisor connection * @pool: pool owning the volume * @name: pointer to the storage vol name * @key: pointer to unique key of the volume * @privateData: pointer to driver specific private data * @freeFunc: private data cleanup function pointer specfic to driver * * Lookup if the storage vol is already registered for that connection, * if yes return a new pointer to it, if no allocate a new structure, * and register it in the table. In any case a corresponding call to * virObjectUnref() is needed to not leak data. * * Returns a pointer to the storage vol, or NULL in case of failure */ virStorageVolPtr virGetStorageVol(virConnectPtr conn, const char *pool, const char *name, const char *key, void *privateData, virFreeCallback freeFunc) { virStorageVolPtr ret = NULL; if (virDataTypesInitialize() < 0) return NULL; if (!VIR_IS_CONNECT(conn)) { virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection")); return NULL; } virCheckNonNullArgReturn(pool, NULL); virCheckNonNullArgReturn(name, NULL); virCheckNonNullArgReturn(key, NULL); if (!(ret = virObjectNew(virStorageVolClass))) return NULL; if (VIR_STRDUP(ret->pool, pool) < 0 || VIR_STRDUP(ret->name, name) < 0 || VIR_STRDUP(ret->key, key) < 0) goto error; ret->conn = virObjectRef(conn); /* set driver specific data */ ret->privateData = privateData; ret->privateDataFreeFunc = freeFunc; return ret; error: virObjectUnref(ret); return NULL; }
/** * virGetStoragePool: * @conn: the hypervisor connection * @name: pointer to the storage pool name * @uuid: pointer to the uuid * @privateData: pointer to driver specific private data * @freeFunc: private data cleanup function pointer specfic to driver * * Lookup if the storage pool is already registered for that connection, * if yes return a new pointer to it, if no allocate a new structure, * and register it in the table. In any case a corresponding call to * virObjectUnref() is needed to not leak data. * * Returns a pointer to the storage pool, or NULL in case of failure */ virStoragePoolPtr virGetStoragePool(virConnectPtr conn, const char *name, const unsigned char *uuid, void *privateData, virFreeCallback freeFunc) { virStoragePoolPtr ret = NULL; if (virDataTypesInitialize() < 0) return NULL; if (!VIR_IS_CONNECT(conn)) { virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection")); return NULL; } virCheckNonNullArgReturn(name, NULL); virCheckNonNullArgReturn(uuid, NULL); if (!(ret = virObjectNew(virStoragePoolClass))) return NULL; if (VIR_STRDUP(ret->name, name) < 0) goto error; ret->conn = virObjectRef(conn); memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); /* set the driver specific data */ ret->privateData = privateData; ret->privateDataFreeFunc = freeFunc; return ret; error: virObjectUnref(ret); return NULL; }