您的位置:首页 > 运维架构 > Nginx

Nginx源码完全注释(9)nginx.c: ngx_get_options

2012-09-29 15:33 2176 查看

Nginx源码完全注释(9)nginx.c: ngx_get_options

作者:柳大·Poechant(钟超)
博客:Blog.CSDN.net/Poechant
邮箱:zhongchao.ustc#gmail.com (# -> @)
日期:2012/09/29
本文分析 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 和 argv
static ngx_int_t
ngx_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
-转载请注明来自柳大·Poechant(钟超)的CSDN博客-
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: