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#ifndef APR_ARCH_POLL_PRIVATE_H
18#define APR_ARCH_POLL_PRIVATE_H
19
20#if HAVE_POLL_H
21#include <poll.h>
22#endif
23
24#if HAVE_SYS_POLL_H
25#include <sys/poll.h>
26#endif
27
28#ifdef HAVE_PORT_CREATE
29#include <port.h>
30#include <sys/port_impl.h>
31#endif
32
33#ifdef HAVE_KQUEUE
34#include <sys/types.h>
35#include <sys/event.h>
36#include <sys/time.h>
37#endif
38
39#ifdef HAVE_EPOLL
40#include <sys/epoll.h>
41#endif
42
43#ifdef NETWARE
44#define HAS_SOCKETS(dt) (dt == APR_POLL_SOCKET) ? 1 : 0
45#define HAS_PIPES(dt) (dt == APR_POLL_FILE) ? 1 : 0
46#endif
47
48#if defined(HAVE_AIO_H) && defined(HAVE_AIO_MSGQ)
49#define _AIO_OS390	/* enable a bunch of z/OS aio.h definitions */
50#include <aio.h>	/* aiocb	*/
51#endif
52
53/* Choose the best method platform specific to use in apr_pollset */
54#ifdef HAVE_KQUEUE
55#define POLLSET_USES_KQUEUE
56#define POLLSET_DEFAULT_METHOD APR_POLLSET_KQUEUE
57#elif defined(HAVE_PORT_CREATE)
58#define POLLSET_USES_PORT
59#define POLLSET_DEFAULT_METHOD APR_POLLSET_PORT
60#elif defined(HAVE_EPOLL)
61#define POLLSET_USES_EPOLL
62#define POLLSET_DEFAULT_METHOD APR_POLLSET_EPOLL
63#elif defined(HAVE_AIO_MSGQ)
64#define POLLSET_USES_AIO_MSGQ
65#define POLLSET_DEFAULT_METHOD APR_POLLSET_AIO_MSGQ
66#elif defined(HAVE_POLL)
67#define POLLSET_USES_POLL
68#define POLLSET_DEFAULT_METHOD APR_POLLSET_POLL
69#else
70#define POLLSET_USES_SELECT
71#define POLLSET_DEFAULT_METHOD APR_POLLSET_SELECT
72#endif
73
74#ifdef WIN32
75#define POLL_USES_SELECT
76#undef POLLSET_DEFAULT_METHOD
77#define POLLSET_DEFAULT_METHOD APR_POLLSET_SELECT
78#else
79#ifdef HAVE_POLL
80#define POLL_USES_POLL
81#else
82#define POLL_USES_SELECT
83#endif
84#endif
85
86#if defined(POLLSET_USES_KQUEUE) || defined(POLLSET_USES_EPOLL) || defined(POLLSET_USES_PORT) || defined(POLLSET_USES_AIO_MSGQ)
87
88#include "apr_ring.h"
89
90#if APR_HAS_THREADS
91#include "apr_thread_mutex.h"
92#define pollset_lock_rings() \
93    if (pollset->flags & APR_POLLSET_THREADSAFE) \
94        apr_thread_mutex_lock(pollset->p->ring_lock);
95#define pollset_unlock_rings() \
96    if (pollset->flags & APR_POLLSET_THREADSAFE) \
97        apr_thread_mutex_unlock(pollset->p->ring_lock);
98#else
99#define pollset_lock_rings()
100#define pollset_unlock_rings()
101#endif
102
103typedef struct pfd_elem_t pfd_elem_t;
104
105struct pfd_elem_t {
106    APR_RING_ENTRY(pfd_elem_t) link;
107    apr_pollfd_t pfd;
108#ifdef HAVE_PORT_CREATE
109   int on_query_ring;
110#endif
111};
112
113#endif
114
115typedef struct apr_pollset_private_t apr_pollset_private_t;
116typedef struct apr_pollset_provider_t apr_pollset_provider_t;
117typedef struct apr_pollcb_provider_t apr_pollcb_provider_t;
118
119struct apr_pollset_t
120{
121    apr_pool_t *pool;
122    apr_uint32_t nelts;
123    apr_uint32_t nalloc;
124    apr_uint32_t flags;
125    /* Pipe descriptors used for wakeup */
126    apr_file_t *wakeup_pipe[2];
127    apr_pollfd_t wakeup_pfd;
128    apr_pollset_private_t *p;
129    const apr_pollset_provider_t *provider;
130};
131
132typedef union {
133#if defined(HAVE_EPOLL)
134    struct epoll_event *epoll;
135#endif
136#if defined(HAVE_PORT_CREATE)
137    port_event_t *port;
138#endif
139#if defined(HAVE_KQUEUE)
140    struct kevent *ke;
141#endif
142#if defined(HAVE_POLL)
143    struct pollfd *ps;
144#endif
145    void *undef;
146} apr_pollcb_pset;
147
148struct apr_pollcb_t {
149    apr_pool_t *pool;
150    apr_uint32_t nelts;
151    apr_uint32_t nalloc;
152    apr_uint32_t flags;
153    /* Pipe descriptors used for wakeup */
154    apr_file_t *wakeup_pipe[2];
155    apr_pollfd_t wakeup_pfd;
156    int fd;
157    apr_pollcb_pset pollset;
158    apr_pollfd_t **copyset;
159    const apr_pollcb_provider_t *provider;
160};
161
162struct apr_pollset_provider_t {
163    apr_status_t (*create)(apr_pollset_t *, apr_uint32_t, apr_pool_t *, apr_uint32_t);
164    apr_status_t (*add)(apr_pollset_t *, const apr_pollfd_t *);
165    apr_status_t (*remove)(apr_pollset_t *, const apr_pollfd_t *);
166    apr_status_t (*poll)(apr_pollset_t *, apr_interval_time_t, apr_int32_t *, const apr_pollfd_t **);
167    apr_status_t (*cleanup)(apr_pollset_t *);
168    const char *name;
169};
170
171struct apr_pollcb_provider_t {
172    apr_status_t (*create)(apr_pollcb_t *, apr_uint32_t, apr_pool_t *, apr_uint32_t);
173    apr_status_t (*add)(apr_pollcb_t *, apr_pollfd_t *);
174    apr_status_t (*remove)(apr_pollcb_t *, apr_pollfd_t *);
175    apr_status_t (*poll)(apr_pollcb_t *, apr_interval_time_t, apr_pollcb_cb_t, void *);
176    apr_status_t (*cleanup)(apr_pollcb_t *);
177    const char *name;
178};
179
180/*
181 * Private functions used for the implementation of both apr_pollcb_* and
182 * apr_pollset_*
183 */
184apr_status_t apr_poll_create_wakeup_pipe(apr_pool_t *pool, apr_pollfd_t *pfd,
185                                         apr_file_t **wakeup_pipe);
186apr_status_t apr_poll_close_wakeup_pipe(apr_file_t **wakeup_pipe);
187void apr_poll_drain_wakeup_pipe(apr_file_t **wakeup_pipe);
188
189#endif /* APR_ARCH_POLL_PRIVATE_H */
190