由于上一篇《菜鸟学习Nginx之启动流程(2)》篇幅比较长,因此特地在写一篇。而本篇将是启动流程中最后一篇,主要介绍worker进程主函数。
一、ngx_worker_process_cycle
worker进程主函数,内容并不是很多,但若是展开说明则将会把整个Nginx框架都牵扯进来。所以这里不会展开说明,后续文章将会深入探讨。
/**
* 子进程 进程main函数
*/
static void
ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
{
ngx_int_t worker = (intptr_t)data;
ngx_process = NGX_PROCESS_WORKER;
ngx_worker = worker;
/**
* 初始化worker进程
* 将listening socket 和 channel添加到epoll事件驱动中
*/
ngx_worker_process_init(cycle, worker);
ngx_setproctitle("worker process"); /* 设置进程名称 */
for (;;)
{
if (ngx_exiting)
{//退出标志 执行exit
if (ngx_event_no_timers_left() == NGX_OK)
{
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
ngx_worker_process_exit(cycle);
}
}
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");
/*很重要: 阻塞在这里等待网络事件或者定时器超时事件 */
ngx_process_events_and_timers(cycle);
if (ngx_terminate)
{/* 处理Terminate事件 */
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
ngx_worker_process_exit(cycle);
}
if (ngx_quit)
{/* 处理Quit事件 */
ngx_quit = 0;
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
"gracefully shutting down");
ngx_setproctitle("worker process is shutting down");
if (!ngx_exiting)
{
ngx_exiting = 1;
ngx_set_shutdown_timer(cycle);
ngx_close_listening_sockets(cycle);
ngx_close_idle_connections(cycle);
}
}
if (ngx_reopen)
{/* 处理Reopen事件 */
ngx_reopen = 0;
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
ngx_reopen_files(cycle, -1);
}
}
}
二、ngx_worker_process_init
此函数还是做了一些内容,例如:将listening socket添加到epoll事件驱动中,下面来看一下ngx_worker_process_init做了哪些工作:
/**
* worker进程初始化
* @param cycle 核心结构
* @param worker 当前worker进程在ngx_processes数组索引
*/
static void
ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker)
{
sigset_t set;
ngx_int_t n;
ngx_time_t *tp;
ngx_uint_t i;
ngx_cpuset_t *cpu_affinity;
struct rlimit rlmt;
ngx_core_conf_t *ccf;
ngx_listening_t *ls;
if (ngx_set_environment(cycle, NULL) == NULL)
{
/* fatal */
exit(2);
}
ccf = (ngx_core_conf_t *)ngx_get_conf(cycle->conf_ctx, ngx_core_module);
/* 设置资源使用门限阈值 */
if (worker >= 0 && ccf->priority != 0)
{
if (setpriority(PRIO_PROCESS, 0, ccf->priority) == -1)
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"setpriority(%d) failed", ccf->priority);
}
}
if (ccf->rlimit_nofile != NGX_CONF_UNSET)
{
rlmt.rlim_cur = (rlim_t)ccf->rlimit_nofile;
rlmt.rlim_max = (rlim_t)ccf->rlimit_nofile;
if (setrlimit(RLIMIT_NOFILE, &rlmt) == -1)
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"setrlimit(RLIMIT_NOFILE, %i) failed",
ccf->rlimit_nofile);
}
}
if (ccf->rlimit_core != NGX_CONF_UNSET)
{
rlmt.rlim_cur = (rlim_t)ccf->rlimit_core;
rlmt.rlim_max = (rlim_t)ccf->rlimit_core;
if (setrlimit(RLIMIT_CORE, &rlmt) == -1)
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"setrlimit(RLIMIT_CORE, %O) failed",
ccf->rlimit_core);
}
}
if (geteuid() == 0)
{/* 设置进程用户、用户组信息 */
if (setgid(ccf->group) == -1)
{
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
"setgid(%d) failed", ccf->group);
/* fatal */
exit(2);
}
if (initgroups(ccf->username, ccf->group) == -1)
{
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
"initgroups(%s, %d) failed",
ccf->username, ccf->group);
}
if (setuid(ccf->user) == -1)
{
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
"setuid(%d) failed", ccf->user);
/* fatal */
exit(2);
}
}
if (worker >= 0)
{//为了提升性能,Nginx采用cpu绑定进程方式
cpu_affinity = ngx_get_cpu_affinity(worker);//进程绑定cpu
if (cpu_affinity)
{
ngx_setaffinity(cpu_affinity, cycle->log);
}
}
#if (NGX_HAVE_PR_SET_DUMPABLE)
/* allow coredump after setuid() in Linux 2.4.x */
if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1)
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"prctl(PR_SET_DUMPABLE) failed");
}
#endif
if (ccf->working_directory.len)
{
if (chdir((char *)ccf->working_directory.data) == -1)
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"chdir(\"%s\") failed", ccf->working_directory.data);
/* fatal */
exit(2);
}
}
/* worker进程清空 信号屏蔽字 */
sigemptyset(&set);
if (sigprocmask(SIG_SETMASK, &set, NULL) == -1)
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"sigprocmask() failed");
}
/* 初始化随机种子 */
tp = ngx_timeofday();
srandom(((unsigned)ngx_pid << 16) ^ tp->sec ^ tp->msec);
/*
* disable deleting previous events for the listening sockets because
* in the worker processes there are no events at all at this point
*/
ls = cycle->listening.elts;
for (i = 0; i < cycle->listening.nelts; i++)
{
ls[i].previous = NULL;
}
/**
* ngx_event_core_module 定义init_proccess 该ngx_event_process_init方法将
* listening socket注册到事件驱动中,用于接收连接事件
* 重点内容
*/
for (i = 0; cycle->modules[i]; i++)
{
if (cycle->modules[i]->init_process)
{
if (cycle->modules[i]->init_process(cycle) == NGX_ERROR)
{
/* fatal */
exit(2);
}
}
}
/* 关闭除自己以外worker进程的channel通道 */
for (n = 0; n < ngx_last_process; n++)
{
if (ngx_processes[n].pid == -1)
{
continue;
}
if (n == ngx_process_slot)
{//不处理自己所在的线程
continue;
}
if (ngx_processes[n].channel[1] == -1)
{
continue;
}
if (close(ngx_processes[n].channel[1]) == -1)
{//关闭其他子进程socket
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"close() channel failed");
}
}
/* 关闭父进程socket */
if (close(ngx_processes[ngx_process_slot].channel[0]) == -1)
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
"close() channel failed");
}
#if 0
ngx_last_process = 0;
#endif
/* 子进程将socketpair添加到epoll事件对象中 方便以后读取事件 */
if (ngx_add_channel_event(cycle, ngx_channel, NGX_READ_EVENT,
ngx_channel_handler) == NGX_ERROR)
{
/* fatal */
exit(2);
}
}
三、总结
至此,关于Nginx启动流程内容基本介绍完毕,个人认为对于启动流程这部分,我们需要掌握的内容如下:
1、ngx_cycle_t初始化,尤其是重要成员的初始化。2、各个module初始化工作。
3、监听端口创建以及如何添加到事件驱动中。
4、各类信号处理。
从下一篇开始,将介绍Nginx基础数据结构。