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

« back to all changes in this revision

Viewing changes to srclib/apr/poll/unix/poll.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
#if defined(POLL_USES_POLL) || defined(POLLSET_USES_POLL)
 
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
#endif /* POLL_USES_POLL || POLLSET_USES_POLL */
 
62
 
 
63
 
 
64
#ifdef POLL_USES_POLL
 
65
 
 
66
#define SMALL_POLLSET_LIMIT  8
 
67
 
 
68
APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, apr_int32_t num,
 
69
                                   apr_int32_t *nsds, 
 
70
                                   apr_interval_time_t timeout)
 
71
{
 
72
    int i, num_to_poll;
 
73
#ifdef HAVE_VLA
 
74
    /* XXX: I trust that this is a segv when insufficient stack exists? */
 
75
    struct pollfd pollset[num];
 
76
#elif defined(HAVE_ALLOCA)
 
77
    struct pollfd *pollset = alloca(sizeof(struct pollfd) * num);
 
78
    if (!pollset)
 
79
        return APR_ENOMEM;
 
80
#else
 
81
    struct pollfd tmp_pollset[SMALL_POLLSET_LIMIT];
 
82
    struct pollfd *pollset;
 
83
 
 
84
    if (num <= SMALL_POLLSET_LIMIT) {
 
85
        pollset = tmp_pollset;
 
86
    }
 
87
    else {
 
88
        /* This does require O(n) to copy the descriptors to the internal
 
89
         * mapping.
 
90
         */
 
91
        pollset = malloc(sizeof(struct pollfd) * num);
 
92
        /* The other option is adding an apr_pool_abort() fn to invoke
 
93
         * the pool's out of memory handler
 
94
         */
 
95
        if (!pollset)
 
96
            return APR_ENOMEM;
 
97
    }
 
98
#endif
 
99
    for (i = 0; i < num; i++) {
 
100
        if (aprset[i].desc_type == APR_POLL_SOCKET) {
 
101
            pollset[i].fd = aprset[i].desc.s->socketdes;
 
102
        }
 
103
        else if (aprset[i].desc_type == APR_POLL_FILE) {
 
104
            pollset[i].fd = aprset[i].desc.f->filedes;
 
105
        }
 
106
        else {
 
107
            break;
 
108
        }
 
109
        pollset[i].events = get_event(aprset[i].reqevents);
 
110
    }
 
111
    num_to_poll = i;
 
112
 
 
113
    if (timeout > 0) {
 
114
        timeout /= 1000; /* convert microseconds to milliseconds */
 
115
    }
 
116
 
 
117
    i = poll(pollset, num_to_poll, timeout);
 
118
    (*nsds) = i;
 
119
 
 
120
    if (i > 0) { /* poll() sets revents only if an event was signalled;
 
121
                  * we don't promise to set rtnevents unless an event
 
122
                  * was signalled
 
123
                  */
 
124
        for (i = 0; i < num; i++) {
 
125
            aprset[i].rtnevents = get_revent(pollset[i].revents);
 
126
        }
 
127
    }
 
128
    
 
129
#if !defined(HAVE_VLA) && !defined(HAVE_ALLOCA)
 
130
    if (num > SMALL_POLLSET_LIMIT) {
 
131
        free(pollset);
 
132
    }
 
133
#endif
 
134
 
 
135
    if ((*nsds) < 0) {
 
136
        return apr_get_netos_error();
 
137
    }
 
138
    if ((*nsds) == 0) {
 
139
        return APR_TIMEUP;
 
140
    }
 
141
    return APR_SUCCESS;
 
142
}
 
143
 
 
144
 
 
145
#endif /* POLL_USES_POLL */
 
146
 
 
147
 
 
148
#ifdef POLLSET_USES_POLL
 
149
 
 
150
struct apr_pollset_t
 
151
{
 
152
    apr_pool_t *pool;
 
153
    apr_uint32_t nelts;
 
154
    apr_uint32_t nalloc;
 
155
    struct pollfd *pollset;
 
156
    apr_pollfd_t *query_set;
 
157
    apr_pollfd_t *result_set;
 
158
};
 
159
 
 
160
APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset,
 
161
                                             apr_uint32_t size,
 
162
                                             apr_pool_t *p,
 
163
                                             apr_uint32_t flags)
 
164
{
 
165
    if (flags & APR_POLLSET_THREADSAFE) {                
 
166
        *pollset = NULL;
 
167
        return APR_ENOTIMPL;
 
168
    }
 
169
 
 
170
    *pollset = apr_palloc(p, sizeof(**pollset));
 
171
    (*pollset)->nelts = 0;
 
172
    (*pollset)->nalloc = size;
 
173
    (*pollset)->pool = p;
 
174
    (*pollset)->pollset = apr_palloc(p, size * sizeof(struct pollfd));
 
175
    (*pollset)->query_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
 
176
    (*pollset)->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
 
177
    return APR_SUCCESS;
 
178
}
 
179
 
 
180
APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t *pollset)
 
181
{
 
182
    return APR_SUCCESS;
 
183
}
 
184
 
 
185
APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset,
 
186
                                          const apr_pollfd_t *descriptor)
 
187
{
 
188
    if (pollset->nelts == pollset->nalloc) {
 
189
        return APR_ENOMEM;
 
190
    }
 
191
 
 
192
    pollset->query_set[pollset->nelts] = *descriptor;
 
193
 
 
194
    if (descriptor->desc_type == APR_POLL_SOCKET) {
 
195
        pollset->pollset[pollset->nelts].fd = descriptor->desc.s->socketdes;
 
196
    }
 
197
    else {
 
198
        pollset->pollset[pollset->nelts].fd = descriptor->desc.f->filedes;
 
199
    }
 
200
 
 
201
    pollset->pollset[pollset->nelts].events =
 
202
        get_event(descriptor->reqevents);
 
203
    pollset->nelts++;
 
204
 
 
205
    return APR_SUCCESS;
 
206
}
 
207
 
 
208
APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset,
 
209
                                             const apr_pollfd_t *descriptor)
 
210
{
 
211
    apr_uint32_t i;
 
212
 
 
213
    for (i = 0; i < pollset->nelts; i++) {
 
214
        if (descriptor->desc.s == pollset->query_set[i].desc.s) {
 
215
            /* Found an instance of the fd: remove this and any other copies */
 
216
            apr_uint32_t dst = i;
 
217
            apr_uint32_t old_nelts = pollset->nelts;
 
218
            pollset->nelts--;
 
219
            for (i++; i < old_nelts; i++) {
 
220
                if (descriptor->desc.s == pollset->query_set[i].desc.s) {
 
221
                    pollset->nelts--;
 
222
                }
 
223
                else {
 
224
                    pollset->pollset[dst] = pollset->pollset[i];
 
225
                    pollset->query_set[dst] = pollset->query_set[i];
 
226
                    dst++;
 
227
                }
 
228
            }
 
229
            return APR_SUCCESS;
 
230
        }
 
231
    }
 
232
 
 
233
    return APR_NOTFOUND;
 
234
}
 
235
 
 
236
APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
 
237
                                           apr_interval_time_t timeout,
 
238
                                           apr_int32_t *num,
 
239
                                           const apr_pollfd_t **descriptors)
 
240
{
 
241
    int rv;
 
242
    apr_uint32_t i, j;
 
243
 
 
244
    if (timeout > 0) {
 
245
        timeout /= 1000;
 
246
    }
 
247
    rv = poll(pollset->pollset, pollset->nelts, timeout);
 
248
    (*num) = rv;
 
249
    if (rv < 0) {
 
250
        return apr_get_netos_error();
 
251
    }
 
252
    if (rv == 0) {
 
253
        return APR_TIMEUP;
 
254
    }
 
255
    j = 0;
 
256
    for (i = 0; i < pollset->nelts; i++) {
 
257
        if (pollset->pollset[i].revents != 0) {
 
258
            pollset->result_set[j] = pollset->query_set[i];
 
259
            pollset->result_set[j].rtnevents =
 
260
                get_revent(pollset->pollset[i].revents);
 
261
            j++;
 
262
        }
 
263
    }
 
264
    if (descriptors)
 
265
        *descriptors = pollset->result_set;
 
266
    return APR_SUCCESS;
 
267
}
 
268
 
 
269
#endif /* POLLSET_USES_POLL */