From nobody Fri May 16 03:58:02 2025 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