From nobody Fri May 16 03:58:02 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501625597810642.7655032872307; Tue, 1 Aug 2017 15:13:17 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4C88C4482F4; Tue, 1 Aug 2017 22:13:15 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1381B784AB; Tue, 1 Aug 2017 22:13:15 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id A29FE180B467; Tue, 1 Aug 2017 22:13:14 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkeGS007962 for ; Tue, 1 Aug 2017 17:46:40 -0400 Received: by smtp.corp.redhat.com (Postfix) id C086C6375F; Tue, 1 Aug 2017 21:46:40 +0000 (UTC) Received: from mx1.redhat.com (ext-mx04.extmail.prod.ext.phx2.redhat.com [10.5.110.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AEB594DA95 for ; Tue, 1 Aug 2017 21:46:38 +0000 (UTC) Received: from mail-qt0-f181.google.com (mail-qt0-f181.google.com [209.85.216.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B24AB108D42 for ; Tue, 1 Aug 2017 21:46:23 +0000 (UTC) Received: by mail-qt0-f181.google.com with SMTP id 16so17124097qtz.4 for ; Tue, 01 Aug 2017 14:46:23 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.19 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:20 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 4C88C4482F4 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 4C88C4482F4 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="k/ixDg9C" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com B24AB108D42 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com B24AB108D42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=+Nu2+uE+29TdfOEf/KbNgasDm9W28j06NawHSFMjO7Q=; b=k/ixDg9CGJSgpgM2T25nqoFJF1Epno8Y0QxkjxLdh1BO63Ids8LIG3oVPseWH5ET4c xDG8Fn/tdi6md6hcuPN93xsRzNKOkLqCRBD5w9VboAY9KUU13bWGXSFSjR5rj69bS6oF 69dwwqa1RIhMdq7vyK5yLUkTHHbammFI8ub92zQxPcydfPPoKfsqmmOrwZ8X/2D6bI5I ggnOJuzHpQmg18i0HDBSMhIuOdQatyIZS2mvdFSrOA+9Xva6mVanvWyYwOjoOVebI6rz xE/4tJPWV/5pqgz1zn0wK0+1LhzX2xPHFkyMg/xs6FK1+7MsdADt3dcGxuwct+51izWF fwww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=+Nu2+uE+29TdfOEf/KbNgasDm9W28j06NawHSFMjO7Q=; b=hd3/smjOnhhaz22V6C1XpbI1Uwv1bOJnxObvNPdYssZ0jWsJTRcKQ48CpFVYHHYUaB +X6oHS3BVdsR/JDndGr3zX1PWJQxFLUokG5LshFHOcD+1tu/+TN2lfZ2O4/BybxnakdC pVH4Hx+UB56oOVPfBaOKqOQcbUU/lfj2lU01RYUYqlz5QBTRP+sW7nXWZncSd8CiA37X NCDTtaHf+AzKHLIekdmoWncsbB/N962QRKH+5fBPZ9OxqFsyxSq7U+XqPUfd9TVYr7+P KzmeE7ZRKCYlJTXZcyTIZPLCAOfkgVXH0rmmQfJEfp+dQdnFabkCIkeYhLzIEaYh5PJD 11YQ== X-Gm-Message-State: AIVw112pPk1ov+RzfDg1S8yPHtA6h+PzJUSI6xL3HEDNlEGQHUTI0ig2 H89QH89BEyMHhlOOVsM= X-Received: by 10.200.13.67 with SMTP id r3mr30650279qti.286.1501623981080; Tue, 01 Aug 2017 14:46:21 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:08 -0400 Message-Id: <20170801214614.8915-8-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 01 Aug 2017 21:46:24 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 01 Aug 2017 21:46:24 +0000 (UTC) for IP:'209.85.216.181' DOMAIN:'mail-qt0-f181.google.com' HELO:'mail-qt0-f181.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: -1.12 (DCC_REPUT_00_12, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.216.181 mail-qt0-f181.google.com 209.85.216.181 mail-qt0-f181.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.28 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 07/13] Split up the bindings for libvirt domain API X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 01 Aug 2017 22:13:16 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" --- src/Makefile.am | 3 +- src/libvirt-domain.c | 3339 ++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-domain.h | 208 +++ src/libvirt-php.c | 3789 +++-------------------------------------------= ---- src/libvirt-php.h | 119 +- 5 files changed, 3749 insertions(+), 3709 deletions(-) create mode 100644 src/libvirt-domain.c create mode 100644 src/libvirt-domain.h diff --git a/src/Makefile.am b/src/Makefile.am index ddfad41..b8eae3a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -24,7 +24,8 @@ libvirt_php_la_SOURCES =3D \ libvirt-php.c libvirt-php.h \ libvirt-connection.c libvirt-connection.h \ libvirt-node.c libvirt-node.h \ - libvirt-stream.c libvirt-stream.h + libvirt-stream.c libvirt-stream.h \ + libvirt-domain.c libvirt-domain.h libvirt_php_la_CFLAGS =3D \ $(AM_CFLAGS) \ -DCOMPILE_DL_LIBVIRT=3D1 diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c new file mode 100644 index 0000000..7d8587e --- /dev/null +++ b/src/libvirt-domain.c @@ -0,0 +1,3339 @@ +/* + * libvirt-domain.c: The PHP bindings to libvirt domain API + * + * See COPYING for the license of this software + */ + +#include +#include =20 +#include + +#include "libvirt-domain.h" +#include "sockets.h" +#include "vncfunc.h" + +DEBUG_INIT("domain"); + +void +php_libvirt_domain_dtor(virt_resource *rsrc TSRMLS_DC) +{ + php_libvirt_domain *domain =3D (php_libvirt_domain *)rsrc->ptr; + int rv =3D 0; + + if (domain !=3D NULL) { + if (domain->domain !=3D NULL) { + if (!check_resource_allocation(domain->conn->conn, INT_RESOURC= E_DOMAIN, domain->domain TSRMLS_CC)) { + domain->domain =3D NULL; + efree(domain); + return; + } + + rv =3D virDomainFree(domain->domain); + if (rv !=3D 0) { + DPRINTF("%s: virDomainFree(%p) returned %d (%s)\n", __FUNC= TION__, domain->domain, rv, LIBVIRT_G(last_error)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainFree= failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virDomainFree(%p) completed successfully\n", = __FUNCTION__, domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, domain->conn-= >conn, domain->domain, 0 TSRMLS_CC); + } + domain->domain =3D NULL; + } + efree(domain); + } +} + +/* + * Function name: libvirt_domain_new + * Since version: 0.4.5 + * Description: Function is used to install a new virtual machine to t= he machine + * Arguments: @conn [resource]: libvirt connection resource + * @name [string]: name of the new domain + * @arch [string]: optional architecture string, can be N= ULL to get default (or false) + * @memMB [int]: number of megabytes of RAM to be allocat= ed for domain + * @maxmemMB [int]: maximum number of megabytes of RAM to= be allocated for domain + * @vcpus [int]: number of VCPUs to be allocated to domain + * @iso_image [string]: installation ISO image for domain + * @disks [array]: array of disk devices for domain, cons= ist of keys as 'path' (storage location), 'driver' (image type, e.g. 'raw' = or 'qcow2'), 'bus' (e.g. 'ide', 'scsi'), 'dev' (device to be presented to t= he guest - e.g. 'hda'), 'size' (with 'M' or 'G' suffixes, like '10G' for 10= gigabytes image etc.) and 'flags' (VIR_DOMAIN_DISK_FILE or VIR_DOMAIN_DISK= _BLOCK, optionally VIR_DOMAIN_DISK_ACCESS_ALL to allow access to the disk f= or all users on the host system) + * @networks [array]: array of network devices for domain= , consists of keys as 'mac' (for MAC address), 'network' (for network name)= and optional 'model' for model of NIC device + * @flags [int]: bit array of flags + * Returns: a new domain resource + */ +PHP_FUNCTION(libvirt_domain_new) +{ + php_libvirt_connection *conn =3D NULL; + php_libvirt_domain *res_domain =3D NULL; + virDomainPtr domain2 =3D NULL; + virDomainPtr domain =3D NULL; + zval *zconn; + char *arch =3D NULL; + strsize_t arch_len; + char *tmp; + char *name; + strsize_t name_len =3D 0; + // char *emulator; + char *iso_image =3D NULL; + strsize_t iso_image_len; + zend_long vcpus =3D -1; + zend_long memMB =3D -1; + zval *disks, *networks; + tVMDisk *vmDisks =3D NULL; + tVMNetwork *vmNetworks =3D NULL; + zend_long maxmemMB =3D -1; + HashTable *arr_hash; + int numDisks, numNets, i; + zval *data; + HashPosition pointer; + char vncl[2048] =3D { 0 }; + char tmpname[1024] =3D { 0 }; + char *xml =3D NULL; + int retval =3D 0; + char *uuid =3D NULL; + zend_long flags =3D 0; + int fd =3D -1; + + GET_CONNECTION_FROM_ARGS("rsslllsaa|l", &zconn, &name, &name_len, &arc= h, &arch_len, &memMB, &maxmemMB, &vcpus, &iso_image, &iso_image_len, &disks= , &networks, &flags); + + if (iso_image =3D=3D NULL) { + DPRINTF("%s: Iso image is not defined\n", PHPFUNC); + RETURN_FALSE; + } + + if ((arch =3D=3D NULL) || (arch_len =3D=3D 0)) + arch =3D NULL; + + //DPRINTF("%s: name: %s, arch: %s, memMB: %d, maxmemMB: %d, vcpus: %d,= iso_image: %s\n", PHPFUNC, name, arch, memMB, maxmemMB, vcpus, iso_image); + if (memMB =3D=3D 0) + memMB =3D maxmemMB; + + /* Parse all disks from array */ + arr_hash =3D Z_ARRVAL_P(disks); + numDisks =3D zend_hash_num_elements(arr_hash); + vmDisks =3D (tVMDisk *)malloc(numDisks * sizeof(tVMDisk)); + memset(vmDisks, 0, numDisks * sizeof(tVMDisk)); + i =3D 0; + VIRT_FOREACH(arr_hash, pointer, data) { + if (Z_TYPE_P(data) =3D=3D IS_ARRAY) { + tVMDisk disk; + parse_array(data, &disk, NULL); + if (disk.path !=3D NULL) { + //DPRINTF("Disk =3D> path =3D '%s', driver =3D '%s', bus = =3D '%s', dev =3D '%s', size =3D %ld MB, flags =3D %d\n", + // disk.path, disk.driver, disk.bus, disk.dev, disk.size,= disk.flags); + vmDisks[i++] =3D disk; + } + } + } VIRT_FOREACH_END(); + numDisks =3D i; + + /* Parse all networks from array */ + arr_hash =3D Z_ARRVAL_P(networks); + numNets =3D zend_hash_num_elements(arr_hash); + vmNetworks =3D (tVMNetwork *)malloc(numNets * sizeof(tVMNetwork)); + memset(vmNetworks, 0, numNets * sizeof(tVMNetwork)); + i =3D 0; + VIRT_FOREACH(arr_hash, pointer, data) { + if (Z_TYPE_P(data) =3D=3D IS_ARRAY) { + tVMNetwork network; + parse_array(data, NULL, &network); + if (network.mac !=3D NULL) { + //DPRINTF("Network =3D> mac =3D '%s', network =3D '%s', mo= del =3D '%s'\n", network.mac, network.network, network.model); + vmNetworks[i++] =3D network; + } + } + } VIRT_FOREACH_END(); + numNets =3D i; + + snprintf(tmpname, sizeof(tmpname), "%s-install", name); + DPRINTF("%s: Name is '%s', memMB is %d, maxmemMB is %d\n", PHPFUNC, tm= pname, (int) memMB, (int) maxmemMB); + tmp =3D installation_get_xml(1, + conn->conn, tmpname, memMB, maxmemMB, NULL = /* arch */, NULL, vcpus, iso_image, + vmDisks, numDisks, vmNetworks, numNets, + flags TSRMLS_CC); + if (tmp =3D=3D NULL) { + DPRINTF("%s: Cannot get installation XML\n", PHPFUNC); + set_error("Cannot get installation XML" TSRMLS_CC); + RETURN_FALSE; + } + + domain =3D virDomainCreateXML(conn->conn, tmp, 0); + if (domain =3D=3D NULL) { + set_error_if_unset("Cannot create installation domain from the XML= description" TSRMLS_CC); + DPRINTF("%s: Cannot create installation domain from the XML descri= ption (%s): %s\n", PHPFUNC, LIBVIRT_G(last_error), tmp); + RETURN_FALSE; + } + + xml =3D virDomainGetXMLDesc(domain, 0); + if (!xml) { + DPRINTF("%s: Cannot get the XML description\n", PHPFUNC); + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics[@type=3D= 'vnc']/@port", NULL, &retval); + if (retval < 0) { + DPRINTF("%s: Cannot get port from XML description\n", PHPFUNC); + set_error_if_unset("Cannot get port from XML description" TSRMLS_C= C); + RETURN_FALSE; + } + + snprintf(vncl, sizeof(vncl), "%s:%s", virConnectGetHostname(conn->conn= ), tmp); + DPRINTF("%s: Trying to connect to '%s'\n", PHPFUNC, vncl); + +#ifndef EXTWIN + if ((fd =3D connect_socket(virConnectGetHostname(conn->conn), tmp, 0, = 0, flags & DOMAIN_FLAG_TEST_LOCAL_VNC)) < 0) { + DPRINTF("%s: Cannot connect to '%s'\n", PHPFUNC, vncl); + snprintf(vncl, sizeof(vncl), "Connection failed, port %s is most l= ikely forbidden on firewall (iptables) on the host (%s)" + " or the emulator VNC server is bound to localhost addres= s only.", + tmp, virConnectGetHostname(conn->conn)); + } else { + close(fd); + DPRINTF("%s: Connection to '%s' successfull (%s local connection)\= n", PHPFUNC, vncl, + (flags & DOMAIN_FLAG_TEST_LOCAL_VNC) ? "using" : "not usin= g"); + } +#endif + + set_vnc_location(vncl TSRMLS_CC); + + tmp =3D installation_get_xml(2, + conn->conn, name, memMB, maxmemMB, NULL /* = arch */, NULL, vcpus, iso_image, + vmDisks, numDisks, vmNetworks, numNets, + flags TSRMLS_CC); + if (tmp =3D=3D NULL) { + DPRINTF("%s: Cannot get installation XML, step 2\n", PHPFUNC); + set_error("Cannot get installation XML, step 2" TSRMLS_CC); + virDomainFree(domain); + RETURN_FALSE; + } + + domain2 =3D virDomainDefineXML(conn->conn, tmp); + if (domain2 =3D=3D NULL) { + set_error_if_unset("Cannot define domain from the XML description"= TSRMLS_CC); + DPRINTF("%s: Cannot define domain from the XML description (name = =3D '%s', uuid =3D '%s', error =3D '%s')\n", PHPFUNC, name, uuid, LIBVIRT_G= (last_error)); + RETURN_FALSE; + } + virDomainFree(domain2); + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D domain; + res_domain->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_new_get_vnc + * Since version: 0.4.5 + * Description: Function is used to get the VNC server location for th= e newly created domain (newly started installation) + * Arguments: None + * Returns: a VNC server for a newly created domain resource (if a= ny) + */ +PHP_FUNCTION(libvirt_domain_new_get_vnc) +{ + if (LIBVIRT_G(vnc_location)) + VIRT_RETURN_STRING(LIBVIRT_G(vnc_location)); + + RETURN_NULL(); +} + +/* + * Function name: libvirt_domain_get_counts + * Since version: 0.4.1(-1) + * Description: Function is getting domain counts for all, active and = inactive domains + * Arguments: @conn [resource]: libvirt connection resource from lib= virt_connect() + * Returns: array of total, active and inactive (but defined) doma= in counts + */ +PHP_FUNCTION(libvirt_domain_get_counts) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count_defined; + int count_active; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + count_defined =3D virConnectNumOfDefinedDomains(conn->conn); + count_active =3D virConnectNumOfDomains(conn->conn); + + array_init(return_value); + add_assoc_long(return_value, "total", (long)(count_defined + count_act= ive)); + add_assoc_long(return_value, "active", (long)count_active); + add_assoc_long(return_value, "inactive", (long)count_defined); +} + +/* + * Function name: libvirt_domain_is_persistent + * Since version: 0.4.9 + * Description: Function to get information whether domain is persiste= nt or not + * Arguments: @res [resource]: libvirt domain resource + * Returns: TRUE for persistent, FALSE for not persistent, -1 on e= rror + */ +PHP_FUNCTION(libvirt_domain_is_persistent) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int p; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + if ((p =3D virDomainIsPersistent(domain->domain)) < 0) + RETURN_LONG(-1); + + if (p =3D=3D 1) + RETURN_TRUE; + + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_lookup_by_name + * Since version: 0.4.1(-1) + * Description: Function is used to lookup for domain by it's name + * Arguments: @res [resource]: libvirt connection resource from libv= irt_connect() + * @name [string]: domain name to look for + * Returns: libvirt domain resource + */ +PHP_FUNCTION(libvirt_domain_lookup_by_name) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + strsize_t name_len; + char *name =3D NULL; + virDomainPtr domain =3D NULL; + php_libvirt_domain *res_domain; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); + if ((name =3D=3D NULL) || (name_len < 1)) + RETURN_FALSE; + domain =3D virDomainLookupByName(conn->conn, name); + if (domain =3D=3D NULL) + RETURN_FALSE; + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D domain; + res_domain->conn =3D conn; + + DPRINTF("%s: domain name =3D '%s', returning %p\n", PHPFUNC, name, res= _domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_get_xml_desc + * Since version: 0.4.1(-1), changed 0.4.2 + * Description: Function is used to get the domain's XML description + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL + * Returns: domain XML description string or result of xPath expre= ssion + */ +PHP_FUNCTION(libvirt_domain_get_xml_desc) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *tmp =3D NULL; + char *xml; + char *xpath =3D NULL; + strsize_t xpath_len; + zend_long flags =3D 0; + int retval =3D -1; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &xpath, &xpath_len, &flags); + if (xpath_len < 1) + xpath =3D NULL; + + DPRINTF("%s: Getting the XML for domain %p (xPath =3D %s)\n", PHPFUNC,= domain->domain, xpath); + + xml =3D virDomainGetXMLDesc(domain->domain, flags); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + VIRT_RETVAL_STRING(xml); + } else { + VIRT_RETVAL_STRING(tmp); + } + + free(tmp); + free(xml); +} + +/* + * Function name: libvirt_domain_get_disk_devices + * Since version: 0.4.4 + * Description: Function is used to get disk devices for the domain + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: list of domain disk devices + */ +PHP_FUNCTION(libvirt_domain_get_disk_devices) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *xml; + int retval =3D -1; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + DPRINTF("%s: Getting disk device list for domain %p\n", PHPFUNC, domai= n->domain); + + xml =3D virDomainGetXMLDesc(domain->domain, 0); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + array_init(return_value); + + free(get_string_from_xpath(xml, "//domain/devices/disk/target/@dev", &= return_value, &retval)); + free(xml); + + if (retval < 0) + add_assoc_long(return_value, "error_code", (long)retval); + else + add_assoc_long(return_value, "num", (long)retval); +} + +/* + * Function name: libvirt_domain_get_interface_devices + * Since version: 0.4.4 + * Description: Function is used to get network interface devices for = the domain + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: list of domain interface devices + */ +PHP_FUNCTION(libvirt_domain_get_interface_devices) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *xml; + int retval =3D -1; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + DPRINTF("%s: Getting interface device list for domain %p\n", PHPFUNC, = domain->domain); + + xml =3D virDomainGetXMLDesc(domain->domain, 0); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + array_init(return_value); + + free(get_string_from_xpath(xml, "//domain/devices/interface/target/@de= v", &return_value, &retval)); + free(xml); + + if (retval < 0) + add_assoc_long(return_value, "error_code", (long)retval); + else + add_assoc_long(return_value, "num", (long)retval); +} + +/* + * Function name: libvirt_domain_change_vcpus + * Since version: 0.4.2 + * Description: Function is used to change the VCPU count for the doma= in + * Arguments: @res [resource]: libvirt domain resource + * @numCpus [int]: number of VCPUs to be set for the guest + * @flags [int]: flags for virDomainSetVcpusFlags (availa= ble at http://libvirt.org/html/libvirt-libvirt.html#virDomainVcpuFlags) + * Returns: true on success, false on error + */ +PHP_FUNCTION(libvirt_domain_change_vcpus) +{ + zend_long numCpus, flags =3D 0; + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + + GET_DOMAIN_FROM_ARGS("rl|l", &zdomain, &numCpus, &flags); + + if (virDomainSetVcpusFlags(domain->domain, numCpus, flags) =3D=3D 0) { + RETURN_TRUE; + } else { + RETURN_FALSE; + } +} + +/* + * Function name: libvirt_domain_change_memory + * Since version: 0.4.2 + * Description: Function is used to change the domain memory allocation + * Arguments: @res [resource]: libvirt domain resource + * @allocMem [int]: number of MiBs to be set as immediate= memory value + * @allocMax [int]: number of MiBs to be set as the maxim= um allocation + * @flags [int]: flags + * Returns: new domain resource + */ +PHP_FUNCTION(libvirt_domain_change_memory) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *tmpA =3D NULL; + char *tmp1 =3D NULL; + char *tmp2 =3D NULL; + char *xml; + char *new_xml =3D NULL; + int new_len; + char *newXml =3D NULL; + zend_long xflags =3D 0; + zend_long allocMem =3D 0; + zend_long allocMax =3D 0; + // int pos =3D -1; + int len =3D 0; + php_libvirt_domain *res_domain =3D NULL; + php_libvirt_connection *conn =3D NULL; + virDomainPtr dom =3D NULL; + + GET_DOMAIN_FROM_ARGS("rll|l", &zdomain, &allocMem, &allocMax, &xflags); + + DPRINTF("%s: Changing domain memory count to %d MiB current/%d MiB max= , domain =3D %p\n", + PHPFUNC, (int)allocMem, (int)allocMax, domain->domain); + + allocMem *=3D 1024; + allocMax *=3D 1024; + + if (allocMem > allocMax) + allocMem =3D allocMax; + + xml =3D virDomainGetXMLDesc(domain->domain, xflags); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + snprintf(newXml, sizeof(newXml), " %d\n %d\n", allocMax, allocMem); + tmpA =3D strstr(xml, ""); + tmp1 =3D strstr(xml, "") + strlen(""); + // pos =3D strlen(xml) - strlen(tmp1); + len =3D strlen(xml) - strlen(tmpA); + + tmp2 =3D (char *)emalloc((len + 1) * sizeof(char)); + memset(tmp2, 0, len + 1); + memcpy(tmp2, xml, len); + + new_len =3D strlen(tmp1) + strlen(tmp2) + strlen(newXml) + 2; + new_xml =3D (char *)emalloc(new_len * sizeof(char)); + snprintf(new_xml, new_len, "%s\n%s%s", tmp2, newXml, tmp1); + + conn =3D domain->conn; + + dom =3D virDomainDefineXML(conn->conn, new_xml); + if (dom =3D=3D NULL) { + free(xml); + efree(new_xml); + RETURN_FALSE; + } + free(xml); + efree(new_xml); + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D dom; + res_domain->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_change_boot_devices + * Since version: 0.4.2 + * Description: Function is used to change the domain boot devices + * Arguments: @res [resource]: libvirt domain resource + * @first [string]: first boot device to be set + * @second [string]: second boot device to be set + * @flags [int]: flags + * Returns: new domain resource + */ +PHP_FUNCTION(libvirt_domain_change_boot_devices) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *tmpA =3D NULL; + char *tmp1 =3D NULL; + char *tmp2 =3D NULL; + char *xml; + char *new_xml =3D NULL; + int new_len; + char newXml[4096] =3D { 0 }; + zend_long xflags =3D 0; + char *first =3D NULL; + strsize_t first_len; + char *second =3D NULL; + strsize_t second_len; + // int pos =3D -1; + int len =3D 0; + php_libvirt_domain *res_domain =3D NULL; + php_libvirt_connection *conn =3D NULL; + virDomainPtr dom =3D NULL; + + GET_DOMAIN_FROM_ARGS("rss|l", &zdomain, &first, &first_len, &second, &= second_len, &xflags); + + xml =3D virDomainGetXMLDesc(domain->domain, xflags); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + DPRINTF("%s: Changing domain boot order, domain =3D %p\n", PHPFUNC, do= main->domain); + + if (!second || (strcmp(second, "-") =3D=3D 0)) + snprintf(newXml, sizeof(newXml), " \n", first= ); + else + snprintf(newXml, sizeof(newXml), " \n \n", first, second); + + tmpA =3D strstr(xml, "") + strlen(""); + tmp1 =3D strstr(xml, ""); + // pos =3D strlen(xml) - strlen(tmp1); + len =3D strlen(xml) - strlen(tmpA); + + tmp2 =3D (char *)emalloc((len + 1) * sizeof(char)); + memset(tmp2, 0, len + 1); + memcpy(tmp2, xml, len); + + new_len =3D strlen(tmp1) + strlen(tmp2) + strlen(newXml) + 2; + new_xml =3D (char *)emalloc(new_len * sizeof(char)); + snprintf(new_xml, new_len, "%s\n%s%s", tmp2, newXml, tmp1); + + conn =3D domain->conn; + + dom =3D virDomainDefineXML(conn->conn, new_xml); + if (dom =3D=3D NULL) { + DPRINTF("%s: Function failed, restoring original XML\n", PHPFUNC); + free(xml); + efree(newXml); + RETURN_FALSE; + } + free(xml); + efree(newXml); + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D dom; + res_domain->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_disk_add + * Since version: 0.4.2 + * Description: Function is used to add the disk to the virtual machin= e using set of API functions to make it as simple as possible for the user + * Arguments: @res [resource]: libvirt domain resource + * @img [string]: string for the image file on the host s= ystem + * @dev [string]: string for the device to be presented t= o the guest (e.g. hda) + * @typ [string]: bus type for the device in the guest, u= sually 'ide' or 'scsi' + * @driver [string]: driver type to be specified, like 'r= aw' or 'qcow2' + * @flags [int]: flags for getting the XML description + * Returns: new domain resource + */ +PHP_FUNCTION(libvirt_domain_disk_add) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *xml; + char *img =3D NULL; + strsize_t img_len; + char *dev =3D NULL; + strsize_t dev_len; + char *driver =3D NULL; + strsize_t driver_len; + char *typ =3D NULL; + strsize_t typ_len; + char *newXml =3D NULL; + zend_long xflags =3D 0; + int retval =3D -1; + char *xpath =3D NULL; + char *tmp =3D NULL; + + GET_DOMAIN_FROM_ARGS("rssss|l", &zdomain, &img, &img_len, &dev, &dev_l= en, &typ, &typ_len, &driver, &driver_len, &xflags); + + DPRINTF("%s: Domain %p, device =3D %s, image =3D %s, type =3D %s, driv= er =3D %s\n", PHPFUNC, + domain->domain, dev, img, typ, driver); + + xml =3D virDomainGetXMLDesc(domain->domain, xflags); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + goto error; + } + + if (asprintf(&xpath, "//domain/devices/disk/source[@file=3D\"%s\"]/./@= file", img) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (tmp !=3D NULL) { + free(tmp); + if (asprintf(&tmp, "Domain already has image %s connected",= img) < 0) + set_error("Out of memory" TSRMLS_CC); + else + set_error(tmp TSRMLS_CC); + goto error; + } + + free(xpath); + if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/./@dev= ", dev) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, newXml, NULL, &retval); + if (tmp !=3D NULL) { + free(tmp); + if (asprintf(&tmp, "Domain already has device %s connected"= , dev) < 0) + set_error("Out of memory" TSRMLS_CC); + else + set_error(tmp TSRMLS_CC); + goto error; + } + + if (asprintf(&newXml, + " \n" + " \n" + " \n" + " \n" + " ", driver, img, dev, typ) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + + if (virDomainAttachDeviceFlags(domain->domain, + newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { + set_error("Unable to attach disk" TSRMLS_CC); + goto error; + } + + free(tmp); + free(xpath); + free(xml); + RETURN_TRUE; + + error: + free(tmp); + free(xpath); + free(xml); + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_disk_remove + * Since version: 0.4.2 + * Description: Function is used to remove the disk from the virtual m= achine using set of API functions to make it as simple as possible + * Arguments: @res [resource]: libvirt domain resource + * @dev [string]: string for the device to be removed fro= m the guest (e.g. 'hdb') + * @flags [int]: flags for getting the XML description + * Returns: new domain resource + */ +PHP_FUNCTION(libvirt_domain_disk_remove) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *xml; + char *dev =3D NULL; + strsize_t dev_len; + char *newXml =3D NULL; + zend_long xflags =3D 0; + int retval =3D -1; + char *xpath =3D NULL; + char *tmp =3D NULL; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &dev, &dev_len, &xflags); + + DPRINTF("%s: Trying to remove %s from domain %p\n", PHPFUNC, dev, doma= in->domain); + + xml =3D virDomainGetXMLDesc(domain->domain, xflags); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/./@dev= ", dev) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (!tmp) { + if (asprintf(&tmp, "Device %s is not connected to the guest= ", dev) < 0) + set_error("Out of memory" TSRMLS_CC); + else + set_error(tmp TSRMLS_CC); + goto error; + } + + if (asprintf(&newXml, + " \n" + " \n" + " ", dev) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + + if (virDomainDetachDeviceFlags(domain->domain, + newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { + set_error("Unable to attach disk" TSRMLS_CC); + goto error; + } + + free(tmp); + free(xpath); + free(xml); + RETURN_TRUE; + + error: + free(tmp); + free(xpath); + free(xml); + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_nic_add + * Since version: 0.4.2 + * Description: Function is used to add the NIC card to the virtual ma= chine using set of API functions to make it as simple as possible for the u= ser + * Arguments: @res [resource]: libvirt domain resource + * @mac [string]: MAC string interpretation to be used fo= r the NIC device + * @network [string]: network name where to connect this = NIC + * @model [string]: string of the NIC model + * @flags [int]: flags for getting the XML description + * Returns: new domain resource + */ +PHP_FUNCTION(libvirt_domain_nic_add) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *xml; + char *mac =3D NULL; + strsize_t mac_len; + char *net =3D NULL; + strsize_t net_len; + char *model =3D NULL; + strsize_t model_len; + char *newXml =3D NULL; + zend_long xflags =3D 0; + int retval =3D -1; + char *xpath =3D NULL; + char *tmp =3D NULL; + + DPRINTF("%s: Entering\n", PHPFUNC); + + GET_DOMAIN_FROM_ARGS("rsss|l", &zdomain, &mac, &mac_len, &net, &net_le= n, &model, &model_len, &xflags); + if (model_len < 1) + model =3D NULL; + + DPRINTF("%s: domain =3D %p, mac =3D %s, net =3D %s, model =3D %s\n", P= HPFUNC, domain->domain, mac, net, model); + + xml =3D virDomainGetXMLDesc(domain->domain, xflags); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/./@mac", mac) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (tmp) { + free(tmp); + if (asprintf(&tmp, "Domain already has NIC device with MAC address= %s connected", mac) < 0) + set_error("Out of memory" TSRMLS_CC); + else + set_error(tmp TSRMLS_CC); + goto error; + } + + if (model) { + if (asprintf(&newXml, + " \n" + " \n" + " \n" + " \n" + " ", mac, net, model) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + } else { + if (asprintf(&newXml, + " \n" + " \n" + " \n" + " ", mac, net) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + } + + if (virDomainAttachDeviceFlags(domain->domain, + newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { + set_error("Unable to attach interface" TSRMLS_CC); + goto error; + } + + free(tmp); + free(xpath); + free(xml); + RETURN_TRUE; + + error: + free(tmp); + free(xpath); + free(xml); + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_nic_remove + * Since version: 0.4.2 + * Description: Function is used to remove the NIC from the virtual ma= chine using set of API functions to make it as simple as possible + * Arguments: @res [resource]: libvirt domain resource + * @dev [string]: string representation of the IP address= to be removed (e.g. 54:52:00:xx:yy:zz) + * @flags [int]: optional flags for getting the XML descr= iption + * Returns: new domain resource + */ +PHP_FUNCTION(libvirt_domain_nic_remove) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *xml; + char *mac =3D NULL; + strsize_t mac_len; + char *newXml =3D NULL; + zend_long xflags =3D 0; + int retval =3D -1; + char *xpath =3D NULL; + char *tmp =3D NULL; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &mac, &mac_len, &xflags); + + DPRINTF("%s: Trying to remove NIC device with MAC address %s from doma= in %p\n", PHPFUNC, mac, domain->domain); + + xml =3D virDomainGetXMLDesc(domain->domain, xflags); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/./@mac", mac) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (!tmp) { + free(tmp); + if (asprintf(&tmp, "Domain has no such interface with mac %s", mac= ) < 0) + set_error("Out of memory" TSRMLS_CC); + else + set_error(tmp TSRMLS_CC); + goto error; + } + + if (asprintf(&newXml, + " \n" + " \n" + " ", mac) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + + if (virDomainDetachDeviceFlags(domain->domain, + newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { + set_error("Unable to detach interface" TSRMLS_CC); + goto error; + } + + free(tmp); + free(xpath); + free(xml); + RETURN_TRUE; + + error: + free(tmp); + free(xpath); + free(xml); + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_attach_device + * Since version: 0.5.3 + * Description: Function is used to attach a virtual device to a domai= n. + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @xml [string]: XML description of one device. + * @flags [int]: optional flags to control how the device= is attached. Defaults to VIR_DOMAIN_AFFECT_LIVE + * Returns: TRUE for success, FALSE on error. + */ +PHP_FUNCTION(libvirt_domain_attach_device) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain =3D NULL; + char *xml =3D NULL; + strsize_t xml_len =3D 0; + zend_long flags =3D VIR_DOMAIN_AFFECT_LIVE; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &xml, &xml_len, &flags); + + if (virDomainAttachDeviceFlags(domain->domain, xml, flags) < 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_detach_device + * Since version: 0.5.3 + * Description: Function is used to detach a virtual device from a dom= ain. + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @xml [string]: XML description of one device. + * @flags [int]: optional flags to control how the device= is attached. Defaults to VIR_DOMAIN_AFFECT_LIVE + * Returns: TRUE for success, FALSE on error. + */ +PHP_FUNCTION(libvirt_domain_detach_device) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain =3D NULL; + char *xml =3D NULL; + strsize_t xml_len =3D 0; + zend_long flags =3D VIR_DOMAIN_AFFECT_LIVE; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &xml, &xml_len, &flags); + + if (virDomainDetachDeviceFlags(domain->domain, xml, flags) < 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_get_info + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's information + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: domain information array + */ +PHP_FUNCTION(libvirt_domain_get_info) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + virDomainInfo domainInfo; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainGetInfo(domain->domain, &domainInfo); + DPRINTF("%s: virDomainGetInfo(%p) returned %d\n", PHPFUNC, domain->dom= ain, retval); + if (retval !=3D 0) + RETURN_FALSE; + + array_init(return_value); + add_assoc_long(return_value, "maxMem", domainInfo.maxMem); + add_assoc_long(return_value, "memory", domainInfo.memory); + add_assoc_long(return_value, "state", (long)domainInfo.state); + add_assoc_long(return_value, "nrVirtCpu", domainInfo.nrVirtCpu); + add_assoc_double(return_value, "cpuUsed", (double)((double)domainInfo.= cpuTime/1000000000.0)); +} + +/* + * Function name: libvirt_domain_get_name + * Since version: 0.4.1(-1) + * Description: Function is used to get domain name from it's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: domain name string + */ +PHP_FUNCTION(libvirt_domain_get_name) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + const char *name =3D NULL; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + if (domain->domain =3D=3D NULL) + RETURN_FALSE; + + name =3D virDomainGetName(domain->domain); + DPRINTF("%s: virDomainGetName(%p) returned %s\n", PHPFUNC, domain->dom= ain, name); + if (name =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETURN_STRING(name); //we can use the copy mechanism as we need = not to free name (we even can not!) +} + +/* + * Function name: libvirt_domain_get_uuid + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's UUID in binary fo= rmat + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: domain UUID in binary format + */ +PHP_FUNCTION(libvirt_domain_get_uuid) +{ + + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *uuid; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + uuid =3D (char *)emalloc(VIR_UUID_BUFLEN); + retval =3D virDomainGetUUID(domain->domain, (unsigned char *)uuid); + DPRINTF("%s: virDomainGetUUID(%p, %p) returned %d\n", PHPFUNC, domain-= >domain, uuid, retval); + if (retval !=3D 0) + RETURN_FALSE; + + VIRT_RETVAL_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_domain_get_uuid_string + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's UUID in string fo= rmat + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: domain UUID string + */ +PHP_FUNCTION(libvirt_domain_get_uuid_string) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *uuid; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + uuid =3D (char *)emalloc(VIR_UUID_STRING_BUFLEN); + retval =3D virDomainGetUUIDString(domain->domain, uuid); + DPRINTF("%s: virDomainGetUUIDString(%p) returned %d (%s)\n", PHPFUNC, = domain->domain, retval, uuid); + if (retval !=3D 0) + RETURN_FALSE; + + VIRT_RETVAL_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_domain_get_id + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's ID, applicable to= running guests only + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: running domain ID or -1 if not running + */ +PHP_FUNCTION(libvirt_domain_get_id) +{ + + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainGetID(domain->domain); + DPRINTF("%s: virDomainGetID(%p) returned %d\n", PHPFUNC, domain->domai= n, retval); + + RETURN_LONG(retval); +} + +/* + * Function name: libvirt_domain_lookup_by_uuid + * Since version: 0.4.1(-1) + * Description: Function is used to lookup for domain by it's UUID in = the binary format + * Arguments: @res [resource]: libvirt connection resource from libv= irt_connect() + * @uuid [string]: binary defined UUID to look for + * Returns: libvirt domain resource + */ +PHP_FUNCTION(libvirt_domain_lookup_by_uuid) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + strsize_t uuid_len; + unsigned char *uuid =3D NULL; + virDomainPtr domain =3D NULL; + php_libvirt_domain *res_domain; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); + + if ((uuid =3D=3D NULL) || (uuid_len < 1)) + RETURN_FALSE; + domain =3D virDomainLookupByUUID(conn->conn, uuid); + if (domain =3D=3D NULL) + RETURN_FALSE; + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D domain; + res_domain->conn =3D conn; + + DPRINTF("%s: domain UUID =3D '%s', returning %p\n", PHPFUNC, uuid, res= _domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_lookup_by_uuid_string + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain by it's UUID that's= accepted in string format + * Arguments: @res [resource]: libvirt connection resource from libv= irt_connect() + * @uuid [string]: domain UUID [in string format] to look= for + * Returns: libvirt domain resource + */ +PHP_FUNCTION(libvirt_domain_lookup_by_uuid_string) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + strsize_t uuid_len; + char *uuid =3D NULL; + virDomainPtr domain =3D NULL; + php_libvirt_domain *res_domain; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); + + if ((uuid =3D=3D NULL) || (uuid_len < 1)) + RETURN_FALSE; + domain =3D virDomainLookupByUUIDString(conn->conn, uuid); + if (domain =3D=3D NULL) + RETURN_FALSE; + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D domain; + res_domain->conn =3D conn; + + DPRINTF("%s: domain UUID string =3D '%s', returning %p\n", PHPFUNC, uu= id, res_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_lookup_by_id + * Since version: 0.4.1(-1) + * Description: Function is used to get domain by it's ID, applicable = only to running guests + * Arguments: @conn [resource]: libvirt connection resource from lib= virt_connect() + * @id [string]: domain id to look for + * Returns: libvirt domain resource + */ +PHP_FUNCTION(libvirt_domain_lookup_by_id) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + zend_long id; + virDomainPtr domain =3D NULL; + php_libvirt_domain *res_domain; + + GET_CONNECTION_FROM_ARGS("rl", &zconn, &id); + + domain =3D virDomainLookupByID(conn->conn, (int)id); + if (domain =3D=3D NULL) + RETURN_FALSE; + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D domain; + res_domain->conn =3D conn; + + DPRINTF("%s: domain id =3D '%d', returning %p\n", PHPFUNC, (int)id, re= s_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_create + * Since version: 0.4.1(-1) + * Description: Function is used to create the domain identified by it= 's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: result of domain creation (startup) + */ +PHP_FUNCTION(libvirt_domain_create) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainCreate(domain->domain); + DPRINTF("%s: virDomainCreate(%p) returned %d\n", PHPFUNC, domain->doma= in, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_destroy + * Since version: 0.4.1(-1) + * Description: Function is used to destroy the domain identified by i= t's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: result of domain destroy + */ +PHP_FUNCTION(libvirt_domain_destroy) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainDestroy(domain->domain); + DPRINTF("%s: virDomainDestroy(%p) returned %d\n", PHPFUNC, domain->dom= ain, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_resume + * Since version: 0.4.1(-1) + * Description: Function is used to resume the domain identified by it= 's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: result of domain resume + */ +PHP_FUNCTION(libvirt_domain_resume) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainResume(domain->domain); + DPRINTF("%s: virDomainResume(%p) returned %d\n", PHPFUNC, domain->doma= in, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_core_dump + * Since version: 0.4.1(-2) + * Description: Function is used to dump core of the domain identified= by it's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @to [string]: to + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_core_dump) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + strsize_t to_len; + char *to; + + GET_DOMAIN_FROM_ARGS("rs", &zdomain, &to, &to_len); + + retval =3D virDomainCoreDump(domain->domain, to, 0); + DPRINTF("%s: virDomainCoreDump(%p, %s, 0) returned %d\n", PHPFUNC, dom= ain->domain, to, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_shutdown + * Since version: 0.4.1(-1) + * Description: Function is used to shutdown the domain identified by = it's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_shutdown) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainShutdown(domain->domain); + DPRINTF("%s: virDomainShutdown(%p) returned %d\n", PHPFUNC, domain->do= main, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_suspend + * Since version: 0.4.1(-1) + * Description: Function is used to suspend the domain identified by i= t's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_suspend) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainSuspend(domain->domain); + DPRINTF("%s: virDomainSuspend(%p) returned %d\n", PHPFUNC, domain->dom= ain, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_managedsave + * Since version: 0.4.1(-1) + * Description: Function is used to managed save the domain (domain wa= s unloaded from memory and it state saved to disk) identified by it's resou= rce + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_managedsave) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + retval =3D virDomainManagedSave(domain->domain, 0); + DPRINTF("%s: virDomainManagedSave(%p) returned %d\n", PHPFUNC, domain-= >domain, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_undefine + * Since version: 0.4.1(-1) + * Description: Function is used to undefine the domain identified by = it's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_undefine) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainUndefine(domain->domain); + DPRINTF("%s: virDomainUndefine(%p) returned %d\n", PHPFUNC, domain->do= main, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_reboot + * Since version: 0.4.1(-1) + * Description: Function is used to reboot the domain identified by it= 's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @flags [int]: optional flags + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_reboot) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); + + retval =3D virDomainReboot(domain->domain, flags); + DPRINTF("%s: virDomainReboot(%p) returned %d\n", PHPFUNC, domain->doma= in, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_define_xml + * Since version: 0.4.1(-1) + * Description: Function is used to define the domain from XML string + * Arguments: @conn [resource]: libvirt connection resource + * @xml [string]: XML string to define guest from + * Returns: newly defined domain resource + */ +PHP_FUNCTION(libvirt_domain_define_xml) +{ + php_libvirt_domain *res_domain =3D NULL; + php_libvirt_connection *conn =3D NULL; + zval *zconn; + virDomainPtr domain =3D NULL; + char *xml; + strsize_t xml_len; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &xml, &xml_len); + + domain =3D virDomainDefineXML(conn->conn, xml); + if (domain =3D=3D NULL) + RETURN_FALSE; + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D domain; + res_domain->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_create_xml + * Since version: 0.4.1(-1) + * Description: Function is used to create the domain identified by it= 's resource + * Arguments: @conn [resource]: libvirt connection resource + * @xml [string]: XML string to create guest from + * Returns: newly started/created domain resource + */ +PHP_FUNCTION(libvirt_domain_create_xml) +{ + php_libvirt_domain *res_domain =3D NULL; + php_libvirt_connection *conn =3D NULL; + zval *zconn; + virDomainPtr domain =3D NULL; + char *xml; + strsize_t xml_len; + zend_long flags =3D 0; + + GET_CONNECTION_FROM_ARGS("rs|l", &zconn, &xml, &xml_len, &flags); + + domain =3D virDomainCreateXML(conn->conn, xml, flags); + DPRINTF("%s: virDomainCreateXML(%p, , 0) returned %p\n", PHPFUNC,= conn->conn, domain); + if (domain =3D=3D NULL) + RETURN_FALSE; + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D domain; + res_domain->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_xml_from_native + * Since version: 0.5.3 + * Description: Function is used to convert native configuration data = to libvirt domain XML + * Arguments: @conn [resource]: libvirt connection resource + * @format [string]: configuration format converting from + * @config_data [string]: content of the native config fi= le + * Returns: libvirt domain XML, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_xml_from_native) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + char *config_data =3D NULL; + char *format =3D NULL; + char *xml =3D NULL; + strsize_t config_data_len; + strsize_t format_len; + unsigned int flags =3D 0; + + GET_CONNECTION_FROM_ARGS("rss", &zconn, &format, &format_len, &config_= data, &config_data_len); + + xml =3D virConnectDomainXMLFromNative(conn->conn, format, config_data,= flags); + + if (xml =3D=3D NULL) { + set_error_if_unset("Cannot convert native format to XML" TSRMLS_CC= ); + RETURN_FALSE; + } + + VIRT_RETVAL_STRING(xml); + free(xml); +} + +/* + * Function name: libvirt_domain_xml_to_native + * Since version: 0.5.3 + * Description: Function is used to convert libvirt domain XML to nati= ve configuration + * Arguments: @conn [resource]: libvirt connection resource + * @format [string]: configuration format converting to + * @xml_data [string]: content of the libvirt domain xml = file + * Returns: contents of the native data file, FALSE on error +*/ +PHP_FUNCTION(libvirt_domain_xml_to_native) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + char *xml_data =3D NULL; + char *format =3D NULL; + char *config_data =3D NULL; + strsize_t xml_data_len; + strsize_t format_len; + unsigned int flags =3D 0; + + GET_CONNECTION_FROM_ARGS("rss", &zconn, &format, &format_len, &xml_dat= a, &xml_data_len); + + config_data =3D virConnectDomainXMLToNative(conn->conn, format, xml_da= ta, flags); + + if (config_data =3D=3D NULL) { + set_error_if_unset("Cannot convert to native format from XML" TSRM= LS_CC); + RETURN_FALSE; + } + + VIRT_RETVAL_STRING(config_data); + free(config_data); +} + +/* + * Function name: libvirt_domain_set_max_memory + * Since version: 0.5.1 + * Description: Function to set max memory for domain + * Arguments: @res [resource]: libvirt domain resource + * @memory [int]: memory size in 1024 bytes (Kb) + * Returns: TRUE for success, FALSE for failure + */ +PHP_FUNCTION(libvirt_domain_set_max_memory) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zend_long memory =3D 0; + + GET_DOMAIN_FROM_ARGS("rl", &zdomain, &memory); + + if (virDomainSetMaxMemory(domain->domain, memory) !=3D 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_set_memory + * Since version: 0.5.1 + * Description: Function to set memory for domain + * Arguments: @res [resource]: libvirt domain resource + * @memory [int]: memory size in 1024 bytes (Kb) + * Returns: TRUE for success, FALSE for failure + */ +PHP_FUNCTION(libvirt_domain_set_memory) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zend_long memory =3D 0; + + GET_DOMAIN_FROM_ARGS("rl", &zdomain, &memory); + + if (virDomainSetMemory(domain->domain, memory) !=3D 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_set_memory_flags + * Since version: 0.5.1 + * Description: Function to set max memory for domain + * Arguments: @res [resource]: libvirt domain resource + * @memory [int]: memory size in 1024 bytes (Kb) + * @flags [int]: bitwise-OR VIR_DOMAIN_MEM_* flags + * Returns: TRUE for success, FALSE for failure + */ +PHP_FUNCTION(libvirt_domain_set_memory_flags) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zend_long memory =3D 0; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("rl|l", &zdomain, &memory, &flags); + + if (virDomainSetMemoryFlags(domain->domain, memory, flags) !=3D 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_memory_peek + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's memory peek value + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @start [int]: start + * @size [int]: size + * @flags [int]: optional flags + * Returns: domain memory peek + */ +PHP_FUNCTION(libvirt_domain_memory_peek) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + zend_long flags =3D 0; + zend_long start; + zend_long size; + char *buff; + + GET_DOMAIN_FROM_ARGS("rlll", &zdomain, &start, &size, &flags); + if (start < 0) { + set_error("Negative argument start" TSRMLS_CC); + RETURN_FALSE; + } + buff =3D (char *)emalloc(size); + retval =3D virDomainMemoryPeek(domain->domain, start, size, buff, flag= s); + if (retval !=3D 0) + RETURN_FALSE; + VIRT_RETVAL_STRINGL(buff, size); + efree(buff); +} + +/* + * Function name: libvirt_domain_memory_stats + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's memory stats + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @flags [int]: optional flags + * Returns: domain memory stats array (same fields as virDomainMem= oryStats, please see libvirt documentation) + */ +PHP_FUNCTION(libvirt_domain_memory_stats) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + zend_long flags =3D 0; + int i; + struct _virDomainMemoryStat stats[VIR_DOMAIN_MEMORY_STAT_NR]; + + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); + + retval =3D virDomainMemoryStats(domain->domain, stats, VIR_DOMAIN_MEMO= RY_STAT_NR, flags); + DPRINTF("%s: virDomainMemoryStats(%p...) returned %d\n", PHPFUNC, doma= in->domain, retval); + + if (retval =3D=3D -1) + RETURN_FALSE; + LONGLONG_INIT; + array_init(return_value); + for (i =3D 0; i < retval; i++) + LONGLONG_INDEX(return_value, stats[i].tag, stats[i].val) +} + +/* + * Function name: libvirt_domain_block_commit + * Since version: 0.5.2(-1) + * Description: Function is used to commit block job + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @disk [string]: path to the block device, or device sh= orthand + * @base [string]: path to backing file to merge into, or= device shorthand, or NULL for default + * @top [string]: path to file within backing chain that = contains data to be merged, or device shorthand, or NULL to merge all possi= ble data + * @bandwidth [int]: (optional) specify bandwidth limit; = flags determine the unit + * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_COMMIT_* + * Returns: true on success fail on error + */ +PHP_FUNCTION(libvirt_domain_block_commit) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *disk =3D NULL; + strsize_t disk_len; + char *base =3D NULL; + strsize_t base_len; + char *top =3D NULL; + strsize_t top_len; + zend_long bandwidth =3D 0; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("rs|ssll", &zdomain, &disk, &disk_len, &base, &ba= se_len, &top, &top_len, &bandwidth, &flags); + if (strcmp(disk, "") =3D=3D 0) + RETURN_FALSE; + if (strcmp(base, "") =3D=3D 0) + base =3D NULL; + if (strcmp(top, "") =3D=3D 0) + top =3D NULL; + + retval =3D virDomainBlockCommit(domain->domain, disk, base, top, bandw= idth, flags); + if (retval =3D=3D -1) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_block_stats + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's block stats + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @path [string]: device path to get statistics about + * Returns: domain block stats array, fields are rd_req, rd_bytes,= wr_req, wr_bytes and errs + */ +PHP_FUNCTION(libvirt_domain_block_stats) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *path; + strsize_t path_len; + + struct _virDomainBlockStats stats; + + GET_DOMAIN_FROM_ARGS("rs", &zdomain, &path, &path_len); + + retval =3D virDomainBlockStats(domain->domain, path, &stats, sizeof st= ats); + DPRINTF("%s: virDomainBlockStats(%p, %s, , ) returned %d\= n", PHPFUNC, domain->domain, path, retval); + if (retval =3D=3D -1) + RETURN_FALSE; + + array_init(return_value); + LONGLONG_INIT; + LONGLONG_ASSOC(return_value, "rd_req", stats.rd_req); + LONGLONG_ASSOC(return_value, "rd_bytes", stats.rd_bytes); + LONGLONG_ASSOC(return_value, "wr_req", stats.wr_req); + LONGLONG_ASSOC(return_value, "wr_bytes", stats.wr_bytes); + LONGLONG_ASSOC(return_value, "errs", stats.errs); +} + +/* + * Function name: libvirt_domain_block_resize + * Since version: 0.5.1(-1) + * Description: Function is used to resize the domain's block device + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @path [string]: device path to resize + * @size [int]: size of device + * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_RESIZE_* + * Returns: true on success fail on error + */ +PHP_FUNCTION(libvirt_domain_block_resize) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *path; + strsize_t path_len; + zend_long size =3D 0; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("rsl|l", &zdomain, &path, &path_len, &size, &flag= s); + + retval =3D virDomainBlockResize(domain->domain, path, size, flags); + if (retval =3D=3D -1) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_block_job_info + * Since version: 0.5.2(-1) + * Description: Function is used to request block job information for = the given disk + * Arguments: @dom [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @disk [string]: path to the block device, or device sh= orthand + * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_COMMIT_* + * Returns: Array with status virDomainGetBlockJobInfo and blockjo= b information. + */ +PHP_FUNCTION(libvirt_domain_block_job_info) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *disk; + int disk_len; + long flags =3D 0; + virDomainBlockJobInfo info; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &disk, &disk_len, &flags); + + retval =3D virDomainGetBlockJobInfo(domain->domain, disk, &info, flags= ); + + array_init(return_value); + add_assoc_long(return_value, "status", (int)retval); + add_assoc_long(return_value, "type", (int)info.type); + add_assoc_long(return_value, "bandwidth", (unsigned long)info.bandwidt= h); + add_assoc_long(return_value, "cur", (unsigned long long)info.cur); + add_assoc_long(return_value, "end", (unsigned long long)info.end); +} + + +/* + * Function name: libvirt_domain_block_job_abort + * Since version: 0.5.1(-1) + * Description: Function is used to abort block job + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @path [string]: device path to resize + * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_JOB_ABORT= _* + * Returns: true on success fail on error + */ +PHP_FUNCTION(libvirt_domain_block_job_abort) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *path; + strsize_t path_len; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &path, &path_len, &flags); + + retval =3D virDomainBlockJobAbort(domain->domain, path, flags); + if (retval =3D=3D -1) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_block_job_set_speed + * Since version: 0.5.1(-1) + * Description: Function is used to set speed of block job + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @path [string]: device path to resize + * @bandwidth [int]: bandwidth + * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_JOB_SPEED= _BANDWIDTH_* + * Returns: true on success fail on error + */ +PHP_FUNCTION(libvirt_domain_block_job_set_speed) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *path; + strsize_t path_len; + zend_long bandwidth =3D 0; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("rsl|l", &zdomain, &path, &path_len, &bandwidth, = &flags); + + retval =3D virDomainBlockJobSetSpeed(domain->domain, path, bandwidth, = flags); + if (retval =3D=3D -1) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_interface_stats + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's interface stats + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @path [string]: path to interface device + * Returns: interface stats array of {tx|rx}_{bytes|packets|errs|d= rop} fields + */ +PHP_FUNCTION(libvirt_domain_interface_stats) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *path; + strsize_t path_len; + + struct _virDomainInterfaceStats stats; + + GET_DOMAIN_FROM_ARGS("rs", &zdomain, &path, &path_len); + + retval =3D virDomainInterfaceStats(domain->domain, path, &stats, sizeo= f stats); + DPRINTF("%s: virDomainInterfaceStats(%p, %s, , ) returned= %d\n", PHPFUNC, domain->domain, path, retval); + if (retval =3D=3D -1) + RETURN_FALSE; + + array_init(return_value); + LONGLONG_INIT; + LONGLONG_ASSOC(return_value, "rx_bytes", stats.rx_bytes); + LONGLONG_ASSOC(return_value, "rx_packets", stats.rx_packets); + LONGLONG_ASSOC(return_value, "rx_errs", stats.rx_errs); + LONGLONG_ASSOC(return_value, "rx_drop", stats.rx_drop); + LONGLONG_ASSOC(return_value, "tx_bytes", stats.tx_bytes); + LONGLONG_ASSOC(return_value, "tx_packets", stats.tx_packets); + LONGLONG_ASSOC(return_value, "tx_errs", stats.tx_errs); + LONGLONG_ASSOC(return_value, "tx_drop", stats.tx_drop); +} + +/* + * Function name: libvirt_domain_get_connect + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's connection resour= ce. This function should *not* be used! + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: libvirt connection resource + */ +PHP_FUNCTION(libvirt_domain_get_connect) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + php_libvirt_connection *conn; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + conn =3D domain->conn; + if (conn->conn =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETURN_RESOURCE(conn->resource); + /* since we're returning already registered resource, bump refcount */ + Z_ADDREF_P(return_value); +} + +/* + * Function name: libvirt_domain_migrate + * Since version: 0.4.1(-1) + * Description: Function is used migrate domain to another domain + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @dest_conn [string]: destination host connection object + * @flags [int]: migration flags + * @dname [string]: domain name to rename domain to on de= stination side + * @bandwidth [int]: migration bandwidth in Mbps + * Returns: libvirt domain resource for migrated domain + */ +PHP_FUNCTION(libvirt_domain_migrate) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + php_libvirt_connection *dconn =3D NULL; + zval *zdconn; + virDomainPtr destdomain =3D NULL; + php_libvirt_domain *res_domain; + + zend_long flags =3D 0; + char *dname; + strsize_t dname_len; + zend_long bandwidth; + + dname =3D NULL; + dname_len =3D 0; + bandwidth =3D 0; + + GET_DOMAIN_FROM_ARGS("rrl|sl", &zdomain, &zdconn, &flags, &dname, &dna= me_len, &bandwidth); + + if ((domain->domain =3D=3D NULL) || (domain->conn->conn =3D=3D NULL)) { + set_error("Domain object is not valid" TSRMLS_CC); + RETURN_FALSE; + } + + VIRT_FETCH_RESOURCE(dconn, php_libvirt_connection*, &zdconn, PHP_LIBVI= RT_CONNECTION_RES_NAME, le_libvirt_connection); + if ((dconn =3D=3D NULL) || (dconn->conn =3D=3D NULL)) { + set_error("Destination connection object is not valid" TSRMLS_CC); + RETURN_FALSE; + } + + destdomain =3D virDomainMigrate(domain->domain, dconn->conn, flags, dn= ame, NULL, bandwidth); + if (destdomain =3D=3D NULL) + RETURN_FALSE; + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D destdomain; + res_domain->conn =3D dconn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, dconn->conn, res_domain->= domain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_migrate_to_uri + * Since version: 0.4.1(-1) + * Description: Function is used migrate domain to another libvirt dae= mon specified by it's URI + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @dest_uri [string]: destination URI to migrate to + * @flags [int]: migration flags + * @dname [string]: domain name to rename domain to on de= stination side + * @bandwidth [int]: migration bandwidth in Mbps + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_migrate_to_uri) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + long flags =3D 0; + char *duri; + strsize_t duri_len; + char *dname; + strsize_t dname_len; + zend_long bandwidth; + + dname =3D NULL; + dname_len =3D 0; + bandwidth =3D 0; + GET_DOMAIN_FROM_ARGS("rsl|sl", &zdomain, &duri, &duri_len, &flags, &dn= ame, &dname_len, &bandwidth); + + retval =3D virDomainMigrateToURI(domain->domain, duri, flags, dname, b= andwidth); + DPRINTF("%s: virDomainMigrateToURI() returned %d\n", PHPFUNC, retval); + + if (retval =3D=3D 0) + RETURN_TRUE; + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_migrate_to_uri2 + * Since version: 0.4.6(-1) + * Description: Function is used migrate domain to another libvirt dae= mon specified by it's URI + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @dconnuri [string]: URI for target libvirtd + * @miguri [string]: URI for invoking the migration + * @dxml [string]: XML config for launching guest on targ= et + * @flags [int]: migration flags + * @dname [string]: domain name to rename domain to on de= stination side + * @bandwidth [int]: migration bandwidth in Mbps + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_migrate_to_uri2) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *dconnuri; + strsize_t dconnuri_len; + char *miguri; + strsize_t miguri_len; + char *dxml; + strsize_t dxml_len; + zend_long flags =3D 0; + char *dname; + strsize_t dname_len; + zend_long bandwidth; + + dconnuri =3D NULL; + dconnuri_len =3D 0; + miguri =3D NULL; + miguri_len =3D 0; + dxml =3D NULL; + dxml_len =3D 0; + dname =3D NULL; + dname_len =3D 0; + bandwidth =3D 0; + GET_DOMAIN_FROM_ARGS("r|ssslsl", &zdomain, &dconnuri, &dconnuri_len, &= miguri, &miguri_len, &dxml, &dxml_len, &flags, &dname, &dname_len, &bandwid= th); + + // set to NULL if empty string + if (dconnuri_len =3D=3D 0) + dconnuri=3DNULL; + if (miguri_len =3D=3D 0) + miguri=3DNULL; + if (dxml_len =3D=3D 0) + dxml=3DNULL; + if (dname_len =3D=3D 0) + dname=3DNULL; + + retval =3D virDomainMigrateToURI2(domain->domain, dconnuri, miguri, dx= ml, flags, dname, bandwidth); + DPRINTF("%s: virDomainMigrateToURI2() returned %d\n", PHPFUNC, retval); + + if (retval =3D=3D 0) + RETURN_TRUE; + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_get_job_info + * Since version: 0.4.1(-1) + * Description: Function is used get job information for the domain + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: job information array of type, time, data, mem and fil= e fields + */ +PHP_FUNCTION(libvirt_domain_get_job_info) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + struct _virDomainJobInfo jobinfo; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainGetJobInfo(domain->domain, &jobinfo); + if (retval =3D=3D -1) + RETURN_FALSE; + + array_init(return_value); + LONGLONG_INIT; + add_assoc_long(return_value, "type", jobinfo.type); + LONGLONG_ASSOC(return_value, "time_elapsed", jobinfo.timeElapsed); + LONGLONG_ASSOC(return_value, "time_remaining", jobinfo.timeRemaining); + LONGLONG_ASSOC(return_value, "data_total", jobinfo.dataTotal); + LONGLONG_ASSOC(return_value, "data_processed", jobinfo.dataProcessed); + LONGLONG_ASSOC(return_value, "data_remaining", jobinfo.dataRemaining); + LONGLONG_ASSOC(return_value, "mem_total", jobinfo.memTotal); + LONGLONG_ASSOC(return_value, "mem_processed", jobinfo.memProcessed); + LONGLONG_ASSOC(return_value, "mem_remaining", jobinfo.memRemaining); + LONGLONG_ASSOC(return_value, "file_total", jobinfo.fileTotal); + LONGLONG_ASSOC(return_value, "file_processed", jobinfo.fileProcessed); + LONGLONG_ASSOC(return_value, "file_remaining", jobinfo.fileRemaining); +} + +/* + * Function name: libvirt_domain_xml_xpath + * Since version: 0.4.1(-1) + * Description: Function is used to get the result of xPath expression= that's run against the domain + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @xpath [string]: xPath expression to parse against the= domain + * @flags [int]: optional flags + * Returns: result of the expression in an array + */ +PHP_FUNCTION(libvirt_domain_xml_xpath) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zval *zpath; + char *xml; + char *tmp =3D NULL; + strsize_t path_len =3D -1; + zend_long flags =3D 0; + int rc =3D 0; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &zpath, &path_len, &flags); + + xml =3D virDomainGetXMLDesc(domain->domain, flags); + if (!xml) + RETURN_FALSE; + + array_init(return_value); + + free(get_string_from_xpath(xml, (char *)zpath, &return_value, &rc)); + if (return_value < 0) { + free(xml); + RETURN_FALSE; + } + + free(tmp); + free(xml); + + if (rc =3D=3D 0) + RETURN_FALSE; + + VIRT_ADD_ASSOC_STRING(return_value, "xpath", (char *)zpath); + if (rc < 0) + add_assoc_long(return_value, "error_code", (long)rc); +} + +/* + * Function name: libvirt_domain_get_block_info + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's block device info= rmation + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @dev [string]: device to get block information about + * Returns: domain block device information array of device, file = or partition, capacity, allocation and physical size + */ +PHP_FUNCTION(libvirt_domain_get_block_info) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *dev; + char *xml; + char *tmp =3D NULL; + strsize_t dev_len; + int isFile; + char *xpath =3D NULL; + + struct _virDomainBlockInfo info; + + GET_DOMAIN_FROM_ARGS("rs", &zdomain, &dev, &dev_len); + + /* Get XML for the domain */ + xml =3D virDomainGetXMLDesc(domain->domain, VIR_DOMAIN_XML_INACTIVE); + if (!xml) { + set_error("Cannot get domain XML" TSRMLS_CC); + RETURN_FALSE; + } + + isFile =3D 0; + + if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/../sou= rce/@dev", dev) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (retval < 0) { + set_error("Cannot get XPath expression result for device storage" = TSRMLS_CC); + goto error; + } + + if (retval =3D=3D 0) { + free(xpath); + if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/..= /source/@file", dev) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + free(tmp); + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (retval < 0) { + set_error("Cannot get XPath expression result for file storage= " TSRMLS_CC); + goto error; + } + isFile =3D 1; + } + + if (retval =3D=3D 0) { + set_error("No relevant node found" TSRMLS_CC); + goto error; + } + + retval =3D virDomainGetBlockInfo(domain->domain, tmp, &info, 0); + if (retval =3D=3D -1) { + set_error("Cannot get domain block information" TSRMLS_CC); + goto error; + } + + array_init(return_value); + LONGLONG_INIT; + VIRT_ADD_ASSOC_STRING(return_value, "device", dev); + + if (isFile) + VIRT_ADD_ASSOC_STRING(return_value, "file", tmp); + else + VIRT_ADD_ASSOC_STRING(return_value, "partition", tmp); + + free(xpath); + if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/../dri= ver/@type", dev) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + free(tmp); + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (tmp !=3D NULL) + VIRT_ADD_ASSOC_STRING(return_value, "type", tmp); + + LONGLONG_ASSOC(return_value, "capacity", info.capacity); + LONGLONG_ASSOC(return_value, "allocation", info.allocation); + LONGLONG_ASSOC(return_value, "physical", info.physical); + + free(xpath); + free(tmp); + free(xml); + return; + + error: + free(xpath); + free(tmp); + free(xml); + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_get_network_info + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's network informati= on + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @mac [string]: mac address of the network device + * Returns: domain network info array of MAC address, network name= and type of NIC card + */ +PHP_FUNCTION(libvirt_domain_get_network_info) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *mac; + char *xml; + char *tmp =3D NULL; + strsize_t mac_len; + char *xpath =3D NULL; + + GET_DOMAIN_FROM_ARGS("rs", &zdomain, &mac, &mac_len); + + /* Get XML for the domain */ + xml =3D virDomainGetXMLDesc(domain->domain, VIR_DOMAIN_XML_INACTIVE); + if (!xml) { + set_error("Cannot get domain XML" TSRMLS_CC); + RETURN_FALSE; + } + + DPRINTF("%s: Getting network information for NIC with MAC address '%s'= \n", PHPFUNC, mac); + if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/../source/@network", mac) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (tmp =3D=3D NULL) { + set_error("Invalid XPath node for source network" TSRMLS_CC); + goto error; + } + + if (retval < 0) { + set_error("Cannot get XPath expression result for network source" = TSRMLS_CC); + goto error; + } + + array_init(return_value); + VIRT_ADD_ASSOC_STRING(return_value, "mac", mac); + VIRT_ADD_ASSOC_STRING(return_value, "network", tmp); + + free(tmp); + free(xpath); + + if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/../model/@type", mac) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "nic_type", tmp); + else + VIRT_ADD_ASSOC_STRING(return_value, "nic_type", "default"); + + free(xml); + free(xpath); + free(tmp); + return; + + error: + free(xml); + free(xpath); + free(tmp); + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_get_autostart + * Since version: 0.4.1(-1) + * Description: Function is getting the autostart value for the domain + * Arguments: @res [resource]: libvirt domain resource + * Returns: autostart value or -1 + */ +PHP_FUNCTION(libvirt_domain_get_autostart) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int flags =3D 0; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + if (virDomainGetAutostart (domain->domain, &flags) !=3D 0) { + RETURN_LONG(-1); + } + RETURN_LONG((long)flags); +} + +/* + * Function name: libvirt_domain_set_autostart + * Since version: 0.4.1(-1) + * Description: Function is setting the autostart value for the domain + * Arguments: @res [resource]: libvirt domain resource + * @flags [int]: flag to enable/disable autostart + * Returns: TRUE on success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_set_autostart) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zend_bool flags =3D 0; + + GET_DOMAIN_FROM_ARGS("rb", &zdomain, &flags); + + if (virDomainSetAutostart (domain->domain, flags) !=3D 0) { + RETURN_FALSE; + } + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_get_metadata + * Since version: 0.4.9 + * Description: Function retrieve appropriate domain element given by = @type. + * Arguments: @res [resource]: libvirt domain resource + * @type [int]: virDomainMetadataType type of description + * @uri [string]: XML namespace identifier + * @flags [int]: bitwise-OR of virDomainModificationImpact + * Returns: metadata string, NULL on error or FALSE on API not sup= ported + */ +PHP_FUNCTION(libvirt_domain_get_metadata) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zend_long type =3D 0; + zend_long flags =3D 0; + char *uri =3D NULL; + strsize_t uri_len; + char *ret =3D NULL; + + GET_DOMAIN_FROM_ARGS("rlsl", &zdomain, &type, &uri, &uri_len, &flags); + + if ((uri !=3D NULL) && (strlen(uri) =3D=3D 0)) + uri =3D NULL; + + ret =3D virDomainGetMetadata(domain->domain, type, uri, flags); + if (ret =3D=3D NULL) { + if (strstr(LIBVIRT_G(last_error), "not supported") !=3D NULL) + RETURN_FALSE; + RETURN_NULL(); + } else { + VIRT_RETVAL_STRING(ret); + free(ret); + } +} + +/* + * Function name: libvirt_domain_set_metadata + * Since version: 0.4.9 + * Description: Function sets the appropriate domain element given by = @type to the value of @description. No new lines are permitted. + * Arguments: @res [resource]: libvirt domain resource + * @type [int]: virDomainMetadataType type of description + * @metadata [string]: new metadata text + * @key [string]: XML namespace key or empty string (alia= s of NULL) + * @uri [string]: XML namespace identifier or empty strin= g (alias of NULL) + * @flags [int]: bitwise-OR of virDomainModificationImpact + * Returns: -1 on error, 0 on success + */ +PHP_FUNCTION(libvirt_domain_set_metadata) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + strsize_t metadata_len, key_len, uri_len; + char *metadata =3D NULL; + char *key =3D NULL; + char *uri =3D NULL; + zend_long type =3D 0; + zend_long flags =3D 0; + int rc; + + GET_DOMAIN_FROM_ARGS("rlsssl", &zdomain, &type, &metadata, &metadata_l= en, &key, &key_len, &uri, &uri_len, &flags); + + if ((key !=3D NULL) && (strlen(key) =3D=3D 0)) + key =3D NULL; + if ((uri !=3D NULL) && (strlen(uri) =3D=3D 0)) + uri =3D NULL; + + rc =3D virDomainSetMetadata(domain->domain, type, metadata, key, uri, = flags); + RETURN_LONG(rc); +} + +/* + * Function name: libvirt_domain_is_active + * Since version: 0.4.1(-1) + * Description: Function is getting information whether domain identif= ied by resource is active or not + * Arguments: @res [resource]: libvirt domain resource + * Returns: virDomainIsActive() result on the domain + */ +PHP_FUNCTION(libvirt_domain_is_active) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + RETURN_LONG(virDomainIsActive(domain->domain)); +} + +/* + * Function name: libvirt_domain_get_next_dev_ids + * Since version: 0.4.2 + * Description: This functions can be used to get the next free slot i= f you intend to add a new device identified by slot to the domain, e.g. NIC= device + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: next free slot number for the domain + */ +PHP_FUNCTION(libvirt_domain_get_next_dev_ids) +{ + long dom, bus, slot, func; + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + DPRINTF("%s: Getting the next dev ids for domain %p\n", PHPFUNC, domai= n->domain); + + dom =3D get_next_free_numeric_value(domain->domain, "//@domain"); + bus =3D get_next_free_numeric_value(domain->domain, "//@bus"); + slot =3D get_next_free_numeric_value(domain->domain, "//@slot"); + func =3D get_next_free_numeric_value(domain->domain, "//@func"); + + array_init(return_value); + add_assoc_long(return_value, "next_domain", dom); + add_assoc_long(return_value, "next_bus", bus); + add_assoc_long(return_value, "next_slot", slot); + add_assoc_long(return_value, "next_func", func); +} + +/* + * Function name: libvirt_domain_get_screenshot + * Since version: 0.4.2 + * Description: Function uses gvnccapture (if available) to get the sc= reenshot of the running domain + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @server [string]: server string for the host machine + * @scancode [int]: integer value of the scancode to be s= end to refresh screen + * Returns: PNG image binary data + */ +PHP_FUNCTION(libvirt_domain_get_screenshot) +{ +#ifndef EXTWIN + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + pid_t childpid =3D -1; + pid_t w =3D -1; + int retval =3D -1; + int fd =3D -1, fsize =3D -1; + char file[] =3D "/tmp/libvirt-php-tmp-XXXXXX"; + char *buf =3D NULL; + char *tmp =3D NULL; + char *xml =3D NULL; + int port =3D -1; + char *hostname =3D NULL; + strsize_t hostname_len; + zend_long scancode =3D 10; + const char *path; + char *pathDup =3D NULL; + char name[1024] =3D { 0 }; + int use_builtin =3D 0; + + path =3D get_feature_binary("screenshot"); + DPRINTF("%s: get_feature_binary('screenshot') returned %s\n", PHPFUNC,= path); + + if ((path =3D=3D NULL) || (access(path, X_OK) !=3D 0)) { + use_builtin =3D 1; + } else { + if (!(pathDup =3D strdup(path))) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + } + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &hostname, &hostname_len, &scan= code); + + xml =3D virDomainGetXMLDesc(domain->domain, 0); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + goto error; + } + + tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + set_error("Cannot get the VNC port" TSRMLS_CC); + goto error; + } + + if (mkstemp(file) =3D=3D 0) + goto error; + + /* Get the current hostname and override to localhost if local machine= */ + gethostname(name, 1024); + if (strcmp(name, hostname) =3D=3D 0) + hostname =3D strdup("localhost"); + + vnc_refresh_screen(hostname, tmp, scancode); + + if (use_builtin =3D=3D 1) { + DPRINTF("%s: Binary not found, using builtin approach to %s:%s, tm= p file =3D %s\n", PHPFUNC, hostname, tmp, file); + + if (vnc_get_bitmap(hostname, tmp, file) !=3D 0) { + set_error("Cannot use builtin approach to get VNC window conte= nts" TSRMLS_CC); + goto error; + } + } else { + port =3D atoi(tmp)-5900; + + DPRINTF("%s: Getting screenshot of %s:%d to temporary file %s\n", = PHPFUNC, hostname, port, file); + + childpid =3D fork(); + if (childpid =3D=3D -1) + goto error; + + if (childpid =3D=3D 0) { + char tmpp[64] =3D { 0 }; + + snprintf(tmpp, sizeof(tmpp), "%s:%d", hostname, port); + retval =3D execlp(path, basename(pathDup), tmpp, file, NULL); + _exit(retval); + } else { + do { + w =3D waitpid(childpid, &retval, 0); + if (w =3D=3D -1) + goto error; + } while (!WIFEXITED(retval) && !WIFSIGNALED(retval)); + } + + if (WEXITSTATUS(retval) !=3D 0) { + set_error("Cannot spawn utility to get screenshot" TSRMLS_CC); + goto error; + } + } + + fd =3D open(file, O_RDONLY); + fsize =3D lseek(fd, 0, SEEK_END); + lseek(fd, 0, SEEK_SET); + buf =3D emalloc((fsize + 1) * sizeof(char)); + memset(buf, 0, fsize + 1); + if (read(fd, buf, fsize) < 0) { + close(fd); + unlink(file); + goto error; + } + close(fd); + + if (access(file, F_OK) =3D=3D 0) { + DPRINTF("%s: Temporary file %s deleted\n", PHPFUNC, file); + unlink(file); + } + + /* This is necessary to make the output binary safe */ + VIRT_ZVAL_STRINGL(return_value, buf, fsize); + + efree(buf); + free(tmp); + free(xml); + free(pathDup); + return; + + error: + efree(buf); + free(tmp); + free(xml); + free(pathDup); + RETURN_FALSE; +#else + set_error("Function is not supported on Windows systems" TSRMLS_CC); + RETURN_FALSE; +#endif +} + +/* + * Function name: libvirt_domain_get_screenshot_api + * Since version: 0.4.5 + * Description: Function is trying to get domain screenshot using libv= irt virGetDomainScreenshot() API if available. + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_get_by_*() + * @screenID [int]: monitor ID from where to take screens= hot + * Returns: array of filename and mime type as type is hypervisor = specific, caller is responsible for temporary file deletion + */ +PHP_FUNCTION(libvirt_domain_get_screenshot_api) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zend_long screen =3D 0; + int fd =3D -1; + char file[] =3D "/tmp/libvirt-php-tmp-XXXXXX"; + virStreamPtr st =3D NULL; + char *mime =3D NULL; + const char *bin =3D get_feature_binary("screenshot-convert"); + +#ifdef EXTWIN + set_error_if_unset("Cannot get domain screenshot on Windows systems" T= SRMLS_CC); + RETURN_FALSE; +#endif + + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &screen); + + if (!(st =3D virStreamNew(domain->conn->conn, 0))) { + set_error("Cannot create new stream" TSRMLS_CC); + goto error; + } + + mime =3D virDomainScreenshot(domain->domain, st, screen, 0); + if (!mime) { + set_error_if_unset("Cannot get domain screenshot" TSRMLS_CC); + goto error; + } + + if (!(fd =3D mkstemp(file))) { + virStreamAbort(st); + set_error_if_unset("Cannot get create file to save domain screensh= ot" TSRMLS_CC); + goto error; + } + + if (virStreamRecvAll(st, streamSink, &fd) < 0) { + set_error_if_unset("Cannot receive screenshot data" TSRMLS_CC); + virStreamAbort(st); + goto error; + } + + if (virStreamFinish(st) < 0) { + set_error_if_unset("Cannot close stream for domain" TSRMLS_CC); + goto error; + } + + virStreamFree(st); + st =3D NULL; + + array_init(return_value); + if (bin) { + char tmp[4096] =3D { 0 }; + char fileNew[1024] =3D { 0 }; + int exitStatus; + + snprintf(fileNew, sizeof(fileNew), "%s.png", file); + snprintf(tmp, sizeof(tmp), "%s %s %s > /dev/null 2> /dev/null", bi= n, file, fileNew); + exitStatus =3D system(tmp); + if (WEXITSTATUS(exitStatus) !=3D 0) + goto error; + + unlink(file); + close(fd); + fd =3D -1; + VIRT_ADD_ASSOC_STRING(return_value, "file", fileNew); + VIRT_ADD_ASSOC_STRING(return_value, "mime", "image/png"); + } else { + close(fd); + fd =3D -1; + VIRT_ADD_ASSOC_STRING(return_value, "file", file); + VIRT_ADD_ASSOC_STRING(return_value, "mime", mime); + } + + free(mime); + return; + + error: + free(mime); + if (fd !=3D -1) { + unlink(file); + close(fd); + } + if (st) + virStreamFree(st); + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_get_screen_dimensions + * Since version: 0.4.3 + * Description: Function get screen dimensions of the VNC window + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @server [string]: server string of the host machine + * Returns: array of height and width on success, FALSE otherwise + */ +PHP_FUNCTION(libvirt_domain_get_screen_dimensions) +{ +#ifndef EXTWIN + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval =3D -1; + char *tmp =3D NULL; + char *xml =3D NULL; + char *hostname =3D NULL; + strsize_t hostname_len; + int ret; + int width; + int height; + + GET_DOMAIN_FROM_ARGS("rs", &zdomain, &hostname, &hostname_len); + + xml =3D virDomainGetXMLDesc(domain->domain, 0); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + goto error; + } + + tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + set_error("Cannot get the VNC port" TSRMLS_CC); + goto error; + } + + DPRINTF("%s: hostname =3D %s, port =3D %s\n", PHPFUNC, hostname, tmp); + ret =3D vnc_get_dimensions(hostname, tmp, &width, &height); + free(tmp); + if (ret !=3D 0) { + char error[1024] =3D { 0 }; + if (ret =3D=3D -9) + snprintf(error, sizeof(error), "Cannot connect to VNC server. = Please make sure domain is running and VNC graphics are set"); + else + snprintf(error, sizeof(error), "Cannot get screen dimensions, = error code =3D %d (%s)", ret, strerror(-ret)); + + set_error(error TSRMLS_CC); + goto error; + } + + array_init(return_value); + add_assoc_long(return_value, "width", (long)width); + add_assoc_long(return_value, "height", (long)height); + + free(tmp); + free(xml); + return; + + error: + free(tmp); + free(xml); + RETURN_FALSE; +#else + set_error("Function is not supported on Windows systems" TSRMLS_CC); + RETURN_FALSE; +#endif +} + +/* + * Function name: libvirt_domain_send_keys + * Since version: 0.4.2 + * Description: Function sends keys to the domain's VNC window + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @server [string]: server string of the host machine + * @scancode [int]: integer scancode to be sent to VNC wi= ndow + * Returns: TRUE on success, FALSE otherwise + */ +PHP_FUNCTION(libvirt_domain_send_keys) +{ +#ifndef EXTWIN + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval =3D -1; + char *tmp =3D NULL; + char *xml =3D NULL; + char *hostname =3D NULL; + strsize_t hostname_len; + char *keys =3D NULL; + strsize_t keys_len; + int ret =3D 0; + + GET_DOMAIN_FROM_ARGS("rss", &zdomain, &hostname, &hostname_len, &keys,= &keys_len); + + DPRINTF("%s: Sending %d VNC keys to %s...\n", PHPFUNC, (int)strlen((co= nst char *)keys), hostname); + + xml =3D virDomainGetXMLDesc(domain->domain, 0); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + goto error; + } + + tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + set_error("Cannot get the VNC port" TSRMLS_CC); + goto error; + } + + DPRINTF("%s: About to send string '%s' (%d keys) to %s:%s\n", PHPFUNC,= keys, (int)strlen((const char *)keys), hostname, tmp); + + ret =3D vnc_send_keys(hostname, tmp, keys); + DPRINTF("%s: Sequence sending result is %d\n", PHPFUNC, ret); + + if (!ret) { + char tmpp[64] =3D { 0 }; + snprintf(tmpp, sizeof(tmpp), "Cannot send keys, error code %d", re= t); + set_error(tmpp TSRMLS_CC); + goto error; + } + + free(tmp); + free(xml); + RETURN_TRUE; + + error: + free(tmp); + free(xml); + RETURN_FALSE; +#else + set_error("Function is not supported on Windows systems" TSRMLS_CC); + RETURN_FALSE; +#endif +} + +/* + * Function name: libvirt_domain_send_key_api + * Since version: 0.5.3 + * Description: Function sends keys to domain via libvirt API + * Arguments: @res[resource]: libvirt domain resource, e.g. from lib= virt_domaing_lookup_by_*() + * @codeset [int]: the codeset of keycodes, from virKeyco= deSet + * @holdtime [int]: the duration (in miliseconds) that th= e keys will be held + * @keycodes [array]: array of keycodes + * @flags [int]: extra flags; not used yet so callers sho= uld alway pass 0 + * Returns: TRUE for success, FALSE for failure + */ +PHP_FUNCTION(libvirt_domain_send_key_api) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zend_long codeset; + zend_long holdtime =3D 0; + zend_long flags =3D 0; + zval *zkeycodes, *data =3D NULL; + HashTable *arr_hash =3D NULL; + HashPosition pointer; + int count, i; + uint *keycodes =3D NULL; + + GET_DOMAIN_FROM_ARGS("rlla|l", &zdomain, &codeset, &holdtime, &zkeycod= es, + &flags); + + arr_hash =3D Z_ARRVAL_P(zkeycodes); + count =3D zend_hash_num_elements(arr_hash); + + keycodes =3D (uint *) emalloc(count * sizeof(uint)); + + i =3D 0; + VIRT_FOREACH(arr_hash, pointer, data) { + if (Z_TYPE_P(data) =3D=3D IS_LONG) { + keycodes[i++] =3D (uint) Z_LVAL_P(data); + } + } VIRT_FOREACH_END(); + + if (virDomainSendKey(domain->domain, codeset, holdtime, keycodes, coun= t, + flags) !=3D 0) { + efree(keycodes); + RETURN_FALSE; + } + + efree(keycodes); + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_send_pointer_event + * Since version: 0.4.2 + * Description: Function sends keys to the domain's VNC window + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @server [string]: server string of the host machine + * @pos_x [int]: position on x-axis + * @pos_y [int]: position on y-axis + * @clicked [int]: mask of clicked buttons (0 for none, b= it 1 for button #1, bit 8 for button #8) + * @release [int]: boolean value (0 or 1) whether to rele= ase the buttons automatically once pressed + * Returns: TRUE on success, FALSE otherwise + */ +PHP_FUNCTION(libvirt_domain_send_pointer_event) +{ +#ifndef EXTWIN + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval =3D -1; + char *tmp =3D NULL; + char *xml =3D NULL; + char *hostname =3D NULL; + strsize_t hostname_len; + zend_long pos_x =3D 0; + zend_long pos_y =3D 0; + zend_long clicked =3D 0; + zend_bool release =3D 1; + int ret; + + GET_DOMAIN_FROM_ARGS("rslll|b", &zdomain, &hostname, &hostname_len, &p= os_x, &pos_y, &clicked, &release); + + xml =3D virDomainGetXMLDesc(domain->domain, 0); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + goto error; + } + + tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + set_error("Cannot get the VNC port" TSRMLS_CC); + goto error; + } + + DPRINTF("%s: x =3D %d, y =3D %d, clicked =3D %d, release =3D %d, hostn= ame =3D %s...\n", PHPFUNC, (int) pos_x, (int) pos_y, (int) clicked, release= , hostname); + ret =3D vnc_send_pointer_event(hostname, tmp, pos_x, pos_y, clicked, r= elease); + if (!ret) { + char error[1024] =3D { 0 }; + if (ret =3D=3D -9) + snprintf(error, sizeof(error), "Cannot connect to VNC server. = Please make sure domain is running and VNC graphics are set"); + else + snprintf(error, sizeof(error), "Cannot send pointer event, err= or code =3D %d (%s)", ret, strerror(-ret)); + + set_error(error TSRMLS_CC); + goto error; + } + + free(tmp); + free(xml); + RETURN_TRUE; + + error: + free(tmp); + free(xml); + RETURN_FALSE; +#else + set_error("Function is not supported on Windows systems" TSRMLS_CC); + RETURN_FALSE; +#endif +} + +/* + * Function name: libvirt_domain_update_device + * Since version: 0.4.1(-1) + * Description: Function is used to update the domain's devices from t= he XML string + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @xml [string]: XML string for the update + * @flags [int]: Flags to update the device (VIR_DOMAIN_D= EVICE_MODIFY_CURRENT, VIR_DOMAIN_DEVICE_MODIFY_LIVE, VIR_DOMAIN_DEVICE_MODI= FY_CONFIG, VIR_DOMAIN_DEVICE_MODIFY_FORCE) + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_update_device) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *xml; + strsize_t xml_len; + zend_long flags; + int res; + + GET_DOMAIN_FROM_ARGS("rsl", &zdomain, &xml, &xml_len, &flags); + + res =3D virDomainUpdateDeviceFlags(domain->domain, xml, flags); + DPRINTF("%s: virDomainUpdateDeviceFlags(%p) returned %d\n", PHPFUNC, d= omain->domain, res); + if (res !=3D 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_qemu_agent_command + * Since version: 0.5.2(-1) + * Description: Function is used to send qemu-ga command + * Arguments: @res [resource]: libvirt domain resource, e.g. from lib= virt_domain_lookup_by_*() + * @cmd [string]: command + * @timeout [int] timeout for waiting (-2 block, -1 defaul= t, 0 no wait, >0 wait specific time + * @flags [int]: unknown + * Returns: String on success and FALSE on error + */ +PHP_FUNCTION(libvirt_domain_qemu_agent_command) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + const char *cmd; + strsize_t cmd_len; + char *ret; + zend_long timeout =3D -1; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("rs|ll", &zdomain, &cmd, &cmd_len, &timeout, &fla= gs); + + ret =3D virDomainQemuAgentCommand(domain->domain, cmd, timeout, flags); + if (ret =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETVAL_STRING(ret); + free(ret); +} + +/* + * Function name: libvirt_list_domains + * Since version: 0.4.1(-1) + * Description: Function is used to list domains on the connection + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt domain names array for the connection + */ +PHP_FUNCTION(libvirt_list_domains) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + int *ids; + char **names; + const char *name; + int i, rv; + virDomainPtr domain =3D NULL; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) + RETURN_FALSE; + + DPRINTF("%s: Found %d domains\n", PHPFUNC, expectedcount); + + ids =3D (int *)emalloc(sizeof(int) * expectedcount); + count =3D virConnectListDomains(conn->conn, ids, expectedcount); + DPRINTF("%s: virConnectListDomains returned %d domains\n", PHPFUNC, co= unt); + + array_init(return_value); + for (i =3D 0; i < count; i++) { + domain =3D virDomainLookupByID(conn->conn, ids[i]); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, domain, 1= TSRMLS_CC); + if (domain !=3D NULL) { + name =3D virDomainGetName(domain); + if (name !=3D NULL) { + DPRINTF("%s: Found running domain %s with ID =3D %d\n", PH= PFUNC, name, ids[i]); + VIRT_ADD_NEXT_INDEX_STRING(return_value, name); + } else { + DPRINTF("%s: Cannot get ID for running domain %d\n", PHPFU= NC, ids[i]); + } + } + rv =3D virDomainFree(domain); + if (rv !=3D 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainFree fai= led with %i on list_domain: %s", + rv, LIBVIRT_G(last_error)); + } else { + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, domai= n, 0 TSRMLS_CC); + } + domain =3D NULL; + } + efree(ids); + + expectedcount =3D virConnectNumOfDefinedDomains(conn->conn); + DPRINTF("%s: virConnectNumOfDefinedDomains returned %d domains\n", PHP= FUNC, expectedcount); + if (expectedcount < 0) { + DPRINTF("%s: virConnectNumOfDefinedDomains failed with error code = %d\n", PHPFUNC, expectedcount); + RETURN_FALSE; + } + + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virConnectListDefinedDomains(conn->conn, names, expectedcoun= t); + DPRINTF("%s: virConnectListDefinedDomains returned %d domains\n", PHPF= UNC, count); + if (count < 0) { + DPRINTF("%s: virConnectListDefinedDomains failed with error code %= d\n", PHPFUNC, count); + RETURN_FALSE; + } + + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + DPRINTF("%s: Found inactive domain %s\n", PHPFUNC, names[i]); + free(names[i]); + } + efree(names); +} + +/* + * Function name: libvirt_list_domain_resources + * Since version: 0.4.1(-1) + * Description: Function is used to list domain resources on the conne= ction + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt domain resources array for the connection + */ +PHP_FUNCTION(libvirt_list_domain_resources) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + int *ids; + char **names; + int i; + + virDomainPtr domain =3D NULL; + php_libvirt_domain *res_domain; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) + RETURN_FALSE; + + ids =3D (int *)emalloc(sizeof(int) * expectedcount); + count =3D virConnectListDomains(conn->conn, ids, expectedcount); + if ((count !=3D expectedcount) || (count < 0)) { + efree(ids); + RETURN_FALSE; + } + array_init(return_value); + for (i =3D 0; i < count; i++) { + domain =3D virDomainLookupByID(conn->conn, ids[i]); + if (domain !=3D NULL) { + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvir= t_domain)); + res_domain->domain =3D domain; + + res_domain->conn =3D conn; + + VIRT_REGISTER_LIST_RESOURCE(domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_d= omain->domain, 1 TSRMLS_CC); + } + } + efree(ids); + + if ((expectedcount =3D virConnectNumOfDefinedDomains(conn->conn)) < 0) + RETURN_FALSE; + + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virConnectListDefinedDomains(conn->conn, names, expectedcoun= t); + if ((count !=3D expectedcount) || (count < 0)) { + efree(names); + RETURN_FALSE; + } + for (i =3D 0; i < count; i++) { + domain =3D virDomainLookupByName(conn->conn, names[i]); + if (domain !=3D NULL) { + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvir= t_domain)); + res_domain->domain =3D domain; + + res_domain->conn =3D conn; + + VIRT_REGISTER_LIST_RESOURCE(domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_d= omain->domain, 1 TSRMLS_CC); + } + free(names[i]); + } + efree(names); +} + +/* + * Function name: libvirt_list_active_domain_ids + * Since version: 0.4.1(-1) + * Description: Function is used to list active domain IDs on the conn= ection + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt active domain ids array for the connection + */ +PHP_FUNCTION(libvirt_list_active_domain_ids) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + int *ids; + int i; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) + RETURN_FALSE; + + ids =3D (int *)emalloc(sizeof(int) * expectedcount); + count =3D virConnectListDomains(conn->conn, ids, expectedcount); + if ((count !=3D expectedcount) || (count < 0)) { + efree(ids); + RETURN_FALSE; + } + array_init(return_value); + for (i =3D 0; i < count; i++) + add_next_index_long(return_value, ids[i]); + efree(ids); +} + +/* + * Function name: libvirt_list_active_domains + * Since version: 0.4.1(-1) + * Description: Function is used to list active domain names on the co= nnection + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt active domain names array for the connection + */ +PHP_FUNCTION(libvirt_list_active_domains) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + int *ids; + int i; + virDomainPtr domain =3D NULL; + const char *name; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) + RETURN_FALSE; + + ids =3D (int *)emalloc(sizeof(int) * expectedcount); + count =3D virConnectListDomains(conn->conn, ids, expectedcount); + if ((count !=3D expectedcount) || (count < 0)) { + efree(ids); + RETURN_FALSE; + } + + array_init(return_value); + for (i =3D 0; i < count; i++) { + domain =3D virDomainLookupByID(conn->conn, ids[i]); + if (domain !=3D NULL) { + if (!(name =3D virDomainGetName(domain))) { + efree(ids); + RETURN_FALSE; + } + + VIRT_ADD_NEXT_INDEX_STRING(return_value, name); + + if (virDomainFree(domain)) + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, d= omain, 0 TSRMLS_CC); + } + } + efree(ids); +} + +/* + * Function name: libvirt_list_inactive_domains + * Since version: 0.4.1(-1) + * Description: Function is used to list inactive domain names on the = connection + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt inactive domain names array for the connection + */ +PHP_FUNCTION(libvirt_list_inactive_domains) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + char **names; + int i; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((expectedcount =3D virConnectNumOfDefinedDomains(conn->conn)) < 0) + RETURN_FALSE; + + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virConnectListDefinedDomains(conn->conn, names, expectedcoun= t); + if ((count !=3D expectedcount) || (count < 0)) { + efree(names); + RETURN_FALSE; + } + + array_init(return_value); + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + efree(names); +} + diff --git a/src/libvirt-domain.h b/src/libvirt-domain.h new file mode 100644 index 0000000..dc0ab46 --- /dev/null +++ b/src/libvirt-domain.h @@ -0,0 +1,208 @@ +/* + * libvirt-domain.h: The PHP bindings to libvirt domain API + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_DOMAIN_H__ +# define __LIBVIRT_DOMAIN_H__ + +# include "libvirt-connection.h" + +# define PHP_LIBVIRT_DOMAIN_RES_NAME "Libvirt domain" +# define INT_RESOURCE_DOMAIN 0x02 + +# define DOMAIN_DISK_FILE 0x01 +# define DOMAIN_DISK_BLOCK 0x02 +# define DOMAIN_DISK_ACCESS_ALL 0x04 + +# define DOMAIN_FLAG_FEATURE_ACPI 0x01 +# define DOMAIN_FLAG_FEATURE_APIC 0x02 +# define DOMAIN_FLAG_FEATURE_PAE 0x04 +# define DOMAIN_FLAG_CLOCK_LOCALTIME 0x08 +# define DOMAIN_FLAG_TEST_LOCAL_VNC 0x10 +# define DOMAIN_FLAG_SOUND_AC97 0x20 + +# define GET_DOMAIN_FROM_ARGS(args, ...) = \ + do { = \ + reset_error(TSRMLS_C); = \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, = \ + args, = \ + __VA_ARGS__) =3D=3D FAILURE) { = \ + set_error("Invalid arguments" TSRMLS_CC); = \ + RETURN_FALSE; = \ + } = \ + = \ + VIRT_FETCH_RESOURCE(domain, php_libvirt_domain*, &zdomain, = \ + PHP_LIBVIRT_DOMAIN_RES_NAME, le_libvirt_domain= ); \ + if (domain =3D=3D NULL || domain->domain =3D=3D NULL) = \ + RETURN_FALSE; = \ + } while (0) = \ + +# define PHP_FE_LIBVIRT_DOMAIN = \ + PHP_FE(libvirt_domain_new, arginfo_libvirt_domain_ne= w) \ + PHP_FE(libvirt_domain_new_get_vnc, arginfo_libvirt_void) = \ + PHP_FE(libvirt_domain_get_counts, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_is_persistent, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_lookup_by_name, arginfo_libvirt_conn_name= ) \ + PHP_FE(libvirt_domain_get_xml_desc, arginfo_libvirt_conn_xpat= h) \ + PHP_FE(libvirt_domain_get_disk_devices, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_get_interface_devices, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_change_vcpus, arginfo_libvirt_domain_ch= ange_vcpus) \ + PHP_FE(libvirt_domain_change_memory, arginfo_libvirt_domain_ch= ange_memory) \ + PHP_FE(libvirt_domain_change_boot_devices, arginfo_libvirt_domain_ch= ange_boot_devices) \ + PHP_FE(libvirt_domain_disk_add, arginfo_libvirt_domain_di= sk_add) \ + PHP_FE(libvirt_domain_disk_remove, arginfo_libvirt_domain_di= sk_remove) \ + PHP_FE(libvirt_domain_nic_add, arginfo_libvirt_domain_ni= c_add) \ + PHP_FE(libvirt_domain_nic_remove, arginfo_libvirt_domain_ni= c_remove) \ + PHP_FE(libvirt_domain_attach_device, arginfo_libvirt_domain_at= tach_device) \ + PHP_FE(libvirt_domain_detach_device, arginfo_libvirt_domain_de= tach_device) \ + PHP_FE(libvirt_domain_get_info, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_get_name, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_get_uuid, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_get_uuid_string, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_get_id, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_lookup_by_uuid, arginfo_libvirt_conn_uuid= ) \ + PHP_FE(libvirt_domain_lookup_by_uuid_string, arginfo_libvirt_conn_uuid= ) \ + PHP_FE(libvirt_domain_lookup_by_id, arginfo_libvirt_domain_lo= okup_by_id) \ + PHP_FE(libvirt_domain_create, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_destroy, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_resume, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_core_dump, arginfo_libvirt_domain_co= re_dump) \ + PHP_FE(libvirt_domain_shutdown, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_suspend, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_managedsave, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_undefine, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_reboot, arginfo_libvirt_conn_flag= s) \ + PHP_FE(libvirt_domain_define_xml, arginfo_libvirt_conn_xml)= \ + PHP_FE(libvirt_domain_create_xml, arginfo_libvirt_conn_xml)= \ + PHP_FE(libvirt_domain_xml_from_native, arginfo_libvirt_domain_xm= l_from_native) \ + PHP_FE(libvirt_domain_xml_to_native, arginfo_libvirt_domain_xm= l_to_native) \ + PHP_FE(libvirt_domain_memory_peek, arginfo_libvirt_domain_me= mory_peek) \ + PHP_FE(libvirt_domain_memory_stats, arginfo_libvirt_conn_flag= s) \ + PHP_FE(libvirt_domain_set_memory, arginfo_libvirt_domain_se= t_memory) \ + PHP_FE(libvirt_domain_set_max_memory, arginfo_libvirt_domain_se= t_memory) \ + PHP_FE(libvirt_domain_set_memory_flags, arginfo_libvirt_domain_se= t_memory_flags) \ + PHP_FE(libvirt_domain_block_commit, arginfo_libvirt_domain_bl= ock_commit) \ + PHP_FE(libvirt_domain_block_stats, arginfo_libvirt_conn_path= ) \ + PHP_FE(libvirt_domain_block_resize, arginfo_libvirt_domain_bl= ock_resize) \ + PHP_FE(libvirt_domain_block_job_info, arginfo_libvirt_domain_bl= ock_job_info) \ + PHP_FE(libvirt_domain_block_job_abort, arginfo_libvirt_domain_bl= ock_job_abort) \ + PHP_FE(libvirt_domain_block_job_set_speed, arginfo_libvirt_domain_bl= ock_job_set_speed) \ + PHP_FE(libvirt_domain_interface_stats, arginfo_libvirt_conn_path= ) \ + PHP_FE(libvirt_domain_get_connect, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_migrate, arginfo_libvirt_domain_mi= grate) \ + PHP_FE(libvirt_domain_migrate_to_uri, arginfo_libvirt_domain_mi= grate_to_uri) \ + PHP_FE(libvirt_domain_migrate_to_uri2, arginfo_libvirt_domain_mi= grate_to_uri2) \ + PHP_FE(libvirt_domain_get_job_info, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_xml_xpath, arginfo_libvirt_domain_xm= l_xpath) \ + PHP_FE(libvirt_domain_get_block_info, arginfo_libvirt_domain_ge= t_block_info) \ + PHP_FE(libvirt_domain_get_network_info, arginfo_libvirt_domain_ge= t_network_info) \ + PHP_FE(libvirt_domain_get_autostart, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_set_autostart, arginfo_libvirt_conn_flag= s) \ + PHP_FE(libvirt_domain_get_metadata, arginfo_libvirt_domain_ge= t_metadata) \ + PHP_FE(libvirt_domain_set_metadata, arginfo_libvirt_domain_se= t_metadata) \ + PHP_FE(libvirt_domain_is_active, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_get_next_dev_ids, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_get_screenshot, arginfo_libvirt_domain_ge= t_screenshot) \ + PHP_FE(libvirt_domain_get_screenshot_api, arginfo_libvirt_domain_ge= t_screenshot_api) \ + PHP_FE(libvirt_domain_get_screen_dimensions, arginfo_libvirt_domain_ge= t_screen_dimensions) \ + PHP_FE(libvirt_domain_send_keys, arginfo_libvirt_domain_se= nd_keys) \ + PHP_FE(libvirt_domain_send_key_api, arginfo_libvirt_domain_se= nd_key_api) \ + PHP_FE(libvirt_domain_send_pointer_event, arginfo_libvirt_domain_se= nd_pointer_event) \ + PHP_FE(libvirt_domain_update_device, arginfo_libvirt_domain_up= date_device) \ + PHP_FE(libvirt_domain_qemu_agent_command, arginfo_libvirt_domain_qe= mu_agent_command) \ + PHP_FE(libvirt_list_domains, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_list_domain_resources, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_list_active_domain_ids, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_list_active_domains, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_list_inactive_domains, arginfo_libvirt_conn) + +int le_libvirt_domain; + +typedef struct _php_libvirt_domain { + virDomainPtr domain; + php_libvirt_connection* conn; +} php_libvirt_domain; + +void php_libvirt_domain_dtor(virt_resource *rsrc TSRMLS_DC); + +PHP_FUNCTION(libvirt_domain_new); +PHP_FUNCTION(libvirt_domain_new_get_vnc); +PHP_FUNCTION(libvirt_domain_get_counts); +PHP_FUNCTION(libvirt_domain_is_persistent); +PHP_FUNCTION(libvirt_domain_lookup_by_name); +PHP_FUNCTION(libvirt_domain_get_xml_desc); +PHP_FUNCTION(libvirt_domain_get_disk_devices); +PHP_FUNCTION(libvirt_domain_get_interface_devices); +PHP_FUNCTION(libvirt_domain_get_screenshot); +PHP_FUNCTION(libvirt_domain_get_screenshot_api); +PHP_FUNCTION(libvirt_domain_get_screen_dimensions); +PHP_FUNCTION(libvirt_domain_change_vcpus); +PHP_FUNCTION(libvirt_domain_change_memory); +PHP_FUNCTION(libvirt_domain_change_boot_devices); +PHP_FUNCTION(libvirt_domain_disk_add); +PHP_FUNCTION(libvirt_domain_disk_remove); +PHP_FUNCTION(libvirt_domain_nic_add); +PHP_FUNCTION(libvirt_domain_nic_remove); +PHP_FUNCTION(libvirt_domain_attach_device); +PHP_FUNCTION(libvirt_domain_detach_device); +PHP_FUNCTION(libvirt_domain_get_info); +PHP_FUNCTION(libvirt_domain_get_uuid); +PHP_FUNCTION(libvirt_domain_get_uuid_string); +PHP_FUNCTION(libvirt_domain_get_name); +PHP_FUNCTION(libvirt_domain_get_id); +PHP_FUNCTION(libvirt_domain_lookup_by_uuid); +PHP_FUNCTION(libvirt_domain_lookup_by_uuid_string); +PHP_FUNCTION(libvirt_domain_lookup_by_id); +PHP_FUNCTION(libvirt_domain_create); +PHP_FUNCTION(libvirt_domain_destroy); +PHP_FUNCTION(libvirt_domain_resume); +PHP_FUNCTION(libvirt_domain_core_dump); +PHP_FUNCTION(libvirt_domain_shutdown); +PHP_FUNCTION(libvirt_domain_suspend); +PHP_FUNCTION(libvirt_domain_managedsave); +PHP_FUNCTION(libvirt_domain_undefine); +PHP_FUNCTION(libvirt_domain_reboot); +PHP_FUNCTION(libvirt_domain_define_xml); +PHP_FUNCTION(libvirt_domain_create_xml); +PHP_FUNCTION(libvirt_domain_xml_from_native); +PHP_FUNCTION(libvirt_domain_xml_to_native); +PHP_FUNCTION(libvirt_domain_set_max_memory); +PHP_FUNCTION(libvirt_domain_set_memory); +PHP_FUNCTION(libvirt_domain_set_memory_flags); +PHP_FUNCTION(libvirt_domain_memory_peek); +PHP_FUNCTION(libvirt_domain_memory_stats); +PHP_FUNCTION(libvirt_domain_block_commit); +PHP_FUNCTION(libvirt_domain_block_stats); +PHP_FUNCTION(libvirt_domain_block_resize); +PHP_FUNCTION(libvirt_domain_block_job_info); +PHP_FUNCTION(libvirt_domain_block_job_abort); +PHP_FUNCTION(libvirt_domain_block_job_set_speed); +PHP_FUNCTION(libvirt_domain_interface_stats); +PHP_FUNCTION(libvirt_domain_get_connect); +PHP_FUNCTION(libvirt_domain_migrate); +PHP_FUNCTION(libvirt_domain_migrate_to_uri); +PHP_FUNCTION(libvirt_domain_migrate_to_uri2); +PHP_FUNCTION(libvirt_domain_get_job_info); +PHP_FUNCTION(libvirt_domain_xml_xpath); +PHP_FUNCTION(libvirt_domain_get_block_info); +PHP_FUNCTION(libvirt_domain_get_network_info); +PHP_FUNCTION(libvirt_domain_get_autostart); +PHP_FUNCTION(libvirt_domain_set_autostart); +PHP_FUNCTION(libvirt_domain_get_metadata); +PHP_FUNCTION(libvirt_domain_set_metadata); +PHP_FUNCTION(libvirt_domain_is_active); +PHP_FUNCTION(libvirt_domain_get_next_dev_ids); +PHP_FUNCTION(libvirt_domain_send_keys); +PHP_FUNCTION(libvirt_domain_send_key_api); +PHP_FUNCTION(libvirt_domain_send_pointer_event); +PHP_FUNCTION(libvirt_domain_update_device); +PHP_FUNCTION(libvirt_domain_qemu_agent_command); +PHP_FUNCTION(libvirt_list_domains); +PHP_FUNCTION(libvirt_list_domain_resources); +PHP_FUNCTION(libvirt_list_active_domain_ids); +PHP_FUNCTION(libvirt_list_active_domains); +PHP_FUNCTION(libvirt_list_inactive_domains); + +#endif diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 80491da..05f36ae 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -23,6 +23,7 @@ #include "libvirt-connection.h" #include "libvirt-node.h" #include "libvirt-stream.h" +#include "libvirt-domain.h" =20 DEBUG_INIT("core"); =20 @@ -36,7 +37,6 @@ const char *features_binaries[] =3D { NULL }; #endif =20 /* ZEND thread safe per request globals definition */ -int le_libvirt_domain; int le_libvirt_storagepool; int le_libvirt_volume; int le_libvirt_network; @@ -477,78 +477,7 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE(libvirt_get_last_error, arginfo_libvirt_void) PHP_FE_LIBVIRT_CONNECTION PHP_FE_LIBVIRT_STREAM - /* Domain functions */ - PHP_FE(libvirt_domain_new, arginfo_libvirt_domain_ne= w) - PHP_FE(libvirt_domain_new_get_vnc, arginfo_libvirt_void) - PHP_FE(libvirt_domain_get_counts, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_is_persistent, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_lookup_by_name, arginfo_libvirt_conn_name) - PHP_FE(libvirt_domain_get_xml_desc, arginfo_libvirt_conn_xpat= h) - PHP_FE(libvirt_domain_get_disk_devices, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_get_interface_devices, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_change_vcpus, arginfo_libvirt_domain_ch= ange_vcpus) - PHP_FE(libvirt_domain_change_memory, arginfo_libvirt_domain_ch= ange_memory) - PHP_FE(libvirt_domain_change_boot_devices, arginfo_libvirt_domain_ch= ange_boot_devices) - PHP_FE(libvirt_domain_disk_add, arginfo_libvirt_domain_di= sk_add) - PHP_FE(libvirt_domain_disk_remove, arginfo_libvirt_domain_di= sk_remove) - PHP_FE(libvirt_domain_nic_add, arginfo_libvirt_domain_ni= c_add) - PHP_FE(libvirt_domain_nic_remove, arginfo_libvirt_domain_ni= c_remove) - PHP_FE(libvirt_domain_attach_device, arginfo_libvirt_domain_at= tach_device) - PHP_FE(libvirt_domain_detach_device, arginfo_libvirt_domain_de= tach_device) - PHP_FE(libvirt_domain_get_info, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_get_name, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_get_uuid, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_get_uuid_string, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_get_id, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_lookup_by_id, arginfo_libvirt_domain_lo= okup_by_id) - PHP_FE(libvirt_domain_lookup_by_uuid, arginfo_libvirt_conn_uuid) - PHP_FE(libvirt_domain_lookup_by_uuid_string, arginfo_libvirt_conn_uuid) - PHP_FE(libvirt_domain_destroy, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_create, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_resume, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_core_dump, arginfo_libvirt_domain_co= re_dump) - PHP_FE(libvirt_domain_shutdown, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_suspend, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_managedsave, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_undefine, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_reboot, arginfo_libvirt_conn_flag= s) - PHP_FE(libvirt_domain_define_xml, arginfo_libvirt_conn_xml) - PHP_FE(libvirt_domain_create_xml, arginfo_libvirt_conn_xml) - PHP_FE(libvirt_domain_xml_from_native, arginfo_libvirt_domain_xm= l_from_native) - PHP_FE(libvirt_domain_xml_to_native, arginfo_libvirt_domain_xm= l_to_native) - PHP_FE(libvirt_domain_memory_peek, arginfo_libvirt_domain_me= mory_peek) - PHP_FE(libvirt_domain_memory_stats, arginfo_libvirt_conn_flag= s) - PHP_FE(libvirt_domain_set_memory, arginfo_libvirt_domain_se= t_memory) - PHP_FE(libvirt_domain_set_max_memory, arginfo_libvirt_domain_se= t_memory) - PHP_FE(libvirt_domain_set_memory_flags, arginfo_libvirt_domain_se= t_memory_flags) - PHP_FE(libvirt_domain_block_commit, arginfo_libvirt_domain_bl= ock_commit) - PHP_FE(libvirt_domain_block_stats, arginfo_libvirt_conn_path) - PHP_FE(libvirt_domain_block_resize, arginfo_libvirt_domain_bl= ock_resize) - PHP_FE(libvirt_domain_block_job_abort, arginfo_libvirt_domain_bl= ock_job_abort) - PHP_FE(libvirt_domain_block_job_set_speed, arginfo_libvirt_domain_bl= ock_job_set_speed) - PHP_FE(libvirt_domain_block_job_info, arginfo_libvirt_domain_bl= ock_job_info) - PHP_FE(libvirt_domain_interface_stats, arginfo_libvirt_conn_path) - PHP_FE(libvirt_domain_get_connect, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_migrate, arginfo_libvirt_domain_mi= grate) - PHP_FE(libvirt_domain_migrate_to_uri, arginfo_libvirt_domain_mi= grate_to_uri) - PHP_FE(libvirt_domain_migrate_to_uri2, arginfo_libvirt_domain_mi= grate_to_uri2) - PHP_FE(libvirt_domain_get_job_info, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_xml_xpath, arginfo_libvirt_domain_xm= l_xpath) - PHP_FE(libvirt_domain_get_block_info, arginfo_libvirt_domain_ge= t_block_info) - PHP_FE(libvirt_domain_get_network_info, arginfo_libvirt_domain_ge= t_network_info) - PHP_FE(libvirt_domain_get_autostart, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_set_autostart, arginfo_libvirt_conn_flag= s) - PHP_FE(libvirt_domain_get_metadata, arginfo_libvirt_domain_ge= t_metadata) - PHP_FE(libvirt_domain_set_metadata, arginfo_libvirt_domain_se= t_metadata) - PHP_FE(libvirt_domain_is_active, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_get_next_dev_ids, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_get_screenshot, arginfo_libvirt_domain_ge= t_screenshot) - PHP_FE(libvirt_domain_get_screenshot_api, arginfo_libvirt_domain_ge= t_screenshot_api) - PHP_FE(libvirt_domain_get_screen_dimensions, arginfo_libvirt_domain_ge= t_screen_dimensions) - PHP_FE(libvirt_domain_send_keys, arginfo_libvirt_domain_se= nd_keys) - PHP_FE(libvirt_domain_send_key_api, arginfo_libvirt_domain_se= nd_key_api) - PHP_FE(libvirt_domain_send_pointer_event, arginfo_libvirt_domain_se= nd_pointer_event) - PHP_FE(libvirt_domain_update_device, arginfo_libvirt_domain_up= date_device) + PHP_FE_LIBVIRT_DOMAIN /* Domain snapshot functions */ PHP_FE(libvirt_domain_has_current_snapshot, arginfo_libvirt_conn_optf= lags) PHP_FE(libvirt_domain_snapshot_create, arginfo_libvirt_conn_optf= lags) @@ -619,9 +548,7 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE(libvirt_nwfilter_lookup_by_uuid_string, arginfo_libvirt_conn_uu= id) PHP_FE(libvirt_nwfilter_lookup_by_uuid, arginfo_libvirt_conn_uuid) /* List functions */ - PHP_FE(libvirt_list_domains, arginfo_libvirt_conn) PHP_FE(libvirt_list_domain_snapshots, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_list_domain_resources, arginfo_libvirt_conn) PHP_FE(libvirt_list_nodedevs, arginfo_libvirt_conn_optc= ap) PHP_FE(libvirt_list_all_networks, arginfo_libvirt_conn_optf= lags) PHP_FE(libvirt_list_networks, arginfo_libvirt_conn_optf= lags) @@ -629,9 +556,6 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE(libvirt_list_active_storagepools, arginfo_libvirt_conn) PHP_FE(libvirt_list_inactive_storagepools, arginfo_libvirt_conn) PHP_FE(libvirt_storagepool_list_volumes, arginfo_libvirt_conn) - PHP_FE(libvirt_list_active_domains, arginfo_libvirt_conn) - PHP_FE(libvirt_list_active_domain_ids, arginfo_libvirt_conn) - PHP_FE(libvirt_list_inactive_domains, arginfo_libvirt_conn) PHP_FE(libvirt_list_all_nwfilters, arginfo_libvirt_conn) PHP_FE(libvirt_list_nwfilters, arginfo_libvirt_conn) /* Version information and common function */ @@ -644,13 +568,7 @@ static zend_function_entry libvirt_functions[] =3D { /* Debugging functions */ PHP_FE(libvirt_logfile_set, arginfo_libvirt_logfile_s= et) PHP_FE(libvirt_print_binding_resources, arginfo_libvirt_void) - /* Agent functions */ - PHP_FE(libvirt_domain_qemu_agent_command, arginfo_libvirt_domain_qe= mu_agent_command) -#ifdef PHP_FE_END PHP_FE_END -#else - {NULL, NULL, NULL} -#endif }; =20 =20 @@ -1321,34 +1239,6 @@ int is_local_connection(virConnectPtr conn) #endif } =20 -/* Destructor for domain resource */ -static void php_libvirt_domain_dtor(virt_resource *rsrc TSRMLS_DC) -{ - php_libvirt_domain *domain =3D (php_libvirt_domain *)rsrc->ptr; - int rv =3D 0; - - if (domain !=3D NULL) { - if (domain->domain !=3D NULL) { - if (!check_resource_allocation(domain->conn->conn, INT_RESOURC= E_DOMAIN, domain->domain TSRMLS_CC)) { - domain->domain =3D NULL; - efree(domain); - return; - } - - rv =3D virDomainFree(domain->domain); - if (rv !=3D 0) { - DPRINTF("%s: virDomainFree(%p) returned %d (%s)\n", __FUNC= TION__, domain->domain, rv, LIBVIRT_G(last_error)); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainFree= failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); - } else { - DPRINTF("%s: virDomainFree(%p) completed successfully\n", = __FUNCTION__, domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, domain->conn-= >conn, domain->domain, 0 TSRMLS_CC); - } - domain->domain =3D NULL; - } - efree(domain); - } -} - /* Destructor for storagepool resource */ static void php_libvirt_storagepool_dtor(virt_resource *rsrc TSRMLS_DC) { @@ -1837,19 +1727,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) } =20 /* Macros for obtaining resources from arguments */ -#define GET_DOMAIN_FROM_ARGS(args, ...) = \ - do { = \ - reset_error(TSRMLS_C); = \ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_AR= GS__) =3D=3D FAILURE) { \ - set_error("Invalid arguments" TSRMLS_CC); = \ - RETURN_FALSE; = \ - } = \ - = \ - VIRT_FETCH_RESOURCE(domain, php_libvirt_domain*, &zdomain, PHP_LIB= VIRT_DOMAIN_RES_NAME, le_libvirt_domain);\ - if ((domain =3D=3D NULL) || (domain->domain =3D=3D NULL)) = \ - RETURN_FALSE; = \ - } while (0) = \ - #define GET_NETWORK_FROM_ARGS(args, ...) = \ do { = \ reset_error(TSRMLS_C); = \ @@ -2831,3307 +2708,270 @@ char *installation_get_xml(int step, virConnectP= tr conn, char *name, int memMB, return (strlen(xml) > 0) ? strdup(xml) : NULL; } =20 -/* Domain functions */ - /* - * Function name: libvirt_domain_get_counts - * Since version: 0.4.1(-1) - * Description: Function is getting domain counts for all, active and = inactive domains - * Arguments: @conn [resource]: libvirt connection resource from lib= virt_connect() - * Returns: array of total, active and inactive (but defined) doma= in counts + * Private function name: streamSink + * Since version: 0.4.5 + * Description: Function to write stream to file, borrowed fro= m libvirt + * Arguments: @st [virStreamPtr]: stream pointer + * @bytes [void *]: buffer array + * @nbytes [int]: size of buffer + * @opaque [void *]: used for file descriptor + * Returns: write() error code as it's calling write */ -PHP_FUNCTION(libvirt_domain_get_counts) +int streamSink(virStreamPtr st ATTRIBUTE_UNUSED, + const char *bytes, size_t nbytes, void *opaque) { - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count_defined; - int count_active; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - count_defined =3D virConnectNumOfDefinedDomains(conn->conn); - count_active =3D virConnectNumOfDomains(conn->conn); + int *fd =3D (int *)opaque; =20 - array_init(return_value); - add_assoc_long(return_value, "total", (long)(count_defined + count_act= ive)); - add_assoc_long(return_value, "active", (long)count_active); - add_assoc_long(return_value, "inactive", (long)count_defined); + return write(*fd, bytes, nbytes); } =20 -/* - * Function name: libvirt_domain_is_persistent - * Since version: 0.4.9 - * Description: Function to get information whether domain is persiste= nt or not - * Arguments: @res [resource]: libvirt domain resource - * Returns: TRUE for persistent, FALSE for not persistent, -1 on e= rror - */ -PHP_FUNCTION(libvirt_domain_is_persistent) +void parse_array(zval *arr, tVMDisk *disk, tVMNetwork *network) { - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int p; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); + HashTable *arr_hash; + // int array_count; + zval *data; + php_libvirt_hash_key_info key; + HashPosition pointer; + unsigned long index; =20 - if ((p =3D virDomainIsPersistent(domain->domain)) < 0) - RETURN_LONG(-1); + arr_hash =3D Z_ARRVAL_P(arr); + //array_count =3D zend_hash_num_elements(arr_hash); =20 - if (p =3D=3D 1) - RETURN_TRUE; + if (disk !=3D NULL) + memset(disk, 0, sizeof(tVMDisk)); + if (network !=3D NULL) + memset(network, 0, sizeof(tVMNetwork)); =20 - RETURN_FALSE; + VIRT_FOREACH(arr_hash, pointer, data) { + if ((Z_TYPE_P(data) =3D=3D IS_STRING) || (Z_TYPE_P(data) =3D=3D IS= _LONG)) { + VIRT_HASH_CURRENT_KEY_INFO(arr_hash, pointer, index, key); + if (key.type =3D=3D HASH_KEY_IS_STRING) { + if (disk !=3D NULL) { + if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(key.na= me, "path") =3D=3D 0) + disk->path =3D strdup(Z_STRVAL_P(data)); + else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(k= ey.name, "driver") =3D=3D 0) + disk->driver =3D strdup(Z_STRVAL_P(data)); + else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(k= ey.name, "bus") =3D=3D 0) + disk->bus =3D strdup(Z_STRVAL_P(data)); + else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(k= ey.name, "dev") =3D=3D 0) + disk->dev =3D strdup(Z_STRVAL_P(data)); + else if (strcmp(key.name, "size") =3D=3D 0) { + if (Z_TYPE_P(data) =3D=3D IS_LONG) { + disk->size =3D Z_LVAL_P(data); + } else { + disk->size =3D size_def_to_mbytes(Z_STRVAL_P(d= ata)); + } + } else if ((Z_TYPE_P(data) =3D=3D IS_LONG) && strcmp(k= ey.name, "flags") =3D=3D 0) + disk->flags =3D Z_LVAL_P(data); + } else { + if (network !=3D NULL) { + if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(ke= y.name, "mac") =3D=3D 0) + network->mac =3D strdup(Z_STRVAL_P(data)); + else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strc= mp(key.name, "network") =3D=3D 0) + network->network =3D strdup(Z_STRVAL_P(data)); + else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strc= mp(key.name, "model") =3D=3D 0) + network->model =3D strdup(Z_STRVAL_P(data)); + } + } + } + } + } VIRT_FOREACH_END(); } =20 /* - * Function name: libvirt_domain_set_max_memory - * Since version: 0.5.1 - * Description: Function to set max memory for domain + * Function name: libvirt_domain_has_current_snapshot + * Since version: 0.4.1(-2) + * Description: Function is used to get the information whether domain= has the current snapshot * Arguments: @res [resource]: libvirt domain resource - * @memory [int]: memory size in 1024 bytes (Kb) - * Returns: TRUE for success, FALSE for failure + * @flags [int]: libvirt snapshot flags + * Returns: TRUE is domain has the current snapshot, otherwise FAL= SE (you may need to check for error using libvirt_get_last_error()) */ -PHP_FUNCTION(libvirt_domain_set_max_memory) +PHP_FUNCTION(libvirt_domain_has_current_snapshot) { php_libvirt_domain *domain =3D NULL; zval *zdomain; - zend_long memory =3D 0; + int retval; + zend_long flags =3D 0; =20 - GET_DOMAIN_FROM_ARGS("rl", &zdomain, &memory); + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); =20 - if (virDomainSetMaxMemory(domain->domain, memory) !=3D 0) + retval =3D virDomainHasCurrentSnapshot(domain->domain, flags); + if (retval <=3D 0) RETURN_FALSE; - RETURN_TRUE; } =20 /* - * Function name: libvirt_domain_set_memory - * Since version: 0.5.1 - * Description: Function to set memory for domain + * Function name: libvirt_domain_snapshot_lookup_by_name + * Since version: 0.4.1(-2) + * Description: This functions is used to lookup for the snapshot by i= t's name * Arguments: @res [resource]: libvirt domain resource - * @memory [int]: memory size in 1024 bytes (Kb) - * Returns: TRUE for success, FALSE for failure + * @name [string]: name of the snapshot to get the resour= ce + * @flags [int]: libvirt snapshot flags + * Returns: domain snapshot resource */ -PHP_FUNCTION(libvirt_domain_set_memory) +PHP_FUNCTION(libvirt_domain_snapshot_lookup_by_name) { php_libvirt_domain *domain =3D NULL; zval *zdomain; - zend_long memory =3D 0; + strsize_t name_len; + char *name =3D NULL; + zend_long flags =3D 0; + php_libvirt_snapshot *res_snapshot; + virDomainSnapshotPtr snapshot =3D NULL; =20 - GET_DOMAIN_FROM_ARGS("rl", &zdomain, &memory); + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &name, &name_len, &flags); =20 - if (virDomainSetMemory(domain->domain, memory) !=3D 0) + if ((name =3D=3D NULL) || (name_len < 1)) + RETURN_FALSE; + snapshot=3DvirDomainSnapshotLookupByName(domain->domain, name, flags); + if (snapshot =3D=3D NULL) RETURN_FALSE; =20 - RETURN_TRUE; + res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); + res_snapshot->domain =3D domain; + res_snapshot->snapshot =3D snapshot; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); + resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); } =20 /* - * Function name: libvirt_domain_set_memory_flags - * Since version: 0.5.1 - * Description: Function to set max memory for domain + * Function name: libvirt_domain_snapshot_create + * Since version: 0.4.1(-2) + * Description: This function creates the domain snapshot for the doma= in identified by it's resource * Arguments: @res [resource]: libvirt domain resource - * @memory [int]: memory size in 1024 bytes (Kb) - * @flags [int]: bitwise-OR VIR_DOMAIN_MEM_* flags - * Returns: TRUE for success, FALSE for failure + * @flags [int]: libvirt snapshot flags + * Returns: domain snapshot resource */ -PHP_FUNCTION(libvirt_domain_set_memory_flags) +PHP_FUNCTION(libvirt_domain_snapshot_create) { php_libvirt_domain *domain =3D NULL; + php_libvirt_snapshot *res_snapshot; zval *zdomain; - zend_long memory =3D 0; + virDomainSnapshotPtr snapshot =3D NULL; zend_long flags =3D 0; =20 - GET_DOMAIN_FROM_ARGS("rl|l", &zdomain, &memory, &flags); + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); =20 - if (virDomainSetMemoryFlags(domain->domain, memory, flags) !=3D 0) + snapshot =3D virDomainSnapshotCreateXML(domain->domain, "", flags); + DPRINTF("%s: virDomainSnapshotCreateXML(%p, ) returned %p\n", PHP= FUNC, domain->domain, snapshot); + if (snapshot =3D=3D NULL) RETURN_FALSE; =20 - RETURN_TRUE; + res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); + res_snapshot->domain =3D domain; + res_snapshot->snapshot =3D snapshot; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); + resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); } =20 /* - * Function name: libvirt_domain_get_autostart - * Since version: 0.4.1(-1) - * Description: Function is getting the autostart value for the domain - * Arguments: @res [resource]: libvirt domain resource - * Returns: autostart value or -1 + * Function name: libvirt_domain_snapshot_get_xml + * Since version: 0.4.1(-2) + * Description: Function is used to get the XML description of the sna= pshot identified by it's resource + * Arguments: @res [resource]: libvirt snapshot resource + * @flags [int]: libvirt snapshot flags + * Returns: XML description string for the snapshot */ -PHP_FUNCTION(libvirt_domain_get_autostart) +PHP_FUNCTION(libvirt_domain_snapshot_get_xml) { - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int flags =3D 0; + char *xml; + zval *zsnapshot; + php_libvirt_snapshot *snapshot; + zend_long flags =3D 0; =20 - GET_DOMAIN_FROM_ARGS("r", &zdomain); + GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); =20 - if (virDomainGetAutostart (domain->domain, &flags) !=3D 0) { - RETURN_LONG(-1); - } - RETURN_LONG((long)flags); + xml =3D virDomainSnapshotGetXMLDesc(snapshot->snapshot, flags); + if (xml =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETVAL_STRING(xml); + free(xml); } =20 /* - * Function name: libvirt_domain_set_autostart - * Since version: 0.4.1(-1) - * Description: Function is setting the autostart value for the domain - * Arguments: @res [resource]: libvirt domain resource - * @flags [int]: flag to enable/disable autostart + * Function name: libvirt_domain_snapshot_revert + * Since version: 0.4.1(-2) + * Description: Function is used to revert the domain state to the sta= te identified by the snapshot + * Arguments: @res [resource]: libvirt snapshot resource + * @flags [int]: libvirt snapshot flags * Returns: TRUE on success, FALSE on error */ -PHP_FUNCTION(libvirt_domain_set_autostart) +PHP_FUNCTION(libvirt_domain_snapshot_revert) { - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - zend_bool flags =3D 0; + zval *zsnapshot; + php_libvirt_snapshot *snapshot; + int ret; + zend_long flags =3D 0; =20 - GET_DOMAIN_FROM_ARGS("rb", &zdomain, &flags); + GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); =20 - if (virDomainSetAutostart (domain->domain, flags) !=3D 0) { + ret =3D virDomainRevertToSnapshot(snapshot->snapshot, flags); + DPRINTF("%s: virDomainRevertToSnapshot(%p, 0) returned %d\n", PHPFUNC,= snapshot->snapshot, ret); + if (ret =3D=3D -1) RETURN_FALSE; - } RETURN_TRUE; } =20 /* - * Function name: libvirt_domain_get_metadata - * Since version: 0.4.9 - * Description: Function retrieve appropriate domain element given by = @type. - * Arguments: @res [resource]: libvirt domain resource - * @type [int]: virDomainMetadataType type of description - * @uri [string]: XML namespace identifier - * @flags [int]: bitwise-OR of virDomainModificationImpact - * Returns: metadata string, NULL on error or FALSE on API not sup= ported + * Function name: libvirt_domain_snapshot_delete + * Since version: 0.4.1(-2) + * Description: Function is used to revert the domain state to the sta= te identified by the snapshot + * Arguments: @res [resource]: libvirt snapshot resource + * @flags [int]: 0 to delete just snapshot, VIR_SNAPSHOT_= DELETE_CHILDREN to delete snapshot children as well + * Returns: TRUE on success, FALSE on error */ -PHP_FUNCTION(libvirt_domain_get_metadata) +PHP_FUNCTION(libvirt_domain_snapshot_delete) { - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - zend_long type =3D 0; + zval *zsnapshot; + php_libvirt_snapshot *snapshot; zend_long flags =3D 0; - char *uri =3D NULL; - strsize_t uri_len; - char *ret =3D NULL; - - GET_DOMAIN_FROM_ARGS("rlsl", &zdomain, &type, &uri, &uri_len, &flags); + int retval; =20 - if ((uri !=3D NULL) && (strlen(uri) =3D=3D 0)) - uri =3D NULL; + GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); =20 - ret =3D virDomainGetMetadata(domain->domain, type, uri, flags); - if (ret =3D=3D NULL) { - if (strstr(LIBVIRT_G(last_error), "not supported") !=3D NULL) - RETURN_FALSE; - RETURN_NULL(); - } else { - VIRT_RETVAL_STRING(ret); - free(ret); - } + retval =3D virDomainSnapshotDelete(snapshot->snapshot, flags); + DPRINTF("%s: virDomainSnapshotDelete(%p, %d) returned %d\n", PHPFUNC, = snapshot->snapshot, (int) flags, retval); + if (retval =3D=3D -1) + RETURN_FALSE; + RETURN_TRUE; } =20 /* - * Function name: libvirt_domain_set_metadata - * Since version: 0.4.9 - * Description: Function sets the appropriate domain element given by = @type to the value of @description. No new lines are permitted. + * Function name: libvirt_list_domain_snapshots + * Since version: 0.4.1(-2) + * Description: Function is used to list domain snapshots for the doma= in specified by it's resource * Arguments: @res [resource]: libvirt domain resource - * @type [int]: virDomainMetadataType type of description - * @metadata [string]: new metadata text - * @key [string]: XML namespace key or empty string (alia= s of NULL) - * @uri [string]: XML namespace identifier or empty strin= g (alias of NULL) - * @flags [int]: bitwise-OR of virDomainModificationImpact - * Returns: -1 on error, 0 on success + * @flags [int]: libvirt snapshot flags + * Returns: libvirt domain snapshot names array */ -PHP_FUNCTION(libvirt_domain_set_metadata) +PHP_FUNCTION(libvirt_list_domain_snapshots) { php_libvirt_domain *domain =3D NULL; zval *zdomain; - strsize_t metadata_len, key_len, uri_len; - char *metadata =3D NULL; - char *key =3D NULL; - char *uri =3D NULL; - zend_long type =3D 0; + int count =3D -1; + int expectedcount =3D -1; + char **names; zend_long flags =3D 0; - int rc; - - GET_DOMAIN_FROM_ARGS("rlsssl", &zdomain, &type, &metadata, &metadata_l= en, &key, &key_len, &uri, &uri_len, &flags); - - if ((key !=3D NULL) && (strlen(key) =3D=3D 0)) - key =3D NULL; - if ((uri !=3D NULL) && (strlen(uri) =3D=3D 0)) - uri =3D NULL; + int i; =20 - rc =3D virDomainSetMetadata(domain->domain, type, metadata, key, uri, = flags); - RETURN_LONG(rc); -} + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); =20 -/* - * Function name: libvirt_domain_is_active - * Since version: 0.4.1(-1) - * Description: Function is getting information whether domain identif= ied by resource is active or not - * Arguments: @res [resource]: libvirt domain resource - * Returns: virDomainIsActive() result on the domain - */ -PHP_FUNCTION(libvirt_domain_is_active) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; + expectedcount =3D virDomainSnapshotNum(domain->domain, flags); + DPRINTF("%s: virDomainSnapshotNum(%p, 0) returned %d\n", PHPFUNC, doma= in->domain, expectedcount); =20 - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - RETURN_LONG(virDomainIsActive(domain->domain)); -} - -/* - * Function name: libvirt_domain_lookup_by_name - * Since version: 0.4.1(-1) - * Description: Function is used to lookup for domain by it's name - * Arguments: @res [resource]: libvirt connection resource from libv= irt_connect() - * @name [string]: domain name to look for - * Returns: libvirt domain resource - */ -PHP_FUNCTION(libvirt_domain_lookup_by_name) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - strsize_t name_len; - char *name =3D NULL; - virDomainPtr domain =3D NULL; - php_libvirt_domain *res_domain; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); - if ((name =3D=3D NULL) || (name_len < 1)) - RETURN_FALSE; - domain =3D virDomainLookupByName(conn->conn, name); - if (domain =3D=3D NULL) - RETURN_FALSE; - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D domain; - res_domain->conn =3D conn; - - DPRINTF("%s: domain name =3D '%s', returning %p\n", PHPFUNC, name, res= _domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_lookup_by_uuid - * Since version: 0.4.1(-1) - * Description: Function is used to lookup for domain by it's UUID in = the binary format - * Arguments: @res [resource]: libvirt connection resource from libv= irt_connect() - * @uuid [string]: binary defined UUID to look for - * Returns: libvirt domain resource - */ -PHP_FUNCTION(libvirt_domain_lookup_by_uuid) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - strsize_t uuid_len; - unsigned char *uuid =3D NULL; - virDomainPtr domain =3D NULL; - php_libvirt_domain *res_domain; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); - - if ((uuid =3D=3D NULL) || (uuid_len < 1)) - RETURN_FALSE; - domain =3D virDomainLookupByUUID(conn->conn, uuid); - if (domain =3D=3D NULL) - RETURN_FALSE; - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D domain; - res_domain->conn =3D conn; - - DPRINTF("%s: domain UUID =3D '%s', returning %p\n", PHPFUNC, uuid, res= _domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_qemu_agent_command - * Since version: 0.5.2(-1) - * Description: Function is used to send qemu-ga command - * Arguments: @res [resource]: libvirt domain resource, e.g. from lib= virt_domain_lookup_by_*() - * @cmd [string]: command - * @timeout [int] timeout for waiting (-2 block, -1 defaul= t, 0 no wait, >0 wait specific time - * @flags [int]: unknown - * Returns: String on success and FALSE on error - */ -PHP_FUNCTION(libvirt_domain_qemu_agent_command) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - const char *cmd; - strsize_t cmd_len; - char *ret; - zend_long timeout =3D -1; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("rs|ll", &zdomain, &cmd, &cmd_len, &timeout, &fla= gs); - - ret =3D virDomainQemuAgentCommand(domain->domain, cmd, timeout, flags); - if (ret =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETVAL_STRING(ret); - free(ret); -} - -/* - * Function name: libvirt_domain_lookup_by_uuid_string - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain by it's UUID that's= accepted in string format - * Arguments: @res [resource]: libvirt connection resource from libv= irt_connect() - * @uuid [string]: domain UUID [in string format] to look= for - * Returns: libvirt domain resource - */ -PHP_FUNCTION(libvirt_domain_lookup_by_uuid_string) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - strsize_t uuid_len; - char *uuid =3D NULL; - virDomainPtr domain =3D NULL; - php_libvirt_domain *res_domain; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); - - if ((uuid =3D=3D NULL) || (uuid_len < 1)) - RETURN_FALSE; - domain =3D virDomainLookupByUUIDString(conn->conn, uuid); - if (domain =3D=3D NULL) - RETURN_FALSE; - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D domain; - res_domain->conn =3D conn; - - DPRINTF("%s: domain UUID string =3D '%s', returning %p\n", PHPFUNC, uu= id, res_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_lookup_by_id - * Since version: 0.4.1(-1) - * Description: Function is used to get domain by it's ID, applicable = only to running guests - * Arguments: @conn [resource]: libvirt connection resource from lib= virt_connect() - * @id [string]: domain id to look for - * Returns: libvirt domain resource - */ -PHP_FUNCTION(libvirt_domain_lookup_by_id) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - zend_long id; - virDomainPtr domain =3D NULL; - php_libvirt_domain *res_domain; - - GET_CONNECTION_FROM_ARGS("rl", &zconn, &id); - - domain =3D virDomainLookupByID(conn->conn, (int)id); - if (domain =3D=3D NULL) - RETURN_FALSE; - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D domain; - res_domain->conn =3D conn; - - DPRINTF("%s: domain id =3D '%d', returning %p\n", PHPFUNC, (int)id, re= s_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_get_name - * Since version: 0.4.1(-1) - * Description: Function is used to get domain name from it's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: domain name string - */ -PHP_FUNCTION(libvirt_domain_get_name) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - const char *name =3D NULL; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - if (domain->domain =3D=3D NULL) - RETURN_FALSE; - - name =3D virDomainGetName(domain->domain); - DPRINTF("%s: virDomainGetName(%p) returned %s\n", PHPFUNC, domain->dom= ain, name); - if (name =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETURN_STRING(name); //we can use the copy mechanism as we need = not to free name (we even can not!) -} - -/* - * Function name: libvirt_domain_get_uuid_string - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's UUID in string fo= rmat - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: domain UUID string - */ -PHP_FUNCTION(libvirt_domain_get_uuid_string) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *uuid; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - uuid =3D (char *)emalloc(VIR_UUID_STRING_BUFLEN); - retval =3D virDomainGetUUIDString(domain->domain, uuid); - DPRINTF("%s: virDomainGetUUIDString(%p) returned %d (%s)\n", PHPFUNC, = domain->domain, retval, uuid); - if (retval !=3D 0) - RETURN_FALSE; - - VIRT_RETVAL_STRING(uuid); - efree(uuid); -} - -/* - * Private function name: streamSink - * Since version: 0.4.5 - * Description: Function to write stream to file, borrowed fro= m libvirt - * Arguments: @st [virStreamPtr]: stream pointer - * @bytes [void *]: buffer array - * @nbytes [int]: size of buffer - * @opaque [void *]: used for file descriptor - * Returns: write() error code as it's calling write - */ -static int streamSink(virStreamPtr st ATTRIBUTE_UNUSED, - const char *bytes, size_t nbytes, void *opaque) -{ - int *fd =3D (int *)opaque; - - return write(*fd, bytes, nbytes); -} - -/* - * Function name: libvirt_domain_get_screenshot_api - * Since version: 0.4.5 - * Description: Function is trying to get domain screenshot using libv= irt virGetDomainScreenshot() API if available. - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_get_by_*() - * @screenID [int]: monitor ID from where to take screens= hot - * Returns: array of filename and mime type as type is hypervisor = specific, caller is responsible for temporary file deletion - */ -PHP_FUNCTION(libvirt_domain_get_screenshot_api) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - zend_long screen =3D 0; - int fd =3D -1; - char file[] =3D "/tmp/libvirt-php-tmp-XXXXXX"; - virStreamPtr st =3D NULL; - char *mime =3D NULL; - const char *bin =3D get_feature_binary("screenshot-convert"); - -#ifdef EXTWIN - set_error_if_unset("Cannot get domain screenshot on Windows systems" T= SRMLS_CC); - RETURN_FALSE; -#endif - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &screen); - - if (!(st =3D virStreamNew(domain->conn->conn, 0))) { - set_error("Cannot create new stream" TSRMLS_CC); - goto error; - } - - mime =3D virDomainScreenshot(domain->domain, st, screen, 0); - if (!mime) { - set_error_if_unset("Cannot get domain screenshot" TSRMLS_CC); - goto error; - } - - if (!(fd =3D mkstemp(file))) { - virStreamAbort(st); - set_error_if_unset("Cannot get create file to save domain screensh= ot" TSRMLS_CC); - goto error; - } - - if (virStreamRecvAll(st, streamSink, &fd) < 0) { - set_error_if_unset("Cannot receive screenshot data" TSRMLS_CC); - virStreamAbort(st); - goto error; - } - - if (virStreamFinish(st) < 0) { - set_error_if_unset("Cannot close stream for domain" TSRMLS_CC); - goto error; - } - - virStreamFree(st); - st =3D NULL; - - array_init(return_value); - if (bin) { - char tmp[4096] =3D { 0 }; - char fileNew[1024] =3D { 0 }; - int exitStatus; - - snprintf(fileNew, sizeof(fileNew), "%s.png", file); - snprintf(tmp, sizeof(tmp), "%s %s %s > /dev/null 2> /dev/null", bi= n, file, fileNew); - exitStatus =3D system(tmp); - if (WEXITSTATUS(exitStatus) !=3D 0) - goto error; - - unlink(file); - close(fd); - fd =3D -1; - VIRT_ADD_ASSOC_STRING(return_value, "file", fileNew); - VIRT_ADD_ASSOC_STRING(return_value, "mime", "image/png"); - } else { - close(fd); - fd =3D -1; - VIRT_ADD_ASSOC_STRING(return_value, "file", file); - VIRT_ADD_ASSOC_STRING(return_value, "mime", mime); - } - - free(mime); - return; - - error: - free(mime); - if (fd !=3D -1) { - unlink(file); - close(fd); - } - if (st) - virStreamFree(st); - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_get_screenshot - * Since version: 0.4.2 - * Description: Function uses gvnccapture (if available) to get the sc= reenshot of the running domain - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @server [string]: server string for the host machine - * @scancode [int]: integer value of the scancode to be s= end to refresh screen - * Returns: PNG image binary data - */ -PHP_FUNCTION(libvirt_domain_get_screenshot) -{ -#ifndef EXTWIN - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - pid_t childpid =3D -1; - pid_t w =3D -1; - int retval =3D -1; - int fd =3D -1, fsize =3D -1; - char file[] =3D "/tmp/libvirt-php-tmp-XXXXXX"; - char *buf =3D NULL; - char *tmp =3D NULL; - char *xml =3D NULL; - int port =3D -1; - char *hostname =3D NULL; - strsize_t hostname_len; - zend_long scancode =3D 10; - const char *path; - char *pathDup =3D NULL; - char name[1024] =3D { 0 }; - int use_builtin =3D 0; - - path =3D get_feature_binary("screenshot"); - DPRINTF("%s: get_feature_binary('screenshot') returned %s\n", PHPFUNC,= path); - - if ((path =3D=3D NULL) || (access(path, X_OK) !=3D 0)) { - use_builtin =3D 1; - } else { - if (!(pathDup =3D strdup(path))) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - } - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &hostname, &hostname_len, &scan= code); - - xml =3D virDomainGetXMLDesc(domain->domain, 0); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - goto error; - } - - tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - set_error("Cannot get the VNC port" TSRMLS_CC); - goto error; - } - - if (mkstemp(file) =3D=3D 0) - goto error; - - /* Get the current hostname and override to localhost if local machine= */ - gethostname(name, 1024); - if (strcmp(name, hostname) =3D=3D 0) - hostname =3D strdup("localhost"); - - vnc_refresh_screen(hostname, tmp, scancode); - - if (use_builtin =3D=3D 1) { - DPRINTF("%s: Binary not found, using builtin approach to %s:%s, tm= p file =3D %s\n", PHPFUNC, hostname, tmp, file); - - if (vnc_get_bitmap(hostname, tmp, file) !=3D 0) { - set_error("Cannot use builtin approach to get VNC window conte= nts" TSRMLS_CC); - goto error; - } - } else { - port =3D atoi(tmp)-5900; - - DPRINTF("%s: Getting screenshot of %s:%d to temporary file %s\n", = PHPFUNC, hostname, port, file); - - childpid =3D fork(); - if (childpid =3D=3D -1) - goto error; - - if (childpid =3D=3D 0) { - char tmpp[64] =3D { 0 }; - - snprintf(tmpp, sizeof(tmpp), "%s:%d", hostname, port); - retval =3D execlp(path, basename(pathDup), tmpp, file, NULL); - _exit(retval); - } else { - do { - w =3D waitpid(childpid, &retval, 0); - if (w =3D=3D -1) - goto error; - } while (!WIFEXITED(retval) && !WIFSIGNALED(retval)); - } - - if (WEXITSTATUS(retval) !=3D 0) { - set_error("Cannot spawn utility to get screenshot" TSRMLS_CC); - goto error; - } - } - - fd =3D open(file, O_RDONLY); - fsize =3D lseek(fd, 0, SEEK_END); - lseek(fd, 0, SEEK_SET); - buf =3D emalloc((fsize + 1) * sizeof(char)); - memset(buf, 0, fsize + 1); - if (read(fd, buf, fsize) < 0) { - close(fd); - unlink(file); - goto error; - } - close(fd); - - if (access(file, F_OK) =3D=3D 0) { - DPRINTF("%s: Temporary file %s deleted\n", PHPFUNC, file); - unlink(file); - } - - /* This is necessary to make the output binary safe */ - VIRT_ZVAL_STRINGL(return_value, buf, fsize); - - efree(buf); - free(tmp); - free(xml); - free(pathDup); - return; - - error: - efree(buf); - free(tmp); - free(xml); - free(pathDup); - RETURN_FALSE; -#else - set_error("Function is not supported on Windows systems" TSRMLS_CC); - RETURN_FALSE; -#endif -} - -/* - * Function name: libvirt_domain_get_screen_dimensions - * Since version: 0.4.3 - * Description: Function get screen dimensions of the VNC window - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @server [string]: server string of the host machine - * Returns: array of height and width on success, FALSE otherwise - */ -PHP_FUNCTION(libvirt_domain_get_screen_dimensions) -{ -#ifndef EXTWIN - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval =3D -1; - char *tmp =3D NULL; - char *xml =3D NULL; - char *hostname =3D NULL; - strsize_t hostname_len; - int ret; - int width; - int height; - - GET_DOMAIN_FROM_ARGS("rs", &zdomain, &hostname, &hostname_len); - - xml =3D virDomainGetXMLDesc(domain->domain, 0); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - goto error; - } - - tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - set_error("Cannot get the VNC port" TSRMLS_CC); - goto error; - } - - DPRINTF("%s: hostname =3D %s, port =3D %s\n", PHPFUNC, hostname, tmp); - ret =3D vnc_get_dimensions(hostname, tmp, &width, &height); - free(tmp); - if (ret !=3D 0) { - char error[1024] =3D { 0 }; - if (ret =3D=3D -9) - snprintf(error, sizeof(error), "Cannot connect to VNC server. = Please make sure domain is running and VNC graphics are set"); - else - snprintf(error, sizeof(error), "Cannot get screen dimensions, = error code =3D %d (%s)", ret, strerror(-ret)); - - set_error(error TSRMLS_CC); - goto error; - } - - array_init(return_value); - add_assoc_long(return_value, "width", (long)width); - add_assoc_long(return_value, "height", (long)height); - - free(tmp); - free(xml); - return; - - error: - free(tmp); - free(xml); - RETURN_FALSE; -#else - set_error("Function is not supported on Windows systems" TSRMLS_CC); - RETURN_FALSE; -#endif -} - -/* - * Function name: libvirt_domain_send_keys - * Since version: 0.4.2 - * Description: Function sends keys to the domain's VNC window - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @server [string]: server string of the host machine - * @scancode [int]: integer scancode to be sent to VNC wi= ndow - * Returns: TRUE on success, FALSE otherwise - */ -PHP_FUNCTION(libvirt_domain_send_keys) -{ -#ifndef EXTWIN - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval =3D -1; - char *tmp =3D NULL; - char *xml =3D NULL; - char *hostname =3D NULL; - strsize_t hostname_len; - char *keys =3D NULL; - strsize_t keys_len; - int ret =3D 0; - - GET_DOMAIN_FROM_ARGS("rss", &zdomain, &hostname, &hostname_len, &keys,= &keys_len); - - DPRINTF("%s: Sending %d VNC keys to %s...\n", PHPFUNC, (int)strlen((co= nst char *)keys), hostname); - - xml =3D virDomainGetXMLDesc(domain->domain, 0); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - goto error; - } - - tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - set_error("Cannot get the VNC port" TSRMLS_CC); - goto error; - } - - DPRINTF("%s: About to send string '%s' (%d keys) to %s:%s\n", PHPFUNC,= keys, (int)strlen((const char *)keys), hostname, tmp); - - ret =3D vnc_send_keys(hostname, tmp, keys); - DPRINTF("%s: Sequence sending result is %d\n", PHPFUNC, ret); - - if (!ret) { - char tmpp[64] =3D { 0 }; - snprintf(tmpp, sizeof(tmpp), "Cannot send keys, error code %d", re= t); - set_error(tmpp TSRMLS_CC); - goto error; - } - - free(tmp); - free(xml); - RETURN_TRUE; - - error: - free(tmp); - free(xml); - RETURN_FALSE; -#else - set_error("Function is not supported on Windows systems" TSRMLS_CC); - RETURN_FALSE; -#endif -} - -/* - * Function name: libvirt_domain_send_key_api - * Since version: 0.5.3 - * Description: Function sends keys to domain via libvirt API - * Arguments: @res[resource]: libvirt domain resource, e.g. from lib= virt_domaing_lookup_by_*() - * @codeset [int]: the codeset of keycodes, from virKeyco= deSet - * @holdtime [int]: the duration (in miliseconds) that th= e keys will be held - * @keycodes [array]: array of keycodes - * @flags [int]: extra flags; not used yet so callers sho= uld alway pass 0 - * Returns: TRUE for success, FALSE for failure - */ -PHP_FUNCTION(libvirt_domain_send_key_api) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - zend_long codeset; - zend_long holdtime =3D 0; - zend_long flags =3D 0; - zval *zkeycodes, *data =3D NULL; - HashTable *arr_hash =3D NULL; - HashPosition pointer; - int count, i; - uint *keycodes =3D NULL; - - GET_DOMAIN_FROM_ARGS("rlla|l", &zdomain, &codeset, &holdtime, &zkeycod= es, - &flags); - - arr_hash =3D Z_ARRVAL_P(zkeycodes); - count =3D zend_hash_num_elements(arr_hash); - - keycodes =3D (uint *) emalloc(count * sizeof(uint)); - - i =3D 0; - VIRT_FOREACH(arr_hash, pointer, data) { - if (Z_TYPE_P(data) =3D=3D IS_LONG) { - keycodes[i++] =3D (uint) Z_LVAL_P(data); - } - } VIRT_FOREACH_END(); - - if (virDomainSendKey(domain->domain, codeset, holdtime, keycodes, coun= t, - flags) !=3D 0) { - efree(keycodes); - RETURN_FALSE; - } - - efree(keycodes); - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_send_pointer_event - * Since version: 0.4.2 - * Description: Function sends keys to the domain's VNC window - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @server [string]: server string of the host machine - * @pos_x [int]: position on x-axis - * @pos_y [int]: position on y-axis - * @clicked [int]: mask of clicked buttons (0 for none, b= it 1 for button #1, bit 8 for button #8) - * @release [int]: boolean value (0 or 1) whether to rele= ase the buttons automatically once pressed - * Returns: TRUE on success, FALSE otherwise - */ -PHP_FUNCTION(libvirt_domain_send_pointer_event) -{ -#ifndef EXTWIN - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval =3D -1; - char *tmp =3D NULL; - char *xml =3D NULL; - char *hostname =3D NULL; - strsize_t hostname_len; - zend_long pos_x =3D 0; - zend_long pos_y =3D 0; - zend_long clicked =3D 0; - zend_bool release =3D 1; - int ret; - - GET_DOMAIN_FROM_ARGS("rslll|b", &zdomain, &hostname, &hostname_len, &p= os_x, &pos_y, &clicked, &release); - - xml =3D virDomainGetXMLDesc(domain->domain, 0); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - goto error; - } - - tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - set_error("Cannot get the VNC port" TSRMLS_CC); - goto error; - } - - DPRINTF("%s: x =3D %d, y =3D %d, clicked =3D %d, release =3D %d, hostn= ame =3D %s...\n", PHPFUNC, (int) pos_x, (int) pos_y, (int) clicked, release= , hostname); - ret =3D vnc_send_pointer_event(hostname, tmp, pos_x, pos_y, clicked, r= elease); - if (!ret) { - char error[1024] =3D { 0 }; - if (ret =3D=3D -9) - snprintf(error, sizeof(error), "Cannot connect to VNC server. = Please make sure domain is running and VNC graphics are set"); - else - snprintf(error, sizeof(error), "Cannot send pointer event, err= or code =3D %d (%s)", ret, strerror(-ret)); - - set_error(error TSRMLS_CC); - goto error; - } - - free(tmp); - free(xml); - RETURN_TRUE; - - error: - free(tmp); - free(xml); - RETURN_FALSE; -#else - set_error("Function is not supported on Windows systems" TSRMLS_CC); - RETURN_FALSE; -#endif -} - -/* - * Function name: libvirt_domain_get_uuid - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's UUID in binary fo= rmat - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: domain UUID in binary format - */ -PHP_FUNCTION(libvirt_domain_get_uuid) -{ - - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *uuid; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - uuid =3D (char *)emalloc(VIR_UUID_BUFLEN); - retval =3D virDomainGetUUID(domain->domain, (unsigned char *)uuid); - DPRINTF("%s: virDomainGetUUID(%p, %p) returned %d\n", PHPFUNC, domain-= >domain, uuid, retval); - if (retval !=3D 0) - RETURN_FALSE; - - VIRT_RETVAL_STRING(uuid); - efree(uuid); -} - -/* - * Function name: libvirt_domain_get_id - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's ID, applicable to= running guests only - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: running domain ID or -1 if not running - */ -PHP_FUNCTION(libvirt_domain_get_id) -{ - - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainGetID(domain->domain); - DPRINTF("%s: virDomainGetID(%p) returned %d\n", PHPFUNC, domain->domai= n, retval); - - RETURN_LONG(retval); -} - -/* - * Function name: libvirt_domain_get_next_dev_ids - * Since version: 0.4.2 - * Description: This functions can be used to get the next free slot i= f you intend to add a new device identified by slot to the domain, e.g. NIC= device - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: next free slot number for the domain - */ -PHP_FUNCTION(libvirt_domain_get_next_dev_ids) -{ - long dom, bus, slot, func; - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - DPRINTF("%s: Getting the next dev ids for domain %p\n", PHPFUNC, domai= n->domain); - - dom =3D get_next_free_numeric_value(domain->domain, "//@domain"); - bus =3D get_next_free_numeric_value(domain->domain, "//@bus"); - slot =3D get_next_free_numeric_value(domain->domain, "//@slot"); - func =3D get_next_free_numeric_value(domain->domain, "//@func"); - - array_init(return_value); - add_assoc_long(return_value, "next_domain", dom); - add_assoc_long(return_value, "next_bus", bus); - add_assoc_long(return_value, "next_slot", slot); - add_assoc_long(return_value, "next_func", func); -} - -void parse_array(zval *arr, tVMDisk *disk, tVMNetwork *network) -{ - HashTable *arr_hash; - // int array_count; - zval *data; - php_libvirt_hash_key_info key; - HashPosition pointer; - unsigned long index; - - arr_hash =3D Z_ARRVAL_P(arr); - //array_count =3D zend_hash_num_elements(arr_hash); - - if (disk !=3D NULL) - memset(disk, 0, sizeof(tVMDisk)); - if (network !=3D NULL) - memset(network, 0, sizeof(tVMNetwork)); - - VIRT_FOREACH(arr_hash, pointer, data) { - if ((Z_TYPE_P(data) =3D=3D IS_STRING) || (Z_TYPE_P(data) =3D=3D IS= _LONG)) { - VIRT_HASH_CURRENT_KEY_INFO(arr_hash, pointer, index, key); - if (key.type =3D=3D HASH_KEY_IS_STRING) { - if (disk !=3D NULL) { - if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(key.na= me, "path") =3D=3D 0) - disk->path =3D strdup(Z_STRVAL_P(data)); - else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(k= ey.name, "driver") =3D=3D 0) - disk->driver =3D strdup(Z_STRVAL_P(data)); - else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(k= ey.name, "bus") =3D=3D 0) - disk->bus =3D strdup(Z_STRVAL_P(data)); - else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(k= ey.name, "dev") =3D=3D 0) - disk->dev =3D strdup(Z_STRVAL_P(data)); - else if (strcmp(key.name, "size") =3D=3D 0) { - if (Z_TYPE_P(data) =3D=3D IS_LONG) { - disk->size =3D Z_LVAL_P(data); - } else { - disk->size =3D size_def_to_mbytes(Z_STRVAL_P(d= ata)); - } - } else if ((Z_TYPE_P(data) =3D=3D IS_LONG) && strcmp(k= ey.name, "flags") =3D=3D 0) - disk->flags =3D Z_LVAL_P(data); - } else { - if (network !=3D NULL) { - if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(ke= y.name, "mac") =3D=3D 0) - network->mac =3D strdup(Z_STRVAL_P(data)); - else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strc= mp(key.name, "network") =3D=3D 0) - network->network =3D strdup(Z_STRVAL_P(data)); - else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strc= mp(key.name, "model") =3D=3D 0) - network->model =3D strdup(Z_STRVAL_P(data)); - } - } - } - } - } VIRT_FOREACH_END(); -} - -/* - * Function name: libvirt_domain_new - * Since version: 0.4.5 - * Description: Function is used to install a new virtual machine to t= he machine - * Arguments: @conn [resource]: libvirt connection resource - * @name [string]: name of the new domain - * @arch [string]: optional architecture string, can be N= ULL to get default (or false) - * @memMB [int]: number of megabytes of RAM to be allocat= ed for domain - * @maxmemMB [int]: maximum number of megabytes of RAM to= be allocated for domain - * @vcpus [int]: number of VCPUs to be allocated to domain - * @iso_image [string]: installation ISO image for domain - * @disks [array]: array of disk devices for domain, cons= ist of keys as 'path' (storage location), 'driver' (image type, e.g. 'raw' = or 'qcow2'), 'bus' (e.g. 'ide', 'scsi'), 'dev' (device to be presented to t= he guest - e.g. 'hda'), 'size' (with 'M' or 'G' suffixes, like '10G' for 10= gigabytes image etc.) and 'flags' (VIR_DOMAIN_DISK_FILE or VIR_DOMAIN_DISK= _BLOCK, optionally VIR_DOMAIN_DISK_ACCESS_ALL to allow access to the disk f= or all users on the host system) - * @networks [array]: array of network devices for domain= , consists of keys as 'mac' (for MAC address), 'network' (for network name)= and optional 'model' for model of NIC device - * @flags [int]: bit array of flags - * Returns: a new domain resource - */ -PHP_FUNCTION(libvirt_domain_new) -{ - php_libvirt_connection *conn =3D NULL; - php_libvirt_domain *res_domain =3D NULL; - virDomainPtr domain2 =3D NULL; - virDomainPtr domain =3D NULL; - zval *zconn; - char *arch =3D NULL; - strsize_t arch_len; - char *tmp; - char *name; - strsize_t name_len =3D 0; - // char *emulator; - char *iso_image =3D NULL; - strsize_t iso_image_len; - zend_long vcpus =3D -1; - zend_long memMB =3D -1; - zval *disks, *networks; - tVMDisk *vmDisks =3D NULL; - tVMNetwork *vmNetworks =3D NULL; - zend_long maxmemMB =3D -1; - HashTable *arr_hash; - int numDisks, numNets, i; - zval *data; - HashPosition pointer; - char vncl[2048] =3D { 0 }; - char tmpname[1024] =3D { 0 }; - char *xml =3D NULL; - int retval =3D 0; - char *uuid =3D NULL; - zend_long flags =3D 0; - int fd =3D -1; - - GET_CONNECTION_FROM_ARGS("rsslllsaa|l", &zconn, &name, &name_len, &arc= h, &arch_len, &memMB, &maxmemMB, &vcpus, &iso_image, &iso_image_len, &disks= , &networks, &flags); - - if (iso_image =3D=3D NULL) { - DPRINTF("%s: Iso image is not defined\n", PHPFUNC); - RETURN_FALSE; - } - - if ((arch =3D=3D NULL) || (arch_len =3D=3D 0)) - arch =3D NULL; - - //DPRINTF("%s: name: %s, arch: %s, memMB: %d, maxmemMB: %d, vcpus: %d,= iso_image: %s\n", PHPFUNC, name, arch, memMB, maxmemMB, vcpus, iso_image); - if (memMB =3D=3D 0) - memMB =3D maxmemMB; - - /* Parse all disks from array */ - arr_hash =3D Z_ARRVAL_P(disks); - numDisks =3D zend_hash_num_elements(arr_hash); - vmDisks =3D (tVMDisk *)malloc(numDisks * sizeof(tVMDisk)); - memset(vmDisks, 0, numDisks * sizeof(tVMDisk)); - i =3D 0; - VIRT_FOREACH(arr_hash, pointer, data) { - if (Z_TYPE_P(data) =3D=3D IS_ARRAY) { - tVMDisk disk; - parse_array(data, &disk, NULL); - if (disk.path !=3D NULL) { - //DPRINTF("Disk =3D> path =3D '%s', driver =3D '%s', bus = =3D '%s', dev =3D '%s', size =3D %ld MB, flags =3D %d\n", - // disk.path, disk.driver, disk.bus, disk.dev, disk.size,= disk.flags); - vmDisks[i++] =3D disk; - } - } - } VIRT_FOREACH_END(); - numDisks =3D i; - - /* Parse all networks from array */ - arr_hash =3D Z_ARRVAL_P(networks); - numNets =3D zend_hash_num_elements(arr_hash); - vmNetworks =3D (tVMNetwork *)malloc(numNets * sizeof(tVMNetwork)); - memset(vmNetworks, 0, numNets * sizeof(tVMNetwork)); - i =3D 0; - VIRT_FOREACH(arr_hash, pointer, data) { - if (Z_TYPE_P(data) =3D=3D IS_ARRAY) { - tVMNetwork network; - parse_array(data, NULL, &network); - if (network.mac !=3D NULL) { - //DPRINTF("Network =3D> mac =3D '%s', network =3D '%s', mo= del =3D '%s'\n", network.mac, network.network, network.model); - vmNetworks[i++] =3D network; - } - } - } VIRT_FOREACH_END(); - numNets =3D i; - - snprintf(tmpname, sizeof(tmpname), "%s-install", name); - DPRINTF("%s: Name is '%s', memMB is %d, maxmemMB is %d\n", PHPFUNC, tm= pname, (int) memMB, (int) maxmemMB); - tmp =3D installation_get_xml(1, - conn->conn, tmpname, memMB, maxmemMB, NULL = /* arch */, NULL, vcpus, iso_image, - vmDisks, numDisks, vmNetworks, numNets, - flags TSRMLS_CC); - if (tmp =3D=3D NULL) { - DPRINTF("%s: Cannot get installation XML\n", PHPFUNC); - set_error("Cannot get installation XML" TSRMLS_CC); - RETURN_FALSE; - } - - domain =3D virDomainCreateXML(conn->conn, tmp, 0); - if (domain =3D=3D NULL) { - set_error_if_unset("Cannot create installation domain from the XML= description" TSRMLS_CC); - DPRINTF("%s: Cannot create installation domain from the XML descri= ption (%s): %s\n", PHPFUNC, LIBVIRT_G(last_error), tmp); - RETURN_FALSE; - } - - xml =3D virDomainGetXMLDesc(domain, 0); - if (!xml) { - DPRINTF("%s: Cannot get the XML description\n", PHPFUNC); - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics[@type=3D= 'vnc']/@port", NULL, &retval); - if (retval < 0) { - DPRINTF("%s: Cannot get port from XML description\n", PHPFUNC); - set_error_if_unset("Cannot get port from XML description" TSRMLS_C= C); - RETURN_FALSE; - } - - snprintf(vncl, sizeof(vncl), "%s:%s", virConnectGetHostname(conn->conn= ), tmp); - DPRINTF("%s: Trying to connect to '%s'\n", PHPFUNC, vncl); - -#ifndef EXTWIN - if ((fd =3D connect_socket(virConnectGetHostname(conn->conn), tmp, 0, = 0, flags & DOMAIN_FLAG_TEST_LOCAL_VNC)) < 0) { - DPRINTF("%s: Cannot connect to '%s'\n", PHPFUNC, vncl); - snprintf(vncl, sizeof(vncl), "Connection failed, port %s is most l= ikely forbidden on firewall (iptables) on the host (%s)" - " or the emulator VNC server is bound to localhost addres= s only.", - tmp, virConnectGetHostname(conn->conn)); - } else { - close(fd); - DPRINTF("%s: Connection to '%s' successfull (%s local connection)\= n", PHPFUNC, vncl, - (flags & DOMAIN_FLAG_TEST_LOCAL_VNC) ? "using" : "not usin= g"); - } -#endif - - set_vnc_location(vncl TSRMLS_CC); - - tmp =3D installation_get_xml(2, - conn->conn, name, memMB, maxmemMB, NULL /* = arch */, NULL, vcpus, iso_image, - vmDisks, numDisks, vmNetworks, numNets, - flags TSRMLS_CC); - if (tmp =3D=3D NULL) { - DPRINTF("%s: Cannot get installation XML, step 2\n", PHPFUNC); - set_error("Cannot get installation XML, step 2" TSRMLS_CC); - virDomainFree(domain); - RETURN_FALSE; - } - - domain2 =3D virDomainDefineXML(conn->conn, tmp); - if (domain2 =3D=3D NULL) { - set_error_if_unset("Cannot define domain from the XML description"= TSRMLS_CC); - DPRINTF("%s: Cannot define domain from the XML description (name = =3D '%s', uuid =3D '%s', error =3D '%s')\n", PHPFUNC, name, uuid, LIBVIRT_G= (last_error)); - RETURN_FALSE; - } - virDomainFree(domain2); - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D domain; - res_domain->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_new_get_vnc - * Since version: 0.4.5 - * Description: Function is used to get the VNC server location for th= e newly created domain (newly started installation) - * Arguments: None - * Returns: a VNC server for a newly created domain resource (if a= ny) - */ -PHP_FUNCTION(libvirt_domain_new_get_vnc) -{ - if (LIBVIRT_G(vnc_location)) - VIRT_RETURN_STRING(LIBVIRT_G(vnc_location)); - - RETURN_NULL(); -} - -/* - * Function name: libvirt_domain_get_xml_desc - * Since version: 0.4.1(-1), changed 0.4.2 - * Description: Function is used to get the domain's XML description - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL - * Returns: domain XML description string or result of xPath expre= ssion - */ -PHP_FUNCTION(libvirt_domain_get_xml_desc) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *tmp =3D NULL; - char *xml; - char *xpath =3D NULL; - strsize_t xpath_len; - zend_long flags =3D 0; - int retval =3D -1; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &xpath, &xpath_len, &flags); - if (xpath_len < 1) - xpath =3D NULL; - - DPRINTF("%s: Getting the XML for domain %p (xPath =3D %s)\n", PHPFUNC,= domain->domain, xpath); - - xml =3D virDomainGetXMLDesc(domain->domain, flags); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - VIRT_RETVAL_STRING(xml); - } else { - VIRT_RETVAL_STRING(tmp); - } - - free(tmp); - free(xml); -} - -/* - * Function name: libvirt_domain_get_disk_devices - * Since version: 0.4.4 - * Description: Function is used to get disk devices for the domain - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: list of domain disk devices - */ -PHP_FUNCTION(libvirt_domain_get_disk_devices) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *xml; - int retval =3D -1; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - DPRINTF("%s: Getting disk device list for domain %p\n", PHPFUNC, domai= n->domain); - - xml =3D virDomainGetXMLDesc(domain->domain, 0); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - array_init(return_value); - - free(get_string_from_xpath(xml, "//domain/devices/disk/target/@dev", &= return_value, &retval)); - free(xml); - - if (retval < 0) - add_assoc_long(return_value, "error_code", (long)retval); - else - add_assoc_long(return_value, "num", (long)retval); -} - -/* - * Function name: libvirt_domain_get_interface_devices - * Since version: 0.4.4 - * Description: Function is used to get network interface devices for = the domain - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: list of domain interface devices - */ -PHP_FUNCTION(libvirt_domain_get_interface_devices) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *xml; - int retval =3D -1; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - DPRINTF("%s: Getting interface device list for domain %p\n", PHPFUNC, = domain->domain); - - xml =3D virDomainGetXMLDesc(domain->domain, 0); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - array_init(return_value); - - free(get_string_from_xpath(xml, "//domain/devices/interface/target/@de= v", &return_value, &retval)); - free(xml); - - if (retval < 0) - add_assoc_long(return_value, "error_code", (long)retval); - else - add_assoc_long(return_value, "num", (long)retval); -} - -/* - * Function name: libvirt_domain_change_vcpus - * Since version: 0.4.2 - * Description: Function is used to change the VCPU count for the doma= in - * Arguments: @res [resource]: libvirt domain resource - * @numCpus [int]: number of VCPUs to be set for the guest - * @flags [int]: flags for virDomainSetVcpusFlags (availa= ble at http://libvirt.org/html/libvirt-libvirt.html#virDomainVcpuFlags) - * Returns: true on success, false on error - */ -PHP_FUNCTION(libvirt_domain_change_vcpus) -{ - zend_long numCpus, flags =3D 0; - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - - GET_DOMAIN_FROM_ARGS("rl|l", &zdomain, &numCpus, &flags); - - if (virDomainSetVcpusFlags(domain->domain, numCpus, flags) =3D=3D 0) { - RETURN_TRUE; - } else { - RETURN_FALSE; - } -} - -/* - * Function name: libvirt_domain_change_memory - * Since version: 0.4.2 - * Description: Function is used to change the domain memory allocation - * Arguments: @res [resource]: libvirt domain resource - * @allocMem [int]: number of MiBs to be set as immediate= memory value - * @allocMax [int]: number of MiBs to be set as the maxim= um allocation - * @flags [int]: flags - * Returns: new domain resource - */ -PHP_FUNCTION(libvirt_domain_change_memory) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *tmpA =3D NULL; - char *tmp1 =3D NULL; - char *tmp2 =3D NULL; - char *xml; - char *new_xml =3D NULL; - int new_len; - char *newXml =3D NULL; - zend_long xflags =3D 0; - zend_long allocMem =3D 0; - zend_long allocMax =3D 0; - // int pos =3D -1; - int len =3D 0; - php_libvirt_domain *res_domain =3D NULL; - php_libvirt_connection *conn =3D NULL; - virDomainPtr dom =3D NULL; - - GET_DOMAIN_FROM_ARGS("rll|l", &zdomain, &allocMem, &allocMax, &xflags); - - DPRINTF("%s: Changing domain memory count to %d MiB current/%d MiB max= , domain =3D %p\n", - PHPFUNC, (int)allocMem, (int)allocMax, domain->domain); - - allocMem *=3D 1024; - allocMax *=3D 1024; - - if (allocMem > allocMax) - allocMem =3D allocMax; - - xml =3D virDomainGetXMLDesc(domain->domain, xflags); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - snprintf(newXml, sizeof(newXml), " %d\n %d\n", allocMax, allocMem); - tmpA =3D strstr(xml, ""); - tmp1 =3D strstr(xml, "") + strlen(""); - // pos =3D strlen(xml) - strlen(tmp1); - len =3D strlen(xml) - strlen(tmpA); - - tmp2 =3D (char *)emalloc((len + 1) * sizeof(char)); - memset(tmp2, 0, len + 1); - memcpy(tmp2, xml, len); - - new_len =3D strlen(tmp1) + strlen(tmp2) + strlen(newXml) + 2; - new_xml =3D (char *)emalloc(new_len * sizeof(char)); - snprintf(new_xml, new_len, "%s\n%s%s", tmp2, newXml, tmp1); - - conn =3D domain->conn; - - dom =3D virDomainDefineXML(conn->conn, new_xml); - if (dom =3D=3D NULL) { - free(xml); - efree(new_xml); - RETURN_FALSE; - } - free(xml); - efree(new_xml); - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D dom; - res_domain->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_change_boot_devices - * Since version: 0.4.2 - * Description: Function is used to change the domain boot devices - * Arguments: @res [resource]: libvirt domain resource - * @first [string]: first boot device to be set - * @second [string]: second boot device to be set - * @flags [int]: flags - * Returns: new domain resource - */ -PHP_FUNCTION(libvirt_domain_change_boot_devices) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *tmpA =3D NULL; - char *tmp1 =3D NULL; - char *tmp2 =3D NULL; - char *xml; - char *new_xml =3D NULL; - int new_len; - char newXml[4096] =3D { 0 }; - zend_long xflags =3D 0; - char *first =3D NULL; - strsize_t first_len; - char *second =3D NULL; - strsize_t second_len; - // int pos =3D -1; - int len =3D 0; - php_libvirt_domain *res_domain =3D NULL; - php_libvirt_connection *conn =3D NULL; - virDomainPtr dom =3D NULL; - - GET_DOMAIN_FROM_ARGS("rss|l", &zdomain, &first, &first_len, &second, &= second_len, &xflags); - - xml =3D virDomainGetXMLDesc(domain->domain, xflags); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - DPRINTF("%s: Changing domain boot order, domain =3D %p\n", PHPFUNC, do= main->domain); - - if (!second || (strcmp(second, "-") =3D=3D 0)) - snprintf(newXml, sizeof(newXml), " \n", first= ); - else - snprintf(newXml, sizeof(newXml), " \n \n", first, second); - - tmpA =3D strstr(xml, "") + strlen(""); - tmp1 =3D strstr(xml, ""); - // pos =3D strlen(xml) - strlen(tmp1); - len =3D strlen(xml) - strlen(tmpA); - - tmp2 =3D (char *)emalloc((len + 1) * sizeof(char)); - memset(tmp2, 0, len + 1); - memcpy(tmp2, xml, len); - - new_len =3D strlen(tmp1) + strlen(tmp2) + strlen(newXml) + 2; - new_xml =3D (char *)emalloc(new_len * sizeof(char)); - snprintf(new_xml, new_len, "%s\n%s%s", tmp2, newXml, tmp1); - - conn =3D domain->conn; - - dom =3D virDomainDefineXML(conn->conn, new_xml); - if (dom =3D=3D NULL) { - DPRINTF("%s: Function failed, restoring original XML\n", PHPFUNC); - free(xml); - efree(newXml); - RETURN_FALSE; - } - free(xml); - efree(newXml); - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D dom; - res_domain->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_disk_add - * Since version: 0.4.2 - * Description: Function is used to add the disk to the virtual machin= e using set of API functions to make it as simple as possible for the user - * Arguments: @res [resource]: libvirt domain resource - * @img [string]: string for the image file on the host s= ystem - * @dev [string]: string for the device to be presented t= o the guest (e.g. hda) - * @typ [string]: bus type for the device in the guest, u= sually 'ide' or 'scsi' - * @driver [string]: driver type to be specified, like 'r= aw' or 'qcow2' - * @flags [int]: flags for getting the XML description - * Returns: new domain resource - */ -PHP_FUNCTION(libvirt_domain_disk_add) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *xml; - char *img =3D NULL; - strsize_t img_len; - char *dev =3D NULL; - strsize_t dev_len; - char *driver =3D NULL; - strsize_t driver_len; - char *typ =3D NULL; - strsize_t typ_len; - char *newXml =3D NULL; - zend_long xflags =3D 0; - int retval =3D -1; - char *xpath =3D NULL; - char *tmp =3D NULL; - - GET_DOMAIN_FROM_ARGS("rssss|l", &zdomain, &img, &img_len, &dev, &dev_l= en, &typ, &typ_len, &driver, &driver_len, &xflags); - - DPRINTF("%s: Domain %p, device =3D %s, image =3D %s, type =3D %s, driv= er =3D %s\n", PHPFUNC, - domain->domain, dev, img, typ, driver); - - xml =3D virDomainGetXMLDesc(domain->domain, xflags); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - goto error; - } - - if (asprintf(&xpath, "//domain/devices/disk/source[@file=3D\"%s\"]/./@= file", img) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (tmp !=3D NULL) { - free(tmp); - if (asprintf(&tmp, "Domain already has image %s connected",= img) < 0) - set_error("Out of memory" TSRMLS_CC); - else - set_error(tmp TSRMLS_CC); - goto error; - } - - free(xpath); - if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/./@dev= ", dev) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, newXml, NULL, &retval); - if (tmp !=3D NULL) { - free(tmp); - if (asprintf(&tmp, "Domain already has device %s connected"= , dev) < 0) - set_error("Out of memory" TSRMLS_CC); - else - set_error(tmp TSRMLS_CC); - goto error; - } - - if (asprintf(&newXml, - " \n" - " \n" - " \n" - " \n" - " ", driver, img, dev, typ) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - - if (virDomainAttachDeviceFlags(domain->domain, - newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { - set_error("Unable to attach disk" TSRMLS_CC); - goto error; - } - - free(tmp); - free(xpath); - free(xml); - RETURN_TRUE; - - error: - free(tmp); - free(xpath); - free(xml); - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_disk_remove - * Since version: 0.4.2 - * Description: Function is used to remove the disk from the virtual m= achine using set of API functions to make it as simple as possible - * Arguments: @res [resource]: libvirt domain resource - * @dev [string]: string for the device to be removed fro= m the guest (e.g. 'hdb') - * @flags [int]: flags for getting the XML description - * Returns: new domain resource - */ -PHP_FUNCTION(libvirt_domain_disk_remove) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *xml; - char *dev =3D NULL; - strsize_t dev_len; - char *newXml =3D NULL; - zend_long xflags =3D 0; - int retval =3D -1; - char *xpath =3D NULL; - char *tmp =3D NULL; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &dev, &dev_len, &xflags); - - DPRINTF("%s: Trying to remove %s from domain %p\n", PHPFUNC, dev, doma= in->domain); - - xml =3D virDomainGetXMLDesc(domain->domain, xflags); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/./@dev= ", dev) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (!tmp) { - if (asprintf(&tmp, "Device %s is not connected to the guest= ", dev) < 0) - set_error("Out of memory" TSRMLS_CC); - else - set_error(tmp TSRMLS_CC); - goto error; - } - - if (asprintf(&newXml, - " \n" - " \n" - " ", dev) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - - if (virDomainDetachDeviceFlags(domain->domain, - newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { - set_error("Unable to attach disk" TSRMLS_CC); - goto error; - } - - free(tmp); - free(xpath); - free(xml); - RETURN_TRUE; - - error: - free(tmp); - free(xpath); - free(xml); - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_nic_add - * Since version: 0.4.2 - * Description: Function is used to add the NIC card to the virtual ma= chine using set of API functions to make it as simple as possible for the u= ser - * Arguments: @res [resource]: libvirt domain resource - * @mac [string]: MAC string interpretation to be used fo= r the NIC device - * @network [string]: network name where to connect this = NIC - * @model [string]: string of the NIC model - * @flags [int]: flags for getting the XML description - * Returns: new domain resource - */ -PHP_FUNCTION(libvirt_domain_nic_add) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *xml; - char *mac =3D NULL; - strsize_t mac_len; - char *net =3D NULL; - strsize_t net_len; - char *model =3D NULL; - strsize_t model_len; - char *newXml =3D NULL; - zend_long xflags =3D 0; - int retval =3D -1; - char *xpath =3D NULL; - char *tmp =3D NULL; - - DPRINTF("%s: Entering\n", PHPFUNC); - - GET_DOMAIN_FROM_ARGS("rsss|l", &zdomain, &mac, &mac_len, &net, &net_le= n, &model, &model_len, &xflags); - if (model_len < 1) - model =3D NULL; - - DPRINTF("%s: domain =3D %p, mac =3D %s, net =3D %s, model =3D %s\n", P= HPFUNC, domain->domain, mac, net, model); - - xml =3D virDomainGetXMLDesc(domain->domain, xflags); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/./@mac", mac) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (tmp) { - free(tmp); - if (asprintf(&tmp, "Domain already has NIC device with MAC address= %s connected", mac) < 0) - set_error("Out of memory" TSRMLS_CC); - else - set_error(tmp TSRMLS_CC); - goto error; - } - - if (model) { - if (asprintf(&newXml, - " \n" - " \n" - " \n" - " \n" - " ", mac, net, model) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - } else { - if (asprintf(&newXml, - " \n" - " \n" - " \n" - " ", mac, net) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - } - - if (virDomainAttachDeviceFlags(domain->domain, - newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { - set_error("Unable to attach interface" TSRMLS_CC); - goto error; - } - - free(tmp); - free(xpath); - free(xml); - RETURN_TRUE; - - error: - free(tmp); - free(xpath); - free(xml); - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_nic_remove - * Since version: 0.4.2 - * Description: Function is used to remove the NIC from the virtual ma= chine using set of API functions to make it as simple as possible - * Arguments: @res [resource]: libvirt domain resource - * @dev [string]: string representation of the IP address= to be removed (e.g. 54:52:00:xx:yy:zz) - * @flags [int]: optional flags for getting the XML descr= iption - * Returns: new domain resource - */ -PHP_FUNCTION(libvirt_domain_nic_remove) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *xml; - char *mac =3D NULL; - strsize_t mac_len; - char *newXml =3D NULL; - zend_long xflags =3D 0; - int retval =3D -1; - char *xpath =3D NULL; - char *tmp =3D NULL; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &mac, &mac_len, &xflags); - - DPRINTF("%s: Trying to remove NIC device with MAC address %s from doma= in %p\n", PHPFUNC, mac, domain->domain); - - xml =3D virDomainGetXMLDesc(domain->domain, xflags); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/./@mac", mac) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (!tmp) { - free(tmp); - if (asprintf(&tmp, "Domain has no such interface with mac %s", mac= ) < 0) - set_error("Out of memory" TSRMLS_CC); - else - set_error(tmp TSRMLS_CC); - goto error; - } - - if (asprintf(&newXml, - " \n" - " \n" - " ", mac) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - - if (virDomainDetachDeviceFlags(domain->domain, - newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { - set_error("Unable to detach interface" TSRMLS_CC); - goto error; - } - - free(tmp); - free(xpath); - free(xml); - RETURN_TRUE; - - error: - free(tmp); - free(xpath); - free(xml); - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_attach_device - * Since version: 0.5.3 - * Description: Function is used to attach a virtual device to a domai= n. - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @xml [string]: XML description of one device. - * @flags [int]: optional flags to control how the device= is attached. Defaults to VIR_DOMAIN_AFFECT_LIVE - * Returns: TRUE for success, FALSE on error. - */ -PHP_FUNCTION(libvirt_domain_attach_device) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain =3D NULL; - char *xml =3D NULL; - strsize_t xml_len =3D 0; - zend_long flags =3D VIR_DOMAIN_AFFECT_LIVE; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &xml, &xml_len, &flags); - - if (virDomainAttachDeviceFlags(domain->domain, xml, flags) < 0) - RETURN_FALSE; - - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_detach_device - * Since version: 0.5.3 - * Description: Function is used to detach a virtual device from a dom= ain. - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @xml [string]: XML description of one device. - * @flags [int]: optional flags to control how the device= is attached. Defaults to VIR_DOMAIN_AFFECT_LIVE - * Returns: TRUE for success, FALSE on error. - */ -PHP_FUNCTION(libvirt_domain_detach_device) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain =3D NULL; - char *xml =3D NULL; - strsize_t xml_len =3D 0; - zend_long flags =3D VIR_DOMAIN_AFFECT_LIVE; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &xml, &xml_len, &flags); - - if (virDomainDetachDeviceFlags(domain->domain, xml, flags) < 0) - RETURN_FALSE; - - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_get_info - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's information - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: domain information array - */ -PHP_FUNCTION(libvirt_domain_get_info) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - virDomainInfo domainInfo; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainGetInfo(domain->domain, &domainInfo); - DPRINTF("%s: virDomainGetInfo(%p) returned %d\n", PHPFUNC, domain->dom= ain, retval); - if (retval !=3D 0) - RETURN_FALSE; - - array_init(return_value); - add_assoc_long(return_value, "maxMem", domainInfo.maxMem); - add_assoc_long(return_value, "memory", domainInfo.memory); - add_assoc_long(return_value, "state", (long)domainInfo.state); - add_assoc_long(return_value, "nrVirtCpu", domainInfo.nrVirtCpu); - add_assoc_double(return_value, "cpuUsed", (double)((double)domainInfo.= cpuTime/1000000000.0)); -} - -/* - * Function name: libvirt_domain_create - * Since version: 0.4.1(-1) - * Description: Function is used to create the domain identified by it= 's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: result of domain creation (startup) - */ -PHP_FUNCTION(libvirt_domain_create) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainCreate(domain->domain); - DPRINTF("%s: virDomainCreate(%p) returned %d\n", PHPFUNC, domain->doma= in, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_destroy - * Since version: 0.4.1(-1) - * Description: Function is used to destroy the domain identified by i= t's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: result of domain destroy - */ -PHP_FUNCTION(libvirt_domain_destroy) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainDestroy(domain->domain); - DPRINTF("%s: virDomainDestroy(%p) returned %d\n", PHPFUNC, domain->dom= ain, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_resume - * Since version: 0.4.1(-1) - * Description: Function is used to resume the domain identified by it= 's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: result of domain resume - */ -PHP_FUNCTION(libvirt_domain_resume) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainResume(domain->domain); - DPRINTF("%s: virDomainResume(%p) returned %d\n", PHPFUNC, domain->doma= in, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_core_dump - * Since version: 0.4.1(-2) - * Description: Function is used to dump core of the domain identified= by it's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @to [string]: to - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_core_dump) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - strsize_t to_len; - char *to; - - GET_DOMAIN_FROM_ARGS("rs", &zdomain, &to, &to_len); - - retval =3D virDomainCoreDump(domain->domain, to, 0); - DPRINTF("%s: virDomainCoreDump(%p, %s, 0) returned %d\n", PHPFUNC, dom= ain->domain, to, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_shutdown - * Since version: 0.4.1(-1) - * Description: Function is used to shutdown the domain identified by = it's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_shutdown) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainShutdown(domain->domain); - DPRINTF("%s: virDomainShutdown(%p) returned %d\n", PHPFUNC, domain->do= main, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_managedsave - * Since version: 0.4.1(-1) - * Description: Function is used to managed save the domain (domain wa= s unloaded from memory and it state saved to disk) identified by it's resou= rce - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_managedsave) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - retval =3D virDomainManagedSave(domain->domain, 0); - DPRINTF("%s: virDomainManagedSave(%p) returned %d\n", PHPFUNC, domain-= >domain, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_suspend - * Since version: 0.4.1(-1) - * Description: Function is used to suspend the domain identified by i= t's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_suspend) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainSuspend(domain->domain); - DPRINTF("%s: virDomainSuspend(%p) returned %d\n", PHPFUNC, domain->dom= ain, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_undefine - * Since version: 0.4.1(-1) - * Description: Function is used to undefine the domain identified by = it's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_undefine) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainUndefine(domain->domain); - DPRINTF("%s: virDomainUndefine(%p) returned %d\n", PHPFUNC, domain->do= main, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_reboot - * Since version: 0.4.1(-1) - * Description: Function is used to reboot the domain identified by it= 's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @flags [int]: optional flags - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_reboot) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - retval =3D virDomainReboot(domain->domain, flags); - DPRINTF("%s: virDomainReboot(%p) returned %d\n", PHPFUNC, domain->doma= in, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_define_xml - * Since version: 0.4.1(-1) - * Description: Function is used to define the domain from XML string - * Arguments: @conn [resource]: libvirt connection resource - * @xml [string]: XML string to define guest from - * Returns: newly defined domain resource - */ -PHP_FUNCTION(libvirt_domain_define_xml) -{ - php_libvirt_domain *res_domain =3D NULL; - php_libvirt_connection *conn =3D NULL; - zval *zconn; - virDomainPtr domain =3D NULL; - char *xml; - strsize_t xml_len; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &xml, &xml_len); - - domain =3D virDomainDefineXML(conn->conn, xml); - if (domain =3D=3D NULL) - RETURN_FALSE; - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D domain; - res_domain->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_create_xml - * Since version: 0.4.1(-1) - * Description: Function is used to create the domain identified by it= 's resource - * Arguments: @conn [resource]: libvirt connection resource - * @xml [string]: XML string to create guest from - * Returns: newly started/created domain resource - */ -PHP_FUNCTION(libvirt_domain_create_xml) -{ - php_libvirt_domain *res_domain =3D NULL; - php_libvirt_connection *conn =3D NULL; - zval *zconn; - virDomainPtr domain =3D NULL; - char *xml; - strsize_t xml_len; - zend_long flags =3D 0; - - GET_CONNECTION_FROM_ARGS("rs|l", &zconn, &xml, &xml_len, &flags); - - domain =3D virDomainCreateXML(conn->conn, xml, flags); - DPRINTF("%s: virDomainCreateXML(%p, , 0) returned %p\n", PHPFUNC,= conn->conn, domain); - if (domain =3D=3D NULL) - RETURN_FALSE; - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D domain; - res_domain->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_xml_from_native - * Since version: 0.5.3 - * Description: Function is used to convert native configuration data = to libvirt domain XML - * Arguments: @conn [resource]: libvirt connection resource - * @format [string]: configuration format converting from - * @config_data [string]: content of the native config fi= le - * Returns: libvirt domain XML, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_xml_from_native) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - char *config_data =3D NULL; - char *format =3D NULL; - char *xml =3D NULL; - strsize_t config_data_len; - strsize_t format_len; - unsigned int flags =3D 0; - - GET_CONNECTION_FROM_ARGS("rss", &zconn, &format, &format_len, &config_= data, &config_data_len); - - xml =3D virConnectDomainXMLFromNative(conn->conn, format, config_data,= flags); - - if (xml =3D=3D NULL) { - set_error_if_unset("Cannot convert native format to XML" TSRMLS_CC= ); - RETURN_FALSE; - } - - VIRT_RETVAL_STRING(xml); - free(xml); -} - -/* - * Function name: libvirt_domain_xml_to_native - * Since version: 0.5.3 - * Description: Function is used to convert libvirt domain XML to nati= ve configuration - * Arguments: @conn [resource]: libvirt connection resource - * @format [string]: configuration format converting to - * @xml_data [string]: content of the libvirt domain xml = file - * Returns: contents of the native data file, FALSE on error -*/ -PHP_FUNCTION(libvirt_domain_xml_to_native) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - char *xml_data =3D NULL; - char *format =3D NULL; - char *config_data =3D NULL; - strsize_t xml_data_len; - strsize_t format_len; - unsigned int flags =3D 0; - - GET_CONNECTION_FROM_ARGS("rss", &zconn, &format, &format_len, &xml_dat= a, &xml_data_len); - - config_data =3D virConnectDomainXMLToNative(conn->conn, format, xml_da= ta, flags); - - if (config_data =3D=3D NULL) { - set_error_if_unset("Cannot convert to native format from XML" TSRM= LS_CC); - RETURN_FALSE; - } - - VIRT_RETVAL_STRING(config_data); - free(config_data); -} - -/* - * Function name: libvirt_domain_memory_peek - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's memory peek value - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @start [int]: start - * @size [int]: size - * @flags [int]: optional flags - * Returns: domain memory peek - */ -PHP_FUNCTION(libvirt_domain_memory_peek) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - zend_long flags =3D 0; - zend_long start; - zend_long size; - char *buff; - - GET_DOMAIN_FROM_ARGS("rlll", &zdomain, &start, &size, &flags); - if (start < 0) { - set_error("Negative argument start" TSRMLS_CC); - RETURN_FALSE; - } - buff =3D (char *)emalloc(size); - retval =3D virDomainMemoryPeek(domain->domain, start, size, buff, flag= s); - if (retval !=3D 0) - RETURN_FALSE; - VIRT_RETVAL_STRINGL(buff, size); - efree(buff); -} - -/* - * Function name: libvirt_domain_memory_stats - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's memory stats - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @flags [int]: optional flags - * Returns: domain memory stats array (same fields as virDomainMem= oryStats, please see libvirt documentation) - */ -PHP_FUNCTION(libvirt_domain_memory_stats) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - zend_long flags =3D 0; - int i; - struct _virDomainMemoryStat stats[VIR_DOMAIN_MEMORY_STAT_NR]; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - retval =3D virDomainMemoryStats(domain->domain, stats, VIR_DOMAIN_MEMO= RY_STAT_NR, flags); - DPRINTF("%s: virDomainMemoryStats(%p...) returned %d\n", PHPFUNC, doma= in->domain, retval); - - if (retval =3D=3D -1) - RETURN_FALSE; - LONGLONG_INIT; - array_init(return_value); - for (i =3D 0; i < retval; i++) - LONGLONG_INDEX(return_value, stats[i].tag, stats[i].val) -} - -/* - * Function name: libvirt_domain_update_device - * Since version: 0.4.1(-1) - * Description: Function is used to update the domain's devices from t= he XML string - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @xml [string]: XML string for the update - * @flags [int]: Flags to update the device (VIR_DOMAIN_D= EVICE_MODIFY_CURRENT, VIR_DOMAIN_DEVICE_MODIFY_LIVE, VIR_DOMAIN_DEVICE_MODI= FY_CONFIG, VIR_DOMAIN_DEVICE_MODIFY_FORCE) - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_update_device) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *xml; - strsize_t xml_len; - zend_long flags; - int res; - - GET_DOMAIN_FROM_ARGS("rsl", &zdomain, &xml, &xml_len, &flags); - - res =3D virDomainUpdateDeviceFlags(domain->domain, xml, flags); - DPRINTF("%s: virDomainUpdateDeviceFlags(%p) returned %d\n", PHPFUNC, d= omain->domain, res); - if (res !=3D 0) - RETURN_FALSE; - - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_block_stats - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's block stats - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @path [string]: device path to get statistics about - * Returns: domain block stats array, fields are rd_req, rd_bytes,= wr_req, wr_bytes and errs - */ -PHP_FUNCTION(libvirt_domain_block_stats) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *path; - strsize_t path_len; - - struct _virDomainBlockStats stats; - - GET_DOMAIN_FROM_ARGS("rs", &zdomain, &path, &path_len); - - retval =3D virDomainBlockStats(domain->domain, path, &stats, sizeof st= ats); - DPRINTF("%s: virDomainBlockStats(%p, %s, , ) returned %d\= n", PHPFUNC, domain->domain, path, retval); - if (retval =3D=3D -1) - RETURN_FALSE; - - array_init(return_value); - LONGLONG_INIT; - LONGLONG_ASSOC(return_value, "rd_req", stats.rd_req); - LONGLONG_ASSOC(return_value, "rd_bytes", stats.rd_bytes); - LONGLONG_ASSOC(return_value, "wr_req", stats.wr_req); - LONGLONG_ASSOC(return_value, "wr_bytes", stats.wr_bytes); - LONGLONG_ASSOC(return_value, "errs", stats.errs); -} - -/* - * Function name: libvirt_domain_block_resize - * Since version: 0.5.1(-1) - * Description: Function is used to resize the domain's block device - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @path [string]: device path to resize - * @size [int]: size of device - * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_RESIZE_* - * Returns: true on success fail on error - */ -PHP_FUNCTION(libvirt_domain_block_resize) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *path; - strsize_t path_len; - zend_long size =3D 0; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("rsl|l", &zdomain, &path, &path_len, &size, &flag= s); - - retval =3D virDomainBlockResize(domain->domain, path, size, flags); - if (retval =3D=3D -1) - RETURN_FALSE; - - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_block_commit - * Since version: 0.5.2(-1) - * Description: Function is used to commit block job - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @disk [string]: path to the block device, or device sh= orthand - * @base [string]: path to backing file to merge into, or= device shorthand, or NULL for default - * @top [string]: path to file within backing chain that = contains data to be merged, or device shorthand, or NULL to merge all possi= ble data - * @bandwidth [int]: (optional) specify bandwidth limit; = flags determine the unit - * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_COMMIT_* - * Returns: true on success fail on error - */ -PHP_FUNCTION(libvirt_domain_block_commit) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *disk =3D NULL; - strsize_t disk_len; - char *base =3D NULL; - strsize_t base_len; - char *top =3D NULL; - strsize_t top_len; - zend_long bandwidth =3D 0; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("rs|ssll", &zdomain, &disk, &disk_len, &base, &ba= se_len, &top, &top_len, &bandwidth, &flags); - if (strcmp(disk, "") =3D=3D 0) - RETURN_FALSE; - if (strcmp(base, "") =3D=3D 0) - base =3D NULL; - if (strcmp(top, "") =3D=3D 0) - top =3D NULL; - - retval =3D virDomainBlockCommit(domain->domain, disk, base, top, bandw= idth, flags); - if (retval =3D=3D -1) - RETURN_FALSE; - - RETURN_TRUE; -} - - -/* - * Function name: libvirt_domain_block_job_info - * Since version: 0.5.2(-1) - * Description: Function is used to request block job information = for the given disk - * Arguments: @dom [resource]: libvirt domain resource, e.g. fro= m libvirt_domain_lookup_by_*() - * @disk [string]: path to the block device, or devic= e shorthand - * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_COMMI= T_* - * Returns: Array with status virDomainGetBlockJobInfo and blo= ckjob information. -*/ -PHP_FUNCTION(libvirt_domain_block_job_info) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *disk; - int disk_len; - long flags =3D 0; - virDomainBlockJobInfo info; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &disk, &disk_len, &flags); - - retval =3D virDomainGetBlockJobInfo(domain->domain, disk, &info, flags= ); - - array_init(return_value); - add_assoc_long(return_value, "status", (int)retval); - add_assoc_long(return_value, "type", (int)info.type); - add_assoc_long(return_value, "bandwidth", (unsigned long)info.bandwidt= h); - add_assoc_long(return_value, "cur", (unsigned long long)info.cur); - add_assoc_long(return_value, "end", (unsigned long long)info.end); -} - - -/* - * Function name: libvirt_domain_block_job_abort - * Since version: 0.5.1(-1) - * Description: Function is used to abort block job - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @path [string]: device path to resize - * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_JOB_ABORT= _* - * Returns: true on success fail on error - */ -PHP_FUNCTION(libvirt_domain_block_job_abort) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *path; - strsize_t path_len; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &path, &path_len, &flags); - - retval =3D virDomainBlockJobAbort(domain->domain, path, flags); - if (retval =3D=3D -1) - RETURN_FALSE; - - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_block_job_set_speed - * Since version: 0.5.1(-1) - * Description: Function is used to set speed of block job - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @path [string]: device path to resize - * @bandwidth [int]: bandwidth - * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_JOB_SPEED= _BANDWIDTH_* - * Returns: true on success fail on error - */ -PHP_FUNCTION(libvirt_domain_block_job_set_speed) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *path; - strsize_t path_len; - zend_long bandwidth =3D 0; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("rsl|l", &zdomain, &path, &path_len, &bandwidth, = &flags); - - retval =3D virDomainBlockJobSetSpeed(domain->domain, path, bandwidth, = flags); - if (retval =3D=3D -1) - RETURN_FALSE; - - RETURN_TRUE; -} - - - -/* - * Function name: libvirt_domain_get_network_info - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's network informati= on - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @mac [string]: mac address of the network device - * Returns: domain network info array of MAC address, network name= and type of NIC card - */ -PHP_FUNCTION(libvirt_domain_get_network_info) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *mac; - char *xml; - char *tmp =3D NULL; - strsize_t mac_len; - char *xpath =3D NULL; - - GET_DOMAIN_FROM_ARGS("rs", &zdomain, &mac, &mac_len); - - /* Get XML for the domain */ - xml =3D virDomainGetXMLDesc(domain->domain, VIR_DOMAIN_XML_INACTIVE); - if (!xml) { - set_error("Cannot get domain XML" TSRMLS_CC); - RETURN_FALSE; - } - - DPRINTF("%s: Getting network information for NIC with MAC address '%s'= \n", PHPFUNC, mac); - if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/../source/@network", mac) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (tmp =3D=3D NULL) { - set_error("Invalid XPath node for source network" TSRMLS_CC); - goto error; - } - - if (retval < 0) { - set_error("Cannot get XPath expression result for network source" = TSRMLS_CC); - goto error; - } - - array_init(return_value); - VIRT_ADD_ASSOC_STRING(return_value, "mac", mac); - VIRT_ADD_ASSOC_STRING(return_value, "network", tmp); - - free(tmp); - free(xpath); - - if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/../model/@type", mac) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "nic_type", tmp); - else - VIRT_ADD_ASSOC_STRING(return_value, "nic_type", "default"); - - free(xml); - free(xpath); - free(tmp); - return; - - error: - free(xml); - free(xpath); - free(tmp); - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_get_block_info - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's block device info= rmation - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @dev [string]: device to get block information about - * Returns: domain block device information array of device, file = or partition, capacity, allocation and physical size - */ -PHP_FUNCTION(libvirt_domain_get_block_info) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *dev; - char *xml; - char *tmp =3D NULL; - strsize_t dev_len; - int isFile; - char *xpath =3D NULL; - - struct _virDomainBlockInfo info; - - GET_DOMAIN_FROM_ARGS("rs", &zdomain, &dev, &dev_len); - - /* Get XML for the domain */ - xml =3D virDomainGetXMLDesc(domain->domain, VIR_DOMAIN_XML_INACTIVE); - if (!xml) { - set_error("Cannot get domain XML" TSRMLS_CC); - RETURN_FALSE; - } - - isFile =3D 0; - - if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/../sou= rce/@dev", dev) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (retval < 0) { - set_error("Cannot get XPath expression result for device storage" = TSRMLS_CC); - goto error; - } - - if (retval =3D=3D 0) { - free(xpath); - if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/..= /source/@file", dev) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - free(tmp); - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (retval < 0) { - set_error("Cannot get XPath expression result for file storage= " TSRMLS_CC); - goto error; - } - isFile =3D 1; - } - - if (retval =3D=3D 0) { - set_error("No relevant node found" TSRMLS_CC); - goto error; - } - - retval =3D virDomainGetBlockInfo(domain->domain, tmp, &info, 0); - if (retval =3D=3D -1) { - set_error("Cannot get domain block information" TSRMLS_CC); - goto error; - } - - array_init(return_value); - LONGLONG_INIT; - VIRT_ADD_ASSOC_STRING(return_value, "device", dev); - - if (isFile) - VIRT_ADD_ASSOC_STRING(return_value, "file", tmp); - else - VIRT_ADD_ASSOC_STRING(return_value, "partition", tmp); - - free(xpath); - if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/../dri= ver/@type", dev) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - free(tmp); - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (tmp !=3D NULL) - VIRT_ADD_ASSOC_STRING(return_value, "type", tmp); - - LONGLONG_ASSOC(return_value, "capacity", info.capacity); - LONGLONG_ASSOC(return_value, "allocation", info.allocation); - LONGLONG_ASSOC(return_value, "physical", info.physical); - - free(xpath); - free(tmp); - free(xml); - return; - - error: - free(xpath); - free(tmp); - free(xml); - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_xml_xpath - * Since version: 0.4.1(-1) - * Description: Function is used to get the result of xPath expression= that's run against the domain - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @xpath [string]: xPath expression to parse against the= domain - * @flags [int]: optional flags - * Returns: result of the expression in an array - */ -PHP_FUNCTION(libvirt_domain_xml_xpath) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - zval *zpath; - char *xml; - char *tmp =3D NULL; - strsize_t path_len =3D -1; - zend_long flags =3D 0; - int rc =3D 0; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &zpath, &path_len, &flags); - - xml =3D virDomainGetXMLDesc(domain->domain, flags); - if (!xml) - RETURN_FALSE; - - array_init(return_value); - - free(get_string_from_xpath(xml, (char *)zpath, &return_value, &rc)); - if (return_value < 0) { - free(xml); - RETURN_FALSE; - } - - free(tmp); - free(xml); - - if (rc =3D=3D 0) - RETURN_FALSE; - - VIRT_ADD_ASSOC_STRING(return_value, "xpath", (char *)zpath); - if (rc < 0) - add_assoc_long(return_value, "error_code", (long)rc); -} - -/* - * Function name: libvirt_domain_interface_stats - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's interface stats - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @path [string]: path to interface device - * Returns: interface stats array of {tx|rx}_{bytes|packets|errs|d= rop} fields - */ -PHP_FUNCTION(libvirt_domain_interface_stats) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *path; - strsize_t path_len; - - struct _virDomainInterfaceStats stats; - - GET_DOMAIN_FROM_ARGS("rs", &zdomain, &path, &path_len); - - retval =3D virDomainInterfaceStats(domain->domain, path, &stats, sizeo= f stats); - DPRINTF("%s: virDomainInterfaceStats(%p, %s, , ) returned= %d\n", PHPFUNC, domain->domain, path, retval); - if (retval =3D=3D -1) - RETURN_FALSE; - - array_init(return_value); - LONGLONG_INIT; - LONGLONG_ASSOC(return_value, "rx_bytes", stats.rx_bytes); - LONGLONG_ASSOC(return_value, "rx_packets", stats.rx_packets); - LONGLONG_ASSOC(return_value, "rx_errs", stats.rx_errs); - LONGLONG_ASSOC(return_value, "rx_drop", stats.rx_drop); - LONGLONG_ASSOC(return_value, "tx_bytes", stats.tx_bytes); - LONGLONG_ASSOC(return_value, "tx_packets", stats.tx_packets); - LONGLONG_ASSOC(return_value, "tx_errs", stats.tx_errs); - LONGLONG_ASSOC(return_value, "tx_drop", stats.tx_drop); -} - -/* - * Function name: libvirt_domain_get_connect - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's connection resour= ce. This function should *not* be used! - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: libvirt connection resource - */ -PHP_FUNCTION(libvirt_domain_get_connect) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - php_libvirt_connection *conn; - - DPRINTF("%s: Warning: libvirt_domain_get_connect() used. This function= should not be used!\n", PHPFUNC); - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - conn =3D domain->conn; - if (conn->conn =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETURN_RESOURCE(conn->resource); - /* since we're returning already registered resource, bump refcount */ - Z_ADDREF_P(return_value); -} - -/* - * Function name: libvirt_domain_migrate_to_uri - * Since version: 0.4.1(-1) - * Description: Function is used migrate domain to another libvirt dae= mon specified by it's URI - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @dest_uri [string]: destination URI to migrate to - * @flags [int]: migration flags - * @dname [string]: domain name to rename domain to on de= stination side - * @bandwidth [int]: migration bandwidth in Mbps - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_migrate_to_uri) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - long flags =3D 0; - char *duri; - strsize_t duri_len; - char *dname; - strsize_t dname_len; - zend_long bandwidth; - - dname =3D NULL; - dname_len =3D 0; - bandwidth =3D 0; - GET_DOMAIN_FROM_ARGS("rsl|sl", &zdomain, &duri, &duri_len, &flags, &dn= ame, &dname_len, &bandwidth); - - retval =3D virDomainMigrateToURI(domain->domain, duri, flags, dname, b= andwidth); - DPRINTF("%s: virDomainMigrateToURI() returned %d\n", PHPFUNC, retval); - - if (retval =3D=3D 0) - RETURN_TRUE; - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_migrate_to_uri2 - * Since version: 0.4.6(-1) - * Description: Function is used migrate domain to another libvirt dae= mon specified by it's URI - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @dconnuri [string]: URI for target libvirtd - * @miguri [string]: URI for invoking the migration - * @dxml [string]: XML config for launching guest on targ= et - * @flags [int]: migration flags - * @dname [string]: domain name to rename domain to on de= stination side - * @bandwidth [int]: migration bandwidth in Mbps - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_migrate_to_uri2) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *dconnuri; - strsize_t dconnuri_len; - char *miguri; - strsize_t miguri_len; - char *dxml; - strsize_t dxml_len; - zend_long flags =3D 0; - char *dname; - strsize_t dname_len; - zend_long bandwidth; - - dconnuri =3D NULL; - dconnuri_len =3D 0; - miguri =3D NULL; - miguri_len =3D 0; - dxml =3D NULL; - dxml_len =3D 0; - dname =3D NULL; - dname_len =3D 0; - bandwidth =3D 0; - GET_DOMAIN_FROM_ARGS("r|ssslsl", &zdomain, &dconnuri, &dconnuri_len, &= miguri, &miguri_len, &dxml, &dxml_len, &flags, &dname, &dname_len, &bandwid= th); - - // set to NULL if empty string - if (dconnuri_len =3D=3D 0) - dconnuri=3DNULL; - if (miguri_len =3D=3D 0) - miguri=3DNULL; - if (dxml_len =3D=3D 0) - dxml=3DNULL; - if (dname_len =3D=3D 0) - dname=3DNULL; - - retval =3D virDomainMigrateToURI2(domain->domain, dconnuri, miguri, dx= ml, flags, dname, bandwidth); - DPRINTF("%s: virDomainMigrateToURI2() returned %d\n", PHPFUNC, retval); - - if (retval =3D=3D 0) - RETURN_TRUE; - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_migrate - * Since version: 0.4.1(-1) - * Description: Function is used migrate domain to another domain - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @dest_conn [string]: destination host connection object - * @flags [int]: migration flags - * @dname [string]: domain name to rename domain to on de= stination side - * @bandwidth [int]: migration bandwidth in Mbps - * Returns: libvirt domain resource for migrated domain - */ -PHP_FUNCTION(libvirt_domain_migrate) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - php_libvirt_connection *dconn =3D NULL; - zval *zdconn; - virDomainPtr destdomain =3D NULL; - php_libvirt_domain *res_domain; - - zend_long flags =3D 0; - char *dname; - strsize_t dname_len; - zend_long bandwidth; - - dname =3D NULL; - dname_len =3D 0; - bandwidth =3D 0; - - GET_DOMAIN_FROM_ARGS("rrl|sl", &zdomain, &zdconn, &flags, &dname, &dna= me_len, &bandwidth); - - if ((domain->domain =3D=3D NULL) || (domain->conn->conn =3D=3D NULL)) { - set_error("Domain object is not valid" TSRMLS_CC); - RETURN_FALSE; - } - - VIRT_FETCH_RESOURCE(dconn, php_libvirt_connection*, &zdconn, PHP_LIBVI= RT_CONNECTION_RES_NAME, le_libvirt_connection); - if ((dconn =3D=3D NULL) || (dconn->conn =3D=3D NULL)) { - set_error("Destination connection object is not valid" TSRMLS_CC); - RETURN_FALSE; - } - - destdomain =3D virDomainMigrate(domain->domain, dconn->conn, flags, dn= ame, NULL, bandwidth); - if (destdomain =3D=3D NULL) - RETURN_FALSE; - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D destdomain; - res_domain->conn =3D dconn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, dconn->conn, res_domain->= domain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_get_job_info - * Since version: 0.4.1(-1) - * Description: Function is used get job information for the domain - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: job information array of type, time, data, mem and fil= e fields - */ -PHP_FUNCTION(libvirt_domain_get_job_info) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - struct _virDomainJobInfo jobinfo; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainGetJobInfo(domain->domain, &jobinfo); - if (retval =3D=3D -1) - RETURN_FALSE; - - array_init(return_value); - LONGLONG_INIT; - add_assoc_long(return_value, "type", jobinfo.type); - LONGLONG_ASSOC(return_value, "time_elapsed", jobinfo.timeElapsed); - LONGLONG_ASSOC(return_value, "time_remaining", jobinfo.timeRemaining); - LONGLONG_ASSOC(return_value, "data_total", jobinfo.dataTotal); - LONGLONG_ASSOC(return_value, "data_processed", jobinfo.dataProcessed); - LONGLONG_ASSOC(return_value, "data_remaining", jobinfo.dataRemaining); - LONGLONG_ASSOC(return_value, "mem_total", jobinfo.memTotal); - LONGLONG_ASSOC(return_value, "mem_processed", jobinfo.memProcessed); - LONGLONG_ASSOC(return_value, "mem_remaining", jobinfo.memRemaining); - LONGLONG_ASSOC(return_value, "file_total", jobinfo.fileTotal); - LONGLONG_ASSOC(return_value, "file_processed", jobinfo.fileProcessed); - LONGLONG_ASSOC(return_value, "file_remaining", jobinfo.fileRemaining); -} - -/* - * Function name: libvirt_domain_has_current_snapshot - * Since version: 0.4.1(-2) - * Description: Function is used to get the information whether domain= has the current snapshot - * Arguments: @res [resource]: libvirt domain resource - * @flags [int]: libvirt snapshot flags - * Returns: TRUE is domain has the current snapshot, otherwise FAL= SE (you may need to check for error using libvirt_get_last_error()) - */ -PHP_FUNCTION(libvirt_domain_has_current_snapshot) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - retval =3D virDomainHasCurrentSnapshot(domain->domain, flags); - if (retval <=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_snapshot_lookup_by_name - * Since version: 0.4.1(-2) - * Description: This functions is used to lookup for the snapshot by i= t's name - * Arguments: @res [resource]: libvirt domain resource - * @name [string]: name of the snapshot to get the resour= ce - * @flags [int]: libvirt snapshot flags - * Returns: domain snapshot resource - */ -PHP_FUNCTION(libvirt_domain_snapshot_lookup_by_name) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - strsize_t name_len; - char *name =3D NULL; - zend_long flags =3D 0; - php_libvirt_snapshot *res_snapshot; - virDomainSnapshotPtr snapshot =3D NULL; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &name, &name_len, &flags); - - if ((name =3D=3D NULL) || (name_len < 1)) - RETURN_FALSE; - snapshot=3DvirDomainSnapshotLookupByName(domain->domain, name, flags); - if (snapshot =3D=3D NULL) - RETURN_FALSE; - - res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); - res_snapshot->domain =3D domain; - res_snapshot->snapshot =3D snapshot; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); - resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); -} - -/* - * Function name: libvirt_domain_snapshot_create - * Since version: 0.4.1(-2) - * Description: This function creates the domain snapshot for the doma= in identified by it's resource - * Arguments: @res [resource]: libvirt domain resource - * @flags [int]: libvirt snapshot flags - * Returns: domain snapshot resource - */ -PHP_FUNCTION(libvirt_domain_snapshot_create) -{ - php_libvirt_domain *domain =3D NULL; - php_libvirt_snapshot *res_snapshot; - zval *zdomain; - virDomainSnapshotPtr snapshot =3D NULL; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - snapshot =3D virDomainSnapshotCreateXML(domain->domain, "", flags); - DPRINTF("%s: virDomainSnapshotCreateXML(%p, ) returned %p\n", PHP= FUNC, domain->domain, snapshot); - if (snapshot =3D=3D NULL) - RETURN_FALSE; - - res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); - res_snapshot->domain =3D domain; - res_snapshot->snapshot =3D snapshot; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); - resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); -} - -/* - * Function name: libvirt_domain_snapshot_get_xml - * Since version: 0.4.1(-2) - * Description: Function is used to get the XML description of the sna= pshot identified by it's resource - * Arguments: @res [resource]: libvirt snapshot resource - * @flags [int]: libvirt snapshot flags - * Returns: XML description string for the snapshot - */ -PHP_FUNCTION(libvirt_domain_snapshot_get_xml) -{ - char *xml; - zval *zsnapshot; - php_libvirt_snapshot *snapshot; - zend_long flags =3D 0; - - GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); - - xml =3D virDomainSnapshotGetXMLDesc(snapshot->snapshot, flags); - if (xml =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETVAL_STRING(xml); - free(xml); -} - -/* - * Function name: libvirt_domain_snapshot_revert - * Since version: 0.4.1(-2) - * Description: Function is used to revert the domain state to the sta= te identified by the snapshot - * Arguments: @res [resource]: libvirt snapshot resource - * @flags [int]: libvirt snapshot flags - * Returns: TRUE on success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_snapshot_revert) -{ - zval *zsnapshot; - php_libvirt_snapshot *snapshot; - int ret; - zend_long flags =3D 0; - - GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); - - ret =3D virDomainRevertToSnapshot(snapshot->snapshot, flags); - DPRINTF("%s: virDomainRevertToSnapshot(%p, 0) returned %d\n", PHPFUNC,= snapshot->snapshot, ret); - if (ret =3D=3D -1) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_snapshot_delete - * Since version: 0.4.1(-2) - * Description: Function is used to revert the domain state to the sta= te identified by the snapshot - * Arguments: @res [resource]: libvirt snapshot resource - * @flags [int]: 0 to delete just snapshot, VIR_SNAPSHOT_= DELETE_CHILDREN to delete snapshot children as well - * Returns: TRUE on success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_snapshot_delete) -{ - zval *zsnapshot; - php_libvirt_snapshot *snapshot; - zend_long flags =3D 0; - int retval; - - GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); - - retval =3D virDomainSnapshotDelete(snapshot->snapshot, flags); - DPRINTF("%s: virDomainSnapshotDelete(%p, %d) returned %d\n", PHPFUNC, = snapshot->snapshot, (int) flags, retval); - if (retval =3D=3D -1) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_list_domain_snapshots - * Since version: 0.4.1(-2) - * Description: Function is used to list domain snapshots for the doma= in specified by it's resource - * Arguments: @res [resource]: libvirt domain resource - * @flags [int]: libvirt snapshot flags - * Returns: libvirt domain snapshot names array - */ -PHP_FUNCTION(libvirt_list_domain_snapshots) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int count =3D -1; - int expectedcount =3D -1; - char **names; - zend_long flags =3D 0; - int i; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - expectedcount =3D virDomainSnapshotNum(domain->domain, flags); - DPRINTF("%s: virDomainSnapshotNum(%p, 0) returned %d\n", PHPFUNC, doma= in->domain, expectedcount); - - if (expectedcount !=3D -1) { - names =3D (char **)emalloc(expectedcount * sizeof(char *)); - count =3D virDomainSnapshotListNames(domain->domain, names, expect= edcount, 0); - } + if (expectedcount !=3D -1) { + names =3D (char **)emalloc(expectedcount * sizeof(char *)); + count =3D virDomainSnapshotListNames(domain->domain, names, expect= edcount, 0); + } =20 if ((count !=3D expectedcount) || (count < 0)) { RETURN_FALSE; @@ -7214,271 +4054,6 @@ PHP_FUNCTION(libvirt_list_inactive_storagepools) } =20 /* - * Function name: libvirt_list_domains - * Since version: 0.4.1(-1) - * Description: Function is used to list domains on the connection - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt domain names array for the connection - */ -PHP_FUNCTION(libvirt_list_domains) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - int *ids; - char **names; - const char *name; - int i, rv; - virDomainPtr domain =3D NULL; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) - RETURN_FALSE; - - DPRINTF("%s: Found %d domains\n", PHPFUNC, expectedcount); - - ids =3D (int *)emalloc(sizeof(int) * expectedcount); - count =3D virConnectListDomains(conn->conn, ids, expectedcount); - DPRINTF("%s: virConnectListDomains returned %d domains\n", PHPFUNC, co= unt); - - array_init(return_value); - for (i =3D 0; i < count; i++) { - domain =3D virDomainLookupByID(conn->conn, ids[i]); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, domain, 1= TSRMLS_CC); - if (domain !=3D NULL) { - name =3D virDomainGetName(domain); - if (name !=3D NULL) { - DPRINTF("%s: Found running domain %s with ID =3D %d\n", PH= PFUNC, name, ids[i]); - VIRT_ADD_NEXT_INDEX_STRING(return_value, name); - } else { - DPRINTF("%s: Cannot get ID for running domain %d\n", PHPFU= NC, ids[i]); - } - } - rv =3D virDomainFree(domain); - if (rv !=3D 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainFree fai= led with %i on list_domain: %s", - rv, LIBVIRT_G(last_error)); - } else { - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, domai= n, 0 TSRMLS_CC); - } - domain =3D NULL; - } - efree(ids); - - expectedcount =3D virConnectNumOfDefinedDomains(conn->conn); - DPRINTF("%s: virConnectNumOfDefinedDomains returned %d domains\n", PHP= FUNC, expectedcount); - if (expectedcount < 0) { - DPRINTF("%s: virConnectNumOfDefinedDomains failed with error code = %d\n", PHPFUNC, expectedcount); - RETURN_FALSE; - } - - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virConnectListDefinedDomains(conn->conn, names, expectedcoun= t); - DPRINTF("%s: virConnectListDefinedDomains returned %d domains\n", PHPF= UNC, count); - if (count < 0) { - DPRINTF("%s: virConnectListDefinedDomains failed with error code %= d\n", PHPFUNC, count); - RETURN_FALSE; - } - - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - DPRINTF("%s: Found inactive domain %s\n", PHPFUNC, names[i]); - free(names[i]); - } - efree(names); -} - -/* - * Function name: libvirt_list_domain_resources - * Since version: 0.4.1(-1) - * Description: Function is used to list domain resources on the conne= ction - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt domain resources array for the connection - */ -PHP_FUNCTION(libvirt_list_domain_resources) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - int *ids; - char **names; - int i; - - virDomainPtr domain =3D NULL; - php_libvirt_domain *res_domain; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) - RETURN_FALSE; - - ids =3D (int *)emalloc(sizeof(int) * expectedcount); - count =3D virConnectListDomains(conn->conn, ids, expectedcount); - if ((count !=3D expectedcount) || (count < 0)) { - efree(ids); - RETURN_FALSE; - } - array_init(return_value); - for (i =3D 0; i < count; i++) { - domain =3D virDomainLookupByID(conn->conn, ids[i]); - if (domain !=3D NULL) { - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvir= t_domain)); - res_domain->domain =3D domain; - - res_domain->conn =3D conn; - - VIRT_REGISTER_LIST_RESOURCE(domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_d= omain->domain, 1 TSRMLS_CC); - } - } - efree(ids); - - if ((expectedcount =3D virConnectNumOfDefinedDomains(conn->conn)) < 0) - RETURN_FALSE; - - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virConnectListDefinedDomains(conn->conn, names, expectedcoun= t); - if ((count !=3D expectedcount) || (count < 0)) { - efree(names); - RETURN_FALSE; - } - for (i =3D 0; i < count; i++) { - domain =3D virDomainLookupByName(conn->conn, names[i]); - if (domain !=3D NULL) { - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvir= t_domain)); - res_domain->domain =3D domain; - - res_domain->conn =3D conn; - - VIRT_REGISTER_LIST_RESOURCE(domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_d= omain->domain, 1 TSRMLS_CC); - } - free(names[i]); - } - efree(names); -} - -/* - * Function name: libvirt_list_active_domain_ids - * Since version: 0.4.1(-1) - * Description: Function is used to list active domain IDs on the conn= ection - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt active domain ids array for the connection - */ -PHP_FUNCTION(libvirt_list_active_domain_ids) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - int *ids; - int i; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) - RETURN_FALSE; - - ids =3D (int *)emalloc(sizeof(int) * expectedcount); - count =3D virConnectListDomains(conn->conn, ids, expectedcount); - if ((count !=3D expectedcount) || (count < 0)) { - efree(ids); - RETURN_FALSE; - } - array_init(return_value); - for (i =3D 0; i < count; i++) - add_next_index_long(return_value, ids[i]); - efree(ids); -} - -/* - * Function name: libvirt_list_active_domains - * Since version: 0.4.1(-1) - * Description: Function is used to list active domain names on the co= nnection - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt active domain names array for the connection - */ -PHP_FUNCTION(libvirt_list_active_domains) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - int *ids; - int i; - virDomainPtr domain =3D NULL; - const char *name; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) - RETURN_FALSE; - - ids =3D (int *)emalloc(sizeof(int) * expectedcount); - count =3D virConnectListDomains(conn->conn, ids, expectedcount); - if ((count !=3D expectedcount) || (count < 0)) { - efree(ids); - RETURN_FALSE; - } - - array_init(return_value); - for (i =3D 0; i < count; i++) { - domain =3D virDomainLookupByID(conn->conn, ids[i]); - if (domain !=3D NULL) { - if (!(name =3D virDomainGetName(domain))) { - efree(ids); - RETURN_FALSE; - } - - VIRT_ADD_NEXT_INDEX_STRING(return_value, name); - - if (virDomainFree(domain)) - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, d= omain, 0 TSRMLS_CC); - } - } - efree(ids); -} - -/* - * Function name: libvirt_list_inactive_domains - * Since version: 0.4.1(-1) - * Description: Function is used to list inactive domain names on the = connection - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt inactive domain names array for the connection - */ -PHP_FUNCTION(libvirt_list_inactive_domains) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - char **names; - int i; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((expectedcount =3D virConnectNumOfDefinedDomains(conn->conn)) < 0) - RETURN_FALSE; - - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virConnectListDefinedDomains(conn->conn, names, expectedcoun= t); - if ((count !=3D expectedcount) || (count < 0)) { - efree(names); - RETURN_FALSE; - } - - array_init(return_value); - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - efree(names); -} - -/* * Function name: libvirt_list_all_networks * Since version: 0.5.3 * Description: Function is used to list networks on the connection diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 1a009d3..a27ed82 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -127,21 +127,7 @@ typedef uint64_t arch_uint; #define PHP_LIBVIRT_WORLD_VERSION VERSION #define PHP_LIBVIRT_WORLD_EXTNAME "libvirt" =20 -/* Domain flags */ -#define DOMAIN_FLAG_FEATURE_ACPI 0x01 -#define DOMAIN_FLAG_FEATURE_APIC 0x02 -#define DOMAIN_FLAG_FEATURE_PAE 0x04 -#define DOMAIN_FLAG_CLOCK_LOCALTIME 0x08 -#define DOMAIN_FLAG_TEST_LOCAL_VNC 0x10 -#define DOMAIN_FLAG_SOUND_AC97 0x20 - -/* Domain disk flags */ -#define DOMAIN_DISK_FILE 0x01 -#define DOMAIN_DISK_BLOCK 0x02 -#define DOMAIN_DISK_ACCESS_ALL 0x04 - /* Internal resource identifier objects */ -#define INT_RESOURCE_DOMAIN 0x02 #define INT_RESOURCE_NETWORK 0x04 #define INT_RESOURCE_NODEDEV 0x08 #define INT_RESOURCE_STORAGEPOOL 0x10 @@ -176,15 +162,11 @@ typedef struct tVMNetwork { char *model; } tVMNetwork; =20 -/* TODO: temporary forward declaration until other parts are "modularized"= */ +/* TODO: temporary forward declarations until other parts are "modularized= " */ typedef struct _php_libvirt_connection php_libvirt_connection; +typedef struct _php_libvirt_domain php_libvirt_domain; =20 /* Libvirt-php types */ -typedef struct _php_libvirt_domain { - virDomainPtr domain; - php_libvirt_connection* conn; -} php_libvirt_domain; - typedef struct _php_libvirt_snapshot { virDomainSnapshotPtr snapshot; php_libvirt_domain* domain; @@ -239,10 +221,13 @@ ZEND_DECLARE_MODULE_GLOBALS(libvirt) =20 /* Private definitions */ void set_error(char *msg TSRMLS_DC); +void set_error_if_unset(char *msg TSRMLS_DC); void reset_error(TSRMLS_D); int count_resources(int type TSRMLS_DC); -int resource_change_counter(int type, virConnectPtr conn, void *mem, int i= nc TSRMLS_DC); -int check_resource_allocation(virConnectPtr conn, int type, void *mem TSRM= LS_DC); +int resource_change_counter(int type, virConnectPtr conn, void *mem, + int inc TSRMLS_DC); +int check_resource_allocation(virConnectPtr conn, int type, + void *mem TSRMLS_DC); void free_resource(int type, void *mem TSRMLS_DC); char *connection_get_emulator(virConnectPtr conn, char *arch TSRMLS_DC); int is_local_connection(virConnectPtr conn); @@ -251,8 +236,18 @@ void free_tokens(tTokenizer t); int set_logfile(char *filename, long maxsize TSRMLS_DC); char *get_string_from_xpath(char *xml, char *xpath, zval **val, int *retVa= l); char **get_array_from_xpath(char *xml, char *xpath, int *num); +void parse_array(zval *arr, tVMDisk *disk, tVMNetwork *network); +char *installation_get_xml(int step, virConnectPtr conn, char *name, int m= emMB, + int maxmemMB, char *arch, char *uuid, int vCpus, + char *iso_image, tVMDisk *disks, int numDisks, + tVMNetwork *networks, int numNetworks, + int domain_flags TSRMLS_DC); +void set_vnc_location(char *msg TSRMLS_DC); +int streamSink(virStreamPtr st ATTRIBUTE_UNUSED, + const char *bytes, size_t nbytes, void *opaque); +const char *get_feature_binary(const char *name); +long get_next_free_numeric_value(virDomainPtr domain, char *xpath); =20 -#define PHP_LIBVIRT_DOMAIN_RES_NAME "Libvirt domain" #define PHP_LIBVIRT_STORAGEPOOL_RES_NAME "Libvirt storagepool" #define PHP_LIBVIRT_VOLUME_RES_NAME "Libvirt volume" #define PHP_LIBVIRT_NETWORK_RES_NAME "Libvirt virtual network" @@ -268,79 +263,6 @@ PHP_MINFO_FUNCTION(libvirt); =20 /* Common functions */ PHP_FUNCTION(libvirt_get_last_error); -/* Domain functions */ -PHP_FUNCTION(libvirt_domain_new); -PHP_FUNCTION(libvirt_domain_new_get_vnc); -PHP_FUNCTION(libvirt_domain_get_counts); -PHP_FUNCTION(libvirt_domain_is_persistent); -PHP_FUNCTION(libvirt_domain_lookup_by_name); -PHP_FUNCTION(libvirt_domain_get_xml_desc); -PHP_FUNCTION(libvirt_domain_get_disk_devices); -PHP_FUNCTION(libvirt_domain_get_interface_devices); -PHP_FUNCTION(libvirt_domain_get_screenshot); -PHP_FUNCTION(libvirt_domain_get_screenshot_api); -PHP_FUNCTION(libvirt_domain_get_screen_dimensions); -PHP_FUNCTION(libvirt_domain_change_vcpus); -PHP_FUNCTION(libvirt_domain_change_memory); -PHP_FUNCTION(libvirt_domain_change_boot_devices); -PHP_FUNCTION(libvirt_domain_disk_add); -PHP_FUNCTION(libvirt_domain_disk_remove); -PHP_FUNCTION(libvirt_domain_nic_add); -PHP_FUNCTION(libvirt_domain_nic_remove); -PHP_FUNCTION(libvirt_domain_attach_device); -PHP_FUNCTION(libvirt_domain_detach_device); -PHP_FUNCTION(libvirt_domain_get_info); -PHP_FUNCTION(libvirt_domain_get_uuid); -PHP_FUNCTION(libvirt_domain_get_uuid_string); -PHP_FUNCTION(libvirt_domain_get_name); -PHP_FUNCTION(libvirt_domain_get_id); -PHP_FUNCTION(libvirt_domain_lookup_by_id); -PHP_FUNCTION(libvirt_domain_lookup_by_uuid); -PHP_FUNCTION(libvirt_domain_lookup_by_uuid_string); -PHP_FUNCTION(libvirt_domain_destroy); -PHP_FUNCTION(libvirt_domain_create); -PHP_FUNCTION(libvirt_domain_resume); -PHP_FUNCTION(libvirt_domain_core_dump); -PHP_FUNCTION(libvirt_domain_shutdown); -PHP_FUNCTION(libvirt_domain_suspend); -PHP_FUNCTION(libvirt_domain_managedsave); -PHP_FUNCTION(libvirt_domain_undefine); -PHP_FUNCTION(libvirt_domain_reboot); -PHP_FUNCTION(libvirt_domain_define_xml); -PHP_FUNCTION(libvirt_domain_create_xml); -PHP_FUNCTION(libvirt_domain_xml_from_native); -PHP_FUNCTION(libvirt_domain_xml_to_native); -PHP_FUNCTION(libvirt_domain_set_max_memory); -PHP_FUNCTION(libvirt_domain_set_memory); -PHP_FUNCTION(libvirt_domain_set_memory_flags); -PHP_FUNCTION(libvirt_domain_memory_peek); -PHP_FUNCTION(libvirt_domain_memory_stats); -PHP_FUNCTION(libvirt_domain_update_device); -PHP_FUNCTION(libvirt_domain_block_commit); -PHP_FUNCTION(libvirt_domain_block_stats); -PHP_FUNCTION(libvirt_domain_block_resize); -PHP_FUNCTION(libvirt_domain_block_job_abort); -PHP_FUNCTION(libvirt_domain_block_job_set_speed); -PHP_FUNCTION(libvirt_domain_block_job_info); -PHP_FUNCTION(libvirt_domain_interface_stats); -PHP_FUNCTION(libvirt_domain_get_connect); -PHP_FUNCTION(libvirt_domain_migrate); -PHP_FUNCTION(libvirt_domain_get_job_info); -PHP_FUNCTION(libvirt_domain_xml_xpath); -PHP_FUNCTION(libvirt_domain_get_block_info); -PHP_FUNCTION(libvirt_domain_get_network_info); -PHP_FUNCTION(libvirt_domain_migrate_to_uri); -PHP_FUNCTION(libvirt_domain_migrate_to_uri2); -PHP_FUNCTION(libvirt_domain_get_autostart); -PHP_FUNCTION(libvirt_domain_set_autostart); -PHP_FUNCTION(libvirt_domain_is_active); -PHP_FUNCTION(libvirt_domain_get_next_dev_ids); -PHP_FUNCTION(libvirt_domain_send_keys); -PHP_FUNCTION(libvirt_domain_send_key_api); -PHP_FUNCTION(libvirt_domain_send_pointer_event); -PHP_FUNCTION(libvirt_domain_get_metadata); -PHP_FUNCTION(libvirt_domain_set_metadata); -PHP_FUNCTION(libvirt_domain_qemu_agent_command); /* Domain snapshot functions */ PHP_FUNCTION(libvirt_domain_has_current_snapshot); PHP_FUNCTION(libvirt_domain_snapshot_create); @@ -415,12 +337,7 @@ PHP_FUNCTION(libvirt_list_all_networks); PHP_FUNCTION(libvirt_list_networks); PHP_FUNCTION(libvirt_list_all_nwfilters); PHP_FUNCTION(libvirt_list_nwfilters); -PHP_FUNCTION(libvirt_list_domains); PHP_FUNCTION(libvirt_list_domain_snapshots); -PHP_FUNCTION(libvirt_list_domain_resources); -PHP_FUNCTION(libvirt_list_active_domains); -PHP_FUNCTION(libvirt_list_active_domain_ids); -PHP_FUNCTION(libvirt_list_inactive_domains); PHP_FUNCTION(libvirt_list_storagepools); PHP_FUNCTION(libvirt_list_active_storagepools); PHP_FUNCTION(libvirt_list_inactive_storagepools); --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list