php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #72409
Patch return-previous-handler revision 2016-06-18 08:20 UTC by dave at mudsite dot com
revision 2016-06-18 08:14 UTC by dave at mudsite dot com
revision 2016-06-18 08:11 UTC by dave at mudsite dot com

Patch return-previous-handler for PCNTL related Bug #72409

Patch version 2016-06-18 08:20 UTC

Return to Bug #72409 | Download this patch
This patch renders other patches obsolete

Obsolete patches:

Patch Revisions:

Developer: dave@mudsite.com

From aaa123a375e17a08f2f441b932e6165899edeb64 Mon Sep 17 00:00:00 2001
From: David Walker <dave@mudsite.com>
Date: Sat, 18 Jun 2016 01:13:25 -0600
Subject: [PATCH] Have pcntl_signal() return previously set handlers

---
 ext/pcntl/pcntl.c                 | 17 ++++++++++++++---
 ext/pcntl/tests/pcntl_signal.phpt |  7 ++++++-
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c
index 5592b31..e50913d 100644
--- a/ext/pcntl/pcntl.c
+++ b/ext/pcntl/pcntl.c
@@ -959,7 +959,7 @@ PHP_FUNCTION(pcntl_exec)
    Assigns a system signal handler to a PHP function */
 PHP_FUNCTION(pcntl_signal)
 {
-	zval *handle;
+	zval *handle, *prev;
 	zend_string *func_name;
 	zend_long signo;
 	zend_bool restart_syscalls = 1;
@@ -1009,9 +1009,17 @@ PHP_FUNCTION(pcntl_signal)
 	}
 	zend_string_release(func_name);
 
+	/* If we had a previous handler, save and return it */
+	if ((prev = zend_hash_index_find(&PCNTL_G(php_signal_table), signo)) != NULL) {
+		ZVAL_DEREF(prev);
+		ZVAL_COPY(return_value, prev);
+	}
+
 	/* Add the function name to our signal table */
 	if (zend_hash_index_update(&PCNTL_G(php_signal_table), signo, handle)) {
-		if (Z_REFCOUNTED_P(handle)) Z_ADDREF_P(handle);
+		if (Z_REFCOUNTED_P(handle)) {
+			Z_ADDREF_P(handle);
+		}
 	}
 
 	if (php_signal4(signo, pcntl_signal_handler, (int) restart_syscalls, 1) == (Sigfunc *)SIG_ERR) {
@@ -1019,7 +1027,10 @@ PHP_FUNCTION(pcntl_signal)
 		php_error_docref(NULL, E_WARNING, "Error assigning signal");
 		RETURN_FALSE;
 	}
-	RETURN_TRUE;
+
+	if (Z_ISNULL_P(return_value)) {
+		RETURN_TRUE;
+	}
 }
 /* }}} */
 
diff --git a/ext/pcntl/tests/pcntl_signal.phpt b/ext/pcntl/tests/pcntl_signal.phpt
index a521b00..5eb58b3 100644
--- a/ext/pcntl/tests/pcntl_signal.phpt
+++ b/ext/pcntl/tests/pcntl_signal.phpt
@@ -5,7 +5,10 @@ pcntl_signal()
 <?php if (!extension_loaded("posix")) die("skip posix extension not available"); ?>
 --FILE--
 <?php
-pcntl_signal(SIGTERM, function($signo){
+function test() {}
+
+pcntl_signal(SIGTERM, 'test');
+$prev = pcntl_signal(SIGTERM, function($signo){
 	echo "signal dispatched\n";
 });
 posix_kill(posix_getpid(), SIGTERM);
@@ -17,6 +20,7 @@ pcntl_signal(SIGUSR1, function($signo, $siginfo){
 posix_kill(posix_getpid(), SIGUSR1);
 pcntl_signal_dispatch();
 
+var_dump($prev);
 var_dump(pcntl_signal());
 var_dump(pcntl_signal(SIGALRM, SIG_IGN));
 var_dump(pcntl_signal(-1, -1));
@@ -31,6 +35,7 @@ echo "ok\n";
 --EXPECTF--
 signal dispatched
 got signal from %d
+string(4) "test"
 
 Warning: pcntl_signal() expects at least 2 parameters, 0 given in %s
 NULL
-- 
2.8.1.217.ge6ac6e1

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 06:01:29 2024 UTC