@@ -534,9 +534,19 @@ static void cli_register_file_handles(bool no_close) /* {{{ */
534
534
php_stream_context * sc_in = NULL , * sc_out = NULL , * sc_err = NULL ;
535
535
zend_constant ic , oc , ec ;
536
536
537
- s_in = php_stream_open_wrapper_ex ("php://stdin" , "rb" , 0 , NULL , sc_in );
538
- s_out = php_stream_open_wrapper_ex ("php://stdout" , "wb" , 0 , NULL , sc_out );
539
- s_err = php_stream_open_wrapper_ex ("php://stderr" , "wb" , 0 , NULL , sc_err );
537
+ s_in = php_stream_open_wrapper_ex ("php://stdin" , "rb" , options , NULL , sc_in );
538
+ s_out = php_stream_open_wrapper_ex ("php://stdout" , "wb" , options , NULL , sc_out );
539
+ s_err = php_stream_open_wrapper_ex ("php://stderr" , "wb" , options , NULL , sc_err );
540
+
541
+ /* Release stream resources, but don't free the underlying handles. Othewrise,
542
+ * extensions which write to stderr or company during mshutdown/gshutdown
543
+ * won't have the expected functionality.
544
+ */
545
+ if (no_close ) {
546
+ s_in -> flags |= PHP_STREAM_FLAG_NO_CLOSE ;
547
+ s_out -> flags |= PHP_STREAM_FLAG_NO_CLOSE ;
548
+ s_err -> flags |= PHP_STREAM_FLAG_NO_CLOSE ;
549
+ }
540
550
541
551
if (s_in == NULL || s_out == NULL || s_err == NULL ) {
542
552
if (s_in ) php_stream_close (s_in );
@@ -545,12 +555,6 @@ static void cli_register_file_handles(bool no_close) /* {{{ */
545
555
return ;
546
556
}
547
557
548
- if (no_close ) {
549
- s_in -> flags |= PHP_STREAM_FLAG_NO_CLOSE ;
550
- s_out -> flags |= PHP_STREAM_FLAG_NO_CLOSE ;
551
- s_err -> flags |= PHP_STREAM_FLAG_NO_CLOSE ;
552
- }
553
-
554
558
s_in_process = s_in ;
555
559
556
560
php_stream_to_zval (s_in , & ic .value );
@@ -956,7 +960,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
956
960
switch (behavior ) {
957
961
case PHP_MODE_STANDARD :
958
962
if (script_file ) {
959
- cli_register_file_handles (/* no_close */ PHP_DEBUG || num_repeats > 1 );
963
+ cli_register_file_handles (/* no_close */ true );
960
964
}
961
965
962
966
if (interactive ) {
@@ -991,7 +995,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
991
995
}
992
996
break ;
993
997
case PHP_MODE_CLI_DIRECT :
994
- cli_register_file_handles (/* no_close */ PHP_DEBUG || num_repeats > 1 );
998
+ cli_register_file_handles (/* no_close */ true );
995
999
zend_eval_string_ex (exec_direct , NULL , "Command line code" , 1 );
996
1000
break ;
997
1001
@@ -1006,7 +1010,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
1006
1010
file_handle .filename = NULL ;
1007
1011
}
1008
1012
1009
- cli_register_file_handles (/* no_close */ PHP_DEBUG || num_repeats > 1 );
1013
+ cli_register_file_handles (/* no_close */ true );
1010
1014
1011
1015
if (exec_begin ) {
1012
1016
zend_eval_string_ex (exec_begin , NULL , "Command line begin code" , 1 );
0 commit comments