]> granicus.if.org Git - apache/blob - server/mpm/simple/simple_io.c
c3d5373e54a0d20fcdf5c144155172b18e00bb95
[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 /* Code disabled because it does nothing yet but causes a compiler warning */
35 #if 0
36     simple_conn_t *scon = (simple_conn_t *) baton;
37     /* pqXXXXX: handle timeouts. */
38     conn_rec *c = scon->c;
39
40     cs = NULL;
41 #endif
42
43     ap_log_error(APLOG_MARK, APLOG_WARNING, 0, ap_server_conf,
44                  "io timeout hit (?)");
45 }
46
47 static apr_status_t simple_io_process(simple_conn_t * scon)
48 {
49     apr_status_t rv;
50     simple_core_t *sc;
51     conn_rec *c;
52
53     if (scon->c->clogging_input_filters && !scon->c->aborted) {
54         /* Since we have an input filter which 'cloggs' the input stream,
55          * like mod_ssl, lets just do the normal read from input filters,
56          * like the Worker MPM does.
57          */
58         ap_run_process_connection(scon->c);
59         if (scon->cs.state != CONN_STATE_SUSPENDED) {
60             scon->cs.state = CONN_STATE_LINGER;
61         }
62     }
63
64     sc = scon->sc;
65     c = scon->c;
66
67     while (!c->aborted) {
68
69         if (scon->pfd.reqevents != 0) {
70             rv = apr_pollcb_remove(sc->pollcb, &scon->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             scon->pfd.reqevents = 0;
77         }
78
79         if (scon->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                 scon->cs.state = CONN_STATE_LINGER;
89             }
90         }
91
92         if (scon->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                 scon->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                 scon->pfd.reqevents = APR_POLLOUT | APR_POLLHUP | APR_POLLERR;
121
122                 rv = apr_pollcb_add(sc->pollcb, &scon->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                 scon->cs.state = CONN_STATE_LINGER;
134             }
135             else if (c->data_in_input_filters) {
136                 scon->cs.state = CONN_STATE_READ_REQUEST_LINE;
137             }
138             else {
139                 scon->cs.state = CONN_STATE_CHECK_REQUEST_LINE_READABLE;
140             }
141         }
142
143         if (scon->cs.state == CONN_STATE_LINGER) {
144             ap_lingering_close(c);
145             apr_pool_destroy(scon->pool);
146             return APR_SUCCESS;
147         }
148
149         if (scon->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             scon->pfd.reqevents = APR_POLLIN;
159
160             rv = apr_pollcb_add(sc->pollcb, &scon->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     long conn_id = 0;
200     simple_sb_t *sb;
201     simple_conn_t *scon = (simple_conn_t *) baton;
202
203     /* pqXXXXX: remove this. */
204     ap_create_sb_handle(&sbh, scon->pool, 0, 0);
205
206     scon->ba = apr_bucket_alloc_create(scon->pool);
207
208     scon->c = ap_run_create_connection(scon->pool, ap_server_conf, scon->sock,
209                                        conn_id, sbh, scon->ba);
210     /* XXX: handle failure */
211
212     scon->c->cs = &scon->cs;
213     sb = apr_pcalloc(scon->pool, sizeof(simple_sb_t));
214
215     scon->c->current_thread = thread;
216
217     scon->pfd.p = scon->pool;
218     scon->pfd.desc_type = APR_POLL_SOCKET;
219     scon->pfd.desc.s = scon->sock;
220     scon->pfd.reqevents = APR_POLLIN;
221
222     sb->type = SIMPLE_PT_CORE_IO;
223     sb->baton = scon;
224     scon->pfd.client_data = sb;
225
226     ap_update_vhost_given_ip(scon->c);
227
228     rv = ap_run_pre_connection(scon->c, scon->sock);
229     if (rv != OK && rv != DONE) {
230         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
231                      "simple_io_setup_conn: connection aborted");
232         scon->c->aborted = 1;
233     }
234
235     scon->cs.state = CONN_STATE_READ_REQUEST_LINE;
236
237     rv = simple_io_process(scon);
238
239     if (rv) {
240         ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, ap_server_conf,
241                      "simple_io_setup_conn: simple_io_process failed (?)");
242     }
243
244     return NULL;
245 }
246
247 apr_status_t simple_io_accept(simple_core_t * sc, simple_sb_t * sb)
248 {
249     apr_status_t rv;
250     apr_pool_t *ptrans;
251     apr_socket_t *socket;
252     ap_listen_rec *lr = (ap_listen_rec *) sb->baton;
253
254     /* pqXXXXXX: Consider doing pool recycling like the event/worker MPMs do. */
255     apr_pool_create(&ptrans, NULL);
256
257     apr_pool_tag(ptrans, "transaction");
258
259     rv = apr_socket_accept(&socket, lr->sd, ptrans);
260     if (rv) {
261         /* pqXXXXXX: unixd.c has _tons_ of custom handling on return values
262          * from accept, but it seems really crazy, it either worked, or didn't,
263          * but taking this approach of swallowing the error it is possible we have a
264          * fatal error on our listening socket, but we don't notice.
265          *
266          * Need to discuss this on dev@
267          */
268         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
269                      "simple_io_accept: apr_socket_accept failed");
270         return APR_SUCCESS;
271     }
272     else {
273         simple_conn_t *scon = apr_pcalloc(ptrans, sizeof(simple_conn_t));
274         scon->pool = ptrans;
275         scon->sock = socket;
276         scon->sc = sc;
277
278         return apr_thread_pool_push(sc->workers,
279                                     simple_io_setup_conn,
280                                     scon,
281                                     APR_THREAD_TASK_PRIORITY_NORMAL, NULL);
282     }
283
284     return APR_SUCCESS;
285 }
286
287 apr_status_t simple_io_event_process(simple_core_t * sc, simple_sb_t * sb)
288 {
289     /* pqXXXXX: In theory, if we have non-blocking operations on the connection
290      *  we can do them here, before pushing to another thread, thats just
291      * not implemented right now.
292      */
293     return apr_thread_pool_push(sc->workers,
294                                 simple_io_invoke,
295                                 sb, APR_THREAD_TASK_PRIORITY_NORMAL, NULL);
296 }