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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "apr_arch_networkio.h"
20 #include "apr_arch_file_io.h"
28 APR_DECLARE(apr_status_t) apr_poll_setup(apr_pollfd_t **new, apr_int32_t num, apr_pool_t *cont)
30 (*new) = (apr_pollfd_t *)apr_pcalloc(cont, sizeof(apr_pollfd_t) * (num + 1));
34 (*new)[num].desc_type = APR_POLL_LASTDESC;
39 static apr_pollfd_t *find_poll_sock(apr_pollfd_t *aprset, apr_socket_t *sock)
41 apr_pollfd_t *curr = aprset;
43 while (curr->desc.s != sock) {
44 if (curr->desc_type == APR_POLL_LASTDESC) {
53 APR_DECLARE(apr_status_t) apr_poll_socket_add(apr_pollfd_t *aprset,
54 apr_socket_t *sock, apr_int16_t event)
56 apr_pollfd_t *curr = aprset;
58 while (curr->desc_type != APR_NO_DESC) {
59 if (curr->desc_type == APR_POLL_LASTDESC) {
65 curr->desc_type = APR_POLL_SOCKET;
66 curr->reqevents = event;
71 APR_DECLARE(apr_status_t) apr_poll_revents_get(apr_int16_t *event, apr_socket_t *sock, apr_pollfd_t *aprset)
73 apr_pollfd_t *curr = find_poll_sock(aprset, sock);
78 (*event) = curr->rtnevents;
82 APR_DECLARE(apr_status_t) apr_poll_socket_mask(apr_pollfd_t *aprset,
83 apr_socket_t *sock, apr_int16_t events)
85 apr_pollfd_t *curr = find_poll_sock(aprset, sock);
90 if (curr->reqevents & events) {
91 curr->reqevents ^= events;
97 APR_DECLARE(apr_status_t) apr_poll_socket_remove(apr_pollfd_t *aprset, apr_socket_t *sock)
99 apr_pollfd_t *match = NULL;
102 for (curr = aprset; (curr->desc_type != APR_POLL_LASTDESC) &&
103 (curr->desc_type != APR_NO_DESC); curr++) {
104 if (curr->desc.s == sock) {
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.
121 curr->desc_type = APR_NO_DESC;
126 APR_DECLARE(apr_status_t) apr_poll_socket_clear(apr_pollfd_t *aprset, apr_int16_t events)
128 apr_pollfd_t *curr = aprset;
130 while (curr->desc_type != APR_POLL_LASTDESC) {
131 if (curr->reqevents & events) {
132 curr->reqevents &= ~events;
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
144 APR_DECLARE(apr_status_t) apr_socket_from_file(apr_socket_t **newsock, apr_file_t *file)
146 (*newsock) = apr_pcalloc(file->pool, sizeof(**newsock));
147 (*newsock)->socketdes = file->filedes;
148 (*newsock)->cntxt = file->pool;
149 (*newsock)->timeout = file->timeout;