nginx 源码学习笔记(十三)——文件读写和配置文件读取

来源:互联网 发布:最好的数据恢复软件 编辑:程序博客网 时间:2024/05/17 04:52

对于c语言来说,如果需要支持多个操作系统,就需要封装一下文件的读写。封装文件的读写还有一个益处就是能够把读写异常,读写的内存控制,日志的记录封装起来,以便于其他的模块更好的应用。文件的读写一般会封装成打开文件,关闭打开的文件,读写文件。

在nginx的源码中,文件读写主要放在core/ngx_file.c,core/ngx_file.h,src/os/unix/ngx_files.h和src/os/unix/ngx_files.c中。由于nginx的文件读写函数较多,我们只是详细介绍比较重要,经常使用,实现很有技巧的函数。

[html] view plaincopyprint?
  1. typedef struct {  
  2.     ngx_str_t                  name;             //路径数据字符串  
  3.     size_t                     len;              //目录长度  
  4.     size_t                     level[3];         //临时目录的三级目录大小  
  5.   
  6.     ngx_path_manager_pt        manager;  
  7.     ngx_path_loader_pt         loader;  
  8.     void                      *data;              
  9.   
  10.     u_char                    *conf_file;        //该路径的来源的配置文件  
  11.     ngx_uint_t                 line;             //该路径在来源的配置文件中的行数,主要用于记录日志,排查错误  
  12. } ngx_path_t;  


我们首先介绍简单封装的函数,再介绍复杂封装的函数,简单封装的函数有如下函数:
ngx_open_file/ngx_close_file

[cpp] view plaincopyprint?
  1. src/os/unix/ngx_files.h  
  2. #define ngx_open_file(name, mode, create, access)                            \  
  3.     open((const char *) name, mode|create|O_BINARY, access)  
  4.   
  5. #else  
  6.   
  7. #define ngx_open_file(name, mode, create, access)                            \  
  8.     open((const char *) name, mode|create, access)  
  9.   
  10. #endif  
  11.   
  12. #define ngx_close_file           close  


这里可以看到单纯的对c语言的open和close进行了封装,是不是非常简单呢?

[cpp] view plaincopyprint?
  1. src/os/unix/ngx_files.h  
  2. #define ngx_delete_file(name)    unlink((const char *) name)  


ngx_delete_file删除文件函数,调用系统的unlink函数,unlink()会删除pathname指定的文件。如果该文件名最后连接点,但有其他进程打开了此文件,则在所有关于此文件的文件描述词皆关闭后才会删除。如果参数pathname为一符号连接(symboliclink),则此连接会被删除。

在ngx_files.c文件中存在大量的文件读写操作,这里就不一一讲解;

配置文件一般有三个作用:

根据配置文件启用某段程序,

用配置文件传入参数,

设定启动程序程序之间的相互关系。

Nginx的配置文件也实现了这三部分的功能,nginx的配置文件主要有以下几个主要的结构体和函数配置文件的结构体有:

结构体说明
ngx_command_s           定义了配置文件中的 tag的配置,以及遇到该tag该怎么处理的函数
ngx_open_file_s            定义了打开文件的参数的结构体
ngx_conf_file_t             定义了缓存配置文件的数据的结构体
ngx_conf_s                    定义了配置文件解析过程中需要缓存的数据

ngx_command_s          这个结构体定义了配置文件中的tag,以及遇到该tag,该怎么处理,其结构如下表:

结构体说明
name 配置文件中的
tag
Type                    
配置类型,这个参数中会定义这个配置是什么范围内的配置(核心配置或是普通配置),以及有多少参数, 是块配置,还是行配置
Set                       解析该配置的函数
ConfOffset           指定配置存储的位置
Post                     指向模块在读配置的时候需要的一些零碎变量。一般它是NULL

在每一个ngx_command_s数组的结尾必须有一个ngx_null_command,以用于判断是否结束。
配置文件解析的入口为ngx_conf_parse,如果了解了ngx_conf_parse,对nginx的配置文件的原理就有了基本的了解,下面我们详细的解释一下

[cpp] view plaincopyprint?
  1. src/ngx_conf_file.h  
  2.   
  3. char *  
  4. ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename)   
  5. //cf是输入参数也是输出参数,在文件名为空的情况下,第二个  
  6. //参数是文件名,可能为空,一般在解析配置块或是一行的时候。  
  7. {  
  8.     char             *rv;  
  9.     ngx_fd_t          fd;  
  10.     ngx_int_t         rc;  
  11.     ngx_buf_t         buf;  
  12.     ngx_conf_file_t  *prev, conf_file;  
  13.     enum {  
  14.         parse_file = 0,  
  15.         parse_block,  
  16.         parse_param  
  17.     } type;  
  18.   
  19. #if (NGX_SUPPRESS_WARN)  
  20.     fd = NGX_INVALID_FILE;  
  21.     prev = NULL;  
  22. #endif  
  23.   
  24.     if (filename) { //如果文件名不为空 则打开配置文件  
  25.   
  26.         /* open configuration file */  
  27.   
  28.         fd = ngx_open_file(filename->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);<span style="color:#ff0000;">//打开配置文件</span>  
  29.         if (fd == NGX_INVALID_FILE) {  
  30.             ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,  
  31.                                ngx_open_file_n " \"%s\" failed",  
  32.                                filename->data);  
  33.             return NGX_CONF_ERROR;  
  34.         }  
  35.   
  36.         prev = cf->conf_file;  
  37.   
  38.         cf->conf_file = &conf_file;  
  39.   
  40.         if (ngx_fd_info(fd, &cf->conf_file->file.info) == -1) {  
  41.             ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno,  
  42.                           ngx_fd_info_n " \"%s\" failed", filename->data);  
  43.         }  
  44.   
  45.         cf->conf_file->buffer = &buf;  
  46.   
  47.         buf.start = ngx_alloc(NGX_CONF_BUFFER, cf->log);  
  48.         if (buf.start == NULL) {  
  49.             goto failed;  
  50.         }  
  51.   
  52.         buf.pos = buf.start;  
  53.         buf.last = buf.start;  
  54.         buf.end = buf.last + NGX_CONF_BUFFER;  
  55.         buf.temporary = 1;  
  56.   
  57.         cf->conf_file->file.fd = fd;  
  58.         cf->conf_file->file.name.len = filename->len;  
  59.         cf->conf_file->file.name.data = filename->data;  
  60.         cf->conf_file->file.offset = 0;  
  61.         cf->conf_file->file.log = cf->log;  
  62.         cf->conf_file->line = 1;  
  63.   
  64.         type = parse_file;  
  65.   
  66.     } else if (cf->conf_file->file.fd != NGX_INVALID_FILE) {  
  67.   
  68.         type = parse_block;  
  69.   
  70.     } else {  
  71.         type = parse_param;  
  72.     }  
  73.   
  74.   
  75.     for ( ;; ) {  
  76.         rc = ngx_conf_read_token(cf);      
  77.        //ngx_conf_read_token函数的主要功能是读取并解析配置文件,  
  78.         //解析的参数存到cf->args中,读取到";"标点则返回  
  79.         //读取到"{",则接着读取,读取到"}",这直接完成。  
  80.   
  81.         /* 
  82.          * ngx_conf_read_token() may return 
  83.          * 
  84.          *    NGX_ERROR             there is error 
  85.          *    NGX_OK                the token terminated by ";" was found 
  86.          *    NGX_CONF_BLOCK_START  the token terminated by "{" was found 
  87.          *    NGX_CONF_BLOCK_DONE   the "}" was found 
  88.          *    NGX_CONF_FILE_DONE    the configuration file is done 
  89.          */  
  90.   
  91.         if (rc == NGX_ERROR) {  
  92.             goto done;                    //如果错误 结束  
  93.         }  
  94.   
  95.         if (rc == NGX_CONF_BLOCK_DONE) {   //如果发现“}”  
  96.   
  97.             if (type != parse_block) {  
  98.                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "unexpected \"}\"");  
  99.                 goto failed;  
  100.             }  
  101.   
  102.             goto done;  
  103.         }  
  104.   
  105.         if (rc == NGX_CONF_FILE_DONE) {     //如果发现文件结束  
  106.   
  107.             if (type == parse_block) {  
  108.                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,  
  109.                                    "unexpected end of file, expecting \"}\"");  
  110.                 goto failed;  
  111.             }  
  112.   
  113.             goto done;  
  114.         }  
  115.   
  116.         if (rc == NGX_CONF_BLOCK_START) {   //如果发现“{”  
  117.   
  118.             if (type == parse_param) {  
  119.                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,  
  120.                                    "block directives are not supported "  
  121.                                    "in -g option");  
  122.                 goto failed;  
  123.             }  
  124.         }  
  125.   
  126.         /* rc == NGX_OK || rc == NGX_CONF_BLOCK_START */  
  127.   
  128.         if (cf->handler) {  //这个函数只在模块的时候使用type  
  129.   
  130.             /* 
  131.              * the custom handler, i.e., that is used in the http's 
  132.              * "types { ... }" directive 
  133.              */  
  134.   
  135.             rv = (*cf->handler)(cf, NULL, cf->handler_conf);   
  136.             if (rv == NGX_CONF_OK) {  
  137.                 continue;  
  138.             }  
  139.   
  140.             if (rv == NGX_CONF_ERROR) {  
  141.                 goto failed;  
  142.             }  
  143.   
  144.             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, rv);  
  145.   
  146.             goto failed;  
  147.         }  
  148.   
  149.   
  150.         rc = ngx_conf_handler(cf, rc);  
  151.         //ngx_conf_handler函数主要功能是先验证配置有没有问题,然后调用cmd->set函数  
  152.         if (rc == NGX_ERROR) {  
  153.             goto failed;  
  154.         }  
  155.     }  
  156.   
  157. failed:  
  158.   
  159.     rc = NGX_ERROR;  
  160.   
  161. done:  
  162.   
  163.     if (filename) {  
  164.         if (cf->conf_file->buffer->start) {  
  165.             ngx_free(cf->conf_file->buffer->start);  
  166.         }  
  167.   
  168.         if (ngx_close_file(fd) == NGX_FILE_ERROR) { //关闭文件  
  169.             ngx_log_error(NGX_LOG_ALERT, cf->log, ngx_errno,  
  170.                           ngx_close_file_n " %s failed",  
  171.                           filename->data);  
  172.             return NGX_CONF_ERROR;  
  173.         }  
  174.   
  175.         cf->conf_file = prev;  
  176.     }  
  177.   
  178.     if (rc == NGX_ERROR) {  
  179.         return NGX_CONF_ERROR;  
  180.     }  
  181.   
  182.     return NGX_CONF_OK;  
  183. }  



ngx_conf_parse函数除了在ngx_init_cycle函数中调用外,在配置块的解析中,即cmd->set函数(例如解析http配置块的ngx_http_block函数)中也会调用该函数,从某种程度上理解该函数其实是一个递归函数,只不过中间调用了其他函数。在讲解http的配置文件那一章我们会根据http的配置例子详细的讲解。

0 0
原创粉丝点击