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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
|
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2009 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Jason Greene <jason@inetgurus.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#define PCNTL_DEBUG 0
#if PCNTL_DEBUG
#define DEBUG_OUT printf("DEBUG: ");printf
#define IF_DEBUG(z) z
#else
#define IF_DEBUG(z)
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_pcntl.h"
#if HAVE_GETPRIORITY || HAVE_SETPRIORITY || HAVE_WAIT3
#include <sys/time.h>
#include <sys/resource.h>
#endif
ZEND_DECLARE_MODULE_GLOBALS(pcntl)
static PHP_GINIT_FUNCTION(pcntl);
zend_function_entry pcntl_functions[] = {
PHP_FE(pcntl_fork, NULL)
PHP_FE(pcntl_waitpid, second_arg_force_ref)
PHP_FE(pcntl_wait, first_arg_force_ref)
PHP_FE(pcntl_signal, NULL)
PHP_FE(pcntl_wifexited, NULL)
PHP_FE(pcntl_wifstopped, NULL)
PHP_FE(pcntl_wifsignaled, NULL)
PHP_FE(pcntl_wexitstatus, NULL)
PHP_FE(pcntl_wtermsig, NULL)
PHP_FE(pcntl_wstopsig, NULL)
PHP_FE(pcntl_exec, NULL)
PHP_FE(pcntl_alarm, NULL)
#ifdef HAVE_GETPRIORITY
PHP_FE(pcntl_getpriority, NULL)
#endif
#ifdef HAVE_SETPRIORITY
PHP_FE(pcntl_setpriority, NULL)
#endif
{NULL, NULL, NULL}
};
zend_module_entry pcntl_module_entry = {
STANDARD_MODULE_HEADER,
"pcntl",
pcntl_functions,
PHP_MINIT(pcntl),
PHP_MSHUTDOWN(pcntl),
PHP_RINIT(pcntl),
PHP_RSHUTDOWN(pcntl),
PHP_MINFO(pcntl),
NO_VERSION_YET,
PHP_MODULE_GLOBALS(pcntl),
PHP_GINIT(pcntl),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
#ifdef COMPILE_DL_PCNTL
ZEND_GET_MODULE(pcntl)
# ifdef PHP_WIN32
# include "zend_arg_defs.c"
# endif
#endif
static void pcntl_signal_handler(int);
static void pcntl_tick_handler();
void php_register_signal_constants(INIT_FUNC_ARGS)
{
/* Wait Constants */
#ifdef WNOHANG
REGISTER_LONG_CONSTANT("WNOHANG", (long) WNOHANG, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef WUNTRACED
REGISTER_LONG_CONSTANT("WUNTRACED", (long) WUNTRACED, CONST_CS | CONST_PERSISTENT);
#endif
/* Signal Constants */
REGISTER_LONG_CONSTANT("SIG_IGN", (long) SIG_IGN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIG_DFL", (long) SIG_DFL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIG_ERR", (long) SIG_ERR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGHUP", (long) SIGHUP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGINT", (long) SIGINT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGQUIT", (long) SIGQUIT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGILL", (long) SIGILL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGTRAP", (long) SIGTRAP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGABRT", (long) SIGABRT, CONST_CS | CONST_PERSISTENT);
#ifdef SIGIOT
REGISTER_LONG_CONSTANT("SIGIOT", (long) SIGIOT, CONST_CS | CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("SIGBUS", (long) SIGBUS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGFPE", (long) SIGFPE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGKILL", (long) SIGKILL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGUSR1", (long) SIGUSR1, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGSEGV", (long) SIGSEGV, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGUSR2", (long) SIGUSR2, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGPIPE", (long) SIGPIPE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGALRM", (long) SIGALRM, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGTERM", (long) SIGTERM, CONST_CS | CONST_PERSISTENT);
#ifdef SIGSTKFLT
REGISTER_LONG_CONSTANT("SIGSTKFLT",(long) SIGSTKFLT, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SIGCLD
REGISTER_LONG_CONSTANT("SIGCLD", (long) SIGCLD, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SIGCHLD
REGISTER_LONG_CONSTANT("SIGCHLD", (long) SIGCHLD, CONST_CS | CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("SIGCONT", (long) SIGCONT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGSTOP", (long) SIGSTOP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGTSTP", (long) SIGTSTP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGTTIN", (long) SIGTTIN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGTTOU", (long) SIGTTOU, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGURG", (long) SIGURG , CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGXCPU", (long) SIGXCPU, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGXFSZ", (long) SIGXFSZ, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGVTALRM",(long) SIGVTALRM, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGPROF", (long) SIGPROF, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGWINCH", (long) SIGWINCH, CONST_CS | CONST_PERSISTENT);
#ifdef SIGPOLL
REGISTER_LONG_CONSTANT("SIGPOLL", (long) SIGPOLL, CONST_CS | CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("SIGIO", (long) SIGIO, CONST_CS | CONST_PERSISTENT);
#ifdef SIGPWR
REGISTER_LONG_CONSTANT("SIGPWR", (long) SIGPWR, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SIGSYS
REGISTER_LONG_CONSTANT("SIGSYS", (long) SIGSYS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SIGBABY", (long) SIGSYS, CONST_CS | CONST_PERSISTENT);
#endif
#if HAVE_GETPRIORITY || HAVE_SETPRIORITY
REGISTER_LONG_CONSTANT("PRIO_PGRP", PRIO_PGRP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PRIO_USER", PRIO_USER, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PRIO_PROCESS", PRIO_PROCESS, CONST_CS | CONST_PERSISTENT);
#endif
}
static PHP_GINIT_FUNCTION(pcntl)
{
memset(pcntl_globals, 0, sizeof(*pcntl_globals));
}
PHP_RINIT_FUNCTION(pcntl)
{
zend_hash_init(&PCNTL_G(php_signal_table), 16, NULL, ZVAL_PTR_DTOR, 0);
PCNTL_G(head) = PCNTL_G(tail) = PCNTL_G(spares) = NULL;
return SUCCESS;
}
PHP_MINIT_FUNCTION(pcntl)
{
php_register_signal_constants(INIT_FUNC_ARGS_PASSTHRU);
php_add_tick_function(pcntl_tick_handler);
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(pcntl)
{
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(pcntl)
{
struct php_pcntl_pending_signal *sig;
/* FIXME: if a signal is delivered after this point, things will go pear shaped;
* need to remove signal handlers */
zend_hash_destroy(&PCNTL_G(php_signal_table));
while (PCNTL_G(head)) {
sig = PCNTL_G(head);
PCNTL_G(head) = sig->next;
efree(sig);
}
while (PCNTL_G(spares)) {
sig = PCNTL_G(spares);
PCNTL_G(spares) = sig->next;
efree(sig);
}
return SUCCESS;
}
PHP_MINFO_FUNCTION(pcntl)
{
php_info_print_table_start();
php_info_print_table_header(2, "pcntl support", "enabled");
php_info_print_table_end();
}
/* {{{ proto int pcntl_fork(void)
Forks the currently running process following the same behavior as the UNIX fork() system call*/
PHP_FUNCTION(pcntl_fork)
{
pid_t id;
id = fork();
if (id == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error %d", errno);
}
RETURN_LONG((long) id);
}
/* }}} */
/* {{{ proto int pcntl_alarm(int seconds)
Set an alarm clock for delivery of a signal*/
PHP_FUNCTION(pcntl_alarm)
{
long seconds;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &seconds) == FAILURE)
return;
RETURN_LONG ((long) alarm(seconds));
}
/* }}} */
/* {{{ proto int pcntl_waitpid(int pid, int &status, int options)
Waits on or returns the status of a forked child as defined by the waitpid() system call */
PHP_FUNCTION(pcntl_waitpid)
{
long pid, options = 0;
zval *z_status = NULL;
int status;
pid_t child_id;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz|l", &pid, &z_status, &options) == FAILURE)
return;
convert_to_long_ex(&z_status);
status = Z_LVAL_P(z_status);
child_id = waitpid((pid_t) pid, &status, options);
Z_LVAL_P(z_status) = status;
RETURN_LONG((long) child_id);
}
/* }}} */
/* {{{ proto int pcntl_wait(int &status)
Waits on or returns the status of a forked child as defined by the waitpid() system call */
PHP_FUNCTION(pcntl_wait)
{
long options = 0;
zval *z_status = NULL;
int status;
pid_t child_id;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &z_status, &options) == FAILURE)
return;
convert_to_long_ex(&z_status);
status = Z_LVAL_P(z_status);
#ifdef HAVE_WAIT3
if(options) {
child_id = wait3(&status, options, NULL);
}
else {
child_id = wait(&status);
}
#else
child_id = wait(&status);
#endif
Z_LVAL_P(z_status) = status;
RETURN_LONG((long) child_id);
}
/* }}} */
/* {{{ proto bool pcntl_wifexited(int status)
Returns true if the child status code represents a successful exit */
PHP_FUNCTION(pcntl_wifexited)
{
#ifdef WIFEXITED
zval **status;
int status_word;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &status) == FAILURE) {
WRONG_PARAM_COUNT;
}
status_word = (int) Z_LVAL_PP(status);
if (WIFEXITED(status_word)) RETURN_TRUE;
#endif
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool pcntl_wifstopped(int status)
Returns true if the child status code represents a stopped process (WUNTRACED must have been used with waitpid) */
PHP_FUNCTION(pcntl_wifstopped)
{
#ifdef WIFSTOPPED
zval **status;
int status_word;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &status) == FAILURE) {
WRONG_PARAM_COUNT;
}
status_word = (int) Z_LVAL_PP(status);
if (WIFSTOPPED(status_word)) RETURN_TRUE;
#endif
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool pcntl_wifsignaled(int status)
Returns true if the child status code represents a process that was terminated due to a signal */
PHP_FUNCTION(pcntl_wifsignaled)
{
#ifdef WIFSIGNALED
zval **status;
int status_word;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &status) == FAILURE) {
WRONG_PARAM_COUNT;
}
status_word = (int) Z_LVAL_PP(status);
if (WIFSIGNALED(status_word)) RETURN_TRUE;
#endif
RETURN_FALSE;
}
/* }}} */
/* {{{ proto int pcntl_wexitstatus(int status)
Returns the status code of a child's exit */
PHP_FUNCTION(pcntl_wexitstatus)
{
#ifdef WEXITSTATUS
zval **status;
int status_word;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &status) == FAILURE) {
WRONG_PARAM_COUNT;
}
status_word = (int) Z_LVAL_PP(status);
/* WEXITSTATUS only returns 8 bits so we *MUST* cast this to signed char
if you want to have valid negative exit codes */
RETURN_LONG((signed char) WEXITSTATUS(status_word));
#else
RETURN_FALSE;
#endif
}
/* }}} */
/* {{{ proto int pcntl_wtermsig(int status)
Returns the number of the signal that terminated the process who's status code is passed */
PHP_FUNCTION(pcntl_wtermsig)
{
#ifdef WTERMSIG
zval **status;
int status_word;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &status) == FAILURE) {
WRONG_PARAM_COUNT;
}
status_word = (int) Z_LVAL_PP(status);
RETURN_LONG(WTERMSIG(status_word));
#else
RETURN_FALSE;
#endif
}
/* }}} */
/* {{{ proto int pcntl_wstopsig(int status)
Returns the number of the signal that caused the process to stop who's status code is passed */
PHP_FUNCTION(pcntl_wstopsig)
{
#ifdef WSTOPSIG
zval **status;
int status_word;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &status) == FAILURE) {
WRONG_PARAM_COUNT;
}
status_word = (int) Z_LVAL_PP(status);
RETURN_LONG(WSTOPSIG(status_word));
#else
RETURN_FALSE;
#endif
}
/* }}} */
/* {{{ proto bool pcntl_exec(string path [, array args [, array envs]])
Executes specified program in current process space as defined by exec(2) */
PHP_FUNCTION(pcntl_exec)
{
zval *args, *envs;
zval **element;
HashTable *args_hash, *envs_hash;
int argc = 0, argi = 0;
int envc = 0, envi = 0;
int return_val = 0;
char **argv = NULL, **envp = NULL;
char **current_arg, **pair;
int pair_length;
char *key;
int key_length;
char *path;
int path_len;
long key_num;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|aa", &path, &path_len, &args, &envs) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS() > 1) {
/* Build argumnent list */
args_hash = HASH_OF(args);
argc = zend_hash_num_elements(args_hash);
argv = safe_emalloc((argc + 2), sizeof(char *), 0);
*argv = path;
for ( zend_hash_internal_pointer_reset(args_hash), current_arg = argv+1;
(argi < argc && (zend_hash_get_current_data(args_hash, (void **) &element) == SUCCESS));
(argi++, current_arg++, zend_hash_move_forward(args_hash)) ) {
convert_to_string_ex(element);
*current_arg = Z_STRVAL_PP(element);
}
*(current_arg) = NULL;
} else {
argv = emalloc(2 * sizeof(char *));
*argv = path;
*(argv+1) = NULL;
}
if ( ZEND_NUM_ARGS() == 3 ) {
/* Build environment pair list */
envs_hash = HASH_OF(envs);
envc = zend_hash_num_elements(envs_hash);
envp = safe_emalloc((envc + 1), sizeof(char *), 0);
for ( zend_hash_internal_pointer_reset(envs_hash), pair = envp;
(envi < envc && (zend_hash_get_current_data(envs_hash, (void **) &element) == SUCCESS));
(envi++, pair++, zend_hash_move_forward(envs_hash)) ) {
switch (return_val = zend_hash_get_current_key_ex(envs_hash, &key, &key_length, &key_num, 0, NULL)) {
case HASH_KEY_IS_LONG:
key = emalloc(101);
snprintf(key, 100, "%ld", key_num);
key_length = strlen(key);
break;
case HASH_KEY_NON_EXISTANT:
pair--;
continue;
}
convert_to_string_ex(element);
/* Length of element + equal sign + length of key + null */
pair_length = Z_STRLEN_PP(element) + key_length + 2;
*pair = emalloc(pair_length);
strlcpy(*pair, key, key_length);
strlcat(*pair, "=", pair_length);
strlcat(*pair, Z_STRVAL_PP(element), pair_length);
/* Cleanup */
if (return_val == HASH_KEY_IS_LONG) efree(key);
}
*(pair) = NULL;
}
if (execve(path, argv, envp) == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error has occured: (errno %d) %s", errno, strerror(errno));
}
/* Cleanup */
if (envp != NULL) {
for (pair = envp; *pair != NULL; pair++) efree(*pair);
efree(envp);
}
efree(argv);
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool pcntl_signal(int signo, callback handle [, bool restart_syscalls])
Assigns a system signal handler to a PHP function */
PHP_FUNCTION(pcntl_signal)
{
zval *handle, **dest_handle = NULL;
char *func_name;
long signo;
zend_bool restart_syscalls = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz|b", &signo, &handle, &restart_syscalls) == FAILURE) {
return;
}
if (!PCNTL_G(spares)) {
/* since calling malloc() from within a signal handler is not portable,
* pre-allocate a few records for recording signals */
int i;
for (i = 0; i < 32; i++) {
struct php_pcntl_pending_signal *psig;
psig = emalloc(sizeof(*psig));
psig->next = PCNTL_G(spares);
PCNTL_G(spares) = psig;
}
}
/* Special long value case for SIG_DFL and SIG_IGN */
if (Z_TYPE_P(handle)==IS_LONG) {
if (Z_LVAL_P(handle)!= (long) SIG_DFL && Z_LVAL_P(handle) != (long) SIG_IGN) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid value for handle argument specified");
}
if (php_signal(signo, (Sigfunc *) Z_LVAL_P(handle), (int) restart_syscalls) == SIG_ERR) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error assigning signal");
RETURN_FALSE;
}
RETURN_TRUE;
}
if (!zend_is_callable(handle, 0, &func_name)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s is not a callable function name error", func_name);
efree(func_name);
RETURN_FALSE;
}
efree(func_name);
/* Add the function name to our signal table */
zend_hash_index_update(&PCNTL_G(php_signal_table), signo, (void **) &handle, sizeof(zval *), (void **) &dest_handle);
if (dest_handle) zval_add_ref(dest_handle);
if (php_signal(signo, pcntl_signal_handler, (int) restart_syscalls) == SIG_ERR) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error assigning signal");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
#ifdef HAVE_GETPRIORITY
/* {{{ proto int pcntl_getpriority([int pid [, int process_identifier]])
Get the priority of any process */
PHP_FUNCTION(pcntl_getpriority)
{
long who = PRIO_PROCESS;
long pid = getpid();
int pri;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &pid, &who) == FAILURE) {
RETURN_FALSE;
}
/* needs to be cleared, since any returned value is valid */
errno = 0;
pri = getpriority(who, pid);
if (errno) {
switch (errno) {
case ESRCH:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error %d: No process was located using the given parameters", errno);
break;
case EINVAL:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error %d: Invalid identifier flag", errno);
break;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown error %d has occured", errno);
break;
}
RETURN_FALSE;
}
RETURN_LONG(pri);
}
/* }}} */
#endif
#ifdef HAVE_SETPRIORITY
/* {{{ proto bool pcntl_setpriority(int priority [, int pid [, int process_identifier]])
Change the priority of any process */
PHP_FUNCTION(pcntl_setpriority)
{
long who = PRIO_PROCESS;
long pid = getpid();
long pri;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|ll", &pri, &pid, &who) == FAILURE) {
RETURN_FALSE;
}
if (setpriority(who, pid, pri)) {
switch (errno) {
case ESRCH:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error %d: No process was located using the given parameters", errno);
break;
case EINVAL:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error %d: Invalid identifier flag", errno);
break;
case EPERM:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error %d: A process was located, but neither its effective nor real user ID matched the effective user ID of the caller", errno);
break;
case EACCES:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error %d: Only a super user may attempt to increase the process priority", errno);
break;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown error %d has occured", errno);
break;
}
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
#endif
/* Our custom signal handler that calls the appropriate php_function */
static void pcntl_signal_handler(int signo)
{
struct php_pcntl_pending_signal *psig;
TSRMLS_FETCH();
psig = PCNTL_G(spares);
if (!psig) {
/* oops, too many signals for us to track, so we'll forget about this one */
return;
}
PCNTL_G(spares) = psig->next;
psig->signo = signo;
psig->next = NULL;
/* the head check is important, as the tick handler cannot atomically clear both
* the head and tail */
if (PCNTL_G(head) && PCNTL_G(tail)) {
PCNTL_G(tail)->next = psig;
} else {
PCNTL_G(head) = psig;
}
PCNTL_G(tail) = psig;
}
void pcntl_tick_handler()
{
zval *param, **handle, *retval;
struct php_pcntl_pending_signal *queue, *next;
TSRMLS_FETCH();
/* Bail if the queue is empty or if we are already playing the queue*/
if (! PCNTL_G(head) || PCNTL_G(processing_signal_queue))
return;
/* Prevent reentrant handler calls */
PCNTL_G(processing_signal_queue) = 1;
queue = PCNTL_G(head);
PCNTL_G(head) = NULL; /* simple stores are atomic */
/* Allocate */
while (queue) {
if (zend_hash_index_find(&PCNTL_G(php_signal_table), queue->signo, (void **) &handle)==SUCCESS) {
MAKE_STD_ZVAL(retval);
MAKE_STD_ZVAL(param);
ZVAL_NULL(retval);
ZVAL_LONG(param, queue->signo);
/* Call php signal handler - Note that we do not report errors, and we ignore the return value */
/* FIXME: this is probably broken when multiple signals are handled in this while loop (retval) */
call_user_function(EG(function_table), NULL, *handle, retval, 1, ¶m TSRMLS_CC);
zval_ptr_dtor(¶m);
zval_ptr_dtor(&retval);
}
next = queue->next;
queue->next = PCNTL_G(spares);
PCNTL_G(spares) = queue;
queue = next;
}
/* Re-enable queue */
PCNTL_G(processing_signal_queue) = 0;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
*/
|