1. 程式人生 > >菜鳥學習Nginx之啟動流程(3)

菜鳥學習Nginx之啟動流程(3)

由於上一篇《菜鳥學習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基礎資料結構