2
+----------------------------------------------------------------------+
4
+----------------------------------------------------------------------+
5
| Copyright (c) 1997-2004 The PHP Group |
6
+----------------------------------------------------------------------+
7
| This source file is subject to version 3.0 of the PHP license, |
8
| that is bundled with this package in the file LICENSE, and is |
9
| available through the world-wide-web at the following url: |
10
| http://www.php.net/license/3_0.txt. |
11
| If you did not receive a copy of the PHP license and are unable to |
12
| obtain it through the world-wide-web, please send a note to |
13
| license@php.net so we can mail you a copy immediately. |
14
+----------------------------------------------------------------------+
15
| Author: Jason Greene <jason@inetgurus.net> |
16
+----------------------------------------------------------------------+
19
/* $Id: pcntl.c,v 1.44.2.3 2005/05/07 14:58:02 wez Exp $ */
24
#define DEBUG_OUT printf("DEBUG: ");printf
36
#include "ext/standard/info.h"
37
#include "php_pcntl.h"
39
#if HAVE_GETPRIORITY || HAVE_SETPRIORITY || HAVE_WAIT3
41
#include <sys/resource.h>
44
ZEND_DECLARE_MODULE_GLOBALS(pcntl)
46
function_entry pcntl_functions[] = {
47
PHP_FE(pcntl_fork, NULL)
48
PHP_FE(pcntl_waitpid, second_arg_force_ref)
49
PHP_FE(pcntl_wait, first_arg_force_ref)
50
PHP_FE(pcntl_signal, NULL)
51
PHP_FE(pcntl_wifexited, NULL)
52
PHP_FE(pcntl_wifstopped, NULL)
53
PHP_FE(pcntl_wifsignaled, NULL)
54
PHP_FE(pcntl_wexitstatus, NULL)
55
PHP_FE(pcntl_wtermsig, NULL)
56
PHP_FE(pcntl_wstopsig, NULL)
57
PHP_FE(pcntl_exec, NULL)
58
PHP_FE(pcntl_alarm, NULL)
59
#ifdef HAVE_GETPRIORITY
60
PHP_FE(pcntl_getpriority, NULL)
62
#ifdef HAVE_SETPRIORITY
63
PHP_FE(pcntl_setpriority, NULL)
68
zend_module_entry pcntl_module_entry = {
69
STANDARD_MODULE_HEADER,
78
STANDARD_MODULE_PROPERTIES
81
#ifdef COMPILE_DL_PCNTL
82
ZEND_GET_MODULE(pcntl)
84
# include "zend_arg_defs.c"
88
static void pcntl_signal_handler(int);
89
static void pcntl_tick_handler();
91
void php_register_signal_constants(INIT_FUNC_ARGS)
96
REGISTER_LONG_CONSTANT("WNOHANG", (long) WNOHANG, CONST_CS | CONST_PERSISTENT);
99
REGISTER_LONG_CONSTANT("WUNTRACED", (long) WUNTRACED, CONST_CS | CONST_PERSISTENT);
102
/* Signal Constants */
103
REGISTER_LONG_CONSTANT("SIG_IGN", (long) SIG_IGN, CONST_CS | CONST_PERSISTENT);
104
REGISTER_LONG_CONSTANT("SIG_DFL", (long) SIG_DFL, CONST_CS | CONST_PERSISTENT);
105
REGISTER_LONG_CONSTANT("SIG_ERR", (long) SIG_ERR, CONST_CS | CONST_PERSISTENT);
106
REGISTER_LONG_CONSTANT("SIGHUP", (long) SIGHUP, CONST_CS | CONST_PERSISTENT);
107
REGISTER_LONG_CONSTANT("SIGINT", (long) SIGINT, CONST_CS | CONST_PERSISTENT);
108
REGISTER_LONG_CONSTANT("SIGQUIT", (long) SIGQUIT, CONST_CS | CONST_PERSISTENT);
109
REGISTER_LONG_CONSTANT("SIGILL", (long) SIGILL, CONST_CS | CONST_PERSISTENT);
110
REGISTER_LONG_CONSTANT("SIGTRAP", (long) SIGTRAP, CONST_CS | CONST_PERSISTENT);
111
REGISTER_LONG_CONSTANT("SIGABRT", (long) SIGABRT, CONST_CS | CONST_PERSISTENT);
113
REGISTER_LONG_CONSTANT("SIGIOT", (long) SIGIOT, CONST_CS | CONST_PERSISTENT);
115
REGISTER_LONG_CONSTANT("SIGBUS", (long) SIGBUS, CONST_CS | CONST_PERSISTENT);
116
REGISTER_LONG_CONSTANT("SIGFPE", (long) SIGFPE, CONST_CS | CONST_PERSISTENT);
117
REGISTER_LONG_CONSTANT("SIGKILL", (long) SIGKILL, CONST_CS | CONST_PERSISTENT);
118
REGISTER_LONG_CONSTANT("SIGUSR1", (long) SIGUSR1, CONST_CS | CONST_PERSISTENT);
119
REGISTER_LONG_CONSTANT("SIGSEGV", (long) SIGSEGV, CONST_CS | CONST_PERSISTENT);
120
REGISTER_LONG_CONSTANT("SIGUSR2", (long) SIGUSR2, CONST_CS | CONST_PERSISTENT);
121
REGISTER_LONG_CONSTANT("SIGPIPE", (long) SIGPIPE, CONST_CS | CONST_PERSISTENT);
122
REGISTER_LONG_CONSTANT("SIGALRM", (long) SIGALRM, CONST_CS | CONST_PERSISTENT);
123
REGISTER_LONG_CONSTANT("SIGTERM", (long) SIGTERM, CONST_CS | CONST_PERSISTENT);
125
REGISTER_LONG_CONSTANT("SIGSTKFLT",(long) SIGSTKFLT, CONST_CS | CONST_PERSISTENT);
128
REGISTER_LONG_CONSTANT("SIGCLD", (long) SIGCLD, CONST_CS | CONST_PERSISTENT);
131
REGISTER_LONG_CONSTANT("SIGCHLD", (long) SIGCHLD, CONST_CS | CONST_PERSISTENT);
133
REGISTER_LONG_CONSTANT("SIGCONT", (long) SIGCONT, CONST_CS | CONST_PERSISTENT);
134
REGISTER_LONG_CONSTANT("SIGSTOP", (long) SIGSTOP, CONST_CS | CONST_PERSISTENT);
135
REGISTER_LONG_CONSTANT("SIGTSTP", (long) SIGTSTP, CONST_CS | CONST_PERSISTENT);
136
REGISTER_LONG_CONSTANT("SIGTTIN", (long) SIGTTIN, CONST_CS | CONST_PERSISTENT);
137
REGISTER_LONG_CONSTANT("SIGTTOU", (long) SIGTTOU, CONST_CS | CONST_PERSISTENT);
138
REGISTER_LONG_CONSTANT("SIGURG", (long) SIGURG , CONST_CS | CONST_PERSISTENT);
139
REGISTER_LONG_CONSTANT("SIGXCPU", (long) SIGXCPU, CONST_CS | CONST_PERSISTENT);
140
REGISTER_LONG_CONSTANT("SIGXFSZ", (long) SIGXFSZ, CONST_CS | CONST_PERSISTENT);
141
REGISTER_LONG_CONSTANT("SIGVTALRM",(long) SIGVTALRM, CONST_CS | CONST_PERSISTENT);
142
REGISTER_LONG_CONSTANT("SIGPROF", (long) SIGPROF, CONST_CS | CONST_PERSISTENT);
143
REGISTER_LONG_CONSTANT("SIGWINCH", (long) SIGWINCH, CONST_CS | CONST_PERSISTENT);
145
REGISTER_LONG_CONSTANT("SIGPOLL", (long) SIGPOLL, CONST_CS | CONST_PERSISTENT);
147
REGISTER_LONG_CONSTANT("SIGIO", (long) SIGIO, CONST_CS | CONST_PERSISTENT);
149
REGISTER_LONG_CONSTANT("SIGPWR", (long) SIGPWR, CONST_CS | CONST_PERSISTENT);
152
REGISTER_LONG_CONSTANT("SIGSYS", (long) SIGSYS, CONST_CS | CONST_PERSISTENT);
153
REGISTER_LONG_CONSTANT("SIGBABY", (long) SIGSYS, CONST_CS | CONST_PERSISTENT);
156
#if HAVE_GETPRIORITY || HAVE_SETPRIORITY
157
REGISTER_LONG_CONSTANT("PRIO_PGRP", PRIO_PGRP, CONST_CS | CONST_PERSISTENT);
158
REGISTER_LONG_CONSTANT("PRIO_USER", PRIO_USER, CONST_CS | CONST_PERSISTENT);
159
REGISTER_LONG_CONSTANT("PRIO_PROCESS", PRIO_PROCESS, CONST_CS | CONST_PERSISTENT);
163
static void php_pcntl_init_globals(zend_pcntl_globals *pcntl_globals)
165
memset(pcntl_globals, 0, sizeof(*pcntl_globals));
168
PHP_RINIT_FUNCTION(pcntl)
170
zend_hash_init(&PCNTL_G(php_signal_table), 16, NULL, ZVAL_PTR_DTOR, 0);
171
PCNTL_G(head) = PCNTL_G(tail) = PCNTL_G(spares) = NULL;
175
PHP_MINIT_FUNCTION(pcntl)
177
php_register_signal_constants(INIT_FUNC_ARGS_PASSTHRU);
178
ZEND_INIT_MODULE_GLOBALS(pcntl, php_pcntl_init_globals, NULL);
179
php_add_tick_function(pcntl_tick_handler);
184
PHP_MSHUTDOWN_FUNCTION(pcntl)
189
PHP_RSHUTDOWN_FUNCTION(pcntl)
191
struct php_pcntl_pending_signal *sig;
193
/* FIXME: if a signal is delivered after this point, things will go pear shaped;
194
* need to remove signal handlers */
195
zend_hash_destroy(&PCNTL_G(php_signal_table));
196
while (PCNTL_G(head)) {
198
PCNTL_G(head) = sig->next;
201
while (PCNTL_G(spares)) {
202
sig = PCNTL_G(spares);
203
PCNTL_G(spares) = sig->next;
209
PHP_MINFO_FUNCTION(pcntl)
211
php_info_print_table_start();
212
php_info_print_table_header(2, "pcntl support", "enabled");
213
php_info_print_table_end();
216
/* {{{ proto int pcntl_fork(void)
217
Forks the currently running process following the same behavior as the UNIX fork() system call*/
218
PHP_FUNCTION(pcntl_fork)
224
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Error %d", errno);
227
RETURN_LONG((long) id);
231
/* {{{ proto int pcntl_alarm(int seconds)
232
Set an alarm clock for delivery of a signal*/
233
PHP_FUNCTION(pcntl_alarm)
237
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &seconds) == FAILURE)
240
RETURN_LONG ((long) alarm(seconds));
244
/* {{{ proto int pcntl_waitpid(int pid, int &status, int options)
245
Waits on or returns the status of a forked child as defined by the waitpid() system call */
246
PHP_FUNCTION(pcntl_waitpid)
248
long pid, options = 0;
249
zval *z_status = NULL;
253
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz|l", &pid, &z_status, &options) == FAILURE)
256
convert_to_long_ex(&z_status);
258
status = Z_LVAL_P(z_status);
260
child_id = waitpid((pid_t) pid, &status, options);
262
Z_LVAL_P(z_status) = status;
264
RETURN_LONG((long) child_id);
268
/* {{{ proto int pcntl_wait(int &status)
269
Waits on or returns the status of a forked child as defined by the waitpid() system call */
270
PHP_FUNCTION(pcntl_wait)
273
zval *z_status = NULL;
277
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &z_status, &options) == FAILURE)
280
convert_to_long_ex(&z_status);
282
status = Z_LVAL_P(z_status);
285
child_id = wait3(&status, options, NULL);
288
child_id = wait(&status);
291
child_id = wait(&status);
293
Z_LVAL_P(z_status) = status;
295
RETURN_LONG((long) child_id);
299
/* {{{ proto bool pcntl_wifexited(int status)
300
Returns true if the child status code represents a successful exit */
301
PHP_FUNCTION(pcntl_wifexited)
307
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &status) == FAILURE) {
311
status_word = (int) Z_LVAL_PP(status);
313
if (WIFEXITED(status_word)) RETURN_TRUE;
319
/* {{{ proto bool pcntl_wifstopped(int status)
320
Returns true if the child status code represents a stopped process (WUNTRACED must have been used with waitpid) */
321
PHP_FUNCTION(pcntl_wifstopped)
327
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &status) == FAILURE) {
331
status_word = (int) Z_LVAL_PP(status);
333
if (WIFSTOPPED(status_word)) RETURN_TRUE;
339
/* {{{ proto bool pcntl_wifsignaled(int status)
340
Returns true if the child status code represents a process that was terminated due to a signal */
341
PHP_FUNCTION(pcntl_wifsignaled)
347
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &status) == FAILURE) {
351
status_word = (int) Z_LVAL_PP(status);
353
if (WIFSIGNALED(status_word)) RETURN_TRUE;
359
/* {{{ proto int pcntl_wexitstatus(int status)
360
Returns the status code of a child's exit */
361
PHP_FUNCTION(pcntl_wexitstatus)
367
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &status) == FAILURE) {
371
status_word = (int) Z_LVAL_PP(status);
373
/* WEXITSTATUS only returns 8 bits so we *MUST* cast this to signed char
374
if you want to have valid negative exit codes */
375
RETURN_LONG((signed char) WEXITSTATUS(status_word));
382
/* {{{ proto int pcntl_wtermsig(int status)
383
Returns the number of the signal that terminated the process who's status code is passed */
384
PHP_FUNCTION(pcntl_wtermsig)
390
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &status) == FAILURE) {
394
status_word = (int) Z_LVAL_PP(status);
396
RETURN_LONG(WTERMSIG(status_word));
403
/* {{{ proto int pcntl_wstopsig(int status)
404
Returns the number of the signal that caused the process to stop who's status code is passed */
405
PHP_FUNCTION(pcntl_wstopsig)
411
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &status) == FAILURE) {
415
status_word = (int) Z_LVAL_PP(status);
417
RETURN_LONG(WSTOPSIG(status_word));
424
/* {{{ proto bool pcntl_exec(string path [, array args [, array envs]])
425
Executes specified program in current process space as defined by exec(2) */
426
PHP_FUNCTION(pcntl_exec)
430
HashTable *args_hash, *envs_hash;
431
int argc = 0, argi = 0;
432
int envc = 0, envi = 0;
434
char **argv = NULL, **envp = NULL;
435
char **current_arg, **pair;
443
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|aa", &path, &path_len, &args, &envs) == FAILURE) {
447
if (ZEND_NUM_ARGS() > 1) {
448
/* Build argumnent list */
449
args_hash = HASH_OF(args);
450
argc = zend_hash_num_elements(args_hash);
452
argv = safe_emalloc((argc + 2), sizeof(char *), 0);
454
for ( zend_hash_internal_pointer_reset(args_hash), current_arg = argv+1;
455
(argi < argc && (zend_hash_get_current_data(args_hash, (void **) &element) == SUCCESS));
456
(argi++, current_arg++, zend_hash_move_forward(args_hash)) ) {
458
convert_to_string_ex(element);
459
*current_arg = Z_STRVAL_PP(element);
461
*(current_arg) = NULL;
463
argv = emalloc(2 * sizeof(char *));
468
if ( ZEND_NUM_ARGS() == 3 ) {
469
/* Build environment pair list */
470
envs_hash = HASH_OF(envs);
471
envc = zend_hash_num_elements(envs_hash);
473
envp = safe_emalloc((envc + 1), sizeof(char *), 0);
474
for ( zend_hash_internal_pointer_reset(envs_hash), pair = envp;
475
(envi < envc && (zend_hash_get_current_data(envs_hash, (void **) &element) == SUCCESS));
476
(envi++, pair++, zend_hash_move_forward(envs_hash)) ) {
477
switch (return_val = zend_hash_get_current_key_ex(envs_hash, &key, &key_length, &key_num, 0, NULL)) {
478
case HASH_KEY_IS_LONG:
480
snprintf(key, 100, "%ld", key_num);
481
key_length = strlen(key);
483
case HASH_KEY_NON_EXISTANT:
488
convert_to_string_ex(element);
490
/* Length of element + equal sign + length of key + null */
491
pair_length = Z_STRLEN_PP(element) + key_length + 2;
492
*pair = emalloc(pair_length);
493
strlcpy(*pair, key, key_length);
494
strlcat(*pair, "=", pair_length);
495
strlcat(*pair, Z_STRVAL_PP(element), pair_length);
498
if (return_val == HASH_KEY_IS_LONG) efree(key);
503
if (execve(path, argv, envp) == -1) {
504
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error has occured: (errno %d) %s", errno, strerror(errno));
509
for (pair = envp; *pair != NULL; pair++) efree(*pair);
519
/* {{{ proto bool pcntl_signal(int signo, callback handle [, bool restart_syscalls])
520
Assigns a system signal handler to a PHP function */
521
PHP_FUNCTION(pcntl_signal)
523
zval *handle, **dest_handle = NULL;
526
zend_bool restart_syscalls = 1;
528
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz|b", &signo, &handle, &restart_syscalls) == FAILURE) {
532
if (!PCNTL_G(spares)) {
533
/* since calling malloc() from within a signal handler is not portable,
534
* pre-allocate a few records for recording signals */
536
for (i = 0; i < 32; i++) {
537
struct php_pcntl_pending_signal *psig;
539
psig = emalloc(sizeof(*psig));
540
psig->next = PCNTL_G(spares);
541
PCNTL_G(spares) = psig;
545
/* Special long value case for SIG_DFL and SIG_IGN */
546
if (Z_TYPE_P(handle)==IS_LONG) {
547
if (Z_LVAL_P(handle)!= (long) SIG_DFL && Z_LVAL_P(handle) != (long) SIG_IGN) {
548
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid value for handle argument specifEied");
550
if (php_signal(signo, (Sigfunc *) Z_LVAL_P(handle), (int) restart_syscalls) == SIG_ERR) {
551
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error assigning signal");
557
if (!zend_is_callable(handle, 0, &func_name)) {
558
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s is not a callable function name error", func_name);
564
/* Add the function name to our signal table */
565
zend_hash_index_update(&PCNTL_G(php_signal_table), signo, (void **) &handle, sizeof(zval *), (void **) &dest_handle);
566
if (dest_handle) zval_add_ref(dest_handle);
568
if (php_signal(signo, pcntl_signal_handler, (int) restart_syscalls) == SIG_ERR) {
569
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error assigning signal");
576
#ifdef HAVE_GETPRIORITY
577
/* {{{ proto int pcntl_getpriority([int pid [, int process_identifier]])
578
Get the priority of any process */
579
PHP_FUNCTION(pcntl_getpriority)
581
long who = PRIO_PROCESS;
585
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &pid, &who) == FAILURE) {
589
/* needs to be cleared, since any returned value is valid */
592
pri = getpriority(who, pid);
597
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error %d: No process was located using the given parameters", errno);
600
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error %d: Invalid identifier flag", errno);
603
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown error %d has occured", errno);
614
#ifdef HAVE_SETPRIORITY
615
/* {{{ proto bool pcntl_setpriority(int priority [, int pid [, int process_identifier]])
616
Change the priority of any process */
617
PHP_FUNCTION(pcntl_setpriority)
619
long who = PRIO_PROCESS;
623
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|ll", &pri, &pid, &who) == FAILURE) {
627
if (setpriority(who, pid, pri)) {
630
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error %d: No process was located using the given parameters", errno);
633
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error %d: Invalid identifier flag", errno);
636
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);
639
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error %d: Only a super user may attempt to increase the process priority", errno);
642
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown error %d has occured", errno);
653
/* Our custom signal handler that calls the appropriate php_function */
654
static void pcntl_signal_handler(int signo)
656
struct php_pcntl_pending_signal *psig;
659
psig = PCNTL_G(spares);
661
/* oops, too many signals for us to track, so we'll forget about this one */
664
PCNTL_G(spares) = psig->next;
669
/* the head check is important, as the tick handler cannot atomically clear both
670
* the head and tail */
671
if (PCNTL_G(head) && PCNTL_G(tail)) {
672
PCNTL_G(tail)->next = psig;
674
PCNTL_G(head) = psig;
676
PCNTL_G(tail) = psig;
679
void pcntl_tick_handler()
681
zval *param, **handle, *retval;
682
struct php_pcntl_pending_signal *queue, *next;
685
/* Bail if the queue is empty or if we are already playing the queue*/
686
if (! PCNTL_G(head) || PCNTL_G(processing_signal_queue))
689
/* Prevent reentrant handler calls */
690
PCNTL_G(processing_signal_queue) = 1;
692
queue = PCNTL_G(head);
693
PCNTL_G(head) = NULL; /* simple stores are atomic */
696
MAKE_STD_ZVAL(param);
697
MAKE_STD_ZVAL(retval);
700
if (zend_hash_index_find(&PCNTL_G(php_signal_table), queue->signo, (void **) &handle)==SUCCESS) {
701
ZVAL_LONG(param, queue->signo);
703
/* Call php signal handler - Note that we do not report errors, and we ignore the return value */
704
/* FIXME: this is probably broken when multiple signals are handled in this while loop (retval) */
705
call_user_function(EG(function_table), NULL, *handle, retval, 1, ¶m TSRMLS_CC);
709
queue->next = PCNTL_G(spares);
710
PCNTL_G(spares) = queue;
714
/* Re-enable queue */
715
PCNTL_G(processing_signal_queue) = 0;
728
* indent-tabs-mode: t