以apache2 的mod_php5模組為例,通過php_ap2_register_hook()函數來註冊鉤子,pre_config,post_config,child_init是啟動掛鈎,它們在伺服器啟動時調用,handler 是請求鉤子,掛鈎到apache 的一次請求。很自然的,要在apache啟動和請求的時候,分別完成不同的工作。
void php_ap2_register_hook(apr_pool_t *p){ap_hook_pre_config(php_pre_config, NULL, NULL, APR_HOOK_MIDDLE);ap_hook_post_config(php_apache_server_startup, NULL, NULL, APR_HOOK_MIDDLE);ap_hook_handler(php_handler, NULL, NULL, APR_HOOK_MIDDLE);ap_hook_child_init(php_apache_child_init, NULL, NULL, APR_HOOK_MIDDLE);}
在php_apache_server_startup中
static intphp_apache_server_startup(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s){void *data = NULL;const char *userdata_key = "apache2hook_post_config";/* Apache will load, unload and then reload a DSO module. This * prevents us from starting PHP until the second load. */apr_pool_userdata_get(&data, userdata_key, s->process->pool);if (data == NULL) {/* We must use set() here and *not* setn(), otherwise the * static string pointed to by userdata_key will be mapped * to a different location when the DSO is reloaded and the * pointers won't match, causing get() to return NULL when * we expected it to return non-NULL. */apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool);return OK;}/* Set up our overridden path. */if (apache2_php_ini_path_override) {apache2_sapi_module.php_ini_path_override = apache2_php_ini_path_override;}#ifdef ZTStsrm_startup(1, 1, 0, NULL);#endifsapi_startup(&apache2_sapi_module);//完成sapi的啟動,初始化全域變數等apache2_sapi_module.startup(&apache2_sapi_module);//通過調用_sapi_module_struct的startup函數,註冊apache sapi module 一系列函數apr_pool_cleanup_register(pconf, NULL, php_apache_server_shutdown, apr_pool_cleanup_null);php_apache_add_version(pconf);return OK;}
隨後又會調用php_module_startup來啟動作為apache一個模組的PHP,初始化一些PHP的狀態,函數等等。這篇文章要用到的代碼為:
int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_modules, uint num_additional_modules){zend_utility_functions zuf;。。。//省略部分代碼 php_output_startup();//初始化PHP的輸出函數zuf.error_function = php_error_cb;zuf.printf_function = php_printf;zuf.write_function = php_body_write_wrapper;//PHP的預設輸出函數zuf.fopen_function = php_fopen_wrapper_for_zend;zuf.message_handler = php_message_handler_for_zend;zuf.block_interruptions = sapi_module.block_interruptions;zuf.unblock_interruptions = sapi_module.unblock_interruptions;zuf.get_configuration_directive = php_get_configuration_directive_for_zend;zuf.ticks_function = php_run_ticks;zuf.on_timeout = php_on_timeout;zuf.stream_open_function = php_stream_open_for_zend;zuf.vspprintf_function = vspprintf;zuf.getenv_function = sapi_getenv;zuf.resolve_path_function = php_resolve_path_for_zend;zend_startup(&zuf, NULL TSRMLS_CC);。。。//省略部分代碼}
PHPAPI void php_output_startup(void)->static void php_output_init_globals(php_output_globals *output_globals_p TSRMLS_DC)
此時PHP的預設一系列輸出函數為:
static void php_output_init_globals(php_output_globals *output_globals_p TSRMLS_DC){OG(php_body_write) = php_default_output_func;OG(php_header_write) = php_default_output_func;OG(implicit_flush) = 0;OG(output_start_filename) = NULL;OG(output_start_lineno) = 0;}
/* {{{ php_default_output_func */PHPAPI int php_default_output_func(const char *str, uint str_len TSRMLS_DC){fwrite(str, 1, str_len, stderr);//預設輸出到標準錯誤/* See http://support.microsoft.com/kb/190351 */#ifdef PHP_WIN32fflush(stderr);#endifreturn str_len;}
上述情況是發生在apache啟動的時候,初始化PHP的過程,在每一次請求過程中,顯然輸出函數不對。跟蹤函數php_handler->php_apache_request_ctor->php_request_startup-> php_output_activate,在這個函數中:
PHPAPI void php_output_activate(TSRMLS_D){OG(php_body_write) = php_ub_body_write;//輸出函數OG(php_header_write) = sapi_module.ub_write;OG(ob_nesting_level) = 0;OG(ob_lock) = 0;OG(disable_output) = 0;OG(output_start_filename) = NULL;OG(output_start_lineno) = 0;}PHPAPI int php_ub_body_write(const char *str, uint str_length TSRMLS_DC){int result = 0;if (SG(request_info).headers_only) {if(SG(headers_sent)) {return 0;}php_header(TSRMLS_C);zend_bailout();}if (php_header(TSRMLS_C)) {if (zend_is_compiling(TSRMLS_C)) {OG(output_start_filename) = zend_get_compiled_filename(TSRMLS_C);OG(output_start_lineno) = zend_get_compiled_lineno(TSRMLS_C);} else if (zend_is_executing(TSRMLS_C)) {OG(output_start_filename) = zend_get_executed_filename(TSRMLS_C);OG(output_start_lineno) = zend_get_executed_lineno(TSRMLS_C);}OG(php_body_write) = php_ub_body_write_no_header;//輸出函數重新賦值result = php_ub_body_write_no_header(str, str_length TSRMLS_CC);}return result;}PHPAPI int php_ub_body_write_no_header(const char *str, uint str_length TSRMLS_DC){int result;if (OG(disable_output)) {return 0;}result = OG(php_header_write)(str, str_length TSRMLS_CC);//實際上調用的是OG(php_header_write)也就是sapi_module.ub_write為apache的函數if (OG(implicit_flush)) {sapi_flush(TSRMLS_C);}return result;}
這樣一來結果就瞭然了,在PHP啟動的時候,輸出函數指向的fd是stderr,在每一次請求的時候,再將函數指標指向apache的輸出函數。
在zend_startup的時候會有:
ZEND_API zend_write_func_t zend_write;zend_write = (zend_write_func_t) utility_functions->write_function;
在實際使用的時候,以echo為例,最終會調用:
ZEND_API int zend_print_zval(zval *expr, int indent) /* {{{ */{return zend_print_zval_ex(zend_write, expr, indent);//zend_write為實際調用的指標函數}
想了一下PHP在請求的時候,可以動態指定輸出函數,這樣更加方便SAPI層的介面編寫,這個輸出函數的控制權力在server端才合理,但是PHP在自身上麵包裝了很多層,不知道是不是命名和年代久遠的關係。