Skip to content

Commit d5ce545

Browse files
committed
Reduce signal forwarding for the stable branch
The helper now runs from first use until shutdown. If its pipe fails, it delivers what is pending and starts a new helper with a new pipe. This removes the idle timeout and the races that came with it. When pcntl_unshare() or pcntl_setns() fail with EINVAL, they retry with the helper stopped, as the kernel may require a process with a single thread. Forwarding is only compiled on systems where pthread_getspecific() was checked to be safe in a signal handler. A forked child can now arm the execution timer over a handler inherited from a thread that does not exist there, and shutdown no longer waits for a helper that cannot be woken up. The test scenarios move to their own file, and ext/pcntl no longer changes php_signal.c.
1 parent 718ebf8 commit d5ce545

18 files changed

Lines changed: 1388 additions & 1146 deletions

‎Zend/zend_signal_forward.c‎

Lines changed: 141 additions & 106 deletions
Large diffs are not rendered by default.

‎Zend/zend_signal_forward.h‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@
2121

2222
#include <signal.h>
2323

24+
#include "zend_atomic.h"
2425
#include "zend_portability.h"
2526

27+
/* See the assumptions at the top of zend_signal_forward.c. Elsewhere, a signal on a thread
28+
* without PHP context is not forwarded. */
29+
#if defined(ZEND_ATOMIC_SIGNAL_SAFE) && !defined(ZEND_SIGNAL_FORWARD_DISABLED) \
30+
&& ((defined(__linux__) && !defined(__BIONIC__) && !defined(__UCLIBC__)) \
31+
|| defined(__APPLE__) || defined(__FreeBSD__))
32+
# define ZEND_SIGNAL_FORWARD_ENABLED 1
33+
#endif
34+
2635
BEGIN_EXTERN_C()
2736

2837
void zend_signal_forward_startup(void);
@@ -36,6 +45,15 @@ ZEND_API void zend_signal_forward_or_default(int signo);
3645

3746
/* The calling thread handles signo. If act is given, it is installed. */
3847
ZEND_API int zend_signal_forward_acquire(int signo, const struct sigaction *act);
48+
/* The handler that the calling thread registered for signo was installed for it, and is
49+
* removed with the thread. */
50+
ZEND_API void zend_signal_forward_mark_installed(int signo);
51+
/* The helper thread stops and starts again, for a system call that requires a process
52+
* with a single thread. Signals meanwhile are forwarded when it has started again.
53+
* Returns whether a helper was stopped. The system may count the thread for a moment
54+
* after it was joined. */
55+
ZEND_API bool zend_signal_forward_suspend(void);
56+
ZEND_API void zend_signal_forward_resume(void);
3957
/* The calling thread no longer handles signo. If act is given, it is installed
4058
* unless the process-wide disposition is still needed by another thread. */
4159
ZEND_API int zend_signal_forward_release(int signo, const struct sigaction *act);
@@ -65,8 +83,6 @@ typedef enum {
6583
ZEND_SIGNAL_FORWARD_TEST_SATURATE,
6684
ZEND_SIGNAL_FORWARD_TEST_FAIL_READ,
6785
ZEND_SIGNAL_FORWARD_TEST_HANDLER_EAGAIN,
68-
ZEND_SIGNAL_FORWARD_TEST_IDLE_SOON,
69-
ZEND_SIGNAL_FORWARD_TEST_IDLE_NORMAL,
7086
ZEND_SIGNAL_FORWARD_TEST_RUNNING,
7187
ZEND_SIGNAL_FORWARD_TEST_RESTART,
7288
ZEND_SIGNAL_FORWARD_TEST_READ_FD,

‎ext/pcntl/pcntl.c‎

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,14 @@ static int pcntl_signal_restore(int signo, Sigfunc *func, int restart)
196196
struct sigaction act;
197197
int result;
198198

199-
php_signal_action(&act, func, restart, 0);
199+
memset(&act, 0, sizeof(act));
200+
#ifdef HAVE_STRUCT_SIGINFO_T
201+
act.sa_sigaction = func;
202+
#else
203+
act.sa_handler = func;
204+
#endif
205+
sigemptyset(&act.sa_mask);
206+
act.sa_flags = restart ? SA_RESTART : 0;
200207
/* A signal already forwarded to this thread is still handled during the release. */
201208
result = zend_signal_forward_release(signo, &act);
202209
pcntl_signal_set_handled(signo, false);
@@ -850,30 +857,27 @@ PHP_FUNCTION(pcntl_signal)
850857
}
851858

852859
/* Register with the OS first so that on failure we don't record a handler that was never installed */
853-
#if defined(ZTS) && !defined(ZEND_SIGNALS)
854-
struct sigaction act;
855-
856-
php_signal_action(&act, pcntl_signal_handler, (int) restart_syscalls, 1);
857-
pcntl_signal_set_handled((int) signo, true);
858-
if (zend_signal_forward_acquire((int) signo, &act) != 0) {
859-
PCNTL_G(last_error) = errno;
860-
pcntl_signal_set_handled((int) signo, false);
861-
php_error_docref(NULL, E_WARNING, "Error assigning signal");
862-
RETURN_FALSE;
863-
}
864-
#else
865-
# ifdef ZTS
860+
#ifdef ZTS
866861
/* Before the handler is installed, so that no signal finds it without a thread to go to */
867-
zend_signal_forward_acquire((int) signo, NULL);
862+
# ifndef ZEND_SIGNALS
863+
pcntl_signal_set_handled((int) signo, true);
868864
# endif
865+
zend_signal_forward_acquire((int) signo, NULL);
866+
#endif
869867
if (php_signal4(signo, pcntl_signal_handler, (int) restart_syscalls, 1) == (void *)SIG_ERR) {
870868
PCNTL_G(last_error) = errno;
871-
# ifdef ZTS
869+
#ifdef ZTS
872870
zend_signal_forward_release((int) signo, NULL);
871+
# ifndef ZEND_SIGNALS
872+
pcntl_signal_set_handled((int) signo, false);
873873
# endif
874+
#endif
874875
php_error_docref(NULL, E_WARNING, "Error assigning signal");
875876
RETURN_FALSE;
876877
}
878+
#if defined(ZTS) && !defined(ZEND_SIGNALS)
879+
/* Installed directly, so it must not outlive the threads that handle the signal. */
880+
zend_signal_forward_mark_installed((int) signo);
877881
#endif
878882

879883
/* Add the function name to our signal table */
@@ -1557,7 +1561,20 @@ PHP_FUNCTION(pcntl_unshare)
15571561
Z_PARAM_LONG(flags)
15581562
ZEND_PARSE_PARAMETERS_END();
15591563

1560-
if (unshare(flags) == -1) {
1564+
int result = unshare(flags);
1565+
#ifdef ZTS
1566+
if (result == -1 && errno == EINVAL && zend_signal_forward_suspend()) {
1567+
/* The kernel may require a single thread, which the signal forwarding helper is not. */
1568+
for (int i = 0; i < 100 && (result = unshare(flags)) == -1 && errno == EINVAL; i++) {
1569+
usleep(1000);
1570+
}
1571+
int unshare_errno = errno;
1572+
zend_signal_forward_resume();
1573+
errno = unshare_errno;
1574+
}
1575+
#endif
1576+
1577+
if (result == -1) {
15611578
PCNTL_G(last_error) = errno;
15621579
switch (errno) {
15631580
#ifdef EINVAL
@@ -1758,6 +1775,16 @@ PHP_FUNCTION(pcntl_setns)
17581775
}
17591776
ret = setns(fd, (int)nstype);
17601777
int setns_errno = errno;
1778+
#ifdef ZTS
1779+
if (ret == -1 && setns_errno == EINVAL && zend_signal_forward_suspend()) {
1780+
/* The kernel may require a single thread, which the signal forwarding helper is not. */
1781+
for (int i = 0; i < 100 && (ret = setns(fd, (int)nstype)) == -1 && errno == EINVAL; i++) {
1782+
usleep(1000);
1783+
}
1784+
setns_errno = errno;
1785+
zend_signal_forward_resume();
1786+
}
1787+
#endif
17611788
close(fd);
17621789

17631790
if (ret == -1) {

‎ext/pcntl/php_signal.c‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,33 @@
2121

2222
/* php_signal using sigaction is derived from Advanced Programming
2323
* in the Unix Environment by W. Richard Stevens p 298. */
24-
void php_signal_action(struct sigaction *act, Sigfunc *func, int restart, int mask_all)
24+
Sigfunc *php_signal4(int signo, Sigfunc *func, int restart, int mask_all)
2525
{
26-
memset(act, 0, sizeof(*act));
26+
struct sigaction act,oact;
27+
2728
#ifdef HAVE_STRUCT_SIGINFO_T
28-
act->sa_sigaction = func;
29+
act.sa_sigaction = func;
2930
#else
30-
act->sa_handler = func;
31+
act.sa_handler = func;
3132
#endif
3233
if (mask_all) {
33-
sigfillset(&act->sa_mask);
34+
sigfillset(&act.sa_mask);
3435
} else {
35-
sigemptyset(&act->sa_mask);
36+
sigemptyset(&act.sa_mask);
3637
}
37-
act->sa_flags = SA_ONSTACK;
38+
act.sa_flags = SA_ONSTACK;
3839
#ifdef HAVE_STRUCT_SIGINFO_T
39-
act->sa_flags |= SA_SIGINFO;
40+
act.sa_flags |= SA_SIGINFO;
4041
#endif
4142
if (!restart) {
4243
#ifdef SA_INTERRUPT
43-
act->sa_flags |= SA_INTERRUPT; /* SunOS */
44+
act.sa_flags |= SA_INTERRUPT; /* SunOS */
4445
#endif
4546
} else {
4647
#ifdef SA_RESTART
47-
act->sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
48+
act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
4849
#endif
4950
}
50-
}
51-
52-
Sigfunc *php_signal4(int signo, Sigfunc *func, int restart, int mask_all)
53-
{
54-
struct sigaction act,oact;
55-
56-
php_signal_action(&act, func, restart, mask_all);
5751
zend_sigaction(signo, &act, &oact);
5852

5953
#ifdef HAVE_STRUCT_SIGINFO_T

‎ext/pcntl/php_signal.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ typedef void Sigfunc(int, siginfo_t*, void*);
2323
#else
2424
typedef void Sigfunc(int);
2525
#endif
26-
void php_signal_action(struct sigaction *act, Sigfunc *func, int restart, int mask_all);
2726
Sigfunc *php_signal(int signo, Sigfunc *func, int restart);
2827
Sigfunc *php_signal4(int signo, Sigfunc *func, int restart, int mask_all);
2928

‎ext/pcntl/tests/gh25541.phpt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ zend_test
77
<?php
88
if (!PHP_ZTS) die('skip ZTS only');
99
if (!function_exists('zend_test_raise_on_foreign_thread')) die('skip zend_test_raise_on_foreign_thread() not available');
10+
if (!zend_test_signal_forwarding('supported')) die('skip signal forwarding is not supported on this platform');
1011
?>
1112
--FILE--
1213
<?php

‎ext/pcntl/tests/gh25541_fork.phpt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ zend_test
88
<?php
99
if (!PHP_ZTS) die('skip ZTS only');
1010
if (!function_exists('zend_test_raise_on_foreign_thread')) die('skip zend_test_raise_on_foreign_thread() not available');
11+
if (!zend_test_signal_forwarding('supported')) die('skip signal forwarding is not supported on this platform');
1112
?>
1213
--FILE--
1314
<?php

‎ext/pcntl/tests/gh25541_rfork.phpt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ zend_test
99
if (!PHP_ZTS) die('skip ZTS only');
1010
if (!function_exists('pcntl_rfork')) die('skip pcntl_rfork() is not available');
1111
if (!function_exists('zend_test_raise_on_foreign_thread')) die('skip zend_test_raise_on_foreign_thread() not available');
12+
if (!zend_test_signal_forwarding('supported')) die('skip signal forwarding is not supported on this platform');
1213
?>
1314
--FILE--
1415
<?php
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
GH-25541 (pcntl_unshare() still works, and forwarding goes on, with a signal handler)
3+
--EXTENSIONS--
4+
pcntl
5+
zend_test
6+
--SKIPIF--
7+
<?php
8+
if (!PHP_ZTS) die('skip ZTS only');
9+
if (!function_exists('pcntl_unshare')) die('skip pcntl_unshare() is not available');
10+
if (!defined('CLONE_NEWUSER')) die('skip flag unavailable');
11+
if (!function_exists('zend_test_raise_on_foreign_thread')) die('skip zend_test_raise_on_foreign_thread() not available');
12+
if (!zend_test_signal_forwarding('supported')) die('skip signal forwarding is not supported on this platform');
13+
if (@pcntl_unshare(CLONE_NEWUSER) == false && pcntl_get_last_error() == PCNTL_EPERM) {
14+
die('skip Insufficient privileges to use CLONE_NEWUSER');
15+
}
16+
if (getenv('SKIP_REPEAT')) die('skip cannot be repeated: the process is in a new user namespace');
17+
?>
18+
--FILE--
19+
<?php
20+
$handled = false;
21+
pcntl_signal(SIGUSR1, function () use (&$handled) {
22+
$handled = true;
23+
});
24+
25+
var_dump(pcntl_unshare(CLONE_NEWUSER));
26+
27+
zend_test_raise_on_foreign_thread(SIGUSR1);
28+
for ($i = 0; !$handled && $i < 3000; $i++) {
29+
pcntl_signal_dispatch();
30+
usleep(10000);
31+
}
32+
var_dump($handled);
33+
?>
34+
--EXPECT--
35+
bool(true)
36+
bool(true)

‎ext/pcntl/tests/gh25548.phpt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ zend_test
88
if (!PHP_ZTS) die('skip ZTS only');
99
if (!function_exists('zend_test_timeout_signal') || zend_test_timeout_signal() === null) die('skip the execution timer is not process-wide');
1010
if (getenv('SKIP_SLOW_TESTS')) die('skip slow test');
11+
if (!zend_test_signal_forwarding('supported')) die('skip signal forwarding is not supported on this platform');
1112
?>
1213
--FILE--
1314
<?php

0 commit comments

Comments
 (0)