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

本文分析 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.5
built 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
时间: 2024-08-02 09:53:52

Nginx源码完全注释(9)nginx.c: ngx_get_options的相关文章

Nginx 源码完全注释(11)ngx_spinlock

Nginx 是多进程模式的,一个 master 与多个 workers,一般工作在多核 CPU 上,所以自旋锁就是必须用到的.Nginx 中的自旋锁的定义,位于 ngx_spinlock.c 中,如下: void ngx_spinlock(ngx_atomic_t *lock, ngx_atomic_int_t value, ngx_uint_t spin) { #if (NGX_HAVE_ATOMIC_OPS) ngx_uint_t i, n; for ( ;; ) { // lock 即为锁

Nginx源码完全注释(6)core/murmurhash

下面是摘自 Google Code 的 Murmurhash 开源项目主页上的 Murmurhash2,Nginx 就是采用的这个. uint32_t MurmurHash2 ( const void * key, int len, uint32_t seed ) { // 'm' and 'r' are mixing constants generated offline. // They're not really 'magic', they just happen to work well

Nginx源码分析1--------编写Nginx扩展模块

近日来申请通过CSDN准专家,为了顺利在六个月后升级为认证专家,并对得起这个勋章,我感觉 不能松懈博客的更新频率以及质量了.C/C++ windows下的开发是我相对来说做的比较多的地方,对于Linux下的服务器开发 等等也算是半路出家,恰逢近来在研究分布式存储,涉及到了 Nginx 扩展开发以及配置,查阅了好多的资料发现Nginx配置部署起来相当的容易,但是源代码是真的晦涩难懂,经常会看的我们百思不得其解,所以扩展开发Nginx模块也不是一个轻松的事情,那我就从Nginx扩展模块开始,一步一步

Nginx源码完全注释(2)ngx_array.h / ngx_array.c

数组头文件 ngx_array.h #include <ngx_config.h> #include <ngx_core.h> struct ngx_array_s { void *elts; ngx_uint_t nelts; size_t size; ngx_uint_t nalloc; ngx_pool_t *pool; }; ngx_array_t *ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size); vo

Nginx源码完全注释(8)ngx_errno.c

errno.h中的strerror(int errno)可以确定指定的errno的错误的提示信息.在 Nginx 中,将所有错误提示信息预先存储在一个数组里,而预先确定这个数组的大小,是在自动化脚本中完成的,如下是auto/unix脚本:(其中自动化脚本auto/feature的作用参考<解剖 Nginx·自动脚本篇(4)工具型脚本系列>一文) // auto/unix ngx_feature="sys_nerr" ngx_feature_name="NGX_SY

Nginx源码完全注释(5)core/ngx_cpuinfo.c

/* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #include <ngx_config.h> #include <ngx_core.h> // 如果 CPU 架构是 i386 或 amd64,并且编译器是 GNU Compiler 或 Intel Compiler,则定义 cngx_puid 函数 // 否则 ngx_cpuid 函数为空 #if (( __i386__ || __amd64__ ) &a

nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c

首先看 ngx_alloc.h 文件,主要声明或宏定义了 ngx_alloc,ngx_calloc,ngx_memalign,ngx_free. /* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #ifndef _NGX_ALLOC_H_INCLUDED_ #define _NGX_ALLOC_H_INCLUDED_ #include #include void *ngx_alloc(size_t size, ngx_lo

Nginx 源码完全注释(10)ngx_radix_tree

ngx_radix_tree.h // 未被使用的节点 #define NGX_RADIX_NO_VALUE (uintptr_t) -1 typedef struct ngx_radix_node_s ngx_radix_node_t; struct ngx_radix_node_s { ngx_radix_node_t *right; // 右子树的根节点 ngx_radix_node_t *left; // 左子树的根节点 ngx_radix_node_t *parent; // 父节点

Nginx源码完全注释(4)ngx_queue.h / ngx_queue.c

队列头文件ngx_queue.h #include <ngx_config.h> #include <ngx_core.h> #ifndef _NGX_QUEUE_H_INCLUDED_ #define _NGX_QUEUE_H_INCLUDED_ typedef struct ngx_queue_s ngx_queue_t; // 队列的节点,也直接表示队列.注意这是一个双向循环队列 struct ngx_queue_s { ngx_queue_t *prev; ngx_queu