@@ -35,19 +35,63 @@ extern int ngx_http_lua_event_inited;
35
35
static ngx_inline ngx_int_t
36
36
ngx_http_lua_init_event (ngx_cycle_t * cycle )
37
37
{
38
- void * * * ccf ;
39
- ngx_event_conf_t * ecf ;
38
+ ngx_module_t * module ;
39
+ ngx_module_t * * modules ;
40
+ ngx_event_module_t * event_module ;
41
+ ngx_uint_t i ;
42
+ ngx_connection_t * c , * next ;
43
+ ngx_event_t * rev , * wev ;
44
+
45
+ module = NULL ;
46
+
47
+ #if (nginx_version >= 1009011 )
48
+ modules = cycle -> modules ;
49
+ #else
50
+ modules = ngx_modules ;
51
+ #endif
52
+
53
+ for (i = 0 ; modules [i ]; i ++ ) {
54
+ if (modules [i ]-> type != NGX_EVENT_MODULE ) {
55
+ continue ;
56
+ }
57
+
58
+ event_module = modules [i ]-> ctx ;
59
+ if (ngx_strcmp (event_module -> name -> data , "event_core" ) == 0 )
60
+ {
61
+ continue ;
62
+ }
40
63
41
- ccf = ngx_get_conf (cycle -> conf_ctx , ngx_events_module );
42
- if (ccf == NULL ) {
64
+ module = modules [i ];
65
+ break ;
66
+ }
67
+
68
+ if (module == NULL ) {
69
+ ngx_log_error (NGX_LOG_EMERG , cycle -> log , 0 , "no events module found" );
43
70
return NGX_ERROR ;
44
71
}
45
72
46
- ecf = (* ccf )[ngx_event_core_module .ctx_index ];
73
+ event_module = module -> ctx ;
74
+
75
+ /* FIXME: should init event_module actions here, like:
76
+ * ```
77
+ * if (event_module->actions.init(cycle, ngx_timer_resolution) != NGX_OK) {
78
+ * ```
79
+ *
80
+ * but `epcf` is not initial here before
81
+ * ```
82
+ * static ngx_int_t
83
+ * ngx_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer)
84
+ * {
85
+ * ngx_epoll_conf_t *epcf;
86
+ *
87
+ * // Segmentation fault below
88
+ * epcf = ngx_event_get_conf(cycle->conf_ctx, ngx_epoll_module);
89
+ * ```
90
+ */
47
91
48
92
#if (NGX_HAVE_EPOLL ) && !(NGX_TEST_BUILD_EPOLL )
49
93
50
- if (ngx_strcmp (ecf -> name , "epoll" ) == 0 ) {
94
+ if (ngx_strcmp (event_module -> name -> data , "epoll" ) == 0 ) {
51
95
ngx_http_lua_event_actions = ngx_http_lua_epoll ;
52
96
53
97
} else
@@ -56,7 +100,7 @@ ngx_http_lua_init_event(ngx_cycle_t *cycle)
56
100
57
101
#if (NGX_HAVE_POLL )
58
102
59
- if (ngx_strcmp (ecf -> name , "poll" ) == 0 ) {
103
+ if (ngx_strcmp (event_module -> name -> data , "poll" ) == 0 ) {
60
104
ngx_http_lua_event_actions = ngx_http_lua_poll ;
61
105
62
106
} else
@@ -65,7 +109,7 @@ ngx_http_lua_init_event(ngx_cycle_t *cycle)
65
109
66
110
#if (NGX_HAVE_KQUEUE )
67
111
68
- if (ngx_strcmp (ecf -> name , "kqueue" ) == 0 ) {
112
+ if (ngx_strcmp (event_module -> name -> data , "kqueue" ) == 0 ) {
69
113
ngx_http_lua_event_actions = ngx_http_lua_kqueue ;
70
114
71
115
} else
@@ -76,6 +120,55 @@ ngx_http_lua_init_event(ngx_cycle_t *cycle)
76
120
return NGX_ERROR ;
77
121
}
78
122
123
+ cycle -> connection_n = 128 ;
124
+ cycle -> connections =
125
+ ngx_alloc (sizeof (ngx_connection_t ) * cycle -> connection_n , cycle -> log );
126
+ if (cycle -> connections == NULL ) {
127
+ return NGX_ERROR ;
128
+ }
129
+
130
+ c = cycle -> connections ;
131
+
132
+ cycle -> read_events = ngx_alloc (sizeof (ngx_event_t ) * cycle -> connection_n ,
133
+ cycle -> log );
134
+ if (cycle -> read_events == NULL ) {
135
+ return NGX_ERROR ;
136
+ }
137
+
138
+ rev = cycle -> read_events ;
139
+ for (i = 0 ; i < cycle -> connection_n ; i ++ ) {
140
+ rev [i ].closed = 1 ;
141
+ rev [i ].instance = 1 ;
142
+ }
143
+
144
+ cycle -> write_events = ngx_alloc (sizeof (ngx_event_t ) * cycle -> connection_n ,
145
+ cycle -> log );
146
+ if (cycle -> write_events == NULL ) {
147
+ return NGX_ERROR ;
148
+ }
149
+
150
+ wev = cycle -> write_events ;
151
+ for (i = 0 ; i < cycle -> connection_n ; i ++ ) {
152
+ wev [i ].closed = 1 ;
153
+ }
154
+
155
+ i = cycle -> connection_n ;
156
+ next = NULL ;
157
+
158
+ do {
159
+ i -- ;
160
+
161
+ c [i ].data = next ;
162
+ c [i ].read = & cycle -> read_events [i ];
163
+ c [i ].write = & cycle -> write_events [i ];
164
+ c [i ].fd = (ngx_socket_t ) - 1 ;
165
+
166
+ next = & c [i ];
167
+ } while (i );
168
+
169
+ cycle -> free_connections = next ;
170
+ cycle -> free_connection_n = cycle -> connection_n ;
171
+
79
172
return ngx_http_lua_event_actions .init_event (cycle );
80
173
}
81
174
0 commit comments