From: Sascha Schumann Date: Sat, 19 May 2001 14:30:33 +0000 (+0000) Subject: Use write(2), if we can successfully write to stdout. X-Git-Tag: PRE_GRANULAR_GARBAGE_FIX~347 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bc7a582f8480ce92fecde267d4cd3c3f628a4ba9;p=php Use write(2), if we can successfully write to stdout. --- diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c index dc1cfe9191..cf482d0a67 100644 --- a/sapi/cgi/cgi_main.c +++ b/sapi/cgi/cgi_main.c @@ -87,6 +87,26 @@ static int _print_module_info ( zend_module_entry *module, void *arg ) { return 0; } +#ifndef STDOUT_FILENO +#define STDOUT_FILENO 1 +#endif + +static inline size_t sapi_cgibin_single_write(const char *str, uint str_length) +{ +#ifdef PHP_WRITE_STDOUT + long ret; + + ret = write(STDOUT_FILENO, str, str_length); + if (ret <= 0) return 0; + return ret; +#else + size_t ret; + + ret = fwrite(str, 1, MIN(str_length, 16384), stdout); + return ret; +#endif +} + static int sapi_cgibin_ub_write(const char *str, uint str_length) { const char *ptr = str; @@ -95,7 +115,7 @@ static int sapi_cgibin_ub_write(const char *str, uint str_length) while (remaining > 0) { - ret = fwrite(ptr, 1, MIN(remaining, 16384), stdout); + ret = sapi_cgibin_single_write(ptr, remaining); if (!ret) { php_handle_aborted_connection(); } diff --git a/sapi/cgi/config.m4 b/sapi/cgi/config.m4 index 9c1ea3acc5..7a0007e572 100644 --- a/sapi/cgi/config.m4 +++ b/sapi/cgi/config.m4 @@ -1,6 +1,36 @@ +AC_DEFUN(PHP_TEST_WRITE_STDOUT,[ + AC_CACHE_CHECK(whether writing to stdout works,ac_cv_write_stdout,[ + AC_TRY_RUN([ +#ifdef HAVE_UNISTD_H +#include +#endif + +#define TEXT "This is the test message -- " + +main() +{ + int n; + + n = write(1, TEXT, sizeof(TEXT)-1); + return (!(n == sizeof(TEXT)-1)); +} + ],[ + ac_cv_write_stdout=yes + ],[ + ac_cv_write_stdout=no + ],[ + ac_cv_write_stdout=no + ]) + ]) + if test "$ac_cv_write_stdout" = "yes"; then + AC_DEFINE(PHP_WRITE_STDOUT, 1, [whether write(2) works]) + fi +]) if test "$PHP_SAPI" = "cgi"; then + PHP_TEST_WRITE_STDOUT + PHP_ARG_ENABLE(force-cgi-redirect,whether to force Apache CGI redirect, [ --enable-force-cgi-redirect Enable the security check for internal server