forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_process.c
425 lines (351 loc) · 9.33 KB
/
swoole_process.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache 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.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
#include "php_streams.h"
#include "php_network.h"
#define SWOOLE_GET_WORKER(zobject, process) zval **zprocess;\
if (zend_hash_find(Z_OBJPROP_P(zobject), ZEND_STRS("_process"), (void **) &zprocess) == FAILURE){ \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not have process");\
RETURN_FALSE;}\
ZEND_FETCH_RESOURCE(process, swWorker *, zprocess, -1, SW_RES_PROCESS_NAME, le_swoole_process);
void swoole_destory_process(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
swWorker *process = (swWorker *) rsrc->ptr;
swPipe *_pipe = process->ptr;
if (_pipe)
{
_pipe->close(_pipe);
efree(_pipe);
}
efree(process);
}
PHP_METHOD(swoole_process, __construct)
{
#ifdef ZTS
if(sw_thread_ctx == NULL)
{
TSRMLS_SET_CTX(sw_thread_ctx);
}
#endif
/**
* Reactor has already been created, Dont fork.
*/
if (SwooleG.main_reactor && SwooleGS->start == 0)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "swoole_process must create before the event loop.");
return;
}
zend_bool redirect_stdin_and_stdout = 0;
zend_bool create_pipe = 1;
zval *callback;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|bb", &callback, &redirect_stdin_and_stdout, &create_pipe) == FAILURE)
{
RETURN_FALSE;
}
char *func_name = NULL;
if (!zend_is_callable(callback, 0, &func_name TSRMLS_CC))
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "function '%s' is not callable", func_name);
efree(func_name);
RETURN_FALSE;
}
efree(func_name);
swWorker *process = emalloc(sizeof(swWorker));
bzero(process, sizeof(swWorker));
if (redirect_stdin_and_stdout)
{
process->redirect_stdin = 1;
process->redirect_stdout = 1;
create_pipe = 1;
}
if (create_pipe)
{
swPipe *_pipe = emalloc(sizeof(swWorker));
if (swPipeUnsock_create(_pipe, 1, SOCK_STREAM) < 0)
{
RETURN_FALSE;
}
process->ptr = _pipe;
process->pipe_master = _pipe->getFd(_pipe, 1);
process->pipe_worker = _pipe->getFd(_pipe, 0);
}
zval *zres;
MAKE_STD_ZVAL(zres);
ZEND_REGISTER_RESOURCE(zres, process, le_swoole_process);
zend_update_property(swoole_process_class_entry_ptr, getThis(), ZEND_STRL("callback"), callback TSRMLS_CC);
zend_update_property(swoole_process_class_entry_ptr, getThis(), ZEND_STRL("_process"), zres TSRMLS_CC);
zval_ptr_dtor(&zres);
}
PHP_METHOD(swoole_process, wait)
{
int status;
pid_t pid = wait(&status);
if (pid > 0)
{
array_init(return_value);
add_assoc_long(return_value, "code", WEXITSTATUS(status));
add_assoc_long(return_value, "pid", pid);
}
else
{
RETURN_FALSE;
}
}
PHP_METHOD(swoole_process, kill)
{
long pid;
long sig = SIGTERM;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &pid, &sig) == FAILURE)
{
RETURN_FALSE;
}
int ret = kill((int) pid, (int) sig);
if (ret < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "kill(%d, %d) failed. Error: %s[%d]", (int) pid, (int) sig, strerror(errno), errno);
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_METHOD(swoole_process, start)
{
swWorker *process;
SWOOLE_GET_WORKER(getThis(), process);
zval *zpipe;
pid_t pid = fork();
if (pid < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "fork() failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
else if(pid > 0)
{
process->pid = pid;
process->pipe = process->pipe_master;
close(process->pipe_worker);
MAKE_STD_ZVAL(zpipe);
ZVAL_LONG(zpipe, process->pipe);
zend_update_property(swoole_process_class_entry_ptr, getThis(), ZEND_STRL("pipe"), zpipe TSRMLS_CC);
zval_ptr_dtor(&zpipe);
RETURN_LONG(pid);
}
else
{
process->pipe = process->pipe_worker;
process->pid = getpid();
close(process->pipe_master);
if (process->redirect_stdin)
{
if (dup2(process->pipe, STDIN_FILENO) < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "dup2() failed. Error: %s[%d]", strerror(errno), errno);
}
}
if (process->redirect_stdout)
{
if (dup2(process->pipe, STDOUT_FILENO) < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "dup2() failed. Error: %s[%d]", strerror(errno), errno);
}
}
/**
* Close EventLoop
*/
if (SwooleG.main_reactor)
{
SwooleG.main_reactor->free(SwooleG.main_reactor);
SwooleG.main_reactor = NULL;
php_sw_reactor_ok = 0;
}
zval *zpid;
MAKE_STD_ZVAL(zpid);
ZVAL_LONG(zpid, process->pid);
zend_update_property(swoole_server_class_entry_ptr, getThis(), ZEND_STRL("pid"), zpid TSRMLS_CC);
zval_ptr_dtor(&zpid);
MAKE_STD_ZVAL(zpipe);
ZVAL_LONG(zpipe, process->pipe);
zend_update_property(swoole_process_class_entry_ptr, getThis(), ZEND_STRL("pipe"), zpipe TSRMLS_CC);
zval_ptr_dtor(&zpipe);
zval *zcallback = zend_read_property(swoole_process_class_entry_ptr, getThis(), ZEND_STRL("callback"), 0 TSRMLS_CC);
zval **args[1];
if (zcallback == NULL || ZVAL_IS_NULL(zcallback))
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "no callback.");
RETURN_FALSE;
}
zval *retval;
args[0] = &getThis();
if (call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "callback function error");
RETURN_FALSE;
}
if (retval)
{
zval_ptr_dtor(&retval);
}
zend_bailout();
}
RETURN_TRUE;
}
PHP_METHOD(swoole_process, read)
{
long buf_size = 8192;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &buf_size) == FAILURE)
{
RETURN_FALSE;
}
if (buf_size > 65536)
{
buf_size = 65536;
}
swWorker *process;
SWOOLE_GET_WORKER(getThis(), process);
if (process->pipe == 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "have not pipe, can not use write()");
RETURN_FALSE;
}
char *buf = emalloc(buf_size);
int ret;
do
{
ret = read(process->pipe, buf, buf_size - 1);
}
while(errno < 0 && errno == EINTR);
if (ret < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
buf[ret] = 0;
ZVAL_STRINGL(return_value, buf, ret, 0);
}
PHP_METHOD(swoole_process, write)
{
char *data = NULL;
int data_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) == FAILURE)
{
RETURN_FALSE;
}
if (data_len < 1)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "send data empty.");
RETURN_FALSE;
}
swWorker *process;
SWOOLE_GET_WORKER(getThis(), process);
if (process->pipe == 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "have not pipe, can not use read()");
RETURN_FALSE;
}
int ret;
do
{
ret = write(process->pipe, data, (size_t) data_len);
}
while(errno < 0 && errno == EINTR);
if (ret < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
ZVAL_LONG(return_value, ret);
}
PHP_METHOD(swoole_process, exec)
{
char *execfile = NULL;
int execfile_len = 0;
zval *args;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &execfile, &execfile_len, &args) == FAILURE)
{
RETURN_FALSE;
}
if (execfile_len < 1)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "execfile name empty.");
RETURN_FALSE;
}
swWorker *process;
SWOOLE_GET_WORKER(getThis(), process);
if (process->pipe == 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "have not pipe, can not use exec()");
RETURN_FALSE;
}
swBreakPoint();
int exec_argc = php_swoole_array_length(args);
char **exec_args = emalloc(sizeof(char*) * exec_argc + 1);
zval **value;
Bucket *_p;
_p = Z_ARRVAL_P(args)->pListHead;
exec_args[0] = strdup(execfile);
int i = 1;
while(_p != NULL)
{
value = (zval **) _p->pData;
convert_to_string(*value);
zval_add_ref(value);
exec_args[i] = Z_STRVAL_PP(value);
_p = _p->pListNext;
i++;
}
exec_args[i] = NULL;
if (execv(execfile, exec_args) < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "execv() failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
else
{
RETURN_TRUE;
}
}
PHP_METHOD(swoole_process, exit)
{
long ret_code = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &ret_code) == FAILURE)
{
RETURN_FALSE;
}
swWorker *process;
SWOOLE_GET_WORKER(getThis(), process);
if (getpid() != process->pid)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "not current process.");
RETURN_FALSE;
}
if (ret_code < 0 || ret_code > 255)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "exit ret_code range is [>0 and <255] ");
ret_code = 1;
}
close(process->pipe);
if (SwooleG.main_reactor != NULL)
{
SwooleG.running = 0;
}
if (ret_code == 0)
{
zend_bailout();
}
else
{
exit(ret_code);
}
}