]> git.kaiwu.me - nginx.git/commitdiff
Dynamic modules: moved module-related stuff to separate files.
authorMaxim Dounin <mdounin@mdounin.ru>
Thu, 4 Feb 2016 15:30:21 +0000 (18:30 +0300)
committerMaxim Dounin <mdounin@mdounin.ru>
Thu, 4 Feb 2016 15:30:21 +0000 (18:30 +0300)
auto/sources
src/core/nginx.c
src/core/ngx_conf_file.h
src/core/ngx_core.h
src/core/ngx_cycle.c
src/core/ngx_module.c [new file with mode: 0644]
src/core/ngx_module.h [new file with mode: 0644]
src/event/ngx_event.c
src/http/ngx_http.c
src/mail/ngx_mail.c
src/stream/ngx_stream.c

index 2e44ce1849a0c1db9bb8d18c408b7990a43b2945..d325ec3fd2ac5fd8d6a325f004e65ce09de8e464 100644 (file)
@@ -36,6 +36,7 @@ CORE_DEPS="src/core/nginx.h \
            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 @@ CORE_SRCS="src/core/nginx.c \
            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 \
index 64db3818e6dfd0ebf237e42a9fad8d646b69bf7c..22441a84c0e27e66eb07faeb86f388f38ae7d60a 100644 (file)
@@ -160,8 +160,6 @@ ngx_module_t  ngx_core_module = {
 };
 
 
-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 @@ main(int argc, char *const *argv)
         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);
index 2d03f4347b79fa027ada6ee1e85b129559318f1d..9ccee366f2b02c027290ffa17868a0ef58e8f3fa 100644 (file)
@@ -96,53 +96,6 @@ struct ngx_open_file_s {
 };
 
 
-#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_enum_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 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_ */
index 6b317056a6324478d5bd69671116eb4acf0e1abf..2855a59c1b611f4d326508c5f62a75bfa813c311 100644 (file)
@@ -83,6 +83,7 @@ typedef void (*ngx_connection_handler_pt)(ngx_connection_t *c);
 #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>
index ad4bf92547cf4e2ffc91e4a81718e1638534fa9b..bebb3732e9c57b651f24c9f8fc8462aba7e21ca6 100644 (file)
@@ -612,13 +612,9 @@ ngx_init_cycle(ngx_cycle_t *old_cycle)
 
     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 (file)
index 0000000..dc4e91f
--- /dev/null
@@ -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 (file)
index 0000000..fb5ac9b
--- /dev/null
@@ -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_ */
index 15da213405e85292dfce7e2f1613b64528ca9b46..165753ae9f3d55d34cbe481d6c004d349a05926d 100644 (file)
@@ -891,14 +891,7 @@ ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 
     /* 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) {
index 64af447609736037d1b1b291701e9239618b0e60..05bc48b0f91a4dae79b175f69d532da470f91cee 100644 (file)
@@ -144,14 +144,7 @@ ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 
     /* 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 */
index 962ae8be53d6d55d62367d63f84a687f794d5629..7524bee79fa3a6119c76b2623ae745abe6143ca3 100644 (file)
@@ -91,14 +91,7 @@ ngx_mail_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 
     /* 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 */
index f0aa532b6e17153107f03ecd8209450562803b4f..045b6f3cf351b0c50b03853e3bf641552366f248 100644 (file)
@@ -91,14 +91,7 @@ ngx_stream_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 
     /* 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 */