nginx iocp(3):scm服务控制
为了使nginx支持windows服务,本文阐述以下主要的改进实现。
ngx_main函数
为了在SCM服务中复用main函数的逻辑,将其重命名为ngx_main,并添加第3个参数is_scm以兼容控制台运行方式,声明在core/nginx.h中。
1
#if
(NGX_WIN32 && NGX_USE_SERVICE)
2 extern int ngx_main (int argc, char * const * argv, int is_scm);
3 #endif
2 extern int ngx_main (int argc, char * const * argv, int is_scm);
3 #endif
1
int
ngx_cdecl
2 #if (NGX_WIN32 && NGX_USE_SERVICE)
3 ngx_main ( int argc, char * const * argv, int is_scm)
4 #else
5 main ( int argc, char * const * argv)
6 #endif
2 #if (NGX_WIN32 && NGX_USE_SERVICE)
3 ngx_main ( int argc, char * const * argv, int is_scm)
4 #else
5 main ( int argc, char * const * argv)
6 #endif
不显示版本和帮助
只有以控制台方式运行时,才能查看版本和帮助,即这部分代码当is_scm为0时才有效。
1
#if
(NGX_WIN32 && NGX_USE_SERVICE)
2 if ( ! is_scm) {
3 if (ngx_show_version) {
4 ngx_write_stderr("nginx version: " NGINX_VER NGX_LINEFEED);
5
6 if (ngx_show_help) {
7
8 }
9
10 if (ngx_show_configure) {
11
12 }
13
14 if (!ngx_test_config) {
15 return 0;
16 }
17 }
18}
19 #endif
2 if ( ! is_scm) {
3 if (ngx_show_version) {
4 ngx_write_stderr("nginx version: " NGINX_VER NGX_LINEFEED);
5
6 if (ngx_show_help) {
7
8 }
9
10 if (ngx_show_configure) {
11
12 }
13
14 if (!ngx_test_config) {
15 return 0;
16 }
17 }
18}
19 #endif
增加服务初始化
1
log
=
ngx_log_init(ngx_prefix);
2 if (log == NULL) {
3 return 1;
4}
5
6 #if (NGX_WIN32 && NGX_USE_SERVICE)
7 if (is_scm) {
8 ngx_service_init(log,argc,argv);
9}
10 #endif
2 if (log == NULL) {
3 return 1;
4}
5
6 #if (NGX_WIN32 && NGX_USE_SERVICE)
7 if (is_scm) {
8 ngx_service_init(log,argc,argv);
9}
10 #endif
不测试配置和处理信号
与不显示版本和帮助同理,只有以控制台方式运行时,这部分处理才有效。
1
#if
(NGX_WIN32 && NGX_USE_SERVICE)
2 if ( ! is_scm) {
3 if (ngx_test_config) {
4 if (!ngx_quiet_mode) {
5 ngx_log_stderr(0, "configuration file %s test is successful", cycle->conf_file.data);
6 }
7
8 return 0;
9 }
10
11 if (ngx_signal) {
12 return ngx_signal_process(cycle, ngx_signal);
13 }
14 }
15 #endif
2 if ( ! is_scm) {
3 if (ngx_test_config) {
4 if (!ngx_quiet_mode) {
5 ngx_log_stderr(0, "configuration file %s test is successful", cycle->conf_file.data);
6 }
7
8 return 0;
9 }
10
11 if (ngx_signal) {
12 return ngx_signal_process(cycle, ngx_signal);
13 }
14 }
15 #endif
设置状态为正在运行
1
#if
(NGX_WIN32 && NGX_USE_SERVICE)
2 if (is_scm && ngx_service_update_state(ngx_cycle -> log, SERVICE_RUNNING ,0 , 0 )) {
3 exit(1);
4 }
5 #endif
6 ngx_use_stderr = 0 ;
7
8 if (ngx_process == NGX_PROCESS_SINGLE) {
9 ngx_single_process_cycle(cycle);
10
11} else {
12 ngx_master_process_cycle(cycle);
13}
14
15 return 0 ;
2 if (is_scm && ngx_service_update_state(ngx_cycle -> log, SERVICE_RUNNING ,0 , 0 )) {
3 exit(1);
4 }
5 #endif
6 ngx_use_stderr = 0 ;
7
8 if (ngx_process == NGX_PROCESS_SINGLE) {
9 ngx_single_process_cycle(cycle);
10
11} else {
12 ngx_master_process_cycle(cycle);
13}
14
15 return 0 ;
以下5个部分对应的实现函数,都定义在os/win32/ngx_service.c中。
main函数
nginx程序的启动入口主函数,可被控制台或SCM调用,当被SCM调用时,负责用SCM来注册服务,以及启动服务控制调度程序。
1
void
main(
int
argc,
char
*
const
*
argv)
2 {
3 SERVICE_TABLE_ENTRY st[] = {
4 { "nginx", ngx_service_main },
5 { NULL, NULL }
6 };
7
8 /* StartServiceCtrlDispatcher() should be called within 30 seconds */
9 if (StartServiceCtrlDispatcher(st) == 0) {
10 if(ERROR_FAILED_SERVICE_CONTROLLER_CONNECT==ngx_errno){
11 ngx_main(argc,argv,0);
12 }
13 }
14}
2 {
3 SERVICE_TABLE_ENTRY st[] = {
4 { "nginx", ngx_service_main },
5 { NULL, NULL }
6 };
7
8 /* StartServiceCtrlDispatcher() should be called within 30 seconds */
9 if (StartServiceCtrlDispatcher(st) == 0) {
10 if(ERROR_FAILED_SERVICE_CONTROLLER_CONNECT==ngx_errno){
11 ngx_main(argc,argv,0);
12 }
13 }
14}
ServiceMain函数
由SCM生成的一个逻辑线程调用。
1
static
void
WINAPI
ngx_service_main
(u_int argc,
char
**
argv)
2 {
3 ngx_main(argc,argv,1);
4 ngx_service_update_state(ngx_cycle->log,SERVICE_STOPPED,0,0);
5}
2 {
3 ngx_main(argc,argv,1);
4 ngx_service_update_state(ngx_cycle->log,SERVICE_STOPPED,0,0);
5}
服务初始化
由ngx_main调用。
1
void
ngx_service_init
(ngx_log_t
*
log,
int
argc,
char
*
const
*
argv)
2 {
3 atexit(ngx_exit_handler);
4
5 ngx_service = RegisterServiceCtrlHandlerEx("nginx", ngx_service_control, NULL);
6 if(ngx_service == INVALID_HANDLE_VALUE) {
7 ngx_log_error(NGX_LOG_EMERG,log,ngx_errno,"RegisterServiceCtrlHandlerEx fail");
8 exit(1);
9 }
10
11 ngx_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
12 ngx_status.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_PARAMCHANGE;
13 ngx_status.dwWin32ExitCode = NO_ERROR;
14 ngx_status.dwServiceSpecificExitCode = 0;
15
16 /* SetServiceStatus() should be called within 80 seconds */
17 if(ngx_service_update_state(log,SERVICE_START_PENDING,1,2000)){
18 exit(1);
19 }
20}
2 {
3 atexit(ngx_exit_handler);
4
5 ngx_service = RegisterServiceCtrlHandlerEx("nginx", ngx_service_control, NULL);
6 if(ngx_service == INVALID_HANDLE_VALUE) {
7 ngx_log_error(NGX_LOG_EMERG,log,ngx_errno,"RegisterServiceCtrlHandlerEx fail");
8 exit(1);
9 }
10
11 ngx_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
12 ngx_status.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_PARAMCHANGE;
13 ngx_status.dwWin32ExitCode = NO_ERROR;
14 ngx_status.dwServiceSpecificExitCode = 0;
15
16 /* SetServiceStatus() should be called within 80 seconds */
17 if(ngx_service_update_state(log,SERVICE_START_PENDING,1,2000)){
18 exit(1);
19 }
20}
1
static
void
ngx_exit_handler(
void
)
2 {
3 ngx_service_update_state(ngx_cycle->log,SERVICE_STOPPED,0,0);
4}
2 {
3 ngx_service_update_state(ngx_cycle->log,SERVICE_STOPPED,0,0);
4}
服务控制处理器
由SCM生成的一个逻辑线程调用。
1
static
u_long WINAPI
ngx_service_control
(u_long control, u_long type,
void
*
data,
void
*
ctx)
2 {
3 switch(control) {
4 case SERVICE_CONTROL_STOP:
5 ngx_service_update_state(ngx_cycle->log, SERVICE_STOP_PENDING, 0, 0);
6 ngx_os_signal_process(ngx_cycle,"quit",ngx_pid);
7 break;
8 }
9
10 return NO_ERROR;
11}
2 {
3 switch(control) {
4 case SERVICE_CONTROL_STOP:
5 ngx_service_update_state(ngx_cycle->log, SERVICE_STOP_PENDING, 0, 0);
6 ngx_os_signal_process(ngx_cycle,"quit",ngx_pid);
7 break;
8 }
9
10 return NO_ERROR;
11}
设置服务状态
1
int
ngx_service_update_state
(ngx_log_t
*
log, u_long state, u_long checkpoint,u_long waithint)
2 {
3 ngx_status.dwCurrentState = state;
4 ngx_status.dwCheckPoint = checkpoint;
5 ngx_status.dwWaitHint = waithint;
6
7 if(SetServiceStatus(ngx_service, &ngx_status) == 0) {
8 ngx_log_error(NGX_LOG_EMERG,log,ngx_errno,"SetServiceStatus fail");
9 return -1;
10 }
11
12 return 0;
13}
2 {
3 ngx_status.dwCurrentState = state;
4 ngx_status.dwCheckPoint = checkpoint;
5 ngx_status.dwWaitHint = waithint;
6
7 if(SetServiceStatus(ngx_service, &ngx_status) == 0) {
8 ngx_log_error(NGX_LOG_EMERG,log,ngx_errno,"SetServiceStatus fail");
9 return -1;
10 }
11
12 return 0;
13}