Skip to content

Commit 18faa35

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 ebdee20 commit 18faa35

File tree

4 files changed

+107
-73
lines changed

4 files changed

+107
-73
lines changed

Zend/zend.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ 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+
/* for blocking/unblocking signals */
71+
ZEND_API sigset_t mask_all_signals;
72+
73+
# if ZEND_DEBUG
74+
ZEND_API TSRM_TLS int _signals_masked = 0;
75+
# endif
76+
#endif
77+
6978
/* true multithread-shared globals */
7079
ZEND_API zend_class_entry *zend_standard_class_def = NULL;
7180
ZEND_API size_t (*zend_printf)(const char *format, ...);
@@ -803,6 +812,10 @@ int zend_startup(zend_utility_functions *utility_functions) /* {{{ */
803812
fpsetmask(0);
804813
#endif
805814

815+
#ifdef HAVE_SIGPROCMASK
816+
sigfillset(&mask_all_signals);
817+
#endif
818+
806819
zend_startup_strtod();
807820
zend_startup_extensions_mechanism();
808821

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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
extern TSRM_TLS int _signals_masked;
28+
29+
# define DEBUG_BLOCK_ALL_SIGNALS() _signals_masked += 1
30+
# define DEBUG_UNBLOCK_ALL_SIGNALS() \
31+
if (--_signals_masked) \
32+
zend_error_noreturn(E_ERROR, "Cannot nest BLOCK_ALL_SIGNALS; it is not re-entrant")
33+
# else
34+
# define DEBUG_BLOCK_ALL_SIGNALS() do {} while (0)
35+
# define DEBUG_UNBLOCK_ALL_SIGNALS() do {} while (0)
36+
# endif
37+
38+
# define ZEND_SIGNAL_BLOCK_INTERRUPTIONS() \
39+
sigset_t _oldmask; \
40+
DEBUG_BLOCK_ALL_SIGNALS(); \
41+
MASK_ALL_SIGNALS()
42+
# define ZEND_SIGNAL_UNBLOCK_INTERRUPTIONS() \
43+
DEBUG_UNBLOCK_ALL_SIGNALS(); \
44+
UNMASK_ALL_SIGNALS()
45+
46+
# ifdef ZTS
47+
# define MASK_ALL_SIGNALS() \
48+
tsrm_sigmask(SIG_BLOCK, &mask_all_signals, &_oldmask)
49+
# define UNMASK_ALL_SIGNALS() \
50+
tsrm_sigmask(SIG_SETMASK, &_oldmask, NULL)
51+
# else
52+
# define MASK_ALL_SIGNALS() \
53+
sigprocmask(SIG_BLOCK, &mask_all_signals, &_oldmask)
54+
# define UNMASK_ALL_SIGNALS() \
55+
sigprocmask(SIG_SETMASK, &_oldmask, NULL)
56+
# endif
57+
58+
#else
59+
60+
# define ZEND_SIGNAL_BLOCK_INTERRUPTIONS() do {} while(0)
61+
# define ZEND_SIGNAL_UNBLOCK_INTERRUPTIONS() do {} while(0)
62+
63+
#endif /* HAVE_SIGPROCMASK */
64+
#endif /* ZEND_SIGNAL_H */

ext/opcache/ZendAccelerator.c

Lines changed: 26 additions & 73 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);
@@ -787,7 +744,7 @@ static zend_string* ZEND_FASTCALL accel_replace_string_by_shm_permanent(zend_str
787744

788745
static void accel_use_shm_interned_strings(void)
789746
{
790-
BLOCK_ALL_SIGNALS();
747+
HANDLE_BLOCK_INTERRUPTIONS();
791748
SHM_UNPROTECT();
792749
zend_shared_alloc_lock();
793750

@@ -802,7 +759,7 @@ static void accel_use_shm_interned_strings(void)
802759

803760
zend_shared_alloc_unlock();
804761
SHM_PROTECT();
805-
UNBLOCK_ALL_SIGNALS();
762+
HANDLE_UNBLOCK_INTERRUPTIONS();
806763
}
807764

808765
#ifndef ZEND_WIN32
@@ -1201,7 +1158,7 @@ char *accel_make_persistent_key(const char *path, size_t path_length, int *key_l
12011158

12021159
zend_string *str = accel_find_interned_string(cwd_str);
12031160
if (!str) {
1204-
BLOCK_ALL_SIGNALS();
1161+
HANDLE_BLOCK_INTERRUPTIONS();
12051162
SHM_UNPROTECT();
12061163
zend_shared_alloc_lock();
12071164
str = accel_new_interned_string(zend_string_copy(cwd_str));
@@ -1211,7 +1168,7 @@ char *accel_make_persistent_key(const char *path, size_t path_length, int *key_l
12111168
}
12121169
zend_shared_alloc_unlock();
12131170
SHM_PROTECT();
1214-
UNBLOCK_ALL_SIGNALS();
1171+
HANDLE_UNBLOCK_INTERRUPTIONS();
12151172
}
12161173
if (str) {
12171174
char buf[32];
@@ -1245,7 +1202,7 @@ char *accel_make_persistent_key(const char *path, size_t path_length, int *key_l
12451202

12461203
zend_string *str = accel_find_interned_string(ZCG(include_path));
12471204
if (!str) {
1248-
BLOCK_ALL_SIGNALS();
1205+
HANDLE_BLOCK_INTERRUPTIONS();
12491206
SHM_UNPROTECT();
12501207
zend_shared_alloc_lock();
12511208
str = accel_new_interned_string(zend_string_copy(ZCG(include_path)));
@@ -1254,7 +1211,7 @@ char *accel_make_persistent_key(const char *path, size_t path_length, int *key_l
12541211
}
12551212
zend_shared_alloc_unlock();
12561213
SHM_PROTECT();
1257-
UNBLOCK_ALL_SIGNALS();
1214+
HANDLE_UNBLOCK_INTERRUPTIONS();
12581215
}
12591216
if (str) {
12601217
char buf[32];
@@ -1351,7 +1308,7 @@ int zend_accel_invalidate(const char *filename, size_t filename_len, zend_bool f
13511308
if (force ||
13521309
!ZCG(accel_directives).validate_timestamps ||
13531310
do_validate_timestamps(persistent_script, &file_handle) == FAILURE) {
1354-
BLOCK_ALL_SIGNALS();
1311+
HANDLE_BLOCK_INTERRUPTIONS();
13551312
SHM_UNPROTECT();
13561313
zend_shared_alloc_lock();
13571314
if (!persistent_script->corrupted) {
@@ -1366,7 +1323,7 @@ int zend_accel_invalidate(const char *filename, size_t filename_len, zend_bool f
13661323
}
13671324
zend_shared_alloc_unlock();
13681325
SHM_PROTECT();
1369-
UNBLOCK_ALL_SIGNALS();
1326+
HANDLE_UNBLOCK_INTERRUPTIONS();
13701327
}
13711328
}
13721329

@@ -1881,11 +1838,11 @@ zend_op_array *file_cache_compile_file(zend_file_handle *file_handle, int type)
18811838
}
18821839
}
18831840

1884-
BLOCK_ALL_SIGNALS();
1841+
HANDLE_BLOCK_INTERRUPTIONS();
18851842
SHM_UNPROTECT();
18861843
persistent_script = zend_file_cache_script_load(file_handle);
18871844
SHM_PROTECT();
1888-
UNBLOCK_ALL_SIGNALS();
1845+
HANDLE_UNBLOCK_INTERRUPTIONS();
18891846
if (persistent_script) {
18901847
/* see bug #15471 (old BTS) */
18911848
if (persistent_script->script.filename) {
@@ -2043,13 +2000,13 @@ zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)
20432000
persistent_script = (zend_persistent_script *)bucket->data;
20442001

20452002
if (key && !persistent_script->corrupted) {
2046-
BLOCK_ALL_SIGNALS();
2003+
HANDLE_BLOCK_INTERRUPTIONS();
20472004
SHM_UNPROTECT();
20482005
zend_shared_alloc_lock();
20492006
zend_accel_add_key(key, key_length, bucket);
20502007
zend_shared_alloc_unlock();
20512008
SHM_PROTECT();
2052-
UNBLOCK_ALL_SIGNALS();
2009+
HANDLE_UNBLOCK_INTERRUPTIONS();
20532010
}
20542011
}
20552012
}
@@ -2094,7 +2051,7 @@ zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)
20942051
return NULL;
20952052
}
20962053

2097-
BLOCK_ALL_SIGNALS();
2054+
HANDLE_BLOCK_INTERRUPTIONS();
20982055
SHM_UNPROTECT();
20992056

21002057
/* If script is found then validate_timestamps if option is enabled */
@@ -2157,17 +2114,17 @@ zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)
21572114
/* No memory left. Behave like without the Accelerator */
21582115
if (ZSMMG(memory_exhausted) || ZCSG(restart_pending)) {
21592116
SHM_PROTECT();
2160-
UNBLOCK_ALL_SIGNALS();
2117+
HANDLE_UNBLOCK_INTERRUPTIONS();
21612118
if (ZCG(accel_directives).file_cache) {
21622119
return file_cache_compile_file(file_handle, type);
21632120
}
21642121
return accelerator_orig_compile_file(file_handle, type);
21652122
}
21662123

21672124
SHM_PROTECT();
2168-
UNBLOCK_ALL_SIGNALS();
2125+
HANDLE_UNBLOCK_INTERRUPTIONS();
21692126
persistent_script = opcache_compile_file(file_handle, type, key, &op_array);
2170-
BLOCK_ALL_SIGNALS();
2127+
HANDLE_BLOCK_INTERRUPTIONS();
21712128
SHM_UNPROTECT();
21722129

21732130
/* Try and cache the script and assume that it is returned from_shared_memory.
@@ -2183,7 +2140,7 @@ zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)
21832140
*/
21842141
if (!persistent_script) {
21852142
SHM_PROTECT();
2186-
UNBLOCK_ALL_SIGNALS();
2143+
HANDLE_UNBLOCK_INTERRUPTIONS();
21872144
return op_array;
21882145
}
21892146
if (from_shared_memory) {
@@ -2237,7 +2194,7 @@ zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)
22372194
persistent_script->dynamic_members.last_used = ZCG(request_time);
22382195

22392196
SHM_PROTECT();
2240-
UNBLOCK_ALL_SIGNALS();
2197+
HANDLE_UNBLOCK_INTERRUPTIONS();
22412198

22422199
/* Fetch jit auto globals used in the script before execution */
22432200
if (persistent_script->ping_auto_globals_mask) {
@@ -2343,13 +2300,13 @@ static zend_string* persistent_zend_resolve_path(const char *filename, size_t fi
23432300
if (!persistent_script->corrupted) {
23442301
if (key) {
23452302
/* add another "key" for the same bucket */
2346-
BLOCK_ALL_SIGNALS();
2303+
HANDLE_BLOCK_INTERRUPTIONS();
23472304
SHM_UNPROTECT();
23482305
zend_shared_alloc_lock();
23492306
zend_accel_add_key(key, key_length, bucket);
23502307
zend_shared_alloc_unlock();
23512308
SHM_PROTECT();
2352-
UNBLOCK_ALL_SIGNALS();
2309+
HANDLE_UNBLOCK_INTERRUPTIONS();
23532310
} else {
23542311
ZCG(key_len) = 0;
23552312
}
@@ -2446,7 +2403,7 @@ int accel_activate(INIT_FUNC_ARGS)
24462403
}
24472404
#endif
24482405

2449-
BLOCK_ALL_SIGNALS();
2406+
HANDLE_BLOCK_INTERRUPTIONS();
24502407
SHM_UNPROTECT();
24512408

24522409
if (ZCG(counted)) {
@@ -2505,7 +2462,7 @@ int accel_activate(INIT_FUNC_ARGS)
25052462
ZCG(accelerator_enabled) = ZCSG(accelerator_enabled);
25062463

25072464
SHM_PROTECT();
2508-
UNBLOCK_ALL_SIGNALS();
2465+
HANDLE_UNBLOCK_INTERRUPTIONS();
25092466

25102467
if (ZCG(accelerator_enabled) && ZCSG(last_restart_time) != ZCG(last_restart_time)) {
25112468
/* SHM was reinitialized. */
@@ -2928,10 +2885,6 @@ static int accel_startup(zend_extension *extension)
29282885
}
29292886
#endif
29302887

2931-
#ifdef HAVE_SIGPROCMASK
2932-
sigfillset(&mask_all_signals);
2933-
#endif
2934-
29352888
/* no supported SAPI found - disable acceleration and stop initialization */
29362889
if (accel_find_sapi() == FAILURE) {
29372890
accel_startup_ok = 0;
@@ -3204,7 +3157,7 @@ void zend_accel_schedule_restart(zend_accel_restart_reason reason)
32043157
zend_accel_error(ACCEL_LOG_DEBUG, "Restart Scheduled! Reason: %s",
32053158
zend_accel_restart_reason_text[reason]);
32063159

3207-
BLOCK_ALL_SIGNALS();
3160+
HANDLE_BLOCK_INTERRUPTIONS();
32083161
SHM_UNPROTECT();
32093162
ZCSG(restart_pending) = 1;
32103163
ZCSG(restart_reason) = reason;
@@ -3217,7 +3170,7 @@ void zend_accel_schedule_restart(zend_accel_restart_reason reason)
32173170
ZCSG(force_restart_time) = 0;
32183171
}
32193172
SHM_PROTECT();
3220-
UNBLOCK_ALL_SIGNALS();
3173+
HANDLE_UNBLOCK_INTERRUPTIONS();
32213174
}
32223175

32233176
/* this is needed because on WIN32 lock is not decreased unless ZCG(counted) is set */
@@ -4696,7 +4649,7 @@ static int accel_preload(const char *config)
46964649
ZCSG(preload_script) = preload_script_in_shared_memory(script);
46974650

46984651
SHM_PROTECT();
4699-
UNBLOCK_ALL_SIGNALS();
4652+
HANDLE_UNBLOCK_INTERRUPTIONS();
47004653

47014654
zend_string_release(filename);
47024655

@@ -4722,7 +4675,7 @@ static int accel_preload(const char *config)
47224675
accel_interned_strings_save_state();
47234676

47244677
SHM_PROTECT();
4725-
UNBLOCK_ALL_SIGNALS();
4678+
HANDLE_UNBLOCK_INTERRUPTIONS();
47264679

47274680
zend_shared_alloc_destroy_xlat_table();
47284681

0 commit comments

Comments
 (0)