php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #60896
Patch php5-pipe2 revision 2014-07-09 10:34 UTC by bandie9100 at gmail dot com

Patch php5-pipe2 for POSIX related Bug #60896

Patch version 2014-07-09 10:34 UTC

Return to Bug #60896 | Download this patch
Patch Revisions:

Developer: bandie9100@gmail.com

diff -Nuwr php5_5.4.4/ext/pipe2/config.m4 contrib/ext/pipe2/config.m4
--- php5_5.4.4/ext/pipe2/config.m4	1970-01-01 01:00:00.000000000 +0100
+++ contrib/ext/pipe2/config.m4	1404662134.-1249481112
@@ -0,0 +1,8 @@
+PHP_ARG_ENABLE(pipe2, whether to enable pipe(2) support,
+ [ --enable-pipe2   Enable pipe(2) support])
+
+
+if test "$PHP_PIPE2" = "yes"; then
+   AC_DEFINE(HAVE_PIPE2, 1, [Whether you have pipe2])
+   PHP_NEW_EXTENSION(pipe2, pipe2.c, $ext_shared)
+ fi
Binary files php5_5.4.4/ext/pipe2/modules/pipe2.so and contrib/ext/pipe2/modules/pipe2.so differ
diff -Nuwr php5_5.4.4/ext/pipe2/moduletest.php contrib/ext/pipe2/moduletest.php
--- php5_5.4.4/ext/pipe2/moduletest.php	1970-01-01 01:00:00.000000000 +0100
+++ contrib/ext/pipe2/moduletest.php	1404822047.-1249481112
@@ -0,0 +1,14 @@
+<?php
+$br = (php_sapi_name() == "cli")? "":"<br>";
+
+$module = 'pipe2';
+
+if(!extension_loaded( $module )) {
+	dl($module . '.' . PHP_SHLIB_SUFFIX);
+}
+$functions = get_extension_funcs($module);
+echo "Functions available in the test extension:$br\n";
+foreach($functions as $func) {
+    echo $func."$br\n";
+}
+echo "$br\n";
diff -Nuwr php5_5.4.4/ext/pipe2/phpapi.h contrib/ext/pipe2/phpapi.h
--- php5_5.4.4/ext/pipe2/phpapi.h	1970-01-01 01:00:00.000000000 +0100
+++ contrib/ext/pipe2/phpapi.h	1404900830.-1249481112
@@ -0,0 +1,31 @@
+
+/*
+  This code is copied from main/streams/plain_wrapper.c
+ */
+
+typedef struct {
+	FILE *file;
+	int fd;					/* underlying file descriptor */
+	unsigned is_process_pipe:1;	/* use pclose instead of fclose */
+	unsigned is_pipe:1;			/* don't try and seek */
+	unsigned cached_fstat:1;	/* sb is valid */
+	unsigned _reserved:29;
+
+	int lock_flag;			/* stores the lock state */
+	char *temp_file_name;	/* if non-null, this is the path to a temporary file that
+							 * is to be deleted when the stream is closed */
+#if HAVE_FLUSHIO
+	char last_op;
+#endif
+
+#if HAVE_MMAP
+	char *last_mapped_addr;
+	size_t last_mapped_len;
+#endif
+#ifdef PHP_WIN32
+	char *last_mapped_addr;
+	HANDLE file_mapping;
+#endif
+
+	struct stat sb;
+} php_stdio_stream_data;
diff -Nuwr php5_5.4.4/ext/pipe2/php_pipe2.h contrib/ext/pipe2/php_pipe2.h
--- php5_5.4.4/ext/pipe2/php_pipe2.h	1970-01-01 01:00:00.000000000 +0100
+++ contrib/ext/pipe2/php_pipe2.h	1404900559.-1249481112
@@ -0,0 +1,14 @@
+#ifndef PHP_PIPE2_H
+#define PHP_PIPE2_H 1
+
+#define PHP_PIPE2_VERSION "1.0"
+#define PHP_PIPE2_EXTNAME "pipe2"
+
+PHP_FUNCTION(posix_pipe);
+PHP_FUNCTION(stream_dup2);
+PHP_FUNCTION(posix_dup2);
+
+extern zend_module_entry pipe2_module_entry;
+#define phpext_pipe2_ptr &pipe2_module_entry
+
+#endif
diff -Nuwr php5_5.4.4/ext/pipe2/pipe2.c contrib/ext/pipe2/pipe2.c
--- php5_5.4.4/ext/pipe2/pipe2.c	1970-01-01 01:00:00.000000000 +0100
+++ contrib/ext/pipe2/pipe2.c	1404900590.-1249481112
@@ -0,0 +1,132 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "php.h"
+#include "php_pipe2.h"
+#include "phpapi.h"
+#include "../../main/streams/php_streams_int.h"
+
+
+static zend_function_entry pipe2_functions[] = {
+     PHP_FE(posix_pipe, NULL)
+     PHP_FE(posix_dup2, NULL)
+     PHP_FE(stream_dup2, NULL)
+     {NULL, NULL, NULL}
+};
+
+zend_module_entry pipe2_module_entry = {
+#if ZEND_MODULE_API_NO >= 20010901
+     STANDARD_MODULE_HEADER,
+#endif
+     PHP_PIPE2_EXTNAME,
+     pipe2_functions,
+     NULL,
+     NULL,
+     NULL,
+     NULL,
+     NULL,
+#if ZEND_MODULE_API_NO >= 20010901
+     PHP_PIPE2_VERSION,
+#endif
+     STANDARD_MODULE_PROPERTIES
+};
+
+#ifdef COMPILE_DL_PIPE2
+ZEND_GET_MODULE(pipe2)
+#endif
+
+
+
+static php_stream *pipe2_stream_from_fd(int fd, const char *mode)
+{
+	php_stdio_stream_data *self;
+	php_stream *stream;
+
+	self = emalloc_rel_orig(sizeof(*self));
+	memset(self, 0, sizeof(*self));
+	self->file = NULL;
+	self->is_pipe = 1;
+	self->lock_flag = /* LOCK_UN */ 8;
+	self->is_process_pipe = 0;
+	self->temp_file_name = NULL;
+	self->fd = fd;
+
+	stream = php_stream_alloc_rel(&php_stream_stdio_ops, self, NULL, mode);
+	stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
+	return stream;
+}
+
+
+PHP_FUNCTION(posix_pipe)
+{
+	int pipefd[2];
+	char *buf;
+
+	php_stream *stream_reader;
+	php_stream *stream_writer;
+	php_stdio_stream_data *abstract_stream;
+
+
+	if(pipe(pipefd) == 0)
+	{
+		stream_reader = pipe2_stream_from_fd(pipefd[0], "rb");
+		stream_writer = pipe2_stream_from_fd(pipefd[1], "w");
+
+		array_init(return_value);
+		add_next_index_resource(return_value, php_stream_get_resource_id(stream_reader));
+		add_next_index_resource(return_value, php_stream_get_resource_id(stream_writer));
+		add_next_index_long(return_value, pipefd[0]);
+		add_next_index_long(return_value, pipefd[1]);
+	}
+	else
+	{
+		RETURN_FALSE;
+	}
+}
+
+PHP_FUNCTION(stream_dup2)
+{
+	zval *zstream_old;
+	zval *zstream_new;
+	php_stream *stream_old;
+	php_stream *stream_new;
+	
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &zstream_old, &zstream_new) != SUCCESS)
+	{
+		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Stream type resources needed.");
+		RETURN_NULL();
+	}
+	
+	php_stream_from_zval(stream_old, &zstream_old);
+	php_stream_from_zval(stream_new, &zstream_new);
+	
+	php_stdio_stream_data *abstract_old = (php_stdio_stream_data*)stream_old->abstract;
+	php_stdio_stream_data *abstract_new = (php_stdio_stream_data*)stream_new->abstract;
+	
+	if(dup2(abstract_old->fd, abstract_new->fd) == -1)
+	{
+		RETURN_FALSE;
+	}
+	
+	RETURN_TRUE;
+}
+
+PHP_FUNCTION(posix_dup2)
+{
+	int fd_old;
+	int fd_new;
+	
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &fd_old, &fd_new) != SUCCESS)
+	{
+		RETURN_NULL();
+	}
+	
+	if(dup2(fd_old, fd_new) == -1)
+	{
+		RETURN_FALSE;
+	}
+	
+	RETURN_TRUE;
+}
+
diff -Nuwr php5_5.4.4/ext/pipe2/pipetest.php contrib/ext/pipe2/pipetest.php
--- php5_5.4.4/ext/pipe2/pipetest.php	1970-01-01 01:00:00.000000000 +0100
+++ contrib/ext/pipe2/pipetest.php	1404900684.-1249481112
@@ -0,0 +1,39 @@
+<?php
+
+$pipes = posix_pipe();
+var_dump($pipes);
+list($rdr, $wtr, $fd0, $fd1) = $pipes;
+
+$pid = pcntl_fork();
+if($pid==0)
+{
+	fclose($rdr);
+	fwrite($wtr, "fwrite wtr\n");
+	
+	//ob_end_clean();
+	//fclose(STDOUT);
+	//$stdout = fopen("php://fd/$fd1", "w");
+	
+	//$ok = stream_dup2($wtr, STDOUT);
+	$ok = posix_dup2($fd1, 1);
+	//var_dump($ok);
+
+	echo "echo\n";
+	
+	fwrite(STDOUT, "fwrite STDOUT\n");
+	file_put_contents("/dev/stdout", "/dev/stdout\n");
+	//file_put_contents("/proc/self/fd/1", "/dev/stdout\n");
+	
+	//foreach(glob("/proc/self/fd/*") as $f) var_dump(array($f,readlink($f)));
+	foreach(glob("/dev/std*") as $f) var_dump(array($f,readlink($f)));
+	
+	die;
+}
+else
+{
+	fclose($wtr);
+
+	while(!feof($rdr)) @$buf .= fread($rdr, 4096);
+	var_dump( $buf );
+}
+;
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 21:01:28 2024 UTC