Skip to content

Commit ce002d4

Browse files
committed
Move signal blocking and unblocking macros into zend_signal.h
Also give them names which match the old ones from Zend signal handling. This will allow some extensions which used the old macros to continue working without change.
1 parent ae250a4 commit ce002d4

File tree

4 files changed

+110
-71
lines changed

4 files changed

+110
-71
lines changed

Zend/zend.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ static uint32_t zend_version_info_length;
6666
#define ZEND_CORE_VERSION_INFO "Zend Engine v" ZEND_VERSION ", Copyright (c) Zend Technologies\n"
6767
#define PRINT_ZVAL_INDENT 4
6868

69+
#ifdef HAVE_SIGPROCMASK
70+
ZEND_API sigset_t mask_all_signals; /* For blocking/unblocking signals */
71+
72+
# if ZEND_DEBUG
73+
/* Ensure that signal blocking macros in zend_signal.h are used correctly */
74+
ZEND_TLS int _signals_masked = 0;
75+
ZEND_API void zend_debug_mask_signals()
76+
{
77+
_signals_masked++;
78+
}
79+
ZEND_API void zend_debug_unmask_signals()
80+
{
81+
if (--_signals_masked)
82+
zend_error_noreturn(E_ERROR, "Cannot nest ZEND_SIGNAL_BLOCK_INTERRUPTIONS; it is not re-entrant");
83+
}
84+
# endif
85+
#endif
86+
6987
/* true multithread-shared globals */
7088
ZEND_API zend_class_entry *zend_standard_class_def = NULL;
7189
ZEND_API size_t (*zend_printf)(const char *format, ...);
@@ -803,6 +821,10 @@ int zend_startup(zend_utility_functions *utility_functions) /* {{{ */
803821
fpsetmask(0);
804822
#endif
805823

824+
#ifdef HAVE_SIGPROCMASK
825+
sigfillset(&mask_all_signals);
826+
#endif
827+
806828
zend_startup_strtod();
807829
zend_startup_extensions_mechanism();
808830

Zend/zend.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@
3838
#include "zend_stream.h"
3939
#include "zend_smart_str_public.h"
4040
#include "zend_smart_string_public.h"
41+
#include "zend_signal.h"
4142

4243
#define zend_sprintf sprintf
4344

45+
#define HANDLE_BLOCK_INTERRUPTIONS() ZEND_SIGNAL_BLOCK_INTERRUPTIONS()
46+
#define HANDLE_UNBLOCK_INTERRUPTIONS() ZEND_SIGNAL_UNBLOCK_INTERRUPTIONS()
47+
4448
#define INTERNAL_FUNCTION_PARAMETERS zend_execute_data *execute_data, zval *return_value
4549
#define INTERNAL_FUNCTION_PARAM_PASSTHRU execute_data, return_value
4650

Zend/zend_signal.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Blocking and Unblocking Signals |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifndef ZEND_SIGNAL_H
18+
#define ZEND_SIGNAL_H
19+
20+
#include <signal.h>
21+
22+
#ifdef HAVE_SIGPROCMASK
23+
24+
extern sigset_t mask_all_signals;
25+
26+
# if ZEND_DEBUG
27+
ZEND_API void zend_debug_mask_signals();
28+
ZEND_API void zend_debug_unmask_signals();
29+
# else
30+
# define zend_debug_mask_signals() do {} while (0)
31+
# define zend_debug_unmask_signals() do {} while (0)
32+
# endif
33+
34+
# define ZEND_SIGNAL_BLOCK_INTERRUPTIONS() \
35+
sigset_t _oldmask; \
36+
zend_debug_mask_signals(); \
37+
MASK_ALL_SIGNALS()
38+
# define ZEND_SIGNAL_UNBLOCK_INTERRUPTIONS() \
39+
zend_debug_unmask_signals(); \
40+
UNMASK_ALL_SIGNALS()
41+
42+
# ifdef ZTS
43+
# define MASK_ALL_SIGNALS() \
44+
tsrm_sigmask(SIG_BLOCK, &mask_all_signals, &_oldmask)
45+
# define UNMASK_ALL_SIGNALS() \
46+
tsrm_sigmask(SIG_SETMASK, &_oldmask, NULL)
47+
# else
48+
# define MASK_ALL_SIGNALS() \
49+
sigprocmask(SIG_BLOCK, &mask_all_signals, &_oldmask)
50+
# define UNMASK_ALL_SIGNALS() \
51+
sigprocmask(SIG_SETMASK, &_oldmask, NULL)
52+
# endif
53+
54+
#else
55+
56+
# define ZEND_SIGNAL_BLOCK_INTERRUPTIONS() do {} while(0)
57+
# define ZEND_SIGNAL_UNBLOCK_INTERRUPTIONS() do {} while(0)
58+
59+
#endif /* HAVE_SIGPROCMASK */
60+
#endif /* ZEND_SIGNAL_H */

ext/opcache/ZendAccelerator.c

Lines changed: 24 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -118,49 +118,6 @@ zend_bool file_cache_only = 0; /* process uses file cache only */
118118
zend_bool fallback_process = 0; /* process uses file cache fallback */
119119
#endif
120120

121-
#ifdef HAVE_SIGPROCMASK
122-
static sigset_t mask_all_signals;
123-
124-
# if ZEND_DEBUG
125-
# ifdef ZTS
126-
ZEND_TLS int _signals_masked = 0;
127-
# else
128-
static int _signals_masked = 0;
129-
# endif
130-
# define DEBUG_BLOCK_ALL_SIGNALS() _signals_masked += 1
131-
# define DEBUG_UNBLOCK_ALL_SIGNALS() \
132-
if (--_signals_masked) \
133-
zend_error_noreturn(E_ERROR, "Cannot nest BLOCK_ALL_SIGNALS; it is not re-entrant")
134-
# else
135-
# define DEBUG_BLOCK_ALL_SIGNALS() do {} while (0)
136-
# define DEBUG_UNBLOCK_ALL_SIGNALS() do {} while (0)
137-
# endif
138-
139-
# define BLOCK_ALL_SIGNALS() \
140-
sigset_t _oldmask; \
141-
DEBUG_BLOCK_ALL_SIGNALS(); \
142-
MASK_ALL_SIGNALS()
143-
# define UNBLOCK_ALL_SIGNALS() \
144-
DEBUG_UNBLOCK_ALL_SIGNALS(); \
145-
UNMASK_ALL_SIGNALS()
146-
147-
# ifdef ZTS
148-
# define MASK_ALL_SIGNALS() \
149-
tsrm_sigmask(SIG_BLOCK, &mask_all_signals, &_oldmask)
150-
# define UNMASK_ALL_SIGNALS() \
151-
tsrm_sigmask(SIG_SETMASK, &_oldmask, NULL)
152-
# else
153-
# define MASK_ALL_SIGNALS() \
154-
sigprocmask(SIG_BLOCK, &mask_all_signals, &_oldmask)
155-
# define UNMASK_ALL_SIGNALS() \
156-
sigprocmask(SIG_SETMASK, &_oldmask, NULL)
157-
# endif
158-
159-
#else
160-
# define BLOCK_ALL_SIGNALS() do {} while(0)
161-
# define UNBLOCK_ALL_SIGNALS() do {} while(0)
162-
#endif
163-
164121
static zend_op_array *(*accelerator_orig_compile_file)(zend_file_handle *file_handle, int type);
165122
static int (*accelerator_orig_zend_stream_open_function)(const char *filename, zend_file_handle *handle );
166123
static zend_string *(*accelerator_orig_zend_resolve_path)(const char *filename, size_t filename_len);
@@ -788,7 +745,7 @@ static zend_string* ZEND_FASTCALL accel_replace_string_by_shm_permanent(zend_str
788745

789746
static void accel_use_shm_interned_strings(void)
790747
{
791-
BLOCK_ALL_SIGNALS();
748+
HANDLE_BLOCK_INTERRUPTIONS();
792749
SHM_UNPROTECT();
793750
zend_shared_alloc_lock();
794751

@@ -803,7 +760,7 @@ static void accel_use_shm_interned_strings(void)
803760

804761
zend_shared_alloc_unlock();
805762
SHM_PROTECT();
806-
UNBLOCK_ALL_SIGNALS();
763+
HANDLE_UNBLOCK_INTERRUPTIONS();
807764
}
808765

809766
#ifndef ZEND_WIN32
@@ -1202,7 +1159,7 @@ char *accel_make_persistent_key(const char *path, size_t path_length, int *key_l
12021159

12031160
zend_string *str = accel_find_interned_string(cwd_str);
12041161
if (!str) {
1205-
BLOCK_ALL_SIGNALS();
1162+
HANDLE_BLOCK_INTERRUPTIONS();
12061163
SHM_UNPROTECT();
12071164
zend_shared_alloc_lock();
12081165
str = accel_new_interned_string(zend_string_copy(cwd_str));
@@ -1212,7 +1169,7 @@ char *accel_make_persistent_key(const char *path, size_t path_length, int *key_l
12121169
}
12131170
zend_shared_alloc_unlock();
12141171
SHM_PROTECT();
1215-
UNBLOCK_ALL_SIGNALS();
1172+
HANDLE_UNBLOCK_INTERRUPTIONS();
12161173
}
12171174
if (str) {
12181175
char buf[32];
@@ -1246,7 +1203,7 @@ char *accel_make_persistent_key(const char *path, size_t path_length, int *key_l
12461203

12471204
zend_string *str = accel_find_interned_string(ZCG(include_path));
12481205
if (!str) {
1249-
BLOCK_ALL_SIGNALS();
1206+
HANDLE_BLOCK_INTERRUPTIONS();
12501207
SHM_UNPROTECT();
12511208
zend_shared_alloc_lock();
12521209
str = accel_new_interned_string(zend_string_copy(ZCG(include_path)));
@@ -1255,7 +1212,7 @@ char *accel_make_persistent_key(const char *path, size_t path_length, int *key_l
12551212
}
12561213
zend_shared_alloc_unlock();
12571214
SHM_PROTECT();
1258-
UNBLOCK_ALL_SIGNALS();
1215+
HANDLE_UNBLOCK_INTERRUPTIONS();
12591216
}
12601217
if (str) {
12611218
char buf[32];
@@ -1352,7 +1309,7 @@ int zend_accel_invalidate(const char *filename, size_t filename_len, zend_bool f
13521309
if (force ||
13531310
!ZCG(accel_directives).validate_timestamps ||
13541311
do_validate_timestamps(persistent_script, &file_handle) == FAILURE) {
1355-
BLOCK_ALL_SIGNALS();
1312+
HANDLE_BLOCK_INTERRUPTIONS();
13561313
SHM_UNPROTECT();
13571314
zend_shared_alloc_lock();
13581315
if (!persistent_script->corrupted) {
@@ -1367,7 +1324,7 @@ int zend_accel_invalidate(const char *filename, size_t filename_len, zend_bool f
13671324
}
13681325
zend_shared_alloc_unlock();
13691326
SHM_PROTECT();
1370-
UNBLOCK_ALL_SIGNALS();
1327+
HANDLE_UNBLOCK_INTERRUPTIONS();
13711328
}
13721329
}
13731330

@@ -1948,11 +1905,11 @@ zend_op_array *file_cache_compile_file(zend_file_handle *file_handle, int type)
19481905
}
19491906
}
19501907

1951-
BLOCK_ALL_SIGNALS();
1908+
HANDLE_BLOCK_INTERRUPTIONS();
19521909
SHM_UNPROTECT();
19531910
persistent_script = zend_file_cache_script_load(file_handle);
19541911
SHM_PROTECT();
1955-
UNBLOCK_ALL_SIGNALS();
1912+
HANDLE_UNBLOCK_INTERRUPTIONS();
19561913
if (persistent_script) {
19571914
/* see bug #15471 (old BTS) */
19581915
if (persistent_script->script.filename) {
@@ -2111,13 +2068,13 @@ zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)
21112068
persistent_script = (zend_persistent_script *)bucket->data;
21122069

21132070
if (key && !persistent_script->corrupted) {
2114-
BLOCK_ALL_SIGNALS();
2071+
HANDLE_BLOCK_INTERRUPTIONS();
21152072
SHM_UNPROTECT();
21162073
zend_shared_alloc_lock();
21172074
zend_accel_add_key(key, key_length, bucket);
21182075
zend_shared_alloc_unlock();
21192076
SHM_PROTECT();
2120-
UNBLOCK_ALL_SIGNALS();
2077+
HANDLE_UNBLOCK_INTERRUPTIONS();
21212078
}
21222079
}
21232080
}
@@ -2162,7 +2119,7 @@ zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)
21622119
return NULL;
21632120
}
21642121

2165-
BLOCK_ALL_SIGNALS();
2122+
HANDLE_BLOCK_INTERRUPTIONS();
21662123
SHM_UNPROTECT();
21672124

21682125
/* If script is found then validate_timestamps if option is enabled */
@@ -2225,17 +2182,17 @@ zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)
22252182
/* No memory left. Behave like without the Accelerator */
22262183
if (ZSMMG(memory_exhausted) || ZCSG(restart_pending)) {
22272184
SHM_PROTECT();
2228-
UNBLOCK_ALL_SIGNALS();
2185+
HANDLE_UNBLOCK_INTERRUPTIONS();
22292186
if (ZCG(accel_directives).file_cache) {
22302187
return file_cache_compile_file(file_handle, type);
22312188
}
22322189
return accelerator_orig_compile_file(file_handle, type);
22332190
}
22342191

22352192
SHM_PROTECT();
2236-
UNBLOCK_ALL_SIGNALS();
2193+
HANDLE_UNBLOCK_INTERRUPTIONS();
22372194
persistent_script = opcache_compile_file(file_handle, type, key, &op_array);
2238-
BLOCK_ALL_SIGNALS();
2195+
HANDLE_BLOCK_INTERRUPTIONS();
22392196
SHM_UNPROTECT();
22402197

22412198
/* Try and cache the script and assume that it is returned from_shared_memory.
@@ -2251,7 +2208,7 @@ zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)
22512208
*/
22522209
if (!persistent_script) {
22532210
SHM_PROTECT();
2254-
UNBLOCK_ALL_SIGNALS();
2211+
HANDLE_UNBLOCK_INTERRUPTIONS();
22552212
return op_array;
22562213
}
22572214
if (from_shared_memory) {
@@ -2306,7 +2263,7 @@ zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)
23062263
persistent_script->dynamic_members.last_used = ZCG(request_time);
23072264

23082265
SHM_PROTECT();
2309-
UNBLOCK_ALL_SIGNALS();
2266+
HANDLE_UNBLOCK_INTERRUPTIONS();
23102267

23112268
/* Fetch jit auto globals used in the script before execution */
23122269
if (persistent_script->ping_auto_globals_mask) {
@@ -2412,13 +2369,13 @@ static zend_string* persistent_zend_resolve_path(const char *filename, size_t fi
24122369
if (!persistent_script->corrupted) {
24132370
if (key) {
24142371
/* add another "key" for the same bucket */
2415-
BLOCK_ALL_SIGNALS();
2372+
HANDLE_BLOCK_INTERRUPTIONS();
24162373
SHM_UNPROTECT();
24172374
zend_shared_alloc_lock();
24182375
zend_accel_add_key(key, key_length, bucket);
24192376
zend_shared_alloc_unlock();
24202377
SHM_PROTECT();
2421-
UNBLOCK_ALL_SIGNALS();
2378+
HANDLE_UNBLOCK_INTERRUPTIONS();
24222379
} else {
24232380
ZCG(key_len) = 0;
24242381
}
@@ -2515,7 +2472,7 @@ int accel_activate(INIT_FUNC_ARGS)
25152472
}
25162473
#endif
25172474

2518-
BLOCK_ALL_SIGNALS();
2475+
HANDLE_BLOCK_INTERRUPTIONS();
25192476
SHM_UNPROTECT();
25202477

25212478
if (ZCG(counted)) {
@@ -2574,7 +2531,7 @@ int accel_activate(INIT_FUNC_ARGS)
25742531
ZCG(accelerator_enabled) = ZCSG(accelerator_enabled);
25752532

25762533
SHM_PROTECT();
2577-
UNBLOCK_ALL_SIGNALS();
2534+
HANDLE_UNBLOCK_INTERRUPTIONS();
25782535

25792536
if (ZCG(accelerator_enabled) && ZCSG(last_restart_time) != ZCG(last_restart_time)) {
25802537
/* SHM was reinitialized. */
@@ -3001,10 +2958,6 @@ static int accel_startup(zend_extension *extension)
30012958
}
30022959
#endif
30032960

3004-
#ifdef HAVE_SIGPROCMASK
3005-
sigfillset(&mask_all_signals);
3006-
#endif
3007-
30082961
/* no supported SAPI found - disable acceleration and stop initialization */
30092962
if (accel_find_sapi() == FAILURE) {
30102963
accel_startup_ok = 0;
@@ -3277,7 +3230,7 @@ void zend_accel_schedule_restart(zend_accel_restart_reason reason)
32773230
zend_accel_error(ACCEL_LOG_DEBUG, "Restart Scheduled! Reason: %s",
32783231
zend_accel_restart_reason_text[reason]);
32793232

3280-
BLOCK_ALL_SIGNALS();
3233+
HANDLE_BLOCK_INTERRUPTIONS();
32813234
SHM_UNPROTECT();
32823235
ZCSG(restart_pending) = 1;
32833236
ZCSG(restart_reason) = reason;
@@ -3290,7 +3243,7 @@ void zend_accel_schedule_restart(zend_accel_restart_reason reason)
32903243
ZCSG(force_restart_time) = 0;
32913244
}
32923245
SHM_PROTECT();
3293-
UNBLOCK_ALL_SIGNALS();
3246+
HANDLE_UNBLOCK_INTERRUPTIONS();
32943247
}
32953248

32963249
/* this is needed because on WIN32 lock is not decreased unless ZCG(counted) is set */

0 commit comments

Comments
 (0)