upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / poll / unix / pollacc.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 "apr.h"
18 #include "apr_poll.h"
19 #include "apr_arch_networkio.h"
20 #include "apr_arch_file_io.h"
21 #if HAVE_POLL_H
22 #include <poll.h>
23 #endif
24 #if HAVE_SYS_POLL_H
25 #include <sys/poll.h>
26 #endif
27
28 APR_DECLARE(apr_status_t) apr_poll_setup(apr_pollfd_t **new, apr_int32_t num, apr_pool_t *cont)
29 {
30     (*new) = (apr_pollfd_t *)apr_pcalloc(cont, sizeof(apr_pollfd_t) * (num + 1));
31     if ((*new) == NULL) {
32         return APR_ENOMEM;
33     }
34     (*new)[num].desc_type = APR_POLL_LASTDESC;
35     (*new)[0].p = cont;
36     return APR_SUCCESS;
37 }
38
39 static apr_pollfd_t *find_poll_sock(apr_pollfd_t *aprset, apr_socket_t *sock)
40 {
41     apr_pollfd_t *curr = aprset;
42     
43     while (curr->desc.s != sock) {
44         if (curr->desc_type == APR_POLL_LASTDESC) {
45             return NULL;
46         }
47         curr++;
48     }
49
50     return curr;
51 }
52
53 APR_DECLARE(apr_status_t) apr_poll_socket_add(apr_pollfd_t *aprset, 
54                                apr_socket_t *sock, apr_int16_t event)
55 {
56     apr_pollfd_t *curr = aprset;
57     
58     while (curr->desc_type != APR_NO_DESC) {
59         if (curr->desc_type == APR_POLL_LASTDESC) {
60             return APR_ENOMEM;
61         }
62         curr++;
63     }
64     curr->desc.s = sock;
65     curr->desc_type = APR_POLL_SOCKET;
66     curr->reqevents = event;
67
68     return APR_SUCCESS;
69 }
70
71 APR_DECLARE(apr_status_t) apr_poll_revents_get(apr_int16_t *event, apr_socket_t *sock, apr_pollfd_t *aprset)
72 {
73     apr_pollfd_t *curr = find_poll_sock(aprset, sock);
74     if (curr == NULL) {
75         return APR_NOTFOUND;
76     }
77
78     (*event) = curr->rtnevents;
79     return APR_SUCCESS;
80 }
81
82 APR_DECLARE(apr_status_t) apr_poll_socket_mask(apr_pollfd_t *aprset, 
83                                   apr_socket_t *sock, apr_int16_t events)
84 {
85     apr_pollfd_t *curr = find_poll_sock(aprset, sock);
86     if (curr == NULL) {
87         return APR_NOTFOUND;
88     }
89     
90     if (curr->reqevents & events) {
91         curr->reqevents ^= events;
92     }
93
94     return APR_SUCCESS;
95 }
96
97 APR_DECLARE(apr_status_t) apr_poll_socket_remove(apr_pollfd_t *aprset, apr_socket_t *sock)
98 {
99     apr_pollfd_t *match = NULL;
100     apr_pollfd_t *curr;
101
102     for (curr = aprset; (curr->desc_type != APR_POLL_LASTDESC) &&
103              (curr->desc_type != APR_NO_DESC); curr++) {
104         if (curr->desc.s == sock) {
105             match = curr;
106         }
107     }
108     if (match == NULL) {
109         return APR_NOTFOUND;
110     }
111
112     /* Remove this entry by swapping the last entry into its place.
113      * This ensures that the non-APR_NO_DESC entries are all at the
114      * start of the array, so that apr_poll() doesn't have to worry
115      * about invalid entries in the middle of the pollset.
116      */
117     curr--;
118     if (curr != match) {
119         *match = *curr;
120     }
121     curr->desc_type = APR_NO_DESC;
122
123     return APR_SUCCESS;
124 }
125
126 APR_DECLARE(apr_status_t) apr_poll_socket_clear(apr_pollfd_t *aprset, apr_int16_t events)
127 {
128     apr_pollfd_t *curr = aprset;
129
130     while (curr->desc_type != APR_POLL_LASTDESC) {
131         if (curr->reqevents & events) {
132             curr->reqevents &= ~events;
133         }
134         curr++;
135     }
136     return APR_SUCCESS;
137 }
138
139 #if APR_FILES_AS_SOCKETS
140 /* I'm not sure if this needs to return an apr_status_t or not, but
141  * for right now, we'll leave it this way, and change it later if
142  * necessary.
143  */
144 APR_DECLARE(apr_status_t) apr_socket_from_file(apr_socket_t **newsock, apr_file_t *file)
145 {
146     (*newsock) = apr_pcalloc(file->pool, sizeof(**newsock));
147     (*newsock)->socketdes = file->filedes;
148     (*newsock)->cntxt = file->pool;
149     (*newsock)->timeout = file->timeout;
150     return APR_SUCCESS;
151 }
152 #endif