[libvirt] [libvirt-php][PATCH] Fix virConnectClose failed issue on list_domains

This patch for libvirt-php Pre-4.2 version. I checked with libvirt 0.8.8 version. I fixed irConnectClose failed issue on list_domains. And web example codes updated for 0.8.8 API. This issue is: $domains = libvirt_list_domains($conn); PHP Warning: main(): virConnectClose failed with 1 on destructor: (null) And if ssh connect to remote host then some netcat processes remain on remote host. # ps axu|grep libvirt|grep 'nc -U' apache 27867 0.0 0.0 7332 368 ? Ss 12:09 0:00 nc -U /var/run/libvirt/libvirt-sock apache 27871 0.0 0.0 7332 372 ? Ss 12:09 0:00 nc -U /var/run/libvirt/libvirt-sock apache 27875 0.0 0.0 7332 368 ? Ss 12:09 0:00 nc -U /var/run/libvirt/libvirt-sock Thanks for libvirt-php. Regards, == Yukihiro Kawada diff --git a/examples/libvirt.php b/examples/libvirt.php index 10e7e38..8a54acd 100644 --- a/examples/libvirt.php +++ b/examples/libvirt.php @@ -22,7 +22,7 @@ } function get_hostname() { - return libvirt_get_hostname($this->conn); + return libvirt_connect_get_hostname($this->conn); } function get_domain_object($nameRes) { @@ -178,30 +178,23 @@ } function get_uri() { - return libvirt_get_uri($this->conn); + return libvirt_connect_get_uri($this->conn); } function get_domain_count() { - $ac = libvirt_get_active_domain_count($this->conn); - $ic = libvirt_get_inactive_domain_count($this->conn); - $tc = libvirt_get_domain_count($this->conn); - - return array( - 'active' => $ac, - 'inactive' => $ic, - 'total' => $tc - ); + return libvirt_domain_get_counts($this->conn); } function get_domains() { $domNames = array(); $doms = libvirt_list_domains($this->conn); - foreach ($doms as $dom) { - $tmp = libvirt_domain_get_uuid_string($dom); - $domNames[$tmp] = libvirt_domain_get_name($dom); + foreach ($doms as $nam) { + $dom = libvirt_domain_lookup_by_name($this->conn, $nam); + $tmp = libvirt_domain_get_uuid_string($dom); + $domNames[$tmp] = $nam; } - + unset($doms); ksort($domNames); return $domNames; } @@ -316,7 +309,7 @@ $ret = array(); if ($name != false) { - $dom=libvirt_domain_lookup_by_name($this->conn, $name); + $dom = libvirt_domain_lookup_by_name($this->conn, $name); if (!$dom) return false; diff --git a/src/libvirt-php.cb/src/libvirt-php.c index 5e77994..f5a927b 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -3467,7 +3467,7 @@ PHP_FUNCTION(libvirt_list_domains) int *ids; char **names; const char *name; - int i; + int i, rv; virDomainPtr domain=NULL; GET_CONNECTION_FROM_ARGS("r",&zconn); @@ -3490,6 +3490,12 @@ PHP_FUNCTION(libvirt_list_domains) } efree(ids); + rv = virDomainFree (domain); // Y.Kawada + if (rv != 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING,"virDomainFree failed with %i on list_domain: %s", rv, LIBVIRT_G (last_error)); + } + domain = NULL; + expectedcount=virConnectNumOfDefinedDomains (conn->conn); names=emalloc(expectedcount*sizeof(char *)); count=virConnectListDefinedDomains (conn->conn,names ,expectedcount);

sorry. fixed a little. Regards, Yukihiro Kawada diff --git a/examples/libvirt.php b/examples/libvirt.php index 10e7e38..8a54acd 100644 --- a/examples/libvirt.php +++ b/examples/libvirt.php @@ -22,7 +22,7 @@ } function get_hostname() { - return libvirt_get_hostname($this->conn); + return libvirt_connect_get_hostname($this->conn); } function get_domain_object($nameRes) { @@ -178,30 +178,23 @@ } function get_uri() { - return libvirt_get_uri($this->conn); + return libvirt_connect_get_uri($this->conn); } function get_domain_count() { - $ac = libvirt_get_active_domain_count($this->conn); - $ic = libvirt_get_inactive_domain_count($this->conn); - $tc = libvirt_get_domain_count($this->conn); - - return array( - 'active' => $ac, - 'inactive' => $ic, - 'total' => $tc - ); + return libvirt_domain_get_counts($this->conn); } function get_domains() { $domNames = array(); $doms = libvirt_list_domains($this->conn); - foreach ($doms as $dom) { - $tmp = libvirt_domain_get_uuid_string($dom); - $domNames[$tmp] = libvirt_domain_get_name($dom); + foreach ($doms as $nam) { + $dom = libvirt_domain_lookup_by_name($this->conn, $nam); + $tmp = libvirt_domain_get_uuid_string($dom); + $domNames[$tmp] = $nam; } - + unset($doms); ksort($domNames); return $domNames; } @@ -316,7 +309,7 @@ $ret = array(); if ($name != false) { - $dom=libvirt_domain_lookup_by_name($this->conn, $name); + $dom = libvirt_domain_lookup_by_name($this->conn, $name); if (!$dom) return false; diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 5e77994..fbd910b 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -3467,7 +3467,7 @@ PHP_FUNCTION(libvirt_list_domains) int *ids; char **names; const char *name; - int i; + int i, rv; virDomainPtr domain=NULL; GET_CONNECTION_FROM_ARGS("r",&zconn); @@ -3490,6 +3490,14 @@ PHP_FUNCTION(libvirt_list_domains) } efree(ids); + if (domain != NULL) { + rv = virDomainFree (domain); // Y.Kawada + if (rv != 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING,"virDomainFree failed with %i on list_domain: %s", rv, LIBVIRT_G (last_error)); + } + domain = NULL; + } + expectedcount=virConnectNumOfDefinedDomains (conn->conn); names=emalloc(expectedcount*sizeof(char *)); count=virConnectListDefinedDomains (conn->conn,names ,expectedcount);

[snip]
+ if (domain != NULL) { + rv = virDomainFree (domain); // Y.Kawada + if (rv != 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING,"virDomainFree failed with %i on list_domain: %s", rv, LIBVIRT_G (last_error)); + } + domain = NULL; + }
Well, this is not the right place to place it. You should place it to the end of the for cycle that's getting the domain pointers to free the domains once they're not needed anymore. This has been fixed and pushed to the repository stating you as the authors however :) Also, the example PHP scripts have been merged from the local webserver directory I used for development so please pull the latest contents from git and before you write any other patch please pull the current tree from the repository using `git pull` command on the cloned master branch of the repository. Your version of the patch was not working for me since it returned "Unknown error" in my Google Chrome window every time I tried to libvirt_list_domains() function. I fixed it to put it to the right place and pushed now. Thanks for the patch, Michal -- Michal Novotny <minovotn@redhat.com>, RHCE Virtualization Team (xen userspace), Red Hat
participants (3)
-
Michal Novotny
-
warp kawada
-
warp.kawada@gmail.com