other-licenses/android/ev_streams.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /*	$NetBSD: ev_streams.c,v 1.2 2004/05/20 19:52:31 christos Exp $	*/
     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  */
    20 /* ev_streams.c - implement asynch stream file IO for the eventlib
    21  * vix 04mar96 [initial]
    22  */
    24 /*
    25  * This version of this file is derived from Android 2.3 "Gingerbread",
    26  * which contains uncredited changes by Android/Google developers.  It has
    27  * been modified in 2011 for use in the Android build of Mozilla Firefox by
    28  * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
    29  * and Steve Workman <sjhworkman@gmail.com>).
    30  * These changes are offered under the same license as the original NetBSD
    31  * file, whose copyright and license are unchanged above.
    32  */
    34 #define ANDROID_CHANGES 1
    35 #define MOZILLA_NECKO_EXCLUDE_CODE 1
    37 #include <sys/cdefs.h>
    38 #if !defined(LINT) && !defined(CODECENTER) && !defined(lint)
    39 #ifdef notdef
    40 static const char rcsid[] = "Id: ev_streams.c,v 1.2.206.2 2004/03/17 00:29:51 marka Exp";
    41 #else
    42 __RCSID("$NetBSD: ev_streams.c,v 1.2 2004/05/20 19:52:31 christos Exp $");
    43 #endif
    44 #endif
    46 #include <sys/types.h>
    47 #include <sys/uio.h>
    49 #include <errno.h>
    51 #include "eventlib.h"
    52 #include "eventlib_p.h"
    54 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
    55 #ifndef _LIBC
    56 static int	copyvec(evStream *str, const struct iovec *iov, int iocnt);
    57 static void	consume(evStream *str, size_t bytes);
    58 static void	done(evContext opaqueCtx, evStream *str);
    59 static void	writable(evContext opaqueCtx, void *uap, int fd, int evmask);
    60 static void	readable(evContext opaqueCtx, void *uap, int fd, int evmask);
    61 #endif
    62 #endif
    64 struct iovec
    65 evConsIovec(void *buf, size_t cnt) {
    66 	struct iovec ret;
    68 	memset(&ret, 0xf5, sizeof ret);
    69 	ret.iov_base = buf;
    70 	ret.iov_len = cnt;
    71 	return (ret);
    72 }
    74 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
    75 #ifndef _LIBC
    76 int
    77 evWrite(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
    78 	evStreamFunc func, void *uap, evStreamID *id)
    79 {
    80 	evContext_p *ctx = opaqueCtx.opaque;
    81 	evStream *new;
    82 	int save;
    84 	OKNEW(new);
    85 	new->func = func;
    86 	new->uap = uap;
    87 	new->fd = fd;
    88 	new->flags = 0;
    89 	if (evSelectFD(opaqueCtx, fd, EV_WRITE, writable, new, &new->file) < 0)
    90 		goto free;
    91 	if (copyvec(new, iov, iocnt) < 0)
    92 		goto free;
    93 	new->prevDone = NULL;
    94 	new->nextDone = NULL;
    95 	if (ctx->streams != NULL)
    96 		ctx->streams->prev = new;
    97 	new->prev = NULL;
    98 	new->next = ctx->streams;
    99 	ctx->streams = new;
   100 	if (id != NULL)
   101 		id->opaque = new;
   102 	return (0);
   103  free:
   104 	save = errno;
   105 	FREE(new);
   106 	errno = save;
   107 	return (-1);
   108 }
   110 int
   111 evRead(evContext opaqueCtx, int fd, const struct iovec *iov, int iocnt,
   112        evStreamFunc func, void *uap, evStreamID *id)
   113 {
   114 	evContext_p *ctx = opaqueCtx.opaque;
   115 	evStream *new;
   116 	int save;
   118 	OKNEW(new);
   119 	new->func = func;
   120 	new->uap = uap;
   121 	new->fd = fd;
   122 	new->flags = 0;
   123 	if (evSelectFD(opaqueCtx, fd, EV_READ, readable, new, &new->file) < 0)
   124 		goto free;
   125 	if (copyvec(new, iov, iocnt) < 0)
   126 		goto free;
   127 	new->prevDone = NULL;
   128 	new->nextDone = NULL;
   129 	if (ctx->streams != NULL)
   130 		ctx->streams->prev = new;
   131 	new->prev = NULL;
   132 	new->next = ctx->streams;
   133 	ctx->streams = new;
   134 	if (id)
   135 		id->opaque = new;
   136 	return (0);
   137  free:
   138 	save = errno;
   139 	FREE(new);
   140 	errno = save;
   141 	return (-1);
   142 }
   144 int
   145 evTimeRW(evContext opaqueCtx, evStreamID id, evTimerID timer) /*ARGSUSED*/ {
   146 	evStream *str = id.opaque;
   148 	UNUSED(opaqueCtx);
   150 	str->timer = timer;
   151 	str->flags |= EV_STR_TIMEROK;
   152 	return (0);
   153 }
   155 int
   156 evUntimeRW(evContext opaqueCtx, evStreamID id) /*ARGSUSED*/ {
   157 	evStream *str = id.opaque;
   159 	UNUSED(opaqueCtx);
   161 	str->flags &= ~EV_STR_TIMEROK;
   162 	return (0);
   163 }
   165 int
   166 evCancelRW(evContext opaqueCtx, evStreamID id) {
   167 	evContext_p *ctx = opaqueCtx.opaque;
   168 	evStream *old = id.opaque;
   170 	/*
   171 	 * The streams list is doubly threaded.  First, there's ctx->streams
   172 	 * that's used by evDestroy() to find and cancel all streams.  Second,
   173 	 * there's ctx->strDone (head) and ctx->strLast (tail) which thread
   174 	 * through the potentially smaller number of "IO completed" streams,
   175 	 * used in evGetNext() to avoid scanning the entire list.
   176 	 */
   178 	/* Unlink from ctx->streams. */
   179 	if (old->prev != NULL)
   180 		old->prev->next = old->next;
   181 	else
   182 		ctx->streams = old->next;
   183 	if (old->next != NULL)
   184 		old->next->prev = old->prev;
   186 	/*
   187 	 * If 'old' is on the ctx->strDone list, remove it.  Update
   188 	 * ctx->strLast if necessary.
   189 	 */
   190 	if (old->prevDone == NULL && old->nextDone == NULL) {
   191 		/*
   192 		 * Either 'old' is the only item on the done list, or it's
   193 		 * not on the done list.  If the former, then we unlink it
   194 		 * from the list.  If the latter, we leave the list alone.
   195 		 */
   196 		if (ctx->strDone == old) {
   197 			ctx->strDone = NULL;
   198 			ctx->strLast = NULL;
   199 		}
   200 	} else {
   201 		if (old->prevDone != NULL)
   202 			old->prevDone->nextDone = old->nextDone;
   203 		else
   204 			ctx->strDone = old->nextDone;
   205 		if (old->nextDone != NULL)
   206 			old->nextDone->prevDone = old->prevDone;
   207 		else
   208 			ctx->strLast = old->prevDone;
   209 	}
   211 	/* Deallocate the stream. */
   212 	if (old->file.opaque)
   213 		evDeselectFD(opaqueCtx, old->file);
   214 	memput(old->iovOrig, sizeof (struct iovec) * old->iovOrigCount);
   215 	FREE(old);
   216 	return (0);
   217 }
   219 /* Copy a scatter/gather vector and initialize a stream handler's IO. */
   220 static int
   221 copyvec(evStream *str, const struct iovec *iov, int iocnt) {
   222 	int i;
   224 	str->iovOrig = (struct iovec *)memget(sizeof(struct iovec) * iocnt);
   225 	if (str->iovOrig == NULL) {
   226 		errno = ENOMEM;
   227 		return (-1);
   228 	}
   229 	str->ioTotal = 0;
   230 	for (i = 0; i < iocnt; i++) {
   231 		str->iovOrig[i] = iov[i];
   232 		str->ioTotal += iov[i].iov_len;
   233 	}
   234 	str->iovOrigCount = iocnt;
   235 	str->iovCur = str->iovOrig;
   236 	str->iovCurCount = str->iovOrigCount;
   237 	str->ioDone = 0;
   238 	return (0);
   239 }
   241 /* Pull off or truncate lead iovec(s). */
   242 static void
   243 consume(evStream *str, size_t bytes) {
   244 	while (bytes > 0U) {
   245 		if (bytes < (size_t)str->iovCur->iov_len) {
   246 			str->iovCur->iov_len -= bytes;
   247 			str->iovCur->iov_base = (void *)
   248 				((u_char *)str->iovCur->iov_base + bytes);
   249 			str->ioDone += bytes;
   250 			bytes = 0;
   251 		} else {
   252 			bytes -= str->iovCur->iov_len;
   253 			str->ioDone += str->iovCur->iov_len;
   254 			str->iovCur++;
   255 			str->iovCurCount--;
   256 		}
   257 	}
   258 }
   260 /* Add a stream to Done list and deselect the FD. */
   261 static void
   262 done(evContext opaqueCtx, evStream *str) {
   263 	evContext_p *ctx = opaqueCtx.opaque;
   265 	if (ctx->strLast != NULL) {
   266 		str->prevDone = ctx->strLast;
   267 		ctx->strLast->nextDone = str;
   268 		ctx->strLast = str;
   269 	} else {
   270 		INSIST(ctx->strDone == NULL);
   271 		ctx->strDone = ctx->strLast = str;
   272 	}
   273 	evDeselectFD(opaqueCtx, str->file);
   274 	str->file.opaque = NULL;
   275 	/* evDrop() will call evCancelRW() on us. */
   276 }
   278 /* Dribble out some bytes on the stream.  (Called by evDispatch().) */
   279 static void
   280 writable(evContext opaqueCtx, void *uap, int fd, int evmask) {
   281 	evStream *str = uap;
   282 	int bytes;
   284 	UNUSED(evmask);
   286 	bytes = writev(fd, str->iovCur, str->iovCurCount);
   287 	if (bytes > 0) {
   288 		if ((str->flags & EV_STR_TIMEROK) != 0)
   289 			evTouchIdleTimer(opaqueCtx, str->timer);
   290 		consume(str, bytes);
   291 	} else {
   292 		if (bytes < 0 && errno != EINTR) {
   293 			str->ioDone = -1;
   294 			str->ioErrno = errno;
   295 		}
   296 	}
   297 	if (str->ioDone == -1 || str->ioDone == str->ioTotal)
   298 		done(opaqueCtx, str);
   299 }
   301 /* Scoop up some bytes from the stream.  (Called by evDispatch().) */
   302 static void
   303 readable(evContext opaqueCtx, void *uap, int fd, int evmask) {
   304 	evStream *str = uap;
   305 	int bytes;
   307 	UNUSED(evmask);
   309 	bytes = readv(fd, str->iovCur, str->iovCurCount);
   310 	if (bytes > 0) {
   311 		if ((str->flags & EV_STR_TIMEROK) != 0)
   312 			evTouchIdleTimer(opaqueCtx, str->timer);
   313 		consume(str, bytes);
   314 	} else {
   315 		if (bytes == 0)
   316 			str->ioDone = 0;
   317 		else {
   318 			if (errno != EINTR) {
   319 				str->ioDone = -1;
   320 				str->ioErrno = errno;
   321 			}
   322 		}
   323 	}
   324 	if (str->ioDone <= 0 || str->ioDone == str->ioTotal)
   325 		done(opaqueCtx, str);
   326 }
   327 #endif
   328 #endif

mercurial