forked from Jille/php-sigsock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sigsock.c
230 lines (194 loc) · 5.28 KB
/
sigsock.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
/*
+----------------------------------------------------------------------+
| unknown license: |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
*/
/* $ Id: $ */
#include <fcntl.h>
#include "php_sigsock.h"
#include "php_network.h"
#include "php_signal.c"
#if HAVE_SIGSOCK
#undef ZEND_ASSERT
#define ZEND_ASSERT(x) if (!(x)) { printf("Assertion failed: " #x "\n"); abort(); }
ZEND_DECLARE_MODULE_GLOBALS(sigsock)
ZEND_BEGIN_ARG_INFO_EX(arginfo_sigsock_register, 0, 0, 2)
ZEND_ARG_INFO(0, stream)
ZEND_ARG_INFO(0, signo)
ZEND_ARG_INFO(0, restart_syscalls)
ZEND_END_ARG_INFO()
/* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(sigsock)
{
int i;
for(i = 0; SIGSOCK_MAX_SIGNALS > i; i++) {
SIGSOCK_G(socktable)[i] = -1;
SIGSOCK_G(streamtable)[i] = NULL;
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(sigsock)
{
int i;
for(i = 0; SIGSOCK_MAX_SIGNALS > i; i++) {
if (SIGSOCK_G(socktable)[i] != -1) {
ZEND_ASSERT(SIGSOCK_G(streamtable)[i] != NULL);
close(SIGSOCK_G(socktable)[i]);
zval_ptr_dtor(&SIGSOCK_G(streamtable)[i]);
} else {
ZEND_ASSERT(SIGSOCK_G(streamtable)[i] == NULL);
}
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(sigsock)
{
php_printf("Receive signals via a stream\n");
php_info_print_table_start();
php_info_print_table_row(2, "Version", PHP_SIGSOCK_VERSION);
php_info_print_table_end();
}
/* }}} */
static void sigsock_signal_handler(int signo)
{
TSRMLS_FETCH();
int fd = SIGSOCK_G(socktable)[signo];
ZEND_ASSERT(fd != -1);
char buf = signo;
if (write(fd, &buf, 1) == -1) {
// TODO: Can we do anything reasonable here?
}
}
static zend_bool sigsock_sock_is_used(int fd) {
int i;
for(i = 0; SIGSOCK_MAX_SIGNALS > i; i++) {
if (SIGSOCK_G(socktable)[fd] == SIGSOCK_G(socktable)[i]) {
return 1;
}
}
return 0;
}
/* {{{ proto stream sigsock_register(stream s, int signo [, bool restart_syscalls])
Assigns a system signal handler to a PHP stream */
PHP_FUNCTION(sigsock_register)
{
zval *z_stream;
long signo;
zend_bool restart_syscalls = 1;
int copyfromsig;
int old_fd = 1, new_fd = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r!l|b", &z_stream, &signo, &restart_syscalls) == FAILURE) {
return;
}
if (signo < 1 || signo > SIGSOCK_MAX_SIGNALS) {
php_error_docref(NULL, E_WARNING, "Invalid signal");
RETURN_FALSE;
}
if (z_stream == NULL) {
copyfromsig = -1;
} else {
int i;
ZEND_ASSERT(Z_TYPE_P(z_stream) == IS_RESOURCE);
copyfromsig = -1;
for(i = 0; SIGSOCK_MAX_SIGNALS > i; i++) {
if (SIGSOCK_G(streamtable)[i] != NULL) {
if (SIGSOCK_G(streamtable)[i] == z_stream) {
copyfromsig = i;
break;
}
}
}
if (copyfromsig == -1) {
php_error_docref(NULL, E_WARNING, "Stream given to sigsock_register() was not created by sigsock");
RETURN_FALSE;
}
}
if (copyfromsig == -1) {
int pair[2];
php_stream *stream;
// TODO: CLOEXEC
if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create socket pair: %s", strerror(errno));
return;
}
int flags = fcntl(pair[0], F_GETFL);
if (flags == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to fcntl: %s", strerror(errno));
close(pair[0]);
close(pair[1]);
return;
}
flags |= O_NONBLOCK;
if (fcntl(pair[0], F_SETFL, flags) == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to fcntl: %s", strerror(errno));
close(pair[0]);
close(pair[1]);
return;
}
new_fd = pair[0];
stream = php_stream_sock_open_from_socket(pair[1], 0);
php_stream_auto_cleanup(stream);
RETVAL_RESOURCE(php_stream_get_resource_id(stream));
} else {
new_fd = SIGSOCK_G(socktable)[copyfromsig];
RETVAL_ZVAL(SIGSOCK_G(streamtable)[copyfromsig], 1, 0);
}
if (php_signal4(signo, sigsock_signal_handler, (int) restart_syscalls, 1) == SIG_ERR) {
php_error_docref(NULL, E_WARNING, "Error assigning signal");
zval_ptr_dtor(&return_value);
if (!sigsock_sock_is_used(new_fd)) {
close(new_fd);
}
RETURN_FALSE;
}
Z_ADDREF_P(return_value);
old_fd = SIGSOCK_G(socktable)[signo];
SIGSOCK_G(socktable)[signo] = new_fd;
if (old_fd != -1) {
if (!sigsock_sock_is_used(old_fd)) {
close(old_fd);
}
ZEND_ASSERT(SIGSOCK_G(streamtable)[signo] != NULL);
zval_ptr_dtor(&SIGSOCK_G(streamtable)[signo]);
}
SIGSOCK_G(streamtable)[signo] = return_value;
}
/* }}} */
/* {{{ sigsock_functions[] */
zend_function_entry sigsock_functions[] = {
PHP_FE(sigsock_register, arginfo_sigsock_register)
{ NULL, NULL, NULL }
};
/* }}} */
/* {{{ sigsock_module_entry
*/
zend_module_entry sigsock_module_entry = {
STANDARD_MODULE_HEADER,
"sigsock",
sigsock_functions,
NULL,
NULL,
PHP_RINIT(sigsock), /* Replace with NULL if there is nothing to do at request start */
PHP_RSHUTDOWN(sigsock), /* Replace with NULL if there is nothing to do at request end */
PHP_MINFO(sigsock),
PHP_SIGSOCK_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_SIGSOCK
ZEND_GET_MODULE(sigsock)
#endif
#endif /* HAVE_SIGSOCK */
/*
* 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
*/