nginx源码分析—如何发送信号
2012-02-29 23:09
661 查看
作者:阿波
链接:/article/1451041.html
Content
0.序
1.发送信号
(1)通过命令行"-s"选项启动nginx
(2) ngx_signal_process()函数处理
(3) ngx_os_signal_process()函数处理
2.小结
0. 序
本文主要分析nginx如何发送信号并进行处理。文中如无特别说明,.表示nginx-1.0.4代码目录,本文为/usr/src/nginx-1.0.4。
1. 发送信号
nginx的发送信号由一个新的进程ngx_process=NGX_PROCESS_SIGNALLER=2来完成。主要有以下3步完成。
(1) 通过命令行"-s"选项启动nginx
对发送的信号,只处理4种:"stop", "quit", "reopen", "reload"。发送方式如下。请参考ngx_get_options()函数对"-s"选项的处理。
# ./nginx -s reopen
ngx_get_options()会将"reopen"信号赋值给全局变量ngx_signal,并将ngx_process赋值为NGX_PROCESS_SIGNALLER=2。且该命令会启动一个新的nginx进程,并进入main()函数开始执行, 并直接调用ngx_signal_process(),如下。
可自行实验,即开启nginx,再另外开启nginx发送信号,查看nginx进程的变化。
(2) ngx_signal_process()函数处理
该函数作用:
读取ngx_core_module模块的配置结构ngx_core_conf_t;
根据配置结构找到其工作进程文件,如"/usr/local/nginx/logs/nginx.pid"(该文件保存nginx进程ID,即pid);
打开该文件,读取pid;
调用ngx_os_signal_process()发送信号;
(3) ngx_os_signal_process()函数处理
遍历signals数组,根据给定信号name,找到对应signo;
调用kill向该pid发送signo号信号;
代码如下,其他可参考源代码。
kill - send signal to a process
DESCRIPTION
The kill() system call can be used to send any signal to any process group or process.
If pid is positive, then signal sig is sent to the process with the ID specified by pid.
If pid equals 0, then sig is sent to every process in the process group of the calling process.
If pid equals -1, then sig is sent to every process for which the calling process has permission to send signals, except for process 1 (init), but see below.
If pid is less than -1, then sig is sent to every process in the process group whose ID is -pid.
If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID.
For a process to have permission to send a signal it must either be privileged (under Linux: have the CAP_KILL capability), or the real or effective user ID of the sending process must equal the real or saved set-user-ID
of the target process. In the case of SIGCONT it suffices when the sending and receiving processes belong to the same session.
2. 小结
本文主要分析nginx在运行过程中如何发送信号。
Reference
#man kill
链接:/article/1451041.html
Content
0.序
1.发送信号
(1)通过命令行"-s"选项启动nginx
(2) ngx_signal_process()函数处理
(3) ngx_os_signal_process()函数处理
2.小结
0. 序
本文主要分析nginx如何发送信号并进行处理。文中如无特别说明,.表示nginx-1.0.4代码目录,本文为/usr/src/nginx-1.0.4。
1. 发送信号
nginx的发送信号由一个新的进程ngx_process=NGX_PROCESS_SIGNALLER=2来完成。主要有以下3步完成。
(1) 通过命令行"-s"选项启动nginx
对发送的信号,只处理4种:"stop", "quit", "reopen", "reload"。发送方式如下。请参考ngx_get_options()函数对"-s"选项的处理。
# ./nginx -s reopen
ngx_get_options()会将"reopen"信号赋值给全局变量ngx_signal,并将ngx_process赋值为NGX_PROCESS_SIGNALLER=2。且该命令会启动一个新的nginx进程,并进入main()函数开始执行, 并直接调用ngx_signal_process(),如下。
if (ngx_signal) { return ngx_signal_process(cycle, ngx_signal); }
可自行实验,即开启nginx,再另外开启nginx发送信号,查看nginx进程的变化。
(2) ngx_signal_process()函数处理
该函数作用:
读取ngx_core_module模块的配置结构ngx_core_conf_t;
根据配置结构找到其工作进程文件,如"/usr/local/nginx/logs/nginx.pid"(该文件保存nginx进程ID,即pid);
打开该文件,读取pid;
调用ngx_os_signal_process()发送信号;
(3) ngx_os_signal_process()函数处理
遍历signals数组,根据给定信号name,找到对应signo;
调用kill向该pid发送signo号信号;
代码如下,其他可参考源代码。
ngx_int_t ngx_os_signal_process(ngx_cycle_t *cycle, char *name, ngx_int_t pid) { ngx_signal_t *sig; for (sig = signals; sig->signo != 0; sig++) { if (ngx_strcmp(name, sig->name) == 0) { if (kill(pid, sig->signo) != -1) { /* 通过系统调用向该进程发送信号 */ return 0; } ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, "kill(%P, %d) failed", pid, sig->signo); } } return 1; }
kill - send signal to a process
DESCRIPTION
The kill() system call can be used to send any signal to any process group or process.
If pid is positive, then signal sig is sent to the process with the ID specified by pid.
If pid equals 0, then sig is sent to every process in the process group of the calling process.
If pid equals -1, then sig is sent to every process for which the calling process has permission to send signals, except for process 1 (init), but see below.
If pid is less than -1, then sig is sent to every process in the process group whose ID is -pid.
If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID.
For a process to have permission to send a signal it must either be privileged (under Linux: have the CAP_KILL capability), or the real or effective user ID of the sending process must equal the real or saved set-user-ID
of the target process. In the case of SIGCONT it suffices when the sending and receiving processes belong to the same session.
2. 小结
本文主要分析nginx在运行过程中如何发送信号。
Reference
#man kill
相关文章推荐
- 文章10:Nginx源码分析----Nginx如何获得数据包(未完成)
- Nginx源码分析 - 主流程篇 - 平滑重启和信号控制
- libevent源码分析--如何将定时器和信号事件都集合到I/O复用中
- nginx源码分析—信号初始化
- nginx源码分析—信号初始化和使用
- nginx源码分析--nginx外部信号 命令参数
- Nginx源码分析与实践---进程间通信机制(信号)
- 《Linux内核修炼之道》精华分享与讨论(6)——分析内核源码如何入手?(上)
- Android源码学习笔记1-短信发送流程分析
- Tomcat源码分析(三)--连接器是如何与容器关联的?
- nginx源码分析(19)-方法(3)
- freeswitch mrcp 源码分析--数据包的发送
- ARouter 学习-源码分析如何暴露服务
- nginx源码分析2———基础数据结构三(内存池)
- angular源码分析:angular中的依赖注入式如何实现的
- nginx源码分析--数据结构 哈希 ngx_hash_t
- Volley源码分析 (三):如何分发结果
- Qt源码分析之信号和槽机制
- React-Native-源码分析三-JSX如何渲染成原生页面(下)
- Heritrix源码分析(五) 如何让Heritrix在Ecplise等IDE下编程启动