当前位置:学者斋 >

计算机 >php语言 >

php内核分析之sapi-module-struct

php内核分析之sapi-module-struct

为了帮助同学们学习PHP内核技术,小编整理了php内核分析之sapi_module_struct,希望对大家有所帮助!更多内容请关注应届毕业生网!

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:

};

  • 文章版权属于文章作者所有,转载请注明 https://xuezhezhai.com/jsj/php/4654rm.html