[libvirt] [libvirt-php PATCH 0/2] Add binding for virDomainInterfaceAddresses

Hello, The following two patches add a new bingding for virDomainInterfaceAddresses. While working on it I have found that the PHP7 version of the VIRT_ARRAY_INIT macro was causing segfaults which I have fixed in the first patch wheread the actual implementation of the binding is in the second one. Details are included in the commit messages of each patch. Regards, Dawid Dawid Zamirski (2): Fix PHP7 VIRT_ARRAY_INIT macro implementation. Add binding for virDomainInterfaceAddresses. src/libvirt-domain.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-domain.h | 2 ++ src/libvirt-php.c | 9 ++++++++ src/util.h | 7 +++--- 4 files changed, 70 insertions(+), 3 deletions(-) -- 2.21.0

This is a PHP 7 compatibilty macro which was segfaulting due to the temporary variable being defined in the do..while scoped block (to swallow semicolon for macros), e.g: zval *arr; VIRT_ARRAY_INIT(arr); VIRT_ADD_ASSOC_STRING(arr, "foo", "bar"); // <= segfault here The VIRT_ARRAY_INIT above was expanding to: do { zval z_arr; // <= local scope definition arr = &z_arr; array_init(arr); } while (0) After this patch, the macro expands to: zval z_arr; // now defined in the scope of the macro caller do { arr = &z_arr; array_init(arr); } while (0) which solved the issue. --- src/util.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/util.h b/src/util.h index 3af77d4..c96fd0c 100644 --- a/src/util.h +++ b/src/util.h @@ -151,10 +151,11 @@ } \ } while(0) -# define VIRT_ARRAY_INIT(_name) do { \ +# define VIRT_ARRAY_INIT(_name) \ zval z##_name; \ - _name = &z##_name; \ - array_init(_name); \ + do { \ + _name = &z##_name; \ + array_init(_name); \ } while(0) # else /* PHP_MAJOR_VERSION < 7 */ -- 2.21.0

A straight-forward 1:1 mapping to the C API. Returns array in the following format: array ( 0 => array ( 'name' => 'vnet0', 'hwaddr' => '52:54:00:3a:cd:94', 'naddrs' => 1, 'addrs' => array ( 'addr' => '192.168.254.224', 'prefix' => 24, 'type' => 0, ), ), ) --- src/libvirt-domain.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-domain.h | 2 ++ src/libvirt-php.c | 9 ++++++++ 3 files changed, 66 insertions(+) diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c index 8b8bb45..877d311 100644 --- a/src/libvirt-domain.c +++ b/src/libvirt-domain.c @@ -1948,6 +1948,61 @@ PHP_FUNCTION(libvirt_domain_block_job_set_speed) RETURN_TRUE; } +/* + * Function name: libvirt_domain_interface_addresses + * Since version: 0.5.5 + * Description: Function is used to get network interface addresses for the domain + * Arguments: @domain [resource]: libvirt domain resource, e.g. from libvirt_domain_lookup_by_*() + * @source [int]: one of the VIR_DOMAIN_ADDRESSES_SRC_* flags. + * Returns: interface array of a domain holding information about addresses resembling the virDomainInterface structure, false on error + */ +PHP_FUNCTION(libvirt_domain_interface_addresses) +{ + php_libvirt_domain *domain = NULL; + zval *zdomain; + zend_long source = 0; + + virDomainInterfacePtr *ifaces = NULL; + int count = 0; + size_t i, j; + + GET_DOMAIN_FROM_ARGS("rl", &zdomain, &source); + + if ((count = virDomainInterfaceAddresses(domain->domain, &ifaces, source, 0)) < 0) { + RETURN_FALSE + goto cleanup; + } + + array_init(return_value); + + for (i = 0; i < count; i++) { + zval *iface; + VIRT_ARRAY_INIT(iface); + VIRT_ADD_ASSOC_STRING(iface, "name", ifaces[i]->name); + VIRT_ADD_ASSOC_STRING(iface, "hwaddr", ifaces[i]->hwaddr); + add_assoc_long(iface, "naddrs", ifaces[i]->naddrs); + + for (j = 0; j < ifaces[i]->naddrs; j++) { + zval *ifaddr; + VIRT_ARRAY_INIT(ifaddr); + VIRT_ADD_ASSOC_STRING(ifaddr, "addr", ifaces[i]->addrs[j].addr); + add_assoc_long(ifaddr, "prefix", ifaces[i]->addrs[j].prefix); + add_assoc_long(ifaddr, "type", ifaces[i]->addrs[j].type); + + add_assoc_zval(iface, "addrs", ifaddr); + } + + add_index_zval(return_value, i, iface); + } + + cleanup: + if (ifaces && count > 0) { + for (i = 0; i < count; i++) + virDomainInterfaceFree(ifaces[i]); + } + VIR_FREE(ifaces); +} + /* * Function name: libvirt_domain_interface_stats * Since version: 0.4.1(-1) diff --git a/src/libvirt-domain.h b/src/libvirt-domain.h index dc0ab46..f15237f 100644 --- a/src/libvirt-domain.h +++ b/src/libvirt-domain.h @@ -89,6 +89,7 @@ PHP_FE(libvirt_domain_block_job_info, arginfo_libvirt_domain_block_job_info) \ PHP_FE(libvirt_domain_block_job_abort, arginfo_libvirt_domain_block_job_abort) \ PHP_FE(libvirt_domain_block_job_set_speed, arginfo_libvirt_domain_block_job_set_speed) \ + PHP_FE(libvirt_domain_interface_addresses, arginfo_libvirt_domain_interface_addresses) \ 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_migrate) \ @@ -179,6 +180,7 @@ 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_addresses); PHP_FUNCTION(libvirt_domain_interface_stats); PHP_FUNCTION(libvirt_domain_get_connect); PHP_FUNCTION(libvirt_domain_migrate); diff --git a/src/libvirt-php.c b/src/libvirt-php.c index cf8fd7f..04b7c07 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -248,6 +248,11 @@ ZEND_ARG_INFO(0, bandwidth) ZEND_ARG_INFO(0, flags) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_domain_interface_addresses, 0, 0, 2) +ZEND_ARG_INFO(0, domain) +ZEND_ARG_INFO(0, source) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_domain_block_job_info, 0, 0, 2) ZEND_ARG_INFO(0, dom) ZEND_ARG_INFO(0, disk) @@ -1476,6 +1481,10 @@ PHP_MINIT_FUNCTION(libvirt) REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEM_LIVE", VIR_DOMAIN_MEM_LIVE, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEM_MAXIMUM", VIR_DOMAIN_MEM_MAXIMUM, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE", VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT", VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP", VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE, CONST_CS | CONST_PERSISTENT); + /* Connect flags */ REGISTER_LONG_CONSTANT("VIR_CONNECT_FLAG_SOUNDHW_GET_NAMES", CONNECT_FLAG_SOUNDHW_GET_NAMES, CONST_CS | CONST_PERSISTENT); -- 2.21.0

On 7/8/19 11:32 PM, Dawid Zamirski wrote:
Hello,
The following two patches add a new bingding for virDomainInterfaceAddresses. While working on it I have found that the PHP7 version of the VIRT_ARRAY_INIT macro was causing segfaults which I have fixed in the first patch wheread the actual implementation of the binding is in the second one. Details are included in the commit messages of each patch.
Regards, Dawid
Dawid Zamirski (2): Fix PHP7 VIRT_ARRAY_INIT macro implementation. Add binding for virDomainInterfaceAddresses.
src/libvirt-domain.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-domain.h | 2 ++ src/libvirt-php.c | 9 ++++++++ src/util.h | 7 +++--- 4 files changed, 70 insertions(+), 3 deletions(-)
Patches look good, but I cannot push them because they are missing your signoff. I can fix it, but I need your permisson to add the following at the end of both messages: Signed-off-by: Dawid Zamirski <dzamirski@datto.com> This confirms also that you've agreed to Developer Certificate of Origin 1.1: https://libvirt.org/hacking.html Michal

Hi Michal, Yes, please feel free to add: Signed-off-by: Dawid Zamirski <dzamirski@datto.com> to both messages. I'll make sure it is included in my future submissions from now on. Thanks, Dawid On Fri, 2019-07-12 at 17:12 +0200, Michal Privoznik wrote:
On 7/8/19 11:32 PM, Dawid Zamirski wrote:
Hello,
The following two patches add a new bingding for virDomainInterfaceAddresses. While working on it I have found that the PHP7 version of the VIRT_ARRAY_INIT macro was causing segfaults which I have fixed in the first patch wheread the actual implementation of the binding is in the second one. Details are included in the commit messages of each patch.
Regards, Dawid
Dawid Zamirski (2): Fix PHP7 VIRT_ARRAY_INIT macro implementation. Add binding for virDomainInterfaceAddresses.
src/libvirt-domain.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-domain.h | 2 ++ src/libvirt-php.c | 9 ++++++++ src/util.h | 7 +++--- 4 files changed, 70 insertions(+), 3 deletions(-)
Patches look good, but I cannot push them because they are missing your signoff. I can fix it, but I need your permisson to add the following at the end of both messages:
Signed-off-by: Dawid Zamirski <dzamirski@datto.com>
This confirms also that you've agreed to Developer Certificate of Origin 1.1:
https://libvirt.org/hacking.html
Michal
participants (2)
-
Dawid Zamirski
-
Michal Privoznik