|
| 1 | +/* |
| 2 | + * Copyright (C) Yichun Zhang (agentzh) |
| 3 | + */ |
| 4 | + |
| 5 | +#include <ngx_config.h> |
| 6 | + |
| 7 | +#include "ngx_meta_lua_shdict.h" |
| 8 | + |
| 9 | + |
| 10 | +static void *ngx_meta_lua_module_create_conf(ngx_cycle_t *cycle); |
| 11 | +static char *ngx_meta_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
| 12 | +static ngx_int_t ngx_meta_lua_run_init_handler(ngx_cycle_t *cycle, |
| 13 | + ngx_log_t *log, ngx_meta_lua_init_handler_t *inh); |
| 14 | + |
| 15 | + |
| 16 | +static ngx_command_t ngx_meta_lua_cmds[] = { |
| 17 | + |
| 18 | + { ngx_string("lua"), |
| 19 | + NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, |
| 20 | + ngx_meta_lua_block, |
| 21 | + 0, |
| 22 | + 0, |
| 23 | + NULL }, |
| 24 | + |
| 25 | + { ngx_string("lua_shared_dict"), |
| 26 | + NGX_META_LUA_CONF|NGX_CONF_TAKE2, |
| 27 | + ngx_meta_lua_shdict_directive, |
| 28 | + 0, |
| 29 | + 0, |
| 30 | + NULL }, |
| 31 | + |
| 32 | + ngx_null_command |
| 33 | +}; |
| 34 | + |
| 35 | + |
| 36 | +static ngx_core_module_t ngx_meta_lua_module_ctx = { |
| 37 | + ngx_string("lua"), |
| 38 | + ngx_meta_lua_module_create_conf, |
| 39 | + NULL |
| 40 | +}; |
| 41 | + |
| 42 | + |
| 43 | +ngx_module_t ngx_meta_lua_module = { |
| 44 | + NGX_MODULE_V1, |
| 45 | + &ngx_meta_lua_module_ctx, /* module context */ |
| 46 | + ngx_meta_lua_cmds, /* module directives */ |
| 47 | + NGX_CORE_MODULE, /* module type */ |
| 48 | + NULL, /* init master */ |
| 49 | + NULL, /* init module */ |
| 50 | + NULL, /* init process */ |
| 51 | + NULL, /* init thread */ |
| 52 | + NULL, /* exit thread */ |
| 53 | + NULL, /* exit process */ |
| 54 | + NULL, /* exit master */ |
| 55 | + NGX_MODULE_V1_PADDING |
| 56 | +}; |
| 57 | + |
| 58 | + |
| 59 | +static void * |
| 60 | +ngx_meta_lua_module_create_conf(ngx_cycle_t *cycle) |
| 61 | +{ |
| 62 | + ngx_meta_lua_conf_t *mcf; |
| 63 | + |
| 64 | + mcf = ngx_pcalloc(cycle->pool, sizeof(ngx_meta_lua_conf_t)); |
| 65 | + if (mcf == NULL) { |
| 66 | + return NULL; |
| 67 | + } |
| 68 | + |
| 69 | + mcf->shdict_zones = ngx_array_create(cycle->pool, 2, |
| 70 | + sizeof(ngx_shm_zone_t *)); |
| 71 | + if (mcf->shdict_zones == NULL) { |
| 72 | + return NULL; |
| 73 | + } |
| 74 | + |
| 75 | + mcf->init_handlers = ngx_array_create(cycle->pool, 2, |
| 76 | + sizeof(ngx_meta_lua_init_handler_t *)); |
| 77 | + if (mcf->init_handlers == NULL) { |
| 78 | + return NULL; |
| 79 | + } |
| 80 | + |
| 81 | + return mcf; |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +static char * |
| 86 | +ngx_meta_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
| 87 | +{ |
| 88 | + char *rv; |
| 89 | + ngx_conf_t pcf; |
| 90 | + ngx_meta_lua_conf_t *mcf = *(ngx_meta_lua_conf_t **) conf; |
| 91 | + |
| 92 | + if (mcf->parsed_lua_block) { |
| 93 | + return "is duplicate"; |
| 94 | + } |
| 95 | + |
| 96 | + /* parse the lua{} block */ |
| 97 | + |
| 98 | + mcf->parsed_lua_block = 1; |
| 99 | + |
| 100 | + pcf = *cf; |
| 101 | + |
| 102 | + cf->ctx = mcf; |
| 103 | + cf->module_type = NGX_CORE_MODULE; |
| 104 | + cf->cmd_type = NGX_META_LUA_CONF; |
| 105 | + |
| 106 | + rv = ngx_conf_parse(cf, NULL); |
| 107 | + |
| 108 | + *cf = pcf; |
| 109 | + |
| 110 | + if (rv != NGX_CONF_OK) { |
| 111 | + return NGX_CONF_ERROR; |
| 112 | + } |
| 113 | + |
| 114 | + return NGX_CONF_OK; |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +ngx_int_t |
| 119 | +ngx_meta_lua_post_init_handler(ngx_conf_t *cf, |
| 120 | + ngx_meta_lua_main_conf_handler_pt init_handler, ngx_str_t init_src, |
| 121 | + lua_State *L) |
| 122 | +{ |
| 123 | + ngx_meta_lua_conf_t *mcf; |
| 124 | + ngx_meta_lua_init_handler_t **inhp, *inh, iinh; |
| 125 | + |
| 126 | + if (init_handler == NULL) { |
| 127 | + return NGX_OK; |
| 128 | + } |
| 129 | + |
| 130 | + mcf = ngx_meta_lua_conf_get_main_conf(cf); |
| 131 | + |
| 132 | + if (!mcf->delay_init_handlers) { |
| 133 | + iinh.init_handler = init_handler; |
| 134 | + iinh.init_src = init_src; |
| 135 | + iinh.L = L; |
| 136 | + |
| 137 | + return ngx_meta_lua_run_init_handler(cf->cycle, cf->log, &iinh); |
| 138 | + } |
| 139 | + |
| 140 | + inh = ngx_palloc(cf->pool, sizeof(ngx_meta_lua_init_handler_t)); |
| 141 | + if (inh == NULL) { |
| 142 | + return NGX_ERROR; |
| 143 | + } |
| 144 | + |
| 145 | + inh->init_handler = init_handler; |
| 146 | + inh->init_src = init_src; |
| 147 | + inh->L = L; |
| 148 | + |
| 149 | + inhp = ngx_array_push(mcf->init_handlers); |
| 150 | + if (inhp == NULL) { |
| 151 | + return NGX_ERROR; |
| 152 | + } |
| 153 | + |
| 154 | + *inhp = inh; |
| 155 | + |
| 156 | + return NGX_OK; |
| 157 | +} |
| 158 | + |
| 159 | + |
| 160 | +ngx_int_t |
| 161 | +ngx_meta_lua_run_delayed_init_handlers(ngx_meta_lua_conf_t *mcf, |
| 162 | + ngx_cycle_t *cycle, ngx_log_t *log) |
| 163 | +{ |
| 164 | + ngx_uint_t i; |
| 165 | + ngx_meta_lua_init_handler_t **inhp; |
| 166 | + |
| 167 | + inhp = mcf->init_handlers->elts; |
| 168 | + |
| 169 | + /* respect order in which the modules were compiled */ |
| 170 | + |
| 171 | + for (i = 0; i < mcf->init_handlers->nelts; i++) { |
| 172 | + ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, |
| 173 | + "lua run delayed init_handler: %p", inhp[i]); |
| 174 | + |
| 175 | + if (ngx_meta_lua_run_init_handler(cycle, log, inhp[i]) != NGX_OK) { |
| 176 | + return NGX_ERROR; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + return NGX_OK; |
| 181 | +} |
| 182 | + |
| 183 | + |
| 184 | +static ngx_int_t |
| 185 | +ngx_meta_lua_run_init_handler(ngx_cycle_t *cycle, ngx_log_t *log, |
| 186 | + ngx_meta_lua_init_handler_t *inh) |
| 187 | +{ |
| 188 | + volatile ngx_cycle_t *saved_cycle; |
| 189 | + ngx_int_t rc; |
| 190 | + |
| 191 | + saved_cycle = ngx_cycle; |
| 192 | + ngx_cycle = cycle; |
| 193 | + |
| 194 | + rc = inh->init_handler(log, inh->init_src, inh->L); |
| 195 | + |
| 196 | + ngx_cycle = saved_cycle; |
| 197 | + |
| 198 | + if (rc != NGX_OK) { |
| 199 | + return NGX_ERROR; |
| 200 | + } |
| 201 | + |
| 202 | + return NGX_OK; |
| 203 | +} |
| 204 | + |
| 205 | + |
| 206 | +/* vi:set ft=c ts=4 sw=4 et fdm=marker: */ |
0 commit comments