[libvirt] [libvirt-php PATCH 0/7] add bindings for NWFilter APIs

Hello, This series adds support for libvirt's virNWFilter* APIs. Since it introduces new resource type, I took the opportunity to cleanup the driver code a little: * in patches 1-3: added macros to take care of the differences in how PHP5 and PHP7 handle resource types. * in patch 4: libvirt_doman_get_connect was segfaulting when called multiple times because it was not bumping reference count on the resource it was returning to the calling code. * in patch 5: added a macro to take care of the differences in how PHP5 and PHP7 initialize arrays. * patches 6 and 7: implement the missing binding to NWFilter APIs. Dawid Zamirski (7): move macros to header file. add wrappers for PHP resource handling. update code to use resource handling macros fix libvirt_doman_get_connect implementation. add and use VIRT_ARRAY_INIT macro add nwfilter resource type implement NWFilter API bindings. src/libvirt-php.c | 923 +++++++++++++++++++++++++++++++----------------------- src/libvirt-php.h | 150 ++++++++- 2 files changed, 672 insertions(+), 401 deletions(-) -- 2.13.0

So that PHP version handling wrappers are all in one place --- src/libvirt-php.c | 88 ------------------------------------------------------- src/libvirt-php.h | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 88 deletions(-) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index c2ab0da..ece98d6 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -45,94 +45,6 @@ const char *features[] = { NULL }; const char *features_binaries[] = { NULL }; #endif -#if PHP_MAJOR_VERSION >= 7 -typedef size_t strsize_t; - - -#define VIRT_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \ - if ((_state = (_type)zend_fetch_resource(Z_RES_P(*_zval), _name, _le)) == NULL) { \ - RETURN_FALSE; \ - } - -#define VIRT_RETVAL_STRING(_str) \ - RETVAL_STRING(_str) -#define VIRT_RETVAL_STRINGL(_str, _len) \ - RETVAL_STRINGL(_str, _len) -#define VIRT_RETURN_STRING(_str) \ - RETURN_STRING(_str) -#define VIRT_RETURN_STRINGL(_str, _len) \ - RETURN_STRINGL(_str, _len) -#define VIRT_ZVAL_STRINGL(_zv, _str, _len) \ - ZVAL_STRINGL(_zv, _str, _len) -#define VIRT_ADD_INDEX_STRING(_arg, _idx, _str) \ - add_index_string(_arg, _idx, _str) -#define VIRT_ADD_NEXT_INDEX_STRING(_arg, _str) \ - add_next_index_string(_arg, _str) -#define VIRT_ADD_ASSOC_STRING(_arg, _key, _str) \ - add_assoc_string(_arg, _key, _str) -#define VIRT_ADD_ASSOC_STRING_EX(_arg, _key, _key_len, _value) \ - add_assoc_string_ex(_arg, _key, _key_len, _value) - -#define VIRT_FOREACH(_ht, _pos, _zv) \ - for (zend_hash_internal_pointer_reset_ex(_ht, &_pos); \ - (_zv = zend_hash_get_current_data_ex(_ht, &_pos)) != NULL; \ - zend_hash_move_forward_ex(_ht, &_pos)) \ - -#define VIRT_FOREACH_END(_dummy) - -#define VIRT_HASH_CURRENT_KEY_INFO(_ht, _pos, _idx, _info) \ - do { \ - zend_string *tmp_key_info; \ - _info.type = zend_hash_get_current_key_ex(_ht, &tmp_key_info, &_idx, &_pos); \ - _info.name = ZSTR_VAL(tmp_key_info); \ - _info.length = ZSTR_LEN(tmp_key_info); \ - } while(0) - -#else /* PHP_MAJOR_VERSION < 7 */ -typedef int strsize_t; -typedef long zend_long; -typedef unsigned long zend_ulong; - -#define VIRT_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \ - ZEND_FETCH_RESOURCE(_state, _type, _zval, -1, _name, _le); - -#define VIRT_RETVAL_STRING(_str) \ - RETVAL_STRING(_str, 1) -#define VIRT_RETVAL_STRINGL(_str, _len) \ - RETVAL_STRINGL(_str, _len, 1) -#define VIRT_RETURN_STRING(_str) \ - RETURN_STRING(_str, 1) -#define VIRT_RETURN_STRINGL(_str, _len) \ - RETURN_STRINGL(_str, _len, 1) -#define VIRT_ZVAL_STRINGL(_zv, _str, _len) \ - ZVAL_STRINGL(_zv, _str, _len, 1) -#define VIRT_ADD_INDEX_STRING(_arg, _idx, _str) \ - add_index_string(_arg, _idx, _str, 1) -#define VIRT_ADD_NEXT_INDEX_STRING(_arg, _str) \ - add_next_index_string(_arg, _str, 1) -#define VIRT_ADD_ASSOC_STRING(_arg, _key, _str) \ - add_assoc_string(_arg, _key, _str, 1) -#define VIRT_ADD_ASSOC_STRING_EX(_arg, _key, _key_len, _value) \ - add_assoc_string_ex(_arg, _key, _key_len, _value, 1) - -#define VIRT_FOREACH(_ht, _pos, _zv) \ - { \ - zval **pzv = &_zv; \ - for (zend_hash_internal_pointer_reset_ex(_ht, &_pos); \ - zend_hash_get_current_data_ex(_ht, (void **) &pzv, &_pos) == SUCCESS; \ - zend_hash_move_forward_ex(_ht, &_pos)) { \ - _zv = *pzv; - -#define VIRT_FOREACH_END(_dummy) \ - }} - -#define VIRT_HASH_CURRENT_KEY_INFO(_ht, _pos, _idx, _info) \ - do { \ - _info.type = zend_hash_get_current_key_ex(_ht, &_info.name, &_info.length, &_idx, 0, &_pos); \ - } while(0) - -#endif /* PHP_MAJOR_VERSION < 7 */ - /* ZEND thread safe per request globals definition */ int le_libvirt_connection; int le_libvirt_domain; diff --git a/src/libvirt-php.h b/src/libvirt-php.h index f9dec09..d25fa94 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -121,10 +121,89 @@ typedef uint64_t arch_uint; #if PHP_MAJOR_VERSION >= 7 typedef size_t strsize_t; + +#define VIRT_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \ + if ((_state = (_type)zend_fetch_resource(Z_RES_P(*_zval), _name, _le)) == NULL) { \ + RETURN_FALSE; \ + } + +#define VIRT_RETVAL_STRING(_str) \ + RETVAL_STRING(_str) +#define VIRT_RETVAL_STRINGL(_str, _len) \ + RETVAL_STRINGL(_str, _len) +#define VIRT_RETURN_STRING(_str) \ + RETURN_STRING(_str) +#define VIRT_RETURN_STRINGL(_str, _len) \ + RETURN_STRINGL(_str, _len) +#define VIRT_ZVAL_STRINGL(_zv, _str, _len) \ + ZVAL_STRINGL(_zv, _str, _len) +#define VIRT_ADD_INDEX_STRING(_arg, _idx, _str) \ + add_index_string(_arg, _idx, _str) +#define VIRT_ADD_NEXT_INDEX_STRING(_arg, _str) \ + add_next_index_string(_arg, _str) +#define VIRT_ADD_ASSOC_STRING(_arg, _key, _str) \ + add_assoc_string(_arg, _key, _str) +#define VIRT_ADD_ASSOC_STRING_EX(_arg, _key, _key_len, _value) \ + add_assoc_string_ex(_arg, _key, _key_len, _value) + +#define VIRT_FOREACH(_ht, _pos, _zv) \ + for (zend_hash_internal_pointer_reset_ex(_ht, &_pos); \ + (_zv = zend_hash_get_current_data_ex(_ht, &_pos)) != NULL; \ + zend_hash_move_forward_ex(_ht, &_pos)) \ + +#define VIRT_FOREACH_END(_dummy) + +#define VIRT_HASH_CURRENT_KEY_INFO(_ht, _pos, _idx, _info) \ + do { \ + zend_string *tmp_key_info; \ + _info.type = zend_hash_get_current_key_ex(_ht, &tmp_key_info, &_idx, &_pos); \ + _info.name = ZSTR_VAL(tmp_key_info); \ + _info.length = ZSTR_LEN(tmp_key_info); \ + } while(0) + #else /* PHP_MAJOR_VERSION < 7 */ typedef int strsize_t; typedef long zend_long; typedef unsigned long zend_ulong; + +#define VIRT_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \ + ZEND_FETCH_RESOURCE(_state, _type, _zval, -1, _name, _le); + +#define VIRT_RETVAL_STRING(_str) \ + RETVAL_STRING(_str, 1) +#define VIRT_RETVAL_STRINGL(_str, _len) \ + RETVAL_STRINGL(_str, _len, 1) +#define VIRT_RETURN_STRING(_str) \ + RETURN_STRING(_str, 1) +#define VIRT_RETURN_STRINGL(_str, _len) \ + RETURN_STRINGL(_str, _len, 1) +#define VIRT_ZVAL_STRINGL(_zv, _str, _len) \ + ZVAL_STRINGL(_zv, _str, _len, 1) +#define VIRT_ADD_INDEX_STRING(_arg, _idx, _str) \ + add_index_string(_arg, _idx, _str, 1) +#define VIRT_ADD_NEXT_INDEX_STRING(_arg, _str) \ + add_next_index_string(_arg, _str, 1) +#define VIRT_ADD_ASSOC_STRING(_arg, _key, _str) \ + add_assoc_string(_arg, _key, _str, 1) +#define VIRT_ADD_ASSOC_STRING_EX(_arg, _key, _key_len, _value) \ + add_assoc_string_ex(_arg, _key, _key_len, _value, 1) + +#define VIRT_FOREACH(_ht, _pos, _zv) \ + { \ + zval **pzv = &_zv; \ + for (zend_hash_internal_pointer_reset_ex(_ht, &_pos); \ + zend_hash_get_current_data_ex(_ht, (void **) &pzv, &_pos) == SUCCESS; \ + zend_hash_move_forward_ex(_ht, &_pos)) { \ + _zv = *pzv; + +#define VIRT_FOREACH_END(_dummy) \ + }} + +#define VIRT_HASH_CURRENT_KEY_INFO(_ht, _pos, _idx, _info) \ + do { \ + _info.type = zend_hash_get_current_key_ex(_ht, &_info.name, &_info.length, &_idx, 0, &_pos); \ + } while(0) + #endif /* PHP_MAJOR_VERSION < 7 */ typedef struct tTokenizer { -- 2.13.0

There were quite a few places in code that were using preprocessor directives to handle differences in dealing with resources between PHP versions which can be dealt with by using typdefs and macros. --- src/libvirt-php.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/libvirt-php.h b/src/libvirt-php.h index d25fa94..0422661 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -121,6 +121,19 @@ typedef uint64_t arch_uint; #if PHP_MAJOR_VERSION >= 7 typedef size_t strsize_t; +typedef zend_resource virt_resource; + +#define VIRT_RETURN_RESOURCE(_resource) \ + RETVAL_RES(_resource) + +#define VIRT_REGISTER_RESOURCE(_resource, _le_resource) \ + VIRT_RETURN_RESOURCE(zend_register_resource(_resource, _le_resource)) + +#define VIRT_REGISTER_LIST_RESOURCE(_name) do { \ + zval zret; \ + ZVAL_RES(&zret, zend_register_resource(res_##_name, le_libvirt_##_name)); \ + add_next_index_zval(return_value, &zret); \ + } while(0) #define VIRT_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \ if ((_state = (_type)zend_fetch_resource(Z_RES_P(*_zval), _name, _le)) == NULL) { \ @@ -165,6 +178,20 @@ typedef size_t strsize_t; typedef int strsize_t; typedef long zend_long; typedef unsigned long zend_ulong; +typedef zend_rsrc_list_entry virt_resource; + +#define VIRT_RETURN_RESOURCE(_resource) \ + RETVAL_RESOURCE((long) _resource) + +#define VIRT_REGISTER_RESOURCE(_resource, _le_resource) \ + ZEND_REGISTER_RESOURCE(return_value, _resource, _le_resource) + +#define VIRT_REGISTER_LIST_RESOURCE(_name) do { \ + zval *zret; \ + ALLOC_INIT_ZVAL(zret); \ + ZEND_REGISTER_RESOURCE(zret, res_##_name, le_libvirt_##_name); \ + add_next_index_zval(return_value, zret); \ + } while(0) #define VIRT_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \ ZEND_FETCH_RESOURCE(_state, _type, _zval, -1, _name, _le); -- 2.13.0

--- src/libvirt-php.c | 270 ++++++++++++------------------------------------------ 1 file changed, 59 insertions(+), 211 deletions(-) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index ece98d6..73466f1 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -1368,13 +1368,7 @@ int is_local_connection(virConnectPtr conn) } /* Destructor for connection resource */ -static void php_libvirt_connection_dtor( -#if PHP_MAJOR_VERSION >= 7 - zend_resource *rsrc -#else - zend_rsrc_list_entry *rsrc -#endif - TSRMLS_DC) +static void php_libvirt_connection_dtor(virt_resource *rsrc TSRMLS_DC) { php_libvirt_connection *conn = (php_libvirt_connection *)rsrc->ptr; int rv = 0; @@ -1398,13 +1392,7 @@ static void php_libvirt_connection_dtor( } /* Destructor for domain resource */ -static void php_libvirt_domain_dtor( -#if PHP_MAJOR_VERSION >= 7 - zend_resource *rsrc -#else - zend_rsrc_list_entry *rsrc -#endif - TSRMLS_DC) +static void php_libvirt_domain_dtor(virt_resource *rsrc TSRMLS_DC) { php_libvirt_domain *domain = (php_libvirt_domain *)rsrc->ptr; int rv = 0; @@ -1432,13 +1420,7 @@ static void php_libvirt_domain_dtor( } /* Destructor for stream resource */ -static void php_libvirt_stream_dtor( -#if PHP_MAJOR_VERSION >= 7 - zend_resource *rsrc -#else - zend_rsrc_list_entry *rsrc -#endif - TSRMLS_DC) +static void php_libvirt_stream_dtor(virt_resource *rsrc TSRMLS_DC) { php_libvirt_stream *stream = (php_libvirt_stream *)rsrc->ptr; int rv = 0; @@ -1465,13 +1447,7 @@ static void php_libvirt_stream_dtor( } /* Destructor for storagepool resource */ -static void php_libvirt_storagepool_dtor( -#if PHP_MAJOR_VERSION >= 7 - zend_resource *rsrc -#else - zend_rsrc_list_entry *rsrc -#endif - TSRMLS_DC) +static void php_libvirt_storagepool_dtor(virt_resource *rsrc TSRMLS_DC) { php_libvirt_storagepool *pool = (php_libvirt_storagepool *)rsrc->ptr; int rv = 0; @@ -1498,13 +1474,7 @@ static void php_libvirt_storagepool_dtor( } /* Destructor for volume resource */ -static void php_libvirt_volume_dtor( -#if PHP_MAJOR_VERSION >= 7 - zend_resource *rsrc -#else - zend_rsrc_list_entry *rsrc -#endif - TSRMLS_DC) +static void php_libvirt_volume_dtor(virt_resource *rsrc TSRMLS_DC) { php_libvirt_volume *volume = (php_libvirt_volume *)rsrc->ptr; int rv = 0; @@ -1531,13 +1501,7 @@ static void php_libvirt_volume_dtor( } /* Destructor for network resource */ -static void php_libvirt_network_dtor( -#if PHP_MAJOR_VERSION >= 7 - zend_resource *rsrc -#else - zend_rsrc_list_entry *rsrc -#endif - TSRMLS_DC) +static void php_libvirt_network_dtor(virt_resource *rsrc TSRMLS_DC) { php_libvirt_network *network = (php_libvirt_network *)rsrc->ptr; int rv = 0; @@ -1564,13 +1528,7 @@ static void php_libvirt_network_dtor( } /* Destructor for nodedev resource */ -static void php_libvirt_nodedev_dtor( -#if PHP_MAJOR_VERSION >= 7 - zend_resource *rsrc -#else - zend_rsrc_list_entry *rsrc -#endif - TSRMLS_DC) +static void php_libvirt_nodedev_dtor(virt_resource *rsrc TSRMLS_DC) { php_libvirt_nodedev *nodedev = (php_libvirt_nodedev *)rsrc->ptr; int rv = 0; @@ -1597,13 +1555,7 @@ static void php_libvirt_nodedev_dtor( } /* Destructor for snapshot resource */ -static void php_libvirt_snapshot_dtor( -#if PHP_MAJOR_VERSION >= 7 - zend_resource *rsrc -#else - zend_rsrc_list_entry *rsrc -#endif - TSRMLS_DC) +static void php_libvirt_snapshot_dtor(virt_resource *rsrc TSRMLS_DC) { php_libvirt_snapshot *snapshot = (php_libvirt_snapshot *)rsrc->ptr; int rv = 0; @@ -4198,11 +4150,8 @@ PHP_FUNCTION(libvirt_domain_lookup_by_name) DPRINTF("%s: domain name = '%s', returning %p\n", PHPFUNC, name, res_domain->domain); resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->domain, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_domain, le_libvirt_domain)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_domain, le_libvirt_domain); -#endif + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); } /* @@ -4236,11 +4185,8 @@ PHP_FUNCTION(libvirt_domain_lookup_by_uuid) DPRINTF("%s: domain UUID = '%s', returning %p\n", PHPFUNC, uuid, res_domain->domain); resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->domain, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_domain, le_libvirt_domain)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_domain, le_libvirt_domain); -#endif + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); } /* @@ -4304,11 +4250,8 @@ PHP_FUNCTION(libvirt_domain_lookup_by_uuid_string) DPRINTF("%s: domain UUID string = '%s', returning %p\n", PHPFUNC, uuid, res_domain->domain); resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->domain, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_domain, le_libvirt_domain)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_domain, le_libvirt_domain); -#endif + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); } /* @@ -4342,11 +4285,8 @@ PHP_FUNCTION(libvirt_stream_create) res_stream->conn = conn; resource_change_counter(INT_RESOURCE_STREAM, conn->conn, res_stream->stream, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_stream, le_libvirt_stream)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_stream, le_libvirt_stream); -#endif + + VIRT_REGISTER_RESOURCE(res_stream, le_libvirt_stream); } /* @@ -4533,11 +4473,8 @@ PHP_FUNCTION(libvirt_domain_lookup_by_id) DPRINTF("%s: domain id = '%d', returning %p\n", PHPFUNC, (int)id, res_domain->domain); resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->domain, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_domain, le_libvirt_domain)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_domain, le_libvirt_domain); -#endif + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); } /* @@ -5644,11 +5581,8 @@ PHP_FUNCTION(libvirt_domain_new) DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->domain, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_domain, le_libvirt_domain)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_domain, le_libvirt_domain); -#endif + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); } /* @@ -5880,11 +5814,8 @@ PHP_FUNCTION(libvirt_domain_change_memory) DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->domain, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_domain, le_libvirt_domain)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_domain, le_libvirt_domain); -#endif + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); } /* @@ -5965,11 +5896,8 @@ PHP_FUNCTION(libvirt_domain_change_boot_devices) DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->domain, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_domain, le_libvirt_domain)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_domain, le_libvirt_domain); -#endif + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); } /* @@ -6618,11 +6546,8 @@ PHP_FUNCTION(libvirt_domain_define_xml) DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->domain, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_domain, le_libvirt_domain)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_domain, le_libvirt_domain); -#endif + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); } /* @@ -6656,11 +6581,8 @@ PHP_FUNCTION(libvirt_domain_create_xml) DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->domain, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_domain, le_libvirt_domain)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_domain, le_libvirt_domain); -#endif + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); } /* @@ -7447,11 +7369,8 @@ PHP_FUNCTION(libvirt_domain_migrate) DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); resource_change_counter(INT_RESOURCE_DOMAIN, dconn->conn, res_domain->domain, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_domain, le_libvirt_domain)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_domain, le_libvirt_domain); -#endif + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); } /* @@ -7547,11 +7466,8 @@ PHP_FUNCTION(libvirt_domain_snapshot_lookup_by_name) DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res_snapshot->snapshot, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_snapshot, le_libvirt_snapshot)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_snapshot, le_libvirt_snapshot); -#endif + + VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); } /* @@ -7583,11 +7499,8 @@ PHP_FUNCTION(libvirt_domain_snapshot_create) DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res_snapshot->snapshot, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_snapshot, le_libvirt_snapshot)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_snapshot, le_libvirt_snapshot); -#endif + + VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); } /* @@ -7737,11 +7650,8 @@ PHP_FUNCTION(libvirt_storagepool_lookup_by_name) DPRINTF("%s: returning %p\n", PHPFUNC, res_pool->pool); resource_change_counter(INT_RESOURCE_STORAGEPOOL, conn->conn, res_pool->pool, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_pool, le_libvirt_storagepool)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_pool, le_libvirt_storagepool); -#endif + + VIRT_REGISTER_RESOURCE(res_pool, le_libvirt_storagepool); } /* Storagepool functions */ @@ -7773,11 +7683,8 @@ PHP_FUNCTION(libvirt_storagepool_lookup_by_volume) DPRINTF("%s: returning %p\n", PHPFUNC, res_pool->pool); resource_change_counter(INT_RESOURCE_STORAGEPOOL, res_pool->conn->conn, res_pool->pool, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_pool, le_libvirt_storagepool)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_pool, le_libvirt_storagepool); -#endif + + VIRT_REGISTER_RESOURCE(res_pool, le_libvirt_storagepool); } /* @@ -7881,11 +7788,8 @@ PHP_FUNCTION(libvirt_storagevolume_lookup_by_name) DPRINTF("%s: returning %p\n", PHPFUNC, res_volume->volume); resource_change_counter(INT_RESOURCE_VOLUME, pool->conn->conn, res_volume->volume, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_volume, le_libvirt_volume)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_volume, le_libvirt_volume); -#endif + + VIRT_REGISTER_RESOURCE(res_volume, le_libvirt_volume); } /* @@ -7922,11 +7826,8 @@ PHP_FUNCTION(libvirt_storagevolume_lookup_by_path) DPRINTF("%s: returning %p\n", PHPFUNC, res_volume->volume); resource_change_counter(INT_RESOURCE_VOLUME, conn->conn, res_volume->volume, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_volume, le_libvirt_volume)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_volume, le_libvirt_volume); -#endif + + VIRT_REGISTER_RESOURCE(res_volume, le_libvirt_volume); } /* @@ -8079,11 +7980,8 @@ PHP_FUNCTION(libvirt_storagevolume_create_xml) DPRINTF("%s: returning %p\n", PHPFUNC, res_volume->volume); resource_change_counter(INT_RESOURCE_VOLUME, pool->conn->conn, res_volume->volume, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_volume, le_libvirt_volume)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_volume, le_libvirt_volume); -#endif + + VIRT_REGISTER_RESOURCE(res_volume, le_libvirt_volume); } /* @@ -8132,11 +8030,8 @@ PHP_FUNCTION(libvirt_storagevolume_create_xml_from) DPRINTF("%s: returning %p\n", PHPFUNC, res_volume->volume); resource_change_counter(INT_RESOURCE_VOLUME, pool->conn->conn, res_volume->volume, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_volume, le_libvirt_volume)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_volume, le_libvirt_volume); -#endif + + VIRT_REGISTER_RESOURCE(res_volume, le_libvirt_volume); } /* @@ -8361,11 +8256,8 @@ PHP_FUNCTION(libvirt_storagepool_lookup_by_uuid_string) DPRINTF("%s: returning %p\n", PHPFUNC, res_pool->pool); resource_change_counter(INT_RESOURCE_STORAGEPOOL, conn->conn, res_pool->pool, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_pool, le_libvirt_storagepool)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_pool, le_libvirt_storagepool); -#endif + + VIRT_REGISTER_RESOURCE(res_pool, le_libvirt_storagepool); } /* @@ -8443,11 +8335,8 @@ PHP_FUNCTION(libvirt_storagepool_define_xml) DPRINTF("%s: returning %p\n", PHPFUNC, res_pool->pool); resource_change_counter(INT_RESOURCE_STORAGEPOOL, conn->conn, res_pool->pool, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_pool, le_libvirt_storagepool)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_pool, le_libvirt_storagepool); -#endif + + VIRT_REGISTER_RESOURCE(res_pool, le_libvirt_storagepool); } /* @@ -8883,11 +8772,6 @@ PHP_FUNCTION(libvirt_list_domain_resources) { php_libvirt_connection *conn = NULL; zval *zconn; -#if PHP_MAJOR_VERSION >= 7 - zval zdomain; -#else - zval *zdomain; -#endif int count = -1; int expectedcount = -1; int *ids; @@ -8917,15 +8801,8 @@ PHP_FUNCTION(libvirt_list_domain_resources) res_domain->conn = conn; + VIRT_REGISTER_LIST_RESOURCE(domain); resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->domain, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(&zdomain, zend_register_resource(res_domain, le_libvirt_domain)); - add_next_index_zval(return_value, &zdomain); -#else - ALLOC_INIT_ZVAL(zdomain); - ZEND_REGISTER_RESOURCE(zdomain, res_domain, le_libvirt_domain); - add_next_index_zval(return_value, zdomain); -#endif } } efree(ids); @@ -8947,14 +8824,7 @@ PHP_FUNCTION(libvirt_list_domain_resources) res_domain->conn = conn; -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(&zdomain, zend_register_resource(res_domain, le_libvirt_domain)); - add_next_index_zval(return_value, &zdomain); -#else - ALLOC_INIT_ZVAL(zdomain); - ZEND_REGISTER_RESOURCE(zdomain, res_domain, le_libvirt_domain); - add_next_index_zval(return_value, zdomain); -#endif + VIRT_REGISTER_LIST_RESOURCE(domain); resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->domain, 1 TSRMLS_CC); } free(names[i]); @@ -9091,11 +8961,6 @@ PHP_FUNCTION(libvirt_list_all_networks) { php_libvirt_connection *conn = NULL; zval *zconn; -#if PHP_MAJOR_VERSION >= 7 - zval znetwork; -#else - zval *znetwork; -#endif zend_long flags = VIR_CONNECT_LIST_NETWORKS_ACTIVE | VIR_CONNECT_LIST_NETWORKS_INACTIVE; int count = -1; @@ -9119,17 +8984,9 @@ PHP_FUNCTION(libvirt_list_all_networks) res_network->network = network; res_network->conn = conn; + VIRT_REGISTER_LIST_RESOURCE(network); resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, res_network->network, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(&znetwork, zend_register_resource(res_network, - le_libvirt_network)); - add_next_index_zval(return_value, &znetwork); -#else - ALLOC_INIT_ZVAL(znetwork); - ZEND_REGISTER_RESOURCE(znetwork, res_network, le_libvirt_network); - add_next_index_zval(return_value, znetwork); -#endif } } @@ -9267,11 +9124,8 @@ PHP_FUNCTION(libvirt_nodedev_get) DPRINTF("%s: returning %p\n", PHPFUNC, res_dev->device); resource_change_counter(INT_RESOURCE_NODEDEV, conn->conn, res_dev->device, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_dev, le_libvirt_nodedev)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_dev, le_libvirt_nodedev); -#endif + + VIRT_REGISTER_RESOURCE(res_dev, le_libvirt_nodedev); } /* @@ -9534,11 +9388,8 @@ PHP_FUNCTION(libvirt_network_define_xml) DPRINTF("%s: returning %p\n", PHPFUNC, res_net->network); resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, res_net->network, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_net, le_libvirt_network)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_net, le_libvirt_network); -#endif + + VIRT_REGISTER_RESOURCE(res_net, le_libvirt_network); } /* @@ -9591,11 +9442,8 @@ PHP_FUNCTION(libvirt_network_get) DPRINTF("%s: returning %p\n", PHPFUNC, res_net->network); resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, res_net->network, 1 TSRMLS_CC); -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, zend_register_resource(res_net, le_libvirt_network)); -#else - ZEND_REGISTER_RESOURCE(return_value, res_net, le_libvirt_network); -#endif + + VIRT_REGISTER_RESOURCE(res_net, le_libvirt_network); } /* -- 2.13.0

This function must bump refcount of the returned resource because it just returns the one that was registered with libvirt_connect call, otherwise it would sefgault if calling code called this function more than once and the return of the first call would be GCd before the 2nd call. --- src/libvirt-php.c | 18 ++++++------------ src/libvirt-php.h | 14 +++++++++----- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 73466f1..89b17bb 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -2177,13 +2177,8 @@ PHP_FUNCTION(libvirt_connect) resource_change_counter(INT_RESOURCE_CONNECTION, NULL, conn->conn, 1 TSRMLS_CC); DPRINTF("%s: Connection to %s established, returning %p\n", PHPFUNC, url, conn->conn); -#if PHP_MAJOR_VERSION >= 7 - conn->resource_id = zend_register_resource(conn, le_libvirt_connection); - ZVAL_RES(return_value, conn->resource_id); -#else - ZEND_REGISTER_RESOURCE(return_value, conn, le_libvirt_connection); - conn->resource_id = Z_LVAL_P(return_value); -#endif + VIRT_REGISTER_RESOURCE(conn, le_libvirt_connection); + conn->resource = VIRT_RESOURCE_HANDLE(return_value); } /* @@ -7216,11 +7211,10 @@ PHP_FUNCTION(libvirt_domain_get_connect) conn = domain->conn; if (conn->conn == NULL) RETURN_FALSE; -#if PHP_MAJOR_VERSION >= 7 - ZVAL_RES(return_value, conn->resource_id); -#else - RETURN_RESOURCE(conn->resource_id); -#endif + + VIRT_RETURN_RESOURCE(conn->resource); + /* since we're returning already registered resource, bump refcount */ + Z_ADDREF_P(return_value); } /* diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 0422661..ed6a8bc 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -122,6 +122,7 @@ typedef uint64_t arch_uint; #if PHP_MAJOR_VERSION >= 7 typedef size_t strsize_t; typedef zend_resource virt_resource; +typedef virt_resource *virt_resource_handle; #define VIRT_RETURN_RESOURCE(_resource) \ RETVAL_RES(_resource) @@ -135,6 +136,9 @@ typedef zend_resource virt_resource; add_next_index_zval(return_value, &zret); \ } while(0) +#define VIRT_RESOURCE_HANDLE(_resource) \ + Z_RES_P(_resource) + #define VIRT_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \ if ((_state = (_type)zend_fetch_resource(Z_RES_P(*_zval), _name, _le)) == NULL) { \ RETURN_FALSE; \ @@ -179,6 +183,7 @@ typedef int strsize_t; typedef long zend_long; typedef unsigned long zend_ulong; typedef zend_rsrc_list_entry virt_resource; +typedef long virt_resource_handle; #define VIRT_RETURN_RESOURCE(_resource) \ RETVAL_RESOURCE((long) _resource) @@ -193,6 +198,9 @@ typedef zend_rsrc_list_entry virt_resource; add_next_index_zval(return_value, zret); \ } while(0) +#define VIRT_RESOURCE_HANDLE(_resource) \ + Z_LVAL_P(_resource) + #define VIRT_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \ ZEND_FETCH_RESOURCE(_state, _type, _zval, -1, _name, _le); @@ -298,11 +306,7 @@ typedef struct tVMNetwork { /* Libvirt-php types */ typedef struct _php_libvirt_connection { virConnectPtr conn; -#if PHP_MAJOR_VERSION >= 7 - zend_resource *resource_id; -#else - long resource_id; -#endif + virt_resource_handle resource; } php_libvirt_connection; typedef struct _php_libvirt_stream { -- 2.13.0

This macro handles differences in array initialization between PHP7 and older. --- src/libvirt-php.c | 96 ++++++++----------------------------------------------- src/libvirt-php.h | 11 +++++++ 2 files changed, 24 insertions(+), 83 deletions(-) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 89b17bb..7784450 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -2265,11 +2265,8 @@ PHP_FUNCTION(libvirt_node_get_cpu_stats) array_init(return_value); for (i = 0; i < 2; i++) { -#if PHP_MAJOR_VERSION >= 7 - zval *arr, zarr; -#else zval *arr; -#endif + if (i > 0) #ifdef EXTWIN Sleep(1000); @@ -2282,12 +2279,7 @@ PHP_FUNCTION(libvirt_node_get_cpu_stats) RETURN_FALSE; } -#if PHP_MAJOR_VERSION >= 7 - arr = &zarr; -#else - ALLOC_INIT_ZVAL(arr); -#endif - array_init(arr); + VIRT_ARRAY_INIT(arr); for (j = 0; j < nparams; j++) { DPRINTF("%s: Field %s has value of %llu\n", __FUNCTION__, params[j].field, params[j].value); @@ -2333,11 +2325,7 @@ PHP_FUNCTION(libvirt_node_get_cpu_stats_for_each_cpu) int done = 0; int i, j, numCpus; time_t startTime = 0; -#if PHP_MAJOR_VERSION >= 7 - zval *time_array, ztime_array; -#else zval *time_array; -#endif GET_CONNECTION_FROM_ARGS("r|l", &zconn, &avg); @@ -2366,33 +2354,18 @@ PHP_FUNCTION(libvirt_node_get_cpu_stats_for_each_cpu) iter = 0; done = 0; while (!done) { -#if PHP_MAJOR_VERSION >= 7 - zval *arr, zarr; - arr = &zarr; -#else zval *arr; - ALLOC_INIT_ZVAL(arr); -#endif + VIRT_ARRAY_INIT(arr); - array_init(arr); for (i = 0; i < numCpus; i++) { -#if PHP_MAJOR_VERSION >= 7 - zval *arr2, zarr2; -#else zval *arr2; -#endif if (virNodeGetCPUStats(conn->conn, i, params, &nparams, 0) != 0) { set_error("Unable to get node cpu stats" TSRMLS_CC); RETURN_FALSE; } -#if PHP_MAJOR_VERSION >= 7 - arr2 = &zarr2; -#else - ALLOC_INIT_ZVAL(arr2); -#endif - array_init(arr2); + VIRT_ARRAY_INIT(arr2); for (j = 0; j < nparams; j++) add_assoc_long(arr2, params[j].field, params[j].value); @@ -2416,13 +2389,7 @@ PHP_FUNCTION(libvirt_node_get_cpu_stats_for_each_cpu) iter++; } -#if PHP_MAJOR_VERSION >= 7 - time_array = &ztime_array; -#else - ALLOC_INIT_ZVAL(time_array); -#endif - array_init(time_array); - + VIRT_ARRAY_INIT(time_array); add_assoc_long(time_array, "start", startTime); add_assoc_long(time_array, "finish", time(NULL)); add_assoc_long(time_array, "duration", time(NULL) - startTime); @@ -2539,28 +2506,15 @@ PHP_FUNCTION(libvirt_connect_get_machine_types) snprintf(tmp, sizeof(tmp), "//capabilities/guest/arch[@name=\"%s\"]/domain/@type", ret[i]); char **ret2 = get_array_from_xpath(caps, tmp, &num2); if (ret2 != NULL) { -#if PHP_MAJOR_VERSION >= 7 - zval *arr2, zarr2; - arr2 = &zarr2; -#else zval *arr2; - ALLOC_INIT_ZVAL(arr2); -#endif - array_init(arr2); + VIRT_ARRAY_INIT(arr2); for (j = 0; j < num2; j++) { int num3, k; char tmp2[1024] = { 0 }; - - /* Common */ -#if PHP_MAJOR_VERSION >= 7 - zval *arr3, zarr3; - arr3 = &zarr3; -#else zval *arr3; - ALLOC_INIT_ZVAL(arr3); -#endif - array_init(arr3); + + VIRT_ARRAY_INIT(arr3); snprintf(tmp2, sizeof(tmp2), "//capabilities/guest/arch[@name=\"%s\"]/machine", ret[i]); @@ -2582,15 +2536,8 @@ PHP_FUNCTION(libvirt_connect_get_machine_types) if (numTmp == NULL) { VIRT_ADD_ASSOC_STRING(arr2, key, ret3[k]); } else { -#if PHP_MAJOR_VERSION >= 7 - zval *arr4, zarr4; - arr4 = &zarr4; -#else zval *arr4; - ALLOC_INIT_ZVAL(arr4); -#endif - array_init(arr4); - + VIRT_ARRAY_INIT(arr4); VIRT_ADD_ASSOC_STRING(arr4, "name", ret3[k]); VIRT_ADD_ASSOC_STRING(arr4, "maxCpus", numTmp); @@ -2622,14 +2569,8 @@ PHP_FUNCTION(libvirt_connect_get_machine_types) if (numTmp == NULL) { VIRT_ADD_ASSOC_STRING(arr3, key, ret3[k]); } else { -#if PHP_MAJOR_VERSION >= 7 - zval *arr4, zarr4; - arr4 = &zarr4; -#else zval *arr4; - ALLOC_INIT_ZVAL(arr4); -#endif - array_init(arr4); + VIRT_ARRAY_INIT(arr4); VIRT_ADD_ASSOC_STRING(arr4, "name", ret3[k]); VIRT_ADD_ASSOC_STRING(arr4, "maxCpus", numTmp); @@ -3007,14 +2948,9 @@ PHP_FUNCTION(libvirt_connect_get_all_domain_stats) RETURN_FALSE; for (i = 0; i < retval; i++) { -#if PHP_MAJOR_VERSION >= 7 - zval *arr2, zarr2; - arr2 = &zarr2; -#else zval *arr2; - ALLOC_INIT_ZVAL(arr2); -#endif - array_init(arr2); + VIRT_ARRAY_INIT(arr2); + for (j = 0; j < retstats[i]->nparams; j++) { params = retstats[i]->params[j]; switch (params.type) { @@ -5316,13 +5252,7 @@ PHP_FUNCTION(libvirt_connect_get_soundhw_models) continue; if ((i > 0) && (flags & CONNECT_FLAG_SOUNDHW_GET_NAMES)) { -#if PHP_MAJOR_VERSION >= 7 - zval *arr, zarr; - arr = &zarr; -#else zval *arr; - ALLOC_INIT_ZVAL(arr); -#endif memset(desc, 0, sizeof(desc)); for (i = 1; i < t.numTokens; i++) { strcat(desc, t.tokens[i]); @@ -5330,7 +5260,7 @@ PHP_FUNCTION(libvirt_connect_get_soundhw_models) strcat(desc, " "); } - array_init(arr); + VIRT_ARRAY_INIT(arr); VIRT_ADD_ASSOC_STRING(arr, "name", t.tokens[0]); VIRT_ADD_ASSOC_STRING(arr, "description", desc); add_next_index_zval(return_value, arr); diff --git a/src/libvirt-php.h b/src/libvirt-php.h index ed6a8bc..757c5f5 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -178,6 +178,12 @@ typedef virt_resource *virt_resource_handle; _info.length = ZSTR_LEN(tmp_key_info); \ } while(0) +#define VIRT_ARRAY_INIT(_name) do { \ + zval z##_name; \ + _name = &z##_name; \ + array_init(_name); \ + } while(0) + #else /* PHP_MAJOR_VERSION < 7 */ typedef int strsize_t; typedef long zend_long; @@ -239,6 +245,11 @@ typedef long virt_resource_handle; _info.type = zend_hash_get_current_key_ex(_ht, &_info.name, &_info.length, &_idx, 0, &_pos); \ } while(0) +#define VIRT_ARRAY_INIT(_name) do {\ + ALLOC_INIT_ZVAL(_name); \ + array_init(_name); \ + } while(0) + #endif /* PHP_MAJOR_VERSION < 7 */ typedef struct tTokenizer { -- 2.13.0

--- src/libvirt-php.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/libvirt-php.h | 7 +++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 7784450..535d321 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -54,6 +54,7 @@ int le_libvirt_network; int le_libvirt_nodedev; int le_libvirt_stream; int le_libvirt_snapshot; +int le_libvirt_nwfilter; ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_connect, 0, 0, 0) ZEND_ARG_INFO(0, url) @@ -823,6 +824,8 @@ translate_counter_type(int type) return "storage volume"; case INT_RESOURCE_SNAPSHOT: return "snapshot"; + case INT_RESOURCE_NWFILTER: + return "nwfilter"; } return "unknown"; @@ -1226,6 +1229,17 @@ void free_resource(int type, void *mem TSRMLS_DC) resource_change_counter(INT_RESOURCE_SNAPSHOT, NULL, (virDomainSnapshotPtr)mem, 0 TSRMLS_CC); } } + + if (type == INT_RESOURCE_NWFILTER) { + rv = virNWFilterFree((virNWFilterPtr) mem); + if (rv != 0) { + DPRINTF("%s: virNWFilterFree(%p) returned %d (%s)\n", __FUNCTION__, (virNWFilterPtr) mem, rv, LIBVIRT_G(last_error)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainSnapshotFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virNWFilterFree(%p) completed successfully\n", __FUNCTION__, (virNWFilterPtr) mem); + resource_change_counter(INT_RESOURCE_NWFILTER, NULL, (virNWFilterPtr) mem, 0 TSRMLS_CC); + } + } } /* @@ -1570,7 +1584,7 @@ static void php_libvirt_snapshot_dtor(virt_resource *rsrc TSRMLS_DC) rv = virDomainSnapshotFree(snapshot->snapshot); if (rv != 0) { DPRINTF("%s: virDomainSnapshotFree(%p) returned %d\n", __FUNCTION__, snapshot->snapshot, rv); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStorageVolFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainSnapshotFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); } else { DPRINTF("%s: virDomainSnapshotFree(%p) completed successfully\n", __FUNCTION__, snapshot->snapshot); resource_change_counter(INT_RESOURCE_SNAPSHOT, snapshot->domain->conn->conn, snapshot->snapshot, 0 TSRMLS_CC); @@ -1581,6 +1595,34 @@ static void php_libvirt_snapshot_dtor(virt_resource *rsrc TSRMLS_DC) } } +/* Destructor for nwfilter resource */ +static void php_libvirt_nwfilter_dtor(virt_resource *rsrc) +{ + php_libvirt_nwfilter *nwfilter = (php_libvirt_nwfilter *) rsrc->ptr; + int rv = 0; + + if (nwfilter != NULL) { + if (nwfilter->nwfilter != NULL) { + if (!check_resource_allocation(NULL, INT_RESOURCE_NWFILTER, nwfilter->nwfilter TSRMLS_CC)) { + nwfilter->nwfilter = NULL; + efree(nwfilter); + + return; + } + rv = virNWFilterFree(nwfilter->nwfilter); + if (rv != 0) { + DPRINTF("%s: virNWFilterFree(%p) returned %d\n", __FUNCTION__, nwfilter->nwfilter, rv); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virNWFilterFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virNWFilterFee(%p) completed successfully\n", __FUNCTION__, nwfilter->nwfilter); + resource_change_counter(INT_RESOURCE_NWFILTER, nwfilter->conn->conn, nwfilter->nwfilter, 0 TSRMLS_CC); + } + nwfilter->nwfilter = NULL; + } + efree(nwfilter); + } +} + /* ZEND Module inicialization function */ PHP_MINIT_FUNCTION(libvirt) { @@ -1593,6 +1635,7 @@ PHP_MINIT_FUNCTION(libvirt) le_libvirt_network = zend_register_list_destructors_ex(php_libvirt_network_dtor, NULL, PHP_LIBVIRT_NETWORK_RES_NAME, module_number); le_libvirt_nodedev = zend_register_list_destructors_ex(php_libvirt_nodedev_dtor, NULL, PHP_LIBVIRT_NODEDEV_RES_NAME, module_number); le_libvirt_snapshot = zend_register_list_destructors_ex(php_libvirt_snapshot_dtor, NULL, PHP_LIBVIRT_SNAPSHOT_RES_NAME, module_number); + le_libvirt_nwfilter = zend_register_list_destructors_ex(php_libvirt_nwfilter_dtor, NULL, PHP_LIBVIRT_NWFILTER_RES_NAME, module_number); ZEND_INIT_MODULE_GLOBALS(libvirt, php_libvirt_init_globals, NULL); @@ -1994,7 +2037,21 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) VIRT_FETCH_RESOURCE(snapshot, php_libvirt_snapshot*, &zsnapshot, PHP_LIBVIRT_SNAPSHOT_RES_NAME, le_libvirt_snapshot);\ if ((snapshot == NULL) || (snapshot->snapshot == NULL)) \ RETURN_FALSE; \ -} while (0) \ + } while (0) \ + +#define GET_NWFILTER_FROM_ARGS(args, ...) \ + do { \ + reset_error(TSRMLS_C); \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_ARGS__) == FAILURE) { \ + set_error("Invalid arguments" TSRMLS_CC); \ + RETURN_FALSE; \ + } \ + \ + VIRT_FETCH_RESOURCE(nwfilter, php_libvirt_nwfilter *, &znwfilter, \ + PHP_LIBVIRT_NWFILTER_RES_NAME, le_libvirt_nwfilter); \ + if ((nwfilter == NULL) || (nwfilter->nwfilter == NULL)) \ + RETURN_FALSE; \ + } while (0) \ #define LONGLONG_INIT \ char tmpnumber[64] diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 757c5f5..2e7a3ca 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -298,6 +298,7 @@ typedef struct _resource_info { #define INT_RESOURCE_VOLUME 0x20 #define INT_RESOURCE_SNAPSHOT 0x40 #define INT_RESOURCE_STREAM 0x50 +#define INT_RESOURCE_NWFILTER 0x60 typedef struct tVMDisk { char *path; @@ -355,6 +356,11 @@ typedef struct _php_libvirt_volume { php_libvirt_connection* conn; } php_libvirt_volume; +typedef struct _php_libvirt_nwfilter { + virNWFilterPtr nwfilter; + php_libvirt_connection* conn; +} php_libvirt_nwfilter; + typedef struct _php_libvirt_cred_value { int count; int type; @@ -382,6 +388,7 @@ char **get_array_from_xpath(char *xml, char *xpath, int *num); #define PHP_LIBVIRT_NETWORK_RES_NAME "Libvirt virtual network" #define PHP_LIBVIRT_NODEDEV_RES_NAME "Libvirt node device" #define PHP_LIBVIRT_SNAPSHOT_RES_NAME "Libvirt domain snapshot" +#define PHP_LIBVIRT_NWFILTER_RES_NAME "Libvirt nwfilter" PHP_MINIT_FUNCTION(libvirt); PHP_MSHUTDOWN_FUNCTION(libvirt); -- 2.13.0

On 06/22/2017 09:14 PM, Dawid Zamirski wrote:
--- src/libvirt-php.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/libvirt-php.h | 7 +++++++ 2 files changed, 66 insertions(+), 2 deletions(-)
diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 7784450..535d321 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -54,6 +54,7 @@ int le_libvirt_network; int le_libvirt_nodedev; int le_libvirt_stream; int le_libvirt_snapshot; +int le_libvirt_nwfilter;
ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_connect, 0, 0, 0) ZEND_ARG_INFO(0, url) @@ -823,6 +824,8 @@ translate_counter_type(int type) return "storage volume"; case INT_RESOURCE_SNAPSHOT: return "snapshot"; + case INT_RESOURCE_NWFILTER: + return "nwfilter"; }
return "unknown"; @@ -1226,6 +1229,17 @@ void free_resource(int type, void *mem TSRMLS_DC) resource_change_counter(INT_RESOURCE_SNAPSHOT, NULL, (virDomainSnapshotPtr)mem, 0 TSRMLS_CC); } } + + if (type == INT_RESOURCE_NWFILTER) { + rv = virNWFilterFree((virNWFilterPtr) mem); + if (rv != 0) { + DPRINTF("%s: virNWFilterFree(%p) returned %d (%s)\n", __FUNCTION__, (virNWFilterPtr) mem, rv, LIBVIRT_G(last_error)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainSnapshotFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virNWFilterFree(%p) completed successfully\n", __FUNCTION__, (virNWFilterPtr) mem); + resource_change_counter(INT_RESOURCE_NWFILTER, NULL, (virNWFilterPtr) mem, 0 TSRMLS_CC); + } + } }
/* @@ -1570,7 +1584,7 @@ static void php_libvirt_snapshot_dtor(virt_resource *rsrc TSRMLS_DC) rv = virDomainSnapshotFree(snapshot->snapshot); if (rv != 0) { DPRINTF("%s: virDomainSnapshotFree(%p) returned %d\n", __FUNCTION__, snapshot->snapshot, rv); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStorageVolFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainSnapshotFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); } else { DPRINTF("%s: virDomainSnapshotFree(%p) completed successfully\n", __FUNCTION__, snapshot->snapshot); resource_change_counter(INT_RESOURCE_SNAPSHOT, snapshot->domain->conn->conn, snapshot->snapshot, 0 TSRMLS_CC); @@ -1581,6 +1595,34 @@ static void php_libvirt_snapshot_dtor(virt_resource *rsrc TSRMLS_DC) } }
+/* Destructor for nwfilter resource */ +static void php_libvirt_nwfilter_dtor(virt_resource *rsrc)
Missing TSRMLS_DC after @rsrc. I'll fix this before pushing.
+{ + php_libvirt_nwfilter *nwfilter = (php_libvirt_nwfilter *) rsrc->ptr; + int rv = 0; + + if (nwfilter != NULL) { + if (nwfilter->nwfilter != NULL) { + if (!check_resource_allocation(NULL, INT_RESOURCE_NWFILTER, nwfilter->nwfilter TSRMLS_CC)) { + nwfilter->nwfilter = NULL; + efree(nwfilter); + + return; + } + rv = virNWFilterFree(nwfilter->nwfilter); + if (rv != 0) { + DPRINTF("%s: virNWFilterFree(%p) returned %d\n", __FUNCTION__, nwfilter->nwfilter, rv); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virNWFilterFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virNWFilterFee(%p) completed successfully\n", __FUNCTION__, nwfilter->nwfilter); + resource_change_counter(INT_RESOURCE_NWFILTER, nwfilter->conn->conn, nwfilter->nwfilter, 0 TSRMLS_CC); + } + nwfilter->nwfilter = NULL; + } + efree(nwfilter); + } +} +
Michal

adds the following functions: * libvirt_nwfilter_define_xml * libvirt_nwfilter_undefine * libvirt_nwfilter_get_xml_desc * libvirt_nwfilter_get_name * libvirt_nwfilter_get_uuid * libvirt_nwfilter_get_uuid_string * libvirt_nwfilter_lookup_by_name * libvirt_nwfilter_lookup_by_uuid * libvirt_nwfilter_lookup_by_uuid_string * libvirt_list_nwfilters * libvirt_list_all_nwfilters --- src/libvirt-php.c | 390 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-php.h | 12 ++ 2 files changed, 402 insertions(+) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 535d321..5893742 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -645,6 +645,16 @@ static zend_function_entry libvirt_functions[] = { 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) + /* NWFilter functions */ + PHP_FE(libvirt_nwfilter_define_xml, arginfo_libvirt_conn_xml) + PHP_FE(libvirt_nwfilter_undefine, arginfo_libvirt_conn) + PHP_FE(libvirt_nwfilter_get_xml_desc, arginfo_libvirt_conn_xpath) + PHP_FE(libvirt_nwfilter_get_uuid_string, arginfo_libvirt_conn) + PHP_FE(libvirt_nwfilter_get_uuid, arginfo_libvirt_conn) + PHP_FE(libvirt_nwfilter_get_name, arginfo_libvirt_conn) + PHP_FE(libvirt_nwfilter_lookup_by_name, arginfo_libvirt_conn_name) + PHP_FE(libvirt_nwfilter_lookup_by_uuid_string, arginfo_libvirt_conn_uuid) + 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_optflags) @@ -659,6 +669,8 @@ static zend_function_entry libvirt_functions[] = { 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 */ PHP_FE(libvirt_version, arginfo_libvirt_opttype) PHP_FE(libvirt_check_version, arginfo_libvirt_check_version) @@ -9074,7 +9086,93 @@ PHP_FUNCTION(libvirt_list_nodedevs) efree(names); } + +/* + * Function name: libvirt_list_all_nwfilters + * Since version: 0.5.4 + * Description: Function is used to list nwfilters on the connection + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt nwfilter resources array for the connection + */ +PHP_FUNCTION(libvirt_list_all_nwfilters) +{ + php_libvirt_nwfilter *res_nwfilter; + php_libvirt_connection *conn = NULL; + virNWFilterPtr *filters = NULL; + virNWFilterPtr nwfilter = NULL; + zval *zconn; + int count = -1; + size_t i = 0; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + /* in current libvirt version, flags are not used for this, so passing 0 */ + if ((count = virConnectListAllNWFilters(conn->conn, &filters, 0)) < 0) + RETURN_FALSE; + + DPRINTF("%s: Found %d nwfilters\n", PHPFUNC, count); + + array_init(return_value); + + for (i = 0; i < count; i++) { + nwfilter = filters[i]; + res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); + res_nwfilter->nwfilter = nwfilter; + res_nwfilter->conn = conn; + + resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, + res_nwfilter->nwfilter, 1 TSRMLS_CC); + VIRT_REGISTER_LIST_RESOURCE(nwfilter); + } +} + +/* + * Function name: libvirt_list_nwfilters + * Since version: 0.5.4 + * Description: Function is used to list nwfilters on the connection + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt nwfilter names array for the connection + */ +PHP_FUNCTION(libvirt_list_nwfilters) +{ + php_libvirt_connection *conn = NULL; + zval *zconn; + int count = -1; + int expectedcount = -1; + char **names; + int i, done = 0; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + array_init(return_value); + + if ((expectedcount = virConnectNumOfNWFilters(conn->conn)) < 0) + RETURN_FALSE; + + names = (char **) emalloc(expectedcount * sizeof(char *)); + count = virConnectListNWFilters(conn->conn, names, expectedcount); + + if (count != expectedcount || count < 0) { + efree(names); + DPRINTF("%s: virConnectListNWFilters returned %d filters, while %d was " + "expected\n", PHPFUNC, count, expectedcount); + RETURN_FALSE; + } + + for (i = 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + + efree(names); + done++; + + + if (!done) + RETURN_FALSE; +} /* Nodedev functions */ + /* * Function name: libvirt_nodedev_get * Since version: 0.4.1(-1) @@ -9782,6 +9880,298 @@ PHP_FUNCTION(libvirt_network_set_autostart) RETURN_TRUE; } +/* NWFilter functions */ + +/* + * Function name: libvirt_nwfilter_define_xml + * Since version: 0.5.4 + * Description: Function is used to define a new nwfilter based on the XML description + * Arguments: @res [resource]: libvirt connection resource + * @xml [string]: XML string definition of nwfilter to be defined + * Returns: libvirt nwfilter resource of newly defined nwfilter + */ +PHP_FUNCTION(libvirt_nwfilter_define_xml) +{ + php_libvirt_connection *conn = NULL; + php_libvirt_nwfilter *res_nwfilter = NULL; + virNWFilter *nwfilter; + zval *zconn; + char *xml = NULL; + strsize_t xml_len; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &xml, &xml_len); + + if ((nwfilter = virNWFilterDefineXML(conn->conn, xml)) == NULL) { + set_error_if_unset("Cannot define a new NWFilter" TSRMLS_CC); + RETURN_FALSE; + } + + res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); + res_nwfilter->nwfilter = nwfilter; + res_nwfilter->conn = conn; + + resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, + res_nwfilter->nwfilter, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_nwfilter, le_libvirt_nwfilter); +} + +/* + * Function name: libvirt_nwfilter_undefine + * Since version: 0.5.4 + * Description: Function is used to undefine already defined nwfilter + * Arguments: @res [resource]: libvirt nwfilter resource + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_nwfilter_undefine) +{ + php_libvirt_nwfilter *nwfilter = NULL; + zval *znwfilter; + + GET_NWFILTER_FROM_ARGS("r", &znwfilter); + + if (virNWFilterUndefine(nwfilter->nwfilter) != 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_nwfilter_get_xml_desc + * Since version: 0.5.4 + * Description: Function is used to get the XML description for the nwfilter + * Arguments: @res [resource]: libvirt nwfilter resource + * @xpath [string]: optional xPath expression string to get just this entry, can be NULL + * Returns: nwfilter XML string or result of xPath expression + */ +PHP_FUNCTION(libvirt_nwfilter_get_xml_desc) +{ + php_libvirt_nwfilter *nwfilter = NULL; + zval *znwfilter; + char *xml = NULL; + char *xpath = NULL; + char *tmp; + strsize_t xpath_len; + int retval = -1; + + GET_NWFILTER_FROM_ARGS("r|s", &znwfilter, &xpath, &xpath_len); + + if (xpath_len < 1) + xpath = NULL; + + xml = virNWFilterGetXMLDesc(nwfilter->nwfilter, 0); + + if (xml == NULL) { + set_error_if_unset("Cannot get nwfilter XML" TSRMLS_CC); + RETURN_FALSE; + } + + tmp = get_string_from_xpath(xml, xpath, NULL, &retval); + + if (tmp == NULL || retval < 0) + VIRT_RETVAL_STRING(xml); + else + VIRT_RETVAL_STRING(tmp); + + free(xml); + free(tmp); +} + +/* + * Function name: libvirt_nwfilter_get_uuid_string + * Since version: 0.5.4 + * Description: Function is used to get nwfilter's UUID in string format + * Arguments: @res [resource]: libvirt nwfilter resource + * Returns: nwfilter UUID string or FALSE on failure + */ +PHP_FUNCTION(libvirt_nwfilter_get_uuid_string) +{ + php_libvirt_nwfilter *nwfilter = NULL; + zval *znwfilter; + char *uuid = NULL; + int ret = -1; + + GET_NWFILTER_FROM_ARGS("r", &znwfilter); + + uuid = (char *) emalloc(VIR_UUID_STRING_BUFLEN); + ret = virNWFilterGetUUIDString(nwfilter->nwfilter, uuid); + + DPRINTF("%s: virNWFilterGetUUIDString(%p) returned %d (%s)\n", PHPFUNC, + nwfilter->nwfilter, ret, uuid); + + if (ret != 0) + RETURN_FALSE; + + VIRT_RETURN_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_nwfilter_get_uuid + * Since version: 0.5.3 + * Descirption: Function is used to get nwfilter's UUID in binary format + * Arguments: @res [resource]: libvirt netowrk resource + * Returns: nwfilter UUID in binary format or FALSE on failure + */ +PHP_FUNCTION(libvirt_nwfilter_get_uuid) +{ + php_libvirt_nwfilter *nwfilter = NULL; + zval *znwfilter; + char *uuid = NULL; + int ret = -1; + + GET_NWFILTER_FROM_ARGS("r", &znwfilter); + + uuid = (char *) emalloc(VIR_UUID_BUFLEN); + ret = virNWFilterGetUUID(nwfilter->nwfilter, (unsigned char *) uuid); + + DPRINTF("%s: virNWFilterUUID(%p, %p) returned %d\n", PHPFUNC, + nwfilter->nwfilter, uuid, ret); + + if (ret != 0) + RETURN_FALSE; + + VIRT_RETVAL_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_nwfilter_get_name + * Since version: 0.5.4 + * Description: Function is used to get nwfilter's name + * Arguments: @res [resource]: libvirt nwfilter resource + * Returns: nwfilter name string or FALSE on failure + */ +PHP_FUNCTION(libvirt_nwfilter_get_name) +{ + php_libvirt_nwfilter *nwfilter = NULL; + zval *znwfilter; + const char *name = NULL; + + GET_NWFILTER_FROM_ARGS("r", &znwfilter); + name = virNWFilterGetName(nwfilter->nwfilter); + + DPRINTF("%s: virNWFilterGetName(%p) returned %s\n", PHPFUNC, + nwfilter->nwfilter, name); + + if (name == NULL) + RETURN_FALSE; + + /* name should not be freed as its lifetime is the same as nwfilter resource */ + VIRT_RETURN_STRING(name); +} + +/* + * Function name: libvirt_nwfilter_lookup_by_name + * Since version: 0.5.4 + * Description: This functions is used to lookup for the nwfilter by it's name + * Arguments: @res [resource]: libvirt connection resource + * @name [string]: name of the nwfilter to get the resource + * Returns: libvirt nwfilter resource + */ +PHP_FUNCTION(libvirt_nwfilter_lookup_by_name) +{ + php_libvirt_nwfilter *res_nwfilter = NULL; + php_libvirt_connection *conn = NULL; + virNWFilterPtr nwfilter = NULL; + zval *zconn; + strsize_t name_len; + char *name = NULL; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); + + if (name == NULL || name_len < 1) + RETURN_FALSE; + + nwfilter = virNWFilterLookupByName(conn->conn, name); + + if (nwfilter == NULL) + RETURN_FALSE; + + res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); + res_nwfilter->conn = conn; + res_nwfilter->nwfilter = nwfilter; + + resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, + res_nwfilter->nwfilter, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_nwfilter, le_libvirt_nwfilter); +} + +/* + * Function name: libvirt_nwfilter_lookup_by_uuid_string + * Since version: 0.5.4 + * Description: Function is used to lookup for nwfilter identified by UUID string + * Arguments: @res [resource]: libvirt connection resource + * @uuid [string]: UUID string to look for nwfilter + * Returns: libvirt nwfilter resource + */ +PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid_string) +{ + php_libvirt_nwfilter *res_nwfilter = NULL; + php_libvirt_connection *conn = NULL; + virNWFilterPtr nwfilter = NULL; + zval *zconn; + char *uuid = NULL; + strsize_t uuid_len; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); + + if (uuid == NULL || uuid_len < 1) + RETURN_FALSE; + + nwfilter = virNWFilterLookupByUUIDString(conn->conn, uuid); + + if (nwfilter == NULL) + RETURN_FALSE; + + res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); + res_nwfilter->conn = conn; + res_nwfilter->nwfilter = nwfilter; + + resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, + res_nwfilter->nwfilter, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_nwfilter, le_libvirt_nwfilter); +} + +/* + * Function name: libvirt_nwfilter_lookup_by_uuid + * Since version: 0.5.4 + * Description: Function is used to lookup for nwfilter by it's UUID in the binary format + * Arguments: @res [resource]: libvirt connection resource from libvirt_connect() + * @uuid [string]: binary defined UUID to look for + * Returns: libvirt nwfilter resource + */ +PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid) +{ + php_libvirt_nwfilter *res_nwfilter = NULL; + php_libvirt_connection *conn = NULL; + virNWFilterPtr nwfilter = NULL; + zval *zconn; + strsize_t uuid_len; + unsigned char *uuid = NULL; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); + + if ((uuid == NULL) || (uuid_len < 1)) + RETURN_FALSE; + + nwfilter = virNWFilterLookupByUUID(conn->conn, uuid); + + if (nwfilter == NULL) + RETURN_FALSE; + + res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); + res_nwfilter->conn = conn; + res_nwfilter->nwfilter = nwfilter; + + resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, + res_nwfilter->nwfilter, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_nwfilter, le_libvirt_nwfilter); +} + /* * Function name: libvirt_version * Since version: 0.4.1(-1) diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 2e7a3ca..7962e33 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -558,10 +558,22 @@ 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); +PHP_FUNCTION(libvirt_nwfilter_get_xml_desc); +PHP_FUNCTION(libvirt_nwfilter_get_name); +PHP_FUNCTION(libvirt_nwfilter_get_uuid_string); +PHP_FUNCTION(libvirt_nwfilter_get_uuid); +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_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); -- 2.13.0

On 06/22/2017 09:14 PM, Dawid Zamirski wrote:
Hello,
This series adds support for libvirt's virNWFilter* APIs. Since it introduces new resource type, I took the opportunity to cleanup the driver code a little:
* in patches 1-3: added macros to take care of the differences in how PHP5 and PHP7 handle resource types. * in patch 4: libvirt_doman_get_connect was segfaulting when called multiple times because it was not bumping reference count on the resource it was returning to the calling code. * in patch 5: added a macro to take care of the differences in how PHP5 and PHP7 initialize arrays. * patches 6 and 7: implement the missing binding to NWFilter APIs.
Dawid Zamirski (7): move macros to header file. add wrappers for PHP resource handling. update code to use resource handling macros fix libvirt_doman_get_connect implementation. add and use VIRT_ARRAY_INIT macro add nwfilter resource type implement NWFilter API bindings.
src/libvirt-php.c | 923 +++++++++++++++++++++++++++++++----------------------- src/libvirt-php.h | 150 ++++++++- 2 files changed, 672 insertions(+), 401 deletions(-)
ACK. Just a side note, The first line of commit message (usually referred to as subject line) should start with capital letter. I'll fix that before pushing. But this got me thinking, should we follow libvirt's example and finally split src/libvirt-php.c into smaller files that would handle just one object? For example: libvirt-domain.c libvirt-nwfilter.c libvirt-storage.c libvirt-network.c and so on. The other thing that comes to my mind - would you mind updating the example under examples/ so that we can demonstrate how NWFilters work in php? Thanks! Michal

On Fri, Jun 23, 2017 at 4:58 AM, Michal Privoznik <mprivozn@redhat.com> wrote:
But this got me thinking, should we follow libvirt's example and finally split src/libvirt-php.c into smaller files that would handle just one object? For example:
libvirt-domain.c libvirt-nwfilter.c libvirt-storage.c libvirt-network.c
and so on.
If this isn't too difficult to do, I think it'd be a good idea. The massive src/libvirt-php.c is difficult to parse and diff between releases. Breaking it up into separate logical files would make diffs cleaner, and also probably make it easier to centralize the abstractions between different PHP versions as support for them is added. -- 真実はいつも一つ!/ Always, there's only one truth!

On 06/24/2017 06:52 PM, Neal Gompa wrote:
On Fri, Jun 23, 2017 at 4:58 AM, Michal Privoznik <mprivozn@redhat.com> wrote:
But this got me thinking, should we follow libvirt's example and finally split src/libvirt-php.c into smaller files that would handle just one object? For example:
libvirt-domain.c libvirt-nwfilter.c libvirt-storage.c libvirt-network.c
and so on.
If this isn't too difficult to do, I think it'd be a good idea. The massive src/libvirt-php.c is difficult to parse and diff between releases. Breaking it up into separate logical files would make diffs cleaner, and also probably make it easier to centralize the abstractions between different PHP versions as support for them is added.
Exactly. And if we ever want to have configure options to deliberately disable some 'modules' (e.g. I want my php bindings without support for say networks), we can just not compile that file in. It's doubtful that somebody will ever need this though. Perhaps when they are running libvirt without the corresponding module enabled too? Michal

On 06/23/2017 10:58 AM, Michal Privoznik wrote:
<snip/>
But this got me thinking, should we follow libvirt's example and finally split src/libvirt-php.c into smaller files that would handle just one object? For example:
libvirt-domain.c libvirt-nwfilter.c libvirt-storage.c libvirt-network.c
and so on.
Just a clarification on this: I think libvirt-php.c should contain just the necessary glue/register functions for PHP objects and include libvirt-domain.h, libvirt-nwfilter.h, libvirt-storage.h, etc. The macros we have for dealing with different version of PHP (e.g. VIRT_RETURN_RESOURE or VIRT_RETVAL_STRING and friends) can then go to util.h (which can be included from all the new *.c files). libvirt-domain.h could then have all those PHP_FUNCTION(libvirt_domain_*) declarations from libvirt-php.h; libvirt-nwfilter.h could have all those PHP_FUNCTION(libvirt_nwfilter_*) declarations, and so on. The reasoning for this is to have clear dependencies between source files. This basically mimics what we have in libvirt too. Unfortunately, I'm leaving the office for some time now so I will not have time to write this myself. So if you guys want to propose the patches, please be my guest. Otherwise I'll look into it once I'm back. Michal

On Sat, 2017-06-24 at 19:35 +0200, Michal Privoznik wrote:
On 06/23/2017 10:58 AM, Michal Privoznik wrote:
<snip/>
But this got me thinking, should we follow libvirt's example and finally split src/libvirt-php.c into smaller files that would handle just one object? For example:
libvirt-domain.c libvirt-nwfilter.c libvirt-storage.c libvirt-network.c
and so on.
Just a clarification on this: I think libvirt-php.c should contain just the necessary glue/register functions for PHP objects and include libvirt-domain.h, libvirt-nwfilter.h, libvirt-storage.h, etc.
The macros we have for dealing with different version of PHP (e.g. VIRT_RETURN_RESOURE or VIRT_RETVAL_STRING and friends) can then go to util.h (which can be included from all the new *.c files).
libvirt-domain.h could then have all those PHP_FUNCTION(libvirt_domain_*) declarations from libvirt-php.h; libvirt-nwfilter.h could have all those PHP_FUNCTION(libvirt_nwfilter_*) declarations, and so on.
The reasoning for this is to have clear dependencies between source files. This basically mimics what we have in libvirt too.
Unfortunately, I'm leaving the office for some time now so I will not have time to write this myself. So if you guys want to propose the patches, please be my guest. Otherwise I'll look into it once I'm back.
Michal
I'll take care of this some time and also add the examples for the NWFilter API usage some time this or next week (that is, as soon as I'm done with my php-dbus work) Dawid

On 06/27/2017 03:45 PM, Dawid Zamirski wrote:
On Sat, 2017-06-24 at 19:35 +0200, Michal Privoznik wrote:
On 06/23/2017 10:58 AM, Michal Privoznik wrote:
<snip/>
But this got me thinking, should we follow libvirt's example and finally split src/libvirt-php.c into smaller files that would handle just one object? For example:
libvirt-domain.c libvirt-nwfilter.c libvirt-storage.c libvirt-network.c
and so on.
Just a clarification on this: I think libvirt-php.c should contain just the necessary glue/register functions for PHP objects and include libvirt-domain.h, libvirt-nwfilter.h, libvirt-storage.h, etc.
The macros we have for dealing with different version of PHP (e.g. VIRT_RETURN_RESOURE or VIRT_RETVAL_STRING and friends) can then go to util.h (which can be included from all the new *.c files).
libvirt-domain.h could then have all those PHP_FUNCTION(libvirt_domain_*) declarations from libvirt-php.h; libvirt-nwfilter.h could have all those PHP_FUNCTION(libvirt_nwfilter_*) declarations, and so on.
The reasoning for this is to have clear dependencies between source files. This basically mimics what we have in libvirt too.
Unfortunately, I'm leaving the office for some time now so I will not have time to write this myself. So if you guys want to propose the patches, please be my guest. Otherwise I'll look into it once I'm back.
Michal
I'll take care of this some time and also add the examples for the NWFilter API usage some time this or next week (that is, as soon as I'm done with my php-dbus work)
Dawid
Dawid - any progress on this? I'd like to make the release as requested. For that the NWFilter usage example should be enough. The split of libvirt-php.c is rather a big change and as such not really a fit for last minute merge before the release. Again, if you don't have the time I can look into this. Michal

On Mon, 2017-07-31 at 12:35 +0200, Michal Privoznik wrote:
On 06/27/2017 03:45 PM, Dawid Zamirski wrote:
Dawid - any progress on this? I'd like to make the release as requested. For that the NWFilter usage example should be enough. The split of libvirt-php.c is rather a big change and as such not really a fit for last minute merge before the release.
Again, if you don't have the time I can look into this.
Michal
Hi Michal, I've been busy at work lately with little or no spare time for my OSS work :-( I've started on this last week but have been dragged away. I'll try to send the patches by tomorrow morning, which I should manage given I won't run into any difficulties. Regards, Dawid

On Mon, 2017-07-31 at 11:31 -0400, Dawid Zamirski wrote:
On Mon, 2017-07-31 at 12:35 +0200, Michal Privoznik wrote:
Hi Michal,
I've been busy at work lately with little or no spare time for my OSS work :-( I've started on this last week but have been dragged away. I'll try to send the patches by tomorrow morning, which I should manage given I won't run into any difficulties.
Regards, Dawid
Hi Michal, I've finally posted the patches here: https://www.redhat.com/archives/libvir-list/2017-August/msg00046.html Regards, Dawid
participants (4)
-
Dawid Zamirski
-
Dawid Zamirski
-
Michal Privoznik
-
Neal Gompa