Skip to content

Commit

Permalink
bugfix: use of C global variables at config time could lead to issues…
Browse files Browse the repository at this point in the history
… when HUP reload failed in the middle.
  • Loading branch information
agentzh committed Sep 3, 2013
1 parent 7acbd7b commit fd202bb
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 31 deletions.
9 changes: 6 additions & 3 deletions src/ngx_http_echo_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <ngx_log.h>


unsigned ngx_http_echo_filter_used;

ngx_http_output_header_filter_pt ngx_http_echo_next_header_filter;

Expand All @@ -27,9 +26,13 @@ static ngx_int_t ngx_http_echo_exec_filter_cmds(ngx_http_request_t *r,


ngx_int_t
ngx_http_echo_filter_init (ngx_conf_t *cf)
ngx_http_echo_filter_init(ngx_conf_t *cf)
{
if (ngx_http_echo_filter_used) {
ngx_http_echo_main_conf_t *emcf;

emcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_echo_module);

if (emcf->filter_used) {
dd("top header filter: %ld",
(unsigned long) ngx_http_top_header_filter);

Expand Down
1 change: 0 additions & 1 deletion src/ngx_http_echo_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "ngx_http_echo_module.h"

extern unsigned ngx_http_echo_filter_used;

extern ngx_http_output_header_filter_pt ngx_http_echo_next_header_filter;

Expand Down
18 changes: 0 additions & 18 deletions src/ngx_http_echo_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,6 @@
#include <ngx_log.h>


ngx_int_t
ngx_http_echo_handler_init(ngx_conf_t *cf)
{
ngx_int_t rc;

#if 1
ngx_http_echo_filter_used = 0;
#endif

rc = ngx_http_echo_echo_init(cf);
if (rc != NGX_OK) {
return rc;
}

return ngx_http_echo_add_variables(cf);
}


void
ngx_http_echo_wev_handler(ngx_http_request_t *r)
{
Expand Down
58 changes: 49 additions & 9 deletions src/ngx_http_echo_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

#include "ngx_http_echo_handler.h"
#include "ngx_http_echo_filter.h"
#include "ngx_http_echo_echo.h"
#include "ngx_http_echo_request_info.h"
#include "ngx_http_echo_var.h"

#include <nginx.h>
#include <ngx_config.h>
Expand All @@ -15,6 +17,8 @@
static void * ngx_http_echo_create_loc_conf(ngx_conf_t *cf);
static char * ngx_http_echo_merge_loc_conf(ngx_conf_t *cf, void *parent,
void *child);
static void *ngx_http_echo_create_main_conf(ngx_conf_t *cf);
static ngx_int_t ngx_http_echo_post_config(ngx_conf_t *cf);

/* config directive handlers */
static char * ngx_http_echo_echo(ngx_conf_t *cf, ngx_command_t *cmd,
Expand Down Expand Up @@ -59,17 +63,17 @@ static char * ngx_http_echo_helper(ngx_http_echo_opcode_t opcode,


static ngx_http_module_t ngx_http_echo_module_ctx = {
ngx_http_echo_handler_init, /* preconfiguration */
ngx_http_echo_filter_init, /* postconfiguration */
NULL, /* preconfiguration */
ngx_http_echo_post_config, /* postconfiguration */

NULL, /* create main configuration */
NULL, /* init main configuration */
ngx_http_echo_create_main_conf, /* create main configuration */
NULL, /* init main configuration */

NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */

ngx_http_echo_create_loc_conf, /* create location configuration */
ngx_http_echo_merge_loc_conf /* merge location configuration */
ngx_http_echo_create_loc_conf, /* create location configuration */
ngx_http_echo_merge_loc_conf /* merge location configuration */
};


Expand Down Expand Up @@ -289,8 +293,11 @@ ngx_http_echo_helper(ngx_http_echo_opcode_t opcode,
ngx_http_echo_cmd_t *echo_cmd;
ngx_http_core_loc_conf_t *clcf;
ngx_http_script_compile_t sc;
ngx_http_echo_main_conf_t *emcf;
ngx_http_echo_arg_template_t *arg;

emcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_echo_module);

/* cmds_ptr points to ngx_http_echo_loc_conf_t's
* handler_cmds, before_body_cmds, or after_body_cmds
* array, depending on the actual offset */
Expand All @@ -315,7 +322,7 @@ ngx_http_echo_helper(ngx_http_echo_opcode_t opcode,

} else {
dd("filter used = 1");
ngx_http_echo_filter_used = 1;
emcf->filter_used = 1;
}
}

Expand Down Expand Up @@ -610,3 +617,36 @@ ngx_http_echo_echo_exec(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
return ngx_http_echo_helper(echo_opcode_echo_exec, echo_handler_cmd,
cf, cmd, conf);
}


static void *
ngx_http_echo_create_main_conf(ngx_conf_t *cf)
{
ngx_http_echo_main_conf_t *emcf;

emcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_echo_main_conf_t));
if (emcf == NULL) {
return NULL;
}

return emcf;
}


static ngx_int_t
ngx_http_echo_post_config(ngx_conf_t *cf)
{
ngx_int_t rc;

rc = ngx_http_echo_filter_init(cf);
if (rc != NGX_OK) {
return rc;
}

rc = ngx_http_echo_echo_init(cf);
if (rc != NGX_OK) {
return rc;
}

return ngx_http_echo_add_variables(cf);
}
5 changes: 5 additions & 0 deletions src/ngx_http_echo_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ typedef struct {
} ngx_http_echo_loc_conf_t;


typedef struct {
ngx_int_t filter_used;
} ngx_http_echo_main_conf_t;


typedef struct {
ngx_array_t *choices; /* items after splitting */
ngx_uint_t next_choice; /* current item index */
Expand Down
59 changes: 59 additions & 0 deletions t/filter-used.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# vi:filetype=

use lib 'lib';
use Test::Nginx::Socket;

repeat_each(2);

plan tests => repeat_each() * (3 * blocks());

no_long_string();
log_level('warn');

#master_on();
#workers(1);

run_tests();

__DATA__
=== TEST 1: filter indeed used
--- http_config
postpone_output 1;
--- config
location /echo {
echo_after_body hello;
echo world;
}
--- request
GET /echo
--- stap
F(ngx_http_echo_header_filter) {
println("echo header filter called")
}
--- stap_out
echo header filter called
--- response_body
world
hello
=== TEST 2: filter not used
--- http_config
postpone_output 1;
--- config
location /echo {
#echo_after_body hello;
echo world;
}
--- request
GET /echo
--- stap
F(ngx_http_echo_header_filter) {
println("echo header filter called")
}
--- stap_out
--- response_body
world

0 comments on commit fd202bb

Please sign in to comment.