-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtast_process.c
480 lines (391 loc) · 11.5 KB
/
tast_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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
+----------------------------------------------------------------------+
| tast-process |
+----------------------------------------------------------------------+
| Copyright (c) 2017 xujiajun |
+----------------------------------------------------------------------+
| This source file is subject to MIT license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://opensource.org/licenses/MIT |
| If you did not receive a copy of the MIT 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: xujiajun <[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_tast_process.h"
#include <errno.h>
/* If you declare any globals in php_tast_process.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(tast_process)
*/
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_process, 0, 0, 2)
ZEND_ARG_ARRAY_INFO(0, funs_arr, 0)
ZEND_ARG_INFO(0, open_process_num)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_fork_void, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_kill, 0, 0, 2)
ZEND_ARG_INFO(0, pid)
ZEND_ARG_INFO(0, sig)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_waitpid, 0, 0, 2)
ZEND_ARG_INFO(0, pid)
ZEND_ARG_INFO(1, z_status)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_getpid_void, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_getppid_void, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wifexitstatus, 0, 0, 1)
ZEND_ARG_INFO(1, status)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_strerror, 0, 0, 1)
ZEND_ARG_INFO(0, errno)
ZEND_END_ARG_INFO()
/* True global resources - no need for thread safety here */
static int le_tast_process;
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("tast_process.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_tast_process_globals, tast_process_globals)
STD_PHP_INI_ENTRY("tast_process.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_tast_process_globals, tast_process_globals)
PHP_INI_END()
*/
/* }}} */
/* Remove the following function when you have successfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_tast_process_compiled(string arg)
Return a string to confirm that the module is compiled in */
#define MAX_PROCESS_NUM 100
static pid_t createProcess(void(*ptr) (),zval *arg);
static int addInProcessList(pid_t pid);
static void waitAllProcess();
static int isOutsideProcess(pid_t pid);
static int isOutsideAllProcess();
static int processError();
static void killProcess();
static void killChildren();
static void childProcess(zval *arg);
static void parentForkInfo(pid_t pid);
static int tprintf(const char* fmt, ...);
int processCount = 0;
pid_t processList[MAX_PROCESS_NUM];
PHP_FUNCTION(tast_process)
{
zval *funs_arr,*fun;
long open_process_num;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "al", &funs_arr, &open_process_num) == FAILURE) {
return;
}
if (open_process_num < 0 || open_process_num > MAX_PROCESS_NUM) {
tast_php_error(E_ERROR, "Invalid argument `open_process_num` (%d)", open_process_num);
}
printf("Hello from Parent Process, PID is %d.\n", getpid());
for (int i = 0; i < open_process_num; ++i) {
fun = zend_hash_index_find(Z_ARRVAL_P(funs_arr), i);
createProcess(childProcess,fun);
}
if (isOutsideAllProcess()) {
for (int i = 0; i < open_process_num; ++i) {
parentForkInfo(processList[i]);
}
waitAllProcess();
tprintf("Children processes have exited.\n");
tprintf("Parent had exited.\n");
} else if (!processError()) {
tprintf("Everything was gone without error.\n");
}
RETURN_NULL();
}
PHP_FUNCTION(tast_fork)
{
pid_t pid;
pid = fork();
if (pid < 0) {
tast_php_error(E_WARNING, "Fork error (%s) \n", strerror(errno));
}
RETURN_LONG((zend_long) pid);
}
PHP_FUNCTION(tast_getpid)
{
pid_t pid = getpid();
RETURN_LONG((zend_long) pid);
}
PHP_FUNCTION(tast_getppid)
{
pid_t ppid = getppid();
RETURN_LONG((zend_long) ppid);
}
PHP_FUNCTION(tast_kill)
{
int pid;
int sig = SIGTERM;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &pid, &sig) == FAILURE) {
return;
}
int ret = kill((int) pid,(int) sig);
if (ret < 0)
{
if (!(sig == 0 && errno == ESRCH))
{
tast_php_error(E_WARNING, "kill(%d, %d) failed. Error: %s[%d]", (int) pid, (int) sig, strerror(errno), errno);
}
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(tast_waitpid)
{
zend_long pid, options = 0;
zval *z_status = NULL;
int status;
pid_t child_id;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "lz/|l", &pid, &z_status, &options) == FAILURE) {
return;
}
status = zval_get_long(z_status);
child_id = waitpid((pid_t) pid, &status, options);
if (child_id < 0 && errno != ECHILD) {
tast_php_error(E_WARNING, "error in waitpid (%s) \n", strerror(errno));
}
zval_dtor(z_status);
ZVAL_LONG(z_status, status);
RETURN_LONG((zend_long) child_id);
}
PHP_FUNCTION(tast_wexitstatus)
{
#ifdef WEXITSTATUS
zend_long status_word;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &status_word) == FAILURE) {
return;
}
RETURN_LONG(WEXITSTATUS(status_word));
#else
RETURN_FALSE;
#endif
}
PHP_FUNCTION(tast_strerror)
{
zend_long error;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &error) == FAILURE) {
RETURN_FALSE;
}
RETURN_STRING(strerror(error));
}
static int addInProcessList(pid_t pid)
{
if (processCount < MAX_PROCESS_NUM) {
processList[processCount] = pid;
processCount = processCount + 1;
return SUCCESS;
}
return FAILURE;
}
static void waitAllProcess()
{
int i;
for(i = 0;i < processCount; ++i){
waitpid(processList[i], NULL, 0);
}
}
static int processError()
{
int i;
for(i = 0;i < processCount; ++i){
if(processList[i] == -1){
return SUCCESS;
}
}
return FAILURE;
}
static pid_t createProcess(void(*ptr) (),zval *arg)
{
pid_t pid;
if (isOutsideAllProcess()) {
pid = fork();
if (FAILURE != addInProcessList(pid)) {
if (pid == 0) {
(*ptr)(arg);
}
} else {
printf("Full processes!");
killChildren();
exit(1);
}
}
return pid;
}
static void killProcess(pid_t pid)
{
if (pid == 0) {
printf("Process %d exit!", getpid());
exit(1);
}
}
static void killChildren()
{
int i;
for (i = 0;i < processCount; ++i) {
killProcess(processList[i]);
}
}
static int isOutsideProcess(pid_t pid)
{
return (int) (pid != -1 && pid != 0);
}
static int isOutsideAllProcess()
{
int i;
for (i = 0;i < processCount;++i) {
if (isOutsideProcess(processList[i]) == 0) {
return 0;
}
}
return 1;
}
static void childProcess(zval *arg)
{
int i;
zval ret;
if (FAILURE == call_user_function_ex(EG(function_table), NULL, arg, &ret, 0, NULL, 0, NULL TSRMLS_CC)) {
tast_php_error(E_WARNING, "failed to call function ");
}
tprintf("Hello from Child Process %d. %d times\n", getpid(), i+1);
}
static void parentForkInfo(pid_t pid)
{
tprintf("Parent forked one child process -- %d.\n", pid);
tprintf("Parent is waiting for children to exit.\n");
}
static int tprintf(const char* fmt, ...)
{
va_list args;
struct tm * tstruct;
time_t tsec = time(NULL);
tstruct = localtime (&tsec);
printf("%02d:%02d:%02d: %5d|", tstruct->tm_hour, tstruct->tm_min, tstruct->tm_sec, getpid());
va_start(args, fmt);
return vprintf(fmt, args);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
const zend_function_entry tast_process_functions[] = {
PHP_FE(tast_process, arginfo_process)
PHP_FE(tast_fork, arginfo_fork_void)
PHP_FE(tast_kill, arginfo_kill)
PHP_FE(tast_getpid, arginfo_getpid_void)
PHP_FE(tast_getppid, arginfo_getppid_void)
PHP_FE(tast_waitpid, arginfo_waitpid)
PHP_FE(tast_wexitstatus, arginfo_wifexitstatus)
PHP_FE(tast_strerror, arginfo_strerror)
PHP_FE_END
};
/* {{{ php_tast_process_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_tast_process_init_globals(zend_tast_process_globals *tast_process_globals)
{
tast_process_globals->global_value = 0;
tast_process_globals->global_string = NULL;
}
*/
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(tast_process)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(tast_process)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(tast_process)
{
#if defined(COMPILE_DL_TAST_PROCESS) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(tast_process)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(tast_process)
{
php_info_print_table_start();
php_info_print_table_header(2, "tast_process support", "enabled");
php_info_print_table_row(2, "version", PHP_TAST_PROCESS_VERSION);
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
/* {{{ tast_process_module_entry
*/
zend_module_entry tast_process_module_entry = {
STANDARD_MODULE_HEADER,
"tast_process",
tast_process_functions,
PHP_MINIT(tast_process),
PHP_MSHUTDOWN(tast_process),
PHP_RINIT(tast_process), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(tast_process), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(tast_process),
PHP_TAST_PROCESS_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_TAST_PROCESS
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(tast_process)
#endif
/*
* 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
*/