~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/poll/unix/port.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * 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 "apr_arch_poll_private.h"
 
18
 
 
19
#ifdef POLLSET_USES_PORT
 
20
 
 
21
static apr_int16_t get_event(apr_int16_t event)
 
22
{
 
23
    apr_int16_t rv = 0;
 
24
 
 
25
    if (event & APR_POLLIN)
 
26
        rv |= POLLIN;
 
27
    if (event & APR_POLLPRI)
 
28
        rv |= POLLPRI;
 
29
    if (event & APR_POLLOUT)
 
30
        rv |= POLLOUT;
 
31
    if (event & APR_POLLERR)
 
32
        rv |= POLLERR;
 
33
    if (event & APR_POLLHUP)
 
34
        rv |= POLLHUP;
 
35
    if (event & APR_POLLNVAL)
 
36
        rv |= POLLNVAL;
 
37
 
 
38
    return rv;
 
39
}
 
40
 
 
41
static apr_int16_t get_revent(apr_int16_t event)
 
42
{
 
43
    apr_int16_t rv = 0;
 
44
 
 
45
    if (event & POLLIN)
 
46
        rv |= APR_POLLIN;
 
47
    if (event & POLLPRI)
 
48
        rv |= APR_POLLPRI;
 
49
    if (event & POLLOUT)
 
50
        rv |= APR_POLLOUT;
 
51
    if (event & POLLERR)
 
52
        rv |= APR_POLLERR;
 
53
    if (event & POLLHUP)
 
54
        rv |= APR_POLLHUP;
 
55
    if (event & POLLNVAL)
 
56
        rv |= APR_POLLNVAL;
 
57
 
 
58
    return rv;
 
59
}
 
60
 
 
61
 
 
62
struct apr_pollset_t
 
63
{
 
64
    apr_pool_t *pool;
 
65
    apr_uint32_t nelts;
 
66
    apr_uint32_t nalloc;
 
67
    int port_fd;
 
68
    port_event_t *port_set;
 
69
    apr_pollfd_t *result_set;
 
70
    apr_uint32_t flags;
 
71
#if APR_HAS_THREADS
 
72
    /* A thread mutex to protect operations on the rings */
 
73
    apr_thread_mutex_t *ring_lock;
 
74
#endif
 
75
    /* A ring containing all of the pollfd_t that are active */
 
76
    APR_RING_HEAD(pfd_query_ring_t, pfd_elem_t) query_ring;
 
77
    APR_RING_HEAD(pfd_add_ring_t, pfd_elem_t) add_ring;
 
78
    /* A ring of pollfd_t that have been used, and then _remove'd */
 
79
    APR_RING_HEAD(pfd_free_ring_t, pfd_elem_t) free_ring;
 
80
    /* A ring of pollfd_t where rings that have been _remove'd but
 
81
       might still be inside a _poll */
 
82
    APR_RING_HEAD(pfd_dead_ring_t, pfd_elem_t) dead_ring;
 
83
};
 
84
 
 
85
static apr_status_t backend_cleanup(void *p_)
 
86
{
 
87
    apr_pollset_t *pollset = (apr_pollset_t *) p_;
 
88
    close(pollset->port_fd);
 
89
    return APR_SUCCESS;
 
90
}
 
91
 
 
92
APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset,
 
93
                                             apr_uint32_t size,
 
94
                                             apr_pool_t *p,
 
95
                                             apr_uint32_t flags)
 
96
{
 
97
    apr_status_t rv = APR_SUCCESS;
 
98
    *pollset = apr_palloc(p, sizeof(**pollset));
 
99
#if APR_HAS_THREADS
 
100
    if (flags & APR_POLLSET_THREADSAFE &&
 
101
        ((rv = apr_thread_mutex_create(&(*pollset)->ring_lock,
 
102
                                       APR_THREAD_MUTEX_DEFAULT,
 
103
                                       p) != APR_SUCCESS))) {
 
104
        *pollset = NULL;
 
105
        return rv;
 
106
    }
 
107
#else
 
108
    if (flags & APR_POLLSET_THREADSAFE) {
 
109
        *pollset = NULL;
 
110
        return APR_ENOTIMPL;
 
111
    }
 
112
#endif
 
113
    (*pollset)->nelts = 0;
 
114
    (*pollset)->nalloc = size;
 
115
    (*pollset)->flags = flags;
 
116
    (*pollset)->pool = p;
 
117
 
 
118
    (*pollset)->port_set = apr_palloc(p, size * sizeof(port_event_t));
 
119
 
 
120
    (*pollset)->port_fd = port_create();
 
121
 
 
122
    if ((*pollset)->port_fd < 0) {
 
123
        return APR_ENOMEM;
 
124
    }
 
125
 
 
126
    apr_pool_cleanup_register(p, (void *) (*pollset), backend_cleanup,
 
127
                              apr_pool_cleanup_null);
 
128
 
 
129
    (*pollset)->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
 
130
 
 
131
    APR_RING_INIT(&(*pollset)->query_ring, pfd_elem_t, link);
 
132
    APR_RING_INIT(&(*pollset)->add_ring, pfd_elem_t, link);
 
133
    APR_RING_INIT(&(*pollset)->free_ring, pfd_elem_t, link);
 
134
    APR_RING_INIT(&(*pollset)->dead_ring, pfd_elem_t, link);
 
135
 
 
136
    return rv;
 
137
}
 
138
 
 
139
APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t *pollset)
 
140
{
 
141
    return apr_pool_cleanup_run(pollset->pool, pollset, backend_cleanup);
 
142
}
 
143
 
 
144
APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset,
 
145
                                          const apr_pollfd_t *descriptor)
 
146
{
 
147
    apr_os_sock_t fd;
 
148
    pfd_elem_t *elem;
 
149
    int res;
 
150
    apr_status_t rv = APR_SUCCESS;
 
151
 
 
152
    pollset_lock_rings();
 
153
 
 
154
    if (!APR_RING_EMPTY(&(pollset->free_ring), pfd_elem_t, link)) {
 
155
        elem = APR_RING_FIRST(&(pollset->free_ring));
 
156
        APR_RING_REMOVE(elem, link);
 
157
    }
 
158
    else {
 
159
        elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t));
 
160
        APR_RING_ELEM_INIT(elem, link);
 
161
    }
 
162
    elem->pfd = *descriptor;
 
163
 
 
164
    if (descriptor->desc_type == APR_POLL_SOCKET) {
 
165
        fd = descriptor->desc.s->socketdes;
 
166
    }
 
167
    else {
 
168
        fd = descriptor->desc.f->filedes;
 
169
    }
 
170
 
 
171
    res = port_associate(pollset->port_fd, PORT_SOURCE_FD, fd, 
 
172
                         get_event(descriptor->reqevents), (void *)elem);
 
173
 
 
174
    if (res < 0) {
 
175
        rv = APR_ENOMEM;
 
176
        APR_RING_INSERT_TAIL(&(pollset->free_ring), elem, pfd_elem_t, link);
 
177
    }
 
178
    else {
 
179
        pollset->nelts++;
 
180
        APR_RING_INSERT_TAIL(&(pollset->query_ring), elem, pfd_elem_t, link);
 
181
    }
 
182
 
 
183
    pollset_unlock_rings();
 
184
 
 
185
    return rv;
 
186
}
 
187
 
 
188
APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset,
 
189
                                             const apr_pollfd_t *descriptor)
 
190
{
 
191
    apr_os_sock_t fd;
 
192
    pfd_elem_t *ep;
 
193
    apr_status_t rv = APR_SUCCESS;
 
194
    int res;
 
195
 
 
196
    pollset_lock_rings();
 
197
 
 
198
    if (descriptor->desc_type == APR_POLL_SOCKET) {
 
199
        fd = descriptor->desc.s->socketdes;
 
200
    }
 
201
    else {
 
202
        fd = descriptor->desc.f->filedes;
 
203
    }
 
204
 
 
205
    res = port_dissociate(pollset->port_fd, PORT_SOURCE_FD, fd);
 
206
 
 
207
    if (res < 0) {
 
208
        rv = APR_NOTFOUND;
 
209
    }
 
210
 
 
211
    if (!APR_RING_EMPTY(&(pollset->query_ring), pfd_elem_t, link)) {
 
212
        for (ep = APR_RING_FIRST(&(pollset->query_ring));
 
213
             ep != APR_RING_SENTINEL(&(pollset->query_ring),
 
214
                                     pfd_elem_t, link);
 
215
             ep = APR_RING_NEXT(ep, link)) {
 
216
 
 
217
            if (descriptor->desc.s == ep->pfd.desc.s) {
 
218
                APR_RING_REMOVE(ep, link);
 
219
                APR_RING_INSERT_TAIL(&(pollset->dead_ring),
 
220
                                     ep, pfd_elem_t, link);
 
221
                break;
 
222
            }
 
223
        }
 
224
    }
 
225
 
 
226
    if (!APR_RING_EMPTY(&(pollset->add_ring), pfd_elem_t, link)) {
 
227
        for (ep = APR_RING_FIRST(&(pollset->add_ring));
 
228
             ep != APR_RING_SENTINEL(&(pollset->add_ring),
 
229
                                     pfd_elem_t, link);
 
230
             ep = APR_RING_NEXT(ep, link)) {
 
231
 
 
232
            if (descriptor->desc.s == ep->pfd.desc.s) {
 
233
                APR_RING_REMOVE(ep, link);
 
234
                APR_RING_INSERT_TAIL(&(pollset->dead_ring),
 
235
                                     ep, pfd_elem_t, link);
 
236
                break;
 
237
            }
 
238
        }
 
239
    }
 
240
 
 
241
    pollset_unlock_rings();
 
242
 
 
243
    return rv;
 
244
}
 
245
 
 
246
APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
 
247
                                           apr_interval_time_t timeout,
 
248
                                           apr_int32_t *num,
 
249
                                           const apr_pollfd_t **descriptors)
 
250
{
 
251
    apr_os_sock_t fd;
 
252
    int ret, i;
 
253
    unsigned int nget;
 
254
    pfd_elem_t *ep;
 
255
    struct timespec tv, *tvptr;
 
256
    apr_status_t rv = APR_SUCCESS;
 
257
 
 
258
    if (timeout < 0) {
 
259
        tvptr = NULL;
 
260
    }
 
261
    else {
 
262
        tv.tv_sec = (long) apr_time_sec(timeout);
 
263
        tv.tv_nsec = (long) apr_time_msec(timeout);
 
264
        tvptr = &tv;
 
265
    }
 
266
 
 
267
    nget = 1;
 
268
 
 
269
    pollset_lock_rings();
 
270
 
 
271
    while (!APR_RING_EMPTY(&(pollset->add_ring), pfd_elem_t, link)) {
 
272
        ep = APR_RING_FIRST(&(pollset->add_ring));
 
273
        APR_RING_REMOVE(ep, link);
 
274
 
 
275
        if (ep->pfd.desc_type == APR_POLL_SOCKET) {
 
276
            fd = ep->pfd.desc.s->socketdes;
 
277
        }
 
278
        else {
 
279
            fd = ep->pfd.desc.f->filedes;
 
280
        }
 
281
 
 
282
        port_associate(pollset->port_fd, PORT_SOURCE_FD, 
 
283
                           fd, get_event(ep->pfd.reqevents), ep);
 
284
 
 
285
        APR_RING_INSERT_TAIL(&(pollset->query_ring), ep, pfd_elem_t, link);
 
286
 
 
287
    }
 
288
 
 
289
    pollset_unlock_rings();
 
290
 
 
291
    ret = port_getn(pollset->port_fd, pollset->port_set, pollset->nalloc,
 
292
                    &nget, tvptr);
 
293
 
 
294
    (*num) = nget;
 
295
 
 
296
    if (ret == -1) {
 
297
        (*num) = 0;
 
298
        if (errno == ETIME || errno == EINTR) {
 
299
            rv = APR_TIMEUP;
 
300
        }
 
301
        else {
 
302
            rv = APR_EGENERAL;
 
303
        }
 
304
    }
 
305
    else if (nget == 0) {
 
306
        rv = APR_TIMEUP;
 
307
    }
 
308
    else {
 
309
 
 
310
        pollset_lock_rings();
 
311
 
 
312
        for (i = 0; i < nget; i++) {
 
313
            pollset->result_set[i] =
 
314
                (((pfd_elem_t*)(pollset->port_set[i].portev_user))->pfd);
 
315
            pollset->result_set[i].rtnevents =
 
316
                get_revent(pollset->port_set[i].portev_events);
 
317
 
 
318
            APR_RING_REMOVE((pfd_elem_t*)pollset->port_set[i].portev_user, link);
 
319
 
 
320
            APR_RING_INSERT_TAIL(&(pollset->add_ring), 
 
321
                                 (pfd_elem_t*)pollset->port_set[i].portev_user,
 
322
                                 pfd_elem_t, link);
 
323
        }
 
324
 
 
325
        pollset_unlock_rings();
 
326
 
 
327
        if (descriptors) {
 
328
            *descriptors = pollset->result_set;
 
329
        }
 
330
    }
 
331
 
 
332
 
 
333
    pollset_lock_rings();
 
334
 
 
335
    /* Shift all PFDs in the Dead Ring to be Free Ring */
 
336
    APR_RING_CONCAT(&(pollset->free_ring), &(pollset->dead_ring), pfd_elem_t, link);
 
337
 
 
338
    pollset_unlock_rings();
 
339
 
 
340
    return rv;
 
341
}
 
342
 
 
343
#endif /* POLLSET_USES_PORT */