]> granicus.if.org Git - apache/blob - server/mpm/simple/simple_io.c
Check the return value from ap_run_create_connection in mpm_event.
[apache] / server / mpm / simple / simple_io.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "httpd.h"
18 #include "http_log.h"
19 #include "ap_listen.h"
20 #include "simple_types.h"
21 #include "simple_io.h"
22 #include "simple_event.h"
23
24 #include "http_connection.h"
25 #include "util_filter.h"
26 #include "http_main.h"
27 #include "scoreboard.h"
28 #include "http_vhost.h"
29
30 APLOG_USE_MODULE(mpm_simple);
31
32 static void simple_io_timeout_cb(simple_core_t * sc, void *baton)
33 {
34     simple_conn_t *scon = (simple_conn_t *) baton;
35     /* pqXXXXX: handle timeouts. */
36     conn_rec *c = scon->c;
37     conn_state_t *cs = c->cs;
38
39     cs = NULL;
40
41     ap_log_error(APLOG_MARK, APLOG_WARNING, 0, ap_server_conf,
42                  "io timeout hit (?)");
43 }
44
45 static apr_status_t simple_io_process(simple_conn_t * scon)
46 {
47     apr_status_t rv;
48     simple_core_t *sc;
49     conn_rec *c;
50     conn_state_t *cs;
51
52     if (scon->c->clogging_input_filters && !scon->c->aborted) {
53         /* Since we have an input filter which 'cloggs' the input stream,
54          * like mod_ssl, lets just do the normal read from input filters,
55          * like the Worker MPM does.
56          */
57         ap_run_process_connection(scon->c);
58         if (scon->c->cs->state != CONN_STATE_SUSPENDED) {
59             scon->c->cs->state = CONN_STATE_LINGER;
60         }
61     }
62
63     sc = scon->sc;
64     c = scon->c;
65     cs = c->cs;
66
67     while (!c->aborted) {
68
69         if (cs->pfd.reqevents != 0) {
70             rv = apr_pollcb_remove(sc->pollcb, &cs->pfd);
71             if (rv) {
72                 ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf,
73                              "simple_io_process: apr_pollcb_remove failure");
74                 /*AP_DEBUG_ASSERT(rv == APR_SUCCESS);*/
75             }
76             cs->pfd.reqevents = 0;
77         }
78
79         if (cs->state == CONN_STATE_READ_REQUEST_LINE) {
80             if (!c->aborted) {
81                 ap_run_process_connection(c);
82                 /* state will be updated upon return
83                  * fall thru to either wait for readability/timeout or
84                  * do lingering close
85                  */
86             }
87             else {
88                 cs->state = CONN_STATE_LINGER;
89             }
90         }
91
92         if (cs->state == CONN_STATE_WRITE_COMPLETION) {
93             ap_filter_t *output_filter = c->output_filters;
94             while (output_filter->next != NULL) {
95                 output_filter = output_filter->next;
96             }
97
98             rv = output_filter->frec->filter_func.out_func(output_filter,
99                                                            NULL);
100
101             if (rv != APR_SUCCESS) {
102                 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
103                              "network write failure in core output filter");
104                 cs->state = CONN_STATE_LINGER;
105             }
106             else if (c->data_in_output_filters) {
107                 /* Still in WRITE_COMPLETION_STATE:
108                  * Set a write timeout for this connection, and let the
109                  * event thread poll for writeability.
110                  */
111
112                 simple_register_timer(scon->sc,
113                                       simple_io_timeout_cb,
114                                       scon,
115                                       scon->c->base_server !=
116                                       NULL ? scon->c->base_server->
117                                       timeout : ap_server_conf->timeout,
118                                       scon->pool);
119
120                 cs->pfd.reqevents = APR_POLLOUT | APR_POLLHUP | APR_POLLERR;
121
122                 rv = apr_pollcb_add(sc->pollcb, &cs->pfd);
123
124                 if (rv != APR_SUCCESS) {
125                     ap_log_error(APLOG_MARK, APLOG_WARNING, rv,
126                                  ap_server_conf,
127                                  "apr_pollcb_add: failed in write completion");
128                     AP_DEBUG_ASSERT(rv == APR_SUCCESS);
129                 }
130                 return APR_SUCCESS;
131             }
132             else if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted) {
133                 c->cs->state = CONN_STATE_LINGER;
134             }
135             else if (c->data_in_input_filters) {
136                 cs->state = CONN_STATE_READ_REQUEST_LINE;
137             }
138             else {
139                 cs->state = CONN_STATE_CHECK_REQUEST_LINE_READABLE;
140             }
141         }
142
143         if (cs->state == CONN_STATE_LINGER) {
144             ap_lingering_close(c);
145             apr_pool_destroy(scon->pool);
146             return APR_SUCCESS;
147         }
148
149         if (cs->state == CONN_STATE_CHECK_REQUEST_LINE_READABLE) {
150             simple_register_timer(scon->sc,
151                                   simple_io_timeout_cb,
152                                   scon,
153                                   scon->c->base_server !=
154                                   NULL ? scon->c->base_server->
155                                   timeout : ap_server_conf->timeout,
156                                   scon->pool);
157
158             cs->pfd.reqevents = APR_POLLIN;
159
160             rv = apr_pollcb_add(sc->pollcb, &cs->pfd);
161
162             if (rv) {
163                 ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf,
164                              "process_socket: apr_pollcb_add failure in read request line");
165                 AP_DEBUG_ASSERT(rv == APR_SUCCESS);
166             }
167
168             return APR_SUCCESS;
169         }
170     }
171
172     ap_lingering_close(c);
173     apr_pool_destroy(scon->pool);
174     return APR_SUCCESS;
175 }
176
177 static void *simple_io_invoke(apr_thread_t * thread, void *baton)
178 {
179     simple_sb_t *sb = (simple_sb_t *) baton;
180     simple_conn_t *scon = (simple_conn_t *) sb->baton;
181     apr_status_t rv;
182
183     scon->c->current_thread = thread;
184
185     rv = simple_io_process(scon);
186
187     if (rv) {
188         ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, ap_server_conf,
189                      "simple_io_invoke: simple_io_process failed (?)");
190     }
191
192     return NULL;
193 }
194
195 static void *simple_io_setup_conn(apr_thread_t * thread, void *baton)
196 {
197     apr_status_t rv;
198     ap_sb_handle_t *sbh;
199     conn_state_t *cs;
200     long conn_id = 0;
201     simple_sb_t *sb;
202     simple_conn_t *scon = (simple_conn_t *) baton;
203
204     /* pqXXXXX: remove this. */
205     ap_create_sb_handle(&sbh, scon->pool, 0, 0);
206
207     scon->ba = apr_bucket_alloc_create(scon->pool);
208
209     scon->c = ap_run_create_connection(scon->pool, ap_server_conf, scon->sock,
210                                        conn_id, sbh, scon->ba);
211     /* XXX: handle failure */
212
213     scon->c->cs = apr_pcalloc(scon->pool, sizeof(conn_state_t));
214     cs = scon->c->cs;
215     sb = apr_pcalloc(scon->pool, sizeof(simple_sb_t));
216
217     scon->c->current_thread = thread;
218
219     cs->pfd.p = scon->pool;
220     cs->pfd.desc_type = APR_POLL_SOCKET;
221     cs->pfd.desc.s = scon->sock;
222     cs->pfd.reqevents = APR_POLLIN;
223
224     sb->type = SIMPLE_PT_CORE_IO;
225     sb->baton = scon;
226     cs->pfd.client_data = sb;
227
228     ap_update_vhost_given_ip(scon->c);
229
230     rv = ap_run_pre_connection(scon->c, scon->sock);
231     if (rv != OK && rv != DONE) {
232         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
233                      "simple_io_setup_conn: connection aborted");
234         scon->c->aborted = 1;
235     }
236
237     scon->c->cs->state = CONN_STATE_READ_REQUEST_LINE;
238
239     rv = simple_io_process(scon);
240
241     if (rv) {
242         ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, ap_server_conf,
243                      "simple_io_setup_conn: simple_io_process failed (?)");
244     }
245
246     return NULL;
247 }
248
249 apr_status_t simple_io_accept(simple_core_t * sc, simple_sb_t * sb)
250 {
251     apr_status_t rv;
252     apr_pool_t *ptrans;
253     apr_socket_t *socket;
254     ap_listen_rec *lr = (ap_listen_rec *) sb->baton;
255
256     /* pqXXXXXX: Consider doing pool recycling like the event/worker MPMs do. */
257     apr_pool_create(&ptrans, NULL);
258
259     apr_pool_tag(ptrans, "transaction");
260
261     rv = apr_socket_accept(&socket, lr->sd, ptrans);
262     if (rv) {
263         /* pqXXXXXX: unixd.c has _tons_ of custom handling on return values
264          * from accept, but it seems really crazy, it either worked, or didn't,
265          * but taking this approach of swallowing the error it is possible we have a
266          * fatal error on our listening socket, but we don't notice.
267          *
268          * Need to discuss this on dev@
269          */
270         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
271                      "simple_io_accept: apr_socket_accept failed");
272         return APR_SUCCESS;
273     }
274     else {
275         simple_conn_t *scon = apr_pcalloc(ptrans, sizeof(simple_conn_t));
276         scon->pool = ptrans;
277         scon->sock = socket;
278         scon->sc = sc;
279
280         return apr_thread_pool_push(sc->workers,
281                                     simple_io_setup_conn,
282                                     scon,
283                                     APR_THREAD_TASK_PRIORITY_NORMAL, NULL);
284     }
285
286     return APR_SUCCESS;
287 }
288
289 apr_status_t simple_io_event_process(simple_core_t * sc, simple_sb_t * sb)
290 {
291     /* pqXXXXX: In theory, if we have non-blocking operations on the connection
292      *  we can do them here, before pushing to another thread, thats just
293      * not implemented right now.
294      */
295     return apr_thread_pool_push(sc->workers,
296                                 simple_io_invoke,
297                                 sb, APR_THREAD_TASK_PRIORITY_NORMAL, NULL);
298 }