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