-
Notifications
You must be signed in to change notification settings - Fork 15
/
signal_handler.c
270 lines (234 loc) · 6.86 KB
/
signal_handler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Mateusz Kaczanowski <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_signal_handler.h"
#ifdef PHP_WIN32
# include "win32/php_stdint.h"
# include "win32/signal.h"
#else
# include <signal.h>
# include <stdint.h>
#endif
#if PHP_MAJOR_VERSION < 7
#define _DECLARE_ZVAL(name) zval * name
#define hp_ptr_dtor(val) zval_ptr_dtor(&val);
#define zend_string_release(str) efree(str)
#define ZSTR_VAL(str) (str)
#else
#include <zend_string.h>
#define _DECLARE_ZVAL(name) zval name ## _v; zval * name = &name ## _v
#define hp_ptr_dtor(val) zval_ptr_dtor(val);
#endif
ZEND_DECLARE_MODULE_GLOBALS(signal_handler);
static PHP_GINIT_FUNCTION(signal_handler);
/* {{{ signal_handler_functions[]
*/
const zend_function_entry signal_handler_functions[] = {
PHP_FE(attach_signal, NULL)
PHP_FE(detach_signal, NULL)
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 3 && PHP_RELEASE_VERSION >= 7) || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION > 5)
PHP_FE_END
#else
{NULL,NULL,NULL}
#endif
};
/* }}} */
/* {{{ signal_handler_module_entry
*/
zend_module_entry signal_handler_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"signal_handler",
signal_handler_functions,
PHP_MINIT(signal_handler),
PHP_MSHUTDOWN(signal_handler),
PHP_RINIT(signal_handler),
PHP_RSHUTDOWN(signal_handler),
PHP_MINFO(signal_handler),
#if ZEND_MODULE_API_NO >= 20010901
PHP_SIGNAL_HANDLER_VERSION,
#endif
PHP_MODULE_GLOBALS(signal_handler),
PHP_GINIT(signal_handler),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
#ifdef COMPILE_DL_SIGNAL_HANDLER
ZEND_GET_MODULE(signal_handler)
#endif
/* {{{ PHP_GINIT_FUNCTION
*/
static PHP_GINIT_FUNCTION(signal_handler)
{
memset(signal_handler_globals, 0, sizeof(*signal_handler_globals));
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(signal_handler)
{
zend_hash_init(&SIGNAL_HANDLER_G(php_signal_table), 16, NULL, ZVAL_PTR_DTOR, 0);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(signal_handler)
{
zend_hash_destroy(&SIGNAL_HANDLER_G(php_signal_table));
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(signal_handler)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(signal_handler)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(signal_handler)
{
php_info_print_table_start();
php_info_print_table_header(2, "Signal Handler support", "enabled");
php_info_print_table_end();
}
/* }}} */
void php_signal_callback_handler(int signo)
{
TSRMLS_FETCH();
#if PHP_MAJOR_VERSION >= 7
zval *handle;
_DECLARE_ZVAL(retval);
_DECLARE_ZVAL(param);
handle = zend_hash_index_find(&SIGNAL_HANDLER_G(php_signal_table), signo);
if (!handle) {
#else
zval *param, **handle, *retval;
MAKE_STD_ZVAL(retval);
MAKE_STD_ZVAL(param);
if(zend_hash_index_find(&SIGNAL_HANDLER_G(php_signal_table), signo, (void **) &handle) == FAILURE){
#endif
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Callback function not found for signo: %d", signo);
return;
}
ZVAL_NULL(retval);
ZVAL_LONG(param, signo);
/* Call the function */
#if PHP_MAJOR_VERSION >= 7
call_user_function(EG(function_table), NULL, handle, retval, 1, param TSRMLS_CC);
#else
call_user_function(EG(function_table), NULL, *handle, retval, 1, ¶m TSRMLS_CC);
#endif
hp_ptr_dtor(param);
hp_ptr_dtor(retval);
}
/* {{{ proto attach_signal(int signo, callback)
attach signal handler */
PHP_FUNCTION(attach_signal)
{
zval *handle, **dest_handle = NULL;
#if PHP_MAJOR_VERSION >= 7
zend_string *func_name;
#else
char *func_name;
#endif
long signo;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &signo, &handle) == FAILURE) {
return;
}
/* Check signal code */
if (signo < 1 || signo > 32) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid signal");
RETURN_FALSE;
}
/* Check if given parameter is callable */
#if ZEND_MODULE_API_NO <= 20060613
if (!zend_is_callable(handle, 0, &func_name)) {
#else
if (!zend_is_callable(handle, 0, &func_name TSRMLS_CC)) {
#endif
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s is not a callable function name error", ZSTR_VAL(func_name));
zend_string_release(func_name);
RETURN_FALSE;
}
zend_string_release(func_name);
/* Set the handler for the signal */
if (signal(signo, php_signal_callback_handler) == SIG_ERR) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while setting a signal handler for signo: %lu", signo);
RETURN_FALSE;
}
/* Add the function name to our signal table */
#if PHP_MAJOR_VERSION >= 7
zend_hash_index_update(&SIGNAL_HANDLER_G(php_signal_table), signo, handle);
zval_add_ref(handle);
#else
zend_hash_index_update(&SIGNAL_HANDLER_G(php_signal_table), signo, (void **) &handle, sizeof(zval *), (void **) &dest_handle);
if (dest_handle) zval_add_ref(dest_handle);
#endif
RETURN_TRUE;
}
/* }}} */
/* {{{ proto detach_signal(int signo)
detach signal handler */
PHP_FUNCTION(detach_signal)
{
long signo;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &signo) == FAILURE) {
return;
}
/* Check signal code */
if (signo < 1 || signo > 32) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid signal");
RETURN_FALSE;
}
/* If signal handler is registred, set SIG_DFL */
if(zend_hash_index_exists(&SIGNAL_HANDLER_G(php_signal_table), signo) && signal(signo, SIG_DFL) != SIG_ERR){
zend_hash_index_del(&SIGNAL_HANDLER_G(php_signal_table), signo);
RETURN_TRUE;
}else{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot detach signo: %lu", signo);
RETURN_FALSE;
}
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/