Allow to not close stream on rscr dtor in php cli sapi

This commit is contained in:
Jakub Zelenka 2022-06-20 14:14:29 +01:00
parent 55a88f36b6
commit 0a4a55fd44
No known key found for this signature in database
GPG key ID: 1C0779DC5C0A9DE4
9 changed files with 26 additions and 34 deletions

View file

@ -526,7 +526,7 @@ static void php_cli_usage(char *argv0)
static php_stream *s_in_process = NULL;
static void cli_register_file_handles(bool no_close) /* {{{ */
static void cli_register_file_handles(void)
{
php_stream *s_in, *s_out, *s_err;
php_stream_context *sc_in=NULL, *sc_out=NULL, *sc_err=NULL;
@ -536,6 +536,14 @@ static void cli_register_file_handles(bool no_close) /* {{{ */
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);
/* Release stream resources, but don't free the underlying handles. Othewrise,
* extensions which write to stderr or company during mshutdown/gshutdown
* won't have the expected functionality.
*/
if (s_in) s_in->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE;
if (s_out) s_out->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE;
if (s_err) s_err->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE;
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);
@ -543,12 +551,6 @@ static void cli_register_file_handles(bool no_close) /* {{{ */
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);
@ -567,7 +569,6 @@ static void cli_register_file_handles(bool no_close) /* {{{ */
ec.name = zend_string_init_interned("STDERR", sizeof("STDERR")-1, 0);
zend_register_constant(&ec);
}
/* }}} */
static const char *param_mode_conflict = "Either execute direct code, process stdin or use a file.\n";
@ -954,7 +955,7 @@ do_repeat:
switch (behavior) {
case PHP_MODE_STANDARD:
if (script_file) {
cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
cli_register_file_handles();
}
if (interactive) {
@ -990,7 +991,7 @@ do_repeat:
}
break;
case PHP_MODE_CLI_DIRECT:
cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
cli_register_file_handles();
zend_eval_string_ex(exec_direct, NULL, "Command line code", 1);
break;
@ -1005,7 +1006,7 @@ do_repeat:
file_handle.filename = NULL;
}
cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
cli_register_file_handles();
if (exec_begin) {
zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1);