Fix #72409 - return previous handler #1985

Closed
wants to merge 4 commits into
from

4 participants

@bp1222
bp1222 commented Jul 5, 2016 edited

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.

@bp1222 bp1222 Fix #72409 - return previous handler
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.
b4a2190
@laruence laruence commented on the diff Jul 6, 2016
ext/pcntl/pcntl.c
@@ -1012,6 +1017,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) {
@laruence
php.net member
laruence added a note Jul 6, 2016 edited

I think it's better to use NSIG instead of hardcode 32

@bp1222
bp1222 added a note Jul 6, 2016

I was copy-pasting that via pcntl_signal(). The problem with using NSIG is that on my box(linux 4.4.0, libc 2.22-4) it's value is 64, however 32 is the largest defined signal. That said, NSIG on FreeBSD is defined to 32, even though it has SIGLIBRT defined at 33. So, I'm not sure what happens if you're to sigaction() a signal that's not defined in signal.h, or if in general we might look to be smarter about what is the highest signal we support per-platform.

Regardless, this could be something addressed in a distinct PR trying to abstract and remove the hard-coded values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weltling weltling commented on the diff Jul 6, 2016
ext/pcntl/pcntl.c
@@ -986,7 +991,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);
@weltling
weltling added a note Jul 6, 2016

handle is not a calable but integer at this point. Could you please explain why this needs to be updated in this case? I guess a non callable should not be added to the signal mapping.

Thanks.

@bp1222
bp1222 added a note Jul 6, 2016

Since a user can set a signal to be handled by \SIG_ING we would need to take note of it, for the new function to acquire the handler. I guess we wouldn't need to save \SIG_DFL but, that is why I changed this from _del to _update

@bp1222
bp1222 added a note Jul 6, 2016

Although, this does make me think about the handling side in pcntl_signal_dispatch. No reason to attempt to call the handler, if it is a long.

@weltling
weltling added a note Jul 6, 2016

Ah, i see. Need that so the new function can return it. Looks fine then, The recorded zval won't be found in the lookup table, as the signal won't be delivered into the queue. Seems fine.

Thanks.

@weltling
weltling added a note Jul 6, 2016

Yes, that was my concern, it is tricky, After thinking a bit more, probably it should be ensured that pcntl_signal_dispatch() doesn't handle an integer as a function. Possible erroneous scenario

pcntl_signal(SIGINT, "some_handler"); /* recorded into the lookup table */
/* do something else */
/* get SIGINT from outside, it has landed in the queue, but not yet handled */
pcntl_signal(SIGINT, SIG_IGN); /* the handler is an int */

By this state, it could be possible that to the time of dispatching signals, call_user_function gets an integer instead fo callable. Not sure how to phpt test this, would be yet more tricky :)

Thanks.

@bp1222
bp1222 added a note Jul 6, 2016

Well, zend_call_function() will properly make sure that we're dealing with a callable and fail gracefully. That said, it's simple (already added) a check to make sure the handle isn't a long.

@weltling
weltling added a note Jul 6, 2016

@bp1222 looks fine for what i'd care. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weltling

@trowski about the question from the other PR - if this PR gets accepted in 7.1, i don't mind a backport. Though please lets make the gap a bit longer, letting it to go through 7.1 QA first (still it would be beta). Then target 7.0.11 if everything is ok, so merging into 7.0 like after August 4th.

Thanks.

@trowski

@weltling Sounds good, thanks!

@php-pulls

Comment on behalf of trowski at php.net:

Merged into master and will be included in 7.1.0-beta1. I'll merge the changes down into 7.0 later as suggested.

@php-pulls php-pulls closed this Jul 6, 2016
@bp1222 bp1222 deleted the bp1222:fix3-74209 branch Jul 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment