-
Notifications
You must be signed in to change notification settings - Fork 395
simply use the built-in php-cli #1757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
withinboredom
wants to merge
7
commits into
main
Choose a base branch
from
refactor/php-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8045011
simply use the built-in php-cli
withinboredom 44408b6
refactor code around calling do_php_cli
henderkes 2b75b00
move old cli sapi emulation out to emulate_php_cli.c
henderkes a8762a0
EOF newlines
henderkes dce590c
shuffle things to work
henderkes e335244
fix 80500
henderkes d1bb029
Merge pull request #1782 from static-php/cli_sapi
henderkes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
#include <SAPI.h> | ||
#include <Zend/zend_alloc.h> | ||
#include <Zend/zend_exceptions.h> | ||
#include <Zend/zend_interfaces.h> | ||
#include <Zend/zend_types.h> | ||
#include <errno.h> | ||
#include <ext/spl/spl_exceptions.h> | ||
#include <ext/standard/head.h> | ||
#include <inttypes.h> | ||
#include <php.h> | ||
#include <php_config.h> | ||
#include <php_ini.h> | ||
#include <php_main.h> | ||
#include <php_output.h> | ||
#include <php_variables.h> | ||
#include <php_version.h> | ||
#include <pthread.h> | ||
#include <sapi/embed/php_embed.h> | ||
#include <signal.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#if defined(__linux__) | ||
#include <sys/prctl.h> | ||
#elif defined(__FreeBSD__) || defined(__OpenBSD__) | ||
#include <pthread_np.h> | ||
#endif | ||
|
||
typedef struct { | ||
char *script; | ||
int argc; | ||
char **argv; | ||
bool eval; | ||
} cli_exec_args_t; | ||
cli_exec_args_t *cli_args; | ||
|
||
/* Function declaration to avoid implicit declaration error */ | ||
void register_server_variable_filtered(const char *key, char **val, size_t *val_len, zval *track_vars_array); | ||
|
||
/* | ||
* CLI code is adapted from | ||
* https://github.com/php/php-src/blob/master/sapi/cli/php_cli.c Copyright (c) | ||
* The PHP Group Licensed under The PHP License Original uthors: Edin Kadribasic | ||
* <[email protected]>, Marcus Boerger <[email protected]> and Johannes Schlueter | ||
* <[email protected]> Parts based on CGI SAPI Module by Rasmus Lerdorf, Stig | ||
* Bakken and Zeev Suraski | ||
*/ | ||
static void cli_register_file_handles(bool no_close) /* {{{ */ | ||
{ | ||
php_stream *s_in, *s_out, *s_err; | ||
php_stream_context *sc_in = NULL, *sc_out = NULL, *sc_err = NULL; | ||
zend_constant ic, oc, ec; | ||
|
||
s_in = php_stream_open_wrapper_ex("php://stdin", "rb", 0, NULL, sc_in); | ||
s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out); | ||
s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err); | ||
|
||
if (s_in == NULL || s_out == NULL || s_err == NULL) { | ||
if (s_in) | ||
php_stream_close(s_in); | ||
if (s_out) | ||
php_stream_close(s_out); | ||
if (s_err) | ||
php_stream_close(s_err); | ||
return; | ||
} | ||
|
||
if (no_close) { | ||
s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE; | ||
s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE; | ||
s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE; | ||
} | ||
|
||
/*s_in_process = s_in;*/ | ||
|
||
php_stream_to_zval(s_in, &ic.value); | ||
php_stream_to_zval(s_out, &oc.value); | ||
php_stream_to_zval(s_err, &ec.value); | ||
|
||
ZEND_CONSTANT_SET_FLAGS(&ic, CONST_CS, 0); | ||
ic.name = zend_string_init_interned("STDIN", sizeof("STDIN") - 1, 0); | ||
zend_register_constant(&ic); | ||
|
||
ZEND_CONSTANT_SET_FLAGS(&oc, CONST_CS, 0); | ||
oc.name = zend_string_init_interned("STDOUT", sizeof("STDOUT") - 1, 0); | ||
zend_register_constant(&oc); | ||
|
||
ZEND_CONSTANT_SET_FLAGS(&ec, CONST_CS, 0); | ||
ec.name = zend_string_init_interned("STDERR", sizeof("STDERR") - 1, 0); | ||
zend_register_constant(&ec); | ||
} | ||
/* }}} */ | ||
|
||
static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */ | ||
{ | ||
size_t len = strlen(cli_args->script); | ||
char *docroot = ""; | ||
|
||
/* | ||
* In CGI mode, we consider the environment to be a part of the server | ||
* variables | ||
*/ | ||
php_import_environment_variables(track_vars_array); | ||
|
||
/* Build the special-case PHP_SELF variable for the CLI version */ | ||
register_server_variable_filtered("PHP_SELF", &cli_args->script, &len, | ||
track_vars_array); | ||
register_server_variable_filtered("SCRIPT_NAME", &cli_args->script, &len, | ||
track_vars_array); | ||
|
||
/* filenames are empty for stdin */ | ||
register_server_variable_filtered("SCRIPT_FILENAME", &cli_args->script, &len, | ||
track_vars_array); | ||
register_server_variable_filtered("PATH_TRANSLATED", &cli_args->script, &len, | ||
track_vars_array); | ||
|
||
/* just make it available */ | ||
len = 0U; | ||
register_server_variable_filtered("DOCUMENT_ROOT", &docroot, &len, | ||
track_vars_array); | ||
} | ||
/* }}} */ | ||
|
||
void *emulate_script_cli(void *arg) { | ||
void *exit_status; | ||
cli_exec_args_t* args = arg; | ||
cli_args = args; | ||
bool eval = args->eval; | ||
|
||
/* | ||
* The SAPI name "cli" is hardcoded into too many programs... let's usurp it. | ||
*/ | ||
php_embed_module.name = "cli"; | ||
php_embed_module.pretty_name = "PHP CLI embedded in FrankenPHP"; | ||
php_embed_module.register_server_variables = sapi_cli_register_variables; | ||
|
||
php_embed_init(cli_args->argc, cli_args->argv); | ||
|
||
cli_register_file_handles(false); | ||
zend_first_try { | ||
if (eval) { | ||
/* evaluate the cli_args->script as literal PHP code (php-cli -r "...") */ | ||
zend_eval_string_ex(cli_args->script, NULL, "Command line code", 1); | ||
} else { | ||
zend_file_handle file_handle; | ||
zend_stream_init_filename(&file_handle, cli_args->script); | ||
|
||
CG(skip_shebang) = 1; | ||
php_execute_script(&file_handle); | ||
} | ||
} | ||
zend_end_try(); | ||
|
||
exit_status = (void *)(intptr_t)EG(exit_status); | ||
|
||
php_embed_shutdown(); | ||
|
||
return exit_status; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
void *emulate_script_cli(void *arg); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
#include <php_main.h> | ||
#include <php_output.h> | ||
#include <php_variables.h> | ||
#include <php_version.h> | ||
#include <pthread.h> | ||
#include <sapi/embed/php_embed.h> | ||
#include <signal.h> | ||
|
@@ -26,6 +27,12 @@ | |
#include <pthread_np.h> | ||
#endif | ||
|
||
#if PHP_VERSION_ID >= 80500 | ||
#include <sapi/cli/cli.h> | ||
#else | ||
#include "emulate_php_cli.h" | ||
#endif | ||
|
||
#include "_cgo_export.h" | ||
#include "frankenphp_arginfo.h" | ||
|
||
|
@@ -748,7 +755,7 @@ void frankenphp_register_variable_safe(char *key, char *val, size_t val_len, | |
} | ||
} | ||
|
||
static inline void register_server_variable_filtered(const char *key, | ||
void register_server_variable_filtered(const char *key, | ||
char **val, | ||
size_t *val_len, | ||
zval *track_vars_array) { | ||
|
@@ -1015,128 +1022,23 @@ int frankenphp_execute_script(char *file_name) { | |
return status; | ||
} | ||
|
||
/* Use global variables to store CLI arguments to prevent useless allocations */ | ||
static char *cli_script; | ||
static int cli_argc; | ||
static char **cli_argv; | ||
|
||
/* | ||
* CLI code is adapted from | ||
* https://github.com/php/php-src/blob/master/sapi/cli/php_cli.c Copyright (c) | ||
* The PHP Group Licensed under The PHP License Original uthors: Edin Kadribasic | ||
* <[email protected]>, Marcus Boerger <[email protected]> and Johannes Schlueter | ||
* <[email protected]> Parts based on CGI SAPI Module by Rasmus Lerdorf, Stig | ||
* Bakken and Zeev Suraski | ||
*/ | ||
static void cli_register_file_handles(bool no_close) /* {{{ */ | ||
{ | ||
php_stream *s_in, *s_out, *s_err; | ||
php_stream_context *sc_in = NULL, *sc_out = NULL, *sc_err = NULL; | ||
zend_constant ic, oc, ec; | ||
|
||
s_in = php_stream_open_wrapper_ex("php://stdin", "rb", 0, NULL, sc_in); | ||
s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out); | ||
s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err); | ||
|
||
if (s_in == NULL || s_out == NULL || s_err == NULL) { | ||
if (s_in) | ||
php_stream_close(s_in); | ||
if (s_out) | ||
php_stream_close(s_out); | ||
if (s_err) | ||
php_stream_close(s_err); | ||
return; | ||
} | ||
|
||
if (no_close) { | ||
s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE; | ||
s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE; | ||
s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE; | ||
} | ||
|
||
/*s_in_process = s_in;*/ | ||
|
||
php_stream_to_zval(s_in, &ic.value); | ||
php_stream_to_zval(s_out, &oc.value); | ||
php_stream_to_zval(s_err, &ec.value); | ||
|
||
ZEND_CONSTANT_SET_FLAGS(&ic, CONST_CS, 0); | ||
ic.name = zend_string_init_interned("STDIN", sizeof("STDIN") - 1, 0); | ||
zend_register_constant(&ic); | ||
|
||
ZEND_CONSTANT_SET_FLAGS(&oc, CONST_CS, 0); | ||
oc.name = zend_string_init_interned("STDOUT", sizeof("STDOUT") - 1, 0); | ||
zend_register_constant(&oc); | ||
|
||
ZEND_CONSTANT_SET_FLAGS(&ec, CONST_CS, 0); | ||
ec.name = zend_string_init_interned("STDERR", sizeof("STDERR") - 1, 0); | ||
zend_register_constant(&ec); | ||
} | ||
/* }}} */ | ||
|
||
static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */ | ||
{ | ||
size_t len = strlen(cli_script); | ||
char *docroot = ""; | ||
|
||
/* | ||
* In CGI mode, we consider the environment to be a part of the server | ||
* variables | ||
*/ | ||
php_import_environment_variables(track_vars_array); | ||
|
||
/* Build the special-case PHP_SELF variable for the CLI version */ | ||
register_server_variable_filtered("PHP_SELF", &cli_script, &len, | ||
track_vars_array); | ||
register_server_variable_filtered("SCRIPT_NAME", &cli_script, &len, | ||
track_vars_array); | ||
|
||
/* filenames are empty for stdin */ | ||
register_server_variable_filtered("SCRIPT_FILENAME", &cli_script, &len, | ||
track_vars_array); | ||
register_server_variable_filtered("PATH_TRANSLATED", &cli_script, &len, | ||
track_vars_array); | ||
|
||
/* just make it available */ | ||
len = 0U; | ||
register_server_variable_filtered("DOCUMENT_ROOT", &docroot, &len, | ||
track_vars_array); | ||
} | ||
/* }}} */ | ||
typedef struct { | ||
char *script; | ||
int argc; | ||
char **argv; | ||
bool eval; | ||
} cli_exec_args_t; | ||
|
||
static void *execute_script_cli(void *arg) { | ||
void *exit_status; | ||
bool eval = (bool)arg; | ||
cli_exec_args_t *args = (cli_exec_args_t *)arg; | ||
volatile int v = PHP_VERSION_ID; | ||
(void)v; | ||
|
||
/* | ||
* The SAPI name "cli" is hardcoded into too many programs... let's usurp it. | ||
*/ | ||
php_embed_module.name = "cli"; | ||
php_embed_module.pretty_name = "PHP CLI embedded in FrankenPHP"; | ||
php_embed_module.register_server_variables = sapi_cli_register_variables; | ||
|
||
php_embed_init(cli_argc, cli_argv); | ||
|
||
cli_register_file_handles(false); | ||
zend_first_try { | ||
if (eval) { | ||
/* evaluate the cli_script as literal PHP code (php-cli -r "...") */ | ||
zend_eval_string_ex(cli_script, NULL, "Command line code", 1); | ||
} else { | ||
zend_file_handle file_handle; | ||
zend_stream_init_filename(&file_handle, cli_script); | ||
|
||
CG(skip_shebang) = 1; | ||
php_execute_script(&file_handle); | ||
} | ||
} | ||
zend_end_try(); | ||
|
||
exit_status = (void *)(intptr_t)EG(exit_status); | ||
|
||
php_embed_shutdown(); | ||
|
||
return exit_status; | ||
#if PHP_VERSION_ID >= 80500 | ||
return (void *)(intptr_t)do_php_cli(args->argc, args->argv); | ||
#else | ||
return (void *)(intptr_t)emulate_script_cli(args); | ||
#endif | ||
} | ||
|
||
int frankenphp_execute_script_cli(char *script, int argc, char **argv, | ||
|
@@ -1145,15 +1047,15 @@ int frankenphp_execute_script_cli(char *script, int argc, char **argv, | |
int err; | ||
void *exit_status; | ||
|
||
cli_script = script; | ||
cli_argc = argc; | ||
cli_argv = argv; | ||
cli_exec_args_t args = { | ||
.script = script, .argc = argc, .argv = argv, .eval = eval | ||
}; | ||
|
||
/* | ||
* Start the script in a dedicated thread to prevent conflicts between Go and | ||
* PHP signal handlers | ||
*/ | ||
err = pthread_create(&thread, NULL, execute_script_cli, (void *)eval); | ||
err = pthread_create(&thread, NULL, execute_script_cli, &args); | ||
if (err != 0) { | ||
return err; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.