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

« back to all changes in this revision

Viewing changes to srclib/apr/poll/unix/select.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
#ifdef WIN32
 
18
/* POSIX defines 1024 for the FD_SETSIZE */
 
19
#define FD_SETSIZE 1024
 
20
#endif
 
21
 
 
22
#include "apr.h"
 
23
#include "apr_poll.h"
 
24
#include "apr_time.h"
 
25
#include "apr_portable.h"
 
26
#include "apr_arch_networkio.h"
 
27
#include "apr_arch_file_io.h"
 
28
#include "apr_arch_poll_private.h"
 
29
 
 
30
#ifdef POLL_USES_SELECT
 
31
 
 
32
APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, int num,
 
33
                                   apr_int32_t *nsds,
 
34
                                   apr_interval_time_t timeout)
 
35
{
 
36
    fd_set readset, writeset, exceptset;
 
37
    int rv, i;
 
38
    int maxfd = -1;
 
39
    struct timeval tv, *tvptr;
 
40
#ifdef NETWARE
 
41
    apr_datatype_e set_type = APR_NO_DESC;
 
42
#endif
 
43
 
 
44
    if (timeout < 0) {
 
45
        tvptr = NULL;
 
46
    }
 
47
    else {
 
48
        tv.tv_sec = (long) apr_time_sec(timeout);
 
49
        tv.tv_usec = (long) apr_time_usec(timeout);
 
50
        tvptr = &tv;
 
51
    }
 
52
 
 
53
    FD_ZERO(&readset);
 
54
    FD_ZERO(&writeset);
 
55
    FD_ZERO(&exceptset);
 
56
 
 
57
    for (i = 0; i < num; i++) {
 
58
        apr_os_sock_t fd;
 
59
 
 
60
        aprset[i].rtnevents = 0;
 
61
 
 
62
        if (aprset[i].desc_type == APR_POLL_SOCKET) {
 
63
#ifdef NETWARE
 
64
            if (HAS_PIPES(set_type)) {
 
65
                return APR_EBADF;
 
66
            }
 
67
            else {
 
68
                set_type = APR_POLL_SOCKET;
 
69
            }
 
70
#endif
 
71
            fd = aprset[i].desc.s->socketdes;
 
72
        }
 
73
        else if (aprset[i].desc_type == APR_POLL_FILE) {
 
74
#if !APR_FILES_AS_SOCKETS
 
75
            return APR_EBADF;
 
76
#else
 
77
#ifdef NETWARE
 
78
            if (aprset[i].desc.f->is_pipe && !HAS_SOCKETS(set_type)) {
 
79
                set_type = APR_POLL_FILE;
 
80
            }
 
81
            else
 
82
                return APR_EBADF;
 
83
#endif /* NETWARE */
 
84
 
 
85
            fd = aprset[i].desc.f->filedes;
 
86
 
 
87
#endif /* APR_FILES_AS_SOCKETS */
 
88
        }
 
89
        else {
 
90
            break;
 
91
        }
 
92
#if !defined(WIN32) && !defined(NETWARE)        /* socket sets handled with array of handles */
 
93
        if (fd >= FD_SETSIZE) {
 
94
            /* XXX invent new error code so application has a clue */
 
95
            return APR_EBADF;
 
96
        }
 
97
#endif
 
98
        if (aprset[i].reqevents & APR_POLLIN) {
 
99
            FD_SET(fd, &readset);
 
100
        }
 
101
        if (aprset[i].reqevents & APR_POLLOUT) {
 
102
            FD_SET(fd, &writeset);
 
103
        }
 
104
        if (aprset[i].reqevents &
 
105
            (APR_POLLPRI | APR_POLLERR | APR_POLLHUP | APR_POLLNVAL)) {
 
106
            FD_SET(fd, &exceptset);
 
107
        }
 
108
        if ((int) fd > maxfd) {
 
109
            maxfd = (int) fd;
 
110
        }
 
111
    }
 
112
 
 
113
#ifdef NETWARE
 
114
    if (HAS_PIPES(set_type)) {
 
115
        rv = pipe_select(maxfd + 1, &readset, &writeset, &exceptset, tvptr);
 
116
    }
 
117
    else {
 
118
#endif
 
119
 
 
120
        rv = select(maxfd + 1, &readset, &writeset, &exceptset, tvptr);
 
121
 
 
122
#ifdef NETWARE
 
123
    }
 
124
#endif
 
125
 
 
126
    (*nsds) = rv;
 
127
    if ((*nsds) == 0) {
 
128
        return APR_TIMEUP;
 
129
    }
 
130
    if ((*nsds) < 0) {
 
131
        return apr_get_netos_error();
 
132
    }
 
133
 
 
134
    (*nsds) = 0;
 
135
    for (i = 0; i < num; i++) {
 
136
        apr_os_sock_t fd;
 
137
 
 
138
        if (aprset[i].desc_type == APR_POLL_SOCKET) {
 
139
            fd = aprset[i].desc.s->socketdes;
 
140
        }
 
141
        else if (aprset[i].desc_type == APR_POLL_FILE) {
 
142
#if !APR_FILES_AS_SOCKETS
 
143
            return APR_EBADF;
 
144
#else
 
145
            fd = aprset[i].desc.f->filedes;
 
146
#endif
 
147
        }
 
148
        else {
 
149
            break;
 
150
        }
 
151
        if (FD_ISSET(fd, &readset)) {
 
152
            aprset[i].rtnevents |= APR_POLLIN;
 
153
        }
 
154
        if (FD_ISSET(fd, &writeset)) {
 
155
            aprset[i].rtnevents |= APR_POLLOUT;
 
156
        }
 
157
        if (FD_ISSET(fd, &exceptset)) {
 
158
            aprset[i].rtnevents |= APR_POLLERR;
 
159
        }
 
160
        if (aprset[i].rtnevents) {
 
161
            (*nsds)++;
 
162
        }
 
163
    }
 
164
 
 
165
    return APR_SUCCESS;
 
166
}
 
167
 
 
168
#endif /* POLL_USES_SELECT */
 
169
 
 
170
#ifdef POLLSET_USES_SELECT
 
171
 
 
172
struct apr_pollset_t
 
173
{
 
174
    apr_pool_t *pool;
 
175
 
 
176
    apr_uint32_t nelts;
 
177
    apr_uint32_t nalloc;
 
178
    fd_set readset, writeset, exceptset;
 
179
    int maxfd;
 
180
    apr_pollfd_t *query_set;
 
181
    apr_pollfd_t *result_set;
 
182
#ifdef NETWARE
 
183
    int set_type;
 
184
#endif
 
185
};
 
186
 
 
187
APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset,
 
188
                                             apr_uint32_t size,
 
189
                                             apr_pool_t *p,
 
190
                                             apr_uint32_t flags)
 
191
{
 
192
    if (flags & APR_POLLSET_THREADSAFE) {                
 
193
        *pollset = NULL;
 
194
        return APR_ENOTIMPL;
 
195
    }
 
196
#ifdef FD_SETSIZE
 
197
    if (size > FD_SETSIZE) {
 
198
        *pollset = NULL;
 
199
        return APR_EINVAL;
 
200
    }
 
201
#endif
 
202
    *pollset = apr_palloc(p, sizeof(**pollset));
 
203
    (*pollset)->nelts = 0;
 
204
    (*pollset)->nalloc = size;
 
205
    (*pollset)->pool = p;
 
206
    FD_ZERO(&((*pollset)->readset));
 
207
    FD_ZERO(&((*pollset)->writeset));
 
208
    FD_ZERO(&((*pollset)->exceptset));
 
209
    (*pollset)->maxfd = 0;
 
210
#ifdef NETWARE
 
211
    (*pollset)->set_type = APR_NO_DESC;
 
212
#endif
 
213
    (*pollset)->query_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
 
214
    (*pollset)->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
 
215
 
 
216
    return APR_SUCCESS;
 
217
}
 
218
 
 
219
APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t * pollset)
 
220
{
 
221
    return APR_SUCCESS;
 
222
}
 
223
 
 
224
APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset,
 
225
                                          const apr_pollfd_t *descriptor)
 
226
{
 
227
    apr_os_sock_t fd;
 
228
 
 
229
    if (pollset->nelts == pollset->nalloc) {
 
230
        return APR_ENOMEM;
 
231
    }
 
232
 
 
233
    pollset->query_set[pollset->nelts] = *descriptor;
 
234
 
 
235
    if (descriptor->desc_type == APR_POLL_SOCKET) {
 
236
#ifdef NETWARE
 
237
        /* NetWare can't handle mixed descriptor types in select() */
 
238
        if (HAS_PIPES(pollset->set_type)) {
 
239
            return APR_EBADF;
 
240
        }
 
241
        else {
 
242
            pollset->set_type = APR_POLL_SOCKET;
 
243
        }
 
244
#endif
 
245
        fd = descriptor->desc.s->socketdes;
 
246
    }
 
247
    else {
 
248
#if !APR_FILES_AS_SOCKETS
 
249
        return APR_EBADF;
 
250
#else
 
251
#ifdef NETWARE
 
252
        /* NetWare can't handle mixed descriptor types in select() */
 
253
        if (descriptor->desc.f->is_pipe && !HAS_SOCKETS(pollset->set_type)) {
 
254
            pollset->set_type = APR_POLL_FILE;
 
255
            fd = descriptor->desc.f->filedes;
 
256
        }
 
257
        else {
 
258
            return APR_EBADF;
 
259
        }
 
260
#else
 
261
        fd = descriptor->desc.f->filedes;
 
262
#endif
 
263
#endif
 
264
    }
 
265
#if !defined(WIN32) && !defined(NETWARE)        /* socket sets handled with array of handles */
 
266
    if (fd >= FD_SETSIZE) {
 
267
        /* XXX invent new error code so application has a clue */
 
268
        return APR_EBADF;
 
269
    }
 
270
#endif
 
271
    if (descriptor->reqevents & APR_POLLIN) {
 
272
        FD_SET(fd, &(pollset->readset));
 
273
    }
 
274
    if (descriptor->reqevents & APR_POLLOUT) {
 
275
        FD_SET(fd, &(pollset->writeset));
 
276
    }
 
277
    if (descriptor->reqevents &
 
278
        (APR_POLLPRI | APR_POLLERR | APR_POLLHUP | APR_POLLNVAL)) {
 
279
        FD_SET(fd, &(pollset->exceptset));
 
280
    }
 
281
    if ((int) fd > pollset->maxfd) {
 
282
        pollset->maxfd = (int) fd;
 
283
    }
 
284
    pollset->nelts++;
 
285
    return APR_SUCCESS;
 
286
}
 
287
 
 
288
APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t * pollset,
 
289
                                             const apr_pollfd_t * descriptor)
 
290
{
 
291
    apr_uint32_t i;
 
292
    apr_os_sock_t fd;
 
293
 
 
294
    if (descriptor->desc_type == APR_POLL_SOCKET) {
 
295
        fd = descriptor->desc.s->socketdes;
 
296
    }
 
297
    else {
 
298
#if !APR_FILES_AS_SOCKETS
 
299
        return APR_EBADF;
 
300
#else
 
301
        fd = descriptor->desc.f->filedes;
 
302
#endif
 
303
    }
 
304
 
 
305
    for (i = 0; i < pollset->nelts; i++) {
 
306
        if (descriptor->desc.s == pollset->query_set[i].desc.s) {
 
307
            /* Found an instance of the fd: remove this and any other copies */
 
308
            apr_uint32_t dst = i;
 
309
            apr_uint32_t old_nelts = pollset->nelts;
 
310
            pollset->nelts--;
 
311
            for (i++; i < old_nelts; i++) {
 
312
                if (descriptor->desc.s == pollset->query_set[i].desc.s) {
 
313
                    pollset->nelts--;
 
314
                }
 
315
                else {
 
316
                    pollset->query_set[dst] = pollset->query_set[i];
 
317
                    dst++;
 
318
                }
 
319
            }
 
320
            FD_CLR(fd, &(pollset->readset));
 
321
            FD_CLR(fd, &(pollset->writeset));
 
322
            FD_CLR(fd, &(pollset->exceptset));
 
323
            if (((int) fd == pollset->maxfd) && (pollset->maxfd > 0)) {
 
324
                pollset->maxfd--;
 
325
            }
 
326
            return APR_SUCCESS;
 
327
        }
 
328
    }
 
329
 
 
330
    return APR_NOTFOUND;
 
331
}
 
332
 
 
333
APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
 
334
                                           apr_interval_time_t timeout,
 
335
                                           apr_int32_t *num,
 
336
                                           const apr_pollfd_t **descriptors)
 
337
{
 
338
    int rv;
 
339
    apr_uint32_t i, j;
 
340
    struct timeval tv, *tvptr;
 
341
    fd_set readset, writeset, exceptset;
 
342
 
 
343
    if (timeout < 0) {
 
344
        tvptr = NULL;
 
345
    }
 
346
    else {
 
347
        tv.tv_sec = (long) apr_time_sec(timeout);
 
348
        tv.tv_usec = (long) apr_time_usec(timeout);
 
349
        tvptr = &tv;
 
350
    }
 
351
 
 
352
    memcpy(&readset, &(pollset->readset), sizeof(fd_set));
 
353
    memcpy(&writeset, &(pollset->writeset), sizeof(fd_set));
 
354
    memcpy(&exceptset, &(pollset->exceptset), sizeof(fd_set));
 
355
 
 
356
#ifdef NETWARE
 
357
    if (HAS_PIPES(pollset->set_type)) {
 
358
        rv = pipe_select(pollset->maxfd + 1, &readset, &writeset, &exceptset,
 
359
                         tvptr);
 
360
    }
 
361
    else
 
362
#endif
 
363
        rv = select(pollset->maxfd + 1, &readset, &writeset, &exceptset,
 
364
                    tvptr);
 
365
 
 
366
    (*num) = rv;
 
367
    if (rv < 0) {
 
368
        return apr_get_netos_error();
 
369
    }
 
370
    if (rv == 0) {
 
371
        return APR_TIMEUP;
 
372
    }
 
373
    j = 0;
 
374
    for (i = 0; i < pollset->nelts; i++) {
 
375
        apr_os_sock_t fd;
 
376
        if (pollset->query_set[i].desc_type == APR_POLL_SOCKET) {
 
377
            fd = pollset->query_set[i].desc.s->socketdes;
 
378
        }
 
379
        else {
 
380
#if !APR_FILES_AS_SOCKETS
 
381
            return APR_EBADF;
 
382
#else
 
383
            fd = pollset->query_set[i].desc.f->filedes;
 
384
#endif
 
385
        }
 
386
        if (FD_ISSET(fd, &readset) || FD_ISSET(fd, &writeset) ||
 
387
            FD_ISSET(fd, &exceptset)) {
 
388
            pollset->result_set[j] = pollset->query_set[i];
 
389
            pollset->result_set[j].rtnevents = 0;
 
390
            if (FD_ISSET(fd, &readset)) {
 
391
                pollset->result_set[j].rtnevents |= APR_POLLIN;
 
392
            }
 
393
            if (FD_ISSET(fd, &writeset)) {
 
394
                pollset->result_set[j].rtnevents |= APR_POLLOUT;
 
395
            }
 
396
            if (FD_ISSET(fd, &exceptset)) {
 
397
                pollset->result_set[j].rtnevents |= APR_POLLERR;
 
398
            }
 
399
            j++;
 
400
        }
 
401
    }
 
402
    (*num) = j;
 
403
 
 
404
    if (descriptors)
 
405
        *descriptors = pollset->result_set;
 
406
    return APR_SUCCESS;
 
407
}
 
408
 
 
409
#endif /* POLLSET_USES_SELECT */