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

« back to all changes in this revision

Viewing changes to srclib/apr/poll/unix/epoll.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_EPOLL
 
20
 
 
21
static apr_int16_t get_epoll_event(apr_int16_t event)
 
22
{
 
23
    apr_int16_t rv = 0;
 
24
 
 
25
    if (event & APR_POLLIN)
 
26
        rv |= EPOLLIN;
 
27
    if (event & APR_POLLPRI)
 
28
        rv |= EPOLLPRI;
 
29
    if (event & APR_POLLOUT)
 
30
        rv |= EPOLLOUT;
 
31
    if (event & APR_POLLERR)
 
32
        rv |= EPOLLERR;
 
33
    if (event & APR_POLLHUP)
 
34
        rv |= EPOLLHUP;
 
35
    /* APR_POLLNVAL is not handled by epoll. */
 
36
 
 
37
    return rv;
 
38
}
 
39
 
 
40
static apr_int16_t get_epoll_revent(apr_int16_t event)
 
41
{
 
42
    apr_int16_t rv = 0;
 
43
 
 
44
    if (event & EPOLLIN)
 
45
        rv |= APR_POLLIN;
 
46
    if (event & EPOLLPRI)
 
47
        rv |= APR_POLLPRI;
 
48
    if (event & EPOLLOUT)
 
49
        rv |= APR_POLLOUT;
 
50
    if (event & EPOLLERR)
 
51
        rv |= APR_POLLERR;
 
52
    if (event & EPOLLHUP)
 
53
        rv |= APR_POLLHUP;
 
54
    /* APR_POLLNVAL is not handled by epoll. */
 
55
 
 
56
    return rv;
 
57
}
 
58
 
 
59
struct apr_pollset_t
 
60
{
 
61
    apr_pool_t *pool;
 
62
    apr_uint32_t nelts;
 
63
    apr_uint32_t nalloc;
 
64
    int epoll_fd;
 
65
    struct epoll_event *pollset;
 
66
    apr_pollfd_t *result_set;
 
67
    apr_uint32_t flags;
 
68
#if APR_HAS_THREADS
 
69
    /* A thread mutex to protect operations on the rings */
 
70
    apr_thread_mutex_t *ring_lock;
 
71
#endif
 
72
    /* A ring containing all of the pollfd_t that are active */
 
73
    APR_RING_HEAD(pfd_query_ring_t, pfd_elem_t) query_ring;
 
74
    /* A ring of pollfd_t that have been used, and then _remove()'d */
 
75
    APR_RING_HEAD(pfd_free_ring_t, pfd_elem_t) free_ring;
 
76
    /* A ring of pollfd_t where rings that have been _remove()`ed but
 
77
        might still be inside a _poll() */
 
78
    APR_RING_HEAD(pfd_dead_ring_t, pfd_elem_t) dead_ring;
 
79
};
 
80
 
 
81
static apr_status_t backend_cleanup(void *p_)
 
82
{
 
83
    apr_pollset_t *pollset = (apr_pollset_t *) p_;
 
84
    close(pollset->epoll_fd);
 
85
    return APR_SUCCESS;
 
86
}
 
87
 
 
88
APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset,
 
89
                                             apr_uint32_t size,
 
90
                                             apr_pool_t *p,
 
91
                                             apr_uint32_t flags)
 
92
{
 
93
    apr_status_t rv;
 
94
    int fd;
 
95
 
 
96
    fd = epoll_create(size);
 
97
    if (fd < 0) {
 
98
        *pollset = NULL;
 
99
        return errno;
 
100
    }
 
101
 
 
102
    *pollset = apr_palloc(p, sizeof(**pollset));
 
103
#if APR_HAS_THREADS
 
104
    if (flags & APR_POLLSET_THREADSAFE &&
 
105
        ((rv = apr_thread_mutex_create(&(*pollset)->ring_lock,
 
106
                                       APR_THREAD_MUTEX_DEFAULT,
 
107
                                       p) != APR_SUCCESS))) {
 
108
        *pollset = NULL;
 
109
        return rv;
 
110
    }
 
111
#else
 
112
    if (flags & APR_POLLSET_THREADSAFE) {
 
113
        *pollset = NULL;
 
114
        return APR_ENOTIMPL;
 
115
    }
 
116
#endif
 
117
    (*pollset)->nelts = 0;
 
118
    (*pollset)->nalloc = size;
 
119
    (*pollset)->flags = flags;
 
120
    (*pollset)->pool = p;
 
121
    (*pollset)->epoll_fd = fd;
 
122
    (*pollset)->pollset = apr_palloc(p, size * sizeof(struct epoll_event));
 
123
    apr_pool_cleanup_register(p, *pollset, backend_cleanup, backend_cleanup);
 
124
    (*pollset)->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
 
125
 
 
126
    APR_RING_INIT(&(*pollset)->query_ring, pfd_elem_t, link);
 
127
    APR_RING_INIT(&(*pollset)->free_ring, pfd_elem_t, link);
 
128
    APR_RING_INIT(&(*pollset)->dead_ring, pfd_elem_t, link);
 
129
 
 
130
    return APR_SUCCESS;
 
131
}
 
132
 
 
133
APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t *pollset)
 
134
{
 
135
    return apr_pool_cleanup_run(pollset->pool, pollset, backend_cleanup);
 
136
}
 
137
 
 
138
APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset,
 
139
                                          const apr_pollfd_t *descriptor)
 
140
{
 
141
    struct epoll_event ev;
 
142
    int ret = -1;
 
143
    pfd_elem_t *elem;
 
144
    apr_status_t rv = APR_SUCCESS;
 
145
 
 
146
    pollset_lock_rings();
 
147
 
 
148
    if (!APR_RING_EMPTY(&(pollset->free_ring), pfd_elem_t, link)) {
 
149
        elem = APR_RING_FIRST(&(pollset->free_ring));
 
150
        APR_RING_REMOVE(elem, link);
 
151
    }
 
152
    else {
 
153
        elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t));
 
154
        APR_RING_ELEM_INIT(elem, link);
 
155
    }
 
156
    elem->pfd = *descriptor;
 
157
 
 
158
    ev.events = get_epoll_event(descriptor->reqevents);
 
159
    ev.data.ptr = elem;
 
160
    if (descriptor->desc_type == APR_POLL_SOCKET) {
 
161
        ret = epoll_ctl(pollset->epoll_fd, EPOLL_CTL_ADD,
 
162
                        descriptor->desc.s->socketdes, &ev);
 
163
    }
 
164
    else {
 
165
        ret = epoll_ctl(pollset->epoll_fd, EPOLL_CTL_ADD,
 
166
                        descriptor->desc.f->filedes, &ev);
 
167
    }
 
168
 
 
169
    if (0 != ret) {
 
170
        rv = APR_EBADF;
 
171
        APR_RING_INSERT_TAIL(&(pollset->free_ring), elem, pfd_elem_t, link);
 
172
    }
 
173
    else {
 
174
        pollset->nelts++;
 
175
        APR_RING_INSERT_TAIL(&(pollset->query_ring), elem, pfd_elem_t, link);
 
176
    }
 
177
 
 
178
    pollset_unlock_rings();
 
179
 
 
180
    return rv;
 
181
}
 
182
 
 
183
APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset,
 
184
                                             const apr_pollfd_t *descriptor)
 
185
{
 
186
    pfd_elem_t *ep;
 
187
    apr_status_t rv = APR_SUCCESS;
 
188
    struct epoll_event ev;
 
189
    int ret = -1;
 
190
 
 
191
    pollset_lock_rings();
 
192
 
 
193
    ev.events = get_epoll_event(descriptor->reqevents);
 
194
 
 
195
    if (descriptor->desc_type == APR_POLL_SOCKET) {
 
196
        ret = epoll_ctl(pollset->epoll_fd, EPOLL_CTL_DEL,
 
197
                        descriptor->desc.s->socketdes, &ev);
 
198
    }
 
199
    else {
 
200
        ret = epoll_ctl(pollset->epoll_fd, EPOLL_CTL_DEL,
 
201
                        descriptor->desc.f->filedes, &ev);
 
202
    }
 
203
    if (ret < 0) {
 
204
        rv = APR_NOTFOUND;
 
205
    }
 
206
 
 
207
    if (!APR_RING_EMPTY(&(pollset->query_ring), pfd_elem_t, link)) {
 
208
        for (ep = APR_RING_FIRST(&(pollset->query_ring));
 
209
             ep != APR_RING_SENTINEL(&(pollset->query_ring),
 
210
                                     pfd_elem_t, link);
 
211
             ep = APR_RING_NEXT(ep, link)) {
 
212
 
 
213
            if (descriptor->desc.s == ep->pfd.desc.s) {
 
214
                APR_RING_REMOVE(ep, link);
 
215
                APR_RING_INSERT_TAIL(&(pollset->dead_ring),
 
216
                                     ep, pfd_elem_t, link);
 
217
                break;
 
218
            }
 
219
        }
 
220
    }
 
221
 
 
222
    pollset_unlock_rings();
 
223
 
 
224
    return rv;
 
225
}
 
226
 
 
227
APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
 
228
                                           apr_interval_time_t timeout,
 
229
                                           apr_int32_t *num,
 
230
                                           const apr_pollfd_t **descriptors)
 
231
{
 
232
    int ret, i;
 
233
    apr_status_t rv = APR_SUCCESS;
 
234
 
 
235
    if (timeout > 0) {
 
236
        timeout /= 1000;
 
237
    }
 
238
 
 
239
    ret = epoll_wait(pollset->epoll_fd, pollset->pollset, pollset->nalloc,
 
240
                     timeout);
 
241
    (*num) = ret;
 
242
 
 
243
    if (ret < 0) {
 
244
        rv = apr_get_netos_error();
 
245
    }
 
246
    else if (ret == 0) {
 
247
        rv = APR_TIMEUP;
 
248
    }
 
249
    else {
 
250
        for (i = 0; i < ret; i++) {
 
251
            pollset->result_set[i] =
 
252
                (((pfd_elem_t *) (pollset->pollset[i].data.ptr))->pfd);
 
253
            pollset->result_set[i].rtnevents =
 
254
                get_epoll_revent(pollset->pollset[i].events);
 
255
        }
 
256
 
 
257
        if (descriptors) {
 
258
            *descriptors = pollset->result_set;
 
259
        }
 
260
    }
 
261
 
 
262
    pollset_lock_rings();
 
263
 
 
264
    /* Shift all PFDs in the Dead Ring to be Free Ring */
 
265
    APR_RING_CONCAT(&(pollset->free_ring), &(pollset->dead_ring), pfd_elem_t, link);
 
266
 
 
267
    pollset_unlock_rings();
 
268
 
 
269
    return rv;
 
270
}
 
271
 
 
272
#endif /* POLLSET_USES_EPOLL */