From nobody Mon Apr 29 23:55:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501624185986799.4450728655546; Tue, 1 Aug 2017 14:49:45 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 06EAA757A3; Tue, 1 Aug 2017 21:49:44 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CD0006375F; Tue, 1 Aug 2017 21:49:43 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 88022180B467; Tue, 1 Aug 2017 21:49:43 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkLfx007824 for ; Tue, 1 Aug 2017 17:46:21 -0400 Received: by smtp.corp.redhat.com (Postfix) id 27A945D6AA; Tue, 1 Aug 2017 21:46:21 +0000 (UTC) Received: from mx1.redhat.com (ext-mx03.extmail.prod.ext.phx2.redhat.com [10.5.110.27]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 95FA217483 for ; Tue, 1 Aug 2017 21:46:18 +0000 (UTC) Received: from mail-qk0-f193.google.com (mail-qk0-f193.google.com [209.85.220.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 41A6316AB0F for ; Tue, 1 Aug 2017 21:46:17 +0000 (UTC) Received: by mail-qk0-f193.google.com with SMTP id q66so2919415qki.1 for ; Tue, 01 Aug 2017 14:46:17 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.15 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:15 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 06EAA757A3 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 06EAA757A3 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZCyuGLWD" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 41A6316AB0F Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 41A6316AB0F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=cjF4QoWxY+RBVX5QzywK9XxtkVfZbo0cAKQMe7QbAks=; b=ZCyuGLWDTY8xuSmDZwtWgZ7HBhJ4Nc6NsmsiylT5JRmWc//+1y6CPD770tFfWWFfAS JcoiWx+elIInddYnt98FhglYbV9IbW7N0oKbpVW/4b0XAYoBB+7WAeWqikW1yiJJWbok eNsGNJKwNJcLkTMMWsVijP0TMjd3SbMNAAdCGPz93FPOYJnS4qrQ2uvnillxV275h39Q 6AF12iYlXbkyyRBmvWXB9Hj2uMO9EV6Io0UW1/mglCTF7K66RNxarbXlHqVsCBu9ApKv mKkVpdb7bb4UnCJ2nNROdsfbrz0/j8t6pqQrA9godWfpspmlYOeDXCDpd6X2OjZjDTkI ocWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=cjF4QoWxY+RBVX5QzywK9XxtkVfZbo0cAKQMe7QbAks=; b=hecoGCBySHUKLmG6u9qn/UOqpsyWb1BiHPheCxDesfu8I+JquMjz/pABC3awFyxaLg lAIBZDDj6RcUgc4aoF9BnHCwc3uK4iIwgoKDuUY5wqDrm1W8eHyD4bX9+LgmUu6/Og9u wYoqV8IPRfwnvVU8Ocm/B8EArQXnBtGYREXLmMNGHVZdXQ9gBnbQg1LW3eF7S3dRO79v U2RC7GTAM2VAlY+D1sNdngLZY3AlDmohSkz23sJwSowinTqO1vaCOxm4z/viF5WIx5bj T0MjED5suA3CVMW0pnBptBitiv8fpXf5kjnGgc25XMj7Yi3CLOOsZVNrK+19M7Zniqj2 b7OQ== X-Gm-Message-State: AIVw112D4Tgsj6M3QO86s39m6+4bVMCvvIGI9ZTlwR4P1rm3DsZ0u+XL bAwYn+PHLJnE1FuYZdE= X-Received: by 10.55.168.140 with SMTP id r134mr29343685qke.204.1501623976056; Tue, 01 Aug 2017 14:46:16 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:02 -0400 Message-Id: <20170801214614.8915-2-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 01 Aug 2017 21:46:17 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 01 Aug 2017 21:46:17 +0000 (UTC) for IP:'209.85.220.193' DOMAIN:'mail-qk0-f193.google.com' HELO:'mail-qk0-f193.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.37 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.220.193 mail-qk0-f193.google.com 209.85.220.193 mail-qk0-f193.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.27 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 01/13] Move PHP version compat macros to utils.h X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 01 Aug 2017 21:49:44 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" --- src/libvirt-php.h | 144 ++++---------------------------------------------- src/util.h | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 164 insertions(+), 134 deletions(-) diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 7962e33..867eb60 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -119,139 +119,6 @@ typedef uint64_t arch_uint; #define UINTx PRIx64 #endif =20 -#if PHP_MAJOR_VERSION >=3D 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) - -#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_RESOURCE_HANDLE(_resource) \ - Z_RES_P(_resource) - -#define VIRT_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \ - if ((_state =3D (_type)zend_fetch_resource(Z_RES_P(*_zval), _name, _le= )) =3D=3D 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 =3D zend_hash_get_current_data_ex(_ht, &_pos)) !=3D 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 =3D zend_hash_get_current_key_ex(_ht, &tmp_key_info, &_idx,= &_pos); \ - _info.name =3D ZSTR_VAL(tmp_key_info); \ - _info.length =3D ZSTR_LEN(tmp_key_info); \ - } while(0) - -#define VIRT_ARRAY_INIT(_name) do { \ - zval z##_name; \ - _name =3D &z##_name; \ - array_init(_name); \ - } while(0) - -#else /* PHP_MAJOR_VERSION < 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) - -#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_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); - -#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 =3D &_zv; \ - for (zend_hash_internal_pointer_reset_ex(_ht, &_pos); \ - zend_hash_get_current_data_ex(_ht, (void **) &pzv, &_pos) =3D=3D = SUCCESS; \ - zend_hash_move_forward_ex(_ht, &_pos)) { \ - _zv =3D *pzv; - -#define VIRT_FOREACH_END(_dummy) \ - }} - -#define VIRT_HASH_CURRENT_KEY_INFO(_ht, _pos, _idx, _info) \ - do { \ - _info.type =3D zend_hash_get_current_key_ex(_ht, &_info.name, &_info.l= ength, &_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 { char **tokens; int numTokens; @@ -315,6 +182,16 @@ typedef struct tVMNetwork { char *model; } tVMNetwork; =20 +/* TODO: Temporary due to code movement to util.h - will be removed when + * libvirt-connection.h is separated out + */ +#if PHP_MAJOR_VERSION >=3D 7 +typedef zend_resource virt_resource; +typedef virt_resource *virt_resource_handle; +#else +typedef long virt_resource_handle; +#endif + /* Libvirt-php types */ typedef struct _php_libvirt_connection { virConnectPtr conn; @@ -376,7 +253,6 @@ typedef struct _php_libvirt_hash_key_info { =20 /* Private definitions */ int set_logfile(char *filename, long maxsize TSRMLS_DC); -char *get_datetime(void); char *get_string_from_xpath(char *xml, char *xpath, zval **val, int *retVa= l); char **get_array_from_xpath(char *xml, char *xpath, int *num); =20 diff --git a/src/util.h b/src/util.h index c2b7324..533505d 100644 --- a/src/util.h +++ b/src/util.h @@ -11,6 +11,7 @@ # define __UTIL_H__ =20 # include +# include "libvirt-php.h" =20 # define DEBUG_SUPPORT =20 @@ -65,6 +66,159 @@ ((uint32_t)var[2] << 8) + \ ((uint32_t)var[3])) =20 +# if PHP_MAJOR_VERSION >=3D 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) + +# 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_RESOURCE_HANDLE(_resource) \ + Z_RES_P(_resource) + +# define VIRT_FETCH_RESOURCE(_state, _type, _zval, _name, _le) \ + if ((_state =3D (_type)zend_fetch_resource(Z_RES_P(*_zval), _name, _le= )) =3D=3D 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 =3D zend_hash_get_current_data_ex(_ht, &_pos)) !=3D 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 =3D zend_hash_get_current_key_ex(_ht, &tmp_key_info, &_idx,= &_pos); \ + _info.name =3D ZSTR_VAL(tmp_key_info); \ + _info.length =3D ZSTR_LEN(tmp_key_info); \ + } while(0) + +# define VIRT_ARRAY_INIT(_name) do { \ + zval z##_name; \ + _name =3D &z##_name; \ + array_init(_name); \ + } while(0) + +# else /* PHP_MAJOR_VERSION < 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) + +# 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_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); + +# 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 =3D &_zv; \ + for (zend_hash_internal_pointer_reset_ex(_ht, &_pos); \ + zend_hash_get_current_data_ex(_ht, (void **) &pzv, &_pos) =3D=3D = SUCCESS; \ + zend_hash_move_forward_ex(_ht, &_pos)) { \ + _zv =3D *pzv; + +# define VIRT_FOREACH_END(_dummy) \ + }} + +# define VIRT_HASH_CURRENT_KEY_INFO(_ht, _pos, _idx, _info) \ + do { \ + _info.type =3D zend_hash_get_current_key_ex(_ht, &_info.name, &_info.l= ength, &_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 */ + +# ifndef PHP_FE_END +# define PHP_FE_END {NULL, NULL, NULL} +# endif + void debugPrint(const char *source, const char *fmt, ...); =20 --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 23:55:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501624207730703.7329436943772; Tue, 1 Aug 2017 14:50:07 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3D67AA710; Tue, 1 Aug 2017 21:50:05 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E52B177D49; Tue, 1 Aug 2017 21:50:04 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 8DCFB1800C8D; Tue, 1 Aug 2017 21:50:04 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkLp1007829 for ; Tue, 1 Aug 2017 17:46:21 -0400 Received: by smtp.corp.redhat.com (Postfix) id 9E99463767; Tue, 1 Aug 2017 21:46:21 +0000 (UTC) Received: from mx1.redhat.com (ext-mx01.extmail.prod.ext.phx2.redhat.com [10.5.110.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 971E34DA95 for ; Tue, 1 Aug 2017 21:46:19 +0000 (UTC) Received: from mail-qk0-f196.google.com (mail-qk0-f196.google.com [209.85.220.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C0CD6116B15 for ; Tue, 1 Aug 2017 21:46:17 +0000 (UTC) Received: by mail-qk0-f196.google.com with SMTP id d136so2903121qkg.3 for ; Tue, 01 Aug 2017 14:46:17 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.16 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:16 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3D67AA710 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 3D67AA710 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dDUB6QDG" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C0CD6116B15 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com C0CD6116B15 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=d+8jElTqgZcY0509Ds/5z6ik0u3bkguWg/wVhDeWxBs=; b=dDUB6QDGAsOywiBkyUJEt4GG48batgHm7t3WNrCkCFIGv8UjrnHQaXmMYEjRwxaanA +7EnY/BwS2BTBhlKNhMxZxkHY8wJYlmzOh9b4IdA7iG3vQehLQ7prQFKmntj+5s3PlP3 XiMXkGx1uLksXp3d3jQN6S0NJCjaPJkiOdLR4hXOqHcNYjpKUsvE6YoOOgjiASfLnzpm Bv2x0VYvIAGEaM0ty15mk6M//Q6iysChGPTkg0P4ENscwFq5xL4evnQTn3zsS1Pd7dMH g7wT5d591cT5wkHYguZKTSzy+uCl3xq2KDszBB5TUe/oT0qBymQMWZriA7FxjPwD74tV WBwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=d+8jElTqgZcY0509Ds/5z6ik0u3bkguWg/wVhDeWxBs=; b=mcXd0IOP18TK8PwCnAzzyOdwuJNgv6nJ96Zd648k8TrTZRQU6jcsjc9bGcOUZocgnX N+gSFg3U/JaEB5ee2iymCYhX/Y444DfCphP7ateJg+EN+gQIA62azbYi9TaTwsbsrwb2 btPU3WSKi4aCQRm5CZDpKR0+gIeJrRlExas/jvyOHKyPvEPj2Sg1pWySZ+phMTfLIbPv nkA63ESnUErll59yHT6vgcNPoASOBlURzDgy5KITRsP50bSqhrm2gPTMnhu7vcA9Bkhg Hjz8fJWOtHAL9kd4iXf58RwmqFQRRg5fdo1mvZ4g8i0QN7sXS+OBbZKeJh1KN/gpEeeb 1oxg== X-Gm-Message-State: AIVw111YX6xXYxgIXH/NKS0xpxaQg82ZY2csOUyiMrYAO+GJTPNpqIHG XrkMAAib4oL41tpFaxk= X-Received: by 10.55.40.194 with SMTP id o63mr25941413qko.310.1501623976659; Tue, 01 Aug 2017 14:46:16 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:03 -0400 Message-Id: <20170801214614.8915-3-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> MIME-Version: 1.0 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 01 Aug 2017 21:46:18 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 01 Aug 2017 21:46:18 +0000 (UTC) for IP:'209.85.220.196' DOMAIN:'mail-qk0-f196.google.com' HELO:'mail-qk0-f196.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.37 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.220.196 mail-qk0-f196.google.com 209.85.220.196 mail-qk0-f196.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.25 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 02/13] Update AUTHORS file X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 01 Aug 2017 21:50:06 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Since the project is about to get a bunch of new header files, it's much easier to maintain just the AUTHORS file. --- AUTHORS | 29 +++++++++++++++-------------- src/libvirt-php.c | 10 ---------- src/libvirt-php.h | 10 ---------- src/sockets.c | 3 --- src/sockets.h | 4 ---- src/util.c | 3 --- src/util.h | 3 --- src/vncfunc.c | 3 --- src/vncfunc.h | 4 ---- 9 files changed, 15 insertions(+), 54 deletions(-) diff --git a/AUTHORS b/AUTHORS index 9899c00..52c074d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -3,28 +3,29 @@ Libvirt-php extension =20 Libvirt-php extension is currently maintained and developed by: =20 - Michal Novotny (or ) + Michal Novotny (or ) =20 The original project, called php-libvirt, has been originally developed an= d maintained by: =20 - Radek Hladik + Radek Hladik =20 who is still contributing to the project with his patches. =20 There are also other people that have contributed to the project: =20 - David King - Jan-Paul van Burgsteden - Lyre (or <4179e1@gmail.com>) - Daniel P. Berrange - Tiziano Mueller - Yukihiro Kawada - Remi Collet - Ivo van den Abeelen - Tiziano M=C3=BCller - Pavel Odintsov - Tugdual Saunier - Stefan Kuhn + David King + Jan-Paul van Burgsteden + Lyre (or <4179e1@gmail.com>) + Daniel P. Berrange + Tiziano Mueller + Yukihiro Kawada + Remi Collet + Ivo van den Abeelen + Tiziano M=C3=BCller + Pavel Odintsov + Tugdual Saunier + Stefan Kuhn + Dawid Zamirski =20 Thanks goes to all of them with big thanks to Tugdual Saunier for various fixes and OS-X compilation support. diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 6734e3b..aea69e7 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -2,16 +2,6 @@ * libvirt-php.c: Core of the PHP bindings library/module * * See COPYING for the license of this software - * - * Written by: - * Radek Hladik - * Michal Novotny - * David King - * Jan-Paul van Burgsteden - * Lyre (or <4179e1@gmail.com>) - * Daniel P. Berrange - * Tiziano Mueller - * Yukihiro Kawada */ =20 #ifdef _MSC_VER diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 867eb60..66be53a 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -2,16 +2,6 @@ * libvirt-php.h: libvirt PHP bindings header file * * See COPYING for the license of this software - * - * Written by: - * Radek Hladik - * Michal Novotny - * David King - * Jan-Paul van Burgsteden - * Lyre (or <4179e1@gmail.com>) - * Daniel P. Berrange - * Tiziano Mueller - * Yukihiro Kawada */ =20 #ifndef PHP_LIBVIRT_H diff --git a/src/sockets.c b/src/sockets.c index 0a3e3c2..9c66257 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -2,9 +2,6 @@ * sockets.c: Socket functions for libvirt-php * * See COPYING for the license of this software - * - * Written by: - * Michal Novotny */ =20 #include diff --git a/src/sockets.h b/src/sockets.h index ce7a668..a9645a9 100644 --- a/src/sockets.h +++ b/src/sockets.h @@ -2,10 +2,6 @@ * sockets.h: Socket functions for libvirt-php * * See COPYING for the license of this software - * - * Written by: - * Michal Novotny - * Michal Privoznik */ =20 #ifndef __SOCKETS_H__ diff --git a/src/util.c b/src/util.c index 53096ae..cb8ccbe 100644 --- a/src/util.c +++ b/src/util.c @@ -2,9 +2,6 @@ * util.c: common, generic utility functions * * See COPYING for the license of this software - * - * Written by: - * Michal Privoznik */ =20 #include diff --git a/src/util.h b/src/util.h index 533505d..6400164 100644 --- a/src/util.h +++ b/src/util.h @@ -2,9 +2,6 @@ * util.h: common, generic utility functions * * See COPYING for the license of this software - * - * Written by: - * Michal Privoznik */ =20 #ifndef __UTIL_H__ diff --git a/src/vncfunc.c b/src/vncfunc.c index 45f4007..eeadf42 100644 --- a/src/vncfunc.c +++ b/src/vncfunc.c @@ -2,9 +2,6 @@ * vncfunc.c: VNC Client functions to be used for the graphical VNC consol= e of libvirt-php * * See COPYING for the license of this software - * - * Written by: - * Michal Novotny */ =20 #include diff --git a/src/vncfunc.h b/src/vncfunc.h index 4758f95..0e72234 100644 --- a/src/vncfunc.h +++ b/src/vncfunc.h @@ -2,10 +2,6 @@ * vncfunc.h: VNC Client functions to be used for the graphical VNC consol= e of libvirt-php * * See COPYING for the license of this software - * - * Written by: - * Michal Novotny - * Michal Privoznik */ =20 #ifndef __VNCFUNC_H__ --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 23:55:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 150162549640085.11041817877708; Tue, 1 Aug 2017 15:11:36 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2974F159526; Tue, 1 Aug 2017 22:11:34 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C25FE5D6AA; Tue, 1 Aug 2017 22:11:33 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 65429180B467; Tue, 1 Aug 2017 22:11:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkMLt007839 for ; Tue, 1 Aug 2017 17:46:22 -0400 Received: by smtp.corp.redhat.com (Postfix) id C8D1D60476; Tue, 1 Aug 2017 21:46:22 +0000 (UTC) Received: from mx1.redhat.com (ext-mx06.extmail.prod.ext.phx2.redhat.com [10.5.110.30]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C13BC60462 for ; Tue, 1 Aug 2017 21:46:20 +0000 (UTC) Received: from mail-qt0-f173.google.com (mail-qt0-f173.google.com [209.85.216.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1725F64E9D for ; Tue, 1 Aug 2017 21:46:19 +0000 (UTC) Received: by mail-qt0-f173.google.com with SMTP id p3so17074013qtg.2 for ; Tue, 01 Aug 2017 14:46:19 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.16 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:16 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2974F159526 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 2974F159526 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="A5KT2fsd" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1725F64E9D Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 1725F64E9D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=+p1cGaglrUe+LTUIIw5/zZ0uvPNhW+sMkNscaUH6Ox8=; b=A5KT2fsdasUhq830Us1f1ikWT2i0pPuV+QdeNDVLW54Rt9r+Ho/1KK1XEojggFVe6x YMKXaqZHakEJr1cjGjOwtiznHf7FjkUqne8fWj2PM0w6JEiSUCLdtv1SMuE166bx3uwl EQq4X4kFUKBL65umM7TX2oNY7vQ9qLhYWc5RRDHnLoRbiGqmc/awWE50RnMv4FcVEArt pmuKeuH2EmmsKds+8dGed/OFOuUvCSLdygj6x2w+FyveUuXRfsuLxCveYmwbWgBRRRcR Fpy8GVbqGrJAiVp/JqFhLv3vvUUvU+hRx/tJORNUVBcrbMhYUi2SGBMIoF4YTODfElF2 SFCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=+p1cGaglrUe+LTUIIw5/zZ0uvPNhW+sMkNscaUH6Ox8=; b=KC6EcIWAblwo9imh5gwrOXYNRWBY1jL+eezoPteFqkLhjr6AR+gBvVYFxqL8Y4o3Qa t8OMTQwVssV+lXCapNItG94FCjYxnJ9abktilO51pQYE8Q5D0o2S6DYFbWrjJRYvpdIT JWv9vds4G3Cv+B1n7S+M9YYeg7Uue9OrYruY4zqWkMnImUUXvcVQ2acsW16/ZXlMdGKT Lc1eqNkpsi9X79pyZAY2QmkwpSvdBt2Ni6qtlwoFz733iab2tIH2g2jdmXZW08SLdytA y9nk9PzI/Fn5OIWeCV7oObANRb93B4taQy3GSheTRVCZzEvNHJP4lJ4WFIRAzvTs4VGq i1fQ== X-Gm-Message-State: AIVw111LXPzZEitLOYpmoRzrPmUfK7kTWp44R6d3ORuogp9h5B1racFh PE1c9jrjCxAJKP7enYk= X-Received: by 10.200.3.43 with SMTP id q43mr30758523qtg.274.1501623977643; Tue, 01 Aug 2017 14:46:17 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:04 -0400 Message-Id: <20170801214614.8915-4-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 01 Aug 2017 21:46:19 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 01 Aug 2017 21:46:19 +0000 (UTC) for IP:'209.85.216.173' DOMAIN:'mail-qt0-f173.google.com' HELO:'mail-qt0-f173.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: -0.43 (DCC_REPUT_00_12, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.216.173 mail-qt0-f173.google.com 209.85.216.173 mail-qt0-f173.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.30 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 03/13] Do not reuse PHPFUNC macro definition X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 01 Aug 2017 22:11:35 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The sockets.c and vncfunc.c should have their own function macros as they are "helper" functions not directly related to PHP bindings themselves. This is simple s/PHPFUNC/SOCKETFUNC/ and s/PHPFUNC/VNCFUNC/ --- src/sockets.c | 44 +++++++++---------- src/vncfunc.c | 138 +++++++++++++++++++++++++++++-------------------------= ---- 2 files changed, 91 insertions(+), 91 deletions(-) diff --git a/src/sockets.c b/src/sockets.c index 9c66257..ef9d154 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -26,7 +26,7 @@ DEBUG_INIT("sockets"); =20 /* Function macro */ -#define PHPFUNC __FUNCTION__ +#define SOCKETFUNC __FUNCTION__ =20 /* * Private function name: connect_socket @@ -59,7 +59,7 @@ int connect_socket(char *server, char *port, int keepaliv= e, int nodelay, int all server =3D strdup("localhost"); } =20 - DPRINTF("%s: Connecting to %s:%s\n", PHPFUNC, server, port); + DPRINTF("%s: Connecting to %s:%s\n", SOCKETFUNC, server, port); =20 s =3D getaddrinfo(server, port, &hints, &result); if (s !=3D 0) @@ -80,18 +80,18 @@ int connect_socket(char *server, char *port, int keepal= ive, int nodelay, int all return -errno; =20 freeaddrinfo(result); - DPRINTF("%s: Socket descriptor #%d opened\n", PHPFUNC, sfd); + DPRINTF("%s: Socket descriptor #%d opened\n", SOCKETFUNC, sfd); =20 if (keepalive) { int on =3D 1; if (setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0= ) { int err =3D errno; close(sfd); - DPRINTF("%s: Cannot set keep alive option on socket\n", PHPFUN= C); + DPRINTF("%s: Cannot set keep alive option on socket\n", SOCKET= FUNC); return -err; } =20 - DPRINTF("%s: Socket #%d set as keepalive socket\n", PHPFUNC, sfd); + DPRINTF("%s: Socket #%d set as keepalive socket\n", SOCKETFUNC, sf= d); } =20 if (nodelay) { @@ -99,11 +99,11 @@ int connect_socket(char *server, char *port, int keepal= ive, int nodelay, int all if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0= ) { int err =3D errno; close(sfd); - DPRINTF("%s: Cannot set no delay option on socket\n", PHPFUNC); + DPRINTF("%s: Cannot set no delay option on socket\n", SOCKETFU= NC); return -err; } =20 - DPRINTF("%s: Socket #%d set as no delay socket\n", PHPFUNC, sfd); + DPRINTF("%s: Socket #%d set as no delay socket\n", SOCKETFUNC, sfd= ); } =20 return sfd; @@ -130,7 +130,7 @@ int socket_has_data(int sfd, long maxtime, int ignorems= g) } =20 if (!ignoremsg) - DPRINTF("%s: Checking data on socket %d, timeout =3D { %ld, %ld }\= n", PHPFUNC, sfd, + DPRINTF("%s: Checking data on socket %d, timeout =3D { %ld, %ld }\= n", SOCKETFUNC, sfd, (long)timeout.tv_sec, (long)timeout.tv_usec); =20 FD_ZERO(&fds); @@ -141,12 +141,12 @@ int socket_has_data(int sfd, long maxtime, int ignore= msg) rc =3D select( sizeof(fds), &fds, NULL, NULL, NULL); =20 if (rc=3D=3D-1) { - DPRINTF("%s: Select with error %d (%s)\n", PHPFUNC, errno, strerro= r(-errno)); + DPRINTF("%s: Select with error %d (%s)\n", SOCKETFUNC, errno, stre= rror(-errno)); return -errno; } =20 if (!ignoremsg) - DPRINTF("%s: Select returned %d\n", PHPFUNC, rc); + DPRINTF("%s: Select returned %d\n", SOCKETFUNC, rc); =20 return (rc =3D=3D 1); } @@ -165,19 +165,19 @@ void socket_read(int sfd, long length) unsigned char bigbuf[1048576]; =20 if (socket_has_data(sfd, 50000, 0) !=3D 1) { - DPRINTF("%s: No data appears to be available\n", PHPFUNC); + DPRINTF("%s: No data appears to be available\n", SOCKETFUNC); return; } =20 if (length =3D=3D -1) { - DPRINTF("%s: Reading all the data from socket\n", PHPFUNC); + DPRINTF("%s: Reading all the data from socket\n", SOCKETFUNC); while (socket_has_data(sfd, 50000, 1) =3D=3D 1) while ((len =3D read(sfd, bigbuf, sizeof(bigbuf))) =3D=3D size= of(bigbuf)) ; - DPRINTF("%s: Read done ...\n", PHPFUNC); + DPRINTF("%s: Read done ...\n", SOCKETFUNC); return; } =20 - DPRINTF("%s: Reading %ld bytes\n", PHPFUNC, length); + DPRINTF("%s: Reading %ld bytes\n", SOCKETFUNC, length); while (length > 0) { len =3D read(sfd, bigbuf, sizeof(bigbuf)); =20 @@ -188,9 +188,9 @@ void socket_read(int sfd, long length) =20 if (length && read(sfd, bigbuf, length) !=3D length) - DPRINTF("%s: not all byes read\n", PHPFUNC); + DPRINTF("%s: not all byes read\n", SOCKETFUNC); else - DPRINTF("%s: All bytes read\n", PHPFUNC); + DPRINTF("%s: All bytes read\n", SOCKETFUNC); } =20 /* @@ -218,11 +218,11 @@ int socket_read_and_save(int sfd, char *fn, long leng= th) return -EPERM; =20 if (socket_has_data(sfd, 50000, 0) !=3D 1) { - DPRINTF("%s: No data appears to be available\n", PHPFUNC); + DPRINTF("%s: No data appears to be available\n", SOCKETFUNC); return -ENOENT; } =20 - DPRINTF("%s: Reading %ld bytes\n", PHPFUNC, length); + DPRINTF("%s: Reading %ld bytes\n", SOCKETFUNC, length); while (length > 0) { len =3D read(sfd, bigbuf, sizeof(bigbuf)); =20 @@ -230,7 +230,7 @@ int socket_read_and_save(int sfd, char *fn, long length) SWAP2_BYTES_ENDIAN(1, bigbuf[i+1], bigbuf[i+2]); =20 if (write(fd, bigbuf, len) !=3D len) - DPRINTF("%s: unable to write to %d", PHPFUNC, fd); + DPRINTF("%s: unable to write to %d", SOCKETFUNC, fd); =20 length -=3D len; if (length < 0) @@ -244,14 +244,14 @@ int socket_read_and_save(int sfd, char *fn, long leng= th) SWAP2_BYTES_ENDIAN(1, bigbuf[i+1], bigbuf[i+2]); =20 if (write(fd, bigbuf, len) !=3D len) - DPRINTF("%s: unable to write to %d", PHPFUNC, fd); + DPRINTF("%s: unable to write to %d", SOCKETFUNC, fd); } =20 if (ftruncate(fd, orig_len) < 0) - DPRINTF("%s: Unable to truncate %d", PHPFUNC, fd); + DPRINTF("%s: Unable to truncate %d", SOCKETFUNC, fd); =20 close(fd); =20 - DPRINTF("%s: All bytes read\n", PHPFUNC); + DPRINTF("%s: All bytes read\n", SOCKETFUNC); return 0; } diff --git a/src/vncfunc.c b/src/vncfunc.c index eeadf42..c1430d1 100644 --- a/src/vncfunc.c +++ b/src/vncfunc.c @@ -23,7 +23,7 @@ DEBUG_INIT("vncfunc"); =20 /* Function macro */ -#define PHPFUNC __FUNCTION__ +#define VNCFUNC __FUNCTION__ =20 #define UC(a) (unsigned char)a #define CALC_UINT32(a, b, c, d) (uint32_t)((a >> 24) + (b >> 16) + (c >> 8= ) + d) @@ -93,11 +93,11 @@ vnc_write_client_version(int sfd) if (write(sfd, buf, 12) < 0) { int err =3D errno; close(sfd); - DPRINTF("%s: Write of client version failed\n", PHPFUNC); + DPRINTF("%s: Write of client version failed\n", VNCFUNC); return -err; } =20 - DPRINTF("%s: VNC Client version packet sent\n", PHPFUNC); + DPRINTF("%s: VNC Client version packet sent\n", VNCFUNC); return 0; } =20 @@ -121,7 +121,7 @@ vnc_authorize(int sfd) /* Read number security types supported */ if ((num =3D read(sfd, buf, 1)) < 0) { int err =3D errno; - DPRINTF("%s: Cannot read number of security types, error code %d (= %s)\n", PHPFUNC, err, strerror(err)); + DPRINTF("%s: Cannot read number of security types, error code %d (= %s)\n", VNCFUNC, err, strerror(err)); close(sfd); return -err; } @@ -129,7 +129,7 @@ vnc_authorize(int sfd) /* Read all the security types */ if (read(sfd, buf2, num) < 0) { int err =3D errno; - DPRINTF("%s: Read function failed with error code %d (%s)\n", PHPF= UNC, err, strerror(err)); + DPRINTF("%s: Read function failed with error code %d (%s)\n", VNCF= UNC, err, strerror(err)); close(sfd); return -err; } @@ -144,7 +144,7 @@ vnc_authorize(int sfd) /* Bail if security type None is not supported */ if (ok =3D=3D 0) { close(sfd); - DPRINTF("%s: Security type None is not supported\n", PHPFUNC); + DPRINTF("%s: Security type None is not supported\n", VNCFUNC); return -ENOTSUP; } =20 @@ -156,7 +156,7 @@ vnc_authorize(int sfd) return -err; } =20 - DPRINTF("%s: Security None selected\n", PHPFUNC); + DPRINTF("%s: Security None selected\n", VNCFUNC); =20 /* Just bogus to wait for proper auth response */ i =3D 0; @@ -164,7 +164,7 @@ vnc_authorize(int sfd) while (buf[0] + buf[1] + buf[2] + buf[3] !=3D 0) { if (read(sfd, buf, 4) < 0) { int err =3D errno; - DPRINTF("%s: Read function failed with error code %d (%s)\n", = PHPFUNC, err, strerror(err)); + DPRINTF("%s: Read function failed with error code %d (%s)\n", = VNCFUNC, err, strerror(err)); close(sfd); return -err; } @@ -174,7 +174,7 @@ vnc_authorize(int sfd) } } =20 - DPRINTF("%s: VNC Client authorized\n", PHPFUNC); + DPRINTF("%s: VNC Client authorized\n", VNCFUNC); return 0; } =20 @@ -200,13 +200,13 @@ vnc_parse_fb_params(unsigned char *buf, int len) =20 little_endian =3D (buf[6] =3D=3D 0); =20 - DPRINTF("%s: Read dimension bytes: width =3D { 0x%02x, 0x%02x }, heigh= t =3D { 0x%02x, 0x%02x }, %s endian\n", PHPFUNC, + DPRINTF("%s: Read dimension bytes: width =3D { 0x%02x, 0x%02x }, heigh= t =3D { 0x%02x, 0x%02x }, %s endian\n", VNCFUNC, w1, w2, h1, h2, little_endian ? "little" : "big"); =20 w =3D SWAP2_BY_ENDIAN(little_endian, w1, w2); h =3D SWAP2_BY_ENDIAN(little_endian, h1, h2); =20 - DPRINTF("%s: Filling the parameters structure with width =3D %d, heigh= t =3D %d\n", PHPFUNC, w, h); + DPRINTF("%s: Filling the parameters structure with width =3D %d, heigh= t =3D %d\n", VNCFUNC, w, h); =20 params.width =3D w; params.height =3D h; @@ -229,14 +229,14 @@ vnc_parse_fb_params(unsigned char *buf, int len) params.desktopNameLen =3D nlen; params.desktopName =3D (unsigned char*) strdup((char *)buf + 24); =20 - DPRINTF("%s: Desktop name set to '%s'\n", PHPFUNC, params.desktopName); + DPRINTF("%s: Desktop name set to '%s'\n", VNCFUNC, params.desktopName); =20 DPRINTF("%s: width =3D %d, height =3D %d, bpp =3D %d, depth =3D %d, bi= gEndian =3D %d, trueColor =3D %d\n", - PHPFUNC, params.width, params.height, params.bpp, params.depth= , params.bigEndian, params.trueColor); - DPRINTF("%s: maxColors =3D { %d, %d, %d }, shifts =3D { %d, %d, %d }\n= ", PHPFUNC, params.maxRed, + VNCFUNC, params.width, params.height, params.bpp, params.depth= , params.bigEndian, params.trueColor); + DPRINTF("%s: maxColors =3D { %d, %d, %d }, shifts =3D { %d, %d, %d }\n= ", VNCFUNC, params.maxRed, params.maxGreen, params.maxBlue, params.shiftRed, params.shift= Green, params.shiftBlue); =20 - DPRINTF("%s: Desktop name is '%s' (%d bytes)\n", PHPFUNC, params.deskt= opName, params.desktopNameLen); + DPRINTF("%s: Desktop name is '%s' (%d bytes)\n", VNCFUNC, params.deskt= opName, params.desktopNameLen); =20 return params; } @@ -266,18 +266,18 @@ vnc_send_key(int sfd, unsigned char key, int modifier= , int release) buf[6] =3D modifier ? 0xff : 0x00; buf[7] =3D key; =20 - DPRINTF("%s: %s key %d [0x%02x], modifier: %s\n", PHPFUNC, (release ? = "Releasing" : "Pressing"), + DPRINTF("%s: %s key %d [0x%02x], modifier: %s\n", VNCFUNC, (release ? = "Releasing" : "Pressing"), key, key, modifier ? "true" : "false"); =20 if (write(sfd, buf, 8) < 0) { int err =3D errno; DPRINTF("%s: Error occured while writing to socket descriptor #%d:= %d (%s)\n", - PHPFUNC, sfd, err, strerror(err)); + VNCFUNC, sfd, err, strerror(err)); close(sfd); return -err; } =20 - DPRINTF("%s: Write of 8 bytes successful\n", PHPFUNC); + DPRINTF("%s: Write of 8 bytes successful\n", VNCFUNC); =20 return 0; } @@ -298,7 +298,7 @@ vnc_send_client_pointer(int sfd, int clicked, int pos_x= , int pos_y) unsigned char buf[6] =3D { 0 }; =20 if (sfd < 0) { - DPRINTF("%s: Socket is not opened!\n", PHPFUNC); + DPRINTF("%s: Socket is not opened!\n", VNCFUNC); return -EINVAL; } =20 @@ -311,14 +311,14 @@ vnc_send_client_pointer(int sfd, int clicked, int pos= _x, int pos_y) =20 if (write(sfd, buf, 6) < 0) { int err =3D errno; - DPRINTF("%s: Write function failed with error code %d (%s)\n", PHP= FUNC, err, strerror(err)); + DPRINTF("%s: Write function failed with error code %d (%s)\n", VNC= FUNC, err, strerror(err)); close(sfd); return -err; } =20 =20 DPRINTF("%s: Wrote 6 bytes of client pointer event, clicked =3D %d, x = =3D { 0x%02x, 0x%02x }, y =3D { 0x%02x, 0x%02x }\n", - PHPFUNC, buf[1], buf[2], buf[3], buf[4], buf[5]); + VNCFUNC, buf[1], buf[2], buf[3], buf[4], buf[5]); return 0; } =20 @@ -336,11 +336,11 @@ vnc_set_pixel_format(int sfd, tServerFBParams params) unsigned char buf[20]; =20 if (sfd < 0) { - DPRINTF("%s: Socket is not opened!\n", PHPFUNC); + DPRINTF("%s: Socket is not opened!\n", VNCFUNC); return -EINVAL; } =20 - DPRINTF("%s: Setting up pixel format\n", PHPFUNC); + DPRINTF("%s: Setting up pixel format\n", VNCFUNC); =20 memset(buf, 0, 20); /* Message type 0 is SetPixelFormat message */ @@ -370,12 +370,12 @@ vnc_set_pixel_format(int sfd, tServerFBParams params) =20 if (write(sfd, buf, 20) < 0) { int err =3D errno; - DPRINTF("%s: Write function failed with error code %d (%s)\n", PHP= FUNC, err, strerror(err)); + DPRINTF("%s: Write function failed with error code %d (%s)\n", VNC= FUNC, err, strerror(err)); close(sfd); return -err; } =20 - DPRINTF("%s: Pixel format set\n", PHPFUNC); + DPRINTF("%s: Pixel format set\n", VNCFUNC); =20 return 0; } @@ -393,11 +393,11 @@ vnc_set_encoding(int sfd) unsigned char buf[8]; =20 if (sfd < 0) { - DPRINTF("%s: Socket is not opened!\n", PHPFUNC); + DPRINTF("%s: Socket is not opened!\n", VNCFUNC); return -EINVAL; } =20 - DPRINTF("%s: Setting up encoding\n", PHPFUNC); + DPRINTF("%s: Setting up encoding\n", VNCFUNC); =20 memset(buf, 0, 8); buf[0] =3D 0x02; @@ -412,12 +412,12 @@ vnc_set_encoding(int sfd) =20 if (write(sfd, buf, 8) < 0) { int err =3D errno; - DPRINTF("%s: Write function failed with error code %d (%s)\n", PHP= FUNC, err, strerror(err)); + DPRINTF("%s: Write function failed with error code %d (%s)\n", VNC= FUNC, err, strerror(err)); close(sfd); return -err; } =20 - DPRINTF("%s: Client encoding set\n", PHPFUNC); + DPRINTF("%s: Client encoding set\n", VNCFUNC); return 0; } =20 @@ -439,11 +439,11 @@ vnc_send_framebuffer_update(int sfd, int incrementalU= pdate, int x, int y, int w, unsigned char buf[10]; =20 if (sfd < 0) { - DPRINTF("%s: Socket is not opened!\n", PHPFUNC); + DPRINTF("%s: Socket is not opened!\n", VNCFUNC); return -EINVAL; } =20 - DPRINTF("%s: Sending %s update request\n", PHPFUNC, (incrementalUpdate= ? "standard" : "incremental")); + DPRINTF("%s: Sending %s update request\n", VNCFUNC, (incrementalUpdate= ? "standard" : "incremental")); memset(buf, 0, 10); buf[0] =3D 0x03; buf[1] =3D incrementalUpdate; @@ -455,12 +455,12 @@ vnc_send_framebuffer_update(int sfd, int incrementalU= pdate, int x, int y, int w, =20 if (write(sfd, buf, 10) < 0) { int err =3D errno; - DPRINTF("%s: Write function failed with error code %d (%s)\n", PHP= FUNC, err, strerror(err)); + DPRINTF("%s: Write function failed with error code %d (%s)\n", VNC= FUNC, err, strerror(err)); close(sfd); return -err; } =20 - DPRINTF("%s: Request sent\n", PHPFUNC); + DPRINTF("%s: Request sent\n", VNCFUNC); =20 return 0; } @@ -499,11 +499,11 @@ vnc_connect(char *server, char *port, int share) if (sfd < 0) return sfd; =20 - DPRINTF("%s: Opened socket with descriptor #%d\n", PHPFUNC, sfd); + DPRINTF("%s: Opened socket with descriptor #%d\n", VNCFUNC, sfd); =20 if (read(sfd, buf, 1024) < 0) { int err =3D errno; - DPRINTF("%s: Read function failed with error code %d (%s)\n", PHPF= UNC, err, strerror(err)); + DPRINTF("%s: Read function failed with error code %d (%s)\n", VNCF= UNC, err, strerror(err)); close(sfd); return -err; } @@ -522,7 +522,7 @@ vnc_connect(char *server, char *port, int share) return -err; } =20 - DPRINTF("%s: Share desktop flag sent (%d)\n", PHPFUNC, buf[0]); + DPRINTF("%s: Share desktop flag sent (%d)\n", VNCFUNC, buf[0]); =20 return sfd; } @@ -542,16 +542,16 @@ vnc_read_server_init(int sfd) tServerFBParams params =3D { 0 }; int len =3D 0, namelen =3D 0; =20 - DPRINTF("%s: Server init - reading framebuffer parameters\n", PHPFUNC); + DPRINTF("%s: Server init - reading framebuffer parameters\n", VNCFUNC); if (read(sfd, tmpbuf, 24) < 0) { int err =3D errno; - DPRINTF("%s: Read function failed with error code %d (%s)\n", PHPF= UNC, err, strerror(err)); + DPRINTF("%s: Read function failed with error code %d (%s)\n", VNCF= UNC, err, strerror(err)); close(sfd); goto cleanup; } =20 namelen =3D (int)CALC_UINT32(tmpbuf[20], tmpbuf[21], tmpbuf[22], tmpbu= f[23]); - DPRINTF("%s: Name length is %d\n", PHPFUNC, namelen); + DPRINTF("%s: Name length is %d\n", VNCFUNC, namelen); =20 buf =3D (unsigned char *)malloc(namelen + 25); memset(buf, 0, namelen + 25); @@ -559,7 +559,7 @@ vnc_read_server_init(int sfd) =20 if ((len =3D read(sfd, buf + 24, namelen)) < 0) { int err =3D errno; - DPRINTF("%s: Read function failed with error code %d (%s)\n", PHPF= UNC, err, strerror(err)); + DPRINTF("%s: Read function failed with error code %d (%s)\n", VNCF= UNC, err, strerror(err)); close(sfd); goto cleanup; } @@ -587,16 +587,16 @@ int vnc_get_dimensions(char *server, char *port, int = *width, int *height) tServerFBParams params; =20 if (!width && !height) { - DPRINTF("%s: Neither width or height output value not defined\n", = PHPFUNC); + DPRINTF("%s: Neither width or height output value not defined\n", = VNCFUNC); return -EINVAL; } =20 - DPRINTF("%s: server is %s, port is %s\n", PHPFUNC, server, port); + DPRINTF("%s: server is %s, port is %s\n", VNCFUNC, server, port); =20 sfd =3D vnc_connect(server, port, 1); if (sfd < 0) { int err =3D errno; - DPRINTF("%s: VNC Connection failed with error code %d (%s)\n", PHP= FUNC, err, strerror(err)); + DPRINTF("%s: VNC Connection failed with error code %d (%s)\n", VNC= FUNC, err, strerror(err)); close(sfd); return -err; } @@ -606,13 +606,13 @@ int vnc_get_dimensions(char *server, char *port, int = *width, int *height) if (width) { *width =3D params.width; =20 - DPRINTF("%s: Output parameter of width set to %d\n", PHPFUNC, *wid= th); + DPRINTF("%s: Output parameter of width set to %d\n", VNCFUNC, *wid= th); } =20 if (height) { *height =3D params.height; =20 - DPRINTF("%s: Output parameter of height set to %d\n", PHPFUNC, *he= ight); + DPRINTF("%s: Output parameter of height set to %d\n", VNCFUNC, *he= ight); } =20 while (socket_has_data(sfd, 500000, 0) =3D=3D 1) @@ -620,7 +620,7 @@ int vnc_get_dimensions(char *server, char *port, int *w= idth, int *height) =20 shutdown(sfd, SHUT_RDWR); close(sfd); - DPRINTF("%s: Closed descriptor #%d\n", PHPFUNC, sfd); + DPRINTF("%s: Closed descriptor #%d\n", VNCFUNC, sfd); return 0; } =20 @@ -740,7 +740,7 @@ int vnc_get_bitmap(char *server, char *port, char *fn) sfd =3D vnc_connect(server, port, 1); if (sfd < 0) { int err =3D errno; - DPRINTF("%s: VNC Connection failed with error code %d (%s)\n", PHP= FUNC, err, strerror(err)); + DPRINTF("%s: VNC Connection failed with error code %d (%s)\n", VNC= FUNC, err, strerror(err)); close(sfd); return -err; } @@ -748,12 +748,12 @@ int vnc_get_bitmap(char *server, char *port, char *fn) params =3D vnc_read_server_init(sfd); =20 pattern_size =3D params.width * params.height * (params.bpp / 8); - DPRINTF("%s: %ld\n", PHPFUNC, pattern_size); + DPRINTF("%s: %ld\n", VNCFUNC, pattern_size); =20 vnc_set_pixel_format(sfd, params); vnc_set_encoding(sfd); =20 - DPRINTF("%s: Requesting framebuffer update\n", PHPFUNC); + DPRINTF("%s: Requesting framebuffer update\n", VNCFUNC); vnc_send_framebuffer_update_request(sfd, 1, params); =20 while (socket_has_data(sfd, 50000, 0) =3D=3D 0) { @@ -766,7 +766,7 @@ int vnc_get_bitmap(char *server, char *port, char *fn) =20 vnc_raw_to_bmp(file, fn, params.width, params.height); unlink(file); - DPRINTF("%s: Closed descriptor #%d\n", PHPFUNC, sfd); + DPRINTF("%s: Closed descriptor #%d\n", VNCFUNC, sfd); return 0; } =20 @@ -785,12 +785,12 @@ int vnc_send_keys(char *server, char *port, char *key= s) int i, skip_next; tServerFBParams params; =20 - DPRINTF("%s: server is %s, port is %s, keys are '%s'\n", PHPFUNC, serv= er, port, keys); + DPRINTF("%s: server is %s, port is %s, keys are '%s'\n", VNCFUNC, serv= er, port, keys); =20 sfd =3D vnc_connect(server, port, 1); if (sfd < 0) { int err =3D errno; - DPRINTF("%s: VNC Connection failed with error code %d (%s)\n", PHP= FUNC, err, strerror(err)); + DPRINTF("%s: VNC Connection failed with error code %d (%s)\n", VNC= FUNC, err, strerror(err)); close(sfd); return -err; } @@ -798,9 +798,9 @@ int vnc_send_keys(char *server, char *port, char *keys) params =3D vnc_read_server_init(sfd); =20 skip_next =3D 0; - DPRINTF("%s: About to process key sequence '%s' (%d keys)\n", PHPFUNC,= keys, (int)strlen(keys)); + DPRINTF("%s: About to process key sequence '%s' (%d keys)\n", VNCFUNC,= keys, (int)strlen(keys)); for (i =3D 0; i < strlen(keys); i++) { - DPRINTF("%s: Processing key %d: %d [0x%02x]\n", PHPFUNC, i, keys[i= ], keys[i]); + DPRINTF("%s: Processing key %d: %d [0x%02x]\n", VNCFUNC, i, keys[i= ], keys[i]); if (skip_next) { skip_next =3D 0; continue; @@ -815,25 +815,25 @@ int vnc_send_keys(char *server, char *port, char *key= s) skip_next =3D 1; } =20 - DPRINTF("%s: Sending key press emulation for key %d\n", PHPFUNC, k= eys[i]); + DPRINTF("%s: Sending key press emulation for key %d\n", VNCFUNC, k= eys[i]); vnc_send_key(sfd, keys[i], skip_next, 0); - DPRINTF("%s: Requesting framebuffer update\n", PHPFUNC); + DPRINTF("%s: Requesting framebuffer update\n", VNCFUNC); vnc_send_framebuffer_update_request(sfd, 1, params); - DPRINTF("%s: Sending key release emulation for key %d\n", PHPFUNC,= keys[i]); + DPRINTF("%s: Sending key release emulation for key %d\n", VNCFUNC,= keys[i]); vnc_send_key(sfd, keys[i], skip_next, 1); =20 /* Sleep for 50 ms, required to make VNC server accept the keystro= ke emulation */ usleep(50000); } =20 - DPRINTF("%s: All %d keys sent\n", PHPFUNC, (int)strlen(keys)); + DPRINTF("%s: All %d keys sent\n", VNCFUNC, (int)strlen(keys)); =20 while (socket_has_data(sfd, 500000, 0) =3D=3D 1) socket_read(sfd, -1); =20 shutdown(sfd, SHUT_RDWR); close(sfd); - DPRINTF("%s: Closed descriptor #%d\n", PHPFUNC, sfd); + DPRINTF("%s: Closed descriptor #%d\n", VNCFUNC, sfd); return 0; } =20 @@ -854,13 +854,13 @@ int vnc_send_pointer_event(char *server, char *port, = int pos_x, int pos_y, int c int sfd; tServerFBParams params; =20 - DPRINTF("%s: Server is %s, port is %s, x is %d, y is %d, clicked is %d= , release is %d\n", PHPFUNC, + DPRINTF("%s: Server is %s, port is %s, x is %d, y is %d, clicked is %d= , release is %d\n", VNCFUNC, server, port, pos_x, pos_y, clicked, release); =20 sfd =3D vnc_connect(server, port, 0); if (sfd < 0) { int err =3D errno; - DPRINTF("%s: VNC Connection failed with error code %d (%s)\n", PHP= FUNC, err, strerror(err)); + DPRINTF("%s: VNC Connection failed with error code %d (%s)\n", VNC= FUNC, err, strerror(err)); close(sfd); return -err; } @@ -870,7 +870,7 @@ int vnc_send_pointer_event(char *server, char *port, in= t pos_x, int pos_y, int c if (((pos_x > params.width) || (pos_y < 0)) || ((pos_y > params.height) || (pos_y < 0))) { DPRINTF("%s: Required positions out of range (width =3D %d, height= =3D %d, x =3D %d, y =3D %d) for '%s'\n", - PHPFUNC, params.width, params.height, pos_x, pos_y, params= .desktopName); + VNCFUNC, params.width, params.height, pos_x, pos_y, params= .desktopName); return -EINVAL; } =20 @@ -901,7 +901,7 @@ int vnc_send_pointer_event(char *server, char *port, in= t pos_x, int pos_y, int c shutdown(sfd, SHUT_RDWR); close(sfd); =20 - DPRINTF("%s: Closed descriptor #%d\n", PHPFUNC, sfd); + DPRINTF("%s: Closed descriptor #%d\n", VNCFUNC, sfd); =20 return 0; } @@ -920,29 +920,29 @@ int vnc_refresh_screen(char *server, char *port, int = scancode) int sfd; tServerFBParams params; =20 - DPRINTF("%s: Server is %s, port is %s, scancode is %d\n", PHPFUNC, ser= ver, port, scancode); + DPRINTF("%s: Server is %s, port is %s, scancode is %d\n", VNCFUNC, ser= ver, port, scancode); =20 - DPRINTF("%s: server is %s, port is %s\n", PHPFUNC, server, port); + DPRINTF("%s: server is %s, port is %s\n", VNCFUNC, server, port); =20 sfd =3D vnc_connect(server, port, 1); if (sfd < 0) { int err =3D errno; - DPRINTF("%s: VNC Connection failed with error code %d (%s)\n", PHP= FUNC, err, strerror(err)); + DPRINTF("%s: VNC Connection failed with error code %d (%s)\n", VNC= FUNC, err, strerror(err)); close(sfd); return -err; } =20 params =3D vnc_read_server_init(sfd); =20 - DPRINTF("%s: Sending key press emulation for key %d\n", PHPFUNC, scanc= ode); + DPRINTF("%s: Sending key press emulation for key %d\n", VNCFUNC, scanc= ode); if (scancode > -1) vnc_send_key(sfd, scancode, 1, 0); =20 - DPRINTF("%s: Requesting framebuffer update\n", PHPFUNC); + DPRINTF("%s: Requesting framebuffer update\n", VNCFUNC); vnc_send_framebuffer_update_request(sfd, 1, params); =20 shutdown(sfd, SHUT_RDWR); close(sfd); - DPRINTF("%s: Closed descriptor #%d\n", PHPFUNC, sfd); + DPRINTF("%s: Closed descriptor #%d\n", VNCFUNC, sfd); return 0; } --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 23:55:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501624273022803.7034992005271; Tue, 1 Aug 2017 14:51:13 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2168616AB07; Tue, 1 Aug 2017 21:51:11 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C125B6D2A9; Tue, 1 Aug 2017 21:51:10 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 18899180B467; Tue, 1 Aug 2017 21:51:10 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkS8V007865 for ; Tue, 1 Aug 2017 17:46:28 -0400 Received: by smtp.corp.redhat.com (Postfix) id AF3B160462; Tue, 1 Aug 2017 21:46:28 +0000 (UTC) Received: from mx1.redhat.com (ext-mx02.extmail.prod.ext.phx2.redhat.com [10.5.110.26]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A51A818A4F for ; Tue, 1 Aug 2017 21:46:28 +0000 (UTC) Received: from mail-qk0-f174.google.com (mail-qk0-f174.google.com [209.85.220.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7E9D1A4FAD for ; Tue, 1 Aug 2017 21:46:20 +0000 (UTC) Received: by mail-qk0-f174.google.com with SMTP id d136so17050000qkg.3 for ; Tue, 01 Aug 2017 14:46:20 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.17 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:17 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2168616AB07 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 2168616AB07 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KON73yHb" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 7E9D1A4FAD Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 7E9D1A4FAD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=PCrm24RaTkJz7Cu6+e214CMaCwXD7wNTx2vhV/kWlF8=; b=KON73yHb4AfUNiKXMTTVIAsGhC8AVWkEO9Jtzb5Vug1KT8oMl5NpBL36SqZJSck3NH WU4fv2UJ4UpTUI5KWWyNA3eI2zyIwlwUM9LC4da0C95encwwvRCgo6P/aEf5ficKAGoK W8kHwpy9oM+tI5nD9lhOg35BnmfNfYPrkM/x1KnygSP15BoCOAYVnilQzpDOoEP8JiVm J6qtSR7zHqkxPZ4Kq1PpqCwWghzediez/nG1mDU7xqRx8wx054zKB26aQNZ4FeSxnL+Q pwyXLwu2MlYaIPNy44rnNvuwMhWJ5o53wvU8abqVBZsnH/mnh6XSZyeBMdbklReOSmAe nTCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=PCrm24RaTkJz7Cu6+e214CMaCwXD7wNTx2vhV/kWlF8=; b=ifGBFBJBmzb4dblDQIJXm1vD7tJY6DE4Aa61VXcTDvPZvRDP82JOkAe8fKx06AKmRf pVtRSpIrZ8W9SI8yRd0jOsDH+1XDTdMU3YzMM+7v/f3Dkj+CSeQOC4KQ7wDRcUhbLK3O 2mZn2UtsvvnQ2ilwJ91FKj9vmIvBz9Y0+95Xgex/Q5tGmtCrscU1oz0Z6nU7JX6gXmvO V6ZnXh/QrpTo9B5e1HBxtTvrRhGvb+dP+pH8aTvPsFIjcvUdY5lPVHp+Fz/MUvxySmOV Oq1XN8QV1NWdam1wmQHpk9bjWfbBK06kuYo3boizaOOc4AO3BCzVOfndRDoT1a6ZVSWz jTOg== X-Gm-Message-State: AHYfb5gzWjJo8rMSxzY6doNjNw7A0Sp7+ro1jvr8sRze2JVACIgnD32z ddkEieRjMebAXh69FL8= X-Received: by 10.55.140.196 with SMTP id o187mr2351708qkd.80.1501623978496; Tue, 01 Aug 2017 14:46:18 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:05 -0400 Message-Id: <20170801214614.8915-5-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 01 Aug 2017 21:46:21 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 01 Aug 2017 21:46:21 +0000 (UTC) for IP:'209.85.220.174' DOMAIN:'mail-qk0-f174.google.com' HELO:'mail-qk0-f174.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: -0.43 (DCC_REPUT_00_12, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.220.174 mail-qk0-f174.google.com 209.85.220.174 mail-qk0-f174.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.26 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 04/13] Split up the bindings for libvirt connection API X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 01 Aug 2017 21:51:11 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" * add libvirt-connection.h and libvirt-connection.c * move all libvirt_connect_* function declarations and definitions to above files * other minor adjusments to libvirt-php.h and util.h to keep the code compilable while the code is being moved around. --- src/Makefile.am | 3 +- src/libvirt-connection.c | 885 +++++++++++++++++++++++++++++++++++++++++++= ++ src/libvirt-connection.h | 83 +++++ src/libvirt-php.c | 919 +------------------------------------------= ---- src/libvirt-php.h | 104 +++--- src/util.h | 7 - 6 files changed, 1024 insertions(+), 977 deletions(-) create mode 100644 src/libvirt-connection.c create mode 100644 src/libvirt-connection.h diff --git a/src/Makefile.am b/src/Makefile.am index bbee667..0819dc6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,7 +21,8 @@ libvirt_php_la_SOURCES =3D \ util.c util.h \ vncfunc.c vncfunc.h \ sockets.c sockets.h \ - libvirt-php.c libvirt-php.h + libvirt-php.c libvirt-php.h \ + libvirt-connection.c libvirt-connection.h libvirt_php_la_CFLAGS =3D \ $(AM_CFLAGS) \ -DCOMPILE_DL_LIBVIRT=3D1 diff --git a/src/libvirt-connection.c b/src/libvirt-connection.c new file mode 100644 index 0000000..bcebd44 --- /dev/null +++ b/src/libvirt-connection.c @@ -0,0 +1,885 @@ +/* + * libvirt-connection.c: The PHP bindings to libvirt connection API + * + * See COPYING for the license of this software + */ + +#include + +#include "libvirt-connection.h" + +DEBUG_INIT("connection"); + +/* + * Private function name: free_resources_on_connection + * Since version: 0.4.2 + * Description: Function is used to free all the resources ass= igned to the connection identified by conn + * Arguments: @conn [virConnectPtr]: libvirt connection poin= ter + * Returns: None + */ +static void +free_resources_on_connection(virConnectPtr conn TSRMLS_DC) +{ + int binding_resources_count =3D 0; + resource_info *binding_resources; + int i; + + binding_resources_count =3D LIBVIRT_G(binding_resources_count); + binding_resources =3D LIBVIRT_G(binding_resources); + + for (i =3D 0; i < binding_resources_count; i++) { + if ((binding_resources[i].overwrite =3D=3D 0) && (binding_resource= s[i].conn =3D=3D conn)) + free_resource(binding_resources[i].type, binding_resources[i].= mem TSRMLS_CC); + } +} + +/* Destructor for connection resource */ +void +php_libvirt_connection_dtor(virt_resource *rsrc TSRMLS_DC) +{ + php_libvirt_connection *conn =3D (php_libvirt_connection *) rsrc->ptr; + int rv =3D 0; + + if (conn !=3D NULL) { + if (conn->conn !=3D NULL) { + free_resources_on_connection(conn->conn TSRMLS_CC); + + rv =3D virConnectClose(conn->conn); + if (rv =3D=3D -1) { + DPRINTF("%s: virConnectClose(%p) returned %d (%s)\n", __FU= NCTION__, conn->conn, rv, LIBVIRT_G(last_error)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virConnectClo= se failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virConnectClose(%p) completed successfully\n"= , __FUNCTION__, conn->conn); + resource_change_counter(INT_RESOURCE_CONNECTION, NULL, con= n->conn, 0 TSRMLS_CC); + } + conn->conn =3D NULL; + } + efree(conn); + } +} + +/* Authentication callback function. + * + * Should receive list of credentials via cbdata and pass the requested on= e to + * libvirt + */ +static int libvirt_virConnectAuthCallback(virConnectCredentialPtr cred, + unsigned int ncred, void *cbdata) +{ + TSRMLS_FETCH(); + + unsigned int i, j; + php_libvirt_cred_value *creds =3D (php_libvirt_cred_value *) cbdata; + for (i =3D 0; i < (unsigned int)ncred; i++) { + DPRINTF("%s: cred %d, type %d, prompt %s challenge %s\n ", __FUNCT= ION__, i, cred[i].type, cred[i].prompt, cred[i].challenge); + if (creds !=3D NULL) + for (j =3D 0; j < (unsigned int)creds[0].count; j++) { + if (creds[j].type =3D=3D cred[i].type) { + cred[i].resultlen =3D creds[j].resultlen; + cred[i].result =3D (char *)malloc(creds[j].resultlen += 1); + memset(cred[i].result, 0, creds[j].resultlen + 1); + strncpy(cred[i].result, creds[j].result, creds[j].resu= ltlen); + } + } + DPRINTF("%s: result %s (%d)\n", __FUNCTION__, cred[i].result, cred= [i].resultlen); + } + + return 0; +} + +static int libvirt_virConnectCredType[] =3D { + VIR_CRED_AUTHNAME, + VIR_CRED_ECHOPROMPT, + VIR_CRED_REALM, + VIR_CRED_PASSPHRASE, + VIR_CRED_NOECHOPROMPT, + //VIR_CRED_EXTERNAL, +}; + +/* + * Function name: libvirt_connect + * Since version: 0.4.1(-1) + * Description: libvirt_connect() is used to connect to the specified = libvirt daemon using the specified URL, user can also set the readonly flag= and/or set credentials for connection + * Arguments: @url [string]: URI for connection + * @readonly [bool]: flag whether to use read-only connec= tion or not + * @credentials [array]: array of connection credentials + * Returns: libvirt connection resource + */ +PHP_FUNCTION(libvirt_connect) +{ + php_libvirt_connection *conn; + php_libvirt_cred_value *creds =3D NULL; + zval *zcreds =3D NULL; + zval *data; + int i; + int j; + int credscount =3D 0; + + virConnectAuth libvirt_virConnectAuth =3D { + libvirt_virConnectCredType, + sizeof(libvirt_virConnectCredType) / sizeof(int), + libvirt_virConnectAuthCallback, + NULL + }; + + char *url =3D NULL; + strsize_t url_len =3D 0; + zend_bool readonly =3D 1; + + HashTable *arr_hash; + HashPosition pointer; + int array_count; + + zend_ulong index; + + unsigned long libVer; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sba", &url, &ur= l_len, &readonly, &zcreds) =3D=3D FAILURE) { + RETURN_FALSE; + } + + if (virGetVersion(&libVer, NULL, NULL)!=3D 0) + RETURN_FALSE; + + if (libVer < 6002) { + set_error("Only libvirt 0.6.2 and higher supported. Please upgrade= your libvirt" TSRMLS_CC); + RETURN_FALSE; + } + + if ((count_resources(INT_RESOURCE_CONNECTION TSRMLS_CC) + 1) > LIBVIRT= _G(max_connections_ini)) { + DPRINTF("%s: maximum number of connections allowed exceeded (max %= lu)\n", PHPFUNC, (unsigned long)LIBVIRT_G(max_connections_ini)); + set_error("Maximum number of connections allowed exceeded" TSRMLS_= CC); + RETURN_FALSE; + } + + /* If 'null' value has been passed as URL override url to NULL value t= o autodetect the hypervisor */ + if ((url =3D=3D NULL) || (strcasecmp(url, "NULL") =3D=3D 0)) + url =3D NULL; + + conn =3D (php_libvirt_connection *)emalloc(sizeof(php_libvirt_connecti= on)); + if (zcreds =3D=3D NULL) { + /* connecting without providing authentication */ + if (readonly) + conn->conn =3D virConnectOpenReadOnly(url); + else + conn->conn =3D virConnectOpen(url); + } else { + /* connecting with authentication (using callback) */ + arr_hash =3D Z_ARRVAL_P(zcreds); + array_count =3D zend_hash_num_elements(arr_hash); + + credscount =3D array_count; + creds =3D (php_libvirt_cred_value *)emalloc(credscount * sizeof(ph= p_libvirt_cred_value)); + j =3D 0; + /* parse the input Array and create list of credentials. The list = (array) is passed to callback function. */ + VIRT_FOREACH(arr_hash, pointer, data) { + if (Z_TYPE_P(data) =3D=3D IS_STRING) { + php_libvirt_hash_key_info info; + VIRT_HASH_CURRENT_KEY_INFO(arr_hash, pointer, index, info); + + if (info.type =3D=3D HASH_KEY_IS_STRING) { + PHPWRITE(info.name, info.length); + } else { + DPRINTF("%s: credentials index %d\n", PHPFUNC, (int)in= dex); + creds[j].type =3D index; + creds[j].result =3D (char *)emalloc(Z_STRLEN_P(data) += 1); + memset(creds[j].result, 0, Z_STRLEN_P(data) + 1); + creds[j].resultlen =3D Z_STRLEN_P(data); + strncpy(creds[j].result, Z_STRVAL_P(data), Z_STRLEN_P(= data)); + j++; + } + } + } VIRT_FOREACH_END(); + DPRINTF("%s: Found %d elements for credentials\n", PHPFUNC, j); + creds[0].count =3D j; + libvirt_virConnectAuth.cbdata =3D (void *)creds; + conn->conn =3D virConnectOpenAuth(url, &libvirt_virConnectAuth, re= adonly ? VIR_CONNECT_RO : 0); + for (i =3D 0; i < creds[0].count; i++) + efree(creds[i].result); + efree(creds); + } + + if (conn->conn =3D=3D NULL) { + DPRINTF("%s: Cannot establish connection to %s\n", PHPFUNC, url); + efree(conn); + RETURN_FALSE; + } + + resource_change_counter(INT_RESOURCE_CONNECTION, NULL, conn->conn, 1 T= SRMLS_CC); + DPRINTF("%s: Connection to %s established, returning %p\n", PHPFUNC, u= rl, conn->conn); + + VIRT_REGISTER_RESOURCE(conn, le_libvirt_connection); + conn->resource =3D VIRT_RESOURCE_HANDLE(return_value); +} + +/* + * Function name: libvirt_connect_get_uri + * Since version: 0.4.1(-1) + * Description: Function is used to get the connection URI. This is us= eful to check the hypervisor type of host machine when using "null" uri to = libvirt_connect() + * Arguments: @conn [resource]: resource for connection + * Returns: connection URI string or FALSE for error + */ +PHP_FUNCTION(libvirt_connect_get_uri) +{ + zval *zconn; + char *uri; + php_libvirt_connection *conn =3D NULL; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + uri =3D virConnectGetURI(conn->conn); + DPRINTF("%s: virConnectGetURI returned %s\n", PHPFUNC, uri); + if (uri =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETVAL_STRING(uri); + free(uri); +} + +/* + * Function name: libvirt_connect_get_hostname + * Since version: 0.4.1(-1) + * Description: Function is used to get the hostname of the guest asso= ciated with the connection + * Arguments: @conn [resource]: resource for connection + * Returns: hostname of the host node or FALSE for error + */ +PHP_FUNCTION(libvirt_connect_get_hostname) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + char *hostname; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + hostname =3D virConnectGetHostname(conn->conn); + DPRINTF("%s: virConnectGetHostname returned %s\n", PHPFUNC, hostname); + if (hostname =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETVAL_STRING(hostname); + free(hostname); +} + +/* + * Function name: libvirt_connect_get_hypervisor + * Since version: 0.4.1(-2) + * Description: Function is used to get the information about the hype= rvisor on the connection identified by the connection pointer + * Arguments: @conn [resource]: resource for connection + * Returns: array of hypervisor information if available + */ +PHP_FUNCTION(libvirt_connect_get_hypervisor) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + unsigned long hvVer =3D 0; + const char *type =3D NULL; + char hvStr[64] =3D { 0 }; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if (virConnectGetVersion(conn->conn, &hvVer) !=3D 0) + RETURN_FALSE; + + type =3D virConnectGetType(conn->conn); + if (type =3D=3D NULL) + RETURN_FALSE; + + DPRINTF("%s: virConnectGetType returned %s\n", PHPFUNC, type); + + array_init(return_value); + VIRT_ADD_ASSOC_STRING(return_value, "hypervisor", (char *)type); + add_assoc_long(return_value, "major", (long)((hvVer/1000000) % 1000)); + add_assoc_long(return_value, "minor", (long)((hvVer/1000) % 1000)); + add_assoc_long(return_value, "release", (long)(hvVer % 1000)); + + snprintf(hvStr, sizeof(hvStr), "%s %d.%d.%d", type, + (long)((hvVer/1000000) % 1000), (long)((hvVer/1000) % 1000), = (long)(hvVer % 1000)); + VIRT_ADD_ASSOC_STRING(return_value, "hypervisor_string", hvStr); +} + +/* + * Function name: libvirt_connect_get_capabilities + * Since version: 0.4.1(-2) + * Description: Function is used to get the capabilities information f= rom the connection + * Arguments: @conn [resource]: resource for connection + * @xpath [string]: optional xPath query to be applied on= the result + * Returns: capabilities XML from the connection or FALSE for error + */ +PHP_FUNCTION(libvirt_connect_get_capabilities) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + char *caps; + char *xpath =3D NULL; + strsize_t xpath_len; + char *tmp =3D NULL; + int retval =3D -1; + + GET_CONNECTION_FROM_ARGS("r|s", &zconn, &xpath, &xpath_len); + + caps =3D virConnectGetCapabilities(conn->conn); + if (caps =3D=3D NULL) + RETURN_FALSE; + + tmp =3D get_string_from_xpath(caps, xpath, NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + VIRT_RETVAL_STRING(caps); + } else { + VIRT_RETVAL_STRING(tmp); + } + + free(caps); + free(tmp); +} + +/* + * Function name: libvirt_connect_get_emulator + * Since version: 0.4.5 + * Description: Function is used to get the emulator for requested con= nection/architecture + * Arguments: @conn [resource]: libvirt connection resource + * @arch [string]: optional architecture string, can be N= ULL to get default + * Returns: path to the emulator + */ +PHP_FUNCTION(libvirt_connect_get_emulator) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + char *arch =3D NULL; + strsize_t arch_len; + char *tmp; + + GET_CONNECTION_FROM_ARGS("r|s", &zconn, &arch, &arch_len); + + if ((arch =3D=3D NULL) || (arch_len =3D=3D 0)) + arch =3D NULL; + + tmp =3D connection_get_emulator(conn->conn, arch TSRMLS_CC); + if (tmp =3D=3D NULL) { + set_error("Cannot get emulator" TSRMLS_CC); + RETURN_FALSE; + } + + VIRT_RETVAL_STRING(tmp); + free(tmp); +} + +/* + * Function name: libvirt_connect_get_nic_models + * Since version: 0.4.9 + * Description: Function is used to get NIC models for requested conne= ction/architecture + * Arguments: @conn [resource]: libvirt connection resource + * @arch [string]: optional architecture string, can be N= ULL to get default + * Returns: array of models + */ +PHP_FUNCTION(libvirt_connect_get_nic_models) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + char *arch =3D NULL; + strsize_t arch_len; + char *tmp; + + GET_CONNECTION_FROM_ARGS("r|s", &zconn, &arch, &arch_len); + + /* Disable getting it on remote connections */ + if (!is_local_connection(conn->conn)) { + set_error("This function works only on local connections" TSRMLS_C= C); + RETURN_FALSE; + } + + /* This approach is working only for QEMU driver so bails if not curre= ntly using it */ + if (strcmp(virConnectGetType(conn->conn), "QEMU") !=3D 0) { + set_error("This function can be used only for QEMU driver" TSRMLS_= CC); + RETURN_FALSE; + } + +#ifndef EXTWIN + if ((arch =3D=3D NULL) || (arch_len =3D=3D 0)) + arch =3D NULL; + + tmp =3D connection_get_emulator(conn->conn, arch TSRMLS_CC); + if (tmp =3D=3D NULL) { + set_error("Cannot get emulator" TSRMLS_CC); + RETURN_FALSE; + } + + char cmd[4096] =3D { 0 }; + char tmp2[16] =3D { 0 }; + snprintf(cmd, sizeof(cmd), "%s -net nic,model=3D? 2>&1", tmp); + + FILE *fp =3D popen(cmd, "r"); + if (fp =3D=3D NULL) + RETURN_FALSE; + + array_init(return_value); + while (!feof(fp)) { + memset(cmd, 0, sizeof(cmd)); + if (!fgets(cmd, sizeof(cmd), fp)) + break; + + if ((tmp =3D strstr(cmd, "Supported NIC models:")) !=3D NULL) { + tmp =3D strstr(tmp, ":") + 2; + + int i; + tTokenizer t =3D tokenize(tmp, ","); + for (i =3D 0; i < t.numTokens; i++) { + snprintf(tmp2, sizeof(tmp2), "%d", i); + VIRT_ADD_ASSOC_STRING(return_value, tmp2, t.tokens[i]); + } + free_tokens(t); + } + } + fclose(fp); +#endif +} + +/* + * Function name: libvirt_connect_get_soundhw_models + * Since version: 0.4.9 + * Description: Function is used to get sound hardware models for requ= ested connection/architecture + * Arguments: @conn [resource]: libvirt connection resource + * @arch [string]: optional architecture string, can be N= ULL to get default + * @flags [int]: flags for getting sound hardware. Can be= either 0 or VIR_CONNECT_SOUNDHW_GET_NAMES + * Returns: array of models + */ +PHP_FUNCTION(libvirt_connect_get_soundhw_models) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + char *arch =3D NULL; + strsize_t arch_len; + char *tmp; + zend_long flags =3D 0; + + GET_CONNECTION_FROM_ARGS("r|sl", &zconn, &arch, &arch_len, &flags); + + if ((arch =3D=3D NULL) || (arch_len =3D=3D 0)) + arch =3D NULL; + + /* Disable getting it on remote connections */ + if (!is_local_connection(conn->conn)) { + set_error("This function works only on local connections" TSRMLS_C= C); + RETURN_FALSE; + } + +#ifndef EXTWIN + /* This approach is working only for QEMU driver so bails if not curre= ntly using it */ + if (strcmp(virConnectGetType(conn->conn), "QEMU") !=3D 0) { + set_error("This function can be used only for QEMU driver" TSRMLS_= CC); + RETURN_FALSE; + } + + tmp =3D connection_get_emulator(conn->conn, arch TSRMLS_CC); + if (tmp =3D=3D NULL) { + set_error("Cannot get emulator" TSRMLS_CC); + RETURN_FALSE; + } + + char cmd[4096] =3D { 0 }; + snprintf(cmd, sizeof(cmd), "%s -soundhw help 2>&1", tmp); + + FILE *fp =3D popen(cmd, "r"); + if (fp =3D=3D NULL) + RETURN_FALSE; + + short inFunc =3D 0; + + int n =3D 0; + array_init(return_value); + while (!feof(fp)) { + memset(cmd, 0, sizeof(cmd)); + if (!fgets(cmd, sizeof(cmd), fp)) + break; + + if (strncmp(cmd, "Valid ", 6) =3D=3D 0) { + inFunc =3D 1; + continue; + } else + if (strlen(cmd) < 2) + inFunc =3D 0; + + if (inFunc) { + int i =3D 0; + char desc[1024] =3D { 0 }; + tTokenizer t =3D tokenize(cmd, " "); + if (t.numTokens =3D=3D 0) + continue; + + if ((i > 0) && (flags & CONNECT_FLAG_SOUNDHW_GET_NAMES)) { + zval *arr; + memset(desc, 0, sizeof(desc)); + for (i =3D 1; i < t.numTokens; i++) { + strcat(desc, t.tokens[i]); + if (i < t.numTokens - 1) + strcat(desc, " "); + } + + 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); + } else { + char tmp2[16] =3D { 0 }; + snprintf(tmp2, sizeof(tmp2), "%d", n++); + VIRT_ADD_ASSOC_STRING(return_value, tmp2, t.tokens[0]); + } + + free_tokens(t); + } + } + fclose(fp); +#endif +} + +/* + * Function name: libvirt_connect_get_maxvcpus + * Since version: 0.4.1(-2) + * Description: Function is used to get maximum number of VCPUs per VM= on the hypervisor connection + * Arguments: @conn [resource]: resource for connection + * Returns: number of VCPUs available per VM on the connection or = FALSE for error + */ +PHP_FUNCTION(libvirt_connect_get_maxvcpus) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + const char *type =3D NULL; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + type =3D virConnectGetType(conn->conn); + if (type =3D=3D NULL) + RETURN_FALSE; + + RETURN_LONG(virConnectGetMaxVcpus(conn->conn, type)); +} + +/* + * Function name: libvirt_connect_get_sysinfo + * Since version: 0.4.1(-2) + * Description: Function is used to get the system information from co= nnection if available + * Arguments: @conn [resource]: resource for connection + * Returns: XML description of system information from the connect= ion or FALSE for error + */ +PHP_FUNCTION(libvirt_connect_get_sysinfo) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + char *sysinfo; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + sysinfo =3D virConnectGetSysinfo(conn->conn, 0); + if (sysinfo =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETVAL_STRING(sysinfo); + free(sysinfo); +} + +/* + * Function name: libvirt_connect_is_encrypted + * Since version: 0.4.1(-2) + * Description: Function is used to get the information whether the co= nnection is encrypted or not + * Arguments: @conn [resource]: resource for connection + * Returns: 1 if encrypted, 0 if not encrypted, -1 on error + */ +PHP_FUNCTION(libvirt_connect_get_encrypted) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + RETURN_LONG(virConnectIsEncrypted(conn->conn)); +} + +/* + * Function name: libvirt_connect_is_secure + * Since version: 0.4.1(-2) + * Description: Function is used to get the information whether the co= nnection is secure or not + * Arguments: @conn [resource]: resource for connection + * Returns: 1 if secure, 0 if not secure, -1 on error + */ +PHP_FUNCTION(libvirt_connect_get_secure) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + RETURN_LONG(virConnectIsSecure(conn->conn)); +} + +/* + * Function name: libvirt_connect_get_information + * Since version: 0.4.1(-2) + * Description: Function is used to get the information about the conn= ection + * Arguments: @conn [resource]: resource for connection + * Returns: array of information about the connection + */ +PHP_FUNCTION(libvirt_connect_get_information) +{ + zval *zconn; + char *tmp; + unsigned long hvVer =3D 0; + const char *type =3D NULL; + char hvStr[64] =3D { 0 }; + int iTmp =3D -1, maxvcpus =3D -1; + php_libvirt_connection *conn =3D NULL; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + tmp =3D virConnectGetURI(conn->conn); + DPRINTF("%s: Got connection URI of %s...\n", PHPFUNC, tmp); + array_init(return_value); + VIRT_ADD_ASSOC_STRING(return_value, "uri", tmp ? tmp : "unknown"); + free(tmp); + tmp =3D virConnectGetHostname(conn->conn); + VIRT_ADD_ASSOC_STRING(return_value, "hostname", tmp ? tmp : "unknown"); + free(tmp); + + if ((virConnectGetVersion(conn->conn, &hvVer) =3D=3D 0) && (type =3D v= irConnectGetType(conn->conn))) { + VIRT_ADD_ASSOC_STRING(return_value, "hypervisor", (char *)type); + add_assoc_long(return_value, "hypervisor_major", (long)((hvVer/100= 0000) % 1000)); + add_assoc_long(return_value, "hypervisor_minor", (long)((hvVer/100= 0) % 1000)); + add_assoc_long(return_value, "hypervisor_release", (long)(hvVer % = 1000)); + snprintf(hvStr, sizeof(hvStr), "%s %d.%d.%d", type, + (long)((hvVer/1000000) % 1000), (long)((hvVer/1000) % 100= 0), (long)(hvVer % 1000)); + VIRT_ADD_ASSOC_STRING(return_value, "hypervisor_string", hvStr); + } + + if (strcmp(type, "QEMU") =3D=3D 0) { + /* For QEMU the value is not reliable so we return -1 instead */ + maxvcpus =3D -1; + } else { + maxvcpus =3D virConnectGetMaxVcpus(conn->conn, type); + } + + add_assoc_long(return_value, "hypervisor_maxvcpus", maxvcpus); + iTmp =3D virConnectIsEncrypted(conn->conn); + if (iTmp =3D=3D 1) + VIRT_ADD_ASSOC_STRING(return_value, "encrypted", "Yes"); + else + if (iTmp =3D=3D 0) + VIRT_ADD_ASSOC_STRING(return_value, "encrypted", "No"); + else + VIRT_ADD_ASSOC_STRING(return_value, "encrypted", "unknown"); + + iTmp =3D virConnectIsSecure(conn->conn); + if (iTmp =3D=3D 1) + VIRT_ADD_ASSOC_STRING(return_value, "secure", "Yes"); + else + if (iTmp =3D=3D 0) + VIRT_ADD_ASSOC_STRING(return_value, "secure", "No"); + else + VIRT_ADD_ASSOC_STRING(return_value, "secure", "unknown"); + + add_assoc_long(return_value, "num_inactive_domains", virConnectNumOfDe= finedDomains(conn->conn)); + add_assoc_long(return_value, "num_inactive_interfaces", virConnectNumO= fDefinedInterfaces(conn->conn)); + add_assoc_long(return_value, "num_inactive_networks", virConnectNumOfD= efinedNetworks(conn->conn)); + add_assoc_long(return_value, "num_inactive_storagepools", virConnectNu= mOfDefinedStoragePools(conn->conn)); + + add_assoc_long(return_value, "num_active_domains", virConnectNumOfDoma= ins(conn->conn)); + add_assoc_long(return_value, "num_active_interfaces", virConnectNumOfI= nterfaces(conn->conn)); + add_assoc_long(return_value, "num_active_networks", virConnectNumOfNet= works(conn->conn)); + add_assoc_long(return_value, "num_active_storagepools", virConnectNumO= fStoragePools(conn->conn)); + + add_assoc_long(return_value, "num_total_domains", virConnectNumOfDomai= ns(conn->conn) + virConnectNumOfDefinedDomains(conn->conn)); + add_assoc_long(return_value, "num_total_interfaces", virConnectNumOfIn= terfaces(conn->conn) + virConnectNumOfDefinedInterfaces(conn->conn)); + add_assoc_long(return_value, "num_total_networks", virConnectNumOfNetw= orks(conn->conn) + virConnectNumOfDefinedNetworks(conn->conn)); + add_assoc_long(return_value, "num_total_storagepools", virConnectNumOf= StoragePools(conn->conn) + virConnectNumOfDefinedStoragePools(conn->conn)); + + add_assoc_long(return_value, "num_secrets", virConnectNumOfSecrets(con= n->conn)); + add_assoc_long(return_value, "num_nwfilters", virConnectNumOfNWFilters= (conn->conn)); +} + +/* + * Function name: libvirt_connect_get_machine_types + * Since version: 0.4.9 + * Description: Function is used to get machine types supported by hyp= ervisor on the conneciton + * Arguments: @conn [resource]: resource for connection + * Returns: array of machine types for the connection incl. maxCpu= s if appropriate + */ +PHP_FUNCTION(libvirt_connect_get_machine_types) +{ + zval *zconn; + php_libvirt_connection *conn =3D NULL; + char *caps =3D NULL; + char **ret =3D NULL; + int i, num =3D -1; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + caps =3D virConnectGetCapabilities(conn->conn); + if (caps =3D=3D NULL) + RETURN_FALSE; + + array_init(return_value); + + ret =3D get_array_from_xpath(caps, "//capabilities/guest/arch/@name", = &num); + if (ret !=3D NULL) { + for (i =3D 0; i < num; i++) { + int num2, j; + char tmp[1024] =3D { 0 }; + + snprintf(tmp, sizeof(tmp), "//capabilities/guest/arch[@name=3D= \"%s\"]/domain/@type", ret[i]); + char **ret2 =3D get_array_from_xpath(caps, tmp, &num2); + if (ret2 !=3D NULL) { + zval *arr2; + VIRT_ARRAY_INIT(arr2); + + for (j =3D 0; j < num2; j++) { + int num3, k; + char tmp2[1024] =3D { 0 }; + zval *arr3; + + VIRT_ARRAY_INIT(arr3); + + snprintf(tmp2, sizeof(tmp2), "//capabilities/guest/arc= h[@name=3D\"%s\"]/machine", + ret[i]); + + char **ret3 =3D get_array_from_xpath(caps, tmp2, &num3= ); + if (ret3 !=3D NULL) { + for (k =3D 0; k < num3; k++) { + char *numTmp =3D NULL; + char key[8] =3D { 0 }; + char tmp3[2048] =3D { 0 }; + + snprintf(key, sizeof(key), "%d", k); + //VIRT_ADD_ASSOC_STRING(arr2, key, ret3[k]); + + snprintf(tmp3, sizeof(tmp3), "//capabilities/g= uest/arch[@name=3D\"%s\"]/machine[text()=3D\"%s\"]/@maxCpus", + ret[i], ret3[k]); + + numTmp =3D get_string_from_xpath(caps, tmp3, N= ULL, NULL); + if (numTmp =3D=3D NULL) { + VIRT_ADD_ASSOC_STRING(arr2, key, ret3[k]); + } else { + zval *arr4; + VIRT_ARRAY_INIT(arr4); + VIRT_ADD_ASSOC_STRING(arr4, "name", ret3[k= ]); + VIRT_ADD_ASSOC_STRING(arr4, "maxCpus", num= Tmp); + + add_assoc_zval_ex(arr2, key, strlen(key) += 1, arr4); + free(numTmp); + } + + free(ret3[k]); + } + } + + /* Domain type specific */ + snprintf(tmp2, sizeof(tmp2), "//capabilities/guest/arc= h[@name=3D\"%s\"]/domain[@type=3D\"%s\"]/machine", + ret[i], ret2[j]); + + ret3 =3D get_array_from_xpath(caps, tmp2, &num3); + if (ret3 !=3D NULL) { + for (k =3D 0; k < num3; k++) { + char key[8] =3D { 0 }; + char tmp3[2048] =3D { 0 }; + char *numTmp =3D NULL; + + snprintf(key, sizeof(key), "%d", k); + snprintf(tmp3, sizeof(tmp3), + "//capabilities/guest/arch[@name=3D\"= %s\"]/domain[@type=3D\"%s\"]/machine[text()=3D\"%s\"]/@maxCpus", + ret[i], ret2[j], ret3[k]); + + numTmp =3D get_string_from_xpath(caps, tmp3, N= ULL, NULL); + if (numTmp =3D=3D NULL) { + VIRT_ADD_ASSOC_STRING(arr3, key, ret3[k]); + } else { + zval *arr4; + VIRT_ARRAY_INIT(arr4); + + VIRT_ADD_ASSOC_STRING(arr4, "name", ret3[k= ]); + VIRT_ADD_ASSOC_STRING(arr4, "maxCpus", num= Tmp); + + add_assoc_zval_ex(arr3, key, strlen(key) += 1, arr4); + free(numTmp); + } + + free(ret3[k]); + } + + add_assoc_zval_ex(arr2, ret2[j], strlen(ret2[j]) += 1, arr3); + } + } + //free(ret2[j]); + + add_assoc_zval_ex(return_value, ret[i], strlen(ret[i]) + 1= , arr2); + } + free(ret[i]); + } + } +} + +/* + * Function name: libvirt_connect_get_all_domain_stats + * Since version: 0.5.1(-1) + * Description: Query statistics for all domains on a given connection + * Arguments: @conn [resource]: resource for connection + * @stats [int]: the statistic groups from VIR_DOMAIN_STA= TS_* + * @flags [int]: the filter flags from VIR_CONNECT_GET_AL= L_DOMAINS_STATS_* + * Returns: assoc array with statistics or false on error + */ +PHP_FUNCTION(libvirt_connect_get_all_domain_stats) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int retval =3D -1; + zend_long flags =3D 0; + zend_long stats =3D 0; + const char *name =3D NULL; + int i; + int j; + virTypedParameter params; + virDomainStatsRecordPtr *retstats =3D NULL; + + GET_CONNECTION_FROM_ARGS("r|l|l", &zconn, &stats, &flags); + + retval =3D virConnectGetAllDomainStats(conn->conn, stats, &retstats, f= lags); + + array_init(return_value); + if (retval < 0) + RETURN_FALSE; + + for (i =3D 0; i < retval; i++) { + zval *arr2; + VIRT_ARRAY_INIT(arr2); + + for (j =3D 0; j < retstats[i]->nparams; j++) { + params =3D retstats[i]->params[j]; + switch (params.type) { + case VIR_TYPED_PARAM_INT: + add_assoc_long(arr2, params.field, params.value.i); + break; + case VIR_TYPED_PARAM_UINT: + add_assoc_long(arr2, params.field, params.value.ui); + break; + case VIR_TYPED_PARAM_LLONG: + add_assoc_long(arr2, params.field, params.value.l); + break; + case VIR_TYPED_PARAM_ULLONG: + add_assoc_long(arr2, params.field, params.value.ul); + break; + case VIR_TYPED_PARAM_DOUBLE: + add_assoc_double(arr2, params.field, params.value.d); + break; + case VIR_TYPED_PARAM_BOOLEAN: + add_assoc_bool(arr2, params.field, params.value.b); + break; + case VIR_TYPED_PARAM_STRING: + VIRT_ADD_ASSOC_STRING(arr2, params.field, params.value.s); + break; + } + } + name =3D virDomainGetName(retstats[i]->dom); +#if PHP_MAJOR_VERSION >=3D 7 + zend_hash_update(Z_ARRVAL_P(return_value), zend_string_init(name, = strlen(name), 0), arr2); +#else + zend_hash_update(Z_ARRVAL_P(return_value), name, strlen(name)+1, &= arr2, sizeof(arr2), NULL); +#endif + } + + virDomainStatsRecordListFree(retstats); +} + diff --git a/src/libvirt-connection.h b/src/libvirt-connection.h new file mode 100644 index 0000000..2c50ec9 --- /dev/null +++ b/src/libvirt-connection.h @@ -0,0 +1,83 @@ +/* + * libvirt-connection.h: The PHP bindings to libvirt connection API + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_CONNECTION_H__ +# define __LIBVIRT_CONNECTION_H__ + +# include "util.h" + +# define PHP_LIBVIRT_CONNECTION_RES_NAME "Libvirt connection" +# define INT_RESOURCE_CONNECTION 0x01 +# define CONNECT_FLAG_SOUNDHW_GET_NAMES 0x01 + +# define PHP_FE_LIBVIRT_CONNECTION = \ + PHP_FE(libvirt_connect, arginfo_libvirt_connect) = \ + PHP_FE(libvirt_connect_get_uri, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_connect_get_hostname, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_connect_get_hypervisor, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_connect_get_capabilities, arginfo_libvirt_conn_xpat= h) \ + PHP_FE(libvirt_connect_get_emulator, arginfo_libvirt_connect_g= et_emulator) \ + PHP_FE(libvirt_connect_get_nic_models, arginfo_libvirt_connect_g= et_emulator) \ + PHP_FE(libvirt_connect_get_soundhw_models, arginfo_libvirt_connect_g= et_soundhw_models) \ + PHP_FE(libvirt_connect_get_maxvcpus, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_connect_get_sysinfo, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_connect_get_encrypted, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_connect_get_secure, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_connect_get_information, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_connect_get_machine_types, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_connect_get_all_domain_stats, arginfo_libvirt_connect_g= et_all_domain_stats) + +# define GET_CONNECTION_FROM_ARGS(args, ...) = \ + do { = \ + reset_error(TSRMLS_C); = \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, = \ + args, = \ + __VA_ARGS__) =3D=3D FAILURE) { = \ + set_error("Invalid arguments" TSRMLS_CC); = \ + RETURN_FALSE; = \ + } = \ + = \ + VIRT_FETCH_RESOURCE(conn, php_libvirt_connection*, &zconn, = \ + PHP_LIBVIRT_CONNECTION_RES_NAME, = \ + le_libvirt_connection); = \ + = \ + if (conn =3D=3D NULL || conn->conn =3D=3D NULL) = \ + RETURN_FALSE; = \ + } while (0) + +int le_libvirt_connection; + +typedef struct _php_libvirt_connection { + virConnectPtr conn; + virt_resource_handle resource; +} php_libvirt_connection; + +typedef struct _php_libvirt_cred_value { + int count; + int type; + char *result; + unsigned int resultlen; +} php_libvirt_cred_value; + +void php_libvirt_connection_dtor(virt_resource *rsrc TSRMLS_DC); + +PHP_FUNCTION(libvirt_connect); +PHP_FUNCTION(libvirt_connect_get_uri); +PHP_FUNCTION(libvirt_connect_get_hostname); +PHP_FUNCTION(libvirt_connect_get_hypervisor); +PHP_FUNCTION(libvirt_connect_get_capabilities); +PHP_FUNCTION(libvirt_connect_get_emulator); +PHP_FUNCTION(libvirt_connect_get_nic_models); +PHP_FUNCTION(libvirt_connect_get_soundhw_models); +PHP_FUNCTION(libvirt_connect_get_maxvcpus); +PHP_FUNCTION(libvirt_connect_get_sysinfo); +PHP_FUNCTION(libvirt_connect_get_encrypted); +PHP_FUNCTION(libvirt_connect_get_secure); +PHP_FUNCTION(libvirt_connect_get_information); +PHP_FUNCTION(libvirt_connect_get_machine_types); +PHP_FUNCTION(libvirt_connect_get_all_domain_stats); + +#endif diff --git a/src/libvirt-php.c b/src/libvirt-php.c index aea69e7..3e11e9d 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -20,11 +20,11 @@ #include "util.h" #include "vncfunc.h" #include "sockets.h" +#include "libvirt-connection.h" =20 DEBUG_INIT("core"); =20 -/* PHP functions are prefixed with `zif_` so strip it */ -#define PHPFUNC (__FUNCTION__ + 4) + =20 #ifndef EXTWIN /* Additional binaries */ @@ -36,7 +36,6 @@ const char *features_binaries[] =3D { NULL }; #endif =20 /* ZEND thread safe per request globals definition */ -int le_libvirt_connection; int le_libvirt_domain; int le_libvirt_storagepool; int le_libvirt_volume; @@ -478,21 +477,7 @@ static zend_function_entry libvirt_functions[] =3D { /* Common functions */ PHP_FE(libvirt_get_last_error, arginfo_libvirt_void) /* Connect functions */ - PHP_FE(libvirt_connect, arginfo_libvirt_connect) - PHP_FE(libvirt_connect_get_uri, arginfo_libvirt_conn) - PHP_FE(libvirt_connect_get_hostname, arginfo_libvirt_conn) - PHP_FE(libvirt_connect_get_capabilities, arginfo_libvirt_conn_xpat= h) - PHP_FE(libvirt_connect_get_emulator, arginfo_libvirt_connect_g= et_emulator) - PHP_FE(libvirt_connect_get_nic_models, arginfo_libvirt_connect_g= et_emulator) - PHP_FE(libvirt_connect_get_soundhw_models, arginfo_libvirt_connect_g= et_soundhw_models) - PHP_FE(libvirt_connect_get_information, arginfo_libvirt_conn) - PHP_FE(libvirt_connect_get_machine_types, arginfo_libvirt_conn) - PHP_FE(libvirt_connect_get_hypervisor, arginfo_libvirt_conn) - PHP_FE(libvirt_connect_get_sysinfo, arginfo_libvirt_conn) - PHP_FE(libvirt_connect_get_maxvcpus, arginfo_libvirt_conn) - PHP_FE(libvirt_connect_get_encrypted, arginfo_libvirt_conn) - PHP_FE(libvirt_connect_get_secure, arginfo_libvirt_conn) - PHP_FE(libvirt_connect_get_all_domain_stats, arginfo_libvirt_connect_g= et_all_domain_stats) + PHP_FE_LIBVIRT_CONNECTION /* Stream functions */ PHP_FE(libvirt_stream_create, arginfo_libvirt_conn) PHP_FE(libvirt_stream_close, arginfo_libvirt_conn) @@ -703,21 +688,6 @@ zend_module_entry libvirt_module_entry =3D { ZEND_GET_MODULE(libvirt) #endif =20 -ZEND_BEGIN_MODULE_GLOBALS(libvirt) - char *last_error; - char *vnc_location; - zend_bool longlong_to_string_ini; - char *iso_path_ini; - char *image_path_ini; - zend_long max_connections_ini; -#ifdef DEBUG_SUPPORT - int debug; -#endif - resource_info *binding_resources; - int binding_resources_count; -ZEND_END_MODULE_GLOBALS(libvirt) - -ZEND_DECLARE_MODULE_GLOBALS(libvirt) =20 /* PHP init options */ PHP_INI_BEGIN() @@ -729,7 +699,9 @@ PHP_INI_END() =20 void change_debug(int val TSRMLS_DC) { +#ifdef DEBUG_SUPPORT LIBVIRT_G(debug) =3D val; +#endif setDebug(val); } =20 @@ -1245,28 +1217,6 @@ void free_resource(int type, void *mem TSRMLS_DC) } =20 /* - * Private function name: free_resources_on_connection - * Since version: 0.4.2 - * Description: Function is used to free all the resources ass= igned to the connection identified by conn - * Arguments: @conn [virConnectPtr]: libvirt connection poin= ter - * Returns: None - */ -void free_resources_on_connection(virConnectPtr conn TSRMLS_DC) -{ - int binding_resources_count =3D 0; - resource_info *binding_resources; - int i; - - binding_resources_count =3D LIBVIRT_G(binding_resources_count); - binding_resources =3D LIBVIRT_G(binding_resources); - - for (i =3D 0; i < binding_resources_count; i++) { - if ((binding_resources[i].overwrite =3D=3D 0) && (binding_resource= s[i].conn =3D=3D conn)) - free_resource(binding_resources[i].type, binding_resources[i].= mem TSRMLS_CC); - } -} - -/* * Private function name: check_resource_allocation * Since version: 0.4.2 * Description: Function is used to check whether the resource= identified by type and memory is allocated for connection conn or not @@ -1383,30 +1333,6 @@ int is_local_connection(virConnectPtr conn) #endif } =20 -/* Destructor for connection resource */ -static void php_libvirt_connection_dtor(virt_resource *rsrc TSRMLS_DC) -{ - php_libvirt_connection *conn =3D (php_libvirt_connection *)rsrc->ptr; - int rv =3D 0; - - if (conn !=3D NULL) { - if (conn->conn !=3D NULL) { - free_resources_on_connection(conn->conn TSRMLS_CC); - - rv =3D virConnectClose(conn->conn); - if (rv =3D=3D -1) { - DPRINTF("%s: virConnectClose(%p) returned %d (%s)\n", __FU= NCTION__, conn->conn, rv, LIBVIRT_G(last_error)); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virConnectClo= se failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); - } else { - DPRINTF("%s: virConnectClose(%p) completed successfully\n"= , __FUNCTION__, conn->conn); - resource_change_counter(INT_RESOURCE_CONNECTION, NULL, con= n->conn, 0 TSRMLS_CC); - } - conn->conn =3D NULL; - } - efree(conn); - } -} - /* Destructor for domain resource */ static void php_libvirt_domain_dtor(virt_resource *rsrc TSRMLS_DC) { @@ -1950,19 +1876,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) } =20 /* Macros for obtaining resources from arguments */ -#define GET_CONNECTION_FROM_ARGS(args, ...) = \ - do { = \ - reset_error(TSRMLS_C); = \ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_AR= GS__) =3D=3D FAILURE) { \ - set_error("Invalid arguments" TSRMLS_CC); = \ - RETURN_FALSE; = \ - } = \ - = \ - VIRT_FETCH_RESOURCE(conn, php_libvirt_connection*, &zconn, PHP_LIB= VIRT_CONNECTION_RES_NAME, le_libvirt_connection);\ - if ((conn =3D=3D NULL) || (conn->conn =3D=3D NULL)) = \ - RETURN_FALSE; = \ - } while (0) = \ - #define GET_DOMAIN_FROM_ARGS(args, ...) = \ do { = \ reset_error(TSRMLS_C); = \ @@ -2080,40 +1993,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) VIRT_RETURN_STRING(tmpnumber); \ } while (0) =20 - -/* Authentication callback function. Should receive list of credentials vi= a cbdata and pass the requested one to libvirt */ -static int libvirt_virConnectAuthCallback(virConnectCredentialPtr cred, un= signed int ncred, void *cbdata) -{ - TSRMLS_FETCH(); - - unsigned int i, j; - php_libvirt_cred_value *creds =3D (php_libvirt_cred_value *) cbdata; - for (i =3D 0; i < (unsigned int)ncred; i++) { - DPRINTF("%s: cred %d, type %d, prompt %s challenge %s\n ", __FUNCT= ION__, i, cred[i].type, cred[i].prompt, cred[i].challenge); - if (creds !=3D NULL) - for (j =3D 0; j < (unsigned int)creds[0].count; j++) { - if (creds[j].type =3D=3D cred[i].type) { - cred[i].resultlen =3D creds[j].resultlen; - cred[i].result =3D (char *)malloc(creds[j].resultlen += 1); - memset(cred[i].result, 0, creds[j].resultlen + 1); - strncpy(cred[i].result, creds[j].result, creds[j].resu= ltlen); - } - } - DPRINTF("%s: result %s (%d)\n", __FUNCTION__, cred[i].result, cred= [i].resultlen); - } - - return 0; -} - -static int libvirt_virConnectCredType[] =3D { - VIR_CRED_AUTHNAME, - VIR_CRED_ECHOPROMPT, - VIR_CRED_REALM, - VIR_CRED_PASSPHRASE, - VIR_CRED_NOECHOPROMPT, - //VIR_CRED_EXTERNAL, -}; - /* Common functions */ =20 /* @@ -2130,117 +2009,6 @@ PHP_FUNCTION(libvirt_get_last_error) } =20 /* - * Function name: libvirt_connect - * Since version: 0.4.1(-1) - * Description: libvirt_connect() is used to connect to the specified = libvirt daemon using the specified URL, user can also set the readonly flag= and/or set credentials for connection - * Arguments: @url [string]: URI for connection - * @readonly [bool]: flag whether to use read-only connec= tion or not - * @credentials [array]: array of connection credentials - * Returns: libvirt connection resource - */ -PHP_FUNCTION(libvirt_connect) -{ - php_libvirt_connection *conn; - php_libvirt_cred_value *creds =3D NULL; - zval *zcreds =3D NULL; - zval *data; - int i; - int j; - int credscount =3D 0; - - virConnectAuth libvirt_virConnectAuth =3D { libvirt_virConnectCredType= , sizeof(libvirt_virConnectCredType)/sizeof(int), libvirt_virConnectAuthCal= lback, NULL}; - - char *url =3D NULL; - strsize_t url_len =3D 0; - zend_bool readonly =3D 1; - - HashTable *arr_hash; - HashPosition pointer; - int array_count; - - zend_ulong index; - - unsigned long libVer; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sba", &url, &ur= l_len, &readonly, &zcreds) =3D=3D FAILURE) { - RETURN_FALSE; - } - - if (virGetVersion(&libVer, NULL, NULL)!=3D 0) - RETURN_FALSE; - - if (libVer < 6002) { - set_error("Only libvirt 0.6.2 and higher supported. Please upgrade= your libvirt" TSRMLS_CC); - RETURN_FALSE; - } - - if ((count_resources(INT_RESOURCE_CONNECTION TSRMLS_CC) + 1) > LIBVIRT= _G(max_connections_ini)) { - DPRINTF("%s: maximum number of connections allowed exceeded (max %= lu)\n", PHPFUNC, (unsigned long)LIBVIRT_G(max_connections_ini)); - set_error("Maximum number of connections allowed exceeded" TSRMLS_= CC); - RETURN_FALSE; - } - - /* If 'null' value has been passed as URL override url to NULL value t= o autodetect the hypervisor */ - if ((url =3D=3D NULL) || (strcasecmp(url, "NULL") =3D=3D 0)) - url =3D NULL; - - conn =3D (php_libvirt_connection *)emalloc(sizeof(php_libvirt_connecti= on)); - if (zcreds =3D=3D NULL) { - /* connecting without providing authentication */ - if (readonly) - conn->conn =3D virConnectOpenReadOnly(url); - else - conn->conn =3D virConnectOpen(url); - } else { - /* connecting with authentication (using callback) */ - arr_hash =3D Z_ARRVAL_P(zcreds); - array_count =3D zend_hash_num_elements(arr_hash); - - credscount =3D array_count; - creds =3D (php_libvirt_cred_value *)emalloc(credscount * sizeof(ph= p_libvirt_cred_value)); - j =3D 0; - /* parse the input Array and create list of credentials. The list = (array) is passed to callback function. */ - VIRT_FOREACH(arr_hash, pointer, data) { - if (Z_TYPE_P(data) =3D=3D IS_STRING) { - php_libvirt_hash_key_info info; - VIRT_HASH_CURRENT_KEY_INFO(arr_hash, pointer, index, info); - - if (info.type =3D=3D HASH_KEY_IS_STRING) { - PHPWRITE(info.name, info.length); - } else { - DPRINTF("%s: credentials index %d\n", PHPFUNC, (int)in= dex); - creds[j].type =3D index; - creds[j].result =3D (char *)emalloc(Z_STRLEN_P(data) += 1); - memset(creds[j].result, 0, Z_STRLEN_P(data) + 1); - creds[j].resultlen =3D Z_STRLEN_P(data); - strncpy(creds[j].result, Z_STRVAL_P(data), Z_STRLEN_P(= data)); - j++; - } - } - } VIRT_FOREACH_END(); - DPRINTF("%s: Found %d elements for credentials\n", PHPFUNC, j); - creds[0].count =3D j; - libvirt_virConnectAuth.cbdata =3D (void *)creds; - conn->conn =3D virConnectOpenAuth(url, &libvirt_virConnectAuth, re= adonly ? VIR_CONNECT_RO : 0); - for (i =3D 0; i < creds[0].count; i++) - efree(creds[i].result); - efree(creds); - } - - if (conn->conn =3D=3D NULL) { - DPRINTF("%s: Cannot establish connection to %s\n", PHPFUNC, url); - efree(conn); - RETURN_FALSE; - } - - resource_change_counter(INT_RESOURCE_CONNECTION, NULL, conn->conn, 1 T= SRMLS_CC); - DPRINTF("%s: Connection to %s established, returning %p\n", PHPFUNC, u= rl, conn->conn); - - VIRT_REGISTER_RESOURCE(conn, le_libvirt_connection); - conn->resource =3D VIRT_RESOURCE_HANDLE(return_value); -} - -/* * Function name: libvirt_node_get_info * Since version: 0.4.1(-1) * Description: Function is used to get the information about host nod= e, mainly total memory installed, total CPUs installed and model informatio= n are useful @@ -2531,258 +2299,6 @@ PHP_FUNCTION(libvirt_node_get_free_memory) } } =20 -//virsh capabilities | xpath '//capabilities/guest/arch[@name=3D"x86_64"]/= machine[@maxCpus=3D1]' - -/* - * Function name: libvirt_connect_get_machine_types - * Since version: 0.4.9 - * Description: Function is used to get machine types supported by hyp= ervisor on the conneciton - * Arguments: @conn [resource]: resource for connection - * Returns: array of machine types for the connection incl. maxCpu= s if appropriate - */ -PHP_FUNCTION(libvirt_connect_get_machine_types) -{ - zval *zconn; - php_libvirt_connection *conn =3D NULL; - char *caps =3D NULL; - char **ret =3D NULL; - int i, num =3D -1; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - caps =3D virConnectGetCapabilities(conn->conn); - if (caps =3D=3D NULL) - RETURN_FALSE; - - array_init(return_value); - - ret =3D get_array_from_xpath(caps, "//capabilities/guest/arch/@name", = &num); - if (ret !=3D NULL) { - for (i =3D 0; i < num; i++) { - int num2, j; - char tmp[1024] =3D { 0 }; - - snprintf(tmp, sizeof(tmp), "//capabilities/guest/arch[@name=3D= \"%s\"]/domain/@type", ret[i]); - char **ret2 =3D get_array_from_xpath(caps, tmp, &num2); - if (ret2 !=3D NULL) { - zval *arr2; - VIRT_ARRAY_INIT(arr2); - - for (j =3D 0; j < num2; j++) { - int num3, k; - char tmp2[1024] =3D { 0 }; - zval *arr3; - - VIRT_ARRAY_INIT(arr3); - - snprintf(tmp2, sizeof(tmp2), "//capabilities/guest/arc= h[@name=3D\"%s\"]/machine", - ret[i]); - - char **ret3 =3D get_array_from_xpath(caps, tmp2, &num3= ); - if (ret3 !=3D NULL) { - for (k =3D 0; k < num3; k++) { - char *numTmp =3D NULL; - char key[8] =3D { 0 }; - char tmp3[2048] =3D { 0 }; - - snprintf(key, sizeof(key), "%d", k); - //VIRT_ADD_ASSOC_STRING(arr2, key, ret3[k]); - - snprintf(tmp3, sizeof(tmp3), "//capabilities/g= uest/arch[@name=3D\"%s\"]/machine[text()=3D\"%s\"]/@maxCpus", - ret[i], ret3[k]); - - numTmp =3D get_string_from_xpath(caps, tmp3, N= ULL, NULL); - if (numTmp =3D=3D NULL) { - VIRT_ADD_ASSOC_STRING(arr2, key, ret3[k]); - } else { - zval *arr4; - VIRT_ARRAY_INIT(arr4); - VIRT_ADD_ASSOC_STRING(arr4, "name", ret3[k= ]); - VIRT_ADD_ASSOC_STRING(arr4, "maxCpus", num= Tmp); - - add_assoc_zval_ex(arr2, key, strlen(key) += 1, arr4); - free(numTmp); - } - - free(ret3[k]); - } - } - - /* Domain type specific */ - snprintf(tmp2, sizeof(tmp2), "//capabilities/guest/arc= h[@name=3D\"%s\"]/domain[@type=3D\"%s\"]/machine", - ret[i], ret2[j]); - - ret3 =3D get_array_from_xpath(caps, tmp2, &num3); - if (ret3 !=3D NULL) { - for (k =3D 0; k < num3; k++) { - char key[8] =3D { 0 }; - char tmp3[2048] =3D { 0 }; - char *numTmp =3D NULL; - - snprintf(key, sizeof(key), "%d", k); - snprintf(tmp3, sizeof(tmp3), - "//capabilities/guest/arch[@name=3D\"= %s\"]/domain[@type=3D\"%s\"]/machine[text()=3D\"%s\"]/@maxCpus", - ret[i], ret2[j], ret3[k]); - - numTmp =3D get_string_from_xpath(caps, tmp3, N= ULL, NULL); - if (numTmp =3D=3D NULL) { - VIRT_ADD_ASSOC_STRING(arr3, key, ret3[k]); - } else { - zval *arr4; - VIRT_ARRAY_INIT(arr4); - - VIRT_ADD_ASSOC_STRING(arr4, "name", ret3[k= ]); - VIRT_ADD_ASSOC_STRING(arr4, "maxCpus", num= Tmp); - - add_assoc_zval_ex(arr3, key, strlen(key) += 1, arr4); - free(numTmp); - } - - free(ret3[k]); - } - - add_assoc_zval_ex(arr2, ret2[j], strlen(ret2[j]) += 1, arr3); - } - } - //free(ret2[j]); - - add_assoc_zval_ex(return_value, ret[i], strlen(ret[i]) + 1= , arr2); - } - free(ret[i]); - } - } -} - -/* - * Function name: libvirt_connect_get_information - * Since version: 0.4.1(-2) - * Description: Function is used to get the information about the conn= ection - * Arguments: @conn [resource]: resource for connection - * Returns: array of information about the connection - */ -PHP_FUNCTION(libvirt_connect_get_information) -{ - zval *zconn; - char *tmp; - unsigned long hvVer =3D 0; - const char *type =3D NULL; - char hvStr[64] =3D { 0 }; - int iTmp =3D -1, maxvcpus =3D -1; - php_libvirt_connection *conn =3D NULL; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - tmp =3D virConnectGetURI(conn->conn); - DPRINTF("%s: Got connection URI of %s...\n", PHPFUNC, tmp); - array_init(return_value); - VIRT_ADD_ASSOC_STRING(return_value, "uri", tmp ? tmp : "unknown"); - free(tmp); - tmp =3D virConnectGetHostname(conn->conn); - VIRT_ADD_ASSOC_STRING(return_value, "hostname", tmp ? tmp : "unknown"); - free(tmp); - - if ((virConnectGetVersion(conn->conn, &hvVer) =3D=3D 0) && (type =3D v= irConnectGetType(conn->conn))) { - VIRT_ADD_ASSOC_STRING(return_value, "hypervisor", (char *)type); - add_assoc_long(return_value, "hypervisor_major", (long)((hvVer/100= 0000) % 1000)); - add_assoc_long(return_value, "hypervisor_minor", (long)((hvVer/100= 0) % 1000)); - add_assoc_long(return_value, "hypervisor_release", (long)(hvVer % = 1000)); - snprintf(hvStr, sizeof(hvStr), "%s %d.%d.%d", type, - (long)((hvVer/1000000) % 1000), (long)((hvVer/1000) % 100= 0), (long)(hvVer % 1000)); - VIRT_ADD_ASSOC_STRING(return_value, "hypervisor_string", hvStr); - } - - if (strcmp(type, "QEMU") =3D=3D 0) { - /* For QEMU the value is not reliable so we return -1 instead */ - maxvcpus =3D -1; - } else { - maxvcpus =3D virConnectGetMaxVcpus(conn->conn, type); - } - - add_assoc_long(return_value, "hypervisor_maxvcpus", maxvcpus); - iTmp =3D virConnectIsEncrypted(conn->conn); - if (iTmp =3D=3D 1) - VIRT_ADD_ASSOC_STRING(return_value, "encrypted", "Yes"); - else - if (iTmp =3D=3D 0) - VIRT_ADD_ASSOC_STRING(return_value, "encrypted", "No"); - else - VIRT_ADD_ASSOC_STRING(return_value, "encrypted", "unknown"); - - iTmp =3D virConnectIsSecure(conn->conn); - if (iTmp =3D=3D 1) - VIRT_ADD_ASSOC_STRING(return_value, "secure", "Yes"); - else - if (iTmp =3D=3D 0) - VIRT_ADD_ASSOC_STRING(return_value, "secure", "No"); - else - VIRT_ADD_ASSOC_STRING(return_value, "secure", "unknown"); - - add_assoc_long(return_value, "num_inactive_domains", virConnectNumOfDe= finedDomains(conn->conn)); - add_assoc_long(return_value, "num_inactive_interfaces", virConnectNumO= fDefinedInterfaces(conn->conn)); - add_assoc_long(return_value, "num_inactive_networks", virConnectNumOfD= efinedNetworks(conn->conn)); - add_assoc_long(return_value, "num_inactive_storagepools", virConnectNu= mOfDefinedStoragePools(conn->conn)); - - add_assoc_long(return_value, "num_active_domains", virConnectNumOfDoma= ins(conn->conn)); - add_assoc_long(return_value, "num_active_interfaces", virConnectNumOfI= nterfaces(conn->conn)); - add_assoc_long(return_value, "num_active_networks", virConnectNumOfNet= works(conn->conn)); - add_assoc_long(return_value, "num_active_storagepools", virConnectNumO= fStoragePools(conn->conn)); - - add_assoc_long(return_value, "num_total_domains", virConnectNumOfDomai= ns(conn->conn) + virConnectNumOfDefinedDomains(conn->conn)); - add_assoc_long(return_value, "num_total_interfaces", virConnectNumOfIn= terfaces(conn->conn) + virConnectNumOfDefinedInterfaces(conn->conn)); - add_assoc_long(return_value, "num_total_networks", virConnectNumOfNetw= orks(conn->conn) + virConnectNumOfDefinedNetworks(conn->conn)); - add_assoc_long(return_value, "num_total_storagepools", virConnectNumOf= StoragePools(conn->conn) + virConnectNumOfDefinedStoragePools(conn->conn)); - - add_assoc_long(return_value, "num_secrets", virConnectNumOfSecrets(con= n->conn)); - add_assoc_long(return_value, "num_nwfilters", virConnectNumOfNWFilters= (conn->conn)); -} - -/* - * Function name: libvirt_connect_get_uri - * Since version: 0.4.1(-1) - * Description: Function is used to get the connection URI. This is us= eful to check the hypervisor type of host machine when using "null" uri to = libvirt_connect() - * Arguments: @conn [resource]: resource for connection - * Returns: connection URI string or FALSE for error - */ -PHP_FUNCTION(libvirt_connect_get_uri) -{ - zval *zconn; - char *uri; - php_libvirt_connection *conn =3D NULL; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - uri =3D virConnectGetURI(conn->conn); - DPRINTF("%s: virConnectGetURI returned %s\n", PHPFUNC, uri); - if (uri =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETVAL_STRING(uri); - free(uri); -} - -/* - * Function name: libvirt_connect_get_hostname - * Since version: 0.4.1(-1) - * Description: Function is used to get the hostname of the guest asso= ciated with the connection - * Arguments: @conn [resource]: resource for connection - * Returns: hostname of the host node or FALSE for error - */ -PHP_FUNCTION(libvirt_connect_get_hostname) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - char *hostname; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - hostname =3D virConnectGetHostname(conn->conn); - DPRINTF("%s: virConnectGetHostname returned %s\n", PHPFUNC, hostname); - if (hostname =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETVAL_STRING(hostname); - free(hostname); -} - /* * Function name: libvirt_image_create * Since version: 0.4.2 @@ -2904,197 +2420,6 @@ PHP_FUNCTION(libvirt_image_remove) } =20 /* - * Function name: libvirt_connect_get_hypervisor - * Since version: 0.4.1(-2) - * Description: Function is used to get the information about the hype= rvisor on the connection identified by the connection pointer - * Arguments: @conn [resource]: resource for connection - * Returns: array of hypervisor information if available - */ -PHP_FUNCTION(libvirt_connect_get_hypervisor) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - unsigned long hvVer =3D 0; - const char *type =3D NULL; - char hvStr[64] =3D { 0 }; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if (virConnectGetVersion(conn->conn, &hvVer) !=3D 0) - RETURN_FALSE; - - type =3D virConnectGetType(conn->conn); - if (type =3D=3D NULL) - RETURN_FALSE; - - DPRINTF("%s: virConnectGetType returned %s\n", PHPFUNC, type); - - array_init(return_value); - VIRT_ADD_ASSOC_STRING(return_value, "hypervisor", (char *)type); - add_assoc_long(return_value, "major", (long)((hvVer/1000000) % 1000)); - add_assoc_long(return_value, "minor", (long)((hvVer/1000) % 1000)); - add_assoc_long(return_value, "release", (long)(hvVer % 1000)); - - snprintf(hvStr, sizeof(hvStr), "%s %d.%d.%d", type, - (long)((hvVer/1000000) % 1000), (long)((hvVer/1000) % 1000), = (long)(hvVer % 1000)); - VIRT_ADD_ASSOC_STRING(return_value, "hypervisor_string", hvStr); -} - -/* - * Function name: libvirt_connect_is_encrypted - * Since version: 0.4.1(-2) - * Description: Function is used to get the information whether the co= nnection is encrypted or not - * Arguments: @conn [resource]: resource for connection - * Returns: 1 if encrypted, 0 if not encrypted, -1 on error - */ -PHP_FUNCTION(libvirt_connect_get_encrypted) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - RETURN_LONG(virConnectIsEncrypted(conn->conn)); -} - - -/* - * Function name: libvirt_connect_is_secure - * Since version: 0.4.1(-2) - * Description: Function is used to get the information whether the co= nnection is secure or not - * Arguments: @conn [resource]: resource for connection - * Returns: 1 if secure, 0 if not secure, -1 on error - */ -PHP_FUNCTION(libvirt_connect_get_secure) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - RETURN_LONG(virConnectIsSecure(conn->conn)); -} - - -/* - * Function name: libvirt_connect_get_all_domain_stats - * Since version: 0.5.1(-1) - * Description: Query statistics for all domains on a given connection - * Arguments: @conn [resource]: resource for connection - * @stats [int]: the statistic groups from VIR_DOMAIN_STA= TS_* - * @flags [int]: the filter flags from VIR_CONNECT_GET_AL= L_DOMAINS_STATS_* - * Returns: assoc array with statistics or false on error - */ -PHP_FUNCTION(libvirt_connect_get_all_domain_stats) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int retval =3D -1; - zend_long flags =3D 0; - zend_long stats =3D 0; - const char *name =3D NULL; - int i; - int j; - virTypedParameter params; - virDomainStatsRecordPtr *retstats =3D NULL; - - GET_CONNECTION_FROM_ARGS("r|l|l", &zconn, &stats, &flags); - - retval =3D virConnectGetAllDomainStats(conn->conn, stats, &retstats, f= lags); - - array_init(return_value); - if (retval < 0) - RETURN_FALSE; - - for (i =3D 0; i < retval; i++) { - zval *arr2; - VIRT_ARRAY_INIT(arr2); - - for (j =3D 0; j < retstats[i]->nparams; j++) { - params =3D retstats[i]->params[j]; - switch (params.type) { - case VIR_TYPED_PARAM_INT: - add_assoc_long(arr2, params.field, params.value.i); - break; - case VIR_TYPED_PARAM_UINT: - add_assoc_long(arr2, params.field, params.value.ui); - break; - case VIR_TYPED_PARAM_LLONG: - add_assoc_long(arr2, params.field, params.value.l); - break; - case VIR_TYPED_PARAM_ULLONG: - add_assoc_long(arr2, params.field, params.value.ul); - break; - case VIR_TYPED_PARAM_DOUBLE: - add_assoc_double(arr2, params.field, params.value.d); - break; - case VIR_TYPED_PARAM_BOOLEAN: - add_assoc_bool(arr2, params.field, params.value.b); - break; - case VIR_TYPED_PARAM_STRING: - VIRT_ADD_ASSOC_STRING(arr2, params.field, params.value.s); - break; - } - } - name =3D virDomainGetName(retstats[i]->dom); -#if PHP_MAJOR_VERSION >=3D 7 - zend_hash_update(Z_ARRVAL_P(return_value), zend_string_init(name, = strlen(name), 0), arr2); -#else - zend_hash_update(Z_ARRVAL_P(return_value), name, strlen(name)+1, &= arr2, sizeof(arr2), NULL); -#endif - } - - virDomainStatsRecordListFree(retstats); -} - - - -/* - * Function name: libvirt_connect_get_maxvcpus - * Since version: 0.4.1(-2) - * Description: Function is used to get maximum number of VCPUs per VM= on the hypervisor connection - * Arguments: @conn [resource]: resource for connection - * Returns: number of VCPUs available per VM on the connection or = FALSE for error - */ -PHP_FUNCTION(libvirt_connect_get_maxvcpus) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - const char *type =3D NULL; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - type =3D virConnectGetType(conn->conn); - if (type =3D=3D NULL) - RETURN_FALSE; - - RETURN_LONG(virConnectGetMaxVcpus(conn->conn, type)); -} - -/* - * Function name: libvirt_connect_get_sysinfo - * Since version: 0.4.1(-2) - * Description: Function is used to get the system information from co= nnection if available - * Arguments: @conn [resource]: resource for connection - * Returns: XML description of system information from the connect= ion or FALSE for error - */ -PHP_FUNCTION(libvirt_connect_get_sysinfo) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - char *sysinfo; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - sysinfo =3D virConnectGetSysinfo(conn->conn, 0); - if (sysinfo =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETVAL_STRING(sysinfo); - free(sysinfo); -} - -/* * Private function name: get_string_from_xpath * Since version: 0.4.1(-1) * Description: Function is used to get the XML xPath expressi= on from the XML document. This can be added to val array if not NULL. @@ -5102,240 +4427,6 @@ PHP_FUNCTION(libvirt_domain_get_next_dev_ids) add_assoc_long(return_value, "next_func", func); } =20 -/* - * Function name: libvirt_connect_get_capabilities - * Since version: 0.4.1(-2) - * Description: Function is used to get the capabilities information f= rom the connection - * Arguments: @conn [resource]: resource for connection - * @xpath [string]: optional xPath query to be applied on= the result - * Returns: capabilities XML from the connection or FALSE for error - */ -PHP_FUNCTION(libvirt_connect_get_capabilities) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - char *caps; - char *xpath =3D NULL; - strsize_t xpath_len; - char *tmp =3D NULL; - int retval =3D -1; - - GET_CONNECTION_FROM_ARGS("r|s", &zconn, &xpath, &xpath_len); - - caps =3D virConnectGetCapabilities(conn->conn); - if (caps =3D=3D NULL) - RETURN_FALSE; - - tmp =3D get_string_from_xpath(caps, xpath, NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - VIRT_RETVAL_STRING(caps); - } else { - VIRT_RETVAL_STRING(tmp); - } - - free(caps); - free(tmp); -} - -/* - * Function name: libvirt_connect_get_emulator - * Since version: 0.4.5 - * Description: Function is used to get the emulator for requested con= nection/architecture - * Arguments: @conn [resource]: libvirt connection resource - * @arch [string]: optional architecture string, can be N= ULL to get default - * Returns: path to the emulator - */ -PHP_FUNCTION(libvirt_connect_get_emulator) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - char *arch =3D NULL; - strsize_t arch_len; - char *tmp; - - GET_CONNECTION_FROM_ARGS("r|s", &zconn, &arch, &arch_len); - - if ((arch =3D=3D NULL) || (arch_len =3D=3D 0)) - arch =3D NULL; - - tmp =3D connection_get_emulator(conn->conn, arch TSRMLS_CC); - if (tmp =3D=3D NULL) { - set_error("Cannot get emulator" TSRMLS_CC); - RETURN_FALSE; - } - - VIRT_RETVAL_STRING(tmp); - free(tmp); -} - -/* - * Function name: libvirt_connect_get_nic_models - * Since version: 0.4.9 - * Description: Function is used to get NIC models for requested conne= ction/architecture - * Arguments: @conn [resource]: libvirt connection resource - * @arch [string]: optional architecture string, can be N= ULL to get default - * Returns: array of models - */ -PHP_FUNCTION(libvirt_connect_get_nic_models) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - char *arch =3D NULL; - strsize_t arch_len; - char *tmp; - - GET_CONNECTION_FROM_ARGS("r|s", &zconn, &arch, &arch_len); - - /* Disable getting it on remote connections */ - if (!is_local_connection(conn->conn)) { - set_error("This function works only on local connections" TSRMLS_C= C); - RETURN_FALSE; - } - - /* This approach is working only for QEMU driver so bails if not curre= ntly using it */ - if (strcmp(virConnectGetType(conn->conn), "QEMU") !=3D 0) { - set_error("This function can be used only for QEMU driver" TSRMLS_= CC); - RETURN_FALSE; - } - -#ifndef EXTWIN - if ((arch =3D=3D NULL) || (arch_len =3D=3D 0)) - arch =3D NULL; - - tmp =3D connection_get_emulator(conn->conn, arch TSRMLS_CC); - if (tmp =3D=3D NULL) { - set_error("Cannot get emulator" TSRMLS_CC); - RETURN_FALSE; - } - - char cmd[4096] =3D { 0 }; - char tmp2[16] =3D { 0 }; - snprintf(cmd, sizeof(cmd), "%s -net nic,model=3D? 2>&1", tmp); - - FILE *fp =3D popen(cmd, "r"); - if (fp =3D=3D NULL) - RETURN_FALSE; - - array_init(return_value); - while (!feof(fp)) { - memset(cmd, 0, sizeof(cmd)); - if (!fgets(cmd, sizeof(cmd), fp)) - break; - - if ((tmp =3D strstr(cmd, "Supported NIC models:")) !=3D NULL) { - tmp =3D strstr(tmp, ":") + 2; - - int i; - tTokenizer t =3D tokenize(tmp, ","); - for (i =3D 0; i < t.numTokens; i++) { - snprintf(tmp2, sizeof(tmp2), "%d", i); - VIRT_ADD_ASSOC_STRING(return_value, tmp2, t.tokens[i]); - } - free_tokens(t); - } - } - fclose(fp); -#endif -} - -/* - * Function name: libvirt_connect_get_soundhw_models - * Since version: 0.4.9 - * Description: Function is used to get sound hardware models for requ= ested connection/architecture - * Arguments: @conn [resource]: libvirt connection resource - * @arch [string]: optional architecture string, can be N= ULL to get default - * @flags [int]: flags for getting sound hardware. Can be= either 0 or VIR_CONNECT_SOUNDHW_GET_NAMES - * Returns: array of models - */ -PHP_FUNCTION(libvirt_connect_get_soundhw_models) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - char *arch =3D NULL; - strsize_t arch_len; - char *tmp; - zend_long flags =3D 0; - - GET_CONNECTION_FROM_ARGS("r|sl", &zconn, &arch, &arch_len, &flags); - - if ((arch =3D=3D NULL) || (arch_len =3D=3D 0)) - arch =3D NULL; - - /* Disable getting it on remote connections */ - if (!is_local_connection(conn->conn)) { - set_error("This function works only on local connections" TSRMLS_C= C); - RETURN_FALSE; - } - -#ifndef EXTWIN - /* This approach is working only for QEMU driver so bails if not curre= ntly using it */ - if (strcmp(virConnectGetType(conn->conn), "QEMU") !=3D 0) { - set_error("This function can be used only for QEMU driver" TSRMLS_= CC); - RETURN_FALSE; - } - - tmp =3D connection_get_emulator(conn->conn, arch TSRMLS_CC); - if (tmp =3D=3D NULL) { - set_error("Cannot get emulator" TSRMLS_CC); - RETURN_FALSE; - } - - char cmd[4096] =3D { 0 }; - snprintf(cmd, sizeof(cmd), "%s -soundhw help 2>&1", tmp); - - FILE *fp =3D popen(cmd, "r"); - if (fp =3D=3D NULL) - RETURN_FALSE; - - short inFunc =3D 0; - - int n =3D 0; - array_init(return_value); - while (!feof(fp)) { - memset(cmd, 0, sizeof(cmd)); - if (!fgets(cmd, sizeof(cmd), fp)) - break; - - if (strncmp(cmd, "Valid ", 6) =3D=3D 0) { - inFunc =3D 1; - continue; - } else - if (strlen(cmd) < 2) - inFunc =3D 0; - - if (inFunc) { - int i =3D 0; - char desc[1024] =3D { 0 }; - tTokenizer t =3D tokenize(cmd, " "); - if (t.numTokens =3D=3D 0) - continue; - - if ((i > 0) && (flags & CONNECT_FLAG_SOUNDHW_GET_NAMES)) { - zval *arr; - memset(desc, 0, sizeof(desc)); - for (i =3D 1; i < t.numTokens; i++) { - strcat(desc, t.tokens[i]); - if (i < t.numTokens - 1) - strcat(desc, " "); - } - - 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); - } else { - char tmp2[16] =3D { 0 }; - snprintf(tmp2, sizeof(tmp2), "%d", n++); - VIRT_ADD_ASSOC_STRING(return_value, tmp2, t.tokens[0]); - } - - free_tokens(t); - } - } - fclose(fp); -#endif -} - void parse_array(zval *arr, tVMDisk *disk, tVMNetwork *network) { HashTable *arr_hash; diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 66be53a..6cbcd27 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -7,7 +7,6 @@ #ifndef PHP_LIBVIRT_H #define PHP_LIBVIRT_H 1 =20 - /* Network constants */ #define VIR_NETWORKS_ACTIVE 1 #define VIR_NETWORKS_INACTIVE 2 @@ -50,10 +49,10 @@ #endif =20 #ifndef VERSION -#define VERSION "0.5.1" +#define VERSION "0.5.5" #define VERSION_MAJOR 0 #define VERSION_MINOR 5 -#define VERSION_MICRO 1 +#define VERSION_MICRO 5 #endif =20 #include @@ -109,17 +108,15 @@ typedef uint64_t arch_uint; #define UINTx PRIx64 #endif =20 -typedef struct tTokenizer { - char **tokens; - int numTokens; -} tTokenizer; +# define DEBUG_SUPPORT =20 -typedef struct _resource_info { - int type; - virConnectPtr conn; - void *mem; - int overwrite; -} resource_info; +# ifdef DEBUG_SUPPORT +# define DEBUG_CORE +# define DEBUG_VNC +# endif + +/* PHP functions are prefixed with `zif_` so strip it */ +# define PHPFUNC (__FUNCTION__ + 4) =20 #ifdef ZTS #define LIBVIRT_G(v) TSRMG(libvirt_globals_id, zend_libvirt_globals *, v) @@ -130,9 +127,6 @@ typedef struct _resource_info { #define PHP_LIBVIRT_WORLD_VERSION VERSION #define PHP_LIBVIRT_WORLD_EXTNAME "libvirt" =20 -/* Connect flags */ -#define CONNECT_FLAG_SOUNDHW_GET_NAMES 0x01 - /* Domain flags */ #define DOMAIN_FLAG_FEATURE_ACPI 0x01 #define DOMAIN_FLAG_FEATURE_APIC 0x02 @@ -147,7 +141,6 @@ typedef struct _resource_info { #define DOMAIN_DISK_ACCESS_ALL 0x04 =20 /* Internal resource identifier objects */ -#define INT_RESOURCE_CONNECTION 0x01 #define INT_RESOURCE_DOMAIN 0x02 #define INT_RESOURCE_NETWORK 0x04 #define INT_RESOURCE_NODEDEV 0x08 @@ -157,6 +150,18 @@ typedef struct _resource_info { #define INT_RESOURCE_STREAM 0x50 #define INT_RESOURCE_NWFILTER 0x60 =20 +typedef struct tTokenizer { + char **tokens; + int numTokens; +} tTokenizer; + +typedef struct _resource_info { + int type; + virConnectPtr conn; + void *mem; + int overwrite; +} resource_info; + typedef struct tVMDisk { char *path; char *driver; @@ -172,22 +177,10 @@ typedef struct tVMNetwork { char *model; } tVMNetwork; =20 -/* TODO: Temporary due to code movement to util.h - will be removed when - * libvirt-connection.h is separated out - */ -#if PHP_MAJOR_VERSION >=3D 7 -typedef zend_resource virt_resource; -typedef virt_resource *virt_resource_handle; -#else -typedef long virt_resource_handle; -#endif +/* TODO: temporary forward declaration until other parts are "modularized"= */ +typedef struct _php_libvirt_connection php_libvirt_connection; =20 /* Libvirt-php types */ -typedef struct _php_libvirt_connection { - virConnectPtr conn; - virt_resource_handle resource; -} php_libvirt_connection; - typedef struct _php_libvirt_stream { virStreamPtr stream; php_libvirt_connection* conn; @@ -228,25 +221,42 @@ typedef struct _php_libvirt_nwfilter { php_libvirt_connection* conn; } php_libvirt_nwfilter; =20 -typedef struct _php_libvirt_cred_value { - int count; - int type; - char *result; - unsigned int resultlen; -} php_libvirt_cred_value; - typedef struct _php_libvirt_hash_key_info { char *name; unsigned int length; unsigned int type; } php_libvirt_hash_key_info; =20 +ZEND_BEGIN_MODULE_GLOBALS(libvirt) + char *last_error; + char *vnc_location; + zend_bool longlong_to_string_ini; + char *iso_path_ini; + char *image_path_ini; + zend_long max_connections_ini; +# ifdef DEBUG_SUPPORT + int debug; +# endif + resource_info *binding_resources; + int binding_resources_count; +ZEND_END_MODULE_GLOBALS(libvirt) + +ZEND_DECLARE_MODULE_GLOBALS(libvirt) + /* Private definitions */ +void set_error(char *msg TSRMLS_DC); +void reset_error(TSRMLS_D); +int count_resources(int type TSRMLS_DC); +int resource_change_counter(int type, virConnectPtr conn, void *mem, int i= nc TSRMLS_DC); +void free_resource(int type, void *mem TSRMLS_DC); +char *connection_get_emulator(virConnectPtr conn, char *arch TSRMLS_DC); +int is_local_connection(virConnectPtr conn); +tTokenizer tokenize(char *string, char *by); +void free_tokens(tTokenizer t); int set_logfile(char *filename, long maxsize TSRMLS_DC); char *get_string_from_xpath(char *xml, char *xpath, zval **val, int *retVa= l); char **get_array_from_xpath(char *xml, char *xpath, int *num); =20 -#define PHP_LIBVIRT_CONNECTION_RES_NAME "Libvirt connection" #define PHP_LIBVIRT_DOMAIN_RES_NAME "Libvirt domain" #define PHP_LIBVIRT_STREAM_RES_NAME "Libvirt stream" #define PHP_LIBVIRT_STORAGEPOOL_RES_NAME "Libvirt storagepool" @@ -264,22 +274,6 @@ PHP_MINFO_FUNCTION(libvirt); =20 /* Common functions */ PHP_FUNCTION(libvirt_get_last_error); -/* Connect functions */ -PHP_FUNCTION(libvirt_connect); -PHP_FUNCTION(libvirt_connect_get_uri); -PHP_FUNCTION(libvirt_connect_get_hostname); -PHP_FUNCTION(libvirt_connect_get_hypervisor); -PHP_FUNCTION(libvirt_connect_get_capabilities); -PHP_FUNCTION(libvirt_connect_get_emulator); -PHP_FUNCTION(libvirt_connect_get_nic_models); -PHP_FUNCTION(libvirt_connect_get_soundhw_models); -PHP_FUNCTION(libvirt_connect_get_maxvcpus); -PHP_FUNCTION(libvirt_connect_get_sysinfo); -PHP_FUNCTION(libvirt_connect_get_encrypted); -PHP_FUNCTION(libvirt_connect_get_secure); -PHP_FUNCTION(libvirt_connect_get_information); -PHP_FUNCTION(libvirt_connect_get_machine_types); -PHP_FUNCTION(libvirt_connect_get_all_domain_stats); /* Node functions */ PHP_FUNCTION(libvirt_node_get_info); PHP_FUNCTION(libvirt_node_get_cpu_stats); diff --git a/src/util.h b/src/util.h index 6400164..167bd90 100644 --- a/src/util.h +++ b/src/util.h @@ -10,13 +10,6 @@ # include # include "libvirt-php.h" =20 -# define DEBUG_SUPPORT - -# ifdef DEBUG_SUPPORT -# define DEBUG_CORE -# define DEBUG_VNC -# endif - # define DEBUG_INIT(source) \ static const char *debugSource =3D "" source "" =20 --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 23:55:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501624249413332.69454874480334; Tue, 1 Aug 2017 14:50:49 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 88D3F64E94; Tue, 1 Aug 2017 21:50:47 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 62A431820A; Tue, 1 Aug 2017 21:50:47 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id D7CC3180B467; Tue, 1 Aug 2017 21:50:46 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkSjV007860 for ; Tue, 1 Aug 2017 17:46:28 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1158E63777; Tue, 1 Aug 2017 21:46:28 +0000 (UTC) Received: from mx1.redhat.com (ext-mx09.extmail.prod.ext.phx2.redhat.com [10.5.110.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 031EB4DA95 for ; Tue, 1 Aug 2017 21:46:27 +0000 (UTC) Received: from mail-qt0-f194.google.com (mail-qt0-f194.google.com [209.85.216.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 552C3CAA88 for ; Tue, 1 Aug 2017 21:46:20 +0000 (UTC) Received: by mail-qt0-f194.google.com with SMTP id p3so2910504qtg.5 for ; Tue, 01 Aug 2017 14:46:20 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.18 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:18 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 88D3F64E94 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 88D3F64E94 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ly2Dcxi5" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 552C3CAA88 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 552C3CAA88 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=w9NpXS9+TmrdNHnufCbRmERmddsHjQxwEw2zeNPWueY=; b=ly2Dcxi5VyrdKWZqIdO/xe9Ci+YhfUI5P7iCo7fm/Wf3XKOh9YO+q4KoKpH2pYpxCz h0Yv5XNXflWQy7zQsrW6ZEST9zRMxNGL8sevvz3fzFa3rqaEckZ242pxxWvxPJRI4bLX 0rGLbo4zTYNzg2OXTBIyzn+v+f+ITCh6vg1tsuveoiNEyQPrZNO9EhU5GlvbN8XdktFv fBewAzn7EGk96ii11OhDtcyd9WtYP2RjYLVwyZ1iAwekgoJo1Obkm4tX+MqmmhGuTZvR DNrHFe8yeXV0jiA7Hn0dzH+Lp8DShk8h1zaHCkpzWkH09i6k5dYWIbcCw9hGKhe+hMes Aa0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=w9NpXS9+TmrdNHnufCbRmERmddsHjQxwEw2zeNPWueY=; b=BCO5tj6RjAG7ffkATsAiWf2hzNvD5rxGnP6GwOVStluOhCHeSRZG/V/SzqRUNZKkVd E6mfBX/5npyQOpnmtG8Eflo1PvkzFaJe9noQ3Yga7DqeN3y/ltPzOaWi/BlFoyOWccnY yp/DkXIJKnxc9TERpZX9OTXudAIW+5BvG2M1rvan2sX8TJHktG/zVPmlFVZQYlPTwieG SBHIW81q0RgTA3jprd/JTCArvNXf0xEj+208NZtB+T4LVMmPNXJ1rCVlSekTd88G0nSC apCY6VNF8XaRlmXXdehah4NAMNKz8jhKcbB1PldB0zqxrrYooPM5BO5NO3sGUoa5BZGB Q/wg== X-Gm-Message-State: AIVw112gYS8VAwyyte/DSH/G3lIliWXHEkEi5o1ljArZXX+ln1xdOAJ4 2QQRIuV6Uk0LweNV+hE= X-Received: by 10.200.35.23 with SMTP id a23mr28491032qta.56.1501623979102; Tue, 01 Aug 2017 14:46:19 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:06 -0400 Message-Id: <20170801214614.8915-6-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 01 Aug 2017 21:46:20 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 01 Aug 2017 21:46:20 +0000 (UTC) for IP:'209.85.216.194' DOMAIN:'mail-qt0-f194.google.com' HELO:'mail-qt0-f194.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.37 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.216.194 mail-qt0-f194.google.com 209.85.216.194 mail-qt0-f194.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.38 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 05/13] Split up the bindings for libvirt node API X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 01 Aug 2017 21:50:48 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" --- src/Makefile.am | 3 +- src/libvirt-node.c | 304 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-node.h | 25 +++++ src/libvirt-php.c | 322 +------------------------------------------------= ---- src/libvirt-php.h | 6 - src/util.h | 25 +++++ 6 files changed, 358 insertions(+), 327 deletions(-) create mode 100644 src/libvirt-node.c create mode 100644 src/libvirt-node.h diff --git a/src/Makefile.am b/src/Makefile.am index 0819dc6..ba2be62 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -22,7 +22,8 @@ libvirt_php_la_SOURCES =3D \ vncfunc.c vncfunc.h \ sockets.c sockets.h \ libvirt-php.c libvirt-php.h \ - libvirt-connection.c libvirt-connection.h + libvirt-connection.c libvirt-connection.h \ + libvirt-node.c libvirt-node.h libvirt_php_la_CFLAGS =3D \ $(AM_CFLAGS) \ -DCOMPILE_DL_LIBVIRT=3D1 diff --git a/src/libvirt-node.c b/src/libvirt-node.c new file mode 100644 index 0000000..a6de1ac --- /dev/null +++ b/src/libvirt-node.c @@ -0,0 +1,304 @@ +/* + * libvirt-node.c: The PHP bindings to libvirt connection API + * + * See COPYING for the license of this software + */ + +#include + +#include "libvirt-node.h" +#include "libvirt-connection.h" + +DEBUG_INIT("node"); + +/* + * Function name: libvirt_node_get_info + * Since version: 0.4.1(-1) + * Description: Function is used to get the information about host nod= e, mainly total memory installed, total CPUs installed and model informatio= n are useful + * Arguments: @conn [resource]: resource for connection + * Returns: array of node information or FALSE for error + */ +PHP_FUNCTION(libvirt_node_get_info) +{ + virNodeInfo info; + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int retval; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + retval =3D virNodeGetInfo (conn->conn, &info); + DPRINTF("%s: virNodeGetInfo returned %d\n", PHPFUNC, retval); + if (retval =3D=3D -1) + RETURN_FALSE; + + array_init(return_value); + VIRT_ADD_ASSOC_STRING(return_value, "model", info.model); + 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); + add_assoc_long(return_value, "sockets", (long)info.sockets); + add_assoc_long(return_value, "cores", (long)info.cores); + add_assoc_long(return_value, "threads", (long)info.threads); + add_assoc_long(return_value, "mhz", (long)info.mhz); +} + +/* + * Function name: libvirt_node_get_cpu_stats + * Since version: 0.4.6 + * Description: Function is used to get the CPU stats per nodes + * Arguments: @conn [resource]: resource for connection + * @cpunr [int]: CPU number to get information about, def= aults to VIR_NODE_CPU_STATS_ALL_CPUS to get information about all CPUs + * Returns: array of node CPU statistics including time (in second= s since UNIX epoch), cpu number and total number of CPUs on node or FALSE f= or error + */ +PHP_FUNCTION(libvirt_node_get_cpu_stats) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int cpuNum =3D VIR_NODE_CPU_STATS_ALL_CPUS; + virNodeCPUStatsPtr params; + virNodeInfo info; + zend_long cpunr =3D -1; + int nparams =3D 0; + int i, j, numCpus; + + GET_CONNECTION_FROM_ARGS("r|l", &zconn, &cpunr); + + if (virNodeGetInfo(conn->conn, &info) !=3D 0) { + set_error("Cannot get number of CPUs" TSRMLS_CC); + RETURN_FALSE; + } + + numCpus =3D info.cpus; + if (cpunr > numCpus - 1) { + char tmp[256] =3D { 0 }; + snprintf(tmp, sizeof(tmp), "Invalid CPU number, valid numbers in r= ange 0 to %d or VIR_NODE_CPU_STATS_ALL_CPUS", + numCpus - 1); + set_error(tmp TSRMLS_CC); + + RETURN_FALSE; + } + + cpuNum =3D (int)cpunr; + + if (virNodeGetCPUStats(conn->conn, cpuNum, NULL, &nparams, 0) !=3D 0) { + set_error("Cannot get number of CPU stats" TSRMLS_CC); + RETURN_FALSE; + } + + if (nparams =3D=3D 0) + RETURN_TRUE; + + DPRINTF("%s: Number of parameters got from virNodeGetCPUStats is %d\n"= , __FUNCTION__, nparams); + + params =3D (virNodeCPUStatsPtr)calloc(nparams, nparams * sizeof(*param= s)); + + array_init(return_value); + for (i =3D 0; i < 2; i++) { + zval *arr; + + if (i > 0) +#ifdef EXTWIN + Sleep(1000); +#else + sleep(1); +#endif + + if (virNodeGetCPUStats(conn->conn, cpuNum, params, &nparams, 0) != =3D 0) { + set_error("Unable to get node cpu stats" TSRMLS_CC); + RETURN_FALSE; + } + + VIRT_ARRAY_INIT(arr); + + for (j =3D 0; j < nparams; j++) { + DPRINTF("%s: Field %s has value of %llu\n", __FUNCTION__, para= ms[j].field, params[j].value); + + add_assoc_long(arr, params[j].field, params[j].value); + } + + add_assoc_long(arr, "time", time(NULL)); + + add_index_zval(return_value, i, arr); + } + + add_assoc_long(return_value, "cpus", numCpus); + if (cpuNum >=3D 0) { + add_assoc_long(return_value, "cpu", cpunr); + } else { + if (cpuNum =3D=3D VIR_NODE_CPU_STATS_ALL_CPUS) + VIRT_ADD_ASSOC_STRING(return_value, "cpu", "all"); + else + VIRT_ADD_ASSOC_STRING(return_value, "cpu", "unknown"); + } + + free(params); + params =3D NULL; +} + +/* + * Function name: libvirt_node_get_cpu_stats_for_each_cpu + * Since version: 0.4.6 + * Description: Function is used to get the CPU stats for each CPU on = the host node + * Arguments: @conn [resource]: resource for connection + * @time [int]: time in seconds to get the information ab= out, without aggregation for further processing + * Returns: array of node CPU statistics for each CPU including ti= me (in seconds since UNIX epoch), cpu number and total number of CPUs on no= de or FALSE for error + */ +PHP_FUNCTION(libvirt_node_get_cpu_stats_for_each_cpu) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + virNodeCPUStatsPtr params; + virNodeInfo info; + int nparams =3D 0; + zend_long avg =3D 0, iter =3D 0; + int done =3D 0; + int i, j, numCpus; + time_t startTime =3D 0; + zval *time_array; + + GET_CONNECTION_FROM_ARGS("r|l", &zconn, &avg); + + if (virNodeGetInfo(conn->conn, &info) !=3D 0) { + set_error("Cannot get number of CPUs" TSRMLS_CC); + RETURN_FALSE; + } + + if (virNodeGetCPUStats(conn->conn, VIR_NODE_CPU_STATS_ALL_CPUS, NULL, = &nparams, 0) !=3D 0) { + set_error("Cannot get number of CPU stats" TSRMLS_CC); + RETURN_FALSE; + } + + if (nparams =3D=3D 0) + RETURN_TRUE; + + DPRINTF("%s: Number of parameters got from virNodeGetCPUStats is %d\n"= , __FUNCTION__, nparams); + + params =3D (virNodeCPUStatsPtr)calloc(nparams, nparams * sizeof(*param= s)); + + numCpus =3D info.cpus; + array_init(return_value); + + startTime =3D time(NULL); + + iter =3D 0; + done =3D 0; + while (!done) { + zval *arr; + VIRT_ARRAY_INIT(arr); + + for (i =3D 0; i < numCpus; i++) { + zval *arr2; + + if (virNodeGetCPUStats(conn->conn, i, params, &nparams, 0) != =3D 0) { + set_error("Unable to get node cpu stats" TSRMLS_CC); + RETURN_FALSE; + } + + VIRT_ARRAY_INIT(arr2); + + for (j =3D 0; j < nparams; j++) + add_assoc_long(arr2, params[j].field, params[j].value); + + add_assoc_long(arr, "time", time(NULL)); + add_index_zval(arr, i, arr2); + } + + add_index_zval(return_value, iter, arr); + + if ((avg <=3D 0) || (iter =3D=3D avg - 1)) { + done =3D 1; + break; + } + +#ifndef EXTWIN + sleep(1); +#else + Sleep(1000); +#endif + iter++; + } + + 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); + + add_assoc_zval(return_value, "times", time_array); + + free(params); + params =3D NULL; +} + +/* + * Function name: libvirt_node_get_mem_stats + * Since version: 0.4.6 + * Description: Function is used to get the memory stats per node + * Arguments: @conn [resource]: resource for connection + * Returns: array of node memory statistics including time (in sec= onds since UNIX epoch) or FALSE for error + */ +PHP_FUNCTION(libvirt_node_get_mem_stats) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int memNum =3D VIR_NODE_MEMORY_STATS_ALL_CELLS; + virNodeMemoryStatsPtr params; + int nparams =3D 0; + int j; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if (virNodeGetMemoryStats(conn->conn, memNum, NULL, &nparams, 0) !=3D = 0) { + set_error("Cannot get number of memory stats" TSRMLS_CC); + RETURN_FALSE; + } + + if (nparams =3D=3D 0) + RETURN_TRUE; + + DPRINTF("%s: Number of parameters got from virNodeGetMemoryStats is %d= \n", __FUNCTION__, nparams); + + params =3D (virNodeMemoryStatsPtr)calloc(nparams, nparams * sizeof(*pa= rams)); + + array_init(return_value); + if (virNodeGetMemoryStats(conn->conn, memNum, params, &nparams, 0) != =3D 0) { + set_error("Unable to get node memory stats" TSRMLS_CC); + RETURN_FALSE; + } + + for (j =3D 0; j < nparams; j++) { + DPRINTF("%s: Field %s has value of %llu\n", __FUNCTION__, params[j= ].field, params[j].value); + + add_assoc_long(return_value, params[j].field, params[j].value); + } + + add_assoc_long(return_value, "time", time(NULL)); + + free(params); + params =3D NULL; +} + +/* + * Function name: libvirt_node_get_free_memory + * Since version: 0.5.3 + * Description: Function is used to get free memory available on the n= ode. + * Arguments: @conn [resource]: resource for connection. + * Returns: The available free memery in bytes as string or FALSE = for error. + */ +PHP_FUNCTION(libvirt_node_get_free_memory) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + unsigned long long ret; + LONGLONG_INIT; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((ret =3D virNodeGetFreeMemory(conn->conn)) !=3D 0) { + LONGLONG_RETURN_AS_STRING(ret); + } else { + set_error("Cannot get the free memory for the node" TSRMLS_CC); + RETURN_FALSE; + } +} + diff --git a/src/libvirt-node.h b/src/libvirt-node.h new file mode 100644 index 0000000..63d79b6 --- /dev/null +++ b/src/libvirt-node.h @@ -0,0 +1,25 @@ +/* + * libvirt-node.h: The PHP bindings to libvirt node API + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_NODE_H__ +# define __LIBVIRT_NODE_H__ + +# include "libvirt-php.h"=20 + +# define PHP_FE_LIBVIRT_NODE = \ + PHP_FE(libvirt_node_get_info, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_node_get_cpu_stats, arginfo_libvirt_conn_o= ptcpunr) \ + PHP_FE(libvirt_node_get_cpu_stats_for_each_cpu, arginfo_libvirt_conn_o= pttime) \ + PHP_FE(libvirt_node_get_mem_stats, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_node_get_free_memory, arginfo_libvirt_conn) + +PHP_FUNCTION(libvirt_node_get_info); +PHP_FUNCTION(libvirt_node_get_cpu_stats); +PHP_FUNCTION(libvirt_node_get_cpu_stats_for_each_cpu); +PHP_FUNCTION(libvirt_node_get_mem_stats); +PHP_FUNCTION(libvirt_node_get_free_memory); + +#endif diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 3e11e9d..7e9c96b 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -21,6 +21,7 @@ #include "vncfunc.h" #include "sockets.h" #include "libvirt-connection.h" +#include "libvirt-node.h" =20 DEBUG_INIT("core"); =20 @@ -610,11 +611,7 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE(libvirt_network_get_autostart, arginfo_libvirt_conn) PHP_FE(libvirt_network_set_autostart, arginfo_libvirt_conn_flag= s) /* Node functions */ - PHP_FE(libvirt_node_get_info, arginfo_libvirt_conn) - PHP_FE(libvirt_node_get_cpu_stats, arginfo_libvirt_conn_optc= punr) - PHP_FE(libvirt_node_get_cpu_stats_for_each_cpu, arginfo_libvirt_conn_o= pttime) - PHP_FE(libvirt_node_get_mem_stats, arginfo_libvirt_conn) - PHP_FE(libvirt_node_get_free_memory, arginfo_libvirt_conn) + PHP_FE_LIBVIRT_NODE /* Nodedev functions */ PHP_FE(libvirt_nodedev_get, arginfo_libvirt_conn) PHP_FE(libvirt_nodedev_capabilities, arginfo_libvirt_conn) @@ -1968,30 +1965,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) RETURN_FALSE; = \ } while (0) = \ =20 -#define LONGLONG_INIT \ - char tmpnumber[64] - -#define LONGLONG_ASSOC(out, key, in) \ - if (LIBVIRT_G(longlong_to_string_ini)) { \ - snprintf(tmpnumber, 63, "%llu", in); \ - VIRT_ADD_ASSOC_STRING(out, key, tmpnumber); \ - } else { \ - add_assoc_long(out, key, in); \ - } - -#define LONGLONG_INDEX(out, key, in) \ - if (LIBVIRT_G(longlong_to_string_ini)) { \ - snprintf(tmpnumber, 63, "%llu", in); \ - VIRT_ADD_INDEX_STRING(out, key, tmpnumber); \ - } else { \ - add_index_long(out, key, in); \ - } - -#define LONGLONG_RETURN_AS_STRING(in) \ - do { \ - snprintf(tmpnumber, 63, "%llu", in); \ - VIRT_RETURN_STRING(tmpnumber); \ - } while (0) =20 /* Common functions */ =20 @@ -2009,297 +1982,6 @@ PHP_FUNCTION(libvirt_get_last_error) } =20 /* - * Function name: libvirt_node_get_info - * Since version: 0.4.1(-1) - * Description: Function is used to get the information about host nod= e, mainly total memory installed, total CPUs installed and model informatio= n are useful - * Arguments: @conn [resource]: resource for connection - * Returns: array of node information or FALSE for error - */ -PHP_FUNCTION(libvirt_node_get_info) -{ - virNodeInfo info; - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int retval; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - retval =3D virNodeGetInfo (conn->conn, &info); - DPRINTF("%s: virNodeGetInfo returned %d\n", PHPFUNC, retval); - if (retval =3D=3D -1) - RETURN_FALSE; - - array_init(return_value); - VIRT_ADD_ASSOC_STRING(return_value, "model", info.model); - 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); - add_assoc_long(return_value, "sockets", (long)info.sockets); - add_assoc_long(return_value, "cores", (long)info.cores); - add_assoc_long(return_value, "threads", (long)info.threads); - add_assoc_long(return_value, "mhz", (long)info.mhz); -} - -/* - * Function name: libvirt_node_get_cpu_stats - * Since version: 0.4.6 - * Description: Function is used to get the CPU stats per nodes - * Arguments: @conn [resource]: resource for connection - * @cpunr [int]: CPU number to get information about, def= aults to VIR_NODE_CPU_STATS_ALL_CPUS to get information about all CPUs - * Returns: array of node CPU statistics including time (in second= s since UNIX epoch), cpu number and total number of CPUs on node or FALSE f= or error - */ -PHP_FUNCTION(libvirt_node_get_cpu_stats) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int cpuNum =3D VIR_NODE_CPU_STATS_ALL_CPUS; - virNodeCPUStatsPtr params; - virNodeInfo info; - zend_long cpunr =3D -1; - int nparams =3D 0; - int i, j, numCpus; - - GET_CONNECTION_FROM_ARGS("r|l", &zconn, &cpunr); - - if (virNodeGetInfo(conn->conn, &info) !=3D 0) { - set_error("Cannot get number of CPUs" TSRMLS_CC); - RETURN_FALSE; - } - - numCpus =3D info.cpus; - if (cpunr > numCpus - 1) { - char tmp[256] =3D { 0 }; - snprintf(tmp, sizeof(tmp), "Invalid CPU number, valid numbers in r= ange 0 to %d or VIR_NODE_CPU_STATS_ALL_CPUS", - numCpus - 1); - set_error(tmp TSRMLS_CC); - - RETURN_FALSE; - } - - cpuNum =3D (int)cpunr; - - if (virNodeGetCPUStats(conn->conn, cpuNum, NULL, &nparams, 0) !=3D 0) { - set_error("Cannot get number of CPU stats" TSRMLS_CC); - RETURN_FALSE; - } - - if (nparams =3D=3D 0) - RETURN_TRUE; - - DPRINTF("%s: Number of parameters got from virNodeGetCPUStats is %d\n"= , __FUNCTION__, nparams); - - params =3D (virNodeCPUStatsPtr)calloc(nparams, nparams * sizeof(*param= s)); - - array_init(return_value); - for (i =3D 0; i < 2; i++) { - zval *arr; - - if (i > 0) -#ifdef EXTWIN - Sleep(1000); -#else - sleep(1); -#endif - - if (virNodeGetCPUStats(conn->conn, cpuNum, params, &nparams, 0) != =3D 0) { - set_error("Unable to get node cpu stats" TSRMLS_CC); - RETURN_FALSE; - } - - VIRT_ARRAY_INIT(arr); - - for (j =3D 0; j < nparams; j++) { - DPRINTF("%s: Field %s has value of %llu\n", __FUNCTION__, para= ms[j].field, params[j].value); - - add_assoc_long(arr, params[j].field, params[j].value); - } - - add_assoc_long(arr, "time", time(NULL)); - - add_index_zval(return_value, i, arr); - } - - add_assoc_long(return_value, "cpus", numCpus); - if (cpuNum >=3D 0) { - add_assoc_long(return_value, "cpu", cpunr); - } else { - if (cpuNum =3D=3D VIR_NODE_CPU_STATS_ALL_CPUS) - VIRT_ADD_ASSOC_STRING(return_value, "cpu", "all"); - else - VIRT_ADD_ASSOC_STRING(return_value, "cpu", "unknown"); - } - - free(params); - params =3D NULL; -} - -/* - * Function name: libvirt_node_get_cpu_stats_for_each_cpu - * Since version: 0.4.6 - * Description: Function is used to get the CPU stats for each CPU on = the host node - * Arguments: @conn [resource]: resource for connection - * @time [int]: time in seconds to get the information ab= out, without aggregation for further processing - * Returns: array of node CPU statistics for each CPU including ti= me (in seconds since UNIX epoch), cpu number and total number of CPUs on no= de or FALSE for error - */ -PHP_FUNCTION(libvirt_node_get_cpu_stats_for_each_cpu) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - virNodeCPUStatsPtr params; - virNodeInfo info; - int nparams =3D 0; - zend_long avg =3D 0, iter =3D 0; - int done =3D 0; - int i, j, numCpus; - time_t startTime =3D 0; - zval *time_array; - - GET_CONNECTION_FROM_ARGS("r|l", &zconn, &avg); - - if (virNodeGetInfo(conn->conn, &info) !=3D 0) { - set_error("Cannot get number of CPUs" TSRMLS_CC); - RETURN_FALSE; - } - - if (virNodeGetCPUStats(conn->conn, VIR_NODE_CPU_STATS_ALL_CPUS, NULL, = &nparams, 0) !=3D 0) { - set_error("Cannot get number of CPU stats" TSRMLS_CC); - RETURN_FALSE; - } - - if (nparams =3D=3D 0) - RETURN_TRUE; - - DPRINTF("%s: Number of parameters got from virNodeGetCPUStats is %d\n"= , __FUNCTION__, nparams); - - params =3D (virNodeCPUStatsPtr)calloc(nparams, nparams * sizeof(*param= s)); - - numCpus =3D info.cpus; - array_init(return_value); - - startTime =3D time(NULL); - - iter =3D 0; - done =3D 0; - while (!done) { - zval *arr; - VIRT_ARRAY_INIT(arr); - - for (i =3D 0; i < numCpus; i++) { - zval *arr2; - - if (virNodeGetCPUStats(conn->conn, i, params, &nparams, 0) != =3D 0) { - set_error("Unable to get node cpu stats" TSRMLS_CC); - RETURN_FALSE; - } - - VIRT_ARRAY_INIT(arr2); - - for (j =3D 0; j < nparams; j++) - add_assoc_long(arr2, params[j].field, params[j].value); - - add_assoc_long(arr, "time", time(NULL)); - add_index_zval(arr, i, arr2); - } - - add_index_zval(return_value, iter, arr); - - if ((avg <=3D 0) || (iter =3D=3D avg - 1)) { - done =3D 1; - break; - } - -#ifndef EXTWIN - sleep(1); -#else - Sleep(1000); -#endif - iter++; - } - - 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); - - add_assoc_zval(return_value, "times", time_array); - - free(params); - params =3D NULL; -} - -/* - * Function name: libvirt_node_get_mem_stats - * Since version: 0.4.6 - * Description: Function is used to get the memory stats per node - * Arguments: @conn [resource]: resource for connection - * Returns: array of node memory statistics including time (in sec= onds since UNIX epoch) or FALSE for error - */ -PHP_FUNCTION(libvirt_node_get_mem_stats) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int memNum =3D VIR_NODE_MEMORY_STATS_ALL_CELLS; - virNodeMemoryStatsPtr params; - int nparams =3D 0; - int j; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if (virNodeGetMemoryStats(conn->conn, memNum, NULL, &nparams, 0) !=3D = 0) { - set_error("Cannot get number of memory stats" TSRMLS_CC); - RETURN_FALSE; - } - - if (nparams =3D=3D 0) - RETURN_TRUE; - - DPRINTF("%s: Number of parameters got from virNodeGetMemoryStats is %d= \n", __FUNCTION__, nparams); - - params =3D (virNodeMemoryStatsPtr)calloc(nparams, nparams * sizeof(*pa= rams)); - - array_init(return_value); - if (virNodeGetMemoryStats(conn->conn, memNum, params, &nparams, 0) != =3D 0) { - set_error("Unable to get node memory stats" TSRMLS_CC); - RETURN_FALSE; - } - - for (j =3D 0; j < nparams; j++) { - DPRINTF("%s: Field %s has value of %llu\n", __FUNCTION__, params[j= ].field, params[j].value); - - add_assoc_long(return_value, params[j].field, params[j].value); - } - - add_assoc_long(return_value, "time", time(NULL)); - - free(params); - params =3D NULL; -} - -/* - * Function name: libvirt_node_get_free_memory - * Since version: 0.5.3 - * Description: Function is used to get free memory available on the n= ode. - * Arguments: @conn [resource]: resource for connection. - * Returns: The available free memery in bytes as string or FALSE = for error. - */ -PHP_FUNCTION(libvirt_node_get_free_memory) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - unsigned long long ret; - LONGLONG_INIT; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((ret =3D virNodeGetFreeMemory(conn->conn)) !=3D 0) { - LONGLONG_RETURN_AS_STRING(ret); - } else { - set_error("Cannot get the free memory for the node" TSRMLS_CC); - RETURN_FALSE; - } -} - -/* * Function name: libvirt_image_create * Since version: 0.4.2 * Description: Function is used to create the image of desired name, = size and format. The image will be created in the image path (libvirt.image= _path INI variable). Works only o diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 6cbcd27..02b9cec 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -274,12 +274,6 @@ PHP_MINFO_FUNCTION(libvirt); =20 /* Common functions */ PHP_FUNCTION(libvirt_get_last_error); -/* Node functions */ -PHP_FUNCTION(libvirt_node_get_info); -PHP_FUNCTION(libvirt_node_get_cpu_stats); -PHP_FUNCTION(libvirt_node_get_cpu_stats_for_each_cpu); -PHP_FUNCTION(libvirt_node_get_mem_stats); -PHP_FUNCTION(libvirt_node_get_free_memory); /* Stream functions */ PHP_FUNCTION(libvirt_stream_create); PHP_FUNCTION(libvirt_stream_close); diff --git a/src/util.h b/src/util.h index 167bd90..d18b51d 100644 --- a/src/util.h +++ b/src/util.h @@ -205,6 +205,31 @@ =20 # endif /* PHP_MAJOR_VERSION < 7 */ =20 +# define LONGLONG_INIT = \ + char tmpnumber[64] + +# define LONGLONG_ASSOC(out, key, in) = \ + if (LIBVIRT_G(longlong_to_string_ini)) { = \ + snprintf(tmpnumber, 63, "%llu", in); = \ + VIRT_ADD_ASSOC_STRING(out, key, tmpnumber); = \ + } else { = \ + add_assoc_long(out, key, in); = \ + } + +# define LONGLONG_INDEX(out, key, in) = \ + if (LIBVIRT_G(longlong_to_string_ini)) { = \ + snprintf(tmpnumber, 63, "%llu", in); = \ + VIRT_ADD_INDEX_STRING(out, key, tmpnumber); = \ + } else { = \ + add_index_long(out, key, in); = \ + } + +# define LONGLONG_RETURN_AS_STRING(in) = \ + do { = \ + snprintf(tmpnumber, 63, "%llu", in); = \ + VIRT_RETURN_STRING(tmpnumber); = \ + } while (0) + # ifndef PHP_FE_END # define PHP_FE_END {NULL, NULL, NULL} # endif --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 23:55:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501623994713463.191771173691; Tue, 1 Aug 2017 14:46:34 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 62EB2C076434; Tue, 1 Aug 2017 21:46:32 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E71E018175; Tue, 1 Aug 2017 21:46:31 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 508771800C89; Tue, 1 Aug 2017 21:46:31 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkSgE007866 for ; Tue, 1 Aug 2017 17:46:28 -0400 Received: by smtp.corp.redhat.com (Postfix) id B4FDC7839B; Tue, 1 Aug 2017 21:46:28 +0000 (UTC) Received: from mx1.redhat.com (ext-mx05.extmail.prod.ext.phx2.redhat.com [10.5.110.29]) by smtp.corp.redhat.com (Postfix) with ESMTPS id ADF1A784A1 for ; Tue, 1 Aug 2017 21:46:28 +0000 (UTC) Received: from mail-qt0-f196.google.com (mail-qt0-f196.google.com [209.85.216.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0DD50A710 for ; Tue, 1 Aug 2017 21:46:21 +0000 (UTC) Received: by mail-qt0-f196.google.com with SMTP id c15so2928407qta.3 for ; Tue, 01 Aug 2017 14:46:21 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.19 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:19 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 62EB2C076434 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 62EB2C076434 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="pCt62xLo" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 0DD50A710 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 0DD50A710 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=HRPW9GlugF7CxVKyvfLlrSskK58J2lSokEeKXjRM220=; b=pCt62xLouRhstKABA3j0UnG7qU7X+rrEB1qUfz4sITwKHj3qMarXzy2hUQBcJRVuUN DVdV2vR61W0SThjNbf8KcXHi21uxr/28+L/9cQV0IqpEwIUE4hvqGGZGBMsSdg6XWofi xx/HL6shvyLn9tlyAs/S/BaeLiHoUwIwCZXPq5Z+dUyf1lsz4uZ19CoEU1UA0AO3o9S3 QjJPdzQKK/CoIVOMeDm6M8iGGRPWPx9cM4nmEesX4w4lJbWR5GY7bp6kKofnt3SwBBtZ mIbXBZucWT++E8Dg/MW4bteSm5l7FhTMBQo7CKwfsaqnexWt7IxtC2brNDUtJv2QlGcR eGPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=HRPW9GlugF7CxVKyvfLlrSskK58J2lSokEeKXjRM220=; b=fAs9Ojge6/Rszlco5DC4Si2KIp63ukgfoMJCR8lGdIdHOHN306HTUOrt5r880G2oT+ Qd6Ubj2BmAWWFFl0vtQMV2K37XNTxA3I0ZZST//80TsmtvrBoVb+eQbadlSTYkCjkK4D Ysc5OwDRXNulAdjlGStze3/Bt7SvLXHSr1ILSPRCI9/QTC/T44QO2JbOfkR+MuC7hKNA AL/CZqMNG4OToO4M4DImbFjDMSM8uMo8qJjyoI0mDMfV2+GWBivu69cg/qbuwt1GuNm4 lhKhMGv9PwdkjKp9jBN7HjS6pXnNInfNZQN+W2wv9pqCSorRvU5+vWJWKURdZOGkJcVG IcAA== X-Gm-Message-State: AIVw110g56dDVXOCbaehgENWMPSnvXaPpCRU9mXQxW/0n4tE1MRs8jn3 wVssEpjy77caJGgziUw= X-Received: by 10.200.51.57 with SMTP id t54mr15238255qta.12.1501623979696; Tue, 01 Aug 2017 14:46:19 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:07 -0400 Message-Id: <20170801214614.8915-7-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 01 Aug 2017 21:46:21 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 01 Aug 2017 21:46:21 +0000 (UTC) for IP:'209.85.216.196' DOMAIN:'mail-qt0-f196.google.com' HELO:'mail-qt0-f196.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.37 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.216.196 mail-qt0-f196.google.com 209.85.216.196 mail-qt0-f196.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.29 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 06/13] Split up the bindings for libvirt stream API X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 01 Aug 2017 21:46:33 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" --- src/Makefile.am | 3 +- src/libvirt-php.c | 231 +----------------------------------------------= ---- src/libvirt-php.h | 15 +--- src/libvirt-stream.c | 229 +++++++++++++++++++++++++++++++++++++++++++++++= +++ src/libvirt-stream.h | 39 +++++++++ 5 files changed, 273 insertions(+), 244 deletions(-) create mode 100644 src/libvirt-stream.c create mode 100644 src/libvirt-stream.h diff --git a/src/Makefile.am b/src/Makefile.am index ba2be62..ddfad41 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -23,7 +23,8 @@ libvirt_php_la_SOURCES =3D \ sockets.c sockets.h \ libvirt-php.c libvirt-php.h \ libvirt-connection.c libvirt-connection.h \ - libvirt-node.c libvirt-node.h + libvirt-node.c libvirt-node.h \ + libvirt-stream.c libvirt-stream.h libvirt_php_la_CFLAGS =3D \ $(AM_CFLAGS) \ -DCOMPILE_DL_LIBVIRT=3D1 diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 7e9c96b..80491da 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -22,11 +22,10 @@ #include "sockets.h" #include "libvirt-connection.h" #include "libvirt-node.h" +#include "libvirt-stream.h" =20 DEBUG_INIT("core"); =20 - - #ifndef EXTWIN /* Additional binaries */ const char *features[] =3D { "screenshot", "create-image", "screenshot-con= vert", NULL }; @@ -42,7 +41,6 @@ int le_libvirt_storagepool; int le_libvirt_volume; int le_libvirt_network; int le_libvirt_nodedev; -int le_libvirt_stream; int le_libvirt_snapshot; int le_libvirt_nwfilter; =20 @@ -477,15 +475,8 @@ ZEND_END_ARG_INFO() static zend_function_entry libvirt_functions[] =3D { /* Common functions */ PHP_FE(libvirt_get_last_error, arginfo_libvirt_void) - /* Connect functions */ PHP_FE_LIBVIRT_CONNECTION - /* Stream functions */ - PHP_FE(libvirt_stream_create, arginfo_libvirt_conn) - PHP_FE(libvirt_stream_close, arginfo_libvirt_conn) - PHP_FE(libvirt_stream_abort, arginfo_libvirt_conn) - PHP_FE(libvirt_stream_finish, arginfo_libvirt_conn) - PHP_FE(libvirt_stream_send, arginfo_libvirt_stream_se= nd) - PHP_FE(libvirt_stream_recv, arginfo_libvirt_stream_re= cv) + PHP_FE_LIBVIRT_STREAM /* Domain functions */ PHP_FE(libvirt_domain_new, arginfo_libvirt_domain_ne= w) PHP_FE(libvirt_domain_new_get_vnc, arginfo_libvirt_void) @@ -1358,33 +1349,6 @@ static void php_libvirt_domain_dtor(virt_resource *r= src TSRMLS_DC) } } =20 -/* Destructor for stream resource */ -static void php_libvirt_stream_dtor(virt_resource *rsrc TSRMLS_DC) -{ - php_libvirt_stream *stream =3D (php_libvirt_stream *)rsrc->ptr; - int rv =3D 0; - - if (stream !=3D NULL) { - if (stream->stream !=3D NULL) { - if (!check_resource_allocation(NULL, INT_RESOURCE_STREAM, stre= am->stream TSRMLS_CC)) { - stream->stream =3D NULL; - efree(stream); - return; - } - rv =3D virStreamFree(stream->stream); - if (rv !=3D 0) { - DPRINTF("%s: virStreamFree(%p) returned %d (%s)\n", __FUNC= TION__, stream->stream, rv, LIBVIRT_G(last_error)); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStreamFree= failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); - } else { - DPRINTF("%s: virStreamFree(%p) completed successfully\n", = __FUNCTION__, stream->stream); - resource_change_counter(INT_RESOURCE_STREAM, NULL, stream-= >stream, 0 TSRMLS_CC); - } - stream->stream =3D NULL; - } - efree(stream); - } -} - /* Destructor for storagepool resource */ static void php_libvirt_storagepool_dtor(virt_resource *rsrc TSRMLS_DC) { @@ -3252,197 +3216,6 @@ PHP_FUNCTION(libvirt_domain_lookup_by_uuid_string) } =20 /* - * Function name: libvirt_stream_create - * Since version: 0.5.0 - * Description: Function is used to create new stream from libvirt conn - * Arguments: @res [resource]: libvirt connection resource from libv= irt_connect() - * Returns: resource libvirt stream resource - */ -PHP_FUNCTION(libvirt_stream_create) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - virStreamPtr stream =3D NULL; - php_libvirt_stream *res_stream; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zconn) =3D= =3D FAILURE) - RETURN_FALSE; - VIRT_FETCH_RESOURCE(conn, php_libvirt_connection*, &zconn, PHP_LIBVIRT= _CONNECTION_RES_NAME, le_libvirt_connection); - if ((conn =3D=3D NULL) || (conn->conn =3D=3D NULL)) - RETURN_FALSE; - - stream =3D virStreamNew(conn->conn, 0); - if (stream =3D=3D NULL) { - set_error("Cannot create new stream" TSRMLS_CC); - RETURN_FALSE; - } - - res_stream =3D (php_libvirt_stream *)emalloc(sizeof(php_libvirt_stream= )); - res_stream->stream =3D stream; - res_stream->conn =3D conn; - - resource_change_counter(INT_RESOURCE_STREAM, conn->conn, res_stream->s= tream, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_stream, le_libvirt_stream); -} - -/* - * Function name: libvirt_stream_close - * Since version: 0.5.0 - * Description: Function is used to close stream - * Arguments: @res [resource]: libvirt stream resource from libvirt_= stream_create() - * Returns: int - */ -PHP_FUNCTION(libvirt_stream_close) -{ - zval *zstream; - php_libvirt_stream *stream =3D NULL; - int retval =3D -1; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) = =3D=3D FAILURE) - RETURN_LONG(retval); - VIRT_FETCH_RESOURCE(stream, php_libvirt_stream*, &zstream, PHP_LIBVIRT= _STREAM_RES_NAME, le_libvirt_stream); - if ((stream =3D=3D NULL) || (stream->stream =3D=3D NULL)) - RETURN_LONG(retval); - - retval =3D virStreamFree(stream->stream); - if (retval !=3D 0) { - set_error("Cannot free stream" TSRMLS_CC); - RETURN_LONG(retval); - } - - resource_change_counter(INT_RESOURCE_STREAM, stream->conn->conn, strea= m->stream, 0 TSRMLS_CC); - RETURN_LONG(retval); -} - -/* - * Function name: libvirt_stream_abort - * Since version: 0.5.0 - * Description: Function is used to abort transfer - * Arguments: @res [resource]: libvirt stream resource from libvirt_= stream_create() - * Returns: int - */ -PHP_FUNCTION(libvirt_stream_abort) -{ - zval *zstream; - php_libvirt_stream *stream =3D NULL; - int retval =3D -1; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) = =3D=3D FAILURE) - RETURN_LONG(retval); - VIRT_FETCH_RESOURCE(stream, php_libvirt_stream*, &zstream, PHP_LIBVIRT= _STREAM_RES_NAME, le_libvirt_stream); - if ((stream =3D=3D NULL) || (stream->stream =3D=3D NULL)) - RETURN_LONG(retval); - - retval =3D virStreamAbort(stream->stream); - if (retval !=3D 0) { - set_error("Cannot abort stream" TSRMLS_CC); - RETURN_LONG(retval); - } - RETURN_LONG(retval); -} - -/* - * Function name: libvirt_stream_finish - * Since version: 0.5.0 - * Description: Function is used to finish transfer - * Arguments: @res [resource]: libvirt stream resource from libvirt_= stream_create() - * Returns: int - */ -PHP_FUNCTION(libvirt_stream_finish) -{ - zval *zstream; - php_libvirt_stream *stream =3D NULL; - int retval =3D -1; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) = =3D=3D FAILURE) - RETURN_LONG(retval); - VIRT_FETCH_RESOURCE(stream, php_libvirt_stream*, &zstream, PHP_LIBVIRT= _STREAM_RES_NAME, le_libvirt_stream); - if ((stream =3D=3D NULL) || (stream->stream =3D=3D NULL)) - RETURN_LONG(retval); - - retval =3D virStreamFinish(stream->stream); - if (retval !=3D 0) { - set_error("Cannot finish stream" TSRMLS_CC); - RETURN_LONG(retval); - } - RETURN_LONG(retval); -} - -/* - * Function name: libvirt_stream_recv - * Since version: 0.5.0 - * Description: Function is used to recv from stream via libvirt conn - * Arguments: @res [resource]: libvirt stream resource from libvirt_= stream_create() - * @data [string]: buffer - * @len [int]: amout of data to recieve - * Returns: int - */ -PHP_FUNCTION(libvirt_stream_recv) -{ - zval *zstream, *zbuf; - php_libvirt_stream *stream =3D NULL; - char *recv_buf =3D NULL; - int retval =3D -1; - zend_long length =3D 0; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|l", &zstream,= &zbuf, &length) =3D=3D FAILURE) - RETURN_LONG(retval); - VIRT_FETCH_RESOURCE(stream, php_libvirt_stream*, &zstream, PHP_LIBVIRT= _STREAM_RES_NAME, le_libvirt_stream); - if ((stream =3D=3D NULL) || (stream->stream =3D=3D NULL)) - RETURN_LONG(retval); - - recv_buf =3D emalloc(length + 1); - - retval =3D virStreamRecv(stream->stream, recv_buf, length); - if (retval < 0) { - zval_dtor(zbuf); - ZVAL_NULL(zbuf); - } else { - recv_buf[retval] =3D '\0'; - VIRT_ZVAL_STRINGL(zbuf, recv_buf, retval); - } - - if (retval =3D=3D -1) - set_error("Cannot recv from stream" TSRMLS_CC); - - efree(recv_buf); - RETURN_LONG(retval); -} - -/* - * Function name: libvirt_stream_send - * Since version: 0.5.0 - * Description: Function is used to send to stream via libvirt conn - * Arguments: @res [resource]: libvirt stream resource from libvirt_= stream_create() - * @data [string]: buffer - * @length [int]: amout of data to send - * Returns: int - */ -PHP_FUNCTION(libvirt_stream_send) -{ - zval *zstream, *zbuf; - php_libvirt_stream *stream =3D NULL; - int retval =3D -1; - zend_long length =3D 0; - char *cstr; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|l", &zstream,= &zbuf, &length) =3D=3D FAILURE) - RETURN_LONG(retval); - VIRT_FETCH_RESOURCE(stream, php_libvirt_stream*, &zstream, PHP_LIBVIRT= _STREAM_RES_NAME, le_libvirt_stream); - if ((stream =3D=3D NULL) || (stream->stream =3D=3D NULL)) - RETURN_LONG(retval); - - cstr =3D Z_STRVAL_P(zbuf); - - retval =3D virStreamSend(stream->stream, cstr, length); - if (retval =3D=3D -1) - set_error("Cannot send to stream" TSRMLS_CC); - - RETURN_LONG(retval); -} - -/* * Function name: libvirt_domain_lookup_by_id * Since version: 0.4.1(-1) * Description: Function is used to get domain by it's ID, applicable = only to running guests diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 02b9cec..1a009d3 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -147,7 +147,6 @@ typedef uint64_t arch_uint; #define INT_RESOURCE_STORAGEPOOL 0x10 #define INT_RESOURCE_VOLUME 0x20 #define INT_RESOURCE_SNAPSHOT 0x40 -#define INT_RESOURCE_STREAM 0x50 #define INT_RESOURCE_NWFILTER 0x60 =20 typedef struct tTokenizer { @@ -181,11 +180,6 @@ typedef struct tVMNetwork { typedef struct _php_libvirt_connection php_libvirt_connection; =20 /* Libvirt-php types */ -typedef struct _php_libvirt_stream { - virStreamPtr stream; - php_libvirt_connection* conn; -} php_libvirt_stream; - typedef struct _php_libvirt_domain { virDomainPtr domain; php_libvirt_connection* conn; @@ -248,6 +242,7 @@ void set_error(char *msg TSRMLS_DC); void reset_error(TSRMLS_D); int count_resources(int type TSRMLS_DC); int resource_change_counter(int type, virConnectPtr conn, void *mem, int i= nc TSRMLS_DC); +int check_resource_allocation(virConnectPtr conn, int type, void *mem TSRM= LS_DC); void free_resource(int type, void *mem TSRMLS_DC); char *connection_get_emulator(virConnectPtr conn, char *arch TSRMLS_DC); int is_local_connection(virConnectPtr conn); @@ -258,7 +253,6 @@ char *get_string_from_xpath(char *xml, char *xpath, zva= l **val, int *retVal); char **get_array_from_xpath(char *xml, char *xpath, int *num); =20 #define PHP_LIBVIRT_DOMAIN_RES_NAME "Libvirt domain" -#define PHP_LIBVIRT_STREAM_RES_NAME "Libvirt stream" #define PHP_LIBVIRT_STORAGEPOOL_RES_NAME "Libvirt storagepool" #define PHP_LIBVIRT_VOLUME_RES_NAME "Libvirt volume" #define PHP_LIBVIRT_NETWORK_RES_NAME "Libvirt virtual network" @@ -274,13 +268,6 @@ PHP_MINFO_FUNCTION(libvirt); =20 /* Common functions */ PHP_FUNCTION(libvirt_get_last_error); -/* Stream functions */ -PHP_FUNCTION(libvirt_stream_create); -PHP_FUNCTION(libvirt_stream_close); -PHP_FUNCTION(libvirt_stream_abort); -PHP_FUNCTION(libvirt_stream_finish); -PHP_FUNCTION(libvirt_stream_recv); -PHP_FUNCTION(libvirt_stream_send); /* Domain functions */ PHP_FUNCTION(libvirt_domain_new); PHP_FUNCTION(libvirt_domain_new_get_vnc); diff --git a/src/libvirt-stream.c b/src/libvirt-stream.c new file mode 100644 index 0000000..1ba040c --- /dev/null +++ b/src/libvirt-stream.c @@ -0,0 +1,229 @@ +/* + * libvirt-stream.c: The PHP bindings to libvirt stream API + * + * See COPYING for the license of this software + */ + +#include + +#include "libvirt-stream.h" + +DEBUG_INIT("stream"); + +void +php_libvirt_stream_dtor(virt_resource *rsrc TSRMLS_DC) +{ + php_libvirt_stream *stream =3D (php_libvirt_stream *)rsrc->ptr; + int rv =3D 0; + + if (stream !=3D NULL) { + if (stream->stream !=3D NULL) { + if (!check_resource_allocation(NULL, INT_RESOURCE_STREAM, stre= am->stream TSRMLS_CC)) { + stream->stream =3D NULL; + efree(stream); + return; + } + rv =3D virStreamFree(stream->stream); + if (rv !=3D 0) { + DPRINTF("%s: virStreamFree(%p) returned %d (%s)\n", __FUNC= TION__, stream->stream, rv, LIBVIRT_G(last_error)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStreamFree= failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virStreamFree(%p) completed successfully\n", = __FUNCTION__, stream->stream); + resource_change_counter(INT_RESOURCE_STREAM, NULL, stream-= >stream, 0 TSRMLS_CC); + } + stream->stream =3D NULL; + } + efree(stream); + } +} + +/* + * Function name: libvirt_stream_create + * Since version: 0.5.0 + * Description: Function is used to create new stream from libvirt conn + * Arguments: @res [resource]: libvirt connection resource from libv= irt_connect() + * Returns: resource libvirt stream resource + */ +PHP_FUNCTION(libvirt_stream_create) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + virStreamPtr stream =3D NULL; + php_libvirt_stream *res_stream; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zconn) =3D= =3D FAILURE) + RETURN_FALSE; + VIRT_FETCH_RESOURCE(conn, php_libvirt_connection*, &zconn, PHP_LIBVIRT= _CONNECTION_RES_NAME, le_libvirt_connection); + if ((conn =3D=3D NULL) || (conn->conn =3D=3D NULL)) + RETURN_FALSE; + + stream =3D virStreamNew(conn->conn, 0); + if (stream =3D=3D NULL) { + set_error("Cannot create new stream" TSRMLS_CC); + RETURN_FALSE; + } + + res_stream =3D (php_libvirt_stream *)emalloc(sizeof(php_libvirt_stream= )); + res_stream->stream =3D stream; + res_stream->conn =3D conn; + + resource_change_counter(INT_RESOURCE_STREAM, conn->conn, res_stream->s= tream, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_stream, le_libvirt_stream); +} + +/* + * Function name: libvirt_stream_close + * Since version: 0.5.0 + * Description: Function is used to close stream + * Arguments: @res [resource]: libvirt stream resource from libvirt_= stream_create() + * Returns: int + */ +PHP_FUNCTION(libvirt_stream_close) +{ + zval *zstream; + php_libvirt_stream *stream =3D NULL; + int retval =3D -1; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) = =3D=3D FAILURE) + RETURN_LONG(retval); + VIRT_FETCH_RESOURCE(stream, php_libvirt_stream*, &zstream, PHP_LIBVIRT= _STREAM_RES_NAME, le_libvirt_stream); + if ((stream =3D=3D NULL) || (stream->stream =3D=3D NULL)) + RETURN_LONG(retval); + + retval =3D virStreamFree(stream->stream); + if (retval !=3D 0) { + set_error("Cannot free stream" TSRMLS_CC); + RETURN_LONG(retval); + } + + resource_change_counter(INT_RESOURCE_STREAM, stream->conn->conn, strea= m->stream, 0 TSRMLS_CC); + RETURN_LONG(retval); +} + +/* + * Function name: libvirt_stream_abort + * Since version: 0.5.0 + * Description: Function is used to abort transfer + * Arguments: @res [resource]: libvirt stream resource from libvirt_= stream_create() + * Returns: int + */ +PHP_FUNCTION(libvirt_stream_abort) +{ + zval *zstream; + php_libvirt_stream *stream =3D NULL; + int retval =3D -1; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) = =3D=3D FAILURE) + RETURN_LONG(retval); + VIRT_FETCH_RESOURCE(stream, php_libvirt_stream*, &zstream, PHP_LIBVIRT= _STREAM_RES_NAME, le_libvirt_stream); + if ((stream =3D=3D NULL) || (stream->stream =3D=3D NULL)) + RETURN_LONG(retval); + + retval =3D virStreamAbort(stream->stream); + if (retval !=3D 0) { + set_error("Cannot abort stream" TSRMLS_CC); + RETURN_LONG(retval); + } + RETURN_LONG(retval); +} + +/* + * Function name: libvirt_stream_finish + * Since version: 0.5.0 + * Description: Function is used to finish transfer + * Arguments: @res [resource]: libvirt stream resource from libvirt_= stream_create() + * Returns: int + */ +PHP_FUNCTION(libvirt_stream_finish) +{ + zval *zstream; + php_libvirt_stream *stream =3D NULL; + int retval =3D -1; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) = =3D=3D FAILURE) + RETURN_LONG(retval); + VIRT_FETCH_RESOURCE(stream, php_libvirt_stream*, &zstream, PHP_LIBVIRT= _STREAM_RES_NAME, le_libvirt_stream); + if ((stream =3D=3D NULL) || (stream->stream =3D=3D NULL)) + RETURN_LONG(retval); + + retval =3D virStreamFinish(stream->stream); + if (retval !=3D 0) { + set_error("Cannot finish stream" TSRMLS_CC); + RETURN_LONG(retval); + } + RETURN_LONG(retval); +} + +/* + * Function name: libvirt_stream_recv + * Since version: 0.5.0 + * Description: Function is used to recv from stream via libvirt conn + * Arguments: @res [resource]: libvirt stream resource from libvirt_= stream_create() + * @data [string]: buffer + * @len [int]: amout of data to recieve + * Returns: int + */ +PHP_FUNCTION(libvirt_stream_recv) +{ + zval *zstream, *zbuf; + php_libvirt_stream *stream =3D NULL; + char *recv_buf =3D NULL; + int retval =3D -1; + zend_long length =3D 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|l", &zstream,= &zbuf, &length) =3D=3D FAILURE) + RETURN_LONG(retval); + VIRT_FETCH_RESOURCE(stream, php_libvirt_stream*, &zstream, PHP_LIBVIRT= _STREAM_RES_NAME, le_libvirt_stream); + if ((stream =3D=3D NULL) || (stream->stream =3D=3D NULL)) + RETURN_LONG(retval); + + recv_buf =3D emalloc(length + 1); + + retval =3D virStreamRecv(stream->stream, recv_buf, length); + if (retval < 0) { + zval_dtor(zbuf); + ZVAL_NULL(zbuf); + } else { + recv_buf[retval] =3D '\0'; + VIRT_ZVAL_STRINGL(zbuf, recv_buf, retval); + } + + if (retval =3D=3D -1) + set_error("Cannot recv from stream" TSRMLS_CC); + + efree(recv_buf); + RETURN_LONG(retval); +} + +/* + * Function name: libvirt_stream_send + * Since version: 0.5.0 + * Description: Function is used to send to stream via libvirt conn + * Arguments: @res [resource]: libvirt stream resource from libvirt_= stream_create() + * @data [string]: buffer + * @length [int]: amout of data to send + * Returns: int + */ +PHP_FUNCTION(libvirt_stream_send) +{ + zval *zstream, *zbuf; + php_libvirt_stream *stream =3D NULL; + int retval =3D -1; + zend_long length =3D 0; + char *cstr; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|l", &zstream,= &zbuf, &length) =3D=3D FAILURE) + RETURN_LONG(retval); + VIRT_FETCH_RESOURCE(stream, php_libvirt_stream*, &zstream, PHP_LIBVIRT= _STREAM_RES_NAME, le_libvirt_stream); + if ((stream =3D=3D NULL) || (stream->stream =3D=3D NULL)) + RETURN_LONG(retval); + + cstr =3D Z_STRVAL_P(zbuf); + + retval =3D virStreamSend(stream->stream, cstr, length); + if (retval =3D=3D -1) + set_error("Cannot send to stream" TSRMLS_CC); + + RETURN_LONG(retval); +} diff --git a/src/libvirt-stream.h b/src/libvirt-stream.h new file mode 100644 index 0000000..89b28a6 --- /dev/null +++ b/src/libvirt-stream.h @@ -0,0 +1,39 @@ +/* + * libvirt-stream.h: The PHP bindings to libvirt stream API + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_STREAM_H__ +# define __LIBVIRT_STREAM_H__ + +# include "libvirt-connection.h" + +# define PHP_LIBVIRT_STREAM_RES_NAME "Libvirt stream" +# define INT_RESOURCE_STREAM 0x50 + +# define PHP_FE_LIBVIRT_STREAM = \ + PHP_FE(libvirt_stream_create, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_stream_close, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_stream_abort, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_stream_finish, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_stream_send, arginfo_libvirt_stream_send) = \ + PHP_FE(libvirt_stream_recv, arginfo_libvirt_stream_recv) + +int le_libvirt_stream; + +typedef struct _php_libvirt_stream { + virStreamPtr stream; + php_libvirt_connection* conn; +} php_libvirt_stream; + +void php_libvirt_stream_dtor(virt_resource *rsrc TSRMLS_DC); + +PHP_FUNCTION(libvirt_stream_create); +PHP_FUNCTION(libvirt_stream_close); +PHP_FUNCTION(libvirt_stream_abort); +PHP_FUNCTION(libvirt_stream_finish); +PHP_FUNCTION(libvirt_stream_recv); +PHP_FUNCTION(libvirt_stream_send); + +#endif --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 23:55:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501625597810642.7655032872307; Tue, 1 Aug 2017 15:13:17 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4C88C4482F4; Tue, 1 Aug 2017 22:13:15 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1381B784AB; Tue, 1 Aug 2017 22:13:15 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id A29FE180B467; Tue, 1 Aug 2017 22:13:14 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkeGS007962 for ; Tue, 1 Aug 2017 17:46:40 -0400 Received: by smtp.corp.redhat.com (Postfix) id C086C6375F; Tue, 1 Aug 2017 21:46:40 +0000 (UTC) Received: from mx1.redhat.com (ext-mx04.extmail.prod.ext.phx2.redhat.com [10.5.110.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AEB594DA95 for ; Tue, 1 Aug 2017 21:46:38 +0000 (UTC) Received: from mail-qt0-f181.google.com (mail-qt0-f181.google.com [209.85.216.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B24AB108D42 for ; Tue, 1 Aug 2017 21:46:23 +0000 (UTC) Received: by mail-qt0-f181.google.com with SMTP id 16so17124097qtz.4 for ; Tue, 01 Aug 2017 14:46:23 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.19 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:20 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 4C88C4482F4 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 4C88C4482F4 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="k/ixDg9C" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com B24AB108D42 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com B24AB108D42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=+Nu2+uE+29TdfOEf/KbNgasDm9W28j06NawHSFMjO7Q=; b=k/ixDg9CGJSgpgM2T25nqoFJF1Epno8Y0QxkjxLdh1BO63Ids8LIG3oVPseWH5ET4c xDG8Fn/tdi6md6hcuPN93xsRzNKOkLqCRBD5w9VboAY9KUU13bWGXSFSjR5rj69bS6oF 69dwwqa1RIhMdq7vyK5yLUkTHHbammFI8ub92zQxPcydfPPoKfsqmmOrwZ8X/2D6bI5I ggnOJuzHpQmg18i0HDBSMhIuOdQatyIZS2mvdFSrOA+9Xva6mVanvWyYwOjoOVebI6rz xE/4tJPWV/5pqgz1zn0wK0+1LhzX2xPHFkyMg/xs6FK1+7MsdADt3dcGxuwct+51izWF fwww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=+Nu2+uE+29TdfOEf/KbNgasDm9W28j06NawHSFMjO7Q=; b=hd3/smjOnhhaz22V6C1XpbI1Uwv1bOJnxObvNPdYssZ0jWsJTRcKQ48CpFVYHHYUaB +X6oHS3BVdsR/JDndGr3zX1PWJQxFLUokG5LshFHOcD+1tu/+TN2lfZ2O4/BybxnakdC pVH4Hx+UB56oOVPfBaOKqOQcbUU/lfj2lU01RYUYqlz5QBTRP+sW7nXWZncSd8CiA37X NCDTtaHf+AzKHLIekdmoWncsbB/N962QRKH+5fBPZ9OxqFsyxSq7U+XqPUfd9TVYr7+P KzmeE7ZRKCYlJTXZcyTIZPLCAOfkgVXH0rmmQfJEfp+dQdnFabkCIkeYhLzIEaYh5PJD 11YQ== X-Gm-Message-State: AIVw112pPk1ov+RzfDg1S8yPHtA6h+PzJUSI6xL3HEDNlEGQHUTI0ig2 H89QH89BEyMHhlOOVsM= X-Received: by 10.200.13.67 with SMTP id r3mr30650279qti.286.1501623981080; Tue, 01 Aug 2017 14:46:21 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:08 -0400 Message-Id: <20170801214614.8915-8-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 01 Aug 2017 21:46:24 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 01 Aug 2017 21:46:24 +0000 (UTC) for IP:'209.85.216.181' DOMAIN:'mail-qt0-f181.google.com' HELO:'mail-qt0-f181.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: -1.12 (DCC_REPUT_00_12, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.216.181 mail-qt0-f181.google.com 209.85.216.181 mail-qt0-f181.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.28 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 07/13] Split up the bindings for libvirt domain API X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 01 Aug 2017 22:13:16 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" --- src/Makefile.am | 3 +- src/libvirt-domain.c | 3339 ++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-domain.h | 208 +++ src/libvirt-php.c | 3789 +++-------------------------------------------= ---- src/libvirt-php.h | 119 +- 5 files changed, 3749 insertions(+), 3709 deletions(-) create mode 100644 src/libvirt-domain.c create mode 100644 src/libvirt-domain.h diff --git a/src/Makefile.am b/src/Makefile.am index ddfad41..b8eae3a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -24,7 +24,8 @@ libvirt_php_la_SOURCES =3D \ libvirt-php.c libvirt-php.h \ libvirt-connection.c libvirt-connection.h \ libvirt-node.c libvirt-node.h \ - libvirt-stream.c libvirt-stream.h + libvirt-stream.c libvirt-stream.h \ + libvirt-domain.c libvirt-domain.h libvirt_php_la_CFLAGS =3D \ $(AM_CFLAGS) \ -DCOMPILE_DL_LIBVIRT=3D1 diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c new file mode 100644 index 0000000..7d8587e --- /dev/null +++ b/src/libvirt-domain.c @@ -0,0 +1,3339 @@ +/* + * libvirt-domain.c: The PHP bindings to libvirt domain API + * + * See COPYING for the license of this software + */ + +#include +#include =20 +#include + +#include "libvirt-domain.h" +#include "sockets.h" +#include "vncfunc.h" + +DEBUG_INIT("domain"); + +void +php_libvirt_domain_dtor(virt_resource *rsrc TSRMLS_DC) +{ + php_libvirt_domain *domain =3D (php_libvirt_domain *)rsrc->ptr; + int rv =3D 0; + + if (domain !=3D NULL) { + if (domain->domain !=3D NULL) { + if (!check_resource_allocation(domain->conn->conn, INT_RESOURC= E_DOMAIN, domain->domain TSRMLS_CC)) { + domain->domain =3D NULL; + efree(domain); + return; + } + + rv =3D virDomainFree(domain->domain); + if (rv !=3D 0) { + DPRINTF("%s: virDomainFree(%p) returned %d (%s)\n", __FUNC= TION__, domain->domain, rv, LIBVIRT_G(last_error)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainFree= failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virDomainFree(%p) completed successfully\n", = __FUNCTION__, domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, domain->conn-= >conn, domain->domain, 0 TSRMLS_CC); + } + domain->domain =3D NULL; + } + efree(domain); + } +} + +/* + * Function name: libvirt_domain_new + * Since version: 0.4.5 + * Description: Function is used to install a new virtual machine to t= he machine + * Arguments: @conn [resource]: libvirt connection resource + * @name [string]: name of the new domain + * @arch [string]: optional architecture string, can be N= ULL to get default (or false) + * @memMB [int]: number of megabytes of RAM to be allocat= ed for domain + * @maxmemMB [int]: maximum number of megabytes of RAM to= be allocated for domain + * @vcpus [int]: number of VCPUs to be allocated to domain + * @iso_image [string]: installation ISO image for domain + * @disks [array]: array of disk devices for domain, cons= ist of keys as 'path' (storage location), 'driver' (image type, e.g. 'raw' = or 'qcow2'), 'bus' (e.g. 'ide', 'scsi'), 'dev' (device to be presented to t= he guest - e.g. 'hda'), 'size' (with 'M' or 'G' suffixes, like '10G' for 10= gigabytes image etc.) and 'flags' (VIR_DOMAIN_DISK_FILE or VIR_DOMAIN_DISK= _BLOCK, optionally VIR_DOMAIN_DISK_ACCESS_ALL to allow access to the disk f= or all users on the host system) + * @networks [array]: array of network devices for domain= , consists of keys as 'mac' (for MAC address), 'network' (for network name)= and optional 'model' for model of NIC device + * @flags [int]: bit array of flags + * Returns: a new domain resource + */ +PHP_FUNCTION(libvirt_domain_new) +{ + php_libvirt_connection *conn =3D NULL; + php_libvirt_domain *res_domain =3D NULL; + virDomainPtr domain2 =3D NULL; + virDomainPtr domain =3D NULL; + zval *zconn; + char *arch =3D NULL; + strsize_t arch_len; + char *tmp; + char *name; + strsize_t name_len =3D 0; + // char *emulator; + char *iso_image =3D NULL; + strsize_t iso_image_len; + zend_long vcpus =3D -1; + zend_long memMB =3D -1; + zval *disks, *networks; + tVMDisk *vmDisks =3D NULL; + tVMNetwork *vmNetworks =3D NULL; + zend_long maxmemMB =3D -1; + HashTable *arr_hash; + int numDisks, numNets, i; + zval *data; + HashPosition pointer; + char vncl[2048] =3D { 0 }; + char tmpname[1024] =3D { 0 }; + char *xml =3D NULL; + int retval =3D 0; + char *uuid =3D NULL; + zend_long flags =3D 0; + int fd =3D -1; + + GET_CONNECTION_FROM_ARGS("rsslllsaa|l", &zconn, &name, &name_len, &arc= h, &arch_len, &memMB, &maxmemMB, &vcpus, &iso_image, &iso_image_len, &disks= , &networks, &flags); + + if (iso_image =3D=3D NULL) { + DPRINTF("%s: Iso image is not defined\n", PHPFUNC); + RETURN_FALSE; + } + + if ((arch =3D=3D NULL) || (arch_len =3D=3D 0)) + arch =3D NULL; + + //DPRINTF("%s: name: %s, arch: %s, memMB: %d, maxmemMB: %d, vcpus: %d,= iso_image: %s\n", PHPFUNC, name, arch, memMB, maxmemMB, vcpus, iso_image); + if (memMB =3D=3D 0) + memMB =3D maxmemMB; + + /* Parse all disks from array */ + arr_hash =3D Z_ARRVAL_P(disks); + numDisks =3D zend_hash_num_elements(arr_hash); + vmDisks =3D (tVMDisk *)malloc(numDisks * sizeof(tVMDisk)); + memset(vmDisks, 0, numDisks * sizeof(tVMDisk)); + i =3D 0; + VIRT_FOREACH(arr_hash, pointer, data) { + if (Z_TYPE_P(data) =3D=3D IS_ARRAY) { + tVMDisk disk; + parse_array(data, &disk, NULL); + if (disk.path !=3D NULL) { + //DPRINTF("Disk =3D> path =3D '%s', driver =3D '%s', bus = =3D '%s', dev =3D '%s', size =3D %ld MB, flags =3D %d\n", + // disk.path, disk.driver, disk.bus, disk.dev, disk.size,= disk.flags); + vmDisks[i++] =3D disk; + } + } + } VIRT_FOREACH_END(); + numDisks =3D i; + + /* Parse all networks from array */ + arr_hash =3D Z_ARRVAL_P(networks); + numNets =3D zend_hash_num_elements(arr_hash); + vmNetworks =3D (tVMNetwork *)malloc(numNets * sizeof(tVMNetwork)); + memset(vmNetworks, 0, numNets * sizeof(tVMNetwork)); + i =3D 0; + VIRT_FOREACH(arr_hash, pointer, data) { + if (Z_TYPE_P(data) =3D=3D IS_ARRAY) { + tVMNetwork network; + parse_array(data, NULL, &network); + if (network.mac !=3D NULL) { + //DPRINTF("Network =3D> mac =3D '%s', network =3D '%s', mo= del =3D '%s'\n", network.mac, network.network, network.model); + vmNetworks[i++] =3D network; + } + } + } VIRT_FOREACH_END(); + numNets =3D i; + + snprintf(tmpname, sizeof(tmpname), "%s-install", name); + DPRINTF("%s: Name is '%s', memMB is %d, maxmemMB is %d\n", PHPFUNC, tm= pname, (int) memMB, (int) maxmemMB); + tmp =3D installation_get_xml(1, + conn->conn, tmpname, memMB, maxmemMB, NULL = /* arch */, NULL, vcpus, iso_image, + vmDisks, numDisks, vmNetworks, numNets, + flags TSRMLS_CC); + if (tmp =3D=3D NULL) { + DPRINTF("%s: Cannot get installation XML\n", PHPFUNC); + set_error("Cannot get installation XML" TSRMLS_CC); + RETURN_FALSE; + } + + domain =3D virDomainCreateXML(conn->conn, tmp, 0); + if (domain =3D=3D NULL) { + set_error_if_unset("Cannot create installation domain from the XML= description" TSRMLS_CC); + DPRINTF("%s: Cannot create installation domain from the XML descri= ption (%s): %s\n", PHPFUNC, LIBVIRT_G(last_error), tmp); + RETURN_FALSE; + } + + xml =3D virDomainGetXMLDesc(domain, 0); + if (!xml) { + DPRINTF("%s: Cannot get the XML description\n", PHPFUNC); + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics[@type=3D= 'vnc']/@port", NULL, &retval); + if (retval < 0) { + DPRINTF("%s: Cannot get port from XML description\n", PHPFUNC); + set_error_if_unset("Cannot get port from XML description" TSRMLS_C= C); + RETURN_FALSE; + } + + snprintf(vncl, sizeof(vncl), "%s:%s", virConnectGetHostname(conn->conn= ), tmp); + DPRINTF("%s: Trying to connect to '%s'\n", PHPFUNC, vncl); + +#ifndef EXTWIN + if ((fd =3D connect_socket(virConnectGetHostname(conn->conn), tmp, 0, = 0, flags & DOMAIN_FLAG_TEST_LOCAL_VNC)) < 0) { + DPRINTF("%s: Cannot connect to '%s'\n", PHPFUNC, vncl); + snprintf(vncl, sizeof(vncl), "Connection failed, port %s is most l= ikely forbidden on firewall (iptables) on the host (%s)" + " or the emulator VNC server is bound to localhost addres= s only.", + tmp, virConnectGetHostname(conn->conn)); + } else { + close(fd); + DPRINTF("%s: Connection to '%s' successfull (%s local connection)\= n", PHPFUNC, vncl, + (flags & DOMAIN_FLAG_TEST_LOCAL_VNC) ? "using" : "not usin= g"); + } +#endif + + set_vnc_location(vncl TSRMLS_CC); + + tmp =3D installation_get_xml(2, + conn->conn, name, memMB, maxmemMB, NULL /* = arch */, NULL, vcpus, iso_image, + vmDisks, numDisks, vmNetworks, numNets, + flags TSRMLS_CC); + if (tmp =3D=3D NULL) { + DPRINTF("%s: Cannot get installation XML, step 2\n", PHPFUNC); + set_error("Cannot get installation XML, step 2" TSRMLS_CC); + virDomainFree(domain); + RETURN_FALSE; + } + + domain2 =3D virDomainDefineXML(conn->conn, tmp); + if (domain2 =3D=3D NULL) { + set_error_if_unset("Cannot define domain from the XML description"= TSRMLS_CC); + DPRINTF("%s: Cannot define domain from the XML description (name = =3D '%s', uuid =3D '%s', error =3D '%s')\n", PHPFUNC, name, uuid, LIBVIRT_G= (last_error)); + RETURN_FALSE; + } + virDomainFree(domain2); + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D domain; + res_domain->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_new_get_vnc + * Since version: 0.4.5 + * Description: Function is used to get the VNC server location for th= e newly created domain (newly started installation) + * Arguments: None + * Returns: a VNC server for a newly created domain resource (if a= ny) + */ +PHP_FUNCTION(libvirt_domain_new_get_vnc) +{ + if (LIBVIRT_G(vnc_location)) + VIRT_RETURN_STRING(LIBVIRT_G(vnc_location)); + + RETURN_NULL(); +} + +/* + * Function name: libvirt_domain_get_counts + * Since version: 0.4.1(-1) + * Description: Function is getting domain counts for all, active and = inactive domains + * Arguments: @conn [resource]: libvirt connection resource from lib= virt_connect() + * Returns: array of total, active and inactive (but defined) doma= in counts + */ +PHP_FUNCTION(libvirt_domain_get_counts) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count_defined; + int count_active; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + count_defined =3D virConnectNumOfDefinedDomains(conn->conn); + count_active =3D virConnectNumOfDomains(conn->conn); + + array_init(return_value); + add_assoc_long(return_value, "total", (long)(count_defined + count_act= ive)); + add_assoc_long(return_value, "active", (long)count_active); + add_assoc_long(return_value, "inactive", (long)count_defined); +} + +/* + * Function name: libvirt_domain_is_persistent + * Since version: 0.4.9 + * Description: Function to get information whether domain is persiste= nt or not + * Arguments: @res [resource]: libvirt domain resource + * Returns: TRUE for persistent, FALSE for not persistent, -1 on e= rror + */ +PHP_FUNCTION(libvirt_domain_is_persistent) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int p; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + if ((p =3D virDomainIsPersistent(domain->domain)) < 0) + RETURN_LONG(-1); + + if (p =3D=3D 1) + RETURN_TRUE; + + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_lookup_by_name + * Since version: 0.4.1(-1) + * Description: Function is used to lookup for domain by it's name + * Arguments: @res [resource]: libvirt connection resource from libv= irt_connect() + * @name [string]: domain name to look for + * Returns: libvirt domain resource + */ +PHP_FUNCTION(libvirt_domain_lookup_by_name) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + strsize_t name_len; + char *name =3D NULL; + virDomainPtr domain =3D NULL; + php_libvirt_domain *res_domain; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); + if ((name =3D=3D NULL) || (name_len < 1)) + RETURN_FALSE; + domain =3D virDomainLookupByName(conn->conn, name); + if (domain =3D=3D NULL) + RETURN_FALSE; + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D domain; + res_domain->conn =3D conn; + + DPRINTF("%s: domain name =3D '%s', returning %p\n", PHPFUNC, name, res= _domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_get_xml_desc + * Since version: 0.4.1(-1), changed 0.4.2 + * Description: Function is used to get the domain's XML description + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL + * Returns: domain XML description string or result of xPath expre= ssion + */ +PHP_FUNCTION(libvirt_domain_get_xml_desc) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *tmp =3D NULL; + char *xml; + char *xpath =3D NULL; + strsize_t xpath_len; + zend_long flags =3D 0; + int retval =3D -1; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &xpath, &xpath_len, &flags); + if (xpath_len < 1) + xpath =3D NULL; + + DPRINTF("%s: Getting the XML for domain %p (xPath =3D %s)\n", PHPFUNC,= domain->domain, xpath); + + xml =3D virDomainGetXMLDesc(domain->domain, flags); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + VIRT_RETVAL_STRING(xml); + } else { + VIRT_RETVAL_STRING(tmp); + } + + free(tmp); + free(xml); +} + +/* + * Function name: libvirt_domain_get_disk_devices + * Since version: 0.4.4 + * Description: Function is used to get disk devices for the domain + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: list of domain disk devices + */ +PHP_FUNCTION(libvirt_domain_get_disk_devices) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *xml; + int retval =3D -1; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + DPRINTF("%s: Getting disk device list for domain %p\n", PHPFUNC, domai= n->domain); + + xml =3D virDomainGetXMLDesc(domain->domain, 0); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + array_init(return_value); + + free(get_string_from_xpath(xml, "//domain/devices/disk/target/@dev", &= return_value, &retval)); + free(xml); + + if (retval < 0) + add_assoc_long(return_value, "error_code", (long)retval); + else + add_assoc_long(return_value, "num", (long)retval); +} + +/* + * Function name: libvirt_domain_get_interface_devices + * Since version: 0.4.4 + * Description: Function is used to get network interface devices for = the domain + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: list of domain interface devices + */ +PHP_FUNCTION(libvirt_domain_get_interface_devices) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *xml; + int retval =3D -1; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + DPRINTF("%s: Getting interface device list for domain %p\n", PHPFUNC, = domain->domain); + + xml =3D virDomainGetXMLDesc(domain->domain, 0); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + array_init(return_value); + + free(get_string_from_xpath(xml, "//domain/devices/interface/target/@de= v", &return_value, &retval)); + free(xml); + + if (retval < 0) + add_assoc_long(return_value, "error_code", (long)retval); + else + add_assoc_long(return_value, "num", (long)retval); +} + +/* + * Function name: libvirt_domain_change_vcpus + * Since version: 0.4.2 + * Description: Function is used to change the VCPU count for the doma= in + * Arguments: @res [resource]: libvirt domain resource + * @numCpus [int]: number of VCPUs to be set for the guest + * @flags [int]: flags for virDomainSetVcpusFlags (availa= ble at http://libvirt.org/html/libvirt-libvirt.html#virDomainVcpuFlags) + * Returns: true on success, false on error + */ +PHP_FUNCTION(libvirt_domain_change_vcpus) +{ + zend_long numCpus, flags =3D 0; + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + + GET_DOMAIN_FROM_ARGS("rl|l", &zdomain, &numCpus, &flags); + + if (virDomainSetVcpusFlags(domain->domain, numCpus, flags) =3D=3D 0) { + RETURN_TRUE; + } else { + RETURN_FALSE; + } +} + +/* + * Function name: libvirt_domain_change_memory + * Since version: 0.4.2 + * Description: Function is used to change the domain memory allocation + * Arguments: @res [resource]: libvirt domain resource + * @allocMem [int]: number of MiBs to be set as immediate= memory value + * @allocMax [int]: number of MiBs to be set as the maxim= um allocation + * @flags [int]: flags + * Returns: new domain resource + */ +PHP_FUNCTION(libvirt_domain_change_memory) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *tmpA =3D NULL; + char *tmp1 =3D NULL; + char *tmp2 =3D NULL; + char *xml; + char *new_xml =3D NULL; + int new_len; + char *newXml =3D NULL; + zend_long xflags =3D 0; + zend_long allocMem =3D 0; + zend_long allocMax =3D 0; + // int pos =3D -1; + int len =3D 0; + php_libvirt_domain *res_domain =3D NULL; + php_libvirt_connection *conn =3D NULL; + virDomainPtr dom =3D NULL; + + GET_DOMAIN_FROM_ARGS("rll|l", &zdomain, &allocMem, &allocMax, &xflags); + + DPRINTF("%s: Changing domain memory count to %d MiB current/%d MiB max= , domain =3D %p\n", + PHPFUNC, (int)allocMem, (int)allocMax, domain->domain); + + allocMem *=3D 1024; + allocMax *=3D 1024; + + if (allocMem > allocMax) + allocMem =3D allocMax; + + xml =3D virDomainGetXMLDesc(domain->domain, xflags); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + snprintf(newXml, sizeof(newXml), " %d\n %d\n", allocMax, allocMem); + tmpA =3D strstr(xml, ""); + tmp1 =3D strstr(xml, "") + strlen(""); + // pos =3D strlen(xml) - strlen(tmp1); + len =3D strlen(xml) - strlen(tmpA); + + tmp2 =3D (char *)emalloc((len + 1) * sizeof(char)); + memset(tmp2, 0, len + 1); + memcpy(tmp2, xml, len); + + new_len =3D strlen(tmp1) + strlen(tmp2) + strlen(newXml) + 2; + new_xml =3D (char *)emalloc(new_len * sizeof(char)); + snprintf(new_xml, new_len, "%s\n%s%s", tmp2, newXml, tmp1); + + conn =3D domain->conn; + + dom =3D virDomainDefineXML(conn->conn, new_xml); + if (dom =3D=3D NULL) { + free(xml); + efree(new_xml); + RETURN_FALSE; + } + free(xml); + efree(new_xml); + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D dom; + res_domain->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_change_boot_devices + * Since version: 0.4.2 + * Description: Function is used to change the domain boot devices + * Arguments: @res [resource]: libvirt domain resource + * @first [string]: first boot device to be set + * @second [string]: second boot device to be set + * @flags [int]: flags + * Returns: new domain resource + */ +PHP_FUNCTION(libvirt_domain_change_boot_devices) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *tmpA =3D NULL; + char *tmp1 =3D NULL; + char *tmp2 =3D NULL; + char *xml; + char *new_xml =3D NULL; + int new_len; + char newXml[4096] =3D { 0 }; + zend_long xflags =3D 0; + char *first =3D NULL; + strsize_t first_len; + char *second =3D NULL; + strsize_t second_len; + // int pos =3D -1; + int len =3D 0; + php_libvirt_domain *res_domain =3D NULL; + php_libvirt_connection *conn =3D NULL; + virDomainPtr dom =3D NULL; + + GET_DOMAIN_FROM_ARGS("rss|l", &zdomain, &first, &first_len, &second, &= second_len, &xflags); + + xml =3D virDomainGetXMLDesc(domain->domain, xflags); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + DPRINTF("%s: Changing domain boot order, domain =3D %p\n", PHPFUNC, do= main->domain); + + if (!second || (strcmp(second, "-") =3D=3D 0)) + snprintf(newXml, sizeof(newXml), " \n", first= ); + else + snprintf(newXml, sizeof(newXml), " \n \n", first, second); + + tmpA =3D strstr(xml, "") + strlen(""); + tmp1 =3D strstr(xml, ""); + // pos =3D strlen(xml) - strlen(tmp1); + len =3D strlen(xml) - strlen(tmpA); + + tmp2 =3D (char *)emalloc((len + 1) * sizeof(char)); + memset(tmp2, 0, len + 1); + memcpy(tmp2, xml, len); + + new_len =3D strlen(tmp1) + strlen(tmp2) + strlen(newXml) + 2; + new_xml =3D (char *)emalloc(new_len * sizeof(char)); + snprintf(new_xml, new_len, "%s\n%s%s", tmp2, newXml, tmp1); + + conn =3D domain->conn; + + dom =3D virDomainDefineXML(conn->conn, new_xml); + if (dom =3D=3D NULL) { + DPRINTF("%s: Function failed, restoring original XML\n", PHPFUNC); + free(xml); + efree(newXml); + RETURN_FALSE; + } + free(xml); + efree(newXml); + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D dom; + res_domain->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_disk_add + * Since version: 0.4.2 + * Description: Function is used to add the disk to the virtual machin= e using set of API functions to make it as simple as possible for the user + * Arguments: @res [resource]: libvirt domain resource + * @img [string]: string for the image file on the host s= ystem + * @dev [string]: string for the device to be presented t= o the guest (e.g. hda) + * @typ [string]: bus type for the device in the guest, u= sually 'ide' or 'scsi' + * @driver [string]: driver type to be specified, like 'r= aw' or 'qcow2' + * @flags [int]: flags for getting the XML description + * Returns: new domain resource + */ +PHP_FUNCTION(libvirt_domain_disk_add) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *xml; + char *img =3D NULL; + strsize_t img_len; + char *dev =3D NULL; + strsize_t dev_len; + char *driver =3D NULL; + strsize_t driver_len; + char *typ =3D NULL; + strsize_t typ_len; + char *newXml =3D NULL; + zend_long xflags =3D 0; + int retval =3D -1; + char *xpath =3D NULL; + char *tmp =3D NULL; + + GET_DOMAIN_FROM_ARGS("rssss|l", &zdomain, &img, &img_len, &dev, &dev_l= en, &typ, &typ_len, &driver, &driver_len, &xflags); + + DPRINTF("%s: Domain %p, device =3D %s, image =3D %s, type =3D %s, driv= er =3D %s\n", PHPFUNC, + domain->domain, dev, img, typ, driver); + + xml =3D virDomainGetXMLDesc(domain->domain, xflags); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + goto error; + } + + if (asprintf(&xpath, "//domain/devices/disk/source[@file=3D\"%s\"]/./@= file", img) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (tmp !=3D NULL) { + free(tmp); + if (asprintf(&tmp, "Domain already has image %s connected",= img) < 0) + set_error("Out of memory" TSRMLS_CC); + else + set_error(tmp TSRMLS_CC); + goto error; + } + + free(xpath); + if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/./@dev= ", dev) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, newXml, NULL, &retval); + if (tmp !=3D NULL) { + free(tmp); + if (asprintf(&tmp, "Domain already has device %s connected"= , dev) < 0) + set_error("Out of memory" TSRMLS_CC); + else + set_error(tmp TSRMLS_CC); + goto error; + } + + if (asprintf(&newXml, + " \n" + " \n" + " \n" + " \n" + " ", driver, img, dev, typ) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + + if (virDomainAttachDeviceFlags(domain->domain, + newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { + set_error("Unable to attach disk" TSRMLS_CC); + goto error; + } + + free(tmp); + free(xpath); + free(xml); + RETURN_TRUE; + + error: + free(tmp); + free(xpath); + free(xml); + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_disk_remove + * Since version: 0.4.2 + * Description: Function is used to remove the disk from the virtual m= achine using set of API functions to make it as simple as possible + * Arguments: @res [resource]: libvirt domain resource + * @dev [string]: string for the device to be removed fro= m the guest (e.g. 'hdb') + * @flags [int]: flags for getting the XML description + * Returns: new domain resource + */ +PHP_FUNCTION(libvirt_domain_disk_remove) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *xml; + char *dev =3D NULL; + strsize_t dev_len; + char *newXml =3D NULL; + zend_long xflags =3D 0; + int retval =3D -1; + char *xpath =3D NULL; + char *tmp =3D NULL; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &dev, &dev_len, &xflags); + + DPRINTF("%s: Trying to remove %s from domain %p\n", PHPFUNC, dev, doma= in->domain); + + xml =3D virDomainGetXMLDesc(domain->domain, xflags); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/./@dev= ", dev) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (!tmp) { + if (asprintf(&tmp, "Device %s is not connected to the guest= ", dev) < 0) + set_error("Out of memory" TSRMLS_CC); + else + set_error(tmp TSRMLS_CC); + goto error; + } + + if (asprintf(&newXml, + " \n" + " \n" + " ", dev) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + + if (virDomainDetachDeviceFlags(domain->domain, + newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { + set_error("Unable to attach disk" TSRMLS_CC); + goto error; + } + + free(tmp); + free(xpath); + free(xml); + RETURN_TRUE; + + error: + free(tmp); + free(xpath); + free(xml); + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_nic_add + * Since version: 0.4.2 + * Description: Function is used to add the NIC card to the virtual ma= chine using set of API functions to make it as simple as possible for the u= ser + * Arguments: @res [resource]: libvirt domain resource + * @mac [string]: MAC string interpretation to be used fo= r the NIC device + * @network [string]: network name where to connect this = NIC + * @model [string]: string of the NIC model + * @flags [int]: flags for getting the XML description + * Returns: new domain resource + */ +PHP_FUNCTION(libvirt_domain_nic_add) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *xml; + char *mac =3D NULL; + strsize_t mac_len; + char *net =3D NULL; + strsize_t net_len; + char *model =3D NULL; + strsize_t model_len; + char *newXml =3D NULL; + zend_long xflags =3D 0; + int retval =3D -1; + char *xpath =3D NULL; + char *tmp =3D NULL; + + DPRINTF("%s: Entering\n", PHPFUNC); + + GET_DOMAIN_FROM_ARGS("rsss|l", &zdomain, &mac, &mac_len, &net, &net_le= n, &model, &model_len, &xflags); + if (model_len < 1) + model =3D NULL; + + DPRINTF("%s: domain =3D %p, mac =3D %s, net =3D %s, model =3D %s\n", P= HPFUNC, domain->domain, mac, net, model); + + xml =3D virDomainGetXMLDesc(domain->domain, xflags); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/./@mac", mac) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (tmp) { + free(tmp); + if (asprintf(&tmp, "Domain already has NIC device with MAC address= %s connected", mac) < 0) + set_error("Out of memory" TSRMLS_CC); + else + set_error(tmp TSRMLS_CC); + goto error; + } + + if (model) { + if (asprintf(&newXml, + " \n" + " \n" + " \n" + " \n" + " ", mac, net, model) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + } else { + if (asprintf(&newXml, + " \n" + " \n" + " \n" + " ", mac, net) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + } + + if (virDomainAttachDeviceFlags(domain->domain, + newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { + set_error("Unable to attach interface" TSRMLS_CC); + goto error; + } + + free(tmp); + free(xpath); + free(xml); + RETURN_TRUE; + + error: + free(tmp); + free(xpath); + free(xml); + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_nic_remove + * Since version: 0.4.2 + * Description: Function is used to remove the NIC from the virtual ma= chine using set of API functions to make it as simple as possible + * Arguments: @res [resource]: libvirt domain resource + * @dev [string]: string representation of the IP address= to be removed (e.g. 54:52:00:xx:yy:zz) + * @flags [int]: optional flags for getting the XML descr= iption + * Returns: new domain resource + */ +PHP_FUNCTION(libvirt_domain_nic_remove) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *xml; + char *mac =3D NULL; + strsize_t mac_len; + char *newXml =3D NULL; + zend_long xflags =3D 0; + int retval =3D -1; + char *xpath =3D NULL; + char *tmp =3D NULL; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &mac, &mac_len, &xflags); + + DPRINTF("%s: Trying to remove NIC device with MAC address %s from doma= in %p\n", PHPFUNC, mac, domain->domain); + + xml =3D virDomainGetXMLDesc(domain->domain, xflags); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/./@mac", mac) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (!tmp) { + free(tmp); + if (asprintf(&tmp, "Domain has no such interface with mac %s", mac= ) < 0) + set_error("Out of memory" TSRMLS_CC); + else + set_error(tmp TSRMLS_CC); + goto error; + } + + if (asprintf(&newXml, + " \n" + " \n" + " ", mac) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + + if (virDomainDetachDeviceFlags(domain->domain, + newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { + set_error("Unable to detach interface" TSRMLS_CC); + goto error; + } + + free(tmp); + free(xpath); + free(xml); + RETURN_TRUE; + + error: + free(tmp); + free(xpath); + free(xml); + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_attach_device + * Since version: 0.5.3 + * Description: Function is used to attach a virtual device to a domai= n. + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @xml [string]: XML description of one device. + * @flags [int]: optional flags to control how the device= is attached. Defaults to VIR_DOMAIN_AFFECT_LIVE + * Returns: TRUE for success, FALSE on error. + */ +PHP_FUNCTION(libvirt_domain_attach_device) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain =3D NULL; + char *xml =3D NULL; + strsize_t xml_len =3D 0; + zend_long flags =3D VIR_DOMAIN_AFFECT_LIVE; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &xml, &xml_len, &flags); + + if (virDomainAttachDeviceFlags(domain->domain, xml, flags) < 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_detach_device + * Since version: 0.5.3 + * Description: Function is used to detach a virtual device from a dom= ain. + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @xml [string]: XML description of one device. + * @flags [int]: optional flags to control how the device= is attached. Defaults to VIR_DOMAIN_AFFECT_LIVE + * Returns: TRUE for success, FALSE on error. + */ +PHP_FUNCTION(libvirt_domain_detach_device) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain =3D NULL; + char *xml =3D NULL; + strsize_t xml_len =3D 0; + zend_long flags =3D VIR_DOMAIN_AFFECT_LIVE; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &xml, &xml_len, &flags); + + if (virDomainDetachDeviceFlags(domain->domain, xml, flags) < 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_get_info + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's information + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: domain information array + */ +PHP_FUNCTION(libvirt_domain_get_info) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + virDomainInfo domainInfo; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainGetInfo(domain->domain, &domainInfo); + DPRINTF("%s: virDomainGetInfo(%p) returned %d\n", PHPFUNC, domain->dom= ain, retval); + if (retval !=3D 0) + RETURN_FALSE; + + array_init(return_value); + add_assoc_long(return_value, "maxMem", domainInfo.maxMem); + add_assoc_long(return_value, "memory", domainInfo.memory); + add_assoc_long(return_value, "state", (long)domainInfo.state); + add_assoc_long(return_value, "nrVirtCpu", domainInfo.nrVirtCpu); + add_assoc_double(return_value, "cpuUsed", (double)((double)domainInfo.= cpuTime/1000000000.0)); +} + +/* + * Function name: libvirt_domain_get_name + * Since version: 0.4.1(-1) + * Description: Function is used to get domain name from it's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: domain name string + */ +PHP_FUNCTION(libvirt_domain_get_name) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + const char *name =3D NULL; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + if (domain->domain =3D=3D NULL) + RETURN_FALSE; + + name =3D virDomainGetName(domain->domain); + DPRINTF("%s: virDomainGetName(%p) returned %s\n", PHPFUNC, domain->dom= ain, name); + if (name =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETURN_STRING(name); //we can use the copy mechanism as we need = not to free name (we even can not!) +} + +/* + * Function name: libvirt_domain_get_uuid + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's UUID in binary fo= rmat + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: domain UUID in binary format + */ +PHP_FUNCTION(libvirt_domain_get_uuid) +{ + + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *uuid; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + uuid =3D (char *)emalloc(VIR_UUID_BUFLEN); + retval =3D virDomainGetUUID(domain->domain, (unsigned char *)uuid); + DPRINTF("%s: virDomainGetUUID(%p, %p) returned %d\n", PHPFUNC, domain-= >domain, uuid, retval); + if (retval !=3D 0) + RETURN_FALSE; + + VIRT_RETVAL_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_domain_get_uuid_string + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's UUID in string fo= rmat + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: domain UUID string + */ +PHP_FUNCTION(libvirt_domain_get_uuid_string) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *uuid; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + uuid =3D (char *)emalloc(VIR_UUID_STRING_BUFLEN); + retval =3D virDomainGetUUIDString(domain->domain, uuid); + DPRINTF("%s: virDomainGetUUIDString(%p) returned %d (%s)\n", PHPFUNC, = domain->domain, retval, uuid); + if (retval !=3D 0) + RETURN_FALSE; + + VIRT_RETVAL_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_domain_get_id + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's ID, applicable to= running guests only + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: running domain ID or -1 if not running + */ +PHP_FUNCTION(libvirt_domain_get_id) +{ + + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainGetID(domain->domain); + DPRINTF("%s: virDomainGetID(%p) returned %d\n", PHPFUNC, domain->domai= n, retval); + + RETURN_LONG(retval); +} + +/* + * Function name: libvirt_domain_lookup_by_uuid + * Since version: 0.4.1(-1) + * Description: Function is used to lookup for domain by it's UUID in = the binary format + * Arguments: @res [resource]: libvirt connection resource from libv= irt_connect() + * @uuid [string]: binary defined UUID to look for + * Returns: libvirt domain resource + */ +PHP_FUNCTION(libvirt_domain_lookup_by_uuid) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + strsize_t uuid_len; + unsigned char *uuid =3D NULL; + virDomainPtr domain =3D NULL; + php_libvirt_domain *res_domain; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); + + if ((uuid =3D=3D NULL) || (uuid_len < 1)) + RETURN_FALSE; + domain =3D virDomainLookupByUUID(conn->conn, uuid); + if (domain =3D=3D NULL) + RETURN_FALSE; + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D domain; + res_domain->conn =3D conn; + + DPRINTF("%s: domain UUID =3D '%s', returning %p\n", PHPFUNC, uuid, res= _domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_lookup_by_uuid_string + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain by it's UUID that's= accepted in string format + * Arguments: @res [resource]: libvirt connection resource from libv= irt_connect() + * @uuid [string]: domain UUID [in string format] to look= for + * Returns: libvirt domain resource + */ +PHP_FUNCTION(libvirt_domain_lookup_by_uuid_string) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + strsize_t uuid_len; + char *uuid =3D NULL; + virDomainPtr domain =3D NULL; + php_libvirt_domain *res_domain; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); + + if ((uuid =3D=3D NULL) || (uuid_len < 1)) + RETURN_FALSE; + domain =3D virDomainLookupByUUIDString(conn->conn, uuid); + if (domain =3D=3D NULL) + RETURN_FALSE; + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D domain; + res_domain->conn =3D conn; + + DPRINTF("%s: domain UUID string =3D '%s', returning %p\n", PHPFUNC, uu= id, res_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_lookup_by_id + * Since version: 0.4.1(-1) + * Description: Function is used to get domain by it's ID, applicable = only to running guests + * Arguments: @conn [resource]: libvirt connection resource from lib= virt_connect() + * @id [string]: domain id to look for + * Returns: libvirt domain resource + */ +PHP_FUNCTION(libvirt_domain_lookup_by_id) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + zend_long id; + virDomainPtr domain =3D NULL; + php_libvirt_domain *res_domain; + + GET_CONNECTION_FROM_ARGS("rl", &zconn, &id); + + domain =3D virDomainLookupByID(conn->conn, (int)id); + if (domain =3D=3D NULL) + RETURN_FALSE; + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D domain; + res_domain->conn =3D conn; + + DPRINTF("%s: domain id =3D '%d', returning %p\n", PHPFUNC, (int)id, re= s_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_create + * Since version: 0.4.1(-1) + * Description: Function is used to create the domain identified by it= 's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: result of domain creation (startup) + */ +PHP_FUNCTION(libvirt_domain_create) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainCreate(domain->domain); + DPRINTF("%s: virDomainCreate(%p) returned %d\n", PHPFUNC, domain->doma= in, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_destroy + * Since version: 0.4.1(-1) + * Description: Function is used to destroy the domain identified by i= t's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: result of domain destroy + */ +PHP_FUNCTION(libvirt_domain_destroy) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainDestroy(domain->domain); + DPRINTF("%s: virDomainDestroy(%p) returned %d\n", PHPFUNC, domain->dom= ain, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_resume + * Since version: 0.4.1(-1) + * Description: Function is used to resume the domain identified by it= 's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: result of domain resume + */ +PHP_FUNCTION(libvirt_domain_resume) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainResume(domain->domain); + DPRINTF("%s: virDomainResume(%p) returned %d\n", PHPFUNC, domain->doma= in, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_core_dump + * Since version: 0.4.1(-2) + * Description: Function is used to dump core of the domain identified= by it's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @to [string]: to + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_core_dump) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + strsize_t to_len; + char *to; + + GET_DOMAIN_FROM_ARGS("rs", &zdomain, &to, &to_len); + + retval =3D virDomainCoreDump(domain->domain, to, 0); + DPRINTF("%s: virDomainCoreDump(%p, %s, 0) returned %d\n", PHPFUNC, dom= ain->domain, to, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_shutdown + * Since version: 0.4.1(-1) + * Description: Function is used to shutdown the domain identified by = it's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_shutdown) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainShutdown(domain->domain); + DPRINTF("%s: virDomainShutdown(%p) returned %d\n", PHPFUNC, domain->do= main, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_suspend + * Since version: 0.4.1(-1) + * Description: Function is used to suspend the domain identified by i= t's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_suspend) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainSuspend(domain->domain); + DPRINTF("%s: virDomainSuspend(%p) returned %d\n", PHPFUNC, domain->dom= ain, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_managedsave + * Since version: 0.4.1(-1) + * Description: Function is used to managed save the domain (domain wa= s unloaded from memory and it state saved to disk) identified by it's resou= rce + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_managedsave) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + retval =3D virDomainManagedSave(domain->domain, 0); + DPRINTF("%s: virDomainManagedSave(%p) returned %d\n", PHPFUNC, domain-= >domain, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_undefine + * Since version: 0.4.1(-1) + * Description: Function is used to undefine the domain identified by = it's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_undefine) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainUndefine(domain->domain); + DPRINTF("%s: virDomainUndefine(%p) returned %d\n", PHPFUNC, domain->do= main, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_reboot + * Since version: 0.4.1(-1) + * Description: Function is used to reboot the domain identified by it= 's resource + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @flags [int]: optional flags + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_reboot) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); + + retval =3D virDomainReboot(domain->domain, flags); + DPRINTF("%s: virDomainReboot(%p) returned %d\n", PHPFUNC, domain->doma= in, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_define_xml + * Since version: 0.4.1(-1) + * Description: Function is used to define the domain from XML string + * Arguments: @conn [resource]: libvirt connection resource + * @xml [string]: XML string to define guest from + * Returns: newly defined domain resource + */ +PHP_FUNCTION(libvirt_domain_define_xml) +{ + php_libvirt_domain *res_domain =3D NULL; + php_libvirt_connection *conn =3D NULL; + zval *zconn; + virDomainPtr domain =3D NULL; + char *xml; + strsize_t xml_len; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &xml, &xml_len); + + domain =3D virDomainDefineXML(conn->conn, xml); + if (domain =3D=3D NULL) + RETURN_FALSE; + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D domain; + res_domain->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_create_xml + * Since version: 0.4.1(-1) + * Description: Function is used to create the domain identified by it= 's resource + * Arguments: @conn [resource]: libvirt connection resource + * @xml [string]: XML string to create guest from + * Returns: newly started/created domain resource + */ +PHP_FUNCTION(libvirt_domain_create_xml) +{ + php_libvirt_domain *res_domain =3D NULL; + php_libvirt_connection *conn =3D NULL; + zval *zconn; + virDomainPtr domain =3D NULL; + char *xml; + strsize_t xml_len; + zend_long flags =3D 0; + + GET_CONNECTION_FROM_ARGS("rs|l", &zconn, &xml, &xml_len, &flags); + + domain =3D virDomainCreateXML(conn->conn, xml, flags); + DPRINTF("%s: virDomainCreateXML(%p, , 0) returned %p\n", PHPFUNC,= conn->conn, domain); + if (domain =3D=3D NULL) + RETURN_FALSE; + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D domain; + res_domain->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_xml_from_native + * Since version: 0.5.3 + * Description: Function is used to convert native configuration data = to libvirt domain XML + * Arguments: @conn [resource]: libvirt connection resource + * @format [string]: configuration format converting from + * @config_data [string]: content of the native config fi= le + * Returns: libvirt domain XML, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_xml_from_native) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + char *config_data =3D NULL; + char *format =3D NULL; + char *xml =3D NULL; + strsize_t config_data_len; + strsize_t format_len; + unsigned int flags =3D 0; + + GET_CONNECTION_FROM_ARGS("rss", &zconn, &format, &format_len, &config_= data, &config_data_len); + + xml =3D virConnectDomainXMLFromNative(conn->conn, format, config_data,= flags); + + if (xml =3D=3D NULL) { + set_error_if_unset("Cannot convert native format to XML" TSRMLS_CC= ); + RETURN_FALSE; + } + + VIRT_RETVAL_STRING(xml); + free(xml); +} + +/* + * Function name: libvirt_domain_xml_to_native + * Since version: 0.5.3 + * Description: Function is used to convert libvirt domain XML to nati= ve configuration + * Arguments: @conn [resource]: libvirt connection resource + * @format [string]: configuration format converting to + * @xml_data [string]: content of the libvirt domain xml = file + * Returns: contents of the native data file, FALSE on error +*/ +PHP_FUNCTION(libvirt_domain_xml_to_native) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + char *xml_data =3D NULL; + char *format =3D NULL; + char *config_data =3D NULL; + strsize_t xml_data_len; + strsize_t format_len; + unsigned int flags =3D 0; + + GET_CONNECTION_FROM_ARGS("rss", &zconn, &format, &format_len, &xml_dat= a, &xml_data_len); + + config_data =3D virConnectDomainXMLToNative(conn->conn, format, xml_da= ta, flags); + + if (config_data =3D=3D NULL) { + set_error_if_unset("Cannot convert to native format from XML" TSRM= LS_CC); + RETURN_FALSE; + } + + VIRT_RETVAL_STRING(config_data); + free(config_data); +} + +/* + * Function name: libvirt_domain_set_max_memory + * Since version: 0.5.1 + * Description: Function to set max memory for domain + * Arguments: @res [resource]: libvirt domain resource + * @memory [int]: memory size in 1024 bytes (Kb) + * Returns: TRUE for success, FALSE for failure + */ +PHP_FUNCTION(libvirt_domain_set_max_memory) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zend_long memory =3D 0; + + GET_DOMAIN_FROM_ARGS("rl", &zdomain, &memory); + + if (virDomainSetMaxMemory(domain->domain, memory) !=3D 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_set_memory + * Since version: 0.5.1 + * Description: Function to set memory for domain + * Arguments: @res [resource]: libvirt domain resource + * @memory [int]: memory size in 1024 bytes (Kb) + * Returns: TRUE for success, FALSE for failure + */ +PHP_FUNCTION(libvirt_domain_set_memory) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zend_long memory =3D 0; + + GET_DOMAIN_FROM_ARGS("rl", &zdomain, &memory); + + if (virDomainSetMemory(domain->domain, memory) !=3D 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_set_memory_flags + * Since version: 0.5.1 + * Description: Function to set max memory for domain + * Arguments: @res [resource]: libvirt domain resource + * @memory [int]: memory size in 1024 bytes (Kb) + * @flags [int]: bitwise-OR VIR_DOMAIN_MEM_* flags + * Returns: TRUE for success, FALSE for failure + */ +PHP_FUNCTION(libvirt_domain_set_memory_flags) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zend_long memory =3D 0; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("rl|l", &zdomain, &memory, &flags); + + if (virDomainSetMemoryFlags(domain->domain, memory, flags) !=3D 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_memory_peek + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's memory peek value + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @start [int]: start + * @size [int]: size + * @flags [int]: optional flags + * Returns: domain memory peek + */ +PHP_FUNCTION(libvirt_domain_memory_peek) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + zend_long flags =3D 0; + zend_long start; + zend_long size; + char *buff; + + GET_DOMAIN_FROM_ARGS("rlll", &zdomain, &start, &size, &flags); + if (start < 0) { + set_error("Negative argument start" TSRMLS_CC); + RETURN_FALSE; + } + buff =3D (char *)emalloc(size); + retval =3D virDomainMemoryPeek(domain->domain, start, size, buff, flag= s); + if (retval !=3D 0) + RETURN_FALSE; + VIRT_RETVAL_STRINGL(buff, size); + efree(buff); +} + +/* + * Function name: libvirt_domain_memory_stats + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's memory stats + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @flags [int]: optional flags + * Returns: domain memory stats array (same fields as virDomainMem= oryStats, please see libvirt documentation) + */ +PHP_FUNCTION(libvirt_domain_memory_stats) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + zend_long flags =3D 0; + int i; + struct _virDomainMemoryStat stats[VIR_DOMAIN_MEMORY_STAT_NR]; + + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); + + retval =3D virDomainMemoryStats(domain->domain, stats, VIR_DOMAIN_MEMO= RY_STAT_NR, flags); + DPRINTF("%s: virDomainMemoryStats(%p...) returned %d\n", PHPFUNC, doma= in->domain, retval); + + if (retval =3D=3D -1) + RETURN_FALSE; + LONGLONG_INIT; + array_init(return_value); + for (i =3D 0; i < retval; i++) + LONGLONG_INDEX(return_value, stats[i].tag, stats[i].val) +} + +/* + * Function name: libvirt_domain_block_commit + * Since version: 0.5.2(-1) + * Description: Function is used to commit block job + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @disk [string]: path to the block device, or device sh= orthand + * @base [string]: path to backing file to merge into, or= device shorthand, or NULL for default + * @top [string]: path to file within backing chain that = contains data to be merged, or device shorthand, or NULL to merge all possi= ble data + * @bandwidth [int]: (optional) specify bandwidth limit; = flags determine the unit + * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_COMMIT_* + * Returns: true on success fail on error + */ +PHP_FUNCTION(libvirt_domain_block_commit) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *disk =3D NULL; + strsize_t disk_len; + char *base =3D NULL; + strsize_t base_len; + char *top =3D NULL; + strsize_t top_len; + zend_long bandwidth =3D 0; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("rs|ssll", &zdomain, &disk, &disk_len, &base, &ba= se_len, &top, &top_len, &bandwidth, &flags); + if (strcmp(disk, "") =3D=3D 0) + RETURN_FALSE; + if (strcmp(base, "") =3D=3D 0) + base =3D NULL; + if (strcmp(top, "") =3D=3D 0) + top =3D NULL; + + retval =3D virDomainBlockCommit(domain->domain, disk, base, top, bandw= idth, flags); + if (retval =3D=3D -1) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_block_stats + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's block stats + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @path [string]: device path to get statistics about + * Returns: domain block stats array, fields are rd_req, rd_bytes,= wr_req, wr_bytes and errs + */ +PHP_FUNCTION(libvirt_domain_block_stats) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *path; + strsize_t path_len; + + struct _virDomainBlockStats stats; + + GET_DOMAIN_FROM_ARGS("rs", &zdomain, &path, &path_len); + + retval =3D virDomainBlockStats(domain->domain, path, &stats, sizeof st= ats); + DPRINTF("%s: virDomainBlockStats(%p, %s, , ) returned %d\= n", PHPFUNC, domain->domain, path, retval); + if (retval =3D=3D -1) + RETURN_FALSE; + + array_init(return_value); + LONGLONG_INIT; + LONGLONG_ASSOC(return_value, "rd_req", stats.rd_req); + LONGLONG_ASSOC(return_value, "rd_bytes", stats.rd_bytes); + LONGLONG_ASSOC(return_value, "wr_req", stats.wr_req); + LONGLONG_ASSOC(return_value, "wr_bytes", stats.wr_bytes); + LONGLONG_ASSOC(return_value, "errs", stats.errs); +} + +/* + * Function name: libvirt_domain_block_resize + * Since version: 0.5.1(-1) + * Description: Function is used to resize the domain's block device + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @path [string]: device path to resize + * @size [int]: size of device + * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_RESIZE_* + * Returns: true on success fail on error + */ +PHP_FUNCTION(libvirt_domain_block_resize) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *path; + strsize_t path_len; + zend_long size =3D 0; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("rsl|l", &zdomain, &path, &path_len, &size, &flag= s); + + retval =3D virDomainBlockResize(domain->domain, path, size, flags); + if (retval =3D=3D -1) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_block_job_info + * Since version: 0.5.2(-1) + * Description: Function is used to request block job information for = the given disk + * Arguments: @dom [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @disk [string]: path to the block device, or device sh= orthand + * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_COMMIT_* + * Returns: Array with status virDomainGetBlockJobInfo and blockjo= b information. + */ +PHP_FUNCTION(libvirt_domain_block_job_info) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *disk; + int disk_len; + long flags =3D 0; + virDomainBlockJobInfo info; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &disk, &disk_len, &flags); + + retval =3D virDomainGetBlockJobInfo(domain->domain, disk, &info, flags= ); + + array_init(return_value); + add_assoc_long(return_value, "status", (int)retval); + add_assoc_long(return_value, "type", (int)info.type); + add_assoc_long(return_value, "bandwidth", (unsigned long)info.bandwidt= h); + add_assoc_long(return_value, "cur", (unsigned long long)info.cur); + add_assoc_long(return_value, "end", (unsigned long long)info.end); +} + + +/* + * Function name: libvirt_domain_block_job_abort + * Since version: 0.5.1(-1) + * Description: Function is used to abort block job + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @path [string]: device path to resize + * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_JOB_ABORT= _* + * Returns: true on success fail on error + */ +PHP_FUNCTION(libvirt_domain_block_job_abort) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *path; + strsize_t path_len; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &path, &path_len, &flags); + + retval =3D virDomainBlockJobAbort(domain->domain, path, flags); + if (retval =3D=3D -1) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_block_job_set_speed + * Since version: 0.5.1(-1) + * Description: Function is used to set speed of block job + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @path [string]: device path to resize + * @bandwidth [int]: bandwidth + * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_JOB_SPEED= _BANDWIDTH_* + * Returns: true on success fail on error + */ +PHP_FUNCTION(libvirt_domain_block_job_set_speed) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *path; + strsize_t path_len; + zend_long bandwidth =3D 0; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("rsl|l", &zdomain, &path, &path_len, &bandwidth, = &flags); + + retval =3D virDomainBlockJobSetSpeed(domain->domain, path, bandwidth, = flags); + if (retval =3D=3D -1) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_interface_stats + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's interface stats + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @path [string]: path to interface device + * Returns: interface stats array of {tx|rx}_{bytes|packets|errs|d= rop} fields + */ +PHP_FUNCTION(libvirt_domain_interface_stats) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *path; + strsize_t path_len; + + struct _virDomainInterfaceStats stats; + + GET_DOMAIN_FROM_ARGS("rs", &zdomain, &path, &path_len); + + retval =3D virDomainInterfaceStats(domain->domain, path, &stats, sizeo= f stats); + DPRINTF("%s: virDomainInterfaceStats(%p, %s, , ) returned= %d\n", PHPFUNC, domain->domain, path, retval); + if (retval =3D=3D -1) + RETURN_FALSE; + + array_init(return_value); + LONGLONG_INIT; + LONGLONG_ASSOC(return_value, "rx_bytes", stats.rx_bytes); + LONGLONG_ASSOC(return_value, "rx_packets", stats.rx_packets); + LONGLONG_ASSOC(return_value, "rx_errs", stats.rx_errs); + LONGLONG_ASSOC(return_value, "rx_drop", stats.rx_drop); + LONGLONG_ASSOC(return_value, "tx_bytes", stats.tx_bytes); + LONGLONG_ASSOC(return_value, "tx_packets", stats.tx_packets); + LONGLONG_ASSOC(return_value, "tx_errs", stats.tx_errs); + LONGLONG_ASSOC(return_value, "tx_drop", stats.tx_drop); +} + +/* + * Function name: libvirt_domain_get_connect + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's connection resour= ce. This function should *not* be used! + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: libvirt connection resource + */ +PHP_FUNCTION(libvirt_domain_get_connect) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + php_libvirt_connection *conn; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + conn =3D domain->conn; + if (conn->conn =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETURN_RESOURCE(conn->resource); + /* since we're returning already registered resource, bump refcount */ + Z_ADDREF_P(return_value); +} + +/* + * Function name: libvirt_domain_migrate + * Since version: 0.4.1(-1) + * Description: Function is used migrate domain to another domain + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @dest_conn [string]: destination host connection object + * @flags [int]: migration flags + * @dname [string]: domain name to rename domain to on de= stination side + * @bandwidth [int]: migration bandwidth in Mbps + * Returns: libvirt domain resource for migrated domain + */ +PHP_FUNCTION(libvirt_domain_migrate) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + php_libvirt_connection *dconn =3D NULL; + zval *zdconn; + virDomainPtr destdomain =3D NULL; + php_libvirt_domain *res_domain; + + zend_long flags =3D 0; + char *dname; + strsize_t dname_len; + zend_long bandwidth; + + dname =3D NULL; + dname_len =3D 0; + bandwidth =3D 0; + + GET_DOMAIN_FROM_ARGS("rrl|sl", &zdomain, &zdconn, &flags, &dname, &dna= me_len, &bandwidth); + + if ((domain->domain =3D=3D NULL) || (domain->conn->conn =3D=3D NULL)) { + set_error("Domain object is not valid" TSRMLS_CC); + RETURN_FALSE; + } + + VIRT_FETCH_RESOURCE(dconn, php_libvirt_connection*, &zdconn, PHP_LIBVI= RT_CONNECTION_RES_NAME, le_libvirt_connection); + if ((dconn =3D=3D NULL) || (dconn->conn =3D=3D NULL)) { + set_error("Destination connection object is not valid" TSRMLS_CC); + RETURN_FALSE; + } + + destdomain =3D virDomainMigrate(domain->domain, dconn->conn, flags, dn= ame, NULL, bandwidth); + if (destdomain =3D=3D NULL) + RETURN_FALSE; + + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); + res_domain->domain =3D destdomain; + res_domain->conn =3D dconn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); + resource_change_counter(INT_RESOURCE_DOMAIN, dconn->conn, res_domain->= domain, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); +} + +/* + * Function name: libvirt_domain_migrate_to_uri + * Since version: 0.4.1(-1) + * Description: Function is used migrate domain to another libvirt dae= mon specified by it's URI + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @dest_uri [string]: destination URI to migrate to + * @flags [int]: migration flags + * @dname [string]: domain name to rename domain to on de= stination side + * @bandwidth [int]: migration bandwidth in Mbps + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_migrate_to_uri) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + long flags =3D 0; + char *duri; + strsize_t duri_len; + char *dname; + strsize_t dname_len; + zend_long bandwidth; + + dname =3D NULL; + dname_len =3D 0; + bandwidth =3D 0; + GET_DOMAIN_FROM_ARGS("rsl|sl", &zdomain, &duri, &duri_len, &flags, &dn= ame, &dname_len, &bandwidth); + + retval =3D virDomainMigrateToURI(domain->domain, duri, flags, dname, b= andwidth); + DPRINTF("%s: virDomainMigrateToURI() returned %d\n", PHPFUNC, retval); + + if (retval =3D=3D 0) + RETURN_TRUE; + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_migrate_to_uri2 + * Since version: 0.4.6(-1) + * Description: Function is used migrate domain to another libvirt dae= mon specified by it's URI + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @dconnuri [string]: URI for target libvirtd + * @miguri [string]: URI for invoking the migration + * @dxml [string]: XML config for launching guest on targ= et + * @flags [int]: migration flags + * @dname [string]: domain name to rename domain to on de= stination side + * @bandwidth [int]: migration bandwidth in Mbps + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_migrate_to_uri2) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *dconnuri; + strsize_t dconnuri_len; + char *miguri; + strsize_t miguri_len; + char *dxml; + strsize_t dxml_len; + zend_long flags =3D 0; + char *dname; + strsize_t dname_len; + zend_long bandwidth; + + dconnuri =3D NULL; + dconnuri_len =3D 0; + miguri =3D NULL; + miguri_len =3D 0; + dxml =3D NULL; + dxml_len =3D 0; + dname =3D NULL; + dname_len =3D 0; + bandwidth =3D 0; + GET_DOMAIN_FROM_ARGS("r|ssslsl", &zdomain, &dconnuri, &dconnuri_len, &= miguri, &miguri_len, &dxml, &dxml_len, &flags, &dname, &dname_len, &bandwid= th); + + // set to NULL if empty string + if (dconnuri_len =3D=3D 0) + dconnuri=3DNULL; + if (miguri_len =3D=3D 0) + miguri=3DNULL; + if (dxml_len =3D=3D 0) + dxml=3DNULL; + if (dname_len =3D=3D 0) + dname=3DNULL; + + retval =3D virDomainMigrateToURI2(domain->domain, dconnuri, miguri, dx= ml, flags, dname, bandwidth); + DPRINTF("%s: virDomainMigrateToURI2() returned %d\n", PHPFUNC, retval); + + if (retval =3D=3D 0) + RETURN_TRUE; + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_get_job_info + * Since version: 0.4.1(-1) + * Description: Function is used get job information for the domain + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: job information array of type, time, data, mem and fil= e fields + */ +PHP_FUNCTION(libvirt_domain_get_job_info) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + + struct _virDomainJobInfo jobinfo; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + retval =3D virDomainGetJobInfo(domain->domain, &jobinfo); + if (retval =3D=3D -1) + RETURN_FALSE; + + array_init(return_value); + LONGLONG_INIT; + add_assoc_long(return_value, "type", jobinfo.type); + LONGLONG_ASSOC(return_value, "time_elapsed", jobinfo.timeElapsed); + LONGLONG_ASSOC(return_value, "time_remaining", jobinfo.timeRemaining); + LONGLONG_ASSOC(return_value, "data_total", jobinfo.dataTotal); + LONGLONG_ASSOC(return_value, "data_processed", jobinfo.dataProcessed); + LONGLONG_ASSOC(return_value, "data_remaining", jobinfo.dataRemaining); + LONGLONG_ASSOC(return_value, "mem_total", jobinfo.memTotal); + LONGLONG_ASSOC(return_value, "mem_processed", jobinfo.memProcessed); + LONGLONG_ASSOC(return_value, "mem_remaining", jobinfo.memRemaining); + LONGLONG_ASSOC(return_value, "file_total", jobinfo.fileTotal); + LONGLONG_ASSOC(return_value, "file_processed", jobinfo.fileProcessed); + LONGLONG_ASSOC(return_value, "file_remaining", jobinfo.fileRemaining); +} + +/* + * Function name: libvirt_domain_xml_xpath + * Since version: 0.4.1(-1) + * Description: Function is used to get the result of xPath expression= that's run against the domain + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @xpath [string]: xPath expression to parse against the= domain + * @flags [int]: optional flags + * Returns: result of the expression in an array + */ +PHP_FUNCTION(libvirt_domain_xml_xpath) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zval *zpath; + char *xml; + char *tmp =3D NULL; + strsize_t path_len =3D -1; + zend_long flags =3D 0; + int rc =3D 0; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &zpath, &path_len, &flags); + + xml =3D virDomainGetXMLDesc(domain->domain, flags); + if (!xml) + RETURN_FALSE; + + array_init(return_value); + + free(get_string_from_xpath(xml, (char *)zpath, &return_value, &rc)); + if (return_value < 0) { + free(xml); + RETURN_FALSE; + } + + free(tmp); + free(xml); + + if (rc =3D=3D 0) + RETURN_FALSE; + + VIRT_ADD_ASSOC_STRING(return_value, "xpath", (char *)zpath); + if (rc < 0) + add_assoc_long(return_value, "error_code", (long)rc); +} + +/* + * Function name: libvirt_domain_get_block_info + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's block device info= rmation + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @dev [string]: device to get block information about + * Returns: domain block device information array of device, file = or partition, capacity, allocation and physical size + */ +PHP_FUNCTION(libvirt_domain_get_block_info) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *dev; + char *xml; + char *tmp =3D NULL; + strsize_t dev_len; + int isFile; + char *xpath =3D NULL; + + struct _virDomainBlockInfo info; + + GET_DOMAIN_FROM_ARGS("rs", &zdomain, &dev, &dev_len); + + /* Get XML for the domain */ + xml =3D virDomainGetXMLDesc(domain->domain, VIR_DOMAIN_XML_INACTIVE); + if (!xml) { + set_error("Cannot get domain XML" TSRMLS_CC); + RETURN_FALSE; + } + + isFile =3D 0; + + if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/../sou= rce/@dev", dev) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (retval < 0) { + set_error("Cannot get XPath expression result for device storage" = TSRMLS_CC); + goto error; + } + + if (retval =3D=3D 0) { + free(xpath); + if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/..= /source/@file", dev) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + free(tmp); + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (retval < 0) { + set_error("Cannot get XPath expression result for file storage= " TSRMLS_CC); + goto error; + } + isFile =3D 1; + } + + if (retval =3D=3D 0) { + set_error("No relevant node found" TSRMLS_CC); + goto error; + } + + retval =3D virDomainGetBlockInfo(domain->domain, tmp, &info, 0); + if (retval =3D=3D -1) { + set_error("Cannot get domain block information" TSRMLS_CC); + goto error; + } + + array_init(return_value); + LONGLONG_INIT; + VIRT_ADD_ASSOC_STRING(return_value, "device", dev); + + if (isFile) + VIRT_ADD_ASSOC_STRING(return_value, "file", tmp); + else + VIRT_ADD_ASSOC_STRING(return_value, "partition", tmp); + + free(xpath); + if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/../dri= ver/@type", dev) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + free(tmp); + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (tmp !=3D NULL) + VIRT_ADD_ASSOC_STRING(return_value, "type", tmp); + + LONGLONG_ASSOC(return_value, "capacity", info.capacity); + LONGLONG_ASSOC(return_value, "allocation", info.allocation); + LONGLONG_ASSOC(return_value, "physical", info.physical); + + free(xpath); + free(tmp); + free(xml); + return; + + error: + free(xpath); + free(tmp); + free(xml); + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_get_network_info + * Since version: 0.4.1(-1) + * Description: Function is used to get the domain's network informati= on + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @mac [string]: mac address of the network device + * Returns: domain network info array of MAC address, network name= and type of NIC card + */ +PHP_FUNCTION(libvirt_domain_get_network_info) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + char *mac; + char *xml; + char *tmp =3D NULL; + strsize_t mac_len; + char *xpath =3D NULL; + + GET_DOMAIN_FROM_ARGS("rs", &zdomain, &mac, &mac_len); + + /* Get XML for the domain */ + xml =3D virDomainGetXMLDesc(domain->domain, VIR_DOMAIN_XML_INACTIVE); + if (!xml) { + set_error("Cannot get domain XML" TSRMLS_CC); + RETURN_FALSE; + } + + DPRINTF("%s: Getting network information for NIC with MAC address '%s'= \n", PHPFUNC, mac); + if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/../source/@network", mac) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if (tmp =3D=3D NULL) { + set_error("Invalid XPath node for source network" TSRMLS_CC); + goto error; + } + + if (retval < 0) { + set_error("Cannot get XPath expression result for network source" = TSRMLS_CC); + goto error; + } + + array_init(return_value); + VIRT_ADD_ASSOC_STRING(return_value, "mac", mac); + VIRT_ADD_ASSOC_STRING(return_value, "network", tmp); + + free(tmp); + free(xpath); + + if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/../model/@type", mac) < 0) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "nic_type", tmp); + else + VIRT_ADD_ASSOC_STRING(return_value, "nic_type", "default"); + + free(xml); + free(xpath); + free(tmp); + return; + + error: + free(xml); + free(xpath); + free(tmp); + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_get_autostart + * Since version: 0.4.1(-1) + * Description: Function is getting the autostart value for the domain + * Arguments: @res [resource]: libvirt domain resource + * Returns: autostart value or -1 + */ +PHP_FUNCTION(libvirt_domain_get_autostart) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int flags =3D 0; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + if (virDomainGetAutostart (domain->domain, &flags) !=3D 0) { + RETURN_LONG(-1); + } + RETURN_LONG((long)flags); +} + +/* + * Function name: libvirt_domain_set_autostart + * Since version: 0.4.1(-1) + * Description: Function is setting the autostart value for the domain + * Arguments: @res [resource]: libvirt domain resource + * @flags [int]: flag to enable/disable autostart + * Returns: TRUE on success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_set_autostart) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zend_bool flags =3D 0; + + GET_DOMAIN_FROM_ARGS("rb", &zdomain, &flags); + + if (virDomainSetAutostart (domain->domain, flags) !=3D 0) { + RETURN_FALSE; + } + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_get_metadata + * Since version: 0.4.9 + * Description: Function retrieve appropriate domain element given by = @type. + * Arguments: @res [resource]: libvirt domain resource + * @type [int]: virDomainMetadataType type of description + * @uri [string]: XML namespace identifier + * @flags [int]: bitwise-OR of virDomainModificationImpact + * Returns: metadata string, NULL on error or FALSE on API not sup= ported + */ +PHP_FUNCTION(libvirt_domain_get_metadata) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zend_long type =3D 0; + zend_long flags =3D 0; + char *uri =3D NULL; + strsize_t uri_len; + char *ret =3D NULL; + + GET_DOMAIN_FROM_ARGS("rlsl", &zdomain, &type, &uri, &uri_len, &flags); + + if ((uri !=3D NULL) && (strlen(uri) =3D=3D 0)) + uri =3D NULL; + + ret =3D virDomainGetMetadata(domain->domain, type, uri, flags); + if (ret =3D=3D NULL) { + if (strstr(LIBVIRT_G(last_error), "not supported") !=3D NULL) + RETURN_FALSE; + RETURN_NULL(); + } else { + VIRT_RETVAL_STRING(ret); + free(ret); + } +} + +/* + * Function name: libvirt_domain_set_metadata + * Since version: 0.4.9 + * Description: Function sets the appropriate domain element given by = @type to the value of @description. No new lines are permitted. + * Arguments: @res [resource]: libvirt domain resource + * @type [int]: virDomainMetadataType type of description + * @metadata [string]: new metadata text + * @key [string]: XML namespace key or empty string (alia= s of NULL) + * @uri [string]: XML namespace identifier or empty strin= g (alias of NULL) + * @flags [int]: bitwise-OR of virDomainModificationImpact + * Returns: -1 on error, 0 on success + */ +PHP_FUNCTION(libvirt_domain_set_metadata) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + strsize_t metadata_len, key_len, uri_len; + char *metadata =3D NULL; + char *key =3D NULL; + char *uri =3D NULL; + zend_long type =3D 0; + zend_long flags =3D 0; + int rc; + + GET_DOMAIN_FROM_ARGS("rlsssl", &zdomain, &type, &metadata, &metadata_l= en, &key, &key_len, &uri, &uri_len, &flags); + + if ((key !=3D NULL) && (strlen(key) =3D=3D 0)) + key =3D NULL; + if ((uri !=3D NULL) && (strlen(uri) =3D=3D 0)) + uri =3D NULL; + + rc =3D virDomainSetMetadata(domain->domain, type, metadata, key, uri, = flags); + RETURN_LONG(rc); +} + +/* + * Function name: libvirt_domain_is_active + * Since version: 0.4.1(-1) + * Description: Function is getting information whether domain identif= ied by resource is active or not + * Arguments: @res [resource]: libvirt domain resource + * Returns: virDomainIsActive() result on the domain + */ +PHP_FUNCTION(libvirt_domain_is_active) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + RETURN_LONG(virDomainIsActive(domain->domain)); +} + +/* + * Function name: libvirt_domain_get_next_dev_ids + * Since version: 0.4.2 + * Description: This functions can be used to get the next free slot i= f you intend to add a new device identified by slot to the domain, e.g. NIC= device + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * Returns: next free slot number for the domain + */ +PHP_FUNCTION(libvirt_domain_get_next_dev_ids) +{ + long dom, bus, slot, func; + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + + GET_DOMAIN_FROM_ARGS("r", &zdomain); + + DPRINTF("%s: Getting the next dev ids for domain %p\n", PHPFUNC, domai= n->domain); + + dom =3D get_next_free_numeric_value(domain->domain, "//@domain"); + bus =3D get_next_free_numeric_value(domain->domain, "//@bus"); + slot =3D get_next_free_numeric_value(domain->domain, "//@slot"); + func =3D get_next_free_numeric_value(domain->domain, "//@func"); + + array_init(return_value); + add_assoc_long(return_value, "next_domain", dom); + add_assoc_long(return_value, "next_bus", bus); + add_assoc_long(return_value, "next_slot", slot); + add_assoc_long(return_value, "next_func", func); +} + +/* + * Function name: libvirt_domain_get_screenshot + * Since version: 0.4.2 + * Description: Function uses gvnccapture (if available) to get the sc= reenshot of the running domain + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @server [string]: server string for the host machine + * @scancode [int]: integer value of the scancode to be s= end to refresh screen + * Returns: PNG image binary data + */ +PHP_FUNCTION(libvirt_domain_get_screenshot) +{ +#ifndef EXTWIN + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + pid_t childpid =3D -1; + pid_t w =3D -1; + int retval =3D -1; + int fd =3D -1, fsize =3D -1; + char file[] =3D "/tmp/libvirt-php-tmp-XXXXXX"; + char *buf =3D NULL; + char *tmp =3D NULL; + char *xml =3D NULL; + int port =3D -1; + char *hostname =3D NULL; + strsize_t hostname_len; + zend_long scancode =3D 10; + const char *path; + char *pathDup =3D NULL; + char name[1024] =3D { 0 }; + int use_builtin =3D 0; + + path =3D get_feature_binary("screenshot"); + DPRINTF("%s: get_feature_binary('screenshot') returned %s\n", PHPFUNC,= path); + + if ((path =3D=3D NULL) || (access(path, X_OK) !=3D 0)) { + use_builtin =3D 1; + } else { + if (!(pathDup =3D strdup(path))) { + set_error("Out of memory" TSRMLS_CC); + goto error; + } + } + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &hostname, &hostname_len, &scan= code); + + xml =3D virDomainGetXMLDesc(domain->domain, 0); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + goto error; + } + + tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + set_error("Cannot get the VNC port" TSRMLS_CC); + goto error; + } + + if (mkstemp(file) =3D=3D 0) + goto error; + + /* Get the current hostname and override to localhost if local machine= */ + gethostname(name, 1024); + if (strcmp(name, hostname) =3D=3D 0) + hostname =3D strdup("localhost"); + + vnc_refresh_screen(hostname, tmp, scancode); + + if (use_builtin =3D=3D 1) { + DPRINTF("%s: Binary not found, using builtin approach to %s:%s, tm= p file =3D %s\n", PHPFUNC, hostname, tmp, file); + + if (vnc_get_bitmap(hostname, tmp, file) !=3D 0) { + set_error("Cannot use builtin approach to get VNC window conte= nts" TSRMLS_CC); + goto error; + } + } else { + port =3D atoi(tmp)-5900; + + DPRINTF("%s: Getting screenshot of %s:%d to temporary file %s\n", = PHPFUNC, hostname, port, file); + + childpid =3D fork(); + if (childpid =3D=3D -1) + goto error; + + if (childpid =3D=3D 0) { + char tmpp[64] =3D { 0 }; + + snprintf(tmpp, sizeof(tmpp), "%s:%d", hostname, port); + retval =3D execlp(path, basename(pathDup), tmpp, file, NULL); + _exit(retval); + } else { + do { + w =3D waitpid(childpid, &retval, 0); + if (w =3D=3D -1) + goto error; + } while (!WIFEXITED(retval) && !WIFSIGNALED(retval)); + } + + if (WEXITSTATUS(retval) !=3D 0) { + set_error("Cannot spawn utility to get screenshot" TSRMLS_CC); + goto error; + } + } + + fd =3D open(file, O_RDONLY); + fsize =3D lseek(fd, 0, SEEK_END); + lseek(fd, 0, SEEK_SET); + buf =3D emalloc((fsize + 1) * sizeof(char)); + memset(buf, 0, fsize + 1); + if (read(fd, buf, fsize) < 0) { + close(fd); + unlink(file); + goto error; + } + close(fd); + + if (access(file, F_OK) =3D=3D 0) { + DPRINTF("%s: Temporary file %s deleted\n", PHPFUNC, file); + unlink(file); + } + + /* This is necessary to make the output binary safe */ + VIRT_ZVAL_STRINGL(return_value, buf, fsize); + + efree(buf); + free(tmp); + free(xml); + free(pathDup); + return; + + error: + efree(buf); + free(tmp); + free(xml); + free(pathDup); + RETURN_FALSE; +#else + set_error("Function is not supported on Windows systems" TSRMLS_CC); + RETURN_FALSE; +#endif +} + +/* + * Function name: libvirt_domain_get_screenshot_api + * Since version: 0.4.5 + * Description: Function is trying to get domain screenshot using libv= irt virGetDomainScreenshot() API if available. + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_get_by_*() + * @screenID [int]: monitor ID from where to take screens= hot + * Returns: array of filename and mime type as type is hypervisor = specific, caller is responsible for temporary file deletion + */ +PHP_FUNCTION(libvirt_domain_get_screenshot_api) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zend_long screen =3D 0; + int fd =3D -1; + char file[] =3D "/tmp/libvirt-php-tmp-XXXXXX"; + virStreamPtr st =3D NULL; + char *mime =3D NULL; + const char *bin =3D get_feature_binary("screenshot-convert"); + +#ifdef EXTWIN + set_error_if_unset("Cannot get domain screenshot on Windows systems" T= SRMLS_CC); + RETURN_FALSE; +#endif + + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &screen); + + if (!(st =3D virStreamNew(domain->conn->conn, 0))) { + set_error("Cannot create new stream" TSRMLS_CC); + goto error; + } + + mime =3D virDomainScreenshot(domain->domain, st, screen, 0); + if (!mime) { + set_error_if_unset("Cannot get domain screenshot" TSRMLS_CC); + goto error; + } + + if (!(fd =3D mkstemp(file))) { + virStreamAbort(st); + set_error_if_unset("Cannot get create file to save domain screensh= ot" TSRMLS_CC); + goto error; + } + + if (virStreamRecvAll(st, streamSink, &fd) < 0) { + set_error_if_unset("Cannot receive screenshot data" TSRMLS_CC); + virStreamAbort(st); + goto error; + } + + if (virStreamFinish(st) < 0) { + set_error_if_unset("Cannot close stream for domain" TSRMLS_CC); + goto error; + } + + virStreamFree(st); + st =3D NULL; + + array_init(return_value); + if (bin) { + char tmp[4096] =3D { 0 }; + char fileNew[1024] =3D { 0 }; + int exitStatus; + + snprintf(fileNew, sizeof(fileNew), "%s.png", file); + snprintf(tmp, sizeof(tmp), "%s %s %s > /dev/null 2> /dev/null", bi= n, file, fileNew); + exitStatus =3D system(tmp); + if (WEXITSTATUS(exitStatus) !=3D 0) + goto error; + + unlink(file); + close(fd); + fd =3D -1; + VIRT_ADD_ASSOC_STRING(return_value, "file", fileNew); + VIRT_ADD_ASSOC_STRING(return_value, "mime", "image/png"); + } else { + close(fd); + fd =3D -1; + VIRT_ADD_ASSOC_STRING(return_value, "file", file); + VIRT_ADD_ASSOC_STRING(return_value, "mime", mime); + } + + free(mime); + return; + + error: + free(mime); + if (fd !=3D -1) { + unlink(file); + close(fd); + } + if (st) + virStreamFree(st); + RETURN_FALSE; +} + +/* + * Function name: libvirt_domain_get_screen_dimensions + * Since version: 0.4.3 + * Description: Function get screen dimensions of the VNC window + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @server [string]: server string of the host machine + * Returns: array of height and width on success, FALSE otherwise + */ +PHP_FUNCTION(libvirt_domain_get_screen_dimensions) +{ +#ifndef EXTWIN + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval =3D -1; + char *tmp =3D NULL; + char *xml =3D NULL; + char *hostname =3D NULL; + strsize_t hostname_len; + int ret; + int width; + int height; + + GET_DOMAIN_FROM_ARGS("rs", &zdomain, &hostname, &hostname_len); + + xml =3D virDomainGetXMLDesc(domain->domain, 0); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + goto error; + } + + tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + set_error("Cannot get the VNC port" TSRMLS_CC); + goto error; + } + + DPRINTF("%s: hostname =3D %s, port =3D %s\n", PHPFUNC, hostname, tmp); + ret =3D vnc_get_dimensions(hostname, tmp, &width, &height); + free(tmp); + if (ret !=3D 0) { + char error[1024] =3D { 0 }; + if (ret =3D=3D -9) + snprintf(error, sizeof(error), "Cannot connect to VNC server. = Please make sure domain is running and VNC graphics are set"); + else + snprintf(error, sizeof(error), "Cannot get screen dimensions, = error code =3D %d (%s)", ret, strerror(-ret)); + + set_error(error TSRMLS_CC); + goto error; + } + + array_init(return_value); + add_assoc_long(return_value, "width", (long)width); + add_assoc_long(return_value, "height", (long)height); + + free(tmp); + free(xml); + return; + + error: + free(tmp); + free(xml); + RETURN_FALSE; +#else + set_error("Function is not supported on Windows systems" TSRMLS_CC); + RETURN_FALSE; +#endif +} + +/* + * Function name: libvirt_domain_send_keys + * Since version: 0.4.2 + * Description: Function sends keys to the domain's VNC window + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @server [string]: server string of the host machine + * @scancode [int]: integer scancode to be sent to VNC wi= ndow + * Returns: TRUE on success, FALSE otherwise + */ +PHP_FUNCTION(libvirt_domain_send_keys) +{ +#ifndef EXTWIN + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval =3D -1; + char *tmp =3D NULL; + char *xml =3D NULL; + char *hostname =3D NULL; + strsize_t hostname_len; + char *keys =3D NULL; + strsize_t keys_len; + int ret =3D 0; + + GET_DOMAIN_FROM_ARGS("rss", &zdomain, &hostname, &hostname_len, &keys,= &keys_len); + + DPRINTF("%s: Sending %d VNC keys to %s...\n", PHPFUNC, (int)strlen((co= nst char *)keys), hostname); + + xml =3D virDomainGetXMLDesc(domain->domain, 0); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + goto error; + } + + tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + set_error("Cannot get the VNC port" TSRMLS_CC); + goto error; + } + + DPRINTF("%s: About to send string '%s' (%d keys) to %s:%s\n", PHPFUNC,= keys, (int)strlen((const char *)keys), hostname, tmp); + + ret =3D vnc_send_keys(hostname, tmp, keys); + DPRINTF("%s: Sequence sending result is %d\n", PHPFUNC, ret); + + if (!ret) { + char tmpp[64] =3D { 0 }; + snprintf(tmpp, sizeof(tmpp), "Cannot send keys, error code %d", re= t); + set_error(tmpp TSRMLS_CC); + goto error; + } + + free(tmp); + free(xml); + RETURN_TRUE; + + error: + free(tmp); + free(xml); + RETURN_FALSE; +#else + set_error("Function is not supported on Windows systems" TSRMLS_CC); + RETURN_FALSE; +#endif +} + +/* + * Function name: libvirt_domain_send_key_api + * Since version: 0.5.3 + * Description: Function sends keys to domain via libvirt API + * Arguments: @res[resource]: libvirt domain resource, e.g. from lib= virt_domaing_lookup_by_*() + * @codeset [int]: the codeset of keycodes, from virKeyco= deSet + * @holdtime [int]: the duration (in miliseconds) that th= e keys will be held + * @keycodes [array]: array of keycodes + * @flags [int]: extra flags; not used yet so callers sho= uld alway pass 0 + * Returns: TRUE for success, FALSE for failure + */ +PHP_FUNCTION(libvirt_domain_send_key_api) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + zend_long codeset; + zend_long holdtime =3D 0; + zend_long flags =3D 0; + zval *zkeycodes, *data =3D NULL; + HashTable *arr_hash =3D NULL; + HashPosition pointer; + int count, i; + uint *keycodes =3D NULL; + + GET_DOMAIN_FROM_ARGS("rlla|l", &zdomain, &codeset, &holdtime, &zkeycod= es, + &flags); + + arr_hash =3D Z_ARRVAL_P(zkeycodes); + count =3D zend_hash_num_elements(arr_hash); + + keycodes =3D (uint *) emalloc(count * sizeof(uint)); + + i =3D 0; + VIRT_FOREACH(arr_hash, pointer, data) { + if (Z_TYPE_P(data) =3D=3D IS_LONG) { + keycodes[i++] =3D (uint) Z_LVAL_P(data); + } + } VIRT_FOREACH_END(); + + if (virDomainSendKey(domain->domain, codeset, holdtime, keycodes, coun= t, + flags) !=3D 0) { + efree(keycodes); + RETURN_FALSE; + } + + efree(keycodes); + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_send_pointer_event + * Since version: 0.4.2 + * Description: Function sends keys to the domain's VNC window + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @server [string]: server string of the host machine + * @pos_x [int]: position on x-axis + * @pos_y [int]: position on y-axis + * @clicked [int]: mask of clicked buttons (0 for none, b= it 1 for button #1, bit 8 for button #8) + * @release [int]: boolean value (0 or 1) whether to rele= ase the buttons automatically once pressed + * Returns: TRUE on success, FALSE otherwise + */ +PHP_FUNCTION(libvirt_domain_send_pointer_event) +{ +#ifndef EXTWIN + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval =3D -1; + char *tmp =3D NULL; + char *xml =3D NULL; + char *hostname =3D NULL; + strsize_t hostname_len; + zend_long pos_x =3D 0; + zend_long pos_y =3D 0; + zend_long clicked =3D 0; + zend_bool release =3D 1; + int ret; + + GET_DOMAIN_FROM_ARGS("rslll|b", &zdomain, &hostname, &hostname_len, &p= os_x, &pos_y, &clicked, &release); + + xml =3D virDomainGetXMLDesc(domain->domain, 0); + if (!xml) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + goto error; + } + + tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + set_error("Cannot get the VNC port" TSRMLS_CC); + goto error; + } + + DPRINTF("%s: x =3D %d, y =3D %d, clicked =3D %d, release =3D %d, hostn= ame =3D %s...\n", PHPFUNC, (int) pos_x, (int) pos_y, (int) clicked, release= , hostname); + ret =3D vnc_send_pointer_event(hostname, tmp, pos_x, pos_y, clicked, r= elease); + if (!ret) { + char error[1024] =3D { 0 }; + if (ret =3D=3D -9) + snprintf(error, sizeof(error), "Cannot connect to VNC server. = Please make sure domain is running and VNC graphics are set"); + else + snprintf(error, sizeof(error), "Cannot send pointer event, err= or code =3D %d (%s)", ret, strerror(-ret)); + + set_error(error TSRMLS_CC); + goto error; + } + + free(tmp); + free(xml); + RETURN_TRUE; + + error: + free(tmp); + free(xml); + RETURN_FALSE; +#else + set_error("Function is not supported on Windows systems" TSRMLS_CC); + RETURN_FALSE; +#endif +} + +/* + * Function name: libvirt_domain_update_device + * Since version: 0.4.1(-1) + * Description: Function is used to update the domain's devices from t= he XML string + * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() + * @xml [string]: XML string for the update + * @flags [int]: Flags to update the device (VIR_DOMAIN_D= EVICE_MODIFY_CURRENT, VIR_DOMAIN_DEVICE_MODIFY_LIVE, VIR_DOMAIN_DEVICE_MODI= FY_CONFIG, VIR_DOMAIN_DEVICE_MODIFY_FORCE) + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_update_device) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + char *xml; + strsize_t xml_len; + zend_long flags; + int res; + + GET_DOMAIN_FROM_ARGS("rsl", &zdomain, &xml, &xml_len, &flags); + + res =3D virDomainUpdateDeviceFlags(domain->domain, xml, flags); + DPRINTF("%s: virDomainUpdateDeviceFlags(%p) returned %d\n", PHPFUNC, d= omain->domain, res); + if (res !=3D 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_qemu_agent_command + * Since version: 0.5.2(-1) + * Description: Function is used to send qemu-ga command + * Arguments: @res [resource]: libvirt domain resource, e.g. from lib= virt_domain_lookup_by_*() + * @cmd [string]: command + * @timeout [int] timeout for waiting (-2 block, -1 defaul= t, 0 no wait, >0 wait specific time + * @flags [int]: unknown + * Returns: String on success and FALSE on error + */ +PHP_FUNCTION(libvirt_domain_qemu_agent_command) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + const char *cmd; + strsize_t cmd_len; + char *ret; + zend_long timeout =3D -1; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("rs|ll", &zdomain, &cmd, &cmd_len, &timeout, &fla= gs); + + ret =3D virDomainQemuAgentCommand(domain->domain, cmd, timeout, flags); + if (ret =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETVAL_STRING(ret); + free(ret); +} + +/* + * Function name: libvirt_list_domains + * Since version: 0.4.1(-1) + * Description: Function is used to list domains on the connection + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt domain names array for the connection + */ +PHP_FUNCTION(libvirt_list_domains) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + int *ids; + char **names; + const char *name; + int i, rv; + virDomainPtr domain =3D NULL; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) + RETURN_FALSE; + + DPRINTF("%s: Found %d domains\n", PHPFUNC, expectedcount); + + ids =3D (int *)emalloc(sizeof(int) * expectedcount); + count =3D virConnectListDomains(conn->conn, ids, expectedcount); + DPRINTF("%s: virConnectListDomains returned %d domains\n", PHPFUNC, co= unt); + + array_init(return_value); + for (i =3D 0; i < count; i++) { + domain =3D virDomainLookupByID(conn->conn, ids[i]); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, domain, 1= TSRMLS_CC); + if (domain !=3D NULL) { + name =3D virDomainGetName(domain); + if (name !=3D NULL) { + DPRINTF("%s: Found running domain %s with ID =3D %d\n", PH= PFUNC, name, ids[i]); + VIRT_ADD_NEXT_INDEX_STRING(return_value, name); + } else { + DPRINTF("%s: Cannot get ID for running domain %d\n", PHPFU= NC, ids[i]); + } + } + rv =3D virDomainFree(domain); + if (rv !=3D 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainFree fai= led with %i on list_domain: %s", + rv, LIBVIRT_G(last_error)); + } else { + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, domai= n, 0 TSRMLS_CC); + } + domain =3D NULL; + } + efree(ids); + + expectedcount =3D virConnectNumOfDefinedDomains(conn->conn); + DPRINTF("%s: virConnectNumOfDefinedDomains returned %d domains\n", PHP= FUNC, expectedcount); + if (expectedcount < 0) { + DPRINTF("%s: virConnectNumOfDefinedDomains failed with error code = %d\n", PHPFUNC, expectedcount); + RETURN_FALSE; + } + + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virConnectListDefinedDomains(conn->conn, names, expectedcoun= t); + DPRINTF("%s: virConnectListDefinedDomains returned %d domains\n", PHPF= UNC, count); + if (count < 0) { + DPRINTF("%s: virConnectListDefinedDomains failed with error code %= d\n", PHPFUNC, count); + RETURN_FALSE; + } + + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + DPRINTF("%s: Found inactive domain %s\n", PHPFUNC, names[i]); + free(names[i]); + } + efree(names); +} + +/* + * Function name: libvirt_list_domain_resources + * Since version: 0.4.1(-1) + * Description: Function is used to list domain resources on the conne= ction + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt domain resources array for the connection + */ +PHP_FUNCTION(libvirt_list_domain_resources) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + int *ids; + char **names; + int i; + + virDomainPtr domain =3D NULL; + php_libvirt_domain *res_domain; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) + RETURN_FALSE; + + ids =3D (int *)emalloc(sizeof(int) * expectedcount); + count =3D virConnectListDomains(conn->conn, ids, expectedcount); + if ((count !=3D expectedcount) || (count < 0)) { + efree(ids); + RETURN_FALSE; + } + array_init(return_value); + for (i =3D 0; i < count; i++) { + domain =3D virDomainLookupByID(conn->conn, ids[i]); + if (domain !=3D NULL) { + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvir= t_domain)); + res_domain->domain =3D domain; + + res_domain->conn =3D conn; + + VIRT_REGISTER_LIST_RESOURCE(domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_d= omain->domain, 1 TSRMLS_CC); + } + } + efree(ids); + + if ((expectedcount =3D virConnectNumOfDefinedDomains(conn->conn)) < 0) + RETURN_FALSE; + + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virConnectListDefinedDomains(conn->conn, names, expectedcoun= t); + if ((count !=3D expectedcount) || (count < 0)) { + efree(names); + RETURN_FALSE; + } + for (i =3D 0; i < count; i++) { + domain =3D virDomainLookupByName(conn->conn, names[i]); + if (domain !=3D NULL) { + res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvir= t_domain)); + res_domain->domain =3D domain; + + res_domain->conn =3D conn; + + VIRT_REGISTER_LIST_RESOURCE(domain); + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_d= omain->domain, 1 TSRMLS_CC); + } + free(names[i]); + } + efree(names); +} + +/* + * Function name: libvirt_list_active_domain_ids + * Since version: 0.4.1(-1) + * Description: Function is used to list active domain IDs on the conn= ection + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt active domain ids array for the connection + */ +PHP_FUNCTION(libvirt_list_active_domain_ids) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + int *ids; + int i; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) + RETURN_FALSE; + + ids =3D (int *)emalloc(sizeof(int) * expectedcount); + count =3D virConnectListDomains(conn->conn, ids, expectedcount); + if ((count !=3D expectedcount) || (count < 0)) { + efree(ids); + RETURN_FALSE; + } + array_init(return_value); + for (i =3D 0; i < count; i++) + add_next_index_long(return_value, ids[i]); + efree(ids); +} + +/* + * Function name: libvirt_list_active_domains + * Since version: 0.4.1(-1) + * Description: Function is used to list active domain names on the co= nnection + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt active domain names array for the connection + */ +PHP_FUNCTION(libvirt_list_active_domains) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + int *ids; + int i; + virDomainPtr domain =3D NULL; + const char *name; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) + RETURN_FALSE; + + ids =3D (int *)emalloc(sizeof(int) * expectedcount); + count =3D virConnectListDomains(conn->conn, ids, expectedcount); + if ((count !=3D expectedcount) || (count < 0)) { + efree(ids); + RETURN_FALSE; + } + + array_init(return_value); + for (i =3D 0; i < count; i++) { + domain =3D virDomainLookupByID(conn->conn, ids[i]); + if (domain !=3D NULL) { + if (!(name =3D virDomainGetName(domain))) { + efree(ids); + RETURN_FALSE; + } + + VIRT_ADD_NEXT_INDEX_STRING(return_value, name); + + if (virDomainFree(domain)) + resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, d= omain, 0 TSRMLS_CC); + } + } + efree(ids); +} + +/* + * Function name: libvirt_list_inactive_domains + * Since version: 0.4.1(-1) + * Description: Function is used to list inactive domain names on the = connection + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt inactive domain names array for the connection + */ +PHP_FUNCTION(libvirt_list_inactive_domains) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + char **names; + int i; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((expectedcount =3D virConnectNumOfDefinedDomains(conn->conn)) < 0) + RETURN_FALSE; + + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virConnectListDefinedDomains(conn->conn, names, expectedcoun= t); + if ((count !=3D expectedcount) || (count < 0)) { + efree(names); + RETURN_FALSE; + } + + array_init(return_value); + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + efree(names); +} + diff --git a/src/libvirt-domain.h b/src/libvirt-domain.h new file mode 100644 index 0000000..dc0ab46 --- /dev/null +++ b/src/libvirt-domain.h @@ -0,0 +1,208 @@ +/* + * libvirt-domain.h: The PHP bindings to libvirt domain API + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_DOMAIN_H__ +# define __LIBVIRT_DOMAIN_H__ + +# include "libvirt-connection.h" + +# define PHP_LIBVIRT_DOMAIN_RES_NAME "Libvirt domain" +# define INT_RESOURCE_DOMAIN 0x02 + +# define DOMAIN_DISK_FILE 0x01 +# define DOMAIN_DISK_BLOCK 0x02 +# define DOMAIN_DISK_ACCESS_ALL 0x04 + +# define DOMAIN_FLAG_FEATURE_ACPI 0x01 +# define DOMAIN_FLAG_FEATURE_APIC 0x02 +# define DOMAIN_FLAG_FEATURE_PAE 0x04 +# define DOMAIN_FLAG_CLOCK_LOCALTIME 0x08 +# define DOMAIN_FLAG_TEST_LOCAL_VNC 0x10 +# define DOMAIN_FLAG_SOUND_AC97 0x20 + +# define GET_DOMAIN_FROM_ARGS(args, ...) = \ + do { = \ + reset_error(TSRMLS_C); = \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, = \ + args, = \ + __VA_ARGS__) =3D=3D FAILURE) { = \ + set_error("Invalid arguments" TSRMLS_CC); = \ + RETURN_FALSE; = \ + } = \ + = \ + VIRT_FETCH_RESOURCE(domain, php_libvirt_domain*, &zdomain, = \ + PHP_LIBVIRT_DOMAIN_RES_NAME, le_libvirt_domain= ); \ + if (domain =3D=3D NULL || domain->domain =3D=3D NULL) = \ + RETURN_FALSE; = \ + } while (0) = \ + +# define PHP_FE_LIBVIRT_DOMAIN = \ + PHP_FE(libvirt_domain_new, arginfo_libvirt_domain_ne= w) \ + PHP_FE(libvirt_domain_new_get_vnc, arginfo_libvirt_void) = \ + PHP_FE(libvirt_domain_get_counts, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_is_persistent, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_lookup_by_name, arginfo_libvirt_conn_name= ) \ + PHP_FE(libvirt_domain_get_xml_desc, arginfo_libvirt_conn_xpat= h) \ + PHP_FE(libvirt_domain_get_disk_devices, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_get_interface_devices, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_change_vcpus, arginfo_libvirt_domain_ch= ange_vcpus) \ + PHP_FE(libvirt_domain_change_memory, arginfo_libvirt_domain_ch= ange_memory) \ + PHP_FE(libvirt_domain_change_boot_devices, arginfo_libvirt_domain_ch= ange_boot_devices) \ + PHP_FE(libvirt_domain_disk_add, arginfo_libvirt_domain_di= sk_add) \ + PHP_FE(libvirt_domain_disk_remove, arginfo_libvirt_domain_di= sk_remove) \ + PHP_FE(libvirt_domain_nic_add, arginfo_libvirt_domain_ni= c_add) \ + PHP_FE(libvirt_domain_nic_remove, arginfo_libvirt_domain_ni= c_remove) \ + PHP_FE(libvirt_domain_attach_device, arginfo_libvirt_domain_at= tach_device) \ + PHP_FE(libvirt_domain_detach_device, arginfo_libvirt_domain_de= tach_device) \ + PHP_FE(libvirt_domain_get_info, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_get_name, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_get_uuid, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_get_uuid_string, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_get_id, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_lookup_by_uuid, arginfo_libvirt_conn_uuid= ) \ + PHP_FE(libvirt_domain_lookup_by_uuid_string, arginfo_libvirt_conn_uuid= ) \ + PHP_FE(libvirt_domain_lookup_by_id, arginfo_libvirt_domain_lo= okup_by_id) \ + PHP_FE(libvirt_domain_create, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_destroy, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_resume, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_core_dump, arginfo_libvirt_domain_co= re_dump) \ + PHP_FE(libvirt_domain_shutdown, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_suspend, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_managedsave, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_undefine, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_reboot, arginfo_libvirt_conn_flag= s) \ + PHP_FE(libvirt_domain_define_xml, arginfo_libvirt_conn_xml)= \ + PHP_FE(libvirt_domain_create_xml, arginfo_libvirt_conn_xml)= \ + PHP_FE(libvirt_domain_xml_from_native, arginfo_libvirt_domain_xm= l_from_native) \ + PHP_FE(libvirt_domain_xml_to_native, arginfo_libvirt_domain_xm= l_to_native) \ + PHP_FE(libvirt_domain_memory_peek, arginfo_libvirt_domain_me= mory_peek) \ + PHP_FE(libvirt_domain_memory_stats, arginfo_libvirt_conn_flag= s) \ + PHP_FE(libvirt_domain_set_memory, arginfo_libvirt_domain_se= t_memory) \ + PHP_FE(libvirt_domain_set_max_memory, arginfo_libvirt_domain_se= t_memory) \ + PHP_FE(libvirt_domain_set_memory_flags, arginfo_libvirt_domain_se= t_memory_flags) \ + PHP_FE(libvirt_domain_block_commit, arginfo_libvirt_domain_bl= ock_commit) \ + PHP_FE(libvirt_domain_block_stats, arginfo_libvirt_conn_path= ) \ + PHP_FE(libvirt_domain_block_resize, arginfo_libvirt_domain_bl= ock_resize) \ + PHP_FE(libvirt_domain_block_job_info, arginfo_libvirt_domain_bl= ock_job_info) \ + PHP_FE(libvirt_domain_block_job_abort, arginfo_libvirt_domain_bl= ock_job_abort) \ + PHP_FE(libvirt_domain_block_job_set_speed, arginfo_libvirt_domain_bl= ock_job_set_speed) \ + PHP_FE(libvirt_domain_interface_stats, arginfo_libvirt_conn_path= ) \ + PHP_FE(libvirt_domain_get_connect, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_migrate, arginfo_libvirt_domain_mi= grate) \ + PHP_FE(libvirt_domain_migrate_to_uri, arginfo_libvirt_domain_mi= grate_to_uri) \ + PHP_FE(libvirt_domain_migrate_to_uri2, arginfo_libvirt_domain_mi= grate_to_uri2) \ + PHP_FE(libvirt_domain_get_job_info, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_xml_xpath, arginfo_libvirt_domain_xm= l_xpath) \ + PHP_FE(libvirt_domain_get_block_info, arginfo_libvirt_domain_ge= t_block_info) \ + PHP_FE(libvirt_domain_get_network_info, arginfo_libvirt_domain_ge= t_network_info) \ + PHP_FE(libvirt_domain_get_autostart, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_set_autostart, arginfo_libvirt_conn_flag= s) \ + PHP_FE(libvirt_domain_get_metadata, arginfo_libvirt_domain_ge= t_metadata) \ + PHP_FE(libvirt_domain_set_metadata, arginfo_libvirt_domain_se= t_metadata) \ + PHP_FE(libvirt_domain_is_active, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_get_next_dev_ids, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_domain_get_screenshot, arginfo_libvirt_domain_ge= t_screenshot) \ + PHP_FE(libvirt_domain_get_screenshot_api, arginfo_libvirt_domain_ge= t_screenshot_api) \ + PHP_FE(libvirt_domain_get_screen_dimensions, arginfo_libvirt_domain_ge= t_screen_dimensions) \ + PHP_FE(libvirt_domain_send_keys, arginfo_libvirt_domain_se= nd_keys) \ + PHP_FE(libvirt_domain_send_key_api, arginfo_libvirt_domain_se= nd_key_api) \ + PHP_FE(libvirt_domain_send_pointer_event, arginfo_libvirt_domain_se= nd_pointer_event) \ + PHP_FE(libvirt_domain_update_device, arginfo_libvirt_domain_up= date_device) \ + PHP_FE(libvirt_domain_qemu_agent_command, arginfo_libvirt_domain_qe= mu_agent_command) \ + PHP_FE(libvirt_list_domains, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_list_domain_resources, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_list_active_domain_ids, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_list_active_domains, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_list_inactive_domains, arginfo_libvirt_conn) + +int le_libvirt_domain; + +typedef struct _php_libvirt_domain { + virDomainPtr domain; + php_libvirt_connection* conn; +} php_libvirt_domain; + +void php_libvirt_domain_dtor(virt_resource *rsrc TSRMLS_DC); + +PHP_FUNCTION(libvirt_domain_new); +PHP_FUNCTION(libvirt_domain_new_get_vnc); +PHP_FUNCTION(libvirt_domain_get_counts); +PHP_FUNCTION(libvirt_domain_is_persistent); +PHP_FUNCTION(libvirt_domain_lookup_by_name); +PHP_FUNCTION(libvirt_domain_get_xml_desc); +PHP_FUNCTION(libvirt_domain_get_disk_devices); +PHP_FUNCTION(libvirt_domain_get_interface_devices); +PHP_FUNCTION(libvirt_domain_get_screenshot); +PHP_FUNCTION(libvirt_domain_get_screenshot_api); +PHP_FUNCTION(libvirt_domain_get_screen_dimensions); +PHP_FUNCTION(libvirt_domain_change_vcpus); +PHP_FUNCTION(libvirt_domain_change_memory); +PHP_FUNCTION(libvirt_domain_change_boot_devices); +PHP_FUNCTION(libvirt_domain_disk_add); +PHP_FUNCTION(libvirt_domain_disk_remove); +PHP_FUNCTION(libvirt_domain_nic_add); +PHP_FUNCTION(libvirt_domain_nic_remove); +PHP_FUNCTION(libvirt_domain_attach_device); +PHP_FUNCTION(libvirt_domain_detach_device); +PHP_FUNCTION(libvirt_domain_get_info); +PHP_FUNCTION(libvirt_domain_get_uuid); +PHP_FUNCTION(libvirt_domain_get_uuid_string); +PHP_FUNCTION(libvirt_domain_get_name); +PHP_FUNCTION(libvirt_domain_get_id); +PHP_FUNCTION(libvirt_domain_lookup_by_uuid); +PHP_FUNCTION(libvirt_domain_lookup_by_uuid_string); +PHP_FUNCTION(libvirt_domain_lookup_by_id); +PHP_FUNCTION(libvirt_domain_create); +PHP_FUNCTION(libvirt_domain_destroy); +PHP_FUNCTION(libvirt_domain_resume); +PHP_FUNCTION(libvirt_domain_core_dump); +PHP_FUNCTION(libvirt_domain_shutdown); +PHP_FUNCTION(libvirt_domain_suspend); +PHP_FUNCTION(libvirt_domain_managedsave); +PHP_FUNCTION(libvirt_domain_undefine); +PHP_FUNCTION(libvirt_domain_reboot); +PHP_FUNCTION(libvirt_domain_define_xml); +PHP_FUNCTION(libvirt_domain_create_xml); +PHP_FUNCTION(libvirt_domain_xml_from_native); +PHP_FUNCTION(libvirt_domain_xml_to_native); +PHP_FUNCTION(libvirt_domain_set_max_memory); +PHP_FUNCTION(libvirt_domain_set_memory); +PHP_FUNCTION(libvirt_domain_set_memory_flags); +PHP_FUNCTION(libvirt_domain_memory_peek); +PHP_FUNCTION(libvirt_domain_memory_stats); +PHP_FUNCTION(libvirt_domain_block_commit); +PHP_FUNCTION(libvirt_domain_block_stats); +PHP_FUNCTION(libvirt_domain_block_resize); +PHP_FUNCTION(libvirt_domain_block_job_info); +PHP_FUNCTION(libvirt_domain_block_job_abort); +PHP_FUNCTION(libvirt_domain_block_job_set_speed); +PHP_FUNCTION(libvirt_domain_interface_stats); +PHP_FUNCTION(libvirt_domain_get_connect); +PHP_FUNCTION(libvirt_domain_migrate); +PHP_FUNCTION(libvirt_domain_migrate_to_uri); +PHP_FUNCTION(libvirt_domain_migrate_to_uri2); +PHP_FUNCTION(libvirt_domain_get_job_info); +PHP_FUNCTION(libvirt_domain_xml_xpath); +PHP_FUNCTION(libvirt_domain_get_block_info); +PHP_FUNCTION(libvirt_domain_get_network_info); +PHP_FUNCTION(libvirt_domain_get_autostart); +PHP_FUNCTION(libvirt_domain_set_autostart); +PHP_FUNCTION(libvirt_domain_get_metadata); +PHP_FUNCTION(libvirt_domain_set_metadata); +PHP_FUNCTION(libvirt_domain_is_active); +PHP_FUNCTION(libvirt_domain_get_next_dev_ids); +PHP_FUNCTION(libvirt_domain_send_keys); +PHP_FUNCTION(libvirt_domain_send_key_api); +PHP_FUNCTION(libvirt_domain_send_pointer_event); +PHP_FUNCTION(libvirt_domain_update_device); +PHP_FUNCTION(libvirt_domain_qemu_agent_command); +PHP_FUNCTION(libvirt_list_domains); +PHP_FUNCTION(libvirt_list_domain_resources); +PHP_FUNCTION(libvirt_list_active_domain_ids); +PHP_FUNCTION(libvirt_list_active_domains); +PHP_FUNCTION(libvirt_list_inactive_domains); + +#endif diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 80491da..05f36ae 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -23,6 +23,7 @@ #include "libvirt-connection.h" #include "libvirt-node.h" #include "libvirt-stream.h" +#include "libvirt-domain.h" =20 DEBUG_INIT("core"); =20 @@ -36,7 +37,6 @@ const char *features_binaries[] =3D { NULL }; #endif =20 /* ZEND thread safe per request globals definition */ -int le_libvirt_domain; int le_libvirt_storagepool; int le_libvirt_volume; int le_libvirt_network; @@ -477,78 +477,7 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE(libvirt_get_last_error, arginfo_libvirt_void) PHP_FE_LIBVIRT_CONNECTION PHP_FE_LIBVIRT_STREAM - /* Domain functions */ - PHP_FE(libvirt_domain_new, arginfo_libvirt_domain_ne= w) - PHP_FE(libvirt_domain_new_get_vnc, arginfo_libvirt_void) - PHP_FE(libvirt_domain_get_counts, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_is_persistent, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_lookup_by_name, arginfo_libvirt_conn_name) - PHP_FE(libvirt_domain_get_xml_desc, arginfo_libvirt_conn_xpat= h) - PHP_FE(libvirt_domain_get_disk_devices, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_get_interface_devices, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_change_vcpus, arginfo_libvirt_domain_ch= ange_vcpus) - PHP_FE(libvirt_domain_change_memory, arginfo_libvirt_domain_ch= ange_memory) - PHP_FE(libvirt_domain_change_boot_devices, arginfo_libvirt_domain_ch= ange_boot_devices) - PHP_FE(libvirt_domain_disk_add, arginfo_libvirt_domain_di= sk_add) - PHP_FE(libvirt_domain_disk_remove, arginfo_libvirt_domain_di= sk_remove) - PHP_FE(libvirt_domain_nic_add, arginfo_libvirt_domain_ni= c_add) - PHP_FE(libvirt_domain_nic_remove, arginfo_libvirt_domain_ni= c_remove) - PHP_FE(libvirt_domain_attach_device, arginfo_libvirt_domain_at= tach_device) - PHP_FE(libvirt_domain_detach_device, arginfo_libvirt_domain_de= tach_device) - PHP_FE(libvirt_domain_get_info, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_get_name, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_get_uuid, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_get_uuid_string, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_get_id, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_lookup_by_id, arginfo_libvirt_domain_lo= okup_by_id) - PHP_FE(libvirt_domain_lookup_by_uuid, arginfo_libvirt_conn_uuid) - PHP_FE(libvirt_domain_lookup_by_uuid_string, arginfo_libvirt_conn_uuid) - PHP_FE(libvirt_domain_destroy, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_create, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_resume, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_core_dump, arginfo_libvirt_domain_co= re_dump) - PHP_FE(libvirt_domain_shutdown, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_suspend, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_managedsave, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_undefine, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_reboot, arginfo_libvirt_conn_flag= s) - PHP_FE(libvirt_domain_define_xml, arginfo_libvirt_conn_xml) - PHP_FE(libvirt_domain_create_xml, arginfo_libvirt_conn_xml) - PHP_FE(libvirt_domain_xml_from_native, arginfo_libvirt_domain_xm= l_from_native) - PHP_FE(libvirt_domain_xml_to_native, arginfo_libvirt_domain_xm= l_to_native) - PHP_FE(libvirt_domain_memory_peek, arginfo_libvirt_domain_me= mory_peek) - PHP_FE(libvirt_domain_memory_stats, arginfo_libvirt_conn_flag= s) - PHP_FE(libvirt_domain_set_memory, arginfo_libvirt_domain_se= t_memory) - PHP_FE(libvirt_domain_set_max_memory, arginfo_libvirt_domain_se= t_memory) - PHP_FE(libvirt_domain_set_memory_flags, arginfo_libvirt_domain_se= t_memory_flags) - PHP_FE(libvirt_domain_block_commit, arginfo_libvirt_domain_bl= ock_commit) - PHP_FE(libvirt_domain_block_stats, arginfo_libvirt_conn_path) - PHP_FE(libvirt_domain_block_resize, arginfo_libvirt_domain_bl= ock_resize) - PHP_FE(libvirt_domain_block_job_abort, arginfo_libvirt_domain_bl= ock_job_abort) - PHP_FE(libvirt_domain_block_job_set_speed, arginfo_libvirt_domain_bl= ock_job_set_speed) - PHP_FE(libvirt_domain_block_job_info, arginfo_libvirt_domain_bl= ock_job_info) - PHP_FE(libvirt_domain_interface_stats, arginfo_libvirt_conn_path) - PHP_FE(libvirt_domain_get_connect, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_migrate, arginfo_libvirt_domain_mi= grate) - PHP_FE(libvirt_domain_migrate_to_uri, arginfo_libvirt_domain_mi= grate_to_uri) - PHP_FE(libvirt_domain_migrate_to_uri2, arginfo_libvirt_domain_mi= grate_to_uri2) - PHP_FE(libvirt_domain_get_job_info, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_xml_xpath, arginfo_libvirt_domain_xm= l_xpath) - PHP_FE(libvirt_domain_get_block_info, arginfo_libvirt_domain_ge= t_block_info) - PHP_FE(libvirt_domain_get_network_info, arginfo_libvirt_domain_ge= t_network_info) - PHP_FE(libvirt_domain_get_autostart, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_set_autostart, arginfo_libvirt_conn_flag= s) - PHP_FE(libvirt_domain_get_metadata, arginfo_libvirt_domain_ge= t_metadata) - PHP_FE(libvirt_domain_set_metadata, arginfo_libvirt_domain_se= t_metadata) - PHP_FE(libvirt_domain_is_active, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_get_next_dev_ids, arginfo_libvirt_conn) - PHP_FE(libvirt_domain_get_screenshot, arginfo_libvirt_domain_ge= t_screenshot) - PHP_FE(libvirt_domain_get_screenshot_api, arginfo_libvirt_domain_ge= t_screenshot_api) - PHP_FE(libvirt_domain_get_screen_dimensions, arginfo_libvirt_domain_ge= t_screen_dimensions) - PHP_FE(libvirt_domain_send_keys, arginfo_libvirt_domain_se= nd_keys) - PHP_FE(libvirt_domain_send_key_api, arginfo_libvirt_domain_se= nd_key_api) - PHP_FE(libvirt_domain_send_pointer_event, arginfo_libvirt_domain_se= nd_pointer_event) - PHP_FE(libvirt_domain_update_device, arginfo_libvirt_domain_up= date_device) + PHP_FE_LIBVIRT_DOMAIN /* Domain snapshot functions */ PHP_FE(libvirt_domain_has_current_snapshot, arginfo_libvirt_conn_optf= lags) PHP_FE(libvirt_domain_snapshot_create, arginfo_libvirt_conn_optf= lags) @@ -619,9 +548,7 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE(libvirt_nwfilter_lookup_by_uuid_string, arginfo_libvirt_conn_uu= id) PHP_FE(libvirt_nwfilter_lookup_by_uuid, arginfo_libvirt_conn_uuid) /* List functions */ - PHP_FE(libvirt_list_domains, arginfo_libvirt_conn) PHP_FE(libvirt_list_domain_snapshots, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_list_domain_resources, arginfo_libvirt_conn) PHP_FE(libvirt_list_nodedevs, arginfo_libvirt_conn_optc= ap) PHP_FE(libvirt_list_all_networks, arginfo_libvirt_conn_optf= lags) PHP_FE(libvirt_list_networks, arginfo_libvirt_conn_optf= lags) @@ -629,9 +556,6 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE(libvirt_list_active_storagepools, arginfo_libvirt_conn) PHP_FE(libvirt_list_inactive_storagepools, arginfo_libvirt_conn) PHP_FE(libvirt_storagepool_list_volumes, arginfo_libvirt_conn) - 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 */ @@ -644,13 +568,7 @@ static zend_function_entry libvirt_functions[] =3D { /* Debugging functions */ PHP_FE(libvirt_logfile_set, arginfo_libvirt_logfile_s= et) PHP_FE(libvirt_print_binding_resources, arginfo_libvirt_void) - /* Agent functions */ - PHP_FE(libvirt_domain_qemu_agent_command, arginfo_libvirt_domain_qe= mu_agent_command) -#ifdef PHP_FE_END PHP_FE_END -#else - {NULL, NULL, NULL} -#endif }; =20 =20 @@ -1321,34 +1239,6 @@ int is_local_connection(virConnectPtr conn) #endif } =20 -/* Destructor for domain resource */ -static void php_libvirt_domain_dtor(virt_resource *rsrc TSRMLS_DC) -{ - php_libvirt_domain *domain =3D (php_libvirt_domain *)rsrc->ptr; - int rv =3D 0; - - if (domain !=3D NULL) { - if (domain->domain !=3D NULL) { - if (!check_resource_allocation(domain->conn->conn, INT_RESOURC= E_DOMAIN, domain->domain TSRMLS_CC)) { - domain->domain =3D NULL; - efree(domain); - return; - } - - rv =3D virDomainFree(domain->domain); - if (rv !=3D 0) { - DPRINTF("%s: virDomainFree(%p) returned %d (%s)\n", __FUNC= TION__, domain->domain, rv, LIBVIRT_G(last_error)); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainFree= failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); - } else { - DPRINTF("%s: virDomainFree(%p) completed successfully\n", = __FUNCTION__, domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, domain->conn-= >conn, domain->domain, 0 TSRMLS_CC); - } - domain->domain =3D NULL; - } - efree(domain); - } -} - /* Destructor for storagepool resource */ static void php_libvirt_storagepool_dtor(virt_resource *rsrc TSRMLS_DC) { @@ -1837,19 +1727,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) } =20 /* Macros for obtaining resources from arguments */ -#define GET_DOMAIN_FROM_ARGS(args, ...) = \ - do { = \ - reset_error(TSRMLS_C); = \ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_AR= GS__) =3D=3D FAILURE) { \ - set_error("Invalid arguments" TSRMLS_CC); = \ - RETURN_FALSE; = \ - } = \ - = \ - VIRT_FETCH_RESOURCE(domain, php_libvirt_domain*, &zdomain, PHP_LIB= VIRT_DOMAIN_RES_NAME, le_libvirt_domain);\ - if ((domain =3D=3D NULL) || (domain->domain =3D=3D NULL)) = \ - RETURN_FALSE; = \ - } while (0) = \ - #define GET_NETWORK_FROM_ARGS(args, ...) = \ do { = \ reset_error(TSRMLS_C); = \ @@ -2831,3307 +2708,270 @@ char *installation_get_xml(int step, virConnectP= tr conn, char *name, int memMB, return (strlen(xml) > 0) ? strdup(xml) : NULL; } =20 -/* Domain functions */ - /* - * Function name: libvirt_domain_get_counts - * Since version: 0.4.1(-1) - * Description: Function is getting domain counts for all, active and = inactive domains - * Arguments: @conn [resource]: libvirt connection resource from lib= virt_connect() - * Returns: array of total, active and inactive (but defined) doma= in counts + * Private function name: streamSink + * Since version: 0.4.5 + * Description: Function to write stream to file, borrowed fro= m libvirt + * Arguments: @st [virStreamPtr]: stream pointer + * @bytes [void *]: buffer array + * @nbytes [int]: size of buffer + * @opaque [void *]: used for file descriptor + * Returns: write() error code as it's calling write */ -PHP_FUNCTION(libvirt_domain_get_counts) +int streamSink(virStreamPtr st ATTRIBUTE_UNUSED, + const char *bytes, size_t nbytes, void *opaque) { - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count_defined; - int count_active; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - count_defined =3D virConnectNumOfDefinedDomains(conn->conn); - count_active =3D virConnectNumOfDomains(conn->conn); + int *fd =3D (int *)opaque; =20 - array_init(return_value); - add_assoc_long(return_value, "total", (long)(count_defined + count_act= ive)); - add_assoc_long(return_value, "active", (long)count_active); - add_assoc_long(return_value, "inactive", (long)count_defined); + return write(*fd, bytes, nbytes); } =20 -/* - * Function name: libvirt_domain_is_persistent - * Since version: 0.4.9 - * Description: Function to get information whether domain is persiste= nt or not - * Arguments: @res [resource]: libvirt domain resource - * Returns: TRUE for persistent, FALSE for not persistent, -1 on e= rror - */ -PHP_FUNCTION(libvirt_domain_is_persistent) +void parse_array(zval *arr, tVMDisk *disk, tVMNetwork *network) { - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int p; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); + HashTable *arr_hash; + // int array_count; + zval *data; + php_libvirt_hash_key_info key; + HashPosition pointer; + unsigned long index; =20 - if ((p =3D virDomainIsPersistent(domain->domain)) < 0) - RETURN_LONG(-1); + arr_hash =3D Z_ARRVAL_P(arr); + //array_count =3D zend_hash_num_elements(arr_hash); =20 - if (p =3D=3D 1) - RETURN_TRUE; + if (disk !=3D NULL) + memset(disk, 0, sizeof(tVMDisk)); + if (network !=3D NULL) + memset(network, 0, sizeof(tVMNetwork)); =20 - RETURN_FALSE; + VIRT_FOREACH(arr_hash, pointer, data) { + if ((Z_TYPE_P(data) =3D=3D IS_STRING) || (Z_TYPE_P(data) =3D=3D IS= _LONG)) { + VIRT_HASH_CURRENT_KEY_INFO(arr_hash, pointer, index, key); + if (key.type =3D=3D HASH_KEY_IS_STRING) { + if (disk !=3D NULL) { + if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(key.na= me, "path") =3D=3D 0) + disk->path =3D strdup(Z_STRVAL_P(data)); + else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(k= ey.name, "driver") =3D=3D 0) + disk->driver =3D strdup(Z_STRVAL_P(data)); + else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(k= ey.name, "bus") =3D=3D 0) + disk->bus =3D strdup(Z_STRVAL_P(data)); + else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(k= ey.name, "dev") =3D=3D 0) + disk->dev =3D strdup(Z_STRVAL_P(data)); + else if (strcmp(key.name, "size") =3D=3D 0) { + if (Z_TYPE_P(data) =3D=3D IS_LONG) { + disk->size =3D Z_LVAL_P(data); + } else { + disk->size =3D size_def_to_mbytes(Z_STRVAL_P(d= ata)); + } + } else if ((Z_TYPE_P(data) =3D=3D IS_LONG) && strcmp(k= ey.name, "flags") =3D=3D 0) + disk->flags =3D Z_LVAL_P(data); + } else { + if (network !=3D NULL) { + if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(ke= y.name, "mac") =3D=3D 0) + network->mac =3D strdup(Z_STRVAL_P(data)); + else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strc= mp(key.name, "network") =3D=3D 0) + network->network =3D strdup(Z_STRVAL_P(data)); + else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strc= mp(key.name, "model") =3D=3D 0) + network->model =3D strdup(Z_STRVAL_P(data)); + } + } + } + } + } VIRT_FOREACH_END(); } =20 /* - * Function name: libvirt_domain_set_max_memory - * Since version: 0.5.1 - * Description: Function to set max memory for domain + * Function name: libvirt_domain_has_current_snapshot + * Since version: 0.4.1(-2) + * Description: Function is used to get the information whether domain= has the current snapshot * Arguments: @res [resource]: libvirt domain resource - * @memory [int]: memory size in 1024 bytes (Kb) - * Returns: TRUE for success, FALSE for failure + * @flags [int]: libvirt snapshot flags + * Returns: TRUE is domain has the current snapshot, otherwise FAL= SE (you may need to check for error using libvirt_get_last_error()) */ -PHP_FUNCTION(libvirt_domain_set_max_memory) +PHP_FUNCTION(libvirt_domain_has_current_snapshot) { php_libvirt_domain *domain =3D NULL; zval *zdomain; - zend_long memory =3D 0; + int retval; + zend_long flags =3D 0; =20 - GET_DOMAIN_FROM_ARGS("rl", &zdomain, &memory); + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); =20 - if (virDomainSetMaxMemory(domain->domain, memory) !=3D 0) + retval =3D virDomainHasCurrentSnapshot(domain->domain, flags); + if (retval <=3D 0) RETURN_FALSE; - RETURN_TRUE; } =20 /* - * Function name: libvirt_domain_set_memory - * Since version: 0.5.1 - * Description: Function to set memory for domain + * Function name: libvirt_domain_snapshot_lookup_by_name + * Since version: 0.4.1(-2) + * Description: This functions is used to lookup for the snapshot by i= t's name * Arguments: @res [resource]: libvirt domain resource - * @memory [int]: memory size in 1024 bytes (Kb) - * Returns: TRUE for success, FALSE for failure + * @name [string]: name of the snapshot to get the resour= ce + * @flags [int]: libvirt snapshot flags + * Returns: domain snapshot resource */ -PHP_FUNCTION(libvirt_domain_set_memory) +PHP_FUNCTION(libvirt_domain_snapshot_lookup_by_name) { php_libvirt_domain *domain =3D NULL; zval *zdomain; - zend_long memory =3D 0; + strsize_t name_len; + char *name =3D NULL; + zend_long flags =3D 0; + php_libvirt_snapshot *res_snapshot; + virDomainSnapshotPtr snapshot =3D NULL; =20 - GET_DOMAIN_FROM_ARGS("rl", &zdomain, &memory); + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &name, &name_len, &flags); =20 - if (virDomainSetMemory(domain->domain, memory) !=3D 0) + if ((name =3D=3D NULL) || (name_len < 1)) + RETURN_FALSE; + snapshot=3DvirDomainSnapshotLookupByName(domain->domain, name, flags); + if (snapshot =3D=3D NULL) RETURN_FALSE; =20 - RETURN_TRUE; + res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); + res_snapshot->domain =3D domain; + res_snapshot->snapshot =3D snapshot; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); + resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); } =20 /* - * Function name: libvirt_domain_set_memory_flags - * Since version: 0.5.1 - * Description: Function to set max memory for domain + * Function name: libvirt_domain_snapshot_create + * Since version: 0.4.1(-2) + * Description: This function creates the domain snapshot for the doma= in identified by it's resource * Arguments: @res [resource]: libvirt domain resource - * @memory [int]: memory size in 1024 bytes (Kb) - * @flags [int]: bitwise-OR VIR_DOMAIN_MEM_* flags - * Returns: TRUE for success, FALSE for failure + * @flags [int]: libvirt snapshot flags + * Returns: domain snapshot resource */ -PHP_FUNCTION(libvirt_domain_set_memory_flags) +PHP_FUNCTION(libvirt_domain_snapshot_create) { php_libvirt_domain *domain =3D NULL; + php_libvirt_snapshot *res_snapshot; zval *zdomain; - zend_long memory =3D 0; + virDomainSnapshotPtr snapshot =3D NULL; zend_long flags =3D 0; =20 - GET_DOMAIN_FROM_ARGS("rl|l", &zdomain, &memory, &flags); + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); =20 - if (virDomainSetMemoryFlags(domain->domain, memory, flags) !=3D 0) + snapshot =3D virDomainSnapshotCreateXML(domain->domain, "", flags); + DPRINTF("%s: virDomainSnapshotCreateXML(%p, ) returned %p\n", PHP= FUNC, domain->domain, snapshot); + if (snapshot =3D=3D NULL) RETURN_FALSE; =20 - RETURN_TRUE; + res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); + res_snapshot->domain =3D domain; + res_snapshot->snapshot =3D snapshot; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); + resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); } =20 /* - * Function name: libvirt_domain_get_autostart - * Since version: 0.4.1(-1) - * Description: Function is getting the autostart value for the domain - * Arguments: @res [resource]: libvirt domain resource - * Returns: autostart value or -1 + * Function name: libvirt_domain_snapshot_get_xml + * Since version: 0.4.1(-2) + * Description: Function is used to get the XML description of the sna= pshot identified by it's resource + * Arguments: @res [resource]: libvirt snapshot resource + * @flags [int]: libvirt snapshot flags + * Returns: XML description string for the snapshot */ -PHP_FUNCTION(libvirt_domain_get_autostart) +PHP_FUNCTION(libvirt_domain_snapshot_get_xml) { - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int flags =3D 0; + char *xml; + zval *zsnapshot; + php_libvirt_snapshot *snapshot; + zend_long flags =3D 0; =20 - GET_DOMAIN_FROM_ARGS("r", &zdomain); + GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); =20 - if (virDomainGetAutostart (domain->domain, &flags) !=3D 0) { - RETURN_LONG(-1); - } - RETURN_LONG((long)flags); + xml =3D virDomainSnapshotGetXMLDesc(snapshot->snapshot, flags); + if (xml =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETVAL_STRING(xml); + free(xml); } =20 /* - * Function name: libvirt_domain_set_autostart - * Since version: 0.4.1(-1) - * Description: Function is setting the autostart value for the domain - * Arguments: @res [resource]: libvirt domain resource - * @flags [int]: flag to enable/disable autostart + * Function name: libvirt_domain_snapshot_revert + * Since version: 0.4.1(-2) + * Description: Function is used to revert the domain state to the sta= te identified by the snapshot + * Arguments: @res [resource]: libvirt snapshot resource + * @flags [int]: libvirt snapshot flags * Returns: TRUE on success, FALSE on error */ -PHP_FUNCTION(libvirt_domain_set_autostart) +PHP_FUNCTION(libvirt_domain_snapshot_revert) { - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - zend_bool flags =3D 0; + zval *zsnapshot; + php_libvirt_snapshot *snapshot; + int ret; + zend_long flags =3D 0; =20 - GET_DOMAIN_FROM_ARGS("rb", &zdomain, &flags); + GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); =20 - if (virDomainSetAutostart (domain->domain, flags) !=3D 0) { + ret =3D virDomainRevertToSnapshot(snapshot->snapshot, flags); + DPRINTF("%s: virDomainRevertToSnapshot(%p, 0) returned %d\n", PHPFUNC,= snapshot->snapshot, ret); + if (ret =3D=3D -1) RETURN_FALSE; - } RETURN_TRUE; } =20 /* - * Function name: libvirt_domain_get_metadata - * Since version: 0.4.9 - * Description: Function retrieve appropriate domain element given by = @type. - * Arguments: @res [resource]: libvirt domain resource - * @type [int]: virDomainMetadataType type of description - * @uri [string]: XML namespace identifier - * @flags [int]: bitwise-OR of virDomainModificationImpact - * Returns: metadata string, NULL on error or FALSE on API not sup= ported + * Function name: libvirt_domain_snapshot_delete + * Since version: 0.4.1(-2) + * Description: Function is used to revert the domain state to the sta= te identified by the snapshot + * Arguments: @res [resource]: libvirt snapshot resource + * @flags [int]: 0 to delete just snapshot, VIR_SNAPSHOT_= DELETE_CHILDREN to delete snapshot children as well + * Returns: TRUE on success, FALSE on error */ -PHP_FUNCTION(libvirt_domain_get_metadata) +PHP_FUNCTION(libvirt_domain_snapshot_delete) { - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - zend_long type =3D 0; + zval *zsnapshot; + php_libvirt_snapshot *snapshot; zend_long flags =3D 0; - char *uri =3D NULL; - strsize_t uri_len; - char *ret =3D NULL; - - GET_DOMAIN_FROM_ARGS("rlsl", &zdomain, &type, &uri, &uri_len, &flags); + int retval; =20 - if ((uri !=3D NULL) && (strlen(uri) =3D=3D 0)) - uri =3D NULL; + GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); =20 - ret =3D virDomainGetMetadata(domain->domain, type, uri, flags); - if (ret =3D=3D NULL) { - if (strstr(LIBVIRT_G(last_error), "not supported") !=3D NULL) - RETURN_FALSE; - RETURN_NULL(); - } else { - VIRT_RETVAL_STRING(ret); - free(ret); - } + retval =3D virDomainSnapshotDelete(snapshot->snapshot, flags); + DPRINTF("%s: virDomainSnapshotDelete(%p, %d) returned %d\n", PHPFUNC, = snapshot->snapshot, (int) flags, retval); + if (retval =3D=3D -1) + RETURN_FALSE; + RETURN_TRUE; } =20 /* - * Function name: libvirt_domain_set_metadata - * Since version: 0.4.9 - * Description: Function sets the appropriate domain element given by = @type to the value of @description. No new lines are permitted. + * Function name: libvirt_list_domain_snapshots + * Since version: 0.4.1(-2) + * Description: Function is used to list domain snapshots for the doma= in specified by it's resource * Arguments: @res [resource]: libvirt domain resource - * @type [int]: virDomainMetadataType type of description - * @metadata [string]: new metadata text - * @key [string]: XML namespace key or empty string (alia= s of NULL) - * @uri [string]: XML namespace identifier or empty strin= g (alias of NULL) - * @flags [int]: bitwise-OR of virDomainModificationImpact - * Returns: -1 on error, 0 on success + * @flags [int]: libvirt snapshot flags + * Returns: libvirt domain snapshot names array */ -PHP_FUNCTION(libvirt_domain_set_metadata) +PHP_FUNCTION(libvirt_list_domain_snapshots) { php_libvirt_domain *domain =3D NULL; zval *zdomain; - strsize_t metadata_len, key_len, uri_len; - char *metadata =3D NULL; - char *key =3D NULL; - char *uri =3D NULL; - zend_long type =3D 0; + int count =3D -1; + int expectedcount =3D -1; + char **names; zend_long flags =3D 0; - int rc; - - GET_DOMAIN_FROM_ARGS("rlsssl", &zdomain, &type, &metadata, &metadata_l= en, &key, &key_len, &uri, &uri_len, &flags); - - if ((key !=3D NULL) && (strlen(key) =3D=3D 0)) - key =3D NULL; - if ((uri !=3D NULL) && (strlen(uri) =3D=3D 0)) - uri =3D NULL; + int i; =20 - rc =3D virDomainSetMetadata(domain->domain, type, metadata, key, uri, = flags); - RETURN_LONG(rc); -} + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); =20 -/* - * Function name: libvirt_domain_is_active - * Since version: 0.4.1(-1) - * Description: Function is getting information whether domain identif= ied by resource is active or not - * Arguments: @res [resource]: libvirt domain resource - * Returns: virDomainIsActive() result on the domain - */ -PHP_FUNCTION(libvirt_domain_is_active) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; + expectedcount =3D virDomainSnapshotNum(domain->domain, flags); + DPRINTF("%s: virDomainSnapshotNum(%p, 0) returned %d\n", PHPFUNC, doma= in->domain, expectedcount); =20 - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - RETURN_LONG(virDomainIsActive(domain->domain)); -} - -/* - * Function name: libvirt_domain_lookup_by_name - * Since version: 0.4.1(-1) - * Description: Function is used to lookup for domain by it's name - * Arguments: @res [resource]: libvirt connection resource from libv= irt_connect() - * @name [string]: domain name to look for - * Returns: libvirt domain resource - */ -PHP_FUNCTION(libvirt_domain_lookup_by_name) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - strsize_t name_len; - char *name =3D NULL; - virDomainPtr domain =3D NULL; - php_libvirt_domain *res_domain; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); - if ((name =3D=3D NULL) || (name_len < 1)) - RETURN_FALSE; - domain =3D virDomainLookupByName(conn->conn, name); - if (domain =3D=3D NULL) - RETURN_FALSE; - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D domain; - res_domain->conn =3D conn; - - DPRINTF("%s: domain name =3D '%s', returning %p\n", PHPFUNC, name, res= _domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_lookup_by_uuid - * Since version: 0.4.1(-1) - * Description: Function is used to lookup for domain by it's UUID in = the binary format - * Arguments: @res [resource]: libvirt connection resource from libv= irt_connect() - * @uuid [string]: binary defined UUID to look for - * Returns: libvirt domain resource - */ -PHP_FUNCTION(libvirt_domain_lookup_by_uuid) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - strsize_t uuid_len; - unsigned char *uuid =3D NULL; - virDomainPtr domain =3D NULL; - php_libvirt_domain *res_domain; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); - - if ((uuid =3D=3D NULL) || (uuid_len < 1)) - RETURN_FALSE; - domain =3D virDomainLookupByUUID(conn->conn, uuid); - if (domain =3D=3D NULL) - RETURN_FALSE; - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D domain; - res_domain->conn =3D conn; - - DPRINTF("%s: domain UUID =3D '%s', returning %p\n", PHPFUNC, uuid, res= _domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_qemu_agent_command - * Since version: 0.5.2(-1) - * Description: Function is used to send qemu-ga command - * Arguments: @res [resource]: libvirt domain resource, e.g. from lib= virt_domain_lookup_by_*() - * @cmd [string]: command - * @timeout [int] timeout for waiting (-2 block, -1 defaul= t, 0 no wait, >0 wait specific time - * @flags [int]: unknown - * Returns: String on success and FALSE on error - */ -PHP_FUNCTION(libvirt_domain_qemu_agent_command) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - const char *cmd; - strsize_t cmd_len; - char *ret; - zend_long timeout =3D -1; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("rs|ll", &zdomain, &cmd, &cmd_len, &timeout, &fla= gs); - - ret =3D virDomainQemuAgentCommand(domain->domain, cmd, timeout, flags); - if (ret =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETVAL_STRING(ret); - free(ret); -} - -/* - * Function name: libvirt_domain_lookup_by_uuid_string - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain by it's UUID that's= accepted in string format - * Arguments: @res [resource]: libvirt connection resource from libv= irt_connect() - * @uuid [string]: domain UUID [in string format] to look= for - * Returns: libvirt domain resource - */ -PHP_FUNCTION(libvirt_domain_lookup_by_uuid_string) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - strsize_t uuid_len; - char *uuid =3D NULL; - virDomainPtr domain =3D NULL; - php_libvirt_domain *res_domain; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); - - if ((uuid =3D=3D NULL) || (uuid_len < 1)) - RETURN_FALSE; - domain =3D virDomainLookupByUUIDString(conn->conn, uuid); - if (domain =3D=3D NULL) - RETURN_FALSE; - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D domain; - res_domain->conn =3D conn; - - DPRINTF("%s: domain UUID string =3D '%s', returning %p\n", PHPFUNC, uu= id, res_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_lookup_by_id - * Since version: 0.4.1(-1) - * Description: Function is used to get domain by it's ID, applicable = only to running guests - * Arguments: @conn [resource]: libvirt connection resource from lib= virt_connect() - * @id [string]: domain id to look for - * Returns: libvirt domain resource - */ -PHP_FUNCTION(libvirt_domain_lookup_by_id) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - zend_long id; - virDomainPtr domain =3D NULL; - php_libvirt_domain *res_domain; - - GET_CONNECTION_FROM_ARGS("rl", &zconn, &id); - - domain =3D virDomainLookupByID(conn->conn, (int)id); - if (domain =3D=3D NULL) - RETURN_FALSE; - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D domain; - res_domain->conn =3D conn; - - DPRINTF("%s: domain id =3D '%d', returning %p\n", PHPFUNC, (int)id, re= s_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_get_name - * Since version: 0.4.1(-1) - * Description: Function is used to get domain name from it's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: domain name string - */ -PHP_FUNCTION(libvirt_domain_get_name) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - const char *name =3D NULL; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - if (domain->domain =3D=3D NULL) - RETURN_FALSE; - - name =3D virDomainGetName(domain->domain); - DPRINTF("%s: virDomainGetName(%p) returned %s\n", PHPFUNC, domain->dom= ain, name); - if (name =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETURN_STRING(name); //we can use the copy mechanism as we need = not to free name (we even can not!) -} - -/* - * Function name: libvirt_domain_get_uuid_string - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's UUID in string fo= rmat - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: domain UUID string - */ -PHP_FUNCTION(libvirt_domain_get_uuid_string) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *uuid; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - uuid =3D (char *)emalloc(VIR_UUID_STRING_BUFLEN); - retval =3D virDomainGetUUIDString(domain->domain, uuid); - DPRINTF("%s: virDomainGetUUIDString(%p) returned %d (%s)\n", PHPFUNC, = domain->domain, retval, uuid); - if (retval !=3D 0) - RETURN_FALSE; - - VIRT_RETVAL_STRING(uuid); - efree(uuid); -} - -/* - * Private function name: streamSink - * Since version: 0.4.5 - * Description: Function to write stream to file, borrowed fro= m libvirt - * Arguments: @st [virStreamPtr]: stream pointer - * @bytes [void *]: buffer array - * @nbytes [int]: size of buffer - * @opaque [void *]: used for file descriptor - * Returns: write() error code as it's calling write - */ -static int streamSink(virStreamPtr st ATTRIBUTE_UNUSED, - const char *bytes, size_t nbytes, void *opaque) -{ - int *fd =3D (int *)opaque; - - return write(*fd, bytes, nbytes); -} - -/* - * Function name: libvirt_domain_get_screenshot_api - * Since version: 0.4.5 - * Description: Function is trying to get domain screenshot using libv= irt virGetDomainScreenshot() API if available. - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_get_by_*() - * @screenID [int]: monitor ID from where to take screens= hot - * Returns: array of filename and mime type as type is hypervisor = specific, caller is responsible for temporary file deletion - */ -PHP_FUNCTION(libvirt_domain_get_screenshot_api) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - zend_long screen =3D 0; - int fd =3D -1; - char file[] =3D "/tmp/libvirt-php-tmp-XXXXXX"; - virStreamPtr st =3D NULL; - char *mime =3D NULL; - const char *bin =3D get_feature_binary("screenshot-convert"); - -#ifdef EXTWIN - set_error_if_unset("Cannot get domain screenshot on Windows systems" T= SRMLS_CC); - RETURN_FALSE; -#endif - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &screen); - - if (!(st =3D virStreamNew(domain->conn->conn, 0))) { - set_error("Cannot create new stream" TSRMLS_CC); - goto error; - } - - mime =3D virDomainScreenshot(domain->domain, st, screen, 0); - if (!mime) { - set_error_if_unset("Cannot get domain screenshot" TSRMLS_CC); - goto error; - } - - if (!(fd =3D mkstemp(file))) { - virStreamAbort(st); - set_error_if_unset("Cannot get create file to save domain screensh= ot" TSRMLS_CC); - goto error; - } - - if (virStreamRecvAll(st, streamSink, &fd) < 0) { - set_error_if_unset("Cannot receive screenshot data" TSRMLS_CC); - virStreamAbort(st); - goto error; - } - - if (virStreamFinish(st) < 0) { - set_error_if_unset("Cannot close stream for domain" TSRMLS_CC); - goto error; - } - - virStreamFree(st); - st =3D NULL; - - array_init(return_value); - if (bin) { - char tmp[4096] =3D { 0 }; - char fileNew[1024] =3D { 0 }; - int exitStatus; - - snprintf(fileNew, sizeof(fileNew), "%s.png", file); - snprintf(tmp, sizeof(tmp), "%s %s %s > /dev/null 2> /dev/null", bi= n, file, fileNew); - exitStatus =3D system(tmp); - if (WEXITSTATUS(exitStatus) !=3D 0) - goto error; - - unlink(file); - close(fd); - fd =3D -1; - VIRT_ADD_ASSOC_STRING(return_value, "file", fileNew); - VIRT_ADD_ASSOC_STRING(return_value, "mime", "image/png"); - } else { - close(fd); - fd =3D -1; - VIRT_ADD_ASSOC_STRING(return_value, "file", file); - VIRT_ADD_ASSOC_STRING(return_value, "mime", mime); - } - - free(mime); - return; - - error: - free(mime); - if (fd !=3D -1) { - unlink(file); - close(fd); - } - if (st) - virStreamFree(st); - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_get_screenshot - * Since version: 0.4.2 - * Description: Function uses gvnccapture (if available) to get the sc= reenshot of the running domain - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @server [string]: server string for the host machine - * @scancode [int]: integer value of the scancode to be s= end to refresh screen - * Returns: PNG image binary data - */ -PHP_FUNCTION(libvirt_domain_get_screenshot) -{ -#ifndef EXTWIN - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - pid_t childpid =3D -1; - pid_t w =3D -1; - int retval =3D -1; - int fd =3D -1, fsize =3D -1; - char file[] =3D "/tmp/libvirt-php-tmp-XXXXXX"; - char *buf =3D NULL; - char *tmp =3D NULL; - char *xml =3D NULL; - int port =3D -1; - char *hostname =3D NULL; - strsize_t hostname_len; - zend_long scancode =3D 10; - const char *path; - char *pathDup =3D NULL; - char name[1024] =3D { 0 }; - int use_builtin =3D 0; - - path =3D get_feature_binary("screenshot"); - DPRINTF("%s: get_feature_binary('screenshot') returned %s\n", PHPFUNC,= path); - - if ((path =3D=3D NULL) || (access(path, X_OK) !=3D 0)) { - use_builtin =3D 1; - } else { - if (!(pathDup =3D strdup(path))) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - } - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &hostname, &hostname_len, &scan= code); - - xml =3D virDomainGetXMLDesc(domain->domain, 0); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - goto error; - } - - tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - set_error("Cannot get the VNC port" TSRMLS_CC); - goto error; - } - - if (mkstemp(file) =3D=3D 0) - goto error; - - /* Get the current hostname and override to localhost if local machine= */ - gethostname(name, 1024); - if (strcmp(name, hostname) =3D=3D 0) - hostname =3D strdup("localhost"); - - vnc_refresh_screen(hostname, tmp, scancode); - - if (use_builtin =3D=3D 1) { - DPRINTF("%s: Binary not found, using builtin approach to %s:%s, tm= p file =3D %s\n", PHPFUNC, hostname, tmp, file); - - if (vnc_get_bitmap(hostname, tmp, file) !=3D 0) { - set_error("Cannot use builtin approach to get VNC window conte= nts" TSRMLS_CC); - goto error; - } - } else { - port =3D atoi(tmp)-5900; - - DPRINTF("%s: Getting screenshot of %s:%d to temporary file %s\n", = PHPFUNC, hostname, port, file); - - childpid =3D fork(); - if (childpid =3D=3D -1) - goto error; - - if (childpid =3D=3D 0) { - char tmpp[64] =3D { 0 }; - - snprintf(tmpp, sizeof(tmpp), "%s:%d", hostname, port); - retval =3D execlp(path, basename(pathDup), tmpp, file, NULL); - _exit(retval); - } else { - do { - w =3D waitpid(childpid, &retval, 0); - if (w =3D=3D -1) - goto error; - } while (!WIFEXITED(retval) && !WIFSIGNALED(retval)); - } - - if (WEXITSTATUS(retval) !=3D 0) { - set_error("Cannot spawn utility to get screenshot" TSRMLS_CC); - goto error; - } - } - - fd =3D open(file, O_RDONLY); - fsize =3D lseek(fd, 0, SEEK_END); - lseek(fd, 0, SEEK_SET); - buf =3D emalloc((fsize + 1) * sizeof(char)); - memset(buf, 0, fsize + 1); - if (read(fd, buf, fsize) < 0) { - close(fd); - unlink(file); - goto error; - } - close(fd); - - if (access(file, F_OK) =3D=3D 0) { - DPRINTF("%s: Temporary file %s deleted\n", PHPFUNC, file); - unlink(file); - } - - /* This is necessary to make the output binary safe */ - VIRT_ZVAL_STRINGL(return_value, buf, fsize); - - efree(buf); - free(tmp); - free(xml); - free(pathDup); - return; - - error: - efree(buf); - free(tmp); - free(xml); - free(pathDup); - RETURN_FALSE; -#else - set_error("Function is not supported on Windows systems" TSRMLS_CC); - RETURN_FALSE; -#endif -} - -/* - * Function name: libvirt_domain_get_screen_dimensions - * Since version: 0.4.3 - * Description: Function get screen dimensions of the VNC window - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @server [string]: server string of the host machine - * Returns: array of height and width on success, FALSE otherwise - */ -PHP_FUNCTION(libvirt_domain_get_screen_dimensions) -{ -#ifndef EXTWIN - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval =3D -1; - char *tmp =3D NULL; - char *xml =3D NULL; - char *hostname =3D NULL; - strsize_t hostname_len; - int ret; - int width; - int height; - - GET_DOMAIN_FROM_ARGS("rs", &zdomain, &hostname, &hostname_len); - - xml =3D virDomainGetXMLDesc(domain->domain, 0); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - goto error; - } - - tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - set_error("Cannot get the VNC port" TSRMLS_CC); - goto error; - } - - DPRINTF("%s: hostname =3D %s, port =3D %s\n", PHPFUNC, hostname, tmp); - ret =3D vnc_get_dimensions(hostname, tmp, &width, &height); - free(tmp); - if (ret !=3D 0) { - char error[1024] =3D { 0 }; - if (ret =3D=3D -9) - snprintf(error, sizeof(error), "Cannot connect to VNC server. = Please make sure domain is running and VNC graphics are set"); - else - snprintf(error, sizeof(error), "Cannot get screen dimensions, = error code =3D %d (%s)", ret, strerror(-ret)); - - set_error(error TSRMLS_CC); - goto error; - } - - array_init(return_value); - add_assoc_long(return_value, "width", (long)width); - add_assoc_long(return_value, "height", (long)height); - - free(tmp); - free(xml); - return; - - error: - free(tmp); - free(xml); - RETURN_FALSE; -#else - set_error("Function is not supported on Windows systems" TSRMLS_CC); - RETURN_FALSE; -#endif -} - -/* - * Function name: libvirt_domain_send_keys - * Since version: 0.4.2 - * Description: Function sends keys to the domain's VNC window - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @server [string]: server string of the host machine - * @scancode [int]: integer scancode to be sent to VNC wi= ndow - * Returns: TRUE on success, FALSE otherwise - */ -PHP_FUNCTION(libvirt_domain_send_keys) -{ -#ifndef EXTWIN - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval =3D -1; - char *tmp =3D NULL; - char *xml =3D NULL; - char *hostname =3D NULL; - strsize_t hostname_len; - char *keys =3D NULL; - strsize_t keys_len; - int ret =3D 0; - - GET_DOMAIN_FROM_ARGS("rss", &zdomain, &hostname, &hostname_len, &keys,= &keys_len); - - DPRINTF("%s: Sending %d VNC keys to %s...\n", PHPFUNC, (int)strlen((co= nst char *)keys), hostname); - - xml =3D virDomainGetXMLDesc(domain->domain, 0); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - goto error; - } - - tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - set_error("Cannot get the VNC port" TSRMLS_CC); - goto error; - } - - DPRINTF("%s: About to send string '%s' (%d keys) to %s:%s\n", PHPFUNC,= keys, (int)strlen((const char *)keys), hostname, tmp); - - ret =3D vnc_send_keys(hostname, tmp, keys); - DPRINTF("%s: Sequence sending result is %d\n", PHPFUNC, ret); - - if (!ret) { - char tmpp[64] =3D { 0 }; - snprintf(tmpp, sizeof(tmpp), "Cannot send keys, error code %d", re= t); - set_error(tmpp TSRMLS_CC); - goto error; - } - - free(tmp); - free(xml); - RETURN_TRUE; - - error: - free(tmp); - free(xml); - RETURN_FALSE; -#else - set_error("Function is not supported on Windows systems" TSRMLS_CC); - RETURN_FALSE; -#endif -} - -/* - * Function name: libvirt_domain_send_key_api - * Since version: 0.5.3 - * Description: Function sends keys to domain via libvirt API - * Arguments: @res[resource]: libvirt domain resource, e.g. from lib= virt_domaing_lookup_by_*() - * @codeset [int]: the codeset of keycodes, from virKeyco= deSet - * @holdtime [int]: the duration (in miliseconds) that th= e keys will be held - * @keycodes [array]: array of keycodes - * @flags [int]: extra flags; not used yet so callers sho= uld alway pass 0 - * Returns: TRUE for success, FALSE for failure - */ -PHP_FUNCTION(libvirt_domain_send_key_api) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - zend_long codeset; - zend_long holdtime =3D 0; - zend_long flags =3D 0; - zval *zkeycodes, *data =3D NULL; - HashTable *arr_hash =3D NULL; - HashPosition pointer; - int count, i; - uint *keycodes =3D NULL; - - GET_DOMAIN_FROM_ARGS("rlla|l", &zdomain, &codeset, &holdtime, &zkeycod= es, - &flags); - - arr_hash =3D Z_ARRVAL_P(zkeycodes); - count =3D zend_hash_num_elements(arr_hash); - - keycodes =3D (uint *) emalloc(count * sizeof(uint)); - - i =3D 0; - VIRT_FOREACH(arr_hash, pointer, data) { - if (Z_TYPE_P(data) =3D=3D IS_LONG) { - keycodes[i++] =3D (uint) Z_LVAL_P(data); - } - } VIRT_FOREACH_END(); - - if (virDomainSendKey(domain->domain, codeset, holdtime, keycodes, coun= t, - flags) !=3D 0) { - efree(keycodes); - RETURN_FALSE; - } - - efree(keycodes); - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_send_pointer_event - * Since version: 0.4.2 - * Description: Function sends keys to the domain's VNC window - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @server [string]: server string of the host machine - * @pos_x [int]: position on x-axis - * @pos_y [int]: position on y-axis - * @clicked [int]: mask of clicked buttons (0 for none, b= it 1 for button #1, bit 8 for button #8) - * @release [int]: boolean value (0 or 1) whether to rele= ase the buttons automatically once pressed - * Returns: TRUE on success, FALSE otherwise - */ -PHP_FUNCTION(libvirt_domain_send_pointer_event) -{ -#ifndef EXTWIN - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval =3D -1; - char *tmp =3D NULL; - char *xml =3D NULL; - char *hostname =3D NULL; - strsize_t hostname_len; - zend_long pos_x =3D 0; - zend_long pos_y =3D 0; - zend_long clicked =3D 0; - zend_bool release =3D 1; - int ret; - - GET_DOMAIN_FROM_ARGS("rslll|b", &zdomain, &hostname, &hostname_len, &p= os_x, &pos_y, &clicked, &release); - - xml =3D virDomainGetXMLDesc(domain->domain, 0); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - goto error; - } - - tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics/@port", = NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - set_error("Cannot get the VNC port" TSRMLS_CC); - goto error; - } - - DPRINTF("%s: x =3D %d, y =3D %d, clicked =3D %d, release =3D %d, hostn= ame =3D %s...\n", PHPFUNC, (int) pos_x, (int) pos_y, (int) clicked, release= , hostname); - ret =3D vnc_send_pointer_event(hostname, tmp, pos_x, pos_y, clicked, r= elease); - if (!ret) { - char error[1024] =3D { 0 }; - if (ret =3D=3D -9) - snprintf(error, sizeof(error), "Cannot connect to VNC server. = Please make sure domain is running and VNC graphics are set"); - else - snprintf(error, sizeof(error), "Cannot send pointer event, err= or code =3D %d (%s)", ret, strerror(-ret)); - - set_error(error TSRMLS_CC); - goto error; - } - - free(tmp); - free(xml); - RETURN_TRUE; - - error: - free(tmp); - free(xml); - RETURN_FALSE; -#else - set_error("Function is not supported on Windows systems" TSRMLS_CC); - RETURN_FALSE; -#endif -} - -/* - * Function name: libvirt_domain_get_uuid - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's UUID in binary fo= rmat - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: domain UUID in binary format - */ -PHP_FUNCTION(libvirt_domain_get_uuid) -{ - - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *uuid; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - uuid =3D (char *)emalloc(VIR_UUID_BUFLEN); - retval =3D virDomainGetUUID(domain->domain, (unsigned char *)uuid); - DPRINTF("%s: virDomainGetUUID(%p, %p) returned %d\n", PHPFUNC, domain-= >domain, uuid, retval); - if (retval !=3D 0) - RETURN_FALSE; - - VIRT_RETVAL_STRING(uuid); - efree(uuid); -} - -/* - * Function name: libvirt_domain_get_id - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's ID, applicable to= running guests only - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: running domain ID or -1 if not running - */ -PHP_FUNCTION(libvirt_domain_get_id) -{ - - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainGetID(domain->domain); - DPRINTF("%s: virDomainGetID(%p) returned %d\n", PHPFUNC, domain->domai= n, retval); - - RETURN_LONG(retval); -} - -/* - * Function name: libvirt_domain_get_next_dev_ids - * Since version: 0.4.2 - * Description: This functions can be used to get the next free slot i= f you intend to add a new device identified by slot to the domain, e.g. NIC= device - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: next free slot number for the domain - */ -PHP_FUNCTION(libvirt_domain_get_next_dev_ids) -{ - long dom, bus, slot, func; - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - DPRINTF("%s: Getting the next dev ids for domain %p\n", PHPFUNC, domai= n->domain); - - dom =3D get_next_free_numeric_value(domain->domain, "//@domain"); - bus =3D get_next_free_numeric_value(domain->domain, "//@bus"); - slot =3D get_next_free_numeric_value(domain->domain, "//@slot"); - func =3D get_next_free_numeric_value(domain->domain, "//@func"); - - array_init(return_value); - add_assoc_long(return_value, "next_domain", dom); - add_assoc_long(return_value, "next_bus", bus); - add_assoc_long(return_value, "next_slot", slot); - add_assoc_long(return_value, "next_func", func); -} - -void parse_array(zval *arr, tVMDisk *disk, tVMNetwork *network) -{ - HashTable *arr_hash; - // int array_count; - zval *data; - php_libvirt_hash_key_info key; - HashPosition pointer; - unsigned long index; - - arr_hash =3D Z_ARRVAL_P(arr); - //array_count =3D zend_hash_num_elements(arr_hash); - - if (disk !=3D NULL) - memset(disk, 0, sizeof(tVMDisk)); - if (network !=3D NULL) - memset(network, 0, sizeof(tVMNetwork)); - - VIRT_FOREACH(arr_hash, pointer, data) { - if ((Z_TYPE_P(data) =3D=3D IS_STRING) || (Z_TYPE_P(data) =3D=3D IS= _LONG)) { - VIRT_HASH_CURRENT_KEY_INFO(arr_hash, pointer, index, key); - if (key.type =3D=3D HASH_KEY_IS_STRING) { - if (disk !=3D NULL) { - if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(key.na= me, "path") =3D=3D 0) - disk->path =3D strdup(Z_STRVAL_P(data)); - else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(k= ey.name, "driver") =3D=3D 0) - disk->driver =3D strdup(Z_STRVAL_P(data)); - else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(k= ey.name, "bus") =3D=3D 0) - disk->bus =3D strdup(Z_STRVAL_P(data)); - else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(k= ey.name, "dev") =3D=3D 0) - disk->dev =3D strdup(Z_STRVAL_P(data)); - else if (strcmp(key.name, "size") =3D=3D 0) { - if (Z_TYPE_P(data) =3D=3D IS_LONG) { - disk->size =3D Z_LVAL_P(data); - } else { - disk->size =3D size_def_to_mbytes(Z_STRVAL_P(d= ata)); - } - } else if ((Z_TYPE_P(data) =3D=3D IS_LONG) && strcmp(k= ey.name, "flags") =3D=3D 0) - disk->flags =3D Z_LVAL_P(data); - } else { - if (network !=3D NULL) { - if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strcmp(ke= y.name, "mac") =3D=3D 0) - network->mac =3D strdup(Z_STRVAL_P(data)); - else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strc= mp(key.name, "network") =3D=3D 0) - network->network =3D strdup(Z_STRVAL_P(data)); - else if ((Z_TYPE_P(data) =3D=3D IS_STRING) && strc= mp(key.name, "model") =3D=3D 0) - network->model =3D strdup(Z_STRVAL_P(data)); - } - } - } - } - } VIRT_FOREACH_END(); -} - -/* - * Function name: libvirt_domain_new - * Since version: 0.4.5 - * Description: Function is used to install a new virtual machine to t= he machine - * Arguments: @conn [resource]: libvirt connection resource - * @name [string]: name of the new domain - * @arch [string]: optional architecture string, can be N= ULL to get default (or false) - * @memMB [int]: number of megabytes of RAM to be allocat= ed for domain - * @maxmemMB [int]: maximum number of megabytes of RAM to= be allocated for domain - * @vcpus [int]: number of VCPUs to be allocated to domain - * @iso_image [string]: installation ISO image for domain - * @disks [array]: array of disk devices for domain, cons= ist of keys as 'path' (storage location), 'driver' (image type, e.g. 'raw' = or 'qcow2'), 'bus' (e.g. 'ide', 'scsi'), 'dev' (device to be presented to t= he guest - e.g. 'hda'), 'size' (with 'M' or 'G' suffixes, like '10G' for 10= gigabytes image etc.) and 'flags' (VIR_DOMAIN_DISK_FILE or VIR_DOMAIN_DISK= _BLOCK, optionally VIR_DOMAIN_DISK_ACCESS_ALL to allow access to the disk f= or all users on the host system) - * @networks [array]: array of network devices for domain= , consists of keys as 'mac' (for MAC address), 'network' (for network name)= and optional 'model' for model of NIC device - * @flags [int]: bit array of flags - * Returns: a new domain resource - */ -PHP_FUNCTION(libvirt_domain_new) -{ - php_libvirt_connection *conn =3D NULL; - php_libvirt_domain *res_domain =3D NULL; - virDomainPtr domain2 =3D NULL; - virDomainPtr domain =3D NULL; - zval *zconn; - char *arch =3D NULL; - strsize_t arch_len; - char *tmp; - char *name; - strsize_t name_len =3D 0; - // char *emulator; - char *iso_image =3D NULL; - strsize_t iso_image_len; - zend_long vcpus =3D -1; - zend_long memMB =3D -1; - zval *disks, *networks; - tVMDisk *vmDisks =3D NULL; - tVMNetwork *vmNetworks =3D NULL; - zend_long maxmemMB =3D -1; - HashTable *arr_hash; - int numDisks, numNets, i; - zval *data; - HashPosition pointer; - char vncl[2048] =3D { 0 }; - char tmpname[1024] =3D { 0 }; - char *xml =3D NULL; - int retval =3D 0; - char *uuid =3D NULL; - zend_long flags =3D 0; - int fd =3D -1; - - GET_CONNECTION_FROM_ARGS("rsslllsaa|l", &zconn, &name, &name_len, &arc= h, &arch_len, &memMB, &maxmemMB, &vcpus, &iso_image, &iso_image_len, &disks= , &networks, &flags); - - if (iso_image =3D=3D NULL) { - DPRINTF("%s: Iso image is not defined\n", PHPFUNC); - RETURN_FALSE; - } - - if ((arch =3D=3D NULL) || (arch_len =3D=3D 0)) - arch =3D NULL; - - //DPRINTF("%s: name: %s, arch: %s, memMB: %d, maxmemMB: %d, vcpus: %d,= iso_image: %s\n", PHPFUNC, name, arch, memMB, maxmemMB, vcpus, iso_image); - if (memMB =3D=3D 0) - memMB =3D maxmemMB; - - /* Parse all disks from array */ - arr_hash =3D Z_ARRVAL_P(disks); - numDisks =3D zend_hash_num_elements(arr_hash); - vmDisks =3D (tVMDisk *)malloc(numDisks * sizeof(tVMDisk)); - memset(vmDisks, 0, numDisks * sizeof(tVMDisk)); - i =3D 0; - VIRT_FOREACH(arr_hash, pointer, data) { - if (Z_TYPE_P(data) =3D=3D IS_ARRAY) { - tVMDisk disk; - parse_array(data, &disk, NULL); - if (disk.path !=3D NULL) { - //DPRINTF("Disk =3D> path =3D '%s', driver =3D '%s', bus = =3D '%s', dev =3D '%s', size =3D %ld MB, flags =3D %d\n", - // disk.path, disk.driver, disk.bus, disk.dev, disk.size,= disk.flags); - vmDisks[i++] =3D disk; - } - } - } VIRT_FOREACH_END(); - numDisks =3D i; - - /* Parse all networks from array */ - arr_hash =3D Z_ARRVAL_P(networks); - numNets =3D zend_hash_num_elements(arr_hash); - vmNetworks =3D (tVMNetwork *)malloc(numNets * sizeof(tVMNetwork)); - memset(vmNetworks, 0, numNets * sizeof(tVMNetwork)); - i =3D 0; - VIRT_FOREACH(arr_hash, pointer, data) { - if (Z_TYPE_P(data) =3D=3D IS_ARRAY) { - tVMNetwork network; - parse_array(data, NULL, &network); - if (network.mac !=3D NULL) { - //DPRINTF("Network =3D> mac =3D '%s', network =3D '%s', mo= del =3D '%s'\n", network.mac, network.network, network.model); - vmNetworks[i++] =3D network; - } - } - } VIRT_FOREACH_END(); - numNets =3D i; - - snprintf(tmpname, sizeof(tmpname), "%s-install", name); - DPRINTF("%s: Name is '%s', memMB is %d, maxmemMB is %d\n", PHPFUNC, tm= pname, (int) memMB, (int) maxmemMB); - tmp =3D installation_get_xml(1, - conn->conn, tmpname, memMB, maxmemMB, NULL = /* arch */, NULL, vcpus, iso_image, - vmDisks, numDisks, vmNetworks, numNets, - flags TSRMLS_CC); - if (tmp =3D=3D NULL) { - DPRINTF("%s: Cannot get installation XML\n", PHPFUNC); - set_error("Cannot get installation XML" TSRMLS_CC); - RETURN_FALSE; - } - - domain =3D virDomainCreateXML(conn->conn, tmp, 0); - if (domain =3D=3D NULL) { - set_error_if_unset("Cannot create installation domain from the XML= description" TSRMLS_CC); - DPRINTF("%s: Cannot create installation domain from the XML descri= ption (%s): %s\n", PHPFUNC, LIBVIRT_G(last_error), tmp); - RETURN_FALSE; - } - - xml =3D virDomainGetXMLDesc(domain, 0); - if (!xml) { - DPRINTF("%s: Cannot get the XML description\n", PHPFUNC); - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - tmp =3D get_string_from_xpath(xml, "//domain/devices/graphics[@type=3D= 'vnc']/@port", NULL, &retval); - if (retval < 0) { - DPRINTF("%s: Cannot get port from XML description\n", PHPFUNC); - set_error_if_unset("Cannot get port from XML description" TSRMLS_C= C); - RETURN_FALSE; - } - - snprintf(vncl, sizeof(vncl), "%s:%s", virConnectGetHostname(conn->conn= ), tmp); - DPRINTF("%s: Trying to connect to '%s'\n", PHPFUNC, vncl); - -#ifndef EXTWIN - if ((fd =3D connect_socket(virConnectGetHostname(conn->conn), tmp, 0, = 0, flags & DOMAIN_FLAG_TEST_LOCAL_VNC)) < 0) { - DPRINTF("%s: Cannot connect to '%s'\n", PHPFUNC, vncl); - snprintf(vncl, sizeof(vncl), "Connection failed, port %s is most l= ikely forbidden on firewall (iptables) on the host (%s)" - " or the emulator VNC server is bound to localhost addres= s only.", - tmp, virConnectGetHostname(conn->conn)); - } else { - close(fd); - DPRINTF("%s: Connection to '%s' successfull (%s local connection)\= n", PHPFUNC, vncl, - (flags & DOMAIN_FLAG_TEST_LOCAL_VNC) ? "using" : "not usin= g"); - } -#endif - - set_vnc_location(vncl TSRMLS_CC); - - tmp =3D installation_get_xml(2, - conn->conn, name, memMB, maxmemMB, NULL /* = arch */, NULL, vcpus, iso_image, - vmDisks, numDisks, vmNetworks, numNets, - flags TSRMLS_CC); - if (tmp =3D=3D NULL) { - DPRINTF("%s: Cannot get installation XML, step 2\n", PHPFUNC); - set_error("Cannot get installation XML, step 2" TSRMLS_CC); - virDomainFree(domain); - RETURN_FALSE; - } - - domain2 =3D virDomainDefineXML(conn->conn, tmp); - if (domain2 =3D=3D NULL) { - set_error_if_unset("Cannot define domain from the XML description"= TSRMLS_CC); - DPRINTF("%s: Cannot define domain from the XML description (name = =3D '%s', uuid =3D '%s', error =3D '%s')\n", PHPFUNC, name, uuid, LIBVIRT_G= (last_error)); - RETURN_FALSE; - } - virDomainFree(domain2); - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D domain; - res_domain->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_new_get_vnc - * Since version: 0.4.5 - * Description: Function is used to get the VNC server location for th= e newly created domain (newly started installation) - * Arguments: None - * Returns: a VNC server for a newly created domain resource (if a= ny) - */ -PHP_FUNCTION(libvirt_domain_new_get_vnc) -{ - if (LIBVIRT_G(vnc_location)) - VIRT_RETURN_STRING(LIBVIRT_G(vnc_location)); - - RETURN_NULL(); -} - -/* - * Function name: libvirt_domain_get_xml_desc - * Since version: 0.4.1(-1), changed 0.4.2 - * Description: Function is used to get the domain's XML description - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL - * Returns: domain XML description string or result of xPath expre= ssion - */ -PHP_FUNCTION(libvirt_domain_get_xml_desc) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *tmp =3D NULL; - char *xml; - char *xpath =3D NULL; - strsize_t xpath_len; - zend_long flags =3D 0; - int retval =3D -1; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &xpath, &xpath_len, &flags); - if (xpath_len < 1) - xpath =3D NULL; - - DPRINTF("%s: Getting the XML for domain %p (xPath =3D %s)\n", PHPFUNC,= domain->domain, xpath); - - xml =3D virDomainGetXMLDesc(domain->domain, flags); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - VIRT_RETVAL_STRING(xml); - } else { - VIRT_RETVAL_STRING(tmp); - } - - free(tmp); - free(xml); -} - -/* - * Function name: libvirt_domain_get_disk_devices - * Since version: 0.4.4 - * Description: Function is used to get disk devices for the domain - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: list of domain disk devices - */ -PHP_FUNCTION(libvirt_domain_get_disk_devices) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *xml; - int retval =3D -1; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - DPRINTF("%s: Getting disk device list for domain %p\n", PHPFUNC, domai= n->domain); - - xml =3D virDomainGetXMLDesc(domain->domain, 0); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - array_init(return_value); - - free(get_string_from_xpath(xml, "//domain/devices/disk/target/@dev", &= return_value, &retval)); - free(xml); - - if (retval < 0) - add_assoc_long(return_value, "error_code", (long)retval); - else - add_assoc_long(return_value, "num", (long)retval); -} - -/* - * Function name: libvirt_domain_get_interface_devices - * Since version: 0.4.4 - * Description: Function is used to get network interface devices for = the domain - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: list of domain interface devices - */ -PHP_FUNCTION(libvirt_domain_get_interface_devices) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *xml; - int retval =3D -1; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - DPRINTF("%s: Getting interface device list for domain %p\n", PHPFUNC, = domain->domain); - - xml =3D virDomainGetXMLDesc(domain->domain, 0); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - array_init(return_value); - - free(get_string_from_xpath(xml, "//domain/devices/interface/target/@de= v", &return_value, &retval)); - free(xml); - - if (retval < 0) - add_assoc_long(return_value, "error_code", (long)retval); - else - add_assoc_long(return_value, "num", (long)retval); -} - -/* - * Function name: libvirt_domain_change_vcpus - * Since version: 0.4.2 - * Description: Function is used to change the VCPU count for the doma= in - * Arguments: @res [resource]: libvirt domain resource - * @numCpus [int]: number of VCPUs to be set for the guest - * @flags [int]: flags for virDomainSetVcpusFlags (availa= ble at http://libvirt.org/html/libvirt-libvirt.html#virDomainVcpuFlags) - * Returns: true on success, false on error - */ -PHP_FUNCTION(libvirt_domain_change_vcpus) -{ - zend_long numCpus, flags =3D 0; - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - - GET_DOMAIN_FROM_ARGS("rl|l", &zdomain, &numCpus, &flags); - - if (virDomainSetVcpusFlags(domain->domain, numCpus, flags) =3D=3D 0) { - RETURN_TRUE; - } else { - RETURN_FALSE; - } -} - -/* - * Function name: libvirt_domain_change_memory - * Since version: 0.4.2 - * Description: Function is used to change the domain memory allocation - * Arguments: @res [resource]: libvirt domain resource - * @allocMem [int]: number of MiBs to be set as immediate= memory value - * @allocMax [int]: number of MiBs to be set as the maxim= um allocation - * @flags [int]: flags - * Returns: new domain resource - */ -PHP_FUNCTION(libvirt_domain_change_memory) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *tmpA =3D NULL; - char *tmp1 =3D NULL; - char *tmp2 =3D NULL; - char *xml; - char *new_xml =3D NULL; - int new_len; - char *newXml =3D NULL; - zend_long xflags =3D 0; - zend_long allocMem =3D 0; - zend_long allocMax =3D 0; - // int pos =3D -1; - int len =3D 0; - php_libvirt_domain *res_domain =3D NULL; - php_libvirt_connection *conn =3D NULL; - virDomainPtr dom =3D NULL; - - GET_DOMAIN_FROM_ARGS("rll|l", &zdomain, &allocMem, &allocMax, &xflags); - - DPRINTF("%s: Changing domain memory count to %d MiB current/%d MiB max= , domain =3D %p\n", - PHPFUNC, (int)allocMem, (int)allocMax, domain->domain); - - allocMem *=3D 1024; - allocMax *=3D 1024; - - if (allocMem > allocMax) - allocMem =3D allocMax; - - xml =3D virDomainGetXMLDesc(domain->domain, xflags); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - snprintf(newXml, sizeof(newXml), " %d\n %d\n", allocMax, allocMem); - tmpA =3D strstr(xml, ""); - tmp1 =3D strstr(xml, "") + strlen(""); - // pos =3D strlen(xml) - strlen(tmp1); - len =3D strlen(xml) - strlen(tmpA); - - tmp2 =3D (char *)emalloc((len + 1) * sizeof(char)); - memset(tmp2, 0, len + 1); - memcpy(tmp2, xml, len); - - new_len =3D strlen(tmp1) + strlen(tmp2) + strlen(newXml) + 2; - new_xml =3D (char *)emalloc(new_len * sizeof(char)); - snprintf(new_xml, new_len, "%s\n%s%s", tmp2, newXml, tmp1); - - conn =3D domain->conn; - - dom =3D virDomainDefineXML(conn->conn, new_xml); - if (dom =3D=3D NULL) { - free(xml); - efree(new_xml); - RETURN_FALSE; - } - free(xml); - efree(new_xml); - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D dom; - res_domain->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_change_boot_devices - * Since version: 0.4.2 - * Description: Function is used to change the domain boot devices - * Arguments: @res [resource]: libvirt domain resource - * @first [string]: first boot device to be set - * @second [string]: second boot device to be set - * @flags [int]: flags - * Returns: new domain resource - */ -PHP_FUNCTION(libvirt_domain_change_boot_devices) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *tmpA =3D NULL; - char *tmp1 =3D NULL; - char *tmp2 =3D NULL; - char *xml; - char *new_xml =3D NULL; - int new_len; - char newXml[4096] =3D { 0 }; - zend_long xflags =3D 0; - char *first =3D NULL; - strsize_t first_len; - char *second =3D NULL; - strsize_t second_len; - // int pos =3D -1; - int len =3D 0; - php_libvirt_domain *res_domain =3D NULL; - php_libvirt_connection *conn =3D NULL; - virDomainPtr dom =3D NULL; - - GET_DOMAIN_FROM_ARGS("rss|l", &zdomain, &first, &first_len, &second, &= second_len, &xflags); - - xml =3D virDomainGetXMLDesc(domain->domain, xflags); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - DPRINTF("%s: Changing domain boot order, domain =3D %p\n", PHPFUNC, do= main->domain); - - if (!second || (strcmp(second, "-") =3D=3D 0)) - snprintf(newXml, sizeof(newXml), " \n", first= ); - else - snprintf(newXml, sizeof(newXml), " \n \n", first, second); - - tmpA =3D strstr(xml, "") + strlen(""); - tmp1 =3D strstr(xml, ""); - // pos =3D strlen(xml) - strlen(tmp1); - len =3D strlen(xml) - strlen(tmpA); - - tmp2 =3D (char *)emalloc((len + 1) * sizeof(char)); - memset(tmp2, 0, len + 1); - memcpy(tmp2, xml, len); - - new_len =3D strlen(tmp1) + strlen(tmp2) + strlen(newXml) + 2; - new_xml =3D (char *)emalloc(new_len * sizeof(char)); - snprintf(new_xml, new_len, "%s\n%s%s", tmp2, newXml, tmp1); - - conn =3D domain->conn; - - dom =3D virDomainDefineXML(conn->conn, new_xml); - if (dom =3D=3D NULL) { - DPRINTF("%s: Function failed, restoring original XML\n", PHPFUNC); - free(xml); - efree(newXml); - RETURN_FALSE; - } - free(xml); - efree(newXml); - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D dom; - res_domain->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_disk_add - * Since version: 0.4.2 - * Description: Function is used to add the disk to the virtual machin= e using set of API functions to make it as simple as possible for the user - * Arguments: @res [resource]: libvirt domain resource - * @img [string]: string for the image file on the host s= ystem - * @dev [string]: string for the device to be presented t= o the guest (e.g. hda) - * @typ [string]: bus type for the device in the guest, u= sually 'ide' or 'scsi' - * @driver [string]: driver type to be specified, like 'r= aw' or 'qcow2' - * @flags [int]: flags for getting the XML description - * Returns: new domain resource - */ -PHP_FUNCTION(libvirt_domain_disk_add) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *xml; - char *img =3D NULL; - strsize_t img_len; - char *dev =3D NULL; - strsize_t dev_len; - char *driver =3D NULL; - strsize_t driver_len; - char *typ =3D NULL; - strsize_t typ_len; - char *newXml =3D NULL; - zend_long xflags =3D 0; - int retval =3D -1; - char *xpath =3D NULL; - char *tmp =3D NULL; - - GET_DOMAIN_FROM_ARGS("rssss|l", &zdomain, &img, &img_len, &dev, &dev_l= en, &typ, &typ_len, &driver, &driver_len, &xflags); - - DPRINTF("%s: Domain %p, device =3D %s, image =3D %s, type =3D %s, driv= er =3D %s\n", PHPFUNC, - domain->domain, dev, img, typ, driver); - - xml =3D virDomainGetXMLDesc(domain->domain, xflags); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - goto error; - } - - if (asprintf(&xpath, "//domain/devices/disk/source[@file=3D\"%s\"]/./@= file", img) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (tmp !=3D NULL) { - free(tmp); - if (asprintf(&tmp, "Domain already has image %s connected",= img) < 0) - set_error("Out of memory" TSRMLS_CC); - else - set_error(tmp TSRMLS_CC); - goto error; - } - - free(xpath); - if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/./@dev= ", dev) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, newXml, NULL, &retval); - if (tmp !=3D NULL) { - free(tmp); - if (asprintf(&tmp, "Domain already has device %s connected"= , dev) < 0) - set_error("Out of memory" TSRMLS_CC); - else - set_error(tmp TSRMLS_CC); - goto error; - } - - if (asprintf(&newXml, - " \n" - " \n" - " \n" - " \n" - " ", driver, img, dev, typ) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - - if (virDomainAttachDeviceFlags(domain->domain, - newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { - set_error("Unable to attach disk" TSRMLS_CC); - goto error; - } - - free(tmp); - free(xpath); - free(xml); - RETURN_TRUE; - - error: - free(tmp); - free(xpath); - free(xml); - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_disk_remove - * Since version: 0.4.2 - * Description: Function is used to remove the disk from the virtual m= achine using set of API functions to make it as simple as possible - * Arguments: @res [resource]: libvirt domain resource - * @dev [string]: string for the device to be removed fro= m the guest (e.g. 'hdb') - * @flags [int]: flags for getting the XML description - * Returns: new domain resource - */ -PHP_FUNCTION(libvirt_domain_disk_remove) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *xml; - char *dev =3D NULL; - strsize_t dev_len; - char *newXml =3D NULL; - zend_long xflags =3D 0; - int retval =3D -1; - char *xpath =3D NULL; - char *tmp =3D NULL; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &dev, &dev_len, &xflags); - - DPRINTF("%s: Trying to remove %s from domain %p\n", PHPFUNC, dev, doma= in->domain); - - xml =3D virDomainGetXMLDesc(domain->domain, xflags); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/./@dev= ", dev) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (!tmp) { - if (asprintf(&tmp, "Device %s is not connected to the guest= ", dev) < 0) - set_error("Out of memory" TSRMLS_CC); - else - set_error(tmp TSRMLS_CC); - goto error; - } - - if (asprintf(&newXml, - " \n" - " \n" - " ", dev) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - - if (virDomainDetachDeviceFlags(domain->domain, - newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { - set_error("Unable to attach disk" TSRMLS_CC); - goto error; - } - - free(tmp); - free(xpath); - free(xml); - RETURN_TRUE; - - error: - free(tmp); - free(xpath); - free(xml); - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_nic_add - * Since version: 0.4.2 - * Description: Function is used to add the NIC card to the virtual ma= chine using set of API functions to make it as simple as possible for the u= ser - * Arguments: @res [resource]: libvirt domain resource - * @mac [string]: MAC string interpretation to be used fo= r the NIC device - * @network [string]: network name where to connect this = NIC - * @model [string]: string of the NIC model - * @flags [int]: flags for getting the XML description - * Returns: new domain resource - */ -PHP_FUNCTION(libvirt_domain_nic_add) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *xml; - char *mac =3D NULL; - strsize_t mac_len; - char *net =3D NULL; - strsize_t net_len; - char *model =3D NULL; - strsize_t model_len; - char *newXml =3D NULL; - zend_long xflags =3D 0; - int retval =3D -1; - char *xpath =3D NULL; - char *tmp =3D NULL; - - DPRINTF("%s: Entering\n", PHPFUNC); - - GET_DOMAIN_FROM_ARGS("rsss|l", &zdomain, &mac, &mac_len, &net, &net_le= n, &model, &model_len, &xflags); - if (model_len < 1) - model =3D NULL; - - DPRINTF("%s: domain =3D %p, mac =3D %s, net =3D %s, model =3D %s\n", P= HPFUNC, domain->domain, mac, net, model); - - xml =3D virDomainGetXMLDesc(domain->domain, xflags); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/./@mac", mac) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (tmp) { - free(tmp); - if (asprintf(&tmp, "Domain already has NIC device with MAC address= %s connected", mac) < 0) - set_error("Out of memory" TSRMLS_CC); - else - set_error(tmp TSRMLS_CC); - goto error; - } - - if (model) { - if (asprintf(&newXml, - " \n" - " \n" - " \n" - " \n" - " ", mac, net, model) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - } else { - if (asprintf(&newXml, - " \n" - " \n" - " \n" - " ", mac, net) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - } - - if (virDomainAttachDeviceFlags(domain->domain, - newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { - set_error("Unable to attach interface" TSRMLS_CC); - goto error; - } - - free(tmp); - free(xpath); - free(xml); - RETURN_TRUE; - - error: - free(tmp); - free(xpath); - free(xml); - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_nic_remove - * Since version: 0.4.2 - * Description: Function is used to remove the NIC from the virtual ma= chine using set of API functions to make it as simple as possible - * Arguments: @res [resource]: libvirt domain resource - * @dev [string]: string representation of the IP address= to be removed (e.g. 54:52:00:xx:yy:zz) - * @flags [int]: optional flags for getting the XML descr= iption - * Returns: new domain resource - */ -PHP_FUNCTION(libvirt_domain_nic_remove) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *xml; - char *mac =3D NULL; - strsize_t mac_len; - char *newXml =3D NULL; - zend_long xflags =3D 0; - int retval =3D -1; - char *xpath =3D NULL; - char *tmp =3D NULL; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &mac, &mac_len, &xflags); - - DPRINTF("%s: Trying to remove NIC device with MAC address %s from doma= in %p\n", PHPFUNC, mac, domain->domain); - - xml =3D virDomainGetXMLDesc(domain->domain, xflags); - if (!xml) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/./@mac", mac) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (!tmp) { - free(tmp); - if (asprintf(&tmp, "Domain has no such interface with mac %s", mac= ) < 0) - set_error("Out of memory" TSRMLS_CC); - else - set_error(tmp TSRMLS_CC); - goto error; - } - - if (asprintf(&newXml, - " \n" - " \n" - " ", mac) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - - if (virDomainDetachDeviceFlags(domain->domain, - newXml, VIR_DOMAIN_AFFECT_CONFIG) < 0) { - set_error("Unable to detach interface" TSRMLS_CC); - goto error; - } - - free(tmp); - free(xpath); - free(xml); - RETURN_TRUE; - - error: - free(tmp); - free(xpath); - free(xml); - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_attach_device - * Since version: 0.5.3 - * Description: Function is used to attach a virtual device to a domai= n. - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @xml [string]: XML description of one device. - * @flags [int]: optional flags to control how the device= is attached. Defaults to VIR_DOMAIN_AFFECT_LIVE - * Returns: TRUE for success, FALSE on error. - */ -PHP_FUNCTION(libvirt_domain_attach_device) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain =3D NULL; - char *xml =3D NULL; - strsize_t xml_len =3D 0; - zend_long flags =3D VIR_DOMAIN_AFFECT_LIVE; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &xml, &xml_len, &flags); - - if (virDomainAttachDeviceFlags(domain->domain, xml, flags) < 0) - RETURN_FALSE; - - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_detach_device - * Since version: 0.5.3 - * Description: Function is used to detach a virtual device from a dom= ain. - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @xml [string]: XML description of one device. - * @flags [int]: optional flags to control how the device= is attached. Defaults to VIR_DOMAIN_AFFECT_LIVE - * Returns: TRUE for success, FALSE on error. - */ -PHP_FUNCTION(libvirt_domain_detach_device) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain =3D NULL; - char *xml =3D NULL; - strsize_t xml_len =3D 0; - zend_long flags =3D VIR_DOMAIN_AFFECT_LIVE; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &xml, &xml_len, &flags); - - if (virDomainDetachDeviceFlags(domain->domain, xml, flags) < 0) - RETURN_FALSE; - - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_get_info - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's information - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: domain information array - */ -PHP_FUNCTION(libvirt_domain_get_info) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - virDomainInfo domainInfo; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainGetInfo(domain->domain, &domainInfo); - DPRINTF("%s: virDomainGetInfo(%p) returned %d\n", PHPFUNC, domain->dom= ain, retval); - if (retval !=3D 0) - RETURN_FALSE; - - array_init(return_value); - add_assoc_long(return_value, "maxMem", domainInfo.maxMem); - add_assoc_long(return_value, "memory", domainInfo.memory); - add_assoc_long(return_value, "state", (long)domainInfo.state); - add_assoc_long(return_value, "nrVirtCpu", domainInfo.nrVirtCpu); - add_assoc_double(return_value, "cpuUsed", (double)((double)domainInfo.= cpuTime/1000000000.0)); -} - -/* - * Function name: libvirt_domain_create - * Since version: 0.4.1(-1) - * Description: Function is used to create the domain identified by it= 's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: result of domain creation (startup) - */ -PHP_FUNCTION(libvirt_domain_create) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainCreate(domain->domain); - DPRINTF("%s: virDomainCreate(%p) returned %d\n", PHPFUNC, domain->doma= in, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_destroy - * Since version: 0.4.1(-1) - * Description: Function is used to destroy the domain identified by i= t's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: result of domain destroy - */ -PHP_FUNCTION(libvirt_domain_destroy) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainDestroy(domain->domain); - DPRINTF("%s: virDomainDestroy(%p) returned %d\n", PHPFUNC, domain->dom= ain, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_resume - * Since version: 0.4.1(-1) - * Description: Function is used to resume the domain identified by it= 's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: result of domain resume - */ -PHP_FUNCTION(libvirt_domain_resume) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainResume(domain->domain); - DPRINTF("%s: virDomainResume(%p) returned %d\n", PHPFUNC, domain->doma= in, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_core_dump - * Since version: 0.4.1(-2) - * Description: Function is used to dump core of the domain identified= by it's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @to [string]: to - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_core_dump) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - strsize_t to_len; - char *to; - - GET_DOMAIN_FROM_ARGS("rs", &zdomain, &to, &to_len); - - retval =3D virDomainCoreDump(domain->domain, to, 0); - DPRINTF("%s: virDomainCoreDump(%p, %s, 0) returned %d\n", PHPFUNC, dom= ain->domain, to, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_shutdown - * Since version: 0.4.1(-1) - * Description: Function is used to shutdown the domain identified by = it's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_shutdown) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainShutdown(domain->domain); - DPRINTF("%s: virDomainShutdown(%p) returned %d\n", PHPFUNC, domain->do= main, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_managedsave - * Since version: 0.4.1(-1) - * Description: Function is used to managed save the domain (domain wa= s unloaded from memory and it state saved to disk) identified by it's resou= rce - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_managedsave) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - retval =3D virDomainManagedSave(domain->domain, 0); - DPRINTF("%s: virDomainManagedSave(%p) returned %d\n", PHPFUNC, domain-= >domain, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_suspend - * Since version: 0.4.1(-1) - * Description: Function is used to suspend the domain identified by i= t's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_suspend) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainSuspend(domain->domain); - DPRINTF("%s: virDomainSuspend(%p) returned %d\n", PHPFUNC, domain->dom= ain, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_undefine - * Since version: 0.4.1(-1) - * Description: Function is used to undefine the domain identified by = it's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_undefine) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainUndefine(domain->domain); - DPRINTF("%s: virDomainUndefine(%p) returned %d\n", PHPFUNC, domain->do= main, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_reboot - * Since version: 0.4.1(-1) - * Description: Function is used to reboot the domain identified by it= 's resource - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @flags [int]: optional flags - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_reboot) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - retval =3D virDomainReboot(domain->domain, flags); - DPRINTF("%s: virDomainReboot(%p) returned %d\n", PHPFUNC, domain->doma= in, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_define_xml - * Since version: 0.4.1(-1) - * Description: Function is used to define the domain from XML string - * Arguments: @conn [resource]: libvirt connection resource - * @xml [string]: XML string to define guest from - * Returns: newly defined domain resource - */ -PHP_FUNCTION(libvirt_domain_define_xml) -{ - php_libvirt_domain *res_domain =3D NULL; - php_libvirt_connection *conn =3D NULL; - zval *zconn; - virDomainPtr domain =3D NULL; - char *xml; - strsize_t xml_len; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &xml, &xml_len); - - domain =3D virDomainDefineXML(conn->conn, xml); - if (domain =3D=3D NULL) - RETURN_FALSE; - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D domain; - res_domain->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_create_xml - * Since version: 0.4.1(-1) - * Description: Function is used to create the domain identified by it= 's resource - * Arguments: @conn [resource]: libvirt connection resource - * @xml [string]: XML string to create guest from - * Returns: newly started/created domain resource - */ -PHP_FUNCTION(libvirt_domain_create_xml) -{ - php_libvirt_domain *res_domain =3D NULL; - php_libvirt_connection *conn =3D NULL; - zval *zconn; - virDomainPtr domain =3D NULL; - char *xml; - strsize_t xml_len; - zend_long flags =3D 0; - - GET_CONNECTION_FROM_ARGS("rs|l", &zconn, &xml, &xml_len, &flags); - - domain =3D virDomainCreateXML(conn->conn, xml, flags); - DPRINTF("%s: virDomainCreateXML(%p, , 0) returned %p\n", PHPFUNC,= conn->conn, domain); - if (domain =3D=3D NULL) - RETURN_FALSE; - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D domain; - res_domain->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_domain->d= omain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_xml_from_native - * Since version: 0.5.3 - * Description: Function is used to convert native configuration data = to libvirt domain XML - * Arguments: @conn [resource]: libvirt connection resource - * @format [string]: configuration format converting from - * @config_data [string]: content of the native config fi= le - * Returns: libvirt domain XML, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_xml_from_native) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - char *config_data =3D NULL; - char *format =3D NULL; - char *xml =3D NULL; - strsize_t config_data_len; - strsize_t format_len; - unsigned int flags =3D 0; - - GET_CONNECTION_FROM_ARGS("rss", &zconn, &format, &format_len, &config_= data, &config_data_len); - - xml =3D virConnectDomainXMLFromNative(conn->conn, format, config_data,= flags); - - if (xml =3D=3D NULL) { - set_error_if_unset("Cannot convert native format to XML" TSRMLS_CC= ); - RETURN_FALSE; - } - - VIRT_RETVAL_STRING(xml); - free(xml); -} - -/* - * Function name: libvirt_domain_xml_to_native - * Since version: 0.5.3 - * Description: Function is used to convert libvirt domain XML to nati= ve configuration - * Arguments: @conn [resource]: libvirt connection resource - * @format [string]: configuration format converting to - * @xml_data [string]: content of the libvirt domain xml = file - * Returns: contents of the native data file, FALSE on error -*/ -PHP_FUNCTION(libvirt_domain_xml_to_native) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - char *xml_data =3D NULL; - char *format =3D NULL; - char *config_data =3D NULL; - strsize_t xml_data_len; - strsize_t format_len; - unsigned int flags =3D 0; - - GET_CONNECTION_FROM_ARGS("rss", &zconn, &format, &format_len, &xml_dat= a, &xml_data_len); - - config_data =3D virConnectDomainXMLToNative(conn->conn, format, xml_da= ta, flags); - - if (config_data =3D=3D NULL) { - set_error_if_unset("Cannot convert to native format from XML" TSRM= LS_CC); - RETURN_FALSE; - } - - VIRT_RETVAL_STRING(config_data); - free(config_data); -} - -/* - * Function name: libvirt_domain_memory_peek - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's memory peek value - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @start [int]: start - * @size [int]: size - * @flags [int]: optional flags - * Returns: domain memory peek - */ -PHP_FUNCTION(libvirt_domain_memory_peek) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - zend_long flags =3D 0; - zend_long start; - zend_long size; - char *buff; - - GET_DOMAIN_FROM_ARGS("rlll", &zdomain, &start, &size, &flags); - if (start < 0) { - set_error("Negative argument start" TSRMLS_CC); - RETURN_FALSE; - } - buff =3D (char *)emalloc(size); - retval =3D virDomainMemoryPeek(domain->domain, start, size, buff, flag= s); - if (retval !=3D 0) - RETURN_FALSE; - VIRT_RETVAL_STRINGL(buff, size); - efree(buff); -} - -/* - * Function name: libvirt_domain_memory_stats - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's memory stats - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @flags [int]: optional flags - * Returns: domain memory stats array (same fields as virDomainMem= oryStats, please see libvirt documentation) - */ -PHP_FUNCTION(libvirt_domain_memory_stats) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - zend_long flags =3D 0; - int i; - struct _virDomainMemoryStat stats[VIR_DOMAIN_MEMORY_STAT_NR]; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - retval =3D virDomainMemoryStats(domain->domain, stats, VIR_DOMAIN_MEMO= RY_STAT_NR, flags); - DPRINTF("%s: virDomainMemoryStats(%p...) returned %d\n", PHPFUNC, doma= in->domain, retval); - - if (retval =3D=3D -1) - RETURN_FALSE; - LONGLONG_INIT; - array_init(return_value); - for (i =3D 0; i < retval; i++) - LONGLONG_INDEX(return_value, stats[i].tag, stats[i].val) -} - -/* - * Function name: libvirt_domain_update_device - * Since version: 0.4.1(-1) - * Description: Function is used to update the domain's devices from t= he XML string - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @xml [string]: XML string for the update - * @flags [int]: Flags to update the device (VIR_DOMAIN_D= EVICE_MODIFY_CURRENT, VIR_DOMAIN_DEVICE_MODIFY_LIVE, VIR_DOMAIN_DEVICE_MODI= FY_CONFIG, VIR_DOMAIN_DEVICE_MODIFY_FORCE) - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_update_device) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - char *xml; - strsize_t xml_len; - zend_long flags; - int res; - - GET_DOMAIN_FROM_ARGS("rsl", &zdomain, &xml, &xml_len, &flags); - - res =3D virDomainUpdateDeviceFlags(domain->domain, xml, flags); - DPRINTF("%s: virDomainUpdateDeviceFlags(%p) returned %d\n", PHPFUNC, d= omain->domain, res); - if (res !=3D 0) - RETURN_FALSE; - - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_block_stats - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's block stats - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @path [string]: device path to get statistics about - * Returns: domain block stats array, fields are rd_req, rd_bytes,= wr_req, wr_bytes and errs - */ -PHP_FUNCTION(libvirt_domain_block_stats) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *path; - strsize_t path_len; - - struct _virDomainBlockStats stats; - - GET_DOMAIN_FROM_ARGS("rs", &zdomain, &path, &path_len); - - retval =3D virDomainBlockStats(domain->domain, path, &stats, sizeof st= ats); - DPRINTF("%s: virDomainBlockStats(%p, %s, , ) returned %d\= n", PHPFUNC, domain->domain, path, retval); - if (retval =3D=3D -1) - RETURN_FALSE; - - array_init(return_value); - LONGLONG_INIT; - LONGLONG_ASSOC(return_value, "rd_req", stats.rd_req); - LONGLONG_ASSOC(return_value, "rd_bytes", stats.rd_bytes); - LONGLONG_ASSOC(return_value, "wr_req", stats.wr_req); - LONGLONG_ASSOC(return_value, "wr_bytes", stats.wr_bytes); - LONGLONG_ASSOC(return_value, "errs", stats.errs); -} - -/* - * Function name: libvirt_domain_block_resize - * Since version: 0.5.1(-1) - * Description: Function is used to resize the domain's block device - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @path [string]: device path to resize - * @size [int]: size of device - * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_RESIZE_* - * Returns: true on success fail on error - */ -PHP_FUNCTION(libvirt_domain_block_resize) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *path; - strsize_t path_len; - zend_long size =3D 0; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("rsl|l", &zdomain, &path, &path_len, &size, &flag= s); - - retval =3D virDomainBlockResize(domain->domain, path, size, flags); - if (retval =3D=3D -1) - RETURN_FALSE; - - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_block_commit - * Since version: 0.5.2(-1) - * Description: Function is used to commit block job - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @disk [string]: path to the block device, or device sh= orthand - * @base [string]: path to backing file to merge into, or= device shorthand, or NULL for default - * @top [string]: path to file within backing chain that = contains data to be merged, or device shorthand, or NULL to merge all possi= ble data - * @bandwidth [int]: (optional) specify bandwidth limit; = flags determine the unit - * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_COMMIT_* - * Returns: true on success fail on error - */ -PHP_FUNCTION(libvirt_domain_block_commit) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *disk =3D NULL; - strsize_t disk_len; - char *base =3D NULL; - strsize_t base_len; - char *top =3D NULL; - strsize_t top_len; - zend_long bandwidth =3D 0; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("rs|ssll", &zdomain, &disk, &disk_len, &base, &ba= se_len, &top, &top_len, &bandwidth, &flags); - if (strcmp(disk, "") =3D=3D 0) - RETURN_FALSE; - if (strcmp(base, "") =3D=3D 0) - base =3D NULL; - if (strcmp(top, "") =3D=3D 0) - top =3D NULL; - - retval =3D virDomainBlockCommit(domain->domain, disk, base, top, bandw= idth, flags); - if (retval =3D=3D -1) - RETURN_FALSE; - - RETURN_TRUE; -} - - -/* - * Function name: libvirt_domain_block_job_info - * Since version: 0.5.2(-1) - * Description: Function is used to request block job information = for the given disk - * Arguments: @dom [resource]: libvirt domain resource, e.g. fro= m libvirt_domain_lookup_by_*() - * @disk [string]: path to the block device, or devic= e shorthand - * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_COMMI= T_* - * Returns: Array with status virDomainGetBlockJobInfo and blo= ckjob information. -*/ -PHP_FUNCTION(libvirt_domain_block_job_info) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *disk; - int disk_len; - long flags =3D 0; - virDomainBlockJobInfo info; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &disk, &disk_len, &flags); - - retval =3D virDomainGetBlockJobInfo(domain->domain, disk, &info, flags= ); - - array_init(return_value); - add_assoc_long(return_value, "status", (int)retval); - add_assoc_long(return_value, "type", (int)info.type); - add_assoc_long(return_value, "bandwidth", (unsigned long)info.bandwidt= h); - add_assoc_long(return_value, "cur", (unsigned long long)info.cur); - add_assoc_long(return_value, "end", (unsigned long long)info.end); -} - - -/* - * Function name: libvirt_domain_block_job_abort - * Since version: 0.5.1(-1) - * Description: Function is used to abort block job - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @path [string]: device path to resize - * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_JOB_ABORT= _* - * Returns: true on success fail on error - */ -PHP_FUNCTION(libvirt_domain_block_job_abort) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *path; - strsize_t path_len; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &path, &path_len, &flags); - - retval =3D virDomainBlockJobAbort(domain->domain, path, flags); - if (retval =3D=3D -1) - RETURN_FALSE; - - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_block_job_set_speed - * Since version: 0.5.1(-1) - * Description: Function is used to set speed of block job - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @path [string]: device path to resize - * @bandwidth [int]: bandwidth - * @flags [int]: bitwise-OR of VIR_DOMAIN_BLOCK_JOB_SPEED= _BANDWIDTH_* - * Returns: true on success fail on error - */ -PHP_FUNCTION(libvirt_domain_block_job_set_speed) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *path; - strsize_t path_len; - zend_long bandwidth =3D 0; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("rsl|l", &zdomain, &path, &path_len, &bandwidth, = &flags); - - retval =3D virDomainBlockJobSetSpeed(domain->domain, path, bandwidth, = flags); - if (retval =3D=3D -1) - RETURN_FALSE; - - RETURN_TRUE; -} - - - -/* - * Function name: libvirt_domain_get_network_info - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's network informati= on - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @mac [string]: mac address of the network device - * Returns: domain network info array of MAC address, network name= and type of NIC card - */ -PHP_FUNCTION(libvirt_domain_get_network_info) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *mac; - char *xml; - char *tmp =3D NULL; - strsize_t mac_len; - char *xpath =3D NULL; - - GET_DOMAIN_FROM_ARGS("rs", &zdomain, &mac, &mac_len); - - /* Get XML for the domain */ - xml =3D virDomainGetXMLDesc(domain->domain, VIR_DOMAIN_XML_INACTIVE); - if (!xml) { - set_error("Cannot get domain XML" TSRMLS_CC); - RETURN_FALSE; - } - - DPRINTF("%s: Getting network information for NIC with MAC address '%s'= \n", PHPFUNC, mac); - if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/../source/@network", mac) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (tmp =3D=3D NULL) { - set_error("Invalid XPath node for source network" TSRMLS_CC); - goto error; - } - - if (retval < 0) { - set_error("Cannot get XPath expression result for network source" = TSRMLS_CC); - goto error; - } - - array_init(return_value); - VIRT_ADD_ASSOC_STRING(return_value, "mac", mac); - VIRT_ADD_ASSOC_STRING(return_value, "network", tmp); - - free(tmp); - free(xpath); - - if (asprintf(&xpath, "//domain/devices/interface[@type=3D'network']/ma= c[@address=3D'%s']/../model/@type", mac) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "nic_type", tmp); - else - VIRT_ADD_ASSOC_STRING(return_value, "nic_type", "default"); - - free(xml); - free(xpath); - free(tmp); - return; - - error: - free(xml); - free(xpath); - free(tmp); - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_get_block_info - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's block device info= rmation - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @dev [string]: device to get block information about - * Returns: domain block device information array of device, file = or partition, capacity, allocation and physical size - */ -PHP_FUNCTION(libvirt_domain_get_block_info) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *dev; - char *xml; - char *tmp =3D NULL; - strsize_t dev_len; - int isFile; - char *xpath =3D NULL; - - struct _virDomainBlockInfo info; - - GET_DOMAIN_FROM_ARGS("rs", &zdomain, &dev, &dev_len); - - /* Get XML for the domain */ - xml =3D virDomainGetXMLDesc(domain->domain, VIR_DOMAIN_XML_INACTIVE); - if (!xml) { - set_error("Cannot get domain XML" TSRMLS_CC); - RETURN_FALSE; - } - - isFile =3D 0; - - if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/../sou= rce/@dev", dev) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (retval < 0) { - set_error("Cannot get XPath expression result for device storage" = TSRMLS_CC); - goto error; - } - - if (retval =3D=3D 0) { - free(xpath); - if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/..= /source/@file", dev) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - free(tmp); - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (retval < 0) { - set_error("Cannot get XPath expression result for file storage= " TSRMLS_CC); - goto error; - } - isFile =3D 1; - } - - if (retval =3D=3D 0) { - set_error("No relevant node found" TSRMLS_CC); - goto error; - } - - retval =3D virDomainGetBlockInfo(domain->domain, tmp, &info, 0); - if (retval =3D=3D -1) { - set_error("Cannot get domain block information" TSRMLS_CC); - goto error; - } - - array_init(return_value); - LONGLONG_INIT; - VIRT_ADD_ASSOC_STRING(return_value, "device", dev); - - if (isFile) - VIRT_ADD_ASSOC_STRING(return_value, "file", tmp); - else - VIRT_ADD_ASSOC_STRING(return_value, "partition", tmp); - - free(xpath); - if (asprintf(&xpath, "//domain/devices/disk/target[@dev=3D'%s']/../dri= ver/@type", dev) < 0) { - set_error("Out of memory" TSRMLS_CC); - goto error; - } - free(tmp); - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if (tmp !=3D NULL) - VIRT_ADD_ASSOC_STRING(return_value, "type", tmp); - - LONGLONG_ASSOC(return_value, "capacity", info.capacity); - LONGLONG_ASSOC(return_value, "allocation", info.allocation); - LONGLONG_ASSOC(return_value, "physical", info.physical); - - free(xpath); - free(tmp); - free(xml); - return; - - error: - free(xpath); - free(tmp); - free(xml); - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_xml_xpath - * Since version: 0.4.1(-1) - * Description: Function is used to get the result of xPath expression= that's run against the domain - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @xpath [string]: xPath expression to parse against the= domain - * @flags [int]: optional flags - * Returns: result of the expression in an array - */ -PHP_FUNCTION(libvirt_domain_xml_xpath) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - zval *zpath; - char *xml; - char *tmp =3D NULL; - strsize_t path_len =3D -1; - zend_long flags =3D 0; - int rc =3D 0; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &zpath, &path_len, &flags); - - xml =3D virDomainGetXMLDesc(domain->domain, flags); - if (!xml) - RETURN_FALSE; - - array_init(return_value); - - free(get_string_from_xpath(xml, (char *)zpath, &return_value, &rc)); - if (return_value < 0) { - free(xml); - RETURN_FALSE; - } - - free(tmp); - free(xml); - - if (rc =3D=3D 0) - RETURN_FALSE; - - VIRT_ADD_ASSOC_STRING(return_value, "xpath", (char *)zpath); - if (rc < 0) - add_assoc_long(return_value, "error_code", (long)rc); -} - -/* - * Function name: libvirt_domain_interface_stats - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's interface stats - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @path [string]: path to interface device - * Returns: interface stats array of {tx|rx}_{bytes|packets|errs|d= rop} fields - */ -PHP_FUNCTION(libvirt_domain_interface_stats) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *path; - strsize_t path_len; - - struct _virDomainInterfaceStats stats; - - GET_DOMAIN_FROM_ARGS("rs", &zdomain, &path, &path_len); - - retval =3D virDomainInterfaceStats(domain->domain, path, &stats, sizeo= f stats); - DPRINTF("%s: virDomainInterfaceStats(%p, %s, , ) returned= %d\n", PHPFUNC, domain->domain, path, retval); - if (retval =3D=3D -1) - RETURN_FALSE; - - array_init(return_value); - LONGLONG_INIT; - LONGLONG_ASSOC(return_value, "rx_bytes", stats.rx_bytes); - LONGLONG_ASSOC(return_value, "rx_packets", stats.rx_packets); - LONGLONG_ASSOC(return_value, "rx_errs", stats.rx_errs); - LONGLONG_ASSOC(return_value, "rx_drop", stats.rx_drop); - LONGLONG_ASSOC(return_value, "tx_bytes", stats.tx_bytes); - LONGLONG_ASSOC(return_value, "tx_packets", stats.tx_packets); - LONGLONG_ASSOC(return_value, "tx_errs", stats.tx_errs); - LONGLONG_ASSOC(return_value, "tx_drop", stats.tx_drop); -} - -/* - * Function name: libvirt_domain_get_connect - * Since version: 0.4.1(-1) - * Description: Function is used to get the domain's connection resour= ce. This function should *not* be used! - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: libvirt connection resource - */ -PHP_FUNCTION(libvirt_domain_get_connect) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - php_libvirt_connection *conn; - - DPRINTF("%s: Warning: libvirt_domain_get_connect() used. This function= should not be used!\n", PHPFUNC); - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - conn =3D domain->conn; - if (conn->conn =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETURN_RESOURCE(conn->resource); - /* since we're returning already registered resource, bump refcount */ - Z_ADDREF_P(return_value); -} - -/* - * Function name: libvirt_domain_migrate_to_uri - * Since version: 0.4.1(-1) - * Description: Function is used migrate domain to another libvirt dae= mon specified by it's URI - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @dest_uri [string]: destination URI to migrate to - * @flags [int]: migration flags - * @dname [string]: domain name to rename domain to on de= stination side - * @bandwidth [int]: migration bandwidth in Mbps - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_migrate_to_uri) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - long flags =3D 0; - char *duri; - strsize_t duri_len; - char *dname; - strsize_t dname_len; - zend_long bandwidth; - - dname =3D NULL; - dname_len =3D 0; - bandwidth =3D 0; - GET_DOMAIN_FROM_ARGS("rsl|sl", &zdomain, &duri, &duri_len, &flags, &dn= ame, &dname_len, &bandwidth); - - retval =3D virDomainMigrateToURI(domain->domain, duri, flags, dname, b= andwidth); - DPRINTF("%s: virDomainMigrateToURI() returned %d\n", PHPFUNC, retval); - - if (retval =3D=3D 0) - RETURN_TRUE; - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_migrate_to_uri2 - * Since version: 0.4.6(-1) - * Description: Function is used migrate domain to another libvirt dae= mon specified by it's URI - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @dconnuri [string]: URI for target libvirtd - * @miguri [string]: URI for invoking the migration - * @dxml [string]: XML config for launching guest on targ= et - * @flags [int]: migration flags - * @dname [string]: domain name to rename domain to on de= stination side - * @bandwidth [int]: migration bandwidth in Mbps - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_migrate_to_uri2) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - char *dconnuri; - strsize_t dconnuri_len; - char *miguri; - strsize_t miguri_len; - char *dxml; - strsize_t dxml_len; - zend_long flags =3D 0; - char *dname; - strsize_t dname_len; - zend_long bandwidth; - - dconnuri =3D NULL; - dconnuri_len =3D 0; - miguri =3D NULL; - miguri_len =3D 0; - dxml =3D NULL; - dxml_len =3D 0; - dname =3D NULL; - dname_len =3D 0; - bandwidth =3D 0; - GET_DOMAIN_FROM_ARGS("r|ssslsl", &zdomain, &dconnuri, &dconnuri_len, &= miguri, &miguri_len, &dxml, &dxml_len, &flags, &dname, &dname_len, &bandwid= th); - - // set to NULL if empty string - if (dconnuri_len =3D=3D 0) - dconnuri=3DNULL; - if (miguri_len =3D=3D 0) - miguri=3DNULL; - if (dxml_len =3D=3D 0) - dxml=3DNULL; - if (dname_len =3D=3D 0) - dname=3DNULL; - - retval =3D virDomainMigrateToURI2(domain->domain, dconnuri, miguri, dx= ml, flags, dname, bandwidth); - DPRINTF("%s: virDomainMigrateToURI2() returned %d\n", PHPFUNC, retval); - - if (retval =3D=3D 0) - RETURN_TRUE; - RETURN_FALSE; -} - -/* - * Function name: libvirt_domain_migrate - * Since version: 0.4.1(-1) - * Description: Function is used migrate domain to another domain - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * @dest_conn [string]: destination host connection object - * @flags [int]: migration flags - * @dname [string]: domain name to rename domain to on de= stination side - * @bandwidth [int]: migration bandwidth in Mbps - * Returns: libvirt domain resource for migrated domain - */ -PHP_FUNCTION(libvirt_domain_migrate) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - php_libvirt_connection *dconn =3D NULL; - zval *zdconn; - virDomainPtr destdomain =3D NULL; - php_libvirt_domain *res_domain; - - zend_long flags =3D 0; - char *dname; - strsize_t dname_len; - zend_long bandwidth; - - dname =3D NULL; - dname_len =3D 0; - bandwidth =3D 0; - - GET_DOMAIN_FROM_ARGS("rrl|sl", &zdomain, &zdconn, &flags, &dname, &dna= me_len, &bandwidth); - - if ((domain->domain =3D=3D NULL) || (domain->conn->conn =3D=3D NULL)) { - set_error("Domain object is not valid" TSRMLS_CC); - RETURN_FALSE; - } - - VIRT_FETCH_RESOURCE(dconn, php_libvirt_connection*, &zdconn, PHP_LIBVI= RT_CONNECTION_RES_NAME, le_libvirt_connection); - if ((dconn =3D=3D NULL) || (dconn->conn =3D=3D NULL)) { - set_error("Destination connection object is not valid" TSRMLS_CC); - RETURN_FALSE; - } - - destdomain =3D virDomainMigrate(domain->domain, dconn->conn, flags, dn= ame, NULL, bandwidth); - if (destdomain =3D=3D NULL) - RETURN_FALSE; - - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvirt_domain= )); - res_domain->domain =3D destdomain; - res_domain->conn =3D dconn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_domain->domain); - resource_change_counter(INT_RESOURCE_DOMAIN, dconn->conn, res_domain->= domain, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_domain, le_libvirt_domain); -} - -/* - * Function name: libvirt_domain_get_job_info - * Since version: 0.4.1(-1) - * Description: Function is used get job information for the domain - * Arguments: @res [resource]: libvirt domain resource, e.g. from li= bvirt_domain_lookup_by_*() - * Returns: job information array of type, time, data, mem and fil= e fields - */ -PHP_FUNCTION(libvirt_domain_get_job_info) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - - struct _virDomainJobInfo jobinfo; - - GET_DOMAIN_FROM_ARGS("r", &zdomain); - - retval =3D virDomainGetJobInfo(domain->domain, &jobinfo); - if (retval =3D=3D -1) - RETURN_FALSE; - - array_init(return_value); - LONGLONG_INIT; - add_assoc_long(return_value, "type", jobinfo.type); - LONGLONG_ASSOC(return_value, "time_elapsed", jobinfo.timeElapsed); - LONGLONG_ASSOC(return_value, "time_remaining", jobinfo.timeRemaining); - LONGLONG_ASSOC(return_value, "data_total", jobinfo.dataTotal); - LONGLONG_ASSOC(return_value, "data_processed", jobinfo.dataProcessed); - LONGLONG_ASSOC(return_value, "data_remaining", jobinfo.dataRemaining); - LONGLONG_ASSOC(return_value, "mem_total", jobinfo.memTotal); - LONGLONG_ASSOC(return_value, "mem_processed", jobinfo.memProcessed); - LONGLONG_ASSOC(return_value, "mem_remaining", jobinfo.memRemaining); - LONGLONG_ASSOC(return_value, "file_total", jobinfo.fileTotal); - LONGLONG_ASSOC(return_value, "file_processed", jobinfo.fileProcessed); - LONGLONG_ASSOC(return_value, "file_remaining", jobinfo.fileRemaining); -} - -/* - * Function name: libvirt_domain_has_current_snapshot - * Since version: 0.4.1(-2) - * Description: Function is used to get the information whether domain= has the current snapshot - * Arguments: @res [resource]: libvirt domain resource - * @flags [int]: libvirt snapshot flags - * Returns: TRUE is domain has the current snapshot, otherwise FAL= SE (you may need to check for error using libvirt_get_last_error()) - */ -PHP_FUNCTION(libvirt_domain_has_current_snapshot) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - retval =3D virDomainHasCurrentSnapshot(domain->domain, flags); - if (retval <=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_snapshot_lookup_by_name - * Since version: 0.4.1(-2) - * Description: This functions is used to lookup for the snapshot by i= t's name - * Arguments: @res [resource]: libvirt domain resource - * @name [string]: name of the snapshot to get the resour= ce - * @flags [int]: libvirt snapshot flags - * Returns: domain snapshot resource - */ -PHP_FUNCTION(libvirt_domain_snapshot_lookup_by_name) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - strsize_t name_len; - char *name =3D NULL; - zend_long flags =3D 0; - php_libvirt_snapshot *res_snapshot; - virDomainSnapshotPtr snapshot =3D NULL; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &name, &name_len, &flags); - - if ((name =3D=3D NULL) || (name_len < 1)) - RETURN_FALSE; - snapshot=3DvirDomainSnapshotLookupByName(domain->domain, name, flags); - if (snapshot =3D=3D NULL) - RETURN_FALSE; - - res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); - res_snapshot->domain =3D domain; - res_snapshot->snapshot =3D snapshot; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); - resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); -} - -/* - * Function name: libvirt_domain_snapshot_create - * Since version: 0.4.1(-2) - * Description: This function creates the domain snapshot for the doma= in identified by it's resource - * Arguments: @res [resource]: libvirt domain resource - * @flags [int]: libvirt snapshot flags - * Returns: domain snapshot resource - */ -PHP_FUNCTION(libvirt_domain_snapshot_create) -{ - php_libvirt_domain *domain =3D NULL; - php_libvirt_snapshot *res_snapshot; - zval *zdomain; - virDomainSnapshotPtr snapshot =3D NULL; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - snapshot =3D virDomainSnapshotCreateXML(domain->domain, "", flags); - DPRINTF("%s: virDomainSnapshotCreateXML(%p, ) returned %p\n", PHP= FUNC, domain->domain, snapshot); - if (snapshot =3D=3D NULL) - RETURN_FALSE; - - res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); - res_snapshot->domain =3D domain; - res_snapshot->snapshot =3D snapshot; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); - resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); -} - -/* - * Function name: libvirt_domain_snapshot_get_xml - * Since version: 0.4.1(-2) - * Description: Function is used to get the XML description of the sna= pshot identified by it's resource - * Arguments: @res [resource]: libvirt snapshot resource - * @flags [int]: libvirt snapshot flags - * Returns: XML description string for the snapshot - */ -PHP_FUNCTION(libvirt_domain_snapshot_get_xml) -{ - char *xml; - zval *zsnapshot; - php_libvirt_snapshot *snapshot; - zend_long flags =3D 0; - - GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); - - xml =3D virDomainSnapshotGetXMLDesc(snapshot->snapshot, flags); - if (xml =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETVAL_STRING(xml); - free(xml); -} - -/* - * Function name: libvirt_domain_snapshot_revert - * Since version: 0.4.1(-2) - * Description: Function is used to revert the domain state to the sta= te identified by the snapshot - * Arguments: @res [resource]: libvirt snapshot resource - * @flags [int]: libvirt snapshot flags - * Returns: TRUE on success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_snapshot_revert) -{ - zval *zsnapshot; - php_libvirt_snapshot *snapshot; - int ret; - zend_long flags =3D 0; - - GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); - - ret =3D virDomainRevertToSnapshot(snapshot->snapshot, flags); - DPRINTF("%s: virDomainRevertToSnapshot(%p, 0) returned %d\n", PHPFUNC,= snapshot->snapshot, ret); - if (ret =3D=3D -1) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_snapshot_delete - * Since version: 0.4.1(-2) - * Description: Function is used to revert the domain state to the sta= te identified by the snapshot - * Arguments: @res [resource]: libvirt snapshot resource - * @flags [int]: 0 to delete just snapshot, VIR_SNAPSHOT_= DELETE_CHILDREN to delete snapshot children as well - * Returns: TRUE on success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_snapshot_delete) -{ - zval *zsnapshot; - php_libvirt_snapshot *snapshot; - zend_long flags =3D 0; - int retval; - - GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); - - retval =3D virDomainSnapshotDelete(snapshot->snapshot, flags); - DPRINTF("%s: virDomainSnapshotDelete(%p, %d) returned %d\n", PHPFUNC, = snapshot->snapshot, (int) flags, retval); - if (retval =3D=3D -1) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_list_domain_snapshots - * Since version: 0.4.1(-2) - * Description: Function is used to list domain snapshots for the doma= in specified by it's resource - * Arguments: @res [resource]: libvirt domain resource - * @flags [int]: libvirt snapshot flags - * Returns: libvirt domain snapshot names array - */ -PHP_FUNCTION(libvirt_list_domain_snapshots) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int count =3D -1; - int expectedcount =3D -1; - char **names; - zend_long flags =3D 0; - int i; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - expectedcount =3D virDomainSnapshotNum(domain->domain, flags); - DPRINTF("%s: virDomainSnapshotNum(%p, 0) returned %d\n", PHPFUNC, doma= in->domain, expectedcount); - - if (expectedcount !=3D -1) { - names =3D (char **)emalloc(expectedcount * sizeof(char *)); - count =3D virDomainSnapshotListNames(domain->domain, names, expect= edcount, 0); - } + if (expectedcount !=3D -1) { + names =3D (char **)emalloc(expectedcount * sizeof(char *)); + count =3D virDomainSnapshotListNames(domain->domain, names, expect= edcount, 0); + } =20 if ((count !=3D expectedcount) || (count < 0)) { RETURN_FALSE; @@ -7214,271 +4054,6 @@ PHP_FUNCTION(libvirt_list_inactive_storagepools) } =20 /* - * Function name: libvirt_list_domains - * Since version: 0.4.1(-1) - * Description: Function is used to list domains on the connection - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt domain names array for the connection - */ -PHP_FUNCTION(libvirt_list_domains) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - int *ids; - char **names; - const char *name; - int i, rv; - virDomainPtr domain =3D NULL; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) - RETURN_FALSE; - - DPRINTF("%s: Found %d domains\n", PHPFUNC, expectedcount); - - ids =3D (int *)emalloc(sizeof(int) * expectedcount); - count =3D virConnectListDomains(conn->conn, ids, expectedcount); - DPRINTF("%s: virConnectListDomains returned %d domains\n", PHPFUNC, co= unt); - - array_init(return_value); - for (i =3D 0; i < count; i++) { - domain =3D virDomainLookupByID(conn->conn, ids[i]); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, domain, 1= TSRMLS_CC); - if (domain !=3D NULL) { - name =3D virDomainGetName(domain); - if (name !=3D NULL) { - DPRINTF("%s: Found running domain %s with ID =3D %d\n", PH= PFUNC, name, ids[i]); - VIRT_ADD_NEXT_INDEX_STRING(return_value, name); - } else { - DPRINTF("%s: Cannot get ID for running domain %d\n", PHPFU= NC, ids[i]); - } - } - rv =3D virDomainFree(domain); - if (rv !=3D 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainFree fai= led with %i on list_domain: %s", - rv, LIBVIRT_G(last_error)); - } else { - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, domai= n, 0 TSRMLS_CC); - } - domain =3D NULL; - } - efree(ids); - - expectedcount =3D virConnectNumOfDefinedDomains(conn->conn); - DPRINTF("%s: virConnectNumOfDefinedDomains returned %d domains\n", PHP= FUNC, expectedcount); - if (expectedcount < 0) { - DPRINTF("%s: virConnectNumOfDefinedDomains failed with error code = %d\n", PHPFUNC, expectedcount); - RETURN_FALSE; - } - - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virConnectListDefinedDomains(conn->conn, names, expectedcoun= t); - DPRINTF("%s: virConnectListDefinedDomains returned %d domains\n", PHPF= UNC, count); - if (count < 0) { - DPRINTF("%s: virConnectListDefinedDomains failed with error code %= d\n", PHPFUNC, count); - RETURN_FALSE; - } - - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - DPRINTF("%s: Found inactive domain %s\n", PHPFUNC, names[i]); - free(names[i]); - } - efree(names); -} - -/* - * Function name: libvirt_list_domain_resources - * Since version: 0.4.1(-1) - * Description: Function is used to list domain resources on the conne= ction - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt domain resources array for the connection - */ -PHP_FUNCTION(libvirt_list_domain_resources) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - int *ids; - char **names; - int i; - - virDomainPtr domain =3D NULL; - php_libvirt_domain *res_domain; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) - RETURN_FALSE; - - ids =3D (int *)emalloc(sizeof(int) * expectedcount); - count =3D virConnectListDomains(conn->conn, ids, expectedcount); - if ((count !=3D expectedcount) || (count < 0)) { - efree(ids); - RETURN_FALSE; - } - array_init(return_value); - for (i =3D 0; i < count; i++) { - domain =3D virDomainLookupByID(conn->conn, ids[i]); - if (domain !=3D NULL) { - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvir= t_domain)); - res_domain->domain =3D domain; - - res_domain->conn =3D conn; - - VIRT_REGISTER_LIST_RESOURCE(domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_d= omain->domain, 1 TSRMLS_CC); - } - } - efree(ids); - - if ((expectedcount =3D virConnectNumOfDefinedDomains(conn->conn)) < 0) - RETURN_FALSE; - - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virConnectListDefinedDomains(conn->conn, names, expectedcoun= t); - if ((count !=3D expectedcount) || (count < 0)) { - efree(names); - RETURN_FALSE; - } - for (i =3D 0; i < count; i++) { - domain =3D virDomainLookupByName(conn->conn, names[i]); - if (domain !=3D NULL) { - res_domain =3D (php_libvirt_domain *)emalloc(sizeof(php_libvir= t_domain)); - res_domain->domain =3D domain; - - res_domain->conn =3D conn; - - VIRT_REGISTER_LIST_RESOURCE(domain); - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, res_d= omain->domain, 1 TSRMLS_CC); - } - free(names[i]); - } - efree(names); -} - -/* - * Function name: libvirt_list_active_domain_ids - * Since version: 0.4.1(-1) - * Description: Function is used to list active domain IDs on the conn= ection - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt active domain ids array for the connection - */ -PHP_FUNCTION(libvirt_list_active_domain_ids) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - int *ids; - int i; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) - RETURN_FALSE; - - ids =3D (int *)emalloc(sizeof(int) * expectedcount); - count =3D virConnectListDomains(conn->conn, ids, expectedcount); - if ((count !=3D expectedcount) || (count < 0)) { - efree(ids); - RETURN_FALSE; - } - array_init(return_value); - for (i =3D 0; i < count; i++) - add_next_index_long(return_value, ids[i]); - efree(ids); -} - -/* - * Function name: libvirt_list_active_domains - * Since version: 0.4.1(-1) - * Description: Function is used to list active domain names on the co= nnection - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt active domain names array for the connection - */ -PHP_FUNCTION(libvirt_list_active_domains) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - int *ids; - int i; - virDomainPtr domain =3D NULL; - const char *name; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((expectedcount =3D virConnectNumOfDomains(conn->conn)) < 0) - RETURN_FALSE; - - ids =3D (int *)emalloc(sizeof(int) * expectedcount); - count =3D virConnectListDomains(conn->conn, ids, expectedcount); - if ((count !=3D expectedcount) || (count < 0)) { - efree(ids); - RETURN_FALSE; - } - - array_init(return_value); - for (i =3D 0; i < count; i++) { - domain =3D virDomainLookupByID(conn->conn, ids[i]); - if (domain !=3D NULL) { - if (!(name =3D virDomainGetName(domain))) { - efree(ids); - RETURN_FALSE; - } - - VIRT_ADD_NEXT_INDEX_STRING(return_value, name); - - if (virDomainFree(domain)) - resource_change_counter(INT_RESOURCE_DOMAIN, conn->conn, d= omain, 0 TSRMLS_CC); - } - } - efree(ids); -} - -/* - * Function name: libvirt_list_inactive_domains - * Since version: 0.4.1(-1) - * Description: Function is used to list inactive domain names on the = connection - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt inactive domain names array for the connection - */ -PHP_FUNCTION(libvirt_list_inactive_domains) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - char **names; - int i; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((expectedcount =3D virConnectNumOfDefinedDomains(conn->conn)) < 0) - RETURN_FALSE; - - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virConnectListDefinedDomains(conn->conn, names, expectedcoun= t); - if ((count !=3D expectedcount) || (count < 0)) { - efree(names); - RETURN_FALSE; - } - - array_init(return_value); - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - efree(names); -} - -/* * Function name: libvirt_list_all_networks * Since version: 0.5.3 * Description: Function is used to list networks on the connection diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 1a009d3..a27ed82 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -127,21 +127,7 @@ typedef uint64_t arch_uint; #define PHP_LIBVIRT_WORLD_VERSION VERSION #define PHP_LIBVIRT_WORLD_EXTNAME "libvirt" =20 -/* Domain flags */ -#define DOMAIN_FLAG_FEATURE_ACPI 0x01 -#define DOMAIN_FLAG_FEATURE_APIC 0x02 -#define DOMAIN_FLAG_FEATURE_PAE 0x04 -#define DOMAIN_FLAG_CLOCK_LOCALTIME 0x08 -#define DOMAIN_FLAG_TEST_LOCAL_VNC 0x10 -#define DOMAIN_FLAG_SOUND_AC97 0x20 - -/* Domain disk flags */ -#define DOMAIN_DISK_FILE 0x01 -#define DOMAIN_DISK_BLOCK 0x02 -#define DOMAIN_DISK_ACCESS_ALL 0x04 - /* Internal resource identifier objects */ -#define INT_RESOURCE_DOMAIN 0x02 #define INT_RESOURCE_NETWORK 0x04 #define INT_RESOURCE_NODEDEV 0x08 #define INT_RESOURCE_STORAGEPOOL 0x10 @@ -176,15 +162,11 @@ typedef struct tVMNetwork { char *model; } tVMNetwork; =20 -/* TODO: temporary forward declaration until other parts are "modularized"= */ +/* TODO: temporary forward declarations until other parts are "modularized= " */ typedef struct _php_libvirt_connection php_libvirt_connection; +typedef struct _php_libvirt_domain php_libvirt_domain; =20 /* Libvirt-php types */ -typedef struct _php_libvirt_domain { - virDomainPtr domain; - php_libvirt_connection* conn; -} php_libvirt_domain; - typedef struct _php_libvirt_snapshot { virDomainSnapshotPtr snapshot; php_libvirt_domain* domain; @@ -239,10 +221,13 @@ ZEND_DECLARE_MODULE_GLOBALS(libvirt) =20 /* Private definitions */ void set_error(char *msg TSRMLS_DC); +void set_error_if_unset(char *msg TSRMLS_DC); void reset_error(TSRMLS_D); int count_resources(int type TSRMLS_DC); -int resource_change_counter(int type, virConnectPtr conn, void *mem, int i= nc TSRMLS_DC); -int check_resource_allocation(virConnectPtr conn, int type, void *mem TSRM= LS_DC); +int resource_change_counter(int type, virConnectPtr conn, void *mem, + int inc TSRMLS_DC); +int check_resource_allocation(virConnectPtr conn, int type, + void *mem TSRMLS_DC); void free_resource(int type, void *mem TSRMLS_DC); char *connection_get_emulator(virConnectPtr conn, char *arch TSRMLS_DC); int is_local_connection(virConnectPtr conn); @@ -251,8 +236,18 @@ void free_tokens(tTokenizer t); int set_logfile(char *filename, long maxsize TSRMLS_DC); char *get_string_from_xpath(char *xml, char *xpath, zval **val, int *retVa= l); char **get_array_from_xpath(char *xml, char *xpath, int *num); +void parse_array(zval *arr, tVMDisk *disk, tVMNetwork *network); +char *installation_get_xml(int step, virConnectPtr conn, char *name, int m= emMB, + int maxmemMB, char *arch, char *uuid, int vCpus, + char *iso_image, tVMDisk *disks, int numDisks, + tVMNetwork *networks, int numNetworks, + int domain_flags TSRMLS_DC); +void set_vnc_location(char *msg TSRMLS_DC); +int streamSink(virStreamPtr st ATTRIBUTE_UNUSED, + const char *bytes, size_t nbytes, void *opaque); +const char *get_feature_binary(const char *name); +long get_next_free_numeric_value(virDomainPtr domain, char *xpath); =20 -#define PHP_LIBVIRT_DOMAIN_RES_NAME "Libvirt domain" #define PHP_LIBVIRT_STORAGEPOOL_RES_NAME "Libvirt storagepool" #define PHP_LIBVIRT_VOLUME_RES_NAME "Libvirt volume" #define PHP_LIBVIRT_NETWORK_RES_NAME "Libvirt virtual network" @@ -268,79 +263,6 @@ PHP_MINFO_FUNCTION(libvirt); =20 /* Common functions */ PHP_FUNCTION(libvirt_get_last_error); -/* Domain functions */ -PHP_FUNCTION(libvirt_domain_new); -PHP_FUNCTION(libvirt_domain_new_get_vnc); -PHP_FUNCTION(libvirt_domain_get_counts); -PHP_FUNCTION(libvirt_domain_is_persistent); -PHP_FUNCTION(libvirt_domain_lookup_by_name); -PHP_FUNCTION(libvirt_domain_get_xml_desc); -PHP_FUNCTION(libvirt_domain_get_disk_devices); -PHP_FUNCTION(libvirt_domain_get_interface_devices); -PHP_FUNCTION(libvirt_domain_get_screenshot); -PHP_FUNCTION(libvirt_domain_get_screenshot_api); -PHP_FUNCTION(libvirt_domain_get_screen_dimensions); -PHP_FUNCTION(libvirt_domain_change_vcpus); -PHP_FUNCTION(libvirt_domain_change_memory); -PHP_FUNCTION(libvirt_domain_change_boot_devices); -PHP_FUNCTION(libvirt_domain_disk_add); -PHP_FUNCTION(libvirt_domain_disk_remove); -PHP_FUNCTION(libvirt_domain_nic_add); -PHP_FUNCTION(libvirt_domain_nic_remove); -PHP_FUNCTION(libvirt_domain_attach_device); -PHP_FUNCTION(libvirt_domain_detach_device); -PHP_FUNCTION(libvirt_domain_get_info); -PHP_FUNCTION(libvirt_domain_get_uuid); -PHP_FUNCTION(libvirt_domain_get_uuid_string); -PHP_FUNCTION(libvirt_domain_get_name); -PHP_FUNCTION(libvirt_domain_get_id); -PHP_FUNCTION(libvirt_domain_lookup_by_id); -PHP_FUNCTION(libvirt_domain_lookup_by_uuid); -PHP_FUNCTION(libvirt_domain_lookup_by_uuid_string); -PHP_FUNCTION(libvirt_domain_destroy); -PHP_FUNCTION(libvirt_domain_create); -PHP_FUNCTION(libvirt_domain_resume); -PHP_FUNCTION(libvirt_domain_core_dump); -PHP_FUNCTION(libvirt_domain_shutdown); -PHP_FUNCTION(libvirt_domain_suspend); -PHP_FUNCTION(libvirt_domain_managedsave); -PHP_FUNCTION(libvirt_domain_undefine); -PHP_FUNCTION(libvirt_domain_reboot); -PHP_FUNCTION(libvirt_domain_define_xml); -PHP_FUNCTION(libvirt_domain_create_xml); -PHP_FUNCTION(libvirt_domain_xml_from_native); -PHP_FUNCTION(libvirt_domain_xml_to_native); -PHP_FUNCTION(libvirt_domain_set_max_memory); -PHP_FUNCTION(libvirt_domain_set_memory); -PHP_FUNCTION(libvirt_domain_set_memory_flags); -PHP_FUNCTION(libvirt_domain_memory_peek); -PHP_FUNCTION(libvirt_domain_memory_stats); -PHP_FUNCTION(libvirt_domain_update_device); -PHP_FUNCTION(libvirt_domain_block_commit); -PHP_FUNCTION(libvirt_domain_block_stats); -PHP_FUNCTION(libvirt_domain_block_resize); -PHP_FUNCTION(libvirt_domain_block_job_abort); -PHP_FUNCTION(libvirt_domain_block_job_set_speed); -PHP_FUNCTION(libvirt_domain_block_job_info); -PHP_FUNCTION(libvirt_domain_interface_stats); -PHP_FUNCTION(libvirt_domain_get_connect); -PHP_FUNCTION(libvirt_domain_migrate); -PHP_FUNCTION(libvirt_domain_get_job_info); -PHP_FUNCTION(libvirt_domain_xml_xpath); -PHP_FUNCTION(libvirt_domain_get_block_info); -PHP_FUNCTION(libvirt_domain_get_network_info); -PHP_FUNCTION(libvirt_domain_migrate_to_uri); -PHP_FUNCTION(libvirt_domain_migrate_to_uri2); -PHP_FUNCTION(libvirt_domain_get_autostart); -PHP_FUNCTION(libvirt_domain_set_autostart); -PHP_FUNCTION(libvirt_domain_is_active); -PHP_FUNCTION(libvirt_domain_get_next_dev_ids); -PHP_FUNCTION(libvirt_domain_send_keys); -PHP_FUNCTION(libvirt_domain_send_key_api); -PHP_FUNCTION(libvirt_domain_send_pointer_event); -PHP_FUNCTION(libvirt_domain_get_metadata); -PHP_FUNCTION(libvirt_domain_set_metadata); -PHP_FUNCTION(libvirt_domain_qemu_agent_command); /* Domain snapshot functions */ PHP_FUNCTION(libvirt_domain_has_current_snapshot); PHP_FUNCTION(libvirt_domain_snapshot_create); @@ -415,12 +337,7 @@ 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); -PHP_FUNCTION(libvirt_list_active_domains); -PHP_FUNCTION(libvirt_list_active_domain_ids); -PHP_FUNCTION(libvirt_list_inactive_domains); PHP_FUNCTION(libvirt_list_storagepools); PHP_FUNCTION(libvirt_list_active_storagepools); PHP_FUNCTION(libvirt_list_inactive_storagepools); --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 23:55:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 150162555604588.42396533889541; Tue, 1 Aug 2017 15:12:36 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 762EE101731; Tue, 1 Aug 2017 22:12:33 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4C73118174; Tue, 1 Aug 2017 22:12:33 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id DA74C1800C82; Tue, 1 Aug 2017 22:12:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkVB4007886 for ; Tue, 1 Aug 2017 17:46:31 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1997560476; Tue, 1 Aug 2017 21:46:31 +0000 (UTC) Received: from mx1.redhat.com (ext-mx07.extmail.prod.ext.phx2.redhat.com [10.5.110.31]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1140160462 for ; Tue, 1 Aug 2017 21:46:31 +0000 (UTC) Received: from mail-qk0-f193.google.com (mail-qk0-f193.google.com [209.85.220.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 60823C08C533 for ; Tue, 1 Aug 2017 21:46:23 +0000 (UTC) Received: by mail-qk0-f193.google.com with SMTP id d145so2929239qkc.0 for ; Tue, 01 Aug 2017 14:46:23 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.21 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:21 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 762EE101731 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 762EE101731 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GZAWXn5p" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 60823C08C533 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 60823C08C533 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=vbD9T46peKuVuWVXgU4L562EvkcM3pBZQg2XGEQt4sI=; b=GZAWXn5pD8xJGgtBHGtXS9+H1HHc4tq8qQ+X/pWhXIzkHUmLXbnGzTUu5Vpr57P8zV oChzuxyDzmzG+ARdEVOjAtUWJGAv+kaYiEQtwP4Tn6yZclABmQxTpDirCzQP3JDCpZIQ G9K5OtDhsNQe3m42r+SYrLmbjlf7+HB0KSuyVLMVW/2YZWZMMNPxV2BzshFKeG9LxpqM Q0bR8pp59QecXm7F9mS+y9xM7qt+Qq95qQsAKdyNO2rASzxZ338rqmkkbe/xiX8VdHYz Tmjp8N7xKE4XfxQm8XCahbw8g8N3q5nRcVV6OTwfojC3CC7SHv9w28XqBDSA2AqXGFdc dVsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=vbD9T46peKuVuWVXgU4L562EvkcM3pBZQg2XGEQt4sI=; b=WrpW38Dj2siOAasarZU1ZIoFWTW8KX3oDJZx5QxhqSYG/b/JB4G07pywEo/qUypjhU xGx8wCubaDty75w+FXX2ncmyezESjtIh/XQdJ4x4t6I3N+XFDedQeFAOo+Sx6LH2kaTK 2ygvKlAHOlm0wNG05lOZlulcGcQxigOdaLTej0DaQhFu/4xw6LZjyIyqeGH0KL6pOdvn VTZygP9lD6IYlMhLCdWC4Roy5/oIIHNe7PslLyufgaF6gBZB47Xa2CJl4eMheur17jdX IcouFVflDs9oPeroRiGD2IsOsnIprkM7MpnLK1ykdUDquTn1+LAqvnl9pWtelzhcPIyi BGxA== X-Gm-Message-State: AIVw112jjSjJ0Kl2hJvtJbS3949YYFkK9pJ4OsIfLG740XIL+eOXjLO3 vvMBqwZ2EbaDKT3+4pg= X-Received: by 10.55.80.70 with SMTP id e67mr26873368qkb.40.1501623981936; Tue, 01 Aug 2017 14:46:21 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:09 -0400 Message-Id: <20170801214614.8915-9-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 01 Aug 2017 21:46:23 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 01 Aug 2017 21:46:23 +0000 (UTC) for IP:'209.85.220.193' DOMAIN:'mail-qk0-f193.google.com' HELO:'mail-qk0-f193.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.37 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.220.193 mail-qk0-f193.google.com 209.85.220.193 mail-qk0-f193.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.31 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 08/13] Split up the bindings for libvirt network API X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 01 Aug 2017 22:12:34 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" --- src/Makefile.am | 3 +- src/libvirt-network.c | 586 ++++++++++++++++++++++++++++++++++++++++++++++= ++ src/libvirt-network.h | 73 ++++++ src/libvirt-php.c | 610 +---------------------------------------------= ---- src/libvirt-php.h | 24 +- 5 files changed, 664 insertions(+), 632 deletions(-) create mode 100644 src/libvirt-network.c create mode 100644 src/libvirt-network.h diff --git a/src/Makefile.am b/src/Makefile.am index b8eae3a..4ae01db 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -25,7 +25,8 @@ libvirt_php_la_SOURCES =3D \ libvirt-connection.c libvirt-connection.h \ libvirt-node.c libvirt-node.h \ libvirt-stream.c libvirt-stream.h \ - libvirt-domain.c libvirt-domain.h + libvirt-domain.c libvirt-domain.h \ + libvirt-network.c libvirt-network.h=20 libvirt_php_la_CFLAGS =3D \ $(AM_CFLAGS) \ -DCOMPILE_DL_LIBVIRT=3D1 diff --git a/src/libvirt-network.c b/src/libvirt-network.c new file mode 100644 index 0000000..4316ee2 --- /dev/null +++ b/src/libvirt-network.c @@ -0,0 +1,586 @@ +/* + * libvirt-network.c: The PHP bindings to libvirt network API + * + * See COPYING for the license of this software + */ + +#include + +#include "libvirt-network.h" + +DEBUG_INIT("network"); + +void +php_libvirt_network_dtor(virt_resource *rsrc TSRMLS_DC) +{ + php_libvirt_network *network =3D (php_libvirt_network *)rsrc->ptr; + int rv =3D 0; + + if (network !=3D NULL) { + if (network->network !=3D NULL) { + if (!check_resource_allocation(network->conn->conn, INT_RESOUR= CE_NETWORK, network->network TSRMLS_CC)) { + network->network =3D NULL; + efree(network); + return; + } + rv =3D virNetworkFree(network->network); + if (rv !=3D 0) { + DPRINTF("%s: virNetworkFree(%p) returned %d (%s)\n", __FUN= CTION__, network->network, rv, LIBVIRT_G(last_error)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStorageVol= Free failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virNetworkFree(%p) completed successfully\n",= __FUNCTION__, network->network); + resource_change_counter(INT_RESOURCE_NETWORK, NULL, networ= k->network, 0 TSRMLS_CC); + } + network->network =3D NULL; + } + efree(network); + } +} + +/* + * Function name: libvirt_network_define_xml + * Since version: 0.4.2 + * Description: Function is used to define a new virtual network based= on the XML description + * Arguments: @res [resource]: libvirt connection resource + * @xml [string]: XML string definition of network to be = defined + * Returns: libvirt network resource of newly defined network + */ +PHP_FUNCTION(libvirt_network_define_xml) +{ + php_libvirt_connection *conn =3D NULL; + php_libvirt_network *res_net =3D NULL; + virNetwork *net; + zval *zconn; + char *xml =3D NULL; + strsize_t xml_len; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &xml, &xml_len); + + if ((net =3D virNetworkDefineXML(conn->conn, xml)) =3D=3D NULL) { + set_error_if_unset("Cannot define a new network" TSRMLS_CC); + RETURN_FALSE; + } + + res_net =3D (php_libvirt_network *)emalloc(sizeof(php_libvirt_network)= ); + res_net->network =3D net; + res_net->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_net->network); + resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, res_net->net= work, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_net, le_libvirt_network); +} + +/* + * Function name: libvirt_network_get_xml_desc + * Since version: 0.4.1(-1) + * Description: Function is used to get the XML description for the ne= twork + * Arguments: @res [resource]: libvirt network resource + * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL + * Returns: network XML string or result of xPath expression + */ +PHP_FUNCTION(libvirt_network_get_xml_desc) +{ + php_libvirt_network *network; + zval *znetwork; + char *xml =3D NULL; + char *xpath =3D NULL; + char *tmp; + strsize_t xpath_len; + int retval =3D -1; + + GET_NETWORK_FROM_ARGS("r|s", &znetwork, &xpath, &xpath_len); + if (xpath_len < 1) + xpath =3D NULL; + + xml =3D virNetworkGetXMLDesc(network->network, 0); + + if (xml =3D=3D NULL) { + set_error_if_unset("Cannot get network XML" TSRMLS_CC); + RETURN_FALSE; + } + + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + VIRT_RETVAL_STRING(xml); + } else { + VIRT_RETVAL_STRING(tmp); + } + + free(xml); + free(tmp); +} + +/* + * Function name: libvirt_network_undefine + * Since version: 0.4.2 + * Description: Function is used to undefine already defined network + * Arguments: @res [resource]: libvirt network resource + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_network_undefine) +{ + php_libvirt_network *network =3D NULL; + zval *znetwork; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + if (virNetworkUndefine(network->network) !=3D 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_network_get + * Since version: 0.4.1(-1) + * Description: Function is used to get the network resource from name + * Arguments: @res [resource]: libvirt connection resource + * @name [string]: network name string + * Returns: libvirt network resource + */ +PHP_FUNCTION(libvirt_network_get) +{ + php_libvirt_connection *conn =3D NULL; + php_libvirt_network *res_net =3D NULL; + virNetwork *net; + zval *zconn; + char *name; + strsize_t name_len; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); + + if ((net =3D virNetworkLookupByName(conn->conn, name)) =3D=3D NULL) { + set_error_if_unset("Cannot get find requested network" TSRMLS_CC); + RETURN_FALSE; + } + + res_net =3D (php_libvirt_network *)emalloc(sizeof(php_libvirt_network)= ); + res_net->network =3D net; + res_net->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_net->network); + resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, res_net->net= work, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_net, le_libvirt_network); +} + +/* + * Function name: libvirt_network_get_bridge + * Since version: 0.4.1(-1) + * Description: Function is used to get the bridge associated with the= network + * Arguments: @res [resource]: libvirt network resource + * Returns: bridge name string + */ +PHP_FUNCTION(libvirt_network_get_bridge) +{ + php_libvirt_network *network; + zval *znetwork; + char *name; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + name =3D virNetworkGetBridgeName(network->network); + + if (name =3D=3D NULL) { + set_error_if_unset("Cannot get network bridge name" TSRMLS_CC); + RETURN_FALSE; + } + + VIRT_RETVAL_STRING(name); + free(name); +} + +/* + * Function name: libvirt_network_get_active + * Since version: 0.4.1(-1) + * Description: Function is used to get the activity state of the netw= ork + * Arguments: @res [resource]: libvirt network resource + * Returns: 1 when active, 0 when inactive, FALSE on error + */ +PHP_FUNCTION(libvirt_network_get_active) +{ + php_libvirt_network *network; + zval *znetwork; + int res; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + res =3D virNetworkIsActive(network->network); + + if (res =3D=3D -1) { + set_error_if_unset("Error getting virtual network state" TSRMLS_CC= ); + RETURN_FALSE; + } + + RETURN_LONG(res); +} + +/* + * Function name: libvirt_network_set_active + * Since version: 0.4.1(-1) + * Description: Function is used to set the activity state of the netw= ork + * Arguments: @res [resource]: libvirt network resource + * @flags [int]: active + * Returns: TRUE if success, FALSE on error + */ +PHP_FUNCTION(libvirt_network_set_active) +{ + php_libvirt_network *network; + zval *znetwork; + zend_long act =3D 0; + + DPRINTF("%s: Setting network activity...\n", PHPFUNC); + + GET_NETWORK_FROM_ARGS("rl", &znetwork, &act); + + if ((act !=3D 0) && (act !=3D 1)) { + set_error("Invalid network activity state" TSRMLS_CC); + RETURN_FALSE; + } + + DPRINTF("%s: %sabling network...\n", PHPFUNC, (act =3D=3D 1) ? "En" : = "Dis"); + + if (act =3D=3D 1) { + if (virNetworkCreate(network->network) =3D=3D 0) { + // Network is up and running + RETURN_TRUE; + } else { + // We don't have to set error since it's caught by libvirt err= or handler itself + RETURN_FALSE; + } + } + + if (virNetworkDestroy(network->network) =3D=3D 0) { + // Network is down + RETURN_TRUE; + } else { + // Caught by libvirt error handler too + RETURN_FALSE; + } +} + +/* + * Function name: libvirt_network_get_information + * Since version: 0.4.1(-1) + * Description: Function is used to get the network information + * Arguments: @res [resource]: libvirt network resource + * Returns: network information array + */ +PHP_FUNCTION(libvirt_network_get_information) +{ + php_libvirt_network *network =3D NULL; + zval *znetwork; + int retval =3D 0; + char *xml =3D NULL; + char *name =3D NULL; + char *ipaddr =3D NULL; + char *netmask =3D NULL; + char *mode =3D NULL; + char *dev =3D NULL; + char *dhcp_start =3D NULL; + char *dhcp_end =3D NULL; + char fixedtemp[32] =3D { 0 }; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + xml =3D virNetworkGetXMLDesc(network->network, 0); + + if (xml =3D=3D NULL) { + set_error_if_unset("Cannot get network XML" TSRMLS_CC); + RETURN_FALSE; + } + + array_init(return_value); + + /* Get name */ + name =3D get_string_from_xpath(xml, "//network/name", NULL, &retval); + if (name =3D=3D NULL) { + set_error("Invalid XPath node for network name" TSRMLS_CC); + RETURN_FALSE; + } + + if (retval < 0) { + set_error("Cannot get XPath expression result for network name" TS= RMLS_CC); + RETURN_FALSE; + } + + VIRT_ADD_ASSOC_STRING(return_value, "name", name); + + /* Get gateway IP address */ + ipaddr =3D get_string_from_xpath(xml, "//network/ip/@address", NULL, &= retval); + if (ipaddr && retval > 0) + VIRT_ADD_ASSOC_STRING(return_value, "ip", ipaddr); + + /* Get netmask */ + netmask =3D get_string_from_xpath(xml, "//network/ip/@netmask", NULL, = &retval); + if (netmask && retval > 0) { + int subnet_bits =3D get_subnet_bits(netmask); + VIRT_ADD_ASSOC_STRING(return_value, "netmask", netmask); + add_assoc_long(return_value, "netmask_bits", (long) subnet_bits); + + /* Format CIDR address representation */ + ipaddr[strlen(ipaddr) - 1] =3D ipaddr[strlen(ipaddr) - 1] - 1; + snprintf(fixedtemp, sizeof(fixedtemp), "%s/%d", ipaddr, subnet_bit= s); + VIRT_ADD_ASSOC_STRING(return_value, "ip_range", fixedtemp); + } + + /* Get forwarding settings */ + mode =3D get_string_from_xpath(xml, "//network/forward/@mode", NULL, &= retval); + if (mode && retval > 0) + VIRT_ADD_ASSOC_STRING(return_value, "forwarding", mode); + + /* Get forwarding settings */ + dev =3D get_string_from_xpath(xml, "//network/forward/@dev", NULL, &re= tval); + if (dev && retval > 0) + VIRT_ADD_ASSOC_STRING(return_value, "forward_dev", dev); + + /* Get DHCP values */ + dhcp_start =3D get_string_from_xpath(xml, "//network/ip/dhcp/range/@st= art", NULL, &retval); + dhcp_end =3D get_string_from_xpath(xml, "//network/ip/dhcp/range/@end"= , NULL, &retval); + if (dhcp_start && dhcp_end && retval > 0) { + VIRT_ADD_ASSOC_STRING(return_value, "dhcp_start", dhcp_start); + VIRT_ADD_ASSOC_STRING(return_value, "dhcp_end", dhcp_end); + } + + free(dhcp_end); + free(dhcp_start); + free(dev); + free(mode); + free(netmask); + free(ipaddr); + free(name); + free(xml); +} + +/* + * Function name: libvirt_network_get_uuid_string + * Since version: 0.5.3 + * Description: Function is used to get network's UUID in string format + * Arguments: @res [resource]: libvirt network resource + * Returns: network UUID string or FALSE on failure + */ +PHP_FUNCTION(libvirt_network_get_uuid_string) +{ + php_libvirt_network *network =3D NULL; + zval *znetwork; + char *uuid =3D NULL; + int ret =3D -1; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + uuid =3D (char *) emalloc(VIR_UUID_STRING_BUFLEN); + ret =3D virNetworkGetUUIDString(network->network, uuid); + + DPRINTF("%s: virNetworkGetUUIDString(%p) returned %d (%s)\n", PHPFUNC, + network->network, ret, uuid); + + if (ret !=3D 0) + RETURN_FALSE; + + VIRT_RETURN_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_network_get_uuid + * Since version: 0.5.3 + * Descirption: Function is used to get network's UUID in binary format + * Arguments: @res [resource]: libvirt netowrk resource + * Returns: network UUID in binary format or FALSE on failure + */ +PHP_FUNCTION(libvirt_network_get_uuid) +{ + php_libvirt_network *network =3D NULL; + zval *znetwork; + char *uuid =3D NULL; + int ret =3D -1; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + uuid =3D (char *) emalloc(VIR_UUID_BUFLEN); + ret =3D virNetworkGetUUID(network->network, (unsigned char *)uuid); + + DPRINTF("%s: virNetworkGetUUID(%p, %p) returned %d\n", PHPFUNC, + network->network, uuid, ret); + + if (ret !=3D 0) + RETURN_FALSE; + + VIRT_RETVAL_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_network_get_name + * Since version: 0.5.3 + * Description: Function is used to get network's name + * Arguments: @res [resource]: libvirt network resource + * Returns: network name string or FALSE on failure + */ +PHP_FUNCTION(libvirt_network_get_name) +{ + php_libvirt_network *network =3D NULL; + zval *znetwork; + const char *name =3D NULL; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + name =3D virNetworkGetName(network->network); + + DPRINTF("%s: virNetworkGetName(%p) returned %s\n", PHPFUNC, + network->network, name); + + if (name =3D=3D NULL) + RETURN_FALSE; + + /* name should not be freed as its lifetime is the same as network res= ource */ + VIRT_RETURN_STRING(name); +} + +/* + * Function name: libvirt_network_get_autostart + * Since version: 0.5.4 + * Description: Function is getting the autostart value for the network + * Arguments: @res [resource]: libvirt network resource + * Returns: autostart value or -1 on error + */ +PHP_FUNCTION(libvirt_network_get_autostart) +{ + php_libvirt_network *network =3D NULL; + zval *znetwork; + int autostart; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + if (virNetworkGetAutostart(network->network, &autostart) !=3D 0) + RETURN_LONG(-1); + + RETURN_LONG((long) autostart); +} + +/* + * Function name: libvirt_network_set_autostart + * Since version: 0.5.4 + * Description: Function is setting the autostart value for the network + * Arguments: @res [resource]: libvirt network resource + * @flags [int]: flag to enable/disable autostart + * Returns: TRUE on success, FALSE on error + */ +PHP_FUNCTION(libvirt_network_set_autostart) +{ + php_libvirt_network *network =3D NULL; + zval *znetwork; + zend_long autostart =3D 0; + + GET_NETWORK_FROM_ARGS("rl", &znetwork, &autostart); + + if (virNetworkSetAutostart(network->network, autostart) < 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_list_all_networks + * Since version: 0.5.3 + * Description: Function is used to list networks on the connection + * Arguments: @res [resource]: libvirt connection resource + * @flags [int]: optional flags to filter the results for= a smaller list of targetted networks (bitwise-OR VIR_CONNECT_LIST_NETWORKS= _* constants) + * Returns: libvirt network resources array for the connection + */ +PHP_FUNCTION(libvirt_list_all_networks) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + zend_long flags =3D VIR_CONNECT_LIST_NETWORKS_ACTIVE | + VIR_CONNECT_LIST_NETWORKS_INACTIVE; + int count =3D -1; + size_t i =3D 0; + virNetworkPtr *nets =3D NULL; + virNetworkPtr network =3D NULL; + php_libvirt_network *res_network; + + GET_CONNECTION_FROM_ARGS("r|l", &zconn, &flags); + + if ((count =3D virConnectListAllNetworks(conn->conn, &nets, flags)) < = 0) + RETURN_FALSE; + + DPRINTF("%s: Found %d networks\n", PHPFUNC, count); + + array_init(return_value); + + for (i =3D 0; i < count; i++) { + network =3D nets[i]; + res_network =3D (php_libvirt_network *) emalloc(sizeof(php_libvirt= _network)); + res_network->network =3D network; + res_network->conn =3D conn; + + VIRT_REGISTER_LIST_RESOURCE(network); + resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, + res_network->network, 1 TSRMLS_CC); + } +} + +/* + * Function name: libvirt_list_networks + * Since version: 0.4.1(-1) + * Description: Function is used to list networks on the connection + * Arguments: @res [resource]: libvirt connection resource + * @flags [int]: flags whether to list active, inactive o= r all networks (VIR_NETWORKS_{ACTIVE|INACTIVE|ALL} constants) + * Returns: libvirt network names array for the connection + */ +PHP_FUNCTION(libvirt_list_networks) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + zend_long flags =3D VIR_NETWORKS_ACTIVE | VIR_NETWORKS_INACTIVE; + int count =3D -1; + int expectedcount =3D -1; + char **names; + int i, done =3D 0; + + GET_CONNECTION_FROM_ARGS("r|l", &zconn, &flags); + + array_init(return_value); + if (flags & VIR_NETWORKS_ACTIVE) { + if ((expectedcount =3D virConnectNumOfNetworks(conn->conn)) < 0) + RETURN_FALSE; + + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virConnectListNetworks(conn->conn, names, expectedcount); + if ((count !=3D expectedcount) || (count < 0)) { + efree(names); + RETURN_FALSE; + } + + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + + efree(names); + done++; + } + + if (flags & VIR_NETWORKS_INACTIVE) { + if ((expectedcount =3D virConnectNumOfDefinedNetworks(conn->conn))= < 0) + RETURN_FALSE; + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virConnectListDefinedNetworks(conn->conn, names, expecte= dcount); + if ((count !=3D expectedcount) || (count < 0)) { + efree(names); + RETURN_FALSE; + } + + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + + efree(names); + done++; + } + + if (!done) + RETURN_FALSE; +} diff --git a/src/libvirt-network.h b/src/libvirt-network.h new file mode 100644 index 0000000..604b803 --- /dev/null +++ b/src/libvirt-network.h @@ -0,0 +1,73 @@ +/* + * libvirt-network.h: The PHP bindings to libvirt network API + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_NETWORK_H__ +# define __LIBVIRT_NETWORK_H__ + +# include "libvirt-connection.h" + +# define PHP_LIBVIRT_NETWORK_RES_NAME "Libvirt virtual network" +# define INT_RESOURCE_NETWORK 0x04 + +# define GET_NETWORK_FROM_ARGS(args, ...) = \ + do { = \ + reset_error(TSRMLS_C); = \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, = \ + args, = \ + __VA_ARGS__) =3D=3D FAILURE) { = \ + set_error("Invalid arguments" TSRMLS_CC); = \ + RETURN_FALSE; = \ + } = \ + = \ + VIRT_FETCH_RESOURCE(network, php_libvirt_network*, &znetwork, = \ + PHP_LIBVIRT_NETWORK_RES_NAME, le_libvirt_netwo= rk); \ + if (network =3D=3D NULL || network->network =3D=3D NULL) = \ + RETURN_FALSE; = \ + } while (0) = \ + +# define PHP_FE_LIBVIRT_NETWORK = \ + PHP_FE(libvirt_network_define_xml, arginfo_libvirt_conn_xml) = \ + PHP_FE(libvirt_network_get_xml_desc, arginfo_libvirt_conn_xpath) = \ + PHP_FE(libvirt_network_undefine, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_get, arginfo_libvirt_conn_name) = \ + PHP_FE(libvirt_network_get_active, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_set_active, arginfo_libvirt_conn_flags) = \ + PHP_FE(libvirt_network_get_bridge, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_get_information, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_get_uuid_string, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_get_uuid, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_get_name, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_get_autostart, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_set_autostart, arginfo_libvirt_conn_flags) = \ + PHP_FE(libvirt_list_all_networks, arginfo_libvirt_conn_optflags)= \ + PHP_FE(libvirt_list_networks, arginfo_libvirt_conn_optflags) + +int le_libvirt_network; + +typedef struct _php_libvirt_network { + virNetworkPtr network; + php_libvirt_connection* conn; +} php_libvirt_network; + +void php_libvirt_network_dtor(virt_resource *rsrc TSRMLS_DC); + +PHP_FUNCTION(libvirt_network_define_xml); +PHP_FUNCTION(libvirt_network_get_xml_desc); +PHP_FUNCTION(libvirt_network_undefine); +PHP_FUNCTION(libvirt_network_get); +PHP_FUNCTION(libvirt_network_get_active); +PHP_FUNCTION(libvirt_network_set_active); +PHP_FUNCTION(libvirt_network_get_bridge); +PHP_FUNCTION(libvirt_network_get_information); +PHP_FUNCTION(libvirt_network_get_uuid_string); +PHP_FUNCTION(libvirt_network_get_uuid); +PHP_FUNCTION(libvirt_network_get_name); +PHP_FUNCTION(libvirt_network_get_autostart); +PHP_FUNCTION(libvirt_network_set_autostart); +PHP_FUNCTION(libvirt_list_all_networks); +PHP_FUNCTION(libvirt_list_networks); + +#endif diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 05f36ae..9e43a71 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -24,6 +24,7 @@ #include "libvirt-node.h" #include "libvirt-stream.h" #include "libvirt-domain.h" +#include "libvirt-network.h" =20 DEBUG_INIT("core"); =20 @@ -39,7 +40,6 @@ const char *features_binaries[] =3D { NULL }; /* ZEND thread safe per request globals definition */ int le_libvirt_storagepool; int le_libvirt_volume; -int le_libvirt_network; int le_libvirt_nodedev; int le_libvirt_snapshot; int le_libvirt_nwfilter; @@ -517,20 +517,7 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE(libvirt_storagepool_build, arginfo_libvirt_conn) PHP_FE(libvirt_storagepool_delete, arginfo_libvirt_conn) /* Network functions */ - PHP_FE(libvirt_network_define_xml, arginfo_libvirt_conn_xml) - PHP_FE(libvirt_network_undefine, arginfo_libvirt_conn) - PHP_FE(libvirt_network_get, arginfo_libvirt_conn_name) - PHP_FE(libvirt_network_get_xml_desc, arginfo_libvirt_conn_xpat= h) - PHP_FE(libvirt_network_get_bridge, arginfo_libvirt_conn) - PHP_FE(libvirt_network_get_information, arginfo_libvirt_conn) - PHP_FE(libvirt_network_get_active, arginfo_libvirt_conn) - PHP_FE(libvirt_network_set_active, arginfo_libvirt_conn_flag= s) - PHP_FE(libvirt_network_get_uuid_string, arginfo_libvirt_conn) - PHP_FE(libvirt_network_get_uuid, arginfo_libvirt_conn) - PHP_FE(libvirt_network_get_name, arginfo_libvirt_conn) - PHP_FE(libvirt_network_get_autostart, arginfo_libvirt_conn) - PHP_FE(libvirt_network_set_autostart, arginfo_libvirt_conn_flag= s) - /* Node functions */ + PHP_FE_LIBVIRT_NETWORK PHP_FE_LIBVIRT_NODE /* Nodedev functions */ PHP_FE(libvirt_nodedev_get, arginfo_libvirt_conn) @@ -550,8 +537,6 @@ static zend_function_entry libvirt_functions[] =3D { /* List functions */ PHP_FE(libvirt_list_domain_snapshots, arginfo_libvirt_conn_optf= lags) PHP_FE(libvirt_list_nodedevs, arginfo_libvirt_conn_optc= ap) - PHP_FE(libvirt_list_all_networks, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_list_networks, arginfo_libvirt_conn_optf= lags) PHP_FE(libvirt_list_storagepools, arginfo_libvirt_conn) PHP_FE(libvirt_list_active_storagepools, arginfo_libvirt_conn) PHP_FE(libvirt_list_inactive_storagepools, arginfo_libvirt_conn) @@ -571,7 +556,6 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE_END }; =20 - /* Zend module basic definition */ zend_module_entry libvirt_module_entry =3D { #if ZEND_MODULE_API_NO >=3D 20010901 @@ -1293,33 +1277,6 @@ static void php_libvirt_volume_dtor(virt_resource *r= src TSRMLS_DC) } } =20 -/* Destructor for network resource */ -static void php_libvirt_network_dtor(virt_resource *rsrc TSRMLS_DC) -{ - php_libvirt_network *network =3D (php_libvirt_network *)rsrc->ptr; - int rv =3D 0; - - if (network !=3D NULL) { - if (network->network !=3D NULL) { - if (!check_resource_allocation(network->conn->conn, INT_RESOUR= CE_NETWORK, network->network TSRMLS_CC)) { - network->network =3D NULL; - efree(network); - return; - } - rv =3D virNetworkFree(network->network); - if (rv !=3D 0) { - DPRINTF("%s: virNetworkFree(%p) returned %d (%s)\n", __FUN= CTION__, network->network, rv, LIBVIRT_G(last_error)); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStorageVol= Free failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); - } else { - DPRINTF("%s: virNetworkFree(%p) completed successfully\n",= __FUNCTION__, network->network); - resource_change_counter(INT_RESOURCE_NETWORK, NULL, networ= k->network, 0 TSRMLS_CC); - } - network->network =3D NULL; - } - efree(network); - } -} - /* Destructor for nodedev resource */ static void php_libvirt_nodedev_dtor(virt_resource *rsrc TSRMLS_DC) { @@ -1727,19 +1684,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) } =20 /* Macros for obtaining resources from arguments */ -#define GET_NETWORK_FROM_ARGS(args, ...) = \ - do { = \ - reset_error(TSRMLS_C); = \ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_AR= GS__) =3D=3D FAILURE) { \ - set_error("Invalid arguments" TSRMLS_CC); = \ - RETURN_FALSE; = \ - } = \ - = \ - VIRT_FETCH_RESOURCE(network, php_libvirt_network*, &znetwork, PHP_= LIBVIRT_NETWORK_RES_NAME, le_libvirt_network);\ - if ((network =3D=3D NULL) || (network->network =3D=3D NULL)) = \ - RETURN_FALSE; = \ - } while (0) = \ - #define GET_NODEDEV_FROM_ARGS(args, ...) = \ do { = \ reset_error(TSRMLS_C); = \ @@ -4054,111 +3998,6 @@ PHP_FUNCTION(libvirt_list_inactive_storagepools) } =20 /* - * Function name: libvirt_list_all_networks - * Since version: 0.5.3 - * Description: Function is used to list networks on the connection - * Arguments: @res [resource]: libvirt connection resource - * @flags [int]: optional flags to filter the results for= a smaller list of targetted networks (bitwise-OR VIR_CONNECT_LIST_NETWORKS= _* constants) - * Returns: libvirt network resources array for the connection - */ -PHP_FUNCTION(libvirt_list_all_networks) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - zend_long flags =3D VIR_CONNECT_LIST_NETWORKS_ACTIVE | - VIR_CONNECT_LIST_NETWORKS_INACTIVE; - int count =3D -1; - size_t i =3D 0; - virNetworkPtr *nets =3D NULL; - virNetworkPtr network =3D NULL; - php_libvirt_network *res_network; - - GET_CONNECTION_FROM_ARGS("r|l", &zconn, &flags); - - if ((count =3D virConnectListAllNetworks(conn->conn, &nets, flags)) < = 0) - RETURN_FALSE; - - DPRINTF("%s: Found %d networks\n", PHPFUNC, count); - - array_init(return_value); - - for (i =3D 0; i < count; i++) { - network =3D nets[i]; - res_network =3D (php_libvirt_network *) emalloc(sizeof(php_libvirt= _network)); - res_network->network =3D network; - res_network->conn =3D conn; - - VIRT_REGISTER_LIST_RESOURCE(network); - resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, - res_network->network, 1 TSRMLS_CC); - } -} - -/* - * Function name: libvirt_list_networks - * Since version: 0.4.1(-1) - * Description: Function is used to list networks on the connection - * Arguments: @res [resource]: libvirt connection resource - * @flags [int]: flags whether to list active, inactive o= r all networks (VIR_NETWORKS_{ACTIVE|INACTIVE|ALL} constants) - * Returns: libvirt network names array for the connection - */ -PHP_FUNCTION(libvirt_list_networks) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - zend_long flags =3D VIR_NETWORKS_ACTIVE | VIR_NETWORKS_INACTIVE; - int count =3D -1; - int expectedcount =3D -1; - char **names; - int i, done =3D 0; - - GET_CONNECTION_FROM_ARGS("r|l", &zconn, &flags); - - array_init(return_value); - if (flags & VIR_NETWORKS_ACTIVE) { - if ((expectedcount =3D virConnectNumOfNetworks(conn->conn)) < 0) - RETURN_FALSE; - - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virConnectListNetworks(conn->conn, names, expectedcount); - if ((count !=3D expectedcount) || (count < 0)) { - efree(names); - RETURN_FALSE; - } - - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - - efree(names); - done++; - } - - if (flags & VIR_NETWORKS_INACTIVE) { - if ((expectedcount =3D virConnectNumOfDefinedNetworks(conn->conn))= < 0) - RETURN_FALSE; - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virConnectListDefinedNetworks(conn->conn, names, expecte= dcount); - if ((count !=3D expectedcount) || (count < 0)) { - efree(names); - RETURN_FALSE; - } - - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - - efree(names); - done++; - } - - if (!done) - RETURN_FALSE; -} - -/* * Function name: libvirt_list_nodedevs * Since version: 0.4.1(-1) * Description: Function is used to list node devices on the connection @@ -4546,451 +4385,6 @@ PHP_FUNCTION(libvirt_nodedev_get_information) RETURN_FALSE; } =20 -/* Network functions */ - -/* - * Function name: libvirt_network_define_xml - * Since version: 0.4.2 - * Description: Function is used to define a new virtual network based= on the XML description - * Arguments: @res [resource]: libvirt connection resource - * @xml [string]: XML string definition of network to be = defined - * Returns: libvirt network resource of newly defined network - */ -PHP_FUNCTION(libvirt_network_define_xml) -{ - php_libvirt_connection *conn =3D NULL; - php_libvirt_network *res_net =3D NULL; - virNetwork *net; - zval *zconn; - char *xml =3D NULL; - strsize_t xml_len; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &xml, &xml_len); - - if ((net =3D virNetworkDefineXML(conn->conn, xml)) =3D=3D NULL) { - set_error_if_unset("Cannot define a new network" TSRMLS_CC); - RETURN_FALSE; - } - - res_net =3D (php_libvirt_network *)emalloc(sizeof(php_libvirt_network)= ); - res_net->network =3D net; - res_net->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_net->network); - resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, res_net->net= work, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_net, le_libvirt_network); -} - -/* - * Function name: libvirt_network_undefine - * Since version: 0.4.2 - * Description: Function is used to undefine already defined network - * Arguments: @res [resource]: libvirt network resource - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_network_undefine) -{ - php_libvirt_network *network =3D NULL; - zval *znetwork; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - - if (virNetworkUndefine(network->network) !=3D 0) - RETURN_FALSE; - - RETURN_TRUE; -} - -/* - * Function name: libvirt_network_get - * Since version: 0.4.1(-1) - * Description: Function is used to get the network resource from name - * Arguments: @res [resource]: libvirt connection resource - * @name [string]: network name string - * Returns: libvirt network resource - */ -PHP_FUNCTION(libvirt_network_get) -{ - php_libvirt_connection *conn =3D NULL; - php_libvirt_network *res_net =3D NULL; - virNetwork *net; - zval *zconn; - char *name; - strsize_t name_len; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); - - if ((net =3D virNetworkLookupByName(conn->conn, name)) =3D=3D NULL) { - set_error_if_unset("Cannot get find requested network" TSRMLS_CC); - RETURN_FALSE; - } - - res_net =3D (php_libvirt_network *)emalloc(sizeof(php_libvirt_network)= ); - res_net->network =3D net; - res_net->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_net->network); - resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, res_net->net= work, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_net, le_libvirt_network); -} - -/* - * Function name: libvirt_network_get_bridge - * Since version: 0.4.1(-1) - * Description: Function is used to get the bridge associated with the= network - * Arguments: @res [resource]: libvirt network resource - * Returns: bridge name string - */ -PHP_FUNCTION(libvirt_network_get_bridge) -{ - php_libvirt_network *network; - zval *znetwork; - char *name; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - - name =3D virNetworkGetBridgeName(network->network); - - if (name =3D=3D NULL) { - set_error_if_unset("Cannot get network bridge name" TSRMLS_CC); - RETURN_FALSE; - } - - VIRT_RETVAL_STRING(name); - free(name); -} - -/* - * Function name: libvirt_network_get_active - * Since version: 0.4.1(-1) - * Description: Function is used to get the activity state of the netw= ork - * Arguments: @res [resource]: libvirt network resource - * Returns: 1 when active, 0 when inactive, FALSE on error - */ -PHP_FUNCTION(libvirt_network_get_active) -{ - php_libvirt_network *network; - zval *znetwork; - int res; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - - res =3D virNetworkIsActive(network->network); - - if (res =3D=3D -1) { - set_error_if_unset("Error getting virtual network state" TSRMLS_CC= ); - RETURN_FALSE; - } - - RETURN_LONG(res); -} - -/* - * Function name: libvirt_network_get_information - * Since version: 0.4.1(-1) - * Description: Function is used to get the network information - * Arguments: @res [resource]: libvirt network resource - * Returns: network information array - */ -PHP_FUNCTION(libvirt_network_get_information) -{ - php_libvirt_network *network =3D NULL; - zval *znetwork; - int retval =3D 0; - char *xml =3D NULL; - char *name =3D NULL; - char *ipaddr =3D NULL; - char *netmask =3D NULL; - char *mode =3D NULL; - char *dev =3D NULL; - char *dhcp_start =3D NULL; - char *dhcp_end =3D NULL; - char fixedtemp[32] =3D { 0 }; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - - xml =3D virNetworkGetXMLDesc(network->network, 0); - - if (xml =3D=3D NULL) { - set_error_if_unset("Cannot get network XML" TSRMLS_CC); - RETURN_FALSE; - } - - array_init(return_value); - - /* Get name */ - name =3D get_string_from_xpath(xml, "//network/name", NULL, &retval); - if (name =3D=3D NULL) { - set_error("Invalid XPath node for network name" TSRMLS_CC); - RETURN_FALSE; - } - - if (retval < 0) { - set_error("Cannot get XPath expression result for network name" TS= RMLS_CC); - RETURN_FALSE; - } - - VIRT_ADD_ASSOC_STRING(return_value, "name", name); - - /* Get gateway IP address */ - ipaddr =3D get_string_from_xpath(xml, "//network/ip/@address", NULL, &= retval); - if (ipaddr && retval > 0) - VIRT_ADD_ASSOC_STRING(return_value, "ip", ipaddr); - - /* Get netmask */ - netmask =3D get_string_from_xpath(xml, "//network/ip/@netmask", NULL, = &retval); - if (netmask && retval > 0) { - int subnet_bits =3D get_subnet_bits(netmask); - VIRT_ADD_ASSOC_STRING(return_value, "netmask", netmask); - add_assoc_long(return_value, "netmask_bits", (long) subnet_bits); - - /* Format CIDR address representation */ - ipaddr[strlen(ipaddr) - 1] =3D ipaddr[strlen(ipaddr) - 1] - 1; - snprintf(fixedtemp, sizeof(fixedtemp), "%s/%d", ipaddr, subnet_bit= s); - VIRT_ADD_ASSOC_STRING(return_value, "ip_range", fixedtemp); - } - - /* Get forwarding settings */ - mode =3D get_string_from_xpath(xml, "//network/forward/@mode", NULL, &= retval); - if (mode && retval > 0) - VIRT_ADD_ASSOC_STRING(return_value, "forwarding", mode); - - /* Get forwarding settings */ - dev =3D get_string_from_xpath(xml, "//network/forward/@dev", NULL, &re= tval); - if (dev && retval > 0) - VIRT_ADD_ASSOC_STRING(return_value, "forward_dev", dev); - - /* Get DHCP values */ - dhcp_start =3D get_string_from_xpath(xml, "//network/ip/dhcp/range/@st= art", NULL, &retval); - dhcp_end =3D get_string_from_xpath(xml, "//network/ip/dhcp/range/@end"= , NULL, &retval); - if (dhcp_start && dhcp_end && retval > 0) { - VIRT_ADD_ASSOC_STRING(return_value, "dhcp_start", dhcp_start); - VIRT_ADD_ASSOC_STRING(return_value, "dhcp_end", dhcp_end); - } - - free(dhcp_end); - free(dhcp_start); - free(dev); - free(mode); - free(netmask); - free(ipaddr); - free(name); - free(xml); -} - -/* - * Function name: libvirt_network_set_active - * Since version: 0.4.1(-1) - * Description: Function is used to set the activity state of the netw= ork - * Arguments: @res [resource]: libvirt network resource - * @flags [int]: active - * Returns: TRUE if success, FALSE on error - */ -PHP_FUNCTION(libvirt_network_set_active) -{ - php_libvirt_network *network; - zval *znetwork; - zend_long act =3D 0; - - DPRINTF("%s: Setting network activity...\n", PHPFUNC); - - GET_NETWORK_FROM_ARGS("rl", &znetwork, &act); - - if ((act !=3D 0) && (act !=3D 1)) { - set_error("Invalid network activity state" TSRMLS_CC); - RETURN_FALSE; - } - - DPRINTF("%s: %sabling network...\n", PHPFUNC, (act =3D=3D 1) ? "En" : = "Dis"); - - if (act =3D=3D 1) { - if (virNetworkCreate(network->network) =3D=3D 0) { - // Network is up and running - RETURN_TRUE; - } else { - // We don't have to set error since it's caught by libvirt err= or handler itself - RETURN_FALSE; - } - } - - if (virNetworkDestroy(network->network) =3D=3D 0) { - // Network is down - RETURN_TRUE; - } else { - // Caught by libvirt error handler too - RETURN_FALSE; - } -} - -/* - * Function name: libvirt_network_get_xml_desc - * Since version: 0.4.1(-1) - * Description: Function is used to get the XML description for the ne= twork - * Arguments: @res [resource]: libvirt network resource - * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL - * Returns: network XML string or result of xPath expression - */ -PHP_FUNCTION(libvirt_network_get_xml_desc) -{ - php_libvirt_network *network; - zval *znetwork; - char *xml =3D NULL; - char *xpath =3D NULL; - char *tmp; - strsize_t xpath_len; - int retval =3D -1; - - GET_NETWORK_FROM_ARGS("r|s", &znetwork, &xpath, &xpath_len); - if (xpath_len < 1) - xpath =3D NULL; - - xml =3D virNetworkGetXMLDesc(network->network, 0); - - if (xml =3D=3D NULL) { - set_error_if_unset("Cannot get network XML" TSRMLS_CC); - RETURN_FALSE; - } - - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - VIRT_RETVAL_STRING(xml); - } else { - VIRT_RETVAL_STRING(tmp); - } - - free(xml); - free(tmp); -} - -/* - * Function name: libvirt_network_get_uuid_string - * Since version: 0.5.3 - * Description: Function is used to get network's UUID in string format - * Arguments: @res [resource]: libvirt network resource - * Returns: network UUID string or FALSE on failure - */ -PHP_FUNCTION(libvirt_network_get_uuid_string) -{ - php_libvirt_network *network =3D NULL; - zval *znetwork; - char *uuid =3D NULL; - int ret =3D -1; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - - uuid =3D (char *) emalloc(VIR_UUID_STRING_BUFLEN); - ret =3D virNetworkGetUUIDString(network->network, uuid); - - DPRINTF("%s: virNetworkGetUUIDString(%p) returned %d (%s)\n", PHPFUNC, - network->network, ret, uuid); - - if (ret !=3D 0) - RETURN_FALSE; - - VIRT_RETURN_STRING(uuid); - efree(uuid); -} - -/* - * Function name: libvirt_network_get_uuid - * Since version: 0.5.3 - * Descirption: Function is used to get network's UUID in binary format - * Arguments: @res [resource]: libvirt netowrk resource - * Returns: network UUID in binary format or FALSE on failure - */ -PHP_FUNCTION(libvirt_network_get_uuid) -{ - php_libvirt_network *network =3D NULL; - zval *znetwork; - char *uuid =3D NULL; - int ret =3D -1; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - - uuid =3D (char *) emalloc(VIR_UUID_BUFLEN); - ret =3D virNetworkGetUUID(network->network, (unsigned char *)uuid); - - DPRINTF("%s: virNetworkGetUUID(%p, %p) returned %d\n", PHPFUNC, - network->network, uuid, ret); - - if (ret !=3D 0) - RETURN_FALSE; - - VIRT_RETVAL_STRING(uuid); - efree(uuid); -} - -/* - * Function name: libvirt_network_get_name - * Since version: 0.5.3 - * Description: Function is used to get network's name - * Arguments: @res [resource]: libvirt network resource - * Returns: network name string or FALSE on failure - */ -PHP_FUNCTION(libvirt_network_get_name) -{ - php_libvirt_network *network =3D NULL; - zval *znetwork; - const char *name =3D NULL; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - name =3D virNetworkGetName(network->network); - - DPRINTF("%s: virNetworkGetName(%p) returned %s\n", PHPFUNC, - network->network, name); - - if (name =3D=3D NULL) - RETURN_FALSE; - - /* name should not be freed as its lifetime is the same as network res= ource */ - VIRT_RETURN_STRING(name); -} - -/* - * Function name: libvirt_network_get_autostart - * Since version: 0.5.4 - * Description: Function is getting the autostart value for the network - * Arguments: @res [resource]: libvirt network resource - * Returns: autostart value or -1 on error - */ -PHP_FUNCTION(libvirt_network_get_autostart) -{ - php_libvirt_network *network =3D NULL; - zval *znetwork; - int autostart; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - - if (virNetworkGetAutostart(network->network, &autostart) !=3D 0) - RETURN_LONG(-1); - - RETURN_LONG((long) autostart); -} - -/* - * Function name: libvirt_network_set_autostart - * Since version: 0.5.4 - * Description: Function is setting the autostart value for the network - * Arguments: @res [resource]: libvirt network resource - * @flags [int]: flag to enable/disable autostart - * Returns: TRUE on success, FALSE on error - */ -PHP_FUNCTION(libvirt_network_set_autostart) -{ - php_libvirt_network *network =3D NULL; - zval *znetwork; - zend_long autostart =3D 0; - - GET_NETWORK_FROM_ARGS("rl", &znetwork, &autostart); - - if (virNetworkSetAutostart(network->network, autostart) < 0) - RETURN_FALSE; - - RETURN_TRUE; -} - /* NWFilter functions */ =20 /* diff --git a/src/libvirt-php.h b/src/libvirt-php.h index a27ed82..7670582 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -128,7 +128,6 @@ typedef uint64_t arch_uint; #define PHP_LIBVIRT_WORLD_EXTNAME "libvirt" =20 /* Internal resource identifier objects */ -#define INT_RESOURCE_NETWORK 0x04 #define INT_RESOURCE_NODEDEV 0x08 #define INT_RESOURCE_STORAGEPOOL 0x10 #define INT_RESOURCE_VOLUME 0x20 @@ -172,11 +171,6 @@ typedef struct _php_libvirt_snapshot { php_libvirt_domain* domain; } php_libvirt_snapshot; =20 -typedef struct _php_libvirt_network { - virNetworkPtr network; - php_libvirt_connection* conn; -} php_libvirt_network; - typedef struct _php_libvirt_nodedev { virNodeDevicePtr device; php_libvirt_connection* conn; @@ -247,10 +241,10 @@ int streamSink(virStreamPtr st ATTRIBUTE_UNUSED, const char *bytes, size_t nbytes, void *opaque); const char *get_feature_binary(const char *name); long get_next_free_numeric_value(virDomainPtr domain, char *xpath); +int get_subnet_bits(char *ip); =20 #define PHP_LIBVIRT_STORAGEPOOL_RES_NAME "Libvirt storagepool" #define PHP_LIBVIRT_VOLUME_RES_NAME "Libvirt volume" -#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" @@ -302,20 +296,6 @@ PHP_FUNCTION(libvirt_storagepool_set_autostart); PHP_FUNCTION(libvirt_storagepool_get_autostart); PHP_FUNCTION(libvirt_storagepool_build); PHP_FUNCTION(libvirt_storagepool_delete); -/* Network functions */ -PHP_FUNCTION(libvirt_network_define_xml); -PHP_FUNCTION(libvirt_network_undefine); -PHP_FUNCTION(libvirt_network_get); -PHP_FUNCTION(libvirt_network_get_xml_desc); -PHP_FUNCTION(libvirt_network_get_bridge); -PHP_FUNCTION(libvirt_network_get_information); -PHP_FUNCTION(libvirt_network_get_active); -PHP_FUNCTION(libvirt_network_set_active); -PHP_FUNCTION(libvirt_network_get_uuid_string); -PHP_FUNCTION(libvirt_network_get_uuid); -PHP_FUNCTION(libvirt_network_get_name); -PHP_FUNCTION(libvirt_network_get_autostart); -PHP_FUNCTION(libvirt_network_set_autostart); /* Nodedev functions */ PHP_FUNCTION(libvirt_nodedev_get); PHP_FUNCTION(libvirt_nodedev_capabilities); @@ -333,8 +313,6 @@ 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_domain_snapshots); --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 23:55:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501624044280379.692844700966; Tue, 1 Aug 2017 14:47:24 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 13F0E2F011A; Tue, 1 Aug 2017 21:47:22 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 664C377D49; Tue, 1 Aug 2017 21:47:21 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id D85ED4A468; Tue, 1 Aug 2017 21:47:20 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkWAf007916 for ; Tue, 1 Aug 2017 17:46:33 -0400 Received: by smtp.corp.redhat.com (Postfix) id EF1E960476; Tue, 1 Aug 2017 21:46:32 +0000 (UTC) Received: from mx1.redhat.com (ext-mx01.extmail.prod.ext.phx2.redhat.com [10.5.110.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E826A60462 for ; Tue, 1 Aug 2017 21:46:32 +0000 (UTC) Received: from mail-qt0-f182.google.com (mail-qt0-f182.google.com [209.85.216.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 27823AACFC for ; Tue, 1 Aug 2017 21:46:25 +0000 (UTC) Received: by mail-qt0-f182.google.com with SMTP id s6so17228772qtc.1 for ; Tue, 01 Aug 2017 14:46:25 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.21 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:22 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 13F0E2F011A Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 13F0E2F011A Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="C+scOqLF" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 27823AACFC Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 27823AACFC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=8dm0ZtuVeQN3XDu7oaAqEZmRyo/iRsy+6OFT8fVqQyE=; b=C+scOqLFzlHu6qARPM+d98aOYXq/STiZzzB726YX8uwARzMPR6ETKXSukUkjXKOp3J 0vKMX2KvQUhDGsNswhBcgfIN2oZEpBtjAWiOSsTvLVgkMBYoWRR1M7mqBMrOnmgO5YMR xxprUixvInPICmGFFSyIxztN8EJ+ysU+N7Bhnczt4WiDapslQ2uwuzcfirblnJRVOqTk sVv1EaeUgZRuKkk9Z6GX8+BKG2Zce+c8zF8cFkfcU44nGeiqg8lorvDfrB7uM8rTf5Rf 1X3iioMwt/Sb75i5ZBb2B/dvxDquGt5mYTXjqbL9/JGowHnzrGx+wkU67c0iZQPnWHqY /ZWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=8dm0ZtuVeQN3XDu7oaAqEZmRyo/iRsy+6OFT8fVqQyE=; b=MUIH0uNo9X1epXR/4dYWYcl+oxi85Ib6mpsW9ATRMHuV8rjWJHUIxtI3n09pCim1Hv sHTxozAk8bxd9Adzy/ygz6+kOGRemB695qtzbiMfshPPFmk3aiI9nc0vutEXLNP2S1Qw PhC/4uMYbDdy+ZCpOgCQIFqyAfQ6c3Zno5UXAPQ4sTUOBYiylwdbH1Fino+IFgoM5OM+ U7dPWLXRPyw/xpbesfPlT5HER5zKmFgVOgQDKbDyWnbNXxLRK3WoUhsg8ddo4LZDttzP lvthlUj80Zqssa49DwmS9GqhiuKdNSN3k0EM0YBmbme3HX+fLVoQ1QbL4xvrvJm32Xep ETBw== X-Gm-Message-State: AIVw112lkTO6NTBslEx7FhIpzEAQmVJrm6ZN87518MR6Z3Gr5YfbEchD XDWXSSRFX2BiCIG415Y= X-Received: by 10.200.39.105 with SMTP id h38mr13882853qth.268.1501623982703; Tue, 01 Aug 2017 14:46:22 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:10 -0400 Message-Id: <20170801214614.8915-10-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 01 Aug 2017 21:46:25 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 01 Aug 2017 21:46:25 +0000 (UTC) for IP:'209.85.216.182' DOMAIN:'mail-qt0-f182.google.com' HELO:'mail-qt0-f182.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: -1.12 (DCC_REPUT_00_12, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.216.182 mail-qt0-f182.google.com 209.85.216.182 mail-qt0-f182.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.25 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 09/13] Split up the bindings for libvirt storage API X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 01 Aug 2017 21:47:22 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" --- src/Makefile.am | 3 +- src/libvirt-php.c | 1188 +--------------------------------------------= ---- src/libvirt-php.h | 49 -- src/libvirt-storage.c | 1129 ++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-storage.h | 138 ++++++ 5 files changed, 1271 insertions(+), 1236 deletions(-) create mode 100644 src/libvirt-storage.c create mode 100644 src/libvirt-storage.h diff --git a/src/Makefile.am b/src/Makefile.am index 4ae01db..32b23cf 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -26,7 +26,8 @@ libvirt_php_la_SOURCES =3D \ libvirt-node.c libvirt-node.h \ libvirt-stream.c libvirt-stream.h \ libvirt-domain.c libvirt-domain.h \ - libvirt-network.c libvirt-network.h=20 + libvirt-network.c libvirt-network.h \ + libvirt-storage.c libvirt-storage.h libvirt_php_la_CFLAGS =3D \ $(AM_CFLAGS) \ -DCOMPILE_DL_LIBVIRT=3D1 diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 9e43a71..1f0d058 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -25,6 +25,7 @@ #include "libvirt-stream.h" #include "libvirt-domain.h" #include "libvirt-network.h" +#include "libvirt-storage.h" =20 DEBUG_INIT("core"); =20 @@ -38,8 +39,6 @@ const char *features_binaries[] =3D { NULL }; #endif =20 /* ZEND thread safe per request globals definition */ -int le_libvirt_storagepool; -int le_libvirt_volume; int le_libvirt_nodedev; int le_libvirt_snapshot; int le_libvirt_nwfilter; @@ -485,38 +484,7 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE(libvirt_domain_snapshot_revert, arginfo_libvirt_conn_optf= lags) PHP_FE(libvirt_domain_snapshot_delete, arginfo_libvirt_conn_optf= lags) PHP_FE(libvirt_domain_snapshot_lookup_by_name, arginfo_libvirt_domain_= snapshot_lookup_by_name) - /* Storagepool functions */ - PHP_FE(libvirt_storagepool_lookup_by_name, arginfo_libvirt_conn_name) - PHP_FE(libvirt_storagepool_lookup_by_volume, arginfo_libvirt_conn) - PHP_FE(libvirt_storagepool_get_info, arginfo_libvirt_conn) - PHP_FE(libvirt_storagevolume_lookup_by_name, arginfo_libvirt_conn_name) - PHP_FE(libvirt_storagevolume_lookup_by_path, arginfo_libvirt_storagevo= lume_lookup_by_path) - PHP_FE(libvirt_storagevolume_get_name, arginfo_libvirt_conn) - PHP_FE(libvirt_storagevolume_get_path, arginfo_libvirt_conn) - PHP_FE(libvirt_storagevolume_get_info, arginfo_libvirt_conn) - PHP_FE(libvirt_storagevolume_get_xml_desc, arginfo_libvirt_storagevo= lume_get_xml_desc) - PHP_FE(libvirt_storagevolume_create_xml, arginfo_libvirt_conn_xml) - PHP_FE(libvirt_storagevolume_create_xml_from,arginfo_libvirt_storagevo= lume_create_xml_from) - PHP_FE(libvirt_storagevolume_delete, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_storagevolume_download, arginfo_libvirt_storagevo= lume_download) - PHP_FE(libvirt_storagevolume_upload, arginfo_libvirt_storagevo= lume_download) - PHP_FE(libvirt_storagevolume_resize, arginfo_libvirt_storagevo= lume_resize) - PHP_FE(libvirt_storagepool_get_uuid_string, arginfo_libvirt_conn) - PHP_FE(libvirt_storagepool_get_name, arginfo_libvirt_conn) - PHP_FE(libvirt_storagepool_lookup_by_uuid_string, arginfo_libvirt_conn= _uuid) - PHP_FE(libvirt_storagepool_get_xml_desc, arginfo_libvirt_conn_xpat= h) - PHP_FE(libvirt_storagepool_define_xml, arginfo_libvirt_storagepo= ol_define_xml) - PHP_FE(libvirt_storagepool_undefine, arginfo_libvirt_conn) - PHP_FE(libvirt_storagepool_create, arginfo_libvirt_conn) - PHP_FE(libvirt_storagepool_destroy, arginfo_libvirt_conn) - PHP_FE(libvirt_storagepool_is_active, arginfo_libvirt_conn) - PHP_FE(libvirt_storagepool_get_volume_count, arginfo_libvirt_conn) - PHP_FE(libvirt_storagepool_refresh, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_storagepool_set_autostart, arginfo_libvirt_conn_flag= s) - PHP_FE(libvirt_storagepool_get_autostart, arginfo_libvirt_conn) - PHP_FE(libvirt_storagepool_build, arginfo_libvirt_conn) - PHP_FE(libvirt_storagepool_delete, arginfo_libvirt_conn) - /* Network functions */ + PHP_FE_LIBVIRT_STORAGE PHP_FE_LIBVIRT_NETWORK PHP_FE_LIBVIRT_NODE /* Nodedev functions */ @@ -537,10 +505,6 @@ static zend_function_entry libvirt_functions[] =3D { /* List functions */ PHP_FE(libvirt_list_domain_snapshots, arginfo_libvirt_conn_optf= lags) PHP_FE(libvirt_list_nodedevs, arginfo_libvirt_conn_optc= ap) - PHP_FE(libvirt_list_storagepools, arginfo_libvirt_conn) - PHP_FE(libvirt_list_active_storagepools, arginfo_libvirt_conn) - PHP_FE(libvirt_list_inactive_storagepools, arginfo_libvirt_conn) - PHP_FE(libvirt_storagepool_list_volumes, 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 */ @@ -578,7 +542,6 @@ zend_module_entry libvirt_module_entry =3D { ZEND_GET_MODULE(libvirt) #endif =20 - /* PHP init options */ PHP_INI_BEGIN() STD_PHP_INI_ENTRY("libvirt.longlong_to_string", "1", PHP_INI_ALL, OnUpdate= Bool, longlong_to_string_ini, zend_libvirt_globals, libvirt_globals) @@ -1223,60 +1186,6 @@ int is_local_connection(virConnectPtr conn) #endif } =20 -/* Destructor for storagepool resource */ -static void php_libvirt_storagepool_dtor(virt_resource *rsrc TSRMLS_DC) -{ - php_libvirt_storagepool *pool =3D (php_libvirt_storagepool *)rsrc->ptr; - int rv =3D 0; - - if (pool !=3D NULL) { - if (pool->pool !=3D NULL) { - if (!check_resource_allocation(NULL, INT_RESOURCE_STORAGEPOOL,= pool->pool TSRMLS_CC)) { - pool->pool =3D NULL; - efree(pool); - return; - } - rv =3D virStoragePoolFree(pool->pool); - if (rv !=3D 0) { - DPRINTF("%s: virStoragePoolFree(%p) returned %d (%s)\n", _= _FUNCTION__, pool->pool, rv, LIBVIRT_G(last_error)); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStoragePoo= lFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); - } else { - DPRINTF("%s: virStoragePoolFree(%p) completed successfully= \n", __FUNCTION__, pool->pool); - resource_change_counter(INT_RESOURCE_STORAGEPOOL, NULL, po= ol->pool, 0 TSRMLS_CC); - } - pool->pool =3D NULL; - } - efree(pool); - } -} - -/* Destructor for volume resource */ -static void php_libvirt_volume_dtor(virt_resource *rsrc TSRMLS_DC) -{ - php_libvirt_volume *volume =3D (php_libvirt_volume *)rsrc->ptr; - int rv =3D 0; - - if (volume !=3D NULL) { - if (volume->volume !=3D NULL) { - if (!check_resource_allocation(NULL, INT_RESOURCE_VOLUME, volu= me->volume TSRMLS_CC)) { - volume->volume =3D NULL; - efree(volume); - return; - } - rv =3D virStorageVolFree(volume->volume); - if (rv !=3D 0) { - DPRINTF("%s: virStorageVolFree(%p) returned %d (%s)\n", __= FUNCTION__, volume->volume, rv, LIBVIRT_G(last_error)); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStorageVol= Free failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); - } else { - DPRINTF("%s: virStorageVolFree(%p) completed successfully\= n", __FUNCTION__, volume->volume); - resource_change_counter(INT_RESOURCE_VOLUME, NULL, volume-= >volume, 0 TSRMLS_CC); - } - volume->volume =3D NULL; - } - efree(volume); - } -} - /* Destructor for nodedev resource */ static void php_libvirt_nodedev_dtor(virt_resource *rsrc TSRMLS_DC) { @@ -1697,32 +1606,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) RETURN_FALSE; = \ } while (0) =20 -#define GET_STORAGEPOOL_FROM_ARGS(args, ...) = \ - do { = \ - reset_error(TSRMLS_C); = \ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_AR= GS__) =3D=3D FAILURE) { \ - set_error("Invalid arguments" TSRMLS_CC); = \ - RETURN_FALSE; = \ - } = \ - = \ - VIRT_FETCH_RESOURCE(pool, php_libvirt_storagepool*, &zpool, PHP_LI= BVIRT_STORAGEPOOL_RES_NAME, le_libvirt_storagepool);\ - if ((pool =3D=3D NULL) || (pool->pool =3D=3D NULL)) = \ - RETURN_FALSE; = \ - } while (0) = \ - -#define GET_VOLUME_FROM_ARGS(args, ...) = \ - do { = \ - reset_error(TSRMLS_C); = \ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_AR= GS__) =3D=3D FAILURE) { \ - set_error("Invalid arguments" TSRMLS_CC); = \ - RETURN_FALSE; = \ - } = \ - = \ - VIRT_FETCH_RESOURCE(volume, php_libvirt_volume*, &zvolume, PHP_LIB= VIRT_VOLUME_RES_NAME, le_libvirt_volume);\ - if ((volume =3D=3D NULL) || (volume->volume =3D=3D NULL)) = \ - RETURN_FALSE; = \ - } while (0) = \ - #define GET_SNAPSHOT_FROM_ARGS(args, ...) = \ do { = \ reset_error(TSRMLS_C); = \ @@ -2929,1075 +2812,8 @@ PHP_FUNCTION(libvirt_list_domain_snapshots) efree(names); } =20 -/* Storagepool functions */ - -/* - * Function name: libvirt_storagepool_lookup_by_name - * Since version: 0.4.1(-1) - * Description: Function is used to lookup for storage pool by it's na= me - * Arguments: @res [resource]: libvirt connection resource - * @name [string]: storage pool name - * Returns: libvirt storagepool resource - */ -PHP_FUNCTION(libvirt_storagepool_lookup_by_name) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - strsize_t name_len; - char *name =3D NULL; - virStoragePoolPtr pool =3D NULL; - php_libvirt_storagepool *res_pool; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); - - if ((name =3D=3D NULL) || (name_len < 1)) - RETURN_FALSE; - pool =3D virStoragePoolLookupByName(conn->conn, name); - DPRINTF("%s: virStoragePoolLookupByName(%p, %s) returned %p\n", PHPFUN= C, conn->conn, name, pool); - if (pool =3D=3D NULL) - RETURN_FALSE; - - res_pool =3D (php_libvirt_storagepool *)emalloc(sizeof(php_libvirt_sto= ragepool)); - res_pool->pool =3D pool; - res_pool->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_pool->pool); - resource_change_counter(INT_RESOURCE_STORAGEPOOL, conn->conn, res_pool= ->pool, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_pool, le_libvirt_storagepool); -} - -/* Storagepool functions */ - -/* - * Function name: libvirt_storagepool_lookup_by_volume - * Since version: 0.4.1(-1) - * Description: Function is used to lookup for storage pool by a volume - * Arguments: @res [volume]: volume resource of storage pool - * Returns: libvirt storagepool resource - */ -PHP_FUNCTION(libvirt_storagepool_lookup_by_volume) -{ - php_libvirt_volume *volume; - zval *zvolume; - virStoragePoolPtr pool =3D NULL; - php_libvirt_storagepool *res_pool; - - GET_VOLUME_FROM_ARGS("r", &zvolume); - - pool =3D virStoragePoolLookupByVolume(volume->volume); - DPRINTF("%s: virStoragePoolLookupByVolume(%p) returned %p\n", PHPFUNC,= volume->volume, pool); - if (pool =3D=3D NULL) - RETURN_FALSE; - - res_pool =3D (php_libvirt_storagepool *)emalloc(sizeof(php_libvirt_sto= ragepool)); - res_pool->pool =3D pool; - res_pool->conn =3D volume->conn; - - 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); - - VIRT_REGISTER_RESOURCE(res_pool, le_libvirt_storagepool); -} - -/* - * Function name: libvirt_storagepool_list_volumes - * Since version: 0.4.1(-1) - * Description: Function is used to list volumes in the specified stor= age pool - * Arguments: @res [resource]: libvirt storagepool resource - * Returns: list of storage volume names in the storage pool in an= array using default keys (indexes) - */ -PHP_FUNCTION(libvirt_storagepool_list_volumes) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - char **names =3D NULL; - int expectedcount =3D -1; - int i; - int count =3D -1; - - GET_STORAGEPOOL_FROM_ARGS("r", &zpool); - - if ((expectedcount =3D virStoragePoolNumOfVolumes(pool->pool)) < 0) - RETURN_FALSE; - - DPRINTF("%s: virStoragePoolNumOfVolumes(%p) returned %d\n", PHPFUNC, p= ool->pool, expectedcount); - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - - count =3D virStoragePoolListVolumes(pool->pool, names, expectedcount); - DPRINTF("%s: virStoragePoolListVolumes(%p, %p, %d) returned %d\n", PHP= FUNC, pool->pool, names, expectedcount, count); - array_init(return_value); - - if ((count !=3D expectedcount) || (count < 0)) - RETURN_FALSE; - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - - efree(names); -} - -/* - * Function name: libvirt_storagepool_get_info - * Since version: 0.4.1(-1) - * Description: Function is used to get information about the storage = pool - * Arguments: @res [resource]: libvirt storagepool resource - * Returns: storage pool information array of state, capacity, all= ocation and available space - */ -PHP_FUNCTION(libvirt_storagepool_get_info) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - virStoragePoolInfo poolInfo; - int retval; - - GET_STORAGEPOOL_FROM_ARGS("r", &zpool); - - retval =3D virStoragePoolGetInfo(pool->pool, &poolInfo); - DPRINTF("%s: virStoragePoolGetInfo(%p, ) returned %d\n", PHPFUNC= , pool->pool, retval); - if (retval !=3D 0) - RETURN_FALSE; - - array_init(return_value); - - // @todo: fix the long long returns - LONGLONG_INIT; - add_assoc_long(return_value, "state", (long)poolInfo.state); - LONGLONG_ASSOC(return_value, "capacity", poolInfo.capacity); - LONGLONG_ASSOC(return_value, "allocation", poolInfo.allocation); - LONGLONG_ASSOC(return_value, "available", poolInfo.available); -} - -/* - * Function name: libvirt_storagevolume_lookup_by_name - * Since version: 0.4.1(-1) - * Description: Function is used to lookup for storage volume by it's = name - * Arguments: @res [resource]: libvirt storagepool resource - * @name [string]: name of the storage volume to look for - * Returns: libvirt storagevolume resource - */ -PHP_FUNCTION(libvirt_storagevolume_lookup_by_name) -{ - php_libvirt_storagepool *pool =3D NULL; - php_libvirt_volume *res_volume; - zval *zpool; - strsize_t name_len; - char *name =3D NULL; - virStorageVolPtr volume =3D NULL; - - GET_STORAGEPOOL_FROM_ARGS("rs", &zpool, &name, &name_len); - if ((name =3D=3D NULL) || (name_len < 1)) - RETURN_FALSE; - - volume =3D virStorageVolLookupByName(pool->pool, name); - DPRINTF("%s: virStorageVolLookupByName(%p, %s) returned %p\n", PHPFUNC= , pool->pool, name, volume); - if (volume =3D=3D NULL) - RETURN_FALSE; - - res_volume =3D (php_libvirt_volume *)emalloc(sizeof(php_libvirt_volume= )); - res_volume->volume =3D volume; - res_volume->conn =3D pool->conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_volume->volume); - resource_change_counter(INT_RESOURCE_VOLUME, pool->conn->conn, res_vol= ume->volume, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_volume, le_libvirt_volume); -} - -/* - * Function name: libvirt_storagevolume_lookup_by_path - * Since version: 0.4.1(-2) - * Description: Function is used to lookup for storage volume by it's = path - * Arguments: @res [resource]: libvirt connection resource - * @path [string]: path of the storage volume to look for - * Returns: libvirt storagevolume resource - */ -PHP_FUNCTION(libvirt_storagevolume_lookup_by_path) -{ - php_libvirt_connection *conn =3D NULL; - php_libvirt_volume *res_volume; - zval *zconn; - strsize_t name_len; - char *name =3D NULL; - virStorageVolPtr volume =3D NULL; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); - if ((name =3D=3D NULL) || (name_len < 1)) - RETURN_FALSE; - - volume =3D virStorageVolLookupByPath(conn->conn, name); - DPRINTF("%s: virStorageVolLookupByPath(%p, %s) returned %p\n", PHPFUNC= , conn->conn, name, volume); - if (volume =3D=3D NULL) { - set_error_if_unset("Cannot find storage volume on requested path" = TSRMLS_CC); - RETURN_FALSE; - } - - res_volume =3D (php_libvirt_volume *)emalloc(sizeof(php_libvirt_volume= )); - res_volume->volume =3D volume; - res_volume->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_volume->volume); - resource_change_counter(INT_RESOURCE_VOLUME, conn->conn, res_volume->v= olume, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_volume, le_libvirt_volume); -} - -/* - * Function name: libvirt_storagevolume_get_name - * Since version: 0.4.1(-2) - * Description: Function is used to get the storage volume name - * Arguments: @res [resource]: libvirt storagevolume resource - * Returns: storagevolume name - */ -PHP_FUNCTION(libvirt_storagevolume_get_name) -{ - php_libvirt_volume *volume =3D NULL; - zval *zvolume; - const char *retval; - - GET_VOLUME_FROM_ARGS("r", &zvolume); - - retval =3D virStorageVolGetName(volume->volume); - DPRINTF("%s: virStorageVolGetName(%p) returned %s\n", PHPFUNC, volume-= >volume, retval); - if (retval =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETURN_STRING(retval); -} - -/* - * Function name: libvirt_storagevolume_get_path - * Since version: 0.4.1(-2) - * Description: Function is used to get the storage volume path - * Arguments: @res [resource]: libvirt storagevolume resource - * Returns: storagevolume path - */ -PHP_FUNCTION(libvirt_storagevolume_get_path) -{ - php_libvirt_volume *volume =3D NULL; - zval *zvolume; - char *retval; - - GET_VOLUME_FROM_ARGS("r", &zvolume); - - retval =3D virStorageVolGetPath(volume->volume); - DPRINTF("%s: virStorageVolGetPath(%p) returned %s\n", PHPFUNC, volume-= >volume, retval); - if (retval =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETVAL_STRING(retval); - free(retval); -} - -/* - * Function name: libvirt_storagevolume_get_info - * Since version: 0.4.1(-1) - * Description: Function is used to get the storage volume information - * Arguments: @res [resource]: libvirt storagevolume resource - * Returns: storage volume information array of type, allocation a= nd capacity - */ -PHP_FUNCTION(libvirt_storagevolume_get_info) -{ - php_libvirt_volume *volume =3D NULL; - zval *zvolume; - virStorageVolInfo volumeInfo; - int retval; - - GET_VOLUME_FROM_ARGS("r", &zvolume); - - retval =3D virStorageVolGetInfo(volume->volume, &volumeInfo); - DPRINTF("%s: virStorageVolGetInfo(%p, ) returned %d\n", PHPFUNC,= volume->volume, retval); - if (retval !=3D 0) - RETURN_FALSE; - - array_init(return_value); - LONGLONG_INIT; - add_assoc_long(return_value, "type", (long)volumeInfo.type); - LONGLONG_ASSOC(return_value, "capacity", volumeInfo.capacity); - LONGLONG_ASSOC(return_value, "allocation", volumeInfo.allocation); -} - -/* - * Function name: libvirt_storagevolume_get_xml_desc - * Since version: 0.4.1(-1), changed 0.4.2 - * Description: Function is used to get the storage volume XML descrip= tion - * Arguments: @res [resource]: libvirt storagevolume resource - * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL - * @flags [int]: optional flags - * Returns: storagevolume XML description or result of xPath expre= ssion - */ -PHP_FUNCTION(libvirt_storagevolume_get_xml_desc) -{ - php_libvirt_volume *volume =3D NULL; - zval *zvolume; - char *tmp =3D NULL; - char *xml; - char *xpath =3D NULL; - strsize_t xpath_len; - zend_long flags =3D 0; - int retval =3D -1; - - GET_VOLUME_FROM_ARGS("rs|l", &zvolume, &xpath, &xpath_len, &flags); - if (xpath_len < 1) - xpath =3D NULL; - - DPRINTF("%s: volume =3D %p, xpath =3D %s, flags =3D %ld\n", PHPFUNC, v= olume->volume, xpath, flags); - - xml =3D virStorageVolGetXMLDesc(volume->volume, flags); - if (xml =3D=3D NULL) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - VIRT_RETVAL_STRING(xml); - } else { - VIRT_RETVAL_STRING(tmp); - } - - free(xml); - free(tmp); -} - -/* - * Function name: libvirt_storagevolume_create_xml - * Since version: 0.4.1(-1) - * Description: Function is used to create the new storage pool and re= turn the handle to new storage pool - * Arguments: @res [resource]: libvirt storagepool resource - * @xml [string]: XML string to create the storage volume= in the storage pool - * @flags [int]: virStorageVolCreateXML flags - * Returns: libvirt storagevolume resource - */ -PHP_FUNCTION(libvirt_storagevolume_create_xml) -{ - php_libvirt_volume *res_volume =3D NULL; - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - virStorageVolPtr volume =3D NULL; - char *xml; - zend_long flags =3D 0; - strsize_t xml_len; - - GET_STORAGEPOOL_FROM_ARGS("rs|l", &zpool, &xml, &xml_len, &flags); - - volume =3D virStorageVolCreateXML(pool->pool, xml, flags); - DPRINTF("%s: virStorageVolCreateXML(%p, , 0) returned %p\n", PHPF= UNC, pool->pool, volume); - if (volume =3D=3D NULL) - RETURN_FALSE; - - res_volume =3D (php_libvirt_volume *)emalloc(sizeof(php_libvirt_volume= )); - res_volume->volume =3D volume; - res_volume->conn =3D pool->conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_volume->volume); - resource_change_counter(INT_RESOURCE_VOLUME, pool->conn->conn, res_vol= ume->volume, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_volume, le_libvirt_volume); -} - -/* - * Function name: libvirt_storagevolume_create_xml_from - * Since version: 0.4.1(-2) - * Description: Function is used to clone the new storage volume into = pool from the orignial volume - * Arguments: @pool [resource]: libvirt storagepool resource - * @xml [string]: XML string to create the storage volume= in the storage pool - * @original_volume [resource]: libvirt storagevolume res= ource - * Returns: libvirt storagevolume resource - */ -PHP_FUNCTION(libvirt_storagevolume_create_xml_from) -{ - php_libvirt_volume *res_volume =3D NULL; - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - - php_libvirt_volume *pl_volume =3D NULL; - zval *zvolume; - - virStorageVolPtr volume =3D NULL; - char *xml; - strsize_t xml_len; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsr", &zpool, &x= ml, &xml_len, &zvolume) =3D=3D FAILURE) { - set_error("Invalid pool resource, XML or volume resouce" TSRMLS_CC= ); - RETURN_FALSE; - } - - VIRT_FETCH_RESOURCE(pool, php_libvirt_storagepool*, &zpool, PHP_LIBVIR= T_STORAGEPOOL_RES_NAME, le_libvirt_storagepool); - if ((pool =3D=3D NULL) || (pool->pool =3D=3D NULL)) - RETURN_FALSE; - VIRT_FETCH_RESOURCE(pl_volume, php_libvirt_volume*, &zvolume, PHP_LIBV= IRT_VOLUME_RES_NAME, le_libvirt_volume); - if ((pl_volume =3D=3D NULL) || (pl_volume->volume =3D=3D NULL)) - RETURN_FALSE; - resource_change_counter(INT_RESOURCE_VOLUME, NULL, pl_volume->volume, = 1 TSRMLS_CC); - - volume =3D virStorageVolCreateXMLFrom(pool->pool, xml, pl_volume->volu= me, 0); - DPRINTF("%s: virStorageVolCreateXMLFrom(%p, , %p, 0) returned %p\= n", PHPFUNC, pool->pool, pl_volume->volume, volume); - if (volume =3D=3D NULL) - RETURN_FALSE; - - res_volume =3D (php_libvirt_volume *)emalloc(sizeof(php_libvirt_volume= )); - res_volume->volume =3D volume; - res_volume->conn =3D pool->conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_volume->volume); - resource_change_counter(INT_RESOURCE_VOLUME, pool->conn->conn, res_vol= ume->volume, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_volume, le_libvirt_volume); -} - -/* - * Function name: libvirt_storagevolume_delete - * Since version: 0.4.2 - * Description: Function is used to delete to volume identified by it'= s resource - * Arguments: @res [resource]: libvirt storagevolume resource - * @flags [int]: optional flags for the storage volume de= letion for virStorageVolDelete() - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_storagevolume_delete) -{ - php_libvirt_volume *volume =3D NULL; - zval *zvolume; - zend_long flags =3D 0; - int retval =3D 0; - - GET_VOLUME_FROM_ARGS("r|l", &zvolume, &flags); - - retval =3D virStorageVolDelete(volume->volume, flags); - DPRINTF("%s: virStorageVolDelete(%p, %d) returned %d\n", PHPFUNC, volu= me->volume, (int) flags, retval); - if (retval !=3D 0) { - set_error_if_unset("Cannot delete storage volume" TSRMLS_CC); - RETURN_FALSE; - } - - RETURN_TRUE; -} - -/* - * Function name: libvirt_storagevolume_resize - * Since version: 0.5.0 - * Description: Function is used to resize volume identified by it's r= esource - * Arguments: @res [resource]: libvirt storagevolume resource - * @capacity [int]: capacity for the storage volume - * @flags [int]: optional flags for the storage volume re= size for virStorageVolResize() - * Returns: int - */ -PHP_FUNCTION(libvirt_storagevolume_resize) -{ - php_libvirt_volume *volume =3D NULL; - zval *zvolume; - zend_long flags =3D 0; - zend_long capacity =3D 0; - int retval =3D -1; - - GET_VOLUME_FROM_ARGS("rl|l", &zvolume, &capacity, &flags); - - retval =3D virStorageVolResize(volume->volume, capacity, flags); - DPRINTF("%s: virStorageVolResize(%p, %d, %d) returned %d\n", PHPFUNC, = volume->volume, (int) capacity, (int) flags, retval); - if (retval !=3D 0) { - set_error_if_unset("Cannot resize storage volume" TSRMLS_CC); - RETURN_LONG(retval); - } - - RETURN_LONG(retval); -} - -/* - * Function name: libvirt_storagevolume_download - * Since version: 0.5.0 - * Description: Function is used to download volume identified by it's= resource - * Arguments: @res [resource]: libvirt storagevolume resource - * @stream [resource]: stream to use as output - * @offset [int]: position to start reading from - * @length [int] : limit on amount of data to download - * @flags [int]: optional flags for the storage volume do= wnload for virStorageVolDownload() - * Returns: int - */ -PHP_FUNCTION(libvirt_storagevolume_download) -{ - php_libvirt_volume *volume =3D NULL; - php_libvirt_stream *stream =3D NULL; - zval *zvolume; - zval *zstream; - zend_long flags =3D 0; - zend_long offset =3D 0; - zend_long length =3D 0; - int retval =3D -1; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|l|l|l", &zvol= ume, &zstream, &offset, &length, &flags) =3D=3D FAILURE) - RETURN_LONG(retval); - VIRT_FETCH_RESOURCE(volume, php_libvirt_volume*, &zvolume, PHP_LIBVIRT= _VOLUME_RES_NAME, le_libvirt_volume); - if ((volume =3D=3D NULL) || (volume->volume =3D=3D NULL)) - RETURN_LONG(retval); - VIRT_FETCH_RESOURCE(stream, php_libvirt_stream*, &zstream, PHP_LIBVIRT= _STREAM_RES_NAME, le_libvirt_stream); - if ((stream =3D=3D NULL) || (stream->stream =3D=3D NULL)) - RETURN_LONG(retval); - - retval =3D virStorageVolDownload(volume->volume, stream->stream, offse= t, length, flags); - DPRINTF("%s: virStorageVolDownload(%p, %p, %d, %d, %d) returned %d\n",= PHPFUNC, volume->volume, stream->stream, (int) offset, (int) length, (int)= flags, retval); - - if (retval =3D=3D -1) { - set_error("Cannot download from stream" TSRMLS_CC); - RETURN_LONG(retval); - } - - RETURN_LONG(retval); -} - -/* - * Function name: libvirt_storagevolume_upload - * Since version: 0.5.0 - * Description: Function is used to upload volume identified by it's r= esource - * Arguments: @res [resource]: libvirt storagevolume resource - * @stream [resource]: stream to use as input - * @offset [int]: position to start writing to - * @length [int] : limit on amount of data to upload - * @flags [int]: optional flags for the storage volume up= load for virStorageVolUpload() - * Returns: int - */ -PHP_FUNCTION(libvirt_storagevolume_upload) -{ - php_libvirt_volume *volume =3D NULL; - php_libvirt_stream *stream =3D NULL; - zval *zvolume; - zval *zstream; - zend_long flags =3D 0; - zend_long offset =3D 0; - zend_long length =3D 0; - int retval =3D -1; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|l|l|l", &zvol= ume, &zstream, &offset, &length, &flags) =3D=3D FAILURE) - RETURN_LONG(retval); - VIRT_FETCH_RESOURCE(volume, php_libvirt_volume*, &zvolume, PHP_LIBVIRT= _VOLUME_RES_NAME, le_libvirt_volume); - if ((volume =3D=3D NULL) || (volume->volume =3D=3D NULL)) - RETURN_LONG(retval); - VIRT_FETCH_RESOURCE(stream, php_libvirt_stream*, &zstream, PHP_LIBVIRT= _STREAM_RES_NAME, le_libvirt_stream); - if ((stream =3D=3D NULL) || (stream->stream =3D=3D NULL)) - RETURN_LONG(retval); - - retval =3D virStorageVolUpload(volume->volume, stream->stream, offset,= length, flags); - DPRINTF("%s: virStorageVolUpload(%p, %p, %d, %d, %d) returned %d\n", P= HPFUNC, volume->volume, stream->stream, (int) offset, (int) length, (int) f= lags, retval); - - if (retval =3D=3D -1) { - set_error_if_unset("Cannot upload storage volume" TSRMLS_CC); - RETURN_LONG(retval); - } - - RETURN_LONG(retval); -} - -/* - * Function name: libvirt_storagepool_get_uuid_string - * Since version: 0.4.1(-1) - * Description: Function is used to get storage pool by UUID string - * Arguments: @res [resource]: libvirt storagepool resource - * Returns: storagepool UUID string - */ -PHP_FUNCTION(libvirt_storagepool_get_uuid_string) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - char *uuid; - int retval; - - GET_STORAGEPOOL_FROM_ARGS("r", &zpool); - - uuid =3D (char *)emalloc(VIR_UUID_STRING_BUFLEN); - retval =3D virStoragePoolGetUUIDString(pool->pool, uuid); - DPRINTF("%s: virStoragePoolGetUUIDString(%p, %p) returned %d (%s)\n", = PHPFUNC, pool->pool, uuid, retval, uuid); - if (retval !=3D 0) - RETURN_FALSE; - - VIRT_RETVAL_STRING(uuid); - efree(uuid); -} - -/* - * Function name: libvirt_storagepool_get_name - * Since version: 0.4.1(-1) - * Description: Function is used to get storage pool name from the sto= rage pool resource - * Arguments: @res [resource]: libvirt storagepool resource - * Returns: storagepool name string - */ -PHP_FUNCTION(libvirt_storagepool_get_name) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - const char *name =3D NULL; - - GET_STORAGEPOOL_FROM_ARGS("r", &zpool); - - name =3D virStoragePoolGetName(pool->pool); - DPRINTF("%s: virStoragePoolGetName(%p) returned %s\n", PHPFUNC, pool->= pool, name); - if (name =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETURN_STRING(name); -} - -/* - * Function name: libvirt_storagepool_lookup_by_uuid_string - * Since version: 0.4.1(-1) - * Description: Function is used to lookup for storage pool identified= by UUID string - * Arguments: @res [resource]: libvirt connection resource - * @uuid [string]: UUID string to look for storagepool - * Returns: libvirt storagepool resource - */ -PHP_FUNCTION(libvirt_storagepool_lookup_by_uuid_string) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - char *uuid =3D NULL; - strsize_t uuid_len; - virStoragePoolPtr storage =3D NULL; - php_libvirt_storagepool *res_pool; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); - - if ((uuid =3D=3D NULL) || (uuid_len < 1)) - RETURN_FALSE; - - storage =3D virStoragePoolLookupByUUIDString(conn->conn, uuid); - DPRINTF("%s: virStoragePoolLookupByUUIDString(%p, %s) returned %p\n", = PHPFUNC, conn->conn, uuid, storage); - if (storage =3D=3D NULL) - RETURN_FALSE; - - res_pool =3D (php_libvirt_storagepool *)emalloc(sizeof(php_libvirt_sto= ragepool)); - res_pool->pool =3D storage; - res_pool->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_pool->pool); - resource_change_counter(INT_RESOURCE_STORAGEPOOL, conn->conn, res_pool= ->pool, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_pool, le_libvirt_storagepool); -} - -/* - * Function name: libvirt_storagepool_get_xml_desc - * Since version: 0.4.1(-1), changed 0.4.2 - * Description: Function is used to get the XML description for the st= orage pool identified by res - * Arguments: @res [resource]: libvirt storagepool resource - * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL - * Returns: storagepool XML description string or result of xPath = expression - */ -PHP_FUNCTION(libvirt_storagepool_get_xml_desc) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - char *xml; - char *xpath =3D NULL; - char *tmp =3D NULL; - zend_long flags =3D 0; - strsize_t xpath_len; - int retval =3D -1; - - GET_STORAGEPOOL_FROM_ARGS("r|s", &zpool, &xpath, &xpath_len, &flags); - if (xpath_len < 1) - xpath =3D NULL; - - DPRINTF("%s: pool =3D %p, flags =3D %ld, xpath =3D %s\n", PHPFUNC, poo= l->pool, flags, xpath); - - xml =3D virStoragePoolGetXMLDesc(pool->pool, flags); - if (xml =3D=3D NULL) { - set_error_if_unset("Cannot get the XML description" TSRMLS_CC); - RETURN_FALSE; - } - - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - VIRT_RETVAL_STRING(xml); - } else { - VIRT_RETVAL_STRING(tmp); - } - - free(xml); - free(tmp); -} - -/* - * Function name: libvirt_storagepool_define_xml - * Since version: 0.4.1(-1) - * Description: Function is used to define the storage pool from XML s= tring and return it's resource - * Arguments: @res [resource]: libvirt connection resource - * @xml [string]: XML string definition of storagepool - * @flags [int]: flags to define XML - * Returns: libvirt storagepool resource - */ -PHP_FUNCTION(libvirt_storagepool_define_xml) -{ - php_libvirt_storagepool *res_pool =3D NULL; - php_libvirt_connection *conn =3D NULL; - zval *zconn; - virStoragePoolPtr pool =3D NULL; - char *xml; - strsize_t xml_len; - zend_long flags =3D 0; - - - GET_CONNECTION_FROM_ARGS("rs|l", &zconn, &xml, &xml_len, &flags); - - pool =3D virStoragePoolDefineXML(conn->conn, xml, (unsigned int)flags); - DPRINTF("%s: virStoragePoolDefineXML(%p, ) returned %p\n", PHPFUN= C, conn->conn, pool); - if (pool =3D=3D NULL) - RETURN_FALSE; - - res_pool =3D (php_libvirt_storagepool *)emalloc(sizeof(php_libvirt_sto= ragepool)); - res_pool->pool =3D pool; - res_pool->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_pool->pool); - resource_change_counter(INT_RESOURCE_STORAGEPOOL, conn->conn, res_pool= ->pool, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_pool, le_libvirt_storagepool); -} - -/* - * Function name: libvirt_storagepool_undefine - * Since version: 0.4.1(-1) - * Description: Function is used to undefine the storage pool identifi= ed by it's resource - * Arguments: @res [resource]: libvirt storagepool resource - * Returns: TRUE if success, FALSE on error - */ -PHP_FUNCTION(libvirt_storagepool_undefine) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - int retval =3D 0; - - GET_STORAGEPOOL_FROM_ARGS("r", &zpool); - - retval =3D virStoragePoolUndefine(pool->pool); - DPRINTF("%s: virStoragePoolUndefine(%p) returned %d\n", PHPFUNC, pool-= >pool, retval); - if (retval !=3D 0) - RETURN_FALSE; - - RETURN_TRUE; -} - -/* - * Function name: libvirt_storagepool_create - * Since version: 0.4.1(-1) - * Description: Function is used to create/start the storage pool - * Arguments: @res [resource]: libvirt storagepool resource - * Returns: TRUE if success, FALSE on error - */ -PHP_FUNCTION(libvirt_storagepool_create) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - int retval; - - GET_STORAGEPOOL_FROM_ARGS("r", &zpool); - - retval =3D virStoragePoolCreate(pool->pool, 0); - DPRINTF("%s: virStoragePoolCreate(%p, 0) returned %d\n", PHPFUNC, pool= ->pool, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_storagepool_destroy - * Since version: 0.4.1(-1) - * Description: Function is used to destory the storage pool - * Arguments: @res [resource]: libvirt storagepool resource - * Returns: TRUE if success, FALSE on error - */ -PHP_FUNCTION(libvirt_storagepool_destroy) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - int retval; - - GET_STORAGEPOOL_FROM_ARGS("r", &zpool); - - retval =3D virStoragePoolDestroy(pool->pool); - DPRINTF("%s: virStoragePoolDestroy(%p) returned %d\n", PHPFUNC, pool->= pool, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_storagepool_is_active - * Since version: 0.4.1(-1) - * Description: Function is used to get information whether storage po= ol is active or not - * Arguments: @res [resource]: libvirt storagepool resource - * Returns: result of virStoragePoolIsActive - */ -PHP_FUNCTION(libvirt_storagepool_is_active) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - - GET_STORAGEPOOL_FROM_ARGS("r", &zpool); - - RETURN_LONG(virStoragePoolIsActive(pool->pool)); -} - -/* - * Function name: libvirt_storagepool_get_volume_count - * Since version: 0.4.1(-1) - * Description: Function is used to get storage volume count in the st= orage pool - * Arguments: @res [resource]: libvirt storagepool resource - * Returns: number of volumes in the pool - */ -PHP_FUNCTION(libvirt_storagepool_get_volume_count) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - - GET_STORAGEPOOL_FROM_ARGS("r", &zpool); - - RETURN_LONG(virStoragePoolNumOfVolumes(pool->pool)); -} - -/* - * Function name: libvirt_storagepool_refresh - * Since version: 0.4.1(-1) - * Description: Function is used to refresh the storage pool informati= on - * Arguments: @res [resource]: libvirt storagepool resource - * @flags [int]: refresh flags - * Returns: TRUE if success, FALSE on error - */ -PHP_FUNCTION(libvirt_storagepool_refresh) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - zend_long flags =3D 0; - int retval; - - GET_STORAGEPOOL_FROM_ARGS("r|l", &zpool, &flags); - - retval =3D virStoragePoolRefresh(pool->pool, flags); - DPRINTF("%s: virStoragePoolRefresh(%p, %ld) returned %d\n", PHPFUNC, p= ool->pool, flags, retval); - if (retval < 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_storagepool_set_autostart - * Since version: 0.4.1(-1) - * Description: Function is used to set autostart of the storage pool - * Arguments: @res [resource]: libvirt storagepool resource - * @flags [int]: flags to set autostart - * Returns: result on setting storagepool autostart value - */ -PHP_FUNCTION(libvirt_storagepool_set_autostart) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - zend_bool flags =3D 0; - int retval; - - GET_STORAGEPOOL_FROM_ARGS("rb", &zpool, &flags); - - retval =3D virStoragePoolSetAutostart(pool->pool, flags); - DPRINTF("%s: virStoragePoolSetAutostart(%p, %d) returned %d\n", PHPFUN= C, pool->pool, flags, retval); - if (retval !=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_storagepool_get_autostart - * Since version: 0.4.1(-1) - * Description: Function is used to get autostart of the storage pool - * Arguments: @res [resource]: libvirt storagepool resource - * Returns: TRUE for autostart enabled, FALSE for autostart disabl= ed, FALSE with last_error set for error - */ -PHP_FUNCTION(libvirt_storagepool_get_autostart) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - int autostart; - - GET_STORAGEPOOL_FROM_ARGS("r", &zpool); - - if (virStoragePoolGetAutostart(pool->pool, &autostart) =3D=3D 0 && - autostart !=3D 0) - RETURN_TRUE; - - RETURN_FALSE; -} - -/* - * Function name: libvirt_storagepool_build - * Since version: 0.4.2 - * Description: Function is used to Build the underlying storage pool,= e.g. create the destination directory for NFS - * Arguments: @res [resource]: libvirt storagepool resource - * Returns: TRUE if success, FALSE on error - */ -PHP_FUNCTION(libvirt_storagepool_build) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - int flags =3D 0; - int retval; - - GET_STORAGEPOOL_FROM_ARGS("r", &zpool); - - retval =3D virStoragePoolBuild(pool->pool, flags); - DPRINTF("%s: virStoragePoolBuild(%p, %d) returned %d\n", PHPFUNC, pool= ->pool, flags, retval); - if (retval =3D=3D 0) - RETURN_TRUE; - - RETURN_FALSE; -} - -/* - * Function name: libvirt_storagepool_delete - * Since version: 0.4.6 - * Description: Function is used to Delete the underlying storage pool= , e.g. remove the destination directory for NFS - * Arguments: @res [resource]: libvirt storagepool resource - * Returns: TRUE if success, FALSE on error - */ -PHP_FUNCTION(libvirt_storagepool_delete) -{ - php_libvirt_storagepool *pool =3D NULL; - zval *zpool; - int flags =3D 0; - int retval; - - GET_STORAGEPOOL_FROM_ARGS("r", &zpool); - - retval =3D virStoragePoolDelete(pool->pool, flags); - DPRINTF("%s: virStoragePoolDelete(%p, %d) returned %d\n", PHPFUNC, poo= l->pool, flags, retval); - if (retval =3D=3D 0) - RETURN_TRUE; - - RETURN_FALSE; -} - /* Listing functions */ /* - * Function name: libvirt_list_storagepools - * Since version: 0.4.1(-1) - * Description: Function is used to list storage pools on the connecti= on - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt storagepool names array for the connection - */ -PHP_FUNCTION(libvirt_list_storagepools) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - char **names; - int i; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((expectedcount =3D virConnectNumOfStoragePools(conn->conn)) < 0) - RETURN_FALSE; - - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virConnectListStoragePools(conn->conn, names, expectedcount); - - if ((count !=3D expectedcount) || (count < 0)) { - efree(names); - RETURN_FALSE; - } - - array_init(return_value); - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - efree(names); - - - if ((expectedcount =3D virConnectNumOfDefinedStoragePools(conn->conn))= < 0) - RETURN_FALSE; - names =3D (char **)emalloc(expectedcount * sizeof(char *)); - count =3D virConnectListDefinedStoragePools(conn->conn, names, expecte= dcount); - if ((count !=3D expectedcount) || (count < 0)) { - efree(names); - RETURN_FALSE; - } - - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - efree(names); -} - -/* - * Function name: libvirt_list_active_storagepools - * Since version: 0.4.1(-1) - * Description: Function is used to list active storage pools on the c= onnection - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt storagepool names array for the connection - */ -PHP_FUNCTION(libvirt_list_active_storagepools) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - char **names; - int i; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((expectedcount =3D virConnectNumOfStoragePools(conn->conn)) < 0) - RETURN_FALSE; - - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virConnectListStoragePools(conn->conn, names, expectedcount); - - if ((count !=3D expectedcount) || (count < 0)) { - efree(names); - RETURN_FALSE; - } - array_init(return_value); - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - efree(names); -} - -/* - * Function name: libvirt_list_inactive_storagepools - * Since version: 0.4.1(-1) - * Description: Function is used to list inactive storage pools on the= connection - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt storagepool names array for the connection - */ -PHP_FUNCTION(libvirt_list_inactive_storagepools) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - char **names; - int i; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((expectedcount =3D virConnectNumOfDefinedStoragePools(conn->conn))= < 0) - RETURN_FALSE; - - names =3D (char **)emalloc(expectedcount * sizeof(char *)); - count =3D virConnectListDefinedStoragePools(conn->conn, names, expecte= dcount); - if ((count !=3D expectedcount) || (count < 0)) { - efree(names); - RETURN_FALSE; - } - - array_init(return_value); - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - efree(names); -} - -/* * Function name: libvirt_list_nodedevs * Since version: 0.4.1(-1) * Description: Function is used to list node devices on the connection diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 7670582..1505878 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -129,8 +129,6 @@ typedef uint64_t arch_uint; =20 /* Internal resource identifier objects */ #define INT_RESOURCE_NODEDEV 0x08 -#define INT_RESOURCE_STORAGEPOOL 0x10 -#define INT_RESOURCE_VOLUME 0x20 #define INT_RESOURCE_SNAPSHOT 0x40 #define INT_RESOURCE_NWFILTER 0x60 =20 @@ -176,16 +174,6 @@ typedef struct _php_libvirt_nodedev { php_libvirt_connection* conn; } php_libvirt_nodedev; =20 -typedef struct _php_libvirt_storagepool { - virStoragePoolPtr pool; - php_libvirt_connection* conn; -} php_libvirt_storagepool; - -typedef struct _php_libvirt_volume { - virStorageVolPtr volume; - php_libvirt_connection* conn; -} php_libvirt_volume; - typedef struct _php_libvirt_nwfilter { virNWFilterPtr nwfilter; php_libvirt_connection* conn; @@ -243,8 +231,6 @@ const char *get_feature_binary(const char *name); long get_next_free_numeric_value(virDomainPtr domain, char *xpath); int get_subnet_bits(char *ip); =20 -#define PHP_LIBVIRT_STORAGEPOOL_RES_NAME "Libvirt storagepool" -#define PHP_LIBVIRT_VOLUME_RES_NAME "Libvirt volume" #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" @@ -264,38 +250,6 @@ PHP_FUNCTION(libvirt_domain_snapshot_lookup_by_name); PHP_FUNCTION(libvirt_domain_snapshot_get_xml); PHP_FUNCTION(libvirt_domain_snapshot_revert); PHP_FUNCTION(libvirt_domain_snapshot_delete); -/* Storage pool and storage volume functions */ -PHP_FUNCTION(libvirt_storagepool_lookup_by_name); -PHP_FUNCTION(libvirt_storagepool_lookup_by_volume); -PHP_FUNCTION(libvirt_storagepool_list_volumes); -PHP_FUNCTION(libvirt_storagepool_get_info); -PHP_FUNCTION(libvirt_storagevolume_lookup_by_name); -PHP_FUNCTION(libvirt_storagevolume_lookup_by_path); -PHP_FUNCTION(libvirt_storagevolume_get_name); -PHP_FUNCTION(libvirt_storagevolume_get_path); -PHP_FUNCTION(libvirt_storagevolume_get_info); -PHP_FUNCTION(libvirt_storagevolume_get_xml_desc); -PHP_FUNCTION(libvirt_storagevolume_create_xml); -PHP_FUNCTION(libvirt_storagevolume_create_xml_from); -PHP_FUNCTION(libvirt_storagevolume_delete); -PHP_FUNCTION(libvirt_storagevolume_download); -PHP_FUNCTION(libvirt_storagevolume_upload); -PHP_FUNCTION(libvirt_storagevolume_resize); -PHP_FUNCTION(libvirt_storagepool_get_uuid_string); -PHP_FUNCTION(libvirt_storagepool_get_name); -PHP_FUNCTION(libvirt_storagepool_lookup_by_uuid_string); -PHP_FUNCTION(libvirt_storagepool_get_xml_desc); -PHP_FUNCTION(libvirt_storagepool_define_xml); -PHP_FUNCTION(libvirt_storagepool_undefine); -PHP_FUNCTION(libvirt_storagepool_create); -PHP_FUNCTION(libvirt_storagepool_destroy); -PHP_FUNCTION(libvirt_storagepool_is_active); -PHP_FUNCTION(libvirt_storagepool_get_volume_count); -PHP_FUNCTION(libvirt_storagepool_refresh); -PHP_FUNCTION(libvirt_storagepool_set_autostart); -PHP_FUNCTION(libvirt_storagepool_get_autostart); -PHP_FUNCTION(libvirt_storagepool_build); -PHP_FUNCTION(libvirt_storagepool_delete); /* Nodedev functions */ PHP_FUNCTION(libvirt_nodedev_get); PHP_FUNCTION(libvirt_nodedev_capabilities); @@ -316,9 +270,6 @@ PHP_FUNCTION(libvirt_list_nodedevs); PHP_FUNCTION(libvirt_list_all_nwfilters); PHP_FUNCTION(libvirt_list_nwfilters); PHP_FUNCTION(libvirt_list_domain_snapshots); -PHP_FUNCTION(libvirt_list_storagepools); -PHP_FUNCTION(libvirt_list_active_storagepools); -PHP_FUNCTION(libvirt_list_inactive_storagepools); /* Common functions */ PHP_FUNCTION(libvirt_version); PHP_FUNCTION(libvirt_check_version); diff --git a/src/libvirt-storage.c b/src/libvirt-storage.c new file mode 100644 index 0000000..b620902 --- /dev/null +++ b/src/libvirt-storage.c @@ -0,0 +1,1129 @@ +/* + * libvirt-storage.c: The PHP bindings to libvirt storage API + * + * See COPYING for the license of this software + */ + +#include + +#include "libvirt-storage.h" +#include "libvirt-stream.h" + +DEBUG_INIT("storage"); + +void +php_libvirt_storagepool_dtor(virt_resource *rsrc TSRMLS_DC) +{ + php_libvirt_storagepool *pool =3D (php_libvirt_storagepool *)rsrc->ptr; + int rv =3D 0; + + if (pool !=3D NULL) { + if (pool->pool !=3D NULL) { + if (!check_resource_allocation(NULL, INT_RESOURCE_STORAGEPOOL,= pool->pool TSRMLS_CC)) { + pool->pool =3D NULL; + efree(pool); + return; + } + rv =3D virStoragePoolFree(pool->pool); + if (rv !=3D 0) { + DPRINTF("%s: virStoragePoolFree(%p) returned %d (%s)\n", _= _FUNCTION__, pool->pool, rv, LIBVIRT_G(last_error)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStoragePoo= lFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virStoragePoolFree(%p) completed successfully= \n", __FUNCTION__, pool->pool); + resource_change_counter(INT_RESOURCE_STORAGEPOOL, NULL, po= ol->pool, 0 TSRMLS_CC); + } + pool->pool =3D NULL; + } + efree(pool); + } +} + +void +php_libvirt_volume_dtor(virt_resource *rsrc TSRMLS_DC) +{ + php_libvirt_volume *volume =3D (php_libvirt_volume *)rsrc->ptr; + int rv =3D 0; + + if (volume !=3D NULL) { + if (volume->volume !=3D NULL) { + if (!check_resource_allocation(NULL, INT_RESOURCE_VOLUME, volu= me->volume TSRMLS_CC)) { + volume->volume =3D NULL; + efree(volume); + return; + } + rv =3D virStorageVolFree(volume->volume); + if (rv !=3D 0) { + DPRINTF("%s: virStorageVolFree(%p) returned %d (%s)\n", __= FUNCTION__, volume->volume, rv, LIBVIRT_G(last_error)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStorageVol= Free failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virStorageVolFree(%p) completed successfully\= n", __FUNCTION__, volume->volume); + resource_change_counter(INT_RESOURCE_VOLUME, NULL, volume-= >volume, 0 TSRMLS_CC); + } + volume->volume =3D NULL; + } + efree(volume); + } +} + +/* + * Function name: libvirt_storagepool_lookup_by_name + * Since version: 0.4.1(-1) + * Description: Function is used to lookup for storage pool by it's na= me + * Arguments: @res [resource]: libvirt connection resource + * @name [string]: storage pool name + * Returns: libvirt storagepool resource + */ +PHP_FUNCTION(libvirt_storagepool_lookup_by_name) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + strsize_t name_len; + char *name =3D NULL; + virStoragePoolPtr pool =3D NULL; + php_libvirt_storagepool *res_pool; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); + + if ((name =3D=3D NULL) || (name_len < 1)) + RETURN_FALSE; + pool =3D virStoragePoolLookupByName(conn->conn, name); + DPRINTF("%s: virStoragePoolLookupByName(%p, %s) returned %p\n", PHPFUN= C, conn->conn, name, pool); + if (pool =3D=3D NULL) + RETURN_FALSE; + + res_pool =3D (php_libvirt_storagepool *)emalloc(sizeof(php_libvirt_sto= ragepool)); + res_pool->pool =3D pool; + res_pool->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_pool->pool); + resource_change_counter(INT_RESOURCE_STORAGEPOOL, conn->conn, res_pool= ->pool, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_pool, le_libvirt_storagepool); +} + +/* + * Function name: libvirt_storagepool_lookup_by_volume + * Since version: 0.4.1(-1) + * Description: Function is used to lookup for storage pool by a volume + * Arguments: @res [volume]: volume resource of storage pool + * Returns: libvirt storagepool resource + */ +PHP_FUNCTION(libvirt_storagepool_lookup_by_volume) +{ + php_libvirt_volume *volume; + zval *zvolume; + virStoragePoolPtr pool =3D NULL; + php_libvirt_storagepool *res_pool; + + GET_VOLUME_FROM_ARGS("r", &zvolume); + + pool =3D virStoragePoolLookupByVolume(volume->volume); + DPRINTF("%s: virStoragePoolLookupByVolume(%p) returned %p\n", PHPFUNC,= volume->volume, pool); + if (pool =3D=3D NULL) + RETURN_FALSE; + + res_pool =3D (php_libvirt_storagepool *)emalloc(sizeof(php_libvirt_sto= ragepool)); + res_pool->pool =3D pool; + res_pool->conn =3D volume->conn; + + 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); + + VIRT_REGISTER_RESOURCE(res_pool, le_libvirt_storagepool); +} + +/* + * Function name: libvirt_storagepool_list_volumes + * Since version: 0.4.1(-1) + * Description: Function is used to list volumes in the specified stor= age pool + * Arguments: @res [resource]: libvirt storagepool resource + * Returns: list of storage volume names in the storage pool in an= array using default keys (indexes) + */ +PHP_FUNCTION(libvirt_storagepool_list_volumes) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + char **names =3D NULL; + int expectedcount =3D -1; + int i; + int count =3D -1; + + GET_STORAGEPOOL_FROM_ARGS("r", &zpool); + + if ((expectedcount =3D virStoragePoolNumOfVolumes(pool->pool)) < 0) + RETURN_FALSE; + + DPRINTF("%s: virStoragePoolNumOfVolumes(%p) returned %d\n", PHPFUNC, p= ool->pool, expectedcount); + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + + count =3D virStoragePoolListVolumes(pool->pool, names, expectedcount); + DPRINTF("%s: virStoragePoolListVolumes(%p, %p, %d) returned %d\n", PHP= FUNC, pool->pool, names, expectedcount, count); + array_init(return_value); + + if ((count !=3D expectedcount) || (count < 0)) + RETURN_FALSE; + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + + efree(names); +} + +/* + * Function name: libvirt_storagepool_get_info + * Since version: 0.4.1(-1) + * Description: Function is used to get information about the storage = pool + * Arguments: @res [resource]: libvirt storagepool resource + * Returns: storage pool information array of state, capacity, all= ocation and available space + */ +PHP_FUNCTION(libvirt_storagepool_get_info) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + virStoragePoolInfo poolInfo; + int retval; + + GET_STORAGEPOOL_FROM_ARGS("r", &zpool); + + retval =3D virStoragePoolGetInfo(pool->pool, &poolInfo); + DPRINTF("%s: virStoragePoolGetInfo(%p, ) returned %d\n", PHPFUNC= , pool->pool, retval); + if (retval !=3D 0) + RETURN_FALSE; + + array_init(return_value); + + // @todo: fix the long long returns + LONGLONG_INIT; + add_assoc_long(return_value, "state", (long)poolInfo.state); + LONGLONG_ASSOC(return_value, "capacity", poolInfo.capacity); + LONGLONG_ASSOC(return_value, "allocation", poolInfo.allocation); + LONGLONG_ASSOC(return_value, "available", poolInfo.available); +} + +/* + * Function name: libvirt_storagepool_get_uuid_string + * Since version: 0.4.1(-1) + * Description: Function is used to get storage pool by UUID string + * Arguments: @res [resource]: libvirt storagepool resource + * Returns: storagepool UUID string + */ +PHP_FUNCTION(libvirt_storagepool_get_uuid_string) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + char *uuid; + int retval; + + GET_STORAGEPOOL_FROM_ARGS("r", &zpool); + + uuid =3D (char *)emalloc(VIR_UUID_STRING_BUFLEN); + retval =3D virStoragePoolGetUUIDString(pool->pool, uuid); + DPRINTF("%s: virStoragePoolGetUUIDString(%p, %p) returned %d (%s)\n", = PHPFUNC, pool->pool, uuid, retval, uuid); + if (retval !=3D 0) + RETURN_FALSE; + + VIRT_RETVAL_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_storagepool_get_name + * Since version: 0.4.1(-1) + * Description: Function is used to get storage pool name from the sto= rage pool resource + * Arguments: @res [resource]: libvirt storagepool resource + * Returns: storagepool name string + */ +PHP_FUNCTION(libvirt_storagepool_get_name) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + const char *name =3D NULL; + + GET_STORAGEPOOL_FROM_ARGS("r", &zpool); + + name =3D virStoragePoolGetName(pool->pool); + DPRINTF("%s: virStoragePoolGetName(%p) returned %s\n", PHPFUNC, pool->= pool, name); + if (name =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETURN_STRING(name); +} + +/* + * Function name: libvirt_storagepool_lookup_by_uuid_string + * Since version: 0.4.1(-1) + * Description: Function is used to lookup for storage pool identified= by UUID string + * Arguments: @res [resource]: libvirt connection resource + * @uuid [string]: UUID string to look for storagepool + * Returns: libvirt storagepool resource + */ +PHP_FUNCTION(libvirt_storagepool_lookup_by_uuid_string) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + char *uuid =3D NULL; + strsize_t uuid_len; + virStoragePoolPtr storage =3D NULL; + php_libvirt_storagepool *res_pool; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); + + if ((uuid =3D=3D NULL) || (uuid_len < 1)) + RETURN_FALSE; + + storage =3D virStoragePoolLookupByUUIDString(conn->conn, uuid); + DPRINTF("%s: virStoragePoolLookupByUUIDString(%p, %s) returned %p\n", = PHPFUNC, conn->conn, uuid, storage); + if (storage =3D=3D NULL) + RETURN_FALSE; + + res_pool =3D (php_libvirt_storagepool *)emalloc(sizeof(php_libvirt_sto= ragepool)); + res_pool->pool =3D storage; + res_pool->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_pool->pool); + resource_change_counter(INT_RESOURCE_STORAGEPOOL, conn->conn, res_pool= ->pool, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_pool, le_libvirt_storagepool); +} + +/* + * Function name: libvirt_storagepool_get_xml_desc + * Since version: 0.4.1(-1), changed 0.4.2 + * Description: Function is used to get the XML description for the st= orage pool identified by res + * Arguments: @res [resource]: libvirt storagepool resource + * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL + * Returns: storagepool XML description string or result of xPath = expression + */ +PHP_FUNCTION(libvirt_storagepool_get_xml_desc) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + char *xml; + char *xpath =3D NULL; + char *tmp =3D NULL; + zend_long flags =3D 0; + strsize_t xpath_len; + int retval =3D -1; + + GET_STORAGEPOOL_FROM_ARGS("r|s", &zpool, &xpath, &xpath_len, &flags); + if (xpath_len < 1) + xpath =3D NULL; + + DPRINTF("%s: pool =3D %p, flags =3D %ld, xpath =3D %s\n", PHPFUNC, poo= l->pool, flags, xpath); + + xml =3D virStoragePoolGetXMLDesc(pool->pool, flags); + if (xml =3D=3D NULL) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + VIRT_RETVAL_STRING(xml); + } else { + VIRT_RETVAL_STRING(tmp); + } + + free(xml); + free(tmp); +} + +/* + * Function name: libvirt_storagepool_define_xml + * Since version: 0.4.1(-1) + * Description: Function is used to define the storage pool from XML s= tring and return it's resource + * Arguments: @res [resource]: libvirt connection resource + * @xml [string]: XML string definition of storagepool + * @flags [int]: flags to define XML + * Returns: libvirt storagepool resource + */ +PHP_FUNCTION(libvirt_storagepool_define_xml) +{ + php_libvirt_storagepool *res_pool =3D NULL; + php_libvirt_connection *conn =3D NULL; + zval *zconn; + virStoragePoolPtr pool =3D NULL; + char *xml; + strsize_t xml_len; + zend_long flags =3D 0; + + + GET_CONNECTION_FROM_ARGS("rs|l", &zconn, &xml, &xml_len, &flags); + + pool =3D virStoragePoolDefineXML(conn->conn, xml, (unsigned int)flags); + DPRINTF("%s: virStoragePoolDefineXML(%p, ) returned %p\n", PHPFUN= C, conn->conn, pool); + if (pool =3D=3D NULL) + RETURN_FALSE; + + res_pool =3D (php_libvirt_storagepool *)emalloc(sizeof(php_libvirt_sto= ragepool)); + res_pool->pool =3D pool; + res_pool->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_pool->pool); + resource_change_counter(INT_RESOURCE_STORAGEPOOL, conn->conn, res_pool= ->pool, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_pool, le_libvirt_storagepool); +} + +/* + * Function name: libvirt_storagepool_undefine + * Since version: 0.4.1(-1) + * Description: Function is used to undefine the storage pool identifi= ed by it's resource + * Arguments: @res [resource]: libvirt storagepool resource + * Returns: TRUE if success, FALSE on error + */ +PHP_FUNCTION(libvirt_storagepool_undefine) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + int retval =3D 0; + + GET_STORAGEPOOL_FROM_ARGS("r", &zpool); + + retval =3D virStoragePoolUndefine(pool->pool); + DPRINTF("%s: virStoragePoolUndefine(%p) returned %d\n", PHPFUNC, pool-= >pool, retval); + if (retval !=3D 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_storagepool_create + * Since version: 0.4.1(-1) + * Description: Function is used to create/start the storage pool + * Arguments: @res [resource]: libvirt storagepool resource + * Returns: TRUE if success, FALSE on error + */ +PHP_FUNCTION(libvirt_storagepool_create) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + int retval; + + GET_STORAGEPOOL_FROM_ARGS("r", &zpool); + + retval =3D virStoragePoolCreate(pool->pool, 0); + DPRINTF("%s: virStoragePoolCreate(%p, 0) returned %d\n", PHPFUNC, pool= ->pool, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_storagepool_destroy + * Since version: 0.4.1(-1) + * Description: Function is used to destory the storage pool + * Arguments: @res [resource]: libvirt storagepool resource + * Returns: TRUE if success, FALSE on error + */ +PHP_FUNCTION(libvirt_storagepool_destroy) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + int retval; + + GET_STORAGEPOOL_FROM_ARGS("r", &zpool); + + retval =3D virStoragePoolDestroy(pool->pool); + DPRINTF("%s: virStoragePoolDestroy(%p) returned %d\n", PHPFUNC, pool->= pool, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_storagepool_is_active + * Since version: 0.4.1(-1) + * Description: Function is used to get information whether storage po= ol is active or not + * Arguments: @res [resource]: libvirt storagepool resource + * Returns: result of virStoragePoolIsActive + */ +PHP_FUNCTION(libvirt_storagepool_is_active) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + + GET_STORAGEPOOL_FROM_ARGS("r", &zpool); + + RETURN_LONG(virStoragePoolIsActive(pool->pool)); +} + +/* + * Function name: libvirt_storagepool_get_volume_count + * Since version: 0.4.1(-1) + * Description: Function is used to get storage volume count in the st= orage pool + * Arguments: @res [resource]: libvirt storagepool resource + * Returns: number of volumes in the pool + */ +PHP_FUNCTION(libvirt_storagepool_get_volume_count) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + + GET_STORAGEPOOL_FROM_ARGS("r", &zpool); + + RETURN_LONG(virStoragePoolNumOfVolumes(pool->pool)); +} + +/* + * Function name: libvirt_storagepool_refresh + * Since version: 0.4.1(-1) + * Description: Function is used to refresh the storage pool informati= on + * Arguments: @res [resource]: libvirt storagepool resource + * @flags [int]: refresh flags + * Returns: TRUE if success, FALSE on error + */ +PHP_FUNCTION(libvirt_storagepool_refresh) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + zend_long flags =3D 0; + int retval; + + GET_STORAGEPOOL_FROM_ARGS("r|l", &zpool, &flags); + + retval =3D virStoragePoolRefresh(pool->pool, flags); + DPRINTF("%s: virStoragePoolRefresh(%p, %ld) returned %d\n", PHPFUNC, p= ool->pool, flags, retval); + if (retval < 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_storagepool_set_autostart + * Since version: 0.4.1(-1) + * Description: Function is used to set autostart of the storage pool + * Arguments: @res [resource]: libvirt storagepool resource + * @flags [int]: flags to set autostart + * Returns: result on setting storagepool autostart value + */ +PHP_FUNCTION(libvirt_storagepool_set_autostart) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + zend_bool flags =3D 0; + int retval; + + GET_STORAGEPOOL_FROM_ARGS("rb", &zpool, &flags); + + retval =3D virStoragePoolSetAutostart(pool->pool, flags); + DPRINTF("%s: virStoragePoolSetAutostart(%p, %d) returned %d\n", PHPFUN= C, pool->pool, flags, retval); + if (retval !=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_storagepool_get_autostart + * Since version: 0.4.1(-1) + * Description: Function is used to get autostart of the storage pool + * Arguments: @res [resource]: libvirt storagepool resource + * Returns: TRUE for autostart enabled, FALSE for autostart disabl= ed, FALSE with last_error set for error + */ +PHP_FUNCTION(libvirt_storagepool_get_autostart) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + int autostart; + + GET_STORAGEPOOL_FROM_ARGS("r", &zpool); + + if (virStoragePoolGetAutostart(pool->pool, &autostart) =3D=3D 0 && + autostart !=3D 0) + RETURN_TRUE; + + RETURN_FALSE; +} + +/* + * Function name: libvirt_storagepool_build + * Since version: 0.4.2 + * Description: Function is used to Build the underlying storage pool,= e.g. create the destination directory for NFS + * Arguments: @res [resource]: libvirt storagepool resource + * Returns: TRUE if success, FALSE on error + */ +PHP_FUNCTION(libvirt_storagepool_build) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + int flags =3D 0; + int retval; + + GET_STORAGEPOOL_FROM_ARGS("r", &zpool); + + retval =3D virStoragePoolBuild(pool->pool, flags); + DPRINTF("%s: virStoragePoolBuild(%p, %d) returned %d\n", PHPFUNC, pool= ->pool, flags, retval); + if (retval =3D=3D 0) + RETURN_TRUE; + + RETURN_FALSE; +} + +/* + * Function name: libvirt_storagepool_delete + * Since version: 0.4.6 + * Description: Function is used to Delete the underlying storage pool= , e.g. remove the destination directory for NFS + * Arguments: @res [resource]: libvirt storagepool resource + * Returns: TRUE if success, FALSE on error + */ +PHP_FUNCTION(libvirt_storagepool_delete) +{ + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + int flags =3D 0; + int retval; + + GET_STORAGEPOOL_FROM_ARGS("r", &zpool); + + retval =3D virStoragePoolDelete(pool->pool, flags); + DPRINTF("%s: virStoragePoolDelete(%p, %d) returned %d\n", PHPFUNC, poo= l->pool, flags, retval); + if (retval =3D=3D 0) + RETURN_TRUE; + + RETURN_FALSE; +} + +/* + * Function name: libvirt_storagevolume_lookup_by_name + * Since version: 0.4.1(-1) + * Description: Function is used to lookup for storage volume by it's = name + * Arguments: @res [resource]: libvirt storagepool resource + * @name [string]: name of the storage volume to look for + * Returns: libvirt storagevolume resource + */ +PHP_FUNCTION(libvirt_storagevolume_lookup_by_name) +{ + php_libvirt_storagepool *pool =3D NULL; + php_libvirt_volume *res_volume; + zval *zpool; + strsize_t name_len; + char *name =3D NULL; + virStorageVolPtr volume =3D NULL; + + GET_STORAGEPOOL_FROM_ARGS("rs", &zpool, &name, &name_len); + if ((name =3D=3D NULL) || (name_len < 1)) + RETURN_FALSE; + + volume =3D virStorageVolLookupByName(pool->pool, name); + DPRINTF("%s: virStorageVolLookupByName(%p, %s) returned %p\n", PHPFUNC= , pool->pool, name, volume); + if (volume =3D=3D NULL) + RETURN_FALSE; + + res_volume =3D (php_libvirt_volume *)emalloc(sizeof(php_libvirt_volume= )); + res_volume->volume =3D volume; + res_volume->conn =3D pool->conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_volume->volume); + resource_change_counter(INT_RESOURCE_VOLUME, pool->conn->conn, res_vol= ume->volume, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_volume, le_libvirt_volume); +} + +/* + * Function name: libvirt_storagevolume_lookup_by_path + * Since version: 0.4.1(-2) + * Description: Function is used to lookup for storage volume by it's = path + * Arguments: @res [resource]: libvirt connection resource + * @path [string]: path of the storage volume to look for + * Returns: libvirt storagevolume resource + */ +PHP_FUNCTION(libvirt_storagevolume_lookup_by_path) +{ + php_libvirt_connection *conn =3D NULL; + php_libvirt_volume *res_volume; + zval *zconn; + strsize_t name_len; + char *name =3D NULL; + virStorageVolPtr volume =3D NULL; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); + if ((name =3D=3D NULL) || (name_len < 1)) + RETURN_FALSE; + + volume =3D virStorageVolLookupByPath(conn->conn, name); + DPRINTF("%s: virStorageVolLookupByPath(%p, %s) returned %p\n", PHPFUNC= , conn->conn, name, volume); + if (volume =3D=3D NULL) { + set_error_if_unset("Cannot find storage volume on requested path" = TSRMLS_CC); + RETURN_FALSE; + } + + res_volume =3D (php_libvirt_volume *)emalloc(sizeof(php_libvirt_volume= )); + res_volume->volume =3D volume; + res_volume->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_volume->volume); + resource_change_counter(INT_RESOURCE_VOLUME, conn->conn, res_volume->v= olume, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_volume, le_libvirt_volume); +} + +/* + * Function name: libvirt_storagevolume_get_name + * Since version: 0.4.1(-2) + * Description: Function is used to get the storage volume name + * Arguments: @res [resource]: libvirt storagevolume resource + * Returns: storagevolume name + */ +PHP_FUNCTION(libvirt_storagevolume_get_name) +{ + php_libvirt_volume *volume =3D NULL; + zval *zvolume; + const char *retval; + + GET_VOLUME_FROM_ARGS("r", &zvolume); + + retval =3D virStorageVolGetName(volume->volume); + DPRINTF("%s: virStorageVolGetName(%p) returned %s\n", PHPFUNC, volume-= >volume, retval); + if (retval =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETURN_STRING(retval); +} + +/* + * Function name: libvirt_storagevolume_get_path + * Since version: 0.4.1(-2) + * Description: Function is used to get the storage volume path + * Arguments: @res [resource]: libvirt storagevolume resource + * Returns: storagevolume path + */ +PHP_FUNCTION(libvirt_storagevolume_get_path) +{ + php_libvirt_volume *volume =3D NULL; + zval *zvolume; + char *retval; + + GET_VOLUME_FROM_ARGS("r", &zvolume); + + retval =3D virStorageVolGetPath(volume->volume); + DPRINTF("%s: virStorageVolGetPath(%p) returned %s\n", PHPFUNC, volume-= >volume, retval); + if (retval =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETVAL_STRING(retval); + free(retval); +} + +/* + * Function name: libvirt_storagevolume_get_info + * Since version: 0.4.1(-1) + * Description: Function is used to get the storage volume information + * Arguments: @res [resource]: libvirt storagevolume resource + * Returns: storage volume information array of type, allocation a= nd capacity + */ +PHP_FUNCTION(libvirt_storagevolume_get_info) +{ + php_libvirt_volume *volume =3D NULL; + zval *zvolume; + virStorageVolInfo volumeInfo; + int retval; + + GET_VOLUME_FROM_ARGS("r", &zvolume); + + retval =3D virStorageVolGetInfo(volume->volume, &volumeInfo); + DPRINTF("%s: virStorageVolGetInfo(%p, ) returned %d\n", PHPFUNC,= volume->volume, retval); + if (retval !=3D 0) + RETURN_FALSE; + + array_init(return_value); + LONGLONG_INIT; + add_assoc_long(return_value, "type", (long)volumeInfo.type); + LONGLONG_ASSOC(return_value, "capacity", volumeInfo.capacity); + LONGLONG_ASSOC(return_value, "allocation", volumeInfo.allocation); +} + +/* + * Function name: libvirt_storagevolume_get_xml_desc + * Since version: 0.4.1(-1), changed 0.4.2 + * Description: Function is used to get the storage volume XML descrip= tion + * Arguments: @res [resource]: libvirt storagevolume resource + * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL + * @flags [int]: optional flags + * Returns: storagevolume XML description or result of xPath expre= ssion + */ +PHP_FUNCTION(libvirt_storagevolume_get_xml_desc) +{ + php_libvirt_volume *volume =3D NULL; + zval *zvolume; + char *tmp =3D NULL; + char *xml; + char *xpath =3D NULL; + strsize_t xpath_len; + zend_long flags =3D 0; + int retval =3D -1; + + GET_VOLUME_FROM_ARGS("rs|l", &zvolume, &xpath, &xpath_len, &flags); + if (xpath_len < 1) + xpath =3D NULL; + + DPRINTF("%s: volume =3D %p, xpath =3D %s, flags =3D %ld\n", PHPFUNC, v= olume->volume, xpath, flags); + + xml =3D virStorageVolGetXMLDesc(volume->volume, flags); + if (xml =3D=3D NULL) { + set_error_if_unset("Cannot get the XML description" TSRMLS_CC); + RETURN_FALSE; + } + + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + VIRT_RETVAL_STRING(xml); + } else { + VIRT_RETVAL_STRING(tmp); + } + + free(xml); + free(tmp); +} + +/* + * Function name: libvirt_storagevolume_create_xml + * Since version: 0.4.1(-1) + * Description: Function is used to create the new storage pool and re= turn the handle to new storage pool + * Arguments: @res [resource]: libvirt storagepool resource + * @xml [string]: XML string to create the storage volume= in the storage pool + * @flags [int]: virStorageVolCreateXML flags + * Returns: libvirt storagevolume resource + */ +PHP_FUNCTION(libvirt_storagevolume_create_xml) +{ + php_libvirt_volume *res_volume =3D NULL; + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + virStorageVolPtr volume =3D NULL; + char *xml; + zend_long flags =3D 0; + strsize_t xml_len; + + GET_STORAGEPOOL_FROM_ARGS("rs|l", &zpool, &xml, &xml_len, &flags); + + volume =3D virStorageVolCreateXML(pool->pool, xml, flags); + DPRINTF("%s: virStorageVolCreateXML(%p, , 0) returned %p\n", PHPF= UNC, pool->pool, volume); + if (volume =3D=3D NULL) + RETURN_FALSE; + + res_volume =3D (php_libvirt_volume *)emalloc(sizeof(php_libvirt_volume= )); + res_volume->volume =3D volume; + res_volume->conn =3D pool->conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_volume->volume); + resource_change_counter(INT_RESOURCE_VOLUME, pool->conn->conn, res_vol= ume->volume, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_volume, le_libvirt_volume); +} + +/* + * Function name: libvirt_storagevolume_create_xml_from + * Since version: 0.4.1(-2) + * Description: Function is used to clone the new storage volume into = pool from the orignial volume + * Arguments: @pool [resource]: libvirt storagepool resource + * @xml [string]: XML string to create the storage volume= in the storage pool + * @original_volume [resource]: libvirt storagevolume res= ource + * Returns: libvirt storagevolume resource + */ +PHP_FUNCTION(libvirt_storagevolume_create_xml_from) +{ + php_libvirt_volume *res_volume =3D NULL; + php_libvirt_storagepool *pool =3D NULL; + zval *zpool; + + php_libvirt_volume *pl_volume =3D NULL; + zval *zvolume; + + virStorageVolPtr volume =3D NULL; + char *xml; + strsize_t xml_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsr", &zpool, &x= ml, &xml_len, &zvolume) =3D=3D FAILURE) { + set_error("Invalid pool resource, XML or volume resouce" TSRMLS_CC= ); + RETURN_FALSE; + } + + VIRT_FETCH_RESOURCE(pool, php_libvirt_storagepool*, &zpool, PHP_LIBVIR= T_STORAGEPOOL_RES_NAME, le_libvirt_storagepool); + if ((pool =3D=3D NULL) || (pool->pool =3D=3D NULL)) + RETURN_FALSE; + VIRT_FETCH_RESOURCE(pl_volume, php_libvirt_volume*, &zvolume, PHP_LIBV= IRT_VOLUME_RES_NAME, le_libvirt_volume); + if ((pl_volume =3D=3D NULL) || (pl_volume->volume =3D=3D NULL)) + RETURN_FALSE; + resource_change_counter(INT_RESOURCE_VOLUME, NULL, pl_volume->volume, = 1 TSRMLS_CC); + + volume =3D virStorageVolCreateXMLFrom(pool->pool, xml, pl_volume->volu= me, 0); + DPRINTF("%s: virStorageVolCreateXMLFrom(%p, , %p, 0) returned %p\= n", PHPFUNC, pool->pool, pl_volume->volume, volume); + if (volume =3D=3D NULL) + RETURN_FALSE; + + res_volume =3D (php_libvirt_volume *)emalloc(sizeof(php_libvirt_volume= )); + res_volume->volume =3D volume; + res_volume->conn =3D pool->conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_volume->volume); + resource_change_counter(INT_RESOURCE_VOLUME, pool->conn->conn, res_vol= ume->volume, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_volume, le_libvirt_volume); +} + +/* + * Function name: libvirt_storagevolume_delete + * Since version: 0.4.2 + * Description: Function is used to delete to volume identified by it'= s resource + * Arguments: @res [resource]: libvirt storagevolume resource + * @flags [int]: optional flags for the storage volume de= letion for virStorageVolDelete() + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_storagevolume_delete) +{ + php_libvirt_volume *volume =3D NULL; + zval *zvolume; + zend_long flags =3D 0; + int retval =3D 0; + + GET_VOLUME_FROM_ARGS("r|l", &zvolume, &flags); + + retval =3D virStorageVolDelete(volume->volume, flags); + DPRINTF("%s: virStorageVolDelete(%p, %d) returned %d\n", PHPFUNC, volu= me->volume, (int) flags, retval); + if (retval !=3D 0) { + set_error_if_unset("Cannot delete storage volume" TSRMLS_CC); + RETURN_FALSE; + } + + RETURN_TRUE; +} + +/* + * Function name: libvirt_storagevolume_download + * Since version: 0.5.0 + * Description: Function is used to download volume identified by it's= resource + * Arguments: @res [resource]: libvirt storagevolume resource + * @stream [resource]: stream to use as output + * @offset [int]: position to start reading from + * @length [int] : limit on amount of data to download + * @flags [int]: optional flags for the storage volume do= wnload for virStorageVolDownload() + * Returns: int + */ +PHP_FUNCTION(libvirt_storagevolume_download) +{ + php_libvirt_volume *volume =3D NULL; + php_libvirt_stream *stream =3D NULL; + zval *zvolume; + zval *zstream; + zend_long flags =3D 0; + zend_long offset =3D 0; + zend_long length =3D 0; + int retval =3D -1; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|l|l|l", &zvol= ume, &zstream, &offset, &length, &flags) =3D=3D FAILURE) + RETURN_LONG(retval); + VIRT_FETCH_RESOURCE(volume, php_libvirt_volume*, &zvolume, PHP_LIBVIRT= _VOLUME_RES_NAME, le_libvirt_volume); + if ((volume =3D=3D NULL) || (volume->volume =3D=3D NULL)) + RETURN_LONG(retval); + VIRT_FETCH_RESOURCE(stream, php_libvirt_stream*, &zstream, PHP_LIBVIRT= _STREAM_RES_NAME, le_libvirt_stream); + if ((stream =3D=3D NULL) || (stream->stream =3D=3D NULL)) + RETURN_LONG(retval); + + retval =3D virStorageVolDownload(volume->volume, stream->stream, offse= t, length, flags); + DPRINTF("%s: virStorageVolDownload(%p, %p, %d, %d, %d) returned %d\n",= PHPFUNC, volume->volume, stream->stream, (int) offset, (int) length, (int)= flags, retval); + + if (retval =3D=3D -1) { + set_error("Cannot download from stream" TSRMLS_CC); + RETURN_LONG(retval); + } + + RETURN_LONG(retval); +} + +/* + * Function name: libvirt_storagevolume_upload + * Since version: 0.5.0 + * Description: Function is used to upload volume identified by it's r= esource + * Arguments: @res [resource]: libvirt storagevolume resource + * @stream [resource]: stream to use as input + * @offset [int]: position to start writing to + * @length [int] : limit on amount of data to upload + * @flags [int]: optional flags for the storage volume up= load for virStorageVolUpload() + * Returns: int + */ +PHP_FUNCTION(libvirt_storagevolume_upload) +{ + php_libvirt_volume *volume =3D NULL; + php_libvirt_stream *stream =3D NULL; + zval *zvolume; + zval *zstream; + zend_long flags =3D 0; + zend_long offset =3D 0; + zend_long length =3D 0; + int retval =3D -1; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|l|l|l", &zvol= ume, &zstream, &offset, &length, &flags) =3D=3D FAILURE) + RETURN_LONG(retval); + VIRT_FETCH_RESOURCE(volume, php_libvirt_volume*, &zvolume, PHP_LIBVIRT= _VOLUME_RES_NAME, le_libvirt_volume); + if ((volume =3D=3D NULL) || (volume->volume =3D=3D NULL)) + RETURN_LONG(retval); + VIRT_FETCH_RESOURCE(stream, php_libvirt_stream*, &zstream, PHP_LIBVIRT= _STREAM_RES_NAME, le_libvirt_stream); + if ((stream =3D=3D NULL) || (stream->stream =3D=3D NULL)) + RETURN_LONG(retval); + + retval =3D virStorageVolUpload(volume->volume, stream->stream, offset,= length, flags); + DPRINTF("%s: virStorageVolUpload(%p, %p, %d, %d, %d) returned %d\n", P= HPFUNC, volume->volume, stream->stream, (int) offset, (int) length, (int) f= lags, retval); + + if (retval =3D=3D -1) { + set_error_if_unset("Cannot upload storage volume" TSRMLS_CC); + RETURN_LONG(retval); + } + + RETURN_LONG(retval); +} + +/* + * Function name: libvirt_storagevolume_resize + * Since version: 0.5.0 + * Description: Function is used to resize volume identified by it's r= esource + * Arguments: @res [resource]: libvirt storagevolume resource + * @capacity [int]: capacity for the storage volume + * @flags [int]: optional flags for the storage volume re= size for virStorageVolResize() + * Returns: int + */ +PHP_FUNCTION(libvirt_storagevolume_resize) +{ + php_libvirt_volume *volume =3D NULL; + zval *zvolume; + zend_long flags =3D 0; + zend_long capacity =3D 0; + int retval =3D -1; + + GET_VOLUME_FROM_ARGS("rl|l", &zvolume, &capacity, &flags); + + retval =3D virStorageVolResize(volume->volume, capacity, flags); + DPRINTF("%s: virStorageVolResize(%p, %d, %d) returned %d\n", PHPFUNC, = volume->volume, (int) capacity, (int) flags, retval); + if (retval !=3D 0) { + set_error_if_unset("Cannot resize storage volume" TSRMLS_CC); + RETURN_LONG(retval); + } + + RETURN_LONG(retval); +} + +/* + * Function name: libvirt_list_storagepools + * Since version: 0.4.1(-1) + * Description: Function is used to list storage pools on the connecti= on + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt storagepool names array for the connection + */ +PHP_FUNCTION(libvirt_list_storagepools) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + char **names; + int i; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((expectedcount =3D virConnectNumOfStoragePools(conn->conn)) < 0) + RETURN_FALSE; + + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virConnectListStoragePools(conn->conn, names, expectedcount); + + if ((count !=3D expectedcount) || (count < 0)) { + efree(names); + RETURN_FALSE; + } + + array_init(return_value); + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + efree(names); + + + if ((expectedcount =3D virConnectNumOfDefinedStoragePools(conn->conn))= < 0) + RETURN_FALSE; + names =3D (char **)emalloc(expectedcount * sizeof(char *)); + count =3D virConnectListDefinedStoragePools(conn->conn, names, expecte= dcount); + if ((count !=3D expectedcount) || (count < 0)) { + efree(names); + RETURN_FALSE; + } + + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + efree(names); +} + +/* + * Function name: libvirt_list_active_storagepools + * Since version: 0.4.1(-1) + * Description: Function is used to list active storage pools on the c= onnection + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt storagepool names array for the connection + */ +PHP_FUNCTION(libvirt_list_active_storagepools) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + char **names; + int i; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((expectedcount =3D virConnectNumOfStoragePools(conn->conn)) < 0) + RETURN_FALSE; + + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virConnectListStoragePools(conn->conn, names, expectedcount); + + if ((count !=3D expectedcount) || (count < 0)) { + efree(names); + RETURN_FALSE; + } + array_init(return_value); + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + efree(names); +} + +/* + * Function name: libvirt_list_inactive_storagepools + * Since version: 0.4.1(-1) + * Description: Function is used to list inactive storage pools on the= connection + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt storagepool names array for the connection + */ +PHP_FUNCTION(libvirt_list_inactive_storagepools) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + char **names; + int i; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((expectedcount =3D virConnectNumOfDefinedStoragePools(conn->conn))= < 0) + RETURN_FALSE; + + names =3D (char **)emalloc(expectedcount * sizeof(char *)); + count =3D virConnectListDefinedStoragePools(conn->conn, names, expecte= dcount); + if ((count !=3D expectedcount) || (count < 0)) { + efree(names); + RETURN_FALSE; + } + + array_init(return_value); + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + efree(names); +} diff --git a/src/libvirt-storage.h b/src/libvirt-storage.h new file mode 100644 index 0000000..2f6f5b3 --- /dev/null +++ b/src/libvirt-storage.h @@ -0,0 +1,138 @@ +/* + * libvirt-storage.h: The PHP bindings to libvirt storage API + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_STORAGE_H__ +# define __LIBVIRT_STORAGE_H__ + +# include "util.h" +# include "libvirt-connection.h" + +# define PHP_LIBVIRT_STORAGEPOOL_RES_NAME "Libvirt storagepool" +# define PHP_LIBVIRT_VOLUME_RES_NAME "Libvirt volume" +# define INT_RESOURCE_STORAGEPOOL 0x10 +# define INT_RESOURCE_VOLUME 0x20 + +# define PHP_FE_LIBVIRT_STORAGE = \ + PHP_FE(libvirt_storagepool_lookup_by_name, arginfo_libvirt_conn= _name) \ + PHP_FE(libvirt_storagepool_lookup_by_volume, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagepool_list_volumes, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagepool_get_info, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagepool_get_uuid_string, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagepool_get_name, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagepool_lookup_by_uuid_string, arginfo_libvirt_conn= _uuid) \ + PHP_FE(libvirt_storagepool_get_xml_desc, arginfo_libvirt_conn= _xpath) \ + PHP_FE(libvirt_storagepool_define_xml, arginfo_libvirt_stor= agepool_define_xml) \ + PHP_FE(libvirt_storagepool_undefine, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagepool_create, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagepool_destroy, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagepool_is_active, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagepool_get_volume_count, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagepool_refresh, arginfo_libvirt_conn= _optflags) \ + PHP_FE(libvirt_storagepool_set_autostart, arginfo_libvirt_conn= _flags) \ + PHP_FE(libvirt_storagepool_get_autostart, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagepool_build, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagepool_delete, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagevolume_lookup_by_name, arginfo_libvirt_conn= _name) \ + PHP_FE(libvirt_storagevolume_lookup_by_path, arginfo_libvirt_stor= agevolume_lookup_by_path) \ + PHP_FE(libvirt_storagevolume_get_name, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagevolume_get_path, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagevolume_get_info, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_storagevolume_get_xml_desc, arginfo_libvirt_stor= agevolume_get_xml_desc) \ + PHP_FE(libvirt_storagevolume_create_xml, arginfo_libvirt_conn= _xml) \ + PHP_FE(libvirt_storagevolume_create_xml_from, arginfo_libvirt_stor= agevolume_create_xml_from) \ + PHP_FE(libvirt_storagevolume_delete, arginfo_libvirt_conn= _optflags) \ + PHP_FE(libvirt_storagevolume_download, arginfo_libvirt_stor= agevolume_download) \ + PHP_FE(libvirt_storagevolume_upload, arginfo_libvirt_stor= agevolume_download) \ + PHP_FE(libvirt_storagevolume_resize, arginfo_libvirt_stor= agevolume_resize) \ + PHP_FE(libvirt_list_storagepools, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_list_active_storagepools, arginfo_libvirt_conn= ) \ + PHP_FE(libvirt_list_inactive_storagepools, arginfo_libvirt_conn) + +# define GET_STORAGEPOOL_FROM_ARGS(args, ...) = \ + do { = \ + reset_error(TSRMLS_C); = \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, = \ + args, = \ + __VA_ARGS__) =3D=3D FAILURE) { = \ + set_error("Invalid arguments" TSRMLS_CC); = \ + RETURN_FALSE; = \ + } = \ + = \ + VIRT_FETCH_RESOURCE(pool, php_libvirt_storagepool*, &zpool, = \ + PHP_LIBVIRT_STORAGEPOOL_RES_NAME, = \ + le_libvirt_storagepool); = \ + if ((pool =3D=3D NULL) || (pool->pool =3D=3D NULL)) = \ + RETURN_FALSE; = \ + } while (0) = \ + +# define GET_VOLUME_FROM_ARGS(args, ...) = \ + do { = \ + reset_error(TSRMLS_C); = \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, = \ + args, = \ + __VA_ARGS__) =3D=3D FAILURE) { = \ + set_error("Invalid arguments" TSRMLS_CC); = \ + RETURN_FALSE; = \ + } = \ + = \ + VIRT_FETCH_RESOURCE(volume, php_libvirt_volume*, &zvolume, = \ + PHP_LIBVIRT_VOLUME_RES_NAME, le_libvirt_volume= ); \ + if ((volume =3D=3D NULL) || (volume->volume =3D=3D NULL)) = \ + RETURN_FALSE; = \ + } while (0) = \ + +int le_libvirt_storagepool; +int le_libvirt_volume; + +typedef struct _php_libvirt_storagepool { + virStoragePoolPtr pool; + php_libvirt_connection* conn; +} php_libvirt_storagepool; + +typedef struct _php_libvirt_volume { + virStorageVolPtr volume; + php_libvirt_connection* conn; +} php_libvirt_volume; + +void php_libvirt_storagepool_dtor(virt_resource *rsrc TSRMLS_DC); +void php_libvirt_volume_dtor(virt_resource *rsrc TSRMLS_DC); + +PHP_FUNCTION(libvirt_storagepool_lookup_by_name); +PHP_FUNCTION(libvirt_storagepool_lookup_by_volume); +PHP_FUNCTION(libvirt_storagepool_list_volumes); +PHP_FUNCTION(libvirt_storagepool_get_info); +PHP_FUNCTION(libvirt_storagepool_get_uuid_string); +PHP_FUNCTION(libvirt_storagepool_get_name); +PHP_FUNCTION(libvirt_storagepool_lookup_by_uuid_string); +PHP_FUNCTION(libvirt_storagepool_get_xml_desc); +PHP_FUNCTION(libvirt_storagepool_define_xml); +PHP_FUNCTION(libvirt_storagepool_undefine); +PHP_FUNCTION(libvirt_storagepool_create); +PHP_FUNCTION(libvirt_storagepool_destroy); +PHP_FUNCTION(libvirt_storagepool_is_active); +PHP_FUNCTION(libvirt_storagepool_get_volume_count); +PHP_FUNCTION(libvirt_storagepool_refresh); +PHP_FUNCTION(libvirt_storagepool_set_autostart); +PHP_FUNCTION(libvirt_storagepool_get_autostart); +PHP_FUNCTION(libvirt_storagepool_build); +PHP_FUNCTION(libvirt_storagepool_delete); +PHP_FUNCTION(libvirt_storagevolume_lookup_by_name); +PHP_FUNCTION(libvirt_storagevolume_lookup_by_path); +PHP_FUNCTION(libvirt_storagevolume_get_name); +PHP_FUNCTION(libvirt_storagevolume_get_path); +PHP_FUNCTION(libvirt_storagevolume_get_info); +PHP_FUNCTION(libvirt_storagevolume_get_xml_desc); +PHP_FUNCTION(libvirt_storagevolume_create_xml); +PHP_FUNCTION(libvirt_storagevolume_create_xml_from); +PHP_FUNCTION(libvirt_storagevolume_delete); +PHP_FUNCTION(libvirt_storagevolume_download); +PHP_FUNCTION(libvirt_storagevolume_upload); +PHP_FUNCTION(libvirt_storagevolume_resize); +PHP_FUNCTION(libvirt_list_storagepools); +PHP_FUNCTION(libvirt_list_active_storagepools); +PHP_FUNCTION(libvirt_list_inactive_storagepools); + +#endif --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 23:55:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 15016242967221002.6522838011917; Tue, 1 Aug 2017 14:51:36 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8D08FE977F; Tue, 1 Aug 2017 21:51:34 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 300DA61B8B; Tue, 1 Aug 2017 21:51:34 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 899E74BB79; Tue, 1 Aug 2017 21:51:33 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkWv9007911 for ; Tue, 1 Aug 2017 17:46:32 -0400 Received: by smtp.corp.redhat.com (Postfix) id 9D25218174; Tue, 1 Aug 2017 21:46:32 +0000 (UTC) Received: from mx1.redhat.com (ext-mx01.extmail.prod.ext.phx2.redhat.com [10.5.110.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 95BB018205 for ; Tue, 1 Aug 2017 21:46:32 +0000 (UTC) Received: from mail-qt0-f196.google.com (mail-qt0-f196.google.com [209.85.216.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F3D49159526 for ; Tue, 1 Aug 2017 21:46:24 +0000 (UTC) Received: by mail-qt0-f196.google.com with SMTP id t37so2931332qtg.2 for ; Tue, 01 Aug 2017 14:46:24 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.22 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:22 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 8D08FE977F Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 8D08FE977F Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aM0g0j7U" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com F3D49159526 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com F3D49159526 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=wNL+lS+AcvkOsSdWms0l5HfnhkogvuB2kaWcygSaNok=; b=aM0g0j7UmkOrdIjt5Ed4ZOK2UUEmYzBH0el2PBw2xX3d0ljPbsFfcjHdNquzjm7L5C UmkWOag6BNHlYQ0CG/j6nO39d7kRnOIILODpWzt1dIn4BkdRXut7wPlxZUNRMIUWwARe 3KR5iEg7xfpL0Z4D4JXgth8wVfw04gNBzPt+6vdnDb8VVgDQgvV/A5tJcZGzOYyB9LnZ c+lEmmmWA4Fqesa8NGUvDei/BP90wnYZhrSmMx2o2G9X4bmJcmVM6a28my0L2LSwd2bJ JkIJXxU6ssyJnEL5EpE09UnqgbWU2CmSf+Efm1mT8iGG9doa4uUXDe2KtHm+enVD0uHn AJfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=wNL+lS+AcvkOsSdWms0l5HfnhkogvuB2kaWcygSaNok=; b=TpxtnYH5L63IAiNR289yuUaHyjeFfdPGS9YZq9Ko3f8A4vSPpA9hCXhL0fKhP9c1iV cjw3J10R9MC+axXBpQ8HMt4SPmPxVShxRQBjY9fMCJXKzGZqowcC8qyqZsv045LOkaYr NMUleqZC+GikwnGC1GKRxtQoDmZF3CBZzEGzZgNJiw9dOim0sKy35dNvyY6ehXd1oZYe Eig7pa2g+lvURqzVQPgkBjQt21ufXZBAeICzmpOGDSdDD0JvWVfingc3U5c9e7YG6NOU kKmv8BRcAA/ZR1AOTucAZEvp9e2z1sIaJbC+Onaqd6xFQ0LIEwIBBcR95IJEiLHTuf/L aICA== X-Gm-Message-State: AIVw110X4EhA2t0x9PWQOmBff4SM/EIjSr2LJICqaCiPUxcQzGZ/LBR9 5SAyXh2Jr6xxQBSWmEQ= X-Received: by 10.200.34.162 with SMTP id f31mr27206567qta.132.1501623983602; Tue, 01 Aug 2017 14:46:23 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:11 -0400 Message-Id: <20170801214614.8915-11-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 01 Aug 2017 21:46:25 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 01 Aug 2017 21:46:25 +0000 (UTC) for IP:'209.85.216.196' DOMAIN:'mail-qt0-f196.google.com' HELO:'mail-qt0-f196.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.37 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.216.196 mail-qt0-f196.google.com 209.85.216.196 mail-qt0-f196.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.25 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 10/13] Split up the bindings for libvirt snapshot API X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 01 Aug 2017 21:51:35 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" --- src/Makefile.am | 1 + src/libvirt-php.c | 257 +--------------------------------------------= ---- src/libvirt-php.h | 18 +--- src/libvirt-snapshot.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-snapshot.h | 58 +++++++++++ 5 files changed, 305 insertions(+), 272 deletions(-) create mode 100644 src/libvirt-snapshot.c create mode 100644 src/libvirt-snapshot.h diff --git a/src/Makefile.am b/src/Makefile.am index 32b23cf..1b78011 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -26,6 +26,7 @@ libvirt_php_la_SOURCES =3D \ libvirt-node.c libvirt-node.h \ libvirt-stream.c libvirt-stream.h \ libvirt-domain.c libvirt-domain.h \ + libvirt-snapshot.c libvirt-snapshot.h \ libvirt-network.c libvirt-network.h \ libvirt-storage.c libvirt-storage.h libvirt_php_la_CFLAGS =3D \ diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 1f0d058..8814e54 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -24,6 +24,7 @@ #include "libvirt-node.h" #include "libvirt-stream.h" #include "libvirt-domain.h" +#include "libvirt-snapshot.h" #include "libvirt-network.h" #include "libvirt-storage.h" =20 @@ -40,7 +41,6 @@ const char *features_binaries[] =3D { NULL }; =20 /* ZEND thread safe per request globals definition */ int le_libvirt_nodedev; -int le_libvirt_snapshot; int le_libvirt_nwfilter; =20 ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_connect, 0, 0, 0) @@ -477,13 +477,7 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE_LIBVIRT_CONNECTION PHP_FE_LIBVIRT_STREAM PHP_FE_LIBVIRT_DOMAIN - /* Domain snapshot functions */ - PHP_FE(libvirt_domain_has_current_snapshot, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_domain_snapshot_create, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_domain_snapshot_get_xml, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_domain_snapshot_revert, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_domain_snapshot_delete, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_domain_snapshot_lookup_by_name, arginfo_libvirt_domain_= snapshot_lookup_by_name) + PHP_FE_LIBVIRT_SNAPSHOT PHP_FE_LIBVIRT_STORAGE PHP_FE_LIBVIRT_NETWORK PHP_FE_LIBVIRT_NODE @@ -503,7 +497,6 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE(libvirt_nwfilter_lookup_by_uuid_string, arginfo_libvirt_conn_uu= id) PHP_FE(libvirt_nwfilter_lookup_by_uuid, arginfo_libvirt_conn_uuid) /* List functions */ - PHP_FE(libvirt_list_domain_snapshots, arginfo_libvirt_conn_optf= lags) PHP_FE(libvirt_list_nodedevs, arginfo_libvirt_conn_optc= ap) PHP_FE(libvirt_list_all_nwfilters, arginfo_libvirt_conn) PHP_FE(libvirt_list_nwfilters, arginfo_libvirt_conn) @@ -1213,33 +1206,6 @@ static void php_libvirt_nodedev_dtor(virt_resource *= rsrc TSRMLS_DC) } } =20 -/* Destructor for snapshot resource */ -static void php_libvirt_snapshot_dtor(virt_resource *rsrc TSRMLS_DC) -{ - php_libvirt_snapshot *snapshot =3D (php_libvirt_snapshot *)rsrc->ptr; - int rv =3D 0; - - if (snapshot !=3D NULL) { - if (snapshot->snapshot !=3D NULL) { - if (!check_resource_allocation(NULL, INT_RESOURCE_SNAPSHOT, sn= apshot->snapshot TSRMLS_CC)) { - snapshot->snapshot =3D NULL; - efree(snapshot); - return; - } - rv =3D virDomainSnapshotFree(snapshot->snapshot); - if (rv !=3D 0) { - DPRINTF("%s: virDomainSnapshotFree(%p) returned %d\n", __F= UNCTION__, snapshot->snapshot, rv); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainSnap= shotFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); - } else { - DPRINTF("%s: virDomainSnapshotFree(%p) completed successfu= lly\n", __FUNCTION__, snapshot->snapshot); - resource_change_counter(INT_RESOURCE_SNAPSHOT, snapshot->d= omain->conn->conn, snapshot->snapshot, 0 TSRMLS_CC); - } - snapshot->snapshot =3D NULL; - } - efree(snapshot); - } -} - /* Destructor for nwfilter resource */ static void php_libvirt_nwfilter_dtor(virt_resource *rsrc TSRMLS_DC) { @@ -1606,19 +1572,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) RETURN_FALSE; = \ } while (0) =20 -#define GET_SNAPSHOT_FROM_ARGS(args, ...) = \ - do { = \ - reset_error(TSRMLS_C); = \ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_AR= GS__) =3D=3D FAILURE) { \ - set_error("Invalid arguments" TSRMLS_CC); = \ - RETURN_FALSE; = \ - } = \ - = \ - VIRT_FETCH_RESOURCE(snapshot, php_libvirt_snapshot*, &zsnapshot, P= HP_LIBVIRT_SNAPSHOT_RES_NAME, le_libvirt_snapshot);\ - if ((snapshot =3D=3D NULL) || (snapshot->snapshot =3D=3D NULL)) = \ - RETURN_FALSE; = \ - } while (0) = \ - #define GET_NWFILTER_FROM_ARGS(args, ...) = \ do { = \ reset_error(TSRMLS_C); = \ @@ -2606,212 +2559,6 @@ void parse_array(zval *arr, tVMDisk *disk, tVMNetwo= rk *network) } VIRT_FOREACH_END(); } =20 -/* - * Function name: libvirt_domain_has_current_snapshot - * Since version: 0.4.1(-2) - * Description: Function is used to get the information whether domain= has the current snapshot - * Arguments: @res [resource]: libvirt domain resource - * @flags [int]: libvirt snapshot flags - * Returns: TRUE is domain has the current snapshot, otherwise FAL= SE (you may need to check for error using libvirt_get_last_error()) - */ -PHP_FUNCTION(libvirt_domain_has_current_snapshot) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - retval =3D virDomainHasCurrentSnapshot(domain->domain, flags); - if (retval <=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_snapshot_lookup_by_name - * Since version: 0.4.1(-2) - * Description: This functions is used to lookup for the snapshot by i= t's name - * Arguments: @res [resource]: libvirt domain resource - * @name [string]: name of the snapshot to get the resour= ce - * @flags [int]: libvirt snapshot flags - * Returns: domain snapshot resource - */ -PHP_FUNCTION(libvirt_domain_snapshot_lookup_by_name) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - strsize_t name_len; - char *name =3D NULL; - zend_long flags =3D 0; - php_libvirt_snapshot *res_snapshot; - virDomainSnapshotPtr snapshot =3D NULL; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &name, &name_len, &flags); - - if ((name =3D=3D NULL) || (name_len < 1)) - RETURN_FALSE; - snapshot=3DvirDomainSnapshotLookupByName(domain->domain, name, flags); - if (snapshot =3D=3D NULL) - RETURN_FALSE; - - res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); - res_snapshot->domain =3D domain; - res_snapshot->snapshot =3D snapshot; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); - resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); -} - -/* - * Function name: libvirt_domain_snapshot_create - * Since version: 0.4.1(-2) - * Description: This function creates the domain snapshot for the doma= in identified by it's resource - * Arguments: @res [resource]: libvirt domain resource - * @flags [int]: libvirt snapshot flags - * Returns: domain snapshot resource - */ -PHP_FUNCTION(libvirt_domain_snapshot_create) -{ - php_libvirt_domain *domain =3D NULL; - php_libvirt_snapshot *res_snapshot; - zval *zdomain; - virDomainSnapshotPtr snapshot =3D NULL; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - snapshot =3D virDomainSnapshotCreateXML(domain->domain, "", flags); - DPRINTF("%s: virDomainSnapshotCreateXML(%p, ) returned %p\n", PHP= FUNC, domain->domain, snapshot); - if (snapshot =3D=3D NULL) - RETURN_FALSE; - - res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); - res_snapshot->domain =3D domain; - res_snapshot->snapshot =3D snapshot; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); - resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); -} - -/* - * Function name: libvirt_domain_snapshot_get_xml - * Since version: 0.4.1(-2) - * Description: Function is used to get the XML description of the sna= pshot identified by it's resource - * Arguments: @res [resource]: libvirt snapshot resource - * @flags [int]: libvirt snapshot flags - * Returns: XML description string for the snapshot - */ -PHP_FUNCTION(libvirt_domain_snapshot_get_xml) -{ - char *xml; - zval *zsnapshot; - php_libvirt_snapshot *snapshot; - zend_long flags =3D 0; - - GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); - - xml =3D virDomainSnapshotGetXMLDesc(snapshot->snapshot, flags); - if (xml =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETVAL_STRING(xml); - free(xml); -} - -/* - * Function name: libvirt_domain_snapshot_revert - * Since version: 0.4.1(-2) - * Description: Function is used to revert the domain state to the sta= te identified by the snapshot - * Arguments: @res [resource]: libvirt snapshot resource - * @flags [int]: libvirt snapshot flags - * Returns: TRUE on success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_snapshot_revert) -{ - zval *zsnapshot; - php_libvirt_snapshot *snapshot; - int ret; - zend_long flags =3D 0; - - GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); - - ret =3D virDomainRevertToSnapshot(snapshot->snapshot, flags); - DPRINTF("%s: virDomainRevertToSnapshot(%p, 0) returned %d\n", PHPFUNC,= snapshot->snapshot, ret); - if (ret =3D=3D -1) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_snapshot_delete - * Since version: 0.4.1(-2) - * Description: Function is used to revert the domain state to the sta= te identified by the snapshot - * Arguments: @res [resource]: libvirt snapshot resource - * @flags [int]: 0 to delete just snapshot, VIR_SNAPSHOT_= DELETE_CHILDREN to delete snapshot children as well - * Returns: TRUE on success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_snapshot_delete) -{ - zval *zsnapshot; - php_libvirt_snapshot *snapshot; - zend_long flags =3D 0; - int retval; - - GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); - - retval =3D virDomainSnapshotDelete(snapshot->snapshot, flags); - DPRINTF("%s: virDomainSnapshotDelete(%p, %d) returned %d\n", PHPFUNC, = snapshot->snapshot, (int) flags, retval); - if (retval =3D=3D -1) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_list_domain_snapshots - * Since version: 0.4.1(-2) - * Description: Function is used to list domain snapshots for the doma= in specified by it's resource - * Arguments: @res [resource]: libvirt domain resource - * @flags [int]: libvirt snapshot flags - * Returns: libvirt domain snapshot names array - */ -PHP_FUNCTION(libvirt_list_domain_snapshots) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int count =3D -1; - int expectedcount =3D -1; - char **names; - zend_long flags =3D 0; - int i; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - expectedcount =3D virDomainSnapshotNum(domain->domain, flags); - DPRINTF("%s: virDomainSnapshotNum(%p, 0) returned %d\n", PHPFUNC, doma= in->domain, expectedcount); - - if (expectedcount !=3D -1) { - names =3D (char **)emalloc(expectedcount * sizeof(char *)); - count =3D virDomainSnapshotListNames(domain->domain, names, expect= edcount, 0); - } - - if ((count !=3D expectedcount) || (count < 0)) { - RETURN_FALSE; - } else { - array_init(return_value); - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - } - efree(names); -} - /* Listing functions */ /* * Function name: libvirt_list_nodedevs diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 1505878..ca10d23 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -129,7 +129,6 @@ typedef uint64_t arch_uint; =20 /* Internal resource identifier objects */ #define INT_RESOURCE_NODEDEV 0x08 -#define INT_RESOURCE_SNAPSHOT 0x40 #define INT_RESOURCE_NWFILTER 0x60 =20 typedef struct tTokenizer { @@ -159,16 +158,10 @@ typedef struct tVMNetwork { char *model; } tVMNetwork; =20 -/* TODO: temporary forward declarations until other parts are "modularized= " */ +/* TODO: temporary forward declaration until other parts are "modularized"= */ typedef struct _php_libvirt_connection php_libvirt_connection; -typedef struct _php_libvirt_domain php_libvirt_domain; =20 /* Libvirt-php types */ -typedef struct _php_libvirt_snapshot { - virDomainSnapshotPtr snapshot; - php_libvirt_domain* domain; -} php_libvirt_snapshot; - typedef struct _php_libvirt_nodedev { virNodeDevicePtr device; php_libvirt_connection* conn; @@ -232,7 +225,6 @@ long get_next_free_numeric_value(virDomainPtr domain, c= har *xpath); int get_subnet_bits(char *ip); =20 #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" =20 PHP_MINIT_FUNCTION(libvirt); @@ -243,13 +235,6 @@ PHP_MINFO_FUNCTION(libvirt); =20 /* Common functions */ PHP_FUNCTION(libvirt_get_last_error); -/* Domain snapshot functions */ -PHP_FUNCTION(libvirt_domain_has_current_snapshot); -PHP_FUNCTION(libvirt_domain_snapshot_create); -PHP_FUNCTION(libvirt_domain_snapshot_lookup_by_name); -PHP_FUNCTION(libvirt_domain_snapshot_get_xml); -PHP_FUNCTION(libvirt_domain_snapshot_revert); -PHP_FUNCTION(libvirt_domain_snapshot_delete); /* Nodedev functions */ PHP_FUNCTION(libvirt_nodedev_get); PHP_FUNCTION(libvirt_nodedev_capabilities); @@ -269,7 +254,6 @@ PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid); PHP_FUNCTION(libvirt_list_nodedevs); PHP_FUNCTION(libvirt_list_all_nwfilters); PHP_FUNCTION(libvirt_list_nwfilters); -PHP_FUNCTION(libvirt_list_domain_snapshots); /* Common functions */ PHP_FUNCTION(libvirt_version); PHP_FUNCTION(libvirt_check_version); diff --git a/src/libvirt-snapshot.c b/src/libvirt-snapshot.c new file mode 100644 index 0000000..c4c8d81 --- /dev/null +++ b/src/libvirt-snapshot.c @@ -0,0 +1,243 @@ +/* + * libvirt-snapshot.c: The PHP bindings to libvirt domain snapshot API + * + * See COPYING for the license of this software + */ + +#include + +#include "libvirt-snapshot.h" + +DEBUG_INIT("snapshot"); + +void php_libvirt_snapshot_dtor(virt_resource *rsrc TSRMLS_DC) +{ + php_libvirt_snapshot *snapshot =3D (php_libvirt_snapshot *)rsrc->ptr; + int rv =3D 0; + + if (snapshot !=3D NULL) { + if (snapshot->snapshot !=3D NULL) { + if (!check_resource_allocation(NULL, INT_RESOURCE_SNAPSHOT, sn= apshot->snapshot TSRMLS_CC)) { + snapshot->snapshot =3D NULL; + efree(snapshot); + return; + } + rv =3D virDomainSnapshotFree(snapshot->snapshot); + if (rv !=3D 0) { + DPRINTF("%s: virDomainSnapshotFree(%p) returned %d\n", __F= UNCTION__, snapshot->snapshot, rv); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainSnap= shotFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virDomainSnapshotFree(%p) completed successfu= lly\n", __FUNCTION__, snapshot->snapshot); + resource_change_counter(INT_RESOURCE_SNAPSHOT, snapshot->d= omain->conn->conn, snapshot->snapshot, 0 TSRMLS_CC); + } + snapshot->snapshot =3D NULL; + } + efree(snapshot); + } +} + +/* + * Function name: libvirt_domain_has_current_snapshot + * Since version: 0.4.1(-2) + * Description: Function is used to get the information whether domain= has the current snapshot + * Arguments: @res [resource]: libvirt domain resource + * @flags [int]: libvirt snapshot flags + * Returns: TRUE is domain has the current snapshot, otherwise FAL= SE (you may need to check for error using libvirt_get_last_error()) + */ +PHP_FUNCTION(libvirt_domain_has_current_snapshot) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); + + retval =3D virDomainHasCurrentSnapshot(domain->domain, flags); + if (retval <=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_snapshot_lookup_by_name + * Since version: 0.4.1(-2) + * Description: This functions is used to lookup for the snapshot by i= t's name + * Arguments: @res [resource]: libvirt domain resource + * @name [string]: name of the snapshot to get the resour= ce + * @flags [int]: libvirt snapshot flags + * Returns: domain snapshot resource + */ +PHP_FUNCTION(libvirt_domain_snapshot_lookup_by_name) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + strsize_t name_len; + char *name =3D NULL; + zend_long flags =3D 0; + php_libvirt_snapshot *res_snapshot; + virDomainSnapshotPtr snapshot =3D NULL; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &name, &name_len, &flags); + + if ((name =3D=3D NULL) || (name_len < 1)) + RETURN_FALSE; + snapshot=3DvirDomainSnapshotLookupByName(domain->domain, name, flags); + if (snapshot =3D=3D NULL) + RETURN_FALSE; + + res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); + res_snapshot->domain =3D domain; + res_snapshot->snapshot =3D snapshot; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); + resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); +} + +/* + * Function name: libvirt_domain_snapshot_create + * Since version: 0.4.1(-2) + * Description: This function creates the domain snapshot for the doma= in identified by it's resource + * Arguments: @res [resource]: libvirt domain resource + * @flags [int]: libvirt snapshot flags + * Returns: domain snapshot resource + */ +PHP_FUNCTION(libvirt_domain_snapshot_create) +{ + php_libvirt_domain *domain =3D NULL; + php_libvirt_snapshot *res_snapshot; + zval *zdomain; + virDomainSnapshotPtr snapshot =3D NULL; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); + + snapshot =3D virDomainSnapshotCreateXML(domain->domain, "", flags); + DPRINTF("%s: virDomainSnapshotCreateXML(%p, ) returned %p\n", PHP= FUNC, domain->domain, snapshot); + if (snapshot =3D=3D NULL) + RETURN_FALSE; + + res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); + res_snapshot->domain =3D domain; + res_snapshot->snapshot =3D snapshot; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); + resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); +} + +/* + * Function name: libvirt_domain_snapshot_get_xml + * Since version: 0.4.1(-2) + * Description: Function is used to get the XML description of the sna= pshot identified by it's resource + * Arguments: @res [resource]: libvirt snapshot resource + * @flags [int]: libvirt snapshot flags + * Returns: XML description string for the snapshot + */ +PHP_FUNCTION(libvirt_domain_snapshot_get_xml) +{ + char *xml; + zval *zsnapshot; + php_libvirt_snapshot *snapshot; + zend_long flags =3D 0; + + GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); + + xml =3D virDomainSnapshotGetXMLDesc(snapshot->snapshot, flags); + if (xml =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETVAL_STRING(xml); + free(xml); +} + +/* + * Function name: libvirt_domain_snapshot_revert + * Since version: 0.4.1(-2) + * Description: Function is used to revert the domain state to the sta= te identified by the snapshot + * Arguments: @res [resource]: libvirt snapshot resource + * @flags [int]: libvirt snapshot flags + * Returns: TRUE on success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_snapshot_revert) +{ + zval *zsnapshot; + php_libvirt_snapshot *snapshot; + int ret; + zend_long flags =3D 0; + + GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); + + ret =3D virDomainRevertToSnapshot(snapshot->snapshot, flags); + DPRINTF("%s: virDomainRevertToSnapshot(%p, 0) returned %d\n", PHPFUNC,= snapshot->snapshot, ret); + if (ret =3D=3D -1) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_snapshot_delete + * Since version: 0.4.1(-2) + * Description: Function is used to revert the domain state to the sta= te identified by the snapshot + * Arguments: @res [resource]: libvirt snapshot resource + * @flags [int]: 0 to delete just snapshot, VIR_SNAPSHOT_= DELETE_CHILDREN to delete snapshot children as well + * Returns: TRUE on success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_snapshot_delete) +{ + zval *zsnapshot; + php_libvirt_snapshot *snapshot; + zend_long flags =3D 0; + int retval; + + GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); + + retval =3D virDomainSnapshotDelete(snapshot->snapshot, flags); + DPRINTF("%s: virDomainSnapshotDelete(%p, %d) returned %d\n", PHPFUNC, = snapshot->snapshot, (int) flags, retval); + if (retval =3D=3D -1) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_list_domain_snapshots + * Since version: 0.4.1(-2) + * Description: Function is used to list domain snapshots for the doma= in specified by it's resource + * Arguments: @res [resource]: libvirt domain resource + * @flags [int]: libvirt snapshot flags + * Returns: libvirt domain snapshot names array + */ +PHP_FUNCTION(libvirt_list_domain_snapshots) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int count =3D -1; + int expectedcount =3D -1; + char **names; + zend_long flags =3D 0; + int i; + + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); + + expectedcount =3D virDomainSnapshotNum(domain->domain, flags); + DPRINTF("%s: virDomainSnapshotNum(%p, 0) returned %d\n", PHPFUNC, doma= in->domain, expectedcount); + + if (expectedcount !=3D -1) { + names =3D (char **)emalloc(expectedcount * sizeof(char *)); + count =3D virDomainSnapshotListNames(domain->domain, names, expect= edcount, 0); + } + + if ((count !=3D expectedcount) || (count < 0)) { + RETURN_FALSE; + } else { + array_init(return_value); + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + } + efree(names); +} diff --git a/src/libvirt-snapshot.h b/src/libvirt-snapshot.h new file mode 100644 index 0000000..e6092aa --- /dev/null +++ b/src/libvirt-snapshot.h @@ -0,0 +1,58 @@ +/* + * libvirt-snapshot.h: The PHP bindings to libvirt domain snapshot API + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_SNAPSHOT_H__ +# define __LIBVIRT_SNAPSHOT_H__ + +# include "libvirt-domain.h" + +# define PHP_LIBVIRT_SNAPSHOT_RES_NAME "Libvirt domain snapshot" +# define INT_RESOURCE_SNAPSHOT 0x40 + +# define PHP_FE_LIBVIRT_SNAPSHOT = \ + PHP_FE(libvirt_domain_has_current_snapshot, arginfo_libvirt_conn_op= tflags) \ + PHP_FE(libvirt_domain_snapshot_lookup_by_name, arginfo_libvirt_domain_= snapshot_lookup_by_name) \ + PHP_FE(libvirt_domain_snapshot_create, arginfo_libvirt_conn_op= tflags) \ + PHP_FE(libvirt_domain_snapshot_get_xml, arginfo_libvirt_conn_op= tflags) \ + PHP_FE(libvirt_domain_snapshot_revert, arginfo_libvirt_conn_op= tflags) \ + PHP_FE(libvirt_domain_snapshot_delete, arginfo_libvirt_conn_op= tflags) \ + PHP_FE(libvirt_list_domain_snapshots, arginfo_libvirt_conn_op= tflags) + +# define GET_SNAPSHOT_FROM_ARGS(args, ...) = \ + do { = \ + reset_error(TSRMLS_C); = \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, = \ + args, = \ + __VA_ARGS__) =3D=3D FAILURE) { = \ + set_error("Invalid arguments" TSRMLS_CC); = \ + RETURN_FALSE; = \ + } = \ + = \ + VIRT_FETCH_RESOURCE(snapshot, php_libvirt_snapshot*, &zsnapshot, = \ + PHP_LIBVIRT_SNAPSHOT_RES_NAME, = \ + le_libvirt_snapshot); = \ + if (snapshot =3D=3D NULL || snapshot->snapshot =3D=3D NULL) = \ + RETURN_FALSE; = \ + } while (0) = \ + +int le_libvirt_snapshot; + +typedef struct _php_libvirt_snapshot { + virDomainSnapshotPtr snapshot; + php_libvirt_domain* domain; +} php_libvirt_snapshot; + +void php_libvirt_snapshot_dtor(virt_resource *rsrc TSRMLS_DC); + +PHP_FUNCTION(libvirt_domain_has_current_snapshot); +PHP_FUNCTION(libvirt_domain_snapshot_lookup_by_name); +PHP_FUNCTION(libvirt_domain_snapshot_create); +PHP_FUNCTION(libvirt_domain_snapshot_get_xml); +PHP_FUNCTION(libvirt_domain_snapshot_revert); +PHP_FUNCTION(libvirt_domain_snapshot_delete); +PHP_FUNCTION(libvirt_list_domain_snapshots); + +#endif --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 23:55:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501624071374530.178770088587; Tue, 1 Aug 2017 14:47:51 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1F7D231CE55; Tue, 1 Aug 2017 21:47:49 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F0C4618206; Tue, 1 Aug 2017 21:47:48 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 9CF963888; Tue, 1 Aug 2017 21:47:48 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkXKf007926 for ; Tue, 1 Aug 2017 17:46:33 -0400 Received: by smtp.corp.redhat.com (Postfix) id A04CC18152; Tue, 1 Aug 2017 21:46:33 +0000 (UTC) Received: from mx1.redhat.com (ext-mx10.extmail.prod.ext.phx2.redhat.com [10.5.110.39]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9952217D5F for ; Tue, 1 Aug 2017 21:46:33 +0000 (UTC) Received: from mail-qk0-f176.google.com (mail-qk0-f176.google.com [209.85.220.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C706C25F951 for ; Tue, 1 Aug 2017 21:46:25 +0000 (UTC) Received: by mail-qk0-f176.google.com with SMTP id d136so17050951qkg.3 for ; Tue, 01 Aug 2017 14:46:25 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.23 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:23 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1F7D231CE55 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 1F7D231CE55 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mS+0Ex8w" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C706C25F951 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com C706C25F951 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=aX24wNK/H80PyLBeOnYHsGh9vWCfTkQ3pC0JmkXdjag=; b=mS+0Ex8wA/ISjNYnLbDL805+qSBXPgjUnTbOigWoeGK22W/UTOXtcik8LQwQO2NRPj LiOcWQcZuQraOQsapnBlBRR5EHVos+lKONssx/LQ4f85Mjvcv0cDRTgERMBCsqhNWUpD C4lomFFmVl+7cdcwb125EbBW/refNwQULaBAtUsbvzu0hvm8PuZhA49LlqGyWLegVfba F1jpjECfeL8X0rIkEt6YvINDJ1kQRBikjxoe/cmLWrVarp35cxiP17G8t7KlB1D5uDCZ XP65AGgtzKvukV2ouXMrpKW6VzqtM7UjXu2ZPePkZTM807Bd0QmduyxhvsXshTLJfbVp GGlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=aX24wNK/H80PyLBeOnYHsGh9vWCfTkQ3pC0JmkXdjag=; b=lfM6P1aXr7cl4k17zqvk+qolE6KgPcCu9m7UL1cBD9dMBe7we6e7uMUN2kfQ9dDR3u BwDcGU7ylXVyZ3+PlRQ5Q9bgeioxp9wQEDMCdBIZwILEkutxlEB7O5ayyXkRgqSkuwKC 2gWsX9lT8fGav4M9EI2qnV0jUKK97iKaVEAxK7/LV0dFbtZ+WzkKQg8hmBrHRVEDU0cC uI8KCCOEYbL3dAdCaYIWc6kc5ct98lqPb435HjhSahjsc6Ibj9IW6jauXMPJWG2CFw+v xc46aPx3/k+7ojL7LBFKhvuxXOOgB5IjsVRgrL7bLT89YPmKKQgzQyBjEuiT4Vgd4yG8 0AAg== X-Gm-Message-State: AIVw113akYG0LHrEo55RKt2k44rGBNGqfufgX2cN3gB9oVwle1Fmkiqi pFmUsEQkOWctL1aAnPw= X-Received: by 10.55.207.85 with SMTP id e82mr26022390qkj.235.1501623984362; Tue, 01 Aug 2017 14:46:24 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:12 -0400 Message-Id: <20170801214614.8915-12-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 01 Aug 2017 21:46:26 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 01 Aug 2017 21:46:26 +0000 (UTC) for IP:'209.85.220.176' DOMAIN:'mail-qk0-f176.google.com' HELO:'mail-qk0-f176.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: -3.21 (DCC_REPUT_00_12, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.220.176 mail-qk0-f176.google.com 209.85.220.176 mail-qk0-f176.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.39 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 11/13] Split up the bindings for libvirt nodedev API X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 01 Aug 2017 21:47:50 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" --- src/Makefile.am | 3 +- src/libvirt-nodedev.c | 339 ++++++++++++++++++++++++++++++++++++++++++++++= ++ src/libvirt-nodedev.h | 54 ++++++++ src/libvirt-php.c | 353 +---------------------------------------------= ---- src/libvirt-php.h | 13 -- 5 files changed, 398 insertions(+), 364 deletions(-) create mode 100644 src/libvirt-nodedev.c create mode 100644 src/libvirt-nodedev.h diff --git a/src/Makefile.am b/src/Makefile.am index 1b78011..30bebad 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,8 +27,9 @@ libvirt_php_la_SOURCES =3D \ libvirt-stream.c libvirt-stream.h \ libvirt-domain.c libvirt-domain.h \ libvirt-snapshot.c libvirt-snapshot.h \ + libvirt-storage.c libvirt-storage.h \ libvirt-network.c libvirt-network.h \ - libvirt-storage.c libvirt-storage.h + libvirt-nodedev.c libvirt-nodedev.h libvirt_php_la_CFLAGS =3D \ $(AM_CFLAGS) \ -DCOMPILE_DL_LIBVIRT=3D1 diff --git a/src/libvirt-nodedev.c b/src/libvirt-nodedev.c new file mode 100644 index 0000000..06a420d --- /dev/null +++ b/src/libvirt-nodedev.c @@ -0,0 +1,339 @@ +/* + * libvirt-nodedev.c: The PHP bindings to libvirt nodedev API + * + * See COPYING for the license of this software + */ + +#include + +#include "libvirt-nodedev.h" + +DEBUG_INIT("nodedev"); + +void +php_libvirt_nodedev_dtor(virt_resource *rsrc TSRMLS_DC) +{ + php_libvirt_nodedev *nodedev =3D (php_libvirt_nodedev *)rsrc->ptr; + int rv =3D 0; + + if (nodedev !=3D NULL) { + if (nodedev->device !=3D NULL) { + if (!check_resource_allocation(nodedev->conn->conn, INT_RESOUR= CE_NODEDEV, nodedev->device TSRMLS_CC)) { + nodedev->device =3D NULL; + efree(nodedev); + return; + } + rv =3D virNodeDeviceFree(nodedev->device); + if (rv !=3D 0) { + DPRINTF("%s: virNodeDeviceFree(%p) returned %d (%s)\n", __= FUNCTION__, nodedev->device, rv, LIBVIRT_G(last_error)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStorageVol= Free failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virNodeDeviceFree(%p) completed successfully\= n", __FUNCTION__, nodedev->device); + resource_change_counter(INT_RESOURCE_NODEDEV, nodedev->con= n->conn, nodedev->device, 0 TSRMLS_CC); + } + nodedev->device =3D NULL; + } + efree(nodedev); + } +} + +/* + * Function name: libvirt_nodedev_get + * Since version: 0.4.1(-1) + * Description: Function is used to get the node device by it's name + * Arguments: @res [resource]: libvirt connection resource + * @name [string]: name of the nodedev to get resource + * Returns: libvirt nodedev resource + */ +PHP_FUNCTION(libvirt_nodedev_get) +{ + php_libvirt_connection *conn =3D NULL; + php_libvirt_nodedev *res_dev =3D NULL; + virNodeDevice *dev; + zval *zconn; + char *name; + strsize_t name_len; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); + + if ((dev =3D virNodeDeviceLookupByName(conn->conn, name)) =3D=3D NULL)= { + set_error("Cannot get find requested node device" TSRMLS_CC); + RETURN_FALSE; + } + + res_dev =3D (php_libvirt_nodedev *)emalloc(sizeof(php_libvirt_nodedev)= ); + res_dev->device =3D dev; + res_dev->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_dev->device); + resource_change_counter(INT_RESOURCE_NODEDEV, conn->conn, res_dev->dev= ice, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_dev, le_libvirt_nodedev); +} + +/* + * Function name: libvirt_nodedev_capabilities + * Since version: 0.4.1(-1) + * Description: Function is used to list node devices by capabilities + * Arguments: @res [resource]: libvirt nodedev resource + * Returns: nodedev capabilities array + */ +PHP_FUNCTION(libvirt_nodedev_capabilities) +{ + php_libvirt_nodedev *nodedev =3D NULL; + zval *znodedev; + int count =3D -1; + int expectedcount =3D -1; + char **names; + int i; + + GET_NODEDEV_FROM_ARGS("r", &znodedev); + + if ((expectedcount =3D virNodeDeviceNumOfCaps(nodedev->device)) < 0) + RETURN_FALSE; + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virNodeDeviceListCaps(nodedev->device, names, expectedcount); + if ((count !=3D expectedcount) || (count < 0)) + RETURN_FALSE; + + array_init(return_value); + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + + efree(names); +} + +/* + * Function name: libvirt_nodedev_get_xml_desc + * Since version: 0.4.1(-1), changed 0.4.2 + * Description: Function is used to get the node device's XML descript= ion + * Arguments: @res [resource]: libvirt nodedev resource + * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL + * Returns: nodedev XML description string or result of xPath expr= ession + */ +PHP_FUNCTION(libvirt_nodedev_get_xml_desc) +{ + php_libvirt_nodedev *nodedev =3D NULL; + zval *znodedev; + char *tmp =3D NULL; + char *xml =3D NULL; + char *xpath =3D NULL; + strsize_t xpath_len; + int retval =3D -1; + + GET_NODEDEV_FROM_ARGS("r|s", &znodedev, &xpath, &xpath_len); + if (xpath_len < 1) + xpath =3D NULL; + + xml =3D virNodeDeviceGetXMLDesc(nodedev->device, 0); + if (!xml) { + set_error("Cannot get the device XML information" TSRMLS_CC); + RETURN_FALSE; + } + + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) + VIRT_RETVAL_STRING(xml); + else + VIRT_RETVAL_STRING(tmp); + + free(xml); + free(tmp); +} + +/* + * Function name: libvirt_nodedev_get_information + * Since version: 0.4.1(-1) + * Description: Function is used to get the node device's information + * Arguments: @res [resource]: libvirt nodedev resource + * Returns: nodedev information array + */ +PHP_FUNCTION(libvirt_nodedev_get_information) +{ + php_libvirt_nodedev *nodedev =3D NULL; + zval *znodedev; + int retval =3D -1; + char *xml =3D NULL; + char *tmp =3D NULL; + char *cap =3D NULL; + + GET_NODEDEV_FROM_ARGS("r", &znodedev); + + xml =3D virNodeDeviceGetXMLDesc(nodedev->device, 0); + if (!xml) { + set_error("Cannot get the device XML information" TSRMLS_CC); + RETURN_FALSE; + } + + array_init(return_value); + + /* Get name */ + tmp =3D get_string_from_xpath(xml, "//device/name", NULL, &retval); + if (tmp =3D=3D NULL) { + set_error("Invalid XPath node for device name" TSRMLS_CC); + goto error; + } + + if (retval < 0) { + set_error("Cannot get XPath expression result for device name" TSR= MLS_CC); + goto error; + } + + VIRT_ADD_ASSOC_STRING(return_value, "name", tmp); + + /* Get parent name */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/parent", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "parent", tmp); + + /* Get capability */ + cap =3D get_string_from_xpath(xml, "//device/capability/@type", NULL, = &retval); + if ((cap !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "capability", cap); + + /* System capability is having hardware and firmware sub-blocks */ + if (strcmp(cap, "system") =3D=3D 0) { + /* Get hardware vendor */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/v= endor", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "hardware_vendor", tmp); + + /* Get hardware version */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/v= ersion", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "hardware_version", tmp); + + /* Get hardware serial */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/s= erial", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "hardware_serial", tmp); + + /* Get hardware UUID */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/u= uid", NULL, &retval); + if (tmp !=3D NULL) + VIRT_ADD_ASSOC_STRING(return_value, "hardware_uuid", tmp); + + /* Get firmware vendor */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/firmware/v= endor", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "firmware_vendor", tmp); + + /* Get firmware version */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/firmware/v= ersion", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "firmware_version", tmp); + + /* Get firmware release date */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/firmware/r= elease_date", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "firmware_release_date", t= mp); + } + + /* Get product_id */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/product/@id", = NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "product_id", tmp); + + /* Get product_name */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/product", NULL= , &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "product_name", tmp); + + /* Get vendor_id */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/vendor/@id", N= ULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "vendor_id", tmp); + + /* Get vendor_name */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/vendor", NULL,= &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "vendor_name", tmp); + + /* Get driver name */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/driver/name", NULL, &retv= al); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "driver_name", tmp); + + /* Get driver name */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/interface", NU= LL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "interface_name", tmp); + + /* Get driver name */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/address", NULL= , &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "address", tmp); + + /* Get driver name */ + free(tmp); + tmp =3D get_string_from_xpath(xml, "//device/capability/capability/@ty= pe", NULL, &retval); + if ((tmp !=3D NULL) && (retval > 0)) + VIRT_ADD_ASSOC_STRING(return_value, "capabilities", tmp); + + free(cap); + free(tmp); + free(xml); + return; + + error: + free(cap); + free(tmp); + free(xml); + RETURN_FALSE; +} + +/* + * Function name: libvirt_list_nodedevs + * Since version: 0.4.1(-1) + * Description: Function is used to list node devices on the connection + * Arguments: @res [resource]: libvirt connection resource + * @cap [string]: optional capability string + * Returns: libvirt nodedev names array for the connection + */ +PHP_FUNCTION(libvirt_list_nodedevs) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + char *cap =3D NULL; + char **names; + int i; + strsize_t cap_len; + + GET_CONNECTION_FROM_ARGS("r|s", &zconn, &cap, &cap_len); + + if ((expectedcount =3D virNodeNumOfDevices(conn->conn, cap, 0)) < 0) + RETURN_FALSE; + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virNodeListDevices(conn->conn, cap, names, expectedcount, 0); + if ((count !=3D expectedcount) || (count < 0)) { + efree(names); + RETURN_FALSE; + } + + array_init(return_value); + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + + efree(names); +} diff --git a/src/libvirt-nodedev.h b/src/libvirt-nodedev.h new file mode 100644 index 0000000..350b213 --- /dev/null +++ b/src/libvirt-nodedev.h @@ -0,0 +1,54 @@ +/* + * libvirt-nodedev.h: The PHP bindings to libvirt nodedev API + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_NODEDEV_H__ +# define __LIBVIRT_NODEDEV_H__ + +# include "libvirt-connection.h" + +# define PHP_LIBVIRT_NODEDEV_RES_NAME "Libvirt node device" +# define INT_RESOURCE_NODEDEV 0x08 + +# define PHP_FE_LIBVIRT_NODEDEV = \ + PHP_FE(libvirt_nodedev_get, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_nodedev_capabilities, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_nodedev_get_xml_desc, arginfo_libvirt_conn_xpath) = \ + PHP_FE(libvirt_nodedev_get_information, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_list_nodedevs, arginfo_libvirt_conn_optcap) + +# define GET_NODEDEV_FROM_ARGS(args, ...) = \ + do { = \ + reset_error(TSRMLS_C); = \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, = \ + args, = \ + __VA_ARGS__) =3D=3D FAILURE) { = \ + set_error("Invalid arguments" TSRMLS_CC); = \ + RETURN_FALSE; = \ + } = \ + = \ + VIRT_FETCH_RESOURCE(nodedev, php_libvirt_nodedev*, &znodedev, = \ + PHP_LIBVIRT_NODEDEV_RES_NAME, le_libvirt_noded= ev); \ + if (nodedev =3D=3D NULL || nodedev->device =3D=3D NULL) = \ + RETURN_FALSE; = \ + } while (0) + +int le_libvirt_nodedev; + +typedef struct _php_libvirt_nodedev { + virNodeDevicePtr device; + php_libvirt_connection* conn; +} php_libvirt_nodedev; + + +void php_libvirt_nodedev_dtor(virt_resource *rsrc TSRMLS_DC); + +PHP_FUNCTION(libvirt_nodedev_get); +PHP_FUNCTION(libvirt_nodedev_capabilities); +PHP_FUNCTION(libvirt_nodedev_get_xml_desc); +PHP_FUNCTION(libvirt_nodedev_get_information); +PHP_FUNCTION(libvirt_list_nodedevs); + +#endif diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 8814e54..b62bcac 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -25,8 +25,9 @@ #include "libvirt-stream.h" #include "libvirt-domain.h" #include "libvirt-snapshot.h" -#include "libvirt-network.h" #include "libvirt-storage.h" +#include "libvirt-network.h" +#include "libvirt-nodedev.h" =20 DEBUG_INIT("core"); =20 @@ -40,7 +41,6 @@ const char *features_binaries[] =3D { NULL }; #endif =20 /* ZEND thread safe per request globals definition */ -int le_libvirt_nodedev; int le_libvirt_nwfilter; =20 ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_connect, 0, 0, 0) @@ -481,11 +481,7 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE_LIBVIRT_STORAGE PHP_FE_LIBVIRT_NETWORK PHP_FE_LIBVIRT_NODE - /* Nodedev functions */ - PHP_FE(libvirt_nodedev_get, arginfo_libvirt_conn) - PHP_FE(libvirt_nodedev_capabilities, arginfo_libvirt_conn) - PHP_FE(libvirt_nodedev_get_xml_desc, arginfo_libvirt_conn_xpat= h) - PHP_FE(libvirt_nodedev_get_information, arginfo_libvirt_conn) + PHP_FE_LIBVIRT_NODEDEV /* NWFilter functions */ PHP_FE(libvirt_nwfilter_define_xml, arginfo_libvirt_conn_xml) PHP_FE(libvirt_nwfilter_undefine, arginfo_libvirt_conn) @@ -497,7 +493,6 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE(libvirt_nwfilter_lookup_by_uuid_string, arginfo_libvirt_conn_uu= id) PHP_FE(libvirt_nwfilter_lookup_by_uuid, arginfo_libvirt_conn_uuid) /* List functions */ - PHP_FE(libvirt_list_nodedevs, arginfo_libvirt_conn_optc= ap) PHP_FE(libvirt_list_all_nwfilters, arginfo_libvirt_conn) PHP_FE(libvirt_list_nwfilters, arginfo_libvirt_conn) /* Version information and common function */ @@ -1179,33 +1174,6 @@ int is_local_connection(virConnectPtr conn) #endif } =20 -/* Destructor for nodedev resource */ -static void php_libvirt_nodedev_dtor(virt_resource *rsrc TSRMLS_DC) -{ - php_libvirt_nodedev *nodedev =3D (php_libvirt_nodedev *)rsrc->ptr; - int rv =3D 0; - - if (nodedev !=3D NULL) { - if (nodedev->device !=3D NULL) { - if (!check_resource_allocation(nodedev->conn->conn, INT_RESOUR= CE_NODEDEV, nodedev->device TSRMLS_CC)) { - nodedev->device =3D NULL; - efree(nodedev); - return; - } - rv =3D virNodeDeviceFree(nodedev->device); - if (rv !=3D 0) { - DPRINTF("%s: virNodeDeviceFree(%p) returned %d (%s)\n", __= FUNCTION__, nodedev->device, rv, LIBVIRT_G(last_error)); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStorageVol= Free failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); - } else { - DPRINTF("%s: virNodeDeviceFree(%p) completed successfully\= n", __FUNCTION__, nodedev->device); - resource_change_counter(INT_RESOURCE_NODEDEV, nodedev->con= n->conn, nodedev->device, 0 TSRMLS_CC); - } - nodedev->device =3D NULL; - } - efree(nodedev); - } -} - /* Destructor for nwfilter resource */ static void php_libvirt_nwfilter_dtor(virt_resource *rsrc TSRMLS_DC) { @@ -1559,19 +1527,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) } =20 /* Macros for obtaining resources from arguments */ -#define GET_NODEDEV_FROM_ARGS(args, ...) = \ - do { = \ - reset_error(TSRMLS_C); = \ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_AR= GS__) =3D=3D FAILURE) { \ - set_error("Invalid arguments" TSRMLS_CC); = \ - RETURN_FALSE; = \ - } = \ - = \ - VIRT_FETCH_RESOURCE(nodedev, php_libvirt_nodedev*, &znodedev, PHP_= LIBVIRT_NODEDEV_RES_NAME, le_libvirt_nodedev);\ - if ((nodedev =3D=3D NULL) || (nodedev->device =3D=3D NULL)) = \ - RETURN_FALSE; = \ - } while (0) - #define GET_NWFILTER_FROM_ARGS(args, ...) = \ do { = \ reset_error(TSRMLS_C); = \ @@ -2560,45 +2515,6 @@ void parse_array(zval *arr, tVMDisk *disk, tVMNetwor= k *network) } =20 /* Listing functions */ -/* - * Function name: libvirt_list_nodedevs - * Since version: 0.4.1(-1) - * Description: Function is used to list node devices on the connection - * Arguments: @res [resource]: libvirt connection resource - * @cap [string]: optional capability string - * Returns: libvirt nodedev names array for the connection - */ -PHP_FUNCTION(libvirt_list_nodedevs) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - char *cap =3D NULL; - char **names; - int i; - strsize_t cap_len; - - GET_CONNECTION_FROM_ARGS("r|s", &zconn, &cap, &cap_len); - - if ((expectedcount =3D virNodeNumOfDevices(conn->conn, cap, 0)) < 0) - RETURN_FALSE; - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virNodeListDevices(conn->conn, cap, names, expectedcount, 0); - if ((count !=3D expectedcount) || (count < 0)) { - efree(names); - RETURN_FALSE; - } - - array_init(return_value); - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - - efree(names); -} - =20 /* * Function name: libvirt_list_all_nwfilters @@ -2684,269 +2600,6 @@ PHP_FUNCTION(libvirt_list_nwfilters) if (!done) RETURN_FALSE; } -/* Nodedev functions */ - -/* - * Function name: libvirt_nodedev_get - * Since version: 0.4.1(-1) - * Description: Function is used to get the node device by it's name - * Arguments: @res [resource]: libvirt connection resource - * @name [string]: name of the nodedev to get resource - * Returns: libvirt nodedev resource - */ -PHP_FUNCTION(libvirt_nodedev_get) -{ - php_libvirt_connection *conn =3D NULL; - php_libvirt_nodedev *res_dev =3D NULL; - virNodeDevice *dev; - zval *zconn; - char *name; - strsize_t name_len; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); - - if ((dev =3D virNodeDeviceLookupByName(conn->conn, name)) =3D=3D NULL)= { - set_error("Cannot get find requested node device" TSRMLS_CC); - RETURN_FALSE; - } - - res_dev =3D (php_libvirt_nodedev *)emalloc(sizeof(php_libvirt_nodedev)= ); - res_dev->device =3D dev; - res_dev->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_dev->device); - resource_change_counter(INT_RESOURCE_NODEDEV, conn->conn, res_dev->dev= ice, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_dev, le_libvirt_nodedev); -} - -/* - * Function name: libvirt_nodedev_capabilities - * Since version: 0.4.1(-1) - * Description: Function is used to list node devices by capabilities - * Arguments: @res [resource]: libvirt nodedev resource - * Returns: nodedev capabilities array - */ -PHP_FUNCTION(libvirt_nodedev_capabilities) -{ - php_libvirt_nodedev *nodedev =3D NULL; - zval *znodedev; - int count =3D -1; - int expectedcount =3D -1; - char **names; - int i; - - GET_NODEDEV_FROM_ARGS("r", &znodedev); - - if ((expectedcount =3D virNodeDeviceNumOfCaps(nodedev->device)) < 0) - RETURN_FALSE; - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virNodeDeviceListCaps(nodedev->device, names, expectedcount); - if ((count !=3D expectedcount) || (count < 0)) - RETURN_FALSE; - - array_init(return_value); - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - - efree(names); -} - -/* - * Function name: libvirt_nodedev_get_xml_desc - * Since version: 0.4.1(-1), changed 0.4.2 - * Description: Function is used to get the node device's XML descript= ion - * Arguments: @res [resource]: libvirt nodedev resource - * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL - * Returns: nodedev XML description string or result of xPath expr= ession - */ -PHP_FUNCTION(libvirt_nodedev_get_xml_desc) -{ - php_libvirt_nodedev *nodedev =3D NULL; - zval *znodedev; - char *tmp =3D NULL; - char *xml =3D NULL; - char *xpath =3D NULL; - strsize_t xpath_len; - int retval =3D -1; - - GET_NODEDEV_FROM_ARGS("r|s", &znodedev, &xpath, &xpath_len); - if (xpath_len < 1) - xpath =3D NULL; - - xml =3D virNodeDeviceGetXMLDesc(nodedev->device, 0); - if (!xml) { - set_error("Cannot get the device XML information" TSRMLS_CC); - RETURN_FALSE; - } - - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) - VIRT_RETVAL_STRING(xml); - else - VIRT_RETVAL_STRING(tmp); - - free(xml); - free(tmp); -} - -/* - * Function name: libvirt_nodedev_get_information - * Since version: 0.4.1(-1) - * Description: Function is used to get the node device's information - * Arguments: @res [resource]: libvirt nodedev resource - * Returns: nodedev information array - */ -PHP_FUNCTION(libvirt_nodedev_get_information) -{ - php_libvirt_nodedev *nodedev =3D NULL; - zval *znodedev; - int retval =3D -1; - char *xml =3D NULL; - char *tmp =3D NULL; - char *cap =3D NULL; - - GET_NODEDEV_FROM_ARGS("r", &znodedev); - - xml =3D virNodeDeviceGetXMLDesc(nodedev->device, 0); - if (!xml) { - set_error("Cannot get the device XML information" TSRMLS_CC); - RETURN_FALSE; - } - - array_init(return_value); - - /* Get name */ - tmp =3D get_string_from_xpath(xml, "//device/name", NULL, &retval); - if (tmp =3D=3D NULL) { - set_error("Invalid XPath node for device name" TSRMLS_CC); - goto error; - } - - if (retval < 0) { - set_error("Cannot get XPath expression result for device name" TSR= MLS_CC); - goto error; - } - - VIRT_ADD_ASSOC_STRING(return_value, "name", tmp); - - /* Get parent name */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/parent", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "parent", tmp); - - /* Get capability */ - cap =3D get_string_from_xpath(xml, "//device/capability/@type", NULL, = &retval); - if ((cap !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "capability", cap); - - /* System capability is having hardware and firmware sub-blocks */ - if (strcmp(cap, "system") =3D=3D 0) { - /* Get hardware vendor */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/v= endor", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "hardware_vendor", tmp); - - /* Get hardware version */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/v= ersion", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "hardware_version", tmp); - - /* Get hardware serial */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/s= erial", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "hardware_serial", tmp); - - /* Get hardware UUID */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/hardware/u= uid", NULL, &retval); - if (tmp !=3D NULL) - VIRT_ADD_ASSOC_STRING(return_value, "hardware_uuid", tmp); - - /* Get firmware vendor */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/firmware/v= endor", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "firmware_vendor", tmp); - - /* Get firmware version */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/firmware/v= ersion", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "firmware_version", tmp); - - /* Get firmware release date */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/firmware/r= elease_date", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "firmware_release_date", t= mp); - } - - /* Get product_id */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/product/@id", = NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "product_id", tmp); - - /* Get product_name */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/product", NULL= , &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "product_name", tmp); - - /* Get vendor_id */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/vendor/@id", N= ULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "vendor_id", tmp); - - /* Get vendor_name */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/vendor", NULL,= &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "vendor_name", tmp); - - /* Get driver name */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/driver/name", NULL, &retv= al); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "driver_name", tmp); - - /* Get driver name */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/interface", NU= LL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "interface_name", tmp); - - /* Get driver name */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/address", NULL= , &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "address", tmp); - - /* Get driver name */ - free(tmp); - tmp =3D get_string_from_xpath(xml, "//device/capability/capability/@ty= pe", NULL, &retval); - if ((tmp !=3D NULL) && (retval > 0)) - VIRT_ADD_ASSOC_STRING(return_value, "capabilities", tmp); - - free(cap); - free(tmp); - free(xml); - return; - - error: - free(cap); - free(tmp); - free(xml); - RETURN_FALSE; -} =20 /* NWFilter functions */ =20 diff --git a/src/libvirt-php.h b/src/libvirt-php.h index ca10d23..676b29e 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -128,7 +128,6 @@ typedef uint64_t arch_uint; #define PHP_LIBVIRT_WORLD_EXTNAME "libvirt" =20 /* Internal resource identifier objects */ -#define INT_RESOURCE_NODEDEV 0x08 #define INT_RESOURCE_NWFILTER 0x60 =20 typedef struct tTokenizer { @@ -162,11 +161,6 @@ typedef struct tVMNetwork { typedef struct _php_libvirt_connection php_libvirt_connection; =20 /* Libvirt-php types */ -typedef struct _php_libvirt_nodedev { - virNodeDevicePtr device; - php_libvirt_connection* conn; -} php_libvirt_nodedev; - typedef struct _php_libvirt_nwfilter { virNWFilterPtr nwfilter; php_libvirt_connection* conn; @@ -224,7 +218,6 @@ const char *get_feature_binary(const char *name); long get_next_free_numeric_value(virDomainPtr domain, char *xpath); int get_subnet_bits(char *ip); =20 -#define PHP_LIBVIRT_NODEDEV_RES_NAME "Libvirt node device" #define PHP_LIBVIRT_NWFILTER_RES_NAME "Libvirt nwfilter" =20 PHP_MINIT_FUNCTION(libvirt); @@ -235,11 +228,6 @@ PHP_MINFO_FUNCTION(libvirt); =20 /* Common functions */ PHP_FUNCTION(libvirt_get_last_error); -/* Nodedev functions */ -PHP_FUNCTION(libvirt_nodedev_get); -PHP_FUNCTION(libvirt_nodedev_capabilities); -PHP_FUNCTION(libvirt_nodedev_get_xml_desc); -PHP_FUNCTION(libvirt_nodedev_get_information); /* NWFilter functions */ PHP_FUNCTION(libvirt_nwfilter_define_xml); PHP_FUNCTION(libvirt_nwfilter_undefine); @@ -251,7 +239,6 @@ PHP_FUNCTION(libvirt_nwfilter_lookup_by_name); PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid_string); PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid); /* Listing functions */ -PHP_FUNCTION(libvirt_list_nodedevs); PHP_FUNCTION(libvirt_list_all_nwfilters); PHP_FUNCTION(libvirt_list_nwfilters); /* Common functions */ --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 23:55:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501624320416119.38424753613515; Tue, 1 Aug 2017 14:52:00 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3576A64058; Tue, 1 Aug 2017 21:51:58 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0693460BE9; Tue, 1 Aug 2017 21:51:58 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 9D95A180B467; Tue, 1 Aug 2017 21:51:57 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkYlf007931 for ; Tue, 1 Aug 2017 17:46:34 -0400 Received: by smtp.corp.redhat.com (Postfix) id 3C61B4DA95; Tue, 1 Aug 2017 21:46:34 +0000 (UTC) Received: from mx1.redhat.com (ext-mx06.extmail.prod.ext.phx2.redhat.com [10.5.110.30]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3316C6375F for ; Tue, 1 Aug 2017 21:46:34 +0000 (UTC) Received: from mail-qt0-f180.google.com (mail-qt0-f180.google.com [209.85.216.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8ADDF64E79 for ; Tue, 1 Aug 2017 21:46:26 +0000 (UTC) Received: by mail-qt0-f180.google.com with SMTP id s6so17229021qtc.1 for ; Tue, 01 Aug 2017 14:46:26 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.24 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:24 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3576A64058 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 3576A64058 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="U6+LyA/4" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 8ADDF64E79 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 8ADDF64E79 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=C4tDplXtWtvXb0rTFm2c9isj83/zLD0n6E6A6Rw9LSY=; b=U6+LyA/4H06NkITNbBAItkyT6IP9YsVDuK6tYjwfFolhhIUu0+WFHDA9bG6EXQhejp TwMl9Y/6oZhk9yCeooO++aADnpiQxsZeCVb3zsNs8/DeT41NStFPQqQUcA7rtUBhQ3Bq 1eLPqvhVrBOsEpPy531H+Z0LW5WHIn17D0vePywC9l14zRT6feMMxzq7H+ODFEVOQXiv F1iqzf3m2w4WbKGVRXDCICPw9OStwsukBlGyqqPyVhbCx7ks5HuOW05C0HYwky+AVMrv 7XT1+cDidn+wGlJ7Gs1JrG2jv9Lqod05U6DHpDEoqb9jwS7RAUeoPqdLxLm2KqUfc1Wi dW+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=C4tDplXtWtvXb0rTFm2c9isj83/zLD0n6E6A6Rw9LSY=; b=qksMZXxzVBJnynLvaAVjqdd0cEYmB5LK72NkovuhJSllvoA2m8jIicv2OM6BDnKnUl rvI0hCrP6XFEnUY5bE0nzpCJftcKGtvcviIHoHQD618//3Py4IyAkYCZeOrvll20orey opQm1mIid0m9gxgOKE97AcbGzy+e0lfmq9Ukb6uRRJh7kA8Uq4zpTubmDLqYlTIj0WWb 8G4P0dUFu59ri0RQmczVht9DxlBm3wbvYc0Vq3HkIKcKDv3Ays/tRWxXbCgt4fao+Nrc BvpCf5O4ACuMyUoDeQa3SO7GzrxxvYXcXGCgJQod9LLOgS4t8pVphkCYjPy3HJOlOvca OGMA== X-Gm-Message-State: AIVw110dD0bFqGSJdDA7hCaS4dhE2ZVtSgjTWF1mjXQr3lf/1NB37IkG QpZ5Qfimyfy0eN9FnhM= X-Received: by 10.200.9.27 with SMTP id t27mr27874449qth.107.1501623985202; Tue, 01 Aug 2017 14:46:25 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:13 -0400 Message-Id: <20170801214614.8915-13-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 01 Aug 2017 21:46:26 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 01 Aug 2017 21:46:26 +0000 (UTC) for IP:'209.85.216.180' DOMAIN:'mail-qt0-f180.google.com' HELO:'mail-qt0-f180.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: -1.12 (DCC_REPUT_00_12, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.216.180 mail-qt0-f180.google.com 209.85.216.180 mail-qt0-f180.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.30 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 12/13] Split up the bindings for libvirt NWFilter API X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 01 Aug 2017 21:51:59 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" --- src/Makefile.am | 3 +- src/libvirt-nwfilter.c | 414 +++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-nwfilter.h | 66 ++++++++ src/libvirt-php.c | 445 +--------------------------------------------= ---- src/libvirt-php.h | 28 ---- 5 files changed, 486 insertions(+), 470 deletions(-) create mode 100644 src/libvirt-nwfilter.c create mode 100644 src/libvirt-nwfilter.h diff --git a/src/Makefile.am b/src/Makefile.am index 30bebad..707a1e8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -29,7 +29,8 @@ libvirt_php_la_SOURCES =3D \ libvirt-snapshot.c libvirt-snapshot.h \ libvirt-storage.c libvirt-storage.h \ libvirt-network.c libvirt-network.h \ - libvirt-nodedev.c libvirt-nodedev.h + libvirt-nodedev.c libvirt-nodedev.h \ + libvirt-nwfilter.c libvirt-nwfilter.h libvirt_php_la_CFLAGS =3D \ $(AM_CFLAGS) \ -DCOMPILE_DL_LIBVIRT=3D1 diff --git a/src/libvirt-nwfilter.c b/src/libvirt-nwfilter.c new file mode 100644 index 0000000..f610f40 --- /dev/null +++ b/src/libvirt-nwfilter.c @@ -0,0 +1,414 @@ +/* + * libvirt-nwfilter.c: The PHP bindings to libvirt NWFilter API + * + * See COPYING for the license of this software + */ + +#include + +#include "libvirt-nwfilter.h" + +DEBUG_INIT("nwfilter"); + +void +php_libvirt_nwfilter_dtor(virt_resource *rsrc TSRMLS_DC) +{ + php_libvirt_nwfilter *nwfilter =3D (php_libvirt_nwfilter *) rsrc->ptr; + int rv =3D 0; + + if (nwfilter !=3D NULL) { + if (nwfilter->nwfilter !=3D NULL) { + if (!check_resource_allocation(NULL, INT_RESOURCE_NWFILTER, nw= filter->nwfilter TSRMLS_CC)) { + nwfilter->nwfilter =3D NULL; + efree(nwfilter); + + return; + } + rv =3D virNWFilterFree(nwfilter->nwfilter); + if (rv !=3D 0) { + DPRINTF("%s: virNWFilterFree(%p) returned %d\n", __FUNCTIO= N__, nwfilter->nwfilter, rv); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virNWFilterFr= ee 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->c= onn->conn, nwfilter->nwfilter, 0 TSRMLS_CC); + } + nwfilter->nwfilter =3D NULL; + } + efree(nwfilter); + } +} + +/* + * 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 =3D NULL; + php_libvirt_nwfilter *res_nwfilter =3D NULL; + virNWFilter *nwfilter; + zval *zconn; + char *xml =3D NULL; + strsize_t xml_len; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &xml, &xml_len); + + if ((nwfilter =3D virNWFilterDefineXML(conn->conn, xml)) =3D=3D NULL) { + set_error_if_unset("Cannot define a new NWFilter" TSRMLS_CC); + RETURN_FALSE; + } + + res_nwfilter =3D (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_n= wfilter)); + res_nwfilter->nwfilter =3D nwfilter; + res_nwfilter->conn =3D 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 =3D NULL; + zval *znwfilter; + + GET_NWFILTER_FROM_ARGS("r", &znwfilter); + + if (virNWFilterUndefine(nwfilter->nwfilter) !=3D 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 nw= filter + * Arguments: @res [resource]: libvirt nwfilter resource + * @xpath [string]: optional xPath expression string to g= et 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 =3D NULL; + zval *znwfilter; + char *xml =3D NULL; + char *xpath =3D NULL; + char *tmp; + strsize_t xpath_len; + int retval =3D -1; + + GET_NWFILTER_FROM_ARGS("r|s", &znwfilter, &xpath, &xpath_len); + + if (xpath_len < 1) + xpath =3D NULL; + + xml =3D virNWFilterGetXMLDesc(nwfilter->nwfilter, 0); + + if (xml =3D=3D NULL) { + set_error_if_unset("Cannot get nwfilter XML" TSRMLS_CC); + RETURN_FALSE; + } + + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + + if (tmp =3D=3D NULL || retval < 0) + VIRT_RETVAL_STRING(xml); + else + VIRT_RETVAL_STRING(tmp); + + free(xml); + free(tmp); +} + +/* + * Function name: libvirt_nwfilter_get_uuid_string + * Since version: 0.5.4 + * Description: Function is used to get nwfilter's UUID in string form= at + * 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 =3D NULL; + zval *znwfilter; + char *uuid =3D NULL; + int ret =3D -1; + + GET_NWFILTER_FROM_ARGS("r", &znwfilter); + + uuid =3D (char *) emalloc(VIR_UUID_STRING_BUFLEN); + ret =3D virNWFilterGetUUIDString(nwfilter->nwfilter, uuid); + + DPRINTF("%s: virNWFilterGetUUIDString(%p) returned %d (%s)\n", PHPFUNC, + nwfilter->nwfilter, ret, uuid); + + if (ret !=3D 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 form= at + * 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 =3D NULL; + zval *znwfilter; + char *uuid =3D NULL; + int ret =3D -1; + + GET_NWFILTER_FROM_ARGS("r", &znwfilter); + + uuid =3D (char *) emalloc(VIR_UUID_BUFLEN); + ret =3D virNWFilterGetUUID(nwfilter->nwfilter, (unsigned char *) uuid); + + DPRINTF("%s: virNWFilterUUID(%p, %p) returned %d\n", PHPFUNC, + nwfilter->nwfilter, uuid, ret); + + if (ret !=3D 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 =3D NULL; + zval *znwfilter; + const char *name =3D NULL; + + GET_NWFILTER_FROM_ARGS("r", &znwfilter); + name =3D virNWFilterGetName(nwfilter->nwfilter); + + DPRINTF("%s: virNWFilterGetName(%p) returned %s\n", PHPFUNC, + nwfilter->nwfilter, name); + + if (name =3D=3D NULL) + RETURN_FALSE; + + /* name should not be freed as its lifetime is the same as nwfilter re= source */ + 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 i= t's name + * Arguments: @res [resource]: libvirt connection resource + * @name [string]: name of the nwfilter to get the resour= ce + * Returns: libvirt nwfilter resource + */ +PHP_FUNCTION(libvirt_nwfilter_lookup_by_name) +{ + php_libvirt_nwfilter *res_nwfilter =3D NULL; + php_libvirt_connection *conn =3D NULL; + virNWFilterPtr nwfilter =3D NULL; + zval *zconn; + strsize_t name_len; + char *name =3D NULL; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); + + if (name =3D=3D NULL || name_len < 1) + RETURN_FALSE; + + nwfilter =3D virNWFilterLookupByName(conn->conn, name); + + if (nwfilter =3D=3D NULL) + RETURN_FALSE; + + res_nwfilter =3D (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_n= wfilter)); + res_nwfilter->conn =3D conn; + res_nwfilter->nwfilter =3D 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 =3D NULL; + php_libvirt_connection *conn =3D NULL; + virNWFilterPtr nwfilter =3D NULL; + zval *zconn; + char *uuid =3D NULL; + strsize_t uuid_len; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); + + if (uuid =3D=3D NULL || uuid_len < 1) + RETURN_FALSE; + + nwfilter =3D virNWFilterLookupByUUIDString(conn->conn, uuid); + + if (nwfilter =3D=3D NULL) + RETURN_FALSE; + + res_nwfilter =3D (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_n= wfilter)); + res_nwfilter->conn =3D conn; + res_nwfilter->nwfilter =3D 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 i= n the binary format + * Arguments: @res [resource]: libvirt connection resource from libv= irt_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 =3D NULL; + php_libvirt_connection *conn =3D NULL; + virNWFilterPtr nwfilter =3D NULL; + zval *zconn; + strsize_t uuid_len; + unsigned char *uuid =3D NULL; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); + + if ((uuid =3D=3D NULL) || (uuid_len < 1)) + RETURN_FALSE; + + nwfilter =3D virNWFilterLookupByUUID(conn->conn, uuid); + + if (nwfilter =3D=3D NULL) + RETURN_FALSE; + + res_nwfilter =3D (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_n= wfilter)); + res_nwfilter->conn =3D conn; + res_nwfilter->nwfilter =3D 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_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 =3D NULL; + virNWFilterPtr *filters =3D NULL; + virNWFilterPtr nwfilter =3D NULL; + zval *zconn; + int count =3D -1; + size_t i =3D 0; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + /* in current libvirt version, flags are not used for this, so passing= 0 */ + if ((count =3D virConnectListAllNWFilters(conn->conn, &filters, 0)) < = 0) + RETURN_FALSE; + + DPRINTF("%s: Found %d nwfilters\n", PHPFUNC, count); + + array_init(return_value); + + for (i =3D 0; i < count; i++) { + nwfilter =3D filters[i]; + res_nwfilter =3D (php_libvirt_nwfilter *) emalloc(sizeof(php_libvi= rt_nwfilter)); + res_nwfilter->nwfilter =3D nwfilter; + res_nwfilter->conn =3D 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 =3D NULL; + zval *zconn; + int count =3D -1; + int expectedcount =3D -1; + char **names; + int i, done =3D 0; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + array_init(return_value); + + if ((expectedcount =3D virConnectNumOfNWFilters(conn->conn)) < 0) + RETURN_FALSE; + + names =3D (char **) emalloc(expectedcount * sizeof(char *)); + count =3D virConnectListNWFilters(conn->conn, names, expectedcount); + + if (count !=3D expectedcount || count < 0) { + efree(names); + DPRINTF("%s: virConnectListNWFilters returned %d filters, while %d= was " + "expected\n", PHPFUNC, count, expectedcount); + RETURN_FALSE; + } + + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + + efree(names); + done++; + + + if (!done) + RETURN_FALSE; +} diff --git a/src/libvirt-nwfilter.h b/src/libvirt-nwfilter.h new file mode 100644 index 0000000..c0cebb3 --- /dev/null +++ b/src/libvirt-nwfilter.h @@ -0,0 +1,66 @@ +/* + * libvirt-nwfilter.h: libvirt PHP binding for the NWFilter driver + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_NWFILTER_H__ +# define __LIBVIRT_NWFILTER_H__ + +# include "libvirt-connection.h" + +# define PHP_LIBVIRT_NWFILTER_RES_NAME "Libvirt nwfilter" +# define INT_RESOURCE_NWFILTER 0x60 + +# define PHP_FE_LIBVIRT_NWFILTER = \ + PHP_FE(libvirt_nwfilter_define_xml, arginfo_libvirt_conn_xm= l) \ + PHP_FE(libvirt_nwfilter_undefine, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_nwfilter_get_xml_desc, arginfo_libvirt_conn_xp= ath) \ + 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_na= me) \ + PHP_FE(libvirt_nwfilter_lookup_by_uuid_string, arginfo_libvirt_conn_uu= id) \ + PHP_FE(libvirt_nwfilter_lookup_by_uuid, arginfo_libvirt_conn_uu= id) \ + PHP_FE(libvirt_list_all_nwfilters, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_list_nwfilters, arginfo_libvirt_conn) + +# define GET_NWFILTER_FROM_ARGS(args, ...) = \ + do { = \ + reset_error(TSRMLS_C); = \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, = \ + args, = \ + __VA_ARGS__) =3D=3D FAILURE) { = \ + set_error("Invalid arguments" TSRMLS_CC); = \ + RETURN_FALSE; = \ + } = \ + = \ + VIRT_FETCH_RESOURCE(nwfilter, php_libvirt_nwfilter *, &znwfilter, = \ + PHP_LIBVIRT_NWFILTER_RES_NAME, = \ + le_libvirt_nwfilter); = \ + if ((nwfilter =3D=3D NULL) || (nwfilter->nwfilter =3D=3D NULL)) = \ + RETURN_FALSE; = \ + } while (0) = \ + +int le_libvirt_nwfilter; + +typedef struct _php_libvirt_nwfilter { + virNWFilterPtr nwfilter; + php_libvirt_connection* conn; +} php_libvirt_nwfilter; + +void php_libvirt_nwfilter_dtor(virt_resource *rsrc TSRMLS_DC); + +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); +PHP_FUNCTION(libvirt_list_all_nwfilters); +PHP_FUNCTION(libvirt_list_nwfilters); + +#endif diff --git a/src/libvirt-php.c b/src/libvirt-php.c index b62bcac..4ef06b2 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -28,6 +28,7 @@ #include "libvirt-storage.h" #include "libvirt-network.h" #include "libvirt-nodedev.h" +#include "libvirt-nwfilter.h" =20 DEBUG_INIT("core"); =20 @@ -40,9 +41,6 @@ const char *features[] =3D { NULL }; const char *features_binaries[] =3D { NULL }; #endif =20 -/* ZEND thread safe per request globals definition */ -int le_libvirt_nwfilter; - ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_connect, 0, 0, 0) ZEND_ARG_INFO(0, url) ZEND_ARG_INFO(0, readonly) @@ -472,8 +470,6 @@ ZEND_ARG_INFO(0, flags) ZEND_END_ARG_INFO() =20 static zend_function_entry libvirt_functions[] =3D { - /* Common functions */ - PHP_FE(libvirt_get_last_error, arginfo_libvirt_void) PHP_FE_LIBVIRT_CONNECTION PHP_FE_LIBVIRT_STREAM PHP_FE_LIBVIRT_DOMAIN @@ -482,19 +478,9 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE_LIBVIRT_NETWORK PHP_FE_LIBVIRT_NODE PHP_FE_LIBVIRT_NODEDEV - /* 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_xpat= h) - 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_uu= id) - PHP_FE(libvirt_nwfilter_lookup_by_uuid, arginfo_libvirt_conn_uuid) - /* List functions */ - PHP_FE(libvirt_list_all_nwfilters, arginfo_libvirt_conn) - PHP_FE(libvirt_list_nwfilters, arginfo_libvirt_conn) + PHP_FE_LIBVIRT_NWFILTER + /* Common functions */ + PHP_FE(libvirt_get_last_error, arginfo_libvirt_void) /* Version information and common function */ PHP_FE(libvirt_version, arginfo_libvirt_opttype) PHP_FE(libvirt_check_version, arginfo_libvirt_check_ver= sion) @@ -1174,34 +1160,6 @@ int is_local_connection(virConnectPtr conn) #endif } =20 -/* Destructor for nwfilter resource */ -static void php_libvirt_nwfilter_dtor(virt_resource *rsrc TSRMLS_DC) -{ - php_libvirt_nwfilter *nwfilter =3D (php_libvirt_nwfilter *) rsrc->ptr; - int rv =3D 0; - - if (nwfilter !=3D NULL) { - if (nwfilter->nwfilter !=3D NULL) { - if (!check_resource_allocation(NULL, INT_RESOURCE_NWFILTER, nw= filter->nwfilter TSRMLS_CC)) { - nwfilter->nwfilter =3D NULL; - efree(nwfilter); - - return; - } - rv =3D virNWFilterFree(nwfilter->nwfilter); - if (rv !=3D 0) { - DPRINTF("%s: virNWFilterFree(%p) returned %d\n", __FUNCTIO= N__, nwfilter->nwfilter, rv); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virNWFilterFr= ee 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->c= onn->conn, nwfilter->nwfilter, 0 TSRMLS_CC); - } - nwfilter->nwfilter =3D NULL; - } - efree(nwfilter); - } -} - /* ZEND Module inicialization function */ PHP_MINIT_FUNCTION(libvirt) { @@ -1526,22 +1484,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) return SUCCESS; } =20 -/* Macros for obtaining resources from arguments */ -#define GET_NWFILTER_FROM_ARGS(args, ...) = \ - do { = \ - reset_error(TSRMLS_C); = \ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_AR= GS__) =3D=3D FAILURE) { \ - set_error("Invalid arguments" TSRMLS_CC); = \ - RETURN_FALSE; = \ - } = \ - = \ - VIRT_FETCH_RESOURCE(nwfilter, php_libvirt_nwfilter *, &znwfilter, = \ - PHP_LIBVIRT_NWFILTER_RES_NAME, le_libvirt_nwfi= lter); \ - if ((nwfilter =3D=3D NULL) || (nwfilter->nwfilter =3D=3D NULL)) = \ - RETURN_FALSE; = \ - } while (0) = \ - - /* Common functions */ =20 /* @@ -2514,385 +2456,6 @@ void parse_array(zval *arr, tVMDisk *disk, tVMNetwo= rk *network) } VIRT_FOREACH_END(); } =20 -/* Listing functions */ - -/* - * 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 =3D NULL; - virNWFilterPtr *filters =3D NULL; - virNWFilterPtr nwfilter =3D NULL; - zval *zconn; - int count =3D -1; - size_t i =3D 0; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - /* in current libvirt version, flags are not used for this, so passing= 0 */ - if ((count =3D virConnectListAllNWFilters(conn->conn, &filters, 0)) < = 0) - RETURN_FALSE; - - DPRINTF("%s: Found %d nwfilters\n", PHPFUNC, count); - - array_init(return_value); - - for (i =3D 0; i < count; i++) { - nwfilter =3D filters[i]; - res_nwfilter =3D (php_libvirt_nwfilter *) emalloc(sizeof(php_libvi= rt_nwfilter)); - res_nwfilter->nwfilter =3D nwfilter; - res_nwfilter->conn =3D 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 =3D NULL; - zval *zconn; - int count =3D -1; - int expectedcount =3D -1; - char **names; - int i, done =3D 0; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - array_init(return_value); - - if ((expectedcount =3D virConnectNumOfNWFilters(conn->conn)) < 0) - RETURN_FALSE; - - names =3D (char **) emalloc(expectedcount * sizeof(char *)); - count =3D virConnectListNWFilters(conn->conn, names, expectedcount); - - if (count !=3D expectedcount || count < 0) { - efree(names); - DPRINTF("%s: virConnectListNWFilters returned %d filters, while %d= was " - "expected\n", PHPFUNC, count, expectedcount); - RETURN_FALSE; - } - - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - - efree(names); - done++; - - - if (!done) - RETURN_FALSE; -} - -/* 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 =3D NULL; - php_libvirt_nwfilter *res_nwfilter =3D NULL; - virNWFilter *nwfilter; - zval *zconn; - char *xml =3D NULL; - strsize_t xml_len; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &xml, &xml_len); - - if ((nwfilter =3D virNWFilterDefineXML(conn->conn, xml)) =3D=3D NULL) { - set_error_if_unset("Cannot define a new NWFilter" TSRMLS_CC); - RETURN_FALSE; - } - - res_nwfilter =3D (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_n= wfilter)); - res_nwfilter->nwfilter =3D nwfilter; - res_nwfilter->conn =3D 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 =3D NULL; - zval *znwfilter; - - GET_NWFILTER_FROM_ARGS("r", &znwfilter); - - if (virNWFilterUndefine(nwfilter->nwfilter) !=3D 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 nw= filter - * Arguments: @res [resource]: libvirt nwfilter resource - * @xpath [string]: optional xPath expression string to g= et 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 =3D NULL; - zval *znwfilter; - char *xml =3D NULL; - char *xpath =3D NULL; - char *tmp; - strsize_t xpath_len; - int retval =3D -1; - - GET_NWFILTER_FROM_ARGS("r|s", &znwfilter, &xpath, &xpath_len); - - if (xpath_len < 1) - xpath =3D NULL; - - xml =3D virNWFilterGetXMLDesc(nwfilter->nwfilter, 0); - - if (xml =3D=3D NULL) { - set_error_if_unset("Cannot get nwfilter XML" TSRMLS_CC); - RETURN_FALSE; - } - - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - - if (tmp =3D=3D NULL || retval < 0) - VIRT_RETVAL_STRING(xml); - else - VIRT_RETVAL_STRING(tmp); - - free(xml); - free(tmp); -} - -/* - * Function name: libvirt_nwfilter_get_uuid_string - * Since version: 0.5.4 - * Description: Function is used to get nwfilter's UUID in string form= at - * 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 =3D NULL; - zval *znwfilter; - char *uuid =3D NULL; - int ret =3D -1; - - GET_NWFILTER_FROM_ARGS("r", &znwfilter); - - uuid =3D (char *) emalloc(VIR_UUID_STRING_BUFLEN); - ret =3D virNWFilterGetUUIDString(nwfilter->nwfilter, uuid); - - DPRINTF("%s: virNWFilterGetUUIDString(%p) returned %d (%s)\n", PHPFUNC, - nwfilter->nwfilter, ret, uuid); - - if (ret !=3D 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 form= at - * 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 =3D NULL; - zval *znwfilter; - char *uuid =3D NULL; - int ret =3D -1; - - GET_NWFILTER_FROM_ARGS("r", &znwfilter); - - uuid =3D (char *) emalloc(VIR_UUID_BUFLEN); - ret =3D virNWFilterGetUUID(nwfilter->nwfilter, (unsigned char *) uuid); - - DPRINTF("%s: virNWFilterUUID(%p, %p) returned %d\n", PHPFUNC, - nwfilter->nwfilter, uuid, ret); - - if (ret !=3D 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 =3D NULL; - zval *znwfilter; - const char *name =3D NULL; - - GET_NWFILTER_FROM_ARGS("r", &znwfilter); - name =3D virNWFilterGetName(nwfilter->nwfilter); - - DPRINTF("%s: virNWFilterGetName(%p) returned %s\n", PHPFUNC, - nwfilter->nwfilter, name); - - if (name =3D=3D NULL) - RETURN_FALSE; - - /* name should not be freed as its lifetime is the same as nwfilter re= source */ - 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 i= t's name - * Arguments: @res [resource]: libvirt connection resource - * @name [string]: name of the nwfilter to get the resour= ce - * Returns: libvirt nwfilter resource - */ -PHP_FUNCTION(libvirt_nwfilter_lookup_by_name) -{ - php_libvirt_nwfilter *res_nwfilter =3D NULL; - php_libvirt_connection *conn =3D NULL; - virNWFilterPtr nwfilter =3D NULL; - zval *zconn; - strsize_t name_len; - char *name =3D NULL; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); - - if (name =3D=3D NULL || name_len < 1) - RETURN_FALSE; - - nwfilter =3D virNWFilterLookupByName(conn->conn, name); - - if (nwfilter =3D=3D NULL) - RETURN_FALSE; - - res_nwfilter =3D (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_n= wfilter)); - res_nwfilter->conn =3D conn; - res_nwfilter->nwfilter =3D 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 =3D NULL; - php_libvirt_connection *conn =3D NULL; - virNWFilterPtr nwfilter =3D NULL; - zval *zconn; - char *uuid =3D NULL; - strsize_t uuid_len; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); - - if (uuid =3D=3D NULL || uuid_len < 1) - RETURN_FALSE; - - nwfilter =3D virNWFilterLookupByUUIDString(conn->conn, uuid); - - if (nwfilter =3D=3D NULL) - RETURN_FALSE; - - res_nwfilter =3D (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_n= wfilter)); - res_nwfilter->conn =3D conn; - res_nwfilter->nwfilter =3D 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 i= n the binary format - * Arguments: @res [resource]: libvirt connection resource from libv= irt_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 =3D NULL; - php_libvirt_connection *conn =3D NULL; - virNWFilterPtr nwfilter =3D NULL; - zval *zconn; - strsize_t uuid_len; - unsigned char *uuid =3D NULL; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); - - if ((uuid =3D=3D NULL) || (uuid_len < 1)) - RETURN_FALSE; - - nwfilter =3D virNWFilterLookupByUUID(conn->conn, uuid); - - if (nwfilter =3D=3D NULL) - RETURN_FALSE; - - res_nwfilter =3D (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_n= wfilter)); - res_nwfilter->conn =3D conn; - res_nwfilter->nwfilter =3D 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 676b29e..82b70dc 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -127,9 +127,6 @@ typedef uint64_t arch_uint; #define PHP_LIBVIRT_WORLD_VERSION VERSION #define PHP_LIBVIRT_WORLD_EXTNAME "libvirt" =20 -/* Internal resource identifier objects */ -#define INT_RESOURCE_NWFILTER 0x60 - typedef struct tTokenizer { char **tokens; int numTokens; @@ -157,15 +154,6 @@ typedef struct tVMNetwork { char *model; } tVMNetwork; =20 -/* TODO: temporary forward declaration until other parts are "modularized"= */ -typedef struct _php_libvirt_connection php_libvirt_connection; - -/* Libvirt-php types */ -typedef struct _php_libvirt_nwfilter { - virNWFilterPtr nwfilter; - php_libvirt_connection* conn; -} php_libvirt_nwfilter; - typedef struct _php_libvirt_hash_key_info { char *name; unsigned int length; @@ -218,8 +206,6 @@ const char *get_feature_binary(const char *name); long get_next_free_numeric_value(virDomainPtr domain, char *xpath); int get_subnet_bits(char *ip); =20 -#define PHP_LIBVIRT_NWFILTER_RES_NAME "Libvirt nwfilter" - PHP_MINIT_FUNCTION(libvirt); PHP_MSHUTDOWN_FUNCTION(libvirt); PHP_RINIT_FUNCTION(libvirt); @@ -228,20 +214,6 @@ PHP_MINFO_FUNCTION(libvirt); =20 /* Common functions */ PHP_FUNCTION(libvirt_get_last_error); -/* 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_all_nwfilters); -PHP_FUNCTION(libvirt_list_nwfilters); -/* Common functions */ PHP_FUNCTION(libvirt_version); PHP_FUNCTION(libvirt_check_version); PHP_FUNCTION(libvirt_has_feature); --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 23:55:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501624019190522.9871345673769; Tue, 1 Aug 2017 14:46:59 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1D43D622A6; Tue, 1 Aug 2017 21:46:57 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E72E863770; Tue, 1 Aug 2017 21:46:56 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 9B5954A467; Tue, 1 Aug 2017 21:46:56 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkT01007880 for ; Tue, 1 Aug 2017 17:46:29 -0400 Received: by smtp.corp.redhat.com (Postfix) id 9036218173; Tue, 1 Aug 2017 21:46:29 +0000 (UTC) Received: from mx1.redhat.com (ext-mx01.extmail.prod.ext.phx2.redhat.com [10.5.110.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7F16818152 for ; Tue, 1 Aug 2017 21:46:27 +0000 (UTC) Received: from mail-qt0-f174.google.com (mail-qt0-f174.google.com [209.85.216.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DB3455DBB9 for ; Tue, 1 Aug 2017 21:46:26 +0000 (UTC) Received: by mail-qt0-f174.google.com with SMTP id t37so17089029qtg.5 for ; Tue, 01 Aug 2017 14:46:26 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.25 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:25 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1D43D622A6 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 1D43D622A6 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="as56e+lU" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com DB3455DBB9 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com DB3455DBB9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=+cUvqUubkDxb03ijxnnHCDW+Bz6SU/nmb/BImXQcgo0=; b=as56e+lU6x3qJapw2quPBcLOCtC8jW0ThDCeEPU6pkdBYr8dxFfRdlpvWJXFF4QOhp LBsN2Jc1Rsp4yT4XYeahTLk3ElOYmHPyHyUsfZl5E1lWIlwqm3qkC3UHldSllH/03AZs P1oDrjHwo1VXZfVnN0NL+jEuT4+ViZCRG81J8olDyw3JIYyNGjMjo65f4MUQkUYWO16r WovYWd4ozC79MFJQQFfdSwHJD0/cvTWiqOR2M+91lftCZMEQuZfVjf11EmygDx9HAdB+ LLckyn9gSBLjmgNdAM9jwQInNPJHHWXm1MJtYfXtmA/Q1Hd4LlPh2iFBDOYM0R9Y99ZZ qEtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=+cUvqUubkDxb03ijxnnHCDW+Bz6SU/nmb/BImXQcgo0=; b=TKAWyPiWVQfIPyFu0iKPe0tWm8j6KMwz+/+cqWOMywj8o9AK2rJAkBNgOop4/V3t/t 02u9zd3ZFDs38xoy6IHtytNYi6vlJpTbABIsMKvCpXYZUr6/iA1E/ZqCWdXRvy+JiPe2 SKEomAUotm/E+anc4aUia6WYr/8ZLkZtRrfbwNqyQgbAkCIvChju0kZqli0BYT6VyO5+ qU671e7jMtmacyxpE3YUGKCaKqdCM6vt4hz02mFzQsZmgRhZYbcwm86XtDGcFZ+wbIw+ iMXlH/o9FwZYdR1AAkg0QEZS9MRW5F0CAuoyQDFJy/3hkTUg+37Rg22IVJpdKvZV9Pg4 1Ebw== X-Gm-Message-State: AIVw111ADdzbzUeM6mGMBdZRm1/aSOE+hF0Os23F1gOLAlzPEEkDmUxn XraNM7Ezc1qolAXTyFo= X-Received: by 10.200.9.93 with SMTP id z29mr27687744qth.102.1501623985850; Tue, 01 Aug 2017 14:46:25 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:14 -0400 Message-Id: <20170801214614.8915-14-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 01 Aug 2017 21:46:27 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 01 Aug 2017 21:46:27 +0000 (UTC) for IP:'209.85.216.174' DOMAIN:'mail-qt0-f174.google.com' HELO:'mail-qt0-f174.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: -0.43 (DCC_REPUT_00_12, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.216.174 mail-qt0-f174.google.com 209.85.216.174 mail-qt0-f174.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.25 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 13/13] Fix is_local_connection implementation. X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 01 Aug 2017 21:46:57 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" As it was failing when local host is using FQDN for hostnames. The logic to do so follows libvirt's implementation for the same thing. This fixes an issue where unit tests would falsely fail on workstations that have FQDN hostnames. --- src/libvirt-php.c | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 4ef06b2..c019d99 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -1146,13 +1146,43 @@ int is_local_connection(virConnectPtr conn) { #ifndef EXTWIN int ret; - char *hostname; + char *lv_hostname =3D NULL, *result =3D NULL; char name[1024]; + struct addrinfo hints, *info =3D NULL; =20 - hostname =3D virConnectGetHostname(conn); + name[1023] =3D '\0'; gethostname(name, 1024); - ret =3D strcmp(name, hostname) =3D=3D 0; - free(hostname); + + if (strcmp(name, "localhost") =3D=3D 0) + return 1; + + lv_hostname =3D virConnectGetHostname(conn); + + /* gethostname gave us FQDN, compare */ + if (strchr(name, '.') && strcmp(name, lv_hostname) =3D=3D 0) + return 1; + + /* need to get FQDN of the local name */ + memset(&hints, 0, sizeof(hints)); + hints.ai_flags =3D AI_CANONNAME|AI_CANONIDN; + hints.ai_family =3D AF_UNSPEC; + + /* could not get FQDN or got localhost, use whatever gethostname gave = us */ + if (getaddrinfo(name, NULL, &hints, &info) !=3D 0 || + info->ai_canonname =3D=3D NULL || + strcmp(info->ai_canonname, "localhost") =3D=3D 0) + result =3D strdup(name); + else + result =3D strdup(info->ai_canonname); + + ret =3D strcmp(result, lv_hostname) =3D=3D 0; + + freeaddrinfo(info); + if (lv_hostname) + free(lv_hostname); + if (result) + free(result); + return ret; #else // Libvirt daemon doesn't work on Windows systems so always return 0 (= FALSE) --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list