php-src/ext/standard/proc_open.c

979 lines
25 KiB
C
Raw Normal View History

/*
+----------------------------------------------------------------------+
2004-01-08 16:18:22 +08:00
| PHP Version 5 |
+----------------------------------------------------------------------+
2014-01-03 11:08:10 +08:00
| Copyright (c) 1997-2014 The PHP Group |
+----------------------------------------------------------------------+
2006-01-01 20:51:34 +08:00
| 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: |
2006-01-01 20:51:34 +08:00
| 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: Wez Furlong <wez@thebrainroom.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
2004-05-29 17:27:18 +08:00
#if 0 && (defined(__linux__) || defined(sun) || defined(__IRIX__))
# define _BSD_SOURCE /* linux wants this when XOPEN mode is on */
# define _BSD_COMPAT /* irix: uint */
# define _XOPEN_SOURCE 500 /* turn on Unix98 */
# define __EXTENSIONS__ 1 /* Solaris: uint */
#endif
#include "php.h"
#include <stdio.h>
#include <ctype.h>
#include "php_string.h"
#include "ext/standard/head.h"
#include "ext/standard/basic_functions.h"
#include "ext/standard/file.h"
#include "exec.h"
#include "php_globals.h"
#include "SAPI.h"
#include "main/php_network.h"
2004-09-29 14:04:36 +08:00
#ifdef NETWARE
#include <proc.h>
#include <library.h>
#endif
#if HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
/* This symbol is defined in ext/standard/config.m4.
* Essentially, it is set if you HAVE_FORK || PHP_WIN32
2012-05-29 21:44:04 +08:00
* Other platforms may modify that configure check and add suitable #ifdefs
* around the alternate code.
* */
#ifdef PHP_CAN_SUPPORT_PROC_OPEN
2004-05-29 17:27:18 +08:00
#if 0 && HAVE_PTSNAME && HAVE_GRANTPT && HAVE_UNLOCKPT && HAVE_SYS_IOCTL_H && HAVE_TERMIOS_H
# include <sys/ioctl.h>
# include <termios.h>
2004-05-29 08:30:11 +08:00
# define PHP_CAN_DO_PTS 1
#endif
#include "proc_open.h"
static int le_proc_open;
/* {{{ _php_array_to_envp */
static php_process_env_t _php_array_to_envp(zval *environment, int is_persistent TSRMLS_DC)
{
zval *element;
php_process_env_t env;
zend_string *string_key;
#ifndef PHP_WIN32
char **ep;
#endif
char *p;
2014-05-09 19:01:38 +08:00
uint cnt, l, sizeenv=0;
HashTable *target_hash;
memset(&env, 0, sizeof(env));
2010-08-31 00:52:48 +08:00
if (!environment) {
return env;
}
2010-08-31 00:52:48 +08:00
cnt = zend_hash_num_elements(Z_ARRVAL_P(environment));
2010-08-31 00:52:48 +08:00
if (cnt < 1) {
#ifndef PHP_WIN32
env.envarray = (char **) pecalloc(1, sizeof(char *), is_persistent);
#endif
env.envp = (char *) pecalloc(4, 1, is_persistent);
return env;
}
target_hash = HASH_OF(environment);
if (!target_hash) {
return env;
}
/* first, we have to get the size of all the elements in the hash */
2014-05-09 19:01:38 +08:00
ZEND_HASH_FOREACH_STR_KEY_VAL(target_hash, string_key, element) {
zend_string *str = zval_get_string(element);
uint el_len = str->len;
STR_RELEASE(str);
if (el_len == 0) {
continue;
}
2010-08-31 00:52:48 +08:00
2014-05-09 19:01:38 +08:00
sizeenv += el_len + 1;
2010-09-08 17:11:18 +08:00
if (string_key) {
if (string_key->len == 0) {
continue;
}
Merge mainstream 'master' branch into refactoring During merge I had to revert: Nikita's patch for php_splice() (it probably needs to be applyed again) Bob Weinand's patches related to constant expression handling (we need to review them carefully) I also reverted all our attempts to support sapi/phpdbg (we didn't test it anyway) Conflicts: Zend/zend.h Zend/zend_API.c Zend/zend_ast.c Zend/zend_compile.c Zend/zend_compile.h Zend/zend_constants.c Zend/zend_exceptions.c Zend/zend_execute.c Zend/zend_execute.h Zend/zend_execute_API.c Zend/zend_hash.c Zend/zend_highlight.c Zend/zend_language_parser.y Zend/zend_language_scanner.c Zend/zend_language_scanner_defs.h Zend/zend_variables.c Zend/zend_vm_def.h Zend/zend_vm_execute.h ext/date/php_date.c ext/dom/documenttype.c ext/hash/hash.c ext/iconv/iconv.c ext/mbstring/tests/zend_multibyte-10.phpt ext/mbstring/tests/zend_multibyte-11.phpt ext/mbstring/tests/zend_multibyte-12.phpt ext/mysql/php_mysql.c ext/mysqli/mysqli.c ext/mysqlnd/mysqlnd_reverse_api.c ext/mysqlnd/php_mysqlnd.c ext/opcache/ZendAccelerator.c ext/opcache/zend_accelerator_util_funcs.c ext/opcache/zend_persist.c ext/opcache/zend_persist_calc.c ext/pcre/php_pcre.c ext/pdo/pdo_dbh.c ext/pdo/pdo_stmt.c ext/pdo_pgsql/pgsql_driver.c ext/pgsql/pgsql.c ext/reflection/php_reflection.c ext/session/session.c ext/spl/spl_array.c ext/spl/spl_observer.c ext/standard/array.c ext/standard/basic_functions.c ext/standard/html.c ext/standard/mail.c ext/standard/php_array.h ext/standard/proc_open.c ext/standard/streamsfuncs.c ext/standard/user_filters.c ext/standard/var_unserializer.c ext/standard/var_unserializer.re main/php_variables.c sapi/phpdbg/phpdbg.c sapi/phpdbg/phpdbg_bp.c sapi/phpdbg/phpdbg_frame.c sapi/phpdbg/phpdbg_help.c sapi/phpdbg/phpdbg_list.c sapi/phpdbg/phpdbg_print.c sapi/phpdbg/phpdbg_prompt.c
2014-04-26 04:32:51 +08:00
sizeenv += string_key->len + 1;
}
} ZEND_HASH_FOREACH_END();
#ifndef PHP_WIN32
ep = env.envarray = (char **) pecalloc(cnt + 1, sizeof(char *), is_persistent);
#endif
p = env.envp = (char *) pecalloc(sizeenv + 4, 1, is_persistent);
2014-05-09 19:01:38 +08:00
ZEND_HASH_FOREACH_STR_KEY_VAL(target_hash, string_key, element) {
zend_string *str = zval_get_string(element);
2010-08-31 00:52:48 +08:00
2014-05-09 19:01:38 +08:00
if (str->len == 0) {
goto next_element;
}
2010-08-31 00:52:48 +08:00
if (string_key) {
if (string_key->len == 0) {
Merge mainstream 'master' branch into refactoring During merge I had to revert: Nikita's patch for php_splice() (it probably needs to be applyed again) Bob Weinand's patches related to constant expression handling (we need to review them carefully) I also reverted all our attempts to support sapi/phpdbg (we didn't test it anyway) Conflicts: Zend/zend.h Zend/zend_API.c Zend/zend_ast.c Zend/zend_compile.c Zend/zend_compile.h Zend/zend_constants.c Zend/zend_exceptions.c Zend/zend_execute.c Zend/zend_execute.h Zend/zend_execute_API.c Zend/zend_hash.c Zend/zend_highlight.c Zend/zend_language_parser.y Zend/zend_language_scanner.c Zend/zend_language_scanner_defs.h Zend/zend_variables.c Zend/zend_vm_def.h Zend/zend_vm_execute.h ext/date/php_date.c ext/dom/documenttype.c ext/hash/hash.c ext/iconv/iconv.c ext/mbstring/tests/zend_multibyte-10.phpt ext/mbstring/tests/zend_multibyte-11.phpt ext/mbstring/tests/zend_multibyte-12.phpt ext/mysql/php_mysql.c ext/mysqli/mysqli.c ext/mysqlnd/mysqlnd_reverse_api.c ext/mysqlnd/php_mysqlnd.c ext/opcache/ZendAccelerator.c ext/opcache/zend_accelerator_util_funcs.c ext/opcache/zend_persist.c ext/opcache/zend_persist_calc.c ext/pcre/php_pcre.c ext/pdo/pdo_dbh.c ext/pdo/pdo_stmt.c ext/pdo_pgsql/pgsql_driver.c ext/pgsql/pgsql.c ext/reflection/php_reflection.c ext/session/session.c ext/spl/spl_array.c ext/spl/spl_observer.c ext/standard/array.c ext/standard/basic_functions.c ext/standard/html.c ext/standard/mail.c ext/standard/php_array.h ext/standard/proc_open.c ext/standard/streamsfuncs.c ext/standard/user_filters.c ext/standard/var_unserializer.c ext/standard/var_unserializer.re main/php_variables.c sapi/phpdbg/phpdbg.c sapi/phpdbg/phpdbg_bp.c sapi/phpdbg/phpdbg_frame.c sapi/phpdbg/phpdbg_help.c sapi/phpdbg/phpdbg_list.c sapi/phpdbg/phpdbg_print.c sapi/phpdbg/phpdbg_prompt.c
2014-04-26 04:32:51 +08:00
goto next_element;
}
2014-05-09 19:01:38 +08:00
l = string_key->len + str->len + 2;
memcpy(p, string_key->val, string_key->len);
strncat(p, "=", 1);
2014-05-09 19:01:38 +08:00
strncat(p, str->val, str->len);
2010-08-31 00:52:48 +08:00
#ifndef PHP_WIN32
*ep = p;
++ep;
#endif
p += l;
} else {
2014-05-09 19:01:38 +08:00
memcpy(p, str->val, str->len);
#ifndef PHP_WIN32
*ep = p;
++ep;
#endif
2014-05-09 19:01:38 +08:00
p += str->len + 1;
}
next_element:
2014-05-09 19:01:38 +08:00
STR_RELEASE(str);
} ZEND_HASH_FOREACH_END();
2010-09-06 18:28:04 +08:00
assert((uint)(p - env.envp) <= sizeenv);
2010-08-31 00:52:48 +08:00
return env;
}
/* }}} */
/* {{{ _php_free_envp */
static void _php_free_envp(php_process_env_t env, int is_persistent)
{
#ifndef PHP_WIN32
if (env.envarray) {
pefree(env.envarray, is_persistent);
}
#endif
if (env.envp) {
pefree(env.envp, is_persistent);
}
}
/* }}} */
2003-07-12 15:01:30 +08:00
/* {{{ proc_open_rsrc_dtor */
static void proc_open_rsrc_dtor(zend_resource *rsrc TSRMLS_DC)
{
struct php_process_handle *proc = (struct php_process_handle*)rsrc->ptr;
int i;
#ifdef PHP_WIN32
DWORD wstatus;
#elif HAVE_SYS_WAIT_H
int wstatus;
int waitpid_options = 0;
pid_t wait_pid;
#endif
/* Close all handles to avoid a deadlock */
for (i = 0; i < proc->npipes; i++) {
if (proc->pipes[i] != 0) {
2014-05-09 06:24:01 +08:00
GC_REFCOUNT(proc->pipes[i])--;
zend_list_close(proc->pipes[i]);
proc->pipes[i] = 0;
}
}
2010-08-31 00:52:48 +08:00
#ifdef PHP_WIN32
if (FG(pclose_wait)) {
WaitForSingleObject(proc->childHandle, INFINITE);
}
GetExitCodeProcess(proc->childHandle, &wstatus);
if (wstatus == STILL_ACTIVE) {
FG(pclose_ret) = -1;
} else {
FG(pclose_ret) = wstatus;
}
CloseHandle(proc->childHandle);
2010-08-31 00:52:48 +08:00
#elif HAVE_SYS_WAIT_H
2010-08-31 00:52:48 +08:00
if (!FG(pclose_wait)) {
waitpid_options = WNOHANG;
}
do {
wait_pid = waitpid(proc->child, &wstatus, waitpid_options);
} while (wait_pid == -1 && errno == EINTR);
2010-08-31 00:52:48 +08:00
if (wait_pid <= 0) {
FG(pclose_ret) = -1;
2009-07-20 12:31:07 +08:00
} else {
if (WIFEXITED(wstatus))
wstatus = WEXITSTATUS(wstatus);
FG(pclose_ret) = wstatus;
}
2010-08-31 00:52:48 +08:00
#else
FG(pclose_ret) = -1;
#endif
_php_free_envp(proc->env, proc->is_persistent);
pefree(proc->command, proc->is_persistent);
pefree(proc, proc->is_persistent);
2010-08-31 00:52:48 +08:00
}
2003-07-12 15:01:30 +08:00
/* }}} */
2003-07-12 15:01:30 +08:00
/* {{{ PHP_MINIT_FUNCTION(proc_open) */
PHP_MINIT_FUNCTION(proc_open)
{
le_proc_open = zend_register_list_destructors_ex(proc_open_rsrc_dtor, NULL, "process", module_number);
return SUCCESS;
}
2003-07-12 15:01:30 +08:00
/* }}} */
/* {{{ proto bool proc_terminate(resource process [, long signal])
kill a process opened by proc_open */
PHP_FUNCTION(proc_terminate)
{
zval *zproc;
struct php_process_handle *proc;
long sig_no = SIGTERM;
2010-08-31 00:52:48 +08:00
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zproc, &sig_no) == FAILURE) {
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(proc, struct php_process_handle *, zproc, -1, "process", le_proc_open);
2010-08-31 00:52:48 +08:00
#ifdef PHP_WIN32
if (TerminateProcess(proc->childHandle, 255)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
#else
if (kill(proc->child, sig_no) == 0) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
#endif
}
/* }}} */
/* {{{ proto int proc_close(resource process)
close a process opened by proc_open */
PHP_FUNCTION(proc_close)
{
zval *zproc;
struct php_process_handle *proc;
2010-08-31 00:52:48 +08:00
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zproc) == FAILURE) {
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(proc, struct php_process_handle *, zproc, -1, "process", le_proc_open);
2010-08-31 00:52:48 +08:00
FG(pclose_wait) = 1;
2014-05-09 06:24:01 +08:00
zend_list_close(Z_RES_P(zproc));
FG(pclose_wait) = 0;
RETURN_LONG(FG(pclose_ret));
}
/* }}} */
/* {{{ proto array proc_get_status(resource process)
get information about a process opened by proc_open */
PHP_FUNCTION(proc_get_status)
{
zval *zproc;
struct php_process_handle *proc;
#ifdef PHP_WIN32
DWORD wstatus;
#elif HAVE_SYS_WAIT_H
int wstatus;
pid_t wait_pid;
#endif
int running = 1, signaled = 0, stopped = 0;
int exitcode = -1, termsig = 0, stopsig = 0;
2010-08-31 00:52:48 +08:00
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zproc) == FAILURE) {
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(proc, struct php_process_handle *, zproc, -1, "process", le_proc_open);
array_init(return_value);
2014-04-15 19:40:40 +08:00
add_assoc_string(return_value, "command", proc->command);
2003-01-25 00:45:34 +08:00
add_assoc_long(return_value, "pid", (long) proc->child);
2010-08-31 00:52:48 +08:00
#ifdef PHP_WIN32
2010-08-31 00:52:48 +08:00
GetExitCodeProcess(proc->childHandle, &wstatus);
running = wstatus == STILL_ACTIVE;
exitcode = running ? -1 : wstatus;
2010-08-31 00:52:48 +08:00
#elif HAVE_SYS_WAIT_H
2010-08-31 00:52:48 +08:00
errno = 0;
wait_pid = waitpid(proc->child, &wstatus, WNOHANG|WUNTRACED);
2010-08-31 00:52:48 +08:00
if (wait_pid == proc->child) {
if (WIFEXITED(wstatus)) {
running = 0;
exitcode = WEXITSTATUS(wstatus);
}
if (WIFSIGNALED(wstatus)) {
running = 0;
signaled = 1;
#ifdef NETWARE
termsig = WIFTERMSIG(wstatus);
#else
termsig = WTERMSIG(wstatus);
#endif
}
if (WIFSTOPPED(wstatus)) {
stopped = 1;
stopsig = WSTOPSIG(wstatus);
}
} else if (wait_pid == -1) {
running = 0;
}
#endif
add_assoc_bool(return_value, "running", running);
add_assoc_bool(return_value, "signaled", signaled);
add_assoc_bool(return_value, "stopped", stopped);
add_assoc_long(return_value, "exitcode", exitcode);
add_assoc_long(return_value, "termsig", termsig);
add_assoc_long(return_value, "stopsig", stopsig);
}
/* }}} */
/* {{{ handy definitions for portability/readability */
#ifdef PHP_WIN32
# define pipe(pair) (CreatePipe(&pair[0], &pair[1], &security, 0) ? 0 : -1)
# define COMSPEC_NT "cmd.exe"
static inline HANDLE dup_handle(HANDLE src, BOOL inherit, BOOL closeorig)
{
HANDLE copy, self = GetCurrentProcess();
2010-08-31 00:52:48 +08:00
if (!DuplicateHandle(self, src, self, &copy, 0, inherit, DUPLICATE_SAME_ACCESS |
(closeorig ? DUPLICATE_CLOSE_SOURCE : 0)))
return NULL;
return copy;
}
static inline HANDLE dup_fd_as_handle(int fd)
{
return dup_handle((HANDLE)_get_osfhandle(fd), TRUE, FALSE);
}
# define close_descriptor(fd) CloseHandle(fd)
#else
# define close_descriptor(fd) close(fd)
#endif
#define DESC_PIPE 1
#define DESC_FILE 2
#define DESC_PARENT_MODE_WRITE 8
struct php_proc_open_descriptor_item {
int index; /* desired fd number in child process */
php_file_descriptor_t parentend, childend; /* fds for pipes in parent/child */
int mode; /* mode for proc_open code */
int mode_flags; /* mode flags for opening fds */
};
/* }}} */
2003-12-06 18:48:56 +08:00
/* {{{ proto resource proc_open(string command, array descriptorspec, array &pipes [, string cwd [, array env [, array other_options]]])
Run a process with more control over it's file descriptors */
PHP_FUNCTION(proc_open)
{
char *command, *cwd=NULL;
2008-10-22 06:08:38 +08:00
int command_len, cwd_len = 0;
zval *descriptorspec;
zval *pipes;
zval *environment = NULL;
zval *other_options = NULL;
php_process_env_t env;
int ndesc = 0;
int i;
zval *descitem = NULL;
zend_string *str_index;
ulong nindex;
struct php_proc_open_descriptor_item descriptors[PHP_PROC_OPEN_MAX_DESCRIPTORS];
#ifdef PHP_WIN32
PROCESS_INFORMATION pi;
HANDLE childHandle;
STARTUPINFO si;
BOOL newprocok;
SECURITY_ATTRIBUTES security;
DWORD dwCreateFlags = 0;
char *command_with_cmd;
UINT old_error_mode;
2013-01-07 20:03:12 +08:00
char cur_cwd[MAXPATHLEN];
2004-09-29 14:04:36 +08:00
#endif
#ifdef NETWARE
2009-07-20 19:48:04 +08:00
char** child_argv = NULL;
2004-09-29 14:04:36 +08:00
char* command_dup = NULL;
char* orig_cwd = NULL;
int command_num_args = 0;
wiring_t channel;
#endif
php_process_id_t child;
struct php_process_handle *proc;
int is_persistent = 0; /* TODO: ensure that persistent procs will work */
#ifdef PHP_WIN32
2003-12-05 21:48:40 +08:00
int suppress_errors = 0;
int bypass_shell = 0;
2009-07-20 19:48:04 +08:00
#endif
#if PHP_CAN_DO_PTS
php_file_descriptor_t dev_ptmx = -1; /* master */
php_file_descriptor_t slave_pty = -1;
#endif
2009-07-20 19:48:04 +08:00
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "saz|s!a!a!", &command,
&command_len, &descriptorspec, &pipes, &cwd, &cwd_len, &environment,
&other_options) == FAILURE) {
RETURN_FALSE;
}
command = pestrdup(command, is_persistent);
2009-07-20 19:48:04 +08:00
#ifdef PHP_WIN32
if (other_options) {
zval **item;
if (SUCCESS == zend_hash_find(Z_ARRVAL_P(other_options), "suppress_errors", sizeof("suppress_errors"), (void**)&item)) {
if ((Z_TYPE_PP(item) == IS_BOOL || Z_TYPE_PP(item) == IS_LONG) &&
Z_LVAL_PP(item)) {
suppress_errors = 1;
}
2010-08-31 00:52:48 +08:00
}
if (SUCCESS == zend_hash_find(Z_ARRVAL_P(other_options), "bypass_shell", sizeof("bypass_shell"), (void**)&item)) {
if ((Z_TYPE_PP(item) == IS_BOOL || Z_TYPE_PP(item) == IS_LONG) &&
Z_LVAL_PP(item)) {
bypass_shell = 1;
}
2010-08-31 00:52:48 +08:00
}
}
#endif
2010-08-31 00:52:48 +08:00
command_len = strlen(command);
if (environment) {
env = _php_array_to_envp(environment, is_persistent TSRMLS_CC);
} else {
memset(&env, 0, sizeof(env));
}
memset(descriptors, 0, sizeof(descriptors));
#ifdef PHP_WIN32
/* we use this to allow the child to inherit handles */
memset(&security, 0, sizeof(security));
security.nLength = sizeof(security);
security.bInheritHandle = TRUE;
security.lpSecurityDescriptor = NULL;
#endif
2010-08-31 00:52:48 +08:00
/* walk the descriptor spec and set up files/pipes */
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(descriptorspec), nindex, str_index, descitem) {
zval *ztype;
if (str_index) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "descriptor spec must be an integer indexed array");
goto exit_fail;
}
descriptors[ndesc].index = nindex;
if (Z_TYPE_P(descitem) == IS_RESOURCE) {
/* should be a stream - try and dup the descriptor */
php_stream *stream;
php_socket_t fd;
php_stream_from_zval(stream, descitem);
if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **)&fd, REPORT_ERRORS)) {
goto exit_fail;
}
#ifdef PHP_WIN32
descriptors[ndesc].childend = dup_fd_as_handle(fd);
if (descriptors[ndesc].childend == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to dup File-Handle for descriptor %d", nindex);
goto exit_fail;
}
#else
descriptors[ndesc].childend = dup(fd);
if (descriptors[ndesc].childend < 0) {
2003-08-29 00:49:57 +08:00
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to dup File-Handle for descriptor %ld - %s", nindex, strerror(errno));
goto exit_fail;
}
#endif
descriptors[ndesc].mode = DESC_FILE;
} else if (Z_TYPE_P(descitem) != IS_ARRAY) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Descriptor item must be either an array or a File-Handle");
goto exit_fail;
} else {
if ((ztype = zend_hash_index_find(Z_ARRVAL_P(descitem), 0)) != NULL) {
convert_to_string_ex(ztype);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing handle qualifier in array");
goto exit_fail;
}
if (strcmp(Z_STRVAL_P(ztype), "pipe") == 0) {
php_file_descriptor_t newpipe[2];
zval *zmode;
if ((zmode = zend_hash_index_find(Z_ARRVAL_P(descitem), 1)) != NULL) {
convert_to_string_ex(zmode);
} else {
2003-08-29 00:49:57 +08:00
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing mode parameter for 'pipe'");
goto exit_fail;
}
descriptors[ndesc].mode = DESC_PIPE;
if (0 != pipe(newpipe)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to create pipe %s", strerror(errno));
goto exit_fail;
}
if (strncmp(Z_STRVAL_P(zmode), "w", 1) != 0) {
descriptors[ndesc].parentend = newpipe[1];
descriptors[ndesc].childend = newpipe[0];
descriptors[ndesc].mode |= DESC_PARENT_MODE_WRITE;
} else {
descriptors[ndesc].parentend = newpipe[0];
descriptors[ndesc].childend = newpipe[1];
}
#ifdef PHP_WIN32
/* don't let the child inherit the parent side of the pipe */
descriptors[ndesc].parentend = dup_handle(descriptors[ndesc].parentend, FALSE, TRUE);
#endif
descriptors[ndesc].mode_flags = descriptors[ndesc].mode & DESC_PARENT_MODE_WRITE ? O_WRONLY : O_RDONLY;
#ifdef PHP_WIN32
if (Z_STRLEN_P(zmode) >= 2 && Z_STRVAL_P(zmode)[1] == 'b')
descriptors[ndesc].mode_flags |= O_BINARY;
#endif
} else if (strcmp(Z_STRVAL_P(ztype), "file") == 0) {
zval *zfile, *zmode;
php_socket_t fd;
php_stream *stream;
descriptors[ndesc].mode = DESC_FILE;
if ((zfile = zend_hash_index_find(Z_ARRVAL_P(descitem), 1)) != NULL) {
convert_to_string_ex(zfile);
} else {
2003-08-29 00:49:57 +08:00
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing file name parameter for 'file'");
goto exit_fail;
}
if ((zmode = zend_hash_index_find(Z_ARRVAL_P(descitem), 2)) != NULL) {
convert_to_string_ex(zmode);
} else {
2003-08-29 00:49:57 +08:00
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing mode parameter for 'file'");
goto exit_fail;
}
/* try a wrapper */
stream = php_stream_open_wrapper(Z_STRVAL_P(zfile), Z_STRVAL_P(zmode),
REPORT_ERRORS|STREAM_WILL_CAST, NULL);
/* force into an fd */
if (stream == NULL || FAILURE == php_stream_cast(stream,
PHP_STREAM_CAST_RELEASE|PHP_STREAM_AS_FD,
(void **)&fd, REPORT_ERRORS)) {
goto exit_fail;
}
#ifdef PHP_WIN32
descriptors[ndesc].childend = dup_fd_as_handle(fd);
_close(fd);
/* simulate the append mode by fseeking to the end of the file
this introduces a potential race-condition, but it is the best we can do, though */
if (strchr(Z_STRVAL_PP(zmode), 'a')) {
SetFilePointer(descriptors[ndesc].childend, 0, NULL, FILE_END);
}
#else
descriptors[ndesc].childend = fd;
#endif
} else if (strcmp(Z_STRVAL_P(ztype), "pty") == 0) {
#if PHP_CAN_DO_PTS
if (dev_ptmx == -1) {
/* open things up */
dev_ptmx = open("/dev/ptmx", O_RDWR);
if (dev_ptmx == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to open /dev/ptmx, errno %d", errno);
goto exit_fail;
}
grantpt(dev_ptmx);
unlockpt(dev_ptmx);
slave_pty = open(ptsname(dev_ptmx), O_RDWR);
if (slave_pty == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to open slave pty, errno %d", errno);
goto exit_fail;
}
}
descriptors[ndesc].mode = DESC_PIPE;
descriptors[ndesc].childend = dup(slave_pty);
descriptors[ndesc].parentend = dup(dev_ptmx);
descriptors[ndesc].mode_flags = O_RDWR;
#else
2005-06-20 19:10:11 +08:00
php_error_docref(NULL TSRMLS_CC, E_WARNING, "pty pseudo terminal not supported on this system");
goto exit_fail;
#endif
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s is not a valid descriptor spec/mode", Z_STRVAL_P(ztype));
goto exit_fail;
}
}
if (++ndesc == PHP_PROC_OPEN_MAX_DESCRIPTORS)
break;
} ZEND_HASH_FOREACH_END();
#ifdef PHP_WIN32
if (cwd == NULL) {
char *getcwd_result;
getcwd_result = VCWD_GETCWD(cur_cwd, MAXPATHLEN);
if (!getcwd_result) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot get current directory");
goto exit_fail;
}
2013-01-07 20:03:12 +08:00
cwd = cur_cwd;
}
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
2010-08-31 00:52:48 +08:00
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
2010-08-31 00:52:48 +08:00
/* redirect stdin/stdout/stderr if requested */
for (i = 0; i < ndesc; i++) {
switch(descriptors[i].index) {
case 0:
si.hStdInput = descriptors[i].childend;
break;
case 1:
si.hStdOutput = descriptors[i].childend;
break;
case 2:
si.hStdError = descriptors[i].childend;
break;
}
}
2010-08-31 00:52:48 +08:00
memset(&pi, 0, sizeof(pi));
2010-08-31 00:52:48 +08:00
if (suppress_errors) {
old_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOGPFAULTERRORBOX);
}
2010-08-31 00:52:48 +08:00
dwCreateFlags = NORMAL_PRIORITY_CLASS;
if(strcmp(sapi_module.name, "cli") != 0) {
dwCreateFlags |= CREATE_NO_WINDOW;
}
if (bypass_shell) {
newprocok = CreateProcess(NULL, command, &security, &security, TRUE, dwCreateFlags, env.envp, cwd, &si, &pi);
} else {
spprintf(&command_with_cmd, 0, "%s /c %s", COMSPEC_NT, command);
newprocok = CreateProcess(NULL, command_with_cmd, &security, &security, TRUE, dwCreateFlags, env.envp, cwd, &si, &pi);
2010-08-31 00:52:48 +08:00
efree(command_with_cmd);
}
if (suppress_errors) {
SetErrorMode(old_error_mode);
}
2010-08-31 00:52:48 +08:00
if (FALSE == newprocok) {
2008-08-21 19:27:57 +08:00
DWORD dw = GetLastError();
/* clean up all the descriptors */
for (i = 0; i < ndesc; i++) {
CloseHandle(descriptors[i].childend);
if (descriptors[i].parentend) {
CloseHandle(descriptors[i].parentend);
}
}
2008-08-21 19:27:57 +08:00
php_error_docref(NULL TSRMLS_CC, E_WARNING, "CreateProcess failed, error code - %u", dw);
goto exit_fail;
}
childHandle = pi.hProcess;
child = pi.dwProcessId;
CloseHandle(pi.hThread);
#elif defined(NETWARE)
if (cwd) {
orig_cwd = getcwd(NULL, PATH_MAX);
chdir2(cwd);
}
channel.infd = descriptors[0].childend;
channel.outfd = descriptors[1].childend;
channel.errfd = -1;
/* Duplicate the command as processing downwards will modify it*/
command_dup = strdup(command);
2007-02-24 11:24:40 +08:00
if (!command_dup) {
goto exit_fail;
}
/* get a number of args */
construct_argc_argv(command_dup, NULL, &command_num_args, NULL);
child_argv = (char**) malloc((command_num_args + 1) * sizeof(char*));
if(!child_argv) {
free(command_dup);
if (cwd && orig_cwd) {
chdir2(orig_cwd);
free(orig_cwd);
}
}
/* fill the child arg vector */
construct_argc_argv(command_dup, NULL, &command_num_args, child_argv);
child_argv[command_num_args] = NULL;
child = procve(child_argv[0], PROC_DETACHED|PROC_INHERIT_CWD, NULL, &channel, NULL, NULL, 0, NULL, (const char**)child_argv);
free(child_argv);
free(command_dup);
if (cwd && orig_cwd) {
chdir2(orig_cwd);
free(orig_cwd);
}
if (child < 0) {
/* failed to fork() */
/* clean up all the descriptors */
for (i = 0; i < ndesc; i++) {
close(descriptors[i].childend);
if (descriptors[i].parentend)
close(descriptors[i].parentend);
}
php_error_docref(NULL TSRMLS_CC, E_WARNING, "procve failed - %s", strerror(errno));
goto exit_fail;
}
#elif HAVE_FORK
/* the unix way */
child = fork();
if (child == 0) {
/* this is the child process */
#if PHP_CAN_DO_PTS
if (dev_ptmx >= 0) {
int my_pid = getpid();
2004-05-29 08:30:11 +08:00
#ifdef TIOCNOTTY
/* detach from original tty. Might only need this if isatty(0) is true */
ioctl(0,TIOCNOTTY,NULL);
2004-05-29 08:30:11 +08:00
#else
setsid();
#endif
/* become process group leader */
setpgid(my_pid, my_pid);
tcsetpgrp(0, my_pid);
}
#endif
2010-08-31 00:52:48 +08:00
/* close those descriptors that we just opened for the parent stuff,
* dup new descriptors into required descriptors and close the original
* cruft */
for (i = 0; i < ndesc; i++) {
switch (descriptors[i].mode & ~DESC_PARENT_MODE_WRITE) {
case DESC_PIPE:
close(descriptors[i].parentend);
break;
}
if (dup2(descriptors[i].childend, descriptors[i].index) < 0)
perror("dup2");
if (descriptors[i].childend != descriptors[i].index)
close(descriptors[i].childend);
}
#if PHP_CAN_DO_PTS
if (dev_ptmx >= 0) {
close(dev_ptmx);
close(slave_pty);
}
#endif
2010-08-31 00:52:48 +08:00
if (cwd) {
php_ignore_value(chdir(cwd));
}
2010-08-31 00:52:48 +08:00
2009-07-20 19:48:04 +08:00
if (env.envarray) {
execle("/bin/sh", "sh", "-c", command, NULL, env.envarray);
} else {
execl("/bin/sh", "sh", "-c", command, NULL);
}
_exit(127);
} else if (child < 0) {
/* failed to fork() */
/* clean up all the descriptors */
for (i = 0; i < ndesc; i++) {
close(descriptors[i].childend);
if (descriptors[i].parentend)
close(descriptors[i].parentend);
}
php_error_docref(NULL TSRMLS_CC, E_WARNING, "fork failed - %s", strerror(errno));
goto exit_fail;
}
#else
# error You lose (configure should not have let you get here)
#endif
/* we forked/spawned and this is the parent */
proc = (struct php_process_handle*)pemalloc(sizeof(struct php_process_handle), is_persistent);
proc->is_persistent = is_persistent;
proc->command = command;
proc->npipes = ndesc;
proc->child = child;
#ifdef PHP_WIN32
proc->childHandle = childHandle;
#endif
proc->env = env;
if (pipes != NULL) {
2014-04-15 19:40:40 +08:00
ZVAL_DEREF(pipes);
zval_dtor(pipes);
2014-03-03 16:50:35 +08:00
}
array_init(pipes);
#if PHP_CAN_DO_PTS
if (dev_ptmx >= 0) {
close(dev_ptmx);
close(slave_pty);
}
#endif
/* clean up all the child ends and then open streams on the parent
* ends, where appropriate */
for (i = 0; i < ndesc; i++) {
char *mode_string=NULL;
php_stream *stream = NULL;
close_descriptor(descriptors[i].childend);
switch (descriptors[i].mode & ~DESC_PARENT_MODE_WRITE) {
case DESC_PIPE:
switch(descriptors[i].mode_flags) {
#ifdef PHP_WIN32
case O_WRONLY|O_BINARY:
mode_string = "wb";
break;
case O_RDONLY|O_BINARY:
mode_string = "rb";
break;
#endif
case O_WRONLY:
mode_string = "w";
break;
case O_RDONLY:
mode_string = "r";
break;
case O_RDWR:
mode_string = "r+";
break;
}
#ifdef PHP_WIN32
2007-04-16 16:09:56 +08:00
stream = php_stream_fopen_from_fd(_open_osfhandle((zend_intptr_t)descriptors[i].parentend,
descriptors[i].mode_flags), mode_string, NULL);
#else
stream = php_stream_fopen_from_fd(descriptors[i].parentend, mode_string, NULL);
# if defined(F_SETFD) && defined(FD_CLOEXEC)
/* mark the descriptor close-on-exec, so that it won't be inherited by potential other children */
fcntl(descriptors[i].parentend, F_SETFD, FD_CLOEXEC);
# endif
#endif
if (stream) {
zval retfp;
/* nasty hack; don't copy it */
stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
2010-08-31 00:52:48 +08:00
php_stream_to_zval(stream, &retfp);
add_index_zval(pipes, descriptors[i].index, &retfp);
2014-04-09 19:05:22 +08:00
proc->pipes[i] = Z_RES(retfp);
Z_ADDREF(retfp);
}
break;
default:
proc->pipes[i] = NULL;
}
}
ZEND_REGISTER_RESOURCE(return_value, proc, le_proc_open);
return;
exit_fail:
_php_free_envp(env, is_persistent);
pefree(command, is_persistent);
#if PHP_CAN_DO_PTS
if (dev_ptmx >= 0) {
close(dev_ptmx);
}
if (slave_pty >= 0) {
close(slave_pty);
}
#endif
RETURN_FALSE;
}
/* }}} */
#endif /* PHP_CAN_SUPPORT_PROC_OPEN */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/