From nobody Fri May 16 04:03:10 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 1501624071374530.178770088587; Tue, 1 Aug 2017 14:47:51 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1F7D231CE55; Tue, 1 Aug 2017 21:47:49 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F0C4618206; Tue, 1 Aug 2017 21:47:48 +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 9CF963888; Tue, 1 Aug 2017 21:47:48 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkXKf007926 for ; Tue, 1 Aug 2017 17:46:33 -0400 Received: by smtp.corp.redhat.com (Postfix) id A04CC18152; Tue, 1 Aug 2017 21:46:33 +0000 (UTC) Received: from mx1.redhat.com (ext-mx10.extmail.prod.ext.phx2.redhat.com [10.5.110.39]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9952217D5F for ; Tue, 1 Aug 2017 21:46:33 +0000 (UTC) Received: from mail-qk0-f176.google.com (mail-qk0-f176.google.com [209.85.220.176]) (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 C706C25F951 for ; Tue, 1 Aug 2017 21:46:25 +0000 (UTC) Received: by mail-qk0-f176.google.com with SMTP id d136so17050951qkg.3 for ; Tue, 01 Aug 2017 14:46:25 -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.23 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:23 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1F7D231CE55 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 1F7D231CE55 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mS+0Ex8w" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C706C25F951 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com C706C25F951 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=aX24wNK/H80PyLBeOnYHsGh9vWCfTkQ3pC0JmkXdjag=; b=mS+0Ex8wA/ISjNYnLbDL805+qSBXPgjUnTbOigWoeGK22W/UTOXtcik8LQwQO2NRPj LiOcWQcZuQraOQsapnBlBRR5EHVos+lKONssx/LQ4f85Mjvcv0cDRTgERMBCsqhNWUpD C4lomFFmVl+7cdcwb125EbBW/refNwQULaBAtUsbvzu0hvm8PuZhA49LlqGyWLegVfba F1jpjECfeL8X0rIkEt6YvINDJ1kQRBikjxoe/cmLWrVarp35cxiP17G8t7KlB1D5uDCZ XP65AGgtzKvukV2ouXMrpKW6VzqtM7UjXu2ZPePkZTM807Bd0QmduyxhvsXshTLJfbVp GGlw== 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=aX24wNK/H80PyLBeOnYHsGh9vWCfTkQ3pC0JmkXdjag=; b=lfM6P1aXr7cl4k17zqvk+qolE6KgPcCu9m7UL1cBD9dMBe7we6e7uMUN2kfQ9dDR3u BwDcGU7ylXVyZ3+PlRQ5Q9bgeioxp9wQEDMCdBIZwILEkutxlEB7O5ayyXkRgqSkuwKC 2gWsX9lT8fGav4M9EI2qnV0jUKK97iKaVEAxK7/LV0dFbtZ+WzkKQg8hmBrHRVEDU0cC uI8KCCOEYbL3dAdCaYIWc6kc5ct98lqPb435HjhSahjsc6Ibj9IW6jauXMPJWG2CFw+v xc46aPx3/k+7ojL7LBFKhvuxXOOgB5IjsVRgrL7bLT89YPmKKQgzQyBjEuiT4Vgd4yG8 0AAg== X-Gm-Message-State: AIVw113akYG0LHrEo55RKt2k44rGBNGqfufgX2cN3gB9oVwle1Fmkiqi pFmUsEQkOWctL1aAnPw= X-Received: by 10.55.207.85 with SMTP id e82mr26022390qkj.235.1501623984362; Tue, 01 Aug 2017 14:46:24 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:12 -0400 Message-Id: <20170801214614.8915-12-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.39]); Tue, 01 Aug 2017 21:46:26 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 01 Aug 2017 21:46:26 +0000 (UTC) for IP:'209.85.220.176' DOMAIN:'mail-qk0-f176.google.com' HELO:'mail-qk0-f176.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: -3.21 (DCC_REPUT_00_12, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.220.176 mail-qk0-f176.google.com 209.85.220.176 mail-qk0-f176.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.39 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 11/13] Split up the bindings for libvirt nodedev 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.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 01 Aug 2017 21:47:50 +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-nodedev.c | 339 ++++++++++++++++++++++++++++++++++++++++++++++= ++ src/libvirt-nodedev.h | 54 ++++++++ src/libvirt-php.c | 353 +---------------------------------------------= ---- src/libvirt-php.h | 13 -- 5 files changed, 398 insertions(+), 364 deletions(-) create mode 100644 src/libvirt-nodedev.c create mode 100644 src/libvirt-nodedev.h diff --git a/src/Makefile.am b/src/Makefile.am index 1b78011..30bebad 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,8 +27,9 @@ libvirt_php_la_SOURCES =3D \ libvirt-stream.c libvirt-stream.h \ libvirt-domain.c libvirt-domain.h \ libvirt-snapshot.c libvirt-snapshot.h \ + libvirt-storage.c libvirt-storage.h \ libvirt-network.c libvirt-network.h \ - libvirt-storage.c libvirt-storage.h + libvirt-nodedev.c libvirt-nodedev.h libvirt_php_la_CFLAGS =3D \ $(AM_CFLAGS) \ -DCOMPILE_DL_LIBVIRT=3D1 diff --git a/src/libvirt-nodedev.c b/src/libvirt-nodedev.c new file mode 100644 index 0000000..06a420d --- /dev/null +++ b/src/libvirt-nodedev.c @@ -0,0 +1,339 @@ +/* + * libvirt-nodedev.c: The PHP bindings to libvirt nodedev API + * + * See COPYING for the license of this software + */ + +#include + +#include "libvirt-nodedev.h" + +DEBUG_INIT("nodedev"); + +void +php_libvirt_nodedev_dtor(virt_resource *rsrc TSRMLS_DC) +{ + php_libvirt_nodedev *nodedev =3D (php_libvirt_nodedev *)rsrc->ptr; + int rv =3D 0; + + if (nodedev !=3D NULL) { + if (nodedev->device !=3D NULL) { + if (!check_resource_allocation(nodedev->conn->conn, INT_RESOUR= CE_NODEDEV, nodedev->device TSRMLS_CC)) { + nodedev->device =3D NULL; + efree(nodedev); + return; + } + rv =3D virNodeDeviceFree(nodedev->device); + if (rv !=3D 0) { + DPRINTF("%s: virNodeDeviceFree(%p) returned %d (%s)\n", __= FUNCTION__, nodedev->device, rv, LIBVIRT_G(last_error)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStorageVol= Free failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virNodeDeviceFree(%p) completed successfully\= n", __FUNCTION__, nodedev->device); + resource_change_counter(INT_RESOURCE_NODEDEV, nodedev->con= n->conn, nodedev->device, 0 TSRMLS_CC); + } + nodedev->device =3D NULL; + } + efree(nodedev); + } +} + +/* + * Function name: libvirt_nodedev_get + * Since version: 0.4.1(-1) + * Description: Function is used to get the node device by it's name + * Arguments: @res [resource]: libvirt connection resource + * @name [string]: name of the nodedev to get resource + * Returns: libvirt nodedev resource + */ +PHP_FUNCTION(libvirt_nodedev_get) +{ + php_libvirt_connection *conn =3D NULL; + php_libvirt_nodedev *res_dev =3D NULL; + virNodeDevice *dev; + zval *zconn; + char *name; + strsize_t name_len; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); + + if ((dev =3D virNodeDeviceLookupByName(conn->conn, name)) =3D=3D NULL)= { + set_error("Cannot get find requested node device" TSRMLS_CC); + RETURN_FALSE; + } + + res_dev =3D (php_libvirt_nodedev *)emalloc(sizeof(php_libvirt_nodedev)= ); + res_dev->device =3D dev; + res_dev->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_dev->device); + resource_change_counter(INT_RESOURCE_NODEDEV, conn->conn, res_dev->dev= ice, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_dev, le_libvirt_nodedev); +} + +/* + * Function name: libvirt_nodedev_capabilities + * Since version: 0.4.1(-1) + * Description: Function is used to list node devices by capabilities + * Arguments: @res [resource]: libvirt nodedev resource + * Returns: nodedev capabilities array + */ +PHP_FUNCTION(libvirt_nodedev_capabilities) +{ + php_libvirt_nodedev *nodedev =3D NULL; + zval *znodedev; + int count =3D -1; + int expectedcount =3D -1; + char **names; + int i; + + GET_NODEDEV_FROM_ARGS("r", &znodedev); + + if ((expectedcount =3D virNodeDeviceNumOfCaps(nodedev->device)) < 0) + RETURN_FALSE; + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virNodeDeviceListCaps(nodedev->device, names, expectedcount); + if ((count !=3D expectedcount) || (count < 0)) + 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_nodedev_get_xml_desc + * Since version: 0.4.1(-1), changed 0.4.2 + * Description: Function is used to get the node device's XML descript= ion + * Arguments: @res [resource]: libvirt nodedev resource + * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL + * Returns: nodedev XML description string or result of xPath expr= ession + */ +PHP_FUNCTION(libvirt_nodedev_get_xml_desc) +{ + php_libvirt_nodedev *nodedev =3D NULL; + zval *znodedev; + char *tmp =3D NULL; + char *xml =3D NULL; + char *xpath =3D NULL; + strsize_t xpath_len; + int retval =3D -1; + + GET_NODEDEV_FROM_ARGS("r|s", &znodedev, &xpath, &xpath_len); + if (xpath_len < 1) + xpath =3D NULL; + + xml =3D virNodeDeviceGetXMLDesc(nodedev->device, 0); + if (!xml) { + set_error("Cannot get the device XML information" 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(xml); + free(tmp); +} + +/* + * Function name: libvirt_nodedev_get_information + * Since version: 0.4.1(-1) + * Description: Function is used to get the node device's information + * Arguments: @res [resource]: libvirt nodedev resource + * Returns: nodedev information array + */ +PHP_FUNCTION(libvirt_nodedev_get_information) +{ + php_libvirt_nodedev *nodedev =3D NULL; + zval *znodedev; + int retval =3D -1; + char *xml =3D NULL; + char *tmp =3D NULL; + char *cap =3D NULL; + + GET_NODEDEV_FROM_ARGS("r", &znodedev); + + xml =3D virNodeDeviceGetXMLDesc(nodedev->device, 0); + if (!xml) { + set_error("Cannot get the device XML information" TSRMLS_CC); + RETURN_FALSE; + } + + array_init(return_value); + + /* Get name */ + tmp =3D get_string_from_xpath(xml, "//device/name", NULL, &retval); + if (tmp =3D=3D NULL) { + set_error("Invalid XPath node for device name" TSRMLS_CC); + goto error; + } + + if (retval < 0) { + set_error("Cannot get XPath expression result for device name" TSR= MLS_CC); + goto error; + } + + VIRT_ADD_ASSOC_STRING(return_value, "name", tmp); + + /* Get parent name */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/parent", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "parent", tmp); + + /* Get capability */ + cap =3D get_string_from_xpath(xml, "//device/capability/@type", NULL, = &retval); + if ((cap !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "capability", cap); + + /* System capability is having hardware and firmware sub-blocks */ + if (strcmp(cap, "system") =3D=3D 0) { + /* Get hardware vendor */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/v= endor", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "hardware_vendor", tmp); + + /* Get hardware version */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/v= ersion", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "hardware_version", tmp); + + /* Get hardware serial */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/s= erial", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "hardware_serial", tmp); + + /* Get hardware UUID */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/u= uid", NULL, &retval); + if (tmp !=3D NULL) + VIRT_ADD_ASSOC_STRING(return_value, "hardware_uuid", tmp); + + /* Get firmware vendor */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/firmware/v= endor", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "firmware_vendor", tmp); + + /* Get firmware version */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/firmware/v= ersion", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "firmware_version", tmp); + + /* Get firmware release date */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/firmware/r= elease_date", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "firmware_release_date", t= mp); + } + + /* Get product_id */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/product/@id", = NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "product_id", tmp); + + /* Get product_name */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/product", NULL= , &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "product_name", tmp); + + /* Get vendor_id */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/vendor/@id", N= ULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "vendor_id", tmp); + + /* Get vendor_name */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/vendor", NULL,= &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "vendor_name", tmp); + + /* Get driver name */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/driver/name", NULL, &retv= al); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "driver_name", tmp); + + /* Get driver name */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/interface", NU= LL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "interface_name", tmp); + + /* Get driver name */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/address", NULL= , &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "address", tmp); + + /* Get driver name */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/capability/@ty= pe", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "capabilities", tmp); + + free(cap); + free(tmp); + free(xml); + return; + + error: + free(cap); + free(tmp); + free(xml); + RETURN_FALSE; +} + +/* + * Function name: libvirt_list_nodedevs + * Since version: 0.4.1(-1) + * Description: Function is used to list node devices on the connection + * Arguments: @res [resource]: libvirt connection resource + * @cap [string]: optional capability string + * Returns: libvirt nodedev names array for the connection + */ +PHP_FUNCTION(libvirt_list_nodedevs) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + char *cap =3D NULL; + char **names; + int i; + strsize_t cap_len; + + GET_CONNECTION_FROM_ARGS("r|s", &zconn, &cap, &cap_len); + + if ((expectedcount =3D virNodeNumOfDevices(conn->conn, cap, 0)) < 0) + RETURN_FALSE; + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virNodeListDevices(conn->conn, cap, names, expectedcount, 0); + 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-nodedev.h b/src/libvirt-nodedev.h new file mode 100644 index 0000000..350b213 --- /dev/null +++ b/src/libvirt-nodedev.h @@ -0,0 +1,54 @@ +/* + * libvirt-nodedev.h: The PHP bindings to libvirt nodedev API + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_NODEDEV_H__ +# define __LIBVIRT_NODEDEV_H__ + +# include "libvirt-connection.h" + +# define PHP_LIBVIRT_NODEDEV_RES_NAME "Libvirt node device" +# define INT_RESOURCE_NODEDEV 0x08 + +# define PHP_FE_LIBVIRT_NODEDEV = \ + PHP_FE(libvirt_nodedev_get, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_nodedev_capabilities, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_nodedev_get_xml_desc, arginfo_libvirt_conn_xpath) = \ + PHP_FE(libvirt_nodedev_get_information, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_list_nodedevs, arginfo_libvirt_conn_optcap) + +# define GET_NODEDEV_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(nodedev, php_libvirt_nodedev*, &znodedev, = \ + PHP_LIBVIRT_NODEDEV_RES_NAME, le_libvirt_noded= ev); \ + if (nodedev =3D=3D NULL || nodedev->device =3D=3D NULL) = \ + RETURN_FALSE; = \ + } while (0) + +int le_libvirt_nodedev; + +typedef struct _php_libvirt_nodedev { + virNodeDevicePtr device; + php_libvirt_connection* conn; +} php_libvirt_nodedev; + + +void php_libvirt_nodedev_dtor(virt_resource *rsrc TSRMLS_DC); + +PHP_FUNCTION(libvirt_nodedev_get); +PHP_FUNCTION(libvirt_nodedev_capabilities); +PHP_FUNCTION(libvirt_nodedev_get_xml_desc); +PHP_FUNCTION(libvirt_nodedev_get_information); +PHP_FUNCTION(libvirt_list_nodedevs); + +#endif diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 8814e54..b62bcac 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -25,8 +25,9 @@ #include "libvirt-stream.h" #include "libvirt-domain.h" #include "libvirt-snapshot.h" -#include "libvirt-network.h" #include "libvirt-storage.h" +#include "libvirt-network.h" +#include "libvirt-nodedev.h" =20 DEBUG_INIT("core"); =20 @@ -40,7 +41,6 @@ const char *features_binaries[] =3D { NULL }; #endif =20 /* ZEND thread safe per request globals definition */ -int le_libvirt_nodedev; int le_libvirt_nwfilter; =20 ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_connect, 0, 0, 0) @@ -481,11 +481,7 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE_LIBVIRT_STORAGE PHP_FE_LIBVIRT_NETWORK PHP_FE_LIBVIRT_NODE - /* Nodedev functions */ - PHP_FE(libvirt_nodedev_get, arginfo_libvirt_conn) - PHP_FE(libvirt_nodedev_capabilities, arginfo_libvirt_conn) - PHP_FE(libvirt_nodedev_get_xml_desc, arginfo_libvirt_conn_xpat= h) - PHP_FE(libvirt_nodedev_get_information, arginfo_libvirt_conn) + PHP_FE_LIBVIRT_NODEDEV /* NWFilter functions */ PHP_FE(libvirt_nwfilter_define_xml, arginfo_libvirt_conn_xml) PHP_FE(libvirt_nwfilter_undefine, arginfo_libvirt_conn) @@ -497,7 +493,6 @@ 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_nodedevs, arginfo_libvirt_conn_optc= ap) PHP_FE(libvirt_list_all_nwfilters, arginfo_libvirt_conn) PHP_FE(libvirt_list_nwfilters, arginfo_libvirt_conn) /* Version information and common function */ @@ -1179,33 +1174,6 @@ int is_local_connection(virConnectPtr conn) #endif } =20 -/* Destructor for nodedev resource */ -static void php_libvirt_nodedev_dtor(virt_resource *rsrc TSRMLS_DC) -{ - php_libvirt_nodedev *nodedev =3D (php_libvirt_nodedev *)rsrc->ptr; - int rv =3D 0; - - if (nodedev !=3D NULL) { - if (nodedev->device !=3D NULL) { - if (!check_resource_allocation(nodedev->conn->conn, INT_RESOUR= CE_NODEDEV, nodedev->device TSRMLS_CC)) { - nodedev->device =3D NULL; - efree(nodedev); - return; - } - rv =3D virNodeDeviceFree(nodedev->device); - if (rv !=3D 0) { - DPRINTF("%s: virNodeDeviceFree(%p) returned %d (%s)\n", __= FUNCTION__, nodedev->device, rv, LIBVIRT_G(last_error)); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStorageVol= Free failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); - } else { - DPRINTF("%s: virNodeDeviceFree(%p) completed successfully\= n", __FUNCTION__, nodedev->device); - resource_change_counter(INT_RESOURCE_NODEDEV, nodedev->con= n->conn, nodedev->device, 0 TSRMLS_CC); - } - nodedev->device =3D NULL; - } - efree(nodedev); - } -} - /* Destructor for nwfilter resource */ static void php_libvirt_nwfilter_dtor(virt_resource *rsrc TSRMLS_DC) { @@ -1559,19 +1527,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) } =20 /* Macros for obtaining resources from arguments */ -#define GET_NODEDEV_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(nodedev, php_libvirt_nodedev*, &znodedev, PHP_= LIBVIRT_NODEDEV_RES_NAME, le_libvirt_nodedev);\ - if ((nodedev =3D=3D NULL) || (nodedev->device =3D=3D NULL)) = \ - RETURN_FALSE; = \ - } while (0) - #define GET_NWFILTER_FROM_ARGS(args, ...) = \ do { = \ reset_error(TSRMLS_C); = \ @@ -2560,45 +2515,6 @@ void parse_array(zval *arr, tVMDisk *disk, tVMNetwor= k *network) } =20 /* Listing functions */ -/* - * Function name: libvirt_list_nodedevs - * Since version: 0.4.1(-1) - * Description: Function is used to list node devices on the connection - * Arguments: @res [resource]: libvirt connection resource - * @cap [string]: optional capability string - * Returns: libvirt nodedev names array for the connection - */ -PHP_FUNCTION(libvirt_list_nodedevs) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - char *cap =3D NULL; - char **names; - int i; - strsize_t cap_len; - - GET_CONNECTION_FROM_ARGS("r|s", &zconn, &cap, &cap_len); - - if ((expectedcount =3D virNodeNumOfDevices(conn->conn, cap, 0)) < 0) - RETURN_FALSE; - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virNodeListDevices(conn->conn, cap, names, expectedcount, 0); - 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); -} - =20 /* * Function name: libvirt_list_all_nwfilters @@ -2684,269 +2600,6 @@ PHP_FUNCTION(libvirt_list_nwfilters) if (!done) RETURN_FALSE; } -/* Nodedev functions */ - -/* - * Function name: libvirt_nodedev_get - * Since version: 0.4.1(-1) - * Description: Function is used to get the node device by it's name - * Arguments: @res [resource]: libvirt connection resource - * @name [string]: name of the nodedev to get resource - * Returns: libvirt nodedev resource - */ -PHP_FUNCTION(libvirt_nodedev_get) -{ - php_libvirt_connection *conn =3D NULL; - php_libvirt_nodedev *res_dev =3D NULL; - virNodeDevice *dev; - zval *zconn; - char *name; - strsize_t name_len; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); - - if ((dev =3D virNodeDeviceLookupByName(conn->conn, name)) =3D=3D NULL)= { - set_error("Cannot get find requested node device" TSRMLS_CC); - RETURN_FALSE; - } - - res_dev =3D (php_libvirt_nodedev *)emalloc(sizeof(php_libvirt_nodedev)= ); - res_dev->device =3D dev; - res_dev->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_dev->device); - resource_change_counter(INT_RESOURCE_NODEDEV, conn->conn, res_dev->dev= ice, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_dev, le_libvirt_nodedev); -} - -/* - * Function name: libvirt_nodedev_capabilities - * Since version: 0.4.1(-1) - * Description: Function is used to list node devices by capabilities - * Arguments: @res [resource]: libvirt nodedev resource - * Returns: nodedev capabilities array - */ -PHP_FUNCTION(libvirt_nodedev_capabilities) -{ - php_libvirt_nodedev *nodedev =3D NULL; - zval *znodedev; - int count =3D -1; - int expectedcount =3D -1; - char **names; - int i; - - GET_NODEDEV_FROM_ARGS("r", &znodedev); - - if ((expectedcount =3D virNodeDeviceNumOfCaps(nodedev->device)) < 0) - RETURN_FALSE; - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virNodeDeviceListCaps(nodedev->device, names, expectedcount); - if ((count !=3D expectedcount) || (count < 0)) - 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_nodedev_get_xml_desc - * Since version: 0.4.1(-1), changed 0.4.2 - * Description: Function is used to get the node device's XML descript= ion - * Arguments: @res [resource]: libvirt nodedev resource - * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL - * Returns: nodedev XML description string or result of xPath expr= ession - */ -PHP_FUNCTION(libvirt_nodedev_get_xml_desc) -{ - php_libvirt_nodedev *nodedev =3D NULL; - zval *znodedev; - char *tmp =3D NULL; - char *xml =3D NULL; - char *xpath =3D NULL; - strsize_t xpath_len; - int retval =3D -1; - - GET_NODEDEV_FROM_ARGS("r|s", &znodedev, &xpath, &xpath_len); - if (xpath_len < 1) - xpath =3D NULL; - - xml =3D virNodeDeviceGetXMLDesc(nodedev->device, 0); - if (!xml) { - set_error("Cannot get the device XML information" 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(xml); - free(tmp); -} - -/* - * Function name: libvirt_nodedev_get_information - * Since version: 0.4.1(-1) - * Description: Function is used to get the node device's information - * Arguments: @res [resource]: libvirt nodedev resource - * Returns: nodedev information array - */ -PHP_FUNCTION(libvirt_nodedev_get_information) -{ - php_libvirt_nodedev *nodedev =3D NULL; - zval *znodedev; - int retval =3D -1; - char *xml =3D NULL; - char *tmp =3D NULL; - char *cap =3D NULL; - - GET_NODEDEV_FROM_ARGS("r", &znodedev); - - xml =3D virNodeDeviceGetXMLDesc(nodedev->device, 0); - if (!xml) { - set_error("Cannot get the device XML information" TSRMLS_CC); - RETURN_FALSE; - } - - array_init(return_value); - - /* Get name */ - tmp =3D get_string_from_xpath(xml, "//device/name", NULL, &retval); - if (tmp =3D=3D NULL) { - set_error("Invalid XPath node for device name" TSRMLS_CC); - goto error; - } - - if (retval < 0) { - set_error("Cannot get XPath expression result for device name" TSR= MLS_CC); - goto error; - } - - VIRT_ADD_ASSOC_STRING(return_value, "name", tmp); - - /* Get parent name */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/parent", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "parent", tmp); - - /* Get capability */ - cap =3D get_string_from_xpath(xml, "//device/capability/@type", NULL, = &retval); - if ((cap !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "capability", cap); - - /* System capability is having hardware and firmware sub-blocks */ - if (strcmp(cap, "system") =3D=3D 0) { - /* Get hardware vendor */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/v= endor", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "hardware_vendor", tmp); - - /* Get hardware version */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/v= ersion", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "hardware_version", tmp); - - /* Get hardware serial */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/s= erial", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "hardware_serial", tmp); - - /* Get hardware UUID */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/u= uid", NULL, &retval); - if (tmp !=3D NULL) - VIRT_ADD_ASSOC_STRING(return_value, "hardware_uuid", tmp); - - /* Get firmware vendor */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/firmware/v= endor", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "firmware_vendor", tmp); - - /* Get firmware version */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/firmware/v= ersion", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "firmware_version", tmp); - - /* Get firmware release date */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/firmware/r= elease_date", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "firmware_release_date", t= mp); - } - - /* Get product_id */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/product/@id", = NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "product_id", tmp); - - /* Get product_name */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/product", NULL= , &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "product_name", tmp); - - /* Get vendor_id */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/vendor/@id", N= ULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "vendor_id", tmp); - - /* Get vendor_name */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/vendor", NULL,= &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "vendor_name", tmp); - - /* Get driver name */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/driver/name", NULL, &retv= al); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "driver_name", tmp); - - /* Get driver name */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/interface", NU= LL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "interface_name", tmp); - - /* Get driver name */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/address", NULL= , &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "address", tmp); - - /* Get driver name */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/capability/@ty= pe", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "capabilities", tmp); - - free(cap); - free(tmp); - free(xml); - return; - - error: - free(cap); - free(tmp); - free(xml); - RETURN_FALSE; -} =20 /* NWFilter functions */ =20 diff --git a/src/libvirt-php.h b/src/libvirt-php.h index ca10d23..676b29e 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -128,7 +128,6 @@ typedef uint64_t arch_uint; #define PHP_LIBVIRT_WORLD_EXTNAME "libvirt" =20 /* Internal resource identifier objects */ -#define INT_RESOURCE_NODEDEV 0x08 #define INT_RESOURCE_NWFILTER 0x60 =20 typedef struct tTokenizer { @@ -162,11 +161,6 @@ typedef struct tVMNetwork { typedef struct _php_libvirt_connection php_libvirt_connection; =20 /* Libvirt-php types */ -typedef struct _php_libvirt_nodedev { - virNodeDevicePtr device; - php_libvirt_connection* conn; -} php_libvirt_nodedev; - typedef struct _php_libvirt_nwfilter { virNWFilterPtr nwfilter; php_libvirt_connection* conn; @@ -224,7 +218,6 @@ const char *get_feature_binary(const char *name); long get_next_free_numeric_value(virDomainPtr domain, char *xpath); int get_subnet_bits(char *ip); =20 -#define PHP_LIBVIRT_NODEDEV_RES_NAME "Libvirt node device" #define PHP_LIBVIRT_NWFILTER_RES_NAME "Libvirt nwfilter" =20 PHP_MINIT_FUNCTION(libvirt); @@ -235,11 +228,6 @@ PHP_MINFO_FUNCTION(libvirt); =20 /* Common functions */ PHP_FUNCTION(libvirt_get_last_error); -/* Nodedev functions */ -PHP_FUNCTION(libvirt_nodedev_get); -PHP_FUNCTION(libvirt_nodedev_capabilities); -PHP_FUNCTION(libvirt_nodedev_get_xml_desc); -PHP_FUNCTION(libvirt_nodedev_get_information); /* NWFilter functions */ PHP_FUNCTION(libvirt_nwfilter_define_xml); PHP_FUNCTION(libvirt_nwfilter_undefine); @@ -251,7 +239,6 @@ PHP_FUNCTION(libvirt_nwfilter_lookup_by_name); PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid_string); PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid); /* Listing functions */ -PHP_FUNCTION(libvirt_list_nodedevs); PHP_FUNCTION(libvirt_list_all_nwfilters); PHP_FUNCTION(libvirt_list_nwfilters); /* Common functions */ --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list