xref: /freebsd/lib/libc/isc/ev_streams.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (c) 1996-1999 by Internet Software Consortium
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* ev_streams.c - implement asynch stream file IO for the eventlib
21  * vix 04mar96 [initial]
22  */
23 
24 #if !defined(LINT) && !defined(CODECENTER)
25 static const char rcsid[] = "$Id: ev_streams.c,v 1.5 2005/04/27 04:56:36 sra Exp $";
26 #endif
27 #include <sys/cdefs.h>
28 #include "port_before.h"
29 #ifndef _LIBC
30 #include "fd_setsize.h"
31 #endif
32 
33 #include <sys/types.h>
34 #include <sys/uio.h>
35 
36 #include <errno.h>
37 
38 #include <isc/eventlib.h>
39 #ifndef _LIBC
40 #include <isc/assertions.h>
41 #endif
42 #include "eventlib_p.h"
43 
44 #include "port_after.h"
45 
46 #ifndef _LIBC
47 static int	copyvec(evStream *str, const struct iovec *iov, int iocnt);
48 static void	consume(evStream *str, size_t bytes);
49 static void	done(evContext opaqueCtx, evStream *str);
50 static void	writable(evContext opaqueCtx, void *uap, int fd, int evmask);
51 static void	readable(evContext opaqueCtx, void *uap, int fd, int evmask);
52 #endif
53 
54 struct iovec
55 evConsIovec(void *buf, size_t cnt) {
56 	struct iovec ret;
57 
58 	memset(&ret, 0xf5, sizeof ret);
59 	ret.iov_base = buf;
60 	ret.iov_len = cnt;
61 	return (ret);
62 }
63 
64 #ifndef _LIBC
65 int
66 evWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
67 	evStreamFunc func, void *uap, evStreamID *id)
68 {
69 	evContext_p *ctx = opaqueCtx.opaque;
70 	evStream *new;
71 	int save;
72 
73 	OKNEW(new);
74 	new->func = func;
75 	new->uap = uap;
76 	new->fd = fd;
77 	new->flags = 0;
78 	if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0)
79 		goto free;
80 	if (copyvec(new, iov, iocnt) < 0)
81 		goto free;
82 	new->prevDone = NULL;
83 	new->nextDone = NULL;
84 	if (ctx->streams != NULL)
85 		ctx->streams->prev = new;
86 	new->prev = NULL;
87 	new->next = ctx->streams;
88 	ctx->streams = new;
89 	if (id != NULL)
90 		id->opaque = new;
91 	return (0);
92  free:
93 	save = errno;
94 	FREE(new);
95 	errno = save;
96 	return (-1);
97 }
98 
99 int
100 evRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
101        evStreamFunc func, void *uap, evStreamID *id)
102 {
103 	evContext_p *ctx = opaqueCtx.opaque;
104 	evStream *new;
105 	int save;
106 
107 	OKNEW(new);
108 	new->func = func;
109 	new->uap = uap;
110 	new->fd = fd;
111 	new->flags = 0;
112 	if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0)
113 		goto free;
114 	if (copyvec(new, iov, iocnt) < 0)
115 		goto free;
116 	new->prevDone = NULL;
117 	new->nextDone = NULL;
118 	if (ctx->streams != NULL)
119 		ctx->streams->prev = new;
120 	new->prev = NULL;
121 	new->next = ctx->streams;
122 	ctx->streams = new;
123 	if (id)
124 		id->opaque = new;
125 	return (0);
126  free:
127 	save = errno;
128 	FREE(new);
129 	errno = save;
130 	return (-1);
131 }
132 
133 int
134 evTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ {
135 	evStream *str = id.opaque;
136 
137 	UNUSED(opaqueCtx);
138 
139 	str->timer = timer;
140 	str->flags |= EV_STR_TIMEROK;
141 	return (0);
142 }
143 
144 int
145 evUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ {
146 	evStream *str = id.opaque;
147 
148 	UNUSED(opaqueCtx);
149 
150 	str->flags &= ~EV_STR_TIMEROK;
151 	return (0);
152 }
153 
154 int
155 evCancelRW(evContext opaqueCtx, evStreamID id) {
156 	evContext_p *ctx = opaqueCtx.opaque;
157 	evStream *old = id.opaque;
158 
159 	/*
160 	 * The streams list is doubly threaded.  First, there's ctx->streams
161 	 * that's used by evDestroy() to find and cancel all streams.  Second,
162 	 * there's ctx->strDone (head) and ctx->strLast (tail) which thread
163 	 * through the potentially smaller number of "IO completed" streams,
164 	 * used in evGetNext() to avoid scanning the entire list.
165 	 */
166 
167 	/* Unlink from ctx->streams. */
168 	if (old->prev != NULL)
169 		old->prev->next = old->next;
170 	else
171 		ctx->streams = old->next;
172 	if (old->next != NULL)
173 		old->next->prev = old->prev;
174 
175 	/*
176 	 * If 'old' is on the ctx->strDone list, remove it.  Update
177 	 * ctx->strLast if necessary.
178 	 */
179 	if (old->prevDone == NULL && old->nextDone == NULL) {
180 		/*
181 		 * Either 'old' is the only item on the done list, or it's
182 		 * not on the done list.  If the former, then we unlink it
183 		 * from the list.  If the latter, we leave the list alone.
184 		 */
185 		if (ctx->strDone == old) {
186 			ctx->strDone = NULL;
187 			ctx->strLast = NULL;
188 		}
189 	} else {
190 		if (old->prevDone != NULL)
191 			old->prevDone->nextDone = old->nextDone;
192 		else
193 			ctx->strDone = old->nextDone;
194 		if (old->nextDone != NULL)
195 			old->nextDone->prevDone = old->prevDone;
196 		else
197 			ctx->strLast = old->prevDone;
198 	}
199 
200 	/* Deallocate the stream. */
201 	if (old->file.opaque)
202 		evDeselectFD(opaqueCtx, old->file);
203 	memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount);
204 	FREE(old);
205 	return (0);
206 }
207 
208 /* Copy a scatter/gather vector and initialize a stream handler's IO. */
209 static int
210 copyvec(evStream *str, const struct iovec *iov, int iocnt) {
211 	int i;
212 
213 	str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt);
214 	if (str->iovOrig == NULL) {
215 		errno = ENOMEM;
216 		return (-1);
217 	}
218 	str->ioTotal = 0;
219 	for (i = 0; i < iocnt; i++) {
220 		str->iovOrig[i] = iov[i];
221 		str->ioTotal += iov[i].iov_len;
222 	}
223 	str->iovOrigCount = iocnt;
224 	str->iovCur = str->iovOrig;
225 	str->iovCurCount = str->iovOrigCount;
226 	str->ioDone = 0;
227 	return (0);
228 }
229 
230 /* Pull off or truncate lead iovec(s). */
231 static void
232 consume(evStream *str, size_t bytes) {
233 	while (bytes > 0U) {
234 		if (bytes < (size_t)str->iovCur->iov_len) {
235 			str->iovCur->iov_len -= bytes;
236 			str->iovCur->iov_base = (void *)
237 				((u_char *)str->iovCur->iov_base + bytes);
238 			str->ioDone += bytes;
239 			bytes = 0;
240 		} else {
241 			bytes -= str->iovCur->iov_len;
242 			str->ioDone += str->iovCur->iov_len;
243 			str->iovCur++;
244 			str->iovCurCount--;
245 		}
246 	}
247 }
248 
249 /* Add a stream to Done list and deselect the FD. */
250 static void
251 done(evContext opaqueCtx, evStream *str) {
252 	evContext_p *ctx = opaqueCtx.opaque;
253 
254 	if (ctx->strLast != NULL) {
255 		str->prevDone = ctx->strLast;
256 		ctx->strLast->nextDone = str;
257 		ctx->strLast = str;
258 	} else {
259 		INSIST(ctx->strDone == NULL);
260 		ctx->strDone = ctx->strLast = str;
261 	}
262 	evDeselectFD(opaqueCtx, str->file);
263 	str->file.opaque = NULL;
264 	/* evDrop() will call evCancelRW() on us. */
265 }
266 
267 /* Dribble out some bytes on the stream.  (Called by evDispatch().) */
268 static void
269 writable(evContext opaqueCtx, void *uap, int fd, int evmask) {
270 	evStream *str = uap;
271 	int bytes;
272 
273 	UNUSED(evmask);
274 
275 	bytes = writev(fd, str->iovCur, str->iovCurCount);
276 	if (bytes > 0) {
277 		if ((str->flags & EV_STR_TIMEROK) != 0)
278 			evTouchIdleTimer(opaqueCtx, str->timer);
279 		consume(str, bytes);
280 	} else {
281 		if (bytes < 0 && errno != EINTR) {
282 			str->ioDone = -1;
283 			str->ioErrno = errno;
284 		}
285 	}
286 	if (str->ioDone == -1 || str->ioDone == str->ioTotal)
287 		done(opaqueCtx, str);
288 }
289 
290 /* Scoop up some bytes from the stream.  (Called by evDispatch().) */
291 static void
292 readable(evContext opaqueCtx, void *uap, int fd, int evmask) {
293 	evStream *str = uap;
294 	int bytes;
295 
296 	UNUSED(evmask);
297 
298 	bytes = readv(fd, str->iovCur, str->iovCurCount);
299 	if (bytes > 0) {
300 		if ((str->flags & EV_STR_TIMEROK) != 0)
301 			evTouchIdleTimer(opaqueCtx, str->timer);
302 		consume(str, bytes);
303 	} else {
304 		if (bytes == 0)
305 			str->ioDone = 0;
306 		else {
307 			if (errno != EINTR) {
308 				str->ioDone = -1;
309 				str->ioErrno = errno;
310 			}
311 		}
312 	}
313 	if (str->ioDone <= 0 || str->ioDone == str->ioTotal)
314 		done(opaqueCtx, str);
315 }
316 #endif
317 
318 /*! \file */
319