From afc5738154b8e0e7f8bcb5d6a521514bb495a0c0 Mon Sep 17 00:00:00 2001 From: Calvin Buckley Date: Mon, 12 Aug 2024 23:24:41 -0300 Subject: [PATCH] Show build provider and unify version information printing (#14657) * Show build provider information in "php -v" Vendors such as distributions can set the `PHP_BUILD_PROVIDER` variable, that gets printed in phpinfo. However, I find that users check `php -v` more often than phpinfo to see what PHP they're running. The problem with this is that it does not show that build provider information. This change makes the build provider information printed on an additional line of the version information. * Put on same line so it works with or without env var Unbreaks build without PHP_BUILD_PROVIDER set. * change wording in provider version text better grammatically; many different possibilities here though * Unify SAPI version printing This makes it so that all of the SAPIs share the same code for printing version information. This is useful in case of any future changes to the version information, such as i.e. adding build provider to the output. * Make include for php_print_version explicit * Preserve phpdbg version and output channel php_printf doesn't have same semantics, as phpdbg_out could be on a different output than stdout/err. Also add the phpdbg version (in case it differs from PHP's, to keep similar output before this PR) * remove size variables we don't use them and CI doesn't like unused variables * Fix format string insecurity --- main/main.c | 41 +++++++++++++++++++++++++++++++++++++ main/php_main.h | 6 ++++++ sapi/cgi/cgi_main.c | 6 +----- sapi/cgi/tests/001.phpt | 2 +- sapi/cli/php_cli.c | 23 +-------------------- sapi/cli/tests/001.phpt | 2 +- sapi/fpm/fpm/fpm_main.c | 6 +----- sapi/litespeed/lsapi_main.c | 6 +----- sapi/phpdbg/phpdbg.c | 17 +++++++-------- 9 files changed, 62 insertions(+), 47 deletions(-) diff --git a/main/main.c b/main/main.c index f93ae526c8a..15a9f9a59b2 100644 --- a/main/main.c +++ b/main/main.c @@ -108,6 +108,47 @@ PHPAPI unsigned int php_version_id(void) return PHP_VERSION_ID; } +PHPAPI char *php_get_version(sapi_module_struct *sapi_module) +{ + char *version_info; + spprintf(&version_info, 0, "PHP %s (%s) (built: %s %s) (%s)\nCopyright (c) The PHP Group\n%s%s", + PHP_VERSION, sapi_module->name, __DATE__, __TIME__, +#ifdef ZTS + "ZTS" +#else + "NTS" +#endif +#ifdef PHP_BUILD_COMPILER + " " PHP_BUILD_COMPILER +#endif +#ifdef PHP_BUILD_ARCH + " " PHP_BUILD_ARCH +#endif +#if ZEND_DEBUG + " DEBUG" +#endif +#ifdef HAVE_GCOV + " GCOV" +#endif + , +#ifdef PHP_BUILD_PROVIDER + "Built by " PHP_BUILD_PROVIDER "\n" +#else + "" +#endif + , + get_zend_version() + ); + return version_info; +} + +PHPAPI void php_print_version(sapi_module_struct *sapi_module) +{ + char *version_info = php_get_version(sapi_module); + php_printf("%s", version_info); + efree(version_info); +} + /* {{{ PHP_INI_MH */ static PHP_INI_MH(OnSetFacility) { diff --git a/main/php_main.h b/main/php_main.h index 1fb460f07f2..889fcf48501 100644 --- a/main/php_main.h +++ b/main/php_main.h @@ -36,6 +36,12 @@ PHPAPI const char *php_version(void); */ PHPAPI unsigned int php_version_id(void); +/* Prints the PHP version string for the -v option. It's in main/ so that + * it can be shared between SAPIs. + */ +PHPAPI char *php_get_version(sapi_module_struct *sapi_module); +PHPAPI void php_print_version(sapi_module_struct *sapi_module); + PHPAPI zend_result php_request_startup(void); PHPAPI void php_request_shutdown(void *dummy); PHPAPI zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_module); diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c index 4137489305b..4096b8d665c 100644 --- a/sapi/cgi/cgi_main.c +++ b/sapi/cgi/cgi_main.c @@ -2384,11 +2384,7 @@ parent_loop_end: } SG(headers_sent) = 1; SG(request_info).no_headers = 1; -#if ZEND_DEBUG - php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#else - php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#endif + php_print_version(&cgi_sapi_module); php_request_shutdown((void *) 0); fcgi_shutdown(); exit_status = 0; diff --git a/sapi/cgi/tests/001.phpt b/sapi/cgi/tests/001.phpt index 948f4ce3047..c4b539aa83b 100644 --- a/sapi/cgi/tests/001.phpt +++ b/sapi/cgi/tests/001.phpt @@ -17,6 +17,6 @@ echo "Done\n"; --EXPECTF-- string(%d) "PHP %s (cgi%s (built: %s Copyright (c) The PHP Group -Zend Engine v%s, Copyright (c) Zend Technologies +%AZend Engine v%s, Copyright (c) Zend Technologies " Done diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index ae7dbfb51fe..840cdae3780 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -625,28 +625,7 @@ static int do_cli(int argc, char **argv) /* {{{ */ goto out; case 'v': /* show php version & quit */ - php_printf("PHP %s (%s) (built: %s %s) (%s)\nCopyright (c) The PHP Group\n%s", - PHP_VERSION, cli_sapi_module.name, __DATE__, __TIME__, -#ifdef ZTS - "ZTS" -#else - "NTS" -#endif -#ifdef PHP_BUILD_COMPILER - " " PHP_BUILD_COMPILER -#endif -#ifdef PHP_BUILD_ARCH - " " PHP_BUILD_ARCH -#endif -#if ZEND_DEBUG - " DEBUG" -#endif -#ifdef HAVE_GCOV - " GCOV" -#endif - , - get_zend_version() - ); + php_print_version(&cli_sapi_module); sapi_deactivate(); goto out; diff --git a/sapi/cli/tests/001.phpt b/sapi/cli/tests/001.phpt index aced33623ef..e13ab8def30 100644 --- a/sapi/cli/tests/001.phpt +++ b/sapi/cli/tests/001.phpt @@ -14,6 +14,6 @@ echo "Done\n"; --EXPECTF-- string(%d) "PHP %s (cli) (built: %s)%s Copyright (c) The PHP Group -Zend Engine v%s, Copyright (c) Zend Technologies +%AZend Engine v%s, Copyright (c) Zend Technologies " Done diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c index ae38b213e98..d138738b279 100644 --- a/sapi/fpm/fpm/fpm_main.c +++ b/sapi/fpm/fpm/fpm_main.c @@ -1705,11 +1705,7 @@ int main(int argc, char *argv[]) SG(headers_sent) = 1; SG(request_info).no_headers = 1; -#if ZEND_DEBUG - php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#else - php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#endif + php_print_version(&sapi_module); php_request_shutdown((void *) 0); fcgi_shutdown(); exit_status = FPM_EXIT_OK; diff --git a/sapi/litespeed/lsapi_main.c b/sapi/litespeed/lsapi_main.c index d0a81c7cb6c..259d4a985cc 100644 --- a/sapi/litespeed/lsapi_main.c +++ b/sapi/litespeed/lsapi_main.c @@ -1272,11 +1272,7 @@ static int cli_main( int argc, char * argv[] ) break; case 'v': if (php_request_startup() != FAILURE) { -#if ZEND_DEBUG - php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#else - php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#endif + php_print_version(&sapi_module); #ifdef PHP_OUTPUT_NEWAPI php_output_end_all(); #else diff --git a/sapi/phpdbg/phpdbg.c b/sapi/phpdbg/phpdbg.c index d09ededc1d6..76b3c7f7259 100644 --- a/sapi/phpdbg/phpdbg.c +++ b/sapi/phpdbg/phpdbg.c @@ -30,6 +30,7 @@ #include "phpdbg_arginfo.h" #include "zend_vm.h" #include "php_ini_builder.h" +#include "php_main.h" #include "ext/standard/basic_functions.h" @@ -1369,14 +1370,14 @@ phpdbg_main: if (show_help) { phpdbg_do_help_cmd(exec); } else if (show_version) { - phpdbg_out( - "phpdbg %s (built: %s %s)\nPHP %s, Copyright (c) The PHP Group\n%s", - PHPDBG_VERSION, - __DATE__, - __TIME__, - PHP_VERSION, - get_zend_version() - ); + char *version_info = php_get_version(&phpdbg_sapi_module); + /* we also want to include phpdbg version */ + char *prepended_version_info; + spprintf(&prepended_version_info, 0, + "phpdbg %s, %s", PHPDBG_VERSION, version_info); + phpdbg_out("%s", prepended_version_info); + efree(prepended_version_info); + efree(version_info); } PHPDBG_G(flags) |= PHPDBG_IS_QUITTING; php_module_shutdown();