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:11 UTC

Return to Bug #72409 | Download this patch
This patch is obsolete

Obsoleted by patches:

Patch Revisions:

Developer: dave@mudsite.com

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

---
 ext/pcntl/pcntl.c                 | 15 ++++++++-------
 ext/pcntl/tests/pcntl_signal.phpt |  7 ++++++-
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c
index 0b49d3c..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,8 +1009,12 @@ PHP_FUNCTION(pcntl_signal)
 	}
 	zend_string_release(func_name);
 
-    zval *prev = NULL;
-    prev = zend_hash_index_find(&PCNTL_G(php_signal_table), signo);
+	/* 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)) {
@@ -1024,12 +1028,9 @@ PHP_FUNCTION(pcntl_signal)
 		RETURN_FALSE;
 	}
 
-	if (!zend_is_callable(prev, 0, &func_name)) {
-		zend_string_release(func_name);
+	if (Z_ISNULL_P(return_value)) {
 		RETURN_TRUE;
 	}
-    zend_string_release(func_name);
-    ZVAL_COPY(return_value, prev);
 }
 /* }}} */
 
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-2021 The PHP Group
All rights reserved.
Last updated: Sat Jul 24 08:01:23 2021 UTC