Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Maxim Dounin
0005  * Copyright (C) Nginx, Inc.
0006  */
0007 
0008 
0009 #include <ngx_config.h>
0010 #include <ngx_core.h>
0011 
0012 
0013 #define NGX_MAX_DYNAMIC_MODULES  128
0014 
0015 
0016 static ngx_uint_t ngx_module_index(ngx_cycle_t *cycle);
0017 static ngx_uint_t ngx_module_ctx_index(ngx_cycle_t *cycle, ngx_uint_t type,
0018     ngx_uint_t index);
0019 
0020 
0021 ngx_uint_t         ngx_max_module;
0022 static ngx_uint_t  ngx_modules_n;
0023 
0024 
0025 ngx_int_t
0026 ngx_preinit_modules(void)
0027 {
0028     ngx_uint_t  i;
0029 
0030     for (i = 0; ngx_modules[i]; i++) {
0031         ngx_modules[i]->index = i;
0032         ngx_modules[i]->name = ngx_module_names[i];
0033     }
0034 
0035     ngx_modules_n = i;
0036     ngx_max_module = ngx_modules_n + NGX_MAX_DYNAMIC_MODULES;
0037 
0038     return NGX_OK;
0039 }
0040 
0041 
0042 ngx_int_t
0043 ngx_cycle_modules(ngx_cycle_t *cycle)
0044 {
0045     /*
0046      * create a list of modules to be used for this cycle,
0047      * copy static modules to it
0048      */
0049 
0050     cycle->modules = ngx_pcalloc(cycle->pool, (ngx_max_module + 1)
0051                                               * sizeof(ngx_module_t *));
0052     if (cycle->modules == NULL) {
0053         return NGX_ERROR;
0054     }
0055 
0056     ngx_memcpy(cycle->modules, ngx_modules,
0057                ngx_modules_n * sizeof(ngx_module_t *));
0058 
0059     cycle->modules_n = ngx_modules_n;
0060 
0061     return NGX_OK;
0062 }
0063 
0064 
0065 ngx_int_t
0066 ngx_init_modules(ngx_cycle_t *cycle)
0067 {
0068     ngx_uint_t  i;
0069 
0070     for (i = 0; cycle->modules[i]; i++) {
0071         if (cycle->modules[i]->init_module) {
0072             if (cycle->modules[i]->init_module(cycle) != NGX_OK) {
0073                 return NGX_ERROR;
0074             }
0075         }
0076     }
0077 
0078     return NGX_OK;
0079 }
0080 
0081 
0082 ngx_int_t
0083 ngx_count_modules(ngx_cycle_t *cycle, ngx_uint_t type)
0084 {
0085     ngx_uint_t     i, next, max;
0086     ngx_module_t  *module;
0087 
0088     next = 0;
0089     max = 0;
0090 
0091     /* count appropriate modules, set up their indices */
0092 
0093     for (i = 0; cycle->modules[i]; i++) {
0094         module = cycle->modules[i];
0095 
0096         if (module->type != type) {
0097             continue;
0098         }
0099 
0100         if (module->ctx_index != NGX_MODULE_UNSET_INDEX) {
0101 
0102             /* if ctx_index was assigned, preserve it */
0103 
0104             if (module->ctx_index > max) {
0105                 max = module->ctx_index;
0106             }
0107 
0108             if (module->ctx_index == next) {
0109                 next++;
0110             }
0111 
0112             continue;
0113         }
0114 
0115         /* search for some free index */
0116 
0117         module->ctx_index = ngx_module_ctx_index(cycle, type, next);
0118 
0119         if (module->ctx_index > max) {
0120             max = module->ctx_index;
0121         }
0122 
0123         next = module->ctx_index + 1;
0124     }
0125 
0126     /*
0127      * make sure the number returned is big enough for previous
0128      * cycle as well, else there will be problems if the number
0129      * will be stored in a global variable (as it's used to be)
0130      * and we'll have to roll back to the previous cycle
0131      */
0132 
0133     if (cycle->old_cycle && cycle->old_cycle->modules) {
0134 
0135         for (i = 0; cycle->old_cycle->modules[i]; i++) {
0136             module = cycle->old_cycle->modules[i];
0137 
0138             if (module->type != type) {
0139                 continue;
0140             }
0141 
0142             if (module->ctx_index > max) {
0143                 max = module->ctx_index;
0144             }
0145         }
0146     }
0147 
0148     /* prevent loading of additional modules */
0149 
0150     cycle->modules_used = 1;
0151 
0152     return max + 1;
0153 }
0154 
0155 
0156 ngx_int_t
0157 ngx_add_module(ngx_conf_t *cf, ngx_str_t *file, ngx_module_t *module,
0158     char **order)
0159 {
0160     void               *rv;
0161     ngx_uint_t          i, m, before;
0162     ngx_core_module_t  *core_module;
0163 
0164     if (cf->cycle->modules_n >= ngx_max_module) {
0165         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0166                            "too many modules loaded");
0167         return NGX_ERROR;
0168     }
0169 
0170     if (module->version != nginx_version) {
0171         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0172                            "module \"%V\" version %ui instead of %ui",
0173                            file, module->version, (ngx_uint_t) nginx_version);
0174         return NGX_ERROR;
0175     }
0176 
0177     if (ngx_strcmp(module->signature, NGX_MODULE_SIGNATURE) != 0) {
0178         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0179                            "module \"%V\" is not binary compatible",
0180                            file);
0181         return NGX_ERROR;
0182     }
0183 
0184     for (m = 0; cf->cycle->modules[m]; m++) {
0185         if (ngx_strcmp(cf->cycle->modules[m]->name, module->name) == 0) {
0186             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0187                                "module \"%s\" is already loaded",
0188                                module->name);
0189             return NGX_ERROR;
0190         }
0191     }
0192 
0193     /*
0194      * if the module wasn't previously loaded, assign an index
0195      */
0196 
0197     if (module->index == NGX_MODULE_UNSET_INDEX) {
0198         module->index = ngx_module_index(cf->cycle);
0199 
0200         if (module->index >= ngx_max_module) {
0201             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0202                                "too many modules loaded");
0203             return NGX_ERROR;
0204         }
0205     }
0206 
0207     /*
0208      * put the module into the cycle->modules array
0209      */
0210 
0211     before = cf->cycle->modules_n;
0212 
0213     if (order) {
0214         for (i = 0; order[i]; i++) {
0215             if (ngx_strcmp(order[i], module->name) == 0) {
0216                 i++;
0217                 break;
0218             }
0219         }
0220 
0221         for ( /* void */ ; order[i]; i++) {
0222 
0223 #if 0
0224             ngx_log_debug2(NGX_LOG_DEBUG_CORE, cf->log, 0,
0225                            "module: %s before %s",
0226                            module->name, order[i]);
0227 #endif
0228 
0229             for (m = 0; m < before; m++) {
0230                 if (ngx_strcmp(cf->cycle->modules[m]->name, order[i]) == 0) {
0231 
0232                     ngx_log_debug3(NGX_LOG_DEBUG_CORE, cf->log, 0,
0233                                    "module: %s before %s:%i",
0234                                    module->name, order[i], m);
0235 
0236                     before = m;
0237                     break;
0238                 }
0239             }
0240         }
0241     }
0242 
0243     /* put the module before modules[before] */
0244 
0245     if (before != cf->cycle->modules_n) {
0246         ngx_memmove(&cf->cycle->modules[before + 1],
0247                     &cf->cycle->modules[before],
0248                     (cf->cycle->modules_n - before) * sizeof(ngx_module_t *));
0249     }
0250 
0251     cf->cycle->modules[before] = module;
0252     cf->cycle->modules_n++;
0253 
0254     if (module->type == NGX_CORE_MODULE) {
0255 
0256         /*
0257          * we are smart enough to initialize core modules;
0258          * other modules are expected to be loaded before
0259          * initialization - e.g., http modules must be loaded
0260          * before http{} block
0261          */
0262 
0263         core_module = module->ctx;
0264 
0265         if (core_module->create_conf) {
0266             rv = core_module->create_conf(cf->cycle);
0267             if (rv == NULL) {
0268                 return NGX_ERROR;
0269             }
0270 
0271             cf->cycle->conf_ctx[module->index] = rv;
0272         }
0273     }
0274 
0275     return NGX_OK;
0276 }
0277 
0278 
0279 static ngx_uint_t
0280 ngx_module_index(ngx_cycle_t *cycle)
0281 {
0282     ngx_uint_t     i, index;
0283     ngx_module_t  *module;
0284 
0285     index = 0;
0286 
0287 again:
0288 
0289     /* find an unused index */
0290 
0291     for (i = 0; cycle->modules[i]; i++) {
0292         module = cycle->modules[i];
0293 
0294         if (module->index == index) {
0295             index++;
0296             goto again;
0297         }
0298     }
0299 
0300     /* check previous cycle */
0301 
0302     if (cycle->old_cycle && cycle->old_cycle->modules) {
0303 
0304         for (i = 0; cycle->old_cycle->modules[i]; i++) {
0305             module = cycle->old_cycle->modules[i];
0306 
0307             if (module->index == index) {
0308                 index++;
0309                 goto again;
0310             }
0311         }
0312     }
0313 
0314     return index;
0315 }
0316 
0317 
0318 static ngx_uint_t
0319 ngx_module_ctx_index(ngx_cycle_t *cycle, ngx_uint_t type, ngx_uint_t index)
0320 {
0321     ngx_uint_t     i;
0322     ngx_module_t  *module;
0323 
0324 again:
0325 
0326     /* find an unused ctx_index */
0327 
0328     for (i = 0; cycle->modules[i]; i++) {
0329         module = cycle->modules[i];
0330 
0331         if (module->type != type) {
0332             continue;
0333         }
0334 
0335         if (module->ctx_index == index) {
0336             index++;
0337             goto again;
0338         }
0339     }
0340 
0341     /* check previous cycle */
0342 
0343     if (cycle->old_cycle && cycle->old_cycle->modules) {
0344 
0345         for (i = 0; cycle->old_cycle->modules[i]; i++) {
0346             module = cycle->old_cycle->modules[i];
0347 
0348             if (module->type != type) {
0349                 continue;
0350             }
0351 
0352             if (module->ctx_index == index) {
0353                 index++;
0354                 goto again;
0355             }
0356         }
0357     }
0358 
0359     return index;
0360 }