Nginx源碼完全注釋(9)nginx.c: ngx_get_options

來源:互聯網
上載者:User

標籤:style   color   io   os   ar   使用   for   sp   檔案   

本文分析 ngxin.c 中的 ngx_get_options 函數,其影響:

nginx.c 中的:

static ngx_uint_t   ngx_show_help;static ngx_uint_t   ngx_show_version;static ngx_uint_t   ngx_show_configure;static u_char      *ngx_prefix;static u_char      *ngx_conf_file;  static u_char      *ngx_conf_params;    static char        *ngx_signal;

ngx_cycle.c 中的:

ngx_uint_t             ngx_test_config;ngx_uint_t             ngx_quiet_mode;

ngx_process_cycle.c(src/os/win32 或 src/os/unix)中的:

ngx_uint_t    ngx_process;

這些變數的範圍由 static 限制為 nginx.c 檔案。ngx_get_options 函數如下:

// 傳入的是 main 函數的兩個參數 argc 和 argvstatic ngx_int_tngx_get_options(int argc, char *const *argv){    u_char     *p;    ngx_int_t   i;    // 對於每一個 argv(注意是從 1 開始,因為 0 是 "nginx")    for (i = 1; i < argc; i++) {        // p 為第 i 個參數的地址        p = (u_char *) argv[i];        //         if (*p++ != ‘-‘) {            ngx_log_stderr(0, "invalid option: \"%s\"", argv[i]);            return NGX_ERROR;        }        // 之所以 while 迴圈是因為一個減號可以帶過個參數,比如 -hV        while (*p) {            // 注意 p 被加 1            switch (*p++) {            // 問號和 h 都是顯示協助資訊和版本資訊            case ‘?‘:            case ‘h‘:                ngx_show_version = 1;                ngx_show_help = 1;                break;            // 小 v 顯示版本資訊            case ‘v‘:                ngx_show_version = 1;                break;            // 大 v 顯示版本資訊和配置資訊            case ‘V‘:                ngx_show_version = 1;                ngx_show_configure = 1;                break;            // t 用於測試組態檔案            case ‘t‘:                ngx_test_config = 1;                break;            // q 表示安靜模式            case ‘q‘:                ngx_quiet_mode = 1;                break;            // p 為指定 prefix path            case ‘p‘:                if (*p) {                    ngx_prefix = p;                    goto next;                }                if (argv[++i]) {                    ngx_prefix = (u_char *) argv[i];                    goto next;                }                ngx_log_stderr(0, "option \"-p\" requires directory name");                return NGX_ERROR;            // 使用指定的設定檔            case ‘c‘:                if (*p) {                    ngx_conf_file = p;                    goto next;                }                if (argv[++i]) {                    ngx_conf_file = (u_char *) argv[i];                    goto next;                }                ngx_log_stderr(0, "option \"-c\" requires file name");                return NGX_ERROR;            // 在設定檔之外設定全域指令            case ‘g‘:                if (*p) {                    ngx_conf_params = p;                    goto next;                }                if (argv[++i]) {                    ngx_conf_params = (u_char *) argv[i];                    goto next;                }                ngx_log_stderr(0, "option \"-g\" requires parameter");                return NGX_ERROR;            // s 為 signal,即給 Nginx 發送訊號            case ‘s‘:                if (*p) { // 下一個參數緊跟在 -s 後,比如 -sstop                    ngx_signal = (char *) p;                } else if (argv[++i]) { // 下一個參數                    ngx_signal = argv[i];                } else { // -s 沒有帶參數時                    ngx_log_stderr(0, "option \"-s\" requires parameter");                    return NGX_ERROR;                }                // 四個訊號分別對應:停止、退出、重新開啟檔案(記錄檔等)、重新載入設定檔                if (ngx_strcmp(ngx_signal, "stop") == 0                    || ngx_strcmp(ngx_signal, "quit") == 0                    || ngx_strcmp(ngx_signal, "reopen") == 0                    || ngx_strcmp(ngx_signal, "reload") == 0)                {                    ngx_process = NGX_PROCESS_SIGNALLER;                    goto next;                }                ngx_log_stderr(0, "invalid option: \"-s %s\"", ngx_signal);                return NGX_ERROR;            default:                ngx_log_stderr(0, "invalid option: \"%c\"", *(p - 1));                return NGX_ERROR;            }        }    next:        continue;    }    return NGX_OK;}

協助資訊如下:

Options:  -?,-h         : this help  -v            : show version and exit  -V            : show version and configure options then exit  -t            : test configuration and exit  -q            : suppress non-error messages during configuration testing  -s signal     : send signal to a master process: stop, quit, reopen, reload  -p prefix     : set prefix path (default: /usr/local/nginx/)  -c filename   : set configuration file (default: conf/nginx.conf)  -g directives : set global directives out of configuration file

v 版本資訊形式如下:

nginx version: nginx/1.3.5

V 版本資訊如下:

nginx version: nginx/1.3.5built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) configure arguments: --with-pcre=/home/michael/packages.d/pcre-8.20 --with-zlib=/home/michael/packages.d/zlib-1.2.7

Nginx源碼完全注釋(9)nginx.c: ngx_get_options

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.