| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /*	$NetBSD: ev_streams.c,v 1.2 2004/05/20 19:52:31 christos Exp $	*/ | 
 | 2 |  | 
 | 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 | #include <sys/cdefs.h> | 
 | 25 | #if !defined(LINT) && !defined(CODECENTER) && !defined(lint) | 
 | 26 | #ifdef notdef | 
 | 27 | static const char rcsid[] = "Id: ev_streams.c,v 1.2.206.2 2004/03/17 00:29:51 marka Exp"; | 
 | 28 | #else | 
 | 29 | __RCSID("$NetBSD: ev_streams.c,v 1.2 2004/05/20 19:52:31 christos Exp $"); | 
 | 30 | #endif | 
 | 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 | #include "eventlib_p.h" | 
 | 40 |  | 
 | 41 | #ifndef _LIBC | 
 | 42 | static int	copyvec(evStream *str, const struct iovec *iov, int iocnt); | 
 | 43 | static void	consume(evStream *str, size_t bytes); | 
 | 44 | static void	done(evContext opaqueCtx, evStream *str); | 
 | 45 | static void	writable(evContext opaqueCtx, void *uap, int fd, int evmask); | 
 | 46 | static void	readable(evContext opaqueCtx, void *uap, int fd, int evmask); | 
 | 47 | #endif | 
 | 48 |  | 
 | 49 | struct iovec | 
 | 50 | evConsIovec(void *buf, size_t cnt) { | 
 | 51 | 	struct iovec ret; | 
 | 52 |  | 
 | 53 | 	memset(&ret, 0xf5, sizeof ret); | 
 | 54 | 	ret.iov_base = buf; | 
 | 55 | 	ret.iov_len = cnt; | 
 | 56 | 	return (ret); | 
 | 57 | } | 
 | 58 |  | 
 | 59 | #ifndef _LIBC | 
 | 60 | int | 
 | 61 | evWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt, | 
 | 62 | 	evStreamFunc func, void *uap, evStreamID *id) | 
 | 63 | { | 
 | 64 | 	evContext_p *ctx = opaqueCtx.opaque; | 
 | 65 | 	evStream *new; | 
 | 66 | 	int save; | 
 | 67 |  | 
 | 68 | 	OKNEW(new); | 
 | 69 | 	new->func = func; | 
 | 70 | 	new->uap = uap; | 
 | 71 | 	new->fd = fd; | 
 | 72 | 	new->flags = 0; | 
 | 73 | 	if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0) | 
 | 74 | 		goto free; | 
 | 75 | 	if (copyvec(new, iov, iocnt) < 0) | 
 | 76 | 		goto free; | 
 | 77 | 	new->prevDone = NULL; | 
 | 78 | 	new->nextDone = NULL; | 
 | 79 | 	if (ctx->streams != NULL) | 
 | 80 | 		ctx->streams->prev = new; | 
 | 81 | 	new->prev = NULL; | 
 | 82 | 	new->next = ctx->streams; | 
 | 83 | 	ctx->streams = new; | 
 | 84 | 	if (id != NULL) | 
 | 85 | 		id->opaque = new; | 
 | 86 | 	return (0); | 
 | 87 |  free: | 
 | 88 | 	save = errno; | 
 | 89 | 	FREE(new); | 
 | 90 | 	errno = save; | 
 | 91 | 	return (-1); | 
 | 92 | } | 
 | 93 |  | 
 | 94 | int | 
 | 95 | evRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt, | 
 | 96 |        evStreamFunc func, void *uap, evStreamID *id) | 
 | 97 | { | 
 | 98 | 	evContext_p *ctx = opaqueCtx.opaque; | 
 | 99 | 	evStream *new; | 
 | 100 | 	int save; | 
 | 101 |  | 
 | 102 | 	OKNEW(new); | 
 | 103 | 	new->func = func; | 
 | 104 | 	new->uap = uap; | 
 | 105 | 	new->fd = fd; | 
 | 106 | 	new->flags = 0; | 
 | 107 | 	if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0) | 
 | 108 | 		goto free; | 
 | 109 | 	if (copyvec(new, iov, iocnt) < 0) | 
 | 110 | 		goto free; | 
 | 111 | 	new->prevDone = NULL; | 
 | 112 | 	new->nextDone = NULL; | 
 | 113 | 	if (ctx->streams != NULL) | 
 | 114 | 		ctx->streams->prev = new; | 
 | 115 | 	new->prev = NULL; | 
 | 116 | 	new->next = ctx->streams; | 
 | 117 | 	ctx->streams = new; | 
 | 118 | 	if (id) | 
 | 119 | 		id->opaque = new; | 
 | 120 | 	return (0); | 
 | 121 |  free: | 
 | 122 | 	save = errno; | 
 | 123 | 	FREE(new); | 
 | 124 | 	errno = save; | 
 | 125 | 	return (-1); | 
 | 126 | } | 
 | 127 |  | 
 | 128 | int | 
 | 129 | evTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ { | 
 | 130 | 	evStream *str = id.opaque; | 
 | 131 |  | 
 | 132 | 	UNUSED(opaqueCtx); | 
 | 133 |  | 
 | 134 | 	str->timer = timer; | 
 | 135 | 	str->flags |= EV_STR_TIMEROK; | 
 | 136 | 	return (0); | 
 | 137 | } | 
 | 138 |  | 
 | 139 | int | 
 | 140 | evUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ { | 
 | 141 | 	evStream *str = id.opaque; | 
 | 142 |  | 
 | 143 | 	UNUSED(opaqueCtx); | 
 | 144 |  | 
 | 145 | 	str->flags &= ~EV_STR_TIMEROK; | 
 | 146 | 	return (0); | 
 | 147 | } | 
 | 148 |  | 
 | 149 | int | 
 | 150 | evCancelRW(evContext opaqueCtx, evStreamID id) { | 
 | 151 | 	evContext_p *ctx = opaqueCtx.opaque; | 
 | 152 | 	evStream *old = id.opaque; | 
 | 153 |  | 
 | 154 | 	/* | 
 | 155 | 	 * The streams list is doubly threaded.  First, there's ctx->streams | 
 | 156 | 	 * that's used by evDestroy() to find and cancel all streams.  Second, | 
 | 157 | 	 * there's ctx->strDone (head) and ctx->strLast (tail) which thread | 
 | 158 | 	 * through the potentially smaller number of "IO completed" streams, | 
 | 159 | 	 * used in evGetNext() to avoid scanning the entire list. | 
 | 160 | 	 */ | 
 | 161 |  | 
 | 162 | 	/* Unlink from ctx->streams. */ | 
 | 163 | 	if (old->prev != NULL) | 
 | 164 | 		old->prev->next = old->next; | 
 | 165 | 	else | 
 | 166 | 		ctx->streams = old->next; | 
 | 167 | 	if (old->next != NULL) | 
 | 168 | 		old->next->prev = old->prev; | 
 | 169 |  | 
 | 170 | 	/* | 
 | 171 | 	 * If 'old' is on the ctx->strDone list, remove it.  Update | 
 | 172 | 	 * ctx->strLast if necessary. | 
 | 173 | 	 */ | 
 | 174 | 	if (old->prevDone == NULL && old->nextDone == NULL) { | 
 | 175 | 		/* | 
 | 176 | 		 * Either 'old' is the only item on the done list, or it's | 
 | 177 | 		 * not on the done list.  If the former, then we unlink it | 
 | 178 | 		 * from the list.  If the latter, we leave the list alone. | 
 | 179 | 		 */ | 
 | 180 | 		if (ctx->strDone == old) { | 
 | 181 | 			ctx->strDone = NULL; | 
 | 182 | 			ctx->strLast = NULL; | 
 | 183 | 		} | 
 | 184 | 	} else { | 
 | 185 | 		if (old->prevDone != NULL) | 
 | 186 | 			old->prevDone->nextDone = old->nextDone; | 
 | 187 | 		else | 
 | 188 | 			ctx->strDone = old->nextDone; | 
 | 189 | 		if (old->nextDone != NULL) | 
 | 190 | 			old->nextDone->prevDone = old->prevDone; | 
 | 191 | 		else | 
 | 192 | 			ctx->strLast = old->prevDone; | 
 | 193 | 	} | 
 | 194 |  | 
 | 195 | 	/* Deallocate the stream. */ | 
 | 196 | 	if (old->file.opaque) | 
 | 197 | 		evDeselectFD(opaqueCtx, old->file); | 
 | 198 | 	memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount); | 
 | 199 | 	FREE(old); | 
 | 200 | 	return (0); | 
 | 201 | } | 
 | 202 |  | 
 | 203 | /* Copy a scatter/gather vector and initialize a stream handler's IO. */ | 
 | 204 | static int | 
 | 205 | copyvec(evStream *str, const struct iovec *iov, int iocnt) { | 
 | 206 | 	int i; | 
 | 207 |  | 
 | 208 | 	str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt); | 
 | 209 | 	if (str->iovOrig == NULL) { | 
 | 210 | 		errno = ENOMEM; | 
 | 211 | 		return (-1); | 
 | 212 | 	} | 
 | 213 | 	str->ioTotal = 0; | 
 | 214 | 	for (i = 0; i < iocnt; i++) { | 
 | 215 | 		str->iovOrig[i] = iov[i]; | 
 | 216 | 		str->ioTotal += iov[i].iov_len; | 
 | 217 | 	} | 
 | 218 | 	str->iovOrigCount = iocnt; | 
 | 219 | 	str->iovCur = str->iovOrig; | 
 | 220 | 	str->iovCurCount = str->iovOrigCount; | 
 | 221 | 	str->ioDone = 0; | 
 | 222 | 	return (0); | 
 | 223 | } | 
 | 224 |  | 
 | 225 | /* Pull off or truncate lead iovec(s). */ | 
 | 226 | static void | 
 | 227 | consume(evStream *str, size_t bytes) { | 
 | 228 | 	while (bytes > 0U) { | 
 | 229 | 		if (bytes < (size_t)str->iovCur->iov_len) { | 
 | 230 | 			str->iovCur->iov_len -= bytes; | 
 | 231 | 			str->iovCur->iov_base = (void *) | 
 | 232 | 				((u_char *)str->iovCur->iov_base + bytes); | 
 | 233 | 			str->ioDone += bytes; | 
 | 234 | 			bytes = 0; | 
 | 235 | 		} else { | 
 | 236 | 			bytes -= str->iovCur->iov_len; | 
 | 237 | 			str->ioDone += str->iovCur->iov_len; | 
 | 238 | 			str->iovCur++; | 
 | 239 | 			str->iovCurCount--; | 
 | 240 | 		} | 
 | 241 | 	} | 
 | 242 | } | 
 | 243 |  | 
 | 244 | /* Add a stream to Done list and deselect the FD. */ | 
 | 245 | static void | 
 | 246 | done(evContext opaqueCtx, evStream *str) { | 
 | 247 | 	evContext_p *ctx = opaqueCtx.opaque; | 
 | 248 |  | 
 | 249 | 	if (ctx->strLast != NULL) { | 
 | 250 | 		str->prevDone = ctx->strLast; | 
 | 251 | 		ctx->strLast->nextDone = str; | 
 | 252 | 		ctx->strLast = str; | 
 | 253 | 	} else { | 
 | 254 | 		INSIST(ctx->strDone == NULL); | 
 | 255 | 		ctx->strDone = ctx->strLast = str; | 
 | 256 | 	} | 
 | 257 | 	evDeselectFD(opaqueCtx, str->file); | 
 | 258 | 	str->file.opaque = NULL; | 
 | 259 | 	/* evDrop() will call evCancelRW() on us. */ | 
 | 260 | } | 
 | 261 |  | 
 | 262 | /* Dribble out some bytes on the stream.  (Called by evDispatch().) */ | 
 | 263 | static void | 
 | 264 | writable(evContext opaqueCtx, void *uap, int fd, int evmask) { | 
 | 265 | 	evStream *str = uap; | 
 | 266 | 	int bytes; | 
 | 267 |  | 
 | 268 | 	UNUSED(evmask); | 
 | 269 |  | 
 | 270 | 	bytes = writev(fd, str->iovCur, str->iovCurCount); | 
 | 271 | 	if (bytes > 0) { | 
 | 272 | 		if ((str->flags & EV_STR_TIMEROK) != 0) | 
 | 273 | 			evTouchIdleTimer(opaqueCtx, str->timer); | 
 | 274 | 		consume(str, bytes); | 
 | 275 | 	} else { | 
 | 276 | 		if (bytes < 0 && errno != EINTR) { | 
 | 277 | 			str->ioDone = -1; | 
 | 278 | 			str->ioErrno = errno; | 
 | 279 | 		} | 
 | 280 | 	} | 
 | 281 | 	if (str->ioDone == -1 || str->ioDone == str->ioTotal) | 
 | 282 | 		done(opaqueCtx, str); | 
 | 283 | } | 
 | 284 |  | 
 | 285 | /* Scoop up some bytes from the stream.  (Called by evDispatch().) */ | 
 | 286 | static void | 
 | 287 | readable(evContext opaqueCtx, void *uap, int fd, int evmask) { | 
 | 288 | 	evStream *str = uap; | 
 | 289 | 	int bytes; | 
 | 290 |  | 
 | 291 | 	UNUSED(evmask); | 
 | 292 |  | 
 | 293 | 	bytes = readv(fd, str->iovCur, str->iovCurCount); | 
 | 294 | 	if (bytes > 0) { | 
 | 295 | 		if ((str->flags & EV_STR_TIMEROK) != 0) | 
 | 296 | 			evTouchIdleTimer(opaqueCtx, str->timer); | 
 | 297 | 		consume(str, bytes); | 
 | 298 | 	} else { | 
 | 299 | 		if (bytes == 0) | 
 | 300 | 			str->ioDone = 0; | 
 | 301 | 		else { | 
 | 302 | 			if (errno != EINTR) { | 
 | 303 | 				str->ioDone = -1; | 
 | 304 | 				str->ioErrno = errno; | 
 | 305 | 			} | 
 | 306 | 		} | 
 | 307 | 	} | 
 | 308 | 	if (str->ioDone <= 0 || str->ioDone == str->ioTotal) | 
 | 309 | 		done(opaqueCtx, str); | 
 | 310 | } | 
 | 311 | #endif |