问题
I build PHP for android using the NDK. Most functions tested so far run perfectly, except...
All exec related php function (like exec, shell_exec, popen, ..) all sefgault.
php sample code (test.php)
<?php
$s=shell_exec("ls");
echo $s
?>
result:
# php test.php
[1] Segmentation fault
I added some debug code to the internal shell_exec function
PHP_FUNCTION(shell_exec)
{
FILE *in;
size_t total_readbytes;
zval **cmd;
char *ret;
php_stream *stream;
if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &cmd)==FAILURE) {
WRONG_PARAM_COUNT;
}
if (PG(safe_mode)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot execute using backquotes in Safe Mode");
RETURN_FALSE;
}
convert_to_string_ex(cmd);
#ifdef PHP_WIN32
if ((in=VCWD_POPEN(Z_STRVAL_PP(cmd), "rt"))==NULL) {
#else
if ((in=VCWD_POPEN(Z_STRVAL_PP(cmd), "r"))==NULL) {
#endif
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to execute '%s'", Z_STRVAL_PP(cmd));
RETURN_FALSE;
}
stream = php_stream_fopen_from_pipe(in, "rb");
total_readbytes = php_stream_copy_to_mem(stream, &ret, PHP_STREAM_COPY_ALL, 0);
php_stream_close(stream);
printf("TEST\n%s\n",ret);
if (total_readbytes > 0) {
RETURN_STRINGL(ret, total_readbytes, 0);
} else {
RETURN_NULL();
}
}
result
# php test.php
TEST
test.php
[1] Segmentation fault
I conclude
- the exec of ls is working fine
- the result of the ls is ok
Still something gets wrong.
Question 1:
Can someone point me to the code section where the PHP_FUNCTION(shell_exec) is executed, so i can try to see some more details of where it goes wrong
Question 2:
Any hints on what might be wrong.
Version info
Android 2.3.1
Android NDK 6
PHP 5.2.17
回答1:
Thanx to the anonymous who send me an email with some usefull tips.
As it turns out php uses popen for al its exec related code. A some might know androids bionic version of popen was quite buggy up until ICS and it is know the corrupt the stack.
I modified php to use a modified verion of popen Now php exec/shell_exec etc all run without any problems
来源:https://stackoverflow.com/questions/9922517/all-exec-related-php-functions-segfault