2003-01-15 16:29:00 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2014-09-19 16:33:14 +00:00
|
|
|
| PHP Version 7 |
|
2003-01-15 16:29:00 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2016-01-01 17:19:27 +00:00
|
|
|
| Copyright (c) 1997-2016 The PHP Group |
|
2003-01-15 16:29:00 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 12:51:34 +00:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2003-01-15 16:29:00 +00:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-10 20:04:29 +00:00
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 12:51:34 +00:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
2003-01-15 16:29:00 +00:00
|
|
|
| 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. |
|
|
|
|
+----------------------------------------------------------------------+
|
2003-01-15 18:54:03 +00:00
|
|
|
| Author: Wez Furlong <wez@thebrainroom.com> |
|
2003-01-15 16:29:00 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
/* $Id$ */
|
|
|
|
|
2004-05-29 09:27:18 +00:00
|
|
|
#if 0 && (defined(__linux__) || defined(sun) || defined(__IRIX__))
|
2004-05-28 13:25:51 +00:00
|
|
|
# define _BSD_SOURCE /* linux wants this when XOPEN mode is on */
|
2016-11-26 14:18:42 +00:00
|
|
|
# define _BSD_COMPAT /* irix: uint32_t */
|
2004-05-28 13:25:51 +00:00
|
|
|
# define _XOPEN_SOURCE 500 /* turn on Unix98 */
|
2016-11-26 14:18:42 +00:00
|
|
|
# define __EXTENSIONS__ 1 /* Solaris: uint32_t */
|
2004-05-28 13:25:51 +00:00
|
|
|
#endif
|
2004-04-19 15:07:30 +00:00
|
|
|
|
2003-01-15 16:29:00 +00:00
|
|
|
#include "php.h"
|
2004-04-19 15:07:30 +00:00
|
|
|
#include <stdio.h>
|
2003-01-15 16:29:00 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include "php_string.h"
|
|
|
|
#include "ext/standard/head.h"
|
2009-07-26 15:14:18 +00:00
|
|
|
#include "ext/standard/basic_functions.h"
|
2003-01-15 16:29:00 +00:00
|
|
|
#include "ext/standard/file.h"
|
|
|
|
#include "exec.h"
|
|
|
|
#include "php_globals.h"
|
|
|
|
#include "SAPI.h"
|
2014-03-19 05:26:54 +00:00
|
|
|
#include "main/php_network.h"
|
2003-01-15 16:29:00 +00:00
|
|
|
|
|
|
|
#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 13:44:04 +00:00
|
|
|
* Other platforms may modify that configure check and add suitable #ifdefs
|
2003-01-15 16:29:00 +00:00
|
|
|
* around the alternate code.
|
|
|
|
* */
|
|
|
|
#ifdef PHP_CAN_SUPPORT_PROC_OPEN
|
|
|
|
|
2004-05-29 09:27:18 +00:00
|
|
|
#if 0 && HAVE_PTSNAME && HAVE_GRANTPT && HAVE_UNLOCKPT && HAVE_SYS_IOCTL_H && HAVE_TERMIOS_H
|
2004-04-19 15:07:30 +00:00
|
|
|
# include <sys/ioctl.h>
|
|
|
|
# include <termios.h>
|
2004-05-29 00:30:11 +00:00
|
|
|
# define PHP_CAN_DO_PTS 1
|
2004-04-19 15:07:30 +00:00
|
|
|
#endif
|
|
|
|
|
2003-01-15 18:54:03 +00:00
|
|
|
#include "proc_open.h"
|
|
|
|
|
2003-01-15 16:29:00 +00:00
|
|
|
static int le_proc_open;
|
|
|
|
|
2003-02-16 15:38:54 +00:00
|
|
|
/* {{{ _php_array_to_envp */
|
2014-12-13 22:06:14 +00:00
|
|
|
static php_process_env_t _php_array_to_envp(zval *environment, int is_persistent)
|
2003-02-16 15:38:54 +00:00
|
|
|
{
|
2014-02-14 09:40:11 +00:00
|
|
|
zval *element;
|
2003-02-16 15:38:54 +00:00
|
|
|
php_process_env_t env;
|
2016-06-13 13:08:04 +00:00
|
|
|
zend_string *key, *str;
|
2003-02-16 15:38:54 +00:00
|
|
|
#ifndef PHP_WIN32
|
|
|
|
char **ep;
|
|
|
|
#endif
|
|
|
|
char *p;
|
2016-06-13 13:08:04 +00:00
|
|
|
size_t cnt, l, sizeenv = 0;
|
|
|
|
HashTable *env_hash;
|
2003-02-16 15:38:54 +00:00
|
|
|
|
|
|
|
memset(&env, 0, sizeof(env));
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-02-16 15:38:54 +00:00
|
|
|
if (!environment) {
|
|
|
|
return env;
|
|
|
|
}
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-02-16 15:38:54 +00:00
|
|
|
cnt = zend_hash_num_elements(Z_ARRVAL_P(environment));
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-02-16 15:38:54 +00:00
|
|
|
if (cnt < 1) {
|
2007-08-17 10:42:30 +00:00
|
|
|
#ifndef PHP_WIN32
|
|
|
|
env.envarray = (char **) pecalloc(1, sizeof(char *), is_persistent);
|
|
|
|
#endif
|
|
|
|
env.envp = (char *) pecalloc(4, 1, is_persistent);
|
2003-02-16 15:38:54 +00:00
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
2016-06-13 13:08:04 +00:00
|
|
|
ALLOC_HASHTABLE(env_hash);
|
|
|
|
zend_hash_init(env_hash, cnt, NULL, NULL, 0);
|
2003-02-16 15:38:54 +00:00
|
|
|
|
|
|
|
/* first, we have to get the size of all the elements in the hash */
|
2016-06-13 13:08:04 +00:00
|
|
|
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(environment), key, element) {
|
|
|
|
str = zval_get_string(element);
|
2014-05-09 11:01:38 +00:00
|
|
|
|
2016-06-13 13:08:04 +00:00
|
|
|
if (ZSTR_LEN(str) == 0) {
|
|
|
|
zend_string_release(str);
|
2003-02-16 15:38:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2016-06-13 13:08:04 +00:00
|
|
|
sizeenv += ZSTR_LEN(str) + 1;
|
2010-09-08 09:11:18 +00:00
|
|
|
|
2016-06-13 13:08:04 +00:00
|
|
|
if (key && ZSTR_LEN(key)) {
|
|
|
|
sizeenv += ZSTR_LEN(key) + 1;
|
|
|
|
zend_hash_add_ptr(env_hash, key, str);
|
|
|
|
} else {
|
|
|
|
zend_hash_next_index_insert_ptr(env_hash, str);
|
2003-02-16 15:38:54 +00:00
|
|
|
}
|
2014-04-18 15:18:11 +00:00
|
|
|
} ZEND_HASH_FOREACH_END();
|
2003-02-16 15:38:54 +00:00
|
|
|
|
|
|
|
#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);
|
|
|
|
|
2016-06-13 13:08:04 +00:00
|
|
|
ZEND_HASH_FOREACH_STR_KEY_PTR(env_hash, key, str) {
|
|
|
|
if (key) {
|
|
|
|
l = ZSTR_LEN(key) + ZSTR_LEN(str) + 2;
|
|
|
|
memcpy(p, ZSTR_VAL(key), ZSTR_LEN(key));
|
2014-04-18 15:18:11 +00:00
|
|
|
strncat(p, "=", 1);
|
2015-06-30 01:05:24 +00:00
|
|
|
strncat(p, ZSTR_VAL(str), ZSTR_LEN(str));
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-02-16 15:38:54 +00:00
|
|
|
#ifndef PHP_WIN32
|
2014-04-18 15:18:11 +00:00
|
|
|
*ep = p;
|
|
|
|
++ep;
|
2003-02-16 15:38:54 +00:00
|
|
|
#endif
|
2014-04-18 15:18:11 +00:00
|
|
|
p += l;
|
|
|
|
} else {
|
2015-06-30 01:05:24 +00:00
|
|
|
memcpy(p, ZSTR_VAL(str), ZSTR_LEN(str));
|
2003-02-16 15:38:54 +00:00
|
|
|
#ifndef PHP_WIN32
|
2014-04-18 15:18:11 +00:00
|
|
|
*ep = p;
|
|
|
|
++ep;
|
2003-02-16 15:38:54 +00:00
|
|
|
#endif
|
2015-06-30 01:05:24 +00:00
|
|
|
p += ZSTR_LEN(str) + 1;
|
2003-02-16 15:38:54 +00:00
|
|
|
}
|
2014-08-25 17:24:55 +00:00
|
|
|
zend_string_release(str);
|
2014-04-18 15:18:11 +00:00
|
|
|
} ZEND_HASH_FOREACH_END();
|
2003-02-16 15:38:54 +00:00
|
|
|
|
2016-11-26 14:18:42 +00:00
|
|
|
assert((uint32_t)(p - env.envp) <= sizeenv);
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2016-06-13 13:08:04 +00:00
|
|
|
zend_hash_destroy(env_hash);
|
|
|
|
FREE_HASHTABLE(env_hash);
|
|
|
|
|
2003-02-16 15:38:54 +00: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 07:01:30 +00:00
|
|
|
/* {{{ proc_open_rsrc_dtor */
|
2014-12-13 22:06:14 +00:00
|
|
|
static void proc_open_rsrc_dtor(zend_resource *rsrc)
|
2003-01-15 16:29:00 +00:00
|
|
|
{
|
2003-01-15 18:54:03 +00:00
|
|
|
struct php_process_handle *proc = (struct php_process_handle*)rsrc->ptr;
|
|
|
|
int i;
|
2003-01-15 16:29:00 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
DWORD wstatus;
|
2003-01-15 18:54:03 +00:00
|
|
|
#elif HAVE_SYS_WAIT_H
|
|
|
|
int wstatus;
|
2013-01-15 10:25:55 +00:00
|
|
|
int waitpid_options = 0;
|
2003-01-15 18:54:03 +00:00
|
|
|
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-08 22:24:01 +00:00
|
|
|
GC_REFCOUNT(proc->pipes[i])--;
|
|
|
|
zend_list_close(proc->pipes[i]);
|
2003-01-15 18:54:03 +00:00
|
|
|
proc->pipes[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-01-15 18:54:03 +00:00
|
|
|
#ifdef PHP_WIN32
|
2013-01-15 10:25:55 +00:00
|
|
|
if (FG(pclose_wait)) {
|
|
|
|
WaitForSingleObject(proc->childHandle, INFINITE);
|
|
|
|
}
|
2006-12-31 14:47:17 +00:00
|
|
|
GetExitCodeProcess(proc->childHandle, &wstatus);
|
2013-01-15 10:25:55 +00:00
|
|
|
if (wstatus == STILL_ACTIVE) {
|
|
|
|
FG(pclose_ret) = -1;
|
|
|
|
} else {
|
|
|
|
FG(pclose_ret) = wstatus;
|
|
|
|
}
|
2006-12-31 14:47:17 +00:00
|
|
|
CloseHandle(proc->childHandle);
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-01-15 18:54:03 +00:00
|
|
|
#elif HAVE_SYS_WAIT_H
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2013-01-15 10:25:55 +00:00
|
|
|
if (!FG(pclose_wait)) {
|
|
|
|
waitpid_options = WNOHANG;
|
|
|
|
}
|
2003-01-15 16:29:00 +00:00
|
|
|
do {
|
2013-01-15 10:25:55 +00:00
|
|
|
wait_pid = waitpid(proc->child, &wstatus, waitpid_options);
|
2003-01-15 16:29:00 +00:00
|
|
|
} while (wait_pid == -1 && errno == EINTR);
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2013-01-15 10:25:55 +00:00
|
|
|
if (wait_pid <= 0) {
|
2003-01-15 16:29:00 +00:00
|
|
|
FG(pclose_ret) = -1;
|
2009-07-20 04:31:07 +00:00
|
|
|
} else {
|
2003-01-15 16:29:00 +00:00
|
|
|
if (WIFEXITED(wstatus))
|
|
|
|
wstatus = WEXITSTATUS(wstatus);
|
|
|
|
FG(pclose_ret) = wstatus;
|
|
|
|
}
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-01-15 18:54:03 +00:00
|
|
|
#else
|
2003-01-15 16:29:00 +00:00
|
|
|
FG(pclose_ret) = -1;
|
|
|
|
#endif
|
2003-02-16 15:38:54 +00:00
|
|
|
_php_free_envp(proc->env, proc->is_persistent);
|
2014-12-18 12:28:01 +00:00
|
|
|
pefree(proc->pipes, proc->is_persistent);
|
2003-01-15 18:54:03 +00:00
|
|
|
pefree(proc->command, proc->is_persistent);
|
|
|
|
pefree(proc, proc->is_persistent);
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-01-15 16:29:00 +00:00
|
|
|
}
|
2003-07-12 07:01:30 +00:00
|
|
|
/* }}} */
|
2003-01-15 16:29:00 +00:00
|
|
|
|
2003-07-12 07:01:30 +00:00
|
|
|
/* {{{ PHP_MINIT_FUNCTION(proc_open) */
|
2003-01-15 16:29:00 +00:00
|
|
|
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 07:01:30 +00:00
|
|
|
/* }}} */
|
2003-02-15 17:18:57 +00:00
|
|
|
|
2007-02-14 19:13:31 +00:00
|
|
|
/* {{{ proto bool proc_terminate(resource process [, long signal])
|
2003-02-15 17:18:57 +00:00
|
|
|
kill a process opened by proc_open */
|
|
|
|
PHP_FUNCTION(proc_terminate)
|
|
|
|
{
|
|
|
|
zval *zproc;
|
|
|
|
struct php_process_handle *proc;
|
2014-08-25 17:24:55 +00:00
|
|
|
zend_long sig_no = SIGTERM;
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &zproc, &sig_no) == FAILURE) {
|
2003-02-15 17:18:57 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2015-02-02 05:23:16 +00:00
|
|
|
if ((proc = (struct php_process_handle *)zend_fetch_resource(Z_RES_P(zproc), "process", le_proc_open)) == NULL) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-02-15 17:18:57 +00:00
|
|
|
#ifdef PHP_WIN32
|
2007-02-14 19:13:31 +00:00
|
|
|
if (TerminateProcess(proc->childHandle, 255)) {
|
|
|
|
RETURN_TRUE;
|
|
|
|
} else {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2003-02-15 17:18:57 +00:00
|
|
|
#else
|
2007-02-14 19:13:31 +00:00
|
|
|
if (kill(proc->child, sig_no) == 0) {
|
|
|
|
RETURN_TRUE;
|
|
|
|
} else {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2003-02-15 17:18:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-01-15 16:29:00 +00:00
|
|
|
/* {{{ proto int proc_close(resource process)
|
|
|
|
close a process opened by proc_open */
|
|
|
|
PHP_FUNCTION(proc_close)
|
|
|
|
{
|
2003-01-15 18:54:03 +00:00
|
|
|
zval *zproc;
|
|
|
|
struct php_process_handle *proc;
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zproc) == FAILURE) {
|
2003-01-15 16:29:00 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2015-02-02 05:23:16 +00:00
|
|
|
if ((proc = (struct php_process_handle *)zend_fetch_resource(Z_RES_P(zproc), "process", le_proc_open)) == NULL) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2013-01-15 10:25:55 +00:00
|
|
|
FG(pclose_wait) = 1;
|
2014-05-08 22:24:01 +00:00
|
|
|
zend_list_close(Z_RES_P(zproc));
|
2013-01-15 10:25:55 +00:00
|
|
|
FG(pclose_wait) = 0;
|
2014-08-25 17:24:55 +00:00
|
|
|
RETURN_LONG(FG(pclose_ret));
|
2003-01-15 16:29:00 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-01-15 18:54:03 +00:00
|
|
|
/* {{{ 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-30 16:52:48 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zproc) == FAILURE) {
|
2003-01-15 18:54:03 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2015-02-02 05:23:16 +00:00
|
|
|
if ((proc = (struct php_process_handle *)zend_fetch_resource(Z_RES_P(zproc), "process", le_proc_open)) == NULL) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2003-01-15 18:54:03 +00:00
|
|
|
|
|
|
|
array_init(return_value);
|
|
|
|
|
2014-04-15 11:40:40 +00:00
|
|
|
add_assoc_string(return_value, "command", proc->command);
|
2014-08-25 17:24:55 +00:00
|
|
|
add_assoc_long(return_value, "pid", (zend_long) proc->child);
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-01-15 18:54:03 +00:00
|
|
|
#ifdef PHP_WIN32
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2006-12-31 14:47:17 +00:00
|
|
|
GetExitCodeProcess(proc->childHandle, &wstatus);
|
2003-01-15 18:54:03 +00:00
|
|
|
|
|
|
|
running = wstatus == STILL_ACTIVE;
|
2009-08-03 19:05:56 +00:00
|
|
|
exitcode = running ? -1 : wstatus;
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-01-15 18:54:03 +00:00
|
|
|
#elif HAVE_SYS_WAIT_H
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-01-15 18:54:03 +00:00
|
|
|
errno = 0;
|
|
|
|
wait_pid = waitpid(proc->child, &wstatus, WNOHANG|WUNTRACED);
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-01-15 18:54:03 +00:00
|
|
|
if (wait_pid == proc->child) {
|
|
|
|
if (WIFEXITED(wstatus)) {
|
|
|
|
running = 0;
|
|
|
|
exitcode = WEXITSTATUS(wstatus);
|
|
|
|
}
|
|
|
|
if (WIFSIGNALED(wstatus)) {
|
2005-04-07 23:07:45 +00:00
|
|
|
running = 0;
|
2003-01-15 18:54:03 +00:00
|
|
|
signaled = 1;
|
2016-11-12 10:20:01 +00:00
|
|
|
|
2003-01-15 18:54:03 +00:00
|
|
|
termsig = WTERMSIG(wstatus);
|
|
|
|
}
|
|
|
|
if (WIFSTOPPED(wstatus)) {
|
|
|
|
stopped = 1;
|
|
|
|
stopsig = WSTOPSIG(wstatus);
|
|
|
|
}
|
2005-04-07 23:07:45 +00:00
|
|
|
} else if (wait_pid == -1) {
|
2005-03-07 16:17:12 +00:00
|
|
|
running = 0;
|
2003-01-15 18:54:03 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
add_assoc_bool(return_value, "running", running);
|
|
|
|
add_assoc_bool(return_value, "signaled", signaled);
|
|
|
|
add_assoc_bool(return_value, "stopped", stopped);
|
2014-08-25 17:24:55 +00:00
|
|
|
add_assoc_long(return_value, "exitcode", exitcode);
|
|
|
|
add_assoc_long(return_value, "termsig", termsig);
|
|
|
|
add_assoc_long(return_value, "stopsig", stopsig);
|
2003-01-15 18:54:03 +00:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-01-15 16:29:00 +00:00
|
|
|
/* {{{ handy definitions for portability/readability */
|
|
|
|
#ifdef PHP_WIN32
|
2011-10-24 12:39:55 +00:00
|
|
|
# define pipe(pair) (CreatePipe(&pair[0], &pair[1], &security, 0) ? 0 : -1)
|
2003-01-15 16:29:00 +00:00
|
|
|
|
|
|
|
# define COMSPEC_NT "cmd.exe"
|
|
|
|
|
|
|
|
static inline HANDLE dup_handle(HANDLE src, BOOL inherit, BOOL closeorig)
|
|
|
|
{
|
|
|
|
HANDLE copy, self = GetCurrentProcess();
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-01-15 16:29:00 +00:00
|
|
|
if (!DuplicateHandle(self, src, self, ©, 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 */
|
2003-01-15 18:54:03 +00:00
|
|
|
php_file_descriptor_t parentend, childend; /* fds for pipes in parent/child */
|
2003-01-15 16:29:00 +00:00
|
|
|
int mode; /* mode for proc_open code */
|
|
|
|
int mode_flags; /* mode flags for opening fds */
|
|
|
|
};
|
|
|
|
/* }}} */
|
|
|
|
|
2003-12-06 10:48:56 +00:00
|
|
|
/* {{{ proto resource proc_open(string command, array descriptorspec, array &pipes [, string cwd [, array env [, array other_options]]])
|
2003-01-15 16:29:00 +00:00
|
|
|
Run a process with more control over it's file descriptors */
|
|
|
|
PHP_FUNCTION(proc_open)
|
|
|
|
{
|
2003-02-16 15:38:54 +00:00
|
|
|
char *command, *cwd=NULL;
|
2014-08-27 13:31:48 +00:00
|
|
|
size_t command_len, cwd_len = 0;
|
2003-01-15 16:29:00 +00:00
|
|
|
zval *descriptorspec;
|
|
|
|
zval *pipes;
|
2003-02-16 15:38:54 +00:00
|
|
|
zval *environment = NULL;
|
2003-12-05 13:45:00 +00:00
|
|
|
zval *other_options = NULL;
|
2003-02-16 15:38:54 +00:00
|
|
|
php_process_env_t env;
|
2003-01-15 16:29:00 +00:00
|
|
|
int ndesc = 0;
|
|
|
|
int i;
|
2014-02-14 09:40:11 +00:00
|
|
|
zval *descitem = NULL;
|
2014-04-18 15:18:11 +00:00
|
|
|
zend_string *str_index;
|
2014-08-25 17:24:55 +00:00
|
|
|
zend_ulong nindex;
|
2014-12-18 12:28:01 +00:00
|
|
|
struct php_proc_open_descriptor_item *descriptors = NULL;
|
|
|
|
int ndescriptors_array;
|
2003-01-15 16:29:00 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
PROCESS_INFORMATION pi;
|
2006-12-31 14:47:17 +00:00
|
|
|
HANDLE childHandle;
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
STARTUPINFOW si;
|
2003-01-15 16:29:00 +00:00
|
|
|
BOOL newprocok;
|
|
|
|
SECURITY_ATTRIBUTES security;
|
2009-06-09 00:25:37 +00:00
|
|
|
DWORD dwCreateFlags = 0;
|
2003-12-05 13:45:00 +00:00
|
|
|
UINT old_error_mode;
|
2013-01-07 12:03:12 +00:00
|
|
|
char cur_cwd[MAXPATHLEN];
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
wchar_t *cmdw = NULL, *cwdw = NULL, *envpw = NULL;
|
|
|
|
size_t tmp_len;
|
2003-01-15 16:29:00 +00:00
|
|
|
#endif
|
2003-01-15 18:54:03 +00:00
|
|
|
php_process_id_t child;
|
|
|
|
struct php_process_handle *proc;
|
|
|
|
int is_persistent = 0; /* TODO: ensure that persistent procs will work */
|
2006-06-01 14:03:49 +00:00
|
|
|
#ifdef PHP_WIN32
|
2003-12-05 13:48:40 +00:00
|
|
|
int suppress_errors = 0;
|
2009-07-19 14:52:27 +00:00
|
|
|
int bypass_shell = 0;
|
2015-06-30 13:47:25 +00:00
|
|
|
int blocking_pipes = 0;
|
2009-07-20 11:48:04 +00:00
|
|
|
#endif
|
2004-04-19 15:07:30 +00:00
|
|
|
#if PHP_CAN_DO_PTS
|
|
|
|
php_file_descriptor_t dev_ptmx = -1; /* master */
|
|
|
|
php_file_descriptor_t slave_pty = -1;
|
|
|
|
#endif
|
2003-01-15 16:29:00 +00:00
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS(), "saz/|s!a!a!", &command,
|
2009-07-20 11:48:04 +00:00
|
|
|
&command_len, &descriptorspec, &pipes, &cwd, &cwd_len, &environment,
|
2003-12-05 13:45:00 +00:00
|
|
|
&other_options) == FAILURE) {
|
2003-01-15 16:29:00 +00:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-04-26 23:53:30 +00:00
|
|
|
command = pestrdup(command, is_persistent);
|
2009-07-20 11:48:04 +00:00
|
|
|
|
|
|
|
#ifdef PHP_WIN32
|
2003-12-05 13:45:00 +00:00
|
|
|
if (other_options) {
|
2014-05-13 06:11:44 +00:00
|
|
|
zval *item = zend_hash_str_find(Z_ARRVAL_P(other_options), "suppress_errors", sizeof("suppress_errors") - 1);
|
2014-05-13 04:50:20 +00:00
|
|
|
if (item != NULL) {
|
2014-08-25 17:24:55 +00:00
|
|
|
if (Z_TYPE_P(item) == IS_TRUE || ((Z_TYPE_P(item) == IS_LONG) && Z_LVAL_P(item))) {
|
2003-12-05 13:45:00 +00:00
|
|
|
suppress_errors = 1;
|
|
|
|
}
|
2010-08-30 16:52:48 +00:00
|
|
|
}
|
2014-05-13 04:50:20 +00:00
|
|
|
|
2014-05-13 06:11:44 +00:00
|
|
|
item = zend_hash_str_find(Z_ARRVAL_P(other_options), "bypass_shell", sizeof("bypass_shell") - 1);
|
2014-05-13 04:50:20 +00:00
|
|
|
if (item != NULL) {
|
2014-08-25 17:24:55 +00:00
|
|
|
if (Z_TYPE_P(item) == IS_TRUE || ((Z_TYPE_P(item) == IS_LONG) && Z_LVAL_P(item))) {
|
2007-01-06 09:02:02 +00:00
|
|
|
bypass_shell = 1;
|
|
|
|
}
|
2010-08-30 16:52:48 +00:00
|
|
|
}
|
2015-06-30 13:47:25 +00:00
|
|
|
|
|
|
|
item = zend_hash_str_find(Z_ARRVAL_P(other_options), "blocking_pipes", sizeof("blocking_pipes") - 1);
|
|
|
|
if (item != NULL) {
|
|
|
|
if (Z_TYPE_P(item) == IS_TRUE || ((Z_TYPE_P(item) == IS_LONG) && Z_LVAL_P(item))) {
|
|
|
|
blocking_pipes = 1;
|
|
|
|
}
|
|
|
|
}
|
2009-07-19 14:52:27 +00:00
|
|
|
}
|
|
|
|
#endif
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-02-17 06:45:37 +00:00
|
|
|
command_len = strlen(command);
|
2003-02-17 03:26:16 +00:00
|
|
|
|
2003-02-16 15:38:54 +00:00
|
|
|
if (environment) {
|
2014-12-13 22:06:14 +00:00
|
|
|
env = _php_array_to_envp(environment, is_persistent);
|
2003-02-16 15:38:54 +00:00
|
|
|
} else {
|
|
|
|
memset(&env, 0, sizeof(env));
|
|
|
|
}
|
|
|
|
|
2014-12-18 12:28:01 +00:00
|
|
|
ndescriptors_array = zend_hash_num_elements(Z_ARRVAL_P(descriptorspec));
|
|
|
|
|
|
|
|
descriptors = safe_emalloc(sizeof(struct php_proc_open_descriptor_item), ndescriptors_array, 0);
|
|
|
|
|
|
|
|
memset(descriptors, 0, sizeof(struct php_proc_open_descriptor_item) * ndescriptors_array);
|
2003-01-15 16:29:00 +00:00
|
|
|
|
|
|
|
#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-30 16:52:48 +00:00
|
|
|
|
2003-01-15 16:29:00 +00:00
|
|
|
/* walk the descriptor spec and set up files/pipes */
|
2014-04-18 15:18:11 +00:00
|
|
|
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(descriptorspec), nindex, str_index, descitem) {
|
2014-02-14 09:40:11 +00:00
|
|
|
zval *ztype;
|
2003-01-15 16:29:00 +00:00
|
|
|
|
|
|
|
if (str_index) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "descriptor spec must be an integer indexed array");
|
2003-01-15 16:29:00 +00:00
|
|
|
goto exit_fail;
|
|
|
|
}
|
|
|
|
|
2014-10-23 14:29:02 +00:00
|
|
|
descriptors[ndesc].index = (int)nindex;
|
2003-01-15 16:29:00 +00:00
|
|
|
|
2014-02-14 09:40:11 +00:00
|
|
|
if (Z_TYPE_P(descitem) == IS_RESOURCE) {
|
2003-01-15 16:29:00 +00:00
|
|
|
/* should be a stream - try and dup the descriptor */
|
|
|
|
php_stream *stream;
|
2014-03-19 05:26:54 +00:00
|
|
|
php_socket_t fd;
|
2003-01-15 16:29:00 +00:00
|
|
|
|
|
|
|
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
|
2014-10-23 14:29:02 +00:00
|
|
|
descriptors[ndesc].childend = dup_fd_as_handle((int)fd);
|
2003-01-15 16:29:00 +00:00
|
|
|
if (descriptors[ndesc].childend == NULL) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "unable to dup File-Handle for descriptor %d", nindex);
|
2003-01-15 16:29:00 +00:00
|
|
|
goto exit_fail;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
descriptors[ndesc].childend = dup(fd);
|
|
|
|
if (descriptors[ndesc].childend < 0) {
|
2016-06-21 13:00:37 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "unable to dup File-Handle for descriptor " ZEND_ULONG_FMT " - %s", nindex, strerror(errno));
|
2003-01-15 16:29:00 +00:00
|
|
|
goto exit_fail;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
descriptors[ndesc].mode = DESC_FILE;
|
|
|
|
|
2014-02-14 09:40:11 +00:00
|
|
|
} else if (Z_TYPE_P(descitem) != IS_ARRAY) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Descriptor item must be either an array or a File-Handle");
|
2003-01-15 16:29:00 +00:00
|
|
|
goto exit_fail;
|
|
|
|
} else {
|
|
|
|
|
2014-02-14 09:40:11 +00:00
|
|
|
if ((ztype = zend_hash_index_find(Z_ARRVAL_P(descitem), 0)) != NULL) {
|
2003-01-15 16:29:00 +00:00
|
|
|
convert_to_string_ex(ztype);
|
|
|
|
} else {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Missing handle qualifier in array");
|
2003-01-15 16:29:00 +00:00
|
|
|
goto exit_fail;
|
|
|
|
}
|
|
|
|
|
2014-02-14 09:40:11 +00:00
|
|
|
if (strcmp(Z_STRVAL_P(ztype), "pipe") == 0) {
|
2003-01-15 18:54:03 +00:00
|
|
|
php_file_descriptor_t newpipe[2];
|
2014-02-14 09:40:11 +00:00
|
|
|
zval *zmode;
|
2003-01-15 16:29:00 +00:00
|
|
|
|
2014-02-14 09:40:11 +00:00
|
|
|
if ((zmode = zend_hash_index_find(Z_ARRVAL_P(descitem), 1)) != NULL) {
|
2003-01-15 16:29:00 +00:00
|
|
|
convert_to_string_ex(zmode);
|
|
|
|
} else {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Missing mode parameter for 'pipe'");
|
2003-01-15 16:29:00 +00:00
|
|
|
goto exit_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
descriptors[ndesc].mode = DESC_PIPE;
|
|
|
|
|
|
|
|
if (0 != pipe(newpipe)) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "unable to create pipe %s", strerror(errno));
|
2003-01-15 16:29:00 +00:00
|
|
|
goto exit_fail;
|
|
|
|
}
|
|
|
|
|
2014-02-14 09:40:11 +00:00
|
|
|
if (strncmp(Z_STRVAL_P(zmode), "w", 1) != 0) {
|
2003-01-15 16:29:00 +00:00
|
|
|
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
|
2014-08-25 17:24:55 +00:00
|
|
|
if (Z_STRLEN_P(zmode) >= 2 && Z_STRVAL_P(zmode)[1] == 'b')
|
2003-01-15 16:29:00 +00:00
|
|
|
descriptors[ndesc].mode_flags |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
|
2014-02-14 09:40:11 +00:00
|
|
|
} else if (strcmp(Z_STRVAL_P(ztype), "file") == 0) {
|
|
|
|
zval *zfile, *zmode;
|
2014-03-19 05:26:54 +00:00
|
|
|
php_socket_t fd;
|
2003-01-15 16:29:00 +00:00
|
|
|
php_stream *stream;
|
|
|
|
|
|
|
|
descriptors[ndesc].mode = DESC_FILE;
|
|
|
|
|
2014-02-14 09:40:11 +00:00
|
|
|
if ((zfile = zend_hash_index_find(Z_ARRVAL_P(descitem), 1)) != NULL) {
|
2003-01-15 16:29:00 +00:00
|
|
|
convert_to_string_ex(zfile);
|
|
|
|
} else {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Missing file name parameter for 'file'");
|
2003-01-15 16:29:00 +00:00
|
|
|
goto exit_fail;
|
|
|
|
}
|
|
|
|
|
2014-02-14 09:40:11 +00:00
|
|
|
if ((zmode = zend_hash_index_find(Z_ARRVAL_P(descitem), 2)) != NULL) {
|
2003-01-15 16:29:00 +00:00
|
|
|
convert_to_string_ex(zmode);
|
|
|
|
} else {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Missing mode parameter for 'file'");
|
2003-01-15 16:29:00 +00:00
|
|
|
goto exit_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try a wrapper */
|
2014-02-14 09:40:11 +00:00
|
|
|
stream = php_stream_open_wrapper(Z_STRVAL_P(zfile), Z_STRVAL_P(zmode),
|
2010-04-26 23:53:30 +00:00
|
|
|
REPORT_ERRORS|STREAM_WILL_CAST, NULL);
|
2003-01-15 16:29:00 +00:00
|
|
|
|
|
|
|
/* 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
|
2014-10-23 14:29:02 +00:00
|
|
|
descriptors[ndesc].childend = dup_fd_as_handle((int)fd);
|
|
|
|
_close((int)fd);
|
2007-09-12 11:42:43 +00:00
|
|
|
|
|
|
|
/* 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 */
|
2014-05-13 04:50:20 +00:00
|
|
|
if (strchr(Z_STRVAL_P(zmode), 'a')) {
|
2007-09-12 11:42:43 +00:00
|
|
|
SetFilePointer(descriptors[ndesc].childend, 0, NULL, FILE_END);
|
|
|
|
}
|
2003-01-15 16:29:00 +00:00
|
|
|
#else
|
|
|
|
descriptors[ndesc].childend = fd;
|
|
|
|
#endif
|
2014-02-14 09:40:11 +00:00
|
|
|
} else if (strcmp(Z_STRVAL_P(ztype), "pty") == 0) {
|
2004-04-19 15:07:30 +00:00
|
|
|
#if PHP_CAN_DO_PTS
|
|
|
|
if (dev_ptmx == -1) {
|
|
|
|
/* open things up */
|
|
|
|
dev_ptmx = open("/dev/ptmx", O_RDWR);
|
|
|
|
if (dev_ptmx == -1) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "failed to open /dev/ptmx, errno %d", errno);
|
2004-04-19 15:07:30 +00:00
|
|
|
goto exit_fail;
|
|
|
|
}
|
|
|
|
grantpt(dev_ptmx);
|
|
|
|
unlockpt(dev_ptmx);
|
|
|
|
slave_pty = open(ptsname(dev_ptmx), O_RDWR);
|
2003-01-15 16:29:00 +00:00
|
|
|
|
2004-04-19 15:07:30 +00:00
|
|
|
if (slave_pty == -1) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "failed to open slave pty, errno %d", errno);
|
2004-04-19 15:07:30 +00:00
|
|
|
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
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "pty pseudo terminal not supported on this system");
|
2004-04-19 15:07:30 +00:00
|
|
|
goto exit_fail;
|
|
|
|
#endif
|
2003-01-15 16:29:00 +00:00
|
|
|
} else {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "%s is not a valid descriptor spec/mode", Z_STRVAL_P(ztype));
|
2003-01-15 16:29:00 +00:00
|
|
|
goto exit_fail;
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 12:28:01 +00:00
|
|
|
ndesc++;
|
2014-04-18 15:18:11 +00:00
|
|
|
} ZEND_HASH_FOREACH_END();
|
2003-01-15 16:29:00 +00:00
|
|
|
|
|
|
|
#ifdef PHP_WIN32
|
2010-09-08 08:34:57 +00:00
|
|
|
if (cwd == NULL) {
|
|
|
|
char *getcwd_result;
|
|
|
|
getcwd_result = VCWD_GETCWD(cur_cwd, MAXPATHLEN);
|
|
|
|
if (!getcwd_result) {
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Cannot get current directory");
|
2010-09-08 08:34:57 +00:00
|
|
|
goto exit_fail;
|
|
|
|
}
|
2013-01-07 12:03:12 +00:00
|
|
|
cwd = cur_cwd;
|
2010-09-08 08:34:57 +00:00
|
|
|
}
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
cwdw = php_win32_cp_any_to_w(cwd);
|
|
|
|
if (!cwdw) {
|
|
|
|
php_error_docref(NULL, E_WARNING, "CWD conversion failed");
|
|
|
|
goto exit_fail;
|
|
|
|
}
|
2010-09-08 08:34:57 +00:00
|
|
|
|
2003-01-15 16:29:00 +00:00
|
|
|
memset(&si, 0, sizeof(si));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES;
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-01-15 16:29:00 +00:00
|
|
|
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-01-15 16:29:00 +00: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-30 16:52:48 +00:00
|
|
|
|
2003-01-15 16:29:00 +00:00
|
|
|
memset(&pi, 0, sizeof(pi));
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-12-05 13:45:00 +00:00
|
|
|
if (suppress_errors) {
|
|
|
|
old_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOGPFAULTERRORBOX);
|
|
|
|
}
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2009-06-09 00:25:37 +00:00
|
|
|
dwCreateFlags = NORMAL_PRIORITY_CLASS;
|
|
|
|
if(strcmp(sapi_module.name, "cli") != 0) {
|
|
|
|
dwCreateFlags |= CREATE_NO_WINDOW;
|
|
|
|
}
|
|
|
|
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
envpw = php_win32_cp_env_any_to_w(env.envp);
|
|
|
|
if (envpw) {
|
|
|
|
dwCreateFlags |= CREATE_UNICODE_ENVIRONMENT;
|
|
|
|
} else {
|
|
|
|
if (env.envp) {
|
|
|
|
php_error_docref(NULL, E_WARNING, "ENV conversion failed");
|
|
|
|
goto exit_fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdw = php_win32_cp_conv_any_to_w(command, command_len, &tmp_len);
|
|
|
|
if (!cmdw) {
|
|
|
|
php_error_docref(NULL, E_WARNING, "Command conversion failed");
|
|
|
|
goto exit_fail;
|
|
|
|
}
|
|
|
|
|
2007-01-06 09:02:02 +00:00
|
|
|
if (bypass_shell) {
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
newprocok = CreateProcessW(NULL, cmdw, &security, &security, TRUE, dwCreateFlags, envpw, cwdw, &si, &pi);
|
2007-01-06 09:02:02 +00:00
|
|
|
} else {
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
wchar_t *cmdw2;
|
|
|
|
|
2007-01-06 09:02:02 +00:00
|
|
|
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
len = (sizeof(COMSPEC_NT) + sizeof(" /c ") + tmp_len + 1);
|
|
|
|
cmdw2 = (wchar_t *)malloc(len * sizeof(wchar_t));
|
2016-08-29 15:25:46 +00:00
|
|
|
if (!cmdw2) {
|
|
|
|
php_error_docref(NULL, E_WARNING, "Command conversion failed");
|
|
|
|
goto exit_fail;
|
|
|
|
}
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
ret = _snwprintf(cmdw2, len, L"%hs /c %s", COMSPEC_NT, cmdw);
|
|
|
|
|
|
|
|
if (-1 == ret) {
|
2016-08-29 15:25:46 +00:00
|
|
|
free(cmdw2);
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "Command conversion failed");
|
|
|
|
goto exit_fail;
|
|
|
|
}
|
2010-08-30 16:52:48 +00:00
|
|
|
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
newprocok = CreateProcessW(NULL, cmdw2, &security, &security, TRUE, dwCreateFlags, envpw, cwdw, &si, &pi);
|
|
|
|
free(cmdw2);
|
2007-01-06 09:02:02 +00:00
|
|
|
}
|
2003-12-05 13:45:00 +00:00
|
|
|
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
free(cwdw);
|
|
|
|
cwdw = NULL;
|
|
|
|
free(cmdw);
|
|
|
|
cmdw = NULL;
|
|
|
|
free(envpw);
|
|
|
|
envpw = NULL;
|
|
|
|
|
2003-12-05 13:45:00 +00:00
|
|
|
if (suppress_errors) {
|
|
|
|
SetErrorMode(old_error_mode);
|
|
|
|
}
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-01-15 16:29:00 +00:00
|
|
|
if (FALSE == newprocok) {
|
2008-08-21 11:27:57 +00:00
|
|
|
DWORD dw = GetLastError();
|
|
|
|
|
2007-01-09 16:27:17 +00:00
|
|
|
/* clean up all the descriptors */
|
|
|
|
for (i = 0; i < ndesc; i++) {
|
|
|
|
CloseHandle(descriptors[i].childend);
|
|
|
|
if (descriptors[i].parentend) {
|
|
|
|
CloseHandle(descriptors[i].parentend);
|
|
|
|
}
|
|
|
|
}
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "CreateProcess failed, error code - %u", dw);
|
2003-01-15 16:29:00 +00:00
|
|
|
goto exit_fail;
|
|
|
|
}
|
|
|
|
|
2006-12-31 14:47:17 +00:00
|
|
|
childHandle = pi.hProcess;
|
|
|
|
child = pi.dwProcessId;
|
2003-01-15 16:29:00 +00:00
|
|
|
CloseHandle(pi.hThread);
|
2003-01-15 18:54:03 +00:00
|
|
|
#elif HAVE_FORK
|
2003-01-15 16:29:00 +00:00
|
|
|
/* the unix way */
|
|
|
|
child = fork();
|
|
|
|
|
|
|
|
if (child == 0) {
|
|
|
|
/* this is the child process */
|
|
|
|
|
2004-04-19 15:07:30 +00:00
|
|
|
#if PHP_CAN_DO_PTS
|
|
|
|
if (dev_ptmx >= 0) {
|
|
|
|
int my_pid = getpid();
|
|
|
|
|
2004-05-29 00:30:11 +00:00
|
|
|
#ifdef TIOCNOTTY
|
2004-04-19 15:07:30 +00:00
|
|
|
/* detach from original tty. Might only need this if isatty(0) is true */
|
|
|
|
ioctl(0,TIOCNOTTY,NULL);
|
2004-05-29 00:30:11 +00:00
|
|
|
#else
|
|
|
|
setsid();
|
|
|
|
#endif
|
2004-04-19 15:07:30 +00:00
|
|
|
/* become process group leader */
|
|
|
|
setpgid(my_pid, my_pid);
|
|
|
|
tcsetpgrp(0, my_pid);
|
|
|
|
}
|
|
|
|
#endif
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-01-15 16:29:00 +00: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);
|
|
|
|
}
|
2004-04-19 15:07:30 +00:00
|
|
|
|
|
|
|
#if PHP_CAN_DO_PTS
|
|
|
|
if (dev_ptmx >= 0) {
|
|
|
|
close(dev_ptmx);
|
|
|
|
close(slave_pty);
|
|
|
|
}
|
|
|
|
#endif
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2003-02-16 15:38:54 +00:00
|
|
|
if (cwd) {
|
2011-05-16 17:22:41 +00:00
|
|
|
php_ignore_value(chdir(cwd));
|
2003-02-16 15:38:54 +00:00
|
|
|
}
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2009-07-20 11:48:04 +00:00
|
|
|
if (env.envarray) {
|
2003-02-16 15:38:54 +00:00
|
|
|
execle("/bin/sh", "sh", "-c", command, NULL, env.envarray);
|
|
|
|
} else {
|
|
|
|
execl("/bin/sh", "sh", "-c", command, NULL);
|
|
|
|
}
|
2003-01-15 16:29:00 +00:00
|
|
|
_exit(127);
|
|
|
|
|
|
|
|
} else if (child < 0) {
|
|
|
|
/* failed to fork() */
|
|
|
|
|
|
|
|
/* clean up all the descriptors */
|
|
|
|
for (i = 0; i < ndesc; i++) {
|
|
|
|
close(descriptors[i].childend);
|
2007-01-02 15:29:09 +00:00
|
|
|
if (descriptors[i].parentend)
|
|
|
|
close(descriptors[i].parentend);
|
2003-01-15 16:29:00 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "fork failed - %s", strerror(errno));
|
2003-01-15 16:29:00 +00:00
|
|
|
|
|
|
|
goto exit_fail;
|
|
|
|
|
|
|
|
}
|
2003-01-15 18:54:03 +00:00
|
|
|
#else
|
|
|
|
# error You lose (configure should not have let you get here)
|
2003-01-15 16:29:00 +00:00
|
|
|
#endif
|
|
|
|
/* we forked/spawned and this is the parent */
|
|
|
|
|
2003-01-15 18:54:03 +00:00
|
|
|
proc = (struct php_process_handle*)pemalloc(sizeof(struct php_process_handle), is_persistent);
|
|
|
|
proc->is_persistent = is_persistent;
|
|
|
|
proc->command = command;
|
2014-12-18 12:28:01 +00:00
|
|
|
proc->pipes = pemalloc(sizeof(zend_resource *) * ndesc, is_persistent);
|
2003-01-15 18:54:03 +00:00
|
|
|
proc->npipes = ndesc;
|
|
|
|
proc->child = child;
|
2006-12-31 14:47:17 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
proc->childHandle = childHandle;
|
|
|
|
#endif
|
2003-02-16 15:38:54 +00:00
|
|
|
proc->env = env;
|
2003-01-15 18:54:03 +00:00
|
|
|
|
2003-07-12 06:50:32 +00:00
|
|
|
if (pipes != NULL) {
|
|
|
|
zval_dtor(pipes);
|
2015-01-03 09:22:58 +00:00
|
|
|
}
|
2014-03-03 08:50:35 +00:00
|
|
|
|
2003-01-15 16:29:00 +00:00
|
|
|
array_init(pipes);
|
|
|
|
|
2004-04-19 15:07:30 +00:00
|
|
|
#if PHP_CAN_DO_PTS
|
|
|
|
if (dev_ptmx >= 0) {
|
|
|
|
close(dev_ptmx);
|
|
|
|
close(slave_pty);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-01-15 16:29:00 +00:00
|
|
|
/* 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;
|
2004-04-19 15:07:30 +00:00
|
|
|
php_stream *stream = NULL;
|
2003-01-15 16:29:00 +00:00
|
|
|
|
|
|
|
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;
|
2004-04-19 15:07:30 +00:00
|
|
|
case O_RDWR:
|
|
|
|
mode_string = "r+";
|
|
|
|
break;
|
2003-01-15 16:29:00 +00:00
|
|
|
}
|
|
|
|
#ifdef PHP_WIN32
|
2007-04-16 08:09:56 +00:00
|
|
|
stream = php_stream_fopen_from_fd(_open_osfhandle((zend_intptr_t)descriptors[i].parentend,
|
2004-04-19 15:07:30 +00:00
|
|
|
descriptors[i].mode_flags), mode_string, NULL);
|
2015-06-30 15:21:21 +00:00
|
|
|
php_stream_set_option(stream, PHP_STREAM_OPTION_PIPE_BLOCKING, blocking_pipes, NULL);
|
2003-01-15 16:29:00 +00:00
|
|
|
#else
|
2004-04-19 15:07:30 +00:00
|
|
|
stream = php_stream_fopen_from_fd(descriptors[i].parentend, mode_string, NULL);
|
2007-02-13 19:53:42 +00:00
|
|
|
# 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
|
2003-01-15 16:29:00 +00:00
|
|
|
#endif
|
2004-04-19 15:07:30 +00:00
|
|
|
if (stream) {
|
2014-02-14 09:40:11 +00:00
|
|
|
zval retfp;
|
2003-01-15 16:29:00 +00:00
|
|
|
|
2004-04-19 15:07:30 +00:00
|
|
|
/* nasty hack; don't copy it */
|
2008-07-23 11:25:14 +00:00
|
|
|
stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
|
2010-08-30 16:52:48 +00:00
|
|
|
|
2014-02-14 09:40:11 +00:00
|
|
|
php_stream_to_zval(stream, &retfp);
|
|
|
|
add_index_zval(pipes, descriptors[i].index, &retfp);
|
2003-01-15 18:54:03 +00:00
|
|
|
|
2014-04-09 11:05:22 +00:00
|
|
|
proc->pipes[i] = Z_RES(retfp);
|
|
|
|
Z_ADDREF(retfp);
|
2003-01-15 16:29:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2003-01-15 18:54:03 +00:00
|
|
|
default:
|
2014-02-14 09:40:11 +00:00
|
|
|
proc->pipes[i] = NULL;
|
2003-01-15 16:29:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 12:28:01 +00:00
|
|
|
efree(descriptors);
|
2015-02-02 05:23:16 +00:00
|
|
|
ZVAL_RES(return_value, zend_register_resource(proc, le_proc_open));
|
2003-01-15 16:29:00 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
exit_fail:
|
2014-12-18 12:28:01 +00:00
|
|
|
efree(descriptors);
|
2003-02-16 15:38:54 +00:00
|
|
|
_php_free_envp(env, is_persistent);
|
2003-01-15 18:54:03 +00:00
|
|
|
pefree(command, is_persistent);
|
Fixed the UTF-8 and long path support in the streams on Windows.
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx
The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:
https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672
Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.
The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it
https://msdn.microsoft.com/en-us/library/tsbaswba.aspx
However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.
For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.
This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).
In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.
The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).
The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.
Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.
Thanks.
2016-06-20 07:32:19 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
free(cwdw);
|
|
|
|
free(cmdw);
|
|
|
|
free(envpw);
|
|
|
|
#endif
|
2004-04-19 15:07:30 +00:00
|
|
|
#if PHP_CAN_DO_PTS
|
|
|
|
if (dev_ptmx >= 0) {
|
|
|
|
close(dev_ptmx);
|
|
|
|
}
|
|
|
|
if (slave_pty >= 0) {
|
|
|
|
close(slave_pty);
|
|
|
|
}
|
|
|
|
#endif
|
2003-01-15 16:29:00 +00:00
|
|
|
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
|
|
|
|
*/
|