eventlib_p.h revision 162541
1/*
2 * Copyright (c) 2005 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1995-1999 by Internet Software Consortium
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* eventlib_p.h - private interfaces for eventlib
19 * vix 09sep95 [initial]
20 *
21 * $Id: eventlib_p.h,v 1.3.2.1.4.3 2005/07/28 07:43:20 marka Exp $
22 * $FreeBSD: head/lib/libc/isc/eventlib_p.h 162541 2006-09-22 01:52:26Z kan $
23 */
24
25#ifndef _EVENTLIB_P_H
26#define _EVENTLIB_P_H
27
28#include <sys/param.h>
29#include <sys/types.h>
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <sys/un.h>
33
34#define EVENTLIB_DEBUG 1
35
36#include <errno.h>
37#include <fcntl.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41
42#ifndef _LIBC
43#include <isc/list.h>
44#include <isc/heap.h>
45#include <isc/memcluster.h>
46#endif
47
48#define	EV_MASK_ALL	(EV_READ | EV_WRITE | EV_EXCEPT)
49#define EV_ERR(e)		return (errno = (e), -1)
50#define OK(x)		if ((x) < 0) EV_ERR(errno); else (void)NULL
51
52#define	NEW(p)		if (((p) = memget(sizeof *(p))) != NULL) \
53				FILL(p); \
54			else \
55				(void)NULL;
56#define OKNEW(p)	if (!((p) = memget(sizeof *(p)))) { \
57				errno = ENOMEM; \
58				return (-1); \
59			} else \
60				FILL(p)
61#define FREE(p)		memput((p), sizeof *(p))
62
63#if EVENTLIB_DEBUG
64#define FILL(p)		memset((p), 0xF5, sizeof *(p))
65#else
66#define FILL(p)
67#endif
68
69#ifdef USE_POLL
70#ifdef HAVE_STROPTS_H
71#include <stropts.h>
72#endif
73#include <poll.h>
74#endif /* USE_POLL */
75
76typedef struct evConn {
77	evConnFunc	func;
78	void *		uap;
79	int		fd;
80	int		flags;
81#define EV_CONN_LISTEN		0x0001		/* Connection is a listener. */
82#define EV_CONN_SELECTED	0x0002		/* evSelectFD(conn->file). */
83#define EV_CONN_BLOCK		0x0004		/* Listener fd was blocking. */
84	evFileID	file;
85	struct evConn *	prev;
86	struct evConn *	next;
87} evConn;
88
89#ifndef _LIBC
90typedef struct evAccept {
91	int		fd;
92	union {
93		struct sockaddr		sa;
94		struct sockaddr_in	in;
95#ifndef NO_SOCKADDR_UN
96		struct sockaddr_un	un;
97#endif
98	}		la;
99	ISC_SOCKLEN_T	lalen;
100	union {
101		struct sockaddr		sa;
102		struct sockaddr_in	in;
103#ifndef NO_SOCKADDR_UN
104		struct sockaddr_un	un;
105#endif
106	}		ra;
107	ISC_SOCKLEN_T	ralen;
108	int		ioErrno;
109	evConn *	conn;
110	LINK(struct evAccept) link;
111} evAccept;
112
113typedef struct evFile {
114	evFileFunc	func;
115	void *		uap;
116	int		fd;
117	int		eventmask;
118	int		preemptive;
119	struct evFile *	prev;
120	struct evFile *	next;
121	struct evFile *	fdprev;
122	struct evFile *	fdnext;
123} evFile;
124
125typedef struct evStream {
126	evStreamFunc	func;
127	void *		uap;
128	evFileID	file;
129	evTimerID	timer;
130	int		flags;
131#define EV_STR_TIMEROK	0x0001	/* IFF timer valid. */
132	int		fd;
133	struct iovec *	iovOrig;
134	int		iovOrigCount;
135	struct iovec *	iovCur;
136	int		iovCurCount;
137	int		ioTotal;
138	int		ioDone;
139	int		ioErrno;
140	struct evStream	*prevDone, *nextDone;
141	struct evStream	*prev, *next;
142} evStream;
143
144typedef struct evTimer {
145	evTimerFunc	func;
146	void *		uap;
147	struct timespec	due, inter;
148	int		index;
149	int		mode;
150#define EV_TMR_RATE	1
151} evTimer;
152
153typedef struct evWait {
154	evWaitFunc	func;
155	void *		uap;
156	const void *	tag;
157	struct evWait *	next;
158} evWait;
159
160typedef struct evWaitList {
161	evWait *		first;
162	evWait *		last;
163	struct evWaitList *	prev;
164	struct evWaitList *	next;
165} evWaitList;
166
167typedef struct evEvent_p {
168	enum {  Accept, File, Stream, Timer, Wait, Free, Null  } type;
169	union {
170		struct {  evAccept *this;  }			accept;
171		struct {  evFile *this; int eventmask;  }	file;
172		struct {  evStream *this;  }			stream;
173		struct {  evTimer *this;  }			timer;
174		struct {  evWait *this;  }			wait;
175		struct {  struct evEvent_p *next;  }		free;
176		struct {  const void *placeholder;  }		null;
177	} u;
178} evEvent_p;
179#endif
180
181#ifdef USE_POLL
182typedef struct {
183	void		*ctx;	/* pointer to the evContext_p   */
184	uint32_t	type;	/* READ, WRITE, EXCEPT, nonblk  */
185	uint32_t	result;	/* 1 => revents, 0 => events    */
186} __evEmulMask;
187
188#define emulMaskInit(ctx, field, ev, lastnext) \
189	ctx->field.ctx = ctx; \
190	ctx->field.type = ev; \
191	ctx->field.result = lastnext;
192
193extern short	*__fd_eventfield(int fd, __evEmulMask *maskp);
194extern short	__poll_event(__evEmulMask *maskp);
195extern void		__fd_clr(int fd, __evEmulMask *maskp);
196extern void		__fd_set(int fd, __evEmulMask *maskp);
197
198#undef  FD_ZERO
199#define FD_ZERO(maskp)
200
201#undef  FD_SET
202#define FD_SET(fd, maskp) \
203	__fd_set(fd, maskp)
204
205#undef  FD_CLR
206#define FD_CLR(fd, maskp) \
207	__fd_clr(fd, maskp)
208
209#undef  FD_ISSET
210#define FD_ISSET(fd, maskp) \
211	((*__fd_eventfield(fd, maskp) & __poll_event(maskp)) != 0)
212
213#endif /* USE_POLL */
214
215#ifndef _LIBC
216typedef struct {
217	/* Global. */
218	const evEvent_p	*cur;
219	/* Debugging. */
220	int		debug;
221	FILE		*output;
222	/* Connections. */
223	evConn		*conns;
224	LIST(evAccept)	accepts;
225	/* Files. */
226	evFile		*files, *fdNext;
227#ifndef USE_POLL
228	fd_set		rdLast, rdNext;
229	fd_set		wrLast, wrNext;
230	fd_set		exLast, exNext;
231	fd_set		nonblockBefore;
232	int		fdMax, fdCount, highestFD;
233	evFile		*fdTable[FD_SETSIZE];
234#else
235	struct pollfd	*pollfds;	/* Allocated as needed  */
236	evFile		**fdTable;	/* Ditto                */
237	int		maxnfds;	/* # elements in above  */
238	int		firstfd;	/* First active fd      */
239	int		fdMax;		/* Last active fd       */
240	int		fdCount;	/* # fd:s with I/O      */
241	int		highestFD;	/* max fd allowed by OS */
242	__evEmulMask	rdLast, rdNext;
243	__evEmulMask	wrLast, wrNext;
244	__evEmulMask	exLast, exNext;
245	__evEmulMask	nonblockBefore;
246#endif /* USE_POLL */
247#ifdef EVENTLIB_TIME_CHECKS
248	struct timespec	lastSelectTime;
249	int		lastFdCount;
250#endif
251	/* Streams. */
252	evStream	*streams;
253	evStream	*strDone, *strLast;
254	/* Timers. */
255	struct timespec	lastEventTime;
256	heap_context	timers;
257	/* Waits. */
258	evWaitList	*waitLists;
259	evWaitList	waitDone;
260} evContext_p;
261
262/* eventlib.c */
263#define evPrintf __evPrintf
264void evPrintf(const evContext_p *ctx, int level, const char *fmt, ...)
265     ISC_FORMAT_PRINTF(3, 4);
266
267#ifdef USE_POLL
268extern int evPollfdRealloc(evContext_p *ctx, int pollfd_chunk_size, int fd);
269#endif /* USE_POLL */
270
271/* ev_timers.c */
272#define evCreateTimers __evCreateTimers
273heap_context evCreateTimers(const evContext_p *);
274#define evDestroyTimers __evDestroyTimers
275void evDestroyTimers(const evContext_p *);
276
277/* ev_waits.c */
278#define evFreeWait __evFreeWait
279evWait *evFreeWait(evContext_p *ctx, evWait *old);
280#endif
281
282/* Global options */
283#ifndef _LIBC
284extern int	__evOptMonoTime;
285#endif
286
287#endif /*_EVENTLIB_P_H*/
288