]> granicus.if.org Git - php/commitdiff
Fix #72409 - return previous handler
authorDavid Walker <dave@mudsite.com>
Sat, 2 Jul 2016 16:44:17 +0000 (10:44 -0600)
committerAaron Piotrowski <aaron@trowski.com>
Wed, 6 Jul 2016 18:57:37 +0000 (13:57 -0500)
This patch addresses https://bugs.php.net/bug.php?id=72409

This patch is applied to master compared to PR#1952 which was
patching 5.6.x branch of PHP

This patch takes into account discussions on PR #1978
Addressing that rather than have pcntl_signal() return a value
to create a new function that can be used to get the current
value of the signal handler.

ext/pcntl/pcntl.c
ext/pcntl/php_pcntl.h
ext/pcntl/tests/pcntl_signal_get_handler.phpt [new file with mode: 0644]

index 5a7747acf4e1d3c8fd6c5ff4eef950c9a173aa75..811f7406ced210d1478289045d232da44888a00c 100644 (file)
@@ -72,6 +72,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_pcntl_signal, 0, 0, 2)
        ZEND_ARG_INFO(0, restart_syscalls)
 ZEND_END_ARG_INFO()
 
+ZEND_BEGIN_ARG_INFO_EX(arginfo_pcntl_signal_get_handler, 0, 0, 1)
+       ZEND_ARG_INFO(0, signo)
+ZEND_END_ARG_INFO()
+
 ZEND_BEGIN_ARG_INFO_EX(arginfo_pcntl_sigprocmask, 0, 0, 2)
        ZEND_ARG_INFO(0, how)
        ZEND_ARG_INFO(0, set)
@@ -159,6 +163,7 @@ const zend_function_entry pcntl_functions[] = {
        PHP_FE(pcntl_waitpid,           arginfo_pcntl_waitpid)
        PHP_FE(pcntl_wait,                      arginfo_pcntl_wait)
        PHP_FE(pcntl_signal,            arginfo_pcntl_signal)
+       PHP_FE(pcntl_signal_get_handler,                arginfo_pcntl_signal_get_handler)
        PHP_FE(pcntl_signal_dispatch,   arginfo_pcntl_void)
        PHP_FE(pcntl_wifexited,         arginfo_pcntl_wifexited)
        PHP_FE(pcntl_wifstopped,        arginfo_pcntl_wifstopped)
@@ -997,7 +1002,7 @@ PHP_FUNCTION(pcntl_signal)
                        php_error_docref(NULL, E_WARNING, "Error assigning signal");
                        RETURN_FALSE;
                }
-               zend_hash_index_del(&PCNTL_G(php_signal_table), signo);
+               zend_hash_index_update(&PCNTL_G(php_signal_table), signo, handle);
                RETURN_TRUE;
        }
 
@@ -1023,6 +1028,29 @@ PHP_FUNCTION(pcntl_signal)
 }
 /* }}} */
 
+/* {{{ proto bool pcntl_signal_get_handler(int signo)
+   Gets signal handler */
+PHP_FUNCTION(pcntl_signal_get_handler)
+{
+       zval *prev_handle;
+       zend_long signo;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &signo) == FAILURE) {
+               return;
+       }
+
+       if (signo < 1 || signo > 32) {
+               php_error_docref(NULL, E_WARNING, "Invalid signal");
+               RETURN_FALSE;
+       }
+
+       if ((prev_handle = zend_hash_index_find(&PCNTL_G(php_signal_table), signo)) != NULL) {
+               RETURN_ZVAL(prev_handle, 1, 0);
+       } else {
+               RETURN_LONG((long)SIG_DFL);
+       }
+}
+
 /* {{{ proto bool pcntl_signal_dispatch()
    Dispatch signals to signal handlers */
 PHP_FUNCTION(pcntl_signal_dispatch)
index 816bffb480f1a6ddc9eacc5dc44cbe18a03f90a4..856ebe33efe64611a9b6fe64d60f3010d6ce6bb7 100644 (file)
@@ -51,6 +51,7 @@ PHP_FUNCTION(pcntl_wexitstatus);
 PHP_FUNCTION(pcntl_wtermsig);
 PHP_FUNCTION(pcntl_wstopsig);
 PHP_FUNCTION(pcntl_signal);
+PHP_FUNCTION(pcntl_signal_get_handler);
 PHP_FUNCTION(pcntl_signal_dispatch);
 PHP_FUNCTION(pcntl_get_last_error);
 PHP_FUNCTION(pcntl_strerror);
diff --git a/ext/pcntl/tests/pcntl_signal_get_handler.phpt b/ext/pcntl/tests/pcntl_signal_get_handler.phpt
new file mode 100644 (file)
index 0000000..7d66f65
--- /dev/null
@@ -0,0 +1,32 @@
+--TEST--
+pcntl_signal()
+--SKIPIF--
+<?php if (!extension_loaded("pcntl")) print "skip"; ?>
+<?php if (!extension_loaded("posix")) die("skip posix extension not available"); ?>
+--FILE--
+<?php
+var_dump(pcntl_signal_get_handler(SIGUSR1));
+
+function pcntl_test($signo) {}
+pcntl_signal(SIGUSR1, 'pcntl_test');
+var_dump(pcntl_signal_get_handler(SIGUSR1));
+
+pcntl_signal(SIGUSR1, SIG_IGN);
+var_dump(pcntl_signal_get_handler(SIGUSR1));
+
+pcntl_signal(SIGUSR1, SIG_DFL);
+var_dump(pcntl_signal_get_handler(SIGUSR1));
+
+posix_kill(posix_getpid(), SIGUSR1);
+pcntl_signal_dispatch();
+
+echo "ok\n";
+?>
+--EXPECTF--
+int(0)
+string(10) "pcntl_test"
+int(1)
+int(0)
+User defined signal 1
+
+Termsig=10