[libvirt] [libvirt-php 0/2] Introduce set_error() function and fix compilation warnings

Hi, this is the patch to introduce set_error() function to be able to set error from everywhere we want (and not just using the libvirt error handler), e.g. when the API function is not available in the libvirt version the user is using. Also, the formatting and error handling has been fixed. Second part of the patch is fixing the compilation warnings. Patch has been tested on Fedora 14 box using php 5.3.4-1 version of Fedora 14 and it was working as designed. Michal Signed-off-by: Michal Novotny <minovotn@redhat.com> Michal Novotny (2): Introduce set_error() function and fix error handling/formatting Fix compilation warnings libvirt.c | 382 ++++++++++++++++++++++++++++++++------------------------- php_libvirt.h | 4 - 2 files changed, 212 insertions(+), 174 deletions(-) -- 1.7.3.2

Hi, this is the patch to switch error reporting facility to set_error() function which is having the original code. This is useful to set error from every place we need instead of setting it just by libvirt error handler. This was mainly done to bail with error in the case you call libvirt function that's not supported by your version of libvirt (using LIBVIR_VERSION_NUMBER constant). Signed-off-by: Michal Novotny <minovotn@redhat.com> --- libvirt.c | 279 ++++++++++++++++++++++++++++++++------------------------ php_libvirt.h | 4 - 2 files changed, 159 insertions(+), 124 deletions(-) diff --git a/libvirt.c b/libvirt.c index 2926813..ebd009a 100644 --- a/libvirt.c +++ b/libvirt.c @@ -12,7 +12,7 @@ #define VIR_NETWORKS_ACTIVE 1 #define VIR_NETWORKS_INACTIVE 2 -//----------------- ZEND thread safe per request globals definition +/* ZEND thread safe per request globals definition */ int le_libvirt_connection; int le_libvirt_domain; int le_libvirt_storagepool; @@ -22,7 +22,6 @@ int le_libvirt_nodedev; ZEND_DECLARE_MODULE_GLOBALS(libvirt) -//static ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_connect, 0, 0, 0) ZEND_ARG_INFO(0, url) ZEND_ARG_INFO(0, readonly) @@ -60,18 +59,14 @@ static function_entry libvirt_functions[] = { PHP_FE(libvirt_domain_define_xml, NULL) PHP_FE(libvirt_domain_create_xml, NULL) PHP_FE(libvirt_domain_memory_peek,NULL) -#if LIBVIR_VERSION_NUMBER>=7005 PHP_FE(libvirt_domain_memory_stats,NULL) -#endif PHP_FE(libvirt_domain_block_stats,NULL) PHP_FE(libvirt_domain_interface_stats,NULL) PHP_FE(libvirt_version,NULL) PHP_FE(libvirt_domain_get_connect, NULL) PHP_FE(libvirt_domain_migrate, NULL) PHP_FE(libvirt_domain_migrate_to_uri, NULL) -#if LIBVIR_VERSION_NUMBER>=7007 PHP_FE(libvirt_domain_get_job_info, NULL) -#endif PHP_FE(libvirt_domain_xml_xpath, NULL) PHP_FE(libvirt_domain_get_block_info, NULL) PHP_FE(libvirt_domain_get_network_info, NULL) @@ -121,32 +116,32 @@ zend_module_entry libvirt_module_entry = { ZEND_GET_MODULE(libvirt) #endif -//----------------- PHP init options +/* PHP init options */ PHP_INI_BEGIN() -STD_PHP_INI_ENTRY("libvirt.longlong_to_string", "1", PHP_INI_ALL, OnUpdateBool, longlong_to_string_ini, zend_libvirt_globals, libvirt_globals) + STD_PHP_INI_ENTRY("libvirt.longlong_to_string", "1", PHP_INI_ALL, OnUpdateBool, longlong_to_string_ini, zend_libvirt_globals, libvirt_globals) PHP_INI_END() -//PHP requires to have this function defined +/* PHP requires to have this function defined */ static void php_libvirt_init_globals(zend_libvirt_globals *libvirt_globals) { libvirt_globals->longlong_to_string_ini = 1; } -//PHP request initialization +/* PHP request initialization */ PHP_RINIT_FUNCTION(libvirt) { LIBVIRT_G (last_error)=NULL; return SUCCESS; } -//PHP request destruction +/* PHP request destruction */ PHP_RSHUTDOWN_FUNCTION(libvirt) { if (LIBVIRT_G (last_error)!=NULL) efree(LIBVIRT_G (last_error)); return SUCCESS; } -//phpinfo() +/* Information function for phpinfo() */ PHP_MINFO_FUNCTION(libvirt) { unsigned long libVer; @@ -166,16 +161,22 @@ PHP_MINFO_FUNCTION(libvirt) php_info_print_table_end(); } -//calback error for receiving errors from libvirt. Pass them to PHP and stores for last_error function -catch_error(void *userData, virErrorPtr error) +/* Function to set the error message and pass it to PHP */ +void set_error(char *msg) { - php_error_docref(NULL TSRMLS_CC, E_WARNING,"%s",error->message); + php_error_docref(NULL TSRMLS_CC, E_WARNING,"%s",msg); if (LIBVIRT_G (last_error)!=NULL) efree(LIBVIRT_G (last_error)); - LIBVIRT_G (last_error)=estrndup(error->message,strlen(error->message)); + LIBVIRT_G (last_error)=estrndup(msg,strlen(msg)); +} + +/* Error handler for receiving libvirt errors */ +catch_error(void *userData, virErrorPtr error) +{ + set_error(error->message); } -//Destructor for connection resource +/* Destructor for connection resource */ static void php_libvirt_connection_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) { php_libvirt_connection *conn = (php_libvirt_connection*)rsrc->ptr; @@ -186,7 +187,7 @@ static void php_libvirt_connection_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) conn->conn=NULL; } -//Destructor for domain resource +/* Destructor for domain resource */ static void php_libvirt_domain_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) { php_libvirt_domain *domain = (php_libvirt_domain*)rsrc->ptr; @@ -196,7 +197,7 @@ static void php_libvirt_domain_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) domain->domain=NULL; } -//Destructor for storagepool resource +/* Destructor for storagepool resource */ static void php_libvirt_storagepool_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) { php_libvirt_storagepool *pool = (php_libvirt_storagepool*)rsrc->ptr; @@ -211,7 +212,7 @@ static void php_libvirt_storagepool_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) } } -//Destructor for volume resource +/* Destructor for volume resource */ static void php_libvirt_volume_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) { php_libvirt_volume *volume = (php_libvirt_volume*)rsrc->ptr; @@ -226,7 +227,7 @@ static void php_libvirt_volume_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) } } -//Destructor for network resource +/* Destructor for network resource */ static void php_libvirt_network_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) { php_libvirt_network *network = (php_libvirt_network*)rsrc->ptr; @@ -241,7 +242,7 @@ static void php_libvirt_network_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) } } -//Destructor for nodedev resource +/* Destructor for nodedev resource */ static void php_libvirt_nodedev_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) { php_libvirt_nodedev *nodedev = (php_libvirt_nodedev*)rsrc->ptr; @@ -256,7 +257,7 @@ static void php_libvirt_nodedev_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) } } -//ZEND Module inicialization function +/* ZEND Module inicialization function */ PHP_MINIT_FUNCTION(libvirt) { le_libvirt_connection = zend_register_list_destructors_ex(php_libvirt_connection_dtor, NULL, PHP_LIBVIRT_CONNECTION_RES_NAME, module_number); @@ -270,69 +271,97 @@ PHP_MINIT_FUNCTION(libvirt) ZEND_INIT_MODULE_GLOBALS(libvirt, php_libvirt_init_globals, NULL); /* LIBVIRT CONSTANTS */ - REGISTER_LONG_CONSTANT("VIR_DOMAIN_XML_SECURE", 1, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_DOMAIN_XML_INACTIVE", 2, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_DOMAIN_NOSTATE", 0, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_DOMAIN_RUNNING", 1, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_DOMAIN_BLOCKED", 2, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_DOMAIN_PAUSED", 3, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_DOMAIN_SHUTDOWN", 4, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_DOMAIN_SHUTOFF", 5, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_DOMAIN_CRASHED", 6, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_MEMORY_VIRTUAL ", 1, CONST_CS | CONST_PERSISTENT); - /* Network constants */ - REGISTER_LONG_CONSTANT("VIR_NETWORKS_ACTIVE", VIR_NETWORKS_ACTIVE, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_NETWORKS_INACTIVE", VIR_NETWORKS_INACTIVE, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_NETWORKS_ALL", VIR_NETWORKS_ACTIVE | VIR_NETWORKS_INACTIVE, CONST_CS | CONST_PERSISTENT); - - REGISTER_LONG_CONSTANT("VIR_CRED_USERNAME",1, CONST_CS | CONST_PERSISTENT);// : Identity to act as - REGISTER_LONG_CONSTANT("VIR_CRED_AUTHNAME",2,CONST_CS | CONST_PERSISTENT);// : Identify to authorize as - REGISTER_LONG_CONSTANT("VIR_CRED_LANGUAGE",3, CONST_CS | CONST_PERSISTENT);// : RFC 1766 languages, comma separated - REGISTER_LONG_CONSTANT("VIR_CRED_CNONCE",4 , CONST_CS | CONST_PERSISTENT);//: client supplies a nonce - REGISTER_LONG_CONSTANT("VIR_CRED_PASSPHRASE",5, CONST_CS | CONST_PERSISTENT);// : Passphrase secret - REGISTER_LONG_CONSTANT("VIR_CRED_ECHOPROMPT",6, CONST_CS | CONST_PERSISTENT);// : Challenge response - REGISTER_LONG_CONSTANT("VIR_CRED_NOECHOPROMP",7, CONST_CS | CONST_PERSISTENT);// : Challenge response - REGISTER_LONG_CONSTANT("VIR_CRED_REALM",8, CONST_CS | CONST_PERSISTENT);// : Authentication realm - REGISTER_LONG_CONSTANT("VIR_CRED_EXTERNAL",9, CONST_CS | CONST_PERSISTENT);// : Externally managed credential More may be added - expect the unexpected + /* XML contants */ + REGISTER_LONG_CONSTANT("VIR_DOMAIN_XML_SECURE", 1, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_XML_INACTIVE", 2, CONST_CS | CONST_PERSISTENT); + + /* Domain constants */ + REGISTER_LONG_CONSTANT("VIR_DOMAIN_NOSTATE", 0, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_RUNNING", 1, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_BLOCKED", 2, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_PAUSED", 3, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_SHUTDOWN", 4, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_SHUTOFF", 5, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_CRASHED", 6, CONST_CS | CONST_PERSISTENT); + /* Memory constants */ + REGISTER_LONG_CONSTANT("VIR_MEMORY_VIRTUAL", 1, CONST_CS | CONST_PERSISTENT); + + /* Network constants */ + REGISTER_LONG_CONSTANT("VIR_NETWORKS_ACTIVE", VIR_NETWORKS_ACTIVE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_NETWORKS_INACTIVE", VIR_NETWORKS_INACTIVE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_NETWORKS_ALL", VIR_NETWORKS_ACTIVE | + VIR_NETWORKS_INACTIVE, CONST_CS | CONST_PERSISTENT); + + /* Credential constants */ + REGISTER_LONG_CONSTANT("VIR_CRED_USERNAME", 1, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_CRED_AUTHNAME", 2, CONST_CS | CONST_PERSISTENT); + /* RFC 1766 languages */ + REGISTER_LONG_CONSTANT("VIR_CRED_LANGUAGE", 3, CONST_CS | CONST_PERSISTENT); + /* Client supplied a nonce */ + REGISTER_LONG_CONSTANT("VIR_CRED_CNONCE", 4, CONST_CS | CONST_PERSISTENT); + /* Passphrase secret */ + REGISTER_LONG_CONSTANT("VIR_CRED_PASSPHRASE", 5, CONST_CS | CONST_PERSISTENT); + /* Challenge response */ + REGISTER_LONG_CONSTANT("VIR_CRED_ECHOPROMPT", 6, CONST_CS | CONST_PERSISTENT); + /* Challenge responce */ + REGISTER_LONG_CONSTANT("VIR_CRED_NOECHOPROMP", 7, CONST_CS | CONST_PERSISTENT); + /* Authentication realm */ + REGISTER_LONG_CONSTANT("VIR_CRED_REALM", 8, CONST_CS | CONST_PERSISTENT); + /* Externally managed credential More may be added - expect the unexpected */ + REGISTER_LONG_CONSTANT("VIR_CRED_EXTERNAL", 9, CONST_CS | CONST_PERSISTENT); + + /* Domain memory constants */ /* The total amount of memory written out to swap space (in kB). */ - REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_SWAP_IN",0,CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_SWAP_IN", 0, CONST_CS | CONST_PERSISTENT); /* Page faults occur when a process makes a valid access to virtual memory that is not available. */ /* When servicing the page fault, if disk IO is * required, it is considered a major fault. If not, */ /* it is a minor fault. * These are expressed as the number of faults that have occurred. */ - REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_SWAP_OUT",1, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_MAJOR_FAULT",2, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_SWAP_OUT", 1, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_MAJOR_FAULT", 2, CONST_CS | CONST_PERSISTENT); /* The amount of memory left completely unused by the system. Memory that is available but used for */ /* reclaimable caches should NOT be reported as free. This value is expressed in kB. */ - REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_MINOR_FAULT",3,CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_MINOR_FAULT", 3, CONST_CS | CONST_PERSISTENT); /* The total amount of usable memory as seen by the domain. This value * may be less than the amount */ /* of memory assigned to the domain if a * balloon driver is in use or if the guest OS does not initialize */ /* all * assigned pages. This value is expressed in kB. */ - REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_UNUSED",4,CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_UNUSED", 4, CONST_CS | CONST_PERSISTENT); /* The number of statistics supported by this version of the interface. To add new statistics, add them */ /* to the enum and increase this value. */ - REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_AVAILABLE",5,CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_NR",6,CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_AVAILABLE", 5, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_DOMAIN_MEMORY_STAT_NR", 6, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_NONE",0,CONST_CS | CONST_PERSISTENT); // No job is active - REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_BOUNDED",1,CONST_CS | CONST_PERSISTENT); //Job with a finite completion time - REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_UNBOUNDED",2,CONST_CS | CONST_PERSISTENT); // Job without a finite completion time - REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_COMPLETED", 3,CONST_CS | CONST_PERSISTENT); // Job has finished, but isn't cleaned up - REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_FAILED",4,CONST_CS | CONST_PERSISTENT); //Job hit error, but isn't cleaned up - REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_CANCELLED",5,CONST_CS | CONST_PERSISTENT); // Job was aborted, but isn't cleaned up - - REGISTER_LONG_CONSTANT("VIR_MIGRATE_LIVE",1,CONST_CS | CONST_PERSISTENT); // live migration + /* Job constants */ + REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_NONE", 0, CONST_CS | CONST_PERSISTENT); + /* Job with a finite completion time */ + REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_BOUNDED", 1, CONST_CS | CONST_PERSISTENT); + /* Job without a finite completion time */ + REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_UNBOUNDED", 2, CONST_CS | CONST_PERSISTENT); + /* Job has finished but it's not cleaned up yet */ + REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_COMPLETED", 3, CONST_CS | CONST_PERSISTENT); + /* Job hit error but it's not cleaned up yet */ + REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_FAILED", 4, CONST_CS | CONST_PERSISTENT); + /* Job was aborted but it's not cleanup up yet */ + REGISTER_LONG_CONSTANT("VIR_DOMAIN_JOB_CANCELLED", 5, CONST_CS | CONST_PERSISTENT); + + /* Migration constants */ + REGISTER_LONG_CONSTANT("VIR_MIGRATE_LIVE", 1, CONST_CS | CONST_PERSISTENT); /* direct source -> dest host control channel Note the less-common spelling that we're stuck with: */ /* VIR_MIGRATE_TUNNELLED should be VIR_MIGRATE_TUNNELED */ - REGISTER_LONG_CONSTANT("VIR_MIGRATE_PEER2PEER",2,CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("VIR_MIGRATE_TUNNELLED",4,CONST_CS | CONST_PERSISTENT); // tunnel migration data over libvirtd connection - REGISTER_LONG_CONSTANT("VIR_MIGRATE_PERSIST_DEST",8,CONST_CS | CONST_PERSISTENT); // persist the VM on the destination - REGISTER_LONG_CONSTANT("VIR_MIGRATE_UNDEFINE_SOURCE",16,CONST_CS | CONST_PERSISTENT); // undefine the VM on the source - REGISTER_LONG_CONSTANT("VIR_MIGRATE_PAUSED",32,CONST_CS | CONST_PERSISTENT); // pause on remote side - REGISTER_LONG_CONSTANT("VIR_MIGRATE_NON_SHARED_DISK",64,CONST_CS | CONST_PERSISTENT); // migration with non-shared storage with full disk copy + REGISTER_LONG_CONSTANT("VIR_MIGRATE_PEER2PEER", 2, CONST_CS | CONST_PERSISTENT); + /* tunnel migration data over libvirtd connection */ + REGISTER_LONG_CONSTANT("VIR_MIGRATE_TUNNELLED", 4, CONST_CS | CONST_PERSISTENT); + /* persist the VM on the destination */ + REGISTER_LONG_CONSTANT("VIR_MIGRATE_PERSIST_DEST", 8, CONST_CS | CONST_PERSISTENT); + /* undefine the VM on the source */ + REGISTER_LONG_CONSTANT("VIR_MIGRATE_UNDEFINE_SOURCE", 16, CONST_CS | CONST_PERSISTENT); + /* pause on remote side */ + REGISTER_LONG_CONSTANT("VIR_MIGRATE_PAUSED", 32, CONST_CS | CONST_PERSISTENT); + /* migration with non-shared storage with full disk copy */ + REGISTER_LONG_CONSTANT("VIR_MIGRATE_NON_SHARED_DISK", 64, CONST_CS | CONST_PERSISTENT); /* migration with non-shared storage with incremental copy (same base image shared between source and destination) */ - REGISTER_LONG_CONSTANT("VIR_MIGRATE_NON_SHARED_INC",128,CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("VIR_MIGRATE_NON_SHARED_INC", 128, CONST_CS | CONST_PERSISTENT); REGISTER_INI_ENTRIES(); @@ -343,17 +372,17 @@ PHP_MINIT_FUNCTION(libvirt) return SUCCESS; } -// Zend module destruction +/* Zend module destruction */ PHP_MSHUTDOWN_FUNCTION(libvirt) { UNREGISTER_INI_ENTRIES(); - //return error callback back to default (outouts to STDOUT) + /* return error callback back to default (outouts to STDOUT) */ virSetErrorFunc(NULL, NULL); return SUCCESS; } -//Macros for obtaining resources from arguments +/* Macros for obtaining resources from arguments */ #define GET_CONNECTION_FROM_ARGS(args, ...) \ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_ARGS__) == FAILURE) {\ RETURN_FALSE;\ @@ -430,7 +459,7 @@ str_out = estrndup(str_in, strlen(str_in)); \ add_index_long(out, key,in); \ } -//Authentication callback function. Should receive list of credentials via cbdata and pass the requested one to libvirt +/* Authentication callback function. Should receive list of credentials via cbdata and pass the requested one to libvirt */ static int libvirt_virConnectAuthCallback(virConnectCredentialPtr cred, unsigned int ncred, void *cbdata) { int i,j; @@ -497,33 +526,31 @@ PHP_FUNCTION(libvirt_connect) if (libVer<6002) { - php_error_docref(NULL TSRMLS_CC, E_WARNING,"Only libvirt 0.6.2 and higher supported. Please upgrade your libvirt."); - if (LIBVIRT_G (last_error)!=NULL) efree(LIBVIRT_G (last_error)); - LIBVIRT_G (last_error)=estrdup("Only libvirt 0.6.2 and higher supported. Please upgrade your libvirt."); + set_error("Only libvirt 0.6.2 and higher supported. Please upgrade your libvirt"); RETURN_FALSE; } - // If 'null' value has been passed as URL override url to NULL value to autodetect the hypervisor + /* If 'null' value has been passed as URL override url to NULL value to autodetect the hypervisor */ if (strcasecmp(url, "NULL") == 0) url = NULL; conn=emalloc(sizeof(php_libvirt_connection)); if (zcreds==NULL) - { //connecting without providing authentication + { /* connecting without providing authentication */ if (readonly) conn->conn = virConnectOpenReadOnly(url); else conn->conn = virConnectOpen(url); } else - { //connecting with authentication (using callback) + { /* connecting with authentication (using callback) */ arr_hash = Z_ARRVAL_P(zcreds); array_count = zend_hash_num_elements(arr_hash); credscount=array_count; creds=emalloc(credscount*sizeof(php_libvirt_cred_value)); j=0; - //parse the input Array and create list of credentials. The list (array) is passed to callback function. + /* parse the input Array and create list of credentials. The list (array) is passed to callback function. */ for (zend_hash_internal_pointer_reset_ex(arr_hash, &pointer); zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS; zend_hash_move_forward_ex(arr_hash, &pointer)) { @@ -1356,6 +1383,11 @@ PHP_FUNCTION(libvirt_domain_memory_stats) LONGLONG_INDEX(return_value, stats[i].tag,stats[i].val) } } +#else +PHP_FUNCTION(libvirt_domain_memory_stats) +{ + set_error("Only libvirt 0.7.5 and higher supports getting the job information"); +} #endif PHP_FUNCTION(libvirt_domain_block_stats) @@ -1494,19 +1526,19 @@ PHP_FUNCTION(libvirt_domain_get_network_info) { /* Get XML for the domain */ xml=virDomainGetXMLDesc(domain->domain, VIR_DOMAIN_XML_INACTIVE); if (xml==NULL) { - LIBVIRT_G (last_error)=estrdup("Cannot get domain XML"); + set_error("Cannot get domain XML"); RETURN_FALSE; } snprintf(fnpath, sizeof(fnpath), "//domain/devices/interface[@type='network']/mac[@address='%s']/../source/@network", mac); tmp = get_string_from_xpath(xml, fnpath, NULL, &retval); if (tmp == NULL) { - LIBVIRT_G (last_error)=estrdup("Invalid XPath node for source network"); + set_error("Invalid XPath node for source network"); RETURN_FALSE; } if (retval < 0) { - LIBVIRT_G (last_error)=estrdup("Cannot get XPath expression result for network source"); + set_error("Cannot get XPath expression result for network source"); RETURN_FALSE; } @@ -1621,7 +1653,7 @@ PHP_FUNCTION(libvirt_nodedev_get) GET_CONNECTION_FROM_ARGS("rs",&zconn,&name,&name_len); if ((dev = virNodeDeviceLookupByName(conn->conn, name)) == NULL) { - LIBVIRT_G (last_error)=estrdup("Cannot get find requested node device"); + set_error("Cannot get find requested node device"); RETURN_FALSE; } @@ -1669,7 +1701,7 @@ PHP_FUNCTION(libvirt_nodedev_get_xml) xml=virNodeDeviceGetXMLDesc(nodedev->device, 0); if ( xml == NULL ) { - LIBVIRT_G(last_error) = estrdup("Cannot get the device XML information"); + set_error("Cannot get the device XML information"); RETURN_FALSE; } @@ -1689,7 +1721,7 @@ PHP_FUNCTION(libvirt_nodedev_get_information) xml=virNodeDeviceGetXMLDesc(nodedev->device, 0); if ( xml == NULL ) { - LIBVIRT_G(last_error) = estrdup("Cannot get the device XML information"); + set_error("Cannot get the device XML information"); RETURN_FALSE; } @@ -1698,12 +1730,12 @@ PHP_FUNCTION(libvirt_nodedev_get_information) /* Get name */ tmp = get_string_from_xpath(xml, "//device/name", NULL, &retval); if (tmp == NULL) { - LIBVIRT_G (last_error)=estrdup("Invalid XPath node for device name"); + set_error("Invalid XPath node for device name"); RETURN_FALSE; } if (retval < 0) { - LIBVIRT_G (last_error)=estrdup("Cannot get XPath expression result for device name"); + set_error("Cannot get XPath expression result for device name"); RETURN_FALSE; } @@ -1812,7 +1844,7 @@ PHP_FUNCTION(libvirt_network_get) GET_CONNECTION_FROM_ARGS("rs",&zconn,&name,&name_len); if ((net = virNetworkLookupByName(conn->conn, name)) == NULL) { - LIBVIRT_G (last_error)=estrdup("Cannot get find requested network"); + set_error("Cannot get find requested network"); RETURN_FALSE; } @@ -1834,7 +1866,7 @@ PHP_FUNCTION(libvirt_network_get_bridge) name = virNetworkGetBridgeName(network->network); if (name == NULL) { - LIBVIRT_G (last_error)=estrdup("Cannot get network bridge name"); + set_error("Cannot get network bridge name"); RETURN_FALSE; } @@ -1852,7 +1884,7 @@ PHP_FUNCTION(libvirt_network_get_active) res = virNetworkIsActive(network->network); if (res == -1) { - LIBVIRT_G (last_error)=estrdup("Error getting virtual network state"); + set_error("Error getting virtual network state"); RETURN_FALSE; } @@ -1939,7 +1971,7 @@ PHP_FUNCTION(libvirt_network_get_information) xml=virNetworkGetXMLDesc(network->network, 0); if (xml==NULL) { - LIBVIRT_G (last_error)=estrdup("Cannot get network XML"); + set_error("Cannot get network XML"); RETURN_FALSE; } @@ -1948,12 +1980,12 @@ PHP_FUNCTION(libvirt_network_get_information) /* Get name */ tmp = get_string_from_xpath(xml, "//network/name", NULL, &retval); if (tmp == NULL) { - LIBVIRT_G (last_error)=estrdup("Invalid XPath node for network name"); + set_error("Invalid XPath node for network name"); RETURN_FALSE; } if (retval < 0) { - LIBVIRT_G (last_error)=estrdup("Cannot get XPath expression result for network name"); + set_error("Cannot get XPath expression result for network name"); RETURN_FALSE; } @@ -1962,12 +1994,12 @@ PHP_FUNCTION(libvirt_network_get_information) /* Get gateway IP address */ tmp = get_string_from_xpath(xml, "//network/ip/@address", NULL, &retval); if (tmp == NULL) { - LIBVIRT_G (last_error)=estrdup("Invalid XPath node for network gateway IP address"); + set_error("Invalid XPath node for network gateway IP address"); RETURN_FALSE; } if (retval < 0) { - LIBVIRT_G (last_error)=estrdup("Cannot get XPath expression result for network gateway IP address"); + set_error("Cannot get XPath expression result for network gateway IP address"); RETURN_FALSE; } @@ -1976,12 +2008,12 @@ PHP_FUNCTION(libvirt_network_get_information) /* Get netmask */ tmp2 = get_string_from_xpath(xml, "//network/ip/@netmask", NULL, &retval); if (tmp2 == NULL) { - LIBVIRT_G (last_error)=estrdup("Invalid XPath node for network mask"); + set_error("Invalid XPath node for network mask"); RETURN_FALSE; } if (retval < 0) { - LIBVIRT_G (last_error)=estrdup("Cannot get XPath expression result for network mask"); + set_error("Cannot get XPath expression result for network mask"); RETURN_FALSE; } @@ -2025,23 +2057,27 @@ PHP_FUNCTION(libvirt_network_set_active) GET_NETWORK_FROM_ARGS("rl",&znetwork,&act); if ((act != 0) && (act != 1)) { - LIBVIRT_G (last_error)=estrdup("Invalid network activity state"); + set_error("Invalid network activity state"); RETURN_FALSE; } if (act == 1) { if (virNetworkCreate(network->network) == 0) { + /* Network is up and running */ RETURN_TRUE; } else { + /* We don't have to set error since it's caught by libvirt error handler itself */ RETURN_FALSE; } } if (virNetworkDestroy(network->network) == 0) { + /* Network is down */ RETURN_TRUE; } else { + /* Caught by libvirt error handler too */ RETURN_FALSE; } } @@ -2057,19 +2093,15 @@ PHP_FUNCTION(libvirt_network_get_xml) xml=virNetworkGetXMLDesc(network->network, 0); if (xml==NULL) { - LIBVIRT_G (last_error)=estrdup("Cannot get network XML"); + set_error("Cannot get network XML"); RETURN_FALSE; } RETURN_STRING(xml, 1); } +#if LIBVIR_VERSION_NUMBER>=8000 PHP_FUNCTION(libvirt_domain_get_block_info) { - #if LIBVIR_VERSION_NUMBER<8000 - LIBVIRT_G (last_error)=estrdup("Only libvirt 0.8.0 and higher supports getting the block info."); - RETURN_FALSE - #endif - php_libvirt_domain *domain=NULL; zval *zdomain; int retval, retval2; @@ -2087,7 +2119,7 @@ PHP_FUNCTION(libvirt_domain_get_block_info) { /* Get XML for the domain */ xml=virDomainGetXMLDesc(domain->domain, VIR_DOMAIN_XML_INACTIVE); if (xml==NULL) { - LIBVIRT_G (last_error)=estrdup("Cannot get domain XML"); + set_error("Cannot get domain XML"); RETURN_FALSE; } @@ -2096,7 +2128,7 @@ PHP_FUNCTION(libvirt_domain_get_block_info) { tmp = get_string_from_xpath(xml, fnpath, NULL, &retval); if (retval < 0) { - LIBVIRT_G (last_error)=estrdup("Cannot get XPath expression result for device storage"); + set_error("Cannot get XPath expression result for device storage"); RETURN_FALSE; } @@ -2104,21 +2136,21 @@ PHP_FUNCTION(libvirt_domain_get_block_info) { snprintf(fnpath, sizeof(fnpath), "//domain/devices/disk/target[@dev='%s']/../source/@file", dev); tmp = get_string_from_xpath(xml, fnpath, NULL, &retval); if (retval < 0) { - LIBVIRT_G (last_error)=estrdup("Cannot get XPath expression result for file storage"); + set_error("Cannot get XPath expression result for file storage"); RETURN_FALSE; } isFile = 1; } if (retval == 0) { - LIBVIRT_G (last_error)=estrdup("No relevant node found"); + set_error("No relevant node found"); RETURN_FALSE; } retval=virDomainGetBlockInfo(domain->domain, tmp, &info,0); if (retval == -1) { - LIBVIRT_G (last_error)=estrdup("Cannot get domain block information"); + set_error("Cannot get domain block information"); RETURN_FALSE; } @@ -2140,6 +2172,13 @@ PHP_FUNCTION(libvirt_domain_get_block_info) { LONGLONG_ASSOC(return_value, "allocation", info.allocation); LONGLONG_ASSOC(return_value, "physical", info.physical); } +#else +PHP_FUNCTION(libvirt_domain_get_block_info) +{ + set_error("Only libvirt 0.8.0 and higher supports getting the block information"); + RETURN_FALSE; +} +#endif PHP_FUNCTION(libvirt_domain_xml_xpath) { php_libvirt_domain *domain=NULL; @@ -2161,12 +2200,12 @@ PHP_FUNCTION(libvirt_domain_xml_xpath) { free(xml); - if (rc == 0) - RETURN_FALSE; + if (rc == 0) + RETURN_FALSE; add_assoc_string(return_value, "xpath", (long)zpath, 1); if (rc < 0) - add_assoc_long(return_value, "error_code", (long)rc); + add_assoc_long(return_value, "error_code", (long)rc); } PHP_FUNCTION(libvirt_domain_interface_stats) @@ -2211,16 +2250,13 @@ PHP_FUNCTION(libvirt_version) if (virGetVersion(&libVer,type,&typeVer) != 0) RETURN_FALSE; - //major * 1,000,000 + minor * 1,000 + release. + /* The version is returned as: major * 1,000,000 + minor * 1,000 + release. */ array_init(return_value); add_assoc_long(return_value, "libvirt.release",(long)(libVer %1000)); add_assoc_long(return_value, "libvirt.minor",(long)((libVer/1000) % 1000)); add_assoc_long(return_value, "libvirt.major",(long)((libVer/1000000) % 1000)); - if (type != NULL) - add_assoc_string(return_value, "type.type", (long)type, 1); - add_assoc_string(return_value, "connector.version", (long)PHP_LIBVIRT_WORLD_VERSION, 1); add_assoc_long(return_value, "type.release",(long)(typeVer %1000)); add_assoc_long(return_value, "type.minor",(long)((typeVer/1000) % 1000)); @@ -2229,7 +2265,6 @@ PHP_FUNCTION(libvirt_version) PHP_FUNCTION(libvirt_domain_get_connect) { - php_libvirt_domain *domain=NULL; zval *zdomain; php_libvirt_connection *conn; @@ -2241,8 +2276,6 @@ PHP_FUNCTION(libvirt_domain_get_connect) RETURN_RESOURCE(conn->resource_id); } - - PHP_FUNCTION(libvirt_domain_migrate_to_uri) { php_libvirt_domain *domain=NULL; @@ -2335,4 +2368,10 @@ PHP_FUNCTION(libvirt_domain_get_job_info) LONGLONG_ASSOC(return_value, "file_processed", jobinfo.fileProcessed); LONGLONG_ASSOC(return_value, "file_remaining", jobinfo.fileRemaining); } +#else +PHP_FUNCTION(libvirt_domain_get_job_info) +{ + set_error("Only libvirt 0.7.7 and higher supports getting the job information"); + RETURN_FALSE; +} #endif diff --git a/php_libvirt.h b/php_libvirt.h index 3311263..054df46 100644 --- a/php_libvirt.h +++ b/php_libvirt.h @@ -104,17 +104,13 @@ PHP_FUNCTION(libvirt_domain_reboot); PHP_FUNCTION(libvirt_domain_define_xml); PHP_FUNCTION(libvirt_domain_create_xml); PHP_FUNCTION(libvirt_domain_memory_peek); -#if LIBVIR_VERSION_NUMBER>=7005 PHP_FUNCTION(libvirt_domain_memory_stats); -#endif PHP_FUNCTION(libvirt_domain_block_stats); PHP_FUNCTION(libvirt_domain_interface_stats); PHP_FUNCTION(libvirt_version); PHP_FUNCTION(libvirt_domain_get_connect); PHP_FUNCTION(libvirt_domain_migrate); -#if LIBVIR_VERSION_NUMBER>=7007 PHP_FUNCTION(libvirt_domain_get_job_info); -#endif PHP_FUNCTION(libvirt_domain_xml_xpath); PHP_FUNCTION(libvirt_domain_get_block_info); PHP_FUNCTION(libvirt_domain_get_network_info); -- 1.7.3.2

Hi, this is the patch to fix the compilation warnings like: libvirt.c: In function ‘zif_libvirt_node_get_info’: libvirt.c:613:2: warning: passing argument 4 of ‘add_assoc_string_ex’ makes pointer from integer without a cast /usr/include/php/Zend/zend_API.h:360:45: note: expected ‘char *’ but argument is of type ‘long int’ libvirt.c: In function ‘zif_libvirt_domain_get_name’: libvirt.c:1081:6: warning: assignment discards qualifiers from pointer target type With this patch applied I was unable to see any warning to the number warnings dropped to zero on my Fedora-14 box. Michal Signed-off-by: Michal Novotny <minovotn@redhat.com> --- libvirt.c | 105 +++++++++++++++++++++++++++++++----------------------------- 1 files changed, 54 insertions(+), 51 deletions(-) diff --git a/libvirt.c b/libvirt.c index ebd009a..693443c 100644 --- a/libvirt.c +++ b/libvirt.c @@ -170,7 +170,8 @@ void set_error(char *msg) } /* Error handler for receiving libvirt errors */ -catch_error(void *userData, virErrorPtr error) +static void catch_error(void *userData ATTRIBUTE_UNUSED, + virErrorPtr error) { set_error(error->message); } @@ -442,7 +443,7 @@ str_out = estrndup(str_in, strlen(str_in)); \ #define LONGLONG_ASSOC(out,key,in) \ if (LIBVIRT_G(longlong_to_string_ini)) { \ snprintf(tmpnumber,63,"%llu",in); \ - add_assoc_string(out,key,tmpnumber,1); \ + add_assoc_string_ex(out,key,strlen(key)+1,tmpnumber,1); \ } \ else \ { \ @@ -637,7 +638,7 @@ PHP_FUNCTION(libvirt_node_get_info) if (retval==-1) RETURN_FALSE; array_init(return_value); - add_assoc_string(return_value, "model", (long)info.model,1); + add_assoc_string_ex(return_value, "model", 6, info.model, 1); add_assoc_long(return_value, "memory", (long)info.memory); add_assoc_long(return_value, "cpus", (long)info.cpus); add_assoc_long(return_value, "nodes", (long)info.nodes); @@ -1100,11 +1101,14 @@ PHP_FUNCTION(libvirt_domain_get_name) { php_libvirt_domain *domain=NULL; zval *zdomain; - char *name=NULL; + const char *name=NULL; char *name_out; GET_DOMAIN_FROM_ARGS("r",&zdomain); + if (domain->domain == NULL) + RETURN_FALSE; + name=virDomainGetName(domain->domain); if (name==NULL) RETURN_FALSE; @@ -1486,7 +1490,7 @@ char *get_string_from_xpath(char *xml, char *xpath, zval **val, int *retVal) value = (char *)xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1); snprintf(key, sizeof(key), "%d", i); - add_assoc_string(*val, key, (long)value, 1); + add_assoc_string_ex(*val, key, strlen(key)+1, value, 1); ret++; } } @@ -1543,15 +1547,15 @@ PHP_FUNCTION(libvirt_domain_get_network_info) { } array_init(return_value); - add_assoc_string(return_value, "mac", (long)mac, 1); - add_assoc_string(return_value, "network", (long)tmp, 1); + add_assoc_string_ex(return_value, "mac", 4, mac, 1); + add_assoc_string_ex(return_value, "network", 8, tmp, 1); snprintf(fnpath, sizeof(fnpath), "//domain/devices/interface[@type='network']/mac[@address='%s']/../model/@type", mac); tmp = get_string_from_xpath(xml, fnpath, NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "nic_type", (long)tmp, 1); + add_assoc_string_ex(return_value, "nic_type", 9, tmp, 1); else - add_assoc_string(return_value, "nic_type", (long)"default", 1); + add_assoc_string_ex(return_value, "nic_type", 9, "default", 1); } PHP_FUNCTION(libvirt_list_networks) @@ -1631,11 +1635,6 @@ PHP_FUNCTION(libvirt_list_nodedevs) free(names[i]); } -/* - if (cap != NULL) - add_assoc_string(return_value, "capability", (long)cap, 1); -*/ - efree(names); } @@ -1739,95 +1738,95 @@ PHP_FUNCTION(libvirt_nodedev_get_information) RETURN_FALSE; } - add_assoc_string(return_value, "name", (long)tmp, 1); + add_assoc_string_ex(return_value, "name", 5, tmp, 1); /* Get parent name */ tmp = get_string_from_xpath(xml, "//device/parent", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "parent", (long)tmp, 1); + add_assoc_string_ex(return_value, "parent", 7, tmp, 1); /* Get capability */ cap = get_string_from_xpath(xml, "//device/capability/@type", NULL, &retval); if ((cap != NULL) && (retval > 0)) - add_assoc_string(return_value, "capability", (long)cap, 1); + add_assoc_string_ex(return_value, "capability", 11, cap, 1); /* System capability is having hardware and firmware sub-blocks */ if (strcmp(cap, "system") == 0) { /* Get hardware vendor */ tmp = get_string_from_xpath(xml, "//device/capability/hardware/vendor", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "hardware_vendor", (long)tmp, 1); + add_assoc_string_ex(return_value, "hardware_vendor", 16, tmp, 1); /* Get hardware version */ tmp = get_string_from_xpath(xml, "//device/capability/hardware/version", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "hardware_version", (long)tmp, 1); + add_assoc_string_ex(return_value, "hardware_version", 17, tmp, 1); /* Get hardware serial */ tmp = get_string_from_xpath(xml, "//device/capability/hardware/serial", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "hardware_serial", (long)tmp, 1); + add_assoc_string_ex(return_value, "hardware_serial", 16, tmp, 1); /* Get hardware UUID */ tmp = get_string_from_xpath(xml, "//device/capability/hardware/uuid", NULL, &retval); if (tmp != NULL) - add_assoc_string(return_value, "hardware_uuid", (long)tmp, 1); + add_assoc_string_ex(return_value, "hardware_uuid", 15, tmp, 1); /* Get firmware vendor */ tmp = get_string_from_xpath(xml, "//device/capability/firmware/vendor", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "firmware_vendor", (long)tmp, 1); + add_assoc_string_ex(return_value, "firmware_vendor", 16, tmp, 1); /* Get firmware version */ tmp = get_string_from_xpath(xml, "//device/capability/firmware/version", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "firmware_version", (long)tmp, 1); + add_assoc_string_ex(return_value, "firmware_version", 17, tmp, 1); /* Get firmware release date */ tmp = get_string_from_xpath(xml, "//device/capability/firmware/release_date", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "firmware_release_date", (long)tmp, 1); + add_assoc_string_ex(return_value, "firmware_release_date", 22, tmp, 1); } /* Get product_id */ tmp = get_string_from_xpath(xml, "//device/capability/product/@id", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "product_id", (long)tmp, 1); + add_assoc_string_ex(return_value, "product_id", 11, tmp, 1); /* Get product_name */ tmp = get_string_from_xpath(xml, "//device/capability/product", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "product_name", (long)tmp, 1); + add_assoc_string_ex(return_value, "product_name", 13, tmp, 1); /* Get vendor_id */ tmp = get_string_from_xpath(xml, "//device/capability/vendor/@id", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "vendor_id", (long)tmp, 1); + add_assoc_string_ex(return_value, "vendor_id", 10, tmp, 1); /* Get vendor_name */ tmp = get_string_from_xpath(xml, "//device/capability/vendor", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "vendor_name", (long)tmp, 1); + add_assoc_string_ex(return_value, "vendor_name", 12, tmp, 1); /* Get driver name */ tmp = get_string_from_xpath(xml, "//device/driver/name", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "driver_name", (long)tmp, 1); + add_assoc_string_ex(return_value, "driver_name", 12, tmp, 1); /* Get driver name */ tmp = get_string_from_xpath(xml, "//device/capability/interface", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "interface_name", (long)tmp, 1); + add_assoc_string_ex(return_value, "interface_name", 15, tmp, 1); /* Get driver name */ tmp = get_string_from_xpath(xml, "//device/capability/address", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "address", (long)tmp, 1); + add_assoc_string_ex(return_value, "address", 8, tmp, 1); /* Get driver name */ tmp = get_string_from_xpath(xml, "//device/capability/capability/@type", NULL, &retval); if ((tmp != NULL) && (retval > 0)) - add_assoc_string(return_value, "capabilities", (long)tmp, 1); + add_assoc_string_ex(return_value, "capabilities", 11, tmp, 1); } PHP_FUNCTION(libvirt_network_get) @@ -1928,7 +1927,8 @@ int get_subnet_bits(char *ip) char tmp[4] = { 0 }; int i, part = 0, ii = 0, skip = 0; unsigned long long retval = 0; - char binary[64] = { 0 }; + char *binary; + int maxBits = 64; for (i = 0; i < strlen(ip); i++) { if (ip[i] == '.') { @@ -1943,7 +1943,8 @@ int get_subnet_bits(char *ip) } retval += (atoi(tmp) * pow(256, 3 - part)); - dec2bin(retval, &binary); + binary = (char *)malloc( maxBits * sizeof(char) ); + dec2bin(retval, binary); for (i = 0; i < strlen(binary); i++) { if ((binary[i] != '1') && (binary[i] != '0')) @@ -1952,6 +1953,7 @@ int get_subnet_bits(char *ip) if (binary[i] != '1') break; } + free(binary); return i - skip; } @@ -1989,7 +1991,7 @@ PHP_FUNCTION(libvirt_network_get_information) RETURN_FALSE; } - add_assoc_string(return_value, "name", (long)tmp, 1); + add_assoc_string_ex(return_value, "name", 5, tmp, 1); /* Get gateway IP address */ tmp = get_string_from_xpath(xml, "//network/ip/@address", NULL, &retval); @@ -2003,7 +2005,7 @@ PHP_FUNCTION(libvirt_network_get_information) RETURN_FALSE; } - add_assoc_string(return_value, "ip", (long)tmp, 1); + add_assoc_string_ex(return_value, "ip", 3, tmp, 1); /* Get netmask */ tmp2 = get_string_from_xpath(xml, "//network/ip/@netmask", NULL, &retval); @@ -2017,34 +2019,34 @@ PHP_FUNCTION(libvirt_network_get_information) RETURN_FALSE; } - add_assoc_string(return_value, "netmask", (long)tmp2, 1); + add_assoc_string_ex(return_value, "netmask", 8, tmp2, 1); add_assoc_long(return_value, "netmask_bits", (long)get_subnet_bits(tmp2)); /* Format CIDR address representation */ tmp[strlen(tmp) - 1] = tmp[strlen(tmp) - 1] - 1; snprintf(fixedtemp, sizeof(fixedtemp), "%s/%d", tmp, get_subnet_bits(tmp2)); - add_assoc_string(return_value, "ip_range", fixedtemp, 1); + add_assoc_string_ex(return_value, "ip_range", 9, fixedtemp, 1); /* Get forwarding settings */ tmp = get_string_from_xpath(xml, "//network/forward/@mode", NULL, &retval); if ((tmp == NULL) || (retval < 0)) - add_assoc_string(return_value, "forwarding", (long)"None", 1); + add_assoc_string_ex(return_value, "forwarding", 11, "None", 1); else - add_assoc_string(return_value, "forwarding", (long)tmp, 1); + add_assoc_string_ex(return_value, "forwarding", 11, tmp, 1); /* Get forwarding settings */ tmp = get_string_from_xpath(xml, "//network/forward/@dev", NULL, &retval); if ((tmp == NULL) || (retval < 0)) - add_assoc_string(return_value, "forward_dev", (long)"any interface", 1); + add_assoc_string_ex(return_value, "forward_dev", 12, "any interface", 1); else - add_assoc_string(return_value, "forward_dev", (long)tmp, 1); + add_assoc_string_ex(return_value, "forward_dev", 12, tmp, 1); /* Get DHCP values */ tmp = get_string_from_xpath(xml, "//network/ip/dhcp/range/@start", NULL, &retval); tmp2 = get_string_from_xpath(xml, "//network/ip/dhcp/range/@end", NULL, &retval); if ((retval > 0) && (tmp != NULL) && (tmp2 != NULL)) { - add_assoc_string(return_value, "dhcp_start", (long)tmp, 1); - add_assoc_string(return_value, "dhcp_end", (long)tmp2, 1); + add_assoc_string_ex(return_value, "dhcp_start", 11, tmp, 1); + add_assoc_string_ex(return_value, "dhcp_end", 9, tmp2, 1); } } @@ -2156,17 +2158,17 @@ PHP_FUNCTION(libvirt_domain_get_block_info) { array_init(return_value); LONGLONG_INIT - add_assoc_string(return_value, "device", (long)dev, 1); + add_assoc_string_ex(return_value, "device", 7, dev, 1); if (isFile) - add_assoc_string(return_value, "file", (long)tmp, 1); + add_assoc_string_ex(return_value, "file", 5, tmp, 1); else - add_assoc_string(return_value, "partition", (long)tmp, 1); + add_assoc_string_ex(return_value, "partition", 10, tmp, 1); snprintf(fnpath, sizeof(fnpath), "//domain/devices/disk/target[@dev='%s']/../driver/@type", dev); tmp = get_string_from_xpath(xml, fnpath, NULL, &retval); if (tmp != NULL) - add_assoc_string(return_value, "type", (long)tmp, 1); + add_assoc_string_ex(return_value, "type", 5, tmp, 1); LONGLONG_ASSOC(return_value, "capacity", info.capacity); LONGLONG_ASSOC(return_value, "allocation", info.allocation); @@ -2186,7 +2188,8 @@ PHP_FUNCTION(libvirt_domain_xml_xpath) { zval *zpath; char *xml; char *xml_out; - long path_len=-1, flags = 0, rc = 0; + long path_len=-1, flags = 0; + int rc = 0; GET_DOMAIN_FROM_ARGS("rs|l",&zdomain, &zpath, &path_len, &flags); @@ -2203,7 +2206,7 @@ PHP_FUNCTION(libvirt_domain_xml_xpath) { if (rc == 0) RETURN_FALSE; - add_assoc_string(return_value, "xpath", (long)zpath, 1); + add_assoc_string_ex(return_value, "xpath", 6, (char *)zpath, 1); if (rc < 0) add_assoc_long(return_value, "error_code", (long)rc); } @@ -2257,7 +2260,7 @@ PHP_FUNCTION(libvirt_version) add_assoc_long(return_value, "libvirt.minor",(long)((libVer/1000) % 1000)); add_assoc_long(return_value, "libvirt.major",(long)((libVer/1000000) % 1000)); - add_assoc_string(return_value, "connector.version", (long)PHP_LIBVIRT_WORLD_VERSION, 1); + add_assoc_string_ex(return_value, "connector.version", 18, PHP_LIBVIRT_WORLD_VERSION, 1); add_assoc_long(return_value, "type.release",(long)(typeVer %1000)); add_assoc_long(return_value, "type.minor",(long)((typeVer/1000) % 1000)); add_assoc_long(return_value, "type.major",(long)((typeVer/1000000) % 1000)); -- 1.7.3.2

On Wed, Feb 09, 2011 at 02:11:40PM +0100, Michal Novotny wrote:
Hi, this is the patch to fix the compilation warnings like:
libvirt.c: In function ‘zif_libvirt_node_get_info’: libvirt.c:613:2: warning: passing argument 4 of ‘add_assoc_string_ex’ makes pointer from integer without a cast /usr/include/php/Zend/zend_API.h:360:45: note: expected ‘char *’ but argument is of type ‘long int’ libvirt.c: In function ‘zif_libvirt_domain_get_name’: libvirt.c:1081:6: warning: assignment discards qualifiers from pointer target type
With this patch applied I was unable to see any warning to the number warnings dropped to zero on my Fedora-14 box.
Cool, thanks ! I'm not sure I should ACK I guess you don't have to be too formal :-) Daniel -- Daniel Veillard | libxml Gnome XML XSLT toolkit http://xmlsoft.org/ daniel@veillard.com | Rpmfind RPM search engine http://rpmfind.net/ http://veillard.com/ | virtualization library http://libvirt.org/

On 02/09/2011 02:57 PM, Daniel Veillard wrote:
On Wed, Feb 09, 2011 at 02:11:40PM +0100, Michal Novotny wrote:
Hi, this is the patch to fix the compilation warnings like:
libvirt.c: In function ‘zif_libvirt_node_get_info’: libvirt.c:613:2: warning: passing argument 4 of ‘add_assoc_string_ex’ makes pointer from integer without a cast /usr/include/php/Zend/zend_API.h:360:45: note: expected ‘char *’ but argument is of type ‘long int’ libvirt.c: In function ‘zif_libvirt_domain_get_name’: libvirt.c:1081:6: warning: assignment discards qualifiers from pointer target type
With this patch applied I was unable to see any warning to the number warnings dropped to zero on my Fedora-14 box. Cool, thanks ! I'm not sure I should ACK I guess you don't have to be too formal :-)
Daniel
Well, I think it's good to review it and ACK :) To leave just one ACK either explicitly or wait few days to auto-ACK it. The main reason for posting this to the list is to allow engineers to explicitly NACK it if it doesn't make any sense. However you ACKed it now so I'll commit it to the repository now ;) Michal -- Michal Novotny<minovotn@redhat.com>, RHCE Virtualization Team (xen userspace), Red Hat
participants (2)
-
Daniel Veillard
-
Michal Novotny