|
| 1 | +/* |
| 2 | + * This fake module was used to reproduce a bug in ngx_lua's init_worker_by_lua |
| 3 | + * implementation. The bug would cause this module's main_conf->a to be reset |
| 4 | + * to 0 due to init_worker_by_lua invoking merge_loc_conf with a brand new |
| 5 | + * loc_conf, whose a member would be 0. |
| 6 | + * The test case for this bug is in 124-init-worker.t. |
| 7 | + */ |
| 8 | + |
| 9 | + |
| 10 | +#include <ngx_config.h> |
| 11 | +#include <ngx_core.h> |
| 12 | +#include <ngx_http.h> |
| 13 | +#include <nginx.h> |
| 14 | + |
| 15 | + |
| 16 | +typedef struct { |
| 17 | + ngx_flag_t a; |
| 18 | +} ngx_http_fake_merge_main_conf_t; |
| 19 | + |
| 20 | + |
| 21 | +typedef struct { |
| 22 | + ngx_flag_t a; |
| 23 | +} ngx_http_fake_merge_loc_conf_t; |
| 24 | + |
| 25 | + |
| 26 | +static ngx_int_t ngx_http_fake_merge_var(ngx_http_request_t *r, |
| 27 | + ngx_http_variable_value_t *v, uintptr_t data); |
| 28 | +static ngx_int_t ngx_http_fake_merge_add_variables(ngx_conf_t *cf); |
| 29 | +static ngx_int_t ngx_http_fake_merge_init(ngx_conf_t *cf); |
| 30 | +static void *ngx_http_fake_merge_create_main_conf(ngx_conf_t *cf); |
| 31 | +static void *ngx_http_fake_merge_create_loc_conf(ngx_conf_t *cf); |
| 32 | +static char *ngx_http_fake_merge_merge_loc_conf(ngx_conf_t *cf, void *prev, |
| 33 | + void *conf); |
| 34 | + |
| 35 | + |
| 36 | +static ngx_http_module_t ngx_http_fake_merge_module_ctx = { |
| 37 | + ngx_http_fake_merge_init, /* preconfiguration */ |
| 38 | + NULL, /* postconfiguration */ |
| 39 | + |
| 40 | + ngx_http_fake_merge_create_main_conf, /* create main configuration */ |
| 41 | + NULL, /* init main configuration */ |
| 42 | + |
| 43 | + NULL, /* create server configuration */ |
| 44 | + NULL, /* merge server configuration */ |
| 45 | + |
| 46 | + ngx_http_fake_merge_create_loc_conf, /* create location configuration */ |
| 47 | + ngx_http_fake_merge_merge_loc_conf /* merge location configuration */ |
| 48 | +}; |
| 49 | + |
| 50 | + |
| 51 | +ngx_module_t ngx_http_fake_merge_module = { |
| 52 | + NGX_MODULE_V1, |
| 53 | + &ngx_http_fake_merge_module_ctx, /* module context */ |
| 54 | + NULL, /* module directives */ |
| 55 | + NGX_HTTP_MODULE, /* module type */ |
| 56 | + NULL, /* init master */ |
| 57 | + NULL, /* init module */ |
| 58 | + NULL, /* init process */ |
| 59 | + NULL, /* init thread */ |
| 60 | + NULL, /* exit thread */ |
| 61 | + NULL, /* exit process */ |
| 62 | + NULL, /* exit master */ |
| 63 | + NGX_MODULE_V1_PADDING |
| 64 | +}; |
| 65 | + |
| 66 | + |
| 67 | +static ngx_http_variable_t ngx_http_fake_merge_variables[] = { |
| 68 | + |
| 69 | + { ngx_string("fake_var"), NULL, |
| 70 | + ngx_http_fake_merge_var, 0, |
| 71 | + NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
| 72 | + |
| 73 | + { ngx_null_string, NULL, NULL, 0, 0, 0 } |
| 74 | +}; |
| 75 | + |
| 76 | + |
| 77 | +static ngx_int_t |
| 78 | +ngx_http_fake_merge_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, |
| 79 | + uintptr_t data) |
| 80 | +{ |
| 81 | + static char *str[] = {"0", "1"}; |
| 82 | + ngx_http_fake_merge_main_conf_t *fmcf; |
| 83 | + |
| 84 | + fmcf = ngx_http_get_module_main_conf(r, ngx_http_fake_merge_module); |
| 85 | + if (fmcf == NULL) { |
| 86 | + return NGX_ERROR; |
| 87 | + } |
| 88 | + |
| 89 | + v->len = 1; |
| 90 | + v->data = (u_char *) str[fmcf->a]; |
| 91 | + v->valid = 1; |
| 92 | + v->no_cacheable = 0; |
| 93 | + v->not_found = 0; |
| 94 | + |
| 95 | + return NGX_OK; |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +static ngx_int_t |
| 100 | +ngx_http_fake_merge_add_variables(ngx_conf_t *cf) |
| 101 | +{ |
| 102 | + ngx_http_variable_t *var, *v; |
| 103 | + |
| 104 | + for (v = ngx_http_fake_merge_variables; v->name.len; v++) { |
| 105 | + var = ngx_http_add_variable(cf, &v->name, v->flags); |
| 106 | + if (var == NULL) { |
| 107 | + return NGX_ERROR; |
| 108 | + } |
| 109 | + |
| 110 | + var->get_handler = v->get_handler; |
| 111 | + var->data = v->data; |
| 112 | + } |
| 113 | + |
| 114 | + return NGX_OK; |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +static ngx_int_t |
| 119 | +ngx_http_fake_merge_init(ngx_conf_t *cf) |
| 120 | +{ |
| 121 | + ngx_http_fake_merge_loc_conf_t *flcf; |
| 122 | + |
| 123 | + flcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_fake_merge_module); |
| 124 | + if (flcf == NULL) { |
| 125 | + return NGX_ERROR; |
| 126 | + } |
| 127 | + |
| 128 | + if (ngx_http_fake_merge_add_variables(cf) != NGX_OK) { |
| 129 | + return NGX_ERROR; |
| 130 | + } |
| 131 | + |
| 132 | + flcf->a = 1; |
| 133 | + |
| 134 | + return NGX_OK; |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +static void * |
| 139 | +ngx_http_fake_merge_create_main_conf(ngx_conf_t *cf) |
| 140 | +{ |
| 141 | + ngx_http_fake_merge_main_conf_t *fmcf; |
| 142 | + |
| 143 | + fmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_fake_merge_main_conf_t)); |
| 144 | + if (fmcf == NULL) { |
| 145 | + return NULL; |
| 146 | + } |
| 147 | + |
| 148 | + fmcf->a = NGX_CONF_UNSET; |
| 149 | + |
| 150 | + return fmcf; |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +static void * |
| 155 | +ngx_http_fake_merge_create_loc_conf(ngx_conf_t *cf) |
| 156 | +{ |
| 157 | + ngx_http_fake_merge_loc_conf_t *flcf; |
| 158 | + |
| 159 | + flcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_fake_merge_loc_conf_t)); |
| 160 | + if (flcf == NULL) { |
| 161 | + return NULL; |
| 162 | + } |
| 163 | + |
| 164 | + flcf->a = NGX_CONF_UNSET; |
| 165 | + |
| 166 | + return flcf; |
| 167 | +} |
| 168 | + |
| 169 | + |
| 170 | +static char * |
| 171 | +ngx_http_fake_merge_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) |
| 172 | +{ |
| 173 | + ngx_http_fake_merge_loc_conf_t *conf = child; |
| 174 | + ngx_http_fake_merge_loc_conf_t *prev = parent; |
| 175 | + ngx_http_fake_merge_main_conf_t *fmcf; |
| 176 | + |
| 177 | + ngx_conf_merge_value(conf->a, prev->a, 0); |
| 178 | + |
| 179 | + fmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_fake_merge_module); |
| 180 | + if (fmcf == NULL) { |
| 181 | + return NGX_CONF_ERROR; |
| 182 | + } |
| 183 | + |
| 184 | + fmcf->a = conf->a; |
| 185 | + |
| 186 | + return NGX_CONF_OK; |
| 187 | +} |
0 commit comments