Module Introduction
When nginx receives the abnormal exit signal SIGINT during operation, the current function call stack will be output to the log file. In addition to handling SIGINT, you can also add corresponding signals in the module.
Module development process
<code><span>typedef</span><span>struct</span> ngx_xqw_backtrace_conf_s{ ngx_log_t *<span>log</span>; <span>// 日志</span> ngx_int_t size; <span>// 栈最大深度</span> }ngx_xqw_backtrace_conf_t;</code>
Each module has a corresponding configuration structure.
2. Implement create_conf to allocate memory for the configuration structure and return the corresponding pointer
<code><span>// 初始化配置结构体</span><span>static</span><span>void</span> * ngx_http_xqw_backtrace_create_conf(ngx_cycle_t *cycle) { ngx_xqw_backtrace_conf_t *bcf = <span>NULL</span>; bcf = ngx_palloc(cycle->pool, <span>sizeof</span>(ngx_xqw_backtrace_conf_t)); <span>if</span> (bcf == <span>NULL</span>) <span>return</span><span>NULL</span>; bcf->size = NGX_CONF_UNSET; <span>return</span> bcf; }</code>
The above is the process of creating the structure.
3. Set a set callback function for each configuration item. If the configuration item parameter is a number, you can set set to ngx_conf_set_num_slot, otherwise you need to implement it yourself.
<code><span>// a、用ngx_get_conf得到该模块的结构体</span><span>// b、获取配置项中的参数 cf->cycle->elts</span><span>// c、由参数构造模块结构体中的成员</span> static char* ngx_xqw_backtrace_log(ngx_conf_t <span>*cf</span>, ngx_command_t <span>*cmd</span>, void <span>*conf</span>) { ngx_str_t <span>file</span>, <span>*value</span>; ngx_log_t <span>*log</span>; ngx_xqw_backtrace_conf_t <span>*bcf</span>; bcf = (ngx_xqw_backtrace_conf_t <span>*)</span> ngx_get_conf(cf->cycle->conf_ctx, ngx_xqw_backtrace_module); value = cf->args->elts; <span>file</span> = value[<span>1</span>]; <span>// 解析配置文件出错返回 NGX_CONF_ERROR</span><span>if</span> (ngx_conf_full_name(cf->cycle, &<span>file</span>, <span>1</span>) != NGX_OK) <span>return</span> NGX_CONF_ERROR; <span>log</span> = ngx_log_create(cf->cycle, &<span>file</span>); <span>if</span> (<span>log</span> == NULL) <span>return</span> NGX_CONF_ERROR; bcf-><span>log</span> = <span>log</span>; bcf-><span>log</span>->log_level = NGX_LOG_ERR; <span>// 每个LOGFILE都有相应的级别,</span><span>// 需要设置好级别后才能输出到自定义的LOGFILE</span><span>return</span> NGX_CONF_OK; } </code>
The above is the process of parsing configuration items
4. Initialize the signal processing function
<code><span>static</span> ngx_int_t ngx_init_error_signal(ngx_log_t *log) { ngx_backtrace_signal_t *sigs; <span>struct</span> sigaction sa; <span>// 不需要用index的循环方法</span><span>for</span> (sigs = ngx_backtrace_signals; sigs->name != <span>NULL</span>; sigs ++) { ngx_memzero(&sa, <span>sizeof</span>(<span>struct</span> sigaction)); sa<span>.sa_handler</span> = sigs->handler; sigemptyset(&sa<span>.sa_mask</span>); <span>if</span> (sigaction(sigs->value, &sa, <span>NULL</span>) == -<span>1</span>) { perror(<span>"sigaction"</span>); <span>return</span> NGX_ERROR; } } <span>return</span> NGX_OK; }</code>
implemented around sigaction
5. When writing the exception handling function
<code>static <span>void</span> ngx_backtrace_signal_handler(int sig) { ngx_backtrace_signal_t <span>*</span>sigs; ngx_xqw_backtrace_conf_t <span>*</span>bcf; <span>void</span><span>*</span>buff; size_t size; bcf <span>=</span> (ngx_xqw_backtrace_conf_t<span>*</span>) ngx_get_conf(ngx_cycle<span>-></span>conf_ctx, ngx_xqw_backtrace_module); ngx_log_error(NGX_LOG_ERR, bcf<span>-></span><span>log</span>, <span>0</span>, <span>"hello error\n"</span>); <span>if</span> (bcf <span>==</span><span>NULL</span>) fprintf(stderr, <span>"ngx_get_conf error\n"</span>); for (sigs <span>=</span> ngx_backtrace_signals; sigs<span>-></span>name <span>!=</span><span>NULL</span>; <span>++</span> sigs) <span>if</span> (sigs<span>-></span>value <span>==</span> sig) break; <span>// 要退出了,故将信号设置为默认处理方式</span> struct sigaction sa; ngx_memzero(<span>&</span>sa, sizeof(struct sigaction)); sigemptyset(<span>&</span>sa<span>.</span>sa_mask); sa<span>.</span>sa_handler <span>=</span> SIG_DFL; <span>if</span> (sigaction(sigs<span>-></span>value, <span>&</span>sa, <span>NULL</span>) <span>==</span><span>-</span><span>1</span>) perror(<span>"signal handler sigaction:"</span>); <span>if</span> (bcf<span>-></span>size <span>==</span> NGX_CONF_UNSET) bcf<span>-></span>size <span>=</span> MAX_STACK_SIZE; buff <span>=</span> (<span>void</span><span>*</span>)ngx_palloc(ngx_cycle<span>-></span>pool, bcf<span>-></span>size <span>*</span> sizeof (<span>void</span><span>*</span>)); <span>if</span> (buff <span>==</span><span>NULL</span>) fprintf(stderr, <span>"ngx_palloc error\n"</span>); size <span>=</span> backtrace(buff, bcf<span>-></span>size); backtrace_symbols_fd(buff, size, bcf<span>-></span><span>log</span><span>-></span>file<span>-></span>fd); kill(ngx_getpid(), sig); }</code>
, you need to restore the signal processing function to the default processing situation, and send the signal again after the processing is completed.
Reference materials
ngx_backtrace_module module released by Taobao
ngx_xqw_backtrace_module download link
http://download.csdn.net/detail/wxq714586001/8719535
The above introduces the nginx module development: ngx_xqw_backtrace_module, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.