首先,了解一下每個Apache模塊都會輸出一個模塊數據結構,其定義結構如下(以PHP模塊為例):
- AP_MODULE_DECLARE_DATA module php5_module = {
- STANDARD20_MODULE_STUFF,
- create_php_config, /* create per-directory config structure */
- merge_php_config, /* merge per-directory config structures */
- NULL, /* create per-server config structure */
- NULL, /* merge per-server config structures */
- php_dir_cmds, /* command apr_table_t */
- php_ap2_register_hook /* register hooks */
- };
STANDARD20_MODULE_STUFF這個宏是必須的,這個宏在經過擴展以後,可以為編譯後的模塊載入服務器構建提供版本信息,在PHP的模塊中,create_php_config函數創建目錄配置結構,,merge_php_config合並目錄配置結構,php_dir_cmds函數為模塊配置相關指令,php_ap2_register_hook 注冊模塊的鉤子程序,對這個結構體會在apache的模塊中專門說明,在這裡主要關注的是php_ap2_register_hook.這裡定義的一些回調方法,會在Apache啟動的時候直接調用。
php_ap2_register_hook函數的實現在源碼sapi/apache2handler/sapi_apache2.c文件中:
- 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 int
- php_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 ZTS
- tsrm_startup(1, 1, 0, NULL);
- #endif
- sapi_startup(&apache2_sapi_module);
- apache2_sapi_module.startup(&apache2_sapi_module);
- apr_pool_cleanup_register(pconf, NULL, php_apache_server_shutdown, apr_pool_cleanup_null);
- php_apache_add_version(pconf);
-
- return OK;
- }