为了使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 定义在core/nginx.c中,供main函数和ServiceMain函数调用。
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 is_scm为非0表示以SCM服务方式运行,否则以控制台方式运行。宏NGX_USE_SERVICE用于支持SCM服务方式的条件编译。相比老的main函数,ngx_main依次修改了以下几方面:
不显示版本和帮助 只有以控制台方式运行时,才能查看版本和帮助,即这部分代码当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 增加服务初始化
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 当is_scm为非0即nginx以服务方式运行时,调用外部函数ngx_service_init初始化。因为此时ngx_cycle还没构造,而ngx_service_init须将错误记录在log中,所以应在ngx_log_init完成后调用。
不测试配置和处理信号 与不显示版本和帮助同理,只有以控制台方式运行时,这部分处理才有效。
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 设置状态为正在运行
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; 为了准确报告服务正在运行的状态,应在所有初始化完成后进行,所以在最后的主循环前调用ngx_service_update_state。
以下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
} 指定ServiceMain函数为ngx_service_main,有2种情况会以控制台方式运行:1)在控制台启动nginx产生的master和worker进程,2)在SCM中启动nginx产生的worker进程。当以控制台方式运行时,StartServiceCtrlDispatcher失败返回ERROR_FAILED_SERVICE_CONTROLLER_CONNECT错误,以is_scm为0调用ngx_main函数;当以SCM服务方式运行成功时,会调用到ServiceMain函数。
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
} 以is_scm为1调用ngx_main。当ngx_main异常退出时,会调用到ngx_exit_handler;当正常返回时直接更新服务状态为已停止。
服务初始化 由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
} 先注册退出回调ngx_exit_handler,再注册服务控制处理器ngx_service_control,最后设置服务状态为正在启动。 由于在nginx实现中,有多处出现异常错误时直接调用exit,为了简单方便,在退出时报告服务停止的状态,因此首先使用atexit注册了ngx_exit_handler。
1
static void ngx_exit_handler(void)
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
} 当停止或重新启动服务时会进入到SERVICE_CONTROL_STOP分支,设置服务状态为正在停止,发送quit信号(在nginx中用命名event代替实现)给master进程。
设置服务状态
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
} 由ngx_service_init、ngx_main、ngx_service_control和ngx_exit_handler调用,分别设置正在启动、正在运行、正在停止和已停止状态。
posted on 2016-07-12 15:31
春秋十二月 阅读(4494)
评论(0) 编辑 收藏 引用 所属分类:
Opensrc