接下来,master 进程就要正式开始发挥他作为一个 daemon 进程的责任了,陷入循环,等待信号发生,做出相应处理
ngx_new_binary = 0;
delay = 0;
sigio = 0;
live = 1;
for ( ;; ) {
// 设定定时器,worker 定时退出
if (delay) {
if (ngx_sigalrm) {
sigio = 0;
delay *= 2;
ngx_sigalrm = 0;
}
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"termination cycle: %d", delay);
itv.it_interval.tv_sec = 0;
itv.it_interval.tv_usec = 0;
itv.it_value.tv_sec = delay / 1000;
itv.it_value.tv_usec = (delay % 1000 ) * 1000;
// 设定精确的定时功能,以系统时间计算
if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"setitimer() failed");
}
}
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "sigsuspend");
// 等待信号
// 信号的默认处理函数是 ngx_init_signals 中设定的 ngx_signal_handler 函数
sigsuspend(&set);
// 更新时间
ngx_time_update();
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"wake up, sigio %i", sigio);
// SIGCHLD 信号响应
if (ngx_reap) {
ngx_reap = 0;
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "reap children");
// 有 worker 异常退出,重启 worker
live = ngx_reap_children(cycle);
}
// 收到 NGX_CMD_TERMINATE 命令 或 SIGTERM 信号 或 SIGINT 信号
// 或 NGX_CMD_QUIT 命令 或 SIGQUIT 信号
if (!live && (ngx_terminate || ngx_quit)) {
// 退出 master
ngx_master_process_exit(cycle);
}
// 收到 NGX_CMD_TERMINATE 命令 或 SIGTERM 信号 或 SIGINT 信号
// 给所有进程发送 NGX_TERMINATE_SIGNAL 信号,然后等待
// 如果超时,再次发送 SIGKILL 信号强制退出
if (ngx_terminate) {
if (delay == 0) {
delay = 50;
}
if (sigio) {
sigio--;
continue;
}
sigio = ccf->worker_processes + 2 /* cache processes */;
if (delay > 1000) {
ngx_signal_worker_processes(cycle, SIGKILL);
} else {
ngx_signal_worker_processes(cycle,
ngx_signal_value(NGX_TERMINATE_SIGNAL));
}
continue;
}
// 收到 NGX_CMD_QUIT 命令 或 SIGQUIT 信号
if (ngx_quit) {
// 给所有 worker 发送 SIGQUIT 信号
ngx_signal_worker_processes(cycle,
ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
// 关闭所有监听 socket
ls = cycle->listening.elts;
for (n = 0; n < cycle->listening.nelts; n++) {
if (ngx_close_socket(ls[n].fd) == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
ngx_close_socket_n " %V failed",
&ls[n].addr_text);
}
}
cycle->listening.nelts = 0;
continue;
}
// 收到 SIGHUP 信号
if (ngx_reconfigure) {
ngx_reconfigure = 0;
// 重启 worker,不需要重新初始化配置
if (ngx_new_binary) {
ngx_start_worker_processes(cycle, ccf->worker_processes,
NGX_PROCESS_RESPAWN);
ngx_start_cache_manager_processes(cycle, 0);
ngx_noaccepting = 0;
continue;
}
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reconfiguring");
// 重新初始化配置
cycle = ngx_init_cycle(cycle);
if (cycle == NULL) {
cycle = (ngx_cycle_t *) ngx_cycle;
continue;
}
// 重启 worker
ngx_cycle = cycle;
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx,
ngx_core_module);
ngx_start_worker_processes(cycle, ccf->worker_processes,
NGX_PROCESS_JUST_RESPAWN);
ngx_start_cache_manager_processes(cycle, 1);
/* allow new processes to start */
ngx_msleep(100);
live = 1;
ngx_signal_worker_processes(cycle,
ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
}
// worker 需要重启
if (ngx_restart) {
ngx_restart = 0;
ngx_start_worker_processes(cycle, ccf->worker_processes,
NGX_PROCESS_RESPAWN);
ngx_start_cache_manager_processes(cycle, 0);
live = 1;
}
// 收到 SIGUSR1 信号,重新打开 log
if (ngx_reopen) {
ngx_reopen = 0;
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
ngx_reopen_files(cycle, ccf->user);
ngx_signal_worker_processes(cycle,
ngx_signal_value(NGX_REOPEN_SIGNAL));
}
// 收到 SIGUSR2 信号,热代码替换
if (ngx_change_binary) {
ngx_change_binary = 0;
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "changing binary");
ngx_new_binary = ngx_exec_new_binary(cycle, ngx_argv);
}
// 收到SIGWINCH信号不在接受请求,worker退出,master不退出
if (ngx_noaccept) {
ngx_noaccept = 0;
ngx_noaccepting = 1;
ngx_signal_worker_processes(cycle,
ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
}
}
这里的代码还是比较容易理解的,添加了大量的注释帮助理解
使用了 setitimer 设置了定时功能,虽然 alerm 与 sigsuspend 调用同样可以实现相同功能,但是 setitimer 可以实现更加精准的定时功能
关于 setitimer 的用法可以参看:
基于 setitimer 实现的精确计时 sleep
当子进程退出后,会给守护进程发送 SIGCHLD 信号,在默认的信号处理函数 ngx_signal_handler 中,将 ngx_reap 置为 1
随即会执行下面的方法:
// SIGCHLD 信号响应
if (ngx_reap) {
ngx_reap = 0;
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "reap children");
// 有 worker 异常退出,重启 worker
live = ngx_reap_children(cycle);
}
// static ngx_uint_t ngx_reap_children(ngx_cycle_t *cycle)
// 重启子进程 {{{
static ngx_uint_t
ngx_reap_children(ngx_cycle_t *cycle)
{
ngx_int_t i, n;
ngx_uint_t live;
ngx_channel_t ch;
ngx_core_conf_t *ccf;
ngx_memzero(&ch, sizeof(ngx_channel_t));
ch.command = NGX_CMD_CLOSE_CHANNEL;
ch.fd = -1;
live = 0;
for (i = 0; i < ngx_last_process; i++) {
ngx_log_debug7(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"child: %d %P e:%d t:%d d:%d r:%d j:%d",
i,
ngx_processes[i].pid,
ngx_processes[i].exiting,
ngx_processes[i].exited,
ngx_processes[i].detached,
ngx_processes[i].respawn,
ngx_processes[i].just_spawn);
if (ngx_processes[i].pid == -1) {
continue;
}
// 进程已退出则重启
if (ngx_processes[i].exited) {
if (!ngx_processes[i].detached) {
// 关闭与已退出进程通信的 fd
ngx_close_channel(ngx_processes[i].channel, cycle->log);
ngx_processes[i].channel[0] = -1;
ngx_processes[i].channel[1] = -1;
ch.pid = ngx_processes[i].pid;
ch.slot = i;
// 通知其他子进程关闭与已退出进程通信的fd
for (n = 0; n < ngx_last_process; n++) {
if (ngx_processes[n].exited
|| ngx_processes[n].pid == -1
|| ngx_processes[n].channel[0] == -1)
{
continue;
}
ngx_log_debug3(NGX_LOG_DEBUG_CORE, cycle->log, 0,
"pass close channel s:%i pid:%P to:%P",
ch.slot, ch.pid, ngx_processes[n].pid);
/* TODO: NGX_AGAIN */
// 向子进程发送数据
ngx_write_channel(ngx_processes[n].channel[0],
&ch, sizeof(ngx_channel_t), cycle->log);
}
}
// 重启需要重启的已退出进程
if (ngx_processes[i].respawn
&& !ngx_processes[i].exiting
&& !ngx_terminate
&& !ngx_quit)
{
if (ngx_spawn_process(cycle, ngx_processes[i].proc,
ngx_processes[i].data,
ngx_processes[i].name, i)
== NGX_INVALID_PID)
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
"could not respawn %s",
ngx_processes[i].name);
continue;
}
ch.command = NGX_CMD_OPEN_CHANNEL;
ch.pid = ngx_processes[ngx_process_slot].pid;
ch.slot = ngx_process_slot;
ch.fd = ngx_processes[ngx_process_slot].channel[0];
// 向其他子进程广播新创建进程信息
ngx_pass_open_channel(cycle, &ch);
live = 1;
continue;
}
if (ngx_processes[i].pid == ngx_new_binary) {
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx,
ngx_core_module);
if (ngx_rename_file((char *) ccf->oldpid.data,
(char *) ccf->pid.data)
== NGX_FILE_ERROR)
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
ngx_rename_file_n " %s back to %s failed "
"after the new binary process \"%s\" exited",
ccf->oldpid.data, ccf->pid.data, ngx_argv[0]);
}
ngx_new_binary = 0;
if (ngx_noaccepting) {
ngx_restart = 1;
ngx_noaccepting = 0;
}
}
if (i == ngx_last_process - 1) {
ngx_last_process--;
} else {
ngx_processes[i].pid = -1;
}
} else if (ngx_processes[i].exiting || !ngx_processes[i].detached) {
live = 1;
}
}
return live;
} // }}}
在这一过程中,守护进程首先关闭与已退出子进程通信的域套接字 fd,然后向其他子进程广播相应消息
然后执行 ngx_spawn_process 函数重新启动子进程,并广播新的域套接字 fd
当收到 NGX_CMD_TERMINATE 命令或 SIGTERM 或 SIGINT 或 SIGQUIT 信号后,master 进程随即退出
// 收到 NGX_CMD_TERMINATE 命令 或 SIGTERM 信号 或 SIGINT 信号
// 或 NGX_CMD_QUIT 命令 或 SIGQUIT 信号
if (!live && (ngx_terminate || ngx_quit)) {
// 退出 master
ngx_master_process_exit(cycle);
}
// static void ngx_master_process_exit(ngx_cycle_t *cycle)
// master 的退出 {{{
static void
ngx_master_process_exit(ngx_cycle_t *cycle)
{
ngx_uint_t i;
// 删除 pid 文件
ngx_delete_pidfile(cycle);
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exit");
// 执行模块退出回调函数
for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->exit_master) {
ngx_modules[i]->exit_master(cycle);
}
}
// 关闭套接字
ngx_close_listening_sockets(cycle);
/*
* Copy ngx_cycle->log related data to the special static exit cycle,
* log, and log file structures enough to allow a signal handler to log.
* The handler may be called when standard ngx_cycle->log allocated from
* ngx_cycle->pool is already destroyed.
*/
ngx_exit_log = *ngx_log_get_file_log(ngx_cycle->log);
ngx_exit_log_file.fd = ngx_exit_log.file->fd;
ngx_exit_log.file = &ngx_exit_log_file;
ngx_exit_log.next = NULL;
ngx_exit_log.writer = NULL;
ngx_exit_cycle.log = &ngx_exit_log;
ngx_exit_cycle.files = ngx_cycle->files;
ngx_exit_cycle.files_n = ngx_cycle->files_n;
ngx_cycle = &ngx_exit_cycle;
// 销毁内存池
ngx_destroy_pool(cycle->pool);
// 退出进程
exit(0);
} // }}}
主要进行了以下操作:
- 删除 pid 配置文件
- 执行退出模块的回调函数结束所有模块
- 关闭套接字
- 最后销毁进程池
- 退出进程
需要注意的是,在上述操作中 worker 并没有退出
于是接下来对信号进行判断
// 收到 NGX_CMD_TERMINATE 命令 或 SIGTERM 信号 或 SIGINT 信号
// 给所有进程发送 NGX_TERMINATE_SIGNAL 信号,然后等待
// 如果超时,再次发送 SIGKILL 信号强制退出
if (ngx_terminate) {
if (delay == 0) {
delay = 50;
}
if (sigio) {
sigio--;
continue;
}
sigio = ccf->worker_processes + 2 /* cache processes */;
if (delay > 1000) {
ngx_signal_worker_processes(cycle, SIGKILL);
} else {
ngx_signal_worker_processes(cycle,
ngx_signal_value(NGX_TERMINATE_SIGNAL));
}
continue;
}
// 收到 NGX_CMD_QUIT 命令 或 SIGQUIT 信号
if (ngx_quit) {
// 给所有 worker 发送 SIGQUIT 信号
ngx_signal_worker_processes(cycle,
ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
// 关闭所有监听 socket
ls = cycle->listening.elts;
for (n = 0; n < cycle->listening.nelts; n++) {
if (ngx_close_socket(ls[n].fd) == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
ngx_close_socket_n " %V failed",
&ls[n].addr_text);
}
}
cycle->listening.nelts = 0;
continue;
}
只有当收到 NGX_CMD_TERMINATE 命令或 SIGTERM 或 SIGINT 信号时才会结束所有的子进程
之后关闭所有监听套接字,结束工作
产生 SIGHUP 信号的三种原因:
- 中断关闭,会话首进程,及所有后台进程会收到该信号(默认操作为退出)
- 会话首进程退出后,会话中每个进程会收到该信号(默认操作为退出)
- 父进程退出时,如果子进程已经收到过 SIGSTOP 或 SIGTSTP 信号,则子进程将收到 SIGHUP 信号
由于 master 已经脱离终端,所以不会收到 SIGHUP,SIGHUP 信号被重用为重启 worker,重新初始化配置信号
if (ngx_reconfigure) {
ngx_reconfigure = 0;
// 重启 worker,不需要重新初始化配置
if (ngx_new_binary) {
ngx_start_worker_processes(cycle, ccf->worker_processes,
NGX_PROCESS_RESPAWN);
ngx_start_cache_manager_processes(cycle, 0);
ngx_noaccepting = 0;
continue;
}
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reconfiguring");
// 重新初始化配置
cycle = ngx_init_cycle(cycle);
if (cycle == NULL) {
cycle = (ngx_cycle_t *) ngx_cycle;
continue;
}
// 重启 worker
ngx_cycle = cycle;
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx,
ngx_core_module);
ngx_start_worker_processes(cycle, ccf->worker_processes,
NGX_PROCESS_JUST_RESPAWN);
ngx_start_cache_manager_processes(cycle, 1);
/* allow new processes to start */
ngx_msleep(100);
live = 1;
ngx_signal_worker_processes(cycle,
ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
}
技术帖
network
龙潭书斋
进程
signal
sigprocmask
sigint
sigquit
SIGCHLD
sigsuspend
nginx
worker
master
opensource
daemon
sighup