php內核分析之sapi-module-struct
爲了幫助同學們學習PHP內核技術,小編整理了php內核分析之sapi_module_struct,希望對大家有所幫助!更多內容請關注應屆畢業生網!
這裏閱讀的php版本爲PHP-7.1.0 RC3,閱讀代碼的平臺爲linux
首先是尋找php的入口,php有很多種模式,apache,php-fpm, cli模式,我要入手的話,只能先從最簡單的cli模型開始。
那麼,我需要先尋找
php -r 'echo 12;'
這個命令是如何執行的。
首先還是尋找main入口,由於我們看的是命令行的php程序。所以,這個入口在sapi/cli/php_cli.c中。
首先是定義一系列的變量
int c;
zend_file_handle file_handle;
int behavior = PHP_MODE_STANDARD;
char *reflection_what = NULL;
volatile int request_started = 0;
volatile int exit_status = 0;
char *php_optarg = NULL, *orig_optarg = NULL;
int php_optind = 1, orig_optind = 1;
char *exec_direct=NULL, *exec_run=NULL, *exec_begin=NULL, *exec_end=NULL;
char *arg_free=NULL, **arg_excp=&arg_free;
char *script_file=NULL, *translated_path = NULL;
int interactive=0;
int lineno = 0;
const char *param_error=NULL;
int hide_argv = 0;
然後是這個
sapi_module_struct *sapi_module = &cli_sapi_module;
這是一個sapi_module_struct結構,這個結構是sapi中最重要的數據結構。它的定義在main/SAPI.h中。
下面是增加了註釋的代碼:
struct _sapi_module_struct { // SAPI模塊結構
char *name; // 應用層名稱,比如cli,cgi等
char *pretty_name; // 應用層更易讀的名字,比如cli對應的就是Command Line Interface
int (*startup)(struct _sapi_module_struct *sapi_module); // 當一個應用要調用php的時候,這個模塊啓動的時候會調用的函數
int (*shutdown)(struct _sapi_module_struct *sapi_module); // 當一個應用要調用php的時候,這個模塊結束的時候會調用的函數
int (*activate)(void); // 在處理每個request的時候,激活需要調用的函數
int (*deactivate)(void); // 在處理完每個request的時候,收尾時候要調用的函數
size_t (*ub_write)(const char *str, size_t str_length); // 這個函數告訴php如何輸出數據
void (*flush)(void *server_context); // 提供給php的刷新緩存的函數指針
zend_stat_t *(*get_stat)(void); // 用來判斷要執行文件的權限,來判斷是否有執行權限
char *(*getenv)(char *name, size_t name_len); // 獲取環境變量的方法
void (*sapi_error)(int type, const char *error_msg, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3); // 錯誤處理方法
int (*header_handler)(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers); // 這個函數會在我們調用header()的時候被調用
int (*send_headers)(sapi_headers_struct *sapi_headers); // 發送所有的header
void (*send_header)(sapi_header_struct *sapi_header, void *server_context); // 單獨發送某一個header
size_t (*read_post)(char *buffer, size_t count_bytes); // 如何獲取HTTP POST中的數據
char *(*read_cookies)(void); // 如何獲取cookie中的數據
void (*register_server_variables)(zval *track_vars_array); // 這個函數可以給$_SERVER中獲取變量
void (*log_message)(char *message, int syslog_type_int); // 輸出錯誤信息函數
double (*get_request_time)(void); // 獲取請求時間的函數
void (*terminate_process)(void); // TODO: 調用exit的時候調用的方法
char *php_ini_path_override; // PHP的ini文件被複寫了所複寫的地址
void (*default_post_reader)(void); // 這裏和前面的read_post有個差別,read_post負責如何獲取POST數據,而這裏的函數負責如何解析POST數據
void (*treat_data)(int arg, char *str, zval *destArray); // 對數據進行處理,比如進行安全過濾等。 default_post_reader/tread_data/input_filter是三個能對輸入進行過濾和處理的.函數
char *executable_location; // 執行的地理位置
int php_ini_ignore; // 是否不使用任何ini配置文件,比如php -n 就將這個位置設置爲1
int php_ini_ignore_cwd; // 不在當前路徑尋找
int (*get_fd)(int *fd); // 獲取執行文件的fd
int (*force_http_10)(void); // 強制使用http1.0
int (*get_target_uid)(uid_t *); // 獲取執行程序的uid
int (*get_target_gid)(gid_t *); // 獲取執行程序的gid
unsigned int (*input_filter)(int arg, char *var, char **val, size_t val_len, size_t *new_val_len); // 對輸入進行過濾。比如將輸入參數填充到自動全局變量$_GET, $_POST, $_COOKIE中
void (*ini_defaults)(HashTable *configuration_hash); // 默認的ini配置
int phpinfo_as_text; // 是否打印phpinfo信息
char *ini_entries; // 有沒有附帶的ini配置,比如使用php -d zone=America/Adak,可以在命令行中設置時區
const zend_function_entry *additional_functions; // 每個SAPI模塊特有的一些函數註冊,比如cli的cli_get_process_title
unsigned int (*input_filter_init)(void); // TODO:
};
相關文章
-
Efforts Urged to Improve Womens Reproductive Health英語閱讀
Efforts Urged to Improve Women's Reproductive HealthParliamentarians from over 100 countries are being urged to take action in improving women's reproductive health ahead of the 2015 deadline -
PHP中CURL的CURLOPT-POSTFIELDS參數使用細節
CURL確實是一個不錯的好工具,不僅在PHP中還是其他的操作系統中,都是一個非常好用的。但是如果你有些參數沒有用好的話,那可能會得不到自己理想中的結果。具體情況如下,更多消息請關注應屆畢業生網! 在通常情況下,我 -
Thinkpad筆記本開機顯示Tointerrruptnormalstartup解決方法
Thinkpad筆記本開機顯示To interrrupt normal startup的解決方法1、先等待5-6分鐘,如果系統又恢復了正常,說明可能是系統硬盤或者風扇區出了問題;2、假如過了5-6分鐘依然沒有反應,則可嘗試先拆掉筆記本電池在裝上;3、或 -
英語報紙解讀:Data may disrupt a peculiar business大綱
原本想做精緻些,整理出一週閱讀素材的處理方法,每日一篇,留日後反思。現在就跟着小編一起來了解一下《時尚玄學或遭數據瓦解》吧。In the film The Devil Wears Prada, the character of Miranda Priestly, whose role i -
英語報紙解讀:Data may disrupt a peculiar business
原本想做精緻些,整理出一週閱讀素材的處理方法,每日一篇,留日後反思。現在就跟着小編一起來了解一下《時尚玄學或遭數據瓦解》吧。In the film The Devil Wears Prada, the character of Miranda Priestly, whose role i -
英語短文Do Old Folks Still Appreciate Humor?
Maybe when people get older, sense of a humor is the first thing to go.Actually, research suggests that old folks, if they’re healthy, can still appreciate humor as much as a younger person still sm -
Chimp Cops Arbitrate Disputes美文欣賞
學期末工作總結 篇1一學期來,我校在教育局、鎮學區的關心支持和正確領導下,繼續發揚“勤奮好學,團結共事”的`工作作風,弘揚“愛崗敬業,團結協作,努力進取,爭取第一”的教育精神,以辦好讓人民滿意的教育爲目標,以提高教育教學 -
人教PEP版國小五年級英語下冊Unit3Myschoolcalender.ALet’stalk
本部分學習的核心句型是:Whenistheparty?It’sinApril.教材通過Mike和張鵬對校歷上四、五月要舉行的活動的討論場景,介紹了孩子們在春天將有很多趣味性活動的情景,即四月的運動會個萬聖節聚會、五月的春遊,讓學生感知上述 -
Jolie drops battle over perfume named Shilohy英語作文
angelina jolie has dropped her legal battle with a perfume-maker who had named her fragrance shiloh - the same as jolies youngest daughter.jolie had filed a note with the u.s. patent and trademark off -
At Climate Summit, Hope for Emissions Deal美文欣賞
It's known as "the roof of the world," four thousand seven hundred meters above sea level lies the Tibetan Plateau. Its glaciers are the source of many of Asia’s most important rivers.But the ice