📄 pcntl.c
字号:
RETURN_FALSE;}/* }}} *//* {{{ proto int pcntl_wexitstatus(long 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(long 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(long 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(E_WARNING, "Error has occured in %s: (errno %d) %s", get_active_function_name(TSRMLS_C), 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(long signo, mixed 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(E_WARNING, "Invalid value for handle argument specifEied in %s", get_active_function_name(TSRMLS_C)); } if (php_signal(signo, (Sigfunc *) Z_LVAL_P(handle), (int) restart_syscalls) == SIG_ERR) { php_error(E_WARNING, "Error assigning signal in %s", get_active_function_name(TSRMLS_C)); RETURN_FALSE; } RETURN_TRUE; } if (!zend_is_callable(handle, 0, &func_name)) { php_error(E_WARNING, "%s: %s is not a callable function name error", get_active_function_name(TSRMLS_C), 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(E_WARNING, "Error assigning signal in %s", get_active_function_name(TSRMLS_C)); RETURN_FALSE; } RETURN_TRUE;}/* }}} *//* 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 */ MAKE_STD_ZVAL(param); MAKE_STD_ZVAL(retval); while (queue) { if (zend_hash_index_find(&PCNTL_G(php_signal_table), queue->signo, (void **) &handle)==SUCCESS) { 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); } next = queue->next; queue->next = PCNTL_G(spares); PCNTL_G(spares) = queue; queue = next; } /* Re-enable queue */ PCNTL_G(processing_signal_queue) = 0; /* Clean up */ efree(param); efree(retval);}/* * Local variables: * tab-width: 4 * c-basic-offset: 4 * indent-tabs-mode: t * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -