Dynamic modules: moved module-related stuff to separate files.
diff --git a/auto/sources b/auto/sources
index 2e44ce1..d325ec3 100644
--- a/auto/sources
+++ b/auto/sources
@@ -36,6 +36,7 @@
src/core/ngx_connection.h \
src/core/ngx_cycle.h \
src/core/ngx_conf_file.h \
+ src/core/ngx_module.h \
src/core/ngx_resolver.h \
src/core/ngx_open_file_cache.h \
src/core/ngx_crypt.h \
@@ -71,6 +72,7 @@
src/core/ngx_rwlock.c \
src/core/ngx_cpuinfo.c \
src/core/ngx_conf_file.c \
+ src/core/ngx_module.c \
src/core/ngx_resolver.c \
src/core/ngx_open_file_cache.c \
src/core/ngx_crypt.c \
diff --git a/src/core/nginx.c b/src/core/nginx.c
index 64db381..22441a8 100644
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -160,8 +160,6 @@
};
-ngx_uint_t ngx_max_module;
-
static ngx_uint_t ngx_show_help;
static ngx_uint_t ngx_show_version;
static ngx_uint_t ngx_show_configure;
@@ -260,9 +258,8 @@
return 1;
}
- ngx_max_module = 0;
- for (i = 0; ngx_modules[i]; i++) {
- ngx_modules[i]->index = ngx_max_module++;
+ if (ngx_preinit_modules() != NGX_OK) {
+ return 1;
}
cycle = ngx_init_cycle(&init_cycle);
diff --git a/src/core/ngx_conf_file.h b/src/core/ngx_conf_file.h
index 2d03f43..9ccee36 100644
--- a/src/core/ngx_conf_file.h
+++ b/src/core/ngx_conf_file.h
@@ -96,53 +96,6 @@
};
-#define NGX_MODULE_V1 0, 0, 0, 0, 0, 0, 1
-#define NGX_MODULE_V1_PADDING 0, 0, 0, 0, 0, 0, 0, 0
-
-struct ngx_module_s {
- ngx_uint_t ctx_index;
- ngx_uint_t index;
-
- ngx_uint_t spare0;
- ngx_uint_t spare1;
- ngx_uint_t spare2;
- ngx_uint_t spare3;
-
- ngx_uint_t version;
-
- void *ctx;
- ngx_command_t *commands;
- ngx_uint_t type;
-
- ngx_int_t (*init_master)(ngx_log_t *log);
-
- ngx_int_t (*init_module)(ngx_cycle_t *cycle);
-
- ngx_int_t (*init_process)(ngx_cycle_t *cycle);
- ngx_int_t (*init_thread)(ngx_cycle_t *cycle);
- void (*exit_thread)(ngx_cycle_t *cycle);
- void (*exit_process)(ngx_cycle_t *cycle);
-
- void (*exit_master)(ngx_cycle_t *cycle);
-
- uintptr_t spare_hook0;
- uintptr_t spare_hook1;
- uintptr_t spare_hook2;
- uintptr_t spare_hook3;
- uintptr_t spare_hook4;
- uintptr_t spare_hook5;
- uintptr_t spare_hook6;
- uintptr_t spare_hook7;
-};
-
-
-typedef struct {
- ngx_str_t name;
- void *(*create_conf)(ngx_cycle_t *cycle);
- char *(*init_conf)(ngx_cycle_t *cycle, void *conf);
-} ngx_core_module_t;
-
-
typedef struct {
ngx_file_t file;
ngx_buf_t *buffer;
@@ -340,8 +293,4 @@
char *ngx_conf_set_bitmask_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
-extern ngx_uint_t ngx_max_module;
-extern ngx_module_t *ngx_modules[];
-
-
#endif /* _NGX_CONF_FILE_H_INCLUDED_ */
diff --git a/src/core/ngx_core.h b/src/core/ngx_core.h
index 6b31705..2855a59 100644
--- a/src/core/ngx_core.h
+++ b/src/core/ngx_core.h
@@ -83,6 +83,7 @@
#endif
#include <ngx_process_cycle.h>
#include <ngx_conf_file.h>
+#include <ngx_module.h>
#include <ngx_open_file_cache.h>
#include <ngx_os.h>
#include <ngx_connection.h>
diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c
index ad4bf92..bebb373 100644
--- a/src/core/ngx_cycle.c
+++ b/src/core/ngx_cycle.c
@@ -612,13 +612,9 @@
pool->log = cycle->log;
- for (i = 0; ngx_modules[i]; i++) {
- if (ngx_modules[i]->init_module) {
- if (ngx_modules[i]->init_module(cycle) != NGX_OK) {
- /* fatal */
- exit(1);
- }
- }
+ if (ngx_init_modules(cycle) != NGX_OK) {
+ /* fatal */
+ exit(1);
}
diff --git a/src/core/ngx_module.c b/src/core/ngx_module.c
new file mode 100644
index 0000000..dc4e91f
--- /dev/null
+++ b/src/core/ngx_module.c
@@ -0,0 +1,65 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ * Copyright (C) Maxim Dounin
+ * Copyright (C) Nginx, Inc.
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+
+
+ngx_uint_t ngx_max_module;
+
+
+ngx_int_t
+ngx_preinit_modules()
+{
+ ngx_uint_t i;
+
+ ngx_max_module = 0;
+ for (i = 0; ngx_modules[i]; i++) {
+ ngx_modules[i]->index = ngx_max_module++;
+ }
+
+ return NGX_OK;
+}
+
+
+ngx_int_t
+ngx_init_modules(ngx_cycle_t *cycle)
+{
+ ngx_uint_t i;
+
+ for (i = 0; ngx_modules[i]; i++) {
+ if (ngx_modules[i]->init_module) {
+ if (ngx_modules[i]->init_module(cycle) != NGX_OK) {
+ return NGX_ERROR;
+ }
+ }
+ }
+
+ return NGX_OK;
+}
+
+
+ngx_int_t
+ngx_count_modules(ngx_cycle_t *cycle, ngx_uint_t type)
+{
+ ngx_uint_t i, max;
+
+ max = 0;
+
+ /* count appropriate modules, set up their indices */
+
+ for (i = 0; ngx_modules[i]; i++) {
+ if (ngx_modules[i]->type != type) {
+ continue;
+ }
+
+ ngx_modules[i]->ctx_index = max++;
+ }
+
+ return max;
+}
diff --git a/src/core/ngx_module.h b/src/core/ngx_module.h
new file mode 100644
index 0000000..fb5ac9b
--- /dev/null
+++ b/src/core/ngx_module.h
@@ -0,0 +1,74 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ * Copyright (C) Maxim Dounin
+ * Copyright (C) Nginx, Inc.
+ */
+
+
+#ifndef _NGX_MODULE_H_INCLUDED_
+#define _NGX_MODULE_H_INCLUDED_
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+
+
+#define NGX_MODULE_V1 0, 0, 0, 0, 0, 0, 1
+#define NGX_MODULE_V1_PADDING 0, 0, 0, 0, 0, 0, 0, 0
+
+
+struct ngx_module_s {
+ ngx_uint_t ctx_index;
+ ngx_uint_t index;
+
+ ngx_uint_t spare0;
+ ngx_uint_t spare1;
+ ngx_uint_t spare2;
+ ngx_uint_t spare3;
+
+ ngx_uint_t version;
+
+ void *ctx;
+ ngx_command_t *commands;
+ ngx_uint_t type;
+
+ ngx_int_t (*init_master)(ngx_log_t *log);
+
+ ngx_int_t (*init_module)(ngx_cycle_t *cycle);
+
+ ngx_int_t (*init_process)(ngx_cycle_t *cycle);
+ ngx_int_t (*init_thread)(ngx_cycle_t *cycle);
+ void (*exit_thread)(ngx_cycle_t *cycle);
+ void (*exit_process)(ngx_cycle_t *cycle);
+
+ void (*exit_master)(ngx_cycle_t *cycle);
+
+ uintptr_t spare_hook0;
+ uintptr_t spare_hook1;
+ uintptr_t spare_hook2;
+ uintptr_t spare_hook3;
+ uintptr_t spare_hook4;
+ uintptr_t spare_hook5;
+ uintptr_t spare_hook6;
+ uintptr_t spare_hook7;
+};
+
+
+typedef struct {
+ ngx_str_t name;
+ void *(*create_conf)(ngx_cycle_t *cycle);
+ char *(*init_conf)(ngx_cycle_t *cycle, void *conf);
+} ngx_core_module_t;
+
+
+ngx_int_t ngx_preinit_modules();
+ngx_int_t ngx_init_modules(ngx_cycle_t *cycle);
+ngx_int_t ngx_count_modules(ngx_cycle_t *cycle, ngx_uint_t type);
+
+
+extern ngx_module_t *ngx_modules[];
+extern ngx_uint_t ngx_max_module;
+
+
+#endif /* _NGX_MODULE_H_INCLUDED_ */
diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c
index 15da213..165753a 100644
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -891,14 +891,7 @@
/* count the number of the event modules and set up their indices */
- ngx_event_max_module = 0;
- for (i = 0; ngx_modules[i]; i++) {
- if (ngx_modules[i]->type != NGX_EVENT_MODULE) {
- continue;
- }
-
- ngx_modules[i]->ctx_index = ngx_event_max_module++;
- }
+ ngx_event_max_module = ngx_count_modules(cf->cycle, NGX_EVENT_MODULE);
ctx = ngx_pcalloc(cf->pool, sizeof(void *));
if (ctx == NULL) {
diff --git a/src/http/ngx_http.c b/src/http/ngx_http.c
index 64af447..05bc48b 100644
--- a/src/http/ngx_http.c
+++ b/src/http/ngx_http.c
@@ -144,14 +144,7 @@
/* count the number of the http modules and set up their indices */
- ngx_http_max_module = 0;
- for (m = 0; ngx_modules[m]; m++) {
- if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
- continue;
- }
-
- ngx_modules[m]->ctx_index = ngx_http_max_module++;
- }
+ ngx_http_max_module = ngx_count_modules(cf->cycle, NGX_HTTP_MODULE);
/* the http main_conf context, it is the same in the all http contexts */
diff --git a/src/mail/ngx_mail.c b/src/mail/ngx_mail.c
index 962ae8b..7524bee 100644
--- a/src/mail/ngx_mail.c
+++ b/src/mail/ngx_mail.c
@@ -91,14 +91,7 @@
/* count the number of the mail modules and set up their indices */
- ngx_mail_max_module = 0;
- for (m = 0; ngx_modules[m]; m++) {
- if (ngx_modules[m]->type != NGX_MAIL_MODULE) {
- continue;
- }
-
- ngx_modules[m]->ctx_index = ngx_mail_max_module++;
- }
+ ngx_mail_max_module = ngx_count_modules(cf->cycle, NGX_MAIL_MODULE);
/* the mail main_conf context, it is the same in the all mail contexts */
diff --git a/src/stream/ngx_stream.c b/src/stream/ngx_stream.c
index f0aa532..045b6f3 100644
--- a/src/stream/ngx_stream.c
+++ b/src/stream/ngx_stream.c
@@ -91,14 +91,7 @@
/* count the number of the stream modules and set up their indices */
- ngx_stream_max_module = 0;
- for (m = 0; ngx_modules[m]; m++) {
- if (ngx_modules[m]->type != NGX_STREAM_MODULE) {
- continue;
- }
-
- ngx_modules[m]->ctx_index = ngx_stream_max_module++;
- }
+ ngx_stream_max_module = ngx_count_modules(cf->cycle, NGX_STREAM_MODULE);
/* the stream main_conf context, it's the same in the all stream contexts */